blob: 772a334b9ee4a98b5e898b6f87ed17ddc8a07ec6 [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
Sunny Goyald1ea63f2015-08-05 12:32:47 -070029import com.android.launcher3.util.Thunk;
30
Winson Chung4c98d922011-05-31 16:50:48 -070031/*
32 * Ths bar will manage the transition between the QSB search bar and the delete drop
33 * targets so that each of the individual IconDropTargets don't have to.
34 */
35public class SearchDropTargetBar extends FrameLayout implements DragController.DragListener {
36
Winson Chung006ee262015-08-03 14:40:11 -070037 /** The different states that the search bar space can be in. */
38 public enum State {
39 INVISIBLE (0f, 0f),
40 SEARCH_BAR (1f, 0f),
41 DROP_TARGET (0f, 1f);
Winson Chung4c98d922011-05-31 16:50:48 -070042
Winson Chung006ee262015-08-03 14:40:11 -070043 private final float mSearchBarAlpha;
44 private final float mDropTargetBarAlpha;
45
46 State(float sbAlpha, float dtbAlpha) {
47 mSearchBarAlpha = sbAlpha;
48 mDropTargetBarAlpha = dtbAlpha;
49 }
50
51 float getSearchBarAlpha() {
52 return mSearchBarAlpha;
53 }
54
55 float getDropTargetBarAlpha() {
56 return mDropTargetBarAlpha;
57 }
58 }
59
60 private static int DEFAULT_DRAG_FADE_DURATION = 175;
61
62 private LauncherViewPropertyAnimator mDropTargetBarAnimator;
63 private LauncherViewPropertyAnimator mQSBSearchBarAnimator;
Winson Chung17f1bb82012-05-25 14:25:44 -070064 private static final AccelerateInterpolator sAccelerateInterpolator =
65 new AccelerateInterpolator();
Winson Chung201bc822011-06-20 15:41:53 -070066
Winson Chung006ee262015-08-03 14:40:11 -070067 private State mState = State.SEARCH_BAR;
Sunny Goyald1ea63f2015-08-05 12:32:47 -070068 @Thunk View mQSB;
69 @Thunk View mDropTargetBar;
Adam Cohend4d7aa52011-07-19 21:47:37 -070070 private boolean mDeferOnDragEnd = false;
Sunny Goyald1ea63f2015-08-05 12:32:47 -070071 @Thunk boolean mAccessibilityEnabled = false;
Winson Chung4c98d922011-05-31 16:50:48 -070072
Sunny Goyalfa401a12015-04-10 13:45:42 -070073 // Drop targets
74 private ButtonDropTarget mInfoDropTarget;
75 private ButtonDropTarget mDeleteDropTarget;
76 private ButtonDropTarget mUninstallDropTarget;
77
Winson Chung4c98d922011-05-31 16:50:48 -070078 public SearchDropTargetBar(Context context, AttributeSet attrs) {
79 this(context, attrs, 0);
80 }
81
82 public SearchDropTargetBar(Context context, AttributeSet attrs, int defStyle) {
83 super(context, attrs, defStyle);
84 }
85
86 public void setup(Launcher launcher, DragController dragController) {
87 dragController.addDragListener(this);
Sunny Goyalfa401a12015-04-10 13:45:42 -070088 dragController.setFlingToDeleteDropTarget(mDeleteDropTarget);
89
Winson Chung4c98d922011-05-31 16:50:48 -070090 dragController.addDragListener(mInfoDropTarget);
91 dragController.addDragListener(mDeleteDropTarget);
Sunny Goyalfa401a12015-04-10 13:45:42 -070092 dragController.addDragListener(mUninstallDropTarget);
93
Winson Chung4c98d922011-05-31 16:50:48 -070094 dragController.addDropTarget(mInfoDropTarget);
95 dragController.addDropTarget(mDeleteDropTarget);
Sunny Goyalfa401a12015-04-10 13:45:42 -070096 dragController.addDropTarget(mUninstallDropTarget);
97
Winson Chung4c98d922011-05-31 16:50:48 -070098 mInfoDropTarget.setLauncher(launcher);
99 mDeleteDropTarget.setLauncher(launcher);
Sunny Goyalfa401a12015-04-10 13:45:42 -0700100 mUninstallDropTarget.setLauncher(launcher);
Sunny Goyal594d76d2014-11-06 10:12:54 -0800101 }
102
Winson Chung4c98d922011-05-31 16:50:48 -0700103 @Override
104 protected void onFinishInflate() {
105 super.onFinishInflate();
106
107 // Get the individual components
Winson Chung4c98d922011-05-31 16:50:48 -0700108 mDropTargetBar = findViewById(R.id.drag_target_bar);
Winson Chunga6427b12011-07-27 10:53:39 -0700109 mInfoDropTarget = (ButtonDropTarget) mDropTargetBar.findViewById(R.id.info_target_text);
110 mDeleteDropTarget = (ButtonDropTarget) mDropTargetBar.findViewById(R.id.delete_target_text);
Sunny Goyalfa401a12015-04-10 13:45:42 -0700111 mUninstallDropTarget = (ButtonDropTarget) mDropTargetBar.findViewById(R.id.uninstall_target_text);
Winson Chunga62e9fd2011-07-11 15:20:48 -0700112
Adam Cohend4d7aa52011-07-19 21:47:37 -0700113 mInfoDropTarget.setSearchDropTargetBar(this);
114 mDeleteDropTarget.setSearchDropTargetBar(this);
Sunny Goyalfa401a12015-04-10 13:45:42 -0700115 mUninstallDropTarget.setSearchDropTargetBar(this);
Adam Cohend4d7aa52011-07-19 21:47:37 -0700116
Winson Chung201bc822011-06-20 15:41:53 -0700117 // Create the various fade animations
Winson Chungdc61c4d2015-04-20 18:26:57 -0700118 mDropTargetBar.setAlpha(0f);
Winson Chung006ee262015-08-03 14:40:11 -0700119 mDropTargetBarAnimator = new LauncherViewPropertyAnimator(mDropTargetBar);
120 mDropTargetBarAnimator.setInterpolator(sAccelerateInterpolator);
121 mDropTargetBarAnimator.addListener(new AnimatorListenerAdapter() {
122 @Override
123 public void onAnimationStart(Animator animation) {
124 // Ensure that the view is visible for the animation
125 mDropTargetBar.setVisibility(View.VISIBLE);
126 }
127
128 @Override
129 public void onAnimationEnd(Animator animation) {
130 if (mDropTargetBar != null) {
131 AlphaUpdateListener.updateVisibility(mDropTargetBar, mAccessibilityEnabled);
132 }
133 }
134 });
Winson Chung201bc822011-06-20 15:41:53 -0700135 }
136
Winson Chung006ee262015-08-03 14:40:11 -0700137 public void setQsbSearchBar(View qsb) {
138 mQSB = qsb;
139 if (mQSB != null) {
140 // Update the search ber animation
141 mQSBSearchBarAnimator = new LauncherViewPropertyAnimator(mQSB);
142 mQSBSearchBarAnimator.setInterpolator(sAccelerateInterpolator);
143 mQSBSearchBarAnimator.addListener(new AnimatorListenerAdapter() {
144 @Override
145 public void onAnimationStart(Animator animation) {
146 // Ensure that the view is visible for the animation
147 if (mQSB != null) {
148 mQSB.setVisibility(View.VISIBLE);
149 }
150 }
Winson Chung4c98d922011-05-31 16:50:48 -0700151
Winson Chung006ee262015-08-03 14:40:11 -0700152 @Override
153 public void onAnimationEnd(Animator animation) {
154 if (mQSB != null) {
155 AlphaUpdateListener.updateVisibility(mQSB, mAccessibilityEnabled);
156 }
157 }
158 });
Winson Chungf0ea4d32011-06-06 14:27:16 -0700159 } else {
Winson Chung006ee262015-08-03 14:40:11 -0700160 mQSBSearchBarAnimator = null;
161 }
162 }
163
164 /**
165 * Animates the current search bar state to a new state. If the {@param duration} is 0, then
166 * the state is applied immediately.
167 */
168 public void animateToState(State newState, int duration) {
169 if (mState != newState) {
170 mState = newState;
171
172 // Update the accessibility state
173 AccessibilityManager am = (AccessibilityManager)
174 getContext().getSystemService(Context.ACCESSIBILITY_SERVICE);
175 mAccessibilityEnabled = am.isEnabled();
176
177 animateViewAlpha(mQSBSearchBarAnimator, mQSB, newState.getSearchBarAlpha(),
178 duration);
179 animateViewAlpha(mDropTargetBarAnimator, mDropTargetBar, newState.getDropTargetBarAlpha(),
180 duration);
181 }
182 }
183
184 /**
185 * Convenience method to animate the alpha of a view using hardware layers.
186 */
187 private void animateViewAlpha(LauncherViewPropertyAnimator animator, View v, float alpha,
188 int duration) {
Winson81c5f7e2015-08-20 15:22:18 -0700189 if (v == null) {
190 return;
191 }
192
193 animator.cancel();
194 if (Float.compare(v.getAlpha(), alpha) != 0) {
Winson Chung006ee262015-08-03 14:40:11 -0700195 if (duration > 0) {
196 animator.alpha(alpha).withLayer().setDuration(duration).start();
197 } else {
198 v.setAlpha(alpha);
199 AlphaUpdateListener.updateVisibility(v, mAccessibilityEnabled);
Michael Jurka19e33472012-05-14 20:41:52 -0700200 }
Winson Chungf0ea4d32011-06-06 14:27:16 -0700201 }
Winson Chungf0ea4d32011-06-06 14:27:16 -0700202 }
203
204 /*
Winson Chung4c98d922011-05-31 16:50:48 -0700205 * DragController.DragListener implementation
206 */
207 @Override
208 public void onDragStart(DragSource source, Object info, int dragAction) {
Winson Chung006ee262015-08-03 14:40:11 -0700209 animateToState(State.DROP_TARGET, DEFAULT_DRAG_FADE_DURATION);
Adam Cohenc9735cf2015-01-23 16:11:55 -0800210 }
211
Winson Chung006ee262015-08-03 14:40:11 -0700212 /**
213 * This is called to defer hiding the delete drop target until the drop animation has completed,
214 * instead of hiding immediately when the drag has ended.
215 */
Adam Cohend4d7aa52011-07-19 21:47:37 -0700216 public void deferOnDragEnd() {
217 mDeferOnDragEnd = true;
218 }
219
Winson Chung4c98d922011-05-31 16:50:48 -0700220 @Override
221 public void onDragEnd() {
Adam Cohend4d7aa52011-07-19 21:47:37 -0700222 if (!mDeferOnDragEnd) {
Winson Chung006ee262015-08-03 14:40:11 -0700223 animateToState(State.SEARCH_BAR, DEFAULT_DRAG_FADE_DURATION);
Adam Cohend4d7aa52011-07-19 21:47:37 -0700224 } else {
225 mDeferOnDragEnd = false;
Winson Chungf0ea4d32011-06-06 14:27:16 -0700226 }
Winson Chung4c98d922011-05-31 16:50:48 -0700227 }
Winson Chungc51db6a2011-10-05 11:44:49 -0700228
Winson Chung006ee262015-08-03 14:40:11 -0700229 /**
230 * @return the bounds of the QSB search bar.
231 */
Mathew Inwoodcf7f63b2011-10-13 11:31:38 +0100232 public Rect getSearchBarBounds() {
Winson Chung006ee262015-08-03 14:40:11 -0700233 if (mQSB != null) {
Mathew Inwoodcf7f63b2011-10-13 11:31:38 +0100234 final int[] pos = new int[2];
Winson Chung006ee262015-08-03 14:40:11 -0700235 mQSB.getLocationOnScreen(pos);
Mathew Inwoodcf7f63b2011-10-13 11:31:38 +0100236
237 final Rect rect = new Rect();
Michael Jurka629758f2012-06-14 16:18:21 -0700238 rect.left = pos[0];
239 rect.top = pos[1];
Winson Chung006ee262015-08-03 14:40:11 -0700240 rect.right = pos[0] + mQSB.getWidth();
241 rect.bottom = pos[1] + mQSB.getHeight();
Mathew Inwoodcf7f63b2011-10-13 11:31:38 +0100242 return rect;
243 } else {
244 return null;
245 }
246 }
Sunny Goyal1a70cef2015-04-22 11:29:51 -0700247
248 public void enableAccessibleDrag(boolean enable) {
Winson Chung006ee262015-08-03 14:40:11 -0700249 if (mQSB != null) {
250 mQSB.setVisibility(enable ? View.GONE : View.VISIBLE);
Sunny Goyal1a70cef2015-04-22 11:29:51 -0700251 }
252 mInfoDropTarget.enableAccessibleDrag(enable);
253 mDeleteDropTarget.enableAccessibleDrag(enable);
254 mUninstallDropTarget.enableAccessibleDrag(enable);
255 }
Winson Chung4c98d922011-05-31 16:50:48 -0700256}