blob: 7e9e8645e64c1195f236c927117c63c6bcaf7e56 [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;
Sunny Goyal82dfc152023-02-24 16:50:09 -080074import com.android.launcher3.util.MultiTranslateDelegate;
Sunny Goyale2fd14b2015-08-27 17:45:46 -070075import com.android.launcher3.util.ParcelableSparseArray;
Sunny Goyal9b29ca52017-02-17 10:39:44 -080076import com.android.launcher3.util.Themes;
Adam Cohen091440a2015-03-18 14:16:05 -070077import com.android.launcher3.util.Thunk;
Sunny Goyalab770a12018-11-14 15:17:26 -080078import com.android.launcher3.views.ActivityContext;
Steven Ng32427202021-04-19 18:12:12 +010079import com.android.launcher3.widget.LauncherAppWidgetHostView;
Sunny Goyal5bc6b6f2017-10-26 15:36:10 -070080
Sunny Goyalc13403c2016-11-18 23:44:48 -080081import java.lang.annotation.Retention;
82import java.lang.annotation.RetentionPolicy;
Adam Cohen69ce2e52011-07-03 19:25:21 -070083import java.util.ArrayList;
Adam Cohenc0dcf592011-06-01 15:30:43 -070084import java.util.Arrays;
Adam Cohend41fbf52012-02-16 23:53:59 -080085import java.util.Stack;
Adam Cohenc0dcf592011-06-01 15:30:43 -070086
Sunny Goyalc4d32012020-04-03 17:10:11 -070087public class CellLayout extends ViewGroup {
Tony Wickhama0628cc2015-10-14 15:23:04 -070088 private static final String TAG = "CellLayout";
89 private static final boolean LOGD = false;
Winson Chungaafa03c2010-06-11 17:34:16 -070090
Tony Wickhamec6fd6f2023-03-11 02:08:57 +000091 /** The color of the "leave-behind" shape when a folder is opened from Hotseat. */
92 private static final int FOLDER_LEAVE_BEHIND_COLOR = Color.argb(160, 245, 245, 245);
93
Sunny Goyalab770a12018-11-14 15:17:26 -080094 protected final ActivityContext mActivity;
Sunny Goyal4ffec482016-02-09 11:28:52 -080095 @ViewDebug.ExportedProperty(category = "launcher")
Adam Cohen091440a2015-03-18 14:16:05 -070096 @Thunk int mCellWidth;
Sunny Goyal4ffec482016-02-09 11:28:52 -080097 @ViewDebug.ExportedProperty(category = "launcher")
Adam Cohen091440a2015-03-18 14:16:05 -070098 @Thunk int mCellHeight;
Winson Chung11a1a532013-09-13 11:14:45 -070099 private int mFixedCellWidth;
100 private int mFixedCellHeight;
Jon Miranda228877d2021-02-09 11:05:00 -0500101 @ViewDebug.ExportedProperty(category = "launcher")
Sebastian Franco25423862023-03-10 10:50:37 -0800102 protected Point mBorderSpace;
Winson Chungaafa03c2010-06-11 17:34:16 -0700103
Sunny Goyal4ffec482016-02-09 11:28:52 -0800104 @ViewDebug.ExportedProperty(category = "launcher")
Sebastian Franco09589322022-11-02 15:25:58 -0700105 protected int mCountX;
Sunny Goyal4ffec482016-02-09 11:28:52 -0800106 @ViewDebug.ExportedProperty(category = "launcher")
Sebastian Franco09589322022-11-02 15:25:58 -0700107 protected int mCountY;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800108
Adam Cohen917e3882013-10-31 15:03:35 -0700109 private boolean mDropPending = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800110
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700111 // These are temporary variables to prevent having to allocate a new object just to
112 // return an (x, y) value from helper functions. Do NOT use them to maintain other state.
Adam Cohen091440a2015-03-18 14:16:05 -0700113 @Thunk final int[] mTmpPoint = new int[2];
Sunny Goyal2805e632015-05-20 15:35:32 -0700114 @Thunk final int[] mTempLocation = new int[2];
Sebastian Franco0dd5db82023-10-13 11:09:21 -0700115
116 @Thunk final Rect mTempOnDrawCellToRect = new Rect();
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700117
Sebastian Franco09589322022-11-02 15:25:58 -0700118 protected GridOccupancy mOccupied;
Sebastian Francoe4c03452022-12-27 14:50:02 -0600119 public GridOccupancy mTmpOccupied;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800120
Michael Jurkadee05892010-07-27 10:01:56 -0700121 private OnTouchListener mInterceptTouchListener;
122
Samuel Fufa1e2d0042019-11-18 17:12:46 -0800123 private final ArrayList<DelegatedCellDrawing> mDelegatedCellDrawings = new ArrayList<>();
Sunny Goyal638a6872024-04-25 15:55:11 -0700124 final PreviewBackground mFolderLeaveBehind = new PreviewBackground(getContext());
Adam Cohen69ce2e52011-07-03 19:25:21 -0700125
Sunny Goyalf5440cb2016-12-14 15:13:00 -0800126 private static final int[] BACKGROUND_STATE_ACTIVE = new int[] { android.R.attr.state_active };
Sunny Goyale15e2a82017-12-15 13:05:42 -0800127 private static final int[] BACKGROUND_STATE_DEFAULT = EMPTY_STATE_SET;
Sebastian Franco09589322022-11-02 15:25:58 -0700128 protected final Drawable mBackground;
Sunny Goyal2805e632015-05-20 15:35:32 -0700129
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700130 // These values allow a fixed measurement to be set on the CellLayout.
131 private int mFixedWidth = -1;
132 private int mFixedHeight = -1;
133
Michael Jurka33945b22010-12-21 18:19:38 -0800134 // If we're actively dragging something over this screen, mIsDragOverlapping is true
135 private boolean mIsDragOverlapping = false;
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700136
Winson Chung150fbab2010-09-29 17:14:26 -0700137 // These arrays are used to implement the drag visualization on x-large screens.
Joe Onorato4be866d2010-10-10 11:26:02 -0700138 // They are used as circular arrays, indexed by mDragOutlineCurrent.
Sebastian Francod4682992022-10-05 13:03:09 -0500139 @Thunk final CellLayoutLayoutParams[] mDragOutlines = new CellLayoutLayoutParams[4];
Rajeev Kumar9962dbe2017-06-12 12:16:20 -0700140 @Thunk final float[] mDragOutlineAlphas = new float[mDragOutlines.length];
141 private final InterruptibleInOutAnimator[] mDragOutlineAnims =
Joe Onorato4be866d2010-10-10 11:26:02 -0700142 new InterruptibleInOutAnimator[mDragOutlines.length];
Winson Chung150fbab2010-09-29 17:14:26 -0700143
144 // Used as an index into the above 3 arrays; indicates which is the most current value.
Joe Onorato4be866d2010-10-10 11:26:02 -0700145 private int mDragOutlineCurrent = 0;
Patrick Dubroy8e58e912010-10-14 13:21:48 -0700146 private final Paint mDragOutlinePaint = new Paint();
Winson Chung150fbab2010-09-29 17:14:26 -0700147
Sebastian Francod4682992022-10-05 13:03:09 -0500148 @Thunk final ArrayMap<CellLayoutLayoutParams, Animator> mReorderAnimators = new ArrayMap<>();
Adam Cohend9162062020-03-24 16:35:35 -0700149 @Thunk final ArrayMap<Reorderable, ReorderPreviewAnimation> mShakeAnimators = new ArrayMap<>();
Adam Cohen19f37922012-03-21 11:59:11 -0700150
151 private boolean mItemPlacementDirty = false;
Adam Cohenbfbfd262011-06-13 16:55:12 -0700152
Adam Cohenf7ca3b42021-02-22 11:03:58 -0800153 // Used to visualize the grid and drop locations
154 private boolean mVisualizeCells = false;
155 private boolean mVisualizeDropLocation = true;
156 private RectF mVisualizeGridRect = new RectF();
157 private Paint mVisualizeGridPaint = new Paint();
Adam Cohenf7ca3b42021-02-22 11:03:58 -0800158 private int mGridVisualizationRoundingRadius;
159 private float mGridAlpha = 0f;
160 private int mGridColor = 0;
Sebastian Franco09589322022-11-02 15:25:58 -0700161 protected float mSpringLoadedProgress = 0f;
Adam Cohenf7ca3b42021-02-22 11:03:58 -0800162 private float mScrollProgress = 0f;
163
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700164 // When a drag operation is in progress, holds the nearest cell to the touch point
165 private final int[] mDragCell = new int[2];
Adam Cohenf7ca3b42021-02-22 11:03:58 -0800166 private final int[] mDragCellSpan = new int[2];
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800167
Joe Onorato4be866d2010-10-10 11:26:02 -0700168 private boolean mDragging = false;
169
Rajeev Kumar9962dbe2017-06-12 12:16:20 -0700170 private final TimeInterpolator mEaseOutInterpolator;
Sebastian Franco09589322022-11-02 15:25:58 -0700171 protected final ShortcutAndWidgetContainer mShortcutsAndWidgets;
Sebastian Franco9ea36d42023-09-21 13:56:42 -0700172 @Px
173 protected int mSpaceBetweenCellLayoutsPx = 0;
Patrick Dubroyce34a972010-10-19 10:34:32 -0700174
Sunny Goyalc13403c2016-11-18 23:44:48 -0800175 @Retention(RetentionPolicy.SOURCE)
176 @IntDef({WORKSPACE, HOTSEAT, FOLDER})
177 public @interface ContainerType{}
178 public static final int WORKSPACE = 0;
179 public static final int HOTSEAT = 1;
180 public static final int FOLDER = 2;
181
182 @ContainerType private final int mContainerType;
183
Sebastian Francof7654252024-01-18 11:50:50 -0800184 public static final float DEFAULT_SCALE = 1f;
Andrew Flynn0dca1ec2012-02-29 13:33:22 -0800185
Adam Cohenfa3c58f2013-12-06 16:10:55 -0800186 public static final int MODE_SHOW_REORDER_HINT = 0;
187 public static final int MODE_DRAG_OVER = 1;
188 public static final int MODE_ON_DROP = 2;
189 public static final int MODE_ON_DROP_EXTERNAL = 3;
190 public static final int MODE_ACCEPT_DROP = 4;
Adam Cohen19f37922012-03-21 11:59:11 -0700191 private static final boolean DESTRUCTIVE_REORDER = false;
Adam Cohen482ed822012-03-02 14:15:13 -0800192 private static final boolean DEBUG_VISUALIZE_OCCUPIED = false;
193
Sebastian Francof7654252024-01-18 11:50:50 -0800194 public static final float REORDER_PREVIEW_MAGNITUDE = 0.12f;
195 public static final int REORDER_ANIMATION_DURATION = 150;
Sunny Goyalc13403c2016-11-18 23:44:48 -0800196 @Thunk final float mReorderPreviewAnimationMagnitude;
Adam Cohen19f37922012-03-21 11:59:11 -0700197
Sebastian Francoe4c03452022-12-27 14:50:02 -0600198 public final int[] mDirectionVector = new int[2];
Steven Ng30dd1d62021-03-15 21:45:49 +0000199
Sebastian Franco53a15a42022-10-25 17:28:54 -0700200 ItemConfiguration mPreviousSolution = null;
Adam Cohen482ed822012-03-02 14:15:13 -0800201
Sunny Goyal2805e632015-05-20 15:35:32 -0700202 private final Rect mTempRect = new Rect();
Winson Chung3a6e7f32013-10-09 15:50:52 -0700203
Sunny Goyal73b5a272019-12-09 14:55:56 -0800204 private static final Paint sPaint = new Paint();
Romain Guy8a0bff52012-05-06 13:14:33 -0700205
Adam Cohenc9735cf2015-01-23 16:11:55 -0800206 // Related to accessible drag and drop
Adam Cohen6e7c37a2020-06-25 19:22:37 -0700207 DragAndDropAccessibilityDelegate mTouchHelper;
Adam Cohenc9735cf2015-01-23 16:11:55 -0800208
Sebastian Franco2986e0b2024-01-25 10:23:39 -0800209 CellLayoutContainer mCellLayoutContainer;
Adam Cohenf7ca3b42021-02-22 11:03:58 -0800210
211 public static final FloatProperty<CellLayout> SPRING_LOADED_PROGRESS =
212 new FloatProperty<CellLayout>("spring_loaded_progress") {
213 @Override
214 public Float get(CellLayout cl) {
215 return cl.getSpringLoadedProgress();
216 }
217
218 @Override
219 public void setValue(CellLayout cl, float progress) {
220 cl.setSpringLoadedProgress(progress);
221 }
222 };
223
Sebastian Franco2986e0b2024-01-25 10:23:39 -0800224 public CellLayout(Context context, CellLayoutContainer container) {
225 this(context, (AttributeSet) null);
226 this.mCellLayoutContainer = container;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800227 }
228
229 public CellLayout(Context context, AttributeSet attrs) {
230 this(context, attrs, 0);
231 }
232
233 public CellLayout(Context context, AttributeSet attrs, int defStyle) {
234 super(context, attrs, defStyle);
Sunny Goyalc13403c2016-11-18 23:44:48 -0800235 TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.CellLayout, defStyle, 0);
236 mContainerType = a.getInteger(R.styleable.CellLayout_containerType, WORKSPACE);
237 a.recycle();
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700238
239 // A ViewGroup usually does not draw, but CellLayout needs to draw a rectangle to show
240 // the user where a dragged item will land when dropped.
241 setWillNotDraw(false);
Romain Guyce3cbd12013-02-25 15:00:36 -0800242 setClipToPadding(false);
Pat Manningd0f729d2023-01-09 12:04:25 +0000243 setClipChildren(false);
Sunny Goyalab770a12018-11-14 15:17:26 -0800244 mActivity = ActivityContext.lookupContext(context);
Steven Ngcc505b82021-03-18 23:04:35 +0000245 DeviceProfile deviceProfile = mActivity.getDeviceProfile();
Michael Jurkaa63c4522010-08-19 13:52:27 -0700246
Thales Lima8cd020b2022-03-15 20:15:14 +0000247 resetCellSizeInternal(deviceProfile);
Sunny Goyalff4ba2d2016-04-02 14:12:34 -0700248
Steven Ngcc505b82021-03-18 23:04:35 +0000249 mCountX = deviceProfile.inv.numColumns;
250 mCountY = deviceProfile.inv.numRows;
Sunny Goyalff4ba2d2016-04-02 14:12:34 -0700251 mOccupied = new GridOccupancy(mCountX, mCountY);
252 mTmpOccupied = new GridOccupancy(mCountX, mCountY);
253
Samuel Fufa1e2d0042019-11-18 17:12:46 -0800254 mFolderLeaveBehind.mDelegateCellX = -1;
255 mFolderLeaveBehind.mDelegateCellY = -1;
Adam Cohenefca0272016-02-24 19:19:06 -0800256
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800257 setAlwaysDrawnWithCacheEnabled(false);
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700258
Adam Cohenf7ca3b42021-02-22 11:03:58 -0800259 Resources res = getResources();
260
261 mBackground = getContext().getDrawable(R.drawable.bg_celllayout);
Sunny Goyal2805e632015-05-20 15:35:32 -0700262 mBackground.setCallback(this);
Sunny Goyalaeb16432017-10-16 11:46:41 -0700263 mBackground.setAlpha(0);
Michael Jurka33945b22010-12-21 18:19:38 -0800264
Yogisha Dixitc0ac1dd2021-05-29 00:26:25 +0100265 mGridColor = Themes.getAttrColor(getContext(), R.attr.workspaceAccentColor);
Adam Cohenf7ca3b42021-02-22 11:03:58 -0800266 mGridVisualizationRoundingRadius =
267 res.getDimensionPixelSize(R.dimen.grid_visualization_rounding_radius);
Steven Ngcc505b82021-03-18 23:04:35 +0000268 mReorderPreviewAnimationMagnitude = (REORDER_PREVIEW_MAGNITUDE * deviceProfile.iconSizePx);
Adam Cohen19f37922012-03-21 11:59:11 -0700269
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700270 // Initialize the data structures used for the drag visualization.
Kateryna Ivanova71203732023-05-24 15:09:00 +0000271 mEaseOutInterpolator = Interpolators.DECELERATE_QUINT; // Quint ease out
Winson Chungb8c69f32011-10-19 21:36:08 -0700272 mDragCell[0] = mDragCell[1] = -1;
Adam Cohenf7ca3b42021-02-22 11:03:58 -0800273 mDragCellSpan[0] = mDragCellSpan[1] = -1;
Joe Onorato4be866d2010-10-10 11:26:02 -0700274 for (int i = 0; i < mDragOutlines.length; i++) {
Sunny Goyal669b71f2023-01-27 14:37:07 -0800275 mDragOutlines[i] = new CellLayoutLayoutParams(0, 0, 0, 0);
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700276 }
Mario Bertschler54ba6012017-06-08 10:53:53 -0700277 mDragOutlinePaint.setColor(Themes.getAttrColor(context, R.attr.workspaceTextColor));
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700278
279 // When dragging things around the home screens, we show a green outline of
280 // where the item will land. The outlines gradually fade out, leaving a trail
281 // behind the drag path.
282 // Set up all the animations that are used to implement this fading.
283 final int duration = res.getInteger(R.integer.config_dragOutlineFadeTime);
Chet Haase472b2812010-10-14 07:02:04 -0700284 final float fromAlphaValue = 0;
285 final float toAlphaValue = (float)res.getInteger(R.integer.config_dragOutlineMaxAlpha);
Joe Onorato4be866d2010-10-10 11:26:02 -0700286
Patrick Dubroy8e58e912010-10-14 13:21:48 -0700287 Arrays.fill(mDragOutlineAlphas, fromAlphaValue);
Joe Onorato4be866d2010-10-10 11:26:02 -0700288
289 for (int i = 0; i < mDragOutlineAnims.length; i++) {
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700290 final InterruptibleInOutAnimator anim =
Sebastian Francof153d912022-04-22 16:15:27 -0500291 new InterruptibleInOutAnimator(duration, fromAlphaValue, toAlphaValue);
Patrick Dubroyce34a972010-10-19 10:34:32 -0700292 anim.getAnimator().setInterpolator(mEaseOutInterpolator);
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700293 final int thisIndex = i;
Chet Haase472b2812010-10-14 07:02:04 -0700294 anim.getAnimator().addUpdateListener(new AnimatorUpdateListener() {
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700295 public void onAnimationUpdate(ValueAnimator animation) {
Joe Onorato4be866d2010-10-10 11:26:02 -0700296 // If an animation is started and then stopped very quickly, we can still
297 // get spurious updates we've cleared the tag. Guard against this.
Adam Cohenf7ca3b42021-02-22 11:03:58 -0800298 mDragOutlineAlphas[thisIndex] = (Float) animation.getAnimatedValue();
299 CellLayout.this.invalidate();
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700300 }
301 });
Joe Onorato4be866d2010-10-10 11:26:02 -0700302 // The animation holds a reference to the drag outline bitmap as long is it's
303 // running. This way the bitmap can be GCed when the animations are complete.
Joe Onorato4be866d2010-10-10 11:26:02 -0700304 mDragOutlineAnims[i] = anim;
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700305 }
Patrick Dubroyce34a972010-10-19 10:34:32 -0700306
Sunny Goyalc13403c2016-11-18 23:44:48 -0800307 mShortcutsAndWidgets = new ShortcutAndWidgetContainer(context, mContainerType);
Jon Miranda228877d2021-02-09 11:05:00 -0500308 mShortcutsAndWidgets.setCellDimensions(mCellWidth, mCellHeight, mCountX, mCountY,
Thales Lima78d00ad2021-09-30 11:29:06 +0100309 mBorderSpace);
Michael Jurkaa52570f2012-03-20 03:18:20 -0700310 addView(mShortcutsAndWidgets);
Michael Jurka18014792010-10-14 09:01:34 -0700311 }
312
Sebastian Franco2986e0b2024-01-25 10:23:39 -0800313 public CellLayoutContainer getCellLayoutContainer() {
314 return mCellLayoutContainer;
315 }
316
317 public void setCellLayoutContainer(CellLayoutContainer cellLayoutContainer) {
318 mCellLayoutContainer = cellLayoutContainer;
319 }
320
Sunny Goyal9b180102020-03-11 10:02:29 -0700321 /**
322 * Sets or clears a delegate used for accessible drag and drop
323 */
324 public void setDragAndDropAccessibilityDelegate(DragAndDropAccessibilityDelegate delegate) {
Sunny Goyal9b180102020-03-11 10:02:29 -0700325 ViewCompat.setAccessibilityDelegate(this, delegate);
326
Adam Cohen6e7c37a2020-06-25 19:22:37 -0700327 mTouchHelper = delegate;
328 int accessibilityFlag = mTouchHelper != null
Sunny Goyal9b180102020-03-11 10:02:29 -0700329 ? IMPORTANT_FOR_ACCESSIBILITY_YES : IMPORTANT_FOR_ACCESSIBILITY_NO;
330 setImportantForAccessibility(accessibilityFlag);
331 getShortcutsAndWidgets().setImportantForAccessibility(accessibilityFlag);
Sunny Goyal384b5782021-02-09 22:50:02 -0800332 // ExploreByTouchHelper sets focusability. Clear it when the delegate is cleared.
333 setFocusable(delegate != null);
Adam Cohenc9735cf2015-01-23 16:11:55 -0800334 // Invalidate the accessibility hierarchy
335 if (getParent() != null) {
336 getParent().notifySubtreeAccessibilityStateChanged(
337 this, this, AccessibilityEvent.CONTENT_CHANGE_TYPE_SUBTREE);
338 }
339 }
340
Sunny Goyala4647b62021-02-02 13:45:34 -0800341 /**
342 * Returns the currently set accessibility delegate
343 */
344 public DragAndDropAccessibilityDelegate getDragAndDropAccessibilityDelegate() {
345 return mTouchHelper;
346 }
347
Adam Cohenc9735cf2015-01-23 16:11:55 -0800348 @Override
Adam Cohen6e7c37a2020-06-25 19:22:37 -0700349 public boolean dispatchHoverEvent(MotionEvent event) {
350 // Always attempt to dispatch hover events to accessibility first.
351 if (mTouchHelper != null && mTouchHelper.dispatchHoverEvent(event)) {
352 return true;
353 }
354 return super.dispatchHoverEvent(event);
355 }
356
357 @Override
Adam Cohenc9735cf2015-01-23 16:11:55 -0800358 public boolean onInterceptTouchEvent(MotionEvent ev) {
Winson Chungf9935182020-10-23 09:26:44 -0700359 return mTouchHelper != null
360 || (mInterceptTouchListener != null && mInterceptTouchListener.onTouch(this, ev));
Adam Cohenc9735cf2015-01-23 16:11:55 -0800361 }
362
Chris Craik01f2d7f2013-10-01 14:41:56 -0700363 public void enableHardwareLayer(boolean hasLayer) {
364 mShortcutsAndWidgets.setLayerType(hasLayer ? LAYER_TYPE_HARDWARE : LAYER_TYPE_NONE, sPaint);
Michael Jurkad51f33a2012-06-28 15:35:26 -0700365 }
366
vadimt04f356f2019-02-14 18:46:36 -0800367 public boolean isHardwareLayerEnabled() {
368 return mShortcutsAndWidgets.getLayerType() == LAYER_TYPE_HARDWARE;
369 }
370
Thales Lima8cd020b2022-03-15 20:15:14 +0000371 /**
372 * Change sizes of cells
373 *
374 * @param width the new width of the cells
375 * @param height the new height of the cells
376 */
Winson Chung5f8afe62013-08-12 16:19:28 -0700377 public void setCellDimensions(int width, int height) {
Winson Chung11a1a532013-09-13 11:14:45 -0700378 mFixedCellWidth = mCellWidth = width;
379 mFixedCellHeight = mCellHeight = height;
Jon Miranda228877d2021-02-09 11:05:00 -0500380 mShortcutsAndWidgets.setCellDimensions(mCellWidth, mCellHeight, mCountX, mCountY,
Thales Lima78d00ad2021-09-30 11:29:06 +0100381 mBorderSpace);
Winson Chung5f8afe62013-08-12 16:19:28 -0700382 }
383
Thales Lima8cd020b2022-03-15 20:15:14 +0000384 private void resetCellSizeInternal(DeviceProfile deviceProfile) {
385 switch (mContainerType) {
386 case FOLDER:
Jordan Silva637f4eb2023-06-13 11:21:53 +0100387 mBorderSpace = new Point(deviceProfile.folderCellLayoutBorderSpacePx);
Thales Lima8cd020b2022-03-15 20:15:14 +0000388 break;
389 case HOTSEAT:
390 mBorderSpace = new Point(deviceProfile.hotseatBorderSpace,
391 deviceProfile.hotseatBorderSpace);
392 break;
393 case WORKSPACE:
394 default:
395 mBorderSpace = new Point(deviceProfile.cellLayoutBorderSpacePx);
396 break;
397 }
398
399 mCellWidth = mCellHeight = -1;
400 mFixedCellWidth = mFixedCellHeight = -1;
401 }
402
403 /**
404 * Reset the cell sizes and border space
405 */
406 public void resetCellSize(DeviceProfile deviceProfile) {
407 resetCellSizeInternal(deviceProfile);
408 requestLayout();
409 }
410
Adam Cohen2801caf2011-05-13 20:57:39 -0700411 public void setGridSize(int x, int y) {
412 mCountX = x;
413 mCountY = y;
Sunny Goyalff4ba2d2016-04-02 14:12:34 -0700414 mOccupied = new GridOccupancy(mCountX, mCountY);
415 mTmpOccupied = new GridOccupancy(mCountX, mCountY);
Jon Miranda228877d2021-02-09 11:05:00 -0500416 mShortcutsAndWidgets.setCellDimensions(mCellWidth, mCellHeight, mCountX, mCountY,
Thales Lima78d00ad2021-09-30 11:29:06 +0100417 mBorderSpace);
Adam Cohen76fc0852011-06-17 13:26:23 -0700418 requestLayout();
Adam Cohen2801caf2011-05-13 20:57:39 -0700419 }
420
Adam Cohen2374abf2013-04-16 14:56:57 -0700421 // Set whether or not to invert the layout horizontally if the layout is in RTL mode.
422 public void setInvertIfRtl(boolean invert) {
423 mShortcutsAndWidgets.setInvertIfRtl(invert);
424 }
425
Adam Cohen917e3882013-10-31 15:03:35 -0700426 public void setDropPending(boolean pending) {
427 mDropPending = pending;
428 }
429
430 public boolean isDropPending() {
431 return mDropPending;
432 }
433
Adam Cohenc50438c2014-08-19 17:43:05 -0700434 void setIsDragOverlapping(boolean isDragOverlapping) {
435 if (mIsDragOverlapping != isDragOverlapping) {
436 mIsDragOverlapping = isDragOverlapping;
Sunny Goyalf5440cb2016-12-14 15:13:00 -0800437 mBackground.setState(mIsDragOverlapping
438 ? BACKGROUND_STATE_ACTIVE : BACKGROUND_STATE_DEFAULT);
Adam Cohenc50438c2014-08-19 17:43:05 -0700439 invalidate();
440 }
441 }
442
Sunny Goyale2fd14b2015-08-27 17:45:46 -0700443 @Override
444 protected void dispatchSaveInstanceState(SparseArray<Parcelable> container) {
Sunny Goyal7ce471b2017-08-02 03:37:39 -0700445 ParcelableSparseArray jail = getJailedArray(container);
446 super.dispatchSaveInstanceState(jail);
447 container.put(R.id.cell_layout_jail_id, jail);
Sunny Goyale2fd14b2015-08-27 17:45:46 -0700448 }
449
450 @Override
451 protected void dispatchRestoreInstanceState(SparseArray<Parcelable> container) {
Sunny Goyal7ce471b2017-08-02 03:37:39 -0700452 super.dispatchRestoreInstanceState(getJailedArray(container));
Sunny Goyale2fd14b2015-08-27 17:45:46 -0700453 }
454
Sunny Goyal7ce471b2017-08-02 03:37:39 -0700455 /**
456 * Wrap the SparseArray in another Parcelable so that the item ids do not conflict with our
457 * our internal resource ids
458 */
Sunny Goyale2fd14b2015-08-27 17:45:46 -0700459 private ParcelableSparseArray getJailedArray(SparseArray<Parcelable> container) {
460 final Parcelable parcelable = container.get(R.id.cell_layout_jail_id);
461 return parcelable instanceof ParcelableSparseArray ?
462 (ParcelableSparseArray) parcelable : new ParcelableSparseArray();
463 }
464
Tony Wickham0f97b782015-12-02 17:55:07 -0800465 public boolean getIsDragOverlapping() {
466 return mIsDragOverlapping;
467 }
468
Jeff Sharkey83f111d2009-04-20 21:03:13 -0700469 @Override
Patrick Dubroy1262e362010-10-06 15:49:50 -0700470 protected void onDraw(Canvas canvas) {
Michael Jurka3e7c7632010-10-02 16:01:03 -0700471 // When we're large, we are either drawn in a "hover" state (ie when dragging an item to
472 // a neighboring page) or with just a normal background (if backgroundAlpha > 0.0f)
473 // When we're small, we are either drawn normally or in the "accepts drops" state (during
474 // a drag). However, we also drag the mini hover background *over* one of those two
475 // backgrounds
Sunny Goyalaeb16432017-10-16 11:46:41 -0700476 if (mBackground.getAlpha() > 0) {
Sunny Goyal2805e632015-05-20 15:35:32 -0700477 mBackground.draw(canvas);
Michael Jurkaa63c4522010-08-19 13:52:27 -0700478 }
Romain Guya6abce82009-11-10 02:54:41 -0800479
Adam Cohen482ed822012-03-02 14:15:13 -0800480 if (DEBUG_VISUALIZE_OCCUPIED) {
Tony Wickham0ac045f2021-11-03 13:17:02 -0700481 Rect cellBounds = new Rect();
482 // Will contain the bounds of the cell including spacing between cells.
483 Rect cellBoundsWithSpacing = new Rect();
Tony Wickham12784902021-11-03 14:02:10 -0700484 int[] targetCell = new int[2];
Tony Wickham0ac045f2021-11-03 13:17:02 -0700485 int[] cellCenter = new int[2];
486 Paint debugPaint = new Paint();
487 debugPaint.setStrokeWidth(Utilities.dpToPx(1));
488 for (int x = 0; x < mCountX; x++) {
489 for (int y = 0; y < mCountY; y++) {
490 if (!mOccupied.cells[x][y]) {
491 continue;
Adam Cohen482ed822012-03-02 14:15:13 -0800492 }
Tony Wickham12784902021-11-03 14:02:10 -0700493 targetCell[0] = x;
494 targetCell[1] = y;
Tony Wickham0ac045f2021-11-03 13:17:02 -0700495
Tony Wickham12784902021-11-03 14:02:10 -0700496 boolean canCreateFolder = canCreateFolder(getChildAt(x, y));
Tony Wickham0ac045f2021-11-03 13:17:02 -0700497 cellToRect(x, y, 1, 1, cellBounds);
498 cellBoundsWithSpacing.set(cellBounds);
499 cellBoundsWithSpacing.inset(-mBorderSpace.x / 2, -mBorderSpace.y / 2);
Tony Wickham3cfa5ed2021-11-03 13:20:43 -0700500 getWorkspaceCellVisualCenter(x, y, cellCenter);
Tony Wickham0ac045f2021-11-03 13:17:02 -0700501
502 canvas.save();
503 canvas.clipRect(cellBoundsWithSpacing);
504
505 // Draw reorder drag target.
506 debugPaint.setColor(Color.RED);
Sebastian Franco6e1024e2022-07-29 13:46:49 -0700507 canvas.drawCircle(cellCenter[0], cellCenter[1],
508 getReorderRadius(targetCell, 1, 1), debugPaint);
Tony Wickham0ac045f2021-11-03 13:17:02 -0700509
510 // Draw folder creation drag target.
511 if (canCreateFolder) {
512 debugPaint.setColor(Color.GREEN);
513 canvas.drawCircle(cellCenter[0], cellCenter[1],
Tony Wickham12784902021-11-03 14:02:10 -0700514 getFolderCreationRadius(targetCell), debugPaint);
Tony Wickham0ac045f2021-11-03 13:17:02 -0700515 }
516
517 canvas.restore();
Adam Cohen482ed822012-03-02 14:15:13 -0800518 }
519 }
520 }
521
Samuel Fufa1e2d0042019-11-18 17:12:46 -0800522 for (int i = 0; i < mDelegatedCellDrawings.size(); i++) {
523 DelegatedCellDrawing cellDrawing = mDelegatedCellDrawings.get(i);
524 cellToPoint(cellDrawing.mDelegateCellX, cellDrawing.mDelegateCellY, mTempLocation);
Adam Cohenefca0272016-02-24 19:19:06 -0800525 canvas.save();
526 canvas.translate(mTempLocation[0], mTempLocation[1]);
Samuel Fufa1e2d0042019-11-18 17:12:46 -0800527 cellDrawing.drawUnderItem(canvas);
Adam Cohenefca0272016-02-24 19:19:06 -0800528 canvas.restore();
Adam Cohen69ce2e52011-07-03 19:25:21 -0700529 }
Adam Cohenc51934b2011-07-26 21:07:43 -0700530
Samuel Fufa1e2d0042019-11-18 17:12:46 -0800531 if (mFolderLeaveBehind.mDelegateCellX >= 0 && mFolderLeaveBehind.mDelegateCellY >= 0) {
532 cellToPoint(mFolderLeaveBehind.mDelegateCellX,
533 mFolderLeaveBehind.mDelegateCellY, mTempLocation);
Adam Cohenefca0272016-02-24 19:19:06 -0800534 canvas.save();
535 canvas.translate(mTempLocation[0], mTempLocation[1]);
Tony Wickhamec6fd6f2023-03-11 02:08:57 +0000536 mFolderLeaveBehind.drawLeaveBehind(canvas, FOLDER_LEAVE_BEHIND_COLOR);
Adam Cohenefca0272016-02-24 19:19:06 -0800537 canvas.restore();
Adam Cohenc51934b2011-07-26 21:07:43 -0700538 }
Adam Cohen65086992020-02-19 08:40:49 -0800539
Adam Cohenf7ca3b42021-02-22 11:03:58 -0800540 if (mVisualizeCells || mVisualizeDropLocation) {
Adam Cohen65086992020-02-19 08:40:49 -0800541 visualizeGrid(canvas);
542 }
543 }
544
Adam Cohenf7ca3b42021-02-22 11:03:58 -0800545 /**
Tony Wickham12784902021-11-03 14:02:10 -0700546 * Returns whether dropping an icon on the given View can create (or add to) a folder.
547 */
548 private boolean canCreateFolder(View child) {
549 return child instanceof DraggableView
550 && ((DraggableView) child).getViewType() == DRAGGABLE_ICON;
551 }
552
553 /**
Adam Cohenf7ca3b42021-02-22 11:03:58 -0800554 * Indicates the progress of the Workspace entering the SpringLoaded state; allows the
555 * CellLayout to update various visuals for this state.
556 *
557 * @param progress
558 */
559 public void setSpringLoadedProgress(float progress) {
560 if (Float.compare(progress, mSpringLoadedProgress) != 0) {
561 mSpringLoadedProgress = progress;
fbarone74256b2023-04-10 14:50:31 -0700562 updateBgAlpha();
Adam Cohenf7ca3b42021-02-22 11:03:58 -0800563 setGridAlpha(progress);
564 }
565 }
566
567 /**
568 * See setSpringLoadedProgress
569 * @return progress
570 */
571 public float getSpringLoadedProgress() {
572 return mSpringLoadedProgress;
573 }
574
Sebastian Franco09589322022-11-02 15:25:58 -0700575 protected void updateBgAlpha() {
Sebastian Franco2986e0b2024-01-25 10:23:39 -0800576 mBackground.setAlpha((int) (mSpringLoadedProgress * 255));
Adam Cohenf7ca3b42021-02-22 11:03:58 -0800577 }
578
579 /**
580 * Set the progress of this page's scroll
581 *
582 * @param progress 0 if the screen is centered, +/-1 if it is to the right / left respectively
583 */
584 public void setScrollProgress(float progress) {
585 if (Float.compare(Math.abs(progress), mScrollProgress) != 0) {
586 mScrollProgress = Math.abs(progress);
fbarone74256b2023-04-10 14:50:31 -0700587 updateBgAlpha();
Adam Cohenf7ca3b42021-02-22 11:03:58 -0800588 }
589 }
590
591 private void setGridAlpha(float gridAlpha) {
592 if (Float.compare(gridAlpha, mGridAlpha) != 0) {
593 mGridAlpha = gridAlpha;
594 invalidate();
595 }
596 }
597
Adam Cohen65086992020-02-19 08:40:49 -0800598 protected void visualizeGrid(Canvas canvas) {
Adam Cohen0c4d2782021-04-29 15:56:13 -0700599 DeviceProfile dp = mActivity.getDeviceProfile();
Alex Chau51da2192022-05-20 13:32:10 +0100600 int paddingX = Math.min((mCellWidth - dp.iconSizePx) / 2, dp.gridVisualizationPaddingX);
601 int paddingY = Math.min((mCellHeight - dp.iconSizePx) / 2, dp.gridVisualizationPaddingY);
Adam Cohen65086992020-02-19 08:40:49 -0800602
Adam Cohenf7ca3b42021-02-22 11:03:58 -0800603 mVisualizeGridPaint.setStrokeWidth(8);
Adam Cohen65086992020-02-19 08:40:49 -0800604
Sebastian Franco0dd5db82023-10-13 11:09:21 -0700605 // This is used for debugging purposes only
Adam Cohenf7ca3b42021-02-22 11:03:58 -0800606 if (mVisualizeCells) {
Sebastian Franco0dd5db82023-10-13 11:09:21 -0700607 int paintAlpha = (int) (120 * mGridAlpha);
608 mVisualizeGridPaint.setColor(ColorUtils.setAlphaComponent(mGridColor, paintAlpha));
Adam Cohenf7ca3b42021-02-22 11:03:58 -0800609 for (int i = 0; i < mCountX; i++) {
610 for (int j = 0; j < mCountY; j++) {
Sebastian Franco0dd5db82023-10-13 11:09:21 -0700611 cellToRect(i, j, 1, 1, mTempOnDrawCellToRect);
612 mVisualizeGridRect.set(mTempOnDrawCellToRect);
613 mVisualizeGridRect.inset(paddingX, paddingY);
Adam Cohenf7ca3b42021-02-22 11:03:58 -0800614 mVisualizeGridPaint.setStyle(Paint.Style.FILL);
615 canvas.drawRoundRect(mVisualizeGridRect, mGridVisualizationRoundingRadius,
616 mGridVisualizationRoundingRadius, mVisualizeGridPaint);
617 }
618 }
619 }
Adam Cohen65086992020-02-19 08:40:49 -0800620
fbarone74256b2023-04-10 14:50:31 -0700621 if (mVisualizeDropLocation) {
Adam Cohenf7ca3b42021-02-22 11:03:58 -0800622 for (int i = 0; i < mDragOutlines.length; i++) {
623 final float alpha = mDragOutlineAlphas[i];
624 if (alpha <= 0) continue;
Sebastian Franco0dd5db82023-10-13 11:09:21 -0700625 CellLayoutLayoutParams params = mDragOutlines[i];
626 cellToRect(params.getCellX(), params.getCellY(), params.cellHSpan, params.cellVSpan,
627 mTempOnDrawCellToRect);
628 mVisualizeGridRect.set(mTempOnDrawCellToRect);
629 mVisualizeGridRect.inset(paddingX, paddingY);
Adam Cohen65086992020-02-19 08:40:49 -0800630
Adam Cohenf7ca3b42021-02-22 11:03:58 -0800631 mVisualizeGridPaint.setAlpha(255);
Adam Cohen65086992020-02-19 08:40:49 -0800632 mVisualizeGridPaint.setStyle(Paint.Style.STROKE);
Adam Cohenf7ca3b42021-02-22 11:03:58 -0800633 mVisualizeGridPaint.setColor(Color.argb((int) (alpha),
634 Color.red(mGridColor), Color.green(mGridColor), Color.blue(mGridColor)));
Adam Cohen65086992020-02-19 08:40:49 -0800635
Sebastian Franco9ea36d42023-09-21 13:56:42 -0700636 canvas.save();
637 canvas.translate(getMarginForGivenCellParams(params), 0);
Adam Cohenf7ca3b42021-02-22 11:03:58 -0800638 canvas.drawRoundRect(mVisualizeGridRect, mGridVisualizationRoundingRadius,
639 mGridVisualizationRoundingRadius, mVisualizeGridPaint);
Sebastian Franco9ea36d42023-09-21 13:56:42 -0700640 canvas.restore();
Adam Cohen65086992020-02-19 08:40:49 -0800641 }
642 }
Adam Cohen69ce2e52011-07-03 19:25:21 -0700643 }
644
Sebastian Franco9ea36d42023-09-21 13:56:42 -0700645 protected float getMarginForGivenCellParams(CellLayoutLayoutParams params) {
646 return 0;
647 }
648
Adam Cohenefca0272016-02-24 19:19:06 -0800649 @Override
650 protected void dispatchDraw(Canvas canvas) {
651 super.dispatchDraw(canvas);
652
Samuel Fufa1e2d0042019-11-18 17:12:46 -0800653 for (int i = 0; i < mDelegatedCellDrawings.size(); i++) {
654 DelegatedCellDrawing bg = mDelegatedCellDrawings.get(i);
655 cellToPoint(bg.mDelegateCellX, bg.mDelegateCellY, mTempLocation);
656 canvas.save();
657 canvas.translate(mTempLocation[0], mTempLocation[1]);
658 bg.drawOverItem(canvas);
659 canvas.restore();
Adam Cohenefca0272016-02-24 19:19:06 -0800660 }
Adam Cohen69ce2e52011-07-03 19:25:21 -0700661 }
662
Samuel Fufa1e2d0042019-11-18 17:12:46 -0800663 /**
664 * Add Delegated cell drawing
665 */
666 public void addDelegatedCellDrawing(DelegatedCellDrawing bg) {
667 mDelegatedCellDrawings.add(bg);
Adam Cohenefca0272016-02-24 19:19:06 -0800668 }
Samuel Fufa1e2d0042019-11-18 17:12:46 -0800669
670 /**
671 * Remove item from DelegatedCellDrawings
672 */
673 public void removeDelegatedCellDrawing(DelegatedCellDrawing bg) {
674 mDelegatedCellDrawings.remove(bg);
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700675 }
676
Adam Cohenc51934b2011-07-26 21:07:43 -0700677 public void setFolderLeaveBehindCell(int x, int y) {
Adam Cohenefca0272016-02-24 19:19:06 -0800678 View child = getChildAt(x, y);
Sunny Goyalab770a12018-11-14 15:17:26 -0800679 mFolderLeaveBehind.setup(getContext(), mActivity, null,
Adam Cohenefca0272016-02-24 19:19:06 -0800680 child.getMeasuredWidth(), child.getPaddingTop());
681
Samuel Fufa1e2d0042019-11-18 17:12:46 -0800682 mFolderLeaveBehind.mDelegateCellX = x;
683 mFolderLeaveBehind.mDelegateCellY = y;
Adam Cohenc51934b2011-07-26 21:07:43 -0700684 invalidate();
685 }
686
687 public void clearFolderLeaveBehind() {
Samuel Fufa1e2d0042019-11-18 17:12:46 -0800688 mFolderLeaveBehind.mDelegateCellX = -1;
689 mFolderLeaveBehind.mDelegateCellY = -1;
Adam Cohenc51934b2011-07-26 21:07:43 -0700690 invalidate();
691 }
692
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700693 @Override
Michael Jurkae6235dd2011-10-04 15:02:05 -0700694 public boolean shouldDelayChildPressedState() {
695 return false;
696 }
697
Adam Cohen1462de32012-07-24 22:34:36 -0700698 public void restoreInstanceState(SparseArray<Parcelable> states) {
Sunny Goyal33a152f2014-07-22 12:13:14 -0700699 try {
700 dispatchRestoreInstanceState(states);
701 } catch (IllegalArgumentException ex) {
Zak Cohen3eeb41d2020-02-14 14:15:13 -0800702 if (FeatureFlags.IS_STUDIO_BUILD) {
Sunny Goyal33a152f2014-07-22 12:13:14 -0700703 throw ex;
704 }
705 // Mismatched viewId / viewType preventing restore. Skip restore on production builds.
706 Log.e(TAG, "Ignoring an error while restoring a view instance state", ex);
707 }
Adam Cohen1462de32012-07-24 22:34:36 -0700708 }
709
Michael Jurkae6235dd2011-10-04 15:02:05 -0700710 @Override
Jeff Sharkey83f111d2009-04-20 21:03:13 -0700711 public void cancelLongPress() {
712 super.cancelLongPress();
713
714 // Cancel long press for all children
715 final int count = getChildCount();
716 for (int i = 0; i < count; i++) {
717 final View child = getChildAt(i);
718 child.cancelLongPress();
719 }
720 }
721
Michael Jurkadee05892010-07-27 10:01:56 -0700722 public void setOnInterceptTouchListener(View.OnTouchListener listener) {
723 mInterceptTouchListener = listener;
724 }
725
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800726 public int getCountX() {
Adam Cohend22015c2010-07-26 22:02:18 -0700727 return mCountX;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800728 }
729
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800730 public int getCountY() {
Adam Cohend22015c2010-07-26 22:02:18 -0700731 return mCountY;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800732 }
733
Sunny Goyalc13403c2016-11-18 23:44:48 -0800734 public boolean acceptsWidget() {
735 return mContainerType == WORKSPACE;
Sunny Goyale9b651e2015-04-24 11:44:51 -0700736 }
737
Sebastian Francod4682992022-10-05 13:03:09 -0500738 /**
739 * Adds the given view to the CellLayout
740 *
741 * @param child view to add.
742 * @param index index of the CellLayout children where to add the view.
743 * @param childId id of the view.
744 * @param params represent the logic of the view on the CellLayout.
745 * @param markCells if the occupied cells should be marked or not
746 * @return if adding the view was successful
747 */
748 public boolean addViewToCellLayout(View child, int index, int childId,
749 CellLayoutLayoutParams params, boolean markCells) {
750 final CellLayoutLayoutParams lp = params;
Winson Chungaafa03c2010-06-11 17:34:16 -0700751
Andrew Flynnde38e422012-05-08 11:22:15 -0700752 // Hotseat icons - remove text
Andrew Flynn0dca1ec2012-02-29 13:33:22 -0800753 if (child instanceof BubbleTextView) {
754 BubbleTextView bubbleChild = (BubbleTextView) child;
Jon Mirandaf1eae802017-10-04 11:23:33 -0700755 bubbleChild.setTextVisibility(mContainerType != HOTSEAT);
Andrew Flynn0dca1ec2012-02-29 13:33:22 -0800756 }
757
Sebastian Francof7654252024-01-18 11:50:50 -0800758 child.setScaleX(DEFAULT_SCALE);
759 child.setScaleY(DEFAULT_SCALE);
Adam Cohen307fe232012-08-16 17:55:58 -0700760
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800761 // Generate an id for each view, this assumes we have at most 256x256 cells
762 // per workspace screen
Sebastian Franco877088e2023-01-03 15:16:22 -0700763 if (lp.getCellX() >= 0 && lp.getCellX() <= mCountX - 1
764 && lp.getCellY() >= 0 && lp.getCellY() <= mCountY - 1) {
Winson Chungaafa03c2010-06-11 17:34:16 -0700765 // If the horizontal or vertical span is set to -1, it is taken to
766 // mean that it spans the extent of the CellLayout
Adam Cohend22015c2010-07-26 22:02:18 -0700767 if (lp.cellHSpan < 0) lp.cellHSpan = mCountX;
768 if (lp.cellVSpan < 0) lp.cellVSpan = mCountY;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800769
Winson Chungaafa03c2010-06-11 17:34:16 -0700770 child.setId(childId);
Tony Wickhama0628cc2015-10-14 15:23:04 -0700771 if (LOGD) {
772 Log.d(TAG, "Adding view to ShortcutsAndWidgetsContainer: " + child);
773 }
Sunny Goyalf7a29e82015-04-24 15:20:43 -0700774 mShortcutsAndWidgets.addView(child, index, lp);
Michael Jurkadee05892010-07-27 10:01:56 -0700775
Michael Jurkaf3ca3ab2010-10-20 17:08:24 -0700776 if (markCells) markCellsAsOccupiedForView(child);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700777
Winson Chungaafa03c2010-06-11 17:34:16 -0700778 return true;
779 }
780 return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800781 }
Michael Jurka3e7c7632010-10-02 16:01:03 -0700782
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800783 @Override
Michael Jurka0280c3b2010-09-17 15:00:07 -0700784 public void removeAllViews() {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -0700785 mOccupied.clear();
Michael Jurkaa52570f2012-03-20 03:18:20 -0700786 mShortcutsAndWidgets.removeAllViews();
Michael Jurka0280c3b2010-09-17 15:00:07 -0700787 }
788
789 @Override
790 public void removeAllViewsInLayout() {
Michael Jurkaa52570f2012-03-20 03:18:20 -0700791 if (mShortcutsAndWidgets.getChildCount() > 0) {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -0700792 mOccupied.clear();
Michael Jurkaa52570f2012-03-20 03:18:20 -0700793 mShortcutsAndWidgets.removeAllViewsInLayout();
Michael Jurka7cfc2822011-08-02 20:19:24 -0700794 }
Michael Jurka0280c3b2010-09-17 15:00:07 -0700795 }
796
797 @Override
798 public void removeView(View view) {
799 markCellsAsUnoccupiedForView(view);
Michael Jurkaa52570f2012-03-20 03:18:20 -0700800 mShortcutsAndWidgets.removeView(view);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700801 }
802
803 @Override
804 public void removeViewAt(int index) {
Michael Jurkaa52570f2012-03-20 03:18:20 -0700805 markCellsAsUnoccupiedForView(mShortcutsAndWidgets.getChildAt(index));
806 mShortcutsAndWidgets.removeViewAt(index);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700807 }
808
809 @Override
810 public void removeViewInLayout(View view) {
811 markCellsAsUnoccupiedForView(view);
Michael Jurkaa52570f2012-03-20 03:18:20 -0700812 mShortcutsAndWidgets.removeViewInLayout(view);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700813 }
814
815 @Override
816 public void removeViews(int start, int count) {
817 for (int i = start; i < start + count; i++) {
Michael Jurkaa52570f2012-03-20 03:18:20 -0700818 markCellsAsUnoccupiedForView(mShortcutsAndWidgets.getChildAt(i));
Michael Jurka0280c3b2010-09-17 15:00:07 -0700819 }
Michael Jurkaa52570f2012-03-20 03:18:20 -0700820 mShortcutsAndWidgets.removeViews(start, count);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700821 }
822
823 @Override
824 public void removeViewsInLayout(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.removeViewsInLayout(start, count);
Michael Jurkaabded662011-03-04 12:06:57 -0800829 }
830
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700831 /**
Winson Chungaafa03c2010-06-11 17:34:16 -0700832 * Given a point, return the cell that strictly encloses that point
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800833 * @param x X coordinate of the point
834 * @param y Y coordinate of the point
835 * @param result Array of 2 ints to hold the x and y coordinate of the cell
836 */
Sunny Goyale9b651e2015-04-24 11:44:51 -0700837 public void pointToCellExact(int x, int y, int[] result) {
Winson Chung4b825dcd2011-06-19 12:41:22 -0700838 final int hStartPadding = getPaddingLeft();
839 final int vStartPadding = getPaddingTop();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800840
Sebastian Francob57c0b22022-06-28 13:54:35 -0700841 result[0] = (x - hStartPadding) / (mCellWidth + mBorderSpace.x);
842 result[1] = (y - vStartPadding) / (mCellHeight + mBorderSpace.y);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800843
Adam Cohend22015c2010-07-26 22:02:18 -0700844 final int xAxis = mCountX;
845 final int yAxis = mCountY;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800846
847 if (result[0] < 0) result[0] = 0;
848 if (result[0] >= xAxis) result[0] = xAxis - 1;
849 if (result[1] < 0) result[1] = 0;
850 if (result[1] >= yAxis) result[1] = yAxis - 1;
851 }
Winson Chungaafa03c2010-06-11 17:34:16 -0700852
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800853 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800854 * Given a cell coordinate, return the point that represents the upper left corner of that cell
Winson Chungaafa03c2010-06-11 17:34:16 -0700855 *
856 * @param cellX X coordinate of the cell
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800857 * @param cellY Y coordinate of the cell
Winson Chungaafa03c2010-06-11 17:34:16 -0700858 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800859 * @param result Array of 2 ints to hold the x and y coordinate of the point
860 */
861 void cellToPoint(int cellX, int cellY, int[] result) {
Jon Miranda228877d2021-02-09 11:05:00 -0500862 cellToRect(cellX, cellY, 1, 1, mTempRect);
863 result[0] = mTempRect.left;
864 result[1] = mTempRect.top;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800865 }
866
Adam Cohene3e27a82011-04-15 12:07:39 -0700867 /**
Adam Cohen482ed822012-03-02 14:15:13 -0800868 * Given a cell coordinate, return the point that represents the center of the cell
Adam Cohene3e27a82011-04-15 12:07:39 -0700869 *
870 * @param cellX X coordinate of the cell
871 * @param cellY Y coordinate of the cell
872 *
873 * @param result Array of 2 ints to hold the x and y coordinate of the point
874 */
875 void cellToCenterPoint(int cellX, int cellY, int[] result) {
Adam Cohen47a876d2012-03-19 13:21:41 -0700876 regionToCenterPoint(cellX, cellY, 1, 1, result);
877 }
878
879 /**
Tony Wickham0ac045f2021-11-03 13:17:02 -0700880 * Given a cell coordinate and span return the point that represents the center of the region
Adam Cohen47a876d2012-03-19 13:21:41 -0700881 *
882 * @param cellX X coordinate of the cell
883 * @param cellY Y coordinate of the cell
884 *
885 * @param result Array of 2 ints to hold the x and y coordinate of the point
886 */
Sebastian Franco45b39b52023-01-10 10:47:46 -0600887 public void regionToCenterPoint(int cellX, int cellY, int spanX, int spanY, int[] result) {
Jon Miranda228877d2021-02-09 11:05:00 -0500888 cellToRect(cellX, cellY, spanX, spanY, mTempRect);
889 result[0] = mTempRect.centerX();
890 result[1] = mTempRect.centerY();
Adam Cohen19f37922012-03-21 11:59:11 -0700891 }
892
Tony Wickham3cfa5ed2021-11-03 13:20:43 -0700893 /**
894 * Returns the distance between the given coordinate and the visual center of the given cell.
895 */
896 public float getDistanceFromWorkspaceCellVisualCenter(float x, float y, int[] cell) {
897 getWorkspaceCellVisualCenter(cell[0], cell[1], mTmpPoint);
Sunny Goyalf7a29e82015-04-24 15:20:43 -0700898 return (float) Math.hypot(x - mTmpPoint[0], y - mTmpPoint[1]);
Adam Cohen482ed822012-03-02 14:15:13 -0800899 }
900
Tony Wickham3cfa5ed2021-11-03 13:20:43 -0700901 private void getWorkspaceCellVisualCenter(int cellX, int cellY, int[] outPoint) {
902 View child = getChildAt(cellX, cellY);
903 if (child instanceof DraggableView) {
904 DraggableView draggableChild = (DraggableView) child;
905 if (draggableChild.getViewType() == DRAGGABLE_ICON) {
906 cellToPoint(cellX, cellY, outPoint);
907 draggableChild.getWorkspaceVisualDragBounds(mTempRect);
908 mTempRect.offset(outPoint[0], outPoint[1]);
909 outPoint[0] = mTempRect.centerX();
910 outPoint[1] = mTempRect.centerY();
911 return;
912 }
913 }
914 cellToCenterPoint(cellX, cellY, outPoint);
915 }
916
Tony Wickham0ac045f2021-11-03 13:17:02 -0700917 /**
918 * Returns the max distance from the center of a cell that can accept a drop to create a folder.
919 */
Tony Wickham12784902021-11-03 14:02:10 -0700920 public float getFolderCreationRadius(int[] targetCell) {
Tony Wickham0ac045f2021-11-03 13:17:02 -0700921 DeviceProfile grid = mActivity.getDeviceProfile();
Tony Wickham12784902021-11-03 14:02:10 -0700922 float iconVisibleRadius = ICON_VISIBLE_AREA_FACTOR * grid.iconSizePx / 2;
923 // Halfway between reorder radius and icon.
Sebastian Franco6e1024e2022-07-29 13:46:49 -0700924 return (getReorderRadius(targetCell, 1, 1) + iconVisibleRadius) / 2;
Tony Wickham12784902021-11-03 14:02:10 -0700925 }
926
927 /**
928 * Returns the max distance from the center of a cell that will start to reorder on drag over.
929 */
Sebastian Franco6e1024e2022-07-29 13:46:49 -0700930 public float getReorderRadius(int[] targetCell, int spanX, int spanY) {
Tony Wickham12784902021-11-03 14:02:10 -0700931 int[] centerPoint = mTmpPoint;
932 getWorkspaceCellVisualCenter(targetCell[0], targetCell[1], centerPoint);
933
934 Rect cellBoundsWithSpacing = mTempRect;
Sebastian Franco6e1024e2022-07-29 13:46:49 -0700935 cellToRect(targetCell[0], targetCell[1], spanX, spanY, cellBoundsWithSpacing);
Tony Wickham12784902021-11-03 14:02:10 -0700936 cellBoundsWithSpacing.inset(-mBorderSpace.x / 2, -mBorderSpace.y / 2);
937
Sebastian Francoc8392ea2022-10-28 16:38:37 -0700938 if (canCreateFolder(getChildAt(targetCell[0], targetCell[1])) && spanX == 1 && spanY == 1) {
Tony Wickham12784902021-11-03 14:02:10 -0700939 // Take only the circle in the smaller dimension, to ensure we don't start reordering
940 // too soon before accepting a folder drop.
941 int minRadius = centerPoint[0] - cellBoundsWithSpacing.left;
942 minRadius = Math.min(minRadius, centerPoint[1] - cellBoundsWithSpacing.top);
943 minRadius = Math.min(minRadius, cellBoundsWithSpacing.right - centerPoint[0]);
944 minRadius = Math.min(minRadius, cellBoundsWithSpacing.bottom - centerPoint[1]);
945 return minRadius;
946 }
947 // Take up the entire cell, including space between this cell and the adjacent ones.
Sebastian Francoc8392ea2022-10-28 16:38:37 -0700948 // Multiply by span to scale radius
949 return (float) Math.hypot(spanX * cellBoundsWithSpacing.width() / 2f,
950 spanY * cellBoundsWithSpacing.height() / 2f);
Tony Wickham0ac045f2021-11-03 13:17:02 -0700951 }
952
Adam Cohenf9c184a2016-01-15 16:47:43 -0800953 public int getCellWidth() {
Romain Guy84f296c2009-11-04 15:00:44 -0800954 return mCellWidth;
955 }
956
Sunny Goyal0b754e52017-08-07 07:42:45 -0700957 public int getCellHeight() {
Romain Guy84f296c2009-11-04 15:00:44 -0800958 return mCellHeight;
959 }
960
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700961 public void setFixedSize(int width, int height) {
962 mFixedWidth = width;
963 mFixedHeight = height;
964 }
965
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800966 @Override
967 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800968 int widthSpecMode = MeasureSpec.getMode(widthMeasureSpec);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800969 int heightSpecMode = MeasureSpec.getMode(heightMeasureSpec);
Winson Chung5f8afe62013-08-12 16:19:28 -0700970 int widthSize = MeasureSpec.getSize(widthMeasureSpec);
971 int heightSize = MeasureSpec.getSize(heightMeasureSpec);
Winson Chung2d75f122013-09-23 16:53:31 -0700972 int childWidthSize = widthSize - (getPaddingLeft() + getPaddingRight());
973 int childHeightSize = heightSize - (getPaddingTop() + getPaddingBottom());
Sunny Goyalae6e3182019-04-30 12:04:37 -0700974
Winson Chung11a1a532013-09-13 11:14:45 -0700975 if (mFixedCellWidth < 0 || mFixedCellHeight < 0) {
Thales Lima78d00ad2021-09-30 11:29:06 +0100976 int cw = DeviceProfile.calculateCellWidth(childWidthSize, mBorderSpace.x,
Jon Miranda228877d2021-02-09 11:05:00 -0500977 mCountX);
Thales Lima78d00ad2021-09-30 11:29:06 +0100978 int ch = DeviceProfile.calculateCellHeight(childHeightSize, mBorderSpace.y,
Jon Miranda228877d2021-02-09 11:05:00 -0500979 mCountY);
Winson Chung11a1a532013-09-13 11:14:45 -0700980 if (cw != mCellWidth || ch != mCellHeight) {
981 mCellWidth = cw;
982 mCellHeight = ch;
Jon Miranda228877d2021-02-09 11:05:00 -0500983 mShortcutsAndWidgets.setCellDimensions(mCellWidth, mCellHeight, mCountX, mCountY,
Thales Lima78d00ad2021-09-30 11:29:06 +0100984 mBorderSpace);
Winson Chung11a1a532013-09-13 11:14:45 -0700985 }
Winson Chung5f8afe62013-08-12 16:19:28 -0700986 }
Winson Chungaafa03c2010-06-11 17:34:16 -0700987
Winson Chung2d75f122013-09-23 16:53:31 -0700988 int newWidth = childWidthSize;
989 int newHeight = childHeightSize;
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700990 if (mFixedWidth > 0 && mFixedHeight > 0) {
991 newWidth = mFixedWidth;
992 newHeight = mFixedHeight;
993 } else if (widthSpecMode == MeasureSpec.UNSPECIFIED || heightSpecMode == MeasureSpec.UNSPECIFIED) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800994 throw new RuntimeException("CellLayout cannot have UNSPECIFIED dimensions");
995 }
996
Sunny Goyal4fe5a372015-05-14 19:55:10 -0700997 mShortcutsAndWidgets.measure(
998 MeasureSpec.makeMeasureSpec(newWidth, MeasureSpec.EXACTLY),
999 MeasureSpec.makeMeasureSpec(newHeight, MeasureSpec.EXACTLY));
1000
1001 int maxWidth = mShortcutsAndWidgets.getMeasuredWidth();
1002 int maxHeight = mShortcutsAndWidgets.getMeasuredHeight();
Winson Chung2d75f122013-09-23 16:53:31 -07001003 if (mFixedWidth > 0 && mFixedHeight > 0) {
1004 setMeasuredDimension(maxWidth, maxHeight);
1005 } else {
1006 setMeasuredDimension(widthSize, heightSize);
1007 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001008 }
1009
1010 @Override
Michael Jurka28750fb2010-09-24 17:43:49 -07001011 protected void onLayout(boolean changed, int l, int t, int r, int b) {
Tony Wickham26b01422015-11-10 14:44:32 -08001012 int left = getPaddingLeft();
Sunny Goyal7ce471b2017-08-02 03:37:39 -07001013 left += (int) Math.ceil(getUnusedHorizontalSpace() / 2f);
Sunny Goyal7c786f72016-06-01 14:08:21 -07001014 int right = r - l - getPaddingRight();
Sunny Goyal7ce471b2017-08-02 03:37:39 -07001015 right -= (int) Math.ceil(getUnusedHorizontalSpace() / 2f);
Sunny Goyal7c786f72016-06-01 14:08:21 -07001016
Winson Chung38848ca2013-10-08 12:03:44 -07001017 int top = getPaddingTop();
Sunny Goyal7c786f72016-06-01 14:08:21 -07001018 int bottom = b - t - getPaddingBottom();
Sunny Goyal4fe5a372015-05-14 19:55:10 -07001019
Sunny Goyal7c786f72016-06-01 14:08:21 -07001020 // Expand the background drawing bounds by the padding baked into the background drawable
1021 mBackground.getPadding(mTempRect);
1022 mBackground.setBounds(
Jon Miranda28032002017-07-13 16:18:56 -07001023 left - mTempRect.left - getPaddingLeft(),
1024 top - mTempRect.top - getPaddingTop(),
1025 right + mTempRect.right + getPaddingRight(),
1026 bottom + mTempRect.bottom + getPaddingBottom());
Sunny Goyalae6e3182019-04-30 12:04:37 -07001027
Sunny Goyalc4d32012020-04-03 17:10:11 -07001028 mShortcutsAndWidgets.layout(left, top, right, bottom);
Sunny Goyal7c786f72016-06-01 14:08:21 -07001029 }
1030
Tony Wickhama501d492015-11-03 18:05:01 -08001031 /**
1032 * Returns the amount of space left over after subtracting padding and cells. This space will be
1033 * 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 -05001034 * width in {@link DeviceProfile#calculateCellWidth(int, int, int)}.
Tony Wickhama501d492015-11-03 18:05:01 -08001035 */
1036 public int getUnusedHorizontalSpace() {
Jon Miranda228877d2021-02-09 11:05:00 -05001037 return getMeasuredWidth() - getPaddingLeft() - getPaddingRight() - (mCountX * mCellWidth)
Thales Lima78d00ad2021-09-30 11:29:06 +01001038 - ((mCountX - 1) * mBorderSpace.x);
Tony Wickhama501d492015-11-03 18:05:01 -08001039 }
1040
Sunny Goyal2805e632015-05-20 15:35:32 -07001041 @Override
1042 protected boolean verifyDrawable(Drawable who) {
Sunny Goyal7ce471b2017-08-02 03:37:39 -07001043 return super.verifyDrawable(who) || (who == mBackground);
Sunny Goyal2805e632015-05-20 15:35:32 -07001044 }
1045
Michael Jurkaa52570f2012-03-20 03:18:20 -07001046 public ShortcutAndWidgetContainer getShortcutsAndWidgets() {
Sunny Goyaldcbcc862014-08-12 15:58:36 -07001047 return mShortcutsAndWidgets;
Michael Jurkaa52570f2012-03-20 03:18:20 -07001048 }
1049
Jon Miranda228877d2021-02-09 11:05:00 -05001050 public View getChildAt(int cellX, int cellY) {
1051 return mShortcutsAndWidgets.getChildAt(cellX, cellY);
Patrick Dubroy440c3602010-07-13 17:50:32 -07001052 }
1053
Adam Cohen76fc0852011-06-17 13:26:23 -07001054 public boolean animateChildToPosition(final View child, int cellX, int cellY, int duration,
Adam Cohen482ed822012-03-02 14:15:13 -08001055 int delay, boolean permanent, boolean adjustOccupied) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07001056 ShortcutAndWidgetContainer clc = getShortcutsAndWidgets();
Adam Cohen482ed822012-03-02 14:15:13 -08001057
Adam Cohen1d13c0b2020-04-21 16:29:12 -07001058 if (clc.indexOfChild(child) != -1 && (child instanceof Reorderable)) {
Sebastian Francod4682992022-10-05 13:03:09 -05001059 final CellLayoutLayoutParams lp = (CellLayoutLayoutParams) child.getLayoutParams();
Adam Cohenbfbfd262011-06-13 16:55:12 -07001060 final ItemInfo info = (ItemInfo) child.getTag();
Adam Cohen1d13c0b2020-04-21 16:29:12 -07001061 final Reorderable item = (Reorderable) child;
Adam Cohenbfbfd262011-06-13 16:55:12 -07001062
1063 // We cancel any existing animations
1064 if (mReorderAnimators.containsKey(lp)) {
1065 mReorderAnimators.get(lp).cancel();
1066 mReorderAnimators.remove(lp);
1067 }
1068
Adam Cohen1d13c0b2020-04-21 16:29:12 -07001069
Adam Cohen482ed822012-03-02 14:15:13 -08001070 if (adjustOccupied) {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001071 GridOccupancy occupied = permanent ? mOccupied : mTmpOccupied;
Sebastian Franco877088e2023-01-03 15:16:22 -07001072 occupied.markCells(lp.getCellX(), lp.getCellY(), lp.cellHSpan, lp.cellVSpan, false);
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001073 occupied.markCells(cellX, cellY, lp.cellHSpan, lp.cellVSpan, true);
Adam Cohen482ed822012-03-02 14:15:13 -08001074 }
Adam Cohen1d13c0b2020-04-21 16:29:12 -07001075
1076 // Compute the new x and y position based on the new cellX and cellY
1077 // We leverage the actual layout logic in the layout params and hence need to modify
1078 // state and revert that state.
1079 final int oldX = lp.x;
1080 final int oldY = lp.y;
Adam Cohenbfbfd262011-06-13 16:55:12 -07001081 lp.isLockedToGrid = true;
Adam Cohen482ed822012-03-02 14:15:13 -08001082 if (permanent) {
Sunny Goyal669b71f2023-01-27 14:37:07 -08001083 lp.setCellX(cellX);
1084 lp.setCellY(cellY);
Adam Cohen482ed822012-03-02 14:15:13 -08001085 } else {
Sebastian Franco877088e2023-01-03 15:16:22 -07001086 lp.setTmpCellX(cellX);
1087 lp.setTmpCellY(cellY);
Adam Cohen482ed822012-03-02 14:15:13 -08001088 }
Jon Mirandae96798e2016-12-07 12:10:44 -08001089 clc.setupLp(child);
Adam Cohen482ed822012-03-02 14:15:13 -08001090 final int newX = lp.x;
1091 final int newY = lp.y;
Adam Cohen76fc0852011-06-17 13:26:23 -07001092 lp.x = oldX;
1093 lp.y = oldY;
Adam Cohen1d13c0b2020-04-21 16:29:12 -07001094 lp.isLockedToGrid = false;
1095 // End compute new x and y
1096
Sunny Goyal82dfc152023-02-24 16:50:09 -08001097 MultiTranslateDelegate mtd = item.getTranslateDelegate();
1098 float initPreviewOffsetX = mtd.getTranslationX(INDEX_REORDER_PREVIEW_OFFSET).getValue();
1099 float initPreviewOffsetY = mtd.getTranslationY(INDEX_REORDER_PREVIEW_OFFSET).getValue();
Adam Cohen1d13c0b2020-04-21 16:29:12 -07001100 final float finalPreviewOffsetX = newX - oldX;
1101 final float finalPreviewOffsetY = newY - oldY;
1102
Adam Cohen482ed822012-03-02 14:15:13 -08001103 // Exit early if we're not actually moving the view
Adam Cohen1d13c0b2020-04-21 16:29:12 -07001104 if (finalPreviewOffsetX == 0 && finalPreviewOffsetY == 0
1105 && initPreviewOffsetX == 0 && initPreviewOffsetY == 0) {
Adam Cohen482ed822012-03-02 14:15:13 -08001106 lp.isLockedToGrid = true;
1107 return true;
1108 }
1109
Sunny Goyal849c6a22018-08-08 16:33:46 -07001110 ValueAnimator va = ValueAnimator.ofFloat(0f, 1f);
Adam Cohen482ed822012-03-02 14:15:13 -08001111 va.setDuration(duration);
1112 mReorderAnimators.put(lp, va);
1113
1114 va.addUpdateListener(new AnimatorUpdateListener() {
1115 @Override
Adam Cohenbfbfd262011-06-13 16:55:12 -07001116 public void onAnimationUpdate(ValueAnimator animation) {
Jon Mirandae96798e2016-12-07 12:10:44 -08001117 float r = (Float) animation.getAnimatedValue();
Adam Cohen1d13c0b2020-04-21 16:29:12 -07001118 float x = (1 - r) * initPreviewOffsetX + r * finalPreviewOffsetX;
1119 float y = (1 - r) * initPreviewOffsetY + r * finalPreviewOffsetY;
Sunny Goyal82dfc152023-02-24 16:50:09 -08001120 item.getTranslateDelegate().setTranslation(INDEX_REORDER_PREVIEW_OFFSET, x, y);
Adam Cohenbfbfd262011-06-13 16:55:12 -07001121 }
1122 });
Adam Cohen482ed822012-03-02 14:15:13 -08001123 va.addListener(new AnimatorListenerAdapter() {
Adam Cohenbfbfd262011-06-13 16:55:12 -07001124 boolean cancelled = false;
1125 public void onAnimationEnd(Animator animation) {
1126 // If the animation was cancelled, it means that another animation
1127 // has interrupted this one, and we don't want to lock the item into
1128 // place just yet.
1129 if (!cancelled) {
1130 lp.isLockedToGrid = true;
Sunny Goyal82dfc152023-02-24 16:50:09 -08001131 item.getTranslateDelegate()
1132 .setTranslation(INDEX_REORDER_PREVIEW_OFFSET, 0, 0);
Adam Cohen482ed822012-03-02 14:15:13 -08001133 child.requestLayout();
Adam Cohenbfbfd262011-06-13 16:55:12 -07001134 }
1135 if (mReorderAnimators.containsKey(lp)) {
1136 mReorderAnimators.remove(lp);
1137 }
1138 }
1139 public void onAnimationCancel(Animator animation) {
1140 cancelled = true;
1141 }
1142 });
Adam Cohen482ed822012-03-02 14:15:13 -08001143 va.setStartDelay(delay);
1144 va.start();
Adam Cohenbfbfd262011-06-13 16:55:12 -07001145 return true;
1146 }
1147 return false;
1148 }
1149
Adam Cohenf7ca3b42021-02-22 11:03:58 -08001150 void visualizeDropLocation(int cellX, int cellY, int spanX, int spanY,
1151 DropTarget.DragObject dragObject) {
1152 if (mDragCell[0] != cellX || mDragCell[1] != cellY || mDragCellSpan[0] != spanX
1153 || mDragCellSpan[1] != spanY) {
Adam Cohen482ed822012-03-02 14:15:13 -08001154 mDragCell[0] = cellX;
1155 mDragCell[1] = cellY;
Adam Cohenf7ca3b42021-02-22 11:03:58 -08001156 mDragCellSpan[0] = spanX;
1157 mDragCellSpan[1] = spanY;
Steven Ng30dd1d62021-03-15 21:45:49 +00001158
Joe Onorato4be866d2010-10-10 11:26:02 -07001159 final int oldIndex = mDragOutlineCurrent;
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -07001160 mDragOutlineAnims[oldIndex].animateOut();
1161 mDragOutlineCurrent = (oldIndex + 1) % mDragOutlines.length;
Sunny Goyal106bf642015-07-16 12:18:06 -07001162
Sebastian Francod4682992022-10-05 13:03:09 -05001163 CellLayoutLayoutParams cell = mDragOutlines[mDragOutlineCurrent];
Sebastian Franco877088e2023-01-03 15:16:22 -07001164 cell.setCellX(cellX);
1165 cell.setCellY(cellY);
Adam Cohenf7ca3b42021-02-22 11:03:58 -08001166 cell.cellHSpan = spanX;
1167 cell.cellVSpan = spanY;
Adam Cohen65086992020-02-19 08:40:49 -08001168
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -07001169 mDragOutlineAnims[mDragOutlineCurrent].animateIn();
Adam Cohenf7ca3b42021-02-22 11:03:58 -08001170 invalidate();
Sunny Goyale78e3d72015-09-24 11:23:31 -07001171
1172 if (dragObject.stateAnnouncer != null) {
Sunny Goyalc13403c2016-11-18 23:44:48 -08001173 dragObject.stateAnnouncer.announce(getItemMoveDescription(cellX, cellY));
Sunny Goyale78e3d72015-09-24 11:23:31 -07001174 }
Adam Cohenf7ca3b42021-02-22 11:03:58 -08001175
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001176 }
1177 }
1178
Sunny Goyal726bee72018-03-05 12:54:24 -08001179 @SuppressLint("StringFormatMatches")
Sunny Goyalc13403c2016-11-18 23:44:48 -08001180 public String getItemMoveDescription(int cellX, int cellY) {
1181 if (mContainerType == HOTSEAT) {
1182 return getContext().getString(R.string.move_to_hotseat_position,
1183 Math.max(cellX, cellY) + 1);
1184 } else {
Andras Kloczl2dacbee2022-02-21 16:53:28 +00001185 int row = cellY + 1;
Sebastian Franco2986e0b2024-01-25 10:23:39 -08001186 int col = Utilities.isRtl(getResources()) ? mCountX - cellX : cellX + 1;
1187 int panelCount = mCellLayoutContainer.getPanelCount();
1188 int pageIndex = mCellLayoutContainer.getCellLayoutIndex(this);
Andras Kloczl2dacbee2022-02-21 16:53:28 +00001189 if (panelCount > 1) {
1190 // Increment the column if the target is on the right side of a two panel home
Andras Kloczl2dacbee2022-02-21 16:53:28 +00001191 col += (pageIndex % panelCount) * mCountX;
1192 }
Sebastian Franco930531f2022-06-16 16:49:11 -07001193 return getContext().getString(R.string.move_to_empty_cell_description, row, col,
Sebastian Franco2986e0b2024-01-25 10:23:39 -08001194 mCellLayoutContainer.getPageDescription(pageIndex));
Sunny Goyalc13403c2016-11-18 23:44:48 -08001195 }
1196 }
1197
Adam Cohene0310962011-04-18 16:15:31 -07001198 public void clearDragOutlines() {
1199 final int oldIndex = mDragOutlineCurrent;
1200 mDragOutlineAnims[oldIndex].animateOut();
Adam Cohend41fbf52012-02-16 23:53:59 -08001201 mDragCell[0] = mDragCell[1] = -1;
Adam Cohene0310962011-04-18 16:15:31 -07001202 }
1203
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001204 /**
Jeff Sharkey70864282009-04-07 21:08:40 -07001205 * Find a vacant area that will fit the given bounds nearest the requested
1206 * cell location. Uses Euclidean distance to score multiple vacant areas.
Winson Chungaafa03c2010-06-11 17:34:16 -07001207 *
Romain Guy51afc022009-05-04 18:03:43 -07001208 * @param pixelX The X location at which you want to search for a vacant area.
1209 * @param pixelY The Y location at which you want to search for a vacant area.
Adam Cohend41fbf52012-02-16 23:53:59 -08001210 * @param minSpanX The minimum horizontal span required
1211 * @param minSpanY The minimum vertical span required
1212 * @param spanX Horizontal span of the object.
1213 * @param spanY Vertical span of the object.
1214 * @param result Array in which to place the result, or null (in which case a new array will
1215 * be allocated)
1216 * @return The X, Y cell of a vacant area that can contain this object,
1217 * nearest the requested location.
1218 */
Sebastian Francoe4c03452022-12-27 14:50:02 -06001219 public int[] findNearestVacantArea(int pixelX, int pixelY, int minSpanX, int minSpanY,
1220 int spanX, int spanY, int[] result, int[] resultSpan) {
Sebastian Francob57c0b22022-06-28 13:54:35 -07001221 return findNearestArea(pixelX, pixelY, minSpanX, minSpanY, spanX, spanY, false,
Adam Cohend41fbf52012-02-16 23:53:59 -08001222 result, resultSpan);
1223 }
1224
Adam Cohend41fbf52012-02-16 23:53:59 -08001225 /**
1226 * Find a vacant area that will fit the given bounds nearest the requested
1227 * cell location. Uses Euclidean distance to score multiple vacant areas.
Sebastian Francob57c0b22022-06-28 13:54:35 -07001228 * @param relativeXPos The X location relative to the Cell layout at which you want to search
1229 * for a vacant area.
1230 * @param relativeYPos The Y location relative to the Cell layout at which you want to search
1231 * for a vacant area.
Adam Cohend41fbf52012-02-16 23:53:59 -08001232 * @param minSpanX The minimum horizontal span required
1233 * @param minSpanY The minimum vertical span required
1234 * @param spanX Horizontal span of the object.
1235 * @param spanY Vertical span of the object.
1236 * @param ignoreOccupied If true, the result can be an occupied cell
1237 * @param result Array in which to place the result, or null (in which case a new array will
1238 * be allocated)
1239 * @return The X, Y cell of a vacant area that can contain this object,
1240 * nearest the requested location.
1241 */
Sebastian Franco96c46e72023-05-08 10:04:44 -06001242 protected int[] findNearestArea(int relativeXPos, int relativeYPos, int minSpanX, int minSpanY,
Sebastian Francob57c0b22022-06-28 13:54:35 -07001243 int spanX, int spanY, boolean ignoreOccupied, int[] result, int[] resultSpan) {
Sebastian Francob57c0b22022-06-28 13:54:35 -07001244 // For items with a spanX / spanY > 1, the passed in point (relativeXPos, relativeYPos)
1245 // corresponds to the center of the item, but we are searching based on the top-left cell,
1246 // so we translate the point over to correspond to the top-left.
1247 relativeXPos = (int) (relativeXPos - (mCellWidth + mBorderSpace.x) * (spanX - 1) / 2f);
1248 relativeYPos = (int) (relativeYPos - (mCellHeight + mBorderSpace.y) * (spanY - 1) / 2f);
Adam Cohene3e27a82011-04-15 12:07:39 -07001249
Jeff Sharkey70864282009-04-07 21:08:40 -07001250 // Keep track of best-scoring drop area
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001251 final int[] bestXY = result != null ? result : new int[2];
Jeff Sharkey70864282009-04-07 21:08:40 -07001252 double bestDistance = Double.MAX_VALUE;
Adam Cohend41fbf52012-02-16 23:53:59 -08001253 final Rect bestRect = new Rect(-1, -1, -1, -1);
Rajeev Kumar9962dbe2017-06-12 12:16:20 -07001254 final Stack<Rect> validRegions = new Stack<>();
Winson Chungaafa03c2010-06-11 17:34:16 -07001255
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001256 final int countX = mCountX;
1257 final int countY = mCountY;
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001258
Adam Cohend41fbf52012-02-16 23:53:59 -08001259 if (minSpanX <= 0 || minSpanY <= 0 || spanX <= 0 || spanY <= 0 ||
1260 spanX < minSpanX || spanY < minSpanY) {
1261 return bestXY;
1262 }
1263
1264 for (int y = 0; y < countY - (minSpanY - 1); y++) {
Michael Jurkac28de512010-08-13 11:27:44 -07001265 inner:
Adam Cohend41fbf52012-02-16 23:53:59 -08001266 for (int x = 0; x < countX - (minSpanX - 1); x++) {
1267 int ySize = -1;
1268 int xSize = -1;
Sebastian Francob57c0b22022-06-28 13:54:35 -07001269 if (!ignoreOccupied) {
Adam Cohend41fbf52012-02-16 23:53:59 -08001270 // First, let's see if this thing fits anywhere
1271 for (int i = 0; i < minSpanX; i++) {
1272 for (int j = 0; j < minSpanY; j++) {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001273 if (mOccupied.cells[x + i][y + j]) {
Adam Cohendf035382011-04-11 17:22:04 -07001274 continue inner;
1275 }
Michael Jurkac28de512010-08-13 11:27:44 -07001276 }
1277 }
Adam Cohend41fbf52012-02-16 23:53:59 -08001278 xSize = minSpanX;
1279 ySize = minSpanY;
1280
1281 // We know that the item will fit at _some_ acceptable size, now let's see
1282 // how big we can make it. We'll alternate between incrementing x and y spans
1283 // until we hit a limit.
1284 boolean incX = true;
1285 boolean hitMaxX = xSize >= spanX;
1286 boolean hitMaxY = ySize >= spanY;
1287 while (!(hitMaxX && hitMaxY)) {
1288 if (incX && !hitMaxX) {
1289 for (int j = 0; j < ySize; j++) {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001290 if (x + xSize > countX -1 || mOccupied.cells[x + xSize][y + j]) {
Adam Cohend41fbf52012-02-16 23:53:59 -08001291 // We can't move out horizontally
1292 hitMaxX = true;
1293 }
1294 }
1295 if (!hitMaxX) {
1296 xSize++;
1297 }
1298 } else if (!hitMaxY) {
1299 for (int i = 0; i < xSize; i++) {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001300 if (y + ySize > countY - 1 || mOccupied.cells[x + i][y + ySize]) {
Adam Cohend41fbf52012-02-16 23:53:59 -08001301 // We can't move out vertically
1302 hitMaxY = true;
1303 }
1304 }
1305 if (!hitMaxY) {
1306 ySize++;
1307 }
1308 }
1309 hitMaxX |= xSize >= spanX;
1310 hitMaxY |= ySize >= spanY;
1311 incX = !incX;
1312 }
Michael Jurkac28de512010-08-13 11:27:44 -07001313 }
Sunny Goyal2805e632015-05-20 15:35:32 -07001314 final int[] cellXY = mTmpPoint;
Adam Cohene3e27a82011-04-15 12:07:39 -07001315 cellToCenterPoint(x, y, cellXY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001316
Adam Cohend41fbf52012-02-16 23:53:59 -08001317 // We verify that the current rect is not a sub-rect of any of our previous
1318 // candidates. In this case, the current rect is disqualified in favour of the
1319 // containing rect.
Sebastian Franco4a922672022-10-27 16:42:24 -07001320 Rect currentRect = new Rect(x, y, x + xSize, y + ySize);
Adam Cohend41fbf52012-02-16 23:53:59 -08001321 boolean contained = false;
1322 for (Rect r : validRegions) {
1323 if (r.contains(currentRect)) {
1324 contained = true;
1325 break;
1326 }
1327 }
1328 validRegions.push(currentRect);
Sebastian Francob57c0b22022-06-28 13:54:35 -07001329 double distance = Math.hypot(cellXY[0] - relativeXPos, cellXY[1] - relativeYPos);
Adam Cohen482ed822012-03-02 14:15:13 -08001330
Adam Cohend41fbf52012-02-16 23:53:59 -08001331 if ((distance <= bestDistance && !contained) ||
1332 currentRect.contains(bestRect)) {
Michael Jurkac28de512010-08-13 11:27:44 -07001333 bestDistance = distance;
1334 bestXY[0] = x;
1335 bestXY[1] = y;
Adam Cohend41fbf52012-02-16 23:53:59 -08001336 if (resultSpan != null) {
1337 resultSpan[0] = xSize;
1338 resultSpan[1] = ySize;
1339 }
1340 bestRect.set(currentRect);
Michael Jurkac28de512010-08-13 11:27:44 -07001341 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001342 }
1343 }
1344
Adam Cohenc0dcf592011-06-01 15:30:43 -07001345 // Return -1, -1 if no suitable location found
1346 if (bestDistance == Double.MAX_VALUE) {
1347 bestXY[0] = -1;
1348 bestXY[1] = -1;
Jeff Sharkey70864282009-04-07 21:08:40 -07001349 }
Adam Cohenc0dcf592011-06-01 15:30:43 -07001350 return bestXY;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001351 }
Winson Chungaafa03c2010-06-11 17:34:16 -07001352
Sebastian Francoe4c03452022-12-27 14:50:02 -06001353 public GridOccupancy getOccupied() {
1354 return mOccupied;
1355 }
1356
Adam Cohen482ed822012-03-02 14:15:13 -08001357 private void copySolutionToTempState(ItemConfiguration solution, View dragView) {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001358 mTmpOccupied.clear();
Adam Cohen482ed822012-03-02 14:15:13 -08001359
Michael Jurkaa52570f2012-03-20 03:18:20 -07001360 int childCount = mShortcutsAndWidgets.getChildCount();
Adam Cohen482ed822012-03-02 14:15:13 -08001361 for (int i = 0; i < childCount; i++) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07001362 View child = mShortcutsAndWidgets.getChildAt(i);
Adam Cohen482ed822012-03-02 14:15:13 -08001363 if (child == dragView) continue;
Sebastian Francod4682992022-10-05 13:03:09 -05001364 CellLayoutLayoutParams lp = (CellLayoutLayoutParams) child.getLayoutParams();
Adam Cohen8baab352012-03-20 17:39:21 -07001365 CellAndSpan c = solution.map.get(child);
1366 if (c != null) {
Sebastian Franco877088e2023-01-03 15:16:22 -07001367 lp.setTmpCellX(c.cellX);
1368 lp.setTmpCellY(c.cellY);
Adam Cohen8baab352012-03-20 17:39:21 -07001369 lp.cellHSpan = c.spanX;
1370 lp.cellVSpan = c.spanY;
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001371 mTmpOccupied.markCells(c, true);
Adam Cohen482ed822012-03-02 14:15:13 -08001372 }
1373 }
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001374 mTmpOccupied.markCells(solution, true);
Adam Cohen482ed822012-03-02 14:15:13 -08001375 }
1376
1377 private void animateItemsToSolution(ItemConfiguration solution, View dragView, boolean
1378 commitDragView) {
1379
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001380 GridOccupancy occupied = DESTRUCTIVE_REORDER ? mOccupied : mTmpOccupied;
1381 occupied.clear();
Adam Cohen482ed822012-03-02 14:15:13 -08001382
Michael Jurkaa52570f2012-03-20 03:18:20 -07001383 int childCount = mShortcutsAndWidgets.getChildCount();
Adam Cohen482ed822012-03-02 14:15:13 -08001384 for (int i = 0; i < childCount; i++) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07001385 View child = mShortcutsAndWidgets.getChildAt(i);
Adam Cohen482ed822012-03-02 14:15:13 -08001386 if (child == dragView) continue;
Adam Cohen8baab352012-03-20 17:39:21 -07001387 CellAndSpan c = solution.map.get(child);
1388 if (c != null) {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001389 animateChildToPosition(child, c.cellX, c.cellY, REORDER_ANIMATION_DURATION, 0,
Adam Cohen19f37922012-03-21 11:59:11 -07001390 DESTRUCTIVE_REORDER, false);
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001391 occupied.markCells(c, true);
Adam Cohen482ed822012-03-02 14:15:13 -08001392 }
1393 }
1394 if (commitDragView) {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001395 occupied.markCells(solution, true);
Adam Cohen482ed822012-03-02 14:15:13 -08001396 }
1397 }
1398
Adam Cohenfa3c58f2013-12-06 16:10:55 -08001399
1400 // This method starts or changes the reorder preview animations
1401 private void beginOrAdjustReorderPreviewAnimations(ItemConfiguration solution,
Adam Cohen65086992020-02-19 08:40:49 -08001402 View dragView, int mode) {
Adam Cohen19f37922012-03-21 11:59:11 -07001403 int childCount = mShortcutsAndWidgets.getChildCount();
Adam Cohen19f37922012-03-21 11:59:11 -07001404 for (int i = 0; i < childCount; i++) {
1405 View child = mShortcutsAndWidgets.getChildAt(i);
1406 if (child == dragView) continue;
1407 CellAndSpan c = solution.map.get(child);
Sebastian Franco5f0af4f2023-11-21 10:45:45 -06001408 boolean skip = mode == ReorderPreviewAnimation.MODE_HINT
1409 && !solution.intersectingViews.contains(child);
Adam Cohend9162062020-03-24 16:35:35 -07001410
Sebastian Francod4682992022-10-05 13:03:09 -05001411 CellLayoutLayoutParams lp = (CellLayoutLayoutParams) child.getLayoutParams();
Adam Cohend9162062020-03-24 16:35:35 -07001412 if (c != null && !skip && (child instanceof Reorderable)) {
Sebastian Francof7654252024-01-18 11:50:50 -08001413 ReorderPreviewAnimation rha = new ReorderPreviewAnimation(child, mode,
1414 lp.getCellX(), lp.getCellY(), c.cellX, c.cellY, c.spanX, c.spanY,
1415 mReorderPreviewAnimationMagnitude, this, mShakeAnimators);
Adam Cohend024f982012-05-23 18:26:45 -07001416 rha.animate();
Adam Cohen19f37922012-03-21 11:59:11 -07001417 }
1418 }
1419 }
1420
Adam Cohenfa3c58f2013-12-06 16:10:55 -08001421 private void completeAndClearReorderPreviewAnimations() {
1422 for (ReorderPreviewAnimation a: mShakeAnimators.values()) {
Adam Cohend9162062020-03-24 16:35:35 -07001423 a.finishAnimation();
Adam Cohen19f37922012-03-21 11:59:11 -07001424 }
1425 mShakeAnimators.clear();
1426 }
1427
Sunny Goyal711c5962021-06-23 12:36:18 -07001428 private void commitTempPlacement(View dragView) {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001429 mTmpOccupied.copyTo(mOccupied);
Sunny Goyalaa8ef112015-06-12 20:04:41 -07001430
Sebastian Franco2986e0b2024-01-25 10:23:39 -08001431 int screenId = mCellLayoutContainer.getCellLayoutId(this);
Sunny Goyalaa8ef112015-06-12 20:04:41 -07001432 int container = Favorites.CONTAINER_DESKTOP;
1433
Sunny Goyalc13403c2016-11-18 23:44:48 -08001434 if (mContainerType == HOTSEAT) {
Sunny Goyalaa8ef112015-06-12 20:04:41 -07001435 screenId = -1;
1436 container = Favorites.CONTAINER_HOTSEAT;
1437 }
1438
Michael Jurkaa52570f2012-03-20 03:18:20 -07001439 int childCount = mShortcutsAndWidgets.getChildCount();
Adam Cohen482ed822012-03-02 14:15:13 -08001440 for (int i = 0; i < childCount; i++) {
Adam Cohenea889a22012-03-27 16:45:39 -07001441 View child = mShortcutsAndWidgets.getChildAt(i);
Sebastian Francod4682992022-10-05 13:03:09 -05001442 CellLayoutLayoutParams lp = (CellLayoutLayoutParams) child.getLayoutParams();
Adam Cohenea889a22012-03-27 16:45:39 -07001443 ItemInfo info = (ItemInfo) child.getTag();
Adam Cohen2acce882012-03-28 19:03:19 -07001444 // We do a null check here because the item info can be null in the case of the
1445 // AllApps button in the hotseat.
Sunny Goyal711c5962021-06-23 12:36:18 -07001446 if (info != null && child != dragView) {
Sunny Goyal669b71f2023-01-27 14:37:07 -08001447 CellPos presenterPos = mActivity.getCellPosMapper().mapModelToPresenter(info);
1448 final boolean requiresDbUpdate = (presenterPos.cellX != lp.getTmpCellX()
1449 || presenterPos.cellY != lp.getTmpCellY() || info.spanX != lp.cellHSpan
1450 || info.spanY != lp.cellVSpan || presenterPos.screenId != screenId);
Sunny Goyalaa8ef112015-06-12 20:04:41 -07001451
Sebastian Franco877088e2023-01-03 15:16:22 -07001452 lp.setCellX(lp.getTmpCellX());
Sebastian Franco877088e2023-01-03 15:16:22 -07001453 lp.setCellY(lp.getTmpCellY());
Sunny Goyalaa8ef112015-06-12 20:04:41 -07001454 if (requiresDbUpdate) {
Sunny Goyalab770a12018-11-14 15:17:26 -08001455 Launcher.cast(mActivity).getModelWriter().modifyItemInDatabase(info, container,
Sunny Goyal669b71f2023-01-27 14:37:07 -08001456 screenId, lp.getCellX(), lp.getCellY(), lp.cellHSpan, lp.cellVSpan);
Sunny Goyalaa8ef112015-06-12 20:04:41 -07001457 }
Adam Cohen2acce882012-03-28 19:03:19 -07001458 }
Adam Cohen482ed822012-03-02 14:15:13 -08001459 }
1460 }
1461
Sunny Goyalf7a29e82015-04-24 15:20:43 -07001462 private void setUseTempCoords(boolean useTempCoords) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07001463 int childCount = mShortcutsAndWidgets.getChildCount();
Adam Cohen482ed822012-03-02 14:15:13 -08001464 for (int i = 0; i < childCount; i++) {
Sebastian Francod4682992022-10-05 13:03:09 -05001465 CellLayoutLayoutParams lp = (CellLayoutLayoutParams) mShortcutsAndWidgets.getChildAt(
1466 i).getLayoutParams();
Adam Cohen482ed822012-03-02 14:15:13 -08001467 lp.useTmpCoords = useTempCoords;
1468 }
1469 }
1470
Sebastian Franco5c8f8682023-11-14 09:52:41 -06001471 /**
1472 * For a given region, return the rectangle of the overlapping cell and span with the given
1473 * region including the region itself. If there is no overlap the rectangle will be
1474 * invalid i.e. -1, 0, -1, 0.
1475 */
1476 @Nullable
1477 public Rect getIntersectingRectanglesInRegion(final Rect region, final View dragView) {
1478 Rect boundingRect = new Rect(region);
Adam Cohen19f37922012-03-21 11:59:11 -07001479 Rect r1 = new Rect();
Sebastian Franco5c8f8682023-11-14 09:52:41 -06001480 boolean isOverlapping = false;
Adam Cohen19f37922012-03-21 11:59:11 -07001481 final int count = mShortcutsAndWidgets.getChildCount();
1482 for (int i = 0; i < count; i++) {
1483 View child = mShortcutsAndWidgets.getChildAt(i);
1484 if (child == dragView) continue;
Sebastian Francod4682992022-10-05 13:03:09 -05001485 CellLayoutLayoutParams
1486 lp = (CellLayoutLayoutParams) child.getLayoutParams();
Sebastian Franco877088e2023-01-03 15:16:22 -07001487 r1.set(lp.getCellX(), lp.getCellY(), lp.getCellX() + lp.cellHSpan,
1488 lp.getCellY() + lp.cellVSpan);
Sebastian Franco5c8f8682023-11-14 09:52:41 -06001489 if (Rect.intersects(region, r1)) {
1490 isOverlapping = true;
1491 boundingRect.union(r1);
Adam Cohen19f37922012-03-21 11:59:11 -07001492 }
1493 }
Sebastian Franco5c8f8682023-11-14 09:52:41 -06001494 return isOverlapping ? boundingRect : null;
Adam Cohen19f37922012-03-21 11:59:11 -07001495 }
1496
Sebastian Francoe4c03452022-12-27 14:50:02 -06001497 public boolean isNearestDropLocationOccupied(int pixelX, int pixelY, int spanX, int spanY,
Adam Cohen19f37922012-03-21 11:59:11 -07001498 View dragView, int[] result) {
Sebastián Francof9a6ac22022-11-15 22:56:37 +00001499 result = findNearestAreaIgnoreOccupied(pixelX, pixelY, spanX, spanY, result);
Sebastian Franco5c8f8682023-11-14 09:52:41 -06001500 return getIntersectingRectanglesInRegion(
1501 new Rect(result[0], result[1], result[0] + spanX, result[1] + spanY),
1502 dragView
1503 ) != null;
Adam Cohen19f37922012-03-21 11:59:11 -07001504 }
1505
1506 void revertTempState() {
Adam Cohenfa3c58f2013-12-06 16:10:55 -08001507 completeAndClearReorderPreviewAnimations();
1508 if (isItemPlacementDirty() && !DESTRUCTIVE_REORDER) {
1509 final int count = mShortcutsAndWidgets.getChildCount();
1510 for (int i = 0; i < count; i++) {
1511 View child = mShortcutsAndWidgets.getChildAt(i);
Sebastian Francod4682992022-10-05 13:03:09 -05001512 CellLayoutLayoutParams
1513 lp = (CellLayoutLayoutParams) child.getLayoutParams();
Sebastian Franco877088e2023-01-03 15:16:22 -07001514 if (lp.getTmpCellX() != lp.getCellX() || lp.getTmpCellY() != lp.getCellY()) {
1515 lp.setTmpCellX(lp.getCellX());
1516 lp.setTmpCellY(lp.getCellY());
1517 animateChildToPosition(child, lp.getCellX(), lp.getCellY(),
1518 REORDER_ANIMATION_DURATION, 0, false, false);
Adam Cohenfa3c58f2013-12-06 16:10:55 -08001519 }
Adam Cohen19f37922012-03-21 11:59:11 -07001520 }
Adam Cohenfa3c58f2013-12-06 16:10:55 -08001521 setItemPlacementDirty(false);
Adam Cohen19f37922012-03-21 11:59:11 -07001522 }
Adam Cohen19f37922012-03-21 11:59:11 -07001523 }
1524
Adam Cohenbebf0422012-04-11 18:06:28 -07001525 boolean createAreaForResize(int cellX, int cellY, int spanX, int spanY,
1526 View dragView, int[] direction, boolean commit) {
1527 int[] pixelXY = new int[2];
1528 regionToCenterPoint(cellX, cellY, spanX, spanY, pixelXY);
1529
1530 // First we determine if things have moved enough to cause a different layout
Adam Cohenfa3c58f2013-12-06 16:10:55 -08001531 ItemConfiguration swapSolution = findReorderSolution(pixelXY[0], pixelXY[1], spanX, spanY,
Charlie Anderson46122392024-01-18 17:23:46 -05001532 spanX, spanY, direction, dragView, true);
Adam Cohenbebf0422012-04-11 18:06:28 -07001533
1534 setUseTempCoords(true);
1535 if (swapSolution != null && swapSolution.isSolution) {
1536 // If we're just testing for a possible location (MODE_ACCEPT_DROP), we don't bother
1537 // committing anything or animating anything as we just want to determine if a solution
1538 // exists
1539 copySolutionToTempState(swapSolution, dragView);
1540 setItemPlacementDirty(true);
1541 animateItemsToSolution(swapSolution, dragView, commit);
1542
1543 if (commit) {
Sunny Goyal711c5962021-06-23 12:36:18 -07001544 commitTempPlacement(null);
Adam Cohenfa3c58f2013-12-06 16:10:55 -08001545 completeAndClearReorderPreviewAnimations();
Adam Cohenbebf0422012-04-11 18:06:28 -07001546 setItemPlacementDirty(false);
1547 } else {
Adam Cohenfa3c58f2013-12-06 16:10:55 -08001548 beginOrAdjustReorderPreviewAnimations(swapSolution, dragView,
Adam Cohen65086992020-02-19 08:40:49 -08001549 ReorderPreviewAnimation.MODE_PREVIEW);
Adam Cohenbebf0422012-04-11 18:06:28 -07001550 }
1551 mShortcutsAndWidgets.requestLayout();
1552 }
1553 return swapSolution.isSolution;
1554 }
1555
Sebastian Francoe4c03452022-12-27 14:50:02 -06001556 public ReorderAlgorithm createReorderAlgorithm() {
1557 return new ReorderAlgorithm(this);
1558 }
1559
Sebastian Franco09589322022-11-02 15:25:58 -07001560 protected ItemConfiguration findReorderSolution(int pixelX, int pixelY, int minSpanX,
Charlie Anderson46122392024-01-18 17:23:46 -05001561 int minSpanY, int spanX, int spanY, int[] direction, View dragView, boolean decX) {
Sebastian Franco25f8e402023-12-02 13:54:05 -06001562 ItemConfiguration configuration = new ItemConfiguration();
1563 copyCurrentStateToSolution(configuration);
1564 ReorderParameters parameters = new ReorderParameters(pixelX, pixelY, spanX, spanY, minSpanX,
1565 minSpanY, dragView, configuration);
Charlie Anderson46122392024-01-18 17:23:46 -05001566 int[] directionVector = direction != null ? direction : mDirectionVector;
1567 return createReorderAlgorithm().findReorderSolution(parameters, directionVector, decX);
Sebastian Franco09589322022-11-02 15:25:58 -07001568 }
1569
Sebastián Franco61fbd182023-11-29 21:30:43 +00001570 public void copyCurrentStateToSolution(ItemConfiguration solution) {
Sebastian Franco9cab1c32022-10-25 17:28:54 -07001571 int childCount = mShortcutsAndWidgets.getChildCount();
1572 for (int i = 0; i < childCount; i++) {
1573 View child = mShortcutsAndWidgets.getChildAt(i);
1574 CellLayoutLayoutParams lp = (CellLayoutLayoutParams) child.getLayoutParams();
Sebastián Franco61fbd182023-11-29 21:30:43 +00001575 solution.add(child,
1576 new CellAndSpan(lp.getCellX(), lp.getCellY(), lp.cellHSpan, lp.cellVSpan));
Sebastian Franco9cab1c32022-10-25 17:28:54 -07001577 }
Sebastian Franco53a15a42022-10-25 17:28:54 -07001578 }
1579
1580 /**
Sebastian Franco53a15a42022-10-25 17:28:54 -07001581 * When the user drags an Item in the workspace sometimes we need to move the items already in
1582 * the workspace to make space for the new item, this function return a solution for that
1583 * reorder.
1584 *
1585 * @param pixelX X coordinate in the screen of the dragView in pixels
1586 * @param pixelY Y coordinate in the screen of the dragView in pixels
1587 * @param minSpanX minimum horizontal span the item can be shrunk to
1588 * @param minSpanY minimum vertical span the item can be shrunk to
1589 * @param spanX occupied horizontal span
1590 * @param spanY occupied vertical span
1591 * @param dragView the view of the item being draged
1592 * @return returns a solution for the given parameters, the solution contains all the icons and
1593 * the locations they should be in the given solution.
1594 */
1595 public ItemConfiguration calculateReorder(int pixelX, int pixelY, int minSpanX, int minSpanY,
1596 int spanX, int spanY, View dragView) {
Sebastian Franco25f8e402023-12-02 13:54:05 -06001597 ItemConfiguration configuration = new ItemConfiguration();
1598 copyCurrentStateToSolution(configuration);
1599 return createReorderAlgorithm().calculateReorder(
1600 new ReorderParameters(pixelX, pixelY, spanX, spanY, minSpanX, minSpanY, dragView,
1601 configuration)
1602 );
Sebastian Franco53a15a42022-10-25 17:28:54 -07001603 }
Adam Cohen482ed822012-03-02 14:15:13 -08001604
Sebastian Franco9cab1c32022-10-25 17:28:54 -07001605 int[] performReorder(int pixelX, int pixelY, int minSpanX, int minSpanY, int spanX, int spanY,
1606 View dragView, int[] result, int[] resultSpan, int mode) {
1607 if (resultSpan == null) {
1608 resultSpan = new int[]{-1, -1};
1609 }
1610 if (result == null) {
1611 result = new int[]{-1, -1};
1612 }
Sebastian Franco5d990ee2022-11-01 16:08:24 -07001613
1614 ItemConfiguration finalSolution = null;
1615 // We want the solution to match the animation of the preview and to match the drop so we
1616 // only recalculate in mode MODE_SHOW_REORDER_HINT because that the first one to run in the
1617 // reorder cycle.
1618 if (mode == MODE_SHOW_REORDER_HINT || mPreviousSolution == null) {
1619 finalSolution = calculateReorder(pixelX, pixelY, minSpanX, minSpanY, spanX, spanY,
1620 dragView);
1621 mPreviousSolution = finalSolution;
1622 } else {
Sebastian Franco9cab1c32022-10-25 17:28:54 -07001623 finalSolution = mPreviousSolution;
1624 // We reset this vector after drop
1625 if (mode == MODE_ON_DROP || mode == MODE_ON_DROP_EXTERNAL) {
1626 mPreviousSolution = null;
1627 }
Sebastian Franco9cab1c32022-10-25 17:28:54 -07001628 }
1629
1630 if (finalSolution == null || !finalSolution.isSolution) {
1631 result[0] = result[1] = resultSpan[0] = resultSpan[1] = -1;
1632 } else {
1633 result[0] = finalSolution.cellX;
1634 result[1] = finalSolution.cellY;
1635 resultSpan[0] = finalSolution.spanX;
1636 resultSpan[1] = finalSolution.spanY;
Sebastian Franco9c743272022-11-15 15:03:25 -08001637 performReorder(finalSolution, dragView, mode);
Sebastian Franco9cab1c32022-10-25 17:28:54 -07001638 }
Sebastian Franco9cab1c32022-10-25 17:28:54 -07001639 return result;
1640 }
1641
Sebastian Franco53a15a42022-10-25 17:28:54 -07001642 /**
1643 * Animates and submits in the DB the given ItemConfiguration depending of the mode.
1644 *
1645 * @param solution represents widgets on the screen which the Workspace will animate to and
1646 * would be submitted to the database.
1647 * @param dragView view which is being dragged over the workspace that trigger the reorder
1648 * @param mode depending on the mode different animations would be played and depending on the
1649 * mode the solution would be submitted or not the database.
1650 * The possible modes are {@link MODE_SHOW_REORDER_HINT}, {@link MODE_DRAG_OVER},
1651 * {@link MODE_ON_DROP}, {@link MODE_ON_DROP_EXTERNAL}, {@link MODE_ACCEPT_DROP}
1652 * defined in {@link CellLayout}.
1653 */
Sebastian Francoe4c03452022-12-27 14:50:02 -06001654 public void performReorder(ItemConfiguration solution, View dragView, int mode) {
Adam Cohenfa3c58f2013-12-06 16:10:55 -08001655 if (mode == MODE_SHOW_REORDER_HINT) {
Sebastian Franco53a15a42022-10-25 17:28:54 -07001656 beginOrAdjustReorderPreviewAnimations(solution, dragView,
1657 ReorderPreviewAnimation.MODE_HINT);
1658 return;
1659 }
1660 // If we're just testing for a possible location (MODE_ACCEPT_DROP), we don't bother
1661 // committing anything or animating anything as we just want to determine if a solution
1662 // exists
1663 if (mode == MODE_DRAG_OVER || mode == MODE_ON_DROP || mode == MODE_ON_DROP_EXTERNAL) {
1664 if (!DESTRUCTIVE_REORDER) {
1665 setUseTempCoords(true);
1666 }
1667
1668 if (!DESTRUCTIVE_REORDER) {
1669 copySolutionToTempState(solution, dragView);
1670 }
1671 setItemPlacementDirty(true);
1672 animateItemsToSolution(solution, dragView, mode == MODE_ON_DROP);
1673
1674 if (!DESTRUCTIVE_REORDER
1675 && (mode == MODE_ON_DROP || mode == MODE_ON_DROP_EXTERNAL)) {
1676 // Since the temp solution didn't update dragView, don't commit it either
1677 commitTempPlacement(dragView);
1678 completeAndClearReorderPreviewAnimations();
1679 setItemPlacementDirty(false);
Adam Cohenfa3c58f2013-12-06 16:10:55 -08001680 } else {
Sebastian Franco53a15a42022-10-25 17:28:54 -07001681 beginOrAdjustReorderPreviewAnimations(solution, dragView,
1682 ReorderPreviewAnimation.MODE_PREVIEW);
Adam Cohenfa3c58f2013-12-06 16:10:55 -08001683 }
Adam Cohenfa3c58f2013-12-06 16:10:55 -08001684 }
1685
Sebastian Franco53a15a42022-10-25 17:28:54 -07001686 if (mode == MODE_ON_DROP && !DESTRUCTIVE_REORDER) {
Adam Cohen482ed822012-03-02 14:15:13 -08001687 setUseTempCoords(false);
1688 }
Adam Cohen482ed822012-03-02 14:15:13 -08001689
Michael Jurkaa52570f2012-03-20 03:18:20 -07001690 mShortcutsAndWidgets.requestLayout();
Adam Cohen482ed822012-03-02 14:15:13 -08001691 }
1692
Adam Cohen19f37922012-03-21 11:59:11 -07001693 void setItemPlacementDirty(boolean dirty) {
1694 mItemPlacementDirty = dirty;
Adam Cohen482ed822012-03-02 14:15:13 -08001695 }
Adam Cohen19f37922012-03-21 11:59:11 -07001696 boolean isItemPlacementDirty() {
1697 return mItemPlacementDirty;
Adam Cohen482ed822012-03-02 14:15:13 -08001698 }
1699
Sebastian Francoe4c03452022-12-27 14:50:02 -06001700 /**
Adam Cohendf035382011-04-11 17:22:04 -07001701 * Find a starting cell position that will fit the given bounds nearest the requested
1702 * cell location. Uses Euclidean distance to score multiple vacant areas.
1703 *
1704 * @param pixelX The X location at which you want to search for a vacant area.
1705 * @param pixelY The Y location at which you want to search for a vacant area.
1706 * @param spanX Horizontal span of the object.
1707 * @param spanY Vertical span of the object.
Adam Cohendf035382011-04-11 17:22:04 -07001708 * @param result Previously returned value to possibly recycle.
1709 * @return The X, Y cell of a vacant area that can contain this object,
1710 * nearest the requested location.
1711 */
Sebastián Francof9a6ac22022-11-15 22:56:37 +00001712 public int[] findNearestAreaIgnoreOccupied(int pixelX, int pixelY, int spanX, int spanY,
1713 int[] result) {
Sebastian Francob57c0b22022-06-28 13:54:35 -07001714 return findNearestArea(pixelX, pixelY, spanX, spanY, spanX, spanY, true, result, null);
Adam Cohendf035382011-04-11 17:22:04 -07001715 }
1716
Michael Jurka0280c3b2010-09-17 15:00:07 -07001717 boolean existsEmptyCell() {
1718 return findCellForSpan(null, 1, 1);
1719 }
1720
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001721 /**
Michael Jurka0280c3b2010-09-17 15:00:07 -07001722 * Finds the upper-left coordinate of the first rectangle in the grid that can
1723 * hold a cell of the specified dimensions. If intersectX and intersectY are not -1,
1724 * then this method will only return coordinates for rectangles that contain the cell
1725 * (intersectX, intersectY)
1726 *
1727 * @param cellXY The array that will contain the position of a vacant cell if such a cell
1728 * can be found.
1729 * @param spanX The horizontal span of the cell we want to find.
1730 * @param spanY The vertical span of the cell we want to find.
1731 *
1732 * @return True if a vacant cell of the specified dimension was found, false otherwise.
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001733 */
Hyunyoung Song3f471442015-04-08 19:01:34 -07001734 public boolean findCellForSpan(int[] cellXY, int spanX, int spanY) {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001735 if (cellXY == null) {
1736 cellXY = new int[2];
Michael Jurka0280c3b2010-09-17 15:00:07 -07001737 }
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001738 return mOccupied.findVacantCell(cellXY, spanX, spanY);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001739 }
1740
1741 /**
Winson Chungc07918d2011-07-01 15:35:26 -07001742 * A drag event has begun over this layout.
1743 * It may have begun over this layout (in which case onDragChild is called first),
1744 * or it may have begun on another layout.
1745 */
1746 void onDragEnter() {
Winson Chungc07918d2011-07-01 15:35:26 -07001747 mDragging = true;
Sebastian Franco5aa71ce2022-12-14 12:13:19 -06001748 mPreviousSolution = null;
Winson Chungc07918d2011-07-01 15:35:26 -07001749 }
1750
1751 /**
Michael Jurka0280c3b2010-09-17 15:00:07 -07001752 * Called when drag has left this CellLayout or has been completed (successfully or not)
1753 */
1754 void onDragExit() {
Joe Onorato4be866d2010-10-10 11:26:02 -07001755 // This can actually be called when we aren't in a drag, e.g. when adding a new
1756 // item to this layout via the customize drawer.
1757 // Guard against that case.
1758 if (mDragging) {
1759 mDragging = false;
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001760 }
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -07001761
1762 // Invalidate the drag data
Sebastian Franco5aa71ce2022-12-14 12:13:19 -06001763 mPreviousSolution = null;
Adam Cohend41fbf52012-02-16 23:53:59 -08001764 mDragCell[0] = mDragCell[1] = -1;
Adam Cohenf7ca3b42021-02-22 11:03:58 -08001765 mDragCellSpan[0] = mDragCellSpan[1] = -1;
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -07001766 mDragOutlineAnims[mDragOutlineCurrent].animateOut();
1767 mDragOutlineCurrent = (mDragOutlineCurrent + 1) % mDragOutlineAnims.length;
Adam Cohen19f37922012-03-21 11:59:11 -07001768 revertTempState();
Michael Jurka33945b22010-12-21 18:19:38 -08001769 setIsDragOverlapping(false);
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001770 }
1771
1772 /**
Winson Chungaafa03c2010-06-11 17:34:16 -07001773 * Mark a child as having been dropped.
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001774 * At the beginning of the drag operation, the child may have been on another
Patrick Dubroyce34a972010-10-19 10:34:32 -07001775 * screen, but it is re-parented before this method is called.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001776 *
1777 * @param child The child that is being dropped
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001778 */
Adam Cohen716b51e2011-06-30 12:09:54 -07001779 void onDropChild(View child) {
Romain Guyd94533d2009-08-17 10:01:15 -07001780 if (child != null) {
Sebastian Francod4682992022-10-05 13:03:09 -05001781 CellLayoutLayoutParams
1782 lp = (CellLayoutLayoutParams) child.getLayoutParams();
Romain Guy84f296c2009-11-04 15:00:44 -08001783 lp.dropped = true;
Romain Guyd94533d2009-08-17 10:01:15 -07001784 child.requestLayout();
Tony Wickham1cdb6d02015-09-17 11:08:27 -07001785 markCellsAsOccupiedForView(child);
Romain Guyd94533d2009-08-17 10:01:15 -07001786 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001787 }
1788
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001789 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001790 * Computes a bounding rectangle for a range of cells
Winson Chungaafa03c2010-06-11 17:34:16 -07001791 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001792 * @param cellX X coordinate of upper left corner expressed as a cell position
1793 * @param cellY Y coordinate of upper left corner expressed as a cell position
Winson Chungaafa03c2010-06-11 17:34:16 -07001794 * @param cellHSpan Width in cells
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001795 * @param cellVSpan Height in cells
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001796 * @param resultRect Rect into which to put the results
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001797 */
Adam Cohend41fbf52012-02-16 23:53:59 -08001798 public void cellToRect(int cellX, int cellY, int cellHSpan, int cellVSpan, Rect resultRect) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001799 final int cellWidth = mCellWidth;
1800 final int cellHeight = mCellHeight;
Winson Chungaafa03c2010-06-11 17:34:16 -07001801
Pierre Barbier de Reuille1b8bbb62021-05-19 22:45:16 +01001802 // We observe a shift of 1 pixel on the x coordinate compared to the actual cell coordinates
1803 final int hStartPadding = getPaddingLeft()
1804 + (int) Math.ceil(getUnusedHorizontalSpace() / 2f);
Winson Chung4b825dcd2011-06-19 12:41:22 -07001805 final int vStartPadding = getPaddingTop();
Winson Chungaafa03c2010-06-11 17:34:16 -07001806
Thales Lima78d00ad2021-09-30 11:29:06 +01001807 int x = hStartPadding + (cellX * mBorderSpace.x) + (cellX * cellWidth);
1808 int y = vStartPadding + (cellY * mBorderSpace.y) + (cellY * cellHeight);
Jon Miranda228877d2021-02-09 11:05:00 -05001809
Thales Lima78d00ad2021-09-30 11:29:06 +01001810 int width = cellHSpan * cellWidth + ((cellHSpan - 1) * mBorderSpace.x);
1811 int height = cellVSpan * cellHeight + ((cellVSpan - 1) * mBorderSpace.y);
Winson Chungaafa03c2010-06-11 17:34:16 -07001812
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001813 resultRect.set(x, y, x + width, y + height);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001814 }
Winson Chungaafa03c2010-06-11 17:34:16 -07001815
Adam Cohend4844c32011-02-18 19:25:06 -08001816 public void markCellsAsOccupiedForView(View view) {
Sebastian Francof153d912022-04-22 16:15:27 -05001817 if (view instanceof LauncherAppWidgetHostView
1818 && view.getTag() instanceof LauncherAppWidgetInfo) {
1819 LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) view.getTag();
Sunny Goyal669b71f2023-01-27 14:37:07 -08001820 CellPos pos = mActivity.getCellPosMapper().mapModelToPresenter(info);
1821 mOccupied.markCells(pos.cellX, pos.cellY, info.spanX, info.spanY, true);
Sebastian Francof153d912022-04-22 16:15:27 -05001822 return;
1823 }
Michael Jurkaa52570f2012-03-20 03:18:20 -07001824 if (view == null || view.getParent() != mShortcutsAndWidgets) return;
Sebastian Francod4682992022-10-05 13:03:09 -05001825 CellLayoutLayoutParams
1826 lp = (CellLayoutLayoutParams) view.getLayoutParams();
Sebastian Franco877088e2023-01-03 15:16:22 -07001827 mOccupied.markCells(lp.getCellX(), lp.getCellY(), lp.cellHSpan, lp.cellVSpan, true);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001828 }
1829
Adam Cohend4844c32011-02-18 19:25:06 -08001830 public void markCellsAsUnoccupiedForView(View view) {
Sebastian Francof153d912022-04-22 16:15:27 -05001831 if (view instanceof LauncherAppWidgetHostView
1832 && view.getTag() instanceof LauncherAppWidgetInfo) {
1833 LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) view.getTag();
Sunny Goyal669b71f2023-01-27 14:37:07 -08001834 CellPos pos = mActivity.getCellPosMapper().mapModelToPresenter(info);
1835 mOccupied.markCells(pos.cellX, pos.cellY, info.spanX, info.spanY, false);
Sebastian Francof153d912022-04-22 16:15:27 -05001836 return;
1837 }
Michael Jurkaa52570f2012-03-20 03:18:20 -07001838 if (view == null || view.getParent() != mShortcutsAndWidgets) return;
Sebastian Francod4682992022-10-05 13:03:09 -05001839 CellLayoutLayoutParams
1840 lp = (CellLayoutLayoutParams) view.getLayoutParams();
Sebastian Franco877088e2023-01-03 15:16:22 -07001841 mOccupied.markCells(lp.getCellX(), lp.getCellY(), lp.cellHSpan, lp.cellVSpan, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001842 }
1843
Adam Cohen2801caf2011-05-13 20:57:39 -07001844 public int getDesiredWidth() {
Jon Miranda228877d2021-02-09 11:05:00 -05001845 return getPaddingLeft() + getPaddingRight() + (mCountX * mCellWidth)
Thales Lima78d00ad2021-09-30 11:29:06 +01001846 + ((mCountX - 1) * mBorderSpace.x);
Adam Cohen2801caf2011-05-13 20:57:39 -07001847 }
1848
1849 public int getDesiredHeight() {
Jon Miranda228877d2021-02-09 11:05:00 -05001850 return getPaddingTop() + getPaddingBottom() + (mCountY * mCellHeight)
Thales Lima78d00ad2021-09-30 11:29:06 +01001851 + ((mCountY - 1) * mBorderSpace.y);
Adam Cohen2801caf2011-05-13 20:57:39 -07001852 }
1853
Michael Jurka66d72172011-04-12 16:29:25 -07001854 public boolean isOccupied(int x, int y) {
Schneider Victor-tulias750c5af2023-07-26 10:16:04 -07001855 if (x >= 0 && x < mCountX && y >= 0 && y < mCountY) {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001856 return mOccupied.cells[x][y];
Schneider Victor-tulias750c5af2023-07-26 10:16:04 -07001857 }
1858 if (BuildConfig.IS_STUDIO_BUILD) {
Michael Jurka66d72172011-04-12 16:29:25 -07001859 throw new RuntimeException("Position exceeds the bound of this CellLayout");
1860 }
Schneider Victor-tulias750c5af2023-07-26 10:16:04 -07001861 return true;
Michael Jurka66d72172011-04-12 16:29:25 -07001862 }
1863
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001864 @Override
1865 public ViewGroup.LayoutParams generateLayoutParams(AttributeSet attrs) {
Sebastian Francod4682992022-10-05 13:03:09 -05001866 return new CellLayoutLayoutParams(getContext(), attrs);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001867 }
1868
1869 @Override
1870 protected boolean checkLayoutParams(ViewGroup.LayoutParams p) {
Sebastian Francod4682992022-10-05 13:03:09 -05001871 return p instanceof CellLayoutLayoutParams;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001872 }
1873
1874 @Override
1875 protected ViewGroup.LayoutParams generateLayoutParams(ViewGroup.LayoutParams p) {
Sebastian Francod4682992022-10-05 13:03:09 -05001876 return new CellLayoutLayoutParams(p);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001877 }
1878
Samuel Fufa1e2d0042019-11-18 17:12:46 -08001879 /**
Tony Wickham86930612015-09-09 13:50:40 -07001880 * Returns whether an item can be placed in this CellLayout (after rearranging and/or resizing
1881 * if necessary).
1882 */
1883 public boolean hasReorderSolution(ItemInfo itemInfo) {
1884 int[] cellPoint = new int[2];
1885 // Check for a solution starting at every cell.
1886 for (int cellX = 0; cellX < getCountX(); cellX++) {
1887 for (int cellY = 0; cellY < getCountY(); cellY++) {
1888 cellToPoint(cellX, cellY, cellPoint);
1889 if (findReorderSolution(cellPoint[0], cellPoint[1], itemInfo.minSpanX,
1890 itemInfo.minSpanY, itemInfo.spanX, itemInfo.spanY, mDirectionVector, null,
Charlie Anderson46122392024-01-18 17:23:46 -05001891 true).isSolution) {
Tony Wickham86930612015-09-09 13:50:40 -07001892 return true;
1893 }
1894 }
1895 }
1896 return false;
1897 }
1898
Samuel Fufaa4211432020-02-25 18:47:54 -08001899 /**
1900 * Finds solution to accept hotseat migration to cell layout. commits solution if commitConfig
1901 */
1902 public boolean makeSpaceForHotseatMigration(boolean commitConfig) {
Samuel Fufaa4211432020-02-25 18:47:54 -08001903 int[] cellPoint = new int[2];
1904 int[] directionVector = new int[]{0, -1};
1905 cellToPoint(0, mCountY, cellPoint);
Charlie Anderson46122392024-01-18 17:23:46 -05001906 ItemConfiguration configuration = findReorderSolution(
1907 cellPoint[0] /* pixelX */,
1908 cellPoint[1] /* pixelY */,
1909 mCountX /* minSpanX */,
1910 1 /* minSpanY */,
1911 mCountX /* spanX */,
1912 1 /* spanY */,
1913 directionVector /* direction */,
1914 null /* dragView */,
1915 false /* decX */
1916 );
1917 if (configuration.isSolution) {
Samuel Fufaa4211432020-02-25 18:47:54 -08001918 if (commitConfig) {
1919 copySolutionToTempState(configuration, null);
Sunny Goyal711c5962021-06-23 12:36:18 -07001920 commitTempPlacement(null);
Samuel Fufa82bbdac2020-03-09 18:24:47 -07001921 // undo marking cells occupied since there is actually nothing being placed yet.
1922 mOccupied.markCells(0, mCountY - 1, mCountX, 1, false);
Samuel Fufaa4211432020-02-25 18:47:54 -08001923 }
1924 return true;
1925 }
1926 return false;
1927 }
1928
Samuel Fufa82bbdac2020-03-09 18:24:47 -07001929 /**
1930 * returns a copy of cell layout's grid occupancy
1931 */
1932 public GridOccupancy cloneGridOccupancy() {
1933 GridOccupancy occupancy = new GridOccupancy(mCountX, mCountY);
1934 mOccupied.copyTo(occupancy);
1935 return occupancy;
1936 }
1937
Sunny Goyal9ca9c132015-04-29 14:57:22 -07001938 public boolean isRegionVacant(int x, int y, int spanX, int spanY) {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001939 return mOccupied.isRegionVacant(x, y, spanX, spanY);
Sunny Goyal9ca9c132015-04-29 14:57:22 -07001940 }
Sebastian Franco9ea36d42023-09-21 13:56:42 -07001941
1942 public void setSpaceBetweenCellLayoutsPx(@Px int spaceBetweenCellLayoutsPx) {
1943 mSpaceBetweenCellLayoutsPx = spaceBetweenCellLayoutsPx;
1944 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001945}