ParametricEditor.java revision 33de212ec780eaf2bc8d86908f07da33ea8dd7f2
1235d2179374bec14040f20af91dc753e38ea3639John Hoford/*
2235d2179374bec14040f20af91dc753e38ea3639John Hoford * Copyright (C) 2013 The Android Open Source Project
3235d2179374bec14040f20af91dc753e38ea3639John Hoford *
4235d2179374bec14040f20af91dc753e38ea3639John Hoford * Licensed under the Apache License, Version 2.0 (the "License");
5235d2179374bec14040f20af91dc753e38ea3639John Hoford * you may not use this file except in compliance with the License.
6235d2179374bec14040f20af91dc753e38ea3639John Hoford * You may obtain a copy of the License at
7235d2179374bec14040f20af91dc753e38ea3639John Hoford *
8235d2179374bec14040f20af91dc753e38ea3639John Hoford *      http://www.apache.org/licenses/LICENSE-2.0
9235d2179374bec14040f20af91dc753e38ea3639John Hoford *
10235d2179374bec14040f20af91dc753e38ea3639John Hoford * Unless required by applicable law or agreed to in writing, software
11235d2179374bec14040f20af91dc753e38ea3639John Hoford * distributed under the License is distributed on an "AS IS" BASIS,
12235d2179374bec14040f20af91dc753e38ea3639John Hoford * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13235d2179374bec14040f20af91dc753e38ea3639John Hoford * See the License for the specific language governing permissions and
14235d2179374bec14040f20af91dc753e38ea3639John Hoford * limitations under the License.
15235d2179374bec14040f20af91dc753e38ea3639John Hoford */
16235d2179374bec14040f20af91dc753e38ea3639John Hoford
17235d2179374bec14040f20af91dc753e38ea3639John Hofordpackage com.android.gallery3d.filtershow.editors;
18235d2179374bec14040f20af91dc753e38ea3639John Hoford
19235d2179374bec14040f20af91dc753e38ea3639John Hofordimport android.content.Context;
20235d2179374bec14040f20af91dc753e38ea3639John Hofordimport android.graphics.Point;
21235d2179374bec14040f20af91dc753e38ea3639John Hofordimport android.util.Log;
22235d2179374bec14040f20af91dc753e38ea3639John Hofordimport android.view.View;
23235d2179374bec14040f20af91dc753e38ea3639John Hofordimport android.view.ViewGroup;
24235d2179374bec14040f20af91dc753e38ea3639John Hofordimport android.view.ViewGroup.LayoutParams;
25235d2179374bec14040f20af91dc753e38ea3639John Hofordimport android.view.WindowManager;
26235d2179374bec14040f20af91dc753e38ea3639John Hofordimport android.widget.FrameLayout;
27235d2179374bec14040f20af91dc753e38ea3639John Hofordimport android.widget.LinearLayout;
28235d2179374bec14040f20af91dc753e38ea3639John Hofordimport android.widget.SeekBar;
29235d2179374bec14040f20af91dc753e38ea3639John Hoford
30235d2179374bec14040f20af91dc753e38ea3639John Hofordimport com.android.gallery3d.R;
31235d2179374bec14040f20af91dc753e38ea3639John Hofordimport com.android.gallery3d.filtershow.controller.ActionSlider;
32235d2179374bec14040f20af91dc753e38ea3639John Hofordimport com.android.gallery3d.filtershow.controller.BasicSlider;
33235d2179374bec14040f20af91dc753e38ea3639John Hofordimport com.android.gallery3d.filtershow.controller.Control;
34235d2179374bec14040f20af91dc753e38ea3639John Hofordimport com.android.gallery3d.filtershow.controller.Parameter;
35235d2179374bec14040f20af91dc753e38ea3639John Hofordimport com.android.gallery3d.filtershow.controller.ParameterActionAndInt;
36235d2179374bec14040f20af91dc753e38ea3639John Hofordimport com.android.gallery3d.filtershow.controller.ParameterInteger;
37166524339e9f8ad86c0922142b961776e0da3d8fJohn Hofordimport com.android.gallery3d.filtershow.controller.ParameterStyles;
38166524339e9f8ad86c0922142b961776e0da3d8fJohn Hofordimport com.android.gallery3d.filtershow.controller.StyleChooser;
39235d2179374bec14040f20af91dc753e38ea3639John Hofordimport com.android.gallery3d.filtershow.controller.TitledSlider;
40235d2179374bec14040f20af91dc753e38ea3639John Hofordimport com.android.gallery3d.filtershow.filters.FilterBasicRepresentation;
41235d2179374bec14040f20af91dc753e38ea3639John Hofordimport com.android.gallery3d.filtershow.filters.FilterRepresentation;
42235d2179374bec14040f20af91dc753e38ea3639John Hoford
43235d2179374bec14040f20af91dc753e38ea3639John Hofordimport java.lang.reflect.Constructor;
44235d2179374bec14040f20af91dc753e38ea3639John Hofordimport java.util.HashMap;
45235d2179374bec14040f20af91dc753e38ea3639John Hoford
46235d2179374bec14040f20af91dc753e38ea3639John Hofordpublic class ParametricEditor extends Editor {
47235d2179374bec14040f20af91dc753e38ea3639John Hoford    private int mLayoutID;
48235d2179374bec14040f20af91dc753e38ea3639John Hoford    private int mViewID;
49235d2179374bec14040f20af91dc753e38ea3639John Hoford    public static int ID = R.id.editorParametric;
50235d2179374bec14040f20af91dc753e38ea3639John Hoford    private final String LOGTAG = "ParametricEditor";
51235d2179374bec14040f20af91dc753e38ea3639John Hoford    protected Control mControl;
52235d2179374bec14040f20af91dc753e38ea3639John Hoford    public static final int MINIMUM_WIDTH = 600;
53235d2179374bec14040f20af91dc753e38ea3639John Hoford    public static final int MINIMUM_HEIGHT = 800;
54166524339e9f8ad86c0922142b961776e0da3d8fJohn Hoford    View mActionButton;
55166524339e9f8ad86c0922142b961776e0da3d8fJohn Hoford    View mEditControl;
56235d2179374bec14040f20af91dc753e38ea3639John Hoford    static HashMap<String, Class> portraitMap = new HashMap<String, Class>();
57235d2179374bec14040f20af91dc753e38ea3639John Hoford    static HashMap<String, Class> landscapeMap = new HashMap<String, Class>();
58235d2179374bec14040f20af91dc753e38ea3639John Hoford    static {
59235d2179374bec14040f20af91dc753e38ea3639John Hoford        portraitMap.put(ParameterInteger.sParameterType, BasicSlider.class);
60235d2179374bec14040f20af91dc753e38ea3639John Hoford        landscapeMap.put(ParameterInteger.sParameterType, TitledSlider.class);
61235d2179374bec14040f20af91dc753e38ea3639John Hoford        portraitMap.put(ParameterActionAndInt.sParameterType, ActionSlider.class);
62235d2179374bec14040f20af91dc753e38ea3639John Hoford        landscapeMap.put(ParameterActionAndInt.sParameterType, ActionSlider.class);
63166524339e9f8ad86c0922142b961776e0da3d8fJohn Hoford        portraitMap.put(ParameterStyles.sParameterType, StyleChooser.class);
64166524339e9f8ad86c0922142b961776e0da3d8fJohn Hoford        landscapeMap.put(ParameterStyles.sParameterType, StyleChooser.class);
65235d2179374bec14040f20af91dc753e38ea3639John Hoford    }
66235d2179374bec14040f20af91dc753e38ea3639John Hoford
67235d2179374bec14040f20af91dc753e38ea3639John Hoford    static Constructor getConstructor(Class cl) {
68235d2179374bec14040f20af91dc753e38ea3639John Hoford        try {
69235d2179374bec14040f20af91dc753e38ea3639John Hoford            return cl.getConstructor(Context.class, ViewGroup.class);
70235d2179374bec14040f20af91dc753e38ea3639John Hoford        } catch (Exception e) {
71235d2179374bec14040f20af91dc753e38ea3639John Hoford            return null;
72235d2179374bec14040f20af91dc753e38ea3639John Hoford        }
73235d2179374bec14040f20af91dc753e38ea3639John Hoford    }
74235d2179374bec14040f20af91dc753e38ea3639John Hoford
75235d2179374bec14040f20af91dc753e38ea3639John Hoford    public ParametricEditor() {
76235d2179374bec14040f20af91dc753e38ea3639John Hoford        super(ID);
77235d2179374bec14040f20af91dc753e38ea3639John Hoford    }
78235d2179374bec14040f20af91dc753e38ea3639John Hoford
79235d2179374bec14040f20af91dc753e38ea3639John Hoford    protected ParametricEditor(int id) {
80235d2179374bec14040f20af91dc753e38ea3639John Hoford        super(id);
81235d2179374bec14040f20af91dc753e38ea3639John Hoford    }
82235d2179374bec14040f20af91dc753e38ea3639John Hoford
83235d2179374bec14040f20af91dc753e38ea3639John Hoford    protected ParametricEditor(int id, int layoutID, int viewID) {
84235d2179374bec14040f20af91dc753e38ea3639John Hoford        super(id);
85235d2179374bec14040f20af91dc753e38ea3639John Hoford        mLayoutID = layoutID;
86235d2179374bec14040f20af91dc753e38ea3639John Hoford        mViewID = viewID;
87235d2179374bec14040f20af91dc753e38ea3639John Hoford    }
88235d2179374bec14040f20af91dc753e38ea3639John Hoford
89235d2179374bec14040f20af91dc753e38ea3639John Hoford    @Override
90235d2179374bec14040f20af91dc753e38ea3639John Hoford    public String calculateUserMessage(Context context, String effectName, Object parameterValue) {
91235d2179374bec14040f20af91dc753e38ea3639John Hoford        String apply = "";
92235d2179374bec14040f20af91dc753e38ea3639John Hoford
93235d2179374bec14040f20af91dc753e38ea3639John Hoford        if (mShowParameter == SHOW_VALUE_INT & useCompact(context)) {
94235d2179374bec14040f20af91dc753e38ea3639John Hoford           if (getLocalRepresentation() instanceof FilterBasicRepresentation) {
95235d2179374bec14040f20af91dc753e38ea3639John Hoford            FilterBasicRepresentation interval = (FilterBasicRepresentation) getLocalRepresentation();
9633de212ec780eaf2bc8d86908f07da33ea8dd7f2John Hoford                apply += " " + effectName.toUpperCase() + " " + interval.getStateRepresentation();
97235d2179374bec14040f20af91dc753e38ea3639John Hoford           } else {
9833de212ec780eaf2bc8d86908f07da33ea8dd7f2John Hoford                apply += " " + effectName.toUpperCase() + " " + parameterValue;
99235d2179374bec14040f20af91dc753e38ea3639John Hoford           }
100235d2179374bec14040f20af91dc753e38ea3639John Hoford        } else {
10133de212ec780eaf2bc8d86908f07da33ea8dd7f2John Hoford            apply += " " + effectName.toUpperCase();
102235d2179374bec14040f20af91dc753e38ea3639John Hoford        }
103235d2179374bec14040f20af91dc753e38ea3639John Hoford        return apply;
104235d2179374bec14040f20af91dc753e38ea3639John Hoford    }
105235d2179374bec14040f20af91dc753e38ea3639John Hoford
106235d2179374bec14040f20af91dc753e38ea3639John Hoford    @Override
107235d2179374bec14040f20af91dc753e38ea3639John Hoford    public void createEditor(Context context, FrameLayout frameLayout) {
108235d2179374bec14040f20af91dc753e38ea3639John Hoford        super.createEditor(context, frameLayout);
109235d2179374bec14040f20af91dc753e38ea3639John Hoford        unpack(mViewID, mLayoutID);
110235d2179374bec14040f20af91dc753e38ea3639John Hoford    }
111235d2179374bec14040f20af91dc753e38ea3639John Hoford
112235d2179374bec14040f20af91dc753e38ea3639John Hoford    @Override
113235d2179374bec14040f20af91dc753e38ea3639John Hoford    public void reflectCurrentFilter() {
114235d2179374bec14040f20af91dc753e38ea3639John Hoford        super.reflectCurrentFilter();
115235d2179374bec14040f20af91dc753e38ea3639John Hoford        if (getLocalRepresentation() != null
116235d2179374bec14040f20af91dc753e38ea3639John Hoford                && getLocalRepresentation() instanceof FilterBasicRepresentation) {
117235d2179374bec14040f20af91dc753e38ea3639John Hoford            FilterBasicRepresentation interval = (FilterBasicRepresentation) getLocalRepresentation();
118235d2179374bec14040f20af91dc753e38ea3639John Hoford            mControl.setPrameter(interval);
119235d2179374bec14040f20af91dc753e38ea3639John Hoford        }
120235d2179374bec14040f20af91dc753e38ea3639John Hoford    }
121235d2179374bec14040f20af91dc753e38ea3639John Hoford
122235d2179374bec14040f20af91dc753e38ea3639John Hoford    @Override
123235d2179374bec14040f20af91dc753e38ea3639John Hoford    public Control[] getControls() {
124235d2179374bec14040f20af91dc753e38ea3639John Hoford        BasicSlider slider = new BasicSlider();
125235d2179374bec14040f20af91dc753e38ea3639John Hoford        return new Control[] {
126235d2179374bec14040f20af91dc753e38ea3639John Hoford                slider
127235d2179374bec14040f20af91dc753e38ea3639John Hoford        };
128235d2179374bec14040f20af91dc753e38ea3639John Hoford    }
129235d2179374bec14040f20af91dc753e38ea3639John Hoford
130166524339e9f8ad86c0922142b961776e0da3d8fJohn Hoford    // TODO: need a better way to decide which representation
131235d2179374bec14040f20af91dc753e38ea3639John Hoford    static boolean useCompact(Context context) {
132235d2179374bec14040f20af91dc753e38ea3639John Hoford        WindowManager w = ((WindowManager) context.getSystemService(Context.WINDOW_SERVICE));
133235d2179374bec14040f20af91dc753e38ea3639John Hoford        Point size = new Point();
134235d2179374bec14040f20af91dc753e38ea3639John Hoford        w.getDefaultDisplay().getSize(size);
135235d2179374bec14040f20af91dc753e38ea3639John Hoford        if (size.x < size.y) { // if tall than wider
136235d2179374bec14040f20af91dc753e38ea3639John Hoford            return true;
137235d2179374bec14040f20af91dc753e38ea3639John Hoford        }
138235d2179374bec14040f20af91dc753e38ea3639John Hoford        if (size.x < MINIMUM_WIDTH) {
139235d2179374bec14040f20af91dc753e38ea3639John Hoford            return true;
140235d2179374bec14040f20af91dc753e38ea3639John Hoford        }
141235d2179374bec14040f20af91dc753e38ea3639John Hoford        if (size.y < MINIMUM_HEIGHT) {
142235d2179374bec14040f20af91dc753e38ea3639John Hoford            return true;
143235d2179374bec14040f20af91dc753e38ea3639John Hoford        }
144235d2179374bec14040f20af91dc753e38ea3639John Hoford        return false;
145235d2179374bec14040f20af91dc753e38ea3639John Hoford    }
146235d2179374bec14040f20af91dc753e38ea3639John Hoford
147166524339e9f8ad86c0922142b961776e0da3d8fJohn Hoford    protected Parameter getParameterToEdit(FilterRepresentation rep) {
148166524339e9f8ad86c0922142b961776e0da3d8fJohn Hoford        if (this instanceof Parameter) {
149166524339e9f8ad86c0922142b961776e0da3d8fJohn Hoford            return (Parameter) this;
150166524339e9f8ad86c0922142b961776e0da3d8fJohn Hoford        } else if (rep instanceof Parameter) {
151166524339e9f8ad86c0922142b961776e0da3d8fJohn Hoford            return ((Parameter) rep);
152166524339e9f8ad86c0922142b961776e0da3d8fJohn Hoford        }
153166524339e9f8ad86c0922142b961776e0da3d8fJohn Hoford        return null;
154166524339e9f8ad86c0922142b961776e0da3d8fJohn Hoford    }
155166524339e9f8ad86c0922142b961776e0da3d8fJohn Hoford
156235d2179374bec14040f20af91dc753e38ea3639John Hoford    @Override
157235d2179374bec14040f20af91dc753e38ea3639John Hoford    public void setUtilityPanelUI(View actionButton, View editControl) {
158166524339e9f8ad86c0922142b961776e0da3d8fJohn Hoford        mActionButton = actionButton;
159166524339e9f8ad86c0922142b961776e0da3d8fJohn Hoford        mEditControl = editControl;
160235d2179374bec14040f20af91dc753e38ea3639John Hoford        FilterRepresentation rep = getLocalRepresentation();
161166524339e9f8ad86c0922142b961776e0da3d8fJohn Hoford        Parameter param = getParameterToEdit(rep);
162166524339e9f8ad86c0922142b961776e0da3d8fJohn Hoford        if (param != null) {
163166524339e9f8ad86c0922142b961776e0da3d8fJohn Hoford            control(param, editControl);
164235d2179374bec14040f20af91dc753e38ea3639John Hoford        } else {
165235d2179374bec14040f20af91dc753e38ea3639John Hoford            mSeekBar = new SeekBar(editControl.getContext());
166235d2179374bec14040f20af91dc753e38ea3639John Hoford            LayoutParams lp = new LinearLayout.LayoutParams(
167235d2179374bec14040f20af91dc753e38ea3639John Hoford                    LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
168235d2179374bec14040f20af91dc753e38ea3639John Hoford            mSeekBar.setLayoutParams(lp);
169235d2179374bec14040f20af91dc753e38ea3639John Hoford            ((LinearLayout) editControl).addView(mSeekBar);
170235d2179374bec14040f20af91dc753e38ea3639John Hoford            mSeekBar.setOnSeekBarChangeListener(this);
171235d2179374bec14040f20af91dc753e38ea3639John Hoford        }
172235d2179374bec14040f20af91dc753e38ea3639John Hoford    }
173235d2179374bec14040f20af91dc753e38ea3639John Hoford
174235d2179374bec14040f20af91dc753e38ea3639John Hoford    protected void control(Parameter p, View editControl) {
175235d2179374bec14040f20af91dc753e38ea3639John Hoford        String pType = p.getParameterType();
176235d2179374bec14040f20af91dc753e38ea3639John Hoford        Context context = editControl.getContext();
177235d2179374bec14040f20af91dc753e38ea3639John Hoford        Class c = ((useCompact(context)) ? portraitMap : landscapeMap).get(pType);
178235d2179374bec14040f20af91dc753e38ea3639John Hoford
179235d2179374bec14040f20af91dc753e38ea3639John Hoford        if (c != null) {
180235d2179374bec14040f20af91dc753e38ea3639John Hoford            try {
181235d2179374bec14040f20af91dc753e38ea3639John Hoford                mControl = (Control) c.newInstance();
182166524339e9f8ad86c0922142b961776e0da3d8fJohn Hoford                p.setController(mControl);
183235d2179374bec14040f20af91dc753e38ea3639John Hoford                mControl.setUp((ViewGroup) editControl, p, this);
184235d2179374bec14040f20af91dc753e38ea3639John Hoford
185166524339e9f8ad86c0922142b961776e0da3d8fJohn Hoford
186235d2179374bec14040f20af91dc753e38ea3639John Hoford            } catch (Exception e) {
187235d2179374bec14040f20af91dc753e38ea3639John Hoford                Log.e(LOGTAG, "Error in loading Control ", e);
188235d2179374bec14040f20af91dc753e38ea3639John Hoford            }
189235d2179374bec14040f20af91dc753e38ea3639John Hoford        } else {
190235d2179374bec14040f20af91dc753e38ea3639John Hoford            Log.e(LOGTAG, "Unable to find class for " + pType);
191235d2179374bec14040f20af91dc753e38ea3639John Hoford            for (String string : portraitMap.keySet()) {
192235d2179374bec14040f20af91dc753e38ea3639John Hoford                Log.e(LOGTAG, "for " + string + " use " + portraitMap.get(string));
193235d2179374bec14040f20af91dc753e38ea3639John Hoford            }
194235d2179374bec14040f20af91dc753e38ea3639John Hoford        }
195235d2179374bec14040f20af91dc753e38ea3639John Hoford    }
196235d2179374bec14040f20af91dc753e38ea3639John Hoford
197235d2179374bec14040f20af91dc753e38ea3639John Hoford    @Override
198235d2179374bec14040f20af91dc753e38ea3639John Hoford    public void onProgressChanged(SeekBar sbar, int progress, boolean arg2) {
199235d2179374bec14040f20af91dc753e38ea3639John Hoford    }
200235d2179374bec14040f20af91dc753e38ea3639John Hoford
201235d2179374bec14040f20af91dc753e38ea3639John Hoford    @Override
202235d2179374bec14040f20af91dc753e38ea3639John Hoford    public void onStartTrackingTouch(SeekBar arg0) {
203235d2179374bec14040f20af91dc753e38ea3639John Hoford    }
204235d2179374bec14040f20af91dc753e38ea3639John Hoford
205235d2179374bec14040f20af91dc753e38ea3639John Hoford    @Override
206235d2179374bec14040f20af91dc753e38ea3639John Hoford    public void onStopTrackingTouch(SeekBar arg0) {
207235d2179374bec14040f20af91dc753e38ea3639John Hoford    }
208235d2179374bec14040f20af91dc753e38ea3639John Hoford}
209