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