2 * Copyright (C) 2010 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package com.android.gallery3d.photoeditor;
19 import android.content.Context;
20 import android.os.Handler;
21 import android.os.Message;
22 import android.util.AttributeSet;
23 import android.view.MotionEvent;
24 import android.view.View;
25 import android.view.ViewGroup.OnHierarchyChangeListener;
26 import android.view.animation.Animation;
27 import android.view.animation.AnimationUtils;
28 import android.widget.RelativeLayout;
30 import com.android.gallery3d.R;
32 import java.util.ArrayList;
33 import java.util.List;
36 * Toolbar that contains all tools and controls their idle/awake behaviors from UI thread.
38 public class Toolbar extends RelativeLayout implements OnHierarchyChangeListener {
40 private final ToolbarIdleHandler idleHandler;
41 private final List<View> tools = new ArrayList<View>();
43 public Toolbar(Context context, AttributeSet attrs) {
44 super(context, attrs);
46 setOnHierarchyChangeListener(this);
47 idleHandler = new ToolbarIdleHandler(this);
48 idleHandler.killIdle();
52 public void onChildViewAdded(View parent, View child) {
53 // Photo-view isn't treated as a tool that responds to user events.
54 if (child.getId() != R.id.photo_view) {
60 public void onChildViewRemoved(View parent, View child) {
64 public List<View> getTools() {
69 public boolean dispatchTouchEvent(MotionEvent ev) {
70 idleHandler.killIdle();
71 return super.dispatchTouchEvent(ev);
74 private static class ToolbarIdleHandler {
76 private static final int MAKE_IDLE = 1;
77 private static final int TIMEOUT_IDLE = 8000;
79 private final List<View> tools;
80 private final Handler mainHandler;
81 private final Animation fadeIn;
82 private final Animation fadeOut;
85 public ToolbarIdleHandler(Toolbar toolbar) {
86 tools = toolbar.getTools();
87 mainHandler = new Handler() {
90 public void handleMessage(Message msg) {
95 for (View view : tools) {
96 view.startAnimation(fadeOut);
104 Context context = toolbar.getContext();
105 fadeIn = AnimationUtils.loadAnimation(context, R.anim.photoeditor_fade_in);
106 fadeOut = AnimationUtils.loadAnimation(context, R.anim.photoeditor_fade_out);
109 public void killIdle() {
110 mainHandler.removeMessages(MAKE_IDLE);
113 for (View view : tools) {
114 view.startAnimation(fadeIn);
117 mainHandler.sendEmptyMessageDelayed(MAKE_IDLE, TIMEOUT_IDLE);