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;
202e1bc2e37f285bfaf302a0533a372d4373474d9cnicolasroardimport android.content.res.Configuration;
21235d2179374bec14040f20af91dc753e38ea3639John Hofordimport android.graphics.Point;
22235d2179374bec14040f20af91dc753e38ea3639John Hofordimport android.util.Log;
23235d2179374bec14040f20af91dc753e38ea3639John Hofordimport android.view.View;
24235d2179374bec14040f20af91dc753e38ea3639John Hofordimport android.view.ViewGroup;
25235d2179374bec14040f20af91dc753e38ea3639John Hofordimport android.view.ViewGroup.LayoutParams;
26235d2179374bec14040f20af91dc753e38ea3639John Hofordimport android.view.WindowManager;
27235d2179374bec14040f20af91dc753e38ea3639John Hofordimport android.widget.FrameLayout;
28235d2179374bec14040f20af91dc753e38ea3639John Hofordimport android.widget.LinearLayout;
29235d2179374bec14040f20af91dc753e38ea3639John Hofordimport android.widget.SeekBar;
30235d2179374bec14040f20af91dc753e38ea3639John Hoford
31235d2179374bec14040f20af91dc753e38ea3639John Hofordimport com.android.gallery3d.R;
32235d2179374bec14040f20af91dc753e38ea3639John Hofordimport com.android.gallery3d.filtershow.controller.ActionSlider;
33235d2179374bec14040f20af91dc753e38ea3639John Hofordimport com.android.gallery3d.filtershow.controller.BasicSlider;
34a4f38c0f8f70c998391780b672eea5d5e49277c7John Hofordimport com.android.gallery3d.filtershow.controller.ColorChooser;
35235d2179374bec14040f20af91dc753e38ea3639John Hofordimport com.android.gallery3d.filtershow.controller.Control;
36235d2179374bec14040f20af91dc753e38ea3639John Hofordimport com.android.gallery3d.filtershow.controller.Parameter;
37235d2179374bec14040f20af91dc753e38ea3639John Hofordimport com.android.gallery3d.filtershow.controller.ParameterActionAndInt;
38f4b659334750a5aa75f929d18857a2ab93c9d939John Hofordimport com.android.gallery3d.filtershow.controller.ParameterBrightness;
39a4f38c0f8f70c998391780b672eea5d5e49277c7John Hofordimport com.android.gallery3d.filtershow.controller.ParameterColor;
40f4b659334750a5aa75f929d18857a2ab93c9d939John Hofordimport com.android.gallery3d.filtershow.controller.ParameterHue;
41235d2179374bec14040f20af91dc753e38ea3639John Hofordimport com.android.gallery3d.filtershow.controller.ParameterInteger;
42f4b659334750a5aa75f929d18857a2ab93c9d939John Hofordimport com.android.gallery3d.filtershow.controller.ParameterOpacity;
43f4b659334750a5aa75f929d18857a2ab93c9d939John Hofordimport com.android.gallery3d.filtershow.controller.ParameterSaturation;
44166524339e9f8ad86c0922142b961776e0da3d8fJohn Hofordimport com.android.gallery3d.filtershow.controller.ParameterStyles;
45f4b659334750a5aa75f929d18857a2ab93c9d939John Hofordimport com.android.gallery3d.filtershow.controller.SliderBrightness;
46f4b659334750a5aa75f929d18857a2ab93c9d939John Hofordimport com.android.gallery3d.filtershow.controller.SliderHue;
47f4b659334750a5aa75f929d18857a2ab93c9d939John Hofordimport com.android.gallery3d.filtershow.controller.SliderOpacity;
48f4b659334750a5aa75f929d18857a2ab93c9d939John Hofordimport com.android.gallery3d.filtershow.controller.SliderSaturation;
49166524339e9f8ad86c0922142b961776e0da3d8fJohn Hofordimport com.android.gallery3d.filtershow.controller.StyleChooser;
50235d2179374bec14040f20af91dc753e38ea3639John Hofordimport com.android.gallery3d.filtershow.controller.TitledSlider;
51235d2179374bec14040f20af91dc753e38ea3639John Hofordimport com.android.gallery3d.filtershow.filters.FilterBasicRepresentation;
52235d2179374bec14040f20af91dc753e38ea3639John Hofordimport com.android.gallery3d.filtershow.filters.FilterRepresentation;
53235d2179374bec14040f20af91dc753e38ea3639John Hoford
54235d2179374bec14040f20af91dc753e38ea3639John Hofordimport java.lang.reflect.Constructor;
55235d2179374bec14040f20af91dc753e38ea3639John Hofordimport java.util.HashMap;
56235d2179374bec14040f20af91dc753e38ea3639John Hoford
57235d2179374bec14040f20af91dc753e38ea3639John Hofordpublic class ParametricEditor extends Editor {
58235d2179374bec14040f20af91dc753e38ea3639John Hoford    private int mLayoutID;
59235d2179374bec14040f20af91dc753e38ea3639John Hoford    private int mViewID;
60235d2179374bec14040f20af91dc753e38ea3639John Hoford    public static int ID = R.id.editorParametric;
61235d2179374bec14040f20af91dc753e38ea3639John Hoford    private final String LOGTAG = "ParametricEditor";
62235d2179374bec14040f20af91dc753e38ea3639John Hoford    protected Control mControl;
63235d2179374bec14040f20af91dc753e38ea3639John Hoford    public static final int MINIMUM_WIDTH = 600;
64235d2179374bec14040f20af91dc753e38ea3639John Hoford    public static final int MINIMUM_HEIGHT = 800;
65166524339e9f8ad86c0922142b961776e0da3d8fJohn Hoford    View mActionButton;
66166524339e9f8ad86c0922142b961776e0da3d8fJohn Hoford    View mEditControl;
67235d2179374bec14040f20af91dc753e38ea3639John Hoford    static HashMap<String, Class> portraitMap = new HashMap<String, Class>();
68235d2179374bec14040f20af91dc753e38ea3639John Hoford    static HashMap<String, Class> landscapeMap = new HashMap<String, Class>();
69235d2179374bec14040f20af91dc753e38ea3639John Hoford    static {
70f4b659334750a5aa75f929d18857a2ab93c9d939John Hoford        portraitMap.put(ParameterSaturation.sParameterType, SliderSaturation.class);
71f4b659334750a5aa75f929d18857a2ab93c9d939John Hoford        landscapeMap.put(ParameterSaturation.sParameterType, SliderSaturation.class);
72f4b659334750a5aa75f929d18857a2ab93c9d939John Hoford        portraitMap.put(ParameterHue.sParameterType, SliderHue.class);
73f4b659334750a5aa75f929d18857a2ab93c9d939John Hoford        landscapeMap.put(ParameterHue.sParameterType, SliderHue.class);
74f4b659334750a5aa75f929d18857a2ab93c9d939John Hoford        portraitMap.put(ParameterOpacity.sParameterType, SliderOpacity.class);
75f4b659334750a5aa75f929d18857a2ab93c9d939John Hoford        landscapeMap.put(ParameterOpacity.sParameterType, SliderOpacity.class);
76f4b659334750a5aa75f929d18857a2ab93c9d939John Hoford        portraitMap.put(ParameterBrightness.sParameterType, SliderBrightness.class);
77f4b659334750a5aa75f929d18857a2ab93c9d939John Hoford        landscapeMap.put(ParameterBrightness.sParameterType, SliderBrightness.class);
78a4f38c0f8f70c998391780b672eea5d5e49277c7John Hoford        portraitMap.put(ParameterColor.sParameterType, ColorChooser.class);
79a4f38c0f8f70c998391780b672eea5d5e49277c7John Hoford        landscapeMap.put(ParameterColor.sParameterType, ColorChooser.class);
80f4b659334750a5aa75f929d18857a2ab93c9d939John Hoford
81235d2179374bec14040f20af91dc753e38ea3639John Hoford        portraitMap.put(ParameterInteger.sParameterType, BasicSlider.class);
82235d2179374bec14040f20af91dc753e38ea3639John Hoford        landscapeMap.put(ParameterInteger.sParameterType, TitledSlider.class);
83235d2179374bec14040f20af91dc753e38ea3639John Hoford        portraitMap.put(ParameterActionAndInt.sParameterType, ActionSlider.class);
84235d2179374bec14040f20af91dc753e38ea3639John Hoford        landscapeMap.put(ParameterActionAndInt.sParameterType, ActionSlider.class);
85166524339e9f8ad86c0922142b961776e0da3d8fJohn Hoford        portraitMap.put(ParameterStyles.sParameterType, StyleChooser.class);
86166524339e9f8ad86c0922142b961776e0da3d8fJohn Hoford        landscapeMap.put(ParameterStyles.sParameterType, StyleChooser.class);
87235d2179374bec14040f20af91dc753e38ea3639John Hoford    }
88235d2179374bec14040f20af91dc753e38ea3639John Hoford
89235d2179374bec14040f20af91dc753e38ea3639John Hoford    static Constructor getConstructor(Class cl) {
90235d2179374bec14040f20af91dc753e38ea3639John Hoford        try {
91235d2179374bec14040f20af91dc753e38ea3639John Hoford            return cl.getConstructor(Context.class, ViewGroup.class);
92235d2179374bec14040f20af91dc753e38ea3639John Hoford        } catch (Exception e) {
93235d2179374bec14040f20af91dc753e38ea3639John Hoford            return null;
94235d2179374bec14040f20af91dc753e38ea3639John Hoford        }
95235d2179374bec14040f20af91dc753e38ea3639John Hoford    }
96235d2179374bec14040f20af91dc753e38ea3639John Hoford
97235d2179374bec14040f20af91dc753e38ea3639John Hoford    public ParametricEditor() {
98235d2179374bec14040f20af91dc753e38ea3639John Hoford        super(ID);
99235d2179374bec14040f20af91dc753e38ea3639John Hoford    }
100235d2179374bec14040f20af91dc753e38ea3639John Hoford
101235d2179374bec14040f20af91dc753e38ea3639John Hoford    protected ParametricEditor(int id) {
102235d2179374bec14040f20af91dc753e38ea3639John Hoford        super(id);
103235d2179374bec14040f20af91dc753e38ea3639John Hoford    }
104235d2179374bec14040f20af91dc753e38ea3639John Hoford
105235d2179374bec14040f20af91dc753e38ea3639John Hoford    protected ParametricEditor(int id, int layoutID, int viewID) {
106235d2179374bec14040f20af91dc753e38ea3639John Hoford        super(id);
107235d2179374bec14040f20af91dc753e38ea3639John Hoford        mLayoutID = layoutID;
108235d2179374bec14040f20af91dc753e38ea3639John Hoford        mViewID = viewID;
109235d2179374bec14040f20af91dc753e38ea3639John Hoford    }
110235d2179374bec14040f20af91dc753e38ea3639John Hoford
111235d2179374bec14040f20af91dc753e38ea3639John Hoford    @Override
112235d2179374bec14040f20af91dc753e38ea3639John Hoford    public String calculateUserMessage(Context context, String effectName, Object parameterValue) {
113235d2179374bec14040f20af91dc753e38ea3639John Hoford        String apply = "";
114235d2179374bec14040f20af91dc753e38ea3639John Hoford
115235d2179374bec14040f20af91dc753e38ea3639John Hoford        if (mShowParameter == SHOW_VALUE_INT & useCompact(context)) {
116235d2179374bec14040f20af91dc753e38ea3639John Hoford           if (getLocalRepresentation() instanceof FilterBasicRepresentation) {
117235d2179374bec14040f20af91dc753e38ea3639John Hoford            FilterBasicRepresentation interval = (FilterBasicRepresentation) getLocalRepresentation();
11833de212ec780eaf2bc8d86908f07da33ea8dd7f2John Hoford                apply += " " + effectName.toUpperCase() + " " + interval.getStateRepresentation();
119235d2179374bec14040f20af91dc753e38ea3639John Hoford           } else {
12033de212ec780eaf2bc8d86908f07da33ea8dd7f2John Hoford                apply += " " + effectName.toUpperCase() + " " + parameterValue;
121235d2179374bec14040f20af91dc753e38ea3639John Hoford           }
122235d2179374bec14040f20af91dc753e38ea3639John Hoford        } else {
12333de212ec780eaf2bc8d86908f07da33ea8dd7f2John Hoford            apply += " " + effectName.toUpperCase();
124235d2179374bec14040f20af91dc753e38ea3639John Hoford        }
125235d2179374bec14040f20af91dc753e38ea3639John Hoford        return apply;
126235d2179374bec14040f20af91dc753e38ea3639John Hoford    }
127235d2179374bec14040f20af91dc753e38ea3639John Hoford
128235d2179374bec14040f20af91dc753e38ea3639John Hoford    @Override
129235d2179374bec14040f20af91dc753e38ea3639John Hoford    public void createEditor(Context context, FrameLayout frameLayout) {
130235d2179374bec14040f20af91dc753e38ea3639John Hoford        super.createEditor(context, frameLayout);
131235d2179374bec14040f20af91dc753e38ea3639John Hoford        unpack(mViewID, mLayoutID);
132235d2179374bec14040f20af91dc753e38ea3639John Hoford    }
133235d2179374bec14040f20af91dc753e38ea3639John Hoford
134235d2179374bec14040f20af91dc753e38ea3639John Hoford    @Override
135235d2179374bec14040f20af91dc753e38ea3639John Hoford    public void reflectCurrentFilter() {
136235d2179374bec14040f20af91dc753e38ea3639John Hoford        super.reflectCurrentFilter();
137235d2179374bec14040f20af91dc753e38ea3639John Hoford        if (getLocalRepresentation() != null
138235d2179374bec14040f20af91dc753e38ea3639John Hoford                && getLocalRepresentation() instanceof FilterBasicRepresentation) {
139235d2179374bec14040f20af91dc753e38ea3639John Hoford            FilterBasicRepresentation interval = (FilterBasicRepresentation) getLocalRepresentation();
140235d2179374bec14040f20af91dc753e38ea3639John Hoford            mControl.setPrameter(interval);
141235d2179374bec14040f20af91dc753e38ea3639John Hoford        }
142235d2179374bec14040f20af91dc753e38ea3639John Hoford    }
143235d2179374bec14040f20af91dc753e38ea3639John Hoford
144235d2179374bec14040f20af91dc753e38ea3639John Hoford    @Override
145235d2179374bec14040f20af91dc753e38ea3639John Hoford    public Control[] getControls() {
146235d2179374bec14040f20af91dc753e38ea3639John Hoford        BasicSlider slider = new BasicSlider();
147235d2179374bec14040f20af91dc753e38ea3639John Hoford        return new Control[] {
148235d2179374bec14040f20af91dc753e38ea3639John Hoford                slider
149235d2179374bec14040f20af91dc753e38ea3639John Hoford        };
150235d2179374bec14040f20af91dc753e38ea3639John Hoford    }
151235d2179374bec14040f20af91dc753e38ea3639John Hoford
1520b33ef03f2023a49d82cd608d79bb9b59c791e06John Hoford    protected static boolean useCompact(Context context) {
1532e1bc2e37f285bfaf302a0533a372d4373474d9cnicolasroard        return context.getResources().getConfiguration().orientation
1542e1bc2e37f285bfaf302a0533a372d4373474d9cnicolasroard                == Configuration.ORIENTATION_PORTRAIT;
155235d2179374bec14040f20af91dc753e38ea3639John Hoford    }
156235d2179374bec14040f20af91dc753e38ea3639John Hoford
157166524339e9f8ad86c0922142b961776e0da3d8fJohn Hoford    protected Parameter getParameterToEdit(FilterRepresentation rep) {
158166524339e9f8ad86c0922142b961776e0da3d8fJohn Hoford        if (this instanceof Parameter) {
159166524339e9f8ad86c0922142b961776e0da3d8fJohn Hoford            return (Parameter) this;
160166524339e9f8ad86c0922142b961776e0da3d8fJohn Hoford        } else if (rep instanceof Parameter) {
161166524339e9f8ad86c0922142b961776e0da3d8fJohn Hoford            return ((Parameter) rep);
162166524339e9f8ad86c0922142b961776e0da3d8fJohn Hoford        }
163166524339e9f8ad86c0922142b961776e0da3d8fJohn Hoford        return null;
164166524339e9f8ad86c0922142b961776e0da3d8fJohn Hoford    }
165166524339e9f8ad86c0922142b961776e0da3d8fJohn Hoford
166235d2179374bec14040f20af91dc753e38ea3639John Hoford    @Override
167235d2179374bec14040f20af91dc753e38ea3639John Hoford    public void setUtilityPanelUI(View actionButton, View editControl) {
168166524339e9f8ad86c0922142b961776e0da3d8fJohn Hoford        mActionButton = actionButton;
169166524339e9f8ad86c0922142b961776e0da3d8fJohn Hoford        mEditControl = editControl;
170235d2179374bec14040f20af91dc753e38ea3639John Hoford        FilterRepresentation rep = getLocalRepresentation();
171166524339e9f8ad86c0922142b961776e0da3d8fJohn Hoford        Parameter param = getParameterToEdit(rep);
172166524339e9f8ad86c0922142b961776e0da3d8fJohn Hoford        if (param != null) {
173166524339e9f8ad86c0922142b961776e0da3d8fJohn Hoford            control(param, editControl);
174235d2179374bec14040f20af91dc753e38ea3639John Hoford        } else {
175235d2179374bec14040f20af91dc753e38ea3639John Hoford            mSeekBar = new SeekBar(editControl.getContext());
176235d2179374bec14040f20af91dc753e38ea3639John Hoford            LayoutParams lp = new LinearLayout.LayoutParams(
177235d2179374bec14040f20af91dc753e38ea3639John Hoford                    LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
178235d2179374bec14040f20af91dc753e38ea3639John Hoford            mSeekBar.setLayoutParams(lp);
179235d2179374bec14040f20af91dc753e38ea3639John Hoford            ((LinearLayout) editControl).addView(mSeekBar);
180235d2179374bec14040f20af91dc753e38ea3639John Hoford            mSeekBar.setOnSeekBarChangeListener(this);
181235d2179374bec14040f20af91dc753e38ea3639John Hoford        }
182235d2179374bec14040f20af91dc753e38ea3639John Hoford    }
183235d2179374bec14040f20af91dc753e38ea3639John Hoford
184235d2179374bec14040f20af91dc753e38ea3639John Hoford    protected void control(Parameter p, View editControl) {
185235d2179374bec14040f20af91dc753e38ea3639John Hoford        String pType = p.getParameterType();
186235d2179374bec14040f20af91dc753e38ea3639John Hoford        Context context = editControl.getContext();
187235d2179374bec14040f20af91dc753e38ea3639John Hoford        Class c = ((useCompact(context)) ? portraitMap : landscapeMap).get(pType);
188235d2179374bec14040f20af91dc753e38ea3639John Hoford
189235d2179374bec14040f20af91dc753e38ea3639John Hoford        if (c != null) {
190235d2179374bec14040f20af91dc753e38ea3639John Hoford            try {
191235d2179374bec14040f20af91dc753e38ea3639John Hoford                mControl = (Control) c.newInstance();
192166524339e9f8ad86c0922142b961776e0da3d8fJohn Hoford                p.setController(mControl);
193235d2179374bec14040f20af91dc753e38ea3639John Hoford                mControl.setUp((ViewGroup) editControl, p, this);
194235d2179374bec14040f20af91dc753e38ea3639John Hoford            } catch (Exception e) {
195235d2179374bec14040f20af91dc753e38ea3639John Hoford                Log.e(LOGTAG, "Error in loading Control ", e);
196235d2179374bec14040f20af91dc753e38ea3639John Hoford            }
197235d2179374bec14040f20af91dc753e38ea3639John Hoford        } else {
198235d2179374bec14040f20af91dc753e38ea3639John Hoford            Log.e(LOGTAG, "Unable to find class for " + pType);
199235d2179374bec14040f20af91dc753e38ea3639John Hoford            for (String string : portraitMap.keySet()) {
200235d2179374bec14040f20af91dc753e38ea3639John Hoford                Log.e(LOGTAG, "for " + string + " use " + portraitMap.get(string));
201235d2179374bec14040f20af91dc753e38ea3639John Hoford            }
202235d2179374bec14040f20af91dc753e38ea3639John Hoford        }
203235d2179374bec14040f20af91dc753e38ea3639John Hoford    }
204235d2179374bec14040f20af91dc753e38ea3639John Hoford
205235d2179374bec14040f20af91dc753e38ea3639John Hoford    @Override
206235d2179374bec14040f20af91dc753e38ea3639John Hoford    public void onProgressChanged(SeekBar sbar, int progress, boolean arg2) {
207235d2179374bec14040f20af91dc753e38ea3639John Hoford    }
208235d2179374bec14040f20af91dc753e38ea3639John Hoford
209235d2179374bec14040f20af91dc753e38ea3639John Hoford    @Override
210235d2179374bec14040f20af91dc753e38ea3639John Hoford    public void onStartTrackingTouch(SeekBar arg0) {
211235d2179374bec14040f20af91dc753e38ea3639John Hoford    }
212235d2179374bec14040f20af91dc753e38ea3639John Hoford
213235d2179374bec14040f20af91dc753e38ea3639John Hoford    @Override
214235d2179374bec14040f20af91dc753e38ea3639John Hoford    public void onStopTrackingTouch(SeekBar arg0) {
215235d2179374bec14040f20af91dc753e38ea3639John Hoford    }
216235d2179374bec14040f20af91dc753e38ea3639John Hoford}
217