blob: 6205088c55adabaaa0d036189f6976ed601eaee5 [file] [log] [blame]
Winson Chung4c98d922011-05-31 16:50:48 -07001/*
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 Sandler325dc232013-06-05 22:57:57 -040017package com.android.launcher3;
Winson Chung4c98d922011-05-31 16:50:48 -070018
Winson Chung201bc822011-06-20 15:41:53 -070019import android.animation.Animator;
20import android.animation.AnimatorListenerAdapter;
21import android.animation.ObjectAnimator;
Adam Cohenc2d6e892014-10-16 09:49:24 -070022import android.animation.ValueAnimator;
Winson Chung4c98d922011-05-31 16:50:48 -070023import android.content.Context;
Mathew Inwoodcf7f63b2011-10-13 11:31:38 +010024import android.graphics.Rect;
Winson Chungc51db6a2011-10-05 11:44:49 -070025import android.graphics.drawable.Drawable;
Winson Chung4c98d922011-05-31 16:50:48 -070026import android.util.AttributeSet;
27import android.view.View;
Winson Chunga62e9fd2011-07-11 15:20:48 -070028import android.view.animation.AccelerateInterpolator;
Winson Chung4c98d922011-05-31 16:50:48 -070029import android.widget.FrameLayout;
30
Winson Chung4c98d922011-05-31 16:50:48 -070031/*
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 */
35public class SearchDropTargetBar extends FrameLayout implements DragController.DragListener {
36
Winson Chunga62e9fd2011-07-11 15:20:48 -070037 private static final int sTransitionInDuration = 200;
38 private static final int sTransitionOutDuration = 175;
Winson Chung4c98d922011-05-31 16:50:48 -070039
Winson Chung17f1bb82012-05-25 14:25:44 -070040 private ObjectAnimator mDropTargetBarAnim;
Adam Cohenc2d6e892014-10-16 09:49:24 -070041 private ValueAnimator mQSBSearchBarAnim;
Winson Chung17f1bb82012-05-25 14:25:44 -070042 private static final AccelerateInterpolator sAccelerateInterpolator =
43 new AccelerateInterpolator();
Winson Chung201bc822011-06-20 15:41:53 -070044
Winson Chungf0ea4d32011-06-06 14:27:16 -070045 private boolean mIsSearchBarHidden;
Winson Chung4c98d922011-05-31 16:50:48 -070046 private View mQSBSearchBar;
47 private View mDropTargetBar;
Winson Chung61fa4192011-06-12 15:15:29 -070048 private ButtonDropTarget mInfoDropTarget;
49 private ButtonDropTarget mDeleteDropTarget;
Winson Chunga62e9fd2011-07-11 15:20:48 -070050 private int mBarHeight;
Adam Cohend4d7aa52011-07-19 21:47:37 -070051 private boolean mDeferOnDragEnd = false;
Winson Chung4c98d922011-05-31 16:50:48 -070052
Winson Chungc51db6a2011-10-05 11:44:49 -070053 private Drawable mPreviousBackground;
Michael Jurka19e33472012-05-14 20:41:52 -070054 private boolean mEnableDropDownDropTargets;
Winson Chungc51db6a2011-10-05 11:44:49 -070055
Winson Chung4c98d922011-05-31 16:50:48 -070056 public SearchDropTargetBar(Context context, AttributeSet attrs) {
57 this(context, attrs, 0);
58 }
59
60 public SearchDropTargetBar(Context context, AttributeSet attrs, int defStyle) {
61 super(context, attrs, defStyle);
62 }
63
64 public void setup(Launcher launcher, DragController dragController) {
65 dragController.addDragListener(this);
66 dragController.addDragListener(mInfoDropTarget);
67 dragController.addDragListener(mDeleteDropTarget);
68 dragController.addDropTarget(mInfoDropTarget);
69 dragController.addDropTarget(mDeleteDropTarget);
Winson Chung043f2af2012-03-01 16:09:54 -080070 dragController.setFlingToDeleteDropTarget(mDeleteDropTarget);
Winson Chung4c98d922011-05-31 16:50:48 -070071 mInfoDropTarget.setLauncher(launcher);
72 mDeleteDropTarget.setLauncher(launcher);
Cristina Stancu476493b2013-08-07 17:20:14 +010073 mQSBSearchBar = launcher.getQsbBar();
Adam Cohenc2d6e892014-10-16 09:49:24 -070074 if (mQSBSearchBar != null) {
75 if (mEnableDropDownDropTargets) {
76 mQSBSearchBarAnim = LauncherAnimUtils.ofFloat(mQSBSearchBar, "translationY", 0,
77 -mBarHeight);
78 } else {
79 mQSBSearchBarAnim = LauncherAnimUtils.ofFloat(mQSBSearchBar, "alpha", 1f, 0f);
80 }
81 setupAnimation(mQSBSearchBarAnim, mQSBSearchBar);
Cristina Stancu476493b2013-08-07 17:20:14 +010082 } else {
Adam Cohenc2d6e892014-10-16 09:49:24 -070083 // Create a no-op animation of the search bar is null
84 mQSBSearchBarAnim = ValueAnimator.ofFloat(0, 0);
85 mQSBSearchBarAnim.setDuration(sTransitionInDuration);
Cristina Stancu476493b2013-08-07 17:20:14 +010086 }
Winson Chung4c98d922011-05-31 16:50:48 -070087 }
88
Winson Chungc7d2b602012-05-16 17:02:20 -070089 private void prepareStartAnimation(View v) {
Winson Chung17f1bb82012-05-25 14:25:44 -070090 // Enable the hw layers before the animation starts (will be disabled in the onAnimationEnd
91 // callback below)
Adam Cohenc2d6e892014-10-16 09:49:24 -070092 if (v != null) {
93 v.setLayerType(View.LAYER_TYPE_HARDWARE, null);
94 }
Winson Chungc7d2b602012-05-16 17:02:20 -070095 }
96
Adam Cohenc2d6e892014-10-16 09:49:24 -070097 private void setupAnimation(ValueAnimator anim, final View v) {
Winson Chung17f1bb82012-05-25 14:25:44 -070098 anim.setInterpolator(sAccelerateInterpolator);
99 anim.setDuration(sTransitionInDuration);
100 anim.addListener(new AnimatorListenerAdapter() {
Winson Chung315b3ba2012-05-11 10:51:32 -0700101 @Override
102 public void onAnimationEnd(Animator animation) {
Adam Cohenc2d6e892014-10-16 09:49:24 -0700103 if (v != null) {
104 v.setLayerType(View.LAYER_TYPE_NONE, null);
105 }
Winson Chung315b3ba2012-05-11 10:51:32 -0700106 }
107 });
108 }
109
Winson Chung4c98d922011-05-31 16:50:48 -0700110 @Override
111 protected void onFinishInflate() {
112 super.onFinishInflate();
113
114 // Get the individual components
Winson Chung4c98d922011-05-31 16:50:48 -0700115 mDropTargetBar = findViewById(R.id.drag_target_bar);
Winson Chunga6427b12011-07-27 10:53:39 -0700116 mInfoDropTarget = (ButtonDropTarget) mDropTargetBar.findViewById(R.id.info_target_text);
117 mDeleteDropTarget = (ButtonDropTarget) mDropTargetBar.findViewById(R.id.delete_target_text);
Winson Chunga62e9fd2011-07-11 15:20:48 -0700118
Adam Cohend4d7aa52011-07-19 21:47:37 -0700119 mInfoDropTarget.setSearchDropTargetBar(this);
120 mDeleteDropTarget.setSearchDropTargetBar(this);
121
Michael Jurka19e33472012-05-14 20:41:52 -0700122 mEnableDropDownDropTargets =
Winson Chunga62e9fd2011-07-11 15:20:48 -0700123 getResources().getBoolean(R.bool.config_useDropTargetDownTransition);
Winson Chung201bc822011-06-20 15:41:53 -0700124
125 // Create the various fade animations
Michael Jurka19e33472012-05-14 20:41:52 -0700126 if (mEnableDropDownDropTargets) {
Winson Chung5f8afe62013-08-12 16:19:28 -0700127 LauncherAppState app = LauncherAppState.getInstance();
128 DeviceProfile grid = app.getDynamicGrid().getDeviceProfile();
129 mBarHeight = grid.searchBarSpaceHeightPx;
Winson Chunga62e9fd2011-07-11 15:20:48 -0700130 mDropTargetBar.setTranslationY(-mBarHeight);
Michael Jurka66b7d012013-11-06 16:37:34 +0100131 mDropTargetBarAnim = LauncherAnimUtils.ofFloat(mDropTargetBar, "translationY",
Winson Chung17f1bb82012-05-25 14:25:44 -0700132 -mBarHeight, 0f);
Cristina Stancu476493b2013-08-07 17:20:14 +0100133
Winson Chung315b3ba2012-05-11 10:51:32 -0700134 } else {
135 mDropTargetBar.setAlpha(0f);
Michael Jurka66b7d012013-11-06 16:37:34 +0100136 mDropTargetBarAnim = LauncherAnimUtils.ofFloat(mDropTargetBar, "alpha", 0f, 1f);
Winson Chunga62e9fd2011-07-11 15:20:48 -0700137 }
Winson Chung17f1bb82012-05-25 14:25:44 -0700138 setupAnimation(mDropTargetBarAnim, mDropTargetBar);
Winson Chung201bc822011-06-20 15:41:53 -0700139 }
140
Winson Chung043f2af2012-03-01 16:09:54 -0800141 public void finishAnimations() {
Winson Chung17f1bb82012-05-25 14:25:44 -0700142 prepareStartAnimation(mDropTargetBar);
143 mDropTargetBarAnim.reverse();
144 prepareStartAnimation(mQSBSearchBar);
145 mQSBSearchBarAnim.reverse();
Winson Chung4c98d922011-05-31 16:50:48 -0700146 }
147
148 /*
Winson Chungf0ea4d32011-06-06 14:27:16 -0700149 * Shows and hides the search bar.
150 */
151 public void showSearchBar(boolean animated) {
Mac Duy Hai8246a142013-10-16 13:46:04 +0100152 boolean needToCancelOngoingAnimation = mQSBSearchBarAnim.isRunning() && !animated;
153 if (!mIsSearchBarHidden && !needToCancelOngoingAnimation) return;
Winson Chungf0ea4d32011-06-06 14:27:16 -0700154 if (animated) {
Winson Chungc7d2b602012-05-16 17:02:20 -0700155 prepareStartAnimation(mQSBSearchBar);
Winson Chung17f1bb82012-05-25 14:25:44 -0700156 mQSBSearchBarAnim.reverse();
Winson Chungf0ea4d32011-06-06 14:27:16 -0700157 } else {
Winson Chung17f1bb82012-05-25 14:25:44 -0700158 mQSBSearchBarAnim.cancel();
Adam Cohenc2d6e892014-10-16 09:49:24 -0700159 if (mQSBSearchBar != null && mEnableDropDownDropTargets) {
Michael Jurka19e33472012-05-14 20:41:52 -0700160 mQSBSearchBar.setTranslationY(0);
Adam Cohenc2d6e892014-10-16 09:49:24 -0700161 } else if (mQSBSearchBar != null) {
Michael Jurka324dbdd2012-06-18 14:31:28 -0700162 mQSBSearchBar.setAlpha(1f);
Michael Jurka19e33472012-05-14 20:41:52 -0700163 }
Winson Chungf0ea4d32011-06-06 14:27:16 -0700164 }
165 mIsSearchBarHidden = false;
166 }
167 public void hideSearchBar(boolean animated) {
Mac Duy Hai8246a142013-10-16 13:46:04 +0100168 boolean needToCancelOngoingAnimation = mQSBSearchBarAnim.isRunning() && !animated;
169 if (mIsSearchBarHidden && !needToCancelOngoingAnimation) return;
Winson Chungf0ea4d32011-06-06 14:27:16 -0700170 if (animated) {
Winson Chungc7d2b602012-05-16 17:02:20 -0700171 prepareStartAnimation(mQSBSearchBar);
Winson Chung17f1bb82012-05-25 14:25:44 -0700172 mQSBSearchBarAnim.start();
Winson Chungf0ea4d32011-06-06 14:27:16 -0700173 } else {
Winson Chung17f1bb82012-05-25 14:25:44 -0700174 mQSBSearchBarAnim.cancel();
Adam Cohenc2d6e892014-10-16 09:49:24 -0700175 if (mQSBSearchBar != null && mEnableDropDownDropTargets) {
Winson Chung17f1bb82012-05-25 14:25:44 -0700176 mQSBSearchBar.setTranslationY(-mBarHeight);
Adam Cohenc2d6e892014-10-16 09:49:24 -0700177 } else if (mQSBSearchBar != null) {
Michael Jurka324dbdd2012-06-18 14:31:28 -0700178 mQSBSearchBar.setAlpha(0f);
Michael Jurka19e33472012-05-14 20:41:52 -0700179 }
Winson Chungf0ea4d32011-06-06 14:27:16 -0700180 }
181 mIsSearchBarHidden = true;
182 }
183
184 /*
185 * Gets various transition durations.
186 */
187 public int getTransitionInDuration() {
188 return sTransitionInDuration;
189 }
190 public int getTransitionOutDuration() {
191 return sTransitionOutDuration;
192 }
193
194 /*
Winson Chung4c98d922011-05-31 16:50:48 -0700195 * DragController.DragListener implementation
196 */
197 @Override
198 public void onDragStart(DragSource source, Object info, int dragAction) {
199 // Animate out the QSB search bar, and animate in the drop target bar
Winson Chungc7d2b602012-05-16 17:02:20 -0700200 prepareStartAnimation(mDropTargetBar);
Winson Chung17f1bb82012-05-25 14:25:44 -0700201 mDropTargetBarAnim.start();
Winson Chungf0ea4d32011-06-06 14:27:16 -0700202 if (!mIsSearchBarHidden) {
Winson Chungc7d2b602012-05-16 17:02:20 -0700203 prepareStartAnimation(mQSBSearchBar);
Winson Chung17f1bb82012-05-25 14:25:44 -0700204 mQSBSearchBarAnim.start();
Winson Chungf0ea4d32011-06-06 14:27:16 -0700205 }
Winson Chung4c98d922011-05-31 16:50:48 -0700206 }
207
Adam Cohend4d7aa52011-07-19 21:47:37 -0700208 public void deferOnDragEnd() {
209 mDeferOnDragEnd = true;
210 }
211
Winson Chung4c98d922011-05-31 16:50:48 -0700212 @Override
213 public void onDragEnd() {
Adam Cohend4d7aa52011-07-19 21:47:37 -0700214 if (!mDeferOnDragEnd) {
215 // Restore the QSB search bar, and animate out the drop target bar
Winson Chungc7d2b602012-05-16 17:02:20 -0700216 prepareStartAnimation(mDropTargetBar);
Winson Chung17f1bb82012-05-25 14:25:44 -0700217 mDropTargetBarAnim.reverse();
Adam Cohend4d7aa52011-07-19 21:47:37 -0700218 if (!mIsSearchBarHidden) {
Winson Chungc7d2b602012-05-16 17:02:20 -0700219 prepareStartAnimation(mQSBSearchBar);
Winson Chung17f1bb82012-05-25 14:25:44 -0700220 mQSBSearchBarAnim.reverse();
Adam Cohend4d7aa52011-07-19 21:47:37 -0700221 }
222 } else {
223 mDeferOnDragEnd = false;
Winson Chungf0ea4d32011-06-06 14:27:16 -0700224 }
Winson Chung4c98d922011-05-31 16:50:48 -0700225 }
Winson Chungc51db6a2011-10-05 11:44:49 -0700226
227 public void onSearchPackagesChanged(boolean searchVisible, boolean voiceVisible) {
228 if (mQSBSearchBar != null) {
229 Drawable bg = mQSBSearchBar.getBackground();
230 if (bg != null && (!searchVisible && !voiceVisible)) {
231 // Save the background and disable it
232 mPreviousBackground = bg;
233 mQSBSearchBar.setBackgroundResource(0);
234 } else if (mPreviousBackground != null && (searchVisible || voiceVisible)) {
235 // Restore the background
Michael Jurka3a9fced2012-04-13 14:44:29 -0700236 mQSBSearchBar.setBackground(mPreviousBackground);
Winson Chungc51db6a2011-10-05 11:44:49 -0700237 }
238 }
239 }
Mathew Inwoodcf7f63b2011-10-13 11:31:38 +0100240
241 public Rect getSearchBarBounds() {
242 if (mQSBSearchBar != null) {
Mathew Inwoodcf7f63b2011-10-13 11:31:38 +0100243 final int[] pos = new int[2];
244 mQSBSearchBar.getLocationOnScreen(pos);
245
246 final Rect rect = new Rect();
Michael Jurka629758f2012-06-14 16:18:21 -0700247 rect.left = pos[0];
248 rect.top = pos[1];
249 rect.right = pos[0] + mQSBSearchBar.getWidth();
250 rect.bottom = pos[1] + mQSBSearchBar.getHeight();
Mathew Inwoodcf7f63b2011-10-13 11:31:38 +0100251 return rect;
252 } else {
253 return null;
254 }
255 }
Winson Chung4c98d922011-05-31 16:50:48 -0700256}