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.util.Log;
23 import android.view.LayoutInflater;
24 import android.view.Menu;
25 import android.view.MenuItem;
26 import android.view.View;
27 import android.view.ViewGroup;
28 import android.widget.Button;
29 import android.widget.FrameLayout;
30 import android.widget.LinearLayout;
31 import android.widget.PopupMenu;
32 import android.widget.SeekBar;
33 import android.widget.SeekBar.OnSeekBarChangeListener;
35 import com.android.gallery3d.R;
36 import com.android.gallery3d.filtershow.controller.Control;
37 import com.android.gallery3d.filtershow.filters.FilterRepresentation;
38 import com.android.gallery3d.filtershow.imageshow.ImageShow;
39 import com.android.gallery3d.filtershow.imageshow.MasterImage;
40 import com.android.gallery3d.filtershow.pipeline.ImagePreset;
42 import java.util.ArrayList;
43 import java.util.Collection;
46 * Base class for Editors Must contain a mImageShow and a top level view
48 public class Editor implements OnSeekBarChangeListener, SwapButton.SwapButtonListener {
49 protected Context mContext;
51 protected ImageShow mImageShow;
52 protected FrameLayout mFrameLayout;
53 protected SeekBar mSeekBar;
55 protected Button mFilterTitle;
57 private final String LOGTAG = "Editor";
58 protected boolean mChangesGeometry = false;
59 protected FilterRepresentation mLocalRepresentation = null;
60 protected byte mShowParameter = SHOW_VALUE_UNDEFINED;
61 private Button mButton;
62 public static byte SHOW_VALUE_UNDEFINED = -1;
63 public static byte SHOW_VALUE_OFF = 0;
64 public static byte SHOW_VALUE_INT = 1;
66 public static void hackFixStrings(Menu menu) {
67 int count = menu.size();
68 for (int i = 0; i < count; i++) {
69 MenuItem item = menu.getItem(i);
70 item.setTitle(item.getTitle().toString().toUpperCase());
74 public String calculateUserMessage(Context context, String effectName, Object parameterValue) {
75 return effectName.toUpperCase() + " " + parameterValue;
78 protected Editor(int id) {
86 public byte showParameterValue() {
87 return mShowParameter;
90 public boolean showsSeekBar() {
94 public void setUpEditorUI(View actionButton, View editControl,
95 Button editTitle, Button stateButton) {
96 mEditTitle = editTitle;
97 mFilterTitle = stateButton;
99 MasterImage.getImage().resetGeometryImages(false);
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 setMenuIcon(showsPopupIndicator());
135 public void onProgressChanged(SeekBar sbar, int progress, boolean arg2) {
139 public void setPanel() {
143 public void createEditor(Context context, FrameLayout frameLayout) {
145 mFrameLayout = frameLayout;
146 mLocalRepresentation = null;
149 protected void unpack(int viewid, int layoutid) {
152 mView = mFrameLayout.findViewById(viewid);
154 LayoutInflater inflater = (LayoutInflater) mContext.getSystemService
155 (Context.LAYOUT_INFLATER_SERVICE);
156 mView = inflater.inflate(layoutid, mFrameLayout, false);
157 mFrameLayout.addView(mView, mView.getLayoutParams());
160 mImageShow = findImageShow(mView);
163 private ImageShow findImageShow(View view) {
164 if (view instanceof ImageShow) {
165 return (ImageShow) view;
167 if (!(view instanceof ViewGroup)) {
170 ViewGroup vg = (ViewGroup) view;
171 int n = vg.getChildCount();
172 for (int i = 0; i < n; i++) {
173 View v = vg.getChildAt(i);
174 if (v instanceof ImageShow) {
175 return (ImageShow) v;
176 } else if (v instanceof ViewGroup) {
177 return findImageShow(v);
183 public View getTopLevelView() {
187 public ImageShow getImageShow() {
191 public void setVisibility(int visible) {
192 mView.setVisibility(visible);
195 public FilterRepresentation getLocalRepresentation() {
196 if (mLocalRepresentation == null) {
197 ImagePreset preset = MasterImage.getImage().getPreset();
198 FilterRepresentation filterRepresentation = MasterImage.getImage().getCurrentFilterRepresentation();
199 mLocalRepresentation = preset.getFilterRepresentationCopyFrom(filterRepresentation);
200 if (mShowParameter == SHOW_VALUE_UNDEFINED && filterRepresentation != null) {
201 boolean show = filterRepresentation.showParameterValue();
202 mShowParameter = show ? SHOW_VALUE_INT : SHOW_VALUE_OFF;
206 return mLocalRepresentation;
210 * Call this to update the preset in MasterImage with the current representation
211 * returned by getLocalRepresentation. This causes the preview bitmap to be
214 public void commitLocalRepresentation() {
215 commitLocalRepresentation(getLocalRepresentation());
219 * Call this to update the preset in MasterImage with a given representation.
220 * This causes the preview bitmap to be regenerated.
222 public void commitLocalRepresentation(FilterRepresentation rep) {
223 ArrayList<FilterRepresentation> filter = new ArrayList<FilterRepresentation>(1);
225 commitLocalRepresentation(filter);
229 * Call this to update the preset in MasterImage with a collection of FilterRepresentations.
230 * This causes the preview bitmap to be regenerated.
232 public void commitLocalRepresentation(Collection<FilterRepresentation> reps) {
233 ImagePreset preset = MasterImage.getImage().getPreset();
234 preset.updateFilterRepresentations(reps);
235 if (mButton != null) {
238 if (mChangesGeometry) {
239 // Regenerate both the filtered and the geometry-only bitmaps
240 MasterImage.getImage().resetGeometryImages(true);
242 // Regenerate the filtered bitmap.
243 MasterImage.getImage().invalidateFiltersOnly();
244 preset.fillImageStateAdapter(MasterImage.getImage().getState());
248 * This is called in response to a click to apply and leave the editor.
250 public void finalApplyCalled() {
251 commitLocalRepresentation();
254 protected void updateText() {
256 if (mLocalRepresentation != null) {
257 s = mContext.getString(mLocalRepresentation.getTextId());
259 mButton.setText(calculateUserMessage(mContext, s, ""));
263 * called after the filter is set and the select is called
265 public void reflectCurrentFilter() {
266 mLocalRepresentation = null;
267 FilterRepresentation representation = getLocalRepresentation();
268 if (representation != null && mFilterTitle != null && representation.getTextId() != 0) {
269 String text = mContext.getString(representation.getTextId()).toUpperCase();
270 mFilterTitle.setText(text);
275 public boolean useUtilityPanel() {
279 public void openUtilityPanel(LinearLayout mAccessoryViewList) {
280 setMenuIcon(showsPopupIndicator());
281 if (mImageShow != null) {
282 mImageShow.openUtilityPanel(mAccessoryViewList);
286 protected void setMenuIcon(boolean on) {
287 mEditTitle.setCompoundDrawablesRelativeWithIntrinsicBounds(
288 0, 0, on ? R.drawable.filtershow_menu_marker_rtl : 0, 0);
291 protected void createMenu(int[] strId, View button) {
292 PopupMenu pmenu = new PopupMenu(mContext, button);
293 Menu menu = pmenu.getMenu();
294 for (int i = 0; i < strId.length; i++) {
295 menu.add(Menu.NONE, Menu.FIRST + i, 0, mContext.getString(strId[i]));
301 public Control[] getControls() {
306 public void onStartTrackingTouch(SeekBar arg0) {
311 public void onStopTrackingTouch(SeekBar arg0) {
316 public void swapLeft(MenuItem item) {
321 public void swapRight(MenuItem item) {
325 public void detach() {
326 if (mImageShow != null) {