blob: ee72c222ebe5be09be92fe6af70cbe80c77bdb23 [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";
Stefan Andonianfb348912024-08-14 22:51:32 +000089 private static final boolean LOGD = true;
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;
Stefan Andonianfb348912024-08-14 22:51:32 +0000169 public boolean mHasOnLayoutBeenCalled = false;
Joe Onorato4be866d2010-10-10 11:26:02 -0700170
Rajeev Kumar9962dbe2017-06-12 12:16:20 -0700171 private final TimeInterpolator mEaseOutInterpolator;
Sebastian Franco09589322022-11-02 15:25:58 -0700172 protected final ShortcutAndWidgetContainer mShortcutsAndWidgets;
Sebastian Franco9ea36d42023-09-21 13:56:42 -0700173 @Px
174 protected int mSpaceBetweenCellLayoutsPx = 0;
Patrick Dubroyce34a972010-10-19 10:34:32 -0700175
Sunny Goyalc13403c2016-11-18 23:44:48 -0800176 @Retention(RetentionPolicy.SOURCE)
177 @IntDef({WORKSPACE, HOTSEAT, FOLDER})
178 public @interface ContainerType{}
179 public static final int WORKSPACE = 0;
180 public static final int HOTSEAT = 1;
181 public static final int FOLDER = 2;
182
183 @ContainerType private final int mContainerType;
184
Sebastian Francof7654252024-01-18 11:50:50 -0800185 public static final float DEFAULT_SCALE = 1f;
Andrew Flynn0dca1ec2012-02-29 13:33:22 -0800186
Adam Cohenfa3c58f2013-12-06 16:10:55 -0800187 public static final int MODE_SHOW_REORDER_HINT = 0;
188 public static final int MODE_DRAG_OVER = 1;
189 public static final int MODE_ON_DROP = 2;
190 public static final int MODE_ON_DROP_EXTERNAL = 3;
191 public static final int MODE_ACCEPT_DROP = 4;
Adam Cohen19f37922012-03-21 11:59:11 -0700192 private static final boolean DESTRUCTIVE_REORDER = false;
Adam Cohen482ed822012-03-02 14:15:13 -0800193 private static final boolean DEBUG_VISUALIZE_OCCUPIED = false;
194
Sebastian Francof7654252024-01-18 11:50:50 -0800195 public static final float REORDER_PREVIEW_MAGNITUDE = 0.12f;
196 public static final int REORDER_ANIMATION_DURATION = 150;
Sunny Goyalc13403c2016-11-18 23:44:48 -0800197 @Thunk final float mReorderPreviewAnimationMagnitude;
Adam Cohen19f37922012-03-21 11:59:11 -0700198
Sebastian Francoe4c03452022-12-27 14:50:02 -0600199 public final int[] mDirectionVector = new int[2];
Steven Ng30dd1d62021-03-15 21:45:49 +0000200
Sebastian Franco53a15a42022-10-25 17:28:54 -0700201 ItemConfiguration mPreviousSolution = null;
Adam Cohen482ed822012-03-02 14:15:13 -0800202
Sunny Goyal2805e632015-05-20 15:35:32 -0700203 private final Rect mTempRect = new Rect();
Winson Chung3a6e7f32013-10-09 15:50:52 -0700204
Sunny Goyal73b5a272019-12-09 14:55:56 -0800205 private static final Paint sPaint = new Paint();
Romain Guy8a0bff52012-05-06 13:14:33 -0700206
Adam Cohenc9735cf2015-01-23 16:11:55 -0800207 // Related to accessible drag and drop
Adam Cohen6e7c37a2020-06-25 19:22:37 -0700208 DragAndDropAccessibilityDelegate mTouchHelper;
Adam Cohenc9735cf2015-01-23 16:11:55 -0800209
Sebastian Franco2986e0b2024-01-25 10:23:39 -0800210 CellLayoutContainer mCellLayoutContainer;
Adam Cohenf7ca3b42021-02-22 11:03:58 -0800211
212 public static final FloatProperty<CellLayout> SPRING_LOADED_PROGRESS =
213 new FloatProperty<CellLayout>("spring_loaded_progress") {
214 @Override
215 public Float get(CellLayout cl) {
216 return cl.getSpringLoadedProgress();
217 }
218
219 @Override
220 public void setValue(CellLayout cl, float progress) {
221 cl.setSpringLoadedProgress(progress);
222 }
223 };
224
Sebastian Franco2986e0b2024-01-25 10:23:39 -0800225 public CellLayout(Context context, CellLayoutContainer container) {
226 this(context, (AttributeSet) null);
227 this.mCellLayoutContainer = container;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800228 }
229
230 public CellLayout(Context context, AttributeSet attrs) {
231 this(context, attrs, 0);
232 }
233
234 public CellLayout(Context context, AttributeSet attrs, int defStyle) {
235 super(context, attrs, defStyle);
Sunny Goyalc13403c2016-11-18 23:44:48 -0800236 TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.CellLayout, defStyle, 0);
237 mContainerType = a.getInteger(R.styleable.CellLayout_containerType, WORKSPACE);
238 a.recycle();
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700239
240 // A ViewGroup usually does not draw, but CellLayout needs to draw a rectangle to show
241 // the user where a dragged item will land when dropped.
242 setWillNotDraw(false);
Romain Guyce3cbd12013-02-25 15:00:36 -0800243 setClipToPadding(false);
Pat Manningd0f729d2023-01-09 12:04:25 +0000244 setClipChildren(false);
Sunny Goyalab770a12018-11-14 15:17:26 -0800245 mActivity = ActivityContext.lookupContext(context);
Steven Ngcc505b82021-03-18 23:04:35 +0000246 DeviceProfile deviceProfile = mActivity.getDeviceProfile();
Michael Jurkaa63c4522010-08-19 13:52:27 -0700247
Thales Lima8cd020b2022-03-15 20:15:14 +0000248 resetCellSizeInternal(deviceProfile);
Sunny Goyalff4ba2d2016-04-02 14:12:34 -0700249
Steven Ngcc505b82021-03-18 23:04:35 +0000250 mCountX = deviceProfile.inv.numColumns;
251 mCountY = deviceProfile.inv.numRows;
Sunny Goyalff4ba2d2016-04-02 14:12:34 -0700252 mOccupied = new GridOccupancy(mCountX, mCountY);
253 mTmpOccupied = new GridOccupancy(mCountX, mCountY);
254
Samuel Fufa1e2d0042019-11-18 17:12:46 -0800255 mFolderLeaveBehind.mDelegateCellX = -1;
256 mFolderLeaveBehind.mDelegateCellY = -1;
Adam Cohenefca0272016-02-24 19:19:06 -0800257
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800258 setAlwaysDrawnWithCacheEnabled(false);
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700259
Adam Cohenf7ca3b42021-02-22 11:03:58 -0800260 Resources res = getResources();
261
262 mBackground = getContext().getDrawable(R.drawable.bg_celllayout);
Sunny Goyal2805e632015-05-20 15:35:32 -0700263 mBackground.setCallback(this);
Sunny Goyalaeb16432017-10-16 11:46:41 -0700264 mBackground.setAlpha(0);
Michael Jurka33945b22010-12-21 18:19:38 -0800265
Yogisha Dixitc0ac1dd2021-05-29 00:26:25 +0100266 mGridColor = Themes.getAttrColor(getContext(), R.attr.workspaceAccentColor);
Adam Cohenf7ca3b42021-02-22 11:03:58 -0800267 mGridVisualizationRoundingRadius =
268 res.getDimensionPixelSize(R.dimen.grid_visualization_rounding_radius);
Steven Ngcc505b82021-03-18 23:04:35 +0000269 mReorderPreviewAnimationMagnitude = (REORDER_PREVIEW_MAGNITUDE * deviceProfile.iconSizePx);
Adam Cohen19f37922012-03-21 11:59:11 -0700270
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700271 // Initialize the data structures used for the drag visualization.
Kateryna Ivanova71203732023-05-24 15:09:00 +0000272 mEaseOutInterpolator = Interpolators.DECELERATE_QUINT; // Quint ease out
Winson Chungb8c69f32011-10-19 21:36:08 -0700273 mDragCell[0] = mDragCell[1] = -1;
Adam Cohenf7ca3b42021-02-22 11:03:58 -0800274 mDragCellSpan[0] = mDragCellSpan[1] = -1;
Joe Onorato4be866d2010-10-10 11:26:02 -0700275 for (int i = 0; i < mDragOutlines.length; i++) {
Sunny Goyal669b71f2023-01-27 14:37:07 -0800276 mDragOutlines[i] = new CellLayoutLayoutParams(0, 0, 0, 0);
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700277 }
Mario Bertschler54ba6012017-06-08 10:53:53 -0700278 mDragOutlinePaint.setColor(Themes.getAttrColor(context, R.attr.workspaceTextColor));
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700279
280 // When dragging things around the home screens, we show a green outline of
281 // where the item will land. The outlines gradually fade out, leaving a trail
282 // behind the drag path.
283 // Set up all the animations that are used to implement this fading.
284 final int duration = res.getInteger(R.integer.config_dragOutlineFadeTime);
Chet Haase472b2812010-10-14 07:02:04 -0700285 final float fromAlphaValue = 0;
286 final float toAlphaValue = (float)res.getInteger(R.integer.config_dragOutlineMaxAlpha);
Joe Onorato4be866d2010-10-10 11:26:02 -0700287
Patrick Dubroy8e58e912010-10-14 13:21:48 -0700288 Arrays.fill(mDragOutlineAlphas, fromAlphaValue);
Joe Onorato4be866d2010-10-10 11:26:02 -0700289
290 for (int i = 0; i < mDragOutlineAnims.length; i++) {
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700291 final InterruptibleInOutAnimator anim =
Sebastian Francof153d912022-04-22 16:15:27 -0500292 new InterruptibleInOutAnimator(duration, fromAlphaValue, toAlphaValue);
Patrick Dubroyce34a972010-10-19 10:34:32 -0700293 anim.getAnimator().setInterpolator(mEaseOutInterpolator);
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700294 final int thisIndex = i;
Chet Haase472b2812010-10-14 07:02:04 -0700295 anim.getAnimator().addUpdateListener(new AnimatorUpdateListener() {
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700296 public void onAnimationUpdate(ValueAnimator animation) {
Joe Onorato4be866d2010-10-10 11:26:02 -0700297 // If an animation is started and then stopped very quickly, we can still
298 // get spurious updates we've cleared the tag. Guard against this.
Adam Cohenf7ca3b42021-02-22 11:03:58 -0800299 mDragOutlineAlphas[thisIndex] = (Float) animation.getAnimatedValue();
300 CellLayout.this.invalidate();
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700301 }
302 });
Joe Onorato4be866d2010-10-10 11:26:02 -0700303 // The animation holds a reference to the drag outline bitmap as long is it's
304 // running. This way the bitmap can be GCed when the animations are complete.
Joe Onorato4be866d2010-10-10 11:26:02 -0700305 mDragOutlineAnims[i] = anim;
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700306 }
Patrick Dubroyce34a972010-10-19 10:34:32 -0700307
Sunny Goyalc13403c2016-11-18 23:44:48 -0800308 mShortcutsAndWidgets = new ShortcutAndWidgetContainer(context, mContainerType);
Jon Miranda228877d2021-02-09 11:05:00 -0500309 mShortcutsAndWidgets.setCellDimensions(mCellWidth, mCellHeight, mCountX, mCountY,
Thales Lima78d00ad2021-09-30 11:29:06 +0100310 mBorderSpace);
Michael Jurkaa52570f2012-03-20 03:18:20 -0700311 addView(mShortcutsAndWidgets);
Michael Jurka18014792010-10-14 09:01:34 -0700312 }
313
Sebastian Franco2986e0b2024-01-25 10:23:39 -0800314 public CellLayoutContainer getCellLayoutContainer() {
315 return mCellLayoutContainer;
316 }
317
318 public void setCellLayoutContainer(CellLayoutContainer cellLayoutContainer) {
319 mCellLayoutContainer = cellLayoutContainer;
320 }
321
Sunny Goyal9b180102020-03-11 10:02:29 -0700322 /**
323 * Sets or clears a delegate used for accessible drag and drop
324 */
325 public void setDragAndDropAccessibilityDelegate(DragAndDropAccessibilityDelegate delegate) {
Sunny Goyal9b180102020-03-11 10:02:29 -0700326 ViewCompat.setAccessibilityDelegate(this, delegate);
327
Adam Cohen6e7c37a2020-06-25 19:22:37 -0700328 mTouchHelper = delegate;
329 int accessibilityFlag = mTouchHelper != null
Sunny Goyal9b180102020-03-11 10:02:29 -0700330 ? IMPORTANT_FOR_ACCESSIBILITY_YES : IMPORTANT_FOR_ACCESSIBILITY_NO;
331 setImportantForAccessibility(accessibilityFlag);
332 getShortcutsAndWidgets().setImportantForAccessibility(accessibilityFlag);
Sunny Goyal384b5782021-02-09 22:50:02 -0800333 // ExploreByTouchHelper sets focusability. Clear it when the delegate is cleared.
334 setFocusable(delegate != null);
Adam Cohenc9735cf2015-01-23 16:11:55 -0800335 // Invalidate the accessibility hierarchy
336 if (getParent() != null) {
337 getParent().notifySubtreeAccessibilityStateChanged(
338 this, this, AccessibilityEvent.CONTENT_CHANGE_TYPE_SUBTREE);
339 }
340 }
341
Sunny Goyala4647b62021-02-02 13:45:34 -0800342 /**
343 * Returns the currently set accessibility delegate
344 */
345 public DragAndDropAccessibilityDelegate getDragAndDropAccessibilityDelegate() {
346 return mTouchHelper;
347 }
348
Adam Cohenc9735cf2015-01-23 16:11:55 -0800349 @Override
Adam Cohen6e7c37a2020-06-25 19:22:37 -0700350 public boolean dispatchHoverEvent(MotionEvent event) {
351 // Always attempt to dispatch hover events to accessibility first.
352 if (mTouchHelper != null && mTouchHelper.dispatchHoverEvent(event)) {
353 return true;
354 }
355 return super.dispatchHoverEvent(event);
356 }
357
358 @Override
Adam Cohenc9735cf2015-01-23 16:11:55 -0800359 public boolean onInterceptTouchEvent(MotionEvent ev) {
Winson Chungf9935182020-10-23 09:26:44 -0700360 return mTouchHelper != null
361 || (mInterceptTouchListener != null && mInterceptTouchListener.onTouch(this, ev));
Adam Cohenc9735cf2015-01-23 16:11:55 -0800362 }
363
Chris Craik01f2d7f2013-10-01 14:41:56 -0700364 public void enableHardwareLayer(boolean hasLayer) {
365 mShortcutsAndWidgets.setLayerType(hasLayer ? LAYER_TYPE_HARDWARE : LAYER_TYPE_NONE, sPaint);
Michael Jurkad51f33a2012-06-28 15:35:26 -0700366 }
367
vadimt04f356f2019-02-14 18:46:36 -0800368 public boolean isHardwareLayerEnabled() {
369 return mShortcutsAndWidgets.getLayerType() == LAYER_TYPE_HARDWARE;
370 }
371
Thales Lima8cd020b2022-03-15 20:15:14 +0000372 /**
373 * Change sizes of cells
374 *
375 * @param width the new width of the cells
376 * @param height the new height of the cells
377 */
Winson Chung5f8afe62013-08-12 16:19:28 -0700378 public void setCellDimensions(int width, int height) {
Winson Chung11a1a532013-09-13 11:14:45 -0700379 mFixedCellWidth = mCellWidth = width;
380 mFixedCellHeight = mCellHeight = height;
Jon Miranda228877d2021-02-09 11:05:00 -0500381 mShortcutsAndWidgets.setCellDimensions(mCellWidth, mCellHeight, mCountX, mCountY,
Thales Lima78d00ad2021-09-30 11:29:06 +0100382 mBorderSpace);
Winson Chung5f8afe62013-08-12 16:19:28 -0700383 }
384
Thales Lima8cd020b2022-03-15 20:15:14 +0000385 private void resetCellSizeInternal(DeviceProfile deviceProfile) {
386 switch (mContainerType) {
387 case FOLDER:
Jordan Silva637f4eb2023-06-13 11:21:53 +0100388 mBorderSpace = new Point(deviceProfile.folderCellLayoutBorderSpacePx);
Thales Lima8cd020b2022-03-15 20:15:14 +0000389 break;
390 case HOTSEAT:
391 mBorderSpace = new Point(deviceProfile.hotseatBorderSpace,
392 deviceProfile.hotseatBorderSpace);
393 break;
394 case WORKSPACE:
395 default:
396 mBorderSpace = new Point(deviceProfile.cellLayoutBorderSpacePx);
397 break;
398 }
399
400 mCellWidth = mCellHeight = -1;
401 mFixedCellWidth = mFixedCellHeight = -1;
402 }
403
404 /**
405 * Reset the cell sizes and border space
406 */
407 public void resetCellSize(DeviceProfile deviceProfile) {
408 resetCellSizeInternal(deviceProfile);
409 requestLayout();
410 }
411
Adam Cohen2801caf2011-05-13 20:57:39 -0700412 public void setGridSize(int x, int y) {
413 mCountX = x;
414 mCountY = y;
Sunny Goyalff4ba2d2016-04-02 14:12:34 -0700415 mOccupied = new GridOccupancy(mCountX, mCountY);
416 mTmpOccupied = new GridOccupancy(mCountX, mCountY);
Jon Miranda228877d2021-02-09 11:05:00 -0500417 mShortcutsAndWidgets.setCellDimensions(mCellWidth, mCellHeight, mCountX, mCountY,
Thales Lima78d00ad2021-09-30 11:29:06 +0100418 mBorderSpace);
Adam Cohen76fc0852011-06-17 13:26:23 -0700419 requestLayout();
Adam Cohen2801caf2011-05-13 20:57:39 -0700420 }
421
Adam Cohen2374abf2013-04-16 14:56:57 -0700422 // Set whether or not to invert the layout horizontally if the layout is in RTL mode.
423 public void setInvertIfRtl(boolean invert) {
424 mShortcutsAndWidgets.setInvertIfRtl(invert);
425 }
426
Adam Cohen917e3882013-10-31 15:03:35 -0700427 public void setDropPending(boolean pending) {
428 mDropPending = pending;
429 }
430
431 public boolean isDropPending() {
432 return mDropPending;
433 }
434
Adam Cohenc50438c2014-08-19 17:43:05 -0700435 void setIsDragOverlapping(boolean isDragOverlapping) {
436 if (mIsDragOverlapping != isDragOverlapping) {
437 mIsDragOverlapping = isDragOverlapping;
Sunny Goyalf5440cb2016-12-14 15:13:00 -0800438 mBackground.setState(mIsDragOverlapping
439 ? BACKGROUND_STATE_ACTIVE : BACKGROUND_STATE_DEFAULT);
Adam Cohenc50438c2014-08-19 17:43:05 -0700440 invalidate();
441 }
442 }
443
Sunny Goyale2fd14b2015-08-27 17:45:46 -0700444 @Override
445 protected void dispatchSaveInstanceState(SparseArray<Parcelable> container) {
Sunny Goyal7ce471b2017-08-02 03:37:39 -0700446 ParcelableSparseArray jail = getJailedArray(container);
447 super.dispatchSaveInstanceState(jail);
448 container.put(R.id.cell_layout_jail_id, jail);
Sunny Goyale2fd14b2015-08-27 17:45:46 -0700449 }
450
451 @Override
452 protected void dispatchRestoreInstanceState(SparseArray<Parcelable> container) {
Sunny Goyal7ce471b2017-08-02 03:37:39 -0700453 super.dispatchRestoreInstanceState(getJailedArray(container));
Sunny Goyale2fd14b2015-08-27 17:45:46 -0700454 }
455
Sunny Goyal7ce471b2017-08-02 03:37:39 -0700456 /**
457 * Wrap the SparseArray in another Parcelable so that the item ids do not conflict with our
458 * our internal resource ids
459 */
Sunny Goyale2fd14b2015-08-27 17:45:46 -0700460 private ParcelableSparseArray getJailedArray(SparseArray<Parcelable> container) {
461 final Parcelable parcelable = container.get(R.id.cell_layout_jail_id);
462 return parcelable instanceof ParcelableSparseArray ?
463 (ParcelableSparseArray) parcelable : new ParcelableSparseArray();
464 }
465
Tony Wickham0f97b782015-12-02 17:55:07 -0800466 public boolean getIsDragOverlapping() {
467 return mIsDragOverlapping;
468 }
469
Jeff Sharkey83f111d2009-04-20 21:03:13 -0700470 @Override
Patrick Dubroy1262e362010-10-06 15:49:50 -0700471 protected void onDraw(Canvas canvas) {
Michael Jurka3e7c7632010-10-02 16:01:03 -0700472 // When we're large, we are either drawn in a "hover" state (ie when dragging an item to
473 // a neighboring page) or with just a normal background (if backgroundAlpha > 0.0f)
474 // When we're small, we are either drawn normally or in the "accepts drops" state (during
475 // a drag). However, we also drag the mini hover background *over* one of those two
476 // backgrounds
Sunny Goyalaeb16432017-10-16 11:46:41 -0700477 if (mBackground.getAlpha() > 0) {
Sunny Goyal2805e632015-05-20 15:35:32 -0700478 mBackground.draw(canvas);
Michael Jurkaa63c4522010-08-19 13:52:27 -0700479 }
Romain Guya6abce82009-11-10 02:54:41 -0800480
Adam Cohen482ed822012-03-02 14:15:13 -0800481 if (DEBUG_VISUALIZE_OCCUPIED) {
Tony Wickham0ac045f2021-11-03 13:17:02 -0700482 Rect cellBounds = new Rect();
483 // Will contain the bounds of the cell including spacing between cells.
484 Rect cellBoundsWithSpacing = new Rect();
Tony Wickham12784902021-11-03 14:02:10 -0700485 int[] targetCell = new int[2];
Tony Wickham0ac045f2021-11-03 13:17:02 -0700486 int[] cellCenter = new int[2];
487 Paint debugPaint = new Paint();
488 debugPaint.setStrokeWidth(Utilities.dpToPx(1));
489 for (int x = 0; x < mCountX; x++) {
490 for (int y = 0; y < mCountY; y++) {
491 if (!mOccupied.cells[x][y]) {
492 continue;
Adam Cohen482ed822012-03-02 14:15:13 -0800493 }
Tony Wickham12784902021-11-03 14:02:10 -0700494 targetCell[0] = x;
495 targetCell[1] = y;
Tony Wickham0ac045f2021-11-03 13:17:02 -0700496
Tony Wickham12784902021-11-03 14:02:10 -0700497 boolean canCreateFolder = canCreateFolder(getChildAt(x, y));
Tony Wickham0ac045f2021-11-03 13:17:02 -0700498 cellToRect(x, y, 1, 1, cellBounds);
499 cellBoundsWithSpacing.set(cellBounds);
500 cellBoundsWithSpacing.inset(-mBorderSpace.x / 2, -mBorderSpace.y / 2);
Tony Wickham3cfa5ed2021-11-03 13:20:43 -0700501 getWorkspaceCellVisualCenter(x, y, cellCenter);
Tony Wickham0ac045f2021-11-03 13:17:02 -0700502
503 canvas.save();
504 canvas.clipRect(cellBoundsWithSpacing);
505
506 // Draw reorder drag target.
507 debugPaint.setColor(Color.RED);
Sebastian Franco6e1024e2022-07-29 13:46:49 -0700508 canvas.drawCircle(cellCenter[0], cellCenter[1],
509 getReorderRadius(targetCell, 1, 1), debugPaint);
Tony Wickham0ac045f2021-11-03 13:17:02 -0700510
511 // Draw folder creation drag target.
512 if (canCreateFolder) {
513 debugPaint.setColor(Color.GREEN);
514 canvas.drawCircle(cellCenter[0], cellCenter[1],
Tony Wickham12784902021-11-03 14:02:10 -0700515 getFolderCreationRadius(targetCell), debugPaint);
Tony Wickham0ac045f2021-11-03 13:17:02 -0700516 }
517
518 canvas.restore();
Adam Cohen482ed822012-03-02 14:15:13 -0800519 }
520 }
521 }
522
Samuel Fufa1e2d0042019-11-18 17:12:46 -0800523 for (int i = 0; i < mDelegatedCellDrawings.size(); i++) {
524 DelegatedCellDrawing cellDrawing = mDelegatedCellDrawings.get(i);
525 cellToPoint(cellDrawing.mDelegateCellX, cellDrawing.mDelegateCellY, mTempLocation);
Adam Cohenefca0272016-02-24 19:19:06 -0800526 canvas.save();
527 canvas.translate(mTempLocation[0], mTempLocation[1]);
Samuel Fufa1e2d0042019-11-18 17:12:46 -0800528 cellDrawing.drawUnderItem(canvas);
Adam Cohenefca0272016-02-24 19:19:06 -0800529 canvas.restore();
Adam Cohen69ce2e52011-07-03 19:25:21 -0700530 }
Adam Cohenc51934b2011-07-26 21:07:43 -0700531
Samuel Fufa1e2d0042019-11-18 17:12:46 -0800532 if (mFolderLeaveBehind.mDelegateCellX >= 0 && mFolderLeaveBehind.mDelegateCellY >= 0) {
533 cellToPoint(mFolderLeaveBehind.mDelegateCellX,
534 mFolderLeaveBehind.mDelegateCellY, mTempLocation);
Adam Cohenefca0272016-02-24 19:19:06 -0800535 canvas.save();
536 canvas.translate(mTempLocation[0], mTempLocation[1]);
Tony Wickhamec6fd6f2023-03-11 02:08:57 +0000537 mFolderLeaveBehind.drawLeaveBehind(canvas, FOLDER_LEAVE_BEHIND_COLOR);
Adam Cohenefca0272016-02-24 19:19:06 -0800538 canvas.restore();
Adam Cohenc51934b2011-07-26 21:07:43 -0700539 }
Adam Cohen65086992020-02-19 08:40:49 -0800540
Adam Cohenf7ca3b42021-02-22 11:03:58 -0800541 if (mVisualizeCells || mVisualizeDropLocation) {
Adam Cohen65086992020-02-19 08:40:49 -0800542 visualizeGrid(canvas);
543 }
544 }
545
Adam Cohenf7ca3b42021-02-22 11:03:58 -0800546 /**
Tony Wickham12784902021-11-03 14:02:10 -0700547 * Returns whether dropping an icon on the given View can create (or add to) a folder.
548 */
549 private boolean canCreateFolder(View child) {
550 return child instanceof DraggableView
551 && ((DraggableView) child).getViewType() == DRAGGABLE_ICON;
552 }
553
554 /**
Adam Cohenf7ca3b42021-02-22 11:03:58 -0800555 * Indicates the progress of the Workspace entering the SpringLoaded state; allows the
556 * CellLayout to update various visuals for this state.
557 *
558 * @param progress
559 */
560 public void setSpringLoadedProgress(float progress) {
561 if (Float.compare(progress, mSpringLoadedProgress) != 0) {
562 mSpringLoadedProgress = progress;
fbarone74256b2023-04-10 14:50:31 -0700563 updateBgAlpha();
Adam Cohenf7ca3b42021-02-22 11:03:58 -0800564 setGridAlpha(progress);
565 }
566 }
567
568 /**
569 * See setSpringLoadedProgress
570 * @return progress
571 */
572 public float getSpringLoadedProgress() {
573 return mSpringLoadedProgress;
574 }
575
Sebastian Franco09589322022-11-02 15:25:58 -0700576 protected void updateBgAlpha() {
Sebastian Franco2986e0b2024-01-25 10:23:39 -0800577 mBackground.setAlpha((int) (mSpringLoadedProgress * 255));
Adam Cohenf7ca3b42021-02-22 11:03:58 -0800578 }
579
580 /**
581 * Set the progress of this page's scroll
582 *
583 * @param progress 0 if the screen is centered, +/-1 if it is to the right / left respectively
584 */
585 public void setScrollProgress(float progress) {
586 if (Float.compare(Math.abs(progress), mScrollProgress) != 0) {
587 mScrollProgress = Math.abs(progress);
fbarone74256b2023-04-10 14:50:31 -0700588 updateBgAlpha();
Adam Cohenf7ca3b42021-02-22 11:03:58 -0800589 }
590 }
591
592 private void setGridAlpha(float gridAlpha) {
593 if (Float.compare(gridAlpha, mGridAlpha) != 0) {
594 mGridAlpha = gridAlpha;
595 invalidate();
596 }
597 }
598
Adam Cohen65086992020-02-19 08:40:49 -0800599 protected void visualizeGrid(Canvas canvas) {
Adam Cohen0c4d2782021-04-29 15:56:13 -0700600 DeviceProfile dp = mActivity.getDeviceProfile();
Alex Chau51da2192022-05-20 13:32:10 +0100601 int paddingX = Math.min((mCellWidth - dp.iconSizePx) / 2, dp.gridVisualizationPaddingX);
602 int paddingY = Math.min((mCellHeight - dp.iconSizePx) / 2, dp.gridVisualizationPaddingY);
Adam Cohen65086992020-02-19 08:40:49 -0800603
Adam Cohenf7ca3b42021-02-22 11:03:58 -0800604 mVisualizeGridPaint.setStrokeWidth(8);
Adam Cohen65086992020-02-19 08:40:49 -0800605
Sebastian Franco0dd5db82023-10-13 11:09:21 -0700606 // This is used for debugging purposes only
Adam Cohenf7ca3b42021-02-22 11:03:58 -0800607 if (mVisualizeCells) {
Sebastian Franco0dd5db82023-10-13 11:09:21 -0700608 int paintAlpha = (int) (120 * mGridAlpha);
609 mVisualizeGridPaint.setColor(ColorUtils.setAlphaComponent(mGridColor, paintAlpha));
Adam Cohenf7ca3b42021-02-22 11:03:58 -0800610 for (int i = 0; i < mCountX; i++) {
611 for (int j = 0; j < mCountY; j++) {
Sebastian Franco0dd5db82023-10-13 11:09:21 -0700612 cellToRect(i, j, 1, 1, mTempOnDrawCellToRect);
613 mVisualizeGridRect.set(mTempOnDrawCellToRect);
614 mVisualizeGridRect.inset(paddingX, paddingY);
Adam Cohenf7ca3b42021-02-22 11:03:58 -0800615 mVisualizeGridPaint.setStyle(Paint.Style.FILL);
616 canvas.drawRoundRect(mVisualizeGridRect, mGridVisualizationRoundingRadius,
617 mGridVisualizationRoundingRadius, mVisualizeGridPaint);
618 }
619 }
620 }
Adam Cohen65086992020-02-19 08:40:49 -0800621
fbarone74256b2023-04-10 14:50:31 -0700622 if (mVisualizeDropLocation) {
Adam Cohenf7ca3b42021-02-22 11:03:58 -0800623 for (int i = 0; i < mDragOutlines.length; i++) {
624 final float alpha = mDragOutlineAlphas[i];
625 if (alpha <= 0) continue;
Sebastian Franco0dd5db82023-10-13 11:09:21 -0700626 CellLayoutLayoutParams params = mDragOutlines[i];
627 cellToRect(params.getCellX(), params.getCellY(), params.cellHSpan, params.cellVSpan,
628 mTempOnDrawCellToRect);
629 mVisualizeGridRect.set(mTempOnDrawCellToRect);
630 mVisualizeGridRect.inset(paddingX, paddingY);
Adam Cohen65086992020-02-19 08:40:49 -0800631
Adam Cohenf7ca3b42021-02-22 11:03:58 -0800632 mVisualizeGridPaint.setAlpha(255);
Adam Cohen65086992020-02-19 08:40:49 -0800633 mVisualizeGridPaint.setStyle(Paint.Style.STROKE);
Adam Cohenf7ca3b42021-02-22 11:03:58 -0800634 mVisualizeGridPaint.setColor(Color.argb((int) (alpha),
635 Color.red(mGridColor), Color.green(mGridColor), Color.blue(mGridColor)));
Adam Cohen65086992020-02-19 08:40:49 -0800636
Sebastian Franco9ea36d42023-09-21 13:56:42 -0700637 canvas.save();
638 canvas.translate(getMarginForGivenCellParams(params), 0);
Adam Cohenf7ca3b42021-02-22 11:03:58 -0800639 canvas.drawRoundRect(mVisualizeGridRect, mGridVisualizationRoundingRadius,
640 mGridVisualizationRoundingRadius, mVisualizeGridPaint);
Sebastian Franco9ea36d42023-09-21 13:56:42 -0700641 canvas.restore();
Adam Cohen65086992020-02-19 08:40:49 -0800642 }
643 }
Adam Cohen69ce2e52011-07-03 19:25:21 -0700644 }
645
Sebastian Franco9ea36d42023-09-21 13:56:42 -0700646 protected float getMarginForGivenCellParams(CellLayoutLayoutParams params) {
647 return 0;
648 }
649
Adam Cohenefca0272016-02-24 19:19:06 -0800650 @Override
651 protected void dispatchDraw(Canvas canvas) {
652 super.dispatchDraw(canvas);
653
Samuel Fufa1e2d0042019-11-18 17:12:46 -0800654 for (int i = 0; i < mDelegatedCellDrawings.size(); i++) {
655 DelegatedCellDrawing bg = mDelegatedCellDrawings.get(i);
656 cellToPoint(bg.mDelegateCellX, bg.mDelegateCellY, mTempLocation);
657 canvas.save();
658 canvas.translate(mTempLocation[0], mTempLocation[1]);
659 bg.drawOverItem(canvas);
660 canvas.restore();
Adam Cohenefca0272016-02-24 19:19:06 -0800661 }
Adam Cohen69ce2e52011-07-03 19:25:21 -0700662 }
663
Samuel Fufa1e2d0042019-11-18 17:12:46 -0800664 /**
665 * Add Delegated cell drawing
666 */
667 public void addDelegatedCellDrawing(DelegatedCellDrawing bg) {
668 mDelegatedCellDrawings.add(bg);
Adam Cohenefca0272016-02-24 19:19:06 -0800669 }
Samuel Fufa1e2d0042019-11-18 17:12:46 -0800670
671 /**
672 * Remove item from DelegatedCellDrawings
673 */
674 public void removeDelegatedCellDrawing(DelegatedCellDrawing bg) {
675 mDelegatedCellDrawings.remove(bg);
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700676 }
677
Adam Cohenc51934b2011-07-26 21:07:43 -0700678 public void setFolderLeaveBehindCell(int x, int y) {
Adam Cohenefca0272016-02-24 19:19:06 -0800679 View child = getChildAt(x, y);
Sunny Goyalab770a12018-11-14 15:17:26 -0800680 mFolderLeaveBehind.setup(getContext(), mActivity, null,
Adam Cohenefca0272016-02-24 19:19:06 -0800681 child.getMeasuredWidth(), child.getPaddingTop());
682
Samuel Fufa1e2d0042019-11-18 17:12:46 -0800683 mFolderLeaveBehind.mDelegateCellX = x;
684 mFolderLeaveBehind.mDelegateCellY = y;
Adam Cohenc51934b2011-07-26 21:07:43 -0700685 invalidate();
686 }
687
688 public void clearFolderLeaveBehind() {
Samuel Fufa1e2d0042019-11-18 17:12:46 -0800689 mFolderLeaveBehind.mDelegateCellX = -1;
690 mFolderLeaveBehind.mDelegateCellY = -1;
Adam Cohenc51934b2011-07-26 21:07:43 -0700691 invalidate();
692 }
693
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700694 @Override
Michael Jurkae6235dd2011-10-04 15:02:05 -0700695 public boolean shouldDelayChildPressedState() {
696 return false;
697 }
698
Adam Cohen1462de32012-07-24 22:34:36 -0700699 public void restoreInstanceState(SparseArray<Parcelable> states) {
Sunny Goyal33a152f2014-07-22 12:13:14 -0700700 try {
701 dispatchRestoreInstanceState(states);
702 } catch (IllegalArgumentException ex) {
Zak Cohen3eeb41d2020-02-14 14:15:13 -0800703 if (FeatureFlags.IS_STUDIO_BUILD) {
Sunny Goyal33a152f2014-07-22 12:13:14 -0700704 throw ex;
705 }
706 // Mismatched viewId / viewType preventing restore. Skip restore on production builds.
707 Log.e(TAG, "Ignoring an error while restoring a view instance state", ex);
708 }
Adam Cohen1462de32012-07-24 22:34:36 -0700709 }
710
Michael Jurkae6235dd2011-10-04 15:02:05 -0700711 @Override
Jeff Sharkey83f111d2009-04-20 21:03:13 -0700712 public void cancelLongPress() {
713 super.cancelLongPress();
714
715 // Cancel long press for all children
716 final int count = getChildCount();
717 for (int i = 0; i < count; i++) {
718 final View child = getChildAt(i);
719 child.cancelLongPress();
720 }
721 }
722
Michael Jurkadee05892010-07-27 10:01:56 -0700723 public void setOnInterceptTouchListener(View.OnTouchListener listener) {
724 mInterceptTouchListener = listener;
725 }
726
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800727 public int getCountX() {
Adam Cohend22015c2010-07-26 22:02:18 -0700728 return mCountX;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800729 }
730
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800731 public int getCountY() {
Adam Cohend22015c2010-07-26 22:02:18 -0700732 return mCountY;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800733 }
734
Sunny Goyalc13403c2016-11-18 23:44:48 -0800735 public boolean acceptsWidget() {
736 return mContainerType == WORKSPACE;
Sunny Goyale9b651e2015-04-24 11:44:51 -0700737 }
738
Sebastian Francod4682992022-10-05 13:03:09 -0500739 /**
740 * Adds the given view to the CellLayout
741 *
742 * @param child view to add.
743 * @param index index of the CellLayout children where to add the view.
744 * @param childId id of the view.
745 * @param params represent the logic of the view on the CellLayout.
746 * @param markCells if the occupied cells should be marked or not
747 * @return if adding the view was successful
748 */
749 public boolean addViewToCellLayout(View child, int index, int childId,
750 CellLayoutLayoutParams params, boolean markCells) {
751 final CellLayoutLayoutParams lp = params;
Winson Chungaafa03c2010-06-11 17:34:16 -0700752
Andrew Flynnde38e422012-05-08 11:22:15 -0700753 // Hotseat icons - remove text
Andrew Flynn0dca1ec2012-02-29 13:33:22 -0800754 if (child instanceof BubbleTextView) {
755 BubbleTextView bubbleChild = (BubbleTextView) child;
Jon Mirandaf1eae802017-10-04 11:23:33 -0700756 bubbleChild.setTextVisibility(mContainerType != HOTSEAT);
Andrew Flynn0dca1ec2012-02-29 13:33:22 -0800757 }
758
Sebastian Francof7654252024-01-18 11:50:50 -0800759 child.setScaleX(DEFAULT_SCALE);
760 child.setScaleY(DEFAULT_SCALE);
Adam Cohen307fe232012-08-16 17:55:58 -0700761
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800762 // Generate an id for each view, this assumes we have at most 256x256 cells
763 // per workspace screen
Sebastian Franco877088e2023-01-03 15:16:22 -0700764 if (lp.getCellX() >= 0 && lp.getCellX() <= mCountX - 1
765 && lp.getCellY() >= 0 && lp.getCellY() <= mCountY - 1) {
Winson Chungaafa03c2010-06-11 17:34:16 -0700766 // If the horizontal or vertical span is set to -1, it is taken to
767 // mean that it spans the extent of the CellLayout
Adam Cohend22015c2010-07-26 22:02:18 -0700768 if (lp.cellHSpan < 0) lp.cellHSpan = mCountX;
769 if (lp.cellVSpan < 0) lp.cellVSpan = mCountY;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800770
Winson Chungaafa03c2010-06-11 17:34:16 -0700771 child.setId(childId);
Tony Wickhama0628cc2015-10-14 15:23:04 -0700772 if (LOGD) {
773 Log.d(TAG, "Adding view to ShortcutsAndWidgetsContainer: " + child);
774 }
Sunny Goyalf7a29e82015-04-24 15:20:43 -0700775 mShortcutsAndWidgets.addView(child, index, lp);
Michael Jurkadee05892010-07-27 10:01:56 -0700776
Michael Jurkaf3ca3ab2010-10-20 17:08:24 -0700777 if (markCells) markCellsAsOccupiedForView(child);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700778
Winson Chungaafa03c2010-06-11 17:34:16 -0700779 return true;
780 }
781 return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800782 }
Michael Jurka3e7c7632010-10-02 16:01:03 -0700783
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800784 @Override
Michael Jurka0280c3b2010-09-17 15:00:07 -0700785 public void removeAllViews() {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -0700786 mOccupied.clear();
Michael Jurkaa52570f2012-03-20 03:18:20 -0700787 mShortcutsAndWidgets.removeAllViews();
Michael Jurka0280c3b2010-09-17 15:00:07 -0700788 }
789
790 @Override
791 public void removeAllViewsInLayout() {
Michael Jurkaa52570f2012-03-20 03:18:20 -0700792 if (mShortcutsAndWidgets.getChildCount() > 0) {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -0700793 mOccupied.clear();
Michael Jurkaa52570f2012-03-20 03:18:20 -0700794 mShortcutsAndWidgets.removeAllViewsInLayout();
Michael Jurka7cfc2822011-08-02 20:19:24 -0700795 }
Michael Jurka0280c3b2010-09-17 15:00:07 -0700796 }
797
798 @Override
799 public void removeView(View view) {
800 markCellsAsUnoccupiedForView(view);
Michael Jurkaa52570f2012-03-20 03:18:20 -0700801 mShortcutsAndWidgets.removeView(view);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700802 }
803
804 @Override
805 public void removeViewAt(int index) {
Michael Jurkaa52570f2012-03-20 03:18:20 -0700806 markCellsAsUnoccupiedForView(mShortcutsAndWidgets.getChildAt(index));
807 mShortcutsAndWidgets.removeViewAt(index);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700808 }
809
810 @Override
811 public void removeViewInLayout(View view) {
812 markCellsAsUnoccupiedForView(view);
Michael Jurkaa52570f2012-03-20 03:18:20 -0700813 mShortcutsAndWidgets.removeViewInLayout(view);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700814 }
815
816 @Override
817 public void removeViews(int start, int count) {
818 for (int i = start; i < start + count; i++) {
Michael Jurkaa52570f2012-03-20 03:18:20 -0700819 markCellsAsUnoccupiedForView(mShortcutsAndWidgets.getChildAt(i));
Michael Jurka0280c3b2010-09-17 15:00:07 -0700820 }
Michael Jurkaa52570f2012-03-20 03:18:20 -0700821 mShortcutsAndWidgets.removeViews(start, count);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700822 }
823
824 @Override
825 public void removeViewsInLayout(int start, int count) {
826 for (int i = start; i < start + count; i++) {
Michael Jurkaa52570f2012-03-20 03:18:20 -0700827 markCellsAsUnoccupiedForView(mShortcutsAndWidgets.getChildAt(i));
Michael Jurka0280c3b2010-09-17 15:00:07 -0700828 }
Michael Jurkaa52570f2012-03-20 03:18:20 -0700829 mShortcutsAndWidgets.removeViewsInLayout(start, count);
Michael Jurkaabded662011-03-04 12:06:57 -0800830 }
831
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700832 /**
Winson Chungaafa03c2010-06-11 17:34:16 -0700833 * Given a point, return the cell that strictly encloses that point
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800834 * @param x X coordinate of the point
835 * @param y Y coordinate of the point
836 * @param result Array of 2 ints to hold the x and y coordinate of the cell
837 */
Sunny Goyale9b651e2015-04-24 11:44:51 -0700838 public void pointToCellExact(int x, int y, int[] result) {
Winson Chung4b825dcd2011-06-19 12:41:22 -0700839 final int hStartPadding = getPaddingLeft();
840 final int vStartPadding = getPaddingTop();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800841
Sebastian Francob57c0b22022-06-28 13:54:35 -0700842 result[0] = (x - hStartPadding) / (mCellWidth + mBorderSpace.x);
843 result[1] = (y - vStartPadding) / (mCellHeight + mBorderSpace.y);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800844
Adam Cohend22015c2010-07-26 22:02:18 -0700845 final int xAxis = mCountX;
846 final int yAxis = mCountY;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800847
848 if (result[0] < 0) result[0] = 0;
849 if (result[0] >= xAxis) result[0] = xAxis - 1;
850 if (result[1] < 0) result[1] = 0;
851 if (result[1] >= yAxis) result[1] = yAxis - 1;
852 }
Winson Chungaafa03c2010-06-11 17:34:16 -0700853
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800854 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800855 * Given a cell coordinate, return the point that represents the upper left corner of that cell
Winson Chungaafa03c2010-06-11 17:34:16 -0700856 *
857 * @param cellX X coordinate of the cell
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800858 * @param cellY Y coordinate of the cell
Winson Chungaafa03c2010-06-11 17:34:16 -0700859 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800860 * @param result Array of 2 ints to hold the x and y coordinate of the point
861 */
862 void cellToPoint(int cellX, int cellY, int[] result) {
Jon Miranda228877d2021-02-09 11:05:00 -0500863 cellToRect(cellX, cellY, 1, 1, mTempRect);
864 result[0] = mTempRect.left;
865 result[1] = mTempRect.top;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800866 }
867
Adam Cohene3e27a82011-04-15 12:07:39 -0700868 /**
Adam Cohen482ed822012-03-02 14:15:13 -0800869 * Given a cell coordinate, return the point that represents the center of the cell
Adam Cohene3e27a82011-04-15 12:07:39 -0700870 *
871 * @param cellX X coordinate of the cell
872 * @param cellY Y coordinate of the cell
873 *
874 * @param result Array of 2 ints to hold the x and y coordinate of the point
875 */
876 void cellToCenterPoint(int cellX, int cellY, int[] result) {
Adam Cohen47a876d2012-03-19 13:21:41 -0700877 regionToCenterPoint(cellX, cellY, 1, 1, result);
878 }
879
880 /**
Tony Wickham0ac045f2021-11-03 13:17:02 -0700881 * Given a cell coordinate and span return the point that represents the center of the region
Adam Cohen47a876d2012-03-19 13:21:41 -0700882 *
883 * @param cellX X coordinate of the cell
884 * @param cellY Y coordinate of the cell
885 *
886 * @param result Array of 2 ints to hold the x and y coordinate of the point
887 */
Sebastian Franco45b39b52023-01-10 10:47:46 -0600888 public void regionToCenterPoint(int cellX, int cellY, int spanX, int spanY, int[] result) {
Jon Miranda228877d2021-02-09 11:05:00 -0500889 cellToRect(cellX, cellY, spanX, spanY, mTempRect);
890 result[0] = mTempRect.centerX();
891 result[1] = mTempRect.centerY();
Adam Cohen19f37922012-03-21 11:59:11 -0700892 }
893
Tony Wickham3cfa5ed2021-11-03 13:20:43 -0700894 /**
895 * Returns the distance between the given coordinate and the visual center of the given cell.
896 */
897 public float getDistanceFromWorkspaceCellVisualCenter(float x, float y, int[] cell) {
898 getWorkspaceCellVisualCenter(cell[0], cell[1], mTmpPoint);
Sunny Goyalf7a29e82015-04-24 15:20:43 -0700899 return (float) Math.hypot(x - mTmpPoint[0], y - mTmpPoint[1]);
Adam Cohen482ed822012-03-02 14:15:13 -0800900 }
901
Tony Wickham3cfa5ed2021-11-03 13:20:43 -0700902 private void getWorkspaceCellVisualCenter(int cellX, int cellY, int[] outPoint) {
903 View child = getChildAt(cellX, cellY);
904 if (child instanceof DraggableView) {
905 DraggableView draggableChild = (DraggableView) child;
906 if (draggableChild.getViewType() == DRAGGABLE_ICON) {
907 cellToPoint(cellX, cellY, outPoint);
908 draggableChild.getWorkspaceVisualDragBounds(mTempRect);
909 mTempRect.offset(outPoint[0], outPoint[1]);
910 outPoint[0] = mTempRect.centerX();
911 outPoint[1] = mTempRect.centerY();
912 return;
913 }
914 }
915 cellToCenterPoint(cellX, cellY, outPoint);
916 }
917
Tony Wickham0ac045f2021-11-03 13:17:02 -0700918 /**
919 * Returns the max distance from the center of a cell that can accept a drop to create a folder.
920 */
Tony Wickham12784902021-11-03 14:02:10 -0700921 public float getFolderCreationRadius(int[] targetCell) {
Tony Wickham0ac045f2021-11-03 13:17:02 -0700922 DeviceProfile grid = mActivity.getDeviceProfile();
Tony Wickham12784902021-11-03 14:02:10 -0700923 float iconVisibleRadius = ICON_VISIBLE_AREA_FACTOR * grid.iconSizePx / 2;
924 // Halfway between reorder radius and icon.
Sebastian Franco6e1024e2022-07-29 13:46:49 -0700925 return (getReorderRadius(targetCell, 1, 1) + iconVisibleRadius) / 2;
Tony Wickham12784902021-11-03 14:02:10 -0700926 }
927
928 /**
929 * Returns the max distance from the center of a cell that will start to reorder on drag over.
930 */
Sebastian Franco6e1024e2022-07-29 13:46:49 -0700931 public float getReorderRadius(int[] targetCell, int spanX, int spanY) {
Tony Wickham12784902021-11-03 14:02:10 -0700932 int[] centerPoint = mTmpPoint;
933 getWorkspaceCellVisualCenter(targetCell[0], targetCell[1], centerPoint);
934
935 Rect cellBoundsWithSpacing = mTempRect;
Sebastian Franco6e1024e2022-07-29 13:46:49 -0700936 cellToRect(targetCell[0], targetCell[1], spanX, spanY, cellBoundsWithSpacing);
Tony Wickham12784902021-11-03 14:02:10 -0700937 cellBoundsWithSpacing.inset(-mBorderSpace.x / 2, -mBorderSpace.y / 2);
938
Sebastian Francoc8392ea2022-10-28 16:38:37 -0700939 if (canCreateFolder(getChildAt(targetCell[0], targetCell[1])) && spanX == 1 && spanY == 1) {
Tony Wickham12784902021-11-03 14:02:10 -0700940 // Take only the circle in the smaller dimension, to ensure we don't start reordering
941 // too soon before accepting a folder drop.
942 int minRadius = centerPoint[0] - cellBoundsWithSpacing.left;
943 minRadius = Math.min(minRadius, centerPoint[1] - cellBoundsWithSpacing.top);
944 minRadius = Math.min(minRadius, cellBoundsWithSpacing.right - centerPoint[0]);
945 minRadius = Math.min(minRadius, cellBoundsWithSpacing.bottom - centerPoint[1]);
946 return minRadius;
947 }
948 // Take up the entire cell, including space between this cell and the adjacent ones.
Sebastian Francoc8392ea2022-10-28 16:38:37 -0700949 // Multiply by span to scale radius
950 return (float) Math.hypot(spanX * cellBoundsWithSpacing.width() / 2f,
951 spanY * cellBoundsWithSpacing.height() / 2f);
Tony Wickham0ac045f2021-11-03 13:17:02 -0700952 }
953
Adam Cohenf9c184a2016-01-15 16:47:43 -0800954 public int getCellWidth() {
Romain Guy84f296c2009-11-04 15:00:44 -0800955 return mCellWidth;
956 }
957
Sunny Goyal0b754e52017-08-07 07:42:45 -0700958 public int getCellHeight() {
Romain Guy84f296c2009-11-04 15:00:44 -0800959 return mCellHeight;
960 }
961
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700962 public void setFixedSize(int width, int height) {
963 mFixedWidth = width;
964 mFixedHeight = height;
965 }
966
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800967 @Override
968 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800969 int widthSpecMode = MeasureSpec.getMode(widthMeasureSpec);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800970 int heightSpecMode = MeasureSpec.getMode(heightMeasureSpec);
Winson Chung5f8afe62013-08-12 16:19:28 -0700971 int widthSize = MeasureSpec.getSize(widthMeasureSpec);
972 int heightSize = MeasureSpec.getSize(heightMeasureSpec);
Winson Chung2d75f122013-09-23 16:53:31 -0700973 int childWidthSize = widthSize - (getPaddingLeft() + getPaddingRight());
974 int childHeightSize = heightSize - (getPaddingTop() + getPaddingBottom());
Sunny Goyalae6e3182019-04-30 12:04:37 -0700975
Winson Chung11a1a532013-09-13 11:14:45 -0700976 if (mFixedCellWidth < 0 || mFixedCellHeight < 0) {
Thales Lima78d00ad2021-09-30 11:29:06 +0100977 int cw = DeviceProfile.calculateCellWidth(childWidthSize, mBorderSpace.x,
Jon Miranda228877d2021-02-09 11:05:00 -0500978 mCountX);
Thales Lima78d00ad2021-09-30 11:29:06 +0100979 int ch = DeviceProfile.calculateCellHeight(childHeightSize, mBorderSpace.y,
Jon Miranda228877d2021-02-09 11:05:00 -0500980 mCountY);
Winson Chung11a1a532013-09-13 11:14:45 -0700981 if (cw != mCellWidth || ch != mCellHeight) {
982 mCellWidth = cw;
983 mCellHeight = ch;
Jon Miranda228877d2021-02-09 11:05:00 -0500984 mShortcutsAndWidgets.setCellDimensions(mCellWidth, mCellHeight, mCountX, mCountY,
Thales Lima78d00ad2021-09-30 11:29:06 +0100985 mBorderSpace);
Winson Chung11a1a532013-09-13 11:14:45 -0700986 }
Winson Chung5f8afe62013-08-12 16:19:28 -0700987 }
Winson Chungaafa03c2010-06-11 17:34:16 -0700988
Winson Chung2d75f122013-09-23 16:53:31 -0700989 int newWidth = childWidthSize;
990 int newHeight = childHeightSize;
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700991 if (mFixedWidth > 0 && mFixedHeight > 0) {
992 newWidth = mFixedWidth;
993 newHeight = mFixedHeight;
994 } else if (widthSpecMode == MeasureSpec.UNSPECIFIED || heightSpecMode == MeasureSpec.UNSPECIFIED) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800995 throw new RuntimeException("CellLayout cannot have UNSPECIFIED dimensions");
996 }
997
Sunny Goyal4fe5a372015-05-14 19:55:10 -0700998 mShortcutsAndWidgets.measure(
999 MeasureSpec.makeMeasureSpec(newWidth, MeasureSpec.EXACTLY),
1000 MeasureSpec.makeMeasureSpec(newHeight, MeasureSpec.EXACTLY));
1001
1002 int maxWidth = mShortcutsAndWidgets.getMeasuredWidth();
1003 int maxHeight = mShortcutsAndWidgets.getMeasuredHeight();
Winson Chung2d75f122013-09-23 16:53:31 -07001004 if (mFixedWidth > 0 && mFixedHeight > 0) {
1005 setMeasuredDimension(maxWidth, maxHeight);
1006 } else {
1007 setMeasuredDimension(widthSize, heightSize);
1008 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001009 }
1010
1011 @Override
Michael Jurka28750fb2010-09-24 17:43:49 -07001012 protected void onLayout(boolean changed, int l, int t, int r, int b) {
Stefan Andonianfb348912024-08-14 22:51:32 +00001013 mHasOnLayoutBeenCalled = true; // b/349929393 - is the required call to onLayout not done?
Tony Wickham26b01422015-11-10 14:44:32 -08001014 int left = getPaddingLeft();
Sunny Goyal7ce471b2017-08-02 03:37:39 -07001015 left += (int) Math.ceil(getUnusedHorizontalSpace() / 2f);
Sunny Goyal7c786f72016-06-01 14:08:21 -07001016 int right = r - l - getPaddingRight();
Sunny Goyal7ce471b2017-08-02 03:37:39 -07001017 right -= (int) Math.ceil(getUnusedHorizontalSpace() / 2f);
Sunny Goyal7c786f72016-06-01 14:08:21 -07001018
Winson Chung38848ca2013-10-08 12:03:44 -07001019 int top = getPaddingTop();
Sunny Goyal7c786f72016-06-01 14:08:21 -07001020 int bottom = b - t - getPaddingBottom();
Sunny Goyal4fe5a372015-05-14 19:55:10 -07001021
Sunny Goyal7c786f72016-06-01 14:08:21 -07001022 // Expand the background drawing bounds by the padding baked into the background drawable
1023 mBackground.getPadding(mTempRect);
1024 mBackground.setBounds(
Jon Miranda28032002017-07-13 16:18:56 -07001025 left - mTempRect.left - getPaddingLeft(),
1026 top - mTempRect.top - getPaddingTop(),
1027 right + mTempRect.right + getPaddingRight(),
1028 bottom + mTempRect.bottom + getPaddingBottom());
Sunny Goyalae6e3182019-04-30 12:04:37 -07001029
Sunny Goyalc4d32012020-04-03 17:10:11 -07001030 mShortcutsAndWidgets.layout(left, top, right, bottom);
Sunny Goyal7c786f72016-06-01 14:08:21 -07001031 }
1032
Tony Wickhama501d492015-11-03 18:05:01 -08001033 /**
1034 * Returns the amount of space left over after subtracting padding and cells. This space will be
1035 * very small, a few pixels at most, and is a result of rounding down when calculating the cell
Jon Miranda228877d2021-02-09 11:05:00 -05001036 * width in {@link DeviceProfile#calculateCellWidth(int, int, int)}.
Tony Wickhama501d492015-11-03 18:05:01 -08001037 */
1038 public int getUnusedHorizontalSpace() {
Jon Miranda228877d2021-02-09 11:05:00 -05001039 return getMeasuredWidth() - getPaddingLeft() - getPaddingRight() - (mCountX * mCellWidth)
Thales Lima78d00ad2021-09-30 11:29:06 +01001040 - ((mCountX - 1) * mBorderSpace.x);
Tony Wickhama501d492015-11-03 18:05:01 -08001041 }
1042
Sunny Goyal2805e632015-05-20 15:35:32 -07001043 @Override
1044 protected boolean verifyDrawable(Drawable who) {
Sunny Goyal7ce471b2017-08-02 03:37:39 -07001045 return super.verifyDrawable(who) || (who == mBackground);
Sunny Goyal2805e632015-05-20 15:35:32 -07001046 }
1047
Michael Jurkaa52570f2012-03-20 03:18:20 -07001048 public ShortcutAndWidgetContainer getShortcutsAndWidgets() {
Sunny Goyaldcbcc862014-08-12 15:58:36 -07001049 return mShortcutsAndWidgets;
Michael Jurkaa52570f2012-03-20 03:18:20 -07001050 }
1051
Jon Miranda228877d2021-02-09 11:05:00 -05001052 public View getChildAt(int cellX, int cellY) {
1053 return mShortcutsAndWidgets.getChildAt(cellX, cellY);
Patrick Dubroy440c3602010-07-13 17:50:32 -07001054 }
1055
Adam Cohen76fc0852011-06-17 13:26:23 -07001056 public boolean animateChildToPosition(final View child, int cellX, int cellY, int duration,
Adam Cohen482ed822012-03-02 14:15:13 -08001057 int delay, boolean permanent, boolean adjustOccupied) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07001058 ShortcutAndWidgetContainer clc = getShortcutsAndWidgets();
Adam Cohen482ed822012-03-02 14:15:13 -08001059
Adam Cohen1d13c0b2020-04-21 16:29:12 -07001060 if (clc.indexOfChild(child) != -1 && (child instanceof Reorderable)) {
Sebastian Francod4682992022-10-05 13:03:09 -05001061 final CellLayoutLayoutParams lp = (CellLayoutLayoutParams) child.getLayoutParams();
Adam Cohenbfbfd262011-06-13 16:55:12 -07001062 final ItemInfo info = (ItemInfo) child.getTag();
Adam Cohen1d13c0b2020-04-21 16:29:12 -07001063 final Reorderable item = (Reorderable) child;
Adam Cohenbfbfd262011-06-13 16:55:12 -07001064
1065 // We cancel any existing animations
1066 if (mReorderAnimators.containsKey(lp)) {
1067 mReorderAnimators.get(lp).cancel();
1068 mReorderAnimators.remove(lp);
1069 }
1070
Adam Cohen1d13c0b2020-04-21 16:29:12 -07001071
Adam Cohen482ed822012-03-02 14:15:13 -08001072 if (adjustOccupied) {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001073 GridOccupancy occupied = permanent ? mOccupied : mTmpOccupied;
Sebastian Franco877088e2023-01-03 15:16:22 -07001074 occupied.markCells(lp.getCellX(), lp.getCellY(), lp.cellHSpan, lp.cellVSpan, false);
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001075 occupied.markCells(cellX, cellY, lp.cellHSpan, lp.cellVSpan, true);
Adam Cohen482ed822012-03-02 14:15:13 -08001076 }
Adam Cohen1d13c0b2020-04-21 16:29:12 -07001077
1078 // Compute the new x and y position based on the new cellX and cellY
1079 // We leverage the actual layout logic in the layout params and hence need to modify
1080 // state and revert that state.
1081 final int oldX = lp.x;
1082 final int oldY = lp.y;
Adam Cohenbfbfd262011-06-13 16:55:12 -07001083 lp.isLockedToGrid = true;
Adam Cohen482ed822012-03-02 14:15:13 -08001084 if (permanent) {
Sunny Goyal669b71f2023-01-27 14:37:07 -08001085 lp.setCellX(cellX);
1086 lp.setCellY(cellY);
Adam Cohen482ed822012-03-02 14:15:13 -08001087 } else {
Sebastian Franco877088e2023-01-03 15:16:22 -07001088 lp.setTmpCellX(cellX);
1089 lp.setTmpCellY(cellY);
Adam Cohen482ed822012-03-02 14:15:13 -08001090 }
Jon Mirandae96798e2016-12-07 12:10:44 -08001091 clc.setupLp(child);
Adam Cohen482ed822012-03-02 14:15:13 -08001092 final int newX = lp.x;
1093 final int newY = lp.y;
Adam Cohen76fc0852011-06-17 13:26:23 -07001094 lp.x = oldX;
1095 lp.y = oldY;
Adam Cohen1d13c0b2020-04-21 16:29:12 -07001096 lp.isLockedToGrid = false;
1097 // End compute new x and y
1098
Sunny Goyal82dfc152023-02-24 16:50:09 -08001099 MultiTranslateDelegate mtd = item.getTranslateDelegate();
1100 float initPreviewOffsetX = mtd.getTranslationX(INDEX_REORDER_PREVIEW_OFFSET).getValue();
1101 float initPreviewOffsetY = mtd.getTranslationY(INDEX_REORDER_PREVIEW_OFFSET).getValue();
Adam Cohen1d13c0b2020-04-21 16:29:12 -07001102 final float finalPreviewOffsetX = newX - oldX;
1103 final float finalPreviewOffsetY = newY - oldY;
1104
Adam Cohen482ed822012-03-02 14:15:13 -08001105 // Exit early if we're not actually moving the view
Adam Cohen1d13c0b2020-04-21 16:29:12 -07001106 if (finalPreviewOffsetX == 0 && finalPreviewOffsetY == 0
1107 && initPreviewOffsetX == 0 && initPreviewOffsetY == 0) {
Adam Cohen482ed822012-03-02 14:15:13 -08001108 lp.isLockedToGrid = true;
1109 return true;
1110 }
1111
Sunny Goyal849c6a22018-08-08 16:33:46 -07001112 ValueAnimator va = ValueAnimator.ofFloat(0f, 1f);
Adam Cohen482ed822012-03-02 14:15:13 -08001113 va.setDuration(duration);
1114 mReorderAnimators.put(lp, va);
1115
1116 va.addUpdateListener(new AnimatorUpdateListener() {
1117 @Override
Adam Cohenbfbfd262011-06-13 16:55:12 -07001118 public void onAnimationUpdate(ValueAnimator animation) {
Jon Mirandae96798e2016-12-07 12:10:44 -08001119 float r = (Float) animation.getAnimatedValue();
Adam Cohen1d13c0b2020-04-21 16:29:12 -07001120 float x = (1 - r) * initPreviewOffsetX + r * finalPreviewOffsetX;
1121 float y = (1 - r) * initPreviewOffsetY + r * finalPreviewOffsetY;
Sunny Goyal82dfc152023-02-24 16:50:09 -08001122 item.getTranslateDelegate().setTranslation(INDEX_REORDER_PREVIEW_OFFSET, x, y);
Adam Cohenbfbfd262011-06-13 16:55:12 -07001123 }
1124 });
Adam Cohen482ed822012-03-02 14:15:13 -08001125 va.addListener(new AnimatorListenerAdapter() {
Adam Cohenbfbfd262011-06-13 16:55:12 -07001126 boolean cancelled = false;
1127 public void onAnimationEnd(Animator animation) {
1128 // If the animation was cancelled, it means that another animation
1129 // has interrupted this one, and we don't want to lock the item into
1130 // place just yet.
1131 if (!cancelled) {
1132 lp.isLockedToGrid = true;
Sunny Goyal82dfc152023-02-24 16:50:09 -08001133 item.getTranslateDelegate()
1134 .setTranslation(INDEX_REORDER_PREVIEW_OFFSET, 0, 0);
Adam Cohen482ed822012-03-02 14:15:13 -08001135 child.requestLayout();
Adam Cohenbfbfd262011-06-13 16:55:12 -07001136 }
1137 if (mReorderAnimators.containsKey(lp)) {
1138 mReorderAnimators.remove(lp);
1139 }
1140 }
1141 public void onAnimationCancel(Animator animation) {
1142 cancelled = true;
1143 }
1144 });
Adam Cohen482ed822012-03-02 14:15:13 -08001145 va.setStartDelay(delay);
1146 va.start();
Adam Cohenbfbfd262011-06-13 16:55:12 -07001147 return true;
1148 }
1149 return false;
1150 }
1151
Adam Cohenf7ca3b42021-02-22 11:03:58 -08001152 void visualizeDropLocation(int cellX, int cellY, int spanX, int spanY,
1153 DropTarget.DragObject dragObject) {
1154 if (mDragCell[0] != cellX || mDragCell[1] != cellY || mDragCellSpan[0] != spanX
1155 || mDragCellSpan[1] != spanY) {
Adam Cohen482ed822012-03-02 14:15:13 -08001156 mDragCell[0] = cellX;
1157 mDragCell[1] = cellY;
Adam Cohenf7ca3b42021-02-22 11:03:58 -08001158 mDragCellSpan[0] = spanX;
1159 mDragCellSpan[1] = spanY;
Steven Ng30dd1d62021-03-15 21:45:49 +00001160
Joe Onorato4be866d2010-10-10 11:26:02 -07001161 final int oldIndex = mDragOutlineCurrent;
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -07001162 mDragOutlineAnims[oldIndex].animateOut();
1163 mDragOutlineCurrent = (oldIndex + 1) % mDragOutlines.length;
Sunny Goyal106bf642015-07-16 12:18:06 -07001164
Sebastian Francod4682992022-10-05 13:03:09 -05001165 CellLayoutLayoutParams cell = mDragOutlines[mDragOutlineCurrent];
Sebastian Franco877088e2023-01-03 15:16:22 -07001166 cell.setCellX(cellX);
1167 cell.setCellY(cellY);
Adam Cohenf7ca3b42021-02-22 11:03:58 -08001168 cell.cellHSpan = spanX;
1169 cell.cellVSpan = spanY;
Adam Cohen65086992020-02-19 08:40:49 -08001170
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -07001171 mDragOutlineAnims[mDragOutlineCurrent].animateIn();
Adam Cohenf7ca3b42021-02-22 11:03:58 -08001172 invalidate();
Sunny Goyale78e3d72015-09-24 11:23:31 -07001173
1174 if (dragObject.stateAnnouncer != null) {
Sunny Goyalc13403c2016-11-18 23:44:48 -08001175 dragObject.stateAnnouncer.announce(getItemMoveDescription(cellX, cellY));
Sunny Goyale78e3d72015-09-24 11:23:31 -07001176 }
Adam Cohenf7ca3b42021-02-22 11:03:58 -08001177
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001178 }
1179 }
1180
Sunny Goyal726bee72018-03-05 12:54:24 -08001181 @SuppressLint("StringFormatMatches")
Sunny Goyalc13403c2016-11-18 23:44:48 -08001182 public String getItemMoveDescription(int cellX, int cellY) {
1183 if (mContainerType == HOTSEAT) {
1184 return getContext().getString(R.string.move_to_hotseat_position,
1185 Math.max(cellX, cellY) + 1);
1186 } else {
Andras Kloczl2dacbee2022-02-21 16:53:28 +00001187 int row = cellY + 1;
Sebastian Franco2986e0b2024-01-25 10:23:39 -08001188 int col = Utilities.isRtl(getResources()) ? mCountX - cellX : cellX + 1;
1189 int panelCount = mCellLayoutContainer.getPanelCount();
1190 int pageIndex = mCellLayoutContainer.getCellLayoutIndex(this);
Andras Kloczl2dacbee2022-02-21 16:53:28 +00001191 if (panelCount > 1) {
1192 // Increment the column if the target is on the right side of a two panel home
Andras Kloczl2dacbee2022-02-21 16:53:28 +00001193 col += (pageIndex % panelCount) * mCountX;
1194 }
Sebastian Franco930531f2022-06-16 16:49:11 -07001195 return getContext().getString(R.string.move_to_empty_cell_description, row, col,
Sebastian Franco2986e0b2024-01-25 10:23:39 -08001196 mCellLayoutContainer.getPageDescription(pageIndex));
Sunny Goyalc13403c2016-11-18 23:44:48 -08001197 }
1198 }
1199
Adam Cohene0310962011-04-18 16:15:31 -07001200 public void clearDragOutlines() {
1201 final int oldIndex = mDragOutlineCurrent;
1202 mDragOutlineAnims[oldIndex].animateOut();
Adam Cohend41fbf52012-02-16 23:53:59 -08001203 mDragCell[0] = mDragCell[1] = -1;
Adam Cohene0310962011-04-18 16:15:31 -07001204 }
1205
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001206 /**
Jeff Sharkey70864282009-04-07 21:08:40 -07001207 * Find a vacant area that will fit the given bounds nearest the requested
1208 * cell location. Uses Euclidean distance to score multiple vacant areas.
Winson Chungaafa03c2010-06-11 17:34:16 -07001209 *
Romain Guy51afc022009-05-04 18:03:43 -07001210 * @param pixelX The X location at which you want to search for a vacant area.
1211 * @param pixelY The Y location at which you want to search for a vacant area.
Adam Cohend41fbf52012-02-16 23:53:59 -08001212 * @param minSpanX The minimum horizontal span required
1213 * @param minSpanY The minimum vertical span required
1214 * @param spanX Horizontal span of the object.
1215 * @param spanY Vertical span of the object.
1216 * @param result Array in which to place the result, or null (in which case a new array will
1217 * be allocated)
1218 * @return The X, Y cell of a vacant area that can contain this object,
1219 * nearest the requested location.
1220 */
Sebastian Francoe4c03452022-12-27 14:50:02 -06001221 public int[] findNearestVacantArea(int pixelX, int pixelY, int minSpanX, int minSpanY,
1222 int spanX, int spanY, int[] result, int[] resultSpan) {
Sebastian Francob57c0b22022-06-28 13:54:35 -07001223 return findNearestArea(pixelX, pixelY, minSpanX, minSpanY, spanX, spanY, false,
Adam Cohend41fbf52012-02-16 23:53:59 -08001224 result, resultSpan);
1225 }
1226
Adam Cohend41fbf52012-02-16 23:53:59 -08001227 /**
1228 * Find a vacant area that will fit the given bounds nearest the requested
1229 * cell location. Uses Euclidean distance to score multiple vacant areas.
Sebastian Francob57c0b22022-06-28 13:54:35 -07001230 * @param relativeXPos The X location relative to the Cell layout at which you want to search
1231 * for a vacant area.
1232 * @param relativeYPos The Y location relative to the Cell layout at which you want to search
1233 * for a vacant area.
Adam Cohend41fbf52012-02-16 23:53:59 -08001234 * @param minSpanX The minimum horizontal span required
1235 * @param minSpanY The minimum vertical span required
1236 * @param spanX Horizontal span of the object.
1237 * @param spanY Vertical span of the object.
1238 * @param ignoreOccupied If true, the result can be an occupied cell
1239 * @param result Array in which to place the result, or null (in which case a new array will
1240 * be allocated)
1241 * @return The X, Y cell of a vacant area that can contain this object,
1242 * nearest the requested location.
1243 */
Sebastian Franco96c46e72023-05-08 10:04:44 -06001244 protected int[] findNearestArea(int relativeXPos, int relativeYPos, int minSpanX, int minSpanY,
Sebastian Francob57c0b22022-06-28 13:54:35 -07001245 int spanX, int spanY, boolean ignoreOccupied, int[] result, int[] resultSpan) {
Sebastian Francob57c0b22022-06-28 13:54:35 -07001246 // For items with a spanX / spanY > 1, the passed in point (relativeXPos, relativeYPos)
1247 // corresponds to the center of the item, but we are searching based on the top-left cell,
1248 // so we translate the point over to correspond to the top-left.
1249 relativeXPos = (int) (relativeXPos - (mCellWidth + mBorderSpace.x) * (spanX - 1) / 2f);
1250 relativeYPos = (int) (relativeYPos - (mCellHeight + mBorderSpace.y) * (spanY - 1) / 2f);
Adam Cohene3e27a82011-04-15 12:07:39 -07001251
Jeff Sharkey70864282009-04-07 21:08:40 -07001252 // Keep track of best-scoring drop area
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001253 final int[] bestXY = result != null ? result : new int[2];
Jeff Sharkey70864282009-04-07 21:08:40 -07001254 double bestDistance = Double.MAX_VALUE;
Adam Cohend41fbf52012-02-16 23:53:59 -08001255 final Rect bestRect = new Rect(-1, -1, -1, -1);
Rajeev Kumar9962dbe2017-06-12 12:16:20 -07001256 final Stack<Rect> validRegions = new Stack<>();
Winson Chungaafa03c2010-06-11 17:34:16 -07001257
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001258 final int countX = mCountX;
1259 final int countY = mCountY;
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001260
Adam Cohend41fbf52012-02-16 23:53:59 -08001261 if (minSpanX <= 0 || minSpanY <= 0 || spanX <= 0 || spanY <= 0 ||
1262 spanX < minSpanX || spanY < minSpanY) {
1263 return bestXY;
1264 }
1265
1266 for (int y = 0; y < countY - (minSpanY - 1); y++) {
Michael Jurkac28de512010-08-13 11:27:44 -07001267 inner:
Adam Cohend41fbf52012-02-16 23:53:59 -08001268 for (int x = 0; x < countX - (minSpanX - 1); x++) {
1269 int ySize = -1;
1270 int xSize = -1;
Sebastian Francob57c0b22022-06-28 13:54:35 -07001271 if (!ignoreOccupied) {
Adam Cohend41fbf52012-02-16 23:53:59 -08001272 // First, let's see if this thing fits anywhere
1273 for (int i = 0; i < minSpanX; i++) {
1274 for (int j = 0; j < minSpanY; j++) {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001275 if (mOccupied.cells[x + i][y + j]) {
Adam Cohendf035382011-04-11 17:22:04 -07001276 continue inner;
1277 }
Michael Jurkac28de512010-08-13 11:27:44 -07001278 }
1279 }
Adam Cohend41fbf52012-02-16 23:53:59 -08001280 xSize = minSpanX;
1281 ySize = minSpanY;
1282
1283 // We know that the item will fit at _some_ acceptable size, now let's see
1284 // how big we can make it. We'll alternate between incrementing x and y spans
1285 // until we hit a limit.
1286 boolean incX = true;
1287 boolean hitMaxX = xSize >= spanX;
1288 boolean hitMaxY = ySize >= spanY;
1289 while (!(hitMaxX && hitMaxY)) {
1290 if (incX && !hitMaxX) {
1291 for (int j = 0; j < ySize; j++) {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001292 if (x + xSize > countX -1 || mOccupied.cells[x + xSize][y + j]) {
Adam Cohend41fbf52012-02-16 23:53:59 -08001293 // We can't move out horizontally
1294 hitMaxX = true;
1295 }
1296 }
1297 if (!hitMaxX) {
1298 xSize++;
1299 }
1300 } else if (!hitMaxY) {
1301 for (int i = 0; i < xSize; i++) {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001302 if (y + ySize > countY - 1 || mOccupied.cells[x + i][y + ySize]) {
Adam Cohend41fbf52012-02-16 23:53:59 -08001303 // We can't move out vertically
1304 hitMaxY = true;
1305 }
1306 }
1307 if (!hitMaxY) {
1308 ySize++;
1309 }
1310 }
1311 hitMaxX |= xSize >= spanX;
1312 hitMaxY |= ySize >= spanY;
1313 incX = !incX;
1314 }
Michael Jurkac28de512010-08-13 11:27:44 -07001315 }
Sunny Goyal2805e632015-05-20 15:35:32 -07001316 final int[] cellXY = mTmpPoint;
Adam Cohene3e27a82011-04-15 12:07:39 -07001317 cellToCenterPoint(x, y, cellXY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001318
Adam Cohend41fbf52012-02-16 23:53:59 -08001319 // We verify that the current rect is not a sub-rect of any of our previous
1320 // candidates. In this case, the current rect is disqualified in favour of the
1321 // containing rect.
Sebastian Franco4a922672022-10-27 16:42:24 -07001322 Rect currentRect = new Rect(x, y, x + xSize, y + ySize);
Adam Cohend41fbf52012-02-16 23:53:59 -08001323 boolean contained = false;
1324 for (Rect r : validRegions) {
1325 if (r.contains(currentRect)) {
1326 contained = true;
1327 break;
1328 }
1329 }
1330 validRegions.push(currentRect);
Sebastian Francob57c0b22022-06-28 13:54:35 -07001331 double distance = Math.hypot(cellXY[0] - relativeXPos, cellXY[1] - relativeYPos);
Adam Cohen482ed822012-03-02 14:15:13 -08001332
Adam Cohend41fbf52012-02-16 23:53:59 -08001333 if ((distance <= bestDistance && !contained) ||
1334 currentRect.contains(bestRect)) {
Michael Jurkac28de512010-08-13 11:27:44 -07001335 bestDistance = distance;
1336 bestXY[0] = x;
1337 bestXY[1] = y;
Adam Cohend41fbf52012-02-16 23:53:59 -08001338 if (resultSpan != null) {
1339 resultSpan[0] = xSize;
1340 resultSpan[1] = ySize;
1341 }
1342 bestRect.set(currentRect);
Michael Jurkac28de512010-08-13 11:27:44 -07001343 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001344 }
1345 }
1346
Adam Cohenc0dcf592011-06-01 15:30:43 -07001347 // Return -1, -1 if no suitable location found
1348 if (bestDistance == Double.MAX_VALUE) {
1349 bestXY[0] = -1;
1350 bestXY[1] = -1;
Jeff Sharkey70864282009-04-07 21:08:40 -07001351 }
Adam Cohenc0dcf592011-06-01 15:30:43 -07001352 return bestXY;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001353 }
Winson Chungaafa03c2010-06-11 17:34:16 -07001354
Sebastian Francoe4c03452022-12-27 14:50:02 -06001355 public GridOccupancy getOccupied() {
1356 return mOccupied;
1357 }
1358
Adam Cohen482ed822012-03-02 14:15:13 -08001359 private void copySolutionToTempState(ItemConfiguration solution, View dragView) {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001360 mTmpOccupied.clear();
Adam Cohen482ed822012-03-02 14:15:13 -08001361
Michael Jurkaa52570f2012-03-20 03:18:20 -07001362 int childCount = mShortcutsAndWidgets.getChildCount();
Adam Cohen482ed822012-03-02 14:15:13 -08001363 for (int i = 0; i < childCount; i++) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07001364 View child = mShortcutsAndWidgets.getChildAt(i);
Adam Cohen482ed822012-03-02 14:15:13 -08001365 if (child == dragView) continue;
Sebastian Francod4682992022-10-05 13:03:09 -05001366 CellLayoutLayoutParams lp = (CellLayoutLayoutParams) child.getLayoutParams();
Adam Cohen8baab352012-03-20 17:39:21 -07001367 CellAndSpan c = solution.map.get(child);
1368 if (c != null) {
Sebastian Franco877088e2023-01-03 15:16:22 -07001369 lp.setTmpCellX(c.cellX);
1370 lp.setTmpCellY(c.cellY);
Adam Cohen8baab352012-03-20 17:39:21 -07001371 lp.cellHSpan = c.spanX;
1372 lp.cellVSpan = c.spanY;
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001373 mTmpOccupied.markCells(c, true);
Adam Cohen482ed822012-03-02 14:15:13 -08001374 }
1375 }
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001376 mTmpOccupied.markCells(solution, true);
Adam Cohen482ed822012-03-02 14:15:13 -08001377 }
1378
1379 private void animateItemsToSolution(ItemConfiguration solution, View dragView, boolean
1380 commitDragView) {
1381
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001382 GridOccupancy occupied = DESTRUCTIVE_REORDER ? mOccupied : mTmpOccupied;
1383 occupied.clear();
Adam Cohen482ed822012-03-02 14:15:13 -08001384
Michael Jurkaa52570f2012-03-20 03:18:20 -07001385 int childCount = mShortcutsAndWidgets.getChildCount();
Adam Cohen482ed822012-03-02 14:15:13 -08001386 for (int i = 0; i < childCount; i++) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07001387 View child = mShortcutsAndWidgets.getChildAt(i);
Adam Cohen482ed822012-03-02 14:15:13 -08001388 if (child == dragView) continue;
Adam Cohen8baab352012-03-20 17:39:21 -07001389 CellAndSpan c = solution.map.get(child);
1390 if (c != null) {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001391 animateChildToPosition(child, c.cellX, c.cellY, REORDER_ANIMATION_DURATION, 0,
Adam Cohen19f37922012-03-21 11:59:11 -07001392 DESTRUCTIVE_REORDER, false);
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001393 occupied.markCells(c, true);
Adam Cohen482ed822012-03-02 14:15:13 -08001394 }
1395 }
1396 if (commitDragView) {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001397 occupied.markCells(solution, true);
Adam Cohen482ed822012-03-02 14:15:13 -08001398 }
1399 }
1400
Adam Cohenfa3c58f2013-12-06 16:10:55 -08001401
1402 // This method starts or changes the reorder preview animations
1403 private void beginOrAdjustReorderPreviewAnimations(ItemConfiguration solution,
Adam Cohen65086992020-02-19 08:40:49 -08001404 View dragView, int mode) {
Adam Cohen19f37922012-03-21 11:59:11 -07001405 int childCount = mShortcutsAndWidgets.getChildCount();
Adam Cohen19f37922012-03-21 11:59:11 -07001406 for (int i = 0; i < childCount; i++) {
1407 View child = mShortcutsAndWidgets.getChildAt(i);
1408 if (child == dragView) continue;
1409 CellAndSpan c = solution.map.get(child);
Sebastian Franco5f0af4f2023-11-21 10:45:45 -06001410 boolean skip = mode == ReorderPreviewAnimation.MODE_HINT
1411 && !solution.intersectingViews.contains(child);
Adam Cohend9162062020-03-24 16:35:35 -07001412
Sebastian Francod4682992022-10-05 13:03:09 -05001413 CellLayoutLayoutParams lp = (CellLayoutLayoutParams) child.getLayoutParams();
Adam Cohend9162062020-03-24 16:35:35 -07001414 if (c != null && !skip && (child instanceof Reorderable)) {
Sebastian Francof7654252024-01-18 11:50:50 -08001415 ReorderPreviewAnimation rha = new ReorderPreviewAnimation(child, mode,
1416 lp.getCellX(), lp.getCellY(), c.cellX, c.cellY, c.spanX, c.spanY,
1417 mReorderPreviewAnimationMagnitude, this, mShakeAnimators);
Adam Cohend024f982012-05-23 18:26:45 -07001418 rha.animate();
Adam Cohen19f37922012-03-21 11:59:11 -07001419 }
1420 }
1421 }
1422
Adam Cohenfa3c58f2013-12-06 16:10:55 -08001423 private void completeAndClearReorderPreviewAnimations() {
1424 for (ReorderPreviewAnimation a: mShakeAnimators.values()) {
Adam Cohend9162062020-03-24 16:35:35 -07001425 a.finishAnimation();
Adam Cohen19f37922012-03-21 11:59:11 -07001426 }
1427 mShakeAnimators.clear();
1428 }
1429
Sunny Goyal711c5962021-06-23 12:36:18 -07001430 private void commitTempPlacement(View dragView) {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001431 mTmpOccupied.copyTo(mOccupied);
Sunny Goyalaa8ef112015-06-12 20:04:41 -07001432
Sebastian Franco2986e0b2024-01-25 10:23:39 -08001433 int screenId = mCellLayoutContainer.getCellLayoutId(this);
Sunny Goyalaa8ef112015-06-12 20:04:41 -07001434 int container = Favorites.CONTAINER_DESKTOP;
1435
Sunny Goyalc13403c2016-11-18 23:44:48 -08001436 if (mContainerType == HOTSEAT) {
Sunny Goyalaa8ef112015-06-12 20:04:41 -07001437 screenId = -1;
1438 container = Favorites.CONTAINER_HOTSEAT;
1439 }
1440
Michael Jurkaa52570f2012-03-20 03:18:20 -07001441 int childCount = mShortcutsAndWidgets.getChildCount();
Adam Cohen482ed822012-03-02 14:15:13 -08001442 for (int i = 0; i < childCount; i++) {
Adam Cohenea889a22012-03-27 16:45:39 -07001443 View child = mShortcutsAndWidgets.getChildAt(i);
Sebastian Francod4682992022-10-05 13:03:09 -05001444 CellLayoutLayoutParams lp = (CellLayoutLayoutParams) child.getLayoutParams();
Adam Cohenea889a22012-03-27 16:45:39 -07001445 ItemInfo info = (ItemInfo) child.getTag();
Adam Cohen2acce882012-03-28 19:03:19 -07001446 // We do a null check here because the item info can be null in the case of the
1447 // AllApps button in the hotseat.
Sunny Goyal711c5962021-06-23 12:36:18 -07001448 if (info != null && child != dragView) {
Sunny Goyal669b71f2023-01-27 14:37:07 -08001449 CellPos presenterPos = mActivity.getCellPosMapper().mapModelToPresenter(info);
1450 final boolean requiresDbUpdate = (presenterPos.cellX != lp.getTmpCellX()
1451 || presenterPos.cellY != lp.getTmpCellY() || info.spanX != lp.cellHSpan
1452 || info.spanY != lp.cellVSpan || presenterPos.screenId != screenId);
Sunny Goyalaa8ef112015-06-12 20:04:41 -07001453
Sebastian Franco877088e2023-01-03 15:16:22 -07001454 lp.setCellX(lp.getTmpCellX());
Sebastian Franco877088e2023-01-03 15:16:22 -07001455 lp.setCellY(lp.getTmpCellY());
Sunny Goyalaa8ef112015-06-12 20:04:41 -07001456 if (requiresDbUpdate) {
Sunny Goyalab770a12018-11-14 15:17:26 -08001457 Launcher.cast(mActivity).getModelWriter().modifyItemInDatabase(info, container,
Sunny Goyal669b71f2023-01-27 14:37:07 -08001458 screenId, lp.getCellX(), lp.getCellY(), lp.cellHSpan, lp.cellVSpan);
Sunny Goyalaa8ef112015-06-12 20:04:41 -07001459 }
Adam Cohen2acce882012-03-28 19:03:19 -07001460 }
Adam Cohen482ed822012-03-02 14:15:13 -08001461 }
1462 }
1463
Sunny Goyalf7a29e82015-04-24 15:20:43 -07001464 private void setUseTempCoords(boolean useTempCoords) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07001465 int childCount = mShortcutsAndWidgets.getChildCount();
Adam Cohen482ed822012-03-02 14:15:13 -08001466 for (int i = 0; i < childCount; i++) {
Sebastian Francod4682992022-10-05 13:03:09 -05001467 CellLayoutLayoutParams lp = (CellLayoutLayoutParams) mShortcutsAndWidgets.getChildAt(
1468 i).getLayoutParams();
Adam Cohen482ed822012-03-02 14:15:13 -08001469 lp.useTmpCoords = useTempCoords;
1470 }
1471 }
1472
Sebastian Franco5c8f8682023-11-14 09:52:41 -06001473 /**
1474 * For a given region, return the rectangle of the overlapping cell and span with the given
1475 * region including the region itself. If there is no overlap the rectangle will be
1476 * invalid i.e. -1, 0, -1, 0.
1477 */
1478 @Nullable
1479 public Rect getIntersectingRectanglesInRegion(final Rect region, final View dragView) {
1480 Rect boundingRect = new Rect(region);
Adam Cohen19f37922012-03-21 11:59:11 -07001481 Rect r1 = new Rect();
Sebastian Franco5c8f8682023-11-14 09:52:41 -06001482 boolean isOverlapping = false;
Adam Cohen19f37922012-03-21 11:59:11 -07001483 final int count = mShortcutsAndWidgets.getChildCount();
1484 for (int i = 0; i < count; i++) {
1485 View child = mShortcutsAndWidgets.getChildAt(i);
1486 if (child == dragView) continue;
Sebastian Francod4682992022-10-05 13:03:09 -05001487 CellLayoutLayoutParams
1488 lp = (CellLayoutLayoutParams) child.getLayoutParams();
Sebastian Franco877088e2023-01-03 15:16:22 -07001489 r1.set(lp.getCellX(), lp.getCellY(), lp.getCellX() + lp.cellHSpan,
1490 lp.getCellY() + lp.cellVSpan);
Sebastian Franco5c8f8682023-11-14 09:52:41 -06001491 if (Rect.intersects(region, r1)) {
1492 isOverlapping = true;
1493 boundingRect.union(r1);
Adam Cohen19f37922012-03-21 11:59:11 -07001494 }
1495 }
Sebastian Franco5c8f8682023-11-14 09:52:41 -06001496 return isOverlapping ? boundingRect : null;
Adam Cohen19f37922012-03-21 11:59:11 -07001497 }
1498
Sebastian Francoe4c03452022-12-27 14:50:02 -06001499 public boolean isNearestDropLocationOccupied(int pixelX, int pixelY, int spanX, int spanY,
Adam Cohen19f37922012-03-21 11:59:11 -07001500 View dragView, int[] result) {
Sebastián Francof9a6ac22022-11-15 22:56:37 +00001501 result = findNearestAreaIgnoreOccupied(pixelX, pixelY, spanX, spanY, result);
Sebastian Franco5c8f8682023-11-14 09:52:41 -06001502 return getIntersectingRectanglesInRegion(
1503 new Rect(result[0], result[1], result[0] + spanX, result[1] + spanY),
1504 dragView
1505 ) != null;
Adam Cohen19f37922012-03-21 11:59:11 -07001506 }
1507
1508 void revertTempState() {
Adam Cohenfa3c58f2013-12-06 16:10:55 -08001509 completeAndClearReorderPreviewAnimations();
1510 if (isItemPlacementDirty() && !DESTRUCTIVE_REORDER) {
1511 final int count = mShortcutsAndWidgets.getChildCount();
1512 for (int i = 0; i < count; i++) {
1513 View child = mShortcutsAndWidgets.getChildAt(i);
Sebastian Francod4682992022-10-05 13:03:09 -05001514 CellLayoutLayoutParams
1515 lp = (CellLayoutLayoutParams) child.getLayoutParams();
Sebastian Franco877088e2023-01-03 15:16:22 -07001516 if (lp.getTmpCellX() != lp.getCellX() || lp.getTmpCellY() != lp.getCellY()) {
1517 lp.setTmpCellX(lp.getCellX());
1518 lp.setTmpCellY(lp.getCellY());
1519 animateChildToPosition(child, lp.getCellX(), lp.getCellY(),
1520 REORDER_ANIMATION_DURATION, 0, false, false);
Adam Cohenfa3c58f2013-12-06 16:10:55 -08001521 }
Adam Cohen19f37922012-03-21 11:59:11 -07001522 }
Adam Cohenfa3c58f2013-12-06 16:10:55 -08001523 setItemPlacementDirty(false);
Adam Cohen19f37922012-03-21 11:59:11 -07001524 }
Adam Cohen19f37922012-03-21 11:59:11 -07001525 }
1526
Adam Cohenbebf0422012-04-11 18:06:28 -07001527 boolean createAreaForResize(int cellX, int cellY, int spanX, int spanY,
1528 View dragView, int[] direction, boolean commit) {
1529 int[] pixelXY = new int[2];
1530 regionToCenterPoint(cellX, cellY, spanX, spanY, pixelXY);
1531
1532 // First we determine if things have moved enough to cause a different layout
Adam Cohenfa3c58f2013-12-06 16:10:55 -08001533 ItemConfiguration swapSolution = findReorderSolution(pixelXY[0], pixelXY[1], spanX, spanY,
Charlie Anderson46122392024-01-18 17:23:46 -05001534 spanX, spanY, direction, dragView, true);
Adam Cohenbebf0422012-04-11 18:06:28 -07001535
1536 setUseTempCoords(true);
1537 if (swapSolution != null && swapSolution.isSolution) {
1538 // If we're just testing for a possible location (MODE_ACCEPT_DROP), we don't bother
1539 // committing anything or animating anything as we just want to determine if a solution
1540 // exists
1541 copySolutionToTempState(swapSolution, dragView);
1542 setItemPlacementDirty(true);
1543 animateItemsToSolution(swapSolution, dragView, commit);
1544
1545 if (commit) {
Sunny Goyal711c5962021-06-23 12:36:18 -07001546 commitTempPlacement(null);
Adam Cohenfa3c58f2013-12-06 16:10:55 -08001547 completeAndClearReorderPreviewAnimations();
Adam Cohenbebf0422012-04-11 18:06:28 -07001548 setItemPlacementDirty(false);
1549 } else {
Adam Cohenfa3c58f2013-12-06 16:10:55 -08001550 beginOrAdjustReorderPreviewAnimations(swapSolution, dragView,
Adam Cohen65086992020-02-19 08:40:49 -08001551 ReorderPreviewAnimation.MODE_PREVIEW);
Adam Cohenbebf0422012-04-11 18:06:28 -07001552 }
1553 mShortcutsAndWidgets.requestLayout();
1554 }
1555 return swapSolution.isSolution;
1556 }
1557
Sebastian Francoe4c03452022-12-27 14:50:02 -06001558 public ReorderAlgorithm createReorderAlgorithm() {
1559 return new ReorderAlgorithm(this);
1560 }
1561
Sebastian Franco09589322022-11-02 15:25:58 -07001562 protected ItemConfiguration findReorderSolution(int pixelX, int pixelY, int minSpanX,
Charlie Anderson46122392024-01-18 17:23:46 -05001563 int minSpanY, int spanX, int spanY, int[] direction, View dragView, boolean decX) {
Sebastian Franco25f8e402023-12-02 13:54:05 -06001564 ItemConfiguration configuration = new ItemConfiguration();
1565 copyCurrentStateToSolution(configuration);
1566 ReorderParameters parameters = new ReorderParameters(pixelX, pixelY, spanX, spanY, minSpanX,
1567 minSpanY, dragView, configuration);
Charlie Anderson46122392024-01-18 17:23:46 -05001568 int[] directionVector = direction != null ? direction : mDirectionVector;
1569 return createReorderAlgorithm().findReorderSolution(parameters, directionVector, decX);
Sebastian Franco09589322022-11-02 15:25:58 -07001570 }
1571
Sebastián Franco61fbd182023-11-29 21:30:43 +00001572 public void copyCurrentStateToSolution(ItemConfiguration solution) {
Sebastian Franco9cab1c32022-10-25 17:28:54 -07001573 int childCount = mShortcutsAndWidgets.getChildCount();
1574 for (int i = 0; i < childCount; i++) {
1575 View child = mShortcutsAndWidgets.getChildAt(i);
1576 CellLayoutLayoutParams lp = (CellLayoutLayoutParams) child.getLayoutParams();
Sebastián Franco61fbd182023-11-29 21:30:43 +00001577 solution.add(child,
1578 new CellAndSpan(lp.getCellX(), lp.getCellY(), lp.cellHSpan, lp.cellVSpan));
Sebastian Franco9cab1c32022-10-25 17:28:54 -07001579 }
Sebastian Franco53a15a42022-10-25 17:28:54 -07001580 }
1581
1582 /**
Sebastian Franco53a15a42022-10-25 17:28:54 -07001583 * When the user drags an Item in the workspace sometimes we need to move the items already in
1584 * the workspace to make space for the new item, this function return a solution for that
1585 * reorder.
1586 *
1587 * @param pixelX X coordinate in the screen of the dragView in pixels
1588 * @param pixelY Y coordinate in the screen of the dragView in pixels
1589 * @param minSpanX minimum horizontal span the item can be shrunk to
1590 * @param minSpanY minimum vertical span the item can be shrunk to
1591 * @param spanX occupied horizontal span
1592 * @param spanY occupied vertical span
1593 * @param dragView the view of the item being draged
1594 * @return returns a solution for the given parameters, the solution contains all the icons and
1595 * the locations they should be in the given solution.
1596 */
1597 public ItemConfiguration calculateReorder(int pixelX, int pixelY, int minSpanX, int minSpanY,
1598 int spanX, int spanY, View dragView) {
Sebastian Franco25f8e402023-12-02 13:54:05 -06001599 ItemConfiguration configuration = new ItemConfiguration();
1600 copyCurrentStateToSolution(configuration);
1601 return createReorderAlgorithm().calculateReorder(
1602 new ReorderParameters(pixelX, pixelY, spanX, spanY, minSpanX, minSpanY, dragView,
1603 configuration)
1604 );
Sebastian Franco53a15a42022-10-25 17:28:54 -07001605 }
Adam Cohen482ed822012-03-02 14:15:13 -08001606
Sebastian Franco9cab1c32022-10-25 17:28:54 -07001607 int[] performReorder(int pixelX, int pixelY, int minSpanX, int minSpanY, int spanX, int spanY,
1608 View dragView, int[] result, int[] resultSpan, int mode) {
1609 if (resultSpan == null) {
1610 resultSpan = new int[]{-1, -1};
1611 }
1612 if (result == null) {
1613 result = new int[]{-1, -1};
1614 }
Sebastian Franco5d990ee2022-11-01 16:08:24 -07001615
1616 ItemConfiguration finalSolution = null;
1617 // We want the solution to match the animation of the preview and to match the drop so we
1618 // only recalculate in mode MODE_SHOW_REORDER_HINT because that the first one to run in the
1619 // reorder cycle.
1620 if (mode == MODE_SHOW_REORDER_HINT || mPreviousSolution == null) {
1621 finalSolution = calculateReorder(pixelX, pixelY, minSpanX, minSpanY, spanX, spanY,
1622 dragView);
1623 mPreviousSolution = finalSolution;
1624 } else {
Sebastian Franco9cab1c32022-10-25 17:28:54 -07001625 finalSolution = mPreviousSolution;
1626 // We reset this vector after drop
1627 if (mode == MODE_ON_DROP || mode == MODE_ON_DROP_EXTERNAL) {
1628 mPreviousSolution = null;
1629 }
Sebastian Franco9cab1c32022-10-25 17:28:54 -07001630 }
1631
1632 if (finalSolution == null || !finalSolution.isSolution) {
1633 result[0] = result[1] = resultSpan[0] = resultSpan[1] = -1;
1634 } else {
1635 result[0] = finalSolution.cellX;
1636 result[1] = finalSolution.cellY;
1637 resultSpan[0] = finalSolution.spanX;
1638 resultSpan[1] = finalSolution.spanY;
Sebastian Franco9c743272022-11-15 15:03:25 -08001639 performReorder(finalSolution, dragView, mode);
Sebastian Franco9cab1c32022-10-25 17:28:54 -07001640 }
Sebastian Franco9cab1c32022-10-25 17:28:54 -07001641 return result;
1642 }
1643
Sebastian Franco53a15a42022-10-25 17:28:54 -07001644 /**
1645 * Animates and submits in the DB the given ItemConfiguration depending of the mode.
1646 *
1647 * @param solution represents widgets on the screen which the Workspace will animate to and
1648 * would be submitted to the database.
1649 * @param dragView view which is being dragged over the workspace that trigger the reorder
1650 * @param mode depending on the mode different animations would be played and depending on the
1651 * mode the solution would be submitted or not the database.
1652 * The possible modes are {@link MODE_SHOW_REORDER_HINT}, {@link MODE_DRAG_OVER},
1653 * {@link MODE_ON_DROP}, {@link MODE_ON_DROP_EXTERNAL}, {@link MODE_ACCEPT_DROP}
1654 * defined in {@link CellLayout}.
1655 */
Sebastian Francoe4c03452022-12-27 14:50:02 -06001656 public void performReorder(ItemConfiguration solution, View dragView, int mode) {
Adam Cohenfa3c58f2013-12-06 16:10:55 -08001657 if (mode == MODE_SHOW_REORDER_HINT) {
Sebastian Franco53a15a42022-10-25 17:28:54 -07001658 beginOrAdjustReorderPreviewAnimations(solution, dragView,
1659 ReorderPreviewAnimation.MODE_HINT);
1660 return;
1661 }
1662 // If we're just testing for a possible location (MODE_ACCEPT_DROP), we don't bother
1663 // committing anything or animating anything as we just want to determine if a solution
1664 // exists
1665 if (mode == MODE_DRAG_OVER || mode == MODE_ON_DROP || mode == MODE_ON_DROP_EXTERNAL) {
1666 if (!DESTRUCTIVE_REORDER) {
1667 setUseTempCoords(true);
1668 }
1669
1670 if (!DESTRUCTIVE_REORDER) {
1671 copySolutionToTempState(solution, dragView);
1672 }
1673 setItemPlacementDirty(true);
1674 animateItemsToSolution(solution, dragView, mode == MODE_ON_DROP);
1675
1676 if (!DESTRUCTIVE_REORDER
1677 && (mode == MODE_ON_DROP || mode == MODE_ON_DROP_EXTERNAL)) {
1678 // Since the temp solution didn't update dragView, don't commit it either
1679 commitTempPlacement(dragView);
1680 completeAndClearReorderPreviewAnimations();
1681 setItemPlacementDirty(false);
Adam Cohenfa3c58f2013-12-06 16:10:55 -08001682 } else {
Sebastian Franco53a15a42022-10-25 17:28:54 -07001683 beginOrAdjustReorderPreviewAnimations(solution, dragView,
1684 ReorderPreviewAnimation.MODE_PREVIEW);
Adam Cohenfa3c58f2013-12-06 16:10:55 -08001685 }
Adam Cohenfa3c58f2013-12-06 16:10:55 -08001686 }
1687
Sebastian Franco53a15a42022-10-25 17:28:54 -07001688 if (mode == MODE_ON_DROP && !DESTRUCTIVE_REORDER) {
Adam Cohen482ed822012-03-02 14:15:13 -08001689 setUseTempCoords(false);
1690 }
Adam Cohen482ed822012-03-02 14:15:13 -08001691
Michael Jurkaa52570f2012-03-20 03:18:20 -07001692 mShortcutsAndWidgets.requestLayout();
Adam Cohen482ed822012-03-02 14:15:13 -08001693 }
1694
Adam Cohen19f37922012-03-21 11:59:11 -07001695 void setItemPlacementDirty(boolean dirty) {
1696 mItemPlacementDirty = dirty;
Adam Cohen482ed822012-03-02 14:15:13 -08001697 }
Adam Cohen19f37922012-03-21 11:59:11 -07001698 boolean isItemPlacementDirty() {
1699 return mItemPlacementDirty;
Adam Cohen482ed822012-03-02 14:15:13 -08001700 }
1701
Sebastian Francoe4c03452022-12-27 14:50:02 -06001702 /**
Adam Cohendf035382011-04-11 17:22:04 -07001703 * Find a starting cell position that will fit the given bounds nearest the requested
1704 * cell location. Uses Euclidean distance to score multiple vacant areas.
1705 *
1706 * @param pixelX The X location at which you want to search for a vacant area.
1707 * @param pixelY The Y location at which you want to search for a vacant area.
1708 * @param spanX Horizontal span of the object.
1709 * @param spanY Vertical span of the object.
Adam Cohendf035382011-04-11 17:22:04 -07001710 * @param result Previously returned value to possibly recycle.
1711 * @return The X, Y cell of a vacant area that can contain this object,
1712 * nearest the requested location.
1713 */
Sebastián Francof9a6ac22022-11-15 22:56:37 +00001714 public int[] findNearestAreaIgnoreOccupied(int pixelX, int pixelY, int spanX, int spanY,
1715 int[] result) {
Sebastian Francob57c0b22022-06-28 13:54:35 -07001716 return findNearestArea(pixelX, pixelY, spanX, spanY, spanX, spanY, true, result, null);
Adam Cohendf035382011-04-11 17:22:04 -07001717 }
1718
Michael Jurka0280c3b2010-09-17 15:00:07 -07001719 boolean existsEmptyCell() {
1720 return findCellForSpan(null, 1, 1);
1721 }
1722
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001723 /**
Michael Jurka0280c3b2010-09-17 15:00:07 -07001724 * Finds the upper-left coordinate of the first rectangle in the grid that can
1725 * hold a cell of the specified dimensions. If intersectX and intersectY are not -1,
1726 * then this method will only return coordinates for rectangles that contain the cell
1727 * (intersectX, intersectY)
1728 *
1729 * @param cellXY The array that will contain the position of a vacant cell if such a cell
1730 * can be found.
1731 * @param spanX The horizontal span of the cell we want to find.
1732 * @param spanY The vertical span of the cell we want to find.
1733 *
1734 * @return True if a vacant cell of the specified dimension was found, false otherwise.
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001735 */
Hyunyoung Song3f471442015-04-08 19:01:34 -07001736 public boolean findCellForSpan(int[] cellXY, int spanX, int spanY) {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001737 if (cellXY == null) {
1738 cellXY = new int[2];
Michael Jurka0280c3b2010-09-17 15:00:07 -07001739 }
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001740 return mOccupied.findVacantCell(cellXY, spanX, spanY);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001741 }
1742
1743 /**
Winson Chungc07918d2011-07-01 15:35:26 -07001744 * A drag event has begun over this layout.
1745 * It may have begun over this layout (in which case onDragChild is called first),
1746 * or it may have begun on another layout.
1747 */
1748 void onDragEnter() {
Winson Chungc07918d2011-07-01 15:35:26 -07001749 mDragging = true;
Sebastian Franco5aa71ce2022-12-14 12:13:19 -06001750 mPreviousSolution = null;
Winson Chungc07918d2011-07-01 15:35:26 -07001751 }
1752
1753 /**
Michael Jurka0280c3b2010-09-17 15:00:07 -07001754 * Called when drag has left this CellLayout or has been completed (successfully or not)
1755 */
1756 void onDragExit() {
Joe Onorato4be866d2010-10-10 11:26:02 -07001757 // This can actually be called when we aren't in a drag, e.g. when adding a new
1758 // item to this layout via the customize drawer.
1759 // Guard against that case.
1760 if (mDragging) {
1761 mDragging = false;
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001762 }
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -07001763
1764 // Invalidate the drag data
Sebastian Franco5aa71ce2022-12-14 12:13:19 -06001765 mPreviousSolution = null;
Adam Cohend41fbf52012-02-16 23:53:59 -08001766 mDragCell[0] = mDragCell[1] = -1;
Adam Cohenf7ca3b42021-02-22 11:03:58 -08001767 mDragCellSpan[0] = mDragCellSpan[1] = -1;
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -07001768 mDragOutlineAnims[mDragOutlineCurrent].animateOut();
1769 mDragOutlineCurrent = (mDragOutlineCurrent + 1) % mDragOutlineAnims.length;
Adam Cohen19f37922012-03-21 11:59:11 -07001770 revertTempState();
Michael Jurka33945b22010-12-21 18:19:38 -08001771 setIsDragOverlapping(false);
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001772 }
1773
1774 /**
Winson Chungaafa03c2010-06-11 17:34:16 -07001775 * Mark a child as having been dropped.
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001776 * At the beginning of the drag operation, the child may have been on another
Patrick Dubroyce34a972010-10-19 10:34:32 -07001777 * screen, but it is re-parented before this method is called.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001778 *
1779 * @param child The child that is being dropped
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001780 */
Adam Cohen716b51e2011-06-30 12:09:54 -07001781 void onDropChild(View child) {
Romain Guyd94533d2009-08-17 10:01:15 -07001782 if (child != null) {
Sebastian Francod4682992022-10-05 13:03:09 -05001783 CellLayoutLayoutParams
1784 lp = (CellLayoutLayoutParams) child.getLayoutParams();
Romain Guy84f296c2009-11-04 15:00:44 -08001785 lp.dropped = true;
Romain Guyd94533d2009-08-17 10:01:15 -07001786 child.requestLayout();
Tony Wickham1cdb6d02015-09-17 11:08:27 -07001787 markCellsAsOccupiedForView(child);
Romain Guyd94533d2009-08-17 10:01:15 -07001788 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001789 }
1790
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001791 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001792 * Computes a bounding rectangle for a range of cells
Winson Chungaafa03c2010-06-11 17:34:16 -07001793 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001794 * @param cellX X coordinate of upper left corner expressed as a cell position
1795 * @param cellY Y coordinate of upper left corner expressed as a cell position
Winson Chungaafa03c2010-06-11 17:34:16 -07001796 * @param cellHSpan Width in cells
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001797 * @param cellVSpan Height in cells
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001798 * @param resultRect Rect into which to put the results
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001799 */
Adam Cohend41fbf52012-02-16 23:53:59 -08001800 public void cellToRect(int cellX, int cellY, int cellHSpan, int cellVSpan, Rect resultRect) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001801 final int cellWidth = mCellWidth;
1802 final int cellHeight = mCellHeight;
Winson Chungaafa03c2010-06-11 17:34:16 -07001803
Pierre Barbier de Reuille1b8bbb62021-05-19 22:45:16 +01001804 // We observe a shift of 1 pixel on the x coordinate compared to the actual cell coordinates
1805 final int hStartPadding = getPaddingLeft()
1806 + (int) Math.ceil(getUnusedHorizontalSpace() / 2f);
Winson Chung4b825dcd2011-06-19 12:41:22 -07001807 final int vStartPadding = getPaddingTop();
Winson Chungaafa03c2010-06-11 17:34:16 -07001808
Thales Lima78d00ad2021-09-30 11:29:06 +01001809 int x = hStartPadding + (cellX * mBorderSpace.x) + (cellX * cellWidth);
1810 int y = vStartPadding + (cellY * mBorderSpace.y) + (cellY * cellHeight);
Jon Miranda228877d2021-02-09 11:05:00 -05001811
Thales Lima78d00ad2021-09-30 11:29:06 +01001812 int width = cellHSpan * cellWidth + ((cellHSpan - 1) * mBorderSpace.x);
1813 int height = cellVSpan * cellHeight + ((cellVSpan - 1) * mBorderSpace.y);
Winson Chungaafa03c2010-06-11 17:34:16 -07001814
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001815 resultRect.set(x, y, x + width, y + height);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001816 }
Winson Chungaafa03c2010-06-11 17:34:16 -07001817
Adam Cohend4844c32011-02-18 19:25:06 -08001818 public void markCellsAsOccupiedForView(View view) {
Sebastian Francof153d912022-04-22 16:15:27 -05001819 if (view instanceof LauncherAppWidgetHostView
1820 && view.getTag() instanceof LauncherAppWidgetInfo) {
1821 LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) view.getTag();
Sunny Goyal669b71f2023-01-27 14:37:07 -08001822 CellPos pos = mActivity.getCellPosMapper().mapModelToPresenter(info);
1823 mOccupied.markCells(pos.cellX, pos.cellY, info.spanX, info.spanY, true);
Sebastian Francof153d912022-04-22 16:15:27 -05001824 return;
1825 }
Michael Jurkaa52570f2012-03-20 03:18:20 -07001826 if (view == null || view.getParent() != mShortcutsAndWidgets) return;
Sebastian Francod4682992022-10-05 13:03:09 -05001827 CellLayoutLayoutParams
1828 lp = (CellLayoutLayoutParams) view.getLayoutParams();
Sebastian Franco877088e2023-01-03 15:16:22 -07001829 mOccupied.markCells(lp.getCellX(), lp.getCellY(), lp.cellHSpan, lp.cellVSpan, true);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001830 }
1831
Adam Cohend4844c32011-02-18 19:25:06 -08001832 public void markCellsAsUnoccupiedForView(View view) {
Sebastian Francof153d912022-04-22 16:15:27 -05001833 if (view instanceof LauncherAppWidgetHostView
1834 && view.getTag() instanceof LauncherAppWidgetInfo) {
1835 LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) view.getTag();
Sunny Goyal669b71f2023-01-27 14:37:07 -08001836 CellPos pos = mActivity.getCellPosMapper().mapModelToPresenter(info);
1837 mOccupied.markCells(pos.cellX, pos.cellY, info.spanX, info.spanY, false);
Sebastian Francof153d912022-04-22 16:15:27 -05001838 return;
1839 }
Michael Jurkaa52570f2012-03-20 03:18:20 -07001840 if (view == null || view.getParent() != mShortcutsAndWidgets) return;
Sebastian Francod4682992022-10-05 13:03:09 -05001841 CellLayoutLayoutParams
1842 lp = (CellLayoutLayoutParams) view.getLayoutParams();
Sebastian Franco877088e2023-01-03 15:16:22 -07001843 mOccupied.markCells(lp.getCellX(), lp.getCellY(), lp.cellHSpan, lp.cellVSpan, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001844 }
1845
Adam Cohen2801caf2011-05-13 20:57:39 -07001846 public int getDesiredWidth() {
Jon Miranda228877d2021-02-09 11:05:00 -05001847 return getPaddingLeft() + getPaddingRight() + (mCountX * mCellWidth)
Thales Lima78d00ad2021-09-30 11:29:06 +01001848 + ((mCountX - 1) * mBorderSpace.x);
Adam Cohen2801caf2011-05-13 20:57:39 -07001849 }
1850
1851 public int getDesiredHeight() {
Jon Miranda228877d2021-02-09 11:05:00 -05001852 return getPaddingTop() + getPaddingBottom() + (mCountY * mCellHeight)
Thales Lima78d00ad2021-09-30 11:29:06 +01001853 + ((mCountY - 1) * mBorderSpace.y);
Adam Cohen2801caf2011-05-13 20:57:39 -07001854 }
1855
Michael Jurka66d72172011-04-12 16:29:25 -07001856 public boolean isOccupied(int x, int y) {
Schneider Victor-tulias750c5af2023-07-26 10:16:04 -07001857 if (x >= 0 && x < mCountX && y >= 0 && y < mCountY) {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001858 return mOccupied.cells[x][y];
Schneider Victor-tulias750c5af2023-07-26 10:16:04 -07001859 }
1860 if (BuildConfig.IS_STUDIO_BUILD) {
Michael Jurka66d72172011-04-12 16:29:25 -07001861 throw new RuntimeException("Position exceeds the bound of this CellLayout");
1862 }
Schneider Victor-tulias750c5af2023-07-26 10:16:04 -07001863 return true;
Michael Jurka66d72172011-04-12 16:29:25 -07001864 }
1865
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001866 @Override
1867 public ViewGroup.LayoutParams generateLayoutParams(AttributeSet attrs) {
Sebastian Francod4682992022-10-05 13:03:09 -05001868 return new CellLayoutLayoutParams(getContext(), attrs);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001869 }
1870
1871 @Override
1872 protected boolean checkLayoutParams(ViewGroup.LayoutParams p) {
Sebastian Francod4682992022-10-05 13:03:09 -05001873 return p instanceof CellLayoutLayoutParams;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001874 }
1875
1876 @Override
1877 protected ViewGroup.LayoutParams generateLayoutParams(ViewGroup.LayoutParams p) {
Sebastian Francod4682992022-10-05 13:03:09 -05001878 return new CellLayoutLayoutParams(p);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001879 }
1880
Samuel Fufa1e2d0042019-11-18 17:12:46 -08001881 /**
Tony Wickham86930612015-09-09 13:50:40 -07001882 * Returns whether an item can be placed in this CellLayout (after rearranging and/or resizing
1883 * if necessary).
1884 */
1885 public boolean hasReorderSolution(ItemInfo itemInfo) {
1886 int[] cellPoint = new int[2];
1887 // Check for a solution starting at every cell.
1888 for (int cellX = 0; cellX < getCountX(); cellX++) {
1889 for (int cellY = 0; cellY < getCountY(); cellY++) {
1890 cellToPoint(cellX, cellY, cellPoint);
1891 if (findReorderSolution(cellPoint[0], cellPoint[1], itemInfo.minSpanX,
1892 itemInfo.minSpanY, itemInfo.spanX, itemInfo.spanY, mDirectionVector, null,
Charlie Anderson46122392024-01-18 17:23:46 -05001893 true).isSolution) {
Tony Wickham86930612015-09-09 13:50:40 -07001894 return true;
1895 }
1896 }
1897 }
1898 return false;
1899 }
1900
Samuel Fufaa4211432020-02-25 18:47:54 -08001901 /**
1902 * Finds solution to accept hotseat migration to cell layout. commits solution if commitConfig
1903 */
1904 public boolean makeSpaceForHotseatMigration(boolean commitConfig) {
Samuel Fufaa4211432020-02-25 18:47:54 -08001905 int[] cellPoint = new int[2];
1906 int[] directionVector = new int[]{0, -1};
1907 cellToPoint(0, mCountY, cellPoint);
Charlie Anderson46122392024-01-18 17:23:46 -05001908 ItemConfiguration configuration = findReorderSolution(
1909 cellPoint[0] /* pixelX */,
1910 cellPoint[1] /* pixelY */,
1911 mCountX /* minSpanX */,
1912 1 /* minSpanY */,
1913 mCountX /* spanX */,
1914 1 /* spanY */,
1915 directionVector /* direction */,
1916 null /* dragView */,
1917 false /* decX */
1918 );
1919 if (configuration.isSolution) {
Samuel Fufaa4211432020-02-25 18:47:54 -08001920 if (commitConfig) {
1921 copySolutionToTempState(configuration, null);
Sunny Goyal711c5962021-06-23 12:36:18 -07001922 commitTempPlacement(null);
Samuel Fufa82bbdac2020-03-09 18:24:47 -07001923 // undo marking cells occupied since there is actually nothing being placed yet.
1924 mOccupied.markCells(0, mCountY - 1, mCountX, 1, false);
Samuel Fufaa4211432020-02-25 18:47:54 -08001925 }
1926 return true;
1927 }
1928 return false;
1929 }
1930
Samuel Fufa82bbdac2020-03-09 18:24:47 -07001931 /**
1932 * returns a copy of cell layout's grid occupancy
1933 */
1934 public GridOccupancy cloneGridOccupancy() {
1935 GridOccupancy occupancy = new GridOccupancy(mCountX, mCountY);
1936 mOccupied.copyTo(occupancy);
1937 return occupancy;
1938 }
1939
Sunny Goyal9ca9c132015-04-29 14:57:22 -07001940 public boolean isRegionVacant(int x, int y, int spanX, int spanY) {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001941 return mOccupied.isRegionVacant(x, y, spanX, spanY);
Sunny Goyal9ca9c132015-04-29 14:57:22 -07001942 }
Sebastian Franco9ea36d42023-09-21 13:56:42 -07001943
1944 public void setSpaceBetweenCellLayoutsPx(@Px int spaceBetweenCellLayoutsPx) {
1945 mSpaceBetweenCellLayoutsPx = spaceBetweenCellLayoutsPx;
1946 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001947}