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