Yuli Huang | 6a12ad7 | 2011-09-12 22:25:30 +0800 | [diff] [blame] | 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; |
Yuli Huang | 5cac6d5 | 2012-03-12 16:34:41 +0800 | [diff] [blame] | 20 | import android.content.DialogInterface; |
Yuli Huang | ae70ae4 | 2011-10-01 02:05:06 +0800 | [diff] [blame] | 21 | import android.os.Handler; |
| 22 | import android.os.Message; |
Yuli Huang | 6a12ad7 | 2011-09-12 22:25:30 +0800 | [diff] [blame] | 23 | import android.util.AttributeSet; |
| 24 | import android.view.MotionEvent; |
Yuli Huang | ae70ae4 | 2011-10-01 02:05:06 +0800 | [diff] [blame] | 25 | import android.view.View; |
| 26 | import android.view.animation.Animation; |
| 27 | import android.view.animation.AnimationUtils; |
Yuli Huang | 6a12ad7 | 2011-09-12 22:25:30 +0800 | [diff] [blame] | 28 | import android.widget.RelativeLayout; |
| 29 | |
| 30 | import com.android.gallery3d.R; |
| 31 | |
Yuli Huang | ae70ae4 | 2011-10-01 02:05:06 +0800 | [diff] [blame] | 32 | import java.util.ArrayList; |
| 33 | import java.util.List; |
| 34 | |
Yuli Huang | 6a12ad7 | 2011-09-12 22:25:30 +0800 | [diff] [blame] | 35 | /** |
Yuli Huang | ae70ae4 | 2011-10-01 02:05:06 +0800 | [diff] [blame] | 36 | * Toolbar that contains all tools and controls their idle/awake behaviors from UI thread. |
Yuli Huang | 6a12ad7 | 2011-09-12 22:25:30 +0800 | [diff] [blame] | 37 | */ |
Yuli Huang | 5cac6d5 | 2012-03-12 16:34:41 +0800 | [diff] [blame] | 38 | public class Toolbar extends RelativeLayout { |
Yuli Huang | 6a12ad7 | 2011-09-12 22:25:30 +0800 | [diff] [blame] | 39 | |
| 40 | private final ToolbarIdleHandler idleHandler; |
Yuli Huang | d67ec74 | 2012-03-07 01:18:08 +0800 | [diff] [blame] | 41 | private final List<View> tools = new ArrayList<View>(); |
Yuli Huang | 5cac6d5 | 2012-03-12 16:34:41 +0800 | [diff] [blame] | 42 | private SpinnerProgressDialog spinner; |
Yuli Huang | 6a12ad7 | 2011-09-12 22:25:30 +0800 | [diff] [blame] | 43 | |
| 44 | public Toolbar(Context context, AttributeSet attrs) { |
| 45 | super(context, attrs); |
| 46 | |
Yuli Huang | 5cac6d5 | 2012-03-12 16:34:41 +0800 | [diff] [blame] | 47 | 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 Huang | ae70ae4 | 2011-10-01 02:05:06 +0800 | [diff] [blame] | 64 | idleHandler.killIdle(); |
Yuli Huang | 6a12ad7 | 2011-09-12 22:25:30 +0800 | [diff] [blame] | 65 | } |
| 66 | |
Yuli Huang | 5cac6d5 | 2012-03-12 16:34:41 +0800 | [diff] [blame] | 67 | 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 Huang | d67ec74 | 2012-03-07 01:18:08 +0800 | [diff] [blame] | 81 | } |
| 82 | } |
| 83 | |
Yuli Huang | 5cac6d5 | 2012-03-12 16:34:41 +0800 | [diff] [blame] | 84 | public void dismissSpinner() { |
| 85 | if (spinner != null) { |
| 86 | spinner.dismiss(); |
| 87 | spinner = null; |
| 88 | } |
Yuli Huang | d67ec74 | 2012-03-07 01:18:08 +0800 | [diff] [blame] | 89 | } |
| 90 | |
| 91 | @Override |
Yuli Huang | 6a12ad7 | 2011-09-12 22:25:30 +0800 | [diff] [blame] | 92 | public boolean dispatchTouchEvent(MotionEvent ev) { |
| 93 | idleHandler.killIdle(); |
| 94 | return super.dispatchTouchEvent(ev); |
| 95 | } |
| 96 | |
Yuli Huang | d67ec74 | 2012-03-07 01:18:08 +0800 | [diff] [blame] | 97 | private static class ToolbarIdleHandler { |
Yuli Huang | 6a12ad7 | 2011-09-12 22:25:30 +0800 | [diff] [blame] | 98 | |
Yuli Huang | ae70ae4 | 2011-10-01 02:05:06 +0800 | [diff] [blame] | 99 | private static final int MAKE_IDLE = 1; |
| 100 | private static final int TIMEOUT_IDLE = 8000; |
Yuli Huang | 6a12ad7 | 2011-09-12 22:25:30 +0800 | [diff] [blame] | 101 | |
Yuli Huang | d67ec74 | 2012-03-07 01:18:08 +0800 | [diff] [blame] | 102 | private final List<View> tools; |
Yuli Huang | ae70ae4 | 2011-10-01 02:05:06 +0800 | [diff] [blame] | 103 | private final Handler mainHandler; |
| 104 | private final Animation fadeIn; |
| 105 | private final Animation fadeOut; |
| 106 | private boolean idle; |
Yuli Huang | 6a12ad7 | 2011-09-12 22:25:30 +0800 | [diff] [blame] | 107 | |
Yuli Huang | 5cac6d5 | 2012-03-12 16:34:41 +0800 | [diff] [blame] | 108 | public ToolbarIdleHandler(Context context, final List<View> tools) { |
| 109 | this.tools = tools; |
Yuli Huang | ae70ae4 | 2011-10-01 02:05:06 +0800 | [diff] [blame] | 110 | mainHandler = new Handler() { |
Yuli Huang | 6a12ad7 | 2011-09-12 22:25:30 +0800 | [diff] [blame] | 111 | |
Yuli Huang | ae70ae4 | 2011-10-01 02:05:06 +0800 | [diff] [blame] | 112 | @Override |
| 113 | public void handleMessage(Message msg) { |
| 114 | switch (msg.what) { |
| 115 | case MAKE_IDLE: |
| 116 | if (!idle) { |
| 117 | idle = true; |
Yuli Huang | d67ec74 | 2012-03-07 01:18:08 +0800 | [diff] [blame] | 118 | for (View view : tools) { |
Yuli Huang | ae70ae4 | 2011-10-01 02:05:06 +0800 | [diff] [blame] | 119 | view.startAnimation(fadeOut); |
| 120 | } |
Yuli Huang | 6a12ad7 | 2011-09-12 22:25:30 +0800 | [diff] [blame] | 121 | } |
Yuli Huang | ae70ae4 | 2011-10-01 02:05:06 +0800 | [diff] [blame] | 122 | break; |
Yuli Huang | 6a12ad7 | 2011-09-12 22:25:30 +0800 | [diff] [blame] | 123 | } |
Yuli Huang | ae70ae4 | 2011-10-01 02:05:06 +0800 | [diff] [blame] | 124 | } |
| 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 Huang | d67ec74 | 2012-03-07 01:18:08 +0800 | [diff] [blame] | 135 | for (View view : tools) { |
Yuli Huang | ae70ae4 | 2011-10-01 02:05:06 +0800 | [diff] [blame] | 136 | view.startAnimation(fadeIn); |
| 137 | } |
Yuli Huang | 6a12ad7 | 2011-09-12 22:25:30 +0800 | [diff] [blame] | 138 | } |
Yuli Huang | ae70ae4 | 2011-10-01 02:05:06 +0800 | [diff] [blame] | 139 | mainHandler.sendEmptyMessageDelayed(MAKE_IDLE, TIMEOUT_IDLE); |
| 140 | } |
Yuli Huang | 6a12ad7 | 2011-09-12 22:25:30 +0800 | [diff] [blame] | 141 | } |
| 142 | } |