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