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.photoeditor;
     18 
     19 import android.os.Handler;
     20 import android.os.Message;
     21 import android.view.View;
     22 import android.view.ViewGroup;
     23 import android.view.animation.Animation;
     24 import android.view.animation.AnimationUtils;
     25 
     26 import java.util.ArrayList;
     27 import java.util.List;
     28 
     29 /**
     30  * Handler that controls idle/awake behaviors of toolbar's child views.
     31  */
     32 class ToolbarIdleHandler {
     33 
     34     private static final String IDLE_VIEW_TAG = "fadeOnIdle";
     35     private static final int MAKE_IDLE = 1;
     36     private static final int TIMEOUT_IDLE = 8000;
     37 
     38     private final List<View> childViews = new ArrayList<View>();
     39     private final Handler mainHandler;
     40     private final Animation fadeIn;
     41     private final Animation fadeOut;
     42     private boolean idle;
     43 
     44     /**
     45      * Constructor should only be invoked after toolbar has done inflation and added all its child
     46      * views; then its child views could be found by findViewById calls.
     47      */
     48     public ToolbarIdleHandler(ViewGroup toolbar) {
     49         mainHandler = new Handler() {
     50 
     51             @Override
     52             public void handleMessage(Message msg) {
     53                 switch (msg.what) {
     54                     case MAKE_IDLE:
     55                         if (!idle) {
     56                             idle = true;
     57                             for (View view : childViews) {
     58                                 makeIdle(view);
     59                             }
     60                         }
     61                         break;
     62                 }
     63             }
     64         };
     65 
     66         fadeIn = AnimationUtils.loadAnimation(toolbar.getContext(), R.anim.fade_in);
     67         fadeOut = AnimationUtils.loadAnimation(toolbar.getContext(), R.anim.fade_out);
     68 
     69         for (int i = 0; i < toolbar.getChildCount(); i++) {
     70             View view = toolbar.getChildAt(i);
     71             String tag = (String) view.getTag();
     72             if ((tag != null) && tag.equals(IDLE_VIEW_TAG)) {
     73                 childViews.add(view);
     74             }
     75         }
     76         // Alpha animations don't work well on scroll-view; apply them on container linear-layout.
     77         childViews.add(toolbar.findViewById(R.id.effects_container));
     78     }
     79 
     80     public void killIdle() {
     81         mainHandler.removeMessages(MAKE_IDLE);
     82         if (idle) {
     83             idle = false;
     84             for (View view : childViews) {
     85                 makeAwake(view);
     86             }
     87         }
     88         mainHandler.sendEmptyMessageDelayed(MAKE_IDLE, TIMEOUT_IDLE);
     89     }
     90 
     91     private void makeAwake(View view) {
     92         if (view.getVisibility() == View.VISIBLE) {
     93             view.startAnimation(fadeIn);
     94         }
     95     }
     96 
     97     private void makeIdle(View view) {
     98         if (view.getVisibility() == View.VISIBLE) {
     99             view.startAnimation(fadeOut);
    100         }
    101     }
    102 }
    103