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