Winson Chung | 4c98d92 | 2011-05-31 16:50:48 -0700 | [diff] [blame] | 1 | /* |
| 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 Sandler | 325dc23 | 2013-06-05 22:57:57 -0400 | [diff] [blame] | 17 | package com.android.launcher3; |
Winson Chung | 4c98d92 | 2011-05-31 16:50:48 -0700 | [diff] [blame] | 18 | |
Winson Chung | 201bc82 | 2011-06-20 15:41:53 -0700 | [diff] [blame] | 19 | import android.animation.Animator; |
| 20 | import android.animation.AnimatorListenerAdapter; |
Winson Chung | 4c98d92 | 2011-05-31 16:50:48 -0700 | [diff] [blame] | 21 | import android.content.Context; |
Mathew Inwood | cf7f63b | 2011-10-13 11:31:38 +0100 | [diff] [blame] | 22 | import android.graphics.Rect; |
Winson Chung | 4c98d92 | 2011-05-31 16:50:48 -0700 | [diff] [blame] | 23 | import android.util.AttributeSet; |
| 24 | import android.view.View; |
Winson Chung | 006ee26 | 2015-08-03 14:40:11 -0700 | [diff] [blame] | 25 | import android.view.accessibility.AccessibilityManager; |
Winson Chung | 4c98d92 | 2011-05-31 16:50:48 -0700 | [diff] [blame] | 26 | |
Vadim Tryshev | fedca43 | 2015-08-19 17:55:02 -0700 | [diff] [blame] | 27 | import com.android.launcher3.dragndrop.DragController; |
Sunny Goyal | d1ea63f | 2015-08-05 12:32:47 -0700 | [diff] [blame] | 28 | import com.android.launcher3.util.Thunk; |
| 29 | |
Winson Chung | 4c98d92 | 2011-05-31 16:50:48 -0700 | [diff] [blame] | 30 | /* |
Tony Wickham | b54c4a3 | 2015-09-11 08:40:20 -0700 | [diff] [blame] | 31 | * 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 Chung | 4c98d92 | 2011-05-31 16:50:48 -0700 | [diff] [blame] | 33 | */ |
Tony Wickham | b54c4a3 | 2015-09-11 08:40:20 -0700 | [diff] [blame] | 34 | public class SearchDropTargetBar extends BaseDropTargetBar { |
Winson Chung | 4c98d92 | 2011-05-31 16:50:48 -0700 | [diff] [blame] | 35 | |
Winson Chung | 006ee26 | 2015-08-03 14:40:11 -0700 | [diff] [blame] | 36 | /** 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 Chung | 4c98d92 | 2011-05-31 16:50:48 -0700 | [diff] [blame] | 41 | |
Winson Chung | 006ee26 | 2015-08-03 14:40:11 -0700 | [diff] [blame] | 42 | 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 Chung | 006ee26 | 2015-08-03 14:40:11 -0700 | [diff] [blame] | 59 | |
Winson Chung | 006ee26 | 2015-08-03 14:40:11 -0700 | [diff] [blame] | 60 | private LauncherViewPropertyAnimator mQSBSearchBarAnimator; |
Winson Chung | 201bc82 | 2011-06-20 15:41:53 -0700 | [diff] [blame] | 61 | |
Winson Chung | 006ee26 | 2015-08-03 14:40:11 -0700 | [diff] [blame] | 62 | private State mState = State.SEARCH_BAR; |
Sunny Goyal | d1ea63f | 2015-08-05 12:32:47 -0700 | [diff] [blame] | 63 | @Thunk View mQSB; |
Winson Chung | 4c98d92 | 2011-05-31 16:50:48 -0700 | [diff] [blame] | 64 | |
Sunny Goyal | fa401a1 | 2015-04-10 13:45:42 -0700 | [diff] [blame] | 65 | // Drop targets |
Sunny Goyal | fa401a1 | 2015-04-10 13:45:42 -0700 | [diff] [blame] | 66 | private ButtonDropTarget mDeleteDropTarget; |
| 67 | private ButtonDropTarget mUninstallDropTarget; |
| 68 | |
Winson Chung | 4c98d92 | 2011-05-31 16:50:48 -0700 | [diff] [blame] | 69 | 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 Chung | 4c98d92 | 2011-05-31 16:50:48 -0700 | [diff] [blame] | 77 | @Override |
| 78 | protected void onFinishInflate() { |
| 79 | super.onFinishInflate(); |
| 80 | |
| 81 | // Get the individual components |
Winson Chung | a6427b1 | 2011-07-27 10:53:39 -0700 | [diff] [blame] | 82 | mDeleteDropTarget = (ButtonDropTarget) mDropTargetBar.findViewById(R.id.delete_target_text); |
Tony Wickham | b54c4a3 | 2015-09-11 08:40:20 -0700 | [diff] [blame] | 83 | mUninstallDropTarget = (ButtonDropTarget) mDropTargetBar |
| 84 | .findViewById(R.id.uninstall_target_text); |
Winson Chung | a62e9fd | 2011-07-11 15:20:48 -0700 | [diff] [blame] | 85 | |
Tony Wickham | b54c4a3 | 2015-09-11 08:40:20 -0700 | [diff] [blame] | 86 | mDeleteDropTarget.setDropTargetBar(this); |
| 87 | mUninstallDropTarget.setDropTargetBar(this); |
| 88 | } |
Adam Cohen | d4d7aa5 | 2011-07-19 21:47:37 -0700 | [diff] [blame] | 89 | |
Tony Wickham | b54c4a3 | 2015-09-11 08:40:20 -0700 | [diff] [blame] | 90 | @Override |
| 91 | public void setup(Launcher launcher, DragController dragController) { |
| 92 | dragController.addDragListener(this); |
| 93 | dragController.setFlingToDeleteDropTarget(mDeleteDropTarget); |
Winson Chung | 006ee26 | 2015-08-03 14:40:11 -0700 | [diff] [blame] | 94 | |
Tony Wickham | b54c4a3 | 2015-09-11 08:40:20 -0700 | [diff] [blame] | 95 | 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 Wickham | 9aae47f | 2015-10-01 13:04:22 -0700 | [diff] [blame^] | 106 | public void showDropTargets() { |
Tony Wickham | b54c4a3 | 2015-09-11 08:40:20 -0700 | [diff] [blame] | 107 | animateToState(State.DROP_TARGET, DEFAULT_DRAG_FADE_DURATION); |
| 108 | } |
| 109 | |
| 110 | @Override |
Tony Wickham | 9aae47f | 2015-10-01 13:04:22 -0700 | [diff] [blame^] | 111 | public void hideDropTargets() { |
Tony Wickham | b54c4a3 | 2015-09-11 08:40:20 -0700 | [diff] [blame] | 112 | animateToState(State.SEARCH_BAR, DEFAULT_DRAG_FADE_DURATION); |
Winson Chung | 201bc82 | 2011-06-20 15:41:53 -0700 | [diff] [blame] | 113 | } |
| 114 | |
Winson Chung | 006ee26 | 2015-08-03 14:40:11 -0700 | [diff] [blame] | 115 | public void setQsbSearchBar(View qsb) { |
| 116 | mQSB = qsb; |
| 117 | if (mQSB != null) { |
Tony Wickham | b54c4a3 | 2015-09-11 08:40:20 -0700 | [diff] [blame] | 118 | // Update the search bar animation |
Winson Chung | 006ee26 | 2015-08-03 14:40:11 -0700 | [diff] [blame] | 119 | 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 Chung | 4c98d92 | 2011-05-31 16:50:48 -0700 | [diff] [blame] | 129 | |
Winson Chung | 006ee26 | 2015-08-03 14:40:11 -0700 | [diff] [blame] | 130 | @Override |
| 131 | public void onAnimationEnd(Animator animation) { |
| 132 | if (mQSB != null) { |
| 133 | AlphaUpdateListener.updateVisibility(mQSB, mAccessibilityEnabled); |
| 134 | } |
| 135 | } |
| 136 | }); |
Winson Chung | f0ea4d3 | 2011-06-06 14:27:16 -0700 | [diff] [blame] | 137 | } else { |
Winson Chung | 006ee26 | 2015-08-03 14:40:11 -0700 | [diff] [blame] | 138 | 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 Chung | 006ee26 | 2015-08-03 14:40:11 -0700 | [diff] [blame] | 163 | * @return the bounds of the QSB search bar. |
| 164 | */ |
Mathew Inwood | cf7f63b | 2011-10-13 11:31:38 +0100 | [diff] [blame] | 165 | public Rect getSearchBarBounds() { |
Winson Chung | 006ee26 | 2015-08-03 14:40:11 -0700 | [diff] [blame] | 166 | if (mQSB != null) { |
Mathew Inwood | cf7f63b | 2011-10-13 11:31:38 +0100 | [diff] [blame] | 167 | final int[] pos = new int[2]; |
Winson Chung | 006ee26 | 2015-08-03 14:40:11 -0700 | [diff] [blame] | 168 | mQSB.getLocationOnScreen(pos); |
Mathew Inwood | cf7f63b | 2011-10-13 11:31:38 +0100 | [diff] [blame] | 169 | |
| 170 | final Rect rect = new Rect(); |
Michael Jurka | 629758f | 2012-06-14 16:18:21 -0700 | [diff] [blame] | 171 | rect.left = pos[0]; |
| 172 | rect.top = pos[1]; |
Winson Chung | 006ee26 | 2015-08-03 14:40:11 -0700 | [diff] [blame] | 173 | rect.right = pos[0] + mQSB.getWidth(); |
| 174 | rect.bottom = pos[1] + mQSB.getHeight(); |
Mathew Inwood | cf7f63b | 2011-10-13 11:31:38 +0100 | [diff] [blame] | 175 | return rect; |
| 176 | } else { |
| 177 | return null; |
| 178 | } |
| 179 | } |
Sunny Goyal | 1a70cef | 2015-04-22 11:29:51 -0700 | [diff] [blame] | 180 | |
Tony Wickham | b54c4a3 | 2015-09-11 08:40:20 -0700 | [diff] [blame] | 181 | @Override |
Sunny Goyal | 1a70cef | 2015-04-22 11:29:51 -0700 | [diff] [blame] | 182 | public void enableAccessibleDrag(boolean enable) { |
Winson Chung | 006ee26 | 2015-08-03 14:40:11 -0700 | [diff] [blame] | 183 | if (mQSB != null) { |
| 184 | mQSB.setVisibility(enable ? View.GONE : View.VISIBLE); |
Sunny Goyal | 1a70cef | 2015-04-22 11:29:51 -0700 | [diff] [blame] | 185 | } |
Sunny Goyal | 1a70cef | 2015-04-22 11:29:51 -0700 | [diff] [blame] | 186 | mDeleteDropTarget.enableAccessibleDrag(enable); |
| 187 | mUninstallDropTarget.enableAccessibleDrag(enable); |
| 188 | } |
Winson Chung | 4c98d92 | 2011-05-31 16:50:48 -0700 | [diff] [blame] | 189 | } |