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; |
Winson Chung | 4c98d92 | 2011-05-31 16:50:48 -0700 | [diff] [blame] | 21 | import android.content.Context; |
Mathew Inwood | cf7f63b | 2011-10-13 11:31:38 +0100 | [diff] [blame] | 22 | import android.graphics.Rect; |
Winson Chung | 4c98d92 | 2011-05-31 16:50:48 -0700 | [diff] [blame] | 23 | import android.util.AttributeSet; |
| 24 | import android.view.View; |
Winson Chung | 006ee26 | 2015-08-03 14:40:11 -0700 | [diff] [blame] | 25 | import android.view.accessibility.AccessibilityManager; |
Winson Chung | a62e9fd | 2011-07-11 15:20:48 -0700 | [diff] [blame] | 26 | import android.view.animation.AccelerateInterpolator; |
Winson Chung | 4c98d92 | 2011-05-31 16:50:48 -0700 | [diff] [blame] | 27 | import android.widget.FrameLayout; |
| 28 | |
Vadim Tryshev | fedca43 | 2015-08-19 17:55:02 -0700 | [diff] [blame] | 29 | import com.android.launcher3.dragndrop.DragController; |
Sunny Goyal | d1ea63f | 2015-08-05 12:32:47 -0700 | [diff] [blame] | 30 | import com.android.launcher3.util.Thunk; |
| 31 | |
Winson Chung | 4c98d92 | 2011-05-31 16:50:48 -0700 | [diff] [blame] | 32 | /* |
| 33 | * Ths bar will manage the transition between the QSB search bar and the delete drop |
| 34 | * targets so that each of the individual IconDropTargets don't have to. |
| 35 | */ |
| 36 | public class SearchDropTargetBar extends FrameLayout implements DragController.DragListener { |
| 37 | |
Winson Chung | 006ee26 | 2015-08-03 14:40:11 -0700 | [diff] [blame] | 38 | /** The different states that the search bar space can be in. */ |
| 39 | public enum State { |
| 40 | INVISIBLE (0f, 0f), |
| 41 | SEARCH_BAR (1f, 0f), |
| 42 | DROP_TARGET (0f, 1f); |
Winson Chung | 4c98d92 | 2011-05-31 16:50:48 -0700 | [diff] [blame] | 43 | |
Winson Chung | 006ee26 | 2015-08-03 14:40:11 -0700 | [diff] [blame] | 44 | private final float mSearchBarAlpha; |
| 45 | private final float mDropTargetBarAlpha; |
| 46 | |
| 47 | State(float sbAlpha, float dtbAlpha) { |
| 48 | mSearchBarAlpha = sbAlpha; |
| 49 | mDropTargetBarAlpha = dtbAlpha; |
| 50 | } |
| 51 | |
| 52 | float getSearchBarAlpha() { |
| 53 | return mSearchBarAlpha; |
| 54 | } |
| 55 | |
| 56 | float getDropTargetBarAlpha() { |
| 57 | return mDropTargetBarAlpha; |
| 58 | } |
| 59 | } |
| 60 | |
| 61 | private static int DEFAULT_DRAG_FADE_DURATION = 175; |
| 62 | |
| 63 | private LauncherViewPropertyAnimator mDropTargetBarAnimator; |
| 64 | private LauncherViewPropertyAnimator mQSBSearchBarAnimator; |
Winson Chung | 17f1bb8 | 2012-05-25 14:25:44 -0700 | [diff] [blame] | 65 | private static final AccelerateInterpolator sAccelerateInterpolator = |
| 66 | new AccelerateInterpolator(); |
Winson Chung | 201bc82 | 2011-06-20 15:41:53 -0700 | [diff] [blame] | 67 | |
Winson Chung | 006ee26 | 2015-08-03 14:40:11 -0700 | [diff] [blame] | 68 | private State mState = State.SEARCH_BAR; |
Sunny Goyal | d1ea63f | 2015-08-05 12:32:47 -0700 | [diff] [blame] | 69 | @Thunk View mQSB; |
| 70 | @Thunk View mDropTargetBar; |
Adam Cohen | d4d7aa5 | 2011-07-19 21:47:37 -0700 | [diff] [blame] | 71 | private boolean mDeferOnDragEnd = false; |
Sunny Goyal | d1ea63f | 2015-08-05 12:32:47 -0700 | [diff] [blame] | 72 | @Thunk boolean mAccessibilityEnabled = false; |
Winson Chung | 4c98d92 | 2011-05-31 16:50:48 -0700 | [diff] [blame] | 73 | |
Sunny Goyal | fa401a1 | 2015-04-10 13:45:42 -0700 | [diff] [blame] | 74 | // Drop targets |
| 75 | private ButtonDropTarget mInfoDropTarget; |
| 76 | private ButtonDropTarget mDeleteDropTarget; |
| 77 | private ButtonDropTarget mUninstallDropTarget; |
| 78 | |
Winson Chung | 4c98d92 | 2011-05-31 16:50:48 -0700 | [diff] [blame] | 79 | public SearchDropTargetBar(Context context, AttributeSet attrs) { |
| 80 | this(context, attrs, 0); |
| 81 | } |
| 82 | |
| 83 | public SearchDropTargetBar(Context context, AttributeSet attrs, int defStyle) { |
| 84 | super(context, attrs, defStyle); |
| 85 | } |
| 86 | |
| 87 | public void setup(Launcher launcher, DragController dragController) { |
| 88 | dragController.addDragListener(this); |
Sunny Goyal | fa401a1 | 2015-04-10 13:45:42 -0700 | [diff] [blame] | 89 | dragController.setFlingToDeleteDropTarget(mDeleteDropTarget); |
| 90 | |
Winson Chung | 4c98d92 | 2011-05-31 16:50:48 -0700 | [diff] [blame] | 91 | dragController.addDragListener(mInfoDropTarget); |
| 92 | dragController.addDragListener(mDeleteDropTarget); |
Sunny Goyal | fa401a1 | 2015-04-10 13:45:42 -0700 | [diff] [blame] | 93 | dragController.addDragListener(mUninstallDropTarget); |
| 94 | |
Winson Chung | 4c98d92 | 2011-05-31 16:50:48 -0700 | [diff] [blame] | 95 | dragController.addDropTarget(mInfoDropTarget); |
| 96 | dragController.addDropTarget(mDeleteDropTarget); |
Sunny Goyal | fa401a1 | 2015-04-10 13:45:42 -0700 | [diff] [blame] | 97 | dragController.addDropTarget(mUninstallDropTarget); |
| 98 | |
Winson Chung | 4c98d92 | 2011-05-31 16:50:48 -0700 | [diff] [blame] | 99 | mInfoDropTarget.setLauncher(launcher); |
| 100 | mDeleteDropTarget.setLauncher(launcher); |
Sunny Goyal | fa401a1 | 2015-04-10 13:45:42 -0700 | [diff] [blame] | 101 | mUninstallDropTarget.setLauncher(launcher); |
Sunny Goyal | 594d76d | 2014-11-06 10:12:54 -0800 | [diff] [blame] | 102 | } |
| 103 | |
Winson Chung | 4c98d92 | 2011-05-31 16:50:48 -0700 | [diff] [blame] | 104 | @Override |
| 105 | protected void onFinishInflate() { |
| 106 | super.onFinishInflate(); |
| 107 | |
| 108 | // Get the individual components |
Winson Chung | 4c98d92 | 2011-05-31 16:50:48 -0700 | [diff] [blame] | 109 | mDropTargetBar = findViewById(R.id.drag_target_bar); |
Winson Chung | a6427b1 | 2011-07-27 10:53:39 -0700 | [diff] [blame] | 110 | mInfoDropTarget = (ButtonDropTarget) mDropTargetBar.findViewById(R.id.info_target_text); |
| 111 | mDeleteDropTarget = (ButtonDropTarget) mDropTargetBar.findViewById(R.id.delete_target_text); |
Sunny Goyal | fa401a1 | 2015-04-10 13:45:42 -0700 | [diff] [blame] | 112 | mUninstallDropTarget = (ButtonDropTarget) mDropTargetBar.findViewById(R.id.uninstall_target_text); |
Winson Chung | a62e9fd | 2011-07-11 15:20:48 -0700 | [diff] [blame] | 113 | |
Adam Cohen | d4d7aa5 | 2011-07-19 21:47:37 -0700 | [diff] [blame] | 114 | mInfoDropTarget.setSearchDropTargetBar(this); |
| 115 | mDeleteDropTarget.setSearchDropTargetBar(this); |
Sunny Goyal | fa401a1 | 2015-04-10 13:45:42 -0700 | [diff] [blame] | 116 | mUninstallDropTarget.setSearchDropTargetBar(this); |
Adam Cohen | d4d7aa5 | 2011-07-19 21:47:37 -0700 | [diff] [blame] | 117 | |
Winson Chung | 201bc82 | 2011-06-20 15:41:53 -0700 | [diff] [blame] | 118 | // Create the various fade animations |
Winson Chung | dc61c4d | 2015-04-20 18:26:57 -0700 | [diff] [blame] | 119 | mDropTargetBar.setAlpha(0f); |
Winson Chung | 006ee26 | 2015-08-03 14:40:11 -0700 | [diff] [blame] | 120 | mDropTargetBarAnimator = new LauncherViewPropertyAnimator(mDropTargetBar); |
| 121 | mDropTargetBarAnimator.setInterpolator(sAccelerateInterpolator); |
| 122 | mDropTargetBarAnimator.addListener(new AnimatorListenerAdapter() { |
| 123 | @Override |
| 124 | public void onAnimationStart(Animator animation) { |
| 125 | // Ensure that the view is visible for the animation |
| 126 | mDropTargetBar.setVisibility(View.VISIBLE); |
| 127 | } |
| 128 | |
| 129 | @Override |
| 130 | public void onAnimationEnd(Animator animation) { |
| 131 | if (mDropTargetBar != null) { |
| 132 | AlphaUpdateListener.updateVisibility(mDropTargetBar, mAccessibilityEnabled); |
| 133 | } |
| 134 | } |
| 135 | }); |
Winson Chung | 201bc82 | 2011-06-20 15:41:53 -0700 | [diff] [blame] | 136 | } |
| 137 | |
Winson Chung | 006ee26 | 2015-08-03 14:40:11 -0700 | [diff] [blame] | 138 | public void setQsbSearchBar(View qsb) { |
| 139 | mQSB = qsb; |
| 140 | if (mQSB != null) { |
| 141 | // Update the search ber animation |
| 142 | mQSBSearchBarAnimator = new LauncherViewPropertyAnimator(mQSB); |
| 143 | mQSBSearchBarAnimator.setInterpolator(sAccelerateInterpolator); |
| 144 | mQSBSearchBarAnimator.addListener(new AnimatorListenerAdapter() { |
| 145 | @Override |
| 146 | public void onAnimationStart(Animator animation) { |
| 147 | // Ensure that the view is visible for the animation |
| 148 | if (mQSB != null) { |
| 149 | mQSB.setVisibility(View.VISIBLE); |
| 150 | } |
| 151 | } |
Winson Chung | 4c98d92 | 2011-05-31 16:50:48 -0700 | [diff] [blame] | 152 | |
Winson Chung | 006ee26 | 2015-08-03 14:40:11 -0700 | [diff] [blame] | 153 | @Override |
| 154 | public void onAnimationEnd(Animator animation) { |
| 155 | if (mQSB != null) { |
| 156 | AlphaUpdateListener.updateVisibility(mQSB, mAccessibilityEnabled); |
| 157 | } |
| 158 | } |
| 159 | }); |
Winson Chung | f0ea4d3 | 2011-06-06 14:27:16 -0700 | [diff] [blame] | 160 | } else { |
Winson Chung | 006ee26 | 2015-08-03 14:40:11 -0700 | [diff] [blame] | 161 | mQSBSearchBarAnimator = null; |
| 162 | } |
| 163 | } |
| 164 | |
| 165 | /** |
| 166 | * Animates the current search bar state to a new state. If the {@param duration} is 0, then |
| 167 | * the state is applied immediately. |
| 168 | */ |
| 169 | public void animateToState(State newState, int duration) { |
| 170 | if (mState != newState) { |
| 171 | mState = newState; |
| 172 | |
| 173 | // Update the accessibility state |
| 174 | AccessibilityManager am = (AccessibilityManager) |
| 175 | getContext().getSystemService(Context.ACCESSIBILITY_SERVICE); |
| 176 | mAccessibilityEnabled = am.isEnabled(); |
| 177 | |
| 178 | animateViewAlpha(mQSBSearchBarAnimator, mQSB, newState.getSearchBarAlpha(), |
| 179 | duration); |
| 180 | animateViewAlpha(mDropTargetBarAnimator, mDropTargetBar, newState.getDropTargetBarAlpha(), |
| 181 | duration); |
| 182 | } |
| 183 | } |
| 184 | |
| 185 | /** |
| 186 | * Convenience method to animate the alpha of a view using hardware layers. |
| 187 | */ |
| 188 | private void animateViewAlpha(LauncherViewPropertyAnimator animator, View v, float alpha, |
| 189 | int duration) { |
Winson | 81c5f7e | 2015-08-20 15:22:18 -0700 | [diff] [blame] | 190 | if (v == null) { |
| 191 | return; |
| 192 | } |
| 193 | |
| 194 | animator.cancel(); |
| 195 | if (Float.compare(v.getAlpha(), alpha) != 0) { |
Winson Chung | 006ee26 | 2015-08-03 14:40:11 -0700 | [diff] [blame] | 196 | if (duration > 0) { |
| 197 | animator.alpha(alpha).withLayer().setDuration(duration).start(); |
| 198 | } else { |
| 199 | v.setAlpha(alpha); |
| 200 | AlphaUpdateListener.updateVisibility(v, mAccessibilityEnabled); |
Michael Jurka | 19e3347 | 2012-05-14 20:41:52 -0700 | [diff] [blame] | 201 | } |
Winson Chung | f0ea4d3 | 2011-06-06 14:27:16 -0700 | [diff] [blame] | 202 | } |
Winson Chung | f0ea4d3 | 2011-06-06 14:27:16 -0700 | [diff] [blame] | 203 | } |
| 204 | |
| 205 | /* |
Winson Chung | 4c98d92 | 2011-05-31 16:50:48 -0700 | [diff] [blame] | 206 | * DragController.DragListener implementation |
| 207 | */ |
| 208 | @Override |
Sunny Goyal | aa8ef11 | 2015-06-12 20:04:41 -0700 | [diff] [blame] | 209 | public void onDragStart(DragSource source, ItemInfo info, int dragAction) { |
Winson Chung | 006ee26 | 2015-08-03 14:40:11 -0700 | [diff] [blame] | 210 | animateToState(State.DROP_TARGET, DEFAULT_DRAG_FADE_DURATION); |
Adam Cohen | c9735cf | 2015-01-23 16:11:55 -0800 | [diff] [blame] | 211 | } |
| 212 | |
Winson Chung | 006ee26 | 2015-08-03 14:40:11 -0700 | [diff] [blame] | 213 | /** |
| 214 | * This is called to defer hiding the delete drop target until the drop animation has completed, |
| 215 | * instead of hiding immediately when the drag has ended. |
| 216 | */ |
Adam Cohen | d4d7aa5 | 2011-07-19 21:47:37 -0700 | [diff] [blame] | 217 | public void deferOnDragEnd() { |
| 218 | mDeferOnDragEnd = true; |
| 219 | } |
| 220 | |
Winson Chung | 4c98d92 | 2011-05-31 16:50:48 -0700 | [diff] [blame] | 221 | @Override |
| 222 | public void onDragEnd() { |
Adam Cohen | d4d7aa5 | 2011-07-19 21:47:37 -0700 | [diff] [blame] | 223 | if (!mDeferOnDragEnd) { |
Winson Chung | 006ee26 | 2015-08-03 14:40:11 -0700 | [diff] [blame] | 224 | animateToState(State.SEARCH_BAR, DEFAULT_DRAG_FADE_DURATION); |
Adam Cohen | d4d7aa5 | 2011-07-19 21:47:37 -0700 | [diff] [blame] | 225 | } else { |
| 226 | mDeferOnDragEnd = false; |
Winson Chung | f0ea4d3 | 2011-06-06 14:27:16 -0700 | [diff] [blame] | 227 | } |
Winson Chung | 4c98d92 | 2011-05-31 16:50:48 -0700 | [diff] [blame] | 228 | } |
Winson Chung | c51db6a | 2011-10-05 11:44:49 -0700 | [diff] [blame] | 229 | |
Winson Chung | 006ee26 | 2015-08-03 14:40:11 -0700 | [diff] [blame] | 230 | /** |
| 231 | * @return the bounds of the QSB search bar. |
| 232 | */ |
Mathew Inwood | cf7f63b | 2011-10-13 11:31:38 +0100 | [diff] [blame] | 233 | public Rect getSearchBarBounds() { |
Winson Chung | 006ee26 | 2015-08-03 14:40:11 -0700 | [diff] [blame] | 234 | if (mQSB != null) { |
Mathew Inwood | cf7f63b | 2011-10-13 11:31:38 +0100 | [diff] [blame] | 235 | final int[] pos = new int[2]; |
Winson Chung | 006ee26 | 2015-08-03 14:40:11 -0700 | [diff] [blame] | 236 | mQSB.getLocationOnScreen(pos); |
Mathew Inwood | cf7f63b | 2011-10-13 11:31:38 +0100 | [diff] [blame] | 237 | |
| 238 | final Rect rect = new Rect(); |
Michael Jurka | 629758f | 2012-06-14 16:18:21 -0700 | [diff] [blame] | 239 | rect.left = pos[0]; |
| 240 | rect.top = pos[1]; |
Winson Chung | 006ee26 | 2015-08-03 14:40:11 -0700 | [diff] [blame] | 241 | rect.right = pos[0] + mQSB.getWidth(); |
| 242 | rect.bottom = pos[1] + mQSB.getHeight(); |
Mathew Inwood | cf7f63b | 2011-10-13 11:31:38 +0100 | [diff] [blame] | 243 | return rect; |
| 244 | } else { |
| 245 | return null; |
| 246 | } |
| 247 | } |
Sunny Goyal | 1a70cef | 2015-04-22 11:29:51 -0700 | [diff] [blame] | 248 | |
| 249 | public void enableAccessibleDrag(boolean enable) { |
Winson Chung | 006ee26 | 2015-08-03 14:40:11 -0700 | [diff] [blame] | 250 | if (mQSB != null) { |
| 251 | mQSB.setVisibility(enable ? View.GONE : View.VISIBLE); |
Sunny Goyal | 1a70cef | 2015-04-22 11:29:51 -0700 | [diff] [blame] | 252 | } |
| 253 | mInfoDropTarget.enableAccessibleDrag(enable); |
| 254 | mDeleteDropTarget.enableAccessibleDrag(enable); |
| 255 | mUninstallDropTarget.enableAccessibleDrag(enable); |
| 256 | } |
Winson Chung | 4c98d92 | 2011-05-31 16:50:48 -0700 | [diff] [blame] | 257 | } |