OSDN Git Service

Merge commit '8539fc1a5406af9edd5414fba3615cc045aa63ec' into jb-mr2-release
[android-x86/packages-apps-Gallery2.git] / src / com / android / gallery3d / filtershow / PanelController.java
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;
18
19 import android.annotation.TargetApi;
20 import android.content.Context;
21 import android.text.Html;
22 import android.view.View;
23 import android.view.View.OnClickListener;
24 import android.view.ViewPropertyAnimator;
25 import android.widget.LinearLayout;
26 import android.widget.TextView;
27
28 import com.android.gallery3d.R;
29 import com.android.gallery3d.filtershow.editors.Editor;
30 import com.android.gallery3d.filtershow.filters.FilterRepresentation;
31 import com.android.gallery3d.filtershow.filters.ImageFilter;
32 import com.android.gallery3d.filtershow.filters.ImageFilterTinyPlanet;
33 import com.android.gallery3d.filtershow.imageshow.ImageCrop;
34 import com.android.gallery3d.filtershow.imageshow.ImageShow;
35 import com.android.gallery3d.filtershow.imageshow.MasterImage;
36 import com.android.gallery3d.filtershow.presets.ImagePreset;
37 import com.android.gallery3d.filtershow.ui.FilterIconButton;
38
39 import java.util.HashMap;
40 import java.util.Vector;
41
42 public class PanelController implements OnClickListener {
43     private static int PANEL = 0;
44     private static int COMPONENT = 1;
45     private static int VERTICAL_MOVE = 0;
46     private static int HORIZONTAL_MOVE = 1;
47     private static final int ANIM_DURATION = 200;
48     private static final String LOGTAG = "PanelController";
49     private boolean mDisableFilterButtons = false;
50     private boolean mFixedAspect = false;
51
52     public static boolean useAnimations() {
53         int currentapiVersion = android.os.Build.VERSION.SDK_INT;
54         if (currentapiVersion >= android.os.Build.VERSION_CODES.JELLY_BEAN) {
55             return true;
56         }
57         return false;
58     }
59
60     public void setFixedAspect(boolean t) {
61         mFixedAspect = t;
62     }
63
64     class Panel {
65         private final View mView;
66         private final View mContainer;
67         private int mPosition = 0;
68         private final Vector<View> mSubviews = new Vector<View>();
69
70         public Panel(View view, View container, int position) {
71             mView = view;
72             mContainer = container;
73             mPosition = position;
74         }
75
76         public void addView(View view) {
77             mSubviews.add(view);
78         }
79
80         public int getPosition() {
81             return mPosition;
82         }
83
84         public ViewPropertyAnimator unselect(int newPos, int move) {
85             ViewPropertyAnimator anim = mContainer.animate();
86             mView.setSelected(false);
87             mContainer.setX(0);
88             mContainer.setY(0);
89             int delta = 0;
90             int w = mRowPanel.getWidth();
91             int h = mRowPanel.getHeight();
92             if (move == HORIZONTAL_MOVE) {
93                 if (newPos > mPosition) {
94                     delta = -w;
95                 } else {
96                     delta = w;
97                 }
98                 if (PanelController.useAnimations()) {
99                     anim.x(delta);
100                 } else {
101                     mContainer.setX(delta);
102                 }
103             } else if (move == VERTICAL_MOVE) {
104                 if (PanelController.useAnimations()) {
105                     anim.y(h);
106                 } else {
107                     mContainer.setY(h);
108                 }
109             }
110             if (PanelController.useAnimations()) {
111                 anim.setDuration(ANIM_DURATION).withLayer().withEndAction(new Runnable() {
112                     @Override
113                     public void run() {
114                         mContainer.setVisibility(View.GONE);
115                         }
116                 });
117             } else {
118                 mContainer.setVisibility(View.GONE);
119             }
120             return anim;
121         }
122
123         public ViewPropertyAnimator select(int oldPos, int move) {
124             mView.setSelected(true);
125             mContainer.setVisibility(View.VISIBLE);
126             mContainer.setX(0);
127             mContainer.setY(0);
128             ViewPropertyAnimator anim = mContainer.animate();
129             int w = mRowPanel.getWidth();
130             int h = mRowPanel.getHeight();
131             if (useAnimations()) {
132                 if (move == HORIZONTAL_MOVE) {
133                     if (oldPos < mPosition) {
134                         mContainer.setX(w);
135                     } else {
136                         mContainer.setX(-w);
137                     }
138                     anim.x(0);
139                 } else if (move == VERTICAL_MOVE) {
140                     mContainer.setY(h);
141                     anim.y(0);
142                 }
143                 anim.setDuration(ANIM_DURATION).withLayer();
144             }
145             return anim;
146         }
147     }
148
149     class UtilityPanel {
150         private final Context mContext;
151         private final View mView;
152         private final LinearLayout mAccessoryViewList;
153         private Vector<View> mAccessoryViews = new Vector<View>();
154         private final TextView mTextView;
155         private boolean mSelected = false;
156         private String mEffectName = null;
157         private int mParameterValue = 0;
158         private boolean mShowParameterValue = false;
159         boolean firstTimeCropDisplayed = true;
160
161         public UtilityPanel(Context context, View view, View accessoryViewList,
162                 View textView) {
163             mContext = context;
164             mView = view;
165             mAccessoryViewList = (LinearLayout) accessoryViewList;
166             mTextView = (TextView) textView;
167         }
168
169         public boolean selected() {
170             return mSelected;
171         }
172
173         public void hideAccessoryViews() {
174             int childCount = mAccessoryViewList.getChildCount();
175             for (int i = 0; i < childCount; i++) {
176                 View child = mAccessoryViewList.getChildAt(i);
177                 child.setVisibility(View.GONE);
178             }
179         }
180
181         public void onNewValue(int value) {
182             mParameterValue = value;
183             updateText();
184         }
185
186         public void setEffectName(String effectName) {
187             mEffectName = effectName;
188             setShowParameter(true);
189         }
190
191         public void setShowParameter(boolean s) {
192             mShowParameterValue = s;
193             updateText();
194         }
195
196         public void updateText() {
197             String apply = mContext.getString(R.string.apply_effect);
198             if (mShowParameterValue) {
199                 mTextView.setText(Html.fromHtml(apply + " " + mEffectName + " "
200                         + mParameterValue));
201             } else {
202                 mTextView.setText(Html.fromHtml(apply + " " + mEffectName));
203             }
204         }
205
206         public ViewPropertyAnimator unselect() {
207             ViewPropertyAnimator anim = mView.animate();
208             mView.setX(0);
209             mView.setY(0);
210             int h = mRowPanel.getHeight();
211             anim.y(-h);
212             if (PanelController.useAnimations()) {
213                 anim.setDuration(ANIM_DURATION).withLayer().withEndAction(new Runnable() {
214                         @Override
215                     public void run() {
216                         mView.setVisibility(View.GONE);
217                     }
218                 });
219             } else {
220                 mView.setVisibility(View.GONE);
221             }
222             mSelected = false;
223             return anim;
224         }
225
226         public ViewPropertyAnimator select() {
227             mView.setVisibility(View.VISIBLE);
228             int h = mRowPanel.getHeight();
229             mView.setX(0);
230             mView.setY(-h);
231             updateText();
232             mSelected = true;
233             ViewPropertyAnimator anim = mView.animate();
234             anim.y(0);
235             anim.setDuration(ANIM_DURATION);
236             if (PanelController.useAnimations()) {
237                 anim.withLayer();
238             }
239             return anim;
240         }
241
242     }
243
244     class ViewType {
245         private final int mType;
246         private final View mView;
247
248         public ViewType(View view, int type) {
249             mView = view;
250             mType = type;
251         }
252
253         public int type() {
254             return mType;
255         }
256     }
257
258     private final HashMap<View, Panel> mPanels = new HashMap<View, Panel>();
259     private final HashMap<View, ViewType> mViews = new HashMap<View, ViewType>();
260     private final HashMap<String, ImageFilter> mFilters = new HashMap<String, ImageFilter>();
261     private final Vector<View> mImageViews = new Vector<View>();
262     private View mCurrentPanel = null;
263     private View mRowPanel = null;
264     private UtilityPanel mUtilityPanel = null;
265     private ImageShow mCurrentImage = null;
266     private Editor mCurrentEditor = null;
267     private FilterShowActivity mActivity = null;
268     private EditorPlaceHolder mEditorPlaceHolder = null;
269
270     public void setActivity(FilterShowActivity activity) {
271         mActivity = activity;
272     }
273
274     public void addView(View view) {
275         view.setOnClickListener(this);
276         mViews.put(view, new ViewType(view, COMPONENT));
277     }
278
279     public void addPanel(View view, View container, int position) {
280         mPanels.put(view, new Panel(view, container, position));
281         view.setOnClickListener(this);
282         mViews.put(view, new ViewType(view, PANEL));
283     }
284
285     public void addComponent(View aPanel, View component) {
286         Panel panel = mPanels.get(aPanel);
287         if (panel == null) {
288             return;
289         }
290         panel.addView(component);
291         component.setOnClickListener(this);
292         mViews.put(component, new ViewType(component, COMPONENT));
293     }
294
295     public void addFilter(ImageFilter filter) {
296         mFilters.put(filter.getName(), filter);
297     }
298
299     public void addImageView(View view) {
300         mImageViews.add(view);
301         ImageShow imageShow = (ImageShow) view;
302         imageShow.setPanelController(this);
303     }
304
305     public void resetParameters() {
306         showPanel(mCurrentPanel);
307         if (mCurrentImage != null) {
308             mCurrentImage.resetParameter();
309             mCurrentImage.select();
310             if (mCurrentEditor != null) {
311                 mCurrentEditor.reflectCurrentFilter();
312             }
313
314         }
315         if (mDisableFilterButtons) {
316             mActivity.enableFilterButtons();
317             mDisableFilterButtons = false;
318         }
319     }
320
321     public boolean onBackPressed() {
322         if (mUtilityPanel == null || !mUtilityPanel.selected()) {
323             return true;
324         }
325         HistoryAdapter adapter = MasterImage.getImage().getHistory();
326         int position = adapter.undo();
327         MasterImage.getImage().onHistoryItemClick(position);
328         showPanel(mCurrentPanel);
329         mCurrentImage.select();
330         if (mCurrentEditor != null) {
331             mCurrentEditor.reflectCurrentFilter();
332         }
333
334         if (mDisableFilterButtons) {
335             mActivity.enableFilterButtons();
336             mActivity.resetHistory();
337             mDisableFilterButtons = false;
338         }
339         return false;
340     }
341
342     public void onNewValue(int value) {
343         mUtilityPanel.onNewValue(value);
344     }
345
346     public void showParameter(boolean s) {
347         mUtilityPanel.setShowParameter(s);
348     }
349
350     public void setCurrentPanel(View panel) {
351         showPanel(panel);
352     }
353
354     public void setRowPanel(View rowPanel) {
355         mRowPanel = rowPanel;
356     }
357
358     public void setUtilityPanel(Context context, View utilityPanel,
359             View accessoryViewList, View textView) {
360         mUtilityPanel = new UtilityPanel(context, utilityPanel,
361                 accessoryViewList, textView);
362     }
363
364     @Override
365     public void onClick(View view) {
366         ViewType type = mViews.get(view);
367         if (type.type() == PANEL) {
368             showPanel(view);
369         } else if (type.type() == COMPONENT) {
370             showComponent(view);
371         }
372     }
373
374     public ImageShow showImageView(int id) {
375         ImageShow image = null;
376         mActivity.hideImageViews();
377         for (View view : mImageViews) {
378             if (view.getId() == id) {
379                 view.setVisibility(View.VISIBLE);
380                 image = (ImageShow) view;
381             } else {
382                 view.setVisibility(View.GONE);
383             }
384         }
385         return image;
386     }
387
388     public void showDefaultImageView() {
389         showImageView(R.id.imageShow);
390         MasterImage.getImage().setCurrentFilter(null);
391         MasterImage.getImage().setCurrentFilterRepresentation(null);
392     }
393
394     public void showPanel(View view) {
395         view.setVisibility(View.VISIBLE);
396         boolean removedUtilityPanel = false;
397         Panel current = mPanels.get(mCurrentPanel);
398         if (mUtilityPanel != null && mUtilityPanel.selected()) {
399             ViewPropertyAnimator anim1 = mUtilityPanel.unselect();
400             removedUtilityPanel = true;
401             if (anim1 != null) {
402                 anim1.start();
403             }
404             if (mCurrentPanel == view) {
405                 ViewPropertyAnimator anim2 = current.select(-1, VERTICAL_MOVE);
406                 if (anim2 != null) {
407                     anim2.start();
408                 }
409                 showDefaultImageView();
410             }
411         }
412
413         if (mCurrentPanel == view) {
414             return;
415         }
416
417         Panel panel = mPanels.get(view);
418         if (!removedUtilityPanel) {
419             int currentPos = -1;
420             if (current != null) {
421                 currentPos = current.getPosition();
422             }
423             ViewPropertyAnimator anim1 = panel.select(currentPos, HORIZONTAL_MOVE);
424             if (anim1 != null) {
425                 anim1.start();
426             }
427             if (current != null) {
428                 ViewPropertyAnimator anim2 = current.unselect(panel.getPosition(), HORIZONTAL_MOVE);
429                 if (anim2 != null) {
430                     anim2.start();
431                 }
432             }
433         } else {
434             ViewPropertyAnimator anim = panel.select(-1, VERTICAL_MOVE);
435             if (anim != null) {
436                 anim.start();
437             }
438         }
439
440         showDefaultImageView();
441         mCurrentPanel = view;
442     }
443
444     public ImagePreset getImagePreset() {
445         return MasterImage.getImage().getPreset();
446     }
447
448     public void useFilterRepresentation(FilterRepresentation filterRepresentation) {
449         if (filterRepresentation == null) {
450             return;
451         }
452         if (MasterImage.getImage().getCurrentFilterRepresentation() == filterRepresentation) {
453             return;
454         }
455         ImagePreset oldPreset = MasterImage.getImage().getPreset();
456         ImagePreset copy = new ImagePreset(oldPreset);
457         FilterRepresentation representation = copy.getRepresentation(filterRepresentation);
458         if (representation == null) {
459             copy.addFilter(filterRepresentation);
460         } else {
461             if (filterRepresentation.allowsMultipleInstances()) {
462                 representation.updateTempParametersFrom(filterRepresentation);
463                 copy.setHistoryName(filterRepresentation.getName());
464             }
465             filterRepresentation = representation;
466         }
467         MasterImage.getImage().setPreset(copy, true);
468         MasterImage.getImage().setCurrentFilterRepresentation(filterRepresentation);
469     }
470
471     public void showComponent(View view) {
472
473         boolean doPanelTransition = true;
474         if (view instanceof FilterIconButton) {
475             FilterRepresentation f = ((FilterIconButton) view).getFilterRepresentation();
476             if (f != null) {
477                 // FIXME: this check shouldn't be necessary (f shouldn't be null)
478                 doPanelTransition = f.showUtilityPanel();
479             }
480         }
481
482         if (mUtilityPanel != null && !mUtilityPanel.selected() && doPanelTransition) {
483             Panel current = mPanels.get(mCurrentPanel);
484             ViewPropertyAnimator anim1 = current.unselect(-1, VERTICAL_MOVE);
485             if (anim1 != null) {
486                 anim1.start();
487             }
488             if (mUtilityPanel != null) {
489                 ViewPropertyAnimator anim2 = mUtilityPanel.select();
490                 if (anim2 != null) {
491                     anim2.start();
492                 }
493             }
494         }
495
496         if (mCurrentImage != null) {
497             mCurrentImage.unselect();
498         }
499         mUtilityPanel.hideAccessoryViews();
500
501         if (view instanceof FilterIconButton) {
502             mCurrentEditor = null;
503             FilterIconButton component = (FilterIconButton) view;
504             FilterRepresentation representation = component.getFilterRepresentation();
505             if (representation != null) {
506                 mUtilityPanel.setEffectName(representation.getName());
507                 mUtilityPanel.setShowParameter(representation.showParameterValue());
508
509                 if (representation.getEditorId() != 0) {
510                     if (mEditorPlaceHolder.contains(representation.getEditorId())) {
511                         mCurrentEditor = mEditorPlaceHolder.showEditor(representation.getEditorId());
512                         mCurrentImage = mCurrentEditor.getImageShow();
513                         mCurrentEditor.setPanelController(this);
514                     } else {
515                         mCurrentImage = showImageView(representation.getEditorId());
516                     }
517                 }
518                 mUtilityPanel.setShowParameter(representation.showParameterValue());
519
520                 mCurrentImage.select();
521                 if (mCurrentEditor != null) {
522                     mCurrentEditor.reflectCurrentFilter();
523                     if (mCurrentEditor.useUtilityPanel()) {
524                         mCurrentEditor.openUtilityPanel(mUtilityPanel.mAccessoryViewList);
525                     }
526                 } else if (mCurrentImage.useUtilityPanel()) {
527                     mCurrentImage.openUtilityPanel(mUtilityPanel.mAccessoryViewList);
528                 }
529             }
530             return;
531         }
532
533         switch (view.getId()) {
534             case R.id.tinyplanetButton: {
535                 mCurrentImage = showImageView(R.id.imageTinyPlanet);
536                 String ename = mCurrentImage.getContext().getString(R.string.tinyplanet);
537                 mUtilityPanel.setEffectName(ename);
538                 if (!mDisableFilterButtons) {
539                     mActivity.disableFilterButtons();
540                     mDisableFilterButtons = true;
541                 }
542                 break;
543             }
544             case R.id.straightenButton: {
545                 mCurrentImage = showImageView(R.id.imageStraighten);
546                 String ename = mCurrentImage.getContext().getString(R.string.straighten);
547                 mUtilityPanel.setEffectName(ename);
548                 break;
549             }
550             case R.id.cropButton: {
551                 mCurrentImage = showImageView(R.id.imageCrop);
552                 String ename = mCurrentImage.getContext().getString(R.string.crop);
553                 mUtilityPanel.setEffectName(ename);
554                 mUtilityPanel.setShowParameter(false);
555                 if (mCurrentImage instanceof ImageCrop && mUtilityPanel.firstTimeCropDisplayed) {
556                     ((ImageCrop) mCurrentImage).clear();
557                     mUtilityPanel.firstTimeCropDisplayed = false;
558                 }
559                 ((ImageCrop) mCurrentImage).setFixedAspect(mFixedAspect);
560                 break;
561             }
562             case R.id.rotateButton: {
563                 mCurrentImage = showImageView(R.id.imageRotate);
564                 String ename = mCurrentImage.getContext().getString(R.string.rotate);
565                 mUtilityPanel.setEffectName(ename);
566                 break;
567             }
568             case R.id.flipButton: {
569                 mCurrentImage = showImageView(R.id.imageFlip);
570                 String ename = mCurrentImage.getContext().getString(R.string.mirror);
571                 mUtilityPanel.setEffectName(ename);
572                 mUtilityPanel.setShowParameter(false);
573                 break;
574             }
575             case R.id.applyEffect: {
576                 if (MasterImage.getImage().getCurrentFilter() instanceof ImageFilterTinyPlanet) {
577                     mActivity.saveImage();
578                 } else {
579                     if (mCurrentImage instanceof ImageCrop) {
580                         ((ImageCrop) mCurrentImage).saveAndSetPreset();
581                     }
582                     showPanel(mCurrentPanel);
583                 }
584                 MasterImage.getImage().invalidateFiltersOnly();
585                 break;
586             }
587         }
588         mCurrentImage.select();
589         if (mCurrentEditor != null) {
590             mCurrentEditor.reflectCurrentFilter();
591             if (mCurrentEditor.useUtilityPanel()) {
592                 mCurrentEditor.openUtilityPanel(mUtilityPanel.mAccessoryViewList);
593             }
594         } else if (mCurrentImage.useUtilityPanel()) {
595             mCurrentImage.openUtilityPanel(mUtilityPanel.mAccessoryViewList);
596         }
597
598     }
599
600     public void setEditorPlaceHolder(EditorPlaceHolder editorPlaceHolder) {
601         mEditorPlaceHolder = editorPlaceHolder;
602     }
603 }