blob: fe89c15a73b7609368f77403003a31f6eef1f78b [file] [log] [blame]
Winson Chung4c98d922011-05-31 16:50:48 -07001/*
2 * Copyright (C) 2011 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
Daniel Sandler325dc232013-06-05 22:57:57 -040017package com.android.launcher3;
Winson Chung4c98d922011-05-31 16:50:48 -070018
Winson Chung201bc822011-06-20 15:41:53 -070019import android.animation.Animator;
20import android.animation.AnimatorListenerAdapter;
Winson Chung4c98d922011-05-31 16:50:48 -070021import android.content.Context;
Mathew Inwoodcf7f63b2011-10-13 11:31:38 +010022import android.graphics.Rect;
Winson Chung4c98d922011-05-31 16:50:48 -070023import android.util.AttributeSet;
24import android.view.View;
Winson Chung006ee262015-08-03 14:40:11 -070025import android.view.accessibility.AccessibilityManager;
Winson Chunga62e9fd2011-07-11 15:20:48 -070026import android.view.animation.AccelerateInterpolator;
Winson Chung4c98d922011-05-31 16:50:48 -070027import android.widget.FrameLayout;
28
Winson Chung4c98d922011-05-31 16:50:48 -070029/*
30 * Ths bar will manage the transition between the QSB search bar and the delete drop
31 * targets so that each of the individual IconDropTargets don't have to.
32 */
33public class SearchDropTargetBar extends FrameLayout implements DragController.DragListener {
34
Winson Chung006ee262015-08-03 14:40:11 -070035 /** The different states that the search bar space can be in. */
36 public enum State {
37 INVISIBLE (0f, 0f),
38 SEARCH_BAR (1f, 0f),
39 DROP_TARGET (0f, 1f);
Winson Chung4c98d922011-05-31 16:50:48 -070040
Winson Chung006ee262015-08-03 14:40:11 -070041 private final float mSearchBarAlpha;
42 private final float mDropTargetBarAlpha;
43
44 State(float sbAlpha, float dtbAlpha) {
45 mSearchBarAlpha = sbAlpha;
46 mDropTargetBarAlpha = dtbAlpha;
47 }
48
49 float getSearchBarAlpha() {
50 return mSearchBarAlpha;
51 }
52
53 float getDropTargetBarAlpha() {
54 return mDropTargetBarAlpha;
55 }
56 }
57
58 private static int DEFAULT_DRAG_FADE_DURATION = 175;
59
60 private LauncherViewPropertyAnimator mDropTargetBarAnimator;
61 private LauncherViewPropertyAnimator mQSBSearchBarAnimator;
Winson Chung17f1bb82012-05-25 14:25:44 -070062 private static final AccelerateInterpolator sAccelerateInterpolator =
63 new AccelerateInterpolator();
Winson Chung201bc822011-06-20 15:41:53 -070064
Winson Chung006ee262015-08-03 14:40:11 -070065 private State mState = State.SEARCH_BAR;
66 private View mQSB;
Winson Chung4c98d922011-05-31 16:50:48 -070067 private View mDropTargetBar;
Adam Cohend4d7aa52011-07-19 21:47:37 -070068 private boolean mDeferOnDragEnd = false;
Winson Chung006ee262015-08-03 14:40:11 -070069 private boolean mAccessibilityEnabled = false;
Winson Chung4c98d922011-05-31 16:50:48 -070070
Sunny Goyalfa401a12015-04-10 13:45:42 -070071 // Drop targets
72 private ButtonDropTarget mInfoDropTarget;
73 private ButtonDropTarget mDeleteDropTarget;
74 private ButtonDropTarget mUninstallDropTarget;
75
Winson Chung4c98d922011-05-31 16:50:48 -070076 public SearchDropTargetBar(Context context, AttributeSet attrs) {
77 this(context, attrs, 0);
78 }
79
80 public SearchDropTargetBar(Context context, AttributeSet attrs, int defStyle) {
81 super(context, attrs, defStyle);
82 }
83
84 public void setup(Launcher launcher, DragController dragController) {
85 dragController.addDragListener(this);
Sunny Goyalfa401a12015-04-10 13:45:42 -070086 dragController.setFlingToDeleteDropTarget(mDeleteDropTarget);
87
Winson Chung4c98d922011-05-31 16:50:48 -070088 dragController.addDragListener(mInfoDropTarget);
89 dragController.addDragListener(mDeleteDropTarget);
Sunny Goyalfa401a12015-04-10 13:45:42 -070090 dragController.addDragListener(mUninstallDropTarget);
91
Winson Chung4c98d922011-05-31 16:50:48 -070092 dragController.addDropTarget(mInfoDropTarget);
93 dragController.addDropTarget(mDeleteDropTarget);
Sunny Goyalfa401a12015-04-10 13:45:42 -070094 dragController.addDropTarget(mUninstallDropTarget);
95
Winson Chung4c98d922011-05-31 16:50:48 -070096 mInfoDropTarget.setLauncher(launcher);
97 mDeleteDropTarget.setLauncher(launcher);
Sunny Goyalfa401a12015-04-10 13:45:42 -070098 mUninstallDropTarget.setLauncher(launcher);
Sunny Goyal594d76d2014-11-06 10:12:54 -080099 }
100
Winson Chung4c98d922011-05-31 16:50:48 -0700101 @Override
102 protected void onFinishInflate() {
103 super.onFinishInflate();
104
105 // Get the individual components
Winson Chung4c98d922011-05-31 16:50:48 -0700106 mDropTargetBar = findViewById(R.id.drag_target_bar);
Winson Chunga6427b12011-07-27 10:53:39 -0700107 mInfoDropTarget = (ButtonDropTarget) mDropTargetBar.findViewById(R.id.info_target_text);
108 mDeleteDropTarget = (ButtonDropTarget) mDropTargetBar.findViewById(R.id.delete_target_text);
Sunny Goyalfa401a12015-04-10 13:45:42 -0700109 mUninstallDropTarget = (ButtonDropTarget) mDropTargetBar.findViewById(R.id.uninstall_target_text);
Winson Chunga62e9fd2011-07-11 15:20:48 -0700110
Adam Cohend4d7aa52011-07-19 21:47:37 -0700111 mInfoDropTarget.setSearchDropTargetBar(this);
112 mDeleteDropTarget.setSearchDropTargetBar(this);
Sunny Goyalfa401a12015-04-10 13:45:42 -0700113 mUninstallDropTarget.setSearchDropTargetBar(this);
Adam Cohend4d7aa52011-07-19 21:47:37 -0700114
Winson Chung201bc822011-06-20 15:41:53 -0700115 // Create the various fade animations
Winson Chungdc61c4d2015-04-20 18:26:57 -0700116 mDropTargetBar.setAlpha(0f);
Winson Chung006ee262015-08-03 14:40:11 -0700117 mDropTargetBarAnimator = new LauncherViewPropertyAnimator(mDropTargetBar);
118 mDropTargetBarAnimator.setInterpolator(sAccelerateInterpolator);
119 mDropTargetBarAnimator.addListener(new AnimatorListenerAdapter() {
120 @Override
121 public void onAnimationStart(Animator animation) {
122 // Ensure that the view is visible for the animation
123 mDropTargetBar.setVisibility(View.VISIBLE);
124 }
125
126 @Override
127 public void onAnimationEnd(Animator animation) {
128 if (mDropTargetBar != null) {
129 AlphaUpdateListener.updateVisibility(mDropTargetBar, mAccessibilityEnabled);
130 }
131 }
132 });
Winson Chung201bc822011-06-20 15:41:53 -0700133 }
134
Winson Chung006ee262015-08-03 14:40:11 -0700135 public void setQsbSearchBar(View qsb) {
136 mQSB = qsb;
137 if (mQSB != null) {
138 // Update the search ber animation
139 mQSBSearchBarAnimator = new LauncherViewPropertyAnimator(mQSB);
140 mQSBSearchBarAnimator.setInterpolator(sAccelerateInterpolator);
141 mQSBSearchBarAnimator.addListener(new AnimatorListenerAdapter() {
142 @Override
143 public void onAnimationStart(Animator animation) {
144 // Ensure that the view is visible for the animation
145 if (mQSB != null) {
146 mQSB.setVisibility(View.VISIBLE);
147 }
148 }
Winson Chung4c98d922011-05-31 16:50:48 -0700149
Winson Chung006ee262015-08-03 14:40:11 -0700150 @Override
151 public void onAnimationEnd(Animator animation) {
152 if (mQSB != null) {
153 AlphaUpdateListener.updateVisibility(mQSB, mAccessibilityEnabled);
154 }
155 }
156 });
Winson Chungf0ea4d32011-06-06 14:27:16 -0700157 } else {
Winson Chung006ee262015-08-03 14:40:11 -0700158 mQSBSearchBarAnimator = null;
159 }
160 }
161
162 /**
163 * Animates the current search bar state to a new state. If the {@param duration} is 0, then
164 * the state is applied immediately.
165 */
166 public void animateToState(State newState, int duration) {
167 if (mState != newState) {
168 mState = newState;
169
170 // Update the accessibility state
171 AccessibilityManager am = (AccessibilityManager)
172 getContext().getSystemService(Context.ACCESSIBILITY_SERVICE);
173 mAccessibilityEnabled = am.isEnabled();
174
175 animateViewAlpha(mQSBSearchBarAnimator, mQSB, newState.getSearchBarAlpha(),
176 duration);
177 animateViewAlpha(mDropTargetBarAnimator, mDropTargetBar, newState.getDropTargetBarAlpha(),
178 duration);
179 }
180 }
181
182 /**
183 * Convenience method to animate the alpha of a view using hardware layers.
184 */
185 private void animateViewAlpha(LauncherViewPropertyAnimator animator, View v, float alpha,
186 int duration) {
187 if (v != null && Float.compare(v.getAlpha(), alpha) != 0) {
188 if (duration > 0) {
189 animator.alpha(alpha).withLayer().setDuration(duration).start();
190 } else {
191 v.setAlpha(alpha);
192 AlphaUpdateListener.updateVisibility(v, mAccessibilityEnabled);
Michael Jurka19e33472012-05-14 20:41:52 -0700193 }
Winson Chungf0ea4d32011-06-06 14:27:16 -0700194 }
Winson Chungf0ea4d32011-06-06 14:27:16 -0700195 }
196
197 /*
Winson Chung4c98d922011-05-31 16:50:48 -0700198 * DragController.DragListener implementation
199 */
200 @Override
201 public void onDragStart(DragSource source, Object info, int dragAction) {
Winson Chung006ee262015-08-03 14:40:11 -0700202 animateToState(State.DROP_TARGET, DEFAULT_DRAG_FADE_DURATION);
Adam Cohenc9735cf2015-01-23 16:11:55 -0800203 }
204
Winson Chung006ee262015-08-03 14:40:11 -0700205 /**
206 * This is called to defer hiding the delete drop target until the drop animation has completed,
207 * instead of hiding immediately when the drag has ended.
208 */
Adam Cohend4d7aa52011-07-19 21:47:37 -0700209 public void deferOnDragEnd() {
210 mDeferOnDragEnd = true;
211 }
212
Winson Chung4c98d922011-05-31 16:50:48 -0700213 @Override
214 public void onDragEnd() {
Adam Cohend4d7aa52011-07-19 21:47:37 -0700215 if (!mDeferOnDragEnd) {
Winson Chung006ee262015-08-03 14:40:11 -0700216 animateToState(State.SEARCH_BAR, DEFAULT_DRAG_FADE_DURATION);
Adam Cohend4d7aa52011-07-19 21:47:37 -0700217 } else {
218 mDeferOnDragEnd = false;
Winson Chungf0ea4d32011-06-06 14:27:16 -0700219 }
Winson Chung4c98d922011-05-31 16:50:48 -0700220 }
Winson Chungc51db6a2011-10-05 11:44:49 -0700221
Winson Chung006ee262015-08-03 14:40:11 -0700222 /**
223 * @return the bounds of the QSB search bar.
224 */
Mathew Inwoodcf7f63b2011-10-13 11:31:38 +0100225 public Rect getSearchBarBounds() {
Winson Chung006ee262015-08-03 14:40:11 -0700226 if (mQSB != null) {
Mathew Inwoodcf7f63b2011-10-13 11:31:38 +0100227 final int[] pos = new int[2];
Winson Chung006ee262015-08-03 14:40:11 -0700228 mQSB.getLocationOnScreen(pos);
Mathew Inwoodcf7f63b2011-10-13 11:31:38 +0100229
230 final Rect rect = new Rect();
Michael Jurka629758f2012-06-14 16:18:21 -0700231 rect.left = pos[0];
232 rect.top = pos[1];
Winson Chung006ee262015-08-03 14:40:11 -0700233 rect.right = pos[0] + mQSB.getWidth();
234 rect.bottom = pos[1] + mQSB.getHeight();
Mathew Inwoodcf7f63b2011-10-13 11:31:38 +0100235 return rect;
236 } else {
237 return null;
238 }
239 }
Sunny Goyal1a70cef2015-04-22 11:29:51 -0700240
241 public void enableAccessibleDrag(boolean enable) {
Winson Chung006ee262015-08-03 14:40:11 -0700242 if (mQSB != null) {
243 mQSB.setVisibility(enable ? View.GONE : View.VISIBLE);
Sunny Goyal1a70cef2015-04-22 11:29:51 -0700244 }
245 mInfoDropTarget.enableAccessibleDrag(enable);
246 mDeleteDropTarget.enableAccessibleDrag(enable);
247 mUninstallDropTarget.enableAccessibleDrag(enable);
248 }
Winson Chung4c98d922011-05-31 16:50:48 -0700249}