blob: 4cdf1cac9a32653c41902d012493d126cbf151d6 [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 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 Chungdc61c4d2015-04-20 18:26:57 -070036 private static final int TRANSITION_DURATION = 200;
Winson Chung4c98d922011-05-31 16:50:48 -070037
Winson Chungdc61c4d2015-04-20 18:26:57 -070038 private ObjectAnimator mShowDropTargetBarAnim;
39 private ValueAnimator mHideSearchBarAnim;
Winson Chung17f1bb82012-05-25 14:25:44 -070040 private static final AccelerateInterpolator sAccelerateInterpolator =
41 new AccelerateInterpolator();
Winson Chung201bc822011-06-20 15:41:53 -070042
Winson Chungf0ea4d32011-06-06 14:27:16 -070043 private boolean mIsSearchBarHidden;
Winson Chung4c98d922011-05-31 16:50:48 -070044 private View mQSBSearchBar;
45 private View mDropTargetBar;
Adam Cohend4d7aa52011-07-19 21:47:37 -070046 private boolean mDeferOnDragEnd = false;
Winson Chung4c98d922011-05-31 16:50:48 -070047
Sunny Goyalfa401a12015-04-10 13:45:42 -070048 // Drop targets
49 private ButtonDropTarget mInfoDropTarget;
50 private ButtonDropTarget mDeleteDropTarget;
51 private ButtonDropTarget mUninstallDropTarget;
52
Winson Chung4c98d922011-05-31 16:50:48 -070053 public SearchDropTargetBar(Context context, AttributeSet attrs) {
54 this(context, attrs, 0);
55 }
56
57 public SearchDropTargetBar(Context context, AttributeSet attrs, int defStyle) {
58 super(context, attrs, defStyle);
59 }
60
61 public void setup(Launcher launcher, DragController dragController) {
62 dragController.addDragListener(this);
Sunny Goyalfa401a12015-04-10 13:45:42 -070063 dragController.setFlingToDeleteDropTarget(mDeleteDropTarget);
64
Winson Chung4c98d922011-05-31 16:50:48 -070065 dragController.addDragListener(mInfoDropTarget);
66 dragController.addDragListener(mDeleteDropTarget);
Sunny Goyalfa401a12015-04-10 13:45:42 -070067 dragController.addDragListener(mUninstallDropTarget);
68
Winson Chung4c98d922011-05-31 16:50:48 -070069 dragController.addDropTarget(mInfoDropTarget);
70 dragController.addDropTarget(mDeleteDropTarget);
Sunny Goyalfa401a12015-04-10 13:45:42 -070071 dragController.addDropTarget(mUninstallDropTarget);
72
Winson Chung4c98d922011-05-31 16:50:48 -070073 mInfoDropTarget.setLauncher(launcher);
74 mDeleteDropTarget.setLauncher(launcher);
Sunny Goyalfa401a12015-04-10 13:45:42 -070075 mUninstallDropTarget.setLauncher(launcher);
Sunny Goyal594d76d2014-11-06 10:12:54 -080076 }
77
78 public void setQsbSearchBar(View qsb) {
79 mQSBSearchBar = qsb;
Adam Cohenc2d6e892014-10-16 09:49:24 -070080 if (mQSBSearchBar != null) {
Winson Chungdc61c4d2015-04-20 18:26:57 -070081 mHideSearchBarAnim = LauncherAnimUtils.ofFloat(mQSBSearchBar, "alpha", 1f, 0f);
82 setupAnimation(mHideSearchBarAnim, mQSBSearchBar);
Cristina Stancu476493b2013-08-07 17:20:14 +010083 } else {
Adam Cohenc2d6e892014-10-16 09:49:24 -070084 // Create a no-op animation of the search bar is null
Winson Chungdc61c4d2015-04-20 18:26:57 -070085 mHideSearchBarAnim = ValueAnimator.ofFloat(0, 0);
86 mHideSearchBarAnim.setDuration(TRANSITION_DURATION);
Cristina Stancu476493b2013-08-07 17:20:14 +010087 }
Winson Chung4c98d922011-05-31 16:50:48 -070088 }
89
Winson Chungc7d2b602012-05-16 17:02:20 -070090 private void prepareStartAnimation(View v) {
Winson Chung17f1bb82012-05-25 14:25:44 -070091 // Enable the hw layers before the animation starts (will be disabled in the onAnimationEnd
92 // callback below)
Adam Cohenc2d6e892014-10-16 09:49:24 -070093 if (v != null) {
94 v.setLayerType(View.LAYER_TYPE_HARDWARE, null);
95 }
Winson Chungc7d2b602012-05-16 17:02:20 -070096 }
97
Adam Cohenc2d6e892014-10-16 09:49:24 -070098 private void setupAnimation(ValueAnimator anim, final View v) {
Winson Chung17f1bb82012-05-25 14:25:44 -070099 anim.setInterpolator(sAccelerateInterpolator);
Winson Chungdc61c4d2015-04-20 18:26:57 -0700100 anim.setDuration(TRANSITION_DURATION);
Winson Chung17f1bb82012-05-25 14:25:44 -0700101 anim.addListener(new AnimatorListenerAdapter() {
Winson Chung315b3ba2012-05-11 10:51:32 -0700102 @Override
103 public void onAnimationEnd(Animator animation) {
Adam Cohenc2d6e892014-10-16 09:49:24 -0700104 if (v != null) {
105 v.setLayerType(View.LAYER_TYPE_NONE, null);
106 }
Winson Chung315b3ba2012-05-11 10:51:32 -0700107 }
108 });
109 }
110
Winson Chung4c98d922011-05-31 16:50:48 -0700111 @Override
112 protected void onFinishInflate() {
113 super.onFinishInflate();
114
115 // Get the individual components
Winson Chung4c98d922011-05-31 16:50:48 -0700116 mDropTargetBar = findViewById(R.id.drag_target_bar);
Winson Chunga6427b12011-07-27 10:53:39 -0700117 mInfoDropTarget = (ButtonDropTarget) mDropTargetBar.findViewById(R.id.info_target_text);
118 mDeleteDropTarget = (ButtonDropTarget) mDropTargetBar.findViewById(R.id.delete_target_text);
Sunny Goyalfa401a12015-04-10 13:45:42 -0700119 mUninstallDropTarget = (ButtonDropTarget) mDropTargetBar.findViewById(R.id.uninstall_target_text);
Winson Chunga62e9fd2011-07-11 15:20:48 -0700120
Adam Cohend4d7aa52011-07-19 21:47:37 -0700121 mInfoDropTarget.setSearchDropTargetBar(this);
122 mDeleteDropTarget.setSearchDropTargetBar(this);
Sunny Goyalfa401a12015-04-10 13:45:42 -0700123 mUninstallDropTarget.setSearchDropTargetBar(this);
Adam Cohend4d7aa52011-07-19 21:47:37 -0700124
Winson Chung201bc822011-06-20 15:41:53 -0700125 // Create the various fade animations
Winson Chungdc61c4d2015-04-20 18:26:57 -0700126 mDropTargetBar.setAlpha(0f);
127 mShowDropTargetBarAnim = LauncherAnimUtils.ofFloat(mDropTargetBar, "alpha", 0f, 1f);
128 setupAnimation(mShowDropTargetBarAnim, mDropTargetBar);
Winson Chung201bc822011-06-20 15:41:53 -0700129 }
130
Winson Chungdc61c4d2015-04-20 18:26:57 -0700131 /**
132 * Finishes all the animations on the search and drop target bars.
133 */
Winson Chung043f2af2012-03-01 16:09:54 -0800134 public void finishAnimations() {
Winson Chung17f1bb82012-05-25 14:25:44 -0700135 prepareStartAnimation(mDropTargetBar);
Winson Chungdc61c4d2015-04-20 18:26:57 -0700136 mShowDropTargetBarAnim.reverse();
Winson Chung17f1bb82012-05-25 14:25:44 -0700137 prepareStartAnimation(mQSBSearchBar);
Winson Chungdc61c4d2015-04-20 18:26:57 -0700138 mHideSearchBarAnim.reverse();
Winson Chung4c98d922011-05-31 16:50:48 -0700139 }
140
Winson Chungdc61c4d2015-04-20 18:26:57 -0700141 /**
142 * Shows the search bar.
Winson Chungf0ea4d32011-06-06 14:27:16 -0700143 */
144 public void showSearchBar(boolean animated) {
Winson Chungdc61c4d2015-04-20 18:26:57 -0700145 if (!mIsSearchBarHidden) return;
Winson Chungf0ea4d32011-06-06 14:27:16 -0700146 if (animated) {
Winson Chungc7d2b602012-05-16 17:02:20 -0700147 prepareStartAnimation(mQSBSearchBar);
Winson Chungdc61c4d2015-04-20 18:26:57 -0700148 mHideSearchBarAnim.reverse();
Winson Chungf0ea4d32011-06-06 14:27:16 -0700149 } else {
Winson Chungdc61c4d2015-04-20 18:26:57 -0700150 mHideSearchBarAnim.cancel();
151 if (mQSBSearchBar != null) {
Michael Jurka324dbdd2012-06-18 14:31:28 -0700152 mQSBSearchBar.setAlpha(1f);
Michael Jurka19e33472012-05-14 20:41:52 -0700153 }
Winson Chungf0ea4d32011-06-06 14:27:16 -0700154 }
155 mIsSearchBarHidden = false;
156 }
Winson Chungdc61c4d2015-04-20 18:26:57 -0700157
158 /**
159 * Hides the search bar. We only use this for clings.
160 */
Winson Chungf0ea4d32011-06-06 14:27:16 -0700161 public void hideSearchBar(boolean animated) {
Winson Chungdc61c4d2015-04-20 18:26:57 -0700162 if (mIsSearchBarHidden) return;
Winson Chungf0ea4d32011-06-06 14:27:16 -0700163 if (animated) {
Winson Chungc7d2b602012-05-16 17:02:20 -0700164 prepareStartAnimation(mQSBSearchBar);
Winson Chungdc61c4d2015-04-20 18:26:57 -0700165 mHideSearchBarAnim.start();
Winson Chungf0ea4d32011-06-06 14:27:16 -0700166 } else {
Winson Chungdc61c4d2015-04-20 18:26:57 -0700167 mHideSearchBarAnim.cancel();
168 if (mQSBSearchBar != null) {
Michael Jurka324dbdd2012-06-18 14:31:28 -0700169 mQSBSearchBar.setAlpha(0f);
Michael Jurka19e33472012-05-14 20:41:52 -0700170 }
Winson Chungf0ea4d32011-06-06 14:27:16 -0700171 }
172 mIsSearchBarHidden = true;
173 }
174
Winson Chungdc61c4d2015-04-20 18:26:57 -0700175 /**
176 * Shows the drop target bar.
Winson Chungf0ea4d32011-06-06 14:27:16 -0700177 */
Winson Chungdc61c4d2015-04-20 18:26:57 -0700178 public void showDeleteTarget() {
179 // Animate out the QSB search bar, and animate in the drop target bar
180 prepareStartAnimation(mDropTargetBar);
181 mShowDropTargetBarAnim.start();
182 hideSearchBar(true);
Winson Chungf0ea4d32011-06-06 14:27:16 -0700183 }
Winson Chungdc61c4d2015-04-20 18:26:57 -0700184
185 /**
186 * Hides the drop target bar.
187 */
188 public void hideDeleteTarget() {
189 // Restore the QSB search bar, and animate out the drop target bar
190 prepareStartAnimation(mDropTargetBar);
191 mShowDropTargetBarAnim.reverse();
192 showSearchBar(true);
Winson Chungf0ea4d32011-06-06 14:27:16 -0700193 }
194
195 /*
Winson Chung4c98d922011-05-31 16:50:48 -0700196 * DragController.DragListener implementation
197 */
198 @Override
199 public void onDragStart(DragSource source, Object info, int dragAction) {
Adam Cohenc9735cf2015-01-23 16:11:55 -0800200 showDeleteTarget();
201 }
202
Adam Cohend4d7aa52011-07-19 21:47:37 -0700203 public void deferOnDragEnd() {
204 mDeferOnDragEnd = true;
205 }
206
Winson Chung4c98d922011-05-31 16:50:48 -0700207 @Override
208 public void onDragEnd() {
Adam Cohend4d7aa52011-07-19 21:47:37 -0700209 if (!mDeferOnDragEnd) {
Adam Cohenc9735cf2015-01-23 16:11:55 -0800210 hideDeleteTarget();
Adam Cohend4d7aa52011-07-19 21:47:37 -0700211 } 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
Mathew Inwoodcf7f63b2011-10-13 11:31:38 +0100216 public Rect getSearchBarBounds() {
217 if (mQSBSearchBar != null) {
Mathew Inwoodcf7f63b2011-10-13 11:31:38 +0100218 final int[] pos = new int[2];
219 mQSBSearchBar.getLocationOnScreen(pos);
220
221 final Rect rect = new Rect();
Michael Jurka629758f2012-06-14 16:18:21 -0700222 rect.left = pos[0];
223 rect.top = pos[1];
224 rect.right = pos[0] + mQSBSearchBar.getWidth();
225 rect.bottom = pos[1] + mQSBSearchBar.getHeight();
Mathew Inwoodcf7f63b2011-10-13 11:31:38 +0100226 return rect;
227 } else {
228 return null;
229 }
230 }
Sunny Goyal1a70cef2015-04-22 11:29:51 -0700231
232 public void enableAccessibleDrag(boolean enable) {
233 if (mQSBSearchBar != null) {
234 mQSBSearchBar.setVisibility(enable ? View.GONE : View.VISIBLE);
235 }
236 mInfoDropTarget.enableAccessibleDrag(enable);
237 mDeleteDropTarget.enableAccessibleDrag(enable);
238 mUninstallDropTarget.enableAccessibleDrag(enable);
239 }
Winson Chung4c98d922011-05-31 16:50:48 -0700240}