blob: acd3a255e32263c4d89b4f097744daa98eefa6f9 [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);
Winson Chungc7d2b602012-05-16 17:02:20 -070080 }
81
Winson Chung17f1bb82012-05-25 14:25:44 -070082 private void setupAnimation(ObjectAnimator anim, final View v) {
83 anim.setInterpolator(sAccelerateInterpolator);
84 anim.setDuration(sTransitionInDuration);
85 anim.addListener(new AnimatorListenerAdapter() {
Winson Chung315b3ba2012-05-11 10:51:32 -070086 @Override
87 public void onAnimationEnd(Animator animation) {
Winson Chung315b3ba2012-05-11 10:51:32 -070088 v.setLayerType(View.LAYER_TYPE_NONE, null);
89 }
90 });
91 }
92
Winson Chung4c98d922011-05-31 16:50:48 -070093 @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 Chunga6427b12011-07-27 10:53:39 -0700100 mInfoDropTarget = (ButtonDropTarget) mDropTargetBar.findViewById(R.id.info_target_text);
101 mDeleteDropTarget = (ButtonDropTarget) mDropTargetBar.findViewById(R.id.delete_target_text);
Winson Chunga62e9fd2011-07-11 15:20:48 -0700102 mBarHeight = getResources().getDimensionPixelSize(R.dimen.qsb_bar_height);
103
Adam Cohend4d7aa52011-07-19 21:47:37 -0700104 mInfoDropTarget.setSearchDropTargetBar(this);
105 mDeleteDropTarget.setSearchDropTargetBar(this);
106
Michael Jurka19e33472012-05-14 20:41:52 -0700107 mEnableDropDownDropTargets =
Winson Chunga62e9fd2011-07-11 15:20:48 -0700108 getResources().getBoolean(R.bool.config_useDropTargetDownTransition);
Winson Chung201bc822011-06-20 15:41:53 -0700109
110 // Create the various fade animations
Michael Jurka19e33472012-05-14 20:41:52 -0700111 if (mEnableDropDownDropTargets) {
Winson Chunga62e9fd2011-07-11 15:20:48 -0700112 mDropTargetBar.setTranslationY(-mBarHeight);
Adam Cohen9a6e9512013-11-01 18:31:38 -0700113 mDropTargetBarAnim = ObjectAnimator.ofFloat(mDropTargetBar, "translationY", -mBarHeight, 0f);
114 mQSBSearchBarAnim = ObjectAnimator.ofFloat(mQSBSearchBar, "translationY", 0f, -mBarHeight);
Winson Chung315b3ba2012-05-11 10:51:32 -0700115 } else {
116 mDropTargetBar.setAlpha(0f);
Adam Cohen9a6e9512013-11-01 18:31:38 -0700117 mDropTargetBarAnim = ObjectAnimator.ofFloat(mDropTargetBar, "alpha", 0f, 1f);
118 mQSBSearchBarAnim = ObjectAnimator.ofFloat(mQSBSearchBar, "alpha", 1f, 0f);
Winson Chunga62e9fd2011-07-11 15:20:48 -0700119 }
Winson Chung17f1bb82012-05-25 14:25:44 -0700120 setupAnimation(mDropTargetBarAnim, mDropTargetBar);
121 setupAnimation(mQSBSearchBarAnim, mQSBSearchBar);
Winson Chung201bc822011-06-20 15:41:53 -0700122 }
123
Winson Chung043f2af2012-03-01 16:09:54 -0800124 public void finishAnimations() {
Winson Chung17f1bb82012-05-25 14:25:44 -0700125 prepareStartAnimation(mDropTargetBar);
126 mDropTargetBarAnim.reverse();
127 prepareStartAnimation(mQSBSearchBar);
128 mQSBSearchBarAnim.reverse();
Winson Chung4c98d922011-05-31 16:50:48 -0700129 }
130
131 /*
Winson Chungf0ea4d32011-06-06 14:27:16 -0700132 * Shows and hides the search bar.
133 */
134 public void showSearchBar(boolean animated) {
Winson Chung17f1bb82012-05-25 14:25:44 -0700135 if (!mIsSearchBarHidden) return;
Winson Chungf0ea4d32011-06-06 14:27:16 -0700136 if (animated) {
Winson Chungc7d2b602012-05-16 17:02:20 -0700137 prepareStartAnimation(mQSBSearchBar);
Winson Chung17f1bb82012-05-25 14:25:44 -0700138 mQSBSearchBarAnim.reverse();
Winson Chungf0ea4d32011-06-06 14:27:16 -0700139 } else {
Winson Chung17f1bb82012-05-25 14:25:44 -0700140 mQSBSearchBarAnim.cancel();
Michael Jurka19e33472012-05-14 20:41:52 -0700141 if (mEnableDropDownDropTargets) {
142 mQSBSearchBar.setTranslationY(0);
143 } else {
Michael Jurka324dbdd2012-06-18 14:31:28 -0700144 mQSBSearchBar.setAlpha(1f);
Michael Jurka19e33472012-05-14 20:41:52 -0700145 }
Winson Chungf0ea4d32011-06-06 14:27:16 -0700146 }
147 mIsSearchBarHidden = false;
148 }
149 public void hideSearchBar(boolean animated) {
Winson Chung17f1bb82012-05-25 14:25:44 -0700150 if (mIsSearchBarHidden) return;
Winson Chungf0ea4d32011-06-06 14:27:16 -0700151 if (animated) {
Winson Chungc7d2b602012-05-16 17:02:20 -0700152 prepareStartAnimation(mQSBSearchBar);
Winson Chung17f1bb82012-05-25 14:25:44 -0700153 mQSBSearchBarAnim.start();
Winson Chungf0ea4d32011-06-06 14:27:16 -0700154 } else {
Winson Chung17f1bb82012-05-25 14:25:44 -0700155 mQSBSearchBarAnim.cancel();
Michael Jurka19e33472012-05-14 20:41:52 -0700156 if (mEnableDropDownDropTargets) {
Winson Chung17f1bb82012-05-25 14:25:44 -0700157 mQSBSearchBar.setTranslationY(-mBarHeight);
Michael Jurka19e33472012-05-14 20:41:52 -0700158 } else {
Michael Jurka324dbdd2012-06-18 14:31:28 -0700159 mQSBSearchBar.setAlpha(0f);
Michael Jurka19e33472012-05-14 20:41:52 -0700160 }
Winson Chungf0ea4d32011-06-06 14:27:16 -0700161 }
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 Chung4c98d922011-05-31 16:50:48 -0700176 * 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 Chungc7d2b602012-05-16 17:02:20 -0700181 prepareStartAnimation(mDropTargetBar);
Winson Chung17f1bb82012-05-25 14:25:44 -0700182 mDropTargetBarAnim.start();
Winson Chungf0ea4d32011-06-06 14:27:16 -0700183 if (!mIsSearchBarHidden) {
Winson Chungc7d2b602012-05-16 17:02:20 -0700184 prepareStartAnimation(mQSBSearchBar);
Winson Chung17f1bb82012-05-25 14:25:44 -0700185 mQSBSearchBarAnim.start();
Winson Chungf0ea4d32011-06-06 14:27:16 -0700186 }
Winson Chung4c98d922011-05-31 16:50:48 -0700187 }
188
Adam Cohend4d7aa52011-07-19 21:47:37 -0700189 public void deferOnDragEnd() {
190 mDeferOnDragEnd = true;
191 }
192
Winson Chung4c98d922011-05-31 16:50:48 -0700193 @Override
194 public void onDragEnd() {
Adam Cohend4d7aa52011-07-19 21:47:37 -0700195 if (!mDeferOnDragEnd) {
196 // Restore the QSB search bar, and animate out the drop target bar
Winson Chungc7d2b602012-05-16 17:02:20 -0700197 prepareStartAnimation(mDropTargetBar);
Winson Chung17f1bb82012-05-25 14:25:44 -0700198 mDropTargetBarAnim.reverse();
Adam Cohend4d7aa52011-07-19 21:47:37 -0700199 if (!mIsSearchBarHidden) {
Winson Chungc7d2b602012-05-16 17:02:20 -0700200 prepareStartAnimation(mQSBSearchBar);
Winson Chung17f1bb82012-05-25 14:25:44 -0700201 mQSBSearchBarAnim.reverse();
Adam Cohend4d7aa52011-07-19 21:47:37 -0700202 }
203 } else {
204 mDeferOnDragEnd = false;
Winson Chungf0ea4d32011-06-06 14:27:16 -0700205 }
Winson Chung4c98d922011-05-31 16:50:48 -0700206 }
Winson Chungc51db6a2011-10-05 11:44:49 -0700207
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 Jurka3a9fced2012-04-13 14:44:29 -0700217 mQSBSearchBar.setBackground(mPreviousBackground);
Winson Chungc51db6a2011-10-05 11:44:49 -0700218 }
219 }
220 }
Mathew Inwoodcf7f63b2011-10-13 11:31:38 +0100221
222 public Rect getSearchBarBounds() {
223 if (mQSBSearchBar != null) {
Mathew Inwoodcf7f63b2011-10-13 11:31:38 +0100224 final int[] pos = new int[2];
225 mQSBSearchBar.getLocationOnScreen(pos);
226
227 final Rect rect = new Rect();
Michael Jurka629758f2012-06-14 16:18:21 -0700228 rect.left = pos[0];
229 rect.top = pos[1];
230 rect.right = pos[0] + mQSBSearchBar.getWidth();
231 rect.bottom = pos[1] + mQSBSearchBar.getHeight();
Mathew Inwoodcf7f63b2011-10-13 11:31:38 +0100232 return rect;
233 } else {
234 return null;
235 }
236 }
Winson Chung4c98d922011-05-31 16:50:48 -0700237}