blob: f7288a0a06db7adf6bf38260364c122ffbe44ffa [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;
Sunny Goyal0ac7ede2016-01-29 13:14:14 -080021import android.animation.AnimatorSet;
22import android.animation.ObjectAnimator;
23import android.animation.TimeInterpolator;
Winson Chung4c98d922011-05-31 16:50:48 -070024import android.content.Context;
Mathew Inwoodcf7f63b2011-10-13 11:31:38 +010025import android.graphics.Rect;
Winson Chung4c98d922011-05-31 16:50:48 -070026import android.util.AttributeSet;
Sunny Goyal0ac7ede2016-01-29 13:14:14 -080027import android.util.Log;
Winson Chung4c98d922011-05-31 16:50:48 -070028import android.view.View;
Winson Chung006ee262015-08-03 14:40:11 -070029import android.view.accessibility.AccessibilityManager;
Sunny Goyal0ac7ede2016-01-29 13:14:14 -080030import android.view.animation.AccelerateDecelerateInterpolator;
Winson Chunga62e9fd2011-07-11 15:20:48 -070031import android.view.animation.AccelerateInterpolator;
Sunny Goyal0ac7ede2016-01-29 13:14:14 -080032import android.view.animation.DecelerateInterpolator;
Winson Chung4c98d922011-05-31 16:50:48 -070033import android.widget.FrameLayout;
34
Sunny Goyald1ea63f2015-08-05 12:32:47 -070035import com.android.launcher3.util.Thunk;
36
Winson Chung4c98d922011-05-31 16:50:48 -070037/*
38 * Ths bar will manage the transition between the QSB search bar and the delete drop
39 * targets so that each of the individual IconDropTargets don't have to.
40 */
41public class SearchDropTargetBar extends FrameLayout implements DragController.DragListener {
42
Sunny Goyal0ac7ede2016-01-29 13:14:14 -080043 private static final TimeInterpolator MOVE_DOWN_INTERPOLATOR = new DecelerateInterpolator(0.6f);
44 private static final TimeInterpolator MOVE_UP_INTERPOLATOR = new DecelerateInterpolator(1.5f);
45 private static final TimeInterpolator DEFAULT_INTERPOLATOR = new AccelerateInterpolator();
46
Winson Chung006ee262015-08-03 14:40:11 -070047 /** The different states that the search bar space can be in. */
48 public enum State {
Sunny Goyal0ac7ede2016-01-29 13:14:14 -080049 INVISIBLE (0f, 0f, 0f),
50 INVISIBLE_TRANSLATED (0f, 0f, -1f),
51 SEARCH_BAR (1f, 0f, 0f),
52 DROP_TARGET (0f, 1f, 0f);
Winson Chung4c98d922011-05-31 16:50:48 -070053
Winson Chung006ee262015-08-03 14:40:11 -070054 private final float mSearchBarAlpha;
55 private final float mDropTargetBarAlpha;
Sunny Goyal0ac7ede2016-01-29 13:14:14 -080056 private final float mTranslation;
Winson Chung006ee262015-08-03 14:40:11 -070057
Sunny Goyal0ac7ede2016-01-29 13:14:14 -080058 State(float sbAlpha, float dtbAlpha, float translation) {
Winson Chung006ee262015-08-03 14:40:11 -070059 mSearchBarAlpha = sbAlpha;
60 mDropTargetBarAlpha = dtbAlpha;
Sunny Goyal0ac7ede2016-01-29 13:14:14 -080061 mTranslation = translation;
Winson Chung006ee262015-08-03 14:40:11 -070062 }
63
Winson Chung006ee262015-08-03 14:40:11 -070064 }
65
66 private static int DEFAULT_DRAG_FADE_DURATION = 175;
67
Sunny Goyal0ac7ede2016-01-29 13:14:14 -080068 private AnimatorSet mCurrentAnimation;
Winson Chung201bc822011-06-20 15:41:53 -070069
Winson Chung006ee262015-08-03 14:40:11 -070070 private State mState = State.SEARCH_BAR;
Sunny Goyald1ea63f2015-08-05 12:32:47 -070071 @Thunk View mQSB;
72 @Thunk View mDropTargetBar;
Adam Cohend4d7aa52011-07-19 21:47:37 -070073 private boolean mDeferOnDragEnd = false;
Sunny Goyald1ea63f2015-08-05 12:32:47 -070074 @Thunk boolean mAccessibilityEnabled = false;
Winson Chung4c98d922011-05-31 16:50:48 -070075
Sunny Goyalfa401a12015-04-10 13:45:42 -070076 // Drop targets
77 private ButtonDropTarget mInfoDropTarget;
78 private ButtonDropTarget mDeleteDropTarget;
79 private ButtonDropTarget mUninstallDropTarget;
80
Winson Chung4c98d922011-05-31 16:50:48 -070081 public SearchDropTargetBar(Context context, AttributeSet attrs) {
82 this(context, attrs, 0);
83 }
84
85 public SearchDropTargetBar(Context context, AttributeSet attrs, int defStyle) {
86 super(context, attrs, defStyle);
87 }
88
89 public void setup(Launcher launcher, DragController dragController) {
90 dragController.addDragListener(this);
Sunny Goyalfa401a12015-04-10 13:45:42 -070091 dragController.setFlingToDeleteDropTarget(mDeleteDropTarget);
92
Winson Chung4c98d922011-05-31 16:50:48 -070093 dragController.addDragListener(mInfoDropTarget);
94 dragController.addDragListener(mDeleteDropTarget);
Sunny Goyalfa401a12015-04-10 13:45:42 -070095 dragController.addDragListener(mUninstallDropTarget);
96
Winson Chung4c98d922011-05-31 16:50:48 -070097 dragController.addDropTarget(mInfoDropTarget);
98 dragController.addDropTarget(mDeleteDropTarget);
Sunny Goyalfa401a12015-04-10 13:45:42 -070099 dragController.addDropTarget(mUninstallDropTarget);
100
Winson Chung4c98d922011-05-31 16:50:48 -0700101 mInfoDropTarget.setLauncher(launcher);
102 mDeleteDropTarget.setLauncher(launcher);
Sunny Goyalfa401a12015-04-10 13:45:42 -0700103 mUninstallDropTarget.setLauncher(launcher);
Sunny Goyal594d76d2014-11-06 10:12:54 -0800104 }
105
Winson Chung4c98d922011-05-31 16:50:48 -0700106 @Override
107 protected void onFinishInflate() {
108 super.onFinishInflate();
109
110 // Get the individual components
Winson Chung4c98d922011-05-31 16:50:48 -0700111 mDropTargetBar = findViewById(R.id.drag_target_bar);
Winson Chunga6427b12011-07-27 10:53:39 -0700112 mInfoDropTarget = (ButtonDropTarget) mDropTargetBar.findViewById(R.id.info_target_text);
113 mDeleteDropTarget = (ButtonDropTarget) mDropTargetBar.findViewById(R.id.delete_target_text);
Sunny Goyalfa401a12015-04-10 13:45:42 -0700114 mUninstallDropTarget = (ButtonDropTarget) mDropTargetBar.findViewById(R.id.uninstall_target_text);
Winson Chunga62e9fd2011-07-11 15:20:48 -0700115
Adam Cohend4d7aa52011-07-19 21:47:37 -0700116 mInfoDropTarget.setSearchDropTargetBar(this);
117 mDeleteDropTarget.setSearchDropTargetBar(this);
Sunny Goyalfa401a12015-04-10 13:45:42 -0700118 mUninstallDropTarget.setSearchDropTargetBar(this);
Adam Cohend4d7aa52011-07-19 21:47:37 -0700119
Winson Chung201bc822011-06-20 15:41:53 -0700120 // Create the various fade animations
Winson Chungdc61c4d2015-04-20 18:26:57 -0700121 mDropTargetBar.setAlpha(0f);
Sunny Goyal67313952016-01-29 18:38:34 -0800122 AlphaUpdateListener.updateVisibility(mDropTargetBar, mAccessibilityEnabled);
Winson Chung201bc822011-06-20 15:41:53 -0700123 }
124
Winson Chung006ee262015-08-03 14:40:11 -0700125 public void setQsbSearchBar(View qsb) {
126 mQSB = qsb;
Winson Chung006ee262015-08-03 14:40:11 -0700127 }
128
129 /**
130 * Animates the current search bar state to a new state. If the {@param duration} is 0, then
131 * the state is applied immediately.
132 */
133 public void animateToState(State newState, int duration) {
Sunny Goyal0ac7ede2016-01-29 13:14:14 -0800134 animateToState(newState, duration, null);
135 }
136
137 public void animateToState(State newState, int duration, AnimatorSet animation) {
Winson Chung006ee262015-08-03 14:40:11 -0700138 if (mState != newState) {
139 mState = newState;
140
141 // Update the accessibility state
142 AccessibilityManager am = (AccessibilityManager)
143 getContext().getSystemService(Context.ACCESSIBILITY_SERVICE);
144 mAccessibilityEnabled = am.isEnabled();
145
Sunny Goyal0ac7ede2016-01-29 13:14:14 -0800146 if (mCurrentAnimation != null) {
147 mCurrentAnimation.cancel();
148 mCurrentAnimation = null;
149 }
150 mCurrentAnimation = null;
151
152 if (duration > 0) {
153 mCurrentAnimation = new AnimatorSet();
154 mCurrentAnimation.setDuration(duration);
155
156 animateAlpha(mDropTargetBar, mState.mDropTargetBarAlpha, DEFAULT_INTERPOLATOR);
157 } else {
158 mDropTargetBar.setAlpha(mState.mDropTargetBarAlpha);
159 AlphaUpdateListener.updateVisibility(mDropTargetBar, mAccessibilityEnabled);
160 }
161
162 if (mQSB != null) {
163 boolean isVertical = ((Launcher) getContext()).getDeviceProfile()
164 .isVerticalBarLayout();
165 float translation = isVertical ? 0 : mState.mTranslation * getMeasuredHeight();
166
167 if (duration > 0) {
168 int translationChange = Float.compare(mQSB.getTranslationY(), translation);
169
170 animateAlpha(mQSB, mState.mSearchBarAlpha,
171 translationChange == 0 ? DEFAULT_INTERPOLATOR
172 : (translationChange < 0 ? MOVE_DOWN_INTERPOLATOR
173 : MOVE_UP_INTERPOLATOR));
174
175 if (translationChange != 0) {
176 mCurrentAnimation.play(
177 ObjectAnimator.ofFloat(mQSB, View.TRANSLATION_Y, translation));
178 }
179 } else {
180 mQSB.setTranslationY(translation);
181 mQSB.setAlpha(mState.mSearchBarAlpha);
182 AlphaUpdateListener.updateVisibility(mQSB, mAccessibilityEnabled);
183 }
184 }
185
186 // Start the final animation
187 if (duration > 0) {
188 if (animation != null) {
189 animation.play(mCurrentAnimation);
190 } else {
191 mCurrentAnimation.start();
192 }
193 }
Winson Chung006ee262015-08-03 14:40:11 -0700194 }
195 }
196
Sunny Goyal0ac7ede2016-01-29 13:14:14 -0800197 private void animateAlpha(View v, float alpha, TimeInterpolator interpolator) {
Winson81c5f7e2015-08-20 15:22:18 -0700198 if (Float.compare(v.getAlpha(), alpha) != 0) {
Sunny Goyal0ac7ede2016-01-29 13:14:14 -0800199 ObjectAnimator anim = ObjectAnimator.ofFloat(v, View.ALPHA, alpha);
200 anim.setInterpolator(interpolator);
201 anim.addListener(new ViewVisiblilyUpdateHandler(v));
202 mCurrentAnimation.play(anim);
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 }
Sunny Goyal0ac7ede2016-01-29 13:14:14 -0800258
259 private class ViewVisiblilyUpdateHandler extends AnimatorListenerAdapter {
260 private final View mView;
261
262 ViewVisiblilyUpdateHandler(View v) {
263 mView = v;
264 }
265
266 @Override
267 public void onAnimationStart(Animator animation) {
268 // Ensure that the view is visible for the animation
269 mView.setVisibility(View.VISIBLE);
270 }
271
272 @Override
273 public void onAnimationEnd(Animator animation){
274 AlphaUpdateListener.updateVisibility(mView, mAccessibilityEnabled);
275 }
276
277 }
Winson Chung4c98d922011-05-31 16:50:48 -0700278}