blob: df6802531ff3fe46edf16ce15c74bef0235cbb58 [file] [log] [blame]
Yuli Huang6a12ad72011-09-12 22:25:30 +08001/*
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;
Yuli Huang5cac6d52012-03-12 16:34:41 +080020import android.content.DialogInterface;
Yuli Huangae70ae42011-10-01 02:05:06 +080021import android.os.Handler;
22import android.os.Message;
Yuli Huang6a12ad72011-09-12 22:25:30 +080023import android.util.AttributeSet;
24import android.view.MotionEvent;
Yuli Huangae70ae42011-10-01 02:05:06 +080025import android.view.View;
26import android.view.animation.Animation;
27import android.view.animation.AnimationUtils;
Yuli Huang6a12ad72011-09-12 22:25:30 +080028import android.widget.RelativeLayout;
29
30import com.android.gallery3d.R;
31
Yuli Huangae70ae42011-10-01 02:05:06 +080032import java.util.ArrayList;
33import java.util.List;
34
Yuli Huang6a12ad72011-09-12 22:25:30 +080035/**
Yuli Huangae70ae42011-10-01 02:05:06 +080036 * Toolbar that contains all tools and controls their idle/awake behaviors from UI thread.
Yuli Huang6a12ad72011-09-12 22:25:30 +080037 */
Yuli Huang5cac6d52012-03-12 16:34:41 +080038public class Toolbar extends RelativeLayout {
Yuli Huang6a12ad72011-09-12 22:25:30 +080039
40 private final ToolbarIdleHandler idleHandler;
Yuli Huangd67ec742012-03-07 01:18:08 +080041 private final List<View> tools = new ArrayList<View>();
Yuli Huang5cac6d52012-03-12 16:34:41 +080042 private SpinnerProgressDialog spinner;
Yuli Huang6a12ad72011-09-12 22:25:30 +080043
44 public Toolbar(Context context, AttributeSet attrs) {
45 super(context, attrs);
46
Yuli Huang5cac6d52012-03-12 16:34:41 +080047 setOnHierarchyChangeListener(new OnHierarchyChangeListener() {
48
49 @Override
50 public void onChildViewAdded(View parent, View child) {
51 // Photo-view isn't treated as a tool that responds to user events.
52 if (child.getId() != R.id.photo_view) {
53 tools.add(child);
54 }
55 }
56
57 @Override
58 public void onChildViewRemoved(View parent, View child) {
59 tools.remove(child);
60 }
61 });
62
63 idleHandler = new ToolbarIdleHandler(context, tools);
Yuli Huangae70ae42011-10-01 02:05:06 +080064 idleHandler.killIdle();
Yuli Huang6a12ad72011-09-12 22:25:30 +080065 }
66
Yuli Huang5cac6d52012-03-12 16:34:41 +080067 public void showSpinner() {
68 // There should be only one progress spinner running at a time.
69 if (spinner == null) {
70 spinner = new SpinnerProgressDialog(getContext(), tools,
71 new SpinnerProgressDialog.OnTouchListener() {
72
73 @Override
74 public boolean onTouch(DialogInterface dialog, MotionEvent event) {
75 // Kill idle even when the progress dialog is shown.
76 idleHandler.killIdle();
77 return true;
78 }
79 });
80 spinner.show();
Yuli Huangd67ec742012-03-07 01:18:08 +080081 }
82 }
83
Yuli Huang5cac6d52012-03-12 16:34:41 +080084 public void dismissSpinner() {
85 if (spinner != null) {
86 spinner.dismiss();
87 spinner = null;
88 }
Yuli Huangd67ec742012-03-07 01:18:08 +080089 }
90
91 @Override
Yuli Huang6a12ad72011-09-12 22:25:30 +080092 public boolean dispatchTouchEvent(MotionEvent ev) {
93 idleHandler.killIdle();
94 return super.dispatchTouchEvent(ev);
95 }
96
Yuli Huangd67ec742012-03-07 01:18:08 +080097 private static class ToolbarIdleHandler {
Yuli Huang6a12ad72011-09-12 22:25:30 +080098
Yuli Huangae70ae42011-10-01 02:05:06 +080099 private static final int MAKE_IDLE = 1;
100 private static final int TIMEOUT_IDLE = 8000;
Yuli Huang6a12ad72011-09-12 22:25:30 +0800101
Yuli Huangd67ec742012-03-07 01:18:08 +0800102 private final List<View> tools;
Yuli Huangae70ae42011-10-01 02:05:06 +0800103 private final Handler mainHandler;
104 private final Animation fadeIn;
105 private final Animation fadeOut;
106 private boolean idle;
Yuli Huang6a12ad72011-09-12 22:25:30 +0800107
Yuli Huang5cac6d52012-03-12 16:34:41 +0800108 public ToolbarIdleHandler(Context context, final List<View> tools) {
109 this.tools = tools;
Yuli Huangae70ae42011-10-01 02:05:06 +0800110 mainHandler = new Handler() {
Yuli Huang6a12ad72011-09-12 22:25:30 +0800111
Yuli Huangae70ae42011-10-01 02:05:06 +0800112 @Override
113 public void handleMessage(Message msg) {
114 switch (msg.what) {
115 case MAKE_IDLE:
116 if (!idle) {
117 idle = true;
Yuli Huangd67ec742012-03-07 01:18:08 +0800118 for (View view : tools) {
Yuli Huangae70ae42011-10-01 02:05:06 +0800119 view.startAnimation(fadeOut);
120 }
Yuli Huang6a12ad72011-09-12 22:25:30 +0800121 }
Yuli Huangae70ae42011-10-01 02:05:06 +0800122 break;
Yuli Huang6a12ad72011-09-12 22:25:30 +0800123 }
Yuli Huangae70ae42011-10-01 02:05:06 +0800124 }
125 };
126
127 fadeIn = AnimationUtils.loadAnimation(context, R.anim.photoeditor_fade_in);
128 fadeOut = AnimationUtils.loadAnimation(context, R.anim.photoeditor_fade_out);
129 }
130
131 public void killIdle() {
132 mainHandler.removeMessages(MAKE_IDLE);
133 if (idle) {
134 idle = false;
Yuli Huangd67ec742012-03-07 01:18:08 +0800135 for (View view : tools) {
Yuli Huangae70ae42011-10-01 02:05:06 +0800136 view.startAnimation(fadeIn);
137 }
Yuli Huang6a12ad72011-09-12 22:25:30 +0800138 }
Yuli Huangae70ae42011-10-01 02:05:06 +0800139 mainHandler.sendEmptyMessageDelayed(MAKE_IDLE, TIMEOUT_IDLE);
140 }
Yuli Huang6a12ad72011-09-12 22:25:30 +0800141 }
142}