1 /* 2 * Copyright (C) 2012 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.content.res.Configuration; 21 import android.util.AttributeSet; 22 import android.util.Log; 23 import android.view.LayoutInflater; 24 import android.view.Menu; 25 import android.view.MenuItem; 26 import android.view.View; 27 import android.view.ViewGroup; 28 import android.widget.Button; 29 import android.widget.FrameLayout; 30 import android.widget.LinearLayout; 31 import android.widget.PopupMenu; 32 import android.widget.SeekBar; 33 import android.widget.SeekBar.OnSeekBarChangeListener; 34 35 import com.android.gallery3d.R; 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.PrimaryImage; 40 import com.android.gallery3d.filtershow.pipeline.ImagePreset; 41 42 import java.util.ArrayList; 43 import java.util.Collection; 44 45 /** 46 * Base class for Editors Must contain a mImageShow and a top level view 47 */ 48 public class Editor implements OnSeekBarChangeListener, SwapButton.SwapButtonListener { 49 protected Context mContext; 50 protected View mView; 51 protected ImageShow mImageShow; 52 protected FrameLayout mFrameLayout; 53 protected SeekBar mSeekBar; 54 Button mEditTitle; 55 protected Button mFilterTitle; 56 protected int mID; 57 private final String LOGTAG = "Editor"; 58 protected boolean mChangesGeometry = false; 59 protected FilterRepresentation mLocalRepresentation = null; 60 protected byte mShowParameter = SHOW_VALUE_UNDEFINED; 61 private Button mButton; 62 public static byte SHOW_VALUE_UNDEFINED = -1; 63 public static byte SHOW_VALUE_OFF = 0; 64 public static byte SHOW_VALUE_INT = 1; 65 hackFixStrings(Menu menu)66 public static void hackFixStrings(Menu menu) { 67 int count = menu.size(); 68 for (int i = 0; i < count; i++) { 69 MenuItem item = menu.getItem(i); 70 item.setTitle(item.getTitle().toString().toUpperCase()); 71 } 72 } 73 calculateUserMessage(Context context, String effectName, Object parameterValue)74 public String calculateUserMessage(Context context, String effectName, Object parameterValue) { 75 return effectName.toUpperCase() + " " + parameterValue; 76 } 77 Editor(int id)78 protected Editor(int id) { 79 mID = id; 80 } 81 getID()82 public int getID() { 83 return mID; 84 } 85 showParameterValue()86 public byte showParameterValue() { 87 return mShowParameter; 88 } 89 showsSeekBar()90 public boolean showsSeekBar() { 91 return true; 92 } 93 setUpEditorUI(View actionButton, View editControl, Button editTitle, Button stateButton)94 public void setUpEditorUI(View actionButton, View editControl, 95 Button editTitle, Button stateButton) { 96 mEditTitle = editTitle; 97 mFilterTitle = stateButton; 98 mButton = editTitle; 99 PrimaryImage.getImage().resetGeometryImages(false); 100 setUtilityPanelUI(actionButton, editControl); 101 } 102 showsPopupIndicator()103 public boolean showsPopupIndicator() { 104 return false; 105 } 106 107 /** 108 * @param actionButton the would be the area for menu etc 109 * @param editControl this is the black area for sliders etc 110 */ setUtilityPanelUI(View actionButton, View editControl)111 public void setUtilityPanelUI(View actionButton, View editControl) { 112 113 AttributeSet aset; 114 Context context = editControl.getContext(); 115 LayoutInflater inflater = 116 (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 117 LinearLayout lp = (LinearLayout) inflater.inflate( 118 R.layout.filtershow_seekbar, (ViewGroup) editControl, true); 119 mSeekBar = (SeekBar) lp.findViewById(R.id.primarySeekBar); 120 mSeekBar.setOnSeekBarChangeListener(this); 121 mSeekBar.setVisibility(View.GONE); 122 if (context.getResources().getConfiguration().orientation 123 == Configuration.ORIENTATION_PORTRAIT) { 124 if (showsSeekBar()) { 125 mSeekBar.setVisibility(View.VISIBLE); 126 } 127 } 128 129 if (mButton != null) { 130 setMenuIcon(showsPopupIndicator()); 131 } 132 } 133 134 @Override onProgressChanged(SeekBar sbar, int progress, boolean arg2)135 public void onProgressChanged(SeekBar sbar, int progress, boolean arg2) { 136 137 } 138 setPanel()139 public void setPanel() { 140 141 } 142 createEditor(Context context, FrameLayout frameLayout)143 public void createEditor(Context context, FrameLayout frameLayout) { 144 mContext = context; 145 mFrameLayout = frameLayout; 146 mLocalRepresentation = null; 147 } 148 unpack(int viewid, int layoutid)149 protected void unpack(int viewid, int layoutid) { 150 151 if (mView == null) { 152 mView = mFrameLayout.findViewById(viewid); 153 if (mView == null) { 154 LayoutInflater inflater = (LayoutInflater) mContext.getSystemService 155 (Context.LAYOUT_INFLATER_SERVICE); 156 mView = inflater.inflate(layoutid, mFrameLayout, false); 157 mFrameLayout.addView(mView, mView.getLayoutParams()); 158 } 159 } 160 mImageShow = findImageShow(mView); 161 } 162 findImageShow(View view)163 private ImageShow findImageShow(View view) { 164 if (view instanceof ImageShow) { 165 return (ImageShow) view; 166 } 167 if (!(view instanceof ViewGroup)) { 168 return null; 169 } 170 ViewGroup vg = (ViewGroup) view; 171 int n = vg.getChildCount(); 172 for (int i = 0; i < n; i++) { 173 View v = vg.getChildAt(i); 174 if (v instanceof ImageShow) { 175 return (ImageShow) v; 176 } else if (v instanceof ViewGroup) { 177 return findImageShow(v); 178 } 179 } 180 return null; 181 } 182 getTopLevelView()183 public View getTopLevelView() { 184 return mView; 185 } 186 getImageShow()187 public ImageShow getImageShow() { 188 return mImageShow; 189 } 190 setVisibility(int visible)191 public void setVisibility(int visible) { 192 mView.setVisibility(visible); 193 } 194 getLocalRepresentation()195 public FilterRepresentation getLocalRepresentation() { 196 if (mLocalRepresentation == null) { 197 ImagePreset preset = PrimaryImage.getImage().getPreset(); 198 FilterRepresentation filterRepresentation = 199 PrimaryImage.getImage().getCurrentFilterRepresentation(); 200 mLocalRepresentation = preset.getFilterRepresentationCopyFrom(filterRepresentation); 201 if (mShowParameter == SHOW_VALUE_UNDEFINED && filterRepresentation != null) { 202 boolean show = filterRepresentation.showParameterValue(); 203 mShowParameter = show ? SHOW_VALUE_INT : SHOW_VALUE_OFF; 204 } 205 206 } 207 return mLocalRepresentation; 208 } 209 210 /** 211 * Call this to update the preset in PrimaryImage with the current representation 212 * returned by getLocalRepresentation. This causes the preview bitmap to be 213 * regenerated. 214 */ commitLocalRepresentation()215 public void commitLocalRepresentation() { 216 commitLocalRepresentation(getLocalRepresentation()); 217 } 218 219 /** 220 * Call this to update the preset in PrimaryImage with a given representation. 221 * This causes the preview bitmap to be regenerated. 222 */ commitLocalRepresentation(FilterRepresentation rep)223 public void commitLocalRepresentation(FilterRepresentation rep) { 224 ArrayList<FilterRepresentation> filter = new ArrayList<FilterRepresentation>(1); 225 filter.add(rep); 226 commitLocalRepresentation(filter); 227 } 228 229 /** 230 * Call this to update the preset in PrimaryImage with a collection of FilterRepresentations. 231 * This causes the preview bitmap to be regenerated. 232 */ commitLocalRepresentation(Collection<FilterRepresentation> reps)233 public void commitLocalRepresentation(Collection<FilterRepresentation> reps) { 234 ImagePreset preset = PrimaryImage.getImage().getPreset(); 235 preset.updateFilterRepresentations(reps); 236 if (mButton != null) { 237 updateText(); 238 } 239 if (mChangesGeometry) { 240 // Regenerate both the filtered and the geometry-only bitmaps 241 PrimaryImage.getImage().resetGeometryImages(true); 242 } 243 // Regenerate the filtered bitmap. 244 PrimaryImage.getImage().invalidateFiltersOnly(); 245 preset.fillImageStateAdapter(PrimaryImage.getImage().getState()); 246 } 247 248 /** 249 * This is called in response to a click to apply and leave the editor. 250 */ finalApplyCalled()251 public void finalApplyCalled() { 252 commitLocalRepresentation(); 253 } 254 updateText()255 protected void updateText() { 256 String s = ""; 257 if (mLocalRepresentation != null) { 258 s = mContext.getString(mLocalRepresentation.getTextId()); 259 } 260 mButton.setText(calculateUserMessage(mContext, s, "")); 261 } 262 263 /** 264 * called after the filter is set and the select is called 265 */ reflectCurrentFilter()266 public void reflectCurrentFilter() { 267 mLocalRepresentation = null; 268 FilterRepresentation representation = getLocalRepresentation(); 269 if (representation != null && mFilterTitle != null && representation.getTextId() != 0) { 270 String text = mContext.getString(representation.getTextId()).toUpperCase(); 271 mFilterTitle.setText(text); 272 updateText(); 273 } 274 } 275 useUtilityPanel()276 public boolean useUtilityPanel() { 277 return true; 278 } 279 openUtilityPanel(LinearLayout mAccessoryViewList)280 public void openUtilityPanel(LinearLayout mAccessoryViewList) { 281 setMenuIcon(showsPopupIndicator()); 282 if (mImageShow != null) { 283 mImageShow.openUtilityPanel(mAccessoryViewList); 284 } 285 } 286 setMenuIcon(boolean on)287 protected void setMenuIcon(boolean on) { 288 mEditTitle.setCompoundDrawablesRelativeWithIntrinsicBounds( 289 0, 0, on ? R.drawable.filtershow_menu_marker_rtl : 0, 0); 290 } 291 createMenu(int[] strId, View button)292 protected void createMenu(int[] strId, View button) { 293 PopupMenu pmenu = new PopupMenu(mContext, button); 294 Menu menu = pmenu.getMenu(); 295 for (int i = 0; i < strId.length; i++) { 296 menu.add(Menu.NONE, Menu.FIRST + i, 0, mContext.getString(strId[i])); 297 } 298 setMenuIcon(true); 299 300 } 301 getControls()302 public Control[] getControls() { 303 return null; 304 } 305 306 @Override onStartTrackingTouch(SeekBar arg0)307 public void onStartTrackingTouch(SeekBar arg0) { 308 309 } 310 311 @Override onStopTrackingTouch(SeekBar arg0)312 public void onStopTrackingTouch(SeekBar arg0) { 313 314 } 315 316 @Override swapLeft(MenuItem item)317 public void swapLeft(MenuItem item) { 318 319 } 320 321 @Override swapRight(MenuItem item)322 public void swapRight(MenuItem item) { 323 324 } 325 detach()326 public void detach() { 327 if (mImageShow != null) { 328 mImageShow.detach(); 329 } 330 } 331 } 332