blob: a75c6d1c4d994dddcaa391a6c9fb9b42a4ec4457 [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
Joe Onorato4be866d2010-10-10 11:26:02 -070019import android.animation.Animator;
Michael Jurka629758f2012-06-14 16:18:21 -070020import android.animation.AnimatorListenerAdapter;
Chet Haase00397b12010-10-07 11:13:10 -070021import android.animation.TimeInterpolator;
Patrick Dubroyde7658b2010-09-27 11:15:43 -070022import android.animation.ValueAnimator;
23import android.animation.ValueAnimator.AnimatorUpdateListener;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080024import android.content.Context;
Joe Onorato79e56262009-09-21 15:23:04 -040025import android.content.res.Resources;
Sunny Goyalc13403c2016-11-18 23:44:48 -080026import android.content.res.TypedArray;
Joe Onorato4be866d2010-10-10 11:26:02 -070027import android.graphics.Bitmap;
Winson Chungaafa03c2010-06-11 17:34:16 -070028import android.graphics.Canvas;
Andrew Flynn0dca1ec2012-02-29 13:33:22 -080029import android.graphics.Color;
Joe Onorato4be866d2010-10-10 11:26:02 -070030import android.graphics.Paint;
Patrick Dubroyde7658b2010-09-27 11:15:43 -070031import android.graphics.Point;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080032import android.graphics.Rect;
Adam Cohen482ed822012-03-02 14:15:13 -080033import android.graphics.drawable.ColorDrawable;
Patrick Dubroy6569f2c2010-07-12 14:25:18 -070034import android.graphics.drawable.Drawable;
Adam Cohen1462de32012-07-24 22:34:36 -070035import android.os.Parcelable;
Sunny Goyalc13403c2016-11-18 23:44:48 -080036import android.support.annotation.IntDef;
Adam Cohenc9735cf2015-01-23 16:11:55 -080037import android.support.v4.view.ViewCompat;
Rajeev Kumar9962dbe2017-06-12 12:16:20 -070038import android.util.ArrayMap;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080039import android.util.AttributeSet;
Joe Onorato4be866d2010-10-10 11:26:02 -070040import android.util.Log;
Adam Cohen1462de32012-07-24 22:34:36 -070041import android.util.SparseArray;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080042import android.view.MotionEvent;
43import android.view.View;
44import android.view.ViewDebug;
45import android.view.ViewGroup;
Adam Cohenc9735cf2015-01-23 16:11:55 -080046import android.view.accessibility.AccessibilityEvent;
Winson Chung150fbab2010-09-29 17:14:26 -070047import android.view.animation.DecelerateInterpolator;
Sunny Goyal4b6eb262015-05-14 19:24:40 -070048import com.android.launcher3.BubbleTextView.BubbleTextShadowHandler;
Sunny Goyalaa8ef112015-06-12 20:04:41 -070049import com.android.launcher3.LauncherSettings.Favorites;
Sunny Goyale9b651e2015-04-24 11:44:51 -070050import com.android.launcher3.accessibility.DragAndDropAccessibilityDelegate;
51import com.android.launcher3.accessibility.FolderAccessibilityHelper;
52import com.android.launcher3.accessibility.WorkspaceAccessibilityHelper;
Sunny Goyal9e76f682017-02-13 12:13:43 -080053import com.android.launcher3.anim.PropertyListBuilder;
Sunny Goyal3d706ad2017-03-06 16:56:39 -080054import com.android.launcher3.config.FeatureFlags;
Sunny Goyal26119432016-02-18 22:09:23 +000055import com.android.launcher3.folder.FolderIcon;
Jon Mirandaa0233f72017-06-22 18:34:45 -070056import com.android.launcher3.folder.PreviewBackground;
Sunny Goyal06e21a22016-08-11 16:02:02 -070057import com.android.launcher3.graphics.DragPreviewProvider;
Sunny Goyalff4ba2d2016-04-02 14:12:34 -070058import com.android.launcher3.util.CellAndSpan;
59import com.android.launcher3.util.GridOccupancy;
Sunny Goyale2fd14b2015-08-27 17:45:46 -070060import com.android.launcher3.util.ParcelableSparseArray;
Sunny Goyal9b29ca52017-02-17 10:39:44 -080061import com.android.launcher3.util.Themes;
Adam Cohen091440a2015-03-18 14:16:05 -070062import com.android.launcher3.util.Thunk;
Sunny Goyalc13403c2016-11-18 23:44:48 -080063import java.lang.annotation.Retention;
64import java.lang.annotation.RetentionPolicy;
Adam Cohen69ce2e52011-07-03 19:25:21 -070065import java.util.ArrayList;
Adam Cohenc0dcf592011-06-01 15:30:43 -070066import java.util.Arrays;
Adam Cohenf3900c22012-11-16 18:28:11 -080067import java.util.Collections;
68import java.util.Comparator;
Adam Cohend41fbf52012-02-16 23:53:59 -080069import java.util.Stack;
Adam Cohenc0dcf592011-06-01 15:30:43 -070070
Sunny Goyal4b6eb262015-05-14 19:24:40 -070071public class CellLayout extends ViewGroup implements BubbleTextShadowHandler {
Sunny Goyale9b651e2015-04-24 11:44:51 -070072 public static final int WORKSPACE_ACCESSIBILITY_DRAG = 2;
73 public static final int FOLDER_ACCESSIBILITY_DRAG = 1;
74
Tony Wickhama0628cc2015-10-14 15:23:04 -070075 private static final String TAG = "CellLayout";
76 private static final boolean LOGD = false;
Winson Chungaafa03c2010-06-11 17:34:16 -070077
Rajeev Kumar9962dbe2017-06-12 12:16:20 -070078 private final Launcher mLauncher;
Sunny Goyal4ffec482016-02-09 11:28:52 -080079 @ViewDebug.ExportedProperty(category = "launcher")
Adam Cohen091440a2015-03-18 14:16:05 -070080 @Thunk int mCellWidth;
Sunny Goyal4ffec482016-02-09 11:28:52 -080081 @ViewDebug.ExportedProperty(category = "launcher")
Adam Cohen091440a2015-03-18 14:16:05 -070082 @Thunk int mCellHeight;
Winson Chung11a1a532013-09-13 11:14:45 -070083 private int mFixedCellWidth;
84 private int mFixedCellHeight;
Winson Chungaafa03c2010-06-11 17:34:16 -070085
Sunny Goyal4ffec482016-02-09 11:28:52 -080086 @ViewDebug.ExportedProperty(category = "launcher")
Sunny Goyalff4ba2d2016-04-02 14:12:34 -070087 private int mCountX;
Sunny Goyal4ffec482016-02-09 11:28:52 -080088 @ViewDebug.ExportedProperty(category = "launcher")
Sunny Goyalff4ba2d2016-04-02 14:12:34 -070089 private int mCountY;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080090
Adam Cohen917e3882013-10-31 15:03:35 -070091 private boolean mDropPending = false;
Adam Cohenc50438c2014-08-19 17:43:05 -070092 private boolean mIsDragTarget = true;
Sunny Goyale2fd14b2015-08-27 17:45:46 -070093 private boolean mJailContent = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080094
Patrick Dubroyde7658b2010-09-27 11:15:43 -070095 // These are temporary variables to prevent having to allocate a new object just to
96 // return an (x, y) value from helper functions. Do NOT use them to maintain other state.
Adam Cohen091440a2015-03-18 14:16:05 -070097 @Thunk final int[] mTmpPoint = new int[2];
Sunny Goyal2805e632015-05-20 15:35:32 -070098 @Thunk final int[] mTempLocation = new int[2];
Patrick Dubroy6569f2c2010-07-12 14:25:18 -070099
Sunny Goyalff4ba2d2016-04-02 14:12:34 -0700100 private GridOccupancy mOccupied;
101 private GridOccupancy mTmpOccupied;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800102
Michael Jurkadee05892010-07-27 10:01:56 -0700103 private OnTouchListener mInterceptTouchListener;
Rajeev Kumar9962dbe2017-06-12 12:16:20 -0700104 private final StylusEventHelper mStylusEventHelper;
Michael Jurkadee05892010-07-27 10:01:56 -0700105
Jon Mirandaa0233f72017-06-22 18:34:45 -0700106 private final ArrayList<PreviewBackground> mFolderBackgrounds = new ArrayList<>();
107 final PreviewBackground mFolderLeaveBehind = new PreviewBackground();
Adam Cohen69ce2e52011-07-03 19:25:21 -0700108
Michael Jurka5f1c5092010-09-03 14:15:02 -0700109 private float mBackgroundAlpha;
Adam Cohenf34bab52010-09-30 14:11:56 -0700110
Sunny Goyalf5440cb2016-12-14 15:13:00 -0800111 private static final int[] BACKGROUND_STATE_ACTIVE = new int[] { android.R.attr.state_active };
112 private static final int[] BACKGROUND_STATE_DEFAULT = new int[0];
113 private final Drawable mBackground;
Sunny Goyal2805e632015-05-20 15:35:32 -0700114
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700115 // These values allow a fixed measurement to be set on the CellLayout.
116 private int mFixedWidth = -1;
117 private int mFixedHeight = -1;
118
Michael Jurka33945b22010-12-21 18:19:38 -0800119 // If we're actively dragging something over this screen, mIsDragOverlapping is true
120 private boolean mIsDragOverlapping = false;
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700121
Winson Chung150fbab2010-09-29 17:14:26 -0700122 // These arrays are used to implement the drag visualization on x-large screens.
Joe Onorato4be866d2010-10-10 11:26:02 -0700123 // They are used as circular arrays, indexed by mDragOutlineCurrent.
Rajeev Kumar9962dbe2017-06-12 12:16:20 -0700124 @Thunk final Rect[] mDragOutlines = new Rect[4];
125 @Thunk final float[] mDragOutlineAlphas = new float[mDragOutlines.length];
126 private final InterruptibleInOutAnimator[] mDragOutlineAnims =
Joe Onorato4be866d2010-10-10 11:26:02 -0700127 new InterruptibleInOutAnimator[mDragOutlines.length];
Winson Chung150fbab2010-09-29 17:14:26 -0700128
129 // Used as an index into the above 3 arrays; indicates which is the most current value.
Joe Onorato4be866d2010-10-10 11:26:02 -0700130 private int mDragOutlineCurrent = 0;
Patrick Dubroy8e58e912010-10-14 13:21:48 -0700131 private final Paint mDragOutlinePaint = new Paint();
Winson Chung150fbab2010-09-29 17:14:26 -0700132
Sunny Goyal4fe5a372015-05-14 19:55:10 -0700133 private final ClickShadowView mTouchFeedbackView;
Patrick Dubroy96864c32011-03-10 17:17:23 -0800134
Rajeev Kumar9962dbe2017-06-12 12:16:20 -0700135 @Thunk final ArrayMap<LayoutParams, Animator> mReorderAnimators = new ArrayMap<>();
136 @Thunk final ArrayMap<View, ReorderPreviewAnimation> mShakeAnimators = new ArrayMap<>();
Adam Cohen19f37922012-03-21 11:59:11 -0700137
138 private boolean mItemPlacementDirty = false;
Adam Cohenbfbfd262011-06-13 16:55:12 -0700139
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700140 // When a drag operation is in progress, holds the nearest cell to the touch point
141 private final int[] mDragCell = new int[2];
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800142
Joe Onorato4be866d2010-10-10 11:26:02 -0700143 private boolean mDragging = false;
144
Rajeev Kumar9962dbe2017-06-12 12:16:20 -0700145 private final TimeInterpolator mEaseOutInterpolator;
146 private final ShortcutAndWidgetContainer mShortcutsAndWidgets;
Patrick Dubroyce34a972010-10-19 10:34:32 -0700147
Sunny Goyalc13403c2016-11-18 23:44:48 -0800148 @Retention(RetentionPolicy.SOURCE)
149 @IntDef({WORKSPACE, HOTSEAT, FOLDER})
150 public @interface ContainerType{}
151 public static final int WORKSPACE = 0;
152 public static final int HOTSEAT = 1;
153 public static final int FOLDER = 2;
154
155 @ContainerType private final int mContainerType;
156
Jon Mirandab28c4fc2017-06-20 10:58:36 -0700157 private final float mChildScale = 1f;
Andrew Flynn0dca1ec2012-02-29 13:33:22 -0800158
Adam Cohenfa3c58f2013-12-06 16:10:55 -0800159 public static final int MODE_SHOW_REORDER_HINT = 0;
160 public static final int MODE_DRAG_OVER = 1;
161 public static final int MODE_ON_DROP = 2;
162 public static final int MODE_ON_DROP_EXTERNAL = 3;
163 public static final int MODE_ACCEPT_DROP = 4;
Adam Cohen19f37922012-03-21 11:59:11 -0700164 private static final boolean DESTRUCTIVE_REORDER = false;
Adam Cohen482ed822012-03-02 14:15:13 -0800165 private static final boolean DEBUG_VISUALIZE_OCCUPIED = false;
166
Adam Cohenfa3c58f2013-12-06 16:10:55 -0800167 private static final float REORDER_PREVIEW_MAGNITUDE = 0.12f;
Adam Cohen19f37922012-03-21 11:59:11 -0700168 private static final int REORDER_ANIMATION_DURATION = 150;
Sunny Goyalc13403c2016-11-18 23:44:48 -0800169 @Thunk final float mReorderPreviewAnimationMagnitude;
Adam Cohen19f37922012-03-21 11:59:11 -0700170
Rajeev Kumar9962dbe2017-06-12 12:16:20 -0700171 private final ArrayList<View> mIntersectingViews = new ArrayList<>();
172 private final Rect mOccupiedRect = new Rect();
173 private final int[] mDirectionVector = new int[2];
174 final int[] mPreviousReorderDirection = new int[2];
Adam Cohenb209e632012-03-27 17:09:36 -0700175 private static final int INVALID_DIRECTION = -100;
Adam Cohen482ed822012-03-02 14:15:13 -0800176
Sunny Goyal2805e632015-05-20 15:35:32 -0700177 private final Rect mTempRect = new Rect();
Winson Chung3a6e7f32013-10-09 15:50:52 -0700178
Michael Jurkaca993832012-06-29 15:17:04 -0700179 private final static Paint sPaint = new Paint();
Romain Guy8a0bff52012-05-06 13:14:33 -0700180
Adam Cohenc9735cf2015-01-23 16:11:55 -0800181 // Related to accessible drag and drop
Sunny Goyale9b651e2015-04-24 11:44:51 -0700182 private DragAndDropAccessibilityDelegate mTouchHelper;
Adam Cohenc9735cf2015-01-23 16:11:55 -0800183 private boolean mUseTouchHelper = false;
Adam Cohenc9735cf2015-01-23 16:11:55 -0800184
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800185 public CellLayout(Context context) {
186 this(context, null);
187 }
188
189 public CellLayout(Context context, AttributeSet attrs) {
190 this(context, attrs, 0);
191 }
192
193 public CellLayout(Context context, AttributeSet attrs, int defStyle) {
194 super(context, attrs, defStyle);
Sunny Goyalc13403c2016-11-18 23:44:48 -0800195 TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.CellLayout, defStyle, 0);
196 mContainerType = a.getInteger(R.styleable.CellLayout_containerType, WORKSPACE);
197 a.recycle();
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700198
199 // A ViewGroup usually does not draw, but CellLayout needs to draw a rectangle to show
200 // the user where a dragged item will land when dropped.
201 setWillNotDraw(false);
Romain Guyce3cbd12013-02-25 15:00:36 -0800202 setClipToPadding(false);
Tony2fd02082016-10-07 12:50:01 -0700203 mLauncher = Launcher.getLauncher(context);
Michael Jurkaa63c4522010-08-19 13:52:27 -0700204
Adam Cohen2e6da152015-05-06 11:42:25 -0700205 DeviceProfile grid = mLauncher.getDeviceProfile();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800206
Winson Chung11a1a532013-09-13 11:14:45 -0700207 mCellWidth = mCellHeight = -1;
Nilesh Agrawal5f7099a2014-01-02 15:54:57 -0800208 mFixedCellWidth = mFixedCellHeight = -1;
Sunny Goyalff4ba2d2016-04-02 14:12:34 -0700209
210 mCountX = grid.inv.numColumns;
211 mCountY = grid.inv.numRows;
212 mOccupied = new GridOccupancy(mCountX, mCountY);
213 mTmpOccupied = new GridOccupancy(mCountX, mCountY);
214
Adam Cohen5b53f292012-03-29 14:30:35 -0700215 mPreviousReorderDirection[0] = INVALID_DIRECTION;
216 mPreviousReorderDirection[1] = INVALID_DIRECTION;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800217
Adam Cohenefca0272016-02-24 19:19:06 -0800218 mFolderLeaveBehind.delegateCellX = -1;
219 mFolderLeaveBehind.delegateCellY = -1;
220
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800221 setAlwaysDrawnWithCacheEnabled(false);
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700222 final Resources res = getResources();
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700223
Sunny Goyalf5440cb2016-12-14 15:13:00 -0800224 mBackground = res.getDrawable(R.drawable.bg_celllayout);
Sunny Goyal2805e632015-05-20 15:35:32 -0700225 mBackground.setCallback(this);
Winson Chunge8f1d042015-07-31 12:39:57 -0700226 mBackground.setAlpha((int) (mBackgroundAlpha * 255));
Michael Jurka33945b22010-12-21 18:19:38 -0800227
Sunny Goyalc13403c2016-11-18 23:44:48 -0800228 mReorderPreviewAnimationMagnitude = (REORDER_PREVIEW_MAGNITUDE * grid.iconSizePx);
Adam Cohen19f37922012-03-21 11:59:11 -0700229
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700230 // Initialize the data structures used for the drag visualization.
Patrick Dubroyce34a972010-10-19 10:34:32 -0700231 mEaseOutInterpolator = new DecelerateInterpolator(2.5f); // Quint ease out
Winson Chungb8c69f32011-10-19 21:36:08 -0700232 mDragCell[0] = mDragCell[1] = -1;
Joe Onorato4be866d2010-10-10 11:26:02 -0700233 for (int i = 0; i < mDragOutlines.length; i++) {
Adam Cohend41fbf52012-02-16 23:53:59 -0800234 mDragOutlines[i] = new Rect(-1, -1, -1, -1);
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700235 }
Mario Bertschler54ba6012017-06-08 10:53:53 -0700236 mDragOutlinePaint.setColor(Themes.getAttrColor(context, R.attr.workspaceTextColor));
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700237
238 // When dragging things around the home screens, we show a green outline of
239 // where the item will land. The outlines gradually fade out, leaving a trail
240 // behind the drag path.
241 // Set up all the animations that are used to implement this fading.
242 final int duration = res.getInteger(R.integer.config_dragOutlineFadeTime);
Chet Haase472b2812010-10-14 07:02:04 -0700243 final float fromAlphaValue = 0;
244 final float toAlphaValue = (float)res.getInteger(R.integer.config_dragOutlineMaxAlpha);
Joe Onorato4be866d2010-10-10 11:26:02 -0700245
Patrick Dubroy8e58e912010-10-14 13:21:48 -0700246 Arrays.fill(mDragOutlineAlphas, fromAlphaValue);
Joe Onorato4be866d2010-10-10 11:26:02 -0700247
248 for (int i = 0; i < mDragOutlineAnims.length; i++) {
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700249 final InterruptibleInOutAnimator anim =
Michael Jurkaf1ad6082013-03-13 12:55:46 +0100250 new InterruptibleInOutAnimator(this, duration, fromAlphaValue, toAlphaValue);
Patrick Dubroyce34a972010-10-19 10:34:32 -0700251 anim.getAnimator().setInterpolator(mEaseOutInterpolator);
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700252 final int thisIndex = i;
Chet Haase472b2812010-10-14 07:02:04 -0700253 anim.getAnimator().addUpdateListener(new AnimatorUpdateListener() {
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700254 public void onAnimationUpdate(ValueAnimator animation) {
Joe Onorato4be866d2010-10-10 11:26:02 -0700255 final Bitmap outline = (Bitmap)anim.getTag();
256
257 // If an animation is started and then stopped very quickly, we can still
258 // get spurious updates we've cleared the tag. Guard against this.
259 if (outline == null) {
Tony Wickhama0628cc2015-10-14 15:23:04 -0700260 if (LOGD) {
Patrick Dubroyfe6bd872010-10-13 17:32:10 -0700261 Object val = animation.getAnimatedValue();
262 Log.d(TAG, "anim " + thisIndex + " update: " + val +
263 ", isStopped " + anim.isStopped());
264 }
Joe Onorato4be866d2010-10-10 11:26:02 -0700265 // Try to prevent it from continuing to run
266 animation.cancel();
267 } else {
Chet Haase472b2812010-10-14 07:02:04 -0700268 mDragOutlineAlphas[thisIndex] = (Float) animation.getAnimatedValue();
Adam Cohend41fbf52012-02-16 23:53:59 -0800269 CellLayout.this.invalidate(mDragOutlines[thisIndex]);
Joe Onorato4be866d2010-10-10 11:26:02 -0700270 }
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700271 }
272 });
Joe Onorato4be866d2010-10-10 11:26:02 -0700273 // The animation holds a reference to the drag outline bitmap as long is it's
274 // running. This way the bitmap can be GCed when the animations are complete.
Chet Haase472b2812010-10-14 07:02:04 -0700275 anim.getAnimator().addListener(new AnimatorListenerAdapter() {
Michael Jurka3c4c20f2010-10-28 15:36:06 -0700276 @Override
Joe Onorato4be866d2010-10-10 11:26:02 -0700277 public void onAnimationEnd(Animator animation) {
Chet Haase472b2812010-10-14 07:02:04 -0700278 if ((Float) ((ValueAnimator) animation).getAnimatedValue() == 0f) {
Joe Onorato4be866d2010-10-10 11:26:02 -0700279 anim.setTag(null);
280 }
281 }
282 });
283 mDragOutlineAnims[i] = anim;
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700284 }
Patrick Dubroyce34a972010-10-19 10:34:32 -0700285
Sunny Goyalc13403c2016-11-18 23:44:48 -0800286 mShortcutsAndWidgets = new ShortcutAndWidgetContainer(context, mContainerType);
Sunny Goyalaa8a8712016-11-20 15:26:01 +0530287 mShortcutsAndWidgets.setCellDimensions(mCellWidth, mCellHeight, mCountX, mCountY);
Adam Cohen2374abf2013-04-16 14:56:57 -0700288
Mady Mellorbb835202015-07-15 16:34:34 -0700289 mStylusEventHelper = new StylusEventHelper(new SimpleOnStylusPressListener(this), this);
Mady Melloref044dd2015-06-02 15:35:07 -0700290
Sunny Goyal4fe5a372015-05-14 19:55:10 -0700291 mTouchFeedbackView = new ClickShadowView(context);
292 addView(mTouchFeedbackView);
Michael Jurkaa52570f2012-03-20 03:18:20 -0700293 addView(mShortcutsAndWidgets);
Michael Jurka18014792010-10-14 09:01:34 -0700294 }
295
Sunny Goyale9b651e2015-04-24 11:44:51 -0700296 public void enableAccessibleDrag(boolean enable, int dragType) {
Adam Cohenc9735cf2015-01-23 16:11:55 -0800297 mUseTouchHelper = enable;
298 if (!enable) {
299 ViewCompat.setAccessibilityDelegate(this, null);
300 setImportantForAccessibility(IMPORTANT_FOR_ACCESSIBILITY_NO);
301 getShortcutsAndWidgets().setImportantForAccessibility(IMPORTANT_FOR_ACCESSIBILITY_NO);
302 setOnClickListener(mLauncher);
303 } else {
Sunny Goyale9b651e2015-04-24 11:44:51 -0700304 if (dragType == WORKSPACE_ACCESSIBILITY_DRAG &&
305 !(mTouchHelper instanceof WorkspaceAccessibilityHelper)) {
306 mTouchHelper = new WorkspaceAccessibilityHelper(this);
307 } else if (dragType == FOLDER_ACCESSIBILITY_DRAG &&
308 !(mTouchHelper instanceof FolderAccessibilityHelper)) {
309 mTouchHelper = new FolderAccessibilityHelper(this);
310 }
Adam Cohenc9735cf2015-01-23 16:11:55 -0800311 ViewCompat.setAccessibilityDelegate(this, mTouchHelper);
312 setImportantForAccessibility(IMPORTANT_FOR_ACCESSIBILITY_YES);
313 getShortcutsAndWidgets().setImportantForAccessibility(IMPORTANT_FOR_ACCESSIBILITY_YES);
314 setOnClickListener(mTouchHelper);
315 }
316
317 // Invalidate the accessibility hierarchy
318 if (getParent() != null) {
319 getParent().notifySubtreeAccessibilityStateChanged(
320 this, this, AccessibilityEvent.CONTENT_CHANGE_TYPE_SUBTREE);
321 }
322 }
323
324 @Override
325 public boolean dispatchHoverEvent(MotionEvent event) {
326 // Always attempt to dispatch hover events to accessibility first.
327 if (mUseTouchHelper && mTouchHelper.dispatchHoverEvent(event)) {
328 return true;
329 }
330 return super.dispatchHoverEvent(event);
331 }
332
333 @Override
Adam Cohenc9735cf2015-01-23 16:11:55 -0800334 public boolean onInterceptTouchEvent(MotionEvent ev) {
335 if (mUseTouchHelper ||
336 (mInterceptTouchListener != null && mInterceptTouchListener.onTouch(this, ev))) {
337 return true;
338 }
339 return false;
340 }
341
Mady Melloref044dd2015-06-02 15:35:07 -0700342 @Override
343 public boolean onTouchEvent(MotionEvent ev) {
344 boolean handled = super.onTouchEvent(ev);
345 // Stylus button press on a home screen should not switch between overview mode and
346 // the home screen mode, however, once in overview mode stylus button press should be
347 // enabled to allow rearranging the different home screens. So check what mode
348 // the workspace is in, and only perform stylus button presses while in overview mode.
349 if (mLauncher.mWorkspace.isInOverviewMode()
Mady Mellorbb835202015-07-15 16:34:34 -0700350 && mStylusEventHelper.onMotionEvent(ev)) {
Mady Melloref044dd2015-06-02 15:35:07 -0700351 return true;
352 }
353 return handled;
354 }
355
Chris Craik01f2d7f2013-10-01 14:41:56 -0700356 public void enableHardwareLayer(boolean hasLayer) {
357 mShortcutsAndWidgets.setLayerType(hasLayer ? LAYER_TYPE_HARDWARE : LAYER_TYPE_NONE, sPaint);
Michael Jurkad51f33a2012-06-28 15:35:26 -0700358 }
359
360 public void buildHardwareLayer() {
361 mShortcutsAndWidgets.buildLayer();
Adam Cohen2801caf2011-05-13 20:57:39 -0700362 }
363
Winson Chung5f8afe62013-08-12 16:19:28 -0700364 public void setCellDimensions(int width, int height) {
Winson Chung11a1a532013-09-13 11:14:45 -0700365 mFixedCellWidth = mCellWidth = width;
366 mFixedCellHeight = mCellHeight = height;
Sunny Goyalaa8a8712016-11-20 15:26:01 +0530367 mShortcutsAndWidgets.setCellDimensions(mCellWidth, mCellHeight, mCountX, mCountY);
Winson Chung5f8afe62013-08-12 16:19:28 -0700368 }
369
Adam Cohen2801caf2011-05-13 20:57:39 -0700370 public void setGridSize(int x, int y) {
371 mCountX = x;
372 mCountY = y;
Sunny Goyalff4ba2d2016-04-02 14:12:34 -0700373 mOccupied = new GridOccupancy(mCountX, mCountY);
374 mTmpOccupied = new GridOccupancy(mCountX, mCountY);
Adam Cohen7fbec102012-03-27 12:42:19 -0700375 mTempRectStack.clear();
Sunny Goyalaa8a8712016-11-20 15:26:01 +0530376 mShortcutsAndWidgets.setCellDimensions(mCellWidth, mCellHeight, mCountX, mCountY);
Adam Cohen76fc0852011-06-17 13:26:23 -0700377 requestLayout();
Adam Cohen2801caf2011-05-13 20:57:39 -0700378 }
379
Adam Cohen2374abf2013-04-16 14:56:57 -0700380 // Set whether or not to invert the layout horizontally if the layout is in RTL mode.
381 public void setInvertIfRtl(boolean invert) {
382 mShortcutsAndWidgets.setInvertIfRtl(invert);
383 }
384
Adam Cohen917e3882013-10-31 15:03:35 -0700385 public void setDropPending(boolean pending) {
386 mDropPending = pending;
387 }
388
389 public boolean isDropPending() {
390 return mDropPending;
391 }
392
Sunny Goyal4b6eb262015-05-14 19:24:40 -0700393 @Override
394 public void setPressedIcon(BubbleTextView icon, Bitmap background) {
Sunny Goyal508da152014-08-14 10:53:27 -0700395 if (icon == null || background == null) {
396 mTouchFeedbackView.setBitmap(null);
397 mTouchFeedbackView.animate().cancel();
398 } else {
Sunny Goyal508da152014-08-14 10:53:27 -0700399 if (mTouchFeedbackView.setBitmap(background)) {
Winsonbe9798b2016-07-20 12:55:49 -0700400 mTouchFeedbackView.alignWithIconView(icon, mShortcutsAndWidgets,
401 null /* clipAgainstView */);
Sunny Goyal4fe5a372015-05-14 19:55:10 -0700402 mTouchFeedbackView.animateShadow();
Sunny Goyal508da152014-08-14 10:53:27 -0700403 }
Patrick Dubroy96864c32011-03-10 17:17:23 -0800404 }
405 }
406
Adam Cohenc50438c2014-08-19 17:43:05 -0700407 void disableDragTarget() {
408 mIsDragTarget = false;
409 }
410
Tony Wickham0f97b782015-12-02 17:55:07 -0800411 public boolean isDragTarget() {
412 return mIsDragTarget;
413 }
414
Adam Cohenc50438c2014-08-19 17:43:05 -0700415 void setIsDragOverlapping(boolean isDragOverlapping) {
416 if (mIsDragOverlapping != isDragOverlapping) {
417 mIsDragOverlapping = isDragOverlapping;
Sunny Goyalf5440cb2016-12-14 15:13:00 -0800418 mBackground.setState(mIsDragOverlapping
419 ? BACKGROUND_STATE_ACTIVE : BACKGROUND_STATE_DEFAULT);
Adam Cohenc50438c2014-08-19 17:43:05 -0700420 invalidate();
421 }
422 }
423
Sunny Goyale2fd14b2015-08-27 17:45:46 -0700424 public void disableJailContent() {
425 mJailContent = false;
426 }
427
428 @Override
429 protected void dispatchSaveInstanceState(SparseArray<Parcelable> container) {
430 if (mJailContent) {
431 ParcelableSparseArray jail = getJailedArray(container);
432 super.dispatchSaveInstanceState(jail);
433 container.put(R.id.cell_layout_jail_id, jail);
434 } else {
435 super.dispatchSaveInstanceState(container);
436 }
437 }
438
439 @Override
440 protected void dispatchRestoreInstanceState(SparseArray<Parcelable> container) {
441 super.dispatchRestoreInstanceState(mJailContent ? getJailedArray(container) : container);
442 }
443
444 private ParcelableSparseArray getJailedArray(SparseArray<Parcelable> container) {
445 final Parcelable parcelable = container.get(R.id.cell_layout_jail_id);
446 return parcelable instanceof ParcelableSparseArray ?
447 (ParcelableSparseArray) parcelable : new ParcelableSparseArray();
448 }
449
Tony Wickham0f97b782015-12-02 17:55:07 -0800450 public boolean getIsDragOverlapping() {
451 return mIsDragOverlapping;
452 }
453
Jeff Sharkey83f111d2009-04-20 21:03:13 -0700454 @Override
Patrick Dubroy1262e362010-10-06 15:49:50 -0700455 protected void onDraw(Canvas canvas) {
Sunny Goyal05739772015-05-19 19:59:09 -0700456 if (!mIsDragTarget) {
457 return;
458 }
459
Michael Jurka3e7c7632010-10-02 16:01:03 -0700460 // When we're large, we are either drawn in a "hover" state (ie when dragging an item to
461 // a neighboring page) or with just a normal background (if backgroundAlpha > 0.0f)
462 // When we're small, we are either drawn normally or in the "accepts drops" state (during
463 // a drag). However, we also drag the mini hover background *over* one of those two
464 // backgrounds
Sunny Goyal05739772015-05-19 19:59:09 -0700465 if (mBackgroundAlpha > 0.0f) {
Sunny Goyal2805e632015-05-20 15:35:32 -0700466 mBackground.draw(canvas);
Michael Jurkaa63c4522010-08-19 13:52:27 -0700467 }
Romain Guya6abce82009-11-10 02:54:41 -0800468
Patrick Dubroy8e58e912010-10-14 13:21:48 -0700469 final Paint paint = mDragOutlinePaint;
Joe Onorato4be866d2010-10-10 11:26:02 -0700470 for (int i = 0; i < mDragOutlines.length; i++) {
Chet Haase472b2812010-10-14 07:02:04 -0700471 final float alpha = mDragOutlineAlphas[i];
Joe Onorato4be866d2010-10-10 11:26:02 -0700472 if (alpha > 0) {
Joe Onorato4be866d2010-10-10 11:26:02 -0700473 final Bitmap b = (Bitmap) mDragOutlineAnims[i].getTag();
Chet Haase472b2812010-10-14 07:02:04 -0700474 paint.setAlpha((int)(alpha + .5f));
Sunny Goyal106bf642015-07-16 12:18:06 -0700475 canvas.drawBitmap(b, null, mDragOutlines[i], paint);
Winson Chung150fbab2010-09-29 17:14:26 -0700476 }
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700477 }
Patrick Dubroy96864c32011-03-10 17:17:23 -0800478
Adam Cohen482ed822012-03-02 14:15:13 -0800479 if (DEBUG_VISUALIZE_OCCUPIED) {
480 int[] pt = new int[2];
481 ColorDrawable cd = new ColorDrawable(Color.RED);
Adam Cohene7587d22012-05-24 18:50:02 -0700482 cd.setBounds(0, 0, mCellWidth, mCellHeight);
Adam Cohen482ed822012-03-02 14:15:13 -0800483 for (int i = 0; i < mCountX; i++) {
484 for (int j = 0; j < mCountY; j++) {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -0700485 if (mOccupied.cells[i][j]) {
Adam Cohen482ed822012-03-02 14:15:13 -0800486 cellToPoint(i, j, pt);
487 canvas.save();
488 canvas.translate(pt[0], pt[1]);
489 cd.draw(canvas);
490 canvas.restore();
491 }
492 }
493 }
494 }
495
Adam Cohenefca0272016-02-24 19:19:06 -0800496 for (int i = 0; i < mFolderBackgrounds.size(); i++) {
Jon Mirandaa0233f72017-06-22 18:34:45 -0700497 PreviewBackground bg = mFolderBackgrounds.get(i);
Adam Cohenefca0272016-02-24 19:19:06 -0800498 cellToPoint(bg.delegateCellX, bg.delegateCellY, mTempLocation);
499 canvas.save();
500 canvas.translate(mTempLocation[0], mTempLocation[1]);
Sunny Goyal19b93b72017-02-19 20:21:37 -0800501 bg.drawBackground(canvas);
Adam Cohenf172b742016-03-30 19:28:34 -0700502 if (!bg.isClipping) {
Sunny Goyal19b93b72017-02-19 20:21:37 -0800503 bg.drawBackgroundStroke(canvas);
Adam Cohenf172b742016-03-30 19:28:34 -0700504 }
Adam Cohenefca0272016-02-24 19:19:06 -0800505 canvas.restore();
Adam Cohen69ce2e52011-07-03 19:25:21 -0700506 }
Adam Cohenc51934b2011-07-26 21:07:43 -0700507
Adam Cohenefca0272016-02-24 19:19:06 -0800508 if (mFolderLeaveBehind.delegateCellX >= 0 && mFolderLeaveBehind.delegateCellY >= 0) {
509 cellToPoint(mFolderLeaveBehind.delegateCellX,
510 mFolderLeaveBehind.delegateCellY, mTempLocation);
511 canvas.save();
512 canvas.translate(mTempLocation[0], mTempLocation[1]);
Sunny Goyal19b93b72017-02-19 20:21:37 -0800513 mFolderLeaveBehind.drawLeaveBehind(canvas);
Adam Cohenefca0272016-02-24 19:19:06 -0800514 canvas.restore();
Adam Cohenc51934b2011-07-26 21:07:43 -0700515 }
Adam Cohen69ce2e52011-07-03 19:25:21 -0700516 }
517
Adam Cohenefca0272016-02-24 19:19:06 -0800518 @Override
519 protected void dispatchDraw(Canvas canvas) {
520 super.dispatchDraw(canvas);
521
522 for (int i = 0; i < mFolderBackgrounds.size(); i++) {
Jon Mirandaa0233f72017-06-22 18:34:45 -0700523 PreviewBackground bg = mFolderBackgrounds.get(i);
Adam Cohenf172b742016-03-30 19:28:34 -0700524 if (bg.isClipping) {
525 cellToPoint(bg.delegateCellX, bg.delegateCellY, mTempLocation);
526 canvas.save();
527 canvas.translate(mTempLocation[0], mTempLocation[1]);
Sunny Goyal19b93b72017-02-19 20:21:37 -0800528 bg.drawBackgroundStroke(canvas);
Adam Cohenf172b742016-03-30 19:28:34 -0700529 canvas.restore();
530 }
Adam Cohenefca0272016-02-24 19:19:06 -0800531 }
Adam Cohen69ce2e52011-07-03 19:25:21 -0700532 }
533
Jon Mirandaa0233f72017-06-22 18:34:45 -0700534 public void addFolderBackground(PreviewBackground bg) {
Adam Cohenefca0272016-02-24 19:19:06 -0800535 mFolderBackgrounds.add(bg);
536 }
Jon Mirandaa0233f72017-06-22 18:34:45 -0700537 public void removeFolderBackground(PreviewBackground bg) {
Adam Cohenefca0272016-02-24 19:19:06 -0800538 mFolderBackgrounds.remove(bg);
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700539 }
540
Adam Cohenc51934b2011-07-26 21:07:43 -0700541 public void setFolderLeaveBehindCell(int x, int y) {
Adam Cohenefca0272016-02-24 19:19:06 -0800542 View child = getChildAt(x, y);
Sunny Goyal368ae772017-05-24 13:19:15 -0700543 mFolderLeaveBehind.setup(mLauncher, null,
Adam Cohenefca0272016-02-24 19:19:06 -0800544 child.getMeasuredWidth(), child.getPaddingTop());
545
546 mFolderLeaveBehind.delegateCellX = x;
547 mFolderLeaveBehind.delegateCellY = y;
Adam Cohenc51934b2011-07-26 21:07:43 -0700548 invalidate();
549 }
550
551 public void clearFolderLeaveBehind() {
Adam Cohenefca0272016-02-24 19:19:06 -0800552 mFolderLeaveBehind.delegateCellX = -1;
553 mFolderLeaveBehind.delegateCellY = -1;
Adam Cohenc51934b2011-07-26 21:07:43 -0700554 invalidate();
555 }
556
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700557 @Override
Michael Jurkae6235dd2011-10-04 15:02:05 -0700558 public boolean shouldDelayChildPressedState() {
559 return false;
560 }
561
Adam Cohen1462de32012-07-24 22:34:36 -0700562 public void restoreInstanceState(SparseArray<Parcelable> states) {
Sunny Goyal33a152f2014-07-22 12:13:14 -0700563 try {
564 dispatchRestoreInstanceState(states);
565 } catch (IllegalArgumentException ex) {
Sunny Goyal3d706ad2017-03-06 16:56:39 -0800566 if (FeatureFlags.IS_DOGFOOD_BUILD) {
Sunny Goyal33a152f2014-07-22 12:13:14 -0700567 throw ex;
568 }
569 // Mismatched viewId / viewType preventing restore. Skip restore on production builds.
570 Log.e(TAG, "Ignoring an error while restoring a view instance state", ex);
571 }
Adam Cohen1462de32012-07-24 22:34:36 -0700572 }
573
Michael Jurkae6235dd2011-10-04 15:02:05 -0700574 @Override
Jeff Sharkey83f111d2009-04-20 21:03:13 -0700575 public void cancelLongPress() {
576 super.cancelLongPress();
577
578 // Cancel long press for all children
579 final int count = getChildCount();
580 for (int i = 0; i < count; i++) {
581 final View child = getChildAt(i);
582 child.cancelLongPress();
583 }
584 }
585
Michael Jurkadee05892010-07-27 10:01:56 -0700586 public void setOnInterceptTouchListener(View.OnTouchListener listener) {
587 mInterceptTouchListener = listener;
588 }
589
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800590 public int getCountX() {
Adam Cohend22015c2010-07-26 22:02:18 -0700591 return mCountX;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800592 }
593
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800594 public int getCountY() {
Adam Cohend22015c2010-07-26 22:02:18 -0700595 return mCountY;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800596 }
597
Sunny Goyalc13403c2016-11-18 23:44:48 -0800598 public boolean acceptsWidget() {
599 return mContainerType == WORKSPACE;
Sunny Goyale9b651e2015-04-24 11:44:51 -0700600 }
601
Andrew Flynn0dca1ec2012-02-29 13:33:22 -0800602 public boolean addViewToCellLayout(View child, int index, int childId, LayoutParams params,
Sunny Goyalf7a29e82015-04-24 15:20:43 -0700603 boolean markCells) {
Winson Chungaafa03c2010-06-11 17:34:16 -0700604 final LayoutParams lp = params;
605
Andrew Flynnde38e422012-05-08 11:22:15 -0700606 // Hotseat icons - remove text
Andrew Flynn0dca1ec2012-02-29 13:33:22 -0800607 if (child instanceof BubbleTextView) {
608 BubbleTextView bubbleChild = (BubbleTextView) child;
Jon Mirandaf1eae802017-10-04 11:23:33 -0700609 bubbleChild.setTextVisibility(mContainerType != HOTSEAT);
Andrew Flynn0dca1ec2012-02-29 13:33:22 -0800610 }
611
Sunny Goyalc13403c2016-11-18 23:44:48 -0800612 child.setScaleX(mChildScale);
613 child.setScaleY(mChildScale);
Adam Cohen307fe232012-08-16 17:55:58 -0700614
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800615 // Generate an id for each view, this assumes we have at most 256x256 cells
616 // per workspace screen
Adam Cohend22015c2010-07-26 22:02:18 -0700617 if (lp.cellX >= 0 && lp.cellX <= mCountX - 1 && lp.cellY >= 0 && lp.cellY <= mCountY - 1) {
Winson Chungaafa03c2010-06-11 17:34:16 -0700618 // If the horizontal or vertical span is set to -1, it is taken to
619 // mean that it spans the extent of the CellLayout
Adam Cohend22015c2010-07-26 22:02:18 -0700620 if (lp.cellHSpan < 0) lp.cellHSpan = mCountX;
621 if (lp.cellVSpan < 0) lp.cellVSpan = mCountY;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800622
Winson Chungaafa03c2010-06-11 17:34:16 -0700623 child.setId(childId);
Tony Wickhama0628cc2015-10-14 15:23:04 -0700624 if (LOGD) {
625 Log.d(TAG, "Adding view to ShortcutsAndWidgetsContainer: " + child);
626 }
Sunny Goyalf7a29e82015-04-24 15:20:43 -0700627 mShortcutsAndWidgets.addView(child, index, lp);
Michael Jurkadee05892010-07-27 10:01:56 -0700628
Michael Jurkaf3ca3ab2010-10-20 17:08:24 -0700629 if (markCells) markCellsAsOccupiedForView(child);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700630
Winson Chungaafa03c2010-06-11 17:34:16 -0700631 return true;
632 }
633 return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800634 }
Michael Jurka3e7c7632010-10-02 16:01:03 -0700635
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800636 @Override
Michael Jurka0280c3b2010-09-17 15:00:07 -0700637 public void removeAllViews() {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -0700638 mOccupied.clear();
Michael Jurkaa52570f2012-03-20 03:18:20 -0700639 mShortcutsAndWidgets.removeAllViews();
Michael Jurka0280c3b2010-09-17 15:00:07 -0700640 }
641
642 @Override
643 public void removeAllViewsInLayout() {
Michael Jurkaa52570f2012-03-20 03:18:20 -0700644 if (mShortcutsAndWidgets.getChildCount() > 0) {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -0700645 mOccupied.clear();
Michael Jurkaa52570f2012-03-20 03:18:20 -0700646 mShortcutsAndWidgets.removeAllViewsInLayout();
Michael Jurka7cfc2822011-08-02 20:19:24 -0700647 }
Michael Jurka0280c3b2010-09-17 15:00:07 -0700648 }
649
650 @Override
651 public void removeView(View view) {
652 markCellsAsUnoccupiedForView(view);
Michael Jurkaa52570f2012-03-20 03:18:20 -0700653 mShortcutsAndWidgets.removeView(view);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700654 }
655
656 @Override
657 public void removeViewAt(int index) {
Michael Jurkaa52570f2012-03-20 03:18:20 -0700658 markCellsAsUnoccupiedForView(mShortcutsAndWidgets.getChildAt(index));
659 mShortcutsAndWidgets.removeViewAt(index);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700660 }
661
662 @Override
663 public void removeViewInLayout(View view) {
664 markCellsAsUnoccupiedForView(view);
Michael Jurkaa52570f2012-03-20 03:18:20 -0700665 mShortcutsAndWidgets.removeViewInLayout(view);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700666 }
667
668 @Override
669 public void removeViews(int start, int count) {
670 for (int i = start; i < start + count; i++) {
Michael Jurkaa52570f2012-03-20 03:18:20 -0700671 markCellsAsUnoccupiedForView(mShortcutsAndWidgets.getChildAt(i));
Michael Jurka0280c3b2010-09-17 15:00:07 -0700672 }
Michael Jurkaa52570f2012-03-20 03:18:20 -0700673 mShortcutsAndWidgets.removeViews(start, count);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700674 }
675
676 @Override
677 public void removeViewsInLayout(int start, int count) {
678 for (int i = start; i < start + count; i++) {
Michael Jurkaa52570f2012-03-20 03:18:20 -0700679 markCellsAsUnoccupiedForView(mShortcutsAndWidgets.getChildAt(i));
Michael Jurka0280c3b2010-09-17 15:00:07 -0700680 }
Michael Jurkaa52570f2012-03-20 03:18:20 -0700681 mShortcutsAndWidgets.removeViewsInLayout(start, count);
Michael Jurkaabded662011-03-04 12:06:57 -0800682 }
683
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700684 /**
Winson Chungaafa03c2010-06-11 17:34:16 -0700685 * Given a point, return the cell that strictly encloses that point
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800686 * @param x X coordinate of the point
687 * @param y Y coordinate of the point
688 * @param result Array of 2 ints to hold the x and y coordinate of the cell
689 */
Sunny Goyale9b651e2015-04-24 11:44:51 -0700690 public void pointToCellExact(int x, int y, int[] result) {
Winson Chung4b825dcd2011-06-19 12:41:22 -0700691 final int hStartPadding = getPaddingLeft();
692 final int vStartPadding = getPaddingTop();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800693
Sunny Goyalaa8a8712016-11-20 15:26:01 +0530694 result[0] = (x - hStartPadding) / mCellWidth;
695 result[1] = (y - vStartPadding) / mCellHeight;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800696
Adam Cohend22015c2010-07-26 22:02:18 -0700697 final int xAxis = mCountX;
698 final int yAxis = mCountY;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800699
700 if (result[0] < 0) result[0] = 0;
701 if (result[0] >= xAxis) result[0] = xAxis - 1;
702 if (result[1] < 0) result[1] = 0;
703 if (result[1] >= yAxis) result[1] = yAxis - 1;
704 }
Winson Chungaafa03c2010-06-11 17:34:16 -0700705
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800706 /**
707 * Given a point, return the cell that most closely encloses that point
708 * @param x X coordinate of the point
709 * @param y Y coordinate of the point
710 * @param result Array of 2 ints to hold the x and y coordinate of the cell
711 */
712 void pointToCellRounded(int x, int y, int[] result) {
713 pointToCellExact(x + (mCellWidth / 2), y + (mCellHeight / 2), result);
714 }
715
716 /**
717 * Given a cell coordinate, return the point that represents the upper left corner of that cell
Winson Chungaafa03c2010-06-11 17:34:16 -0700718 *
719 * @param cellX X coordinate of the cell
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800720 * @param cellY Y coordinate of the cell
Winson Chungaafa03c2010-06-11 17:34:16 -0700721 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800722 * @param result Array of 2 ints to hold the x and y coordinate of the point
723 */
724 void cellToPoint(int cellX, int cellY, int[] result) {
Winson Chung4b825dcd2011-06-19 12:41:22 -0700725 final int hStartPadding = getPaddingLeft();
726 final int vStartPadding = getPaddingTop();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800727
Sunny Goyalaa8a8712016-11-20 15:26:01 +0530728 result[0] = hStartPadding + cellX * mCellWidth;
729 result[1] = vStartPadding + cellY * mCellHeight;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800730 }
731
Adam Cohene3e27a82011-04-15 12:07:39 -0700732 /**
Adam Cohen482ed822012-03-02 14:15:13 -0800733 * Given a cell coordinate, return the point that represents the center of the cell
Adam Cohene3e27a82011-04-15 12:07:39 -0700734 *
735 * @param cellX X coordinate of the cell
736 * @param cellY Y coordinate of the cell
737 *
738 * @param result Array of 2 ints to hold the x and y coordinate of the point
739 */
740 void cellToCenterPoint(int cellX, int cellY, int[] result) {
Adam Cohen47a876d2012-03-19 13:21:41 -0700741 regionToCenterPoint(cellX, cellY, 1, 1, result);
742 }
743
744 /**
745 * Given a cell coordinate and span return the point that represents the center of the regio
746 *
747 * @param cellX X coordinate of the cell
748 * @param cellY Y coordinate of the cell
749 *
750 * @param result Array of 2 ints to hold the x and y coordinate of the point
751 */
752 void regionToCenterPoint(int cellX, int cellY, int spanX, int spanY, int[] result) {
Winson Chung4b825dcd2011-06-19 12:41:22 -0700753 final int hStartPadding = getPaddingLeft();
754 final int vStartPadding = getPaddingTop();
Sunny Goyalaa8a8712016-11-20 15:26:01 +0530755 result[0] = hStartPadding + cellX * mCellWidth + (spanX * mCellWidth) / 2;
756 result[1] = vStartPadding + cellY * mCellHeight + (spanY * mCellHeight) / 2;
Adam Cohene3e27a82011-04-15 12:07:39 -0700757 }
758
Adam Cohen19f37922012-03-21 11:59:11 -0700759 /**
760 * Given a cell coordinate and span fills out a corresponding pixel rect
761 *
762 * @param cellX X coordinate of the cell
763 * @param cellY Y coordinate of the cell
764 * @param result Rect in which to write the result
765 */
766 void regionToRect(int cellX, int cellY, int spanX, int spanY, Rect result) {
767 final int hStartPadding = getPaddingLeft();
768 final int vStartPadding = getPaddingTop();
Sunny Goyalaa8a8712016-11-20 15:26:01 +0530769 final int left = hStartPadding + cellX * mCellWidth;
770 final int top = vStartPadding + cellY * mCellHeight;
771 result.set(left, top, left + (spanX * mCellWidth), top + (spanY * mCellHeight));
Adam Cohen19f37922012-03-21 11:59:11 -0700772 }
773
Adam Cohen482ed822012-03-02 14:15:13 -0800774 public float getDistanceFromCell(float x, float y, int[] cell) {
775 cellToCenterPoint(cell[0], cell[1], mTmpPoint);
Sunny Goyalf7a29e82015-04-24 15:20:43 -0700776 return (float) Math.hypot(x - mTmpPoint[0], y - mTmpPoint[1]);
Adam Cohen482ed822012-03-02 14:15:13 -0800777 }
778
Adam Cohenf9c184a2016-01-15 16:47:43 -0800779 public int getCellWidth() {
Romain Guy84f296c2009-11-04 15:00:44 -0800780 return mCellWidth;
781 }
782
Sunny Goyal0b754e52017-08-07 07:42:45 -0700783 public int getCellHeight() {
Romain Guy84f296c2009-11-04 15:00:44 -0800784 return mCellHeight;
785 }
786
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700787 public void setFixedSize(int width, int height) {
788 mFixedWidth = width;
789 mFixedHeight = height;
790 }
791
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800792 @Override
793 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800794 int widthSpecMode = MeasureSpec.getMode(widthMeasureSpec);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800795 int heightSpecMode = MeasureSpec.getMode(heightMeasureSpec);
Winson Chung5f8afe62013-08-12 16:19:28 -0700796 int widthSize = MeasureSpec.getSize(widthMeasureSpec);
797 int heightSize = MeasureSpec.getSize(heightMeasureSpec);
Winson Chung2d75f122013-09-23 16:53:31 -0700798 int childWidthSize = widthSize - (getPaddingLeft() + getPaddingRight());
799 int childHeightSize = heightSize - (getPaddingTop() + getPaddingBottom());
Winson Chung11a1a532013-09-13 11:14:45 -0700800 if (mFixedCellWidth < 0 || mFixedCellHeight < 0) {
Sunny Goyalc6205602015-05-21 20:46:33 -0700801 int cw = DeviceProfile.calculateCellWidth(childWidthSize, mCountX);
802 int ch = DeviceProfile.calculateCellHeight(childHeightSize, mCountY);
Winson Chung11a1a532013-09-13 11:14:45 -0700803 if (cw != mCellWidth || ch != mCellHeight) {
804 mCellWidth = cw;
805 mCellHeight = ch;
Sunny Goyalaa8a8712016-11-20 15:26:01 +0530806 mShortcutsAndWidgets.setCellDimensions(mCellWidth, mCellHeight, mCountX, mCountY);
Winson Chung11a1a532013-09-13 11:14:45 -0700807 }
Winson Chung5f8afe62013-08-12 16:19:28 -0700808 }
Winson Chungaafa03c2010-06-11 17:34:16 -0700809
Winson Chung2d75f122013-09-23 16:53:31 -0700810 int newWidth = childWidthSize;
811 int newHeight = childHeightSize;
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700812 if (mFixedWidth > 0 && mFixedHeight > 0) {
813 newWidth = mFixedWidth;
814 newHeight = mFixedHeight;
815 } else if (widthSpecMode == MeasureSpec.UNSPECIFIED || heightSpecMode == MeasureSpec.UNSPECIFIED) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800816 throw new RuntimeException("CellLayout cannot have UNSPECIFIED dimensions");
817 }
818
Sunny Goyal4fe5a372015-05-14 19:55:10 -0700819 // Make the feedback view large enough to hold the blur bitmap.
820 mTouchFeedbackView.measure(
821 MeasureSpec.makeMeasureSpec(mCellWidth + mTouchFeedbackView.getExtraSize(),
822 MeasureSpec.EXACTLY),
823 MeasureSpec.makeMeasureSpec(mCellHeight + mTouchFeedbackView.getExtraSize(),
824 MeasureSpec.EXACTLY));
825
826 mShortcutsAndWidgets.measure(
827 MeasureSpec.makeMeasureSpec(newWidth, MeasureSpec.EXACTLY),
828 MeasureSpec.makeMeasureSpec(newHeight, MeasureSpec.EXACTLY));
829
830 int maxWidth = mShortcutsAndWidgets.getMeasuredWidth();
831 int maxHeight = mShortcutsAndWidgets.getMeasuredHeight();
Winson Chung2d75f122013-09-23 16:53:31 -0700832 if (mFixedWidth > 0 && mFixedHeight > 0) {
833 setMeasuredDimension(maxWidth, maxHeight);
834 } else {
835 setMeasuredDimension(widthSize, heightSize);
836 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800837 }
838
839 @Override
Michael Jurka28750fb2010-09-24 17:43:49 -0700840 protected void onLayout(boolean changed, int l, int t, int r, int b) {
Tony Wickham26b01422015-11-10 14:44:32 -0800841 boolean isFullscreen = mShortcutsAndWidgets.getChildCount() > 0 &&
842 ((LayoutParams) mShortcutsAndWidgets.getChildAt(0).getLayoutParams()).isFullscreen;
843 int left = getPaddingLeft();
844 if (!isFullscreen) {
Tony Wickhama501d492015-11-03 18:05:01 -0800845 left += (int) Math.ceil(getUnusedHorizontalSpace() / 2f);
Tony Wickham26b01422015-11-10 14:44:32 -0800846 }
Sunny Goyal7c786f72016-06-01 14:08:21 -0700847 int right = r - l - getPaddingRight();
848 if (!isFullscreen) {
849 right -= (int) Math.ceil(getUnusedHorizontalSpace() / 2f);
850 }
851
Winson Chung38848ca2013-10-08 12:03:44 -0700852 int top = getPaddingTop();
Sunny Goyal7c786f72016-06-01 14:08:21 -0700853 int bottom = b - t - getPaddingBottom();
Sunny Goyal4fe5a372015-05-14 19:55:10 -0700854
855 mTouchFeedbackView.layout(left, top,
856 left + mTouchFeedbackView.getMeasuredWidth(),
857 top + mTouchFeedbackView.getMeasuredHeight());
Sunny Goyal7c786f72016-06-01 14:08:21 -0700858 mShortcutsAndWidgets.layout(left, top, right, bottom);
859
860 // Expand the background drawing bounds by the padding baked into the background drawable
861 mBackground.getPadding(mTempRect);
862 mBackground.setBounds(
Jon Miranda28032002017-07-13 16:18:56 -0700863 left - mTempRect.left - getPaddingLeft(),
864 top - mTempRect.top - getPaddingTop(),
865 right + mTempRect.right + getPaddingRight(),
866 bottom + mTempRect.bottom + getPaddingBottom());
Sunny Goyal7c786f72016-06-01 14:08:21 -0700867 }
868
Tony Wickhama501d492015-11-03 18:05:01 -0800869 /**
870 * Returns the amount of space left over after subtracting padding and cells. This space will be
871 * very small, a few pixels at most, and is a result of rounding down when calculating the cell
872 * width in {@link DeviceProfile#calculateCellWidth(int, int)}.
873 */
874 public int getUnusedHorizontalSpace() {
875 return getMeasuredWidth() - getPaddingLeft() - getPaddingRight() - (mCountX * mCellWidth);
876 }
877
Michael Jurka5f1c5092010-09-03 14:15:02 -0700878 public float getBackgroundAlpha() {
879 return mBackgroundAlpha;
Michael Jurkadee05892010-07-27 10:01:56 -0700880 }
881
Michael Jurka5f1c5092010-09-03 14:15:02 -0700882 public void setBackgroundAlpha(float alpha) {
Michael Jurkaafaa0502011-12-13 18:22:50 -0800883 if (mBackgroundAlpha != alpha) {
884 mBackgroundAlpha = alpha;
Sunny Goyal2805e632015-05-20 15:35:32 -0700885 mBackground.setAlpha((int) (mBackgroundAlpha * 255));
Michael Jurkaafaa0502011-12-13 18:22:50 -0800886 }
Michael Jurkadee05892010-07-27 10:01:56 -0700887 }
888
Sunny Goyal2805e632015-05-20 15:35:32 -0700889 @Override
890 protected boolean verifyDrawable(Drawable who) {
891 return super.verifyDrawable(who) || (mIsDragTarget && who == mBackground);
892 }
893
Michael Jurkaa52570f2012-03-20 03:18:20 -0700894 public void setShortcutAndWidgetAlpha(float alpha) {
Sunny Goyal02b50812014-09-10 15:44:42 -0700895 mShortcutsAndWidgets.setAlpha(alpha);
Michael Jurkadee05892010-07-27 10:01:56 -0700896 }
897
Michael Jurkaa52570f2012-03-20 03:18:20 -0700898 public ShortcutAndWidgetContainer getShortcutsAndWidgets() {
Sunny Goyaldcbcc862014-08-12 15:58:36 -0700899 return mShortcutsAndWidgets;
Michael Jurkaa52570f2012-03-20 03:18:20 -0700900 }
901
Patrick Dubroy440c3602010-07-13 17:50:32 -0700902 public View getChildAt(int x, int y) {
Michael Jurkaa52570f2012-03-20 03:18:20 -0700903 return mShortcutsAndWidgets.getChildAt(x, y);
Patrick Dubroy440c3602010-07-13 17:50:32 -0700904 }
905
Adam Cohen76fc0852011-06-17 13:26:23 -0700906 public boolean animateChildToPosition(final View child, int cellX, int cellY, int duration,
Adam Cohen482ed822012-03-02 14:15:13 -0800907 int delay, boolean permanent, boolean adjustOccupied) {
Michael Jurkaa52570f2012-03-20 03:18:20 -0700908 ShortcutAndWidgetContainer clc = getShortcutsAndWidgets();
Adam Cohen482ed822012-03-02 14:15:13 -0800909
Adam Cohen19f37922012-03-21 11:59:11 -0700910 if (clc.indexOfChild(child) != -1) {
Adam Cohenbfbfd262011-06-13 16:55:12 -0700911 final LayoutParams lp = (LayoutParams) child.getLayoutParams();
912 final ItemInfo info = (ItemInfo) child.getTag();
913
914 // We cancel any existing animations
915 if (mReorderAnimators.containsKey(lp)) {
916 mReorderAnimators.get(lp).cancel();
917 mReorderAnimators.remove(lp);
918 }
919
Adam Cohen482ed822012-03-02 14:15:13 -0800920 final int oldX = lp.x;
921 final int oldY = lp.y;
922 if (adjustOccupied) {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -0700923 GridOccupancy occupied = permanent ? mOccupied : mTmpOccupied;
924 occupied.markCells(lp.cellX, lp.cellY, lp.cellHSpan, lp.cellVSpan, false);
925 occupied.markCells(cellX, cellY, lp.cellHSpan, lp.cellVSpan, true);
Adam Cohen482ed822012-03-02 14:15:13 -0800926 }
Adam Cohenbfbfd262011-06-13 16:55:12 -0700927 lp.isLockedToGrid = true;
Adam Cohen482ed822012-03-02 14:15:13 -0800928 if (permanent) {
929 lp.cellX = info.cellX = cellX;
930 lp.cellY = info.cellY = cellY;
931 } else {
932 lp.tmpCellX = cellX;
933 lp.tmpCellY = cellY;
934 }
Jon Mirandae96798e2016-12-07 12:10:44 -0800935 clc.setupLp(child);
Adam Cohenbfbfd262011-06-13 16:55:12 -0700936 lp.isLockedToGrid = false;
Adam Cohen482ed822012-03-02 14:15:13 -0800937 final int newX = lp.x;
938 final int newY = lp.y;
Adam Cohenbfbfd262011-06-13 16:55:12 -0700939
Adam Cohen76fc0852011-06-17 13:26:23 -0700940 lp.x = oldX;
941 lp.y = oldY;
Adam Cohen76fc0852011-06-17 13:26:23 -0700942
Adam Cohen482ed822012-03-02 14:15:13 -0800943 // Exit early if we're not actually moving the view
944 if (oldX == newX && oldY == newY) {
945 lp.isLockedToGrid = true;
946 return true;
947 }
948
Jon Mirandacda3bfb2017-02-06 15:54:41 -0800949 ValueAnimator va = LauncherAnimUtils.ofFloat(0f, 1f);
Adam Cohen482ed822012-03-02 14:15:13 -0800950 va.setDuration(duration);
951 mReorderAnimators.put(lp, va);
952
953 va.addUpdateListener(new AnimatorUpdateListener() {
954 @Override
Adam Cohenbfbfd262011-06-13 16:55:12 -0700955 public void onAnimationUpdate(ValueAnimator animation) {
Jon Mirandae96798e2016-12-07 12:10:44 -0800956 float r = (Float) animation.getAnimatedValue();
Adam Cohen19f37922012-03-21 11:59:11 -0700957 lp.x = (int) ((1 - r) * oldX + r * newX);
958 lp.y = (int) ((1 - r) * oldY + r * newY);
Adam Cohen6b8a02d2012-03-22 15:13:40 -0700959 child.requestLayout();
Adam Cohenbfbfd262011-06-13 16:55:12 -0700960 }
961 });
Adam Cohen482ed822012-03-02 14:15:13 -0800962 va.addListener(new AnimatorListenerAdapter() {
Adam Cohenbfbfd262011-06-13 16:55:12 -0700963 boolean cancelled = false;
964 public void onAnimationEnd(Animator animation) {
965 // If the animation was cancelled, it means that another animation
966 // has interrupted this one, and we don't want to lock the item into
967 // place just yet.
968 if (!cancelled) {
969 lp.isLockedToGrid = true;
Adam Cohen482ed822012-03-02 14:15:13 -0800970 child.requestLayout();
Adam Cohenbfbfd262011-06-13 16:55:12 -0700971 }
972 if (mReorderAnimators.containsKey(lp)) {
973 mReorderAnimators.remove(lp);
974 }
975 }
976 public void onAnimationCancel(Animator animation) {
977 cancelled = true;
978 }
979 });
Adam Cohen482ed822012-03-02 14:15:13 -0800980 va.setStartDelay(delay);
981 va.start();
Adam Cohenbfbfd262011-06-13 16:55:12 -0700982 return true;
983 }
984 return false;
985 }
986
Sunny Goyal06e21a22016-08-11 16:02:02 -0700987 void visualizeDropLocation(View v, DragPreviewProvider outlineProvider, int cellX, int cellY,
988 int spanX, int spanY, boolean resize, DropTarget.DragObject dragObject) {
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -0700989 final int oldDragCellX = mDragCell[0];
990 final int oldDragCellY = mDragCell[1];
Adam Cohen482ed822012-03-02 14:15:13 -0800991
Hyunyoung Song0de01172016-10-05 16:27:48 -0700992 if (outlineProvider == null || outlineProvider.generatedDragOutline == null) {
Adam Cohen2801caf2011-05-13 20:57:39 -0700993 return;
994 }
995
Hyunyoung Song0de01172016-10-05 16:27:48 -0700996 Bitmap dragOutline = outlineProvider.generatedDragOutline;
Adam Cohen482ed822012-03-02 14:15:13 -0800997 if (cellX != oldDragCellX || cellY != oldDragCellY) {
Sunny Goyale78e3d72015-09-24 11:23:31 -0700998 Point dragOffset = dragObject.dragView.getDragVisualizeOffset();
999 Rect dragRegion = dragObject.dragView.getDragRegion();
1000
Adam Cohen482ed822012-03-02 14:15:13 -08001001 mDragCell[0] = cellX;
1002 mDragCell[1] = cellY;
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001003
Joe Onorato4be866d2010-10-10 11:26:02 -07001004 final int oldIndex = mDragOutlineCurrent;
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -07001005 mDragOutlineAnims[oldIndex].animateOut();
1006 mDragOutlineCurrent = (oldIndex + 1) % mDragOutlines.length;
Adam Cohend41fbf52012-02-16 23:53:59 -08001007 Rect r = mDragOutlines[mDragOutlineCurrent];
Sunny Goyal106bf642015-07-16 12:18:06 -07001008
Adam Cohend41fbf52012-02-16 23:53:59 -08001009 if (resize) {
Adam Cohen482ed822012-03-02 14:15:13 -08001010 cellToRect(cellX, cellY, spanX, spanY, r);
Jon Mirandae96798e2016-12-07 12:10:44 -08001011 if (v instanceof LauncherAppWidgetHostView) {
1012 DeviceProfile profile = mLauncher.getDeviceProfile();
Jon Miranda6f6a06a2016-12-15 11:24:18 -08001013 Utilities.shrinkRect(r, profile.appWidgetScale.x, profile.appWidgetScale.y);
Jon Mirandae96798e2016-12-07 12:10:44 -08001014 }
Sunny Goyal106bf642015-07-16 12:18:06 -07001015 } else {
1016 // Find the top left corner of the rect the object will occupy
1017 final int[] topLeft = mTmpPoint;
1018 cellToPoint(cellX, cellY, topLeft);
1019
1020 int left = topLeft[0];
1021 int top = topLeft[1];
1022
1023 if (v != null && dragOffset == null) {
1024 // When drawing the drag outline, it did not account for margin offsets
1025 // added by the view's parent.
1026 MarginLayoutParams lp = (MarginLayoutParams) v.getLayoutParams();
1027 left += lp.leftMargin;
1028 top += lp.topMargin;
1029
1030 // Offsets due to the size difference between the View and the dragOutline.
1031 // There is a size difference to account for the outer blur, which may lie
1032 // outside the bounds of the view.
Jon Mirandaf7ff3fe2016-12-05 12:04:44 -08001033 top += ((mCellHeight * spanY) - dragOutline.getHeight()) / 2;
Sunny Goyal106bf642015-07-16 12:18:06 -07001034 // We center about the x axis
Sunny Goyalaa8a8712016-11-20 15:26:01 +05301035 left += ((mCellWidth * spanX) - dragOutline.getWidth()) / 2;
Sunny Goyal106bf642015-07-16 12:18:06 -07001036 } else {
1037 if (dragOffset != null && dragRegion != null) {
1038 // Center the drag region *horizontally* in the cell and apply a drag
1039 // outline offset
Sunny Goyalaa8a8712016-11-20 15:26:01 +05301040 left += dragOffset.x + ((mCellWidth * spanX) - dragRegion.width()) / 2;
Sunny Goyal106bf642015-07-16 12:18:06 -07001041 int cHeight = getShortcutsAndWidgets().getCellContentHeight();
1042 int cellPaddingY = (int) Math.max(0, ((mCellHeight - cHeight) / 2f));
1043 top += dragOffset.y + cellPaddingY;
1044 } else {
1045 // Center the drag outline in the cell
Sunny Goyalaa8a8712016-11-20 15:26:01 +05301046 left += ((mCellWidth * spanX) - dragOutline.getWidth()) / 2;
1047 top += ((mCellHeight * spanY) - dragOutline.getHeight()) / 2;
Sunny Goyal106bf642015-07-16 12:18:06 -07001048 }
1049 }
1050 r.set(left, top, left + dragOutline.getWidth(), top + dragOutline.getHeight());
Adam Cohend41fbf52012-02-16 23:53:59 -08001051 }
Winson Chung150fbab2010-09-29 17:14:26 -07001052
Jon Miranda6f6a06a2016-12-15 11:24:18 -08001053 Utilities.scaleRectAboutCenter(r, mChildScale);
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -07001054 mDragOutlineAnims[mDragOutlineCurrent].setTag(dragOutline);
1055 mDragOutlineAnims[mDragOutlineCurrent].animateIn();
Sunny Goyale78e3d72015-09-24 11:23:31 -07001056
1057 if (dragObject.stateAnnouncer != null) {
Sunny Goyalc13403c2016-11-18 23:44:48 -08001058 dragObject.stateAnnouncer.announce(getItemMoveDescription(cellX, cellY));
Sunny Goyale78e3d72015-09-24 11:23:31 -07001059 }
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001060 }
1061 }
1062
Sunny Goyalc13403c2016-11-18 23:44:48 -08001063 public String getItemMoveDescription(int cellX, int cellY) {
1064 if (mContainerType == HOTSEAT) {
1065 return getContext().getString(R.string.move_to_hotseat_position,
1066 Math.max(cellX, cellY) + 1);
1067 } else {
1068 return getContext().getString(R.string.move_to_empty_cell,
1069 cellY + 1, cellX + 1);
1070 }
1071 }
1072
Adam Cohene0310962011-04-18 16:15:31 -07001073 public void clearDragOutlines() {
1074 final int oldIndex = mDragOutlineCurrent;
1075 mDragOutlineAnims[oldIndex].animateOut();
Adam Cohend41fbf52012-02-16 23:53:59 -08001076 mDragCell[0] = mDragCell[1] = -1;
Adam Cohene0310962011-04-18 16:15:31 -07001077 }
1078
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001079 /**
Jeff Sharkey70864282009-04-07 21:08:40 -07001080 * Find a vacant area that will fit the given bounds nearest the requested
1081 * cell location. Uses Euclidean distance to score multiple vacant areas.
Winson Chungaafa03c2010-06-11 17:34:16 -07001082 *
Romain Guy51afc022009-05-04 18:03:43 -07001083 * @param pixelX The X location at which you want to search for a vacant area.
1084 * @param pixelY The Y location at which you want to search for a vacant area.
Adam Cohend41fbf52012-02-16 23:53:59 -08001085 * @param minSpanX The minimum horizontal span required
1086 * @param minSpanY The minimum vertical span required
1087 * @param spanX Horizontal span of the object.
1088 * @param spanY Vertical span of the object.
1089 * @param result Array in which to place the result, or null (in which case a new array will
1090 * be allocated)
1091 * @return The X, Y cell of a vacant area that can contain this object,
1092 * nearest the requested location.
1093 */
1094 int[] findNearestVacantArea(int pixelX, int pixelY, int minSpanX, int minSpanY, int spanX,
1095 int spanY, int[] result, int[] resultSpan) {
Sunny Goyalf7a29e82015-04-24 15:20:43 -07001096 return findNearestArea(pixelX, pixelY, minSpanX, minSpanY, spanX, spanY, true,
Adam Cohend41fbf52012-02-16 23:53:59 -08001097 result, resultSpan);
1098 }
1099
Rajeev Kumar9962dbe2017-06-12 12:16:20 -07001100 private final Stack<Rect> mTempRectStack = new Stack<>();
Adam Cohend41fbf52012-02-16 23:53:59 -08001101 private void lazyInitTempRectStack() {
1102 if (mTempRectStack.isEmpty()) {
1103 for (int i = 0; i < mCountX * mCountY; i++) {
1104 mTempRectStack.push(new Rect());
1105 }
1106 }
1107 }
Adam Cohen482ed822012-03-02 14:15:13 -08001108
Adam Cohend41fbf52012-02-16 23:53:59 -08001109 private void recycleTempRects(Stack<Rect> used) {
1110 while (!used.isEmpty()) {
1111 mTempRectStack.push(used.pop());
1112 }
1113 }
1114
1115 /**
1116 * Find a vacant area that will fit the given bounds nearest the requested
1117 * cell location. Uses Euclidean distance to score multiple vacant areas.
1118 *
1119 * @param pixelX The X location at which you want to search for a vacant area.
1120 * @param pixelY The Y location at which you want to search for a vacant area.
1121 * @param minSpanX The minimum horizontal span required
1122 * @param minSpanY The minimum vertical span required
1123 * @param spanX Horizontal span of the object.
1124 * @param spanY Vertical span of the object.
1125 * @param ignoreOccupied If true, the result can be an occupied cell
1126 * @param result Array in which to place the result, or null (in which case a new array will
1127 * be allocated)
1128 * @return The X, Y cell of a vacant area that can contain this object,
1129 * nearest the requested location.
1130 */
Sunny Goyalf7a29e82015-04-24 15:20:43 -07001131 private int[] findNearestArea(int pixelX, int pixelY, int minSpanX, int minSpanY, int spanX,
1132 int spanY, boolean ignoreOccupied, int[] result, int[] resultSpan) {
Adam Cohend41fbf52012-02-16 23:53:59 -08001133 lazyInitTempRectStack();
Michael Jurkac6ee42e2010-09-30 12:04:50 -07001134
Adam Cohene3e27a82011-04-15 12:07:39 -07001135 // For items with a spanX / spanY > 1, the passed in point (pixelX, pixelY) corresponds
1136 // to the center of the item, but we are searching based on the top-left cell, so
1137 // we translate the point over to correspond to the top-left.
Sunny Goyalaa8a8712016-11-20 15:26:01 +05301138 pixelX -= mCellWidth * (spanX - 1) / 2f;
1139 pixelY -= mCellHeight * (spanY - 1) / 2f;
Adam Cohene3e27a82011-04-15 12:07:39 -07001140
Jeff Sharkey70864282009-04-07 21:08:40 -07001141 // Keep track of best-scoring drop area
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001142 final int[] bestXY = result != null ? result : new int[2];
Jeff Sharkey70864282009-04-07 21:08:40 -07001143 double bestDistance = Double.MAX_VALUE;
Adam Cohend41fbf52012-02-16 23:53:59 -08001144 final Rect bestRect = new Rect(-1, -1, -1, -1);
Rajeev Kumar9962dbe2017-06-12 12:16:20 -07001145 final Stack<Rect> validRegions = new Stack<>();
Winson Chungaafa03c2010-06-11 17:34:16 -07001146
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001147 final int countX = mCountX;
1148 final int countY = mCountY;
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001149
Adam Cohend41fbf52012-02-16 23:53:59 -08001150 if (minSpanX <= 0 || minSpanY <= 0 || spanX <= 0 || spanY <= 0 ||
1151 spanX < minSpanX || spanY < minSpanY) {
1152 return bestXY;
1153 }
1154
1155 for (int y = 0; y < countY - (minSpanY - 1); y++) {
Michael Jurkac28de512010-08-13 11:27:44 -07001156 inner:
Adam Cohend41fbf52012-02-16 23:53:59 -08001157 for (int x = 0; x < countX - (minSpanX - 1); x++) {
1158 int ySize = -1;
1159 int xSize = -1;
Adam Cohendf035382011-04-11 17:22:04 -07001160 if (ignoreOccupied) {
Adam Cohend41fbf52012-02-16 23:53:59 -08001161 // First, let's see if this thing fits anywhere
1162 for (int i = 0; i < minSpanX; i++) {
1163 for (int j = 0; j < minSpanY; j++) {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001164 if (mOccupied.cells[x + i][y + j]) {
Adam Cohendf035382011-04-11 17:22:04 -07001165 continue inner;
1166 }
Michael Jurkac28de512010-08-13 11:27:44 -07001167 }
1168 }
Adam Cohend41fbf52012-02-16 23:53:59 -08001169 xSize = minSpanX;
1170 ySize = minSpanY;
1171
1172 // We know that the item will fit at _some_ acceptable size, now let's see
1173 // how big we can make it. We'll alternate between incrementing x and y spans
1174 // until we hit a limit.
1175 boolean incX = true;
1176 boolean hitMaxX = xSize >= spanX;
1177 boolean hitMaxY = ySize >= spanY;
1178 while (!(hitMaxX && hitMaxY)) {
1179 if (incX && !hitMaxX) {
1180 for (int j = 0; j < ySize; j++) {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001181 if (x + xSize > countX -1 || mOccupied.cells[x + xSize][y + j]) {
Adam Cohend41fbf52012-02-16 23:53:59 -08001182 // We can't move out horizontally
1183 hitMaxX = true;
1184 }
1185 }
1186 if (!hitMaxX) {
1187 xSize++;
1188 }
1189 } else if (!hitMaxY) {
1190 for (int i = 0; i < xSize; i++) {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001191 if (y + ySize > countY - 1 || mOccupied.cells[x + i][y + ySize]) {
Adam Cohend41fbf52012-02-16 23:53:59 -08001192 // We can't move out vertically
1193 hitMaxY = true;
1194 }
1195 }
1196 if (!hitMaxY) {
1197 ySize++;
1198 }
1199 }
1200 hitMaxX |= xSize >= spanX;
1201 hitMaxY |= ySize >= spanY;
1202 incX = !incX;
1203 }
1204 incX = true;
1205 hitMaxX = xSize >= spanX;
1206 hitMaxY = ySize >= spanY;
Michael Jurkac28de512010-08-13 11:27:44 -07001207 }
Sunny Goyal2805e632015-05-20 15:35:32 -07001208 final int[] cellXY = mTmpPoint;
Adam Cohene3e27a82011-04-15 12:07:39 -07001209 cellToCenterPoint(x, y, cellXY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001210
Adam Cohend41fbf52012-02-16 23:53:59 -08001211 // We verify that the current rect is not a sub-rect of any of our previous
1212 // candidates. In this case, the current rect is disqualified in favour of the
1213 // containing rect.
1214 Rect currentRect = mTempRectStack.pop();
1215 currentRect.set(x, y, x + xSize, y + ySize);
1216 boolean contained = false;
1217 for (Rect r : validRegions) {
1218 if (r.contains(currentRect)) {
1219 contained = true;
1220 break;
1221 }
1222 }
1223 validRegions.push(currentRect);
Sunny Goyalf7a29e82015-04-24 15:20:43 -07001224 double distance = Math.hypot(cellXY[0] - pixelX, cellXY[1] - pixelY);
Adam Cohen482ed822012-03-02 14:15:13 -08001225
Adam Cohend41fbf52012-02-16 23:53:59 -08001226 if ((distance <= bestDistance && !contained) ||
1227 currentRect.contains(bestRect)) {
Michael Jurkac28de512010-08-13 11:27:44 -07001228 bestDistance = distance;
1229 bestXY[0] = x;
1230 bestXY[1] = y;
Adam Cohend41fbf52012-02-16 23:53:59 -08001231 if (resultSpan != null) {
1232 resultSpan[0] = xSize;
1233 resultSpan[1] = ySize;
1234 }
1235 bestRect.set(currentRect);
Michael Jurkac28de512010-08-13 11:27:44 -07001236 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001237 }
1238 }
1239
Adam Cohenc0dcf592011-06-01 15:30:43 -07001240 // Return -1, -1 if no suitable location found
1241 if (bestDistance == Double.MAX_VALUE) {
1242 bestXY[0] = -1;
1243 bestXY[1] = -1;
Jeff Sharkey70864282009-04-07 21:08:40 -07001244 }
Adam Cohend41fbf52012-02-16 23:53:59 -08001245 recycleTempRects(validRegions);
Adam Cohenc0dcf592011-06-01 15:30:43 -07001246 return bestXY;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001247 }
Winson Chungaafa03c2010-06-11 17:34:16 -07001248
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001249 /**
Adam Cohen482ed822012-03-02 14:15:13 -08001250 * Find a vacant area that will fit the given bounds nearest the requested
1251 * cell location, and will also weigh in a suggested direction vector of the
1252 * desired location. This method computers distance based on unit grid distances,
1253 * not pixel distances.
1254 *
Adam Cohen47a876d2012-03-19 13:21:41 -07001255 * @param cellX The X cell nearest to which you want to search for a vacant area.
1256 * @param cellY The Y cell nearest which you want to search for a vacant area.
Adam Cohen482ed822012-03-02 14:15:13 -08001257 * @param spanX Horizontal span of the object.
1258 * @param spanY Vertical span of the object.
Adam Cohen47a876d2012-03-19 13:21:41 -07001259 * @param direction The favored direction in which the views should move from x, y
Sunny Goyal9eba1fd2015-10-16 08:58:57 -07001260 * @param occupied The array which represents which cells in the CellLayout are occupied
Adam Cohen47a876d2012-03-19 13:21:41 -07001261 * @param blockOccupied The array which represents which cells in the specified block (cellX,
Winson Chung5f8afe62013-08-12 16:19:28 -07001262 * cellY, spanX, spanY) are occupied. This is used when try to move a group of views.
Adam Cohen482ed822012-03-02 14:15:13 -08001263 * @param result Array in which to place the result, or null (in which case a new array will
1264 * be allocated)
1265 * @return The X, Y cell of a vacant area that can contain this object,
1266 * nearest the requested location.
1267 */
1268 private int[] findNearestArea(int cellX, int cellY, int spanX, int spanY, int[] direction,
Adam Cohen47a876d2012-03-19 13:21:41 -07001269 boolean[][] occupied, boolean blockOccupied[][], int[] result) {
Adam Cohen482ed822012-03-02 14:15:13 -08001270 // Keep track of best-scoring drop area
1271 final int[] bestXY = result != null ? result : new int[2];
1272 float bestDistance = Float.MAX_VALUE;
1273 int bestDirectionScore = Integer.MIN_VALUE;
1274
1275 final int countX = mCountX;
1276 final int countY = mCountY;
1277
1278 for (int y = 0; y < countY - (spanY - 1); y++) {
1279 inner:
1280 for (int x = 0; x < countX - (spanX - 1); x++) {
1281 // First, let's see if this thing fits anywhere
1282 for (int i = 0; i < spanX; i++) {
1283 for (int j = 0; j < spanY; j++) {
Adam Cohen47a876d2012-03-19 13:21:41 -07001284 if (occupied[x + i][y + j] && (blockOccupied == null || blockOccupied[i][j])) {
Adam Cohen482ed822012-03-02 14:15:13 -08001285 continue inner;
1286 }
1287 }
1288 }
1289
Sunny Goyalf7a29e82015-04-24 15:20:43 -07001290 float distance = (float) Math.hypot(x - cellX, y - cellY);
Adam Cohen482ed822012-03-02 14:15:13 -08001291 int[] curDirection = mTmpPoint;
Adam Cohen47a876d2012-03-19 13:21:41 -07001292 computeDirectionVector(x - cellX, y - cellY, curDirection);
1293 // The direction score is just the dot product of the two candidate direction
1294 // and that passed in.
Adam Cohen482ed822012-03-02 14:15:13 -08001295 int curDirectionScore = direction[0] * curDirection[0] +
1296 direction[1] * curDirection[1];
Sunny Goyal8f90dcf2016-08-18 15:01:11 -07001297 if (Float.compare(distance, bestDistance) < 0 ||
1298 (Float.compare(distance, bestDistance) == 0
1299 && curDirectionScore > bestDirectionScore)) {
Adam Cohen482ed822012-03-02 14:15:13 -08001300 bestDistance = distance;
1301 bestDirectionScore = curDirectionScore;
1302 bestXY[0] = x;
1303 bestXY[1] = y;
1304 }
1305 }
1306 }
1307
1308 // Return -1, -1 if no suitable location found
1309 if (bestDistance == Float.MAX_VALUE) {
1310 bestXY[0] = -1;
1311 bestXY[1] = -1;
1312 }
1313 return bestXY;
1314 }
1315
1316 private boolean addViewToTempLocation(View v, Rect rectOccupiedByPotentialDrop,
Adam Cohen8baab352012-03-20 17:39:21 -07001317 int[] direction, ItemConfiguration currentState) {
1318 CellAndSpan c = currentState.map.get(v);
Adam Cohen482ed822012-03-02 14:15:13 -08001319 boolean success = false;
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001320 mTmpOccupied.markCells(c, false);
1321 mTmpOccupied.markCells(rectOccupiedByPotentialDrop, true);
Adam Cohen482ed822012-03-02 14:15:13 -08001322
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001323 findNearestArea(c.cellX, c.cellY, c.spanX, c.spanY, direction,
1324 mTmpOccupied.cells, null, mTempLocation);
Adam Cohen482ed822012-03-02 14:15:13 -08001325
1326 if (mTempLocation[0] >= 0 && mTempLocation[1] >= 0) {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001327 c.cellX = mTempLocation[0];
1328 c.cellY = mTempLocation[1];
Adam Cohen482ed822012-03-02 14:15:13 -08001329 success = true;
Adam Cohen482ed822012-03-02 14:15:13 -08001330 }
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001331 mTmpOccupied.markCells(c, true);
Adam Cohen482ed822012-03-02 14:15:13 -08001332 return success;
1333 }
1334
Adam Cohenf3900c22012-11-16 18:28:11 -08001335 /**
1336 * This helper class defines a cluster of views. It helps with defining complex edges
1337 * of the cluster and determining how those edges interact with other views. The edges
1338 * essentially define a fine-grained boundary around the cluster of views -- like a more
1339 * precise version of a bounding box.
1340 */
1341 private class ViewCluster {
Sunny Goyal6dc98b92016-04-03 15:25:29 -07001342 final static int LEFT = 1 << 0;
1343 final static int TOP = 1 << 1;
1344 final static int RIGHT = 1 << 2;
1345 final static int BOTTOM = 1 << 3;
Adam Cohen47a876d2012-03-19 13:21:41 -07001346
Rajeev Kumar9962dbe2017-06-12 12:16:20 -07001347 final ArrayList<View> views;
1348 final ItemConfiguration config;
1349 final Rect boundingRect = new Rect();
Adam Cohen47a876d2012-03-19 13:21:41 -07001350
Rajeev Kumar9962dbe2017-06-12 12:16:20 -07001351 final int[] leftEdge = new int[mCountY];
1352 final int[] rightEdge = new int[mCountY];
1353 final int[] topEdge = new int[mCountX];
1354 final int[] bottomEdge = new int[mCountX];
Sunny Goyal6dc98b92016-04-03 15:25:29 -07001355 int dirtyEdges;
1356 boolean boundingRectDirty;
Adam Cohenf3900c22012-11-16 18:28:11 -08001357
1358 @SuppressWarnings("unchecked")
1359 public ViewCluster(ArrayList<View> views, ItemConfiguration config) {
1360 this.views = (ArrayList<View>) views.clone();
1361 this.config = config;
1362 resetEdges();
Adam Cohen47a876d2012-03-19 13:21:41 -07001363 }
1364
Adam Cohenf3900c22012-11-16 18:28:11 -08001365 void resetEdges() {
1366 for (int i = 0; i < mCountX; i++) {
1367 topEdge[i] = -1;
1368 bottomEdge[i] = -1;
1369 }
1370 for (int i = 0; i < mCountY; i++) {
1371 leftEdge[i] = -1;
1372 rightEdge[i] = -1;
1373 }
Sunny Goyal6dc98b92016-04-03 15:25:29 -07001374 dirtyEdges = LEFT | TOP | RIGHT | BOTTOM;
Adam Cohenf3900c22012-11-16 18:28:11 -08001375 boundingRectDirty = true;
1376 }
Adam Cohen47a876d2012-03-19 13:21:41 -07001377
Sunny Goyal6dc98b92016-04-03 15:25:29 -07001378 void computeEdge(int which) {
Adam Cohenf3900c22012-11-16 18:28:11 -08001379 int count = views.size();
1380 for (int i = 0; i < count; i++) {
1381 CellAndSpan cs = config.map.get(views.get(i));
1382 switch (which) {
1383 case LEFT:
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001384 int left = cs.cellX;
1385 for (int j = cs.cellY; j < cs.cellY + cs.spanY; j++) {
Sunny Goyal6dc98b92016-04-03 15:25:29 -07001386 if (left < leftEdge[j] || leftEdge[j] < 0) {
1387 leftEdge[j] = left;
Adam Cohena56dc102012-07-13 13:41:42 -07001388 }
Adam Cohen47a876d2012-03-19 13:21:41 -07001389 }
Adam Cohenf3900c22012-11-16 18:28:11 -08001390 break;
1391 case RIGHT:
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001392 int right = cs.cellX + cs.spanX;
1393 for (int j = cs.cellY; j < cs.cellY + cs.spanY; j++) {
Sunny Goyal6dc98b92016-04-03 15:25:29 -07001394 if (right > rightEdge[j]) {
1395 rightEdge[j] = right;
Adam Cohenf3900c22012-11-16 18:28:11 -08001396 }
1397 }
1398 break;
1399 case TOP:
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001400 int top = cs.cellY;
1401 for (int j = cs.cellX; j < cs.cellX + cs.spanX; j++) {
Sunny Goyal6dc98b92016-04-03 15:25:29 -07001402 if (top < topEdge[j] || topEdge[j] < 0) {
1403 topEdge[j] = top;
Adam Cohenf3900c22012-11-16 18:28:11 -08001404 }
1405 }
1406 break;
1407 case BOTTOM:
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001408 int bottom = cs.cellY + cs.spanY;
1409 for (int j = cs.cellX; j < cs.cellX + cs.spanX; j++) {
Sunny Goyal6dc98b92016-04-03 15:25:29 -07001410 if (bottom > bottomEdge[j]) {
1411 bottomEdge[j] = bottom;
Adam Cohenf3900c22012-11-16 18:28:11 -08001412 }
1413 }
1414 break;
Adam Cohen47a876d2012-03-19 13:21:41 -07001415 }
1416 }
1417 }
Adam Cohenf3900c22012-11-16 18:28:11 -08001418
1419 boolean isViewTouchingEdge(View v, int whichEdge) {
1420 CellAndSpan cs = config.map.get(v);
1421
Sunny Goyal6dc98b92016-04-03 15:25:29 -07001422 if ((dirtyEdges & whichEdge) == whichEdge) {
1423 computeEdge(whichEdge);
1424 dirtyEdges &= ~whichEdge;
1425 }
Adam Cohenf3900c22012-11-16 18:28:11 -08001426
1427 switch (whichEdge) {
1428 case LEFT:
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001429 for (int i = cs.cellY; i < cs.cellY + cs.spanY; i++) {
1430 if (leftEdge[i] == cs.cellX + cs.spanX) {
Adam Cohenf3900c22012-11-16 18:28:11 -08001431 return true;
1432 }
1433 }
1434 break;
1435 case RIGHT:
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001436 for (int i = cs.cellY; i < cs.cellY + cs.spanY; i++) {
1437 if (rightEdge[i] == cs.cellX) {
Adam Cohenf3900c22012-11-16 18:28:11 -08001438 return true;
1439 }
1440 }
1441 break;
1442 case TOP:
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001443 for (int i = cs.cellX; i < cs.cellX + cs.spanX; i++) {
1444 if (topEdge[i] == cs.cellY + cs.spanY) {
Adam Cohenf3900c22012-11-16 18:28:11 -08001445 return true;
1446 }
1447 }
1448 break;
1449 case BOTTOM:
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001450 for (int i = cs.cellX; i < cs.cellX + cs.spanX; i++) {
1451 if (bottomEdge[i] == cs.cellY) {
Adam Cohenf3900c22012-11-16 18:28:11 -08001452 return true;
1453 }
1454 }
1455 break;
1456 }
1457 return false;
1458 }
1459
1460 void shift(int whichEdge, int delta) {
1461 for (View v: views) {
1462 CellAndSpan c = config.map.get(v);
1463 switch (whichEdge) {
1464 case LEFT:
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001465 c.cellX -= delta;
Adam Cohenf3900c22012-11-16 18:28:11 -08001466 break;
1467 case RIGHT:
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001468 c.cellX += delta;
Adam Cohenf3900c22012-11-16 18:28:11 -08001469 break;
1470 case TOP:
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001471 c.cellY -= delta;
Adam Cohenf3900c22012-11-16 18:28:11 -08001472 break;
1473 case BOTTOM:
1474 default:
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001475 c.cellY += delta;
Adam Cohenf3900c22012-11-16 18:28:11 -08001476 break;
1477 }
1478 }
1479 resetEdges();
1480 }
1481
1482 public void addView(View v) {
1483 views.add(v);
1484 resetEdges();
1485 }
1486
1487 public Rect getBoundingRect() {
1488 if (boundingRectDirty) {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001489 config.getBoundingRectForViews(views, boundingRect);
Adam Cohenf3900c22012-11-16 18:28:11 -08001490 }
1491 return boundingRect;
1492 }
1493
Rajeev Kumar9962dbe2017-06-12 12:16:20 -07001494 final PositionComparator comparator = new PositionComparator();
Adam Cohenf3900c22012-11-16 18:28:11 -08001495 class PositionComparator implements Comparator<View> {
1496 int whichEdge = 0;
1497 public int compare(View left, View right) {
1498 CellAndSpan l = config.map.get(left);
1499 CellAndSpan r = config.map.get(right);
1500 switch (whichEdge) {
1501 case LEFT:
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001502 return (r.cellX + r.spanX) - (l.cellX + l.spanX);
Adam Cohenf3900c22012-11-16 18:28:11 -08001503 case RIGHT:
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001504 return l.cellX - r.cellX;
Adam Cohenf3900c22012-11-16 18:28:11 -08001505 case TOP:
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001506 return (r.cellY + r.spanY) - (l.cellY + l.spanY);
Adam Cohenf3900c22012-11-16 18:28:11 -08001507 case BOTTOM:
1508 default:
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001509 return l.cellY - r.cellY;
Adam Cohenf3900c22012-11-16 18:28:11 -08001510 }
1511 }
1512 }
1513
1514 public void sortConfigurationForEdgePush(int edge) {
1515 comparator.whichEdge = edge;
1516 Collections.sort(config.sortedViews, comparator);
1517 }
Adam Cohen47a876d2012-03-19 13:21:41 -07001518 }
1519
Adam Cohenf3900c22012-11-16 18:28:11 -08001520 private boolean pushViewsToTempLocation(ArrayList<View> views, Rect rectOccupiedByPotentialDrop,
1521 int[] direction, View dragView, ItemConfiguration currentState) {
Adam Cohene0489502012-08-27 15:18:53 -07001522
Adam Cohenf3900c22012-11-16 18:28:11 -08001523 ViewCluster cluster = new ViewCluster(views, currentState);
1524 Rect clusterRect = cluster.getBoundingRect();
1525 int whichEdge;
1526 int pushDistance;
1527 boolean fail = false;
1528
1529 // Determine the edge of the cluster that will be leading the push and how far
1530 // the cluster must be shifted.
1531 if (direction[0] < 0) {
1532 whichEdge = ViewCluster.LEFT;
1533 pushDistance = clusterRect.right - rectOccupiedByPotentialDrop.left;
Adam Cohene0489502012-08-27 15:18:53 -07001534 } else if (direction[0] > 0) {
Adam Cohenf3900c22012-11-16 18:28:11 -08001535 whichEdge = ViewCluster.RIGHT;
1536 pushDistance = rectOccupiedByPotentialDrop.right - clusterRect.left;
1537 } else if (direction[1] < 0) {
1538 whichEdge = ViewCluster.TOP;
1539 pushDistance = clusterRect.bottom - rectOccupiedByPotentialDrop.top;
1540 } else {
1541 whichEdge = ViewCluster.BOTTOM;
1542 pushDistance = rectOccupiedByPotentialDrop.bottom - clusterRect.top;
Adam Cohene0489502012-08-27 15:18:53 -07001543 }
1544
Adam Cohenf3900c22012-11-16 18:28:11 -08001545 // Break early for invalid push distance.
1546 if (pushDistance <= 0) {
1547 return false;
Adam Cohene0489502012-08-27 15:18:53 -07001548 }
Adam Cohenf3900c22012-11-16 18:28:11 -08001549
1550 // Mark the occupied state as false for the group of views we want to move.
1551 for (View v: views) {
1552 CellAndSpan c = currentState.map.get(v);
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001553 mTmpOccupied.markCells(c, false);
Adam Cohenf3900c22012-11-16 18:28:11 -08001554 }
1555
1556 // We save the current configuration -- if we fail to find a solution we will revert
1557 // to the initial state. The process of finding a solution modifies the configuration
1558 // in place, hence the need for revert in the failure case.
1559 currentState.save();
1560
1561 // The pushing algorithm is simplified by considering the views in the order in which
1562 // they would be pushed by the cluster. For example, if the cluster is leading with its
1563 // left edge, we consider sort the views by their right edge, from right to left.
1564 cluster.sortConfigurationForEdgePush(whichEdge);
1565
1566 while (pushDistance > 0 && !fail) {
1567 for (View v: currentState.sortedViews) {
1568 // For each view that isn't in the cluster, we see if the leading edge of the
1569 // cluster is contacting the edge of that view. If so, we add that view to the
1570 // cluster.
1571 if (!cluster.views.contains(v) && v != dragView) {
1572 if (cluster.isViewTouchingEdge(v, whichEdge)) {
1573 LayoutParams lp = (LayoutParams) v.getLayoutParams();
1574 if (!lp.canReorder) {
1575 // The push solution includes the all apps button, this is not viable.
1576 fail = true;
1577 break;
1578 }
1579 cluster.addView(v);
1580 CellAndSpan c = currentState.map.get(v);
1581
1582 // Adding view to cluster, mark it as not occupied.
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001583 mTmpOccupied.markCells(c, false);
Adam Cohenf3900c22012-11-16 18:28:11 -08001584 }
1585 }
1586 }
1587 pushDistance--;
1588
1589 // The cluster has been completed, now we move the whole thing over in the appropriate
1590 // direction.
1591 cluster.shift(whichEdge, 1);
1592 }
1593
1594 boolean foundSolution = false;
1595 clusterRect = cluster.getBoundingRect();
1596
1597 // Due to the nature of the algorithm, the only check required to verify a valid solution
1598 // is to ensure that completed shifted cluster lies completely within the cell layout.
1599 if (!fail && clusterRect.left >= 0 && clusterRect.right <= mCountX && clusterRect.top >= 0 &&
1600 clusterRect.bottom <= mCountY) {
1601 foundSolution = true;
1602 } else {
1603 currentState.restore();
1604 }
1605
1606 // In either case, we set the occupied array as marked for the location of the views
1607 for (View v: cluster.views) {
1608 CellAndSpan c = currentState.map.get(v);
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001609 mTmpOccupied.markCells(c, true);
Adam Cohenf3900c22012-11-16 18:28:11 -08001610 }
1611
1612 return foundSolution;
Adam Cohene0489502012-08-27 15:18:53 -07001613 }
1614
Adam Cohen482ed822012-03-02 14:15:13 -08001615 private boolean addViewsToTempLocation(ArrayList<View> views, Rect rectOccupiedByPotentialDrop,
Adam Cohenf3900c22012-11-16 18:28:11 -08001616 int[] direction, View dragView, ItemConfiguration currentState) {
Adam Cohen482ed822012-03-02 14:15:13 -08001617 if (views.size() == 0) return true;
Adam Cohen482ed822012-03-02 14:15:13 -08001618
Adam Cohen8baab352012-03-20 17:39:21 -07001619 boolean success = false;
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001620 Rect boundingRect = new Rect();
Adam Cohen8baab352012-03-20 17:39:21 -07001621 // We construct a rect which represents the entire group of views passed in
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001622 currentState.getBoundingRectForViews(views, boundingRect);
Adam Cohen8baab352012-03-20 17:39:21 -07001623
Adam Cohen8baab352012-03-20 17:39:21 -07001624 // Mark the occupied state as false for the group of views we want to move.
Adam Cohenf3900c22012-11-16 18:28:11 -08001625 for (View v: views) {
Adam Cohen8baab352012-03-20 17:39:21 -07001626 CellAndSpan c = currentState.map.get(v);
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001627 mTmpOccupied.markCells(c, false);
Adam Cohen8baab352012-03-20 17:39:21 -07001628 }
1629
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001630 GridOccupancy blockOccupied = new GridOccupancy(boundingRect.width(), boundingRect.height());
Adam Cohen47a876d2012-03-19 13:21:41 -07001631 int top = boundingRect.top;
1632 int left = boundingRect.left;
Adam Cohen8baab352012-03-20 17:39:21 -07001633 // We mark more precisely which parts of the bounding rect are truly occupied, allowing
Adam Cohena56dc102012-07-13 13:41:42 -07001634 // for interlocking.
Adam Cohenf3900c22012-11-16 18:28:11 -08001635 for (View v: views) {
Adam Cohen8baab352012-03-20 17:39:21 -07001636 CellAndSpan c = currentState.map.get(v);
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001637 blockOccupied.markCells(c.cellX - left, c.cellY - top, c.spanX, c.spanY, true);
Adam Cohen47a876d2012-03-19 13:21:41 -07001638 }
1639
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001640 mTmpOccupied.markCells(rectOccupiedByPotentialDrop, true);
Adam Cohen482ed822012-03-02 14:15:13 -08001641
Adam Cohenf3900c22012-11-16 18:28:11 -08001642 findNearestArea(boundingRect.left, boundingRect.top, boundingRect.width(),
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001643 boundingRect.height(), direction,
1644 mTmpOccupied.cells, blockOccupied.cells, mTempLocation);
Adam Cohen482ed822012-03-02 14:15:13 -08001645
Adam Cohen8baab352012-03-20 17:39:21 -07001646 // If we successfuly found a location by pushing the block of views, we commit it
Adam Cohen482ed822012-03-02 14:15:13 -08001647 if (mTempLocation[0] >= 0 && mTempLocation[1] >= 0) {
Adam Cohen8baab352012-03-20 17:39:21 -07001648 int deltaX = mTempLocation[0] - boundingRect.left;
1649 int deltaY = mTempLocation[1] - boundingRect.top;
Adam Cohenf3900c22012-11-16 18:28:11 -08001650 for (View v: views) {
Adam Cohen8baab352012-03-20 17:39:21 -07001651 CellAndSpan c = currentState.map.get(v);
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001652 c.cellX += deltaX;
1653 c.cellY += deltaY;
Adam Cohen482ed822012-03-02 14:15:13 -08001654 }
1655 success = true;
1656 }
Adam Cohen8baab352012-03-20 17:39:21 -07001657
1658 // In either case, we set the occupied array as marked for the location of the views
Adam Cohenf3900c22012-11-16 18:28:11 -08001659 for (View v: views) {
Adam Cohen8baab352012-03-20 17:39:21 -07001660 CellAndSpan c = currentState.map.get(v);
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001661 mTmpOccupied.markCells(c, true);
Adam Cohen482ed822012-03-02 14:15:13 -08001662 }
1663 return success;
1664 }
1665
Adam Cohen4abc5bd2012-05-29 21:06:03 -07001666 // This method tries to find a reordering solution which satisfies the push mechanic by trying
1667 // to push items in each of the cardinal directions, in an order based on the direction vector
1668 // passed.
1669 private boolean attemptPushInDirection(ArrayList<View> intersectingViews, Rect occupied,
1670 int[] direction, View ignoreView, ItemConfiguration solution) {
1671 if ((Math.abs(direction[0]) + Math.abs(direction[1])) > 1) {
Winson Chung5f8afe62013-08-12 16:19:28 -07001672 // If the direction vector has two non-zero components, we try pushing
Adam Cohen4abc5bd2012-05-29 21:06:03 -07001673 // separately in each of the components.
1674 int temp = direction[1];
1675 direction[1] = 0;
Adam Cohenf3900c22012-11-16 18:28:11 -08001676
1677 if (pushViewsToTempLocation(intersectingViews, occupied, direction,
Adam Cohen4abc5bd2012-05-29 21:06:03 -07001678 ignoreView, solution)) {
1679 return true;
1680 }
1681 direction[1] = temp;
1682 temp = direction[0];
1683 direction[0] = 0;
Adam Cohenf3900c22012-11-16 18:28:11 -08001684
1685 if (pushViewsToTempLocation(intersectingViews, occupied, direction,
Adam Cohen4abc5bd2012-05-29 21:06:03 -07001686 ignoreView, solution)) {
1687 return true;
1688 }
1689 // Revert the direction
1690 direction[0] = temp;
1691
1692 // Now we try pushing in each component of the opposite direction
1693 direction[0] *= -1;
1694 direction[1] *= -1;
1695 temp = direction[1];
1696 direction[1] = 0;
Adam Cohenf3900c22012-11-16 18:28:11 -08001697 if (pushViewsToTempLocation(intersectingViews, occupied, direction,
Adam Cohen4abc5bd2012-05-29 21:06:03 -07001698 ignoreView, solution)) {
1699 return true;
1700 }
1701
1702 direction[1] = temp;
1703 temp = direction[0];
1704 direction[0] = 0;
Adam Cohenf3900c22012-11-16 18:28:11 -08001705 if (pushViewsToTempLocation(intersectingViews, occupied, direction,
Adam Cohen4abc5bd2012-05-29 21:06:03 -07001706 ignoreView, solution)) {
1707 return true;
1708 }
1709 // revert the direction
1710 direction[0] = temp;
1711 direction[0] *= -1;
1712 direction[1] *= -1;
Winson Chung5f8afe62013-08-12 16:19:28 -07001713
Adam Cohen4abc5bd2012-05-29 21:06:03 -07001714 } else {
1715 // If the direction vector has a single non-zero component, we push first in the
1716 // direction of the vector
Adam Cohenf3900c22012-11-16 18:28:11 -08001717 if (pushViewsToTempLocation(intersectingViews, occupied, direction,
Adam Cohen4abc5bd2012-05-29 21:06:03 -07001718 ignoreView, solution)) {
1719 return true;
1720 }
Adam Cohen4abc5bd2012-05-29 21:06:03 -07001721 // Then we try the opposite direction
1722 direction[0] *= -1;
1723 direction[1] *= -1;
Adam Cohenf3900c22012-11-16 18:28:11 -08001724 if (pushViewsToTempLocation(intersectingViews, occupied, direction,
Adam Cohen4abc5bd2012-05-29 21:06:03 -07001725 ignoreView, solution)) {
1726 return true;
1727 }
1728 // Switch the direction back
1729 direction[0] *= -1;
1730 direction[1] *= -1;
Winson Chung5f8afe62013-08-12 16:19:28 -07001731
1732 // If we have failed to find a push solution with the above, then we try
Adam Cohen4abc5bd2012-05-29 21:06:03 -07001733 // to find a solution by pushing along the perpendicular axis.
1734
1735 // Swap the components
1736 int temp = direction[1];
1737 direction[1] = direction[0];
1738 direction[0] = temp;
Adam Cohenf3900c22012-11-16 18:28:11 -08001739 if (pushViewsToTempLocation(intersectingViews, occupied, direction,
Adam Cohen4abc5bd2012-05-29 21:06:03 -07001740 ignoreView, solution)) {
1741 return true;
1742 }
1743
1744 // Then we try the opposite direction
1745 direction[0] *= -1;
1746 direction[1] *= -1;
Adam Cohenf3900c22012-11-16 18:28:11 -08001747 if (pushViewsToTempLocation(intersectingViews, occupied, direction,
Adam Cohen4abc5bd2012-05-29 21:06:03 -07001748 ignoreView, solution)) {
1749 return true;
1750 }
1751 // Switch the direction back
1752 direction[0] *= -1;
1753 direction[1] *= -1;
1754
1755 // Swap the components back
1756 temp = direction[1];
1757 direction[1] = direction[0];
1758 direction[0] = temp;
1759 }
1760 return false;
1761 }
1762
Adam Cohen482ed822012-03-02 14:15:13 -08001763 private boolean rearrangementExists(int cellX, int cellY, int spanX, int spanY, int[] direction,
Adam Cohen8baab352012-03-20 17:39:21 -07001764 View ignoreView, ItemConfiguration solution) {
Winson Chunge3e03bc2012-05-01 15:10:11 -07001765 // Return early if get invalid cell positions
1766 if (cellX < 0 || cellY < 0) return false;
Adam Cohen482ed822012-03-02 14:15:13 -08001767
Adam Cohen8baab352012-03-20 17:39:21 -07001768 mIntersectingViews.clear();
Adam Cohen482ed822012-03-02 14:15:13 -08001769 mOccupiedRect.set(cellX, cellY, cellX + spanX, cellY + spanY);
Adam Cohen482ed822012-03-02 14:15:13 -08001770
Adam Cohen8baab352012-03-20 17:39:21 -07001771 // Mark the desired location of the view currently being dragged.
Adam Cohen482ed822012-03-02 14:15:13 -08001772 if (ignoreView != null) {
Adam Cohen8baab352012-03-20 17:39:21 -07001773 CellAndSpan c = solution.map.get(ignoreView);
Adam Cohen19f37922012-03-21 11:59:11 -07001774 if (c != null) {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001775 c.cellX = cellX;
1776 c.cellY = cellY;
Adam Cohen19f37922012-03-21 11:59:11 -07001777 }
Adam Cohen482ed822012-03-02 14:15:13 -08001778 }
Adam Cohen482ed822012-03-02 14:15:13 -08001779 Rect r0 = new Rect(cellX, cellY, cellX + spanX, cellY + spanY);
1780 Rect r1 = new Rect();
Adam Cohen8baab352012-03-20 17:39:21 -07001781 for (View child: solution.map.keySet()) {
Adam Cohen482ed822012-03-02 14:15:13 -08001782 if (child == ignoreView) continue;
Adam Cohen8baab352012-03-20 17:39:21 -07001783 CellAndSpan c = solution.map.get(child);
Adam Cohen482ed822012-03-02 14:15:13 -08001784 LayoutParams lp = (LayoutParams) child.getLayoutParams();
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001785 r1.set(c.cellX, c.cellY, c.cellX + c.spanX, c.cellY + c.spanY);
Adam Cohen482ed822012-03-02 14:15:13 -08001786 if (Rect.intersects(r0, r1)) {
1787 if (!lp.canReorder) {
1788 return false;
1789 }
1790 mIntersectingViews.add(child);
1791 }
1792 }
Adam Cohen47a876d2012-03-19 13:21:41 -07001793
Rajeev Kumar9962dbe2017-06-12 12:16:20 -07001794 solution.intersectingViews = new ArrayList<>(mIntersectingViews);
Adam Cohenfa3c58f2013-12-06 16:10:55 -08001795
Winson Chung5f8afe62013-08-12 16:19:28 -07001796 // First we try to find a solution which respects the push mechanic. That is,
Adam Cohen4abc5bd2012-05-29 21:06:03 -07001797 // we try to find a solution such that no displaced item travels through another item
1798 // without also displacing that item.
1799 if (attemptPushInDirection(mIntersectingViews, mOccupiedRect, direction, ignoreView,
Adam Cohen19f37922012-03-21 11:59:11 -07001800 solution)) {
Adam Cohen47a876d2012-03-19 13:21:41 -07001801 return true;
1802 }
Adam Cohen47a876d2012-03-19 13:21:41 -07001803
Adam Cohen4abc5bd2012-05-29 21:06:03 -07001804 // Next we try moving the views as a block, but without requiring the push mechanic.
Adam Cohenf3900c22012-11-16 18:28:11 -08001805 if (addViewsToTempLocation(mIntersectingViews, mOccupiedRect, direction, ignoreView,
Adam Cohen19f37922012-03-21 11:59:11 -07001806 solution)) {
Adam Cohen482ed822012-03-02 14:15:13 -08001807 return true;
1808 }
Adam Cohen47a876d2012-03-19 13:21:41 -07001809
Adam Cohen482ed822012-03-02 14:15:13 -08001810 // Ok, they couldn't move as a block, let's move them individually
1811 for (View v : mIntersectingViews) {
Adam Cohen8baab352012-03-20 17:39:21 -07001812 if (!addViewToTempLocation(v, mOccupiedRect, direction, solution)) {
Adam Cohen482ed822012-03-02 14:15:13 -08001813 return false;
1814 }
1815 }
1816 return true;
1817 }
1818
1819 /*
1820 * Returns a pair (x, y), where x,y are in {-1, 0, 1} corresponding to vector between
1821 * the provided point and the provided cell
1822 */
Adam Cohen47a876d2012-03-19 13:21:41 -07001823 private void computeDirectionVector(float deltaX, float deltaY, int[] result) {
Jon Mirandae96798e2016-12-07 12:10:44 -08001824 double angle = Math.atan(deltaY / deltaX);
Adam Cohen482ed822012-03-02 14:15:13 -08001825
1826 result[0] = 0;
1827 result[1] = 0;
1828 if (Math.abs(Math.cos(angle)) > 0.5f) {
1829 result[0] = (int) Math.signum(deltaX);
1830 }
1831 if (Math.abs(Math.sin(angle)) > 0.5f) {
1832 result[1] = (int) Math.signum(deltaY);
1833 }
1834 }
1835
Sunny Goyalf7a29e82015-04-24 15:20:43 -07001836 private ItemConfiguration findReorderSolution(int pixelX, int pixelY, int minSpanX, int minSpanY,
Adam Cohenfa3c58f2013-12-06 16:10:55 -08001837 int spanX, int spanY, int[] direction, View dragView, boolean decX,
1838 ItemConfiguration solution) {
Adam Cohen8baab352012-03-20 17:39:21 -07001839 // Copy the current state into the solution. This solution will be manipulated as necessary.
1840 copyCurrentStateToSolution(solution, false);
1841 // Copy the current occupied array into the temporary occupied array. This array will be
1842 // manipulated as necessary to find a solution.
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001843 mOccupied.copyTo(mTmpOccupied);
Adam Cohen482ed822012-03-02 14:15:13 -08001844
1845 // We find the nearest cell into which we would place the dragged item, assuming there's
1846 // nothing in its way.
1847 int result[] = new int[2];
1848 result = findNearestArea(pixelX, pixelY, spanX, spanY, result);
1849
Rajeev Kumar9962dbe2017-06-12 12:16:20 -07001850 boolean success;
Adam Cohen482ed822012-03-02 14:15:13 -08001851 // First we try the exact nearest position of the item being dragged,
1852 // we will then want to try to move this around to other neighbouring positions
Adam Cohen8baab352012-03-20 17:39:21 -07001853 success = rearrangementExists(result[0], result[1], spanX, spanY, direction, dragView,
1854 solution);
Adam Cohen482ed822012-03-02 14:15:13 -08001855
1856 if (!success) {
1857 // We try shrinking the widget down to size in an alternating pattern, shrink 1 in
1858 // x, then 1 in y etc.
1859 if (spanX > minSpanX && (minSpanY == spanY || decX)) {
Adam Cohenfa3c58f2013-12-06 16:10:55 -08001860 return findReorderSolution(pixelX, pixelY, minSpanX, minSpanY, spanX - 1, spanY,
1861 direction, dragView, false, solution);
Adam Cohen482ed822012-03-02 14:15:13 -08001862 } else if (spanY > minSpanY) {
Adam Cohenfa3c58f2013-12-06 16:10:55 -08001863 return findReorderSolution(pixelX, pixelY, minSpanX, minSpanY, spanX, spanY - 1,
1864 direction, dragView, true, solution);
Adam Cohen482ed822012-03-02 14:15:13 -08001865 }
1866 solution.isSolution = false;
1867 } else {
1868 solution.isSolution = true;
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001869 solution.cellX = result[0];
1870 solution.cellY = result[1];
1871 solution.spanX = spanX;
1872 solution.spanY = spanY;
Adam Cohen482ed822012-03-02 14:15:13 -08001873 }
1874 return solution;
1875 }
1876
1877 private void copyCurrentStateToSolution(ItemConfiguration solution, boolean temp) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07001878 int childCount = mShortcutsAndWidgets.getChildCount();
Adam Cohen482ed822012-03-02 14:15:13 -08001879 for (int i = 0; i < childCount; i++) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07001880 View child = mShortcutsAndWidgets.getChildAt(i);
Adam Cohen482ed822012-03-02 14:15:13 -08001881 LayoutParams lp = (LayoutParams) child.getLayoutParams();
Adam Cohen8baab352012-03-20 17:39:21 -07001882 CellAndSpan c;
Adam Cohen482ed822012-03-02 14:15:13 -08001883 if (temp) {
Adam Cohen8baab352012-03-20 17:39:21 -07001884 c = new CellAndSpan(lp.tmpCellX, lp.tmpCellY, lp.cellHSpan, lp.cellVSpan);
Adam Cohen482ed822012-03-02 14:15:13 -08001885 } else {
Adam Cohen8baab352012-03-20 17:39:21 -07001886 c = new CellAndSpan(lp.cellX, lp.cellY, lp.cellHSpan, lp.cellVSpan);
Adam Cohen482ed822012-03-02 14:15:13 -08001887 }
Adam Cohenf3900c22012-11-16 18:28:11 -08001888 solution.add(child, c);
Adam Cohen482ed822012-03-02 14:15:13 -08001889 }
1890 }
1891
1892 private void copySolutionToTempState(ItemConfiguration solution, View dragView) {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001893 mTmpOccupied.clear();
Adam Cohen482ed822012-03-02 14:15:13 -08001894
Michael Jurkaa52570f2012-03-20 03:18:20 -07001895 int childCount = mShortcutsAndWidgets.getChildCount();
Adam Cohen482ed822012-03-02 14:15:13 -08001896 for (int i = 0; i < childCount; i++) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07001897 View child = mShortcutsAndWidgets.getChildAt(i);
Adam Cohen482ed822012-03-02 14:15:13 -08001898 if (child == dragView) continue;
1899 LayoutParams lp = (LayoutParams) child.getLayoutParams();
Adam Cohen8baab352012-03-20 17:39:21 -07001900 CellAndSpan c = solution.map.get(child);
1901 if (c != null) {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001902 lp.tmpCellX = c.cellX;
1903 lp.tmpCellY = c.cellY;
Adam Cohen8baab352012-03-20 17:39:21 -07001904 lp.cellHSpan = c.spanX;
1905 lp.cellVSpan = c.spanY;
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001906 mTmpOccupied.markCells(c, true);
Adam Cohen482ed822012-03-02 14:15:13 -08001907 }
1908 }
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001909 mTmpOccupied.markCells(solution, true);
Adam Cohen482ed822012-03-02 14:15:13 -08001910 }
1911
1912 private void animateItemsToSolution(ItemConfiguration solution, View dragView, boolean
1913 commitDragView) {
1914
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001915 GridOccupancy occupied = DESTRUCTIVE_REORDER ? mOccupied : mTmpOccupied;
1916 occupied.clear();
Adam Cohen482ed822012-03-02 14:15:13 -08001917
Michael Jurkaa52570f2012-03-20 03:18:20 -07001918 int childCount = mShortcutsAndWidgets.getChildCount();
Adam Cohen482ed822012-03-02 14:15:13 -08001919 for (int i = 0; i < childCount; i++) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07001920 View child = mShortcutsAndWidgets.getChildAt(i);
Adam Cohen482ed822012-03-02 14:15:13 -08001921 if (child == dragView) continue;
Adam Cohen8baab352012-03-20 17:39:21 -07001922 CellAndSpan c = solution.map.get(child);
1923 if (c != null) {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001924 animateChildToPosition(child, c.cellX, c.cellY, REORDER_ANIMATION_DURATION, 0,
Adam Cohen19f37922012-03-21 11:59:11 -07001925 DESTRUCTIVE_REORDER, false);
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001926 occupied.markCells(c, true);
Adam Cohen482ed822012-03-02 14:15:13 -08001927 }
1928 }
1929 if (commitDragView) {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001930 occupied.markCells(solution, true);
Adam Cohen482ed822012-03-02 14:15:13 -08001931 }
1932 }
1933
Adam Cohenfa3c58f2013-12-06 16:10:55 -08001934
1935 // This method starts or changes the reorder preview animations
1936 private void beginOrAdjustReorderPreviewAnimations(ItemConfiguration solution,
1937 View dragView, int delay, int mode) {
Adam Cohen19f37922012-03-21 11:59:11 -07001938 int childCount = mShortcutsAndWidgets.getChildCount();
Adam Cohen19f37922012-03-21 11:59:11 -07001939 for (int i = 0; i < childCount; i++) {
1940 View child = mShortcutsAndWidgets.getChildAt(i);
1941 if (child == dragView) continue;
1942 CellAndSpan c = solution.map.get(child);
Adam Cohenfa3c58f2013-12-06 16:10:55 -08001943 boolean skip = mode == ReorderPreviewAnimation.MODE_HINT && solution.intersectingViews
1944 != null && !solution.intersectingViews.contains(child);
1945
Adam Cohen19f37922012-03-21 11:59:11 -07001946 LayoutParams lp = (LayoutParams) child.getLayoutParams();
Adam Cohenfa3c58f2013-12-06 16:10:55 -08001947 if (c != null && !skip) {
1948 ReorderPreviewAnimation rha = new ReorderPreviewAnimation(child, mode, lp.cellX,
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001949 lp.cellY, c.cellX, c.cellY, c.spanX, c.spanY);
Adam Cohend024f982012-05-23 18:26:45 -07001950 rha.animate();
Adam Cohen19f37922012-03-21 11:59:11 -07001951 }
1952 }
1953 }
1954
Adam Cohenfa3c58f2013-12-06 16:10:55 -08001955 // Class which represents the reorder preview animations. These animations show that an item is
Adam Cohen19f37922012-03-21 11:59:11 -07001956 // in a temporary state, and hint at where the item will return to.
Adam Cohenfa3c58f2013-12-06 16:10:55 -08001957 class ReorderPreviewAnimation {
Rajeev Kumar9962dbe2017-06-12 12:16:20 -07001958 final View child;
Adam Cohend024f982012-05-23 18:26:45 -07001959 float finalDeltaX;
1960 float finalDeltaY;
1961 float initDeltaX;
1962 float initDeltaY;
Rajeev Kumar9962dbe2017-06-12 12:16:20 -07001963 final float finalScale;
Adam Cohend024f982012-05-23 18:26:45 -07001964 float initScale;
Rajeev Kumar9962dbe2017-06-12 12:16:20 -07001965 final int mode;
Adam Cohenfa3c58f2013-12-06 16:10:55 -08001966 boolean repeating = false;
1967 private static final int PREVIEW_DURATION = 300;
1968 private static final int HINT_DURATION = Workspace.REORDER_TIMEOUT;
1969
Jon Miranda21266912016-12-19 14:12:05 -08001970 private static final float CHILD_DIVIDEND = 4.0f;
1971
Adam Cohenfa3c58f2013-12-06 16:10:55 -08001972 public static final int MODE_HINT = 0;
1973 public static final int MODE_PREVIEW = 1;
1974
Adam Cohene7587d22012-05-24 18:50:02 -07001975 Animator a;
Adam Cohen19f37922012-03-21 11:59:11 -07001976
Adam Cohenfa3c58f2013-12-06 16:10:55 -08001977 public ReorderPreviewAnimation(View child, int mode, int cellX0, int cellY0, int cellX1,
1978 int cellY1, int spanX, int spanY) {
Adam Cohen19f37922012-03-21 11:59:11 -07001979 regionToCenterPoint(cellX0, cellY0, spanX, spanY, mTmpPoint);
1980 final int x0 = mTmpPoint[0];
1981 final int y0 = mTmpPoint[1];
1982 regionToCenterPoint(cellX1, cellY1, spanX, spanY, mTmpPoint);
1983 final int x1 = mTmpPoint[0];
1984 final int y1 = mTmpPoint[1];
1985 final int dX = x1 - x0;
1986 final int dY = y1 - y0;
Jon Miranda21266912016-12-19 14:12:05 -08001987
1988 this.child = child;
1989 this.mode = mode;
1990 setInitialAnimationValues(false);
1991 finalScale = (mChildScale - (CHILD_DIVIDEND / child.getWidth())) * initScale;
1992 finalDeltaX = initDeltaX;
1993 finalDeltaY = initDeltaY;
Adam Cohenfa3c58f2013-12-06 16:10:55 -08001994 int dir = mode == MODE_HINT ? -1 : 1;
Adam Cohen19f37922012-03-21 11:59:11 -07001995 if (dX == dY && dX == 0) {
1996 } else {
1997 if (dY == 0) {
Jon Miranda21266912016-12-19 14:12:05 -08001998 finalDeltaX += - dir * Math.signum(dX) * mReorderPreviewAnimationMagnitude;
Adam Cohen19f37922012-03-21 11:59:11 -07001999 } else if (dX == 0) {
Jon Miranda21266912016-12-19 14:12:05 -08002000 finalDeltaY += - dir * Math.signum(dY) * mReorderPreviewAnimationMagnitude;
Adam Cohen19f37922012-03-21 11:59:11 -07002001 } else {
2002 double angle = Math.atan( (float) (dY) / dX);
Jon Miranda21266912016-12-19 14:12:05 -08002003 finalDeltaX += (int) (- dir * Math.signum(dX) *
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002004 Math.abs(Math.cos(angle) * mReorderPreviewAnimationMagnitude));
Jon Miranda21266912016-12-19 14:12:05 -08002005 finalDeltaY += (int) (- dir * Math.signum(dY) *
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002006 Math.abs(Math.sin(angle) * mReorderPreviewAnimationMagnitude));
Adam Cohen19f37922012-03-21 11:59:11 -07002007 }
2008 }
Jon Miranda21266912016-12-19 14:12:05 -08002009 }
2010
2011 void setInitialAnimationValues(boolean restoreOriginalValues) {
2012 if (restoreOriginalValues) {
2013 if (child instanceof LauncherAppWidgetHostView) {
2014 LauncherAppWidgetHostView lahv = (LauncherAppWidgetHostView) child;
2015 initScale = lahv.getScaleToFit();
2016 initDeltaX = lahv.getTranslationForCentering().x;
2017 initDeltaY = lahv.getTranslationForCentering().y;
2018 } else {
2019 initScale = mChildScale;
2020 initDeltaX = 0;
2021 initDeltaY = 0;
2022 }
2023 } else {
2024 initScale = child.getScaleX();
2025 initDeltaX = child.getTranslationX();
2026 initDeltaY = child.getTranslationY();
2027 }
Adam Cohen19f37922012-03-21 11:59:11 -07002028 }
2029
Adam Cohend024f982012-05-23 18:26:45 -07002030 void animate() {
Jon Miranda21266912016-12-19 14:12:05 -08002031 boolean noMovement = (finalDeltaX == initDeltaX) && (finalDeltaY == initDeltaY);
2032
Adam Cohen19f37922012-03-21 11:59:11 -07002033 if (mShakeAnimators.containsKey(child)) {
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002034 ReorderPreviewAnimation oldAnimation = mShakeAnimators.get(child);
Adam Cohend024f982012-05-23 18:26:45 -07002035 oldAnimation.cancel();
Adam Cohen19f37922012-03-21 11:59:11 -07002036 mShakeAnimators.remove(child);
Jon Miranda21266912016-12-19 14:12:05 -08002037 if (noMovement) {
Adam Cohene7587d22012-05-24 18:50:02 -07002038 completeAnimationImmediately();
2039 return;
2040 }
Adam Cohen19f37922012-03-21 11:59:11 -07002041 }
Jon Miranda21266912016-12-19 14:12:05 -08002042 if (noMovement) {
Adam Cohen19f37922012-03-21 11:59:11 -07002043 return;
2044 }
Jon Mirandacda3bfb2017-02-06 15:54:41 -08002045 ValueAnimator va = LauncherAnimUtils.ofFloat(0f, 1f);
Adam Cohene7587d22012-05-24 18:50:02 -07002046 a = va;
Tony Wickham9e0702f2015-09-02 14:45:39 -07002047
2048 // Animations are disabled in power save mode, causing the repeated animation to jump
2049 // spastically between beginning and end states. Since this looks bad, we don't repeat
2050 // the animation in power save mode.
Tony Wickham112ac952015-11-12 12:31:50 -08002051 if (!Utilities.isPowerSaverOn(getContext())) {
Tony Wickham9e0702f2015-09-02 14:45:39 -07002052 va.setRepeatMode(ValueAnimator.REVERSE);
2053 va.setRepeatCount(ValueAnimator.INFINITE);
2054 }
2055
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002056 va.setDuration(mode == MODE_HINT ? HINT_DURATION : PREVIEW_DURATION);
Adam Cohend024f982012-05-23 18:26:45 -07002057 va.setStartDelay((int) (Math.random() * 60));
Adam Cohen19f37922012-03-21 11:59:11 -07002058 va.addUpdateListener(new AnimatorUpdateListener() {
2059 @Override
2060 public void onAnimationUpdate(ValueAnimator animation) {
Jon Mirandae96798e2016-12-07 12:10:44 -08002061 float r = (Float) animation.getAnimatedValue();
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002062 float r1 = (mode == MODE_HINT && repeating) ? 1.0f : r;
2063 float x = r1 * finalDeltaX + (1 - r1) * initDeltaX;
2064 float y = r1 * finalDeltaY + (1 - r1) * initDeltaY;
Adam Cohen19f37922012-03-21 11:59:11 -07002065 child.setTranslationX(x);
2066 child.setTranslationY(y);
Adam Cohend024f982012-05-23 18:26:45 -07002067 float s = r * finalScale + (1 - r) * initScale;
Brandon Keely50e6e562012-05-08 16:28:49 -07002068 child.setScaleX(s);
2069 child.setScaleY(s);
Adam Cohen19f37922012-03-21 11:59:11 -07002070 }
2071 });
2072 va.addListener(new AnimatorListenerAdapter() {
2073 public void onAnimationRepeat(Animator animation) {
Adam Cohen19f37922012-03-21 11:59:11 -07002074 // We make sure to end only after a full period
Jon Miranda21266912016-12-19 14:12:05 -08002075 setInitialAnimationValues(true);
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002076 repeating = true;
Adam Cohen19f37922012-03-21 11:59:11 -07002077 }
2078 });
Adam Cohen19f37922012-03-21 11:59:11 -07002079 mShakeAnimators.put(child, this);
2080 va.start();
2081 }
2082
Adam Cohend024f982012-05-23 18:26:45 -07002083 private void cancel() {
Adam Cohene7587d22012-05-24 18:50:02 -07002084 if (a != null) {
2085 a.cancel();
2086 }
Adam Cohen19f37922012-03-21 11:59:11 -07002087 }
Adam Cohene7587d22012-05-24 18:50:02 -07002088
Adam Cohen091440a2015-03-18 14:16:05 -07002089 @Thunk void completeAnimationImmediately() {
Adam Cohene7587d22012-05-24 18:50:02 -07002090 if (a != null) {
2091 a.cancel();
2092 }
Brandon Keely50e6e562012-05-08 16:28:49 -07002093
Jon Miranda72dbd912017-01-04 14:03:07 -08002094 setInitialAnimationValues(true);
Sunny Goyal9e76f682017-02-13 12:13:43 -08002095 a = LauncherAnimUtils.ofPropertyValuesHolder(child,
2096 new PropertyListBuilder()
2097 .scale(initScale)
2098 .translationX(initDeltaX)
2099 .translationY(initDeltaY)
2100 .build())
2101 .setDuration(REORDER_ANIMATION_DURATION);
Sunny Goyal5d2fc322015-07-06 22:52:49 -07002102 a.setInterpolator(new android.view.animation.DecelerateInterpolator(1.5f));
2103 a.start();
Brandon Keely50e6e562012-05-08 16:28:49 -07002104 }
Adam Cohen19f37922012-03-21 11:59:11 -07002105 }
2106
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002107 private void completeAndClearReorderPreviewAnimations() {
2108 for (ReorderPreviewAnimation a: mShakeAnimators.values()) {
Brandon Keely50e6e562012-05-08 16:28:49 -07002109 a.completeAnimationImmediately();
Adam Cohen19f37922012-03-21 11:59:11 -07002110 }
2111 mShakeAnimators.clear();
2112 }
2113
Adam Cohen482ed822012-03-02 14:15:13 -08002114 private void commitTempPlacement() {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07002115 mTmpOccupied.copyTo(mOccupied);
Sunny Goyalaa8ef112015-06-12 20:04:41 -07002116
2117 long screenId = mLauncher.getWorkspace().getIdForScreen(this);
2118 int container = Favorites.CONTAINER_DESKTOP;
2119
Sunny Goyalc13403c2016-11-18 23:44:48 -08002120 if (mContainerType == HOTSEAT) {
Sunny Goyalaa8ef112015-06-12 20:04:41 -07002121 screenId = -1;
2122 container = Favorites.CONTAINER_HOTSEAT;
2123 }
2124
Michael Jurkaa52570f2012-03-20 03:18:20 -07002125 int childCount = mShortcutsAndWidgets.getChildCount();
Adam Cohen482ed822012-03-02 14:15:13 -08002126 for (int i = 0; i < childCount; i++) {
Adam Cohenea889a22012-03-27 16:45:39 -07002127 View child = mShortcutsAndWidgets.getChildAt(i);
2128 LayoutParams lp = (LayoutParams) child.getLayoutParams();
2129 ItemInfo info = (ItemInfo) child.getTag();
Adam Cohen2acce882012-03-28 19:03:19 -07002130 // We do a null check here because the item info can be null in the case of the
2131 // AllApps button in the hotseat.
2132 if (info != null) {
Sunny Goyalaa8ef112015-06-12 20:04:41 -07002133 final boolean requiresDbUpdate = (info.cellX != lp.tmpCellX
2134 || info.cellY != lp.tmpCellY || info.spanX != lp.cellHSpan
2135 || info.spanY != lp.cellVSpan);
2136
Adam Cohen2acce882012-03-28 19:03:19 -07002137 info.cellX = lp.cellX = lp.tmpCellX;
2138 info.cellY = lp.cellY = lp.tmpCellY;
Adam Cohenbebf0422012-04-11 18:06:28 -07002139 info.spanX = lp.cellHSpan;
2140 info.spanY = lp.cellVSpan;
Sunny Goyalaa8ef112015-06-12 20:04:41 -07002141
2142 if (requiresDbUpdate) {
Sunny Goyal43bf11d2017-02-02 13:52:53 -08002143 mLauncher.getModelWriter().modifyItemInDatabase(info, container, screenId,
Sunny Goyalaa8ef112015-06-12 20:04:41 -07002144 info.cellX, info.cellY, info.spanX, info.spanY);
2145 }
Adam Cohen2acce882012-03-28 19:03:19 -07002146 }
Adam Cohen482ed822012-03-02 14:15:13 -08002147 }
2148 }
2149
Sunny Goyalf7a29e82015-04-24 15:20:43 -07002150 private void setUseTempCoords(boolean useTempCoords) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07002151 int childCount = mShortcutsAndWidgets.getChildCount();
Adam Cohen482ed822012-03-02 14:15:13 -08002152 for (int i = 0; i < childCount; i++) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07002153 LayoutParams lp = (LayoutParams) mShortcutsAndWidgets.getChildAt(i).getLayoutParams();
Adam Cohen482ed822012-03-02 14:15:13 -08002154 lp.useTmpCoords = useTempCoords;
2155 }
2156 }
2157
Sunny Goyalf7a29e82015-04-24 15:20:43 -07002158 private ItemConfiguration findConfigurationNoShuffle(int pixelX, int pixelY, int minSpanX, int minSpanY,
Adam Cohen482ed822012-03-02 14:15:13 -08002159 int spanX, int spanY, View dragView, ItemConfiguration solution) {
2160 int[] result = new int[2];
2161 int[] resultSpan = new int[2];
Sunny Goyalf7a29e82015-04-24 15:20:43 -07002162 findNearestVacantArea(pixelX, pixelY, minSpanX, minSpanY, spanX, spanY, result,
Adam Cohen482ed822012-03-02 14:15:13 -08002163 resultSpan);
2164 if (result[0] >= 0 && result[1] >= 0) {
2165 copyCurrentStateToSolution(solution, false);
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07002166 solution.cellX = result[0];
2167 solution.cellY = result[1];
2168 solution.spanX = resultSpan[0];
2169 solution.spanY = resultSpan[1];
Adam Cohen482ed822012-03-02 14:15:13 -08002170 solution.isSolution = true;
2171 } else {
2172 solution.isSolution = false;
2173 }
2174 return solution;
2175 }
2176
Adam Cohen19f37922012-03-21 11:59:11 -07002177 /* This seems like it should be obvious and straight-forward, but when the direction vector
2178 needs to match with the notion of the dragView pushing other views, we have to employ
2179 a slightly more subtle notion of the direction vector. The question is what two points is
2180 the vector between? The center of the dragView and its desired destination? Not quite, as
2181 this doesn't necessarily coincide with the interaction of the dragView and items occupying
2182 those cells. Instead we use some heuristics to often lock the vector to up, down, left
2183 or right, which helps make pushing feel right.
2184 */
2185 private void getDirectionVectorForDrop(int dragViewCenterX, int dragViewCenterY, int spanX,
2186 int spanY, View dragView, int[] resultDirection) {
2187 int[] targetDestination = new int[2];
2188
2189 findNearestArea(dragViewCenterX, dragViewCenterY, spanX, spanY, targetDestination);
2190 Rect dragRect = new Rect();
2191 regionToRect(targetDestination[0], targetDestination[1], spanX, spanY, dragRect);
2192 dragRect.offset(dragViewCenterX - dragRect.centerX(), dragViewCenterY - dragRect.centerY());
2193
2194 Rect dropRegionRect = new Rect();
2195 getViewsIntersectingRegion(targetDestination[0], targetDestination[1], spanX, spanY,
2196 dragView, dropRegionRect, mIntersectingViews);
2197
2198 int dropRegionSpanX = dropRegionRect.width();
2199 int dropRegionSpanY = dropRegionRect.height();
2200
2201 regionToRect(dropRegionRect.left, dropRegionRect.top, dropRegionRect.width(),
2202 dropRegionRect.height(), dropRegionRect);
2203
2204 int deltaX = (dropRegionRect.centerX() - dragViewCenterX) / spanX;
2205 int deltaY = (dropRegionRect.centerY() - dragViewCenterY) / spanY;
2206
2207 if (dropRegionSpanX == mCountX || spanX == mCountX) {
2208 deltaX = 0;
2209 }
2210 if (dropRegionSpanY == mCountY || spanY == mCountY) {
2211 deltaY = 0;
2212 }
2213
2214 if (deltaX == 0 && deltaY == 0) {
2215 // No idea what to do, give a random direction.
2216 resultDirection[0] = 1;
2217 resultDirection[1] = 0;
2218 } else {
2219 computeDirectionVector(deltaX, deltaY, resultDirection);
2220 }
2221 }
2222
2223 // For a given cell and span, fetch the set of views intersecting the region.
2224 private void getViewsIntersectingRegion(int cellX, int cellY, int spanX, int spanY,
2225 View dragView, Rect boundingRect, ArrayList<View> intersectingViews) {
2226 if (boundingRect != null) {
2227 boundingRect.set(cellX, cellY, cellX + spanX, cellY + spanY);
2228 }
2229 intersectingViews.clear();
2230 Rect r0 = new Rect(cellX, cellY, cellX + spanX, cellY + spanY);
2231 Rect r1 = new Rect();
2232 final int count = mShortcutsAndWidgets.getChildCount();
2233 for (int i = 0; i < count; i++) {
2234 View child = mShortcutsAndWidgets.getChildAt(i);
2235 if (child == dragView) continue;
2236 LayoutParams lp = (LayoutParams) child.getLayoutParams();
2237 r1.set(lp.cellX, lp.cellY, lp.cellX + lp.cellHSpan, lp.cellY + lp.cellVSpan);
2238 if (Rect.intersects(r0, r1)) {
2239 mIntersectingViews.add(child);
2240 if (boundingRect != null) {
2241 boundingRect.union(r1);
2242 }
2243 }
2244 }
2245 }
2246
2247 boolean isNearestDropLocationOccupied(int pixelX, int pixelY, int spanX, int spanY,
2248 View dragView, int[] result) {
2249 result = findNearestArea(pixelX, pixelY, spanX, spanY, result);
2250 getViewsIntersectingRegion(result[0], result[1], spanX, spanY, dragView, null,
2251 mIntersectingViews);
2252 return !mIntersectingViews.isEmpty();
2253 }
2254
2255 void revertTempState() {
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002256 completeAndClearReorderPreviewAnimations();
2257 if (isItemPlacementDirty() && !DESTRUCTIVE_REORDER) {
2258 final int count = mShortcutsAndWidgets.getChildCount();
2259 for (int i = 0; i < count; i++) {
2260 View child = mShortcutsAndWidgets.getChildAt(i);
2261 LayoutParams lp = (LayoutParams) child.getLayoutParams();
2262 if (lp.tmpCellX != lp.cellX || lp.tmpCellY != lp.cellY) {
2263 lp.tmpCellX = lp.cellX;
2264 lp.tmpCellY = lp.cellY;
2265 animateChildToPosition(child, lp.cellX, lp.cellY, REORDER_ANIMATION_DURATION,
2266 0, false, false);
2267 }
Adam Cohen19f37922012-03-21 11:59:11 -07002268 }
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002269 setItemPlacementDirty(false);
Adam Cohen19f37922012-03-21 11:59:11 -07002270 }
Adam Cohen19f37922012-03-21 11:59:11 -07002271 }
2272
Adam Cohenbebf0422012-04-11 18:06:28 -07002273 boolean createAreaForResize(int cellX, int cellY, int spanX, int spanY,
2274 View dragView, int[] direction, boolean commit) {
2275 int[] pixelXY = new int[2];
2276 regionToCenterPoint(cellX, cellY, spanX, spanY, pixelXY);
2277
2278 // First we determine if things have moved enough to cause a different layout
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002279 ItemConfiguration swapSolution = findReorderSolution(pixelXY[0], pixelXY[1], spanX, spanY,
Adam Cohenbebf0422012-04-11 18:06:28 -07002280 spanX, spanY, direction, dragView, true, new ItemConfiguration());
2281
2282 setUseTempCoords(true);
2283 if (swapSolution != null && swapSolution.isSolution) {
2284 // If we're just testing for a possible location (MODE_ACCEPT_DROP), we don't bother
2285 // committing anything or animating anything as we just want to determine if a solution
2286 // exists
2287 copySolutionToTempState(swapSolution, dragView);
2288 setItemPlacementDirty(true);
2289 animateItemsToSolution(swapSolution, dragView, commit);
2290
2291 if (commit) {
2292 commitTempPlacement();
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002293 completeAndClearReorderPreviewAnimations();
Adam Cohenbebf0422012-04-11 18:06:28 -07002294 setItemPlacementDirty(false);
2295 } else {
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002296 beginOrAdjustReorderPreviewAnimations(swapSolution, dragView,
2297 REORDER_ANIMATION_DURATION, ReorderPreviewAnimation.MODE_PREVIEW);
Adam Cohenbebf0422012-04-11 18:06:28 -07002298 }
2299 mShortcutsAndWidgets.requestLayout();
2300 }
2301 return swapSolution.isSolution;
2302 }
2303
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002304 int[] performReorder(int pixelX, int pixelY, int minSpanX, int minSpanY, int spanX, int spanY,
Adam Cohen482ed822012-03-02 14:15:13 -08002305 View dragView, int[] result, int resultSpan[], int mode) {
Adam Cohen482ed822012-03-02 14:15:13 -08002306 // First we determine if things have moved enough to cause a different layout
Adam Cohen47a876d2012-03-19 13:21:41 -07002307 result = findNearestArea(pixelX, pixelY, spanX, spanY, result);
Adam Cohen482ed822012-03-02 14:15:13 -08002308
2309 if (resultSpan == null) {
2310 resultSpan = new int[2];
2311 }
2312
Adam Cohen19f37922012-03-21 11:59:11 -07002313 // When we are checking drop validity or actually dropping, we don't recompute the
2314 // direction vector, since we want the solution to match the preview, and it's possible
2315 // that the exact position of the item has changed to result in a new reordering outcome.
Adam Cohenb209e632012-03-27 17:09:36 -07002316 if ((mode == MODE_ON_DROP || mode == MODE_ON_DROP_EXTERNAL || mode == MODE_ACCEPT_DROP)
2317 && mPreviousReorderDirection[0] != INVALID_DIRECTION) {
Adam Cohen19f37922012-03-21 11:59:11 -07002318 mDirectionVector[0] = mPreviousReorderDirection[0];
2319 mDirectionVector[1] = mPreviousReorderDirection[1];
2320 // We reset this vector after drop
Adam Cohenb209e632012-03-27 17:09:36 -07002321 if (mode == MODE_ON_DROP || mode == MODE_ON_DROP_EXTERNAL) {
2322 mPreviousReorderDirection[0] = INVALID_DIRECTION;
2323 mPreviousReorderDirection[1] = INVALID_DIRECTION;
Adam Cohen19f37922012-03-21 11:59:11 -07002324 }
2325 } else {
2326 getDirectionVectorForDrop(pixelX, pixelY, spanX, spanY, dragView, mDirectionVector);
2327 mPreviousReorderDirection[0] = mDirectionVector[0];
2328 mPreviousReorderDirection[1] = mDirectionVector[1];
2329 }
2330
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002331 // Find a solution involving pushing / displacing any items in the way
2332 ItemConfiguration swapSolution = findReorderSolution(pixelX, pixelY, minSpanX, minSpanY,
Adam Cohen482ed822012-03-02 14:15:13 -08002333 spanX, spanY, mDirectionVector, dragView, true, new ItemConfiguration());
2334
2335 // We attempt the approach which doesn't shuffle views at all
2336 ItemConfiguration noShuffleSolution = findConfigurationNoShuffle(pixelX, pixelY, minSpanX,
2337 minSpanY, spanX, spanY, dragView, new ItemConfiguration());
2338
2339 ItemConfiguration finalSolution = null;
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002340
2341 // If the reorder solution requires resizing (shrinking) the item being dropped, we instead
2342 // favor a solution in which the item is not resized, but
Adam Cohen482ed822012-03-02 14:15:13 -08002343 if (swapSolution.isSolution && swapSolution.area() >= noShuffleSolution.area()) {
2344 finalSolution = swapSolution;
2345 } else if (noShuffleSolution.isSolution) {
2346 finalSolution = noShuffleSolution;
2347 }
2348
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002349 if (mode == MODE_SHOW_REORDER_HINT) {
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002350 if (finalSolution != null) {
Adam Cohenfe692872013-12-11 14:47:23 -08002351 beginOrAdjustReorderPreviewAnimations(finalSolution, dragView, 0,
2352 ReorderPreviewAnimation.MODE_HINT);
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07002353 result[0] = finalSolution.cellX;
2354 result[1] = finalSolution.cellY;
2355 resultSpan[0] = finalSolution.spanX;
2356 resultSpan[1] = finalSolution.spanY;
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002357 } else {
2358 result[0] = result[1] = resultSpan[0] = resultSpan[1] = -1;
2359 }
2360 return result;
2361 }
2362
Adam Cohen482ed822012-03-02 14:15:13 -08002363 boolean foundSolution = true;
2364 if (!DESTRUCTIVE_REORDER) {
2365 setUseTempCoords(true);
2366 }
2367
2368 if (finalSolution != null) {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07002369 result[0] = finalSolution.cellX;
2370 result[1] = finalSolution.cellY;
2371 resultSpan[0] = finalSolution.spanX;
2372 resultSpan[1] = finalSolution.spanY;
Adam Cohen482ed822012-03-02 14:15:13 -08002373
2374 // If we're just testing for a possible location (MODE_ACCEPT_DROP), we don't bother
2375 // committing anything or animating anything as we just want to determine if a solution
2376 // exists
2377 if (mode == MODE_DRAG_OVER || mode == MODE_ON_DROP || mode == MODE_ON_DROP_EXTERNAL) {
2378 if (!DESTRUCTIVE_REORDER) {
2379 copySolutionToTempState(finalSolution, dragView);
2380 }
2381 setItemPlacementDirty(true);
2382 animateItemsToSolution(finalSolution, dragView, mode == MODE_ON_DROP);
2383
Adam Cohen19f37922012-03-21 11:59:11 -07002384 if (!DESTRUCTIVE_REORDER &&
2385 (mode == MODE_ON_DROP || mode == MODE_ON_DROP_EXTERNAL)) {
Adam Cohen482ed822012-03-02 14:15:13 -08002386 commitTempPlacement();
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002387 completeAndClearReorderPreviewAnimations();
Adam Cohen19f37922012-03-21 11:59:11 -07002388 setItemPlacementDirty(false);
2389 } else {
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002390 beginOrAdjustReorderPreviewAnimations(finalSolution, dragView,
2391 REORDER_ANIMATION_DURATION, ReorderPreviewAnimation.MODE_PREVIEW);
Adam Cohen482ed822012-03-02 14:15:13 -08002392 }
2393 }
2394 } else {
2395 foundSolution = false;
2396 result[0] = result[1] = resultSpan[0] = resultSpan[1] = -1;
2397 }
2398
2399 if ((mode == MODE_ON_DROP || !foundSolution) && !DESTRUCTIVE_REORDER) {
2400 setUseTempCoords(false);
2401 }
Adam Cohen482ed822012-03-02 14:15:13 -08002402
Michael Jurkaa52570f2012-03-20 03:18:20 -07002403 mShortcutsAndWidgets.requestLayout();
Adam Cohen482ed822012-03-02 14:15:13 -08002404 return result;
2405 }
2406
Adam Cohen19f37922012-03-21 11:59:11 -07002407 void setItemPlacementDirty(boolean dirty) {
2408 mItemPlacementDirty = dirty;
Adam Cohen482ed822012-03-02 14:15:13 -08002409 }
Adam Cohen19f37922012-03-21 11:59:11 -07002410 boolean isItemPlacementDirty() {
2411 return mItemPlacementDirty;
Adam Cohen482ed822012-03-02 14:15:13 -08002412 }
2413
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07002414 private static class ItemConfiguration extends CellAndSpan {
Rajeev Kumar9962dbe2017-06-12 12:16:20 -07002415 final ArrayMap<View, CellAndSpan> map = new ArrayMap<>();
2416 private final ArrayMap<View, CellAndSpan> savedMap = new ArrayMap<>();
2417 final ArrayList<View> sortedViews = new ArrayList<>();
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002418 ArrayList<View> intersectingViews;
Adam Cohen482ed822012-03-02 14:15:13 -08002419 boolean isSolution = false;
Adam Cohen482ed822012-03-02 14:15:13 -08002420
Adam Cohenf3900c22012-11-16 18:28:11 -08002421 void save() {
2422 // Copy current state into savedMap
2423 for (View v: map.keySet()) {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07002424 savedMap.get(v).copyFrom(map.get(v));
Adam Cohenf3900c22012-11-16 18:28:11 -08002425 }
2426 }
2427
2428 void restore() {
2429 // Restore current state from savedMap
2430 for (View v: savedMap.keySet()) {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07002431 map.get(v).copyFrom(savedMap.get(v));
Adam Cohenf3900c22012-11-16 18:28:11 -08002432 }
2433 }
2434
2435 void add(View v, CellAndSpan cs) {
2436 map.put(v, cs);
2437 savedMap.put(v, new CellAndSpan());
2438 sortedViews.add(v);
2439 }
2440
Adam Cohen482ed822012-03-02 14:15:13 -08002441 int area() {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07002442 return spanX * spanY;
Adam Cohenf3900c22012-11-16 18:28:11 -08002443 }
2444
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07002445 void getBoundingRectForViews(ArrayList<View> views, Rect outRect) {
2446 boolean first = true;
2447 for (View v: views) {
2448 CellAndSpan c = map.get(v);
2449 if (first) {
2450 outRect.set(c.cellX, c.cellY, c.cellX + c.spanX, c.cellY + c.spanY);
2451 first = false;
2452 } else {
2453 outRect.union(c.cellX, c.cellY, c.cellX + c.spanX, c.cellY + c.spanY);
2454 }
2455 }
Adam Cohenf3900c22012-11-16 18:28:11 -08002456 }
Adam Cohen482ed822012-03-02 14:15:13 -08002457 }
2458
Adam Cohendf035382011-04-11 17:22:04 -07002459 /**
Adam Cohendf035382011-04-11 17:22:04 -07002460 * Find a starting cell position that will fit the given bounds nearest the requested
2461 * cell location. Uses Euclidean distance to score multiple vacant areas.
2462 *
2463 * @param pixelX The X location at which you want to search for a vacant area.
2464 * @param pixelY The Y location at which you want to search for a vacant area.
2465 * @param spanX Horizontal span of the object.
2466 * @param spanY Vertical span of the object.
Adam Cohendf035382011-04-11 17:22:04 -07002467 * @param result Previously returned value to possibly recycle.
2468 * @return The X, Y cell of a vacant area that can contain this object,
2469 * nearest the requested location.
2470 */
Adam Cohenf9c184a2016-01-15 16:47:43 -08002471 public int[] findNearestArea(int pixelX, int pixelY, int spanX, int spanY, int[] result) {
Sunny Goyalf7a29e82015-04-24 15:20:43 -07002472 return findNearestArea(pixelX, pixelY, spanX, spanY, spanX, spanY, false, result, null);
Adam Cohendf035382011-04-11 17:22:04 -07002473 }
2474
Michael Jurka0280c3b2010-09-17 15:00:07 -07002475 boolean existsEmptyCell() {
2476 return findCellForSpan(null, 1, 1);
2477 }
2478
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002479 /**
Michael Jurka0280c3b2010-09-17 15:00:07 -07002480 * Finds the upper-left coordinate of the first rectangle in the grid that can
2481 * hold a cell of the specified dimensions. If intersectX and intersectY are not -1,
2482 * then this method will only return coordinates for rectangles that contain the cell
2483 * (intersectX, intersectY)
2484 *
2485 * @param cellXY The array that will contain the position of a vacant cell if such a cell
2486 * can be found.
2487 * @param spanX The horizontal span of the cell we want to find.
2488 * @param spanY The vertical span of the cell we want to find.
2489 *
2490 * @return True if a vacant cell of the specified dimension was found, false otherwise.
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07002491 */
Hyunyoung Song3f471442015-04-08 19:01:34 -07002492 public boolean findCellForSpan(int[] cellXY, int spanX, int spanY) {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07002493 if (cellXY == null) {
2494 cellXY = new int[2];
Michael Jurka0280c3b2010-09-17 15:00:07 -07002495 }
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07002496 return mOccupied.findVacantCell(cellXY, spanX, spanY);
Michael Jurka0280c3b2010-09-17 15:00:07 -07002497 }
2498
2499 /**
Winson Chungc07918d2011-07-01 15:35:26 -07002500 * A drag event has begun over this layout.
2501 * It may have begun over this layout (in which case onDragChild is called first),
2502 * or it may have begun on another layout.
2503 */
2504 void onDragEnter() {
Winson Chungc07918d2011-07-01 15:35:26 -07002505 mDragging = true;
2506 }
2507
2508 /**
Michael Jurka0280c3b2010-09-17 15:00:07 -07002509 * Called when drag has left this CellLayout or has been completed (successfully or not)
2510 */
2511 void onDragExit() {
Joe Onorato4be866d2010-10-10 11:26:02 -07002512 // This can actually be called when we aren't in a drag, e.g. when adding a new
2513 // item to this layout via the customize drawer.
2514 // Guard against that case.
2515 if (mDragging) {
2516 mDragging = false;
Patrick Dubroyde7658b2010-09-27 11:15:43 -07002517 }
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -07002518
2519 // Invalidate the drag data
Adam Cohend41fbf52012-02-16 23:53:59 -08002520 mDragCell[0] = mDragCell[1] = -1;
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -07002521 mDragOutlineAnims[mDragOutlineCurrent].animateOut();
2522 mDragOutlineCurrent = (mDragOutlineCurrent + 1) % mDragOutlineAnims.length;
Adam Cohen19f37922012-03-21 11:59:11 -07002523 revertTempState();
Michael Jurka33945b22010-12-21 18:19:38 -08002524 setIsDragOverlapping(false);
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07002525 }
2526
2527 /**
Winson Chungaafa03c2010-06-11 17:34:16 -07002528 * Mark a child as having been dropped.
Patrick Dubroyde7658b2010-09-27 11:15:43 -07002529 * At the beginning of the drag operation, the child may have been on another
Patrick Dubroyce34a972010-10-19 10:34:32 -07002530 * screen, but it is re-parented before this method is called.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002531 *
2532 * @param child The child that is being dropped
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002533 */
Adam Cohen716b51e2011-06-30 12:09:54 -07002534 void onDropChild(View child) {
Romain Guyd94533d2009-08-17 10:01:15 -07002535 if (child != null) {
2536 LayoutParams lp = (LayoutParams) child.getLayoutParams();
Romain Guy84f296c2009-11-04 15:00:44 -08002537 lp.dropped = true;
Romain Guyd94533d2009-08-17 10:01:15 -07002538 child.requestLayout();
Tony Wickham1cdb6d02015-09-17 11:08:27 -07002539 markCellsAsOccupiedForView(child);
Romain Guyd94533d2009-08-17 10:01:15 -07002540 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002541 }
2542
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002543 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002544 * Computes a bounding rectangle for a range of cells
Winson Chungaafa03c2010-06-11 17:34:16 -07002545 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002546 * @param cellX X coordinate of upper left corner expressed as a cell position
2547 * @param cellY Y coordinate of upper left corner expressed as a cell position
Winson Chungaafa03c2010-06-11 17:34:16 -07002548 * @param cellHSpan Width in cells
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002549 * @param cellVSpan Height in cells
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07002550 * @param resultRect Rect into which to put the results
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002551 */
Adam Cohend41fbf52012-02-16 23:53:59 -08002552 public void cellToRect(int cellX, int cellY, int cellHSpan, int cellVSpan, Rect resultRect) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002553 final int cellWidth = mCellWidth;
2554 final int cellHeight = mCellHeight;
Winson Chungaafa03c2010-06-11 17:34:16 -07002555
Winson Chung4b825dcd2011-06-19 12:41:22 -07002556 final int hStartPadding = getPaddingLeft();
2557 final int vStartPadding = getPaddingTop();
Winson Chungaafa03c2010-06-11 17:34:16 -07002558
Sunny Goyalaa8a8712016-11-20 15:26:01 +05302559 int width = cellHSpan * cellWidth;
2560 int height = cellVSpan * cellHeight;
2561 int x = hStartPadding + cellX * cellWidth;
2562 int y = vStartPadding + cellY * cellHeight;
Winson Chungaafa03c2010-06-11 17:34:16 -07002563
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07002564 resultRect.set(x, y, x + width, y + height);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002565 }
Winson Chungaafa03c2010-06-11 17:34:16 -07002566
Adam Cohend4844c32011-02-18 19:25:06 -08002567 public void markCellsAsOccupiedForView(View view) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07002568 if (view == null || view.getParent() != mShortcutsAndWidgets) return;
Michael Jurka0280c3b2010-09-17 15:00:07 -07002569 LayoutParams lp = (LayoutParams) view.getLayoutParams();
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07002570 mOccupied.markCells(lp.cellX, lp.cellY, lp.cellHSpan, lp.cellVSpan, true);
Michael Jurka0280c3b2010-09-17 15:00:07 -07002571 }
2572
Adam Cohend4844c32011-02-18 19:25:06 -08002573 public void markCellsAsUnoccupiedForView(View view) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07002574 if (view == null || view.getParent() != mShortcutsAndWidgets) return;
Michael Jurka0280c3b2010-09-17 15:00:07 -07002575 LayoutParams lp = (LayoutParams) view.getLayoutParams();
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07002576 mOccupied.markCells(lp.cellX, lp.cellY, lp.cellHSpan, lp.cellVSpan, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002577 }
2578
Adam Cohen2801caf2011-05-13 20:57:39 -07002579 public int getDesiredWidth() {
Sunny Goyalaa8a8712016-11-20 15:26:01 +05302580 return getPaddingLeft() + getPaddingRight() + (mCountX * mCellWidth);
Adam Cohen2801caf2011-05-13 20:57:39 -07002581 }
2582
2583 public int getDesiredHeight() {
Sunny Goyalaa8a8712016-11-20 15:26:01 +05302584 return getPaddingTop() + getPaddingBottom() + (mCountY * mCellHeight);
Adam Cohen2801caf2011-05-13 20:57:39 -07002585 }
2586
Michael Jurka66d72172011-04-12 16:29:25 -07002587 public boolean isOccupied(int x, int y) {
2588 if (x < mCountX && y < mCountY) {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07002589 return mOccupied.cells[x][y];
Michael Jurka66d72172011-04-12 16:29:25 -07002590 } else {
2591 throw new RuntimeException("Position exceeds the bound of this CellLayout");
2592 }
2593 }
2594
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002595 @Override
2596 public ViewGroup.LayoutParams generateLayoutParams(AttributeSet attrs) {
2597 return new CellLayout.LayoutParams(getContext(), attrs);
2598 }
2599
2600 @Override
2601 protected boolean checkLayoutParams(ViewGroup.LayoutParams p) {
2602 return p instanceof CellLayout.LayoutParams;
2603 }
2604
2605 @Override
2606 protected ViewGroup.LayoutParams generateLayoutParams(ViewGroup.LayoutParams p) {
2607 return new CellLayout.LayoutParams(p);
2608 }
2609
2610 public static class LayoutParams extends ViewGroup.MarginLayoutParams {
2611 /**
2612 * Horizontal location of the item in the grid.
2613 */
2614 @ViewDebug.ExportedProperty
2615 public int cellX;
2616
2617 /**
2618 * Vertical location of the item in the grid.
2619 */
2620 @ViewDebug.ExportedProperty
2621 public int cellY;
2622
2623 /**
Adam Cohen482ed822012-03-02 14:15:13 -08002624 * Temporary horizontal location of the item in the grid during reorder
2625 */
2626 public int tmpCellX;
2627
2628 /**
2629 * Temporary vertical location of the item in the grid during reorder
2630 */
2631 public int tmpCellY;
2632
2633 /**
2634 * Indicates that the temporary coordinates should be used to layout the items
2635 */
2636 public boolean useTmpCoords;
2637
2638 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002639 * Number of cells spanned horizontally by the item.
2640 */
2641 @ViewDebug.ExportedProperty
2642 public int cellHSpan;
2643
2644 /**
2645 * Number of cells spanned vertically by the item.
2646 */
2647 @ViewDebug.ExportedProperty
2648 public int cellVSpan;
Winson Chungaafa03c2010-06-11 17:34:16 -07002649
Adam Cohen1b607ed2011-03-03 17:26:50 -08002650 /**
2651 * Indicates whether the item will set its x, y, width and height parameters freely,
2652 * or whether these will be computed based on cellX, cellY, cellHSpan and cellVSpan.
2653 */
Adam Cohend4844c32011-02-18 19:25:06 -08002654 public boolean isLockedToGrid = true;
2655
Adam Cohen482ed822012-03-02 14:15:13 -08002656 /**
Adam Cohenec40b2b2013-07-23 15:52:40 -07002657 * Indicates that this item should use the full extents of its parent.
2658 */
2659 public boolean isFullscreen = false;
2660
2661 /**
Adam Cohen482ed822012-03-02 14:15:13 -08002662 * Indicates whether this item can be reordered. Always true except in the case of the
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07002663 * the AllApps button and QSB place holder.
Adam Cohen482ed822012-03-02 14:15:13 -08002664 */
2665 public boolean canReorder = true;
2666
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002667 // X coordinate of the view in the layout.
2668 @ViewDebug.ExportedProperty
Vadim Tryshevfedca432015-08-19 17:55:02 -07002669 public int x;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002670 // Y coordinate of the view in the layout.
2671 @ViewDebug.ExportedProperty
Vadim Tryshevfedca432015-08-19 17:55:02 -07002672 public int y;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002673
Romain Guy84f296c2009-11-04 15:00:44 -08002674 boolean dropped;
Romain Guyfcb9e712009-10-02 16:06:52 -07002675
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002676 public LayoutParams(Context c, AttributeSet attrs) {
2677 super(c, attrs);
2678 cellHSpan = 1;
2679 cellVSpan = 1;
2680 }
2681
2682 public LayoutParams(ViewGroup.LayoutParams source) {
2683 super(source);
2684 cellHSpan = 1;
2685 cellVSpan = 1;
2686 }
Winson Chungaafa03c2010-06-11 17:34:16 -07002687
2688 public LayoutParams(LayoutParams source) {
2689 super(source);
2690 this.cellX = source.cellX;
2691 this.cellY = source.cellY;
2692 this.cellHSpan = source.cellHSpan;
2693 this.cellVSpan = source.cellVSpan;
2694 }
2695
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002696 public LayoutParams(int cellX, int cellY, int cellHSpan, int cellVSpan) {
Romain Guy8f19cdd2010-01-08 15:07:00 -08002697 super(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002698 this.cellX = cellX;
2699 this.cellY = cellY;
2700 this.cellHSpan = cellHSpan;
2701 this.cellVSpan = cellVSpan;
2702 }
2703
Sunny Goyalaa8a8712016-11-20 15:26:01 +05302704 public void setup(int cellWidth, int cellHeight, boolean invertHorizontally, int colCount) {
Jon Miranda7ae64ff2016-11-21 16:18:46 -08002705 setup(cellWidth, cellHeight, invertHorizontally, colCount, 1.0f, 1.0f);
2706 }
2707
2708 /**
2709 * Use this method, as opposed to {@link #setup(int, int, boolean, int)}, if the view needs
2710 * to be scaled.
2711 *
2712 * ie. In multi-window mode, we setup widgets so that they are measured and laid out
2713 * using their full/invariant device profile sizes.
2714 */
2715 public void setup(int cellWidth, int cellHeight, boolean invertHorizontally, int colCount,
2716 float cellScaleX, float cellScaleY) {
Adam Cohend4844c32011-02-18 19:25:06 -08002717 if (isLockedToGrid) {
2718 final int myCellHSpan = cellHSpan;
2719 final int myCellVSpan = cellVSpan;
Adam Cohen2374abf2013-04-16 14:56:57 -07002720 int myCellX = useTmpCoords ? tmpCellX : cellX;
2721 int myCellY = useTmpCoords ? tmpCellY : cellY;
2722
2723 if (invertHorizontally) {
2724 myCellX = colCount - myCellX - cellHSpan;
2725 }
Adam Cohen1b607ed2011-03-03 17:26:50 -08002726
Jon Miranda7ae64ff2016-11-21 16:18:46 -08002727 width = (int) (myCellHSpan * cellWidth / cellScaleX - leftMargin - rightMargin);
2728 height = (int) (myCellVSpan * cellHeight / cellScaleY - topMargin - bottomMargin);
Sunny Goyalaa8a8712016-11-20 15:26:01 +05302729 x = (myCellX * cellWidth + leftMargin);
2730 y = (myCellY * cellHeight + topMargin);
Adam Cohend4844c32011-02-18 19:25:06 -08002731 }
2732 }
Winson Chungaafa03c2010-06-11 17:34:16 -07002733
Winson Chungaafa03c2010-06-11 17:34:16 -07002734 public String toString() {
2735 return "(" + this.cellX + ", " + this.cellY + ")";
2736 }
Adam Cohen7f4eabe2011-04-21 16:19:16 -07002737
2738 public void setWidth(int width) {
2739 this.width = width;
2740 }
2741
2742 public int getWidth() {
2743 return width;
2744 }
2745
2746 public void setHeight(int height) {
2747 this.height = height;
2748 }
2749
2750 public int getHeight() {
2751 return height;
2752 }
2753
2754 public void setX(int x) {
2755 this.x = x;
2756 }
2757
2758 public int getX() {
2759 return x;
2760 }
2761
2762 public void setY(int y) {
2763 this.y = y;
2764 }
2765
2766 public int getY() {
2767 return y;
2768 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002769 }
2770
Michael Jurka0280c3b2010-09-17 15:00:07 -07002771 // This class stores info for two purposes:
2772 // 1. When dragging items (mDragInfo in Workspace), we store the View, its cellX & cellY,
2773 // its spanX, spanY, and the screen it is on
2774 // 2. When long clicking on an empty cell in a CellLayout, we save information about the
2775 // cellX and cellY coordinates and which page was clicked. We then set this as a tag on
2776 // the CellLayout that was long clicked
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07002777 public static final class CellInfo extends CellAndSpan {
Rajeev Kumar9962dbe2017-06-12 12:16:20 -07002778 public final View cell;
2779 final long screenId;
2780 final long container;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002781
Sunny Goyal83a8f042015-05-19 12:52:12 -07002782 public CellInfo(View v, ItemInfo info) {
Adam Cohene0aaa0d2014-05-12 12:44:22 -07002783 cellX = info.cellX;
2784 cellY = info.cellY;
2785 spanX = info.spanX;
2786 spanY = info.spanY;
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07002787 cell = v;
Adam Cohene0aaa0d2014-05-12 12:44:22 -07002788 screenId = info.screenId;
2789 container = info.container;
2790 }
2791
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002792 @Override
2793 public String toString() {
Winson Chungaafa03c2010-06-11 17:34:16 -07002794 return "Cell[view=" + (cell == null ? "null" : cell.getClass())
2795 + ", x=" + cellX + ", y=" + cellY + "]";
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002796 }
2797 }
Michael Jurkad771c962011-08-09 15:00:48 -07002798
Tony Wickham86930612015-09-09 13:50:40 -07002799 /**
2800 * Returns whether an item can be placed in this CellLayout (after rearranging and/or resizing
2801 * if necessary).
2802 */
2803 public boolean hasReorderSolution(ItemInfo itemInfo) {
2804 int[] cellPoint = new int[2];
2805 // Check for a solution starting at every cell.
2806 for (int cellX = 0; cellX < getCountX(); cellX++) {
2807 for (int cellY = 0; cellY < getCountY(); cellY++) {
2808 cellToPoint(cellX, cellY, cellPoint);
2809 if (findReorderSolution(cellPoint[0], cellPoint[1], itemInfo.minSpanX,
2810 itemInfo.minSpanY, itemInfo.spanX, itemInfo.spanY, mDirectionVector, null,
2811 true, new ItemConfiguration()).isSolution) {
2812 return true;
2813 }
2814 }
2815 }
2816 return false;
2817 }
2818
Sunny Goyal9ca9c132015-04-29 14:57:22 -07002819 public boolean isRegionVacant(int x, int y, int spanX, int spanY) {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07002820 return mOccupied.isRegionVacant(x, y, spanX, spanY);
Sunny Goyal9ca9c132015-04-29 14:57:22 -07002821 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002822}