blob: 1b4edf8f0909515a25a301d338e7906e01185b5d [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
Charlie Anderson438d4052023-04-25 14:38:57 -040019import static com.android.launcher3.LauncherState.EDIT_MODE;
Tony Wickham0ac045f2021-11-03 13:17:02 -070020import static com.android.launcher3.dragndrop.DraggableView.DRAGGABLE_ICON;
Tony Wickham12784902021-11-03 14:02:10 -070021import static com.android.launcher3.icons.IconNormalizer.ICON_VISIBLE_AREA_FACTOR;
Sunny Goyal82dfc152023-02-24 16:50:09 -080022import static com.android.launcher3.util.MultiTranslateDelegate.INDEX_REORDER_PREVIEW_OFFSET;
Sunny Goyalf0b6db72018-08-13 16:10:14 -070023
Joe Onorato4be866d2010-10-10 11:26:02 -070024import android.animation.Animator;
Michael Jurka629758f2012-06-14 16:18:21 -070025import android.animation.AnimatorListenerAdapter;
Chet Haase00397b12010-10-07 11:13:10 -070026import android.animation.TimeInterpolator;
Patrick Dubroyde7658b2010-09-27 11:15:43 -070027import android.animation.ValueAnimator;
28import android.animation.ValueAnimator.AnimatorUpdateListener;
Sunny Goyal726bee72018-03-05 12:54:24 -080029import android.annotation.SuppressLint;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080030import android.content.Context;
Joe Onorato79e56262009-09-21 15:23:04 -040031import android.content.res.Resources;
Sunny Goyalc13403c2016-11-18 23:44:48 -080032import android.content.res.TypedArray;
Winson Chungaafa03c2010-06-11 17:34:16 -070033import android.graphics.Canvas;
Andrew Flynn0dca1ec2012-02-29 13:33:22 -080034import android.graphics.Color;
Joe Onorato4be866d2010-10-10 11:26:02 -070035import android.graphics.Paint;
Patrick Dubroyde7658b2010-09-27 11:15:43 -070036import android.graphics.Point;
Adam Cohend9162062020-03-24 16:35:35 -070037import android.graphics.PointF;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080038import android.graphics.Rect;
Adam Cohenf7ca3b42021-02-22 11:03:58 -080039import android.graphics.RectF;
Patrick Dubroy6569f2c2010-07-12 14:25:18 -070040import android.graphics.drawable.Drawable;
Adam Cohen1462de32012-07-24 22:34:36 -070041import android.os.Parcelable;
Rajeev Kumar9962dbe2017-06-12 12:16:20 -070042import android.util.ArrayMap;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080043import android.util.AttributeSet;
Adam Cohenf7ca3b42021-02-22 11:03:58 -080044import android.util.FloatProperty;
Joe Onorato4be866d2010-10-10 11:26:02 -070045import android.util.Log;
Adam Cohen1462de32012-07-24 22:34:36 -070046import android.util.SparseArray;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080047import android.view.MotionEvent;
48import android.view.View;
49import android.view.ViewDebug;
50import android.view.ViewGroup;
Adam Cohenc9735cf2015-01-23 16:11:55 -080051import android.view.accessibility.AccessibilityEvent;
Sunny Goyal5bc6b6f2017-10-26 15:36:10 -070052
vadimt04f356f2019-02-14 18:46:36 -080053import androidx.annotation.IntDef;
Sebastian Franco5c8f8682023-11-14 09:52:41 -060054import androidx.annotation.Nullable;
Sebastian Franco9ea36d42023-09-21 13:56:42 -070055import androidx.annotation.Px;
Adam Cohenf7ca3b42021-02-22 11:03:58 -080056import androidx.core.graphics.ColorUtils;
vadimt04f356f2019-02-14 18:46:36 -080057import androidx.core.view.ViewCompat;
58
Kateryna Ivanova71203732023-05-24 15:09:00 +000059import com.android.app.animation.Interpolators;
Sunny Goyalaa8ef112015-06-12 20:04:41 -070060import com.android.launcher3.LauncherSettings.Favorites;
Sunny Goyale9b651e2015-04-24 11:44:51 -070061import com.android.launcher3.accessibility.DragAndDropAccessibilityDelegate;
Sebastian Francod4682992022-10-05 13:03:09 -050062import com.android.launcher3.celllayout.CellLayoutLayoutParams;
Sunny Goyal669b71f2023-01-27 14:37:07 -080063import com.android.launcher3.celllayout.CellPosMapper.CellPos;
Sebastian Franco5f0af4f2023-11-21 10:45:45 -060064import com.android.launcher3.celllayout.DelegatedCellDrawing;
65import com.android.launcher3.celllayout.ItemConfiguration;
Sebastian Francoe4c03452022-12-27 14:50:02 -060066import com.android.launcher3.celllayout.ReorderAlgorithm;
Sebastian Franco25f8e402023-12-02 13:54:05 -060067import com.android.launcher3.celllayout.ReorderParameters;
Sebastian Francof7654252024-01-18 11:50:50 -080068import com.android.launcher3.celllayout.ReorderPreviewAnimation;
Sunny Goyal3d706ad2017-03-06 16:56:39 -080069import com.android.launcher3.config.FeatureFlags;
Tony Wickham0ac045f2021-11-03 13:17:02 -070070import com.android.launcher3.dragndrop.DraggableView;
Jon Mirandaa0233f72017-06-22 18:34:45 -070071import com.android.launcher3.folder.PreviewBackground;
Sunny Goyale396abf2020-04-06 15:11:17 -070072import com.android.launcher3.model.data.ItemInfo;
Sebastian Francof153d912022-04-22 16:15:27 -050073import com.android.launcher3.model.data.LauncherAppWidgetInfo;
Sunny Goyalff4ba2d2016-04-02 14:12:34 -070074import com.android.launcher3.util.CellAndSpan;
75import com.android.launcher3.util.GridOccupancy;
Sunny Goyal82dfc152023-02-24 16:50:09 -080076import com.android.launcher3.util.MultiTranslateDelegate;
Sunny Goyale2fd14b2015-08-27 17:45:46 -070077import com.android.launcher3.util.ParcelableSparseArray;
Sunny Goyal9b29ca52017-02-17 10:39:44 -080078import com.android.launcher3.util.Themes;
Adam Cohen091440a2015-03-18 14:16:05 -070079import com.android.launcher3.util.Thunk;
Sunny Goyalab770a12018-11-14 15:17:26 -080080import com.android.launcher3.views.ActivityContext;
Steven Ng32427202021-04-19 18:12:12 +010081import com.android.launcher3.widget.LauncherAppWidgetHostView;
Sunny Goyal5bc6b6f2017-10-26 15:36:10 -070082
Sunny Goyalc13403c2016-11-18 23:44:48 -080083import java.lang.annotation.Retention;
84import java.lang.annotation.RetentionPolicy;
Adam Cohen69ce2e52011-07-03 19:25:21 -070085import java.util.ArrayList;
Adam Cohenc0dcf592011-06-01 15:30:43 -070086import java.util.Arrays;
Adam Cohend41fbf52012-02-16 23:53:59 -080087import java.util.Stack;
Adam Cohenc0dcf592011-06-01 15:30:43 -070088
Sunny Goyalc4d32012020-04-03 17:10:11 -070089public class CellLayout extends ViewGroup {
Tony Wickhama0628cc2015-10-14 15:23:04 -070090 private static final String TAG = "CellLayout";
91 private static final boolean LOGD = false;
Winson Chungaafa03c2010-06-11 17:34:16 -070092
Tony Wickhamec6fd6f2023-03-11 02:08:57 +000093 /** The color of the "leave-behind" shape when a folder is opened from Hotseat. */
94 private static final int FOLDER_LEAVE_BEHIND_COLOR = Color.argb(160, 245, 245, 245);
95
Sunny Goyalab770a12018-11-14 15:17:26 -080096 protected final ActivityContext mActivity;
Sunny Goyal4ffec482016-02-09 11:28:52 -080097 @ViewDebug.ExportedProperty(category = "launcher")
Adam Cohen091440a2015-03-18 14:16:05 -070098 @Thunk int mCellWidth;
Sunny Goyal4ffec482016-02-09 11:28:52 -080099 @ViewDebug.ExportedProperty(category = "launcher")
Adam Cohen091440a2015-03-18 14:16:05 -0700100 @Thunk int mCellHeight;
Winson Chung11a1a532013-09-13 11:14:45 -0700101 private int mFixedCellWidth;
102 private int mFixedCellHeight;
Jon Miranda228877d2021-02-09 11:05:00 -0500103 @ViewDebug.ExportedProperty(category = "launcher")
Sebastian Franco25423862023-03-10 10:50:37 -0800104 protected Point mBorderSpace;
Winson Chungaafa03c2010-06-11 17:34:16 -0700105
Sunny Goyal4ffec482016-02-09 11:28:52 -0800106 @ViewDebug.ExportedProperty(category = "launcher")
Sebastian Franco09589322022-11-02 15:25:58 -0700107 protected int mCountX;
Sunny Goyal4ffec482016-02-09 11:28:52 -0800108 @ViewDebug.ExportedProperty(category = "launcher")
Sebastian Franco09589322022-11-02 15:25:58 -0700109 protected int mCountY;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800110
Adam Cohen917e3882013-10-31 15:03:35 -0700111 private boolean mDropPending = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800112
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700113 // These are temporary variables to prevent having to allocate a new object just to
114 // return an (x, y) value from helper functions. Do NOT use them to maintain other state.
Adam Cohen091440a2015-03-18 14:16:05 -0700115 @Thunk final int[] mTmpPoint = new int[2];
Sunny Goyal2805e632015-05-20 15:35:32 -0700116 @Thunk final int[] mTempLocation = new int[2];
Sebastian Franco0dd5db82023-10-13 11:09:21 -0700117
118 @Thunk final Rect mTempOnDrawCellToRect = new Rect();
Adam Cohend9162062020-03-24 16:35:35 -0700119 final PointF mTmpPointF = new PointF();
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700120
Sebastian Franco09589322022-11-02 15:25:58 -0700121 protected GridOccupancy mOccupied;
Sebastian Francoe4c03452022-12-27 14:50:02 -0600122 public GridOccupancy mTmpOccupied;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800123
Michael Jurkadee05892010-07-27 10:01:56 -0700124 private OnTouchListener mInterceptTouchListener;
125
Samuel Fufa1e2d0042019-11-18 17:12:46 -0800126 private final ArrayList<DelegatedCellDrawing> mDelegatedCellDrawings = new ArrayList<>();
Jon Mirandaa0233f72017-06-22 18:34:45 -0700127 final PreviewBackground mFolderLeaveBehind = new PreviewBackground();
Adam Cohen69ce2e52011-07-03 19:25:21 -0700128
Sunny Goyalf5440cb2016-12-14 15:13:00 -0800129 private static final int[] BACKGROUND_STATE_ACTIVE = new int[] { android.R.attr.state_active };
Sunny Goyale15e2a82017-12-15 13:05:42 -0800130 private static final int[] BACKGROUND_STATE_DEFAULT = EMPTY_STATE_SET;
Sebastian Franco09589322022-11-02 15:25:58 -0700131 protected final Drawable mBackground;
Sunny Goyal2805e632015-05-20 15:35:32 -0700132
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700133 // These values allow a fixed measurement to be set on the CellLayout.
134 private int mFixedWidth = -1;
135 private int mFixedHeight = -1;
136
Michael Jurka33945b22010-12-21 18:19:38 -0800137 // If we're actively dragging something over this screen, mIsDragOverlapping is true
138 private boolean mIsDragOverlapping = false;
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700139
Winson Chung150fbab2010-09-29 17:14:26 -0700140 // These arrays are used to implement the drag visualization on x-large screens.
Joe Onorato4be866d2010-10-10 11:26:02 -0700141 // They are used as circular arrays, indexed by mDragOutlineCurrent.
Sebastian Francod4682992022-10-05 13:03:09 -0500142 @Thunk final CellLayoutLayoutParams[] mDragOutlines = new CellLayoutLayoutParams[4];
Rajeev Kumar9962dbe2017-06-12 12:16:20 -0700143 @Thunk final float[] mDragOutlineAlphas = new float[mDragOutlines.length];
144 private final InterruptibleInOutAnimator[] mDragOutlineAnims =
Joe Onorato4be866d2010-10-10 11:26:02 -0700145 new InterruptibleInOutAnimator[mDragOutlines.length];
Winson Chung150fbab2010-09-29 17:14:26 -0700146
147 // Used as an index into the above 3 arrays; indicates which is the most current value.
Joe Onorato4be866d2010-10-10 11:26:02 -0700148 private int mDragOutlineCurrent = 0;
Patrick Dubroy8e58e912010-10-14 13:21:48 -0700149 private final Paint mDragOutlinePaint = new Paint();
Winson Chung150fbab2010-09-29 17:14:26 -0700150
Sebastian Francod4682992022-10-05 13:03:09 -0500151 @Thunk final ArrayMap<CellLayoutLayoutParams, Animator> mReorderAnimators = new ArrayMap<>();
Adam Cohend9162062020-03-24 16:35:35 -0700152 @Thunk final ArrayMap<Reorderable, ReorderPreviewAnimation> mShakeAnimators = new ArrayMap<>();
Adam Cohen19f37922012-03-21 11:59:11 -0700153
154 private boolean mItemPlacementDirty = false;
Adam Cohenbfbfd262011-06-13 16:55:12 -0700155
Adam Cohenf7ca3b42021-02-22 11:03:58 -0800156 // Used to visualize the grid and drop locations
157 private boolean mVisualizeCells = false;
158 private boolean mVisualizeDropLocation = true;
159 private RectF mVisualizeGridRect = new RectF();
160 private Paint mVisualizeGridPaint = new Paint();
Adam Cohenf7ca3b42021-02-22 11:03:58 -0800161 private int mGridVisualizationRoundingRadius;
162 private float mGridAlpha = 0f;
163 private int mGridColor = 0;
Sebastian Franco09589322022-11-02 15:25:58 -0700164 protected float mSpringLoadedProgress = 0f;
Adam Cohenf7ca3b42021-02-22 11:03:58 -0800165 private float mScrollProgress = 0f;
166
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700167 // When a drag operation is in progress, holds the nearest cell to the touch point
168 private final int[] mDragCell = new int[2];
Adam Cohenf7ca3b42021-02-22 11:03:58 -0800169 private final int[] mDragCellSpan = new int[2];
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800170
Joe Onorato4be866d2010-10-10 11:26:02 -0700171 private boolean mDragging = false;
172
Rajeev Kumar9962dbe2017-06-12 12:16:20 -0700173 private final TimeInterpolator mEaseOutInterpolator;
Sebastian Franco09589322022-11-02 15:25:58 -0700174 protected final ShortcutAndWidgetContainer mShortcutsAndWidgets;
Sebastian Franco9ea36d42023-09-21 13:56:42 -0700175 @Px
176 protected int mSpaceBetweenCellLayoutsPx = 0;
Patrick Dubroyce34a972010-10-19 10:34:32 -0700177
Sunny Goyalc13403c2016-11-18 23:44:48 -0800178 @Retention(RetentionPolicy.SOURCE)
179 @IntDef({WORKSPACE, HOTSEAT, FOLDER})
180 public @interface ContainerType{}
181 public static final int WORKSPACE = 0;
182 public static final int HOTSEAT = 1;
183 public static final int FOLDER = 2;
184
185 @ContainerType private final int mContainerType;
186
Sebastian Francof7654252024-01-18 11:50:50 -0800187 public static final float DEFAULT_SCALE = 1f;
Andrew Flynn0dca1ec2012-02-29 13:33:22 -0800188
Adam Cohenfa3c58f2013-12-06 16:10:55 -0800189 public static final int MODE_SHOW_REORDER_HINT = 0;
190 public static final int MODE_DRAG_OVER = 1;
191 public static final int MODE_ON_DROP = 2;
192 public static final int MODE_ON_DROP_EXTERNAL = 3;
193 public static final int MODE_ACCEPT_DROP = 4;
Adam Cohen19f37922012-03-21 11:59:11 -0700194 private static final boolean DESTRUCTIVE_REORDER = false;
Adam Cohen482ed822012-03-02 14:15:13 -0800195 private static final boolean DEBUG_VISUALIZE_OCCUPIED = false;
196
Sebastian Francof7654252024-01-18 11:50:50 -0800197 public static final float REORDER_PREVIEW_MAGNITUDE = 0.12f;
198 public static final int REORDER_ANIMATION_DURATION = 150;
Sunny Goyalc13403c2016-11-18 23:44:48 -0800199 @Thunk final float mReorderPreviewAnimationMagnitude;
Adam Cohen19f37922012-03-21 11:59:11 -0700200
Rajeev Kumar9962dbe2017-06-12 12:16:20 -0700201 private final ArrayList<View> mIntersectingViews = new ArrayList<>();
202 private final Rect mOccupiedRect = new Rect();
Sebastian Francoe4c03452022-12-27 14:50:02 -0600203 public final int[] mDirectionVector = new int[2];
Steven Ng30dd1d62021-03-15 21:45:49 +0000204
Sebastian Franco53a15a42022-10-25 17:28:54 -0700205 ItemConfiguration mPreviousSolution = null;
Adam Cohenb209e632012-03-27 17:09:36 -0700206 private static final int INVALID_DIRECTION = -100;
Adam Cohen482ed822012-03-02 14:15:13 -0800207
Sunny Goyal2805e632015-05-20 15:35:32 -0700208 private final Rect mTempRect = new Rect();
Jonathan Miranda21930da2021-05-03 18:44:13 +0000209 private final RectF mTempRectF = new RectF();
Jon Miranda88b7f6a2021-05-03 16:49:53 -0700210 private final float[] mTmpFloatArray = new float[4];
Winson Chung3a6e7f32013-10-09 15:50:52 -0700211
Sunny Goyal73b5a272019-12-09 14:55:56 -0800212 private static final Paint sPaint = new Paint();
Romain Guy8a0bff52012-05-06 13:14:33 -0700213
Adam Cohenc9735cf2015-01-23 16:11:55 -0800214 // Related to accessible drag and drop
Adam Cohen6e7c37a2020-06-25 19:22:37 -0700215 DragAndDropAccessibilityDelegate mTouchHelper;
Adam Cohenc9735cf2015-01-23 16:11:55 -0800216
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
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800231 public CellLayout(Context context) {
232 this(context, null);
233 }
234
235 public CellLayout(Context context, AttributeSet attrs) {
236 this(context, attrs, 0);
237 }
238
239 public CellLayout(Context context, AttributeSet attrs, int defStyle) {
240 super(context, attrs, defStyle);
Sunny Goyalc13403c2016-11-18 23:44:48 -0800241 TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.CellLayout, defStyle, 0);
242 mContainerType = a.getInteger(R.styleable.CellLayout_containerType, WORKSPACE);
243 a.recycle();
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700244
245 // A ViewGroup usually does not draw, but CellLayout needs to draw a rectangle to show
246 // the user where a dragged item will land when dropped.
247 setWillNotDraw(false);
Romain Guyce3cbd12013-02-25 15:00:36 -0800248 setClipToPadding(false);
Pat Manningd0f729d2023-01-09 12:04:25 +0000249 setClipChildren(false);
Sunny Goyalab770a12018-11-14 15:17:26 -0800250 mActivity = ActivityContext.lookupContext(context);
Steven Ngcc505b82021-03-18 23:04:35 +0000251 DeviceProfile deviceProfile = mActivity.getDeviceProfile();
Michael Jurkaa63c4522010-08-19 13:52:27 -0700252
Thales Lima8cd020b2022-03-15 20:15:14 +0000253 resetCellSizeInternal(deviceProfile);
Sunny Goyalff4ba2d2016-04-02 14:12:34 -0700254
Steven Ngcc505b82021-03-18 23:04:35 +0000255 mCountX = deviceProfile.inv.numColumns;
256 mCountY = deviceProfile.inv.numRows;
Sunny Goyalff4ba2d2016-04-02 14:12:34 -0700257 mOccupied = new GridOccupancy(mCountX, mCountY);
258 mTmpOccupied = new GridOccupancy(mCountX, mCountY);
259
Samuel Fufa1e2d0042019-11-18 17:12:46 -0800260 mFolderLeaveBehind.mDelegateCellX = -1;
261 mFolderLeaveBehind.mDelegateCellY = -1;
Adam Cohenefca0272016-02-24 19:19:06 -0800262
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800263 setAlwaysDrawnWithCacheEnabled(false);
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700264
Adam Cohenf7ca3b42021-02-22 11:03:58 -0800265 Resources res = getResources();
266
267 mBackground = getContext().getDrawable(R.drawable.bg_celllayout);
Sunny Goyal2805e632015-05-20 15:35:32 -0700268 mBackground.setCallback(this);
Sunny Goyalaeb16432017-10-16 11:46:41 -0700269 mBackground.setAlpha(0);
Michael Jurka33945b22010-12-21 18:19:38 -0800270
Yogisha Dixitc0ac1dd2021-05-29 00:26:25 +0100271 mGridColor = Themes.getAttrColor(getContext(), R.attr.workspaceAccentColor);
Adam Cohenf7ca3b42021-02-22 11:03:58 -0800272 mGridVisualizationRoundingRadius =
273 res.getDimensionPixelSize(R.dimen.grid_visualization_rounding_radius);
Steven Ngcc505b82021-03-18 23:04:35 +0000274 mReorderPreviewAnimationMagnitude = (REORDER_PREVIEW_MAGNITUDE * deviceProfile.iconSizePx);
Adam Cohen19f37922012-03-21 11:59:11 -0700275
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700276 // Initialize the data structures used for the drag visualization.
Kateryna Ivanova71203732023-05-24 15:09:00 +0000277 mEaseOutInterpolator = Interpolators.DECELERATE_QUINT; // Quint ease out
Winson Chungb8c69f32011-10-19 21:36:08 -0700278 mDragCell[0] = mDragCell[1] = -1;
Adam Cohenf7ca3b42021-02-22 11:03:58 -0800279 mDragCellSpan[0] = mDragCellSpan[1] = -1;
Joe Onorato4be866d2010-10-10 11:26:02 -0700280 for (int i = 0; i < mDragOutlines.length; i++) {
Sunny Goyal669b71f2023-01-27 14:37:07 -0800281 mDragOutlines[i] = new CellLayoutLayoutParams(0, 0, 0, 0);
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700282 }
Mario Bertschler54ba6012017-06-08 10:53:53 -0700283 mDragOutlinePaint.setColor(Themes.getAttrColor(context, R.attr.workspaceTextColor));
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700284
285 // When dragging things around the home screens, we show a green outline of
286 // where the item will land. The outlines gradually fade out, leaving a trail
287 // behind the drag path.
288 // Set up all the animations that are used to implement this fading.
289 final int duration = res.getInteger(R.integer.config_dragOutlineFadeTime);
Chet Haase472b2812010-10-14 07:02:04 -0700290 final float fromAlphaValue = 0;
291 final float toAlphaValue = (float)res.getInteger(R.integer.config_dragOutlineMaxAlpha);
Joe Onorato4be866d2010-10-10 11:26:02 -0700292
Patrick Dubroy8e58e912010-10-14 13:21:48 -0700293 Arrays.fill(mDragOutlineAlphas, fromAlphaValue);
Joe Onorato4be866d2010-10-10 11:26:02 -0700294
295 for (int i = 0; i < mDragOutlineAnims.length; i++) {
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700296 final InterruptibleInOutAnimator anim =
Sebastian Francof153d912022-04-22 16:15:27 -0500297 new InterruptibleInOutAnimator(duration, fromAlphaValue, toAlphaValue);
Patrick Dubroyce34a972010-10-19 10:34:32 -0700298 anim.getAnimator().setInterpolator(mEaseOutInterpolator);
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700299 final int thisIndex = i;
Chet Haase472b2812010-10-14 07:02:04 -0700300 anim.getAnimator().addUpdateListener(new AnimatorUpdateListener() {
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700301 public void onAnimationUpdate(ValueAnimator animation) {
Joe Onorato4be866d2010-10-10 11:26:02 -0700302 // If an animation is started and then stopped very quickly, we can still
303 // get spurious updates we've cleared the tag. Guard against this.
Adam Cohenf7ca3b42021-02-22 11:03:58 -0800304 mDragOutlineAlphas[thisIndex] = (Float) animation.getAnimatedValue();
305 CellLayout.this.invalidate();
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700306 }
307 });
Joe Onorato4be866d2010-10-10 11:26:02 -0700308 // The animation holds a reference to the drag outline bitmap as long is it's
309 // running. This way the bitmap can be GCed when the animations are complete.
Joe Onorato4be866d2010-10-10 11:26:02 -0700310 mDragOutlineAnims[i] = anim;
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700311 }
Patrick Dubroyce34a972010-10-19 10:34:32 -0700312
Sunny Goyalc13403c2016-11-18 23:44:48 -0800313 mShortcutsAndWidgets = new ShortcutAndWidgetContainer(context, mContainerType);
Jon Miranda228877d2021-02-09 11:05:00 -0500314 mShortcutsAndWidgets.setCellDimensions(mCellWidth, mCellHeight, mCountX, mCountY,
Thales Lima78d00ad2021-09-30 11:29:06 +0100315 mBorderSpace);
Michael Jurkaa52570f2012-03-20 03:18:20 -0700316 addView(mShortcutsAndWidgets);
Michael Jurka18014792010-10-14 09:01:34 -0700317 }
318
Sunny Goyal9b180102020-03-11 10:02:29 -0700319 /**
320 * Sets or clears a delegate used for accessible drag and drop
321 */
322 public void setDragAndDropAccessibilityDelegate(DragAndDropAccessibilityDelegate delegate) {
323 setOnClickListener(delegate);
Sunny Goyal9b180102020-03-11 10:02:29 -0700324 ViewCompat.setAccessibilityDelegate(this, delegate);
325
Adam Cohen6e7c37a2020-06-25 19:22:37 -0700326 mTouchHelper = delegate;
327 int accessibilityFlag = mTouchHelper != null
Sunny Goyal9b180102020-03-11 10:02:29 -0700328 ? IMPORTANT_FOR_ACCESSIBILITY_YES : IMPORTANT_FOR_ACCESSIBILITY_NO;
329 setImportantForAccessibility(accessibilityFlag);
330 getShortcutsAndWidgets().setImportantForAccessibility(accessibilityFlag);
Adam Cohenc9735cf2015-01-23 16:11:55 -0800331
Sunny Goyal384b5782021-02-09 22:50:02 -0800332 // ExploreByTouchHelper sets focusability. Clear it when the delegate is cleared.
333 setFocusable(delegate != null);
Adam Cohenc9735cf2015-01-23 16:11:55 -0800334 // Invalidate the accessibility hierarchy
335 if (getParent() != null) {
336 getParent().notifySubtreeAccessibilityStateChanged(
337 this, this, AccessibilityEvent.CONTENT_CHANGE_TYPE_SUBTREE);
338 }
339 }
340
Sunny Goyala4647b62021-02-02 13:45:34 -0800341 /**
342 * Returns the currently set accessibility delegate
343 */
344 public DragAndDropAccessibilityDelegate getDragAndDropAccessibilityDelegate() {
345 return mTouchHelper;
346 }
347
Adam Cohenc9735cf2015-01-23 16:11:55 -0800348 @Override
Adam Cohen6e7c37a2020-06-25 19:22:37 -0700349 public boolean dispatchHoverEvent(MotionEvent event) {
350 // Always attempt to dispatch hover events to accessibility first.
351 if (mTouchHelper != null && mTouchHelper.dispatchHoverEvent(event)) {
352 return true;
353 }
354 return super.dispatchHoverEvent(event);
355 }
356
357 @Override
Adam Cohenc9735cf2015-01-23 16:11:55 -0800358 public boolean onInterceptTouchEvent(MotionEvent ev) {
Winson Chungf9935182020-10-23 09:26:44 -0700359 return mTouchHelper != null
360 || (mInterceptTouchListener != null && mInterceptTouchListener.onTouch(this, ev));
Adam Cohenc9735cf2015-01-23 16:11:55 -0800361 }
362
Chris Craik01f2d7f2013-10-01 14:41:56 -0700363 public void enableHardwareLayer(boolean hasLayer) {
364 mShortcutsAndWidgets.setLayerType(hasLayer ? LAYER_TYPE_HARDWARE : LAYER_TYPE_NONE, sPaint);
Michael Jurkad51f33a2012-06-28 15:35:26 -0700365 }
366
vadimt04f356f2019-02-14 18:46:36 -0800367 public boolean isHardwareLayerEnabled() {
368 return mShortcutsAndWidgets.getLayerType() == LAYER_TYPE_HARDWARE;
369 }
370
Thales Lima8cd020b2022-03-15 20:15:14 +0000371 /**
372 * Change sizes of cells
373 *
374 * @param width the new width of the cells
375 * @param height the new height of the cells
376 */
Winson Chung5f8afe62013-08-12 16:19:28 -0700377 public void setCellDimensions(int width, int height) {
Winson Chung11a1a532013-09-13 11:14:45 -0700378 mFixedCellWidth = mCellWidth = width;
379 mFixedCellHeight = mCellHeight = height;
Jon Miranda228877d2021-02-09 11:05:00 -0500380 mShortcutsAndWidgets.setCellDimensions(mCellWidth, mCellHeight, mCountX, mCountY,
Thales Lima78d00ad2021-09-30 11:29:06 +0100381 mBorderSpace);
Winson Chung5f8afe62013-08-12 16:19:28 -0700382 }
383
Thales Lima8cd020b2022-03-15 20:15:14 +0000384 private void resetCellSizeInternal(DeviceProfile deviceProfile) {
385 switch (mContainerType) {
386 case FOLDER:
Jordan Silva637f4eb2023-06-13 11:21:53 +0100387 mBorderSpace = new Point(deviceProfile.folderCellLayoutBorderSpacePx);
Thales Lima8cd020b2022-03-15 20:15:14 +0000388 break;
389 case HOTSEAT:
390 mBorderSpace = new Point(deviceProfile.hotseatBorderSpace,
391 deviceProfile.hotseatBorderSpace);
392 break;
393 case WORKSPACE:
394 default:
395 mBorderSpace = new Point(deviceProfile.cellLayoutBorderSpacePx);
396 break;
397 }
398
399 mCellWidth = mCellHeight = -1;
400 mFixedCellWidth = mFixedCellHeight = -1;
401 }
402
403 /**
404 * Reset the cell sizes and border space
405 */
406 public void resetCellSize(DeviceProfile deviceProfile) {
407 resetCellSizeInternal(deviceProfile);
408 requestLayout();
409 }
410
Adam Cohen2801caf2011-05-13 20:57:39 -0700411 public void setGridSize(int x, int y) {
412 mCountX = x;
413 mCountY = y;
Sunny Goyalff4ba2d2016-04-02 14:12:34 -0700414 mOccupied = new GridOccupancy(mCountX, mCountY);
415 mTmpOccupied = new GridOccupancy(mCountX, mCountY);
Jon Miranda228877d2021-02-09 11:05:00 -0500416 mShortcutsAndWidgets.setCellDimensions(mCellWidth, mCellHeight, mCountX, mCountY,
Thales Lima78d00ad2021-09-30 11:29:06 +0100417 mBorderSpace);
Adam Cohen76fc0852011-06-17 13:26:23 -0700418 requestLayout();
Adam Cohen2801caf2011-05-13 20:57:39 -0700419 }
420
Adam Cohen2374abf2013-04-16 14:56:57 -0700421 // Set whether or not to invert the layout horizontally if the layout is in RTL mode.
422 public void setInvertIfRtl(boolean invert) {
423 mShortcutsAndWidgets.setInvertIfRtl(invert);
424 }
425
Adam Cohen917e3882013-10-31 15:03:35 -0700426 public void setDropPending(boolean pending) {
427 mDropPending = pending;
428 }
429
430 public boolean isDropPending() {
431 return mDropPending;
432 }
433
Adam Cohenc50438c2014-08-19 17:43:05 -0700434 void setIsDragOverlapping(boolean isDragOverlapping) {
435 if (mIsDragOverlapping != isDragOverlapping) {
436 mIsDragOverlapping = isDragOverlapping;
Sunny Goyalf5440cb2016-12-14 15:13:00 -0800437 mBackground.setState(mIsDragOverlapping
438 ? BACKGROUND_STATE_ACTIVE : BACKGROUND_STATE_DEFAULT);
Adam Cohenc50438c2014-08-19 17:43:05 -0700439 invalidate();
440 }
441 }
442
Sunny Goyale2fd14b2015-08-27 17:45:46 -0700443 @Override
444 protected void dispatchSaveInstanceState(SparseArray<Parcelable> container) {
Sunny Goyal7ce471b2017-08-02 03:37:39 -0700445 ParcelableSparseArray jail = getJailedArray(container);
446 super.dispatchSaveInstanceState(jail);
447 container.put(R.id.cell_layout_jail_id, jail);
Sunny Goyale2fd14b2015-08-27 17:45:46 -0700448 }
449
450 @Override
451 protected void dispatchRestoreInstanceState(SparseArray<Parcelable> container) {
Sunny Goyal7ce471b2017-08-02 03:37:39 -0700452 super.dispatchRestoreInstanceState(getJailedArray(container));
Sunny Goyale2fd14b2015-08-27 17:45:46 -0700453 }
454
Sunny Goyal7ce471b2017-08-02 03:37:39 -0700455 /**
456 * Wrap the SparseArray in another Parcelable so that the item ids do not conflict with our
457 * our internal resource ids
458 */
Sunny Goyale2fd14b2015-08-27 17:45:46 -0700459 private ParcelableSparseArray getJailedArray(SparseArray<Parcelable> container) {
460 final Parcelable parcelable = container.get(R.id.cell_layout_jail_id);
461 return parcelable instanceof ParcelableSparseArray ?
462 (ParcelableSparseArray) parcelable : new ParcelableSparseArray();
463 }
464
Tony Wickham0f97b782015-12-02 17:55:07 -0800465 public boolean getIsDragOverlapping() {
466 return mIsDragOverlapping;
467 }
468
Jeff Sharkey83f111d2009-04-20 21:03:13 -0700469 @Override
Patrick Dubroy1262e362010-10-06 15:49:50 -0700470 protected void onDraw(Canvas canvas) {
Michael Jurka3e7c7632010-10-02 16:01:03 -0700471 // When we're large, we are either drawn in a "hover" state (ie when dragging an item to
472 // a neighboring page) or with just a normal background (if backgroundAlpha > 0.0f)
473 // When we're small, we are either drawn normally or in the "accepts drops" state (during
474 // a drag). However, we also drag the mini hover background *over* one of those two
475 // backgrounds
Sunny Goyalaeb16432017-10-16 11:46:41 -0700476 if (mBackground.getAlpha() > 0) {
Sunny Goyal2805e632015-05-20 15:35:32 -0700477 mBackground.draw(canvas);
Michael Jurkaa63c4522010-08-19 13:52:27 -0700478 }
Romain Guya6abce82009-11-10 02:54:41 -0800479
Adam Cohen482ed822012-03-02 14:15:13 -0800480 if (DEBUG_VISUALIZE_OCCUPIED) {
Tony Wickham0ac045f2021-11-03 13:17:02 -0700481 Rect cellBounds = new Rect();
482 // Will contain the bounds of the cell including spacing between cells.
483 Rect cellBoundsWithSpacing = new Rect();
Tony Wickham12784902021-11-03 14:02:10 -0700484 int[] targetCell = new int[2];
Tony Wickham0ac045f2021-11-03 13:17:02 -0700485 int[] cellCenter = new int[2];
486 Paint debugPaint = new Paint();
487 debugPaint.setStrokeWidth(Utilities.dpToPx(1));
488 for (int x = 0; x < mCountX; x++) {
489 for (int y = 0; y < mCountY; y++) {
490 if (!mOccupied.cells[x][y]) {
491 continue;
Adam Cohen482ed822012-03-02 14:15:13 -0800492 }
Tony Wickham12784902021-11-03 14:02:10 -0700493 targetCell[0] = x;
494 targetCell[1] = y;
Tony Wickham0ac045f2021-11-03 13:17:02 -0700495
Tony Wickham12784902021-11-03 14:02:10 -0700496 boolean canCreateFolder = canCreateFolder(getChildAt(x, y));
Tony Wickham0ac045f2021-11-03 13:17:02 -0700497 cellToRect(x, y, 1, 1, cellBounds);
498 cellBoundsWithSpacing.set(cellBounds);
499 cellBoundsWithSpacing.inset(-mBorderSpace.x / 2, -mBorderSpace.y / 2);
Tony Wickham3cfa5ed2021-11-03 13:20:43 -0700500 getWorkspaceCellVisualCenter(x, y, cellCenter);
Tony Wickham0ac045f2021-11-03 13:17:02 -0700501
502 canvas.save();
503 canvas.clipRect(cellBoundsWithSpacing);
504
505 // Draw reorder drag target.
506 debugPaint.setColor(Color.RED);
Sebastian Franco6e1024e2022-07-29 13:46:49 -0700507 canvas.drawCircle(cellCenter[0], cellCenter[1],
508 getReorderRadius(targetCell, 1, 1), debugPaint);
Tony Wickham0ac045f2021-11-03 13:17:02 -0700509
510 // Draw folder creation drag target.
511 if (canCreateFolder) {
512 debugPaint.setColor(Color.GREEN);
513 canvas.drawCircle(cellCenter[0], cellCenter[1],
Tony Wickham12784902021-11-03 14:02:10 -0700514 getFolderCreationRadius(targetCell), debugPaint);
Tony Wickham0ac045f2021-11-03 13:17:02 -0700515 }
516
517 canvas.restore();
Adam Cohen482ed822012-03-02 14:15:13 -0800518 }
519 }
520 }
521
Samuel Fufa1e2d0042019-11-18 17:12:46 -0800522 for (int i = 0; i < mDelegatedCellDrawings.size(); i++) {
523 DelegatedCellDrawing cellDrawing = mDelegatedCellDrawings.get(i);
524 cellToPoint(cellDrawing.mDelegateCellX, cellDrawing.mDelegateCellY, mTempLocation);
Adam Cohenefca0272016-02-24 19:19:06 -0800525 canvas.save();
526 canvas.translate(mTempLocation[0], mTempLocation[1]);
Samuel Fufa1e2d0042019-11-18 17:12:46 -0800527 cellDrawing.drawUnderItem(canvas);
Adam Cohenefca0272016-02-24 19:19:06 -0800528 canvas.restore();
Adam Cohen69ce2e52011-07-03 19:25:21 -0700529 }
Adam Cohenc51934b2011-07-26 21:07:43 -0700530
Samuel Fufa1e2d0042019-11-18 17:12:46 -0800531 if (mFolderLeaveBehind.mDelegateCellX >= 0 && mFolderLeaveBehind.mDelegateCellY >= 0) {
532 cellToPoint(mFolderLeaveBehind.mDelegateCellX,
533 mFolderLeaveBehind.mDelegateCellY, mTempLocation);
Adam Cohenefca0272016-02-24 19:19:06 -0800534 canvas.save();
535 canvas.translate(mTempLocation[0], mTempLocation[1]);
Tony Wickhamec6fd6f2023-03-11 02:08:57 +0000536 mFolderLeaveBehind.drawLeaveBehind(canvas, FOLDER_LEAVE_BEHIND_COLOR);
Adam Cohenefca0272016-02-24 19:19:06 -0800537 canvas.restore();
Adam Cohenc51934b2011-07-26 21:07:43 -0700538 }
Adam Cohen65086992020-02-19 08:40:49 -0800539
Adam Cohenf7ca3b42021-02-22 11:03:58 -0800540 if (mVisualizeCells || mVisualizeDropLocation) {
Adam Cohen65086992020-02-19 08:40:49 -0800541 visualizeGrid(canvas);
542 }
543 }
544
Adam Cohenf7ca3b42021-02-22 11:03:58 -0800545 /**
Tony Wickham12784902021-11-03 14:02:10 -0700546 * Returns whether dropping an icon on the given View can create (or add to) a folder.
547 */
548 private boolean canCreateFolder(View child) {
549 return child instanceof DraggableView
550 && ((DraggableView) child).getViewType() == DRAGGABLE_ICON;
551 }
552
553 /**
Adam Cohenf7ca3b42021-02-22 11:03:58 -0800554 * Indicates the progress of the Workspace entering the SpringLoaded state; allows the
555 * CellLayout to update various visuals for this state.
556 *
557 * @param progress
558 */
559 public void setSpringLoadedProgress(float progress) {
560 if (Float.compare(progress, mSpringLoadedProgress) != 0) {
561 mSpringLoadedProgress = progress;
fbarone74256b2023-04-10 14:50:31 -0700562 updateBgAlpha();
Adam Cohenf7ca3b42021-02-22 11:03:58 -0800563 setGridAlpha(progress);
564 }
565 }
566
567 /**
568 * See setSpringLoadedProgress
569 * @return progress
570 */
571 public float getSpringLoadedProgress() {
572 return mSpringLoadedProgress;
573 }
574
Sebastian Franco09589322022-11-02 15:25:58 -0700575 protected void updateBgAlpha() {
Charlie Anderson438d4052023-04-25 14:38:57 -0400576 if (!getWorkspace().mLauncher.isInState(EDIT_MODE)) {
577 mBackground.setAlpha((int) (mSpringLoadedProgress * 255));
578 }
Adam Cohenf7ca3b42021-02-22 11:03:58 -0800579 }
580
581 /**
582 * Set the progress of this page's scroll
583 *
584 * @param progress 0 if the screen is centered, +/-1 if it is to the right / left respectively
585 */
586 public void setScrollProgress(float progress) {
587 if (Float.compare(Math.abs(progress), mScrollProgress) != 0) {
588 mScrollProgress = Math.abs(progress);
fbarone74256b2023-04-10 14:50:31 -0700589 updateBgAlpha();
Adam Cohenf7ca3b42021-02-22 11:03:58 -0800590 }
591 }
592
593 private void setGridAlpha(float gridAlpha) {
594 if (Float.compare(gridAlpha, mGridAlpha) != 0) {
595 mGridAlpha = gridAlpha;
596 invalidate();
597 }
598 }
599
Adam Cohen65086992020-02-19 08:40:49 -0800600 protected void visualizeGrid(Canvas canvas) {
Adam Cohen0c4d2782021-04-29 15:56:13 -0700601 DeviceProfile dp = mActivity.getDeviceProfile();
Alex Chau51da2192022-05-20 13:32:10 +0100602 int paddingX = Math.min((mCellWidth - dp.iconSizePx) / 2, dp.gridVisualizationPaddingX);
603 int paddingY = Math.min((mCellHeight - dp.iconSizePx) / 2, dp.gridVisualizationPaddingY);
Adam Cohen65086992020-02-19 08:40:49 -0800604
Adam Cohenf7ca3b42021-02-22 11:03:58 -0800605 mVisualizeGridPaint.setStrokeWidth(8);
Adam Cohen65086992020-02-19 08:40:49 -0800606
Sebastian Franco0dd5db82023-10-13 11:09:21 -0700607 // This is used for debugging purposes only
Adam Cohenf7ca3b42021-02-22 11:03:58 -0800608 if (mVisualizeCells) {
Sebastian Franco0dd5db82023-10-13 11:09:21 -0700609 int paintAlpha = (int) (120 * mGridAlpha);
610 mVisualizeGridPaint.setColor(ColorUtils.setAlphaComponent(mGridColor, paintAlpha));
Adam Cohenf7ca3b42021-02-22 11:03:58 -0800611 for (int i = 0; i < mCountX; i++) {
612 for (int j = 0; j < mCountY; j++) {
Sebastian Franco0dd5db82023-10-13 11:09:21 -0700613 cellToRect(i, j, 1, 1, mTempOnDrawCellToRect);
614 mVisualizeGridRect.set(mTempOnDrawCellToRect);
615 mVisualizeGridRect.inset(paddingX, paddingY);
Adam Cohenf7ca3b42021-02-22 11:03:58 -0800616 mVisualizeGridPaint.setStyle(Paint.Style.FILL);
617 canvas.drawRoundRect(mVisualizeGridRect, mGridVisualizationRoundingRadius,
618 mGridVisualizationRoundingRadius, mVisualizeGridPaint);
619 }
620 }
621 }
Adam Cohen65086992020-02-19 08:40:49 -0800622
fbarone74256b2023-04-10 14:50:31 -0700623 if (mVisualizeDropLocation) {
Adam Cohenf7ca3b42021-02-22 11:03:58 -0800624 for (int i = 0; i < mDragOutlines.length; i++) {
625 final float alpha = mDragOutlineAlphas[i];
626 if (alpha <= 0) continue;
Sebastian Franco0dd5db82023-10-13 11:09:21 -0700627 CellLayoutLayoutParams params = mDragOutlines[i];
628 cellToRect(params.getCellX(), params.getCellY(), params.cellHSpan, params.cellVSpan,
629 mTempOnDrawCellToRect);
630 mVisualizeGridRect.set(mTempOnDrawCellToRect);
631 mVisualizeGridRect.inset(paddingX, paddingY);
Adam Cohen65086992020-02-19 08:40:49 -0800632
Adam Cohenf7ca3b42021-02-22 11:03:58 -0800633 mVisualizeGridPaint.setAlpha(255);
Adam Cohen65086992020-02-19 08:40:49 -0800634 mVisualizeGridPaint.setStyle(Paint.Style.STROKE);
Adam Cohenf7ca3b42021-02-22 11:03:58 -0800635 mVisualizeGridPaint.setColor(Color.argb((int) (alpha),
636 Color.red(mGridColor), Color.green(mGridColor), Color.blue(mGridColor)));
Adam Cohen65086992020-02-19 08:40:49 -0800637
Sebastian Franco9ea36d42023-09-21 13:56:42 -0700638 canvas.save();
639 canvas.translate(getMarginForGivenCellParams(params), 0);
Adam Cohenf7ca3b42021-02-22 11:03:58 -0800640 canvas.drawRoundRect(mVisualizeGridRect, mGridVisualizationRoundingRadius,
641 mGridVisualizationRoundingRadius, mVisualizeGridPaint);
Sebastian Franco9ea36d42023-09-21 13:56:42 -0700642 canvas.restore();
Adam Cohen65086992020-02-19 08:40:49 -0800643 }
644 }
Adam Cohen69ce2e52011-07-03 19:25:21 -0700645 }
646
Sebastian Franco9ea36d42023-09-21 13:56:42 -0700647 protected float getMarginForGivenCellParams(CellLayoutLayoutParams params) {
648 return 0;
649 }
650
Adam Cohenefca0272016-02-24 19:19:06 -0800651 @Override
652 protected void dispatchDraw(Canvas canvas) {
653 super.dispatchDraw(canvas);
654
Samuel Fufa1e2d0042019-11-18 17:12:46 -0800655 for (int i = 0; i < mDelegatedCellDrawings.size(); i++) {
656 DelegatedCellDrawing bg = mDelegatedCellDrawings.get(i);
657 cellToPoint(bg.mDelegateCellX, bg.mDelegateCellY, mTempLocation);
658 canvas.save();
659 canvas.translate(mTempLocation[0], mTempLocation[1]);
660 bg.drawOverItem(canvas);
661 canvas.restore();
Adam Cohenefca0272016-02-24 19:19:06 -0800662 }
Adam Cohen69ce2e52011-07-03 19:25:21 -0700663 }
664
Samuel Fufa1e2d0042019-11-18 17:12:46 -0800665 /**
666 * Add Delegated cell drawing
667 */
668 public void addDelegatedCellDrawing(DelegatedCellDrawing bg) {
669 mDelegatedCellDrawings.add(bg);
Adam Cohenefca0272016-02-24 19:19:06 -0800670 }
Samuel Fufa1e2d0042019-11-18 17:12:46 -0800671
672 /**
673 * Remove item from DelegatedCellDrawings
674 */
675 public void removeDelegatedCellDrawing(DelegatedCellDrawing bg) {
676 mDelegatedCellDrawings.remove(bg);
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700677 }
678
Adam Cohenc51934b2011-07-26 21:07:43 -0700679 public void setFolderLeaveBehindCell(int x, int y) {
Adam Cohenefca0272016-02-24 19:19:06 -0800680 View child = getChildAt(x, y);
Sunny Goyalab770a12018-11-14 15:17:26 -0800681 mFolderLeaveBehind.setup(getContext(), mActivity, null,
Adam Cohenefca0272016-02-24 19:19:06 -0800682 child.getMeasuredWidth(), child.getPaddingTop());
683
Samuel Fufa1e2d0042019-11-18 17:12:46 -0800684 mFolderLeaveBehind.mDelegateCellX = x;
685 mFolderLeaveBehind.mDelegateCellY = y;
Adam Cohenc51934b2011-07-26 21:07:43 -0700686 invalidate();
687 }
688
689 public void clearFolderLeaveBehind() {
Samuel Fufa1e2d0042019-11-18 17:12:46 -0800690 mFolderLeaveBehind.mDelegateCellX = -1;
691 mFolderLeaveBehind.mDelegateCellY = -1;
Adam Cohenc51934b2011-07-26 21:07:43 -0700692 invalidate();
693 }
694
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700695 @Override
Michael Jurkae6235dd2011-10-04 15:02:05 -0700696 public boolean shouldDelayChildPressedState() {
697 return false;
698 }
699
Adam Cohen1462de32012-07-24 22:34:36 -0700700 public void restoreInstanceState(SparseArray<Parcelable> states) {
Sunny Goyal33a152f2014-07-22 12:13:14 -0700701 try {
702 dispatchRestoreInstanceState(states);
703 } catch (IllegalArgumentException ex) {
Zak Cohen3eeb41d2020-02-14 14:15:13 -0800704 if (FeatureFlags.IS_STUDIO_BUILD) {
Sunny Goyal33a152f2014-07-22 12:13:14 -0700705 throw ex;
706 }
707 // Mismatched viewId / viewType preventing restore. Skip restore on production builds.
708 Log.e(TAG, "Ignoring an error while restoring a view instance state", ex);
709 }
Adam Cohen1462de32012-07-24 22:34:36 -0700710 }
711
Michael Jurkae6235dd2011-10-04 15:02:05 -0700712 @Override
Jeff Sharkey83f111d2009-04-20 21:03:13 -0700713 public void cancelLongPress() {
714 super.cancelLongPress();
715
716 // Cancel long press for all children
717 final int count = getChildCount();
718 for (int i = 0; i < count; i++) {
719 final View child = getChildAt(i);
720 child.cancelLongPress();
721 }
722 }
723
Michael Jurkadee05892010-07-27 10:01:56 -0700724 public void setOnInterceptTouchListener(View.OnTouchListener listener) {
725 mInterceptTouchListener = listener;
726 }
727
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800728 public int getCountX() {
Adam Cohend22015c2010-07-26 22:02:18 -0700729 return mCountX;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800730 }
731
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800732 public int getCountY() {
Adam Cohend22015c2010-07-26 22:02:18 -0700733 return mCountY;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800734 }
735
Sunny Goyalc13403c2016-11-18 23:44:48 -0800736 public boolean acceptsWidget() {
737 return mContainerType == WORKSPACE;
Sunny Goyale9b651e2015-04-24 11:44:51 -0700738 }
739
Sebastian Francod4682992022-10-05 13:03:09 -0500740 /**
741 * Adds the given view to the CellLayout
742 *
743 * @param child view to add.
744 * @param index index of the CellLayout children where to add the view.
745 * @param childId id of the view.
746 * @param params represent the logic of the view on the CellLayout.
747 * @param markCells if the occupied cells should be marked or not
748 * @return if adding the view was successful
749 */
750 public boolean addViewToCellLayout(View child, int index, int childId,
751 CellLayoutLayoutParams params, boolean markCells) {
752 final CellLayoutLayoutParams lp = params;
Winson Chungaafa03c2010-06-11 17:34:16 -0700753
Andrew Flynnde38e422012-05-08 11:22:15 -0700754 // Hotseat icons - remove text
Andrew Flynn0dca1ec2012-02-29 13:33:22 -0800755 if (child instanceof BubbleTextView) {
756 BubbleTextView bubbleChild = (BubbleTextView) child;
Jon Mirandaf1eae802017-10-04 11:23:33 -0700757 bubbleChild.setTextVisibility(mContainerType != HOTSEAT);
Andrew Flynn0dca1ec2012-02-29 13:33:22 -0800758 }
759
Sebastian Francof7654252024-01-18 11:50:50 -0800760 child.setScaleX(DEFAULT_SCALE);
761 child.setScaleY(DEFAULT_SCALE);
Adam Cohen307fe232012-08-16 17:55:58 -0700762
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800763 // Generate an id for each view, this assumes we have at most 256x256 cells
764 // per workspace screen
Sebastian Franco877088e2023-01-03 15:16:22 -0700765 if (lp.getCellX() >= 0 && lp.getCellX() <= mCountX - 1
766 && lp.getCellY() >= 0 && lp.getCellY() <= mCountY - 1) {
Winson Chungaafa03c2010-06-11 17:34:16 -0700767 // If the horizontal or vertical span is set to -1, it is taken to
768 // mean that it spans the extent of the CellLayout
Adam Cohend22015c2010-07-26 22:02:18 -0700769 if (lp.cellHSpan < 0) lp.cellHSpan = mCountX;
770 if (lp.cellVSpan < 0) lp.cellVSpan = mCountY;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800771
Winson Chungaafa03c2010-06-11 17:34:16 -0700772 child.setId(childId);
Tony Wickhama0628cc2015-10-14 15:23:04 -0700773 if (LOGD) {
774 Log.d(TAG, "Adding view to ShortcutsAndWidgetsContainer: " + child);
775 }
Sunny Goyalf7a29e82015-04-24 15:20:43 -0700776 mShortcutsAndWidgets.addView(child, index, lp);
Michael Jurkadee05892010-07-27 10:01:56 -0700777
Michael Jurkaf3ca3ab2010-10-20 17:08:24 -0700778 if (markCells) markCellsAsOccupiedForView(child);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700779
Winson Chungaafa03c2010-06-11 17:34:16 -0700780 return true;
781 }
782 return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800783 }
Michael Jurka3e7c7632010-10-02 16:01:03 -0700784
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800785 @Override
Michael Jurka0280c3b2010-09-17 15:00:07 -0700786 public void removeAllViews() {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -0700787 mOccupied.clear();
Michael Jurkaa52570f2012-03-20 03:18:20 -0700788 mShortcutsAndWidgets.removeAllViews();
Michael Jurka0280c3b2010-09-17 15:00:07 -0700789 }
790
791 @Override
792 public void removeAllViewsInLayout() {
Michael Jurkaa52570f2012-03-20 03:18:20 -0700793 if (mShortcutsAndWidgets.getChildCount() > 0) {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -0700794 mOccupied.clear();
Michael Jurkaa52570f2012-03-20 03:18:20 -0700795 mShortcutsAndWidgets.removeAllViewsInLayout();
Michael Jurka7cfc2822011-08-02 20:19:24 -0700796 }
Michael Jurka0280c3b2010-09-17 15:00:07 -0700797 }
798
799 @Override
800 public void removeView(View view) {
801 markCellsAsUnoccupiedForView(view);
Michael Jurkaa52570f2012-03-20 03:18:20 -0700802 mShortcutsAndWidgets.removeView(view);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700803 }
804
805 @Override
806 public void removeViewAt(int index) {
Michael Jurkaa52570f2012-03-20 03:18:20 -0700807 markCellsAsUnoccupiedForView(mShortcutsAndWidgets.getChildAt(index));
808 mShortcutsAndWidgets.removeViewAt(index);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700809 }
810
811 @Override
812 public void removeViewInLayout(View view) {
813 markCellsAsUnoccupiedForView(view);
Michael Jurkaa52570f2012-03-20 03:18:20 -0700814 mShortcutsAndWidgets.removeViewInLayout(view);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700815 }
816
817 @Override
818 public void removeViews(int start, int count) {
819 for (int i = start; i < start + count; i++) {
Michael Jurkaa52570f2012-03-20 03:18:20 -0700820 markCellsAsUnoccupiedForView(mShortcutsAndWidgets.getChildAt(i));
Michael Jurka0280c3b2010-09-17 15:00:07 -0700821 }
Michael Jurkaa52570f2012-03-20 03:18:20 -0700822 mShortcutsAndWidgets.removeViews(start, count);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700823 }
824
825 @Override
826 public void removeViewsInLayout(int start, int count) {
827 for (int i = start; i < start + count; i++) {
Michael Jurkaa52570f2012-03-20 03:18:20 -0700828 markCellsAsUnoccupiedForView(mShortcutsAndWidgets.getChildAt(i));
Michael Jurka0280c3b2010-09-17 15:00:07 -0700829 }
Michael Jurkaa52570f2012-03-20 03:18:20 -0700830 mShortcutsAndWidgets.removeViewsInLayout(start, count);
Michael Jurkaabded662011-03-04 12:06:57 -0800831 }
832
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700833 /**
Winson Chungaafa03c2010-06-11 17:34:16 -0700834 * Given a point, return the cell that strictly encloses that point
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800835 * @param x X coordinate of the point
836 * @param y Y coordinate of the point
837 * @param result Array of 2 ints to hold the x and y coordinate of the cell
838 */
Sunny Goyale9b651e2015-04-24 11:44:51 -0700839 public void pointToCellExact(int x, int y, int[] result) {
Winson Chung4b825dcd2011-06-19 12:41:22 -0700840 final int hStartPadding = getPaddingLeft();
841 final int vStartPadding = getPaddingTop();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800842
Sebastian Francob57c0b22022-06-28 13:54:35 -0700843 result[0] = (x - hStartPadding) / (mCellWidth + mBorderSpace.x);
844 result[1] = (y - vStartPadding) / (mCellHeight + mBorderSpace.y);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800845
Adam Cohend22015c2010-07-26 22:02:18 -0700846 final int xAxis = mCountX;
847 final int yAxis = mCountY;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800848
849 if (result[0] < 0) result[0] = 0;
850 if (result[0] >= xAxis) result[0] = xAxis - 1;
851 if (result[1] < 0) result[1] = 0;
852 if (result[1] >= yAxis) result[1] = yAxis - 1;
853 }
Winson Chungaafa03c2010-06-11 17:34:16 -0700854
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800855 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800856 * Given a cell coordinate, return the point that represents the upper left corner of that cell
Winson Chungaafa03c2010-06-11 17:34:16 -0700857 *
858 * @param cellX X coordinate of the cell
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800859 * @param cellY Y coordinate of the cell
Winson Chungaafa03c2010-06-11 17:34:16 -0700860 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800861 * @param result Array of 2 ints to hold the x and y coordinate of the point
862 */
863 void cellToPoint(int cellX, int cellY, int[] result) {
Jon Miranda228877d2021-02-09 11:05:00 -0500864 cellToRect(cellX, cellY, 1, 1, mTempRect);
865 result[0] = mTempRect.left;
866 result[1] = mTempRect.top;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800867 }
868
Adam Cohene3e27a82011-04-15 12:07:39 -0700869 /**
Adam Cohen482ed822012-03-02 14:15:13 -0800870 * Given a cell coordinate, return the point that represents the center of the cell
Adam Cohene3e27a82011-04-15 12:07:39 -0700871 *
872 * @param cellX X coordinate of the cell
873 * @param cellY Y coordinate of the cell
874 *
875 * @param result Array of 2 ints to hold the x and y coordinate of the point
876 */
877 void cellToCenterPoint(int cellX, int cellY, int[] result) {
Adam Cohen47a876d2012-03-19 13:21:41 -0700878 regionToCenterPoint(cellX, cellY, 1, 1, result);
879 }
880
881 /**
Tony Wickham0ac045f2021-11-03 13:17:02 -0700882 * Given a cell coordinate and span return the point that represents the center of the region
Adam Cohen47a876d2012-03-19 13:21:41 -0700883 *
884 * @param cellX X coordinate of the cell
885 * @param cellY Y coordinate of the cell
886 *
887 * @param result Array of 2 ints to hold the x and y coordinate of the point
888 */
Sebastian Franco45b39b52023-01-10 10:47:46 -0600889 public void regionToCenterPoint(int cellX, int cellY, int spanX, int spanY, int[] result) {
Jon Miranda228877d2021-02-09 11:05:00 -0500890 cellToRect(cellX, cellY, spanX, spanY, mTempRect);
891 result[0] = mTempRect.centerX();
892 result[1] = mTempRect.centerY();
Adam Cohen19f37922012-03-21 11:59:11 -0700893 }
894
Tony Wickham3cfa5ed2021-11-03 13:20:43 -0700895 /**
896 * Returns the distance between the given coordinate and the visual center of the given cell.
897 */
898 public float getDistanceFromWorkspaceCellVisualCenter(float x, float y, int[] cell) {
899 getWorkspaceCellVisualCenter(cell[0], cell[1], mTmpPoint);
Sunny Goyalf7a29e82015-04-24 15:20:43 -0700900 return (float) Math.hypot(x - mTmpPoint[0], y - mTmpPoint[1]);
Adam Cohen482ed822012-03-02 14:15:13 -0800901 }
902
Tony Wickham3cfa5ed2021-11-03 13:20:43 -0700903 private void getWorkspaceCellVisualCenter(int cellX, int cellY, int[] outPoint) {
904 View child = getChildAt(cellX, cellY);
905 if (child instanceof DraggableView) {
906 DraggableView draggableChild = (DraggableView) child;
907 if (draggableChild.getViewType() == DRAGGABLE_ICON) {
908 cellToPoint(cellX, cellY, outPoint);
909 draggableChild.getWorkspaceVisualDragBounds(mTempRect);
910 mTempRect.offset(outPoint[0], outPoint[1]);
911 outPoint[0] = mTempRect.centerX();
912 outPoint[1] = mTempRect.centerY();
913 return;
914 }
915 }
916 cellToCenterPoint(cellX, cellY, outPoint);
917 }
918
Tony Wickham0ac045f2021-11-03 13:17:02 -0700919 /**
920 * Returns the max distance from the center of a cell that can accept a drop to create a folder.
921 */
Tony Wickham12784902021-11-03 14:02:10 -0700922 public float getFolderCreationRadius(int[] targetCell) {
Tony Wickham0ac045f2021-11-03 13:17:02 -0700923 DeviceProfile grid = mActivity.getDeviceProfile();
Tony Wickham12784902021-11-03 14:02:10 -0700924 float iconVisibleRadius = ICON_VISIBLE_AREA_FACTOR * grid.iconSizePx / 2;
925 // Halfway between reorder radius and icon.
Sebastian Franco6e1024e2022-07-29 13:46:49 -0700926 return (getReorderRadius(targetCell, 1, 1) + iconVisibleRadius) / 2;
Tony Wickham12784902021-11-03 14:02:10 -0700927 }
928
929 /**
930 * Returns the max distance from the center of a cell that will start to reorder on drag over.
931 */
Sebastian Franco6e1024e2022-07-29 13:46:49 -0700932 public float getReorderRadius(int[] targetCell, int spanX, int spanY) {
Tony Wickham12784902021-11-03 14:02:10 -0700933 int[] centerPoint = mTmpPoint;
934 getWorkspaceCellVisualCenter(targetCell[0], targetCell[1], centerPoint);
935
936 Rect cellBoundsWithSpacing = mTempRect;
Sebastian Franco6e1024e2022-07-29 13:46:49 -0700937 cellToRect(targetCell[0], targetCell[1], spanX, spanY, cellBoundsWithSpacing);
Tony Wickham12784902021-11-03 14:02:10 -0700938 cellBoundsWithSpacing.inset(-mBorderSpace.x / 2, -mBorderSpace.y / 2);
939
Sebastian Francoc8392ea2022-10-28 16:38:37 -0700940 if (canCreateFolder(getChildAt(targetCell[0], targetCell[1])) && spanX == 1 && spanY == 1) {
Tony Wickham12784902021-11-03 14:02:10 -0700941 // Take only the circle in the smaller dimension, to ensure we don't start reordering
942 // too soon before accepting a folder drop.
943 int minRadius = centerPoint[0] - cellBoundsWithSpacing.left;
944 minRadius = Math.min(minRadius, centerPoint[1] - cellBoundsWithSpacing.top);
945 minRadius = Math.min(minRadius, cellBoundsWithSpacing.right - centerPoint[0]);
946 minRadius = Math.min(minRadius, cellBoundsWithSpacing.bottom - centerPoint[1]);
947 return minRadius;
948 }
949 // Take up the entire cell, including space between this cell and the adjacent ones.
Sebastian Francoc8392ea2022-10-28 16:38:37 -0700950 // Multiply by span to scale radius
951 return (float) Math.hypot(spanX * cellBoundsWithSpacing.width() / 2f,
952 spanY * cellBoundsWithSpacing.height() / 2f);
Tony Wickham0ac045f2021-11-03 13:17:02 -0700953 }
954
Adam Cohenf9c184a2016-01-15 16:47:43 -0800955 public int getCellWidth() {
Romain Guy84f296c2009-11-04 15:00:44 -0800956 return mCellWidth;
957 }
958
Sunny Goyal0b754e52017-08-07 07:42:45 -0700959 public int getCellHeight() {
Romain Guy84f296c2009-11-04 15:00:44 -0800960 return mCellHeight;
961 }
962
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700963 public void setFixedSize(int width, int height) {
964 mFixedWidth = width;
965 mFixedHeight = height;
966 }
967
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800968 @Override
969 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800970 int widthSpecMode = MeasureSpec.getMode(widthMeasureSpec);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800971 int heightSpecMode = MeasureSpec.getMode(heightMeasureSpec);
Winson Chung5f8afe62013-08-12 16:19:28 -0700972 int widthSize = MeasureSpec.getSize(widthMeasureSpec);
973 int heightSize = MeasureSpec.getSize(heightMeasureSpec);
Winson Chung2d75f122013-09-23 16:53:31 -0700974 int childWidthSize = widthSize - (getPaddingLeft() + getPaddingRight());
975 int childHeightSize = heightSize - (getPaddingTop() + getPaddingBottom());
Sunny Goyalae6e3182019-04-30 12:04:37 -0700976
Winson Chung11a1a532013-09-13 11:14:45 -0700977 if (mFixedCellWidth < 0 || mFixedCellHeight < 0) {
Thales Lima78d00ad2021-09-30 11:29:06 +0100978 int cw = DeviceProfile.calculateCellWidth(childWidthSize, mBorderSpace.x,
Jon Miranda228877d2021-02-09 11:05:00 -0500979 mCountX);
Thales Lima78d00ad2021-09-30 11:29:06 +0100980 int ch = DeviceProfile.calculateCellHeight(childHeightSize, mBorderSpace.y,
Jon Miranda228877d2021-02-09 11:05:00 -0500981 mCountY);
Winson Chung11a1a532013-09-13 11:14:45 -0700982 if (cw != mCellWidth || ch != mCellHeight) {
983 mCellWidth = cw;
984 mCellHeight = ch;
Jon Miranda228877d2021-02-09 11:05:00 -0500985 mShortcutsAndWidgets.setCellDimensions(mCellWidth, mCellHeight, mCountX, mCountY,
Thales Lima78d00ad2021-09-30 11:29:06 +0100986 mBorderSpace);
Winson Chung11a1a532013-09-13 11:14:45 -0700987 }
Winson Chung5f8afe62013-08-12 16:19:28 -0700988 }
Winson Chungaafa03c2010-06-11 17:34:16 -0700989
Winson Chung2d75f122013-09-23 16:53:31 -0700990 int newWidth = childWidthSize;
991 int newHeight = childHeightSize;
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700992 if (mFixedWidth > 0 && mFixedHeight > 0) {
993 newWidth = mFixedWidth;
994 newHeight = mFixedHeight;
995 } else if (widthSpecMode == MeasureSpec.UNSPECIFIED || heightSpecMode == MeasureSpec.UNSPECIFIED) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800996 throw new RuntimeException("CellLayout cannot have UNSPECIFIED dimensions");
997 }
998
Sunny Goyal4fe5a372015-05-14 19:55:10 -0700999 mShortcutsAndWidgets.measure(
1000 MeasureSpec.makeMeasureSpec(newWidth, MeasureSpec.EXACTLY),
1001 MeasureSpec.makeMeasureSpec(newHeight, MeasureSpec.EXACTLY));
1002
1003 int maxWidth = mShortcutsAndWidgets.getMeasuredWidth();
1004 int maxHeight = mShortcutsAndWidgets.getMeasuredHeight();
Winson Chung2d75f122013-09-23 16:53:31 -07001005 if (mFixedWidth > 0 && mFixedHeight > 0) {
1006 setMeasuredDimension(maxWidth, maxHeight);
1007 } else {
1008 setMeasuredDimension(widthSize, heightSize);
1009 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001010 }
1011
1012 @Override
Michael Jurka28750fb2010-09-24 17:43:49 -07001013 protected void onLayout(boolean changed, int l, int t, int r, int b) {
Tony Wickham26b01422015-11-10 14:44:32 -08001014 int left = getPaddingLeft();
Sunny Goyal7ce471b2017-08-02 03:37:39 -07001015 left += (int) Math.ceil(getUnusedHorizontalSpace() / 2f);
Sunny Goyal7c786f72016-06-01 14:08:21 -07001016 int right = r - l - getPaddingRight();
Sunny Goyal7ce471b2017-08-02 03:37:39 -07001017 right -= (int) Math.ceil(getUnusedHorizontalSpace() / 2f);
Sunny Goyal7c786f72016-06-01 14:08:21 -07001018
Winson Chung38848ca2013-10-08 12:03:44 -07001019 int top = getPaddingTop();
Sunny Goyal7c786f72016-06-01 14:08:21 -07001020 int bottom = b - t - getPaddingBottom();
Sunny Goyal4fe5a372015-05-14 19:55:10 -07001021
Sunny Goyal7c786f72016-06-01 14:08:21 -07001022 // Expand the background drawing bounds by the padding baked into the background drawable
1023 mBackground.getPadding(mTempRect);
1024 mBackground.setBounds(
Jon Miranda28032002017-07-13 16:18:56 -07001025 left - mTempRect.left - getPaddingLeft(),
1026 top - mTempRect.top - getPaddingTop(),
1027 right + mTempRect.right + getPaddingRight(),
1028 bottom + mTempRect.bottom + getPaddingBottom());
Sunny Goyalae6e3182019-04-30 12:04:37 -07001029
Sunny Goyalc4d32012020-04-03 17:10:11 -07001030 mShortcutsAndWidgets.layout(left, top, right, bottom);
Sunny Goyal7c786f72016-06-01 14:08:21 -07001031 }
1032
Tony Wickhama501d492015-11-03 18:05:01 -08001033 /**
1034 * Returns the amount of space left over after subtracting padding and cells. This space will be
1035 * 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 -05001036 * width in {@link DeviceProfile#calculateCellWidth(int, int, int)}.
Tony Wickhama501d492015-11-03 18:05:01 -08001037 */
1038 public int getUnusedHorizontalSpace() {
Jon Miranda228877d2021-02-09 11:05:00 -05001039 return getMeasuredWidth() - getPaddingLeft() - getPaddingRight() - (mCountX * mCellWidth)
Thales Lima78d00ad2021-09-30 11:29:06 +01001040 - ((mCountX - 1) * mBorderSpace.x);
Tony Wickhama501d492015-11-03 18:05:01 -08001041 }
1042
Sunny Goyal2805e632015-05-20 15:35:32 -07001043 @Override
1044 protected boolean verifyDrawable(Drawable who) {
Sunny Goyal7ce471b2017-08-02 03:37:39 -07001045 return super.verifyDrawable(who) || (who == mBackground);
Sunny Goyal2805e632015-05-20 15:35:32 -07001046 }
1047
Michael Jurkaa52570f2012-03-20 03:18:20 -07001048 public ShortcutAndWidgetContainer getShortcutsAndWidgets() {
Sunny Goyaldcbcc862014-08-12 15:58:36 -07001049 return mShortcutsAndWidgets;
Michael Jurkaa52570f2012-03-20 03:18:20 -07001050 }
1051
Jon Miranda228877d2021-02-09 11:05:00 -05001052 public View getChildAt(int cellX, int cellY) {
1053 return mShortcutsAndWidgets.getChildAt(cellX, cellY);
Patrick Dubroy440c3602010-07-13 17:50:32 -07001054 }
1055
Adam Cohen76fc0852011-06-17 13:26:23 -07001056 public boolean animateChildToPosition(final View child, int cellX, int cellY, int duration,
Adam Cohen482ed822012-03-02 14:15:13 -08001057 int delay, boolean permanent, boolean adjustOccupied) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07001058 ShortcutAndWidgetContainer clc = getShortcutsAndWidgets();
Adam Cohen482ed822012-03-02 14:15:13 -08001059
Adam Cohen1d13c0b2020-04-21 16:29:12 -07001060 if (clc.indexOfChild(child) != -1 && (child instanceof Reorderable)) {
Sebastian Francod4682992022-10-05 13:03:09 -05001061 final CellLayoutLayoutParams lp = (CellLayoutLayoutParams) child.getLayoutParams();
Adam Cohenbfbfd262011-06-13 16:55:12 -07001062 final ItemInfo info = (ItemInfo) child.getTag();
Adam Cohen1d13c0b2020-04-21 16:29:12 -07001063 final Reorderable item = (Reorderable) child;
Adam Cohenbfbfd262011-06-13 16:55:12 -07001064
1065 // We cancel any existing animations
1066 if (mReorderAnimators.containsKey(lp)) {
1067 mReorderAnimators.get(lp).cancel();
1068 mReorderAnimators.remove(lp);
1069 }
1070
Adam Cohen1d13c0b2020-04-21 16:29:12 -07001071
Adam Cohen482ed822012-03-02 14:15:13 -08001072 if (adjustOccupied) {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001073 GridOccupancy occupied = permanent ? mOccupied : mTmpOccupied;
Sebastian Franco877088e2023-01-03 15:16:22 -07001074 occupied.markCells(lp.getCellX(), lp.getCellY(), lp.cellHSpan, lp.cellVSpan, false);
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001075 occupied.markCells(cellX, cellY, lp.cellHSpan, lp.cellVSpan, true);
Adam Cohen482ed822012-03-02 14:15:13 -08001076 }
Adam Cohen1d13c0b2020-04-21 16:29:12 -07001077
1078 // Compute the new x and y position based on the new cellX and cellY
1079 // We leverage the actual layout logic in the layout params and hence need to modify
1080 // state and revert that state.
1081 final int oldX = lp.x;
1082 final int oldY = lp.y;
Adam Cohenbfbfd262011-06-13 16:55:12 -07001083 lp.isLockedToGrid = true;
Adam Cohen482ed822012-03-02 14:15:13 -08001084 if (permanent) {
Sunny Goyal669b71f2023-01-27 14:37:07 -08001085 lp.setCellX(cellX);
1086 lp.setCellY(cellY);
Adam Cohen482ed822012-03-02 14:15:13 -08001087 } else {
Sebastian Franco877088e2023-01-03 15:16:22 -07001088 lp.setTmpCellX(cellX);
1089 lp.setTmpCellY(cellY);
Adam Cohen482ed822012-03-02 14:15:13 -08001090 }
Jon Mirandae96798e2016-12-07 12:10:44 -08001091 clc.setupLp(child);
Adam Cohen482ed822012-03-02 14:15:13 -08001092 final int newX = lp.x;
1093 final int newY = lp.y;
Adam Cohen76fc0852011-06-17 13:26:23 -07001094 lp.x = oldX;
1095 lp.y = oldY;
Adam Cohen1d13c0b2020-04-21 16:29:12 -07001096 lp.isLockedToGrid = false;
1097 // End compute new x and y
1098
Sunny Goyal82dfc152023-02-24 16:50:09 -08001099 MultiTranslateDelegate mtd = item.getTranslateDelegate();
1100 float initPreviewOffsetX = mtd.getTranslationX(INDEX_REORDER_PREVIEW_OFFSET).getValue();
1101 float initPreviewOffsetY = mtd.getTranslationY(INDEX_REORDER_PREVIEW_OFFSET).getValue();
Adam Cohen1d13c0b2020-04-21 16:29:12 -07001102 final float finalPreviewOffsetX = newX - oldX;
1103 final float finalPreviewOffsetY = newY - oldY;
1104
Adam Cohen482ed822012-03-02 14:15:13 -08001105 // Exit early if we're not actually moving the view
Adam Cohen1d13c0b2020-04-21 16:29:12 -07001106 if (finalPreviewOffsetX == 0 && finalPreviewOffsetY == 0
1107 && initPreviewOffsetX == 0 && initPreviewOffsetY == 0) {
Adam Cohen482ed822012-03-02 14:15:13 -08001108 lp.isLockedToGrid = true;
1109 return true;
1110 }
1111
Sunny Goyal849c6a22018-08-08 16:33:46 -07001112 ValueAnimator va = ValueAnimator.ofFloat(0f, 1f);
Adam Cohen482ed822012-03-02 14:15:13 -08001113 va.setDuration(duration);
1114 mReorderAnimators.put(lp, va);
1115
1116 va.addUpdateListener(new AnimatorUpdateListener() {
1117 @Override
Adam Cohenbfbfd262011-06-13 16:55:12 -07001118 public void onAnimationUpdate(ValueAnimator animation) {
Jon Mirandae96798e2016-12-07 12:10:44 -08001119 float r = (Float) animation.getAnimatedValue();
Adam Cohen1d13c0b2020-04-21 16:29:12 -07001120 float x = (1 - r) * initPreviewOffsetX + r * finalPreviewOffsetX;
1121 float y = (1 - r) * initPreviewOffsetY + r * finalPreviewOffsetY;
Sunny Goyal82dfc152023-02-24 16:50:09 -08001122 item.getTranslateDelegate().setTranslation(INDEX_REORDER_PREVIEW_OFFSET, x, y);
Adam Cohenbfbfd262011-06-13 16:55:12 -07001123 }
1124 });
Adam Cohen482ed822012-03-02 14:15:13 -08001125 va.addListener(new AnimatorListenerAdapter() {
Adam Cohenbfbfd262011-06-13 16:55:12 -07001126 boolean cancelled = false;
1127 public void onAnimationEnd(Animator animation) {
1128 // If the animation was cancelled, it means that another animation
1129 // has interrupted this one, and we don't want to lock the item into
1130 // place just yet.
1131 if (!cancelled) {
1132 lp.isLockedToGrid = true;
Sunny Goyal82dfc152023-02-24 16:50:09 -08001133 item.getTranslateDelegate()
1134 .setTranslation(INDEX_REORDER_PREVIEW_OFFSET, 0, 0);
Adam Cohen482ed822012-03-02 14:15:13 -08001135 child.requestLayout();
Adam Cohenbfbfd262011-06-13 16:55:12 -07001136 }
1137 if (mReorderAnimators.containsKey(lp)) {
1138 mReorderAnimators.remove(lp);
1139 }
1140 }
1141 public void onAnimationCancel(Animator animation) {
1142 cancelled = true;
1143 }
1144 });
Adam Cohen482ed822012-03-02 14:15:13 -08001145 va.setStartDelay(delay);
1146 va.start();
Adam Cohenbfbfd262011-06-13 16:55:12 -07001147 return true;
1148 }
1149 return false;
1150 }
1151
Adam Cohenf7ca3b42021-02-22 11:03:58 -08001152 void visualizeDropLocation(int cellX, int cellY, int spanX, int spanY,
1153 DropTarget.DragObject dragObject) {
1154 if (mDragCell[0] != cellX || mDragCell[1] != cellY || mDragCellSpan[0] != spanX
1155 || mDragCellSpan[1] != spanY) {
Adam Cohen482ed822012-03-02 14:15:13 -08001156 mDragCell[0] = cellX;
1157 mDragCell[1] = cellY;
Adam Cohenf7ca3b42021-02-22 11:03:58 -08001158 mDragCellSpan[0] = spanX;
1159 mDragCellSpan[1] = spanY;
Steven Ng30dd1d62021-03-15 21:45:49 +00001160
Steven Nga999e222021-04-19 18:17:15 +01001161 // Apply color extraction on a widget when dragging.
1162 applyColorExtractionOnWidget(dragObject, mDragCell, spanX, spanY);
1163
Joe Onorato4be866d2010-10-10 11:26:02 -07001164 final int oldIndex = mDragOutlineCurrent;
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -07001165 mDragOutlineAnims[oldIndex].animateOut();
1166 mDragOutlineCurrent = (oldIndex + 1) % mDragOutlines.length;
Sunny Goyal106bf642015-07-16 12:18:06 -07001167
Sebastian Francod4682992022-10-05 13:03:09 -05001168 CellLayoutLayoutParams cell = mDragOutlines[mDragOutlineCurrent];
Sebastian Franco877088e2023-01-03 15:16:22 -07001169 cell.setCellX(cellX);
1170 cell.setCellY(cellY);
Adam Cohenf7ca3b42021-02-22 11:03:58 -08001171 cell.cellHSpan = spanX;
1172 cell.cellVSpan = spanY;
Adam Cohen65086992020-02-19 08:40:49 -08001173
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -07001174 mDragOutlineAnims[mDragOutlineCurrent].animateIn();
Adam Cohenf7ca3b42021-02-22 11:03:58 -08001175 invalidate();
Sunny Goyale78e3d72015-09-24 11:23:31 -07001176
1177 if (dragObject.stateAnnouncer != null) {
Sunny Goyalc13403c2016-11-18 23:44:48 -08001178 dragObject.stateAnnouncer.announce(getItemMoveDescription(cellX, cellY));
Sunny Goyale78e3d72015-09-24 11:23:31 -07001179 }
Adam Cohenf7ca3b42021-02-22 11:03:58 -08001180
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001181 }
1182 }
1183
Steven Ng30dd1d62021-03-15 21:45:49 +00001184 /** Applies the local color extraction to a dragging widget object. */
Steven Nga999e222021-04-19 18:17:15 +01001185 private void applyColorExtractionOnWidget(DropTarget.DragObject dragObject, int[] targetCell,
1186 int spanX, int spanY) {
Steven Ng30dd1d62021-03-15 21:45:49 +00001187 // Apply local extracted color if the DragView is an AppWidgetHostViewDrawable.
Steven Ng32427202021-04-19 18:12:12 +01001188 View view = dragObject.dragView.getContentView();
1189 if (view instanceof LauncherAppWidgetHostView) {
Andras Kloczl2dacbee2022-02-21 16:53:28 +00001190 int screenId = getWorkspace().getIdForScreen(this);
Steven Ng30dd1d62021-03-15 21:45:49 +00001191 cellToRect(targetCell[0], targetCell[1], spanX, spanY, mTempRect);
Jonathan Miranda21930da2021-05-03 18:44:13 +00001192
Sunny Goyal69a8eec2021-07-22 10:02:50 -07001193 ((LauncherAppWidgetHostView) view).handleDrag(mTempRect, this, screenId);
Steven Ng30dd1d62021-03-15 21:45:49 +00001194 }
1195 }
1196
Sunny Goyal726bee72018-03-05 12:54:24 -08001197 @SuppressLint("StringFormatMatches")
Sunny Goyalc13403c2016-11-18 23:44:48 -08001198 public String getItemMoveDescription(int cellX, int cellY) {
1199 if (mContainerType == HOTSEAT) {
1200 return getContext().getString(R.string.move_to_hotseat_position,
1201 Math.max(cellX, cellY) + 1);
1202 } else {
Shikha Malhotraf78da1b2022-04-11 10:23:18 +00001203 Workspace<?> workspace = getWorkspace();
Andras Kloczl2dacbee2022-02-21 16:53:28 +00001204 int row = cellY + 1;
1205 int col = workspace.mIsRtl ? mCountX - cellX : cellX + 1;
1206 int panelCount = workspace.getPanelCount();
Sebastian Franco930531f2022-06-16 16:49:11 -07001207 int screenId = workspace.getIdForScreen(this);
1208 int pageIndex = workspace.getPageIndexForScreenId(screenId);
Andras Kloczl2dacbee2022-02-21 16:53:28 +00001209 if (panelCount > 1) {
1210 // Increment the column if the target is on the right side of a two panel home
Andras Kloczl2dacbee2022-02-21 16:53:28 +00001211 col += (pageIndex % panelCount) * mCountX;
1212 }
Sebastian Franco930531f2022-06-16 16:49:11 -07001213 return getContext().getString(R.string.move_to_empty_cell_description, row, col,
1214 workspace.getPageDescription(pageIndex));
Sunny Goyalc13403c2016-11-18 23:44:48 -08001215 }
1216 }
1217
Shikha Malhotraf78da1b2022-04-11 10:23:18 +00001218 private Workspace<?> getWorkspace() {
Andras Kloczl2dacbee2022-02-21 16:53:28 +00001219 return Launcher.cast(mActivity).getWorkspace();
1220 }
1221
Adam Cohene0310962011-04-18 16:15:31 -07001222 public void clearDragOutlines() {
1223 final int oldIndex = mDragOutlineCurrent;
1224 mDragOutlineAnims[oldIndex].animateOut();
Adam Cohend41fbf52012-02-16 23:53:59 -08001225 mDragCell[0] = mDragCell[1] = -1;
Adam Cohene0310962011-04-18 16:15:31 -07001226 }
1227
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001228 /**
Jeff Sharkey70864282009-04-07 21:08:40 -07001229 * Find a vacant area that will fit the given bounds nearest the requested
1230 * cell location. Uses Euclidean distance to score multiple vacant areas.
Winson Chungaafa03c2010-06-11 17:34:16 -07001231 *
Romain Guy51afc022009-05-04 18:03:43 -07001232 * @param pixelX The X location at which you want to search for a vacant area.
1233 * @param pixelY The Y location at which you want to search for a vacant area.
Adam Cohend41fbf52012-02-16 23:53:59 -08001234 * @param minSpanX The minimum horizontal span required
1235 * @param minSpanY The minimum vertical span required
1236 * @param spanX Horizontal span of the object.
1237 * @param spanY Vertical span of the object.
1238 * @param result Array in which to place the result, or null (in which case a new array will
1239 * be allocated)
1240 * @return The X, Y cell of a vacant area that can contain this object,
1241 * nearest the requested location.
1242 */
Sebastian Francoe4c03452022-12-27 14:50:02 -06001243 public int[] findNearestVacantArea(int pixelX, int pixelY, int minSpanX, int minSpanY,
1244 int spanX, int spanY, int[] result, int[] resultSpan) {
Sebastian Francob57c0b22022-06-28 13:54:35 -07001245 return findNearestArea(pixelX, pixelY, minSpanX, minSpanY, spanX, spanY, false,
Adam Cohend41fbf52012-02-16 23:53:59 -08001246 result, resultSpan);
1247 }
1248
Adam Cohend41fbf52012-02-16 23:53:59 -08001249 /**
1250 * Find a vacant area that will fit the given bounds nearest the requested
1251 * cell location. Uses Euclidean distance to score multiple vacant areas.
Sebastian Francob57c0b22022-06-28 13:54:35 -07001252 * @param relativeXPos The X location relative to the Cell layout at which you want to search
1253 * for a vacant area.
1254 * @param relativeYPos The Y location relative to the Cell layout at which you want to search
1255 * for a vacant area.
Adam Cohend41fbf52012-02-16 23:53:59 -08001256 * @param minSpanX The minimum horizontal span required
1257 * @param minSpanY The minimum vertical span required
1258 * @param spanX Horizontal span of the object.
1259 * @param spanY Vertical span of the object.
1260 * @param ignoreOccupied If true, the result can be an occupied cell
1261 * @param result Array in which to place the result, or null (in which case a new array will
1262 * be allocated)
1263 * @return The X, Y cell of a vacant area that can contain this object,
1264 * nearest the requested location.
1265 */
Sebastian Franco96c46e72023-05-08 10:04:44 -06001266 protected int[] findNearestArea(int relativeXPos, int relativeYPos, int minSpanX, int minSpanY,
Sebastian Francob57c0b22022-06-28 13:54:35 -07001267 int spanX, int spanY, boolean ignoreOccupied, int[] result, int[] resultSpan) {
Sebastian Francob57c0b22022-06-28 13:54:35 -07001268 // For items with a spanX / spanY > 1, the passed in point (relativeXPos, relativeYPos)
1269 // corresponds to the center of the item, but we are searching based on the top-left cell,
1270 // so we translate the point over to correspond to the top-left.
1271 relativeXPos = (int) (relativeXPos - (mCellWidth + mBorderSpace.x) * (spanX - 1) / 2f);
1272 relativeYPos = (int) (relativeYPos - (mCellHeight + mBorderSpace.y) * (spanY - 1) / 2f);
Adam Cohene3e27a82011-04-15 12:07:39 -07001273
Jeff Sharkey70864282009-04-07 21:08:40 -07001274 // Keep track of best-scoring drop area
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001275 final int[] bestXY = result != null ? result : new int[2];
Jeff Sharkey70864282009-04-07 21:08:40 -07001276 double bestDistance = Double.MAX_VALUE;
Adam Cohend41fbf52012-02-16 23:53:59 -08001277 final Rect bestRect = new Rect(-1, -1, -1, -1);
Rajeev Kumar9962dbe2017-06-12 12:16:20 -07001278 final Stack<Rect> validRegions = new Stack<>();
Winson Chungaafa03c2010-06-11 17:34:16 -07001279
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001280 final int countX = mCountX;
1281 final int countY = mCountY;
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001282
Adam Cohend41fbf52012-02-16 23:53:59 -08001283 if (minSpanX <= 0 || minSpanY <= 0 || spanX <= 0 || spanY <= 0 ||
1284 spanX < minSpanX || spanY < minSpanY) {
1285 return bestXY;
1286 }
1287
1288 for (int y = 0; y < countY - (minSpanY - 1); y++) {
Michael Jurkac28de512010-08-13 11:27:44 -07001289 inner:
Adam Cohend41fbf52012-02-16 23:53:59 -08001290 for (int x = 0; x < countX - (minSpanX - 1); x++) {
1291 int ySize = -1;
1292 int xSize = -1;
Sebastian Francob57c0b22022-06-28 13:54:35 -07001293 if (!ignoreOccupied) {
Adam Cohend41fbf52012-02-16 23:53:59 -08001294 // First, let's see if this thing fits anywhere
1295 for (int i = 0; i < minSpanX; i++) {
1296 for (int j = 0; j < minSpanY; j++) {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001297 if (mOccupied.cells[x + i][y + j]) {
Adam Cohendf035382011-04-11 17:22:04 -07001298 continue inner;
1299 }
Michael Jurkac28de512010-08-13 11:27:44 -07001300 }
1301 }
Adam Cohend41fbf52012-02-16 23:53:59 -08001302 xSize = minSpanX;
1303 ySize = minSpanY;
1304
1305 // We know that the item will fit at _some_ acceptable size, now let's see
1306 // how big we can make it. We'll alternate between incrementing x and y spans
1307 // until we hit a limit.
1308 boolean incX = true;
1309 boolean hitMaxX = xSize >= spanX;
1310 boolean hitMaxY = ySize >= spanY;
1311 while (!(hitMaxX && hitMaxY)) {
1312 if (incX && !hitMaxX) {
1313 for (int j = 0; j < ySize; j++) {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001314 if (x + xSize > countX -1 || mOccupied.cells[x + xSize][y + j]) {
Adam Cohend41fbf52012-02-16 23:53:59 -08001315 // We can't move out horizontally
1316 hitMaxX = true;
1317 }
1318 }
1319 if (!hitMaxX) {
1320 xSize++;
1321 }
1322 } else if (!hitMaxY) {
1323 for (int i = 0; i < xSize; i++) {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001324 if (y + ySize > countY - 1 || mOccupied.cells[x + i][y + ySize]) {
Adam Cohend41fbf52012-02-16 23:53:59 -08001325 // We can't move out vertically
1326 hitMaxY = true;
1327 }
1328 }
1329 if (!hitMaxY) {
1330 ySize++;
1331 }
1332 }
1333 hitMaxX |= xSize >= spanX;
1334 hitMaxY |= ySize >= spanY;
1335 incX = !incX;
1336 }
Michael Jurkac28de512010-08-13 11:27:44 -07001337 }
Sunny Goyal2805e632015-05-20 15:35:32 -07001338 final int[] cellXY = mTmpPoint;
Adam Cohene3e27a82011-04-15 12:07:39 -07001339 cellToCenterPoint(x, y, cellXY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001340
Adam Cohend41fbf52012-02-16 23:53:59 -08001341 // We verify that the current rect is not a sub-rect of any of our previous
1342 // candidates. In this case, the current rect is disqualified in favour of the
1343 // containing rect.
Sebastian Franco4a922672022-10-27 16:42:24 -07001344 Rect currentRect = new Rect(x, y, x + xSize, y + ySize);
Adam Cohend41fbf52012-02-16 23:53:59 -08001345 boolean contained = false;
1346 for (Rect r : validRegions) {
1347 if (r.contains(currentRect)) {
1348 contained = true;
1349 break;
1350 }
1351 }
1352 validRegions.push(currentRect);
Sebastian Francob57c0b22022-06-28 13:54:35 -07001353 double distance = Math.hypot(cellXY[0] - relativeXPos, cellXY[1] - relativeYPos);
Adam Cohen482ed822012-03-02 14:15:13 -08001354
Adam Cohend41fbf52012-02-16 23:53:59 -08001355 if ((distance <= bestDistance && !contained) ||
1356 currentRect.contains(bestRect)) {
Michael Jurkac28de512010-08-13 11:27:44 -07001357 bestDistance = distance;
1358 bestXY[0] = x;
1359 bestXY[1] = y;
Adam Cohend41fbf52012-02-16 23:53:59 -08001360 if (resultSpan != null) {
1361 resultSpan[0] = xSize;
1362 resultSpan[1] = ySize;
1363 }
1364 bestRect.set(currentRect);
Michael Jurkac28de512010-08-13 11:27:44 -07001365 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001366 }
1367 }
1368
Adam Cohenc0dcf592011-06-01 15:30:43 -07001369 // Return -1, -1 if no suitable location found
1370 if (bestDistance == Double.MAX_VALUE) {
1371 bestXY[0] = -1;
1372 bestXY[1] = -1;
Jeff Sharkey70864282009-04-07 21:08:40 -07001373 }
Adam Cohenc0dcf592011-06-01 15:30:43 -07001374 return bestXY;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001375 }
Winson Chungaafa03c2010-06-11 17:34:16 -07001376
Sebastian Francoe4c03452022-12-27 14:50:02 -06001377 public GridOccupancy getOccupied() {
1378 return mOccupied;
1379 }
1380
Adam Cohen482ed822012-03-02 14:15:13 -08001381 private void copySolutionToTempState(ItemConfiguration solution, View dragView) {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001382 mTmpOccupied.clear();
Adam Cohen482ed822012-03-02 14:15:13 -08001383
Michael Jurkaa52570f2012-03-20 03:18:20 -07001384 int childCount = mShortcutsAndWidgets.getChildCount();
Adam Cohen482ed822012-03-02 14:15:13 -08001385 for (int i = 0; i < childCount; i++) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07001386 View child = mShortcutsAndWidgets.getChildAt(i);
Adam Cohen482ed822012-03-02 14:15:13 -08001387 if (child == dragView) continue;
Sebastian Francod4682992022-10-05 13:03:09 -05001388 CellLayoutLayoutParams lp = (CellLayoutLayoutParams) child.getLayoutParams();
Adam Cohen8baab352012-03-20 17:39:21 -07001389 CellAndSpan c = solution.map.get(child);
1390 if (c != null) {
Sebastian Franco877088e2023-01-03 15:16:22 -07001391 lp.setTmpCellX(c.cellX);
1392 lp.setTmpCellY(c.cellY);
Adam Cohen8baab352012-03-20 17:39:21 -07001393 lp.cellHSpan = c.spanX;
1394 lp.cellVSpan = c.spanY;
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001395 mTmpOccupied.markCells(c, true);
Adam Cohen482ed822012-03-02 14:15:13 -08001396 }
1397 }
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001398 mTmpOccupied.markCells(solution, true);
Adam Cohen482ed822012-03-02 14:15:13 -08001399 }
1400
1401 private void animateItemsToSolution(ItemConfiguration solution, View dragView, boolean
1402 commitDragView) {
1403
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001404 GridOccupancy occupied = DESTRUCTIVE_REORDER ? mOccupied : mTmpOccupied;
1405 occupied.clear();
Adam Cohen482ed822012-03-02 14:15:13 -08001406
Michael Jurkaa52570f2012-03-20 03:18:20 -07001407 int childCount = mShortcutsAndWidgets.getChildCount();
Adam Cohen482ed822012-03-02 14:15:13 -08001408 for (int i = 0; i < childCount; i++) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07001409 View child = mShortcutsAndWidgets.getChildAt(i);
Adam Cohen482ed822012-03-02 14:15:13 -08001410 if (child == dragView) continue;
Adam Cohen8baab352012-03-20 17:39:21 -07001411 CellAndSpan c = solution.map.get(child);
1412 if (c != null) {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001413 animateChildToPosition(child, c.cellX, c.cellY, REORDER_ANIMATION_DURATION, 0,
Adam Cohen19f37922012-03-21 11:59:11 -07001414 DESTRUCTIVE_REORDER, false);
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001415 occupied.markCells(c, true);
Adam Cohen482ed822012-03-02 14:15:13 -08001416 }
1417 }
1418 if (commitDragView) {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001419 occupied.markCells(solution, true);
Adam Cohen482ed822012-03-02 14:15:13 -08001420 }
1421 }
1422
Adam Cohenfa3c58f2013-12-06 16:10:55 -08001423
1424 // This method starts or changes the reorder preview animations
1425 private void beginOrAdjustReorderPreviewAnimations(ItemConfiguration solution,
Adam Cohen65086992020-02-19 08:40:49 -08001426 View dragView, int mode) {
Adam Cohen19f37922012-03-21 11:59:11 -07001427 int childCount = mShortcutsAndWidgets.getChildCount();
Adam Cohen19f37922012-03-21 11:59:11 -07001428 for (int i = 0; i < childCount; i++) {
1429 View child = mShortcutsAndWidgets.getChildAt(i);
1430 if (child == dragView) continue;
1431 CellAndSpan c = solution.map.get(child);
Sebastian Franco5f0af4f2023-11-21 10:45:45 -06001432 boolean skip = mode == ReorderPreviewAnimation.MODE_HINT
1433 && !solution.intersectingViews.contains(child);
Adam Cohend9162062020-03-24 16:35:35 -07001434
Sebastian Francod4682992022-10-05 13:03:09 -05001435 CellLayoutLayoutParams lp = (CellLayoutLayoutParams) child.getLayoutParams();
Adam Cohend9162062020-03-24 16:35:35 -07001436 if (c != null && !skip && (child instanceof Reorderable)) {
Sebastian Francof7654252024-01-18 11:50:50 -08001437 ReorderPreviewAnimation rha = new ReorderPreviewAnimation(child, mode,
1438 lp.getCellX(), lp.getCellY(), c.cellX, c.cellY, c.spanX, c.spanY,
1439 mReorderPreviewAnimationMagnitude, this, mShakeAnimators);
Adam Cohend024f982012-05-23 18:26:45 -07001440 rha.animate();
Adam Cohen19f37922012-03-21 11:59:11 -07001441 }
1442 }
1443 }
1444
Adam Cohenfa3c58f2013-12-06 16:10:55 -08001445 private void completeAndClearReorderPreviewAnimations() {
1446 for (ReorderPreviewAnimation a: mShakeAnimators.values()) {
Adam Cohend9162062020-03-24 16:35:35 -07001447 a.finishAnimation();
Adam Cohen19f37922012-03-21 11:59:11 -07001448 }
1449 mShakeAnimators.clear();
1450 }
1451
Sunny Goyal711c5962021-06-23 12:36:18 -07001452 private void commitTempPlacement(View dragView) {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001453 mTmpOccupied.copyTo(mOccupied);
Sunny Goyalaa8ef112015-06-12 20:04:41 -07001454
Andras Kloczl2dacbee2022-02-21 16:53:28 +00001455 int screenId = getWorkspace().getIdForScreen(this);
Sunny Goyalaa8ef112015-06-12 20:04:41 -07001456 int container = Favorites.CONTAINER_DESKTOP;
1457
Sunny Goyalc13403c2016-11-18 23:44:48 -08001458 if (mContainerType == HOTSEAT) {
Sunny Goyalaa8ef112015-06-12 20:04:41 -07001459 screenId = -1;
1460 container = Favorites.CONTAINER_HOTSEAT;
1461 }
1462
Michael Jurkaa52570f2012-03-20 03:18:20 -07001463 int childCount = mShortcutsAndWidgets.getChildCount();
Adam Cohen482ed822012-03-02 14:15:13 -08001464 for (int i = 0; i < childCount; i++) {
Adam Cohenea889a22012-03-27 16:45:39 -07001465 View child = mShortcutsAndWidgets.getChildAt(i);
Sebastian Francod4682992022-10-05 13:03:09 -05001466 CellLayoutLayoutParams lp = (CellLayoutLayoutParams) child.getLayoutParams();
Adam Cohenea889a22012-03-27 16:45:39 -07001467 ItemInfo info = (ItemInfo) child.getTag();
Adam Cohen2acce882012-03-28 19:03:19 -07001468 // We do a null check here because the item info can be null in the case of the
1469 // AllApps button in the hotseat.
Sunny Goyal711c5962021-06-23 12:36:18 -07001470 if (info != null && child != dragView) {
Sunny Goyal669b71f2023-01-27 14:37:07 -08001471 CellPos presenterPos = mActivity.getCellPosMapper().mapModelToPresenter(info);
1472 final boolean requiresDbUpdate = (presenterPos.cellX != lp.getTmpCellX()
1473 || presenterPos.cellY != lp.getTmpCellY() || info.spanX != lp.cellHSpan
1474 || info.spanY != lp.cellVSpan || presenterPos.screenId != screenId);
Sunny Goyalaa8ef112015-06-12 20:04:41 -07001475
Sebastian Franco877088e2023-01-03 15:16:22 -07001476 lp.setCellX(lp.getTmpCellX());
Sebastian Franco877088e2023-01-03 15:16:22 -07001477 lp.setCellY(lp.getTmpCellY());
Sunny Goyalaa8ef112015-06-12 20:04:41 -07001478 if (requiresDbUpdate) {
Sunny Goyalab770a12018-11-14 15:17:26 -08001479 Launcher.cast(mActivity).getModelWriter().modifyItemInDatabase(info, container,
Sunny Goyal669b71f2023-01-27 14:37:07 -08001480 screenId, lp.getCellX(), lp.getCellY(), lp.cellHSpan, lp.cellVSpan);
Sunny Goyalaa8ef112015-06-12 20:04:41 -07001481 }
Adam Cohen2acce882012-03-28 19:03:19 -07001482 }
Adam Cohen482ed822012-03-02 14:15:13 -08001483 }
1484 }
1485
Sunny Goyalf7a29e82015-04-24 15:20:43 -07001486 private void setUseTempCoords(boolean useTempCoords) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07001487 int childCount = mShortcutsAndWidgets.getChildCount();
Adam Cohen482ed822012-03-02 14:15:13 -08001488 for (int i = 0; i < childCount; i++) {
Sebastian Francod4682992022-10-05 13:03:09 -05001489 CellLayoutLayoutParams lp = (CellLayoutLayoutParams) mShortcutsAndWidgets.getChildAt(
1490 i).getLayoutParams();
Adam Cohen482ed822012-03-02 14:15:13 -08001491 lp.useTmpCoords = useTempCoords;
1492 }
1493 }
1494
Sebastian Franco5c8f8682023-11-14 09:52:41 -06001495 /**
1496 * For a given region, return the rectangle of the overlapping cell and span with the given
1497 * region including the region itself. If there is no overlap the rectangle will be
1498 * invalid i.e. -1, 0, -1, 0.
1499 */
1500 @Nullable
1501 public Rect getIntersectingRectanglesInRegion(final Rect region, final View dragView) {
1502 Rect boundingRect = new Rect(region);
Adam Cohen19f37922012-03-21 11:59:11 -07001503 Rect r1 = new Rect();
Sebastian Franco5c8f8682023-11-14 09:52:41 -06001504 boolean isOverlapping = false;
Adam Cohen19f37922012-03-21 11:59:11 -07001505 final int count = mShortcutsAndWidgets.getChildCount();
1506 for (int i = 0; i < count; i++) {
1507 View child = mShortcutsAndWidgets.getChildAt(i);
1508 if (child == dragView) continue;
Sebastian Francod4682992022-10-05 13:03:09 -05001509 CellLayoutLayoutParams
1510 lp = (CellLayoutLayoutParams) child.getLayoutParams();
Sebastian Franco877088e2023-01-03 15:16:22 -07001511 r1.set(lp.getCellX(), lp.getCellY(), lp.getCellX() + lp.cellHSpan,
1512 lp.getCellY() + lp.cellVSpan);
Sebastian Franco5c8f8682023-11-14 09:52:41 -06001513 if (Rect.intersects(region, r1)) {
1514 isOverlapping = true;
1515 boundingRect.union(r1);
Adam Cohen19f37922012-03-21 11:59:11 -07001516 }
1517 }
Sebastian Franco5c8f8682023-11-14 09:52:41 -06001518 return isOverlapping ? boundingRect : null;
Adam Cohen19f37922012-03-21 11:59:11 -07001519 }
1520
Sebastian Francoe4c03452022-12-27 14:50:02 -06001521 public boolean isNearestDropLocationOccupied(int pixelX, int pixelY, int spanX, int spanY,
Adam Cohen19f37922012-03-21 11:59:11 -07001522 View dragView, int[] result) {
Sebastián Francof9a6ac22022-11-15 22:56:37 +00001523 result = findNearestAreaIgnoreOccupied(pixelX, pixelY, spanX, spanY, result);
Sebastian Franco5c8f8682023-11-14 09:52:41 -06001524 return getIntersectingRectanglesInRegion(
1525 new Rect(result[0], result[1], result[0] + spanX, result[1] + spanY),
1526 dragView
1527 ) != null;
Adam Cohen19f37922012-03-21 11:59:11 -07001528 }
1529
1530 void revertTempState() {
Adam Cohenfa3c58f2013-12-06 16:10:55 -08001531 completeAndClearReorderPreviewAnimations();
1532 if (isItemPlacementDirty() && !DESTRUCTIVE_REORDER) {
1533 final int count = mShortcutsAndWidgets.getChildCount();
1534 for (int i = 0; i < count; i++) {
1535 View child = mShortcutsAndWidgets.getChildAt(i);
Sebastian Francod4682992022-10-05 13:03:09 -05001536 CellLayoutLayoutParams
1537 lp = (CellLayoutLayoutParams) child.getLayoutParams();
Sebastian Franco877088e2023-01-03 15:16:22 -07001538 if (lp.getTmpCellX() != lp.getCellX() || lp.getTmpCellY() != lp.getCellY()) {
1539 lp.setTmpCellX(lp.getCellX());
1540 lp.setTmpCellY(lp.getCellY());
1541 animateChildToPosition(child, lp.getCellX(), lp.getCellY(),
1542 REORDER_ANIMATION_DURATION, 0, false, false);
Adam Cohenfa3c58f2013-12-06 16:10:55 -08001543 }
Adam Cohen19f37922012-03-21 11:59:11 -07001544 }
Adam Cohenfa3c58f2013-12-06 16:10:55 -08001545 setItemPlacementDirty(false);
Adam Cohen19f37922012-03-21 11:59:11 -07001546 }
Adam Cohen19f37922012-03-21 11:59:11 -07001547 }
1548
Adam Cohenbebf0422012-04-11 18:06:28 -07001549 boolean createAreaForResize(int cellX, int cellY, int spanX, int spanY,
1550 View dragView, int[] direction, boolean commit) {
1551 int[] pixelXY = new int[2];
1552 regionToCenterPoint(cellX, cellY, spanX, spanY, pixelXY);
1553
1554 // First we determine if things have moved enough to cause a different layout
Adam Cohenfa3c58f2013-12-06 16:10:55 -08001555 ItemConfiguration swapSolution = findReorderSolution(pixelXY[0], pixelXY[1], spanX, spanY,
Charlie Anderson46122392024-01-18 17:23:46 -05001556 spanX, spanY, direction, dragView, true);
Adam Cohenbebf0422012-04-11 18:06:28 -07001557
1558 setUseTempCoords(true);
1559 if (swapSolution != null && swapSolution.isSolution) {
1560 // If we're just testing for a possible location (MODE_ACCEPT_DROP), we don't bother
1561 // committing anything or animating anything as we just want to determine if a solution
1562 // exists
1563 copySolutionToTempState(swapSolution, dragView);
1564 setItemPlacementDirty(true);
1565 animateItemsToSolution(swapSolution, dragView, commit);
1566
1567 if (commit) {
Sunny Goyal711c5962021-06-23 12:36:18 -07001568 commitTempPlacement(null);
Adam Cohenfa3c58f2013-12-06 16:10:55 -08001569 completeAndClearReorderPreviewAnimations();
Adam Cohenbebf0422012-04-11 18:06:28 -07001570 setItemPlacementDirty(false);
1571 } else {
Adam Cohenfa3c58f2013-12-06 16:10:55 -08001572 beginOrAdjustReorderPreviewAnimations(swapSolution, dragView,
Adam Cohen65086992020-02-19 08:40:49 -08001573 ReorderPreviewAnimation.MODE_PREVIEW);
Adam Cohenbebf0422012-04-11 18:06:28 -07001574 }
1575 mShortcutsAndWidgets.requestLayout();
1576 }
1577 return swapSolution.isSolution;
1578 }
1579
Sebastian Francoe4c03452022-12-27 14:50:02 -06001580 public ReorderAlgorithm createReorderAlgorithm() {
1581 return new ReorderAlgorithm(this);
1582 }
1583
Sebastian Franco09589322022-11-02 15:25:58 -07001584 protected ItemConfiguration findReorderSolution(int pixelX, int pixelY, int minSpanX,
Charlie Anderson46122392024-01-18 17:23:46 -05001585 int minSpanY, int spanX, int spanY, int[] direction, View dragView, boolean decX) {
Sebastian Franco25f8e402023-12-02 13:54:05 -06001586 ItemConfiguration configuration = new ItemConfiguration();
1587 copyCurrentStateToSolution(configuration);
1588 ReorderParameters parameters = new ReorderParameters(pixelX, pixelY, spanX, spanY, minSpanX,
1589 minSpanY, dragView, configuration);
Charlie Anderson46122392024-01-18 17:23:46 -05001590 int[] directionVector = direction != null ? direction : mDirectionVector;
1591 return createReorderAlgorithm().findReorderSolution(parameters, directionVector, decX);
Sebastian Franco09589322022-11-02 15:25:58 -07001592 }
1593
Sebastián Franco61fbd182023-11-29 21:30:43 +00001594 public void copyCurrentStateToSolution(ItemConfiguration solution) {
Sebastian Franco9cab1c32022-10-25 17:28:54 -07001595 int childCount = mShortcutsAndWidgets.getChildCount();
1596 for (int i = 0; i < childCount; i++) {
1597 View child = mShortcutsAndWidgets.getChildAt(i);
1598 CellLayoutLayoutParams lp = (CellLayoutLayoutParams) child.getLayoutParams();
Sebastián Franco61fbd182023-11-29 21:30:43 +00001599 solution.add(child,
1600 new CellAndSpan(lp.getCellX(), lp.getCellY(), lp.cellHSpan, lp.cellVSpan));
Sebastian Franco9cab1c32022-10-25 17:28:54 -07001601 }
Sebastian Franco53a15a42022-10-25 17:28:54 -07001602 }
1603
1604 /**
Sebastian Franco53a15a42022-10-25 17:28:54 -07001605 * When the user drags an Item in the workspace sometimes we need to move the items already in
1606 * the workspace to make space for the new item, this function return a solution for that
1607 * reorder.
1608 *
1609 * @param pixelX X coordinate in the screen of the dragView in pixels
1610 * @param pixelY Y coordinate in the screen of the dragView in pixels
1611 * @param minSpanX minimum horizontal span the item can be shrunk to
1612 * @param minSpanY minimum vertical span the item can be shrunk to
1613 * @param spanX occupied horizontal span
1614 * @param spanY occupied vertical span
1615 * @param dragView the view of the item being draged
1616 * @return returns a solution for the given parameters, the solution contains all the icons and
1617 * the locations they should be in the given solution.
1618 */
1619 public ItemConfiguration calculateReorder(int pixelX, int pixelY, int minSpanX, int minSpanY,
1620 int spanX, int spanY, View dragView) {
Sebastian Franco25f8e402023-12-02 13:54:05 -06001621 ItemConfiguration configuration = new ItemConfiguration();
1622 copyCurrentStateToSolution(configuration);
1623 return createReorderAlgorithm().calculateReorder(
1624 new ReorderParameters(pixelX, pixelY, spanX, spanY, minSpanX, minSpanY, dragView,
1625 configuration)
1626 );
Sebastian Franco53a15a42022-10-25 17:28:54 -07001627 }
Adam Cohen482ed822012-03-02 14:15:13 -08001628
Sebastian Franco9cab1c32022-10-25 17:28:54 -07001629 int[] performReorder(int pixelX, int pixelY, int minSpanX, int minSpanY, int spanX, int spanY,
1630 View dragView, int[] result, int[] resultSpan, int mode) {
1631 if (resultSpan == null) {
1632 resultSpan = new int[]{-1, -1};
1633 }
1634 if (result == null) {
1635 result = new int[]{-1, -1};
1636 }
Sebastian Franco5d990ee2022-11-01 16:08:24 -07001637
1638 ItemConfiguration finalSolution = null;
1639 // We want the solution to match the animation of the preview and to match the drop so we
1640 // only recalculate in mode MODE_SHOW_REORDER_HINT because that the first one to run in the
1641 // reorder cycle.
1642 if (mode == MODE_SHOW_REORDER_HINT || mPreviousSolution == null) {
1643 finalSolution = calculateReorder(pixelX, pixelY, minSpanX, minSpanY, spanX, spanY,
1644 dragView);
1645 mPreviousSolution = finalSolution;
1646 } else {
Sebastian Franco9cab1c32022-10-25 17:28:54 -07001647 finalSolution = mPreviousSolution;
1648 // We reset this vector after drop
1649 if (mode == MODE_ON_DROP || mode == MODE_ON_DROP_EXTERNAL) {
1650 mPreviousSolution = null;
1651 }
Sebastian Franco9cab1c32022-10-25 17:28:54 -07001652 }
1653
1654 if (finalSolution == null || !finalSolution.isSolution) {
1655 result[0] = result[1] = resultSpan[0] = resultSpan[1] = -1;
1656 } else {
1657 result[0] = finalSolution.cellX;
1658 result[1] = finalSolution.cellY;
1659 resultSpan[0] = finalSolution.spanX;
1660 resultSpan[1] = finalSolution.spanY;
Sebastian Franco9c743272022-11-15 15:03:25 -08001661 performReorder(finalSolution, dragView, mode);
Sebastian Franco9cab1c32022-10-25 17:28:54 -07001662 }
Sebastian Franco9cab1c32022-10-25 17:28:54 -07001663 return result;
1664 }
1665
Sebastian Franco53a15a42022-10-25 17:28:54 -07001666 /**
1667 * Animates and submits in the DB the given ItemConfiguration depending of the mode.
1668 *
1669 * @param solution represents widgets on the screen which the Workspace will animate to and
1670 * would be submitted to the database.
1671 * @param dragView view which is being dragged over the workspace that trigger the reorder
1672 * @param mode depending on the mode different animations would be played and depending on the
1673 * mode the solution would be submitted or not the database.
1674 * The possible modes are {@link MODE_SHOW_REORDER_HINT}, {@link MODE_DRAG_OVER},
1675 * {@link MODE_ON_DROP}, {@link MODE_ON_DROP_EXTERNAL}, {@link MODE_ACCEPT_DROP}
1676 * defined in {@link CellLayout}.
1677 */
Sebastian Francoe4c03452022-12-27 14:50:02 -06001678 public void performReorder(ItemConfiguration solution, View dragView, int mode) {
Adam Cohenfa3c58f2013-12-06 16:10:55 -08001679 if (mode == MODE_SHOW_REORDER_HINT) {
Sebastian Franco53a15a42022-10-25 17:28:54 -07001680 beginOrAdjustReorderPreviewAnimations(solution, dragView,
1681 ReorderPreviewAnimation.MODE_HINT);
1682 return;
1683 }
1684 // If we're just testing for a possible location (MODE_ACCEPT_DROP), we don't bother
1685 // committing anything or animating anything as we just want to determine if a solution
1686 // exists
1687 if (mode == MODE_DRAG_OVER || mode == MODE_ON_DROP || mode == MODE_ON_DROP_EXTERNAL) {
1688 if (!DESTRUCTIVE_REORDER) {
1689 setUseTempCoords(true);
1690 }
1691
1692 if (!DESTRUCTIVE_REORDER) {
1693 copySolutionToTempState(solution, dragView);
1694 }
1695 setItemPlacementDirty(true);
1696 animateItemsToSolution(solution, dragView, mode == MODE_ON_DROP);
1697
1698 if (!DESTRUCTIVE_REORDER
1699 && (mode == MODE_ON_DROP || mode == MODE_ON_DROP_EXTERNAL)) {
1700 // Since the temp solution didn't update dragView, don't commit it either
1701 commitTempPlacement(dragView);
1702 completeAndClearReorderPreviewAnimations();
1703 setItemPlacementDirty(false);
Adam Cohenfa3c58f2013-12-06 16:10:55 -08001704 } else {
Sebastian Franco53a15a42022-10-25 17:28:54 -07001705 beginOrAdjustReorderPreviewAnimations(solution, dragView,
1706 ReorderPreviewAnimation.MODE_PREVIEW);
Adam Cohenfa3c58f2013-12-06 16:10:55 -08001707 }
Adam Cohenfa3c58f2013-12-06 16:10:55 -08001708 }
1709
Sebastian Franco53a15a42022-10-25 17:28:54 -07001710 if (mode == MODE_ON_DROP && !DESTRUCTIVE_REORDER) {
Adam Cohen482ed822012-03-02 14:15:13 -08001711 setUseTempCoords(false);
1712 }
Adam Cohen482ed822012-03-02 14:15:13 -08001713
Michael Jurkaa52570f2012-03-20 03:18:20 -07001714 mShortcutsAndWidgets.requestLayout();
Adam Cohen482ed822012-03-02 14:15:13 -08001715 }
1716
Adam Cohen19f37922012-03-21 11:59:11 -07001717 void setItemPlacementDirty(boolean dirty) {
1718 mItemPlacementDirty = dirty;
Adam Cohen482ed822012-03-02 14:15:13 -08001719 }
Adam Cohen19f37922012-03-21 11:59:11 -07001720 boolean isItemPlacementDirty() {
1721 return mItemPlacementDirty;
Adam Cohen482ed822012-03-02 14:15:13 -08001722 }
1723
Sebastian Francoe4c03452022-12-27 14:50:02 -06001724 /**
Adam Cohendf035382011-04-11 17:22:04 -07001725 * Find a starting cell position that will fit the given bounds nearest the requested
1726 * cell location. Uses Euclidean distance to score multiple vacant areas.
1727 *
1728 * @param pixelX The X location at which you want to search for a vacant area.
1729 * @param pixelY The Y location at which you want to search for a vacant area.
1730 * @param spanX Horizontal span of the object.
1731 * @param spanY Vertical span of the object.
Adam Cohendf035382011-04-11 17:22:04 -07001732 * @param result Previously returned value to possibly recycle.
1733 * @return The X, Y cell of a vacant area that can contain this object,
1734 * nearest the requested location.
1735 */
Sebastián Francof9a6ac22022-11-15 22:56:37 +00001736 public int[] findNearestAreaIgnoreOccupied(int pixelX, int pixelY, int spanX, int spanY,
1737 int[] result) {
Sebastian Francob57c0b22022-06-28 13:54:35 -07001738 return findNearestArea(pixelX, pixelY, spanX, spanY, spanX, spanY, true, result, null);
Adam Cohendf035382011-04-11 17:22:04 -07001739 }
1740
Michael Jurka0280c3b2010-09-17 15:00:07 -07001741 boolean existsEmptyCell() {
1742 return findCellForSpan(null, 1, 1);
1743 }
1744
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001745 /**
Michael Jurka0280c3b2010-09-17 15:00:07 -07001746 * Finds the upper-left coordinate of the first rectangle in the grid that can
1747 * hold a cell of the specified dimensions. If intersectX and intersectY are not -1,
1748 * then this method will only return coordinates for rectangles that contain the cell
1749 * (intersectX, intersectY)
1750 *
1751 * @param cellXY The array that will contain the position of a vacant cell if such a cell
1752 * can be found.
1753 * @param spanX The horizontal span of the cell we want to find.
1754 * @param spanY The vertical span of the cell we want to find.
1755 *
1756 * @return True if a vacant cell of the specified dimension was found, false otherwise.
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001757 */
Hyunyoung Song3f471442015-04-08 19:01:34 -07001758 public boolean findCellForSpan(int[] cellXY, int spanX, int spanY) {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001759 if (cellXY == null) {
1760 cellXY = new int[2];
Michael Jurka0280c3b2010-09-17 15:00:07 -07001761 }
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001762 return mOccupied.findVacantCell(cellXY, spanX, spanY);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001763 }
1764
1765 /**
Winson Chungc07918d2011-07-01 15:35:26 -07001766 * A drag event has begun over this layout.
1767 * It may have begun over this layout (in which case onDragChild is called first),
1768 * or it may have begun on another layout.
1769 */
1770 void onDragEnter() {
Winson Chungc07918d2011-07-01 15:35:26 -07001771 mDragging = true;
Sebastian Franco5aa71ce2022-12-14 12:13:19 -06001772 mPreviousSolution = null;
Winson Chungc07918d2011-07-01 15:35:26 -07001773 }
1774
1775 /**
Michael Jurka0280c3b2010-09-17 15:00:07 -07001776 * Called when drag has left this CellLayout or has been completed (successfully or not)
1777 */
1778 void onDragExit() {
Joe Onorato4be866d2010-10-10 11:26:02 -07001779 // This can actually be called when we aren't in a drag, e.g. when adding a new
1780 // item to this layout via the customize drawer.
1781 // Guard against that case.
1782 if (mDragging) {
1783 mDragging = false;
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001784 }
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -07001785
1786 // Invalidate the drag data
Sebastian Franco5aa71ce2022-12-14 12:13:19 -06001787 mPreviousSolution = null;
Adam Cohend41fbf52012-02-16 23:53:59 -08001788 mDragCell[0] = mDragCell[1] = -1;
Adam Cohenf7ca3b42021-02-22 11:03:58 -08001789 mDragCellSpan[0] = mDragCellSpan[1] = -1;
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -07001790 mDragOutlineAnims[mDragOutlineCurrent].animateOut();
1791 mDragOutlineCurrent = (mDragOutlineCurrent + 1) % mDragOutlineAnims.length;
Adam Cohen19f37922012-03-21 11:59:11 -07001792 revertTempState();
Michael Jurka33945b22010-12-21 18:19:38 -08001793 setIsDragOverlapping(false);
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001794 }
1795
1796 /**
Winson Chungaafa03c2010-06-11 17:34:16 -07001797 * Mark a child as having been dropped.
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001798 * At the beginning of the drag operation, the child may have been on another
Patrick Dubroyce34a972010-10-19 10:34:32 -07001799 * screen, but it is re-parented before this method is called.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001800 *
1801 * @param child The child that is being dropped
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001802 */
Adam Cohen716b51e2011-06-30 12:09:54 -07001803 void onDropChild(View child) {
Romain Guyd94533d2009-08-17 10:01:15 -07001804 if (child != null) {
Sebastian Francod4682992022-10-05 13:03:09 -05001805 CellLayoutLayoutParams
1806 lp = (CellLayoutLayoutParams) child.getLayoutParams();
Romain Guy84f296c2009-11-04 15:00:44 -08001807 lp.dropped = true;
Romain Guyd94533d2009-08-17 10:01:15 -07001808 child.requestLayout();
Tony Wickham1cdb6d02015-09-17 11:08:27 -07001809 markCellsAsOccupiedForView(child);
Romain Guyd94533d2009-08-17 10:01:15 -07001810 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001811 }
1812
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001813 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001814 * Computes a bounding rectangle for a range of cells
Winson Chungaafa03c2010-06-11 17:34:16 -07001815 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001816 * @param cellX X coordinate of upper left corner expressed as a cell position
1817 * @param cellY Y coordinate of upper left corner expressed as a cell position
Winson Chungaafa03c2010-06-11 17:34:16 -07001818 * @param cellHSpan Width in cells
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001819 * @param cellVSpan Height in cells
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001820 * @param resultRect Rect into which to put the results
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001821 */
Adam Cohend41fbf52012-02-16 23:53:59 -08001822 public void cellToRect(int cellX, int cellY, int cellHSpan, int cellVSpan, Rect resultRect) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001823 final int cellWidth = mCellWidth;
1824 final int cellHeight = mCellHeight;
Winson Chungaafa03c2010-06-11 17:34:16 -07001825
Pierre Barbier de Reuille1b8bbb62021-05-19 22:45:16 +01001826 // We observe a shift of 1 pixel on the x coordinate compared to the actual cell coordinates
1827 final int hStartPadding = getPaddingLeft()
1828 + (int) Math.ceil(getUnusedHorizontalSpace() / 2f);
Winson Chung4b825dcd2011-06-19 12:41:22 -07001829 final int vStartPadding = getPaddingTop();
Winson Chungaafa03c2010-06-11 17:34:16 -07001830
Thales Lima78d00ad2021-09-30 11:29:06 +01001831 int x = hStartPadding + (cellX * mBorderSpace.x) + (cellX * cellWidth);
1832 int y = vStartPadding + (cellY * mBorderSpace.y) + (cellY * cellHeight);
Jon Miranda228877d2021-02-09 11:05:00 -05001833
Thales Lima78d00ad2021-09-30 11:29:06 +01001834 int width = cellHSpan * cellWidth + ((cellHSpan - 1) * mBorderSpace.x);
1835 int height = cellVSpan * cellHeight + ((cellVSpan - 1) * mBorderSpace.y);
Winson Chungaafa03c2010-06-11 17:34:16 -07001836
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001837 resultRect.set(x, y, x + width, y + height);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001838 }
Winson Chungaafa03c2010-06-11 17:34:16 -07001839
Adam Cohend4844c32011-02-18 19:25:06 -08001840 public void markCellsAsOccupiedForView(View view) {
Sebastian Francof153d912022-04-22 16:15:27 -05001841 if (view instanceof LauncherAppWidgetHostView
1842 && view.getTag() instanceof LauncherAppWidgetInfo) {
1843 LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) view.getTag();
Sunny Goyal669b71f2023-01-27 14:37:07 -08001844 CellPos pos = mActivity.getCellPosMapper().mapModelToPresenter(info);
1845 mOccupied.markCells(pos.cellX, pos.cellY, info.spanX, info.spanY, true);
Sebastian Francof153d912022-04-22 16:15:27 -05001846 return;
1847 }
Michael Jurkaa52570f2012-03-20 03:18:20 -07001848 if (view == null || view.getParent() != mShortcutsAndWidgets) return;
Sebastian Francod4682992022-10-05 13:03:09 -05001849 CellLayoutLayoutParams
1850 lp = (CellLayoutLayoutParams) view.getLayoutParams();
Sebastian Franco877088e2023-01-03 15:16:22 -07001851 mOccupied.markCells(lp.getCellX(), lp.getCellY(), lp.cellHSpan, lp.cellVSpan, true);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001852 }
1853
Adam Cohend4844c32011-02-18 19:25:06 -08001854 public void markCellsAsUnoccupiedForView(View view) {
Sebastian Francof153d912022-04-22 16:15:27 -05001855 if (view instanceof LauncherAppWidgetHostView
1856 && view.getTag() instanceof LauncherAppWidgetInfo) {
1857 LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) view.getTag();
Sunny Goyal669b71f2023-01-27 14:37:07 -08001858 CellPos pos = mActivity.getCellPosMapper().mapModelToPresenter(info);
1859 mOccupied.markCells(pos.cellX, pos.cellY, info.spanX, info.spanY, false);
Sebastian Francof153d912022-04-22 16:15:27 -05001860 return;
1861 }
Michael Jurkaa52570f2012-03-20 03:18:20 -07001862 if (view == null || view.getParent() != mShortcutsAndWidgets) return;
Sebastian Francod4682992022-10-05 13:03:09 -05001863 CellLayoutLayoutParams
1864 lp = (CellLayoutLayoutParams) view.getLayoutParams();
Sebastian Franco877088e2023-01-03 15:16:22 -07001865 mOccupied.markCells(lp.getCellX(), lp.getCellY(), lp.cellHSpan, lp.cellVSpan, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001866 }
1867
Adam Cohen2801caf2011-05-13 20:57:39 -07001868 public int getDesiredWidth() {
Jon Miranda228877d2021-02-09 11:05:00 -05001869 return getPaddingLeft() + getPaddingRight() + (mCountX * mCellWidth)
Thales Lima78d00ad2021-09-30 11:29:06 +01001870 + ((mCountX - 1) * mBorderSpace.x);
Adam Cohen2801caf2011-05-13 20:57:39 -07001871 }
1872
1873 public int getDesiredHeight() {
Jon Miranda228877d2021-02-09 11:05:00 -05001874 return getPaddingTop() + getPaddingBottom() + (mCountY * mCellHeight)
Thales Lima78d00ad2021-09-30 11:29:06 +01001875 + ((mCountY - 1) * mBorderSpace.y);
Adam Cohen2801caf2011-05-13 20:57:39 -07001876 }
1877
Michael Jurka66d72172011-04-12 16:29:25 -07001878 public boolean isOccupied(int x, int y) {
Schneider Victor-tulias750c5af2023-07-26 10:16:04 -07001879 if (x >= 0 && x < mCountX && y >= 0 && y < mCountY) {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001880 return mOccupied.cells[x][y];
Schneider Victor-tulias750c5af2023-07-26 10:16:04 -07001881 }
1882 if (BuildConfig.IS_STUDIO_BUILD) {
Michael Jurka66d72172011-04-12 16:29:25 -07001883 throw new RuntimeException("Position exceeds the bound of this CellLayout");
1884 }
Schneider Victor-tulias750c5af2023-07-26 10:16:04 -07001885 return true;
Michael Jurka66d72172011-04-12 16:29:25 -07001886 }
1887
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001888 @Override
1889 public ViewGroup.LayoutParams generateLayoutParams(AttributeSet attrs) {
Sebastian Francod4682992022-10-05 13:03:09 -05001890 return new CellLayoutLayoutParams(getContext(), attrs);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001891 }
1892
1893 @Override
1894 protected boolean checkLayoutParams(ViewGroup.LayoutParams p) {
Sebastian Francod4682992022-10-05 13:03:09 -05001895 return p instanceof CellLayoutLayoutParams;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001896 }
1897
1898 @Override
1899 protected ViewGroup.LayoutParams generateLayoutParams(ViewGroup.LayoutParams p) {
Sebastian Francod4682992022-10-05 13:03:09 -05001900 return new CellLayoutLayoutParams(p);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001901 }
1902
Samuel Fufa1e2d0042019-11-18 17:12:46 -08001903 /**
Tony Wickham86930612015-09-09 13:50:40 -07001904 * Returns whether an item can be placed in this CellLayout (after rearranging and/or resizing
1905 * if necessary).
1906 */
1907 public boolean hasReorderSolution(ItemInfo itemInfo) {
1908 int[] cellPoint = new int[2];
1909 // Check for a solution starting at every cell.
1910 for (int cellX = 0; cellX < getCountX(); cellX++) {
1911 for (int cellY = 0; cellY < getCountY(); cellY++) {
1912 cellToPoint(cellX, cellY, cellPoint);
1913 if (findReorderSolution(cellPoint[0], cellPoint[1], itemInfo.minSpanX,
1914 itemInfo.minSpanY, itemInfo.spanX, itemInfo.spanY, mDirectionVector, null,
Charlie Anderson46122392024-01-18 17:23:46 -05001915 true).isSolution) {
Tony Wickham86930612015-09-09 13:50:40 -07001916 return true;
1917 }
1918 }
1919 }
1920 return false;
1921 }
1922
Samuel Fufaa4211432020-02-25 18:47:54 -08001923 /**
1924 * Finds solution to accept hotseat migration to cell layout. commits solution if commitConfig
1925 */
1926 public boolean makeSpaceForHotseatMigration(boolean commitConfig) {
Samuel Fufaa4211432020-02-25 18:47:54 -08001927 int[] cellPoint = new int[2];
1928 int[] directionVector = new int[]{0, -1};
1929 cellToPoint(0, mCountY, cellPoint);
Charlie Anderson46122392024-01-18 17:23:46 -05001930 ItemConfiguration configuration = findReorderSolution(
1931 cellPoint[0] /* pixelX */,
1932 cellPoint[1] /* pixelY */,
1933 mCountX /* minSpanX */,
1934 1 /* minSpanY */,
1935 mCountX /* spanX */,
1936 1 /* spanY */,
1937 directionVector /* direction */,
1938 null /* dragView */,
1939 false /* decX */
1940 );
1941 if (configuration.isSolution) {
Samuel Fufaa4211432020-02-25 18:47:54 -08001942 if (commitConfig) {
1943 copySolutionToTempState(configuration, null);
Sunny Goyal711c5962021-06-23 12:36:18 -07001944 commitTempPlacement(null);
Samuel Fufa82bbdac2020-03-09 18:24:47 -07001945 // undo marking cells occupied since there is actually nothing being placed yet.
1946 mOccupied.markCells(0, mCountY - 1, mCountX, 1, false);
Samuel Fufaa4211432020-02-25 18:47:54 -08001947 }
1948 return true;
1949 }
1950 return false;
1951 }
1952
Samuel Fufa82bbdac2020-03-09 18:24:47 -07001953 /**
1954 * returns a copy of cell layout's grid occupancy
1955 */
1956 public GridOccupancy cloneGridOccupancy() {
1957 GridOccupancy occupancy = new GridOccupancy(mCountX, mCountY);
1958 mOccupied.copyTo(occupancy);
1959 return occupancy;
1960 }
1961
Sunny Goyal9ca9c132015-04-29 14:57:22 -07001962 public boolean isRegionVacant(int x, int y, int spanX, int spanY) {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001963 return mOccupied.isRegionVacant(x, y, spanX, spanY);
Sunny Goyal9ca9c132015-04-29 14:57:22 -07001964 }
Sebastian Franco9ea36d42023-09-21 13:56:42 -07001965
1966 public void setSpaceBetweenCellLayoutsPx(@Px int spaceBetweenCellLayoutsPx) {
1967 mSpaceBetweenCellLayoutsPx = spaceBetweenCellLayoutsPx;
1968 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001969}