OSDN Git Service

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