blob: fdcad8217ca18e66000d31670f3c47255f9e1da3 [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;
Winson Chung4c98d922011-05-31 16:50:48 -070021import android.content.Context;
Mathew Inwoodcf7f63b2011-10-13 11:31:38 +010022import android.graphics.Rect;
Winson Chung4c98d922011-05-31 16:50:48 -070023import android.util.AttributeSet;
24import android.view.View;
Winson Chung006ee262015-08-03 14:40:11 -070025import android.view.accessibility.AccessibilityManager;
Winson Chunga62e9fd2011-07-11 15:20:48 -070026import android.view.animation.AccelerateInterpolator;
Winson Chung4c98d922011-05-31 16:50:48 -070027import android.widget.FrameLayout;
28
Vadim Tryshevfedca432015-08-19 17:55:02 -070029import com.android.launcher3.dragndrop.DragController;
Sunny Goyald1ea63f2015-08-05 12:32:47 -070030import com.android.launcher3.util.Thunk;
31
Winson Chung4c98d922011-05-31 16:50:48 -070032/*
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 Chung006ee262015-08-03 14:40:11 -070038 /** The different states that the search bar space can be in. */
39 public enum State {
40 INVISIBLE (0f, 0f),
41 SEARCH_BAR (1f, 0f),
42 DROP_TARGET (0f, 1f);
Winson Chung4c98d922011-05-31 16:50:48 -070043
Winson Chung006ee262015-08-03 14:40:11 -070044 private final float mSearchBarAlpha;
45 private final float mDropTargetBarAlpha;
46
47 State(float sbAlpha, float dtbAlpha) {
48 mSearchBarAlpha = sbAlpha;
49 mDropTargetBarAlpha = dtbAlpha;
50 }
51
52 float getSearchBarAlpha() {
53 return mSearchBarAlpha;
54 }
55
56 float getDropTargetBarAlpha() {
57 return mDropTargetBarAlpha;
58 }
59 }
60
61 private static int DEFAULT_DRAG_FADE_DURATION = 175;
62
63 private LauncherViewPropertyAnimator mDropTargetBarAnimator;
64 private LauncherViewPropertyAnimator mQSBSearchBarAnimator;
Winson Chung17f1bb82012-05-25 14:25:44 -070065 private static final AccelerateInterpolator sAccelerateInterpolator =
66 new AccelerateInterpolator();
Winson Chung201bc822011-06-20 15:41:53 -070067
Winson Chung006ee262015-08-03 14:40:11 -070068 private State mState = State.SEARCH_BAR;
Sunny Goyald1ea63f2015-08-05 12:32:47 -070069 @Thunk View mQSB;
70 @Thunk View mDropTargetBar;
Adam Cohend4d7aa52011-07-19 21:47:37 -070071 private boolean mDeferOnDragEnd = false;
Sunny Goyald1ea63f2015-08-05 12:32:47 -070072 @Thunk boolean mAccessibilityEnabled = false;
Winson Chung4c98d922011-05-31 16:50:48 -070073
Sunny Goyalfa401a12015-04-10 13:45:42 -070074 // Drop targets
75 private ButtonDropTarget mInfoDropTarget;
76 private ButtonDropTarget mDeleteDropTarget;
77 private ButtonDropTarget mUninstallDropTarget;
78
Winson Chung4c98d922011-05-31 16:50:48 -070079 public SearchDropTargetBar(Context context, AttributeSet attrs) {
80 this(context, attrs, 0);
81 }
82
83 public SearchDropTargetBar(Context context, AttributeSet attrs, int defStyle) {
84 super(context, attrs, defStyle);
85 }
86
87 public void setup(Launcher launcher, DragController dragController) {
88 dragController.addDragListener(this);
Sunny Goyalfa401a12015-04-10 13:45:42 -070089 dragController.setFlingToDeleteDropTarget(mDeleteDropTarget);
90
Winson Chung4c98d922011-05-31 16:50:48 -070091 dragController.addDragListener(mInfoDropTarget);
92 dragController.addDragListener(mDeleteDropTarget);
Sunny Goyalfa401a12015-04-10 13:45:42 -070093 dragController.addDragListener(mUninstallDropTarget);
94
Winson Chung4c98d922011-05-31 16:50:48 -070095 dragController.addDropTarget(mInfoDropTarget);
96 dragController.addDropTarget(mDeleteDropTarget);
Sunny Goyalfa401a12015-04-10 13:45:42 -070097 dragController.addDropTarget(mUninstallDropTarget);
98
Winson Chung4c98d922011-05-31 16:50:48 -070099 mInfoDropTarget.setLauncher(launcher);
100 mDeleteDropTarget.setLauncher(launcher);
Sunny Goyalfa401a12015-04-10 13:45:42 -0700101 mUninstallDropTarget.setLauncher(launcher);
Sunny Goyal594d76d2014-11-06 10:12:54 -0800102 }
103
Winson Chung4c98d922011-05-31 16:50:48 -0700104 @Override
105 protected void onFinishInflate() {
106 super.onFinishInflate();
107
108 // Get the individual components
Winson Chung4c98d922011-05-31 16:50:48 -0700109 mDropTargetBar = findViewById(R.id.drag_target_bar);
Winson Chunga6427b12011-07-27 10:53:39 -0700110 mInfoDropTarget = (ButtonDropTarget) mDropTargetBar.findViewById(R.id.info_target_text);
111 mDeleteDropTarget = (ButtonDropTarget) mDropTargetBar.findViewById(R.id.delete_target_text);
Sunny Goyalfa401a12015-04-10 13:45:42 -0700112 mUninstallDropTarget = (ButtonDropTarget) mDropTargetBar.findViewById(R.id.uninstall_target_text);
Winson Chunga62e9fd2011-07-11 15:20:48 -0700113
Adam Cohend4d7aa52011-07-19 21:47:37 -0700114 mInfoDropTarget.setSearchDropTargetBar(this);
115 mDeleteDropTarget.setSearchDropTargetBar(this);
Sunny Goyalfa401a12015-04-10 13:45:42 -0700116 mUninstallDropTarget.setSearchDropTargetBar(this);
Adam Cohend4d7aa52011-07-19 21:47:37 -0700117
Winson Chung201bc822011-06-20 15:41:53 -0700118 // Create the various fade animations
Winson Chungdc61c4d2015-04-20 18:26:57 -0700119 mDropTargetBar.setAlpha(0f);
Winson Chung006ee262015-08-03 14:40:11 -0700120 mDropTargetBarAnimator = new LauncherViewPropertyAnimator(mDropTargetBar);
121 mDropTargetBarAnimator.setInterpolator(sAccelerateInterpolator);
122 mDropTargetBarAnimator.addListener(new AnimatorListenerAdapter() {
123 @Override
124 public void onAnimationStart(Animator animation) {
125 // Ensure that the view is visible for the animation
126 mDropTargetBar.setVisibility(View.VISIBLE);
127 }
128
129 @Override
130 public void onAnimationEnd(Animator animation) {
131 if (mDropTargetBar != null) {
132 AlphaUpdateListener.updateVisibility(mDropTargetBar, mAccessibilityEnabled);
133 }
134 }
135 });
Winson Chung201bc822011-06-20 15:41:53 -0700136 }
137
Winson Chung006ee262015-08-03 14:40:11 -0700138 public void setQsbSearchBar(View qsb) {
139 mQSB = qsb;
140 if (mQSB != null) {
141 // Update the search ber animation
142 mQSBSearchBarAnimator = new LauncherViewPropertyAnimator(mQSB);
143 mQSBSearchBarAnimator.setInterpolator(sAccelerateInterpolator);
144 mQSBSearchBarAnimator.addListener(new AnimatorListenerAdapter() {
145 @Override
146 public void onAnimationStart(Animator animation) {
147 // Ensure that the view is visible for the animation
148 if (mQSB != null) {
149 mQSB.setVisibility(View.VISIBLE);
150 }
151 }
Winson Chung4c98d922011-05-31 16:50:48 -0700152
Winson Chung006ee262015-08-03 14:40:11 -0700153 @Override
154 public void onAnimationEnd(Animator animation) {
155 if (mQSB != null) {
156 AlphaUpdateListener.updateVisibility(mQSB, mAccessibilityEnabled);
157 }
158 }
159 });
Winson Chungf0ea4d32011-06-06 14:27:16 -0700160 } else {
Winson Chung006ee262015-08-03 14:40:11 -0700161 mQSBSearchBarAnimator = null;
162 }
163 }
164
165 /**
166 * Animates the current search bar state to a new state. If the {@param duration} is 0, then
167 * the state is applied immediately.
168 */
169 public void animateToState(State newState, int duration) {
170 if (mState != newState) {
171 mState = newState;
172
173 // Update the accessibility state
174 AccessibilityManager am = (AccessibilityManager)
175 getContext().getSystemService(Context.ACCESSIBILITY_SERVICE);
176 mAccessibilityEnabled = am.isEnabled();
177
178 animateViewAlpha(mQSBSearchBarAnimator, mQSB, newState.getSearchBarAlpha(),
179 duration);
180 animateViewAlpha(mDropTargetBarAnimator, mDropTargetBar, newState.getDropTargetBarAlpha(),
181 duration);
182 }
183 }
184
185 /**
186 * Convenience method to animate the alpha of a view using hardware layers.
187 */
188 private void animateViewAlpha(LauncherViewPropertyAnimator animator, View v, float alpha,
189 int duration) {
Winson81c5f7e2015-08-20 15:22:18 -0700190 if (v == null) {
191 return;
192 }
193
194 animator.cancel();
195 if (Float.compare(v.getAlpha(), alpha) != 0) {
Winson Chung006ee262015-08-03 14:40:11 -0700196 if (duration > 0) {
197 animator.alpha(alpha).withLayer().setDuration(duration).start();
198 } else {
199 v.setAlpha(alpha);
200 AlphaUpdateListener.updateVisibility(v, mAccessibilityEnabled);
Michael Jurka19e33472012-05-14 20:41:52 -0700201 }
Winson Chungf0ea4d32011-06-06 14:27:16 -0700202 }
Winson Chungf0ea4d32011-06-06 14:27:16 -0700203 }
204
205 /*
Winson Chung4c98d922011-05-31 16:50:48 -0700206 * DragController.DragListener implementation
207 */
208 @Override
Sunny Goyalaa8ef112015-06-12 20:04:41 -0700209 public void onDragStart(DragSource source, ItemInfo info, int dragAction) {
Winson Chung006ee262015-08-03 14:40:11 -0700210 animateToState(State.DROP_TARGET, DEFAULT_DRAG_FADE_DURATION);
Adam Cohenc9735cf2015-01-23 16:11:55 -0800211 }
212
Winson Chung006ee262015-08-03 14:40:11 -0700213 /**
214 * This is called to defer hiding the delete drop target until the drop animation has completed,
215 * instead of hiding immediately when the drag has ended.
216 */
Adam Cohend4d7aa52011-07-19 21:47:37 -0700217 public void deferOnDragEnd() {
218 mDeferOnDragEnd = true;
219 }
220
Winson Chung4c98d922011-05-31 16:50:48 -0700221 @Override
222 public void onDragEnd() {
Adam Cohend4d7aa52011-07-19 21:47:37 -0700223 if (!mDeferOnDragEnd) {
Winson Chung006ee262015-08-03 14:40:11 -0700224 animateToState(State.SEARCH_BAR, DEFAULT_DRAG_FADE_DURATION);
Adam Cohend4d7aa52011-07-19 21:47:37 -0700225 } else {
226 mDeferOnDragEnd = false;
Winson Chungf0ea4d32011-06-06 14:27:16 -0700227 }
Winson Chung4c98d922011-05-31 16:50:48 -0700228 }
Winson Chungc51db6a2011-10-05 11:44:49 -0700229
Winson Chung006ee262015-08-03 14:40:11 -0700230 /**
231 * @return the bounds of the QSB search bar.
232 */
Mathew Inwoodcf7f63b2011-10-13 11:31:38 +0100233 public Rect getSearchBarBounds() {
Winson Chung006ee262015-08-03 14:40:11 -0700234 if (mQSB != null) {
Mathew Inwoodcf7f63b2011-10-13 11:31:38 +0100235 final int[] pos = new int[2];
Winson Chung006ee262015-08-03 14:40:11 -0700236 mQSB.getLocationOnScreen(pos);
Mathew Inwoodcf7f63b2011-10-13 11:31:38 +0100237
238 final Rect rect = new Rect();
Michael Jurka629758f2012-06-14 16:18:21 -0700239 rect.left = pos[0];
240 rect.top = pos[1];
Winson Chung006ee262015-08-03 14:40:11 -0700241 rect.right = pos[0] + mQSB.getWidth();
242 rect.bottom = pos[1] + mQSB.getHeight();
Mathew Inwoodcf7f63b2011-10-13 11:31:38 +0100243 return rect;
244 } else {
245 return null;
246 }
247 }
Sunny Goyal1a70cef2015-04-22 11:29:51 -0700248
249 public void enableAccessibleDrag(boolean enable) {
Winson Chung006ee262015-08-03 14:40:11 -0700250 if (mQSB != null) {
251 mQSB.setVisibility(enable ? View.GONE : View.VISIBLE);
Sunny Goyal1a70cef2015-04-22 11:29:51 -0700252 }
253 mInfoDropTarget.enableAccessibleDrag(enable);
254 mDeleteDropTarget.enableAccessibleDrag(enable);
255 mUninstallDropTarget.enableAccessibleDrag(enable);
256 }
Winson Chung4c98d922011-05-31 16:50:48 -0700257}