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; |
| 21 | import android.animation.ObjectAnimator; |
Adam Cohen | c2d6e89 | 2014-10-16 09:49:24 -0700 | [diff] [blame] | 22 | import android.animation.ValueAnimator; |
Winson Chung | 4c98d92 | 2011-05-31 16:50:48 -0700 | [diff] [blame] | 23 | import android.content.Context; |
Mathew Inwood | cf7f63b | 2011-10-13 11:31:38 +0100 | [diff] [blame] | 24 | import android.graphics.Rect; |
Winson Chung | 4c98d92 | 2011-05-31 16:50:48 -0700 | [diff] [blame] | 25 | import android.util.AttributeSet; |
| 26 | import android.view.View; |
Winson Chung | a62e9fd | 2011-07-11 15:20:48 -0700 | [diff] [blame] | 27 | import android.view.animation.AccelerateInterpolator; |
Winson Chung | 4c98d92 | 2011-05-31 16:50:48 -0700 | [diff] [blame] | 28 | import android.widget.FrameLayout; |
| 29 | |
Winson Chung | 4c98d92 | 2011-05-31 16:50:48 -0700 | [diff] [blame] | 30 | /* |
| 31 | * Ths bar will manage the transition between the QSB search bar and the delete drop |
| 32 | * targets so that each of the individual IconDropTargets don't have to. |
| 33 | */ |
| 34 | public class SearchDropTargetBar extends FrameLayout implements DragController.DragListener { |
| 35 | |
Winson Chung | a62e9fd | 2011-07-11 15:20:48 -0700 | [diff] [blame] | 36 | private static final int sTransitionInDuration = 200; |
| 37 | private static final int sTransitionOutDuration = 175; |
Winson Chung | 4c98d92 | 2011-05-31 16:50:48 -0700 | [diff] [blame] | 38 | |
Winson Chung | 17f1bb8 | 2012-05-25 14:25:44 -0700 | [diff] [blame] | 39 | private ObjectAnimator mDropTargetBarAnim; |
Adam Cohen | c2d6e89 | 2014-10-16 09:49:24 -0700 | [diff] [blame] | 40 | private ValueAnimator mQSBSearchBarAnim; |
Winson Chung | 17f1bb8 | 2012-05-25 14:25:44 -0700 | [diff] [blame] | 41 | private static final AccelerateInterpolator sAccelerateInterpolator = |
| 42 | new AccelerateInterpolator(); |
Winson Chung | 201bc82 | 2011-06-20 15:41:53 -0700 | [diff] [blame] | 43 | |
Winson Chung | f0ea4d3 | 2011-06-06 14:27:16 -0700 | [diff] [blame] | 44 | private boolean mIsSearchBarHidden; |
Winson Chung | 4c98d92 | 2011-05-31 16:50:48 -0700 | [diff] [blame] | 45 | private View mQSBSearchBar; |
| 46 | private View mDropTargetBar; |
Winson Chung | 61fa419 | 2011-06-12 15:15:29 -0700 | [diff] [blame] | 47 | private ButtonDropTarget mInfoDropTarget; |
| 48 | private ButtonDropTarget mDeleteDropTarget; |
Winson Chung | a62e9fd | 2011-07-11 15:20:48 -0700 | [diff] [blame] | 49 | private int mBarHeight; |
Adam Cohen | d4d7aa5 | 2011-07-19 21:47:37 -0700 | [diff] [blame] | 50 | private boolean mDeferOnDragEnd = false; |
Winson Chung | 4c98d92 | 2011-05-31 16:50:48 -0700 | [diff] [blame] | 51 | |
Michael Jurka | 19e3347 | 2012-05-14 20:41:52 -0700 | [diff] [blame] | 52 | private boolean mEnableDropDownDropTargets; |
Winson Chung | c51db6a | 2011-10-05 11:44:49 -0700 | [diff] [blame] | 53 | |
Winson Chung | 4c98d92 | 2011-05-31 16:50:48 -0700 | [diff] [blame] | 54 | public SearchDropTargetBar(Context context, AttributeSet attrs) { |
| 55 | this(context, attrs, 0); |
| 56 | } |
| 57 | |
| 58 | public SearchDropTargetBar(Context context, AttributeSet attrs, int defStyle) { |
| 59 | super(context, attrs, defStyle); |
| 60 | } |
| 61 | |
| 62 | public void setup(Launcher launcher, DragController dragController) { |
| 63 | dragController.addDragListener(this); |
| 64 | dragController.addDragListener(mInfoDropTarget); |
| 65 | dragController.addDragListener(mDeleteDropTarget); |
| 66 | dragController.addDropTarget(mInfoDropTarget); |
| 67 | dragController.addDropTarget(mDeleteDropTarget); |
Winson Chung | 043f2af | 2012-03-01 16:09:54 -0800 | [diff] [blame] | 68 | dragController.setFlingToDeleteDropTarget(mDeleteDropTarget); |
Winson Chung | 4c98d92 | 2011-05-31 16:50:48 -0700 | [diff] [blame] | 69 | mInfoDropTarget.setLauncher(launcher); |
| 70 | mDeleteDropTarget.setLauncher(launcher); |
Sunny Goyal | 594d76d | 2014-11-06 10:12:54 -0800 | [diff] [blame] | 71 | } |
| 72 | |
| 73 | public void setQsbSearchBar(View qsb) { |
| 74 | mQSBSearchBar = qsb; |
Adam Cohen | c2d6e89 | 2014-10-16 09:49:24 -0700 | [diff] [blame] | 75 | if (mQSBSearchBar != null) { |
| 76 | if (mEnableDropDownDropTargets) { |
| 77 | mQSBSearchBarAnim = LauncherAnimUtils.ofFloat(mQSBSearchBar, "translationY", 0, |
| 78 | -mBarHeight); |
| 79 | } else { |
| 80 | mQSBSearchBarAnim = LauncherAnimUtils.ofFloat(mQSBSearchBar, "alpha", 1f, 0f); |
| 81 | } |
| 82 | setupAnimation(mQSBSearchBarAnim, mQSBSearchBar); |
Cristina Stancu | 476493b | 2013-08-07 17:20:14 +0100 | [diff] [blame] | 83 | } else { |
Adam Cohen | c2d6e89 | 2014-10-16 09:49:24 -0700 | [diff] [blame] | 84 | // Create a no-op animation of the search bar is null |
| 85 | mQSBSearchBarAnim = ValueAnimator.ofFloat(0, 0); |
| 86 | mQSBSearchBarAnim.setDuration(sTransitionInDuration); |
Cristina Stancu | 476493b | 2013-08-07 17:20:14 +0100 | [diff] [blame] | 87 | } |
Winson Chung | 4c98d92 | 2011-05-31 16:50:48 -0700 | [diff] [blame] | 88 | } |
| 89 | |
Winson Chung | c7d2b60 | 2012-05-16 17:02:20 -0700 | [diff] [blame] | 90 | private void prepareStartAnimation(View v) { |
Winson Chung | 17f1bb8 | 2012-05-25 14:25:44 -0700 | [diff] [blame] | 91 | // Enable the hw layers before the animation starts (will be disabled in the onAnimationEnd |
| 92 | // callback below) |
Adam Cohen | c2d6e89 | 2014-10-16 09:49:24 -0700 | [diff] [blame] | 93 | if (v != null) { |
| 94 | v.setLayerType(View.LAYER_TYPE_HARDWARE, null); |
| 95 | } |
Winson Chung | c7d2b60 | 2012-05-16 17:02:20 -0700 | [diff] [blame] | 96 | } |
| 97 | |
Adam Cohen | c2d6e89 | 2014-10-16 09:49:24 -0700 | [diff] [blame] | 98 | private void setupAnimation(ValueAnimator anim, final View v) { |
Winson Chung | 17f1bb8 | 2012-05-25 14:25:44 -0700 | [diff] [blame] | 99 | anim.setInterpolator(sAccelerateInterpolator); |
| 100 | anim.setDuration(sTransitionInDuration); |
| 101 | anim.addListener(new AnimatorListenerAdapter() { |
Winson Chung | 315b3ba | 2012-05-11 10:51:32 -0700 | [diff] [blame] | 102 | @Override |
| 103 | public void onAnimationEnd(Animator animation) { |
Adam Cohen | c2d6e89 | 2014-10-16 09:49:24 -0700 | [diff] [blame] | 104 | if (v != null) { |
| 105 | v.setLayerType(View.LAYER_TYPE_NONE, null); |
| 106 | } |
Winson Chung | 315b3ba | 2012-05-11 10:51:32 -0700 | [diff] [blame] | 107 | } |
| 108 | }); |
| 109 | } |
| 110 | |
Winson Chung | 4c98d92 | 2011-05-31 16:50:48 -0700 | [diff] [blame] | 111 | @Override |
| 112 | protected void onFinishInflate() { |
| 113 | super.onFinishInflate(); |
| 114 | |
| 115 | // Get the individual components |
Winson Chung | 4c98d92 | 2011-05-31 16:50:48 -0700 | [diff] [blame] | 116 | mDropTargetBar = findViewById(R.id.drag_target_bar); |
Winson Chung | a6427b1 | 2011-07-27 10:53:39 -0700 | [diff] [blame] | 117 | mInfoDropTarget = (ButtonDropTarget) mDropTargetBar.findViewById(R.id.info_target_text); |
| 118 | mDeleteDropTarget = (ButtonDropTarget) mDropTargetBar.findViewById(R.id.delete_target_text); |
Winson Chung | a62e9fd | 2011-07-11 15:20:48 -0700 | [diff] [blame] | 119 | |
Adam Cohen | d4d7aa5 | 2011-07-19 21:47:37 -0700 | [diff] [blame] | 120 | mInfoDropTarget.setSearchDropTargetBar(this); |
| 121 | mDeleteDropTarget.setSearchDropTargetBar(this); |
| 122 | |
Michael Jurka | 19e3347 | 2012-05-14 20:41:52 -0700 | [diff] [blame] | 123 | mEnableDropDownDropTargets = |
Winson Chung | a62e9fd | 2011-07-11 15:20:48 -0700 | [diff] [blame] | 124 | getResources().getBoolean(R.bool.config_useDropTargetDownTransition); |
Winson Chung | 201bc82 | 2011-06-20 15:41:53 -0700 | [diff] [blame] | 125 | |
| 126 | // Create the various fade animations |
Michael Jurka | 19e3347 | 2012-05-14 20:41:52 -0700 | [diff] [blame] | 127 | if (mEnableDropDownDropTargets) { |
Winson Chung | 5f8afe6 | 2013-08-12 16:19:28 -0700 | [diff] [blame] | 128 | LauncherAppState app = LauncherAppState.getInstance(); |
| 129 | DeviceProfile grid = app.getDynamicGrid().getDeviceProfile(); |
| 130 | mBarHeight = grid.searchBarSpaceHeightPx; |
Winson Chung | a62e9fd | 2011-07-11 15:20:48 -0700 | [diff] [blame] | 131 | mDropTargetBar.setTranslationY(-mBarHeight); |
Michael Jurka | 66b7d01 | 2013-11-06 16:37:34 +0100 | [diff] [blame] | 132 | mDropTargetBarAnim = LauncherAnimUtils.ofFloat(mDropTargetBar, "translationY", |
Winson Chung | 17f1bb8 | 2012-05-25 14:25:44 -0700 | [diff] [blame] | 133 | -mBarHeight, 0f); |
Cristina Stancu | 476493b | 2013-08-07 17:20:14 +0100 | [diff] [blame] | 134 | |
Winson Chung | 315b3ba | 2012-05-11 10:51:32 -0700 | [diff] [blame] | 135 | } else { |
| 136 | mDropTargetBar.setAlpha(0f); |
Michael Jurka | 66b7d01 | 2013-11-06 16:37:34 +0100 | [diff] [blame] | 137 | mDropTargetBarAnim = LauncherAnimUtils.ofFloat(mDropTargetBar, "alpha", 0f, 1f); |
Winson Chung | a62e9fd | 2011-07-11 15:20:48 -0700 | [diff] [blame] | 138 | } |
Winson Chung | 17f1bb8 | 2012-05-25 14:25:44 -0700 | [diff] [blame] | 139 | setupAnimation(mDropTargetBarAnim, mDropTargetBar); |
Winson Chung | 201bc82 | 2011-06-20 15:41:53 -0700 | [diff] [blame] | 140 | } |
| 141 | |
Winson Chung | 043f2af | 2012-03-01 16:09:54 -0800 | [diff] [blame] | 142 | public void finishAnimations() { |
Winson Chung | 17f1bb8 | 2012-05-25 14:25:44 -0700 | [diff] [blame] | 143 | prepareStartAnimation(mDropTargetBar); |
| 144 | mDropTargetBarAnim.reverse(); |
| 145 | prepareStartAnimation(mQSBSearchBar); |
| 146 | mQSBSearchBarAnim.reverse(); |
Winson Chung | 4c98d92 | 2011-05-31 16:50:48 -0700 | [diff] [blame] | 147 | } |
| 148 | |
| 149 | /* |
Winson Chung | f0ea4d3 | 2011-06-06 14:27:16 -0700 | [diff] [blame] | 150 | * Shows and hides the search bar. |
| 151 | */ |
| 152 | public void showSearchBar(boolean animated) { |
Mac Duy Hai | 8246a14 | 2013-10-16 13:46:04 +0100 | [diff] [blame] | 153 | boolean needToCancelOngoingAnimation = mQSBSearchBarAnim.isRunning() && !animated; |
| 154 | if (!mIsSearchBarHidden && !needToCancelOngoingAnimation) return; |
Winson Chung | f0ea4d3 | 2011-06-06 14:27:16 -0700 | [diff] [blame] | 155 | if (animated) { |
Winson Chung | c7d2b60 | 2012-05-16 17:02:20 -0700 | [diff] [blame] | 156 | prepareStartAnimation(mQSBSearchBar); |
Winson Chung | 17f1bb8 | 2012-05-25 14:25:44 -0700 | [diff] [blame] | 157 | mQSBSearchBarAnim.reverse(); |
Winson Chung | f0ea4d3 | 2011-06-06 14:27:16 -0700 | [diff] [blame] | 158 | } else { |
Winson Chung | 17f1bb8 | 2012-05-25 14:25:44 -0700 | [diff] [blame] | 159 | mQSBSearchBarAnim.cancel(); |
Adam Cohen | c2d6e89 | 2014-10-16 09:49:24 -0700 | [diff] [blame] | 160 | if (mQSBSearchBar != null && mEnableDropDownDropTargets) { |
Michael Jurka | 19e3347 | 2012-05-14 20:41:52 -0700 | [diff] [blame] | 161 | mQSBSearchBar.setTranslationY(0); |
Adam Cohen | c2d6e89 | 2014-10-16 09:49:24 -0700 | [diff] [blame] | 162 | } else if (mQSBSearchBar != null) { |
Michael Jurka | 324dbdd | 2012-06-18 14:31:28 -0700 | [diff] [blame] | 163 | mQSBSearchBar.setAlpha(1f); |
Michael Jurka | 19e3347 | 2012-05-14 20:41:52 -0700 | [diff] [blame] | 164 | } |
Winson Chung | f0ea4d3 | 2011-06-06 14:27:16 -0700 | [diff] [blame] | 165 | } |
| 166 | mIsSearchBarHidden = false; |
| 167 | } |
| 168 | public void hideSearchBar(boolean animated) { |
Mac Duy Hai | 8246a14 | 2013-10-16 13:46:04 +0100 | [diff] [blame] | 169 | boolean needToCancelOngoingAnimation = mQSBSearchBarAnim.isRunning() && !animated; |
| 170 | if (mIsSearchBarHidden && !needToCancelOngoingAnimation) return; |
Winson Chung | f0ea4d3 | 2011-06-06 14:27:16 -0700 | [diff] [blame] | 171 | if (animated) { |
Winson Chung | c7d2b60 | 2012-05-16 17:02:20 -0700 | [diff] [blame] | 172 | prepareStartAnimation(mQSBSearchBar); |
Winson Chung | 17f1bb8 | 2012-05-25 14:25:44 -0700 | [diff] [blame] | 173 | mQSBSearchBarAnim.start(); |
Winson Chung | f0ea4d3 | 2011-06-06 14:27:16 -0700 | [diff] [blame] | 174 | } else { |
Winson Chung | 17f1bb8 | 2012-05-25 14:25:44 -0700 | [diff] [blame] | 175 | mQSBSearchBarAnim.cancel(); |
Adam Cohen | c2d6e89 | 2014-10-16 09:49:24 -0700 | [diff] [blame] | 176 | if (mQSBSearchBar != null && mEnableDropDownDropTargets) { |
Winson Chung | 17f1bb8 | 2012-05-25 14:25:44 -0700 | [diff] [blame] | 177 | mQSBSearchBar.setTranslationY(-mBarHeight); |
Adam Cohen | c2d6e89 | 2014-10-16 09:49:24 -0700 | [diff] [blame] | 178 | } else if (mQSBSearchBar != null) { |
Michael Jurka | 324dbdd | 2012-06-18 14:31:28 -0700 | [diff] [blame] | 179 | mQSBSearchBar.setAlpha(0f); |
Michael Jurka | 19e3347 | 2012-05-14 20:41:52 -0700 | [diff] [blame] | 180 | } |
Winson Chung | f0ea4d3 | 2011-06-06 14:27:16 -0700 | [diff] [blame] | 181 | } |
| 182 | mIsSearchBarHidden = true; |
| 183 | } |
| 184 | |
| 185 | /* |
| 186 | * Gets various transition durations. |
| 187 | */ |
| 188 | public int getTransitionInDuration() { |
| 189 | return sTransitionInDuration; |
| 190 | } |
| 191 | public int getTransitionOutDuration() { |
| 192 | return sTransitionOutDuration; |
| 193 | } |
| 194 | |
| 195 | /* |
Winson Chung | 4c98d92 | 2011-05-31 16:50:48 -0700 | [diff] [blame] | 196 | * DragController.DragListener implementation |
| 197 | */ |
| 198 | @Override |
| 199 | public void onDragStart(DragSource source, Object info, int dragAction) { |
Adam Cohen | c9735cf | 2015-01-23 16:11:55 -0800 | [diff] [blame^] | 200 | showDeleteTarget(); |
| 201 | } |
| 202 | |
| 203 | public void showDeleteTarget() { |
Winson Chung | 4c98d92 | 2011-05-31 16:50:48 -0700 | [diff] [blame] | 204 | // Animate out the QSB search bar, and animate in the drop target bar |
Winson Chung | c7d2b60 | 2012-05-16 17:02:20 -0700 | [diff] [blame] | 205 | prepareStartAnimation(mDropTargetBar); |
Winson Chung | 17f1bb8 | 2012-05-25 14:25:44 -0700 | [diff] [blame] | 206 | mDropTargetBarAnim.start(); |
Winson Chung | f0ea4d3 | 2011-06-06 14:27:16 -0700 | [diff] [blame] | 207 | if (!mIsSearchBarHidden) { |
Winson Chung | c7d2b60 | 2012-05-16 17:02:20 -0700 | [diff] [blame] | 208 | prepareStartAnimation(mQSBSearchBar); |
Winson Chung | 17f1bb8 | 2012-05-25 14:25:44 -0700 | [diff] [blame] | 209 | mQSBSearchBarAnim.start(); |
Winson Chung | f0ea4d3 | 2011-06-06 14:27:16 -0700 | [diff] [blame] | 210 | } |
Winson Chung | 4c98d92 | 2011-05-31 16:50:48 -0700 | [diff] [blame] | 211 | } |
| 212 | |
Adam Cohen | c9735cf | 2015-01-23 16:11:55 -0800 | [diff] [blame^] | 213 | public void hideDeleteTarget() { |
| 214 | // Restore the QSB search bar, and animate out the drop target bar |
| 215 | prepareStartAnimation(mDropTargetBar); |
| 216 | mDropTargetBarAnim.reverse(); |
| 217 | if (!mIsSearchBarHidden) { |
| 218 | prepareStartAnimation(mQSBSearchBar); |
| 219 | mQSBSearchBarAnim.reverse(); |
| 220 | } |
| 221 | } |
| 222 | |
Adam Cohen | d4d7aa5 | 2011-07-19 21:47:37 -0700 | [diff] [blame] | 223 | public void deferOnDragEnd() { |
| 224 | mDeferOnDragEnd = true; |
| 225 | } |
| 226 | |
Winson Chung | 4c98d92 | 2011-05-31 16:50:48 -0700 | [diff] [blame] | 227 | @Override |
| 228 | public void onDragEnd() { |
Adam Cohen | d4d7aa5 | 2011-07-19 21:47:37 -0700 | [diff] [blame] | 229 | if (!mDeferOnDragEnd) { |
Adam Cohen | c9735cf | 2015-01-23 16:11:55 -0800 | [diff] [blame^] | 230 | hideDeleteTarget(); |
Adam Cohen | d4d7aa5 | 2011-07-19 21:47:37 -0700 | [diff] [blame] | 231 | } else { |
| 232 | mDeferOnDragEnd = false; |
Winson Chung | f0ea4d3 | 2011-06-06 14:27:16 -0700 | [diff] [blame] | 233 | } |
Winson Chung | 4c98d92 | 2011-05-31 16:50:48 -0700 | [diff] [blame] | 234 | } |
Winson Chung | c51db6a | 2011-10-05 11:44:49 -0700 | [diff] [blame] | 235 | |
Mathew Inwood | cf7f63b | 2011-10-13 11:31:38 +0100 | [diff] [blame] | 236 | public Rect getSearchBarBounds() { |
| 237 | if (mQSBSearchBar != null) { |
Mathew Inwood | cf7f63b | 2011-10-13 11:31:38 +0100 | [diff] [blame] | 238 | final int[] pos = new int[2]; |
| 239 | mQSBSearchBar.getLocationOnScreen(pos); |
| 240 | |
| 241 | final Rect rect = new Rect(); |
Michael Jurka | 629758f | 2012-06-14 16:18:21 -0700 | [diff] [blame] | 242 | rect.left = pos[0]; |
| 243 | rect.top = pos[1]; |
| 244 | rect.right = pos[0] + mQSBSearchBar.getWidth(); |
| 245 | rect.bottom = pos[1] + mQSBSearchBar.getHeight(); |
Mathew Inwood | cf7f63b | 2011-10-13 11:31:38 +0100 | [diff] [blame] | 246 | return rect; |
| 247 | } else { |
| 248 | return null; |
| 249 | } |
| 250 | } |
Winson Chung | 4c98d92 | 2011-05-31 16:50:48 -0700 | [diff] [blame] | 251 | } |