OSDN Git Service

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