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