blob: f52a4d6756be3080aa35956e93b7e7f8e6d1846f [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
Sunny Goyalf0b6db72018-08-13 16:10:14 -070019import static com.android.launcher3.anim.Interpolators.DEACCEL_1_5;
20
Joe Onorato4be866d2010-10-10 11:26:02 -070021import android.animation.Animator;
Michael Jurka629758f2012-06-14 16:18:21 -070022import android.animation.AnimatorListenerAdapter;
Sunny Goyal849c6a22018-08-08 16:33:46 -070023import android.animation.ObjectAnimator;
Chet Haase00397b12010-10-07 11:13:10 -070024import android.animation.TimeInterpolator;
Patrick Dubroyde7658b2010-09-27 11:15:43 -070025import android.animation.ValueAnimator;
26import android.animation.ValueAnimator.AnimatorUpdateListener;
Sunny Goyal726bee72018-03-05 12:54:24 -080027import android.annotation.SuppressLint;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080028import android.content.Context;
Joe Onorato79e56262009-09-21 15:23:04 -040029import android.content.res.Resources;
Sunny Goyalc13403c2016-11-18 23:44:48 -080030import android.content.res.TypedArray;
Joe Onorato4be866d2010-10-10 11:26:02 -070031import android.graphics.Bitmap;
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 Cohen482ed822012-03-02 14:15:13 -080037import android.graphics.drawable.ColorDrawable;
Patrick Dubroy6569f2c2010-07-12 14:25:18 -070038import android.graphics.drawable.Drawable;
Adam Cohen1462de32012-07-24 22:34:36 -070039import android.os.Parcelable;
Sunny Goyal849c6a22018-08-08 16:33:46 -070040import android.os.SystemClock;
Sunny Goyalc13403c2016-11-18 23:44:48 -080041import android.support.annotation.IntDef;
Adam Cohenc9735cf2015-01-23 16:11:55 -080042import android.support.v4.view.ViewCompat;
Rajeev Kumar9962dbe2017-06-12 12:16:20 -070043import android.util.ArrayMap;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080044import android.util.AttributeSet;
Joe Onorato4be866d2010-10-10 11:26:02 -070045import android.util.Log;
Sunny Goyal849c6a22018-08-08 16:33:46 -070046import android.util.Property;
Adam Cohen1462de32012-07-24 22:34:36 -070047import android.util.SparseArray;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080048import android.view.MotionEvent;
49import android.view.View;
50import android.view.ViewDebug;
51import android.view.ViewGroup;
Adam Cohenc9735cf2015-01-23 16:11:55 -080052import android.view.accessibility.AccessibilityEvent;
Sunny Goyal5bc6b6f2017-10-26 15:36:10 -070053
Sunny Goyalaa8ef112015-06-12 20:04:41 -070054import com.android.launcher3.LauncherSettings.Favorites;
Sunny Goyale9b651e2015-04-24 11:44:51 -070055import com.android.launcher3.accessibility.DragAndDropAccessibilityDelegate;
56import com.android.launcher3.accessibility.FolderAccessibilityHelper;
57import com.android.launcher3.accessibility.WorkspaceAccessibilityHelper;
Sunny Goyal5bc6b6f2017-10-26 15:36:10 -070058import com.android.launcher3.anim.Interpolators;
Sunny Goyal9e76f682017-02-13 12:13:43 -080059import com.android.launcher3.anim.PropertyListBuilder;
Sunny Goyal3d706ad2017-03-06 16:56:39 -080060import com.android.launcher3.config.FeatureFlags;
Jon Mirandaa0233f72017-06-22 18:34:45 -070061import com.android.launcher3.folder.PreviewBackground;
Sunny Goyal06e21a22016-08-11 16:02:02 -070062import com.android.launcher3.graphics.DragPreviewProvider;
Sunny Goyalff4ba2d2016-04-02 14:12:34 -070063import com.android.launcher3.util.CellAndSpan;
64import com.android.launcher3.util.GridOccupancy;
Sunny Goyale2fd14b2015-08-27 17:45:46 -070065import com.android.launcher3.util.ParcelableSparseArray;
Sunny Goyal9b29ca52017-02-17 10:39:44 -080066import com.android.launcher3.util.Themes;
Adam Cohen091440a2015-03-18 14:16:05 -070067import com.android.launcher3.util.Thunk;
Sunny Goyal29947f02017-12-18 13:49:44 -080068import com.android.launcher3.widget.LauncherAppWidgetHostView;
Sunny Goyal5bc6b6f2017-10-26 15:36:10 -070069
Sunny Goyalc13403c2016-11-18 23:44:48 -080070import java.lang.annotation.Retention;
71import java.lang.annotation.RetentionPolicy;
Adam Cohen69ce2e52011-07-03 19:25:21 -070072import java.util.ArrayList;
Adam Cohenc0dcf592011-06-01 15:30:43 -070073import java.util.Arrays;
Adam Cohenf3900c22012-11-16 18:28:11 -080074import java.util.Collections;
75import java.util.Comparator;
Adam Cohend41fbf52012-02-16 23:53:59 -080076import java.util.Stack;
Adam Cohenc0dcf592011-06-01 15:30:43 -070077
Sunny Goyal726bee72018-03-05 12:54:24 -080078public class CellLayout extends ViewGroup {
Sunny Goyale9b651e2015-04-24 11:44:51 -070079 public static final int WORKSPACE_ACCESSIBILITY_DRAG = 2;
80 public static final int FOLDER_ACCESSIBILITY_DRAG = 1;
81
Tony Wickhama0628cc2015-10-14 15:23:04 -070082 private static final String TAG = "CellLayout";
83 private static final boolean LOGD = false;
Winson Chungaafa03c2010-06-11 17:34:16 -070084
Rajeev Kumar9962dbe2017-06-12 12:16:20 -070085 private final Launcher mLauncher;
Sunny Goyal4ffec482016-02-09 11:28:52 -080086 @ViewDebug.ExportedProperty(category = "launcher")
Adam Cohen091440a2015-03-18 14:16:05 -070087 @Thunk int mCellWidth;
Sunny Goyal4ffec482016-02-09 11:28:52 -080088 @ViewDebug.ExportedProperty(category = "launcher")
Adam Cohen091440a2015-03-18 14:16:05 -070089 @Thunk int mCellHeight;
Winson Chung11a1a532013-09-13 11:14:45 -070090 private int mFixedCellWidth;
91 private int mFixedCellHeight;
Winson Chungaafa03c2010-06-11 17:34:16 -070092
Sunny Goyal4ffec482016-02-09 11:28:52 -080093 @ViewDebug.ExportedProperty(category = "launcher")
Sunny Goyalff4ba2d2016-04-02 14:12:34 -070094 private int mCountX;
Sunny Goyal4ffec482016-02-09 11:28:52 -080095 @ViewDebug.ExportedProperty(category = "launcher")
Sunny Goyalff4ba2d2016-04-02 14:12:34 -070096 private int mCountY;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080097
Adam Cohen917e3882013-10-31 15:03:35 -070098 private boolean mDropPending = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080099
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700100 // These are temporary variables to prevent having to allocate a new object just to
101 // return an (x, y) value from helper functions. Do NOT use them to maintain other state.
Adam Cohen091440a2015-03-18 14:16:05 -0700102 @Thunk final int[] mTmpPoint = new int[2];
Sunny Goyal2805e632015-05-20 15:35:32 -0700103 @Thunk final int[] mTempLocation = new int[2];
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700104
Sunny Goyalff4ba2d2016-04-02 14:12:34 -0700105 private GridOccupancy mOccupied;
106 private GridOccupancy mTmpOccupied;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800107
Michael Jurkadee05892010-07-27 10:01:56 -0700108 private OnTouchListener mInterceptTouchListener;
Rajeev Kumar9962dbe2017-06-12 12:16:20 -0700109 private final StylusEventHelper mStylusEventHelper;
Michael Jurkadee05892010-07-27 10:01:56 -0700110
Jon Mirandaa0233f72017-06-22 18:34:45 -0700111 private final ArrayList<PreviewBackground> mFolderBackgrounds = new ArrayList<>();
112 final PreviewBackground mFolderLeaveBehind = new PreviewBackground();
Adam Cohen69ce2e52011-07-03 19:25:21 -0700113
Sunny Goyalf5440cb2016-12-14 15:13:00 -0800114 private static final int[] BACKGROUND_STATE_ACTIVE = new int[] { android.R.attr.state_active };
Sunny Goyale15e2a82017-12-15 13:05:42 -0800115 private static final int[] BACKGROUND_STATE_DEFAULT = EMPTY_STATE_SET;
Sunny Goyalf5440cb2016-12-14 15:13:00 -0800116 private final Drawable mBackground;
Sunny Goyal2805e632015-05-20 15:35:32 -0700117
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700118 // These values allow a fixed measurement to be set on the CellLayout.
119 private int mFixedWidth = -1;
120 private int mFixedHeight = -1;
121
Michael Jurka33945b22010-12-21 18:19:38 -0800122 // If we're actively dragging something over this screen, mIsDragOverlapping is true
123 private boolean mIsDragOverlapping = false;
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700124
Winson Chung150fbab2010-09-29 17:14:26 -0700125 // These arrays are used to implement the drag visualization on x-large screens.
Joe Onorato4be866d2010-10-10 11:26:02 -0700126 // They are used as circular arrays, indexed by mDragOutlineCurrent.
Rajeev Kumar9962dbe2017-06-12 12:16:20 -0700127 @Thunk final Rect[] mDragOutlines = new Rect[4];
128 @Thunk final float[] mDragOutlineAlphas = new float[mDragOutlines.length];
129 private final InterruptibleInOutAnimator[] mDragOutlineAnims =
Joe Onorato4be866d2010-10-10 11:26:02 -0700130 new InterruptibleInOutAnimator[mDragOutlines.length];
Winson Chung150fbab2010-09-29 17:14:26 -0700131
132 // Used as an index into the above 3 arrays; indicates which is the most current value.
Joe Onorato4be866d2010-10-10 11:26:02 -0700133 private int mDragOutlineCurrent = 0;
Patrick Dubroy8e58e912010-10-14 13:21:48 -0700134 private final Paint mDragOutlinePaint = new Paint();
Winson Chung150fbab2010-09-29 17:14:26 -0700135
Rajeev Kumar9962dbe2017-06-12 12:16:20 -0700136 @Thunk final ArrayMap<LayoutParams, Animator> mReorderAnimators = new ArrayMap<>();
137 @Thunk final ArrayMap<View, ReorderPreviewAnimation> mShakeAnimators = new ArrayMap<>();
Adam Cohen19f37922012-03-21 11:59:11 -0700138
139 private boolean mItemPlacementDirty = false;
Adam Cohenbfbfd262011-06-13 16:55:12 -0700140
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700141 // When a drag operation is in progress, holds the nearest cell to the touch point
142 private final int[] mDragCell = new int[2];
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800143
Joe Onorato4be866d2010-10-10 11:26:02 -0700144 private boolean mDragging = false;
145
Rajeev Kumar9962dbe2017-06-12 12:16:20 -0700146 private final TimeInterpolator mEaseOutInterpolator;
147 private final ShortcutAndWidgetContainer mShortcutsAndWidgets;
Patrick Dubroyce34a972010-10-19 10:34:32 -0700148
Sunny Goyalc13403c2016-11-18 23:44:48 -0800149 @Retention(RetentionPolicy.SOURCE)
150 @IntDef({WORKSPACE, HOTSEAT, FOLDER})
151 public @interface ContainerType{}
152 public static final int WORKSPACE = 0;
153 public static final int HOTSEAT = 1;
154 public static final int FOLDER = 2;
155
156 @ContainerType private final int mContainerType;
157
Jon Mirandab28c4fc2017-06-20 10:58:36 -0700158 private final float mChildScale = 1f;
Andrew Flynn0dca1ec2012-02-29 13:33:22 -0800159
Adam Cohenfa3c58f2013-12-06 16:10:55 -0800160 public static final int MODE_SHOW_REORDER_HINT = 0;
161 public static final int MODE_DRAG_OVER = 1;
162 public static final int MODE_ON_DROP = 2;
163 public static final int MODE_ON_DROP_EXTERNAL = 3;
164 public static final int MODE_ACCEPT_DROP = 4;
Adam Cohen19f37922012-03-21 11:59:11 -0700165 private static final boolean DESTRUCTIVE_REORDER = false;
Adam Cohen482ed822012-03-02 14:15:13 -0800166 private static final boolean DEBUG_VISUALIZE_OCCUPIED = false;
167
Adam Cohenfa3c58f2013-12-06 16:10:55 -0800168 private static final float REORDER_PREVIEW_MAGNITUDE = 0.12f;
Adam Cohen19f37922012-03-21 11:59:11 -0700169 private static final int REORDER_ANIMATION_DURATION = 150;
Sunny Goyalc13403c2016-11-18 23:44:48 -0800170 @Thunk final float mReorderPreviewAnimationMagnitude;
Adam Cohen19f37922012-03-21 11:59:11 -0700171
Rajeev Kumar9962dbe2017-06-12 12:16:20 -0700172 private final ArrayList<View> mIntersectingViews = new ArrayList<>();
173 private final Rect mOccupiedRect = new Rect();
174 private final int[] mDirectionVector = new int[2];
175 final int[] mPreviousReorderDirection = new int[2];
Adam Cohenb209e632012-03-27 17:09:36 -0700176 private static final int INVALID_DIRECTION = -100;
Adam Cohen482ed822012-03-02 14:15:13 -0800177
Sunny Goyal2805e632015-05-20 15:35:32 -0700178 private final Rect mTempRect = new Rect();
Winson Chung3a6e7f32013-10-09 15:50:52 -0700179
Michael Jurkaca993832012-06-29 15:17:04 -0700180 private final static Paint sPaint = new Paint();
Romain Guy8a0bff52012-05-06 13:14:33 -0700181
Adam Cohenc9735cf2015-01-23 16:11:55 -0800182 // Related to accessible drag and drop
Sunny Goyale9b651e2015-04-24 11:44:51 -0700183 private DragAndDropAccessibilityDelegate mTouchHelper;
Adam Cohenc9735cf2015-01-23 16:11:55 -0800184 private boolean mUseTouchHelper = false;
Adam Cohenc9735cf2015-01-23 16:11:55 -0800185
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800186 public CellLayout(Context context) {
187 this(context, null);
188 }
189
190 public CellLayout(Context context, AttributeSet attrs) {
191 this(context, attrs, 0);
192 }
193
194 public CellLayout(Context context, AttributeSet attrs, int defStyle) {
195 super(context, attrs, defStyle);
Sunny Goyalc13403c2016-11-18 23:44:48 -0800196 TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.CellLayout, defStyle, 0);
197 mContainerType = a.getInteger(R.styleable.CellLayout_containerType, WORKSPACE);
198 a.recycle();
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700199
200 // A ViewGroup usually does not draw, but CellLayout needs to draw a rectangle to show
201 // the user where a dragged item will land when dropped.
202 setWillNotDraw(false);
Romain Guyce3cbd12013-02-25 15:00:36 -0800203 setClipToPadding(false);
Tony2fd02082016-10-07 12:50:01 -0700204 mLauncher = Launcher.getLauncher(context);
Michael Jurkaa63c4522010-08-19 13:52:27 -0700205
Adam Cohen2e6da152015-05-06 11:42:25 -0700206 DeviceProfile grid = mLauncher.getDeviceProfile();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800207
Winson Chung11a1a532013-09-13 11:14:45 -0700208 mCellWidth = mCellHeight = -1;
Nilesh Agrawal5f7099a2014-01-02 15:54:57 -0800209 mFixedCellWidth = mFixedCellHeight = -1;
Sunny Goyalff4ba2d2016-04-02 14:12:34 -0700210
211 mCountX = grid.inv.numColumns;
212 mCountY = grid.inv.numRows;
213 mOccupied = new GridOccupancy(mCountX, mCountY);
214 mTmpOccupied = new GridOccupancy(mCountX, mCountY);
215
Adam Cohen5b53f292012-03-29 14:30:35 -0700216 mPreviousReorderDirection[0] = INVALID_DIRECTION;
217 mPreviousReorderDirection[1] = INVALID_DIRECTION;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800218
Adam Cohenefca0272016-02-24 19:19:06 -0800219 mFolderLeaveBehind.delegateCellX = -1;
220 mFolderLeaveBehind.delegateCellY = -1;
221
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800222 setAlwaysDrawnWithCacheEnabled(false);
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700223 final Resources res = getResources();
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700224
Sunny Goyalf5440cb2016-12-14 15:13:00 -0800225 mBackground = res.getDrawable(R.drawable.bg_celllayout);
Sunny Goyal2805e632015-05-20 15:35:32 -0700226 mBackground.setCallback(this);
Sunny Goyalaeb16432017-10-16 11:46:41 -0700227 mBackground.setAlpha(0);
Michael Jurka33945b22010-12-21 18:19:38 -0800228
Sunny Goyalc13403c2016-11-18 23:44:48 -0800229 mReorderPreviewAnimationMagnitude = (REORDER_PREVIEW_MAGNITUDE * grid.iconSizePx);
Adam Cohen19f37922012-03-21 11:59:11 -0700230
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700231 // Initialize the data structures used for the drag visualization.
Sunny Goyal5bc6b6f2017-10-26 15:36:10 -0700232 mEaseOutInterpolator = Interpolators.DEACCEL_2_5; // Quint ease out
Winson Chungb8c69f32011-10-19 21:36:08 -0700233 mDragCell[0] = mDragCell[1] = -1;
Joe Onorato4be866d2010-10-10 11:26:02 -0700234 for (int i = 0; i < mDragOutlines.length; i++) {
Adam Cohend41fbf52012-02-16 23:53:59 -0800235 mDragOutlines[i] = new Rect(-1, -1, -1, -1);
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700236 }
Mario Bertschler54ba6012017-06-08 10:53:53 -0700237 mDragOutlinePaint.setColor(Themes.getAttrColor(context, R.attr.workspaceTextColor));
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700238
239 // When dragging things around the home screens, we show a green outline of
240 // where the item will land. The outlines gradually fade out, leaving a trail
241 // behind the drag path.
242 // Set up all the animations that are used to implement this fading.
243 final int duration = res.getInteger(R.integer.config_dragOutlineFadeTime);
Chet Haase472b2812010-10-14 07:02:04 -0700244 final float fromAlphaValue = 0;
245 final float toAlphaValue = (float)res.getInteger(R.integer.config_dragOutlineMaxAlpha);
Joe Onorato4be866d2010-10-10 11:26:02 -0700246
Patrick Dubroy8e58e912010-10-14 13:21:48 -0700247 Arrays.fill(mDragOutlineAlphas, fromAlphaValue);
Joe Onorato4be866d2010-10-10 11:26:02 -0700248
249 for (int i = 0; i < mDragOutlineAnims.length; i++) {
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700250 final InterruptibleInOutAnimator anim =
Sunny Goyal849c6a22018-08-08 16:33:46 -0700251 new InterruptibleInOutAnimator(duration, fromAlphaValue, toAlphaValue);
Patrick Dubroyce34a972010-10-19 10:34:32 -0700252 anim.getAnimator().setInterpolator(mEaseOutInterpolator);
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700253 final int thisIndex = i;
Chet Haase472b2812010-10-14 07:02:04 -0700254 anim.getAnimator().addUpdateListener(new AnimatorUpdateListener() {
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700255 public void onAnimationUpdate(ValueAnimator animation) {
Joe Onorato4be866d2010-10-10 11:26:02 -0700256 final Bitmap outline = (Bitmap)anim.getTag();
257
258 // If an animation is started and then stopped very quickly, we can still
259 // get spurious updates we've cleared the tag. Guard against this.
260 if (outline == null) {
Tony Wickhama0628cc2015-10-14 15:23:04 -0700261 if (LOGD) {
Patrick Dubroyfe6bd872010-10-13 17:32:10 -0700262 Object val = animation.getAnimatedValue();
263 Log.d(TAG, "anim " + thisIndex + " update: " + val +
264 ", isStopped " + anim.isStopped());
265 }
Joe Onorato4be866d2010-10-10 11:26:02 -0700266 // Try to prevent it from continuing to run
267 animation.cancel();
268 } else {
Chet Haase472b2812010-10-14 07:02:04 -0700269 mDragOutlineAlphas[thisIndex] = (Float) animation.getAnimatedValue();
Adam Cohend41fbf52012-02-16 23:53:59 -0800270 CellLayout.this.invalidate(mDragOutlines[thisIndex]);
Joe Onorato4be866d2010-10-10 11:26:02 -0700271 }
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700272 }
273 });
Joe Onorato4be866d2010-10-10 11:26:02 -0700274 // The animation holds a reference to the drag outline bitmap as long is it's
275 // running. This way the bitmap can be GCed when the animations are complete.
Chet Haase472b2812010-10-14 07:02:04 -0700276 anim.getAnimator().addListener(new AnimatorListenerAdapter() {
Michael Jurka3c4c20f2010-10-28 15:36:06 -0700277 @Override
Joe Onorato4be866d2010-10-10 11:26:02 -0700278 public void onAnimationEnd(Animator animation) {
Chet Haase472b2812010-10-14 07:02:04 -0700279 if ((Float) ((ValueAnimator) animation).getAnimatedValue() == 0f) {
Joe Onorato4be866d2010-10-10 11:26:02 -0700280 anim.setTag(null);
281 }
282 }
283 });
284 mDragOutlineAnims[i] = anim;
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700285 }
Patrick Dubroyce34a972010-10-19 10:34:32 -0700286
Sunny Goyalc13403c2016-11-18 23:44:48 -0800287 mShortcutsAndWidgets = new ShortcutAndWidgetContainer(context, mContainerType);
Sunny Goyalaa8a8712016-11-20 15:26:01 +0530288 mShortcutsAndWidgets.setCellDimensions(mCellWidth, mCellHeight, mCountX, mCountY);
Adam Cohen2374abf2013-04-16 14:56:57 -0700289
Mady Mellorbb835202015-07-15 16:34:34 -0700290 mStylusEventHelper = new StylusEventHelper(new SimpleOnStylusPressListener(this), this);
Michael Jurkaa52570f2012-03-20 03:18:20 -0700291 addView(mShortcutsAndWidgets);
Michael Jurka18014792010-10-14 09:01:34 -0700292 }
293
Sunny Goyale9b651e2015-04-24 11:44:51 -0700294 public void enableAccessibleDrag(boolean enable, int dragType) {
Adam Cohenc9735cf2015-01-23 16:11:55 -0800295 mUseTouchHelper = enable;
296 if (!enable) {
297 ViewCompat.setAccessibilityDelegate(this, null);
298 setImportantForAccessibility(IMPORTANT_FOR_ACCESSIBILITY_NO);
299 getShortcutsAndWidgets().setImportantForAccessibility(IMPORTANT_FOR_ACCESSIBILITY_NO);
Sunny Goyal7c7be8c2018-03-07 19:58:07 -0800300 setOnClickListener(null);
Adam Cohenc9735cf2015-01-23 16:11:55 -0800301 } else {
Sunny Goyale9b651e2015-04-24 11:44:51 -0700302 if (dragType == WORKSPACE_ACCESSIBILITY_DRAG &&
303 !(mTouchHelper instanceof WorkspaceAccessibilityHelper)) {
304 mTouchHelper = new WorkspaceAccessibilityHelper(this);
305 } else if (dragType == FOLDER_ACCESSIBILITY_DRAG &&
306 !(mTouchHelper instanceof FolderAccessibilityHelper)) {
307 mTouchHelper = new FolderAccessibilityHelper(this);
308 }
Adam Cohenc9735cf2015-01-23 16:11:55 -0800309 ViewCompat.setAccessibilityDelegate(this, mTouchHelper);
310 setImportantForAccessibility(IMPORTANT_FOR_ACCESSIBILITY_YES);
311 getShortcutsAndWidgets().setImportantForAccessibility(IMPORTANT_FOR_ACCESSIBILITY_YES);
312 setOnClickListener(mTouchHelper);
313 }
314
315 // Invalidate the accessibility hierarchy
316 if (getParent() != null) {
317 getParent().notifySubtreeAccessibilityStateChanged(
318 this, this, AccessibilityEvent.CONTENT_CHANGE_TYPE_SUBTREE);
319 }
320 }
321
322 @Override
323 public boolean dispatchHoverEvent(MotionEvent event) {
324 // Always attempt to dispatch hover events to accessibility first.
325 if (mUseTouchHelper && mTouchHelper.dispatchHoverEvent(event)) {
326 return true;
327 }
328 return super.dispatchHoverEvent(event);
329 }
330
331 @Override
Adam Cohenc9735cf2015-01-23 16:11:55 -0800332 public boolean onInterceptTouchEvent(MotionEvent ev) {
333 if (mUseTouchHelper ||
334 (mInterceptTouchListener != null && mInterceptTouchListener.onTouch(this, ev))) {
335 return true;
336 }
337 return false;
338 }
339
Mady Melloref044dd2015-06-02 15:35:07 -0700340 @Override
341 public boolean onTouchEvent(MotionEvent ev) {
342 boolean handled = super.onTouchEvent(ev);
343 // Stylus button press on a home screen should not switch between overview mode and
344 // the home screen mode, however, once in overview mode stylus button press should be
345 // enabled to allow rearranging the different home screens. So check what mode
346 // the workspace is in, and only perform stylus button presses while in overview mode.
Sunny Goyalf9403d92017-10-18 10:55:56 -0700347 if (mLauncher.isInState(LauncherState.OVERVIEW)
Mady Mellorbb835202015-07-15 16:34:34 -0700348 && mStylusEventHelper.onMotionEvent(ev)) {
Mady Melloref044dd2015-06-02 15:35:07 -0700349 return true;
350 }
351 return handled;
352 }
353
Chris Craik01f2d7f2013-10-01 14:41:56 -0700354 public void enableHardwareLayer(boolean hasLayer) {
355 mShortcutsAndWidgets.setLayerType(hasLayer ? LAYER_TYPE_HARDWARE : LAYER_TYPE_NONE, sPaint);
Michael Jurkad51f33a2012-06-28 15:35:26 -0700356 }
357
Winson Chung5f8afe62013-08-12 16:19:28 -0700358 public void setCellDimensions(int width, int height) {
Winson Chung11a1a532013-09-13 11:14:45 -0700359 mFixedCellWidth = mCellWidth = width;
360 mFixedCellHeight = mCellHeight = height;
Sunny Goyalaa8a8712016-11-20 15:26:01 +0530361 mShortcutsAndWidgets.setCellDimensions(mCellWidth, mCellHeight, mCountX, mCountY);
Winson Chung5f8afe62013-08-12 16:19:28 -0700362 }
363
Adam Cohen2801caf2011-05-13 20:57:39 -0700364 public void setGridSize(int x, int y) {
365 mCountX = x;
366 mCountY = y;
Sunny Goyalff4ba2d2016-04-02 14:12:34 -0700367 mOccupied = new GridOccupancy(mCountX, mCountY);
368 mTmpOccupied = new GridOccupancy(mCountX, mCountY);
Adam Cohen7fbec102012-03-27 12:42:19 -0700369 mTempRectStack.clear();
Sunny Goyalaa8a8712016-11-20 15:26:01 +0530370 mShortcutsAndWidgets.setCellDimensions(mCellWidth, mCellHeight, mCountX, mCountY);
Adam Cohen76fc0852011-06-17 13:26:23 -0700371 requestLayout();
Adam Cohen2801caf2011-05-13 20:57:39 -0700372 }
373
Adam Cohen2374abf2013-04-16 14:56:57 -0700374 // Set whether or not to invert the layout horizontally if the layout is in RTL mode.
375 public void setInvertIfRtl(boolean invert) {
376 mShortcutsAndWidgets.setInvertIfRtl(invert);
377 }
378
Adam Cohen917e3882013-10-31 15:03:35 -0700379 public void setDropPending(boolean pending) {
380 mDropPending = pending;
381 }
382
383 public boolean isDropPending() {
384 return mDropPending;
385 }
386
Adam Cohenc50438c2014-08-19 17:43:05 -0700387 void setIsDragOverlapping(boolean isDragOverlapping) {
388 if (mIsDragOverlapping != isDragOverlapping) {
389 mIsDragOverlapping = isDragOverlapping;
Sunny Goyalf5440cb2016-12-14 15:13:00 -0800390 mBackground.setState(mIsDragOverlapping
391 ? BACKGROUND_STATE_ACTIVE : BACKGROUND_STATE_DEFAULT);
Adam Cohenc50438c2014-08-19 17:43:05 -0700392 invalidate();
393 }
394 }
395
Sunny Goyale2fd14b2015-08-27 17:45:46 -0700396 @Override
397 protected void dispatchSaveInstanceState(SparseArray<Parcelable> container) {
Sunny Goyal7ce471b2017-08-02 03:37:39 -0700398 ParcelableSparseArray jail = getJailedArray(container);
399 super.dispatchSaveInstanceState(jail);
400 container.put(R.id.cell_layout_jail_id, jail);
Sunny Goyale2fd14b2015-08-27 17:45:46 -0700401 }
402
403 @Override
404 protected void dispatchRestoreInstanceState(SparseArray<Parcelable> container) {
Sunny Goyal7ce471b2017-08-02 03:37:39 -0700405 super.dispatchRestoreInstanceState(getJailedArray(container));
Sunny Goyale2fd14b2015-08-27 17:45:46 -0700406 }
407
Sunny Goyal7ce471b2017-08-02 03:37:39 -0700408 /**
409 * Wrap the SparseArray in another Parcelable so that the item ids do not conflict with our
410 * our internal resource ids
411 */
Sunny Goyale2fd14b2015-08-27 17:45:46 -0700412 private ParcelableSparseArray getJailedArray(SparseArray<Parcelable> container) {
413 final Parcelable parcelable = container.get(R.id.cell_layout_jail_id);
414 return parcelable instanceof ParcelableSparseArray ?
415 (ParcelableSparseArray) parcelable : new ParcelableSparseArray();
416 }
417
Tony Wickham0f97b782015-12-02 17:55:07 -0800418 public boolean getIsDragOverlapping() {
419 return mIsDragOverlapping;
420 }
421
Jeff Sharkey83f111d2009-04-20 21:03:13 -0700422 @Override
Patrick Dubroy1262e362010-10-06 15:49:50 -0700423 protected void onDraw(Canvas canvas) {
Michael Jurka3e7c7632010-10-02 16:01:03 -0700424 // When we're large, we are either drawn in a "hover" state (ie when dragging an item to
425 // a neighboring page) or with just a normal background (if backgroundAlpha > 0.0f)
426 // When we're small, we are either drawn normally or in the "accepts drops" state (during
427 // a drag). However, we also drag the mini hover background *over* one of those two
428 // backgrounds
Sunny Goyalaeb16432017-10-16 11:46:41 -0700429 if (mBackground.getAlpha() > 0) {
Sunny Goyal2805e632015-05-20 15:35:32 -0700430 mBackground.draw(canvas);
Michael Jurkaa63c4522010-08-19 13:52:27 -0700431 }
Romain Guya6abce82009-11-10 02:54:41 -0800432
Patrick Dubroy8e58e912010-10-14 13:21:48 -0700433 final Paint paint = mDragOutlinePaint;
Joe Onorato4be866d2010-10-10 11:26:02 -0700434 for (int i = 0; i < mDragOutlines.length; i++) {
Chet Haase472b2812010-10-14 07:02:04 -0700435 final float alpha = mDragOutlineAlphas[i];
Joe Onorato4be866d2010-10-10 11:26:02 -0700436 if (alpha > 0) {
Joe Onorato4be866d2010-10-10 11:26:02 -0700437 final Bitmap b = (Bitmap) mDragOutlineAnims[i].getTag();
Chet Haase472b2812010-10-14 07:02:04 -0700438 paint.setAlpha((int)(alpha + .5f));
Sunny Goyal106bf642015-07-16 12:18:06 -0700439 canvas.drawBitmap(b, null, mDragOutlines[i], paint);
Winson Chung150fbab2010-09-29 17:14:26 -0700440 }
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700441 }
Patrick Dubroy96864c32011-03-10 17:17:23 -0800442
Adam Cohen482ed822012-03-02 14:15:13 -0800443 if (DEBUG_VISUALIZE_OCCUPIED) {
444 int[] pt = new int[2];
445 ColorDrawable cd = new ColorDrawable(Color.RED);
Adam Cohene7587d22012-05-24 18:50:02 -0700446 cd.setBounds(0, 0, mCellWidth, mCellHeight);
Adam Cohen482ed822012-03-02 14:15:13 -0800447 for (int i = 0; i < mCountX; i++) {
448 for (int j = 0; j < mCountY; j++) {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -0700449 if (mOccupied.cells[i][j]) {
Adam Cohen482ed822012-03-02 14:15:13 -0800450 cellToPoint(i, j, pt);
451 canvas.save();
452 canvas.translate(pt[0], pt[1]);
453 cd.draw(canvas);
454 canvas.restore();
455 }
456 }
457 }
458 }
459
Adam Cohenefca0272016-02-24 19:19:06 -0800460 for (int i = 0; i < mFolderBackgrounds.size(); i++) {
Jon Mirandaa0233f72017-06-22 18:34:45 -0700461 PreviewBackground bg = mFolderBackgrounds.get(i);
Adam Cohenefca0272016-02-24 19:19:06 -0800462 cellToPoint(bg.delegateCellX, bg.delegateCellY, mTempLocation);
463 canvas.save();
464 canvas.translate(mTempLocation[0], mTempLocation[1]);
Sunny Goyal19b93b72017-02-19 20:21:37 -0800465 bg.drawBackground(canvas);
Adam Cohenf172b742016-03-30 19:28:34 -0700466 if (!bg.isClipping) {
Sunny Goyal19b93b72017-02-19 20:21:37 -0800467 bg.drawBackgroundStroke(canvas);
Adam Cohenf172b742016-03-30 19:28:34 -0700468 }
Adam Cohenefca0272016-02-24 19:19:06 -0800469 canvas.restore();
Adam Cohen69ce2e52011-07-03 19:25:21 -0700470 }
Adam Cohenc51934b2011-07-26 21:07:43 -0700471
Adam Cohenefca0272016-02-24 19:19:06 -0800472 if (mFolderLeaveBehind.delegateCellX >= 0 && mFolderLeaveBehind.delegateCellY >= 0) {
473 cellToPoint(mFolderLeaveBehind.delegateCellX,
474 mFolderLeaveBehind.delegateCellY, mTempLocation);
475 canvas.save();
476 canvas.translate(mTempLocation[0], mTempLocation[1]);
Sunny Goyal19b93b72017-02-19 20:21:37 -0800477 mFolderLeaveBehind.drawLeaveBehind(canvas);
Adam Cohenefca0272016-02-24 19:19:06 -0800478 canvas.restore();
Adam Cohenc51934b2011-07-26 21:07:43 -0700479 }
Adam Cohen69ce2e52011-07-03 19:25:21 -0700480 }
481
Adam Cohenefca0272016-02-24 19:19:06 -0800482 @Override
483 protected void dispatchDraw(Canvas canvas) {
484 super.dispatchDraw(canvas);
485
486 for (int i = 0; i < mFolderBackgrounds.size(); i++) {
Jon Mirandaa0233f72017-06-22 18:34:45 -0700487 PreviewBackground bg = mFolderBackgrounds.get(i);
Adam Cohenf172b742016-03-30 19:28:34 -0700488 if (bg.isClipping) {
489 cellToPoint(bg.delegateCellX, bg.delegateCellY, mTempLocation);
490 canvas.save();
491 canvas.translate(mTempLocation[0], mTempLocation[1]);
Sunny Goyal19b93b72017-02-19 20:21:37 -0800492 bg.drawBackgroundStroke(canvas);
Adam Cohenf172b742016-03-30 19:28:34 -0700493 canvas.restore();
494 }
Adam Cohenefca0272016-02-24 19:19:06 -0800495 }
Adam Cohen69ce2e52011-07-03 19:25:21 -0700496 }
497
Jon Mirandaa0233f72017-06-22 18:34:45 -0700498 public void addFolderBackground(PreviewBackground bg) {
Adam Cohenefca0272016-02-24 19:19:06 -0800499 mFolderBackgrounds.add(bg);
500 }
Jon Mirandaa0233f72017-06-22 18:34:45 -0700501 public void removeFolderBackground(PreviewBackground bg) {
Adam Cohenefca0272016-02-24 19:19:06 -0800502 mFolderBackgrounds.remove(bg);
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700503 }
504
Adam Cohenc51934b2011-07-26 21:07:43 -0700505 public void setFolderLeaveBehindCell(int x, int y) {
Adam Cohenefca0272016-02-24 19:19:06 -0800506 View child = getChildAt(x, y);
Sunny Goyal368ae772017-05-24 13:19:15 -0700507 mFolderLeaveBehind.setup(mLauncher, null,
Adam Cohenefca0272016-02-24 19:19:06 -0800508 child.getMeasuredWidth(), child.getPaddingTop());
509
510 mFolderLeaveBehind.delegateCellX = x;
511 mFolderLeaveBehind.delegateCellY = y;
Adam Cohenc51934b2011-07-26 21:07:43 -0700512 invalidate();
513 }
514
515 public void clearFolderLeaveBehind() {
Adam Cohenefca0272016-02-24 19:19:06 -0800516 mFolderLeaveBehind.delegateCellX = -1;
517 mFolderLeaveBehind.delegateCellY = -1;
Adam Cohenc51934b2011-07-26 21:07:43 -0700518 invalidate();
519 }
520
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700521 @Override
Michael Jurkae6235dd2011-10-04 15:02:05 -0700522 public boolean shouldDelayChildPressedState() {
523 return false;
524 }
525
Adam Cohen1462de32012-07-24 22:34:36 -0700526 public void restoreInstanceState(SparseArray<Parcelable> states) {
Sunny Goyal33a152f2014-07-22 12:13:14 -0700527 try {
528 dispatchRestoreInstanceState(states);
529 } catch (IllegalArgumentException ex) {
Sunny Goyal3d706ad2017-03-06 16:56:39 -0800530 if (FeatureFlags.IS_DOGFOOD_BUILD) {
Sunny Goyal33a152f2014-07-22 12:13:14 -0700531 throw ex;
532 }
533 // Mismatched viewId / viewType preventing restore. Skip restore on production builds.
534 Log.e(TAG, "Ignoring an error while restoring a view instance state", ex);
535 }
Adam Cohen1462de32012-07-24 22:34:36 -0700536 }
537
Michael Jurkae6235dd2011-10-04 15:02:05 -0700538 @Override
Jeff Sharkey83f111d2009-04-20 21:03:13 -0700539 public void cancelLongPress() {
540 super.cancelLongPress();
541
542 // Cancel long press for all children
543 final int count = getChildCount();
544 for (int i = 0; i < count; i++) {
545 final View child = getChildAt(i);
546 child.cancelLongPress();
547 }
548 }
549
Michael Jurkadee05892010-07-27 10:01:56 -0700550 public void setOnInterceptTouchListener(View.OnTouchListener listener) {
551 mInterceptTouchListener = listener;
552 }
553
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800554 public int getCountX() {
Adam Cohend22015c2010-07-26 22:02:18 -0700555 return mCountX;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800556 }
557
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800558 public int getCountY() {
Adam Cohend22015c2010-07-26 22:02:18 -0700559 return mCountY;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800560 }
561
Sunny Goyalc13403c2016-11-18 23:44:48 -0800562 public boolean acceptsWidget() {
563 return mContainerType == WORKSPACE;
Sunny Goyale9b651e2015-04-24 11:44:51 -0700564 }
565
Andrew Flynn0dca1ec2012-02-29 13:33:22 -0800566 public boolean addViewToCellLayout(View child, int index, int childId, LayoutParams params,
Sunny Goyalf7a29e82015-04-24 15:20:43 -0700567 boolean markCells) {
Winson Chungaafa03c2010-06-11 17:34:16 -0700568 final LayoutParams lp = params;
569
Andrew Flynnde38e422012-05-08 11:22:15 -0700570 // Hotseat icons - remove text
Andrew Flynn0dca1ec2012-02-29 13:33:22 -0800571 if (child instanceof BubbleTextView) {
572 BubbleTextView bubbleChild = (BubbleTextView) child;
Jon Mirandaf1eae802017-10-04 11:23:33 -0700573 bubbleChild.setTextVisibility(mContainerType != HOTSEAT);
Andrew Flynn0dca1ec2012-02-29 13:33:22 -0800574 }
575
Sunny Goyalc13403c2016-11-18 23:44:48 -0800576 child.setScaleX(mChildScale);
577 child.setScaleY(mChildScale);
Adam Cohen307fe232012-08-16 17:55:58 -0700578
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800579 // Generate an id for each view, this assumes we have at most 256x256 cells
580 // per workspace screen
Adam Cohend22015c2010-07-26 22:02:18 -0700581 if (lp.cellX >= 0 && lp.cellX <= mCountX - 1 && lp.cellY >= 0 && lp.cellY <= mCountY - 1) {
Winson Chungaafa03c2010-06-11 17:34:16 -0700582 // If the horizontal or vertical span is set to -1, it is taken to
583 // mean that it spans the extent of the CellLayout
Adam Cohend22015c2010-07-26 22:02:18 -0700584 if (lp.cellHSpan < 0) lp.cellHSpan = mCountX;
585 if (lp.cellVSpan < 0) lp.cellVSpan = mCountY;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800586
Winson Chungaafa03c2010-06-11 17:34:16 -0700587 child.setId(childId);
Tony Wickhama0628cc2015-10-14 15:23:04 -0700588 if (LOGD) {
589 Log.d(TAG, "Adding view to ShortcutsAndWidgetsContainer: " + child);
590 }
Sunny Goyalf7a29e82015-04-24 15:20:43 -0700591 mShortcutsAndWidgets.addView(child, index, lp);
Michael Jurkadee05892010-07-27 10:01:56 -0700592
Michael Jurkaf3ca3ab2010-10-20 17:08:24 -0700593 if (markCells) markCellsAsOccupiedForView(child);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700594
Winson Chungaafa03c2010-06-11 17:34:16 -0700595 return true;
596 }
597 return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800598 }
Michael Jurka3e7c7632010-10-02 16:01:03 -0700599
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800600 @Override
Michael Jurka0280c3b2010-09-17 15:00:07 -0700601 public void removeAllViews() {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -0700602 mOccupied.clear();
Michael Jurkaa52570f2012-03-20 03:18:20 -0700603 mShortcutsAndWidgets.removeAllViews();
Michael Jurka0280c3b2010-09-17 15:00:07 -0700604 }
605
606 @Override
607 public void removeAllViewsInLayout() {
Michael Jurkaa52570f2012-03-20 03:18:20 -0700608 if (mShortcutsAndWidgets.getChildCount() > 0) {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -0700609 mOccupied.clear();
Michael Jurkaa52570f2012-03-20 03:18:20 -0700610 mShortcutsAndWidgets.removeAllViewsInLayout();
Michael Jurka7cfc2822011-08-02 20:19:24 -0700611 }
Michael Jurka0280c3b2010-09-17 15:00:07 -0700612 }
613
614 @Override
615 public void removeView(View view) {
616 markCellsAsUnoccupiedForView(view);
Michael Jurkaa52570f2012-03-20 03:18:20 -0700617 mShortcutsAndWidgets.removeView(view);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700618 }
619
620 @Override
621 public void removeViewAt(int index) {
Michael Jurkaa52570f2012-03-20 03:18:20 -0700622 markCellsAsUnoccupiedForView(mShortcutsAndWidgets.getChildAt(index));
623 mShortcutsAndWidgets.removeViewAt(index);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700624 }
625
626 @Override
627 public void removeViewInLayout(View view) {
628 markCellsAsUnoccupiedForView(view);
Michael Jurkaa52570f2012-03-20 03:18:20 -0700629 mShortcutsAndWidgets.removeViewInLayout(view);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700630 }
631
632 @Override
633 public void removeViews(int start, int count) {
634 for (int i = start; i < start + count; i++) {
Michael Jurkaa52570f2012-03-20 03:18:20 -0700635 markCellsAsUnoccupiedForView(mShortcutsAndWidgets.getChildAt(i));
Michael Jurka0280c3b2010-09-17 15:00:07 -0700636 }
Michael Jurkaa52570f2012-03-20 03:18:20 -0700637 mShortcutsAndWidgets.removeViews(start, count);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700638 }
639
640 @Override
641 public void removeViewsInLayout(int start, int count) {
642 for (int i = start; i < start + count; i++) {
Michael Jurkaa52570f2012-03-20 03:18:20 -0700643 markCellsAsUnoccupiedForView(mShortcutsAndWidgets.getChildAt(i));
Michael Jurka0280c3b2010-09-17 15:00:07 -0700644 }
Michael Jurkaa52570f2012-03-20 03:18:20 -0700645 mShortcutsAndWidgets.removeViewsInLayout(start, count);
Michael Jurkaabded662011-03-04 12:06:57 -0800646 }
647
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700648 /**
Winson Chungaafa03c2010-06-11 17:34:16 -0700649 * Given a point, return the cell that strictly encloses that point
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800650 * @param x X coordinate of the point
651 * @param y Y coordinate of the point
652 * @param result Array of 2 ints to hold the x and y coordinate of the cell
653 */
Sunny Goyale9b651e2015-04-24 11:44:51 -0700654 public void pointToCellExact(int x, int y, int[] result) {
Winson Chung4b825dcd2011-06-19 12:41:22 -0700655 final int hStartPadding = getPaddingLeft();
656 final int vStartPadding = getPaddingTop();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800657
Sunny Goyalaa8a8712016-11-20 15:26:01 +0530658 result[0] = (x - hStartPadding) / mCellWidth;
659 result[1] = (y - vStartPadding) / mCellHeight;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800660
Adam Cohend22015c2010-07-26 22:02:18 -0700661 final int xAxis = mCountX;
662 final int yAxis = mCountY;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800663
664 if (result[0] < 0) result[0] = 0;
665 if (result[0] >= xAxis) result[0] = xAxis - 1;
666 if (result[1] < 0) result[1] = 0;
667 if (result[1] >= yAxis) result[1] = yAxis - 1;
668 }
Winson Chungaafa03c2010-06-11 17:34:16 -0700669
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800670 /**
671 * Given a point, return the cell that most closely encloses that point
672 * @param x X coordinate of the point
673 * @param y Y coordinate of the point
674 * @param result Array of 2 ints to hold the x and y coordinate of the cell
675 */
676 void pointToCellRounded(int x, int y, int[] result) {
677 pointToCellExact(x + (mCellWidth / 2), y + (mCellHeight / 2), result);
678 }
679
680 /**
681 * Given a cell coordinate, return the point that represents the upper left corner of that cell
Winson Chungaafa03c2010-06-11 17:34:16 -0700682 *
683 * @param cellX X coordinate of the cell
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800684 * @param cellY Y coordinate of the cell
Winson Chungaafa03c2010-06-11 17:34:16 -0700685 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800686 * @param result Array of 2 ints to hold the x and y coordinate of the point
687 */
688 void cellToPoint(int cellX, int cellY, int[] result) {
Winson Chung4b825dcd2011-06-19 12:41:22 -0700689 final int hStartPadding = getPaddingLeft();
690 final int vStartPadding = getPaddingTop();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800691
Sunny Goyalaa8a8712016-11-20 15:26:01 +0530692 result[0] = hStartPadding + cellX * mCellWidth;
693 result[1] = vStartPadding + cellY * mCellHeight;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800694 }
695
Adam Cohene3e27a82011-04-15 12:07:39 -0700696 /**
Adam Cohen482ed822012-03-02 14:15:13 -0800697 * Given a cell coordinate, return the point that represents the center of the cell
Adam Cohene3e27a82011-04-15 12:07:39 -0700698 *
699 * @param cellX X coordinate of the cell
700 * @param cellY Y coordinate of the cell
701 *
702 * @param result Array of 2 ints to hold the x and y coordinate of the point
703 */
704 void cellToCenterPoint(int cellX, int cellY, int[] result) {
Adam Cohen47a876d2012-03-19 13:21:41 -0700705 regionToCenterPoint(cellX, cellY, 1, 1, result);
706 }
707
708 /**
709 * Given a cell coordinate and span return the point that represents the center of the regio
710 *
711 * @param cellX X coordinate of the cell
712 * @param cellY Y coordinate of the cell
713 *
714 * @param result Array of 2 ints to hold the x and y coordinate of the point
715 */
716 void regionToCenterPoint(int cellX, int cellY, int spanX, int spanY, int[] result) {
Winson Chung4b825dcd2011-06-19 12:41:22 -0700717 final int hStartPadding = getPaddingLeft();
718 final int vStartPadding = getPaddingTop();
Sunny Goyalaa8a8712016-11-20 15:26:01 +0530719 result[0] = hStartPadding + cellX * mCellWidth + (spanX * mCellWidth) / 2;
720 result[1] = vStartPadding + cellY * mCellHeight + (spanY * mCellHeight) / 2;
Adam Cohene3e27a82011-04-15 12:07:39 -0700721 }
722
Adam Cohen19f37922012-03-21 11:59:11 -0700723 /**
724 * Given a cell coordinate and span fills out a corresponding pixel rect
725 *
726 * @param cellX X coordinate of the cell
727 * @param cellY Y coordinate of the cell
728 * @param result Rect in which to write the result
729 */
730 void regionToRect(int cellX, int cellY, int spanX, int spanY, Rect result) {
731 final int hStartPadding = getPaddingLeft();
732 final int vStartPadding = getPaddingTop();
Sunny Goyalaa8a8712016-11-20 15:26:01 +0530733 final int left = hStartPadding + cellX * mCellWidth;
734 final int top = vStartPadding + cellY * mCellHeight;
735 result.set(left, top, left + (spanX * mCellWidth), top + (spanY * mCellHeight));
Adam Cohen19f37922012-03-21 11:59:11 -0700736 }
737
Adam Cohen482ed822012-03-02 14:15:13 -0800738 public float getDistanceFromCell(float x, float y, int[] cell) {
739 cellToCenterPoint(cell[0], cell[1], mTmpPoint);
Sunny Goyalf7a29e82015-04-24 15:20:43 -0700740 return (float) Math.hypot(x - mTmpPoint[0], y - mTmpPoint[1]);
Adam Cohen482ed822012-03-02 14:15:13 -0800741 }
742
Adam Cohenf9c184a2016-01-15 16:47:43 -0800743 public int getCellWidth() {
Romain Guy84f296c2009-11-04 15:00:44 -0800744 return mCellWidth;
745 }
746
Sunny Goyal0b754e52017-08-07 07:42:45 -0700747 public int getCellHeight() {
Romain Guy84f296c2009-11-04 15:00:44 -0800748 return mCellHeight;
749 }
750
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700751 public void setFixedSize(int width, int height) {
752 mFixedWidth = width;
753 mFixedHeight = height;
754 }
755
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800756 @Override
757 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800758 int widthSpecMode = MeasureSpec.getMode(widthMeasureSpec);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800759 int heightSpecMode = MeasureSpec.getMode(heightMeasureSpec);
Winson Chung5f8afe62013-08-12 16:19:28 -0700760 int widthSize = MeasureSpec.getSize(widthMeasureSpec);
761 int heightSize = MeasureSpec.getSize(heightMeasureSpec);
Winson Chung2d75f122013-09-23 16:53:31 -0700762 int childWidthSize = widthSize - (getPaddingLeft() + getPaddingRight());
763 int childHeightSize = heightSize - (getPaddingTop() + getPaddingBottom());
Winson Chung11a1a532013-09-13 11:14:45 -0700764 if (mFixedCellWidth < 0 || mFixedCellHeight < 0) {
Sunny Goyalc6205602015-05-21 20:46:33 -0700765 int cw = DeviceProfile.calculateCellWidth(childWidthSize, mCountX);
766 int ch = DeviceProfile.calculateCellHeight(childHeightSize, mCountY);
Winson Chung11a1a532013-09-13 11:14:45 -0700767 if (cw != mCellWidth || ch != mCellHeight) {
768 mCellWidth = cw;
769 mCellHeight = ch;
Sunny Goyalaa8a8712016-11-20 15:26:01 +0530770 mShortcutsAndWidgets.setCellDimensions(mCellWidth, mCellHeight, mCountX, mCountY);
Winson Chung11a1a532013-09-13 11:14:45 -0700771 }
Winson Chung5f8afe62013-08-12 16:19:28 -0700772 }
Winson Chungaafa03c2010-06-11 17:34:16 -0700773
Winson Chung2d75f122013-09-23 16:53:31 -0700774 int newWidth = childWidthSize;
775 int newHeight = childHeightSize;
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700776 if (mFixedWidth > 0 && mFixedHeight > 0) {
777 newWidth = mFixedWidth;
778 newHeight = mFixedHeight;
779 } else if (widthSpecMode == MeasureSpec.UNSPECIFIED || heightSpecMode == MeasureSpec.UNSPECIFIED) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800780 throw new RuntimeException("CellLayout cannot have UNSPECIFIED dimensions");
781 }
782
Sunny Goyal4fe5a372015-05-14 19:55:10 -0700783 mShortcutsAndWidgets.measure(
784 MeasureSpec.makeMeasureSpec(newWidth, MeasureSpec.EXACTLY),
785 MeasureSpec.makeMeasureSpec(newHeight, MeasureSpec.EXACTLY));
786
787 int maxWidth = mShortcutsAndWidgets.getMeasuredWidth();
788 int maxHeight = mShortcutsAndWidgets.getMeasuredHeight();
Winson Chung2d75f122013-09-23 16:53:31 -0700789 if (mFixedWidth > 0 && mFixedHeight > 0) {
790 setMeasuredDimension(maxWidth, maxHeight);
791 } else {
792 setMeasuredDimension(widthSize, heightSize);
793 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800794 }
795
796 @Override
Michael Jurka28750fb2010-09-24 17:43:49 -0700797 protected void onLayout(boolean changed, int l, int t, int r, int b) {
Tony Wickham26b01422015-11-10 14:44:32 -0800798 int left = getPaddingLeft();
Sunny Goyal7ce471b2017-08-02 03:37:39 -0700799 left += (int) Math.ceil(getUnusedHorizontalSpace() / 2f);
Sunny Goyal7c786f72016-06-01 14:08:21 -0700800 int right = r - l - getPaddingRight();
Sunny Goyal7ce471b2017-08-02 03:37:39 -0700801 right -= (int) Math.ceil(getUnusedHorizontalSpace() / 2f);
Sunny Goyal7c786f72016-06-01 14:08:21 -0700802
Winson Chung38848ca2013-10-08 12:03:44 -0700803 int top = getPaddingTop();
Sunny Goyal7c786f72016-06-01 14:08:21 -0700804 int bottom = b - t - getPaddingBottom();
Sunny Goyal4fe5a372015-05-14 19:55:10 -0700805
Sunny Goyal7c786f72016-06-01 14:08:21 -0700806 mShortcutsAndWidgets.layout(left, top, right, bottom);
Sunny Goyal7c786f72016-06-01 14:08:21 -0700807 // Expand the background drawing bounds by the padding baked into the background drawable
808 mBackground.getPadding(mTempRect);
809 mBackground.setBounds(
Jon Miranda28032002017-07-13 16:18:56 -0700810 left - mTempRect.left - getPaddingLeft(),
811 top - mTempRect.top - getPaddingTop(),
812 right + mTempRect.right + getPaddingRight(),
813 bottom + mTempRect.bottom + getPaddingBottom());
Sunny Goyal7c786f72016-06-01 14:08:21 -0700814 }
815
Tony Wickhama501d492015-11-03 18:05:01 -0800816 /**
817 * Returns the amount of space left over after subtracting padding and cells. This space will be
818 * very small, a few pixels at most, and is a result of rounding down when calculating the cell
819 * width in {@link DeviceProfile#calculateCellWidth(int, int)}.
820 */
821 public int getUnusedHorizontalSpace() {
822 return getMeasuredWidth() - getPaddingLeft() - getPaddingRight() - (mCountX * mCellWidth);
823 }
824
Sunny Goyalaeb16432017-10-16 11:46:41 -0700825 public Drawable getScrimBackground() {
826 return mBackground;
Michael Jurkadee05892010-07-27 10:01:56 -0700827 }
828
Sunny Goyal2805e632015-05-20 15:35:32 -0700829 @Override
830 protected boolean verifyDrawable(Drawable who) {
Sunny Goyal7ce471b2017-08-02 03:37:39 -0700831 return super.verifyDrawable(who) || (who == mBackground);
Sunny Goyal2805e632015-05-20 15:35:32 -0700832 }
833
Michael Jurkaa52570f2012-03-20 03:18:20 -0700834 public ShortcutAndWidgetContainer getShortcutsAndWidgets() {
Sunny Goyaldcbcc862014-08-12 15:58:36 -0700835 return mShortcutsAndWidgets;
Michael Jurkaa52570f2012-03-20 03:18:20 -0700836 }
837
Patrick Dubroy440c3602010-07-13 17:50:32 -0700838 public View getChildAt(int x, int y) {
Michael Jurkaa52570f2012-03-20 03:18:20 -0700839 return mShortcutsAndWidgets.getChildAt(x, y);
Patrick Dubroy440c3602010-07-13 17:50:32 -0700840 }
841
Adam Cohen76fc0852011-06-17 13:26:23 -0700842 public boolean animateChildToPosition(final View child, int cellX, int cellY, int duration,
Adam Cohen482ed822012-03-02 14:15:13 -0800843 int delay, boolean permanent, boolean adjustOccupied) {
Michael Jurkaa52570f2012-03-20 03:18:20 -0700844 ShortcutAndWidgetContainer clc = getShortcutsAndWidgets();
Adam Cohen482ed822012-03-02 14:15:13 -0800845
Adam Cohen19f37922012-03-21 11:59:11 -0700846 if (clc.indexOfChild(child) != -1) {
Adam Cohenbfbfd262011-06-13 16:55:12 -0700847 final LayoutParams lp = (LayoutParams) child.getLayoutParams();
848 final ItemInfo info = (ItemInfo) child.getTag();
849
850 // We cancel any existing animations
851 if (mReorderAnimators.containsKey(lp)) {
852 mReorderAnimators.get(lp).cancel();
853 mReorderAnimators.remove(lp);
854 }
855
Adam Cohen482ed822012-03-02 14:15:13 -0800856 final int oldX = lp.x;
857 final int oldY = lp.y;
858 if (adjustOccupied) {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -0700859 GridOccupancy occupied = permanent ? mOccupied : mTmpOccupied;
860 occupied.markCells(lp.cellX, lp.cellY, lp.cellHSpan, lp.cellVSpan, false);
861 occupied.markCells(cellX, cellY, lp.cellHSpan, lp.cellVSpan, true);
Adam Cohen482ed822012-03-02 14:15:13 -0800862 }
Adam Cohenbfbfd262011-06-13 16:55:12 -0700863 lp.isLockedToGrid = true;
Adam Cohen482ed822012-03-02 14:15:13 -0800864 if (permanent) {
865 lp.cellX = info.cellX = cellX;
866 lp.cellY = info.cellY = cellY;
867 } else {
868 lp.tmpCellX = cellX;
869 lp.tmpCellY = cellY;
870 }
Jon Mirandae96798e2016-12-07 12:10:44 -0800871 clc.setupLp(child);
Adam Cohenbfbfd262011-06-13 16:55:12 -0700872 lp.isLockedToGrid = false;
Adam Cohen482ed822012-03-02 14:15:13 -0800873 final int newX = lp.x;
874 final int newY = lp.y;
Adam Cohenbfbfd262011-06-13 16:55:12 -0700875
Adam Cohen76fc0852011-06-17 13:26:23 -0700876 lp.x = oldX;
877 lp.y = oldY;
Adam Cohen76fc0852011-06-17 13:26:23 -0700878
Adam Cohen482ed822012-03-02 14:15:13 -0800879 // Exit early if we're not actually moving the view
880 if (oldX == newX && oldY == newY) {
881 lp.isLockedToGrid = true;
882 return true;
883 }
884
Sunny Goyal849c6a22018-08-08 16:33:46 -0700885 ValueAnimator va = ValueAnimator.ofFloat(0f, 1f);
Adam Cohen482ed822012-03-02 14:15:13 -0800886 va.setDuration(duration);
887 mReorderAnimators.put(lp, va);
888
889 va.addUpdateListener(new AnimatorUpdateListener() {
890 @Override
Adam Cohenbfbfd262011-06-13 16:55:12 -0700891 public void onAnimationUpdate(ValueAnimator animation) {
Jon Mirandae96798e2016-12-07 12:10:44 -0800892 float r = (Float) animation.getAnimatedValue();
Adam Cohen19f37922012-03-21 11:59:11 -0700893 lp.x = (int) ((1 - r) * oldX + r * newX);
894 lp.y = (int) ((1 - r) * oldY + r * newY);
Adam Cohen6b8a02d2012-03-22 15:13:40 -0700895 child.requestLayout();
Adam Cohenbfbfd262011-06-13 16:55:12 -0700896 }
897 });
Adam Cohen482ed822012-03-02 14:15:13 -0800898 va.addListener(new AnimatorListenerAdapter() {
Adam Cohenbfbfd262011-06-13 16:55:12 -0700899 boolean cancelled = false;
900 public void onAnimationEnd(Animator animation) {
901 // If the animation was cancelled, it means that another animation
902 // has interrupted this one, and we don't want to lock the item into
903 // place just yet.
904 if (!cancelled) {
905 lp.isLockedToGrid = true;
Adam Cohen482ed822012-03-02 14:15:13 -0800906 child.requestLayout();
Adam Cohenbfbfd262011-06-13 16:55:12 -0700907 }
908 if (mReorderAnimators.containsKey(lp)) {
909 mReorderAnimators.remove(lp);
910 }
911 }
912 public void onAnimationCancel(Animator animation) {
913 cancelled = true;
914 }
915 });
Adam Cohen482ed822012-03-02 14:15:13 -0800916 va.setStartDelay(delay);
917 va.start();
Adam Cohenbfbfd262011-06-13 16:55:12 -0700918 return true;
919 }
920 return false;
921 }
922
Sunny Goyal06e21a22016-08-11 16:02:02 -0700923 void visualizeDropLocation(View v, DragPreviewProvider outlineProvider, int cellX, int cellY,
924 int spanX, int spanY, boolean resize, DropTarget.DragObject dragObject) {
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -0700925 final int oldDragCellX = mDragCell[0];
926 final int oldDragCellY = mDragCell[1];
Adam Cohen482ed822012-03-02 14:15:13 -0800927
Hyunyoung Song0de01172016-10-05 16:27:48 -0700928 if (outlineProvider == null || outlineProvider.generatedDragOutline == null) {
Adam Cohen2801caf2011-05-13 20:57:39 -0700929 return;
930 }
931
Hyunyoung Song0de01172016-10-05 16:27:48 -0700932 Bitmap dragOutline = outlineProvider.generatedDragOutline;
Adam Cohen482ed822012-03-02 14:15:13 -0800933 if (cellX != oldDragCellX || cellY != oldDragCellY) {
Sunny Goyale78e3d72015-09-24 11:23:31 -0700934 Point dragOffset = dragObject.dragView.getDragVisualizeOffset();
935 Rect dragRegion = dragObject.dragView.getDragRegion();
936
Adam Cohen482ed822012-03-02 14:15:13 -0800937 mDragCell[0] = cellX;
938 mDragCell[1] = cellY;
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700939
Joe Onorato4be866d2010-10-10 11:26:02 -0700940 final int oldIndex = mDragOutlineCurrent;
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -0700941 mDragOutlineAnims[oldIndex].animateOut();
942 mDragOutlineCurrent = (oldIndex + 1) % mDragOutlines.length;
Adam Cohend41fbf52012-02-16 23:53:59 -0800943 Rect r = mDragOutlines[mDragOutlineCurrent];
Sunny Goyal106bf642015-07-16 12:18:06 -0700944
Adam Cohend41fbf52012-02-16 23:53:59 -0800945 if (resize) {
Adam Cohen482ed822012-03-02 14:15:13 -0800946 cellToRect(cellX, cellY, spanX, spanY, r);
Jon Mirandae96798e2016-12-07 12:10:44 -0800947 if (v instanceof LauncherAppWidgetHostView) {
948 DeviceProfile profile = mLauncher.getDeviceProfile();
Jon Miranda6f6a06a2016-12-15 11:24:18 -0800949 Utilities.shrinkRect(r, profile.appWidgetScale.x, profile.appWidgetScale.y);
Jon Mirandae96798e2016-12-07 12:10:44 -0800950 }
Sunny Goyal106bf642015-07-16 12:18:06 -0700951 } else {
952 // Find the top left corner of the rect the object will occupy
953 final int[] topLeft = mTmpPoint;
954 cellToPoint(cellX, cellY, topLeft);
955
956 int left = topLeft[0];
957 int top = topLeft[1];
958
959 if (v != null && dragOffset == null) {
960 // When drawing the drag outline, it did not account for margin offsets
961 // added by the view's parent.
962 MarginLayoutParams lp = (MarginLayoutParams) v.getLayoutParams();
963 left += lp.leftMargin;
964 top += lp.topMargin;
965
966 // Offsets due to the size difference between the View and the dragOutline.
967 // There is a size difference to account for the outer blur, which may lie
968 // outside the bounds of the view.
Jon Mirandaf7ff3fe2016-12-05 12:04:44 -0800969 top += ((mCellHeight * spanY) - dragOutline.getHeight()) / 2;
Sunny Goyal106bf642015-07-16 12:18:06 -0700970 // We center about the x axis
Sunny Goyalaa8a8712016-11-20 15:26:01 +0530971 left += ((mCellWidth * spanX) - dragOutline.getWidth()) / 2;
Sunny Goyal106bf642015-07-16 12:18:06 -0700972 } else {
973 if (dragOffset != null && dragRegion != null) {
974 // Center the drag region *horizontally* in the cell and apply a drag
975 // outline offset
Sunny Goyalaa8a8712016-11-20 15:26:01 +0530976 left += dragOffset.x + ((mCellWidth * spanX) - dragRegion.width()) / 2;
Sunny Goyal106bf642015-07-16 12:18:06 -0700977 int cHeight = getShortcutsAndWidgets().getCellContentHeight();
978 int cellPaddingY = (int) Math.max(0, ((mCellHeight - cHeight) / 2f));
979 top += dragOffset.y + cellPaddingY;
980 } else {
981 // Center the drag outline in the cell
Sunny Goyalaa8a8712016-11-20 15:26:01 +0530982 left += ((mCellWidth * spanX) - dragOutline.getWidth()) / 2;
983 top += ((mCellHeight * spanY) - dragOutline.getHeight()) / 2;
Sunny Goyal106bf642015-07-16 12:18:06 -0700984 }
985 }
986 r.set(left, top, left + dragOutline.getWidth(), top + dragOutline.getHeight());
Adam Cohend41fbf52012-02-16 23:53:59 -0800987 }
Winson Chung150fbab2010-09-29 17:14:26 -0700988
Jon Miranda6f6a06a2016-12-15 11:24:18 -0800989 Utilities.scaleRectAboutCenter(r, mChildScale);
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -0700990 mDragOutlineAnims[mDragOutlineCurrent].setTag(dragOutline);
991 mDragOutlineAnims[mDragOutlineCurrent].animateIn();
Sunny Goyale78e3d72015-09-24 11:23:31 -0700992
993 if (dragObject.stateAnnouncer != null) {
Sunny Goyalc13403c2016-11-18 23:44:48 -0800994 dragObject.stateAnnouncer.announce(getItemMoveDescription(cellX, cellY));
Sunny Goyale78e3d72015-09-24 11:23:31 -0700995 }
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700996 }
997 }
998
Sunny Goyal726bee72018-03-05 12:54:24 -0800999 @SuppressLint("StringFormatMatches")
Sunny Goyalc13403c2016-11-18 23:44:48 -08001000 public String getItemMoveDescription(int cellX, int cellY) {
1001 if (mContainerType == HOTSEAT) {
1002 return getContext().getString(R.string.move_to_hotseat_position,
1003 Math.max(cellX, cellY) + 1);
1004 } else {
1005 return getContext().getString(R.string.move_to_empty_cell,
1006 cellY + 1, cellX + 1);
1007 }
1008 }
1009
Adam Cohene0310962011-04-18 16:15:31 -07001010 public void clearDragOutlines() {
1011 final int oldIndex = mDragOutlineCurrent;
1012 mDragOutlineAnims[oldIndex].animateOut();
Adam Cohend41fbf52012-02-16 23:53:59 -08001013 mDragCell[0] = mDragCell[1] = -1;
Adam Cohene0310962011-04-18 16:15:31 -07001014 }
1015
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001016 /**
Jeff Sharkey70864282009-04-07 21:08:40 -07001017 * Find a vacant area that will fit the given bounds nearest the requested
1018 * cell location. Uses Euclidean distance to score multiple vacant areas.
Winson Chungaafa03c2010-06-11 17:34:16 -07001019 *
Romain Guy51afc022009-05-04 18:03:43 -07001020 * @param pixelX The X location at which you want to search for a vacant area.
1021 * @param pixelY The Y location at which you want to search for a vacant area.
Adam Cohend41fbf52012-02-16 23:53:59 -08001022 * @param minSpanX The minimum horizontal span required
1023 * @param minSpanY The minimum vertical span required
1024 * @param spanX Horizontal span of the object.
1025 * @param spanY Vertical span of the object.
1026 * @param result Array in which to place the result, or null (in which case a new array will
1027 * be allocated)
1028 * @return The X, Y cell of a vacant area that can contain this object,
1029 * nearest the requested location.
1030 */
1031 int[] findNearestVacantArea(int pixelX, int pixelY, int minSpanX, int minSpanY, int spanX,
1032 int spanY, int[] result, int[] resultSpan) {
Sunny Goyalf7a29e82015-04-24 15:20:43 -07001033 return findNearestArea(pixelX, pixelY, minSpanX, minSpanY, spanX, spanY, true,
Adam Cohend41fbf52012-02-16 23:53:59 -08001034 result, resultSpan);
1035 }
1036
Rajeev Kumar9962dbe2017-06-12 12:16:20 -07001037 private final Stack<Rect> mTempRectStack = new Stack<>();
Adam Cohend41fbf52012-02-16 23:53:59 -08001038 private void lazyInitTempRectStack() {
1039 if (mTempRectStack.isEmpty()) {
1040 for (int i = 0; i < mCountX * mCountY; i++) {
1041 mTempRectStack.push(new Rect());
1042 }
1043 }
1044 }
Adam Cohen482ed822012-03-02 14:15:13 -08001045
Adam Cohend41fbf52012-02-16 23:53:59 -08001046 private void recycleTempRects(Stack<Rect> used) {
1047 while (!used.isEmpty()) {
1048 mTempRectStack.push(used.pop());
1049 }
1050 }
1051
1052 /**
1053 * Find a vacant area that will fit the given bounds nearest the requested
1054 * cell location. Uses Euclidean distance to score multiple vacant areas.
1055 *
1056 * @param pixelX The X location at which you want to search for a vacant area.
1057 * @param pixelY The Y location at which you want to search for a vacant area.
1058 * @param minSpanX The minimum horizontal span required
1059 * @param minSpanY The minimum vertical span required
1060 * @param spanX Horizontal span of the object.
1061 * @param spanY Vertical span of the object.
1062 * @param ignoreOccupied If true, the result can be an occupied cell
1063 * @param result Array in which to place the result, or null (in which case a new array will
1064 * be allocated)
1065 * @return The X, Y cell of a vacant area that can contain this object,
1066 * nearest the requested location.
1067 */
Sunny Goyalf7a29e82015-04-24 15:20:43 -07001068 private int[] findNearestArea(int pixelX, int pixelY, int minSpanX, int minSpanY, int spanX,
1069 int spanY, boolean ignoreOccupied, int[] result, int[] resultSpan) {
Adam Cohend41fbf52012-02-16 23:53:59 -08001070 lazyInitTempRectStack();
Michael Jurkac6ee42e2010-09-30 12:04:50 -07001071
Adam Cohene3e27a82011-04-15 12:07:39 -07001072 // For items with a spanX / spanY > 1, the passed in point (pixelX, pixelY) corresponds
1073 // to the center of the item, but we are searching based on the top-left cell, so
1074 // we translate the point over to correspond to the top-left.
Sunny Goyalaa8a8712016-11-20 15:26:01 +05301075 pixelX -= mCellWidth * (spanX - 1) / 2f;
1076 pixelY -= mCellHeight * (spanY - 1) / 2f;
Adam Cohene3e27a82011-04-15 12:07:39 -07001077
Jeff Sharkey70864282009-04-07 21:08:40 -07001078 // Keep track of best-scoring drop area
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001079 final int[] bestXY = result != null ? result : new int[2];
Jeff Sharkey70864282009-04-07 21:08:40 -07001080 double bestDistance = Double.MAX_VALUE;
Adam Cohend41fbf52012-02-16 23:53:59 -08001081 final Rect bestRect = new Rect(-1, -1, -1, -1);
Rajeev Kumar9962dbe2017-06-12 12:16:20 -07001082 final Stack<Rect> validRegions = new Stack<>();
Winson Chungaafa03c2010-06-11 17:34:16 -07001083
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001084 final int countX = mCountX;
1085 final int countY = mCountY;
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001086
Adam Cohend41fbf52012-02-16 23:53:59 -08001087 if (minSpanX <= 0 || minSpanY <= 0 || spanX <= 0 || spanY <= 0 ||
1088 spanX < minSpanX || spanY < minSpanY) {
1089 return bestXY;
1090 }
1091
1092 for (int y = 0; y < countY - (minSpanY - 1); y++) {
Michael Jurkac28de512010-08-13 11:27:44 -07001093 inner:
Adam Cohend41fbf52012-02-16 23:53:59 -08001094 for (int x = 0; x < countX - (minSpanX - 1); x++) {
1095 int ySize = -1;
1096 int xSize = -1;
Adam Cohendf035382011-04-11 17:22:04 -07001097 if (ignoreOccupied) {
Adam Cohend41fbf52012-02-16 23:53:59 -08001098 // First, let's see if this thing fits anywhere
1099 for (int i = 0; i < minSpanX; i++) {
1100 for (int j = 0; j < minSpanY; j++) {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001101 if (mOccupied.cells[x + i][y + j]) {
Adam Cohendf035382011-04-11 17:22:04 -07001102 continue inner;
1103 }
Michael Jurkac28de512010-08-13 11:27:44 -07001104 }
1105 }
Adam Cohend41fbf52012-02-16 23:53:59 -08001106 xSize = minSpanX;
1107 ySize = minSpanY;
1108
1109 // We know that the item will fit at _some_ acceptable size, now let's see
1110 // how big we can make it. We'll alternate between incrementing x and y spans
1111 // until we hit a limit.
1112 boolean incX = true;
1113 boolean hitMaxX = xSize >= spanX;
1114 boolean hitMaxY = ySize >= spanY;
1115 while (!(hitMaxX && hitMaxY)) {
1116 if (incX && !hitMaxX) {
1117 for (int j = 0; j < ySize; j++) {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001118 if (x + xSize > countX -1 || mOccupied.cells[x + xSize][y + j]) {
Adam Cohend41fbf52012-02-16 23:53:59 -08001119 // We can't move out horizontally
1120 hitMaxX = true;
1121 }
1122 }
1123 if (!hitMaxX) {
1124 xSize++;
1125 }
1126 } else if (!hitMaxY) {
1127 for (int i = 0; i < xSize; i++) {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001128 if (y + ySize > countY - 1 || mOccupied.cells[x + i][y + ySize]) {
Adam Cohend41fbf52012-02-16 23:53:59 -08001129 // We can't move out vertically
1130 hitMaxY = true;
1131 }
1132 }
1133 if (!hitMaxY) {
1134 ySize++;
1135 }
1136 }
1137 hitMaxX |= xSize >= spanX;
1138 hitMaxY |= ySize >= spanY;
1139 incX = !incX;
1140 }
1141 incX = true;
1142 hitMaxX = xSize >= spanX;
1143 hitMaxY = ySize >= spanY;
Michael Jurkac28de512010-08-13 11:27:44 -07001144 }
Sunny Goyal2805e632015-05-20 15:35:32 -07001145 final int[] cellXY = mTmpPoint;
Adam Cohene3e27a82011-04-15 12:07:39 -07001146 cellToCenterPoint(x, y, cellXY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001147
Adam Cohend41fbf52012-02-16 23:53:59 -08001148 // We verify that the current rect is not a sub-rect of any of our previous
1149 // candidates. In this case, the current rect is disqualified in favour of the
1150 // containing rect.
1151 Rect currentRect = mTempRectStack.pop();
1152 currentRect.set(x, y, x + xSize, y + ySize);
1153 boolean contained = false;
1154 for (Rect r : validRegions) {
1155 if (r.contains(currentRect)) {
1156 contained = true;
1157 break;
1158 }
1159 }
1160 validRegions.push(currentRect);
Sunny Goyalf7a29e82015-04-24 15:20:43 -07001161 double distance = Math.hypot(cellXY[0] - pixelX, cellXY[1] - pixelY);
Adam Cohen482ed822012-03-02 14:15:13 -08001162
Adam Cohend41fbf52012-02-16 23:53:59 -08001163 if ((distance <= bestDistance && !contained) ||
1164 currentRect.contains(bestRect)) {
Michael Jurkac28de512010-08-13 11:27:44 -07001165 bestDistance = distance;
1166 bestXY[0] = x;
1167 bestXY[1] = y;
Adam Cohend41fbf52012-02-16 23:53:59 -08001168 if (resultSpan != null) {
1169 resultSpan[0] = xSize;
1170 resultSpan[1] = ySize;
1171 }
1172 bestRect.set(currentRect);
Michael Jurkac28de512010-08-13 11:27:44 -07001173 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001174 }
1175 }
1176
Adam Cohenc0dcf592011-06-01 15:30:43 -07001177 // Return -1, -1 if no suitable location found
1178 if (bestDistance == Double.MAX_VALUE) {
1179 bestXY[0] = -1;
1180 bestXY[1] = -1;
Jeff Sharkey70864282009-04-07 21:08:40 -07001181 }
Adam Cohend41fbf52012-02-16 23:53:59 -08001182 recycleTempRects(validRegions);
Adam Cohenc0dcf592011-06-01 15:30:43 -07001183 return bestXY;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001184 }
Winson Chungaafa03c2010-06-11 17:34:16 -07001185
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001186 /**
Adam Cohen482ed822012-03-02 14:15:13 -08001187 * Find a vacant area that will fit the given bounds nearest the requested
1188 * cell location, and will also weigh in a suggested direction vector of the
1189 * desired location. This method computers distance based on unit grid distances,
1190 * not pixel distances.
1191 *
Adam Cohen47a876d2012-03-19 13:21:41 -07001192 * @param cellX The X cell nearest to which you want to search for a vacant area.
1193 * @param cellY The Y cell nearest which you want to search for a vacant area.
Adam Cohen482ed822012-03-02 14:15:13 -08001194 * @param spanX Horizontal span of the object.
1195 * @param spanY Vertical span of the object.
Adam Cohen47a876d2012-03-19 13:21:41 -07001196 * @param direction The favored direction in which the views should move from x, y
Sunny Goyal9eba1fd2015-10-16 08:58:57 -07001197 * @param occupied The array which represents which cells in the CellLayout are occupied
Adam Cohen47a876d2012-03-19 13:21:41 -07001198 * @param blockOccupied The array which represents which cells in the specified block (cellX,
Winson Chung5f8afe62013-08-12 16:19:28 -07001199 * cellY, spanX, spanY) are occupied. This is used when try to move a group of views.
Adam Cohen482ed822012-03-02 14:15:13 -08001200 * @param result Array in which to place the result, or null (in which case a new array will
1201 * be allocated)
1202 * @return The X, Y cell of a vacant area that can contain this object,
1203 * nearest the requested location.
1204 */
1205 private int[] findNearestArea(int cellX, int cellY, int spanX, int spanY, int[] direction,
Adam Cohen47a876d2012-03-19 13:21:41 -07001206 boolean[][] occupied, boolean blockOccupied[][], int[] result) {
Adam Cohen482ed822012-03-02 14:15:13 -08001207 // Keep track of best-scoring drop area
1208 final int[] bestXY = result != null ? result : new int[2];
1209 float bestDistance = Float.MAX_VALUE;
1210 int bestDirectionScore = Integer.MIN_VALUE;
1211
1212 final int countX = mCountX;
1213 final int countY = mCountY;
1214
1215 for (int y = 0; y < countY - (spanY - 1); y++) {
1216 inner:
1217 for (int x = 0; x < countX - (spanX - 1); x++) {
1218 // First, let's see if this thing fits anywhere
1219 for (int i = 0; i < spanX; i++) {
1220 for (int j = 0; j < spanY; j++) {
Adam Cohen47a876d2012-03-19 13:21:41 -07001221 if (occupied[x + i][y + j] && (blockOccupied == null || blockOccupied[i][j])) {
Adam Cohen482ed822012-03-02 14:15:13 -08001222 continue inner;
1223 }
1224 }
1225 }
1226
Sunny Goyalf7a29e82015-04-24 15:20:43 -07001227 float distance = (float) Math.hypot(x - cellX, y - cellY);
Adam Cohen482ed822012-03-02 14:15:13 -08001228 int[] curDirection = mTmpPoint;
Adam Cohen47a876d2012-03-19 13:21:41 -07001229 computeDirectionVector(x - cellX, y - cellY, curDirection);
1230 // The direction score is just the dot product of the two candidate direction
1231 // and that passed in.
Adam Cohen482ed822012-03-02 14:15:13 -08001232 int curDirectionScore = direction[0] * curDirection[0] +
1233 direction[1] * curDirection[1];
Sunny Goyal8f90dcf2016-08-18 15:01:11 -07001234 if (Float.compare(distance, bestDistance) < 0 ||
1235 (Float.compare(distance, bestDistance) == 0
1236 && curDirectionScore > bestDirectionScore)) {
Adam Cohen482ed822012-03-02 14:15:13 -08001237 bestDistance = distance;
1238 bestDirectionScore = curDirectionScore;
1239 bestXY[0] = x;
1240 bestXY[1] = y;
1241 }
1242 }
1243 }
1244
1245 // Return -1, -1 if no suitable location found
1246 if (bestDistance == Float.MAX_VALUE) {
1247 bestXY[0] = -1;
1248 bestXY[1] = -1;
1249 }
1250 return bestXY;
1251 }
1252
1253 private boolean addViewToTempLocation(View v, Rect rectOccupiedByPotentialDrop,
Adam Cohen8baab352012-03-20 17:39:21 -07001254 int[] direction, ItemConfiguration currentState) {
1255 CellAndSpan c = currentState.map.get(v);
Adam Cohen482ed822012-03-02 14:15:13 -08001256 boolean success = false;
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001257 mTmpOccupied.markCells(c, false);
1258 mTmpOccupied.markCells(rectOccupiedByPotentialDrop, true);
Adam Cohen482ed822012-03-02 14:15:13 -08001259
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001260 findNearestArea(c.cellX, c.cellY, c.spanX, c.spanY, direction,
1261 mTmpOccupied.cells, null, mTempLocation);
Adam Cohen482ed822012-03-02 14:15:13 -08001262
1263 if (mTempLocation[0] >= 0 && mTempLocation[1] >= 0) {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001264 c.cellX = mTempLocation[0];
1265 c.cellY = mTempLocation[1];
Adam Cohen482ed822012-03-02 14:15:13 -08001266 success = true;
Adam Cohen482ed822012-03-02 14:15:13 -08001267 }
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001268 mTmpOccupied.markCells(c, true);
Adam Cohen482ed822012-03-02 14:15:13 -08001269 return success;
1270 }
1271
Adam Cohenf3900c22012-11-16 18:28:11 -08001272 /**
1273 * This helper class defines a cluster of views. It helps with defining complex edges
1274 * of the cluster and determining how those edges interact with other views. The edges
1275 * essentially define a fine-grained boundary around the cluster of views -- like a more
1276 * precise version of a bounding box.
1277 */
1278 private class ViewCluster {
Sunny Goyal6dc98b92016-04-03 15:25:29 -07001279 final static int LEFT = 1 << 0;
1280 final static int TOP = 1 << 1;
1281 final static int RIGHT = 1 << 2;
1282 final static int BOTTOM = 1 << 3;
Adam Cohen47a876d2012-03-19 13:21:41 -07001283
Rajeev Kumar9962dbe2017-06-12 12:16:20 -07001284 final ArrayList<View> views;
1285 final ItemConfiguration config;
1286 final Rect boundingRect = new Rect();
Adam Cohen47a876d2012-03-19 13:21:41 -07001287
Rajeev Kumar9962dbe2017-06-12 12:16:20 -07001288 final int[] leftEdge = new int[mCountY];
1289 final int[] rightEdge = new int[mCountY];
1290 final int[] topEdge = new int[mCountX];
1291 final int[] bottomEdge = new int[mCountX];
Sunny Goyal6dc98b92016-04-03 15:25:29 -07001292 int dirtyEdges;
1293 boolean boundingRectDirty;
Adam Cohenf3900c22012-11-16 18:28:11 -08001294
1295 @SuppressWarnings("unchecked")
1296 public ViewCluster(ArrayList<View> views, ItemConfiguration config) {
1297 this.views = (ArrayList<View>) views.clone();
1298 this.config = config;
1299 resetEdges();
Adam Cohen47a876d2012-03-19 13:21:41 -07001300 }
1301
Adam Cohenf3900c22012-11-16 18:28:11 -08001302 void resetEdges() {
1303 for (int i = 0; i < mCountX; i++) {
1304 topEdge[i] = -1;
1305 bottomEdge[i] = -1;
1306 }
1307 for (int i = 0; i < mCountY; i++) {
1308 leftEdge[i] = -1;
1309 rightEdge[i] = -1;
1310 }
Sunny Goyal6dc98b92016-04-03 15:25:29 -07001311 dirtyEdges = LEFT | TOP | RIGHT | BOTTOM;
Adam Cohenf3900c22012-11-16 18:28:11 -08001312 boundingRectDirty = true;
1313 }
Adam Cohen47a876d2012-03-19 13:21:41 -07001314
Sunny Goyal6dc98b92016-04-03 15:25:29 -07001315 void computeEdge(int which) {
Adam Cohenf3900c22012-11-16 18:28:11 -08001316 int count = views.size();
1317 for (int i = 0; i < count; i++) {
1318 CellAndSpan cs = config.map.get(views.get(i));
1319 switch (which) {
1320 case LEFT:
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001321 int left = cs.cellX;
1322 for (int j = cs.cellY; j < cs.cellY + cs.spanY; j++) {
Sunny Goyal6dc98b92016-04-03 15:25:29 -07001323 if (left < leftEdge[j] || leftEdge[j] < 0) {
1324 leftEdge[j] = left;
Adam Cohena56dc102012-07-13 13:41:42 -07001325 }
Adam Cohen47a876d2012-03-19 13:21:41 -07001326 }
Adam Cohenf3900c22012-11-16 18:28:11 -08001327 break;
1328 case RIGHT:
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001329 int right = cs.cellX + cs.spanX;
1330 for (int j = cs.cellY; j < cs.cellY + cs.spanY; j++) {
Sunny Goyal6dc98b92016-04-03 15:25:29 -07001331 if (right > rightEdge[j]) {
1332 rightEdge[j] = right;
Adam Cohenf3900c22012-11-16 18:28:11 -08001333 }
1334 }
1335 break;
1336 case TOP:
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001337 int top = cs.cellY;
1338 for (int j = cs.cellX; j < cs.cellX + cs.spanX; j++) {
Sunny Goyal6dc98b92016-04-03 15:25:29 -07001339 if (top < topEdge[j] || topEdge[j] < 0) {
1340 topEdge[j] = top;
Adam Cohenf3900c22012-11-16 18:28:11 -08001341 }
1342 }
1343 break;
1344 case BOTTOM:
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001345 int bottom = cs.cellY + cs.spanY;
1346 for (int j = cs.cellX; j < cs.cellX + cs.spanX; j++) {
Sunny Goyal6dc98b92016-04-03 15:25:29 -07001347 if (bottom > bottomEdge[j]) {
1348 bottomEdge[j] = bottom;
Adam Cohenf3900c22012-11-16 18:28:11 -08001349 }
1350 }
1351 break;
Adam Cohen47a876d2012-03-19 13:21:41 -07001352 }
1353 }
1354 }
Adam Cohenf3900c22012-11-16 18:28:11 -08001355
1356 boolean isViewTouchingEdge(View v, int whichEdge) {
1357 CellAndSpan cs = config.map.get(v);
1358
Sunny Goyal6dc98b92016-04-03 15:25:29 -07001359 if ((dirtyEdges & whichEdge) == whichEdge) {
1360 computeEdge(whichEdge);
1361 dirtyEdges &= ~whichEdge;
1362 }
Adam Cohenf3900c22012-11-16 18:28:11 -08001363
1364 switch (whichEdge) {
1365 case LEFT:
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001366 for (int i = cs.cellY; i < cs.cellY + cs.spanY; i++) {
1367 if (leftEdge[i] == cs.cellX + cs.spanX) {
Adam Cohenf3900c22012-11-16 18:28:11 -08001368 return true;
1369 }
1370 }
1371 break;
1372 case RIGHT:
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001373 for (int i = cs.cellY; i < cs.cellY + cs.spanY; i++) {
1374 if (rightEdge[i] == cs.cellX) {
Adam Cohenf3900c22012-11-16 18:28:11 -08001375 return true;
1376 }
1377 }
1378 break;
1379 case TOP:
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001380 for (int i = cs.cellX; i < cs.cellX + cs.spanX; i++) {
1381 if (topEdge[i] == cs.cellY + cs.spanY) {
Adam Cohenf3900c22012-11-16 18:28:11 -08001382 return true;
1383 }
1384 }
1385 break;
1386 case BOTTOM:
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001387 for (int i = cs.cellX; i < cs.cellX + cs.spanX; i++) {
1388 if (bottomEdge[i] == cs.cellY) {
Adam Cohenf3900c22012-11-16 18:28:11 -08001389 return true;
1390 }
1391 }
1392 break;
1393 }
1394 return false;
1395 }
1396
1397 void shift(int whichEdge, int delta) {
1398 for (View v: views) {
1399 CellAndSpan c = config.map.get(v);
1400 switch (whichEdge) {
1401 case LEFT:
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001402 c.cellX -= delta;
Adam Cohenf3900c22012-11-16 18:28:11 -08001403 break;
1404 case RIGHT:
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001405 c.cellX += delta;
Adam Cohenf3900c22012-11-16 18:28:11 -08001406 break;
1407 case TOP:
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001408 c.cellY -= delta;
Adam Cohenf3900c22012-11-16 18:28:11 -08001409 break;
1410 case BOTTOM:
1411 default:
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001412 c.cellY += delta;
Adam Cohenf3900c22012-11-16 18:28:11 -08001413 break;
1414 }
1415 }
1416 resetEdges();
1417 }
1418
1419 public void addView(View v) {
1420 views.add(v);
1421 resetEdges();
1422 }
1423
1424 public Rect getBoundingRect() {
1425 if (boundingRectDirty) {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001426 config.getBoundingRectForViews(views, boundingRect);
Adam Cohenf3900c22012-11-16 18:28:11 -08001427 }
1428 return boundingRect;
1429 }
1430
Rajeev Kumar9962dbe2017-06-12 12:16:20 -07001431 final PositionComparator comparator = new PositionComparator();
Adam Cohenf3900c22012-11-16 18:28:11 -08001432 class PositionComparator implements Comparator<View> {
1433 int whichEdge = 0;
1434 public int compare(View left, View right) {
1435 CellAndSpan l = config.map.get(left);
1436 CellAndSpan r = config.map.get(right);
1437 switch (whichEdge) {
1438 case LEFT:
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001439 return (r.cellX + r.spanX) - (l.cellX + l.spanX);
Adam Cohenf3900c22012-11-16 18:28:11 -08001440 case RIGHT:
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001441 return l.cellX - r.cellX;
Adam Cohenf3900c22012-11-16 18:28:11 -08001442 case TOP:
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001443 return (r.cellY + r.spanY) - (l.cellY + l.spanY);
Adam Cohenf3900c22012-11-16 18:28:11 -08001444 case BOTTOM:
1445 default:
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001446 return l.cellY - r.cellY;
Adam Cohenf3900c22012-11-16 18:28:11 -08001447 }
1448 }
1449 }
1450
1451 public void sortConfigurationForEdgePush(int edge) {
1452 comparator.whichEdge = edge;
1453 Collections.sort(config.sortedViews, comparator);
1454 }
Adam Cohen47a876d2012-03-19 13:21:41 -07001455 }
1456
Adam Cohenf3900c22012-11-16 18:28:11 -08001457 private boolean pushViewsToTempLocation(ArrayList<View> views, Rect rectOccupiedByPotentialDrop,
1458 int[] direction, View dragView, ItemConfiguration currentState) {
Adam Cohene0489502012-08-27 15:18:53 -07001459
Adam Cohenf3900c22012-11-16 18:28:11 -08001460 ViewCluster cluster = new ViewCluster(views, currentState);
1461 Rect clusterRect = cluster.getBoundingRect();
1462 int whichEdge;
1463 int pushDistance;
1464 boolean fail = false;
1465
1466 // Determine the edge of the cluster that will be leading the push and how far
1467 // the cluster must be shifted.
1468 if (direction[0] < 0) {
1469 whichEdge = ViewCluster.LEFT;
1470 pushDistance = clusterRect.right - rectOccupiedByPotentialDrop.left;
Adam Cohene0489502012-08-27 15:18:53 -07001471 } else if (direction[0] > 0) {
Adam Cohenf3900c22012-11-16 18:28:11 -08001472 whichEdge = ViewCluster.RIGHT;
1473 pushDistance = rectOccupiedByPotentialDrop.right - clusterRect.left;
1474 } else if (direction[1] < 0) {
1475 whichEdge = ViewCluster.TOP;
1476 pushDistance = clusterRect.bottom - rectOccupiedByPotentialDrop.top;
1477 } else {
1478 whichEdge = ViewCluster.BOTTOM;
1479 pushDistance = rectOccupiedByPotentialDrop.bottom - clusterRect.top;
Adam Cohene0489502012-08-27 15:18:53 -07001480 }
1481
Adam Cohenf3900c22012-11-16 18:28:11 -08001482 // Break early for invalid push distance.
1483 if (pushDistance <= 0) {
1484 return false;
Adam Cohene0489502012-08-27 15:18:53 -07001485 }
Adam Cohenf3900c22012-11-16 18:28:11 -08001486
1487 // Mark the occupied state as false for the group of views we want to move.
1488 for (View v: views) {
1489 CellAndSpan c = currentState.map.get(v);
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001490 mTmpOccupied.markCells(c, false);
Adam Cohenf3900c22012-11-16 18:28:11 -08001491 }
1492
1493 // We save the current configuration -- if we fail to find a solution we will revert
1494 // to the initial state. The process of finding a solution modifies the configuration
1495 // in place, hence the need for revert in the failure case.
1496 currentState.save();
1497
1498 // The pushing algorithm is simplified by considering the views in the order in which
1499 // they would be pushed by the cluster. For example, if the cluster is leading with its
1500 // left edge, we consider sort the views by their right edge, from right to left.
1501 cluster.sortConfigurationForEdgePush(whichEdge);
1502
1503 while (pushDistance > 0 && !fail) {
1504 for (View v: currentState.sortedViews) {
1505 // For each view that isn't in the cluster, we see if the leading edge of the
1506 // cluster is contacting the edge of that view. If so, we add that view to the
1507 // cluster.
1508 if (!cluster.views.contains(v) && v != dragView) {
1509 if (cluster.isViewTouchingEdge(v, whichEdge)) {
1510 LayoutParams lp = (LayoutParams) v.getLayoutParams();
1511 if (!lp.canReorder) {
1512 // The push solution includes the all apps button, this is not viable.
1513 fail = true;
1514 break;
1515 }
1516 cluster.addView(v);
1517 CellAndSpan c = currentState.map.get(v);
1518
1519 // Adding view to cluster, mark it as not occupied.
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001520 mTmpOccupied.markCells(c, false);
Adam Cohenf3900c22012-11-16 18:28:11 -08001521 }
1522 }
1523 }
1524 pushDistance--;
1525
1526 // The cluster has been completed, now we move the whole thing over in the appropriate
1527 // direction.
1528 cluster.shift(whichEdge, 1);
1529 }
1530
1531 boolean foundSolution = false;
1532 clusterRect = cluster.getBoundingRect();
1533
1534 // Due to the nature of the algorithm, the only check required to verify a valid solution
1535 // is to ensure that completed shifted cluster lies completely within the cell layout.
1536 if (!fail && clusterRect.left >= 0 && clusterRect.right <= mCountX && clusterRect.top >= 0 &&
1537 clusterRect.bottom <= mCountY) {
1538 foundSolution = true;
1539 } else {
1540 currentState.restore();
1541 }
1542
1543 // In either case, we set the occupied array as marked for the location of the views
1544 for (View v: cluster.views) {
1545 CellAndSpan c = currentState.map.get(v);
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001546 mTmpOccupied.markCells(c, true);
Adam Cohenf3900c22012-11-16 18:28:11 -08001547 }
1548
1549 return foundSolution;
Adam Cohene0489502012-08-27 15:18:53 -07001550 }
1551
Adam Cohen482ed822012-03-02 14:15:13 -08001552 private boolean addViewsToTempLocation(ArrayList<View> views, Rect rectOccupiedByPotentialDrop,
Adam Cohenf3900c22012-11-16 18:28:11 -08001553 int[] direction, View dragView, ItemConfiguration currentState) {
Adam Cohen482ed822012-03-02 14:15:13 -08001554 if (views.size() == 0) return true;
Adam Cohen482ed822012-03-02 14:15:13 -08001555
Adam Cohen8baab352012-03-20 17:39:21 -07001556 boolean success = false;
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001557 Rect boundingRect = new Rect();
Adam Cohen8baab352012-03-20 17:39:21 -07001558 // We construct a rect which represents the entire group of views passed in
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001559 currentState.getBoundingRectForViews(views, boundingRect);
Adam Cohen8baab352012-03-20 17:39:21 -07001560
Adam Cohen8baab352012-03-20 17:39:21 -07001561 // Mark the occupied state as false for the group of views we want to move.
Adam Cohenf3900c22012-11-16 18:28:11 -08001562 for (View v: views) {
Adam Cohen8baab352012-03-20 17:39:21 -07001563 CellAndSpan c = currentState.map.get(v);
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001564 mTmpOccupied.markCells(c, false);
Adam Cohen8baab352012-03-20 17:39:21 -07001565 }
1566
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001567 GridOccupancy blockOccupied = new GridOccupancy(boundingRect.width(), boundingRect.height());
Adam Cohen47a876d2012-03-19 13:21:41 -07001568 int top = boundingRect.top;
1569 int left = boundingRect.left;
Adam Cohen8baab352012-03-20 17:39:21 -07001570 // We mark more precisely which parts of the bounding rect are truly occupied, allowing
Adam Cohena56dc102012-07-13 13:41:42 -07001571 // for interlocking.
Adam Cohenf3900c22012-11-16 18:28:11 -08001572 for (View v: views) {
Adam Cohen8baab352012-03-20 17:39:21 -07001573 CellAndSpan c = currentState.map.get(v);
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001574 blockOccupied.markCells(c.cellX - left, c.cellY - top, c.spanX, c.spanY, true);
Adam Cohen47a876d2012-03-19 13:21:41 -07001575 }
1576
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001577 mTmpOccupied.markCells(rectOccupiedByPotentialDrop, true);
Adam Cohen482ed822012-03-02 14:15:13 -08001578
Adam Cohenf3900c22012-11-16 18:28:11 -08001579 findNearestArea(boundingRect.left, boundingRect.top, boundingRect.width(),
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001580 boundingRect.height(), direction,
1581 mTmpOccupied.cells, blockOccupied.cells, mTempLocation);
Adam Cohen482ed822012-03-02 14:15:13 -08001582
Adam Cohen8baab352012-03-20 17:39:21 -07001583 // If we successfuly found a location by pushing the block of views, we commit it
Adam Cohen482ed822012-03-02 14:15:13 -08001584 if (mTempLocation[0] >= 0 && mTempLocation[1] >= 0) {
Adam Cohen8baab352012-03-20 17:39:21 -07001585 int deltaX = mTempLocation[0] - boundingRect.left;
1586 int deltaY = mTempLocation[1] - boundingRect.top;
Adam Cohenf3900c22012-11-16 18:28:11 -08001587 for (View v: views) {
Adam Cohen8baab352012-03-20 17:39:21 -07001588 CellAndSpan c = currentState.map.get(v);
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001589 c.cellX += deltaX;
1590 c.cellY += deltaY;
Adam Cohen482ed822012-03-02 14:15:13 -08001591 }
1592 success = true;
1593 }
Adam Cohen8baab352012-03-20 17:39:21 -07001594
1595 // In either case, we set the occupied array as marked for the location of the views
Adam Cohenf3900c22012-11-16 18:28:11 -08001596 for (View v: views) {
Adam Cohen8baab352012-03-20 17:39:21 -07001597 CellAndSpan c = currentState.map.get(v);
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001598 mTmpOccupied.markCells(c, true);
Adam Cohen482ed822012-03-02 14:15:13 -08001599 }
1600 return success;
1601 }
1602
Adam Cohen4abc5bd2012-05-29 21:06:03 -07001603 // This method tries to find a reordering solution which satisfies the push mechanic by trying
1604 // to push items in each of the cardinal directions, in an order based on the direction vector
1605 // passed.
1606 private boolean attemptPushInDirection(ArrayList<View> intersectingViews, Rect occupied,
1607 int[] direction, View ignoreView, ItemConfiguration solution) {
1608 if ((Math.abs(direction[0]) + Math.abs(direction[1])) > 1) {
Winson Chung5f8afe62013-08-12 16:19:28 -07001609 // If the direction vector has two non-zero components, we try pushing
Adam Cohen4abc5bd2012-05-29 21:06:03 -07001610 // separately in each of the components.
1611 int temp = direction[1];
1612 direction[1] = 0;
Adam Cohenf3900c22012-11-16 18:28:11 -08001613
1614 if (pushViewsToTempLocation(intersectingViews, occupied, direction,
Adam Cohen4abc5bd2012-05-29 21:06:03 -07001615 ignoreView, solution)) {
1616 return true;
1617 }
1618 direction[1] = temp;
1619 temp = direction[0];
1620 direction[0] = 0;
Adam Cohenf3900c22012-11-16 18:28:11 -08001621
1622 if (pushViewsToTempLocation(intersectingViews, occupied, direction,
Adam Cohen4abc5bd2012-05-29 21:06:03 -07001623 ignoreView, solution)) {
1624 return true;
1625 }
1626 // Revert the direction
1627 direction[0] = temp;
1628
1629 // Now we try pushing in each component of the opposite direction
1630 direction[0] *= -1;
1631 direction[1] *= -1;
1632 temp = direction[1];
1633 direction[1] = 0;
Adam Cohenf3900c22012-11-16 18:28:11 -08001634 if (pushViewsToTempLocation(intersectingViews, occupied, direction,
Adam Cohen4abc5bd2012-05-29 21:06:03 -07001635 ignoreView, solution)) {
1636 return true;
1637 }
1638
1639 direction[1] = temp;
1640 temp = direction[0];
1641 direction[0] = 0;
Adam Cohenf3900c22012-11-16 18:28:11 -08001642 if (pushViewsToTempLocation(intersectingViews, occupied, direction,
Adam Cohen4abc5bd2012-05-29 21:06:03 -07001643 ignoreView, solution)) {
1644 return true;
1645 }
1646 // revert the direction
1647 direction[0] = temp;
1648 direction[0] *= -1;
1649 direction[1] *= -1;
Winson Chung5f8afe62013-08-12 16:19:28 -07001650
Adam Cohen4abc5bd2012-05-29 21:06:03 -07001651 } else {
1652 // If the direction vector has a single non-zero component, we push first in the
1653 // direction of the vector
Adam Cohenf3900c22012-11-16 18:28:11 -08001654 if (pushViewsToTempLocation(intersectingViews, occupied, direction,
Adam Cohen4abc5bd2012-05-29 21:06:03 -07001655 ignoreView, solution)) {
1656 return true;
1657 }
Adam Cohen4abc5bd2012-05-29 21:06:03 -07001658 // Then we try the opposite direction
1659 direction[0] *= -1;
1660 direction[1] *= -1;
Adam Cohenf3900c22012-11-16 18:28:11 -08001661 if (pushViewsToTempLocation(intersectingViews, occupied, direction,
Adam Cohen4abc5bd2012-05-29 21:06:03 -07001662 ignoreView, solution)) {
1663 return true;
1664 }
1665 // Switch the direction back
1666 direction[0] *= -1;
1667 direction[1] *= -1;
Winson Chung5f8afe62013-08-12 16:19:28 -07001668
1669 // If we have failed to find a push solution with the above, then we try
Adam Cohen4abc5bd2012-05-29 21:06:03 -07001670 // to find a solution by pushing along the perpendicular axis.
1671
1672 // Swap the components
1673 int temp = direction[1];
1674 direction[1] = direction[0];
1675 direction[0] = temp;
Adam Cohenf3900c22012-11-16 18:28:11 -08001676 if (pushViewsToTempLocation(intersectingViews, occupied, direction,
Adam Cohen4abc5bd2012-05-29 21:06:03 -07001677 ignoreView, solution)) {
1678 return true;
1679 }
1680
1681 // Then we try the opposite direction
1682 direction[0] *= -1;
1683 direction[1] *= -1;
Adam Cohenf3900c22012-11-16 18:28:11 -08001684 if (pushViewsToTempLocation(intersectingViews, occupied, direction,
Adam Cohen4abc5bd2012-05-29 21:06:03 -07001685 ignoreView, solution)) {
1686 return true;
1687 }
1688 // Switch the direction back
1689 direction[0] *= -1;
1690 direction[1] *= -1;
1691
1692 // Swap the components back
1693 temp = direction[1];
1694 direction[1] = direction[0];
1695 direction[0] = temp;
1696 }
1697 return false;
1698 }
1699
Adam Cohen482ed822012-03-02 14:15:13 -08001700 private boolean rearrangementExists(int cellX, int cellY, int spanX, int spanY, int[] direction,
Adam Cohen8baab352012-03-20 17:39:21 -07001701 View ignoreView, ItemConfiguration solution) {
Winson Chunge3e03bc2012-05-01 15:10:11 -07001702 // Return early if get invalid cell positions
1703 if (cellX < 0 || cellY < 0) return false;
Adam Cohen482ed822012-03-02 14:15:13 -08001704
Adam Cohen8baab352012-03-20 17:39:21 -07001705 mIntersectingViews.clear();
Adam Cohen482ed822012-03-02 14:15:13 -08001706 mOccupiedRect.set(cellX, cellY, cellX + spanX, cellY + spanY);
Adam Cohen482ed822012-03-02 14:15:13 -08001707
Adam Cohen8baab352012-03-20 17:39:21 -07001708 // Mark the desired location of the view currently being dragged.
Adam Cohen482ed822012-03-02 14:15:13 -08001709 if (ignoreView != null) {
Adam Cohen8baab352012-03-20 17:39:21 -07001710 CellAndSpan c = solution.map.get(ignoreView);
Adam Cohen19f37922012-03-21 11:59:11 -07001711 if (c != null) {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001712 c.cellX = cellX;
1713 c.cellY = cellY;
Adam Cohen19f37922012-03-21 11:59:11 -07001714 }
Adam Cohen482ed822012-03-02 14:15:13 -08001715 }
Adam Cohen482ed822012-03-02 14:15:13 -08001716 Rect r0 = new Rect(cellX, cellY, cellX + spanX, cellY + spanY);
1717 Rect r1 = new Rect();
Adam Cohen8baab352012-03-20 17:39:21 -07001718 for (View child: solution.map.keySet()) {
Adam Cohen482ed822012-03-02 14:15:13 -08001719 if (child == ignoreView) continue;
Adam Cohen8baab352012-03-20 17:39:21 -07001720 CellAndSpan c = solution.map.get(child);
Adam Cohen482ed822012-03-02 14:15:13 -08001721 LayoutParams lp = (LayoutParams) child.getLayoutParams();
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001722 r1.set(c.cellX, c.cellY, c.cellX + c.spanX, c.cellY + c.spanY);
Adam Cohen482ed822012-03-02 14:15:13 -08001723 if (Rect.intersects(r0, r1)) {
1724 if (!lp.canReorder) {
1725 return false;
1726 }
1727 mIntersectingViews.add(child);
1728 }
1729 }
Adam Cohen47a876d2012-03-19 13:21:41 -07001730
Rajeev Kumar9962dbe2017-06-12 12:16:20 -07001731 solution.intersectingViews = new ArrayList<>(mIntersectingViews);
Adam Cohenfa3c58f2013-12-06 16:10:55 -08001732
Winson Chung5f8afe62013-08-12 16:19:28 -07001733 // First we try to find a solution which respects the push mechanic. That is,
Adam Cohen4abc5bd2012-05-29 21:06:03 -07001734 // we try to find a solution such that no displaced item travels through another item
1735 // without also displacing that item.
1736 if (attemptPushInDirection(mIntersectingViews, mOccupiedRect, direction, ignoreView,
Adam Cohen19f37922012-03-21 11:59:11 -07001737 solution)) {
Adam Cohen47a876d2012-03-19 13:21:41 -07001738 return true;
1739 }
Adam Cohen47a876d2012-03-19 13:21:41 -07001740
Adam Cohen4abc5bd2012-05-29 21:06:03 -07001741 // Next we try moving the views as a block, but without requiring the push mechanic.
Adam Cohenf3900c22012-11-16 18:28:11 -08001742 if (addViewsToTempLocation(mIntersectingViews, mOccupiedRect, direction, ignoreView,
Adam Cohen19f37922012-03-21 11:59:11 -07001743 solution)) {
Adam Cohen482ed822012-03-02 14:15:13 -08001744 return true;
1745 }
Adam Cohen47a876d2012-03-19 13:21:41 -07001746
Adam Cohen482ed822012-03-02 14:15:13 -08001747 // Ok, they couldn't move as a block, let's move them individually
1748 for (View v : mIntersectingViews) {
Adam Cohen8baab352012-03-20 17:39:21 -07001749 if (!addViewToTempLocation(v, mOccupiedRect, direction, solution)) {
Adam Cohen482ed822012-03-02 14:15:13 -08001750 return false;
1751 }
1752 }
1753 return true;
1754 }
1755
1756 /*
1757 * Returns a pair (x, y), where x,y are in {-1, 0, 1} corresponding to vector between
1758 * the provided point and the provided cell
1759 */
Adam Cohen47a876d2012-03-19 13:21:41 -07001760 private void computeDirectionVector(float deltaX, float deltaY, int[] result) {
Jon Mirandae96798e2016-12-07 12:10:44 -08001761 double angle = Math.atan(deltaY / deltaX);
Adam Cohen482ed822012-03-02 14:15:13 -08001762
1763 result[0] = 0;
1764 result[1] = 0;
1765 if (Math.abs(Math.cos(angle)) > 0.5f) {
1766 result[0] = (int) Math.signum(deltaX);
1767 }
1768 if (Math.abs(Math.sin(angle)) > 0.5f) {
1769 result[1] = (int) Math.signum(deltaY);
1770 }
1771 }
1772
Sunny Goyalf7a29e82015-04-24 15:20:43 -07001773 private ItemConfiguration findReorderSolution(int pixelX, int pixelY, int minSpanX, int minSpanY,
Adam Cohenfa3c58f2013-12-06 16:10:55 -08001774 int spanX, int spanY, int[] direction, View dragView, boolean decX,
1775 ItemConfiguration solution) {
Adam Cohen8baab352012-03-20 17:39:21 -07001776 // Copy the current state into the solution. This solution will be manipulated as necessary.
1777 copyCurrentStateToSolution(solution, false);
1778 // Copy the current occupied array into the temporary occupied array. This array will be
1779 // manipulated as necessary to find a solution.
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001780 mOccupied.copyTo(mTmpOccupied);
Adam Cohen482ed822012-03-02 14:15:13 -08001781
1782 // We find the nearest cell into which we would place the dragged item, assuming there's
1783 // nothing in its way.
1784 int result[] = new int[2];
1785 result = findNearestArea(pixelX, pixelY, spanX, spanY, result);
1786
Rajeev Kumar9962dbe2017-06-12 12:16:20 -07001787 boolean success;
Adam Cohen482ed822012-03-02 14:15:13 -08001788 // First we try the exact nearest position of the item being dragged,
1789 // we will then want to try to move this around to other neighbouring positions
Adam Cohen8baab352012-03-20 17:39:21 -07001790 success = rearrangementExists(result[0], result[1], spanX, spanY, direction, dragView,
1791 solution);
Adam Cohen482ed822012-03-02 14:15:13 -08001792
1793 if (!success) {
1794 // We try shrinking the widget down to size in an alternating pattern, shrink 1 in
1795 // x, then 1 in y etc.
1796 if (spanX > minSpanX && (minSpanY == spanY || decX)) {
Adam Cohenfa3c58f2013-12-06 16:10:55 -08001797 return findReorderSolution(pixelX, pixelY, minSpanX, minSpanY, spanX - 1, spanY,
1798 direction, dragView, false, solution);
Adam Cohen482ed822012-03-02 14:15:13 -08001799 } else if (spanY > minSpanY) {
Adam Cohenfa3c58f2013-12-06 16:10:55 -08001800 return findReorderSolution(pixelX, pixelY, minSpanX, minSpanY, spanX, spanY - 1,
1801 direction, dragView, true, solution);
Adam Cohen482ed822012-03-02 14:15:13 -08001802 }
1803 solution.isSolution = false;
1804 } else {
1805 solution.isSolution = true;
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001806 solution.cellX = result[0];
1807 solution.cellY = result[1];
1808 solution.spanX = spanX;
1809 solution.spanY = spanY;
Adam Cohen482ed822012-03-02 14:15:13 -08001810 }
1811 return solution;
1812 }
1813
1814 private void copyCurrentStateToSolution(ItemConfiguration solution, boolean temp) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07001815 int childCount = mShortcutsAndWidgets.getChildCount();
Adam Cohen482ed822012-03-02 14:15:13 -08001816 for (int i = 0; i < childCount; i++) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07001817 View child = mShortcutsAndWidgets.getChildAt(i);
Adam Cohen482ed822012-03-02 14:15:13 -08001818 LayoutParams lp = (LayoutParams) child.getLayoutParams();
Adam Cohen8baab352012-03-20 17:39:21 -07001819 CellAndSpan c;
Adam Cohen482ed822012-03-02 14:15:13 -08001820 if (temp) {
Adam Cohen8baab352012-03-20 17:39:21 -07001821 c = new CellAndSpan(lp.tmpCellX, lp.tmpCellY, lp.cellHSpan, lp.cellVSpan);
Adam Cohen482ed822012-03-02 14:15:13 -08001822 } else {
Adam Cohen8baab352012-03-20 17:39:21 -07001823 c = new CellAndSpan(lp.cellX, lp.cellY, lp.cellHSpan, lp.cellVSpan);
Adam Cohen482ed822012-03-02 14:15:13 -08001824 }
Adam Cohenf3900c22012-11-16 18:28:11 -08001825 solution.add(child, c);
Adam Cohen482ed822012-03-02 14:15:13 -08001826 }
1827 }
1828
1829 private void copySolutionToTempState(ItemConfiguration solution, View dragView) {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001830 mTmpOccupied.clear();
Adam Cohen482ed822012-03-02 14:15:13 -08001831
Michael Jurkaa52570f2012-03-20 03:18:20 -07001832 int childCount = mShortcutsAndWidgets.getChildCount();
Adam Cohen482ed822012-03-02 14:15:13 -08001833 for (int i = 0; i < childCount; i++) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07001834 View child = mShortcutsAndWidgets.getChildAt(i);
Adam Cohen482ed822012-03-02 14:15:13 -08001835 if (child == dragView) continue;
1836 LayoutParams lp = (LayoutParams) child.getLayoutParams();
Adam Cohen8baab352012-03-20 17:39:21 -07001837 CellAndSpan c = solution.map.get(child);
1838 if (c != null) {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001839 lp.tmpCellX = c.cellX;
1840 lp.tmpCellY = c.cellY;
Adam Cohen8baab352012-03-20 17:39:21 -07001841 lp.cellHSpan = c.spanX;
1842 lp.cellVSpan = c.spanY;
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001843 mTmpOccupied.markCells(c, true);
Adam Cohen482ed822012-03-02 14:15:13 -08001844 }
1845 }
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001846 mTmpOccupied.markCells(solution, true);
Adam Cohen482ed822012-03-02 14:15:13 -08001847 }
1848
1849 private void animateItemsToSolution(ItemConfiguration solution, View dragView, boolean
1850 commitDragView) {
1851
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001852 GridOccupancy occupied = DESTRUCTIVE_REORDER ? mOccupied : mTmpOccupied;
1853 occupied.clear();
Adam Cohen482ed822012-03-02 14:15:13 -08001854
Michael Jurkaa52570f2012-03-20 03:18:20 -07001855 int childCount = mShortcutsAndWidgets.getChildCount();
Adam Cohen482ed822012-03-02 14:15:13 -08001856 for (int i = 0; i < childCount; i++) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07001857 View child = mShortcutsAndWidgets.getChildAt(i);
Adam Cohen482ed822012-03-02 14:15:13 -08001858 if (child == dragView) continue;
Adam Cohen8baab352012-03-20 17:39:21 -07001859 CellAndSpan c = solution.map.get(child);
1860 if (c != null) {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001861 animateChildToPosition(child, c.cellX, c.cellY, REORDER_ANIMATION_DURATION, 0,
Adam Cohen19f37922012-03-21 11:59:11 -07001862 DESTRUCTIVE_REORDER, false);
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001863 occupied.markCells(c, true);
Adam Cohen482ed822012-03-02 14:15:13 -08001864 }
1865 }
1866 if (commitDragView) {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001867 occupied.markCells(solution, true);
Adam Cohen482ed822012-03-02 14:15:13 -08001868 }
1869 }
1870
Adam Cohenfa3c58f2013-12-06 16:10:55 -08001871
1872 // This method starts or changes the reorder preview animations
1873 private void beginOrAdjustReorderPreviewAnimations(ItemConfiguration solution,
1874 View dragView, int delay, int mode) {
Adam Cohen19f37922012-03-21 11:59:11 -07001875 int childCount = mShortcutsAndWidgets.getChildCount();
Adam Cohen19f37922012-03-21 11:59:11 -07001876 for (int i = 0; i < childCount; i++) {
1877 View child = mShortcutsAndWidgets.getChildAt(i);
1878 if (child == dragView) continue;
1879 CellAndSpan c = solution.map.get(child);
Adam Cohenfa3c58f2013-12-06 16:10:55 -08001880 boolean skip = mode == ReorderPreviewAnimation.MODE_HINT && solution.intersectingViews
1881 != null && !solution.intersectingViews.contains(child);
1882
Adam Cohen19f37922012-03-21 11:59:11 -07001883 LayoutParams lp = (LayoutParams) child.getLayoutParams();
Adam Cohenfa3c58f2013-12-06 16:10:55 -08001884 if (c != null && !skip) {
1885 ReorderPreviewAnimation rha = new ReorderPreviewAnimation(child, mode, lp.cellX,
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001886 lp.cellY, c.cellX, c.cellY, c.spanX, c.spanY);
Adam Cohend024f982012-05-23 18:26:45 -07001887 rha.animate();
Adam Cohen19f37922012-03-21 11:59:11 -07001888 }
1889 }
1890 }
1891
Sunny Goyal849c6a22018-08-08 16:33:46 -07001892 private static final Property<ReorderPreviewAnimation, Float> ANIMATION_PROGRESS =
1893 new Property<ReorderPreviewAnimation, Float>(float.class, "animationProgress") {
1894 @Override
1895 public Float get(ReorderPreviewAnimation anim) {
1896 return anim.animationProgress;
1897 }
1898
1899 @Override
1900 public void set(ReorderPreviewAnimation anim, Float progress) {
1901 anim.setAnimationProgress(progress);
1902 }
1903 };
1904
Adam Cohenfa3c58f2013-12-06 16:10:55 -08001905 // Class which represents the reorder preview animations. These animations show that an item is
Adam Cohen19f37922012-03-21 11:59:11 -07001906 // in a temporary state, and hint at where the item will return to.
Adam Cohenfa3c58f2013-12-06 16:10:55 -08001907 class ReorderPreviewAnimation {
Rajeev Kumar9962dbe2017-06-12 12:16:20 -07001908 final View child;
Adam Cohend024f982012-05-23 18:26:45 -07001909 float finalDeltaX;
1910 float finalDeltaY;
1911 float initDeltaX;
1912 float initDeltaY;
Rajeev Kumar9962dbe2017-06-12 12:16:20 -07001913 final float finalScale;
Adam Cohend024f982012-05-23 18:26:45 -07001914 float initScale;
Rajeev Kumar9962dbe2017-06-12 12:16:20 -07001915 final int mode;
Adam Cohenfa3c58f2013-12-06 16:10:55 -08001916 boolean repeating = false;
1917 private static final int PREVIEW_DURATION = 300;
1918 private static final int HINT_DURATION = Workspace.REORDER_TIMEOUT;
1919
Jon Miranda21266912016-12-19 14:12:05 -08001920 private static final float CHILD_DIVIDEND = 4.0f;
1921
Adam Cohenfa3c58f2013-12-06 16:10:55 -08001922 public static final int MODE_HINT = 0;
1923 public static final int MODE_PREVIEW = 1;
1924
Sunny Goyal849c6a22018-08-08 16:33:46 -07001925 float animationProgress = 0;
Sunny Goyalf0b6db72018-08-13 16:10:14 -07001926 ValueAnimator a;
Adam Cohen19f37922012-03-21 11:59:11 -07001927
Adam Cohenfa3c58f2013-12-06 16:10:55 -08001928 public ReorderPreviewAnimation(View child, int mode, int cellX0, int cellY0, int cellX1,
1929 int cellY1, int spanX, int spanY) {
Adam Cohen19f37922012-03-21 11:59:11 -07001930 regionToCenterPoint(cellX0, cellY0, spanX, spanY, mTmpPoint);
1931 final int x0 = mTmpPoint[0];
1932 final int y0 = mTmpPoint[1];
1933 regionToCenterPoint(cellX1, cellY1, spanX, spanY, mTmpPoint);
1934 final int x1 = mTmpPoint[0];
1935 final int y1 = mTmpPoint[1];
1936 final int dX = x1 - x0;
1937 final int dY = y1 - y0;
Jon Miranda21266912016-12-19 14:12:05 -08001938
1939 this.child = child;
1940 this.mode = mode;
1941 setInitialAnimationValues(false);
1942 finalScale = (mChildScale - (CHILD_DIVIDEND / child.getWidth())) * initScale;
1943 finalDeltaX = initDeltaX;
1944 finalDeltaY = initDeltaY;
Adam Cohenfa3c58f2013-12-06 16:10:55 -08001945 int dir = mode == MODE_HINT ? -1 : 1;
Adam Cohen19f37922012-03-21 11:59:11 -07001946 if (dX == dY && dX == 0) {
1947 } else {
1948 if (dY == 0) {
Jon Miranda21266912016-12-19 14:12:05 -08001949 finalDeltaX += - dir * Math.signum(dX) * mReorderPreviewAnimationMagnitude;
Adam Cohen19f37922012-03-21 11:59:11 -07001950 } else if (dX == 0) {
Jon Miranda21266912016-12-19 14:12:05 -08001951 finalDeltaY += - dir * Math.signum(dY) * mReorderPreviewAnimationMagnitude;
Adam Cohen19f37922012-03-21 11:59:11 -07001952 } else {
1953 double angle = Math.atan( (float) (dY) / dX);
Jon Miranda21266912016-12-19 14:12:05 -08001954 finalDeltaX += (int) (- dir * Math.signum(dX) *
Adam Cohenfa3c58f2013-12-06 16:10:55 -08001955 Math.abs(Math.cos(angle) * mReorderPreviewAnimationMagnitude));
Jon Miranda21266912016-12-19 14:12:05 -08001956 finalDeltaY += (int) (- dir * Math.signum(dY) *
Adam Cohenfa3c58f2013-12-06 16:10:55 -08001957 Math.abs(Math.sin(angle) * mReorderPreviewAnimationMagnitude));
Adam Cohen19f37922012-03-21 11:59:11 -07001958 }
1959 }
Jon Miranda21266912016-12-19 14:12:05 -08001960 }
1961
1962 void setInitialAnimationValues(boolean restoreOriginalValues) {
1963 if (restoreOriginalValues) {
1964 if (child instanceof LauncherAppWidgetHostView) {
1965 LauncherAppWidgetHostView lahv = (LauncherAppWidgetHostView) child;
1966 initScale = lahv.getScaleToFit();
1967 initDeltaX = lahv.getTranslationForCentering().x;
1968 initDeltaY = lahv.getTranslationForCentering().y;
1969 } else {
1970 initScale = mChildScale;
1971 initDeltaX = 0;
1972 initDeltaY = 0;
1973 }
1974 } else {
1975 initScale = child.getScaleX();
1976 initDeltaX = child.getTranslationX();
1977 initDeltaY = child.getTranslationY();
1978 }
Adam Cohen19f37922012-03-21 11:59:11 -07001979 }
1980
Adam Cohend024f982012-05-23 18:26:45 -07001981 void animate() {
Jon Miranda21266912016-12-19 14:12:05 -08001982 boolean noMovement = (finalDeltaX == initDeltaX) && (finalDeltaY == initDeltaY);
1983
Adam Cohen19f37922012-03-21 11:59:11 -07001984 if (mShakeAnimators.containsKey(child)) {
Adam Cohenfa3c58f2013-12-06 16:10:55 -08001985 ReorderPreviewAnimation oldAnimation = mShakeAnimators.get(child);
Adam Cohend024f982012-05-23 18:26:45 -07001986 oldAnimation.cancel();
Adam Cohen19f37922012-03-21 11:59:11 -07001987 mShakeAnimators.remove(child);
Jon Miranda21266912016-12-19 14:12:05 -08001988 if (noMovement) {
Adam Cohene7587d22012-05-24 18:50:02 -07001989 completeAnimationImmediately();
1990 return;
1991 }
Adam Cohen19f37922012-03-21 11:59:11 -07001992 }
Jon Miranda21266912016-12-19 14:12:05 -08001993 if (noMovement) {
Adam Cohen19f37922012-03-21 11:59:11 -07001994 return;
1995 }
Sunny Goyal849c6a22018-08-08 16:33:46 -07001996 ValueAnimator va = ObjectAnimator.ofFloat(this, ANIMATION_PROGRESS, 0, 1);
Adam Cohene7587d22012-05-24 18:50:02 -07001997 a = va;
Tony Wickham9e0702f2015-09-02 14:45:39 -07001998
1999 // Animations are disabled in power save mode, causing the repeated animation to jump
2000 // spastically between beginning and end states. Since this looks bad, we don't repeat
2001 // the animation in power save mode.
Tony Wickham8155fa22018-05-18 17:18:49 -07002002 if (!Utilities.isPowerSaverPreventingAnimation(getContext())) {
Tony Wickham9e0702f2015-09-02 14:45:39 -07002003 va.setRepeatMode(ValueAnimator.REVERSE);
2004 va.setRepeatCount(ValueAnimator.INFINITE);
2005 }
2006
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002007 va.setDuration(mode == MODE_HINT ? HINT_DURATION : PREVIEW_DURATION);
Adam Cohend024f982012-05-23 18:26:45 -07002008 va.setStartDelay((int) (Math.random() * 60));
Adam Cohen19f37922012-03-21 11:59:11 -07002009 va.addListener(new AnimatorListenerAdapter() {
2010 public void onAnimationRepeat(Animator animation) {
Adam Cohen19f37922012-03-21 11:59:11 -07002011 // We make sure to end only after a full period
Jon Miranda21266912016-12-19 14:12:05 -08002012 setInitialAnimationValues(true);
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002013 repeating = true;
Adam Cohen19f37922012-03-21 11:59:11 -07002014 }
2015 });
Adam Cohen19f37922012-03-21 11:59:11 -07002016 mShakeAnimators.put(child, this);
2017 va.start();
2018 }
2019
Sunny Goyal849c6a22018-08-08 16:33:46 -07002020 private void setAnimationProgress(float progress) {
2021 animationProgress = progress;
2022 float r1 = (mode == MODE_HINT && repeating) ? 1.0f : animationProgress;
2023 float x = r1 * finalDeltaX + (1 - r1) * initDeltaX;
2024 float y = r1 * finalDeltaY + (1 - r1) * initDeltaY;
2025 child.setTranslationX(x);
2026 child.setTranslationY(y);
2027 float s = animationProgress * finalScale + (1 - animationProgress) * initScale;
2028 child.setScaleX(s);
2029 child.setScaleY(s);
2030 }
2031
Adam Cohend024f982012-05-23 18:26:45 -07002032 private void cancel() {
Adam Cohene7587d22012-05-24 18:50:02 -07002033 if (a != null) {
2034 a.cancel();
2035 }
Adam Cohen19f37922012-03-21 11:59:11 -07002036 }
Adam Cohene7587d22012-05-24 18:50:02 -07002037
Adam Cohen091440a2015-03-18 14:16:05 -07002038 @Thunk void completeAnimationImmediately() {
Adam Cohene7587d22012-05-24 18:50:02 -07002039 if (a != null) {
2040 a.cancel();
2041 }
Brandon Keely50e6e562012-05-08 16:28:49 -07002042
Jon Miranda72dbd912017-01-04 14:03:07 -08002043 setInitialAnimationValues(true);
Sunny Goyalf0b6db72018-08-13 16:10:14 -07002044 a = new PropertyListBuilder()
2045 .scale(initScale)
2046 .translationX(initDeltaX)
2047 .translationY(initDeltaY)
2048 .build(child)
Sunny Goyal9e76f682017-02-13 12:13:43 -08002049 .setDuration(REORDER_ANIMATION_DURATION);
Sunny Goyalf0b6db72018-08-13 16:10:14 -07002050 mLauncher.getDragController().addFirstFrameAnimationHelper(a);
2051 a.setInterpolator(DEACCEL_1_5);
Sunny Goyal5d2fc322015-07-06 22:52:49 -07002052 a.start();
Brandon Keely50e6e562012-05-08 16:28:49 -07002053 }
Adam Cohen19f37922012-03-21 11:59:11 -07002054 }
2055
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002056 private void completeAndClearReorderPreviewAnimations() {
2057 for (ReorderPreviewAnimation a: mShakeAnimators.values()) {
Brandon Keely50e6e562012-05-08 16:28:49 -07002058 a.completeAnimationImmediately();
Adam Cohen19f37922012-03-21 11:59:11 -07002059 }
2060 mShakeAnimators.clear();
2061 }
2062
Adam Cohen482ed822012-03-02 14:15:13 -08002063 private void commitTempPlacement() {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07002064 mTmpOccupied.copyTo(mOccupied);
Sunny Goyalaa8ef112015-06-12 20:04:41 -07002065
2066 long screenId = mLauncher.getWorkspace().getIdForScreen(this);
2067 int container = Favorites.CONTAINER_DESKTOP;
2068
Sunny Goyalc13403c2016-11-18 23:44:48 -08002069 if (mContainerType == HOTSEAT) {
Sunny Goyalaa8ef112015-06-12 20:04:41 -07002070 screenId = -1;
2071 container = Favorites.CONTAINER_HOTSEAT;
2072 }
2073
Michael Jurkaa52570f2012-03-20 03:18:20 -07002074 int childCount = mShortcutsAndWidgets.getChildCount();
Adam Cohen482ed822012-03-02 14:15:13 -08002075 for (int i = 0; i < childCount; i++) {
Adam Cohenea889a22012-03-27 16:45:39 -07002076 View child = mShortcutsAndWidgets.getChildAt(i);
2077 LayoutParams lp = (LayoutParams) child.getLayoutParams();
2078 ItemInfo info = (ItemInfo) child.getTag();
Adam Cohen2acce882012-03-28 19:03:19 -07002079 // We do a null check here because the item info can be null in the case of the
2080 // AllApps button in the hotseat.
2081 if (info != null) {
Sunny Goyalaa8ef112015-06-12 20:04:41 -07002082 final boolean requiresDbUpdate = (info.cellX != lp.tmpCellX
2083 || info.cellY != lp.tmpCellY || info.spanX != lp.cellHSpan
2084 || info.spanY != lp.cellVSpan);
2085
Adam Cohen2acce882012-03-28 19:03:19 -07002086 info.cellX = lp.cellX = lp.tmpCellX;
2087 info.cellY = lp.cellY = lp.tmpCellY;
Adam Cohenbebf0422012-04-11 18:06:28 -07002088 info.spanX = lp.cellHSpan;
2089 info.spanY = lp.cellVSpan;
Sunny Goyalaa8ef112015-06-12 20:04:41 -07002090
2091 if (requiresDbUpdate) {
Sunny Goyal43bf11d2017-02-02 13:52:53 -08002092 mLauncher.getModelWriter().modifyItemInDatabase(info, container, screenId,
Sunny Goyalaa8ef112015-06-12 20:04:41 -07002093 info.cellX, info.cellY, info.spanX, info.spanY);
2094 }
Adam Cohen2acce882012-03-28 19:03:19 -07002095 }
Adam Cohen482ed822012-03-02 14:15:13 -08002096 }
2097 }
2098
Sunny Goyalf7a29e82015-04-24 15:20:43 -07002099 private void setUseTempCoords(boolean useTempCoords) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07002100 int childCount = mShortcutsAndWidgets.getChildCount();
Adam Cohen482ed822012-03-02 14:15:13 -08002101 for (int i = 0; i < childCount; i++) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07002102 LayoutParams lp = (LayoutParams) mShortcutsAndWidgets.getChildAt(i).getLayoutParams();
Adam Cohen482ed822012-03-02 14:15:13 -08002103 lp.useTmpCoords = useTempCoords;
2104 }
2105 }
2106
Sunny Goyalf7a29e82015-04-24 15:20:43 -07002107 private ItemConfiguration findConfigurationNoShuffle(int pixelX, int pixelY, int minSpanX, int minSpanY,
Adam Cohen482ed822012-03-02 14:15:13 -08002108 int spanX, int spanY, View dragView, ItemConfiguration solution) {
2109 int[] result = new int[2];
2110 int[] resultSpan = new int[2];
Sunny Goyalf7a29e82015-04-24 15:20:43 -07002111 findNearestVacantArea(pixelX, pixelY, minSpanX, minSpanY, spanX, spanY, result,
Adam Cohen482ed822012-03-02 14:15:13 -08002112 resultSpan);
2113 if (result[0] >= 0 && result[1] >= 0) {
2114 copyCurrentStateToSolution(solution, false);
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07002115 solution.cellX = result[0];
2116 solution.cellY = result[1];
2117 solution.spanX = resultSpan[0];
2118 solution.spanY = resultSpan[1];
Adam Cohen482ed822012-03-02 14:15:13 -08002119 solution.isSolution = true;
2120 } else {
2121 solution.isSolution = false;
2122 }
2123 return solution;
2124 }
2125
Adam Cohen19f37922012-03-21 11:59:11 -07002126 /* This seems like it should be obvious and straight-forward, but when the direction vector
2127 needs to match with the notion of the dragView pushing other views, we have to employ
2128 a slightly more subtle notion of the direction vector. The question is what two points is
2129 the vector between? The center of the dragView and its desired destination? Not quite, as
2130 this doesn't necessarily coincide with the interaction of the dragView and items occupying
2131 those cells. Instead we use some heuristics to often lock the vector to up, down, left
2132 or right, which helps make pushing feel right.
2133 */
2134 private void getDirectionVectorForDrop(int dragViewCenterX, int dragViewCenterY, int spanX,
2135 int spanY, View dragView, int[] resultDirection) {
2136 int[] targetDestination = new int[2];
2137
2138 findNearestArea(dragViewCenterX, dragViewCenterY, spanX, spanY, targetDestination);
2139 Rect dragRect = new Rect();
2140 regionToRect(targetDestination[0], targetDestination[1], spanX, spanY, dragRect);
2141 dragRect.offset(dragViewCenterX - dragRect.centerX(), dragViewCenterY - dragRect.centerY());
2142
2143 Rect dropRegionRect = new Rect();
2144 getViewsIntersectingRegion(targetDestination[0], targetDestination[1], spanX, spanY,
2145 dragView, dropRegionRect, mIntersectingViews);
2146
2147 int dropRegionSpanX = dropRegionRect.width();
2148 int dropRegionSpanY = dropRegionRect.height();
2149
2150 regionToRect(dropRegionRect.left, dropRegionRect.top, dropRegionRect.width(),
2151 dropRegionRect.height(), dropRegionRect);
2152
2153 int deltaX = (dropRegionRect.centerX() - dragViewCenterX) / spanX;
2154 int deltaY = (dropRegionRect.centerY() - dragViewCenterY) / spanY;
2155
2156 if (dropRegionSpanX == mCountX || spanX == mCountX) {
2157 deltaX = 0;
2158 }
2159 if (dropRegionSpanY == mCountY || spanY == mCountY) {
2160 deltaY = 0;
2161 }
2162
2163 if (deltaX == 0 && deltaY == 0) {
2164 // No idea what to do, give a random direction.
2165 resultDirection[0] = 1;
2166 resultDirection[1] = 0;
2167 } else {
2168 computeDirectionVector(deltaX, deltaY, resultDirection);
2169 }
2170 }
2171
2172 // For a given cell and span, fetch the set of views intersecting the region.
2173 private void getViewsIntersectingRegion(int cellX, int cellY, int spanX, int spanY,
2174 View dragView, Rect boundingRect, ArrayList<View> intersectingViews) {
2175 if (boundingRect != null) {
2176 boundingRect.set(cellX, cellY, cellX + spanX, cellY + spanY);
2177 }
2178 intersectingViews.clear();
2179 Rect r0 = new Rect(cellX, cellY, cellX + spanX, cellY + spanY);
2180 Rect r1 = new Rect();
2181 final int count = mShortcutsAndWidgets.getChildCount();
2182 for (int i = 0; i < count; i++) {
2183 View child = mShortcutsAndWidgets.getChildAt(i);
2184 if (child == dragView) continue;
2185 LayoutParams lp = (LayoutParams) child.getLayoutParams();
2186 r1.set(lp.cellX, lp.cellY, lp.cellX + lp.cellHSpan, lp.cellY + lp.cellVSpan);
2187 if (Rect.intersects(r0, r1)) {
2188 mIntersectingViews.add(child);
2189 if (boundingRect != null) {
2190 boundingRect.union(r1);
2191 }
2192 }
2193 }
2194 }
2195
2196 boolean isNearestDropLocationOccupied(int pixelX, int pixelY, int spanX, int spanY,
2197 View dragView, int[] result) {
2198 result = findNearestArea(pixelX, pixelY, spanX, spanY, result);
2199 getViewsIntersectingRegion(result[0], result[1], spanX, spanY, dragView, null,
2200 mIntersectingViews);
2201 return !mIntersectingViews.isEmpty();
2202 }
2203
2204 void revertTempState() {
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002205 completeAndClearReorderPreviewAnimations();
2206 if (isItemPlacementDirty() && !DESTRUCTIVE_REORDER) {
2207 final int count = mShortcutsAndWidgets.getChildCount();
2208 for (int i = 0; i < count; i++) {
2209 View child = mShortcutsAndWidgets.getChildAt(i);
2210 LayoutParams lp = (LayoutParams) child.getLayoutParams();
2211 if (lp.tmpCellX != lp.cellX || lp.tmpCellY != lp.cellY) {
2212 lp.tmpCellX = lp.cellX;
2213 lp.tmpCellY = lp.cellY;
2214 animateChildToPosition(child, lp.cellX, lp.cellY, REORDER_ANIMATION_DURATION,
2215 0, false, false);
2216 }
Adam Cohen19f37922012-03-21 11:59:11 -07002217 }
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002218 setItemPlacementDirty(false);
Adam Cohen19f37922012-03-21 11:59:11 -07002219 }
Adam Cohen19f37922012-03-21 11:59:11 -07002220 }
2221
Adam Cohenbebf0422012-04-11 18:06:28 -07002222 boolean createAreaForResize(int cellX, int cellY, int spanX, int spanY,
2223 View dragView, int[] direction, boolean commit) {
2224 int[] pixelXY = new int[2];
2225 regionToCenterPoint(cellX, cellY, spanX, spanY, pixelXY);
2226
2227 // First we determine if things have moved enough to cause a different layout
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002228 ItemConfiguration swapSolution = findReorderSolution(pixelXY[0], pixelXY[1], spanX, spanY,
Adam Cohenbebf0422012-04-11 18:06:28 -07002229 spanX, spanY, direction, dragView, true, new ItemConfiguration());
2230
2231 setUseTempCoords(true);
2232 if (swapSolution != null && swapSolution.isSolution) {
2233 // If we're just testing for a possible location (MODE_ACCEPT_DROP), we don't bother
2234 // committing anything or animating anything as we just want to determine if a solution
2235 // exists
2236 copySolutionToTempState(swapSolution, dragView);
2237 setItemPlacementDirty(true);
2238 animateItemsToSolution(swapSolution, dragView, commit);
2239
2240 if (commit) {
2241 commitTempPlacement();
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002242 completeAndClearReorderPreviewAnimations();
Adam Cohenbebf0422012-04-11 18:06:28 -07002243 setItemPlacementDirty(false);
2244 } else {
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002245 beginOrAdjustReorderPreviewAnimations(swapSolution, dragView,
2246 REORDER_ANIMATION_DURATION, ReorderPreviewAnimation.MODE_PREVIEW);
Adam Cohenbebf0422012-04-11 18:06:28 -07002247 }
2248 mShortcutsAndWidgets.requestLayout();
2249 }
2250 return swapSolution.isSolution;
2251 }
2252
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002253 int[] performReorder(int pixelX, int pixelY, int minSpanX, int minSpanY, int spanX, int spanY,
Adam Cohen482ed822012-03-02 14:15:13 -08002254 View dragView, int[] result, int resultSpan[], int mode) {
Adam Cohen482ed822012-03-02 14:15:13 -08002255 // First we determine if things have moved enough to cause a different layout
Adam Cohen47a876d2012-03-19 13:21:41 -07002256 result = findNearestArea(pixelX, pixelY, spanX, spanY, result);
Adam Cohen482ed822012-03-02 14:15:13 -08002257
2258 if (resultSpan == null) {
2259 resultSpan = new int[2];
2260 }
2261
Adam Cohen19f37922012-03-21 11:59:11 -07002262 // When we are checking drop validity or actually dropping, we don't recompute the
2263 // direction vector, since we want the solution to match the preview, and it's possible
2264 // that the exact position of the item has changed to result in a new reordering outcome.
Adam Cohenb209e632012-03-27 17:09:36 -07002265 if ((mode == MODE_ON_DROP || mode == MODE_ON_DROP_EXTERNAL || mode == MODE_ACCEPT_DROP)
2266 && mPreviousReorderDirection[0] != INVALID_DIRECTION) {
Adam Cohen19f37922012-03-21 11:59:11 -07002267 mDirectionVector[0] = mPreviousReorderDirection[0];
2268 mDirectionVector[1] = mPreviousReorderDirection[1];
2269 // We reset this vector after drop
Adam Cohenb209e632012-03-27 17:09:36 -07002270 if (mode == MODE_ON_DROP || mode == MODE_ON_DROP_EXTERNAL) {
2271 mPreviousReorderDirection[0] = INVALID_DIRECTION;
2272 mPreviousReorderDirection[1] = INVALID_DIRECTION;
Adam Cohen19f37922012-03-21 11:59:11 -07002273 }
2274 } else {
2275 getDirectionVectorForDrop(pixelX, pixelY, spanX, spanY, dragView, mDirectionVector);
2276 mPreviousReorderDirection[0] = mDirectionVector[0];
2277 mPreviousReorderDirection[1] = mDirectionVector[1];
2278 }
2279
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002280 // Find a solution involving pushing / displacing any items in the way
2281 ItemConfiguration swapSolution = findReorderSolution(pixelX, pixelY, minSpanX, minSpanY,
Adam Cohen482ed822012-03-02 14:15:13 -08002282 spanX, spanY, mDirectionVector, dragView, true, new ItemConfiguration());
2283
2284 // We attempt the approach which doesn't shuffle views at all
2285 ItemConfiguration noShuffleSolution = findConfigurationNoShuffle(pixelX, pixelY, minSpanX,
2286 minSpanY, spanX, spanY, dragView, new ItemConfiguration());
2287
2288 ItemConfiguration finalSolution = null;
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002289
2290 // If the reorder solution requires resizing (shrinking) the item being dropped, we instead
2291 // favor a solution in which the item is not resized, but
Adam Cohen482ed822012-03-02 14:15:13 -08002292 if (swapSolution.isSolution && swapSolution.area() >= noShuffleSolution.area()) {
2293 finalSolution = swapSolution;
2294 } else if (noShuffleSolution.isSolution) {
2295 finalSolution = noShuffleSolution;
2296 }
2297
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002298 if (mode == MODE_SHOW_REORDER_HINT) {
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002299 if (finalSolution != null) {
Adam Cohenfe692872013-12-11 14:47:23 -08002300 beginOrAdjustReorderPreviewAnimations(finalSolution, dragView, 0,
2301 ReorderPreviewAnimation.MODE_HINT);
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07002302 result[0] = finalSolution.cellX;
2303 result[1] = finalSolution.cellY;
2304 resultSpan[0] = finalSolution.spanX;
2305 resultSpan[1] = finalSolution.spanY;
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002306 } else {
2307 result[0] = result[1] = resultSpan[0] = resultSpan[1] = -1;
2308 }
2309 return result;
2310 }
2311
Adam Cohen482ed822012-03-02 14:15:13 -08002312 boolean foundSolution = true;
2313 if (!DESTRUCTIVE_REORDER) {
2314 setUseTempCoords(true);
2315 }
2316
2317 if (finalSolution != null) {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07002318 result[0] = finalSolution.cellX;
2319 result[1] = finalSolution.cellY;
2320 resultSpan[0] = finalSolution.spanX;
2321 resultSpan[1] = finalSolution.spanY;
Adam Cohen482ed822012-03-02 14:15:13 -08002322
2323 // If we're just testing for a possible location (MODE_ACCEPT_DROP), we don't bother
2324 // committing anything or animating anything as we just want to determine if a solution
2325 // exists
2326 if (mode == MODE_DRAG_OVER || mode == MODE_ON_DROP || mode == MODE_ON_DROP_EXTERNAL) {
2327 if (!DESTRUCTIVE_REORDER) {
2328 copySolutionToTempState(finalSolution, dragView);
2329 }
2330 setItemPlacementDirty(true);
2331 animateItemsToSolution(finalSolution, dragView, mode == MODE_ON_DROP);
2332
Adam Cohen19f37922012-03-21 11:59:11 -07002333 if (!DESTRUCTIVE_REORDER &&
2334 (mode == MODE_ON_DROP || mode == MODE_ON_DROP_EXTERNAL)) {
Adam Cohen482ed822012-03-02 14:15:13 -08002335 commitTempPlacement();
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002336 completeAndClearReorderPreviewAnimations();
Adam Cohen19f37922012-03-21 11:59:11 -07002337 setItemPlacementDirty(false);
2338 } else {
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002339 beginOrAdjustReorderPreviewAnimations(finalSolution, dragView,
2340 REORDER_ANIMATION_DURATION, ReorderPreviewAnimation.MODE_PREVIEW);
Adam Cohen482ed822012-03-02 14:15:13 -08002341 }
2342 }
2343 } else {
2344 foundSolution = false;
2345 result[0] = result[1] = resultSpan[0] = resultSpan[1] = -1;
2346 }
2347
2348 if ((mode == MODE_ON_DROP || !foundSolution) && !DESTRUCTIVE_REORDER) {
2349 setUseTempCoords(false);
2350 }
Adam Cohen482ed822012-03-02 14:15:13 -08002351
Michael Jurkaa52570f2012-03-20 03:18:20 -07002352 mShortcutsAndWidgets.requestLayout();
Adam Cohen482ed822012-03-02 14:15:13 -08002353 return result;
2354 }
2355
Adam Cohen19f37922012-03-21 11:59:11 -07002356 void setItemPlacementDirty(boolean dirty) {
2357 mItemPlacementDirty = dirty;
Adam Cohen482ed822012-03-02 14:15:13 -08002358 }
Adam Cohen19f37922012-03-21 11:59:11 -07002359 boolean isItemPlacementDirty() {
2360 return mItemPlacementDirty;
Adam Cohen482ed822012-03-02 14:15:13 -08002361 }
2362
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07002363 private static class ItemConfiguration extends CellAndSpan {
Rajeev Kumar9962dbe2017-06-12 12:16:20 -07002364 final ArrayMap<View, CellAndSpan> map = new ArrayMap<>();
2365 private final ArrayMap<View, CellAndSpan> savedMap = new ArrayMap<>();
2366 final ArrayList<View> sortedViews = new ArrayList<>();
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002367 ArrayList<View> intersectingViews;
Adam Cohen482ed822012-03-02 14:15:13 -08002368 boolean isSolution = false;
Adam Cohen482ed822012-03-02 14:15:13 -08002369
Adam Cohenf3900c22012-11-16 18:28:11 -08002370 void save() {
2371 // Copy current state into savedMap
2372 for (View v: map.keySet()) {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07002373 savedMap.get(v).copyFrom(map.get(v));
Adam Cohenf3900c22012-11-16 18:28:11 -08002374 }
2375 }
2376
2377 void restore() {
2378 // Restore current state from savedMap
2379 for (View v: savedMap.keySet()) {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07002380 map.get(v).copyFrom(savedMap.get(v));
Adam Cohenf3900c22012-11-16 18:28:11 -08002381 }
2382 }
2383
2384 void add(View v, CellAndSpan cs) {
2385 map.put(v, cs);
2386 savedMap.put(v, new CellAndSpan());
2387 sortedViews.add(v);
2388 }
2389
Adam Cohen482ed822012-03-02 14:15:13 -08002390 int area() {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07002391 return spanX * spanY;
Adam Cohenf3900c22012-11-16 18:28:11 -08002392 }
2393
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07002394 void getBoundingRectForViews(ArrayList<View> views, Rect outRect) {
2395 boolean first = true;
2396 for (View v: views) {
2397 CellAndSpan c = map.get(v);
2398 if (first) {
2399 outRect.set(c.cellX, c.cellY, c.cellX + c.spanX, c.cellY + c.spanY);
2400 first = false;
2401 } else {
2402 outRect.union(c.cellX, c.cellY, c.cellX + c.spanX, c.cellY + c.spanY);
2403 }
2404 }
Adam Cohenf3900c22012-11-16 18:28:11 -08002405 }
Adam Cohen482ed822012-03-02 14:15:13 -08002406 }
2407
Adam Cohendf035382011-04-11 17:22:04 -07002408 /**
Adam Cohendf035382011-04-11 17:22:04 -07002409 * Find a starting cell position that will fit the given bounds nearest the requested
2410 * cell location. Uses Euclidean distance to score multiple vacant areas.
2411 *
2412 * @param pixelX The X location at which you want to search for a vacant area.
2413 * @param pixelY The Y location at which you want to search for a vacant area.
2414 * @param spanX Horizontal span of the object.
2415 * @param spanY Vertical span of the object.
Adam Cohendf035382011-04-11 17:22:04 -07002416 * @param result Previously returned value to possibly recycle.
2417 * @return The X, Y cell of a vacant area that can contain this object,
2418 * nearest the requested location.
2419 */
Adam Cohenf9c184a2016-01-15 16:47:43 -08002420 public int[] findNearestArea(int pixelX, int pixelY, int spanX, int spanY, int[] result) {
Sunny Goyalf7a29e82015-04-24 15:20:43 -07002421 return findNearestArea(pixelX, pixelY, spanX, spanY, spanX, spanY, false, result, null);
Adam Cohendf035382011-04-11 17:22:04 -07002422 }
2423
Michael Jurka0280c3b2010-09-17 15:00:07 -07002424 boolean existsEmptyCell() {
2425 return findCellForSpan(null, 1, 1);
2426 }
2427
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002428 /**
Michael Jurka0280c3b2010-09-17 15:00:07 -07002429 * Finds the upper-left coordinate of the first rectangle in the grid that can
2430 * hold a cell of the specified dimensions. If intersectX and intersectY are not -1,
2431 * then this method will only return coordinates for rectangles that contain the cell
2432 * (intersectX, intersectY)
2433 *
2434 * @param cellXY The array that will contain the position of a vacant cell if such a cell
2435 * can be found.
2436 * @param spanX The horizontal span of the cell we want to find.
2437 * @param spanY The vertical span of the cell we want to find.
2438 *
2439 * @return True if a vacant cell of the specified dimension was found, false otherwise.
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07002440 */
Hyunyoung Song3f471442015-04-08 19:01:34 -07002441 public boolean findCellForSpan(int[] cellXY, int spanX, int spanY) {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07002442 if (cellXY == null) {
2443 cellXY = new int[2];
Michael Jurka0280c3b2010-09-17 15:00:07 -07002444 }
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07002445 return mOccupied.findVacantCell(cellXY, spanX, spanY);
Michael Jurka0280c3b2010-09-17 15:00:07 -07002446 }
2447
2448 /**
Winson Chungc07918d2011-07-01 15:35:26 -07002449 * A drag event has begun over this layout.
2450 * It may have begun over this layout (in which case onDragChild is called first),
2451 * or it may have begun on another layout.
2452 */
2453 void onDragEnter() {
Winson Chungc07918d2011-07-01 15:35:26 -07002454 mDragging = true;
2455 }
2456
2457 /**
Michael Jurka0280c3b2010-09-17 15:00:07 -07002458 * Called when drag has left this CellLayout or has been completed (successfully or not)
2459 */
2460 void onDragExit() {
Joe Onorato4be866d2010-10-10 11:26:02 -07002461 // This can actually be called when we aren't in a drag, e.g. when adding a new
2462 // item to this layout via the customize drawer.
2463 // Guard against that case.
2464 if (mDragging) {
2465 mDragging = false;
Patrick Dubroyde7658b2010-09-27 11:15:43 -07002466 }
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -07002467
2468 // Invalidate the drag data
Adam Cohend41fbf52012-02-16 23:53:59 -08002469 mDragCell[0] = mDragCell[1] = -1;
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -07002470 mDragOutlineAnims[mDragOutlineCurrent].animateOut();
2471 mDragOutlineCurrent = (mDragOutlineCurrent + 1) % mDragOutlineAnims.length;
Adam Cohen19f37922012-03-21 11:59:11 -07002472 revertTempState();
Michael Jurka33945b22010-12-21 18:19:38 -08002473 setIsDragOverlapping(false);
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07002474 }
2475
2476 /**
Winson Chungaafa03c2010-06-11 17:34:16 -07002477 * Mark a child as having been dropped.
Patrick Dubroyde7658b2010-09-27 11:15:43 -07002478 * At the beginning of the drag operation, the child may have been on another
Patrick Dubroyce34a972010-10-19 10:34:32 -07002479 * screen, but it is re-parented before this method is called.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002480 *
2481 * @param child The child that is being dropped
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002482 */
Adam Cohen716b51e2011-06-30 12:09:54 -07002483 void onDropChild(View child) {
Romain Guyd94533d2009-08-17 10:01:15 -07002484 if (child != null) {
2485 LayoutParams lp = (LayoutParams) child.getLayoutParams();
Romain Guy84f296c2009-11-04 15:00:44 -08002486 lp.dropped = true;
Romain Guyd94533d2009-08-17 10:01:15 -07002487 child.requestLayout();
Tony Wickham1cdb6d02015-09-17 11:08:27 -07002488 markCellsAsOccupiedForView(child);
Romain Guyd94533d2009-08-17 10:01:15 -07002489 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002490 }
2491
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002492 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002493 * Computes a bounding rectangle for a range of cells
Winson Chungaafa03c2010-06-11 17:34:16 -07002494 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002495 * @param cellX X coordinate of upper left corner expressed as a cell position
2496 * @param cellY Y coordinate of upper left corner expressed as a cell position
Winson Chungaafa03c2010-06-11 17:34:16 -07002497 * @param cellHSpan Width in cells
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002498 * @param cellVSpan Height in cells
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07002499 * @param resultRect Rect into which to put the results
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002500 */
Adam Cohend41fbf52012-02-16 23:53:59 -08002501 public void cellToRect(int cellX, int cellY, int cellHSpan, int cellVSpan, Rect resultRect) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002502 final int cellWidth = mCellWidth;
2503 final int cellHeight = mCellHeight;
Winson Chungaafa03c2010-06-11 17:34:16 -07002504
Winson Chung4b825dcd2011-06-19 12:41:22 -07002505 final int hStartPadding = getPaddingLeft();
2506 final int vStartPadding = getPaddingTop();
Winson Chungaafa03c2010-06-11 17:34:16 -07002507
Sunny Goyalaa8a8712016-11-20 15:26:01 +05302508 int width = cellHSpan * cellWidth;
2509 int height = cellVSpan * cellHeight;
2510 int x = hStartPadding + cellX * cellWidth;
2511 int y = vStartPadding + cellY * cellHeight;
Winson Chungaafa03c2010-06-11 17:34:16 -07002512
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07002513 resultRect.set(x, y, x + width, y + height);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002514 }
Winson Chungaafa03c2010-06-11 17:34:16 -07002515
Adam Cohend4844c32011-02-18 19:25:06 -08002516 public void markCellsAsOccupiedForView(View view) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07002517 if (view == null || view.getParent() != mShortcutsAndWidgets) return;
Michael Jurka0280c3b2010-09-17 15:00:07 -07002518 LayoutParams lp = (LayoutParams) view.getLayoutParams();
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07002519 mOccupied.markCells(lp.cellX, lp.cellY, lp.cellHSpan, lp.cellVSpan, true);
Michael Jurka0280c3b2010-09-17 15:00:07 -07002520 }
2521
Adam Cohend4844c32011-02-18 19:25:06 -08002522 public void markCellsAsUnoccupiedForView(View view) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07002523 if (view == null || view.getParent() != mShortcutsAndWidgets) return;
Michael Jurka0280c3b2010-09-17 15:00:07 -07002524 LayoutParams lp = (LayoutParams) view.getLayoutParams();
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07002525 mOccupied.markCells(lp.cellX, lp.cellY, lp.cellHSpan, lp.cellVSpan, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002526 }
2527
Adam Cohen2801caf2011-05-13 20:57:39 -07002528 public int getDesiredWidth() {
Sunny Goyalaa8a8712016-11-20 15:26:01 +05302529 return getPaddingLeft() + getPaddingRight() + (mCountX * mCellWidth);
Adam Cohen2801caf2011-05-13 20:57:39 -07002530 }
2531
2532 public int getDesiredHeight() {
Sunny Goyalaa8a8712016-11-20 15:26:01 +05302533 return getPaddingTop() + getPaddingBottom() + (mCountY * mCellHeight);
Adam Cohen2801caf2011-05-13 20:57:39 -07002534 }
2535
Michael Jurka66d72172011-04-12 16:29:25 -07002536 public boolean isOccupied(int x, int y) {
2537 if (x < mCountX && y < mCountY) {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07002538 return mOccupied.cells[x][y];
Michael Jurka66d72172011-04-12 16:29:25 -07002539 } else {
2540 throw new RuntimeException("Position exceeds the bound of this CellLayout");
2541 }
2542 }
2543
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002544 @Override
2545 public ViewGroup.LayoutParams generateLayoutParams(AttributeSet attrs) {
2546 return new CellLayout.LayoutParams(getContext(), attrs);
2547 }
2548
2549 @Override
2550 protected boolean checkLayoutParams(ViewGroup.LayoutParams p) {
2551 return p instanceof CellLayout.LayoutParams;
2552 }
2553
2554 @Override
2555 protected ViewGroup.LayoutParams generateLayoutParams(ViewGroup.LayoutParams p) {
2556 return new CellLayout.LayoutParams(p);
2557 }
2558
2559 public static class LayoutParams extends ViewGroup.MarginLayoutParams {
2560 /**
2561 * Horizontal location of the item in the grid.
2562 */
2563 @ViewDebug.ExportedProperty
2564 public int cellX;
2565
2566 /**
2567 * Vertical location of the item in the grid.
2568 */
2569 @ViewDebug.ExportedProperty
2570 public int cellY;
2571
2572 /**
Adam Cohen482ed822012-03-02 14:15:13 -08002573 * Temporary horizontal location of the item in the grid during reorder
2574 */
2575 public int tmpCellX;
2576
2577 /**
2578 * Temporary vertical location of the item in the grid during reorder
2579 */
2580 public int tmpCellY;
2581
2582 /**
2583 * Indicates that the temporary coordinates should be used to layout the items
2584 */
2585 public boolean useTmpCoords;
2586
2587 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002588 * Number of cells spanned horizontally by the item.
2589 */
2590 @ViewDebug.ExportedProperty
2591 public int cellHSpan;
2592
2593 /**
2594 * Number of cells spanned vertically by the item.
2595 */
2596 @ViewDebug.ExportedProperty
2597 public int cellVSpan;
Winson Chungaafa03c2010-06-11 17:34:16 -07002598
Adam Cohen1b607ed2011-03-03 17:26:50 -08002599 /**
2600 * Indicates whether the item will set its x, y, width and height parameters freely,
2601 * or whether these will be computed based on cellX, cellY, cellHSpan and cellVSpan.
2602 */
Adam Cohend4844c32011-02-18 19:25:06 -08002603 public boolean isLockedToGrid = true;
2604
Adam Cohen482ed822012-03-02 14:15:13 -08002605 /**
2606 * Indicates whether this item can be reordered. Always true except in the case of the
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07002607 * the AllApps button and QSB place holder.
Adam Cohen482ed822012-03-02 14:15:13 -08002608 */
2609 public boolean canReorder = true;
2610
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002611 // X coordinate of the view in the layout.
2612 @ViewDebug.ExportedProperty
Vadim Tryshevfedca432015-08-19 17:55:02 -07002613 public int x;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002614 // Y coordinate of the view in the layout.
2615 @ViewDebug.ExportedProperty
Vadim Tryshevfedca432015-08-19 17:55:02 -07002616 public int y;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002617
Romain Guy84f296c2009-11-04 15:00:44 -08002618 boolean dropped;
Romain Guyfcb9e712009-10-02 16:06:52 -07002619
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002620 public LayoutParams(Context c, AttributeSet attrs) {
2621 super(c, attrs);
2622 cellHSpan = 1;
2623 cellVSpan = 1;
2624 }
2625
2626 public LayoutParams(ViewGroup.LayoutParams source) {
2627 super(source);
2628 cellHSpan = 1;
2629 cellVSpan = 1;
2630 }
Winson Chungaafa03c2010-06-11 17:34:16 -07002631
2632 public LayoutParams(LayoutParams source) {
2633 super(source);
2634 this.cellX = source.cellX;
2635 this.cellY = source.cellY;
2636 this.cellHSpan = source.cellHSpan;
2637 this.cellVSpan = source.cellVSpan;
2638 }
2639
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002640 public LayoutParams(int cellX, int cellY, int cellHSpan, int cellVSpan) {
Romain Guy8f19cdd2010-01-08 15:07:00 -08002641 super(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002642 this.cellX = cellX;
2643 this.cellY = cellY;
2644 this.cellHSpan = cellHSpan;
2645 this.cellVSpan = cellVSpan;
2646 }
2647
Sunny Goyalaa8a8712016-11-20 15:26:01 +05302648 public void setup(int cellWidth, int cellHeight, boolean invertHorizontally, int colCount) {
Jon Miranda7ae64ff2016-11-21 16:18:46 -08002649 setup(cellWidth, cellHeight, invertHorizontally, colCount, 1.0f, 1.0f);
2650 }
2651
2652 /**
2653 * Use this method, as opposed to {@link #setup(int, int, boolean, int)}, if the view needs
2654 * to be scaled.
2655 *
2656 * ie. In multi-window mode, we setup widgets so that they are measured and laid out
2657 * using their full/invariant device profile sizes.
2658 */
2659 public void setup(int cellWidth, int cellHeight, boolean invertHorizontally, int colCount,
2660 float cellScaleX, float cellScaleY) {
Adam Cohend4844c32011-02-18 19:25:06 -08002661 if (isLockedToGrid) {
2662 final int myCellHSpan = cellHSpan;
2663 final int myCellVSpan = cellVSpan;
Adam Cohen2374abf2013-04-16 14:56:57 -07002664 int myCellX = useTmpCoords ? tmpCellX : cellX;
2665 int myCellY = useTmpCoords ? tmpCellY : cellY;
2666
2667 if (invertHorizontally) {
2668 myCellX = colCount - myCellX - cellHSpan;
2669 }
Adam Cohen1b607ed2011-03-03 17:26:50 -08002670
Jon Miranda7ae64ff2016-11-21 16:18:46 -08002671 width = (int) (myCellHSpan * cellWidth / cellScaleX - leftMargin - rightMargin);
2672 height = (int) (myCellVSpan * cellHeight / cellScaleY - topMargin - bottomMargin);
Sunny Goyalaa8a8712016-11-20 15:26:01 +05302673 x = (myCellX * cellWidth + leftMargin);
2674 y = (myCellY * cellHeight + topMargin);
Adam Cohend4844c32011-02-18 19:25:06 -08002675 }
2676 }
Winson Chungaafa03c2010-06-11 17:34:16 -07002677
Winson Chungaafa03c2010-06-11 17:34:16 -07002678 public String toString() {
2679 return "(" + this.cellX + ", " + this.cellY + ")";
2680 }
Adam Cohen7f4eabe2011-04-21 16:19:16 -07002681
2682 public void setWidth(int width) {
2683 this.width = width;
2684 }
2685
2686 public int getWidth() {
2687 return width;
2688 }
2689
2690 public void setHeight(int height) {
2691 this.height = height;
2692 }
2693
2694 public int getHeight() {
2695 return height;
2696 }
2697
2698 public void setX(int x) {
2699 this.x = x;
2700 }
2701
2702 public int getX() {
2703 return x;
2704 }
2705
2706 public void setY(int y) {
2707 this.y = y;
2708 }
2709
2710 public int getY() {
2711 return y;
2712 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002713 }
2714
Michael Jurka0280c3b2010-09-17 15:00:07 -07002715 // This class stores info for two purposes:
2716 // 1. When dragging items (mDragInfo in Workspace), we store the View, its cellX & cellY,
2717 // its spanX, spanY, and the screen it is on
2718 // 2. When long clicking on an empty cell in a CellLayout, we save information about the
2719 // cellX and cellY coordinates and which page was clicked. We then set this as a tag on
2720 // the CellLayout that was long clicked
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07002721 public static final class CellInfo extends CellAndSpan {
Rajeev Kumar9962dbe2017-06-12 12:16:20 -07002722 public final View cell;
2723 final long screenId;
2724 final long container;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002725
Sunny Goyal83a8f042015-05-19 12:52:12 -07002726 public CellInfo(View v, ItemInfo info) {
Adam Cohene0aaa0d2014-05-12 12:44:22 -07002727 cellX = info.cellX;
2728 cellY = info.cellY;
2729 spanX = info.spanX;
2730 spanY = info.spanY;
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07002731 cell = v;
Adam Cohene0aaa0d2014-05-12 12:44:22 -07002732 screenId = info.screenId;
2733 container = info.container;
2734 }
2735
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002736 @Override
2737 public String toString() {
Winson Chungaafa03c2010-06-11 17:34:16 -07002738 return "Cell[view=" + (cell == null ? "null" : cell.getClass())
2739 + ", x=" + cellX + ", y=" + cellY + "]";
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002740 }
2741 }
Michael Jurkad771c962011-08-09 15:00:48 -07002742
Tony Wickham86930612015-09-09 13:50:40 -07002743 /**
2744 * Returns whether an item can be placed in this CellLayout (after rearranging and/or resizing
2745 * if necessary).
2746 */
2747 public boolean hasReorderSolution(ItemInfo itemInfo) {
2748 int[] cellPoint = new int[2];
2749 // Check for a solution starting at every cell.
2750 for (int cellX = 0; cellX < getCountX(); cellX++) {
2751 for (int cellY = 0; cellY < getCountY(); cellY++) {
2752 cellToPoint(cellX, cellY, cellPoint);
2753 if (findReorderSolution(cellPoint[0], cellPoint[1], itemInfo.minSpanX,
2754 itemInfo.minSpanY, itemInfo.spanX, itemInfo.spanY, mDirectionVector, null,
2755 true, new ItemConfiguration()).isSolution) {
2756 return true;
2757 }
2758 }
2759 }
2760 return false;
2761 }
2762
Sunny Goyal9ca9c132015-04-29 14:57:22 -07002763 public boolean isRegionVacant(int x, int y, int spanX, int spanY) {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07002764 return mOccupied.isRegionVacant(x, y, spanX, spanY);
Sunny Goyal9ca9c132015-04-29 14:57:22 -07002765 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002766}