blob: df5f52029d7856381966aafbe62fc0c13ec4b437 [file] [log] [blame]
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001/*
2 * Copyright (C) 2008 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Daniel Sandler325dc232013-06-05 22:57:57 -040017package com.android.launcher3;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080018
Tony Wickham0ac045f2021-11-03 13:17:02 -070019import static com.android.launcher3.dragndrop.DraggableView.DRAGGABLE_ICON;
Tony Wickham12784902021-11-03 14:02:10 -070020import static com.android.launcher3.icons.IconNormalizer.ICON_VISIBLE_AREA_FACTOR;
Sunny Goyal82dfc152023-02-24 16:50:09 -080021import static com.android.launcher3.util.MultiTranslateDelegate.INDEX_REORDER_PREVIEW_OFFSET;
Sunny Goyalf0b6db72018-08-13 16:10:14 -070022
Joe Onorato4be866d2010-10-10 11:26:02 -070023import android.animation.Animator;
Michael Jurka629758f2012-06-14 16:18:21 -070024import android.animation.AnimatorListenerAdapter;
Chet Haase00397b12010-10-07 11:13:10 -070025import android.animation.TimeInterpolator;
Patrick Dubroyde7658b2010-09-27 11:15:43 -070026import android.animation.ValueAnimator;
27import android.animation.ValueAnimator.AnimatorUpdateListener;
Sunny Goyal726bee72018-03-05 12:54:24 -080028import android.annotation.SuppressLint;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080029import android.content.Context;
Joe Onorato79e56262009-09-21 15:23:04 -040030import android.content.res.Resources;
Sunny Goyalc13403c2016-11-18 23:44:48 -080031import android.content.res.TypedArray;
Winson Chungaafa03c2010-06-11 17:34:16 -070032import android.graphics.Canvas;
Andrew Flynn0dca1ec2012-02-29 13:33:22 -080033import android.graphics.Color;
Joe Onorato4be866d2010-10-10 11:26:02 -070034import android.graphics.Paint;
Patrick Dubroyde7658b2010-09-27 11:15:43 -070035import android.graphics.Point;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080036import android.graphics.Rect;
Adam Cohenf7ca3b42021-02-22 11:03:58 -080037import android.graphics.RectF;
Patrick Dubroy6569f2c2010-07-12 14:25:18 -070038import android.graphics.drawable.Drawable;
Adam Cohen1462de32012-07-24 22:34:36 -070039import android.os.Parcelable;
Rajeev Kumar9962dbe2017-06-12 12:16:20 -070040import android.util.ArrayMap;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080041import android.util.AttributeSet;
Adam Cohenf7ca3b42021-02-22 11:03:58 -080042import android.util.FloatProperty;
Joe Onorato4be866d2010-10-10 11:26:02 -070043import android.util.Log;
Adam Cohen1462de32012-07-24 22:34:36 -070044import android.util.SparseArray;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080045import android.view.MotionEvent;
46import android.view.View;
47import android.view.ViewDebug;
48import android.view.ViewGroup;
Adam Cohenc9735cf2015-01-23 16:11:55 -080049import android.view.accessibility.AccessibilityEvent;
Sunny Goyal5bc6b6f2017-10-26 15:36:10 -070050
vadimt04f356f2019-02-14 18:46:36 -080051import androidx.annotation.IntDef;
Sebastian Franco5c8f8682023-11-14 09:52:41 -060052import androidx.annotation.Nullable;
Sebastian Franco9ea36d42023-09-21 13:56:42 -070053import androidx.annotation.Px;
Adam Cohenf7ca3b42021-02-22 11:03:58 -080054import androidx.core.graphics.ColorUtils;
vadimt04f356f2019-02-14 18:46:36 -080055import androidx.core.view.ViewCompat;
56
Kateryna Ivanova71203732023-05-24 15:09:00 +000057import com.android.app.animation.Interpolators;
Sunny Goyalaa8ef112015-06-12 20:04:41 -070058import com.android.launcher3.LauncherSettings.Favorites;
Sunny Goyale9b651e2015-04-24 11:44:51 -070059import com.android.launcher3.accessibility.DragAndDropAccessibilityDelegate;
Sebastian Francod4682992022-10-05 13:03:09 -050060import com.android.launcher3.celllayout.CellLayoutLayoutParams;
Sunny Goyal669b71f2023-01-27 14:37:07 -080061import com.android.launcher3.celllayout.CellPosMapper.CellPos;
Sebastian Franco5f0af4f2023-11-21 10:45:45 -060062import com.android.launcher3.celllayout.DelegatedCellDrawing;
63import com.android.launcher3.celllayout.ItemConfiguration;
Sebastian Francoe4c03452022-12-27 14:50:02 -060064import com.android.launcher3.celllayout.ReorderAlgorithm;
Sebastian Franco25f8e402023-12-02 13:54:05 -060065import com.android.launcher3.celllayout.ReorderParameters;
Sebastian Francof7654252024-01-18 11:50:50 -080066import com.android.launcher3.celllayout.ReorderPreviewAnimation;
Sunny Goyal3d706ad2017-03-06 16:56:39 -080067import com.android.launcher3.config.FeatureFlags;
Tony Wickham0ac045f2021-11-03 13:17:02 -070068import com.android.launcher3.dragndrop.DraggableView;
Jon Mirandaa0233f72017-06-22 18:34:45 -070069import com.android.launcher3.folder.PreviewBackground;
Sunny Goyale396abf2020-04-06 15:11:17 -070070import com.android.launcher3.model.data.ItemInfo;
Sebastian Francof153d912022-04-22 16:15:27 -050071import com.android.launcher3.model.data.LauncherAppWidgetInfo;
Sunny Goyalff4ba2d2016-04-02 14:12:34 -070072import com.android.launcher3.util.CellAndSpan;
73import com.android.launcher3.util.GridOccupancy;
Juan Sebastian Martineza91fbb22024-11-21 15:49:33 -080074import com.android.launcher3.util.MSDLPlayerWrapper;
Sunny Goyal82dfc152023-02-24 16:50:09 -080075import com.android.launcher3.util.MultiTranslateDelegate;
Sunny Goyale2fd14b2015-08-27 17:45:46 -070076import com.android.launcher3.util.ParcelableSparseArray;
Sunny Goyal9b29ca52017-02-17 10:39:44 -080077import com.android.launcher3.util.Themes;
Adam Cohen091440a2015-03-18 14:16:05 -070078import com.android.launcher3.util.Thunk;
Sunny Goyalab770a12018-11-14 15:17:26 -080079import com.android.launcher3.views.ActivityContext;
Steven Ng32427202021-04-19 18:12:12 +010080import com.android.launcher3.widget.LauncherAppWidgetHostView;
Sunny Goyal5bc6b6f2017-10-26 15:36:10 -070081
Juan Sebastian Martineza91fbb22024-11-21 15:49:33 -080082import com.google.android.msdl.data.model.MSDLToken;
83
Sunny Goyalc13403c2016-11-18 23:44:48 -080084import java.lang.annotation.Retention;
85import java.lang.annotation.RetentionPolicy;
Adam Cohen69ce2e52011-07-03 19:25:21 -070086import java.util.ArrayList;
Adam Cohenc0dcf592011-06-01 15:30:43 -070087import java.util.Arrays;
Adam Cohend41fbf52012-02-16 23:53:59 -080088import java.util.Stack;
Adam Cohenc0dcf592011-06-01 15:30:43 -070089
Sunny Goyalc4d32012020-04-03 17:10:11 -070090public class CellLayout extends ViewGroup {
Tony Wickhama0628cc2015-10-14 15:23:04 -070091 private static final String TAG = "CellLayout";
Stefan Andonianfb348912024-08-14 22:51:32 +000092 private static final boolean LOGD = true;
Winson Chungaafa03c2010-06-11 17:34:16 -070093
Tony Wickhamec6fd6f2023-03-11 02:08:57 +000094 /** The color of the "leave-behind" shape when a folder is opened from Hotseat. */
95 private static final int FOLDER_LEAVE_BEHIND_COLOR = Color.argb(160, 245, 245, 245);
96
Sunny Goyalab770a12018-11-14 15:17:26 -080097 protected final ActivityContext mActivity;
Sunny Goyal4ffec482016-02-09 11:28:52 -080098 @ViewDebug.ExportedProperty(category = "launcher")
Adam Cohen091440a2015-03-18 14:16:05 -070099 @Thunk int mCellWidth;
Sunny Goyal4ffec482016-02-09 11:28:52 -0800100 @ViewDebug.ExportedProperty(category = "launcher")
Adam Cohen091440a2015-03-18 14:16:05 -0700101 @Thunk int mCellHeight;
Winson Chung11a1a532013-09-13 11:14:45 -0700102 private int mFixedCellWidth;
103 private int mFixedCellHeight;
Jon Miranda228877d2021-02-09 11:05:00 -0500104 @ViewDebug.ExportedProperty(category = "launcher")
Sebastian Franco25423862023-03-10 10:50:37 -0800105 protected Point mBorderSpace;
Winson Chungaafa03c2010-06-11 17:34:16 -0700106
Sunny Goyal4ffec482016-02-09 11:28:52 -0800107 @ViewDebug.ExportedProperty(category = "launcher")
Sebastian Franco09589322022-11-02 15:25:58 -0700108 protected int mCountX;
Sunny Goyal4ffec482016-02-09 11:28:52 -0800109 @ViewDebug.ExportedProperty(category = "launcher")
Sebastian Franco09589322022-11-02 15:25:58 -0700110 protected int mCountY;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800111
Adam Cohen917e3882013-10-31 15:03:35 -0700112 private boolean mDropPending = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800113
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700114 // These are temporary variables to prevent having to allocate a new object just to
115 // return an (x, y) value from helper functions. Do NOT use them to maintain other state.
Adam Cohen091440a2015-03-18 14:16:05 -0700116 @Thunk final int[] mTmpPoint = new int[2];
Sunny Goyal2805e632015-05-20 15:35:32 -0700117 @Thunk final int[] mTempLocation = new int[2];
Sebastian Franco0dd5db82023-10-13 11:09:21 -0700118
119 @Thunk final Rect mTempOnDrawCellToRect = new Rect();
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<>();
Sunny Goyal638a6872024-04-25 15:55:11 -0700127 final PreviewBackground mFolderLeaveBehind = new PreviewBackground(getContext());
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;
Stefan Andonianfb348912024-08-14 22:51:32 +0000172 public boolean mHasOnLayoutBeenCalled = false;
Joe Onorato4be866d2010-10-10 11:26:02 -0700173
Rajeev Kumar9962dbe2017-06-12 12:16:20 -0700174 private final TimeInterpolator mEaseOutInterpolator;
Sebastian Franco09589322022-11-02 15:25:58 -0700175 protected final ShortcutAndWidgetContainer mShortcutsAndWidgets;
Sebastian Franco9ea36d42023-09-21 13:56:42 -0700176 @Px
177 protected int mSpaceBetweenCellLayoutsPx = 0;
Patrick Dubroyce34a972010-10-19 10:34:32 -0700178
Sunny Goyalc13403c2016-11-18 23:44:48 -0800179 @Retention(RetentionPolicy.SOURCE)
180 @IntDef({WORKSPACE, HOTSEAT, FOLDER})
181 public @interface ContainerType{}
182 public static final int WORKSPACE = 0;
183 public static final int HOTSEAT = 1;
184 public static final int FOLDER = 2;
185
186 @ContainerType private final int mContainerType;
187
Sebastian Francof7654252024-01-18 11:50:50 -0800188 public static final float DEFAULT_SCALE = 1f;
Andrew Flynn0dca1ec2012-02-29 13:33:22 -0800189
Adam Cohenfa3c58f2013-12-06 16:10:55 -0800190 public static final int MODE_SHOW_REORDER_HINT = 0;
191 public static final int MODE_DRAG_OVER = 1;
192 public static final int MODE_ON_DROP = 2;
193 public static final int MODE_ON_DROP_EXTERNAL = 3;
194 public static final int MODE_ACCEPT_DROP = 4;
Adam Cohen19f37922012-03-21 11:59:11 -0700195 private static final boolean DESTRUCTIVE_REORDER = false;
Adam Cohen482ed822012-03-02 14:15:13 -0800196 private static final boolean DEBUG_VISUALIZE_OCCUPIED = false;
197
Sebastian Francof7654252024-01-18 11:50:50 -0800198 public static final float REORDER_PREVIEW_MAGNITUDE = 0.12f;
199 public static final int REORDER_ANIMATION_DURATION = 150;
Sunny Goyalc13403c2016-11-18 23:44:48 -0800200 @Thunk final float mReorderPreviewAnimationMagnitude;
Adam Cohen19f37922012-03-21 11:59:11 -0700201
Sebastian Francoe4c03452022-12-27 14:50:02 -0600202 public final int[] mDirectionVector = new int[2];
Steven Ng30dd1d62021-03-15 21:45:49 +0000203
Sebastian Franco53a15a42022-10-25 17:28:54 -0700204 ItemConfiguration mPreviousSolution = null;
Adam Cohen482ed822012-03-02 14:15:13 -0800205
Sunny Goyal2805e632015-05-20 15:35:32 -0700206 private final Rect mTempRect = new Rect();
Winson Chung3a6e7f32013-10-09 15:50:52 -0700207
Sunny Goyal73b5a272019-12-09 14:55:56 -0800208 private static final Paint sPaint = new Paint();
Romain Guy8a0bff52012-05-06 13:14:33 -0700209
Juan Sebastian Martineza91fbb22024-11-21 15:49:33 -0800210 private final MSDLPlayerWrapper mMSDLPlayerWrapper;
211
Adam Cohenc9735cf2015-01-23 16:11:55 -0800212 // Related to accessible drag and drop
Adam Cohen6e7c37a2020-06-25 19:22:37 -0700213 DragAndDropAccessibilityDelegate mTouchHelper;
Adam Cohenc9735cf2015-01-23 16:11:55 -0800214
Sebastian Franco2986e0b2024-01-25 10:23:39 -0800215 CellLayoutContainer mCellLayoutContainer;
Adam Cohenf7ca3b42021-02-22 11:03:58 -0800216
217 public static final FloatProperty<CellLayout> SPRING_LOADED_PROGRESS =
218 new FloatProperty<CellLayout>("spring_loaded_progress") {
219 @Override
220 public Float get(CellLayout cl) {
221 return cl.getSpringLoadedProgress();
222 }
223
224 @Override
225 public void setValue(CellLayout cl, float progress) {
226 cl.setSpringLoadedProgress(progress);
227 }
228 };
229
Sebastian Franco2986e0b2024-01-25 10:23:39 -0800230 public CellLayout(Context context, CellLayoutContainer container) {
231 this(context, (AttributeSet) null);
232 this.mCellLayoutContainer = container;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800233 }
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
Juan Sebastian Martineza91fbb22024-11-21 15:49:33 -0800245 mMSDLPlayerWrapper = MSDLPlayerWrapper.INSTANCE.get(context);
246
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700247 // A ViewGroup usually does not draw, but CellLayout needs to draw a rectangle to show
248 // the user where a dragged item will land when dropped.
249 setWillNotDraw(false);
Romain Guyce3cbd12013-02-25 15:00:36 -0800250 setClipToPadding(false);
Pat Manningd0f729d2023-01-09 12:04:25 +0000251 setClipChildren(false);
Sunny Goyalab770a12018-11-14 15:17:26 -0800252 mActivity = ActivityContext.lookupContext(context);
Steven Ngcc505b82021-03-18 23:04:35 +0000253 DeviceProfile deviceProfile = mActivity.getDeviceProfile();
Michael Jurkaa63c4522010-08-19 13:52:27 -0700254
Thales Lima8cd020b2022-03-15 20:15:14 +0000255 resetCellSizeInternal(deviceProfile);
Sunny Goyalff4ba2d2016-04-02 14:12:34 -0700256
Steven Ngcc505b82021-03-18 23:04:35 +0000257 mCountX = deviceProfile.inv.numColumns;
258 mCountY = deviceProfile.inv.numRows;
Sunny Goyalff4ba2d2016-04-02 14:12:34 -0700259 mOccupied = new GridOccupancy(mCountX, mCountY);
260 mTmpOccupied = new GridOccupancy(mCountX, mCountY);
261
Samuel Fufa1e2d0042019-11-18 17:12:46 -0800262 mFolderLeaveBehind.mDelegateCellX = -1;
263 mFolderLeaveBehind.mDelegateCellY = -1;
Adam Cohenefca0272016-02-24 19:19:06 -0800264
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800265 setAlwaysDrawnWithCacheEnabled(false);
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700266
Adam Cohenf7ca3b42021-02-22 11:03:58 -0800267 Resources res = getResources();
268
269 mBackground = getContext().getDrawable(R.drawable.bg_celllayout);
Sunny Goyal2805e632015-05-20 15:35:32 -0700270 mBackground.setCallback(this);
Sunny Goyalaeb16432017-10-16 11:46:41 -0700271 mBackground.setAlpha(0);
Michael Jurka33945b22010-12-21 18:19:38 -0800272
Yogisha Dixitc0ac1dd2021-05-29 00:26:25 +0100273 mGridColor = Themes.getAttrColor(getContext(), R.attr.workspaceAccentColor);
Adam Cohenf7ca3b42021-02-22 11:03:58 -0800274 mGridVisualizationRoundingRadius =
275 res.getDimensionPixelSize(R.dimen.grid_visualization_rounding_radius);
Steven Ngcc505b82021-03-18 23:04:35 +0000276 mReorderPreviewAnimationMagnitude = (REORDER_PREVIEW_MAGNITUDE * deviceProfile.iconSizePx);
Adam Cohen19f37922012-03-21 11:59:11 -0700277
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700278 // Initialize the data structures used for the drag visualization.
Kateryna Ivanova71203732023-05-24 15:09:00 +0000279 mEaseOutInterpolator = Interpolators.DECELERATE_QUINT; // Quint ease out
Winson Chungb8c69f32011-10-19 21:36:08 -0700280 mDragCell[0] = mDragCell[1] = -1;
Adam Cohenf7ca3b42021-02-22 11:03:58 -0800281 mDragCellSpan[0] = mDragCellSpan[1] = -1;
Joe Onorato4be866d2010-10-10 11:26:02 -0700282 for (int i = 0; i < mDragOutlines.length; i++) {
Sunny Goyal669b71f2023-01-27 14:37:07 -0800283 mDragOutlines[i] = new CellLayoutLayoutParams(0, 0, 0, 0);
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700284 }
Mario Bertschler54ba6012017-06-08 10:53:53 -0700285 mDragOutlinePaint.setColor(Themes.getAttrColor(context, R.attr.workspaceTextColor));
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700286
287 // When dragging things around the home screens, we show a green outline of
288 // where the item will land. The outlines gradually fade out, leaving a trail
289 // behind the drag path.
290 // Set up all the animations that are used to implement this fading.
291 final int duration = res.getInteger(R.integer.config_dragOutlineFadeTime);
Chet Haase472b2812010-10-14 07:02:04 -0700292 final float fromAlphaValue = 0;
293 final float toAlphaValue = (float)res.getInteger(R.integer.config_dragOutlineMaxAlpha);
Joe Onorato4be866d2010-10-10 11:26:02 -0700294
Patrick Dubroy8e58e912010-10-14 13:21:48 -0700295 Arrays.fill(mDragOutlineAlphas, fromAlphaValue);
Joe Onorato4be866d2010-10-10 11:26:02 -0700296
297 for (int i = 0; i < mDragOutlineAnims.length; i++) {
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700298 final InterruptibleInOutAnimator anim =
Sebastian Francof153d912022-04-22 16:15:27 -0500299 new InterruptibleInOutAnimator(duration, fromAlphaValue, toAlphaValue);
Patrick Dubroyce34a972010-10-19 10:34:32 -0700300 anim.getAnimator().setInterpolator(mEaseOutInterpolator);
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700301 final int thisIndex = i;
Chet Haase472b2812010-10-14 07:02:04 -0700302 anim.getAnimator().addUpdateListener(new AnimatorUpdateListener() {
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700303 public void onAnimationUpdate(ValueAnimator animation) {
Joe Onorato4be866d2010-10-10 11:26:02 -0700304 // If an animation is started and then stopped very quickly, we can still
305 // get spurious updates we've cleared the tag. Guard against this.
Adam Cohenf7ca3b42021-02-22 11:03:58 -0800306 mDragOutlineAlphas[thisIndex] = (Float) animation.getAnimatedValue();
307 CellLayout.this.invalidate();
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700308 }
309 });
Joe Onorato4be866d2010-10-10 11:26:02 -0700310 // The animation holds a reference to the drag outline bitmap as long is it's
311 // running. This way the bitmap can be GCed when the animations are complete.
Joe Onorato4be866d2010-10-10 11:26:02 -0700312 mDragOutlineAnims[i] = anim;
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700313 }
Patrick Dubroyce34a972010-10-19 10:34:32 -0700314
Sunny Goyalc13403c2016-11-18 23:44:48 -0800315 mShortcutsAndWidgets = new ShortcutAndWidgetContainer(context, mContainerType);
Jon Miranda228877d2021-02-09 11:05:00 -0500316 mShortcutsAndWidgets.setCellDimensions(mCellWidth, mCellHeight, mCountX, mCountY,
Thales Lima78d00ad2021-09-30 11:29:06 +0100317 mBorderSpace);
Michael Jurkaa52570f2012-03-20 03:18:20 -0700318 addView(mShortcutsAndWidgets);
Michael Jurka18014792010-10-14 09:01:34 -0700319 }
320
Sebastian Franco2986e0b2024-01-25 10:23:39 -0800321 public CellLayoutContainer getCellLayoutContainer() {
322 return mCellLayoutContainer;
323 }
324
325 public void setCellLayoutContainer(CellLayoutContainer cellLayoutContainer) {
326 mCellLayoutContainer = cellLayoutContainer;
327 }
328
Sunny Goyal9b180102020-03-11 10:02:29 -0700329 /**
330 * Sets or clears a delegate used for accessible drag and drop
331 */
332 public void setDragAndDropAccessibilityDelegate(DragAndDropAccessibilityDelegate delegate) {
Sunny Goyal9b180102020-03-11 10:02:29 -0700333 ViewCompat.setAccessibilityDelegate(this, delegate);
334
Adam Cohen6e7c37a2020-06-25 19:22:37 -0700335 mTouchHelper = delegate;
336 int accessibilityFlag = mTouchHelper != null
Sunny Goyal9b180102020-03-11 10:02:29 -0700337 ? IMPORTANT_FOR_ACCESSIBILITY_YES : IMPORTANT_FOR_ACCESSIBILITY_NO;
338 setImportantForAccessibility(accessibilityFlag);
339 getShortcutsAndWidgets().setImportantForAccessibility(accessibilityFlag);
Sunny Goyal384b5782021-02-09 22:50:02 -0800340 // ExploreByTouchHelper sets focusability. Clear it when the delegate is cleared.
341 setFocusable(delegate != null);
Adam Cohenc9735cf2015-01-23 16:11:55 -0800342 // Invalidate the accessibility hierarchy
343 if (getParent() != null) {
344 getParent().notifySubtreeAccessibilityStateChanged(
345 this, this, AccessibilityEvent.CONTENT_CHANGE_TYPE_SUBTREE);
346 }
347 }
348
Sunny Goyala4647b62021-02-02 13:45:34 -0800349 /**
350 * Returns the currently set accessibility delegate
351 */
352 public DragAndDropAccessibilityDelegate getDragAndDropAccessibilityDelegate() {
353 return mTouchHelper;
354 }
355
Adam Cohenc9735cf2015-01-23 16:11:55 -0800356 @Override
Adam Cohen6e7c37a2020-06-25 19:22:37 -0700357 public boolean dispatchHoverEvent(MotionEvent event) {
358 // Always attempt to dispatch hover events to accessibility first.
359 if (mTouchHelper != null && mTouchHelper.dispatchHoverEvent(event)) {
360 return true;
361 }
362 return super.dispatchHoverEvent(event);
363 }
364
365 @Override
Adam Cohenc9735cf2015-01-23 16:11:55 -0800366 public boolean onInterceptTouchEvent(MotionEvent ev) {
Winson Chungf9935182020-10-23 09:26:44 -0700367 return mTouchHelper != null
368 || (mInterceptTouchListener != null && mInterceptTouchListener.onTouch(this, ev));
Adam Cohenc9735cf2015-01-23 16:11:55 -0800369 }
370
Chris Craik01f2d7f2013-10-01 14:41:56 -0700371 public void enableHardwareLayer(boolean hasLayer) {
372 mShortcutsAndWidgets.setLayerType(hasLayer ? LAYER_TYPE_HARDWARE : LAYER_TYPE_NONE, sPaint);
Michael Jurkad51f33a2012-06-28 15:35:26 -0700373 }
374
vadimt04f356f2019-02-14 18:46:36 -0800375 public boolean isHardwareLayerEnabled() {
376 return mShortcutsAndWidgets.getLayerType() == LAYER_TYPE_HARDWARE;
377 }
378
Thales Lima8cd020b2022-03-15 20:15:14 +0000379 /**
380 * Change sizes of cells
381 *
382 * @param width the new width of the cells
383 * @param height the new height of the cells
384 */
Winson Chung5f8afe62013-08-12 16:19:28 -0700385 public void setCellDimensions(int width, int height) {
Winson Chung11a1a532013-09-13 11:14:45 -0700386 mFixedCellWidth = mCellWidth = width;
387 mFixedCellHeight = mCellHeight = height;
Jon Miranda228877d2021-02-09 11:05:00 -0500388 mShortcutsAndWidgets.setCellDimensions(mCellWidth, mCellHeight, mCountX, mCountY,
Thales Lima78d00ad2021-09-30 11:29:06 +0100389 mBorderSpace);
Winson Chung5f8afe62013-08-12 16:19:28 -0700390 }
391
Thales Lima8cd020b2022-03-15 20:15:14 +0000392 private void resetCellSizeInternal(DeviceProfile deviceProfile) {
393 switch (mContainerType) {
394 case FOLDER:
Jordan Silva637f4eb2023-06-13 11:21:53 +0100395 mBorderSpace = new Point(deviceProfile.folderCellLayoutBorderSpacePx);
Thales Lima8cd020b2022-03-15 20:15:14 +0000396 break;
397 case HOTSEAT:
398 mBorderSpace = new Point(deviceProfile.hotseatBorderSpace,
399 deviceProfile.hotseatBorderSpace);
400 break;
401 case WORKSPACE:
402 default:
403 mBorderSpace = new Point(deviceProfile.cellLayoutBorderSpacePx);
404 break;
405 }
406
407 mCellWidth = mCellHeight = -1;
408 mFixedCellWidth = mFixedCellHeight = -1;
409 }
410
411 /**
412 * Reset the cell sizes and border space
413 */
414 public void resetCellSize(DeviceProfile deviceProfile) {
415 resetCellSizeInternal(deviceProfile);
416 requestLayout();
417 }
418
Adam Cohen2801caf2011-05-13 20:57:39 -0700419 public void setGridSize(int x, int y) {
420 mCountX = x;
421 mCountY = y;
Sunny Goyalff4ba2d2016-04-02 14:12:34 -0700422 mOccupied = new GridOccupancy(mCountX, mCountY);
423 mTmpOccupied = new GridOccupancy(mCountX, mCountY);
Jon Miranda228877d2021-02-09 11:05:00 -0500424 mShortcutsAndWidgets.setCellDimensions(mCellWidth, mCellHeight, mCountX, mCountY,
Thales Lima78d00ad2021-09-30 11:29:06 +0100425 mBorderSpace);
Adam Cohen76fc0852011-06-17 13:26:23 -0700426 requestLayout();
Adam Cohen2801caf2011-05-13 20:57:39 -0700427 }
428
Adam Cohen2374abf2013-04-16 14:56:57 -0700429 // Set whether or not to invert the layout horizontally if the layout is in RTL mode.
430 public void setInvertIfRtl(boolean invert) {
431 mShortcutsAndWidgets.setInvertIfRtl(invert);
432 }
433
Adam Cohen917e3882013-10-31 15:03:35 -0700434 public void setDropPending(boolean pending) {
435 mDropPending = pending;
436 }
437
438 public boolean isDropPending() {
439 return mDropPending;
440 }
441
Adam Cohenc50438c2014-08-19 17:43:05 -0700442 void setIsDragOverlapping(boolean isDragOverlapping) {
443 if (mIsDragOverlapping != isDragOverlapping) {
444 mIsDragOverlapping = isDragOverlapping;
Sunny Goyalf5440cb2016-12-14 15:13:00 -0800445 mBackground.setState(mIsDragOverlapping
446 ? BACKGROUND_STATE_ACTIVE : BACKGROUND_STATE_DEFAULT);
Adam Cohenc50438c2014-08-19 17:43:05 -0700447 invalidate();
448 }
449 }
450
Sunny Goyale2fd14b2015-08-27 17:45:46 -0700451 @Override
452 protected void dispatchSaveInstanceState(SparseArray<Parcelable> container) {
Sunny Goyal7ce471b2017-08-02 03:37:39 -0700453 ParcelableSparseArray jail = getJailedArray(container);
454 super.dispatchSaveInstanceState(jail);
455 container.put(R.id.cell_layout_jail_id, jail);
Sunny Goyale2fd14b2015-08-27 17:45:46 -0700456 }
457
458 @Override
459 protected void dispatchRestoreInstanceState(SparseArray<Parcelable> container) {
Sunny Goyal7ce471b2017-08-02 03:37:39 -0700460 super.dispatchRestoreInstanceState(getJailedArray(container));
Sunny Goyale2fd14b2015-08-27 17:45:46 -0700461 }
462
Sunny Goyal7ce471b2017-08-02 03:37:39 -0700463 /**
464 * Wrap the SparseArray in another Parcelable so that the item ids do not conflict with our
465 * our internal resource ids
466 */
Sunny Goyale2fd14b2015-08-27 17:45:46 -0700467 private ParcelableSparseArray getJailedArray(SparseArray<Parcelable> container) {
468 final Parcelable parcelable = container.get(R.id.cell_layout_jail_id);
469 return parcelable instanceof ParcelableSparseArray ?
470 (ParcelableSparseArray) parcelable : new ParcelableSparseArray();
471 }
472
Tony Wickham0f97b782015-12-02 17:55:07 -0800473 public boolean getIsDragOverlapping() {
474 return mIsDragOverlapping;
475 }
476
Jeff Sharkey83f111d2009-04-20 21:03:13 -0700477 @Override
Patrick Dubroy1262e362010-10-06 15:49:50 -0700478 protected void onDraw(Canvas canvas) {
Michael Jurka3e7c7632010-10-02 16:01:03 -0700479 // When we're large, we are either drawn in a "hover" state (ie when dragging an item to
480 // a neighboring page) or with just a normal background (if backgroundAlpha > 0.0f)
481 // When we're small, we are either drawn normally or in the "accepts drops" state (during
482 // a drag). However, we also drag the mini hover background *over* one of those two
483 // backgrounds
Sunny Goyalaeb16432017-10-16 11:46:41 -0700484 if (mBackground.getAlpha() > 0) {
Sunny Goyal2805e632015-05-20 15:35:32 -0700485 mBackground.draw(canvas);
Michael Jurkaa63c4522010-08-19 13:52:27 -0700486 }
Romain Guya6abce82009-11-10 02:54:41 -0800487
Adam Cohen482ed822012-03-02 14:15:13 -0800488 if (DEBUG_VISUALIZE_OCCUPIED) {
Tony Wickham0ac045f2021-11-03 13:17:02 -0700489 Rect cellBounds = new Rect();
490 // Will contain the bounds of the cell including spacing between cells.
491 Rect cellBoundsWithSpacing = new Rect();
Tony Wickham12784902021-11-03 14:02:10 -0700492 int[] targetCell = new int[2];
Tony Wickham0ac045f2021-11-03 13:17:02 -0700493 int[] cellCenter = new int[2];
494 Paint debugPaint = new Paint();
495 debugPaint.setStrokeWidth(Utilities.dpToPx(1));
496 for (int x = 0; x < mCountX; x++) {
497 for (int y = 0; y < mCountY; y++) {
498 if (!mOccupied.cells[x][y]) {
499 continue;
Adam Cohen482ed822012-03-02 14:15:13 -0800500 }
Tony Wickham12784902021-11-03 14:02:10 -0700501 targetCell[0] = x;
502 targetCell[1] = y;
Tony Wickham0ac045f2021-11-03 13:17:02 -0700503
Tony Wickham12784902021-11-03 14:02:10 -0700504 boolean canCreateFolder = canCreateFolder(getChildAt(x, y));
Tony Wickham0ac045f2021-11-03 13:17:02 -0700505 cellToRect(x, y, 1, 1, cellBounds);
506 cellBoundsWithSpacing.set(cellBounds);
507 cellBoundsWithSpacing.inset(-mBorderSpace.x / 2, -mBorderSpace.y / 2);
Tony Wickham3cfa5ed2021-11-03 13:20:43 -0700508 getWorkspaceCellVisualCenter(x, y, cellCenter);
Tony Wickham0ac045f2021-11-03 13:17:02 -0700509
510 canvas.save();
511 canvas.clipRect(cellBoundsWithSpacing);
512
513 // Draw reorder drag target.
514 debugPaint.setColor(Color.RED);
Sebastian Franco6e1024e2022-07-29 13:46:49 -0700515 canvas.drawCircle(cellCenter[0], cellCenter[1],
516 getReorderRadius(targetCell, 1, 1), debugPaint);
Tony Wickham0ac045f2021-11-03 13:17:02 -0700517
518 // Draw folder creation drag target.
519 if (canCreateFolder) {
520 debugPaint.setColor(Color.GREEN);
521 canvas.drawCircle(cellCenter[0], cellCenter[1],
Tony Wickham12784902021-11-03 14:02:10 -0700522 getFolderCreationRadius(targetCell), debugPaint);
Tony Wickham0ac045f2021-11-03 13:17:02 -0700523 }
524
525 canvas.restore();
Adam Cohen482ed822012-03-02 14:15:13 -0800526 }
527 }
528 }
529
Samuel Fufa1e2d0042019-11-18 17:12:46 -0800530 for (int i = 0; i < mDelegatedCellDrawings.size(); i++) {
531 DelegatedCellDrawing cellDrawing = mDelegatedCellDrawings.get(i);
532 cellToPoint(cellDrawing.mDelegateCellX, cellDrawing.mDelegateCellY, mTempLocation);
Adam Cohenefca0272016-02-24 19:19:06 -0800533 canvas.save();
534 canvas.translate(mTempLocation[0], mTempLocation[1]);
Samuel Fufa1e2d0042019-11-18 17:12:46 -0800535 cellDrawing.drawUnderItem(canvas);
Adam Cohenefca0272016-02-24 19:19:06 -0800536 canvas.restore();
Adam Cohen69ce2e52011-07-03 19:25:21 -0700537 }
Adam Cohenc51934b2011-07-26 21:07:43 -0700538
Samuel Fufa1e2d0042019-11-18 17:12:46 -0800539 if (mFolderLeaveBehind.mDelegateCellX >= 0 && mFolderLeaveBehind.mDelegateCellY >= 0) {
540 cellToPoint(mFolderLeaveBehind.mDelegateCellX,
541 mFolderLeaveBehind.mDelegateCellY, mTempLocation);
Adam Cohenefca0272016-02-24 19:19:06 -0800542 canvas.save();
543 canvas.translate(mTempLocation[0], mTempLocation[1]);
Tony Wickhamec6fd6f2023-03-11 02:08:57 +0000544 mFolderLeaveBehind.drawLeaveBehind(canvas, FOLDER_LEAVE_BEHIND_COLOR);
Adam Cohenefca0272016-02-24 19:19:06 -0800545 canvas.restore();
Adam Cohenc51934b2011-07-26 21:07:43 -0700546 }
Adam Cohen65086992020-02-19 08:40:49 -0800547
Adam Cohenf7ca3b42021-02-22 11:03:58 -0800548 if (mVisualizeCells || mVisualizeDropLocation) {
Adam Cohen65086992020-02-19 08:40:49 -0800549 visualizeGrid(canvas);
550 }
551 }
552
Adam Cohenf7ca3b42021-02-22 11:03:58 -0800553 /**
Tony Wickham12784902021-11-03 14:02:10 -0700554 * Returns whether dropping an icon on the given View can create (or add to) a folder.
555 */
556 private boolean canCreateFolder(View child) {
557 return child instanceof DraggableView
558 && ((DraggableView) child).getViewType() == DRAGGABLE_ICON;
559 }
560
561 /**
Adam Cohenf7ca3b42021-02-22 11:03:58 -0800562 * Indicates the progress of the Workspace entering the SpringLoaded state; allows the
563 * CellLayout to update various visuals for this state.
564 *
565 * @param progress
566 */
567 public void setSpringLoadedProgress(float progress) {
568 if (Float.compare(progress, mSpringLoadedProgress) != 0) {
569 mSpringLoadedProgress = progress;
fbarone74256b2023-04-10 14:50:31 -0700570 updateBgAlpha();
Adam Cohenf7ca3b42021-02-22 11:03:58 -0800571 setGridAlpha(progress);
572 }
573 }
574
575 /**
576 * See setSpringLoadedProgress
577 * @return progress
578 */
579 public float getSpringLoadedProgress() {
580 return mSpringLoadedProgress;
581 }
582
Sebastian Franco09589322022-11-02 15:25:58 -0700583 protected void updateBgAlpha() {
Sebastian Franco2986e0b2024-01-25 10:23:39 -0800584 mBackground.setAlpha((int) (mSpringLoadedProgress * 255));
Adam Cohenf7ca3b42021-02-22 11:03:58 -0800585 }
586
587 /**
588 * Set the progress of this page's scroll
589 *
590 * @param progress 0 if the screen is centered, +/-1 if it is to the right / left respectively
591 */
592 public void setScrollProgress(float progress) {
593 if (Float.compare(Math.abs(progress), mScrollProgress) != 0) {
594 mScrollProgress = Math.abs(progress);
fbarone74256b2023-04-10 14:50:31 -0700595 updateBgAlpha();
Adam Cohenf7ca3b42021-02-22 11:03:58 -0800596 }
597 }
598
599 private void setGridAlpha(float gridAlpha) {
600 if (Float.compare(gridAlpha, mGridAlpha) != 0) {
601 mGridAlpha = gridAlpha;
602 invalidate();
603 }
604 }
605
Adam Cohen65086992020-02-19 08:40:49 -0800606 protected void visualizeGrid(Canvas canvas) {
Adam Cohen0c4d2782021-04-29 15:56:13 -0700607 DeviceProfile dp = mActivity.getDeviceProfile();
Alex Chau51da2192022-05-20 13:32:10 +0100608 int paddingX = Math.min((mCellWidth - dp.iconSizePx) / 2, dp.gridVisualizationPaddingX);
609 int paddingY = Math.min((mCellHeight - dp.iconSizePx) / 2, dp.gridVisualizationPaddingY);
Adam Cohen65086992020-02-19 08:40:49 -0800610
Adam Cohenf7ca3b42021-02-22 11:03:58 -0800611 mVisualizeGridPaint.setStrokeWidth(8);
Adam Cohen65086992020-02-19 08:40:49 -0800612
Sebastian Franco0dd5db82023-10-13 11:09:21 -0700613 // This is used for debugging purposes only
Adam Cohenf7ca3b42021-02-22 11:03:58 -0800614 if (mVisualizeCells) {
Sebastian Franco0dd5db82023-10-13 11:09:21 -0700615 int paintAlpha = (int) (120 * mGridAlpha);
616 mVisualizeGridPaint.setColor(ColorUtils.setAlphaComponent(mGridColor, paintAlpha));
Adam Cohenf7ca3b42021-02-22 11:03:58 -0800617 for (int i = 0; i < mCountX; i++) {
618 for (int j = 0; j < mCountY; j++) {
Sebastian Franco0dd5db82023-10-13 11:09:21 -0700619 cellToRect(i, j, 1, 1, mTempOnDrawCellToRect);
620 mVisualizeGridRect.set(mTempOnDrawCellToRect);
621 mVisualizeGridRect.inset(paddingX, paddingY);
Adam Cohenf7ca3b42021-02-22 11:03:58 -0800622 mVisualizeGridPaint.setStyle(Paint.Style.FILL);
623 canvas.drawRoundRect(mVisualizeGridRect, mGridVisualizationRoundingRadius,
624 mGridVisualizationRoundingRadius, mVisualizeGridPaint);
625 }
626 }
627 }
Adam Cohen65086992020-02-19 08:40:49 -0800628
fbarone74256b2023-04-10 14:50:31 -0700629 if (mVisualizeDropLocation) {
Adam Cohenf7ca3b42021-02-22 11:03:58 -0800630 for (int i = 0; i < mDragOutlines.length; i++) {
631 final float alpha = mDragOutlineAlphas[i];
632 if (alpha <= 0) continue;
Sebastian Franco0dd5db82023-10-13 11:09:21 -0700633 CellLayoutLayoutParams params = mDragOutlines[i];
634 cellToRect(params.getCellX(), params.getCellY(), params.cellHSpan, params.cellVSpan,
635 mTempOnDrawCellToRect);
636 mVisualizeGridRect.set(mTempOnDrawCellToRect);
637 mVisualizeGridRect.inset(paddingX, paddingY);
Adam Cohen65086992020-02-19 08:40:49 -0800638
Adam Cohenf7ca3b42021-02-22 11:03:58 -0800639 mVisualizeGridPaint.setAlpha(255);
Adam Cohen65086992020-02-19 08:40:49 -0800640 mVisualizeGridPaint.setStyle(Paint.Style.STROKE);
Adam Cohenf7ca3b42021-02-22 11:03:58 -0800641 mVisualizeGridPaint.setColor(Color.argb((int) (alpha),
642 Color.red(mGridColor), Color.green(mGridColor), Color.blue(mGridColor)));
Adam Cohen65086992020-02-19 08:40:49 -0800643
Sebastian Franco9ea36d42023-09-21 13:56:42 -0700644 canvas.save();
645 canvas.translate(getMarginForGivenCellParams(params), 0);
Adam Cohenf7ca3b42021-02-22 11:03:58 -0800646 canvas.drawRoundRect(mVisualizeGridRect, mGridVisualizationRoundingRadius,
647 mGridVisualizationRoundingRadius, mVisualizeGridPaint);
Sebastian Franco9ea36d42023-09-21 13:56:42 -0700648 canvas.restore();
Adam Cohen65086992020-02-19 08:40:49 -0800649 }
650 }
Adam Cohen69ce2e52011-07-03 19:25:21 -0700651 }
652
Sebastian Franco9ea36d42023-09-21 13:56:42 -0700653 protected float getMarginForGivenCellParams(CellLayoutLayoutParams params) {
654 return 0;
655 }
656
Adam Cohenefca0272016-02-24 19:19:06 -0800657 @Override
658 protected void dispatchDraw(Canvas canvas) {
659 super.dispatchDraw(canvas);
660
Samuel Fufa1e2d0042019-11-18 17:12:46 -0800661 for (int i = 0; i < mDelegatedCellDrawings.size(); i++) {
662 DelegatedCellDrawing bg = mDelegatedCellDrawings.get(i);
663 cellToPoint(bg.mDelegateCellX, bg.mDelegateCellY, mTempLocation);
664 canvas.save();
665 canvas.translate(mTempLocation[0], mTempLocation[1]);
666 bg.drawOverItem(canvas);
667 canvas.restore();
Adam Cohenefca0272016-02-24 19:19:06 -0800668 }
Adam Cohen69ce2e52011-07-03 19:25:21 -0700669 }
670
Samuel Fufa1e2d0042019-11-18 17:12:46 -0800671 /**
672 * Add Delegated cell drawing
673 */
674 public void addDelegatedCellDrawing(DelegatedCellDrawing bg) {
675 mDelegatedCellDrawings.add(bg);
Adam Cohenefca0272016-02-24 19:19:06 -0800676 }
Samuel Fufa1e2d0042019-11-18 17:12:46 -0800677
678 /**
679 * Remove item from DelegatedCellDrawings
680 */
681 public void removeDelegatedCellDrawing(DelegatedCellDrawing bg) {
682 mDelegatedCellDrawings.remove(bg);
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700683 }
684
Adam Cohenc51934b2011-07-26 21:07:43 -0700685 public void setFolderLeaveBehindCell(int x, int y) {
Adam Cohenefca0272016-02-24 19:19:06 -0800686 View child = getChildAt(x, y);
Sunny Goyalab770a12018-11-14 15:17:26 -0800687 mFolderLeaveBehind.setup(getContext(), mActivity, null,
Adam Cohenefca0272016-02-24 19:19:06 -0800688 child.getMeasuredWidth(), child.getPaddingTop());
689
Samuel Fufa1e2d0042019-11-18 17:12:46 -0800690 mFolderLeaveBehind.mDelegateCellX = x;
691 mFolderLeaveBehind.mDelegateCellY = y;
Adam Cohenc51934b2011-07-26 21:07:43 -0700692 invalidate();
693 }
694
695 public void clearFolderLeaveBehind() {
Samuel Fufa1e2d0042019-11-18 17:12:46 -0800696 mFolderLeaveBehind.mDelegateCellX = -1;
697 mFolderLeaveBehind.mDelegateCellY = -1;
Adam Cohenc51934b2011-07-26 21:07:43 -0700698 invalidate();
699 }
700
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700701 @Override
Michael Jurkae6235dd2011-10-04 15:02:05 -0700702 public boolean shouldDelayChildPressedState() {
703 return false;
704 }
705
Adam Cohen1462de32012-07-24 22:34:36 -0700706 public void restoreInstanceState(SparseArray<Parcelable> states) {
Sunny Goyal33a152f2014-07-22 12:13:14 -0700707 try {
708 dispatchRestoreInstanceState(states);
709 } catch (IllegalArgumentException ex) {
Zak Cohen3eeb41d2020-02-14 14:15:13 -0800710 if (FeatureFlags.IS_STUDIO_BUILD) {
Sunny Goyal33a152f2014-07-22 12:13:14 -0700711 throw ex;
712 }
713 // Mismatched viewId / viewType preventing restore. Skip restore on production builds.
714 Log.e(TAG, "Ignoring an error while restoring a view instance state", ex);
715 }
Adam Cohen1462de32012-07-24 22:34:36 -0700716 }
717
Michael Jurkae6235dd2011-10-04 15:02:05 -0700718 @Override
Jeff Sharkey83f111d2009-04-20 21:03:13 -0700719 public void cancelLongPress() {
720 super.cancelLongPress();
721
722 // Cancel long press for all children
723 final int count = getChildCount();
724 for (int i = 0; i < count; i++) {
725 final View child = getChildAt(i);
726 child.cancelLongPress();
727 }
728 }
729
Michael Jurkadee05892010-07-27 10:01:56 -0700730 public void setOnInterceptTouchListener(View.OnTouchListener listener) {
731 mInterceptTouchListener = listener;
732 }
733
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800734 public int getCountX() {
Adam Cohend22015c2010-07-26 22:02:18 -0700735 return mCountX;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800736 }
737
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800738 public int getCountY() {
Adam Cohend22015c2010-07-26 22:02:18 -0700739 return mCountY;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800740 }
741
Sunny Goyalc13403c2016-11-18 23:44:48 -0800742 public boolean acceptsWidget() {
743 return mContainerType == WORKSPACE;
Sunny Goyale9b651e2015-04-24 11:44:51 -0700744 }
745
Sebastian Francod4682992022-10-05 13:03:09 -0500746 /**
747 * Adds the given view to the CellLayout
748 *
749 * @param child view to add.
750 * @param index index of the CellLayout children where to add the view.
751 * @param childId id of the view.
752 * @param params represent the logic of the view on the CellLayout.
753 * @param markCells if the occupied cells should be marked or not
754 * @return if adding the view was successful
755 */
756 public boolean addViewToCellLayout(View child, int index, int childId,
757 CellLayoutLayoutParams params, boolean markCells) {
758 final CellLayoutLayoutParams lp = params;
Winson Chungaafa03c2010-06-11 17:34:16 -0700759
Andrew Flynnde38e422012-05-08 11:22:15 -0700760 // Hotseat icons - remove text
Andrew Flynn0dca1ec2012-02-29 13:33:22 -0800761 if (child instanceof BubbleTextView) {
762 BubbleTextView bubbleChild = (BubbleTextView) child;
Jon Mirandaf1eae802017-10-04 11:23:33 -0700763 bubbleChild.setTextVisibility(mContainerType != HOTSEAT);
Andrew Flynn0dca1ec2012-02-29 13:33:22 -0800764 }
765
Sebastian Francof7654252024-01-18 11:50:50 -0800766 child.setScaleX(DEFAULT_SCALE);
767 child.setScaleY(DEFAULT_SCALE);
Adam Cohen307fe232012-08-16 17:55:58 -0700768
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800769 // Generate an id for each view, this assumes we have at most 256x256 cells
770 // per workspace screen
Sebastian Franco877088e2023-01-03 15:16:22 -0700771 if (lp.getCellX() >= 0 && lp.getCellX() <= mCountX - 1
772 && lp.getCellY() >= 0 && lp.getCellY() <= mCountY - 1) {
Winson Chungaafa03c2010-06-11 17:34:16 -0700773 // If the horizontal or vertical span is set to -1, it is taken to
774 // mean that it spans the extent of the CellLayout
Adam Cohend22015c2010-07-26 22:02:18 -0700775 if (lp.cellHSpan < 0) lp.cellHSpan = mCountX;
776 if (lp.cellVSpan < 0) lp.cellVSpan = mCountY;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800777
Winson Chungaafa03c2010-06-11 17:34:16 -0700778 child.setId(childId);
Tony Wickhama0628cc2015-10-14 15:23:04 -0700779 if (LOGD) {
780 Log.d(TAG, "Adding view to ShortcutsAndWidgetsContainer: " + child);
781 }
Sunny Goyalf7a29e82015-04-24 15:20:43 -0700782 mShortcutsAndWidgets.addView(child, index, lp);
Michael Jurkadee05892010-07-27 10:01:56 -0700783
Michael Jurkaf3ca3ab2010-10-20 17:08:24 -0700784 if (markCells) markCellsAsOccupiedForView(child);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700785
Winson Chungaafa03c2010-06-11 17:34:16 -0700786 return true;
787 }
788 return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800789 }
Michael Jurka3e7c7632010-10-02 16:01:03 -0700790
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800791 @Override
Michael Jurka0280c3b2010-09-17 15:00:07 -0700792 public void removeAllViews() {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -0700793 mOccupied.clear();
Michael Jurkaa52570f2012-03-20 03:18:20 -0700794 mShortcutsAndWidgets.removeAllViews();
Michael Jurka0280c3b2010-09-17 15:00:07 -0700795 }
796
797 @Override
798 public void removeAllViewsInLayout() {
Michael Jurkaa52570f2012-03-20 03:18:20 -0700799 if (mShortcutsAndWidgets.getChildCount() > 0) {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -0700800 mOccupied.clear();
Michael Jurkaa52570f2012-03-20 03:18:20 -0700801 mShortcutsAndWidgets.removeAllViewsInLayout();
Michael Jurka7cfc2822011-08-02 20:19:24 -0700802 }
Michael Jurka0280c3b2010-09-17 15:00:07 -0700803 }
804
805 @Override
806 public void removeView(View view) {
807 markCellsAsUnoccupiedForView(view);
Michael Jurkaa52570f2012-03-20 03:18:20 -0700808 mShortcutsAndWidgets.removeView(view);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700809 }
810
811 @Override
812 public void removeViewAt(int index) {
Michael Jurkaa52570f2012-03-20 03:18:20 -0700813 markCellsAsUnoccupiedForView(mShortcutsAndWidgets.getChildAt(index));
814 mShortcutsAndWidgets.removeViewAt(index);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700815 }
816
817 @Override
818 public void removeViewInLayout(View view) {
819 markCellsAsUnoccupiedForView(view);
Michael Jurkaa52570f2012-03-20 03:18:20 -0700820 mShortcutsAndWidgets.removeViewInLayout(view);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700821 }
822
823 @Override
824 public void removeViews(int start, int count) {
825 for (int i = start; i < start + count; i++) {
Michael Jurkaa52570f2012-03-20 03:18:20 -0700826 markCellsAsUnoccupiedForView(mShortcutsAndWidgets.getChildAt(i));
Michael Jurka0280c3b2010-09-17 15:00:07 -0700827 }
Michael Jurkaa52570f2012-03-20 03:18:20 -0700828 mShortcutsAndWidgets.removeViews(start, count);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700829 }
830
831 @Override
832 public void removeViewsInLayout(int start, int count) {
833 for (int i = start; i < start + count; i++) {
Michael Jurkaa52570f2012-03-20 03:18:20 -0700834 markCellsAsUnoccupiedForView(mShortcutsAndWidgets.getChildAt(i));
Michael Jurka0280c3b2010-09-17 15:00:07 -0700835 }
Michael Jurkaa52570f2012-03-20 03:18:20 -0700836 mShortcutsAndWidgets.removeViewsInLayout(start, count);
Michael Jurkaabded662011-03-04 12:06:57 -0800837 }
838
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700839 /**
Winson Chungaafa03c2010-06-11 17:34:16 -0700840 * Given a point, return the cell that strictly encloses that point
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800841 * @param x X coordinate of the point
842 * @param y Y coordinate of the point
843 * @param result Array of 2 ints to hold the x and y coordinate of the cell
844 */
Sunny Goyale9b651e2015-04-24 11:44:51 -0700845 public void pointToCellExact(int x, int y, int[] result) {
Winson Chung4b825dcd2011-06-19 12:41:22 -0700846 final int hStartPadding = getPaddingLeft();
847 final int vStartPadding = getPaddingTop();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800848
Sebastian Francob57c0b22022-06-28 13:54:35 -0700849 result[0] = (x - hStartPadding) / (mCellWidth + mBorderSpace.x);
850 result[1] = (y - vStartPadding) / (mCellHeight + mBorderSpace.y);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800851
Adam Cohend22015c2010-07-26 22:02:18 -0700852 final int xAxis = mCountX;
853 final int yAxis = mCountY;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800854
855 if (result[0] < 0) result[0] = 0;
856 if (result[0] >= xAxis) result[0] = xAxis - 1;
857 if (result[1] < 0) result[1] = 0;
858 if (result[1] >= yAxis) result[1] = yAxis - 1;
859 }
Winson Chungaafa03c2010-06-11 17:34:16 -0700860
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800861 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800862 * Given a cell coordinate, return the point that represents the upper left corner of that cell
Winson Chungaafa03c2010-06-11 17:34:16 -0700863 *
864 * @param cellX X coordinate of the cell
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800865 * @param cellY Y coordinate of the cell
Winson Chungaafa03c2010-06-11 17:34:16 -0700866 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800867 * @param result Array of 2 ints to hold the x and y coordinate of the point
868 */
869 void cellToPoint(int cellX, int cellY, int[] result) {
Jon Miranda228877d2021-02-09 11:05:00 -0500870 cellToRect(cellX, cellY, 1, 1, mTempRect);
871 result[0] = mTempRect.left;
872 result[1] = mTempRect.top;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800873 }
874
Adam Cohene3e27a82011-04-15 12:07:39 -0700875 /**
Adam Cohen482ed822012-03-02 14:15:13 -0800876 * Given a cell coordinate, return the point that represents the center of the cell
Adam Cohene3e27a82011-04-15 12:07:39 -0700877 *
878 * @param cellX X coordinate of the cell
879 * @param cellY Y coordinate of the cell
880 *
881 * @param result Array of 2 ints to hold the x and y coordinate of the point
882 */
883 void cellToCenterPoint(int cellX, int cellY, int[] result) {
Adam Cohen47a876d2012-03-19 13:21:41 -0700884 regionToCenterPoint(cellX, cellY, 1, 1, result);
885 }
886
887 /**
Tony Wickham0ac045f2021-11-03 13:17:02 -0700888 * Given a cell coordinate and span return the point that represents the center of the region
Adam Cohen47a876d2012-03-19 13:21:41 -0700889 *
890 * @param cellX X coordinate of the cell
891 * @param cellY Y coordinate of the cell
892 *
893 * @param result Array of 2 ints to hold the x and y coordinate of the point
894 */
Sebastian Franco45b39b52023-01-10 10:47:46 -0600895 public void regionToCenterPoint(int cellX, int cellY, int spanX, int spanY, int[] result) {
Jon Miranda228877d2021-02-09 11:05:00 -0500896 cellToRect(cellX, cellY, spanX, spanY, mTempRect);
897 result[0] = mTempRect.centerX();
898 result[1] = mTempRect.centerY();
Adam Cohen19f37922012-03-21 11:59:11 -0700899 }
900
Tony Wickham3cfa5ed2021-11-03 13:20:43 -0700901 /**
902 * Returns the distance between the given coordinate and the visual center of the given cell.
903 */
904 public float getDistanceFromWorkspaceCellVisualCenter(float x, float y, int[] cell) {
905 getWorkspaceCellVisualCenter(cell[0], cell[1], mTmpPoint);
Sunny Goyalf7a29e82015-04-24 15:20:43 -0700906 return (float) Math.hypot(x - mTmpPoint[0], y - mTmpPoint[1]);
Adam Cohen482ed822012-03-02 14:15:13 -0800907 }
908
Tony Wickham3cfa5ed2021-11-03 13:20:43 -0700909 private void getWorkspaceCellVisualCenter(int cellX, int cellY, int[] outPoint) {
910 View child = getChildAt(cellX, cellY);
911 if (child instanceof DraggableView) {
912 DraggableView draggableChild = (DraggableView) child;
913 if (draggableChild.getViewType() == DRAGGABLE_ICON) {
914 cellToPoint(cellX, cellY, outPoint);
915 draggableChild.getWorkspaceVisualDragBounds(mTempRect);
916 mTempRect.offset(outPoint[0], outPoint[1]);
917 outPoint[0] = mTempRect.centerX();
918 outPoint[1] = mTempRect.centerY();
919 return;
920 }
921 }
922 cellToCenterPoint(cellX, cellY, outPoint);
923 }
924
Tony Wickham0ac045f2021-11-03 13:17:02 -0700925 /**
926 * Returns the max distance from the center of a cell that can accept a drop to create a folder.
927 */
Tony Wickham12784902021-11-03 14:02:10 -0700928 public float getFolderCreationRadius(int[] targetCell) {
Tony Wickham0ac045f2021-11-03 13:17:02 -0700929 DeviceProfile grid = mActivity.getDeviceProfile();
Tony Wickham12784902021-11-03 14:02:10 -0700930 float iconVisibleRadius = ICON_VISIBLE_AREA_FACTOR * grid.iconSizePx / 2;
931 // Halfway between reorder radius and icon.
Sebastian Franco6e1024e2022-07-29 13:46:49 -0700932 return (getReorderRadius(targetCell, 1, 1) + iconVisibleRadius) / 2;
Tony Wickham12784902021-11-03 14:02:10 -0700933 }
934
935 /**
936 * Returns the max distance from the center of a cell that will start to reorder on drag over.
937 */
Sebastian Franco6e1024e2022-07-29 13:46:49 -0700938 public float getReorderRadius(int[] targetCell, int spanX, int spanY) {
Tony Wickham12784902021-11-03 14:02:10 -0700939 int[] centerPoint = mTmpPoint;
940 getWorkspaceCellVisualCenter(targetCell[0], targetCell[1], centerPoint);
941
942 Rect cellBoundsWithSpacing = mTempRect;
Sebastian Franco6e1024e2022-07-29 13:46:49 -0700943 cellToRect(targetCell[0], targetCell[1], spanX, spanY, cellBoundsWithSpacing);
Tony Wickham12784902021-11-03 14:02:10 -0700944 cellBoundsWithSpacing.inset(-mBorderSpace.x / 2, -mBorderSpace.y / 2);
945
Sebastian Francoc8392ea2022-10-28 16:38:37 -0700946 if (canCreateFolder(getChildAt(targetCell[0], targetCell[1])) && spanX == 1 && spanY == 1) {
Tony Wickham12784902021-11-03 14:02:10 -0700947 // Take only the circle in the smaller dimension, to ensure we don't start reordering
948 // too soon before accepting a folder drop.
949 int minRadius = centerPoint[0] - cellBoundsWithSpacing.left;
950 minRadius = Math.min(minRadius, centerPoint[1] - cellBoundsWithSpacing.top);
951 minRadius = Math.min(minRadius, cellBoundsWithSpacing.right - centerPoint[0]);
952 minRadius = Math.min(minRadius, cellBoundsWithSpacing.bottom - centerPoint[1]);
953 return minRadius;
954 }
955 // Take up the entire cell, including space between this cell and the adjacent ones.
Sebastian Francoc8392ea2022-10-28 16:38:37 -0700956 // Multiply by span to scale radius
957 return (float) Math.hypot(spanX * cellBoundsWithSpacing.width() / 2f,
958 spanY * cellBoundsWithSpacing.height() / 2f);
Tony Wickham0ac045f2021-11-03 13:17:02 -0700959 }
960
Adam Cohenf9c184a2016-01-15 16:47:43 -0800961 public int getCellWidth() {
Romain Guy84f296c2009-11-04 15:00:44 -0800962 return mCellWidth;
963 }
964
Sunny Goyal0b754e52017-08-07 07:42:45 -0700965 public int getCellHeight() {
Romain Guy84f296c2009-11-04 15:00:44 -0800966 return mCellHeight;
967 }
968
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700969 public void setFixedSize(int width, int height) {
970 mFixedWidth = width;
971 mFixedHeight = height;
972 }
973
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800974 @Override
975 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800976 int widthSpecMode = MeasureSpec.getMode(widthMeasureSpec);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800977 int heightSpecMode = MeasureSpec.getMode(heightMeasureSpec);
Winson Chung5f8afe62013-08-12 16:19:28 -0700978 int widthSize = MeasureSpec.getSize(widthMeasureSpec);
979 int heightSize = MeasureSpec.getSize(heightMeasureSpec);
Winson Chung2d75f122013-09-23 16:53:31 -0700980 int childWidthSize = widthSize - (getPaddingLeft() + getPaddingRight());
981 int childHeightSize = heightSize - (getPaddingTop() + getPaddingBottom());
Sunny Goyalae6e3182019-04-30 12:04:37 -0700982
Winson Chung11a1a532013-09-13 11:14:45 -0700983 if (mFixedCellWidth < 0 || mFixedCellHeight < 0) {
Thales Lima78d00ad2021-09-30 11:29:06 +0100984 int cw = DeviceProfile.calculateCellWidth(childWidthSize, mBorderSpace.x,
Jon Miranda228877d2021-02-09 11:05:00 -0500985 mCountX);
Thales Lima78d00ad2021-09-30 11:29:06 +0100986 int ch = DeviceProfile.calculateCellHeight(childHeightSize, mBorderSpace.y,
Jon Miranda228877d2021-02-09 11:05:00 -0500987 mCountY);
Winson Chung11a1a532013-09-13 11:14:45 -0700988 if (cw != mCellWidth || ch != mCellHeight) {
989 mCellWidth = cw;
990 mCellHeight = ch;
Jon Miranda228877d2021-02-09 11:05:00 -0500991 mShortcutsAndWidgets.setCellDimensions(mCellWidth, mCellHeight, mCountX, mCountY,
Thales Lima78d00ad2021-09-30 11:29:06 +0100992 mBorderSpace);
Winson Chung11a1a532013-09-13 11:14:45 -0700993 }
Winson Chung5f8afe62013-08-12 16:19:28 -0700994 }
Winson Chungaafa03c2010-06-11 17:34:16 -0700995
Winson Chung2d75f122013-09-23 16:53:31 -0700996 int newWidth = childWidthSize;
997 int newHeight = childHeightSize;
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700998 if (mFixedWidth > 0 && mFixedHeight > 0) {
999 newWidth = mFixedWidth;
1000 newHeight = mFixedHeight;
1001 } else if (widthSpecMode == MeasureSpec.UNSPECIFIED || heightSpecMode == MeasureSpec.UNSPECIFIED) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001002 throw new RuntimeException("CellLayout cannot have UNSPECIFIED dimensions");
1003 }
1004
Sunny Goyal4fe5a372015-05-14 19:55:10 -07001005 mShortcutsAndWidgets.measure(
1006 MeasureSpec.makeMeasureSpec(newWidth, MeasureSpec.EXACTLY),
1007 MeasureSpec.makeMeasureSpec(newHeight, MeasureSpec.EXACTLY));
1008
1009 int maxWidth = mShortcutsAndWidgets.getMeasuredWidth();
1010 int maxHeight = mShortcutsAndWidgets.getMeasuredHeight();
Winson Chung2d75f122013-09-23 16:53:31 -07001011 if (mFixedWidth > 0 && mFixedHeight > 0) {
1012 setMeasuredDimension(maxWidth, maxHeight);
1013 } else {
1014 setMeasuredDimension(widthSize, heightSize);
1015 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001016 }
1017
1018 @Override
Michael Jurka28750fb2010-09-24 17:43:49 -07001019 protected void onLayout(boolean changed, int l, int t, int r, int b) {
Stefan Andonianfb348912024-08-14 22:51:32 +00001020 mHasOnLayoutBeenCalled = true; // b/349929393 - is the required call to onLayout not done?
Tony Wickham26b01422015-11-10 14:44:32 -08001021 int left = getPaddingLeft();
Sunny Goyal7ce471b2017-08-02 03:37:39 -07001022 left += (int) Math.ceil(getUnusedHorizontalSpace() / 2f);
Sunny Goyal7c786f72016-06-01 14:08:21 -07001023 int right = r - l - getPaddingRight();
Sunny Goyal7ce471b2017-08-02 03:37:39 -07001024 right -= (int) Math.ceil(getUnusedHorizontalSpace() / 2f);
Sunny Goyal7c786f72016-06-01 14:08:21 -07001025
Winson Chung38848ca2013-10-08 12:03:44 -07001026 int top = getPaddingTop();
Sunny Goyal7c786f72016-06-01 14:08:21 -07001027 int bottom = b - t - getPaddingBottom();
Sunny Goyal4fe5a372015-05-14 19:55:10 -07001028
Sunny Goyal7c786f72016-06-01 14:08:21 -07001029 // Expand the background drawing bounds by the padding baked into the background drawable
1030 mBackground.getPadding(mTempRect);
1031 mBackground.setBounds(
Jon Miranda28032002017-07-13 16:18:56 -07001032 left - mTempRect.left - getPaddingLeft(),
1033 top - mTempRect.top - getPaddingTop(),
1034 right + mTempRect.right + getPaddingRight(),
1035 bottom + mTempRect.bottom + getPaddingBottom());
Sunny Goyalae6e3182019-04-30 12:04:37 -07001036
Sunny Goyalc4d32012020-04-03 17:10:11 -07001037 mShortcutsAndWidgets.layout(left, top, right, bottom);
Sunny Goyal7c786f72016-06-01 14:08:21 -07001038 }
1039
Tony Wickhama501d492015-11-03 18:05:01 -08001040 /**
1041 * Returns the amount of space left over after subtracting padding and cells. This space will be
1042 * very small, a few pixels at most, and is a result of rounding down when calculating the cell
Jon Miranda228877d2021-02-09 11:05:00 -05001043 * width in {@link DeviceProfile#calculateCellWidth(int, int, int)}.
Tony Wickhama501d492015-11-03 18:05:01 -08001044 */
1045 public int getUnusedHorizontalSpace() {
Jon Miranda228877d2021-02-09 11:05:00 -05001046 return getMeasuredWidth() - getPaddingLeft() - getPaddingRight() - (mCountX * mCellWidth)
Thales Lima78d00ad2021-09-30 11:29:06 +01001047 - ((mCountX - 1) * mBorderSpace.x);
Tony Wickhama501d492015-11-03 18:05:01 -08001048 }
1049
Sunny Goyal2805e632015-05-20 15:35:32 -07001050 @Override
1051 protected boolean verifyDrawable(Drawable who) {
Sunny Goyal7ce471b2017-08-02 03:37:39 -07001052 return super.verifyDrawable(who) || (who == mBackground);
Sunny Goyal2805e632015-05-20 15:35:32 -07001053 }
1054
Michael Jurkaa52570f2012-03-20 03:18:20 -07001055 public ShortcutAndWidgetContainer getShortcutsAndWidgets() {
Sunny Goyaldcbcc862014-08-12 15:58:36 -07001056 return mShortcutsAndWidgets;
Michael Jurkaa52570f2012-03-20 03:18:20 -07001057 }
1058
Jon Miranda228877d2021-02-09 11:05:00 -05001059 public View getChildAt(int cellX, int cellY) {
1060 return mShortcutsAndWidgets.getChildAt(cellX, cellY);
Patrick Dubroy440c3602010-07-13 17:50:32 -07001061 }
1062
Adam Cohen76fc0852011-06-17 13:26:23 -07001063 public boolean animateChildToPosition(final View child, int cellX, int cellY, int duration,
Adam Cohen482ed822012-03-02 14:15:13 -08001064 int delay, boolean permanent, boolean adjustOccupied) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07001065 ShortcutAndWidgetContainer clc = getShortcutsAndWidgets();
Adam Cohen482ed822012-03-02 14:15:13 -08001066
Adam Cohen1d13c0b2020-04-21 16:29:12 -07001067 if (clc.indexOfChild(child) != -1 && (child instanceof Reorderable)) {
Sebastian Francod4682992022-10-05 13:03:09 -05001068 final CellLayoutLayoutParams lp = (CellLayoutLayoutParams) child.getLayoutParams();
Adam Cohenbfbfd262011-06-13 16:55:12 -07001069 final ItemInfo info = (ItemInfo) child.getTag();
Adam Cohen1d13c0b2020-04-21 16:29:12 -07001070 final Reorderable item = (Reorderable) child;
Adam Cohenbfbfd262011-06-13 16:55:12 -07001071
1072 // We cancel any existing animations
1073 if (mReorderAnimators.containsKey(lp)) {
1074 mReorderAnimators.get(lp).cancel();
1075 mReorderAnimators.remove(lp);
1076 }
1077
Adam Cohen1d13c0b2020-04-21 16:29:12 -07001078
Adam Cohen482ed822012-03-02 14:15:13 -08001079 if (adjustOccupied) {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001080 GridOccupancy occupied = permanent ? mOccupied : mTmpOccupied;
Sebastian Franco877088e2023-01-03 15:16:22 -07001081 occupied.markCells(lp.getCellX(), lp.getCellY(), lp.cellHSpan, lp.cellVSpan, false);
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001082 occupied.markCells(cellX, cellY, lp.cellHSpan, lp.cellVSpan, true);
Adam Cohen482ed822012-03-02 14:15:13 -08001083 }
Adam Cohen1d13c0b2020-04-21 16:29:12 -07001084
1085 // Compute the new x and y position based on the new cellX and cellY
1086 // We leverage the actual layout logic in the layout params and hence need to modify
1087 // state and revert that state.
1088 final int oldX = lp.x;
1089 final int oldY = lp.y;
Adam Cohenbfbfd262011-06-13 16:55:12 -07001090 lp.isLockedToGrid = true;
Adam Cohen482ed822012-03-02 14:15:13 -08001091 if (permanent) {
Sunny Goyal669b71f2023-01-27 14:37:07 -08001092 lp.setCellX(cellX);
1093 lp.setCellY(cellY);
Adam Cohen482ed822012-03-02 14:15:13 -08001094 } else {
Sebastian Franco877088e2023-01-03 15:16:22 -07001095 lp.setTmpCellX(cellX);
1096 lp.setTmpCellY(cellY);
Adam Cohen482ed822012-03-02 14:15:13 -08001097 }
Jon Mirandae96798e2016-12-07 12:10:44 -08001098 clc.setupLp(child);
Adam Cohen482ed822012-03-02 14:15:13 -08001099 final int newX = lp.x;
1100 final int newY = lp.y;
Adam Cohen76fc0852011-06-17 13:26:23 -07001101 lp.x = oldX;
1102 lp.y = oldY;
Adam Cohen1d13c0b2020-04-21 16:29:12 -07001103 lp.isLockedToGrid = false;
1104 // End compute new x and y
1105
Sunny Goyal82dfc152023-02-24 16:50:09 -08001106 MultiTranslateDelegate mtd = item.getTranslateDelegate();
1107 float initPreviewOffsetX = mtd.getTranslationX(INDEX_REORDER_PREVIEW_OFFSET).getValue();
1108 float initPreviewOffsetY = mtd.getTranslationY(INDEX_REORDER_PREVIEW_OFFSET).getValue();
Adam Cohen1d13c0b2020-04-21 16:29:12 -07001109 final float finalPreviewOffsetX = newX - oldX;
1110 final float finalPreviewOffsetY = newY - oldY;
1111
Adam Cohen482ed822012-03-02 14:15:13 -08001112 // Exit early if we're not actually moving the view
Adam Cohen1d13c0b2020-04-21 16:29:12 -07001113 if (finalPreviewOffsetX == 0 && finalPreviewOffsetY == 0
1114 && initPreviewOffsetX == 0 && initPreviewOffsetY == 0) {
Adam Cohen482ed822012-03-02 14:15:13 -08001115 lp.isLockedToGrid = true;
1116 return true;
1117 }
1118
Sunny Goyal849c6a22018-08-08 16:33:46 -07001119 ValueAnimator va = ValueAnimator.ofFloat(0f, 1f);
Adam Cohen482ed822012-03-02 14:15:13 -08001120 va.setDuration(duration);
1121 mReorderAnimators.put(lp, va);
1122
1123 va.addUpdateListener(new AnimatorUpdateListener() {
1124 @Override
Adam Cohenbfbfd262011-06-13 16:55:12 -07001125 public void onAnimationUpdate(ValueAnimator animation) {
Jon Mirandae96798e2016-12-07 12:10:44 -08001126 float r = (Float) animation.getAnimatedValue();
Adam Cohen1d13c0b2020-04-21 16:29:12 -07001127 float x = (1 - r) * initPreviewOffsetX + r * finalPreviewOffsetX;
1128 float y = (1 - r) * initPreviewOffsetY + r * finalPreviewOffsetY;
Sunny Goyal82dfc152023-02-24 16:50:09 -08001129 item.getTranslateDelegate().setTranslation(INDEX_REORDER_PREVIEW_OFFSET, x, y);
Adam Cohenbfbfd262011-06-13 16:55:12 -07001130 }
1131 });
Adam Cohen482ed822012-03-02 14:15:13 -08001132 va.addListener(new AnimatorListenerAdapter() {
Adam Cohenbfbfd262011-06-13 16:55:12 -07001133 boolean cancelled = false;
1134 public void onAnimationEnd(Animator animation) {
1135 // If the animation was cancelled, it means that another animation
1136 // has interrupted this one, and we don't want to lock the item into
1137 // place just yet.
1138 if (!cancelled) {
1139 lp.isLockedToGrid = true;
Sunny Goyal82dfc152023-02-24 16:50:09 -08001140 item.getTranslateDelegate()
1141 .setTranslation(INDEX_REORDER_PREVIEW_OFFSET, 0, 0);
Adam Cohen482ed822012-03-02 14:15:13 -08001142 child.requestLayout();
Adam Cohenbfbfd262011-06-13 16:55:12 -07001143 }
1144 if (mReorderAnimators.containsKey(lp)) {
1145 mReorderAnimators.remove(lp);
1146 }
1147 }
1148 public void onAnimationCancel(Animator animation) {
1149 cancelled = true;
1150 }
1151 });
Adam Cohen482ed822012-03-02 14:15:13 -08001152 va.setStartDelay(delay);
1153 va.start();
Adam Cohenbfbfd262011-06-13 16:55:12 -07001154 return true;
1155 }
1156 return false;
1157 }
1158
Adam Cohenf7ca3b42021-02-22 11:03:58 -08001159 void visualizeDropLocation(int cellX, int cellY, int spanX, int spanY,
1160 DropTarget.DragObject dragObject) {
1161 if (mDragCell[0] != cellX || mDragCell[1] != cellY || mDragCellSpan[0] != spanX
1162 || mDragCellSpan[1] != spanY) {
Juan Sebastian Martineza91fbb22024-11-21 15:49:33 -08001163 if (Flags.msdlFeedback()) {
1164 mMSDLPlayerWrapper.playToken(MSDLToken.DRAG_INDICATOR_DISCRETE);
1165 }
Adam Cohen482ed822012-03-02 14:15:13 -08001166 mDragCell[0] = cellX;
1167 mDragCell[1] = cellY;
Adam Cohenf7ca3b42021-02-22 11:03:58 -08001168 mDragCellSpan[0] = spanX;
1169 mDragCellSpan[1] = spanY;
Steven Ng30dd1d62021-03-15 21:45:49 +00001170
Joe Onorato4be866d2010-10-10 11:26:02 -07001171 final int oldIndex = mDragOutlineCurrent;
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -07001172 mDragOutlineAnims[oldIndex].animateOut();
1173 mDragOutlineCurrent = (oldIndex + 1) % mDragOutlines.length;
Sunny Goyal106bf642015-07-16 12:18:06 -07001174
Sebastian Francod4682992022-10-05 13:03:09 -05001175 CellLayoutLayoutParams cell = mDragOutlines[mDragOutlineCurrent];
Sebastian Franco877088e2023-01-03 15:16:22 -07001176 cell.setCellX(cellX);
1177 cell.setCellY(cellY);
Adam Cohenf7ca3b42021-02-22 11:03:58 -08001178 cell.cellHSpan = spanX;
1179 cell.cellVSpan = spanY;
Adam Cohen65086992020-02-19 08:40:49 -08001180
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -07001181 mDragOutlineAnims[mDragOutlineCurrent].animateIn();
Adam Cohenf7ca3b42021-02-22 11:03:58 -08001182 invalidate();
Sunny Goyale78e3d72015-09-24 11:23:31 -07001183
1184 if (dragObject.stateAnnouncer != null) {
Sunny Goyalc13403c2016-11-18 23:44:48 -08001185 dragObject.stateAnnouncer.announce(getItemMoveDescription(cellX, cellY));
Sunny Goyale78e3d72015-09-24 11:23:31 -07001186 }
Adam Cohenf7ca3b42021-02-22 11:03:58 -08001187
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001188 }
1189 }
1190
Sunny Goyal726bee72018-03-05 12:54:24 -08001191 @SuppressLint("StringFormatMatches")
Sunny Goyalc13403c2016-11-18 23:44:48 -08001192 public String getItemMoveDescription(int cellX, int cellY) {
1193 if (mContainerType == HOTSEAT) {
1194 return getContext().getString(R.string.move_to_hotseat_position,
1195 Math.max(cellX, cellY) + 1);
1196 } else {
Andras Kloczl2dacbee2022-02-21 16:53:28 +00001197 int row = cellY + 1;
Sebastian Franco2986e0b2024-01-25 10:23:39 -08001198 int col = Utilities.isRtl(getResources()) ? mCountX - cellX : cellX + 1;
1199 int panelCount = mCellLayoutContainer.getPanelCount();
1200 int pageIndex = mCellLayoutContainer.getCellLayoutIndex(this);
Andras Kloczl2dacbee2022-02-21 16:53:28 +00001201 if (panelCount > 1) {
1202 // Increment the column if the target is on the right side of a two panel home
Andras Kloczl2dacbee2022-02-21 16:53:28 +00001203 col += (pageIndex % panelCount) * mCountX;
1204 }
Sebastian Franco930531f2022-06-16 16:49:11 -07001205 return getContext().getString(R.string.move_to_empty_cell_description, row, col,
Sebastian Franco2986e0b2024-01-25 10:23:39 -08001206 mCellLayoutContainer.getPageDescription(pageIndex));
Sunny Goyalc13403c2016-11-18 23:44:48 -08001207 }
1208 }
1209
Adam Cohene0310962011-04-18 16:15:31 -07001210 public void clearDragOutlines() {
1211 final int oldIndex = mDragOutlineCurrent;
1212 mDragOutlineAnims[oldIndex].animateOut();
Adam Cohend41fbf52012-02-16 23:53:59 -08001213 mDragCell[0] = mDragCell[1] = -1;
Adam Cohene0310962011-04-18 16:15:31 -07001214 }
1215
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001216 /**
Jeff Sharkey70864282009-04-07 21:08:40 -07001217 * Find a vacant area that will fit the given bounds nearest the requested
1218 * cell location. Uses Euclidean distance to score multiple vacant areas.
Winson Chungaafa03c2010-06-11 17:34:16 -07001219 *
Romain Guy51afc022009-05-04 18:03:43 -07001220 * @param pixelX The X location at which you want to search for a vacant area.
1221 * @param pixelY The Y location at which you want to search for a vacant area.
Adam Cohend41fbf52012-02-16 23:53:59 -08001222 * @param minSpanX The minimum horizontal span required
1223 * @param minSpanY The minimum vertical span required
1224 * @param spanX Horizontal span of the object.
1225 * @param spanY Vertical span of the object.
1226 * @param result Array in which to place the result, or null (in which case a new array will
1227 * be allocated)
1228 * @return The X, Y cell of a vacant area that can contain this object,
1229 * nearest the requested location.
1230 */
Sebastian Francoe4c03452022-12-27 14:50:02 -06001231 public int[] findNearestVacantArea(int pixelX, int pixelY, int minSpanX, int minSpanY,
1232 int spanX, int spanY, int[] result, int[] resultSpan) {
Sebastian Francob57c0b22022-06-28 13:54:35 -07001233 return findNearestArea(pixelX, pixelY, minSpanX, minSpanY, spanX, spanY, false,
Adam Cohend41fbf52012-02-16 23:53:59 -08001234 result, resultSpan);
1235 }
1236
Adam Cohend41fbf52012-02-16 23:53:59 -08001237 /**
1238 * Find a vacant area that will fit the given bounds nearest the requested
1239 * cell location. Uses Euclidean distance to score multiple vacant areas.
Sebastian Francob57c0b22022-06-28 13:54:35 -07001240 * @param relativeXPos The X location relative to the Cell layout at which you want to search
1241 * for a vacant area.
1242 * @param relativeYPos The Y location relative to the Cell layout at which you want to search
1243 * for a vacant area.
Adam Cohend41fbf52012-02-16 23:53:59 -08001244 * @param minSpanX The minimum horizontal span required
1245 * @param minSpanY The minimum vertical span required
1246 * @param spanX Horizontal span of the object.
1247 * @param spanY Vertical span of the object.
1248 * @param ignoreOccupied If true, the result can be an occupied cell
1249 * @param result Array in which to place the result, or null (in which case a new array will
1250 * be allocated)
1251 * @return The X, Y cell of a vacant area that can contain this object,
1252 * nearest the requested location.
1253 */
Sebastian Franco96c46e72023-05-08 10:04:44 -06001254 protected int[] findNearestArea(int relativeXPos, int relativeYPos, int minSpanX, int minSpanY,
Sebastian Francob57c0b22022-06-28 13:54:35 -07001255 int spanX, int spanY, boolean ignoreOccupied, int[] result, int[] resultSpan) {
Sebastian Francob57c0b22022-06-28 13:54:35 -07001256 // For items with a spanX / spanY > 1, the passed in point (relativeXPos, relativeYPos)
1257 // corresponds to the center of the item, but we are searching based on the top-left cell,
1258 // so we translate the point over to correspond to the top-left.
1259 relativeXPos = (int) (relativeXPos - (mCellWidth + mBorderSpace.x) * (spanX - 1) / 2f);
1260 relativeYPos = (int) (relativeYPos - (mCellHeight + mBorderSpace.y) * (spanY - 1) / 2f);
Adam Cohene3e27a82011-04-15 12:07:39 -07001261
Jeff Sharkey70864282009-04-07 21:08:40 -07001262 // Keep track of best-scoring drop area
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001263 final int[] bestXY = result != null ? result : new int[2];
Jeff Sharkey70864282009-04-07 21:08:40 -07001264 double bestDistance = Double.MAX_VALUE;
Adam Cohend41fbf52012-02-16 23:53:59 -08001265 final Rect bestRect = new Rect(-1, -1, -1, -1);
Rajeev Kumar9962dbe2017-06-12 12:16:20 -07001266 final Stack<Rect> validRegions = new Stack<>();
Winson Chungaafa03c2010-06-11 17:34:16 -07001267
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001268 final int countX = mCountX;
1269 final int countY = mCountY;
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001270
Adam Cohend41fbf52012-02-16 23:53:59 -08001271 if (minSpanX <= 0 || minSpanY <= 0 || spanX <= 0 || spanY <= 0 ||
1272 spanX < minSpanX || spanY < minSpanY) {
1273 return bestXY;
1274 }
1275
1276 for (int y = 0; y < countY - (minSpanY - 1); y++) {
Michael Jurkac28de512010-08-13 11:27:44 -07001277 inner:
Adam Cohend41fbf52012-02-16 23:53:59 -08001278 for (int x = 0; x < countX - (minSpanX - 1); x++) {
1279 int ySize = -1;
1280 int xSize = -1;
Sebastian Francob57c0b22022-06-28 13:54:35 -07001281 if (!ignoreOccupied) {
Adam Cohend41fbf52012-02-16 23:53:59 -08001282 // First, let's see if this thing fits anywhere
1283 for (int i = 0; i < minSpanX; i++) {
1284 for (int j = 0; j < minSpanY; j++) {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001285 if (mOccupied.cells[x + i][y + j]) {
Adam Cohendf035382011-04-11 17:22:04 -07001286 continue inner;
1287 }
Michael Jurkac28de512010-08-13 11:27:44 -07001288 }
1289 }
Adam Cohend41fbf52012-02-16 23:53:59 -08001290 xSize = minSpanX;
1291 ySize = minSpanY;
1292
1293 // We know that the item will fit at _some_ acceptable size, now let's see
1294 // how big we can make it. We'll alternate between incrementing x and y spans
1295 // until we hit a limit.
1296 boolean incX = true;
1297 boolean hitMaxX = xSize >= spanX;
1298 boolean hitMaxY = ySize >= spanY;
1299 while (!(hitMaxX && hitMaxY)) {
1300 if (incX && !hitMaxX) {
1301 for (int j = 0; j < ySize; j++) {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001302 if (x + xSize > countX -1 || mOccupied.cells[x + xSize][y + j]) {
Adam Cohend41fbf52012-02-16 23:53:59 -08001303 // We can't move out horizontally
1304 hitMaxX = true;
1305 }
1306 }
1307 if (!hitMaxX) {
1308 xSize++;
1309 }
1310 } else if (!hitMaxY) {
1311 for (int i = 0; i < xSize; i++) {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001312 if (y + ySize > countY - 1 || mOccupied.cells[x + i][y + ySize]) {
Adam Cohend41fbf52012-02-16 23:53:59 -08001313 // We can't move out vertically
1314 hitMaxY = true;
1315 }
1316 }
1317 if (!hitMaxY) {
1318 ySize++;
1319 }
1320 }
1321 hitMaxX |= xSize >= spanX;
1322 hitMaxY |= ySize >= spanY;
1323 incX = !incX;
1324 }
Michael Jurkac28de512010-08-13 11:27:44 -07001325 }
Sunny Goyal2805e632015-05-20 15:35:32 -07001326 final int[] cellXY = mTmpPoint;
Adam Cohene3e27a82011-04-15 12:07:39 -07001327 cellToCenterPoint(x, y, cellXY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001328
Adam Cohend41fbf52012-02-16 23:53:59 -08001329 // We verify that the current rect is not a sub-rect of any of our previous
1330 // candidates. In this case, the current rect is disqualified in favour of the
1331 // containing rect.
Sebastian Franco4a922672022-10-27 16:42:24 -07001332 Rect currentRect = new Rect(x, y, x + xSize, y + ySize);
Adam Cohend41fbf52012-02-16 23:53:59 -08001333 boolean contained = false;
1334 for (Rect r : validRegions) {
1335 if (r.contains(currentRect)) {
1336 contained = true;
1337 break;
1338 }
1339 }
1340 validRegions.push(currentRect);
Sebastian Francob57c0b22022-06-28 13:54:35 -07001341 double distance = Math.hypot(cellXY[0] - relativeXPos, cellXY[1] - relativeYPos);
Adam Cohen482ed822012-03-02 14:15:13 -08001342
Adam Cohend41fbf52012-02-16 23:53:59 -08001343 if ((distance <= bestDistance && !contained) ||
1344 currentRect.contains(bestRect)) {
Michael Jurkac28de512010-08-13 11:27:44 -07001345 bestDistance = distance;
1346 bestXY[0] = x;
1347 bestXY[1] = y;
Adam Cohend41fbf52012-02-16 23:53:59 -08001348 if (resultSpan != null) {
1349 resultSpan[0] = xSize;
1350 resultSpan[1] = ySize;
1351 }
1352 bestRect.set(currentRect);
Michael Jurkac28de512010-08-13 11:27:44 -07001353 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001354 }
1355 }
1356
Adam Cohenc0dcf592011-06-01 15:30:43 -07001357 // Return -1, -1 if no suitable location found
1358 if (bestDistance == Double.MAX_VALUE) {
1359 bestXY[0] = -1;
1360 bestXY[1] = -1;
Jeff Sharkey70864282009-04-07 21:08:40 -07001361 }
Adam Cohenc0dcf592011-06-01 15:30:43 -07001362 return bestXY;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001363 }
Winson Chungaafa03c2010-06-11 17:34:16 -07001364
Sebastian Francoe4c03452022-12-27 14:50:02 -06001365 public GridOccupancy getOccupied() {
1366 return mOccupied;
1367 }
1368
Adam Cohen482ed822012-03-02 14:15:13 -08001369 private void copySolutionToTempState(ItemConfiguration solution, View dragView) {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001370 mTmpOccupied.clear();
Adam Cohen482ed822012-03-02 14:15:13 -08001371
Michael Jurkaa52570f2012-03-20 03:18:20 -07001372 int childCount = mShortcutsAndWidgets.getChildCount();
Adam Cohen482ed822012-03-02 14:15:13 -08001373 for (int i = 0; i < childCount; i++) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07001374 View child = mShortcutsAndWidgets.getChildAt(i);
Adam Cohen482ed822012-03-02 14:15:13 -08001375 if (child == dragView) continue;
Sebastian Francod4682992022-10-05 13:03:09 -05001376 CellLayoutLayoutParams lp = (CellLayoutLayoutParams) child.getLayoutParams();
Adam Cohen8baab352012-03-20 17:39:21 -07001377 CellAndSpan c = solution.map.get(child);
1378 if (c != null) {
Sebastian Franco877088e2023-01-03 15:16:22 -07001379 lp.setTmpCellX(c.cellX);
1380 lp.setTmpCellY(c.cellY);
Adam Cohen8baab352012-03-20 17:39:21 -07001381 lp.cellHSpan = c.spanX;
1382 lp.cellVSpan = c.spanY;
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001383 mTmpOccupied.markCells(c, true);
Adam Cohen482ed822012-03-02 14:15:13 -08001384 }
1385 }
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001386 mTmpOccupied.markCells(solution, true);
Adam Cohen482ed822012-03-02 14:15:13 -08001387 }
1388
1389 private void animateItemsToSolution(ItemConfiguration solution, View dragView, boolean
1390 commitDragView) {
1391
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001392 GridOccupancy occupied = DESTRUCTIVE_REORDER ? mOccupied : mTmpOccupied;
1393 occupied.clear();
Adam Cohen482ed822012-03-02 14:15:13 -08001394
Michael Jurkaa52570f2012-03-20 03:18:20 -07001395 int childCount = mShortcutsAndWidgets.getChildCount();
Adam Cohen482ed822012-03-02 14:15:13 -08001396 for (int i = 0; i < childCount; i++) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07001397 View child = mShortcutsAndWidgets.getChildAt(i);
Adam Cohen482ed822012-03-02 14:15:13 -08001398 if (child == dragView) continue;
Adam Cohen8baab352012-03-20 17:39:21 -07001399 CellAndSpan c = solution.map.get(child);
1400 if (c != null) {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001401 animateChildToPosition(child, c.cellX, c.cellY, REORDER_ANIMATION_DURATION, 0,
Adam Cohen19f37922012-03-21 11:59:11 -07001402 DESTRUCTIVE_REORDER, false);
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001403 occupied.markCells(c, true);
Adam Cohen482ed822012-03-02 14:15:13 -08001404 }
1405 }
1406 if (commitDragView) {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001407 occupied.markCells(solution, true);
Adam Cohen482ed822012-03-02 14:15:13 -08001408 }
1409 }
1410
Adam Cohenfa3c58f2013-12-06 16:10:55 -08001411
1412 // This method starts or changes the reorder preview animations
1413 private void beginOrAdjustReorderPreviewAnimations(ItemConfiguration solution,
Adam Cohen65086992020-02-19 08:40:49 -08001414 View dragView, int mode) {
Adam Cohen19f37922012-03-21 11:59:11 -07001415 int childCount = mShortcutsAndWidgets.getChildCount();
Adam Cohen19f37922012-03-21 11:59:11 -07001416 for (int i = 0; i < childCount; i++) {
1417 View child = mShortcutsAndWidgets.getChildAt(i);
1418 if (child == dragView) continue;
1419 CellAndSpan c = solution.map.get(child);
Sebastian Franco5f0af4f2023-11-21 10:45:45 -06001420 boolean skip = mode == ReorderPreviewAnimation.MODE_HINT
1421 && !solution.intersectingViews.contains(child);
Adam Cohend9162062020-03-24 16:35:35 -07001422
Sebastian Francod4682992022-10-05 13:03:09 -05001423 CellLayoutLayoutParams lp = (CellLayoutLayoutParams) child.getLayoutParams();
Adam Cohend9162062020-03-24 16:35:35 -07001424 if (c != null && !skip && (child instanceof Reorderable)) {
Sebastian Francof7654252024-01-18 11:50:50 -08001425 ReorderPreviewAnimation rha = new ReorderPreviewAnimation(child, mode,
1426 lp.getCellX(), lp.getCellY(), c.cellX, c.cellY, c.spanX, c.spanY,
1427 mReorderPreviewAnimationMagnitude, this, mShakeAnimators);
Adam Cohend024f982012-05-23 18:26:45 -07001428 rha.animate();
Adam Cohen19f37922012-03-21 11:59:11 -07001429 }
1430 }
1431 }
1432
Adam Cohenfa3c58f2013-12-06 16:10:55 -08001433 private void completeAndClearReorderPreviewAnimations() {
1434 for (ReorderPreviewAnimation a: mShakeAnimators.values()) {
Adam Cohend9162062020-03-24 16:35:35 -07001435 a.finishAnimation();
Adam Cohen19f37922012-03-21 11:59:11 -07001436 }
1437 mShakeAnimators.clear();
1438 }
1439
Sunny Goyal711c5962021-06-23 12:36:18 -07001440 private void commitTempPlacement(View dragView) {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001441 mTmpOccupied.copyTo(mOccupied);
Sunny Goyalaa8ef112015-06-12 20:04:41 -07001442
Sebastian Franco2986e0b2024-01-25 10:23:39 -08001443 int screenId = mCellLayoutContainer.getCellLayoutId(this);
Sunny Goyalaa8ef112015-06-12 20:04:41 -07001444 int container = Favorites.CONTAINER_DESKTOP;
1445
Sunny Goyalc13403c2016-11-18 23:44:48 -08001446 if (mContainerType == HOTSEAT) {
Sunny Goyalaa8ef112015-06-12 20:04:41 -07001447 screenId = -1;
1448 container = Favorites.CONTAINER_HOTSEAT;
1449 }
1450
Michael Jurkaa52570f2012-03-20 03:18:20 -07001451 int childCount = mShortcutsAndWidgets.getChildCount();
Adam Cohen482ed822012-03-02 14:15:13 -08001452 for (int i = 0; i < childCount; i++) {
Adam Cohenea889a22012-03-27 16:45:39 -07001453 View child = mShortcutsAndWidgets.getChildAt(i);
Sebastian Francod4682992022-10-05 13:03:09 -05001454 CellLayoutLayoutParams lp = (CellLayoutLayoutParams) child.getLayoutParams();
Adam Cohenea889a22012-03-27 16:45:39 -07001455 ItemInfo info = (ItemInfo) child.getTag();
Adam Cohen2acce882012-03-28 19:03:19 -07001456 // We do a null check here because the item info can be null in the case of the
1457 // AllApps button in the hotseat.
Sunny Goyal711c5962021-06-23 12:36:18 -07001458 if (info != null && child != dragView) {
Sunny Goyal669b71f2023-01-27 14:37:07 -08001459 CellPos presenterPos = mActivity.getCellPosMapper().mapModelToPresenter(info);
1460 final boolean requiresDbUpdate = (presenterPos.cellX != lp.getTmpCellX()
1461 || presenterPos.cellY != lp.getTmpCellY() || info.spanX != lp.cellHSpan
1462 || info.spanY != lp.cellVSpan || presenterPos.screenId != screenId);
Sunny Goyalaa8ef112015-06-12 20:04:41 -07001463
Sebastian Franco877088e2023-01-03 15:16:22 -07001464 lp.setCellX(lp.getTmpCellX());
Sebastian Franco877088e2023-01-03 15:16:22 -07001465 lp.setCellY(lp.getTmpCellY());
Sunny Goyalaa8ef112015-06-12 20:04:41 -07001466 if (requiresDbUpdate) {
Sunny Goyalab770a12018-11-14 15:17:26 -08001467 Launcher.cast(mActivity).getModelWriter().modifyItemInDatabase(info, container,
Sunny Goyal669b71f2023-01-27 14:37:07 -08001468 screenId, lp.getCellX(), lp.getCellY(), lp.cellHSpan, lp.cellVSpan);
Sunny Goyalaa8ef112015-06-12 20:04:41 -07001469 }
Adam Cohen2acce882012-03-28 19:03:19 -07001470 }
Adam Cohen482ed822012-03-02 14:15:13 -08001471 }
1472 }
1473
Sunny Goyalf7a29e82015-04-24 15:20:43 -07001474 private void setUseTempCoords(boolean useTempCoords) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07001475 int childCount = mShortcutsAndWidgets.getChildCount();
Adam Cohen482ed822012-03-02 14:15:13 -08001476 for (int i = 0; i < childCount; i++) {
Sebastian Francod4682992022-10-05 13:03:09 -05001477 CellLayoutLayoutParams lp = (CellLayoutLayoutParams) mShortcutsAndWidgets.getChildAt(
1478 i).getLayoutParams();
Adam Cohen482ed822012-03-02 14:15:13 -08001479 lp.useTmpCoords = useTempCoords;
1480 }
1481 }
1482
Sebastian Franco5c8f8682023-11-14 09:52:41 -06001483 /**
1484 * For a given region, return the rectangle of the overlapping cell and span with the given
1485 * region including the region itself. If there is no overlap the rectangle will be
1486 * invalid i.e. -1, 0, -1, 0.
1487 */
1488 @Nullable
1489 public Rect getIntersectingRectanglesInRegion(final Rect region, final View dragView) {
1490 Rect boundingRect = new Rect(region);
Adam Cohen19f37922012-03-21 11:59:11 -07001491 Rect r1 = new Rect();
Sebastian Franco5c8f8682023-11-14 09:52:41 -06001492 boolean isOverlapping = false;
Adam Cohen19f37922012-03-21 11:59:11 -07001493 final int count = mShortcutsAndWidgets.getChildCount();
1494 for (int i = 0; i < count; i++) {
1495 View child = mShortcutsAndWidgets.getChildAt(i);
1496 if (child == dragView) continue;
Sebastian Francod4682992022-10-05 13:03:09 -05001497 CellLayoutLayoutParams
1498 lp = (CellLayoutLayoutParams) child.getLayoutParams();
Sebastian Franco877088e2023-01-03 15:16:22 -07001499 r1.set(lp.getCellX(), lp.getCellY(), lp.getCellX() + lp.cellHSpan,
1500 lp.getCellY() + lp.cellVSpan);
Sebastian Franco5c8f8682023-11-14 09:52:41 -06001501 if (Rect.intersects(region, r1)) {
1502 isOverlapping = true;
1503 boundingRect.union(r1);
Adam Cohen19f37922012-03-21 11:59:11 -07001504 }
1505 }
Sebastian Franco5c8f8682023-11-14 09:52:41 -06001506 return isOverlapping ? boundingRect : null;
Adam Cohen19f37922012-03-21 11:59:11 -07001507 }
1508
Sebastian Francoe4c03452022-12-27 14:50:02 -06001509 public boolean isNearestDropLocationOccupied(int pixelX, int pixelY, int spanX, int spanY,
Adam Cohen19f37922012-03-21 11:59:11 -07001510 View dragView, int[] result) {
Sebastián Francof9a6ac22022-11-15 22:56:37 +00001511 result = findNearestAreaIgnoreOccupied(pixelX, pixelY, spanX, spanY, result);
Sebastian Franco5c8f8682023-11-14 09:52:41 -06001512 return getIntersectingRectanglesInRegion(
1513 new Rect(result[0], result[1], result[0] + spanX, result[1] + spanY),
1514 dragView
1515 ) != null;
Adam Cohen19f37922012-03-21 11:59:11 -07001516 }
1517
1518 void revertTempState() {
Adam Cohenfa3c58f2013-12-06 16:10:55 -08001519 completeAndClearReorderPreviewAnimations();
1520 if (isItemPlacementDirty() && !DESTRUCTIVE_REORDER) {
1521 final int count = mShortcutsAndWidgets.getChildCount();
1522 for (int i = 0; i < count; i++) {
1523 View child = mShortcutsAndWidgets.getChildAt(i);
Sebastian Francod4682992022-10-05 13:03:09 -05001524 CellLayoutLayoutParams
1525 lp = (CellLayoutLayoutParams) child.getLayoutParams();
Sebastian Franco877088e2023-01-03 15:16:22 -07001526 if (lp.getTmpCellX() != lp.getCellX() || lp.getTmpCellY() != lp.getCellY()) {
1527 lp.setTmpCellX(lp.getCellX());
1528 lp.setTmpCellY(lp.getCellY());
1529 animateChildToPosition(child, lp.getCellX(), lp.getCellY(),
1530 REORDER_ANIMATION_DURATION, 0, false, false);
Adam Cohenfa3c58f2013-12-06 16:10:55 -08001531 }
Adam Cohen19f37922012-03-21 11:59:11 -07001532 }
Adam Cohenfa3c58f2013-12-06 16:10:55 -08001533 setItemPlacementDirty(false);
Adam Cohen19f37922012-03-21 11:59:11 -07001534 }
Adam Cohen19f37922012-03-21 11:59:11 -07001535 }
1536
Adam Cohenbebf0422012-04-11 18:06:28 -07001537 boolean createAreaForResize(int cellX, int cellY, int spanX, int spanY,
1538 View dragView, int[] direction, boolean commit) {
1539 int[] pixelXY = new int[2];
1540 regionToCenterPoint(cellX, cellY, spanX, spanY, pixelXY);
1541
1542 // First we determine if things have moved enough to cause a different layout
Adam Cohenfa3c58f2013-12-06 16:10:55 -08001543 ItemConfiguration swapSolution = findReorderSolution(pixelXY[0], pixelXY[1], spanX, spanY,
Charlie Anderson46122392024-01-18 17:23:46 -05001544 spanX, spanY, direction, dragView, true);
Adam Cohenbebf0422012-04-11 18:06:28 -07001545
1546 setUseTempCoords(true);
1547 if (swapSolution != null && swapSolution.isSolution) {
1548 // If we're just testing for a possible location (MODE_ACCEPT_DROP), we don't bother
1549 // committing anything or animating anything as we just want to determine if a solution
1550 // exists
1551 copySolutionToTempState(swapSolution, dragView);
1552 setItemPlacementDirty(true);
1553 animateItemsToSolution(swapSolution, dragView, commit);
1554
1555 if (commit) {
Sunny Goyal711c5962021-06-23 12:36:18 -07001556 commitTempPlacement(null);
Adam Cohenfa3c58f2013-12-06 16:10:55 -08001557 completeAndClearReorderPreviewAnimations();
Adam Cohenbebf0422012-04-11 18:06:28 -07001558 setItemPlacementDirty(false);
1559 } else {
Adam Cohenfa3c58f2013-12-06 16:10:55 -08001560 beginOrAdjustReorderPreviewAnimations(swapSolution, dragView,
Adam Cohen65086992020-02-19 08:40:49 -08001561 ReorderPreviewAnimation.MODE_PREVIEW);
Adam Cohenbebf0422012-04-11 18:06:28 -07001562 }
1563 mShortcutsAndWidgets.requestLayout();
1564 }
1565 return swapSolution.isSolution;
1566 }
1567
Sebastian Francoe4c03452022-12-27 14:50:02 -06001568 public ReorderAlgorithm createReorderAlgorithm() {
1569 return new ReorderAlgorithm(this);
1570 }
1571
Sebastian Franco09589322022-11-02 15:25:58 -07001572 protected ItemConfiguration findReorderSolution(int pixelX, int pixelY, int minSpanX,
Charlie Anderson46122392024-01-18 17:23:46 -05001573 int minSpanY, int spanX, int spanY, int[] direction, View dragView, boolean decX) {
Sebastian Franco25f8e402023-12-02 13:54:05 -06001574 ItemConfiguration configuration = new ItemConfiguration();
1575 copyCurrentStateToSolution(configuration);
1576 ReorderParameters parameters = new ReorderParameters(pixelX, pixelY, spanX, spanY, minSpanX,
1577 minSpanY, dragView, configuration);
Charlie Anderson46122392024-01-18 17:23:46 -05001578 int[] directionVector = direction != null ? direction : mDirectionVector;
1579 return createReorderAlgorithm().findReorderSolution(parameters, directionVector, decX);
Sebastian Franco09589322022-11-02 15:25:58 -07001580 }
1581
Sebastián Franco61fbd182023-11-29 21:30:43 +00001582 public void copyCurrentStateToSolution(ItemConfiguration solution) {
Sebastian Franco9cab1c32022-10-25 17:28:54 -07001583 int childCount = mShortcutsAndWidgets.getChildCount();
1584 for (int i = 0; i < childCount; i++) {
1585 View child = mShortcutsAndWidgets.getChildAt(i);
1586 CellLayoutLayoutParams lp = (CellLayoutLayoutParams) child.getLayoutParams();
Sebastián Franco61fbd182023-11-29 21:30:43 +00001587 solution.add(child,
1588 new CellAndSpan(lp.getCellX(), lp.getCellY(), lp.cellHSpan, lp.cellVSpan));
Sebastian Franco9cab1c32022-10-25 17:28:54 -07001589 }
Sebastian Franco53a15a42022-10-25 17:28:54 -07001590 }
1591
1592 /**
Sebastian Franco53a15a42022-10-25 17:28:54 -07001593 * When the user drags an Item in the workspace sometimes we need to move the items already in
1594 * the workspace to make space for the new item, this function return a solution for that
1595 * reorder.
1596 *
1597 * @param pixelX X coordinate in the screen of the dragView in pixels
1598 * @param pixelY Y coordinate in the screen of the dragView in pixels
1599 * @param minSpanX minimum horizontal span the item can be shrunk to
1600 * @param minSpanY minimum vertical span the item can be shrunk to
1601 * @param spanX occupied horizontal span
1602 * @param spanY occupied vertical span
1603 * @param dragView the view of the item being draged
1604 * @return returns a solution for the given parameters, the solution contains all the icons and
1605 * the locations they should be in the given solution.
1606 */
1607 public ItemConfiguration calculateReorder(int pixelX, int pixelY, int minSpanX, int minSpanY,
1608 int spanX, int spanY, View dragView) {
Sebastian Franco25f8e402023-12-02 13:54:05 -06001609 ItemConfiguration configuration = new ItemConfiguration();
1610 copyCurrentStateToSolution(configuration);
1611 return createReorderAlgorithm().calculateReorder(
1612 new ReorderParameters(pixelX, pixelY, spanX, spanY, minSpanX, minSpanY, dragView,
1613 configuration)
1614 );
Sebastian Franco53a15a42022-10-25 17:28:54 -07001615 }
Adam Cohen482ed822012-03-02 14:15:13 -08001616
Sebastian Franco9cab1c32022-10-25 17:28:54 -07001617 int[] performReorder(int pixelX, int pixelY, int minSpanX, int minSpanY, int spanX, int spanY,
1618 View dragView, int[] result, int[] resultSpan, int mode) {
1619 if (resultSpan == null) {
1620 resultSpan = new int[]{-1, -1};
1621 }
1622 if (result == null) {
1623 result = new int[]{-1, -1};
1624 }
Sebastian Franco5d990ee2022-11-01 16:08:24 -07001625
1626 ItemConfiguration finalSolution = null;
1627 // We want the solution to match the animation of the preview and to match the drop so we
1628 // only recalculate in mode MODE_SHOW_REORDER_HINT because that the first one to run in the
1629 // reorder cycle.
1630 if (mode == MODE_SHOW_REORDER_HINT || mPreviousSolution == null) {
1631 finalSolution = calculateReorder(pixelX, pixelY, minSpanX, minSpanY, spanX, spanY,
1632 dragView);
1633 mPreviousSolution = finalSolution;
1634 } else {
Sebastian Franco9cab1c32022-10-25 17:28:54 -07001635 finalSolution = mPreviousSolution;
1636 // We reset this vector after drop
1637 if (mode == MODE_ON_DROP || mode == MODE_ON_DROP_EXTERNAL) {
1638 mPreviousSolution = null;
1639 }
Sebastian Franco9cab1c32022-10-25 17:28:54 -07001640 }
1641
1642 if (finalSolution == null || !finalSolution.isSolution) {
1643 result[0] = result[1] = resultSpan[0] = resultSpan[1] = -1;
1644 } else {
1645 result[0] = finalSolution.cellX;
1646 result[1] = finalSolution.cellY;
1647 resultSpan[0] = finalSolution.spanX;
1648 resultSpan[1] = finalSolution.spanY;
Sebastian Franco9c743272022-11-15 15:03:25 -08001649 performReorder(finalSolution, dragView, mode);
Sebastian Franco9cab1c32022-10-25 17:28:54 -07001650 }
Sebastian Franco9cab1c32022-10-25 17:28:54 -07001651 return result;
1652 }
1653
Sebastian Franco53a15a42022-10-25 17:28:54 -07001654 /**
1655 * Animates and submits in the DB the given ItemConfiguration depending of the mode.
1656 *
1657 * @param solution represents widgets on the screen which the Workspace will animate to and
1658 * would be submitted to the database.
1659 * @param dragView view which is being dragged over the workspace that trigger the reorder
1660 * @param mode depending on the mode different animations would be played and depending on the
1661 * mode the solution would be submitted or not the database.
1662 * The possible modes are {@link MODE_SHOW_REORDER_HINT}, {@link MODE_DRAG_OVER},
1663 * {@link MODE_ON_DROP}, {@link MODE_ON_DROP_EXTERNAL}, {@link MODE_ACCEPT_DROP}
1664 * defined in {@link CellLayout}.
1665 */
Sebastian Francoe4c03452022-12-27 14:50:02 -06001666 public void performReorder(ItemConfiguration solution, View dragView, int mode) {
Adam Cohenfa3c58f2013-12-06 16:10:55 -08001667 if (mode == MODE_SHOW_REORDER_HINT) {
Sebastian Franco53a15a42022-10-25 17:28:54 -07001668 beginOrAdjustReorderPreviewAnimations(solution, dragView,
1669 ReorderPreviewAnimation.MODE_HINT);
1670 return;
1671 }
1672 // If we're just testing for a possible location (MODE_ACCEPT_DROP), we don't bother
1673 // committing anything or animating anything as we just want to determine if a solution
1674 // exists
1675 if (mode == MODE_DRAG_OVER || mode == MODE_ON_DROP || mode == MODE_ON_DROP_EXTERNAL) {
1676 if (!DESTRUCTIVE_REORDER) {
1677 setUseTempCoords(true);
1678 }
1679
1680 if (!DESTRUCTIVE_REORDER) {
1681 copySolutionToTempState(solution, dragView);
1682 }
1683 setItemPlacementDirty(true);
1684 animateItemsToSolution(solution, dragView, mode == MODE_ON_DROP);
1685
1686 if (!DESTRUCTIVE_REORDER
1687 && (mode == MODE_ON_DROP || mode == MODE_ON_DROP_EXTERNAL)) {
1688 // Since the temp solution didn't update dragView, don't commit it either
1689 commitTempPlacement(dragView);
1690 completeAndClearReorderPreviewAnimations();
1691 setItemPlacementDirty(false);
Adam Cohenfa3c58f2013-12-06 16:10:55 -08001692 } else {
Sebastian Franco53a15a42022-10-25 17:28:54 -07001693 beginOrAdjustReorderPreviewAnimations(solution, dragView,
1694 ReorderPreviewAnimation.MODE_PREVIEW);
Adam Cohenfa3c58f2013-12-06 16:10:55 -08001695 }
Adam Cohenfa3c58f2013-12-06 16:10:55 -08001696 }
1697
Sebastian Franco53a15a42022-10-25 17:28:54 -07001698 if (mode == MODE_ON_DROP && !DESTRUCTIVE_REORDER) {
Adam Cohen482ed822012-03-02 14:15:13 -08001699 setUseTempCoords(false);
1700 }
Adam Cohen482ed822012-03-02 14:15:13 -08001701
Michael Jurkaa52570f2012-03-20 03:18:20 -07001702 mShortcutsAndWidgets.requestLayout();
Adam Cohen482ed822012-03-02 14:15:13 -08001703 }
1704
Adam Cohen19f37922012-03-21 11:59:11 -07001705 void setItemPlacementDirty(boolean dirty) {
1706 mItemPlacementDirty = dirty;
Adam Cohen482ed822012-03-02 14:15:13 -08001707 }
Adam Cohen19f37922012-03-21 11:59:11 -07001708 boolean isItemPlacementDirty() {
1709 return mItemPlacementDirty;
Adam Cohen482ed822012-03-02 14:15:13 -08001710 }
1711
Sebastian Francoe4c03452022-12-27 14:50:02 -06001712 /**
Adam Cohendf035382011-04-11 17:22:04 -07001713 * Find a starting cell position that will fit the given bounds nearest the requested
1714 * cell location. Uses Euclidean distance to score multiple vacant areas.
1715 *
1716 * @param pixelX The X location at which you want to search for a vacant area.
1717 * @param pixelY The Y location at which you want to search for a vacant area.
1718 * @param spanX Horizontal span of the object.
1719 * @param spanY Vertical span of the object.
Adam Cohendf035382011-04-11 17:22:04 -07001720 * @param result Previously returned value to possibly recycle.
1721 * @return The X, Y cell of a vacant area that can contain this object,
1722 * nearest the requested location.
1723 */
Sebastián Francof9a6ac22022-11-15 22:56:37 +00001724 public int[] findNearestAreaIgnoreOccupied(int pixelX, int pixelY, int spanX, int spanY,
1725 int[] result) {
Sebastian Francob57c0b22022-06-28 13:54:35 -07001726 return findNearestArea(pixelX, pixelY, spanX, spanY, spanX, spanY, true, result, null);
Adam Cohendf035382011-04-11 17:22:04 -07001727 }
1728
Michael Jurka0280c3b2010-09-17 15:00:07 -07001729 boolean existsEmptyCell() {
1730 return findCellForSpan(null, 1, 1);
1731 }
1732
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001733 /**
Michael Jurka0280c3b2010-09-17 15:00:07 -07001734 * Finds the upper-left coordinate of the first rectangle in the grid that can
1735 * hold a cell of the specified dimensions. If intersectX and intersectY are not -1,
1736 * then this method will only return coordinates for rectangles that contain the cell
1737 * (intersectX, intersectY)
1738 *
1739 * @param cellXY The array that will contain the position of a vacant cell if such a cell
1740 * can be found.
1741 * @param spanX The horizontal span of the cell we want to find.
1742 * @param spanY The vertical span of the cell we want to find.
1743 *
1744 * @return True if a vacant cell of the specified dimension was found, false otherwise.
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001745 */
Hyunyoung Song3f471442015-04-08 19:01:34 -07001746 public boolean findCellForSpan(int[] cellXY, int spanX, int spanY) {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001747 if (cellXY == null) {
1748 cellXY = new int[2];
Michael Jurka0280c3b2010-09-17 15:00:07 -07001749 }
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001750 return mOccupied.findVacantCell(cellXY, spanX, spanY);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001751 }
1752
1753 /**
Winson Chungc07918d2011-07-01 15:35:26 -07001754 * A drag event has begun over this layout.
1755 * It may have begun over this layout (in which case onDragChild is called first),
1756 * or it may have begun on another layout.
1757 */
1758 void onDragEnter() {
Winson Chungc07918d2011-07-01 15:35:26 -07001759 mDragging = true;
Sebastian Franco5aa71ce2022-12-14 12:13:19 -06001760 mPreviousSolution = null;
Winson Chungc07918d2011-07-01 15:35:26 -07001761 }
1762
1763 /**
Michael Jurka0280c3b2010-09-17 15:00:07 -07001764 * Called when drag has left this CellLayout or has been completed (successfully or not)
1765 */
1766 void onDragExit() {
Joe Onorato4be866d2010-10-10 11:26:02 -07001767 // This can actually be called when we aren't in a drag, e.g. when adding a new
1768 // item to this layout via the customize drawer.
1769 // Guard against that case.
1770 if (mDragging) {
1771 mDragging = false;
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001772 }
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -07001773
1774 // Invalidate the drag data
Sebastian Franco5aa71ce2022-12-14 12:13:19 -06001775 mPreviousSolution = null;
Adam Cohend41fbf52012-02-16 23:53:59 -08001776 mDragCell[0] = mDragCell[1] = -1;
Adam Cohenf7ca3b42021-02-22 11:03:58 -08001777 mDragCellSpan[0] = mDragCellSpan[1] = -1;
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -07001778 mDragOutlineAnims[mDragOutlineCurrent].animateOut();
1779 mDragOutlineCurrent = (mDragOutlineCurrent + 1) % mDragOutlineAnims.length;
Adam Cohen19f37922012-03-21 11:59:11 -07001780 revertTempState();
Michael Jurka33945b22010-12-21 18:19:38 -08001781 setIsDragOverlapping(false);
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001782 }
1783
1784 /**
Winson Chungaafa03c2010-06-11 17:34:16 -07001785 * Mark a child as having been dropped.
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001786 * At the beginning of the drag operation, the child may have been on another
Patrick Dubroyce34a972010-10-19 10:34:32 -07001787 * screen, but it is re-parented before this method is called.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001788 *
1789 * @param child The child that is being dropped
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001790 */
Adam Cohen716b51e2011-06-30 12:09:54 -07001791 void onDropChild(View child) {
Romain Guyd94533d2009-08-17 10:01:15 -07001792 if (child != null) {
Sebastian Francod4682992022-10-05 13:03:09 -05001793 CellLayoutLayoutParams
1794 lp = (CellLayoutLayoutParams) child.getLayoutParams();
Romain Guy84f296c2009-11-04 15:00:44 -08001795 lp.dropped = true;
Romain Guyd94533d2009-08-17 10:01:15 -07001796 child.requestLayout();
Tony Wickham1cdb6d02015-09-17 11:08:27 -07001797 markCellsAsOccupiedForView(child);
Romain Guyd94533d2009-08-17 10:01:15 -07001798 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001799 }
1800
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001801 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001802 * Computes a bounding rectangle for a range of cells
Winson Chungaafa03c2010-06-11 17:34:16 -07001803 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001804 * @param cellX X coordinate of upper left corner expressed as a cell position
1805 * @param cellY Y coordinate of upper left corner expressed as a cell position
Winson Chungaafa03c2010-06-11 17:34:16 -07001806 * @param cellHSpan Width in cells
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001807 * @param cellVSpan Height in cells
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001808 * @param resultRect Rect into which to put the results
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001809 */
Adam Cohend41fbf52012-02-16 23:53:59 -08001810 public void cellToRect(int cellX, int cellY, int cellHSpan, int cellVSpan, Rect resultRect) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001811 final int cellWidth = mCellWidth;
1812 final int cellHeight = mCellHeight;
Winson Chungaafa03c2010-06-11 17:34:16 -07001813
Pierre Barbier de Reuille1b8bbb62021-05-19 22:45:16 +01001814 // We observe a shift of 1 pixel on the x coordinate compared to the actual cell coordinates
1815 final int hStartPadding = getPaddingLeft()
1816 + (int) Math.ceil(getUnusedHorizontalSpace() / 2f);
Winson Chung4b825dcd2011-06-19 12:41:22 -07001817 final int vStartPadding = getPaddingTop();
Winson Chungaafa03c2010-06-11 17:34:16 -07001818
Thales Lima78d00ad2021-09-30 11:29:06 +01001819 int x = hStartPadding + (cellX * mBorderSpace.x) + (cellX * cellWidth);
1820 int y = vStartPadding + (cellY * mBorderSpace.y) + (cellY * cellHeight);
Jon Miranda228877d2021-02-09 11:05:00 -05001821
Thales Lima78d00ad2021-09-30 11:29:06 +01001822 int width = cellHSpan * cellWidth + ((cellHSpan - 1) * mBorderSpace.x);
1823 int height = cellVSpan * cellHeight + ((cellVSpan - 1) * mBorderSpace.y);
Winson Chungaafa03c2010-06-11 17:34:16 -07001824
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001825 resultRect.set(x, y, x + width, y + height);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001826 }
Winson Chungaafa03c2010-06-11 17:34:16 -07001827
Adam Cohend4844c32011-02-18 19:25:06 -08001828 public void markCellsAsOccupiedForView(View view) {
Sebastian Francof153d912022-04-22 16:15:27 -05001829 if (view instanceof LauncherAppWidgetHostView
1830 && view.getTag() instanceof LauncherAppWidgetInfo) {
1831 LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) view.getTag();
Sunny Goyal669b71f2023-01-27 14:37:07 -08001832 CellPos pos = mActivity.getCellPosMapper().mapModelToPresenter(info);
1833 mOccupied.markCells(pos.cellX, pos.cellY, info.spanX, info.spanY, true);
Sebastian Francof153d912022-04-22 16:15:27 -05001834 return;
1835 }
Michael Jurkaa52570f2012-03-20 03:18:20 -07001836 if (view == null || view.getParent() != mShortcutsAndWidgets) return;
Sebastian Francod4682992022-10-05 13:03:09 -05001837 CellLayoutLayoutParams
1838 lp = (CellLayoutLayoutParams) view.getLayoutParams();
Sebastian Franco877088e2023-01-03 15:16:22 -07001839 mOccupied.markCells(lp.getCellX(), lp.getCellY(), lp.cellHSpan, lp.cellVSpan, true);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001840 }
1841
Adam Cohend4844c32011-02-18 19:25:06 -08001842 public void markCellsAsUnoccupiedForView(View view) {
Sebastian Francof153d912022-04-22 16:15:27 -05001843 if (view instanceof LauncherAppWidgetHostView
1844 && view.getTag() instanceof LauncherAppWidgetInfo) {
1845 LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) view.getTag();
Sunny Goyal669b71f2023-01-27 14:37:07 -08001846 CellPos pos = mActivity.getCellPosMapper().mapModelToPresenter(info);
1847 mOccupied.markCells(pos.cellX, pos.cellY, info.spanX, info.spanY, false);
Sebastian Francof153d912022-04-22 16:15:27 -05001848 return;
1849 }
Michael Jurkaa52570f2012-03-20 03:18:20 -07001850 if (view == null || view.getParent() != mShortcutsAndWidgets) return;
Sebastian Francod4682992022-10-05 13:03:09 -05001851 CellLayoutLayoutParams
1852 lp = (CellLayoutLayoutParams) view.getLayoutParams();
Sebastian Franco877088e2023-01-03 15:16:22 -07001853 mOccupied.markCells(lp.getCellX(), lp.getCellY(), lp.cellHSpan, lp.cellVSpan, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001854 }
1855
Adam Cohen2801caf2011-05-13 20:57:39 -07001856 public int getDesiredWidth() {
Jon Miranda228877d2021-02-09 11:05:00 -05001857 return getPaddingLeft() + getPaddingRight() + (mCountX * mCellWidth)
Thales Lima78d00ad2021-09-30 11:29:06 +01001858 + ((mCountX - 1) * mBorderSpace.x);
Adam Cohen2801caf2011-05-13 20:57:39 -07001859 }
1860
1861 public int getDesiredHeight() {
Jon Miranda228877d2021-02-09 11:05:00 -05001862 return getPaddingTop() + getPaddingBottom() + (mCountY * mCellHeight)
Thales Lima78d00ad2021-09-30 11:29:06 +01001863 + ((mCountY - 1) * mBorderSpace.y);
Adam Cohen2801caf2011-05-13 20:57:39 -07001864 }
1865
Michael Jurka66d72172011-04-12 16:29:25 -07001866 public boolean isOccupied(int x, int y) {
Schneider Victor-tulias750c5af2023-07-26 10:16:04 -07001867 if (x >= 0 && x < mCountX && y >= 0 && y < mCountY) {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001868 return mOccupied.cells[x][y];
Schneider Victor-tulias750c5af2023-07-26 10:16:04 -07001869 }
1870 if (BuildConfig.IS_STUDIO_BUILD) {
Michael Jurka66d72172011-04-12 16:29:25 -07001871 throw new RuntimeException("Position exceeds the bound of this CellLayout");
1872 }
Schneider Victor-tulias750c5af2023-07-26 10:16:04 -07001873 return true;
Michael Jurka66d72172011-04-12 16:29:25 -07001874 }
1875
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001876 @Override
1877 public ViewGroup.LayoutParams generateLayoutParams(AttributeSet attrs) {
Sebastian Francod4682992022-10-05 13:03:09 -05001878 return new CellLayoutLayoutParams(getContext(), attrs);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001879 }
1880
1881 @Override
1882 protected boolean checkLayoutParams(ViewGroup.LayoutParams p) {
Sebastian Francod4682992022-10-05 13:03:09 -05001883 return p instanceof CellLayoutLayoutParams;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001884 }
1885
1886 @Override
1887 protected ViewGroup.LayoutParams generateLayoutParams(ViewGroup.LayoutParams p) {
Sebastian Francod4682992022-10-05 13:03:09 -05001888 return new CellLayoutLayoutParams(p);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001889 }
1890
Samuel Fufa1e2d0042019-11-18 17:12:46 -08001891 /**
Tony Wickham86930612015-09-09 13:50:40 -07001892 * Returns whether an item can be placed in this CellLayout (after rearranging and/or resizing
1893 * if necessary).
1894 */
1895 public boolean hasReorderSolution(ItemInfo itemInfo) {
1896 int[] cellPoint = new int[2];
1897 // Check for a solution starting at every cell.
1898 for (int cellX = 0; cellX < getCountX(); cellX++) {
1899 for (int cellY = 0; cellY < getCountY(); cellY++) {
1900 cellToPoint(cellX, cellY, cellPoint);
1901 if (findReorderSolution(cellPoint[0], cellPoint[1], itemInfo.minSpanX,
1902 itemInfo.minSpanY, itemInfo.spanX, itemInfo.spanY, mDirectionVector, null,
Charlie Anderson46122392024-01-18 17:23:46 -05001903 true).isSolution) {
Tony Wickham86930612015-09-09 13:50:40 -07001904 return true;
1905 }
1906 }
1907 }
1908 return false;
1909 }
1910
Samuel Fufaa4211432020-02-25 18:47:54 -08001911 /**
1912 * Finds solution to accept hotseat migration to cell layout. commits solution if commitConfig
1913 */
1914 public boolean makeSpaceForHotseatMigration(boolean commitConfig) {
Samuel Fufaa4211432020-02-25 18:47:54 -08001915 int[] cellPoint = new int[2];
1916 int[] directionVector = new int[]{0, -1};
1917 cellToPoint(0, mCountY, cellPoint);
Charlie Anderson46122392024-01-18 17:23:46 -05001918 ItemConfiguration configuration = findReorderSolution(
1919 cellPoint[0] /* pixelX */,
1920 cellPoint[1] /* pixelY */,
1921 mCountX /* minSpanX */,
1922 1 /* minSpanY */,
1923 mCountX /* spanX */,
1924 1 /* spanY */,
1925 directionVector /* direction */,
1926 null /* dragView */,
1927 false /* decX */
1928 );
1929 if (configuration.isSolution) {
Samuel Fufaa4211432020-02-25 18:47:54 -08001930 if (commitConfig) {
1931 copySolutionToTempState(configuration, null);
Sunny Goyal711c5962021-06-23 12:36:18 -07001932 commitTempPlacement(null);
Samuel Fufa82bbdac2020-03-09 18:24:47 -07001933 // undo marking cells occupied since there is actually nothing being placed yet.
1934 mOccupied.markCells(0, mCountY - 1, mCountX, 1, false);
Samuel Fufaa4211432020-02-25 18:47:54 -08001935 }
1936 return true;
1937 }
1938 return false;
1939 }
1940
Samuel Fufa82bbdac2020-03-09 18:24:47 -07001941 /**
1942 * returns a copy of cell layout's grid occupancy
1943 */
1944 public GridOccupancy cloneGridOccupancy() {
1945 GridOccupancy occupancy = new GridOccupancy(mCountX, mCountY);
1946 mOccupied.copyTo(occupancy);
1947 return occupancy;
1948 }
1949
Sunny Goyal9ca9c132015-04-29 14:57:22 -07001950 public boolean isRegionVacant(int x, int y, int spanX, int spanY) {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001951 return mOccupied.isRegionVacant(x, y, spanX, spanY);
Sunny Goyal9ca9c132015-04-29 14:57:22 -07001952 }
Sebastian Franco9ea36d42023-09-21 13:56:42 -07001953
1954 public void setSpaceBetweenCellLayoutsPx(@Px int spaceBetweenCellLayoutsPx) {
1955 mSpaceBetweenCellLayoutsPx = spaceBetweenCellLayoutsPx;
1956 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001957}