2 * Copyright (C) 2012 The Android Open Source Project
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 package com.android.gallery3d.filtershow.editors;
19 import android.content.Context;
20 import android.content.res.Configuration;
21 import android.util.AttributeSet;
22 import android.view.LayoutInflater;
23 import android.view.Menu;
24 import android.view.MenuItem;
25 import android.view.View;
26 import android.view.ViewGroup;
27 import android.widget.Button;
28 import android.widget.FrameLayout;
29 import android.widget.LinearLayout;
30 import android.widget.PopupMenu;
31 import android.widget.SeekBar;
32 import android.widget.SeekBar.OnSeekBarChangeListener;
34 import com.android.gallery3d.R;
35 import com.android.gallery3d.filtershow.controller.Control;
36 import com.android.gallery3d.filtershow.filters.FilterRepresentation;
37 import com.android.gallery3d.filtershow.imageshow.ImageShow;
38 import com.android.gallery3d.filtershow.imageshow.MasterImage;
39 import com.android.gallery3d.filtershow.pipeline.ImagePreset;
41 import java.util.ArrayList;
42 import java.util.Collection;
45 * Base class for Editors Must contain a mImageShow and a top level view
47 public class Editor implements OnSeekBarChangeListener, SwapButton.SwapButtonListener {
48 protected Context mContext;
50 protected ImageShow mImageShow;
51 protected FrameLayout mFrameLayout;
52 protected SeekBar mSeekBar;
54 protected Button mFilterTitle;
56 private final String LOGTAG = "Editor";
57 protected boolean mChangesGeometry = false;
58 protected FilterRepresentation mLocalRepresentation = null;
59 protected byte mShowParameter = SHOW_VALUE_UNDEFINED;
60 private Button mButton;
61 public static byte SHOW_VALUE_UNDEFINED = -1;
62 public static byte SHOW_VALUE_OFF = 0;
63 public static byte SHOW_VALUE_INT = 1;
65 public static void hackFixStrings(Menu menu) {
66 int count = menu.size();
67 for (int i = 0; i < count; i++) {
68 MenuItem item = menu.getItem(i);
69 item.setTitle(item.getTitle().toString().toUpperCase());
73 public String calculateUserMessage(Context context, String effectName, Object parameterValue) {
74 return effectName.toUpperCase() + " " + parameterValue;
77 protected Editor(int id) {
85 public byte showParameterValue() {
86 return mShowParameter;
89 public boolean showsSeekBar() {
93 public void setUpEditorUI(View actionButton, View editControl,
94 Button editTitle, Button stateButton) {
95 mEditTitle = editTitle;
96 mFilterTitle = stateButton;
98 MasterImage.getImage().resetGeometryImages();
100 setUtilityPanelUI(actionButton, editControl);
103 public boolean showsPopupIndicator() {
108 * @param actionButton the would be the area for menu etc
109 * @param editControl this is the black area for sliders etc
111 public void setUtilityPanelUI(View actionButton, View editControl) {
114 Context context = editControl.getContext();
115 LayoutInflater inflater =
116 (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
117 LinearLayout lp = (LinearLayout) inflater.inflate(
118 R.layout.filtershow_seekbar, (ViewGroup) editControl, true);
119 mSeekBar = (SeekBar) lp.findViewById(R.id.primarySeekBar);
120 mSeekBar.setOnSeekBarChangeListener(this);
121 mSeekBar.setVisibility(View.GONE);
122 if (context.getResources().getConfiguration().orientation
123 == Configuration.ORIENTATION_PORTRAIT) {
124 if (showsSeekBar()) {
125 mSeekBar.setVisibility(View.VISIBLE);
129 if (mButton != null) {
130 if (showsPopupIndicator()) {
131 mButton.setCompoundDrawablesRelativeWithIntrinsicBounds(0, 0,
132 R.drawable.filtershow_menu_marker, 0);
134 mButton.setCompoundDrawablesRelativeWithIntrinsicBounds(0, 0, 0, 0);
140 public void onProgressChanged(SeekBar sbar, int progress, boolean arg2) {
144 public void setPanel() {
148 public void createEditor(Context context, FrameLayout frameLayout) {
150 mFrameLayout = frameLayout;
151 mLocalRepresentation = null;
154 protected void unpack(int viewid, int layoutid) {
157 mView = mFrameLayout.findViewById(viewid);
159 LayoutInflater inflater = (LayoutInflater) mContext.getSystemService
160 (Context.LAYOUT_INFLATER_SERVICE);
161 mView = inflater.inflate(layoutid, mFrameLayout, false);
162 mFrameLayout.addView(mView, mView.getLayoutParams());
165 mImageShow = findImageShow(mView);
168 private ImageShow findImageShow(View view) {
169 if (view instanceof ImageShow) {
170 return (ImageShow) view;
172 if (!(view instanceof ViewGroup)) {
175 ViewGroup vg = (ViewGroup) view;
176 int n = vg.getChildCount();
177 for (int i = 0; i < n; i++) {
178 View v = vg.getChildAt(i);
179 if (v instanceof ImageShow) {
180 return (ImageShow) v;
181 } else if (v instanceof ViewGroup) {
182 return findImageShow(v);
188 public View getTopLevelView() {
192 public ImageShow getImageShow() {
196 public void setVisibility(int visible) {
197 mView.setVisibility(visible);
200 public FilterRepresentation getLocalRepresentation() {
201 if (mLocalRepresentation == null) {
202 ImagePreset preset = MasterImage.getImage().getPreset();
203 FilterRepresentation filterRepresentation = MasterImage.getImage().getCurrentFilterRepresentation();
204 mLocalRepresentation = preset.getFilterRepresentationCopyFrom(filterRepresentation);
205 if (mShowParameter == SHOW_VALUE_UNDEFINED && filterRepresentation != null) {
206 boolean show = filterRepresentation.showParameterValue();
207 mShowParameter = show ? SHOW_VALUE_INT : SHOW_VALUE_OFF;
211 return mLocalRepresentation;
215 * Call this to update the preset in MasterImage with the current representation
216 * returned by getLocalRepresentation. This causes the preview bitmap to be
219 public void commitLocalRepresentation() {
220 commitLocalRepresentation(getLocalRepresentation());
224 * Call this to update the preset in MasterImage with a given representation.
225 * This causes the preview bitmap to be regenerated.
227 public void commitLocalRepresentation(FilterRepresentation rep) {
228 ArrayList<FilterRepresentation> filter = new ArrayList<FilterRepresentation>(1);
230 commitLocalRepresentation(filter);
234 * Call this to update the preset in MasterImage with a collection of FilterRepresentations.
235 * This causes the preview bitmap to be regenerated.
237 public void commitLocalRepresentation(Collection<FilterRepresentation> reps) {
238 ImagePreset preset = MasterImage.getImage().getPreset();
239 preset.updateFilterRepresentations(reps);
240 if (mButton != null) {
243 if (mChangesGeometry) {
244 // Regenerate both the filtered and the geometry-only bitmaps
245 MasterImage.getImage().resetGeometryImages();
247 // Regenerate the filtered bitmap.
248 MasterImage.getImage().invalidateFiltersOnly();
249 preset.fillImageStateAdapter(MasterImage.getImage().getState());
253 * This is called in response to a click to apply and leave the editor.
255 public void finalApplyCalled() {
256 commitLocalRepresentation();
259 protected void updateText() {
261 if (mLocalRepresentation != null) {
262 s = mContext.getString(mLocalRepresentation.getTextId());
264 mButton.setText(calculateUserMessage(mContext, s, ""));
268 * called after the filter is set and the select is called
270 public void reflectCurrentFilter() {
271 mLocalRepresentation = null;
272 FilterRepresentation representation = getLocalRepresentation();
273 if (representation != null && mFilterTitle != null && representation.getTextId() != 0) {
274 String text = mContext.getString(representation.getTextId()).toUpperCase();
275 mFilterTitle.setText(text);
280 public boolean useUtilityPanel() {
284 public void openUtilityPanel(LinearLayout mAccessoryViewList) {
286 if (mImageShow != null) {
287 mImageShow.openUtilityPanel(mAccessoryViewList);
291 protected void setMenuIcon(boolean on) {
292 mEditTitle.setCompoundDrawablesRelativeWithIntrinsicBounds(
293 0, 0, on ? R.drawable.filtershow_menu_marker : 0, 0);
296 protected void createMenu(int[] strId, View button) {
297 PopupMenu pmenu = new PopupMenu(mContext, button);
298 Menu menu = pmenu.getMenu();
299 for (int i = 0; i < strId.length; i++) {
300 menu.add(Menu.NONE, Menu.FIRST + i, 0, mContext.getString(strId[i]));
306 public Control[] getControls() {
311 public void onStartTrackingTouch(SeekBar arg0) {
316 public void onStopTrackingTouch(SeekBar arg0) {
321 public void swapLeft(MenuItem item) {
326 public void swapRight(MenuItem item) {
330 public void detach() {