blob: 72758f2e6c3146d32013ea51ff2f5b1622648a60 [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) {
Sunny Goyal9b180102020-03-11 10:02:29 -0700332 ViewCompat.setAccessibilityDelegate(this, delegate);
333
Adam Cohen6e7c37a2020-06-25 19:22:37 -0700334 mTouchHelper = delegate;
335 int accessibilityFlag = mTouchHelper != null
Sunny Goyal9b180102020-03-11 10:02:29 -0700336 ? IMPORTANT_FOR_ACCESSIBILITY_YES : IMPORTANT_FOR_ACCESSIBILITY_NO;
337 setImportantForAccessibility(accessibilityFlag);
338 getShortcutsAndWidgets().setImportantForAccessibility(accessibilityFlag);
Sunny Goyal384b5782021-02-09 22:50:02 -0800339 // ExploreByTouchHelper sets focusability. Clear it when the delegate is cleared.
340 setFocusable(delegate != null);
Adam Cohenc9735cf2015-01-23 16:11:55 -0800341 // Invalidate the accessibility hierarchy
342 if (getParent() != null) {
343 getParent().notifySubtreeAccessibilityStateChanged(
344 this, this, AccessibilityEvent.CONTENT_CHANGE_TYPE_SUBTREE);
345 }
346 }
347
Sunny Goyala4647b62021-02-02 13:45:34 -0800348 /**
349 * Returns the currently set accessibility delegate
350 */
351 public DragAndDropAccessibilityDelegate getDragAndDropAccessibilityDelegate() {
352 return mTouchHelper;
353 }
354
Adam Cohenc9735cf2015-01-23 16:11:55 -0800355 @Override
Adam Cohen6e7c37a2020-06-25 19:22:37 -0700356 public boolean dispatchHoverEvent(MotionEvent event) {
357 // Always attempt to dispatch hover events to accessibility first.
358 if (mTouchHelper != null && mTouchHelper.dispatchHoverEvent(event)) {
359 return true;
360 }
361 return super.dispatchHoverEvent(event);
362 }
363
364 @Override
Adam Cohenc9735cf2015-01-23 16:11:55 -0800365 public boolean onInterceptTouchEvent(MotionEvent ev) {
Winson Chungf9935182020-10-23 09:26:44 -0700366 return mTouchHelper != null
367 || (mInterceptTouchListener != null && mInterceptTouchListener.onTouch(this, ev));
Adam Cohenc9735cf2015-01-23 16:11:55 -0800368 }
369
Chris Craik01f2d7f2013-10-01 14:41:56 -0700370 public void enableHardwareLayer(boolean hasLayer) {
371 mShortcutsAndWidgets.setLayerType(hasLayer ? LAYER_TYPE_HARDWARE : LAYER_TYPE_NONE, sPaint);
Michael Jurkad51f33a2012-06-28 15:35:26 -0700372 }
373
vadimt04f356f2019-02-14 18:46:36 -0800374 public boolean isHardwareLayerEnabled() {
375 return mShortcutsAndWidgets.getLayerType() == LAYER_TYPE_HARDWARE;
376 }
377
Thales Lima8cd020b2022-03-15 20:15:14 +0000378 /**
379 * Change sizes of cells
380 *
381 * @param width the new width of the cells
382 * @param height the new height of the cells
383 */
Winson Chung5f8afe62013-08-12 16:19:28 -0700384 public void setCellDimensions(int width, int height) {
Winson Chung11a1a532013-09-13 11:14:45 -0700385 mFixedCellWidth = mCellWidth = width;
386 mFixedCellHeight = mCellHeight = height;
Jon Miranda228877d2021-02-09 11:05:00 -0500387 mShortcutsAndWidgets.setCellDimensions(mCellWidth, mCellHeight, mCountX, mCountY,
Thales Lima78d00ad2021-09-30 11:29:06 +0100388 mBorderSpace);
Winson Chung5f8afe62013-08-12 16:19:28 -0700389 }
390
Thales Lima8cd020b2022-03-15 20:15:14 +0000391 private void resetCellSizeInternal(DeviceProfile deviceProfile) {
392 switch (mContainerType) {
393 case FOLDER:
Jordan Silva637f4eb2023-06-13 11:21:53 +0100394 mBorderSpace = new Point(deviceProfile.folderCellLayoutBorderSpacePx);
Thales Lima8cd020b2022-03-15 20:15:14 +0000395 break;
396 case HOTSEAT:
397 mBorderSpace = new Point(deviceProfile.hotseatBorderSpace,
398 deviceProfile.hotseatBorderSpace);
399 break;
400 case WORKSPACE:
401 default:
402 mBorderSpace = new Point(deviceProfile.cellLayoutBorderSpacePx);
403 break;
404 }
405
406 mCellWidth = mCellHeight = -1;
407 mFixedCellWidth = mFixedCellHeight = -1;
408 }
409
410 /**
411 * Reset the cell sizes and border space
412 */
413 public void resetCellSize(DeviceProfile deviceProfile) {
414 resetCellSizeInternal(deviceProfile);
415 requestLayout();
416 }
417
Adam Cohen2801caf2011-05-13 20:57:39 -0700418 public void setGridSize(int x, int y) {
419 mCountX = x;
420 mCountY = y;
Sunny Goyalff4ba2d2016-04-02 14:12:34 -0700421 mOccupied = new GridOccupancy(mCountX, mCountY);
422 mTmpOccupied = new GridOccupancy(mCountX, mCountY);
Jon Miranda228877d2021-02-09 11:05:00 -0500423 mShortcutsAndWidgets.setCellDimensions(mCellWidth, mCellHeight, mCountX, mCountY,
Thales Lima78d00ad2021-09-30 11:29:06 +0100424 mBorderSpace);
Adam Cohen76fc0852011-06-17 13:26:23 -0700425 requestLayout();
Adam Cohen2801caf2011-05-13 20:57:39 -0700426 }
427
Adam Cohen2374abf2013-04-16 14:56:57 -0700428 // Set whether or not to invert the layout horizontally if the layout is in RTL mode.
429 public void setInvertIfRtl(boolean invert) {
430 mShortcutsAndWidgets.setInvertIfRtl(invert);
431 }
432
Adam Cohen917e3882013-10-31 15:03:35 -0700433 public void setDropPending(boolean pending) {
434 mDropPending = pending;
435 }
436
437 public boolean isDropPending() {
438 return mDropPending;
439 }
440
Adam Cohenc50438c2014-08-19 17:43:05 -0700441 void setIsDragOverlapping(boolean isDragOverlapping) {
442 if (mIsDragOverlapping != isDragOverlapping) {
443 mIsDragOverlapping = isDragOverlapping;
Sunny Goyalf5440cb2016-12-14 15:13:00 -0800444 mBackground.setState(mIsDragOverlapping
445 ? BACKGROUND_STATE_ACTIVE : BACKGROUND_STATE_DEFAULT);
Adam Cohenc50438c2014-08-19 17:43:05 -0700446 invalidate();
447 }
448 }
449
Sunny Goyale2fd14b2015-08-27 17:45:46 -0700450 @Override
451 protected void dispatchSaveInstanceState(SparseArray<Parcelable> container) {
Sunny Goyal7ce471b2017-08-02 03:37:39 -0700452 ParcelableSparseArray jail = getJailedArray(container);
453 super.dispatchSaveInstanceState(jail);
454 container.put(R.id.cell_layout_jail_id, jail);
Sunny Goyale2fd14b2015-08-27 17:45:46 -0700455 }
456
457 @Override
458 protected void dispatchRestoreInstanceState(SparseArray<Parcelable> container) {
Sunny Goyal7ce471b2017-08-02 03:37:39 -0700459 super.dispatchRestoreInstanceState(getJailedArray(container));
Sunny Goyale2fd14b2015-08-27 17:45:46 -0700460 }
461
Sunny Goyal7ce471b2017-08-02 03:37:39 -0700462 /**
463 * Wrap the SparseArray in another Parcelable so that the item ids do not conflict with our
464 * our internal resource ids
465 */
Sunny Goyale2fd14b2015-08-27 17:45:46 -0700466 private ParcelableSparseArray getJailedArray(SparseArray<Parcelable> container) {
467 final Parcelable parcelable = container.get(R.id.cell_layout_jail_id);
468 return parcelable instanceof ParcelableSparseArray ?
469 (ParcelableSparseArray) parcelable : new ParcelableSparseArray();
470 }
471
Tony Wickham0f97b782015-12-02 17:55:07 -0800472 public boolean getIsDragOverlapping() {
473 return mIsDragOverlapping;
474 }
475
Jeff Sharkey83f111d2009-04-20 21:03:13 -0700476 @Override
Patrick Dubroy1262e362010-10-06 15:49:50 -0700477 protected void onDraw(Canvas canvas) {
Michael Jurka3e7c7632010-10-02 16:01:03 -0700478 // When we're large, we are either drawn in a "hover" state (ie when dragging an item to
479 // a neighboring page) or with just a normal background (if backgroundAlpha > 0.0f)
480 // When we're small, we are either drawn normally or in the "accepts drops" state (during
481 // a drag). However, we also drag the mini hover background *over* one of those two
482 // backgrounds
Sunny Goyalaeb16432017-10-16 11:46:41 -0700483 if (mBackground.getAlpha() > 0) {
Sunny Goyal2805e632015-05-20 15:35:32 -0700484 mBackground.draw(canvas);
Michael Jurkaa63c4522010-08-19 13:52:27 -0700485 }
Romain Guya6abce82009-11-10 02:54:41 -0800486
Adam Cohen482ed822012-03-02 14:15:13 -0800487 if (DEBUG_VISUALIZE_OCCUPIED) {
Tony Wickham0ac045f2021-11-03 13:17:02 -0700488 Rect cellBounds = new Rect();
489 // Will contain the bounds of the cell including spacing between cells.
490 Rect cellBoundsWithSpacing = new Rect();
Tony Wickham12784902021-11-03 14:02:10 -0700491 int[] targetCell = new int[2];
Tony Wickham0ac045f2021-11-03 13:17:02 -0700492 int[] cellCenter = new int[2];
493 Paint debugPaint = new Paint();
494 debugPaint.setStrokeWidth(Utilities.dpToPx(1));
495 for (int x = 0; x < mCountX; x++) {
496 for (int y = 0; y < mCountY; y++) {
497 if (!mOccupied.cells[x][y]) {
498 continue;
Adam Cohen482ed822012-03-02 14:15:13 -0800499 }
Tony Wickham12784902021-11-03 14:02:10 -0700500 targetCell[0] = x;
501 targetCell[1] = y;
Tony Wickham0ac045f2021-11-03 13:17:02 -0700502
Tony Wickham12784902021-11-03 14:02:10 -0700503 boolean canCreateFolder = canCreateFolder(getChildAt(x, y));
Tony Wickham0ac045f2021-11-03 13:17:02 -0700504 cellToRect(x, y, 1, 1, cellBounds);
505 cellBoundsWithSpacing.set(cellBounds);
506 cellBoundsWithSpacing.inset(-mBorderSpace.x / 2, -mBorderSpace.y / 2);
Tony Wickham3cfa5ed2021-11-03 13:20:43 -0700507 getWorkspaceCellVisualCenter(x, y, cellCenter);
Tony Wickham0ac045f2021-11-03 13:17:02 -0700508
509 canvas.save();
510 canvas.clipRect(cellBoundsWithSpacing);
511
512 // Draw reorder drag target.
513 debugPaint.setColor(Color.RED);
Sebastian Franco6e1024e2022-07-29 13:46:49 -0700514 canvas.drawCircle(cellCenter[0], cellCenter[1],
515 getReorderRadius(targetCell, 1, 1), debugPaint);
Tony Wickham0ac045f2021-11-03 13:17:02 -0700516
517 // Draw folder creation drag target.
518 if (canCreateFolder) {
519 debugPaint.setColor(Color.GREEN);
520 canvas.drawCircle(cellCenter[0], cellCenter[1],
Tony Wickham12784902021-11-03 14:02:10 -0700521 getFolderCreationRadius(targetCell), debugPaint);
Tony Wickham0ac045f2021-11-03 13:17:02 -0700522 }
523
524 canvas.restore();
Adam Cohen482ed822012-03-02 14:15:13 -0800525 }
526 }
527 }
528
Samuel Fufa1e2d0042019-11-18 17:12:46 -0800529 for (int i = 0; i < mDelegatedCellDrawings.size(); i++) {
530 DelegatedCellDrawing cellDrawing = mDelegatedCellDrawings.get(i);
531 cellToPoint(cellDrawing.mDelegateCellX, cellDrawing.mDelegateCellY, mTempLocation);
Adam Cohenefca0272016-02-24 19:19:06 -0800532 canvas.save();
533 canvas.translate(mTempLocation[0], mTempLocation[1]);
Samuel Fufa1e2d0042019-11-18 17:12:46 -0800534 cellDrawing.drawUnderItem(canvas);
Adam Cohenefca0272016-02-24 19:19:06 -0800535 canvas.restore();
Adam Cohen69ce2e52011-07-03 19:25:21 -0700536 }
Adam Cohenc51934b2011-07-26 21:07:43 -0700537
Samuel Fufa1e2d0042019-11-18 17:12:46 -0800538 if (mFolderLeaveBehind.mDelegateCellX >= 0 && mFolderLeaveBehind.mDelegateCellY >= 0) {
539 cellToPoint(mFolderLeaveBehind.mDelegateCellX,
540 mFolderLeaveBehind.mDelegateCellY, mTempLocation);
Adam Cohenefca0272016-02-24 19:19:06 -0800541 canvas.save();
542 canvas.translate(mTempLocation[0], mTempLocation[1]);
Tony Wickhamec6fd6f2023-03-11 02:08:57 +0000543 mFolderLeaveBehind.drawLeaveBehind(canvas, FOLDER_LEAVE_BEHIND_COLOR);
Adam Cohenefca0272016-02-24 19:19:06 -0800544 canvas.restore();
Adam Cohenc51934b2011-07-26 21:07:43 -0700545 }
Adam Cohen65086992020-02-19 08:40:49 -0800546
Adam Cohenf7ca3b42021-02-22 11:03:58 -0800547 if (mVisualizeCells || mVisualizeDropLocation) {
Adam Cohen65086992020-02-19 08:40:49 -0800548 visualizeGrid(canvas);
549 }
550 }
551
Adam Cohenf7ca3b42021-02-22 11:03:58 -0800552 /**
Tony Wickham12784902021-11-03 14:02:10 -0700553 * Returns whether dropping an icon on the given View can create (or add to) a folder.
554 */
555 private boolean canCreateFolder(View child) {
556 return child instanceof DraggableView
557 && ((DraggableView) child).getViewType() == DRAGGABLE_ICON;
558 }
559
560 /**
Adam Cohenf7ca3b42021-02-22 11:03:58 -0800561 * Indicates the progress of the Workspace entering the SpringLoaded state; allows the
562 * CellLayout to update various visuals for this state.
563 *
564 * @param progress
565 */
566 public void setSpringLoadedProgress(float progress) {
567 if (Float.compare(progress, mSpringLoadedProgress) != 0) {
568 mSpringLoadedProgress = progress;
fbarone74256b2023-04-10 14:50:31 -0700569 updateBgAlpha();
Adam Cohenf7ca3b42021-02-22 11:03:58 -0800570 setGridAlpha(progress);
571 }
572 }
573
574 /**
575 * See setSpringLoadedProgress
576 * @return progress
577 */
578 public float getSpringLoadedProgress() {
579 return mSpringLoadedProgress;
580 }
581
Sebastian Franco09589322022-11-02 15:25:58 -0700582 protected void updateBgAlpha() {
Sebastian Franco2986e0b2024-01-25 10:23:39 -0800583 mBackground.setAlpha((int) (mSpringLoadedProgress * 255));
Adam Cohenf7ca3b42021-02-22 11:03:58 -0800584 }
585
586 /**
587 * Set the progress of this page's scroll
588 *
589 * @param progress 0 if the screen is centered, +/-1 if it is to the right / left respectively
590 */
591 public void setScrollProgress(float progress) {
592 if (Float.compare(Math.abs(progress), mScrollProgress) != 0) {
593 mScrollProgress = Math.abs(progress);
fbarone74256b2023-04-10 14:50:31 -0700594 updateBgAlpha();
Adam Cohenf7ca3b42021-02-22 11:03:58 -0800595 }
596 }
597
598 private void setGridAlpha(float gridAlpha) {
599 if (Float.compare(gridAlpha, mGridAlpha) != 0) {
600 mGridAlpha = gridAlpha;
601 invalidate();
602 }
603 }
604
Adam Cohen65086992020-02-19 08:40:49 -0800605 protected void visualizeGrid(Canvas canvas) {
Adam Cohen0c4d2782021-04-29 15:56:13 -0700606 DeviceProfile dp = mActivity.getDeviceProfile();
Alex Chau51da2192022-05-20 13:32:10 +0100607 int paddingX = Math.min((mCellWidth - dp.iconSizePx) / 2, dp.gridVisualizationPaddingX);
608 int paddingY = Math.min((mCellHeight - dp.iconSizePx) / 2, dp.gridVisualizationPaddingY);
Adam Cohen65086992020-02-19 08:40:49 -0800609
Adam Cohenf7ca3b42021-02-22 11:03:58 -0800610 mVisualizeGridPaint.setStrokeWidth(8);
Adam Cohen65086992020-02-19 08:40:49 -0800611
Sebastian Franco0dd5db82023-10-13 11:09:21 -0700612 // This is used for debugging purposes only
Adam Cohenf7ca3b42021-02-22 11:03:58 -0800613 if (mVisualizeCells) {
Sebastian Franco0dd5db82023-10-13 11:09:21 -0700614 int paintAlpha = (int) (120 * mGridAlpha);
615 mVisualizeGridPaint.setColor(ColorUtils.setAlphaComponent(mGridColor, paintAlpha));
Adam Cohenf7ca3b42021-02-22 11:03:58 -0800616 for (int i = 0; i < mCountX; i++) {
617 for (int j = 0; j < mCountY; j++) {
Sebastian Franco0dd5db82023-10-13 11:09:21 -0700618 cellToRect(i, j, 1, 1, mTempOnDrawCellToRect);
619 mVisualizeGridRect.set(mTempOnDrawCellToRect);
620 mVisualizeGridRect.inset(paddingX, paddingY);
Adam Cohenf7ca3b42021-02-22 11:03:58 -0800621 mVisualizeGridPaint.setStyle(Paint.Style.FILL);
622 canvas.drawRoundRect(mVisualizeGridRect, mGridVisualizationRoundingRadius,
623 mGridVisualizationRoundingRadius, mVisualizeGridPaint);
624 }
625 }
626 }
Adam Cohen65086992020-02-19 08:40:49 -0800627
fbarone74256b2023-04-10 14:50:31 -0700628 if (mVisualizeDropLocation) {
Adam Cohenf7ca3b42021-02-22 11:03:58 -0800629 for (int i = 0; i < mDragOutlines.length; i++) {
630 final float alpha = mDragOutlineAlphas[i];
631 if (alpha <= 0) continue;
Sebastian Franco0dd5db82023-10-13 11:09:21 -0700632 CellLayoutLayoutParams params = mDragOutlines[i];
633 cellToRect(params.getCellX(), params.getCellY(), params.cellHSpan, params.cellVSpan,
634 mTempOnDrawCellToRect);
635 mVisualizeGridRect.set(mTempOnDrawCellToRect);
636 mVisualizeGridRect.inset(paddingX, paddingY);
Adam Cohen65086992020-02-19 08:40:49 -0800637
Adam Cohenf7ca3b42021-02-22 11:03:58 -0800638 mVisualizeGridPaint.setAlpha(255);
Adam Cohen65086992020-02-19 08:40:49 -0800639 mVisualizeGridPaint.setStyle(Paint.Style.STROKE);
Adam Cohenf7ca3b42021-02-22 11:03:58 -0800640 mVisualizeGridPaint.setColor(Color.argb((int) (alpha),
641 Color.red(mGridColor), Color.green(mGridColor), Color.blue(mGridColor)));
Adam Cohen65086992020-02-19 08:40:49 -0800642
Sebastian Franco9ea36d42023-09-21 13:56:42 -0700643 canvas.save();
644 canvas.translate(getMarginForGivenCellParams(params), 0);
Adam Cohenf7ca3b42021-02-22 11:03:58 -0800645 canvas.drawRoundRect(mVisualizeGridRect, mGridVisualizationRoundingRadius,
646 mGridVisualizationRoundingRadius, mVisualizeGridPaint);
Sebastian Franco9ea36d42023-09-21 13:56:42 -0700647 canvas.restore();
Adam Cohen65086992020-02-19 08:40:49 -0800648 }
649 }
Adam Cohen69ce2e52011-07-03 19:25:21 -0700650 }
651
Sebastian Franco9ea36d42023-09-21 13:56:42 -0700652 protected float getMarginForGivenCellParams(CellLayoutLayoutParams params) {
653 return 0;
654 }
655
Adam Cohenefca0272016-02-24 19:19:06 -0800656 @Override
657 protected void dispatchDraw(Canvas canvas) {
658 super.dispatchDraw(canvas);
659
Samuel Fufa1e2d0042019-11-18 17:12:46 -0800660 for (int i = 0; i < mDelegatedCellDrawings.size(); i++) {
661 DelegatedCellDrawing bg = mDelegatedCellDrawings.get(i);
662 cellToPoint(bg.mDelegateCellX, bg.mDelegateCellY, mTempLocation);
663 canvas.save();
664 canvas.translate(mTempLocation[0], mTempLocation[1]);
665 bg.drawOverItem(canvas);
666 canvas.restore();
Adam Cohenefca0272016-02-24 19:19:06 -0800667 }
Adam Cohen69ce2e52011-07-03 19:25:21 -0700668 }
669
Samuel Fufa1e2d0042019-11-18 17:12:46 -0800670 /**
671 * Add Delegated cell drawing
672 */
673 public void addDelegatedCellDrawing(DelegatedCellDrawing bg) {
674 mDelegatedCellDrawings.add(bg);
Adam Cohenefca0272016-02-24 19:19:06 -0800675 }
Samuel Fufa1e2d0042019-11-18 17:12:46 -0800676
677 /**
678 * Remove item from DelegatedCellDrawings
679 */
680 public void removeDelegatedCellDrawing(DelegatedCellDrawing bg) {
681 mDelegatedCellDrawings.remove(bg);
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700682 }
683
Adam Cohenc51934b2011-07-26 21:07:43 -0700684 public void setFolderLeaveBehindCell(int x, int y) {
Adam Cohenefca0272016-02-24 19:19:06 -0800685 View child = getChildAt(x, y);
Sunny Goyalab770a12018-11-14 15:17:26 -0800686 mFolderLeaveBehind.setup(getContext(), mActivity, null,
Adam Cohenefca0272016-02-24 19:19:06 -0800687 child.getMeasuredWidth(), child.getPaddingTop());
688
Samuel Fufa1e2d0042019-11-18 17:12:46 -0800689 mFolderLeaveBehind.mDelegateCellX = x;
690 mFolderLeaveBehind.mDelegateCellY = y;
Adam Cohenc51934b2011-07-26 21:07:43 -0700691 invalidate();
692 }
693
694 public void clearFolderLeaveBehind() {
Samuel Fufa1e2d0042019-11-18 17:12:46 -0800695 mFolderLeaveBehind.mDelegateCellX = -1;
696 mFolderLeaveBehind.mDelegateCellY = -1;
Adam Cohenc51934b2011-07-26 21:07:43 -0700697 invalidate();
698 }
699
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700700 @Override
Michael Jurkae6235dd2011-10-04 15:02:05 -0700701 public boolean shouldDelayChildPressedState() {
702 return false;
703 }
704
Adam Cohen1462de32012-07-24 22:34:36 -0700705 public void restoreInstanceState(SparseArray<Parcelable> states) {
Sunny Goyal33a152f2014-07-22 12:13:14 -0700706 try {
707 dispatchRestoreInstanceState(states);
708 } catch (IllegalArgumentException ex) {
Zak Cohen3eeb41d2020-02-14 14:15:13 -0800709 if (FeatureFlags.IS_STUDIO_BUILD) {
Sunny Goyal33a152f2014-07-22 12:13:14 -0700710 throw ex;
711 }
712 // Mismatched viewId / viewType preventing restore. Skip restore on production builds.
713 Log.e(TAG, "Ignoring an error while restoring a view instance state", ex);
714 }
Adam Cohen1462de32012-07-24 22:34:36 -0700715 }
716
Michael Jurkae6235dd2011-10-04 15:02:05 -0700717 @Override
Jeff Sharkey83f111d2009-04-20 21:03:13 -0700718 public void cancelLongPress() {
719 super.cancelLongPress();
720
721 // Cancel long press for all children
722 final int count = getChildCount();
723 for (int i = 0; i < count; i++) {
724 final View child = getChildAt(i);
725 child.cancelLongPress();
726 }
727 }
728
Michael Jurkadee05892010-07-27 10:01:56 -0700729 public void setOnInterceptTouchListener(View.OnTouchListener listener) {
730 mInterceptTouchListener = listener;
731 }
732
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800733 public int getCountX() {
Adam Cohend22015c2010-07-26 22:02:18 -0700734 return mCountX;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800735 }
736
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800737 public int getCountY() {
Adam Cohend22015c2010-07-26 22:02:18 -0700738 return mCountY;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800739 }
740
Sunny Goyalc13403c2016-11-18 23:44:48 -0800741 public boolean acceptsWidget() {
742 return mContainerType == WORKSPACE;
Sunny Goyale9b651e2015-04-24 11:44:51 -0700743 }
744
Sebastian Francod4682992022-10-05 13:03:09 -0500745 /**
746 * Adds the given view to the CellLayout
747 *
748 * @param child view to add.
749 * @param index index of the CellLayout children where to add the view.
750 * @param childId id of the view.
751 * @param params represent the logic of the view on the CellLayout.
752 * @param markCells if the occupied cells should be marked or not
753 * @return if adding the view was successful
754 */
755 public boolean addViewToCellLayout(View child, int index, int childId,
756 CellLayoutLayoutParams params, boolean markCells) {
757 final CellLayoutLayoutParams lp = params;
Winson Chungaafa03c2010-06-11 17:34:16 -0700758
Andrew Flynnde38e422012-05-08 11:22:15 -0700759 // Hotseat icons - remove text
Andrew Flynn0dca1ec2012-02-29 13:33:22 -0800760 if (child instanceof BubbleTextView) {
761 BubbleTextView bubbleChild = (BubbleTextView) child;
Jon Mirandaf1eae802017-10-04 11:23:33 -0700762 bubbleChild.setTextVisibility(mContainerType != HOTSEAT);
Andrew Flynn0dca1ec2012-02-29 13:33:22 -0800763 }
764
Sebastian Francof7654252024-01-18 11:50:50 -0800765 child.setScaleX(DEFAULT_SCALE);
766 child.setScaleY(DEFAULT_SCALE);
Adam Cohen307fe232012-08-16 17:55:58 -0700767
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800768 // Generate an id for each view, this assumes we have at most 256x256 cells
769 // per workspace screen
Sebastian Franco877088e2023-01-03 15:16:22 -0700770 if (lp.getCellX() >= 0 && lp.getCellX() <= mCountX - 1
771 && lp.getCellY() >= 0 && lp.getCellY() <= mCountY - 1) {
Winson Chungaafa03c2010-06-11 17:34:16 -0700772 // If the horizontal or vertical span is set to -1, it is taken to
773 // mean that it spans the extent of the CellLayout
Adam Cohend22015c2010-07-26 22:02:18 -0700774 if (lp.cellHSpan < 0) lp.cellHSpan = mCountX;
775 if (lp.cellVSpan < 0) lp.cellVSpan = mCountY;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800776
Winson Chungaafa03c2010-06-11 17:34:16 -0700777 child.setId(childId);
Tony Wickhama0628cc2015-10-14 15:23:04 -0700778 if (LOGD) {
779 Log.d(TAG, "Adding view to ShortcutsAndWidgetsContainer: " + child);
780 }
Sunny Goyalf7a29e82015-04-24 15:20:43 -0700781 mShortcutsAndWidgets.addView(child, index, lp);
Michael Jurkadee05892010-07-27 10:01:56 -0700782
Michael Jurkaf3ca3ab2010-10-20 17:08:24 -0700783 if (markCells) markCellsAsOccupiedForView(child);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700784
Winson Chungaafa03c2010-06-11 17:34:16 -0700785 return true;
786 }
787 return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800788 }
Michael Jurka3e7c7632010-10-02 16:01:03 -0700789
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800790 @Override
Michael Jurka0280c3b2010-09-17 15:00:07 -0700791 public void removeAllViews() {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -0700792 mOccupied.clear();
Michael Jurkaa52570f2012-03-20 03:18:20 -0700793 mShortcutsAndWidgets.removeAllViews();
Michael Jurka0280c3b2010-09-17 15:00:07 -0700794 }
795
796 @Override
797 public void removeAllViewsInLayout() {
Michael Jurkaa52570f2012-03-20 03:18:20 -0700798 if (mShortcutsAndWidgets.getChildCount() > 0) {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -0700799 mOccupied.clear();
Michael Jurkaa52570f2012-03-20 03:18:20 -0700800 mShortcutsAndWidgets.removeAllViewsInLayout();
Michael Jurka7cfc2822011-08-02 20:19:24 -0700801 }
Michael Jurka0280c3b2010-09-17 15:00:07 -0700802 }
803
804 @Override
805 public void removeView(View view) {
806 markCellsAsUnoccupiedForView(view);
Michael Jurkaa52570f2012-03-20 03:18:20 -0700807 mShortcutsAndWidgets.removeView(view);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700808 }
809
810 @Override
811 public void removeViewAt(int index) {
Michael Jurkaa52570f2012-03-20 03:18:20 -0700812 markCellsAsUnoccupiedForView(mShortcutsAndWidgets.getChildAt(index));
813 mShortcutsAndWidgets.removeViewAt(index);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700814 }
815
816 @Override
817 public void removeViewInLayout(View view) {
818 markCellsAsUnoccupiedForView(view);
Michael Jurkaa52570f2012-03-20 03:18:20 -0700819 mShortcutsAndWidgets.removeViewInLayout(view);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700820 }
821
822 @Override
823 public void removeViews(int start, int count) {
824 for (int i = start; i < start + count; i++) {
Michael Jurkaa52570f2012-03-20 03:18:20 -0700825 markCellsAsUnoccupiedForView(mShortcutsAndWidgets.getChildAt(i));
Michael Jurka0280c3b2010-09-17 15:00:07 -0700826 }
Michael Jurkaa52570f2012-03-20 03:18:20 -0700827 mShortcutsAndWidgets.removeViews(start, count);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700828 }
829
830 @Override
831 public void removeViewsInLayout(int start, int count) {
832 for (int i = start; i < start + count; i++) {
Michael Jurkaa52570f2012-03-20 03:18:20 -0700833 markCellsAsUnoccupiedForView(mShortcutsAndWidgets.getChildAt(i));
Michael Jurka0280c3b2010-09-17 15:00:07 -0700834 }
Michael Jurkaa52570f2012-03-20 03:18:20 -0700835 mShortcutsAndWidgets.removeViewsInLayout(start, count);
Michael Jurkaabded662011-03-04 12:06:57 -0800836 }
837
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700838 /**
Winson Chungaafa03c2010-06-11 17:34:16 -0700839 * Given a point, return the cell that strictly encloses that point
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800840 * @param x X coordinate of the point
841 * @param y Y coordinate of the point
842 * @param result Array of 2 ints to hold the x and y coordinate of the cell
843 */
Sunny Goyale9b651e2015-04-24 11:44:51 -0700844 public void pointToCellExact(int x, int y, int[] result) {
Winson Chung4b825dcd2011-06-19 12:41:22 -0700845 final int hStartPadding = getPaddingLeft();
846 final int vStartPadding = getPaddingTop();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800847
Sebastian Francob57c0b22022-06-28 13:54:35 -0700848 result[0] = (x - hStartPadding) / (mCellWidth + mBorderSpace.x);
849 result[1] = (y - vStartPadding) / (mCellHeight + mBorderSpace.y);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800850
Adam Cohend22015c2010-07-26 22:02:18 -0700851 final int xAxis = mCountX;
852 final int yAxis = mCountY;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800853
854 if (result[0] < 0) result[0] = 0;
855 if (result[0] >= xAxis) result[0] = xAxis - 1;
856 if (result[1] < 0) result[1] = 0;
857 if (result[1] >= yAxis) result[1] = yAxis - 1;
858 }
Winson Chungaafa03c2010-06-11 17:34:16 -0700859
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800860 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800861 * Given a cell coordinate, return the point that represents the upper left corner of that cell
Winson Chungaafa03c2010-06-11 17:34:16 -0700862 *
863 * @param cellX X coordinate of the cell
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800864 * @param cellY Y coordinate of the cell
Winson Chungaafa03c2010-06-11 17:34:16 -0700865 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800866 * @param result Array of 2 ints to hold the x and y coordinate of the point
867 */
868 void cellToPoint(int cellX, int cellY, int[] result) {
Jon Miranda228877d2021-02-09 11:05:00 -0500869 cellToRect(cellX, cellY, 1, 1, mTempRect);
870 result[0] = mTempRect.left;
871 result[1] = mTempRect.top;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800872 }
873
Adam Cohene3e27a82011-04-15 12:07:39 -0700874 /**
Adam Cohen482ed822012-03-02 14:15:13 -0800875 * Given a cell coordinate, return the point that represents the center of the cell
Adam Cohene3e27a82011-04-15 12:07:39 -0700876 *
877 * @param cellX X coordinate of the cell
878 * @param cellY Y coordinate of the cell
879 *
880 * @param result Array of 2 ints to hold the x and y coordinate of the point
881 */
882 void cellToCenterPoint(int cellX, int cellY, int[] result) {
Adam Cohen47a876d2012-03-19 13:21:41 -0700883 regionToCenterPoint(cellX, cellY, 1, 1, result);
884 }
885
886 /**
Tony Wickham0ac045f2021-11-03 13:17:02 -0700887 * Given a cell coordinate and span return the point that represents the center of the region
Adam Cohen47a876d2012-03-19 13:21:41 -0700888 *
889 * @param cellX X coordinate of the cell
890 * @param cellY Y coordinate of the cell
891 *
892 * @param result Array of 2 ints to hold the x and y coordinate of the point
893 */
Sebastian Franco45b39b52023-01-10 10:47:46 -0600894 public void regionToCenterPoint(int cellX, int cellY, int spanX, int spanY, int[] result) {
Jon Miranda228877d2021-02-09 11:05:00 -0500895 cellToRect(cellX, cellY, spanX, spanY, mTempRect);
896 result[0] = mTempRect.centerX();
897 result[1] = mTempRect.centerY();
Adam Cohen19f37922012-03-21 11:59:11 -0700898 }
899
Tony Wickham3cfa5ed2021-11-03 13:20:43 -0700900 /**
901 * Returns the distance between the given coordinate and the visual center of the given cell.
902 */
903 public float getDistanceFromWorkspaceCellVisualCenter(float x, float y, int[] cell) {
904 getWorkspaceCellVisualCenter(cell[0], cell[1], mTmpPoint);
Sunny Goyalf7a29e82015-04-24 15:20:43 -0700905 return (float) Math.hypot(x - mTmpPoint[0], y - mTmpPoint[1]);
Adam Cohen482ed822012-03-02 14:15:13 -0800906 }
907
Tony Wickham3cfa5ed2021-11-03 13:20:43 -0700908 private void getWorkspaceCellVisualCenter(int cellX, int cellY, int[] outPoint) {
909 View child = getChildAt(cellX, cellY);
910 if (child instanceof DraggableView) {
911 DraggableView draggableChild = (DraggableView) child;
912 if (draggableChild.getViewType() == DRAGGABLE_ICON) {
913 cellToPoint(cellX, cellY, outPoint);
914 draggableChild.getWorkspaceVisualDragBounds(mTempRect);
915 mTempRect.offset(outPoint[0], outPoint[1]);
916 outPoint[0] = mTempRect.centerX();
917 outPoint[1] = mTempRect.centerY();
918 return;
919 }
920 }
921 cellToCenterPoint(cellX, cellY, outPoint);
922 }
923
Tony Wickham0ac045f2021-11-03 13:17:02 -0700924 /**
925 * Returns the max distance from the center of a cell that can accept a drop to create a folder.
926 */
Tony Wickham12784902021-11-03 14:02:10 -0700927 public float getFolderCreationRadius(int[] targetCell) {
Tony Wickham0ac045f2021-11-03 13:17:02 -0700928 DeviceProfile grid = mActivity.getDeviceProfile();
Tony Wickham12784902021-11-03 14:02:10 -0700929 float iconVisibleRadius = ICON_VISIBLE_AREA_FACTOR * grid.iconSizePx / 2;
930 // Halfway between reorder radius and icon.
Sebastian Franco6e1024e2022-07-29 13:46:49 -0700931 return (getReorderRadius(targetCell, 1, 1) + iconVisibleRadius) / 2;
Tony Wickham12784902021-11-03 14:02:10 -0700932 }
933
934 /**
935 * Returns the max distance from the center of a cell that will start to reorder on drag over.
936 */
Sebastian Franco6e1024e2022-07-29 13:46:49 -0700937 public float getReorderRadius(int[] targetCell, int spanX, int spanY) {
Tony Wickham12784902021-11-03 14:02:10 -0700938 int[] centerPoint = mTmpPoint;
939 getWorkspaceCellVisualCenter(targetCell[0], targetCell[1], centerPoint);
940
941 Rect cellBoundsWithSpacing = mTempRect;
Sebastian Franco6e1024e2022-07-29 13:46:49 -0700942 cellToRect(targetCell[0], targetCell[1], spanX, spanY, cellBoundsWithSpacing);
Tony Wickham12784902021-11-03 14:02:10 -0700943 cellBoundsWithSpacing.inset(-mBorderSpace.x / 2, -mBorderSpace.y / 2);
944
Sebastian Francoc8392ea2022-10-28 16:38:37 -0700945 if (canCreateFolder(getChildAt(targetCell[0], targetCell[1])) && spanX == 1 && spanY == 1) {
Tony Wickham12784902021-11-03 14:02:10 -0700946 // Take only the circle in the smaller dimension, to ensure we don't start reordering
947 // too soon before accepting a folder drop.
948 int minRadius = centerPoint[0] - cellBoundsWithSpacing.left;
949 minRadius = Math.min(minRadius, centerPoint[1] - cellBoundsWithSpacing.top);
950 minRadius = Math.min(minRadius, cellBoundsWithSpacing.right - centerPoint[0]);
951 minRadius = Math.min(minRadius, cellBoundsWithSpacing.bottom - centerPoint[1]);
952 return minRadius;
953 }
954 // Take up the entire cell, including space between this cell and the adjacent ones.
Sebastian Francoc8392ea2022-10-28 16:38:37 -0700955 // Multiply by span to scale radius
956 return (float) Math.hypot(spanX * cellBoundsWithSpacing.width() / 2f,
957 spanY * cellBoundsWithSpacing.height() / 2f);
Tony Wickham0ac045f2021-11-03 13:17:02 -0700958 }
959
Adam Cohenf9c184a2016-01-15 16:47:43 -0800960 public int getCellWidth() {
Romain Guy84f296c2009-11-04 15:00:44 -0800961 return mCellWidth;
962 }
963
Sunny Goyal0b754e52017-08-07 07:42:45 -0700964 public int getCellHeight() {
Romain Guy84f296c2009-11-04 15:00:44 -0800965 return mCellHeight;
966 }
967
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700968 public void setFixedSize(int width, int height) {
969 mFixedWidth = width;
970 mFixedHeight = height;
971 }
972
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800973 @Override
974 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800975 int widthSpecMode = MeasureSpec.getMode(widthMeasureSpec);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800976 int heightSpecMode = MeasureSpec.getMode(heightMeasureSpec);
Winson Chung5f8afe62013-08-12 16:19:28 -0700977 int widthSize = MeasureSpec.getSize(widthMeasureSpec);
978 int heightSize = MeasureSpec.getSize(heightMeasureSpec);
Winson Chung2d75f122013-09-23 16:53:31 -0700979 int childWidthSize = widthSize - (getPaddingLeft() + getPaddingRight());
980 int childHeightSize = heightSize - (getPaddingTop() + getPaddingBottom());
Sunny Goyalae6e3182019-04-30 12:04:37 -0700981
Winson Chung11a1a532013-09-13 11:14:45 -0700982 if (mFixedCellWidth < 0 || mFixedCellHeight < 0) {
Thales Lima78d00ad2021-09-30 11:29:06 +0100983 int cw = DeviceProfile.calculateCellWidth(childWidthSize, mBorderSpace.x,
Jon Miranda228877d2021-02-09 11:05:00 -0500984 mCountX);
Thales Lima78d00ad2021-09-30 11:29:06 +0100985 int ch = DeviceProfile.calculateCellHeight(childHeightSize, mBorderSpace.y,
Jon Miranda228877d2021-02-09 11:05:00 -0500986 mCountY);
Winson Chung11a1a532013-09-13 11:14:45 -0700987 if (cw != mCellWidth || ch != mCellHeight) {
988 mCellWidth = cw;
989 mCellHeight = ch;
Jon Miranda228877d2021-02-09 11:05:00 -0500990 mShortcutsAndWidgets.setCellDimensions(mCellWidth, mCellHeight, mCountX, mCountY,
Thales Lima78d00ad2021-09-30 11:29:06 +0100991 mBorderSpace);
Winson Chung11a1a532013-09-13 11:14:45 -0700992 }
Winson Chung5f8afe62013-08-12 16:19:28 -0700993 }
Winson Chungaafa03c2010-06-11 17:34:16 -0700994
Winson Chung2d75f122013-09-23 16:53:31 -0700995 int newWidth = childWidthSize;
996 int newHeight = childHeightSize;
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700997 if (mFixedWidth > 0 && mFixedHeight > 0) {
998 newWidth = mFixedWidth;
999 newHeight = mFixedHeight;
1000 } else if (widthSpecMode == MeasureSpec.UNSPECIFIED || heightSpecMode == MeasureSpec.UNSPECIFIED) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001001 throw new RuntimeException("CellLayout cannot have UNSPECIFIED dimensions");
1002 }
1003
Sunny Goyal4fe5a372015-05-14 19:55:10 -07001004 mShortcutsAndWidgets.measure(
1005 MeasureSpec.makeMeasureSpec(newWidth, MeasureSpec.EXACTLY),
1006 MeasureSpec.makeMeasureSpec(newHeight, MeasureSpec.EXACTLY));
1007
1008 int maxWidth = mShortcutsAndWidgets.getMeasuredWidth();
1009 int maxHeight = mShortcutsAndWidgets.getMeasuredHeight();
Winson Chung2d75f122013-09-23 16:53:31 -07001010 if (mFixedWidth > 0 && mFixedHeight > 0) {
1011 setMeasuredDimension(maxWidth, maxHeight);
1012 } else {
1013 setMeasuredDimension(widthSize, heightSize);
1014 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001015 }
1016
1017 @Override
Michael Jurka28750fb2010-09-24 17:43:49 -07001018 protected void onLayout(boolean changed, int l, int t, int r, int b) {
Tony Wickham26b01422015-11-10 14:44:32 -08001019 int left = getPaddingLeft();
Sunny Goyal7ce471b2017-08-02 03:37:39 -07001020 left += (int) Math.ceil(getUnusedHorizontalSpace() / 2f);
Sunny Goyal7c786f72016-06-01 14:08:21 -07001021 int right = r - l - getPaddingRight();
Sunny Goyal7ce471b2017-08-02 03:37:39 -07001022 right -= (int) Math.ceil(getUnusedHorizontalSpace() / 2f);
Sunny Goyal7c786f72016-06-01 14:08:21 -07001023
Winson Chung38848ca2013-10-08 12:03:44 -07001024 int top = getPaddingTop();
Sunny Goyal7c786f72016-06-01 14:08:21 -07001025 int bottom = b - t - getPaddingBottom();
Sunny Goyal4fe5a372015-05-14 19:55:10 -07001026
Sunny Goyal7c786f72016-06-01 14:08:21 -07001027 // Expand the background drawing bounds by the padding baked into the background drawable
1028 mBackground.getPadding(mTempRect);
1029 mBackground.setBounds(
Jon Miranda28032002017-07-13 16:18:56 -07001030 left - mTempRect.left - getPaddingLeft(),
1031 top - mTempRect.top - getPaddingTop(),
1032 right + mTempRect.right + getPaddingRight(),
1033 bottom + mTempRect.bottom + getPaddingBottom());
Sunny Goyalae6e3182019-04-30 12:04:37 -07001034
Sunny Goyalc4d32012020-04-03 17:10:11 -07001035 mShortcutsAndWidgets.layout(left, top, right, bottom);
Sunny Goyal7c786f72016-06-01 14:08:21 -07001036 }
1037
Tony Wickhama501d492015-11-03 18:05:01 -08001038 /**
1039 * Returns the amount of space left over after subtracting padding and cells. This space will be
1040 * 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 -05001041 * width in {@link DeviceProfile#calculateCellWidth(int, int, int)}.
Tony Wickhama501d492015-11-03 18:05:01 -08001042 */
1043 public int getUnusedHorizontalSpace() {
Jon Miranda228877d2021-02-09 11:05:00 -05001044 return getMeasuredWidth() - getPaddingLeft() - getPaddingRight() - (mCountX * mCellWidth)
Thales Lima78d00ad2021-09-30 11:29:06 +01001045 - ((mCountX - 1) * mBorderSpace.x);
Tony Wickhama501d492015-11-03 18:05:01 -08001046 }
1047
Sunny Goyal2805e632015-05-20 15:35:32 -07001048 @Override
1049 protected boolean verifyDrawable(Drawable who) {
Sunny Goyal7ce471b2017-08-02 03:37:39 -07001050 return super.verifyDrawable(who) || (who == mBackground);
Sunny Goyal2805e632015-05-20 15:35:32 -07001051 }
1052
Michael Jurkaa52570f2012-03-20 03:18:20 -07001053 public ShortcutAndWidgetContainer getShortcutsAndWidgets() {
Sunny Goyaldcbcc862014-08-12 15:58:36 -07001054 return mShortcutsAndWidgets;
Michael Jurkaa52570f2012-03-20 03:18:20 -07001055 }
1056
Jon Miranda228877d2021-02-09 11:05:00 -05001057 public View getChildAt(int cellX, int cellY) {
1058 return mShortcutsAndWidgets.getChildAt(cellX, cellY);
Patrick Dubroy440c3602010-07-13 17:50:32 -07001059 }
1060
Adam Cohen76fc0852011-06-17 13:26:23 -07001061 public boolean animateChildToPosition(final View child, int cellX, int cellY, int duration,
Adam Cohen482ed822012-03-02 14:15:13 -08001062 int delay, boolean permanent, boolean adjustOccupied) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07001063 ShortcutAndWidgetContainer clc = getShortcutsAndWidgets();
Adam Cohen482ed822012-03-02 14:15:13 -08001064
Adam Cohen1d13c0b2020-04-21 16:29:12 -07001065 if (clc.indexOfChild(child) != -1 && (child instanceof Reorderable)) {
Sebastian Francod4682992022-10-05 13:03:09 -05001066 final CellLayoutLayoutParams lp = (CellLayoutLayoutParams) child.getLayoutParams();
Adam Cohenbfbfd262011-06-13 16:55:12 -07001067 final ItemInfo info = (ItemInfo) child.getTag();
Adam Cohen1d13c0b2020-04-21 16:29:12 -07001068 final Reorderable item = (Reorderable) child;
Adam Cohenbfbfd262011-06-13 16:55:12 -07001069
1070 // We cancel any existing animations
1071 if (mReorderAnimators.containsKey(lp)) {
1072 mReorderAnimators.get(lp).cancel();
1073 mReorderAnimators.remove(lp);
1074 }
1075
Adam Cohen1d13c0b2020-04-21 16:29:12 -07001076
Adam Cohen482ed822012-03-02 14:15:13 -08001077 if (adjustOccupied) {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001078 GridOccupancy occupied = permanent ? mOccupied : mTmpOccupied;
Sebastian Franco877088e2023-01-03 15:16:22 -07001079 occupied.markCells(lp.getCellX(), lp.getCellY(), lp.cellHSpan, lp.cellVSpan, false);
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001080 occupied.markCells(cellX, cellY, lp.cellHSpan, lp.cellVSpan, true);
Adam Cohen482ed822012-03-02 14:15:13 -08001081 }
Adam Cohen1d13c0b2020-04-21 16:29:12 -07001082
1083 // Compute the new x and y position based on the new cellX and cellY
1084 // We leverage the actual layout logic in the layout params and hence need to modify
1085 // state and revert that state.
1086 final int oldX = lp.x;
1087 final int oldY = lp.y;
Adam Cohenbfbfd262011-06-13 16:55:12 -07001088 lp.isLockedToGrid = true;
Adam Cohen482ed822012-03-02 14:15:13 -08001089 if (permanent) {
Sunny Goyal669b71f2023-01-27 14:37:07 -08001090 lp.setCellX(cellX);
1091 lp.setCellY(cellY);
Adam Cohen482ed822012-03-02 14:15:13 -08001092 } else {
Sebastian Franco877088e2023-01-03 15:16:22 -07001093 lp.setTmpCellX(cellX);
1094 lp.setTmpCellY(cellY);
Adam Cohen482ed822012-03-02 14:15:13 -08001095 }
Jon Mirandae96798e2016-12-07 12:10:44 -08001096 clc.setupLp(child);
Adam Cohen482ed822012-03-02 14:15:13 -08001097 final int newX = lp.x;
1098 final int newY = lp.y;
Adam Cohen76fc0852011-06-17 13:26:23 -07001099 lp.x = oldX;
1100 lp.y = oldY;
Adam Cohen1d13c0b2020-04-21 16:29:12 -07001101 lp.isLockedToGrid = false;
1102 // End compute new x and y
1103
Sunny Goyal82dfc152023-02-24 16:50:09 -08001104 MultiTranslateDelegate mtd = item.getTranslateDelegate();
1105 float initPreviewOffsetX = mtd.getTranslationX(INDEX_REORDER_PREVIEW_OFFSET).getValue();
1106 float initPreviewOffsetY = mtd.getTranslationY(INDEX_REORDER_PREVIEW_OFFSET).getValue();
Adam Cohen1d13c0b2020-04-21 16:29:12 -07001107 final float finalPreviewOffsetX = newX - oldX;
1108 final float finalPreviewOffsetY = newY - oldY;
1109
Adam Cohen482ed822012-03-02 14:15:13 -08001110 // Exit early if we're not actually moving the view
Adam Cohen1d13c0b2020-04-21 16:29:12 -07001111 if (finalPreviewOffsetX == 0 && finalPreviewOffsetY == 0
1112 && initPreviewOffsetX == 0 && initPreviewOffsetY == 0) {
Adam Cohen482ed822012-03-02 14:15:13 -08001113 lp.isLockedToGrid = true;
1114 return true;
1115 }
1116
Sunny Goyal849c6a22018-08-08 16:33:46 -07001117 ValueAnimator va = ValueAnimator.ofFloat(0f, 1f);
Adam Cohen482ed822012-03-02 14:15:13 -08001118 va.setDuration(duration);
1119 mReorderAnimators.put(lp, va);
1120
1121 va.addUpdateListener(new AnimatorUpdateListener() {
1122 @Override
Adam Cohenbfbfd262011-06-13 16:55:12 -07001123 public void onAnimationUpdate(ValueAnimator animation) {
Jon Mirandae96798e2016-12-07 12:10:44 -08001124 float r = (Float) animation.getAnimatedValue();
Adam Cohen1d13c0b2020-04-21 16:29:12 -07001125 float x = (1 - r) * initPreviewOffsetX + r * finalPreviewOffsetX;
1126 float y = (1 - r) * initPreviewOffsetY + r * finalPreviewOffsetY;
Sunny Goyal82dfc152023-02-24 16:50:09 -08001127 item.getTranslateDelegate().setTranslation(INDEX_REORDER_PREVIEW_OFFSET, x, y);
Adam Cohenbfbfd262011-06-13 16:55:12 -07001128 }
1129 });
Adam Cohen482ed822012-03-02 14:15:13 -08001130 va.addListener(new AnimatorListenerAdapter() {
Adam Cohenbfbfd262011-06-13 16:55:12 -07001131 boolean cancelled = false;
1132 public void onAnimationEnd(Animator animation) {
1133 // If the animation was cancelled, it means that another animation
1134 // has interrupted this one, and we don't want to lock the item into
1135 // place just yet.
1136 if (!cancelled) {
1137 lp.isLockedToGrid = true;
Sunny Goyal82dfc152023-02-24 16:50:09 -08001138 item.getTranslateDelegate()
1139 .setTranslation(INDEX_REORDER_PREVIEW_OFFSET, 0, 0);
Adam Cohen482ed822012-03-02 14:15:13 -08001140 child.requestLayout();
Adam Cohenbfbfd262011-06-13 16:55:12 -07001141 }
1142 if (mReorderAnimators.containsKey(lp)) {
1143 mReorderAnimators.remove(lp);
1144 }
1145 }
1146 public void onAnimationCancel(Animator animation) {
1147 cancelled = true;
1148 }
1149 });
Adam Cohen482ed822012-03-02 14:15:13 -08001150 va.setStartDelay(delay);
1151 va.start();
Adam Cohenbfbfd262011-06-13 16:55:12 -07001152 return true;
1153 }
1154 return false;
1155 }
1156
Adam Cohenf7ca3b42021-02-22 11:03:58 -08001157 void visualizeDropLocation(int cellX, int cellY, int spanX, int spanY,
1158 DropTarget.DragObject dragObject) {
1159 if (mDragCell[0] != cellX || mDragCell[1] != cellY || mDragCellSpan[0] != spanX
1160 || mDragCellSpan[1] != spanY) {
Adam Cohen482ed822012-03-02 14:15:13 -08001161 mDragCell[0] = cellX;
1162 mDragCell[1] = cellY;
Adam Cohenf7ca3b42021-02-22 11:03:58 -08001163 mDragCellSpan[0] = spanX;
1164 mDragCellSpan[1] = spanY;
Steven Ng30dd1d62021-03-15 21:45:49 +00001165
Steven Nga999e222021-04-19 18:17:15 +01001166 // Apply color extraction on a widget when dragging.
1167 applyColorExtractionOnWidget(dragObject, mDragCell, spanX, spanY);
1168
Joe Onorato4be866d2010-10-10 11:26:02 -07001169 final int oldIndex = mDragOutlineCurrent;
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -07001170 mDragOutlineAnims[oldIndex].animateOut();
1171 mDragOutlineCurrent = (oldIndex + 1) % mDragOutlines.length;
Sunny Goyal106bf642015-07-16 12:18:06 -07001172
Sebastian Francod4682992022-10-05 13:03:09 -05001173 CellLayoutLayoutParams cell = mDragOutlines[mDragOutlineCurrent];
Sebastian Franco877088e2023-01-03 15:16:22 -07001174 cell.setCellX(cellX);
1175 cell.setCellY(cellY);
Adam Cohenf7ca3b42021-02-22 11:03:58 -08001176 cell.cellHSpan = spanX;
1177 cell.cellVSpan = spanY;
Adam Cohen65086992020-02-19 08:40:49 -08001178
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -07001179 mDragOutlineAnims[mDragOutlineCurrent].animateIn();
Adam Cohenf7ca3b42021-02-22 11:03:58 -08001180 invalidate();
Sunny Goyale78e3d72015-09-24 11:23:31 -07001181
1182 if (dragObject.stateAnnouncer != null) {
Sunny Goyalc13403c2016-11-18 23:44:48 -08001183 dragObject.stateAnnouncer.announce(getItemMoveDescription(cellX, cellY));
Sunny Goyale78e3d72015-09-24 11:23:31 -07001184 }
Adam Cohenf7ca3b42021-02-22 11:03:58 -08001185
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001186 }
1187 }
1188
Steven Ng30dd1d62021-03-15 21:45:49 +00001189 /** Applies the local color extraction to a dragging widget object. */
Steven Nga999e222021-04-19 18:17:15 +01001190 private void applyColorExtractionOnWidget(DropTarget.DragObject dragObject, int[] targetCell,
1191 int spanX, int spanY) {
Steven Ng30dd1d62021-03-15 21:45:49 +00001192 // Apply local extracted color if the DragView is an AppWidgetHostViewDrawable.
Steven Ng32427202021-04-19 18:12:12 +01001193 View view = dragObject.dragView.getContentView();
1194 if (view instanceof LauncherAppWidgetHostView) {
Sebastian Franco2986e0b2024-01-25 10:23:39 -08001195 int screenId = mCellLayoutContainer.getCellLayoutId(this);
Steven Ng30dd1d62021-03-15 21:45:49 +00001196 cellToRect(targetCell[0], targetCell[1], spanX, spanY, mTempRect);
Jonathan Miranda21930da2021-05-03 18:44:13 +00001197
Sunny Goyal69a8eec2021-07-22 10:02:50 -07001198 ((LauncherAppWidgetHostView) view).handleDrag(mTempRect, this, screenId);
Steven Ng30dd1d62021-03-15 21:45:49 +00001199 }
1200 }
1201
Sunny Goyal726bee72018-03-05 12:54:24 -08001202 @SuppressLint("StringFormatMatches")
Sunny Goyalc13403c2016-11-18 23:44:48 -08001203 public String getItemMoveDescription(int cellX, int cellY) {
1204 if (mContainerType == HOTSEAT) {
1205 return getContext().getString(R.string.move_to_hotseat_position,
1206 Math.max(cellX, cellY) + 1);
1207 } else {
Andras Kloczl2dacbee2022-02-21 16:53:28 +00001208 int row = cellY + 1;
Sebastian Franco2986e0b2024-01-25 10:23:39 -08001209 int col = Utilities.isRtl(getResources()) ? mCountX - cellX : cellX + 1;
1210 int panelCount = mCellLayoutContainer.getPanelCount();
1211 int pageIndex = mCellLayoutContainer.getCellLayoutIndex(this);
Andras Kloczl2dacbee2022-02-21 16:53:28 +00001212 if (panelCount > 1) {
1213 // Increment the column if the target is on the right side of a two panel home
Andras Kloczl2dacbee2022-02-21 16:53:28 +00001214 col += (pageIndex % panelCount) * mCountX;
1215 }
Sebastian Franco930531f2022-06-16 16:49:11 -07001216 return getContext().getString(R.string.move_to_empty_cell_description, row, col,
Sebastian Franco2986e0b2024-01-25 10:23:39 -08001217 mCellLayoutContainer.getPageDescription(pageIndex));
Sunny Goyalc13403c2016-11-18 23:44:48 -08001218 }
1219 }
1220
Adam Cohene0310962011-04-18 16:15:31 -07001221 public void clearDragOutlines() {
1222 final int oldIndex = mDragOutlineCurrent;
1223 mDragOutlineAnims[oldIndex].animateOut();
Adam Cohend41fbf52012-02-16 23:53:59 -08001224 mDragCell[0] = mDragCell[1] = -1;
Adam Cohene0310962011-04-18 16:15:31 -07001225 }
1226
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001227 /**
Jeff Sharkey70864282009-04-07 21:08:40 -07001228 * Find a vacant area that will fit the given bounds nearest the requested
1229 * cell location. Uses Euclidean distance to score multiple vacant areas.
Winson Chungaafa03c2010-06-11 17:34:16 -07001230 *
Romain Guy51afc022009-05-04 18:03:43 -07001231 * @param pixelX The X location at which you want to search for a vacant area.
1232 * @param pixelY The Y location at which you want to search for a vacant area.
Adam Cohend41fbf52012-02-16 23:53:59 -08001233 * @param minSpanX The minimum horizontal span required
1234 * @param minSpanY The minimum vertical span required
1235 * @param spanX Horizontal span of the object.
1236 * @param spanY Vertical span of the object.
1237 * @param result Array in which to place the result, or null (in which case a new array will
1238 * be allocated)
1239 * @return The X, Y cell of a vacant area that can contain this object,
1240 * nearest the requested location.
1241 */
Sebastian Francoe4c03452022-12-27 14:50:02 -06001242 public int[] findNearestVacantArea(int pixelX, int pixelY, int minSpanX, int minSpanY,
1243 int spanX, int spanY, int[] result, int[] resultSpan) {
Sebastian Francob57c0b22022-06-28 13:54:35 -07001244 return findNearestArea(pixelX, pixelY, minSpanX, minSpanY, spanX, spanY, false,
Adam Cohend41fbf52012-02-16 23:53:59 -08001245 result, resultSpan);
1246 }
1247
Adam Cohend41fbf52012-02-16 23:53:59 -08001248 /**
1249 * Find a vacant area that will fit the given bounds nearest the requested
1250 * cell location. Uses Euclidean distance to score multiple vacant areas.
Sebastian Francob57c0b22022-06-28 13:54:35 -07001251 * @param relativeXPos The X location relative to the Cell layout at which you want to search
1252 * for a vacant area.
1253 * @param relativeYPos The Y location relative to the Cell layout at which you want to search
1254 * for a vacant area.
Adam Cohend41fbf52012-02-16 23:53:59 -08001255 * @param minSpanX The minimum horizontal span required
1256 * @param minSpanY The minimum vertical span required
1257 * @param spanX Horizontal span of the object.
1258 * @param spanY Vertical span of the object.
1259 * @param ignoreOccupied If true, the result can be an occupied cell
1260 * @param result Array in which to place the result, or null (in which case a new array will
1261 * be allocated)
1262 * @return The X, Y cell of a vacant area that can contain this object,
1263 * nearest the requested location.
1264 */
Sebastian Franco96c46e72023-05-08 10:04:44 -06001265 protected int[] findNearestArea(int relativeXPos, int relativeYPos, int minSpanX, int minSpanY,
Sebastian Francob57c0b22022-06-28 13:54:35 -07001266 int spanX, int spanY, boolean ignoreOccupied, int[] result, int[] resultSpan) {
Sebastian Francob57c0b22022-06-28 13:54:35 -07001267 // For items with a spanX / spanY > 1, the passed in point (relativeXPos, relativeYPos)
1268 // corresponds to the center of the item, but we are searching based on the top-left cell,
1269 // so we translate the point over to correspond to the top-left.
1270 relativeXPos = (int) (relativeXPos - (mCellWidth + mBorderSpace.x) * (spanX - 1) / 2f);
1271 relativeYPos = (int) (relativeYPos - (mCellHeight + mBorderSpace.y) * (spanY - 1) / 2f);
Adam Cohene3e27a82011-04-15 12:07:39 -07001272
Jeff Sharkey70864282009-04-07 21:08:40 -07001273 // Keep track of best-scoring drop area
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001274 final int[] bestXY = result != null ? result : new int[2];
Jeff Sharkey70864282009-04-07 21:08:40 -07001275 double bestDistance = Double.MAX_VALUE;
Adam Cohend41fbf52012-02-16 23:53:59 -08001276 final Rect bestRect = new Rect(-1, -1, -1, -1);
Rajeev Kumar9962dbe2017-06-12 12:16:20 -07001277 final Stack<Rect> validRegions = new Stack<>();
Winson Chungaafa03c2010-06-11 17:34:16 -07001278
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001279 final int countX = mCountX;
1280 final int countY = mCountY;
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001281
Adam Cohend41fbf52012-02-16 23:53:59 -08001282 if (minSpanX <= 0 || minSpanY <= 0 || spanX <= 0 || spanY <= 0 ||
1283 spanX < minSpanX || spanY < minSpanY) {
1284 return bestXY;
1285 }
1286
1287 for (int y = 0; y < countY - (minSpanY - 1); y++) {
Michael Jurkac28de512010-08-13 11:27:44 -07001288 inner:
Adam Cohend41fbf52012-02-16 23:53:59 -08001289 for (int x = 0; x < countX - (minSpanX - 1); x++) {
1290 int ySize = -1;
1291 int xSize = -1;
Sebastian Francob57c0b22022-06-28 13:54:35 -07001292 if (!ignoreOccupied) {
Adam Cohend41fbf52012-02-16 23:53:59 -08001293 // First, let's see if this thing fits anywhere
1294 for (int i = 0; i < minSpanX; i++) {
1295 for (int j = 0; j < minSpanY; j++) {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001296 if (mOccupied.cells[x + i][y + j]) {
Adam Cohendf035382011-04-11 17:22:04 -07001297 continue inner;
1298 }
Michael Jurkac28de512010-08-13 11:27:44 -07001299 }
1300 }
Adam Cohend41fbf52012-02-16 23:53:59 -08001301 xSize = minSpanX;
1302 ySize = minSpanY;
1303
1304 // We know that the item will fit at _some_ acceptable size, now let's see
1305 // how big we can make it. We'll alternate between incrementing x and y spans
1306 // until we hit a limit.
1307 boolean incX = true;
1308 boolean hitMaxX = xSize >= spanX;
1309 boolean hitMaxY = ySize >= spanY;
1310 while (!(hitMaxX && hitMaxY)) {
1311 if (incX && !hitMaxX) {
1312 for (int j = 0; j < ySize; j++) {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001313 if (x + xSize > countX -1 || mOccupied.cells[x + xSize][y + j]) {
Adam Cohend41fbf52012-02-16 23:53:59 -08001314 // We can't move out horizontally
1315 hitMaxX = true;
1316 }
1317 }
1318 if (!hitMaxX) {
1319 xSize++;
1320 }
1321 } else if (!hitMaxY) {
1322 for (int i = 0; i < xSize; i++) {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001323 if (y + ySize > countY - 1 || mOccupied.cells[x + i][y + ySize]) {
Adam Cohend41fbf52012-02-16 23:53:59 -08001324 // We can't move out vertically
1325 hitMaxY = true;
1326 }
1327 }
1328 if (!hitMaxY) {
1329 ySize++;
1330 }
1331 }
1332 hitMaxX |= xSize >= spanX;
1333 hitMaxY |= ySize >= spanY;
1334 incX = !incX;
1335 }
Michael Jurkac28de512010-08-13 11:27:44 -07001336 }
Sunny Goyal2805e632015-05-20 15:35:32 -07001337 final int[] cellXY = mTmpPoint;
Adam Cohene3e27a82011-04-15 12:07:39 -07001338 cellToCenterPoint(x, y, cellXY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001339
Adam Cohend41fbf52012-02-16 23:53:59 -08001340 // We verify that the current rect is not a sub-rect of any of our previous
1341 // candidates. In this case, the current rect is disqualified in favour of the
1342 // containing rect.
Sebastian Franco4a922672022-10-27 16:42:24 -07001343 Rect currentRect = new Rect(x, y, x + xSize, y + ySize);
Adam Cohend41fbf52012-02-16 23:53:59 -08001344 boolean contained = false;
1345 for (Rect r : validRegions) {
1346 if (r.contains(currentRect)) {
1347 contained = true;
1348 break;
1349 }
1350 }
1351 validRegions.push(currentRect);
Sebastian Francob57c0b22022-06-28 13:54:35 -07001352 double distance = Math.hypot(cellXY[0] - relativeXPos, cellXY[1] - relativeYPos);
Adam Cohen482ed822012-03-02 14:15:13 -08001353
Adam Cohend41fbf52012-02-16 23:53:59 -08001354 if ((distance <= bestDistance && !contained) ||
1355 currentRect.contains(bestRect)) {
Michael Jurkac28de512010-08-13 11:27:44 -07001356 bestDistance = distance;
1357 bestXY[0] = x;
1358 bestXY[1] = y;
Adam Cohend41fbf52012-02-16 23:53:59 -08001359 if (resultSpan != null) {
1360 resultSpan[0] = xSize;
1361 resultSpan[1] = ySize;
1362 }
1363 bestRect.set(currentRect);
Michael Jurkac28de512010-08-13 11:27:44 -07001364 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001365 }
1366 }
1367
Adam Cohenc0dcf592011-06-01 15:30:43 -07001368 // Return -1, -1 if no suitable location found
1369 if (bestDistance == Double.MAX_VALUE) {
1370 bestXY[0] = -1;
1371 bestXY[1] = -1;
Jeff Sharkey70864282009-04-07 21:08:40 -07001372 }
Adam Cohenc0dcf592011-06-01 15:30:43 -07001373 return bestXY;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001374 }
Winson Chungaafa03c2010-06-11 17:34:16 -07001375
Sebastian Francoe4c03452022-12-27 14:50:02 -06001376 public GridOccupancy getOccupied() {
1377 return mOccupied;
1378 }
1379
Adam Cohen482ed822012-03-02 14:15:13 -08001380 private void copySolutionToTempState(ItemConfiguration solution, View dragView) {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001381 mTmpOccupied.clear();
Adam Cohen482ed822012-03-02 14:15:13 -08001382
Michael Jurkaa52570f2012-03-20 03:18:20 -07001383 int childCount = mShortcutsAndWidgets.getChildCount();
Adam Cohen482ed822012-03-02 14:15:13 -08001384 for (int i = 0; i < childCount; i++) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07001385 View child = mShortcutsAndWidgets.getChildAt(i);
Adam Cohen482ed822012-03-02 14:15:13 -08001386 if (child == dragView) continue;
Sebastian Francod4682992022-10-05 13:03:09 -05001387 CellLayoutLayoutParams lp = (CellLayoutLayoutParams) child.getLayoutParams();
Adam Cohen8baab352012-03-20 17:39:21 -07001388 CellAndSpan c = solution.map.get(child);
1389 if (c != null) {
Sebastian Franco877088e2023-01-03 15:16:22 -07001390 lp.setTmpCellX(c.cellX);
1391 lp.setTmpCellY(c.cellY);
Adam Cohen8baab352012-03-20 17:39:21 -07001392 lp.cellHSpan = c.spanX;
1393 lp.cellVSpan = c.spanY;
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001394 mTmpOccupied.markCells(c, true);
Adam Cohen482ed822012-03-02 14:15:13 -08001395 }
1396 }
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001397 mTmpOccupied.markCells(solution, true);
Adam Cohen482ed822012-03-02 14:15:13 -08001398 }
1399
1400 private void animateItemsToSolution(ItemConfiguration solution, View dragView, boolean
1401 commitDragView) {
1402
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001403 GridOccupancy occupied = DESTRUCTIVE_REORDER ? mOccupied : mTmpOccupied;
1404 occupied.clear();
Adam Cohen482ed822012-03-02 14:15:13 -08001405
Michael Jurkaa52570f2012-03-20 03:18:20 -07001406 int childCount = mShortcutsAndWidgets.getChildCount();
Adam Cohen482ed822012-03-02 14:15:13 -08001407 for (int i = 0; i < childCount; i++) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07001408 View child = mShortcutsAndWidgets.getChildAt(i);
Adam Cohen482ed822012-03-02 14:15:13 -08001409 if (child == dragView) continue;
Adam Cohen8baab352012-03-20 17:39:21 -07001410 CellAndSpan c = solution.map.get(child);
1411 if (c != null) {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001412 animateChildToPosition(child, c.cellX, c.cellY, REORDER_ANIMATION_DURATION, 0,
Adam Cohen19f37922012-03-21 11:59:11 -07001413 DESTRUCTIVE_REORDER, false);
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001414 occupied.markCells(c, true);
Adam Cohen482ed822012-03-02 14:15:13 -08001415 }
1416 }
1417 if (commitDragView) {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001418 occupied.markCells(solution, true);
Adam Cohen482ed822012-03-02 14:15:13 -08001419 }
1420 }
1421
Adam Cohenfa3c58f2013-12-06 16:10:55 -08001422
1423 // This method starts or changes the reorder preview animations
1424 private void beginOrAdjustReorderPreviewAnimations(ItemConfiguration solution,
Adam Cohen65086992020-02-19 08:40:49 -08001425 View dragView, int mode) {
Adam Cohen19f37922012-03-21 11:59:11 -07001426 int childCount = mShortcutsAndWidgets.getChildCount();
Adam Cohen19f37922012-03-21 11:59:11 -07001427 for (int i = 0; i < childCount; i++) {
1428 View child = mShortcutsAndWidgets.getChildAt(i);
1429 if (child == dragView) continue;
1430 CellAndSpan c = solution.map.get(child);
Sebastian Franco5f0af4f2023-11-21 10:45:45 -06001431 boolean skip = mode == ReorderPreviewAnimation.MODE_HINT
1432 && !solution.intersectingViews.contains(child);
Adam Cohend9162062020-03-24 16:35:35 -07001433
Sebastian Francod4682992022-10-05 13:03:09 -05001434 CellLayoutLayoutParams lp = (CellLayoutLayoutParams) child.getLayoutParams();
Adam Cohend9162062020-03-24 16:35:35 -07001435 if (c != null && !skip && (child instanceof Reorderable)) {
Sebastian Francof7654252024-01-18 11:50:50 -08001436 ReorderPreviewAnimation rha = new ReorderPreviewAnimation(child, mode,
1437 lp.getCellX(), lp.getCellY(), c.cellX, c.cellY, c.spanX, c.spanY,
1438 mReorderPreviewAnimationMagnitude, this, mShakeAnimators);
Adam Cohend024f982012-05-23 18:26:45 -07001439 rha.animate();
Adam Cohen19f37922012-03-21 11:59:11 -07001440 }
1441 }
1442 }
1443
Adam Cohenfa3c58f2013-12-06 16:10:55 -08001444 private void completeAndClearReorderPreviewAnimations() {
1445 for (ReorderPreviewAnimation a: mShakeAnimators.values()) {
Adam Cohend9162062020-03-24 16:35:35 -07001446 a.finishAnimation();
Adam Cohen19f37922012-03-21 11:59:11 -07001447 }
1448 mShakeAnimators.clear();
1449 }
1450
Sunny Goyal711c5962021-06-23 12:36:18 -07001451 private void commitTempPlacement(View dragView) {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001452 mTmpOccupied.copyTo(mOccupied);
Sunny Goyalaa8ef112015-06-12 20:04:41 -07001453
Sebastian Franco2986e0b2024-01-25 10:23:39 -08001454 int screenId = mCellLayoutContainer.getCellLayoutId(this);
Sunny Goyalaa8ef112015-06-12 20:04:41 -07001455 int container = Favorites.CONTAINER_DESKTOP;
1456
Sunny Goyalc13403c2016-11-18 23:44:48 -08001457 if (mContainerType == HOTSEAT) {
Sunny Goyalaa8ef112015-06-12 20:04:41 -07001458 screenId = -1;
1459 container = Favorites.CONTAINER_HOTSEAT;
1460 }
1461
Michael Jurkaa52570f2012-03-20 03:18:20 -07001462 int childCount = mShortcutsAndWidgets.getChildCount();
Adam Cohen482ed822012-03-02 14:15:13 -08001463 for (int i = 0; i < childCount; i++) {
Adam Cohenea889a22012-03-27 16:45:39 -07001464 View child = mShortcutsAndWidgets.getChildAt(i);
Sebastian Francod4682992022-10-05 13:03:09 -05001465 CellLayoutLayoutParams lp = (CellLayoutLayoutParams) child.getLayoutParams();
Adam Cohenea889a22012-03-27 16:45:39 -07001466 ItemInfo info = (ItemInfo) child.getTag();
Adam Cohen2acce882012-03-28 19:03:19 -07001467 // We do a null check here because the item info can be null in the case of the
1468 // AllApps button in the hotseat.
Sunny Goyal711c5962021-06-23 12:36:18 -07001469 if (info != null && child != dragView) {
Sunny Goyal669b71f2023-01-27 14:37:07 -08001470 CellPos presenterPos = mActivity.getCellPosMapper().mapModelToPresenter(info);
1471 final boolean requiresDbUpdate = (presenterPos.cellX != lp.getTmpCellX()
1472 || presenterPos.cellY != lp.getTmpCellY() || info.spanX != lp.cellHSpan
1473 || info.spanY != lp.cellVSpan || presenterPos.screenId != screenId);
Sunny Goyalaa8ef112015-06-12 20:04:41 -07001474
Sebastian Franco877088e2023-01-03 15:16:22 -07001475 lp.setCellX(lp.getTmpCellX());
Sebastian Franco877088e2023-01-03 15:16:22 -07001476 lp.setCellY(lp.getTmpCellY());
Sunny Goyalaa8ef112015-06-12 20:04:41 -07001477 if (requiresDbUpdate) {
Sunny Goyalab770a12018-11-14 15:17:26 -08001478 Launcher.cast(mActivity).getModelWriter().modifyItemInDatabase(info, container,
Sunny Goyal669b71f2023-01-27 14:37:07 -08001479 screenId, lp.getCellX(), lp.getCellY(), lp.cellHSpan, lp.cellVSpan);
Sunny Goyalaa8ef112015-06-12 20:04:41 -07001480 }
Adam Cohen2acce882012-03-28 19:03:19 -07001481 }
Adam Cohen482ed822012-03-02 14:15:13 -08001482 }
1483 }
1484
Sunny Goyalf7a29e82015-04-24 15:20:43 -07001485 private void setUseTempCoords(boolean useTempCoords) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07001486 int childCount = mShortcutsAndWidgets.getChildCount();
Adam Cohen482ed822012-03-02 14:15:13 -08001487 for (int i = 0; i < childCount; i++) {
Sebastian Francod4682992022-10-05 13:03:09 -05001488 CellLayoutLayoutParams lp = (CellLayoutLayoutParams) mShortcutsAndWidgets.getChildAt(
1489 i).getLayoutParams();
Adam Cohen482ed822012-03-02 14:15:13 -08001490 lp.useTmpCoords = useTempCoords;
1491 }
1492 }
1493
Sebastian Franco5c8f8682023-11-14 09:52:41 -06001494 /**
1495 * For a given region, return the rectangle of the overlapping cell and span with the given
1496 * region including the region itself. If there is no overlap the rectangle will be
1497 * invalid i.e. -1, 0, -1, 0.
1498 */
1499 @Nullable
1500 public Rect getIntersectingRectanglesInRegion(final Rect region, final View dragView) {
1501 Rect boundingRect = new Rect(region);
Adam Cohen19f37922012-03-21 11:59:11 -07001502 Rect r1 = new Rect();
Sebastian Franco5c8f8682023-11-14 09:52:41 -06001503 boolean isOverlapping = false;
Adam Cohen19f37922012-03-21 11:59:11 -07001504 final int count = mShortcutsAndWidgets.getChildCount();
1505 for (int i = 0; i < count; i++) {
1506 View child = mShortcutsAndWidgets.getChildAt(i);
1507 if (child == dragView) continue;
Sebastian Francod4682992022-10-05 13:03:09 -05001508 CellLayoutLayoutParams
1509 lp = (CellLayoutLayoutParams) child.getLayoutParams();
Sebastian Franco877088e2023-01-03 15:16:22 -07001510 r1.set(lp.getCellX(), lp.getCellY(), lp.getCellX() + lp.cellHSpan,
1511 lp.getCellY() + lp.cellVSpan);
Sebastian Franco5c8f8682023-11-14 09:52:41 -06001512 if (Rect.intersects(region, r1)) {
1513 isOverlapping = true;
1514 boundingRect.union(r1);
Adam Cohen19f37922012-03-21 11:59:11 -07001515 }
1516 }
Sebastian Franco5c8f8682023-11-14 09:52:41 -06001517 return isOverlapping ? boundingRect : null;
Adam Cohen19f37922012-03-21 11:59:11 -07001518 }
1519
Sebastian Francoe4c03452022-12-27 14:50:02 -06001520 public boolean isNearestDropLocationOccupied(int pixelX, int pixelY, int spanX, int spanY,
Adam Cohen19f37922012-03-21 11:59:11 -07001521 View dragView, int[] result) {
Sebastián Francof9a6ac22022-11-15 22:56:37 +00001522 result = findNearestAreaIgnoreOccupied(pixelX, pixelY, spanX, spanY, result);
Sebastian Franco5c8f8682023-11-14 09:52:41 -06001523 return getIntersectingRectanglesInRegion(
1524 new Rect(result[0], result[1], result[0] + spanX, result[1] + spanY),
1525 dragView
1526 ) != null;
Adam Cohen19f37922012-03-21 11:59:11 -07001527 }
1528
1529 void revertTempState() {
Adam Cohenfa3c58f2013-12-06 16:10:55 -08001530 completeAndClearReorderPreviewAnimations();
1531 if (isItemPlacementDirty() && !DESTRUCTIVE_REORDER) {
1532 final int count = mShortcutsAndWidgets.getChildCount();
1533 for (int i = 0; i < count; i++) {
1534 View child = mShortcutsAndWidgets.getChildAt(i);
Sebastian Francod4682992022-10-05 13:03:09 -05001535 CellLayoutLayoutParams
1536 lp = (CellLayoutLayoutParams) child.getLayoutParams();
Sebastian Franco877088e2023-01-03 15:16:22 -07001537 if (lp.getTmpCellX() != lp.getCellX() || lp.getTmpCellY() != lp.getCellY()) {
1538 lp.setTmpCellX(lp.getCellX());
1539 lp.setTmpCellY(lp.getCellY());
1540 animateChildToPosition(child, lp.getCellX(), lp.getCellY(),
1541 REORDER_ANIMATION_DURATION, 0, false, false);
Adam Cohenfa3c58f2013-12-06 16:10:55 -08001542 }
Adam Cohen19f37922012-03-21 11:59:11 -07001543 }
Adam Cohenfa3c58f2013-12-06 16:10:55 -08001544 setItemPlacementDirty(false);
Adam Cohen19f37922012-03-21 11:59:11 -07001545 }
Adam Cohen19f37922012-03-21 11:59:11 -07001546 }
1547
Adam Cohenbebf0422012-04-11 18:06:28 -07001548 boolean createAreaForResize(int cellX, int cellY, int spanX, int spanY,
1549 View dragView, int[] direction, boolean commit) {
1550 int[] pixelXY = new int[2];
1551 regionToCenterPoint(cellX, cellY, spanX, spanY, pixelXY);
1552
1553 // First we determine if things have moved enough to cause a different layout
Adam Cohenfa3c58f2013-12-06 16:10:55 -08001554 ItemConfiguration swapSolution = findReorderSolution(pixelXY[0], pixelXY[1], spanX, spanY,
Charlie Anderson46122392024-01-18 17:23:46 -05001555 spanX, spanY, direction, dragView, true);
Adam Cohenbebf0422012-04-11 18:06:28 -07001556
1557 setUseTempCoords(true);
1558 if (swapSolution != null && swapSolution.isSolution) {
1559 // If we're just testing for a possible location (MODE_ACCEPT_DROP), we don't bother
1560 // committing anything or animating anything as we just want to determine if a solution
1561 // exists
1562 copySolutionToTempState(swapSolution, dragView);
1563 setItemPlacementDirty(true);
1564 animateItemsToSolution(swapSolution, dragView, commit);
1565
1566 if (commit) {
Sunny Goyal711c5962021-06-23 12:36:18 -07001567 commitTempPlacement(null);
Adam Cohenfa3c58f2013-12-06 16:10:55 -08001568 completeAndClearReorderPreviewAnimations();
Adam Cohenbebf0422012-04-11 18:06:28 -07001569 setItemPlacementDirty(false);
1570 } else {
Adam Cohenfa3c58f2013-12-06 16:10:55 -08001571 beginOrAdjustReorderPreviewAnimations(swapSolution, dragView,
Adam Cohen65086992020-02-19 08:40:49 -08001572 ReorderPreviewAnimation.MODE_PREVIEW);
Adam Cohenbebf0422012-04-11 18:06:28 -07001573 }
1574 mShortcutsAndWidgets.requestLayout();
1575 }
1576 return swapSolution.isSolution;
1577 }
1578
Sebastian Francoe4c03452022-12-27 14:50:02 -06001579 public ReorderAlgorithm createReorderAlgorithm() {
1580 return new ReorderAlgorithm(this);
1581 }
1582
Sebastian Franco09589322022-11-02 15:25:58 -07001583 protected ItemConfiguration findReorderSolution(int pixelX, int pixelY, int minSpanX,
Charlie Anderson46122392024-01-18 17:23:46 -05001584 int minSpanY, int spanX, int spanY, int[] direction, View dragView, boolean decX) {
Sebastian Franco25f8e402023-12-02 13:54:05 -06001585 ItemConfiguration configuration = new ItemConfiguration();
1586 copyCurrentStateToSolution(configuration);
1587 ReorderParameters parameters = new ReorderParameters(pixelX, pixelY, spanX, spanY, minSpanX,
1588 minSpanY, dragView, configuration);
Charlie Anderson46122392024-01-18 17:23:46 -05001589 int[] directionVector = direction != null ? direction : mDirectionVector;
1590 return createReorderAlgorithm().findReorderSolution(parameters, directionVector, decX);
Sebastian Franco09589322022-11-02 15:25:58 -07001591 }
1592
Sebastián Franco61fbd182023-11-29 21:30:43 +00001593 public void copyCurrentStateToSolution(ItemConfiguration solution) {
Sebastian Franco9cab1c32022-10-25 17:28:54 -07001594 int childCount = mShortcutsAndWidgets.getChildCount();
1595 for (int i = 0; i < childCount; i++) {
1596 View child = mShortcutsAndWidgets.getChildAt(i);
1597 CellLayoutLayoutParams lp = (CellLayoutLayoutParams) child.getLayoutParams();
Sebastián Franco61fbd182023-11-29 21:30:43 +00001598 solution.add(child,
1599 new CellAndSpan(lp.getCellX(), lp.getCellY(), lp.cellHSpan, lp.cellVSpan));
Sebastian Franco9cab1c32022-10-25 17:28:54 -07001600 }
Sebastian Franco53a15a42022-10-25 17:28:54 -07001601 }
1602
1603 /**
Sebastian Franco53a15a42022-10-25 17:28:54 -07001604 * When the user drags an Item in the workspace sometimes we need to move the items already in
1605 * the workspace to make space for the new item, this function return a solution for that
1606 * reorder.
1607 *
1608 * @param pixelX X coordinate in the screen of the dragView in pixels
1609 * @param pixelY Y coordinate in the screen of the dragView in pixels
1610 * @param minSpanX minimum horizontal span the item can be shrunk to
1611 * @param minSpanY minimum vertical span the item can be shrunk to
1612 * @param spanX occupied horizontal span
1613 * @param spanY occupied vertical span
1614 * @param dragView the view of the item being draged
1615 * @return returns a solution for the given parameters, the solution contains all the icons and
1616 * the locations they should be in the given solution.
1617 */
1618 public ItemConfiguration calculateReorder(int pixelX, int pixelY, int minSpanX, int minSpanY,
1619 int spanX, int spanY, View dragView) {
Sebastian Franco25f8e402023-12-02 13:54:05 -06001620 ItemConfiguration configuration = new ItemConfiguration();
1621 copyCurrentStateToSolution(configuration);
1622 return createReorderAlgorithm().calculateReorder(
1623 new ReorderParameters(pixelX, pixelY, spanX, spanY, minSpanX, minSpanY, dragView,
1624 configuration)
1625 );
Sebastian Franco53a15a42022-10-25 17:28:54 -07001626 }
Adam Cohen482ed822012-03-02 14:15:13 -08001627
Sebastian Franco9cab1c32022-10-25 17:28:54 -07001628 int[] performReorder(int pixelX, int pixelY, int minSpanX, int minSpanY, int spanX, int spanY,
1629 View dragView, int[] result, int[] resultSpan, int mode) {
1630 if (resultSpan == null) {
1631 resultSpan = new int[]{-1, -1};
1632 }
1633 if (result == null) {
1634 result = new int[]{-1, -1};
1635 }
Sebastian Franco5d990ee2022-11-01 16:08:24 -07001636
1637 ItemConfiguration finalSolution = null;
1638 // We want the solution to match the animation of the preview and to match the drop so we
1639 // only recalculate in mode MODE_SHOW_REORDER_HINT because that the first one to run in the
1640 // reorder cycle.
1641 if (mode == MODE_SHOW_REORDER_HINT || mPreviousSolution == null) {
1642 finalSolution = calculateReorder(pixelX, pixelY, minSpanX, minSpanY, spanX, spanY,
1643 dragView);
1644 mPreviousSolution = finalSolution;
1645 } else {
Sebastian Franco9cab1c32022-10-25 17:28:54 -07001646 finalSolution = mPreviousSolution;
1647 // We reset this vector after drop
1648 if (mode == MODE_ON_DROP || mode == MODE_ON_DROP_EXTERNAL) {
1649 mPreviousSolution = null;
1650 }
Sebastian Franco9cab1c32022-10-25 17:28:54 -07001651 }
1652
1653 if (finalSolution == null || !finalSolution.isSolution) {
1654 result[0] = result[1] = resultSpan[0] = resultSpan[1] = -1;
1655 } else {
1656 result[0] = finalSolution.cellX;
1657 result[1] = finalSolution.cellY;
1658 resultSpan[0] = finalSolution.spanX;
1659 resultSpan[1] = finalSolution.spanY;
Sebastian Franco9c743272022-11-15 15:03:25 -08001660 performReorder(finalSolution, dragView, mode);
Sebastian Franco9cab1c32022-10-25 17:28:54 -07001661 }
Sebastian Franco9cab1c32022-10-25 17:28:54 -07001662 return result;
1663 }
1664
Sebastian Franco53a15a42022-10-25 17:28:54 -07001665 /**
1666 * Animates and submits in the DB the given ItemConfiguration depending of the mode.
1667 *
1668 * @param solution represents widgets on the screen which the Workspace will animate to and
1669 * would be submitted to the database.
1670 * @param dragView view which is being dragged over the workspace that trigger the reorder
1671 * @param mode depending on the mode different animations would be played and depending on the
1672 * mode the solution would be submitted or not the database.
1673 * The possible modes are {@link MODE_SHOW_REORDER_HINT}, {@link MODE_DRAG_OVER},
1674 * {@link MODE_ON_DROP}, {@link MODE_ON_DROP_EXTERNAL}, {@link MODE_ACCEPT_DROP}
1675 * defined in {@link CellLayout}.
1676 */
Sebastian Francoe4c03452022-12-27 14:50:02 -06001677 public void performReorder(ItemConfiguration solution, View dragView, int mode) {
Adam Cohenfa3c58f2013-12-06 16:10:55 -08001678 if (mode == MODE_SHOW_REORDER_HINT) {
Sebastian Franco53a15a42022-10-25 17:28:54 -07001679 beginOrAdjustReorderPreviewAnimations(solution, dragView,
1680 ReorderPreviewAnimation.MODE_HINT);
1681 return;
1682 }
1683 // If we're just testing for a possible location (MODE_ACCEPT_DROP), we don't bother
1684 // committing anything or animating anything as we just want to determine if a solution
1685 // exists
1686 if (mode == MODE_DRAG_OVER || mode == MODE_ON_DROP || mode == MODE_ON_DROP_EXTERNAL) {
1687 if (!DESTRUCTIVE_REORDER) {
1688 setUseTempCoords(true);
1689 }
1690
1691 if (!DESTRUCTIVE_REORDER) {
1692 copySolutionToTempState(solution, dragView);
1693 }
1694 setItemPlacementDirty(true);
1695 animateItemsToSolution(solution, dragView, mode == MODE_ON_DROP);
1696
1697 if (!DESTRUCTIVE_REORDER
1698 && (mode == MODE_ON_DROP || mode == MODE_ON_DROP_EXTERNAL)) {
1699 // Since the temp solution didn't update dragView, don't commit it either
1700 commitTempPlacement(dragView);
1701 completeAndClearReorderPreviewAnimations();
1702 setItemPlacementDirty(false);
Adam Cohenfa3c58f2013-12-06 16:10:55 -08001703 } else {
Sebastian Franco53a15a42022-10-25 17:28:54 -07001704 beginOrAdjustReorderPreviewAnimations(solution, dragView,
1705 ReorderPreviewAnimation.MODE_PREVIEW);
Adam Cohenfa3c58f2013-12-06 16:10:55 -08001706 }
Adam Cohenfa3c58f2013-12-06 16:10:55 -08001707 }
1708
Sebastian Franco53a15a42022-10-25 17:28:54 -07001709 if (mode == MODE_ON_DROP && !DESTRUCTIVE_REORDER) {
Adam Cohen482ed822012-03-02 14:15:13 -08001710 setUseTempCoords(false);
1711 }
Adam Cohen482ed822012-03-02 14:15:13 -08001712
Michael Jurkaa52570f2012-03-20 03:18:20 -07001713 mShortcutsAndWidgets.requestLayout();
Adam Cohen482ed822012-03-02 14:15:13 -08001714 }
1715
Adam Cohen19f37922012-03-21 11:59:11 -07001716 void setItemPlacementDirty(boolean dirty) {
1717 mItemPlacementDirty = dirty;
Adam Cohen482ed822012-03-02 14:15:13 -08001718 }
Adam Cohen19f37922012-03-21 11:59:11 -07001719 boolean isItemPlacementDirty() {
1720 return mItemPlacementDirty;
Adam Cohen482ed822012-03-02 14:15:13 -08001721 }
1722
Sebastian Francoe4c03452022-12-27 14:50:02 -06001723 /**
Adam Cohendf035382011-04-11 17:22:04 -07001724 * Find a starting cell position that will fit the given bounds nearest the requested
1725 * cell location. Uses Euclidean distance to score multiple vacant areas.
1726 *
1727 * @param pixelX The X location at which you want to search for a vacant area.
1728 * @param pixelY The Y location at which you want to search for a vacant area.
1729 * @param spanX Horizontal span of the object.
1730 * @param spanY Vertical span of the object.
Adam Cohendf035382011-04-11 17:22:04 -07001731 * @param result Previously returned value to possibly recycle.
1732 * @return The X, Y cell of a vacant area that can contain this object,
1733 * nearest the requested location.
1734 */
Sebastián Francof9a6ac22022-11-15 22:56:37 +00001735 public int[] findNearestAreaIgnoreOccupied(int pixelX, int pixelY, int spanX, int spanY,
1736 int[] result) {
Sebastian Francob57c0b22022-06-28 13:54:35 -07001737 return findNearestArea(pixelX, pixelY, spanX, spanY, spanX, spanY, true, result, null);
Adam Cohendf035382011-04-11 17:22:04 -07001738 }
1739
Michael Jurka0280c3b2010-09-17 15:00:07 -07001740 boolean existsEmptyCell() {
1741 return findCellForSpan(null, 1, 1);
1742 }
1743
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001744 /**
Michael Jurka0280c3b2010-09-17 15:00:07 -07001745 * Finds the upper-left coordinate of the first rectangle in the grid that can
1746 * hold a cell of the specified dimensions. If intersectX and intersectY are not -1,
1747 * then this method will only return coordinates for rectangles that contain the cell
1748 * (intersectX, intersectY)
1749 *
1750 * @param cellXY The array that will contain the position of a vacant cell if such a cell
1751 * can be found.
1752 * @param spanX The horizontal span of the cell we want to find.
1753 * @param spanY The vertical span of the cell we want to find.
1754 *
1755 * @return True if a vacant cell of the specified dimension was found, false otherwise.
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001756 */
Hyunyoung Song3f471442015-04-08 19:01:34 -07001757 public boolean findCellForSpan(int[] cellXY, int spanX, int spanY) {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001758 if (cellXY == null) {
1759 cellXY = new int[2];
Michael Jurka0280c3b2010-09-17 15:00:07 -07001760 }
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001761 return mOccupied.findVacantCell(cellXY, spanX, spanY);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001762 }
1763
1764 /**
Winson Chungc07918d2011-07-01 15:35:26 -07001765 * A drag event has begun over this layout.
1766 * It may have begun over this layout (in which case onDragChild is called first),
1767 * or it may have begun on another layout.
1768 */
1769 void onDragEnter() {
Winson Chungc07918d2011-07-01 15:35:26 -07001770 mDragging = true;
Sebastian Franco5aa71ce2022-12-14 12:13:19 -06001771 mPreviousSolution = null;
Winson Chungc07918d2011-07-01 15:35:26 -07001772 }
1773
1774 /**
Michael Jurka0280c3b2010-09-17 15:00:07 -07001775 * Called when drag has left this CellLayout or has been completed (successfully or not)
1776 */
1777 void onDragExit() {
Joe Onorato4be866d2010-10-10 11:26:02 -07001778 // This can actually be called when we aren't in a drag, e.g. when adding a new
1779 // item to this layout via the customize drawer.
1780 // Guard against that case.
1781 if (mDragging) {
1782 mDragging = false;
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001783 }
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -07001784
1785 // Invalidate the drag data
Sebastian Franco5aa71ce2022-12-14 12:13:19 -06001786 mPreviousSolution = null;
Adam Cohend41fbf52012-02-16 23:53:59 -08001787 mDragCell[0] = mDragCell[1] = -1;
Adam Cohenf7ca3b42021-02-22 11:03:58 -08001788 mDragCellSpan[0] = mDragCellSpan[1] = -1;
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -07001789 mDragOutlineAnims[mDragOutlineCurrent].animateOut();
1790 mDragOutlineCurrent = (mDragOutlineCurrent + 1) % mDragOutlineAnims.length;
Adam Cohen19f37922012-03-21 11:59:11 -07001791 revertTempState();
Michael Jurka33945b22010-12-21 18:19:38 -08001792 setIsDragOverlapping(false);
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001793 }
1794
1795 /**
Winson Chungaafa03c2010-06-11 17:34:16 -07001796 * Mark a child as having been dropped.
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001797 * At the beginning of the drag operation, the child may have been on another
Patrick Dubroyce34a972010-10-19 10:34:32 -07001798 * screen, but it is re-parented before this method is called.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001799 *
1800 * @param child The child that is being dropped
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001801 */
Adam Cohen716b51e2011-06-30 12:09:54 -07001802 void onDropChild(View child) {
Romain Guyd94533d2009-08-17 10:01:15 -07001803 if (child != null) {
Sebastian Francod4682992022-10-05 13:03:09 -05001804 CellLayoutLayoutParams
1805 lp = (CellLayoutLayoutParams) child.getLayoutParams();
Romain Guy84f296c2009-11-04 15:00:44 -08001806 lp.dropped = true;
Romain Guyd94533d2009-08-17 10:01:15 -07001807 child.requestLayout();
Tony Wickham1cdb6d02015-09-17 11:08:27 -07001808 markCellsAsOccupiedForView(child);
Romain Guyd94533d2009-08-17 10:01:15 -07001809 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001810 }
1811
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001812 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001813 * Computes a bounding rectangle for a range of cells
Winson Chungaafa03c2010-06-11 17:34:16 -07001814 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001815 * @param cellX X coordinate of upper left corner expressed as a cell position
1816 * @param cellY Y coordinate of upper left corner expressed as a cell position
Winson Chungaafa03c2010-06-11 17:34:16 -07001817 * @param cellHSpan Width in cells
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001818 * @param cellVSpan Height in cells
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001819 * @param resultRect Rect into which to put the results
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001820 */
Adam Cohend41fbf52012-02-16 23:53:59 -08001821 public void cellToRect(int cellX, int cellY, int cellHSpan, int cellVSpan, Rect resultRect) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001822 final int cellWidth = mCellWidth;
1823 final int cellHeight = mCellHeight;
Winson Chungaafa03c2010-06-11 17:34:16 -07001824
Pierre Barbier de Reuille1b8bbb62021-05-19 22:45:16 +01001825 // We observe a shift of 1 pixel on the x coordinate compared to the actual cell coordinates
1826 final int hStartPadding = getPaddingLeft()
1827 + (int) Math.ceil(getUnusedHorizontalSpace() / 2f);
Winson Chung4b825dcd2011-06-19 12:41:22 -07001828 final int vStartPadding = getPaddingTop();
Winson Chungaafa03c2010-06-11 17:34:16 -07001829
Thales Lima78d00ad2021-09-30 11:29:06 +01001830 int x = hStartPadding + (cellX * mBorderSpace.x) + (cellX * cellWidth);
1831 int y = vStartPadding + (cellY * mBorderSpace.y) + (cellY * cellHeight);
Jon Miranda228877d2021-02-09 11:05:00 -05001832
Thales Lima78d00ad2021-09-30 11:29:06 +01001833 int width = cellHSpan * cellWidth + ((cellHSpan - 1) * mBorderSpace.x);
1834 int height = cellVSpan * cellHeight + ((cellVSpan - 1) * mBorderSpace.y);
Winson Chungaafa03c2010-06-11 17:34:16 -07001835
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001836 resultRect.set(x, y, x + width, y + height);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001837 }
Winson Chungaafa03c2010-06-11 17:34:16 -07001838
Adam Cohend4844c32011-02-18 19:25:06 -08001839 public void markCellsAsOccupiedForView(View view) {
Sebastian Francof153d912022-04-22 16:15:27 -05001840 if (view instanceof LauncherAppWidgetHostView
1841 && view.getTag() instanceof LauncherAppWidgetInfo) {
1842 LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) view.getTag();
Sunny Goyal669b71f2023-01-27 14:37:07 -08001843 CellPos pos = mActivity.getCellPosMapper().mapModelToPresenter(info);
1844 mOccupied.markCells(pos.cellX, pos.cellY, info.spanX, info.spanY, true);
Sebastian Francof153d912022-04-22 16:15:27 -05001845 return;
1846 }
Michael Jurkaa52570f2012-03-20 03:18:20 -07001847 if (view == null || view.getParent() != mShortcutsAndWidgets) return;
Sebastian Francod4682992022-10-05 13:03:09 -05001848 CellLayoutLayoutParams
1849 lp = (CellLayoutLayoutParams) view.getLayoutParams();
Sebastian Franco877088e2023-01-03 15:16:22 -07001850 mOccupied.markCells(lp.getCellX(), lp.getCellY(), lp.cellHSpan, lp.cellVSpan, true);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001851 }
1852
Adam Cohend4844c32011-02-18 19:25:06 -08001853 public void markCellsAsUnoccupiedForView(View view) {
Sebastian Francof153d912022-04-22 16:15:27 -05001854 if (view instanceof LauncherAppWidgetHostView
1855 && view.getTag() instanceof LauncherAppWidgetInfo) {
1856 LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) view.getTag();
Sunny Goyal669b71f2023-01-27 14:37:07 -08001857 CellPos pos = mActivity.getCellPosMapper().mapModelToPresenter(info);
1858 mOccupied.markCells(pos.cellX, pos.cellY, info.spanX, info.spanY, false);
Sebastian Francof153d912022-04-22 16:15:27 -05001859 return;
1860 }
Michael Jurkaa52570f2012-03-20 03:18:20 -07001861 if (view == null || view.getParent() != mShortcutsAndWidgets) return;
Sebastian Francod4682992022-10-05 13:03:09 -05001862 CellLayoutLayoutParams
1863 lp = (CellLayoutLayoutParams) view.getLayoutParams();
Sebastian Franco877088e2023-01-03 15:16:22 -07001864 mOccupied.markCells(lp.getCellX(), lp.getCellY(), lp.cellHSpan, lp.cellVSpan, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001865 }
1866
Adam Cohen2801caf2011-05-13 20:57:39 -07001867 public int getDesiredWidth() {
Jon Miranda228877d2021-02-09 11:05:00 -05001868 return getPaddingLeft() + getPaddingRight() + (mCountX * mCellWidth)
Thales Lima78d00ad2021-09-30 11:29:06 +01001869 + ((mCountX - 1) * mBorderSpace.x);
Adam Cohen2801caf2011-05-13 20:57:39 -07001870 }
1871
1872 public int getDesiredHeight() {
Jon Miranda228877d2021-02-09 11:05:00 -05001873 return getPaddingTop() + getPaddingBottom() + (mCountY * mCellHeight)
Thales Lima78d00ad2021-09-30 11:29:06 +01001874 + ((mCountY - 1) * mBorderSpace.y);
Adam Cohen2801caf2011-05-13 20:57:39 -07001875 }
1876
Michael Jurka66d72172011-04-12 16:29:25 -07001877 public boolean isOccupied(int x, int y) {
Schneider Victor-tulias750c5af2023-07-26 10:16:04 -07001878 if (x >= 0 && x < mCountX && y >= 0 && y < mCountY) {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001879 return mOccupied.cells[x][y];
Schneider Victor-tulias750c5af2023-07-26 10:16:04 -07001880 }
1881 if (BuildConfig.IS_STUDIO_BUILD) {
Michael Jurka66d72172011-04-12 16:29:25 -07001882 throw new RuntimeException("Position exceeds the bound of this CellLayout");
1883 }
Schneider Victor-tulias750c5af2023-07-26 10:16:04 -07001884 return true;
Michael Jurka66d72172011-04-12 16:29:25 -07001885 }
1886
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001887 @Override
1888 public ViewGroup.LayoutParams generateLayoutParams(AttributeSet attrs) {
Sebastian Francod4682992022-10-05 13:03:09 -05001889 return new CellLayoutLayoutParams(getContext(), attrs);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001890 }
1891
1892 @Override
1893 protected boolean checkLayoutParams(ViewGroup.LayoutParams p) {
Sebastian Francod4682992022-10-05 13:03:09 -05001894 return p instanceof CellLayoutLayoutParams;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001895 }
1896
1897 @Override
1898 protected ViewGroup.LayoutParams generateLayoutParams(ViewGroup.LayoutParams p) {
Sebastian Francod4682992022-10-05 13:03:09 -05001899 return new CellLayoutLayoutParams(p);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001900 }
1901
Samuel Fufa1e2d0042019-11-18 17:12:46 -08001902 /**
Tony Wickham86930612015-09-09 13:50:40 -07001903 * Returns whether an item can be placed in this CellLayout (after rearranging and/or resizing
1904 * if necessary).
1905 */
1906 public boolean hasReorderSolution(ItemInfo itemInfo) {
1907 int[] cellPoint = new int[2];
1908 // Check for a solution starting at every cell.
1909 for (int cellX = 0; cellX < getCountX(); cellX++) {
1910 for (int cellY = 0; cellY < getCountY(); cellY++) {
1911 cellToPoint(cellX, cellY, cellPoint);
1912 if (findReorderSolution(cellPoint[0], cellPoint[1], itemInfo.minSpanX,
1913 itemInfo.minSpanY, itemInfo.spanX, itemInfo.spanY, mDirectionVector, null,
Charlie Anderson46122392024-01-18 17:23:46 -05001914 true).isSolution) {
Tony Wickham86930612015-09-09 13:50:40 -07001915 return true;
1916 }
1917 }
1918 }
1919 return false;
1920 }
1921
Samuel Fufaa4211432020-02-25 18:47:54 -08001922 /**
1923 * Finds solution to accept hotseat migration to cell layout. commits solution if commitConfig
1924 */
1925 public boolean makeSpaceForHotseatMigration(boolean commitConfig) {
Samuel Fufaa4211432020-02-25 18:47:54 -08001926 int[] cellPoint = new int[2];
1927 int[] directionVector = new int[]{0, -1};
1928 cellToPoint(0, mCountY, cellPoint);
Charlie Anderson46122392024-01-18 17:23:46 -05001929 ItemConfiguration configuration = findReorderSolution(
1930 cellPoint[0] /* pixelX */,
1931 cellPoint[1] /* pixelY */,
1932 mCountX /* minSpanX */,
1933 1 /* minSpanY */,
1934 mCountX /* spanX */,
1935 1 /* spanY */,
1936 directionVector /* direction */,
1937 null /* dragView */,
1938 false /* decX */
1939 );
1940 if (configuration.isSolution) {
Samuel Fufaa4211432020-02-25 18:47:54 -08001941 if (commitConfig) {
1942 copySolutionToTempState(configuration, null);
Sunny Goyal711c5962021-06-23 12:36:18 -07001943 commitTempPlacement(null);
Samuel Fufa82bbdac2020-03-09 18:24:47 -07001944 // undo marking cells occupied since there is actually nothing being placed yet.
1945 mOccupied.markCells(0, mCountY - 1, mCountX, 1, false);
Samuel Fufaa4211432020-02-25 18:47:54 -08001946 }
1947 return true;
1948 }
1949 return false;
1950 }
1951
Samuel Fufa82bbdac2020-03-09 18:24:47 -07001952 /**
1953 * returns a copy of cell layout's grid occupancy
1954 */
1955 public GridOccupancy cloneGridOccupancy() {
1956 GridOccupancy occupancy = new GridOccupancy(mCountX, mCountY);
1957 mOccupied.copyTo(occupancy);
1958 return occupancy;
1959 }
1960
Sunny Goyal9ca9c132015-04-29 14:57:22 -07001961 public boolean isRegionVacant(int x, int y, int spanX, int spanY) {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001962 return mOccupied.isRegionVacant(x, y, spanX, spanY);
Sunny Goyal9ca9c132015-04-29 14:57:22 -07001963 }
Sebastian Franco9ea36d42023-09-21 13:56:42 -07001964
1965 public void setSpaceBetweenCellLayoutsPx(@Px int spaceBetweenCellLayoutsPx) {
1966 mSpaceBetweenCellLayoutsPx = spaceBetweenCellLayoutsPx;
1967 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001968}