OSDN Git Service

Merge "Implements swipe in editor menu" into gb-ub-photos-bryce
[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.util.Log;
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;
33
34 import com.android.gallery3d.R;
35 import com.android.gallery3d.filtershow.PanelController;
36 import com.android.gallery3d.filtershow.cache.ImageLoader;
37 import com.android.gallery3d.filtershow.controller.Control;
38 import com.android.gallery3d.filtershow.filters.FilterRepresentation;
39 import com.android.gallery3d.filtershow.imageshow.ImageShow;
40 import com.android.gallery3d.filtershow.imageshow.MasterImage;
41 import com.android.gallery3d.filtershow.presets.ImagePreset;
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 PanelController mPanelController;
54     protected int mID;
55     private final String LOGTAG = "Editor";
56     protected FilterRepresentation mLocalRepresentation = null;
57     protected byte mShowParameter = SHOW_VALUE_UNDEFINED;
58     public static byte SHOW_VALUE_UNDEFINED = -1;
59     public static byte SHOW_VALUE_OFF = 0;
60     public static byte SHOW_VALUE_INT = 1;
61
62     public void setPanelController(PanelController panelController) {
63         this.mPanelController = panelController;
64     }
65
66     public String calculateUserMessage(Context context, String effectName, Object parameterValue) {
67         String apply = "";
68         if (mShowParameter == SHOW_VALUE_INT) {
69             apply += " " + effectName + " " + parameterValue;
70         } else {
71             apply += " " + effectName;
72         }
73         return apply;
74     }
75
76     protected Editor(int id) {
77         mID = id;
78     }
79
80     public int getID() {
81         return mID;
82     }
83
84
85     public byte showParameterValue() {
86         return mShowParameter;
87     }
88
89     public boolean showsSeekBar() {
90         return true;
91     }
92
93     public void setUpEditorUI(View actionButton, View editControl, Button editTitle) {
94         this.mEditTitle = editTitle;
95         setMenuIcon(true);
96         setUtilityPanelUI(actionButton, editControl);
97     }
98
99     public boolean showsPopupIndicator() {
100         return true;
101     }
102
103     /**
104      * @param actionButton the would be the area for menu etc
105      * @param editControl this is the black area for sliders etc
106      */
107     public void setUtilityPanelUI(View actionButton, View editControl) {
108
109         AttributeSet aset;
110         Context context = editControl.getContext();
111         LayoutInflater inflater =
112                 (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
113         LinearLayout lp = (LinearLayout) inflater.inflate(
114                 R.layout.filtershow_seekbar, (ViewGroup) editControl, true);
115         mSeekBar = (SeekBar) lp.findViewById(R.id.primarySeekBar);
116         mSeekBar.setOnSeekBarChangeListener(this);
117
118         if (showsSeekBar()) {
119             mSeekBar.setOnSeekBarChangeListener(this);
120             mSeekBar.setVisibility(View.VISIBLE);
121         } else {
122             mSeekBar.setVisibility(View.INVISIBLE);
123         }
124
125         Button button = (Button) actionButton.findViewById(R.id.applyEffect);
126         if (button != null) {
127             if (showsPopupIndicator()) {
128                 button.setCompoundDrawablesRelativeWithIntrinsicBounds(0, 0,
129                         R.drawable.filtershow_menu_marker, 0);
130             } else {
131                 button.setCompoundDrawablesRelativeWithIntrinsicBounds(0, 0, 0, 0);
132             }
133         }
134     }
135
136     @Override
137     public void onProgressChanged(SeekBar sbar, int progress, boolean arg2) {
138
139     }
140
141     public void setPanel() {
142
143     }
144
145     public void createEditor(Context context,FrameLayout frameLayout) {
146         mContext = context;
147         mFrameLayout = frameLayout;
148         mLocalRepresentation = null;
149     }
150
151     protected void unpack(int viewid, int layoutid) {
152
153         if (mView == null) {
154             mView = mFrameLayout.findViewById(viewid);
155             if (mView == null) {
156                 LayoutInflater inflater = (LayoutInflater) mContext.getSystemService
157                         (Context.LAYOUT_INFLATER_SERVICE);
158                 mView = inflater.inflate(layoutid, mFrameLayout, false);
159                 mFrameLayout.addView(mView, mView.getLayoutParams());
160             }
161         }
162         mImageShow = findImageShow(mView);
163     }
164
165     private ImageShow findImageShow(View view) {
166         if (view instanceof ImageShow) {
167             return (ImageShow) view;
168         }
169         if (!(view instanceof ViewGroup)) {
170             return null;
171         }
172         ViewGroup vg = (ViewGroup) view;
173         int n = vg.getChildCount();
174         for (int i = 0; i < n; i++) {
175             View v = vg.getChildAt(i);
176             if (v instanceof ImageShow) {
177                 return (ImageShow) v;
178             } else if (v instanceof ViewGroup) {
179                 return findImageShow(v);
180             }
181         }
182         return null;
183     }
184
185     public View getTopLevelView() {
186         return mView;
187     }
188
189     public ImageShow getImageShow() {
190         return mImageShow;
191     }
192
193     public void setImageLoader(ImageLoader imageLoader) {
194         mImageShow.setImageLoader(imageLoader);
195     }
196
197     public void setVisibility(int visible) {
198         mView.setVisibility(visible);
199     }
200
201     public FilterRepresentation getLocalRepresentation() {
202         if (mLocalRepresentation == null) {
203             ImagePreset preset = MasterImage.getImage().getPreset();
204             FilterRepresentation filterRepresentation = MasterImage.getImage().getCurrentFilterRepresentation();
205             mLocalRepresentation = preset.getFilterRepresentationCopyFrom(filterRepresentation);
206             if (mShowParameter == SHOW_VALUE_UNDEFINED) {
207                 boolean show = filterRepresentation.showParameterValue();
208                 mShowParameter = show ? SHOW_VALUE_INT : SHOW_VALUE_OFF;
209             }
210         }
211
212         return mLocalRepresentation;
213     }
214
215     public void commitLocalRepresentation() {
216         ImagePreset preset = MasterImage.getImage().getPreset();
217         preset.updateFilterRepresentation(getLocalRepresentation());
218     }
219
220     /**
221      * called after the filter is set and the select is called
222      */
223     public void reflectCurrentFilter() {
224         mLocalRepresentation = null;
225     }
226
227     public boolean useUtilityPanel() {
228         return true;
229     }
230
231     public void openUtilityPanel(LinearLayout mAccessoryViewList) {
232         setMenuIcon(false);
233         if (mImageShow != null) {
234             mImageShow.openUtilityPanel(mAccessoryViewList);
235         }
236     }
237
238     protected void setMenuIcon(boolean on) {
239         mEditTitle.setCompoundDrawablesRelativeWithIntrinsicBounds(
240                 0, 0, on ? R.drawable.filtershow_menu_marker : 0, 0);
241     }
242     protected void createMenu(int[] strId, View button) {
243         PopupMenu pmenu = new PopupMenu(mContext, button);
244         Menu menu = pmenu.getMenu();
245         for (int i = 0; i < strId.length; i++) {
246             menu.add(Menu.NONE, Menu.FIRST + i, 0, mContext.getString(strId[i]));
247         }
248         setMenuIcon(true);
249
250     }
251
252     public Control[] getControls() {
253         return null;
254     }
255     @Override
256     public void onStartTrackingTouch(SeekBar arg0) {
257
258     }
259
260     @Override
261     public void onStopTrackingTouch(SeekBar arg0) {
262
263     }
264
265     @Override
266     public void swapLeft(MenuItem item) {
267
268     }
269
270     @Override
271     public void swapRight(MenuItem item) {
272
273     }
274 }