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.os.Handler; 21import android.os.Message; 22import android.util.AttributeSet; 23import android.view.MotionEvent; 24import android.view.View; 25import android.view.animation.Animation; 26import android.view.animation.AnimationUtils; 27import android.widget.RelativeLayout; 28 29import com.android.gallery3d.R; 30 31import java.util.ArrayList; 32import java.util.List; 33 34/** 35 * Toolbar that contains all tools and controls their idle/awake behaviors from UI thread. 36 */ 37public class Toolbar extends RelativeLayout { 38 39 private final ToolbarIdleHandler idleHandler; 40 41 public Toolbar(Context context, AttributeSet attrs) { 42 super(context, attrs); 43 44 idleHandler = new ToolbarIdleHandler(context); 45 setOnHierarchyChangeListener(idleHandler); 46 idleHandler.killIdle(); 47 } 48 49 @Override 50 public boolean dispatchTouchEvent(MotionEvent ev) { 51 idleHandler.killIdle(); 52 return super.dispatchTouchEvent(ev); 53 } 54 55 private static class ToolbarIdleHandler implements OnHierarchyChangeListener { 56 57 private static final int MAKE_IDLE = 1; 58 private static final int TIMEOUT_IDLE = 8000; 59 60 private final List<View> childViews = new ArrayList<View>(); 61 private final Handler mainHandler; 62 private final Animation fadeIn; 63 private final Animation fadeOut; 64 private boolean idle; 65 66 public ToolbarIdleHandler(Context context) { 67 mainHandler = new Handler() { 68 69 @Override 70 public void handleMessage(Message msg) { 71 switch (msg.what) { 72 case MAKE_IDLE: 73 if (!idle) { 74 idle = true; 75 for (View view : childViews) { 76 view.startAnimation(fadeOut); 77 } 78 } 79 break; 80 } 81 } 82 }; 83 84 fadeIn = AnimationUtils.loadAnimation(context, R.anim.photoeditor_fade_in); 85 fadeOut = AnimationUtils.loadAnimation(context, R.anim.photoeditor_fade_out); 86 } 87 88 public void killIdle() { 89 mainHandler.removeMessages(MAKE_IDLE); 90 if (idle) { 91 idle = false; 92 for (View view : childViews) { 93 view.startAnimation(fadeIn); 94 } 95 } 96 mainHandler.sendEmptyMessageDelayed(MAKE_IDLE, TIMEOUT_IDLE); 97 } 98 99 @Override 100 public void onChildViewAdded(View parent, View child) { 101 // All child views, except photo-view, will fade out on inactivity timeout. 102 if (child.getId() != R.id.photo_view) { 103 childViews.add(child); 104 } 105 } 106 107 @Override 108 public void onChildViewRemoved(View parent, View child) { 109 childViews.remove(child); 110 } 111 } 112} 113