blob: 941a7932872137153e2ebdf32c9dce8177c5cc4a [file] [log] [blame]
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001/*
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 Sandler325dc232013-06-05 22:57:57 -040017package com.android.launcher3;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080018
Tony Wickham0ac045f2021-11-03 13:17:02 -070019import static com.android.launcher3.dragndrop.DraggableView.DRAGGABLE_ICON;
Tony Wickham12784902021-11-03 14:02:10 -070020import static com.android.launcher3.icons.IconNormalizer.ICON_VISIBLE_AREA_FACTOR;
Sunny Goyal82dfc152023-02-24 16:50:09 -080021import static com.android.launcher3.util.MultiTranslateDelegate.INDEX_REORDER_PREVIEW_OFFSET;
Sunny Goyalf0b6db72018-08-13 16:10:14 -070022
Joe Onorato4be866d2010-10-10 11:26:02 -070023import android.animation.Animator;
Michael Jurka629758f2012-06-14 16:18:21 -070024import android.animation.AnimatorListenerAdapter;
Chet Haase00397b12010-10-07 11:13:10 -070025import android.animation.TimeInterpolator;
Patrick Dubroyde7658b2010-09-27 11:15:43 -070026import android.animation.ValueAnimator;
27import android.animation.ValueAnimator.AnimatorUpdateListener;
Sunny Goyal726bee72018-03-05 12:54:24 -080028import android.annotation.SuppressLint;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080029import android.content.Context;
Joe Onorato79e56262009-09-21 15:23:04 -040030import android.content.res.Resources;
Sunny Goyalc13403c2016-11-18 23:44:48 -080031import android.content.res.TypedArray;
Winson Chungaafa03c2010-06-11 17:34:16 -070032import android.graphics.Canvas;
Andrew Flynn0dca1ec2012-02-29 13:33:22 -080033import android.graphics.Color;
Joe Onorato4be866d2010-10-10 11:26:02 -070034import android.graphics.Paint;
Patrick Dubroyde7658b2010-09-27 11:15:43 -070035import android.graphics.Point;
Adam Cohend9162062020-03-24 16:35:35 -070036import android.graphics.PointF;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080037import android.graphics.Rect;
Adam Cohenf7ca3b42021-02-22 11:03:58 -080038import android.graphics.RectF;
Patrick Dubroy6569f2c2010-07-12 14:25:18 -070039import android.graphics.drawable.Drawable;
Adam Cohen1462de32012-07-24 22:34:36 -070040import android.os.Parcelable;
Rajeev Kumar9962dbe2017-06-12 12:16:20 -070041import android.util.ArrayMap;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080042import android.util.AttributeSet;
Adam Cohenf7ca3b42021-02-22 11:03:58 -080043import android.util.FloatProperty;
Joe Onorato4be866d2010-10-10 11:26:02 -070044import android.util.Log;
Adam Cohen1462de32012-07-24 22:34:36 -070045import android.util.SparseArray;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080046import android.view.MotionEvent;
47import android.view.View;
48import android.view.ViewDebug;
49import android.view.ViewGroup;
Adam Cohenc9735cf2015-01-23 16:11:55 -080050import android.view.accessibility.AccessibilityEvent;
Sunny Goyal5bc6b6f2017-10-26 15:36:10 -070051
vadimt04f356f2019-02-14 18:46:36 -080052import androidx.annotation.IntDef;
Sebastian Franco5c8f8682023-11-14 09:52:41 -060053import androidx.annotation.Nullable;
Sebastian Franco9ea36d42023-09-21 13:56:42 -070054import androidx.annotation.Px;
Adam Cohenf7ca3b42021-02-22 11:03:58 -080055import androidx.core.graphics.ColorUtils;
vadimt04f356f2019-02-14 18:46:36 -080056import androidx.core.view.ViewCompat;
57
Kateryna Ivanova71203732023-05-24 15:09:00 +000058import com.android.app.animation.Interpolators;
Sunny Goyalaa8ef112015-06-12 20:04:41 -070059import com.android.launcher3.LauncherSettings.Favorites;
Sunny Goyale9b651e2015-04-24 11:44:51 -070060import com.android.launcher3.accessibility.DragAndDropAccessibilityDelegate;
Sebastian Francod4682992022-10-05 13:03:09 -050061import com.android.launcher3.celllayout.CellLayoutLayoutParams;
Sunny Goyal669b71f2023-01-27 14:37:07 -080062import com.android.launcher3.celllayout.CellPosMapper.CellPos;
Sebastian Franco5f0af4f2023-11-21 10:45:45 -060063import com.android.launcher3.celllayout.DelegatedCellDrawing;
64import com.android.launcher3.celllayout.ItemConfiguration;
Sebastian Francoe4c03452022-12-27 14:50:02 -060065import com.android.launcher3.celllayout.ReorderAlgorithm;
Sebastian Franco25f8e402023-12-02 13:54:05 -060066import com.android.launcher3.celllayout.ReorderParameters;
Sebastian Francof7654252024-01-18 11:50:50 -080067import com.android.launcher3.celllayout.ReorderPreviewAnimation;
Sunny Goyal3d706ad2017-03-06 16:56:39 -080068import com.android.launcher3.config.FeatureFlags;
Tony Wickham0ac045f2021-11-03 13:17:02 -070069import com.android.launcher3.dragndrop.DraggableView;
Jon Mirandaa0233f72017-06-22 18:34:45 -070070import com.android.launcher3.folder.PreviewBackground;
Sunny Goyale396abf2020-04-06 15:11:17 -070071import com.android.launcher3.model.data.ItemInfo;
Sebastian Francof153d912022-04-22 16:15:27 -050072import com.android.launcher3.model.data.LauncherAppWidgetInfo;
Sunny Goyalff4ba2d2016-04-02 14:12:34 -070073import com.android.launcher3.util.CellAndSpan;
74import com.android.launcher3.util.GridOccupancy;
Sunny Goyal82dfc152023-02-24 16:50:09 -080075import com.android.launcher3.util.MultiTranslateDelegate;
Sunny Goyale2fd14b2015-08-27 17:45:46 -070076import com.android.launcher3.util.ParcelableSparseArray;
Sunny Goyal9b29ca52017-02-17 10:39:44 -080077import com.android.launcher3.util.Themes;
Adam Cohen091440a2015-03-18 14:16:05 -070078import com.android.launcher3.util.Thunk;
Sunny Goyalab770a12018-11-14 15:17:26 -080079import com.android.launcher3.views.ActivityContext;
Steven Ng32427202021-04-19 18:12:12 +010080import com.android.launcher3.widget.LauncherAppWidgetHostView;
Sunny Goyal5bc6b6f2017-10-26 15:36:10 -070081
Sunny Goyalc13403c2016-11-18 23:44:48 -080082import java.lang.annotation.Retention;
83import java.lang.annotation.RetentionPolicy;
Adam Cohen69ce2e52011-07-03 19:25:21 -070084import java.util.ArrayList;
Adam Cohenc0dcf592011-06-01 15:30:43 -070085import java.util.Arrays;
Adam Cohend41fbf52012-02-16 23:53:59 -080086import java.util.Stack;
Adam Cohenc0dcf592011-06-01 15:30:43 -070087
Sunny Goyalc4d32012020-04-03 17:10:11 -070088public class CellLayout extends ViewGroup {
Tony Wickhama0628cc2015-10-14 15:23:04 -070089 private static final String TAG = "CellLayout";
90 private static final boolean LOGD = false;
Winson Chungaafa03c2010-06-11 17:34:16 -070091
Tony Wickhamec6fd6f2023-03-11 02:08:57 +000092 /** The color of the "leave-behind" shape when a folder is opened from Hotseat. */
93 private static final int FOLDER_LEAVE_BEHIND_COLOR = Color.argb(160, 245, 245, 245);
94
Sunny Goyalab770a12018-11-14 15:17:26 -080095 protected final ActivityContext mActivity;
Sunny Goyal4ffec482016-02-09 11:28:52 -080096 @ViewDebug.ExportedProperty(category = "launcher")
Adam Cohen091440a2015-03-18 14:16:05 -070097 @Thunk int mCellWidth;
Sunny Goyal4ffec482016-02-09 11:28:52 -080098 @ViewDebug.ExportedProperty(category = "launcher")
Adam Cohen091440a2015-03-18 14:16:05 -070099 @Thunk int mCellHeight;
Winson Chung11a1a532013-09-13 11:14:45 -0700100 private int mFixedCellWidth;
101 private int mFixedCellHeight;
Jon Miranda228877d2021-02-09 11:05:00 -0500102 @ViewDebug.ExportedProperty(category = "launcher")
Sebastian Franco25423862023-03-10 10:50:37 -0800103 protected Point mBorderSpace;
Winson Chungaafa03c2010-06-11 17:34:16 -0700104
Sunny Goyal4ffec482016-02-09 11:28:52 -0800105 @ViewDebug.ExportedProperty(category = "launcher")
Sebastian Franco09589322022-11-02 15:25:58 -0700106 protected int mCountX;
Sunny Goyal4ffec482016-02-09 11:28:52 -0800107 @ViewDebug.ExportedProperty(category = "launcher")
Sebastian Franco09589322022-11-02 15:25:58 -0700108 protected int mCountY;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800109
Adam Cohen917e3882013-10-31 15:03:35 -0700110 private boolean mDropPending = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800111
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700112 // These are temporary variables to prevent having to allocate a new object just to
113 // return an (x, y) value from helper functions. Do NOT use them to maintain other state.
Adam Cohen091440a2015-03-18 14:16:05 -0700114 @Thunk final int[] mTmpPoint = new int[2];
Sunny Goyal2805e632015-05-20 15:35:32 -0700115 @Thunk final int[] mTempLocation = new int[2];
Sebastian Franco0dd5db82023-10-13 11:09:21 -0700116
117 @Thunk final Rect mTempOnDrawCellToRect = new Rect();
Adam Cohend9162062020-03-24 16:35:35 -0700118 final PointF mTmpPointF = new PointF();
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700119
Sebastian Franco09589322022-11-02 15:25:58 -0700120 protected GridOccupancy mOccupied;
Sebastian Francoe4c03452022-12-27 14:50:02 -0600121 public GridOccupancy mTmpOccupied;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800122
Michael Jurkadee05892010-07-27 10:01:56 -0700123 private OnTouchListener mInterceptTouchListener;
124
Samuel Fufa1e2d0042019-11-18 17:12:46 -0800125 private final ArrayList<DelegatedCellDrawing> mDelegatedCellDrawings = new ArrayList<>();
Jon Mirandaa0233f72017-06-22 18:34:45 -0700126 final PreviewBackground mFolderLeaveBehind = new PreviewBackground();
Adam Cohen69ce2e52011-07-03 19:25:21 -0700127
Sunny Goyalf5440cb2016-12-14 15:13:00 -0800128 private static final int[] BACKGROUND_STATE_ACTIVE = new int[] { android.R.attr.state_active };
Sunny Goyale15e2a82017-12-15 13:05:42 -0800129 private static final int[] BACKGROUND_STATE_DEFAULT = EMPTY_STATE_SET;
Sebastian Franco09589322022-11-02 15:25:58 -0700130 protected final Drawable mBackground;
Sunny Goyal2805e632015-05-20 15:35:32 -0700131
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700132 // These values allow a fixed measurement to be set on the CellLayout.
133 private int mFixedWidth = -1;
134 private int mFixedHeight = -1;
135
Michael Jurka33945b22010-12-21 18:19:38 -0800136 // If we're actively dragging something over this screen, mIsDragOverlapping is true
137 private boolean mIsDragOverlapping = false;
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700138
Winson Chung150fbab2010-09-29 17:14:26 -0700139 // These arrays are used to implement the drag visualization on x-large screens.
Joe Onorato4be866d2010-10-10 11:26:02 -0700140 // They are used as circular arrays, indexed by mDragOutlineCurrent.
Sebastian Francod4682992022-10-05 13:03:09 -0500141 @Thunk final CellLayoutLayoutParams[] mDragOutlines = new CellLayoutLayoutParams[4];
Rajeev Kumar9962dbe2017-06-12 12:16:20 -0700142 @Thunk final float[] mDragOutlineAlphas = new float[mDragOutlines.length];
143 private final InterruptibleInOutAnimator[] mDragOutlineAnims =
Joe Onorato4be866d2010-10-10 11:26:02 -0700144 new InterruptibleInOutAnimator[mDragOutlines.length];
Winson Chung150fbab2010-09-29 17:14:26 -0700145
146 // Used as an index into the above 3 arrays; indicates which is the most current value.
Joe Onorato4be866d2010-10-10 11:26:02 -0700147 private int mDragOutlineCurrent = 0;
Patrick Dubroy8e58e912010-10-14 13:21:48 -0700148 private final Paint mDragOutlinePaint = new Paint();
Winson Chung150fbab2010-09-29 17:14:26 -0700149
Sebastian Francod4682992022-10-05 13:03:09 -0500150 @Thunk final ArrayMap<CellLayoutLayoutParams, Animator> mReorderAnimators = new ArrayMap<>();
Adam Cohend9162062020-03-24 16:35:35 -0700151 @Thunk final ArrayMap<Reorderable, ReorderPreviewAnimation> mShakeAnimators = new ArrayMap<>();
Adam Cohen19f37922012-03-21 11:59:11 -0700152
153 private boolean mItemPlacementDirty = false;
Adam Cohenbfbfd262011-06-13 16:55:12 -0700154
Adam Cohenf7ca3b42021-02-22 11:03:58 -0800155 // Used to visualize the grid and drop locations
156 private boolean mVisualizeCells = false;
157 private boolean mVisualizeDropLocation = true;
158 private RectF mVisualizeGridRect = new RectF();
159 private Paint mVisualizeGridPaint = new Paint();
Adam Cohenf7ca3b42021-02-22 11:03:58 -0800160 private int mGridVisualizationRoundingRadius;
161 private float mGridAlpha = 0f;
162 private int mGridColor = 0;
Sebastian Franco09589322022-11-02 15:25:58 -0700163 protected float mSpringLoadedProgress = 0f;
Adam Cohenf7ca3b42021-02-22 11:03:58 -0800164 private float mScrollProgress = 0f;
165
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700166 // When a drag operation is in progress, holds the nearest cell to the touch point
167 private final int[] mDragCell = new int[2];
Adam Cohenf7ca3b42021-02-22 11:03:58 -0800168 private final int[] mDragCellSpan = new int[2];
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800169
Joe Onorato4be866d2010-10-10 11:26:02 -0700170 private boolean mDragging = false;
171
Rajeev Kumar9962dbe2017-06-12 12:16:20 -0700172 private final TimeInterpolator mEaseOutInterpolator;
Sebastian Franco09589322022-11-02 15:25:58 -0700173 protected final ShortcutAndWidgetContainer mShortcutsAndWidgets;
Sebastian Franco9ea36d42023-09-21 13:56:42 -0700174 @Px
175 protected int mSpaceBetweenCellLayoutsPx = 0;
Patrick Dubroyce34a972010-10-19 10:34:32 -0700176
Sunny Goyalc13403c2016-11-18 23:44:48 -0800177 @Retention(RetentionPolicy.SOURCE)
178 @IntDef({WORKSPACE, HOTSEAT, FOLDER})
179 public @interface ContainerType{}
180 public static final int WORKSPACE = 0;
181 public static final int HOTSEAT = 1;
182 public static final int FOLDER = 2;
183
184 @ContainerType private final int mContainerType;
185
Sebastian Francof7654252024-01-18 11:50:50 -0800186 public static final float DEFAULT_SCALE = 1f;
Andrew Flynn0dca1ec2012-02-29 13:33:22 -0800187
Adam Cohenfa3c58f2013-12-06 16:10:55 -0800188 public static final int MODE_SHOW_REORDER_HINT = 0;
189 public static final int MODE_DRAG_OVER = 1;
190 public static final int MODE_ON_DROP = 2;
191 public static final int MODE_ON_DROP_EXTERNAL = 3;
192 public static final int MODE_ACCEPT_DROP = 4;
Adam Cohen19f37922012-03-21 11:59:11 -0700193 private static final boolean DESTRUCTIVE_REORDER = false;
Adam Cohen482ed822012-03-02 14:15:13 -0800194 private static final boolean DEBUG_VISUALIZE_OCCUPIED = false;
195
Sebastian Francof7654252024-01-18 11:50:50 -0800196 public static final float REORDER_PREVIEW_MAGNITUDE = 0.12f;
197 public static final int REORDER_ANIMATION_DURATION = 150;
Sunny Goyalc13403c2016-11-18 23:44:48 -0800198 @Thunk final float mReorderPreviewAnimationMagnitude;
Adam Cohen19f37922012-03-21 11:59:11 -0700199
Rajeev Kumar9962dbe2017-06-12 12:16:20 -0700200 private final ArrayList<View> mIntersectingViews = new ArrayList<>();
201 private final Rect mOccupiedRect = new Rect();
Sebastian Francoe4c03452022-12-27 14:50:02 -0600202 public final int[] mDirectionVector = new int[2];
Steven Ng30dd1d62021-03-15 21:45:49 +0000203
Sebastian Franco53a15a42022-10-25 17:28:54 -0700204 ItemConfiguration mPreviousSolution = null;
Adam Cohenb209e632012-03-27 17:09:36 -0700205 private static final int INVALID_DIRECTION = -100;
Adam Cohen482ed822012-03-02 14:15:13 -0800206
Sunny Goyal2805e632015-05-20 15:35:32 -0700207 private final Rect mTempRect = new Rect();
Jonathan Miranda21930da2021-05-03 18:44:13 +0000208 private final RectF mTempRectF = new RectF();
Jon Miranda88b7f6a2021-05-03 16:49:53 -0700209 private final float[] mTmpFloatArray = new float[4];
Winson Chung3a6e7f32013-10-09 15:50:52 -0700210
Sunny Goyal73b5a272019-12-09 14:55:56 -0800211 private static final Paint sPaint = new Paint();
Romain Guy8a0bff52012-05-06 13:14:33 -0700212
Adam Cohenc9735cf2015-01-23 16:11:55 -0800213 // Related to accessible drag and drop
Adam Cohen6e7c37a2020-06-25 19:22:37 -0700214 DragAndDropAccessibilityDelegate mTouchHelper;
Adam Cohenc9735cf2015-01-23 16:11:55 -0800215
Sebastian Franco2986e0b2024-01-25 10:23:39 -0800216 CellLayoutContainer mCellLayoutContainer;
Adam Cohenf7ca3b42021-02-22 11:03:58 -0800217
218 public static final FloatProperty<CellLayout> SPRING_LOADED_PROGRESS =
219 new FloatProperty<CellLayout>("spring_loaded_progress") {
220 @Override
221 public Float get(CellLayout cl) {
222 return cl.getSpringLoadedProgress();
223 }
224
225 @Override
226 public void setValue(CellLayout cl, float progress) {
227 cl.setSpringLoadedProgress(progress);
228 }
229 };
230
Sebastian Franco2986e0b2024-01-25 10:23:39 -0800231 public CellLayout(Context context, CellLayoutContainer container) {
232 this(context, (AttributeSet) null);
233 this.mCellLayoutContainer = container;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800234 }
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 Goyalc13403c2016-11-18 23:44:48 -0800242 TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.CellLayout, defStyle, 0);
243 mContainerType = a.getInteger(R.styleable.CellLayout_containerType, WORKSPACE);
244 a.recycle();
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700245
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 Guyce3cbd12013-02-25 15:00:36 -0800249 setClipToPadding(false);
Pat Manningd0f729d2023-01-09 12:04:25 +0000250 setClipChildren(false);
Sunny Goyalab770a12018-11-14 15:17:26 -0800251 mActivity = ActivityContext.lookupContext(context);
Steven Ngcc505b82021-03-18 23:04:35 +0000252 DeviceProfile deviceProfile = mActivity.getDeviceProfile();
Michael Jurkaa63c4522010-08-19 13:52:27 -0700253
Thales Lima8cd020b2022-03-15 20:15:14 +0000254 resetCellSizeInternal(deviceProfile);
Sunny Goyalff4ba2d2016-04-02 14:12:34 -0700255
Steven Ngcc505b82021-03-18 23:04:35 +0000256 mCountX = deviceProfile.inv.numColumns;
257 mCountY = deviceProfile.inv.numRows;
Sunny Goyalff4ba2d2016-04-02 14:12:34 -0700258 mOccupied = new GridOccupancy(mCountX, mCountY);
259 mTmpOccupied = new GridOccupancy(mCountX, mCountY);
260
Samuel Fufa1e2d0042019-11-18 17:12:46 -0800261 mFolderLeaveBehind.mDelegateCellX = -1;
262 mFolderLeaveBehind.mDelegateCellY = -1;
Adam Cohenefca0272016-02-24 19:19:06 -0800263
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800264 setAlwaysDrawnWithCacheEnabled(false);
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700265
Adam Cohenf7ca3b42021-02-22 11:03:58 -0800266 Resources res = getResources();
267
268 mBackground = getContext().getDrawable(R.drawable.bg_celllayout);
Sunny Goyal2805e632015-05-20 15:35:32 -0700269 mBackground.setCallback(this);
Sunny Goyalaeb16432017-10-16 11:46:41 -0700270 mBackground.setAlpha(0);
Michael Jurka33945b22010-12-21 18:19:38 -0800271
Yogisha Dixitc0ac1dd2021-05-29 00:26:25 +0100272 mGridColor = Themes.getAttrColor(getContext(), R.attr.workspaceAccentColor);
Adam Cohenf7ca3b42021-02-22 11:03:58 -0800273 mGridVisualizationRoundingRadius =
274 res.getDimensionPixelSize(R.dimen.grid_visualization_rounding_radius);
Steven Ngcc505b82021-03-18 23:04:35 +0000275 mReorderPreviewAnimationMagnitude = (REORDER_PREVIEW_MAGNITUDE * deviceProfile.iconSizePx);
Adam Cohen19f37922012-03-21 11:59:11 -0700276
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700277 // Initialize the data structures used for the drag visualization.
Kateryna Ivanova71203732023-05-24 15:09:00 +0000278 mEaseOutInterpolator = Interpolators.DECELERATE_QUINT; // Quint ease out
Winson Chungb8c69f32011-10-19 21:36:08 -0700279 mDragCell[0] = mDragCell[1] = -1;
Adam Cohenf7ca3b42021-02-22 11:03:58 -0800280 mDragCellSpan[0] = mDragCellSpan[1] = -1;
Joe Onorato4be866d2010-10-10 11:26:02 -0700281 for (int i = 0; i < mDragOutlines.length; i++) {
Sunny Goyal669b71f2023-01-27 14:37:07 -0800282 mDragOutlines[i] = new CellLayoutLayoutParams(0, 0, 0, 0);
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700283 }
Mario Bertschler54ba6012017-06-08 10:53:53 -0700284 mDragOutlinePaint.setColor(Themes.getAttrColor(context, R.attr.workspaceTextColor));
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700285
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 Haase472b2812010-10-14 07:02:04 -0700291 final float fromAlphaValue = 0;
292 final float toAlphaValue = (float)res.getInteger(R.integer.config_dragOutlineMaxAlpha);
Joe Onorato4be866d2010-10-10 11:26:02 -0700293
Patrick Dubroy8e58e912010-10-14 13:21:48 -0700294 Arrays.fill(mDragOutlineAlphas, fromAlphaValue);
Joe Onorato4be866d2010-10-10 11:26:02 -0700295
296 for (int i = 0; i < mDragOutlineAnims.length; i++) {
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700297 final InterruptibleInOutAnimator anim =
Sebastian Francof153d912022-04-22 16:15:27 -0500298 new InterruptibleInOutAnimator(duration, fromAlphaValue, toAlphaValue);
Patrick Dubroyce34a972010-10-19 10:34:32 -0700299 anim.getAnimator().setInterpolator(mEaseOutInterpolator);
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700300 final int thisIndex = i;
Chet Haase472b2812010-10-14 07:02:04 -0700301 anim.getAnimator().addUpdateListener(new AnimatorUpdateListener() {
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700302 public void onAnimationUpdate(ValueAnimator animation) {
Joe Onorato4be866d2010-10-10 11:26:02 -0700303 // 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 Cohenf7ca3b42021-02-22 11:03:58 -0800305 mDragOutlineAlphas[thisIndex] = (Float) animation.getAnimatedValue();
306 CellLayout.this.invalidate();
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700307 }
308 });
Joe Onorato4be866d2010-10-10 11:26:02 -0700309 // 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 Onorato4be866d2010-10-10 11:26:02 -0700311 mDragOutlineAnims[i] = anim;
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700312 }
Patrick Dubroyce34a972010-10-19 10:34:32 -0700313
Sunny Goyalc13403c2016-11-18 23:44:48 -0800314 mShortcutsAndWidgets = new ShortcutAndWidgetContainer(context, mContainerType);
Jon Miranda228877d2021-02-09 11:05:00 -0500315 mShortcutsAndWidgets.setCellDimensions(mCellWidth, mCellHeight, mCountX, mCountY,
Thales Lima78d00ad2021-09-30 11:29:06 +0100316 mBorderSpace);
Michael Jurkaa52570f2012-03-20 03:18:20 -0700317 addView(mShortcutsAndWidgets);
Michael Jurka18014792010-10-14 09:01:34 -0700318 }
319
Sebastian Franco2986e0b2024-01-25 10:23:39 -0800320 public CellLayoutContainer getCellLayoutContainer() {
321 return mCellLayoutContainer;
322 }
323
324 public void setCellLayoutContainer(CellLayoutContainer cellLayoutContainer) {
325 mCellLayoutContainer = cellLayoutContainer;
326 }
327
Sunny Goyal9b180102020-03-11 10:02:29 -0700328 /**
329 * Sets or clears a delegate used for accessible drag and drop
330 */
331 public void setDragAndDropAccessibilityDelegate(DragAndDropAccessibilityDelegate delegate) {
332 setOnClickListener(delegate);
Sunny Goyal9b180102020-03-11 10:02:29 -0700333 ViewCompat.setAccessibilityDelegate(this, delegate);
334
Adam Cohen6e7c37a2020-06-25 19:22:37 -0700335 mTouchHelper = delegate;
336 int accessibilityFlag = mTouchHelper != null
Sunny Goyal9b180102020-03-11 10:02:29 -0700337 ? IMPORTANT_FOR_ACCESSIBILITY_YES : IMPORTANT_FOR_ACCESSIBILITY_NO;
338 setImportantForAccessibility(accessibilityFlag);
339 getShortcutsAndWidgets().setImportantForAccessibility(accessibilityFlag);
Adam Cohenc9735cf2015-01-23 16:11:55 -0800340
Sunny Goyal384b5782021-02-09 22:50:02 -0800341 // ExploreByTouchHelper sets focusability. Clear it when the delegate is cleared.
342 setFocusable(delegate != null);
Adam Cohenc9735cf2015-01-23 16:11:55 -0800343 // Invalidate the accessibility hierarchy
344 if (getParent() != null) {
345 getParent().notifySubtreeAccessibilityStateChanged(
346 this, this, AccessibilityEvent.CONTENT_CHANGE_TYPE_SUBTREE);
347 }
348 }
349
Sunny Goyala4647b62021-02-02 13:45:34 -0800350 /**
351 * Returns the currently set accessibility delegate
352 */
353 public DragAndDropAccessibilityDelegate getDragAndDropAccessibilityDelegate() {
354 return mTouchHelper;
355 }
356
Adam Cohenc9735cf2015-01-23 16:11:55 -0800357 @Override
Adam Cohen6e7c37a2020-06-25 19:22:37 -0700358 public boolean dispatchHoverEvent(MotionEvent event) {
359 // Always attempt to dispatch hover events to accessibility first.
360 if (mTouchHelper != null && mTouchHelper.dispatchHoverEvent(event)) {
361 return true;
362 }
363 return super.dispatchHoverEvent(event);
364 }
365
366 @Override
Adam Cohenc9735cf2015-01-23 16:11:55 -0800367 public boolean onInterceptTouchEvent(MotionEvent ev) {
Winson Chungf9935182020-10-23 09:26:44 -0700368 return mTouchHelper != null
369 || (mInterceptTouchListener != null && mInterceptTouchListener.onTouch(this, ev));
Adam Cohenc9735cf2015-01-23 16:11:55 -0800370 }
371
Chris Craik01f2d7f2013-10-01 14:41:56 -0700372 public void enableHardwareLayer(boolean hasLayer) {
373 mShortcutsAndWidgets.setLayerType(hasLayer ? LAYER_TYPE_HARDWARE : LAYER_TYPE_NONE, sPaint);
Michael Jurkad51f33a2012-06-28 15:35:26 -0700374 }
375
vadimt04f356f2019-02-14 18:46:36 -0800376 public boolean isHardwareLayerEnabled() {
377 return mShortcutsAndWidgets.getLayerType() == LAYER_TYPE_HARDWARE;
378 }
379
Thales Lima8cd020b2022-03-15 20:15:14 +0000380 /**
381 * Change sizes of cells
382 *
383 * @param width the new width of the cells
384 * @param height the new height of the cells
385 */
Winson Chung5f8afe62013-08-12 16:19:28 -0700386 public void setCellDimensions(int width, int height) {
Winson Chung11a1a532013-09-13 11:14:45 -0700387 mFixedCellWidth = mCellWidth = width;
388 mFixedCellHeight = mCellHeight = height;
Jon Miranda228877d2021-02-09 11:05:00 -0500389 mShortcutsAndWidgets.setCellDimensions(mCellWidth, mCellHeight, mCountX, mCountY,
Thales Lima78d00ad2021-09-30 11:29:06 +0100390 mBorderSpace);
Winson Chung5f8afe62013-08-12 16:19:28 -0700391 }
392
Thales Lima8cd020b2022-03-15 20:15:14 +0000393 private void resetCellSizeInternal(DeviceProfile deviceProfile) {
394 switch (mContainerType) {
395 case FOLDER:
Jordan Silva637f4eb2023-06-13 11:21:53 +0100396 mBorderSpace = new Point(deviceProfile.folderCellLayoutBorderSpacePx);
Thales Lima8cd020b2022-03-15 20:15:14 +0000397 break;
398 case HOTSEAT:
399 mBorderSpace = new Point(deviceProfile.hotseatBorderSpace,
400 deviceProfile.hotseatBorderSpace);
401 break;
402 case WORKSPACE:
403 default:
404 mBorderSpace = new Point(deviceProfile.cellLayoutBorderSpacePx);
405 break;
406 }
407
408 mCellWidth = mCellHeight = -1;
409 mFixedCellWidth = mFixedCellHeight = -1;
410 }
411
412 /**
413 * Reset the cell sizes and border space
414 */
415 public void resetCellSize(DeviceProfile deviceProfile) {
416 resetCellSizeInternal(deviceProfile);
417 requestLayout();
418 }
419
Adam Cohen2801caf2011-05-13 20:57:39 -0700420 public void setGridSize(int x, int y) {
421 mCountX = x;
422 mCountY = y;
Sunny Goyalff4ba2d2016-04-02 14:12:34 -0700423 mOccupied = new GridOccupancy(mCountX, mCountY);
424 mTmpOccupied = new GridOccupancy(mCountX, mCountY);
Jon Miranda228877d2021-02-09 11:05:00 -0500425 mShortcutsAndWidgets.setCellDimensions(mCellWidth, mCellHeight, mCountX, mCountY,
Thales Lima78d00ad2021-09-30 11:29:06 +0100426 mBorderSpace);
Adam Cohen76fc0852011-06-17 13:26:23 -0700427 requestLayout();
Adam Cohen2801caf2011-05-13 20:57:39 -0700428 }
429
Adam Cohen2374abf2013-04-16 14:56:57 -0700430 // Set whether or not to invert the layout horizontally if the layout is in RTL mode.
431 public void setInvertIfRtl(boolean invert) {
432 mShortcutsAndWidgets.setInvertIfRtl(invert);
433 }
434
Adam Cohen917e3882013-10-31 15:03:35 -0700435 public void setDropPending(boolean pending) {
436 mDropPending = pending;
437 }
438
439 public boolean isDropPending() {
440 return mDropPending;
441 }
442
Adam Cohenc50438c2014-08-19 17:43:05 -0700443 void setIsDragOverlapping(boolean isDragOverlapping) {
444 if (mIsDragOverlapping != isDragOverlapping) {
445 mIsDragOverlapping = isDragOverlapping;
Sunny Goyalf5440cb2016-12-14 15:13:00 -0800446 mBackground.setState(mIsDragOverlapping
447 ? BACKGROUND_STATE_ACTIVE : BACKGROUND_STATE_DEFAULT);
Adam Cohenc50438c2014-08-19 17:43:05 -0700448 invalidate();
449 }
450 }
451
Sunny Goyale2fd14b2015-08-27 17:45:46 -0700452 @Override
453 protected void dispatchSaveInstanceState(SparseArray<Parcelable> container) {
Sunny Goyal7ce471b2017-08-02 03:37:39 -0700454 ParcelableSparseArray jail = getJailedArray(container);
455 super.dispatchSaveInstanceState(jail);
456 container.put(R.id.cell_layout_jail_id, jail);
Sunny Goyale2fd14b2015-08-27 17:45:46 -0700457 }
458
459 @Override
460 protected void dispatchRestoreInstanceState(SparseArray<Parcelable> container) {
Sunny Goyal7ce471b2017-08-02 03:37:39 -0700461 super.dispatchRestoreInstanceState(getJailedArray(container));
Sunny Goyale2fd14b2015-08-27 17:45:46 -0700462 }
463
Sunny Goyal7ce471b2017-08-02 03:37:39 -0700464 /**
465 * Wrap the SparseArray in another Parcelable so that the item ids do not conflict with our
466 * our internal resource ids
467 */
Sunny Goyale2fd14b2015-08-27 17:45:46 -0700468 private ParcelableSparseArray getJailedArray(SparseArray<Parcelable> container) {
469 final Parcelable parcelable = container.get(R.id.cell_layout_jail_id);
470 return parcelable instanceof ParcelableSparseArray ?
471 (ParcelableSparseArray) parcelable : new ParcelableSparseArray();
472 }
473
Tony Wickham0f97b782015-12-02 17:55:07 -0800474 public boolean getIsDragOverlapping() {
475 return mIsDragOverlapping;
476 }
477
Jeff Sharkey83f111d2009-04-20 21:03:13 -0700478 @Override
Patrick Dubroy1262e362010-10-06 15:49:50 -0700479 protected void onDraw(Canvas canvas) {
Michael Jurka3e7c7632010-10-02 16:01:03 -0700480 // When we're large, we are either drawn in a "hover" state (ie when dragging an item to
481 // a neighboring page) or with just a normal background (if backgroundAlpha > 0.0f)
482 // When we're small, we are either drawn normally or in the "accepts drops" state (during
483 // a drag). However, we also drag the mini hover background *over* one of those two
484 // backgrounds
Sunny Goyalaeb16432017-10-16 11:46:41 -0700485 if (mBackground.getAlpha() > 0) {
Sunny Goyal2805e632015-05-20 15:35:32 -0700486 mBackground.draw(canvas);
Michael Jurkaa63c4522010-08-19 13:52:27 -0700487 }
Romain Guya6abce82009-11-10 02:54:41 -0800488
Adam Cohen482ed822012-03-02 14:15:13 -0800489 if (DEBUG_VISUALIZE_OCCUPIED) {
Tony Wickham0ac045f2021-11-03 13:17:02 -0700490 Rect cellBounds = new Rect();
491 // Will contain the bounds of the cell including spacing between cells.
492 Rect cellBoundsWithSpacing = new Rect();
Tony Wickham12784902021-11-03 14:02:10 -0700493 int[] targetCell = new int[2];
Tony Wickham0ac045f2021-11-03 13:17:02 -0700494 int[] cellCenter = new int[2];
495 Paint debugPaint = new Paint();
496 debugPaint.setStrokeWidth(Utilities.dpToPx(1));
497 for (int x = 0; x < mCountX; x++) {
498 for (int y = 0; y < mCountY; y++) {
499 if (!mOccupied.cells[x][y]) {
500 continue;
Adam Cohen482ed822012-03-02 14:15:13 -0800501 }
Tony Wickham12784902021-11-03 14:02:10 -0700502 targetCell[0] = x;
503 targetCell[1] = y;
Tony Wickham0ac045f2021-11-03 13:17:02 -0700504
Tony Wickham12784902021-11-03 14:02:10 -0700505 boolean canCreateFolder = canCreateFolder(getChildAt(x, y));
Tony Wickham0ac045f2021-11-03 13:17:02 -0700506 cellToRect(x, y, 1, 1, cellBounds);
507 cellBoundsWithSpacing.set(cellBounds);
508 cellBoundsWithSpacing.inset(-mBorderSpace.x / 2, -mBorderSpace.y / 2);
Tony Wickham3cfa5ed2021-11-03 13:20:43 -0700509 getWorkspaceCellVisualCenter(x, y, cellCenter);
Tony Wickham0ac045f2021-11-03 13:17:02 -0700510
511 canvas.save();
512 canvas.clipRect(cellBoundsWithSpacing);
513
514 // Draw reorder drag target.
515 debugPaint.setColor(Color.RED);
Sebastian Franco6e1024e2022-07-29 13:46:49 -0700516 canvas.drawCircle(cellCenter[0], cellCenter[1],
517 getReorderRadius(targetCell, 1, 1), debugPaint);
Tony Wickham0ac045f2021-11-03 13:17:02 -0700518
519 // Draw folder creation drag target.
520 if (canCreateFolder) {
521 debugPaint.setColor(Color.GREEN);
522 canvas.drawCircle(cellCenter[0], cellCenter[1],
Tony Wickham12784902021-11-03 14:02:10 -0700523 getFolderCreationRadius(targetCell), debugPaint);
Tony Wickham0ac045f2021-11-03 13:17:02 -0700524 }
525
526 canvas.restore();
Adam Cohen482ed822012-03-02 14:15:13 -0800527 }
528 }
529 }
530
Samuel Fufa1e2d0042019-11-18 17:12:46 -0800531 for (int i = 0; i < mDelegatedCellDrawings.size(); i++) {
532 DelegatedCellDrawing cellDrawing = mDelegatedCellDrawings.get(i);
533 cellToPoint(cellDrawing.mDelegateCellX, cellDrawing.mDelegateCellY, mTempLocation);
Adam Cohenefca0272016-02-24 19:19:06 -0800534 canvas.save();
535 canvas.translate(mTempLocation[0], mTempLocation[1]);
Samuel Fufa1e2d0042019-11-18 17:12:46 -0800536 cellDrawing.drawUnderItem(canvas);
Adam Cohenefca0272016-02-24 19:19:06 -0800537 canvas.restore();
Adam Cohen69ce2e52011-07-03 19:25:21 -0700538 }
Adam Cohenc51934b2011-07-26 21:07:43 -0700539
Samuel Fufa1e2d0042019-11-18 17:12:46 -0800540 if (mFolderLeaveBehind.mDelegateCellX >= 0 && mFolderLeaveBehind.mDelegateCellY >= 0) {
541 cellToPoint(mFolderLeaveBehind.mDelegateCellX,
542 mFolderLeaveBehind.mDelegateCellY, mTempLocation);
Adam Cohenefca0272016-02-24 19:19:06 -0800543 canvas.save();
544 canvas.translate(mTempLocation[0], mTempLocation[1]);
Tony Wickhamec6fd6f2023-03-11 02:08:57 +0000545 mFolderLeaveBehind.drawLeaveBehind(canvas, FOLDER_LEAVE_BEHIND_COLOR);
Adam Cohenefca0272016-02-24 19:19:06 -0800546 canvas.restore();
Adam Cohenc51934b2011-07-26 21:07:43 -0700547 }
Adam Cohen65086992020-02-19 08:40:49 -0800548
Adam Cohenf7ca3b42021-02-22 11:03:58 -0800549 if (mVisualizeCells || mVisualizeDropLocation) {
Adam Cohen65086992020-02-19 08:40:49 -0800550 visualizeGrid(canvas);
551 }
552 }
553
Adam Cohenf7ca3b42021-02-22 11:03:58 -0800554 /**
Tony Wickham12784902021-11-03 14:02:10 -0700555 * Returns whether dropping an icon on the given View can create (or add to) a folder.
556 */
557 private boolean canCreateFolder(View child) {
558 return child instanceof DraggableView
559 && ((DraggableView) child).getViewType() == DRAGGABLE_ICON;
560 }
561
562 /**
Adam Cohenf7ca3b42021-02-22 11:03:58 -0800563 * Indicates the progress of the Workspace entering the SpringLoaded state; allows the
564 * CellLayout to update various visuals for this state.
565 *
566 * @param progress
567 */
568 public void setSpringLoadedProgress(float progress) {
569 if (Float.compare(progress, mSpringLoadedProgress) != 0) {
570 mSpringLoadedProgress = progress;
fbarone74256b2023-04-10 14:50:31 -0700571 updateBgAlpha();
Adam Cohenf7ca3b42021-02-22 11:03:58 -0800572 setGridAlpha(progress);
573 }
574 }
575
576 /**
577 * See setSpringLoadedProgress
578 * @return progress
579 */
580 public float getSpringLoadedProgress() {
581 return mSpringLoadedProgress;
582 }
583
Sebastian Franco09589322022-11-02 15:25:58 -0700584 protected void updateBgAlpha() {
Sebastian Franco2986e0b2024-01-25 10:23:39 -0800585 mBackground.setAlpha((int) (mSpringLoadedProgress * 255));
Adam Cohenf7ca3b42021-02-22 11:03:58 -0800586 }
587
588 /**
589 * Set the progress of this page's scroll
590 *
591 * @param progress 0 if the screen is centered, +/-1 if it is to the right / left respectively
592 */
593 public void setScrollProgress(float progress) {
594 if (Float.compare(Math.abs(progress), mScrollProgress) != 0) {
595 mScrollProgress = Math.abs(progress);
fbarone74256b2023-04-10 14:50:31 -0700596 updateBgAlpha();
Adam Cohenf7ca3b42021-02-22 11:03:58 -0800597 }
598 }
599
600 private void setGridAlpha(float gridAlpha) {
601 if (Float.compare(gridAlpha, mGridAlpha) != 0) {
602 mGridAlpha = gridAlpha;
603 invalidate();
604 }
605 }
606
Adam Cohen65086992020-02-19 08:40:49 -0800607 protected void visualizeGrid(Canvas canvas) {
Adam Cohen0c4d2782021-04-29 15:56:13 -0700608 DeviceProfile dp = mActivity.getDeviceProfile();
Alex Chau51da2192022-05-20 13:32:10 +0100609 int paddingX = Math.min((mCellWidth - dp.iconSizePx) / 2, dp.gridVisualizationPaddingX);
610 int paddingY = Math.min((mCellHeight - dp.iconSizePx) / 2, dp.gridVisualizationPaddingY);
Adam Cohen65086992020-02-19 08:40:49 -0800611
Adam Cohenf7ca3b42021-02-22 11:03:58 -0800612 mVisualizeGridPaint.setStrokeWidth(8);
Adam Cohen65086992020-02-19 08:40:49 -0800613
Sebastian Franco0dd5db82023-10-13 11:09:21 -0700614 // This is used for debugging purposes only
Adam Cohenf7ca3b42021-02-22 11:03:58 -0800615 if (mVisualizeCells) {
Sebastian Franco0dd5db82023-10-13 11:09:21 -0700616 int paintAlpha = (int) (120 * mGridAlpha);
617 mVisualizeGridPaint.setColor(ColorUtils.setAlphaComponent(mGridColor, paintAlpha));
Adam Cohenf7ca3b42021-02-22 11:03:58 -0800618 for (int i = 0; i < mCountX; i++) {
619 for (int j = 0; j < mCountY; j++) {
Sebastian Franco0dd5db82023-10-13 11:09:21 -0700620 cellToRect(i, j, 1, 1, mTempOnDrawCellToRect);
621 mVisualizeGridRect.set(mTempOnDrawCellToRect);
622 mVisualizeGridRect.inset(paddingX, paddingY);
Adam Cohenf7ca3b42021-02-22 11:03:58 -0800623 mVisualizeGridPaint.setStyle(Paint.Style.FILL);
624 canvas.drawRoundRect(mVisualizeGridRect, mGridVisualizationRoundingRadius,
625 mGridVisualizationRoundingRadius, mVisualizeGridPaint);
626 }
627 }
628 }
Adam Cohen65086992020-02-19 08:40:49 -0800629
fbarone74256b2023-04-10 14:50:31 -0700630 if (mVisualizeDropLocation) {
Adam Cohenf7ca3b42021-02-22 11:03:58 -0800631 for (int i = 0; i < mDragOutlines.length; i++) {
632 final float alpha = mDragOutlineAlphas[i];
633 if (alpha <= 0) continue;
Sebastian Franco0dd5db82023-10-13 11:09:21 -0700634 CellLayoutLayoutParams params = mDragOutlines[i];
635 cellToRect(params.getCellX(), params.getCellY(), params.cellHSpan, params.cellVSpan,
636 mTempOnDrawCellToRect);
637 mVisualizeGridRect.set(mTempOnDrawCellToRect);
638 mVisualizeGridRect.inset(paddingX, paddingY);
Adam Cohen65086992020-02-19 08:40:49 -0800639
Adam Cohenf7ca3b42021-02-22 11:03:58 -0800640 mVisualizeGridPaint.setAlpha(255);
Adam Cohen65086992020-02-19 08:40:49 -0800641 mVisualizeGridPaint.setStyle(Paint.Style.STROKE);
Adam Cohenf7ca3b42021-02-22 11:03:58 -0800642 mVisualizeGridPaint.setColor(Color.argb((int) (alpha),
643 Color.red(mGridColor), Color.green(mGridColor), Color.blue(mGridColor)));
Adam Cohen65086992020-02-19 08:40:49 -0800644
Sebastian Franco9ea36d42023-09-21 13:56:42 -0700645 canvas.save();
646 canvas.translate(getMarginForGivenCellParams(params), 0);
Adam Cohenf7ca3b42021-02-22 11:03:58 -0800647 canvas.drawRoundRect(mVisualizeGridRect, mGridVisualizationRoundingRadius,
648 mGridVisualizationRoundingRadius, mVisualizeGridPaint);
Sebastian Franco9ea36d42023-09-21 13:56:42 -0700649 canvas.restore();
Adam Cohen65086992020-02-19 08:40:49 -0800650 }
651 }
Adam Cohen69ce2e52011-07-03 19:25:21 -0700652 }
653
Sebastian Franco9ea36d42023-09-21 13:56:42 -0700654 protected float getMarginForGivenCellParams(CellLayoutLayoutParams params) {
655 return 0;
656 }
657
Adam Cohenefca0272016-02-24 19:19:06 -0800658 @Override
659 protected void dispatchDraw(Canvas canvas) {
660 super.dispatchDraw(canvas);
661
Samuel Fufa1e2d0042019-11-18 17:12:46 -0800662 for (int i = 0; i < mDelegatedCellDrawings.size(); i++) {
663 DelegatedCellDrawing bg = mDelegatedCellDrawings.get(i);
664 cellToPoint(bg.mDelegateCellX, bg.mDelegateCellY, mTempLocation);
665 canvas.save();
666 canvas.translate(mTempLocation[0], mTempLocation[1]);
667 bg.drawOverItem(canvas);
668 canvas.restore();
Adam Cohenefca0272016-02-24 19:19:06 -0800669 }
Adam Cohen69ce2e52011-07-03 19:25:21 -0700670 }
671
Samuel Fufa1e2d0042019-11-18 17:12:46 -0800672 /**
673 * Add Delegated cell drawing
674 */
675 public void addDelegatedCellDrawing(DelegatedCellDrawing bg) {
676 mDelegatedCellDrawings.add(bg);
Adam Cohenefca0272016-02-24 19:19:06 -0800677 }
Samuel Fufa1e2d0042019-11-18 17:12:46 -0800678
679 /**
680 * Remove item from DelegatedCellDrawings
681 */
682 public void removeDelegatedCellDrawing(DelegatedCellDrawing bg) {
683 mDelegatedCellDrawings.remove(bg);
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700684 }
685
Adam Cohenc51934b2011-07-26 21:07:43 -0700686 public void setFolderLeaveBehindCell(int x, int y) {
Adam Cohenefca0272016-02-24 19:19:06 -0800687 View child = getChildAt(x, y);
Sunny Goyalab770a12018-11-14 15:17:26 -0800688 mFolderLeaveBehind.setup(getContext(), mActivity, null,
Adam Cohenefca0272016-02-24 19:19:06 -0800689 child.getMeasuredWidth(), child.getPaddingTop());
690
Samuel Fufa1e2d0042019-11-18 17:12:46 -0800691 mFolderLeaveBehind.mDelegateCellX = x;
692 mFolderLeaveBehind.mDelegateCellY = y;
Adam Cohenc51934b2011-07-26 21:07:43 -0700693 invalidate();
694 }
695
696 public void clearFolderLeaveBehind() {
Samuel Fufa1e2d0042019-11-18 17:12:46 -0800697 mFolderLeaveBehind.mDelegateCellX = -1;
698 mFolderLeaveBehind.mDelegateCellY = -1;
Adam Cohenc51934b2011-07-26 21:07:43 -0700699 invalidate();
700 }
701
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700702 @Override
Michael Jurkae6235dd2011-10-04 15:02:05 -0700703 public boolean shouldDelayChildPressedState() {
704 return false;
705 }
706
Adam Cohen1462de32012-07-24 22:34:36 -0700707 public void restoreInstanceState(SparseArray<Parcelable> states) {
Sunny Goyal33a152f2014-07-22 12:13:14 -0700708 try {
709 dispatchRestoreInstanceState(states);
710 } catch (IllegalArgumentException ex) {
Zak Cohen3eeb41d2020-02-14 14:15:13 -0800711 if (FeatureFlags.IS_STUDIO_BUILD) {
Sunny Goyal33a152f2014-07-22 12:13:14 -0700712 throw ex;
713 }
714 // Mismatched viewId / viewType preventing restore. Skip restore on production builds.
715 Log.e(TAG, "Ignoring an error while restoring a view instance state", ex);
716 }
Adam Cohen1462de32012-07-24 22:34:36 -0700717 }
718
Michael Jurkae6235dd2011-10-04 15:02:05 -0700719 @Override
Jeff Sharkey83f111d2009-04-20 21:03:13 -0700720 public void cancelLongPress() {
721 super.cancelLongPress();
722
723 // Cancel long press for all children
724 final int count = getChildCount();
725 for (int i = 0; i < count; i++) {
726 final View child = getChildAt(i);
727 child.cancelLongPress();
728 }
729 }
730
Michael Jurkadee05892010-07-27 10:01:56 -0700731 public void setOnInterceptTouchListener(View.OnTouchListener listener) {
732 mInterceptTouchListener = listener;
733 }
734
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800735 public int getCountX() {
Adam Cohend22015c2010-07-26 22:02:18 -0700736 return mCountX;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800737 }
738
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800739 public int getCountY() {
Adam Cohend22015c2010-07-26 22:02:18 -0700740 return mCountY;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800741 }
742
Sunny Goyalc13403c2016-11-18 23:44:48 -0800743 public boolean acceptsWidget() {
744 return mContainerType == WORKSPACE;
Sunny Goyale9b651e2015-04-24 11:44:51 -0700745 }
746
Sebastian Francod4682992022-10-05 13:03:09 -0500747 /**
748 * Adds the given view to the CellLayout
749 *
750 * @param child view to add.
751 * @param index index of the CellLayout children where to add the view.
752 * @param childId id of the view.
753 * @param params represent the logic of the view on the CellLayout.
754 * @param markCells if the occupied cells should be marked or not
755 * @return if adding the view was successful
756 */
757 public boolean addViewToCellLayout(View child, int index, int childId,
758 CellLayoutLayoutParams params, boolean markCells) {
759 final CellLayoutLayoutParams lp = params;
Winson Chungaafa03c2010-06-11 17:34:16 -0700760
Andrew Flynnde38e422012-05-08 11:22:15 -0700761 // Hotseat icons - remove text
Andrew Flynn0dca1ec2012-02-29 13:33:22 -0800762 if (child instanceof BubbleTextView) {
763 BubbleTextView bubbleChild = (BubbleTextView) child;
Jon Mirandaf1eae802017-10-04 11:23:33 -0700764 bubbleChild.setTextVisibility(mContainerType != HOTSEAT);
Andrew Flynn0dca1ec2012-02-29 13:33:22 -0800765 }
766
Sebastian Francof7654252024-01-18 11:50:50 -0800767 child.setScaleX(DEFAULT_SCALE);
768 child.setScaleY(DEFAULT_SCALE);
Adam Cohen307fe232012-08-16 17:55:58 -0700769
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800770 // Generate an id for each view, this assumes we have at most 256x256 cells
771 // per workspace screen
Sebastian Franco877088e2023-01-03 15:16:22 -0700772 if (lp.getCellX() >= 0 && lp.getCellX() <= mCountX - 1
773 && lp.getCellY() >= 0 && lp.getCellY() <= mCountY - 1) {
Winson Chungaafa03c2010-06-11 17:34:16 -0700774 // If the horizontal or vertical span is set to -1, it is taken to
775 // mean that it spans the extent of the CellLayout
Adam Cohend22015c2010-07-26 22:02:18 -0700776 if (lp.cellHSpan < 0) lp.cellHSpan = mCountX;
777 if (lp.cellVSpan < 0) lp.cellVSpan = mCountY;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800778
Winson Chungaafa03c2010-06-11 17:34:16 -0700779 child.setId(childId);
Tony Wickhama0628cc2015-10-14 15:23:04 -0700780 if (LOGD) {
781 Log.d(TAG, "Adding view to ShortcutsAndWidgetsContainer: " + child);
782 }
Sunny Goyalf7a29e82015-04-24 15:20:43 -0700783 mShortcutsAndWidgets.addView(child, index, lp);
Michael Jurkadee05892010-07-27 10:01:56 -0700784
Michael Jurkaf3ca3ab2010-10-20 17:08:24 -0700785 if (markCells) markCellsAsOccupiedForView(child);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700786
Winson Chungaafa03c2010-06-11 17:34:16 -0700787 return true;
788 }
789 return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800790 }
Michael Jurka3e7c7632010-10-02 16:01:03 -0700791
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800792 @Override
Michael Jurka0280c3b2010-09-17 15:00:07 -0700793 public void removeAllViews() {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -0700794 mOccupied.clear();
Michael Jurkaa52570f2012-03-20 03:18:20 -0700795 mShortcutsAndWidgets.removeAllViews();
Michael Jurka0280c3b2010-09-17 15:00:07 -0700796 }
797
798 @Override
799 public void removeAllViewsInLayout() {
Michael Jurkaa52570f2012-03-20 03:18:20 -0700800 if (mShortcutsAndWidgets.getChildCount() > 0) {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -0700801 mOccupied.clear();
Michael Jurkaa52570f2012-03-20 03:18:20 -0700802 mShortcutsAndWidgets.removeAllViewsInLayout();
Michael Jurka7cfc2822011-08-02 20:19:24 -0700803 }
Michael Jurka0280c3b2010-09-17 15:00:07 -0700804 }
805
806 @Override
807 public void removeView(View view) {
808 markCellsAsUnoccupiedForView(view);
Michael Jurkaa52570f2012-03-20 03:18:20 -0700809 mShortcutsAndWidgets.removeView(view);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700810 }
811
812 @Override
813 public void removeViewAt(int index) {
Michael Jurkaa52570f2012-03-20 03:18:20 -0700814 markCellsAsUnoccupiedForView(mShortcutsAndWidgets.getChildAt(index));
815 mShortcutsAndWidgets.removeViewAt(index);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700816 }
817
818 @Override
819 public void removeViewInLayout(View view) {
820 markCellsAsUnoccupiedForView(view);
Michael Jurkaa52570f2012-03-20 03:18:20 -0700821 mShortcutsAndWidgets.removeViewInLayout(view);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700822 }
823
824 @Override
825 public void removeViews(int start, int count) {
826 for (int i = start; i < start + count; i++) {
Michael Jurkaa52570f2012-03-20 03:18:20 -0700827 markCellsAsUnoccupiedForView(mShortcutsAndWidgets.getChildAt(i));
Michael Jurka0280c3b2010-09-17 15:00:07 -0700828 }
Michael Jurkaa52570f2012-03-20 03:18:20 -0700829 mShortcutsAndWidgets.removeViews(start, count);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700830 }
831
832 @Override
833 public void removeViewsInLayout(int start, int count) {
834 for (int i = start; i < start + count; i++) {
Michael Jurkaa52570f2012-03-20 03:18:20 -0700835 markCellsAsUnoccupiedForView(mShortcutsAndWidgets.getChildAt(i));
Michael Jurka0280c3b2010-09-17 15:00:07 -0700836 }
Michael Jurkaa52570f2012-03-20 03:18:20 -0700837 mShortcutsAndWidgets.removeViewsInLayout(start, count);
Michael Jurkaabded662011-03-04 12:06:57 -0800838 }
839
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700840 /**
Winson Chungaafa03c2010-06-11 17:34:16 -0700841 * Given a point, return the cell that strictly encloses that point
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800842 * @param x X coordinate of the point
843 * @param y Y coordinate of the point
844 * @param result Array of 2 ints to hold the x and y coordinate of the cell
845 */
Sunny Goyale9b651e2015-04-24 11:44:51 -0700846 public void pointToCellExact(int x, int y, int[] result) {
Winson Chung4b825dcd2011-06-19 12:41:22 -0700847 final int hStartPadding = getPaddingLeft();
848 final int vStartPadding = getPaddingTop();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800849
Sebastian Francob57c0b22022-06-28 13:54:35 -0700850 result[0] = (x - hStartPadding) / (mCellWidth + mBorderSpace.x);
851 result[1] = (y - vStartPadding) / (mCellHeight + mBorderSpace.y);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800852
Adam Cohend22015c2010-07-26 22:02:18 -0700853 final int xAxis = mCountX;
854 final int yAxis = mCountY;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800855
856 if (result[0] < 0) result[0] = 0;
857 if (result[0] >= xAxis) result[0] = xAxis - 1;
858 if (result[1] < 0) result[1] = 0;
859 if (result[1] >= yAxis) result[1] = yAxis - 1;
860 }
Winson Chungaafa03c2010-06-11 17:34:16 -0700861
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800862 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800863 * Given a cell coordinate, return the point that represents the upper left corner of that cell
Winson Chungaafa03c2010-06-11 17:34:16 -0700864 *
865 * @param cellX X coordinate of the cell
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800866 * @param cellY Y coordinate of the cell
Winson Chungaafa03c2010-06-11 17:34:16 -0700867 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800868 * @param result Array of 2 ints to hold the x and y coordinate of the point
869 */
870 void cellToPoint(int cellX, int cellY, int[] result) {
Jon Miranda228877d2021-02-09 11:05:00 -0500871 cellToRect(cellX, cellY, 1, 1, mTempRect);
872 result[0] = mTempRect.left;
873 result[1] = mTempRect.top;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800874 }
875
Adam Cohene3e27a82011-04-15 12:07:39 -0700876 /**
Adam Cohen482ed822012-03-02 14:15:13 -0800877 * Given a cell coordinate, return the point that represents the center of the cell
Adam Cohene3e27a82011-04-15 12:07:39 -0700878 *
879 * @param cellX X coordinate of the cell
880 * @param cellY Y coordinate of the cell
881 *
882 * @param result Array of 2 ints to hold the x and y coordinate of the point
883 */
884 void cellToCenterPoint(int cellX, int cellY, int[] result) {
Adam Cohen47a876d2012-03-19 13:21:41 -0700885 regionToCenterPoint(cellX, cellY, 1, 1, result);
886 }
887
888 /**
Tony Wickham0ac045f2021-11-03 13:17:02 -0700889 * Given a cell coordinate and span return the point that represents the center of the region
Adam Cohen47a876d2012-03-19 13:21:41 -0700890 *
891 * @param cellX X coordinate of the cell
892 * @param cellY Y coordinate of the cell
893 *
894 * @param result Array of 2 ints to hold the x and y coordinate of the point
895 */
Sebastian Franco45b39b52023-01-10 10:47:46 -0600896 public void regionToCenterPoint(int cellX, int cellY, int spanX, int spanY, int[] result) {
Jon Miranda228877d2021-02-09 11:05:00 -0500897 cellToRect(cellX, cellY, spanX, spanY, mTempRect);
898 result[0] = mTempRect.centerX();
899 result[1] = mTempRect.centerY();
Adam Cohen19f37922012-03-21 11:59:11 -0700900 }
901
Tony Wickham3cfa5ed2021-11-03 13:20:43 -0700902 /**
903 * Returns the distance between the given coordinate and the visual center of the given cell.
904 */
905 public float getDistanceFromWorkspaceCellVisualCenter(float x, float y, int[] cell) {
906 getWorkspaceCellVisualCenter(cell[0], cell[1], mTmpPoint);
Sunny Goyalf7a29e82015-04-24 15:20:43 -0700907 return (float) Math.hypot(x - mTmpPoint[0], y - mTmpPoint[1]);
Adam Cohen482ed822012-03-02 14:15:13 -0800908 }
909
Tony Wickham3cfa5ed2021-11-03 13:20:43 -0700910 private void getWorkspaceCellVisualCenter(int cellX, int cellY, int[] outPoint) {
911 View child = getChildAt(cellX, cellY);
912 if (child instanceof DraggableView) {
913 DraggableView draggableChild = (DraggableView) child;
914 if (draggableChild.getViewType() == DRAGGABLE_ICON) {
915 cellToPoint(cellX, cellY, outPoint);
916 draggableChild.getWorkspaceVisualDragBounds(mTempRect);
917 mTempRect.offset(outPoint[0], outPoint[1]);
918 outPoint[0] = mTempRect.centerX();
919 outPoint[1] = mTempRect.centerY();
920 return;
921 }
922 }
923 cellToCenterPoint(cellX, cellY, outPoint);
924 }
925
Tony Wickham0ac045f2021-11-03 13:17:02 -0700926 /**
927 * Returns the max distance from the center of a cell that can accept a drop to create a folder.
928 */
Tony Wickham12784902021-11-03 14:02:10 -0700929 public float getFolderCreationRadius(int[] targetCell) {
Tony Wickham0ac045f2021-11-03 13:17:02 -0700930 DeviceProfile grid = mActivity.getDeviceProfile();
Tony Wickham12784902021-11-03 14:02:10 -0700931 float iconVisibleRadius = ICON_VISIBLE_AREA_FACTOR * grid.iconSizePx / 2;
932 // Halfway between reorder radius and icon.
Sebastian Franco6e1024e2022-07-29 13:46:49 -0700933 return (getReorderRadius(targetCell, 1, 1) + iconVisibleRadius) / 2;
Tony Wickham12784902021-11-03 14:02:10 -0700934 }
935
936 /**
937 * Returns the max distance from the center of a cell that will start to reorder on drag over.
938 */
Sebastian Franco6e1024e2022-07-29 13:46:49 -0700939 public float getReorderRadius(int[] targetCell, int spanX, int spanY) {
Tony Wickham12784902021-11-03 14:02:10 -0700940 int[] centerPoint = mTmpPoint;
941 getWorkspaceCellVisualCenter(targetCell[0], targetCell[1], centerPoint);
942
943 Rect cellBoundsWithSpacing = mTempRect;
Sebastian Franco6e1024e2022-07-29 13:46:49 -0700944 cellToRect(targetCell[0], targetCell[1], spanX, spanY, cellBoundsWithSpacing);
Tony Wickham12784902021-11-03 14:02:10 -0700945 cellBoundsWithSpacing.inset(-mBorderSpace.x / 2, -mBorderSpace.y / 2);
946
Sebastian Francoc8392ea2022-10-28 16:38:37 -0700947 if (canCreateFolder(getChildAt(targetCell[0], targetCell[1])) && spanX == 1 && spanY == 1) {
Tony Wickham12784902021-11-03 14:02:10 -0700948 // Take only the circle in the smaller dimension, to ensure we don't start reordering
949 // too soon before accepting a folder drop.
950 int minRadius = centerPoint[0] - cellBoundsWithSpacing.left;
951 minRadius = Math.min(minRadius, centerPoint[1] - cellBoundsWithSpacing.top);
952 minRadius = Math.min(minRadius, cellBoundsWithSpacing.right - centerPoint[0]);
953 minRadius = Math.min(minRadius, cellBoundsWithSpacing.bottom - centerPoint[1]);
954 return minRadius;
955 }
956 // Take up the entire cell, including space between this cell and the adjacent ones.
Sebastian Francoc8392ea2022-10-28 16:38:37 -0700957 // Multiply by span to scale radius
958 return (float) Math.hypot(spanX * cellBoundsWithSpacing.width() / 2f,
959 spanY * cellBoundsWithSpacing.height() / 2f);
Tony Wickham0ac045f2021-11-03 13:17:02 -0700960 }
961
Adam Cohenf9c184a2016-01-15 16:47:43 -0800962 public int getCellWidth() {
Romain Guy84f296c2009-11-04 15:00:44 -0800963 return mCellWidth;
964 }
965
Sunny Goyal0b754e52017-08-07 07:42:45 -0700966 public int getCellHeight() {
Romain Guy84f296c2009-11-04 15:00:44 -0800967 return mCellHeight;
968 }
969
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700970 public void setFixedSize(int width, int height) {
971 mFixedWidth = width;
972 mFixedHeight = height;
973 }
974
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800975 @Override
976 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800977 int widthSpecMode = MeasureSpec.getMode(widthMeasureSpec);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800978 int heightSpecMode = MeasureSpec.getMode(heightMeasureSpec);
Winson Chung5f8afe62013-08-12 16:19:28 -0700979 int widthSize = MeasureSpec.getSize(widthMeasureSpec);
980 int heightSize = MeasureSpec.getSize(heightMeasureSpec);
Winson Chung2d75f122013-09-23 16:53:31 -0700981 int childWidthSize = widthSize - (getPaddingLeft() + getPaddingRight());
982 int childHeightSize = heightSize - (getPaddingTop() + getPaddingBottom());
Sunny Goyalae6e3182019-04-30 12:04:37 -0700983
Winson Chung11a1a532013-09-13 11:14:45 -0700984 if (mFixedCellWidth < 0 || mFixedCellHeight < 0) {
Thales Lima78d00ad2021-09-30 11:29:06 +0100985 int cw = DeviceProfile.calculateCellWidth(childWidthSize, mBorderSpace.x,
Jon Miranda228877d2021-02-09 11:05:00 -0500986 mCountX);
Thales Lima78d00ad2021-09-30 11:29:06 +0100987 int ch = DeviceProfile.calculateCellHeight(childHeightSize, mBorderSpace.y,
Jon Miranda228877d2021-02-09 11:05:00 -0500988 mCountY);
Winson Chung11a1a532013-09-13 11:14:45 -0700989 if (cw != mCellWidth || ch != mCellHeight) {
990 mCellWidth = cw;
991 mCellHeight = ch;
Jon Miranda228877d2021-02-09 11:05:00 -0500992 mShortcutsAndWidgets.setCellDimensions(mCellWidth, mCellHeight, mCountX, mCountY,
Thales Lima78d00ad2021-09-30 11:29:06 +0100993 mBorderSpace);
Winson Chung11a1a532013-09-13 11:14:45 -0700994 }
Winson Chung5f8afe62013-08-12 16:19:28 -0700995 }
Winson Chungaafa03c2010-06-11 17:34:16 -0700996
Winson Chung2d75f122013-09-23 16:53:31 -0700997 int newWidth = childWidthSize;
998 int newHeight = childHeightSize;
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700999 if (mFixedWidth > 0 && mFixedHeight > 0) {
1000 newWidth = mFixedWidth;
1001 newHeight = mFixedHeight;
1002 } else if (widthSpecMode == MeasureSpec.UNSPECIFIED || heightSpecMode == MeasureSpec.UNSPECIFIED) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001003 throw new RuntimeException("CellLayout cannot have UNSPECIFIED dimensions");
1004 }
1005
Sunny Goyal4fe5a372015-05-14 19:55:10 -07001006 mShortcutsAndWidgets.measure(
1007 MeasureSpec.makeMeasureSpec(newWidth, MeasureSpec.EXACTLY),
1008 MeasureSpec.makeMeasureSpec(newHeight, MeasureSpec.EXACTLY));
1009
1010 int maxWidth = mShortcutsAndWidgets.getMeasuredWidth();
1011 int maxHeight = mShortcutsAndWidgets.getMeasuredHeight();
Winson Chung2d75f122013-09-23 16:53:31 -07001012 if (mFixedWidth > 0 && mFixedHeight > 0) {
1013 setMeasuredDimension(maxWidth, maxHeight);
1014 } else {
1015 setMeasuredDimension(widthSize, heightSize);
1016 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001017 }
1018
1019 @Override
Michael Jurka28750fb2010-09-24 17:43:49 -07001020 protected void onLayout(boolean changed, int l, int t, int r, int b) {
Tony Wickham26b01422015-11-10 14:44:32 -08001021 int left = getPaddingLeft();
Sunny Goyal7ce471b2017-08-02 03:37:39 -07001022 left += (int) Math.ceil(getUnusedHorizontalSpace() / 2f);
Sunny Goyal7c786f72016-06-01 14:08:21 -07001023 int right = r - l - getPaddingRight();
Sunny Goyal7ce471b2017-08-02 03:37:39 -07001024 right -= (int) Math.ceil(getUnusedHorizontalSpace() / 2f);
Sunny Goyal7c786f72016-06-01 14:08:21 -07001025
Winson Chung38848ca2013-10-08 12:03:44 -07001026 int top = getPaddingTop();
Sunny Goyal7c786f72016-06-01 14:08:21 -07001027 int bottom = b - t - getPaddingBottom();
Sunny Goyal4fe5a372015-05-14 19:55:10 -07001028
Sunny Goyal7c786f72016-06-01 14:08:21 -07001029 // Expand the background drawing bounds by the padding baked into the background drawable
1030 mBackground.getPadding(mTempRect);
1031 mBackground.setBounds(
Jon Miranda28032002017-07-13 16:18:56 -07001032 left - mTempRect.left - getPaddingLeft(),
1033 top - mTempRect.top - getPaddingTop(),
1034 right + mTempRect.right + getPaddingRight(),
1035 bottom + mTempRect.bottom + getPaddingBottom());
Sunny Goyalae6e3182019-04-30 12:04:37 -07001036
Sunny Goyalc4d32012020-04-03 17:10:11 -07001037 mShortcutsAndWidgets.layout(left, top, right, bottom);
Sunny Goyal7c786f72016-06-01 14:08:21 -07001038 }
1039
Tony Wickhama501d492015-11-03 18:05:01 -08001040 /**
1041 * Returns the amount of space left over after subtracting padding and cells. This space will be
1042 * very small, a few pixels at most, and is a result of rounding down when calculating the cell
Jon Miranda228877d2021-02-09 11:05:00 -05001043 * width in {@link DeviceProfile#calculateCellWidth(int, int, int)}.
Tony Wickhama501d492015-11-03 18:05:01 -08001044 */
1045 public int getUnusedHorizontalSpace() {
Jon Miranda228877d2021-02-09 11:05:00 -05001046 return getMeasuredWidth() - getPaddingLeft() - getPaddingRight() - (mCountX * mCellWidth)
Thales Lima78d00ad2021-09-30 11:29:06 +01001047 - ((mCountX - 1) * mBorderSpace.x);
Tony Wickhama501d492015-11-03 18:05:01 -08001048 }
1049
Sunny Goyal2805e632015-05-20 15:35:32 -07001050 @Override
1051 protected boolean verifyDrawable(Drawable who) {
Sunny Goyal7ce471b2017-08-02 03:37:39 -07001052 return super.verifyDrawable(who) || (who == mBackground);
Sunny Goyal2805e632015-05-20 15:35:32 -07001053 }
1054
Michael Jurkaa52570f2012-03-20 03:18:20 -07001055 public ShortcutAndWidgetContainer getShortcutsAndWidgets() {
Sunny Goyaldcbcc862014-08-12 15:58:36 -07001056 return mShortcutsAndWidgets;
Michael Jurkaa52570f2012-03-20 03:18:20 -07001057 }
1058
Jon Miranda228877d2021-02-09 11:05:00 -05001059 public View getChildAt(int cellX, int cellY) {
1060 return mShortcutsAndWidgets.getChildAt(cellX, cellY);
Patrick Dubroy440c3602010-07-13 17:50:32 -07001061 }
1062
Adam Cohen76fc0852011-06-17 13:26:23 -07001063 public boolean animateChildToPosition(final View child, int cellX, int cellY, int duration,
Adam Cohen482ed822012-03-02 14:15:13 -08001064 int delay, boolean permanent, boolean adjustOccupied) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07001065 ShortcutAndWidgetContainer clc = getShortcutsAndWidgets();
Adam Cohen482ed822012-03-02 14:15:13 -08001066
Adam Cohen1d13c0b2020-04-21 16:29:12 -07001067 if (clc.indexOfChild(child) != -1 && (child instanceof Reorderable)) {
Sebastian Francod4682992022-10-05 13:03:09 -05001068 final CellLayoutLayoutParams lp = (CellLayoutLayoutParams) child.getLayoutParams();
Adam Cohenbfbfd262011-06-13 16:55:12 -07001069 final ItemInfo info = (ItemInfo) child.getTag();
Adam Cohen1d13c0b2020-04-21 16:29:12 -07001070 final Reorderable item = (Reorderable) child;
Adam Cohenbfbfd262011-06-13 16:55:12 -07001071
1072 // We cancel any existing animations
1073 if (mReorderAnimators.containsKey(lp)) {
1074 mReorderAnimators.get(lp).cancel();
1075 mReorderAnimators.remove(lp);
1076 }
1077
Adam Cohen1d13c0b2020-04-21 16:29:12 -07001078
Adam Cohen482ed822012-03-02 14:15:13 -08001079 if (adjustOccupied) {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001080 GridOccupancy occupied = permanent ? mOccupied : mTmpOccupied;
Sebastian Franco877088e2023-01-03 15:16:22 -07001081 occupied.markCells(lp.getCellX(), lp.getCellY(), lp.cellHSpan, lp.cellVSpan, false);
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001082 occupied.markCells(cellX, cellY, lp.cellHSpan, lp.cellVSpan, true);
Adam Cohen482ed822012-03-02 14:15:13 -08001083 }
Adam Cohen1d13c0b2020-04-21 16:29:12 -07001084
1085 // Compute the new x and y position based on the new cellX and cellY
1086 // We leverage the actual layout logic in the layout params and hence need to modify
1087 // state and revert that state.
1088 final int oldX = lp.x;
1089 final int oldY = lp.y;
Adam Cohenbfbfd262011-06-13 16:55:12 -07001090 lp.isLockedToGrid = true;
Adam Cohen482ed822012-03-02 14:15:13 -08001091 if (permanent) {
Sunny Goyal669b71f2023-01-27 14:37:07 -08001092 lp.setCellX(cellX);
1093 lp.setCellY(cellY);
Adam Cohen482ed822012-03-02 14:15:13 -08001094 } else {
Sebastian Franco877088e2023-01-03 15:16:22 -07001095 lp.setTmpCellX(cellX);
1096 lp.setTmpCellY(cellY);
Adam Cohen482ed822012-03-02 14:15:13 -08001097 }
Jon Mirandae96798e2016-12-07 12:10:44 -08001098 clc.setupLp(child);
Adam Cohen482ed822012-03-02 14:15:13 -08001099 final int newX = lp.x;
1100 final int newY = lp.y;
Adam Cohen76fc0852011-06-17 13:26:23 -07001101 lp.x = oldX;
1102 lp.y = oldY;
Adam Cohen1d13c0b2020-04-21 16:29:12 -07001103 lp.isLockedToGrid = false;
1104 // End compute new x and y
1105
Sunny Goyal82dfc152023-02-24 16:50:09 -08001106 MultiTranslateDelegate mtd = item.getTranslateDelegate();
1107 float initPreviewOffsetX = mtd.getTranslationX(INDEX_REORDER_PREVIEW_OFFSET).getValue();
1108 float initPreviewOffsetY = mtd.getTranslationY(INDEX_REORDER_PREVIEW_OFFSET).getValue();
Adam Cohen1d13c0b2020-04-21 16:29:12 -07001109 final float finalPreviewOffsetX = newX - oldX;
1110 final float finalPreviewOffsetY = newY - oldY;
1111
Adam Cohen482ed822012-03-02 14:15:13 -08001112 // Exit early if we're not actually moving the view
Adam Cohen1d13c0b2020-04-21 16:29:12 -07001113 if (finalPreviewOffsetX == 0 && finalPreviewOffsetY == 0
1114 && initPreviewOffsetX == 0 && initPreviewOffsetY == 0) {
Adam Cohen482ed822012-03-02 14:15:13 -08001115 lp.isLockedToGrid = true;
1116 return true;
1117 }
1118
Sunny Goyal849c6a22018-08-08 16:33:46 -07001119 ValueAnimator va = ValueAnimator.ofFloat(0f, 1f);
Adam Cohen482ed822012-03-02 14:15:13 -08001120 va.setDuration(duration);
1121 mReorderAnimators.put(lp, va);
1122
1123 va.addUpdateListener(new AnimatorUpdateListener() {
1124 @Override
Adam Cohenbfbfd262011-06-13 16:55:12 -07001125 public void onAnimationUpdate(ValueAnimator animation) {
Jon Mirandae96798e2016-12-07 12:10:44 -08001126 float r = (Float) animation.getAnimatedValue();
Adam Cohen1d13c0b2020-04-21 16:29:12 -07001127 float x = (1 - r) * initPreviewOffsetX + r * finalPreviewOffsetX;
1128 float y = (1 - r) * initPreviewOffsetY + r * finalPreviewOffsetY;
Sunny Goyal82dfc152023-02-24 16:50:09 -08001129 item.getTranslateDelegate().setTranslation(INDEX_REORDER_PREVIEW_OFFSET, x, y);
Adam Cohenbfbfd262011-06-13 16:55:12 -07001130 }
1131 });
Adam Cohen482ed822012-03-02 14:15:13 -08001132 va.addListener(new AnimatorListenerAdapter() {
Adam Cohenbfbfd262011-06-13 16:55:12 -07001133 boolean cancelled = false;
1134 public void onAnimationEnd(Animator animation) {
1135 // If the animation was cancelled, it means that another animation
1136 // has interrupted this one, and we don't want to lock the item into
1137 // place just yet.
1138 if (!cancelled) {
1139 lp.isLockedToGrid = true;
Sunny Goyal82dfc152023-02-24 16:50:09 -08001140 item.getTranslateDelegate()
1141 .setTranslation(INDEX_REORDER_PREVIEW_OFFSET, 0, 0);
Adam Cohen482ed822012-03-02 14:15:13 -08001142 child.requestLayout();
Adam Cohenbfbfd262011-06-13 16:55:12 -07001143 }
1144 if (mReorderAnimators.containsKey(lp)) {
1145 mReorderAnimators.remove(lp);
1146 }
1147 }
1148 public void onAnimationCancel(Animator animation) {
1149 cancelled = true;
1150 }
1151 });
Adam Cohen482ed822012-03-02 14:15:13 -08001152 va.setStartDelay(delay);
1153 va.start();
Adam Cohenbfbfd262011-06-13 16:55:12 -07001154 return true;
1155 }
1156 return false;
1157 }
1158
Adam Cohenf7ca3b42021-02-22 11:03:58 -08001159 void visualizeDropLocation(int cellX, int cellY, int spanX, int spanY,
1160 DropTarget.DragObject dragObject) {
1161 if (mDragCell[0] != cellX || mDragCell[1] != cellY || mDragCellSpan[0] != spanX
1162 || mDragCellSpan[1] != spanY) {
Adam Cohen482ed822012-03-02 14:15:13 -08001163 mDragCell[0] = cellX;
1164 mDragCell[1] = cellY;
Adam Cohenf7ca3b42021-02-22 11:03:58 -08001165 mDragCellSpan[0] = spanX;
1166 mDragCellSpan[1] = spanY;
Steven Ng30dd1d62021-03-15 21:45:49 +00001167
Steven Nga999e222021-04-19 18:17:15 +01001168 // Apply color extraction on a widget when dragging.
1169 applyColorExtractionOnWidget(dragObject, mDragCell, spanX, spanY);
1170
Joe Onorato4be866d2010-10-10 11:26:02 -07001171 final int oldIndex = mDragOutlineCurrent;
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -07001172 mDragOutlineAnims[oldIndex].animateOut();
1173 mDragOutlineCurrent = (oldIndex + 1) % mDragOutlines.length;
Sunny Goyal106bf642015-07-16 12:18:06 -07001174
Sebastian Francod4682992022-10-05 13:03:09 -05001175 CellLayoutLayoutParams cell = mDragOutlines[mDragOutlineCurrent];
Sebastian Franco877088e2023-01-03 15:16:22 -07001176 cell.setCellX(cellX);
1177 cell.setCellY(cellY);
Adam Cohenf7ca3b42021-02-22 11:03:58 -08001178 cell.cellHSpan = spanX;
1179 cell.cellVSpan = spanY;
Adam Cohen65086992020-02-19 08:40:49 -08001180
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -07001181 mDragOutlineAnims[mDragOutlineCurrent].animateIn();
Adam Cohenf7ca3b42021-02-22 11:03:58 -08001182 invalidate();
Sunny Goyale78e3d72015-09-24 11:23:31 -07001183
1184 if (dragObject.stateAnnouncer != null) {
Sunny Goyalc13403c2016-11-18 23:44:48 -08001185 dragObject.stateAnnouncer.announce(getItemMoveDescription(cellX, cellY));
Sunny Goyale78e3d72015-09-24 11:23:31 -07001186 }
Adam Cohenf7ca3b42021-02-22 11:03:58 -08001187
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001188 }
1189 }
1190
Steven Ng30dd1d62021-03-15 21:45:49 +00001191 /** Applies the local color extraction to a dragging widget object. */
Steven Nga999e222021-04-19 18:17:15 +01001192 private void applyColorExtractionOnWidget(DropTarget.DragObject dragObject, int[] targetCell,
1193 int spanX, int spanY) {
Steven Ng30dd1d62021-03-15 21:45:49 +00001194 // Apply local extracted color if the DragView is an AppWidgetHostViewDrawable.
Steven Ng32427202021-04-19 18:12:12 +01001195 View view = dragObject.dragView.getContentView();
1196 if (view instanceof LauncherAppWidgetHostView) {
Sebastian Franco2986e0b2024-01-25 10:23:39 -08001197 int screenId = mCellLayoutContainer.getCellLayoutId(this);
Steven Ng30dd1d62021-03-15 21:45:49 +00001198 cellToRect(targetCell[0], targetCell[1], spanX, spanY, mTempRect);
Jonathan Miranda21930da2021-05-03 18:44:13 +00001199
Sunny Goyal69a8eec2021-07-22 10:02:50 -07001200 ((LauncherAppWidgetHostView) view).handleDrag(mTempRect, this, screenId);
Steven Ng30dd1d62021-03-15 21:45:49 +00001201 }
1202 }
1203
Sunny Goyal726bee72018-03-05 12:54:24 -08001204 @SuppressLint("StringFormatMatches")
Sunny Goyalc13403c2016-11-18 23:44:48 -08001205 public String getItemMoveDescription(int cellX, int cellY) {
1206 if (mContainerType == HOTSEAT) {
1207 return getContext().getString(R.string.move_to_hotseat_position,
1208 Math.max(cellX, cellY) + 1);
1209 } else {
Andras Kloczl2dacbee2022-02-21 16:53:28 +00001210 int row = cellY + 1;
Sebastian Franco2986e0b2024-01-25 10:23:39 -08001211 int col = Utilities.isRtl(getResources()) ? mCountX - cellX : cellX + 1;
1212 int panelCount = mCellLayoutContainer.getPanelCount();
1213 int pageIndex = mCellLayoutContainer.getCellLayoutIndex(this);
Andras Kloczl2dacbee2022-02-21 16:53:28 +00001214 if (panelCount > 1) {
1215 // Increment the column if the target is on the right side of a two panel home
Andras Kloczl2dacbee2022-02-21 16:53:28 +00001216 col += (pageIndex % panelCount) * mCountX;
1217 }
Sebastian Franco930531f2022-06-16 16:49:11 -07001218 return getContext().getString(R.string.move_to_empty_cell_description, row, col,
Sebastian Franco2986e0b2024-01-25 10:23:39 -08001219 mCellLayoutContainer.getPageDescription(pageIndex));
Sunny Goyalc13403c2016-11-18 23:44:48 -08001220 }
1221 }
1222
Adam Cohene0310962011-04-18 16:15:31 -07001223 public void clearDragOutlines() {
1224 final int oldIndex = mDragOutlineCurrent;
1225 mDragOutlineAnims[oldIndex].animateOut();
Adam Cohend41fbf52012-02-16 23:53:59 -08001226 mDragCell[0] = mDragCell[1] = -1;
Adam Cohene0310962011-04-18 16:15:31 -07001227 }
1228
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001229 /**
Jeff Sharkey70864282009-04-07 21:08:40 -07001230 * Find a vacant area that will fit the given bounds nearest the requested
1231 * cell location. Uses Euclidean distance to score multiple vacant areas.
Winson Chungaafa03c2010-06-11 17:34:16 -07001232 *
Romain Guy51afc022009-05-04 18:03:43 -07001233 * @param pixelX The X location at which you want to search for a vacant area.
1234 * @param pixelY The Y location at which you want to search for a vacant area.
Adam Cohend41fbf52012-02-16 23:53:59 -08001235 * @param minSpanX The minimum horizontal span required
1236 * @param minSpanY The minimum vertical span required
1237 * @param spanX Horizontal span of the object.
1238 * @param spanY Vertical span of the object.
1239 * @param result Array in which to place the result, or null (in which case a new array will
1240 * be allocated)
1241 * @return The X, Y cell of a vacant area that can contain this object,
1242 * nearest the requested location.
1243 */
Sebastian Francoe4c03452022-12-27 14:50:02 -06001244 public int[] findNearestVacantArea(int pixelX, int pixelY, int minSpanX, int minSpanY,
1245 int spanX, int spanY, int[] result, int[] resultSpan) {
Sebastian Francob57c0b22022-06-28 13:54:35 -07001246 return findNearestArea(pixelX, pixelY, minSpanX, minSpanY, spanX, spanY, false,
Adam Cohend41fbf52012-02-16 23:53:59 -08001247 result, resultSpan);
1248 }
1249
Adam Cohend41fbf52012-02-16 23:53:59 -08001250 /**
1251 * Find a vacant area that will fit the given bounds nearest the requested
1252 * cell location. Uses Euclidean distance to score multiple vacant areas.
Sebastian Francob57c0b22022-06-28 13:54:35 -07001253 * @param relativeXPos The X location relative to the Cell layout at which you want to search
1254 * for a vacant area.
1255 * @param relativeYPos The Y location relative to the Cell layout at which you want to search
1256 * for a vacant area.
Adam Cohend41fbf52012-02-16 23:53:59 -08001257 * @param minSpanX The minimum horizontal span required
1258 * @param minSpanY The minimum vertical span required
1259 * @param spanX Horizontal span of the object.
1260 * @param spanY Vertical span of the object.
1261 * @param ignoreOccupied If true, the result can be an occupied cell
1262 * @param result Array in which to place the result, or null (in which case a new array will
1263 * be allocated)
1264 * @return The X, Y cell of a vacant area that can contain this object,
1265 * nearest the requested location.
1266 */
Sebastian Franco96c46e72023-05-08 10:04:44 -06001267 protected int[] findNearestArea(int relativeXPos, int relativeYPos, int minSpanX, int minSpanY,
Sebastian Francob57c0b22022-06-28 13:54:35 -07001268 int spanX, int spanY, boolean ignoreOccupied, int[] result, int[] resultSpan) {
Sebastian Francob57c0b22022-06-28 13:54:35 -07001269 // For items with a spanX / spanY > 1, the passed in point (relativeXPos, relativeYPos)
1270 // corresponds to the center of the item, but we are searching based on the top-left cell,
1271 // so we translate the point over to correspond to the top-left.
1272 relativeXPos = (int) (relativeXPos - (mCellWidth + mBorderSpace.x) * (spanX - 1) / 2f);
1273 relativeYPos = (int) (relativeYPos - (mCellHeight + mBorderSpace.y) * (spanY - 1) / 2f);
Adam Cohene3e27a82011-04-15 12:07:39 -07001274
Jeff Sharkey70864282009-04-07 21:08:40 -07001275 // Keep track of best-scoring drop area
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001276 final int[] bestXY = result != null ? result : new int[2];
Jeff Sharkey70864282009-04-07 21:08:40 -07001277 double bestDistance = Double.MAX_VALUE;
Adam Cohend41fbf52012-02-16 23:53:59 -08001278 final Rect bestRect = new Rect(-1, -1, -1, -1);
Rajeev Kumar9962dbe2017-06-12 12:16:20 -07001279 final Stack<Rect> validRegions = new Stack<>();
Winson Chungaafa03c2010-06-11 17:34:16 -07001280
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001281 final int countX = mCountX;
1282 final int countY = mCountY;
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001283
Adam Cohend41fbf52012-02-16 23:53:59 -08001284 if (minSpanX <= 0 || minSpanY <= 0 || spanX <= 0 || spanY <= 0 ||
1285 spanX < minSpanX || spanY < minSpanY) {
1286 return bestXY;
1287 }
1288
1289 for (int y = 0; y < countY - (minSpanY - 1); y++) {
Michael Jurkac28de512010-08-13 11:27:44 -07001290 inner:
Adam Cohend41fbf52012-02-16 23:53:59 -08001291 for (int x = 0; x < countX - (minSpanX - 1); x++) {
1292 int ySize = -1;
1293 int xSize = -1;
Sebastian Francob57c0b22022-06-28 13:54:35 -07001294 if (!ignoreOccupied) {
Adam Cohend41fbf52012-02-16 23:53:59 -08001295 // First, let's see if this thing fits anywhere
1296 for (int i = 0; i < minSpanX; i++) {
1297 for (int j = 0; j < minSpanY; j++) {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001298 if (mOccupied.cells[x + i][y + j]) {
Adam Cohendf035382011-04-11 17:22:04 -07001299 continue inner;
1300 }
Michael Jurkac28de512010-08-13 11:27:44 -07001301 }
1302 }
Adam Cohend41fbf52012-02-16 23:53:59 -08001303 xSize = minSpanX;
1304 ySize = minSpanY;
1305
1306 // We know that the item will fit at _some_ acceptable size, now let's see
1307 // how big we can make it. We'll alternate between incrementing x and y spans
1308 // until we hit a limit.
1309 boolean incX = true;
1310 boolean hitMaxX = xSize >= spanX;
1311 boolean hitMaxY = ySize >= spanY;
1312 while (!(hitMaxX && hitMaxY)) {
1313 if (incX && !hitMaxX) {
1314 for (int j = 0; j < ySize; j++) {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001315 if (x + xSize > countX -1 || mOccupied.cells[x + xSize][y + j]) {
Adam Cohend41fbf52012-02-16 23:53:59 -08001316 // We can't move out horizontally
1317 hitMaxX = true;
1318 }
1319 }
1320 if (!hitMaxX) {
1321 xSize++;
1322 }
1323 } else if (!hitMaxY) {
1324 for (int i = 0; i < xSize; i++) {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001325 if (y + ySize > countY - 1 || mOccupied.cells[x + i][y + ySize]) {
Adam Cohend41fbf52012-02-16 23:53:59 -08001326 // We can't move out vertically
1327 hitMaxY = true;
1328 }
1329 }
1330 if (!hitMaxY) {
1331 ySize++;
1332 }
1333 }
1334 hitMaxX |= xSize >= spanX;
1335 hitMaxY |= ySize >= spanY;
1336 incX = !incX;
1337 }
Michael Jurkac28de512010-08-13 11:27:44 -07001338 }
Sunny Goyal2805e632015-05-20 15:35:32 -07001339 final int[] cellXY = mTmpPoint;
Adam Cohene3e27a82011-04-15 12:07:39 -07001340 cellToCenterPoint(x, y, cellXY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001341
Adam Cohend41fbf52012-02-16 23:53:59 -08001342 // We verify that the current rect is not a sub-rect of any of our previous
1343 // candidates. In this case, the current rect is disqualified in favour of the
1344 // containing rect.
Sebastian Franco4a922672022-10-27 16:42:24 -07001345 Rect currentRect = new Rect(x, y, x + xSize, y + ySize);
Adam Cohend41fbf52012-02-16 23:53:59 -08001346 boolean contained = false;
1347 for (Rect r : validRegions) {
1348 if (r.contains(currentRect)) {
1349 contained = true;
1350 break;
1351 }
1352 }
1353 validRegions.push(currentRect);
Sebastian Francob57c0b22022-06-28 13:54:35 -07001354 double distance = Math.hypot(cellXY[0] - relativeXPos, cellXY[1] - relativeYPos);
Adam Cohen482ed822012-03-02 14:15:13 -08001355
Adam Cohend41fbf52012-02-16 23:53:59 -08001356 if ((distance <= bestDistance && !contained) ||
1357 currentRect.contains(bestRect)) {
Michael Jurkac28de512010-08-13 11:27:44 -07001358 bestDistance = distance;
1359 bestXY[0] = x;
1360 bestXY[1] = y;
Adam Cohend41fbf52012-02-16 23:53:59 -08001361 if (resultSpan != null) {
1362 resultSpan[0] = xSize;
1363 resultSpan[1] = ySize;
1364 }
1365 bestRect.set(currentRect);
Michael Jurkac28de512010-08-13 11:27:44 -07001366 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001367 }
1368 }
1369
Adam Cohenc0dcf592011-06-01 15:30:43 -07001370 // Return -1, -1 if no suitable location found
1371 if (bestDistance == Double.MAX_VALUE) {
1372 bestXY[0] = -1;
1373 bestXY[1] = -1;
Jeff Sharkey70864282009-04-07 21:08:40 -07001374 }
Adam Cohenc0dcf592011-06-01 15:30:43 -07001375 return bestXY;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001376 }
Winson Chungaafa03c2010-06-11 17:34:16 -07001377
Sebastian Francoe4c03452022-12-27 14:50:02 -06001378 public GridOccupancy getOccupied() {
1379 return mOccupied;
1380 }
1381
Adam Cohen482ed822012-03-02 14:15:13 -08001382 private void copySolutionToTempState(ItemConfiguration solution, View dragView) {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001383 mTmpOccupied.clear();
Adam Cohen482ed822012-03-02 14:15:13 -08001384
Michael Jurkaa52570f2012-03-20 03:18:20 -07001385 int childCount = mShortcutsAndWidgets.getChildCount();
Adam Cohen482ed822012-03-02 14:15:13 -08001386 for (int i = 0; i < childCount; i++) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07001387 View child = mShortcutsAndWidgets.getChildAt(i);
Adam Cohen482ed822012-03-02 14:15:13 -08001388 if (child == dragView) continue;
Sebastian Francod4682992022-10-05 13:03:09 -05001389 CellLayoutLayoutParams lp = (CellLayoutLayoutParams) child.getLayoutParams();
Adam Cohen8baab352012-03-20 17:39:21 -07001390 CellAndSpan c = solution.map.get(child);
1391 if (c != null) {
Sebastian Franco877088e2023-01-03 15:16:22 -07001392 lp.setTmpCellX(c.cellX);
1393 lp.setTmpCellY(c.cellY);
Adam Cohen8baab352012-03-20 17:39:21 -07001394 lp.cellHSpan = c.spanX;
1395 lp.cellVSpan = c.spanY;
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001396 mTmpOccupied.markCells(c, true);
Adam Cohen482ed822012-03-02 14:15:13 -08001397 }
1398 }
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001399 mTmpOccupied.markCells(solution, true);
Adam Cohen482ed822012-03-02 14:15:13 -08001400 }
1401
1402 private void animateItemsToSolution(ItemConfiguration solution, View dragView, boolean
1403 commitDragView) {
1404
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001405 GridOccupancy occupied = DESTRUCTIVE_REORDER ? mOccupied : mTmpOccupied;
1406 occupied.clear();
Adam Cohen482ed822012-03-02 14:15:13 -08001407
Michael Jurkaa52570f2012-03-20 03:18:20 -07001408 int childCount = mShortcutsAndWidgets.getChildCount();
Adam Cohen482ed822012-03-02 14:15:13 -08001409 for (int i = 0; i < childCount; i++) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07001410 View child = mShortcutsAndWidgets.getChildAt(i);
Adam Cohen482ed822012-03-02 14:15:13 -08001411 if (child == dragView) continue;
Adam Cohen8baab352012-03-20 17:39:21 -07001412 CellAndSpan c = solution.map.get(child);
1413 if (c != null) {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001414 animateChildToPosition(child, c.cellX, c.cellY, REORDER_ANIMATION_DURATION, 0,
Adam Cohen19f37922012-03-21 11:59:11 -07001415 DESTRUCTIVE_REORDER, false);
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001416 occupied.markCells(c, true);
Adam Cohen482ed822012-03-02 14:15:13 -08001417 }
1418 }
1419 if (commitDragView) {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001420 occupied.markCells(solution, true);
Adam Cohen482ed822012-03-02 14:15:13 -08001421 }
1422 }
1423
Adam Cohenfa3c58f2013-12-06 16:10:55 -08001424
1425 // This method starts or changes the reorder preview animations
1426 private void beginOrAdjustReorderPreviewAnimations(ItemConfiguration solution,
Adam Cohen65086992020-02-19 08:40:49 -08001427 View dragView, int mode) {
Adam Cohen19f37922012-03-21 11:59:11 -07001428 int childCount = mShortcutsAndWidgets.getChildCount();
Adam Cohen19f37922012-03-21 11:59:11 -07001429 for (int i = 0; i < childCount; i++) {
1430 View child = mShortcutsAndWidgets.getChildAt(i);
1431 if (child == dragView) continue;
1432 CellAndSpan c = solution.map.get(child);
Sebastian Franco5f0af4f2023-11-21 10:45:45 -06001433 boolean skip = mode == ReorderPreviewAnimation.MODE_HINT
1434 && !solution.intersectingViews.contains(child);
Adam Cohend9162062020-03-24 16:35:35 -07001435
Sebastian Francod4682992022-10-05 13:03:09 -05001436 CellLayoutLayoutParams lp = (CellLayoutLayoutParams) child.getLayoutParams();
Adam Cohend9162062020-03-24 16:35:35 -07001437 if (c != null && !skip && (child instanceof Reorderable)) {
Sebastian Francof7654252024-01-18 11:50:50 -08001438 ReorderPreviewAnimation rha = new ReorderPreviewAnimation(child, mode,
1439 lp.getCellX(), lp.getCellY(), c.cellX, c.cellY, c.spanX, c.spanY,
1440 mReorderPreviewAnimationMagnitude, this, mShakeAnimators);
Adam Cohend024f982012-05-23 18:26:45 -07001441 rha.animate();
Adam Cohen19f37922012-03-21 11:59:11 -07001442 }
1443 }
1444 }
1445
Adam Cohenfa3c58f2013-12-06 16:10:55 -08001446 private void completeAndClearReorderPreviewAnimations() {
1447 for (ReorderPreviewAnimation a: mShakeAnimators.values()) {
Adam Cohend9162062020-03-24 16:35:35 -07001448 a.finishAnimation();
Adam Cohen19f37922012-03-21 11:59:11 -07001449 }
1450 mShakeAnimators.clear();
1451 }
1452
Sunny Goyal711c5962021-06-23 12:36:18 -07001453 private void commitTempPlacement(View dragView) {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001454 mTmpOccupied.copyTo(mOccupied);
Sunny Goyalaa8ef112015-06-12 20:04:41 -07001455
Sebastian Franco2986e0b2024-01-25 10:23:39 -08001456 int screenId = mCellLayoutContainer.getCellLayoutId(this);
Sunny Goyalaa8ef112015-06-12 20:04:41 -07001457 int container = Favorites.CONTAINER_DESKTOP;
1458
Sunny Goyalc13403c2016-11-18 23:44:48 -08001459 if (mContainerType == HOTSEAT) {
Sunny Goyalaa8ef112015-06-12 20:04:41 -07001460 screenId = -1;
1461 container = Favorites.CONTAINER_HOTSEAT;
1462 }
1463
Michael Jurkaa52570f2012-03-20 03:18:20 -07001464 int childCount = mShortcutsAndWidgets.getChildCount();
Adam Cohen482ed822012-03-02 14:15:13 -08001465 for (int i = 0; i < childCount; i++) {
Adam Cohenea889a22012-03-27 16:45:39 -07001466 View child = mShortcutsAndWidgets.getChildAt(i);
Sebastian Francod4682992022-10-05 13:03:09 -05001467 CellLayoutLayoutParams lp = (CellLayoutLayoutParams) child.getLayoutParams();
Adam Cohenea889a22012-03-27 16:45:39 -07001468 ItemInfo info = (ItemInfo) child.getTag();
Adam Cohen2acce882012-03-28 19:03:19 -07001469 // We do a null check here because the item info can be null in the case of the
1470 // AllApps button in the hotseat.
Sunny Goyal711c5962021-06-23 12:36:18 -07001471 if (info != null && child != dragView) {
Sunny Goyal669b71f2023-01-27 14:37:07 -08001472 CellPos presenterPos = mActivity.getCellPosMapper().mapModelToPresenter(info);
1473 final boolean requiresDbUpdate = (presenterPos.cellX != lp.getTmpCellX()
1474 || presenterPos.cellY != lp.getTmpCellY() || info.spanX != lp.cellHSpan
1475 || info.spanY != lp.cellVSpan || presenterPos.screenId != screenId);
Sunny Goyalaa8ef112015-06-12 20:04:41 -07001476
Sebastian Franco877088e2023-01-03 15:16:22 -07001477 lp.setCellX(lp.getTmpCellX());
Sebastian Franco877088e2023-01-03 15:16:22 -07001478 lp.setCellY(lp.getTmpCellY());
Sunny Goyalaa8ef112015-06-12 20:04:41 -07001479 if (requiresDbUpdate) {
Sunny Goyalab770a12018-11-14 15:17:26 -08001480 Launcher.cast(mActivity).getModelWriter().modifyItemInDatabase(info, container,
Sunny Goyal669b71f2023-01-27 14:37:07 -08001481 screenId, lp.getCellX(), lp.getCellY(), lp.cellHSpan, lp.cellVSpan);
Sunny Goyalaa8ef112015-06-12 20:04:41 -07001482 }
Adam Cohen2acce882012-03-28 19:03:19 -07001483 }
Adam Cohen482ed822012-03-02 14:15:13 -08001484 }
1485 }
1486
Sunny Goyalf7a29e82015-04-24 15:20:43 -07001487 private void setUseTempCoords(boolean useTempCoords) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07001488 int childCount = mShortcutsAndWidgets.getChildCount();
Adam Cohen482ed822012-03-02 14:15:13 -08001489 for (int i = 0; i < childCount; i++) {
Sebastian Francod4682992022-10-05 13:03:09 -05001490 CellLayoutLayoutParams lp = (CellLayoutLayoutParams) mShortcutsAndWidgets.getChildAt(
1491 i).getLayoutParams();
Adam Cohen482ed822012-03-02 14:15:13 -08001492 lp.useTmpCoords = useTempCoords;
1493 }
1494 }
1495
Sebastian Franco5c8f8682023-11-14 09:52:41 -06001496 /**
1497 * For a given region, return the rectangle of the overlapping cell and span with the given
1498 * region including the region itself. If there is no overlap the rectangle will be
1499 * invalid i.e. -1, 0, -1, 0.
1500 */
1501 @Nullable
1502 public Rect getIntersectingRectanglesInRegion(final Rect region, final View dragView) {
1503 Rect boundingRect = new Rect(region);
Adam Cohen19f37922012-03-21 11:59:11 -07001504 Rect r1 = new Rect();
Sebastian Franco5c8f8682023-11-14 09:52:41 -06001505 boolean isOverlapping = false;
Adam Cohen19f37922012-03-21 11:59:11 -07001506 final int count = mShortcutsAndWidgets.getChildCount();
1507 for (int i = 0; i < count; i++) {
1508 View child = mShortcutsAndWidgets.getChildAt(i);
1509 if (child == dragView) continue;
Sebastian Francod4682992022-10-05 13:03:09 -05001510 CellLayoutLayoutParams
1511 lp = (CellLayoutLayoutParams) child.getLayoutParams();
Sebastian Franco877088e2023-01-03 15:16:22 -07001512 r1.set(lp.getCellX(), lp.getCellY(), lp.getCellX() + lp.cellHSpan,
1513 lp.getCellY() + lp.cellVSpan);
Sebastian Franco5c8f8682023-11-14 09:52:41 -06001514 if (Rect.intersects(region, r1)) {
1515 isOverlapping = true;
1516 boundingRect.union(r1);
Adam Cohen19f37922012-03-21 11:59:11 -07001517 }
1518 }
Sebastian Franco5c8f8682023-11-14 09:52:41 -06001519 return isOverlapping ? boundingRect : null;
Adam Cohen19f37922012-03-21 11:59:11 -07001520 }
1521
Sebastian Francoe4c03452022-12-27 14:50:02 -06001522 public boolean isNearestDropLocationOccupied(int pixelX, int pixelY, int spanX, int spanY,
Adam Cohen19f37922012-03-21 11:59:11 -07001523 View dragView, int[] result) {
Sebastián Francof9a6ac22022-11-15 22:56:37 +00001524 result = findNearestAreaIgnoreOccupied(pixelX, pixelY, spanX, spanY, result);
Sebastian Franco5c8f8682023-11-14 09:52:41 -06001525 return getIntersectingRectanglesInRegion(
1526 new Rect(result[0], result[1], result[0] + spanX, result[1] + spanY),
1527 dragView
1528 ) != null;
Adam Cohen19f37922012-03-21 11:59:11 -07001529 }
1530
1531 void revertTempState() {
Adam Cohenfa3c58f2013-12-06 16:10:55 -08001532 completeAndClearReorderPreviewAnimations();
1533 if (isItemPlacementDirty() && !DESTRUCTIVE_REORDER) {
1534 final int count = mShortcutsAndWidgets.getChildCount();
1535 for (int i = 0; i < count; i++) {
1536 View child = mShortcutsAndWidgets.getChildAt(i);
Sebastian Francod4682992022-10-05 13:03:09 -05001537 CellLayoutLayoutParams
1538 lp = (CellLayoutLayoutParams) child.getLayoutParams();
Sebastian Franco877088e2023-01-03 15:16:22 -07001539 if (lp.getTmpCellX() != lp.getCellX() || lp.getTmpCellY() != lp.getCellY()) {
1540 lp.setTmpCellX(lp.getCellX());
1541 lp.setTmpCellY(lp.getCellY());
1542 animateChildToPosition(child, lp.getCellX(), lp.getCellY(),
1543 REORDER_ANIMATION_DURATION, 0, false, false);
Adam Cohenfa3c58f2013-12-06 16:10:55 -08001544 }
Adam Cohen19f37922012-03-21 11:59:11 -07001545 }
Adam Cohenfa3c58f2013-12-06 16:10:55 -08001546 setItemPlacementDirty(false);
Adam Cohen19f37922012-03-21 11:59:11 -07001547 }
Adam Cohen19f37922012-03-21 11:59:11 -07001548 }
1549
Adam Cohenbebf0422012-04-11 18:06:28 -07001550 boolean createAreaForResize(int cellX, int cellY, int spanX, int spanY,
1551 View dragView, int[] direction, boolean commit) {
1552 int[] pixelXY = new int[2];
1553 regionToCenterPoint(cellX, cellY, spanX, spanY, pixelXY);
1554
1555 // First we determine if things have moved enough to cause a different layout
Adam Cohenfa3c58f2013-12-06 16:10:55 -08001556 ItemConfiguration swapSolution = findReorderSolution(pixelXY[0], pixelXY[1], spanX, spanY,
Charlie Anderson46122392024-01-18 17:23:46 -05001557 spanX, spanY, direction, dragView, true);
Adam Cohenbebf0422012-04-11 18:06:28 -07001558
1559 setUseTempCoords(true);
1560 if (swapSolution != null && swapSolution.isSolution) {
1561 // If we're just testing for a possible location (MODE_ACCEPT_DROP), we don't bother
1562 // committing anything or animating anything as we just want to determine if a solution
1563 // exists
1564 copySolutionToTempState(swapSolution, dragView);
1565 setItemPlacementDirty(true);
1566 animateItemsToSolution(swapSolution, dragView, commit);
1567
1568 if (commit) {
Sunny Goyal711c5962021-06-23 12:36:18 -07001569 commitTempPlacement(null);
Adam Cohenfa3c58f2013-12-06 16:10:55 -08001570 completeAndClearReorderPreviewAnimations();
Adam Cohenbebf0422012-04-11 18:06:28 -07001571 setItemPlacementDirty(false);
1572 } else {
Adam Cohenfa3c58f2013-12-06 16:10:55 -08001573 beginOrAdjustReorderPreviewAnimations(swapSolution, dragView,
Adam Cohen65086992020-02-19 08:40:49 -08001574 ReorderPreviewAnimation.MODE_PREVIEW);
Adam Cohenbebf0422012-04-11 18:06:28 -07001575 }
1576 mShortcutsAndWidgets.requestLayout();
1577 }
1578 return swapSolution.isSolution;
1579 }
1580
Sebastian Francoe4c03452022-12-27 14:50:02 -06001581 public ReorderAlgorithm createReorderAlgorithm() {
1582 return new ReorderAlgorithm(this);
1583 }
1584
Sebastian Franco09589322022-11-02 15:25:58 -07001585 protected ItemConfiguration findReorderSolution(int pixelX, int pixelY, int minSpanX,
Charlie Anderson46122392024-01-18 17:23:46 -05001586 int minSpanY, int spanX, int spanY, int[] direction, View dragView, boolean decX) {
Sebastian Franco25f8e402023-12-02 13:54:05 -06001587 ItemConfiguration configuration = new ItemConfiguration();
1588 copyCurrentStateToSolution(configuration);
1589 ReorderParameters parameters = new ReorderParameters(pixelX, pixelY, spanX, spanY, minSpanX,
1590 minSpanY, dragView, configuration);
Charlie Anderson46122392024-01-18 17:23:46 -05001591 int[] directionVector = direction != null ? direction : mDirectionVector;
1592 return createReorderAlgorithm().findReorderSolution(parameters, directionVector, decX);
Sebastian Franco09589322022-11-02 15:25:58 -07001593 }
1594
Sebastián Franco61fbd182023-11-29 21:30:43 +00001595 public void copyCurrentStateToSolution(ItemConfiguration solution) {
Sebastian Franco9cab1c32022-10-25 17:28:54 -07001596 int childCount = mShortcutsAndWidgets.getChildCount();
1597 for (int i = 0; i < childCount; i++) {
1598 View child = mShortcutsAndWidgets.getChildAt(i);
1599 CellLayoutLayoutParams lp = (CellLayoutLayoutParams) child.getLayoutParams();
Sebastián Franco61fbd182023-11-29 21:30:43 +00001600 solution.add(child,
1601 new CellAndSpan(lp.getCellX(), lp.getCellY(), lp.cellHSpan, lp.cellVSpan));
Sebastian Franco9cab1c32022-10-25 17:28:54 -07001602 }
Sebastian Franco53a15a42022-10-25 17:28:54 -07001603 }
1604
1605 /**
Sebastian Franco53a15a42022-10-25 17:28:54 -07001606 * When the user drags an Item in the workspace sometimes we need to move the items already in
1607 * the workspace to make space for the new item, this function return a solution for that
1608 * reorder.
1609 *
1610 * @param pixelX X coordinate in the screen of the dragView in pixels
1611 * @param pixelY Y coordinate in the screen of the dragView in pixels
1612 * @param minSpanX minimum horizontal span the item can be shrunk to
1613 * @param minSpanY minimum vertical span the item can be shrunk to
1614 * @param spanX occupied horizontal span
1615 * @param spanY occupied vertical span
1616 * @param dragView the view of the item being draged
1617 * @return returns a solution for the given parameters, the solution contains all the icons and
1618 * the locations they should be in the given solution.
1619 */
1620 public ItemConfiguration calculateReorder(int pixelX, int pixelY, int minSpanX, int minSpanY,
1621 int spanX, int spanY, View dragView) {
Sebastian Franco25f8e402023-12-02 13:54:05 -06001622 ItemConfiguration configuration = new ItemConfiguration();
1623 copyCurrentStateToSolution(configuration);
1624 return createReorderAlgorithm().calculateReorder(
1625 new ReorderParameters(pixelX, pixelY, spanX, spanY, minSpanX, minSpanY, dragView,
1626 configuration)
1627 );
Sebastian Franco53a15a42022-10-25 17:28:54 -07001628 }
Adam Cohen482ed822012-03-02 14:15:13 -08001629
Sebastian Franco9cab1c32022-10-25 17:28:54 -07001630 int[] performReorder(int pixelX, int pixelY, int minSpanX, int minSpanY, int spanX, int spanY,
1631 View dragView, int[] result, int[] resultSpan, int mode) {
1632 if (resultSpan == null) {
1633 resultSpan = new int[]{-1, -1};
1634 }
1635 if (result == null) {
1636 result = new int[]{-1, -1};
1637 }
Sebastian Franco5d990ee2022-11-01 16:08:24 -07001638
1639 ItemConfiguration finalSolution = null;
1640 // We want the solution to match the animation of the preview and to match the drop so we
1641 // only recalculate in mode MODE_SHOW_REORDER_HINT because that the first one to run in the
1642 // reorder cycle.
1643 if (mode == MODE_SHOW_REORDER_HINT || mPreviousSolution == null) {
1644 finalSolution = calculateReorder(pixelX, pixelY, minSpanX, minSpanY, spanX, spanY,
1645 dragView);
1646 mPreviousSolution = finalSolution;
1647 } else {
Sebastian Franco9cab1c32022-10-25 17:28:54 -07001648 finalSolution = mPreviousSolution;
1649 // We reset this vector after drop
1650 if (mode == MODE_ON_DROP || mode == MODE_ON_DROP_EXTERNAL) {
1651 mPreviousSolution = null;
1652 }
Sebastian Franco9cab1c32022-10-25 17:28:54 -07001653 }
1654
1655 if (finalSolution == null || !finalSolution.isSolution) {
1656 result[0] = result[1] = resultSpan[0] = resultSpan[1] = -1;
1657 } else {
1658 result[0] = finalSolution.cellX;
1659 result[1] = finalSolution.cellY;
1660 resultSpan[0] = finalSolution.spanX;
1661 resultSpan[1] = finalSolution.spanY;
Sebastian Franco9c743272022-11-15 15:03:25 -08001662 performReorder(finalSolution, dragView, mode);
Sebastian Franco9cab1c32022-10-25 17:28:54 -07001663 }
Sebastian Franco9cab1c32022-10-25 17:28:54 -07001664 return result;
1665 }
1666
Sebastian Franco53a15a42022-10-25 17:28:54 -07001667 /**
1668 * Animates and submits in the DB the given ItemConfiguration depending of the mode.
1669 *
1670 * @param solution represents widgets on the screen which the Workspace will animate to and
1671 * would be submitted to the database.
1672 * @param dragView view which is being dragged over the workspace that trigger the reorder
1673 * @param mode depending on the mode different animations would be played and depending on the
1674 * mode the solution would be submitted or not the database.
1675 * The possible modes are {@link MODE_SHOW_REORDER_HINT}, {@link MODE_DRAG_OVER},
1676 * {@link MODE_ON_DROP}, {@link MODE_ON_DROP_EXTERNAL}, {@link MODE_ACCEPT_DROP}
1677 * defined in {@link CellLayout}.
1678 */
Sebastian Francoe4c03452022-12-27 14:50:02 -06001679 public void performReorder(ItemConfiguration solution, View dragView, int mode) {
Adam Cohenfa3c58f2013-12-06 16:10:55 -08001680 if (mode == MODE_SHOW_REORDER_HINT) {
Sebastian Franco53a15a42022-10-25 17:28:54 -07001681 beginOrAdjustReorderPreviewAnimations(solution, dragView,
1682 ReorderPreviewAnimation.MODE_HINT);
1683 return;
1684 }
1685 // If we're just testing for a possible location (MODE_ACCEPT_DROP), we don't bother
1686 // committing anything or animating anything as we just want to determine if a solution
1687 // exists
1688 if (mode == MODE_DRAG_OVER || mode == MODE_ON_DROP || mode == MODE_ON_DROP_EXTERNAL) {
1689 if (!DESTRUCTIVE_REORDER) {
1690 setUseTempCoords(true);
1691 }
1692
1693 if (!DESTRUCTIVE_REORDER) {
1694 copySolutionToTempState(solution, dragView);
1695 }
1696 setItemPlacementDirty(true);
1697 animateItemsToSolution(solution, dragView, mode == MODE_ON_DROP);
1698
1699 if (!DESTRUCTIVE_REORDER
1700 && (mode == MODE_ON_DROP || mode == MODE_ON_DROP_EXTERNAL)) {
1701 // Since the temp solution didn't update dragView, don't commit it either
1702 commitTempPlacement(dragView);
1703 completeAndClearReorderPreviewAnimations();
1704 setItemPlacementDirty(false);
Adam Cohenfa3c58f2013-12-06 16:10:55 -08001705 } else {
Sebastian Franco53a15a42022-10-25 17:28:54 -07001706 beginOrAdjustReorderPreviewAnimations(solution, dragView,
1707 ReorderPreviewAnimation.MODE_PREVIEW);
Adam Cohenfa3c58f2013-12-06 16:10:55 -08001708 }
Adam Cohenfa3c58f2013-12-06 16:10:55 -08001709 }
1710
Sebastian Franco53a15a42022-10-25 17:28:54 -07001711 if (mode == MODE_ON_DROP && !DESTRUCTIVE_REORDER) {
Adam Cohen482ed822012-03-02 14:15:13 -08001712 setUseTempCoords(false);
1713 }
Adam Cohen482ed822012-03-02 14:15:13 -08001714
Michael Jurkaa52570f2012-03-20 03:18:20 -07001715 mShortcutsAndWidgets.requestLayout();
Adam Cohen482ed822012-03-02 14:15:13 -08001716 }
1717
Adam Cohen19f37922012-03-21 11:59:11 -07001718 void setItemPlacementDirty(boolean dirty) {
1719 mItemPlacementDirty = dirty;
Adam Cohen482ed822012-03-02 14:15:13 -08001720 }
Adam Cohen19f37922012-03-21 11:59:11 -07001721 boolean isItemPlacementDirty() {
1722 return mItemPlacementDirty;
Adam Cohen482ed822012-03-02 14:15:13 -08001723 }
1724
Sebastian Francoe4c03452022-12-27 14:50:02 -06001725 /**
Adam Cohendf035382011-04-11 17:22:04 -07001726 * Find a starting cell position that will fit the given bounds nearest the requested
1727 * cell location. Uses Euclidean distance to score multiple vacant areas.
1728 *
1729 * @param pixelX The X location at which you want to search for a vacant area.
1730 * @param pixelY The Y location at which you want to search for a vacant area.
1731 * @param spanX Horizontal span of the object.
1732 * @param spanY Vertical span of the object.
Adam Cohendf035382011-04-11 17:22:04 -07001733 * @param result Previously returned value to possibly recycle.
1734 * @return The X, Y cell of a vacant area that can contain this object,
1735 * nearest the requested location.
1736 */
Sebastián Francof9a6ac22022-11-15 22:56:37 +00001737 public int[] findNearestAreaIgnoreOccupied(int pixelX, int pixelY, int spanX, int spanY,
1738 int[] result) {
Sebastian Francob57c0b22022-06-28 13:54:35 -07001739 return findNearestArea(pixelX, pixelY, spanX, spanY, spanX, spanY, true, result, null);
Adam Cohendf035382011-04-11 17:22:04 -07001740 }
1741
Michael Jurka0280c3b2010-09-17 15:00:07 -07001742 boolean existsEmptyCell() {
1743 return findCellForSpan(null, 1, 1);
1744 }
1745
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001746 /**
Michael Jurka0280c3b2010-09-17 15:00:07 -07001747 * Finds the upper-left coordinate of the first rectangle in the grid that can
1748 * hold a cell of the specified dimensions. If intersectX and intersectY are not -1,
1749 * then this method will only return coordinates for rectangles that contain the cell
1750 * (intersectX, intersectY)
1751 *
1752 * @param cellXY The array that will contain the position of a vacant cell if such a cell
1753 * can be found.
1754 * @param spanX The horizontal span of the cell we want to find.
1755 * @param spanY The vertical span of the cell we want to find.
1756 *
1757 * @return True if a vacant cell of the specified dimension was found, false otherwise.
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001758 */
Hyunyoung Song3f471442015-04-08 19:01:34 -07001759 public boolean findCellForSpan(int[] cellXY, int spanX, int spanY) {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001760 if (cellXY == null) {
1761 cellXY = new int[2];
Michael Jurka0280c3b2010-09-17 15:00:07 -07001762 }
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001763 return mOccupied.findVacantCell(cellXY, spanX, spanY);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001764 }
1765
1766 /**
Winson Chungc07918d2011-07-01 15:35:26 -07001767 * A drag event has begun over this layout.
1768 * It may have begun over this layout (in which case onDragChild is called first),
1769 * or it may have begun on another layout.
1770 */
1771 void onDragEnter() {
Winson Chungc07918d2011-07-01 15:35:26 -07001772 mDragging = true;
Sebastian Franco5aa71ce2022-12-14 12:13:19 -06001773 mPreviousSolution = null;
Winson Chungc07918d2011-07-01 15:35:26 -07001774 }
1775
1776 /**
Michael Jurka0280c3b2010-09-17 15:00:07 -07001777 * Called when drag has left this CellLayout or has been completed (successfully or not)
1778 */
1779 void onDragExit() {
Joe Onorato4be866d2010-10-10 11:26:02 -07001780 // This can actually be called when we aren't in a drag, e.g. when adding a new
1781 // item to this layout via the customize drawer.
1782 // Guard against that case.
1783 if (mDragging) {
1784 mDragging = false;
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001785 }
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -07001786
1787 // Invalidate the drag data
Sebastian Franco5aa71ce2022-12-14 12:13:19 -06001788 mPreviousSolution = null;
Adam Cohend41fbf52012-02-16 23:53:59 -08001789 mDragCell[0] = mDragCell[1] = -1;
Adam Cohenf7ca3b42021-02-22 11:03:58 -08001790 mDragCellSpan[0] = mDragCellSpan[1] = -1;
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -07001791 mDragOutlineAnims[mDragOutlineCurrent].animateOut();
1792 mDragOutlineCurrent = (mDragOutlineCurrent + 1) % mDragOutlineAnims.length;
Adam Cohen19f37922012-03-21 11:59:11 -07001793 revertTempState();
Michael Jurka33945b22010-12-21 18:19:38 -08001794 setIsDragOverlapping(false);
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001795 }
1796
1797 /**
Winson Chungaafa03c2010-06-11 17:34:16 -07001798 * Mark a child as having been dropped.
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001799 * At the beginning of the drag operation, the child may have been on another
Patrick Dubroyce34a972010-10-19 10:34:32 -07001800 * screen, but it is re-parented before this method is called.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001801 *
1802 * @param child The child that is being dropped
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001803 */
Adam Cohen716b51e2011-06-30 12:09:54 -07001804 void onDropChild(View child) {
Romain Guyd94533d2009-08-17 10:01:15 -07001805 if (child != null) {
Sebastian Francod4682992022-10-05 13:03:09 -05001806 CellLayoutLayoutParams
1807 lp = (CellLayoutLayoutParams) child.getLayoutParams();
Romain Guy84f296c2009-11-04 15:00:44 -08001808 lp.dropped = true;
Romain Guyd94533d2009-08-17 10:01:15 -07001809 child.requestLayout();
Tony Wickham1cdb6d02015-09-17 11:08:27 -07001810 markCellsAsOccupiedForView(child);
Romain Guyd94533d2009-08-17 10:01:15 -07001811 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001812 }
1813
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001814 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001815 * Computes a bounding rectangle for a range of cells
Winson Chungaafa03c2010-06-11 17:34:16 -07001816 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001817 * @param cellX X coordinate of upper left corner expressed as a cell position
1818 * @param cellY Y coordinate of upper left corner expressed as a cell position
Winson Chungaafa03c2010-06-11 17:34:16 -07001819 * @param cellHSpan Width in cells
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001820 * @param cellVSpan Height in cells
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001821 * @param resultRect Rect into which to put the results
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001822 */
Adam Cohend41fbf52012-02-16 23:53:59 -08001823 public void cellToRect(int cellX, int cellY, int cellHSpan, int cellVSpan, Rect resultRect) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001824 final int cellWidth = mCellWidth;
1825 final int cellHeight = mCellHeight;
Winson Chungaafa03c2010-06-11 17:34:16 -07001826
Pierre Barbier de Reuille1b8bbb62021-05-19 22:45:16 +01001827 // We observe a shift of 1 pixel on the x coordinate compared to the actual cell coordinates
1828 final int hStartPadding = getPaddingLeft()
1829 + (int) Math.ceil(getUnusedHorizontalSpace() / 2f);
Winson Chung4b825dcd2011-06-19 12:41:22 -07001830 final int vStartPadding = getPaddingTop();
Winson Chungaafa03c2010-06-11 17:34:16 -07001831
Thales Lima78d00ad2021-09-30 11:29:06 +01001832 int x = hStartPadding + (cellX * mBorderSpace.x) + (cellX * cellWidth);
1833 int y = vStartPadding + (cellY * mBorderSpace.y) + (cellY * cellHeight);
Jon Miranda228877d2021-02-09 11:05:00 -05001834
Thales Lima78d00ad2021-09-30 11:29:06 +01001835 int width = cellHSpan * cellWidth + ((cellHSpan - 1) * mBorderSpace.x);
1836 int height = cellVSpan * cellHeight + ((cellVSpan - 1) * mBorderSpace.y);
Winson Chungaafa03c2010-06-11 17:34:16 -07001837
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001838 resultRect.set(x, y, x + width, y + height);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001839 }
Winson Chungaafa03c2010-06-11 17:34:16 -07001840
Adam Cohend4844c32011-02-18 19:25:06 -08001841 public void markCellsAsOccupiedForView(View view) {
Sebastian Francof153d912022-04-22 16:15:27 -05001842 if (view instanceof LauncherAppWidgetHostView
1843 && view.getTag() instanceof LauncherAppWidgetInfo) {
1844 LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) view.getTag();
Sunny Goyal669b71f2023-01-27 14:37:07 -08001845 CellPos pos = mActivity.getCellPosMapper().mapModelToPresenter(info);
1846 mOccupied.markCells(pos.cellX, pos.cellY, info.spanX, info.spanY, true);
Sebastian Francof153d912022-04-22 16:15:27 -05001847 return;
1848 }
Michael Jurkaa52570f2012-03-20 03:18:20 -07001849 if (view == null || view.getParent() != mShortcutsAndWidgets) return;
Sebastian Francod4682992022-10-05 13:03:09 -05001850 CellLayoutLayoutParams
1851 lp = (CellLayoutLayoutParams) view.getLayoutParams();
Sebastian Franco877088e2023-01-03 15:16:22 -07001852 mOccupied.markCells(lp.getCellX(), lp.getCellY(), lp.cellHSpan, lp.cellVSpan, true);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001853 }
1854
Adam Cohend4844c32011-02-18 19:25:06 -08001855 public void markCellsAsUnoccupiedForView(View view) {
Sebastian Francof153d912022-04-22 16:15:27 -05001856 if (view instanceof LauncherAppWidgetHostView
1857 && view.getTag() instanceof LauncherAppWidgetInfo) {
1858 LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) view.getTag();
Sunny Goyal669b71f2023-01-27 14:37:07 -08001859 CellPos pos = mActivity.getCellPosMapper().mapModelToPresenter(info);
1860 mOccupied.markCells(pos.cellX, pos.cellY, info.spanX, info.spanY, false);
Sebastian Francof153d912022-04-22 16:15:27 -05001861 return;
1862 }
Michael Jurkaa52570f2012-03-20 03:18:20 -07001863 if (view == null || view.getParent() != mShortcutsAndWidgets) return;
Sebastian Francod4682992022-10-05 13:03:09 -05001864 CellLayoutLayoutParams
1865 lp = (CellLayoutLayoutParams) view.getLayoutParams();
Sebastian Franco877088e2023-01-03 15:16:22 -07001866 mOccupied.markCells(lp.getCellX(), lp.getCellY(), lp.cellHSpan, lp.cellVSpan, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001867 }
1868
Adam Cohen2801caf2011-05-13 20:57:39 -07001869 public int getDesiredWidth() {
Jon Miranda228877d2021-02-09 11:05:00 -05001870 return getPaddingLeft() + getPaddingRight() + (mCountX * mCellWidth)
Thales Lima78d00ad2021-09-30 11:29:06 +01001871 + ((mCountX - 1) * mBorderSpace.x);
Adam Cohen2801caf2011-05-13 20:57:39 -07001872 }
1873
1874 public int getDesiredHeight() {
Jon Miranda228877d2021-02-09 11:05:00 -05001875 return getPaddingTop() + getPaddingBottom() + (mCountY * mCellHeight)
Thales Lima78d00ad2021-09-30 11:29:06 +01001876 + ((mCountY - 1) * mBorderSpace.y);
Adam Cohen2801caf2011-05-13 20:57:39 -07001877 }
1878
Michael Jurka66d72172011-04-12 16:29:25 -07001879 public boolean isOccupied(int x, int y) {
Schneider Victor-tulias750c5af2023-07-26 10:16:04 -07001880 if (x >= 0 && x < mCountX && y >= 0 && y < mCountY) {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001881 return mOccupied.cells[x][y];
Schneider Victor-tulias750c5af2023-07-26 10:16:04 -07001882 }
1883 if (BuildConfig.IS_STUDIO_BUILD) {
Michael Jurka66d72172011-04-12 16:29:25 -07001884 throw new RuntimeException("Position exceeds the bound of this CellLayout");
1885 }
Schneider Victor-tulias750c5af2023-07-26 10:16:04 -07001886 return true;
Michael Jurka66d72172011-04-12 16:29:25 -07001887 }
1888
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001889 @Override
1890 public ViewGroup.LayoutParams generateLayoutParams(AttributeSet attrs) {
Sebastian Francod4682992022-10-05 13:03:09 -05001891 return new CellLayoutLayoutParams(getContext(), attrs);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001892 }
1893
1894 @Override
1895 protected boolean checkLayoutParams(ViewGroup.LayoutParams p) {
Sebastian Francod4682992022-10-05 13:03:09 -05001896 return p instanceof CellLayoutLayoutParams;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001897 }
1898
1899 @Override
1900 protected ViewGroup.LayoutParams generateLayoutParams(ViewGroup.LayoutParams p) {
Sebastian Francod4682992022-10-05 13:03:09 -05001901 return new CellLayoutLayoutParams(p);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001902 }
1903
Samuel Fufa1e2d0042019-11-18 17:12:46 -08001904 /**
Tony Wickham86930612015-09-09 13:50:40 -07001905 * Returns whether an item can be placed in this CellLayout (after rearranging and/or resizing
1906 * if necessary).
1907 */
1908 public boolean hasReorderSolution(ItemInfo itemInfo) {
1909 int[] cellPoint = new int[2];
1910 // Check for a solution starting at every cell.
1911 for (int cellX = 0; cellX < getCountX(); cellX++) {
1912 for (int cellY = 0; cellY < getCountY(); cellY++) {
1913 cellToPoint(cellX, cellY, cellPoint);
1914 if (findReorderSolution(cellPoint[0], cellPoint[1], itemInfo.minSpanX,
1915 itemInfo.minSpanY, itemInfo.spanX, itemInfo.spanY, mDirectionVector, null,
Charlie Anderson46122392024-01-18 17:23:46 -05001916 true).isSolution) {
Tony Wickham86930612015-09-09 13:50:40 -07001917 return true;
1918 }
1919 }
1920 }
1921 return false;
1922 }
1923
Samuel Fufaa4211432020-02-25 18:47:54 -08001924 /**
1925 * Finds solution to accept hotseat migration to cell layout. commits solution if commitConfig
1926 */
1927 public boolean makeSpaceForHotseatMigration(boolean commitConfig) {
Samuel Fufaa4211432020-02-25 18:47:54 -08001928 int[] cellPoint = new int[2];
1929 int[] directionVector = new int[]{0, -1};
1930 cellToPoint(0, mCountY, cellPoint);
Charlie Anderson46122392024-01-18 17:23:46 -05001931 ItemConfiguration configuration = findReorderSolution(
1932 cellPoint[0] /* pixelX */,
1933 cellPoint[1] /* pixelY */,
1934 mCountX /* minSpanX */,
1935 1 /* minSpanY */,
1936 mCountX /* spanX */,
1937 1 /* spanY */,
1938 directionVector /* direction */,
1939 null /* dragView */,
1940 false /* decX */
1941 );
1942 if (configuration.isSolution) {
Samuel Fufaa4211432020-02-25 18:47:54 -08001943 if (commitConfig) {
1944 copySolutionToTempState(configuration, null);
Sunny Goyal711c5962021-06-23 12:36:18 -07001945 commitTempPlacement(null);
Samuel Fufa82bbdac2020-03-09 18:24:47 -07001946 // undo marking cells occupied since there is actually nothing being placed yet.
1947 mOccupied.markCells(0, mCountY - 1, mCountX, 1, false);
Samuel Fufaa4211432020-02-25 18:47:54 -08001948 }
1949 return true;
1950 }
1951 return false;
1952 }
1953
Samuel Fufa82bbdac2020-03-09 18:24:47 -07001954 /**
1955 * returns a copy of cell layout's grid occupancy
1956 */
1957 public GridOccupancy cloneGridOccupancy() {
1958 GridOccupancy occupancy = new GridOccupancy(mCountX, mCountY);
1959 mOccupied.copyTo(occupancy);
1960 return occupancy;
1961 }
1962
Sunny Goyal9ca9c132015-04-29 14:57:22 -07001963 public boolean isRegionVacant(int x, int y, int spanX, int spanY) {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001964 return mOccupied.isRegionVacant(x, y, spanX, spanY);
Sunny Goyal9ca9c132015-04-29 14:57:22 -07001965 }
Sebastian Franco9ea36d42023-09-21 13:56:42 -07001966
1967 public void setSpaceBetweenCellLayoutsPx(@Px int spaceBetweenCellLayoutsPx) {
1968 mSpaceBetweenCellLayoutsPx = spaceBetweenCellLayoutsPx;
1969 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001970}