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 | |
Daniel Sandler | 325dc23 | 2013-06-05 22:57:57 -0400 | [diff] [blame] | 17 | package com.android.launcher3; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 18 | |
Sunny Goyal | aeb1643 | 2017-10-16 11:46:41 -0700 | [diff] [blame] | 19 | import static com.android.launcher3.LauncherAnimUtils.OVERVIEW_TRANSITION_MS; |
Sunny Goyal | 5bc6b6f | 2017-10-26 15:36:10 -0700 | [diff] [blame] | 20 | import static com.android.launcher3.LauncherAnimUtils.SPRING_LOADED_EXIT_DELAY; |
Sunny Goyal | aeb1643 | 2017-10-16 11:46:41 -0700 | [diff] [blame] | 21 | import static com.android.launcher3.LauncherAnimUtils.SPRING_LOADED_TRANSITION_MS; |
Jon Miranda | 4334874 | 2019-03-06 10:40:13 -0800 | [diff] [blame] | 22 | import static com.android.launcher3.LauncherSettings.Favorites.ITEM_TYPE_APPLICATION; |
| 23 | import static com.android.launcher3.LauncherSettings.Favorites.ITEM_TYPE_DEEP_SHORTCUT; |
| 24 | import static com.android.launcher3.LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT; |
Sunny Goyal | 3e3f44c | 2017-10-23 17:14:52 -0700 | [diff] [blame] | 25 | import static com.android.launcher3.LauncherState.ALL_APPS; |
| 26 | import static com.android.launcher3.LauncherState.NORMAL; |
| 27 | import static com.android.launcher3.LauncherState.SPRING_LOADED; |
Jon Miranda | 4334874 | 2019-03-06 10:40:13 -0800 | [diff] [blame] | 28 | import static com.android.launcher3.config.FeatureFlags.ADAPTIVE_ICON_WINDOW_ANIM; |
Sunny Goyal | 6001ea2 | 2018-05-10 16:31:00 -0700 | [diff] [blame] | 29 | import static com.android.launcher3.dragndrop.DragLayer.ALPHA_INDEX_OVERLAY; |
Sunny Goyal | aeb1643 | 2017-10-16 11:46:41 -0700 | [diff] [blame] | 30 | |
Michael Jurka | 0280c3b | 2010-09-17 15:00:07 -0700 | [diff] [blame] | 31 | import android.animation.Animator; |
Adam Cohen | f358a4b | 2013-07-23 16:47:31 -0700 | [diff] [blame] | 32 | import android.animation.AnimatorListenerAdapter; |
Adam Cohen | 22cba7f | 2013-07-19 16:14:00 -0700 | [diff] [blame] | 33 | import android.animation.LayoutTransition; |
Michael Jurka | 0280c3b | 2010-09-17 15:00:07 -0700 | [diff] [blame] | 34 | import android.animation.ObjectAnimator; |
Sunny Goyal | 6d64d88 | 2016-06-24 09:57:04 -0700 | [diff] [blame] | 35 | import android.animation.ValueAnimator; |
| 36 | import android.animation.ValueAnimator.AnimatorUpdateListener; |
Sunny Goyal | 7066003 | 2015-05-14 00:07:08 -0700 | [diff] [blame] | 37 | import android.annotation.SuppressLint; |
Dianne Hackborn | 8f57395 | 2009-08-10 23:21:09 -0700 | [diff] [blame] | 38 | import android.app.WallpaperManager; |
Michael Jurka | bed61d2 | 2012-02-14 22:51:29 -0800 | [diff] [blame] | 39 | import android.appwidget.AppWidgetHostView; |
Romain Guy | 629de3e | 2010-01-13 12:20:59 -0800 | [diff] [blame] | 40 | import android.appwidget.AppWidgetProviderInfo; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 41 | import android.content.Context; |
Patrick Dubroy | 7247f63 | 2010-08-04 16:02:59 -0700 | [diff] [blame] | 42 | import android.content.res.Resources; |
Joe Onorato | 4be866d | 2010-10-10 11:26:02 -0700 | [diff] [blame] | 43 | import android.graphics.Bitmap; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 44 | import android.graphics.Canvas; |
Winson Chung | b8c69f3 | 2011-10-19 21:36:08 -0700 | [diff] [blame] | 45 | import android.graphics.Point; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 46 | import android.graphics.Rect; |
The Android Open Source Project | f96811c | 2009-03-18 17:39:48 -0700 | [diff] [blame] | 47 | import android.graphics.drawable.Drawable; |
Sunny Goyal | 0fc1be1 | 2014-08-11 17:05:23 -0700 | [diff] [blame] | 48 | import android.os.Handler; |
Joe Onorato | 956091b | 2010-02-19 12:47:40 -0800 | [diff] [blame] | 49 | import android.os.IBinder; |
Sunny Goyal | 2db5342 | 2019-03-01 16:06:12 -0800 | [diff] [blame] | 50 | import android.os.Message; |
Adam Powell | 495f289 | 2010-04-16 16:40:55 -0700 | [diff] [blame] | 51 | import android.os.Parcelable; |
Sunny Goyal | 7c74e4a | 2016-12-15 15:53:17 -0800 | [diff] [blame] | 52 | import android.os.UserHandle; |
Jon Miranda | 4334874 | 2019-03-06 10:40:13 -0800 | [diff] [blame] | 53 | import android.text.TextUtils; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 54 | import android.util.AttributeSet; |
Daniel Sandler | 291ad12 | 2010-05-24 16:03:53 -0400 | [diff] [blame] | 55 | import android.util.Log; |
Adam Cohen | 1462de3 | 2012-07-24 22:34:36 -0700 | [diff] [blame] | 56 | import android.util.SparseArray; |
Sunny Goyal | 9b29ca5 | 2017-02-17 10:39:44 -0800 | [diff] [blame] | 57 | import android.view.LayoutInflater; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 58 | import android.view.MotionEvent; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 59 | import android.view.View; |
Winson Chung | 6e31408 | 2011-01-27 16:46:51 -0800 | [diff] [blame] | 60 | import android.view.ViewGroup; |
Tony Wickham | 618a504 | 2018-02-12 15:59:15 -0800 | [diff] [blame] | 61 | import android.view.ViewTreeObserver; |
Vadim Tryshev | 7acde48 | 2018-05-31 14:33:09 -0700 | [diff] [blame] | 62 | import android.view.accessibility.AccessibilityNodeInfo; |
Jon Miranda | 9485e5f | 2016-10-18 11:47:42 -0700 | [diff] [blame] | 63 | import android.widget.Toast; |
Sunny Goyal | 64a75aa | 2017-07-03 13:50:52 -0700 | [diff] [blame] | 64 | |
Adam Cohen | c2d6e89 | 2014-10-16 09:49:24 -0700 | [diff] [blame] | 65 | import com.android.launcher3.Launcher.LauncherOverlay; |
Sunny Goyal | 64a75aa | 2017-07-03 13:50:52 -0700 | [diff] [blame] | 66 | import com.android.launcher3.LauncherAppWidgetHost.ProviderChangedListener; |
Sunny Goyal | 3e3f44c | 2017-10-23 17:14:52 -0700 | [diff] [blame] | 67 | import com.android.launcher3.LauncherStateManager.AnimationConfig; |
Hyunyoung Song | 0de0117 | 2016-10-05 16:27:48 -0700 | [diff] [blame] | 68 | import com.android.launcher3.accessibility.AccessibleDragListenerAdapter; |
Sunny Goyal | e78e3d7 | 2015-09-24 11:23:31 -0700 | [diff] [blame] | 69 | import com.android.launcher3.accessibility.WorkspaceAccessibilityHelper; |
Sunny Goyal | e3c6d58 | 2017-12-07 12:45:49 -0800 | [diff] [blame] | 70 | import com.android.launcher3.anim.AnimatorSetBuilder; |
Sunny Goyal | 5bc6b6f | 2017-10-26 15:36:10 -0700 | [diff] [blame] | 71 | import com.android.launcher3.anim.Interpolators; |
Tony Wickham | f34bee8 | 2018-12-03 18:11:39 -0800 | [diff] [blame] | 72 | import com.android.launcher3.dot.FolderDotInfo; |
Sunny Goyal | 470f900 | 2016-03-21 13:02:29 -0700 | [diff] [blame] | 73 | import com.android.launcher3.compat.AppWidgetManagerCompat; |
Tony Wickham | e0c3323 | 2016-02-08 11:37:04 -0800 | [diff] [blame] | 74 | import com.android.launcher3.config.FeatureFlags; |
Vadim Tryshev | fedca43 | 2015-08-19 17:55:02 -0700 | [diff] [blame] | 75 | import com.android.launcher3.dragndrop.DragController; |
| 76 | import com.android.launcher3.dragndrop.DragLayer; |
Sunny Goyal | 94b510c | 2016-08-16 15:36:48 -0700 | [diff] [blame] | 77 | import com.android.launcher3.dragndrop.DragOptions; |
Vadim Tryshev | fedca43 | 2015-08-19 17:55:02 -0700 | [diff] [blame] | 78 | import com.android.launcher3.dragndrop.DragView; |
| 79 | import com.android.launcher3.dragndrop.SpringLoadedDragController; |
Sunny Goyal | 470f900 | 2016-03-21 13:02:29 -0700 | [diff] [blame] | 80 | import com.android.launcher3.folder.Folder; |
| 81 | import com.android.launcher3.folder.FolderIcon; |
Jon Miranda | a0233f7 | 2017-06-22 18:34:45 -0700 | [diff] [blame] | 82 | import com.android.launcher3.folder.PreviewBackground; |
Sunny Goyal | 06e21a2 | 2016-08-11 16:02:02 -0700 | [diff] [blame] | 83 | import com.android.launcher3.graphics.DragPreviewProvider; |
Sunny Goyal | 96ac68a | 2017-02-02 16:37:21 -0800 | [diff] [blame] | 84 | import com.android.launcher3.graphics.PreloadIconDrawable; |
Sunny Goyal | 0bd7f4f | 2018-02-01 09:53:35 -0800 | [diff] [blame] | 85 | import com.android.launcher3.pageindicators.WorkspacePageIndicator; |
Tony Wickham | 540913e | 2017-01-23 11:47:51 -0800 | [diff] [blame] | 86 | import com.android.launcher3.popup.PopupContainerWithArrow; |
Jon Miranda | aa6f66a | 2017-05-09 12:43:44 -0700 | [diff] [blame] | 87 | import com.android.launcher3.shortcuts.ShortcutDragPreviewProvider; |
Sunny Goyal | 7c7be8c | 2018-03-07 19:58:07 -0800 | [diff] [blame] | 88 | import com.android.launcher3.touch.WorkspaceTouchListener; |
Hyunyoung Song | 3f5a11f | 2017-06-29 16:42:04 -0700 | [diff] [blame] | 89 | import com.android.launcher3.userevent.nano.LauncherLogProto.Action; |
Sunny Goyal | 6c46a6d | 2016-11-23 02:24:32 +0530 | [diff] [blame] | 90 | import com.android.launcher3.userevent.nano.LauncherLogProto.ContainerType; |
Hyunyoung Song | ddec1c7 | 2016-04-12 18:32:04 -0700 | [diff] [blame] | 91 | import com.android.launcher3.userevent.nano.LauncherLogProto.Target; |
Sunny Goyal | efb7e84 | 2018-10-04 15:11:00 -0700 | [diff] [blame] | 92 | import com.android.launcher3.util.IntArray; |
Sunny Goyal | efb7e84 | 2018-10-04 15:11:00 -0700 | [diff] [blame] | 93 | import com.android.launcher3.util.IntSet; |
Sunny Goyal | 876e462 | 2018-11-02 13:50:40 -0700 | [diff] [blame] | 94 | import com.android.launcher3.util.IntSparseArrayMap; |
Sunny Goyal | d3b87ef | 2016-07-28 12:11:54 -0700 | [diff] [blame] | 95 | import com.android.launcher3.util.ItemInfoMatcher; |
Tony Wickham | 010d255 | 2017-01-20 08:15:28 -0800 | [diff] [blame] | 96 | import com.android.launcher3.util.PackageUserKey; |
Adam Cohen | 091440a | 2015-03-18 14:16:05 -0700 | [diff] [blame] | 97 | import com.android.launcher3.util.Thunk; |
Sunny Goyal | 9eba1fd | 2015-10-16 08:58:57 -0700 | [diff] [blame] | 98 | import com.android.launcher3.util.WallpaperOffsetInterpolator; |
Sunny Goyal | 29947f0 | 2017-12-18 13:49:44 -0800 | [diff] [blame] | 99 | import com.android.launcher3.widget.LauncherAppWidgetHostView; |
Hyunyoung Song | 3f47144 | 2015-04-08 19:01:34 -0700 | [diff] [blame] | 100 | import com.android.launcher3.widget.PendingAddShortcutInfo; |
| 101 | import com.android.launcher3.widget.PendingAddWidgetInfo; |
Sunny Goyal | 29947f0 | 2017-12-18 13:49:44 -0800 | [diff] [blame] | 102 | import com.android.launcher3.widget.PendingAppWidgetHostView; |
Sunny Goyal | 64a75aa | 2017-07-03 13:50:52 -0700 | [diff] [blame] | 103 | |
Adam Cohen | 716b51e | 2011-06-30 12:09:54 -0700 | [diff] [blame] | 104 | import java.util.ArrayList; |
| 105 | import java.util.HashSet; |
Sunny Goyal | aae6fbb | 2019-01-31 16:05:58 -0800 | [diff] [blame] | 106 | import java.util.function.Predicate; |
Adam Cohen | 716b51e | 2011-06-30 12:09:54 -0700 | [diff] [blame] | 107 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 108 | /** |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 109 | * The workspace is a wide area with a wallpaper and a finite number of pages. |
| 110 | * Each page contains a number of icons, folders or widgets the user can |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 111 | * interact with. A workspace is meant to be used with a fixed width only. |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 112 | */ |
Sunny Goyal | 0bd7f4f | 2018-02-01 09:53:35 -0800 | [diff] [blame] | 113 | public class Workspace extends PagedView<WorkspacePageIndicator> |
Sunny Goyal | f5440cb | 2016-12-14 15:13:00 -0800 | [diff] [blame] | 114 | implements DropTarget, DragSource, View.OnTouchListener, |
Sunny Goyal | ef92b82 | 2018-11-21 14:12:00 -0800 | [diff] [blame] | 115 | DragController.DragListener, Insettable, LauncherStateManager.StateHandler, |
| 116 | WorkspaceLayoutManager { |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 117 | |
Tony Wickham | 251ab6e | 2016-12-02 18:54:32 -0800 | [diff] [blame] | 118 | /** The value that {@link #mTransitionProgress} must be greater than for |
| 119 | * {@link #transitionStateShouldAllowDrop()} to return true. */ |
| 120 | private static final float ALLOW_DROP_TRANSITION_PROGRESS = 0.25f; |
| 121 | |
| 122 | /** The value that {@link #mTransitionProgress} must be greater than for |
| 123 | * {@link #isFinishedSwitchingState()} ()} to return true. */ |
| 124 | private static final float FINISHED_SWITCHING_STATE_TRANSITION_PROGRESS = 0.5f; |
| 125 | |
Rajeev Kumar | 93c0512 | 2017-06-12 12:59:47 -0700 | [diff] [blame] | 126 | private static final boolean ENFORCE_DRAG_EVENT_ORDER = false; |
Sunny Goyal | 5837692 | 2015-05-26 18:56:52 -0700 | [diff] [blame] | 127 | |
Sunny Goyal | cf25b52 | 2015-07-09 00:01:18 -0700 | [diff] [blame] | 128 | private static final int SNAP_OFF_EMPTY_SCREEN_DURATION = 400; |
| 129 | private static final int FADE_EMPTY_SCREEN_DURATION = 150; |
Adam Cohen | ad4e15c | 2013-10-17 16:21:35 -0700 | [diff] [blame] | 130 | |
Adam Cohen | ed51cc9 | 2011-08-01 20:28:08 -0700 | [diff] [blame] | 131 | private static final int ADJACENT_SCREEN_DROP_DURATION = 300; |
| 132 | |
Sunny Goyal | 7ce471b | 2017-08-02 03:37:39 -0700 | [diff] [blame] | 133 | private static final int DEFAULT_PAGE = 0; |
| 134 | |
Sunny Goyal | d0ae492 | 2018-10-11 10:37:53 -0700 | [diff] [blame] | 135 | public static final boolean MAP_NO_RECURSE = false; |
| 136 | public static final boolean MAP_RECURSE = true; |
Sunny Goyal | cf25b52 | 2015-07-09 00:01:18 -0700 | [diff] [blame] | 137 | |
Adam Cohen | 22cba7f | 2013-07-19 16:14:00 -0700 | [diff] [blame] | 138 | private LayoutTransition mLayoutTransition; |
Adam Cohen | 091440a | 2015-03-18 14:16:05 -0700 | [diff] [blame] | 139 | @Thunk final WallpaperManager mWallpaperManager; |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 140 | |
Adam Cohen | 5084cba | 2013-09-03 12:01:16 -0700 | [diff] [blame] | 141 | private ShortcutAndWidgetContainer mDragSourceInternal; |
| 142 | |
Sunny Goyal | efb7e84 | 2018-10-04 15:11:00 -0700 | [diff] [blame] | 143 | @Thunk final IntSparseArrayMap<CellLayout> mWorkspaceScreens = new IntSparseArrayMap<>(); |
| 144 | @Thunk final IntArray mScreenOrder = new IntArray(); |
Adam Cohen | dcd297f | 2013-06-18 13:13:40 -0700 | [diff] [blame] | 145 | |
Adam Cohen | 091440a | 2015-03-18 14:16:05 -0700 | [diff] [blame] | 146 | @Thunk Runnable mRemoveEmptyScreenRunnable; |
| 147 | @Thunk boolean mDeferRemoveExtraEmptyScreen = false; |
Adam Cohen | ad4e15c | 2013-10-17 16:21:35 -0700 | [diff] [blame] | 148 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 149 | /** |
| 150 | * CellInfo for the cell that is currently being dragged |
| 151 | */ |
| 152 | private CellLayout.CellInfo mDragInfo; |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 153 | |
Jeff Sharkey | 7086428 | 2009-04-07 21:08:40 -0700 | [diff] [blame] | 154 | /** |
| 155 | * Target drop area calculated during last acceptDrop call. |
| 156 | */ |
Adam Cohen | 091440a | 2015-03-18 14:16:05 -0700 | [diff] [blame] | 157 | @Thunk int[] mTargetCell = new int[2]; |
Adam Cohen | c6cc61d | 2012-04-04 12:47:08 -0700 | [diff] [blame] | 158 | private int mDragOverX = -1; |
| 159 | private int mDragOverY = -1; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 160 | |
Patrick Dubroy | 6569f2c | 2010-07-12 14:25:18 -0700 | [diff] [blame] | 161 | /** |
| 162 | * The CellLayout that is currently being dragged over |
| 163 | */ |
Adam Cohen | 091440a | 2015-03-18 14:16:05 -0700 | [diff] [blame] | 164 | @Thunk CellLayout mDragTargetLayout = null; |
Adam Cohen | c6cc61d | 2012-04-04 12:47:08 -0700 | [diff] [blame] | 165 | /** |
Tony Wickham | 95cdb3a | 2016-02-18 14:37:07 -0800 | [diff] [blame] | 166 | * The CellLayout that we will show as highlighted |
Adam Cohen | c6cc61d | 2012-04-04 12:47:08 -0700 | [diff] [blame] | 167 | */ |
| 168 | private CellLayout mDragOverlappingLayout = null; |
| 169 | |
| 170 | /** |
| 171 | * The CellLayout which will be dropped to |
| 172 | */ |
| 173 | private CellLayout mDropToLayout = null; |
| 174 | |
Rajeev Kumar | 93c0512 | 2017-06-12 12:59:47 -0700 | [diff] [blame] | 175 | @Thunk final Launcher mLauncher; |
Adam Cohen | 091440a | 2015-03-18 14:16:05 -0700 | [diff] [blame] | 176 | @Thunk DragController mDragController; |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 177 | |
Sunny Goyal | cf25b52 | 2015-07-09 00:01:18 -0700 | [diff] [blame] | 178 | private final int[] mTempXY = new int[2]; |
Adam Cohen | 091440a | 2015-03-18 14:16:05 -0700 | [diff] [blame] | 179 | @Thunk float[] mDragViewVisualCenter = new float[2]; |
Rajeev Kumar | 93c0512 | 2017-06-12 12:59:47 -0700 | [diff] [blame] | 180 | private final float[] mTempTouchCoordinates = new float[2]; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 181 | |
Michael Jurka | c2f7f47 | 2010-12-14 15:34:42 -0800 | [diff] [blame] | 182 | private SpringLoadedDragController mSpringLoadedDragController; |
Michael Jurka | d3ef306 | 2010-11-23 16:23:58 -0800 | [diff] [blame] | 183 | |
Michael Jurka | d74c984 | 2011-07-10 12:44:21 -0700 | [diff] [blame] | 184 | private boolean mIsSwitchingState = false; |
Michael Jurka | d74c984 | 2011-07-10 12:44:21 -0700 | [diff] [blame] | 185 | |
Michael Jurka | d74c984 | 2011-07-10 12:44:21 -0700 | [diff] [blame] | 186 | boolean mChildrenLayersEnabled = true; |
Michael Jurka | dee0589 | 2010-07-27 10:01:56 -0700 | [diff] [blame] | 187 | |
Adam Cohen | accfd56 | 2013-07-12 14:40:40 -0700 | [diff] [blame] | 188 | private boolean mStripScreensOnPageStopMoving = false; |
| 189 | |
Sunny Goyal | 06e21a2 | 2016-08-11 16:02:02 -0700 | [diff] [blame] | 190 | private DragPreviewProvider mOutlineProvider = null; |
Sunny Goyal | 12069e6 | 2018-01-16 14:17:20 -0800 | [diff] [blame] | 191 | private boolean mWorkspaceFadeInAdjacentScreens; |
Joe Onorato | 4be866d | 2010-10-10 11:26:02 -0700 | [diff] [blame] | 192 | |
Sunny Goyal | 9eba1fd | 2015-10-16 08:58:57 -0700 | [diff] [blame] | 193 | final WallpaperOffsetInterpolator mWallpaperOffset; |
Winson | c7d2e83 | 2016-07-28 12:24:55 -0700 | [diff] [blame] | 194 | private boolean mUnlockWallpaperFromDefaultPageOnLayout; |
Michael Jurka | 2a4f492 | 2014-01-29 16:32:39 +0100 | [diff] [blame] | 195 | |
Adam Cohen | 19072da | 2011-05-31 14:30:45 -0700 | [diff] [blame] | 196 | // Variables relating to the creation of user folders by hovering shortcuts over shortcuts |
Adam Cohen | 482ed82 | 2012-03-02 14:15:13 -0800 | [diff] [blame] | 197 | private static final int FOLDER_CREATION_TIMEOUT = 0; |
Jon Miranda | 217f0ca | 2018-05-02 11:57:27 -0700 | [diff] [blame] | 198 | public static final int REORDER_TIMEOUT = 650; |
Adam Cohen | 19072da | 2011-05-31 14:30:45 -0700 | [diff] [blame] | 199 | private final Alarm mFolderCreationAlarm = new Alarm(); |
Adam Cohen | 482ed82 | 2012-03-02 14:15:13 -0800 | [diff] [blame] | 200 | private final Alarm mReorderAlarm = new Alarm(); |
Jon Miranda | a0233f7 | 2017-06-22 18:34:45 -0700 | [diff] [blame] | 201 | private PreviewBackground mFolderCreateBg; |
Adam Cohen | c6cc61d | 2012-04-04 12:47:08 -0700 | [diff] [blame] | 202 | private FolderIcon mDragOverFolderIcon = null; |
Adam Cohen | 19072da | 2011-05-31 14:30:45 -0700 | [diff] [blame] | 203 | private boolean mCreateUserFolderOnDrop = false; |
Adam Cohen | c6cc61d | 2012-04-04 12:47:08 -0700 | [diff] [blame] | 204 | private boolean mAddToExistingFolderOnDrop = false; |
Adam Cohen | 3aff81c | 2012-05-16 21:01:01 -0700 | [diff] [blame] | 205 | private float mMaxDistanceForFolderCreation; |
Adam Cohen | 073a46f | 2011-05-17 16:28:09 -0700 | [diff] [blame] | 206 | |
Adam Cohen | f8d2823 | 2011-02-01 21:47:00 -0800 | [diff] [blame] | 207 | // Variables relating to touch disambiguation (scrolling workspace vs. scrolling a widget) |
| 208 | private float mXDown; |
| 209 | private float mYDown; |
| 210 | final static float START_DAMPING_TOUCH_SLOP_ANGLE = (float) Math.PI / 6; |
| 211 | final static float MAX_SWIPE_ANGLE = (float) Math.PI / 3; |
| 212 | final static float TOUCH_SLOP_DAMPING_FACTOR = 4; |
| 213 | |
Adam Cohen | ed66b2b | 2012-01-23 17:28:51 -0800 | [diff] [blame] | 214 | // Relating to the animation of items being dropped externally |
Adam Cohen | d41fbf5 | 2012-02-16 23:53:59 -0800 | [diff] [blame] | 215 | public static final int ANIMATE_INTO_POSITION_AND_DISAPPEAR = 0; |
| 216 | public static final int ANIMATE_INTO_POSITION_AND_REMAIN = 1; |
| 217 | public static final int ANIMATE_INTO_POSITION_AND_RESIZE = 2; |
| 218 | public static final int COMPLETE_TWO_STAGE_WIDGET_DROP_ANIMATION = 3; |
| 219 | public static final int CANCEL_TWO_STAGE_WIDGET_DROP_ANIMATION = 4; |
Adam Cohen | ed66b2b | 2012-01-23 17:28:51 -0800 | [diff] [blame] | 220 | |
Adam Cohen | 482ed82 | 2012-03-02 14:15:13 -0800 | [diff] [blame] | 221 | // Related to dragging, folder creation and reordering |
| 222 | private static final int DRAG_MODE_NONE = 0; |
| 223 | private static final int DRAG_MODE_CREATE_FOLDER = 1; |
| 224 | private static final int DRAG_MODE_ADD_TO_FOLDER = 2; |
| 225 | private static final int DRAG_MODE_REORDER = 3; |
| 226 | private int mDragMode = DRAG_MODE_NONE; |
Adam Cohen | 091440a | 2015-03-18 14:16:05 -0700 | [diff] [blame] | 227 | @Thunk int mLastReorderX = -1; |
| 228 | @Thunk int mLastReorderY = -1; |
Adam Cohen | 482ed82 | 2012-03-02 14:15:13 -0800 | [diff] [blame] | 229 | |
Adam Cohen | 1462de3 | 2012-07-24 22:34:36 -0700 | [diff] [blame] | 230 | private SparseArray<Parcelable> mSavedStates; |
Sunny Goyal | efb7e84 | 2018-10-04 15:11:00 -0700 | [diff] [blame] | 231 | private final IntArray mRestoredPages = new IntArray(); |
Adam Cohen | 1462de3 | 2012-07-24 22:34:36 -0700 | [diff] [blame] | 232 | |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 233 | private float mCurrentScale; |
Winson Chung | 7044272 | 2012-02-10 15:43:22 -0800 | [diff] [blame] | 234 | private float mTransitionProgress; |
Adam Cohen | 4b285c5 | 2011-07-21 14:24:06 -0700 | [diff] [blame] | 235 | |
Adam Cohen | c2d6e89 | 2014-10-16 09:49:24 -0700 | [diff] [blame] | 236 | // State related to Launcher Overlay |
| 237 | LauncherOverlay mLauncherOverlay; |
| 238 | boolean mScrollInteractionBegan; |
| 239 | boolean mStartedSendingScrollEvents; |
Hyunyoung Song | 0de0117 | 2016-10-05 16:27:48 -0700 | [diff] [blame] | 240 | float mLastOverlayScroll = 0; |
Hyunyoung Song | 3f5a11f | 2017-06-29 16:42:04 -0700 | [diff] [blame] | 241 | boolean mOverlayShown = false; |
Tony Wickham | 618a504 | 2018-02-12 15:59:15 -0800 | [diff] [blame] | 242 | private Runnable mOnOverlayHiddenCallback; |
Adam Cohen | 8d769d6 | 2017-06-23 17:27:38 -0700 | [diff] [blame] | 243 | |
Tony Wickham | 29d853c | 2015-09-08 10:35:56 -0700 | [diff] [blame] | 244 | private boolean mForceDrawAdjacentPages = false; |
Sunny Goyal | c99cb17 | 2017-10-19 16:15:09 -0700 | [diff] [blame] | 245 | |
Sunny Goyal | c86df47 | 2016-02-25 09:19:38 -0800 | [diff] [blame] | 246 | // Total over scrollX in the overlay direction. |
| 247 | private float mOverlayTranslation; |
Adam Cohen | c2d6e89 | 2014-10-16 09:49:24 -0700 | [diff] [blame] | 248 | |
Winson Chung | dc61c4d | 2015-04-20 18:26:57 -0700 | [diff] [blame] | 249 | // Handles workspace state transitions |
Rajeev Kumar | 93c0512 | 2017-06-12 12:59:47 -0700 | [diff] [blame] | 250 | private final WorkspaceStateTransitionAnimation mStateTransitionAnimation; |
Winson Chung | dc61c4d | 2015-04-20 18:26:57 -0700 | [diff] [blame] | 251 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 252 | /** |
| 253 | * Used to inflate the Workspace from XML. |
| 254 | * |
| 255 | * @param context The application's context. |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 256 | * @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] | 257 | */ |
| 258 | public Workspace(Context context, AttributeSet attrs) { |
| 259 | this(context, attrs, 0); |
| 260 | } |
| 261 | |
| 262 | /** |
| 263 | * Used to inflate the Workspace from XML. |
| 264 | * |
| 265 | * @param context The application's context. |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 266 | * @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] | 267 | * @param defStyle Unused. |
| 268 | */ |
| 269 | public Workspace(Context context, AttributeSet attrs, int defStyle) { |
| 270 | super(context, attrs, defStyle); |
Michael Jurka | 5f1c509 | 2010-09-03 14:15:02 -0700 | [diff] [blame] | 271 | |
Tony | 2fd0208 | 2016-10-07 12:50:01 -0700 | [diff] [blame] | 272 | mLauncher = Launcher.getLauncher(context); |
Winson Chung | dc61c4d | 2015-04-20 18:26:57 -0700 | [diff] [blame] | 273 | mStateTransitionAnimation = new WorkspaceStateTransitionAnimation(mLauncher, this); |
Dianne Hackborn | 8f57395 | 2009-08-10 23:21:09 -0700 | [diff] [blame] | 274 | mWallpaperManager = WallpaperManager.getInstance(context); |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 275 | |
Sunny Goyal | 9eba1fd | 2015-10-16 08:58:57 -0700 | [diff] [blame] | 276 | mWallpaperOffset = new WallpaperOffsetInterpolator(this); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 277 | |
Joe Onorato | 0d44e94 | 2009-11-16 18:20:51 -0800 | [diff] [blame] | 278 | setHapticFeedbackEnabled(false); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 279 | initWorkspace(); |
Winson Chung | c35afb2 | 2011-02-23 13:01:49 -0800 | [diff] [blame] | 280 | |
| 281 | // Disable multitouch across the workspace/all apps/customize tray |
| 282 | setMotionEventSplittingEnabled(true); |
Sunny Goyal | 7c7be8c | 2018-03-07 19:58:07 -0800 | [diff] [blame] | 283 | setOnTouchListener(new WorkspaceTouchListener(mLauncher, this)); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 284 | } |
| 285 | |
John Spurlock | 77e1f47 | 2013-09-11 10:09:51 -0400 | [diff] [blame] | 286 | @Override |
| 287 | public void setInsets(Rect insets) { |
| 288 | mInsets.set(insets); |
Sunny Goyal | 07b6929 | 2018-01-08 14:19:34 -0800 | [diff] [blame] | 289 | |
| 290 | DeviceProfile grid = mLauncher.getDeviceProfile(); |
Jon Miranda | 5aab870 | 2018-07-17 13:49:12 -0700 | [diff] [blame] | 291 | mMaxDistanceForFolderCreation = grid.isTablet |
| 292 | ? 0.75f * grid.iconSizePx |
| 293 | : 0.55f * grid.iconSizePx; |
Sunny Goyal | 12069e6 | 2018-01-16 14:17:20 -0800 | [diff] [blame] | 294 | mWorkspaceFadeInAdjacentScreens = grid.shouldFadeAdjacentWorkspaceScreens(); |
| 295 | |
Sunny Goyal | 07b6929 | 2018-01-08 14:19:34 -0800 | [diff] [blame] | 296 | Rect padding = grid.workspacePadding; |
| 297 | setPadding(padding.left, padding.top, padding.right, padding.bottom); |
| 298 | |
| 299 | if (grid.shouldFadeAdjacentWorkspaceScreens()) { |
| 300 | // In landscape mode the page spacing is set to the default. |
| 301 | setPageSpacing(grid.defaultPageSpacingPx); |
| 302 | } else { |
| 303 | // In portrait, we want the pages spaced such that there is no |
| 304 | // overhang of the previous / next page into the current page viewport. |
| 305 | // We assume symmetrical padding in portrait mode. |
| 306 | setPageSpacing(Math.max(grid.defaultPageSpacingPx, padding.left + 1)); |
| 307 | } |
Sunny Goyal | 12069e6 | 2018-01-16 14:17:20 -0800 | [diff] [blame] | 308 | |
| 309 | int paddingLeftRight = grid.cellLayoutPaddingLeftRightPx; |
| 310 | int paddingBottom = grid.cellLayoutBottomPaddingPx; |
| 311 | for (int i = mWorkspaceScreens.size() - 1; i >= 0; i--) { |
| 312 | mWorkspaceScreens.valueAt(i) |
| 313 | .setPadding(paddingLeftRight, 0, paddingLeftRight, paddingBottom); |
| 314 | } |
John Spurlock | 77e1f47 | 2013-09-11 10:09:51 -0400 | [diff] [blame] | 315 | } |
| 316 | |
Jon Miranda | e96798e | 2016-12-07 12:10:44 -0800 | [diff] [blame] | 317 | /** |
| 318 | * Estimates the size of an item using spans: hSpan, vSpan. |
| 319 | * |
Jon Miranda | e96798e | 2016-12-07 12:10:44 -0800 | [diff] [blame] | 320 | * @return MAX_VALUE for each dimension if unsuccessful. |
| 321 | */ |
Sunny Goyal | aa3a2ba | 2017-09-26 12:43:16 -0700 | [diff] [blame] | 322 | public int[] estimateItemSize(ItemInfo itemInfo) { |
Michael Jurka | 038f9d8 | 2011-11-03 13:50:45 -0700 | [diff] [blame] | 323 | int[] size = new int[2]; |
| 324 | if (getChildCount() > 0) { |
Sunny Goyal | 7ce471b | 2017-08-02 03:37:39 -0700 | [diff] [blame] | 325 | // Use the first page to estimate the child position |
| 326 | CellLayout cl = (CellLayout) getChildAt(0); |
Jon Miranda | e96798e | 2016-12-07 12:10:44 -0800 | [diff] [blame] | 327 | boolean isWidget = itemInfo.itemType == LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET; |
| 328 | |
Tony Wickham | a501d49 | 2015-11-03 18:05:01 -0800 | [diff] [blame] | 329 | Rect r = estimateItemPosition(cl, 0, 0, itemInfo.spanX, itemInfo.spanY); |
Jon Miranda | e96798e | 2016-12-07 12:10:44 -0800 | [diff] [blame] | 330 | |
| 331 | float scale = 1; |
| 332 | if (isWidget) { |
| 333 | DeviceProfile profile = mLauncher.getDeviceProfile(); |
Jon Miranda | 6f6a06a | 2016-12-15 11:24:18 -0800 | [diff] [blame] | 334 | scale = Utilities.shrinkRect(r, profile.appWidgetScale.x, profile.appWidgetScale.y); |
Jon Miranda | e96798e | 2016-12-07 12:10:44 -0800 | [diff] [blame] | 335 | } |
Adam Cohen | d41fbf5 | 2012-02-16 23:53:59 -0800 | [diff] [blame] | 336 | size[0] = r.width(); |
| 337 | size[1] = r.height(); |
Jon Miranda | e96798e | 2016-12-07 12:10:44 -0800 | [diff] [blame] | 338 | |
Sunny Goyal | aa3a2ba | 2017-09-26 12:43:16 -0700 | [diff] [blame] | 339 | if (isWidget) { |
Jon Miranda | e96798e | 2016-12-07 12:10:44 -0800 | [diff] [blame] | 340 | size[0] /= scale; |
| 341 | size[1] /= scale; |
| 342 | } |
Michael Jurka | 038f9d8 | 2011-11-03 13:50:45 -0700 | [diff] [blame] | 343 | return size; |
| 344 | } else { |
| 345 | size[0] = Integer.MAX_VALUE; |
| 346 | size[1] = Integer.MAX_VALUE; |
| 347 | return size; |
| 348 | } |
| 349 | } |
Adam Cohen | dcd297f | 2013-06-18 13:13:40 -0700 | [diff] [blame] | 350 | |
Sunny Goyal | 2fd7a8b | 2018-03-30 17:10:13 -0700 | [diff] [blame] | 351 | public float getWallpaperOffsetForCenterPage() { |
| 352 | int pageScroll = getScrollForPage(getPageNearestToCenterOfScreen()); |
| 353 | return mWallpaperOffset.wallpaperOffsetForScroll(pageScroll); |
| 354 | } |
| 355 | |
Tony Wickham | a501d49 | 2015-11-03 18:05:01 -0800 | [diff] [blame] | 356 | public Rect estimateItemPosition(CellLayout cl, int hCell, int vCell, int hSpan, int vSpan) { |
Adam Cohen | d41fbf5 | 2012-02-16 23:53:59 -0800 | [diff] [blame] | 357 | Rect r = new Rect(); |
Michael Jurka | 038f9d8 | 2011-11-03 13:50:45 -0700 | [diff] [blame] | 358 | cl.cellToRect(hCell, vCell, hSpan, vSpan, r); |
Michael Jurka | 038f9d8 | 2011-11-03 13:50:45 -0700 | [diff] [blame] | 359 | return r; |
| 360 | } |
| 361 | |
Sunny Goyal | 5837692 | 2015-05-26 18:56:52 -0700 | [diff] [blame] | 362 | @Override |
Sunny Goyal | 94b510c | 2016-08-16 15:36:48 -0700 | [diff] [blame] | 363 | public void onDragStart(DropTarget.DragObject dragObject, DragOptions options) { |
Sunny Goyal | 5837692 | 2015-05-26 18:56:52 -0700 | [diff] [blame] | 364 | if (ENFORCE_DRAG_EVENT_ORDER) { |
Hyunyoung Song | 0de0117 | 2016-10-05 16:27:48 -0700 | [diff] [blame] | 365 | enforceDragParity("onDragStart", 0, 0); |
Sunny Goyal | 5837692 | 2015-05-26 18:56:52 -0700 | [diff] [blame] | 366 | } |
| 367 | |
Tony Wickham | 4e47b5b | 2016-10-18 14:43:01 -0700 | [diff] [blame] | 368 | if (mDragInfo != null && mDragInfo.cell != null) { |
| 369 | CellLayout layout = (CellLayout) mDragInfo.cell.getParent().getParent(); |
| 370 | layout.markCellsAsUnoccupiedForView(mDragInfo.cell); |
| 371 | } |
| 372 | |
Sunny Goyal | 06e21a2 | 2016-08-11 16:02:02 -0700 | [diff] [blame] | 373 | if (mOutlineProvider != null) { |
Sunny Goyal | aa3a2ba | 2017-09-26 12:43:16 -0700 | [diff] [blame] | 374 | if (dragObject.dragView != null) { |
| 375 | Bitmap preview = dragObject.dragView.getPreviewBitmap(); |
| 376 | |
| 377 | // The outline is used to visualize where the item will land if dropped |
| 378 | mOutlineProvider.generateDragOutline(preview); |
| 379 | } |
Sunny Goyal | 06e21a2 | 2016-08-11 16:02:02 -0700 | [diff] [blame] | 380 | } |
| 381 | |
Sunny Goyal | a502aa3 | 2017-10-02 16:04:06 -0700 | [diff] [blame] | 382 | updateChildrenLayersEnabled(); |
Sunny Goyal | 5837692 | 2015-05-26 18:56:52 -0700 | [diff] [blame] | 383 | |
Sunny Goyal | 94b510c | 2016-08-16 15:36:48 -0700 | [diff] [blame] | 384 | // Do not add a new page if it is a accessible drag which was not started by the workspace. |
| 385 | // We do not support accessibility drag from other sources and instead provide a direct |
| 386 | // action for move/add to homescreen. |
| 387 | // When a accessible drag is started by the folder, we only allow rearranging withing the |
| 388 | // folder. |
| 389 | boolean addNewPage = !(options.isAccessibleDrag && dragObject.dragSource != this); |
| 390 | |
| 391 | if (addNewPage) { |
Sunny Goyal | 5837692 | 2015-05-26 18:56:52 -0700 | [diff] [blame] | 392 | mDeferRemoveExtraEmptyScreen = false; |
| 393 | addExtraEmptyScreenOnDrag(); |
Sunny Goyal | 06e21a2 | 2016-08-11 16:02:02 -0700 | [diff] [blame] | 394 | |
Sunny Goyal | 94b510c | 2016-08-16 15:36:48 -0700 | [diff] [blame] | 395 | if (dragObject.dragInfo.itemType == LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET |
| 396 | && dragObject.dragSource != this) { |
Sunny Goyal | 06e21a2 | 2016-08-11 16:02:02 -0700 | [diff] [blame] | 397 | // When dragging a widget from different source, move to a page which has |
| 398 | // enough space to place this widget (after rearranging/resizing). We special case |
| 399 | // widgets as they cannot be placed inside a folder. |
| 400 | // Start at the current page and search right (on LTR) until finding a page with |
| 401 | // enough space. Since an empty screen is the furthest right, a page must be found. |
| 402 | int currentPage = getPageNearestToCenterOfScreen(); |
| 403 | for (int pageIndex = currentPage; pageIndex < getPageCount(); pageIndex++) { |
| 404 | CellLayout page = (CellLayout) getPageAt(pageIndex); |
Sunny Goyal | 94b510c | 2016-08-16 15:36:48 -0700 | [diff] [blame] | 405 | if (page.hasReorderSolution(dragObject.dragInfo)) { |
Sunny Goyal | 06e21a2 | 2016-08-11 16:02:02 -0700 | [diff] [blame] | 406 | setCurrentPage(pageIndex); |
| 407 | break; |
| 408 | } |
| 409 | } |
| 410 | } |
| 411 | } |
| 412 | |
Sunny Goyal | f5440cb | 2016-12-14 15:13:00 -0800 | [diff] [blame] | 413 | // Always enter the spring loaded mode |
Sunny Goyal | 3e3f44c | 2017-10-23 17:14:52 -0700 | [diff] [blame] | 414 | mLauncher.getStateManager().goToState(SPRING_LOADED); |
Michael Jurka | d74c984 | 2011-07-10 12:44:21 -0700 | [diff] [blame] | 415 | } |
| 416 | |
Adam Cohen | 689ff16 | 2014-05-08 17:27:56 -0700 | [diff] [blame] | 417 | public void deferRemoveExtraEmptyScreen() { |
| 418 | mDeferRemoveExtraEmptyScreen = true; |
| 419 | } |
| 420 | |
Sunny Goyal | 5837692 | 2015-05-26 18:56:52 -0700 | [diff] [blame] | 421 | @Override |
Michael Jurka | d74c984 | 2011-07-10 12:44:21 -0700 | [diff] [blame] | 422 | public void onDragEnd() { |
Sunny Goyal | 5837692 | 2015-05-26 18:56:52 -0700 | [diff] [blame] | 423 | if (ENFORCE_DRAG_EVENT_ORDER) { |
Hyunyoung Song | 0de0117 | 2016-10-05 16:27:48 -0700 | [diff] [blame] | 424 | enforceDragParity("onDragEnd", 0, 0); |
Sunny Goyal | 5837692 | 2015-05-26 18:56:52 -0700 | [diff] [blame] | 425 | } |
| 426 | |
Adam Cohen | 689ff16 | 2014-05-08 17:27:56 -0700 | [diff] [blame] | 427 | if (!mDeferRemoveExtraEmptyScreen) { |
| 428 | removeExtraEmptyScreen(true, mDragSourceInternal != null); |
| 429 | } |
| 430 | |
Sunny Goyal | a502aa3 | 2017-10-02 16:04:06 -0700 | [diff] [blame] | 431 | updateChildrenLayersEnabled(); |
Jon Miranda | 2358518 | 2018-02-09 15:33:37 -0800 | [diff] [blame] | 432 | mDragInfo = null; |
Tony Wickham | 4e47b5b | 2016-10-18 14:43:01 -0700 | [diff] [blame] | 433 | mOutlineProvider = null; |
Adam Cohen | 5084cba | 2013-09-03 12:01:16 -0700 | [diff] [blame] | 434 | mDragSourceInternal = null; |
Michael Jurka | d74c984 | 2011-07-10 12:44:21 -0700 | [diff] [blame] | 435 | } |
| 436 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 437 | /** |
| 438 | * Initializes various states for this workspace. |
| 439 | */ |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 440 | protected void initWorkspace() { |
Sunny Goyal | 7ce471b | 2017-08-02 03:37:39 -0700 | [diff] [blame] | 441 | mCurrentPage = DEFAULT_PAGE; |
Romain Guy | ce3cbd1 | 2013-02-25 15:00:36 -0800 | [diff] [blame] | 442 | setClipToPadding(false); |
Derek Prothro | dadd984 | 2014-01-17 13:43:50 -0500 | [diff] [blame] | 443 | |
Adam Cohen | 22cba7f | 2013-07-19 16:14:00 -0700 | [diff] [blame] | 444 | setupLayoutTransition(); |
Michael Jurka | a6a0547 | 2013-11-13 17:59:46 +0100 | [diff] [blame] | 445 | |
| 446 | // Set the wallpaper dimensions when Launcher starts up |
Adam Cohen | 824fcb3 | 2014-05-21 23:01:25 +0000 | [diff] [blame] | 447 | setWallpaperDimension(); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 448 | } |
| 449 | |
Adam Cohen | 22cba7f | 2013-07-19 16:14:00 -0700 | [diff] [blame] | 450 | private void setupLayoutTransition() { |
| 451 | // We want to show layout transitions when pages are deleted, to close the gap. |
| 452 | mLayoutTransition = new LayoutTransition(); |
| 453 | mLayoutTransition.enableTransitionType(LayoutTransition.DISAPPEARING); |
| 454 | mLayoutTransition.enableTransitionType(LayoutTransition.CHANGE_DISAPPEARING); |
| 455 | mLayoutTransition.disableTransitionType(LayoutTransition.APPEARING); |
| 456 | mLayoutTransition.disableTransitionType(LayoutTransition.CHANGE_APPEARING); |
| 457 | setLayoutTransition(mLayoutTransition); |
| 458 | } |
| 459 | |
Winson Chung | 964df6b | 2013-10-11 15:55:37 -0700 | [diff] [blame] | 460 | void enableLayoutTransitions() { |
| 461 | setLayoutTransition(mLayoutTransition); |
| 462 | } |
| 463 | void disableLayoutTransitions() { |
| 464 | setLayoutTransition(null); |
| 465 | } |
| 466 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 467 | @Override |
Sunny Goyal | 0bd7f4f | 2018-02-01 09:53:35 -0800 | [diff] [blame] | 468 | public void onViewAdded(View child) { |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 469 | if (!(child instanceof CellLayout)) { |
| 470 | throw new IllegalArgumentException("A Workspace can only have CellLayout children."); |
| 471 | } |
Adam Cohen | 2801caf | 2011-05-13 20:57:39 -0700 | [diff] [blame] | 472 | CellLayout cl = ((CellLayout) child); |
| 473 | cl.setOnInterceptTouchListener(this); |
Sunny Goyal | c46bfef | 2015-01-05 12:40:08 -0800 | [diff] [blame] | 474 | cl.setImportantForAccessibility(IMPORTANT_FOR_ACCESSIBILITY_NO); |
Sunny Goyal | 0bd7f4f | 2018-02-01 09:53:35 -0800 | [diff] [blame] | 475 | super.onViewAdded(child); |
Michael Jurka | 8b805b1 | 2012-04-18 14:23:14 -0700 | [diff] [blame] | 476 | } |
| 477 | |
Sunny Goyal | ab770a1 | 2018-11-14 15:17:26 -0800 | [diff] [blame] | 478 | protected boolean onInterceptHotseatTouch(View v, MotionEvent ev) { |
| 479 | // We don't want any clicks to go through to the hotseat unless the workspace is in |
| 480 | // the normal state or an accessible drag is in progress. |
| 481 | return !workspaceIconsCanBeDragged() |
| 482 | && !mLauncher.getAccessibilityDelegate().isInAccessibleDrag(); |
| 483 | } |
| 484 | |
Sunny Goyal | da4fe1a | 2016-05-26 16:05:17 -0700 | [diff] [blame] | 485 | /** |
| 486 | * Initializes and binds the first page |
Hyunyoung Song | 0de0117 | 2016-10-05 16:27:48 -0700 | [diff] [blame] | 487 | * @param qsb an existing qsb to recycle or null. |
Sunny Goyal | da4fe1a | 2016-05-26 16:05:17 -0700 | [diff] [blame] | 488 | */ |
| 489 | public void bindAndInitFirstWorkspaceScreen(View qsb) { |
Jon Miranda | 7143ba6 | 2019-03-15 09:00:05 -0700 | [diff] [blame^] | 490 | if (!FeatureFlags.QSB_ON_FIRST_SCREEN) { |
Sunny Goyal | a9e2f5a | 2016-06-10 12:22:04 -0700 | [diff] [blame] | 491 | return; |
| 492 | } |
Sunny Goyal | da4fe1a | 2016-05-26 16:05:17 -0700 | [diff] [blame] | 493 | // Add the first page |
| 494 | CellLayout firstPage = insertNewWorkspaceScreen(Workspace.FIRST_SCREEN_ID, 0); |
Sunny Goyal | 6178f13 | 2016-07-11 17:30:03 -0700 | [diff] [blame] | 495 | // Always add a QSB on the first screen. |
Sunny Goyal | da4fe1a | 2016-05-26 16:05:17 -0700 | [diff] [blame] | 496 | if (qsb == null) { |
Sunny Goyal | 6178f13 | 2016-07-11 17:30:03 -0700 | [diff] [blame] | 497 | // In transposed layout, we add the QSB in the Grid. As workspace does not touch the |
| 498 | // edges, we do not need a full width QSB. |
Sunny Goyal | 966d901 | 2017-06-06 16:43:59 -0700 | [diff] [blame] | 499 | qsb = LayoutInflater.from(getContext()) |
Sunny Goyal | c7b8df8 | 2017-06-27 11:11:03 -0700 | [diff] [blame] | 500 | .inflate(R.layout.search_container_workspace,firstPage, false); |
Sunny Goyal | da4fe1a | 2016-05-26 16:05:17 -0700 | [diff] [blame] | 501 | } |
| 502 | |
Sunny Goyal | 6178f13 | 2016-07-11 17:30:03 -0700 | [diff] [blame] | 503 | CellLayout.LayoutParams lp = new CellLayout.LayoutParams(0, 0, firstPage.getCountX(), 1); |
Sunny Goyal | da4fe1a | 2016-05-26 16:05:17 -0700 | [diff] [blame] | 504 | lp.canReorder = false; |
Sunny Goyal | c7b8df8 | 2017-06-27 11:11:03 -0700 | [diff] [blame] | 505 | if (!firstPage.addViewToCellLayout(qsb, 0, R.id.search_container_workspace, lp, true)) { |
Sunny Goyal | da4fe1a | 2016-05-26 16:05:17 -0700 | [diff] [blame] | 506 | Log.e(TAG, "Failed to add to item at (0, 0) to CellLayout"); |
| 507 | } |
| 508 | } |
| 509 | |
Winson Chung | 9e6a0a2 | 2013-08-27 11:58:12 -0700 | [diff] [blame] | 510 | public void removeAllWorkspaceScreens() { |
Winson Chung | 964df6b | 2013-10-11 15:55:37 -0700 | [diff] [blame] | 511 | // Disable all layout transitions before removing all pages to ensure that we don't get the |
Sunny Goyal | 7ce471b | 2017-08-02 03:37:39 -0700 | [diff] [blame] | 512 | // transition animations competing with us changing the scroll when we add pages |
Winson Chung | 964df6b | 2013-10-11 15:55:37 -0700 | [diff] [blame] | 513 | disableLayoutTransitions(); |
| 514 | |
Sunny Goyal | da4fe1a | 2016-05-26 16:05:17 -0700 | [diff] [blame] | 515 | // Recycle the QSB widget |
Sunny Goyal | c7b8df8 | 2017-06-27 11:11:03 -0700 | [diff] [blame] | 516 | View qsb = findViewById(R.id.search_container_workspace); |
Sunny Goyal | da4fe1a | 2016-05-26 16:05:17 -0700 | [diff] [blame] | 517 | if (qsb != null) { |
| 518 | ((ViewGroup) qsb.getParent()).removeView(qsb); |
| 519 | } |
| 520 | |
Winson Chung | 9e6a0a2 | 2013-08-27 11:58:12 -0700 | [diff] [blame] | 521 | // Remove the pages and clear the screen models |
Sunny Goyal | 39306af | 2018-05-07 09:49:34 -0700 | [diff] [blame] | 522 | removeFolderListeners(); |
Winson Chung | 9e6a0a2 | 2013-08-27 11:58:12 -0700 | [diff] [blame] | 523 | removeAllViews(); |
| 524 | mScreenOrder.clear(); |
| 525 | mWorkspaceScreens.clear(); |
Winson Chung | 964df6b | 2013-10-11 15:55:37 -0700 | [diff] [blame] | 526 | |
Sunny Goyal | 2db5342 | 2019-03-01 16:06:12 -0800 | [diff] [blame] | 527 | // Remove any deferred refresh callbacks |
| 528 | mLauncher.mHandler.removeCallbacksAndMessages(DeferredWidgetRefresh.class); |
| 529 | |
Sunny Goyal | da4fe1a | 2016-05-26 16:05:17 -0700 | [diff] [blame] | 530 | // Ensure that the first page is always present |
| 531 | bindAndInitFirstWorkspaceScreen(qsb); |
| 532 | |
Winson Chung | 964df6b | 2013-10-11 15:55:37 -0700 | [diff] [blame] | 533 | // Re-enable the layout transitions |
| 534 | enableLayoutTransitions(); |
Winson Chung | 9e6a0a2 | 2013-08-27 11:58:12 -0700 | [diff] [blame] | 535 | } |
| 536 | |
Sunny Goyal | efb7e84 | 2018-10-04 15:11:00 -0700 | [diff] [blame] | 537 | public void insertNewWorkspaceScreenBeforeEmptyScreen(int screenId) { |
Winson Chung | 64359a5 | 2013-07-08 17:17:08 -0700 | [diff] [blame] | 538 | // Find the index to insert this view into. If the empty screen exists, then |
| 539 | // insert it before that. |
| 540 | int insertIndex = mScreenOrder.indexOf(EXTRA_EMPTY_SCREEN_ID); |
| 541 | if (insertIndex < 0) { |
| 542 | insertIndex = mScreenOrder.size(); |
| 543 | } |
Sunny Goyal | da4fe1a | 2016-05-26 16:05:17 -0700 | [diff] [blame] | 544 | insertNewWorkspaceScreen(screenId, insertIndex); |
Adam Cohen | dcd297f | 2013-06-18 13:13:40 -0700 | [diff] [blame] | 545 | } |
| 546 | |
Sunny Goyal | efb7e84 | 2018-10-04 15:11:00 -0700 | [diff] [blame] | 547 | public void insertNewWorkspaceScreen(int screenId) { |
Sunny Goyal | da4fe1a | 2016-05-26 16:05:17 -0700 | [diff] [blame] | 548 | insertNewWorkspaceScreen(screenId, getChildCount()); |
Winson Chung | 64359a5 | 2013-07-08 17:17:08 -0700 | [diff] [blame] | 549 | } |
| 550 | |
Sunny Goyal | efb7e84 | 2018-10-04 15:11:00 -0700 | [diff] [blame] | 551 | public CellLayout insertNewWorkspaceScreen(int screenId, int insertIndex) { |
Adam Cohen | 5084cba | 2013-09-03 12:01:16 -0700 | [diff] [blame] | 552 | if (mWorkspaceScreens.containsKey(screenId)) { |
| 553 | throw new RuntimeException("Screen id " + screenId + " already exists!"); |
| 554 | } |
| 555 | |
Sunny Goyal | 7066003 | 2015-05-14 00:07:08 -0700 | [diff] [blame] | 556 | // Inflate the cell layout, but do not add it automatically so that we can get the newly |
| 557 | // created CellLayout. |
Sunny Goyal | 9b29ca5 | 2017-02-17 10:39:44 -0800 | [diff] [blame] | 558 | CellLayout newScreen = (CellLayout) LayoutInflater.from(getContext()).inflate( |
Sunny Goyal | 7066003 | 2015-05-14 00:07:08 -0700 | [diff] [blame] | 559 | R.layout.workspace_screen, this, false /* attachToRoot */); |
Jon Miranda | 2803200 | 2017-07-13 16:18:56 -0700 | [diff] [blame] | 560 | int paddingLeftRight = mLauncher.getDeviceProfile().cellLayoutPaddingLeftRightPx; |
Jon Miranda | 9593f9e | 2017-08-09 14:57:30 -0700 | [diff] [blame] | 561 | int paddingBottom = mLauncher.getDeviceProfile().cellLayoutBottomPaddingPx; |
| 562 | newScreen.setPadding(paddingLeftRight, 0, paddingLeftRight, paddingBottom); |
Jon Miranda | 2803200 | 2017-07-13 16:18:56 -0700 | [diff] [blame] | 563 | |
Adam Cohen | dcd297f | 2013-06-18 13:13:40 -0700 | [diff] [blame] | 564 | mWorkspaceScreens.put(screenId, newScreen); |
Winson Chung | 64359a5 | 2013-07-08 17:17:08 -0700 | [diff] [blame] | 565 | mScreenOrder.add(insertIndex, screenId); |
| 566 | addView(newScreen, insertIndex); |
Sunny Goyal | ea60926 | 2017-10-25 15:47:38 -0700 | [diff] [blame] | 567 | mStateTransitionAnimation.applyChildState( |
| 568 | mLauncher.getStateManager().getState(), newScreen, insertIndex); |
Adam Cohen | c9735cf | 2015-01-23 16:11:55 -0800 | [diff] [blame] | 569 | |
Sunny Goyal | ae50284 | 2016-06-17 08:43:56 -0700 | [diff] [blame] | 570 | if (mLauncher.getAccessibilityDelegate().isInAccessibleDrag()) { |
Sunny Goyal | e9b651e | 2015-04-24 11:44:51 -0700 | [diff] [blame] | 571 | newScreen.enableAccessibleDrag(true, CellLayout.WORKSPACE_ACCESSIBILITY_DRAG); |
Adam Cohen | c9735cf | 2015-01-23 16:11:55 -0800 | [diff] [blame] | 572 | } |
Sunny Goyal | da4fe1a | 2016-05-26 16:05:17 -0700 | [diff] [blame] | 573 | |
| 574 | return newScreen; |
Adam Cohen | dcd297f | 2013-06-18 13:13:40 -0700 | [diff] [blame] | 575 | } |
| 576 | |
Adam Cohen | 5084cba | 2013-09-03 12:01:16 -0700 | [diff] [blame] | 577 | public void addExtraEmptyScreenOnDrag() { |
| 578 | boolean lastChildOnScreen = false; |
| 579 | boolean childOnFinalScreen = false; |
| 580 | |
Adam Cohen | ad4e15c | 2013-10-17 16:21:35 -0700 | [diff] [blame] | 581 | // Cancel any pending removal of empty screen |
| 582 | mRemoveEmptyScreenRunnable = null; |
| 583 | |
Adam Cohen | 5084cba | 2013-09-03 12:01:16 -0700 | [diff] [blame] | 584 | if (mDragSourceInternal != null) { |
| 585 | if (mDragSourceInternal.getChildCount() == 1) { |
| 586 | lastChildOnScreen = true; |
| 587 | } |
| 588 | CellLayout cl = (CellLayout) mDragSourceInternal.getParent(); |
| 589 | if (indexOfChild(cl) == getChildCount() - 1) { |
| 590 | childOnFinalScreen = true; |
| 591 | } |
| 592 | } |
| 593 | |
| 594 | // If this is the last item on the final screen |
| 595 | if (lastChildOnScreen && childOnFinalScreen) { |
| 596 | return; |
| 597 | } |
| 598 | if (!mWorkspaceScreens.containsKey(EXTRA_EMPTY_SCREEN_ID)) { |
| 599 | insertNewWorkspaceScreen(EXTRA_EMPTY_SCREEN_ID); |
| 600 | } |
| 601 | } |
| 602 | |
| 603 | public boolean addExtraEmptyScreen() { |
| 604 | if (!mWorkspaceScreens.containsKey(EXTRA_EMPTY_SCREEN_ID)) { |
| 605 | insertNewWorkspaceScreen(EXTRA_EMPTY_SCREEN_ID); |
| 606 | return true; |
| 607 | } |
| 608 | return false; |
| 609 | } |
| 610 | |
Adam Cohen | ad4e15c | 2013-10-17 16:21:35 -0700 | [diff] [blame] | 611 | private void convertFinalScreenToEmptyScreenIfNecessary() { |
Adam Cohen | dcb173d | 2014-04-01 13:33:58 -0700 | [diff] [blame] | 612 | if (mLauncher.isWorkspaceLoading()) { |
| 613 | // Invalid and dangerous operation if workspace is loading |
Adam Cohen | dcb173d | 2014-04-01 13:33:58 -0700 | [diff] [blame] | 614 | return; |
| 615 | } |
| 616 | |
Adam Cohen | ad4e15c | 2013-10-17 16:21:35 -0700 | [diff] [blame] | 617 | if (hasExtraEmptyScreen() || mScreenOrder.size() == 0) return; |
Sunny Goyal | efb7e84 | 2018-10-04 15:11:00 -0700 | [diff] [blame] | 618 | int finalScreenId = mScreenOrder.get(mScreenOrder.size() - 1); |
Adam Cohen | ad4e15c | 2013-10-17 16:21:35 -0700 | [diff] [blame] | 619 | |
Adam Cohen | ad4e15c | 2013-10-17 16:21:35 -0700 | [diff] [blame] | 620 | CellLayout finalScreen = mWorkspaceScreens.get(finalScreenId); |
| 621 | |
| 622 | // If the final screen is empty, convert it to the extra empty screen |
Adam Cohen | 917e388 | 2013-10-31 15:03:35 -0700 | [diff] [blame] | 623 | if (finalScreen.getShortcutsAndWidgets().getChildCount() == 0 && |
| 624 | !finalScreen.isDropPending()) { |
Adam Cohen | ad4e15c | 2013-10-17 16:21:35 -0700 | [diff] [blame] | 625 | mWorkspaceScreens.remove(finalScreenId); |
Sunny Goyal | efb7e84 | 2018-10-04 15:11:00 -0700 | [diff] [blame] | 626 | mScreenOrder.removeValue(finalScreenId); |
Adam Cohen | ad4e15c | 2013-10-17 16:21:35 -0700 | [diff] [blame] | 627 | |
Sunny Goyal | 7ce471b | 2017-08-02 03:37:39 -0700 | [diff] [blame] | 628 | // if this is the last screen, convert it to the empty screen |
Adam Cohen | ad4e15c | 2013-10-17 16:21:35 -0700 | [diff] [blame] | 629 | mWorkspaceScreens.put(EXTRA_EMPTY_SCREEN_ID, finalScreen); |
| 630 | mScreenOrder.add(EXTRA_EMPTY_SCREEN_ID); |
Adam Cohen | 5084cba | 2013-09-03 12:01:16 -0700 | [diff] [blame] | 631 | } |
| 632 | } |
| 633 | |
Adam Cohen | 689ff16 | 2014-05-08 17:27:56 -0700 | [diff] [blame] | 634 | public void removeExtraEmptyScreen(final boolean animate, boolean stripEmptyScreens) { |
| 635 | removeExtraEmptyScreenDelayed(animate, null, 0, stripEmptyScreens); |
Adam Cohen | ad4e15c | 2013-10-17 16:21:35 -0700 | [diff] [blame] | 636 | } |
| 637 | |
Adam Cohen | 689ff16 | 2014-05-08 17:27:56 -0700 | [diff] [blame] | 638 | public void removeExtraEmptyScreenDelayed(final boolean animate, final Runnable onComplete, |
Adam Cohen | ad4e15c | 2013-10-17 16:21:35 -0700 | [diff] [blame] | 639 | final int delay, final boolean stripEmptyScreens) { |
Adam Cohen | dcb173d | 2014-04-01 13:33:58 -0700 | [diff] [blame] | 640 | if (mLauncher.isWorkspaceLoading()) { |
| 641 | // Don't strip empty screens if the workspace is still loading |
Adam Cohen | dcb173d | 2014-04-01 13:33:58 -0700 | [diff] [blame] | 642 | return; |
| 643 | } |
| 644 | |
Adam Cohen | ad4e15c | 2013-10-17 16:21:35 -0700 | [diff] [blame] | 645 | if (delay > 0) { |
| 646 | postDelayed(new Runnable() { |
| 647 | @Override |
| 648 | public void run() { |
Adam Cohen | 689ff16 | 2014-05-08 17:27:56 -0700 | [diff] [blame] | 649 | removeExtraEmptyScreenDelayed(animate, onComplete, 0, stripEmptyScreens); |
Adam Cohen | ad4e15c | 2013-10-17 16:21:35 -0700 | [diff] [blame] | 650 | } |
Adam Cohen | ad4e15c | 2013-10-17 16:21:35 -0700 | [diff] [blame] | 651 | }, delay); |
| 652 | return; |
| 653 | } |
| 654 | |
| 655 | convertFinalScreenToEmptyScreenIfNecessary(); |
| 656 | if (hasExtraEmptyScreen()) { |
| 657 | int emptyIndex = mScreenOrder.indexOf(EXTRA_EMPTY_SCREEN_ID); |
| 658 | if (getNextPage() == emptyIndex) { |
| 659 | snapToPage(getNextPage() - 1, SNAP_OFF_EMPTY_SCREEN_DURATION); |
| 660 | fadeAndRemoveEmptyScreen(SNAP_OFF_EMPTY_SCREEN_DURATION, FADE_EMPTY_SCREEN_DURATION, |
| 661 | onComplete, stripEmptyScreens); |
| 662 | } else { |
Sunny Goyal | 2b7bf87 | 2015-05-28 10:06:43 -0700 | [diff] [blame] | 663 | snapToPage(getNextPage(), 0); |
Adam Cohen | ad4e15c | 2013-10-17 16:21:35 -0700 | [diff] [blame] | 664 | fadeAndRemoveEmptyScreen(0, FADE_EMPTY_SCREEN_DURATION, |
| 665 | onComplete, stripEmptyScreens); |
| 666 | } |
| 667 | return; |
Adam Cohen | 3a14eeb | 2013-11-15 16:51:22 +0000 | [diff] [blame] | 668 | } else if (stripEmptyScreens) { |
| 669 | // If we're not going to strip the empty screens after removing |
| 670 | // the extra empty screen, do it right away. |
| 671 | stripEmptyScreens(); |
Adam Cohen | ad4e15c | 2013-10-17 16:21:35 -0700 | [diff] [blame] | 672 | } |
Adam Cohen | 3a14eeb | 2013-11-15 16:51:22 +0000 | [diff] [blame] | 673 | |
Adam Cohen | ad4e15c | 2013-10-17 16:21:35 -0700 | [diff] [blame] | 674 | if (onComplete != null) { |
| 675 | onComplete.run(); |
| 676 | } |
| 677 | } |
| 678 | |
| 679 | private void fadeAndRemoveEmptyScreen(int delay, int duration, final Runnable onComplete, |
| 680 | final boolean stripEmptyScreens) { |
Winson Chung | a90303b | 2013-11-15 13:05:06 -0800 | [diff] [blame] | 681 | // XXX: Do we need to update LM workspace screens below? |
Adam Cohen | ad4e15c | 2013-10-17 16:21:35 -0700 | [diff] [blame] | 682 | final CellLayout cl = mWorkspaceScreens.get(EXTRA_EMPTY_SCREEN_ID); |
| 683 | |
| 684 | mRemoveEmptyScreenRunnable = new Runnable() { |
| 685 | @Override |
| 686 | public void run() { |
| 687 | if (hasExtraEmptyScreen()) { |
| 688 | mWorkspaceScreens.remove(EXTRA_EMPTY_SCREEN_ID); |
Sunny Goyal | efb7e84 | 2018-10-04 15:11:00 -0700 | [diff] [blame] | 689 | mScreenOrder.removeValue(EXTRA_EMPTY_SCREEN_ID); |
Adam Cohen | ad4e15c | 2013-10-17 16:21:35 -0700 | [diff] [blame] | 690 | removeView(cl); |
| 691 | if (stripEmptyScreens) { |
| 692 | stripEmptyScreens(); |
| 693 | } |
Tony Wickham | d4b28d2 | 2016-05-26 13:00:02 -0700 | [diff] [blame] | 694 | // Update the page indicator to reflect the removed page. |
| 695 | showPageIndicatorAtCurrentScroll(); |
Adam Cohen | ad4e15c | 2013-10-17 16:21:35 -0700 | [diff] [blame] | 696 | } |
| 697 | } |
| 698 | }; |
| 699 | |
Sunny Goyal | 55bdeed | 2018-09-18 16:17:22 -0700 | [diff] [blame] | 700 | ObjectAnimator oa = ObjectAnimator.ofFloat(cl, ALPHA, 0f); |
Adam Cohen | ad4e15c | 2013-10-17 16:21:35 -0700 | [diff] [blame] | 701 | oa.setDuration(duration); |
| 702 | oa.setStartDelay(delay); |
| 703 | oa.addListener(new AnimatorListenerAdapter() { |
| 704 | @Override |
| 705 | public void onAnimationEnd(Animator animation) { |
| 706 | if (mRemoveEmptyScreenRunnable != null) { |
| 707 | mRemoveEmptyScreenRunnable.run(); |
| 708 | } |
| 709 | if (onComplete != null) { |
| 710 | onComplete.run(); |
| 711 | } |
| 712 | } |
| 713 | }); |
| 714 | oa.start(); |
| 715 | } |
| 716 | |
Michael Jurka | 2f81731 | 2013-09-20 03:03:42 +0200 | [diff] [blame] | 717 | public boolean hasExtraEmptyScreen() { |
Sunny Goyal | 7ce471b | 2017-08-02 03:37:39 -0700 | [diff] [blame] | 718 | return mWorkspaceScreens.containsKey(EXTRA_EMPTY_SCREEN_ID) && getChildCount() > 1; |
Michael Jurka | 2f81731 | 2013-09-20 03:03:42 +0200 | [diff] [blame] | 719 | } |
| 720 | |
Sunny Goyal | efb7e84 | 2018-10-04 15:11:00 -0700 | [diff] [blame] | 721 | public int commitExtraEmptyScreen() { |
Adam Cohen | dcb173d | 2014-04-01 13:33:58 -0700 | [diff] [blame] | 722 | if (mLauncher.isWorkspaceLoading()) { |
| 723 | // Invalid and dangerous operation if workspace is loading |
Adam Cohen | dcb173d | 2014-04-01 13:33:58 -0700 | [diff] [blame] | 724 | return -1; |
| 725 | } |
Winson Chung | a90303b | 2013-11-15 13:05:06 -0800 | [diff] [blame] | 726 | |
Adam Cohen | dcd297f | 2013-06-18 13:13:40 -0700 | [diff] [blame] | 727 | CellLayout cl = mWorkspaceScreens.get(EXTRA_EMPTY_SCREEN_ID); |
Winson Chung | c916834 | 2013-06-26 14:54:55 -0700 | [diff] [blame] | 728 | mWorkspaceScreens.remove(EXTRA_EMPTY_SCREEN_ID); |
Sunny Goyal | efb7e84 | 2018-10-04 15:11:00 -0700 | [diff] [blame] | 729 | mScreenOrder.removeValue(EXTRA_EMPTY_SCREEN_ID); |
Adam Cohen | dcd297f | 2013-06-18 13:13:40 -0700 | [diff] [blame] | 730 | |
Sunny Goyal | efb7e84 | 2018-10-04 15:11:00 -0700 | [diff] [blame] | 731 | int newId = LauncherSettings.Settings.call(getContext().getContentResolver(), |
Sunny Goyal | d249748 | 2015-09-22 18:24:19 -0700 | [diff] [blame] | 732 | LauncherSettings.Settings.METHOD_NEW_SCREEN_ID) |
Sunny Goyal | efb7e84 | 2018-10-04 15:11:00 -0700 | [diff] [blame] | 733 | .getInt(LauncherSettings.Settings.EXTRA_VALUE); |
Adam Cohen | dcd297f | 2013-06-18 13:13:40 -0700 | [diff] [blame] | 734 | mWorkspaceScreens.put(newId, cl); |
| 735 | mScreenOrder.add(newId); |
| 736 | |
Adam Cohen | dcd297f | 2013-06-18 13:13:40 -0700 | [diff] [blame] | 737 | return newId; |
| 738 | } |
| 739 | |
Sunny Goyal | ef92b82 | 2018-11-21 14:12:00 -0800 | [diff] [blame] | 740 | @Override |
| 741 | public Hotseat getHotseat() { |
| 742 | return mLauncher.getHotseat(); |
| 743 | } |
| 744 | |
| 745 | @Override |
| 746 | public void onAddDropTarget(DropTarget target) { |
| 747 | mDragController.addDropTarget(target); |
| 748 | } |
| 749 | |
| 750 | @Override |
Sunny Goyal | efb7e84 | 2018-10-04 15:11:00 -0700 | [diff] [blame] | 751 | public CellLayout getScreenWithId(int screenId) { |
Sunny Goyal | 9eba1fd | 2015-10-16 08:58:57 -0700 | [diff] [blame] | 752 | return mWorkspaceScreens.get(screenId); |
Adam Cohen | dcd297f | 2013-06-18 13:13:40 -0700 | [diff] [blame] | 753 | } |
| 754 | |
Sunny Goyal | efb7e84 | 2018-10-04 15:11:00 -0700 | [diff] [blame] | 755 | public int getIdForScreen(CellLayout layout) { |
Sunny Goyal | e2df062 | 2015-04-24 11:27:00 -0700 | [diff] [blame] | 756 | int index = mWorkspaceScreens.indexOfValue(layout); |
| 757 | if (index != -1) { |
| 758 | return mWorkspaceScreens.keyAt(index); |
Adam Cohen | dcd297f | 2013-06-18 13:13:40 -0700 | [diff] [blame] | 759 | } |
| 760 | return -1; |
| 761 | } |
| 762 | |
Sunny Goyal | efb7e84 | 2018-10-04 15:11:00 -0700 | [diff] [blame] | 763 | public int getPageIndexForScreenId(int screenId) { |
Adam Cohen | dcd297f | 2013-06-18 13:13:40 -0700 | [diff] [blame] | 764 | return indexOfChild(mWorkspaceScreens.get(screenId)); |
| 765 | } |
| 766 | |
Sunny Goyal | efb7e84 | 2018-10-04 15:11:00 -0700 | [diff] [blame] | 767 | public int getScreenIdForPageIndex(int index) { |
Winson Chung | 5f8afe6 | 2013-08-12 16:19:28 -0700 | [diff] [blame] | 768 | if (0 <= index && index < mScreenOrder.size()) { |
| 769 | return mScreenOrder.get(index); |
| 770 | } |
| 771 | return -1; |
Adam Cohen | dcd297f | 2013-06-18 13:13:40 -0700 | [diff] [blame] | 772 | } |
| 773 | |
Sunny Goyal | efb7e84 | 2018-10-04 15:11:00 -0700 | [diff] [blame] | 774 | public IntArray getScreenOrder() { |
Winson Chung | c916834 | 2013-06-26 14:54:55 -0700 | [diff] [blame] | 775 | return mScreenOrder; |
| 776 | } |
| 777 | |
Adam Cohen | dcd297f | 2013-06-18 13:13:40 -0700 | [diff] [blame] | 778 | public void stripEmptyScreens() { |
Adam Cohen | 65e4303 | 2014-03-03 11:37:21 -0800 | [diff] [blame] | 779 | if (mLauncher.isWorkspaceLoading()) { |
Adam Cohen | dcb173d | 2014-04-01 13:33:58 -0700 | [diff] [blame] | 780 | // Don't strip empty screens if the workspace is still loading. |
| 781 | // This is dangerous and can result in data loss. |
Adam Cohen | 517a7f5 | 2014-03-01 12:12:59 -0800 | [diff] [blame] | 782 | return; |
| 783 | } |
| 784 | |
Sunny Goyal | 9ccafbf | 2016-10-26 13:06:08 -0700 | [diff] [blame] | 785 | if (isPageInTransition()) { |
Adam Cohen | accfd56 | 2013-07-12 14:40:40 -0700 | [diff] [blame] | 786 | mStripScreensOnPageStopMoving = true; |
| 787 | return; |
| 788 | } |
| 789 | |
| 790 | int currentPage = getNextPage(); |
Sunny Goyal | efb7e84 | 2018-10-04 15:11:00 -0700 | [diff] [blame] | 791 | IntArray removeScreens = new IntArray(); |
Sunny Goyal | e2df062 | 2015-04-24 11:27:00 -0700 | [diff] [blame] | 792 | int total = mWorkspaceScreens.size(); |
| 793 | for (int i = 0; i < total; i++) { |
Sunny Goyal | efb7e84 | 2018-10-04 15:11:00 -0700 | [diff] [blame] | 794 | int id = mWorkspaceScreens.keyAt(i); |
Sunny Goyal | e2df062 | 2015-04-24 11:27:00 -0700 | [diff] [blame] | 795 | CellLayout cl = mWorkspaceScreens.valueAt(i); |
Sunny Goyal | da4fe1a | 2016-05-26 16:05:17 -0700 | [diff] [blame] | 796 | // FIRST_SCREEN_ID can never be removed. |
Jon Miranda | 7143ba6 | 2019-03-15 09:00:05 -0700 | [diff] [blame^] | 797 | if ((!FeatureFlags.QSB_ON_FIRST_SCREEN || id > FIRST_SCREEN_ID) |
Sunny Goyal | a9e2f5a | 2016-06-10 12:22:04 -0700 | [diff] [blame] | 798 | && cl.getShortcutsAndWidgets().getChildCount() == 0) { |
Adam Cohen | dcd297f | 2013-06-18 13:13:40 -0700 | [diff] [blame] | 799 | removeScreens.add(id); |
| 800 | } |
| 801 | } |
| 802 | |
Sunny Goyal | ae50284 | 2016-06-17 08:43:56 -0700 | [diff] [blame] | 803 | boolean isInAccessibleDrag = mLauncher.getAccessibilityDelegate().isInAccessibleDrag(); |
Vadim Tryshev | de1e67c | 2015-08-11 16:21:04 -0700 | [diff] [blame] | 804 | |
Adam Cohen | 5084cba | 2013-09-03 12:01:16 -0700 | [diff] [blame] | 805 | // We enforce at least one page to add new items to. In the case that we remove the last |
| 806 | // such screen, we convert the last screen to the empty screen |
Sunny Goyal | 7ce471b | 2017-08-02 03:37:39 -0700 | [diff] [blame] | 807 | int minScreens = 1; |
Adam Cohen | 5084cba | 2013-09-03 12:01:16 -0700 | [diff] [blame] | 808 | |
Adam Cohen | dcd297f | 2013-06-18 13:13:40 -0700 | [diff] [blame] | 809 | int pageShift = 0; |
Sunny Goyal | efb7e84 | 2018-10-04 15:11:00 -0700 | [diff] [blame] | 810 | for (int i = 0; i < removeScreens.size(); i++) { |
| 811 | int id = removeScreens.get(i); |
Adam Cohen | dcd297f | 2013-06-18 13:13:40 -0700 | [diff] [blame] | 812 | CellLayout cl = mWorkspaceScreens.get(id); |
| 813 | mWorkspaceScreens.remove(id); |
Sunny Goyal | efb7e84 | 2018-10-04 15:11:00 -0700 | [diff] [blame] | 814 | mScreenOrder.removeValue(id); |
Adam Cohen | 5084cba | 2013-09-03 12:01:16 -0700 | [diff] [blame] | 815 | |
| 816 | if (getChildCount() > minScreens) { |
| 817 | if (indexOfChild(cl) < currentPage) { |
| 818 | pageShift++; |
| 819 | } |
Vadim Tryshev | de1e67c | 2015-08-11 16:21:04 -0700 | [diff] [blame] | 820 | |
Sunny Goyal | ae50284 | 2016-06-17 08:43:56 -0700 | [diff] [blame] | 821 | if (isInAccessibleDrag) { |
Vadim Tryshev | de1e67c | 2015-08-11 16:21:04 -0700 | [diff] [blame] | 822 | cl.enableAccessibleDrag(false, CellLayout.WORKSPACE_ACCESSIBILITY_DRAG); |
| 823 | } |
| 824 | |
Adam Cohen | 5084cba | 2013-09-03 12:01:16 -0700 | [diff] [blame] | 825 | removeView(cl); |
| 826 | } else { |
Sunny Goyal | 7ce471b | 2017-08-02 03:37:39 -0700 | [diff] [blame] | 827 | // if this is the last screen, convert it to the empty screen |
Adam Cohen | ad4e15c | 2013-10-17 16:21:35 -0700 | [diff] [blame] | 828 | mRemoveEmptyScreenRunnable = null; |
Adam Cohen | 5084cba | 2013-09-03 12:01:16 -0700 | [diff] [blame] | 829 | mWorkspaceScreens.put(EXTRA_EMPTY_SCREEN_ID, cl); |
| 830 | mScreenOrder.add(EXTRA_EMPTY_SCREEN_ID); |
Adam Cohen | dcd297f | 2013-06-18 13:13:40 -0700 | [diff] [blame] | 831 | } |
Adam Cohen | dcd297f | 2013-06-18 13:13:40 -0700 | [diff] [blame] | 832 | } |
Winson Chung | 64359a5 | 2013-07-08 17:17:08 -0700 | [diff] [blame] | 833 | |
Adam Cohen | accfd56 | 2013-07-12 14:40:40 -0700 | [diff] [blame] | 834 | if (pageShift >= 0) { |
| 835 | setCurrentPage(currentPage - pageShift); |
| 836 | } |
Adam Cohen | dcd297f | 2013-06-18 13:13:40 -0700 | [diff] [blame] | 837 | } |
| 838 | |
Sunny Goyal | d5462aa | 2016-11-22 16:52:39 +0530 | [diff] [blame] | 839 | /** |
Patrick Dubroy | e708c52 | 2011-03-01 16:03:43 -0800 | [diff] [blame] | 840 | * Called directly from a CellLayout (not by the framework), after we've been added as a |
| 841 | * listener via setOnInterceptTouchEventListener(). This allows us to tell the CellLayout |
| 842 | * that it should intercept touch events, which is not something that is normally supported. |
| 843 | */ |
Sunny Goyal | 7066003 | 2015-05-14 00:07:08 -0700 | [diff] [blame] | 844 | @SuppressLint("ClickableViewAccessibility") |
Patrick Dubroy | e708c52 | 2011-03-01 16:03:43 -0800 | [diff] [blame] | 845 | @Override |
Michael Jurka | dee0589 | 2010-07-27 10:01:56 -0700 | [diff] [blame] | 846 | public boolean onTouch(View v, MotionEvent event) { |
Hyunyoung Song | 4613361 | 2016-09-15 10:48:25 -0700 | [diff] [blame] | 847 | return shouldConsumeTouch(v); |
| 848 | } |
| 849 | |
| 850 | private boolean shouldConsumeTouch(View v) { |
Tony Wickham | 0c7852f | 2016-12-02 14:47:04 -0800 | [diff] [blame] | 851 | return !workspaceIconsCanBeDragged() |
Adam Cohen | 6c5891a | 2014-07-09 23:53:15 -0700 | [diff] [blame] | 852 | || (!workspaceInModalState() && indexOfChild(v) != mCurrentPage); |
Patrick Dubroy | e708c52 | 2011-03-01 16:03:43 -0800 | [diff] [blame] | 853 | } |
| 854 | |
Adam Cohen | fc53cd2 | 2011-07-20 15:45:11 -0700 | [diff] [blame] | 855 | public boolean isSwitchingState() { |
| 856 | return mIsSwitchingState; |
| 857 | } |
| 858 | |
Winson Chung | 7044272 | 2012-02-10 15:43:22 -0800 | [diff] [blame] | 859 | /** This differs from isSwitchingState in that we take into account how far the transition |
| 860 | * has completed. */ |
Winson Chung | 9b0b2fe | 2012-02-24 13:03:34 -0800 | [diff] [blame] | 861 | public boolean isFinishedSwitchingState() { |
Tony Wickham | 251ab6e | 2016-12-02 18:54:32 -0800 | [diff] [blame] | 862 | return !mIsSwitchingState |
| 863 | || (mTransitionProgress > FINISHED_SWITCHING_STATE_TRANSITION_PROGRESS); |
Winson Chung | 7044272 | 2012-02-10 15:43:22 -0800 | [diff] [blame] | 864 | } |
| 865 | |
Michael Jurka | 5f1c509 | 2010-09-03 14:15:02 -0700 | [diff] [blame] | 866 | @Override |
| 867 | public boolean dispatchUnhandledMove(View focused, int direction) { |
Adam Cohen | 6c5891a | 2014-07-09 23:53:15 -0700 | [diff] [blame] | 868 | if (workspaceInModalState() || !isFinishedSwitchingState()) { |
Michael Jurka | 5f1c509 | 2010-09-03 14:15:02 -0700 | [diff] [blame] | 869 | // when the home screens are shrunken, shouldn't allow side-scrolling |
| 870 | return false; |
| 871 | } |
| 872 | return super.dispatchUnhandledMove(focused, direction); |
| 873 | } |
| 874 | |
| 875 | @Override |
| 876 | public boolean onInterceptTouchEvent(MotionEvent ev) { |
Jon Miranda | d2d39cb | 2018-04-24 15:24:21 -0700 | [diff] [blame] | 877 | if (ev.getActionMasked() == MotionEvent.ACTION_DOWN) { |
Adam Cohen | f8d2823 | 2011-02-01 21:47:00 -0800 | [diff] [blame] | 878 | mXDown = ev.getX(); |
| 879 | mYDown = ev.getY(); |
| 880 | } |
Michael Jurka | 5f1c509 | 2010-09-03 14:15:02 -0700 | [diff] [blame] | 881 | return super.onInterceptTouchEvent(ev); |
| 882 | } |
| 883 | |
Michael Jurka | 1adf539 | 2010-10-18 18:10:22 -0700 | [diff] [blame] | 884 | @Override |
| 885 | protected void determineScrollingStart(MotionEvent ev) { |
Winson Chung | 7044272 | 2012-02-10 15:43:22 -0800 | [diff] [blame] | 886 | if (!isFinishedSwitchingState()) return; |
Adam Cohen | f8d2823 | 2011-02-01 21:47:00 -0800 | [diff] [blame] | 887 | |
Adam Cohen | 3d1b2b4 | 2013-08-14 15:57:58 -0700 | [diff] [blame] | 888 | float deltaX = ev.getX() - mXDown; |
| 889 | float absDeltaX = Math.abs(deltaX); |
| 890 | float absDeltaY = Math.abs(ev.getY() - mYDown); |
Adam Cohen | f8d2823 | 2011-02-01 21:47:00 -0800 | [diff] [blame] | 891 | |
Adam Cohen | 3d1b2b4 | 2013-08-14 15:57:58 -0700 | [diff] [blame] | 892 | if (Float.compare(absDeltaX, 0f) == 0) return; |
Adam Cohen | f8d2823 | 2011-02-01 21:47:00 -0800 | [diff] [blame] | 893 | |
Adam Cohen | 3d1b2b4 | 2013-08-14 15:57:58 -0700 | [diff] [blame] | 894 | float slope = absDeltaY / absDeltaX; |
Winson Chung | 7044272 | 2012-02-10 15:43:22 -0800 | [diff] [blame] | 895 | float theta = (float) Math.atan(slope); |
Adam Cohen | f8d2823 | 2011-02-01 21:47:00 -0800 | [diff] [blame] | 896 | |
Adam Cohen | 3d1b2b4 | 2013-08-14 15:57:58 -0700 | [diff] [blame] | 897 | if (absDeltaX > mTouchSlop || absDeltaY > mTouchSlop) { |
Winson Chung | 7044272 | 2012-02-10 15:43:22 -0800 | [diff] [blame] | 898 | cancelCurrentPageLongPress(); |
| 899 | } |
| 900 | |
| 901 | if (theta > MAX_SWIPE_ANGLE) { |
| 902 | // Above MAX_SWIPE_ANGLE, we don't want to ever start scrolling the workspace |
| 903 | return; |
| 904 | } else if (theta > START_DAMPING_TOUCH_SLOP_ANGLE) { |
| 905 | // Above START_DAMPING_TOUCH_SLOP_ANGLE and below MAX_SWIPE_ANGLE, we want to |
| 906 | // increase the touch slop to make it harder to begin scrolling the workspace. This |
| 907 | // results in vertically scrolling widgets to more easily. The higher the angle, the |
| 908 | // more we increase touch slop. |
| 909 | theta -= START_DAMPING_TOUCH_SLOP_ANGLE; |
| 910 | float extraRatio = (float) |
| 911 | Math.sqrt((theta / (MAX_SWIPE_ANGLE - START_DAMPING_TOUCH_SLOP_ANGLE))); |
| 912 | super.determineScrollingStart(ev, 1 + TOUCH_SLOP_DAMPING_FACTOR * extraRatio); |
| 913 | } else { |
| 914 | // Below START_DAMPING_TOUCH_SLOP_ANGLE, we don't do anything special |
| 915 | super.determineScrollingStart(ev); |
Adam Cohen | f8d2823 | 2011-02-01 21:47:00 -0800 | [diff] [blame] | 916 | } |
Michael Jurka | 1adf539 | 2010-10-18 18:10:22 -0700 | [diff] [blame] | 917 | } |
| 918 | |
Sunny Goyal | 9ccafbf | 2016-10-26 13:06:08 -0700 | [diff] [blame] | 919 | protected void onPageBeginTransition() { |
| 920 | super.onPageBeginTransition(); |
Sunny Goyal | a502aa3 | 2017-10-02 16:04:06 -0700 | [diff] [blame] | 921 | updateChildrenLayersEnabled(); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 922 | } |
| 923 | |
Sunny Goyal | 9ccafbf | 2016-10-26 13:06:08 -0700 | [diff] [blame] | 924 | protected void onPageEndTransition() { |
| 925 | super.onPageEndTransition(); |
Sunny Goyal | a502aa3 | 2017-10-02 16:04:06 -0700 | [diff] [blame] | 926 | updateChildrenLayersEnabled(); |
Michael Jurka | d74c984 | 2011-07-10 12:44:21 -0700 | [diff] [blame] | 927 | |
Winson Chung | 3bc21c3 | 2012-01-20 13:59:18 -0800 | [diff] [blame] | 928 | if (mDragController.isDragging()) { |
Adam Cohen | 6c5891a | 2014-07-09 23:53:15 -0700 | [diff] [blame] | 929 | if (workspaceInModalState()) { |
Winson Chung | 3bc21c3 | 2012-01-20 13:59:18 -0800 | [diff] [blame] | 930 | // If we are in springloaded mode, then force an event to check if the current touch |
| 931 | // is under a new page (to scroll to) |
Winson Chung | 25460a1 | 2013-04-01 18:21:28 -0700 | [diff] [blame] | 932 | mDragController.forceTouchMove(); |
Winson Chung | 3bc21c3 | 2012-01-20 13:59:18 -0800 | [diff] [blame] | 933 | } |
Patrick Dubroy | 1262e36 | 2010-10-06 15:49:50 -0700 | [diff] [blame] | 934 | } |
Adam Cohen | 26976d9 | 2011-03-22 15:33:33 -0700 | [diff] [blame] | 935 | |
Adam Cohen | accfd56 | 2013-07-12 14:40:40 -0700 | [diff] [blame] | 936 | if (mStripScreensOnPageStopMoving) { |
| 937 | stripEmptyScreens(); |
| 938 | mStripScreensOnPageStopMoving = false; |
| 939 | } |
Adam Cohen | c2d6e89 | 2014-10-16 09:49:24 -0700 | [diff] [blame] | 940 | } |
| 941 | |
| 942 | protected void onScrollInteractionBegin() { |
Vadim Tryshev | 2ce6a13 | 2018-06-18 19:14:44 -0700 | [diff] [blame] | 943 | super.onScrollInteractionBegin(); |
Adam Cohen | c2d6e89 | 2014-10-16 09:49:24 -0700 | [diff] [blame] | 944 | mScrollInteractionBegan = true; |
| 945 | } |
| 946 | |
| 947 | protected void onScrollInteractionEnd() { |
| 948 | super.onScrollInteractionEnd(); |
| 949 | mScrollInteractionBegan = false; |
| 950 | if (mStartedSendingScrollEvents) { |
| 951 | mStartedSendingScrollEvents = false; |
| 952 | mLauncherOverlay.onScrollInteractionEnd(); |
| 953 | } |
| 954 | } |
| 955 | |
| 956 | public void setLauncherOverlay(LauncherOverlay overlay) { |
| 957 | mLauncherOverlay = overlay; |
Sunny Goyal | c86df47 | 2016-02-25 09:19:38 -0800 | [diff] [blame] | 958 | // A new overlay has been set. Reset event tracking |
| 959 | mStartedSendingScrollEvents = false; |
| 960 | onOverlayScrollChanged(0); |
| 961 | } |
| 962 | |
Sunny Goyal | c86df47 | 2016-02-25 09:19:38 -0800 | [diff] [blame] | 963 | |
Sunny Goyal | 061380a | 2016-02-29 15:15:03 -0800 | [diff] [blame] | 964 | private boolean isScrollingOverlay() { |
| 965 | return mLauncherOverlay != null && |
Adam Cohen | 8d769d6 | 2017-06-23 17:27:38 -0700 | [diff] [blame] | 966 | ((mIsRtl && getUnboundedScrollX() > mMaxScrollX) || (!mIsRtl && getUnboundedScrollX() < 0)); |
Sunny Goyal | 061380a | 2016-02-29 15:15:03 -0800 | [diff] [blame] | 967 | } |
| 968 | |
| 969 | @Override |
| 970 | protected void snapToDestination() { |
| 971 | // If we're overscrolling the overlay, we make sure to immediately reset the PagedView |
| 972 | // to it's baseline position instead of letting the overscroll settle. The overlay handles |
| 973 | // it's own settling, and every gesture to the overlay should be self-contained and start |
| 974 | // from 0, so we zero it out here. |
| 975 | if (isScrollingOverlay()) { |
Sunny Goyal | 061380a | 2016-02-29 15:15:03 -0800 | [diff] [blame] | 976 | // We reset mWasInOverscroll so that PagedView doesn't zero out the overscroll |
Sunny Goyal | 76dbf6f | 2017-01-03 14:55:47 -0800 | [diff] [blame] | 977 | // interaction when we call snapToPageImmediately. |
Sunny Goyal | 061380a | 2016-02-29 15:15:03 -0800 | [diff] [blame] | 978 | mWasInOverscroll = false; |
Sunny Goyal | 76dbf6f | 2017-01-03 14:55:47 -0800 | [diff] [blame] | 979 | snapToPageImmediately(0); |
Sunny Goyal | 061380a | 2016-02-29 15:15:03 -0800 | [diff] [blame] | 980 | } else { |
| 981 | super.snapToDestination(); |
| 982 | } |
| 983 | } |
| 984 | |
Sunny Goyal | c86df47 | 2016-02-25 09:19:38 -0800 | [diff] [blame] | 985 | @Override |
Tony Wickham | f549dab | 2016-05-16 09:54:06 -0700 | [diff] [blame] | 986 | protected void onScrollChanged(int l, int t, int oldl, int oldt) { |
| 987 | super.onScrollChanged(l, t, oldl, oldt); |
| 988 | |
| 989 | // Update the page indicator progress. |
| 990 | boolean isTransitioning = mIsSwitchingState |
| 991 | || (getLayoutTransition() != null && getLayoutTransition().isRunning()); |
Tony Wickham | ddb7700 | 2016-05-19 12:45:40 -0700 | [diff] [blame] | 992 | if (!isTransitioning) { |
Tony Wickham | f549dab | 2016-05-16 09:54:06 -0700 | [diff] [blame] | 993 | showPageIndicatorAtCurrentScroll(); |
| 994 | } |
Sunny Goyal | 9ccafbf | 2016-10-26 13:06:08 -0700 | [diff] [blame] | 995 | |
| 996 | updatePageAlphaValues(); |
Sunny Goyal | 9ccafbf | 2016-10-26 13:06:08 -0700 | [diff] [blame] | 997 | enableHwLayersOnVisiblePages(); |
Tony Wickham | f549dab | 2016-05-16 09:54:06 -0700 | [diff] [blame] | 998 | } |
| 999 | |
Sunny Goyal | c99cb17 | 2017-10-19 16:15:09 -0700 | [diff] [blame] | 1000 | public void showPageIndicatorAtCurrentScroll() { |
Tony Wickham | ddb7700 | 2016-05-19 12:45:40 -0700 | [diff] [blame] | 1001 | if (mPageIndicator != null) { |
| 1002 | mPageIndicator.setScroll(getScrollX(), computeMaxScrollX()); |
| 1003 | } |
Tony Wickham | f549dab | 2016-05-16 09:54:06 -0700 | [diff] [blame] | 1004 | } |
| 1005 | |
| 1006 | @Override |
Sunny Goyal | b7b0135 | 2018-08-09 12:31:20 -0700 | [diff] [blame] | 1007 | protected void overScroll(int amount) { |
Jon Miranda | 71cb80c | 2019-01-24 21:25:13 -0800 | [diff] [blame] | 1008 | boolean shouldScrollOverlay = mLauncherOverlay != null && !mScroller.isSpringing() && |
Sunny Goyal | 7066003 | 2015-05-14 00:07:08 -0700 | [diff] [blame] | 1009 | ((amount <= 0 && !mIsRtl) || (amount >= 0 && mIsRtl)); |
Adam Cohen | 8c4ca92 | 2014-10-24 17:40:34 -0700 | [diff] [blame] | 1010 | |
Hyunyoung Song | 0de0117 | 2016-10-05 16:27:48 -0700 | [diff] [blame] | 1011 | boolean shouldZeroOverlay = mLauncherOverlay != null && mLastOverlayScroll != 0 && |
Sunny Goyal | 7066003 | 2015-05-14 00:07:08 -0700 | [diff] [blame] | 1012 | ((amount >= 0 && !mIsRtl) || (amount <= 0 && mIsRtl)); |
Adam Cohen | c2d6e89 | 2014-10-16 09:49:24 -0700 | [diff] [blame] | 1013 | |
| 1014 | if (shouldScrollOverlay) { |
| 1015 | if (!mStartedSendingScrollEvents && mScrollInteractionBegan) { |
| 1016 | mStartedSendingScrollEvents = true; |
| 1017 | mLauncherOverlay.onScrollInteractionBegin(); |
Adam Cohen | c2d6e89 | 2014-10-16 09:49:24 -0700 | [diff] [blame] | 1018 | } |
Adam Cohen | c2d6e89 | 2014-10-16 09:49:24 -0700 | [diff] [blame] | 1019 | |
Sunny Goyal | b7b0135 | 2018-08-09 12:31:20 -0700 | [diff] [blame] | 1020 | mLastOverlayScroll = Math.abs(((float) amount) / getMeasuredWidth()); |
Hyunyoung Song | 0de0117 | 2016-10-05 16:27:48 -0700 | [diff] [blame] | 1021 | mLauncherOverlay.onScrollChange(mLastOverlayScroll, mIsRtl); |
Sunny Goyal | 7ce471b | 2017-08-02 03:37:39 -0700 | [diff] [blame] | 1022 | } else { |
Adam Cohen | c2d6e89 | 2014-10-16 09:49:24 -0700 | [diff] [blame] | 1023 | dampedOverScroll(amount); |
Adam Cohen | c2d6e89 | 2014-10-16 09:49:24 -0700 | [diff] [blame] | 1024 | } |
Adam Cohen | 8c4ca92 | 2014-10-24 17:40:34 -0700 | [diff] [blame] | 1025 | |
| 1026 | if (shouldZeroOverlay) { |
Sunny Goyal | 7066003 | 2015-05-14 00:07:08 -0700 | [diff] [blame] | 1027 | mLauncherOverlay.onScrollChange(0, mIsRtl); |
Adam Cohen | 8c4ca92 | 2014-10-24 17:40:34 -0700 | [diff] [blame] | 1028 | } |
Adam Cohen | 89cbbb9 | 2013-09-25 16:52:48 -0700 | [diff] [blame] | 1029 | } |
| 1030 | |
Sunny Goyal | 65d9ceb | 2017-05-08 09:17:42 -0700 | [diff] [blame] | 1031 | @Override |
| 1032 | protected boolean shouldFlingForVelocity(int velocityX) { |
| 1033 | // When the overlay is moving, the fling or settle transition is controlled by the overlay. |
Adam Cohen | 5360b6e | 2017-07-18 16:14:12 -0700 | [diff] [blame] | 1034 | return Float.compare(Math.abs(mOverlayTranslation), 0) == 0 && |
Sunny Goyal | 65d9ceb | 2017-05-08 09:17:42 -0700 | [diff] [blame] | 1035 | super.shouldFlingForVelocity(velocityX); |
| 1036 | } |
| 1037 | |
Sunny Goyal | c86df47 | 2016-02-25 09:19:38 -0800 | [diff] [blame] | 1038 | /** |
| 1039 | * The overlay scroll is being controlled locally, just update our overlay effect |
| 1040 | */ |
| 1041 | public void onOverlayScrollChanged(float scroll) { |
Hyunyoung Song | 3f5a11f | 2017-06-29 16:42:04 -0700 | [diff] [blame] | 1042 | if (Float.compare(scroll, 1f) == 0) { |
| 1043 | if (!mOverlayShown) { |
| 1044 | mLauncher.getUserEventDispatcher().logActionOnContainer(Action.Touch.SWIPE, |
| 1045 | Action.Direction.LEFT, ContainerType.WORKSPACE, 0); |
| 1046 | } |
| 1047 | mOverlayShown = true; |
Vadim Tryshev | 2c430b3 | 2018-04-04 14:45:21 -0700 | [diff] [blame] | 1048 | // Not announcing the overlay page for accessibility since it announces itself. |
Hyunyoung Song | 3f5a11f | 2017-06-29 16:42:04 -0700 | [diff] [blame] | 1049 | } else if (Float.compare(scroll, 0f) == 0) { |
| 1050 | if (mOverlayShown) { |
| 1051 | mLauncher.getUserEventDispatcher().logActionOnContainer(Action.Touch.SWIPE, |
| 1052 | Action.Direction.RIGHT, ContainerType.WORKSPACE, -1); |
Vadim Tryshev | 2c430b3 | 2018-04-04 14:45:21 -0700 | [diff] [blame] | 1053 | } else if (Float.compare(mOverlayTranslation, 0f) != 0) { |
| 1054 | // When arriving to 0 overscroll from non-zero overscroll, announce page for |
| 1055 | // accessibility since default announcements were disabled while in overscroll |
| 1056 | // state. |
| 1057 | // Not doing this if mOverlayShown because in that case the accessibility service |
| 1058 | // will announce the launcher window description upon regaining focus after |
| 1059 | // switching from the overlay screen. |
| 1060 | announcePageForAccessibility(); |
Hyunyoung Song | 3f5a11f | 2017-06-29 16:42:04 -0700 | [diff] [blame] | 1061 | } |
| 1062 | mOverlayShown = false; |
Sunny Goyal | bf6dfc5 | 2018-02-13 17:04:55 -0800 | [diff] [blame] | 1063 | tryRunOverlayCallback(); |
Hyunyoung Song | 3f5a11f | 2017-06-29 16:42:04 -0700 | [diff] [blame] | 1064 | } |
Vadim Tryshev | 2c430b3 | 2018-04-04 14:45:21 -0700 | [diff] [blame] | 1065 | |
Sunny Goyal | c86df47 | 2016-02-25 09:19:38 -0800 | [diff] [blame] | 1066 | float offset = 0f; |
Sunny Goyal | c86df47 | 2016-02-25 09:19:38 -0800 | [diff] [blame] | 1067 | |
| 1068 | scroll = Math.max(scroll - offset, 0); |
| 1069 | scroll = Math.min(1, scroll / (1 - offset)); |
| 1070 | |
Sunny Goyal | 5bc6b6f | 2017-10-26 15:36:10 -0700 | [diff] [blame] | 1071 | float alpha = 1 - Interpolators.DEACCEL_3.getInterpolation(scroll); |
Sunny Goyal | c86df47 | 2016-02-25 09:19:38 -0800 | [diff] [blame] | 1072 | float transX = mLauncher.getDragLayer().getMeasuredWidth() * scroll; |
Sunny Goyal | c86df47 | 2016-02-25 09:19:38 -0800 | [diff] [blame] | 1073 | |
| 1074 | if (mIsRtl) { |
| 1075 | transX = -transX; |
| 1076 | } |
Sunny Goyal | ee68816 | 2016-02-12 14:24:21 -0800 | [diff] [blame] | 1077 | mOverlayTranslation = transX; |
Sunny Goyal | c86df47 | 2016-02-25 09:19:38 -0800 | [diff] [blame] | 1078 | |
| 1079 | // TODO(adamcohen): figure out a final effect here. We may need to recommend |
| 1080 | // different effects based on device performance. On at least one relatively high-end |
| 1081 | // device I've tried, translating the launcher causes things to get quite laggy. |
Sunny Goyal | 19d32b7 | 2018-01-19 14:26:18 -0800 | [diff] [blame] | 1082 | mLauncher.getDragLayer().setTranslationX(transX); |
Sunny Goyal | 6001ea2 | 2018-05-10 16:31:00 -0700 | [diff] [blame] | 1083 | mLauncher.getDragLayer().getAlphaProperty(ALPHA_INDEX_OVERLAY).setValue(alpha); |
Sunny Goyal | a92e0df | 2016-06-09 12:08:22 -0700 | [diff] [blame] | 1084 | } |
| 1085 | |
Tony Wickham | 618a504 | 2018-02-12 15:59:15 -0800 | [diff] [blame] | 1086 | /** |
Sunny Goyal | bf6dfc5 | 2018-02-13 17:04:55 -0800 | [diff] [blame] | 1087 | * @return false if the callback is still pending |
| 1088 | */ |
| 1089 | private boolean tryRunOverlayCallback() { |
| 1090 | if (mOnOverlayHiddenCallback == null) { |
| 1091 | // Return true as no callback is pending. This is used by OnWindowFocusChangeListener |
| 1092 | // to remove itself if multiple focus handles were added. |
| 1093 | return true; |
| 1094 | } |
| 1095 | if (mOverlayShown || !hasWindowFocus()) { |
| 1096 | return false; |
| 1097 | } |
| 1098 | |
| 1099 | mOnOverlayHiddenCallback.run(); |
| 1100 | mOnOverlayHiddenCallback = null; |
| 1101 | return true; |
| 1102 | } |
| 1103 | |
| 1104 | /** |
Tony Wickham | 618a504 | 2018-02-12 15:59:15 -0800 | [diff] [blame] | 1105 | * Runs the given callback when the minus one overlay is hidden. Specifically, it is run |
| 1106 | * when launcher's window has focus and the overlay is no longer being shown. If a callback |
| 1107 | * is already present, the new callback will chain off it so both are run. |
| 1108 | * |
| 1109 | * @return Whether the callback was deferred. |
| 1110 | */ |
| 1111 | public boolean runOnOverlayHidden(Runnable callback) { |
Tony Wickham | 618a504 | 2018-02-12 15:59:15 -0800 | [diff] [blame] | 1112 | if (mOnOverlayHiddenCallback == null) { |
| 1113 | mOnOverlayHiddenCallback = callback; |
| 1114 | } else { |
| 1115 | // Chain the new callback onto the previous callback(s). |
| 1116 | Runnable oldCallback = mOnOverlayHiddenCallback; |
| 1117 | mOnOverlayHiddenCallback = () -> { |
| 1118 | oldCallback.run(); |
| 1119 | callback.run(); |
| 1120 | }; |
| 1121 | } |
Sunny Goyal | bf6dfc5 | 2018-02-13 17:04:55 -0800 | [diff] [blame] | 1122 | if (!tryRunOverlayCallback()) { |
| 1123 | ViewTreeObserver observer = getViewTreeObserver(); |
| 1124 | if (observer != null && observer.isAlive()) { |
| 1125 | observer.addOnWindowFocusChangeListener( |
| 1126 | new ViewTreeObserver.OnWindowFocusChangeListener() { |
| 1127 | @Override |
| 1128 | public void onWindowFocusChanged(boolean hasFocus) { |
| 1129 | if (tryRunOverlayCallback() && observer.isAlive()) { |
| 1130 | observer.removeOnWindowFocusChangeListener(this); |
| 1131 | } |
| 1132 | }}); |
| 1133 | } |
| 1134 | return true; |
| 1135 | } |
| 1136 | return false; |
Tony Wickham | 618a504 | 2018-02-12 15:59:15 -0800 | [diff] [blame] | 1137 | } |
| 1138 | |
Sunny Goyal | ee68816 | 2016-02-12 14:24:21 -0800 | [diff] [blame] | 1139 | @Override |
Hyunyoung Song | 3f5a11f | 2017-06-29 16:42:04 -0700 | [diff] [blame] | 1140 | protected void notifyPageSwitchListener(int prevPage) { |
| 1141 | super.notifyPageSwitchListener(prevPage); |
| 1142 | if (prevPage != mCurrentPage) { |
| 1143 | int swipeDirection = (prevPage < mCurrentPage) ? Action.Direction.RIGHT : Action.Direction.LEFT; |
| 1144 | mLauncher.getUserEventDispatcher().logActionOnContainer(Action.Touch.SWIPE, |
| 1145 | swipeDirection, ContainerType.WORKSPACE, prevPage); |
| 1146 | } |
Adam Cohen | 6fecd41 | 2013-10-02 17:41:50 -0700 | [diff] [blame] | 1147 | } |
| 1148 | |
Adam Cohen | 824fcb3 | 2014-05-21 23:01:25 +0000 | [diff] [blame] | 1149 | protected void setWallpaperDimension() { |
Sunny Goyal | 6f86609 | 2016-03-17 17:04:15 -0700 | [diff] [blame] | 1150 | Utilities.THREAD_POOL_EXECUTOR.execute(new Runnable() { |
| 1151 | @Override |
| 1152 | public void run() { |
Sunny Goyal | 87f784c | 2017-01-11 10:48:34 -0800 | [diff] [blame] | 1153 | final Point size = LauncherAppState.getIDP(getContext()).defaultWallpaperSize; |
Sunny Goyal | 6f86609 | 2016-03-17 17:04:15 -0700 | [diff] [blame] | 1154 | if (size.x != mWallpaperManager.getDesiredMinimumWidth() |
| 1155 | || size.y != mWallpaperManager.getDesiredMinimumHeight()) { |
| 1156 | mWallpaperManager.suggestDesiredDimensions(size.x, size.y); |
Sunny Goyal | 4b17147 | 2015-08-19 12:43:27 -0700 | [diff] [blame] | 1157 | } |
Michael Jurka | a6a0547 | 2013-11-13 17:59:46 +0100 | [diff] [blame] | 1158 | } |
Sunny Goyal | 6f86609 | 2016-03-17 17:04:15 -0700 | [diff] [blame] | 1159 | }); |
Michael Jurka | c5b262c | 2011-01-12 20:24:50 -0800 | [diff] [blame] | 1160 | } |
| 1161 | |
Winson | c7d2e83 | 2016-07-28 12:24:55 -0700 | [diff] [blame] | 1162 | public void lockWallpaperToDefaultPage() { |
| 1163 | mWallpaperOffset.setLockToDefaultPage(true); |
| 1164 | } |
| 1165 | |
| 1166 | public void unlockWallpaperFromDefaultPageOnNextLayout() { |
| 1167 | if (mWallpaperOffset.isLockedToDefaultPage()) { |
| 1168 | mUnlockWallpaperFromDefaultPageOnLayout = true; |
| 1169 | requestLayout(); |
| 1170 | } |
| 1171 | } |
| 1172 | |
Michael Jurka | 340c5f3 | 2010-10-21 16:49:19 -0700 | [diff] [blame] | 1173 | @Override |
| 1174 | public void computeScroll() { |
| 1175 | super.computeScroll(); |
Michael Jurka | fe09cb7 | 2013-08-27 15:48:58 +0200 | [diff] [blame] | 1176 | mWallpaperOffset.syncWithScroll(); |
Michael Jurka | cc07e7a | 2013-08-26 20:56:35 +0200 | [diff] [blame] | 1177 | } |
| 1178 | |
Tony Wickham | 95cdb3a | 2016-02-18 14:37:07 -0800 | [diff] [blame] | 1179 | public void computeScrollWithoutInvalidation() { |
| 1180 | computeScrollHelper(false); |
| 1181 | } |
| 1182 | |
Jason Monk | ed05f09 | 2014-04-24 10:13:05 -0400 | [diff] [blame] | 1183 | @Override |
Tony Wickham | a5209e3 | 2015-11-19 12:45:30 -0800 | [diff] [blame] | 1184 | protected void determineScrollingStart(MotionEvent ev, float touchSlopScale) { |
| 1185 | if (!isSwitchingState()) { |
| 1186 | super.determineScrollingStart(ev, touchSlopScale); |
| 1187 | } |
| 1188 | } |
| 1189 | |
| 1190 | @Override |
Jason Monk | ed05f09 | 2014-04-24 10:13:05 -0400 | [diff] [blame] | 1191 | public void announceForAccessibility(CharSequence text) { |
| 1192 | // Don't announce if apps is on top of us. |
Sunny Goyal | 3e3f44c | 2017-10-23 17:14:52 -0700 | [diff] [blame] | 1193 | if (!mLauncher.isInState(ALL_APPS)) { |
Jason Monk | ed05f09 | 2014-04-24 10:13:05 -0400 | [diff] [blame] | 1194 | super.announceForAccessibility(text); |
| 1195 | } |
| 1196 | } |
| 1197 | |
Sunny Goyal | 9ccafbf | 2016-10-26 13:06:08 -0700 | [diff] [blame] | 1198 | private void updatePageAlphaValues() { |
Jon Miranda | 8d994cd | 2018-05-16 11:31:22 -0700 | [diff] [blame] | 1199 | // We need to check the isDragging case because updatePageAlphaValues is called between |
| 1200 | // goToState(SPRING_LOADED) and onStartStateTransition. |
| 1201 | if (!workspaceInModalState() && !mIsSwitchingState && !mDragController.isDragging()) { |
Sunny Goyal | 228153d | 2018-01-04 15:35:22 -0800 | [diff] [blame] | 1202 | int screenCenter = getScrollX() + getMeasuredWidth() / 2; |
Sunny Goyal | 7ce471b | 2017-08-02 03:37:39 -0700 | [diff] [blame] | 1203 | for (int i = 0; i < getChildCount(); i++) { |
Michael Jurka | 869390b | 2012-05-06 15:55:19 -0700 | [diff] [blame] | 1204 | CellLayout child = (CellLayout) getChildAt(i); |
| 1205 | if (child != null) { |
| 1206 | float scrollProgress = getScrollProgress(screenCenter, child, i); |
Adam Cohen | 7389496 | 2011-10-31 13:17:17 -0700 | [diff] [blame] | 1207 | float alpha = 1 - Math.abs(scrollProgress); |
Tony | 6dea7cd | 2017-07-06 11:32:53 -0700 | [diff] [blame] | 1208 | if (mWorkspaceFadeInAdjacentScreens) { |
| 1209 | child.getShortcutsAndWidgets().setAlpha(alpha); |
| 1210 | } else { |
| 1211 | // Pages that are off-screen aren't important for accessibility. |
| 1212 | child.getShortcutsAndWidgets().setImportantForAccessibility( |
| 1213 | alpha > 0 ? IMPORTANT_FOR_ACCESSIBILITY_AUTO |
| 1214 | : IMPORTANT_FOR_ACCESSIBILITY_NO_HIDE_DESCENDANTS); |
| 1215 | } |
Adam Cohen | 7389496 | 2011-10-31 13:17:17 -0700 | [diff] [blame] | 1216 | } |
Adam Cohen | f34bab5 | 2010-09-30 14:11:56 -0700 | [diff] [blame] | 1217 | } |
| 1218 | } |
Michael Jurka | a3d30ad | 2012-05-08 13:43:43 -0700 | [diff] [blame] | 1219 | } |
| 1220 | |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 1221 | protected void onAttachedToWindow() { |
| 1222 | super.onAttachedToWindow(); |
Sunny Goyal | 9eba1fd | 2015-10-16 08:58:57 -0700 | [diff] [blame] | 1223 | IBinder windowToken = getWindowToken(); |
| 1224 | mWallpaperOffset.setWindowToken(windowToken); |
Joe Onorato | 956091b | 2010-02-19 12:47:40 -0800 | [diff] [blame] | 1225 | computeScroll(); |
Sunny Goyal | 9eba1fd | 2015-10-16 08:58:57 -0700 | [diff] [blame] | 1226 | mDragController.setWindowToken(windowToken); |
Michael Jurka | 9c6fbed | 2011-03-02 17:41:34 -0800 | [diff] [blame] | 1227 | } |
| 1228 | |
| 1229 | protected void onDetachedFromWindow() { |
Romain Guy | e666195 | 2013-08-08 19:13:22 -0700 | [diff] [blame] | 1230 | super.onDetachedFromWindow(); |
Sunny Goyal | 9eba1fd | 2015-10-16 08:58:57 -0700 | [diff] [blame] | 1231 | mWallpaperOffset.setWindowToken(null); |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 1232 | } |
| 1233 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1234 | @Override |
Michael Jurka | 28750fb | 2010-09-24 17:43:49 -0700 | [diff] [blame] | 1235 | protected void onLayout(boolean changed, int left, int top, int right, int bottom) { |
Winson | c7d2e83 | 2016-07-28 12:24:55 -0700 | [diff] [blame] | 1236 | if (mUnlockWallpaperFromDefaultPageOnLayout) { |
| 1237 | mWallpaperOffset.setLockToDefaultPage(false); |
| 1238 | mUnlockWallpaperFromDefaultPageOnLayout = false; |
| 1239 | } |
Michael Jurka | c5b262c | 2011-01-12 20:24:50 -0800 | [diff] [blame] | 1240 | if (mFirstLayout && mCurrentPage >= 0 && mCurrentPage < getChildCount()) { |
Michael Jurka | fe09cb7 | 2013-08-27 15:48:58 +0200 | [diff] [blame] | 1241 | mWallpaperOffset.syncWithScroll(); |
Michael Jurka | cc07e7a | 2013-08-26 20:56:35 +0200 | [diff] [blame] | 1242 | mWallpaperOffset.jumpToFinal(); |
Michael Jurka | c5b262c | 2011-01-12 20:24:50 -0800 | [diff] [blame] | 1243 | } |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 1244 | super.onLayout(changed, left, top, right, bottom); |
Sunny Goyal | cce0780 | 2016-11-01 14:00:41 -0700 | [diff] [blame] | 1245 | updatePageAlphaValues(); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1246 | } |
| 1247 | |
| 1248 | @Override |
Winson Chung | 97d85d2 | 2011-04-13 11:27:36 -0700 | [diff] [blame] | 1249 | public int getDescendantFocusability() { |
Adam Cohen | 6c5891a | 2014-07-09 23:53:15 -0700 | [diff] [blame] | 1250 | if (workspaceInModalState()) { |
Winson Chung | 97d85d2 | 2011-04-13 11:27:36 -0700 | [diff] [blame] | 1251 | return ViewGroup.FOCUS_BLOCK_DESCENDANTS; |
| 1252 | } |
| 1253 | return super.getDescendantFocusability(); |
| 1254 | } |
| 1255 | |
Sunny Goyal | ea60926 | 2017-10-25 15:47:38 -0700 | [diff] [blame] | 1256 | private boolean workspaceInModalState() { |
| 1257 | return !mLauncher.isInState(NORMAL); |
Michael Jurka | d74c984 | 2011-07-10 12:44:21 -0700 | [diff] [blame] | 1258 | } |
| 1259 | |
Tony Wickham | 0c7852f | 2016-12-02 14:47:04 -0800 | [diff] [blame] | 1260 | /** Returns whether a drag should be allowed to be started from the current workspace state. */ |
| 1261 | public boolean workspaceIconsCanBeDragged() { |
Tony Wickham | 5337067 | 2017-12-05 10:10:55 -0800 | [diff] [blame] | 1262 | return mLauncher.getStateManager().getState().workspaceIconsCanBeDragged; |
Tony Wickham | 0c7852f | 2016-12-02 14:47:04 -0800 | [diff] [blame] | 1263 | } |
| 1264 | |
Sunny Goyal | a502aa3 | 2017-10-02 16:04:06 -0700 | [diff] [blame] | 1265 | private void updateChildrenLayersEnabled() { |
Sunny Goyal | 7c7be8c | 2018-03-07 19:58:07 -0800 | [diff] [blame] | 1266 | boolean enableChildrenLayers = mIsSwitchingState || isPageInTransition(); |
Michael Jurka | d74c984 | 2011-07-10 12:44:21 -0700 | [diff] [blame] | 1267 | |
| 1268 | if (enableChildrenLayers != mChildrenLayersEnabled) { |
| 1269 | mChildrenLayersEnabled = enableChildrenLayers; |
Michael Jurka | d51f33a | 2012-06-28 15:35:26 -0700 | [diff] [blame] | 1270 | if (mChildrenLayersEnabled) { |
| 1271 | enableHwLayersOnVisiblePages(); |
| 1272 | } else { |
| 1273 | for (int i = 0; i < getPageCount(); i++) { |
| 1274 | final CellLayout cl = (CellLayout) getChildAt(i); |
Chris Craik | 01f2d7f | 2013-10-01 14:41:56 -0700 | [diff] [blame] | 1275 | cl.enableHardwareLayer(false); |
Michael Jurka | d51f33a | 2012-06-28 15:35:26 -0700 | [diff] [blame] | 1276 | } |
| 1277 | } |
| 1278 | } |
| 1279 | } |
| 1280 | |
| 1281 | private void enableHwLayersOnVisiblePages() { |
| 1282 | if (mChildrenLayersEnabled) { |
| 1283 | final int screenCount = getChildCount(); |
Sunny Goyal | 9ccafbf | 2016-10-26 13:06:08 -0700 | [diff] [blame] | 1284 | |
Vadim Tryshev | 528b9e0 | 2018-05-24 18:22:03 -0700 | [diff] [blame] | 1285 | final int[] visibleScreens = getVisibleChildrenRange(); |
| 1286 | int leftScreen = visibleScreens[0]; |
| 1287 | int rightScreen = visibleScreens[1]; |
Sunny Goyal | 9ccafbf | 2016-10-26 13:06:08 -0700 | [diff] [blame] | 1288 | if (mForceDrawAdjacentPages) { |
| 1289 | // In overview mode, make sure that the two side pages are visible. |
Sunny Goyal | 7ce471b | 2017-08-02 03:37:39 -0700 | [diff] [blame] | 1290 | leftScreen = Utilities.boundToRange(getCurrentPage() - 1, 0, rightScreen); |
Sunny Goyal | 9ccafbf | 2016-10-26 13:06:08 -0700 | [diff] [blame] | 1291 | rightScreen = Utilities.boundToRange(getCurrentPage() + 1, |
| 1292 | leftScreen, getPageCount() - 1); |
| 1293 | } |
| 1294 | |
Michael Jurka | d51f33a | 2012-06-28 15:35:26 -0700 | [diff] [blame] | 1295 | if (leftScreen == rightScreen) { |
| 1296 | // make sure we're caching at least two pages always |
| 1297 | if (rightScreen < screenCount - 1) { |
| 1298 | rightScreen++; |
| 1299 | } else if (leftScreen > 0) { |
| 1300 | leftScreen--; |
| 1301 | } |
| 1302 | } |
Chris Craik | 01f2d7f | 2013-10-01 14:41:56 -0700 | [diff] [blame] | 1303 | |
Sunny Goyal | 7ce471b | 2017-08-02 03:37:39 -0700 | [diff] [blame] | 1304 | for (int i = 0; i < screenCount; i++) { |
Michael Jurka | 47639b9 | 2013-01-14 12:42:27 +0100 | [diff] [blame] | 1305 | final CellLayout layout = (CellLayout) getPageAt(i); |
Sunny Goyal | 9ccafbf | 2016-10-26 13:06:08 -0700 | [diff] [blame] | 1306 | // enable layers between left and right screen inclusive. |
| 1307 | boolean enableLayer = leftScreen <= i && i <= rightScreen; |
Chris Craik | 01f2d7f | 2013-10-01 14:41:56 -0700 | [diff] [blame] | 1308 | layout.enableHardwareLayer(enableLayer); |
Michael Jurka | d74c984 | 2011-07-10 12:44:21 -0700 | [diff] [blame] | 1309 | } |
Michael Jurka | ce7e05f | 2011-02-01 22:02:35 -0800 | [diff] [blame] | 1310 | } |
Michael Jurka | ce7e05f | 2011-02-01 22:02:35 -0800 | [diff] [blame] | 1311 | } |
| 1312 | |
Jon Miranda | d2d39cb | 2018-04-24 15:24:21 -0700 | [diff] [blame] | 1313 | public void onWallpaperTap(MotionEvent ev) { |
Sunny Goyal | cf25b52 | 2015-07-09 00:01:18 -0700 | [diff] [blame] | 1314 | final int[] position = mTempXY; |
Jeff Brown | 1d0867c | 2010-12-02 18:27:39 -0800 | [diff] [blame] | 1315 | getLocationOnScreen(position); |
| 1316 | |
| 1317 | int pointerIndex = ev.getActionIndex(); |
| 1318 | position[0] += (int) ev.getX(pointerIndex); |
| 1319 | position[1] += (int) ev.getY(pointerIndex); |
| 1320 | |
| 1321 | mWallpaperManager.sendWallpaperCommand(getWindowToken(), |
| 1322 | ev.getAction() == MotionEvent.ACTION_UP |
| 1323 | ? WallpaperManager.COMMAND_TAP : WallpaperManager.COMMAND_SECONDARY_TAP, |
| 1324 | position[0], position[1], 0, null); |
| 1325 | } |
| 1326 | |
Sunny Goyal | 06e21a2 | 2016-08-11 16:02:02 -0700 | [diff] [blame] | 1327 | public void prepareDragWithProvider(DragPreviewProvider outlineProvider) { |
| 1328 | mOutlineProvider = outlineProvider; |
Michael Jurka | 3e7c763 | 2010-10-02 16:01:03 -0700 | [diff] [blame] | 1329 | } |
| 1330 | |
Sunny Goyal | ce5a7e5 | 2015-07-08 15:44:27 -0700 | [diff] [blame] | 1331 | public void snapToPageFromOverView(int whichPage) { |
Sunny Goyal | 5bc6b6f | 2017-10-26 15:36:10 -0700 | [diff] [blame] | 1332 | snapToPage(whichPage, OVERVIEW_TRANSITION_MS, Interpolators.ZOOM_IN); |
Sunny Goyal | ce5a7e5 | 2015-07-08 15:44:27 -0700 | [diff] [blame] | 1333 | } |
| 1334 | |
Sunny Goyal | c99cb17 | 2017-10-19 16:15:09 -0700 | [diff] [blame] | 1335 | private void onStartStateTransition(LauncherState state) { |
Sunny Goyal | c99cb17 | 2017-10-19 16:15:09 -0700 | [diff] [blame] | 1336 | mIsSwitchingState = true; |
| 1337 | mTransitionProgress = 0; |
| 1338 | |
| 1339 | updateChildrenLayersEnabled(); |
Adam Cohen | 410f3cd | 2013-09-22 12:09:32 -0700 | [diff] [blame] | 1340 | } |
| 1341 | |
Sunny Goyal | c99cb17 | 2017-10-19 16:15:09 -0700 | [diff] [blame] | 1342 | private void onEndStateTransition() { |
| 1343 | mIsSwitchingState = false; |
| 1344 | mForceDrawAdjacentPages = false; |
| 1345 | mTransitionProgress = 1; |
Sunny Goyal | 47328fd | 2016-05-25 18:56:41 -0700 | [diff] [blame] | 1346 | |
Sunny Goyal | c99cb17 | 2017-10-19 16:15:09 -0700 | [diff] [blame] | 1347 | updateChildrenLayersEnabled(); |
| 1348 | updateAccessibilityFlags(); |
Tony Wickham | 34d2c91 | 2015-09-11 09:27:58 -0700 | [diff] [blame] | 1349 | } |
| 1350 | |
Winson Chung | dc61c4d | 2015-04-20 18:26:57 -0700 | [diff] [blame] | 1351 | /** |
Sunny Goyal | 4c7f215 | 2017-10-17 17:17:16 -0700 | [diff] [blame] | 1352 | * Sets the current workspace {@link LauncherState} and updates the UI without any animations |
Winson Chung | dc61c4d | 2015-04-20 18:26:57 -0700 | [diff] [blame] | 1353 | */ |
Sunny Goyal | c4fa8c3 | 2017-11-07 12:23:58 -0800 | [diff] [blame] | 1354 | @Override |
Sunny Goyal | 4c7f215 | 2017-10-17 17:17:16 -0700 | [diff] [blame] | 1355 | public void setState(LauncherState toState) { |
Sunny Goyal | c99cb17 | 2017-10-19 16:15:09 -0700 | [diff] [blame] | 1356 | onStartStateTransition(toState); |
Sunny Goyal | ea60926 | 2017-10-25 15:47:38 -0700 | [diff] [blame] | 1357 | mStateTransitionAnimation.setState(toState); |
Sunny Goyal | aeb1643 | 2017-10-16 11:46:41 -0700 | [diff] [blame] | 1358 | onEndStateTransition(); |
| 1359 | } |
| 1360 | |
| 1361 | /** |
Sunny Goyal | 4c7f215 | 2017-10-17 17:17:16 -0700 | [diff] [blame] | 1362 | * Sets the current workspace {@link LauncherState}, then animates the UI |
Sunny Goyal | aeb1643 | 2017-10-16 11:46:41 -0700 | [diff] [blame] | 1363 | */ |
Sunny Goyal | c4fa8c3 | 2017-11-07 12:23:58 -0800 | [diff] [blame] | 1364 | @Override |
Sunny Goyal | 3c585dd | 2017-12-07 11:42:33 -0800 | [diff] [blame] | 1365 | public void setStateWithAnimation(LauncherState toState, |
Sunny Goyal | e3c6d58 | 2017-12-07 12:45:49 -0800 | [diff] [blame] | 1366 | AnimatorSetBuilder builder, AnimationConfig config) { |
Sunny Goyal | c99cb17 | 2017-10-19 16:15:09 -0700 | [diff] [blame] | 1367 | StateTransitionListener listener = new StateTransitionListener(toState); |
Sunny Goyal | 3c585dd | 2017-12-07 11:42:33 -0800 | [diff] [blame] | 1368 | mStateTransitionAnimation.setStateWithAnimation(toState, builder, config); |
Winson Chung | dc61c4d | 2015-04-20 18:26:57 -0700 | [diff] [blame] | 1369 | |
Sunny Goyal | c99cb17 | 2017-10-19 16:15:09 -0700 | [diff] [blame] | 1370 | // Invalidate the pages now, so that we have the visible pages before the |
| 1371 | // animation is started |
| 1372 | if (toState.hasMultipleVisiblePages) { |
| 1373 | mForceDrawAdjacentPages = true; |
| 1374 | } |
| 1375 | invalidate(); // This will call dispatchDraw(), which calls getVisiblePages(). |
Mario Bertschler | 5a2edd6 | 2017-01-30 17:47:30 -0800 | [diff] [blame] | 1376 | |
Sunny Goyal | aeb1643 | 2017-10-16 11:46:41 -0700 | [diff] [blame] | 1377 | ValueAnimator stepAnimator = ValueAnimator.ofFloat(0, 1); |
| 1378 | stepAnimator.addUpdateListener(listener); |
Sunny Goyal | 0310220 | 2017-10-27 11:05:26 -0700 | [diff] [blame] | 1379 | stepAnimator.setDuration(config.duration); |
Sunny Goyal | e3c6d58 | 2017-12-07 12:45:49 -0800 | [diff] [blame] | 1380 | stepAnimator.addListener(listener); |
| 1381 | builder.play(stepAnimator); |
Adam Cohen | 5380521 | 2013-10-01 10:39:23 -0700 | [diff] [blame] | 1382 | } |
| 1383 | |
Sunny Goyal | 1d08f70 | 2015-05-04 15:50:25 -0700 | [diff] [blame] | 1384 | public void updateAccessibilityFlags() { |
Sunny Goyal | d8d1993 | 2016-06-22 16:29:24 -0700 | [diff] [blame] | 1385 | // TODO: Update the accessibility flags appropriately when dragging. |
Sunny Goyal | ea60926 | 2017-10-25 15:47:38 -0700 | [diff] [blame] | 1386 | int accessibilityFlag = mLauncher.getStateManager().getState().workspaceAccessibilityFlag; |
Sunny Goyal | d8d1993 | 2016-06-22 16:29:24 -0700 | [diff] [blame] | 1387 | if (!mLauncher.getAccessibilityDelegate().isInAccessibleDrag()) { |
Sunny Goyal | a52ecb0 | 2016-12-16 15:04:51 -0800 | [diff] [blame] | 1388 | int total = getPageCount(); |
Sunny Goyal | 7ce471b | 2017-08-02 03:37:39 -0700 | [diff] [blame] | 1389 | for (int i = 0; i < total; i++) { |
Sunny Goyal | 7c7be8c | 2018-03-07 19:58:07 -0800 | [diff] [blame] | 1390 | updateAccessibilityFlags(accessibilityFlag, (CellLayout) getPageAt(i)); |
Sunny Goyal | d8d1993 | 2016-06-22 16:29:24 -0700 | [diff] [blame] | 1391 | } |
Sunny Goyal | ea60926 | 2017-10-25 15:47:38 -0700 | [diff] [blame] | 1392 | setImportantForAccessibility(accessibilityFlag); |
Sunny Goyal | 1d08f70 | 2015-05-04 15:50:25 -0700 | [diff] [blame] | 1393 | } |
| 1394 | } |
| 1395 | |
Vadim Tryshev | 7acde48 | 2018-05-31 14:33:09 -0700 | [diff] [blame] | 1396 | @Override |
| 1397 | public AccessibilityNodeInfo createAccessibilityNodeInfo() { |
| 1398 | if (getImportantForAccessibility() == IMPORTANT_FOR_ACCESSIBILITY_NO_HIDE_DESCENDANTS) { |
| 1399 | // TAPL tests verify that workspace is not present in Overview and AllApps states. |
| 1400 | // TAPL can work only if UIDevice is set up as setCompressedLayoutHeirarchy(false). |
| 1401 | // Hiding workspace from the tests when it's |
| 1402 | // IMPORTANT_FOR_ACCESSIBILITY_NO_HIDE_DESCENDANTS. |
| 1403 | return null; |
| 1404 | } |
| 1405 | return super.createAccessibilityNodeInfo(); |
| 1406 | } |
| 1407 | |
Sunny Goyal | 7c7be8c | 2018-03-07 19:58:07 -0800 | [diff] [blame] | 1408 | private void updateAccessibilityFlags(int accessibilityFlag, CellLayout page) { |
| 1409 | page.setImportantForAccessibility(IMPORTANT_FOR_ACCESSIBILITY_NO); |
| 1410 | page.getShortcutsAndWidgets().setImportantForAccessibility(accessibilityFlag); |
| 1411 | page.setContentDescription(null); |
| 1412 | page.setAccessibilityDelegate(null); |
Adam Cohen | 3f452c8 | 2013-09-19 11:57:17 -0700 | [diff] [blame] | 1413 | } |
| 1414 | |
Sunny Goyal | 94b510c | 2016-08-16 15:36:48 -0700 | [diff] [blame] | 1415 | public void startDrag(CellLayout.CellInfo cellInfo, DragOptions options) { |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1416 | View child = cellInfo.cell; |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 1417 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1418 | mDragInfo = cellInfo; |
Adam Cohen | d41fbf5 | 2012-02-16 23:53:59 -0800 | [diff] [blame] | 1419 | child.setVisibility(INVISIBLE); |
Joe Onorato | 4be866d | 2010-10-10 11:26:02 -0700 | [diff] [blame] | 1420 | |
Sunny Goyal | 94b510c | 2016-08-16 15:36:48 -0700 | [diff] [blame] | 1421 | if (options.isAccessibleDrag) { |
Hyunyoung Song | 0de0117 | 2016-10-05 16:27:48 -0700 | [diff] [blame] | 1422 | mDragController.addDragListener(new AccessibleDragListenerAdapter( |
Sunny Goyal | 94b510c | 2016-08-16 15:36:48 -0700 | [diff] [blame] | 1423 | this, CellLayout.WORKSPACE_ACCESSIBILITY_DRAG) { |
| 1424 | @Override |
| 1425 | protected void enableAccessibleDrag(boolean enable) { |
| 1426 | super.enableAccessibleDrag(enable); |
Sunny Goyal | 876e462 | 2018-11-02 13:50:40 -0700 | [diff] [blame] | 1427 | setEnableForLayout(mLauncher.getHotseat(),enable); |
Sunny Goyal | 94b510c | 2016-08-16 15:36:48 -0700 | [diff] [blame] | 1428 | } |
| 1429 | }); |
| 1430 | } |
| 1431 | |
| 1432 | beginDragShared(child, this, options); |
Adam Cohen | ac8c876 | 2011-07-13 11:15:27 -0700 | [diff] [blame] | 1433 | } |
| 1434 | |
Sunny Goyal | 94b510c | 2016-08-16 15:36:48 -0700 | [diff] [blame] | 1435 | public void beginDragShared(View child, DragSource source, DragOptions options) { |
Sunny Goyal | 10923b3 | 2016-07-20 15:42:44 -0700 | [diff] [blame] | 1436 | Object dragObject = child.getTag(); |
| 1437 | if (!(dragObject instanceof ItemInfo)) { |
| 1438 | String msg = "Drag started with a view that has no tag set. This " |
| 1439 | + "will cause a crash (issue 11627249) down the line. " |
| 1440 | + "View: " + child + " tag: " + child.getTag(); |
| 1441 | throw new IllegalStateException(msg); |
| 1442 | } |
Sunny Goyal | 94b510c | 2016-08-16 15:36:48 -0700 | [diff] [blame] | 1443 | beginDragShared(child, source, (ItemInfo) dragObject, |
| 1444 | new DragPreviewProvider(child), options); |
Adam Cohen | c9735cf | 2015-01-23 16:11:55 -0800 | [diff] [blame] | 1445 | } |
| 1446 | |
Sunny Goyal | 94b510c | 2016-08-16 15:36:48 -0700 | [diff] [blame] | 1447 | public DragView beginDragShared(View child, DragSource source, ItemInfo dragObject, |
| 1448 | DragPreviewProvider previewProvider, DragOptions dragOptions) { |
Jon Miranda | 31942e7 | 2018-05-24 14:25:27 -0700 | [diff] [blame] | 1449 | float iconScale = 1f; |
| 1450 | if (child instanceof BubbleTextView) { |
| 1451 | Drawable icon = ((BubbleTextView) child).getIcon(); |
| 1452 | if (icon instanceof FastBitmapDrawable) { |
| 1453 | iconScale = ((FastBitmapDrawable) icon).getAnimatedScale(); |
| 1454 | } |
| 1455 | } |
| 1456 | |
Sunny Goyal | 508da15 | 2014-08-14 10:53:27 -0700 | [diff] [blame] | 1457 | child.clearFocus(); |
| 1458 | child.setPressed(false); |
Sunny Goyal | 06e21a2 | 2016-08-11 16:02:02 -0700 | [diff] [blame] | 1459 | mOutlineProvider = previewProvider; |
Sunny Goyal | 508da15 | 2014-08-14 10:53:27 -0700 | [diff] [blame] | 1460 | |
Joe Onorato | 4be866d | 2010-10-10 11:26:02 -0700 | [diff] [blame] | 1461 | // The drag bitmap follows the touch point around on the screen |
Sunny Goyal | aa3a2ba | 2017-09-26 12:43:16 -0700 | [diff] [blame] | 1462 | final Bitmap b = previewProvider.createDragBitmap(); |
Sunny Goyal | 157793d | 2016-07-20 14:55:26 -0700 | [diff] [blame] | 1463 | int halfPadding = previewProvider.previewPadding / 2; |
Joe Onorato | 4be866d | 2010-10-10 11:26:02 -0700 | [diff] [blame] | 1464 | |
Sunny Goyal | a2454ad | 2016-07-22 10:50:11 -0700 | [diff] [blame] | 1465 | float scale = previewProvider.getScaleAndPosition(b, mTempXY); |
| 1466 | int dragLayerX = mTempXY[0]; |
| 1467 | int dragLayerY = mTempXY[1]; |
Adam Cohen | e3e27a8 | 2011-04-15 12:07:39 -0700 | [diff] [blame] | 1468 | |
Adam Cohen | 2e6da15 | 2015-05-06 11:42:25 -0700 | [diff] [blame] | 1469 | DeviceProfile grid = mLauncher.getDeviceProfile(); |
Winson Chung | b8c69f3 | 2011-10-19 21:36:08 -0700 | [diff] [blame] | 1470 | Point dragVisualizeOffset = null; |
Adam Cohen | e3e27a8 | 2011-04-15 12:07:39 -0700 | [diff] [blame] | 1471 | Rect dragRect = null; |
Sunny Goyal | 508da15 | 2014-08-14 10:53:27 -0700 | [diff] [blame] | 1472 | if (child instanceof BubbleTextView) { |
Tony Wickham | 1237df0 | 2017-02-24 08:59:36 -0800 | [diff] [blame] | 1473 | dragRect = new Rect(); |
| 1474 | ((BubbleTextView) child).getIconBounds(dragRect); |
| 1475 | dragLayerY += dragRect.top; |
| 1476 | // Note: The dragRect is used to calculate drag layer offsets, but the |
Winson Chung | b8c69f3 | 2011-10-19 21:36:08 -0700 | [diff] [blame] | 1477 | // dragVisualizeOffset in addition to the dragRect (the size) to position the outline. |
Sunny Goyal | 157793d | 2016-07-20 14:55:26 -0700 | [diff] [blame] | 1478 | dragVisualizeOffset = new Point(- halfPadding, halfPadding); |
Adam Cohen | 0e4857c | 2011-06-30 17:05:14 -0700 | [diff] [blame] | 1479 | } else if (child instanceof FolderIcon) { |
Winson Chung | 5f8afe6 | 2013-08-12 16:19:28 -0700 | [diff] [blame] | 1480 | int previewSize = grid.folderIconSizePx; |
Sunny Goyal | 157793d | 2016-07-20 14:55:26 -0700 | [diff] [blame] | 1481 | dragVisualizeOffset = new Point(- halfPadding, halfPadding - child.getPaddingTop()); |
Winson Chung | 5f8afe6 | 2013-08-12 16:19:28 -0700 | [diff] [blame] | 1482 | dragRect = new Rect(0, child.getPaddingTop(), child.getWidth(), previewSize); |
Jon Miranda | aa6f66a | 2017-05-09 12:43:44 -0700 | [diff] [blame] | 1483 | } else if (previewProvider instanceof ShortcutDragPreviewProvider) { |
| 1484 | dragVisualizeOffset = new Point(- halfPadding, halfPadding); |
Adam Cohen | e3e27a8 | 2011-04-15 12:07:39 -0700 | [diff] [blame] | 1485 | } |
| 1486 | |
Winson Chung | 1e9cbfe | 2011-09-30 16:52:26 -0700 | [diff] [blame] | 1487 | // Clear the pressed state if necessary |
| 1488 | if (child instanceof BubbleTextView) { |
| 1489 | BubbleTextView icon = (BubbleTextView) child; |
Sunny Goyal | 508da15 | 2014-08-14 10:53:27 -0700 | [diff] [blame] | 1490 | icon.clearPressedBackground(); |
Winson Chung | 1e9cbfe | 2011-09-30 16:52:26 -0700 | [diff] [blame] | 1491 | } |
| 1492 | |
Adam Cohen | 5084cba | 2013-09-03 12:01:16 -0700 | [diff] [blame] | 1493 | if (child.getParent() instanceof ShortcutAndWidgetContainer) { |
| 1494 | mDragSourceInternal = (ShortcutAndWidgetContainer) child.getParent(); |
| 1495 | } |
| 1496 | |
Tony Wickham | 0270039 | 2017-05-11 15:20:28 -0700 | [diff] [blame] | 1497 | if (child instanceof BubbleTextView && !dragOptions.isAccessibleDrag) { |
Tony Wickham | 540913e | 2017-01-23 11:47:51 -0800 | [diff] [blame] | 1498 | PopupContainerWithArrow popupContainer = PopupContainerWithArrow |
| 1499 | .showForIcon((BubbleTextView) child); |
| 1500 | if (popupContainer != null) { |
| 1501 | dragOptions.preDragCondition = popupContainer.createPreDragCondition(); |
Jon Miranda | f3e35d9 | 2016-10-05 14:08:11 -0700 | [diff] [blame] | 1502 | |
Hyunyoung Song | b3fbc0b | 2018-02-14 13:40:25 -0800 | [diff] [blame] | 1503 | mLauncher.getUserEventDispatcher().resetElapsedContainerMillis("dragging started"); |
Tony Wickham | 10236d6 | 2016-09-28 12:49:25 -0700 | [diff] [blame] | 1504 | } |
| 1505 | } |
| 1506 | |
Sunny Goyal | aa8ef11 | 2015-06-12 20:04:41 -0700 | [diff] [blame] | 1507 | DragView dv = mDragController.startDrag(b, dragLayerX, dragLayerY, source, |
Jon Miranda | 31942e7 | 2018-05-24 14:25:27 -0700 | [diff] [blame] | 1508 | dragObject, dragVisualizeOffset, dragRect, scale * iconScale, scale, dragOptions); |
Sunny Goyal | 1ce9c47 | 2017-10-03 16:17:32 -0700 | [diff] [blame] | 1509 | dv.setIntrinsicIconScaleFactor(dragOptions.intrinsicIconScaleFactor); |
Sunny Goyal | a2454ad | 2016-07-22 10:50:11 -0700 | [diff] [blame] | 1510 | return dv; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1511 | } |
| 1512 | |
Tony Wickham | 251ab6e | 2016-12-02 18:54:32 -0800 | [diff] [blame] | 1513 | private boolean transitionStateShouldAllowDrop() { |
Sunny Goyal | c99cb17 | 2017-10-19 16:15:09 -0700 | [diff] [blame] | 1514 | return (!isSwitchingState() || mTransitionProgress > ALLOW_DROP_TRANSITION_PROGRESS) && |
| 1515 | workspaceIconsCanBeDragged(); |
Adam Cohen | 4b285c5 | 2011-07-21 14:24:06 -0700 | [diff] [blame] | 1516 | } |
| 1517 | |
Patrick Dubroy | cd68ff5 | 2010-10-28 17:57:05 -0700 | [diff] [blame] | 1518 | /** |
| 1519 | * {@inheritDoc} |
| 1520 | */ |
Sunny Goyal | 1ce9c47 | 2017-10-03 16:17:32 -0700 | [diff] [blame] | 1521 | @Override |
Adam Cohen | cb3382b | 2011-05-24 14:07:08 -0700 | [diff] [blame] | 1522 | public boolean acceptDrop(DragObject d) { |
Patrick Dubroy | cd68ff5 | 2010-10-28 17:57:05 -0700 | [diff] [blame] | 1523 | // If it's an external drop (e.g. from All Apps), check if it should be accepted |
Adam Cohen | c6cc61d | 2012-04-04 12:47:08 -0700 | [diff] [blame] | 1524 | CellLayout dropTargetLayout = mDropToLayout; |
Adam Cohen | cb3382b | 2011-05-24 14:07:08 -0700 | [diff] [blame] | 1525 | if (d.dragSource != this) { |
Patrick Dubroy | cd68ff5 | 2010-10-28 17:57:05 -0700 | [diff] [blame] | 1526 | // Don't accept the drop if we're not over a screen at time of drop |
Adam Cohen | c6cc61d | 2012-04-04 12:47:08 -0700 | [diff] [blame] | 1527 | if (dropTargetLayout == null) { |
Patrick Dubroy | cd68ff5 | 2010-10-28 17:57:05 -0700 | [diff] [blame] | 1528 | return false; |
| 1529 | } |
Adam Cohen | 4b285c5 | 2011-07-21 14:24:06 -0700 | [diff] [blame] | 1530 | if (!transitionStateShouldAllowDrop()) return false; |
Patrick Dubroy | cd68ff5 | 2010-10-28 17:57:05 -0700 | [diff] [blame] | 1531 | |
Sunny Goyal | 1587d53 | 2015-01-29 09:57:16 -0800 | [diff] [blame] | 1532 | mDragViewVisualCenter = d.getVisualCenter(mDragViewVisualCenter); |
Adam Cohen | a65beee | 2011-06-27 21:32:23 -0700 | [diff] [blame] | 1533 | |
Adam Cohen | 2e9f4fb | 2011-08-30 22:46:55 -0700 | [diff] [blame] | 1534 | // We want the point to be mapped to the dragTarget. |
Sunny Goyal | 024659c | 2018-11-08 11:10:20 -0800 | [diff] [blame] | 1535 | mapPointFromDropLayout(dropTargetLayout, mDragViewVisualCenter); |
Adam Cohen | 2e9f4fb | 2011-08-30 22:46:55 -0700 | [diff] [blame] | 1536 | |
Rajeev Kumar | 93c0512 | 2017-06-12 12:59:47 -0700 | [diff] [blame] | 1537 | int spanX; |
| 1538 | int spanY; |
Winson Chung | 557d6ed | 2011-07-08 15:34:52 -0700 | [diff] [blame] | 1539 | if (mDragInfo != null) { |
| 1540 | final CellLayout.CellInfo dragCellInfo = mDragInfo; |
| 1541 | spanX = dragCellInfo.spanX; |
| 1542 | spanY = dragCellInfo.spanY; |
Winson Chung | 557d6ed | 2011-07-08 15:34:52 -0700 | [diff] [blame] | 1543 | } else { |
Sunny Goyal | aa8ef11 | 2015-06-12 20:04:41 -0700 | [diff] [blame] | 1544 | spanX = d.dragInfo.spanX; |
| 1545 | spanY = d.dragInfo.spanY; |
Winson Chung | 557d6ed | 2011-07-08 15:34:52 -0700 | [diff] [blame] | 1546 | } |
Patrick Dubroy | cd68ff5 | 2010-10-28 17:57:05 -0700 | [diff] [blame] | 1547 | |
Adam Cohen | d41fbf5 | 2012-02-16 23:53:59 -0800 | [diff] [blame] | 1548 | int minSpanX = spanX; |
| 1549 | int minSpanY = spanY; |
| 1550 | if (d.dragInfo instanceof PendingAddWidgetInfo) { |
| 1551 | minSpanX = ((PendingAddWidgetInfo) d.dragInfo).minSpanX; |
| 1552 | minSpanY = ((PendingAddWidgetInfo) d.dragInfo).minSpanY; |
| 1553 | } |
Adam Cohen | 482ed82 | 2012-03-02 14:15:13 -0800 | [diff] [blame] | 1554 | |
Adam Cohen | a65beee | 2011-06-27 21:32:23 -0700 | [diff] [blame] | 1555 | mTargetCell = findNearestArea((int) mDragViewVisualCenter[0], |
Adam Cohen | c6cc61d | 2012-04-04 12:47:08 -0700 | [diff] [blame] | 1556 | (int) mDragViewVisualCenter[1], minSpanX, minSpanY, dropTargetLayout, |
Adam Cohen | f0777b9 | 2012-02-28 14:02:45 -0800 | [diff] [blame] | 1557 | mTargetCell); |
Adam Cohen | c6cc61d | 2012-04-04 12:47:08 -0700 | [diff] [blame] | 1558 | float distance = dropTargetLayout.getDistanceFromCell(mDragViewVisualCenter[0], |
Adam Cohen | 482ed82 | 2012-03-02 14:15:13 -0800 | [diff] [blame] | 1559 | mDragViewVisualCenter[1], mTargetCell); |
Sunny Goyal | aa8ef11 | 2015-06-12 20:04:41 -0700 | [diff] [blame] | 1560 | if (mCreateUserFolderOnDrop && willCreateUserFolder(d.dragInfo, |
Adam Cohen | ec0d61d | 2014-02-07 16:34:51 -0800 | [diff] [blame] | 1561 | dropTargetLayout, mTargetCell, distance, true)) { |
Adam Cohen | a65beee | 2011-06-27 21:32:23 -0700 | [diff] [blame] | 1562 | return true; |
| 1563 | } |
Adam Cohen | ec0d61d | 2014-02-07 16:34:51 -0800 | [diff] [blame] | 1564 | |
Sunny Goyal | aa8ef11 | 2015-06-12 20:04:41 -0700 | [diff] [blame] | 1565 | if (mAddToExistingFolderOnDrop && willAddToExistingUserFolder(d.dragInfo, |
Adam Cohen | ec0d61d | 2014-02-07 16:34:51 -0800 | [diff] [blame] | 1566 | dropTargetLayout, mTargetCell, distance)) { |
Adam Cohen | a65beee | 2011-06-27 21:32:23 -0700 | [diff] [blame] | 1567 | return true; |
| 1568 | } |
| 1569 | |
Adam Cohen | 482ed82 | 2012-03-02 14:15:13 -0800 | [diff] [blame] | 1570 | int[] resultSpan = new int[2]; |
Adam Cohen | fa3c58f | 2013-12-06 16:10:55 -0800 | [diff] [blame] | 1571 | mTargetCell = dropTargetLayout.performReorder((int) mDragViewVisualCenter[0], |
Adam Cohen | 482ed82 | 2012-03-02 14:15:13 -0800 | [diff] [blame] | 1572 | (int) mDragViewVisualCenter[1], minSpanX, minSpanY, spanX, spanY, |
| 1573 | null, mTargetCell, resultSpan, CellLayout.MODE_ACCEPT_DROP); |
| 1574 | boolean foundCell = mTargetCell[0] >= 0 && mTargetCell[1] >= 0; |
| 1575 | |
Patrick Dubroy | cd68ff5 | 2010-10-28 17:57:05 -0700 | [diff] [blame] | 1576 | // Don't accept the drop if there's no room for the item |
Adam Cohen | 482ed82 | 2012-03-02 14:15:13 -0800 | [diff] [blame] | 1577 | if (!foundCell) { |
Jon Miranda | 9485e5f | 2016-10-18 11:47:42 -0700 | [diff] [blame] | 1578 | onNoCellFound(dropTargetLayout); |
Patrick Dubroy | cd68ff5 | 2010-10-28 17:57:05 -0700 | [diff] [blame] | 1579 | return false; |
| 1580 | } |
| 1581 | } |
Adam Cohen | dedbd96 | 2013-07-11 14:21:49 -0700 | [diff] [blame] | 1582 | |
Sunny Goyal | efb7e84 | 2018-10-04 15:11:00 -0700 | [diff] [blame] | 1583 | int screenId = getIdForScreen(dropTargetLayout); |
Adam Cohen | dedbd96 | 2013-07-11 14:21:49 -0700 | [diff] [blame] | 1584 | if (screenId == EXTRA_EMPTY_SCREEN_ID) { |
| 1585 | commitExtraEmptyScreen(); |
| 1586 | } |
| 1587 | |
Patrick Dubroy | cd68ff5 | 2010-10-28 17:57:05 -0700 | [diff] [blame] | 1588 | return true; |
| 1589 | } |
| 1590 | |
Sunny Goyal | e78e3d7 | 2015-09-24 11:23:31 -0700 | [diff] [blame] | 1591 | boolean willCreateUserFolder(ItemInfo info, CellLayout target, int[] targetCell, |
| 1592 | float distance, boolean considerTimeout) { |
Adam Cohen | 482ed82 | 2012-03-02 14:15:13 -0800 | [diff] [blame] | 1593 | if (distance > mMaxDistanceForFolderCreation) return false; |
Adam Cohen | a65beee | 2011-06-27 21:32:23 -0700 | [diff] [blame] | 1594 | View dropOverView = target.getChildAt(targetCell[0], targetCell[1]); |
Sunny Goyal | e78e3d7 | 2015-09-24 11:23:31 -0700 | [diff] [blame] | 1595 | return willCreateUserFolder(info, dropOverView, considerTimeout); |
| 1596 | } |
Adam Cohen | a65beee | 2011-06-27 21:32:23 -0700 | [diff] [blame] | 1597 | |
Sunny Goyal | e78e3d7 | 2015-09-24 11:23:31 -0700 | [diff] [blame] | 1598 | boolean willCreateUserFolder(ItemInfo info, View dropOverView, boolean considerTimeout) { |
Adam Cohen | 19f3792 | 2012-03-21 11:59:11 -0700 | [diff] [blame] | 1599 | if (dropOverView != null) { |
| 1600 | CellLayout.LayoutParams lp = (CellLayout.LayoutParams) dropOverView.getLayoutParams(); |
Sunny Goyal | b740f59 | 2015-12-17 23:22:42 -0800 | [diff] [blame] | 1601 | if (lp.useTmpCoords && (lp.tmpCellX != lp.cellX || lp.tmpCellY != lp.cellY)) { |
Adam Cohen | 19f3792 | 2012-03-21 11:59:11 -0700 | [diff] [blame] | 1602 | return false; |
| 1603 | } |
| 1604 | } |
| 1605 | |
Winson Chung | 3d503fb | 2011-07-13 17:25:49 -0700 | [diff] [blame] | 1606 | boolean hasntMoved = false; |
| 1607 | if (mDragInfo != null) { |
Adam Cohen | 482ed82 | 2012-03-02 14:15:13 -0800 | [diff] [blame] | 1608 | hasntMoved = dropOverView == mDragInfo.cell; |
Winson Chung | 3d503fb | 2011-07-13 17:25:49 -0700 | [diff] [blame] | 1609 | } |
Adam Cohen | e031096 | 2011-04-18 16:15:31 -0700 | [diff] [blame] | 1610 | |
Adam Cohen | a65beee | 2011-06-27 21:32:23 -0700 | [diff] [blame] | 1611 | if (dropOverView == null || hasntMoved || (considerTimeout && !mCreateUserFolderOnDrop)) { |
| 1612 | return false; |
| 1613 | } |
Adam Cohen | e031096 | 2011-04-18 16:15:31 -0700 | [diff] [blame] | 1614 | |
Adam Cohen | a65beee | 2011-06-27 21:32:23 -0700 | [diff] [blame] | 1615 | boolean aboveShortcut = (dropOverView.getTag() instanceof ShortcutInfo); |
Adam Cohen | e031096 | 2011-04-18 16:15:31 -0700 | [diff] [blame] | 1616 | boolean willBecomeShortcut = |
Jon Miranda | 4334874 | 2019-03-06 10:40:13 -0800 | [diff] [blame] | 1617 | (info.itemType == ITEM_TYPE_APPLICATION || |
Tony Wickham | bfbf7f9 | 2016-05-19 11:19:39 -0700 | [diff] [blame] | 1618 | info.itemType == LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT || |
| 1619 | info.itemType == LauncherSettings.Favorites.ITEM_TYPE_DEEP_SHORTCUT); |
Adam Cohen | e031096 | 2011-04-18 16:15:31 -0700 | [diff] [blame] | 1620 | |
| 1621 | return (aboveShortcut && willBecomeShortcut); |
| 1622 | } |
| 1623 | |
Sunny Goyal | aa8ef11 | 2015-06-12 20:04:41 -0700 | [diff] [blame] | 1624 | boolean willAddToExistingUserFolder(ItemInfo dragInfo, CellLayout target, int[] targetCell, |
Adam Cohen | 482ed82 | 2012-03-02 14:15:13 -0800 | [diff] [blame] | 1625 | float distance) { |
| 1626 | if (distance > mMaxDistanceForFolderCreation) return false; |
Adam Cohen | a65beee | 2011-06-27 21:32:23 -0700 | [diff] [blame] | 1627 | View dropOverView = target.getChildAt(targetCell[0], targetCell[1]); |
Sunny Goyal | e78e3d7 | 2015-09-24 11:23:31 -0700 | [diff] [blame] | 1628 | return willAddToExistingUserFolder(dragInfo, dropOverView); |
Adam Cohen | 19f3792 | 2012-03-21 11:59:11 -0700 | [diff] [blame] | 1629 | |
Sunny Goyal | e78e3d7 | 2015-09-24 11:23:31 -0700 | [diff] [blame] | 1630 | } |
Sunny Goyal | 9d43808 | 2015-09-25 11:50:16 -0700 | [diff] [blame] | 1631 | boolean willAddToExistingUserFolder(ItemInfo dragInfo, View dropOverView) { |
Adam Cohen | 19f3792 | 2012-03-21 11:59:11 -0700 | [diff] [blame] | 1632 | if (dropOverView != null) { |
| 1633 | CellLayout.LayoutParams lp = (CellLayout.LayoutParams) dropOverView.getLayoutParams(); |
Sunny Goyal | b740f59 | 2015-12-17 23:22:42 -0800 | [diff] [blame] | 1634 | if (lp.useTmpCoords && (lp.tmpCellX != lp.cellX || lp.tmpCellY != lp.cellY)) { |
Adam Cohen | 19f3792 | 2012-03-21 11:59:11 -0700 | [diff] [blame] | 1635 | return false; |
| 1636 | } |
| 1637 | } |
| 1638 | |
Adam Cohen | a65beee | 2011-06-27 21:32:23 -0700 | [diff] [blame] | 1639 | if (dropOverView instanceof FolderIcon) { |
| 1640 | FolderIcon fi = (FolderIcon) dropOverView; |
| 1641 | if (fi.acceptDrop(dragInfo)) { |
| 1642 | return true; |
| 1643 | } |
| 1644 | } |
| 1645 | return false; |
| 1646 | } |
| 1647 | |
Sunny Goyal | efb7e84 | 2018-10-04 15:11:00 -0700 | [diff] [blame] | 1648 | boolean createUserFolderIfNecessary(View newView, int container, CellLayout target, |
Sunny Goyal | 1797af4 | 2017-10-06 13:29:57 -0700 | [diff] [blame] | 1649 | int[] targetCell, float distance, boolean external, DragView dragView) { |
Adam Cohen | 482ed82 | 2012-03-02 14:15:13 -0800 | [diff] [blame] | 1650 | if (distance > mMaxDistanceForFolderCreation) return false; |
Adam Cohen | c0dcf59 | 2011-06-01 15:30:43 -0700 | [diff] [blame] | 1651 | View v = target.getChildAt(targetCell[0], targetCell[1]); |
Adam Cohen | 19f3792 | 2012-03-21 11:59:11 -0700 | [diff] [blame] | 1652 | |
Winson Chung | ec9a0a4 | 2011-07-20 20:42:13 -0700 | [diff] [blame] | 1653 | boolean hasntMoved = false; |
| 1654 | if (mDragInfo != null) { |
| 1655 | CellLayout cellParent = getParentCellLayoutForView(mDragInfo.cell); |
| 1656 | hasntMoved = (mDragInfo.cellX == targetCell[0] && |
| 1657 | mDragInfo.cellY == targetCell[1]) && (cellParent == target); |
| 1658 | } |
Adam Cohen | 10b1737 | 2011-04-15 14:21:25 -0700 | [diff] [blame] | 1659 | |
Adam Cohen | 19072da | 2011-05-31 14:30:45 -0700 | [diff] [blame] | 1660 | if (v == null || hasntMoved || !mCreateUserFolderOnDrop) return false; |
| 1661 | mCreateUserFolderOnDrop = false; |
Sunny Goyal | efb7e84 | 2018-10-04 15:11:00 -0700 | [diff] [blame] | 1662 | final int screenId = getIdForScreen(target); |
Adam Cohen | 10b1737 | 2011-04-15 14:21:25 -0700 | [diff] [blame] | 1663 | |
| 1664 | boolean aboveShortcut = (v.getTag() instanceof ShortcutInfo); |
| 1665 | boolean willBecomeShortcut = (newView.getTag() instanceof ShortcutInfo); |
| 1666 | |
| 1667 | if (aboveShortcut && willBecomeShortcut) { |
| 1668 | ShortcutInfo sourceInfo = (ShortcutInfo) newView.getTag(); |
| 1669 | ShortcutInfo destInfo = (ShortcutInfo) v.getTag(); |
| 1670 | // if the drag started here, we need to remove it from the workspace |
| 1671 | if (!external) { |
Winson Chung | 3d503fb | 2011-07-13 17:25:49 -0700 | [diff] [blame] | 1672 | getParentCellLayoutForView(mDragInfo.cell).removeView(mDragInfo.cell); |
Adam Cohen | 10b1737 | 2011-04-15 14:21:25 -0700 | [diff] [blame] | 1673 | } |
| 1674 | |
Adam Cohen | d044526 | 2011-07-04 23:53:22 -0700 | [diff] [blame] | 1675 | Rect folderLocation = new Rect(); |
Adam Cohen | ac8c876 | 2011-07-13 11:15:27 -0700 | [diff] [blame] | 1676 | float scale = mLauncher.getDragLayer().getDescendantRectRelativeToSelf(v, folderLocation); |
Adam Cohen | 10b1737 | 2011-04-15 14:21:25 -0700 | [diff] [blame] | 1677 | target.removeView(v); |
Adam Cohen | d044526 | 2011-07-04 23:53:22 -0700 | [diff] [blame] | 1678 | |
Winson Chung | 3d503fb | 2011-07-13 17:25:49 -0700 | [diff] [blame] | 1679 | FolderIcon fi = |
Adam Cohen | dcd297f | 2013-06-18 13:13:40 -0700 | [diff] [blame] | 1680 | mLauncher.addFolder(target, container, screenId, targetCell[0], targetCell[1]); |
Adam Cohen | a9cf38f | 2011-05-02 15:36:58 -0700 | [diff] [blame] | 1681 | destInfo.cellX = -1; |
| 1682 | destInfo.cellY = -1; |
| 1683 | sourceInfo.cellX = -1; |
| 1684 | sourceInfo.cellY = -1; |
Adam Cohen | d044526 | 2011-07-04 23:53:22 -0700 | [diff] [blame] | 1685 | |
Adam Cohen | 558baaf | 2011-08-15 15:22:57 -0700 | [diff] [blame] | 1686 | // If the dragView is null, we can't animate |
| 1687 | boolean animate = dragView != null; |
| 1688 | if (animate) { |
Adam Cohen | efca027 | 2016-02-24 19:19:06 -0800 | [diff] [blame] | 1689 | // In order to keep everything continuous, we hand off the currently rendered |
| 1690 | // folder background to the newly created icon. This preserves animation state. |
| 1691 | fi.setFolderBackground(mFolderCreateBg); |
Jon Miranda | a0233f7 | 2017-06-22 18:34:45 -0700 | [diff] [blame] | 1692 | mFolderCreateBg = new PreviewBackground(); |
Sunny Goyal | 1797af4 | 2017-10-06 13:29:57 -0700 | [diff] [blame] | 1693 | fi.performCreateAnimation(destInfo, v, sourceInfo, dragView, folderLocation, scale |
| 1694 | ); |
Adam Cohen | 558baaf | 2011-08-15 15:22:57 -0700 | [diff] [blame] | 1695 | } else { |
Jon Miranda | 6c5d102 | 2017-07-06 09:55:50 -0700 | [diff] [blame] | 1696 | fi.prepareCreateAnimation(v); |
Adam Cohen | 558baaf | 2011-08-15 15:22:57 -0700 | [diff] [blame] | 1697 | fi.addItem(destInfo); |
| 1698 | fi.addItem(sourceInfo); |
| 1699 | } |
Adam Cohen | 10b1737 | 2011-04-15 14:21:25 -0700 | [diff] [blame] | 1700 | return true; |
| 1701 | } |
| 1702 | return false; |
| 1703 | } |
| 1704 | |
Adam Cohen | a65beee | 2011-06-27 21:32:23 -0700 | [diff] [blame] | 1705 | boolean addToExistingFolderIfNecessary(View newView, CellLayout target, int[] targetCell, |
Adam Cohen | 482ed82 | 2012-03-02 14:15:13 -0800 | [diff] [blame] | 1706 | float distance, DragObject d, boolean external) { |
| 1707 | if (distance > mMaxDistanceForFolderCreation) return false; |
| 1708 | |
Adam Cohen | a65beee | 2011-06-27 21:32:23 -0700 | [diff] [blame] | 1709 | View dropOverView = target.getChildAt(targetCell[0], targetCell[1]); |
Adam Cohen | c6cc61d | 2012-04-04 12:47:08 -0700 | [diff] [blame] | 1710 | if (!mAddToExistingFolderOnDrop) return false; |
| 1711 | mAddToExistingFolderOnDrop = false; |
Adam Cohen | 19f3792 | 2012-03-21 11:59:11 -0700 | [diff] [blame] | 1712 | |
Adam Cohen | c0dcf59 | 2011-06-01 15:30:43 -0700 | [diff] [blame] | 1713 | if (dropOverView instanceof FolderIcon) { |
| 1714 | FolderIcon fi = (FolderIcon) dropOverView; |
Adam Cohen | 3e8f811 | 2011-07-02 18:03:00 -0700 | [diff] [blame] | 1715 | if (fi.acceptDrop(d.dragInfo)) { |
Jon Miranda | 6d3586c | 2017-09-08 12:39:10 -0700 | [diff] [blame] | 1716 | fi.onDrop(d, false /* itemReturnedOnFailedDrop */); |
Adam Cohen | c0dcf59 | 2011-06-01 15:30:43 -0700 | [diff] [blame] | 1717 | |
| 1718 | // if the drag started here, we need to remove it from the workspace |
| 1719 | if (!external) { |
Winson Chung | 3d503fb | 2011-07-13 17:25:49 -0700 | [diff] [blame] | 1720 | getParentCellLayoutForView(mDragInfo.cell).removeView(mDragInfo.cell); |
Adam Cohen | c0dcf59 | 2011-06-01 15:30:43 -0700 | [diff] [blame] | 1721 | } |
| 1722 | return true; |
| 1723 | } |
| 1724 | } |
| 1725 | return false; |
| 1726 | } |
| 1727 | |
Sunny Goyal | e9b651e | 2015-04-24 11:44:51 -0700 | [diff] [blame] | 1728 | @Override |
| 1729 | public void prepareAccessibilityDrop() { } |
| 1730 | |
Sunny Goyal | 3dce5f3 | 2017-10-05 11:40:05 -0700 | [diff] [blame] | 1731 | public void onDrop(final DragObject d, DragOptions options) { |
Sunny Goyal | 1587d53 | 2015-01-29 09:57:16 -0800 | [diff] [blame] | 1732 | mDragViewVisualCenter = d.getVisualCenter(mDragViewVisualCenter); |
Adam Cohen | c6cc61d | 2012-04-04 12:47:08 -0700 | [diff] [blame] | 1733 | CellLayout dropTargetLayout = mDropToLayout; |
| 1734 | |
Adam Cohen | e3e27a8 | 2011-04-15 12:07:39 -0700 | [diff] [blame] | 1735 | // We want the point to be mapped to the dragTarget. |
Adam Cohen | c6cc61d | 2012-04-04 12:47:08 -0700 | [diff] [blame] | 1736 | if (dropTargetLayout != null) { |
Sunny Goyal | 024659c | 2018-11-08 11:10:20 -0800 | [diff] [blame] | 1737 | mapPointFromDropLayout(dropTargetLayout, mDragViewVisualCenter); |
Adam Cohen | ba78161 | 2011-05-09 14:37:39 -0700 | [diff] [blame] | 1738 | } |
Michael Jurka | c6ee42e | 2010-09-30 12:04:50 -0700 | [diff] [blame] | 1739 | |
Tony Wickham | 0270039 | 2017-05-11 15:20:28 -0700 | [diff] [blame] | 1740 | boolean droppedOnOriginalCell = false; |
| 1741 | |
Sunny Goyal | ce5a7e5 | 2015-07-08 15:44:27 -0700 | [diff] [blame] | 1742 | int snapScreen = -1; |
Adam Cohen | d41fbf5 | 2012-02-16 23:53:59 -0800 | [diff] [blame] | 1743 | boolean resizeOnDrop = false; |
Sunny Goyal | 1797af4 | 2017-10-06 13:29:57 -0700 | [diff] [blame] | 1744 | if (d.dragSource != this || mDragInfo == null) { |
Adam Cohen | e3e27a8 | 2011-04-15 12:07:39 -0700 | [diff] [blame] | 1745 | final int[] touchXY = new int[] { (int) mDragViewVisualCenter[0], |
| 1746 | (int) mDragViewVisualCenter[1] }; |
Sunny Goyal | dec3a90 | 2017-01-25 18:23:36 -0800 | [diff] [blame] | 1747 | onDropExternal(touchXY, dropTargetLayout, d); |
Sunny Goyal | 1797af4 | 2017-10-06 13:29:57 -0700 | [diff] [blame] | 1748 | } else { |
Patrick Dubroy | ce34a97 | 2010-10-19 10:34:32 -0700 | [diff] [blame] | 1749 | final View cell = mDragInfo.cell; |
Tony Wickham | ea62fe0 | 2016-10-05 09:05:42 -0700 | [diff] [blame] | 1750 | boolean droppedOnOriginalCellDuringTransition = false; |
Sunny Goyal | 1797af4 | 2017-10-06 13:29:57 -0700 | [diff] [blame] | 1751 | Runnable onCompleteRunnable = null; |
Patrick Dubroy | 54fa3b9 | 2010-11-17 12:18:45 -0800 | [diff] [blame] | 1752 | |
Michael Jurka | 1e2f465 | 2013-07-08 18:03:46 -0700 | [diff] [blame] | 1753 | if (dropTargetLayout != null && !d.cancelled) { |
Winson Chung | 3d503fb | 2011-07-13 17:25:49 -0700 | [diff] [blame] | 1754 | // Move internally |
Winson Chung | 40e882b | 2011-07-21 19:01:11 -0700 | [diff] [blame] | 1755 | boolean hasMovedLayouts = (getParentCellLayoutForView(cell) != dropTargetLayout); |
| 1756 | boolean hasMovedIntoHotseat = mLauncher.isHotseatLayout(dropTargetLayout); |
Sunny Goyal | efb7e84 | 2018-10-04 15:11:00 -0700 | [diff] [blame] | 1757 | int container = hasMovedIntoHotseat ? |
Winson Chung | 3d503fb | 2011-07-13 17:25:49 -0700 | [diff] [blame] | 1758 | LauncherSettings.Favorites.CONTAINER_HOTSEAT : |
| 1759 | LauncherSettings.Favorites.CONTAINER_DESKTOP; |
Sunny Goyal | efb7e84 | 2018-10-04 15:11:00 -0700 | [diff] [blame] | 1760 | int screenId = (mTargetCell[0] < 0) ? |
Adam Cohen | dcd297f | 2013-06-18 13:13:40 -0700 | [diff] [blame] | 1761 | mDragInfo.screenId : getIdForScreen(dropTargetLayout); |
Adam Cohen | c0dcf59 | 2011-06-01 15:30:43 -0700 | [diff] [blame] | 1762 | int spanX = mDragInfo != null ? mDragInfo.spanX : 1; |
| 1763 | int spanY = mDragInfo != null ? mDragInfo.spanY : 1; |
| 1764 | // First we find the cell nearest to point at which the item is |
| 1765 | // dropped, without any consideration to whether there is an item there. |
Adam Cohen | 482ed82 | 2012-03-02 14:15:13 -0800 | [diff] [blame] | 1766 | |
Adam Cohen | c0dcf59 | 2011-06-01 15:30:43 -0700 | [diff] [blame] | 1767 | mTargetCell = findNearestArea((int) mDragViewVisualCenter[0], (int) |
| 1768 | mDragViewVisualCenter[1], spanX, spanY, dropTargetLayout, mTargetCell); |
Adam Cohen | 482ed82 | 2012-03-02 14:15:13 -0800 | [diff] [blame] | 1769 | float distance = dropTargetLayout.getDistanceFromCell(mDragViewVisualCenter[0], |
| 1770 | mDragViewVisualCenter[1], mTargetCell); |
| 1771 | |
Adam Cohen | c0dcf59 | 2011-06-01 15:30:43 -0700 | [diff] [blame] | 1772 | // If the item being dropped is a shortcut and the nearest drop |
Adam Cohen | 76078c4 | 2011-06-09 15:06:52 -0700 | [diff] [blame] | 1773 | // cell also contains a shortcut, then create a folder with the two shortcuts. |
Sunny Goyal | f5440cb | 2016-12-14 15:13:00 -0800 | [diff] [blame] | 1774 | if (createUserFolderIfNecessary(cell, container, |
Sunny Goyal | 1797af4 | 2017-10-06 13:29:57 -0700 | [diff] [blame] | 1775 | dropTargetLayout, mTargetCell, distance, false, d.dragView) || |
| 1776 | addToExistingFolderIfNecessary(cell, dropTargetLayout, mTargetCell, |
| 1777 | distance, d, false)) { |
Sunny Goyal | 3e3f44c | 2017-10-23 17:14:52 -0700 | [diff] [blame] | 1778 | mLauncher.getStateManager().goToState(NORMAL, SPRING_LOADED_EXIT_DELAY); |
Adam Cohen | df03538 | 2011-04-11 17:22:04 -0700 | [diff] [blame] | 1779 | return; |
| 1780 | } |
| 1781 | |
| 1782 | // Aside from the special case where we're dropping a shortcut onto a shortcut, |
| 1783 | // we need to find the nearest cell location that is vacant |
Sunny Goyal | aa8ef11 | 2015-06-12 20:04:41 -0700 | [diff] [blame] | 1784 | ItemInfo item = d.dragInfo; |
Adam Cohen | d41fbf5 | 2012-02-16 23:53:59 -0800 | [diff] [blame] | 1785 | int minSpanX = item.spanX; |
| 1786 | int minSpanY = item.spanY; |
| 1787 | if (item.minSpanX > 0 && item.minSpanY > 0) { |
| 1788 | minSpanX = item.minSpanX; |
| 1789 | minSpanY = item.minSpanY; |
| 1790 | } |
Adam Cohen | 482ed82 | 2012-03-02 14:15:13 -0800 | [diff] [blame] | 1791 | |
Tony Wickham | 0270039 | 2017-05-11 15:20:28 -0700 | [diff] [blame] | 1792 | droppedOnOriginalCell = item.screenId == screenId && item.container == container |
Tony Wickham | ea62fe0 | 2016-10-05 09:05:42 -0700 | [diff] [blame] | 1793 | && item.cellX == mTargetCell[0] && item.cellY == mTargetCell[1]; |
Tony Wickham | 0270039 | 2017-05-11 15:20:28 -0700 | [diff] [blame] | 1794 | droppedOnOriginalCellDuringTransition = droppedOnOriginalCell && mIsSwitchingState; |
Tony Wickham | ea62fe0 | 2016-10-05 09:05:42 -0700 | [diff] [blame] | 1795 | |
Tony Wickham | 5dc7435 | 2016-12-06 16:24:43 -0800 | [diff] [blame] | 1796 | // When quickly moving an item, a user may accidentally rearrange their |
| 1797 | // workspace. So instead we move the icon back safely to its original position. |
| 1798 | boolean returnToOriginalCellToPreventShuffling = !isFinishedSwitchingState() |
| 1799 | && !droppedOnOriginalCellDuringTransition && !dropTargetLayout |
| 1800 | .isRegionVacant(mTargetCell[0], mTargetCell[1], spanX, spanY); |
Adam Cohen | d41fbf5 | 2012-02-16 23:53:59 -0800 | [diff] [blame] | 1801 | int[] resultSpan = new int[2]; |
Tony Wickham | 5dc7435 | 2016-12-06 16:24:43 -0800 | [diff] [blame] | 1802 | if (returnToOriginalCellToPreventShuffling) { |
| 1803 | mTargetCell[0] = mTargetCell[1] = -1; |
| 1804 | } else { |
| 1805 | mTargetCell = dropTargetLayout.performReorder((int) mDragViewVisualCenter[0], |
| 1806 | (int) mDragViewVisualCenter[1], minSpanX, minSpanY, spanX, spanY, cell, |
| 1807 | mTargetCell, resultSpan, CellLayout.MODE_ON_DROP); |
| 1808 | } |
Adam Cohen | 482ed82 | 2012-03-02 14:15:13 -0800 | [diff] [blame] | 1809 | |
Adam Cohen | d41fbf5 | 2012-02-16 23:53:59 -0800 | [diff] [blame] | 1810 | boolean foundCell = mTargetCell[0] >= 0 && mTargetCell[1] >= 0; |
Adam Cohen | 9e05a5e | 2012-09-10 15:53:09 -0700 | [diff] [blame] | 1811 | |
Adam Cohen | aaa5c21 | 2012-10-05 18:14:31 -0700 | [diff] [blame] | 1812 | // if the widget resizes on drop |
Adam Cohen | 9e05a5e | 2012-09-10 15:53:09 -0700 | [diff] [blame] | 1813 | if (foundCell && (cell instanceof AppWidgetHostView) && |
Adam Cohen | aaa5c21 | 2012-10-05 18:14:31 -0700 | [diff] [blame] | 1814 | (resultSpan[0] != item.spanX || resultSpan[1] != item.spanY)) { |
Adam Cohen | d41fbf5 | 2012-02-16 23:53:59 -0800 | [diff] [blame] | 1815 | resizeOnDrop = true; |
| 1816 | item.spanX = resultSpan[0]; |
| 1817 | item.spanY = resultSpan[1]; |
Adam Cohen | 7bdfc97 | 2012-05-22 16:50:35 -0700 | [diff] [blame] | 1818 | AppWidgetHostView awhv = (AppWidgetHostView) cell; |
| 1819 | AppWidgetResizeFrame.updateWidgetSizeRanges(awhv, mLauncher, resultSpan[0], |
| 1820 | resultSpan[1]); |
Adam Cohen | d41fbf5 | 2012-02-16 23:53:59 -0800 | [diff] [blame] | 1821 | } |
Adam Cohen | df03538 | 2011-04-11 17:22:04 -0700 | [diff] [blame] | 1822 | |
Adam Cohen | d41fbf5 | 2012-02-16 23:53:59 -0800 | [diff] [blame] | 1823 | if (foundCell) { |
Tony Wickham | d1f0347 | 2016-10-24 10:27:56 -0700 | [diff] [blame] | 1824 | if (getScreenIdForPageIndex(mCurrentPage) != screenId && !hasMovedIntoHotseat) { |
| 1825 | snapScreen = getPageIndexForScreenId(screenId); |
| 1826 | snapToPage(snapScreen); |
| 1827 | } |
| 1828 | |
Adam Cohen | d41fbf5 | 2012-02-16 23:53:59 -0800 | [diff] [blame] | 1829 | final ItemInfo info = (ItemInfo) cell.getTag(); |
Winson Chung | 40e882b | 2011-07-21 19:01:11 -0700 | [diff] [blame] | 1830 | if (hasMovedLayouts) { |
Patrick Dubroy | 9438336 | 2010-10-29 15:03:24 -0700 | [diff] [blame] | 1831 | // Reparent the view |
Sunny Goyal | 25611b1 | 2014-07-22 09:52:37 -0700 | [diff] [blame] | 1832 | CellLayout parentCell = getParentCellLayoutForView(cell); |
| 1833 | if (parentCell != null) { |
| 1834 | parentCell.removeView(cell); |
Sunny Goyal | 3d706ad | 2017-03-06 16:56:39 -0800 | [diff] [blame] | 1835 | } else if (FeatureFlags.IS_DOGFOOD_BUILD) { |
Sunny Goyal | 25611b1 | 2014-07-22 09:52:37 -0700 | [diff] [blame] | 1836 | throw new NullPointerException("mDragInfo.cell has null parent"); |
| 1837 | } |
Adam Cohen | dcd297f | 2013-06-18 13:13:40 -0700 | [diff] [blame] | 1838 | addInScreen(cell, container, screenId, mTargetCell[0], mTargetCell[1], |
Adam Cohen | d41fbf5 | 2012-02-16 23:53:59 -0800 | [diff] [blame] | 1839 | info.spanX, info.spanY); |
Patrick Dubroy | 9438336 | 2010-10-29 15:03:24 -0700 | [diff] [blame] | 1840 | } |
| 1841 | |
| 1842 | // update the item's position after drop |
Patrick Dubroy | 9438336 | 2010-10-29 15:03:24 -0700 | [diff] [blame] | 1843 | CellLayout.LayoutParams lp = (CellLayout.LayoutParams) cell.getLayoutParams(); |
Adam Cohen | 19f3792 | 2012-03-21 11:59:11 -0700 | [diff] [blame] | 1844 | lp.cellX = lp.tmpCellX = mTargetCell[0]; |
| 1845 | lp.cellY = lp.tmpCellY = mTargetCell[1]; |
Adam Cohen | d41fbf5 | 2012-02-16 23:53:59 -0800 | [diff] [blame] | 1846 | lp.cellHSpan = item.spanX; |
| 1847 | lp.cellVSpan = item.spanY; |
Adam Cohen | 19f3792 | 2012-03-21 11:59:11 -0700 | [diff] [blame] | 1848 | lp.isLockedToGrid = true; |
Patrick Dubroy | 9438336 | 2010-10-29 15:03:24 -0700 | [diff] [blame] | 1849 | |
Winson Chung | 3d503fb | 2011-07-13 17:25:49 -0700 | [diff] [blame] | 1850 | if (container != LauncherSettings.Favorites.CONTAINER_HOTSEAT && |
| 1851 | cell instanceof LauncherAppWidgetHostView) { |
Adam Cohen | d4844c3 | 2011-02-18 19:25:06 -0800 | [diff] [blame] | 1852 | final CellLayout cellLayout = dropTargetLayout; |
| 1853 | // We post this call so that the widget has a chance to be placed |
| 1854 | // in its final location |
| 1855 | |
| 1856 | final LauncherAppWidgetHostView hostView = (LauncherAppWidgetHostView) cell; |
Sunny Goyal | 2434d40 | 2015-02-17 11:44:15 -0800 | [diff] [blame] | 1857 | AppWidgetProviderInfo pInfo = hostView.getAppWidgetInfo(); |
Adam Cohen | c9735cf | 2015-01-23 16:11:55 -0800 | [diff] [blame] | 1858 | if (pInfo != null && pInfo.resizeMode != AppWidgetProviderInfo.RESIZE_NONE |
| 1859 | && !d.accessibleDrag) { |
Sunny Goyal | 1797af4 | 2017-10-06 13:29:57 -0700 | [diff] [blame] | 1860 | onCompleteRunnable = new Runnable() { |
Adam Cohen | d4844c3 | 2011-02-18 19:25:06 -0800 | [diff] [blame] | 1861 | public void run() { |
Tony Wickham | ea62fe0 | 2016-10-05 09:05:42 -0700 | [diff] [blame] | 1862 | if (!isPageInTransition()) { |
Sunny Goyal | 3792096 | 2017-09-28 13:43:24 -0700 | [diff] [blame] | 1863 | AppWidgetResizeFrame.showForWidget(hostView, cellLayout); |
Adam Cohen | 26976d9 | 2011-03-22 15:33:33 -0700 | [diff] [blame] | 1864 | } |
| 1865 | } |
Tony Wickham | e038562 | 2015-09-17 09:24:07 -0700 | [diff] [blame] | 1866 | }; |
Adam Cohen | d4844c3 | 2011-02-18 19:25:06 -0800 | [diff] [blame] | 1867 | } |
| 1868 | } |
| 1869 | |
Sunny Goyal | 43bf11d | 2017-02-02 13:52:53 -0800 | [diff] [blame] | 1870 | mLauncher.getModelWriter().modifyItemInDatabase(info, container, screenId, |
| 1871 | lp.cellX, lp.cellY, item.spanX, item.spanY); |
Adam Cohen | d41fbf5 | 2012-02-16 23:53:59 -0800 | [diff] [blame] | 1872 | } else { |
Tony Wickham | 5dc7435 | 2016-12-06 16:24:43 -0800 | [diff] [blame] | 1873 | if (!returnToOriginalCellToPreventShuffling) { |
| 1874 | onNoCellFound(dropTargetLayout); |
| 1875 | } |
Jon Miranda | 9485e5f | 2016-10-18 11:47:42 -0700 | [diff] [blame] | 1876 | |
Adam Cohen | d41fbf5 | 2012-02-16 23:53:59 -0800 | [diff] [blame] | 1877 | // If we can't find a drop location, we return the item to its original position |
| 1878 | CellLayout.LayoutParams lp = (CellLayout.LayoutParams) cell.getLayoutParams(); |
| 1879 | mTargetCell[0] = lp.cellX; |
| 1880 | mTargetCell[1] = lp.cellY; |
Adam Cohen | f1dcdf6 | 2012-05-10 16:51:52 -0700 | [diff] [blame] | 1881 | CellLayout layout = (CellLayout) cell.getParent().getParent(); |
| 1882 | layout.markCellsAsOccupiedForView(cell); |
Patrick Dubroy | cd68ff5 | 2010-10-28 17:57:05 -0700 | [diff] [blame] | 1883 | } |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1884 | } |
Patrick Dubroy | ce34a97 | 2010-10-19 10:34:32 -0700 | [diff] [blame] | 1885 | |
Michael Jurka | 8c920dd | 2011-01-20 14:16:56 -0800 | [diff] [blame] | 1886 | final CellLayout parent = (CellLayout) cell.getParent().getParent(); |
Adam Cohen | fc53cd2 | 2011-07-20 15:45:11 -0700 | [diff] [blame] | 1887 | if (d.dragView.hasDrawn()) { |
Tony Wickham | ea62fe0 | 2016-10-05 09:05:42 -0700 | [diff] [blame] | 1888 | if (droppedOnOriginalCellDuringTransition) { |
| 1889 | // Animate the item to its original position, while simultaneously exiting |
| 1890 | // spring-loaded mode so the page meets the icon where it was picked up. |
| 1891 | mLauncher.getDragController().animateDragViewToOriginalPosition( |
Sunny Goyal | aeb1643 | 2017-10-16 11:46:41 -0700 | [diff] [blame] | 1892 | onCompleteRunnable, cell, SPRING_LOADED_TRANSITION_MS); |
Sunny Goyal | 3e3f44c | 2017-10-23 17:14:52 -0700 | [diff] [blame] | 1893 | mLauncher.getStateManager().goToState(NORMAL); |
Tony Wickham | ea62fe0 | 2016-10-05 09:05:42 -0700 | [diff] [blame] | 1894 | mLauncher.getDropTargetBar().onDragEnd(); |
| 1895 | parent.onDropChild(cell); |
| 1896 | return; |
| 1897 | } |
Adam Cohen | d41fbf5 | 2012-02-16 23:53:59 -0800 | [diff] [blame] | 1898 | final ItemInfo info = (ItemInfo) cell.getTag(); |
Adam Cohen | 5940042 | 2014-03-05 18:07:04 -0800 | [diff] [blame] | 1899 | boolean isWidget = info.itemType == LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET |
| 1900 | || info.itemType == LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET; |
| 1901 | if (isWidget) { |
Adam Cohen | d41fbf5 | 2012-02-16 23:53:59 -0800 | [diff] [blame] | 1902 | int animationType = resizeOnDrop ? ANIMATE_INTO_POSITION_AND_RESIZE : |
| 1903 | ANIMATE_INTO_POSITION_AND_DISAPPEAR; |
Sunny Goyal | a502aa3 | 2017-10-02 16:04:06 -0700 | [diff] [blame] | 1904 | animateWidgetDrop(info, parent, d.dragView, null, animationType, cell, false); |
Adam Cohen | d41fbf5 | 2012-02-16 23:53:59 -0800 | [diff] [blame] | 1905 | } else { |
Sunny Goyal | ce5a7e5 | 2015-07-08 15:44:27 -0700 | [diff] [blame] | 1906 | int duration = snapScreen < 0 ? -1 : ADJACENT_SCREEN_DROP_DURATION; |
Adam Cohen | 85b467b | 2012-02-29 15:38:46 -0800 | [diff] [blame] | 1907 | mLauncher.getDragLayer().animateViewIntoPosition(d.dragView, cell, duration, |
Sunny Goyal | 1797af4 | 2017-10-06 13:29:57 -0700 | [diff] [blame] | 1908 | this); |
Adam Cohen | d41fbf5 | 2012-02-16 23:53:59 -0800 | [diff] [blame] | 1909 | } |
Adam Cohen | fc53cd2 | 2011-07-20 15:45:11 -0700 | [diff] [blame] | 1910 | } else { |
Winson Chung | 7bd1bbb | 2012-02-13 18:29:29 -0800 | [diff] [blame] | 1911 | d.deferDragViewCleanupPostAnimation = false; |
Adam Cohen | fc53cd2 | 2011-07-20 15:45:11 -0700 | [diff] [blame] | 1912 | cell.setVisibility(VISIBLE); |
| 1913 | } |
Adam Cohen | 716b51e | 2011-06-30 12:09:54 -0700 | [diff] [blame] | 1914 | parent.onDropChild(cell); |
Sunny Goyal | 1797af4 | 2017-10-06 13:29:57 -0700 | [diff] [blame] | 1915 | |
Sunny Goyal | 3e3f44c | 2017-10-23 17:14:52 -0700 | [diff] [blame] | 1916 | mLauncher.getStateManager().goToState( |
| 1917 | NORMAL, SPRING_LOADED_EXIT_DELAY, onCompleteRunnable); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1918 | } |
Sunny Goyal | 1797af4 | 2017-10-06 13:29:57 -0700 | [diff] [blame] | 1919 | |
Tony Wickham | 0270039 | 2017-05-11 15:20:28 -0700 | [diff] [blame] | 1920 | if (d.stateAnnouncer != null && !droppedOnOriginalCell) { |
Sunny Goyal | 3f88640 | 2016-10-11 10:54:39 -0700 | [diff] [blame] | 1921 | d.stateAnnouncer.completeAction(R.string.item_moved); |
| 1922 | } |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1923 | } |
| 1924 | |
Jon Miranda | 9485e5f | 2016-10-18 11:47:42 -0700 | [diff] [blame] | 1925 | public void onNoCellFound(View dropTargetLayout) { |
Sunny Goyal | 36b5422 | 2018-07-10 13:50:50 -0700 | [diff] [blame] | 1926 | int strId = mLauncher.isHotseatLayout(dropTargetLayout) |
| 1927 | ? R.string.hotseat_out_of_space : R.string.out_of_space; |
Jon Miranda | 9485e5f | 2016-10-18 11:47:42 -0700 | [diff] [blame] | 1928 | Toast.makeText(mLauncher, mLauncher.getString(strId), Toast.LENGTH_SHORT).show(); |
| 1929 | } |
| 1930 | |
Sunny Goyal | 41d8417 | 2015-07-08 23:28:51 -0700 | [diff] [blame] | 1931 | /** |
| 1932 | * Computes the area relative to dragLayer which is used to display a page. |
| 1933 | */ |
| 1934 | public void getPageAreaRelativeToDragLayer(Rect outArea) { |
| 1935 | CellLayout child = (CellLayout) getChildAt(getNextPage()); |
| 1936 | if (child == null) { |
| 1937 | return; |
Adam Cohen | ed51cc9 | 2011-08-01 20:28:08 -0700 | [diff] [blame] | 1938 | } |
Sunny Goyal | 41d8417 | 2015-07-08 23:28:51 -0700 | [diff] [blame] | 1939 | ShortcutAndWidgetContainer boundingLayout = child.getShortcutsAndWidgets(); |
Adam Cohen | ed51cc9 | 2011-08-01 20:28:08 -0700 | [diff] [blame] | 1940 | |
Sunny Goyal | 41d8417 | 2015-07-08 23:28:51 -0700 | [diff] [blame] | 1941 | // Use the absolute left instead of the child left, as we want the visible area |
| 1942 | // irrespective of the visible child. Since the view can only scroll horizontally, the |
| 1943 | // top position is not affected. |
Sunny Goyal | 228153d | 2018-01-04 15:35:22 -0800 | [diff] [blame] | 1944 | mTempXY[0] = getPaddingLeft() + boundingLayout.getLeft(); |
Sunny Goyal | 41d8417 | 2015-07-08 23:28:51 -0700 | [diff] [blame] | 1945 | mTempXY[1] = child.getTop() + boundingLayout.getTop(); |
| 1946 | |
| 1947 | float scale = mLauncher.getDragLayer().getDescendantCoordRelativeToSelf(this, mTempXY); |
| 1948 | outArea.set(mTempXY[0], mTempXY[1], |
| 1949 | (int) (mTempXY[0] + scale * boundingLayout.getMeasuredWidth()), |
| 1950 | (int) (mTempXY[1] + scale * boundingLayout.getMeasuredHeight())); |
Adam Cohen | ed51cc9 | 2011-08-01 20:28:08 -0700 | [diff] [blame] | 1951 | } |
| 1952 | |
Sunny Goyal | 5837692 | 2015-05-26 18:56:52 -0700 | [diff] [blame] | 1953 | @Override |
Adam Cohen | cb3382b | 2011-05-24 14:07:08 -0700 | [diff] [blame] | 1954 | public void onDragEnter(DragObject d) { |
Sunny Goyal | 5837692 | 2015-05-26 18:56:52 -0700 | [diff] [blame] | 1955 | if (ENFORCE_DRAG_EVENT_ORDER) { |
Hyunyoung Song | 0de0117 | 2016-10-05 16:27:48 -0700 | [diff] [blame] | 1956 | enforceDragParity("onDragEnter", 1, 1); |
Sunny Goyal | 5837692 | 2015-05-26 18:56:52 -0700 | [diff] [blame] | 1957 | } |
| 1958 | |
Adam Cohen | c6cc61d | 2012-04-04 12:47:08 -0700 | [diff] [blame] | 1959 | mCreateUserFolderOnDrop = false; |
| 1960 | mAddToExistingFolderOnDrop = false; |
| 1961 | |
| 1962 | mDropToLayout = null; |
Sunny Goyal | aecb7cf | 2016-12-15 11:02:21 -0800 | [diff] [blame] | 1963 | mDragViewVisualCenter = d.getVisualCenter(mDragViewVisualCenter); |
| 1964 | setDropLayoutForDragObject(d, mDragViewVisualCenter[0], mDragViewVisualCenter[1]); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1965 | } |
| 1966 | |
Sunny Goyal | 5837692 | 2015-05-26 18:56:52 -0700 | [diff] [blame] | 1967 | @Override |
Adam Cohen | c6cc61d | 2012-04-04 12:47:08 -0700 | [diff] [blame] | 1968 | public void onDragExit(DragObject d) { |
Sunny Goyal | 5837692 | 2015-05-26 18:56:52 -0700 | [diff] [blame] | 1969 | if (ENFORCE_DRAG_EVENT_ORDER) { |
Hyunyoung Song | 0de0117 | 2016-10-05 16:27:48 -0700 | [diff] [blame] | 1970 | enforceDragParity("onDragExit", -1, 0); |
Sunny Goyal | 5837692 | 2015-05-26 18:56:52 -0700 | [diff] [blame] | 1971 | } |
Winson Chung | c07918d | 2011-07-01 15:35:26 -0700 | [diff] [blame] | 1972 | |
Adam Cohen | c6cc61d | 2012-04-04 12:47:08 -0700 | [diff] [blame] | 1973 | // Here we store the final page that will be dropped to, if the workspace in fact |
| 1974 | // receives the drop |
Sunny Goyal | f5440cb | 2016-12-14 15:13:00 -0800 | [diff] [blame] | 1975 | mDropToLayout = mDragTargetLayout; |
Adam Cohen | c6cc61d | 2012-04-04 12:47:08 -0700 | [diff] [blame] | 1976 | if (mDragMode == DRAG_MODE_CREATE_FOLDER) { |
| 1977 | mCreateUserFolderOnDrop = true; |
| 1978 | } else if (mDragMode == DRAG_MODE_ADD_TO_FOLDER) { |
| 1979 | mAddToExistingFolderOnDrop = true; |
Adam Cohen | 482ed82 | 2012-03-02 14:15:13 -0800 | [diff] [blame] | 1980 | } |
| 1981 | |
Sunny Goyal | f5440cb | 2016-12-14 15:13:00 -0800 | [diff] [blame] | 1982 | // Reset the previous drag target |
Adam Cohen | c6cc61d | 2012-04-04 12:47:08 -0700 | [diff] [blame] | 1983 | setCurrentDropLayout(null); |
| 1984 | setCurrentDragOverlappingLayout(null); |
Winson Chung | c07918d | 2011-07-01 15:35:26 -0700 | [diff] [blame] | 1985 | |
Adam Cohen | 74c28d1 | 2011-11-18 14:17:11 -0800 | [diff] [blame] | 1986 | mSpringLoadedDragController.cancel(); |
Winson Chung | c07918d | 2011-07-01 15:35:26 -0700 | [diff] [blame] | 1987 | } |
| 1988 | |
Hyunyoung Song | 0de0117 | 2016-10-05 16:27:48 -0700 | [diff] [blame] | 1989 | private void enforceDragParity(String event, int update, int expectedValue) { |
| 1990 | enforceDragParity(this, event, update, expectedValue); |
Sunny Goyal | 5837692 | 2015-05-26 18:56:52 -0700 | [diff] [blame] | 1991 | for (int i = 0; i < getChildCount(); i++) { |
Hyunyoung Song | 0de0117 | 2016-10-05 16:27:48 -0700 | [diff] [blame] | 1992 | enforceDragParity(getChildAt(i), event, update, expectedValue); |
Sunny Goyal | 5837692 | 2015-05-26 18:56:52 -0700 | [diff] [blame] | 1993 | } |
| 1994 | } |
| 1995 | |
Hyunyoung Song | 0de0117 | 2016-10-05 16:27:48 -0700 | [diff] [blame] | 1996 | private void enforceDragParity(View v, String event, int update, int expectedValue) { |
Sunny Goyal | 5837692 | 2015-05-26 18:56:52 -0700 | [diff] [blame] | 1997 | Object tag = v.getTag(R.id.drag_event_parity); |
| 1998 | int value = tag == null ? 0 : (Integer) tag; |
| 1999 | value += update; |
| 2000 | v.setTag(R.id.drag_event_parity, value); |
| 2001 | |
| 2002 | if (value != expectedValue) { |
| 2003 | Log.e(TAG, event + ": Drag contract violated: " + value); |
| 2004 | } |
| 2005 | } |
| 2006 | |
Adam Cohen | c6cc61d | 2012-04-04 12:47:08 -0700 | [diff] [blame] | 2007 | void setCurrentDropLayout(CellLayout layout) { |
| 2008 | if (mDragTargetLayout != null) { |
| 2009 | mDragTargetLayout.revertTempState(); |
| 2010 | mDragTargetLayout.onDragExit(); |
| 2011 | } |
| 2012 | mDragTargetLayout = layout; |
| 2013 | if (mDragTargetLayout != null) { |
| 2014 | mDragTargetLayout.onDragEnter(); |
| 2015 | } |
| 2016 | cleanupReorder(true); |
| 2017 | cleanupFolderCreation(); |
| 2018 | setCurrentDropOverCell(-1, -1); |
| 2019 | } |
| 2020 | |
| 2021 | void setCurrentDragOverlappingLayout(CellLayout layout) { |
| 2022 | if (mDragOverlappingLayout != null) { |
| 2023 | mDragOverlappingLayout.setIsDragOverlapping(false); |
| 2024 | } |
| 2025 | mDragOverlappingLayout = layout; |
| 2026 | if (mDragOverlappingLayout != null) { |
| 2027 | mDragOverlappingLayout.setIsDragOverlapping(true); |
| 2028 | } |
Tony Wickham | 95cdb3a | 2016-02-18 14:37:07 -0800 | [diff] [blame] | 2029 | // Invalidating the scrim will also force this CellLayout |
| 2030 | // to be invalidated so that it is highlighted if necessary. |
Sunny Goyal | 5d1873a | 2018-05-08 11:10:44 -0700 | [diff] [blame] | 2031 | mLauncher.getDragLayer().getScrim().invalidate(); |
Tony Wickham | 95cdb3a | 2016-02-18 14:37:07 -0800 | [diff] [blame] | 2032 | } |
| 2033 | |
| 2034 | public CellLayout getCurrentDragOverlappingLayout() { |
| 2035 | return mDragOverlappingLayout; |
Adam Cohen | c6cc61d | 2012-04-04 12:47:08 -0700 | [diff] [blame] | 2036 | } |
| 2037 | |
| 2038 | void setCurrentDropOverCell(int x, int y) { |
| 2039 | if (x != mDragOverX || y != mDragOverY) { |
| 2040 | mDragOverX = x; |
| 2041 | mDragOverY = y; |
| 2042 | setDragMode(DRAG_MODE_NONE); |
| 2043 | } |
| 2044 | } |
| 2045 | |
| 2046 | void setDragMode(int dragMode) { |
| 2047 | if (dragMode != mDragMode) { |
| 2048 | if (dragMode == DRAG_MODE_NONE) { |
| 2049 | cleanupAddToFolder(); |
| 2050 | // We don't want to cancel the re-order alarm every time the target cell changes |
| 2051 | // as this feels to slow / unresponsive. |
| 2052 | cleanupReorder(false); |
| 2053 | cleanupFolderCreation(); |
| 2054 | } else if (dragMode == DRAG_MODE_ADD_TO_FOLDER) { |
| 2055 | cleanupReorder(true); |
| 2056 | cleanupFolderCreation(); |
| 2057 | } else if (dragMode == DRAG_MODE_CREATE_FOLDER) { |
| 2058 | cleanupAddToFolder(); |
| 2059 | cleanupReorder(true); |
| 2060 | } else if (dragMode == DRAG_MODE_REORDER) { |
| 2061 | cleanupAddToFolder(); |
| 2062 | cleanupFolderCreation(); |
| 2063 | } |
| 2064 | mDragMode = dragMode; |
| 2065 | } |
| 2066 | } |
| 2067 | |
| 2068 | private void cleanupFolderCreation() { |
Adam Cohen | f172b74 | 2016-03-30 19:28:34 -0700 | [diff] [blame] | 2069 | if (mFolderCreateBg != null) { |
| 2070 | mFolderCreateBg.animateToRest(); |
| 2071 | } |
Adam Cohen | 8ec05f9 | 2013-10-13 12:29:03 -0700 | [diff] [blame] | 2072 | mFolderCreationAlarm.setOnAlarmListener(null); |
Adam Cohen | c6cc61d | 2012-04-04 12:47:08 -0700 | [diff] [blame] | 2073 | mFolderCreationAlarm.cancelAlarm(); |
| 2074 | } |
| 2075 | |
| 2076 | private void cleanupAddToFolder() { |
| 2077 | if (mDragOverFolderIcon != null) { |
Sunny Goyal | e393d3a | 2016-09-09 12:42:10 -0700 | [diff] [blame] | 2078 | mDragOverFolderIcon.onDragExit(); |
Adam Cohen | c6cc61d | 2012-04-04 12:47:08 -0700 | [diff] [blame] | 2079 | mDragOverFolderIcon = null; |
| 2080 | } |
| 2081 | } |
| 2082 | |
| 2083 | private void cleanupReorder(boolean cancelAlarm) { |
| 2084 | // Any pending reorders are canceled |
| 2085 | if (cancelAlarm) { |
| 2086 | mReorderAlarm.cancelAlarm(); |
| 2087 | } |
| 2088 | mLastReorderX = -1; |
| 2089 | mLastReorderY = -1; |
Winson Chung | c07918d | 2011-07-01 15:35:26 -0700 | [diff] [blame] | 2090 | } |
| 2091 | |
Michael Jurka | 4516c11 | 2010-10-07 15:13:47 -0700 | [diff] [blame] | 2092 | /* |
| 2093 | * |
| 2094 | * Convert the 2D coordinate xy from the parent View's coordinate space to this CellLayout's |
| 2095 | * coordinate space. The argument xy is modified with the return result. |
Michael Jurka | 4516c11 | 2010-10-07 15:13:47 -0700 | [diff] [blame] | 2096 | */ |
Sunny Goyal | 876e462 | 2018-11-02 13:50:40 -0700 | [diff] [blame] | 2097 | private void mapPointFromSelfToChild(View v, float[] xy) { |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 2098 | xy[0] = xy[0] - v.getLeft(); |
| 2099 | xy[1] = xy[1] - v.getTop(); |
Michael Jurka | 4516c11 | 2010-10-07 15:13:47 -0700 | [diff] [blame] | 2100 | } |
| 2101 | |
Sunny Goyal | cf25b52 | 2015-07-09 00:01:18 -0700 | [diff] [blame] | 2102 | boolean isPointInSelfOverHotseat(int x, int y) { |
| 2103 | mTempXY[0] = x; |
| 2104 | mTempXY[1] = y; |
| 2105 | mLauncher.getDragLayer().getDescendantCoordRelativeToSelf(this, mTempXY, true); |
Sunny Goyal | 6c2975e | 2016-07-06 09:47:56 -0700 | [diff] [blame] | 2106 | View hotseat = mLauncher.getHotseat(); |
| 2107 | return mTempXY[0] >= hotseat.getLeft() && |
| 2108 | mTempXY[0] <= hotseat.getRight() && |
| 2109 | mTempXY[1] >= hotseat.getTop() && |
| 2110 | mTempXY[1] <= hotseat.getBottom(); |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 2111 | } |
Winson Chung | 3d503fb | 2011-07-13 17:25:49 -0700 | [diff] [blame] | 2112 | |
Sunny Goyal | 876e462 | 2018-11-02 13:50:40 -0700 | [diff] [blame] | 2113 | /** |
| 2114 | * Updates the point in {@param xy} to point to the co-ordinate space of {@param layout} |
| 2115 | * @param layout either hotseat of a page in workspace |
| 2116 | * @param xy the point location in workspace co-ordinate space |
| 2117 | */ |
| 2118 | private void mapPointFromDropLayout(CellLayout layout, float[] xy) { |
| 2119 | if (mLauncher.isHotseatLayout(layout)) { |
| 2120 | mTempXY[0] = (int) xy[0]; |
| 2121 | mTempXY[1] = (int) xy[1]; |
| 2122 | mLauncher.getDragLayer().getDescendantCoordRelativeToSelf(this, mTempXY, true); |
| 2123 | mLauncher.getDragLayer().mapCoordInSelfToDescendant(layout, mTempXY); |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 2124 | |
Sunny Goyal | 876e462 | 2018-11-02 13:50:40 -0700 | [diff] [blame] | 2125 | xy[0] = mTempXY[0]; |
| 2126 | xy[1] = mTempXY[1]; |
| 2127 | } else { |
| 2128 | mapPointFromSelfToChild(layout, xy); |
| 2129 | } |
Adam Cohen | c6cc61d | 2012-04-04 12:47:08 -0700 | [diff] [blame] | 2130 | } |
| 2131 | |
Winson Chung | ea359c6 | 2011-08-03 17:06:35 -0700 | [diff] [blame] | 2132 | private boolean isDragWidget(DragObject d) { |
| 2133 | return (d.dragInfo instanceof LauncherAppWidgetInfo || |
| 2134 | d.dragInfo instanceof PendingAddWidgetInfo); |
| 2135 | } |
Winson Chung | ea359c6 | 2011-08-03 17:06:35 -0700 | [diff] [blame] | 2136 | |
Adam Cohen | cb3382b | 2011-05-24 14:07:08 -0700 | [diff] [blame] | 2137 | public void onDragOver(DragObject d) { |
Winson Chung | c07918d | 2011-07-01 15:35:26 -0700 | [diff] [blame] | 2138 | // Skip drag over events while we are dragging over side pages |
Sunny Goyal | f5440cb | 2016-12-14 15:13:00 -0800 | [diff] [blame] | 2139 | if (!transitionStateShouldAllowDrop()) return; |
Winson Chung | c07918d | 2011-07-01 15:35:26 -0700 | [diff] [blame] | 2140 | |
Sunny Goyal | aa8ef11 | 2015-06-12 20:04:41 -0700 | [diff] [blame] | 2141 | ItemInfo item = d.dragInfo; |
Sunny Goyal | 25611b1 | 2014-07-22 09:52:37 -0700 | [diff] [blame] | 2142 | if (item == null) { |
Sunny Goyal | 3d706ad | 2017-03-06 16:56:39 -0800 | [diff] [blame] | 2143 | if (FeatureFlags.IS_DOGFOOD_BUILD) { |
Sunny Goyal | 25611b1 | 2014-07-22 09:52:37 -0700 | [diff] [blame] | 2144 | throw new NullPointerException("DragObject has null info"); |
| 2145 | } |
| 2146 | return; |
| 2147 | } |
Winson Chung | c07918d | 2011-07-01 15:35:26 -0700 | [diff] [blame] | 2148 | |
| 2149 | // Ensure that we have proper spans for the item that we are dropping |
| 2150 | if (item.spanX < 0 || item.spanY < 0) throw new RuntimeException("Improper spans found"); |
Sunny Goyal | 1587d53 | 2015-01-29 09:57:16 -0800 | [diff] [blame] | 2151 | mDragViewVisualCenter = d.getVisualCenter(mDragViewVisualCenter); |
Adam Cohen | 0061875 | 2011-07-20 12:06:04 -0700 | [diff] [blame] | 2152 | |
Adam Cohen | 482ed82 | 2012-03-02 14:15:13 -0800 | [diff] [blame] | 2153 | final View child = (mDragInfo == null) ? null : mDragInfo.cell; |
Sunny Goyal | aecb7cf | 2016-12-15 11:02:21 -0800 | [diff] [blame] | 2154 | if (setDropLayoutForDragObject(d, mDragViewVisualCenter[0], mDragViewVisualCenter[1])) { |
| 2155 | if (mLauncher.isHotseatLayout(mDragTargetLayout)) { |
| 2156 | mSpringLoadedDragController.cancel(); |
| 2157 | } else { |
| 2158 | mSpringLoadedDragController.setAlarm(mDragTargetLayout); |
Winson Chung | 4afe9b3 | 2011-07-27 17:46:20 -0700 | [diff] [blame] | 2159 | } |
Winson Chung | c07918d | 2011-07-01 15:35:26 -0700 | [diff] [blame] | 2160 | } |
| 2161 | |
| 2162 | // Handle the drag over |
| 2163 | if (mDragTargetLayout != null) { |
Winson Chung | c07918d | 2011-07-01 15:35:26 -0700 | [diff] [blame] | 2164 | // We want the point to be mapped to the dragTarget. |
Sunny Goyal | 024659c | 2018-11-08 11:10:20 -0800 | [diff] [blame] | 2165 | mapPointFromDropLayout(mDragTargetLayout, mDragViewVisualCenter); |
Adam Cohen | c6cc61d | 2012-04-04 12:47:08 -0700 | [diff] [blame] | 2166 | |
Adam Cohen | 74c5491 | 2013-09-29 14:48:04 -0700 | [diff] [blame] | 2167 | int minSpanX = item.spanX; |
| 2168 | int minSpanY = item.spanY; |
| 2169 | if (item.minSpanX > 0 && item.minSpanY > 0) { |
| 2170 | minSpanX = item.minSpanX; |
| 2171 | minSpanY = item.minSpanY; |
| 2172 | } |
| 2173 | |
Winson Chung | c07918d | 2011-07-01 15:35:26 -0700 | [diff] [blame] | 2174 | mTargetCell = findNearestArea((int) mDragViewVisualCenter[0], |
Adam Cohen | 74c5491 | 2013-09-29 14:48:04 -0700 | [diff] [blame] | 2175 | (int) mDragViewVisualCenter[1], minSpanX, minSpanY, |
Adam Cohen | d024f98 | 2012-05-23 18:26:45 -0700 | [diff] [blame] | 2176 | mDragTargetLayout, mTargetCell); |
Adam Cohen | 74c5491 | 2013-09-29 14:48:04 -0700 | [diff] [blame] | 2177 | int reorderX = mTargetCell[0]; |
| 2178 | int reorderY = mTargetCell[1]; |
Adam Cohen | c6cc61d | 2012-04-04 12:47:08 -0700 | [diff] [blame] | 2179 | |
| 2180 | setCurrentDropOverCell(mTargetCell[0], mTargetCell[1]); |
| 2181 | |
Adam Cohen | 482ed82 | 2012-03-02 14:15:13 -0800 | [diff] [blame] | 2182 | float targetCellDistance = mDragTargetLayout.getDistanceFromCell( |
| 2183 | mDragViewVisualCenter[0], mDragViewVisualCenter[1], mTargetCell); |
| 2184 | |
Sunny Goyal | e78e3d7 | 2015-09-24 11:23:31 -0700 | [diff] [blame] | 2185 | manageFolderFeedback(mDragTargetLayout, mTargetCell, targetCellDistance, d); |
Adam Cohen | 482ed82 | 2012-03-02 14:15:13 -0800 | [diff] [blame] | 2186 | |
Adam Cohen | c6cc61d | 2012-04-04 12:47:08 -0700 | [diff] [blame] | 2187 | boolean nearestDropOccupied = mDragTargetLayout.isNearestDropLocationOccupied((int) |
| 2188 | mDragViewVisualCenter[0], (int) mDragViewVisualCenter[1], item.spanX, |
| 2189 | item.spanY, child, mTargetCell); |
| 2190 | |
| 2191 | if (!nearestDropOccupied) { |
Sunny Goyal | 06e21a2 | 2016-08-11 16:02:02 -0700 | [diff] [blame] | 2192 | mDragTargetLayout.visualizeDropLocation(child, mOutlineProvider, |
Sunny Goyal | e78e3d7 | 2015-09-24 11:23:31 -0700 | [diff] [blame] | 2193 | mTargetCell[0], mTargetCell[1], item.spanX, item.spanY, false, d); |
Adam Cohen | c6cc61d | 2012-04-04 12:47:08 -0700 | [diff] [blame] | 2194 | } else if ((mDragMode == DRAG_MODE_NONE || mDragMode == DRAG_MODE_REORDER) |
Adam Cohen | 74c5491 | 2013-09-29 14:48:04 -0700 | [diff] [blame] | 2195 | && !mReorderAlarm.alarmPending() && (mLastReorderX != reorderX || |
| 2196 | mLastReorderY != reorderY)) { |
Adam Cohen | d024f98 | 2012-05-23 18:26:45 -0700 | [diff] [blame] | 2197 | |
Adam Cohen | fa3c58f | 2013-12-06 16:10:55 -0800 | [diff] [blame] | 2198 | int[] resultSpan = new int[2]; |
| 2199 | mDragTargetLayout.performReorder((int) mDragViewVisualCenter[0], |
| 2200 | (int) mDragViewVisualCenter[1], minSpanX, minSpanY, item.spanX, item.spanY, |
| 2201 | child, mTargetCell, resultSpan, CellLayout.MODE_SHOW_REORDER_HINT); |
| 2202 | |
Adam Cohen | 19f3792 | 2012-03-21 11:59:11 -0700 | [diff] [blame] | 2203 | // Otherwise, if we aren't adding to or creating a folder and there's no pending |
| 2204 | // reorder, then we schedule a reorder |
Adam Cohen | 482ed82 | 2012-03-02 14:15:13 -0800 | [diff] [blame] | 2205 | ReorderAlarmListener listener = new ReorderAlarmListener(mDragViewVisualCenter, |
Sunny Goyal | e78e3d7 | 2015-09-24 11:23:31 -0700 | [diff] [blame] | 2206 | minSpanX, minSpanY, item.spanX, item.spanY, d, child); |
Adam Cohen | 482ed82 | 2012-03-02 14:15:13 -0800 | [diff] [blame] | 2207 | mReorderAlarm.setOnAlarmListener(listener); |
| 2208 | mReorderAlarm.setAlarm(REORDER_TIMEOUT); |
Adam Cohen | c6cc61d | 2012-04-04 12:47:08 -0700 | [diff] [blame] | 2209 | } |
| 2210 | |
| 2211 | if (mDragMode == DRAG_MODE_CREATE_FOLDER || mDragMode == DRAG_MODE_ADD_TO_FOLDER || |
| 2212 | !nearestDropOccupied) { |
| 2213 | if (mDragTargetLayout != null) { |
| 2214 | mDragTargetLayout.revertTempState(); |
Michael Jurka | d3ef306 | 2010-11-23 16:23:58 -0800 | [diff] [blame] | 2215 | } |
| 2216 | } |
Adam Cohen | 482ed82 | 2012-03-02 14:15:13 -0800 | [diff] [blame] | 2217 | } |
| 2218 | } |
| 2219 | |
Tony Wickham | dbf08b4 | 2016-09-12 18:09:33 -0700 | [diff] [blame] | 2220 | /** |
| 2221 | * Updates {@link #mDragTargetLayout} and {@link #mDragOverlappingLayout} |
| 2222 | * based on the DragObject's position. |
| 2223 | * |
| 2224 | * The layout will be: |
| 2225 | * - The Hotseat if the drag object is over it |
| 2226 | * - A side page if we are in spring-loaded mode and the drag object is over it |
| 2227 | * - The current page otherwise |
| 2228 | * |
| 2229 | * @return whether the layout is different from the current {@link #mDragTargetLayout}. |
| 2230 | */ |
Sunny Goyal | aecb7cf | 2016-12-15 11:02:21 -0800 | [diff] [blame] | 2231 | private boolean setDropLayoutForDragObject(DragObject d, float centerX, float centerY) { |
Tony Wickham | dbf08b4 | 2016-09-12 18:09:33 -0700 | [diff] [blame] | 2232 | CellLayout layout = null; |
| 2233 | // Test to see if we are over the hotseat first |
| 2234 | if (mLauncher.getHotseat() != null && !isDragWidget(d)) { |
| 2235 | if (isPointInSelfOverHotseat(d.x, d.y)) { |
Sunny Goyal | 876e462 | 2018-11-02 13:50:40 -0700 | [diff] [blame] | 2236 | layout = mLauncher.getHotseat(); |
Tony Wickham | dbf08b4 | 2016-09-12 18:09:33 -0700 | [diff] [blame] | 2237 | } |
| 2238 | } |
Sunny Goyal | aecb7cf | 2016-12-15 11:02:21 -0800 | [diff] [blame] | 2239 | |
| 2240 | int nextPage = getNextPage(); |
| 2241 | if (layout == null && !isPageInTransition()) { |
| 2242 | // Check if the item is dragged over left page |
| 2243 | mTempTouchCoordinates[0] = Math.min(centerX, d.x); |
| 2244 | mTempTouchCoordinates[1] = d.y; |
| 2245 | layout = verifyInsidePage(nextPage + (mIsRtl ? 1 : -1), mTempTouchCoordinates); |
| 2246 | } |
| 2247 | |
| 2248 | if (layout == null && !isPageInTransition()) { |
| 2249 | // Check if the item is dragged over right page |
| 2250 | mTempTouchCoordinates[0] = Math.max(centerX, d.x); |
| 2251 | mTempTouchCoordinates[1] = d.y; |
| 2252 | layout = verifyInsidePage(nextPage + (mIsRtl ? -1 : 1), mTempTouchCoordinates); |
| 2253 | } |
| 2254 | |
| 2255 | // Always pick the current page. |
Sunny Goyal | 7ce471b | 2017-08-02 03:37:39 -0700 | [diff] [blame] | 2256 | if (layout == null && nextPage >= 0 && nextPage < getPageCount()) { |
Sunny Goyal | aecb7cf | 2016-12-15 11:02:21 -0800 | [diff] [blame] | 2257 | layout = (CellLayout) getChildAt(nextPage); |
Tony Wickham | dbf08b4 | 2016-09-12 18:09:33 -0700 | [diff] [blame] | 2258 | } |
| 2259 | if (layout != mDragTargetLayout) { |
| 2260 | setCurrentDropLayout(layout); |
| 2261 | setCurrentDragOverlappingLayout(layout); |
| 2262 | return true; |
| 2263 | } |
| 2264 | return false; |
| 2265 | } |
| 2266 | |
Sunny Goyal | aecb7cf | 2016-12-15 11:02:21 -0800 | [diff] [blame] | 2267 | /** |
| 2268 | * Returns the child CellLayout if the point is inside the page coordinates, null otherwise. |
| 2269 | */ |
| 2270 | private CellLayout verifyInsidePage(int pageNo, float[] touchXy) { |
Sunny Goyal | 7ce471b | 2017-08-02 03:37:39 -0700 | [diff] [blame] | 2271 | if (pageNo >= 0 && pageNo < getPageCount()) { |
Sunny Goyal | aecb7cf | 2016-12-15 11:02:21 -0800 | [diff] [blame] | 2272 | CellLayout cl = (CellLayout) getChildAt(pageNo); |
| 2273 | mapPointFromSelfToChild(cl, touchXy); |
| 2274 | if (touchXy[0] >= 0 && touchXy[0] <= cl.getWidth() && |
| 2275 | touchXy[1] >= 0 && touchXy[1] <= cl.getHeight()) { |
| 2276 | // This point is inside the cell layout |
| 2277 | return cl; |
| 2278 | } |
| 2279 | } |
| 2280 | return null; |
| 2281 | } |
| 2282 | |
Sunny Goyal | e78e3d7 | 2015-09-24 11:23:31 -0700 | [diff] [blame] | 2283 | private void manageFolderFeedback(CellLayout targetLayout, |
| 2284 | int[] targetCell, float distance, DragObject dragObject) { |
Jon Miranda | 36f6359 | 2018-06-12 15:05:12 -0700 | [diff] [blame] | 2285 | if (distance > mMaxDistanceForFolderCreation) { |
| 2286 | if (mDragMode != DRAG_MODE_NONE) { |
| 2287 | setDragMode(DRAG_MODE_NONE); |
| 2288 | } |
| 2289 | return; |
| 2290 | } |
Sunny Goyal | e78e3d7 | 2015-09-24 11:23:31 -0700 | [diff] [blame] | 2291 | |
| 2292 | final View dragOverView = mDragTargetLayout.getChildAt(mTargetCell[0], mTargetCell[1]); |
Sunny Goyal | 9d43808 | 2015-09-25 11:50:16 -0700 | [diff] [blame] | 2293 | ItemInfo info = dragObject.dragInfo; |
Sunny Goyal | e78e3d7 | 2015-09-24 11:23:31 -0700 | [diff] [blame] | 2294 | boolean userFolderPending = willCreateUserFolder(info, dragOverView, false); |
Adam Cohen | c6cc61d | 2012-04-04 12:47:08 -0700 | [diff] [blame] | 2295 | if (mDragMode == DRAG_MODE_NONE && userFolderPending && |
| 2296 | !mFolderCreationAlarm.alarmPending()) { |
Adam Cohen | c9735cf | 2015-01-23 16:11:55 -0800 | [diff] [blame] | 2297 | |
| 2298 | FolderCreationAlarmListener listener = new |
| 2299 | FolderCreationAlarmListener(targetLayout, targetCell[0], targetCell[1]); |
| 2300 | |
Sunny Goyal | e78e3d7 | 2015-09-24 11:23:31 -0700 | [diff] [blame] | 2301 | if (!dragObject.accessibleDrag) { |
Adam Cohen | c9735cf | 2015-01-23 16:11:55 -0800 | [diff] [blame] | 2302 | mFolderCreationAlarm.setOnAlarmListener(listener); |
| 2303 | mFolderCreationAlarm.setAlarm(FOLDER_CREATION_TIMEOUT); |
| 2304 | } else { |
| 2305 | listener.onAlarm(mFolderCreationAlarm); |
| 2306 | } |
Sunny Goyal | e78e3d7 | 2015-09-24 11:23:31 -0700 | [diff] [blame] | 2307 | |
| 2308 | if (dragObject.stateAnnouncer != null) { |
| 2309 | dragObject.stateAnnouncer.announce(WorkspaceAccessibilityHelper |
| 2310 | .getDescriptionForDropOver(dragOverView, getContext())); |
| 2311 | } |
Adam Cohen | c6cc61d | 2012-04-04 12:47:08 -0700 | [diff] [blame] | 2312 | return; |
Adam Cohen | 482ed82 | 2012-03-02 14:15:13 -0800 | [diff] [blame] | 2313 | } |
| 2314 | |
Sunny Goyal | e78e3d7 | 2015-09-24 11:23:31 -0700 | [diff] [blame] | 2315 | boolean willAddToFolder = willAddToExistingUserFolder(info, dragOverView); |
Adam Cohen | 482ed82 | 2012-03-02 14:15:13 -0800 | [diff] [blame] | 2316 | if (willAddToFolder && mDragMode == DRAG_MODE_NONE) { |
Adam Cohen | c6cc61d | 2012-04-04 12:47:08 -0700 | [diff] [blame] | 2317 | mDragOverFolderIcon = ((FolderIcon) dragOverView); |
Adam Cohen | c6cc61d | 2012-04-04 12:47:08 -0700 | [diff] [blame] | 2318 | mDragOverFolderIcon.onDragEnter(info); |
Adam Cohen | 482ed82 | 2012-03-02 14:15:13 -0800 | [diff] [blame] | 2319 | if (targetLayout != null) { |
| 2320 | targetLayout.clearDragOutlines(); |
Winson Chung | c07918d | 2011-07-01 15:35:26 -0700 | [diff] [blame] | 2321 | } |
Adam Cohen | c6cc61d | 2012-04-04 12:47:08 -0700 | [diff] [blame] | 2322 | setDragMode(DRAG_MODE_ADD_TO_FOLDER); |
Sunny Goyal | e78e3d7 | 2015-09-24 11:23:31 -0700 | [diff] [blame] | 2323 | |
| 2324 | if (dragObject.stateAnnouncer != null) { |
| 2325 | dragObject.stateAnnouncer.announce(WorkspaceAccessibilityHelper |
| 2326 | .getDescriptionForDropOver(dragOverView, getContext())); |
| 2327 | } |
Adam Cohen | c6cc61d | 2012-04-04 12:47:08 -0700 | [diff] [blame] | 2328 | return; |
Patrick Dubroy | 976ebec | 2010-08-04 20:03:37 -0700 | [diff] [blame] | 2329 | } |
Adam Cohen | 482ed82 | 2012-03-02 14:15:13 -0800 | [diff] [blame] | 2330 | |
Adam Cohen | c6cc61d | 2012-04-04 12:47:08 -0700 | [diff] [blame] | 2331 | if (mDragMode == DRAG_MODE_ADD_TO_FOLDER && !willAddToFolder) { |
| 2332 | setDragMode(DRAG_MODE_NONE); |
| 2333 | } |
| 2334 | if (mDragMode == DRAG_MODE_CREATE_FOLDER && !userFolderPending) { |
| 2335 | setDragMode(DRAG_MODE_NONE); |
Adam Cohen | 482ed82 | 2012-03-02 14:15:13 -0800 | [diff] [blame] | 2336 | } |
Adam Cohen | c0dcf59 | 2011-06-01 15:30:43 -0700 | [diff] [blame] | 2337 | } |
| 2338 | |
Adam Cohen | 19072da | 2011-05-31 14:30:45 -0700 | [diff] [blame] | 2339 | class FolderCreationAlarmListener implements OnAlarmListener { |
Rajeev Kumar | 93c0512 | 2017-06-12 12:59:47 -0700 | [diff] [blame] | 2340 | final CellLayout layout; |
| 2341 | final int cellX; |
| 2342 | final int cellY; |
Adam Cohen | 19072da | 2011-05-31 14:30:45 -0700 | [diff] [blame] | 2343 | |
Jon Miranda | a0233f7 | 2017-06-22 18:34:45 -0700 | [diff] [blame] | 2344 | final PreviewBackground bg = new PreviewBackground(); |
Adam Cohen | f172b74 | 2016-03-30 19:28:34 -0700 | [diff] [blame] | 2345 | |
Adam Cohen | 69ce2e5 | 2011-07-03 19:25:21 -0700 | [diff] [blame] | 2346 | public FolderCreationAlarmListener(CellLayout layout, int cellX, int cellY) { |
| 2347 | this.layout = layout; |
| 2348 | this.cellX = cellX; |
| 2349 | this.cellY = cellY; |
Adam Cohen | efca027 | 2016-02-24 19:19:06 -0800 | [diff] [blame] | 2350 | |
Adam Cohen | efca027 | 2016-02-24 19:19:06 -0800 | [diff] [blame] | 2351 | BubbleTextView cell = (BubbleTextView) layout.getChildAt(cellX, cellY); |
Sunny Goyal | ab770a1 | 2018-11-14 15:17:26 -0800 | [diff] [blame] | 2352 | bg.setup(mLauncher, mLauncher, null, cell.getMeasuredWidth(), cell.getPaddingTop()); |
Adam Cohen | f172b74 | 2016-03-30 19:28:34 -0700 | [diff] [blame] | 2353 | |
| 2354 | // The full preview background should appear behind the icon |
| 2355 | bg.isClipping = false; |
Adam Cohen | 19072da | 2011-05-31 14:30:45 -0700 | [diff] [blame] | 2356 | } |
| 2357 | |
| 2358 | public void onAlarm(Alarm alarm) { |
Adam Cohen | f172b74 | 2016-03-30 19:28:34 -0700 | [diff] [blame] | 2359 | mFolderCreateBg = bg; |
Adam Cohen | efca027 | 2016-02-24 19:19:06 -0800 | [diff] [blame] | 2360 | mFolderCreateBg.animateToAccept(layout, cellX, cellY); |
Adam Cohen | 69ce2e5 | 2011-07-03 19:25:21 -0700 | [diff] [blame] | 2361 | layout.clearDragOutlines(); |
Adam Cohen | c6cc61d | 2012-04-04 12:47:08 -0700 | [diff] [blame] | 2362 | setDragMode(DRAG_MODE_CREATE_FOLDER); |
Adam Cohen | 482ed82 | 2012-03-02 14:15:13 -0800 | [diff] [blame] | 2363 | } |
| 2364 | } |
| 2365 | |
| 2366 | class ReorderAlarmListener implements OnAlarmListener { |
Rajeev Kumar | 93c0512 | 2017-06-12 12:59:47 -0700 | [diff] [blame] | 2367 | final float[] dragViewCenter; |
| 2368 | final int minSpanX, minSpanY, spanX, spanY; |
| 2369 | final DragObject dragObject; |
| 2370 | final View child; |
Adam Cohen | 482ed82 | 2012-03-02 14:15:13 -0800 | [diff] [blame] | 2371 | |
| 2372 | public ReorderAlarmListener(float[] dragViewCenter, int minSpanX, int minSpanY, int spanX, |
Sunny Goyal | e78e3d7 | 2015-09-24 11:23:31 -0700 | [diff] [blame] | 2373 | int spanY, DragObject dragObject, View child) { |
Adam Cohen | 482ed82 | 2012-03-02 14:15:13 -0800 | [diff] [blame] | 2374 | this.dragViewCenter = dragViewCenter; |
| 2375 | this.minSpanX = minSpanX; |
| 2376 | this.minSpanY = minSpanY; |
| 2377 | this.spanX = spanX; |
| 2378 | this.spanY = spanY; |
| 2379 | this.child = child; |
Sunny Goyal | e78e3d7 | 2015-09-24 11:23:31 -0700 | [diff] [blame] | 2380 | this.dragObject = dragObject; |
Adam Cohen | 482ed82 | 2012-03-02 14:15:13 -0800 | [diff] [blame] | 2381 | } |
| 2382 | |
| 2383 | public void onAlarm(Alarm alarm) { |
| 2384 | int[] resultSpan = new int[2]; |
Adam Cohen | 19f3792 | 2012-03-21 11:59:11 -0700 | [diff] [blame] | 2385 | mTargetCell = findNearestArea((int) mDragViewVisualCenter[0], |
Adam Cohen | 74c5491 | 2013-09-29 14:48:04 -0700 | [diff] [blame] | 2386 | (int) mDragViewVisualCenter[1], minSpanX, minSpanY, mDragTargetLayout, |
| 2387 | mTargetCell); |
Adam Cohen | 19f3792 | 2012-03-21 11:59:11 -0700 | [diff] [blame] | 2388 | mLastReorderX = mTargetCell[0]; |
| 2389 | mLastReorderY = mTargetCell[1]; |
| 2390 | |
Adam Cohen | fa3c58f | 2013-12-06 16:10:55 -0800 | [diff] [blame] | 2391 | mTargetCell = mDragTargetLayout.performReorder((int) mDragViewVisualCenter[0], |
Adam Cohen | 482ed82 | 2012-03-02 14:15:13 -0800 | [diff] [blame] | 2392 | (int) mDragViewVisualCenter[1], minSpanX, minSpanY, spanX, spanY, |
| 2393 | child, mTargetCell, resultSpan, CellLayout.MODE_DRAG_OVER); |
| 2394 | |
Adam Cohen | 19f3792 | 2012-03-21 11:59:11 -0700 | [diff] [blame] | 2395 | if (mTargetCell[0] < 0 || mTargetCell[1] < 0) { |
| 2396 | mDragTargetLayout.revertTempState(); |
Adam Cohen | c6cc61d | 2012-04-04 12:47:08 -0700 | [diff] [blame] | 2397 | } else { |
| 2398 | setDragMode(DRAG_MODE_REORDER); |
Adam Cohen | 19f3792 | 2012-03-21 11:59:11 -0700 | [diff] [blame] | 2399 | } |
Adam Cohen | 482ed82 | 2012-03-02 14:15:13 -0800 | [diff] [blame] | 2400 | |
Adam Cohen | 482ed82 | 2012-03-02 14:15:13 -0800 | [diff] [blame] | 2401 | boolean resize = resultSpan[0] != spanX || resultSpan[1] != spanY; |
Sunny Goyal | 06e21a2 | 2016-08-11 16:02:02 -0700 | [diff] [blame] | 2402 | mDragTargetLayout.visualizeDropLocation(child, mOutlineProvider, |
Sunny Goyal | e78e3d7 | 2015-09-24 11:23:31 -0700 | [diff] [blame] | 2403 | mTargetCell[0], mTargetCell[1], resultSpan[0], resultSpan[1], resize, dragObject); |
Adam Cohen | 19072da | 2011-05-31 14:30:45 -0700 | [diff] [blame] | 2404 | } |
| 2405 | } |
| 2406 | |
Winson Chung | a34abf8 | 2010-11-12 12:10:35 -0800 | [diff] [blame] | 2407 | @Override |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 2408 | public void getHitRectRelativeToDragLayer(Rect outRect) { |
Winson Chung | a34abf8 | 2010-11-12 12:10:35 -0800 | [diff] [blame] | 2409 | // We want the workspace to have the whole area of the display (it will find the correct |
| 2410 | // cell layout to drop to in the existing drag/drop logic. |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 2411 | mLauncher.getDragLayer().getDescendantRectRelativeToSelf(this, outRect); |
Winson Chung | a34abf8 | 2010-11-12 12:10:35 -0800 | [diff] [blame] | 2412 | } |
| 2413 | |
Patrick Dubroy | 2b9ff37 | 2010-09-07 17:49:27 -0700 | [diff] [blame] | 2414 | /** |
Adam Cohen | 120980b | 2010-12-08 11:05:37 -0800 | [diff] [blame] | 2415 | * Drop an item that didn't originate on one of the workspace screens. |
| 2416 | * It may have come from Launcher (e.g. from all apps or customize), or it may have |
| 2417 | * come from another app altogether. |
| 2418 | * |
| 2419 | * NOTE: This can also be called when we are outside of a drag event, when we want |
| 2420 | * to add an item to one of the workspace screens. |
| 2421 | */ |
Sunny Goyal | dec3a90 | 2017-01-25 18:23:36 -0800 | [diff] [blame] | 2422 | private void onDropExternal(final int[] touchXY, final CellLayout cellLayout, DragObject d) { |
Sunny Goyal | dec3a90 | 2017-01-25 18:23:36 -0800 | [diff] [blame] | 2423 | if (d.dragInfo instanceof PendingAddShortcutInfo) { |
| 2424 | ShortcutInfo si = ((PendingAddShortcutInfo) d.dragInfo) |
| 2425 | .activityInfo.createShortcutInfo(); |
| 2426 | if (si != null) { |
| 2427 | d.dragInfo = si; |
| 2428 | } |
| 2429 | } |
| 2430 | |
| 2431 | ItemInfo info = d.dragInfo; |
Adam Cohen | b7e1618 | 2011-07-15 17:55:02 -0700 | [diff] [blame] | 2432 | int spanX = info.spanX; |
| 2433 | int spanY = info.spanY; |
| 2434 | if (mDragInfo != null) { |
| 2435 | spanX = mDragInfo.spanX; |
| 2436 | spanY = mDragInfo.spanY; |
| 2437 | } |
| 2438 | |
Sunny Goyal | efb7e84 | 2018-10-04 15:11:00 -0700 | [diff] [blame] | 2439 | final int container = mLauncher.isHotseatLayout(cellLayout) ? |
Winson Chung | 3d503fb | 2011-07-13 17:25:49 -0700 | [diff] [blame] | 2440 | LauncherSettings.Favorites.CONTAINER_HOTSEAT : |
| 2441 | LauncherSettings.Favorites.CONTAINER_DESKTOP; |
Sunny Goyal | efb7e84 | 2018-10-04 15:11:00 -0700 | [diff] [blame] | 2442 | final int screenId = getIdForScreen(cellLayout); |
Adam Cohen | dcd297f | 2013-06-18 13:13:40 -0700 | [diff] [blame] | 2443 | if (!mLauncher.isHotseatLayout(cellLayout) |
| 2444 | && screenId != getScreenIdForPageIndex(mCurrentPage) |
Sunny Goyal | ea60926 | 2017-10-25 15:47:38 -0700 | [diff] [blame] | 2445 | && !mLauncher.isInState(SPRING_LOADED)) { |
Sunny Goyal | 7ce471b | 2017-08-02 03:37:39 -0700 | [diff] [blame] | 2446 | snapToPage(getPageIndexForScreenId(screenId)); |
Adam Cohen | 76078c4 | 2011-06-09 15:06:52 -0700 | [diff] [blame] | 2447 | } |
Adam Cohen | b7e1618 | 2011-07-15 17:55:02 -0700 | [diff] [blame] | 2448 | |
| 2449 | if (info instanceof PendingAddItemInfo) { |
Sunny Goyal | dec3a90 | 2017-01-25 18:23:36 -0800 | [diff] [blame] | 2450 | final PendingAddItemInfo pendingInfo = (PendingAddItemInfo) info; |
Adam Cohen | b7e1618 | 2011-07-15 17:55:02 -0700 | [diff] [blame] | 2451 | |
Adam Cohen | 558baaf | 2011-08-15 15:22:57 -0700 | [diff] [blame] | 2452 | boolean findNearestVacantCell = true; |
| 2453 | if (pendingInfo.itemType == LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT) { |
Rajeev Kumar | 93c0512 | 2017-06-12 12:59:47 -0700 | [diff] [blame] | 2454 | mTargetCell = findNearestArea(touchXY[0], touchXY[1], spanX, spanY, |
Adam Cohen | 558baaf | 2011-08-15 15:22:57 -0700 | [diff] [blame] | 2455 | cellLayout, mTargetCell); |
Adam Cohen | 482ed82 | 2012-03-02 14:15:13 -0800 | [diff] [blame] | 2456 | float distance = cellLayout.getDistanceFromCell(mDragViewVisualCenter[0], |
| 2457 | mDragViewVisualCenter[1], mTargetCell); |
Sunny Goyal | aa8ef11 | 2015-06-12 20:04:41 -0700 | [diff] [blame] | 2458 | if (willCreateUserFolder(d.dragInfo, cellLayout, mTargetCell, distance, true) |
| 2459 | || willAddToExistingUserFolder( |
| 2460 | d.dragInfo, cellLayout, mTargetCell, distance)) { |
Adam Cohen | 558baaf | 2011-08-15 15:22:57 -0700 | [diff] [blame] | 2461 | findNearestVacantCell = false; |
| 2462 | } |
| 2463 | } |
Adam Cohen | 482ed82 | 2012-03-02 14:15:13 -0800 | [diff] [blame] | 2464 | |
Sunny Goyal | aa8ef11 | 2015-06-12 20:04:41 -0700 | [diff] [blame] | 2465 | final ItemInfo item = d.dragInfo; |
Adam Cohen | aaa5c21 | 2012-10-05 18:14:31 -0700 | [diff] [blame] | 2466 | boolean updateWidgetSize = false; |
Adam Cohen | 558baaf | 2011-08-15 15:22:57 -0700 | [diff] [blame] | 2467 | if (findNearestVacantCell) { |
Adam Cohen | 482ed82 | 2012-03-02 14:15:13 -0800 | [diff] [blame] | 2468 | int minSpanX = item.spanX; |
| 2469 | int minSpanY = item.spanY; |
| 2470 | if (item.minSpanX > 0 && item.minSpanY > 0) { |
| 2471 | minSpanX = item.minSpanX; |
| 2472 | minSpanY = item.minSpanY; |
| 2473 | } |
Adam Cohen | d41fbf5 | 2012-02-16 23:53:59 -0800 | [diff] [blame] | 2474 | int[] resultSpan = new int[2]; |
Adam Cohen | fa3c58f | 2013-12-06 16:10:55 -0800 | [diff] [blame] | 2475 | mTargetCell = cellLayout.performReorder((int) mDragViewVisualCenter[0], |
Adam Cohen | 482ed82 | 2012-03-02 14:15:13 -0800 | [diff] [blame] | 2476 | (int) mDragViewVisualCenter[1], minSpanX, minSpanY, info.spanX, info.spanY, |
| 2477 | null, mTargetCell, resultSpan, CellLayout.MODE_ON_DROP_EXTERNAL); |
Adam Cohen | 9e05a5e | 2012-09-10 15:53:09 -0700 | [diff] [blame] | 2478 | |
| 2479 | if (resultSpan[0] != item.spanX || resultSpan[1] != item.spanY) { |
| 2480 | updateWidgetSize = true; |
| 2481 | } |
Adam Cohen | d41fbf5 | 2012-02-16 23:53:59 -0800 | [diff] [blame] | 2482 | item.spanX = resultSpan[0]; |
| 2483 | item.spanY = resultSpan[1]; |
Adam Cohen | 558baaf | 2011-08-15 15:22:57 -0700 | [diff] [blame] | 2484 | } |
| 2485 | |
Adam Cohen | b7e1618 | 2011-07-15 17:55:02 -0700 | [diff] [blame] | 2486 | Runnable onAnimationCompleteRunnable = new Runnable() { |
Winson Chung | 557d6ed | 2011-07-08 15:34:52 -0700 | [diff] [blame] | 2487 | @Override |
| 2488 | public void run() { |
Adam Cohen | 689ff16 | 2014-05-08 17:27:56 -0700 | [diff] [blame] | 2489 | // Normally removeExtraEmptyScreen is called in Workspace#onDragEnd, but when |
| 2490 | // adding an item that may not be dropped right away (due to a config activity) |
| 2491 | // we defer the removal until the activity returns. |
| 2492 | deferRemoveExtraEmptyScreen(); |
| 2493 | |
Winson Chung | 557d6ed | 2011-07-08 15:34:52 -0700 | [diff] [blame] | 2494 | // When dragging and dropping from customization tray, we deal with creating |
| 2495 | // widgets/shortcuts/folders in a slightly different way |
Sunny Goyal | 71b5c0b | 2015-01-08 16:59:04 -0800 | [diff] [blame] | 2496 | mLauncher.addPendingItem(pendingInfo, container, screenId, mTargetCell, |
| 2497 | item.spanX, item.spanY); |
Winson Chung | 557d6ed | 2011-07-08 15:34:52 -0700 | [diff] [blame] | 2498 | } |
Adam Cohen | b7e1618 | 2011-07-15 17:55:02 -0700 | [diff] [blame] | 2499 | }; |
Adam Cohen | 5940042 | 2014-03-05 18:07:04 -0800 | [diff] [blame] | 2500 | boolean isWidget = pendingInfo.itemType == LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET |
| 2501 | || pendingInfo.itemType == LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET; |
| 2502 | |
Sunny Goyal | b740f59 | 2015-12-17 23:22:42 -0800 | [diff] [blame] | 2503 | AppWidgetHostView finalView = isWidget ? |
| 2504 | ((PendingAddWidgetInfo) pendingInfo).boundWidget : null; |
Adam Cohen | 9e05a5e | 2012-09-10 15:53:09 -0700 | [diff] [blame] | 2505 | |
Sunny Goyal | b740f59 | 2015-12-17 23:22:42 -0800 | [diff] [blame] | 2506 | if (finalView != null && updateWidgetSize) { |
| 2507 | AppWidgetResizeFrame.updateWidgetSizeRanges(finalView, mLauncher, item.spanX, |
Adam Cohen | 9e05a5e | 2012-09-10 15:53:09 -0700 | [diff] [blame] | 2508 | item.spanY); |
| 2509 | } |
| 2510 | |
Adam Cohen | d41fbf5 | 2012-02-16 23:53:59 -0800 | [diff] [blame] | 2511 | int animationStyle = ANIMATE_INTO_POSITION_AND_DISAPPEAR; |
Adam Cohen | 5940042 | 2014-03-05 18:07:04 -0800 | [diff] [blame] | 2512 | if (isWidget && ((PendingAddWidgetInfo) pendingInfo).info != null && |
Sunny Goyal | dec3a90 | 2017-01-25 18:23:36 -0800 | [diff] [blame] | 2513 | ((PendingAddWidgetInfo) pendingInfo).getHandler().needsConfigure()) { |
Adam Cohen | d41fbf5 | 2012-02-16 23:53:59 -0800 | [diff] [blame] | 2514 | animationStyle = ANIMATE_INTO_POSITION_AND_REMAIN; |
| 2515 | } |
| 2516 | animateWidgetDrop(info, cellLayout, d.dragView, onAnimationCompleteRunnable, |
| 2517 | animationStyle, finalView, true); |
Patrick Dubroy | 6569f2c | 2010-07-12 14:25:18 -0700 | [diff] [blame] | 2518 | } else { |
Michael Jurka | 9c6de3d | 2010-11-23 16:23:58 -0800 | [diff] [blame] | 2519 | // This is for other drag/drop cases, like dragging from All Apps |
Sunny Goyal | 3e3f44c | 2017-10-23 17:14:52 -0700 | [diff] [blame] | 2520 | mLauncher.getStateManager().goToState(NORMAL, SPRING_LOADED_EXIT_DELAY); |
Sunny Goyal | 1797af4 | 2017-10-06 13:29:57 -0700 | [diff] [blame] | 2521 | |
Rajeev Kumar | 93c0512 | 2017-06-12 12:59:47 -0700 | [diff] [blame] | 2522 | View view; |
Michael Jurka | 9c6de3d | 2010-11-23 16:23:58 -0800 | [diff] [blame] | 2523 | |
| 2524 | switch (info.itemType) { |
Jon Miranda | 4334874 | 2019-03-06 10:40:13 -0800 | [diff] [blame] | 2525 | case ITEM_TYPE_APPLICATION: |
Michael Jurka | 9c6de3d | 2010-11-23 16:23:58 -0800 | [diff] [blame] | 2526 | case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT: |
Tony Wickham | bfbf7f9 | 2016-05-19 11:19:39 -0700 | [diff] [blame] | 2527 | case LauncherSettings.Favorites.ITEM_TYPE_DEEP_SHORTCUT: |
Sunny Goyal | c487b36 | 2018-07-13 17:03:34 +0000 | [diff] [blame] | 2528 | if (info.container == NO_ID && info instanceof AppInfo) { |
Michael Jurka | 9c6de3d | 2010-11-23 16:23:58 -0800 | [diff] [blame] | 2529 | // Came from all apps -- make a copy |
Sunny Goyal | c487b36 | 2018-07-13 17:03:34 +0000 | [diff] [blame] | 2530 | info = ((AppInfo) info).makeShortcut(); |
| 2531 | d.dragInfo = info; |
Michael Jurka | 9c6de3d | 2010-11-23 16:23:58 -0800 | [diff] [blame] | 2532 | } |
Sunny Goyal | dfaccf6 | 2015-05-11 16:30:44 -0700 | [diff] [blame] | 2533 | view = mLauncher.createShortcut(cellLayout, (ShortcutInfo) info); |
Michael Jurka | 9c6de3d | 2010-11-23 16:23:58 -0800 | [diff] [blame] | 2534 | break; |
Adam Cohen | df2cc41 | 2011-04-27 16:56:57 -0700 | [diff] [blame] | 2535 | case LauncherSettings.Favorites.ITEM_TYPE_FOLDER: |
Adam Cohen | c0dcf59 | 2011-06-01 15:30:43 -0700 | [diff] [blame] | 2536 | view = FolderIcon.fromXml(R.layout.folder_icon, mLauncher, cellLayout, |
Sunny Goyal | 1cd01b0 | 2016-11-09 10:43:58 -0800 | [diff] [blame] | 2537 | (FolderInfo) info); |
Michael Jurka | 9c6de3d | 2010-11-23 16:23:58 -0800 | [diff] [blame] | 2538 | break; |
| 2539 | default: |
| 2540 | throw new IllegalStateException("Unknown item type: " + info.itemType); |
| 2541 | } |
| 2542 | |
Adam Cohen | c0dcf59 | 2011-06-01 15:30:43 -0700 | [diff] [blame] | 2543 | // First we find the cell nearest to point at which the item is |
| 2544 | // dropped, without any consideration to whether there is an item there. |
| 2545 | if (touchXY != null) { |
Rajeev Kumar | 93c0512 | 2017-06-12 12:59:47 -0700 | [diff] [blame] | 2546 | mTargetCell = findNearestArea(touchXY[0], touchXY[1], spanX, spanY, |
Adam Cohen | c0dcf59 | 2011-06-01 15:30:43 -0700 | [diff] [blame] | 2547 | cellLayout, mTargetCell); |
Adam Cohen | 482ed82 | 2012-03-02 14:15:13 -0800 | [diff] [blame] | 2548 | float distance = cellLayout.getDistanceFromCell(mDragViewVisualCenter[0], |
| 2549 | mDragViewVisualCenter[1], mTargetCell); |
Adam Cohen | 482ed82 | 2012-03-02 14:15:13 -0800 | [diff] [blame] | 2550 | if (createUserFolderIfNecessary(view, container, cellLayout, mTargetCell, distance, |
Sunny Goyal | 1797af4 | 2017-10-06 13:29:57 -0700 | [diff] [blame] | 2551 | true, d.dragView)) { |
Adam Cohen | c0dcf59 | 2011-06-01 15:30:43 -0700 | [diff] [blame] | 2552 | return; |
| 2553 | } |
Adam Cohen | 482ed82 | 2012-03-02 14:15:13 -0800 | [diff] [blame] | 2554 | if (addToExistingFolderIfNecessary(view, cellLayout, mTargetCell, distance, d, |
| 2555 | true)) { |
Adam Cohen | c0dcf59 | 2011-06-01 15:30:43 -0700 | [diff] [blame] | 2556 | return; |
| 2557 | } |
Adam Cohen | 10b1737 | 2011-04-15 14:21:25 -0700 | [diff] [blame] | 2558 | } |
| 2559 | |
Michael Jurka | c4e772e | 2011-02-10 13:32:01 -0800 | [diff] [blame] | 2560 | if (touchXY != null) { |
Michael Jurka | 9c6de3d | 2010-11-23 16:23:58 -0800 | [diff] [blame] | 2561 | // when dragging and dropping, just find the closest free spot |
Adam Cohen | fa3c58f | 2013-12-06 16:10:55 -0800 | [diff] [blame] | 2562 | mTargetCell = cellLayout.performReorder((int) mDragViewVisualCenter[0], |
Adam Cohen | 482ed82 | 2012-03-02 14:15:13 -0800 | [diff] [blame] | 2563 | (int) mDragViewVisualCenter[1], 1, 1, 1, 1, |
Adam Cohen | ea889a2 | 2012-03-27 16:45:39 -0700 | [diff] [blame] | 2564 | null, mTargetCell, null, CellLayout.MODE_ON_DROP_EXTERNAL); |
Michael Jurka | 9c6de3d | 2010-11-23 16:23:58 -0800 | [diff] [blame] | 2565 | } else { |
| 2566 | cellLayout.findCellForSpan(mTargetCell, 1, 1); |
| 2567 | } |
Sunny Goyal | 95abbb3 | 2014-08-04 10:53:22 -0700 | [diff] [blame] | 2568 | // Add the item to DB before adding to screen ensures that the container and other |
| 2569 | // values of the info is properly updated. |
Sunny Goyal | 43bf11d | 2017-02-02 13:52:53 -0800 | [diff] [blame] | 2570 | mLauncher.getModelWriter().addOrMoveItemInDatabase(info, container, screenId, |
Sunny Goyal | 95abbb3 | 2014-08-04 10:53:22 -0700 | [diff] [blame] | 2571 | mTargetCell[0], mTargetCell[1]); |
| 2572 | |
Sunny Goyal | d5462aa | 2016-11-22 16:52:39 +0530 | [diff] [blame] | 2573 | addInScreen(view, container, screenId, mTargetCell[0], mTargetCell[1], |
| 2574 | info.spanX, info.spanY); |
Adam Cohen | 716b51e | 2011-06-30 12:09:54 -0700 | [diff] [blame] | 2575 | cellLayout.onDropChild(view); |
Michael Jurka | a52570f | 2012-03-20 03:18:20 -0700 | [diff] [blame] | 2576 | cellLayout.getShortcutsAndWidgets().measureChild(view); |
Adam Cohen | d5e4273 | 2011-03-28 17:33:39 -0700 | [diff] [blame] | 2577 | |
Adam Cohen | 3e8f811 | 2011-07-02 18:03:00 -0700 | [diff] [blame] | 2578 | if (d.dragView != null) { |
Adam Cohen | 4b285c5 | 2011-07-21 14:24:06 -0700 | [diff] [blame] | 2579 | // We wrap the animation call in the temporary set and reset of the current |
| 2580 | // cellLayout to its final transform -- this means we animate the drag view to |
| 2581 | // the correct final location. |
Sunny Goyal | aeb1643 | 2017-10-16 11:46:41 -0700 | [diff] [blame] | 2582 | setFinalTransitionTransform(); |
| 2583 | mLauncher.getDragLayer().animateViewIntoPosition(d.dragView, view, this); |
| 2584 | resetTransitionTransform(); |
Adam Cohen | 3e8f811 | 2011-07-02 18:03:00 -0700 | [diff] [blame] | 2585 | } |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 2586 | } |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 2587 | } |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 2588 | |
Adam Cohen | d41fbf5 | 2012-02-16 23:53:59 -0800 | [diff] [blame] | 2589 | public Bitmap createWidgetBitmap(ItemInfo widgetInfo, View layout) { |
Sunny Goyal | aa3a2ba | 2017-09-26 12:43:16 -0700 | [diff] [blame] | 2590 | int[] unScaledSize = estimateItemSize(widgetInfo); |
Adam Cohen | d41fbf5 | 2012-02-16 23:53:59 -0800 | [diff] [blame] | 2591 | int visibility = layout.getVisibility(); |
Adam Cohen | ed66b2b | 2012-01-23 17:28:51 -0800 | [diff] [blame] | 2592 | layout.setVisibility(VISIBLE); |
| 2593 | |
| 2594 | int width = MeasureSpec.makeMeasureSpec(unScaledSize[0], MeasureSpec.EXACTLY); |
| 2595 | int height = MeasureSpec.makeMeasureSpec(unScaledSize[1], MeasureSpec.EXACTLY); |
| 2596 | Bitmap b = Bitmap.createBitmap(unScaledSize[0], unScaledSize[1], |
| 2597 | Bitmap.Config.ARGB_8888); |
Adam Cohen | ed66b2b | 2012-01-23 17:28:51 -0800 | [diff] [blame] | 2598 | layout.measure(width, height); |
| 2599 | layout.layout(0, 0, unScaledSize[0], unScaledSize[1]); |
Sunny Goyal | aa3a2ba | 2017-09-26 12:43:16 -0700 | [diff] [blame] | 2600 | layout.draw(new Canvas(b)); |
Adam Cohen | d41fbf5 | 2012-02-16 23:53:59 -0800 | [diff] [blame] | 2601 | layout.setVisibility(visibility); |
Adam Cohen | ed66b2b | 2012-01-23 17:28:51 -0800 | [diff] [blame] | 2602 | return b; |
| 2603 | } |
| 2604 | |
Adam Cohen | d41fbf5 | 2012-02-16 23:53:59 -0800 | [diff] [blame] | 2605 | private void getFinalPositionForDropAnimation(int[] loc, float[] scaleXY, |
Tony Wickham | a501d49 | 2015-11-03 18:05:01 -0800 | [diff] [blame] | 2606 | DragView dragView, CellLayout layout, ItemInfo info, int[] targetCell, boolean scale) { |
Adam Cohen | ed66b2b | 2012-01-23 17:28:51 -0800 | [diff] [blame] | 2607 | // Now we animate the dragView, (ie. the widget or shortcut preview) into its final |
| 2608 | // location and size on the home screen. |
Adam Cohen | d41fbf5 | 2012-02-16 23:53:59 -0800 | [diff] [blame] | 2609 | int spanX = info.spanX; |
| 2610 | int spanY = info.spanY; |
Adam Cohen | ed66b2b | 2012-01-23 17:28:51 -0800 | [diff] [blame] | 2611 | |
Tony Wickham | a501d49 | 2015-11-03 18:05:01 -0800 | [diff] [blame] | 2612 | Rect r = estimateItemPosition(layout, targetCell[0], targetCell[1], spanX, spanY); |
Jon Miranda | e96798e | 2016-12-07 12:10:44 -0800 | [diff] [blame] | 2613 | if (info.itemType == LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET) { |
| 2614 | DeviceProfile profile = mLauncher.getDeviceProfile(); |
Jon Miranda | 6f6a06a | 2016-12-15 11:24:18 -0800 | [diff] [blame] | 2615 | Utilities.shrinkRect(r, profile.appWidgetScale.x, profile.appWidgetScale.y); |
Jon Miranda | e96798e | 2016-12-07 12:10:44 -0800 | [diff] [blame] | 2616 | } |
Adam Cohen | d41fbf5 | 2012-02-16 23:53:59 -0800 | [diff] [blame] | 2617 | loc[0] = r.left; |
| 2618 | loc[1] = r.top; |
| 2619 | |
Sunny Goyal | aeb1643 | 2017-10-16 11:46:41 -0700 | [diff] [blame] | 2620 | setFinalTransitionTransform(); |
Adam Cohen | d41fbf5 | 2012-02-16 23:53:59 -0800 | [diff] [blame] | 2621 | float cellLayoutScale = |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 2622 | mLauncher.getDragLayer().getDescendantCoordRelativeToSelf(layout, loc, true); |
Sunny Goyal | aeb1643 | 2017-10-16 11:46:41 -0700 | [diff] [blame] | 2623 | resetTransitionTransform(); |
Michael Jurka | a2fe786 | 2012-05-29 05:58:17 -0700 | [diff] [blame] | 2624 | |
Michael Jurka | a2fe786 | 2012-05-29 05:58:17 -0700 | [diff] [blame] | 2625 | if (scale) { |
Sunny Goyal | dec3a90 | 2017-01-25 18:23:36 -0800 | [diff] [blame] | 2626 | float dragViewScaleX = (1.0f * r.width()) / dragView.getMeasuredWidth(); |
| 2627 | float dragViewScaleY = (1.0f * r.height()) / dragView.getMeasuredHeight(); |
| 2628 | |
| 2629 | // The animation will scale the dragView about its center, so we need to center about |
| 2630 | // the final location. |
| 2631 | loc[0] -= (dragView.getMeasuredWidth() - cellLayoutScale * r.width()) / 2 |
| 2632 | - Math.ceil(layout.getUnusedHorizontalSpace() / 2f); |
| 2633 | loc[1] -= (dragView.getMeasuredHeight() - cellLayoutScale * r.height()) / 2; |
| 2634 | scaleXY[0] = dragViewScaleX * cellLayoutScale; |
| 2635 | scaleXY[1] = dragViewScaleY * cellLayoutScale; |
| 2636 | } else { |
| 2637 | // Since we are not cross-fading the dragView, align the drag view to the |
| 2638 | // final cell position. |
| 2639 | float dragScale = dragView.getInitialScale() * cellLayoutScale; |
| 2640 | loc[0] += (dragScale - 1) * dragView.getWidth() / 2; |
| 2641 | loc[1] += (dragScale - 1) * dragView.getHeight() / 2; |
| 2642 | scaleXY[0] = scaleXY[1] = dragScale; |
| 2643 | |
| 2644 | // If a dragRegion was provided, offset the final position accordingly. |
| 2645 | Rect dragRegion = dragView.getDragRegion(); |
| 2646 | if (dragRegion != null) { |
| 2647 | loc[0] += cellLayoutScale * dragRegion.left; |
| 2648 | loc[1] += cellLayoutScale * dragRegion.top; |
| 2649 | } |
Michael Jurka | a2fe786 | 2012-05-29 05:58:17 -0700 | [diff] [blame] | 2650 | } |
Adam Cohen | d41fbf5 | 2012-02-16 23:53:59 -0800 | [diff] [blame] | 2651 | } |
| 2652 | |
Tony Wickham | a501d49 | 2015-11-03 18:05:01 -0800 | [diff] [blame] | 2653 | public void animateWidgetDrop(ItemInfo info, CellLayout cellLayout, final DragView dragView, |
Adam Cohen | d41fbf5 | 2012-02-16 23:53:59 -0800 | [diff] [blame] | 2654 | final Runnable onCompleteRunnable, int animationType, final View finalView, |
| 2655 | boolean external) { |
| 2656 | Rect from = new Rect(); |
| 2657 | mLauncher.getDragLayer().getViewRectRelativeToSelf(dragView, from); |
| 2658 | |
| 2659 | int[] finalPos = new int[2]; |
| 2660 | float scaleXY[] = new float[2]; |
Michael Jurka | a2fe786 | 2012-05-29 05:58:17 -0700 | [diff] [blame] | 2661 | boolean scalePreview = !(info instanceof PendingAddShortcutInfo); |
Adam Cohen | d41fbf5 | 2012-02-16 23:53:59 -0800 | [diff] [blame] | 2662 | getFinalPositionForDropAnimation(finalPos, scaleXY, dragView, cellLayout, info, mTargetCell, |
Tony Wickham | a501d49 | 2015-11-03 18:05:01 -0800 | [diff] [blame] | 2663 | scalePreview); |
Adam Cohen | ed66b2b | 2012-01-23 17:28:51 -0800 | [diff] [blame] | 2664 | |
| 2665 | Resources res = mLauncher.getResources(); |
Adam Cohen | ad4e15c | 2013-10-17 16:21:35 -0700 | [diff] [blame] | 2666 | final int duration = res.getInteger(R.integer.config_dropAnimMaxDuration) - 200; |
Adam Cohen | ed66b2b | 2012-01-23 17:28:51 -0800 | [diff] [blame] | 2667 | |
Adam Cohen | 5940042 | 2014-03-05 18:07:04 -0800 | [diff] [blame] | 2668 | boolean isWidget = info.itemType == LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET || |
| 2669 | info.itemType == LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET; |
Adam Cohen | d41fbf5 | 2012-02-16 23:53:59 -0800 | [diff] [blame] | 2670 | if ((animationType == ANIMATE_INTO_POSITION_AND_RESIZE || external) && finalView != null) { |
| 2671 | Bitmap crossFadeBitmap = createWidgetBitmap(info, finalView); |
Adam Cohen | ed66b2b | 2012-01-23 17:28:51 -0800 | [diff] [blame] | 2672 | dragView.setCrossFadeBitmap(crossFadeBitmap); |
| 2673 | dragView.crossFade((int) (duration * 0.8f)); |
Adam Cohen | 5940042 | 2014-03-05 18:07:04 -0800 | [diff] [blame] | 2674 | } else if (isWidget && external) { |
Adam Cohen | d41fbf5 | 2012-02-16 23:53:59 -0800 | [diff] [blame] | 2675 | scaleXY[0] = scaleXY[1] = Math.min(scaleXY[0], scaleXY[1]); |
Adam Cohen | ed66b2b | 2012-01-23 17:28:51 -0800 | [diff] [blame] | 2676 | } |
| 2677 | |
Adam Cohen | d41fbf5 | 2012-02-16 23:53:59 -0800 | [diff] [blame] | 2678 | DragLayer dragLayer = mLauncher.getDragLayer(); |
Winson Chung | 7bd1bbb | 2012-02-13 18:29:29 -0800 | [diff] [blame] | 2679 | if (animationType == CANCEL_TWO_STAGE_WIDGET_DROP_ANIMATION) { |
Adam Cohen | d41fbf5 | 2012-02-16 23:53:59 -0800 | [diff] [blame] | 2680 | mLauncher.getDragLayer().animateViewIntoPosition(dragView, finalPos, 0f, 0.1f, 0.1f, |
Adam Cohen | ed66b2b | 2012-01-23 17:28:51 -0800 | [diff] [blame] | 2681 | DragLayer.ANIMATION_END_DISAPPEAR, onCompleteRunnable, duration); |
| 2682 | } else { |
Adam Cohen | d41fbf5 | 2012-02-16 23:53:59 -0800 | [diff] [blame] | 2683 | int endStyle; |
| 2684 | if (animationType == ANIMATE_INTO_POSITION_AND_REMAIN) { |
| 2685 | endStyle = DragLayer.ANIMATION_END_REMAIN_VISIBLE; |
| 2686 | } else { |
Tony Wickham | a501d49 | 2015-11-03 18:05:01 -0800 | [diff] [blame] | 2687 | endStyle = DragLayer.ANIMATION_END_DISAPPEAR; |
Adam Cohen | d41fbf5 | 2012-02-16 23:53:59 -0800 | [diff] [blame] | 2688 | } |
| 2689 | |
| 2690 | Runnable onComplete = new Runnable() { |
| 2691 | @Override |
| 2692 | public void run() { |
| 2693 | if (finalView != null) { |
| 2694 | finalView.setVisibility(VISIBLE); |
| 2695 | } |
| 2696 | if (onCompleteRunnable != null) { |
| 2697 | onCompleteRunnable.run(); |
| 2698 | } |
| 2699 | } |
| 2700 | }; |
| 2701 | dragLayer.animateViewIntoPosition(dragView, from.left, from.top, finalPos[0], |
| 2702 | finalPos[1], 1, 1, 1, scaleXY[0], scaleXY[1], onComplete, endStyle, |
| 2703 | duration, this); |
Adam Cohen | ed66b2b | 2012-01-23 17:28:51 -0800 | [diff] [blame] | 2704 | } |
| 2705 | } |
| 2706 | |
Sunny Goyal | aeb1643 | 2017-10-16 11:46:41 -0700 | [diff] [blame] | 2707 | public void setFinalTransitionTransform() { |
Adam Cohen | 4b285c5 | 2011-07-21 14:24:06 -0700 | [diff] [blame] | 2708 | if (isSwitchingState()) { |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 2709 | mCurrentScale = getScaleX(); |
Winson Chung | dc61c4d | 2015-04-20 18:26:57 -0700 | [diff] [blame] | 2710 | setScaleX(mStateTransitionAnimation.getFinalScale()); |
| 2711 | setScaleY(mStateTransitionAnimation.getFinalScale()); |
Adam Cohen | 4b285c5 | 2011-07-21 14:24:06 -0700 | [diff] [blame] | 2712 | } |
| 2713 | } |
Sunny Goyal | aeb1643 | 2017-10-16 11:46:41 -0700 | [diff] [blame] | 2714 | public void resetTransitionTransform() { |
Adam Cohen | 4b285c5 | 2011-07-21 14:24:06 -0700 | [diff] [blame] | 2715 | if (isSwitchingState()) { |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 2716 | setScaleX(mCurrentScale); |
| 2717 | setScaleY(mCurrentScale); |
Adam Cohen | 4b285c5 | 2011-07-21 14:24:06 -0700 | [diff] [blame] | 2718 | } |
| 2719 | } |
| 2720 | |
Jeff Sharkey | 7086428 | 2009-04-07 21:08:40 -0700 | [diff] [blame] | 2721 | /** |
Michael Jurka | 0280c3b | 2010-09-17 15:00:07 -0700 | [diff] [blame] | 2722 | * Return the current CellInfo describing our current drag; this method exists |
| 2723 | * so that Launcher can sync this object with the correct info when the activity is created/ |
| 2724 | * destroyed |
| 2725 | * |
| 2726 | */ |
| 2727 | public CellLayout.CellInfo getDragInfo() { |
| 2728 | return mDragInfo; |
| 2729 | } |
| 2730 | |
| 2731 | /** |
Jeff Sharkey | 7086428 | 2009-04-07 21:08:40 -0700 | [diff] [blame] | 2732 | * Calculate the nearest cell where the given object would be dropped. |
Adam Cohen | e3e27a8 | 2011-04-15 12:07:39 -0700 | [diff] [blame] | 2733 | * |
| 2734 | * pixelX and pixelY should be in the coordinate system of layout |
Jeff Sharkey | 7086428 | 2009-04-07 21:08:40 -0700 | [diff] [blame] | 2735 | */ |
Adam Cohen | 091440a | 2015-03-18 14:16:05 -0700 | [diff] [blame] | 2736 | @Thunk int[] findNearestArea(int pixelX, int pixelY, |
Adam Cohen | e3e27a8 | 2011-04-15 12:07:39 -0700 | [diff] [blame] | 2737 | int spanX, int spanY, CellLayout layout, int[] recycle) { |
Adam Cohen | df03538 | 2011-04-11 17:22:04 -0700 | [diff] [blame] | 2738 | return layout.findNearestArea( |
Adam Cohen | e3e27a8 | 2011-04-15 12:07:39 -0700 | [diff] [blame] | 2739 | pixelX, pixelY, spanX, spanY, recycle); |
Adam Cohen | df03538 | 2011-04-11 17:22:04 -0700 | [diff] [blame] | 2740 | } |
| 2741 | |
Adam Cohen | cff6af8 | 2011-09-13 14:51:53 -0700 | [diff] [blame] | 2742 | void setup(DragController dragController) { |
Michael Jurka | c2f7f47 | 2010-12-14 15:34:42 -0800 | [diff] [blame] | 2743 | mSpringLoadedDragController = new SpringLoadedDragController(mLauncher); |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 2744 | mDragController = dragController; |
Michael Jurka | d74c984 | 2011-07-10 12:44:21 -0700 | [diff] [blame] | 2745 | |
Michael Jurka | d74c984 | 2011-07-10 12:44:21 -0700 | [diff] [blame] | 2746 | // hardware layers on children are enabled on startup, but should be disabled until |
| 2747 | // needed |
Sunny Goyal | a502aa3 | 2017-10-02 16:04:06 -0700 | [diff] [blame] | 2748 | updateChildrenLayersEnabled(); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 2749 | } |
| 2750 | |
Patrick Dubroy | e3887cc | 2011-01-20 10:43:40 -0800 | [diff] [blame] | 2751 | /** |
| 2752 | * Called at the end of a drag which originated on the workspace. |
| 2753 | */ |
Michael Jurka | 1e2f465 | 2013-07-08 18:03:46 -0700 | [diff] [blame] | 2754 | public void onDropCompleted(final View target, final DragObject d, |
Sunny Goyal | 1797af4 | 2017-10-06 13:29:57 -0700 | [diff] [blame] | 2755 | final boolean success) { |
Sunny Goyal | 3dce5f3 | 2017-10-05 11:40:05 -0700 | [diff] [blame] | 2756 | if (success) { |
Sunny Goyal | 95deb3a | 2015-03-05 10:27:01 -0800 | [diff] [blame] | 2757 | if (target != this && mDragInfo != null) { |
Sunny Goyal | 71b5c0b | 2015-01-08 16:59:04 -0800 | [diff] [blame] | 2758 | removeWorkspaceItem(mDragInfo.cell); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 2759 | } |
Patrick Dubroy | ce34a97 | 2010-10-19 10:34:32 -0700 | [diff] [blame] | 2760 | } else if (mDragInfo != null) { |
Sunny Goyal | 9282059 | 2015-03-02 11:31:35 -0800 | [diff] [blame] | 2761 | final CellLayout cellLayout = mLauncher.getCellLayout( |
| 2762 | mDragInfo.container, mDragInfo.screenId); |
Jorim Jaggi | eedb00a | 2014-01-13 13:45:07 -0800 | [diff] [blame] | 2763 | if (cellLayout != null) { |
| 2764 | cellLayout.onDropChild(mDragInfo.cell); |
Sunny Goyal | 3d706ad | 2017-03-06 16:56:39 -0800 | [diff] [blame] | 2765 | } else if (FeatureFlags.IS_DOGFOOD_BUILD) { |
Sunny Goyal | 9282059 | 2015-03-02 11:31:35 -0800 | [diff] [blame] | 2766 | throw new RuntimeException("Invalid state: cellLayout == null in " |
| 2767 | + "Workspace#onDropCompleted. Please file a bug. "); |
Rajeev Kumar | 93c0512 | 2017-06-12 12:59:47 -0700 | [diff] [blame] | 2768 | } |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 2769 | } |
Tony | 4d91032 | 2018-03-08 13:43:48 +0000 | [diff] [blame] | 2770 | View cell = getHomescreenIconByItemId(d.originalDragInfo.id); |
| 2771 | if (d.cancelled && cell != null) { |
| 2772 | cell.setVisibility(VISIBLE); |
Adam Cohen | 36cc09b | 2011-09-29 17:33:15 -0700 | [diff] [blame] | 2773 | } |
Tony Wickham | 4feb27e | 2016-10-26 12:51:14 -0700 | [diff] [blame] | 2774 | mDragInfo = null; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 2775 | } |
| 2776 | |
Hyunyoung Song | 31178b8 | 2015-02-24 14:12:51 -0800 | [diff] [blame] | 2777 | /** |
| 2778 | * For opposite operation. See {@link #addInScreen}. |
| 2779 | */ |
Sunny Goyal | 71b5c0b | 2015-01-08 16:59:04 -0800 | [diff] [blame] | 2780 | public void removeWorkspaceItem(View v) { |
| 2781 | CellLayout parentCell = getParentCellLayoutForView(v); |
| 2782 | if (parentCell != null) { |
| 2783 | parentCell.removeView(v); |
Sunny Goyal | 3d706ad | 2017-03-06 16:56:39 -0800 | [diff] [blame] | 2784 | } else if (FeatureFlags.IS_DOGFOOD_BUILD) { |
Sunny Goyal | 912bdfc | 2015-08-03 19:09:55 -0700 | [diff] [blame] | 2785 | // When an app is uninstalled using the drop target, we wait until resume to remove |
| 2786 | // the icon. We also remove all the corresponding items from the workspace at |
| 2787 | // {@link Launcher#bindComponentsRemoved}. That call can come before or after |
| 2788 | // {@link Launcher#mOnResumeCallbacks} depending on how busy the worker thread is. |
| 2789 | Log.e(TAG, "mDragInfo.cell has null parent"); |
Sunny Goyal | 71b5c0b | 2015-01-08 16:59:04 -0800 | [diff] [blame] | 2790 | } |
| 2791 | if (v instanceof DropTarget) { |
| 2792 | mDragController.removeDropTarget((DropTarget) v); |
| 2793 | } |
| 2794 | } |
| 2795 | |
Sunny Goyal | aaf7d1d | 2016-05-17 13:38:54 -0700 | [diff] [blame] | 2796 | /** |
| 2797 | * Removes all folder listeners |
| 2798 | */ |
| 2799 | public void removeFolderListeners() { |
| 2800 | mapOverItems(false, new ItemOperator() { |
| 2801 | @Override |
| 2802 | public boolean evaluate(ItemInfo info, View view) { |
| 2803 | if (view instanceof FolderIcon) { |
| 2804 | ((FolderIcon) view).removeListeners(); |
| 2805 | } |
| 2806 | return false; |
| 2807 | } |
| 2808 | }); |
| 2809 | } |
| 2810 | |
Michael Jurka | 0280c3b | 2010-09-17 15:00:07 -0700 | [diff] [blame] | 2811 | public boolean isDropEnabled() { |
| 2812 | return true; |
| 2813 | } |
| 2814 | |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 2815 | @Override |
Adam Cohen | 1462de3 | 2012-07-24 22:34:36 -0700 | [diff] [blame] | 2816 | protected void dispatchRestoreInstanceState(SparseArray<Parcelable> container) { |
| 2817 | // We don't dispatch restoreInstanceState to our children using this code path. |
Dave Hawkey | a888158 | 2013-09-17 15:55:33 -0600 | [diff] [blame] | 2818 | // Some pages will be restored immediately as their items are bound immediately, and |
Adam Cohen | 1462de3 | 2012-07-24 22:34:36 -0700 | [diff] [blame] | 2819 | // others we will need to wait until after their items are bound. |
| 2820 | mSavedStates = container; |
| 2821 | } |
| 2822 | |
| 2823 | public void restoreInstanceStateForChild(int child) { |
| 2824 | if (mSavedStates != null) { |
| 2825 | mRestoredPages.add(child); |
| 2826 | CellLayout cl = (CellLayout) getChildAt(child); |
Adam Cohen | b0ee081 | 2013-12-03 10:51:45 -0800 | [diff] [blame] | 2827 | if (cl != null) { |
| 2828 | cl.restoreInstanceState(mSavedStates); |
| 2829 | } |
Adam Cohen | 1462de3 | 2012-07-24 22:34:36 -0700 | [diff] [blame] | 2830 | } |
| 2831 | } |
| 2832 | |
| 2833 | public void restoreInstanceStateForRemainingPages() { |
| 2834 | int count = getChildCount(); |
| 2835 | for (int i = 0; i < count; i++) { |
| 2836 | if (!mRestoredPages.contains(i)) { |
| 2837 | restoreInstanceStateForChild(i); |
| 2838 | } |
| 2839 | } |
| 2840 | mRestoredPages.clear(); |
Winson Chung | d8e596d | 2013-10-21 17:14:12 -0700 | [diff] [blame] | 2841 | mSavedStates = null; |
Adam Cohen | 1462de3 | 2012-07-24 22:34:36 -0700 | [diff] [blame] | 2842 | } |
| 2843 | |
| 2844 | @Override |
Vadim Tryshev | 98913d0 | 2018-05-18 18:41:34 -0700 | [diff] [blame] | 2845 | public boolean scrollLeft() { |
| 2846 | boolean result = false; |
Adam Cohen | 6c5891a | 2014-07-09 23:53:15 -0700 | [diff] [blame] | 2847 | if (!workspaceInModalState() && !mIsSwitchingState) { |
Vadim Tryshev | 98913d0 | 2018-05-18 18:41:34 -0700 | [diff] [blame] | 2848 | result = super.scrollLeft(); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 2849 | } |
Sunny Goyal | 740ac7f | 2016-09-28 16:47:32 -0700 | [diff] [blame] | 2850 | Folder openFolder = Folder.getOpen(mLauncher); |
Adam Cohen | 95bb800 | 2011-07-03 23:40:28 -0700 | [diff] [blame] | 2851 | if (openFolder != null) { |
| 2852 | openFolder.completeDragExit(); |
| 2853 | } |
Vadim Tryshev | 98913d0 | 2018-05-18 18:41:34 -0700 | [diff] [blame] | 2854 | return result; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 2855 | } |
| 2856 | |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 2857 | @Override |
Vadim Tryshev | 98913d0 | 2018-05-18 18:41:34 -0700 | [diff] [blame] | 2858 | public boolean scrollRight() { |
| 2859 | boolean result = false; |
Adam Cohen | 6c5891a | 2014-07-09 23:53:15 -0700 | [diff] [blame] | 2860 | if (!workspaceInModalState() && !mIsSwitchingState) { |
Vadim Tryshev | 98913d0 | 2018-05-18 18:41:34 -0700 | [diff] [blame] | 2861 | result = super.scrollRight(); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 2862 | } |
Sunny Goyal | 740ac7f | 2016-09-28 16:47:32 -0700 | [diff] [blame] | 2863 | Folder openFolder = Folder.getOpen(mLauncher); |
Adam Cohen | 95bb800 | 2011-07-03 23:40:28 -0700 | [diff] [blame] | 2864 | if (openFolder != null) { |
| 2865 | openFolder.completeDragExit(); |
| 2866 | } |
Vadim Tryshev | 98913d0 | 2018-05-18 18:41:34 -0700 | [diff] [blame] | 2867 | return result; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 2868 | } |
| 2869 | |
Winson Chung | 3d503fb | 2011-07-13 17:25:49 -0700 | [diff] [blame] | 2870 | /** |
| 2871 | * Returns a specific CellLayout |
| 2872 | */ |
| 2873 | CellLayout getParentCellLayoutForView(View v) { |
Sunny Goyal | 876e462 | 2018-11-02 13:50:40 -0700 | [diff] [blame] | 2874 | for (CellLayout layout : getWorkspaceAndHotseatCellLayouts()) { |
Michael Jurka | a52570f | 2012-03-20 03:18:20 -0700 | [diff] [blame] | 2875 | if (layout.getShortcutsAndWidgets().indexOfChild(v) > -1) { |
Winson Chung | 3d503fb | 2011-07-13 17:25:49 -0700 | [diff] [blame] | 2876 | return layout; |
| 2877 | } |
| 2878 | } |
| 2879 | return null; |
| 2880 | } |
| 2881 | |
| 2882 | /** |
Sunny Goyal | 876e462 | 2018-11-02 13:50:40 -0700 | [diff] [blame] | 2883 | * Returns a list of all the CellLayouts on the Homescreen. |
Winson Chung | 3d503fb | 2011-07-13 17:25:49 -0700 | [diff] [blame] | 2884 | */ |
Sunny Goyal | 876e462 | 2018-11-02 13:50:40 -0700 | [diff] [blame] | 2885 | private CellLayout[] getWorkspaceAndHotseatCellLayouts() { |
Winson Chung | 3d503fb | 2011-07-13 17:25:49 -0700 | [diff] [blame] | 2886 | int screenCount = getChildCount(); |
Sunny Goyal | 876e462 | 2018-11-02 13:50:40 -0700 | [diff] [blame] | 2887 | final CellLayout[] layouts; |
Winson Chung | 3d503fb | 2011-07-13 17:25:49 -0700 | [diff] [blame] | 2888 | if (mLauncher.getHotseat() != null) { |
Sunny Goyal | 876e462 | 2018-11-02 13:50:40 -0700 | [diff] [blame] | 2889 | layouts = new CellLayout[screenCount + 1]; |
| 2890 | layouts[screenCount] = mLauncher.getHotseat(); |
| 2891 | } else { |
| 2892 | layouts = new CellLayout[screenCount]; |
| 2893 | } |
| 2894 | for (int screen = 0; screen < screenCount; screen++) { |
| 2895 | layouts[screen] = (CellLayout) getChildAt(screen); |
Winson Chung | 3d503fb | 2011-07-13 17:25:49 -0700 | [diff] [blame] | 2896 | } |
| 2897 | return layouts; |
| 2898 | } |
| 2899 | |
Jon Miranda | 0128d3f | 2019-02-14 09:19:31 -0800 | [diff] [blame] | 2900 | /** |
Jon Miranda | 0128d3f | 2019-02-14 09:19:31 -0800 | [diff] [blame] | 2901 | * Similar to {@link #getFirstMatch} but optimized to finding a suitable view for the app close |
| 2902 | * animation. |
| 2903 | * |
Jon Miranda | 4334874 | 2019-03-06 10:40:13 -0800 | [diff] [blame] | 2904 | * @param packageName The package name of the app to match. |
| 2905 | * @param user The user of the app to match. |
Jon Miranda | 0128d3f | 2019-02-14 09:19:31 -0800 | [diff] [blame] | 2906 | */ |
Jon Miranda | 4334874 | 2019-03-06 10:40:13 -0800 | [diff] [blame] | 2907 | public View getFirstMatchForAppClose(String packageName, UserHandle user) { |
Jon Miranda | 0128d3f | 2019-02-14 09:19:31 -0800 | [diff] [blame] | 2908 | final int curPage = getCurrentPage(); |
| 2909 | final CellLayout currentPage = (CellLayout) getPageAt(curPage); |
Jon Miranda | 4334874 | 2019-03-06 10:40:13 -0800 | [diff] [blame] | 2910 | final Workspace.ItemOperator packageAndUser = (ItemInfo info, View view) -> info != null |
| 2911 | && info.getTargetComponent() != null |
| 2912 | && TextUtils.equals(info.getTargetComponent().getPackageName(), packageName) |
| 2913 | && info.user.equals(user); |
| 2914 | final Workspace.ItemOperator packageAndUserAndApp = (ItemInfo info, View view) -> |
| 2915 | packageAndUser.evaluate(info, view) && info.itemType == ITEM_TYPE_APPLICATION; |
| 2916 | final Workspace.ItemOperator packageAndUserAndShortcut = (ItemInfo info, View view) -> |
| 2917 | packageAndUser.evaluate(info, view) && (info.itemType == ITEM_TYPE_SHORTCUT |
| 2918 | || info.itemType == ITEM_TYPE_DEEP_SHORTCUT); |
| 2919 | final Workspace.ItemOperator packageAndUserInFolder = (info, view) -> { |
Jon Miranda | 0128d3f | 2019-02-14 09:19:31 -0800 | [diff] [blame] | 2920 | if (info instanceof FolderInfo) { |
| 2921 | FolderInfo folderInfo = (FolderInfo) info; |
| 2922 | for (ShortcutInfo shortcutInfo : folderInfo.contents) { |
Jon Miranda | 4334874 | 2019-03-06 10:40:13 -0800 | [diff] [blame] | 2923 | if (packageAndUser.evaluate(shortcutInfo, view)) { |
Jon Miranda | 0128d3f | 2019-02-14 09:19:31 -0800 | [diff] [blame] | 2924 | return true; |
| 2925 | } |
| 2926 | } |
| 2927 | } |
| 2928 | return false; |
| 2929 | }; |
| 2930 | |
Jon Miranda | 4334874 | 2019-03-06 10:40:13 -0800 | [diff] [blame] | 2931 | // Order: App icons, shortcuts, app/shortcut in folder. Items in hotseat get returned first. |
| 2932 | if (ADAPTIVE_ICON_WINDOW_ANIM.get()) { |
| 2933 | return getFirstMatch(new CellLayout[] { getHotseat(), currentPage }, |
| 2934 | packageAndUserAndApp, packageAndUserAndShortcut, packageAndUserInFolder); |
| 2935 | } else { |
| 2936 | // Do not use Folder as a criteria, since it'll cause a crash when trying to draw |
| 2937 | // FolderAdaptiveIcon as the background. |
| 2938 | return getFirstMatch(new CellLayout[] { getHotseat(), currentPage }, |
| 2939 | packageAndUserAndApp, packageAndUserAndShortcut); |
Jon Miranda | 0128d3f | 2019-02-14 09:19:31 -0800 | [diff] [blame] | 2940 | } |
Jon Miranda | 0128d3f | 2019-02-14 09:19:31 -0800 | [diff] [blame] | 2941 | } |
| 2942 | |
Sunny Goyal | efb7e84 | 2018-10-04 15:11:00 -0700 | [diff] [blame] | 2943 | public View getHomescreenIconByItemId(final int id) { |
Sunny Goyal | 876e462 | 2018-11-02 13:50:40 -0700 | [diff] [blame] | 2944 | return getFirstMatch((info, v) -> info != null && info.id == id); |
Sunny Goyal | 317698b | 2015-07-29 11:45:41 -0700 | [diff] [blame] | 2945 | } |
| 2946 | |
Sunny Goyal | ff57227 | 2014-07-23 13:58:07 -0700 | [diff] [blame] | 2947 | public LauncherAppWidgetHostView getWidgetForAppWidgetId(final int appWidgetId) { |
Sunny Goyal | 876e462 | 2018-11-02 13:50:40 -0700 | [diff] [blame] | 2948 | return (LauncherAppWidgetHostView) getFirstMatch((info, v) -> |
| 2949 | (info instanceof LauncherAppWidgetInfo) && |
| 2950 | ((LauncherAppWidgetInfo) info).appWidgetId == appWidgetId); |
Sunny Goyal | ff57227 | 2014-07-23 13:58:07 -0700 | [diff] [blame] | 2951 | } |
| 2952 | |
Sunny Goyal | 6d02c7a | 2016-05-19 12:15:39 -0700 | [diff] [blame] | 2953 | public View getFirstMatch(final ItemOperator operator) { |
Chris Wren | aeff7ea | 2014-02-14 16:59:24 -0500 | [diff] [blame] | 2954 | final View[] value = new View[1]; |
Sunny Goyal | 651077b | 2014-06-30 14:15:31 -0700 | [diff] [blame] | 2955 | mapOverItems(MAP_NO_RECURSE, new ItemOperator() { |
Chris Wren | aeff7ea | 2014-02-14 16:59:24 -0500 | [diff] [blame] | 2956 | @Override |
Sunny Goyal | c52ba71 | 2016-04-05 15:59:05 -0700 | [diff] [blame] | 2957 | public boolean evaluate(ItemInfo info, View v) { |
| 2958 | if (operator.evaluate(info, v)) { |
Chris Wren | aeff7ea | 2014-02-14 16:59:24 -0500 | [diff] [blame] | 2959 | value[0] = v; |
| 2960 | return true; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 2961 | } |
Chris Wren | aeff7ea | 2014-02-14 16:59:24 -0500 | [diff] [blame] | 2962 | return false; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 2963 | } |
Chris Wren | aeff7ea | 2014-02-14 16:59:24 -0500 | [diff] [blame] | 2964 | }); |
| 2965 | return value[0]; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 2966 | } |
| 2967 | |
Jon Miranda | 4334874 | 2019-03-06 10:40:13 -0800 | [diff] [blame] | 2968 | /** |
| 2969 | * @param cellLayouts List of CellLayouts to scan, in order of preference. |
| 2970 | * @param operators List of operators, in order starting from best matching operator. |
| 2971 | * @return |
| 2972 | */ |
| 2973 | private View getFirstMatch(CellLayout[] cellLayouts, final ItemOperator... operators) { |
| 2974 | // This array is filled with the first match for each operator. |
| 2975 | final View[] matches = new View[operators.length]; |
| 2976 | // For efficiency, the outer loop should be CellLayout. |
Jon Miranda | 0128d3f | 2019-02-14 09:19:31 -0800 | [diff] [blame] | 2977 | for (CellLayout cellLayout : cellLayouts) { |
| 2978 | mapOverCellLayout(MAP_NO_RECURSE, cellLayout, (info, v) -> { |
Jon Miranda | 4334874 | 2019-03-06 10:40:13 -0800 | [diff] [blame] | 2979 | for (int i = 0; i < operators.length; ++i) { |
| 2980 | if (matches[i] == null && operators[i].evaluate(info, v)) { |
| 2981 | matches[i] = v; |
| 2982 | if (i == 0) { |
| 2983 | // We can return since this is the best match possible. |
| 2984 | return true; |
| 2985 | } |
| 2986 | } |
Jon Miranda | 0128d3f | 2019-02-14 09:19:31 -0800 | [diff] [blame] | 2987 | } |
| 2988 | return false; |
| 2989 | }); |
Jon Miranda | 4334874 | 2019-03-06 10:40:13 -0800 | [diff] [blame] | 2990 | if (matches[0] != null) { |
Jon Miranda | 0128d3f | 2019-02-14 09:19:31 -0800 | [diff] [blame] | 2991 | break; |
| 2992 | } |
| 2993 | } |
Jon Miranda | 4334874 | 2019-03-06 10:40:13 -0800 | [diff] [blame] | 2994 | for (View match : matches) { |
| 2995 | if (match != null) { |
| 2996 | return match; |
| 2997 | } |
| 2998 | } |
| 2999 | return null; |
Jon Miranda | 0128d3f | 2019-02-14 09:19:31 -0800 | [diff] [blame] | 3000 | } |
| 3001 | |
Adam Cohen | df03538 | 2011-04-11 17:22:04 -0700 | [diff] [blame] | 3002 | void clearDropTargets() { |
Sunny Goyal | 651077b | 2014-06-30 14:15:31 -0700 | [diff] [blame] | 3003 | mapOverItems(MAP_NO_RECURSE, new ItemOperator() { |
Chris Wren | aeff7ea | 2014-02-14 16:59:24 -0500 | [diff] [blame] | 3004 | @Override |
Sunny Goyal | c52ba71 | 2016-04-05 15:59:05 -0700 | [diff] [blame] | 3005 | public boolean evaluate(ItemInfo info, View v) { |
Adam Cohen | df03538 | 2011-04-11 17:22:04 -0700 | [diff] [blame] | 3006 | if (v instanceof DropTarget) { |
| 3007 | mDragController.removeDropTarget((DropTarget) v); |
| 3008 | } |
Chris Wren | aeff7ea | 2014-02-14 16:59:24 -0500 | [diff] [blame] | 3009 | // not done, process all the shortcuts |
| 3010 | return false; |
Adam Cohen | df03538 | 2011-04-11 17:22:04 -0700 | [diff] [blame] | 3011 | } |
Chris Wren | aeff7ea | 2014-02-14 16:59:24 -0500 | [diff] [blame] | 3012 | }); |
Adam Cohen | df03538 | 2011-04-11 17:22:04 -0700 | [diff] [blame] | 3013 | } |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 3014 | |
Sunny Goyal | 4390ace | 2014-10-13 11:33:11 -0700 | [diff] [blame] | 3015 | /** |
Sunny Goyal | d3b87ef | 2016-07-28 12:11:54 -0700 | [diff] [blame] | 3016 | * Removes items that match the {@param matcher}. When applications are removed |
Sunny Goyal | 4390ace | 2014-10-13 11:33:11 -0700 | [diff] [blame] | 3017 | * as a part of an update, this is called to ensure that other widgets and application |
| 3018 | * shortcuts are not removed. |
| 3019 | */ |
Sunny Goyal | d3b87ef | 2016-07-28 12:11:54 -0700 | [diff] [blame] | 3020 | public void removeItemsByMatcher(final ItemInfoMatcher matcher) { |
Sunny Goyal | 876e462 | 2018-11-02 13:50:40 -0700 | [diff] [blame] | 3021 | for (final CellLayout layoutParent: getWorkspaceAndHotseatCellLayouts()) { |
Michael Jurka | a52570f | 2012-03-20 03:18:20 -0700 | [diff] [blame] | 3022 | final ViewGroup layout = layoutParent.getShortcutsAndWidgets(); |
Romain Guy | 574d20e | 2009-06-01 15:34:04 -0700 | [diff] [blame] | 3023 | |
Sunny Goyal | efb7e84 | 2018-10-04 15:11:00 -0700 | [diff] [blame] | 3024 | IntSparseArrayMap<View> idToViewMap = new IntSparseArrayMap<>(); |
Sunny Goyal | 40452cf | 2016-09-01 15:17:46 -0700 | [diff] [blame] | 3025 | ArrayList<ItemInfo> items = new ArrayList<>(); |
Winson Chung | 64359a5 | 2013-07-08 17:17:08 -0700 | [diff] [blame] | 3026 | for (int j = 0; j < layout.getChildCount(); j++) { |
| 3027 | final View view = layout.getChildAt(j); |
Sunny Goyal | 40452cf | 2016-09-01 15:17:46 -0700 | [diff] [blame] | 3028 | if (view.getTag() instanceof ItemInfo) { |
| 3029 | ItemInfo item = (ItemInfo) view.getTag(); |
| 3030 | items.add(item); |
| 3031 | idToViewMap.put(item.id, view); |
| 3032 | } |
Winson Chung | 64359a5 | 2013-07-08 17:17:08 -0700 | [diff] [blame] | 3033 | } |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 3034 | |
Sunny Goyal | 40452cf | 2016-09-01 15:17:46 -0700 | [diff] [blame] | 3035 | for (ItemInfo itemToRemove : matcher.filterItemInfos(items)) { |
| 3036 | View child = idToViewMap.get(itemToRemove.id); |
| 3037 | |
| 3038 | if (child != null) { |
| 3039 | // Note: We can not remove the view directly from CellLayoutChildren as this |
| 3040 | // does not re-mark the spaces as unoccupied. |
| 3041 | layoutParent.removeViewInLayout(child); |
| 3042 | if (child instanceof DropTarget) { |
| 3043 | mDragController.removeDropTarget((DropTarget) child); |
Romain Guy | 574d20e | 2009-06-01 15:34:04 -0700 | [diff] [blame] | 3044 | } |
Sunny Goyal | 40452cf | 2016-09-01 15:17:46 -0700 | [diff] [blame] | 3045 | } else if (itemToRemove.container >= 0) { |
| 3046 | // The item may belong to a folder. |
| 3047 | View parent = idToViewMap.get(itemToRemove.container); |
| 3048 | if (parent != null) { |
Jon Miranda | 608d0db | 2017-02-28 13:15:28 -0800 | [diff] [blame] | 3049 | FolderInfo folderInfo = (FolderInfo) parent.getTag(); |
| 3050 | folderInfo.prepareAutoUpdate(); |
| 3051 | folderInfo.remove((ShortcutInfo) itemToRemove, false); |
Sunny Goyal | 40452cf | 2016-09-01 15:17:46 -0700 | [diff] [blame] | 3052 | } |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 3053 | } |
Winson Chung | 64359a5 | 2013-07-08 17:17:08 -0700 | [diff] [blame] | 3054 | } |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 3055 | } |
Winson Chung | 2efec4e | 2012-05-03 12:31:48 -0700 | [diff] [blame] | 3056 | |
Winson Chung | 64359a5 | 2013-07-08 17:17:08 -0700 | [diff] [blame] | 3057 | // Strip all the empty screens |
| 3058 | stripEmptyScreens(); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 3059 | } |
The Android Open Source Project | f96811c | 2009-03-18 17:39:48 -0700 | [diff] [blame] | 3060 | |
Adam Cohen | f9c184a | 2016-01-15 16:47:43 -0800 | [diff] [blame] | 3061 | public interface ItemOperator { |
Chris Wren | aeff7ea | 2014-02-14 16:59:24 -0500 | [diff] [blame] | 3062 | /** |
Sunny Goyal | d0a6ae7 | 2016-06-16 12:29:03 -0700 | [diff] [blame] | 3063 | * Process the next itemInfo, possibly with side-effect on the next item. |
Chris Wren | aeff7ea | 2014-02-14 16:59:24 -0500 | [diff] [blame] | 3064 | * |
| 3065 | * @param info info for the shortcut |
| 3066 | * @param view view for the shortcut |
Chris Wren | aeff7ea | 2014-02-14 16:59:24 -0500 | [diff] [blame] | 3067 | * @return true if done, false to continue the map |
| 3068 | */ |
Rajeev Kumar | 93c0512 | 2017-06-12 12:59:47 -0700 | [diff] [blame] | 3069 | boolean evaluate(ItemInfo info, View view); |
Chris Wren | aeff7ea | 2014-02-14 16:59:24 -0500 | [diff] [blame] | 3070 | } |
| 3071 | |
| 3072 | /** |
Sunny Goyal | 651077b | 2014-06-30 14:15:31 -0700 | [diff] [blame] | 3073 | * Map the operator over the shortcuts and widgets, return the first-non-null value. |
Chris Wren | aeff7ea | 2014-02-14 16:59:24 -0500 | [diff] [blame] | 3074 | * |
| 3075 | * @param recurse true: iterate over folder children. false: op get the folders themselves. |
| 3076 | * @param op the operator to map over the shortcuts |
| 3077 | */ |
Sunny Goyal | d0ae492 | 2018-10-11 10:37:53 -0700 | [diff] [blame] | 3078 | public void mapOverItems(boolean recurse, ItemOperator op) { |
Sunny Goyal | 876e462 | 2018-11-02 13:50:40 -0700 | [diff] [blame] | 3079 | for (CellLayout layout : getWorkspaceAndHotseatCellLayouts()) { |
Jon Miranda | 0128d3f | 2019-02-14 09:19:31 -0800 | [diff] [blame] | 3080 | if (mapOverCellLayout(recurse, layout, op)) { |
| 3081 | return; |
| 3082 | } |
| 3083 | } |
| 3084 | } |
| 3085 | |
| 3086 | private boolean mapOverCellLayout(boolean recurse, CellLayout layout, ItemOperator op) { |
Jon Miranda | 5bc0dd0 | 2019-03-12 17:19:30 -0700 | [diff] [blame] | 3087 | // TODO(b/128460496) Potential race condition where layout is not yet loaded |
| 3088 | if (layout == null) { |
| 3089 | return false; |
| 3090 | } |
Jon Miranda | 0128d3f | 2019-02-14 09:19:31 -0800 | [diff] [blame] | 3091 | ShortcutAndWidgetContainer container = layout.getShortcutsAndWidgets(); |
| 3092 | // map over all the shortcuts on the workspace |
| 3093 | final int itemCount = container.getChildCount(); |
| 3094 | for (int itemIdx = 0; itemIdx < itemCount; itemIdx++) { |
| 3095 | View item = container.getChildAt(itemIdx); |
| 3096 | ItemInfo info = (ItemInfo) item.getTag(); |
| 3097 | if (recurse && info instanceof FolderInfo && item instanceof FolderIcon) { |
| 3098 | FolderIcon folder = (FolderIcon) item; |
| 3099 | ArrayList<View> folderChildren = folder.getFolder().getItemsInReadingOrder(); |
| 3100 | // map over all the children in the folder |
| 3101 | final int childCount = folderChildren.size(); |
| 3102 | for (int childIdx = 0; childIdx < childCount; childIdx++) { |
| 3103 | View child = folderChildren.get(childIdx); |
| 3104 | info = (ItemInfo) child.getTag(); |
| 3105 | if (op.evaluate(info, child)) { |
| 3106 | return true; |
Chris Wren | aeff7ea | 2014-02-14 16:59:24 -0500 | [diff] [blame] | 3107 | } |
Jon Miranda | 0128d3f | 2019-02-14 09:19:31 -0800 | [diff] [blame] | 3108 | } |
| 3109 | } else { |
| 3110 | if (op.evaluate(info, item)) { |
| 3111 | return true; |
Chris Wren | aeff7ea | 2014-02-14 16:59:24 -0500 | [diff] [blame] | 3112 | } |
| 3113 | } |
| 3114 | } |
Jon Miranda | 0128d3f | 2019-02-14 09:19:31 -0800 | [diff] [blame] | 3115 | return false; |
Chris Wren | aeff7ea | 2014-02-14 16:59:24 -0500 | [diff] [blame] | 3116 | } |
| 3117 | |
Sunny Goyal | b50cc8c | 2014-10-06 16:23:56 -0700 | [diff] [blame] | 3118 | void updateShortcuts(ArrayList<ShortcutInfo> shortcuts) { |
Sunny Goyal | c52ba71 | 2016-04-05 15:59:05 -0700 | [diff] [blame] | 3119 | int total = shortcuts.size(); |
Rajeev Kumar | 93c0512 | 2017-06-12 12:59:47 -0700 | [diff] [blame] | 3120 | final HashSet<ShortcutInfo> updates = new HashSet<>(total); |
Sunny Goyal | efb7e84 | 2018-10-04 15:11:00 -0700 | [diff] [blame] | 3121 | final IntSet folderIds = new IntSet(); |
Sunny Goyal | c52ba71 | 2016-04-05 15:59:05 -0700 | [diff] [blame] | 3122 | |
| 3123 | for (int i = 0; i < total; i++) { |
| 3124 | ShortcutInfo s = shortcuts.get(i); |
| 3125 | updates.add(s); |
| 3126 | folderIds.add(s.container); |
| 3127 | } |
| 3128 | |
Sunny Goyal | b50cc8c | 2014-10-06 16:23:56 -0700 | [diff] [blame] | 3129 | mapOverItems(MAP_RECURSE, new ItemOperator() { |
| 3130 | @Override |
Sunny Goyal | c52ba71 | 2016-04-05 15:59:05 -0700 | [diff] [blame] | 3131 | public boolean evaluate(ItemInfo info, View v) { |
Sunny Goyal | b50cc8c | 2014-10-06 16:23:56 -0700 | [diff] [blame] | 3132 | if (info instanceof ShortcutInfo && v instanceof BubbleTextView && |
| 3133 | updates.contains(info)) { |
Sunny Goyal | 4390ace | 2014-10-13 11:33:11 -0700 | [diff] [blame] | 3134 | ShortcutInfo si = (ShortcutInfo) info; |
Sunny Goyal | b50cc8c | 2014-10-06 16:23:56 -0700 | [diff] [blame] | 3135 | BubbleTextView shortcut = (BubbleTextView) v; |
Hyunyoung Song | 1322f9c | 2017-06-23 14:57:38 -0700 | [diff] [blame] | 3136 | Drawable oldIcon = shortcut.getIcon(); |
Sunny Goyal | 3fcab66 | 2015-08-11 15:17:30 -0700 | [diff] [blame] | 3137 | boolean oldPromiseState = (oldIcon instanceof PreloadIconDrawable) |
| 3138 | && ((PreloadIconDrawable) oldIcon).hasNotCompleted(); |
Sunny Goyal | 1cd01b0 | 2016-11-09 10:43:58 -0800 | [diff] [blame] | 3139 | shortcut.applyFromShortcutInfo(si, si.isPromise() != oldPromiseState); |
Sunny Goyal | c52ba71 | 2016-04-05 15:59:05 -0700 | [diff] [blame] | 3140 | } |
| 3141 | // process all the shortcuts |
| 3142 | return false; |
| 3143 | } |
| 3144 | }); |
Sunny Goyal | b50cc8c | 2014-10-06 16:23:56 -0700 | [diff] [blame] | 3145 | |
Sunny Goyal | c52ba71 | 2016-04-05 15:59:05 -0700 | [diff] [blame] | 3146 | // Update folder icons |
| 3147 | mapOverItems(MAP_NO_RECURSE, new ItemOperator() { |
| 3148 | @Override |
| 3149 | public boolean evaluate(ItemInfo info, View v) { |
| 3150 | if (info instanceof FolderInfo && folderIds.contains(info.id)) { |
| 3151 | ((FolderInfo) info).itemsChanged(false); |
Sunny Goyal | b50cc8c | 2014-10-06 16:23:56 -0700 | [diff] [blame] | 3152 | } |
| 3153 | // process all the shortcuts |
| 3154 | return false; |
| 3155 | } |
| 3156 | }); |
| 3157 | } |
| 3158 | |
Sunny Goyal | aae6fbb | 2019-01-31 16:05:58 -0800 | [diff] [blame] | 3159 | public void updateNotificationDots(Predicate<PackageUserKey> updatedDots) { |
Tony Wickham | 010d255 | 2017-01-20 08:15:28 -0800 | [diff] [blame] | 3160 | final PackageUserKey packageUserKey = new PackageUserKey(null, null); |
Sunny Goyal | efb7e84 | 2018-10-04 15:11:00 -0700 | [diff] [blame] | 3161 | final IntSet folderIds = new IntSet(); |
Tony Wickham | 010d255 | 2017-01-20 08:15:28 -0800 | [diff] [blame] | 3162 | mapOverItems(MAP_RECURSE, new ItemOperator() { |
| 3163 | @Override |
| 3164 | public boolean evaluate(ItemInfo info, View v) { |
Tony Wickham | 1e58cd7 | 2018-12-04 14:03:32 -0800 | [diff] [blame] | 3165 | if (info instanceof ShortcutInfo && v instanceof BubbleTextView) { |
| 3166 | if (!packageUserKey.updateFromItemInfo(info) |
Sunny Goyal | aae6fbb | 2019-01-31 16:05:58 -0800 | [diff] [blame] | 3167 | || updatedDots.test(packageUserKey)) { |
Tony Wickham | f34bee8 | 2018-12-03 18:11:39 -0800 | [diff] [blame] | 3168 | ((BubbleTextView) v).applyDotState(info, true /* animate */); |
Tony Wickham | 11ba507 | 2017-02-02 12:42:50 -0800 | [diff] [blame] | 3169 | folderIds.add(info.container); |
Tony Wickham | 010d255 | 2017-01-20 08:15:28 -0800 | [diff] [blame] | 3170 | } |
| 3171 | } |
| 3172 | // process all the shortcuts |
| 3173 | return false; |
| 3174 | } |
| 3175 | }); |
Tony Wickham | 11ba507 | 2017-02-02 12:42:50 -0800 | [diff] [blame] | 3176 | |
| 3177 | // Update folder icons |
| 3178 | mapOverItems(MAP_NO_RECURSE, new ItemOperator() { |
| 3179 | @Override |
| 3180 | public boolean evaluate(ItemInfo info, View v) { |
| 3181 | if (info instanceof FolderInfo && folderIds.contains(info.id) |
| 3182 | && v instanceof FolderIcon) { |
Tony Wickham | f34bee8 | 2018-12-03 18:11:39 -0800 | [diff] [blame] | 3183 | FolderDotInfo folderDotInfo = new FolderDotInfo(); |
Tony Wickham | 11ba507 | 2017-02-02 12:42:50 -0800 | [diff] [blame] | 3184 | for (ShortcutInfo si : ((FolderInfo) info).contents) { |
Tony Wickham | f34bee8 | 2018-12-03 18:11:39 -0800 | [diff] [blame] | 3185 | folderDotInfo.addDotInfo(mLauncher.getDotInfoForItem(si)); |
Tony Wickham | 11ba507 | 2017-02-02 12:42:50 -0800 | [diff] [blame] | 3186 | } |
Tony Wickham | f34bee8 | 2018-12-03 18:11:39 -0800 | [diff] [blame] | 3187 | ((FolderIcon) v).setDotInfo(folderDotInfo); |
Tony Wickham | 11ba507 | 2017-02-02 12:42:50 -0800 | [diff] [blame] | 3188 | } |
| 3189 | // process all the shortcuts |
| 3190 | return false; |
| 3191 | } |
| 3192 | }); |
Tony Wickham | 010d255 | 2017-01-20 08:15:28 -0800 | [diff] [blame] | 3193 | } |
| 3194 | |
Sunny Goyal | 7c74e4a | 2016-12-15 15:53:17 -0800 | [diff] [blame] | 3195 | public void removeAbandonedPromise(String packageName, UserHandle user) { |
Sunny Goyal | 3bbbabc | 2016-03-15 09:16:30 -0700 | [diff] [blame] | 3196 | HashSet<String> packages = new HashSet<>(1); |
Sunny Goyal | e7b8cd9 | 2014-08-27 14:04:33 -0700 | [diff] [blame] | 3197 | packages.add(packageName); |
Sunny Goyal | 40452cf | 2016-09-01 15:17:46 -0700 | [diff] [blame] | 3198 | ItemInfoMatcher matcher = ItemInfoMatcher.ofPackages(packages, user); |
Sunny Goyal | 43bf11d | 2017-02-02 13:52:53 -0800 | [diff] [blame] | 3199 | mLauncher.getModelWriter().deleteItemsFromDatabase(matcher); |
Sunny Goyal | 40452cf | 2016-09-01 15:17:46 -0700 | [diff] [blame] | 3200 | removeItemsByMatcher(matcher); |
Chris Wren | 40c5ed3 | 2014-06-24 18:24:23 -0400 | [diff] [blame] | 3201 | } |
| 3202 | |
Sunny Goyal | 756adbc | 2015-04-16 15:20:51 -0700 | [diff] [blame] | 3203 | public void updateRestoreItems(final HashSet<ItemInfo> updates) { |
Sunny Goyal | a22666f | 2014-09-18 13:25:15 -0700 | [diff] [blame] | 3204 | mapOverItems(MAP_RECURSE, new ItemOperator() { |
| 3205 | @Override |
Sunny Goyal | c52ba71 | 2016-04-05 15:59:05 -0700 | [diff] [blame] | 3206 | public boolean evaluate(ItemInfo info, View v) { |
Sunny Goyal | 756adbc | 2015-04-16 15:20:51 -0700 | [diff] [blame] | 3207 | if (info instanceof ShortcutInfo && v instanceof BubbleTextView |
| 3208 | && updates.contains(info)) { |
Tony Wickham | 9a8d11f | 2017-01-11 09:53:12 -0800 | [diff] [blame] | 3209 | ((BubbleTextView) v).applyPromiseState(false /* promiseStateChanged */); |
Sunny Goyal | 756adbc | 2015-04-16 15:20:51 -0700 | [diff] [blame] | 3210 | } else if (v instanceof PendingAppWidgetHostView |
| 3211 | && info instanceof LauncherAppWidgetInfo |
| 3212 | && updates.contains(info)) { |
| 3213 | ((PendingAppWidgetHostView) v).applyState(); |
Sunny Goyal | a22666f | 2014-09-18 13:25:15 -0700 | [diff] [blame] | 3214 | } |
| 3215 | // process all the shortcuts |
| 3216 | return false; |
| 3217 | } |
| 3218 | }); |
| 3219 | } |
| 3220 | |
Sunny Goyal | 87af0fd | 2016-05-16 14:56:02 -0700 | [diff] [blame] | 3221 | public void widgetsRestored(final ArrayList<LauncherAppWidgetInfo> changedInfo) { |
Sunny Goyal | 0fc1be1 | 2014-08-11 17:05:23 -0700 | [diff] [blame] | 3222 | if (!changedInfo.isEmpty()) { |
| 3223 | DeferredWidgetRefresh widgetRefresh = new DeferredWidgetRefresh(changedInfo, |
| 3224 | mLauncher.getAppWidgetHost()); |
Sunny Goyal | 2e1efb4 | 2016-03-03 16:58:55 -0800 | [diff] [blame] | 3225 | |
| 3226 | LauncherAppWidgetInfo item = changedInfo.get(0); |
| 3227 | final AppWidgetProviderInfo widgetInfo; |
| 3228 | if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_NOT_VALID)) { |
| 3229 | widgetInfo = AppWidgetManagerCompat |
| 3230 | .getInstance(mLauncher).findProvider(item.providerName, item.user); |
| 3231 | } else { |
| 3232 | widgetInfo = AppWidgetManagerCompat.getInstance(mLauncher) |
Sunny Goyal | 952e63d | 2017-08-16 04:59:08 -0700 | [diff] [blame] | 3233 | .getLauncherAppWidgetInfo(item.appWidgetId); |
Sunny Goyal | 2e1efb4 | 2016-03-03 16:58:55 -0800 | [diff] [blame] | 3234 | } |
| 3235 | |
| 3236 | if (widgetInfo != null) { |
Sunny Goyal | 0fc1be1 | 2014-08-11 17:05:23 -0700 | [diff] [blame] | 3237 | // Re-inflate the widgets which have changed status |
| 3238 | widgetRefresh.run(); |
| 3239 | } else { |
| 3240 | // widgetRefresh will automatically run when the packages are updated. |
Sunny Goyal | 4390ace | 2014-10-13 11:33:11 -0700 | [diff] [blame] | 3241 | // For now just update the progress bars |
Sunny Goyal | 87af0fd | 2016-05-16 14:56:02 -0700 | [diff] [blame] | 3242 | mapOverItems(MAP_NO_RECURSE, new ItemOperator() { |
| 3243 | @Override |
| 3244 | public boolean evaluate(ItemInfo info, View view) { |
| 3245 | if (view instanceof PendingAppWidgetHostView |
| 3246 | && changedInfo.contains(info)) { |
| 3247 | ((LauncherAppWidgetInfo) info).installProgress = 100; |
| 3248 | ((PendingAppWidgetHostView) view).applyState(); |
| 3249 | } |
| 3250 | // process all the shortcuts |
| 3251 | return false; |
Sunny Goyal | 4390ace | 2014-10-13 11:33:11 -0700 | [diff] [blame] | 3252 | } |
Sunny Goyal | 87af0fd | 2016-05-16 14:56:02 -0700 | [diff] [blame] | 3253 | }); |
Sunny Goyal | 0fc1be1 | 2014-08-11 17:05:23 -0700 | [diff] [blame] | 3254 | } |
Sunny Goyal | ff57227 | 2014-07-23 13:58:07 -0700 | [diff] [blame] | 3255 | } |
The Android Open Source Project | f96811c | 2009-03-18 17:39:48 -0700 | [diff] [blame] | 3256 | } |
| 3257 | |
Sunny Goyal | 7ce471b | 2017-08-02 03:37:39 -0700 | [diff] [blame] | 3258 | void moveToDefaultScreen() { |
| 3259 | int page = DEFAULT_PAGE; |
Sunny Goyal | c210f5b | 2017-07-10 20:46:53 -0700 | [diff] [blame] | 3260 | if (!workspaceInModalState() && getNextPage() != page) { |
Sunny Goyal | 7ce471b | 2017-08-02 03:37:39 -0700 | [diff] [blame] | 3261 | snapToPage(page); |
Joe Onorato | c45b168 | 2010-01-11 18:48:40 -0500 | [diff] [blame] | 3262 | } |
Allan Wojciechowski | af110e8 | 2013-09-12 10:48:23 +0100 | [diff] [blame] | 3263 | View child = getChildAt(page); |
Winson Chung | 64359a5 | 2013-07-08 17:17:08 -0700 | [diff] [blame] | 3264 | if (child != null) { |
| 3265 | child.requestFocus(); |
| 3266 | } |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 3267 | } |
| 3268 | |
Sunny Goyal | d0a6ae7 | 2016-06-16 12:29:03 -0700 | [diff] [blame] | 3269 | @Override |
Sunny Goyal | ce8809a | 2018-01-18 14:02:47 -0800 | [diff] [blame] | 3270 | public int getExpectedHeight() { |
Sunny Goyal | 85a7fe0 | 2018-02-02 10:09:32 -0800 | [diff] [blame] | 3271 | return getMeasuredHeight() <= 0 || !mIsLayoutValid |
Sunny Goyal | ce8809a | 2018-01-18 14:02:47 -0800 | [diff] [blame] | 3272 | ? mLauncher.getDeviceProfile().heightPx : getMeasuredHeight(); |
| 3273 | } |
| 3274 | |
| 3275 | @Override |
| 3276 | public int getExpectedWidth() { |
Sunny Goyal | 85a7fe0 | 2018-02-02 10:09:32 -0800 | [diff] [blame] | 3277 | return getMeasuredWidth() <= 0 || !mIsLayoutValid |
Sunny Goyal | ce8809a | 2018-01-18 14:02:47 -0800 | [diff] [blame] | 3278 | ? mLauncher.getDeviceProfile().widthPx : getMeasuredWidth(); |
| 3279 | } |
| 3280 | |
| 3281 | @Override |
Vadim Tryshev | 2c430b3 | 2018-04-04 14:45:21 -0700 | [diff] [blame] | 3282 | protected boolean canAnnouncePageDescription() { |
| 3283 | // Disable announcements while overscrolling potentially to overlay screen because if we end |
| 3284 | // up on the overlay screen, it will take care of announcing itself. |
| 3285 | return Float.compare(mOverlayTranslation, 0f) == 0; |
| 3286 | } |
| 3287 | |
| 3288 | @Override |
Winson Chung | 6a0f57d | 2011-06-29 20:10:49 -0700 | [diff] [blame] | 3289 | protected String getCurrentPageDescription() { |
Sunny Goyal | 1d08f70 | 2015-05-04 15:50:25 -0700 | [diff] [blame] | 3290 | int page = (mNextPage != INVALID_PAGE) ? mNextPage : mCurrentPage; |
| 3291 | return getPageDescription(page); |
| 3292 | } |
| 3293 | |
| 3294 | private String getPageDescription(int page) { |
Sunny Goyal | 7ce471b | 2017-08-02 03:37:39 -0700 | [diff] [blame] | 3295 | int nScreens = getChildCount(); |
Sunny Goyal | e78e3d7 | 2015-09-24 11:23:31 -0700 | [diff] [blame] | 3296 | int extraScreenId = mScreenOrder.indexOf(EXTRA_EMPTY_SCREEN_ID); |
| 3297 | if (extraScreenId >= 0 && nScreens > 1) { |
| 3298 | if (page == extraScreenId) { |
| 3299 | return getContext().getString(R.string.workspace_new_page); |
| 3300 | } |
| 3301 | nScreens--; |
| 3302 | } |
Sunny Goyal | 5be3e4c | 2016-04-12 16:09:52 -0700 | [diff] [blame] | 3303 | if (nScreens == 0) { |
| 3304 | // When the workspace is not loaded, we do not know how many screen will be bound. |
| 3305 | return getContext().getString(R.string.all_apps_home_button_label); |
| 3306 | } |
Sunny Goyal | 7ce471b | 2017-08-02 03:37:39 -0700 | [diff] [blame] | 3307 | return getContext().getString(R.string.workspace_scroll_format, page + 1, nScreens); |
Winson Chung | 6a0f57d | 2011-06-29 20:10:49 -0700 | [diff] [blame] | 3308 | } |
Adam Cohen | 8dfcba4 | 2011-07-07 16:38:18 -0700 | [diff] [blame] | 3309 | |
Winson Chung | 8f1eff7 | 2015-05-28 17:33:40 -0700 | [diff] [blame] | 3310 | @Override |
Hyunyoung Song | df7ef68 | 2016-10-06 17:52:22 -0700 | [diff] [blame] | 3311 | public void fillInLogContainerData(View v, ItemInfo info, Target target, Target targetParent) { |
Hyunyoung Song | ddec1c7 | 2016-04-12 18:32:04 -0700 | [diff] [blame] | 3312 | target.gridX = info.cellX; |
| 3313 | target.gridY = info.cellY; |
| 3314 | target.pageIndex = getCurrentPage(); |
Sunny Goyal | 6c46a6d | 2016-11-23 02:24:32 +0530 | [diff] [blame] | 3315 | targetParent.containerType = ContainerType.WORKSPACE; |
Hyunyoung Song | 59a2380 | 2016-09-01 12:47:12 -0700 | [diff] [blame] | 3316 | if (info.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) { |
| 3317 | target.rank = info.rank; |
Sunny Goyal | 6c46a6d | 2016-11-23 02:24:32 +0530 | [diff] [blame] | 3318 | targetParent.containerType = ContainerType.HOTSEAT; |
Hyunyoung Song | 59a2380 | 2016-09-01 12:47:12 -0700 | [diff] [blame] | 3319 | } else if (info.container >= 0) { |
Sunny Goyal | 6c46a6d | 2016-11-23 02:24:32 +0530 | [diff] [blame] | 3320 | targetParent.containerType = ContainerType.FOLDER; |
Hyunyoung Song | 59a2380 | 2016-09-01 12:47:12 -0700 | [diff] [blame] | 3321 | } |
Winson Chung | 8f1eff7 | 2015-05-28 17:33:40 -0700 | [diff] [blame] | 3322 | } |
| 3323 | |
Sunny Goyal | 0fc1be1 | 2014-08-11 17:05:23 -0700 | [diff] [blame] | 3324 | /** |
| 3325 | * Used as a workaround to ensure that the AppWidgetService receives the |
| 3326 | * PACKAGE_ADDED broadcast before updating widgets. |
| 3327 | */ |
Sunny Goyal | 64a75aa | 2017-07-03 13:50:52 -0700 | [diff] [blame] | 3328 | private class DeferredWidgetRefresh implements Runnable, ProviderChangedListener { |
Sunny Goyal | 0fc1be1 | 2014-08-11 17:05:23 -0700 | [diff] [blame] | 3329 | private final ArrayList<LauncherAppWidgetInfo> mInfos; |
| 3330 | private final LauncherAppWidgetHost mHost; |
| 3331 | private final Handler mHandler; |
| 3332 | |
| 3333 | private boolean mRefreshPending; |
| 3334 | |
Rajeev Kumar | 11c7bc6 | 2017-06-15 18:18:17 -0700 | [diff] [blame] | 3335 | DeferredWidgetRefresh(ArrayList<LauncherAppWidgetInfo> infos, |
| 3336 | LauncherAppWidgetHost host) { |
Sunny Goyal | 0fc1be1 | 2014-08-11 17:05:23 -0700 | [diff] [blame] | 3337 | mInfos = infos; |
| 3338 | mHost = host; |
Sunny Goyal | 2db5342 | 2019-03-01 16:06:12 -0800 | [diff] [blame] | 3339 | mHandler = mLauncher.mHandler; |
Sunny Goyal | 0fc1be1 | 2014-08-11 17:05:23 -0700 | [diff] [blame] | 3340 | mRefreshPending = true; |
| 3341 | |
| 3342 | mHost.addProviderChangeListener(this); |
| 3343 | // Force refresh after 10 seconds, if we don't get the provider changed event. |
| 3344 | // This could happen when the provider is no longer available in the app. |
Sunny Goyal | 2db5342 | 2019-03-01 16:06:12 -0800 | [diff] [blame] | 3345 | Message msg = Message.obtain(mHandler, this); |
| 3346 | msg.obj = DeferredWidgetRefresh.class; |
| 3347 | mHandler.sendMessageDelayed(msg, 10000); |
Sunny Goyal | 0fc1be1 | 2014-08-11 17:05:23 -0700 | [diff] [blame] | 3348 | } |
| 3349 | |
| 3350 | @Override |
| 3351 | public void run() { |
| 3352 | mHost.removeProviderChangeListener(this); |
| 3353 | mHandler.removeCallbacks(this); |
| 3354 | |
| 3355 | if (!mRefreshPending) { |
| 3356 | return; |
| 3357 | } |
| 3358 | |
| 3359 | mRefreshPending = false; |
| 3360 | |
Sunny Goyal | c7d808a | 2017-12-04 14:50:41 -0800 | [diff] [blame] | 3361 | ArrayList<PendingAppWidgetHostView> views = new ArrayList<>(mInfos.size()); |
| 3362 | mapOverItems(MAP_NO_RECURSE, (info, view) -> { |
| 3363 | if (view instanceof PendingAppWidgetHostView && mInfos.contains(info)) { |
| 3364 | views.add((PendingAppWidgetHostView) view); |
Sunny Goyal | 0fc1be1 | 2014-08-11 17:05:23 -0700 | [diff] [blame] | 3365 | } |
Sunny Goyal | c7d808a | 2017-12-04 14:50:41 -0800 | [diff] [blame] | 3366 | // process all children |
| 3367 | return false; |
Sunny Goyal | 87af0fd | 2016-05-16 14:56:02 -0700 | [diff] [blame] | 3368 | }); |
Sunny Goyal | c7d808a | 2017-12-04 14:50:41 -0800 | [diff] [blame] | 3369 | for (PendingAppWidgetHostView view : views) { |
Sunny Goyal | 29947f0 | 2017-12-18 13:49:44 -0800 | [diff] [blame] | 3370 | view.reInflate(); |
Sunny Goyal | c7d808a | 2017-12-04 14:50:41 -0800 | [diff] [blame] | 3371 | } |
Sunny Goyal | 0fc1be1 | 2014-08-11 17:05:23 -0700 | [diff] [blame] | 3372 | } |
Sunny Goyal | 64a75aa | 2017-07-03 13:50:52 -0700 | [diff] [blame] | 3373 | |
| 3374 | @Override |
| 3375 | public void notifyWidgetProvidersChanged() { |
| 3376 | run(); |
| 3377 | } |
Sunny Goyal | 0fc1be1 | 2014-08-11 17:05:23 -0700 | [diff] [blame] | 3378 | } |
Sunny Goyal | 9443ef5 | 2016-06-07 16:21:03 -0700 | [diff] [blame] | 3379 | |
Sunny Goyal | db36437 | 2016-10-26 19:12:47 -0700 | [diff] [blame] | 3380 | private class StateTransitionListener extends AnimatorListenerAdapter |
| 3381 | implements AnimatorUpdateListener { |
Sunny Goyal | c99cb17 | 2017-10-19 16:15:09 -0700 | [diff] [blame] | 3382 | |
| 3383 | private final LauncherState mToState; |
| 3384 | |
| 3385 | StateTransitionListener(LauncherState toState) { |
| 3386 | mToState = toState; |
| 3387 | } |
| 3388 | |
Sunny Goyal | db36437 | 2016-10-26 19:12:47 -0700 | [diff] [blame] | 3389 | @Override |
| 3390 | public void onAnimationUpdate(ValueAnimator anim) { |
| 3391 | mTransitionProgress = anim.getAnimatedFraction(); |
| 3392 | } |
| 3393 | |
| 3394 | @Override |
| 3395 | public void onAnimationStart(Animator animation) { |
Sunny Goyal | c99cb17 | 2017-10-19 16:15:09 -0700 | [diff] [blame] | 3396 | onStartStateTransition(mToState); |
Sunny Goyal | db36437 | 2016-10-26 19:12:47 -0700 | [diff] [blame] | 3397 | } |
| 3398 | |
| 3399 | @Override |
| 3400 | public void onAnimationEnd(Animator animation) { |
| 3401 | onEndStateTransition(); |
| 3402 | } |
| 3403 | } |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 3404 | } |