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 | eaf7a95 | 2020-07-29 16:54:20 -0700 | [diff] [blame] | 19 | import static android.animation.ValueAnimator.areAnimatorsEnabled; |
| 20 | |
Kateryna Ivanova | 7120373 | 2023-05-24 15:09:00 +0000 | [diff] [blame] | 21 | import static com.android.app.animation.Interpolators.DECELERATE_1_5; |
Charlie Anderson | 438d405 | 2023-04-25 14:38:57 -0400 | [diff] [blame] | 22 | import static com.android.launcher3.LauncherState.EDIT_MODE; |
Tony Wickham | 0ac045f | 2021-11-03 13:17:02 -0700 | [diff] [blame] | 23 | import static com.android.launcher3.dragndrop.DraggableView.DRAGGABLE_ICON; |
Tony Wickham | 1278490 | 2021-11-03 14:02:10 -0700 | [diff] [blame] | 24 | import static com.android.launcher3.icons.IconNormalizer.ICON_VISIBLE_AREA_FACTOR; |
Sunny Goyal | 82dfc15 | 2023-02-24 16:50:09 -0800 | [diff] [blame] | 25 | import static com.android.launcher3.util.MultiTranslateDelegate.INDEX_REORDER_BOUNCE_OFFSET; |
| 26 | import static com.android.launcher3.util.MultiTranslateDelegate.INDEX_REORDER_PREVIEW_OFFSET; |
Sunny Goyal | f0b6db7 | 2018-08-13 16:10:14 -0700 | [diff] [blame] | 27 | |
Joe Onorato | 4be866d | 2010-10-10 11:26:02 -0700 | [diff] [blame] | 28 | import android.animation.Animator; |
Michael Jurka | 629758f | 2012-06-14 16:18:21 -0700 | [diff] [blame] | 29 | import android.animation.AnimatorListenerAdapter; |
Sunny Goyal | 849c6a2 | 2018-08-08 16:33:46 -0700 | [diff] [blame] | 30 | import android.animation.ObjectAnimator; |
Chet Haase | 00397b1 | 2010-10-07 11:13:10 -0700 | [diff] [blame] | 31 | import android.animation.TimeInterpolator; |
Patrick Dubroy | de7658b | 2010-09-27 11:15:43 -0700 | [diff] [blame] | 32 | import android.animation.ValueAnimator; |
| 33 | import android.animation.ValueAnimator.AnimatorUpdateListener; |
Sunny Goyal | 726bee7 | 2018-03-05 12:54:24 -0800 | [diff] [blame] | 34 | import android.annotation.SuppressLint; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 35 | import android.content.Context; |
Joe Onorato | 79e5626 | 2009-09-21 15:23:04 -0400 | [diff] [blame] | 36 | import android.content.res.Resources; |
Sunny Goyal | c13403c | 2016-11-18 23:44:48 -0800 | [diff] [blame] | 37 | import android.content.res.TypedArray; |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 38 | import android.graphics.Canvas; |
Andrew Flynn | 0dca1ec | 2012-02-29 13:33:22 -0800 | [diff] [blame] | 39 | import android.graphics.Color; |
Joe Onorato | 4be866d | 2010-10-10 11:26:02 -0700 | [diff] [blame] | 40 | import android.graphics.Paint; |
Patrick Dubroy | de7658b | 2010-09-27 11:15:43 -0700 | [diff] [blame] | 41 | import android.graphics.Point; |
Adam Cohen | d916206 | 2020-03-24 16:35:35 -0700 | [diff] [blame] | 42 | import android.graphics.PointF; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 43 | import android.graphics.Rect; |
Adam Cohen | f7ca3b4 | 2021-02-22 11:03:58 -0800 | [diff] [blame] | 44 | import android.graphics.RectF; |
Patrick Dubroy | 6569f2c | 2010-07-12 14:25:18 -0700 | [diff] [blame] | 45 | import android.graphics.drawable.Drawable; |
Adam Cohen | 1462de3 | 2012-07-24 22:34:36 -0700 | [diff] [blame] | 46 | import android.os.Parcelable; |
Rajeev Kumar | 9962dbe | 2017-06-12 12:16:20 -0700 | [diff] [blame] | 47 | import android.util.ArrayMap; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 48 | import android.util.AttributeSet; |
Adam Cohen | f7ca3b4 | 2021-02-22 11:03:58 -0800 | [diff] [blame] | 49 | import android.util.FloatProperty; |
Joe Onorato | 4be866d | 2010-10-10 11:26:02 -0700 | [diff] [blame] | 50 | import android.util.Log; |
Sunny Goyal | 849c6a2 | 2018-08-08 16:33:46 -0700 | [diff] [blame] | 51 | import android.util.Property; |
Adam Cohen | 1462de3 | 2012-07-24 22:34:36 -0700 | [diff] [blame] | 52 | import android.util.SparseArray; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 53 | import android.view.MotionEvent; |
| 54 | import android.view.View; |
| 55 | import android.view.ViewDebug; |
| 56 | import android.view.ViewGroup; |
Adam Cohen | c9735cf | 2015-01-23 16:11:55 -0800 | [diff] [blame] | 57 | import android.view.accessibility.AccessibilityEvent; |
Sunny Goyal | 5bc6b6f | 2017-10-26 15:36:10 -0700 | [diff] [blame] | 58 | |
vadimt | 04f356f | 2019-02-14 18:46:36 -0800 | [diff] [blame] | 59 | import androidx.annotation.IntDef; |
Adam Cohen | f7ca3b4 | 2021-02-22 11:03:58 -0800 | [diff] [blame] | 60 | import androidx.core.graphics.ColorUtils; |
vadimt | 04f356f | 2019-02-14 18:46:36 -0800 | [diff] [blame] | 61 | import androidx.core.view.ViewCompat; |
| 62 | |
Kateryna Ivanova | 7120373 | 2023-05-24 15:09:00 +0000 | [diff] [blame] | 63 | import com.android.app.animation.Interpolators; |
Sunny Goyal | aa8ef11 | 2015-06-12 20:04:41 -0700 | [diff] [blame] | 64 | import com.android.launcher3.LauncherSettings.Favorites; |
Sunny Goyal | e9b651e | 2015-04-24 11:44:51 -0700 | [diff] [blame] | 65 | import com.android.launcher3.accessibility.DragAndDropAccessibilityDelegate; |
Sebastian Franco | d468299 | 2022-10-05 13:03:09 -0500 | [diff] [blame] | 66 | import com.android.launcher3.celllayout.CellLayoutLayoutParams; |
Sunny Goyal | 669b71f | 2023-01-27 14:37:07 -0800 | [diff] [blame] | 67 | import com.android.launcher3.celllayout.CellPosMapper.CellPos; |
Sebastian Franco | e4c0345 | 2022-12-27 14:50:02 -0600 | [diff] [blame] | 68 | import com.android.launcher3.celllayout.ReorderAlgorithm; |
Sunny Goyal | 3d706ad | 2017-03-06 16:56:39 -0800 | [diff] [blame] | 69 | import com.android.launcher3.config.FeatureFlags; |
Tony Wickham | 0ac045f | 2021-11-03 13:17:02 -0700 | [diff] [blame] | 70 | import com.android.launcher3.dragndrop.DraggableView; |
Jon Miranda | a0233f7 | 2017-06-22 18:34:45 -0700 | [diff] [blame] | 71 | import com.android.launcher3.folder.PreviewBackground; |
Sunny Goyal | e396abf | 2020-04-06 15:11:17 -0700 | [diff] [blame] | 72 | import com.android.launcher3.model.data.ItemInfo; |
Sebastian Franco | f153d91 | 2022-04-22 16:15:27 -0500 | [diff] [blame] | 73 | import com.android.launcher3.model.data.LauncherAppWidgetInfo; |
Sunny Goyal | ff4ba2d | 2016-04-02 14:12:34 -0700 | [diff] [blame] | 74 | import com.android.launcher3.util.CellAndSpan; |
| 75 | import com.android.launcher3.util.GridOccupancy; |
Sunny Goyal | 82dfc15 | 2023-02-24 16:50:09 -0800 | [diff] [blame] | 76 | import com.android.launcher3.util.MultiTranslateDelegate; |
Sunny Goyal | e2fd14b | 2015-08-27 17:45:46 -0700 | [diff] [blame] | 77 | import com.android.launcher3.util.ParcelableSparseArray; |
Sunny Goyal | 9b29ca5 | 2017-02-17 10:39:44 -0800 | [diff] [blame] | 78 | import com.android.launcher3.util.Themes; |
Adam Cohen | 091440a | 2015-03-18 14:16:05 -0700 | [diff] [blame] | 79 | import com.android.launcher3.util.Thunk; |
Sunny Goyal | ab770a1 | 2018-11-14 15:17:26 -0800 | [diff] [blame] | 80 | import com.android.launcher3.views.ActivityContext; |
Steven Ng | 3242720 | 2021-04-19 18:12:12 +0100 | [diff] [blame] | 81 | import com.android.launcher3.widget.LauncherAppWidgetHostView; |
Sunny Goyal | 5bc6b6f | 2017-10-26 15:36:10 -0700 | [diff] [blame] | 82 | |
Sunny Goyal | c13403c | 2016-11-18 23:44:48 -0800 | [diff] [blame] | 83 | import java.lang.annotation.Retention; |
| 84 | import java.lang.annotation.RetentionPolicy; |
Adam Cohen | 69ce2e5 | 2011-07-03 19:25:21 -0700 | [diff] [blame] | 85 | import java.util.ArrayList; |
Adam Cohen | c0dcf59 | 2011-06-01 15:30:43 -0700 | [diff] [blame] | 86 | import java.util.Arrays; |
Adam Cohen | f3900c2 | 2012-11-16 18:28:11 -0800 | [diff] [blame] | 87 | import java.util.Collections; |
| 88 | import java.util.Comparator; |
Sebastian Franco | 45b39b5 | 2023-01-10 10:47:46 -0600 | [diff] [blame] | 89 | import java.util.List; |
Adam Cohen | d41fbf5 | 2012-02-16 23:53:59 -0800 | [diff] [blame] | 90 | import java.util.Stack; |
Adam Cohen | c0dcf59 | 2011-06-01 15:30:43 -0700 | [diff] [blame] | 91 | |
Sunny Goyal | c4d3201 | 2020-04-03 17:10:11 -0700 | [diff] [blame] | 92 | public class CellLayout extends ViewGroup { |
Tony Wickham | a0628cc | 2015-10-14 15:23:04 -0700 | [diff] [blame] | 93 | private static final String TAG = "CellLayout"; |
| 94 | private static final boolean LOGD = false; |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 95 | |
Tony Wickham | ec6fd6f | 2023-03-11 02:08:57 +0000 | [diff] [blame] | 96 | /** The color of the "leave-behind" shape when a folder is opened from Hotseat. */ |
| 97 | private static final int FOLDER_LEAVE_BEHIND_COLOR = Color.argb(160, 245, 245, 245); |
| 98 | |
Sunny Goyal | ab770a1 | 2018-11-14 15:17:26 -0800 | [diff] [blame] | 99 | protected final ActivityContext mActivity; |
Sunny Goyal | 4ffec48 | 2016-02-09 11:28:52 -0800 | [diff] [blame] | 100 | @ViewDebug.ExportedProperty(category = "launcher") |
Adam Cohen | 091440a | 2015-03-18 14:16:05 -0700 | [diff] [blame] | 101 | @Thunk int mCellWidth; |
Sunny Goyal | 4ffec48 | 2016-02-09 11:28:52 -0800 | [diff] [blame] | 102 | @ViewDebug.ExportedProperty(category = "launcher") |
Adam Cohen | 091440a | 2015-03-18 14:16:05 -0700 | [diff] [blame] | 103 | @Thunk int mCellHeight; |
Winson Chung | 11a1a53 | 2013-09-13 11:14:45 -0700 | [diff] [blame] | 104 | private int mFixedCellWidth; |
| 105 | private int mFixedCellHeight; |
Jon Miranda | 228877d | 2021-02-09 11:05:00 -0500 | [diff] [blame] | 106 | @ViewDebug.ExportedProperty(category = "launcher") |
Sebastian Franco | 2542386 | 2023-03-10 10:50:37 -0800 | [diff] [blame] | 107 | protected Point mBorderSpace; |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 108 | |
Sunny Goyal | 4ffec48 | 2016-02-09 11:28:52 -0800 | [diff] [blame] | 109 | @ViewDebug.ExportedProperty(category = "launcher") |
Sebastian Franco | 0958932 | 2022-11-02 15:25:58 -0700 | [diff] [blame] | 110 | protected int mCountX; |
Sunny Goyal | 4ffec48 | 2016-02-09 11:28:52 -0800 | [diff] [blame] | 111 | @ViewDebug.ExportedProperty(category = "launcher") |
Sebastian Franco | 0958932 | 2022-11-02 15:25:58 -0700 | [diff] [blame] | 112 | protected int mCountY; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 113 | |
Adam Cohen | 917e388 | 2013-10-31 15:03:35 -0700 | [diff] [blame] | 114 | private boolean mDropPending = false; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 115 | |
Patrick Dubroy | de7658b | 2010-09-27 11:15:43 -0700 | [diff] [blame] | 116 | // These are temporary variables to prevent having to allocate a new object just to |
| 117 | // return an (x, y) value from helper functions. Do NOT use them to maintain other state. |
Adam Cohen | 091440a | 2015-03-18 14:16:05 -0700 | [diff] [blame] | 118 | @Thunk final int[] mTmpPoint = new int[2]; |
Sunny Goyal | 2805e63 | 2015-05-20 15:35:32 -0700 | [diff] [blame] | 119 | @Thunk final int[] mTempLocation = new int[2]; |
Adam Cohen | d916206 | 2020-03-24 16:35:35 -0700 | [diff] [blame] | 120 | final PointF mTmpPointF = new PointF(); |
Patrick Dubroy | 6569f2c | 2010-07-12 14:25:18 -0700 | [diff] [blame] | 121 | |
Sebastian Franco | 0958932 | 2022-11-02 15:25:58 -0700 | [diff] [blame] | 122 | protected GridOccupancy mOccupied; |
Sebastian Franco | e4c0345 | 2022-12-27 14:50:02 -0600 | [diff] [blame] | 123 | public GridOccupancy mTmpOccupied; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 124 | |
Michael Jurka | dee0589 | 2010-07-27 10:01:56 -0700 | [diff] [blame] | 125 | private OnTouchListener mInterceptTouchListener; |
| 126 | |
Samuel Fufa | 1e2d004 | 2019-11-18 17:12:46 -0800 | [diff] [blame] | 127 | private final ArrayList<DelegatedCellDrawing> mDelegatedCellDrawings = new ArrayList<>(); |
Jon Miranda | a0233f7 | 2017-06-22 18:34:45 -0700 | [diff] [blame] | 128 | final PreviewBackground mFolderLeaveBehind = new PreviewBackground(); |
Adam Cohen | 69ce2e5 | 2011-07-03 19:25:21 -0700 | [diff] [blame] | 129 | |
Sunny Goyal | f5440cb | 2016-12-14 15:13:00 -0800 | [diff] [blame] | 130 | private static final int[] BACKGROUND_STATE_ACTIVE = new int[] { android.R.attr.state_active }; |
Sunny Goyal | e15e2a8 | 2017-12-15 13:05:42 -0800 | [diff] [blame] | 131 | private static final int[] BACKGROUND_STATE_DEFAULT = EMPTY_STATE_SET; |
Sebastian Franco | 0958932 | 2022-11-02 15:25:58 -0700 | [diff] [blame] | 132 | protected final Drawable mBackground; |
Sunny Goyal | 2805e63 | 2015-05-20 15:35:32 -0700 | [diff] [blame] | 133 | |
Adam Cohen | f0f4eda | 2013-06-06 21:27:03 -0700 | [diff] [blame] | 134 | // These values allow a fixed measurement to be set on the CellLayout. |
| 135 | private int mFixedWidth = -1; |
| 136 | private int mFixedHeight = -1; |
| 137 | |
Michael Jurka | 33945b2 | 2010-12-21 18:19:38 -0800 | [diff] [blame] | 138 | // If we're actively dragging something over this screen, mIsDragOverlapping is true |
| 139 | private boolean mIsDragOverlapping = false; |
Patrick Dubroy | 6569f2c | 2010-07-12 14:25:18 -0700 | [diff] [blame] | 140 | |
Winson Chung | 150fbab | 2010-09-29 17:14:26 -0700 | [diff] [blame] | 141 | // These arrays are used to implement the drag visualization on x-large screens. |
Joe Onorato | 4be866d | 2010-10-10 11:26:02 -0700 | [diff] [blame] | 142 | // They are used as circular arrays, indexed by mDragOutlineCurrent. |
Sebastian Franco | d468299 | 2022-10-05 13:03:09 -0500 | [diff] [blame] | 143 | @Thunk final CellLayoutLayoutParams[] mDragOutlines = new CellLayoutLayoutParams[4]; |
Rajeev Kumar | 9962dbe | 2017-06-12 12:16:20 -0700 | [diff] [blame] | 144 | @Thunk final float[] mDragOutlineAlphas = new float[mDragOutlines.length]; |
| 145 | private final InterruptibleInOutAnimator[] mDragOutlineAnims = |
Joe Onorato | 4be866d | 2010-10-10 11:26:02 -0700 | [diff] [blame] | 146 | new InterruptibleInOutAnimator[mDragOutlines.length]; |
Winson Chung | 150fbab | 2010-09-29 17:14:26 -0700 | [diff] [blame] | 147 | |
| 148 | // Used as an index into the above 3 arrays; indicates which is the most current value. |
Joe Onorato | 4be866d | 2010-10-10 11:26:02 -0700 | [diff] [blame] | 149 | private int mDragOutlineCurrent = 0; |
Patrick Dubroy | 8e58e91 | 2010-10-14 13:21:48 -0700 | [diff] [blame] | 150 | private final Paint mDragOutlinePaint = new Paint(); |
Winson Chung | 150fbab | 2010-09-29 17:14:26 -0700 | [diff] [blame] | 151 | |
Sebastian Franco | d468299 | 2022-10-05 13:03:09 -0500 | [diff] [blame] | 152 | @Thunk final ArrayMap<CellLayoutLayoutParams, Animator> mReorderAnimators = new ArrayMap<>(); |
Adam Cohen | d916206 | 2020-03-24 16:35:35 -0700 | [diff] [blame] | 153 | @Thunk final ArrayMap<Reorderable, ReorderPreviewAnimation> mShakeAnimators = new ArrayMap<>(); |
Adam Cohen | 19f3792 | 2012-03-21 11:59:11 -0700 | [diff] [blame] | 154 | |
| 155 | private boolean mItemPlacementDirty = false; |
Adam Cohen | bfbfd26 | 2011-06-13 16:55:12 -0700 | [diff] [blame] | 156 | |
Adam Cohen | f7ca3b4 | 2021-02-22 11:03:58 -0800 | [diff] [blame] | 157 | // Used to visualize the grid and drop locations |
| 158 | private boolean mVisualizeCells = false; |
| 159 | private boolean mVisualizeDropLocation = true; |
| 160 | private RectF mVisualizeGridRect = new RectF(); |
| 161 | private Paint mVisualizeGridPaint = new Paint(); |
Adam Cohen | f7ca3b4 | 2021-02-22 11:03:58 -0800 | [diff] [blame] | 162 | private int mGridVisualizationRoundingRadius; |
| 163 | private float mGridAlpha = 0f; |
| 164 | private int mGridColor = 0; |
Sebastian Franco | 0958932 | 2022-11-02 15:25:58 -0700 | [diff] [blame] | 165 | protected float mSpringLoadedProgress = 0f; |
Adam Cohen | f7ca3b4 | 2021-02-22 11:03:58 -0800 | [diff] [blame] | 166 | private float mScrollProgress = 0f; |
| 167 | |
Patrick Dubroy | 6569f2c | 2010-07-12 14:25:18 -0700 | [diff] [blame] | 168 | // When a drag operation is in progress, holds the nearest cell to the touch point |
| 169 | private final int[] mDragCell = new int[2]; |
Adam Cohen | f7ca3b4 | 2021-02-22 11:03:58 -0800 | [diff] [blame] | 170 | private final int[] mDragCellSpan = new int[2]; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 171 | |
Joe Onorato | 4be866d | 2010-10-10 11:26:02 -0700 | [diff] [blame] | 172 | private boolean mDragging = false; |
| 173 | |
Rajeev Kumar | 9962dbe | 2017-06-12 12:16:20 -0700 | [diff] [blame] | 174 | private final TimeInterpolator mEaseOutInterpolator; |
Sebastian Franco | 0958932 | 2022-11-02 15:25:58 -0700 | [diff] [blame] | 175 | protected final ShortcutAndWidgetContainer mShortcutsAndWidgets; |
Patrick Dubroy | ce34a97 | 2010-10-19 10:34:32 -0700 | [diff] [blame] | 176 | |
Sunny Goyal | c13403c | 2016-11-18 23:44:48 -0800 | [diff] [blame] | 177 | @Retention(RetentionPolicy.SOURCE) |
| 178 | @IntDef({WORKSPACE, HOTSEAT, FOLDER}) |
| 179 | public @interface ContainerType{} |
| 180 | public static final int WORKSPACE = 0; |
| 181 | public static final int HOTSEAT = 1; |
| 182 | public static final int FOLDER = 2; |
| 183 | |
| 184 | @ContainerType private final int mContainerType; |
| 185 | |
Jon Miranda | b28c4fc | 2017-06-20 10:58:36 -0700 | [diff] [blame] | 186 | private final float mChildScale = 1f; |
Andrew Flynn | 0dca1ec | 2012-02-29 13:33:22 -0800 | [diff] [blame] | 187 | |
Adam Cohen | fa3c58f | 2013-12-06 16:10:55 -0800 | [diff] [blame] | 188 | public static final int MODE_SHOW_REORDER_HINT = 0; |
| 189 | public static final int MODE_DRAG_OVER = 1; |
| 190 | public static final int MODE_ON_DROP = 2; |
| 191 | public static final int MODE_ON_DROP_EXTERNAL = 3; |
| 192 | public static final int MODE_ACCEPT_DROP = 4; |
Adam Cohen | 19f3792 | 2012-03-21 11:59:11 -0700 | [diff] [blame] | 193 | private static final boolean DESTRUCTIVE_REORDER = false; |
Adam Cohen | 482ed82 | 2012-03-02 14:15:13 -0800 | [diff] [blame] | 194 | private static final boolean DEBUG_VISUALIZE_OCCUPIED = false; |
| 195 | |
Adam Cohen | fa3c58f | 2013-12-06 16:10:55 -0800 | [diff] [blame] | 196 | private static final float REORDER_PREVIEW_MAGNITUDE = 0.12f; |
Adam Cohen | 19f3792 | 2012-03-21 11:59:11 -0700 | [diff] [blame] | 197 | private static final int REORDER_ANIMATION_DURATION = 150; |
Sunny Goyal | c13403c | 2016-11-18 23:44:48 -0800 | [diff] [blame] | 198 | @Thunk final float mReorderPreviewAnimationMagnitude; |
Adam Cohen | 19f3792 | 2012-03-21 11:59:11 -0700 | [diff] [blame] | 199 | |
Rajeev Kumar | 9962dbe | 2017-06-12 12:16:20 -0700 | [diff] [blame] | 200 | private final ArrayList<View> mIntersectingViews = new ArrayList<>(); |
| 201 | private final Rect mOccupiedRect = new Rect(); |
Sebastian Franco | e4c0345 | 2022-12-27 14:50:02 -0600 | [diff] [blame] | 202 | public final int[] mDirectionVector = new int[2]; |
Steven Ng | 30dd1d6 | 2021-03-15 21:45:49 +0000 | [diff] [blame] | 203 | |
Sebastian Franco | 53a15a4 | 2022-10-25 17:28:54 -0700 | [diff] [blame] | 204 | ItemConfiguration mPreviousSolution = null; |
Adam Cohen | b209e63 | 2012-03-27 17:09:36 -0700 | [diff] [blame] | 205 | private static final int INVALID_DIRECTION = -100; |
Adam Cohen | 482ed82 | 2012-03-02 14:15:13 -0800 | [diff] [blame] | 206 | |
Sunny Goyal | 2805e63 | 2015-05-20 15:35:32 -0700 | [diff] [blame] | 207 | private final Rect mTempRect = new Rect(); |
Jonathan Miranda | 21930da | 2021-05-03 18:44:13 +0000 | [diff] [blame] | 208 | private final RectF mTempRectF = new RectF(); |
Jon Miranda | 88b7f6a | 2021-05-03 16:49:53 -0700 | [diff] [blame] | 209 | private final float[] mTmpFloatArray = new float[4]; |
Winson Chung | 3a6e7f3 | 2013-10-09 15:50:52 -0700 | [diff] [blame] | 210 | |
Sunny Goyal | 73b5a27 | 2019-12-09 14:55:56 -0800 | [diff] [blame] | 211 | private static final Paint sPaint = new Paint(); |
Romain Guy | 8a0bff5 | 2012-05-06 13:14:33 -0700 | [diff] [blame] | 212 | |
Adam Cohen | c9735cf | 2015-01-23 16:11:55 -0800 | [diff] [blame] | 213 | // Related to accessible drag and drop |
Adam Cohen | 6e7c37a | 2020-06-25 19:22:37 -0700 | [diff] [blame] | 214 | DragAndDropAccessibilityDelegate mTouchHelper; |
Adam Cohen | c9735cf | 2015-01-23 16:11:55 -0800 | [diff] [blame] | 215 | |
Adam Cohen | f7ca3b4 | 2021-02-22 11:03:58 -0800 | [diff] [blame] | 216 | |
| 217 | public static final FloatProperty<CellLayout> SPRING_LOADED_PROGRESS = |
| 218 | new FloatProperty<CellLayout>("spring_loaded_progress") { |
| 219 | @Override |
| 220 | public Float get(CellLayout cl) { |
| 221 | return cl.getSpringLoadedProgress(); |
| 222 | } |
| 223 | |
| 224 | @Override |
| 225 | public void setValue(CellLayout cl, float progress) { |
| 226 | cl.setSpringLoadedProgress(progress); |
| 227 | } |
| 228 | }; |
| 229 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 230 | public CellLayout(Context context) { |
| 231 | this(context, null); |
| 232 | } |
| 233 | |
| 234 | public CellLayout(Context context, AttributeSet attrs) { |
| 235 | this(context, attrs, 0); |
| 236 | } |
| 237 | |
| 238 | public CellLayout(Context context, AttributeSet attrs, int defStyle) { |
| 239 | super(context, attrs, defStyle); |
Sunny Goyal | c13403c | 2016-11-18 23:44:48 -0800 | [diff] [blame] | 240 | TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.CellLayout, defStyle, 0); |
| 241 | mContainerType = a.getInteger(R.styleable.CellLayout_containerType, WORKSPACE); |
| 242 | a.recycle(); |
Patrick Dubroy | 6569f2c | 2010-07-12 14:25:18 -0700 | [diff] [blame] | 243 | |
| 244 | // A ViewGroup usually does not draw, but CellLayout needs to draw a rectangle to show |
| 245 | // the user where a dragged item will land when dropped. |
| 246 | setWillNotDraw(false); |
Romain Guy | ce3cbd1 | 2013-02-25 15:00:36 -0800 | [diff] [blame] | 247 | setClipToPadding(false); |
Pat Manning | d0f729d | 2023-01-09 12:04:25 +0000 | [diff] [blame^] | 248 | setClipChildren(false); |
Sunny Goyal | ab770a1 | 2018-11-14 15:17:26 -0800 | [diff] [blame] | 249 | mActivity = ActivityContext.lookupContext(context); |
Steven Ng | cc505b8 | 2021-03-18 23:04:35 +0000 | [diff] [blame] | 250 | DeviceProfile deviceProfile = mActivity.getDeviceProfile(); |
Michael Jurka | a63c452 | 2010-08-19 13:52:27 -0700 | [diff] [blame] | 251 | |
Thales Lima | 8cd020b | 2022-03-15 20:15:14 +0000 | [diff] [blame] | 252 | resetCellSizeInternal(deviceProfile); |
Sunny Goyal | ff4ba2d | 2016-04-02 14:12:34 -0700 | [diff] [blame] | 253 | |
Steven Ng | cc505b8 | 2021-03-18 23:04:35 +0000 | [diff] [blame] | 254 | mCountX = deviceProfile.inv.numColumns; |
| 255 | mCountY = deviceProfile.inv.numRows; |
Sunny Goyal | ff4ba2d | 2016-04-02 14:12:34 -0700 | [diff] [blame] | 256 | mOccupied = new GridOccupancy(mCountX, mCountY); |
| 257 | mTmpOccupied = new GridOccupancy(mCountX, mCountY); |
| 258 | |
Samuel Fufa | 1e2d004 | 2019-11-18 17:12:46 -0800 | [diff] [blame] | 259 | mFolderLeaveBehind.mDelegateCellX = -1; |
| 260 | mFolderLeaveBehind.mDelegateCellY = -1; |
Adam Cohen | efca027 | 2016-02-24 19:19:06 -0800 | [diff] [blame] | 261 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 262 | setAlwaysDrawnWithCacheEnabled(false); |
Patrick Dubroy | de7658b | 2010-09-27 11:15:43 -0700 | [diff] [blame] | 263 | |
Adam Cohen | f7ca3b4 | 2021-02-22 11:03:58 -0800 | [diff] [blame] | 264 | Resources res = getResources(); |
| 265 | |
| 266 | mBackground = getContext().getDrawable(R.drawable.bg_celllayout); |
Sunny Goyal | 2805e63 | 2015-05-20 15:35:32 -0700 | [diff] [blame] | 267 | mBackground.setCallback(this); |
Sunny Goyal | aeb1643 | 2017-10-16 11:46:41 -0700 | [diff] [blame] | 268 | mBackground.setAlpha(0); |
Michael Jurka | 33945b2 | 2010-12-21 18:19:38 -0800 | [diff] [blame] | 269 | |
Yogisha Dixit | c0ac1dd | 2021-05-29 00:26:25 +0100 | [diff] [blame] | 270 | mGridColor = Themes.getAttrColor(getContext(), R.attr.workspaceAccentColor); |
Adam Cohen | f7ca3b4 | 2021-02-22 11:03:58 -0800 | [diff] [blame] | 271 | mGridVisualizationRoundingRadius = |
| 272 | res.getDimensionPixelSize(R.dimen.grid_visualization_rounding_radius); |
Steven Ng | cc505b8 | 2021-03-18 23:04:35 +0000 | [diff] [blame] | 273 | mReorderPreviewAnimationMagnitude = (REORDER_PREVIEW_MAGNITUDE * deviceProfile.iconSizePx); |
Adam Cohen | 19f3792 | 2012-03-21 11:59:11 -0700 | [diff] [blame] | 274 | |
Patrick Dubroy | 046e7eb | 2010-10-06 12:14:43 -0700 | [diff] [blame] | 275 | // Initialize the data structures used for the drag visualization. |
Kateryna Ivanova | 7120373 | 2023-05-24 15:09:00 +0000 | [diff] [blame] | 276 | mEaseOutInterpolator = Interpolators.DECELERATE_QUINT; // Quint ease out |
Winson Chung | b8c69f3 | 2011-10-19 21:36:08 -0700 | [diff] [blame] | 277 | mDragCell[0] = mDragCell[1] = -1; |
Adam Cohen | f7ca3b4 | 2021-02-22 11:03:58 -0800 | [diff] [blame] | 278 | mDragCellSpan[0] = mDragCellSpan[1] = -1; |
Joe Onorato | 4be866d | 2010-10-10 11:26:02 -0700 | [diff] [blame] | 279 | for (int i = 0; i < mDragOutlines.length; i++) { |
Sunny Goyal | 669b71f | 2023-01-27 14:37:07 -0800 | [diff] [blame] | 280 | mDragOutlines[i] = new CellLayoutLayoutParams(0, 0, 0, 0); |
Patrick Dubroy | 046e7eb | 2010-10-06 12:14:43 -0700 | [diff] [blame] | 281 | } |
Mario Bertschler | 54ba601 | 2017-06-08 10:53:53 -0700 | [diff] [blame] | 282 | mDragOutlinePaint.setColor(Themes.getAttrColor(context, R.attr.workspaceTextColor)); |
Patrick Dubroy | 046e7eb | 2010-10-06 12:14:43 -0700 | [diff] [blame] | 283 | |
| 284 | // When dragging things around the home screens, we show a green outline of |
| 285 | // where the item will land. The outlines gradually fade out, leaving a trail |
| 286 | // behind the drag path. |
| 287 | // Set up all the animations that are used to implement this fading. |
| 288 | final int duration = res.getInteger(R.integer.config_dragOutlineFadeTime); |
Chet Haase | 472b281 | 2010-10-14 07:02:04 -0700 | [diff] [blame] | 289 | final float fromAlphaValue = 0; |
| 290 | final float toAlphaValue = (float)res.getInteger(R.integer.config_dragOutlineMaxAlpha); |
Joe Onorato | 4be866d | 2010-10-10 11:26:02 -0700 | [diff] [blame] | 291 | |
Patrick Dubroy | 8e58e91 | 2010-10-14 13:21:48 -0700 | [diff] [blame] | 292 | Arrays.fill(mDragOutlineAlphas, fromAlphaValue); |
Joe Onorato | 4be866d | 2010-10-10 11:26:02 -0700 | [diff] [blame] | 293 | |
| 294 | for (int i = 0; i < mDragOutlineAnims.length; i++) { |
Patrick Dubroy | 046e7eb | 2010-10-06 12:14:43 -0700 | [diff] [blame] | 295 | final InterruptibleInOutAnimator anim = |
Sebastian Franco | f153d91 | 2022-04-22 16:15:27 -0500 | [diff] [blame] | 296 | new InterruptibleInOutAnimator(duration, fromAlphaValue, toAlphaValue); |
Patrick Dubroy | ce34a97 | 2010-10-19 10:34:32 -0700 | [diff] [blame] | 297 | anim.getAnimator().setInterpolator(mEaseOutInterpolator); |
Patrick Dubroy | 046e7eb | 2010-10-06 12:14:43 -0700 | [diff] [blame] | 298 | final int thisIndex = i; |
Chet Haase | 472b281 | 2010-10-14 07:02:04 -0700 | [diff] [blame] | 299 | anim.getAnimator().addUpdateListener(new AnimatorUpdateListener() { |
Patrick Dubroy | de7658b | 2010-09-27 11:15:43 -0700 | [diff] [blame] | 300 | public void onAnimationUpdate(ValueAnimator animation) { |
Joe Onorato | 4be866d | 2010-10-10 11:26:02 -0700 | [diff] [blame] | 301 | // If an animation is started and then stopped very quickly, we can still |
| 302 | // get spurious updates we've cleared the tag. Guard against this. |
Adam Cohen | f7ca3b4 | 2021-02-22 11:03:58 -0800 | [diff] [blame] | 303 | mDragOutlineAlphas[thisIndex] = (Float) animation.getAnimatedValue(); |
| 304 | CellLayout.this.invalidate(); |
Patrick Dubroy | de7658b | 2010-09-27 11:15:43 -0700 | [diff] [blame] | 305 | } |
| 306 | }); |
Joe Onorato | 4be866d | 2010-10-10 11:26:02 -0700 | [diff] [blame] | 307 | // The animation holds a reference to the drag outline bitmap as long is it's |
| 308 | // running. This way the bitmap can be GCed when the animations are complete. |
Joe Onorato | 4be866d | 2010-10-10 11:26:02 -0700 | [diff] [blame] | 309 | mDragOutlineAnims[i] = anim; |
Patrick Dubroy | de7658b | 2010-09-27 11:15:43 -0700 | [diff] [blame] | 310 | } |
Patrick Dubroy | ce34a97 | 2010-10-19 10:34:32 -0700 | [diff] [blame] | 311 | |
Sunny Goyal | c13403c | 2016-11-18 23:44:48 -0800 | [diff] [blame] | 312 | mShortcutsAndWidgets = new ShortcutAndWidgetContainer(context, mContainerType); |
Jon Miranda | 228877d | 2021-02-09 11:05:00 -0500 | [diff] [blame] | 313 | mShortcutsAndWidgets.setCellDimensions(mCellWidth, mCellHeight, mCountX, mCountY, |
Thales Lima | 78d00ad | 2021-09-30 11:29:06 +0100 | [diff] [blame] | 314 | mBorderSpace); |
Michael Jurka | a52570f | 2012-03-20 03:18:20 -0700 | [diff] [blame] | 315 | addView(mShortcutsAndWidgets); |
Michael Jurka | 1801479 | 2010-10-14 09:01:34 -0700 | [diff] [blame] | 316 | } |
| 317 | |
Sunny Goyal | 9b18010 | 2020-03-11 10:02:29 -0700 | [diff] [blame] | 318 | /** |
| 319 | * Sets or clears a delegate used for accessible drag and drop |
| 320 | */ |
| 321 | public void setDragAndDropAccessibilityDelegate(DragAndDropAccessibilityDelegate delegate) { |
| 322 | setOnClickListener(delegate); |
Sunny Goyal | 9b18010 | 2020-03-11 10:02:29 -0700 | [diff] [blame] | 323 | ViewCompat.setAccessibilityDelegate(this, delegate); |
| 324 | |
Adam Cohen | 6e7c37a | 2020-06-25 19:22:37 -0700 | [diff] [blame] | 325 | mTouchHelper = delegate; |
| 326 | int accessibilityFlag = mTouchHelper != null |
Sunny Goyal | 9b18010 | 2020-03-11 10:02:29 -0700 | [diff] [blame] | 327 | ? IMPORTANT_FOR_ACCESSIBILITY_YES : IMPORTANT_FOR_ACCESSIBILITY_NO; |
| 328 | setImportantForAccessibility(accessibilityFlag); |
| 329 | getShortcutsAndWidgets().setImportantForAccessibility(accessibilityFlag); |
Adam Cohen | c9735cf | 2015-01-23 16:11:55 -0800 | [diff] [blame] | 330 | |
Sunny Goyal | 384b578 | 2021-02-09 22:50:02 -0800 | [diff] [blame] | 331 | // ExploreByTouchHelper sets focusability. Clear it when the delegate is cleared. |
| 332 | setFocusable(delegate != null); |
Adam Cohen | c9735cf | 2015-01-23 16:11:55 -0800 | [diff] [blame] | 333 | // Invalidate the accessibility hierarchy |
| 334 | if (getParent() != null) { |
| 335 | getParent().notifySubtreeAccessibilityStateChanged( |
| 336 | this, this, AccessibilityEvent.CONTENT_CHANGE_TYPE_SUBTREE); |
| 337 | } |
| 338 | } |
| 339 | |
Sunny Goyal | a4647b6 | 2021-02-02 13:45:34 -0800 | [diff] [blame] | 340 | /** |
| 341 | * Returns the currently set accessibility delegate |
| 342 | */ |
| 343 | public DragAndDropAccessibilityDelegate getDragAndDropAccessibilityDelegate() { |
| 344 | return mTouchHelper; |
| 345 | } |
| 346 | |
Adam Cohen | c9735cf | 2015-01-23 16:11:55 -0800 | [diff] [blame] | 347 | @Override |
Adam Cohen | 6e7c37a | 2020-06-25 19:22:37 -0700 | [diff] [blame] | 348 | public boolean dispatchHoverEvent(MotionEvent event) { |
| 349 | // Always attempt to dispatch hover events to accessibility first. |
| 350 | if (mTouchHelper != null && mTouchHelper.dispatchHoverEvent(event)) { |
| 351 | return true; |
| 352 | } |
| 353 | return super.dispatchHoverEvent(event); |
| 354 | } |
| 355 | |
| 356 | @Override |
Adam Cohen | c9735cf | 2015-01-23 16:11:55 -0800 | [diff] [blame] | 357 | public boolean onInterceptTouchEvent(MotionEvent ev) { |
Winson Chung | f993518 | 2020-10-23 09:26:44 -0700 | [diff] [blame] | 358 | return mTouchHelper != null |
| 359 | || (mInterceptTouchListener != null && mInterceptTouchListener.onTouch(this, ev)); |
Adam Cohen | c9735cf | 2015-01-23 16:11:55 -0800 | [diff] [blame] | 360 | } |
| 361 | |
Chris Craik | 01f2d7f | 2013-10-01 14:41:56 -0700 | [diff] [blame] | 362 | public void enableHardwareLayer(boolean hasLayer) { |
| 363 | mShortcutsAndWidgets.setLayerType(hasLayer ? LAYER_TYPE_HARDWARE : LAYER_TYPE_NONE, sPaint); |
Michael Jurka | d51f33a | 2012-06-28 15:35:26 -0700 | [diff] [blame] | 364 | } |
| 365 | |
vadimt | 04f356f | 2019-02-14 18:46:36 -0800 | [diff] [blame] | 366 | public boolean isHardwareLayerEnabled() { |
| 367 | return mShortcutsAndWidgets.getLayerType() == LAYER_TYPE_HARDWARE; |
| 368 | } |
| 369 | |
Thales Lima | 8cd020b | 2022-03-15 20:15:14 +0000 | [diff] [blame] | 370 | /** |
| 371 | * Change sizes of cells |
| 372 | * |
| 373 | * @param width the new width of the cells |
| 374 | * @param height the new height of the cells |
| 375 | */ |
Winson Chung | 5f8afe6 | 2013-08-12 16:19:28 -0700 | [diff] [blame] | 376 | public void setCellDimensions(int width, int height) { |
Winson Chung | 11a1a53 | 2013-09-13 11:14:45 -0700 | [diff] [blame] | 377 | mFixedCellWidth = mCellWidth = width; |
| 378 | mFixedCellHeight = mCellHeight = height; |
Jon Miranda | 228877d | 2021-02-09 11:05:00 -0500 | [diff] [blame] | 379 | mShortcutsAndWidgets.setCellDimensions(mCellWidth, mCellHeight, mCountX, mCountY, |
Thales Lima | 78d00ad | 2021-09-30 11:29:06 +0100 | [diff] [blame] | 380 | mBorderSpace); |
Winson Chung | 5f8afe6 | 2013-08-12 16:19:28 -0700 | [diff] [blame] | 381 | } |
| 382 | |
Thales Lima | 8cd020b | 2022-03-15 20:15:14 +0000 | [diff] [blame] | 383 | private void resetCellSizeInternal(DeviceProfile deviceProfile) { |
| 384 | switch (mContainerType) { |
| 385 | case FOLDER: |
Jordan Silva | 637f4eb | 2023-06-13 11:21:53 +0100 | [diff] [blame] | 386 | mBorderSpace = new Point(deviceProfile.folderCellLayoutBorderSpacePx); |
Thales Lima | 8cd020b | 2022-03-15 20:15:14 +0000 | [diff] [blame] | 387 | break; |
| 388 | case HOTSEAT: |
| 389 | mBorderSpace = new Point(deviceProfile.hotseatBorderSpace, |
| 390 | deviceProfile.hotseatBorderSpace); |
| 391 | break; |
| 392 | case WORKSPACE: |
| 393 | default: |
| 394 | mBorderSpace = new Point(deviceProfile.cellLayoutBorderSpacePx); |
| 395 | break; |
| 396 | } |
| 397 | |
| 398 | mCellWidth = mCellHeight = -1; |
| 399 | mFixedCellWidth = mFixedCellHeight = -1; |
| 400 | } |
| 401 | |
| 402 | /** |
| 403 | * Reset the cell sizes and border space |
| 404 | */ |
| 405 | public void resetCellSize(DeviceProfile deviceProfile) { |
| 406 | resetCellSizeInternal(deviceProfile); |
| 407 | requestLayout(); |
| 408 | } |
| 409 | |
Adam Cohen | 2801caf | 2011-05-13 20:57:39 -0700 | [diff] [blame] | 410 | public void setGridSize(int x, int y) { |
| 411 | mCountX = x; |
| 412 | mCountY = y; |
Sunny Goyal | ff4ba2d | 2016-04-02 14:12:34 -0700 | [diff] [blame] | 413 | mOccupied = new GridOccupancy(mCountX, mCountY); |
| 414 | mTmpOccupied = new GridOccupancy(mCountX, mCountY); |
Jon Miranda | 228877d | 2021-02-09 11:05:00 -0500 | [diff] [blame] | 415 | mShortcutsAndWidgets.setCellDimensions(mCellWidth, mCellHeight, mCountX, mCountY, |
Thales Lima | 78d00ad | 2021-09-30 11:29:06 +0100 | [diff] [blame] | 416 | mBorderSpace); |
Adam Cohen | 76fc085 | 2011-06-17 13:26:23 -0700 | [diff] [blame] | 417 | requestLayout(); |
Adam Cohen | 2801caf | 2011-05-13 20:57:39 -0700 | [diff] [blame] | 418 | } |
| 419 | |
Adam Cohen | 2374abf | 2013-04-16 14:56:57 -0700 | [diff] [blame] | 420 | // Set whether or not to invert the layout horizontally if the layout is in RTL mode. |
| 421 | public void setInvertIfRtl(boolean invert) { |
| 422 | mShortcutsAndWidgets.setInvertIfRtl(invert); |
| 423 | } |
| 424 | |
Adam Cohen | 917e388 | 2013-10-31 15:03:35 -0700 | [diff] [blame] | 425 | public void setDropPending(boolean pending) { |
| 426 | mDropPending = pending; |
| 427 | } |
| 428 | |
| 429 | public boolean isDropPending() { |
| 430 | return mDropPending; |
| 431 | } |
| 432 | |
Adam Cohen | c50438c | 2014-08-19 17:43:05 -0700 | [diff] [blame] | 433 | void setIsDragOverlapping(boolean isDragOverlapping) { |
| 434 | if (mIsDragOverlapping != isDragOverlapping) { |
| 435 | mIsDragOverlapping = isDragOverlapping; |
Sunny Goyal | f5440cb | 2016-12-14 15:13:00 -0800 | [diff] [blame] | 436 | mBackground.setState(mIsDragOverlapping |
| 437 | ? BACKGROUND_STATE_ACTIVE : BACKGROUND_STATE_DEFAULT); |
Adam Cohen | c50438c | 2014-08-19 17:43:05 -0700 | [diff] [blame] | 438 | invalidate(); |
| 439 | } |
| 440 | } |
| 441 | |
Sunny Goyal | e2fd14b | 2015-08-27 17:45:46 -0700 | [diff] [blame] | 442 | @Override |
| 443 | protected void dispatchSaveInstanceState(SparseArray<Parcelable> container) { |
Sunny Goyal | 7ce471b | 2017-08-02 03:37:39 -0700 | [diff] [blame] | 444 | ParcelableSparseArray jail = getJailedArray(container); |
| 445 | super.dispatchSaveInstanceState(jail); |
| 446 | container.put(R.id.cell_layout_jail_id, jail); |
Sunny Goyal | e2fd14b | 2015-08-27 17:45:46 -0700 | [diff] [blame] | 447 | } |
| 448 | |
| 449 | @Override |
| 450 | protected void dispatchRestoreInstanceState(SparseArray<Parcelable> container) { |
Sunny Goyal | 7ce471b | 2017-08-02 03:37:39 -0700 | [diff] [blame] | 451 | super.dispatchRestoreInstanceState(getJailedArray(container)); |
Sunny Goyal | e2fd14b | 2015-08-27 17:45:46 -0700 | [diff] [blame] | 452 | } |
| 453 | |
Sunny Goyal | 7ce471b | 2017-08-02 03:37:39 -0700 | [diff] [blame] | 454 | /** |
| 455 | * Wrap the SparseArray in another Parcelable so that the item ids do not conflict with our |
| 456 | * our internal resource ids |
| 457 | */ |
Sunny Goyal | e2fd14b | 2015-08-27 17:45:46 -0700 | [diff] [blame] | 458 | private ParcelableSparseArray getJailedArray(SparseArray<Parcelable> container) { |
| 459 | final Parcelable parcelable = container.get(R.id.cell_layout_jail_id); |
| 460 | return parcelable instanceof ParcelableSparseArray ? |
| 461 | (ParcelableSparseArray) parcelable : new ParcelableSparseArray(); |
| 462 | } |
| 463 | |
Tony Wickham | 0f97b78 | 2015-12-02 17:55:07 -0800 | [diff] [blame] | 464 | public boolean getIsDragOverlapping() { |
| 465 | return mIsDragOverlapping; |
| 466 | } |
| 467 | |
Jeff Sharkey | 83f111d | 2009-04-20 21:03:13 -0700 | [diff] [blame] | 468 | @Override |
Patrick Dubroy | 1262e36 | 2010-10-06 15:49:50 -0700 | [diff] [blame] | 469 | protected void onDraw(Canvas canvas) { |
Michael Jurka | 3e7c763 | 2010-10-02 16:01:03 -0700 | [diff] [blame] | 470 | // When we're large, we are either drawn in a "hover" state (ie when dragging an item to |
| 471 | // a neighboring page) or with just a normal background (if backgroundAlpha > 0.0f) |
| 472 | // When we're small, we are either drawn normally or in the "accepts drops" state (during |
| 473 | // a drag). However, we also drag the mini hover background *over* one of those two |
| 474 | // backgrounds |
Sunny Goyal | aeb1643 | 2017-10-16 11:46:41 -0700 | [diff] [blame] | 475 | if (mBackground.getAlpha() > 0) { |
Sunny Goyal | 2805e63 | 2015-05-20 15:35:32 -0700 | [diff] [blame] | 476 | mBackground.draw(canvas); |
Michael Jurka | a63c452 | 2010-08-19 13:52:27 -0700 | [diff] [blame] | 477 | } |
Romain Guy | a6abce8 | 2009-11-10 02:54:41 -0800 | [diff] [blame] | 478 | |
Adam Cohen | 482ed82 | 2012-03-02 14:15:13 -0800 | [diff] [blame] | 479 | if (DEBUG_VISUALIZE_OCCUPIED) { |
Tony Wickham | 0ac045f | 2021-11-03 13:17:02 -0700 | [diff] [blame] | 480 | Rect cellBounds = new Rect(); |
| 481 | // Will contain the bounds of the cell including spacing between cells. |
| 482 | Rect cellBoundsWithSpacing = new Rect(); |
Tony Wickham | 1278490 | 2021-11-03 14:02:10 -0700 | [diff] [blame] | 483 | int[] targetCell = new int[2]; |
Tony Wickham | 0ac045f | 2021-11-03 13:17:02 -0700 | [diff] [blame] | 484 | int[] cellCenter = new int[2]; |
| 485 | Paint debugPaint = new Paint(); |
| 486 | debugPaint.setStrokeWidth(Utilities.dpToPx(1)); |
| 487 | for (int x = 0; x < mCountX; x++) { |
| 488 | for (int y = 0; y < mCountY; y++) { |
| 489 | if (!mOccupied.cells[x][y]) { |
| 490 | continue; |
Adam Cohen | 482ed82 | 2012-03-02 14:15:13 -0800 | [diff] [blame] | 491 | } |
Tony Wickham | 1278490 | 2021-11-03 14:02:10 -0700 | [diff] [blame] | 492 | targetCell[0] = x; |
| 493 | targetCell[1] = y; |
Tony Wickham | 0ac045f | 2021-11-03 13:17:02 -0700 | [diff] [blame] | 494 | |
Tony Wickham | 1278490 | 2021-11-03 14:02:10 -0700 | [diff] [blame] | 495 | boolean canCreateFolder = canCreateFolder(getChildAt(x, y)); |
Tony Wickham | 0ac045f | 2021-11-03 13:17:02 -0700 | [diff] [blame] | 496 | cellToRect(x, y, 1, 1, cellBounds); |
| 497 | cellBoundsWithSpacing.set(cellBounds); |
| 498 | cellBoundsWithSpacing.inset(-mBorderSpace.x / 2, -mBorderSpace.y / 2); |
Tony Wickham | 3cfa5ed | 2021-11-03 13:20:43 -0700 | [diff] [blame] | 499 | getWorkspaceCellVisualCenter(x, y, cellCenter); |
Tony Wickham | 0ac045f | 2021-11-03 13:17:02 -0700 | [diff] [blame] | 500 | |
| 501 | canvas.save(); |
| 502 | canvas.clipRect(cellBoundsWithSpacing); |
| 503 | |
| 504 | // Draw reorder drag target. |
| 505 | debugPaint.setColor(Color.RED); |
Sebastian Franco | 6e1024e | 2022-07-29 13:46:49 -0700 | [diff] [blame] | 506 | canvas.drawCircle(cellCenter[0], cellCenter[1], |
| 507 | getReorderRadius(targetCell, 1, 1), debugPaint); |
Tony Wickham | 0ac045f | 2021-11-03 13:17:02 -0700 | [diff] [blame] | 508 | |
| 509 | // Draw folder creation drag target. |
| 510 | if (canCreateFolder) { |
| 511 | debugPaint.setColor(Color.GREEN); |
| 512 | canvas.drawCircle(cellCenter[0], cellCenter[1], |
Tony Wickham | 1278490 | 2021-11-03 14:02:10 -0700 | [diff] [blame] | 513 | getFolderCreationRadius(targetCell), debugPaint); |
Tony Wickham | 0ac045f | 2021-11-03 13:17:02 -0700 | [diff] [blame] | 514 | } |
| 515 | |
| 516 | canvas.restore(); |
Adam Cohen | 482ed82 | 2012-03-02 14:15:13 -0800 | [diff] [blame] | 517 | } |
| 518 | } |
| 519 | } |
| 520 | |
Samuel Fufa | 1e2d004 | 2019-11-18 17:12:46 -0800 | [diff] [blame] | 521 | for (int i = 0; i < mDelegatedCellDrawings.size(); i++) { |
| 522 | DelegatedCellDrawing cellDrawing = mDelegatedCellDrawings.get(i); |
| 523 | cellToPoint(cellDrawing.mDelegateCellX, cellDrawing.mDelegateCellY, mTempLocation); |
Adam Cohen | efca027 | 2016-02-24 19:19:06 -0800 | [diff] [blame] | 524 | canvas.save(); |
| 525 | canvas.translate(mTempLocation[0], mTempLocation[1]); |
Samuel Fufa | 1e2d004 | 2019-11-18 17:12:46 -0800 | [diff] [blame] | 526 | cellDrawing.drawUnderItem(canvas); |
Adam Cohen | efca027 | 2016-02-24 19:19:06 -0800 | [diff] [blame] | 527 | canvas.restore(); |
Adam Cohen | 69ce2e5 | 2011-07-03 19:25:21 -0700 | [diff] [blame] | 528 | } |
Adam Cohen | c51934b | 2011-07-26 21:07:43 -0700 | [diff] [blame] | 529 | |
Samuel Fufa | 1e2d004 | 2019-11-18 17:12:46 -0800 | [diff] [blame] | 530 | if (mFolderLeaveBehind.mDelegateCellX >= 0 && mFolderLeaveBehind.mDelegateCellY >= 0) { |
| 531 | cellToPoint(mFolderLeaveBehind.mDelegateCellX, |
| 532 | mFolderLeaveBehind.mDelegateCellY, mTempLocation); |
Adam Cohen | efca027 | 2016-02-24 19:19:06 -0800 | [diff] [blame] | 533 | canvas.save(); |
| 534 | canvas.translate(mTempLocation[0], mTempLocation[1]); |
Tony Wickham | ec6fd6f | 2023-03-11 02:08:57 +0000 | [diff] [blame] | 535 | mFolderLeaveBehind.drawLeaveBehind(canvas, FOLDER_LEAVE_BEHIND_COLOR); |
Adam Cohen | efca027 | 2016-02-24 19:19:06 -0800 | [diff] [blame] | 536 | canvas.restore(); |
Adam Cohen | c51934b | 2011-07-26 21:07:43 -0700 | [diff] [blame] | 537 | } |
Adam Cohen | 6508699 | 2020-02-19 08:40:49 -0800 | [diff] [blame] | 538 | |
Adam Cohen | f7ca3b4 | 2021-02-22 11:03:58 -0800 | [diff] [blame] | 539 | if (mVisualizeCells || mVisualizeDropLocation) { |
Adam Cohen | 6508699 | 2020-02-19 08:40:49 -0800 | [diff] [blame] | 540 | visualizeGrid(canvas); |
| 541 | } |
| 542 | } |
| 543 | |
Adam Cohen | f7ca3b4 | 2021-02-22 11:03:58 -0800 | [diff] [blame] | 544 | /** |
Tony Wickham | 1278490 | 2021-11-03 14:02:10 -0700 | [diff] [blame] | 545 | * Returns whether dropping an icon on the given View can create (or add to) a folder. |
| 546 | */ |
| 547 | private boolean canCreateFolder(View child) { |
| 548 | return child instanceof DraggableView |
| 549 | && ((DraggableView) child).getViewType() == DRAGGABLE_ICON; |
| 550 | } |
| 551 | |
| 552 | /** |
Adam Cohen | f7ca3b4 | 2021-02-22 11:03:58 -0800 | [diff] [blame] | 553 | * Indicates the progress of the Workspace entering the SpringLoaded state; allows the |
| 554 | * CellLayout to update various visuals for this state. |
| 555 | * |
| 556 | * @param progress |
| 557 | */ |
| 558 | public void setSpringLoadedProgress(float progress) { |
| 559 | if (Float.compare(progress, mSpringLoadedProgress) != 0) { |
| 560 | mSpringLoadedProgress = progress; |
fbaron | e74256b | 2023-04-10 14:50:31 -0700 | [diff] [blame] | 561 | updateBgAlpha(); |
Adam Cohen | f7ca3b4 | 2021-02-22 11:03:58 -0800 | [diff] [blame] | 562 | setGridAlpha(progress); |
| 563 | } |
| 564 | } |
| 565 | |
| 566 | /** |
| 567 | * See setSpringLoadedProgress |
| 568 | * @return progress |
| 569 | */ |
| 570 | public float getSpringLoadedProgress() { |
| 571 | return mSpringLoadedProgress; |
| 572 | } |
| 573 | |
Sebastian Franco | 0958932 | 2022-11-02 15:25:58 -0700 | [diff] [blame] | 574 | protected void updateBgAlpha() { |
Charlie Anderson | 438d405 | 2023-04-25 14:38:57 -0400 | [diff] [blame] | 575 | if (!getWorkspace().mLauncher.isInState(EDIT_MODE)) { |
| 576 | mBackground.setAlpha((int) (mSpringLoadedProgress * 255)); |
| 577 | } |
Adam Cohen | f7ca3b4 | 2021-02-22 11:03:58 -0800 | [diff] [blame] | 578 | } |
| 579 | |
| 580 | /** |
| 581 | * Set the progress of this page's scroll |
| 582 | * |
| 583 | * @param progress 0 if the screen is centered, +/-1 if it is to the right / left respectively |
| 584 | */ |
| 585 | public void setScrollProgress(float progress) { |
| 586 | if (Float.compare(Math.abs(progress), mScrollProgress) != 0) { |
| 587 | mScrollProgress = Math.abs(progress); |
fbaron | e74256b | 2023-04-10 14:50:31 -0700 | [diff] [blame] | 588 | updateBgAlpha(); |
Adam Cohen | f7ca3b4 | 2021-02-22 11:03:58 -0800 | [diff] [blame] | 589 | } |
| 590 | } |
| 591 | |
| 592 | private void setGridAlpha(float gridAlpha) { |
| 593 | if (Float.compare(gridAlpha, mGridAlpha) != 0) { |
| 594 | mGridAlpha = gridAlpha; |
| 595 | invalidate(); |
| 596 | } |
| 597 | } |
| 598 | |
Adam Cohen | 6508699 | 2020-02-19 08:40:49 -0800 | [diff] [blame] | 599 | protected void visualizeGrid(Canvas canvas) { |
Adam Cohen | 0c4d278 | 2021-04-29 15:56:13 -0700 | [diff] [blame] | 600 | DeviceProfile dp = mActivity.getDeviceProfile(); |
Alex Chau | 51da219 | 2022-05-20 13:32:10 +0100 | [diff] [blame] | 601 | int paddingX = Math.min((mCellWidth - dp.iconSizePx) / 2, dp.gridVisualizationPaddingX); |
| 602 | int paddingY = Math.min((mCellHeight - dp.iconSizePx) / 2, dp.gridVisualizationPaddingY); |
Adam Cohen | 0c4d278 | 2021-04-29 15:56:13 -0700 | [diff] [blame] | 603 | mVisualizeGridRect.set(paddingX, paddingY, |
| 604 | mCellWidth - paddingX, |
| 605 | mCellHeight - paddingY); |
Adam Cohen | 6508699 | 2020-02-19 08:40:49 -0800 | [diff] [blame] | 606 | |
Adam Cohen | f7ca3b4 | 2021-02-22 11:03:58 -0800 | [diff] [blame] | 607 | mVisualizeGridPaint.setStrokeWidth(8); |
| 608 | int paintAlpha = (int) (120 * mGridAlpha); |
| 609 | mVisualizeGridPaint.setColor(ColorUtils.setAlphaComponent(mGridColor, paintAlpha)); |
Adam Cohen | 6508699 | 2020-02-19 08:40:49 -0800 | [diff] [blame] | 610 | |
Adam Cohen | f7ca3b4 | 2021-02-22 11:03:58 -0800 | [diff] [blame] | 611 | if (mVisualizeCells) { |
| 612 | for (int i = 0; i < mCountX; i++) { |
| 613 | for (int j = 0; j < mCountY; j++) { |
Thales Lima | 78d00ad | 2021-09-30 11:29:06 +0100 | [diff] [blame] | 614 | int transX = i * mCellWidth + (i * mBorderSpace.x) + getPaddingLeft() |
Adam Cohen | 0c4d278 | 2021-04-29 15:56:13 -0700 | [diff] [blame] | 615 | + paddingX; |
Thales Lima | 78d00ad | 2021-09-30 11:29:06 +0100 | [diff] [blame] | 616 | int transY = j * mCellHeight + (j * mBorderSpace.y) + getPaddingTop() |
Adam Cohen | 0c4d278 | 2021-04-29 15:56:13 -0700 | [diff] [blame] | 617 | + paddingY; |
Adam Cohen | 6508699 | 2020-02-19 08:40:49 -0800 | [diff] [blame] | 618 | |
Adam Cohen | f7ca3b4 | 2021-02-22 11:03:58 -0800 | [diff] [blame] | 619 | mVisualizeGridRect.offsetTo(transX, transY); |
| 620 | mVisualizeGridPaint.setStyle(Paint.Style.FILL); |
| 621 | canvas.drawRoundRect(mVisualizeGridRect, mGridVisualizationRoundingRadius, |
| 622 | mGridVisualizationRoundingRadius, mVisualizeGridPaint); |
| 623 | } |
| 624 | } |
| 625 | } |
Adam Cohen | 6508699 | 2020-02-19 08:40:49 -0800 | [diff] [blame] | 626 | |
fbaron | e74256b | 2023-04-10 14:50:31 -0700 | [diff] [blame] | 627 | if (mVisualizeDropLocation) { |
Adam Cohen | f7ca3b4 | 2021-02-22 11:03:58 -0800 | [diff] [blame] | 628 | for (int i = 0; i < mDragOutlines.length; i++) { |
| 629 | final float alpha = mDragOutlineAlphas[i]; |
| 630 | if (alpha <= 0) continue; |
Adam Cohen | 6508699 | 2020-02-19 08:40:49 -0800 | [diff] [blame] | 631 | |
Adam Cohen | f7ca3b4 | 2021-02-22 11:03:58 -0800 | [diff] [blame] | 632 | mVisualizeGridPaint.setAlpha(255); |
Sebastian Franco | 877088e | 2023-01-03 15:16:22 -0700 | [diff] [blame] | 633 | int x = mDragOutlines[i].getCellX(); |
| 634 | int y = mDragOutlines[i].getCellY(); |
Adam Cohen | f7ca3b4 | 2021-02-22 11:03:58 -0800 | [diff] [blame] | 635 | int spanX = mDragOutlines[i].cellHSpan; |
| 636 | int spanY = mDragOutlines[i].cellVSpan; |
| 637 | |
Adam Cohen | ed82e0d | 2021-07-21 17:24:12 -0700 | [diff] [blame] | 638 | // TODO b/194414754 clean this up, reconcile with cellToRect |
Adam Cohen | 0c4d278 | 2021-04-29 15:56:13 -0700 | [diff] [blame] | 639 | mVisualizeGridRect.set(paddingX, paddingY, |
Thales Lima | 78d00ad | 2021-09-30 11:29:06 +0100 | [diff] [blame] | 640 | mCellWidth * spanX + mBorderSpace.x * (spanX - 1) - paddingX, |
| 641 | mCellHeight * spanY + mBorderSpace.y * (spanY - 1) - paddingY); |
Adam Cohen | f7ca3b4 | 2021-02-22 11:03:58 -0800 | [diff] [blame] | 642 | |
Thales Lima | 78d00ad | 2021-09-30 11:29:06 +0100 | [diff] [blame] | 643 | int transX = x * mCellWidth + (x * mBorderSpace.x) |
Adam Cohen | 0c4d278 | 2021-04-29 15:56:13 -0700 | [diff] [blame] | 644 | + getPaddingLeft() + paddingX; |
Thales Lima | 78d00ad | 2021-09-30 11:29:06 +0100 | [diff] [blame] | 645 | int transY = y * mCellHeight + (y * mBorderSpace.y) |
Adam Cohen | 0c4d278 | 2021-04-29 15:56:13 -0700 | [diff] [blame] | 646 | + getPaddingTop() + paddingY; |
Adam Cohen | f7ca3b4 | 2021-02-22 11:03:58 -0800 | [diff] [blame] | 647 | |
| 648 | mVisualizeGridRect.offsetTo(transX, transY); |
Adam Cohen | 6508699 | 2020-02-19 08:40:49 -0800 | [diff] [blame] | 649 | |
| 650 | mVisualizeGridPaint.setStyle(Paint.Style.STROKE); |
Adam Cohen | f7ca3b4 | 2021-02-22 11:03:58 -0800 | [diff] [blame] | 651 | mVisualizeGridPaint.setColor(Color.argb((int) (alpha), |
| 652 | Color.red(mGridColor), Color.green(mGridColor), Color.blue(mGridColor))); |
Adam Cohen | 6508699 | 2020-02-19 08:40:49 -0800 | [diff] [blame] | 653 | |
Adam Cohen | f7ca3b4 | 2021-02-22 11:03:58 -0800 | [diff] [blame] | 654 | canvas.drawRoundRect(mVisualizeGridRect, mGridVisualizationRoundingRadius, |
| 655 | mGridVisualizationRoundingRadius, mVisualizeGridPaint); |
Adam Cohen | 6508699 | 2020-02-19 08:40:49 -0800 | [diff] [blame] | 656 | } |
| 657 | } |
Adam Cohen | 69ce2e5 | 2011-07-03 19:25:21 -0700 | [diff] [blame] | 658 | } |
| 659 | |
Adam Cohen | efca027 | 2016-02-24 19:19:06 -0800 | [diff] [blame] | 660 | @Override |
| 661 | protected void dispatchDraw(Canvas canvas) { |
| 662 | super.dispatchDraw(canvas); |
| 663 | |
Samuel Fufa | 1e2d004 | 2019-11-18 17:12:46 -0800 | [diff] [blame] | 664 | for (int i = 0; i < mDelegatedCellDrawings.size(); i++) { |
| 665 | DelegatedCellDrawing bg = mDelegatedCellDrawings.get(i); |
| 666 | cellToPoint(bg.mDelegateCellX, bg.mDelegateCellY, mTempLocation); |
| 667 | canvas.save(); |
| 668 | canvas.translate(mTempLocation[0], mTempLocation[1]); |
| 669 | bg.drawOverItem(canvas); |
| 670 | canvas.restore(); |
Adam Cohen | efca027 | 2016-02-24 19:19:06 -0800 | [diff] [blame] | 671 | } |
Adam Cohen | 69ce2e5 | 2011-07-03 19:25:21 -0700 | [diff] [blame] | 672 | } |
| 673 | |
Samuel Fufa | 1e2d004 | 2019-11-18 17:12:46 -0800 | [diff] [blame] | 674 | /** |
| 675 | * Add Delegated cell drawing |
| 676 | */ |
| 677 | public void addDelegatedCellDrawing(DelegatedCellDrawing bg) { |
| 678 | mDelegatedCellDrawings.add(bg); |
Adam Cohen | efca027 | 2016-02-24 19:19:06 -0800 | [diff] [blame] | 679 | } |
Samuel Fufa | 1e2d004 | 2019-11-18 17:12:46 -0800 | [diff] [blame] | 680 | |
| 681 | /** |
| 682 | * Remove item from DelegatedCellDrawings |
| 683 | */ |
| 684 | public void removeDelegatedCellDrawing(DelegatedCellDrawing bg) { |
| 685 | mDelegatedCellDrawings.remove(bg); |
Patrick Dubroy | 6569f2c | 2010-07-12 14:25:18 -0700 | [diff] [blame] | 686 | } |
| 687 | |
Adam Cohen | c51934b | 2011-07-26 21:07:43 -0700 | [diff] [blame] | 688 | public void setFolderLeaveBehindCell(int x, int y) { |
Adam Cohen | efca027 | 2016-02-24 19:19:06 -0800 | [diff] [blame] | 689 | View child = getChildAt(x, y); |
Sunny Goyal | ab770a1 | 2018-11-14 15:17:26 -0800 | [diff] [blame] | 690 | mFolderLeaveBehind.setup(getContext(), mActivity, null, |
Adam Cohen | efca027 | 2016-02-24 19:19:06 -0800 | [diff] [blame] | 691 | child.getMeasuredWidth(), child.getPaddingTop()); |
| 692 | |
Samuel Fufa | 1e2d004 | 2019-11-18 17:12:46 -0800 | [diff] [blame] | 693 | mFolderLeaveBehind.mDelegateCellX = x; |
| 694 | mFolderLeaveBehind.mDelegateCellY = y; |
Adam Cohen | c51934b | 2011-07-26 21:07:43 -0700 | [diff] [blame] | 695 | invalidate(); |
| 696 | } |
| 697 | |
| 698 | public void clearFolderLeaveBehind() { |
Samuel Fufa | 1e2d004 | 2019-11-18 17:12:46 -0800 | [diff] [blame] | 699 | mFolderLeaveBehind.mDelegateCellX = -1; |
| 700 | mFolderLeaveBehind.mDelegateCellY = -1; |
Adam Cohen | c51934b | 2011-07-26 21:07:43 -0700 | [diff] [blame] | 701 | invalidate(); |
| 702 | } |
| 703 | |
Patrick Dubroy | 6569f2c | 2010-07-12 14:25:18 -0700 | [diff] [blame] | 704 | @Override |
Michael Jurka | e6235dd | 2011-10-04 15:02:05 -0700 | [diff] [blame] | 705 | public boolean shouldDelayChildPressedState() { |
| 706 | return false; |
| 707 | } |
| 708 | |
Adam Cohen | 1462de3 | 2012-07-24 22:34:36 -0700 | [diff] [blame] | 709 | public void restoreInstanceState(SparseArray<Parcelable> states) { |
Sunny Goyal | 33a152f | 2014-07-22 12:13:14 -0700 | [diff] [blame] | 710 | try { |
| 711 | dispatchRestoreInstanceState(states); |
| 712 | } catch (IllegalArgumentException ex) { |
Zak Cohen | 3eeb41d | 2020-02-14 14:15:13 -0800 | [diff] [blame] | 713 | if (FeatureFlags.IS_STUDIO_BUILD) { |
Sunny Goyal | 33a152f | 2014-07-22 12:13:14 -0700 | [diff] [blame] | 714 | throw ex; |
| 715 | } |
| 716 | // Mismatched viewId / viewType preventing restore. Skip restore on production builds. |
| 717 | Log.e(TAG, "Ignoring an error while restoring a view instance state", ex); |
| 718 | } |
Adam Cohen | 1462de3 | 2012-07-24 22:34:36 -0700 | [diff] [blame] | 719 | } |
| 720 | |
Michael Jurka | e6235dd | 2011-10-04 15:02:05 -0700 | [diff] [blame] | 721 | @Override |
Jeff Sharkey | 83f111d | 2009-04-20 21:03:13 -0700 | [diff] [blame] | 722 | public void cancelLongPress() { |
| 723 | super.cancelLongPress(); |
| 724 | |
| 725 | // Cancel long press for all children |
| 726 | final int count = getChildCount(); |
| 727 | for (int i = 0; i < count; i++) { |
| 728 | final View child = getChildAt(i); |
| 729 | child.cancelLongPress(); |
| 730 | } |
| 731 | } |
| 732 | |
Michael Jurka | dee0589 | 2010-07-27 10:01:56 -0700 | [diff] [blame] | 733 | public void setOnInterceptTouchListener(View.OnTouchListener listener) { |
| 734 | mInterceptTouchListener = listener; |
| 735 | } |
| 736 | |
Hyunyoung Song | ee3e6a7 | 2015-02-20 14:25:27 -0800 | [diff] [blame] | 737 | public int getCountX() { |
Adam Cohen | d22015c | 2010-07-26 22:02:18 -0700 | [diff] [blame] | 738 | return mCountX; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 739 | } |
| 740 | |
Hyunyoung Song | ee3e6a7 | 2015-02-20 14:25:27 -0800 | [diff] [blame] | 741 | public int getCountY() { |
Adam Cohen | d22015c | 2010-07-26 22:02:18 -0700 | [diff] [blame] | 742 | return mCountY; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 743 | } |
| 744 | |
Sunny Goyal | c13403c | 2016-11-18 23:44:48 -0800 | [diff] [blame] | 745 | public boolean acceptsWidget() { |
| 746 | return mContainerType == WORKSPACE; |
Sunny Goyal | e9b651e | 2015-04-24 11:44:51 -0700 | [diff] [blame] | 747 | } |
| 748 | |
Sebastian Franco | d468299 | 2022-10-05 13:03:09 -0500 | [diff] [blame] | 749 | /** |
| 750 | * Adds the given view to the CellLayout |
| 751 | * |
| 752 | * @param child view to add. |
| 753 | * @param index index of the CellLayout children where to add the view. |
| 754 | * @param childId id of the view. |
| 755 | * @param params represent the logic of the view on the CellLayout. |
| 756 | * @param markCells if the occupied cells should be marked or not |
| 757 | * @return if adding the view was successful |
| 758 | */ |
| 759 | public boolean addViewToCellLayout(View child, int index, int childId, |
| 760 | CellLayoutLayoutParams params, boolean markCells) { |
| 761 | final CellLayoutLayoutParams lp = params; |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 762 | |
Andrew Flynn | de38e42 | 2012-05-08 11:22:15 -0700 | [diff] [blame] | 763 | // Hotseat icons - remove text |
Andrew Flynn | 0dca1ec | 2012-02-29 13:33:22 -0800 | [diff] [blame] | 764 | if (child instanceof BubbleTextView) { |
| 765 | BubbleTextView bubbleChild = (BubbleTextView) child; |
Jon Miranda | f1eae80 | 2017-10-04 11:23:33 -0700 | [diff] [blame] | 766 | bubbleChild.setTextVisibility(mContainerType != HOTSEAT); |
Andrew Flynn | 0dca1ec | 2012-02-29 13:33:22 -0800 | [diff] [blame] | 767 | } |
| 768 | |
Sunny Goyal | c13403c | 2016-11-18 23:44:48 -0800 | [diff] [blame] | 769 | child.setScaleX(mChildScale); |
| 770 | child.setScaleY(mChildScale); |
Adam Cohen | 307fe23 | 2012-08-16 17:55:58 -0700 | [diff] [blame] | 771 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 772 | // Generate an id for each view, this assumes we have at most 256x256 cells |
| 773 | // per workspace screen |
Sebastian Franco | 877088e | 2023-01-03 15:16:22 -0700 | [diff] [blame] | 774 | if (lp.getCellX() >= 0 && lp.getCellX() <= mCountX - 1 |
| 775 | && lp.getCellY() >= 0 && lp.getCellY() <= mCountY - 1) { |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 776 | // If the horizontal or vertical span is set to -1, it is taken to |
| 777 | // mean that it spans the extent of the CellLayout |
Adam Cohen | d22015c | 2010-07-26 22:02:18 -0700 | [diff] [blame] | 778 | if (lp.cellHSpan < 0) lp.cellHSpan = mCountX; |
| 779 | if (lp.cellVSpan < 0) lp.cellVSpan = mCountY; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 780 | |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 781 | child.setId(childId); |
Tony Wickham | a0628cc | 2015-10-14 15:23:04 -0700 | [diff] [blame] | 782 | if (LOGD) { |
| 783 | Log.d(TAG, "Adding view to ShortcutsAndWidgetsContainer: " + child); |
| 784 | } |
Sunny Goyal | f7a29e8 | 2015-04-24 15:20:43 -0700 | [diff] [blame] | 785 | mShortcutsAndWidgets.addView(child, index, lp); |
Michael Jurka | dee0589 | 2010-07-27 10:01:56 -0700 | [diff] [blame] | 786 | |
Michael Jurka | f3ca3ab | 2010-10-20 17:08:24 -0700 | [diff] [blame] | 787 | if (markCells) markCellsAsOccupiedForView(child); |
Michael Jurka | 0280c3b | 2010-09-17 15:00:07 -0700 | [diff] [blame] | 788 | |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 789 | return true; |
| 790 | } |
| 791 | return false; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 792 | } |
Michael Jurka | 3e7c763 | 2010-10-02 16:01:03 -0700 | [diff] [blame] | 793 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 794 | @Override |
Michael Jurka | 0280c3b | 2010-09-17 15:00:07 -0700 | [diff] [blame] | 795 | public void removeAllViews() { |
Sunny Goyal | ff4ba2d | 2016-04-02 14:12:34 -0700 | [diff] [blame] | 796 | mOccupied.clear(); |
Michael Jurka | a52570f | 2012-03-20 03:18:20 -0700 | [diff] [blame] | 797 | mShortcutsAndWidgets.removeAllViews(); |
Michael Jurka | 0280c3b | 2010-09-17 15:00:07 -0700 | [diff] [blame] | 798 | } |
| 799 | |
| 800 | @Override |
| 801 | public void removeAllViewsInLayout() { |
Michael Jurka | a52570f | 2012-03-20 03:18:20 -0700 | [diff] [blame] | 802 | if (mShortcutsAndWidgets.getChildCount() > 0) { |
Sunny Goyal | ff4ba2d | 2016-04-02 14:12:34 -0700 | [diff] [blame] | 803 | mOccupied.clear(); |
Michael Jurka | a52570f | 2012-03-20 03:18:20 -0700 | [diff] [blame] | 804 | mShortcutsAndWidgets.removeAllViewsInLayout(); |
Michael Jurka | 7cfc282 | 2011-08-02 20:19:24 -0700 | [diff] [blame] | 805 | } |
Michael Jurka | 0280c3b | 2010-09-17 15:00:07 -0700 | [diff] [blame] | 806 | } |
| 807 | |
| 808 | @Override |
| 809 | public void removeView(View view) { |
| 810 | markCellsAsUnoccupiedForView(view); |
Michael Jurka | a52570f | 2012-03-20 03:18:20 -0700 | [diff] [blame] | 811 | mShortcutsAndWidgets.removeView(view); |
Michael Jurka | 0280c3b | 2010-09-17 15:00:07 -0700 | [diff] [blame] | 812 | } |
| 813 | |
| 814 | @Override |
| 815 | public void removeViewAt(int index) { |
Michael Jurka | a52570f | 2012-03-20 03:18:20 -0700 | [diff] [blame] | 816 | markCellsAsUnoccupiedForView(mShortcutsAndWidgets.getChildAt(index)); |
| 817 | mShortcutsAndWidgets.removeViewAt(index); |
Michael Jurka | 0280c3b | 2010-09-17 15:00:07 -0700 | [diff] [blame] | 818 | } |
| 819 | |
| 820 | @Override |
| 821 | public void removeViewInLayout(View view) { |
| 822 | markCellsAsUnoccupiedForView(view); |
Michael Jurka | a52570f | 2012-03-20 03:18:20 -0700 | [diff] [blame] | 823 | mShortcutsAndWidgets.removeViewInLayout(view); |
Michael Jurka | 0280c3b | 2010-09-17 15:00:07 -0700 | [diff] [blame] | 824 | } |
| 825 | |
| 826 | @Override |
| 827 | public void removeViews(int start, int count) { |
| 828 | for (int i = start; i < start + count; i++) { |
Michael Jurka | a52570f | 2012-03-20 03:18:20 -0700 | [diff] [blame] | 829 | markCellsAsUnoccupiedForView(mShortcutsAndWidgets.getChildAt(i)); |
Michael Jurka | 0280c3b | 2010-09-17 15:00:07 -0700 | [diff] [blame] | 830 | } |
Michael Jurka | a52570f | 2012-03-20 03:18:20 -0700 | [diff] [blame] | 831 | mShortcutsAndWidgets.removeViews(start, count); |
Michael Jurka | 0280c3b | 2010-09-17 15:00:07 -0700 | [diff] [blame] | 832 | } |
| 833 | |
| 834 | @Override |
| 835 | public void removeViewsInLayout(int start, int count) { |
| 836 | for (int i = start; i < start + count; i++) { |
Michael Jurka | a52570f | 2012-03-20 03:18:20 -0700 | [diff] [blame] | 837 | markCellsAsUnoccupiedForView(mShortcutsAndWidgets.getChildAt(i)); |
Michael Jurka | 0280c3b | 2010-09-17 15:00:07 -0700 | [diff] [blame] | 838 | } |
Michael Jurka | a52570f | 2012-03-20 03:18:20 -0700 | [diff] [blame] | 839 | mShortcutsAndWidgets.removeViewsInLayout(start, count); |
Michael Jurka | abded66 | 2011-03-04 12:06:57 -0800 | [diff] [blame] | 840 | } |
| 841 | |
Patrick Dubroy | 6569f2c | 2010-07-12 14:25:18 -0700 | [diff] [blame] | 842 | /** |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 843 | * Given a point, return the cell that strictly encloses that point |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 844 | * @param x X coordinate of the point |
| 845 | * @param y Y coordinate of the point |
| 846 | * @param result Array of 2 ints to hold the x and y coordinate of the cell |
| 847 | */ |
Sunny Goyal | e9b651e | 2015-04-24 11:44:51 -0700 | [diff] [blame] | 848 | public void pointToCellExact(int x, int y, int[] result) { |
Winson Chung | 4b825dcd | 2011-06-19 12:41:22 -0700 | [diff] [blame] | 849 | final int hStartPadding = getPaddingLeft(); |
| 850 | final int vStartPadding = getPaddingTop(); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 851 | |
Sebastian Franco | b57c0b2 | 2022-06-28 13:54:35 -0700 | [diff] [blame] | 852 | result[0] = (x - hStartPadding) / (mCellWidth + mBorderSpace.x); |
| 853 | result[1] = (y - vStartPadding) / (mCellHeight + mBorderSpace.y); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 854 | |
Adam Cohen | d22015c | 2010-07-26 22:02:18 -0700 | [diff] [blame] | 855 | final int xAxis = mCountX; |
| 856 | final int yAxis = mCountY; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 857 | |
| 858 | if (result[0] < 0) result[0] = 0; |
| 859 | if (result[0] >= xAxis) result[0] = xAxis - 1; |
| 860 | if (result[1] < 0) result[1] = 0; |
| 861 | if (result[1] >= yAxis) result[1] = yAxis - 1; |
| 862 | } |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 863 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 864 | /** |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 865 | * Given a cell coordinate, return the point that represents the upper left corner of that cell |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 866 | * |
| 867 | * @param cellX X coordinate of the cell |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 868 | * @param cellY Y coordinate of the cell |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 869 | * |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 870 | * @param result Array of 2 ints to hold the x and y coordinate of the point |
| 871 | */ |
| 872 | void cellToPoint(int cellX, int cellY, int[] result) { |
Jon Miranda | 228877d | 2021-02-09 11:05:00 -0500 | [diff] [blame] | 873 | cellToRect(cellX, cellY, 1, 1, mTempRect); |
| 874 | result[0] = mTempRect.left; |
| 875 | result[1] = mTempRect.top; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 876 | } |
| 877 | |
Adam Cohen | e3e27a8 | 2011-04-15 12:07:39 -0700 | [diff] [blame] | 878 | /** |
Adam Cohen | 482ed82 | 2012-03-02 14:15:13 -0800 | [diff] [blame] | 879 | * Given a cell coordinate, return the point that represents the center of the cell |
Adam Cohen | e3e27a8 | 2011-04-15 12:07:39 -0700 | [diff] [blame] | 880 | * |
| 881 | * @param cellX X coordinate of the cell |
| 882 | * @param cellY Y coordinate of the cell |
| 883 | * |
| 884 | * @param result Array of 2 ints to hold the x and y coordinate of the point |
| 885 | */ |
| 886 | void cellToCenterPoint(int cellX, int cellY, int[] result) { |
Adam Cohen | 47a876d | 2012-03-19 13:21:41 -0700 | [diff] [blame] | 887 | regionToCenterPoint(cellX, cellY, 1, 1, result); |
| 888 | } |
| 889 | |
| 890 | /** |
Tony Wickham | 0ac045f | 2021-11-03 13:17:02 -0700 | [diff] [blame] | 891 | * Given a cell coordinate and span return the point that represents the center of the region |
Adam Cohen | 47a876d | 2012-03-19 13:21:41 -0700 | [diff] [blame] | 892 | * |
| 893 | * @param cellX X coordinate of the cell |
| 894 | * @param cellY Y coordinate of the cell |
| 895 | * |
| 896 | * @param result Array of 2 ints to hold the x and y coordinate of the point |
| 897 | */ |
Sebastian Franco | 45b39b5 | 2023-01-10 10:47:46 -0600 | [diff] [blame] | 898 | public void regionToCenterPoint(int cellX, int cellY, int spanX, int spanY, int[] result) { |
Jon Miranda | 228877d | 2021-02-09 11:05:00 -0500 | [diff] [blame] | 899 | cellToRect(cellX, cellY, spanX, spanY, mTempRect); |
| 900 | result[0] = mTempRect.centerX(); |
| 901 | result[1] = mTempRect.centerY(); |
Adam Cohen | 19f3792 | 2012-03-21 11:59:11 -0700 | [diff] [blame] | 902 | } |
| 903 | |
Tony Wickham | 3cfa5ed | 2021-11-03 13:20:43 -0700 | [diff] [blame] | 904 | /** |
| 905 | * Returns the distance between the given coordinate and the visual center of the given cell. |
| 906 | */ |
| 907 | public float getDistanceFromWorkspaceCellVisualCenter(float x, float y, int[] cell) { |
| 908 | getWorkspaceCellVisualCenter(cell[0], cell[1], mTmpPoint); |
Sunny Goyal | f7a29e8 | 2015-04-24 15:20:43 -0700 | [diff] [blame] | 909 | return (float) Math.hypot(x - mTmpPoint[0], y - mTmpPoint[1]); |
Adam Cohen | 482ed82 | 2012-03-02 14:15:13 -0800 | [diff] [blame] | 910 | } |
| 911 | |
Tony Wickham | 3cfa5ed | 2021-11-03 13:20:43 -0700 | [diff] [blame] | 912 | private void getWorkspaceCellVisualCenter(int cellX, int cellY, int[] outPoint) { |
| 913 | View child = getChildAt(cellX, cellY); |
| 914 | if (child instanceof DraggableView) { |
| 915 | DraggableView draggableChild = (DraggableView) child; |
| 916 | if (draggableChild.getViewType() == DRAGGABLE_ICON) { |
| 917 | cellToPoint(cellX, cellY, outPoint); |
| 918 | draggableChild.getWorkspaceVisualDragBounds(mTempRect); |
| 919 | mTempRect.offset(outPoint[0], outPoint[1]); |
| 920 | outPoint[0] = mTempRect.centerX(); |
| 921 | outPoint[1] = mTempRect.centerY(); |
| 922 | return; |
| 923 | } |
| 924 | } |
| 925 | cellToCenterPoint(cellX, cellY, outPoint); |
| 926 | } |
| 927 | |
Tony Wickham | 0ac045f | 2021-11-03 13:17:02 -0700 | [diff] [blame] | 928 | /** |
| 929 | * Returns the max distance from the center of a cell that can accept a drop to create a folder. |
| 930 | */ |
Tony Wickham | 1278490 | 2021-11-03 14:02:10 -0700 | [diff] [blame] | 931 | public float getFolderCreationRadius(int[] targetCell) { |
Tony Wickham | 0ac045f | 2021-11-03 13:17:02 -0700 | [diff] [blame] | 932 | DeviceProfile grid = mActivity.getDeviceProfile(); |
Tony Wickham | 1278490 | 2021-11-03 14:02:10 -0700 | [diff] [blame] | 933 | float iconVisibleRadius = ICON_VISIBLE_AREA_FACTOR * grid.iconSizePx / 2; |
| 934 | // Halfway between reorder radius and icon. |
Sebastian Franco | 6e1024e | 2022-07-29 13:46:49 -0700 | [diff] [blame] | 935 | return (getReorderRadius(targetCell, 1, 1) + iconVisibleRadius) / 2; |
Tony Wickham | 1278490 | 2021-11-03 14:02:10 -0700 | [diff] [blame] | 936 | } |
| 937 | |
| 938 | /** |
| 939 | * Returns the max distance from the center of a cell that will start to reorder on drag over. |
| 940 | */ |
Sebastian Franco | 6e1024e | 2022-07-29 13:46:49 -0700 | [diff] [blame] | 941 | public float getReorderRadius(int[] targetCell, int spanX, int spanY) { |
Tony Wickham | 1278490 | 2021-11-03 14:02:10 -0700 | [diff] [blame] | 942 | int[] centerPoint = mTmpPoint; |
| 943 | getWorkspaceCellVisualCenter(targetCell[0], targetCell[1], centerPoint); |
| 944 | |
| 945 | Rect cellBoundsWithSpacing = mTempRect; |
Sebastian Franco | 6e1024e | 2022-07-29 13:46:49 -0700 | [diff] [blame] | 946 | cellToRect(targetCell[0], targetCell[1], spanX, spanY, cellBoundsWithSpacing); |
Tony Wickham | 1278490 | 2021-11-03 14:02:10 -0700 | [diff] [blame] | 947 | cellBoundsWithSpacing.inset(-mBorderSpace.x / 2, -mBorderSpace.y / 2); |
| 948 | |
Sebastian Franco | c8392ea | 2022-10-28 16:38:37 -0700 | [diff] [blame] | 949 | if (canCreateFolder(getChildAt(targetCell[0], targetCell[1])) && spanX == 1 && spanY == 1) { |
Tony Wickham | 1278490 | 2021-11-03 14:02:10 -0700 | [diff] [blame] | 950 | // Take only the circle in the smaller dimension, to ensure we don't start reordering |
| 951 | // too soon before accepting a folder drop. |
| 952 | int minRadius = centerPoint[0] - cellBoundsWithSpacing.left; |
| 953 | minRadius = Math.min(minRadius, centerPoint[1] - cellBoundsWithSpacing.top); |
| 954 | minRadius = Math.min(minRadius, cellBoundsWithSpacing.right - centerPoint[0]); |
| 955 | minRadius = Math.min(minRadius, cellBoundsWithSpacing.bottom - centerPoint[1]); |
| 956 | return minRadius; |
| 957 | } |
| 958 | // Take up the entire cell, including space between this cell and the adjacent ones. |
Sebastian Franco | c8392ea | 2022-10-28 16:38:37 -0700 | [diff] [blame] | 959 | // Multiply by span to scale radius |
| 960 | return (float) Math.hypot(spanX * cellBoundsWithSpacing.width() / 2f, |
| 961 | spanY * cellBoundsWithSpacing.height() / 2f); |
Tony Wickham | 0ac045f | 2021-11-03 13:17:02 -0700 | [diff] [blame] | 962 | } |
| 963 | |
Adam Cohen | f9c184a | 2016-01-15 16:47:43 -0800 | [diff] [blame] | 964 | public int getCellWidth() { |
Romain Guy | 84f296c | 2009-11-04 15:00:44 -0800 | [diff] [blame] | 965 | return mCellWidth; |
| 966 | } |
| 967 | |
Sunny Goyal | 0b754e5 | 2017-08-07 07:42:45 -0700 | [diff] [blame] | 968 | public int getCellHeight() { |
Romain Guy | 84f296c | 2009-11-04 15:00:44 -0800 | [diff] [blame] | 969 | return mCellHeight; |
| 970 | } |
| 971 | |
Adam Cohen | f0f4eda | 2013-06-06 21:27:03 -0700 | [diff] [blame] | 972 | public void setFixedSize(int width, int height) { |
| 973 | mFixedWidth = width; |
| 974 | mFixedHeight = height; |
| 975 | } |
| 976 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 977 | @Override |
| 978 | protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 979 | int widthSpecMode = MeasureSpec.getMode(widthMeasureSpec); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 980 | int heightSpecMode = MeasureSpec.getMode(heightMeasureSpec); |
Winson Chung | 5f8afe6 | 2013-08-12 16:19:28 -0700 | [diff] [blame] | 981 | int widthSize = MeasureSpec.getSize(widthMeasureSpec); |
| 982 | int heightSize = MeasureSpec.getSize(heightMeasureSpec); |
Winson Chung | 2d75f12 | 2013-09-23 16:53:31 -0700 | [diff] [blame] | 983 | int childWidthSize = widthSize - (getPaddingLeft() + getPaddingRight()); |
| 984 | int childHeightSize = heightSize - (getPaddingTop() + getPaddingBottom()); |
Sunny Goyal | ae6e318 | 2019-04-30 12:04:37 -0700 | [diff] [blame] | 985 | |
Winson Chung | 11a1a53 | 2013-09-13 11:14:45 -0700 | [diff] [blame] | 986 | if (mFixedCellWidth < 0 || mFixedCellHeight < 0) { |
Thales Lima | 78d00ad | 2021-09-30 11:29:06 +0100 | [diff] [blame] | 987 | int cw = DeviceProfile.calculateCellWidth(childWidthSize, mBorderSpace.x, |
Jon Miranda | 228877d | 2021-02-09 11:05:00 -0500 | [diff] [blame] | 988 | mCountX); |
Thales Lima | 78d00ad | 2021-09-30 11:29:06 +0100 | [diff] [blame] | 989 | int ch = DeviceProfile.calculateCellHeight(childHeightSize, mBorderSpace.y, |
Jon Miranda | 228877d | 2021-02-09 11:05:00 -0500 | [diff] [blame] | 990 | mCountY); |
Winson Chung | 11a1a53 | 2013-09-13 11:14:45 -0700 | [diff] [blame] | 991 | if (cw != mCellWidth || ch != mCellHeight) { |
| 992 | mCellWidth = cw; |
| 993 | mCellHeight = ch; |
Jon Miranda | 228877d | 2021-02-09 11:05:00 -0500 | [diff] [blame] | 994 | mShortcutsAndWidgets.setCellDimensions(mCellWidth, mCellHeight, mCountX, mCountY, |
Thales Lima | 78d00ad | 2021-09-30 11:29:06 +0100 | [diff] [blame] | 995 | mBorderSpace); |
Winson Chung | 11a1a53 | 2013-09-13 11:14:45 -0700 | [diff] [blame] | 996 | } |
Winson Chung | 5f8afe6 | 2013-08-12 16:19:28 -0700 | [diff] [blame] | 997 | } |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 998 | |
Winson Chung | 2d75f12 | 2013-09-23 16:53:31 -0700 | [diff] [blame] | 999 | int newWidth = childWidthSize; |
| 1000 | int newHeight = childHeightSize; |
Adam Cohen | f0f4eda | 2013-06-06 21:27:03 -0700 | [diff] [blame] | 1001 | if (mFixedWidth > 0 && mFixedHeight > 0) { |
| 1002 | newWidth = mFixedWidth; |
| 1003 | newHeight = mFixedHeight; |
| 1004 | } else if (widthSpecMode == MeasureSpec.UNSPECIFIED || heightSpecMode == MeasureSpec.UNSPECIFIED) { |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1005 | throw new RuntimeException("CellLayout cannot have UNSPECIFIED dimensions"); |
| 1006 | } |
| 1007 | |
Sunny Goyal | 4fe5a37 | 2015-05-14 19:55:10 -0700 | [diff] [blame] | 1008 | mShortcutsAndWidgets.measure( |
| 1009 | MeasureSpec.makeMeasureSpec(newWidth, MeasureSpec.EXACTLY), |
| 1010 | MeasureSpec.makeMeasureSpec(newHeight, MeasureSpec.EXACTLY)); |
| 1011 | |
| 1012 | int maxWidth = mShortcutsAndWidgets.getMeasuredWidth(); |
| 1013 | int maxHeight = mShortcutsAndWidgets.getMeasuredHeight(); |
Winson Chung | 2d75f12 | 2013-09-23 16:53:31 -0700 | [diff] [blame] | 1014 | if (mFixedWidth > 0 && mFixedHeight > 0) { |
| 1015 | setMeasuredDimension(maxWidth, maxHeight); |
| 1016 | } else { |
| 1017 | setMeasuredDimension(widthSize, heightSize); |
| 1018 | } |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1019 | } |
| 1020 | |
| 1021 | @Override |
Michael Jurka | 28750fb | 2010-09-24 17:43:49 -0700 | [diff] [blame] | 1022 | protected void onLayout(boolean changed, int l, int t, int r, int b) { |
Tony Wickham | 26b0142 | 2015-11-10 14:44:32 -0800 | [diff] [blame] | 1023 | int left = getPaddingLeft(); |
Sunny Goyal | 7ce471b | 2017-08-02 03:37:39 -0700 | [diff] [blame] | 1024 | left += (int) Math.ceil(getUnusedHorizontalSpace() / 2f); |
Sunny Goyal | 7c786f7 | 2016-06-01 14:08:21 -0700 | [diff] [blame] | 1025 | int right = r - l - getPaddingRight(); |
Sunny Goyal | 7ce471b | 2017-08-02 03:37:39 -0700 | [diff] [blame] | 1026 | right -= (int) Math.ceil(getUnusedHorizontalSpace() / 2f); |
Sunny Goyal | 7c786f7 | 2016-06-01 14:08:21 -0700 | [diff] [blame] | 1027 | |
Winson Chung | 38848ca | 2013-10-08 12:03:44 -0700 | [diff] [blame] | 1028 | int top = getPaddingTop(); |
Sunny Goyal | 7c786f7 | 2016-06-01 14:08:21 -0700 | [diff] [blame] | 1029 | int bottom = b - t - getPaddingBottom(); |
Sunny Goyal | 4fe5a37 | 2015-05-14 19:55:10 -0700 | [diff] [blame] | 1030 | |
Sunny Goyal | 7c786f7 | 2016-06-01 14:08:21 -0700 | [diff] [blame] | 1031 | // Expand the background drawing bounds by the padding baked into the background drawable |
| 1032 | mBackground.getPadding(mTempRect); |
| 1033 | mBackground.setBounds( |
Jon Miranda | 2803200 | 2017-07-13 16:18:56 -0700 | [diff] [blame] | 1034 | left - mTempRect.left - getPaddingLeft(), |
| 1035 | top - mTempRect.top - getPaddingTop(), |
| 1036 | right + mTempRect.right + getPaddingRight(), |
| 1037 | bottom + mTempRect.bottom + getPaddingBottom()); |
Sunny Goyal | ae6e318 | 2019-04-30 12:04:37 -0700 | [diff] [blame] | 1038 | |
Sunny Goyal | c4d3201 | 2020-04-03 17:10:11 -0700 | [diff] [blame] | 1039 | mShortcutsAndWidgets.layout(left, top, right, bottom); |
Sunny Goyal | 7c786f7 | 2016-06-01 14:08:21 -0700 | [diff] [blame] | 1040 | } |
| 1041 | |
Tony Wickham | a501d49 | 2015-11-03 18:05:01 -0800 | [diff] [blame] | 1042 | /** |
| 1043 | * Returns the amount of space left over after subtracting padding and cells. This space will be |
| 1044 | * very small, a few pixels at most, and is a result of rounding down when calculating the cell |
Jon Miranda | 228877d | 2021-02-09 11:05:00 -0500 | [diff] [blame] | 1045 | * width in {@link DeviceProfile#calculateCellWidth(int, int, int)}. |
Tony Wickham | a501d49 | 2015-11-03 18:05:01 -0800 | [diff] [blame] | 1046 | */ |
| 1047 | public int getUnusedHorizontalSpace() { |
Jon Miranda | 228877d | 2021-02-09 11:05:00 -0500 | [diff] [blame] | 1048 | return getMeasuredWidth() - getPaddingLeft() - getPaddingRight() - (mCountX * mCellWidth) |
Thales Lima | 78d00ad | 2021-09-30 11:29:06 +0100 | [diff] [blame] | 1049 | - ((mCountX - 1) * mBorderSpace.x); |
Tony Wickham | a501d49 | 2015-11-03 18:05:01 -0800 | [diff] [blame] | 1050 | } |
| 1051 | |
Sunny Goyal | 2805e63 | 2015-05-20 15:35:32 -0700 | [diff] [blame] | 1052 | @Override |
| 1053 | protected boolean verifyDrawable(Drawable who) { |
Sunny Goyal | 7ce471b | 2017-08-02 03:37:39 -0700 | [diff] [blame] | 1054 | return super.verifyDrawable(who) || (who == mBackground); |
Sunny Goyal | 2805e63 | 2015-05-20 15:35:32 -0700 | [diff] [blame] | 1055 | } |
| 1056 | |
Michael Jurka | a52570f | 2012-03-20 03:18:20 -0700 | [diff] [blame] | 1057 | public ShortcutAndWidgetContainer getShortcutsAndWidgets() { |
Sunny Goyal | dcbcc86 | 2014-08-12 15:58:36 -0700 | [diff] [blame] | 1058 | return mShortcutsAndWidgets; |
Michael Jurka | a52570f | 2012-03-20 03:18:20 -0700 | [diff] [blame] | 1059 | } |
| 1060 | |
Jon Miranda | 228877d | 2021-02-09 11:05:00 -0500 | [diff] [blame] | 1061 | public View getChildAt(int cellX, int cellY) { |
| 1062 | return mShortcutsAndWidgets.getChildAt(cellX, cellY); |
Patrick Dubroy | 440c360 | 2010-07-13 17:50:32 -0700 | [diff] [blame] | 1063 | } |
| 1064 | |
Adam Cohen | 76fc085 | 2011-06-17 13:26:23 -0700 | [diff] [blame] | 1065 | public boolean animateChildToPosition(final View child, int cellX, int cellY, int duration, |
Adam Cohen | 482ed82 | 2012-03-02 14:15:13 -0800 | [diff] [blame] | 1066 | int delay, boolean permanent, boolean adjustOccupied) { |
Michael Jurka | a52570f | 2012-03-20 03:18:20 -0700 | [diff] [blame] | 1067 | ShortcutAndWidgetContainer clc = getShortcutsAndWidgets(); |
Adam Cohen | 482ed82 | 2012-03-02 14:15:13 -0800 | [diff] [blame] | 1068 | |
Adam Cohen | 1d13c0b | 2020-04-21 16:29:12 -0700 | [diff] [blame] | 1069 | if (clc.indexOfChild(child) != -1 && (child instanceof Reorderable)) { |
Sebastian Franco | d468299 | 2022-10-05 13:03:09 -0500 | [diff] [blame] | 1070 | final CellLayoutLayoutParams lp = (CellLayoutLayoutParams) child.getLayoutParams(); |
Adam Cohen | bfbfd26 | 2011-06-13 16:55:12 -0700 | [diff] [blame] | 1071 | final ItemInfo info = (ItemInfo) child.getTag(); |
Adam Cohen | 1d13c0b | 2020-04-21 16:29:12 -0700 | [diff] [blame] | 1072 | final Reorderable item = (Reorderable) child; |
Adam Cohen | bfbfd26 | 2011-06-13 16:55:12 -0700 | [diff] [blame] | 1073 | |
| 1074 | // We cancel any existing animations |
| 1075 | if (mReorderAnimators.containsKey(lp)) { |
| 1076 | mReorderAnimators.get(lp).cancel(); |
| 1077 | mReorderAnimators.remove(lp); |
| 1078 | } |
| 1079 | |
Adam Cohen | 1d13c0b | 2020-04-21 16:29:12 -0700 | [diff] [blame] | 1080 | |
Adam Cohen | 482ed82 | 2012-03-02 14:15:13 -0800 | [diff] [blame] | 1081 | if (adjustOccupied) { |
Sunny Goyal | ff4ba2d | 2016-04-02 14:12:34 -0700 | [diff] [blame] | 1082 | GridOccupancy occupied = permanent ? mOccupied : mTmpOccupied; |
Sebastian Franco | 877088e | 2023-01-03 15:16:22 -0700 | [diff] [blame] | 1083 | occupied.markCells(lp.getCellX(), lp.getCellY(), lp.cellHSpan, lp.cellVSpan, false); |
Sunny Goyal | ff4ba2d | 2016-04-02 14:12:34 -0700 | [diff] [blame] | 1084 | occupied.markCells(cellX, cellY, lp.cellHSpan, lp.cellVSpan, true); |
Adam Cohen | 482ed82 | 2012-03-02 14:15:13 -0800 | [diff] [blame] | 1085 | } |
Adam Cohen | 1d13c0b | 2020-04-21 16:29:12 -0700 | [diff] [blame] | 1086 | |
| 1087 | // Compute the new x and y position based on the new cellX and cellY |
| 1088 | // We leverage the actual layout logic in the layout params and hence need to modify |
| 1089 | // state and revert that state. |
| 1090 | final int oldX = lp.x; |
| 1091 | final int oldY = lp.y; |
Adam Cohen | bfbfd26 | 2011-06-13 16:55:12 -0700 | [diff] [blame] | 1092 | lp.isLockedToGrid = true; |
Adam Cohen | 482ed82 | 2012-03-02 14:15:13 -0800 | [diff] [blame] | 1093 | if (permanent) { |
Sunny Goyal | 669b71f | 2023-01-27 14:37:07 -0800 | [diff] [blame] | 1094 | lp.setCellX(cellX); |
| 1095 | lp.setCellY(cellY); |
Adam Cohen | 482ed82 | 2012-03-02 14:15:13 -0800 | [diff] [blame] | 1096 | } else { |
Sebastian Franco | 877088e | 2023-01-03 15:16:22 -0700 | [diff] [blame] | 1097 | lp.setTmpCellX(cellX); |
| 1098 | lp.setTmpCellY(cellY); |
Adam Cohen | 482ed82 | 2012-03-02 14:15:13 -0800 | [diff] [blame] | 1099 | } |
Jon Miranda | e96798e | 2016-12-07 12:10:44 -0800 | [diff] [blame] | 1100 | clc.setupLp(child); |
Adam Cohen | 482ed82 | 2012-03-02 14:15:13 -0800 | [diff] [blame] | 1101 | final int newX = lp.x; |
| 1102 | final int newY = lp.y; |
Adam Cohen | 76fc085 | 2011-06-17 13:26:23 -0700 | [diff] [blame] | 1103 | lp.x = oldX; |
| 1104 | lp.y = oldY; |
Adam Cohen | 1d13c0b | 2020-04-21 16:29:12 -0700 | [diff] [blame] | 1105 | lp.isLockedToGrid = false; |
| 1106 | // End compute new x and y |
| 1107 | |
Sunny Goyal | 82dfc15 | 2023-02-24 16:50:09 -0800 | [diff] [blame] | 1108 | MultiTranslateDelegate mtd = item.getTranslateDelegate(); |
| 1109 | float initPreviewOffsetX = mtd.getTranslationX(INDEX_REORDER_PREVIEW_OFFSET).getValue(); |
| 1110 | float initPreviewOffsetY = mtd.getTranslationY(INDEX_REORDER_PREVIEW_OFFSET).getValue(); |
Adam Cohen | 1d13c0b | 2020-04-21 16:29:12 -0700 | [diff] [blame] | 1111 | final float finalPreviewOffsetX = newX - oldX; |
| 1112 | final float finalPreviewOffsetY = newY - oldY; |
| 1113 | |
Adam Cohen | 482ed82 | 2012-03-02 14:15:13 -0800 | [diff] [blame] | 1114 | // Exit early if we're not actually moving the view |
Adam Cohen | 1d13c0b | 2020-04-21 16:29:12 -0700 | [diff] [blame] | 1115 | if (finalPreviewOffsetX == 0 && finalPreviewOffsetY == 0 |
| 1116 | && initPreviewOffsetX == 0 && initPreviewOffsetY == 0) { |
Adam Cohen | 482ed82 | 2012-03-02 14:15:13 -0800 | [diff] [blame] | 1117 | lp.isLockedToGrid = true; |
| 1118 | return true; |
| 1119 | } |
| 1120 | |
Sunny Goyal | 849c6a2 | 2018-08-08 16:33:46 -0700 | [diff] [blame] | 1121 | ValueAnimator va = ValueAnimator.ofFloat(0f, 1f); |
Adam Cohen | 482ed82 | 2012-03-02 14:15:13 -0800 | [diff] [blame] | 1122 | va.setDuration(duration); |
| 1123 | mReorderAnimators.put(lp, va); |
| 1124 | |
| 1125 | va.addUpdateListener(new AnimatorUpdateListener() { |
| 1126 | @Override |
Adam Cohen | bfbfd26 | 2011-06-13 16:55:12 -0700 | [diff] [blame] | 1127 | public void onAnimationUpdate(ValueAnimator animation) { |
Jon Miranda | e96798e | 2016-12-07 12:10:44 -0800 | [diff] [blame] | 1128 | float r = (Float) animation.getAnimatedValue(); |
Adam Cohen | 1d13c0b | 2020-04-21 16:29:12 -0700 | [diff] [blame] | 1129 | float x = (1 - r) * initPreviewOffsetX + r * finalPreviewOffsetX; |
| 1130 | float y = (1 - r) * initPreviewOffsetY + r * finalPreviewOffsetY; |
Sunny Goyal | 82dfc15 | 2023-02-24 16:50:09 -0800 | [diff] [blame] | 1131 | item.getTranslateDelegate().setTranslation(INDEX_REORDER_PREVIEW_OFFSET, x, y); |
Adam Cohen | bfbfd26 | 2011-06-13 16:55:12 -0700 | [diff] [blame] | 1132 | } |
| 1133 | }); |
Adam Cohen | 482ed82 | 2012-03-02 14:15:13 -0800 | [diff] [blame] | 1134 | va.addListener(new AnimatorListenerAdapter() { |
Adam Cohen | bfbfd26 | 2011-06-13 16:55:12 -0700 | [diff] [blame] | 1135 | boolean cancelled = false; |
| 1136 | public void onAnimationEnd(Animator animation) { |
| 1137 | // If the animation was cancelled, it means that another animation |
| 1138 | // has interrupted this one, and we don't want to lock the item into |
| 1139 | // place just yet. |
| 1140 | if (!cancelled) { |
| 1141 | lp.isLockedToGrid = true; |
Sunny Goyal | 82dfc15 | 2023-02-24 16:50:09 -0800 | [diff] [blame] | 1142 | item.getTranslateDelegate() |
| 1143 | .setTranslation(INDEX_REORDER_PREVIEW_OFFSET, 0, 0); |
Adam Cohen | 482ed82 | 2012-03-02 14:15:13 -0800 | [diff] [blame] | 1144 | child.requestLayout(); |
Adam Cohen | bfbfd26 | 2011-06-13 16:55:12 -0700 | [diff] [blame] | 1145 | } |
| 1146 | if (mReorderAnimators.containsKey(lp)) { |
| 1147 | mReorderAnimators.remove(lp); |
| 1148 | } |
| 1149 | } |
| 1150 | public void onAnimationCancel(Animator animation) { |
| 1151 | cancelled = true; |
| 1152 | } |
| 1153 | }); |
Adam Cohen | 482ed82 | 2012-03-02 14:15:13 -0800 | [diff] [blame] | 1154 | va.setStartDelay(delay); |
| 1155 | va.start(); |
Adam Cohen | bfbfd26 | 2011-06-13 16:55:12 -0700 | [diff] [blame] | 1156 | return true; |
| 1157 | } |
| 1158 | return false; |
| 1159 | } |
| 1160 | |
Adam Cohen | f7ca3b4 | 2021-02-22 11:03:58 -0800 | [diff] [blame] | 1161 | void visualizeDropLocation(int cellX, int cellY, int spanX, int spanY, |
| 1162 | DropTarget.DragObject dragObject) { |
| 1163 | if (mDragCell[0] != cellX || mDragCell[1] != cellY || mDragCellSpan[0] != spanX |
| 1164 | || mDragCellSpan[1] != spanY) { |
Adam Cohen | 482ed82 | 2012-03-02 14:15:13 -0800 | [diff] [blame] | 1165 | mDragCell[0] = cellX; |
| 1166 | mDragCell[1] = cellY; |
Adam Cohen | f7ca3b4 | 2021-02-22 11:03:58 -0800 | [diff] [blame] | 1167 | mDragCellSpan[0] = spanX; |
| 1168 | mDragCellSpan[1] = spanY; |
Steven Ng | 30dd1d6 | 2021-03-15 21:45:49 +0000 | [diff] [blame] | 1169 | |
Steven Ng | a999e22 | 2021-04-19 18:17:15 +0100 | [diff] [blame] | 1170 | // Apply color extraction on a widget when dragging. |
| 1171 | applyColorExtractionOnWidget(dragObject, mDragCell, spanX, spanY); |
| 1172 | |
Joe Onorato | 4be866d | 2010-10-10 11:26:02 -0700 | [diff] [blame] | 1173 | final int oldIndex = mDragOutlineCurrent; |
Patrick Dubroy | 08ae2ec | 2010-10-14 23:54:22 -0700 | [diff] [blame] | 1174 | mDragOutlineAnims[oldIndex].animateOut(); |
| 1175 | mDragOutlineCurrent = (oldIndex + 1) % mDragOutlines.length; |
Sunny Goyal | 106bf64 | 2015-07-16 12:18:06 -0700 | [diff] [blame] | 1176 | |
Sebastian Franco | d468299 | 2022-10-05 13:03:09 -0500 | [diff] [blame] | 1177 | CellLayoutLayoutParams cell = mDragOutlines[mDragOutlineCurrent]; |
Sebastian Franco | 877088e | 2023-01-03 15:16:22 -0700 | [diff] [blame] | 1178 | cell.setCellX(cellX); |
| 1179 | cell.setCellY(cellY); |
Adam Cohen | f7ca3b4 | 2021-02-22 11:03:58 -0800 | [diff] [blame] | 1180 | cell.cellHSpan = spanX; |
| 1181 | cell.cellVSpan = spanY; |
Adam Cohen | 6508699 | 2020-02-19 08:40:49 -0800 | [diff] [blame] | 1182 | |
Patrick Dubroy | 08ae2ec | 2010-10-14 23:54:22 -0700 | [diff] [blame] | 1183 | mDragOutlineAnims[mDragOutlineCurrent].animateIn(); |
Adam Cohen | f7ca3b4 | 2021-02-22 11:03:58 -0800 | [diff] [blame] | 1184 | invalidate(); |
Sunny Goyal | e78e3d7 | 2015-09-24 11:23:31 -0700 | [diff] [blame] | 1185 | |
| 1186 | if (dragObject.stateAnnouncer != null) { |
Sunny Goyal | c13403c | 2016-11-18 23:44:48 -0800 | [diff] [blame] | 1187 | dragObject.stateAnnouncer.announce(getItemMoveDescription(cellX, cellY)); |
Sunny Goyal | e78e3d7 | 2015-09-24 11:23:31 -0700 | [diff] [blame] | 1188 | } |
Adam Cohen | f7ca3b4 | 2021-02-22 11:03:58 -0800 | [diff] [blame] | 1189 | |
Patrick Dubroy | 6569f2c | 2010-07-12 14:25:18 -0700 | [diff] [blame] | 1190 | } |
| 1191 | } |
| 1192 | |
Steven Ng | 30dd1d6 | 2021-03-15 21:45:49 +0000 | [diff] [blame] | 1193 | /** Applies the local color extraction to a dragging widget object. */ |
Steven Ng | a999e22 | 2021-04-19 18:17:15 +0100 | [diff] [blame] | 1194 | private void applyColorExtractionOnWidget(DropTarget.DragObject dragObject, int[] targetCell, |
| 1195 | int spanX, int spanY) { |
Steven Ng | 30dd1d6 | 2021-03-15 21:45:49 +0000 | [diff] [blame] | 1196 | // Apply local extracted color if the DragView is an AppWidgetHostViewDrawable. |
Steven Ng | 3242720 | 2021-04-19 18:12:12 +0100 | [diff] [blame] | 1197 | View view = dragObject.dragView.getContentView(); |
| 1198 | if (view instanceof LauncherAppWidgetHostView) { |
Andras Kloczl | 2dacbee | 2022-02-21 16:53:28 +0000 | [diff] [blame] | 1199 | int screenId = getWorkspace().getIdForScreen(this); |
Steven Ng | 30dd1d6 | 2021-03-15 21:45:49 +0000 | [diff] [blame] | 1200 | cellToRect(targetCell[0], targetCell[1], spanX, spanY, mTempRect); |
Jonathan Miranda | 21930da | 2021-05-03 18:44:13 +0000 | [diff] [blame] | 1201 | |
Sunny Goyal | 69a8eec | 2021-07-22 10:02:50 -0700 | [diff] [blame] | 1202 | ((LauncherAppWidgetHostView) view).handleDrag(mTempRect, this, screenId); |
Steven Ng | 30dd1d6 | 2021-03-15 21:45:49 +0000 | [diff] [blame] | 1203 | } |
| 1204 | } |
| 1205 | |
Sunny Goyal | 726bee7 | 2018-03-05 12:54:24 -0800 | [diff] [blame] | 1206 | @SuppressLint("StringFormatMatches") |
Sunny Goyal | c13403c | 2016-11-18 23:44:48 -0800 | [diff] [blame] | 1207 | public String getItemMoveDescription(int cellX, int cellY) { |
| 1208 | if (mContainerType == HOTSEAT) { |
| 1209 | return getContext().getString(R.string.move_to_hotseat_position, |
| 1210 | Math.max(cellX, cellY) + 1); |
| 1211 | } else { |
Shikha Malhotra | f78da1b | 2022-04-11 10:23:18 +0000 | [diff] [blame] | 1212 | Workspace<?> workspace = getWorkspace(); |
Andras Kloczl | 2dacbee | 2022-02-21 16:53:28 +0000 | [diff] [blame] | 1213 | int row = cellY + 1; |
| 1214 | int col = workspace.mIsRtl ? mCountX - cellX : cellX + 1; |
| 1215 | int panelCount = workspace.getPanelCount(); |
Sebastian Franco | 930531f | 2022-06-16 16:49:11 -0700 | [diff] [blame] | 1216 | int screenId = workspace.getIdForScreen(this); |
| 1217 | int pageIndex = workspace.getPageIndexForScreenId(screenId); |
Andras Kloczl | 2dacbee | 2022-02-21 16:53:28 +0000 | [diff] [blame] | 1218 | if (panelCount > 1) { |
| 1219 | // Increment the column if the target is on the right side of a two panel home |
Andras Kloczl | 2dacbee | 2022-02-21 16:53:28 +0000 | [diff] [blame] | 1220 | col += (pageIndex % panelCount) * mCountX; |
| 1221 | } |
Sebastian Franco | 930531f | 2022-06-16 16:49:11 -0700 | [diff] [blame] | 1222 | return getContext().getString(R.string.move_to_empty_cell_description, row, col, |
| 1223 | workspace.getPageDescription(pageIndex)); |
Sunny Goyal | c13403c | 2016-11-18 23:44:48 -0800 | [diff] [blame] | 1224 | } |
| 1225 | } |
| 1226 | |
Shikha Malhotra | f78da1b | 2022-04-11 10:23:18 +0000 | [diff] [blame] | 1227 | private Workspace<?> getWorkspace() { |
Andras Kloczl | 2dacbee | 2022-02-21 16:53:28 +0000 | [diff] [blame] | 1228 | return Launcher.cast(mActivity).getWorkspace(); |
| 1229 | } |
| 1230 | |
Adam Cohen | e031096 | 2011-04-18 16:15:31 -0700 | [diff] [blame] | 1231 | public void clearDragOutlines() { |
| 1232 | final int oldIndex = mDragOutlineCurrent; |
| 1233 | mDragOutlineAnims[oldIndex].animateOut(); |
Adam Cohen | d41fbf5 | 2012-02-16 23:53:59 -0800 | [diff] [blame] | 1234 | mDragCell[0] = mDragCell[1] = -1; |
Adam Cohen | e031096 | 2011-04-18 16:15:31 -0700 | [diff] [blame] | 1235 | } |
| 1236 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1237 | /** |
Jeff Sharkey | 7086428 | 2009-04-07 21:08:40 -0700 | [diff] [blame] | 1238 | * Find a vacant area that will fit the given bounds nearest the requested |
| 1239 | * cell location. Uses Euclidean distance to score multiple vacant areas. |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 1240 | * |
Romain Guy | 51afc02 | 2009-05-04 18:03:43 -0700 | [diff] [blame] | 1241 | * @param pixelX The X location at which you want to search for a vacant area. |
| 1242 | * @param pixelY The Y location at which you want to search for a vacant area. |
Adam Cohen | d41fbf5 | 2012-02-16 23:53:59 -0800 | [diff] [blame] | 1243 | * @param minSpanX The minimum horizontal span required |
| 1244 | * @param minSpanY The minimum vertical span required |
| 1245 | * @param spanX Horizontal span of the object. |
| 1246 | * @param spanY Vertical span of the object. |
| 1247 | * @param result Array in which to place the result, or null (in which case a new array will |
| 1248 | * be allocated) |
| 1249 | * @return The X, Y cell of a vacant area that can contain this object, |
| 1250 | * nearest the requested location. |
| 1251 | */ |
Sebastian Franco | e4c0345 | 2022-12-27 14:50:02 -0600 | [diff] [blame] | 1252 | public int[] findNearestVacantArea(int pixelX, int pixelY, int minSpanX, int minSpanY, |
| 1253 | int spanX, int spanY, int[] result, int[] resultSpan) { |
Sebastian Franco | b57c0b2 | 2022-06-28 13:54:35 -0700 | [diff] [blame] | 1254 | return findNearestArea(pixelX, pixelY, minSpanX, minSpanY, spanX, spanY, false, |
Adam Cohen | d41fbf5 | 2012-02-16 23:53:59 -0800 | [diff] [blame] | 1255 | result, resultSpan); |
| 1256 | } |
| 1257 | |
Adam Cohen | d41fbf5 | 2012-02-16 23:53:59 -0800 | [diff] [blame] | 1258 | /** |
| 1259 | * Find a vacant area that will fit the given bounds nearest the requested |
| 1260 | * cell location. Uses Euclidean distance to score multiple vacant areas. |
Sebastian Franco | b57c0b2 | 2022-06-28 13:54:35 -0700 | [diff] [blame] | 1261 | * @param relativeXPos The X location relative to the Cell layout at which you want to search |
| 1262 | * for a vacant area. |
| 1263 | * @param relativeYPos The Y location relative to the Cell layout at which you want to search |
| 1264 | * for a vacant area. |
Adam Cohen | d41fbf5 | 2012-02-16 23:53:59 -0800 | [diff] [blame] | 1265 | * @param minSpanX The minimum horizontal span required |
| 1266 | * @param minSpanY The minimum vertical span required |
| 1267 | * @param spanX Horizontal span of the object. |
| 1268 | * @param spanY Vertical span of the object. |
| 1269 | * @param ignoreOccupied If true, the result can be an occupied cell |
| 1270 | * @param result Array in which to place the result, or null (in which case a new array will |
| 1271 | * be allocated) |
| 1272 | * @return The X, Y cell of a vacant area that can contain this object, |
| 1273 | * nearest the requested location. |
| 1274 | */ |
Sebastian Franco | 96c46e7 | 2023-05-08 10:04:44 -0600 | [diff] [blame] | 1275 | protected int[] findNearestArea(int relativeXPos, int relativeYPos, int minSpanX, int minSpanY, |
Sebastian Franco | b57c0b2 | 2022-06-28 13:54:35 -0700 | [diff] [blame] | 1276 | int spanX, int spanY, boolean ignoreOccupied, int[] result, int[] resultSpan) { |
Sebastian Franco | b57c0b2 | 2022-06-28 13:54:35 -0700 | [diff] [blame] | 1277 | // For items with a spanX / spanY > 1, the passed in point (relativeXPos, relativeYPos) |
| 1278 | // corresponds to the center of the item, but we are searching based on the top-left cell, |
| 1279 | // so we translate the point over to correspond to the top-left. |
| 1280 | relativeXPos = (int) (relativeXPos - (mCellWidth + mBorderSpace.x) * (spanX - 1) / 2f); |
| 1281 | relativeYPos = (int) (relativeYPos - (mCellHeight + mBorderSpace.y) * (spanY - 1) / 2f); |
Adam Cohen | e3e27a8 | 2011-04-15 12:07:39 -0700 | [diff] [blame] | 1282 | |
Jeff Sharkey | 7086428 | 2009-04-07 21:08:40 -0700 | [diff] [blame] | 1283 | // Keep track of best-scoring drop area |
Patrick Dubroy | de7658b | 2010-09-27 11:15:43 -0700 | [diff] [blame] | 1284 | final int[] bestXY = result != null ? result : new int[2]; |
Jeff Sharkey | 7086428 | 2009-04-07 21:08:40 -0700 | [diff] [blame] | 1285 | double bestDistance = Double.MAX_VALUE; |
Adam Cohen | d41fbf5 | 2012-02-16 23:53:59 -0800 | [diff] [blame] | 1286 | final Rect bestRect = new Rect(-1, -1, -1, -1); |
Rajeev Kumar | 9962dbe | 2017-06-12 12:16:20 -0700 | [diff] [blame] | 1287 | final Stack<Rect> validRegions = new Stack<>(); |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 1288 | |
Patrick Dubroy | de7658b | 2010-09-27 11:15:43 -0700 | [diff] [blame] | 1289 | final int countX = mCountX; |
| 1290 | final int countY = mCountY; |
Patrick Dubroy | de7658b | 2010-09-27 11:15:43 -0700 | [diff] [blame] | 1291 | |
Adam Cohen | d41fbf5 | 2012-02-16 23:53:59 -0800 | [diff] [blame] | 1292 | if (minSpanX <= 0 || minSpanY <= 0 || spanX <= 0 || spanY <= 0 || |
| 1293 | spanX < minSpanX || spanY < minSpanY) { |
| 1294 | return bestXY; |
| 1295 | } |
| 1296 | |
| 1297 | for (int y = 0; y < countY - (minSpanY - 1); y++) { |
Michael Jurka | c28de51 | 2010-08-13 11:27:44 -0700 | [diff] [blame] | 1298 | inner: |
Adam Cohen | d41fbf5 | 2012-02-16 23:53:59 -0800 | [diff] [blame] | 1299 | for (int x = 0; x < countX - (minSpanX - 1); x++) { |
| 1300 | int ySize = -1; |
| 1301 | int xSize = -1; |
Sebastian Franco | b57c0b2 | 2022-06-28 13:54:35 -0700 | [diff] [blame] | 1302 | if (!ignoreOccupied) { |
Adam Cohen | d41fbf5 | 2012-02-16 23:53:59 -0800 | [diff] [blame] | 1303 | // First, let's see if this thing fits anywhere |
| 1304 | for (int i = 0; i < minSpanX; i++) { |
| 1305 | for (int j = 0; j < minSpanY; j++) { |
Sunny Goyal | ff4ba2d | 2016-04-02 14:12:34 -0700 | [diff] [blame] | 1306 | if (mOccupied.cells[x + i][y + j]) { |
Adam Cohen | df03538 | 2011-04-11 17:22:04 -0700 | [diff] [blame] | 1307 | continue inner; |
| 1308 | } |
Michael Jurka | c28de51 | 2010-08-13 11:27:44 -0700 | [diff] [blame] | 1309 | } |
| 1310 | } |
Adam Cohen | d41fbf5 | 2012-02-16 23:53:59 -0800 | [diff] [blame] | 1311 | xSize = minSpanX; |
| 1312 | ySize = minSpanY; |
| 1313 | |
| 1314 | // We know that the item will fit at _some_ acceptable size, now let's see |
| 1315 | // how big we can make it. We'll alternate between incrementing x and y spans |
| 1316 | // until we hit a limit. |
| 1317 | boolean incX = true; |
| 1318 | boolean hitMaxX = xSize >= spanX; |
| 1319 | boolean hitMaxY = ySize >= spanY; |
| 1320 | while (!(hitMaxX && hitMaxY)) { |
| 1321 | if (incX && !hitMaxX) { |
| 1322 | for (int j = 0; j < ySize; j++) { |
Sunny Goyal | ff4ba2d | 2016-04-02 14:12:34 -0700 | [diff] [blame] | 1323 | if (x + xSize > countX -1 || mOccupied.cells[x + xSize][y + j]) { |
Adam Cohen | d41fbf5 | 2012-02-16 23:53:59 -0800 | [diff] [blame] | 1324 | // We can't move out horizontally |
| 1325 | hitMaxX = true; |
| 1326 | } |
| 1327 | } |
| 1328 | if (!hitMaxX) { |
| 1329 | xSize++; |
| 1330 | } |
| 1331 | } else if (!hitMaxY) { |
| 1332 | for (int i = 0; i < xSize; i++) { |
Sunny Goyal | ff4ba2d | 2016-04-02 14:12:34 -0700 | [diff] [blame] | 1333 | if (y + ySize > countY - 1 || mOccupied.cells[x + i][y + ySize]) { |
Adam Cohen | d41fbf5 | 2012-02-16 23:53:59 -0800 | [diff] [blame] | 1334 | // We can't move out vertically |
| 1335 | hitMaxY = true; |
| 1336 | } |
| 1337 | } |
| 1338 | if (!hitMaxY) { |
| 1339 | ySize++; |
| 1340 | } |
| 1341 | } |
| 1342 | hitMaxX |= xSize >= spanX; |
| 1343 | hitMaxY |= ySize >= spanY; |
| 1344 | incX = !incX; |
| 1345 | } |
Michael Jurka | c28de51 | 2010-08-13 11:27:44 -0700 | [diff] [blame] | 1346 | } |
Sunny Goyal | 2805e63 | 2015-05-20 15:35:32 -0700 | [diff] [blame] | 1347 | final int[] cellXY = mTmpPoint; |
Adam Cohen | e3e27a8 | 2011-04-15 12:07:39 -0700 | [diff] [blame] | 1348 | cellToCenterPoint(x, y, cellXY); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1349 | |
Adam Cohen | d41fbf5 | 2012-02-16 23:53:59 -0800 | [diff] [blame] | 1350 | // We verify that the current rect is not a sub-rect of any of our previous |
| 1351 | // candidates. In this case, the current rect is disqualified in favour of the |
| 1352 | // containing rect. |
Sebastian Franco | 4a92267 | 2022-10-27 16:42:24 -0700 | [diff] [blame] | 1353 | Rect currentRect = new Rect(x, y, x + xSize, y + ySize); |
Adam Cohen | d41fbf5 | 2012-02-16 23:53:59 -0800 | [diff] [blame] | 1354 | boolean contained = false; |
| 1355 | for (Rect r : validRegions) { |
| 1356 | if (r.contains(currentRect)) { |
| 1357 | contained = true; |
| 1358 | break; |
| 1359 | } |
| 1360 | } |
| 1361 | validRegions.push(currentRect); |
Sebastian Franco | b57c0b2 | 2022-06-28 13:54:35 -0700 | [diff] [blame] | 1362 | double distance = Math.hypot(cellXY[0] - relativeXPos, cellXY[1] - relativeYPos); |
Adam Cohen | 482ed82 | 2012-03-02 14:15:13 -0800 | [diff] [blame] | 1363 | |
Adam Cohen | d41fbf5 | 2012-02-16 23:53:59 -0800 | [diff] [blame] | 1364 | if ((distance <= bestDistance && !contained) || |
| 1365 | currentRect.contains(bestRect)) { |
Michael Jurka | c28de51 | 2010-08-13 11:27:44 -0700 | [diff] [blame] | 1366 | bestDistance = distance; |
| 1367 | bestXY[0] = x; |
| 1368 | bestXY[1] = y; |
Adam Cohen | d41fbf5 | 2012-02-16 23:53:59 -0800 | [diff] [blame] | 1369 | if (resultSpan != null) { |
| 1370 | resultSpan[0] = xSize; |
| 1371 | resultSpan[1] = ySize; |
| 1372 | } |
| 1373 | bestRect.set(currentRect); |
Michael Jurka | c28de51 | 2010-08-13 11:27:44 -0700 | [diff] [blame] | 1374 | } |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1375 | } |
| 1376 | } |
| 1377 | |
Adam Cohen | c0dcf59 | 2011-06-01 15:30:43 -0700 | [diff] [blame] | 1378 | // Return -1, -1 if no suitable location found |
| 1379 | if (bestDistance == Double.MAX_VALUE) { |
| 1380 | bestXY[0] = -1; |
| 1381 | bestXY[1] = -1; |
Jeff Sharkey | 7086428 | 2009-04-07 21:08:40 -0700 | [diff] [blame] | 1382 | } |
Adam Cohen | c0dcf59 | 2011-06-01 15:30:43 -0700 | [diff] [blame] | 1383 | return bestXY; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1384 | } |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 1385 | |
Sebastian Franco | e4c0345 | 2022-12-27 14:50:02 -0600 | [diff] [blame] | 1386 | public GridOccupancy getOccupied() { |
| 1387 | return mOccupied; |
| 1388 | } |
| 1389 | |
Adam Cohen | 482ed82 | 2012-03-02 14:15:13 -0800 | [diff] [blame] | 1390 | private void copySolutionToTempState(ItemConfiguration solution, View dragView) { |
Sunny Goyal | ff4ba2d | 2016-04-02 14:12:34 -0700 | [diff] [blame] | 1391 | mTmpOccupied.clear(); |
Adam Cohen | 482ed82 | 2012-03-02 14:15:13 -0800 | [diff] [blame] | 1392 | |
Michael Jurka | a52570f | 2012-03-20 03:18:20 -0700 | [diff] [blame] | 1393 | int childCount = mShortcutsAndWidgets.getChildCount(); |
Adam Cohen | 482ed82 | 2012-03-02 14:15:13 -0800 | [diff] [blame] | 1394 | for (int i = 0; i < childCount; i++) { |
Michael Jurka | a52570f | 2012-03-20 03:18:20 -0700 | [diff] [blame] | 1395 | View child = mShortcutsAndWidgets.getChildAt(i); |
Adam Cohen | 482ed82 | 2012-03-02 14:15:13 -0800 | [diff] [blame] | 1396 | if (child == dragView) continue; |
Sebastian Franco | d468299 | 2022-10-05 13:03:09 -0500 | [diff] [blame] | 1397 | CellLayoutLayoutParams lp = (CellLayoutLayoutParams) child.getLayoutParams(); |
Adam Cohen | 8baab35 | 2012-03-20 17:39:21 -0700 | [diff] [blame] | 1398 | CellAndSpan c = solution.map.get(child); |
| 1399 | if (c != null) { |
Sebastian Franco | 877088e | 2023-01-03 15:16:22 -0700 | [diff] [blame] | 1400 | lp.setTmpCellX(c.cellX); |
| 1401 | lp.setTmpCellY(c.cellY); |
Adam Cohen | 8baab35 | 2012-03-20 17:39:21 -0700 | [diff] [blame] | 1402 | lp.cellHSpan = c.spanX; |
| 1403 | lp.cellVSpan = c.spanY; |
Sunny Goyal | ff4ba2d | 2016-04-02 14:12:34 -0700 | [diff] [blame] | 1404 | mTmpOccupied.markCells(c, true); |
Adam Cohen | 482ed82 | 2012-03-02 14:15:13 -0800 | [diff] [blame] | 1405 | } |
| 1406 | } |
Sunny Goyal | ff4ba2d | 2016-04-02 14:12:34 -0700 | [diff] [blame] | 1407 | mTmpOccupied.markCells(solution, true); |
Adam Cohen | 482ed82 | 2012-03-02 14:15:13 -0800 | [diff] [blame] | 1408 | } |
| 1409 | |
| 1410 | private void animateItemsToSolution(ItemConfiguration solution, View dragView, boolean |
| 1411 | commitDragView) { |
| 1412 | |
Sunny Goyal | ff4ba2d | 2016-04-02 14:12:34 -0700 | [diff] [blame] | 1413 | GridOccupancy occupied = DESTRUCTIVE_REORDER ? mOccupied : mTmpOccupied; |
| 1414 | occupied.clear(); |
Adam Cohen | 482ed82 | 2012-03-02 14:15:13 -0800 | [diff] [blame] | 1415 | |
Michael Jurka | a52570f | 2012-03-20 03:18:20 -0700 | [diff] [blame] | 1416 | int childCount = mShortcutsAndWidgets.getChildCount(); |
Adam Cohen | 482ed82 | 2012-03-02 14:15:13 -0800 | [diff] [blame] | 1417 | for (int i = 0; i < childCount; i++) { |
Michael Jurka | a52570f | 2012-03-20 03:18:20 -0700 | [diff] [blame] | 1418 | View child = mShortcutsAndWidgets.getChildAt(i); |
Adam Cohen | 482ed82 | 2012-03-02 14:15:13 -0800 | [diff] [blame] | 1419 | if (child == dragView) continue; |
Adam Cohen | 8baab35 | 2012-03-20 17:39:21 -0700 | [diff] [blame] | 1420 | CellAndSpan c = solution.map.get(child); |
| 1421 | if (c != null) { |
Sunny Goyal | ff4ba2d | 2016-04-02 14:12:34 -0700 | [diff] [blame] | 1422 | animateChildToPosition(child, c.cellX, c.cellY, REORDER_ANIMATION_DURATION, 0, |
Adam Cohen | 19f3792 | 2012-03-21 11:59:11 -0700 | [diff] [blame] | 1423 | DESTRUCTIVE_REORDER, false); |
Sunny Goyal | ff4ba2d | 2016-04-02 14:12:34 -0700 | [diff] [blame] | 1424 | occupied.markCells(c, true); |
Adam Cohen | 482ed82 | 2012-03-02 14:15:13 -0800 | [diff] [blame] | 1425 | } |
| 1426 | } |
| 1427 | if (commitDragView) { |
Sunny Goyal | ff4ba2d | 2016-04-02 14:12:34 -0700 | [diff] [blame] | 1428 | occupied.markCells(solution, true); |
Adam Cohen | 482ed82 | 2012-03-02 14:15:13 -0800 | [diff] [blame] | 1429 | } |
| 1430 | } |
| 1431 | |
Adam Cohen | fa3c58f | 2013-12-06 16:10:55 -0800 | [diff] [blame] | 1432 | |
| 1433 | // This method starts or changes the reorder preview animations |
| 1434 | private void beginOrAdjustReorderPreviewAnimations(ItemConfiguration solution, |
Adam Cohen | 6508699 | 2020-02-19 08:40:49 -0800 | [diff] [blame] | 1435 | View dragView, int mode) { |
Adam Cohen | 19f3792 | 2012-03-21 11:59:11 -0700 | [diff] [blame] | 1436 | int childCount = mShortcutsAndWidgets.getChildCount(); |
Adam Cohen | 19f3792 | 2012-03-21 11:59:11 -0700 | [diff] [blame] | 1437 | for (int i = 0; i < childCount; i++) { |
| 1438 | View child = mShortcutsAndWidgets.getChildAt(i); |
| 1439 | if (child == dragView) continue; |
| 1440 | CellAndSpan c = solution.map.get(child); |
Adam Cohen | fa3c58f | 2013-12-06 16:10:55 -0800 | [diff] [blame] | 1441 | boolean skip = mode == ReorderPreviewAnimation.MODE_HINT && solution.intersectingViews |
| 1442 | != null && !solution.intersectingViews.contains(child); |
| 1443 | |
Adam Cohen | d916206 | 2020-03-24 16:35:35 -0700 | [diff] [blame] | 1444 | |
Sebastian Franco | d468299 | 2022-10-05 13:03:09 -0500 | [diff] [blame] | 1445 | CellLayoutLayoutParams lp = (CellLayoutLayoutParams) child.getLayoutParams(); |
Adam Cohen | d916206 | 2020-03-24 16:35:35 -0700 | [diff] [blame] | 1446 | if (c != null && !skip && (child instanceof Reorderable)) { |
Sunny Goyal | 82dfc15 | 2023-02-24 16:50:09 -0800 | [diff] [blame] | 1447 | ReorderPreviewAnimation rha = new ReorderPreviewAnimation(child, |
Sebastian Franco | 877088e | 2023-01-03 15:16:22 -0700 | [diff] [blame] | 1448 | mode, lp.getCellX(), lp.getCellY(), c.cellX, c.cellY, c.spanX, c.spanY); |
Adam Cohen | d024f98 | 2012-05-23 18:26:45 -0700 | [diff] [blame] | 1449 | rha.animate(); |
Adam Cohen | 19f3792 | 2012-03-21 11:59:11 -0700 | [diff] [blame] | 1450 | } |
| 1451 | } |
| 1452 | } |
| 1453 | |
Sunny Goyal | 849c6a2 | 2018-08-08 16:33:46 -0700 | [diff] [blame] | 1454 | private static final Property<ReorderPreviewAnimation, Float> ANIMATION_PROGRESS = |
| 1455 | new Property<ReorderPreviewAnimation, Float>(float.class, "animationProgress") { |
| 1456 | @Override |
| 1457 | public Float get(ReorderPreviewAnimation anim) { |
| 1458 | return anim.animationProgress; |
| 1459 | } |
| 1460 | |
| 1461 | @Override |
| 1462 | public void set(ReorderPreviewAnimation anim, Float progress) { |
| 1463 | anim.setAnimationProgress(progress); |
| 1464 | } |
| 1465 | }; |
| 1466 | |
Adam Cohen | fa3c58f | 2013-12-06 16:10:55 -0800 | [diff] [blame] | 1467 | // Class which represents the reorder preview animations. These animations show that an item is |
Adam Cohen | 19f3792 | 2012-03-21 11:59:11 -0700 | [diff] [blame] | 1468 | // in a temporary state, and hint at where the item will return to. |
Sunny Goyal | 82dfc15 | 2023-02-24 16:50:09 -0800 | [diff] [blame] | 1469 | class ReorderPreviewAnimation<T extends View & Reorderable> { |
| 1470 | final T child; |
Adam Cohen | d024f98 | 2012-05-23 18:26:45 -0700 | [diff] [blame] | 1471 | float finalDeltaX; |
| 1472 | float finalDeltaY; |
| 1473 | float initDeltaX; |
| 1474 | float initDeltaY; |
Rajeev Kumar | 9962dbe | 2017-06-12 12:16:20 -0700 | [diff] [blame] | 1475 | final float finalScale; |
Adam Cohen | d024f98 | 2012-05-23 18:26:45 -0700 | [diff] [blame] | 1476 | float initScale; |
Rajeev Kumar | 9962dbe | 2017-06-12 12:16:20 -0700 | [diff] [blame] | 1477 | final int mode; |
Adam Cohen | fa3c58f | 2013-12-06 16:10:55 -0800 | [diff] [blame] | 1478 | boolean repeating = false; |
| 1479 | private static final int PREVIEW_DURATION = 300; |
| 1480 | private static final int HINT_DURATION = Workspace.REORDER_TIMEOUT; |
| 1481 | |
Jon Miranda | 2126691 | 2016-12-19 14:12:05 -0800 | [diff] [blame] | 1482 | private static final float CHILD_DIVIDEND = 4.0f; |
| 1483 | |
Adam Cohen | fa3c58f | 2013-12-06 16:10:55 -0800 | [diff] [blame] | 1484 | public static final int MODE_HINT = 0; |
| 1485 | public static final int MODE_PREVIEW = 1; |
| 1486 | |
Sunny Goyal | 849c6a2 | 2018-08-08 16:33:46 -0700 | [diff] [blame] | 1487 | float animationProgress = 0; |
Sunny Goyal | f0b6db7 | 2018-08-13 16:10:14 -0700 | [diff] [blame] | 1488 | ValueAnimator a; |
Adam Cohen | 19f3792 | 2012-03-21 11:59:11 -0700 | [diff] [blame] | 1489 | |
Sunny Goyal | 82dfc15 | 2023-02-24 16:50:09 -0800 | [diff] [blame] | 1490 | ReorderPreviewAnimation(View childView, int mode, int cellX0, int cellY0, |
Adam Cohen | d916206 | 2020-03-24 16:35:35 -0700 | [diff] [blame] | 1491 | int cellX1, int cellY1, int spanX, int spanY) { |
Adam Cohen | 19f3792 | 2012-03-21 11:59:11 -0700 | [diff] [blame] | 1492 | regionToCenterPoint(cellX0, cellY0, spanX, spanY, mTmpPoint); |
| 1493 | final int x0 = mTmpPoint[0]; |
| 1494 | final int y0 = mTmpPoint[1]; |
| 1495 | regionToCenterPoint(cellX1, cellY1, spanX, spanY, mTmpPoint); |
| 1496 | final int x1 = mTmpPoint[0]; |
| 1497 | final int y1 = mTmpPoint[1]; |
| 1498 | final int dX = x1 - x0; |
| 1499 | final int dY = y1 - y0; |
Jon Miranda | 2126691 | 2016-12-19 14:12:05 -0800 | [diff] [blame] | 1500 | |
Sunny Goyal | 82dfc15 | 2023-02-24 16:50:09 -0800 | [diff] [blame] | 1501 | this.child = (T) childView; |
Jon Miranda | 2126691 | 2016-12-19 14:12:05 -0800 | [diff] [blame] | 1502 | this.mode = mode; |
Adam Cohen | d916206 | 2020-03-24 16:35:35 -0700 | [diff] [blame] | 1503 | finalDeltaX = 0; |
| 1504 | finalDeltaY = 0; |
Adam Cohen | 6508699 | 2020-02-19 08:40:49 -0800 | [diff] [blame] | 1505 | |
Sunny Goyal | 82dfc15 | 2023-02-24 16:50:09 -0800 | [diff] [blame] | 1506 | MultiTranslateDelegate mtd = child.getTranslateDelegate(); |
| 1507 | initDeltaX = mtd.getTranslationX(INDEX_REORDER_BOUNCE_OFFSET).getValue(); |
| 1508 | initDeltaY = mtd.getTranslationY(INDEX_REORDER_BOUNCE_OFFSET).getValue(); |
Adam Cohen | 1d13c0b | 2020-04-21 16:29:12 -0700 | [diff] [blame] | 1509 | initScale = child.getReorderBounceScale(); |
Sunny Goyal | 82dfc15 | 2023-02-24 16:50:09 -0800 | [diff] [blame] | 1510 | finalScale = mChildScale - (CHILD_DIVIDEND / child.getWidth()) * initScale; |
Adam Cohen | d916206 | 2020-03-24 16:35:35 -0700 | [diff] [blame] | 1511 | |
Adam Cohen | fa3c58f | 2013-12-06 16:10:55 -0800 | [diff] [blame] | 1512 | int dir = mode == MODE_HINT ? -1 : 1; |
Adam Cohen | 19f3792 | 2012-03-21 11:59:11 -0700 | [diff] [blame] | 1513 | if (dX == dY && dX == 0) { |
| 1514 | } else { |
| 1515 | if (dY == 0) { |
Adam Cohen | d916206 | 2020-03-24 16:35:35 -0700 | [diff] [blame] | 1516 | finalDeltaX = -dir * Math.signum(dX) * mReorderPreviewAnimationMagnitude; |
Adam Cohen | 19f3792 | 2012-03-21 11:59:11 -0700 | [diff] [blame] | 1517 | } else if (dX == 0) { |
Adam Cohen | d916206 | 2020-03-24 16:35:35 -0700 | [diff] [blame] | 1518 | finalDeltaY = -dir * Math.signum(dY) * mReorderPreviewAnimationMagnitude; |
Adam Cohen | 19f3792 | 2012-03-21 11:59:11 -0700 | [diff] [blame] | 1519 | } else { |
| 1520 | double angle = Math.atan( (float) (dY) / dX); |
Adam Cohen | d916206 | 2020-03-24 16:35:35 -0700 | [diff] [blame] | 1521 | finalDeltaX = (int) (-dir * Math.signum(dX) |
| 1522 | * Math.abs(Math.cos(angle) * mReorderPreviewAnimationMagnitude)); |
| 1523 | finalDeltaY = (int) (-dir * Math.signum(dY) |
| 1524 | * Math.abs(Math.sin(angle) * mReorderPreviewAnimationMagnitude)); |
Adam Cohen | 19f3792 | 2012-03-21 11:59:11 -0700 | [diff] [blame] | 1525 | } |
| 1526 | } |
Jon Miranda | 2126691 | 2016-12-19 14:12:05 -0800 | [diff] [blame] | 1527 | } |
| 1528 | |
Adam Cohen | d916206 | 2020-03-24 16:35:35 -0700 | [diff] [blame] | 1529 | void setInitialAnimationValuesToBaseline() { |
| 1530 | initScale = mChildScale; |
| 1531 | initDeltaX = 0; |
| 1532 | initDeltaY = 0; |
Adam Cohen | 19f3792 | 2012-03-21 11:59:11 -0700 | [diff] [blame] | 1533 | } |
| 1534 | |
Adam Cohen | d024f98 | 2012-05-23 18:26:45 -0700 | [diff] [blame] | 1535 | void animate() { |
Adam Cohen | d916206 | 2020-03-24 16:35:35 -0700 | [diff] [blame] | 1536 | boolean noMovement = (finalDeltaX == 0) && (finalDeltaY == 0); |
Jon Miranda | 2126691 | 2016-12-19 14:12:05 -0800 | [diff] [blame] | 1537 | |
Adam Cohen | 19f3792 | 2012-03-21 11:59:11 -0700 | [diff] [blame] | 1538 | if (mShakeAnimators.containsKey(child)) { |
Adam Cohen | fa3c58f | 2013-12-06 16:10:55 -0800 | [diff] [blame] | 1539 | ReorderPreviewAnimation oldAnimation = mShakeAnimators.get(child); |
Adam Cohen | 19f3792 | 2012-03-21 11:59:11 -0700 | [diff] [blame] | 1540 | mShakeAnimators.remove(child); |
Adam Cohen | d916206 | 2020-03-24 16:35:35 -0700 | [diff] [blame] | 1541 | |
Jon Miranda | 2126691 | 2016-12-19 14:12:05 -0800 | [diff] [blame] | 1542 | if (noMovement) { |
Adam Cohen | d916206 | 2020-03-24 16:35:35 -0700 | [diff] [blame] | 1543 | // A previous animation for this item exists, and no new animation will exist. |
| 1544 | // Finish the old animation smoothly. |
| 1545 | oldAnimation.finishAnimation(); |
Adam Cohen | e7587d2 | 2012-05-24 18:50:02 -0700 | [diff] [blame] | 1546 | return; |
Adam Cohen | d916206 | 2020-03-24 16:35:35 -0700 | [diff] [blame] | 1547 | } else { |
| 1548 | // A previous animation for this item exists, and a new one will exist. Stop |
| 1549 | // the old animation in its tracks, and proceed with the new one. |
| 1550 | oldAnimation.cancel(); |
Adam Cohen | e7587d2 | 2012-05-24 18:50:02 -0700 | [diff] [blame] | 1551 | } |
Adam Cohen | 19f3792 | 2012-03-21 11:59:11 -0700 | [diff] [blame] | 1552 | } |
Jon Miranda | 2126691 | 2016-12-19 14:12:05 -0800 | [diff] [blame] | 1553 | if (noMovement) { |
Adam Cohen | 19f3792 | 2012-03-21 11:59:11 -0700 | [diff] [blame] | 1554 | return; |
| 1555 | } |
Adam Cohen | d916206 | 2020-03-24 16:35:35 -0700 | [diff] [blame] | 1556 | |
Sunny Goyal | 849c6a2 | 2018-08-08 16:33:46 -0700 | [diff] [blame] | 1557 | ValueAnimator va = ObjectAnimator.ofFloat(this, ANIMATION_PROGRESS, 0, 1); |
Adam Cohen | e7587d2 | 2012-05-24 18:50:02 -0700 | [diff] [blame] | 1558 | a = va; |
Tony Wickham | 9e0702f | 2015-09-02 14:45:39 -0700 | [diff] [blame] | 1559 | |
| 1560 | // Animations are disabled in power save mode, causing the repeated animation to jump |
| 1561 | // spastically between beginning and end states. Since this looks bad, we don't repeat |
| 1562 | // the animation in power save mode. |
Sunny Goyal | eaf7a95 | 2020-07-29 16:54:20 -0700 | [diff] [blame] | 1563 | if (areAnimatorsEnabled()) { |
Tony Wickham | 9e0702f | 2015-09-02 14:45:39 -0700 | [diff] [blame] | 1564 | va.setRepeatMode(ValueAnimator.REVERSE); |
| 1565 | va.setRepeatCount(ValueAnimator.INFINITE); |
| 1566 | } |
| 1567 | |
Adam Cohen | fa3c58f | 2013-12-06 16:10:55 -0800 | [diff] [blame] | 1568 | va.setDuration(mode == MODE_HINT ? HINT_DURATION : PREVIEW_DURATION); |
Adam Cohen | d024f98 | 2012-05-23 18:26:45 -0700 | [diff] [blame] | 1569 | va.setStartDelay((int) (Math.random() * 60)); |
Adam Cohen | 19f3792 | 2012-03-21 11:59:11 -0700 | [diff] [blame] | 1570 | va.addListener(new AnimatorListenerAdapter() { |
| 1571 | public void onAnimationRepeat(Animator animation) { |
Adam Cohen | 19f3792 | 2012-03-21 11:59:11 -0700 | [diff] [blame] | 1572 | // We make sure to end only after a full period |
Adam Cohen | d916206 | 2020-03-24 16:35:35 -0700 | [diff] [blame] | 1573 | setInitialAnimationValuesToBaseline(); |
Adam Cohen | fa3c58f | 2013-12-06 16:10:55 -0800 | [diff] [blame] | 1574 | repeating = true; |
Adam Cohen | 19f3792 | 2012-03-21 11:59:11 -0700 | [diff] [blame] | 1575 | } |
| 1576 | }); |
Adam Cohen | 19f3792 | 2012-03-21 11:59:11 -0700 | [diff] [blame] | 1577 | mShakeAnimators.put(child, this); |
| 1578 | va.start(); |
| 1579 | } |
| 1580 | |
Sunny Goyal | 849c6a2 | 2018-08-08 16:33:46 -0700 | [diff] [blame] | 1581 | private void setAnimationProgress(float progress) { |
| 1582 | animationProgress = progress; |
| 1583 | float r1 = (mode == MODE_HINT && repeating) ? 1.0f : animationProgress; |
| 1584 | float x = r1 * finalDeltaX + (1 - r1) * initDeltaX; |
| 1585 | float y = r1 * finalDeltaY + (1 - r1) * initDeltaY; |
Sunny Goyal | 82dfc15 | 2023-02-24 16:50:09 -0800 | [diff] [blame] | 1586 | child.getTranslateDelegate().setTranslation(INDEX_REORDER_BOUNCE_OFFSET, x, y); |
Sunny Goyal | 849c6a2 | 2018-08-08 16:33:46 -0700 | [diff] [blame] | 1587 | float s = animationProgress * finalScale + (1 - animationProgress) * initScale; |
Adam Cohen | 1d13c0b | 2020-04-21 16:29:12 -0700 | [diff] [blame] | 1588 | child.setReorderBounceScale(s); |
Sunny Goyal | 849c6a2 | 2018-08-08 16:33:46 -0700 | [diff] [blame] | 1589 | } |
| 1590 | |
Adam Cohen | d024f98 | 2012-05-23 18:26:45 -0700 | [diff] [blame] | 1591 | private void cancel() { |
Adam Cohen | e7587d2 | 2012-05-24 18:50:02 -0700 | [diff] [blame] | 1592 | if (a != null) { |
| 1593 | a.cancel(); |
| 1594 | } |
Adam Cohen | 19f3792 | 2012-03-21 11:59:11 -0700 | [diff] [blame] | 1595 | } |
Adam Cohen | e7587d2 | 2012-05-24 18:50:02 -0700 | [diff] [blame] | 1596 | |
Adam Cohen | d916206 | 2020-03-24 16:35:35 -0700 | [diff] [blame] | 1597 | /** |
| 1598 | * Smoothly returns the item to its baseline position / scale |
| 1599 | */ |
| 1600 | @Thunk void finishAnimation() { |
Adam Cohen | e7587d2 | 2012-05-24 18:50:02 -0700 | [diff] [blame] | 1601 | if (a != null) { |
| 1602 | a.cancel(); |
| 1603 | } |
Brandon Keely | 50e6e56 | 2012-05-08 16:28:49 -0700 | [diff] [blame] | 1604 | |
Adam Cohen | d916206 | 2020-03-24 16:35:35 -0700 | [diff] [blame] | 1605 | setInitialAnimationValuesToBaseline(); |
| 1606 | ValueAnimator va = ObjectAnimator.ofFloat(this, ANIMATION_PROGRESS, |
| 1607 | animationProgress, 0); |
| 1608 | a = va; |
Kateryna Ivanova | 7120373 | 2023-05-24 15:09:00 +0000 | [diff] [blame] | 1609 | a.setInterpolator(DECELERATE_1_5); |
Adam Cohen | d916206 | 2020-03-24 16:35:35 -0700 | [diff] [blame] | 1610 | a.setDuration(REORDER_ANIMATION_DURATION); |
Sunny Goyal | 5d2fc32 | 2015-07-06 22:52:49 -0700 | [diff] [blame] | 1611 | a.start(); |
Brandon Keely | 50e6e56 | 2012-05-08 16:28:49 -0700 | [diff] [blame] | 1612 | } |
Adam Cohen | 19f3792 | 2012-03-21 11:59:11 -0700 | [diff] [blame] | 1613 | } |
| 1614 | |
Adam Cohen | fa3c58f | 2013-12-06 16:10:55 -0800 | [diff] [blame] | 1615 | private void completeAndClearReorderPreviewAnimations() { |
| 1616 | for (ReorderPreviewAnimation a: mShakeAnimators.values()) { |
Adam Cohen | d916206 | 2020-03-24 16:35:35 -0700 | [diff] [blame] | 1617 | a.finishAnimation(); |
Adam Cohen | 19f3792 | 2012-03-21 11:59:11 -0700 | [diff] [blame] | 1618 | } |
| 1619 | mShakeAnimators.clear(); |
| 1620 | } |
| 1621 | |
Sunny Goyal | 711c596 | 2021-06-23 12:36:18 -0700 | [diff] [blame] | 1622 | private void commitTempPlacement(View dragView) { |
Sunny Goyal | ff4ba2d | 2016-04-02 14:12:34 -0700 | [diff] [blame] | 1623 | mTmpOccupied.copyTo(mOccupied); |
Sunny Goyal | aa8ef11 | 2015-06-12 20:04:41 -0700 | [diff] [blame] | 1624 | |
Andras Kloczl | 2dacbee | 2022-02-21 16:53:28 +0000 | [diff] [blame] | 1625 | int screenId = getWorkspace().getIdForScreen(this); |
Sunny Goyal | aa8ef11 | 2015-06-12 20:04:41 -0700 | [diff] [blame] | 1626 | int container = Favorites.CONTAINER_DESKTOP; |
| 1627 | |
Sunny Goyal | c13403c | 2016-11-18 23:44:48 -0800 | [diff] [blame] | 1628 | if (mContainerType == HOTSEAT) { |
Sunny Goyal | aa8ef11 | 2015-06-12 20:04:41 -0700 | [diff] [blame] | 1629 | screenId = -1; |
| 1630 | container = Favorites.CONTAINER_HOTSEAT; |
| 1631 | } |
| 1632 | |
Michael Jurka | a52570f | 2012-03-20 03:18:20 -0700 | [diff] [blame] | 1633 | int childCount = mShortcutsAndWidgets.getChildCount(); |
Adam Cohen | 482ed82 | 2012-03-02 14:15:13 -0800 | [diff] [blame] | 1634 | for (int i = 0; i < childCount; i++) { |
Adam Cohen | ea889a2 | 2012-03-27 16:45:39 -0700 | [diff] [blame] | 1635 | View child = mShortcutsAndWidgets.getChildAt(i); |
Sebastian Franco | d468299 | 2022-10-05 13:03:09 -0500 | [diff] [blame] | 1636 | CellLayoutLayoutParams lp = (CellLayoutLayoutParams) child.getLayoutParams(); |
Adam Cohen | ea889a2 | 2012-03-27 16:45:39 -0700 | [diff] [blame] | 1637 | ItemInfo info = (ItemInfo) child.getTag(); |
Adam Cohen | 2acce88 | 2012-03-28 19:03:19 -0700 | [diff] [blame] | 1638 | // We do a null check here because the item info can be null in the case of the |
| 1639 | // AllApps button in the hotseat. |
Sunny Goyal | 711c596 | 2021-06-23 12:36:18 -0700 | [diff] [blame] | 1640 | if (info != null && child != dragView) { |
Sunny Goyal | 669b71f | 2023-01-27 14:37:07 -0800 | [diff] [blame] | 1641 | CellPos presenterPos = mActivity.getCellPosMapper().mapModelToPresenter(info); |
| 1642 | final boolean requiresDbUpdate = (presenterPos.cellX != lp.getTmpCellX() |
| 1643 | || presenterPos.cellY != lp.getTmpCellY() || info.spanX != lp.cellHSpan |
| 1644 | || info.spanY != lp.cellVSpan || presenterPos.screenId != screenId); |
Sunny Goyal | aa8ef11 | 2015-06-12 20:04:41 -0700 | [diff] [blame] | 1645 | |
Sebastian Franco | 877088e | 2023-01-03 15:16:22 -0700 | [diff] [blame] | 1646 | lp.setCellX(lp.getTmpCellX()); |
Sebastian Franco | 877088e | 2023-01-03 15:16:22 -0700 | [diff] [blame] | 1647 | lp.setCellY(lp.getTmpCellY()); |
Sunny Goyal | aa8ef11 | 2015-06-12 20:04:41 -0700 | [diff] [blame] | 1648 | if (requiresDbUpdate) { |
Sunny Goyal | ab770a1 | 2018-11-14 15:17:26 -0800 | [diff] [blame] | 1649 | Launcher.cast(mActivity).getModelWriter().modifyItemInDatabase(info, container, |
Sunny Goyal | 669b71f | 2023-01-27 14:37:07 -0800 | [diff] [blame] | 1650 | screenId, lp.getCellX(), lp.getCellY(), lp.cellHSpan, lp.cellVSpan); |
Sunny Goyal | aa8ef11 | 2015-06-12 20:04:41 -0700 | [diff] [blame] | 1651 | } |
Adam Cohen | 2acce88 | 2012-03-28 19:03:19 -0700 | [diff] [blame] | 1652 | } |
Adam Cohen | 482ed82 | 2012-03-02 14:15:13 -0800 | [diff] [blame] | 1653 | } |
| 1654 | } |
| 1655 | |
Sunny Goyal | f7a29e8 | 2015-04-24 15:20:43 -0700 | [diff] [blame] | 1656 | private void setUseTempCoords(boolean useTempCoords) { |
Michael Jurka | a52570f | 2012-03-20 03:18:20 -0700 | [diff] [blame] | 1657 | int childCount = mShortcutsAndWidgets.getChildCount(); |
Adam Cohen | 482ed82 | 2012-03-02 14:15:13 -0800 | [diff] [blame] | 1658 | for (int i = 0; i < childCount; i++) { |
Sebastian Franco | d468299 | 2022-10-05 13:03:09 -0500 | [diff] [blame] | 1659 | CellLayoutLayoutParams lp = (CellLayoutLayoutParams) mShortcutsAndWidgets.getChildAt( |
| 1660 | i).getLayoutParams(); |
Adam Cohen | 482ed82 | 2012-03-02 14:15:13 -0800 | [diff] [blame] | 1661 | lp.useTmpCoords = useTempCoords; |
| 1662 | } |
| 1663 | } |
| 1664 | |
Adam Cohen | 19f3792 | 2012-03-21 11:59:11 -0700 | [diff] [blame] | 1665 | // For a given cell and span, fetch the set of views intersecting the region. |
Sebastian Franco | e4c0345 | 2022-12-27 14:50:02 -0600 | [diff] [blame] | 1666 | public void getViewsIntersectingRegion(int cellX, int cellY, int spanX, int spanY, |
Adam Cohen | 19f3792 | 2012-03-21 11:59:11 -0700 | [diff] [blame] | 1667 | View dragView, Rect boundingRect, ArrayList<View> intersectingViews) { |
| 1668 | if (boundingRect != null) { |
| 1669 | boundingRect.set(cellX, cellY, cellX + spanX, cellY + spanY); |
| 1670 | } |
| 1671 | intersectingViews.clear(); |
| 1672 | Rect r0 = new Rect(cellX, cellY, cellX + spanX, cellY + spanY); |
| 1673 | Rect r1 = new Rect(); |
| 1674 | final int count = mShortcutsAndWidgets.getChildCount(); |
| 1675 | for (int i = 0; i < count; i++) { |
| 1676 | View child = mShortcutsAndWidgets.getChildAt(i); |
| 1677 | if (child == dragView) continue; |
Sebastian Franco | d468299 | 2022-10-05 13:03:09 -0500 | [diff] [blame] | 1678 | CellLayoutLayoutParams |
| 1679 | lp = (CellLayoutLayoutParams) child.getLayoutParams(); |
Sebastian Franco | 877088e | 2023-01-03 15:16:22 -0700 | [diff] [blame] | 1680 | r1.set(lp.getCellX(), lp.getCellY(), lp.getCellX() + lp.cellHSpan, |
| 1681 | lp.getCellY() + lp.cellVSpan); |
Adam Cohen | 19f3792 | 2012-03-21 11:59:11 -0700 | [diff] [blame] | 1682 | if (Rect.intersects(r0, r1)) { |
| 1683 | mIntersectingViews.add(child); |
| 1684 | if (boundingRect != null) { |
| 1685 | boundingRect.union(r1); |
| 1686 | } |
| 1687 | } |
| 1688 | } |
| 1689 | } |
| 1690 | |
Sebastian Franco | e4c0345 | 2022-12-27 14:50:02 -0600 | [diff] [blame] | 1691 | public boolean isNearestDropLocationOccupied(int pixelX, int pixelY, int spanX, int spanY, |
Adam Cohen | 19f3792 | 2012-03-21 11:59:11 -0700 | [diff] [blame] | 1692 | View dragView, int[] result) { |
Sebastián Franco | f9a6ac2 | 2022-11-15 22:56:37 +0000 | [diff] [blame] | 1693 | result = findNearestAreaIgnoreOccupied(pixelX, pixelY, spanX, spanY, result); |
Adam Cohen | 19f3792 | 2012-03-21 11:59:11 -0700 | [diff] [blame] | 1694 | getViewsIntersectingRegion(result[0], result[1], spanX, spanY, dragView, null, |
| 1695 | mIntersectingViews); |
| 1696 | return !mIntersectingViews.isEmpty(); |
| 1697 | } |
| 1698 | |
| 1699 | void revertTempState() { |
Adam Cohen | fa3c58f | 2013-12-06 16:10:55 -0800 | [diff] [blame] | 1700 | completeAndClearReorderPreviewAnimations(); |
| 1701 | if (isItemPlacementDirty() && !DESTRUCTIVE_REORDER) { |
| 1702 | final int count = mShortcutsAndWidgets.getChildCount(); |
| 1703 | for (int i = 0; i < count; i++) { |
| 1704 | View child = mShortcutsAndWidgets.getChildAt(i); |
Sebastian Franco | d468299 | 2022-10-05 13:03:09 -0500 | [diff] [blame] | 1705 | CellLayoutLayoutParams |
| 1706 | lp = (CellLayoutLayoutParams) child.getLayoutParams(); |
Sebastian Franco | 877088e | 2023-01-03 15:16:22 -0700 | [diff] [blame] | 1707 | if (lp.getTmpCellX() != lp.getCellX() || lp.getTmpCellY() != lp.getCellY()) { |
| 1708 | lp.setTmpCellX(lp.getCellX()); |
| 1709 | lp.setTmpCellY(lp.getCellY()); |
| 1710 | animateChildToPosition(child, lp.getCellX(), lp.getCellY(), |
| 1711 | REORDER_ANIMATION_DURATION, 0, false, false); |
Adam Cohen | fa3c58f | 2013-12-06 16:10:55 -0800 | [diff] [blame] | 1712 | } |
Adam Cohen | 19f3792 | 2012-03-21 11:59:11 -0700 | [diff] [blame] | 1713 | } |
Adam Cohen | fa3c58f | 2013-12-06 16:10:55 -0800 | [diff] [blame] | 1714 | setItemPlacementDirty(false); |
Adam Cohen | 19f3792 | 2012-03-21 11:59:11 -0700 | [diff] [blame] | 1715 | } |
Adam Cohen | 19f3792 | 2012-03-21 11:59:11 -0700 | [diff] [blame] | 1716 | } |
| 1717 | |
Adam Cohen | bebf042 | 2012-04-11 18:06:28 -0700 | [diff] [blame] | 1718 | boolean createAreaForResize(int cellX, int cellY, int spanX, int spanY, |
| 1719 | View dragView, int[] direction, boolean commit) { |
| 1720 | int[] pixelXY = new int[2]; |
| 1721 | regionToCenterPoint(cellX, cellY, spanX, spanY, pixelXY); |
| 1722 | |
| 1723 | // First we determine if things have moved enough to cause a different layout |
Adam Cohen | fa3c58f | 2013-12-06 16:10:55 -0800 | [diff] [blame] | 1724 | ItemConfiguration swapSolution = findReorderSolution(pixelXY[0], pixelXY[1], spanX, spanY, |
Sebastian Franco | f153d91 | 2022-04-22 16:15:27 -0500 | [diff] [blame] | 1725 | spanX, spanY, direction, dragView, true, new ItemConfiguration()); |
Adam Cohen | bebf042 | 2012-04-11 18:06:28 -0700 | [diff] [blame] | 1726 | |
| 1727 | setUseTempCoords(true); |
| 1728 | if (swapSolution != null && swapSolution.isSolution) { |
| 1729 | // If we're just testing for a possible location (MODE_ACCEPT_DROP), we don't bother |
| 1730 | // committing anything or animating anything as we just want to determine if a solution |
| 1731 | // exists |
| 1732 | copySolutionToTempState(swapSolution, dragView); |
| 1733 | setItemPlacementDirty(true); |
| 1734 | animateItemsToSolution(swapSolution, dragView, commit); |
| 1735 | |
| 1736 | if (commit) { |
Sunny Goyal | 711c596 | 2021-06-23 12:36:18 -0700 | [diff] [blame] | 1737 | commitTempPlacement(null); |
Adam Cohen | fa3c58f | 2013-12-06 16:10:55 -0800 | [diff] [blame] | 1738 | completeAndClearReorderPreviewAnimations(); |
Adam Cohen | bebf042 | 2012-04-11 18:06:28 -0700 | [diff] [blame] | 1739 | setItemPlacementDirty(false); |
| 1740 | } else { |
Adam Cohen | fa3c58f | 2013-12-06 16:10:55 -0800 | [diff] [blame] | 1741 | beginOrAdjustReorderPreviewAnimations(swapSolution, dragView, |
Adam Cohen | 6508699 | 2020-02-19 08:40:49 -0800 | [diff] [blame] | 1742 | ReorderPreviewAnimation.MODE_PREVIEW); |
Adam Cohen | bebf042 | 2012-04-11 18:06:28 -0700 | [diff] [blame] | 1743 | } |
| 1744 | mShortcutsAndWidgets.requestLayout(); |
| 1745 | } |
| 1746 | return swapSolution.isSolution; |
| 1747 | } |
| 1748 | |
Sebastian Franco | 9cab1c3 | 2022-10-25 17:28:54 -0700 | [diff] [blame] | 1749 | /** |
| 1750 | * Find a vacant area that will fit the given bounds nearest the requested |
| 1751 | * cell location, and will also weigh in a suggested direction vector of the |
| 1752 | * desired location. This method computers distance based on unit grid distances, |
| 1753 | * not pixel distances. |
| 1754 | * |
| 1755 | * @param cellX The X cell nearest to which you want to search for a vacant area. |
| 1756 | * @param cellY The Y cell nearest which you want to search for a vacant area. |
| 1757 | * @param spanX Horizontal span of the object. |
| 1758 | * @param spanY Vertical span of the object. |
| 1759 | * @param direction The favored direction in which the views should move from x, y |
| 1760 | * @param occupied The array which represents which cells in the CellLayout are occupied |
| 1761 | * @param blockOccupied The array which represents which cells in the specified block (cellX, |
| 1762 | * cellY, spanX, spanY) are occupied. This is used when try to move a group of views. |
| 1763 | * @param result Array in which to place the result, or null (in which case a new array will |
| 1764 | * be allocated) |
| 1765 | * @return The X, Y cell of a vacant area that can contain this object, |
| 1766 | * nearest the requested location. |
| 1767 | */ |
| 1768 | private int[] findNearestArea(int cellX, int cellY, int spanX, int spanY, int[] direction, |
| 1769 | boolean[][] occupied, boolean blockOccupied[][], int[] result) { |
| 1770 | // Keep track of best-scoring drop area |
| 1771 | final int[] bestXY = result != null ? result : new int[2]; |
| 1772 | float bestDistance = Float.MAX_VALUE; |
| 1773 | int bestDirectionScore = Integer.MIN_VALUE; |
Adam Cohen | 482ed82 | 2012-03-02 14:15:13 -0800 | [diff] [blame] | 1774 | |
Sebastian Franco | 9cab1c3 | 2022-10-25 17:28:54 -0700 | [diff] [blame] | 1775 | final int countX = mCountX; |
| 1776 | final int countY = mCountY; |
| 1777 | |
| 1778 | for (int y = 0; y < countY - (spanY - 1); y++) { |
| 1779 | inner: |
| 1780 | for (int x = 0; x < countX - (spanX - 1); x++) { |
| 1781 | // First, let's see if this thing fits anywhere |
| 1782 | for (int i = 0; i < spanX; i++) { |
| 1783 | for (int j = 0; j < spanY; j++) { |
| 1784 | if (occupied[x + i][y + j] && (blockOccupied == null || blockOccupied[i][j])) { |
| 1785 | continue inner; |
| 1786 | } |
| 1787 | } |
| 1788 | } |
| 1789 | |
| 1790 | float distance = (float) Math.hypot(x - cellX, y - cellY); |
| 1791 | int[] curDirection = mTmpPoint; |
| 1792 | computeDirectionVector(x - cellX, y - cellY, curDirection); |
| 1793 | // The direction score is just the dot product of the two candidate direction |
| 1794 | // and that passed in. |
| 1795 | int curDirectionScore = direction[0] * curDirection[0] + |
| 1796 | direction[1] * curDirection[1]; |
| 1797 | if (Float.compare(distance, bestDistance) < 0 || |
| 1798 | (Float.compare(distance, bestDistance) == 0 |
| 1799 | && curDirectionScore > bestDirectionScore)) { |
| 1800 | bestDistance = distance; |
| 1801 | bestDirectionScore = curDirectionScore; |
| 1802 | bestXY[0] = x; |
| 1803 | bestXY[1] = y; |
| 1804 | } |
Adam Cohen | 19f3792 | 2012-03-21 11:59:11 -0700 | [diff] [blame] | 1805 | } |
Adam Cohen | 19f3792 | 2012-03-21 11:59:11 -0700 | [diff] [blame] | 1806 | } |
| 1807 | |
Sebastian Franco | 9cab1c3 | 2022-10-25 17:28:54 -0700 | [diff] [blame] | 1808 | // Return -1, -1 if no suitable location found |
| 1809 | if (bestDistance == Float.MAX_VALUE) { |
| 1810 | bestXY[0] = -1; |
| 1811 | bestXY[1] = -1; |
Sebastian Franco | 53a15a4 | 2022-10-25 17:28:54 -0700 | [diff] [blame] | 1812 | } |
Sebastian Franco | 9cab1c3 | 2022-10-25 17:28:54 -0700 | [diff] [blame] | 1813 | return bestXY; |
| 1814 | } |
| 1815 | |
| 1816 | private boolean addViewToTempLocation(View v, Rect rectOccupiedByPotentialDrop, |
| 1817 | int[] direction, ItemConfiguration currentState) { |
| 1818 | CellAndSpan c = currentState.map.get(v); |
| 1819 | boolean success = false; |
| 1820 | mTmpOccupied.markCells(c, false); |
| 1821 | mTmpOccupied.markCells(rectOccupiedByPotentialDrop, true); |
| 1822 | |
| 1823 | findNearestArea(c.cellX, c.cellY, c.spanX, c.spanY, direction, |
| 1824 | mTmpOccupied.cells, null, mTempLocation); |
| 1825 | |
| 1826 | if (mTempLocation[0] >= 0 && mTempLocation[1] >= 0) { |
| 1827 | c.cellX = mTempLocation[0]; |
| 1828 | c.cellY = mTempLocation[1]; |
| 1829 | success = true; |
| 1830 | } |
| 1831 | mTmpOccupied.markCells(c, true); |
| 1832 | return success; |
| 1833 | } |
| 1834 | |
| 1835 | private boolean pushViewsToTempLocation(ArrayList<View> views, Rect rectOccupiedByPotentialDrop, |
| 1836 | int[] direction, View dragView, ItemConfiguration currentState) { |
| 1837 | |
| 1838 | ViewCluster cluster = new ViewCluster(views, currentState); |
| 1839 | Rect clusterRect = cluster.getBoundingRect(); |
| 1840 | int whichEdge; |
| 1841 | int pushDistance; |
| 1842 | boolean fail = false; |
| 1843 | |
| 1844 | // Determine the edge of the cluster that will be leading the push and how far |
| 1845 | // the cluster must be shifted. |
| 1846 | if (direction[0] < 0) { |
| 1847 | whichEdge = ViewCluster.LEFT; |
| 1848 | pushDistance = clusterRect.right - rectOccupiedByPotentialDrop.left; |
| 1849 | } else if (direction[0] > 0) { |
| 1850 | whichEdge = ViewCluster.RIGHT; |
| 1851 | pushDistance = rectOccupiedByPotentialDrop.right - clusterRect.left; |
| 1852 | } else if (direction[1] < 0) { |
| 1853 | whichEdge = ViewCluster.TOP; |
| 1854 | pushDistance = clusterRect.bottom - rectOccupiedByPotentialDrop.top; |
| 1855 | } else { |
| 1856 | whichEdge = ViewCluster.BOTTOM; |
| 1857 | pushDistance = rectOccupiedByPotentialDrop.bottom - clusterRect.top; |
| 1858 | } |
| 1859 | |
| 1860 | // Break early for invalid push distance. |
| 1861 | if (pushDistance <= 0) { |
| 1862 | return false; |
| 1863 | } |
| 1864 | |
| 1865 | // Mark the occupied state as false for the group of views we want to move. |
| 1866 | for (View v: views) { |
| 1867 | CellAndSpan c = currentState.map.get(v); |
| 1868 | mTmpOccupied.markCells(c, false); |
| 1869 | } |
| 1870 | |
| 1871 | // We save the current configuration -- if we fail to find a solution we will revert |
| 1872 | // to the initial state. The process of finding a solution modifies the configuration |
| 1873 | // in place, hence the need for revert in the failure case. |
| 1874 | currentState.save(); |
| 1875 | |
| 1876 | // The pushing algorithm is simplified by considering the views in the order in which |
| 1877 | // they would be pushed by the cluster. For example, if the cluster is leading with its |
| 1878 | // left edge, we consider sort the views by their right edge, from right to left. |
| 1879 | cluster.sortConfigurationForEdgePush(whichEdge); |
| 1880 | |
| 1881 | while (pushDistance > 0 && !fail) { |
| 1882 | for (View v: currentState.sortedViews) { |
| 1883 | // For each view that isn't in the cluster, we see if the leading edge of the |
| 1884 | // cluster is contacting the edge of that view. If so, we add that view to the |
| 1885 | // cluster. |
| 1886 | if (!cluster.views.contains(v) && v != dragView) { |
| 1887 | if (cluster.isViewTouchingEdge(v, whichEdge)) { |
| 1888 | CellLayoutLayoutParams lp = (CellLayoutLayoutParams) v.getLayoutParams(); |
| 1889 | if (!lp.canReorder) { |
| 1890 | // The push solution includes the all apps button, this is not viable. |
| 1891 | fail = true; |
| 1892 | break; |
| 1893 | } |
| 1894 | cluster.addView(v); |
| 1895 | CellAndSpan c = currentState.map.get(v); |
| 1896 | |
| 1897 | // Adding view to cluster, mark it as not occupied. |
| 1898 | mTmpOccupied.markCells(c, false); |
| 1899 | } |
| 1900 | } |
| 1901 | } |
| 1902 | pushDistance--; |
| 1903 | |
| 1904 | // The cluster has been completed, now we move the whole thing over in the appropriate |
| 1905 | // direction. |
| 1906 | cluster.shift(whichEdge, 1); |
| 1907 | } |
| 1908 | |
| 1909 | boolean foundSolution = false; |
| 1910 | clusterRect = cluster.getBoundingRect(); |
| 1911 | |
| 1912 | // Due to the nature of the algorithm, the only check required to verify a valid solution |
| 1913 | // is to ensure that completed shifted cluster lies completely within the cell layout. |
| 1914 | if (!fail && clusterRect.left >= 0 && clusterRect.right <= mCountX && clusterRect.top >= 0 && |
| 1915 | clusterRect.bottom <= mCountY) { |
| 1916 | foundSolution = true; |
| 1917 | } else { |
| 1918 | currentState.restore(); |
| 1919 | } |
| 1920 | |
| 1921 | // In either case, we set the occupied array as marked for the location of the views |
| 1922 | for (View v: cluster.views) { |
| 1923 | CellAndSpan c = currentState.map.get(v); |
| 1924 | mTmpOccupied.markCells(c, true); |
| 1925 | } |
| 1926 | |
| 1927 | return foundSolution; |
| 1928 | } |
| 1929 | |
| 1930 | /** |
| 1931 | * This helper class defines a cluster of views. It helps with defining complex edges |
| 1932 | * of the cluster and determining how those edges interact with other views. The edges |
| 1933 | * essentially define a fine-grained boundary around the cluster of views -- like a more |
| 1934 | * precise version of a bounding box. |
| 1935 | */ |
| 1936 | private class ViewCluster { |
| 1937 | final static int LEFT = 1 << 0; |
| 1938 | final static int TOP = 1 << 1; |
| 1939 | final static int RIGHT = 1 << 2; |
| 1940 | final static int BOTTOM = 1 << 3; |
| 1941 | |
| 1942 | final ArrayList<View> views; |
| 1943 | final ItemConfiguration config; |
| 1944 | final Rect boundingRect = new Rect(); |
| 1945 | |
| 1946 | final int[] leftEdge = new int[mCountY]; |
| 1947 | final int[] rightEdge = new int[mCountY]; |
| 1948 | final int[] topEdge = new int[mCountX]; |
| 1949 | final int[] bottomEdge = new int[mCountX]; |
| 1950 | int dirtyEdges; |
| 1951 | boolean boundingRectDirty; |
| 1952 | |
| 1953 | @SuppressWarnings("unchecked") |
| 1954 | public ViewCluster(ArrayList<View> views, ItemConfiguration config) { |
| 1955 | this.views = (ArrayList<View>) views.clone(); |
| 1956 | this.config = config; |
| 1957 | resetEdges(); |
| 1958 | } |
| 1959 | |
| 1960 | void resetEdges() { |
| 1961 | for (int i = 0; i < mCountX; i++) { |
| 1962 | topEdge[i] = -1; |
| 1963 | bottomEdge[i] = -1; |
| 1964 | } |
| 1965 | for (int i = 0; i < mCountY; i++) { |
| 1966 | leftEdge[i] = -1; |
| 1967 | rightEdge[i] = -1; |
| 1968 | } |
| 1969 | dirtyEdges = LEFT | TOP | RIGHT | BOTTOM; |
| 1970 | boundingRectDirty = true; |
| 1971 | } |
| 1972 | |
| 1973 | void computeEdge(int which) { |
| 1974 | int count = views.size(); |
| 1975 | for (int i = 0; i < count; i++) { |
| 1976 | CellAndSpan cs = config.map.get(views.get(i)); |
| 1977 | switch (which) { |
| 1978 | case LEFT: |
| 1979 | int left = cs.cellX; |
| 1980 | for (int j = cs.cellY; j < cs.cellY + cs.spanY; j++) { |
| 1981 | if (left < leftEdge[j] || leftEdge[j] < 0) { |
| 1982 | leftEdge[j] = left; |
| 1983 | } |
| 1984 | } |
| 1985 | break; |
| 1986 | case RIGHT: |
| 1987 | int right = cs.cellX + cs.spanX; |
| 1988 | for (int j = cs.cellY; j < cs.cellY + cs.spanY; j++) { |
| 1989 | if (right > rightEdge[j]) { |
| 1990 | rightEdge[j] = right; |
| 1991 | } |
| 1992 | } |
| 1993 | break; |
| 1994 | case TOP: |
| 1995 | int top = cs.cellY; |
| 1996 | for (int j = cs.cellX; j < cs.cellX + cs.spanX; j++) { |
| 1997 | if (top < topEdge[j] || topEdge[j] < 0) { |
| 1998 | topEdge[j] = top; |
| 1999 | } |
| 2000 | } |
| 2001 | break; |
| 2002 | case BOTTOM: |
| 2003 | int bottom = cs.cellY + cs.spanY; |
| 2004 | for (int j = cs.cellX; j < cs.cellX + cs.spanX; j++) { |
| 2005 | if (bottom > bottomEdge[j]) { |
| 2006 | bottomEdge[j] = bottom; |
| 2007 | } |
| 2008 | } |
| 2009 | break; |
| 2010 | } |
| 2011 | } |
| 2012 | } |
| 2013 | |
| 2014 | boolean isViewTouchingEdge(View v, int whichEdge) { |
| 2015 | CellAndSpan cs = config.map.get(v); |
| 2016 | |
| 2017 | if ((dirtyEdges & whichEdge) == whichEdge) { |
| 2018 | computeEdge(whichEdge); |
| 2019 | dirtyEdges &= ~whichEdge; |
| 2020 | } |
| 2021 | |
| 2022 | switch (whichEdge) { |
| 2023 | case LEFT: |
| 2024 | for (int i = cs.cellY; i < cs.cellY + cs.spanY; i++) { |
| 2025 | if (leftEdge[i] == cs.cellX + cs.spanX) { |
| 2026 | return true; |
| 2027 | } |
| 2028 | } |
| 2029 | break; |
| 2030 | case RIGHT: |
| 2031 | for (int i = cs.cellY; i < cs.cellY + cs.spanY; i++) { |
| 2032 | if (rightEdge[i] == cs.cellX) { |
| 2033 | return true; |
| 2034 | } |
| 2035 | } |
| 2036 | break; |
| 2037 | case TOP: |
| 2038 | for (int i = cs.cellX; i < cs.cellX + cs.spanX; i++) { |
| 2039 | if (topEdge[i] == cs.cellY + cs.spanY) { |
| 2040 | return true; |
| 2041 | } |
| 2042 | } |
| 2043 | break; |
| 2044 | case BOTTOM: |
| 2045 | for (int i = cs.cellX; i < cs.cellX + cs.spanX; i++) { |
| 2046 | if (bottomEdge[i] == cs.cellY) { |
| 2047 | return true; |
| 2048 | } |
| 2049 | } |
| 2050 | break; |
| 2051 | } |
| 2052 | return false; |
| 2053 | } |
| 2054 | |
| 2055 | void shift(int whichEdge, int delta) { |
| 2056 | for (View v: views) { |
| 2057 | CellAndSpan c = config.map.get(v); |
| 2058 | switch (whichEdge) { |
| 2059 | case LEFT: |
| 2060 | c.cellX -= delta; |
| 2061 | break; |
| 2062 | case RIGHT: |
| 2063 | c.cellX += delta; |
| 2064 | break; |
| 2065 | case TOP: |
| 2066 | c.cellY -= delta; |
| 2067 | break; |
| 2068 | case BOTTOM: |
| 2069 | default: |
| 2070 | c.cellY += delta; |
| 2071 | break; |
| 2072 | } |
| 2073 | } |
| 2074 | resetEdges(); |
| 2075 | } |
| 2076 | |
| 2077 | public void addView(View v) { |
| 2078 | views.add(v); |
| 2079 | resetEdges(); |
| 2080 | } |
| 2081 | |
| 2082 | public Rect getBoundingRect() { |
| 2083 | if (boundingRectDirty) { |
| 2084 | config.getBoundingRectForViews(views, boundingRect); |
| 2085 | } |
| 2086 | return boundingRect; |
| 2087 | } |
| 2088 | |
| 2089 | final PositionComparator comparator = new PositionComparator(); |
| 2090 | class PositionComparator implements Comparator<View> { |
| 2091 | int whichEdge = 0; |
| 2092 | public int compare(View left, View right) { |
| 2093 | CellAndSpan l = config.map.get(left); |
| 2094 | CellAndSpan r = config.map.get(right); |
| 2095 | switch (whichEdge) { |
| 2096 | case LEFT: |
| 2097 | return (r.cellX + r.spanX) - (l.cellX + l.spanX); |
| 2098 | case RIGHT: |
| 2099 | return l.cellX - r.cellX; |
| 2100 | case TOP: |
| 2101 | return (r.cellY + r.spanY) - (l.cellY + l.spanY); |
| 2102 | case BOTTOM: |
| 2103 | default: |
| 2104 | return l.cellY - r.cellY; |
| 2105 | } |
| 2106 | } |
| 2107 | } |
| 2108 | |
| 2109 | public void sortConfigurationForEdgePush(int edge) { |
| 2110 | comparator.whichEdge = edge; |
| 2111 | Collections.sort(config.sortedViews, comparator); |
| 2112 | } |
| 2113 | } |
| 2114 | |
| 2115 | // This method tries to find a reordering solution which satisfies the push mechanic by trying |
| 2116 | // to push items in each of the cardinal directions, in an order based on the direction vector |
| 2117 | // passed. |
| 2118 | private boolean attemptPushInDirection(ArrayList<View> intersectingViews, Rect occupied, |
| 2119 | int[] direction, View ignoreView, ItemConfiguration solution) { |
| 2120 | if ((Math.abs(direction[0]) + Math.abs(direction[1])) > 1) { |
| 2121 | // If the direction vector has two non-zero components, we try pushing |
| 2122 | // separately in each of the components. |
| 2123 | int temp = direction[1]; |
| 2124 | direction[1] = 0; |
| 2125 | |
| 2126 | if (pushViewsToTempLocation(intersectingViews, occupied, direction, |
| 2127 | ignoreView, solution)) { |
| 2128 | return true; |
| 2129 | } |
| 2130 | direction[1] = temp; |
| 2131 | temp = direction[0]; |
| 2132 | direction[0] = 0; |
| 2133 | |
| 2134 | if (pushViewsToTempLocation(intersectingViews, occupied, direction, |
| 2135 | ignoreView, solution)) { |
| 2136 | return true; |
| 2137 | } |
| 2138 | // Revert the direction |
| 2139 | direction[0] = temp; |
| 2140 | |
| 2141 | // Now we try pushing in each component of the opposite direction |
| 2142 | direction[0] *= -1; |
| 2143 | direction[1] *= -1; |
| 2144 | temp = direction[1]; |
| 2145 | direction[1] = 0; |
| 2146 | if (pushViewsToTempLocation(intersectingViews, occupied, direction, |
| 2147 | ignoreView, solution)) { |
| 2148 | return true; |
| 2149 | } |
| 2150 | |
| 2151 | direction[1] = temp; |
| 2152 | temp = direction[0]; |
| 2153 | direction[0] = 0; |
| 2154 | if (pushViewsToTempLocation(intersectingViews, occupied, direction, |
| 2155 | ignoreView, solution)) { |
| 2156 | return true; |
| 2157 | } |
| 2158 | // revert the direction |
| 2159 | direction[0] = temp; |
| 2160 | direction[0] *= -1; |
| 2161 | direction[1] *= -1; |
| 2162 | |
| 2163 | } else { |
| 2164 | // If the direction vector has a single non-zero component, we push first in the |
| 2165 | // direction of the vector |
| 2166 | if (pushViewsToTempLocation(intersectingViews, occupied, direction, |
| 2167 | ignoreView, solution)) { |
| 2168 | return true; |
| 2169 | } |
| 2170 | // Then we try the opposite direction |
| 2171 | direction[0] *= -1; |
| 2172 | direction[1] *= -1; |
| 2173 | if (pushViewsToTempLocation(intersectingViews, occupied, direction, |
| 2174 | ignoreView, solution)) { |
| 2175 | return true; |
| 2176 | } |
| 2177 | // Switch the direction back |
| 2178 | direction[0] *= -1; |
| 2179 | direction[1] *= -1; |
| 2180 | |
| 2181 | // If we have failed to find a push solution with the above, then we try |
| 2182 | // to find a solution by pushing along the perpendicular axis. |
| 2183 | |
| 2184 | // Swap the components |
| 2185 | int temp = direction[1]; |
| 2186 | direction[1] = direction[0]; |
| 2187 | direction[0] = temp; |
| 2188 | if (pushViewsToTempLocation(intersectingViews, occupied, direction, |
| 2189 | ignoreView, solution)) { |
| 2190 | return true; |
| 2191 | } |
| 2192 | |
| 2193 | // Then we try the opposite direction |
| 2194 | direction[0] *= -1; |
| 2195 | direction[1] *= -1; |
| 2196 | if (pushViewsToTempLocation(intersectingViews, occupied, direction, |
| 2197 | ignoreView, solution)) { |
| 2198 | return true; |
| 2199 | } |
| 2200 | // Switch the direction back |
| 2201 | direction[0] *= -1; |
| 2202 | direction[1] *= -1; |
| 2203 | |
| 2204 | // Swap the components back |
| 2205 | temp = direction[1]; |
| 2206 | direction[1] = direction[0]; |
| 2207 | direction[0] = temp; |
| 2208 | } |
| 2209 | return false; |
| 2210 | } |
| 2211 | |
| 2212 | /* |
| 2213 | * Returns a pair (x, y), where x,y are in {-1, 0, 1} corresponding to vector between |
| 2214 | * the provided point and the provided cell |
| 2215 | */ |
| 2216 | private void computeDirectionVector(float deltaX, float deltaY, int[] result) { |
| 2217 | double angle = Math.atan(deltaY / deltaX); |
| 2218 | |
| 2219 | result[0] = 0; |
| 2220 | result[1] = 0; |
| 2221 | if (Math.abs(Math.cos(angle)) > 0.5f) { |
| 2222 | result[0] = (int) Math.signum(deltaX); |
| 2223 | } |
| 2224 | if (Math.abs(Math.sin(angle)) > 0.5f) { |
| 2225 | result[1] = (int) Math.signum(deltaY); |
| 2226 | } |
| 2227 | } |
| 2228 | |
| 2229 | /* This seems like it should be obvious and straight-forward, but when the direction vector |
| 2230 | needs to match with the notion of the dragView pushing other views, we have to employ |
| 2231 | a slightly more subtle notion of the direction vector. The question is what two points is |
| 2232 | the vector between? The center of the dragView and its desired destination? Not quite, as |
| 2233 | this doesn't necessarily coincide with the interaction of the dragView and items occupying |
| 2234 | those cells. Instead we use some heuristics to often lock the vector to up, down, left |
| 2235 | or right, which helps make pushing feel right. |
| 2236 | */ |
Sebastian Franco | e4c0345 | 2022-12-27 14:50:02 -0600 | [diff] [blame] | 2237 | public void getDirectionVectorForDrop(int dragViewCenterX, int dragViewCenterY, int spanX, |
Sebastian Franco | 9cab1c3 | 2022-10-25 17:28:54 -0700 | [diff] [blame] | 2238 | int spanY, View dragView, int[] resultDirection) { |
| 2239 | |
| 2240 | //TODO(adamcohen) b/151776141 use the items visual center for the direction vector |
| 2241 | int[] targetDestination = new int[2]; |
| 2242 | |
Sebastián Franco | f9a6ac2 | 2022-11-15 22:56:37 +0000 | [diff] [blame] | 2243 | findNearestAreaIgnoreOccupied(dragViewCenterX, dragViewCenterY, spanX, spanY, |
| 2244 | targetDestination); |
Sebastian Franco | 9cab1c3 | 2022-10-25 17:28:54 -0700 | [diff] [blame] | 2245 | Rect dragRect = new Rect(); |
| 2246 | cellToRect(targetDestination[0], targetDestination[1], spanX, spanY, dragRect); |
| 2247 | dragRect.offset(dragViewCenterX - dragRect.centerX(), dragViewCenterY - dragRect.centerY()); |
| 2248 | |
| 2249 | Rect dropRegionRect = new Rect(); |
| 2250 | getViewsIntersectingRegion(targetDestination[0], targetDestination[1], spanX, spanY, |
| 2251 | dragView, dropRegionRect, mIntersectingViews); |
| 2252 | |
| 2253 | int dropRegionSpanX = dropRegionRect.width(); |
| 2254 | int dropRegionSpanY = dropRegionRect.height(); |
| 2255 | |
| 2256 | cellToRect(dropRegionRect.left, dropRegionRect.top, dropRegionRect.width(), |
| 2257 | dropRegionRect.height(), dropRegionRect); |
| 2258 | |
| 2259 | int deltaX = (dropRegionRect.centerX() - dragViewCenterX) / spanX; |
| 2260 | int deltaY = (dropRegionRect.centerY() - dragViewCenterY) / spanY; |
| 2261 | |
| 2262 | if (dropRegionSpanX == mCountX || spanX == mCountX) { |
| 2263 | deltaX = 0; |
| 2264 | } |
| 2265 | if (dropRegionSpanY == mCountY || spanY == mCountY) { |
| 2266 | deltaY = 0; |
| 2267 | } |
| 2268 | |
| 2269 | if (deltaX == 0 && deltaY == 0) { |
| 2270 | // No idea what to do, give a random direction. |
| 2271 | resultDirection[0] = 1; |
| 2272 | resultDirection[1] = 0; |
| 2273 | } else { |
| 2274 | computeDirectionVector(deltaX, deltaY, resultDirection); |
| 2275 | } |
| 2276 | } |
| 2277 | |
| 2278 | private boolean addViewsToTempLocation(ArrayList<View> views, Rect rectOccupiedByPotentialDrop, |
| 2279 | int[] direction, View dragView, ItemConfiguration currentState) { |
| 2280 | if (views.size() == 0) return true; |
| 2281 | |
| 2282 | boolean success = false; |
| 2283 | Rect boundingRect = new Rect(); |
| 2284 | // We construct a rect which represents the entire group of views passed in |
| 2285 | currentState.getBoundingRectForViews(views, boundingRect); |
| 2286 | |
| 2287 | // Mark the occupied state as false for the group of views we want to move. |
| 2288 | for (View v: views) { |
| 2289 | CellAndSpan c = currentState.map.get(v); |
| 2290 | mTmpOccupied.markCells(c, false); |
| 2291 | } |
| 2292 | |
| 2293 | GridOccupancy blockOccupied = new GridOccupancy(boundingRect.width(), boundingRect.height()); |
| 2294 | int top = boundingRect.top; |
| 2295 | int left = boundingRect.left; |
| 2296 | // We mark more precisely which parts of the bounding rect are truly occupied, allowing |
| 2297 | // for interlocking. |
| 2298 | for (View v: views) { |
| 2299 | CellAndSpan c = currentState.map.get(v); |
| 2300 | blockOccupied.markCells(c.cellX - left, c.cellY - top, c.spanX, c.spanY, true); |
| 2301 | } |
| 2302 | |
| 2303 | mTmpOccupied.markCells(rectOccupiedByPotentialDrop, true); |
| 2304 | |
| 2305 | findNearestArea(boundingRect.left, boundingRect.top, boundingRect.width(), |
| 2306 | boundingRect.height(), direction, |
| 2307 | mTmpOccupied.cells, blockOccupied.cells, mTempLocation); |
| 2308 | |
| 2309 | // If we successfully found a location by pushing the block of views, we commit it |
| 2310 | if (mTempLocation[0] >= 0 && mTempLocation[1] >= 0) { |
| 2311 | int deltaX = mTempLocation[0] - boundingRect.left; |
| 2312 | int deltaY = mTempLocation[1] - boundingRect.top; |
| 2313 | for (View v: views) { |
| 2314 | CellAndSpan c = currentState.map.get(v); |
| 2315 | c.cellX += deltaX; |
| 2316 | c.cellY += deltaY; |
| 2317 | } |
| 2318 | success = true; |
| 2319 | } |
| 2320 | |
| 2321 | // In either case, we set the occupied array as marked for the location of the views |
| 2322 | for (View v: views) { |
| 2323 | CellAndSpan c = currentState.map.get(v); |
| 2324 | mTmpOccupied.markCells(c, true); |
| 2325 | } |
| 2326 | return success; |
| 2327 | } |
| 2328 | |
Sebastian Franco | e4c0345 | 2022-12-27 14:50:02 -0600 | [diff] [blame] | 2329 | public boolean rearrangementExists(int cellX, int cellY, int spanX, int spanY, int[] direction, |
Sebastian Franco | 9cab1c3 | 2022-10-25 17:28:54 -0700 | [diff] [blame] | 2330 | View ignoreView, ItemConfiguration solution) { |
| 2331 | // Return early if get invalid cell positions |
| 2332 | if (cellX < 0 || cellY < 0) return false; |
| 2333 | |
| 2334 | mIntersectingViews.clear(); |
| 2335 | mOccupiedRect.set(cellX, cellY, cellX + spanX, cellY + spanY); |
| 2336 | |
| 2337 | // Mark the desired location of the view currently being dragged. |
| 2338 | if (ignoreView != null) { |
| 2339 | CellAndSpan c = solution.map.get(ignoreView); |
| 2340 | if (c != null) { |
| 2341 | c.cellX = cellX; |
| 2342 | c.cellY = cellY; |
| 2343 | } |
| 2344 | } |
| 2345 | Rect r0 = new Rect(cellX, cellY, cellX + spanX, cellY + spanY); |
| 2346 | Rect r1 = new Rect(); |
Sebastian Franco | 45b39b5 | 2023-01-10 10:47:46 -0600 | [diff] [blame] | 2347 | // The views need to be sorted so that the results are deterministic on the views positions |
| 2348 | // and not by the views hash which is "random". |
| 2349 | // The views are sorted twice, once for the X position and a second time for the Y position |
| 2350 | // to ensure same order everytime. |
| 2351 | Comparator comparator = Comparator.comparing(view -> |
| 2352 | ((CellLayoutLayoutParams) ((View) view).getLayoutParams()).getCellX()) |
| 2353 | .thenComparing(view -> |
| 2354 | ((CellLayoutLayoutParams) ((View) view).getLayoutParams()).getCellY()); |
| 2355 | List<View> views = solution.map.keySet().stream().sorted(comparator).toList(); |
| 2356 | for (View child : views) { |
Sebastian Franco | 9cab1c3 | 2022-10-25 17:28:54 -0700 | [diff] [blame] | 2357 | if (child == ignoreView) continue; |
| 2358 | CellAndSpan c = solution.map.get(child); |
| 2359 | CellLayoutLayoutParams lp = (CellLayoutLayoutParams) child.getLayoutParams(); |
| 2360 | r1.set(c.cellX, c.cellY, c.cellX + c.spanX, c.cellY + c.spanY); |
| 2361 | if (Rect.intersects(r0, r1)) { |
| 2362 | if (!lp.canReorder) { |
| 2363 | return false; |
| 2364 | } |
| 2365 | mIntersectingViews.add(child); |
| 2366 | } |
| 2367 | } |
| 2368 | |
| 2369 | solution.intersectingViews = new ArrayList<>(mIntersectingViews); |
| 2370 | |
| 2371 | // First we try to find a solution which respects the push mechanic. That is, |
| 2372 | // we try to find a solution such that no displaced item travels through another item |
| 2373 | // without also displacing that item. |
| 2374 | if (attemptPushInDirection(mIntersectingViews, mOccupiedRect, direction, ignoreView, |
| 2375 | solution)) { |
| 2376 | return true; |
| 2377 | } |
| 2378 | |
| 2379 | // Next we try moving the views as a block, but without requiring the push mechanic. |
| 2380 | if (addViewsToTempLocation(mIntersectingViews, mOccupiedRect, direction, ignoreView, |
| 2381 | solution)) { |
| 2382 | return true; |
| 2383 | } |
| 2384 | |
| 2385 | // Ok, they couldn't move as a block, let's move them individually |
| 2386 | for (View v : mIntersectingViews) { |
| 2387 | if (!addViewToTempLocation(v, mOccupiedRect, direction, solution)) { |
| 2388 | return false; |
| 2389 | } |
| 2390 | } |
| 2391 | return true; |
| 2392 | } |
| 2393 | |
Sebastian Franco | e4c0345 | 2022-12-27 14:50:02 -0600 | [diff] [blame] | 2394 | public ReorderAlgorithm createReorderAlgorithm() { |
| 2395 | return new ReorderAlgorithm(this); |
| 2396 | } |
| 2397 | |
Sebastian Franco | 0958932 | 2022-11-02 15:25:58 -0700 | [diff] [blame] | 2398 | protected ItemConfiguration findReorderSolution(int pixelX, int pixelY, int minSpanX, |
| 2399 | int minSpanY, int spanX, int spanY, int[] direction, View dragView, boolean decX, |
| 2400 | ItemConfiguration solution) { |
Sebastian Franco | e4c0345 | 2022-12-27 14:50:02 -0600 | [diff] [blame] | 2401 | return createReorderAlgorithm().findReorderSolution(pixelX, pixelY, minSpanX, minSpanY, |
| 2402 | spanX, spanY, direction, dragView, decX, solution); |
Sebastian Franco | 0958932 | 2022-11-02 15:25:58 -0700 | [diff] [blame] | 2403 | } |
| 2404 | |
Sebastian Franco | e4c0345 | 2022-12-27 14:50:02 -0600 | [diff] [blame] | 2405 | public void copyCurrentStateToSolution(ItemConfiguration solution, boolean temp) { |
Sebastian Franco | 9cab1c3 | 2022-10-25 17:28:54 -0700 | [diff] [blame] | 2406 | int childCount = mShortcutsAndWidgets.getChildCount(); |
| 2407 | for (int i = 0; i < childCount; i++) { |
| 2408 | View child = mShortcutsAndWidgets.getChildAt(i); |
| 2409 | CellLayoutLayoutParams lp = (CellLayoutLayoutParams) child.getLayoutParams(); |
| 2410 | CellAndSpan c; |
| 2411 | if (temp) { |
Sebastian Franco | 877088e | 2023-01-03 15:16:22 -0700 | [diff] [blame] | 2412 | c = new CellAndSpan(lp.getTmpCellX(), lp.getTmpCellY(), lp.cellHSpan, lp.cellVSpan); |
Sebastian Franco | 9cab1c3 | 2022-10-25 17:28:54 -0700 | [diff] [blame] | 2413 | } else { |
Sebastian Franco | 877088e | 2023-01-03 15:16:22 -0700 | [diff] [blame] | 2414 | c = new CellAndSpan(lp.getCellX(), lp.getCellY(), lp.cellHSpan, lp.cellVSpan); |
Sebastian Franco | 9cab1c3 | 2022-10-25 17:28:54 -0700 | [diff] [blame] | 2415 | } |
| 2416 | solution.add(child, c); |
| 2417 | } |
Sebastian Franco | 53a15a4 | 2022-10-25 17:28:54 -0700 | [diff] [blame] | 2418 | } |
| 2419 | |
| 2420 | /** |
Sebastian Franco | 53a15a4 | 2022-10-25 17:28:54 -0700 | [diff] [blame] | 2421 | * When the user drags an Item in the workspace sometimes we need to move the items already in |
| 2422 | * the workspace to make space for the new item, this function return a solution for that |
| 2423 | * reorder. |
| 2424 | * |
| 2425 | * @param pixelX X coordinate in the screen of the dragView in pixels |
| 2426 | * @param pixelY Y coordinate in the screen of the dragView in pixels |
| 2427 | * @param minSpanX minimum horizontal span the item can be shrunk to |
| 2428 | * @param minSpanY minimum vertical span the item can be shrunk to |
| 2429 | * @param spanX occupied horizontal span |
| 2430 | * @param spanY occupied vertical span |
| 2431 | * @param dragView the view of the item being draged |
| 2432 | * @return returns a solution for the given parameters, the solution contains all the icons and |
| 2433 | * the locations they should be in the given solution. |
| 2434 | */ |
| 2435 | public ItemConfiguration calculateReorder(int pixelX, int pixelY, int minSpanX, int minSpanY, |
| 2436 | int spanX, int spanY, View dragView) { |
Sebastian Franco | e4c0345 | 2022-12-27 14:50:02 -0600 | [diff] [blame] | 2437 | return createReorderAlgorithm().calculateReorder(pixelX, pixelY, minSpanX, minSpanY, |
| 2438 | spanX, spanY, dragView); |
Sebastian Franco | 53a15a4 | 2022-10-25 17:28:54 -0700 | [diff] [blame] | 2439 | } |
Adam Cohen | 482ed82 | 2012-03-02 14:15:13 -0800 | [diff] [blame] | 2440 | |
Sebastian Franco | 9cab1c3 | 2022-10-25 17:28:54 -0700 | [diff] [blame] | 2441 | int[] performReorder(int pixelX, int pixelY, int minSpanX, int minSpanY, int spanX, int spanY, |
| 2442 | View dragView, int[] result, int[] resultSpan, int mode) { |
| 2443 | if (resultSpan == null) { |
| 2444 | resultSpan = new int[]{-1, -1}; |
| 2445 | } |
| 2446 | if (result == null) { |
| 2447 | result = new int[]{-1, -1}; |
| 2448 | } |
Sebastian Franco | 5d990ee | 2022-11-01 16:08:24 -0700 | [diff] [blame] | 2449 | |
| 2450 | ItemConfiguration finalSolution = null; |
| 2451 | // We want the solution to match the animation of the preview and to match the drop so we |
| 2452 | // only recalculate in mode MODE_SHOW_REORDER_HINT because that the first one to run in the |
| 2453 | // reorder cycle. |
| 2454 | if (mode == MODE_SHOW_REORDER_HINT || mPreviousSolution == null) { |
| 2455 | finalSolution = calculateReorder(pixelX, pixelY, minSpanX, minSpanY, spanX, spanY, |
| 2456 | dragView); |
| 2457 | mPreviousSolution = finalSolution; |
| 2458 | } else { |
Sebastian Franco | 9cab1c3 | 2022-10-25 17:28:54 -0700 | [diff] [blame] | 2459 | finalSolution = mPreviousSolution; |
| 2460 | // We reset this vector after drop |
| 2461 | if (mode == MODE_ON_DROP || mode == MODE_ON_DROP_EXTERNAL) { |
| 2462 | mPreviousSolution = null; |
| 2463 | } |
Sebastian Franco | 9cab1c3 | 2022-10-25 17:28:54 -0700 | [diff] [blame] | 2464 | } |
| 2465 | |
| 2466 | if (finalSolution == null || !finalSolution.isSolution) { |
| 2467 | result[0] = result[1] = resultSpan[0] = resultSpan[1] = -1; |
| 2468 | } else { |
| 2469 | result[0] = finalSolution.cellX; |
| 2470 | result[1] = finalSolution.cellY; |
| 2471 | resultSpan[0] = finalSolution.spanX; |
| 2472 | resultSpan[1] = finalSolution.spanY; |
Sebastian Franco | 9c74327 | 2022-11-15 15:03:25 -0800 | [diff] [blame] | 2473 | performReorder(finalSolution, dragView, mode); |
Sebastian Franco | 9cab1c3 | 2022-10-25 17:28:54 -0700 | [diff] [blame] | 2474 | } |
Sebastian Franco | 9cab1c3 | 2022-10-25 17:28:54 -0700 | [diff] [blame] | 2475 | return result; |
| 2476 | } |
| 2477 | |
Sebastian Franco | 53a15a4 | 2022-10-25 17:28:54 -0700 | [diff] [blame] | 2478 | /** |
| 2479 | * Animates and submits in the DB the given ItemConfiguration depending of the mode. |
| 2480 | * |
| 2481 | * @param solution represents widgets on the screen which the Workspace will animate to and |
| 2482 | * would be submitted to the database. |
| 2483 | * @param dragView view which is being dragged over the workspace that trigger the reorder |
| 2484 | * @param mode depending on the mode different animations would be played and depending on the |
| 2485 | * mode the solution would be submitted or not the database. |
| 2486 | * The possible modes are {@link MODE_SHOW_REORDER_HINT}, {@link MODE_DRAG_OVER}, |
| 2487 | * {@link MODE_ON_DROP}, {@link MODE_ON_DROP_EXTERNAL}, {@link MODE_ACCEPT_DROP} |
| 2488 | * defined in {@link CellLayout}. |
| 2489 | */ |
Sebastian Franco | e4c0345 | 2022-12-27 14:50:02 -0600 | [diff] [blame] | 2490 | public void performReorder(ItemConfiguration solution, View dragView, int mode) { |
Adam Cohen | fa3c58f | 2013-12-06 16:10:55 -0800 | [diff] [blame] | 2491 | if (mode == MODE_SHOW_REORDER_HINT) { |
Sebastian Franco | 53a15a4 | 2022-10-25 17:28:54 -0700 | [diff] [blame] | 2492 | beginOrAdjustReorderPreviewAnimations(solution, dragView, |
| 2493 | ReorderPreviewAnimation.MODE_HINT); |
| 2494 | return; |
| 2495 | } |
| 2496 | // If we're just testing for a possible location (MODE_ACCEPT_DROP), we don't bother |
| 2497 | // committing anything or animating anything as we just want to determine if a solution |
| 2498 | // exists |
| 2499 | if (mode == MODE_DRAG_OVER || mode == MODE_ON_DROP || mode == MODE_ON_DROP_EXTERNAL) { |
| 2500 | if (!DESTRUCTIVE_REORDER) { |
| 2501 | setUseTempCoords(true); |
| 2502 | } |
| 2503 | |
| 2504 | if (!DESTRUCTIVE_REORDER) { |
| 2505 | copySolutionToTempState(solution, dragView); |
| 2506 | } |
| 2507 | setItemPlacementDirty(true); |
| 2508 | animateItemsToSolution(solution, dragView, mode == MODE_ON_DROP); |
| 2509 | |
| 2510 | if (!DESTRUCTIVE_REORDER |
| 2511 | && (mode == MODE_ON_DROP || mode == MODE_ON_DROP_EXTERNAL)) { |
| 2512 | // Since the temp solution didn't update dragView, don't commit it either |
| 2513 | commitTempPlacement(dragView); |
| 2514 | completeAndClearReorderPreviewAnimations(); |
| 2515 | setItemPlacementDirty(false); |
Adam Cohen | fa3c58f | 2013-12-06 16:10:55 -0800 | [diff] [blame] | 2516 | } else { |
Sebastian Franco | 53a15a4 | 2022-10-25 17:28:54 -0700 | [diff] [blame] | 2517 | beginOrAdjustReorderPreviewAnimations(solution, dragView, |
| 2518 | ReorderPreviewAnimation.MODE_PREVIEW); |
Adam Cohen | fa3c58f | 2013-12-06 16:10:55 -0800 | [diff] [blame] | 2519 | } |
Adam Cohen | fa3c58f | 2013-12-06 16:10:55 -0800 | [diff] [blame] | 2520 | } |
| 2521 | |
Sebastian Franco | 53a15a4 | 2022-10-25 17:28:54 -0700 | [diff] [blame] | 2522 | if (mode == MODE_ON_DROP && !DESTRUCTIVE_REORDER) { |
Adam Cohen | 482ed82 | 2012-03-02 14:15:13 -0800 | [diff] [blame] | 2523 | setUseTempCoords(false); |
| 2524 | } |
Adam Cohen | 482ed82 | 2012-03-02 14:15:13 -0800 | [diff] [blame] | 2525 | |
Michael Jurka | a52570f | 2012-03-20 03:18:20 -0700 | [diff] [blame] | 2526 | mShortcutsAndWidgets.requestLayout(); |
Adam Cohen | 482ed82 | 2012-03-02 14:15:13 -0800 | [diff] [blame] | 2527 | } |
| 2528 | |
Adam Cohen | 19f3792 | 2012-03-21 11:59:11 -0700 | [diff] [blame] | 2529 | void setItemPlacementDirty(boolean dirty) { |
| 2530 | mItemPlacementDirty = dirty; |
Adam Cohen | 482ed82 | 2012-03-02 14:15:13 -0800 | [diff] [blame] | 2531 | } |
Adam Cohen | 19f3792 | 2012-03-21 11:59:11 -0700 | [diff] [blame] | 2532 | boolean isItemPlacementDirty() { |
| 2533 | return mItemPlacementDirty; |
Adam Cohen | 482ed82 | 2012-03-02 14:15:13 -0800 | [diff] [blame] | 2534 | } |
| 2535 | |
Sebastian Franco | e4c0345 | 2022-12-27 14:50:02 -0600 | [diff] [blame] | 2536 | /** |
| 2537 | * Represents the solution to a reorder of items in the Workspace. |
| 2538 | */ |
| 2539 | public static class ItemConfiguration extends CellAndSpan { |
| 2540 | public final ArrayMap<View, CellAndSpan> map = new ArrayMap<>(); |
Rajeev Kumar | 9962dbe | 2017-06-12 12:16:20 -0700 | [diff] [blame] | 2541 | private final ArrayMap<View, CellAndSpan> savedMap = new ArrayMap<>(); |
Sebastian Franco | e4c0345 | 2022-12-27 14:50:02 -0600 | [diff] [blame] | 2542 | public final ArrayList<View> sortedViews = new ArrayList<>(); |
| 2543 | public ArrayList<View> intersectingViews; |
| 2544 | public boolean isSolution = false; |
Adam Cohen | 482ed82 | 2012-03-02 14:15:13 -0800 | [diff] [blame] | 2545 | |
Sebastian Franco | e4c0345 | 2022-12-27 14:50:02 -0600 | [diff] [blame] | 2546 | public void save() { |
Adam Cohen | f3900c2 | 2012-11-16 18:28:11 -0800 | [diff] [blame] | 2547 | // Copy current state into savedMap |
| 2548 | for (View v: map.keySet()) { |
Sunny Goyal | ff4ba2d | 2016-04-02 14:12:34 -0700 | [diff] [blame] | 2549 | savedMap.get(v).copyFrom(map.get(v)); |
Adam Cohen | f3900c2 | 2012-11-16 18:28:11 -0800 | [diff] [blame] | 2550 | } |
| 2551 | } |
| 2552 | |
Sebastian Franco | e4c0345 | 2022-12-27 14:50:02 -0600 | [diff] [blame] | 2553 | public void restore() { |
Adam Cohen | f3900c2 | 2012-11-16 18:28:11 -0800 | [diff] [blame] | 2554 | // Restore current state from savedMap |
| 2555 | for (View v: savedMap.keySet()) { |
Sunny Goyal | ff4ba2d | 2016-04-02 14:12:34 -0700 | [diff] [blame] | 2556 | map.get(v).copyFrom(savedMap.get(v)); |
Adam Cohen | f3900c2 | 2012-11-16 18:28:11 -0800 | [diff] [blame] | 2557 | } |
| 2558 | } |
| 2559 | |
Sebastian Franco | e4c0345 | 2022-12-27 14:50:02 -0600 | [diff] [blame] | 2560 | public void add(View v, CellAndSpan cs) { |
Adam Cohen | f3900c2 | 2012-11-16 18:28:11 -0800 | [diff] [blame] | 2561 | map.put(v, cs); |
| 2562 | savedMap.put(v, new CellAndSpan()); |
| 2563 | sortedViews.add(v); |
| 2564 | } |
| 2565 | |
Sebastian Franco | e4c0345 | 2022-12-27 14:50:02 -0600 | [diff] [blame] | 2566 | public int area() { |
Sunny Goyal | ff4ba2d | 2016-04-02 14:12:34 -0700 | [diff] [blame] | 2567 | return spanX * spanY; |
Adam Cohen | f3900c2 | 2012-11-16 18:28:11 -0800 | [diff] [blame] | 2568 | } |
| 2569 | |
Sebastian Franco | e4c0345 | 2022-12-27 14:50:02 -0600 | [diff] [blame] | 2570 | public void getBoundingRectForViews(ArrayList<View> views, Rect outRect) { |
Sunny Goyal | ff4ba2d | 2016-04-02 14:12:34 -0700 | [diff] [blame] | 2571 | boolean first = true; |
| 2572 | for (View v: views) { |
| 2573 | CellAndSpan c = map.get(v); |
| 2574 | if (first) { |
| 2575 | outRect.set(c.cellX, c.cellY, c.cellX + c.spanX, c.cellY + c.spanY); |
| 2576 | first = false; |
| 2577 | } else { |
| 2578 | outRect.union(c.cellX, c.cellY, c.cellX + c.spanX, c.cellY + c.spanY); |
| 2579 | } |
| 2580 | } |
Adam Cohen | f3900c2 | 2012-11-16 18:28:11 -0800 | [diff] [blame] | 2581 | } |
Adam Cohen | 482ed82 | 2012-03-02 14:15:13 -0800 | [diff] [blame] | 2582 | } |
| 2583 | |
Adam Cohen | df03538 | 2011-04-11 17:22:04 -0700 | [diff] [blame] | 2584 | /** |
Adam Cohen | df03538 | 2011-04-11 17:22:04 -0700 | [diff] [blame] | 2585 | * Find a starting cell position that will fit the given bounds nearest the requested |
| 2586 | * cell location. Uses Euclidean distance to score multiple vacant areas. |
| 2587 | * |
| 2588 | * @param pixelX The X location at which you want to search for a vacant area. |
| 2589 | * @param pixelY The Y location at which you want to search for a vacant area. |
| 2590 | * @param spanX Horizontal span of the object. |
| 2591 | * @param spanY Vertical span of the object. |
Adam Cohen | df03538 | 2011-04-11 17:22:04 -0700 | [diff] [blame] | 2592 | * @param result Previously returned value to possibly recycle. |
| 2593 | * @return The X, Y cell of a vacant area that can contain this object, |
| 2594 | * nearest the requested location. |
| 2595 | */ |
Sebastián Franco | f9a6ac2 | 2022-11-15 22:56:37 +0000 | [diff] [blame] | 2596 | public int[] findNearestAreaIgnoreOccupied(int pixelX, int pixelY, int spanX, int spanY, |
| 2597 | int[] result) { |
Sebastian Franco | b57c0b2 | 2022-06-28 13:54:35 -0700 | [diff] [blame] | 2598 | return findNearestArea(pixelX, pixelY, spanX, spanY, spanX, spanY, true, result, null); |
Adam Cohen | df03538 | 2011-04-11 17:22:04 -0700 | [diff] [blame] | 2599 | } |
| 2600 | |
Michael Jurka | 0280c3b | 2010-09-17 15:00:07 -0700 | [diff] [blame] | 2601 | boolean existsEmptyCell() { |
| 2602 | return findCellForSpan(null, 1, 1); |
| 2603 | } |
| 2604 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 2605 | /** |
Michael Jurka | 0280c3b | 2010-09-17 15:00:07 -0700 | [diff] [blame] | 2606 | * Finds the upper-left coordinate of the first rectangle in the grid that can |
| 2607 | * hold a cell of the specified dimensions. If intersectX and intersectY are not -1, |
| 2608 | * then this method will only return coordinates for rectangles that contain the cell |
| 2609 | * (intersectX, intersectY) |
| 2610 | * |
| 2611 | * @param cellXY The array that will contain the position of a vacant cell if such a cell |
| 2612 | * can be found. |
| 2613 | * @param spanX The horizontal span of the cell we want to find. |
| 2614 | * @param spanY The vertical span of the cell we want to find. |
| 2615 | * |
| 2616 | * @return True if a vacant cell of the specified dimension was found, false otherwise. |
Patrick Dubroy | 6569f2c | 2010-07-12 14:25:18 -0700 | [diff] [blame] | 2617 | */ |
Hyunyoung Song | 3f47144 | 2015-04-08 19:01:34 -0700 | [diff] [blame] | 2618 | public boolean findCellForSpan(int[] cellXY, int spanX, int spanY) { |
Sunny Goyal | ff4ba2d | 2016-04-02 14:12:34 -0700 | [diff] [blame] | 2619 | if (cellXY == null) { |
| 2620 | cellXY = new int[2]; |
Michael Jurka | 0280c3b | 2010-09-17 15:00:07 -0700 | [diff] [blame] | 2621 | } |
Sunny Goyal | ff4ba2d | 2016-04-02 14:12:34 -0700 | [diff] [blame] | 2622 | return mOccupied.findVacantCell(cellXY, spanX, spanY); |
Michael Jurka | 0280c3b | 2010-09-17 15:00:07 -0700 | [diff] [blame] | 2623 | } |
| 2624 | |
| 2625 | /** |
Winson Chung | c07918d | 2011-07-01 15:35:26 -0700 | [diff] [blame] | 2626 | * A drag event has begun over this layout. |
| 2627 | * It may have begun over this layout (in which case onDragChild is called first), |
| 2628 | * or it may have begun on another layout. |
| 2629 | */ |
| 2630 | void onDragEnter() { |
Winson Chung | c07918d | 2011-07-01 15:35:26 -0700 | [diff] [blame] | 2631 | mDragging = true; |
Sebastian Franco | 5aa71ce | 2022-12-14 12:13:19 -0600 | [diff] [blame] | 2632 | mPreviousSolution = null; |
Winson Chung | c07918d | 2011-07-01 15:35:26 -0700 | [diff] [blame] | 2633 | } |
| 2634 | |
| 2635 | /** |
Michael Jurka | 0280c3b | 2010-09-17 15:00:07 -0700 | [diff] [blame] | 2636 | * Called when drag has left this CellLayout or has been completed (successfully or not) |
| 2637 | */ |
| 2638 | void onDragExit() { |
Joe Onorato | 4be866d | 2010-10-10 11:26:02 -0700 | [diff] [blame] | 2639 | // This can actually be called when we aren't in a drag, e.g. when adding a new |
| 2640 | // item to this layout via the customize drawer. |
| 2641 | // Guard against that case. |
| 2642 | if (mDragging) { |
| 2643 | mDragging = false; |
Patrick Dubroy | de7658b | 2010-09-27 11:15:43 -0700 | [diff] [blame] | 2644 | } |
Patrick Dubroy | 08ae2ec | 2010-10-14 23:54:22 -0700 | [diff] [blame] | 2645 | |
| 2646 | // Invalidate the drag data |
Sebastian Franco | 5aa71ce | 2022-12-14 12:13:19 -0600 | [diff] [blame] | 2647 | mPreviousSolution = null; |
Adam Cohen | d41fbf5 | 2012-02-16 23:53:59 -0800 | [diff] [blame] | 2648 | mDragCell[0] = mDragCell[1] = -1; |
Adam Cohen | f7ca3b4 | 2021-02-22 11:03:58 -0800 | [diff] [blame] | 2649 | mDragCellSpan[0] = mDragCellSpan[1] = -1; |
Patrick Dubroy | 08ae2ec | 2010-10-14 23:54:22 -0700 | [diff] [blame] | 2650 | mDragOutlineAnims[mDragOutlineCurrent].animateOut(); |
| 2651 | mDragOutlineCurrent = (mDragOutlineCurrent + 1) % mDragOutlineAnims.length; |
Adam Cohen | 19f3792 | 2012-03-21 11:59:11 -0700 | [diff] [blame] | 2652 | revertTempState(); |
Michael Jurka | 33945b2 | 2010-12-21 18:19:38 -0800 | [diff] [blame] | 2653 | setIsDragOverlapping(false); |
Patrick Dubroy | 6569f2c | 2010-07-12 14:25:18 -0700 | [diff] [blame] | 2654 | } |
| 2655 | |
| 2656 | /** |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 2657 | * Mark a child as having been dropped. |
Patrick Dubroy | de7658b | 2010-09-27 11:15:43 -0700 | [diff] [blame] | 2658 | * At the beginning of the drag operation, the child may have been on another |
Patrick Dubroy | ce34a97 | 2010-10-19 10:34:32 -0700 | [diff] [blame] | 2659 | * screen, but it is re-parented before this method is called. |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 2660 | * |
| 2661 | * @param child The child that is being dropped |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 2662 | */ |
Adam Cohen | 716b51e | 2011-06-30 12:09:54 -0700 | [diff] [blame] | 2663 | void onDropChild(View child) { |
Romain Guy | d94533d | 2009-08-17 10:01:15 -0700 | [diff] [blame] | 2664 | if (child != null) { |
Sebastian Franco | d468299 | 2022-10-05 13:03:09 -0500 | [diff] [blame] | 2665 | CellLayoutLayoutParams |
| 2666 | lp = (CellLayoutLayoutParams) child.getLayoutParams(); |
Romain Guy | 84f296c | 2009-11-04 15:00:44 -0800 | [diff] [blame] | 2667 | lp.dropped = true; |
Romain Guy | d94533d | 2009-08-17 10:01:15 -0700 | [diff] [blame] | 2668 | child.requestLayout(); |
Tony Wickham | 1cdb6d0 | 2015-09-17 11:08:27 -0700 | [diff] [blame] | 2669 | markCellsAsOccupiedForView(child); |
Romain Guy | d94533d | 2009-08-17 10:01:15 -0700 | [diff] [blame] | 2670 | } |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 2671 | } |
| 2672 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 2673 | /** |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 2674 | * Computes a bounding rectangle for a range of cells |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 2675 | * |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 2676 | * @param cellX X coordinate of upper left corner expressed as a cell position |
| 2677 | * @param cellY Y coordinate of upper left corner expressed as a cell position |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 2678 | * @param cellHSpan Width in cells |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 2679 | * @param cellVSpan Height in cells |
Patrick Dubroy | 6569f2c | 2010-07-12 14:25:18 -0700 | [diff] [blame] | 2680 | * @param resultRect Rect into which to put the results |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 2681 | */ |
Adam Cohen | d41fbf5 | 2012-02-16 23:53:59 -0800 | [diff] [blame] | 2682 | public void cellToRect(int cellX, int cellY, int cellHSpan, int cellVSpan, Rect resultRect) { |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 2683 | final int cellWidth = mCellWidth; |
| 2684 | final int cellHeight = mCellHeight; |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 2685 | |
Pierre Barbier de Reuille | 1b8bbb6 | 2021-05-19 22:45:16 +0100 | [diff] [blame] | 2686 | // We observe a shift of 1 pixel on the x coordinate compared to the actual cell coordinates |
| 2687 | final int hStartPadding = getPaddingLeft() |
| 2688 | + (int) Math.ceil(getUnusedHorizontalSpace() / 2f); |
Winson Chung | 4b825dcd | 2011-06-19 12:41:22 -0700 | [diff] [blame] | 2689 | final int vStartPadding = getPaddingTop(); |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 2690 | |
Thales Lima | 78d00ad | 2021-09-30 11:29:06 +0100 | [diff] [blame] | 2691 | int x = hStartPadding + (cellX * mBorderSpace.x) + (cellX * cellWidth); |
| 2692 | int y = vStartPadding + (cellY * mBorderSpace.y) + (cellY * cellHeight); |
Jon Miranda | 228877d | 2021-02-09 11:05:00 -0500 | [diff] [blame] | 2693 | |
Thales Lima | 78d00ad | 2021-09-30 11:29:06 +0100 | [diff] [blame] | 2694 | int width = cellHSpan * cellWidth + ((cellHSpan - 1) * mBorderSpace.x); |
| 2695 | int height = cellVSpan * cellHeight + ((cellVSpan - 1) * mBorderSpace.y); |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 2696 | |
Patrick Dubroy | 6569f2c | 2010-07-12 14:25:18 -0700 | [diff] [blame] | 2697 | resultRect.set(x, y, x + width, y + height); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 2698 | } |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 2699 | |
Adam Cohen | d4844c3 | 2011-02-18 19:25:06 -0800 | [diff] [blame] | 2700 | public void markCellsAsOccupiedForView(View view) { |
Sebastian Franco | f153d91 | 2022-04-22 16:15:27 -0500 | [diff] [blame] | 2701 | if (view instanceof LauncherAppWidgetHostView |
| 2702 | && view.getTag() instanceof LauncherAppWidgetInfo) { |
| 2703 | LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) view.getTag(); |
Sunny Goyal | 669b71f | 2023-01-27 14:37:07 -0800 | [diff] [blame] | 2704 | CellPos pos = mActivity.getCellPosMapper().mapModelToPresenter(info); |
| 2705 | mOccupied.markCells(pos.cellX, pos.cellY, info.spanX, info.spanY, true); |
Sebastian Franco | f153d91 | 2022-04-22 16:15:27 -0500 | [diff] [blame] | 2706 | return; |
| 2707 | } |
Michael Jurka | a52570f | 2012-03-20 03:18:20 -0700 | [diff] [blame] | 2708 | if (view == null || view.getParent() != mShortcutsAndWidgets) return; |
Sebastian Franco | d468299 | 2022-10-05 13:03:09 -0500 | [diff] [blame] | 2709 | CellLayoutLayoutParams |
| 2710 | lp = (CellLayoutLayoutParams) view.getLayoutParams(); |
Sebastian Franco | 877088e | 2023-01-03 15:16:22 -0700 | [diff] [blame] | 2711 | mOccupied.markCells(lp.getCellX(), lp.getCellY(), lp.cellHSpan, lp.cellVSpan, true); |
Michael Jurka | 0280c3b | 2010-09-17 15:00:07 -0700 | [diff] [blame] | 2712 | } |
| 2713 | |
Adam Cohen | d4844c3 | 2011-02-18 19:25:06 -0800 | [diff] [blame] | 2714 | public void markCellsAsUnoccupiedForView(View view) { |
Sebastian Franco | f153d91 | 2022-04-22 16:15:27 -0500 | [diff] [blame] | 2715 | if (view instanceof LauncherAppWidgetHostView |
| 2716 | && view.getTag() instanceof LauncherAppWidgetInfo) { |
| 2717 | LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) view.getTag(); |
Sunny Goyal | 669b71f | 2023-01-27 14:37:07 -0800 | [diff] [blame] | 2718 | CellPos pos = mActivity.getCellPosMapper().mapModelToPresenter(info); |
| 2719 | mOccupied.markCells(pos.cellX, pos.cellY, info.spanX, info.spanY, false); |
Sebastian Franco | f153d91 | 2022-04-22 16:15:27 -0500 | [diff] [blame] | 2720 | return; |
| 2721 | } |
Michael Jurka | a52570f | 2012-03-20 03:18:20 -0700 | [diff] [blame] | 2722 | if (view == null || view.getParent() != mShortcutsAndWidgets) return; |
Sebastian Franco | d468299 | 2022-10-05 13:03:09 -0500 | [diff] [blame] | 2723 | CellLayoutLayoutParams |
| 2724 | lp = (CellLayoutLayoutParams) view.getLayoutParams(); |
Sebastian Franco | 877088e | 2023-01-03 15:16:22 -0700 | [diff] [blame] | 2725 | mOccupied.markCells(lp.getCellX(), lp.getCellY(), lp.cellHSpan, lp.cellVSpan, false); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 2726 | } |
| 2727 | |
Adam Cohen | 2801caf | 2011-05-13 20:57:39 -0700 | [diff] [blame] | 2728 | public int getDesiredWidth() { |
Jon Miranda | 228877d | 2021-02-09 11:05:00 -0500 | [diff] [blame] | 2729 | return getPaddingLeft() + getPaddingRight() + (mCountX * mCellWidth) |
Thales Lima | 78d00ad | 2021-09-30 11:29:06 +0100 | [diff] [blame] | 2730 | + ((mCountX - 1) * mBorderSpace.x); |
Adam Cohen | 2801caf | 2011-05-13 20:57:39 -0700 | [diff] [blame] | 2731 | } |
| 2732 | |
| 2733 | public int getDesiredHeight() { |
Jon Miranda | 228877d | 2021-02-09 11:05:00 -0500 | [diff] [blame] | 2734 | return getPaddingTop() + getPaddingBottom() + (mCountY * mCellHeight) |
Thales Lima | 78d00ad | 2021-09-30 11:29:06 +0100 | [diff] [blame] | 2735 | + ((mCountY - 1) * mBorderSpace.y); |
Adam Cohen | 2801caf | 2011-05-13 20:57:39 -0700 | [diff] [blame] | 2736 | } |
| 2737 | |
Michael Jurka | 66d7217 | 2011-04-12 16:29:25 -0700 | [diff] [blame] | 2738 | public boolean isOccupied(int x, int y) { |
Schneider Victor-tulias | 750c5af | 2023-07-26 10:16:04 -0700 | [diff] [blame] | 2739 | if (x >= 0 && x < mCountX && y >= 0 && y < mCountY) { |
Sunny Goyal | ff4ba2d | 2016-04-02 14:12:34 -0700 | [diff] [blame] | 2740 | return mOccupied.cells[x][y]; |
Schneider Victor-tulias | 750c5af | 2023-07-26 10:16:04 -0700 | [diff] [blame] | 2741 | } |
| 2742 | if (BuildConfig.IS_STUDIO_BUILD) { |
Michael Jurka | 66d7217 | 2011-04-12 16:29:25 -0700 | [diff] [blame] | 2743 | throw new RuntimeException("Position exceeds the bound of this CellLayout"); |
| 2744 | } |
Schneider Victor-tulias | 750c5af | 2023-07-26 10:16:04 -0700 | [diff] [blame] | 2745 | return true; |
Michael Jurka | 66d7217 | 2011-04-12 16:29:25 -0700 | [diff] [blame] | 2746 | } |
| 2747 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 2748 | @Override |
| 2749 | public ViewGroup.LayoutParams generateLayoutParams(AttributeSet attrs) { |
Sebastian Franco | d468299 | 2022-10-05 13:03:09 -0500 | [diff] [blame] | 2750 | return new CellLayoutLayoutParams(getContext(), attrs); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 2751 | } |
| 2752 | |
| 2753 | @Override |
| 2754 | protected boolean checkLayoutParams(ViewGroup.LayoutParams p) { |
Sebastian Franco | d468299 | 2022-10-05 13:03:09 -0500 | [diff] [blame] | 2755 | return p instanceof CellLayoutLayoutParams; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 2756 | } |
| 2757 | |
| 2758 | @Override |
| 2759 | protected ViewGroup.LayoutParams generateLayoutParams(ViewGroup.LayoutParams p) { |
Sebastian Franco | d468299 | 2022-10-05 13:03:09 -0500 | [diff] [blame] | 2760 | return new CellLayoutLayoutParams(p); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 2761 | } |
| 2762 | |
Michael Jurka | 0280c3b | 2010-09-17 15:00:07 -0700 | [diff] [blame] | 2763 | // This class stores info for two purposes: |
| 2764 | // 1. When dragging items (mDragInfo in Workspace), we store the View, its cellX & cellY, |
| 2765 | // its spanX, spanY, and the screen it is on |
| 2766 | // 2. When long clicking on an empty cell in a CellLayout, we save information about the |
| 2767 | // cellX and cellY coordinates and which page was clicked. We then set this as a tag on |
| 2768 | // the CellLayout that was long clicked |
Sunny Goyal | ff4ba2d | 2016-04-02 14:12:34 -0700 | [diff] [blame] | 2769 | public static final class CellInfo extends CellAndSpan { |
Rajeev Kumar | 9962dbe | 2017-06-12 12:16:20 -0700 | [diff] [blame] | 2770 | public final View cell; |
Sunny Goyal | efb7e84 | 2018-10-04 15:11:00 -0700 | [diff] [blame] | 2771 | final int screenId; |
| 2772 | final int container; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 2773 | |
Sunny Goyal | 669b71f | 2023-01-27 14:37:07 -0800 | [diff] [blame] | 2774 | public CellInfo(View v, ItemInfo info, CellPos cellPos) { |
| 2775 | cellX = cellPos.cellX; |
| 2776 | cellY = cellPos.cellY; |
Adam Cohen | e0aaa0d | 2014-05-12 12:44:22 -0700 | [diff] [blame] | 2777 | spanX = info.spanX; |
| 2778 | spanY = info.spanY; |
Sunny Goyal | ff4ba2d | 2016-04-02 14:12:34 -0700 | [diff] [blame] | 2779 | cell = v; |
Sunny Goyal | 669b71f | 2023-01-27 14:37:07 -0800 | [diff] [blame] | 2780 | screenId = cellPos.screenId; |
Adam Cohen | e0aaa0d | 2014-05-12 12:44:22 -0700 | [diff] [blame] | 2781 | container = info.container; |
| 2782 | } |
| 2783 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 2784 | @Override |
| 2785 | public String toString() { |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 2786 | return "Cell[view=" + (cell == null ? "null" : cell.getClass()) |
| 2787 | + ", x=" + cellX + ", y=" + cellY + "]"; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 2788 | } |
| 2789 | } |
Michael Jurka | d771c96 | 2011-08-09 15:00:48 -0700 | [diff] [blame] | 2790 | |
Tony Wickham | 8693061 | 2015-09-09 13:50:40 -0700 | [diff] [blame] | 2791 | /** |
Samuel Fufa | 1e2d004 | 2019-11-18 17:12:46 -0800 | [diff] [blame] | 2792 | * A Delegated cell Drawing for drawing on CellLayout |
| 2793 | */ |
| 2794 | public abstract static class DelegatedCellDrawing { |
| 2795 | public int mDelegateCellX; |
| 2796 | public int mDelegateCellY; |
| 2797 | |
| 2798 | /** |
| 2799 | * Draw under CellLayout |
| 2800 | */ |
| 2801 | public abstract void drawUnderItem(Canvas canvas); |
| 2802 | |
| 2803 | /** |
| 2804 | * Draw over CellLayout |
| 2805 | */ |
| 2806 | public abstract void drawOverItem(Canvas canvas); |
| 2807 | } |
| 2808 | |
| 2809 | /** |
Tony Wickham | 8693061 | 2015-09-09 13:50:40 -0700 | [diff] [blame] | 2810 | * Returns whether an item can be placed in this CellLayout (after rearranging and/or resizing |
| 2811 | * if necessary). |
| 2812 | */ |
| 2813 | public boolean hasReorderSolution(ItemInfo itemInfo) { |
| 2814 | int[] cellPoint = new int[2]; |
| 2815 | // Check for a solution starting at every cell. |
| 2816 | for (int cellX = 0; cellX < getCountX(); cellX++) { |
| 2817 | for (int cellY = 0; cellY < getCountY(); cellY++) { |
| 2818 | cellToPoint(cellX, cellY, cellPoint); |
| 2819 | if (findReorderSolution(cellPoint[0], cellPoint[1], itemInfo.minSpanX, |
| 2820 | itemInfo.minSpanY, itemInfo.spanX, itemInfo.spanY, mDirectionVector, null, |
| 2821 | true, new ItemConfiguration()).isSolution) { |
| 2822 | return true; |
| 2823 | } |
| 2824 | } |
| 2825 | } |
| 2826 | return false; |
| 2827 | } |
| 2828 | |
Samuel Fufa | a421143 | 2020-02-25 18:47:54 -0800 | [diff] [blame] | 2829 | /** |
| 2830 | * Finds solution to accept hotseat migration to cell layout. commits solution if commitConfig |
| 2831 | */ |
| 2832 | public boolean makeSpaceForHotseatMigration(boolean commitConfig) { |
Samuel Fufa | a421143 | 2020-02-25 18:47:54 -0800 | [diff] [blame] | 2833 | int[] cellPoint = new int[2]; |
| 2834 | int[] directionVector = new int[]{0, -1}; |
| 2835 | cellToPoint(0, mCountY, cellPoint); |
| 2836 | ItemConfiguration configuration = new ItemConfiguration(); |
| 2837 | if (findReorderSolution(cellPoint[0], cellPoint[1], mCountX, 1, mCountX, 1, |
| 2838 | directionVector, null, false, configuration).isSolution) { |
| 2839 | if (commitConfig) { |
| 2840 | copySolutionToTempState(configuration, null); |
Sunny Goyal | 711c596 | 2021-06-23 12:36:18 -0700 | [diff] [blame] | 2841 | commitTempPlacement(null); |
Samuel Fufa | 82bbdac | 2020-03-09 18:24:47 -0700 | [diff] [blame] | 2842 | // undo marking cells occupied since there is actually nothing being placed yet. |
| 2843 | mOccupied.markCells(0, mCountY - 1, mCountX, 1, false); |
Samuel Fufa | a421143 | 2020-02-25 18:47:54 -0800 | [diff] [blame] | 2844 | } |
| 2845 | return true; |
| 2846 | } |
| 2847 | return false; |
| 2848 | } |
| 2849 | |
Samuel Fufa | 82bbdac | 2020-03-09 18:24:47 -0700 | [diff] [blame] | 2850 | /** |
| 2851 | * returns a copy of cell layout's grid occupancy |
| 2852 | */ |
| 2853 | public GridOccupancy cloneGridOccupancy() { |
| 2854 | GridOccupancy occupancy = new GridOccupancy(mCountX, mCountY); |
| 2855 | mOccupied.copyTo(occupancy); |
| 2856 | return occupancy; |
| 2857 | } |
| 2858 | |
Sunny Goyal | 9ca9c13 | 2015-04-29 14:57:22 -0700 | [diff] [blame] | 2859 | public boolean isRegionVacant(int x, int y, int spanX, int spanY) { |
Sunny Goyal | ff4ba2d | 2016-04-02 14:12:34 -0700 | [diff] [blame] | 2860 | return mOccupied.isRegionVacant(x, y, spanX, spanY); |
Sunny Goyal | 9ca9c13 | 2015-04-29 14:57:22 -0700 | [diff] [blame] | 2861 | } |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 2862 | } |