blob: fed972c3a913ad1d395f66a1d22da6b3da40df66 [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;
Sunny Goyal4ffec482016-02-09 11:28:52 -080025import android.view.ViewDebug;
Winson Chung006ee262015-08-03 14:40:11 -070026import android.view.accessibility.AccessibilityManager;
Winson Chung4c98d922011-05-31 16:50:48 -070027
Vadim Tryshevfedca432015-08-19 17:55:02 -070028import com.android.launcher3.dragndrop.DragController;
Sunny Goyald1ea63f2015-08-05 12:32:47 -070029import com.android.launcher3.util.Thunk;
30
Winson Chung4c98d922011-05-31 16:50:48 -070031/*
Tony Wickhamb54c4a32015-09-11 08:40:20 -070032 * This bar will manage the transition between the QSB search bar and the delete/uninstall drop
33 * targets so that each of the individual ButtonDropTargets don't have to.
Winson Chung4c98d922011-05-31 16:50:48 -070034 */
Tony Wickhamb54c4a32015-09-11 08:40:20 -070035public class SearchDropTargetBar extends BaseDropTargetBar {
Winson Chung4c98d922011-05-31 16:50:48 -070036
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
Winson Chung006ee262015-08-03 14:40:11 -070060
Winson Chung006ee262015-08-03 14:40:11 -070061 private LauncherViewPropertyAnimator mQSBSearchBarAnimator;
Winson Chung201bc822011-06-20 15:41:53 -070062
Sunny Goyal4ffec482016-02-09 11:28:52 -080063 @ViewDebug.ExportedProperty(category = "launcher")
Winson Chung006ee262015-08-03 14:40:11 -070064 private State mState = State.SEARCH_BAR;
Sunny Goyald1ea63f2015-08-05 12:32:47 -070065 @Thunk View mQSB;
Winson Chung4c98d922011-05-31 16:50:48 -070066
Sunny Goyalfa401a12015-04-10 13:45:42 -070067 // Drop targets
Sunny Goyalfa401a12015-04-10 13:45:42 -070068 private ButtonDropTarget mDeleteDropTarget;
69 private ButtonDropTarget mUninstallDropTarget;
70
Winson Chung4c98d922011-05-31 16:50:48 -070071 public SearchDropTargetBar(Context context, AttributeSet attrs) {
72 this(context, attrs, 0);
73 }
74
75 public SearchDropTargetBar(Context context, AttributeSet attrs, int defStyle) {
76 super(context, attrs, defStyle);
77 }
78
Winson Chung4c98d922011-05-31 16:50:48 -070079 @Override
80 protected void onFinishInflate() {
81 super.onFinishInflate();
82
83 // Get the individual components
Winson Chunga6427b12011-07-27 10:53:39 -070084 mDeleteDropTarget = (ButtonDropTarget) mDropTargetBar.findViewById(R.id.delete_target_text);
Tony Wickhamb54c4a32015-09-11 08:40:20 -070085 mUninstallDropTarget = (ButtonDropTarget) mDropTargetBar
86 .findViewById(R.id.uninstall_target_text);
Winson Chunga62e9fd2011-07-11 15:20:48 -070087
Tony Wickhamb54c4a32015-09-11 08:40:20 -070088 mDeleteDropTarget.setDropTargetBar(this);
89 mUninstallDropTarget.setDropTargetBar(this);
90 }
Adam Cohend4d7aa52011-07-19 21:47:37 -070091
Tony Wickhamb54c4a32015-09-11 08:40:20 -070092 @Override
93 public void setup(Launcher launcher, DragController dragController) {
94 dragController.addDragListener(this);
95 dragController.setFlingToDeleteDropTarget(mDeleteDropTarget);
Winson Chung006ee262015-08-03 14:40:11 -070096
Tony Wickhamb54c4a32015-09-11 08:40:20 -070097 dragController.addDragListener(mDeleteDropTarget);
98 dragController.addDragListener(mUninstallDropTarget);
99
100 dragController.addDropTarget(mDeleteDropTarget);
101 dragController.addDropTarget(mUninstallDropTarget);
102
103 mDeleteDropTarget.setLauncher(launcher);
104 mUninstallDropTarget.setLauncher(launcher);
105 }
106
107 @Override
Tony Wickham9aae47f2015-10-01 13:04:22 -0700108 public void showDropTargets() {
Tony Wickhamb54c4a32015-09-11 08:40:20 -0700109 animateToState(State.DROP_TARGET, DEFAULT_DRAG_FADE_DURATION);
110 }
111
112 @Override
Tony Wickham9aae47f2015-10-01 13:04:22 -0700113 public void hideDropTargets() {
Tony Wickhamb54c4a32015-09-11 08:40:20 -0700114 animateToState(State.SEARCH_BAR, DEFAULT_DRAG_FADE_DURATION);
Winson Chung201bc822011-06-20 15:41:53 -0700115 }
116
Winson Chung006ee262015-08-03 14:40:11 -0700117 public void setQsbSearchBar(View qsb) {
118 mQSB = qsb;
119 if (mQSB != null) {
Tony Wickhamb54c4a32015-09-11 08:40:20 -0700120 // Update the search bar animation
Winson Chung006ee262015-08-03 14:40:11 -0700121 mQSBSearchBarAnimator = new LauncherViewPropertyAnimator(mQSB);
122 mQSBSearchBarAnimator.setInterpolator(sAccelerateInterpolator);
123 mQSBSearchBarAnimator.addListener(new AnimatorListenerAdapter() {
124 @Override
125 public void onAnimationStart(Animator animation) {
126 // Ensure that the view is visible for the animation
127 if (mQSB != null) {
128 mQSB.setVisibility(View.VISIBLE);
129 }
130 }
Winson Chung4c98d922011-05-31 16:50:48 -0700131
Winson Chung006ee262015-08-03 14:40:11 -0700132 @Override
133 public void onAnimationEnd(Animator animation) {
134 if (mQSB != null) {
135 AlphaUpdateListener.updateVisibility(mQSB, mAccessibilityEnabled);
136 }
137 }
138 });
Winson Chungf0ea4d32011-06-06 14:27:16 -0700139 } else {
Winson Chung006ee262015-08-03 14:40:11 -0700140 mQSBSearchBarAnimator = null;
141 }
142 }
143
144 /**
145 * Animates the current search bar state to a new state. If the {@param duration} is 0, then
146 * the state is applied immediately.
147 */
148 public void animateToState(State newState, int duration) {
149 if (mState != newState) {
150 mState = newState;
151
152 // Update the accessibility state
153 AccessibilityManager am = (AccessibilityManager)
154 getContext().getSystemService(Context.ACCESSIBILITY_SERVICE);
155 mAccessibilityEnabled = am.isEnabled();
156
157 animateViewAlpha(mQSBSearchBarAnimator, mQSB, newState.getSearchBarAlpha(),
158 duration);
159 animateViewAlpha(mDropTargetBarAnimator, mDropTargetBar, newState.getDropTargetBarAlpha(),
160 duration);
161 }
162 }
163
164 /**
Winson Chung006ee262015-08-03 14:40:11 -0700165 * @return the bounds of the QSB search bar.
166 */
Mathew Inwoodcf7f63b2011-10-13 11:31:38 +0100167 public Rect getSearchBarBounds() {
Winson Chung006ee262015-08-03 14:40:11 -0700168 if (mQSB != null) {
Mathew Inwoodcf7f63b2011-10-13 11:31:38 +0100169 final int[] pos = new int[2];
Winson Chung006ee262015-08-03 14:40:11 -0700170 mQSB.getLocationOnScreen(pos);
Mathew Inwoodcf7f63b2011-10-13 11:31:38 +0100171
172 final Rect rect = new Rect();
Michael Jurka629758f2012-06-14 16:18:21 -0700173 rect.left = pos[0];
174 rect.top = pos[1];
Winson Chung006ee262015-08-03 14:40:11 -0700175 rect.right = pos[0] + mQSB.getWidth();
176 rect.bottom = pos[1] + mQSB.getHeight();
Mathew Inwoodcf7f63b2011-10-13 11:31:38 +0100177 return rect;
178 } else {
179 return null;
180 }
181 }
Sunny Goyal1a70cef2015-04-22 11:29:51 -0700182
Tony Wickhamb54c4a32015-09-11 08:40:20 -0700183 @Override
Sunny Goyal1a70cef2015-04-22 11:29:51 -0700184 public void enableAccessibleDrag(boolean enable) {
Winson Chung006ee262015-08-03 14:40:11 -0700185 if (mQSB != null) {
186 mQSB.setVisibility(enable ? View.GONE : View.VISIBLE);
Sunny Goyal1a70cef2015-04-22 11:29:51 -0700187 }
Sunny Goyal1a70cef2015-04-22 11:29:51 -0700188 mDeleteDropTarget.enableAccessibleDrag(enable);
189 mUninstallDropTarget.enableAccessibleDrag(enable);
190 }
Winson Chung4c98d922011-05-31 16:50:48 -0700191}