Editor.java revision 7638772c0ceb5528b12c9943bad5391658d6c78d
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
17package com.android.gallery3d.filtershow.editors;
18
19import android.content.Context;
20import android.util.AttributeSet;
21import android.util.Log;
22import android.view.LayoutInflater;
23import android.view.Menu;
24import android.view.MenuItem;
25import android.view.View;
26import android.view.ViewGroup;
27import android.widget.Button;
28import android.widget.FrameLayout;
29import android.widget.LinearLayout;
30import android.widget.PopupMenu;
31import android.widget.SeekBar;
32import android.widget.SeekBar.OnSeekBarChangeListener;
33
34import com.android.gallery3d.R;
35import com.android.gallery3d.filtershow.cache.ImageLoader;
36import com.android.gallery3d.filtershow.controller.Control;
37import com.android.gallery3d.filtershow.filters.FilterRepresentation;
38import com.android.gallery3d.filtershow.imageshow.ImageShow;
39import com.android.gallery3d.filtershow.imageshow.MasterImage;
40import com.android.gallery3d.filtershow.presets.ImagePreset;
41
42/**
43 * Base class for Editors Must contain a mImageShow and a top level view
44 */
45public 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 static void hackFixStrings(Menu menu) {
63        int count = menu.size();
64        for (int i = 0; i < count; i++) {
65            MenuItem item = menu.getItem(i);
66            item.setTitle(item.getTitle().toString().toUpperCase());
67        }
68    }
69
70    public String calculateUserMessage(Context context, String effectName, Object parameterValue) {
71        return effectName.toUpperCase() + " " + parameterValue;
72    }
73
74    protected Editor(int id) {
75        mID = id;
76    }
77
78    public int getID() {
79        return mID;
80    }
81
82    public byte showParameterValue() {
83        return mShowParameter;
84    }
85
86    public boolean showsSeekBar() {
87        return true;
88    }
89
90    public void setUpEditorUI(View actionButton, View editControl,
91                              Button editTitle, Button stateButton) {
92        mEditTitle = editTitle;
93        mFilterTitle = stateButton;
94        mButton = 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        if (mButton != null) {
126            if (showsPopupIndicator()) {
127                mButton.setCompoundDrawablesRelativeWithIntrinsicBounds(0, 0,
128                        R.drawable.filtershow_menu_marker, 0);
129            } else {
130                mButton.setCompoundDrawablesRelativeWithIntrinsicBounds(0, 0, 0, 0);
131            }
132        }
133    }
134
135    @Override
136    public void onProgressChanged(SeekBar sbar, int progress, boolean arg2) {
137
138    }
139
140    public void setPanel() {
141
142    }
143
144    public void createEditor(Context context,FrameLayout frameLayout) {
145        mContext = context;
146        mFrameLayout = frameLayout;
147        mLocalRepresentation = null;
148    }
149
150    protected void unpack(int viewid, int layoutid) {
151
152        if (mView == null) {
153            mView = mFrameLayout.findViewById(viewid);
154            if (mView == null) {
155                LayoutInflater inflater = (LayoutInflater) mContext.getSystemService
156                        (Context.LAYOUT_INFLATER_SERVICE);
157                mView = inflater.inflate(layoutid, mFrameLayout, false);
158                mFrameLayout.addView(mView, mView.getLayoutParams());
159            }
160        }
161        mImageShow = findImageShow(mView);
162    }
163
164    private ImageShow findImageShow(View view) {
165        if (view instanceof ImageShow) {
166            return (ImageShow) view;
167        }
168        if (!(view instanceof ViewGroup)) {
169            return null;
170        }
171        ViewGroup vg = (ViewGroup) view;
172        int n = vg.getChildCount();
173        for (int i = 0; i < n; i++) {
174            View v = vg.getChildAt(i);
175            if (v instanceof ImageShow) {
176                return (ImageShow) v;
177            } else if (v instanceof ViewGroup) {
178                return findImageShow(v);
179            }
180        }
181        return null;
182    }
183
184    public View getTopLevelView() {
185        return mView;
186    }
187
188    public ImageShow getImageShow() {
189        return mImageShow;
190    }
191
192    public void setImageLoader(ImageLoader imageLoader) {
193        mImageShow.setImageLoader(imageLoader);
194    }
195
196    public void setVisibility(int visible) {
197        mView.setVisibility(visible);
198    }
199
200    public FilterRepresentation getLocalRepresentation() {
201        if (mLocalRepresentation == null) {
202            ImagePreset preset = MasterImage.getImage().getPreset();
203            FilterRepresentation filterRepresentation = MasterImage.getImage().getCurrentFilterRepresentation();
204            mLocalRepresentation = preset.getFilterRepresentationCopyFrom(filterRepresentation);
205            if (mShowParameter == SHOW_VALUE_UNDEFINED) {
206                boolean show = filterRepresentation.showParameterValue();
207                mShowParameter = show ? SHOW_VALUE_INT : SHOW_VALUE_OFF;
208            }
209
210        }
211        return mLocalRepresentation;
212    }
213
214    public void commitLocalRepresentation() {
215        ImagePreset preset = MasterImage.getImage().getPreset();
216        preset.updateFilterRepresentation(getLocalRepresentation());
217        if (mButton != null) {
218            updateText();
219        }
220    }
221
222    protected void updateText() {
223        String s = "";
224        if (mLocalRepresentation != null) {
225            s = mContext.getString(mLocalRepresentation.getTextId());
226        }
227        mButton.setText(calculateUserMessage(mContext, s, ""));
228    }
229
230    /**
231     * called after the filter is set and the select is called
232     */
233    public void reflectCurrentFilter() {
234        mLocalRepresentation = null;
235        FilterRepresentation representation = getLocalRepresentation();
236        if (representation != null && mFilterTitle != null && representation.getTextId() != 0) {
237            String text = mContext.getString(representation.getTextId()).toUpperCase();
238            mFilterTitle.setText(text);
239            updateText();
240        }
241    }
242
243    public boolean useUtilityPanel() {
244        return true;
245    }
246
247    public void openUtilityPanel(LinearLayout mAccessoryViewList) {
248        setMenuIcon(false);
249        if (mImageShow != null) {
250            mImageShow.openUtilityPanel(mAccessoryViewList);
251        }
252    }
253
254    protected void setMenuIcon(boolean on) {
255        mEditTitle.setCompoundDrawablesRelativeWithIntrinsicBounds(
256                0, 0, on ? R.drawable.filtershow_menu_marker : 0, 0);
257    }
258
259    protected void createMenu(int[] strId, View button) {
260        PopupMenu pmenu = new PopupMenu(mContext, button);
261        Menu menu = pmenu.getMenu();
262        for (int i = 0; i < strId.length; i++) {
263            menu.add(Menu.NONE, Menu.FIRST + i, 0, mContext.getString(strId[i]));
264        }
265        setMenuIcon(true);
266
267    }
268
269    public Control[] getControls() {
270        return null;
271    }
272    @Override
273    public void onStartTrackingTouch(SeekBar arg0) {
274
275    }
276
277    @Override
278    public void onStopTrackingTouch(SeekBar arg0) {
279
280    }
281
282    @Override
283    public void swapLeft(MenuItem item) {
284
285    }
286
287    @Override
288    public void swapRight(MenuItem item) {
289
290    }
291
292    public void detach() {
293        if (mImageShow != null) {
294            mImageShow.unselect();
295        }
296    }
297}
298