OSDN Git Service

Fix b/5885342: Multiple PhotoEditor activities cause exceptions.
[android-x86/packages-apps-Gallery2.git] / src / com / android / gallery3d / photoeditor / Toolbar.java
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.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;
29
30 import com.android.gallery3d.R;
31
32 import java.util.ArrayList;
33 import java.util.List;
34
35 /**
36  * Toolbar that contains all tools and controls their idle/awake behaviors from UI thread.
37  */
38 public class Toolbar extends RelativeLayout implements OnHierarchyChangeListener {
39
40     private final ToolbarIdleHandler idleHandler;
41     private final List<View> tools = new ArrayList<View>();
42
43     public Toolbar(Context context, AttributeSet attrs) {
44         super(context, attrs);
45
46         setOnHierarchyChangeListener(this);
47         idleHandler = new ToolbarIdleHandler(this);
48         idleHandler.killIdle();
49     }
50
51     @Override
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) {
55             tools.add(child);
56         }
57     }
58
59     @Override
60     public void onChildViewRemoved(View parent, View child) {
61         tools.remove(child);
62     }
63
64     public List<View> getTools() {
65         return tools;
66     }
67
68     @Override
69     public boolean dispatchTouchEvent(MotionEvent ev) {
70         idleHandler.killIdle();
71         return super.dispatchTouchEvent(ev);
72     }
73
74     private static class ToolbarIdleHandler {
75
76         private static final int MAKE_IDLE = 1;
77         private static final int TIMEOUT_IDLE = 8000;
78
79         private final List<View> tools;
80         private final Handler mainHandler;
81         private final Animation fadeIn;
82         private final Animation fadeOut;
83         private boolean idle;
84
85         public ToolbarIdleHandler(Toolbar toolbar) {
86             tools = toolbar.getTools();
87             mainHandler = new Handler() {
88
89                 @Override
90                 public void handleMessage(Message msg) {
91                     switch (msg.what) {
92                         case MAKE_IDLE:
93                             if (!idle) {
94                                 idle = true;
95                                 for (View view : tools) {
96                                     view.startAnimation(fadeOut);
97                                 }
98                             }
99                             break;
100                     }
101                 }
102             };
103
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);
107         }
108
109         public void killIdle() {
110             mainHandler.removeMessages(MAKE_IDLE);
111             if (idle) {
112                 idle = false;
113                 for (View view : tools) {
114                     view.startAnimation(fadeIn);
115                 }
116             }
117             mainHandler.sendEmptyMessageDelayed(MAKE_IDLE, TIMEOUT_IDLE);
118         }
119     }
120 }