blob: 171dd87714438c86ec81eb9124355349a11636e1 [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;
27import android.view.View;
Sunny Goyal4ffec482016-02-09 11:28:52 -080028import android.view.ViewDebug;
Winson Chung006ee262015-08-03 14:40:11 -070029import android.view.accessibility.AccessibilityManager;
Winson Chunga62e9fd2011-07-11 15:20:48 -070030import android.view.animation.AccelerateInterpolator;
Sunny Goyal0ac7ede2016-01-29 13:14:14 -080031import android.view.animation.DecelerateInterpolator;
Winson Chung4c98d922011-05-31 16:50:48 -070032
Vadim Tryshevfedca432015-08-19 17:55:02 -070033import com.android.launcher3.dragndrop.DragController;
Sunny Goyald1ea63f2015-08-05 12:32:47 -070034import com.android.launcher3.util.Thunk;
35
Winson Chung4c98d922011-05-31 16:50:48 -070036/*
Tony Wickhamb54c4a32015-09-11 08:40:20 -070037 * This bar will manage the transition between the QSB search bar and the delete/uninstall drop
38 * targets so that each of the individual ButtonDropTargets don't have to.
Winson Chung4c98d922011-05-31 16:50:48 -070039 */
Tony Wickhamb54c4a32015-09-11 08:40:20 -070040public class SearchDropTargetBar extends BaseDropTargetBar {
Winson Chung4c98d922011-05-31 16:50:48 -070041
Sunny Goyal0ac7ede2016-01-29 13:14:14 -080042 private static final TimeInterpolator MOVE_DOWN_INTERPOLATOR = new DecelerateInterpolator(0.6f);
43 private static final TimeInterpolator MOVE_UP_INTERPOLATOR = new DecelerateInterpolator(1.5f);
Sunny Goyal0ac7ede2016-01-29 13:14:14 -080044
Winson Chung006ee262015-08-03 14:40:11 -070045 /** The different states that the search bar space can be in. */
46 public enum State {
Sunny Goyal0ac7ede2016-01-29 13:14:14 -080047 INVISIBLE (0f, 0f, 0f),
48 INVISIBLE_TRANSLATED (0f, 0f, -1f),
49 SEARCH_BAR (1f, 0f, 0f),
50 DROP_TARGET (0f, 1f, 0f);
Winson Chung4c98d922011-05-31 16:50:48 -070051
Winson Chung006ee262015-08-03 14:40:11 -070052 private final float mSearchBarAlpha;
53 private final float mDropTargetBarAlpha;
Sunny Goyal0ac7ede2016-01-29 13:14:14 -080054 private final float mTranslation;
Winson Chung006ee262015-08-03 14:40:11 -070055
Sunny Goyal0ac7ede2016-01-29 13:14:14 -080056 State(float sbAlpha, float dtbAlpha, float translation) {
Winson Chung006ee262015-08-03 14:40:11 -070057 mSearchBarAlpha = sbAlpha;
58 mDropTargetBarAlpha = dtbAlpha;
Sunny Goyal0ac7ede2016-01-29 13:14:14 -080059 mTranslation = translation;
Winson Chung006ee262015-08-03 14:40:11 -070060 }
61
Winson Chung006ee262015-08-03 14:40:11 -070062 }
63
Winson Chung006ee262015-08-03 14:40:11 -070064
Sunny Goyal4ffec482016-02-09 11:28:52 -080065 @ViewDebug.ExportedProperty(category = "launcher")
Winson Chung006ee262015-08-03 14:40:11 -070066 private State mState = State.SEARCH_BAR;
Sunny Goyald1ea63f2015-08-05 12:32:47 -070067 @Thunk View mQSB;
Winson Chung4c98d922011-05-31 16:50:48 -070068
Sunny Goyalfa401a12015-04-10 13:45:42 -070069 // Drop targets
Sunny Goyalfa401a12015-04-10 13:45:42 -070070 private ButtonDropTarget mDeleteDropTarget;
71 private ButtonDropTarget mUninstallDropTarget;
72
Winson Chung4c98d922011-05-31 16:50:48 -070073 public SearchDropTargetBar(Context context, AttributeSet attrs) {
74 this(context, attrs, 0);
75 }
76
77 public SearchDropTargetBar(Context context, AttributeSet attrs, int defStyle) {
78 super(context, attrs, defStyle);
79 }
80
Winson Chung4c98d922011-05-31 16:50:48 -070081 @Override
82 protected void onFinishInflate() {
83 super.onFinishInflate();
84
85 // Get the individual components
Winson Chunga6427b12011-07-27 10:53:39 -070086 mDeleteDropTarget = (ButtonDropTarget) mDropTargetBar.findViewById(R.id.delete_target_text);
Tony Wickhamb54c4a32015-09-11 08:40:20 -070087 mUninstallDropTarget = (ButtonDropTarget) mDropTargetBar
88 .findViewById(R.id.uninstall_target_text);
Winson Chunga62e9fd2011-07-11 15:20:48 -070089
Tony Wickhamb54c4a32015-09-11 08:40:20 -070090 mDeleteDropTarget.setDropTargetBar(this);
91 mUninstallDropTarget.setDropTargetBar(this);
92 }
Adam Cohend4d7aa52011-07-19 21:47:37 -070093
Tony Wickhamb54c4a32015-09-11 08:40:20 -070094 @Override
95 public void setup(Launcher launcher, DragController dragController) {
96 dragController.addDragListener(this);
97 dragController.setFlingToDeleteDropTarget(mDeleteDropTarget);
Winson Chung006ee262015-08-03 14:40:11 -070098
Tony Wickhamb54c4a32015-09-11 08:40:20 -070099 dragController.addDragListener(mDeleteDropTarget);
100 dragController.addDragListener(mUninstallDropTarget);
101
102 dragController.addDropTarget(mDeleteDropTarget);
103 dragController.addDropTarget(mUninstallDropTarget);
104
105 mDeleteDropTarget.setLauncher(launcher);
106 mUninstallDropTarget.setLauncher(launcher);
107 }
108
109 @Override
Tony Wickham9aae47f2015-10-01 13:04:22 -0700110 public void showDropTargets() {
Tony Wickhamb54c4a32015-09-11 08:40:20 -0700111 animateToState(State.DROP_TARGET, DEFAULT_DRAG_FADE_DURATION);
112 }
113
114 @Override
Tony Wickham9aae47f2015-10-01 13:04:22 -0700115 public void hideDropTargets() {
Tony Wickhamb54c4a32015-09-11 08:40:20 -0700116 animateToState(State.SEARCH_BAR, DEFAULT_DRAG_FADE_DURATION);
Winson Chung201bc822011-06-20 15:41:53 -0700117 }
118
Winson Chung006ee262015-08-03 14:40:11 -0700119 public void setQsbSearchBar(View qsb) {
120 mQSB = qsb;
Winson Chung006ee262015-08-03 14:40:11 -0700121 }
122
123 /**
124 * Animates the current search bar state to a new state. If the {@param duration} is 0, then
125 * the state is applied immediately.
126 */
127 public void animateToState(State newState, int duration) {
Sunny Goyal0ac7ede2016-01-29 13:14:14 -0800128 animateToState(newState, duration, null);
129 }
130
131 public void animateToState(State newState, int duration, AnimatorSet animation) {
Winson Chung006ee262015-08-03 14:40:11 -0700132 if (mState != newState) {
133 mState = newState;
134
Sunny Goyal7c50b312016-02-10 12:26:23 -0800135 resetAnimation(duration);
Sunny Goyal0ac7ede2016-01-29 13:14:14 -0800136 if (duration > 0) {
Sunny Goyal0ac7ede2016-01-29 13:14:14 -0800137 animateAlpha(mDropTargetBar, mState.mDropTargetBarAlpha, DEFAULT_INTERPOLATOR);
138 } else {
139 mDropTargetBar.setAlpha(mState.mDropTargetBarAlpha);
140 AlphaUpdateListener.updateVisibility(mDropTargetBar, mAccessibilityEnabled);
141 }
Winson Chung006ee262015-08-03 14:40:11 -0700142
Sunny Goyal0ac7ede2016-01-29 13:14:14 -0800143 if (mQSB != null) {
144 boolean isVertical = ((Launcher) getContext()).getDeviceProfile()
145 .isVerticalBarLayout();
146 float translation = isVertical ? 0 : mState.mTranslation * getMeasuredHeight();
147
148 if (duration > 0) {
149 int translationChange = Float.compare(mQSB.getTranslationY(), translation);
150
151 animateAlpha(mQSB, mState.mSearchBarAlpha,
152 translationChange == 0 ? DEFAULT_INTERPOLATOR
153 : (translationChange < 0 ? MOVE_DOWN_INTERPOLATOR
154 : MOVE_UP_INTERPOLATOR));
155
156 if (translationChange != 0) {
157 mCurrentAnimation.play(
158 ObjectAnimator.ofFloat(mQSB, View.TRANSLATION_Y, translation));
159 }
160 } else {
161 mQSB.setTranslationY(translation);
162 mQSB.setAlpha(mState.mSearchBarAlpha);
163 AlphaUpdateListener.updateVisibility(mQSB, mAccessibilityEnabled);
164 }
165 }
166
167 // Start the final animation
168 if (duration > 0) {
169 if (animation != null) {
170 animation.play(mCurrentAnimation);
171 } else {
172 mCurrentAnimation.start();
173 }
174 }
Winson Chung006ee262015-08-03 14:40:11 -0700175 }
176 }
177
178 /**
Winson Chung006ee262015-08-03 14:40:11 -0700179 * @return the bounds of the QSB search bar.
180 */
Mathew Inwoodcf7f63b2011-10-13 11:31:38 +0100181 public Rect getSearchBarBounds() {
Winson Chung006ee262015-08-03 14:40:11 -0700182 if (mQSB != null) {
Mathew Inwoodcf7f63b2011-10-13 11:31:38 +0100183 final int[] pos = new int[2];
Winson Chung006ee262015-08-03 14:40:11 -0700184 mQSB.getLocationOnScreen(pos);
Mathew Inwoodcf7f63b2011-10-13 11:31:38 +0100185
186 final Rect rect = new Rect();
Michael Jurka629758f2012-06-14 16:18:21 -0700187 rect.left = pos[0];
188 rect.top = pos[1];
Winson Chung006ee262015-08-03 14:40:11 -0700189 rect.right = pos[0] + mQSB.getWidth();
190 rect.bottom = pos[1] + mQSB.getHeight();
Mathew Inwoodcf7f63b2011-10-13 11:31:38 +0100191 return rect;
192 } else {
193 return null;
194 }
195 }
Sunny Goyal1a70cef2015-04-22 11:29:51 -0700196
Tony Wickhamb54c4a32015-09-11 08:40:20 -0700197 @Override
Sunny Goyal1a70cef2015-04-22 11:29:51 -0700198 public void enableAccessibleDrag(boolean enable) {
Winson Chung006ee262015-08-03 14:40:11 -0700199 if (mQSB != null) {
200 mQSB.setVisibility(enable ? View.GONE : View.VISIBLE);
Sunny Goyal1a70cef2015-04-22 11:29:51 -0700201 }
Sunny Goyal1a70cef2015-04-22 11:29:51 -0700202 mDeleteDropTarget.enableAccessibleDrag(enable);
203 mUninstallDropTarget.enableAccessibleDrag(enable);
204 }
Winson Chung4c98d922011-05-31 16:50:48 -0700205}