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.util.AttributeSet;
21 import android.view.LayoutInflater;
22 import android.view.Menu;
23 import android.view.MenuItem;
24 import android.view.View;
25 import android.view.ViewGroup;
26 import android.widget.Button;
27 import android.widget.FrameLayout;
28 import android.widget.LinearLayout;
29 import android.widget.PopupMenu;
30 import android.widget.SeekBar;
31 import android.widget.SeekBar.OnSeekBarChangeListener;
33 import com.android.gallery3d.R;
34 import com.android.gallery3d.filtershow.controller.Control;
35 import com.android.gallery3d.filtershow.filters.FilterRepresentation;
36 import com.android.gallery3d.filtershow.imageshow.ImageShow;
37 import com.android.gallery3d.filtershow.imageshow.MasterImage;
38 import com.android.gallery3d.filtershow.pipeline.ImagePreset;
40 import java.util.ArrayList;
41 import java.util.Collection;
44 * Base class for Editors Must contain a mImageShow and a top level view
46 public class Editor implements OnSeekBarChangeListener, SwapButton.SwapButtonListener {
47 protected Context mContext;
49 protected ImageShow mImageShow;
50 protected FrameLayout mFrameLayout;
51 protected SeekBar mSeekBar;
53 protected Button mFilterTitle;
55 private final String LOGTAG = "Editor";
56 protected boolean mChangesGeometry = false;
57 protected FilterRepresentation mLocalRepresentation = null;
58 protected byte mShowParameter = SHOW_VALUE_UNDEFINED;
59 private Button mButton;
60 public static byte SHOW_VALUE_UNDEFINED = -1;
61 public static byte SHOW_VALUE_OFF = 0;
62 public static byte SHOW_VALUE_INT = 1;
64 public static void hackFixStrings(Menu menu) {
65 int count = menu.size();
66 for (int i = 0; i < count; i++) {
67 MenuItem item = menu.getItem(i);
68 item.setTitle(item.getTitle().toString().toUpperCase());
72 public String calculateUserMessage(Context context, String effectName, Object parameterValue) {
73 return effectName.toUpperCase() + " " + parameterValue;
76 protected Editor(int id) {
84 public byte showParameterValue() {
85 return mShowParameter;
88 public boolean showsSeekBar() {
92 public void setUpEditorUI(View actionButton, View editControl,
93 Button editTitle, Button stateButton) {
94 mEditTitle = editTitle;
95 mFilterTitle = stateButton;
98 setUtilityPanelUI(actionButton, editControl);
101 public boolean showsPopupIndicator() {
106 * @param actionButton the would be the area for menu etc
107 * @param editControl this is the black area for sliders etc
109 public void setUtilityPanelUI(View actionButton, View editControl) {
112 Context context = editControl.getContext();
113 LayoutInflater inflater =
114 (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
115 LinearLayout lp = (LinearLayout) inflater.inflate(
116 R.layout.filtershow_seekbar, (ViewGroup) editControl, true);
117 mSeekBar = (SeekBar) lp.findViewById(R.id.primarySeekBar);
118 mSeekBar.setOnSeekBarChangeListener(this);
120 if (showsSeekBar()) {
121 mSeekBar.setOnSeekBarChangeListener(this);
122 mSeekBar.setVisibility(View.VISIBLE);
124 mSeekBar.setVisibility(View.INVISIBLE);
127 if (mButton != null) {
128 if (showsPopupIndicator()) {
129 mButton.setCompoundDrawablesRelativeWithIntrinsicBounds(0, 0,
130 R.drawable.filtershow_menu_marker, 0);
132 mButton.setCompoundDrawablesRelativeWithIntrinsicBounds(0, 0, 0, 0);
138 public void onProgressChanged(SeekBar sbar, int progress, boolean arg2) {
142 public void setPanel() {
146 public void createEditor(Context context,FrameLayout frameLayout) {
148 mFrameLayout = frameLayout;
149 mLocalRepresentation = null;
152 protected void unpack(int viewid, int layoutid) {
155 mView = mFrameLayout.findViewById(viewid);
157 LayoutInflater inflater = (LayoutInflater) mContext.getSystemService
158 (Context.LAYOUT_INFLATER_SERVICE);
159 mView = inflater.inflate(layoutid, mFrameLayout, false);
160 mFrameLayout.addView(mView, mView.getLayoutParams());
163 mImageShow = findImageShow(mView);
166 private ImageShow findImageShow(View view) {
167 if (view instanceof ImageShow) {
168 return (ImageShow) view;
170 if (!(view instanceof ViewGroup)) {
173 ViewGroup vg = (ViewGroup) view;
174 int n = vg.getChildCount();
175 for (int i = 0; i < n; i++) {
176 View v = vg.getChildAt(i);
177 if (v instanceof ImageShow) {
178 return (ImageShow) v;
179 } else if (v instanceof ViewGroup) {
180 return findImageShow(v);
186 public View getTopLevelView() {
190 public ImageShow getImageShow() {
194 public void setVisibility(int visible) {
195 mView.setVisibility(visible);
198 public FilterRepresentation getLocalRepresentation() {
199 if (mLocalRepresentation == null) {
200 ImagePreset preset = MasterImage.getImage().getPreset();
201 FilterRepresentation filterRepresentation = MasterImage.getImage().getCurrentFilterRepresentation();
202 mLocalRepresentation = preset.getFilterRepresentationCopyFrom(filterRepresentation);
203 if (mShowParameter == SHOW_VALUE_UNDEFINED && filterRepresentation != null) {
204 boolean show = filterRepresentation.showParameterValue();
205 mShowParameter = show ? SHOW_VALUE_INT : SHOW_VALUE_OFF;
209 return mLocalRepresentation;
213 * Call this to update the preset in MasterImage with the current representation
214 * returned by getLocalRepresentation. This causes the preview bitmap to be
217 public void commitLocalRepresentation() {
218 commitLocalRepresentation(getLocalRepresentation());
222 * Call this to update the preset in MasterImage with a given representation.
223 * This causes the preview bitmap to be regenerated.
225 public void commitLocalRepresentation(FilterRepresentation rep) {
226 ArrayList<FilterRepresentation> filter = new ArrayList<FilterRepresentation>(1);
228 commitLocalRepresentation(filter);
232 * Call this to update the preset in MasterImage with a collection of FilterRepresnations.
233 * This causes the preview bitmap to be regenerated.
235 public void commitLocalRepresentation(Collection<FilterRepresentation> reps) {
236 ImagePreset preset = MasterImage.getImage().getPreset();
237 preset.updateFilterRepresentations(reps);
238 if (mButton != null) {
241 if (mChangesGeometry) {
242 // Regenerate both the filtered and the geometry-only bitmaps
243 MasterImage.getImage().updatePresets(true);
245 // Regenerate only the filtered bitmap.
246 MasterImage.getImage().invalidateFiltersOnly();
248 preset.fillImageStateAdapter(MasterImage.getImage().getState());
252 * This is called in response to a click to apply and leave the editor.
254 public void finalApplyCalled() {
255 commitLocalRepresentation();
258 protected void updateText() {
260 if (mLocalRepresentation != null) {
261 s = mContext.getString(mLocalRepresentation.getTextId());
263 mButton.setText(calculateUserMessage(mContext, s, ""));
267 * called after the filter is set and the select is called
269 public void reflectCurrentFilter() {
270 mLocalRepresentation = null;
271 FilterRepresentation representation = getLocalRepresentation();
272 if (representation != null && mFilterTitle != null && representation.getTextId() != 0) {
273 String text = mContext.getString(representation.getTextId()).toUpperCase();
274 mFilterTitle.setText(text);
279 public boolean useUtilityPanel() {
283 public void openUtilityPanel(LinearLayout mAccessoryViewList) {
285 if (mImageShow != null) {
286 mImageShow.openUtilityPanel(mAccessoryViewList);
290 protected void setMenuIcon(boolean on) {
291 mEditTitle.setCompoundDrawablesRelativeWithIntrinsicBounds(
292 0, 0, on ? R.drawable.filtershow_menu_marker : 0, 0);
295 protected void createMenu(int[] strId, View button) {
296 PopupMenu pmenu = new PopupMenu(mContext, button);
297 Menu menu = pmenu.getMenu();
298 for (int i = 0; i < strId.length; i++) {
299 menu.add(Menu.NONE, Menu.FIRST + i, 0, mContext.getString(strId[i]));
305 public Control[] getControls() {
309 public void onStartTrackingTouch(SeekBar arg0) {
314 public void onStopTrackingTouch(SeekBar arg0) {
319 public void swapLeft(MenuItem item) {
324 public void swapRight(MenuItem item) {
328 public void detach() {