OSDN Git Service

Merge "fix serialization on draw" into gb-ub-photos-carlsbad
[android-x86/packages-apps-Gallery2.git] / src / com / android / gallery3d / filtershow / editors / Editor.java
1 /*
2  * Copyright (C) 2012 The Android Open Source Project
3  *
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
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 package com.android.gallery3d.filtershow.editors;
18
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;
32
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;
39
40 import java.util.ArrayList;
41 import java.util.Collection;
42
43 /**
44  * Base class for Editors Must contain a mImageShow and a top level view
45  */
46 public class Editor implements OnSeekBarChangeListener, SwapButton.SwapButtonListener {
47     protected Context mContext;
48     protected View mView;
49     protected ImageShow mImageShow;
50     protected FrameLayout mFrameLayout;
51     protected SeekBar mSeekBar;
52     Button mEditTitle;
53     protected Button mFilterTitle;
54     protected int mID;
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;
63
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());
69         }
70     }
71
72     public String calculateUserMessage(Context context, String effectName, Object parameterValue) {
73         return effectName.toUpperCase() + " " + parameterValue;
74     }
75
76     protected Editor(int id) {
77         mID = id;
78     }
79
80     public int getID() {
81         return mID;
82     }
83
84     public byte showParameterValue() {
85         return mShowParameter;
86     }
87
88     public boolean showsSeekBar() {
89         return true;
90     }
91
92     public void setUpEditorUI(View actionButton, View editControl,
93                               Button editTitle, Button stateButton) {
94         mEditTitle = editTitle;
95         mFilterTitle = stateButton;
96         mButton = editTitle;
97         MasterImage.getImage().resetGeometryImages();
98         setMenuIcon(true);
99         setUtilityPanelUI(actionButton, editControl);
100     }
101
102     public boolean showsPopupIndicator() {
103         return true;
104     }
105
106     /**
107      * @param actionButton the would be the area for menu etc
108      * @param editControl this is the black area for sliders etc
109      */
110     public void setUtilityPanelUI(View actionButton, View editControl) {
111
112         AttributeSet aset;
113         Context context = editControl.getContext();
114         LayoutInflater inflater =
115                 (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
116         LinearLayout lp = (LinearLayout) inflater.inflate(
117                 R.layout.filtershow_seekbar, (ViewGroup) editControl, true);
118         mSeekBar = (SeekBar) lp.findViewById(R.id.primarySeekBar);
119         mSeekBar.setOnSeekBarChangeListener(this);
120
121         if (showsSeekBar()) {
122             mSeekBar.setOnSeekBarChangeListener(this);
123             mSeekBar.setVisibility(View.VISIBLE);
124         } else {
125             mSeekBar.setVisibility(View.INVISIBLE);
126         }
127
128         if (mButton != null) {
129             if (showsPopupIndicator()) {
130                 mButton.setCompoundDrawablesRelativeWithIntrinsicBounds(0, 0,
131                         R.drawable.filtershow_menu_marker, 0);
132             } else {
133                 mButton.setCompoundDrawablesRelativeWithIntrinsicBounds(0, 0, 0, 0);
134             }
135         }
136     }
137
138     @Override
139     public void onProgressChanged(SeekBar sbar, int progress, boolean arg2) {
140
141     }
142
143     public void setPanel() {
144
145     }
146
147     public void createEditor(Context context, FrameLayout frameLayout) {
148         mContext = context;
149         mFrameLayout = frameLayout;
150         mLocalRepresentation = null;
151     }
152
153     protected void unpack(int viewid, int layoutid) {
154
155         if (mView == null) {
156             mView = mFrameLayout.findViewById(viewid);
157             if (mView == null) {
158                 LayoutInflater inflater = (LayoutInflater) mContext.getSystemService
159                         (Context.LAYOUT_INFLATER_SERVICE);
160                 mView = inflater.inflate(layoutid, mFrameLayout, false);
161                 mFrameLayout.addView(mView, mView.getLayoutParams());
162             }
163         }
164         mImageShow = findImageShow(mView);
165     }
166
167     private ImageShow findImageShow(View view) {
168         if (view instanceof ImageShow) {
169             return (ImageShow) view;
170         }
171         if (!(view instanceof ViewGroup)) {
172             return null;
173         }
174         ViewGroup vg = (ViewGroup) view;
175         int n = vg.getChildCount();
176         for (int i = 0; i < n; i++) {
177             View v = vg.getChildAt(i);
178             if (v instanceof ImageShow) {
179                 return (ImageShow) v;
180             } else if (v instanceof ViewGroup) {
181                 return findImageShow(v);
182             }
183         }
184         return null;
185     }
186
187     public View getTopLevelView() {
188         return mView;
189     }
190
191     public ImageShow getImageShow() {
192         return mImageShow;
193     }
194
195     public void setVisibility(int visible) {
196         mView.setVisibility(visible);
197     }
198
199     public FilterRepresentation getLocalRepresentation() {
200         if (mLocalRepresentation == null) {
201             ImagePreset preset = MasterImage.getImage().getPreset();
202             FilterRepresentation filterRepresentation = MasterImage.getImage().getCurrentFilterRepresentation();
203             mLocalRepresentation = preset.getFilterRepresentationCopyFrom(filterRepresentation);
204             if (mShowParameter == SHOW_VALUE_UNDEFINED && filterRepresentation != null) {
205                 boolean show = filterRepresentation.showParameterValue();
206                 mShowParameter = show ? SHOW_VALUE_INT : SHOW_VALUE_OFF;
207             }
208
209         }
210         return mLocalRepresentation;
211     }
212
213     /**
214      * Call this to update the preset in MasterImage with the current representation
215      * returned by getLocalRepresentation.  This causes the preview bitmap to be
216      * regenerated.
217      */
218     public void commitLocalRepresentation() {
219         commitLocalRepresentation(getLocalRepresentation());
220     }
221
222     /**
223      * Call this to update the preset in MasterImage with a given representation.
224      * This causes the preview bitmap to be regenerated.
225      */
226     public void commitLocalRepresentation(FilterRepresentation rep) {
227         ArrayList<FilterRepresentation> filter = new ArrayList<FilterRepresentation>(1);
228         filter.add(rep);
229         commitLocalRepresentation(filter);
230     }
231
232     /**
233      * Call this to update the preset in MasterImage with a collection of FilterRepresentations.
234      * This causes the preview bitmap to be regenerated.
235      */
236     public void commitLocalRepresentation(Collection<FilterRepresentation> reps) {
237         ImagePreset preset = MasterImage.getImage().getPreset();
238         preset.updateFilterRepresentations(reps);
239         if (mButton != null) {
240             updateText();
241         }
242         if (mChangesGeometry) {
243             // Regenerate both the filtered and the geometry-only bitmaps
244             MasterImage.getImage().resetGeometryImages();
245         }
246         // Regenerate the filtered bitmap.
247         MasterImage.getImage().invalidateFiltersOnly();
248         preset.fillImageStateAdapter(MasterImage.getImage().getState());
249     }
250
251     /**
252      * This is called in response to a click to apply and leave the editor.
253      */
254     public void finalApplyCalled() {
255         commitLocalRepresentation();
256     }
257
258     protected void updateText() {
259         String s = "";
260         if (mLocalRepresentation != null) {
261             s = mContext.getString(mLocalRepresentation.getTextId());
262         }
263         mButton.setText(calculateUserMessage(mContext, s, ""));
264     }
265
266     /**
267      * called after the filter is set and the select is called
268      */
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);
275             updateText();
276         }
277     }
278
279     public boolean useUtilityPanel() {
280         return true;
281     }
282
283     public void openUtilityPanel(LinearLayout mAccessoryViewList) {
284         setMenuIcon(false);
285         if (mImageShow != null) {
286             mImageShow.openUtilityPanel(mAccessoryViewList);
287         }
288     }
289
290     protected void setMenuIcon(boolean on) {
291         mEditTitle.setCompoundDrawablesRelativeWithIntrinsicBounds(
292                 0, 0, on ? R.drawable.filtershow_menu_marker : 0, 0);
293     }
294
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]));
300         }
301         setMenuIcon(true);
302
303     }
304
305     public Control[] getControls() {
306         return null;
307     }
308
309     @Override
310     public void onStartTrackingTouch(SeekBar arg0) {
311
312     }
313
314     @Override
315     public void onStopTrackingTouch(SeekBar arg0) {
316
317     }
318
319     @Override
320     public void swapLeft(MenuItem item) {
321
322     }
323
324     @Override
325     public void swapRight(MenuItem item) {
326
327     }
328
329     public void detach() {
330     }
331 }