2 * Copyright (C) 2012 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package com.android.gallery3d.filtershow.editors;
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;
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;
43 * Base class for Editors Must contain a mImageShow and a top level view
45 public class Editor implements OnSeekBarChangeListener, SwapButton.SwapButtonListener {
46 protected Context mContext;
48 protected ImageShow mImageShow;
49 protected FrameLayout mFrameLayout;
50 protected SeekBar mSeekBar;
52 protected Button mFilterTitle;
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;
62 public String calculateUserMessage(Context context, String effectName, Object parameterValue) {
63 return effectName + " " + parameterValue;
66 protected Editor(int id) {
74 public byte showParameterValue() {
75 return mShowParameter;
78 public boolean showsSeekBar() {
82 public void setUpEditorUI(View actionButton, View editControl,
83 Button editTitle, Button stateButton) {
84 mEditTitle = editTitle;
85 mFilterTitle = stateButton;
88 setUtilityPanelUI(actionButton, editControl);
91 public boolean showsPopupIndicator() {
96 * @param actionButton the would be the area for menu etc
97 * @param editControl this is the black area for sliders etc
99 public void setUtilityPanelUI(View actionButton, View editControl) {
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);
110 if (showsSeekBar()) {
111 mSeekBar.setOnSeekBarChangeListener(this);
112 mSeekBar.setVisibility(View.VISIBLE);
114 mSeekBar.setVisibility(View.INVISIBLE);
117 if (mButton != null) {
118 if (showsPopupIndicator()) {
119 mButton.setCompoundDrawablesRelativeWithIntrinsicBounds(0, 0,
120 R.drawable.filtershow_menu_marker, 0);
122 mButton.setCompoundDrawablesRelativeWithIntrinsicBounds(0, 0, 0, 0);
128 public void onProgressChanged(SeekBar sbar, int progress, boolean arg2) {
132 public void setPanel() {
136 public void createEditor(Context context,FrameLayout frameLayout) {
138 mFrameLayout = frameLayout;
139 mLocalRepresentation = null;
142 protected void unpack(int viewid, int layoutid) {
145 mView = mFrameLayout.findViewById(viewid);
147 LayoutInflater inflater = (LayoutInflater) mContext.getSystemService
148 (Context.LAYOUT_INFLATER_SERVICE);
149 mView = inflater.inflate(layoutid, mFrameLayout, false);
150 mFrameLayout.addView(mView, mView.getLayoutParams());
153 mImageShow = findImageShow(mView);
156 private ImageShow findImageShow(View view) {
157 if (view instanceof ImageShow) {
158 return (ImageShow) view;
160 if (!(view instanceof ViewGroup)) {
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);
176 public View getTopLevelView() {
180 public ImageShow getImageShow() {
184 public void setImageLoader(ImageLoader imageLoader) {
185 mImageShow.setImageLoader(imageLoader);
188 public void setVisibility(int visible) {
189 mView.setVisibility(visible);
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;
203 return mLocalRepresentation;
206 public void commitLocalRepresentation() {
207 ImagePreset preset = MasterImage.getImage().getPreset();
208 preset.updateFilterRepresentation(getLocalRepresentation());
209 if (mButton != null) {
214 protected void updateText() {
216 if (mLocalRepresentation != null) {
217 s = mContext.getString(mLocalRepresentation.getTextId());
219 mButton.setText(calculateUserMessage(mContext, s, ""));
223 * called after the filter is set and the select is called
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);
235 public boolean useUtilityPanel() {
239 public void openUtilityPanel(LinearLayout mAccessoryViewList) {
241 if (mImageShow != null) {
242 mImageShow.openUtilityPanel(mAccessoryViewList);
246 protected void setMenuIcon(boolean on) {
247 mEditTitle.setCompoundDrawablesRelativeWithIntrinsicBounds(
248 0, 0, on ? R.drawable.filtershow_menu_marker : 0, 0);
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]));
261 public Control[] getControls() {
265 public void onStartTrackingTouch(SeekBar arg0) {
270 public void onStopTrackingTouch(SeekBar arg0) {
275 public void swapLeft(MenuItem item) {
280 public void swapRight(MenuItem item) {
284 public void detach() {
285 if (mImageShow != null) {
286 mImageShow.unselect();