blob: 435dbda1d5f3ac9232de3ab70d6cc08d8a33ea20 [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;
Winson Chung4c98d922011-05-31 16:50:48 -070022import android.content.Context;
Mathew Inwoodcf7f63b2011-10-13 11:31:38 +010023import android.graphics.Rect;
Winson Chungc51db6a2011-10-05 11:44:49 -070024import android.graphics.drawable.Drawable;
Winson Chung4c98d922011-05-31 16:50:48 -070025import android.util.AttributeSet;
26import android.view.View;
Winson Chunga62e9fd2011-07-11 15:20:48 -070027import android.view.animation.AccelerateInterpolator;
Winson Chung4c98d922011-05-31 16:50:48 -070028import android.widget.FrameLayout;
29
Winson Chung4c98d922011-05-31 16:50:48 -070030/*
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 */
34public class SearchDropTargetBar extends FrameLayout implements DragController.DragListener {
35
Winson Chunga62e9fd2011-07-11 15:20:48 -070036 private static final int sTransitionInDuration = 200;
37 private static final int sTransitionOutDuration = 175;
Winson Chung4c98d922011-05-31 16:50:48 -070038
Winson Chung17f1bb82012-05-25 14:25:44 -070039 private ObjectAnimator mDropTargetBarAnim;
40 private ObjectAnimator mQSBSearchBarAnim;
41 private static final AccelerateInterpolator sAccelerateInterpolator =
42 new AccelerateInterpolator();
Winson Chung201bc822011-06-20 15:41:53 -070043
Winson Chungf0ea4d32011-06-06 14:27:16 -070044 private boolean mIsSearchBarHidden;
Winson Chung4c98d922011-05-31 16:50:48 -070045 private View mQSBSearchBar;
46 private View mDropTargetBar;
Winson Chung61fa4192011-06-12 15:15:29 -070047 private ButtonDropTarget mInfoDropTarget;
48 private ButtonDropTarget mDeleteDropTarget;
Winson Chunga62e9fd2011-07-11 15:20:48 -070049 private int mBarHeight;
Adam Cohend4d7aa52011-07-19 21:47:37 -070050 private boolean mDeferOnDragEnd = false;
Winson Chung4c98d922011-05-31 16:50:48 -070051
Winson Chungc51db6a2011-10-05 11:44:49 -070052 private Drawable mPreviousBackground;
Michael Jurka19e33472012-05-14 20:41:52 -070053 private boolean mEnableDropDownDropTargets;
Winson Chungc51db6a2011-10-05 11:44:49 -070054
Winson Chung4c98d922011-05-31 16:50:48 -070055 public SearchDropTargetBar(Context context, AttributeSet attrs) {
56 this(context, attrs, 0);
57 }
58
59 public SearchDropTargetBar(Context context, AttributeSet attrs, int defStyle) {
60 super(context, attrs, defStyle);
61 }
62
63 public void setup(Launcher launcher, DragController dragController) {
64 dragController.addDragListener(this);
65 dragController.addDragListener(mInfoDropTarget);
66 dragController.addDragListener(mDeleteDropTarget);
67 dragController.addDropTarget(mInfoDropTarget);
68 dragController.addDropTarget(mDeleteDropTarget);
Winson Chung043f2af2012-03-01 16:09:54 -080069 dragController.setFlingToDeleteDropTarget(mDeleteDropTarget);
Winson Chung4c98d922011-05-31 16:50:48 -070070 mInfoDropTarget.setLauncher(launcher);
71 mDeleteDropTarget.setLauncher(launcher);
Cristina Stancu476493b2013-08-07 17:20:14 +010072 mQSBSearchBar = launcher.getQsbBar();
73 if (mEnableDropDownDropTargets) {
Michael Jurka66b7d012013-11-06 16:37:34 +010074 mQSBSearchBarAnim = LauncherAnimUtils.ofFloat(mQSBSearchBar, "translationY", 0,
Cristina Stancu476493b2013-08-07 17:20:14 +010075 -mBarHeight);
76 } else {
Michael Jurka66b7d012013-11-06 16:37:34 +010077 mQSBSearchBarAnim = LauncherAnimUtils.ofFloat(mQSBSearchBar, "alpha", 1f, 0f);
Cristina Stancu476493b2013-08-07 17:20:14 +010078 }
79 setupAnimation(mQSBSearchBarAnim, mQSBSearchBar);
Winson Chung4c98d922011-05-31 16:50:48 -070080 }
81
Winson Chungc7d2b602012-05-16 17:02:20 -070082 private void prepareStartAnimation(View v) {
Winson Chung17f1bb82012-05-25 14:25:44 -070083 // Enable the hw layers before the animation starts (will be disabled in the onAnimationEnd
84 // callback below)
Winson Chungc7d2b602012-05-16 17:02:20 -070085 v.setLayerType(View.LAYER_TYPE_HARDWARE, null);
Winson Chungc7d2b602012-05-16 17:02:20 -070086 }
87
Winson Chung17f1bb82012-05-25 14:25:44 -070088 private void setupAnimation(ObjectAnimator anim, final View v) {
89 anim.setInterpolator(sAccelerateInterpolator);
90 anim.setDuration(sTransitionInDuration);
91 anim.addListener(new AnimatorListenerAdapter() {
Winson Chung315b3ba2012-05-11 10:51:32 -070092 @Override
93 public void onAnimationEnd(Animator animation) {
Winson Chung315b3ba2012-05-11 10:51:32 -070094 v.setLayerType(View.LAYER_TYPE_NONE, null);
95 }
96 });
97 }
98
Winson Chung4c98d922011-05-31 16:50:48 -070099 @Override
100 protected void onFinishInflate() {
101 super.onFinishInflate();
102
103 // Get the individual components
Winson Chung4c98d922011-05-31 16:50:48 -0700104 mDropTargetBar = findViewById(R.id.drag_target_bar);
Winson Chunga6427b12011-07-27 10:53:39 -0700105 mInfoDropTarget = (ButtonDropTarget) mDropTargetBar.findViewById(R.id.info_target_text);
106 mDeleteDropTarget = (ButtonDropTarget) mDropTargetBar.findViewById(R.id.delete_target_text);
Winson Chunga62e9fd2011-07-11 15:20:48 -0700107
Adam Cohend4d7aa52011-07-19 21:47:37 -0700108 mInfoDropTarget.setSearchDropTargetBar(this);
109 mDeleteDropTarget.setSearchDropTargetBar(this);
110
Michael Jurka19e33472012-05-14 20:41:52 -0700111 mEnableDropDownDropTargets =
Winson Chunga62e9fd2011-07-11 15:20:48 -0700112 getResources().getBoolean(R.bool.config_useDropTargetDownTransition);
Winson Chung201bc822011-06-20 15:41:53 -0700113
114 // Create the various fade animations
Michael Jurka19e33472012-05-14 20:41:52 -0700115 if (mEnableDropDownDropTargets) {
Winson Chung5f8afe62013-08-12 16:19:28 -0700116 LauncherAppState app = LauncherAppState.getInstance();
117 DeviceProfile grid = app.getDynamicGrid().getDeviceProfile();
118 mBarHeight = grid.searchBarSpaceHeightPx;
Winson Chunga62e9fd2011-07-11 15:20:48 -0700119 mDropTargetBar.setTranslationY(-mBarHeight);
Michael Jurka66b7d012013-11-06 16:37:34 +0100120 mDropTargetBarAnim = LauncherAnimUtils.ofFloat(mDropTargetBar, "translationY",
Winson Chung17f1bb82012-05-25 14:25:44 -0700121 -mBarHeight, 0f);
Cristina Stancu476493b2013-08-07 17:20:14 +0100122
Winson Chung315b3ba2012-05-11 10:51:32 -0700123 } else {
124 mDropTargetBar.setAlpha(0f);
Michael Jurka66b7d012013-11-06 16:37:34 +0100125 mDropTargetBarAnim = LauncherAnimUtils.ofFloat(mDropTargetBar, "alpha", 0f, 1f);
Winson Chunga62e9fd2011-07-11 15:20:48 -0700126 }
Winson Chung17f1bb82012-05-25 14:25:44 -0700127 setupAnimation(mDropTargetBarAnim, mDropTargetBar);
Winson Chung201bc822011-06-20 15:41:53 -0700128 }
129
Winson Chung043f2af2012-03-01 16:09:54 -0800130 public void finishAnimations() {
Winson Chung17f1bb82012-05-25 14:25:44 -0700131 prepareStartAnimation(mDropTargetBar);
132 mDropTargetBarAnim.reverse();
133 prepareStartAnimation(mQSBSearchBar);
134 mQSBSearchBarAnim.reverse();
Winson Chung4c98d922011-05-31 16:50:48 -0700135 }
136
137 /*
Winson Chungf0ea4d32011-06-06 14:27:16 -0700138 * Shows and hides the search bar.
139 */
140 public void showSearchBar(boolean animated) {
Mac Duy Hai8246a142013-10-16 13:46:04 +0100141 boolean needToCancelOngoingAnimation = mQSBSearchBarAnim.isRunning() && !animated;
142 if (!mIsSearchBarHidden && !needToCancelOngoingAnimation) return;
Winson Chungf0ea4d32011-06-06 14:27:16 -0700143 if (animated) {
Winson Chungc7d2b602012-05-16 17:02:20 -0700144 prepareStartAnimation(mQSBSearchBar);
Winson Chung17f1bb82012-05-25 14:25:44 -0700145 mQSBSearchBarAnim.reverse();
Winson Chungf0ea4d32011-06-06 14:27:16 -0700146 } else {
Winson Chung17f1bb82012-05-25 14:25:44 -0700147 mQSBSearchBarAnim.cancel();
Michael Jurka19e33472012-05-14 20:41:52 -0700148 if (mEnableDropDownDropTargets) {
149 mQSBSearchBar.setTranslationY(0);
150 } else {
Michael Jurka324dbdd2012-06-18 14:31:28 -0700151 mQSBSearchBar.setAlpha(1f);
Michael Jurka19e33472012-05-14 20:41:52 -0700152 }
Winson Chungf0ea4d32011-06-06 14:27:16 -0700153 }
154 mIsSearchBarHidden = false;
155 }
156 public void hideSearchBar(boolean animated) {
Mac Duy Hai8246a142013-10-16 13:46:04 +0100157 boolean needToCancelOngoingAnimation = mQSBSearchBarAnim.isRunning() && !animated;
158 if (mIsSearchBarHidden && !needToCancelOngoingAnimation) return;
Winson Chungf0ea4d32011-06-06 14:27:16 -0700159 if (animated) {
Winson Chungc7d2b602012-05-16 17:02:20 -0700160 prepareStartAnimation(mQSBSearchBar);
Winson Chung17f1bb82012-05-25 14:25:44 -0700161 mQSBSearchBarAnim.start();
Winson Chungf0ea4d32011-06-06 14:27:16 -0700162 } else {
Winson Chung17f1bb82012-05-25 14:25:44 -0700163 mQSBSearchBarAnim.cancel();
Michael Jurka19e33472012-05-14 20:41:52 -0700164 if (mEnableDropDownDropTargets) {
Winson Chung17f1bb82012-05-25 14:25:44 -0700165 mQSBSearchBar.setTranslationY(-mBarHeight);
Michael Jurka19e33472012-05-14 20:41:52 -0700166 } else {
Michael Jurka324dbdd2012-06-18 14:31:28 -0700167 mQSBSearchBar.setAlpha(0f);
Michael Jurka19e33472012-05-14 20:41:52 -0700168 }
Winson Chungf0ea4d32011-06-06 14:27:16 -0700169 }
170 mIsSearchBarHidden = true;
171 }
172
173 /*
174 * Gets various transition durations.
175 */
176 public int getTransitionInDuration() {
177 return sTransitionInDuration;
178 }
179 public int getTransitionOutDuration() {
180 return sTransitionOutDuration;
181 }
182
183 /*
Winson Chung4c98d922011-05-31 16:50:48 -0700184 * DragController.DragListener implementation
185 */
186 @Override
187 public void onDragStart(DragSource source, Object info, int dragAction) {
188 // Animate out the QSB search bar, and animate in the drop target bar
Winson Chungc7d2b602012-05-16 17:02:20 -0700189 prepareStartAnimation(mDropTargetBar);
Winson Chung17f1bb82012-05-25 14:25:44 -0700190 mDropTargetBarAnim.start();
Winson Chungf0ea4d32011-06-06 14:27:16 -0700191 if (!mIsSearchBarHidden) {
Winson Chungc7d2b602012-05-16 17:02:20 -0700192 prepareStartAnimation(mQSBSearchBar);
Winson Chung17f1bb82012-05-25 14:25:44 -0700193 mQSBSearchBarAnim.start();
Winson Chungf0ea4d32011-06-06 14:27:16 -0700194 }
Winson Chung4c98d922011-05-31 16:50:48 -0700195 }
196
Adam Cohend4d7aa52011-07-19 21:47:37 -0700197 public void deferOnDragEnd() {
198 mDeferOnDragEnd = true;
199 }
200
Winson Chung4c98d922011-05-31 16:50:48 -0700201 @Override
202 public void onDragEnd() {
Adam Cohend4d7aa52011-07-19 21:47:37 -0700203 if (!mDeferOnDragEnd) {
204 // Restore the QSB search bar, and animate out the drop target bar
Winson Chungc7d2b602012-05-16 17:02:20 -0700205 prepareStartAnimation(mDropTargetBar);
Winson Chung17f1bb82012-05-25 14:25:44 -0700206 mDropTargetBarAnim.reverse();
Adam Cohend4d7aa52011-07-19 21:47:37 -0700207 if (!mIsSearchBarHidden) {
Winson Chungc7d2b602012-05-16 17:02:20 -0700208 prepareStartAnimation(mQSBSearchBar);
Winson Chung17f1bb82012-05-25 14:25:44 -0700209 mQSBSearchBarAnim.reverse();
Adam Cohend4d7aa52011-07-19 21:47:37 -0700210 }
211 } else {
212 mDeferOnDragEnd = false;
Winson Chungf0ea4d32011-06-06 14:27:16 -0700213 }
Winson Chung4c98d922011-05-31 16:50:48 -0700214 }
Winson Chungc51db6a2011-10-05 11:44:49 -0700215
216 public void onSearchPackagesChanged(boolean searchVisible, boolean voiceVisible) {
217 if (mQSBSearchBar != null) {
218 Drawable bg = mQSBSearchBar.getBackground();
219 if (bg != null && (!searchVisible && !voiceVisible)) {
220 // Save the background and disable it
221 mPreviousBackground = bg;
222 mQSBSearchBar.setBackgroundResource(0);
223 } else if (mPreviousBackground != null && (searchVisible || voiceVisible)) {
224 // Restore the background
Michael Jurka3a9fced2012-04-13 14:44:29 -0700225 mQSBSearchBar.setBackground(mPreviousBackground);
Winson Chungc51db6a2011-10-05 11:44:49 -0700226 }
227 }
228 }
Mathew Inwoodcf7f63b2011-10-13 11:31:38 +0100229
230 public Rect getSearchBarBounds() {
231 if (mQSBSearchBar != null) {
Mathew Inwoodcf7f63b2011-10-13 11:31:38 +0100232 final int[] pos = new int[2];
233 mQSBSearchBar.getLocationOnScreen(pos);
234
235 final Rect rect = new Rect();
Michael Jurka629758f2012-06-14 16:18:21 -0700236 rect.left = pos[0];
237 rect.top = pos[1];
238 rect.right = pos[0] + mQSBSearchBar.getWidth();
239 rect.bottom = pos[1] + mQSBSearchBar.getHeight();
Mathew Inwoodcf7f63b2011-10-13 11:31:38 +0100240 return rect;
241 } else {
242 return null;
243 }
244 }
Winson Chung4c98d922011-05-31 16:50:48 -0700245}