EffectsBar.java revision d67ec74304219b3e70caae32fbfaadfb9c3c76f4
1/*
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;
25
26import com.android.gallery3d.R;
27import com.android.gallery3d.photoeditor.actions.EffectAction;
28
29/**
30 * Effects bar that contains all effects and shows them in categorized views.
31 */
32public class EffectsBar extends LinearLayout {
33
34    private final LayoutInflater inflater;
35    private FilterStack filterStack;
36    private EffectsMenu effectsMenu;
37    private View effectsGallery;
38    private EffectAction activeEffect;
39
40    public EffectsBar(Context context, AttributeSet attrs) {
41        super(context, attrs);
42        inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
43    }
44
45    public void initialize(FilterStack filterStack) {
46        this.filterStack = filterStack;
47
48        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        });
67    }
68
69    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++) {
75            setupEffect((EffectAction) effects.getChildAt(i));
76        }
77        scrollView.addView(effects);
78        scrollView.scrollTo(0, 0);
79        addView(effectsGallery, 0);
80    }
81
82    private void setupEffect(final EffectAction effect) {
83        effect.setOnClickListener(new View.OnClickListener() {
84
85            @Override
86            public void onClick(View v) {
87                if (isEnabled()) {
88                    // Set the clicked effect active before exiting effects-gallery.
89                    activeEffect = effect;
90                    exitEffectsGallery();
91                    EffectAction.ActionListener listener = new EffectAction.ActionListener() {
92
93                        @Override
94                        public void onOk() {
95                            exit(null);
96                        }
97                    };
98                    activeEffect.begin(getRootView(), filterStack, listener);
99                }
100            }
101        });
102    }
103
104    private boolean exitEffectsGallery() {
105        if (effectsGallery != null) {
106            if (activeEffect != null) {
107                // Detach the active effect to prevent it stopping effects-gallery from gc.
108                ((ViewGroup) activeEffect.getParent()).removeView(activeEffect);
109            }
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) {
119            SpinnerProgressDialog.showDialog((Toolbar) getRootView().findViewById(R.id.toolbar));
120            activeEffect.end(new Runnable() {
121
122                @Override
123                public void run() {
124                    SpinnerProgressDialog.dismissDialog();
125                    activeEffect = null;
126                    if (runnableOnDone != null) {
127                        runnableOnDone.run();
128                    }
129                }
130            });
131            return true;
132        }
133        return false;
134    }
135
136    /**
137     * Exits from effects gallery or the active effect; then executes the runnable if applicable.
138     *
139     * @return true if exiting from effects gallery or the active effect; otherwise, false.
140     */
141    public boolean exit(final Runnable runnableOnDone) {
142        // Exit effects-menu selected states.
143        effectsMenu.clearSelected();
144
145        if (exitActiveEffect(runnableOnDone)) {
146            return true;
147        }
148
149        boolean exited = exitEffectsGallery();
150        if (runnableOnDone != null) {
151            runnableOnDone.run();
152        }
153        return exited;
154    }
155}
156