blob: fad0b90ae9cf522994ae6713a71729c4199c1b60 [file] [log] [blame]
Yuli Huang6a12ad72011-09-12 22:25:30 +08001/*
2 * Copyright (C) 2010 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.photoeditor;
18
19import android.content.Context;
20import android.util.AttributeSet;
21import android.view.LayoutInflater;
22import android.view.View;
23import android.view.ViewGroup;
24import android.widget.LinearLayout;
Yuli Huang6a12ad72011-09-12 22:25:30 +080025
26import com.android.gallery3d.R;
27import com.android.gallery3d.photoeditor.actions.EffectAction;
Yuli Huang6a12ad72011-09-12 22:25:30 +080028
29/**
30 * Effects bar that contains all effects and shows them in categorized views.
31 */
32public class EffectsBar extends LinearLayout {
33
Yuli Huangb5c54b62011-10-18 12:42:50 +080034 private final LayoutInflater inflater;
Yuli Huang6a12ad72011-09-12 22:25:30 +080035 private FilterStack filterStack;
Yuli Huangb5c54b62011-10-18 12:42:50 +080036 private EffectsMenu effectsMenu;
Yuli Huang6a12ad72011-09-12 22:25:30 +080037 private View effectsGallery;
Yuli Huang6a12ad72011-09-12 22:25:30 +080038 private EffectAction activeEffect;
39
40 public EffectsBar(Context context, AttributeSet attrs) {
41 super(context, attrs);
Yuli Huangb5c54b62011-10-18 12:42:50 +080042 inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
Yuli Huang6a12ad72011-09-12 22:25:30 +080043 }
44
45 public void initialize(FilterStack filterStack) {
46 this.filterStack = filterStack;
Yuli Huang6a12ad72011-09-12 22:25:30 +080047
Yuli Huangb5c54b62011-10-18 12:42:50 +080048 effectsMenu = (EffectsMenu) findViewById(R.id.effects_menu);
49 effectsMenu.setOnToggleListener(new EffectsMenu.OnToggleListener() {
50
51 @Override
52 public boolean onToggle(boolean isSelected, final int effectsId) {
53 // Create and show effects-gallery only if the clicked toggle isn't selected or it's
54 // selected but showing an active effect instead of effects-gallery. Set the clicked
55 // toggle selected only when its effects-gallery will be created and shown.
56 boolean select = !isSelected || (effectsGallery == null);
57 exit(select ? new Runnable() {
58
59 @Override
60 public void run() {
61 createEffectsGallery(effectsId);
62 }
63 } : null);
64 return select;
65 }
66 });
Yuli Huang6a12ad72011-09-12 22:25:30 +080067 }
68
Yuli Huangb5c54b62011-10-18 12:42:50 +080069 private void createEffectsGallery(int effectsId) {
70 // Inflate scrollable effects-gallery and desired effects into effects-bar.
71 effectsGallery = inflater.inflate(R.layout.photoeditor_effects_gallery, this, false);
72 ViewGroup scrollView = (ViewGroup) effectsGallery.findViewById(R.id.scroll_view);
73 ViewGroup effects = (ViewGroup) inflater.inflate(effectsId, scrollView, false);
74 for (int i = 0; i < effects.getChildCount(); i++) {
Yuli Huangdc1b41c2011-12-02 19:55:05 +080075 setupEffect((EffectAction) effects.getChildAt(i));
Yuli Huangb5c54b62011-10-18 12:42:50 +080076 }
77 scrollView.addView(effects);
78 scrollView.scrollTo(0, 0);
79 addView(effectsGallery, 0);
Yuli Huang6a12ad72011-09-12 22:25:30 +080080 }
81
Yuli Huangdc1b41c2011-12-02 19:55:05 +080082 private void setupEffect(final EffectAction effect) {
83 effect.setOnClickListener(new View.OnClickListener() {
Yuli Huang6a12ad72011-09-12 22:25:30 +080084
85 @Override
Yuli Huangdc1b41c2011-12-02 19:55:05 +080086 public void onClick(View v) {
Yuli Huang6a12ad72011-09-12 22:25:30 +080087 if (isEnabled()) {
88 // Set the clicked effect active before exiting effects-gallery.
89 activeEffect = effect;
90 exitEffectsGallery();
Yuli Huangdc1b41c2011-12-02 19:55:05 +080091 EffectAction.ActionListener listener = new EffectAction.ActionListener() {
Yuli Huang6a12ad72011-09-12 22:25:30 +080092
Yuli Huangdc1b41c2011-12-02 19:55:05 +080093 @Override
94 public void onOk() {
95 exit(null);
96 }
97 };
Yuli Huang26c03542011-12-03 03:23:52 +080098 activeEffect.begin(getRootView(), filterStack, listener);
Yuli Huangdc1b41c2011-12-02 19:55:05 +080099 }
Yuli Huang6a12ad72011-09-12 22:25:30 +0800100 }
101 });
102 }
103
Yuli Huang6a12ad72011-09-12 22:25:30 +0800104 private boolean exitEffectsGallery() {
105 if (effectsGallery != null) {
106 if (activeEffect != null) {
Yuli Huangb5c54b62011-10-18 12:42:50 +0800107 // Detach the active effect to prevent it stopping effects-gallery from gc.
Yuli Huangdc1b41c2011-12-02 19:55:05 +0800108 ((ViewGroup) activeEffect.getParent()).removeView(activeEffect);
Yuli Huang6a12ad72011-09-12 22:25:30 +0800109 }
110 removeView(effectsGallery);
111 effectsGallery = null;
112 return true;
113 }
114 return false;
115 }
116
117 private boolean exitActiveEffect(final Runnable runnableOnDone) {
118 if (activeEffect != null) {
Yuli Huang5cac6d52012-03-12 16:34:41 +0800119 final Toolbar toolbar = (Toolbar) getRootView().findViewById(R.id.toolbar);
120 toolbar.showSpinner();
Yuli Huang6a12ad72011-09-12 22:25:30 +0800121 activeEffect.end(new Runnable() {
122
123 @Override
124 public void run() {
Yuli Huang5cac6d52012-03-12 16:34:41 +0800125 toolbar.dismissSpinner();
Yuli Huang6a12ad72011-09-12 22:25:30 +0800126 activeEffect = null;
127 if (runnableOnDone != null) {
128 runnableOnDone.run();
129 }
130 }
131 });
132 return true;
133 }
134 return false;
135 }
136
137 /**
138 * Exits from effects gallery or the active effect; then executes the runnable if applicable.
139 *
140 * @return true if exiting from effects gallery or the active effect; otherwise, false.
141 */
142 public boolean exit(final Runnable runnableOnDone) {
143 // Exit effects-menu selected states.
Yuli Huangb5c54b62011-10-18 12:42:50 +0800144 effectsMenu.clearSelected();
Yuli Huang6a12ad72011-09-12 22:25:30 +0800145
146 if (exitActiveEffect(runnableOnDone)) {
147 return true;
148 }
149
150 boolean exited = exitEffectsGallery();
151 if (runnableOnDone != null) {
152 runnableOnDone.run();
153 }
154 return exited;
155 }
156}