blob: 9bda8b8f87976d59b475d37a656755b6b42c964c [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 Chung4c98d922011-05-31 16:50:48 -070026
Vadim Tryshevfedca432015-08-19 17:55:02 -070027import com.android.launcher3.dragndrop.DragController;
Sunny Goyald1ea63f2015-08-05 12:32:47 -070028import com.android.launcher3.util.Thunk;
29
Winson Chung4c98d922011-05-31 16:50:48 -070030/*
Tony Wickhamb54c4a32015-09-11 08:40:20 -070031 * This bar will manage the transition between the QSB search bar and the delete/uninstall drop
32 * targets so that each of the individual ButtonDropTargets don't have to.
Winson Chung4c98d922011-05-31 16:50:48 -070033 */
Tony Wickhamb54c4a32015-09-11 08:40:20 -070034public class SearchDropTargetBar extends BaseDropTargetBar {
Winson Chung4c98d922011-05-31 16:50:48 -070035
Winson Chung006ee262015-08-03 14:40:11 -070036 /** The different states that the search bar space can be in. */
37 public enum State {
38 INVISIBLE (0f, 0f),
39 SEARCH_BAR (1f, 0f),
40 DROP_TARGET (0f, 1f);
Winson Chung4c98d922011-05-31 16:50:48 -070041
Winson Chung006ee262015-08-03 14:40:11 -070042 private final float mSearchBarAlpha;
43 private final float mDropTargetBarAlpha;
44
45 State(float sbAlpha, float dtbAlpha) {
46 mSearchBarAlpha = sbAlpha;
47 mDropTargetBarAlpha = dtbAlpha;
48 }
49
50 float getSearchBarAlpha() {
51 return mSearchBarAlpha;
52 }
53
54 float getDropTargetBarAlpha() {
55 return mDropTargetBarAlpha;
56 }
57 }
58
Winson Chung006ee262015-08-03 14:40:11 -070059
Winson Chung006ee262015-08-03 14:40:11 -070060 private LauncherViewPropertyAnimator mQSBSearchBarAnimator;
Winson Chung201bc822011-06-20 15:41:53 -070061
Winson Chung006ee262015-08-03 14:40:11 -070062 private State mState = State.SEARCH_BAR;
Sunny Goyald1ea63f2015-08-05 12:32:47 -070063 @Thunk View mQSB;
Winson Chung4c98d922011-05-31 16:50:48 -070064
Sunny Goyalfa401a12015-04-10 13:45:42 -070065 // Drop targets
Sunny Goyalfa401a12015-04-10 13:45:42 -070066 private ButtonDropTarget mDeleteDropTarget;
67 private ButtonDropTarget mUninstallDropTarget;
68
Winson Chung4c98d922011-05-31 16:50:48 -070069 public SearchDropTargetBar(Context context, AttributeSet attrs) {
70 this(context, attrs, 0);
71 }
72
73 public SearchDropTargetBar(Context context, AttributeSet attrs, int defStyle) {
74 super(context, attrs, defStyle);
75 }
76
Winson Chung4c98d922011-05-31 16:50:48 -070077 @Override
78 protected void onFinishInflate() {
79 super.onFinishInflate();
80
81 // Get the individual components
Winson Chunga6427b12011-07-27 10:53:39 -070082 mDeleteDropTarget = (ButtonDropTarget) mDropTargetBar.findViewById(R.id.delete_target_text);
Tony Wickhamb54c4a32015-09-11 08:40:20 -070083 mUninstallDropTarget = (ButtonDropTarget) mDropTargetBar
84 .findViewById(R.id.uninstall_target_text);
Winson Chunga62e9fd2011-07-11 15:20:48 -070085
Tony Wickhamb54c4a32015-09-11 08:40:20 -070086 mDeleteDropTarget.setDropTargetBar(this);
87 mUninstallDropTarget.setDropTargetBar(this);
88 }
Adam Cohend4d7aa52011-07-19 21:47:37 -070089
Tony Wickhamb54c4a32015-09-11 08:40:20 -070090 @Override
91 public void setup(Launcher launcher, DragController dragController) {
92 dragController.addDragListener(this);
93 dragController.setFlingToDeleteDropTarget(mDeleteDropTarget);
Winson Chung006ee262015-08-03 14:40:11 -070094
Tony Wickhamb54c4a32015-09-11 08:40:20 -070095 dragController.addDragListener(mDeleteDropTarget);
96 dragController.addDragListener(mUninstallDropTarget);
97
98 dragController.addDropTarget(mDeleteDropTarget);
99 dragController.addDropTarget(mUninstallDropTarget);
100
101 mDeleteDropTarget.setLauncher(launcher);
102 mUninstallDropTarget.setLauncher(launcher);
103 }
104
105 @Override
Tony Wickham9aae47f2015-10-01 13:04:22 -0700106 public void showDropTargets() {
Tony Wickhamb54c4a32015-09-11 08:40:20 -0700107 animateToState(State.DROP_TARGET, DEFAULT_DRAG_FADE_DURATION);
108 }
109
110 @Override
Tony Wickham9aae47f2015-10-01 13:04:22 -0700111 public void hideDropTargets() {
Tony Wickhamb54c4a32015-09-11 08:40:20 -0700112 animateToState(State.SEARCH_BAR, DEFAULT_DRAG_FADE_DURATION);
Winson Chung201bc822011-06-20 15:41:53 -0700113 }
114
Winson Chung006ee262015-08-03 14:40:11 -0700115 public void setQsbSearchBar(View qsb) {
116 mQSB = qsb;
117 if (mQSB != null) {
Tony Wickhamb54c4a32015-09-11 08:40:20 -0700118 // Update the search bar animation
Winson Chung006ee262015-08-03 14:40:11 -0700119 mQSBSearchBarAnimator = new LauncherViewPropertyAnimator(mQSB);
120 mQSBSearchBarAnimator.setInterpolator(sAccelerateInterpolator);
121 mQSBSearchBarAnimator.addListener(new AnimatorListenerAdapter() {
122 @Override
123 public void onAnimationStart(Animator animation) {
124 // Ensure that the view is visible for the animation
125 if (mQSB != null) {
126 mQSB.setVisibility(View.VISIBLE);
127 }
128 }
Winson Chung4c98d922011-05-31 16:50:48 -0700129
Winson Chung006ee262015-08-03 14:40:11 -0700130 @Override
131 public void onAnimationEnd(Animator animation) {
132 if (mQSB != null) {
133 AlphaUpdateListener.updateVisibility(mQSB, mAccessibilityEnabled);
134 }
135 }
136 });
Winson Chungf0ea4d32011-06-06 14:27:16 -0700137 } else {
Winson Chung006ee262015-08-03 14:40:11 -0700138 mQSBSearchBarAnimator = null;
139 }
140 }
141
142 /**
143 * Animates the current search bar state to a new state. If the {@param duration} is 0, then
144 * the state is applied immediately.
145 */
146 public void animateToState(State newState, int duration) {
147 if (mState != newState) {
148 mState = newState;
149
150 // Update the accessibility state
151 AccessibilityManager am = (AccessibilityManager)
152 getContext().getSystemService(Context.ACCESSIBILITY_SERVICE);
153 mAccessibilityEnabled = am.isEnabled();
154
155 animateViewAlpha(mQSBSearchBarAnimator, mQSB, newState.getSearchBarAlpha(),
156 duration);
157 animateViewAlpha(mDropTargetBarAnimator, mDropTargetBar, newState.getDropTargetBarAlpha(),
158 duration);
159 }
160 }
161
162 /**
Winson Chung006ee262015-08-03 14:40:11 -0700163 * @return the bounds of the QSB search bar.
164 */
Mathew Inwoodcf7f63b2011-10-13 11:31:38 +0100165 public Rect getSearchBarBounds() {
Winson Chung006ee262015-08-03 14:40:11 -0700166 if (mQSB != null) {
Mathew Inwoodcf7f63b2011-10-13 11:31:38 +0100167 final int[] pos = new int[2];
Winson Chung006ee262015-08-03 14:40:11 -0700168 mQSB.getLocationOnScreen(pos);
Mathew Inwoodcf7f63b2011-10-13 11:31:38 +0100169
170 final Rect rect = new Rect();
Michael Jurka629758f2012-06-14 16:18:21 -0700171 rect.left = pos[0];
172 rect.top = pos[1];
Winson Chung006ee262015-08-03 14:40:11 -0700173 rect.right = pos[0] + mQSB.getWidth();
174 rect.bottom = pos[1] + mQSB.getHeight();
Mathew Inwoodcf7f63b2011-10-13 11:31:38 +0100175 return rect;
176 } else {
177 return null;
178 }
179 }
Sunny Goyal1a70cef2015-04-22 11:29:51 -0700180
Tony Wickhamb54c4a32015-09-11 08:40:20 -0700181 @Override
Sunny Goyal1a70cef2015-04-22 11:29:51 -0700182 public void enableAccessibleDrag(boolean enable) {
Winson Chung006ee262015-08-03 14:40:11 -0700183 if (mQSB != null) {
184 mQSB.setVisibility(enable ? View.GONE : View.VISIBLE);
Sunny Goyal1a70cef2015-04-22 11:29:51 -0700185 }
Sunny Goyal1a70cef2015-04-22 11:29:51 -0700186 mDeleteDropTarget.enableAccessibleDrag(enable);
187 mUninstallDropTarget.enableAccessibleDrag(enable);
188 }
Winson Chung4c98d922011-05-31 16:50:48 -0700189}