Winson Chung | 4c98d92 | 2011-05-31 16:50:48 -0700 | [diff] [blame] | 1 | /* |
| 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 Sandler | 325dc23 | 2013-06-05 22:57:57 -0400 | [diff] [blame] | 17 | package com.android.launcher3; |
Winson Chung | 4c98d92 | 2011-05-31 16:50:48 -0700 | [diff] [blame] | 18 | |
Winson Chung | 201bc82 | 2011-06-20 15:41:53 -0700 | [diff] [blame] | 19 | import android.animation.Animator; |
| 20 | import android.animation.AnimatorListenerAdapter; |
Sunny Goyal | 0ac7ede | 2016-01-29 13:14:14 -0800 | [diff] [blame] | 21 | import android.animation.AnimatorSet; |
| 22 | import android.animation.ObjectAnimator; |
| 23 | import android.animation.TimeInterpolator; |
Winson Chung | 4c98d92 | 2011-05-31 16:50:48 -0700 | [diff] [blame] | 24 | import android.content.Context; |
Mathew Inwood | cf7f63b | 2011-10-13 11:31:38 +0100 | [diff] [blame] | 25 | import android.graphics.Rect; |
Winson Chung | 4c98d92 | 2011-05-31 16:50:48 -0700 | [diff] [blame] | 26 | import android.util.AttributeSet; |
| 27 | import android.view.View; |
Sunny Goyal | 4ffec48 | 2016-02-09 11:28:52 -0800 | [diff] [blame] | 28 | import android.view.ViewDebug; |
Winson Chung | 006ee26 | 2015-08-03 14:40:11 -0700 | [diff] [blame] | 29 | import android.view.accessibility.AccessibilityManager; |
Winson Chung | a62e9fd | 2011-07-11 15:20:48 -0700 | [diff] [blame] | 30 | import android.view.animation.AccelerateInterpolator; |
Sunny Goyal | 0ac7ede | 2016-01-29 13:14:14 -0800 | [diff] [blame] | 31 | import android.view.animation.DecelerateInterpolator; |
Winson Chung | 4c98d92 | 2011-05-31 16:50:48 -0700 | [diff] [blame] | 32 | |
Vadim Tryshev | fedca43 | 2015-08-19 17:55:02 -0700 | [diff] [blame] | 33 | import com.android.launcher3.dragndrop.DragController; |
Sunny Goyal | d1ea63f | 2015-08-05 12:32:47 -0700 | [diff] [blame] | 34 | import com.android.launcher3.util.Thunk; |
| 35 | |
Winson Chung | 4c98d92 | 2011-05-31 16:50:48 -0700 | [diff] [blame] | 36 | /* |
Tony Wickham | b54c4a3 | 2015-09-11 08:40:20 -0700 | [diff] [blame] | 37 | * 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 Chung | 4c98d92 | 2011-05-31 16:50:48 -0700 | [diff] [blame] | 39 | */ |
Tony Wickham | b54c4a3 | 2015-09-11 08:40:20 -0700 | [diff] [blame] | 40 | public class SearchDropTargetBar extends BaseDropTargetBar { |
Winson Chung | 4c98d92 | 2011-05-31 16:50:48 -0700 | [diff] [blame] | 41 | |
Sunny Goyal | 0ac7ede | 2016-01-29 13:14:14 -0800 | [diff] [blame] | 42 | private static final TimeInterpolator MOVE_DOWN_INTERPOLATOR = new DecelerateInterpolator(0.6f); |
| 43 | private static final TimeInterpolator MOVE_UP_INTERPOLATOR = new DecelerateInterpolator(1.5f); |
Sunny Goyal | 0ac7ede | 2016-01-29 13:14:14 -0800 | [diff] [blame] | 44 | |
Winson Chung | 006ee26 | 2015-08-03 14:40:11 -0700 | [diff] [blame] | 45 | /** The different states that the search bar space can be in. */ |
| 46 | public enum State { |
Sunny Goyal | 0ac7ede | 2016-01-29 13:14:14 -0800 | [diff] [blame] | 47 | INVISIBLE (0f, 0f, 0f), |
| 48 | INVISIBLE_TRANSLATED (0f, 0f, -1f), |
| 49 | SEARCH_BAR (1f, 0f, 0f), |
| 50 | DROP_TARGET (0f, 1f, 0f); |
Winson Chung | 4c98d92 | 2011-05-31 16:50:48 -0700 | [diff] [blame] | 51 | |
Winson Chung | 006ee26 | 2015-08-03 14:40:11 -0700 | [diff] [blame] | 52 | private final float mSearchBarAlpha; |
| 53 | private final float mDropTargetBarAlpha; |
Sunny Goyal | 0ac7ede | 2016-01-29 13:14:14 -0800 | [diff] [blame] | 54 | private final float mTranslation; |
Winson Chung | 006ee26 | 2015-08-03 14:40:11 -0700 | [diff] [blame] | 55 | |
Sunny Goyal | 0ac7ede | 2016-01-29 13:14:14 -0800 | [diff] [blame] | 56 | State(float sbAlpha, float dtbAlpha, float translation) { |
Winson Chung | 006ee26 | 2015-08-03 14:40:11 -0700 | [diff] [blame] | 57 | mSearchBarAlpha = sbAlpha; |
| 58 | mDropTargetBarAlpha = dtbAlpha; |
Sunny Goyal | 0ac7ede | 2016-01-29 13:14:14 -0800 | [diff] [blame] | 59 | mTranslation = translation; |
Winson Chung | 006ee26 | 2015-08-03 14:40:11 -0700 | [diff] [blame] | 60 | } |
| 61 | |
Winson Chung | 006ee26 | 2015-08-03 14:40:11 -0700 | [diff] [blame] | 62 | } |
| 63 | |
Winson Chung | 006ee26 | 2015-08-03 14:40:11 -0700 | [diff] [blame] | 64 | |
Sunny Goyal | 4ffec48 | 2016-02-09 11:28:52 -0800 | [diff] [blame] | 65 | @ViewDebug.ExportedProperty(category = "launcher") |
Winson Chung | 006ee26 | 2015-08-03 14:40:11 -0700 | [diff] [blame] | 66 | private State mState = State.SEARCH_BAR; |
Sunny Goyal | d1ea63f | 2015-08-05 12:32:47 -0700 | [diff] [blame] | 67 | @Thunk View mQSB; |
Winson Chung | 4c98d92 | 2011-05-31 16:50:48 -0700 | [diff] [blame] | 68 | |
Sunny Goyal | fa401a1 | 2015-04-10 13:45:42 -0700 | [diff] [blame] | 69 | // Drop targets |
Sunny Goyal | fa401a1 | 2015-04-10 13:45:42 -0700 | [diff] [blame] | 70 | private ButtonDropTarget mDeleteDropTarget; |
| 71 | private ButtonDropTarget mUninstallDropTarget; |
| 72 | |
Winson Chung | 4c98d92 | 2011-05-31 16:50:48 -0700 | [diff] [blame] | 73 | 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 Chung | 4c98d92 | 2011-05-31 16:50:48 -0700 | [diff] [blame] | 81 | @Override |
| 82 | protected void onFinishInflate() { |
| 83 | super.onFinishInflate(); |
| 84 | |
| 85 | // Get the individual components |
Winson Chung | a6427b1 | 2011-07-27 10:53:39 -0700 | [diff] [blame] | 86 | mDeleteDropTarget = (ButtonDropTarget) mDropTargetBar.findViewById(R.id.delete_target_text); |
Tony Wickham | b54c4a3 | 2015-09-11 08:40:20 -0700 | [diff] [blame] | 87 | mUninstallDropTarget = (ButtonDropTarget) mDropTargetBar |
| 88 | .findViewById(R.id.uninstall_target_text); |
Winson Chung | a62e9fd | 2011-07-11 15:20:48 -0700 | [diff] [blame] | 89 | |
Tony Wickham | b54c4a3 | 2015-09-11 08:40:20 -0700 | [diff] [blame] | 90 | mDeleteDropTarget.setDropTargetBar(this); |
| 91 | mUninstallDropTarget.setDropTargetBar(this); |
| 92 | } |
Adam Cohen | d4d7aa5 | 2011-07-19 21:47:37 -0700 | [diff] [blame] | 93 | |
Tony Wickham | b54c4a3 | 2015-09-11 08:40:20 -0700 | [diff] [blame] | 94 | @Override |
| 95 | public void setup(Launcher launcher, DragController dragController) { |
| 96 | dragController.addDragListener(this); |
| 97 | dragController.setFlingToDeleteDropTarget(mDeleteDropTarget); |
Winson Chung | 006ee26 | 2015-08-03 14:40:11 -0700 | [diff] [blame] | 98 | |
Tony Wickham | b54c4a3 | 2015-09-11 08:40:20 -0700 | [diff] [blame] | 99 | 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 Wickham | 9aae47f | 2015-10-01 13:04:22 -0700 | [diff] [blame] | 110 | public void showDropTargets() { |
Tony Wickham | b54c4a3 | 2015-09-11 08:40:20 -0700 | [diff] [blame] | 111 | animateToState(State.DROP_TARGET, DEFAULT_DRAG_FADE_DURATION); |
| 112 | } |
| 113 | |
| 114 | @Override |
Tony Wickham | 9aae47f | 2015-10-01 13:04:22 -0700 | [diff] [blame] | 115 | public void hideDropTargets() { |
Tony Wickham | b54c4a3 | 2015-09-11 08:40:20 -0700 | [diff] [blame] | 116 | animateToState(State.SEARCH_BAR, DEFAULT_DRAG_FADE_DURATION); |
Winson Chung | 201bc82 | 2011-06-20 15:41:53 -0700 | [diff] [blame] | 117 | } |
| 118 | |
Winson Chung | 006ee26 | 2015-08-03 14:40:11 -0700 | [diff] [blame] | 119 | public void setQsbSearchBar(View qsb) { |
| 120 | mQSB = qsb; |
Winson Chung | 006ee26 | 2015-08-03 14:40:11 -0700 | [diff] [blame] | 121 | } |
| 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 Goyal | 0ac7ede | 2016-01-29 13:14:14 -0800 | [diff] [blame] | 128 | animateToState(newState, duration, null); |
| 129 | } |
| 130 | |
| 131 | public void animateToState(State newState, int duration, AnimatorSet animation) { |
Winson Chung | 006ee26 | 2015-08-03 14:40:11 -0700 | [diff] [blame] | 132 | if (mState != newState) { |
| 133 | mState = newState; |
| 134 | |
Sunny Goyal | 7c50b31 | 2016-02-10 12:26:23 -0800 | [diff] [blame] | 135 | resetAnimation(duration); |
Sunny Goyal | 0ac7ede | 2016-01-29 13:14:14 -0800 | [diff] [blame] | 136 | if (duration > 0) { |
Sunny Goyal | 0ac7ede | 2016-01-29 13:14:14 -0800 | [diff] [blame] | 137 | animateAlpha(mDropTargetBar, mState.mDropTargetBarAlpha, DEFAULT_INTERPOLATOR); |
| 138 | } else { |
| 139 | mDropTargetBar.setAlpha(mState.mDropTargetBarAlpha); |
| 140 | AlphaUpdateListener.updateVisibility(mDropTargetBar, mAccessibilityEnabled); |
| 141 | } |
Winson Chung | 006ee26 | 2015-08-03 14:40:11 -0700 | [diff] [blame] | 142 | |
Sunny Goyal | 0ac7ede | 2016-01-29 13:14:14 -0800 | [diff] [blame] | 143 | 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 Chung | 006ee26 | 2015-08-03 14:40:11 -0700 | [diff] [blame] | 175 | } |
| 176 | } |
| 177 | |
| 178 | /** |
Winson Chung | 006ee26 | 2015-08-03 14:40:11 -0700 | [diff] [blame] | 179 | * @return the bounds of the QSB search bar. |
| 180 | */ |
Mathew Inwood | cf7f63b | 2011-10-13 11:31:38 +0100 | [diff] [blame] | 181 | public Rect getSearchBarBounds() { |
Winson Chung | 006ee26 | 2015-08-03 14:40:11 -0700 | [diff] [blame] | 182 | if (mQSB != null) { |
Mathew Inwood | cf7f63b | 2011-10-13 11:31:38 +0100 | [diff] [blame] | 183 | final int[] pos = new int[2]; |
Winson Chung | 006ee26 | 2015-08-03 14:40:11 -0700 | [diff] [blame] | 184 | mQSB.getLocationOnScreen(pos); |
Mathew Inwood | cf7f63b | 2011-10-13 11:31:38 +0100 | [diff] [blame] | 185 | |
| 186 | final Rect rect = new Rect(); |
Michael Jurka | 629758f | 2012-06-14 16:18:21 -0700 | [diff] [blame] | 187 | rect.left = pos[0]; |
| 188 | rect.top = pos[1]; |
Winson Chung | 006ee26 | 2015-08-03 14:40:11 -0700 | [diff] [blame] | 189 | rect.right = pos[0] + mQSB.getWidth(); |
| 190 | rect.bottom = pos[1] + mQSB.getHeight(); |
Mathew Inwood | cf7f63b | 2011-10-13 11:31:38 +0100 | [diff] [blame] | 191 | return rect; |
| 192 | } else { |
| 193 | return null; |
| 194 | } |
| 195 | } |
Sunny Goyal | 1a70cef | 2015-04-22 11:29:51 -0700 | [diff] [blame] | 196 | |
Tony Wickham | b54c4a3 | 2015-09-11 08:40:20 -0700 | [diff] [blame] | 197 | @Override |
Sunny Goyal | 1a70cef | 2015-04-22 11:29:51 -0700 | [diff] [blame] | 198 | public void enableAccessibleDrag(boolean enable) { |
Winson Chung | 006ee26 | 2015-08-03 14:40:11 -0700 | [diff] [blame] | 199 | if (mQSB != null) { |
| 200 | mQSB.setVisibility(enable ? View.GONE : View.VISIBLE); |
Sunny Goyal | 1a70cef | 2015-04-22 11:29:51 -0700 | [diff] [blame] | 201 | } |
Sunny Goyal | 1a70cef | 2015-04-22 11:29:51 -0700 | [diff] [blame] | 202 | mDeleteDropTarget.enableAccessibleDrag(enable); |
| 203 | mUninstallDropTarget.enableAccessibleDrag(enable); |
| 204 | } |
Winson Chung | 4c98d92 | 2011-05-31 16:50:48 -0700 | [diff] [blame] | 205 | } |