blob: aad6dbc9fc6f836f21df9d0e366a9d7367c70351 [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
Sunny Goyald1ea63f2015-08-05 12:32:47 -070029import com.android.launcher3.util.Thunk;
30
Winson Chung4c98d922011-05-31 16:50:48 -070031/*
32 * Ths bar will manage the transition between the QSB search bar and the delete drop
33 * targets so that each of the individual IconDropTargets don't have to.
34 */
35public class SearchDropTargetBar extends FrameLayout implements DragController.DragListener {
36
Winson Chung006ee262015-08-03 14:40:11 -070037 /** The different states that the search bar space can be in. */
38 public enum State {
39 INVISIBLE (0f, 0f),
40 SEARCH_BAR (1f, 0f),
41 DROP_TARGET (0f, 1f);
Winson Chung4c98d922011-05-31 16:50:48 -070042
Winson Chung006ee262015-08-03 14:40:11 -070043 private final float mSearchBarAlpha;
44 private final float mDropTargetBarAlpha;
45
46 State(float sbAlpha, float dtbAlpha) {
47 mSearchBarAlpha = sbAlpha;
48 mDropTargetBarAlpha = dtbAlpha;
49 }
50
51 float getSearchBarAlpha() {
52 return mSearchBarAlpha;
53 }
54
55 float getDropTargetBarAlpha() {
56 return mDropTargetBarAlpha;
57 }
58 }
59
60 private static int DEFAULT_DRAG_FADE_DURATION = 175;
61
62 private LauncherViewPropertyAnimator mDropTargetBarAnimator;
63 private LauncherViewPropertyAnimator mQSBSearchBarAnimator;
Winson Chung17f1bb82012-05-25 14:25:44 -070064 private static final AccelerateInterpolator sAccelerateInterpolator =
65 new AccelerateInterpolator();
Winson Chung201bc822011-06-20 15:41:53 -070066
Winson Chung006ee262015-08-03 14:40:11 -070067 private State mState = State.SEARCH_BAR;
Sunny Goyald1ea63f2015-08-05 12:32:47 -070068 @Thunk View mQSB;
69 @Thunk View mDropTargetBar;
Adam Cohend4d7aa52011-07-19 21:47:37 -070070 private boolean mDeferOnDragEnd = false;
Sunny Goyald1ea63f2015-08-05 12:32:47 -070071 @Thunk boolean mAccessibilityEnabled = false;
Winson Chung4c98d922011-05-31 16:50:48 -070072
Sunny Goyalfa401a12015-04-10 13:45:42 -070073 // Drop targets
74 private ButtonDropTarget mInfoDropTarget;
75 private ButtonDropTarget mDeleteDropTarget;
76 private ButtonDropTarget mUninstallDropTarget;
77
Winson Chung4c98d922011-05-31 16:50:48 -070078 public SearchDropTargetBar(Context context, AttributeSet attrs) {
79 this(context, attrs, 0);
80 }
81
82 public SearchDropTargetBar(Context context, AttributeSet attrs, int defStyle) {
83 super(context, attrs, defStyle);
84 }
85
86 public void setup(Launcher launcher, DragController dragController) {
87 dragController.addDragListener(this);
Sunny Goyalfa401a12015-04-10 13:45:42 -070088 dragController.setFlingToDeleteDropTarget(mDeleteDropTarget);
89
Winson Chung4c98d922011-05-31 16:50:48 -070090 dragController.addDragListener(mInfoDropTarget);
91 dragController.addDragListener(mDeleteDropTarget);
Sunny Goyalfa401a12015-04-10 13:45:42 -070092 dragController.addDragListener(mUninstallDropTarget);
93
Winson Chung4c98d922011-05-31 16:50:48 -070094 dragController.addDropTarget(mInfoDropTarget);
95 dragController.addDropTarget(mDeleteDropTarget);
Sunny Goyalfa401a12015-04-10 13:45:42 -070096 dragController.addDropTarget(mUninstallDropTarget);
97
Winson Chung4c98d922011-05-31 16:50:48 -070098 mInfoDropTarget.setLauncher(launcher);
99 mDeleteDropTarget.setLauncher(launcher);
Sunny Goyalfa401a12015-04-10 13:45:42 -0700100 mUninstallDropTarget.setLauncher(launcher);
Sunny Goyal594d76d2014-11-06 10:12:54 -0800101 }
102
Winson Chung4c98d922011-05-31 16:50:48 -0700103 @Override
104 protected void onFinishInflate() {
105 super.onFinishInflate();
106
107 // Get the individual components
Winson Chung4c98d922011-05-31 16:50:48 -0700108 mDropTargetBar = findViewById(R.id.drag_target_bar);
Winson Chunga6427b12011-07-27 10:53:39 -0700109 mInfoDropTarget = (ButtonDropTarget) mDropTargetBar.findViewById(R.id.info_target_text);
110 mDeleteDropTarget = (ButtonDropTarget) mDropTargetBar.findViewById(R.id.delete_target_text);
Sunny Goyalfa401a12015-04-10 13:45:42 -0700111 mUninstallDropTarget = (ButtonDropTarget) mDropTargetBar.findViewById(R.id.uninstall_target_text);
Winson Chunga62e9fd2011-07-11 15:20:48 -0700112
Adam Cohend4d7aa52011-07-19 21:47:37 -0700113 mInfoDropTarget.setSearchDropTargetBar(this);
114 mDeleteDropTarget.setSearchDropTargetBar(this);
Sunny Goyalfa401a12015-04-10 13:45:42 -0700115 mUninstallDropTarget.setSearchDropTargetBar(this);
Adam Cohend4d7aa52011-07-19 21:47:37 -0700116
Winson Chung201bc822011-06-20 15:41:53 -0700117 // Create the various fade animations
Winson Chungdc61c4d2015-04-20 18:26:57 -0700118 mDropTargetBar.setAlpha(0f);
Sunny Goyal67313952016-01-29 18:38:34 -0800119 AlphaUpdateListener.updateVisibility(mDropTargetBar, mAccessibilityEnabled);
120
Winson Chung006ee262015-08-03 14:40:11 -0700121 mDropTargetBarAnimator = new LauncherViewPropertyAnimator(mDropTargetBar);
122 mDropTargetBarAnimator.setInterpolator(sAccelerateInterpolator);
123 mDropTargetBarAnimator.addListener(new AnimatorListenerAdapter() {
124 @Override
125 public void onAnimationStart(Animator animation) {
126 // Ensure that the view is visible for the animation
127 mDropTargetBar.setVisibility(View.VISIBLE);
128 }
129
130 @Override
131 public void onAnimationEnd(Animator animation) {
132 if (mDropTargetBar != null) {
133 AlphaUpdateListener.updateVisibility(mDropTargetBar, mAccessibilityEnabled);
134 }
135 }
136 });
Winson Chung201bc822011-06-20 15:41:53 -0700137 }
138
Winson Chung006ee262015-08-03 14:40:11 -0700139 public void setQsbSearchBar(View qsb) {
140 mQSB = qsb;
141 if (mQSB != null) {
142 // Update the search ber animation
143 mQSBSearchBarAnimator = new LauncherViewPropertyAnimator(mQSB);
144 mQSBSearchBarAnimator.setInterpolator(sAccelerateInterpolator);
145 mQSBSearchBarAnimator.addListener(new AnimatorListenerAdapter() {
146 @Override
147 public void onAnimationStart(Animator animation) {
148 // Ensure that the view is visible for the animation
149 if (mQSB != null) {
150 mQSB.setVisibility(View.VISIBLE);
151 }
152 }
Winson Chung4c98d922011-05-31 16:50:48 -0700153
Winson Chung006ee262015-08-03 14:40:11 -0700154 @Override
155 public void onAnimationEnd(Animator animation) {
156 if (mQSB != null) {
157 AlphaUpdateListener.updateVisibility(mQSB, mAccessibilityEnabled);
158 }
159 }
160 });
Winson Chungf0ea4d32011-06-06 14:27:16 -0700161 } else {
Winson Chung006ee262015-08-03 14:40:11 -0700162 mQSBSearchBarAnimator = null;
163 }
164 }
165
166 /**
167 * Animates the current search bar state to a new state. If the {@param duration} is 0, then
168 * the state is applied immediately.
169 */
170 public void animateToState(State newState, int duration) {
171 if (mState != newState) {
172 mState = newState;
173
174 // Update the accessibility state
175 AccessibilityManager am = (AccessibilityManager)
176 getContext().getSystemService(Context.ACCESSIBILITY_SERVICE);
177 mAccessibilityEnabled = am.isEnabled();
178
179 animateViewAlpha(mQSBSearchBarAnimator, mQSB, newState.getSearchBarAlpha(),
180 duration);
181 animateViewAlpha(mDropTargetBarAnimator, mDropTargetBar, newState.getDropTargetBarAlpha(),
182 duration);
183 }
184 }
185
186 /**
187 * Convenience method to animate the alpha of a view using hardware layers.
188 */
189 private void animateViewAlpha(LauncherViewPropertyAnimator animator, View v, float alpha,
190 int duration) {
Winson81c5f7e2015-08-20 15:22:18 -0700191 if (v == null) {
192 return;
193 }
194
195 animator.cancel();
196 if (Float.compare(v.getAlpha(), alpha) != 0) {
Winson Chung006ee262015-08-03 14:40:11 -0700197 if (duration > 0) {
198 animator.alpha(alpha).withLayer().setDuration(duration).start();
199 } else {
200 v.setAlpha(alpha);
201 AlphaUpdateListener.updateVisibility(v, mAccessibilityEnabled);
Michael Jurka19e33472012-05-14 20:41:52 -0700202 }
Winson Chungf0ea4d32011-06-06 14:27:16 -0700203 }
Winson Chungf0ea4d32011-06-06 14:27:16 -0700204 }
205
206 /*
Winson Chung4c98d922011-05-31 16:50:48 -0700207 * DragController.DragListener implementation
208 */
209 @Override
210 public void onDragStart(DragSource source, Object info, int dragAction) {
Winson Chung006ee262015-08-03 14:40:11 -0700211 animateToState(State.DROP_TARGET, DEFAULT_DRAG_FADE_DURATION);
Adam Cohenc9735cf2015-01-23 16:11:55 -0800212 }
213
Winson Chung006ee262015-08-03 14:40:11 -0700214 /**
215 * This is called to defer hiding the delete drop target until the drop animation has completed,
216 * instead of hiding immediately when the drag has ended.
217 */
Adam Cohend4d7aa52011-07-19 21:47:37 -0700218 public void deferOnDragEnd() {
219 mDeferOnDragEnd = true;
220 }
221
Winson Chung4c98d922011-05-31 16:50:48 -0700222 @Override
223 public void onDragEnd() {
Adam Cohend4d7aa52011-07-19 21:47:37 -0700224 if (!mDeferOnDragEnd) {
Winson Chung006ee262015-08-03 14:40:11 -0700225 animateToState(State.SEARCH_BAR, DEFAULT_DRAG_FADE_DURATION);
Adam Cohend4d7aa52011-07-19 21:47:37 -0700226 } else {
227 mDeferOnDragEnd = false;
Winson Chungf0ea4d32011-06-06 14:27:16 -0700228 }
Winson Chung4c98d922011-05-31 16:50:48 -0700229 }
Winson Chungc51db6a2011-10-05 11:44:49 -0700230
Winson Chung006ee262015-08-03 14:40:11 -0700231 /**
232 * @return the bounds of the QSB search bar.
233 */
Mathew Inwoodcf7f63b2011-10-13 11:31:38 +0100234 public Rect getSearchBarBounds() {
Winson Chung006ee262015-08-03 14:40:11 -0700235 if (mQSB != null) {
Mathew Inwoodcf7f63b2011-10-13 11:31:38 +0100236 final int[] pos = new int[2];
Winson Chung006ee262015-08-03 14:40:11 -0700237 mQSB.getLocationOnScreen(pos);
Mathew Inwoodcf7f63b2011-10-13 11:31:38 +0100238
239 final Rect rect = new Rect();
Michael Jurka629758f2012-06-14 16:18:21 -0700240 rect.left = pos[0];
241 rect.top = pos[1];
Winson Chung006ee262015-08-03 14:40:11 -0700242 rect.right = pos[0] + mQSB.getWidth();
243 rect.bottom = pos[1] + mQSB.getHeight();
Mathew Inwoodcf7f63b2011-10-13 11:31:38 +0100244 return rect;
245 } else {
246 return null;
247 }
248 }
Sunny Goyal1a70cef2015-04-22 11:29:51 -0700249
250 public void enableAccessibleDrag(boolean enable) {
Winson Chung006ee262015-08-03 14:40:11 -0700251 if (mQSB != null) {
252 mQSB.setVisibility(enable ? View.GONE : View.VISIBLE);
Sunny Goyal1a70cef2015-04-22 11:29:51 -0700253 }
254 mInfoDropTarget.enableAccessibleDrag(enable);
255 mDeleteDropTarget.enableAccessibleDrag(enable);
256 mUninstallDropTarget.enableAccessibleDrag(enable);
257 }
Winson Chung4c98d922011-05-31 16:50:48 -0700258}