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.content.DialogInterface; 21 import android.os.Handler; 22 import android.os.Message; 23 import android.util.AttributeSet; 24 import android.view.MotionEvent; 25 import android.view.View; 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 { 39 40 private final ToolbarIdleHandler idleHandler; 41 private final List<View> tools = new ArrayList<View>(); 42 private SpinnerProgressDialog spinner; 43 Toolbar(Context context, AttributeSet attrs)44 public Toolbar(Context context, AttributeSet attrs) { 45 super(context, attrs); 46 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); 64 idleHandler.killIdle(); 65 } 66 showSpinner()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(); 81 } 82 } 83 dismissSpinner()84 public void dismissSpinner() { 85 if (spinner != null) { 86 spinner.dismiss(); 87 spinner = null; 88 } 89 } 90 91 @Override dispatchTouchEvent(MotionEvent ev)92 public boolean dispatchTouchEvent(MotionEvent ev) { 93 idleHandler.killIdle(); 94 return super.dispatchTouchEvent(ev); 95 } 96 97 private static class ToolbarIdleHandler { 98 99 private static final int MAKE_IDLE = 1; 100 private static final int TIMEOUT_IDLE = 8000; 101 102 private final List<View> tools; 103 private final Handler mainHandler; 104 private final Animation fadeIn; 105 private final Animation fadeOut; 106 private boolean idle; 107 ToolbarIdleHandler(Context context, final List<View> tools)108 public ToolbarIdleHandler(Context context, final List<View> tools) { 109 this.tools = tools; 110 mainHandler = new Handler() { 111 112 @Override 113 public void handleMessage(Message msg) { 114 switch (msg.what) { 115 case MAKE_IDLE: 116 if (!idle) { 117 idle = true; 118 for (View view : tools) { 119 view.startAnimation(fadeOut); 120 } 121 } 122 break; 123 } 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 killIdle()131 public void killIdle() { 132 mainHandler.removeMessages(MAKE_IDLE); 133 if (idle) { 134 idle = false; 135 for (View view : tools) { 136 view.startAnimation(fadeIn); 137 } 138 } 139 mainHandler.sendEmptyMessageDelayed(MAKE_IDLE, TIMEOUT_IDLE); 140 } 141 } 142 } 143