Home | History | Annotate | Download | only in photoeditor
      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;
     25 
     26 import com.android.gallery3d.R;
     27 import com.android.gallery3d.photoeditor.actions.EffectAction;
     28 
     29 /**
     30  * Effects bar that contains all effects and shows them in categorized views.
     31  */
     32 public 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             final Toolbar toolbar = (Toolbar) getRootView().findViewById(R.id.toolbar);
    120             toolbar.showSpinner();
    121             activeEffect.end(new Runnable() {
    122 
    123                 @Override
    124                 public void run() {
    125                     toolbar.dismissSpinner();
    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.
    144         effectsMenu.clearSelected();
    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 }
    157