Yuli Huang | 6a12ad7 | 2011-09-12 22:25:30 +0800 | [diff] [blame] | 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 | |
| 17 | package com.android.gallery3d.photoeditor; |
| 18 | |
| 19 | import android.content.Context; |
| 20 | import android.util.AttributeSet; |
| 21 | import android.view.LayoutInflater; |
| 22 | import android.view.View; |
| 23 | import android.view.ViewGroup; |
| 24 | import android.widget.LinearLayout; |
Yuli Huang | 6a12ad7 | 2011-09-12 22:25:30 +0800 | [diff] [blame] | 25 | |
| 26 | import com.android.gallery3d.R; |
| 27 | import com.android.gallery3d.photoeditor.actions.EffectAction; |
Yuli Huang | 6a12ad7 | 2011-09-12 22:25:30 +0800 | [diff] [blame] | 28 | |
| 29 | /** |
| 30 | * Effects bar that contains all effects and shows them in categorized views. |
| 31 | */ |
| 32 | public class EffectsBar extends LinearLayout { |
| 33 | |
Yuli Huang | b5c54b6 | 2011-10-18 12:42:50 +0800 | [diff] [blame] | 34 | private final LayoutInflater inflater; |
Yuli Huang | 6a12ad7 | 2011-09-12 22:25:30 +0800 | [diff] [blame] | 35 | private FilterStack filterStack; |
Yuli Huang | b5c54b6 | 2011-10-18 12:42:50 +0800 | [diff] [blame] | 36 | private EffectsMenu effectsMenu; |
Yuli Huang | 6a12ad7 | 2011-09-12 22:25:30 +0800 | [diff] [blame] | 37 | private View effectsGallery; |
Yuli Huang | 6a12ad7 | 2011-09-12 22:25:30 +0800 | [diff] [blame] | 38 | private EffectAction activeEffect; |
| 39 | |
| 40 | public EffectsBar(Context context, AttributeSet attrs) { |
| 41 | super(context, attrs); |
Yuli Huang | b5c54b6 | 2011-10-18 12:42:50 +0800 | [diff] [blame] | 42 | inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); |
Yuli Huang | 6a12ad7 | 2011-09-12 22:25:30 +0800 | [diff] [blame] | 43 | } |
| 44 | |
| 45 | public void initialize(FilterStack filterStack) { |
| 46 | this.filterStack = filterStack; |
Yuli Huang | 6a12ad7 | 2011-09-12 22:25:30 +0800 | [diff] [blame] | 47 | |
Yuli Huang | b5c54b6 | 2011-10-18 12:42:50 +0800 | [diff] [blame] | 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 | }); |
Yuli Huang | 6a12ad7 | 2011-09-12 22:25:30 +0800 | [diff] [blame] | 67 | } |
| 68 | |
Yuli Huang | b5c54b6 | 2011-10-18 12:42:50 +0800 | [diff] [blame] | 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++) { |
Yuli Huang | dc1b41c | 2011-12-02 19:55:05 +0800 | [diff] [blame] | 75 | setupEffect((EffectAction) effects.getChildAt(i)); |
Yuli Huang | b5c54b6 | 2011-10-18 12:42:50 +0800 | [diff] [blame] | 76 | } |
| 77 | scrollView.addView(effects); |
| 78 | scrollView.scrollTo(0, 0); |
| 79 | addView(effectsGallery, 0); |
Yuli Huang | 6a12ad7 | 2011-09-12 22:25:30 +0800 | [diff] [blame] | 80 | } |
| 81 | |
Yuli Huang | dc1b41c | 2011-12-02 19:55:05 +0800 | [diff] [blame] | 82 | private void setupEffect(final EffectAction effect) { |
| 83 | effect.setOnClickListener(new View.OnClickListener() { |
Yuli Huang | 6a12ad7 | 2011-09-12 22:25:30 +0800 | [diff] [blame] | 84 | |
| 85 | @Override |
Yuli Huang | dc1b41c | 2011-12-02 19:55:05 +0800 | [diff] [blame] | 86 | public void onClick(View v) { |
Yuli Huang | 6a12ad7 | 2011-09-12 22:25:30 +0800 | [diff] [blame] | 87 | if (isEnabled()) { |
| 88 | // Set the clicked effect active before exiting effects-gallery. |
| 89 | activeEffect = effect; |
| 90 | exitEffectsGallery(); |
Yuli Huang | dc1b41c | 2011-12-02 19:55:05 +0800 | [diff] [blame] | 91 | EffectAction.ActionListener listener = new EffectAction.ActionListener() { |
Yuli Huang | 6a12ad7 | 2011-09-12 22:25:30 +0800 | [diff] [blame] | 92 | |
Yuli Huang | dc1b41c | 2011-12-02 19:55:05 +0800 | [diff] [blame] | 93 | @Override |
| 94 | public void onOk() { |
| 95 | exit(null); |
| 96 | } |
| 97 | }; |
Yuli Huang | 26c0354 | 2011-12-03 03:23:52 +0800 | [diff] [blame] | 98 | activeEffect.begin(getRootView(), filterStack, listener); |
Yuli Huang | dc1b41c | 2011-12-02 19:55:05 +0800 | [diff] [blame] | 99 | } |
Yuli Huang | 6a12ad7 | 2011-09-12 22:25:30 +0800 | [diff] [blame] | 100 | } |
| 101 | }); |
| 102 | } |
| 103 | |
Yuli Huang | 6a12ad7 | 2011-09-12 22:25:30 +0800 | [diff] [blame] | 104 | private boolean exitEffectsGallery() { |
| 105 | if (effectsGallery != null) { |
| 106 | if (activeEffect != null) { |
Yuli Huang | b5c54b6 | 2011-10-18 12:42:50 +0800 | [diff] [blame] | 107 | // Detach the active effect to prevent it stopping effects-gallery from gc. |
Yuli Huang | dc1b41c | 2011-12-02 19:55:05 +0800 | [diff] [blame] | 108 | ((ViewGroup) activeEffect.getParent()).removeView(activeEffect); |
Yuli Huang | 6a12ad7 | 2011-09-12 22:25:30 +0800 | [diff] [blame] | 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) { |
Yuli Huang | 5cac6d5 | 2012-03-12 16:34:41 +0800 | [diff] [blame] | 119 | final Toolbar toolbar = (Toolbar) getRootView().findViewById(R.id.toolbar); |
| 120 | toolbar.showSpinner(); |
Yuli Huang | 6a12ad7 | 2011-09-12 22:25:30 +0800 | [diff] [blame] | 121 | activeEffect.end(new Runnable() { |
| 122 | |
| 123 | @Override |
| 124 | public void run() { |
Yuli Huang | 5cac6d5 | 2012-03-12 16:34:41 +0800 | [diff] [blame] | 125 | toolbar.dismissSpinner(); |
Yuli Huang | 6a12ad7 | 2011-09-12 22:25:30 +0800 | [diff] [blame] | 126 | 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 Huang | b5c54b6 | 2011-10-18 12:42:50 +0800 | [diff] [blame] | 144 | effectsMenu.clearSelected(); |
Yuli Huang | 6a12ad7 | 2011-09-12 22:25:30 +0800 | [diff] [blame] | 145 | |
| 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 | } |