blob: a1d36cdfad3f1b3e2138182e251a3ed1f6ad23d6 [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
17package com.android.launcher2;
18
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
30import 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 */
36public class SearchDropTargetBar extends FrameLayout implements DragController.DragListener {
37
Winson Chunga62e9fd2011-07-11 15:20:48 -070038 private static final int sTransitionInDuration = 200;
39 private static final int sTransitionOutDuration = 175;
Winson Chung4c98d922011-05-31 16:50:48 -070040
Winson Chung17f1bb82012-05-25 14:25:44 -070041 private ObjectAnimator mDropTargetBarAnim;
42 private ObjectAnimator mQSBSearchBarAnim;
43 private static final AccelerateInterpolator sAccelerateInterpolator =
44 new AccelerateInterpolator();
Winson Chung201bc822011-06-20 15:41:53 -070045
Winson Chungf0ea4d32011-06-06 14:27:16 -070046 private boolean mIsSearchBarHidden;
Winson Chung4c98d922011-05-31 16:50:48 -070047 private View mQSBSearchBar;
48 private View mDropTargetBar;
Winson Chung61fa4192011-06-12 15:15:29 -070049 private ButtonDropTarget mInfoDropTarget;
50 private ButtonDropTarget mDeleteDropTarget;
Winson Chunga62e9fd2011-07-11 15:20:48 -070051 private int mBarHeight;
Adam Cohend4d7aa52011-07-19 21:47:37 -070052 private boolean mDeferOnDragEnd = false;
Winson Chung4c98d922011-05-31 16:50:48 -070053
Winson Chungc51db6a2011-10-05 11:44:49 -070054 private Drawable mPreviousBackground;
Michael Jurka19e33472012-05-14 20:41:52 -070055 private boolean mEnableDropDownDropTargets;
Winson Chungc51db6a2011-10-05 11:44:49 -070056
Winson Chung4c98d922011-05-31 16:50:48 -070057 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 Chung043f2af2012-03-01 16:09:54 -080071 dragController.setFlingToDeleteDropTarget(mDeleteDropTarget);
Winson Chung4c98d922011-05-31 16:50:48 -070072 mInfoDropTarget.setLauncher(launcher);
73 mDeleteDropTarget.setLauncher(launcher);
74 }
75
Winson Chungc7d2b602012-05-16 17:02:20 -070076 private void prepareStartAnimation(View v) {
Winson Chung17f1bb82012-05-25 14:25:44 -070077 // Enable the hw layers before the animation starts (will be disabled in the onAnimationEnd
78 // callback below)
Winson Chungc7d2b602012-05-16 17:02:20 -070079 v.setLayerType(View.LAYER_TYPE_HARDWARE, null);
80 v.buildLayer();
81 }
82
Winson Chung17f1bb82012-05-25 14:25:44 -070083 private void setupAnimation(ObjectAnimator anim, final View v) {
84 anim.setInterpolator(sAccelerateInterpolator);
85 anim.setDuration(sTransitionInDuration);
86 anim.addListener(new AnimatorListenerAdapter() {
Winson Chung315b3ba2012-05-11 10:51:32 -070087 @Override
88 public void onAnimationEnd(Animator animation) {
Winson Chung315b3ba2012-05-11 10:51:32 -070089 v.setLayerType(View.LAYER_TYPE_NONE, null);
90 }
91 });
92 }
93
Winson Chung4c98d922011-05-31 16:50:48 -070094 @Override
95 protected void onFinishInflate() {
96 super.onFinishInflate();
97
98 // Get the individual components
99 mQSBSearchBar = findViewById(R.id.qsb_search_bar);
100 mDropTargetBar = findViewById(R.id.drag_target_bar);
Winson Chunga6427b12011-07-27 10:53:39 -0700101 mInfoDropTarget = (ButtonDropTarget) mDropTargetBar.findViewById(R.id.info_target_text);
102 mDeleteDropTarget = (ButtonDropTarget) mDropTargetBar.findViewById(R.id.delete_target_text);
Winson Chunga62e9fd2011-07-11 15:20:48 -0700103 mBarHeight = getResources().getDimensionPixelSize(R.dimen.qsb_bar_height);
104
Adam Cohend4d7aa52011-07-19 21:47:37 -0700105 mInfoDropTarget.setSearchDropTargetBar(this);
106 mDeleteDropTarget.setSearchDropTargetBar(this);
107
Michael Jurka19e33472012-05-14 20:41:52 -0700108 mEnableDropDownDropTargets =
Winson Chunga62e9fd2011-07-11 15:20:48 -0700109 getResources().getBoolean(R.bool.config_useDropTargetDownTransition);
Winson Chung201bc822011-06-20 15:41:53 -0700110
111 // Create the various fade animations
Michael Jurka19e33472012-05-14 20:41:52 -0700112 if (mEnableDropDownDropTargets) {
Winson Chunga62e9fd2011-07-11 15:20:48 -0700113 mDropTargetBar.setTranslationY(-mBarHeight);
Winson Chung17f1bb82012-05-25 14:25:44 -0700114 mDropTargetBarAnim = ObjectAnimator.ofFloat(mDropTargetBar, "translationY",
115 -mBarHeight, 0f);
116 mQSBSearchBarAnim = ObjectAnimator.ofFloat(mQSBSearchBar, "translationY", 0,
Winson Chung315b3ba2012-05-11 10:51:32 -0700117 -mBarHeight);
118 } else {
119 mDropTargetBar.setAlpha(0f);
Winson Chung17f1bb82012-05-25 14:25:44 -0700120 mDropTargetBarAnim = ObjectAnimator.ofFloat(mDropTargetBar, "alpha", 0f, 1f);
121 mQSBSearchBarAnim = ObjectAnimator.ofFloat(mQSBSearchBar, "alpha", 1f, 0f);
Winson Chunga62e9fd2011-07-11 15:20:48 -0700122 }
Winson Chung17f1bb82012-05-25 14:25:44 -0700123 setupAnimation(mDropTargetBarAnim, mDropTargetBar);
124 setupAnimation(mQSBSearchBarAnim, mQSBSearchBar);
Winson Chung201bc822011-06-20 15:41:53 -0700125 }
126
Winson Chung043f2af2012-03-01 16:09:54 -0800127 public void finishAnimations() {
Winson Chung17f1bb82012-05-25 14:25:44 -0700128 prepareStartAnimation(mDropTargetBar);
129 mDropTargetBarAnim.reverse();
130 prepareStartAnimation(mQSBSearchBar);
131 mQSBSearchBarAnim.reverse();
Winson Chung4c98d922011-05-31 16:50:48 -0700132 }
133
134 /*
Winson Chungf0ea4d32011-06-06 14:27:16 -0700135 * Shows and hides the search bar.
136 */
137 public void showSearchBar(boolean animated) {
Winson Chung17f1bb82012-05-25 14:25:44 -0700138 if (!mIsSearchBarHidden) return;
Winson Chungf0ea4d32011-06-06 14:27:16 -0700139 if (animated) {
Winson Chungc7d2b602012-05-16 17:02:20 -0700140 prepareStartAnimation(mQSBSearchBar);
Winson Chung17f1bb82012-05-25 14:25:44 -0700141 mQSBSearchBarAnim.reverse();
Winson Chungf0ea4d32011-06-06 14:27:16 -0700142 } else {
Winson Chung17f1bb82012-05-25 14:25:44 -0700143 mQSBSearchBarAnim.cancel();
Michael Jurka19e33472012-05-14 20:41:52 -0700144 if (mEnableDropDownDropTargets) {
145 mQSBSearchBar.setTranslationY(0);
146 } else {
Michael Jurka324dbdd2012-06-18 14:31:28 -0700147 mQSBSearchBar.setAlpha(1f);
Michael Jurka19e33472012-05-14 20:41:52 -0700148 }
Winson Chungf0ea4d32011-06-06 14:27:16 -0700149 }
150 mIsSearchBarHidden = false;
151 }
152 public void hideSearchBar(boolean animated) {
Winson Chung17f1bb82012-05-25 14:25:44 -0700153 if (mIsSearchBarHidden) 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.start();
Winson Chungf0ea4d32011-06-06 14:27:16 -0700157 } else {
Winson Chung17f1bb82012-05-25 14:25:44 -0700158 mQSBSearchBarAnim.cancel();
Michael Jurka19e33472012-05-14 20:41:52 -0700159 if (mEnableDropDownDropTargets) {
Winson Chung17f1bb82012-05-25 14:25:44 -0700160 mQSBSearchBar.setTranslationY(-mBarHeight);
Michael Jurka19e33472012-05-14 20:41:52 -0700161 } else {
Michael Jurka324dbdd2012-06-18 14:31:28 -0700162 mQSBSearchBar.setAlpha(0f);
Michael Jurka19e33472012-05-14 20:41:52 -0700163 }
Winson Chungf0ea4d32011-06-06 14:27:16 -0700164 }
165 mIsSearchBarHidden = true;
166 }
167
168 /*
169 * Gets various transition durations.
170 */
171 public int getTransitionInDuration() {
172 return sTransitionInDuration;
173 }
174 public int getTransitionOutDuration() {
175 return sTransitionOutDuration;
176 }
177
178 /*
Winson Chung4c98d922011-05-31 16:50:48 -0700179 * DragController.DragListener implementation
180 */
181 @Override
182 public void onDragStart(DragSource source, Object info, int dragAction) {
183 // Animate out the QSB search bar, and animate in the drop target bar
Winson Chungc7d2b602012-05-16 17:02:20 -0700184 prepareStartAnimation(mDropTargetBar);
Winson Chung17f1bb82012-05-25 14:25:44 -0700185 mDropTargetBarAnim.start();
Winson Chungf0ea4d32011-06-06 14:27:16 -0700186 if (!mIsSearchBarHidden) {
Winson Chungc7d2b602012-05-16 17:02:20 -0700187 prepareStartAnimation(mQSBSearchBar);
Winson Chung17f1bb82012-05-25 14:25:44 -0700188 mQSBSearchBarAnim.start();
Winson Chungf0ea4d32011-06-06 14:27:16 -0700189 }
Winson Chung4c98d922011-05-31 16:50:48 -0700190 }
191
Adam Cohend4d7aa52011-07-19 21:47:37 -0700192 public void deferOnDragEnd() {
193 mDeferOnDragEnd = true;
194 }
195
Winson Chung4c98d922011-05-31 16:50:48 -0700196 @Override
197 public void onDragEnd() {
Adam Cohend4d7aa52011-07-19 21:47:37 -0700198 if (!mDeferOnDragEnd) {
199 // Restore the QSB search bar, and animate out the drop target bar
Winson Chungc7d2b602012-05-16 17:02:20 -0700200 prepareStartAnimation(mDropTargetBar);
Winson Chung17f1bb82012-05-25 14:25:44 -0700201 mDropTargetBarAnim.reverse();
Adam Cohend4d7aa52011-07-19 21:47:37 -0700202 if (!mIsSearchBarHidden) {
Winson Chungc7d2b602012-05-16 17:02:20 -0700203 prepareStartAnimation(mQSBSearchBar);
Winson Chung17f1bb82012-05-25 14:25:44 -0700204 mQSBSearchBarAnim.reverse();
Adam Cohend4d7aa52011-07-19 21:47:37 -0700205 }
206 } else {
207 mDeferOnDragEnd = false;
Winson Chungf0ea4d32011-06-06 14:27:16 -0700208 }
Winson Chung4c98d922011-05-31 16:50:48 -0700209 }
Winson Chungc51db6a2011-10-05 11:44:49 -0700210
211 public void onSearchPackagesChanged(boolean searchVisible, boolean voiceVisible) {
212 if (mQSBSearchBar != null) {
213 Drawable bg = mQSBSearchBar.getBackground();
214 if (bg != null && (!searchVisible && !voiceVisible)) {
215 // Save the background and disable it
216 mPreviousBackground = bg;
217 mQSBSearchBar.setBackgroundResource(0);
218 } else if (mPreviousBackground != null && (searchVisible || voiceVisible)) {
219 // Restore the background
Michael Jurka3a9fced2012-04-13 14:44:29 -0700220 mQSBSearchBar.setBackground(mPreviousBackground);
Winson Chungc51db6a2011-10-05 11:44:49 -0700221 }
222 }
223 }
Mathew Inwoodcf7f63b2011-10-13 11:31:38 +0100224
225 public Rect getSearchBarBounds() {
226 if (mQSBSearchBar != null) {
227 final float appScale = mQSBSearchBar.getContext().getResources()
228 .getCompatibilityInfo().applicationScale;
229 final int[] pos = new int[2];
230 mQSBSearchBar.getLocationOnScreen(pos);
231
232 final Rect rect = new Rect();
233 rect.left = (int) (pos[0] * appScale + 0.5f);
234 rect.top = (int) (pos[1] * appScale + 0.5f);
235 rect.right = (int) ((pos[0] + mQSBSearchBar.getWidth()) * appScale + 0.5f);
236 rect.bottom = (int) ((pos[1] + mQSBSearchBar.getHeight()) * appScale + 0.5f);
237 return rect;
238 } else {
239 return null;
240 }
241 }
Winson Chung4c98d922011-05-31 16:50:48 -0700242}