The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2008 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 | |
Vadim Tryshev | fedca43 | 2015-08-19 17:55:02 -0700 | [diff] [blame] | 17 | package com.android.launcher3.dragndrop; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 18 | |
Sunny Goyal | d0030b0 | 2017-12-08 15:07:24 -0800 | [diff] [blame] | 19 | import static com.android.launcher3.compat.AccessibilityManagerCompat.sendCustomAccessibilityEvent; |
| 20 | |
Adam Cohen | 716b51e | 2011-06-30 12:09:54 -0700 | [diff] [blame] | 21 | import android.animation.Animator; |
| 22 | import android.animation.AnimatorListenerAdapter; |
| 23 | import android.animation.TimeInterpolator; |
| 24 | import android.animation.ValueAnimator; |
| 25 | import android.animation.ValueAnimator.AnimatorUpdateListener; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 26 | import android.content.Context; |
Adam Cohen | 716b51e | 2011-06-30 12:09:54 -0700 | [diff] [blame] | 27 | import android.content.res.Resources; |
Winson Chung | 360e63f | 2012-04-27 13:48:05 -0700 | [diff] [blame] | 28 | import android.graphics.Canvas; |
Adam Cohen | 6788269 | 2011-03-11 15:29:03 -0800 | [diff] [blame] | 29 | import android.graphics.Rect; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 30 | import android.util.AttributeSet; |
Dave Hawkey | 26279c6 | 2013-11-07 14:26:19 -0700 | [diff] [blame] | 31 | import android.view.KeyEvent; |
| 32 | import android.view.MotionEvent; |
| 33 | import android.view.View; |
| 34 | import android.view.ViewGroup; |
Adam Cohen | 3371da0 | 2011-10-25 21:38:29 -0700 | [diff] [blame] | 35 | import android.view.accessibility.AccessibilityEvent; |
| 36 | import android.view.accessibility.AccessibilityManager; |
Adam Cohen | 3e8f811 | 2011-07-02 18:03:00 -0700 | [diff] [blame] | 37 | import android.view.animation.Interpolator; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 38 | import android.widget.FrameLayout; |
Adam Cohen | ac8c876 | 2011-07-13 11:15:27 -0700 | [diff] [blame] | 39 | import android.widget.TextView; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 40 | |
Sunny Goyal | 740ac7f | 2016-09-28 16:47:32 -0700 | [diff] [blame] | 41 | import com.android.launcher3.AbstractFloatingView; |
Vadim Tryshev | fedca43 | 2015-08-19 17:55:02 -0700 | [diff] [blame] | 42 | import com.android.launcher3.CellLayout; |
Tony Wickham | 1bce7fd | 2016-04-28 17:39:03 -0700 | [diff] [blame] | 43 | import com.android.launcher3.DropTargetBar; |
Vadim Tryshev | fedca43 | 2015-08-19 17:55:02 -0700 | [diff] [blame] | 44 | import com.android.launcher3.InsettableFrameLayout; |
Vadim Tryshev | fedca43 | 2015-08-19 17:55:02 -0700 | [diff] [blame] | 45 | import com.android.launcher3.Launcher; |
Adam Cohen | f9c184a | 2016-01-15 16:47:43 -0800 | [diff] [blame] | 46 | import com.android.launcher3.R; |
Vadim Tryshev | fedca43 | 2015-08-19 17:55:02 -0700 | [diff] [blame] | 47 | import com.android.launcher3.ShortcutAndWidgetContainer; |
| 48 | import com.android.launcher3.Utilities; |
Sunny Goyal | 5bc6b6f | 2017-10-26 15:36:10 -0700 | [diff] [blame] | 49 | import com.android.launcher3.anim.Interpolators; |
Sunny Goyal | 2611943 | 2016-02-18 22:09:23 +0000 | [diff] [blame] | 50 | import com.android.launcher3.folder.Folder; |
| 51 | import com.android.launcher3.folder.FolderIcon; |
Sunny Goyal | 6639a5d | 2018-02-28 15:09:36 -0800 | [diff] [blame] | 52 | import com.android.launcher3.graphics.ViewScrim; |
Sunny Goyal | 3333b0c | 2016-05-09 20:43:21 -0700 | [diff] [blame] | 53 | import com.android.launcher3.keyboard.ViewGroupFocusHelper; |
Sunny Goyal | 8552517 | 2017-11-06 13:00:42 -0800 | [diff] [blame] | 54 | import com.android.launcher3.uioverrides.UiFactory; |
Adam Cohen | 091440a | 2015-03-18 14:16:05 -0700 | [diff] [blame] | 55 | import com.android.launcher3.util.Thunk; |
Hyunyoung Song | 645764e | 2016-06-06 14:19:02 -0700 | [diff] [blame] | 56 | import com.android.launcher3.util.TouchController; |
Adam Cohen | bc927f9 | 2014-10-28 16:16:02 -0700 | [diff] [blame] | 57 | |
Adam Cohen | 1d9af7d | 2011-06-22 15:26:58 -0700 | [diff] [blame] | 58 | import java.util.ArrayList; |
Adam Cohen | 6788269 | 2011-03-11 15:29:03 -0800 | [diff] [blame] | 59 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 60 | /** |
Michael Jurka | 0e26059 | 2010-06-30 17:07:39 -0700 | [diff] [blame] | 61 | * A ViewGroup that coordinates dragging across its descendants |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 62 | */ |
Adam Cohen | a6d0492 | 2014-10-23 17:28:30 -0700 | [diff] [blame] | 63 | public class DragLayer extends InsettableFrameLayout { |
Sunny Goyal | 0fa93bc | 2015-05-13 11:23:24 -0700 | [diff] [blame] | 64 | |
Sunny Goyal | b135956 | 2015-05-19 19:07:29 -0700 | [diff] [blame] | 65 | public static final int ANIMATION_END_DISAPPEAR = 0; |
Sunny Goyal | b135956 | 2015-05-19 19:07:29 -0700 | [diff] [blame] | 66 | public static final int ANIMATION_END_REMAIN_VISIBLE = 2; |
| 67 | |
Sunny Goyal | b135956 | 2015-05-19 19:07:29 -0700 | [diff] [blame] | 68 | private final int[] mTmpXY = new int[2]; |
| 69 | |
Adam Cohen | 091440a | 2015-03-18 14:16:05 -0700 | [diff] [blame] | 70 | @Thunk DragController mDragController; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 71 | |
Adam Cohen | 716b51e | 2011-06-30 12:09:54 -0700 | [diff] [blame] | 72 | private Launcher mLauncher; |
| 73 | |
Adam Cohen | 716b51e | 2011-06-30 12:09:54 -0700 | [diff] [blame] | 74 | // Variables relating to animation of views after drop |
| 75 | private ValueAnimator mDropAnim = null; |
Sunny Goyal | 5bc6b6f | 2017-10-26 15:36:10 -0700 | [diff] [blame] | 76 | private final TimeInterpolator mCubicEaseOutInterpolator = Interpolators.DEACCEL_1_5; |
Adam Cohen | 091440a | 2015-03-18 14:16:05 -0700 | [diff] [blame] | 77 | @Thunk DragView mDropView = null; |
| 78 | @Thunk int mAnchorViewInitialScrollX = 0; |
| 79 | @Thunk View mAnchorView = null; |
Adam Cohen | 8dfcba4 | 2011-07-07 16:38:18 -0700 | [diff] [blame] | 80 | |
Adam Cohen | 3371da0 | 2011-10-25 21:38:29 -0700 | [diff] [blame] | 81 | private boolean mHoverPointClosesFolder = false; |
Sunny Goyal | b135956 | 2015-05-19 19:07:29 -0700 | [diff] [blame] | 82 | private final Rect mHitRect = new Rect(); |
Adam Cohen | 6788269 | 2011-03-11 15:29:03 -0800 | [diff] [blame] | 83 | |
Adam Cohen | b0f3d74 | 2013-10-08 19:16:14 -0700 | [diff] [blame] | 84 | private TouchCompleteListener mTouchCompleteListener; |
| 85 | |
Adam Cohen | 432609a | 2014-03-13 17:03:22 -0700 | [diff] [blame] | 86 | private int mTopViewIndex; |
Adam Cohen | 2d783ce | 2014-04-07 14:11:48 -0700 | [diff] [blame] | 87 | private int mChildCountOnLastUpdate = -1; |
Jorim Jaggi | 55bd972 | 2014-01-16 15:30:42 -0800 | [diff] [blame] | 88 | |
Tony Wickham | 0f97b78 | 2015-12-02 17:55:07 -0800 | [diff] [blame] | 89 | // Related to adjacent page hints |
Sunny Goyal | 3333b0c | 2016-05-09 20:43:21 -0700 | [diff] [blame] | 90 | private final ViewGroupFocusHelper mFocusIndicatorHelper; |
| 91 | |
Sunny Goyal | 8552517 | 2017-11-06 13:00:42 -0800 | [diff] [blame] | 92 | protected TouchController[] mControllers; |
Hyunyoung Song | 645764e | 2016-06-06 14:19:02 -0700 | [diff] [blame] | 93 | private TouchController mActiveController; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 94 | /** |
| 95 | * Used to create a new DragLayer from XML. |
| 96 | * |
| 97 | * @param context The application's context. |
Michael Jurka | 0e26059 | 2010-06-30 17:07:39 -0700 | [diff] [blame] | 98 | * @param attrs The attributes set containing the Workspace's customization values. |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 99 | */ |
| 100 | public DragLayer(Context context, AttributeSet attrs) { |
| 101 | super(context, attrs); |
Winson Chung | be62afa | 2011-02-03 23:14:57 -0800 | [diff] [blame] | 102 | |
| 103 | // Disable multitouch across the workspace/all apps/customize tray |
| 104 | setMotionEventSplittingEnabled(false); |
Adam Cohen | 21b4110 | 2011-11-01 17:29:52 -0700 | [diff] [blame] | 105 | setChildrenDrawingOrderEnabled(true); |
Winson Chung | 360e63f | 2012-04-27 13:48:05 -0700 | [diff] [blame] | 106 | |
Sunny Goyal | 3333b0c | 2016-05-09 20:43:21 -0700 | [diff] [blame] | 107 | mFocusIndicatorHelper = new ViewGroupFocusHelper(this); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 108 | } |
| 109 | |
Sunny Goyal | 7ede611 | 2017-12-05 15:11:21 -0800 | [diff] [blame] | 110 | public void setup(Launcher launcher, DragController dragController) { |
Winson Chung | 4c98d92 | 2011-05-31 16:50:48 -0700 | [diff] [blame] | 111 | mLauncher = launcher; |
Hyunyoung Song | 645764e | 2016-06-06 14:19:02 -0700 | [diff] [blame] | 112 | mDragController = dragController; |
Sunny Goyal | 8552517 | 2017-11-06 13:00:42 -0800 | [diff] [blame] | 113 | mControllers = UiFactory.createTouchControllers(mLauncher); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 114 | } |
Adam Cohen | 76fc085 | 2011-06-17 13:26:23 -0700 | [diff] [blame] | 115 | |
Sunny Goyal | 3333b0c | 2016-05-09 20:43:21 -0700 | [diff] [blame] | 116 | public ViewGroupFocusHelper getFocusIndicatorHelper() { |
| 117 | return mFocusIndicatorHelper; |
| 118 | } |
| 119 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 120 | @Override |
| 121 | public boolean dispatchKeyEvent(KeyEvent event) { |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 122 | return mDragController.dispatchKeyEvent(event) || super.dispatchKeyEvent(event); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 123 | } |
| 124 | |
Hyunyoung Song | a9a8a42 | 2016-06-15 16:45:48 -0700 | [diff] [blame] | 125 | public boolean isEventOverHotseat(MotionEvent ev) { |
Tony Wickham | 1bce7fd | 2016-04-28 17:39:03 -0700 | [diff] [blame] | 126 | return isEventOverView(mLauncher.getHotseat(), ev); |
Hyunyoung Song | a9a8a42 | 2016-06-15 16:45:48 -0700 | [diff] [blame] | 127 | } |
| 128 | |
Adam Cohen | 3371da0 | 2011-10-25 21:38:29 -0700 | [diff] [blame] | 129 | private boolean isEventOverFolder(Folder folder, MotionEvent ev) { |
Tony Wickham | 1bce7fd | 2016-04-28 17:39:03 -0700 | [diff] [blame] | 130 | return isEventOverView(folder, ev); |
Adam Cohen | 3371da0 | 2011-10-25 21:38:29 -0700 | [diff] [blame] | 131 | } |
| 132 | |
Sunny Goyal | ccc414b | 2015-04-30 12:28:16 -0700 | [diff] [blame] | 133 | private boolean isEventOverDropTargetBar(MotionEvent ev) { |
Tony Wickham | 1bce7fd | 2016-04-28 17:39:03 -0700 | [diff] [blame] | 134 | return isEventOverView(mLauncher.getDropTargetBar(), ev); |
| 135 | } |
| 136 | |
Hyunyoung Song | 58a9c4b | 2016-08-05 13:57:21 -0700 | [diff] [blame] | 137 | public boolean isEventOverView(View view, MotionEvent ev) { |
Tony Wickham | 1bce7fd | 2016-04-28 17:39:03 -0700 | [diff] [blame] | 138 | getDescendantRectRelativeToSelf(view, mHitRect); |
Sunny Goyal | 47328fd | 2016-05-25 18:56:41 -0700 | [diff] [blame] | 139 | return mHitRect.contains((int) ev.getX(), (int) ev.getY()); |
Sunny Goyal | ccc414b | 2015-04-30 12:28:16 -0700 | [diff] [blame] | 140 | } |
| 141 | |
Adam Cohen | 6788269 | 2011-03-11 15:29:03 -0800 | [diff] [blame] | 142 | @Override |
Sunny Goyal | 6639a5d | 2018-02-28 15:09:36 -0800 | [diff] [blame] | 143 | protected boolean drawChild(Canvas canvas, View child, long drawingTime) { |
| 144 | ViewScrim scrim = ViewScrim.get(child); |
| 145 | if (scrim != null) { |
| 146 | scrim.draw(canvas, getWidth(), getHeight()); |
| 147 | } |
| 148 | return super.drawChild(canvas, child, drawingTime); |
| 149 | } |
| 150 | |
| 151 | @Override |
Adam Cohen | 6788269 | 2011-03-11 15:29:03 -0800 | [diff] [blame] | 152 | public boolean onInterceptTouchEvent(MotionEvent ev) { |
Adam Cohen | b0f3d74 | 2013-10-08 19:16:14 -0700 | [diff] [blame] | 153 | int action = ev.getAction(); |
| 154 | |
Sunny Goyal | 7ede611 | 2017-12-05 15:11:21 -0800 | [diff] [blame] | 155 | if (action == MotionEvent.ACTION_UP || action == MotionEvent.ACTION_CANCEL) { |
Adam Cohen | b0f3d74 | 2013-10-08 19:16:14 -0700 | [diff] [blame] | 156 | if (mTouchCompleteListener != null) { |
| 157 | mTouchCompleteListener.onTouchComplete(); |
| 158 | } |
| 159 | mTouchCompleteListener = null; |
Sunny Goyal | 7c8a65e | 2017-12-22 11:11:33 -0800 | [diff] [blame] | 160 | } else if (action == MotionEvent.ACTION_DOWN) { |
| 161 | mLauncher.finishAutoCancelActionMode(); |
Adam Cohen | 6788269 | 2011-03-11 15:29:03 -0800 | [diff] [blame] | 162 | } |
Sunny Goyal | fa8a5ac | 2018-02-07 13:29:42 -0800 | [diff] [blame] | 163 | return findActiveController(ev); |
| 164 | } |
| 165 | |
| 166 | private boolean findActiveController(MotionEvent ev) { |
Tony Wickham | 98cbd62 | 2016-08-04 11:52:18 -0700 | [diff] [blame] | 167 | mActiveController = null; |
| 168 | |
Sunny Goyal | 3792096 | 2017-09-28 13:43:24 -0700 | [diff] [blame] | 169 | AbstractFloatingView topView = AbstractFloatingView.getTopOpenView(mLauncher); |
| 170 | if (topView != null && topView.onControllerInterceptTouchEvent(ev)) { |
| 171 | mActiveController = topView; |
Sunny Goyal | 6ad72f0 | 2016-09-23 11:01:10 -0700 | [diff] [blame] | 172 | return true; |
Sunny Goyal | 6ad72f0 | 2016-09-23 11:01:10 -0700 | [diff] [blame] | 173 | } |
| 174 | |
Winson Chung | ca84552 | 2018-03-05 19:39:25 +0000 | [diff] [blame] | 175 | if (mLauncher.getStateManager().getState().disableInteraction) { |
| 176 | // You Shall Not Pass!!! |
| 177 | return true; |
| 178 | } |
| 179 | |
Sunny Goyal | 6ad72f0 | 2016-09-23 11:01:10 -0700 | [diff] [blame] | 180 | if (mDragController.onControllerInterceptTouchEvent(ev)) { |
Hyunyoung Song | 645764e | 2016-06-06 14:19:02 -0700 | [diff] [blame] | 181 | mActiveController = mDragController; |
Tony Wickham | c5b3196 | 2016-03-16 13:53:50 -0700 | [diff] [blame] | 182 | return true; |
Tony Wickham | dadb304 | 2016-02-24 11:07:00 -0800 | [diff] [blame] | 183 | } |
Tony Wickham | 1bce7fd | 2016-04-28 17:39:03 -0700 | [diff] [blame] | 184 | |
Sunny Goyal | 8552517 | 2017-11-06 13:00:42 -0800 | [diff] [blame] | 185 | for (TouchController controller : mControllers) { |
| 186 | if (controller.onControllerInterceptTouchEvent(ev)) { |
| 187 | mActiveController = controller; |
| 188 | return true; |
| 189 | } |
Hyunyoung Song | 645764e | 2016-06-06 14:19:02 -0700 | [diff] [blame] | 190 | } |
| 191 | return false; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 192 | } |
| 193 | |
| 194 | @Override |
Adam Cohen | 3371da0 | 2011-10-25 21:38:29 -0700 | [diff] [blame] | 195 | public boolean onInterceptHoverEvent(MotionEvent ev) { |
Michael Jurka | 023c71e | 2012-10-19 17:32:24 +0200 | [diff] [blame] | 196 | if (mLauncher == null || mLauncher.getWorkspace() == null) { |
| 197 | return false; |
| 198 | } |
Sunny Goyal | 740ac7f | 2016-09-28 16:47:32 -0700 | [diff] [blame] | 199 | Folder currentFolder = Folder.getOpen(mLauncher); |
Adam Cohen | 3371da0 | 2011-10-25 21:38:29 -0700 | [diff] [blame] | 200 | if (currentFolder == null) { |
| 201 | return false; |
| 202 | } else { |
Michael Jurka | 8b805b1 | 2012-04-18 14:23:14 -0700 | [diff] [blame] | 203 | AccessibilityManager accessibilityManager = (AccessibilityManager) |
| 204 | getContext().getSystemService(Context.ACCESSIBILITY_SERVICE); |
| 205 | if (accessibilityManager.isTouchExplorationEnabled()) { |
Adam Cohen | 3371da0 | 2011-10-25 21:38:29 -0700 | [diff] [blame] | 206 | final int action = ev.getAction(); |
Sunny Goyal | ccc414b | 2015-04-30 12:28:16 -0700 | [diff] [blame] | 207 | boolean isOverFolderOrSearchBar; |
Adam Cohen | 3371da0 | 2011-10-25 21:38:29 -0700 | [diff] [blame] | 208 | switch (action) { |
| 209 | case MotionEvent.ACTION_HOVER_ENTER: |
Sunny Goyal | ccc414b | 2015-04-30 12:28:16 -0700 | [diff] [blame] | 210 | isOverFolderOrSearchBar = isEventOverFolder(currentFolder, ev) || |
| 211 | (isInAccessibleDrag() && isEventOverDropTargetBar(ev)); |
| 212 | if (!isOverFolderOrSearchBar) { |
Adam Cohen | 3371da0 | 2011-10-25 21:38:29 -0700 | [diff] [blame] | 213 | sendTapOutsideFolderAccessibilityEvent(currentFolder.isEditingName()); |
| 214 | mHoverPointClosesFolder = true; |
| 215 | return true; |
Adam Cohen | 3371da0 | 2011-10-25 21:38:29 -0700 | [diff] [blame] | 216 | } |
Mihail Dumitrescu | 693fbc7 | 2014-01-03 12:40:22 +0000 | [diff] [blame] | 217 | mHoverPointClosesFolder = false; |
| 218 | break; |
Adam Cohen | 3371da0 | 2011-10-25 21:38:29 -0700 | [diff] [blame] | 219 | case MotionEvent.ACTION_HOVER_MOVE: |
Sunny Goyal | ccc414b | 2015-04-30 12:28:16 -0700 | [diff] [blame] | 220 | isOverFolderOrSearchBar = isEventOverFolder(currentFolder, ev) || |
| 221 | (isInAccessibleDrag() && isEventOverDropTargetBar(ev)); |
| 222 | if (!isOverFolderOrSearchBar && !mHoverPointClosesFolder) { |
Adam Cohen | 3371da0 | 2011-10-25 21:38:29 -0700 | [diff] [blame] | 223 | sendTapOutsideFolderAccessibilityEvent(currentFolder.isEditingName()); |
| 224 | mHoverPointClosesFolder = true; |
| 225 | return true; |
Sunny Goyal | ccc414b | 2015-04-30 12:28:16 -0700 | [diff] [blame] | 226 | } else if (!isOverFolderOrSearchBar) { |
Adam Cohen | 3371da0 | 2011-10-25 21:38:29 -0700 | [diff] [blame] | 227 | return true; |
| 228 | } |
Mihail Dumitrescu | 693fbc7 | 2014-01-03 12:40:22 +0000 | [diff] [blame] | 229 | mHoverPointClosesFolder = false; |
Adam Cohen | 3371da0 | 2011-10-25 21:38:29 -0700 | [diff] [blame] | 230 | } |
| 231 | } |
| 232 | } |
| 233 | return false; |
| 234 | } |
| 235 | |
| 236 | private void sendTapOutsideFolderAccessibilityEvent(boolean isEditingName) { |
Sunny Goyal | 52851aa | 2016-09-02 10:41:43 -0700 | [diff] [blame] | 237 | int stringId = isEditingName ? R.string.folder_tap_to_rename : R.string.folder_tap_to_close; |
Sunny Goyal | d0030b0 | 2017-12-08 15:07:24 -0800 | [diff] [blame] | 238 | sendCustomAccessibilityEvent( |
Sunny Goyal | 52851aa | 2016-09-02 10:41:43 -0700 | [diff] [blame] | 239 | this, AccessibilityEvent.TYPE_VIEW_FOCUSED, getContext().getString(stringId)); |
Adam Cohen | 3371da0 | 2011-10-25 21:38:29 -0700 | [diff] [blame] | 240 | } |
| 241 | |
Sunny Goyal | ccc414b | 2015-04-30 12:28:16 -0700 | [diff] [blame] | 242 | private boolean isInAccessibleDrag() { |
Sunny Goyal | ae50284 | 2016-06-17 08:43:56 -0700 | [diff] [blame] | 243 | return mLauncher.getAccessibilityDelegate().isInAccessibleDrag(); |
Sunny Goyal | ccc414b | 2015-04-30 12:28:16 -0700 | [diff] [blame] | 244 | } |
| 245 | |
Adam Cohen | 3371da0 | 2011-10-25 21:38:29 -0700 | [diff] [blame] | 246 | @Override |
Winson Chung | 83ca480 | 2013-04-12 15:10:52 -0700 | [diff] [blame] | 247 | public boolean onRequestSendAccessibilityEvent(View child, AccessibilityEvent event) { |
Sunny Goyal | 7d552c8 | 2016-08-03 10:31:11 -0700 | [diff] [blame] | 248 | // Shortcuts can appear above folder |
Sunny Goyal | 740ac7f | 2016-09-28 16:47:32 -0700 | [diff] [blame] | 249 | View topView = AbstractFloatingView.getTopOpenView(mLauncher); |
Sunny Goyal | 7d552c8 | 2016-08-03 10:31:11 -0700 | [diff] [blame] | 250 | if (topView != null) { |
Sunny Goyal | 52851aa | 2016-09-02 10:41:43 -0700 | [diff] [blame] | 251 | if (child == topView) { |
| 252 | return super.onRequestSendAccessibilityEvent(child, event); |
| 253 | } |
| 254 | if (isInAccessibleDrag() && child instanceof DropTargetBar) { |
| 255 | return super.onRequestSendAccessibilityEvent(child, event); |
| 256 | } |
| 257 | // Skip propagating onRequestSendAccessibilityEvent for all other children |
| 258 | // which are not topView |
| 259 | return false; |
Winson Chung | 83ca480 | 2013-04-12 15:10:52 -0700 | [diff] [blame] | 260 | } |
| 261 | return super.onRequestSendAccessibilityEvent(child, event); |
| 262 | } |
| 263 | |
| 264 | @Override |
| 265 | public void addChildrenForAccessibility(ArrayList<View> childrenForAccessibility) { |
Sunny Goyal | 740ac7f | 2016-09-28 16:47:32 -0700 | [diff] [blame] | 266 | View topView = AbstractFloatingView.getTopOpenView(mLauncher); |
Sunny Goyal | 52851aa | 2016-09-02 10:41:43 -0700 | [diff] [blame] | 267 | if (topView != null) { |
| 268 | // Only add the top view as a child for accessibility when it is open |
| 269 | childrenForAccessibility.add(topView); |
Sunny Goyal | ccc414b | 2015-04-30 12:28:16 -0700 | [diff] [blame] | 270 | |
| 271 | if (isInAccessibleDrag()) { |
Sunny Goyal | 47328fd | 2016-05-25 18:56:41 -0700 | [diff] [blame] | 272 | childrenForAccessibility.add(mLauncher.getDropTargetBar()); |
Sunny Goyal | ccc414b | 2015-04-30 12:28:16 -0700 | [diff] [blame] | 273 | } |
Winson Chung | 83ca480 | 2013-04-12 15:10:52 -0700 | [diff] [blame] | 274 | } else { |
| 275 | super.addChildrenForAccessibility(childrenForAccessibility); |
| 276 | } |
| 277 | } |
| 278 | |
| 279 | @Override |
Adam Cohen | 3371da0 | 2011-10-25 21:38:29 -0700 | [diff] [blame] | 280 | public boolean onHoverEvent(MotionEvent ev) { |
| 281 | // If we've received this, we've already done the necessary handling |
| 282 | // in onInterceptHoverEvent. Return true to consume the event. |
| 283 | return false; |
| 284 | } |
| 285 | |
| 286 | @Override |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 287 | public boolean onTouchEvent(MotionEvent ev) { |
Adam Cohen | 6788269 | 2011-03-11 15:29:03 -0800 | [diff] [blame] | 288 | int action = ev.getAction(); |
Sunny Goyal | 3792096 | 2017-09-28 13:43:24 -0700 | [diff] [blame] | 289 | if (action == MotionEvent.ACTION_UP || action == MotionEvent.ACTION_CANCEL) { |
Adam Cohen | b0f3d74 | 2013-10-08 19:16:14 -0700 | [diff] [blame] | 290 | if (mTouchCompleteListener != null) { |
| 291 | mTouchCompleteListener.onTouchComplete(); |
| 292 | } |
| 293 | mTouchCompleteListener = null; |
Adam Cohen | 6788269 | 2011-03-11 15:29:03 -0800 | [diff] [blame] | 294 | } |
| 295 | |
Hyunyoung Song | 645764e | 2016-06-06 14:19:02 -0700 | [diff] [blame] | 296 | if (mActiveController != null) { |
Sunny Goyal | 6ad72f0 | 2016-09-23 11:01:10 -0700 | [diff] [blame] | 297 | return mActiveController.onControllerTouchEvent(ev); |
Sunny Goyal | fa8a5ac | 2018-02-07 13:29:42 -0800 | [diff] [blame] | 298 | } else { |
| 299 | // In case no child view handled the touch event, we may not get onIntercept anymore |
| 300 | return findActiveController(ev); |
Hyunyoung Song | 645764e | 2016-06-06 14:19:02 -0700 | [diff] [blame] | 301 | } |
Romain Guy | 91a9c96 | 2009-06-12 13:52:17 -0700 | [diff] [blame] | 302 | } |
Romain Guy | ea3763c | 2010-01-11 18:02:04 -0800 | [diff] [blame] | 303 | |
Adam Cohen | 35e7e64 | 2011-07-17 14:47:18 -0700 | [diff] [blame] | 304 | /** |
| 305 | * Determine the rect of the descendant in this DragLayer's coordinates |
| 306 | * |
| 307 | * @param descendant The descendant whose coordinates we want to find. |
| 308 | * @param r The rect into which to place the results. |
| 309 | * @return The factor by which this descendant is scaled relative to this DragLayer. |
| 310 | */ |
Adam Cohen | ac8c876 | 2011-07-13 11:15:27 -0700 | [diff] [blame] | 311 | public float getDescendantRectRelativeToSelf(View descendant, Rect r) { |
Adam Cohen | 716b51e | 2011-06-30 12:09:54 -0700 | [diff] [blame] | 312 | mTmpXY[0] = 0; |
| 313 | mTmpXY[1] = 0; |
Adam Cohen | ac8c876 | 2011-07-13 11:15:27 -0700 | [diff] [blame] | 314 | float scale = getDescendantCoordRelativeToSelf(descendant, mTmpXY); |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 315 | |
Adam Cohen | 8dfcba4 | 2011-07-07 16:38:18 -0700 | [diff] [blame] | 316 | r.set(mTmpXY[0], mTmpXY[1], |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 317 | (int) (mTmpXY[0] + scale * descendant.getMeasuredWidth()), |
| 318 | (int) (mTmpXY[1] + scale * descendant.getMeasuredHeight())); |
Adam Cohen | ac8c876 | 2011-07-13 11:15:27 -0700 | [diff] [blame] | 319 | return scale; |
Adam Cohen | 716b51e | 2011-06-30 12:09:54 -0700 | [diff] [blame] | 320 | } |
Adam Cohen | 76fc085 | 2011-06-17 13:26:23 -0700 | [diff] [blame] | 321 | |
Adam Cohen | 307fe23 | 2012-08-16 17:55:58 -0700 | [diff] [blame] | 322 | public float getLocationInDragLayer(View child, int[] loc) { |
Adam Cohen | 35e7e64 | 2011-07-17 14:47:18 -0700 | [diff] [blame] | 323 | loc[0] = 0; |
| 324 | loc[1] = 0; |
Adam Cohen | 307fe23 | 2012-08-16 17:55:58 -0700 | [diff] [blame] | 325 | return getDescendantCoordRelativeToSelf(child, loc); |
Adam Cohen | 35e7e64 | 2011-07-17 14:47:18 -0700 | [diff] [blame] | 326 | } |
| 327 | |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 328 | public float getDescendantCoordRelativeToSelf(View descendant, int[] coord) { |
| 329 | return getDescendantCoordRelativeToSelf(descendant, coord, false); |
| 330 | } |
| 331 | |
Adam Cohen | 35e7e64 | 2011-07-17 14:47:18 -0700 | [diff] [blame] | 332 | /** |
| 333 | * Given a coordinate relative to the descendant, find the coordinate in this DragLayer's |
| 334 | * coordinates. |
| 335 | * |
| 336 | * @param descendant The descendant to which the passed coordinate is relative. |
| 337 | * @param coord The coordinate that we want mapped. |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 338 | * @param includeRootScroll Whether or not to account for the scroll of the root descendant: |
| 339 | * sometimes this is relevant as in a child's coordinates within the root descendant. |
Adam Cohen | 307fe23 | 2012-08-16 17:55:58 -0700 | [diff] [blame] | 340 | * @return The factor by which this descendant is scaled relative to this DragLayer. Caution |
| 341 | * this scale factor is assumed to be equal in X and Y, and so if at any point this |
| 342 | * assumption fails, we will need to return a pair of scale factors. |
Adam Cohen | 35e7e64 | 2011-07-17 14:47:18 -0700 | [diff] [blame] | 343 | */ |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 344 | public float getDescendantCoordRelativeToSelf(View descendant, int[] coord, |
| 345 | boolean includeRootScroll) { |
Sunny Goyal | e8b5d20 | 2016-06-13 16:36:59 -0700 | [diff] [blame] | 346 | return Utilities.getDescendantCoordRelativeToAncestor(descendant, this, |
Winson Chung | c9ca298 | 2013-07-19 12:07:38 -0700 | [diff] [blame] | 347 | coord, includeRootScroll); |
| 348 | } |
| 349 | |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 350 | /** |
| 351 | * Inverse of {@link #getDescendantCoordRelativeToSelf(View, int[])}. |
| 352 | */ |
Sunny Goyal | 5e0e94f | 2016-10-28 10:01:00 -0700 | [diff] [blame] | 353 | public void mapCoordInSelfToDescendant(View descendant, int[] coord) { |
| 354 | Utilities.mapCoordInSelfToDescendant(descendant, this, coord); |
Adam Cohen | 76fc085 | 2011-06-17 13:26:23 -0700 | [diff] [blame] | 355 | } |
| 356 | |
Adam Cohen | 3e8f811 | 2011-07-02 18:03:00 -0700 | [diff] [blame] | 357 | public void getViewRectRelativeToSelf(View v, Rect r) { |
| 358 | int[] loc = new int[2]; |
Adam Cohen | 8dfcba4 | 2011-07-07 16:38:18 -0700 | [diff] [blame] | 359 | getLocationInWindow(loc); |
Adam Cohen | 3e8f811 | 2011-07-02 18:03:00 -0700 | [diff] [blame] | 360 | int x = loc[0]; |
| 361 | int y = loc[1]; |
Adam Cohen | 716b51e | 2011-06-30 12:09:54 -0700 | [diff] [blame] | 362 | |
Adam Cohen | 8dfcba4 | 2011-07-07 16:38:18 -0700 | [diff] [blame] | 363 | v.getLocationInWindow(loc); |
Adam Cohen | 3e8f811 | 2011-07-02 18:03:00 -0700 | [diff] [blame] | 364 | int vX = loc[0]; |
| 365 | int vY = loc[1]; |
Adam Cohen | 716b51e | 2011-06-30 12:09:54 -0700 | [diff] [blame] | 366 | |
Adam Cohen | 3e8f811 | 2011-07-02 18:03:00 -0700 | [diff] [blame] | 367 | int left = vX - x; |
| 368 | int top = vY - y; |
| 369 | r.set(left, top, left + v.getMeasuredWidth(), top + v.getMeasuredHeight()); |
Adam Cohen | 716b51e | 2011-06-30 12:09:54 -0700 | [diff] [blame] | 370 | } |
| 371 | |
Romain Guy | ea3763c | 2010-01-11 18:02:04 -0800 | [diff] [blame] | 372 | @Override |
| 373 | public boolean dispatchUnhandledMove(View focused, int direction) { |
Tony Wickham | 8dd5168 | 2016-08-31 13:30:27 -0700 | [diff] [blame] | 374 | // Consume the unhandled move if a container is open, to avoid switching pages underneath. |
Sunny Goyal | 740ac7f | 2016-09-28 16:47:32 -0700 | [diff] [blame] | 375 | boolean isContainerOpen = AbstractFloatingView.getTopOpenView(mLauncher) != null; |
Tony Wickham | 8dd5168 | 2016-08-31 13:30:27 -0700 | [diff] [blame] | 376 | return isContainerOpen || mDragController.dispatchUnhandledMove(focused, direction); |
Romain Guy | ea3763c | 2010-01-11 18:02:04 -0800 | [diff] [blame] | 377 | } |
Adam Cohen | 120980b | 2010-12-08 11:05:37 -0800 | [diff] [blame] | 378 | |
Adam Cohen | bc927f9 | 2014-10-28 16:16:02 -0700 | [diff] [blame] | 379 | @Override |
Sunny Goyal | 19d32b7 | 2018-01-19 14:26:18 -0800 | [diff] [blame] | 380 | public boolean dispatchTouchEvent(MotionEvent ev) { |
| 381 | ev.offsetLocation(getTranslationX(), 0); |
| 382 | try { |
| 383 | return super.dispatchTouchEvent(ev); |
| 384 | } finally { |
| 385 | ev.offsetLocation(-getTranslationX(), 0); |
| 386 | } |
| 387 | } |
| 388 | |
| 389 | @Override |
Adam Cohen | bc927f9 | 2014-10-28 16:16:02 -0700 | [diff] [blame] | 390 | public LayoutParams generateLayoutParams(AttributeSet attrs) { |
| 391 | return new LayoutParams(getContext(), attrs); |
| 392 | } |
| 393 | |
| 394 | @Override |
| 395 | protected LayoutParams generateDefaultLayoutParams() { |
| 396 | return new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT); |
| 397 | } |
| 398 | |
| 399 | // Override to allow type-checking of LayoutParams. |
| 400 | @Override |
| 401 | protected boolean checkLayoutParams(ViewGroup.LayoutParams p) { |
| 402 | return p instanceof LayoutParams; |
| 403 | } |
| 404 | |
| 405 | @Override |
| 406 | protected LayoutParams generateLayoutParams(ViewGroup.LayoutParams p) { |
| 407 | return new LayoutParams(p); |
| 408 | } |
| 409 | |
| 410 | public static class LayoutParams extends InsettableFrameLayout.LayoutParams { |
Adam Cohen | 6788269 | 2011-03-11 15:29:03 -0800 | [diff] [blame] | 411 | public int x, y; |
| 412 | public boolean customPosition = false; |
| 413 | |
Adam Cohen | bc927f9 | 2014-10-28 16:16:02 -0700 | [diff] [blame] | 414 | public LayoutParams(Context c, AttributeSet attrs) { |
| 415 | super(c, attrs); |
| 416 | } |
| 417 | |
Adam Cohen | 6788269 | 2011-03-11 15:29:03 -0800 | [diff] [blame] | 418 | public LayoutParams(int width, int height) { |
| 419 | super(width, height); |
| 420 | } |
| 421 | |
Adam Cohen | bc927f9 | 2014-10-28 16:16:02 -0700 | [diff] [blame] | 422 | public LayoutParams(ViewGroup.LayoutParams lp) { |
| 423 | super(lp); |
| 424 | } |
| 425 | |
Adam Cohen | 6788269 | 2011-03-11 15:29:03 -0800 | [diff] [blame] | 426 | public void setWidth(int width) { |
| 427 | this.width = width; |
| 428 | } |
| 429 | |
| 430 | public int getWidth() { |
| 431 | return width; |
| 432 | } |
| 433 | |
| 434 | public void setHeight(int height) { |
| 435 | this.height = height; |
| 436 | } |
| 437 | |
| 438 | public int getHeight() { |
| 439 | return height; |
| 440 | } |
| 441 | |
| 442 | public void setX(int x) { |
| 443 | this.x = x; |
| 444 | } |
| 445 | |
| 446 | public int getX() { |
| 447 | return x; |
| 448 | } |
| 449 | |
| 450 | public void setY(int y) { |
| 451 | this.y = y; |
| 452 | } |
| 453 | |
| 454 | public int getY() { |
| 455 | return y; |
| 456 | } |
| 457 | } |
| 458 | |
| 459 | protected void onLayout(boolean changed, int l, int t, int r, int b) { |
| 460 | super.onLayout(changed, l, t, r, b); |
| 461 | int count = getChildCount(); |
| 462 | for (int i = 0; i < count; i++) { |
| 463 | View child = getChildAt(i); |
| 464 | final FrameLayout.LayoutParams flp = (FrameLayout.LayoutParams) child.getLayoutParams(); |
| 465 | if (flp instanceof LayoutParams) { |
| 466 | final LayoutParams lp = (LayoutParams) flp; |
| 467 | if (lp.customPosition) { |
| 468 | child.layout(lp.x, lp.y, lp.x + lp.width, lp.y + lp.height); |
| 469 | } |
| 470 | } |
| 471 | } |
| 472 | } |
| 473 | |
Winson Chung | 7bd1bbb | 2012-02-13 18:29:29 -0800 | [diff] [blame] | 474 | public void animateViewIntoPosition(DragView dragView, final int[] pos, float alpha, |
| 475 | float scaleX, float scaleY, int animationEndStyle, Runnable onFinishRunnable, |
| 476 | int duration) { |
Adam Cohen | b7e1618 | 2011-07-15 17:55:02 -0700 | [diff] [blame] | 477 | Rect r = new Rect(); |
| 478 | getViewRectRelativeToSelf(dragView, r); |
| 479 | final int fromX = r.left; |
| 480 | final int fromY = r.top; |
| 481 | |
Winson Chung | 7bd1bbb | 2012-02-13 18:29:29 -0800 | [diff] [blame] | 482 | animateViewIntoPosition(dragView, fromX, fromY, pos[0], pos[1], alpha, 1, 1, scaleX, scaleY, |
Adam Cohen | ed66b2b | 2012-01-23 17:28:51 -0800 | [diff] [blame] | 483 | onFinishRunnable, animationEndStyle, duration, null); |
| 484 | } |
| 485 | |
Sunny Goyal | 1797af4 | 2017-10-06 13:29:57 -0700 | [diff] [blame] | 486 | public void animateViewIntoPosition(DragView dragView, final View child, View anchorView) { |
| 487 | animateViewIntoPosition(dragView, child, -1, anchorView); |
Adam Cohen | ed51cc9 | 2011-08-01 20:28:08 -0700 | [diff] [blame] | 488 | } |
| 489 | |
| 490 | public void animateViewIntoPosition(DragView dragView, final View child, int duration, |
Sunny Goyal | 1797af4 | 2017-10-06 13:29:57 -0700 | [diff] [blame] | 491 | View anchorView) { |
Michael Jurka | a52570f | 2012-03-20 03:18:20 -0700 | [diff] [blame] | 492 | ShortcutAndWidgetContainer parentChildren = (ShortcutAndWidgetContainer) child.getParent(); |
Adam Cohen | 716b51e | 2011-06-30 12:09:54 -0700 | [diff] [blame] | 493 | CellLayout.LayoutParams lp = (CellLayout.LayoutParams) child.getLayoutParams(); |
Winson Chung | eecf02d | 2012-03-02 17:14:58 -0800 | [diff] [blame] | 494 | parentChildren.measureChild(child); |
Adam Cohen | 716b51e | 2011-06-30 12:09:54 -0700 | [diff] [blame] | 495 | |
Adam Cohen | 3e8f811 | 2011-07-02 18:03:00 -0700 | [diff] [blame] | 496 | Rect r = new Rect(); |
| 497 | getViewRectRelativeToSelf(dragView, r); |
Adam Cohen | 716b51e | 2011-06-30 12:09:54 -0700 | [diff] [blame] | 498 | |
| 499 | int coord[] = new int[2]; |
Adam Cohen | 307fe23 | 2012-08-16 17:55:58 -0700 | [diff] [blame] | 500 | float childScale = child.getScaleX(); |
| 501 | coord[0] = lp.x + (int) (child.getMeasuredWidth() * (1 - childScale) / 2); |
| 502 | coord[1] = lp.y + (int) (child.getMeasuredHeight() * (1 - childScale) / 2); |
Winson Chung | eecf02d | 2012-03-02 17:14:58 -0800 | [diff] [blame] | 503 | |
Adam Cohen | 8dfcba4 | 2011-07-07 16:38:18 -0700 | [diff] [blame] | 504 | // Since the child hasn't necessarily been laid out, we force the lp to be updated with |
Adam Cohen | ac8c876 | 2011-07-13 11:15:27 -0700 | [diff] [blame] | 505 | // the correct coordinates (above) and use these to determine the final location |
Winson Chung | 557d6ed | 2011-07-08 15:34:52 -0700 | [diff] [blame] | 506 | float scale = getDescendantCoordRelativeToSelf((View) child.getParent(), coord); |
Adam Cohen | 307fe23 | 2012-08-16 17:55:58 -0700 | [diff] [blame] | 507 | // We need to account for the scale of the child itself, as the above only accounts for |
| 508 | // for the scale in parents. |
| 509 | scale *= childScale; |
Adam Cohen | ac8c876 | 2011-07-13 11:15:27 -0700 | [diff] [blame] | 510 | int toX = coord[0]; |
| 511 | int toY = coord[1]; |
Winson Chung | eeb5bbc | 2013-11-13 15:47:05 -0800 | [diff] [blame] | 512 | float toScale = scale; |
Andrew Flynn | 4497ebf | 2012-05-09 11:28:00 -0700 | [diff] [blame] | 513 | if (child instanceof TextView) { |
| 514 | TextView tv = (TextView) child; |
Winson Chung | eeb5bbc | 2013-11-13 15:47:05 -0800 | [diff] [blame] | 515 | // Account for the source scale of the icon (ie. from AllApps to Workspace, in which |
| 516 | // the workspace may have smaller icon bounds). |
| 517 | toScale = scale / dragView.getIntrinsicIconScaleFactor(); |
Andrew Flynn | 4497ebf | 2012-05-09 11:28:00 -0700 | [diff] [blame] | 518 | |
| 519 | // The child may be scaled (always about the center of the view) so to account for it, |
| 520 | // we have to offset the position by the scaled size. Once we do that, we can center |
| 521 | // the drag view about the scaled child view. |
Winson Chung | eeb5bbc | 2013-11-13 15:47:05 -0800 | [diff] [blame] | 522 | toY += Math.round(toScale * tv.getPaddingTop()); |
| 523 | toY -= dragView.getMeasuredHeight() * (1 - toScale) / 2; |
Sunny Goyal | 95abbb3 | 2014-08-04 10:53:22 -0700 | [diff] [blame] | 524 | if (dragView.getDragVisualizeOffset() != null) { |
| 525 | toY -= Math.round(toScale * dragView.getDragVisualizeOffset().y); |
| 526 | } |
| 527 | |
Andrew Flynn | 4497ebf | 2012-05-09 11:28:00 -0700 | [diff] [blame] | 528 | toX -= (dragView.getMeasuredWidth() - Math.round(scale * child.getMeasuredWidth())) / 2; |
| 529 | } else if (child instanceof FolderIcon) { |
Winson Chung | ea359c6 | 2011-08-03 17:06:35 -0700 | [diff] [blame] | 530 | // Account for holographic blur padding on the drag view |
Winson Chung | 5f8afe6 | 2013-08-12 16:19:28 -0700 | [diff] [blame] | 531 | toY += Math.round(scale * (child.getPaddingTop() - dragView.getDragRegionTop())); |
Jon Miranda | c9c5763 | 2016-10-12 20:35:59 -0700 | [diff] [blame] | 532 | toY -= scale * dragView.getBlurSizeOutline() / 2; |
Adam Cohen | 307fe23 | 2012-08-16 17:55:58 -0700 | [diff] [blame] | 533 | toY -= (1 - scale) * dragView.getMeasuredHeight() / 2; |
Winson Chung | ea359c6 | 2011-08-03 17:06:35 -0700 | [diff] [blame] | 534 | // Center in the x coordinate about the target's drawable |
| 535 | toX -= (dragView.getMeasuredWidth() - Math.round(scale * child.getMeasuredWidth())) / 2; |
Adam Cohen | ac8c876 | 2011-07-13 11:15:27 -0700 | [diff] [blame] | 536 | } else { |
| 537 | toY -= (Math.round(scale * (dragView.getHeight() - child.getMeasuredHeight()))) / 2; |
| 538 | toX -= (Math.round(scale * (dragView.getMeasuredWidth() |
| 539 | - child.getMeasuredWidth()))) / 2; |
| 540 | } |
| 541 | |
| 542 | final int fromX = r.left; |
| 543 | final int fromY = r.top; |
Adam Cohen | 716b51e | 2011-06-30 12:09:54 -0700 | [diff] [blame] | 544 | child.setVisibility(INVISIBLE); |
Sunny Goyal | 6639a5d | 2018-02-28 15:09:36 -0800 | [diff] [blame] | 545 | Runnable onCompleteRunnable = () -> child.setVisibility(VISIBLE); |
Winson Chung | eeb5bbc | 2013-11-13 15:47:05 -0800 | [diff] [blame] | 546 | animateViewIntoPosition(dragView, fromX, fromY, toX, toY, 1, 1, 1, toScale, toScale, |
Adam Cohen | d41fbf5 | 2012-02-16 23:53:59 -0800 | [diff] [blame] | 547 | onCompleteRunnable, ANIMATION_END_DISAPPEAR, duration, anchorView); |
Winson Chung | 557d6ed | 2011-07-08 15:34:52 -0700 | [diff] [blame] | 548 | } |
| 549 | |
Adam Cohen | d41fbf5 | 2012-02-16 23:53:59 -0800 | [diff] [blame] | 550 | public void animateViewIntoPosition(final DragView view, final int fromX, final int fromY, |
Adam Cohen | ed66b2b | 2012-01-23 17:28:51 -0800 | [diff] [blame] | 551 | final int toX, final int toY, float finalAlpha, float initScaleX, float initScaleY, |
| 552 | float finalScaleX, float finalScaleY, Runnable onCompleteRunnable, |
| 553 | int animationEndStyle, int duration, View anchorView) { |
Adam Cohen | 8dfcba4 | 2011-07-07 16:38:18 -0700 | [diff] [blame] | 554 | Rect from = new Rect(fromX, fromY, fromX + |
| 555 | view.getMeasuredWidth(), fromY + view.getMeasuredHeight()); |
Adam Cohen | 3e8f811 | 2011-07-02 18:03:00 -0700 | [diff] [blame] | 556 | Rect to = new Rect(toX, toY, toX + view.getMeasuredWidth(), toY + view.getMeasuredHeight()); |
Adam Cohen | ed66b2b | 2012-01-23 17:28:51 -0800 | [diff] [blame] | 557 | animateView(view, from, to, finalAlpha, initScaleX, initScaleY, finalScaleX, finalScaleY, duration, |
| 558 | null, null, onCompleteRunnable, animationEndStyle, anchorView); |
Adam Cohen | 3e8f811 | 2011-07-02 18:03:00 -0700 | [diff] [blame] | 559 | } |
| 560 | |
Adam Cohen | b7e1618 | 2011-07-15 17:55:02 -0700 | [diff] [blame] | 561 | /** |
| 562 | * This method animates a view at the end of a drag and drop animation. |
| 563 | * |
| 564 | * @param view The view to be animated. This view is drawn directly into DragLayer, and so |
| 565 | * doesn't need to be a child of DragLayer. |
| 566 | * @param from The initial location of the view. Only the left and top parameters are used. |
| 567 | * @param to The final location of the view. Only the left and top parameters are used. This |
| 568 | * location doesn't account for scaling, and so should be centered about the desired |
| 569 | * final location (including scaling). |
| 570 | * @param finalAlpha The final alpha of the view, in case we want it to fade as it animates. |
Sunny Goyal | 6178f13 | 2016-07-11 17:30:03 -0700 | [diff] [blame] | 571 | * @param finalScaleX The final scale of the view. The view is scaled about its center. |
| 572 | * @param finalScaleY The final scale of the view. The view is scaled about its center. |
Adam Cohen | b7e1618 | 2011-07-15 17:55:02 -0700 | [diff] [blame] | 573 | * @param duration The duration of the animation. |
| 574 | * @param motionInterpolator The interpolator to use for the location of the view. |
| 575 | * @param alphaInterpolator The interpolator to use for the alpha of the view. |
| 576 | * @param onCompleteRunnable Optional runnable to run on animation completion. |
Sunny Goyal | 6178f13 | 2016-07-11 17:30:03 -0700 | [diff] [blame] | 577 | * @param animationEndStyle Whether or not to fade out the view once the animation completes. |
| 578 | * {@link #ANIMATION_END_DISAPPEAR} or {@link #ANIMATION_END_REMAIN_VISIBLE}. |
Adam Cohen | 6441de0 | 2011-12-14 14:25:32 -0800 | [diff] [blame] | 579 | * @param anchorView If not null, this represents the view which the animated view stays |
| 580 | * anchored to in case scrolling is currently taking place. Note: currently this is |
| 581 | * only used for the X dimension for the case of the workspace. |
Adam Cohen | b7e1618 | 2011-07-15 17:55:02 -0700 | [diff] [blame] | 582 | */ |
Winson Chung | 7bd1bbb | 2012-02-13 18:29:29 -0800 | [diff] [blame] | 583 | public void animateView(final DragView view, final Rect from, final Rect to, |
| 584 | final float finalAlpha, final float initScaleX, final float initScaleY, |
| 585 | final float finalScaleX, final float finalScaleY, int duration, |
| 586 | final Interpolator motionInterpolator, final Interpolator alphaInterpolator, |
| 587 | final Runnable onCompleteRunnable, final int animationEndStyle, View anchorView) { |
| 588 | |
Adam Cohen | 716b51e | 2011-06-30 12:09:54 -0700 | [diff] [blame] | 589 | // Calculate the duration of the animation based on the object's distance |
Sunny Goyal | f7a29e8 | 2015-04-24 15:20:43 -0700 | [diff] [blame] | 590 | final float dist = (float) Math.hypot(to.left - from.left, to.top - from.top); |
Adam Cohen | 716b51e | 2011-06-30 12:09:54 -0700 | [diff] [blame] | 591 | final Resources res = getResources(); |
| 592 | final float maxDist = (float) res.getInteger(R.integer.config_dropAnimMaxDist); |
Adam Cohen | 3e8f811 | 2011-07-02 18:03:00 -0700 | [diff] [blame] | 593 | |
| 594 | // If duration < 0, this is a cue to compute the duration based on the distance |
| 595 | if (duration < 0) { |
Sunny Goyal | dec3a90 | 2017-01-25 18:23:36 -0800 | [diff] [blame] | 596 | duration = res.getInteger(R.integer.config_dropAnimMaxDuration); |
| 597 | if (dist < maxDist) { |
| 598 | duration *= mCubicEaseOutInterpolator.getInterpolation(dist / maxDist); |
Adam Cohen | 3e8f811 | 2011-07-02 18:03:00 -0700 | [diff] [blame] | 599 | } |
Sunny Goyal | dec3a90 | 2017-01-25 18:23:36 -0800 | [diff] [blame] | 600 | duration = Math.max(duration, res.getInteger(R.integer.config_dropAnimMinDuration)); |
Adam Cohen | 716b51e | 2011-06-30 12:09:54 -0700 | [diff] [blame] | 601 | } |
| 602 | |
Winson Chung | 043f2af | 2012-03-01 16:09:54 -0800 | [diff] [blame] | 603 | // Fall back to cubic ease out interpolator for the animation if none is specified |
| 604 | TimeInterpolator interpolator = null; |
Adam Cohen | 3e8f811 | 2011-07-02 18:03:00 -0700 | [diff] [blame] | 605 | if (alphaInterpolator == null || motionInterpolator == null) { |
Winson Chung | 043f2af | 2012-03-01 16:09:54 -0800 | [diff] [blame] | 606 | interpolator = mCubicEaseOutInterpolator; |
Adam Cohen | 3e8f811 | 2011-07-02 18:03:00 -0700 | [diff] [blame] | 607 | } |
Adam Cohen | 716b51e | 2011-06-30 12:09:54 -0700 | [diff] [blame] | 608 | |
Winson Chung | 043f2af | 2012-03-01 16:09:54 -0800 | [diff] [blame] | 609 | // Animate the view |
Winson Chung | 7bd1bbb | 2012-02-13 18:29:29 -0800 | [diff] [blame] | 610 | final float initAlpha = view.getAlpha(); |
Winson Chung | 043f2af | 2012-03-01 16:09:54 -0800 | [diff] [blame] | 611 | final float dropViewScale = view.getScaleX(); |
| 612 | AnimatorUpdateListener updateCb = new AnimatorUpdateListener() { |
| 613 | @Override |
Adam Cohen | 716b51e | 2011-06-30 12:09:54 -0700 | [diff] [blame] | 614 | public void onAnimationUpdate(ValueAnimator animation) { |
| 615 | final float percent = (Float) animation.getAnimatedValue(); |
Winson Chung | 7bd1bbb | 2012-02-13 18:29:29 -0800 | [diff] [blame] | 616 | final int width = view.getMeasuredWidth(); |
| 617 | final int height = view.getMeasuredHeight(); |
Adam Cohen | 716b51e | 2011-06-30 12:09:54 -0700 | [diff] [blame] | 618 | |
Adam Cohen | 3e8f811 | 2011-07-02 18:03:00 -0700 | [diff] [blame] | 619 | float alphaPercent = alphaInterpolator == null ? percent : |
| 620 | alphaInterpolator.getInterpolation(percent); |
| 621 | float motionPercent = motionInterpolator == null ? percent : |
| 622 | motionInterpolator.getInterpolation(percent); |
| 623 | |
Winson Chung | 7bd1bbb | 2012-02-13 18:29:29 -0800 | [diff] [blame] | 624 | float initialScaleX = initScaleX * dropViewScale; |
| 625 | float initialScaleY = initScaleY * dropViewScale; |
| 626 | float scaleX = finalScaleX * percent + initialScaleX * (1 - percent); |
| 627 | float scaleY = finalScaleY * percent + initialScaleY * (1 - percent); |
| 628 | float alpha = finalAlpha * alphaPercent + initAlpha * (1 - alphaPercent); |
| 629 | |
| 630 | float fromLeft = from.left + (initialScaleX - 1f) * width / 2; |
| 631 | float fromTop = from.top + (initialScaleY - 1f) * height / 2; |
| 632 | |
| 633 | int x = (int) (fromLeft + Math.round(((to.left - fromLeft) * motionPercent))); |
| 634 | int y = (int) (fromTop + Math.round(((to.top - fromTop) * motionPercent))); |
| 635 | |
Adam Cohen | 28f852a | 2013-10-15 14:34:05 -0700 | [diff] [blame] | 636 | int anchorAdjust = mAnchorView == null ? 0 : (int) (mAnchorView.getScaleX() * |
| 637 | (mAnchorViewInitialScrollX - mAnchorView.getScrollX())); |
| 638 | |
| 639 | int xPos = x - mDropView.getScrollX() + anchorAdjust; |
Winson Chung | 7bd1bbb | 2012-02-13 18:29:29 -0800 | [diff] [blame] | 640 | int yPos = y - mDropView.getScrollY(); |
| 641 | |
| 642 | mDropView.setTranslationX(xPos); |
| 643 | mDropView.setTranslationY(yPos); |
| 644 | mDropView.setScaleX(scaleX); |
| 645 | mDropView.setScaleY(scaleY); |
| 646 | mDropView.setAlpha(alpha); |
Adam Cohen | 716b51e | 2011-06-30 12:09:54 -0700 | [diff] [blame] | 647 | } |
Winson Chung | 043f2af | 2012-03-01 16:09:54 -0800 | [diff] [blame] | 648 | }; |
| 649 | animateView(view, updateCb, duration, interpolator, onCompleteRunnable, animationEndStyle, |
| 650 | anchorView); |
| 651 | } |
| 652 | |
| 653 | public void animateView(final DragView view, AnimatorUpdateListener updateCb, int duration, |
| 654 | TimeInterpolator interpolator, final Runnable onCompleteRunnable, |
| 655 | final int animationEndStyle, View anchorView) { |
| 656 | // Clean up the previous animations |
| 657 | if (mDropAnim != null) mDropAnim.cancel(); |
Winson Chung | 043f2af | 2012-03-01 16:09:54 -0800 | [diff] [blame] | 658 | |
| 659 | // Show the drop view if it was previously hidden |
| 660 | mDropView = view; |
| 661 | mDropView.cancelAnimation(); |
Vadim Tryshev | d6c8e7e | 2015-07-08 13:40:14 -0700 | [diff] [blame] | 662 | mDropView.requestLayout(); |
Winson Chung | 043f2af | 2012-03-01 16:09:54 -0800 | [diff] [blame] | 663 | |
| 664 | // Set the anchor view if the page is scrolling |
| 665 | if (anchorView != null) { |
| 666 | mAnchorViewInitialScrollX = anchorView.getScrollX(); |
| 667 | } |
| 668 | mAnchorView = anchorView; |
| 669 | |
| 670 | // Create and start the animation |
| 671 | mDropAnim = new ValueAnimator(); |
| 672 | mDropAnim.setInterpolator(interpolator); |
| 673 | mDropAnim.setDuration(duration); |
| 674 | mDropAnim.setFloatValues(0f, 1f); |
| 675 | mDropAnim.addUpdateListener(updateCb); |
Adam Cohen | 3e8f811 | 2011-07-02 18:03:00 -0700 | [diff] [blame] | 676 | mDropAnim.addListener(new AnimatorListenerAdapter() { |
| 677 | public void onAnimationEnd(Animator animation) { |
Adam Cohen | 8dfcba4 | 2011-07-07 16:38:18 -0700 | [diff] [blame] | 678 | if (onCompleteRunnable != null) { |
| 679 | onCompleteRunnable.run(); |
| 680 | } |
Adam Cohen | ed66b2b | 2012-01-23 17:28:51 -0800 | [diff] [blame] | 681 | switch (animationEndStyle) { |
| 682 | case ANIMATION_END_DISAPPEAR: |
| 683 | clearAnimatedView(); |
| 684 | break; |
Adam Cohen | ed66b2b | 2012-01-23 17:28:51 -0800 | [diff] [blame] | 685 | case ANIMATION_END_REMAIN_VISIBLE: |
| 686 | break; |
Adam Cohen | 3e8f811 | 2011-07-02 18:03:00 -0700 | [diff] [blame] | 687 | } |
| 688 | } |
| 689 | }); |
Adam Cohen | 716b51e | 2011-06-30 12:09:54 -0700 | [diff] [blame] | 690 | mDropAnim.start(); |
| 691 | } |
| 692 | |
Adam Cohen | ed66b2b | 2012-01-23 17:28:51 -0800 | [diff] [blame] | 693 | public void clearAnimatedView() { |
Winson Chung | 7bd1bbb | 2012-02-13 18:29:29 -0800 | [diff] [blame] | 694 | if (mDropAnim != null) { |
| 695 | mDropAnim.cancel(); |
| 696 | } |
| 697 | if (mDropView != null) { |
Adam Cohen | 2d783ce | 2014-04-07 14:11:48 -0700 | [diff] [blame] | 698 | mDragController.onDeferredEndDrag(mDropView); |
Winson Chung | 7bd1bbb | 2012-02-13 18:29:29 -0800 | [diff] [blame] | 699 | } |
Adam Cohen | ed66b2b | 2012-01-23 17:28:51 -0800 | [diff] [blame] | 700 | mDropView = null; |
Adam Cohen | ed66b2b | 2012-01-23 17:28:51 -0800 | [diff] [blame] | 701 | invalidate(); |
| 702 | } |
| 703 | |
| 704 | public View getAnimatedView() { |
| 705 | return mDropView; |
| 706 | } |
| 707 | |
Adam Cohen | 716b51e | 2011-06-30 12:09:54 -0700 | [diff] [blame] | 708 | @Override |
Jon Miranda | 758d504 | 2017-11-08 14:38:23 -0800 | [diff] [blame] | 709 | public void onViewAdded(View child) { |
| 710 | super.onViewAdded(child); |
Adam Cohen | 21b4110 | 2011-11-01 17:29:52 -0700 | [diff] [blame] | 711 | updateChildIndices(); |
Tony | 865ee02 | 2018-03-06 23:50:03 +0000 | [diff] [blame] | 712 | UiFactory.onLauncherStateOrFocusChanged(mLauncher); |
Adam Cohen | 21b4110 | 2011-11-01 17:29:52 -0700 | [diff] [blame] | 713 | } |
| 714 | |
| 715 | @Override |
Jon Miranda | 758d504 | 2017-11-08 14:38:23 -0800 | [diff] [blame] | 716 | public void onViewRemoved(View child) { |
| 717 | super.onViewRemoved(child); |
Adam Cohen | 21b4110 | 2011-11-01 17:29:52 -0700 | [diff] [blame] | 718 | updateChildIndices(); |
Tony | 865ee02 | 2018-03-06 23:50:03 +0000 | [diff] [blame] | 719 | UiFactory.onLauncherStateOrFocusChanged(mLauncher); |
Adam Cohen | 21b4110 | 2011-11-01 17:29:52 -0700 | [diff] [blame] | 720 | } |
| 721 | |
Adam Cohen | 432609a | 2014-03-13 17:03:22 -0700 | [diff] [blame] | 722 | @Override |
| 723 | public void bringChildToFront(View child) { |
| 724 | super.bringChildToFront(child); |
| 725 | updateChildIndices(); |
| 726 | } |
| 727 | |
Adam Cohen | 21b4110 | 2011-11-01 17:29:52 -0700 | [diff] [blame] | 728 | private void updateChildIndices() { |
Adam Cohen | 432609a | 2014-03-13 17:03:22 -0700 | [diff] [blame] | 729 | mTopViewIndex = -1; |
Jorim Jaggi | 55bd972 | 2014-01-16 15:30:42 -0800 | [diff] [blame] | 730 | int childCount = getChildCount(); |
| 731 | for (int i = 0; i < childCount; i++) { |
Adam Cohen | b670f19 | 2014-04-08 15:34:17 -0700 | [diff] [blame] | 732 | if (getChildAt(i) instanceof DragView) { |
Adam Cohen | 432609a | 2014-03-13 17:03:22 -0700 | [diff] [blame] | 733 | mTopViewIndex = i; |
Jorim Jaggi | 55bd972 | 2014-01-16 15:30:42 -0800 | [diff] [blame] | 734 | } |
Adam Cohen | 21b4110 | 2011-11-01 17:29:52 -0700 | [diff] [blame] | 735 | } |
Adam Cohen | 2d783ce | 2014-04-07 14:11:48 -0700 | [diff] [blame] | 736 | mChildCountOnLastUpdate = childCount; |
Adam Cohen | 21b4110 | 2011-11-01 17:29:52 -0700 | [diff] [blame] | 737 | } |
| 738 | |
| 739 | @Override |
| 740 | protected int getChildDrawingOrder(int childCount, int i) { |
Adam Cohen | 2d783ce | 2014-04-07 14:11:48 -0700 | [diff] [blame] | 741 | if (mChildCountOnLastUpdate != childCount) { |
| 742 | // between platform versions 17 and 18, behavior for onChildViewRemoved / Added changed. |
| 743 | // Pre-18, the child was not added / removed by the time of those callbacks. We need to |
| 744 | // force update our representation of things here to avoid crashing on pre-18 devices |
| 745 | // in certain instances. |
| 746 | updateChildIndices(); |
| 747 | } |
| 748 | |
Adam Cohen | 432609a | 2014-03-13 17:03:22 -0700 | [diff] [blame] | 749 | // i represents the current draw iteration |
| 750 | if (mTopViewIndex == -1) { |
| 751 | // in general we do nothing |
Adam Cohen | 21b4110 | 2011-11-01 17:29:52 -0700 | [diff] [blame] | 752 | return i; |
Adam Cohen | 432609a | 2014-03-13 17:03:22 -0700 | [diff] [blame] | 753 | } else if (i == childCount - 1) { |
| 754 | // if we have a top index, we return it when drawing last item (highest z-order) |
| 755 | return mTopViewIndex; |
| 756 | } else if (i < mTopViewIndex) { |
Jorim Jaggi | 55bd972 | 2014-01-16 15:30:42 -0800 | [diff] [blame] | 757 | return i; |
Adam Cohen | 21b4110 | 2011-11-01 17:29:52 -0700 | [diff] [blame] | 758 | } else { |
Adam Cohen | 432609a | 2014-03-13 17:03:22 -0700 | [diff] [blame] | 759 | // for indexes greater than the top index, we fetch one item above to shift for the |
| 760 | // displacement of the top index |
| 761 | return i + 1; |
Adam Cohen | 21b4110 | 2011-11-01 17:29:52 -0700 | [diff] [blame] | 762 | } |
| 763 | } |
Winson Chung | 360e63f | 2012-04-27 13:48:05 -0700 | [diff] [blame] | 764 | |
Winson Chung | 360e63f | 2012-04-27 13:48:05 -0700 | [diff] [blame] | 765 | @Override |
| 766 | protected void dispatchDraw(Canvas canvas) { |
Sunny Goyal | 0fa93bc | 2015-05-13 11:23:24 -0700 | [diff] [blame] | 767 | // Draw the background below children. |
Sunny Goyal | 3333b0c | 2016-05-09 20:43:21 -0700 | [diff] [blame] | 768 | mFocusIndicatorHelper.draw(canvas); |
Winson Chung | 360e63f | 2012-04-27 13:48:05 -0700 | [diff] [blame] | 769 | super.dispatchDraw(canvas); |
Adam Cohen | 8cc0550 | 2014-08-22 18:32:15 -0700 | [diff] [blame] | 770 | } |
Winson Chung | 360e63f | 2012-04-27 13:48:05 -0700 | [diff] [blame] | 771 | |
Sunny Goyal | 52851aa | 2016-09-02 10:41:43 -0700 | [diff] [blame] | 772 | @Override |
| 773 | protected boolean onRequestFocusInDescendants(int direction, Rect previouslyFocusedRect) { |
Sunny Goyal | 740ac7f | 2016-09-28 16:47:32 -0700 | [diff] [blame] | 774 | View topView = AbstractFloatingView.getTopOpenView(mLauncher); |
Sunny Goyal | 52851aa | 2016-09-02 10:41:43 -0700 | [diff] [blame] | 775 | if (topView != null) { |
| 776 | return topView.requestFocus(direction, previouslyFocusedRect); |
| 777 | } else { |
| 778 | return super.onRequestFocusInDescendants(direction, previouslyFocusedRect); |
| 779 | } |
| 780 | } |
| 781 | |
| 782 | @Override |
| 783 | public void addFocusables(ArrayList<View> views, int direction, int focusableMode) { |
Sunny Goyal | 740ac7f | 2016-09-28 16:47:32 -0700 | [diff] [blame] | 784 | View topView = AbstractFloatingView.getTopOpenView(mLauncher); |
Sunny Goyal | 52851aa | 2016-09-02 10:41:43 -0700 | [diff] [blame] | 785 | if (topView != null) { |
| 786 | topView.addFocusables(views, direction); |
| 787 | } else { |
| 788 | super.addFocusables(views, direction, focusableMode); |
| 789 | } |
| 790 | } |
| 791 | |
Adam Cohen | b0f3d74 | 2013-10-08 19:16:14 -0700 | [diff] [blame] | 792 | public void setTouchCompleteListener(TouchCompleteListener listener) { |
| 793 | mTouchCompleteListener = listener; |
| 794 | } |
| 795 | |
| 796 | public interface TouchCompleteListener { |
Sunny Goyal | 7ede611 | 2017-12-05 15:11:21 -0800 | [diff] [blame] | 797 | void onTouchComplete(); |
Adam Cohen | b0f3d74 | 2013-10-08 19:16:14 -0700 | [diff] [blame] | 798 | } |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 799 | } |