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; |
Winson Chung | 006ee26 | 2015-08-03 14:40:11 -0700 | [diff] [blame] | 28 | import android.view.accessibility.AccessibilityManager; |
Winson Chung | a62e9fd | 2011-07-11 15:20:48 -0700 | [diff] [blame] | 29 | import android.view.animation.AccelerateInterpolator; |
Sunny Goyal | 0ac7ede | 2016-01-29 13:14:14 -0800 | [diff] [blame] | 30 | import android.view.animation.DecelerateInterpolator; |
Winson Chung | 4c98d92 | 2011-05-31 16:50:48 -0700 | [diff] [blame] | 31 | |
Vadim Tryshev | fedca43 | 2015-08-19 17:55:02 -0700 | [diff] [blame] | 32 | import com.android.launcher3.dragndrop.DragController; |
Sunny Goyal | d1ea63f | 2015-08-05 12:32:47 -0700 | [diff] [blame] | 33 | import com.android.launcher3.util.Thunk; |
| 34 | |
Winson Chung | 4c98d92 | 2011-05-31 16:50:48 -0700 | [diff] [blame] | 35 | /* |
Tony Wickham | b54c4a3 | 2015-09-11 08:40:20 -0700 | [diff] [blame] | 36 | * This bar will manage the transition between the QSB search bar and the delete/uninstall drop |
| 37 | * targets so that each of the individual ButtonDropTargets don't have to. |
Winson Chung | 4c98d92 | 2011-05-31 16:50:48 -0700 | [diff] [blame] | 38 | */ |
Tony Wickham | b54c4a3 | 2015-09-11 08:40:20 -0700 | [diff] [blame] | 39 | public class SearchDropTargetBar extends BaseDropTargetBar { |
Winson Chung | 4c98d92 | 2011-05-31 16:50:48 -0700 | [diff] [blame] | 40 | |
Sunny Goyal | 0ac7ede | 2016-01-29 13:14:14 -0800 | [diff] [blame] | 41 | private static final TimeInterpolator MOVE_DOWN_INTERPOLATOR = new DecelerateInterpolator(0.6f); |
| 42 | private static final TimeInterpolator MOVE_UP_INTERPOLATOR = new DecelerateInterpolator(1.5f); |
Sunny Goyal | 0ac7ede | 2016-01-29 13:14:14 -0800 | [diff] [blame] | 43 | |
Winson Chung | 006ee26 | 2015-08-03 14:40:11 -0700 | [diff] [blame] | 44 | /** The different states that the search bar space can be in. */ |
| 45 | public enum State { |
Sunny Goyal | 0ac7ede | 2016-01-29 13:14:14 -0800 | [diff] [blame] | 46 | INVISIBLE (0f, 0f, 0f), |
| 47 | INVISIBLE_TRANSLATED (0f, 0f, -1f), |
| 48 | SEARCH_BAR (1f, 0f, 0f), |
| 49 | DROP_TARGET (0f, 1f, 0f); |
Winson Chung | 4c98d92 | 2011-05-31 16:50:48 -0700 | [diff] [blame] | 50 | |
Winson Chung | 006ee26 | 2015-08-03 14:40:11 -0700 | [diff] [blame] | 51 | private final float mSearchBarAlpha; |
| 52 | private final float mDropTargetBarAlpha; |
Sunny Goyal | 0ac7ede | 2016-01-29 13:14:14 -0800 | [diff] [blame] | 53 | private final float mTranslation; |
Winson Chung | 006ee26 | 2015-08-03 14:40:11 -0700 | [diff] [blame] | 54 | |
Sunny Goyal | 0ac7ede | 2016-01-29 13:14:14 -0800 | [diff] [blame] | 55 | State(float sbAlpha, float dtbAlpha, float translation) { |
Winson Chung | 006ee26 | 2015-08-03 14:40:11 -0700 | [diff] [blame] | 56 | mSearchBarAlpha = sbAlpha; |
| 57 | mDropTargetBarAlpha = dtbAlpha; |
Sunny Goyal | 0ac7ede | 2016-01-29 13:14:14 -0800 | [diff] [blame] | 58 | mTranslation = translation; |
Winson Chung | 006ee26 | 2015-08-03 14:40:11 -0700 | [diff] [blame] | 59 | } |
| 60 | |
Winson Chung | 006ee26 | 2015-08-03 14:40:11 -0700 | [diff] [blame] | 61 | } |
| 62 | |
Winson Chung | 006ee26 | 2015-08-03 14:40:11 -0700 | [diff] [blame] | 63 | |
Winson Chung | 006ee26 | 2015-08-03 14:40:11 -0700 | [diff] [blame] | 64 | private State mState = State.SEARCH_BAR; |
Sunny Goyal | d1ea63f | 2015-08-05 12:32:47 -0700 | [diff] [blame] | 65 | @Thunk View mQSB; |
Winson Chung | 4c98d92 | 2011-05-31 16:50:48 -0700 | [diff] [blame] | 66 | |
Sunny Goyal | fa401a1 | 2015-04-10 13:45:42 -0700 | [diff] [blame] | 67 | // Drop targets |
Sunny Goyal | fa401a1 | 2015-04-10 13:45:42 -0700 | [diff] [blame] | 68 | private ButtonDropTarget mDeleteDropTarget; |
| 69 | private ButtonDropTarget mUninstallDropTarget; |
| 70 | |
Winson Chung | 4c98d92 | 2011-05-31 16:50:48 -0700 | [diff] [blame] | 71 | public SearchDropTargetBar(Context context, AttributeSet attrs) { |
| 72 | this(context, attrs, 0); |
| 73 | } |
| 74 | |
| 75 | public SearchDropTargetBar(Context context, AttributeSet attrs, int defStyle) { |
| 76 | super(context, attrs, defStyle); |
| 77 | } |
| 78 | |
Winson Chung | 4c98d92 | 2011-05-31 16:50:48 -0700 | [diff] [blame] | 79 | @Override |
| 80 | protected void onFinishInflate() { |
| 81 | super.onFinishInflate(); |
| 82 | |
| 83 | // Get the individual components |
Winson Chung | a6427b1 | 2011-07-27 10:53:39 -0700 | [diff] [blame] | 84 | mDeleteDropTarget = (ButtonDropTarget) mDropTargetBar.findViewById(R.id.delete_target_text); |
Tony Wickham | b54c4a3 | 2015-09-11 08:40:20 -0700 | [diff] [blame] | 85 | mUninstallDropTarget = (ButtonDropTarget) mDropTargetBar |
| 86 | .findViewById(R.id.uninstall_target_text); |
Winson Chung | a62e9fd | 2011-07-11 15:20:48 -0700 | [diff] [blame] | 87 | |
Tony Wickham | b54c4a3 | 2015-09-11 08:40:20 -0700 | [diff] [blame] | 88 | mDeleteDropTarget.setDropTargetBar(this); |
| 89 | mUninstallDropTarget.setDropTargetBar(this); |
| 90 | } |
Adam Cohen | d4d7aa5 | 2011-07-19 21:47:37 -0700 | [diff] [blame] | 91 | |
Tony Wickham | b54c4a3 | 2015-09-11 08:40:20 -0700 | [diff] [blame] | 92 | @Override |
| 93 | public void setup(Launcher launcher, DragController dragController) { |
| 94 | dragController.addDragListener(this); |
| 95 | dragController.setFlingToDeleteDropTarget(mDeleteDropTarget); |
Winson Chung | 006ee26 | 2015-08-03 14:40:11 -0700 | [diff] [blame] | 96 | |
Tony Wickham | b54c4a3 | 2015-09-11 08:40:20 -0700 | [diff] [blame] | 97 | dragController.addDragListener(mDeleteDropTarget); |
| 98 | dragController.addDragListener(mUninstallDropTarget); |
| 99 | |
| 100 | dragController.addDropTarget(mDeleteDropTarget); |
| 101 | dragController.addDropTarget(mUninstallDropTarget); |
| 102 | |
| 103 | mDeleteDropTarget.setLauncher(launcher); |
| 104 | mUninstallDropTarget.setLauncher(launcher); |
| 105 | } |
| 106 | |
| 107 | @Override |
Tony Wickham | 9aae47f | 2015-10-01 13:04:22 -0700 | [diff] [blame] | 108 | public void showDropTargets() { |
Tony Wickham | b54c4a3 | 2015-09-11 08:40:20 -0700 | [diff] [blame] | 109 | animateToState(State.DROP_TARGET, DEFAULT_DRAG_FADE_DURATION); |
| 110 | } |
| 111 | |
| 112 | @Override |
Tony Wickham | 9aae47f | 2015-10-01 13:04:22 -0700 | [diff] [blame] | 113 | public void hideDropTargets() { |
Tony Wickham | b54c4a3 | 2015-09-11 08:40:20 -0700 | [diff] [blame] | 114 | animateToState(State.SEARCH_BAR, DEFAULT_DRAG_FADE_DURATION); |
Winson Chung | 201bc82 | 2011-06-20 15:41:53 -0700 | [diff] [blame] | 115 | } |
| 116 | |
Winson Chung | 006ee26 | 2015-08-03 14:40:11 -0700 | [diff] [blame] | 117 | public void setQsbSearchBar(View qsb) { |
| 118 | mQSB = qsb; |
Winson Chung | 006ee26 | 2015-08-03 14:40:11 -0700 | [diff] [blame] | 119 | } |
| 120 | |
| 121 | /** |
| 122 | * Animates the current search bar state to a new state. If the {@param duration} is 0, then |
| 123 | * the state is applied immediately. |
| 124 | */ |
| 125 | public void animateToState(State newState, int duration) { |
Sunny Goyal | 0ac7ede | 2016-01-29 13:14:14 -0800 | [diff] [blame] | 126 | animateToState(newState, duration, null); |
| 127 | } |
| 128 | |
| 129 | public void animateToState(State newState, int duration, AnimatorSet animation) { |
Winson Chung | 006ee26 | 2015-08-03 14:40:11 -0700 | [diff] [blame] | 130 | if (mState != newState) { |
| 131 | mState = newState; |
| 132 | |
Sunny Goyal | 7c50b31 | 2016-02-10 12:26:23 -0800 | [diff] [blame^] | 133 | resetAnimation(duration); |
Sunny Goyal | 0ac7ede | 2016-01-29 13:14:14 -0800 | [diff] [blame] | 134 | if (duration > 0) { |
Sunny Goyal | 0ac7ede | 2016-01-29 13:14:14 -0800 | [diff] [blame] | 135 | animateAlpha(mDropTargetBar, mState.mDropTargetBarAlpha, DEFAULT_INTERPOLATOR); |
| 136 | } else { |
| 137 | mDropTargetBar.setAlpha(mState.mDropTargetBarAlpha); |
| 138 | AlphaUpdateListener.updateVisibility(mDropTargetBar, mAccessibilityEnabled); |
| 139 | } |
Winson Chung | 006ee26 | 2015-08-03 14:40:11 -0700 | [diff] [blame] | 140 | |
Sunny Goyal | 0ac7ede | 2016-01-29 13:14:14 -0800 | [diff] [blame] | 141 | if (mQSB != null) { |
| 142 | boolean isVertical = ((Launcher) getContext()).getDeviceProfile() |
| 143 | .isVerticalBarLayout(); |
| 144 | float translation = isVertical ? 0 : mState.mTranslation * getMeasuredHeight(); |
| 145 | |
| 146 | if (duration > 0) { |
| 147 | int translationChange = Float.compare(mQSB.getTranslationY(), translation); |
| 148 | |
| 149 | animateAlpha(mQSB, mState.mSearchBarAlpha, |
| 150 | translationChange == 0 ? DEFAULT_INTERPOLATOR |
| 151 | : (translationChange < 0 ? MOVE_DOWN_INTERPOLATOR |
| 152 | : MOVE_UP_INTERPOLATOR)); |
| 153 | |
| 154 | if (translationChange != 0) { |
| 155 | mCurrentAnimation.play( |
| 156 | ObjectAnimator.ofFloat(mQSB, View.TRANSLATION_Y, translation)); |
| 157 | } |
| 158 | } else { |
| 159 | mQSB.setTranslationY(translation); |
| 160 | mQSB.setAlpha(mState.mSearchBarAlpha); |
| 161 | AlphaUpdateListener.updateVisibility(mQSB, mAccessibilityEnabled); |
| 162 | } |
| 163 | } |
| 164 | |
| 165 | // Start the final animation |
| 166 | if (duration > 0) { |
| 167 | if (animation != null) { |
| 168 | animation.play(mCurrentAnimation); |
| 169 | } else { |
| 170 | mCurrentAnimation.start(); |
| 171 | } |
| 172 | } |
Winson Chung | 006ee26 | 2015-08-03 14:40:11 -0700 | [diff] [blame] | 173 | } |
| 174 | } |
| 175 | |
| 176 | /** |
Winson Chung | 006ee26 | 2015-08-03 14:40:11 -0700 | [diff] [blame] | 177 | * @return the bounds of the QSB search bar. |
| 178 | */ |
Mathew Inwood | cf7f63b | 2011-10-13 11:31:38 +0100 | [diff] [blame] | 179 | public Rect getSearchBarBounds() { |
Winson Chung | 006ee26 | 2015-08-03 14:40:11 -0700 | [diff] [blame] | 180 | if (mQSB != null) { |
Mathew Inwood | cf7f63b | 2011-10-13 11:31:38 +0100 | [diff] [blame] | 181 | final int[] pos = new int[2]; |
Winson Chung | 006ee26 | 2015-08-03 14:40:11 -0700 | [diff] [blame] | 182 | mQSB.getLocationOnScreen(pos); |
Mathew Inwood | cf7f63b | 2011-10-13 11:31:38 +0100 | [diff] [blame] | 183 | |
| 184 | final Rect rect = new Rect(); |
Michael Jurka | 629758f | 2012-06-14 16:18:21 -0700 | [diff] [blame] | 185 | rect.left = pos[0]; |
| 186 | rect.top = pos[1]; |
Winson Chung | 006ee26 | 2015-08-03 14:40:11 -0700 | [diff] [blame] | 187 | rect.right = pos[0] + mQSB.getWidth(); |
| 188 | rect.bottom = pos[1] + mQSB.getHeight(); |
Mathew Inwood | cf7f63b | 2011-10-13 11:31:38 +0100 | [diff] [blame] | 189 | return rect; |
| 190 | } else { |
| 191 | return null; |
| 192 | } |
| 193 | } |
Sunny Goyal | 1a70cef | 2015-04-22 11:29:51 -0700 | [diff] [blame] | 194 | |
Tony Wickham | b54c4a3 | 2015-09-11 08:40:20 -0700 | [diff] [blame] | 195 | @Override |
Sunny Goyal | 1a70cef | 2015-04-22 11:29:51 -0700 | [diff] [blame] | 196 | public void enableAccessibleDrag(boolean enable) { |
Winson Chung | 006ee26 | 2015-08-03 14:40:11 -0700 | [diff] [blame] | 197 | if (mQSB != null) { |
| 198 | mQSB.setVisibility(enable ? View.GONE : View.VISIBLE); |
Sunny Goyal | 1a70cef | 2015-04-22 11:29:51 -0700 | [diff] [blame] | 199 | } |
Sunny Goyal | 1a70cef | 2015-04-22 11:29:51 -0700 | [diff] [blame] | 200 | mDeleteDropTarget.enableAccessibleDrag(enable); |
| 201 | mUninstallDropTarget.enableAccessibleDrag(enable); |
| 202 | } |
Winson Chung | 4c98d92 | 2011-05-31 16:50:48 -0700 | [diff] [blame] | 203 | } |