blob: 2ab08b2eb4622ce4e84beca1b94048784477424d [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;
Jon Mirandaa0233f72017-06-22 18:34:45 -070055import com.android.launcher3.folder.PreviewBackground;
Sunny Goyal06e21a22016-08-11 16:02:02 -070056import com.android.launcher3.graphics.DragPreviewProvider;
Sunny Goyalff4ba2d2016-04-02 14:12:34 -070057import com.android.launcher3.util.CellAndSpan;
58import com.android.launcher3.util.GridOccupancy;
Sunny Goyale2fd14b2015-08-27 17:45:46 -070059import com.android.launcher3.util.ParcelableSparseArray;
Sunny Goyal9b29ca52017-02-17 10:39:44 -080060import com.android.launcher3.util.Themes;
Adam Cohen091440a2015-03-18 14:16:05 -070061import com.android.launcher3.util.Thunk;
Sunny Goyalc13403c2016-11-18 23:44:48 -080062import java.lang.annotation.Retention;
63import java.lang.annotation.RetentionPolicy;
Adam Cohen69ce2e52011-07-03 19:25:21 -070064import java.util.ArrayList;
Adam Cohenc0dcf592011-06-01 15:30:43 -070065import java.util.Arrays;
Adam Cohenf3900c22012-11-16 18:28:11 -080066import java.util.Collections;
67import java.util.Comparator;
Adam Cohend41fbf52012-02-16 23:53:59 -080068import java.util.Stack;
Adam Cohenc0dcf592011-06-01 15:30:43 -070069
Sunny Goyal4b6eb262015-05-14 19:24:40 -070070public class CellLayout extends ViewGroup implements BubbleTextShadowHandler {
Sunny Goyale9b651e2015-04-24 11:44:51 -070071 public static final int WORKSPACE_ACCESSIBILITY_DRAG = 2;
72 public static final int FOLDER_ACCESSIBILITY_DRAG = 1;
73
Tony Wickhama0628cc2015-10-14 15:23:04 -070074 private static final String TAG = "CellLayout";
75 private static final boolean LOGD = false;
Winson Chungaafa03c2010-06-11 17:34:16 -070076
Rajeev Kumar9962dbe2017-06-12 12:16:20 -070077 private final Launcher mLauncher;
Sunny Goyal4ffec482016-02-09 11:28:52 -080078 @ViewDebug.ExportedProperty(category = "launcher")
Adam Cohen091440a2015-03-18 14:16:05 -070079 @Thunk int mCellWidth;
Sunny Goyal4ffec482016-02-09 11:28:52 -080080 @ViewDebug.ExportedProperty(category = "launcher")
Adam Cohen091440a2015-03-18 14:16:05 -070081 @Thunk int mCellHeight;
Winson Chung11a1a532013-09-13 11:14:45 -070082 private int mFixedCellWidth;
83 private int mFixedCellHeight;
Winson Chungaafa03c2010-06-11 17:34:16 -070084
Sunny Goyal4ffec482016-02-09 11:28:52 -080085 @ViewDebug.ExportedProperty(category = "launcher")
Sunny Goyalff4ba2d2016-04-02 14:12:34 -070086 private int mCountX;
Sunny Goyal4ffec482016-02-09 11:28:52 -080087 @ViewDebug.ExportedProperty(category = "launcher")
Sunny Goyalff4ba2d2016-04-02 14:12:34 -070088 private int mCountY;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080089
Adam Cohen917e3882013-10-31 15:03:35 -070090 private boolean mDropPending = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080091
Patrick Dubroyde7658b2010-09-27 11:15:43 -070092 // These are temporary variables to prevent having to allocate a new object just to
93 // return an (x, y) value from helper functions. Do NOT use them to maintain other state.
Adam Cohen091440a2015-03-18 14:16:05 -070094 @Thunk final int[] mTmpPoint = new int[2];
Sunny Goyal2805e632015-05-20 15:35:32 -070095 @Thunk final int[] mTempLocation = new int[2];
Patrick Dubroy6569f2c2010-07-12 14:25:18 -070096
Sunny Goyalff4ba2d2016-04-02 14:12:34 -070097 private GridOccupancy mOccupied;
98 private GridOccupancy mTmpOccupied;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080099
Michael Jurkadee05892010-07-27 10:01:56 -0700100 private OnTouchListener mInterceptTouchListener;
Rajeev Kumar9962dbe2017-06-12 12:16:20 -0700101 private final StylusEventHelper mStylusEventHelper;
Michael Jurkadee05892010-07-27 10:01:56 -0700102
Jon Mirandaa0233f72017-06-22 18:34:45 -0700103 private final ArrayList<PreviewBackground> mFolderBackgrounds = new ArrayList<>();
104 final PreviewBackground mFolderLeaveBehind = new PreviewBackground();
Adam Cohen69ce2e52011-07-03 19:25:21 -0700105
Sunny Goyalf5440cb2016-12-14 15:13:00 -0800106 private static final int[] BACKGROUND_STATE_ACTIVE = new int[] { android.R.attr.state_active };
107 private static final int[] BACKGROUND_STATE_DEFAULT = new int[0];
108 private final Drawable mBackground;
Sunny Goyal2805e632015-05-20 15:35:32 -0700109
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700110 // These values allow a fixed measurement to be set on the CellLayout.
111 private int mFixedWidth = -1;
112 private int mFixedHeight = -1;
113
Michael Jurka33945b22010-12-21 18:19:38 -0800114 // If we're actively dragging something over this screen, mIsDragOverlapping is true
115 private boolean mIsDragOverlapping = false;
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700116
Winson Chung150fbab2010-09-29 17:14:26 -0700117 // These arrays are used to implement the drag visualization on x-large screens.
Joe Onorato4be866d2010-10-10 11:26:02 -0700118 // They are used as circular arrays, indexed by mDragOutlineCurrent.
Rajeev Kumar9962dbe2017-06-12 12:16:20 -0700119 @Thunk final Rect[] mDragOutlines = new Rect[4];
120 @Thunk final float[] mDragOutlineAlphas = new float[mDragOutlines.length];
121 private final InterruptibleInOutAnimator[] mDragOutlineAnims =
Joe Onorato4be866d2010-10-10 11:26:02 -0700122 new InterruptibleInOutAnimator[mDragOutlines.length];
Winson Chung150fbab2010-09-29 17:14:26 -0700123
124 // Used as an index into the above 3 arrays; indicates which is the most current value.
Joe Onorato4be866d2010-10-10 11:26:02 -0700125 private int mDragOutlineCurrent = 0;
Patrick Dubroy8e58e912010-10-14 13:21:48 -0700126 private final Paint mDragOutlinePaint = new Paint();
Winson Chung150fbab2010-09-29 17:14:26 -0700127
Sunny Goyal4fe5a372015-05-14 19:55:10 -0700128 private final ClickShadowView mTouchFeedbackView;
Patrick Dubroy96864c32011-03-10 17:17:23 -0800129
Rajeev Kumar9962dbe2017-06-12 12:16:20 -0700130 @Thunk final ArrayMap<LayoutParams, Animator> mReorderAnimators = new ArrayMap<>();
131 @Thunk final ArrayMap<View, ReorderPreviewAnimation> mShakeAnimators = new ArrayMap<>();
Adam Cohen19f37922012-03-21 11:59:11 -0700132
133 private boolean mItemPlacementDirty = false;
Adam Cohenbfbfd262011-06-13 16:55:12 -0700134
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700135 // When a drag operation is in progress, holds the nearest cell to the touch point
136 private final int[] mDragCell = new int[2];
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800137
Joe Onorato4be866d2010-10-10 11:26:02 -0700138 private boolean mDragging = false;
139
Rajeev Kumar9962dbe2017-06-12 12:16:20 -0700140 private final TimeInterpolator mEaseOutInterpolator;
141 private final ShortcutAndWidgetContainer mShortcutsAndWidgets;
Patrick Dubroyce34a972010-10-19 10:34:32 -0700142
Sunny Goyalc13403c2016-11-18 23:44:48 -0800143 @Retention(RetentionPolicy.SOURCE)
144 @IntDef({WORKSPACE, HOTSEAT, FOLDER})
145 public @interface ContainerType{}
146 public static final int WORKSPACE = 0;
147 public static final int HOTSEAT = 1;
148 public static final int FOLDER = 2;
149
150 @ContainerType private final int mContainerType;
151
Jon Mirandab28c4fc2017-06-20 10:58:36 -0700152 private final float mChildScale = 1f;
Andrew Flynn0dca1ec2012-02-29 13:33:22 -0800153
Adam Cohenfa3c58f2013-12-06 16:10:55 -0800154 public static final int MODE_SHOW_REORDER_HINT = 0;
155 public static final int MODE_DRAG_OVER = 1;
156 public static final int MODE_ON_DROP = 2;
157 public static final int MODE_ON_DROP_EXTERNAL = 3;
158 public static final int MODE_ACCEPT_DROP = 4;
Adam Cohen19f37922012-03-21 11:59:11 -0700159 private static final boolean DESTRUCTIVE_REORDER = false;
Adam Cohen482ed822012-03-02 14:15:13 -0800160 private static final boolean DEBUG_VISUALIZE_OCCUPIED = false;
161
Adam Cohenfa3c58f2013-12-06 16:10:55 -0800162 private static final float REORDER_PREVIEW_MAGNITUDE = 0.12f;
Adam Cohen19f37922012-03-21 11:59:11 -0700163 private static final int REORDER_ANIMATION_DURATION = 150;
Sunny Goyalc13403c2016-11-18 23:44:48 -0800164 @Thunk final float mReorderPreviewAnimationMagnitude;
Adam Cohen19f37922012-03-21 11:59:11 -0700165
Rajeev Kumar9962dbe2017-06-12 12:16:20 -0700166 private final ArrayList<View> mIntersectingViews = new ArrayList<>();
167 private final Rect mOccupiedRect = new Rect();
168 private final int[] mDirectionVector = new int[2];
169 final int[] mPreviousReorderDirection = new int[2];
Adam Cohenb209e632012-03-27 17:09:36 -0700170 private static final int INVALID_DIRECTION = -100;
Adam Cohen482ed822012-03-02 14:15:13 -0800171
Sunny Goyal2805e632015-05-20 15:35:32 -0700172 private final Rect mTempRect = new Rect();
Winson Chung3a6e7f32013-10-09 15:50:52 -0700173
Michael Jurkaca993832012-06-29 15:17:04 -0700174 private final static Paint sPaint = new Paint();
Romain Guy8a0bff52012-05-06 13:14:33 -0700175
Adam Cohenc9735cf2015-01-23 16:11:55 -0800176 // Related to accessible drag and drop
Sunny Goyale9b651e2015-04-24 11:44:51 -0700177 private DragAndDropAccessibilityDelegate mTouchHelper;
Adam Cohenc9735cf2015-01-23 16:11:55 -0800178 private boolean mUseTouchHelper = false;
Adam Cohenc9735cf2015-01-23 16:11:55 -0800179
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800180 public CellLayout(Context context) {
181 this(context, null);
182 }
183
184 public CellLayout(Context context, AttributeSet attrs) {
185 this(context, attrs, 0);
186 }
187
188 public CellLayout(Context context, AttributeSet attrs, int defStyle) {
189 super(context, attrs, defStyle);
Sunny Goyalc13403c2016-11-18 23:44:48 -0800190 TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.CellLayout, defStyle, 0);
191 mContainerType = a.getInteger(R.styleable.CellLayout_containerType, WORKSPACE);
192 a.recycle();
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700193
194 // A ViewGroup usually does not draw, but CellLayout needs to draw a rectangle to show
195 // the user where a dragged item will land when dropped.
196 setWillNotDraw(false);
Romain Guyce3cbd12013-02-25 15:00:36 -0800197 setClipToPadding(false);
Tony2fd02082016-10-07 12:50:01 -0700198 mLauncher = Launcher.getLauncher(context);
Michael Jurkaa63c4522010-08-19 13:52:27 -0700199
Adam Cohen2e6da152015-05-06 11:42:25 -0700200 DeviceProfile grid = mLauncher.getDeviceProfile();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800201
Winson Chung11a1a532013-09-13 11:14:45 -0700202 mCellWidth = mCellHeight = -1;
Nilesh Agrawal5f7099a2014-01-02 15:54:57 -0800203 mFixedCellWidth = mFixedCellHeight = -1;
Sunny Goyalff4ba2d2016-04-02 14:12:34 -0700204
205 mCountX = grid.inv.numColumns;
206 mCountY = grid.inv.numRows;
207 mOccupied = new GridOccupancy(mCountX, mCountY);
208 mTmpOccupied = new GridOccupancy(mCountX, mCountY);
209
Adam Cohen5b53f292012-03-29 14:30:35 -0700210 mPreviousReorderDirection[0] = INVALID_DIRECTION;
211 mPreviousReorderDirection[1] = INVALID_DIRECTION;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800212
Adam Cohenefca0272016-02-24 19:19:06 -0800213 mFolderLeaveBehind.delegateCellX = -1;
214 mFolderLeaveBehind.delegateCellY = -1;
215
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800216 setAlwaysDrawnWithCacheEnabled(false);
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700217 final Resources res = getResources();
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700218
Sunny Goyalf5440cb2016-12-14 15:13:00 -0800219 mBackground = res.getDrawable(R.drawable.bg_celllayout);
Sunny Goyal2805e632015-05-20 15:35:32 -0700220 mBackground.setCallback(this);
Sunny Goyalaeb16432017-10-16 11:46:41 -0700221 mBackground.setAlpha(0);
Michael Jurka33945b22010-12-21 18:19:38 -0800222
Sunny Goyalc13403c2016-11-18 23:44:48 -0800223 mReorderPreviewAnimationMagnitude = (REORDER_PREVIEW_MAGNITUDE * grid.iconSizePx);
Adam Cohen19f37922012-03-21 11:59:11 -0700224
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700225 // Initialize the data structures used for the drag visualization.
Patrick Dubroyce34a972010-10-19 10:34:32 -0700226 mEaseOutInterpolator = new DecelerateInterpolator(2.5f); // Quint ease out
Winson Chungb8c69f32011-10-19 21:36:08 -0700227 mDragCell[0] = mDragCell[1] = -1;
Joe Onorato4be866d2010-10-10 11:26:02 -0700228 for (int i = 0; i < mDragOutlines.length; i++) {
Adam Cohend41fbf52012-02-16 23:53:59 -0800229 mDragOutlines[i] = new Rect(-1, -1, -1, -1);
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700230 }
Mario Bertschler54ba6012017-06-08 10:53:53 -0700231 mDragOutlinePaint.setColor(Themes.getAttrColor(context, R.attr.workspaceTextColor));
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700232
233 // When dragging things around the home screens, we show a green outline of
234 // where the item will land. The outlines gradually fade out, leaving a trail
235 // behind the drag path.
236 // Set up all the animations that are used to implement this fading.
237 final int duration = res.getInteger(R.integer.config_dragOutlineFadeTime);
Chet Haase472b2812010-10-14 07:02:04 -0700238 final float fromAlphaValue = 0;
239 final float toAlphaValue = (float)res.getInteger(R.integer.config_dragOutlineMaxAlpha);
Joe Onorato4be866d2010-10-10 11:26:02 -0700240
Patrick Dubroy8e58e912010-10-14 13:21:48 -0700241 Arrays.fill(mDragOutlineAlphas, fromAlphaValue);
Joe Onorato4be866d2010-10-10 11:26:02 -0700242
243 for (int i = 0; i < mDragOutlineAnims.length; i++) {
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700244 final InterruptibleInOutAnimator anim =
Michael Jurkaf1ad6082013-03-13 12:55:46 +0100245 new InterruptibleInOutAnimator(this, duration, fromAlphaValue, toAlphaValue);
Patrick Dubroyce34a972010-10-19 10:34:32 -0700246 anim.getAnimator().setInterpolator(mEaseOutInterpolator);
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700247 final int thisIndex = i;
Chet Haase472b2812010-10-14 07:02:04 -0700248 anim.getAnimator().addUpdateListener(new AnimatorUpdateListener() {
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700249 public void onAnimationUpdate(ValueAnimator animation) {
Joe Onorato4be866d2010-10-10 11:26:02 -0700250 final Bitmap outline = (Bitmap)anim.getTag();
251
252 // If an animation is started and then stopped very quickly, we can still
253 // get spurious updates we've cleared the tag. Guard against this.
254 if (outline == null) {
Tony Wickhama0628cc2015-10-14 15:23:04 -0700255 if (LOGD) {
Patrick Dubroyfe6bd872010-10-13 17:32:10 -0700256 Object val = animation.getAnimatedValue();
257 Log.d(TAG, "anim " + thisIndex + " update: " + val +
258 ", isStopped " + anim.isStopped());
259 }
Joe Onorato4be866d2010-10-10 11:26:02 -0700260 // Try to prevent it from continuing to run
261 animation.cancel();
262 } else {
Chet Haase472b2812010-10-14 07:02:04 -0700263 mDragOutlineAlphas[thisIndex] = (Float) animation.getAnimatedValue();
Adam Cohend41fbf52012-02-16 23:53:59 -0800264 CellLayout.this.invalidate(mDragOutlines[thisIndex]);
Joe Onorato4be866d2010-10-10 11:26:02 -0700265 }
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700266 }
267 });
Joe Onorato4be866d2010-10-10 11:26:02 -0700268 // The animation holds a reference to the drag outline bitmap as long is it's
269 // running. This way the bitmap can be GCed when the animations are complete.
Chet Haase472b2812010-10-14 07:02:04 -0700270 anim.getAnimator().addListener(new AnimatorListenerAdapter() {
Michael Jurka3c4c20f2010-10-28 15:36:06 -0700271 @Override
Joe Onorato4be866d2010-10-10 11:26:02 -0700272 public void onAnimationEnd(Animator animation) {
Chet Haase472b2812010-10-14 07:02:04 -0700273 if ((Float) ((ValueAnimator) animation).getAnimatedValue() == 0f) {
Joe Onorato4be866d2010-10-10 11:26:02 -0700274 anim.setTag(null);
275 }
276 }
277 });
278 mDragOutlineAnims[i] = anim;
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700279 }
Patrick Dubroyce34a972010-10-19 10:34:32 -0700280
Sunny Goyalc13403c2016-11-18 23:44:48 -0800281 mShortcutsAndWidgets = new ShortcutAndWidgetContainer(context, mContainerType);
Sunny Goyalaa8a8712016-11-20 15:26:01 +0530282 mShortcutsAndWidgets.setCellDimensions(mCellWidth, mCellHeight, mCountX, mCountY);
Adam Cohen2374abf2013-04-16 14:56:57 -0700283
Mady Mellorbb835202015-07-15 16:34:34 -0700284 mStylusEventHelper = new StylusEventHelper(new SimpleOnStylusPressListener(this), this);
Mady Melloref044dd2015-06-02 15:35:07 -0700285
Sunny Goyal4fe5a372015-05-14 19:55:10 -0700286 mTouchFeedbackView = new ClickShadowView(context);
287 addView(mTouchFeedbackView);
Michael Jurkaa52570f2012-03-20 03:18:20 -0700288 addView(mShortcutsAndWidgets);
Michael Jurka18014792010-10-14 09:01:34 -0700289 }
290
Sunny Goyale9b651e2015-04-24 11:44:51 -0700291 public void enableAccessibleDrag(boolean enable, int dragType) {
Adam Cohenc9735cf2015-01-23 16:11:55 -0800292 mUseTouchHelper = enable;
293 if (!enable) {
294 ViewCompat.setAccessibilityDelegate(this, null);
295 setImportantForAccessibility(IMPORTANT_FOR_ACCESSIBILITY_NO);
296 getShortcutsAndWidgets().setImportantForAccessibility(IMPORTANT_FOR_ACCESSIBILITY_NO);
297 setOnClickListener(mLauncher);
298 } else {
Sunny Goyale9b651e2015-04-24 11:44:51 -0700299 if (dragType == WORKSPACE_ACCESSIBILITY_DRAG &&
300 !(mTouchHelper instanceof WorkspaceAccessibilityHelper)) {
301 mTouchHelper = new WorkspaceAccessibilityHelper(this);
302 } else if (dragType == FOLDER_ACCESSIBILITY_DRAG &&
303 !(mTouchHelper instanceof FolderAccessibilityHelper)) {
304 mTouchHelper = new FolderAccessibilityHelper(this);
305 }
Adam Cohenc9735cf2015-01-23 16:11:55 -0800306 ViewCompat.setAccessibilityDelegate(this, mTouchHelper);
307 setImportantForAccessibility(IMPORTANT_FOR_ACCESSIBILITY_YES);
308 getShortcutsAndWidgets().setImportantForAccessibility(IMPORTANT_FOR_ACCESSIBILITY_YES);
309 setOnClickListener(mTouchHelper);
310 }
311
312 // Invalidate the accessibility hierarchy
313 if (getParent() != null) {
314 getParent().notifySubtreeAccessibilityStateChanged(
315 this, this, AccessibilityEvent.CONTENT_CHANGE_TYPE_SUBTREE);
316 }
317 }
318
319 @Override
320 public boolean dispatchHoverEvent(MotionEvent event) {
321 // Always attempt to dispatch hover events to accessibility first.
322 if (mUseTouchHelper && mTouchHelper.dispatchHoverEvent(event)) {
323 return true;
324 }
325 return super.dispatchHoverEvent(event);
326 }
327
328 @Override
Adam Cohenc9735cf2015-01-23 16:11:55 -0800329 public boolean onInterceptTouchEvent(MotionEvent ev) {
330 if (mUseTouchHelper ||
331 (mInterceptTouchListener != null && mInterceptTouchListener.onTouch(this, ev))) {
332 return true;
333 }
334 return false;
335 }
336
Mady Melloref044dd2015-06-02 15:35:07 -0700337 @Override
338 public boolean onTouchEvent(MotionEvent ev) {
339 boolean handled = super.onTouchEvent(ev);
340 // Stylus button press on a home screen should not switch between overview mode and
341 // the home screen mode, however, once in overview mode stylus button press should be
342 // enabled to allow rearranging the different home screens. So check what mode
343 // the workspace is in, and only perform stylus button presses while in overview mode.
Sunny Goyalf9403d92017-10-18 10:55:56 -0700344 if (mLauncher.isInState(LauncherState.OVERVIEW)
Mady Mellorbb835202015-07-15 16:34:34 -0700345 && mStylusEventHelper.onMotionEvent(ev)) {
Mady Melloref044dd2015-06-02 15:35:07 -0700346 return true;
347 }
348 return handled;
349 }
350
Chris Craik01f2d7f2013-10-01 14:41:56 -0700351 public void enableHardwareLayer(boolean hasLayer) {
352 mShortcutsAndWidgets.setLayerType(hasLayer ? LAYER_TYPE_HARDWARE : LAYER_TYPE_NONE, sPaint);
Michael Jurkad51f33a2012-06-28 15:35:26 -0700353 }
354
Winson Chung5f8afe62013-08-12 16:19:28 -0700355 public void setCellDimensions(int width, int height) {
Winson Chung11a1a532013-09-13 11:14:45 -0700356 mFixedCellWidth = mCellWidth = width;
357 mFixedCellHeight = mCellHeight = height;
Sunny Goyalaa8a8712016-11-20 15:26:01 +0530358 mShortcutsAndWidgets.setCellDimensions(mCellWidth, mCellHeight, mCountX, mCountY);
Winson Chung5f8afe62013-08-12 16:19:28 -0700359 }
360
Adam Cohen2801caf2011-05-13 20:57:39 -0700361 public void setGridSize(int x, int y) {
362 mCountX = x;
363 mCountY = y;
Sunny Goyalff4ba2d2016-04-02 14:12:34 -0700364 mOccupied = new GridOccupancy(mCountX, mCountY);
365 mTmpOccupied = new GridOccupancy(mCountX, mCountY);
Adam Cohen7fbec102012-03-27 12:42:19 -0700366 mTempRectStack.clear();
Sunny Goyalaa8a8712016-11-20 15:26:01 +0530367 mShortcutsAndWidgets.setCellDimensions(mCellWidth, mCellHeight, mCountX, mCountY);
Adam Cohen76fc0852011-06-17 13:26:23 -0700368 requestLayout();
Adam Cohen2801caf2011-05-13 20:57:39 -0700369 }
370
Adam Cohen2374abf2013-04-16 14:56:57 -0700371 // Set whether or not to invert the layout horizontally if the layout is in RTL mode.
372 public void setInvertIfRtl(boolean invert) {
373 mShortcutsAndWidgets.setInvertIfRtl(invert);
374 }
375
Adam Cohen917e3882013-10-31 15:03:35 -0700376 public void setDropPending(boolean pending) {
377 mDropPending = pending;
378 }
379
380 public boolean isDropPending() {
381 return mDropPending;
382 }
383
Sunny Goyal4b6eb262015-05-14 19:24:40 -0700384 @Override
385 public void setPressedIcon(BubbleTextView icon, Bitmap background) {
Sunny Goyal508da152014-08-14 10:53:27 -0700386 if (icon == null || background == null) {
387 mTouchFeedbackView.setBitmap(null);
388 mTouchFeedbackView.animate().cancel();
389 } else {
Sunny Goyal508da152014-08-14 10:53:27 -0700390 if (mTouchFeedbackView.setBitmap(background)) {
Winsonbe9798b2016-07-20 12:55:49 -0700391 mTouchFeedbackView.alignWithIconView(icon, mShortcutsAndWidgets,
392 null /* clipAgainstView */);
Sunny Goyal4fe5a372015-05-14 19:55:10 -0700393 mTouchFeedbackView.animateShadow();
Sunny Goyal508da152014-08-14 10:53:27 -0700394 }
Patrick Dubroy96864c32011-03-10 17:17:23 -0800395 }
396 }
397
Adam Cohenc50438c2014-08-19 17:43:05 -0700398 void setIsDragOverlapping(boolean isDragOverlapping) {
399 if (mIsDragOverlapping != isDragOverlapping) {
400 mIsDragOverlapping = isDragOverlapping;
Sunny Goyalf5440cb2016-12-14 15:13:00 -0800401 mBackground.setState(mIsDragOverlapping
402 ? BACKGROUND_STATE_ACTIVE : BACKGROUND_STATE_DEFAULT);
Adam Cohenc50438c2014-08-19 17:43:05 -0700403 invalidate();
404 }
405 }
406
Sunny Goyale2fd14b2015-08-27 17:45:46 -0700407 @Override
408 protected void dispatchSaveInstanceState(SparseArray<Parcelable> container) {
Sunny Goyal7ce471b2017-08-02 03:37:39 -0700409 ParcelableSparseArray jail = getJailedArray(container);
410 super.dispatchSaveInstanceState(jail);
411 container.put(R.id.cell_layout_jail_id, jail);
Sunny Goyale2fd14b2015-08-27 17:45:46 -0700412 }
413
414 @Override
415 protected void dispatchRestoreInstanceState(SparseArray<Parcelable> container) {
Sunny Goyal7ce471b2017-08-02 03:37:39 -0700416 super.dispatchRestoreInstanceState(getJailedArray(container));
Sunny Goyale2fd14b2015-08-27 17:45:46 -0700417 }
418
Sunny Goyal7ce471b2017-08-02 03:37:39 -0700419 /**
420 * Wrap the SparseArray in another Parcelable so that the item ids do not conflict with our
421 * our internal resource ids
422 */
Sunny Goyale2fd14b2015-08-27 17:45:46 -0700423 private ParcelableSparseArray getJailedArray(SparseArray<Parcelable> container) {
424 final Parcelable parcelable = container.get(R.id.cell_layout_jail_id);
425 return parcelable instanceof ParcelableSparseArray ?
426 (ParcelableSparseArray) parcelable : new ParcelableSparseArray();
427 }
428
Tony Wickham0f97b782015-12-02 17:55:07 -0800429 public boolean getIsDragOverlapping() {
430 return mIsDragOverlapping;
431 }
432
Jeff Sharkey83f111d2009-04-20 21:03:13 -0700433 @Override
Patrick Dubroy1262e362010-10-06 15:49:50 -0700434 protected void onDraw(Canvas canvas) {
Michael Jurka3e7c7632010-10-02 16:01:03 -0700435 // When we're large, we are either drawn in a "hover" state (ie when dragging an item to
436 // a neighboring page) or with just a normal background (if backgroundAlpha > 0.0f)
437 // When we're small, we are either drawn normally or in the "accepts drops" state (during
438 // a drag). However, we also drag the mini hover background *over* one of those two
439 // backgrounds
Sunny Goyalaeb16432017-10-16 11:46:41 -0700440 if (mBackground.getAlpha() > 0) {
Sunny Goyal2805e632015-05-20 15:35:32 -0700441 mBackground.draw(canvas);
Michael Jurkaa63c4522010-08-19 13:52:27 -0700442 }
Romain Guya6abce82009-11-10 02:54:41 -0800443
Patrick Dubroy8e58e912010-10-14 13:21:48 -0700444 final Paint paint = mDragOutlinePaint;
Joe Onorato4be866d2010-10-10 11:26:02 -0700445 for (int i = 0; i < mDragOutlines.length; i++) {
Chet Haase472b2812010-10-14 07:02:04 -0700446 final float alpha = mDragOutlineAlphas[i];
Joe Onorato4be866d2010-10-10 11:26:02 -0700447 if (alpha > 0) {
Joe Onorato4be866d2010-10-10 11:26:02 -0700448 final Bitmap b = (Bitmap) mDragOutlineAnims[i].getTag();
Chet Haase472b2812010-10-14 07:02:04 -0700449 paint.setAlpha((int)(alpha + .5f));
Sunny Goyal106bf642015-07-16 12:18:06 -0700450 canvas.drawBitmap(b, null, mDragOutlines[i], paint);
Winson Chung150fbab2010-09-29 17:14:26 -0700451 }
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700452 }
Patrick Dubroy96864c32011-03-10 17:17:23 -0800453
Adam Cohen482ed822012-03-02 14:15:13 -0800454 if (DEBUG_VISUALIZE_OCCUPIED) {
455 int[] pt = new int[2];
456 ColorDrawable cd = new ColorDrawable(Color.RED);
Adam Cohene7587d22012-05-24 18:50:02 -0700457 cd.setBounds(0, 0, mCellWidth, mCellHeight);
Adam Cohen482ed822012-03-02 14:15:13 -0800458 for (int i = 0; i < mCountX; i++) {
459 for (int j = 0; j < mCountY; j++) {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -0700460 if (mOccupied.cells[i][j]) {
Adam Cohen482ed822012-03-02 14:15:13 -0800461 cellToPoint(i, j, pt);
462 canvas.save();
463 canvas.translate(pt[0], pt[1]);
464 cd.draw(canvas);
465 canvas.restore();
466 }
467 }
468 }
469 }
470
Adam Cohenefca0272016-02-24 19:19:06 -0800471 for (int i = 0; i < mFolderBackgrounds.size(); i++) {
Jon Mirandaa0233f72017-06-22 18:34:45 -0700472 PreviewBackground bg = mFolderBackgrounds.get(i);
Adam Cohenefca0272016-02-24 19:19:06 -0800473 cellToPoint(bg.delegateCellX, bg.delegateCellY, mTempLocation);
474 canvas.save();
475 canvas.translate(mTempLocation[0], mTempLocation[1]);
Sunny Goyal19b93b72017-02-19 20:21:37 -0800476 bg.drawBackground(canvas);
Adam Cohenf172b742016-03-30 19:28:34 -0700477 if (!bg.isClipping) {
Sunny Goyal19b93b72017-02-19 20:21:37 -0800478 bg.drawBackgroundStroke(canvas);
Adam Cohenf172b742016-03-30 19:28:34 -0700479 }
Adam Cohenefca0272016-02-24 19:19:06 -0800480 canvas.restore();
Adam Cohen69ce2e52011-07-03 19:25:21 -0700481 }
Adam Cohenc51934b2011-07-26 21:07:43 -0700482
Adam Cohenefca0272016-02-24 19:19:06 -0800483 if (mFolderLeaveBehind.delegateCellX >= 0 && mFolderLeaveBehind.delegateCellY >= 0) {
484 cellToPoint(mFolderLeaveBehind.delegateCellX,
485 mFolderLeaveBehind.delegateCellY, mTempLocation);
486 canvas.save();
487 canvas.translate(mTempLocation[0], mTempLocation[1]);
Sunny Goyal19b93b72017-02-19 20:21:37 -0800488 mFolderLeaveBehind.drawLeaveBehind(canvas);
Adam Cohenefca0272016-02-24 19:19:06 -0800489 canvas.restore();
Adam Cohenc51934b2011-07-26 21:07:43 -0700490 }
Adam Cohen69ce2e52011-07-03 19:25:21 -0700491 }
492
Adam Cohenefca0272016-02-24 19:19:06 -0800493 @Override
494 protected void dispatchDraw(Canvas canvas) {
495 super.dispatchDraw(canvas);
496
497 for (int i = 0; i < mFolderBackgrounds.size(); i++) {
Jon Mirandaa0233f72017-06-22 18:34:45 -0700498 PreviewBackground bg = mFolderBackgrounds.get(i);
Adam Cohenf172b742016-03-30 19:28:34 -0700499 if (bg.isClipping) {
500 cellToPoint(bg.delegateCellX, bg.delegateCellY, mTempLocation);
501 canvas.save();
502 canvas.translate(mTempLocation[0], mTempLocation[1]);
Sunny Goyal19b93b72017-02-19 20:21:37 -0800503 bg.drawBackgroundStroke(canvas);
Adam Cohenf172b742016-03-30 19:28:34 -0700504 canvas.restore();
505 }
Adam Cohenefca0272016-02-24 19:19:06 -0800506 }
Adam Cohen69ce2e52011-07-03 19:25:21 -0700507 }
508
Jon Mirandaa0233f72017-06-22 18:34:45 -0700509 public void addFolderBackground(PreviewBackground bg) {
Adam Cohenefca0272016-02-24 19:19:06 -0800510 mFolderBackgrounds.add(bg);
511 }
Jon Mirandaa0233f72017-06-22 18:34:45 -0700512 public void removeFolderBackground(PreviewBackground bg) {
Adam Cohenefca0272016-02-24 19:19:06 -0800513 mFolderBackgrounds.remove(bg);
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700514 }
515
Adam Cohenc51934b2011-07-26 21:07:43 -0700516 public void setFolderLeaveBehindCell(int x, int y) {
Adam Cohenefca0272016-02-24 19:19:06 -0800517 View child = getChildAt(x, y);
Sunny Goyal368ae772017-05-24 13:19:15 -0700518 mFolderLeaveBehind.setup(mLauncher, null,
Adam Cohenefca0272016-02-24 19:19:06 -0800519 child.getMeasuredWidth(), child.getPaddingTop());
520
521 mFolderLeaveBehind.delegateCellX = x;
522 mFolderLeaveBehind.delegateCellY = y;
Adam Cohenc51934b2011-07-26 21:07:43 -0700523 invalidate();
524 }
525
526 public void clearFolderLeaveBehind() {
Adam Cohenefca0272016-02-24 19:19:06 -0800527 mFolderLeaveBehind.delegateCellX = -1;
528 mFolderLeaveBehind.delegateCellY = -1;
Adam Cohenc51934b2011-07-26 21:07:43 -0700529 invalidate();
530 }
531
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700532 @Override
Michael Jurkae6235dd2011-10-04 15:02:05 -0700533 public boolean shouldDelayChildPressedState() {
534 return false;
535 }
536
Adam Cohen1462de32012-07-24 22:34:36 -0700537 public void restoreInstanceState(SparseArray<Parcelable> states) {
Sunny Goyal33a152f2014-07-22 12:13:14 -0700538 try {
539 dispatchRestoreInstanceState(states);
540 } catch (IllegalArgumentException ex) {
Sunny Goyal3d706ad2017-03-06 16:56:39 -0800541 if (FeatureFlags.IS_DOGFOOD_BUILD) {
Sunny Goyal33a152f2014-07-22 12:13:14 -0700542 throw ex;
543 }
544 // Mismatched viewId / viewType preventing restore. Skip restore on production builds.
545 Log.e(TAG, "Ignoring an error while restoring a view instance state", ex);
546 }
Adam Cohen1462de32012-07-24 22:34:36 -0700547 }
548
Michael Jurkae6235dd2011-10-04 15:02:05 -0700549 @Override
Jeff Sharkey83f111d2009-04-20 21:03:13 -0700550 public void cancelLongPress() {
551 super.cancelLongPress();
552
553 // Cancel long press for all children
554 final int count = getChildCount();
555 for (int i = 0; i < count; i++) {
556 final View child = getChildAt(i);
557 child.cancelLongPress();
558 }
559 }
560
Michael Jurkadee05892010-07-27 10:01:56 -0700561 public void setOnInterceptTouchListener(View.OnTouchListener listener) {
562 mInterceptTouchListener = listener;
563 }
564
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800565 public int getCountX() {
Adam Cohend22015c2010-07-26 22:02:18 -0700566 return mCountX;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800567 }
568
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800569 public int getCountY() {
Adam Cohend22015c2010-07-26 22:02:18 -0700570 return mCountY;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800571 }
572
Sunny Goyalc13403c2016-11-18 23:44:48 -0800573 public boolean acceptsWidget() {
574 return mContainerType == WORKSPACE;
Sunny Goyale9b651e2015-04-24 11:44:51 -0700575 }
576
Andrew Flynn0dca1ec2012-02-29 13:33:22 -0800577 public boolean addViewToCellLayout(View child, int index, int childId, LayoutParams params,
Sunny Goyalf7a29e82015-04-24 15:20:43 -0700578 boolean markCells) {
Winson Chungaafa03c2010-06-11 17:34:16 -0700579 final LayoutParams lp = params;
580
Andrew Flynnde38e422012-05-08 11:22:15 -0700581 // Hotseat icons - remove text
Andrew Flynn0dca1ec2012-02-29 13:33:22 -0800582 if (child instanceof BubbleTextView) {
583 BubbleTextView bubbleChild = (BubbleTextView) child;
Jon Mirandaf1eae802017-10-04 11:23:33 -0700584 bubbleChild.setTextVisibility(mContainerType != HOTSEAT);
Andrew Flynn0dca1ec2012-02-29 13:33:22 -0800585 }
586
Sunny Goyalc13403c2016-11-18 23:44:48 -0800587 child.setScaleX(mChildScale);
588 child.setScaleY(mChildScale);
Adam Cohen307fe232012-08-16 17:55:58 -0700589
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800590 // Generate an id for each view, this assumes we have at most 256x256 cells
591 // per workspace screen
Adam Cohend22015c2010-07-26 22:02:18 -0700592 if (lp.cellX >= 0 && lp.cellX <= mCountX - 1 && lp.cellY >= 0 && lp.cellY <= mCountY - 1) {
Winson Chungaafa03c2010-06-11 17:34:16 -0700593 // If the horizontal or vertical span is set to -1, it is taken to
594 // mean that it spans the extent of the CellLayout
Adam Cohend22015c2010-07-26 22:02:18 -0700595 if (lp.cellHSpan < 0) lp.cellHSpan = mCountX;
596 if (lp.cellVSpan < 0) lp.cellVSpan = mCountY;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800597
Winson Chungaafa03c2010-06-11 17:34:16 -0700598 child.setId(childId);
Tony Wickhama0628cc2015-10-14 15:23:04 -0700599 if (LOGD) {
600 Log.d(TAG, "Adding view to ShortcutsAndWidgetsContainer: " + child);
601 }
Sunny Goyalf7a29e82015-04-24 15:20:43 -0700602 mShortcutsAndWidgets.addView(child, index, lp);
Michael Jurkadee05892010-07-27 10:01:56 -0700603
Michael Jurkaf3ca3ab2010-10-20 17:08:24 -0700604 if (markCells) markCellsAsOccupiedForView(child);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700605
Winson Chungaafa03c2010-06-11 17:34:16 -0700606 return true;
607 }
608 return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800609 }
Michael Jurka3e7c7632010-10-02 16:01:03 -0700610
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800611 @Override
Michael Jurka0280c3b2010-09-17 15:00:07 -0700612 public void removeAllViews() {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -0700613 mOccupied.clear();
Michael Jurkaa52570f2012-03-20 03:18:20 -0700614 mShortcutsAndWidgets.removeAllViews();
Michael Jurka0280c3b2010-09-17 15:00:07 -0700615 }
616
617 @Override
618 public void removeAllViewsInLayout() {
Michael Jurkaa52570f2012-03-20 03:18:20 -0700619 if (mShortcutsAndWidgets.getChildCount() > 0) {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -0700620 mOccupied.clear();
Michael Jurkaa52570f2012-03-20 03:18:20 -0700621 mShortcutsAndWidgets.removeAllViewsInLayout();
Michael Jurka7cfc2822011-08-02 20:19:24 -0700622 }
Michael Jurka0280c3b2010-09-17 15:00:07 -0700623 }
624
625 @Override
626 public void removeView(View view) {
627 markCellsAsUnoccupiedForView(view);
Michael Jurkaa52570f2012-03-20 03:18:20 -0700628 mShortcutsAndWidgets.removeView(view);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700629 }
630
631 @Override
632 public void removeViewAt(int index) {
Michael Jurkaa52570f2012-03-20 03:18:20 -0700633 markCellsAsUnoccupiedForView(mShortcutsAndWidgets.getChildAt(index));
634 mShortcutsAndWidgets.removeViewAt(index);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700635 }
636
637 @Override
638 public void removeViewInLayout(View view) {
639 markCellsAsUnoccupiedForView(view);
Michael Jurkaa52570f2012-03-20 03:18:20 -0700640 mShortcutsAndWidgets.removeViewInLayout(view);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700641 }
642
643 @Override
644 public void removeViews(int start, int count) {
645 for (int i = start; i < start + count; i++) {
Michael Jurkaa52570f2012-03-20 03:18:20 -0700646 markCellsAsUnoccupiedForView(mShortcutsAndWidgets.getChildAt(i));
Michael Jurka0280c3b2010-09-17 15:00:07 -0700647 }
Michael Jurkaa52570f2012-03-20 03:18:20 -0700648 mShortcutsAndWidgets.removeViews(start, count);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700649 }
650
651 @Override
652 public void removeViewsInLayout(int start, int count) {
653 for (int i = start; i < start + count; i++) {
Michael Jurkaa52570f2012-03-20 03:18:20 -0700654 markCellsAsUnoccupiedForView(mShortcutsAndWidgets.getChildAt(i));
Michael Jurka0280c3b2010-09-17 15:00:07 -0700655 }
Michael Jurkaa52570f2012-03-20 03:18:20 -0700656 mShortcutsAndWidgets.removeViewsInLayout(start, count);
Michael Jurkaabded662011-03-04 12:06:57 -0800657 }
658
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700659 /**
Winson Chungaafa03c2010-06-11 17:34:16 -0700660 * Given a point, return the cell that strictly encloses that point
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800661 * @param x X coordinate of the point
662 * @param y Y coordinate of the point
663 * @param result Array of 2 ints to hold the x and y coordinate of the cell
664 */
Sunny Goyale9b651e2015-04-24 11:44:51 -0700665 public void pointToCellExact(int x, int y, int[] result) {
Winson Chung4b825dcd2011-06-19 12:41:22 -0700666 final int hStartPadding = getPaddingLeft();
667 final int vStartPadding = getPaddingTop();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800668
Sunny Goyalaa8a8712016-11-20 15:26:01 +0530669 result[0] = (x - hStartPadding) / mCellWidth;
670 result[1] = (y - vStartPadding) / mCellHeight;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800671
Adam Cohend22015c2010-07-26 22:02:18 -0700672 final int xAxis = mCountX;
673 final int yAxis = mCountY;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800674
675 if (result[0] < 0) result[0] = 0;
676 if (result[0] >= xAxis) result[0] = xAxis - 1;
677 if (result[1] < 0) result[1] = 0;
678 if (result[1] >= yAxis) result[1] = yAxis - 1;
679 }
Winson Chungaafa03c2010-06-11 17:34:16 -0700680
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800681 /**
682 * Given a point, return the cell that most closely encloses that point
683 * @param x X coordinate of the point
684 * @param y Y coordinate of the point
685 * @param result Array of 2 ints to hold the x and y coordinate of the cell
686 */
687 void pointToCellRounded(int x, int y, int[] result) {
688 pointToCellExact(x + (mCellWidth / 2), y + (mCellHeight / 2), result);
689 }
690
691 /**
692 * Given a cell coordinate, return the point that represents the upper left corner of that cell
Winson Chungaafa03c2010-06-11 17:34:16 -0700693 *
694 * @param cellX X coordinate of the cell
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800695 * @param cellY Y coordinate of the cell
Winson Chungaafa03c2010-06-11 17:34:16 -0700696 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800697 * @param result Array of 2 ints to hold the x and y coordinate of the point
698 */
699 void cellToPoint(int cellX, int cellY, int[] result) {
Winson Chung4b825dcd2011-06-19 12:41:22 -0700700 final int hStartPadding = getPaddingLeft();
701 final int vStartPadding = getPaddingTop();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800702
Sunny Goyalaa8a8712016-11-20 15:26:01 +0530703 result[0] = hStartPadding + cellX * mCellWidth;
704 result[1] = vStartPadding + cellY * mCellHeight;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800705 }
706
Adam Cohene3e27a82011-04-15 12:07:39 -0700707 /**
Adam Cohen482ed822012-03-02 14:15:13 -0800708 * Given a cell coordinate, return the point that represents the center of the cell
Adam Cohene3e27a82011-04-15 12:07:39 -0700709 *
710 * @param cellX X coordinate of the cell
711 * @param cellY Y coordinate of the cell
712 *
713 * @param result Array of 2 ints to hold the x and y coordinate of the point
714 */
715 void cellToCenterPoint(int cellX, int cellY, int[] result) {
Adam Cohen47a876d2012-03-19 13:21:41 -0700716 regionToCenterPoint(cellX, cellY, 1, 1, result);
717 }
718
719 /**
720 * Given a cell coordinate and span return the point that represents the center of the regio
721 *
722 * @param cellX X coordinate of the cell
723 * @param cellY Y coordinate of the cell
724 *
725 * @param result Array of 2 ints to hold the x and y coordinate of the point
726 */
727 void regionToCenterPoint(int cellX, int cellY, int spanX, int spanY, int[] result) {
Winson Chung4b825dcd2011-06-19 12:41:22 -0700728 final int hStartPadding = getPaddingLeft();
729 final int vStartPadding = getPaddingTop();
Sunny Goyalaa8a8712016-11-20 15:26:01 +0530730 result[0] = hStartPadding + cellX * mCellWidth + (spanX * mCellWidth) / 2;
731 result[1] = vStartPadding + cellY * mCellHeight + (spanY * mCellHeight) / 2;
Adam Cohene3e27a82011-04-15 12:07:39 -0700732 }
733
Adam Cohen19f37922012-03-21 11:59:11 -0700734 /**
735 * Given a cell coordinate and span fills out a corresponding pixel rect
736 *
737 * @param cellX X coordinate of the cell
738 * @param cellY Y coordinate of the cell
739 * @param result Rect in which to write the result
740 */
741 void regionToRect(int cellX, int cellY, int spanX, int spanY, Rect result) {
742 final int hStartPadding = getPaddingLeft();
743 final int vStartPadding = getPaddingTop();
Sunny Goyalaa8a8712016-11-20 15:26:01 +0530744 final int left = hStartPadding + cellX * mCellWidth;
745 final int top = vStartPadding + cellY * mCellHeight;
746 result.set(left, top, left + (spanX * mCellWidth), top + (spanY * mCellHeight));
Adam Cohen19f37922012-03-21 11:59:11 -0700747 }
748
Adam Cohen482ed822012-03-02 14:15:13 -0800749 public float getDistanceFromCell(float x, float y, int[] cell) {
750 cellToCenterPoint(cell[0], cell[1], mTmpPoint);
Sunny Goyalf7a29e82015-04-24 15:20:43 -0700751 return (float) Math.hypot(x - mTmpPoint[0], y - mTmpPoint[1]);
Adam Cohen482ed822012-03-02 14:15:13 -0800752 }
753
Adam Cohenf9c184a2016-01-15 16:47:43 -0800754 public int getCellWidth() {
Romain Guy84f296c2009-11-04 15:00:44 -0800755 return mCellWidth;
756 }
757
Sunny Goyal0b754e52017-08-07 07:42:45 -0700758 public int getCellHeight() {
Romain Guy84f296c2009-11-04 15:00:44 -0800759 return mCellHeight;
760 }
761
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700762 public void setFixedSize(int width, int height) {
763 mFixedWidth = width;
764 mFixedHeight = height;
765 }
766
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800767 @Override
768 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800769 int widthSpecMode = MeasureSpec.getMode(widthMeasureSpec);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800770 int heightSpecMode = MeasureSpec.getMode(heightMeasureSpec);
Winson Chung5f8afe62013-08-12 16:19:28 -0700771 int widthSize = MeasureSpec.getSize(widthMeasureSpec);
772 int heightSize = MeasureSpec.getSize(heightMeasureSpec);
Winson Chung2d75f122013-09-23 16:53:31 -0700773 int childWidthSize = widthSize - (getPaddingLeft() + getPaddingRight());
774 int childHeightSize = heightSize - (getPaddingTop() + getPaddingBottom());
Winson Chung11a1a532013-09-13 11:14:45 -0700775 if (mFixedCellWidth < 0 || mFixedCellHeight < 0) {
Sunny Goyalc6205602015-05-21 20:46:33 -0700776 int cw = DeviceProfile.calculateCellWidth(childWidthSize, mCountX);
777 int ch = DeviceProfile.calculateCellHeight(childHeightSize, mCountY);
Winson Chung11a1a532013-09-13 11:14:45 -0700778 if (cw != mCellWidth || ch != mCellHeight) {
779 mCellWidth = cw;
780 mCellHeight = ch;
Sunny Goyalaa8a8712016-11-20 15:26:01 +0530781 mShortcutsAndWidgets.setCellDimensions(mCellWidth, mCellHeight, mCountX, mCountY);
Winson Chung11a1a532013-09-13 11:14:45 -0700782 }
Winson Chung5f8afe62013-08-12 16:19:28 -0700783 }
Winson Chungaafa03c2010-06-11 17:34:16 -0700784
Winson Chung2d75f122013-09-23 16:53:31 -0700785 int newWidth = childWidthSize;
786 int newHeight = childHeightSize;
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700787 if (mFixedWidth > 0 && mFixedHeight > 0) {
788 newWidth = mFixedWidth;
789 newHeight = mFixedHeight;
790 } else if (widthSpecMode == MeasureSpec.UNSPECIFIED || heightSpecMode == MeasureSpec.UNSPECIFIED) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800791 throw new RuntimeException("CellLayout cannot have UNSPECIFIED dimensions");
792 }
793
Sunny Goyal4fe5a372015-05-14 19:55:10 -0700794 // Make the feedback view large enough to hold the blur bitmap.
795 mTouchFeedbackView.measure(
796 MeasureSpec.makeMeasureSpec(mCellWidth + mTouchFeedbackView.getExtraSize(),
797 MeasureSpec.EXACTLY),
798 MeasureSpec.makeMeasureSpec(mCellHeight + mTouchFeedbackView.getExtraSize(),
799 MeasureSpec.EXACTLY));
800
801 mShortcutsAndWidgets.measure(
802 MeasureSpec.makeMeasureSpec(newWidth, MeasureSpec.EXACTLY),
803 MeasureSpec.makeMeasureSpec(newHeight, MeasureSpec.EXACTLY));
804
805 int maxWidth = mShortcutsAndWidgets.getMeasuredWidth();
806 int maxHeight = mShortcutsAndWidgets.getMeasuredHeight();
Winson Chung2d75f122013-09-23 16:53:31 -0700807 if (mFixedWidth > 0 && mFixedHeight > 0) {
808 setMeasuredDimension(maxWidth, maxHeight);
809 } else {
810 setMeasuredDimension(widthSize, heightSize);
811 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800812 }
813
814 @Override
Michael Jurka28750fb2010-09-24 17:43:49 -0700815 protected void onLayout(boolean changed, int l, int t, int r, int b) {
Tony Wickham26b01422015-11-10 14:44:32 -0800816 int left = getPaddingLeft();
Sunny Goyal7ce471b2017-08-02 03:37:39 -0700817 left += (int) Math.ceil(getUnusedHorizontalSpace() / 2f);
Sunny Goyal7c786f72016-06-01 14:08:21 -0700818 int right = r - l - getPaddingRight();
Sunny Goyal7ce471b2017-08-02 03:37:39 -0700819 right -= (int) Math.ceil(getUnusedHorizontalSpace() / 2f);
Sunny Goyal7c786f72016-06-01 14:08:21 -0700820
Winson Chung38848ca2013-10-08 12:03:44 -0700821 int top = getPaddingTop();
Sunny Goyal7c786f72016-06-01 14:08:21 -0700822 int bottom = b - t - getPaddingBottom();
Sunny Goyal4fe5a372015-05-14 19:55:10 -0700823
824 mTouchFeedbackView.layout(left, top,
825 left + mTouchFeedbackView.getMeasuredWidth(),
826 top + mTouchFeedbackView.getMeasuredHeight());
Sunny Goyal7c786f72016-06-01 14:08:21 -0700827 mShortcutsAndWidgets.layout(left, top, right, bottom);
828
829 // Expand the background drawing bounds by the padding baked into the background drawable
830 mBackground.getPadding(mTempRect);
831 mBackground.setBounds(
Jon Miranda28032002017-07-13 16:18:56 -0700832 left - mTempRect.left - getPaddingLeft(),
833 top - mTempRect.top - getPaddingTop(),
834 right + mTempRect.right + getPaddingRight(),
835 bottom + mTempRect.bottom + getPaddingBottom());
Sunny Goyal7c786f72016-06-01 14:08:21 -0700836 }
837
Tony Wickhama501d492015-11-03 18:05:01 -0800838 /**
839 * Returns the amount of space left over after subtracting padding and cells. This space will be
840 * very small, a few pixels at most, and is a result of rounding down when calculating the cell
841 * width in {@link DeviceProfile#calculateCellWidth(int, int)}.
842 */
843 public int getUnusedHorizontalSpace() {
844 return getMeasuredWidth() - getPaddingLeft() - getPaddingRight() - (mCountX * mCellWidth);
845 }
846
Sunny Goyalaeb16432017-10-16 11:46:41 -0700847 public Drawable getScrimBackground() {
848 return mBackground;
Michael Jurkadee05892010-07-27 10:01:56 -0700849 }
850
Sunny Goyal2805e632015-05-20 15:35:32 -0700851 @Override
852 protected boolean verifyDrawable(Drawable who) {
Sunny Goyal7ce471b2017-08-02 03:37:39 -0700853 return super.verifyDrawable(who) || (who == mBackground);
Sunny Goyal2805e632015-05-20 15:35:32 -0700854 }
855
Michael Jurkaa52570f2012-03-20 03:18:20 -0700856 public void setShortcutAndWidgetAlpha(float alpha) {
Sunny Goyal02b50812014-09-10 15:44:42 -0700857 mShortcutsAndWidgets.setAlpha(alpha);
Michael Jurkadee05892010-07-27 10:01:56 -0700858 }
859
Michael Jurkaa52570f2012-03-20 03:18:20 -0700860 public ShortcutAndWidgetContainer getShortcutsAndWidgets() {
Sunny Goyaldcbcc862014-08-12 15:58:36 -0700861 return mShortcutsAndWidgets;
Michael Jurkaa52570f2012-03-20 03:18:20 -0700862 }
863
Patrick Dubroy440c3602010-07-13 17:50:32 -0700864 public View getChildAt(int x, int y) {
Michael Jurkaa52570f2012-03-20 03:18:20 -0700865 return mShortcutsAndWidgets.getChildAt(x, y);
Patrick Dubroy440c3602010-07-13 17:50:32 -0700866 }
867
Adam Cohen76fc0852011-06-17 13:26:23 -0700868 public boolean animateChildToPosition(final View child, int cellX, int cellY, int duration,
Adam Cohen482ed822012-03-02 14:15:13 -0800869 int delay, boolean permanent, boolean adjustOccupied) {
Michael Jurkaa52570f2012-03-20 03:18:20 -0700870 ShortcutAndWidgetContainer clc = getShortcutsAndWidgets();
Adam Cohen482ed822012-03-02 14:15:13 -0800871
Adam Cohen19f37922012-03-21 11:59:11 -0700872 if (clc.indexOfChild(child) != -1) {
Adam Cohenbfbfd262011-06-13 16:55:12 -0700873 final LayoutParams lp = (LayoutParams) child.getLayoutParams();
874 final ItemInfo info = (ItemInfo) child.getTag();
875
876 // We cancel any existing animations
877 if (mReorderAnimators.containsKey(lp)) {
878 mReorderAnimators.get(lp).cancel();
879 mReorderAnimators.remove(lp);
880 }
881
Adam Cohen482ed822012-03-02 14:15:13 -0800882 final int oldX = lp.x;
883 final int oldY = lp.y;
884 if (adjustOccupied) {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -0700885 GridOccupancy occupied = permanent ? mOccupied : mTmpOccupied;
886 occupied.markCells(lp.cellX, lp.cellY, lp.cellHSpan, lp.cellVSpan, false);
887 occupied.markCells(cellX, cellY, lp.cellHSpan, lp.cellVSpan, true);
Adam Cohen482ed822012-03-02 14:15:13 -0800888 }
Adam Cohenbfbfd262011-06-13 16:55:12 -0700889 lp.isLockedToGrid = true;
Adam Cohen482ed822012-03-02 14:15:13 -0800890 if (permanent) {
891 lp.cellX = info.cellX = cellX;
892 lp.cellY = info.cellY = cellY;
893 } else {
894 lp.tmpCellX = cellX;
895 lp.tmpCellY = cellY;
896 }
Jon Mirandae96798e2016-12-07 12:10:44 -0800897 clc.setupLp(child);
Adam Cohenbfbfd262011-06-13 16:55:12 -0700898 lp.isLockedToGrid = false;
Adam Cohen482ed822012-03-02 14:15:13 -0800899 final int newX = lp.x;
900 final int newY = lp.y;
Adam Cohenbfbfd262011-06-13 16:55:12 -0700901
Adam Cohen76fc0852011-06-17 13:26:23 -0700902 lp.x = oldX;
903 lp.y = oldY;
Adam Cohen76fc0852011-06-17 13:26:23 -0700904
Adam Cohen482ed822012-03-02 14:15:13 -0800905 // Exit early if we're not actually moving the view
906 if (oldX == newX && oldY == newY) {
907 lp.isLockedToGrid = true;
908 return true;
909 }
910
Jon Mirandacda3bfb2017-02-06 15:54:41 -0800911 ValueAnimator va = LauncherAnimUtils.ofFloat(0f, 1f);
Adam Cohen482ed822012-03-02 14:15:13 -0800912 va.setDuration(duration);
913 mReorderAnimators.put(lp, va);
914
915 va.addUpdateListener(new AnimatorUpdateListener() {
916 @Override
Adam Cohenbfbfd262011-06-13 16:55:12 -0700917 public void onAnimationUpdate(ValueAnimator animation) {
Jon Mirandae96798e2016-12-07 12:10:44 -0800918 float r = (Float) animation.getAnimatedValue();
Adam Cohen19f37922012-03-21 11:59:11 -0700919 lp.x = (int) ((1 - r) * oldX + r * newX);
920 lp.y = (int) ((1 - r) * oldY + r * newY);
Adam Cohen6b8a02d2012-03-22 15:13:40 -0700921 child.requestLayout();
Adam Cohenbfbfd262011-06-13 16:55:12 -0700922 }
923 });
Adam Cohen482ed822012-03-02 14:15:13 -0800924 va.addListener(new AnimatorListenerAdapter() {
Adam Cohenbfbfd262011-06-13 16:55:12 -0700925 boolean cancelled = false;
926 public void onAnimationEnd(Animator animation) {
927 // If the animation was cancelled, it means that another animation
928 // has interrupted this one, and we don't want to lock the item into
929 // place just yet.
930 if (!cancelled) {
931 lp.isLockedToGrid = true;
Adam Cohen482ed822012-03-02 14:15:13 -0800932 child.requestLayout();
Adam Cohenbfbfd262011-06-13 16:55:12 -0700933 }
934 if (mReorderAnimators.containsKey(lp)) {
935 mReorderAnimators.remove(lp);
936 }
937 }
938 public void onAnimationCancel(Animator animation) {
939 cancelled = true;
940 }
941 });
Adam Cohen482ed822012-03-02 14:15:13 -0800942 va.setStartDelay(delay);
943 va.start();
Adam Cohenbfbfd262011-06-13 16:55:12 -0700944 return true;
945 }
946 return false;
947 }
948
Sunny Goyal06e21a22016-08-11 16:02:02 -0700949 void visualizeDropLocation(View v, DragPreviewProvider outlineProvider, int cellX, int cellY,
950 int spanX, int spanY, boolean resize, DropTarget.DragObject dragObject) {
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -0700951 final int oldDragCellX = mDragCell[0];
952 final int oldDragCellY = mDragCell[1];
Adam Cohen482ed822012-03-02 14:15:13 -0800953
Hyunyoung Song0de01172016-10-05 16:27:48 -0700954 if (outlineProvider == null || outlineProvider.generatedDragOutline == null) {
Adam Cohen2801caf2011-05-13 20:57:39 -0700955 return;
956 }
957
Hyunyoung Song0de01172016-10-05 16:27:48 -0700958 Bitmap dragOutline = outlineProvider.generatedDragOutline;
Adam Cohen482ed822012-03-02 14:15:13 -0800959 if (cellX != oldDragCellX || cellY != oldDragCellY) {
Sunny Goyale78e3d72015-09-24 11:23:31 -0700960 Point dragOffset = dragObject.dragView.getDragVisualizeOffset();
961 Rect dragRegion = dragObject.dragView.getDragRegion();
962
Adam Cohen482ed822012-03-02 14:15:13 -0800963 mDragCell[0] = cellX;
964 mDragCell[1] = cellY;
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700965
Joe Onorato4be866d2010-10-10 11:26:02 -0700966 final int oldIndex = mDragOutlineCurrent;
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -0700967 mDragOutlineAnims[oldIndex].animateOut();
968 mDragOutlineCurrent = (oldIndex + 1) % mDragOutlines.length;
Adam Cohend41fbf52012-02-16 23:53:59 -0800969 Rect r = mDragOutlines[mDragOutlineCurrent];
Sunny Goyal106bf642015-07-16 12:18:06 -0700970
Adam Cohend41fbf52012-02-16 23:53:59 -0800971 if (resize) {
Adam Cohen482ed822012-03-02 14:15:13 -0800972 cellToRect(cellX, cellY, spanX, spanY, r);
Jon Mirandae96798e2016-12-07 12:10:44 -0800973 if (v instanceof LauncherAppWidgetHostView) {
974 DeviceProfile profile = mLauncher.getDeviceProfile();
Jon Miranda6f6a06a2016-12-15 11:24:18 -0800975 Utilities.shrinkRect(r, profile.appWidgetScale.x, profile.appWidgetScale.y);
Jon Mirandae96798e2016-12-07 12:10:44 -0800976 }
Sunny Goyal106bf642015-07-16 12:18:06 -0700977 } else {
978 // Find the top left corner of the rect the object will occupy
979 final int[] topLeft = mTmpPoint;
980 cellToPoint(cellX, cellY, topLeft);
981
982 int left = topLeft[0];
983 int top = topLeft[1];
984
985 if (v != null && dragOffset == null) {
986 // When drawing the drag outline, it did not account for margin offsets
987 // added by the view's parent.
988 MarginLayoutParams lp = (MarginLayoutParams) v.getLayoutParams();
989 left += lp.leftMargin;
990 top += lp.topMargin;
991
992 // Offsets due to the size difference between the View and the dragOutline.
993 // There is a size difference to account for the outer blur, which may lie
994 // outside the bounds of the view.
Jon Mirandaf7ff3fe2016-12-05 12:04:44 -0800995 top += ((mCellHeight * spanY) - dragOutline.getHeight()) / 2;
Sunny Goyal106bf642015-07-16 12:18:06 -0700996 // We center about the x axis
Sunny Goyalaa8a8712016-11-20 15:26:01 +0530997 left += ((mCellWidth * spanX) - dragOutline.getWidth()) / 2;
Sunny Goyal106bf642015-07-16 12:18:06 -0700998 } else {
999 if (dragOffset != null && dragRegion != null) {
1000 // Center the drag region *horizontally* in the cell and apply a drag
1001 // outline offset
Sunny Goyalaa8a8712016-11-20 15:26:01 +05301002 left += dragOffset.x + ((mCellWidth * spanX) - dragRegion.width()) / 2;
Sunny Goyal106bf642015-07-16 12:18:06 -07001003 int cHeight = getShortcutsAndWidgets().getCellContentHeight();
1004 int cellPaddingY = (int) Math.max(0, ((mCellHeight - cHeight) / 2f));
1005 top += dragOffset.y + cellPaddingY;
1006 } else {
1007 // Center the drag outline in the cell
Sunny Goyalaa8a8712016-11-20 15:26:01 +05301008 left += ((mCellWidth * spanX) - dragOutline.getWidth()) / 2;
1009 top += ((mCellHeight * spanY) - dragOutline.getHeight()) / 2;
Sunny Goyal106bf642015-07-16 12:18:06 -07001010 }
1011 }
1012 r.set(left, top, left + dragOutline.getWidth(), top + dragOutline.getHeight());
Adam Cohend41fbf52012-02-16 23:53:59 -08001013 }
Winson Chung150fbab2010-09-29 17:14:26 -07001014
Jon Miranda6f6a06a2016-12-15 11:24:18 -08001015 Utilities.scaleRectAboutCenter(r, mChildScale);
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -07001016 mDragOutlineAnims[mDragOutlineCurrent].setTag(dragOutline);
1017 mDragOutlineAnims[mDragOutlineCurrent].animateIn();
Sunny Goyale78e3d72015-09-24 11:23:31 -07001018
1019 if (dragObject.stateAnnouncer != null) {
Sunny Goyalc13403c2016-11-18 23:44:48 -08001020 dragObject.stateAnnouncer.announce(getItemMoveDescription(cellX, cellY));
Sunny Goyale78e3d72015-09-24 11:23:31 -07001021 }
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001022 }
1023 }
1024
Sunny Goyalc13403c2016-11-18 23:44:48 -08001025 public String getItemMoveDescription(int cellX, int cellY) {
1026 if (mContainerType == HOTSEAT) {
1027 return getContext().getString(R.string.move_to_hotseat_position,
1028 Math.max(cellX, cellY) + 1);
1029 } else {
1030 return getContext().getString(R.string.move_to_empty_cell,
1031 cellY + 1, cellX + 1);
1032 }
1033 }
1034
Adam Cohene0310962011-04-18 16:15:31 -07001035 public void clearDragOutlines() {
1036 final int oldIndex = mDragOutlineCurrent;
1037 mDragOutlineAnims[oldIndex].animateOut();
Adam Cohend41fbf52012-02-16 23:53:59 -08001038 mDragCell[0] = mDragCell[1] = -1;
Adam Cohene0310962011-04-18 16:15:31 -07001039 }
1040
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001041 /**
Jeff Sharkey70864282009-04-07 21:08:40 -07001042 * Find a vacant area that will fit the given bounds nearest the requested
1043 * cell location. Uses Euclidean distance to score multiple vacant areas.
Winson Chungaafa03c2010-06-11 17:34:16 -07001044 *
Romain Guy51afc022009-05-04 18:03:43 -07001045 * @param pixelX The X location at which you want to search for a vacant area.
1046 * @param pixelY The Y location at which you want to search for a vacant area.
Adam Cohend41fbf52012-02-16 23:53:59 -08001047 * @param minSpanX The minimum horizontal span required
1048 * @param minSpanY The minimum vertical span required
1049 * @param spanX Horizontal span of the object.
1050 * @param spanY Vertical span of the object.
1051 * @param result Array in which to place the result, or null (in which case a new array will
1052 * be allocated)
1053 * @return The X, Y cell of a vacant area that can contain this object,
1054 * nearest the requested location.
1055 */
1056 int[] findNearestVacantArea(int pixelX, int pixelY, int minSpanX, int minSpanY, int spanX,
1057 int spanY, int[] result, int[] resultSpan) {
Sunny Goyalf7a29e82015-04-24 15:20:43 -07001058 return findNearestArea(pixelX, pixelY, minSpanX, minSpanY, spanX, spanY, true,
Adam Cohend41fbf52012-02-16 23:53:59 -08001059 result, resultSpan);
1060 }
1061
Rajeev Kumar9962dbe2017-06-12 12:16:20 -07001062 private final Stack<Rect> mTempRectStack = new Stack<>();
Adam Cohend41fbf52012-02-16 23:53:59 -08001063 private void lazyInitTempRectStack() {
1064 if (mTempRectStack.isEmpty()) {
1065 for (int i = 0; i < mCountX * mCountY; i++) {
1066 mTempRectStack.push(new Rect());
1067 }
1068 }
1069 }
Adam Cohen482ed822012-03-02 14:15:13 -08001070
Adam Cohend41fbf52012-02-16 23:53:59 -08001071 private void recycleTempRects(Stack<Rect> used) {
1072 while (!used.isEmpty()) {
1073 mTempRectStack.push(used.pop());
1074 }
1075 }
1076
1077 /**
1078 * Find a vacant area that will fit the given bounds nearest the requested
1079 * cell location. Uses Euclidean distance to score multiple vacant areas.
1080 *
1081 * @param pixelX The X location at which you want to search for a vacant area.
1082 * @param pixelY The Y location at which you want to search for a vacant area.
1083 * @param minSpanX The minimum horizontal span required
1084 * @param minSpanY The minimum vertical span required
1085 * @param spanX Horizontal span of the object.
1086 * @param spanY Vertical span of the object.
1087 * @param ignoreOccupied If true, the result can be an occupied cell
1088 * @param result Array in which to place the result, or null (in which case a new array will
1089 * be allocated)
1090 * @return The X, Y cell of a vacant area that can contain this object,
1091 * nearest the requested location.
1092 */
Sunny Goyalf7a29e82015-04-24 15:20:43 -07001093 private int[] findNearestArea(int pixelX, int pixelY, int minSpanX, int minSpanY, int spanX,
1094 int spanY, boolean ignoreOccupied, int[] result, int[] resultSpan) {
Adam Cohend41fbf52012-02-16 23:53:59 -08001095 lazyInitTempRectStack();
Michael Jurkac6ee42e2010-09-30 12:04:50 -07001096
Adam Cohene3e27a82011-04-15 12:07:39 -07001097 // For items with a spanX / spanY > 1, the passed in point (pixelX, pixelY) corresponds
1098 // to the center of the item, but we are searching based on the top-left cell, so
1099 // we translate the point over to correspond to the top-left.
Sunny Goyalaa8a8712016-11-20 15:26:01 +05301100 pixelX -= mCellWidth * (spanX - 1) / 2f;
1101 pixelY -= mCellHeight * (spanY - 1) / 2f;
Adam Cohene3e27a82011-04-15 12:07:39 -07001102
Jeff Sharkey70864282009-04-07 21:08:40 -07001103 // Keep track of best-scoring drop area
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001104 final int[] bestXY = result != null ? result : new int[2];
Jeff Sharkey70864282009-04-07 21:08:40 -07001105 double bestDistance = Double.MAX_VALUE;
Adam Cohend41fbf52012-02-16 23:53:59 -08001106 final Rect bestRect = new Rect(-1, -1, -1, -1);
Rajeev Kumar9962dbe2017-06-12 12:16:20 -07001107 final Stack<Rect> validRegions = new Stack<>();
Winson Chungaafa03c2010-06-11 17:34:16 -07001108
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001109 final int countX = mCountX;
1110 final int countY = mCountY;
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001111
Adam Cohend41fbf52012-02-16 23:53:59 -08001112 if (minSpanX <= 0 || minSpanY <= 0 || spanX <= 0 || spanY <= 0 ||
1113 spanX < minSpanX || spanY < minSpanY) {
1114 return bestXY;
1115 }
1116
1117 for (int y = 0; y < countY - (minSpanY - 1); y++) {
Michael Jurkac28de512010-08-13 11:27:44 -07001118 inner:
Adam Cohend41fbf52012-02-16 23:53:59 -08001119 for (int x = 0; x < countX - (minSpanX - 1); x++) {
1120 int ySize = -1;
1121 int xSize = -1;
Adam Cohendf035382011-04-11 17:22:04 -07001122 if (ignoreOccupied) {
Adam Cohend41fbf52012-02-16 23:53:59 -08001123 // First, let's see if this thing fits anywhere
1124 for (int i = 0; i < minSpanX; i++) {
1125 for (int j = 0; j < minSpanY; j++) {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001126 if (mOccupied.cells[x + i][y + j]) {
Adam Cohendf035382011-04-11 17:22:04 -07001127 continue inner;
1128 }
Michael Jurkac28de512010-08-13 11:27:44 -07001129 }
1130 }
Adam Cohend41fbf52012-02-16 23:53:59 -08001131 xSize = minSpanX;
1132 ySize = minSpanY;
1133
1134 // We know that the item will fit at _some_ acceptable size, now let's see
1135 // how big we can make it. We'll alternate between incrementing x and y spans
1136 // until we hit a limit.
1137 boolean incX = true;
1138 boolean hitMaxX = xSize >= spanX;
1139 boolean hitMaxY = ySize >= spanY;
1140 while (!(hitMaxX && hitMaxY)) {
1141 if (incX && !hitMaxX) {
1142 for (int j = 0; j < ySize; j++) {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001143 if (x + xSize > countX -1 || mOccupied.cells[x + xSize][y + j]) {
Adam Cohend41fbf52012-02-16 23:53:59 -08001144 // We can't move out horizontally
1145 hitMaxX = true;
1146 }
1147 }
1148 if (!hitMaxX) {
1149 xSize++;
1150 }
1151 } else if (!hitMaxY) {
1152 for (int i = 0; i < xSize; i++) {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001153 if (y + ySize > countY - 1 || mOccupied.cells[x + i][y + ySize]) {
Adam Cohend41fbf52012-02-16 23:53:59 -08001154 // We can't move out vertically
1155 hitMaxY = true;
1156 }
1157 }
1158 if (!hitMaxY) {
1159 ySize++;
1160 }
1161 }
1162 hitMaxX |= xSize >= spanX;
1163 hitMaxY |= ySize >= spanY;
1164 incX = !incX;
1165 }
1166 incX = true;
1167 hitMaxX = xSize >= spanX;
1168 hitMaxY = ySize >= spanY;
Michael Jurkac28de512010-08-13 11:27:44 -07001169 }
Sunny Goyal2805e632015-05-20 15:35:32 -07001170 final int[] cellXY = mTmpPoint;
Adam Cohene3e27a82011-04-15 12:07:39 -07001171 cellToCenterPoint(x, y, cellXY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001172
Adam Cohend41fbf52012-02-16 23:53:59 -08001173 // We verify that the current rect is not a sub-rect of any of our previous
1174 // candidates. In this case, the current rect is disqualified in favour of the
1175 // containing rect.
1176 Rect currentRect = mTempRectStack.pop();
1177 currentRect.set(x, y, x + xSize, y + ySize);
1178 boolean contained = false;
1179 for (Rect r : validRegions) {
1180 if (r.contains(currentRect)) {
1181 contained = true;
1182 break;
1183 }
1184 }
1185 validRegions.push(currentRect);
Sunny Goyalf7a29e82015-04-24 15:20:43 -07001186 double distance = Math.hypot(cellXY[0] - pixelX, cellXY[1] - pixelY);
Adam Cohen482ed822012-03-02 14:15:13 -08001187
Adam Cohend41fbf52012-02-16 23:53:59 -08001188 if ((distance <= bestDistance && !contained) ||
1189 currentRect.contains(bestRect)) {
Michael Jurkac28de512010-08-13 11:27:44 -07001190 bestDistance = distance;
1191 bestXY[0] = x;
1192 bestXY[1] = y;
Adam Cohend41fbf52012-02-16 23:53:59 -08001193 if (resultSpan != null) {
1194 resultSpan[0] = xSize;
1195 resultSpan[1] = ySize;
1196 }
1197 bestRect.set(currentRect);
Michael Jurkac28de512010-08-13 11:27:44 -07001198 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001199 }
1200 }
1201
Adam Cohenc0dcf592011-06-01 15:30:43 -07001202 // Return -1, -1 if no suitable location found
1203 if (bestDistance == Double.MAX_VALUE) {
1204 bestXY[0] = -1;
1205 bestXY[1] = -1;
Jeff Sharkey70864282009-04-07 21:08:40 -07001206 }
Adam Cohend41fbf52012-02-16 23:53:59 -08001207 recycleTempRects(validRegions);
Adam Cohenc0dcf592011-06-01 15:30:43 -07001208 return bestXY;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001209 }
Winson Chungaafa03c2010-06-11 17:34:16 -07001210
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001211 /**
Adam Cohen482ed822012-03-02 14:15:13 -08001212 * Find a vacant area that will fit the given bounds nearest the requested
1213 * cell location, and will also weigh in a suggested direction vector of the
1214 * desired location. This method computers distance based on unit grid distances,
1215 * not pixel distances.
1216 *
Adam Cohen47a876d2012-03-19 13:21:41 -07001217 * @param cellX The X cell nearest to which you want to search for a vacant area.
1218 * @param cellY The Y cell nearest which you want to search for a vacant area.
Adam Cohen482ed822012-03-02 14:15:13 -08001219 * @param spanX Horizontal span of the object.
1220 * @param spanY Vertical span of the object.
Adam Cohen47a876d2012-03-19 13:21:41 -07001221 * @param direction The favored direction in which the views should move from x, y
Sunny Goyal9eba1fd2015-10-16 08:58:57 -07001222 * @param occupied The array which represents which cells in the CellLayout are occupied
Adam Cohen47a876d2012-03-19 13:21:41 -07001223 * @param blockOccupied The array which represents which cells in the specified block (cellX,
Winson Chung5f8afe62013-08-12 16:19:28 -07001224 * cellY, spanX, spanY) are occupied. This is used when try to move a group of views.
Adam Cohen482ed822012-03-02 14:15:13 -08001225 * @param result Array in which to place the result, or null (in which case a new array will
1226 * be allocated)
1227 * @return The X, Y cell of a vacant area that can contain this object,
1228 * nearest the requested location.
1229 */
1230 private int[] findNearestArea(int cellX, int cellY, int spanX, int spanY, int[] direction,
Adam Cohen47a876d2012-03-19 13:21:41 -07001231 boolean[][] occupied, boolean blockOccupied[][], int[] result) {
Adam Cohen482ed822012-03-02 14:15:13 -08001232 // Keep track of best-scoring drop area
1233 final int[] bestXY = result != null ? result : new int[2];
1234 float bestDistance = Float.MAX_VALUE;
1235 int bestDirectionScore = Integer.MIN_VALUE;
1236
1237 final int countX = mCountX;
1238 final int countY = mCountY;
1239
1240 for (int y = 0; y < countY - (spanY - 1); y++) {
1241 inner:
1242 for (int x = 0; x < countX - (spanX - 1); x++) {
1243 // First, let's see if this thing fits anywhere
1244 for (int i = 0; i < spanX; i++) {
1245 for (int j = 0; j < spanY; j++) {
Adam Cohen47a876d2012-03-19 13:21:41 -07001246 if (occupied[x + i][y + j] && (blockOccupied == null || blockOccupied[i][j])) {
Adam Cohen482ed822012-03-02 14:15:13 -08001247 continue inner;
1248 }
1249 }
1250 }
1251
Sunny Goyalf7a29e82015-04-24 15:20:43 -07001252 float distance = (float) Math.hypot(x - cellX, y - cellY);
Adam Cohen482ed822012-03-02 14:15:13 -08001253 int[] curDirection = mTmpPoint;
Adam Cohen47a876d2012-03-19 13:21:41 -07001254 computeDirectionVector(x - cellX, y - cellY, curDirection);
1255 // The direction score is just the dot product of the two candidate direction
1256 // and that passed in.
Adam Cohen482ed822012-03-02 14:15:13 -08001257 int curDirectionScore = direction[0] * curDirection[0] +
1258 direction[1] * curDirection[1];
Sunny Goyal8f90dcf2016-08-18 15:01:11 -07001259 if (Float.compare(distance, bestDistance) < 0 ||
1260 (Float.compare(distance, bestDistance) == 0
1261 && curDirectionScore > bestDirectionScore)) {
Adam Cohen482ed822012-03-02 14:15:13 -08001262 bestDistance = distance;
1263 bestDirectionScore = curDirectionScore;
1264 bestXY[0] = x;
1265 bestXY[1] = y;
1266 }
1267 }
1268 }
1269
1270 // Return -1, -1 if no suitable location found
1271 if (bestDistance == Float.MAX_VALUE) {
1272 bestXY[0] = -1;
1273 bestXY[1] = -1;
1274 }
1275 return bestXY;
1276 }
1277
1278 private boolean addViewToTempLocation(View v, Rect rectOccupiedByPotentialDrop,
Adam Cohen8baab352012-03-20 17:39:21 -07001279 int[] direction, ItemConfiguration currentState) {
1280 CellAndSpan c = currentState.map.get(v);
Adam Cohen482ed822012-03-02 14:15:13 -08001281 boolean success = false;
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001282 mTmpOccupied.markCells(c, false);
1283 mTmpOccupied.markCells(rectOccupiedByPotentialDrop, true);
Adam Cohen482ed822012-03-02 14:15:13 -08001284
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001285 findNearestArea(c.cellX, c.cellY, c.spanX, c.spanY, direction,
1286 mTmpOccupied.cells, null, mTempLocation);
Adam Cohen482ed822012-03-02 14:15:13 -08001287
1288 if (mTempLocation[0] >= 0 && mTempLocation[1] >= 0) {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001289 c.cellX = mTempLocation[0];
1290 c.cellY = mTempLocation[1];
Adam Cohen482ed822012-03-02 14:15:13 -08001291 success = true;
Adam Cohen482ed822012-03-02 14:15:13 -08001292 }
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001293 mTmpOccupied.markCells(c, true);
Adam Cohen482ed822012-03-02 14:15:13 -08001294 return success;
1295 }
1296
Adam Cohenf3900c22012-11-16 18:28:11 -08001297 /**
1298 * This helper class defines a cluster of views. It helps with defining complex edges
1299 * of the cluster and determining how those edges interact with other views. The edges
1300 * essentially define a fine-grained boundary around the cluster of views -- like a more
1301 * precise version of a bounding box.
1302 */
1303 private class ViewCluster {
Sunny Goyal6dc98b92016-04-03 15:25:29 -07001304 final static int LEFT = 1 << 0;
1305 final static int TOP = 1 << 1;
1306 final static int RIGHT = 1 << 2;
1307 final static int BOTTOM = 1 << 3;
Adam Cohen47a876d2012-03-19 13:21:41 -07001308
Rajeev Kumar9962dbe2017-06-12 12:16:20 -07001309 final ArrayList<View> views;
1310 final ItemConfiguration config;
1311 final Rect boundingRect = new Rect();
Adam Cohen47a876d2012-03-19 13:21:41 -07001312
Rajeev Kumar9962dbe2017-06-12 12:16:20 -07001313 final int[] leftEdge = new int[mCountY];
1314 final int[] rightEdge = new int[mCountY];
1315 final int[] topEdge = new int[mCountX];
1316 final int[] bottomEdge = new int[mCountX];
Sunny Goyal6dc98b92016-04-03 15:25:29 -07001317 int dirtyEdges;
1318 boolean boundingRectDirty;
Adam Cohenf3900c22012-11-16 18:28:11 -08001319
1320 @SuppressWarnings("unchecked")
1321 public ViewCluster(ArrayList<View> views, ItemConfiguration config) {
1322 this.views = (ArrayList<View>) views.clone();
1323 this.config = config;
1324 resetEdges();
Adam Cohen47a876d2012-03-19 13:21:41 -07001325 }
1326
Adam Cohenf3900c22012-11-16 18:28:11 -08001327 void resetEdges() {
1328 for (int i = 0; i < mCountX; i++) {
1329 topEdge[i] = -1;
1330 bottomEdge[i] = -1;
1331 }
1332 for (int i = 0; i < mCountY; i++) {
1333 leftEdge[i] = -1;
1334 rightEdge[i] = -1;
1335 }
Sunny Goyal6dc98b92016-04-03 15:25:29 -07001336 dirtyEdges = LEFT | TOP | RIGHT | BOTTOM;
Adam Cohenf3900c22012-11-16 18:28:11 -08001337 boundingRectDirty = true;
1338 }
Adam Cohen47a876d2012-03-19 13:21:41 -07001339
Sunny Goyal6dc98b92016-04-03 15:25:29 -07001340 void computeEdge(int which) {
Adam Cohenf3900c22012-11-16 18:28:11 -08001341 int count = views.size();
1342 for (int i = 0; i < count; i++) {
1343 CellAndSpan cs = config.map.get(views.get(i));
1344 switch (which) {
1345 case LEFT:
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001346 int left = cs.cellX;
1347 for (int j = cs.cellY; j < cs.cellY + cs.spanY; j++) {
Sunny Goyal6dc98b92016-04-03 15:25:29 -07001348 if (left < leftEdge[j] || leftEdge[j] < 0) {
1349 leftEdge[j] = left;
Adam Cohena56dc102012-07-13 13:41:42 -07001350 }
Adam Cohen47a876d2012-03-19 13:21:41 -07001351 }
Adam Cohenf3900c22012-11-16 18:28:11 -08001352 break;
1353 case RIGHT:
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001354 int right = cs.cellX + cs.spanX;
1355 for (int j = cs.cellY; j < cs.cellY + cs.spanY; j++) {
Sunny Goyal6dc98b92016-04-03 15:25:29 -07001356 if (right > rightEdge[j]) {
1357 rightEdge[j] = right;
Adam Cohenf3900c22012-11-16 18:28:11 -08001358 }
1359 }
1360 break;
1361 case TOP:
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001362 int top = cs.cellY;
1363 for (int j = cs.cellX; j < cs.cellX + cs.spanX; j++) {
Sunny Goyal6dc98b92016-04-03 15:25:29 -07001364 if (top < topEdge[j] || topEdge[j] < 0) {
1365 topEdge[j] = top;
Adam Cohenf3900c22012-11-16 18:28:11 -08001366 }
1367 }
1368 break;
1369 case BOTTOM:
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001370 int bottom = cs.cellY + cs.spanY;
1371 for (int j = cs.cellX; j < cs.cellX + cs.spanX; j++) {
Sunny Goyal6dc98b92016-04-03 15:25:29 -07001372 if (bottom > bottomEdge[j]) {
1373 bottomEdge[j] = bottom;
Adam Cohenf3900c22012-11-16 18:28:11 -08001374 }
1375 }
1376 break;
Adam Cohen47a876d2012-03-19 13:21:41 -07001377 }
1378 }
1379 }
Adam Cohenf3900c22012-11-16 18:28:11 -08001380
1381 boolean isViewTouchingEdge(View v, int whichEdge) {
1382 CellAndSpan cs = config.map.get(v);
1383
Sunny Goyal6dc98b92016-04-03 15:25:29 -07001384 if ((dirtyEdges & whichEdge) == whichEdge) {
1385 computeEdge(whichEdge);
1386 dirtyEdges &= ~whichEdge;
1387 }
Adam Cohenf3900c22012-11-16 18:28:11 -08001388
1389 switch (whichEdge) {
1390 case LEFT:
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001391 for (int i = cs.cellY; i < cs.cellY + cs.spanY; i++) {
1392 if (leftEdge[i] == cs.cellX + cs.spanX) {
Adam Cohenf3900c22012-11-16 18:28:11 -08001393 return true;
1394 }
1395 }
1396 break;
1397 case RIGHT:
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001398 for (int i = cs.cellY; i < cs.cellY + cs.spanY; i++) {
1399 if (rightEdge[i] == cs.cellX) {
Adam Cohenf3900c22012-11-16 18:28:11 -08001400 return true;
1401 }
1402 }
1403 break;
1404 case TOP:
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001405 for (int i = cs.cellX; i < cs.cellX + cs.spanX; i++) {
1406 if (topEdge[i] == cs.cellY + cs.spanY) {
Adam Cohenf3900c22012-11-16 18:28:11 -08001407 return true;
1408 }
1409 }
1410 break;
1411 case BOTTOM:
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001412 for (int i = cs.cellX; i < cs.cellX + cs.spanX; i++) {
1413 if (bottomEdge[i] == cs.cellY) {
Adam Cohenf3900c22012-11-16 18:28:11 -08001414 return true;
1415 }
1416 }
1417 break;
1418 }
1419 return false;
1420 }
1421
1422 void shift(int whichEdge, int delta) {
1423 for (View v: views) {
1424 CellAndSpan c = config.map.get(v);
1425 switch (whichEdge) {
1426 case LEFT:
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001427 c.cellX -= delta;
Adam Cohenf3900c22012-11-16 18:28:11 -08001428 break;
1429 case RIGHT:
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001430 c.cellX += delta;
Adam Cohenf3900c22012-11-16 18:28:11 -08001431 break;
1432 case TOP:
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001433 c.cellY -= delta;
Adam Cohenf3900c22012-11-16 18:28:11 -08001434 break;
1435 case BOTTOM:
1436 default:
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001437 c.cellY += delta;
Adam Cohenf3900c22012-11-16 18:28:11 -08001438 break;
1439 }
1440 }
1441 resetEdges();
1442 }
1443
1444 public void addView(View v) {
1445 views.add(v);
1446 resetEdges();
1447 }
1448
1449 public Rect getBoundingRect() {
1450 if (boundingRectDirty) {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001451 config.getBoundingRectForViews(views, boundingRect);
Adam Cohenf3900c22012-11-16 18:28:11 -08001452 }
1453 return boundingRect;
1454 }
1455
Rajeev Kumar9962dbe2017-06-12 12:16:20 -07001456 final PositionComparator comparator = new PositionComparator();
Adam Cohenf3900c22012-11-16 18:28:11 -08001457 class PositionComparator implements Comparator<View> {
1458 int whichEdge = 0;
1459 public int compare(View left, View right) {
1460 CellAndSpan l = config.map.get(left);
1461 CellAndSpan r = config.map.get(right);
1462 switch (whichEdge) {
1463 case LEFT:
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001464 return (r.cellX + r.spanX) - (l.cellX + l.spanX);
Adam Cohenf3900c22012-11-16 18:28:11 -08001465 case RIGHT:
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001466 return l.cellX - r.cellX;
Adam Cohenf3900c22012-11-16 18:28:11 -08001467 case TOP:
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001468 return (r.cellY + r.spanY) - (l.cellY + l.spanY);
Adam Cohenf3900c22012-11-16 18:28:11 -08001469 case BOTTOM:
1470 default:
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001471 return l.cellY - r.cellY;
Adam Cohenf3900c22012-11-16 18:28:11 -08001472 }
1473 }
1474 }
1475
1476 public void sortConfigurationForEdgePush(int edge) {
1477 comparator.whichEdge = edge;
1478 Collections.sort(config.sortedViews, comparator);
1479 }
Adam Cohen47a876d2012-03-19 13:21:41 -07001480 }
1481
Adam Cohenf3900c22012-11-16 18:28:11 -08001482 private boolean pushViewsToTempLocation(ArrayList<View> views, Rect rectOccupiedByPotentialDrop,
1483 int[] direction, View dragView, ItemConfiguration currentState) {
Adam Cohene0489502012-08-27 15:18:53 -07001484
Adam Cohenf3900c22012-11-16 18:28:11 -08001485 ViewCluster cluster = new ViewCluster(views, currentState);
1486 Rect clusterRect = cluster.getBoundingRect();
1487 int whichEdge;
1488 int pushDistance;
1489 boolean fail = false;
1490
1491 // Determine the edge of the cluster that will be leading the push and how far
1492 // the cluster must be shifted.
1493 if (direction[0] < 0) {
1494 whichEdge = ViewCluster.LEFT;
1495 pushDistance = clusterRect.right - rectOccupiedByPotentialDrop.left;
Adam Cohene0489502012-08-27 15:18:53 -07001496 } else if (direction[0] > 0) {
Adam Cohenf3900c22012-11-16 18:28:11 -08001497 whichEdge = ViewCluster.RIGHT;
1498 pushDistance = rectOccupiedByPotentialDrop.right - clusterRect.left;
1499 } else if (direction[1] < 0) {
1500 whichEdge = ViewCluster.TOP;
1501 pushDistance = clusterRect.bottom - rectOccupiedByPotentialDrop.top;
1502 } else {
1503 whichEdge = ViewCluster.BOTTOM;
1504 pushDistance = rectOccupiedByPotentialDrop.bottom - clusterRect.top;
Adam Cohene0489502012-08-27 15:18:53 -07001505 }
1506
Adam Cohenf3900c22012-11-16 18:28:11 -08001507 // Break early for invalid push distance.
1508 if (pushDistance <= 0) {
1509 return false;
Adam Cohene0489502012-08-27 15:18:53 -07001510 }
Adam Cohenf3900c22012-11-16 18:28:11 -08001511
1512 // Mark the occupied state as false for the group of views we want to move.
1513 for (View v: views) {
1514 CellAndSpan c = currentState.map.get(v);
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001515 mTmpOccupied.markCells(c, false);
Adam Cohenf3900c22012-11-16 18:28:11 -08001516 }
1517
1518 // We save the current configuration -- if we fail to find a solution we will revert
1519 // to the initial state. The process of finding a solution modifies the configuration
1520 // in place, hence the need for revert in the failure case.
1521 currentState.save();
1522
1523 // The pushing algorithm is simplified by considering the views in the order in which
1524 // they would be pushed by the cluster. For example, if the cluster is leading with its
1525 // left edge, we consider sort the views by their right edge, from right to left.
1526 cluster.sortConfigurationForEdgePush(whichEdge);
1527
1528 while (pushDistance > 0 && !fail) {
1529 for (View v: currentState.sortedViews) {
1530 // For each view that isn't in the cluster, we see if the leading edge of the
1531 // cluster is contacting the edge of that view. If so, we add that view to the
1532 // cluster.
1533 if (!cluster.views.contains(v) && v != dragView) {
1534 if (cluster.isViewTouchingEdge(v, whichEdge)) {
1535 LayoutParams lp = (LayoutParams) v.getLayoutParams();
1536 if (!lp.canReorder) {
1537 // The push solution includes the all apps button, this is not viable.
1538 fail = true;
1539 break;
1540 }
1541 cluster.addView(v);
1542 CellAndSpan c = currentState.map.get(v);
1543
1544 // Adding view to cluster, mark it as not occupied.
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001545 mTmpOccupied.markCells(c, false);
Adam Cohenf3900c22012-11-16 18:28:11 -08001546 }
1547 }
1548 }
1549 pushDistance--;
1550
1551 // The cluster has been completed, now we move the whole thing over in the appropriate
1552 // direction.
1553 cluster.shift(whichEdge, 1);
1554 }
1555
1556 boolean foundSolution = false;
1557 clusterRect = cluster.getBoundingRect();
1558
1559 // Due to the nature of the algorithm, the only check required to verify a valid solution
1560 // is to ensure that completed shifted cluster lies completely within the cell layout.
1561 if (!fail && clusterRect.left >= 0 && clusterRect.right <= mCountX && clusterRect.top >= 0 &&
1562 clusterRect.bottom <= mCountY) {
1563 foundSolution = true;
1564 } else {
1565 currentState.restore();
1566 }
1567
1568 // In either case, we set the occupied array as marked for the location of the views
1569 for (View v: cluster.views) {
1570 CellAndSpan c = currentState.map.get(v);
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001571 mTmpOccupied.markCells(c, true);
Adam Cohenf3900c22012-11-16 18:28:11 -08001572 }
1573
1574 return foundSolution;
Adam Cohene0489502012-08-27 15:18:53 -07001575 }
1576
Adam Cohen482ed822012-03-02 14:15:13 -08001577 private boolean addViewsToTempLocation(ArrayList<View> views, Rect rectOccupiedByPotentialDrop,
Adam Cohenf3900c22012-11-16 18:28:11 -08001578 int[] direction, View dragView, ItemConfiguration currentState) {
Adam Cohen482ed822012-03-02 14:15:13 -08001579 if (views.size() == 0) return true;
Adam Cohen482ed822012-03-02 14:15:13 -08001580
Adam Cohen8baab352012-03-20 17:39:21 -07001581 boolean success = false;
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001582 Rect boundingRect = new Rect();
Adam Cohen8baab352012-03-20 17:39:21 -07001583 // We construct a rect which represents the entire group of views passed in
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001584 currentState.getBoundingRectForViews(views, boundingRect);
Adam Cohen8baab352012-03-20 17:39:21 -07001585
Adam Cohen8baab352012-03-20 17:39:21 -07001586 // Mark the occupied state as false for the group of views we want to move.
Adam Cohenf3900c22012-11-16 18:28:11 -08001587 for (View v: views) {
Adam Cohen8baab352012-03-20 17:39:21 -07001588 CellAndSpan c = currentState.map.get(v);
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001589 mTmpOccupied.markCells(c, false);
Adam Cohen8baab352012-03-20 17:39:21 -07001590 }
1591
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001592 GridOccupancy blockOccupied = new GridOccupancy(boundingRect.width(), boundingRect.height());
Adam Cohen47a876d2012-03-19 13:21:41 -07001593 int top = boundingRect.top;
1594 int left = boundingRect.left;
Adam Cohen8baab352012-03-20 17:39:21 -07001595 // We mark more precisely which parts of the bounding rect are truly occupied, allowing
Adam Cohena56dc102012-07-13 13:41:42 -07001596 // for interlocking.
Adam Cohenf3900c22012-11-16 18:28:11 -08001597 for (View v: views) {
Adam Cohen8baab352012-03-20 17:39:21 -07001598 CellAndSpan c = currentState.map.get(v);
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001599 blockOccupied.markCells(c.cellX - left, c.cellY - top, c.spanX, c.spanY, true);
Adam Cohen47a876d2012-03-19 13:21:41 -07001600 }
1601
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001602 mTmpOccupied.markCells(rectOccupiedByPotentialDrop, true);
Adam Cohen482ed822012-03-02 14:15:13 -08001603
Adam Cohenf3900c22012-11-16 18:28:11 -08001604 findNearestArea(boundingRect.left, boundingRect.top, boundingRect.width(),
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001605 boundingRect.height(), direction,
1606 mTmpOccupied.cells, blockOccupied.cells, mTempLocation);
Adam Cohen482ed822012-03-02 14:15:13 -08001607
Adam Cohen8baab352012-03-20 17:39:21 -07001608 // If we successfuly found a location by pushing the block of views, we commit it
Adam Cohen482ed822012-03-02 14:15:13 -08001609 if (mTempLocation[0] >= 0 && mTempLocation[1] >= 0) {
Adam Cohen8baab352012-03-20 17:39:21 -07001610 int deltaX = mTempLocation[0] - boundingRect.left;
1611 int deltaY = mTempLocation[1] - boundingRect.top;
Adam Cohenf3900c22012-11-16 18:28:11 -08001612 for (View v: views) {
Adam Cohen8baab352012-03-20 17:39:21 -07001613 CellAndSpan c = currentState.map.get(v);
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001614 c.cellX += deltaX;
1615 c.cellY += deltaY;
Adam Cohen482ed822012-03-02 14:15:13 -08001616 }
1617 success = true;
1618 }
Adam Cohen8baab352012-03-20 17:39:21 -07001619
1620 // In either case, we set the occupied array as marked for the location of the views
Adam Cohenf3900c22012-11-16 18:28:11 -08001621 for (View v: views) {
Adam Cohen8baab352012-03-20 17:39:21 -07001622 CellAndSpan c = currentState.map.get(v);
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001623 mTmpOccupied.markCells(c, true);
Adam Cohen482ed822012-03-02 14:15:13 -08001624 }
1625 return success;
1626 }
1627
Adam Cohen4abc5bd2012-05-29 21:06:03 -07001628 // This method tries to find a reordering solution which satisfies the push mechanic by trying
1629 // to push items in each of the cardinal directions, in an order based on the direction vector
1630 // passed.
1631 private boolean attemptPushInDirection(ArrayList<View> intersectingViews, Rect occupied,
1632 int[] direction, View ignoreView, ItemConfiguration solution) {
1633 if ((Math.abs(direction[0]) + Math.abs(direction[1])) > 1) {
Winson Chung5f8afe62013-08-12 16:19:28 -07001634 // If the direction vector has two non-zero components, we try pushing
Adam Cohen4abc5bd2012-05-29 21:06:03 -07001635 // separately in each of the components.
1636 int temp = direction[1];
1637 direction[1] = 0;
Adam Cohenf3900c22012-11-16 18:28:11 -08001638
1639 if (pushViewsToTempLocation(intersectingViews, occupied, direction,
Adam Cohen4abc5bd2012-05-29 21:06:03 -07001640 ignoreView, solution)) {
1641 return true;
1642 }
1643 direction[1] = temp;
1644 temp = direction[0];
1645 direction[0] = 0;
Adam Cohenf3900c22012-11-16 18:28:11 -08001646
1647 if (pushViewsToTempLocation(intersectingViews, occupied, direction,
Adam Cohen4abc5bd2012-05-29 21:06:03 -07001648 ignoreView, solution)) {
1649 return true;
1650 }
1651 // Revert the direction
1652 direction[0] = temp;
1653
1654 // Now we try pushing in each component of the opposite direction
1655 direction[0] *= -1;
1656 direction[1] *= -1;
1657 temp = direction[1];
1658 direction[1] = 0;
Adam Cohenf3900c22012-11-16 18:28:11 -08001659 if (pushViewsToTempLocation(intersectingViews, occupied, direction,
Adam Cohen4abc5bd2012-05-29 21:06:03 -07001660 ignoreView, solution)) {
1661 return true;
1662 }
1663
1664 direction[1] = temp;
1665 temp = direction[0];
1666 direction[0] = 0;
Adam Cohenf3900c22012-11-16 18:28:11 -08001667 if (pushViewsToTempLocation(intersectingViews, occupied, direction,
Adam Cohen4abc5bd2012-05-29 21:06:03 -07001668 ignoreView, solution)) {
1669 return true;
1670 }
1671 // revert the direction
1672 direction[0] = temp;
1673 direction[0] *= -1;
1674 direction[1] *= -1;
Winson Chung5f8afe62013-08-12 16:19:28 -07001675
Adam Cohen4abc5bd2012-05-29 21:06:03 -07001676 } else {
1677 // If the direction vector has a single non-zero component, we push first in the
1678 // direction of the vector
Adam Cohenf3900c22012-11-16 18:28:11 -08001679 if (pushViewsToTempLocation(intersectingViews, occupied, direction,
Adam Cohen4abc5bd2012-05-29 21:06:03 -07001680 ignoreView, solution)) {
1681 return true;
1682 }
Adam Cohen4abc5bd2012-05-29 21:06:03 -07001683 // Then we try the opposite direction
1684 direction[0] *= -1;
1685 direction[1] *= -1;
Adam Cohenf3900c22012-11-16 18:28:11 -08001686 if (pushViewsToTempLocation(intersectingViews, occupied, direction,
Adam Cohen4abc5bd2012-05-29 21:06:03 -07001687 ignoreView, solution)) {
1688 return true;
1689 }
1690 // Switch the direction back
1691 direction[0] *= -1;
1692 direction[1] *= -1;
Winson Chung5f8afe62013-08-12 16:19:28 -07001693
1694 // If we have failed to find a push solution with the above, then we try
Adam Cohen4abc5bd2012-05-29 21:06:03 -07001695 // to find a solution by pushing along the perpendicular axis.
1696
1697 // Swap the components
1698 int temp = direction[1];
1699 direction[1] = direction[0];
1700 direction[0] = temp;
Adam Cohenf3900c22012-11-16 18:28:11 -08001701 if (pushViewsToTempLocation(intersectingViews, occupied, direction,
Adam Cohen4abc5bd2012-05-29 21:06:03 -07001702 ignoreView, solution)) {
1703 return true;
1704 }
1705
1706 // Then we try the opposite direction
1707 direction[0] *= -1;
1708 direction[1] *= -1;
Adam Cohenf3900c22012-11-16 18:28:11 -08001709 if (pushViewsToTempLocation(intersectingViews, occupied, direction,
Adam Cohen4abc5bd2012-05-29 21:06:03 -07001710 ignoreView, solution)) {
1711 return true;
1712 }
1713 // Switch the direction back
1714 direction[0] *= -1;
1715 direction[1] *= -1;
1716
1717 // Swap the components back
1718 temp = direction[1];
1719 direction[1] = direction[0];
1720 direction[0] = temp;
1721 }
1722 return false;
1723 }
1724
Adam Cohen482ed822012-03-02 14:15:13 -08001725 private boolean rearrangementExists(int cellX, int cellY, int spanX, int spanY, int[] direction,
Adam Cohen8baab352012-03-20 17:39:21 -07001726 View ignoreView, ItemConfiguration solution) {
Winson Chunge3e03bc2012-05-01 15:10:11 -07001727 // Return early if get invalid cell positions
1728 if (cellX < 0 || cellY < 0) return false;
Adam Cohen482ed822012-03-02 14:15:13 -08001729
Adam Cohen8baab352012-03-20 17:39:21 -07001730 mIntersectingViews.clear();
Adam Cohen482ed822012-03-02 14:15:13 -08001731 mOccupiedRect.set(cellX, cellY, cellX + spanX, cellY + spanY);
Adam Cohen482ed822012-03-02 14:15:13 -08001732
Adam Cohen8baab352012-03-20 17:39:21 -07001733 // Mark the desired location of the view currently being dragged.
Adam Cohen482ed822012-03-02 14:15:13 -08001734 if (ignoreView != null) {
Adam Cohen8baab352012-03-20 17:39:21 -07001735 CellAndSpan c = solution.map.get(ignoreView);
Adam Cohen19f37922012-03-21 11:59:11 -07001736 if (c != null) {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001737 c.cellX = cellX;
1738 c.cellY = cellY;
Adam Cohen19f37922012-03-21 11:59:11 -07001739 }
Adam Cohen482ed822012-03-02 14:15:13 -08001740 }
Adam Cohen482ed822012-03-02 14:15:13 -08001741 Rect r0 = new Rect(cellX, cellY, cellX + spanX, cellY + spanY);
1742 Rect r1 = new Rect();
Adam Cohen8baab352012-03-20 17:39:21 -07001743 for (View child: solution.map.keySet()) {
Adam Cohen482ed822012-03-02 14:15:13 -08001744 if (child == ignoreView) continue;
Adam Cohen8baab352012-03-20 17:39:21 -07001745 CellAndSpan c = solution.map.get(child);
Adam Cohen482ed822012-03-02 14:15:13 -08001746 LayoutParams lp = (LayoutParams) child.getLayoutParams();
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001747 r1.set(c.cellX, c.cellY, c.cellX + c.spanX, c.cellY + c.spanY);
Adam Cohen482ed822012-03-02 14:15:13 -08001748 if (Rect.intersects(r0, r1)) {
1749 if (!lp.canReorder) {
1750 return false;
1751 }
1752 mIntersectingViews.add(child);
1753 }
1754 }
Adam Cohen47a876d2012-03-19 13:21:41 -07001755
Rajeev Kumar9962dbe2017-06-12 12:16:20 -07001756 solution.intersectingViews = new ArrayList<>(mIntersectingViews);
Adam Cohenfa3c58f2013-12-06 16:10:55 -08001757
Winson Chung5f8afe62013-08-12 16:19:28 -07001758 // First we try to find a solution which respects the push mechanic. That is,
Adam Cohen4abc5bd2012-05-29 21:06:03 -07001759 // we try to find a solution such that no displaced item travels through another item
1760 // without also displacing that item.
1761 if (attemptPushInDirection(mIntersectingViews, mOccupiedRect, direction, ignoreView,
Adam Cohen19f37922012-03-21 11:59:11 -07001762 solution)) {
Adam Cohen47a876d2012-03-19 13:21:41 -07001763 return true;
1764 }
Adam Cohen47a876d2012-03-19 13:21:41 -07001765
Adam Cohen4abc5bd2012-05-29 21:06:03 -07001766 // Next we try moving the views as a block, but without requiring the push mechanic.
Adam Cohenf3900c22012-11-16 18:28:11 -08001767 if (addViewsToTempLocation(mIntersectingViews, mOccupiedRect, direction, ignoreView,
Adam Cohen19f37922012-03-21 11:59:11 -07001768 solution)) {
Adam Cohen482ed822012-03-02 14:15:13 -08001769 return true;
1770 }
Adam Cohen47a876d2012-03-19 13:21:41 -07001771
Adam Cohen482ed822012-03-02 14:15:13 -08001772 // Ok, they couldn't move as a block, let's move them individually
1773 for (View v : mIntersectingViews) {
Adam Cohen8baab352012-03-20 17:39:21 -07001774 if (!addViewToTempLocation(v, mOccupiedRect, direction, solution)) {
Adam Cohen482ed822012-03-02 14:15:13 -08001775 return false;
1776 }
1777 }
1778 return true;
1779 }
1780
1781 /*
1782 * Returns a pair (x, y), where x,y are in {-1, 0, 1} corresponding to vector between
1783 * the provided point and the provided cell
1784 */
Adam Cohen47a876d2012-03-19 13:21:41 -07001785 private void computeDirectionVector(float deltaX, float deltaY, int[] result) {
Jon Mirandae96798e2016-12-07 12:10:44 -08001786 double angle = Math.atan(deltaY / deltaX);
Adam Cohen482ed822012-03-02 14:15:13 -08001787
1788 result[0] = 0;
1789 result[1] = 0;
1790 if (Math.abs(Math.cos(angle)) > 0.5f) {
1791 result[0] = (int) Math.signum(deltaX);
1792 }
1793 if (Math.abs(Math.sin(angle)) > 0.5f) {
1794 result[1] = (int) Math.signum(deltaY);
1795 }
1796 }
1797
Sunny Goyalf7a29e82015-04-24 15:20:43 -07001798 private ItemConfiguration findReorderSolution(int pixelX, int pixelY, int minSpanX, int minSpanY,
Adam Cohenfa3c58f2013-12-06 16:10:55 -08001799 int spanX, int spanY, int[] direction, View dragView, boolean decX,
1800 ItemConfiguration solution) {
Adam Cohen8baab352012-03-20 17:39:21 -07001801 // Copy the current state into the solution. This solution will be manipulated as necessary.
1802 copyCurrentStateToSolution(solution, false);
1803 // Copy the current occupied array into the temporary occupied array. This array will be
1804 // manipulated as necessary to find a solution.
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001805 mOccupied.copyTo(mTmpOccupied);
Adam Cohen482ed822012-03-02 14:15:13 -08001806
1807 // We find the nearest cell into which we would place the dragged item, assuming there's
1808 // nothing in its way.
1809 int result[] = new int[2];
1810 result = findNearestArea(pixelX, pixelY, spanX, spanY, result);
1811
Rajeev Kumar9962dbe2017-06-12 12:16:20 -07001812 boolean success;
Adam Cohen482ed822012-03-02 14:15:13 -08001813 // First we try the exact nearest position of the item being dragged,
1814 // we will then want to try to move this around to other neighbouring positions
Adam Cohen8baab352012-03-20 17:39:21 -07001815 success = rearrangementExists(result[0], result[1], spanX, spanY, direction, dragView,
1816 solution);
Adam Cohen482ed822012-03-02 14:15:13 -08001817
1818 if (!success) {
1819 // We try shrinking the widget down to size in an alternating pattern, shrink 1 in
1820 // x, then 1 in y etc.
1821 if (spanX > minSpanX && (minSpanY == spanY || decX)) {
Adam Cohenfa3c58f2013-12-06 16:10:55 -08001822 return findReorderSolution(pixelX, pixelY, minSpanX, minSpanY, spanX - 1, spanY,
1823 direction, dragView, false, solution);
Adam Cohen482ed822012-03-02 14:15:13 -08001824 } else if (spanY > minSpanY) {
Adam Cohenfa3c58f2013-12-06 16:10:55 -08001825 return findReorderSolution(pixelX, pixelY, minSpanX, minSpanY, spanX, spanY - 1,
1826 direction, dragView, true, solution);
Adam Cohen482ed822012-03-02 14:15:13 -08001827 }
1828 solution.isSolution = false;
1829 } else {
1830 solution.isSolution = true;
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001831 solution.cellX = result[0];
1832 solution.cellY = result[1];
1833 solution.spanX = spanX;
1834 solution.spanY = spanY;
Adam Cohen482ed822012-03-02 14:15:13 -08001835 }
1836 return solution;
1837 }
1838
1839 private void copyCurrentStateToSolution(ItemConfiguration solution, boolean temp) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07001840 int childCount = mShortcutsAndWidgets.getChildCount();
Adam Cohen482ed822012-03-02 14:15:13 -08001841 for (int i = 0; i < childCount; i++) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07001842 View child = mShortcutsAndWidgets.getChildAt(i);
Adam Cohen482ed822012-03-02 14:15:13 -08001843 LayoutParams lp = (LayoutParams) child.getLayoutParams();
Adam Cohen8baab352012-03-20 17:39:21 -07001844 CellAndSpan c;
Adam Cohen482ed822012-03-02 14:15:13 -08001845 if (temp) {
Adam Cohen8baab352012-03-20 17:39:21 -07001846 c = new CellAndSpan(lp.tmpCellX, lp.tmpCellY, lp.cellHSpan, lp.cellVSpan);
Adam Cohen482ed822012-03-02 14:15:13 -08001847 } else {
Adam Cohen8baab352012-03-20 17:39:21 -07001848 c = new CellAndSpan(lp.cellX, lp.cellY, lp.cellHSpan, lp.cellVSpan);
Adam Cohen482ed822012-03-02 14:15:13 -08001849 }
Adam Cohenf3900c22012-11-16 18:28:11 -08001850 solution.add(child, c);
Adam Cohen482ed822012-03-02 14:15:13 -08001851 }
1852 }
1853
1854 private void copySolutionToTempState(ItemConfiguration solution, View dragView) {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001855 mTmpOccupied.clear();
Adam Cohen482ed822012-03-02 14:15:13 -08001856
Michael Jurkaa52570f2012-03-20 03:18:20 -07001857 int childCount = mShortcutsAndWidgets.getChildCount();
Adam Cohen482ed822012-03-02 14:15:13 -08001858 for (int i = 0; i < childCount; i++) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07001859 View child = mShortcutsAndWidgets.getChildAt(i);
Adam Cohen482ed822012-03-02 14:15:13 -08001860 if (child == dragView) continue;
1861 LayoutParams lp = (LayoutParams) child.getLayoutParams();
Adam Cohen8baab352012-03-20 17:39:21 -07001862 CellAndSpan c = solution.map.get(child);
1863 if (c != null) {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001864 lp.tmpCellX = c.cellX;
1865 lp.tmpCellY = c.cellY;
Adam Cohen8baab352012-03-20 17:39:21 -07001866 lp.cellHSpan = c.spanX;
1867 lp.cellVSpan = c.spanY;
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001868 mTmpOccupied.markCells(c, true);
Adam Cohen482ed822012-03-02 14:15:13 -08001869 }
1870 }
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001871 mTmpOccupied.markCells(solution, true);
Adam Cohen482ed822012-03-02 14:15:13 -08001872 }
1873
1874 private void animateItemsToSolution(ItemConfiguration solution, View dragView, boolean
1875 commitDragView) {
1876
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001877 GridOccupancy occupied = DESTRUCTIVE_REORDER ? mOccupied : mTmpOccupied;
1878 occupied.clear();
Adam Cohen482ed822012-03-02 14:15:13 -08001879
Michael Jurkaa52570f2012-03-20 03:18:20 -07001880 int childCount = mShortcutsAndWidgets.getChildCount();
Adam Cohen482ed822012-03-02 14:15:13 -08001881 for (int i = 0; i < childCount; i++) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07001882 View child = mShortcutsAndWidgets.getChildAt(i);
Adam Cohen482ed822012-03-02 14:15:13 -08001883 if (child == dragView) continue;
Adam Cohen8baab352012-03-20 17:39:21 -07001884 CellAndSpan c = solution.map.get(child);
1885 if (c != null) {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001886 animateChildToPosition(child, c.cellX, c.cellY, REORDER_ANIMATION_DURATION, 0,
Adam Cohen19f37922012-03-21 11:59:11 -07001887 DESTRUCTIVE_REORDER, false);
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001888 occupied.markCells(c, true);
Adam Cohen482ed822012-03-02 14:15:13 -08001889 }
1890 }
1891 if (commitDragView) {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001892 occupied.markCells(solution, true);
Adam Cohen482ed822012-03-02 14:15:13 -08001893 }
1894 }
1895
Adam Cohenfa3c58f2013-12-06 16:10:55 -08001896
1897 // This method starts or changes the reorder preview animations
1898 private void beginOrAdjustReorderPreviewAnimations(ItemConfiguration solution,
1899 View dragView, int delay, int mode) {
Adam Cohen19f37922012-03-21 11:59:11 -07001900 int childCount = mShortcutsAndWidgets.getChildCount();
Adam Cohen19f37922012-03-21 11:59:11 -07001901 for (int i = 0; i < childCount; i++) {
1902 View child = mShortcutsAndWidgets.getChildAt(i);
1903 if (child == dragView) continue;
1904 CellAndSpan c = solution.map.get(child);
Adam Cohenfa3c58f2013-12-06 16:10:55 -08001905 boolean skip = mode == ReorderPreviewAnimation.MODE_HINT && solution.intersectingViews
1906 != null && !solution.intersectingViews.contains(child);
1907
Adam Cohen19f37922012-03-21 11:59:11 -07001908 LayoutParams lp = (LayoutParams) child.getLayoutParams();
Adam Cohenfa3c58f2013-12-06 16:10:55 -08001909 if (c != null && !skip) {
1910 ReorderPreviewAnimation rha = new ReorderPreviewAnimation(child, mode, lp.cellX,
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001911 lp.cellY, c.cellX, c.cellY, c.spanX, c.spanY);
Adam Cohend024f982012-05-23 18:26:45 -07001912 rha.animate();
Adam Cohen19f37922012-03-21 11:59:11 -07001913 }
1914 }
1915 }
1916
Adam Cohenfa3c58f2013-12-06 16:10:55 -08001917 // Class which represents the reorder preview animations. These animations show that an item is
Adam Cohen19f37922012-03-21 11:59:11 -07001918 // in a temporary state, and hint at where the item will return to.
Adam Cohenfa3c58f2013-12-06 16:10:55 -08001919 class ReorderPreviewAnimation {
Rajeev Kumar9962dbe2017-06-12 12:16:20 -07001920 final View child;
Adam Cohend024f982012-05-23 18:26:45 -07001921 float finalDeltaX;
1922 float finalDeltaY;
1923 float initDeltaX;
1924 float initDeltaY;
Rajeev Kumar9962dbe2017-06-12 12:16:20 -07001925 final float finalScale;
Adam Cohend024f982012-05-23 18:26:45 -07001926 float initScale;
Rajeev Kumar9962dbe2017-06-12 12:16:20 -07001927 final int mode;
Adam Cohenfa3c58f2013-12-06 16:10:55 -08001928 boolean repeating = false;
1929 private static final int PREVIEW_DURATION = 300;
1930 private static final int HINT_DURATION = Workspace.REORDER_TIMEOUT;
1931
Jon Miranda21266912016-12-19 14:12:05 -08001932 private static final float CHILD_DIVIDEND = 4.0f;
1933
Adam Cohenfa3c58f2013-12-06 16:10:55 -08001934 public static final int MODE_HINT = 0;
1935 public static final int MODE_PREVIEW = 1;
1936
Adam Cohene7587d22012-05-24 18:50:02 -07001937 Animator a;
Adam Cohen19f37922012-03-21 11:59:11 -07001938
Adam Cohenfa3c58f2013-12-06 16:10:55 -08001939 public ReorderPreviewAnimation(View child, int mode, int cellX0, int cellY0, int cellX1,
1940 int cellY1, int spanX, int spanY) {
Adam Cohen19f37922012-03-21 11:59:11 -07001941 regionToCenterPoint(cellX0, cellY0, spanX, spanY, mTmpPoint);
1942 final int x0 = mTmpPoint[0];
1943 final int y0 = mTmpPoint[1];
1944 regionToCenterPoint(cellX1, cellY1, spanX, spanY, mTmpPoint);
1945 final int x1 = mTmpPoint[0];
1946 final int y1 = mTmpPoint[1];
1947 final int dX = x1 - x0;
1948 final int dY = y1 - y0;
Jon Miranda21266912016-12-19 14:12:05 -08001949
1950 this.child = child;
1951 this.mode = mode;
1952 setInitialAnimationValues(false);
1953 finalScale = (mChildScale - (CHILD_DIVIDEND / child.getWidth())) * initScale;
1954 finalDeltaX = initDeltaX;
1955 finalDeltaY = initDeltaY;
Adam Cohenfa3c58f2013-12-06 16:10:55 -08001956 int dir = mode == MODE_HINT ? -1 : 1;
Adam Cohen19f37922012-03-21 11:59:11 -07001957 if (dX == dY && dX == 0) {
1958 } else {
1959 if (dY == 0) {
Jon Miranda21266912016-12-19 14:12:05 -08001960 finalDeltaX += - dir * Math.signum(dX) * mReorderPreviewAnimationMagnitude;
Adam Cohen19f37922012-03-21 11:59:11 -07001961 } else if (dX == 0) {
Jon Miranda21266912016-12-19 14:12:05 -08001962 finalDeltaY += - dir * Math.signum(dY) * mReorderPreviewAnimationMagnitude;
Adam Cohen19f37922012-03-21 11:59:11 -07001963 } else {
1964 double angle = Math.atan( (float) (dY) / dX);
Jon Miranda21266912016-12-19 14:12:05 -08001965 finalDeltaX += (int) (- dir * Math.signum(dX) *
Adam Cohenfa3c58f2013-12-06 16:10:55 -08001966 Math.abs(Math.cos(angle) * mReorderPreviewAnimationMagnitude));
Jon Miranda21266912016-12-19 14:12:05 -08001967 finalDeltaY += (int) (- dir * Math.signum(dY) *
Adam Cohenfa3c58f2013-12-06 16:10:55 -08001968 Math.abs(Math.sin(angle) * mReorderPreviewAnimationMagnitude));
Adam Cohen19f37922012-03-21 11:59:11 -07001969 }
1970 }
Jon Miranda21266912016-12-19 14:12:05 -08001971 }
1972
1973 void setInitialAnimationValues(boolean restoreOriginalValues) {
1974 if (restoreOriginalValues) {
1975 if (child instanceof LauncherAppWidgetHostView) {
1976 LauncherAppWidgetHostView lahv = (LauncherAppWidgetHostView) child;
1977 initScale = lahv.getScaleToFit();
1978 initDeltaX = lahv.getTranslationForCentering().x;
1979 initDeltaY = lahv.getTranslationForCentering().y;
1980 } else {
1981 initScale = mChildScale;
1982 initDeltaX = 0;
1983 initDeltaY = 0;
1984 }
1985 } else {
1986 initScale = child.getScaleX();
1987 initDeltaX = child.getTranslationX();
1988 initDeltaY = child.getTranslationY();
1989 }
Adam Cohen19f37922012-03-21 11:59:11 -07001990 }
1991
Adam Cohend024f982012-05-23 18:26:45 -07001992 void animate() {
Jon Miranda21266912016-12-19 14:12:05 -08001993 boolean noMovement = (finalDeltaX == initDeltaX) && (finalDeltaY == initDeltaY);
1994
Adam Cohen19f37922012-03-21 11:59:11 -07001995 if (mShakeAnimators.containsKey(child)) {
Adam Cohenfa3c58f2013-12-06 16:10:55 -08001996 ReorderPreviewAnimation oldAnimation = mShakeAnimators.get(child);
Adam Cohend024f982012-05-23 18:26:45 -07001997 oldAnimation.cancel();
Adam Cohen19f37922012-03-21 11:59:11 -07001998 mShakeAnimators.remove(child);
Jon Miranda21266912016-12-19 14:12:05 -08001999 if (noMovement) {
Adam Cohene7587d22012-05-24 18:50:02 -07002000 completeAnimationImmediately();
2001 return;
2002 }
Adam Cohen19f37922012-03-21 11:59:11 -07002003 }
Jon Miranda21266912016-12-19 14:12:05 -08002004 if (noMovement) {
Adam Cohen19f37922012-03-21 11:59:11 -07002005 return;
2006 }
Jon Mirandacda3bfb2017-02-06 15:54:41 -08002007 ValueAnimator va = LauncherAnimUtils.ofFloat(0f, 1f);
Adam Cohene7587d22012-05-24 18:50:02 -07002008 a = va;
Tony Wickham9e0702f2015-09-02 14:45:39 -07002009
2010 // Animations are disabled in power save mode, causing the repeated animation to jump
2011 // spastically between beginning and end states. Since this looks bad, we don't repeat
2012 // the animation in power save mode.
Tony Wickham112ac952015-11-12 12:31:50 -08002013 if (!Utilities.isPowerSaverOn(getContext())) {
Tony Wickham9e0702f2015-09-02 14:45:39 -07002014 va.setRepeatMode(ValueAnimator.REVERSE);
2015 va.setRepeatCount(ValueAnimator.INFINITE);
2016 }
2017
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002018 va.setDuration(mode == MODE_HINT ? HINT_DURATION : PREVIEW_DURATION);
Adam Cohend024f982012-05-23 18:26:45 -07002019 va.setStartDelay((int) (Math.random() * 60));
Adam Cohen19f37922012-03-21 11:59:11 -07002020 va.addUpdateListener(new AnimatorUpdateListener() {
2021 @Override
2022 public void onAnimationUpdate(ValueAnimator animation) {
Jon Mirandae96798e2016-12-07 12:10:44 -08002023 float r = (Float) animation.getAnimatedValue();
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002024 float r1 = (mode == MODE_HINT && repeating) ? 1.0f : r;
2025 float x = r1 * finalDeltaX + (1 - r1) * initDeltaX;
2026 float y = r1 * finalDeltaY + (1 - r1) * initDeltaY;
Adam Cohen19f37922012-03-21 11:59:11 -07002027 child.setTranslationX(x);
2028 child.setTranslationY(y);
Adam Cohend024f982012-05-23 18:26:45 -07002029 float s = r * finalScale + (1 - r) * initScale;
Brandon Keely50e6e562012-05-08 16:28:49 -07002030 child.setScaleX(s);
2031 child.setScaleY(s);
Adam Cohen19f37922012-03-21 11:59:11 -07002032 }
2033 });
2034 va.addListener(new AnimatorListenerAdapter() {
2035 public void onAnimationRepeat(Animator animation) {
Adam Cohen19f37922012-03-21 11:59:11 -07002036 // We make sure to end only after a full period
Jon Miranda21266912016-12-19 14:12:05 -08002037 setInitialAnimationValues(true);
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002038 repeating = true;
Adam Cohen19f37922012-03-21 11:59:11 -07002039 }
2040 });
Adam Cohen19f37922012-03-21 11:59:11 -07002041 mShakeAnimators.put(child, this);
2042 va.start();
2043 }
2044
Adam Cohend024f982012-05-23 18:26:45 -07002045 private void cancel() {
Adam Cohene7587d22012-05-24 18:50:02 -07002046 if (a != null) {
2047 a.cancel();
2048 }
Adam Cohen19f37922012-03-21 11:59:11 -07002049 }
Adam Cohene7587d22012-05-24 18:50:02 -07002050
Adam Cohen091440a2015-03-18 14:16:05 -07002051 @Thunk void completeAnimationImmediately() {
Adam Cohene7587d22012-05-24 18:50:02 -07002052 if (a != null) {
2053 a.cancel();
2054 }
Brandon Keely50e6e562012-05-08 16:28:49 -07002055
Jon Miranda72dbd912017-01-04 14:03:07 -08002056 setInitialAnimationValues(true);
Sunny Goyal9e76f682017-02-13 12:13:43 -08002057 a = LauncherAnimUtils.ofPropertyValuesHolder(child,
2058 new PropertyListBuilder()
2059 .scale(initScale)
2060 .translationX(initDeltaX)
2061 .translationY(initDeltaY)
2062 .build())
2063 .setDuration(REORDER_ANIMATION_DURATION);
Sunny Goyal5d2fc322015-07-06 22:52:49 -07002064 a.setInterpolator(new android.view.animation.DecelerateInterpolator(1.5f));
2065 a.start();
Brandon Keely50e6e562012-05-08 16:28:49 -07002066 }
Adam Cohen19f37922012-03-21 11:59:11 -07002067 }
2068
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002069 private void completeAndClearReorderPreviewAnimations() {
2070 for (ReorderPreviewAnimation a: mShakeAnimators.values()) {
Brandon Keely50e6e562012-05-08 16:28:49 -07002071 a.completeAnimationImmediately();
Adam Cohen19f37922012-03-21 11:59:11 -07002072 }
2073 mShakeAnimators.clear();
2074 }
2075
Adam Cohen482ed822012-03-02 14:15:13 -08002076 private void commitTempPlacement() {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07002077 mTmpOccupied.copyTo(mOccupied);
Sunny Goyalaa8ef112015-06-12 20:04:41 -07002078
2079 long screenId = mLauncher.getWorkspace().getIdForScreen(this);
2080 int container = Favorites.CONTAINER_DESKTOP;
2081
Sunny Goyalc13403c2016-11-18 23:44:48 -08002082 if (mContainerType == HOTSEAT) {
Sunny Goyalaa8ef112015-06-12 20:04:41 -07002083 screenId = -1;
2084 container = Favorites.CONTAINER_HOTSEAT;
2085 }
2086
Michael Jurkaa52570f2012-03-20 03:18:20 -07002087 int childCount = mShortcutsAndWidgets.getChildCount();
Adam Cohen482ed822012-03-02 14:15:13 -08002088 for (int i = 0; i < childCount; i++) {
Adam Cohenea889a22012-03-27 16:45:39 -07002089 View child = mShortcutsAndWidgets.getChildAt(i);
2090 LayoutParams lp = (LayoutParams) child.getLayoutParams();
2091 ItemInfo info = (ItemInfo) child.getTag();
Adam Cohen2acce882012-03-28 19:03:19 -07002092 // We do a null check here because the item info can be null in the case of the
2093 // AllApps button in the hotseat.
2094 if (info != null) {
Sunny Goyalaa8ef112015-06-12 20:04:41 -07002095 final boolean requiresDbUpdate = (info.cellX != lp.tmpCellX
2096 || info.cellY != lp.tmpCellY || info.spanX != lp.cellHSpan
2097 || info.spanY != lp.cellVSpan);
2098
Adam Cohen2acce882012-03-28 19:03:19 -07002099 info.cellX = lp.cellX = lp.tmpCellX;
2100 info.cellY = lp.cellY = lp.tmpCellY;
Adam Cohenbebf0422012-04-11 18:06:28 -07002101 info.spanX = lp.cellHSpan;
2102 info.spanY = lp.cellVSpan;
Sunny Goyalaa8ef112015-06-12 20:04:41 -07002103
2104 if (requiresDbUpdate) {
Sunny Goyal43bf11d2017-02-02 13:52:53 -08002105 mLauncher.getModelWriter().modifyItemInDatabase(info, container, screenId,
Sunny Goyalaa8ef112015-06-12 20:04:41 -07002106 info.cellX, info.cellY, info.spanX, info.spanY);
2107 }
Adam Cohen2acce882012-03-28 19:03:19 -07002108 }
Adam Cohen482ed822012-03-02 14:15:13 -08002109 }
2110 }
2111
Sunny Goyalf7a29e82015-04-24 15:20:43 -07002112 private void setUseTempCoords(boolean useTempCoords) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07002113 int childCount = mShortcutsAndWidgets.getChildCount();
Adam Cohen482ed822012-03-02 14:15:13 -08002114 for (int i = 0; i < childCount; i++) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07002115 LayoutParams lp = (LayoutParams) mShortcutsAndWidgets.getChildAt(i).getLayoutParams();
Adam Cohen482ed822012-03-02 14:15:13 -08002116 lp.useTmpCoords = useTempCoords;
2117 }
2118 }
2119
Sunny Goyalf7a29e82015-04-24 15:20:43 -07002120 private ItemConfiguration findConfigurationNoShuffle(int pixelX, int pixelY, int minSpanX, int minSpanY,
Adam Cohen482ed822012-03-02 14:15:13 -08002121 int spanX, int spanY, View dragView, ItemConfiguration solution) {
2122 int[] result = new int[2];
2123 int[] resultSpan = new int[2];
Sunny Goyalf7a29e82015-04-24 15:20:43 -07002124 findNearestVacantArea(pixelX, pixelY, minSpanX, minSpanY, spanX, spanY, result,
Adam Cohen482ed822012-03-02 14:15:13 -08002125 resultSpan);
2126 if (result[0] >= 0 && result[1] >= 0) {
2127 copyCurrentStateToSolution(solution, false);
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07002128 solution.cellX = result[0];
2129 solution.cellY = result[1];
2130 solution.spanX = resultSpan[0];
2131 solution.spanY = resultSpan[1];
Adam Cohen482ed822012-03-02 14:15:13 -08002132 solution.isSolution = true;
2133 } else {
2134 solution.isSolution = false;
2135 }
2136 return solution;
2137 }
2138
Adam Cohen19f37922012-03-21 11:59:11 -07002139 /* This seems like it should be obvious and straight-forward, but when the direction vector
2140 needs to match with the notion of the dragView pushing other views, we have to employ
2141 a slightly more subtle notion of the direction vector. The question is what two points is
2142 the vector between? The center of the dragView and its desired destination? Not quite, as
2143 this doesn't necessarily coincide with the interaction of the dragView and items occupying
2144 those cells. Instead we use some heuristics to often lock the vector to up, down, left
2145 or right, which helps make pushing feel right.
2146 */
2147 private void getDirectionVectorForDrop(int dragViewCenterX, int dragViewCenterY, int spanX,
2148 int spanY, View dragView, int[] resultDirection) {
2149 int[] targetDestination = new int[2];
2150
2151 findNearestArea(dragViewCenterX, dragViewCenterY, spanX, spanY, targetDestination);
2152 Rect dragRect = new Rect();
2153 regionToRect(targetDestination[0], targetDestination[1], spanX, spanY, dragRect);
2154 dragRect.offset(dragViewCenterX - dragRect.centerX(), dragViewCenterY - dragRect.centerY());
2155
2156 Rect dropRegionRect = new Rect();
2157 getViewsIntersectingRegion(targetDestination[0], targetDestination[1], spanX, spanY,
2158 dragView, dropRegionRect, mIntersectingViews);
2159
2160 int dropRegionSpanX = dropRegionRect.width();
2161 int dropRegionSpanY = dropRegionRect.height();
2162
2163 regionToRect(dropRegionRect.left, dropRegionRect.top, dropRegionRect.width(),
2164 dropRegionRect.height(), dropRegionRect);
2165
2166 int deltaX = (dropRegionRect.centerX() - dragViewCenterX) / spanX;
2167 int deltaY = (dropRegionRect.centerY() - dragViewCenterY) / spanY;
2168
2169 if (dropRegionSpanX == mCountX || spanX == mCountX) {
2170 deltaX = 0;
2171 }
2172 if (dropRegionSpanY == mCountY || spanY == mCountY) {
2173 deltaY = 0;
2174 }
2175
2176 if (deltaX == 0 && deltaY == 0) {
2177 // No idea what to do, give a random direction.
2178 resultDirection[0] = 1;
2179 resultDirection[1] = 0;
2180 } else {
2181 computeDirectionVector(deltaX, deltaY, resultDirection);
2182 }
2183 }
2184
2185 // For a given cell and span, fetch the set of views intersecting the region.
2186 private void getViewsIntersectingRegion(int cellX, int cellY, int spanX, int spanY,
2187 View dragView, Rect boundingRect, ArrayList<View> intersectingViews) {
2188 if (boundingRect != null) {
2189 boundingRect.set(cellX, cellY, cellX + spanX, cellY + spanY);
2190 }
2191 intersectingViews.clear();
2192 Rect r0 = new Rect(cellX, cellY, cellX + spanX, cellY + spanY);
2193 Rect r1 = new Rect();
2194 final int count = mShortcutsAndWidgets.getChildCount();
2195 for (int i = 0; i < count; i++) {
2196 View child = mShortcutsAndWidgets.getChildAt(i);
2197 if (child == dragView) continue;
2198 LayoutParams lp = (LayoutParams) child.getLayoutParams();
2199 r1.set(lp.cellX, lp.cellY, lp.cellX + lp.cellHSpan, lp.cellY + lp.cellVSpan);
2200 if (Rect.intersects(r0, r1)) {
2201 mIntersectingViews.add(child);
2202 if (boundingRect != null) {
2203 boundingRect.union(r1);
2204 }
2205 }
2206 }
2207 }
2208
2209 boolean isNearestDropLocationOccupied(int pixelX, int pixelY, int spanX, int spanY,
2210 View dragView, int[] result) {
2211 result = findNearestArea(pixelX, pixelY, spanX, spanY, result);
2212 getViewsIntersectingRegion(result[0], result[1], spanX, spanY, dragView, null,
2213 mIntersectingViews);
2214 return !mIntersectingViews.isEmpty();
2215 }
2216
2217 void revertTempState() {
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002218 completeAndClearReorderPreviewAnimations();
2219 if (isItemPlacementDirty() && !DESTRUCTIVE_REORDER) {
2220 final int count = mShortcutsAndWidgets.getChildCount();
2221 for (int i = 0; i < count; i++) {
2222 View child = mShortcutsAndWidgets.getChildAt(i);
2223 LayoutParams lp = (LayoutParams) child.getLayoutParams();
2224 if (lp.tmpCellX != lp.cellX || lp.tmpCellY != lp.cellY) {
2225 lp.tmpCellX = lp.cellX;
2226 lp.tmpCellY = lp.cellY;
2227 animateChildToPosition(child, lp.cellX, lp.cellY, REORDER_ANIMATION_DURATION,
2228 0, false, false);
2229 }
Adam Cohen19f37922012-03-21 11:59:11 -07002230 }
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002231 setItemPlacementDirty(false);
Adam Cohen19f37922012-03-21 11:59:11 -07002232 }
Adam Cohen19f37922012-03-21 11:59:11 -07002233 }
2234
Adam Cohenbebf0422012-04-11 18:06:28 -07002235 boolean createAreaForResize(int cellX, int cellY, int spanX, int spanY,
2236 View dragView, int[] direction, boolean commit) {
2237 int[] pixelXY = new int[2];
2238 regionToCenterPoint(cellX, cellY, spanX, spanY, pixelXY);
2239
2240 // First we determine if things have moved enough to cause a different layout
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002241 ItemConfiguration swapSolution = findReorderSolution(pixelXY[0], pixelXY[1], spanX, spanY,
Adam Cohenbebf0422012-04-11 18:06:28 -07002242 spanX, spanY, direction, dragView, true, new ItemConfiguration());
2243
2244 setUseTempCoords(true);
2245 if (swapSolution != null && swapSolution.isSolution) {
2246 // If we're just testing for a possible location (MODE_ACCEPT_DROP), we don't bother
2247 // committing anything or animating anything as we just want to determine if a solution
2248 // exists
2249 copySolutionToTempState(swapSolution, dragView);
2250 setItemPlacementDirty(true);
2251 animateItemsToSolution(swapSolution, dragView, commit);
2252
2253 if (commit) {
2254 commitTempPlacement();
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002255 completeAndClearReorderPreviewAnimations();
Adam Cohenbebf0422012-04-11 18:06:28 -07002256 setItemPlacementDirty(false);
2257 } else {
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002258 beginOrAdjustReorderPreviewAnimations(swapSolution, dragView,
2259 REORDER_ANIMATION_DURATION, ReorderPreviewAnimation.MODE_PREVIEW);
Adam Cohenbebf0422012-04-11 18:06:28 -07002260 }
2261 mShortcutsAndWidgets.requestLayout();
2262 }
2263 return swapSolution.isSolution;
2264 }
2265
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002266 int[] performReorder(int pixelX, int pixelY, int minSpanX, int minSpanY, int spanX, int spanY,
Adam Cohen482ed822012-03-02 14:15:13 -08002267 View dragView, int[] result, int resultSpan[], int mode) {
Adam Cohen482ed822012-03-02 14:15:13 -08002268 // First we determine if things have moved enough to cause a different layout
Adam Cohen47a876d2012-03-19 13:21:41 -07002269 result = findNearestArea(pixelX, pixelY, spanX, spanY, result);
Adam Cohen482ed822012-03-02 14:15:13 -08002270
2271 if (resultSpan == null) {
2272 resultSpan = new int[2];
2273 }
2274
Adam Cohen19f37922012-03-21 11:59:11 -07002275 // When we are checking drop validity or actually dropping, we don't recompute the
2276 // direction vector, since we want the solution to match the preview, and it's possible
2277 // that the exact position of the item has changed to result in a new reordering outcome.
Adam Cohenb209e632012-03-27 17:09:36 -07002278 if ((mode == MODE_ON_DROP || mode == MODE_ON_DROP_EXTERNAL || mode == MODE_ACCEPT_DROP)
2279 && mPreviousReorderDirection[0] != INVALID_DIRECTION) {
Adam Cohen19f37922012-03-21 11:59:11 -07002280 mDirectionVector[0] = mPreviousReorderDirection[0];
2281 mDirectionVector[1] = mPreviousReorderDirection[1];
2282 // We reset this vector after drop
Adam Cohenb209e632012-03-27 17:09:36 -07002283 if (mode == MODE_ON_DROP || mode == MODE_ON_DROP_EXTERNAL) {
2284 mPreviousReorderDirection[0] = INVALID_DIRECTION;
2285 mPreviousReorderDirection[1] = INVALID_DIRECTION;
Adam Cohen19f37922012-03-21 11:59:11 -07002286 }
2287 } else {
2288 getDirectionVectorForDrop(pixelX, pixelY, spanX, spanY, dragView, mDirectionVector);
2289 mPreviousReorderDirection[0] = mDirectionVector[0];
2290 mPreviousReorderDirection[1] = mDirectionVector[1];
2291 }
2292
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002293 // Find a solution involving pushing / displacing any items in the way
2294 ItemConfiguration swapSolution = findReorderSolution(pixelX, pixelY, minSpanX, minSpanY,
Adam Cohen482ed822012-03-02 14:15:13 -08002295 spanX, spanY, mDirectionVector, dragView, true, new ItemConfiguration());
2296
2297 // We attempt the approach which doesn't shuffle views at all
2298 ItemConfiguration noShuffleSolution = findConfigurationNoShuffle(pixelX, pixelY, minSpanX,
2299 minSpanY, spanX, spanY, dragView, new ItemConfiguration());
2300
2301 ItemConfiguration finalSolution = null;
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002302
2303 // If the reorder solution requires resizing (shrinking) the item being dropped, we instead
2304 // favor a solution in which the item is not resized, but
Adam Cohen482ed822012-03-02 14:15:13 -08002305 if (swapSolution.isSolution && swapSolution.area() >= noShuffleSolution.area()) {
2306 finalSolution = swapSolution;
2307 } else if (noShuffleSolution.isSolution) {
2308 finalSolution = noShuffleSolution;
2309 }
2310
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002311 if (mode == MODE_SHOW_REORDER_HINT) {
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002312 if (finalSolution != null) {
Adam Cohenfe692872013-12-11 14:47:23 -08002313 beginOrAdjustReorderPreviewAnimations(finalSolution, dragView, 0,
2314 ReorderPreviewAnimation.MODE_HINT);
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07002315 result[0] = finalSolution.cellX;
2316 result[1] = finalSolution.cellY;
2317 resultSpan[0] = finalSolution.spanX;
2318 resultSpan[1] = finalSolution.spanY;
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002319 } else {
2320 result[0] = result[1] = resultSpan[0] = resultSpan[1] = -1;
2321 }
2322 return result;
2323 }
2324
Adam Cohen482ed822012-03-02 14:15:13 -08002325 boolean foundSolution = true;
2326 if (!DESTRUCTIVE_REORDER) {
2327 setUseTempCoords(true);
2328 }
2329
2330 if (finalSolution != null) {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07002331 result[0] = finalSolution.cellX;
2332 result[1] = finalSolution.cellY;
2333 resultSpan[0] = finalSolution.spanX;
2334 resultSpan[1] = finalSolution.spanY;
Adam Cohen482ed822012-03-02 14:15:13 -08002335
2336 // If we're just testing for a possible location (MODE_ACCEPT_DROP), we don't bother
2337 // committing anything or animating anything as we just want to determine if a solution
2338 // exists
2339 if (mode == MODE_DRAG_OVER || mode == MODE_ON_DROP || mode == MODE_ON_DROP_EXTERNAL) {
2340 if (!DESTRUCTIVE_REORDER) {
2341 copySolutionToTempState(finalSolution, dragView);
2342 }
2343 setItemPlacementDirty(true);
2344 animateItemsToSolution(finalSolution, dragView, mode == MODE_ON_DROP);
2345
Adam Cohen19f37922012-03-21 11:59:11 -07002346 if (!DESTRUCTIVE_REORDER &&
2347 (mode == MODE_ON_DROP || mode == MODE_ON_DROP_EXTERNAL)) {
Adam Cohen482ed822012-03-02 14:15:13 -08002348 commitTempPlacement();
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002349 completeAndClearReorderPreviewAnimations();
Adam Cohen19f37922012-03-21 11:59:11 -07002350 setItemPlacementDirty(false);
2351 } else {
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002352 beginOrAdjustReorderPreviewAnimations(finalSolution, dragView,
2353 REORDER_ANIMATION_DURATION, ReorderPreviewAnimation.MODE_PREVIEW);
Adam Cohen482ed822012-03-02 14:15:13 -08002354 }
2355 }
2356 } else {
2357 foundSolution = false;
2358 result[0] = result[1] = resultSpan[0] = resultSpan[1] = -1;
2359 }
2360
2361 if ((mode == MODE_ON_DROP || !foundSolution) && !DESTRUCTIVE_REORDER) {
2362 setUseTempCoords(false);
2363 }
Adam Cohen482ed822012-03-02 14:15:13 -08002364
Michael Jurkaa52570f2012-03-20 03:18:20 -07002365 mShortcutsAndWidgets.requestLayout();
Adam Cohen482ed822012-03-02 14:15:13 -08002366 return result;
2367 }
2368
Adam Cohen19f37922012-03-21 11:59:11 -07002369 void setItemPlacementDirty(boolean dirty) {
2370 mItemPlacementDirty = dirty;
Adam Cohen482ed822012-03-02 14:15:13 -08002371 }
Adam Cohen19f37922012-03-21 11:59:11 -07002372 boolean isItemPlacementDirty() {
2373 return mItemPlacementDirty;
Adam Cohen482ed822012-03-02 14:15:13 -08002374 }
2375
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07002376 private static class ItemConfiguration extends CellAndSpan {
Rajeev Kumar9962dbe2017-06-12 12:16:20 -07002377 final ArrayMap<View, CellAndSpan> map = new ArrayMap<>();
2378 private final ArrayMap<View, CellAndSpan> savedMap = new ArrayMap<>();
2379 final ArrayList<View> sortedViews = new ArrayList<>();
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002380 ArrayList<View> intersectingViews;
Adam Cohen482ed822012-03-02 14:15:13 -08002381 boolean isSolution = false;
Adam Cohen482ed822012-03-02 14:15:13 -08002382
Adam Cohenf3900c22012-11-16 18:28:11 -08002383 void save() {
2384 // Copy current state into savedMap
2385 for (View v: map.keySet()) {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07002386 savedMap.get(v).copyFrom(map.get(v));
Adam Cohenf3900c22012-11-16 18:28:11 -08002387 }
2388 }
2389
2390 void restore() {
2391 // Restore current state from savedMap
2392 for (View v: savedMap.keySet()) {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07002393 map.get(v).copyFrom(savedMap.get(v));
Adam Cohenf3900c22012-11-16 18:28:11 -08002394 }
2395 }
2396
2397 void add(View v, CellAndSpan cs) {
2398 map.put(v, cs);
2399 savedMap.put(v, new CellAndSpan());
2400 sortedViews.add(v);
2401 }
2402
Adam Cohen482ed822012-03-02 14:15:13 -08002403 int area() {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07002404 return spanX * spanY;
Adam Cohenf3900c22012-11-16 18:28:11 -08002405 }
2406
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07002407 void getBoundingRectForViews(ArrayList<View> views, Rect outRect) {
2408 boolean first = true;
2409 for (View v: views) {
2410 CellAndSpan c = map.get(v);
2411 if (first) {
2412 outRect.set(c.cellX, c.cellY, c.cellX + c.spanX, c.cellY + c.spanY);
2413 first = false;
2414 } else {
2415 outRect.union(c.cellX, c.cellY, c.cellX + c.spanX, c.cellY + c.spanY);
2416 }
2417 }
Adam Cohenf3900c22012-11-16 18:28:11 -08002418 }
Adam Cohen482ed822012-03-02 14:15:13 -08002419 }
2420
Adam Cohendf035382011-04-11 17:22:04 -07002421 /**
Adam Cohendf035382011-04-11 17:22:04 -07002422 * Find a starting cell position that will fit the given bounds nearest the requested
2423 * cell location. Uses Euclidean distance to score multiple vacant areas.
2424 *
2425 * @param pixelX The X location at which you want to search for a vacant area.
2426 * @param pixelY The Y location at which you want to search for a vacant area.
2427 * @param spanX Horizontal span of the object.
2428 * @param spanY Vertical span of the object.
Adam Cohendf035382011-04-11 17:22:04 -07002429 * @param result Previously returned value to possibly recycle.
2430 * @return The X, Y cell of a vacant area that can contain this object,
2431 * nearest the requested location.
2432 */
Adam Cohenf9c184a2016-01-15 16:47:43 -08002433 public int[] findNearestArea(int pixelX, int pixelY, int spanX, int spanY, int[] result) {
Sunny Goyalf7a29e82015-04-24 15:20:43 -07002434 return findNearestArea(pixelX, pixelY, spanX, spanY, spanX, spanY, false, result, null);
Adam Cohendf035382011-04-11 17:22:04 -07002435 }
2436
Michael Jurka0280c3b2010-09-17 15:00:07 -07002437 boolean existsEmptyCell() {
2438 return findCellForSpan(null, 1, 1);
2439 }
2440
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002441 /**
Michael Jurka0280c3b2010-09-17 15:00:07 -07002442 * Finds the upper-left coordinate of the first rectangle in the grid that can
2443 * hold a cell of the specified dimensions. If intersectX and intersectY are not -1,
2444 * then this method will only return coordinates for rectangles that contain the cell
2445 * (intersectX, intersectY)
2446 *
2447 * @param cellXY The array that will contain the position of a vacant cell if such a cell
2448 * can be found.
2449 * @param spanX The horizontal span of the cell we want to find.
2450 * @param spanY The vertical span of the cell we want to find.
2451 *
2452 * @return True if a vacant cell of the specified dimension was found, false otherwise.
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07002453 */
Hyunyoung Song3f471442015-04-08 19:01:34 -07002454 public boolean findCellForSpan(int[] cellXY, int spanX, int spanY) {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07002455 if (cellXY == null) {
2456 cellXY = new int[2];
Michael Jurka0280c3b2010-09-17 15:00:07 -07002457 }
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07002458 return mOccupied.findVacantCell(cellXY, spanX, spanY);
Michael Jurka0280c3b2010-09-17 15:00:07 -07002459 }
2460
2461 /**
Winson Chungc07918d2011-07-01 15:35:26 -07002462 * A drag event has begun over this layout.
2463 * It may have begun over this layout (in which case onDragChild is called first),
2464 * or it may have begun on another layout.
2465 */
2466 void onDragEnter() {
Winson Chungc07918d2011-07-01 15:35:26 -07002467 mDragging = true;
2468 }
2469
2470 /**
Michael Jurka0280c3b2010-09-17 15:00:07 -07002471 * Called when drag has left this CellLayout or has been completed (successfully or not)
2472 */
2473 void onDragExit() {
Joe Onorato4be866d2010-10-10 11:26:02 -07002474 // This can actually be called when we aren't in a drag, e.g. when adding a new
2475 // item to this layout via the customize drawer.
2476 // Guard against that case.
2477 if (mDragging) {
2478 mDragging = false;
Patrick Dubroyde7658b2010-09-27 11:15:43 -07002479 }
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -07002480
2481 // Invalidate the drag data
Adam Cohend41fbf52012-02-16 23:53:59 -08002482 mDragCell[0] = mDragCell[1] = -1;
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -07002483 mDragOutlineAnims[mDragOutlineCurrent].animateOut();
2484 mDragOutlineCurrent = (mDragOutlineCurrent + 1) % mDragOutlineAnims.length;
Adam Cohen19f37922012-03-21 11:59:11 -07002485 revertTempState();
Michael Jurka33945b22010-12-21 18:19:38 -08002486 setIsDragOverlapping(false);
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07002487 }
2488
2489 /**
Winson Chungaafa03c2010-06-11 17:34:16 -07002490 * Mark a child as having been dropped.
Patrick Dubroyde7658b2010-09-27 11:15:43 -07002491 * At the beginning of the drag operation, the child may have been on another
Patrick Dubroyce34a972010-10-19 10:34:32 -07002492 * screen, but it is re-parented before this method is called.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002493 *
2494 * @param child The child that is being dropped
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002495 */
Adam Cohen716b51e2011-06-30 12:09:54 -07002496 void onDropChild(View child) {
Romain Guyd94533d2009-08-17 10:01:15 -07002497 if (child != null) {
2498 LayoutParams lp = (LayoutParams) child.getLayoutParams();
Romain Guy84f296c2009-11-04 15:00:44 -08002499 lp.dropped = true;
Romain Guyd94533d2009-08-17 10:01:15 -07002500 child.requestLayout();
Tony Wickham1cdb6d02015-09-17 11:08:27 -07002501 markCellsAsOccupiedForView(child);
Romain Guyd94533d2009-08-17 10:01:15 -07002502 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002503 }
2504
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002505 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002506 * Computes a bounding rectangle for a range of cells
Winson Chungaafa03c2010-06-11 17:34:16 -07002507 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002508 * @param cellX X coordinate of upper left corner expressed as a cell position
2509 * @param cellY Y coordinate of upper left corner expressed as a cell position
Winson Chungaafa03c2010-06-11 17:34:16 -07002510 * @param cellHSpan Width in cells
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002511 * @param cellVSpan Height in cells
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07002512 * @param resultRect Rect into which to put the results
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002513 */
Adam Cohend41fbf52012-02-16 23:53:59 -08002514 public void cellToRect(int cellX, int cellY, int cellHSpan, int cellVSpan, Rect resultRect) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002515 final int cellWidth = mCellWidth;
2516 final int cellHeight = mCellHeight;
Winson Chungaafa03c2010-06-11 17:34:16 -07002517
Winson Chung4b825dcd2011-06-19 12:41:22 -07002518 final int hStartPadding = getPaddingLeft();
2519 final int vStartPadding = getPaddingTop();
Winson Chungaafa03c2010-06-11 17:34:16 -07002520
Sunny Goyalaa8a8712016-11-20 15:26:01 +05302521 int width = cellHSpan * cellWidth;
2522 int height = cellVSpan * cellHeight;
2523 int x = hStartPadding + cellX * cellWidth;
2524 int y = vStartPadding + cellY * cellHeight;
Winson Chungaafa03c2010-06-11 17:34:16 -07002525
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07002526 resultRect.set(x, y, x + width, y + height);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002527 }
Winson Chungaafa03c2010-06-11 17:34:16 -07002528
Adam Cohend4844c32011-02-18 19:25:06 -08002529 public void markCellsAsOccupiedForView(View view) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07002530 if (view == null || view.getParent() != mShortcutsAndWidgets) return;
Michael Jurka0280c3b2010-09-17 15:00:07 -07002531 LayoutParams lp = (LayoutParams) view.getLayoutParams();
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07002532 mOccupied.markCells(lp.cellX, lp.cellY, lp.cellHSpan, lp.cellVSpan, true);
Michael Jurka0280c3b2010-09-17 15:00:07 -07002533 }
2534
Adam Cohend4844c32011-02-18 19:25:06 -08002535 public void markCellsAsUnoccupiedForView(View view) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07002536 if (view == null || view.getParent() != mShortcutsAndWidgets) return;
Michael Jurka0280c3b2010-09-17 15:00:07 -07002537 LayoutParams lp = (LayoutParams) view.getLayoutParams();
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07002538 mOccupied.markCells(lp.cellX, lp.cellY, lp.cellHSpan, lp.cellVSpan, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002539 }
2540
Adam Cohen2801caf2011-05-13 20:57:39 -07002541 public int getDesiredWidth() {
Sunny Goyalaa8a8712016-11-20 15:26:01 +05302542 return getPaddingLeft() + getPaddingRight() + (mCountX * mCellWidth);
Adam Cohen2801caf2011-05-13 20:57:39 -07002543 }
2544
2545 public int getDesiredHeight() {
Sunny Goyalaa8a8712016-11-20 15:26:01 +05302546 return getPaddingTop() + getPaddingBottom() + (mCountY * mCellHeight);
Adam Cohen2801caf2011-05-13 20:57:39 -07002547 }
2548
Michael Jurka66d72172011-04-12 16:29:25 -07002549 public boolean isOccupied(int x, int y) {
2550 if (x < mCountX && y < mCountY) {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07002551 return mOccupied.cells[x][y];
Michael Jurka66d72172011-04-12 16:29:25 -07002552 } else {
2553 throw new RuntimeException("Position exceeds the bound of this CellLayout");
2554 }
2555 }
2556
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002557 @Override
2558 public ViewGroup.LayoutParams generateLayoutParams(AttributeSet attrs) {
2559 return new CellLayout.LayoutParams(getContext(), attrs);
2560 }
2561
2562 @Override
2563 protected boolean checkLayoutParams(ViewGroup.LayoutParams p) {
2564 return p instanceof CellLayout.LayoutParams;
2565 }
2566
2567 @Override
2568 protected ViewGroup.LayoutParams generateLayoutParams(ViewGroup.LayoutParams p) {
2569 return new CellLayout.LayoutParams(p);
2570 }
2571
2572 public static class LayoutParams extends ViewGroup.MarginLayoutParams {
2573 /**
2574 * Horizontal location of the item in the grid.
2575 */
2576 @ViewDebug.ExportedProperty
2577 public int cellX;
2578
2579 /**
2580 * Vertical location of the item in the grid.
2581 */
2582 @ViewDebug.ExportedProperty
2583 public int cellY;
2584
2585 /**
Adam Cohen482ed822012-03-02 14:15:13 -08002586 * Temporary horizontal location of the item in the grid during reorder
2587 */
2588 public int tmpCellX;
2589
2590 /**
2591 * Temporary vertical location of the item in the grid during reorder
2592 */
2593 public int tmpCellY;
2594
2595 /**
2596 * Indicates that the temporary coordinates should be used to layout the items
2597 */
2598 public boolean useTmpCoords;
2599
2600 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002601 * Number of cells spanned horizontally by the item.
2602 */
2603 @ViewDebug.ExportedProperty
2604 public int cellHSpan;
2605
2606 /**
2607 * Number of cells spanned vertically by the item.
2608 */
2609 @ViewDebug.ExportedProperty
2610 public int cellVSpan;
Winson Chungaafa03c2010-06-11 17:34:16 -07002611
Adam Cohen1b607ed2011-03-03 17:26:50 -08002612 /**
2613 * Indicates whether the item will set its x, y, width and height parameters freely,
2614 * or whether these will be computed based on cellX, cellY, cellHSpan and cellVSpan.
2615 */
Adam Cohend4844c32011-02-18 19:25:06 -08002616 public boolean isLockedToGrid = true;
2617
Adam Cohen482ed822012-03-02 14:15:13 -08002618 /**
2619 * Indicates whether this item can be reordered. Always true except in the case of the
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07002620 * the AllApps button and QSB place holder.
Adam Cohen482ed822012-03-02 14:15:13 -08002621 */
2622 public boolean canReorder = true;
2623
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002624 // X coordinate of the view in the layout.
2625 @ViewDebug.ExportedProperty
Vadim Tryshevfedca432015-08-19 17:55:02 -07002626 public int x;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002627 // Y coordinate of the view in the layout.
2628 @ViewDebug.ExportedProperty
Vadim Tryshevfedca432015-08-19 17:55:02 -07002629 public int y;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002630
Romain Guy84f296c2009-11-04 15:00:44 -08002631 boolean dropped;
Romain Guyfcb9e712009-10-02 16:06:52 -07002632
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002633 public LayoutParams(Context c, AttributeSet attrs) {
2634 super(c, attrs);
2635 cellHSpan = 1;
2636 cellVSpan = 1;
2637 }
2638
2639 public LayoutParams(ViewGroup.LayoutParams source) {
2640 super(source);
2641 cellHSpan = 1;
2642 cellVSpan = 1;
2643 }
Winson Chungaafa03c2010-06-11 17:34:16 -07002644
2645 public LayoutParams(LayoutParams source) {
2646 super(source);
2647 this.cellX = source.cellX;
2648 this.cellY = source.cellY;
2649 this.cellHSpan = source.cellHSpan;
2650 this.cellVSpan = source.cellVSpan;
2651 }
2652
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002653 public LayoutParams(int cellX, int cellY, int cellHSpan, int cellVSpan) {
Romain Guy8f19cdd2010-01-08 15:07:00 -08002654 super(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002655 this.cellX = cellX;
2656 this.cellY = cellY;
2657 this.cellHSpan = cellHSpan;
2658 this.cellVSpan = cellVSpan;
2659 }
2660
Sunny Goyalaa8a8712016-11-20 15:26:01 +05302661 public void setup(int cellWidth, int cellHeight, boolean invertHorizontally, int colCount) {
Jon Miranda7ae64ff2016-11-21 16:18:46 -08002662 setup(cellWidth, cellHeight, invertHorizontally, colCount, 1.0f, 1.0f);
2663 }
2664
2665 /**
2666 * Use this method, as opposed to {@link #setup(int, int, boolean, int)}, if the view needs
2667 * to be scaled.
2668 *
2669 * ie. In multi-window mode, we setup widgets so that they are measured and laid out
2670 * using their full/invariant device profile sizes.
2671 */
2672 public void setup(int cellWidth, int cellHeight, boolean invertHorizontally, int colCount,
2673 float cellScaleX, float cellScaleY) {
Adam Cohend4844c32011-02-18 19:25:06 -08002674 if (isLockedToGrid) {
2675 final int myCellHSpan = cellHSpan;
2676 final int myCellVSpan = cellVSpan;
Adam Cohen2374abf2013-04-16 14:56:57 -07002677 int myCellX = useTmpCoords ? tmpCellX : cellX;
2678 int myCellY = useTmpCoords ? tmpCellY : cellY;
2679
2680 if (invertHorizontally) {
2681 myCellX = colCount - myCellX - cellHSpan;
2682 }
Adam Cohen1b607ed2011-03-03 17:26:50 -08002683
Jon Miranda7ae64ff2016-11-21 16:18:46 -08002684 width = (int) (myCellHSpan * cellWidth / cellScaleX - leftMargin - rightMargin);
2685 height = (int) (myCellVSpan * cellHeight / cellScaleY - topMargin - bottomMargin);
Sunny Goyalaa8a8712016-11-20 15:26:01 +05302686 x = (myCellX * cellWidth + leftMargin);
2687 y = (myCellY * cellHeight + topMargin);
Adam Cohend4844c32011-02-18 19:25:06 -08002688 }
2689 }
Winson Chungaafa03c2010-06-11 17:34:16 -07002690
Winson Chungaafa03c2010-06-11 17:34:16 -07002691 public String toString() {
2692 return "(" + this.cellX + ", " + this.cellY + ")";
2693 }
Adam Cohen7f4eabe2011-04-21 16:19:16 -07002694
2695 public void setWidth(int width) {
2696 this.width = width;
2697 }
2698
2699 public int getWidth() {
2700 return width;
2701 }
2702
2703 public void setHeight(int height) {
2704 this.height = height;
2705 }
2706
2707 public int getHeight() {
2708 return height;
2709 }
2710
2711 public void setX(int x) {
2712 this.x = x;
2713 }
2714
2715 public int getX() {
2716 return x;
2717 }
2718
2719 public void setY(int y) {
2720 this.y = y;
2721 }
2722
2723 public int getY() {
2724 return y;
2725 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002726 }
2727
Michael Jurka0280c3b2010-09-17 15:00:07 -07002728 // This class stores info for two purposes:
2729 // 1. When dragging items (mDragInfo in Workspace), we store the View, its cellX & cellY,
2730 // its spanX, spanY, and the screen it is on
2731 // 2. When long clicking on an empty cell in a CellLayout, we save information about the
2732 // cellX and cellY coordinates and which page was clicked. We then set this as a tag on
2733 // the CellLayout that was long clicked
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07002734 public static final class CellInfo extends CellAndSpan {
Rajeev Kumar9962dbe2017-06-12 12:16:20 -07002735 public final View cell;
2736 final long screenId;
2737 final long container;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002738
Sunny Goyal83a8f042015-05-19 12:52:12 -07002739 public CellInfo(View v, ItemInfo info) {
Adam Cohene0aaa0d2014-05-12 12:44:22 -07002740 cellX = info.cellX;
2741 cellY = info.cellY;
2742 spanX = info.spanX;
2743 spanY = info.spanY;
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07002744 cell = v;
Adam Cohene0aaa0d2014-05-12 12:44:22 -07002745 screenId = info.screenId;
2746 container = info.container;
2747 }
2748
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002749 @Override
2750 public String toString() {
Winson Chungaafa03c2010-06-11 17:34:16 -07002751 return "Cell[view=" + (cell == null ? "null" : cell.getClass())
2752 + ", x=" + cellX + ", y=" + cellY + "]";
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002753 }
2754 }
Michael Jurkad771c962011-08-09 15:00:48 -07002755
Tony Wickham86930612015-09-09 13:50:40 -07002756 /**
2757 * Returns whether an item can be placed in this CellLayout (after rearranging and/or resizing
2758 * if necessary).
2759 */
2760 public boolean hasReorderSolution(ItemInfo itemInfo) {
2761 int[] cellPoint = new int[2];
2762 // Check for a solution starting at every cell.
2763 for (int cellX = 0; cellX < getCountX(); cellX++) {
2764 for (int cellY = 0; cellY < getCountY(); cellY++) {
2765 cellToPoint(cellX, cellY, cellPoint);
2766 if (findReorderSolution(cellPoint[0], cellPoint[1], itemInfo.minSpanX,
2767 itemInfo.minSpanY, itemInfo.spanX, itemInfo.spanY, mDirectionVector, null,
2768 true, new ItemConfiguration()).isSolution) {
2769 return true;
2770 }
2771 }
2772 }
2773 return false;
2774 }
2775
Sunny Goyal9ca9c132015-04-29 14:57:22 -07002776 public boolean isRegionVacant(int x, int y, int spanX, int spanY) {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07002777 return mOccupied.isRegionVacant(x, y, spanX, spanY);
Sunny Goyal9ca9c132015-04-29 14:57:22 -07002778 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002779}