1/*
2 * Copyright (C) 2013 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.content.res.Configuration;
21import android.graphics.Point;
22import android.util.Log;
23import android.view.View;
24import android.view.ViewGroup;
25import android.view.ViewGroup.LayoutParams;
26import android.view.WindowManager;
27import android.widget.FrameLayout;
28import android.widget.LinearLayout;
29import android.widget.SeekBar;
30
31import com.android.gallery3d.R;
32import com.android.gallery3d.filtershow.controller.ActionSlider;
33import com.android.gallery3d.filtershow.controller.BasicSlider;
34import com.android.gallery3d.filtershow.controller.ColorChooser;
35import com.android.gallery3d.filtershow.controller.Control;
36import com.android.gallery3d.filtershow.controller.Parameter;
37import com.android.gallery3d.filtershow.controller.ParameterActionAndInt;
38import com.android.gallery3d.filtershow.controller.ParameterBrightness;
39import com.android.gallery3d.filtershow.controller.ParameterColor;
40import com.android.gallery3d.filtershow.controller.ParameterHue;
41import com.android.gallery3d.filtershow.controller.ParameterInteger;
42import com.android.gallery3d.filtershow.controller.ParameterOpacity;
43import com.android.gallery3d.filtershow.controller.ParameterSaturation;
44import com.android.gallery3d.filtershow.controller.ParameterStyles;
45import com.android.gallery3d.filtershow.controller.SliderBrightness;
46import com.android.gallery3d.filtershow.controller.SliderHue;
47import com.android.gallery3d.filtershow.controller.SliderOpacity;
48import com.android.gallery3d.filtershow.controller.SliderSaturation;
49import com.android.gallery3d.filtershow.controller.StyleChooser;
50import com.android.gallery3d.filtershow.controller.TitledSlider;
51import com.android.gallery3d.filtershow.filters.FilterBasicRepresentation;
52import com.android.gallery3d.filtershow.filters.FilterRepresentation;
53
54import java.lang.reflect.Constructor;
55import java.util.HashMap;
56
57public class ParametricEditor extends Editor {
58    private int mLayoutID;
59    private int mViewID;
60    public static int ID = R.id.editorParametric;
61    private final String LOGTAG = "ParametricEditor";
62    protected Control mControl;
63    public static final int MINIMUM_WIDTH = 600;
64    public static final int MINIMUM_HEIGHT = 800;
65    View mActionButton;
66    View mEditControl;
67    static HashMap<String, Class> portraitMap = new HashMap<String, Class>();
68    static HashMap<String, Class> landscapeMap = new HashMap<String, Class>();
69    static {
70        portraitMap.put(ParameterSaturation.sParameterType, SliderSaturation.class);
71        landscapeMap.put(ParameterSaturation.sParameterType, SliderSaturation.class);
72        portraitMap.put(ParameterHue.sParameterType, SliderHue.class);
73        landscapeMap.put(ParameterHue.sParameterType, SliderHue.class);
74        portraitMap.put(ParameterOpacity.sParameterType, SliderOpacity.class);
75        landscapeMap.put(ParameterOpacity.sParameterType, SliderOpacity.class);
76        portraitMap.put(ParameterBrightness.sParameterType, SliderBrightness.class);
77        landscapeMap.put(ParameterBrightness.sParameterType, SliderBrightness.class);
78        portraitMap.put(ParameterColor.sParameterType, ColorChooser.class);
79        landscapeMap.put(ParameterColor.sParameterType, ColorChooser.class);
80
81        portraitMap.put(ParameterInteger.sParameterType, BasicSlider.class);
82        landscapeMap.put(ParameterInteger.sParameterType, TitledSlider.class);
83        portraitMap.put(ParameterActionAndInt.sParameterType, ActionSlider.class);
84        landscapeMap.put(ParameterActionAndInt.sParameterType, ActionSlider.class);
85        portraitMap.put(ParameterStyles.sParameterType, StyleChooser.class);
86        landscapeMap.put(ParameterStyles.sParameterType, StyleChooser.class);
87    }
88
89    static Constructor getConstructor(Class cl) {
90        try {
91            return cl.getConstructor(Context.class, ViewGroup.class);
92        } catch (Exception e) {
93            return null;
94        }
95    }
96
97    public ParametricEditor() {
98        super(ID);
99    }
100
101    protected ParametricEditor(int id) {
102        super(id);
103    }
104
105    protected ParametricEditor(int id, int layoutID, int viewID) {
106        super(id);
107        mLayoutID = layoutID;
108        mViewID = viewID;
109    }
110
111    @Override
112    public String calculateUserMessage(Context context, String effectName, Object parameterValue) {
113        String apply = "";
114
115        if (mShowParameter == SHOW_VALUE_INT & useCompact(context)) {
116           if (getLocalRepresentation() instanceof FilterBasicRepresentation) {
117            FilterBasicRepresentation interval = (FilterBasicRepresentation) getLocalRepresentation();
118                apply += " " + effectName.toUpperCase() + " " + interval.getStateRepresentation();
119           } else {
120                apply += " " + effectName.toUpperCase() + " " + parameterValue;
121           }
122        } else {
123            apply += " " + effectName.toUpperCase();
124        }
125        return apply;
126    }
127
128    @Override
129    public void createEditor(Context context, FrameLayout frameLayout) {
130        super.createEditor(context, frameLayout);
131        unpack(mViewID, mLayoutID);
132    }
133
134    @Override
135    public void reflectCurrentFilter() {
136        super.reflectCurrentFilter();
137        if (getLocalRepresentation() != null
138                && getLocalRepresentation() instanceof FilterBasicRepresentation) {
139            FilterBasicRepresentation interval = (FilterBasicRepresentation) getLocalRepresentation();
140            mControl.setPrameter(interval);
141        }
142    }
143
144    @Override
145    public Control[] getControls() {
146        BasicSlider slider = new BasicSlider();
147        return new Control[] {
148                slider
149        };
150    }
151
152    protected static boolean useCompact(Context context) {
153        return context.getResources().getConfiguration().orientation
154                == Configuration.ORIENTATION_PORTRAIT;
155    }
156
157    protected Parameter getParameterToEdit(FilterRepresentation rep) {
158        if (this instanceof Parameter) {
159            return (Parameter) this;
160        } else if (rep instanceof Parameter) {
161            return ((Parameter) rep);
162        }
163        return null;
164    }
165
166    @Override
167    public void setUtilityPanelUI(View actionButton, View editControl) {
168        mActionButton = actionButton;
169        mEditControl = editControl;
170        FilterRepresentation rep = getLocalRepresentation();
171        Parameter param = getParameterToEdit(rep);
172        if (param != null) {
173            control(param, editControl);
174        } else {
175            mSeekBar = new SeekBar(editControl.getContext());
176            LayoutParams lp = new LinearLayout.LayoutParams(
177                    LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
178            mSeekBar.setLayoutParams(lp);
179            ((LinearLayout) editControl).addView(mSeekBar);
180            mSeekBar.setOnSeekBarChangeListener(this);
181        }
182    }
183
184    protected void control(Parameter p, View editControl) {
185        String pType = p.getParameterType();
186        Context context = editControl.getContext();
187        Class c = ((useCompact(context)) ? portraitMap : landscapeMap).get(pType);
188
189        if (c != null) {
190            try {
191                mControl = (Control) c.newInstance();
192                p.setController(mControl);
193                mControl.setUp((ViewGroup) editControl, p, this);
194            } catch (Exception e) {
195                Log.e(LOGTAG, "Error in loading Control ", e);
196            }
197        } else {
198            Log.e(LOGTAG, "Unable to find class for " + pType);
199            for (String string : portraitMap.keySet()) {
200                Log.e(LOGTAG, "for " + string + " use " + portraitMap.get(string));
201            }
202        }
203    }
204
205    @Override
206    public void onProgressChanged(SeekBar sbar, int progress, boolean arg2) {
207    }
208
209    @Override
210    public void onStartTrackingTouch(SeekBar arg0) {
211    }
212
213    @Override
214    public void onStopTrackingTouch(SeekBar arg0) {
215    }
216}
217