blob: d07e3303db332fbd51f284466c11f7135d9c0ef6 [file] [log] [blame]
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001/*
2 * Copyright (C) 2008 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Daniel Sandler325dc232013-06-05 22:57:57 -040017package com.android.launcher3;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080018
Joe Onorato4be866d2010-10-10 11:26:02 -070019import android.animation.Animator;
Michael Jurka629758f2012-06-14 16:18:21 -070020import android.animation.AnimatorListenerAdapter;
Chet Haase00397b12010-10-07 11:13:10 -070021import android.animation.TimeInterpolator;
Patrick Dubroyde7658b2010-09-27 11:15:43 -070022import android.animation.ValueAnimator;
23import android.animation.ValueAnimator.AnimatorUpdateListener;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080024import android.content.Context;
Joe Onorato79e56262009-09-21 15:23:04 -040025import android.content.res.Resources;
Sunny Goyalc13403c2016-11-18 23:44:48 -080026import android.content.res.TypedArray;
Joe Onorato4be866d2010-10-10 11:26:02 -070027import android.graphics.Bitmap;
Winson Chungaafa03c2010-06-11 17:34:16 -070028import android.graphics.Canvas;
Andrew Flynn0dca1ec2012-02-29 13:33:22 -080029import android.graphics.Color;
Joe Onorato4be866d2010-10-10 11:26:02 -070030import android.graphics.Paint;
Patrick Dubroyde7658b2010-09-27 11:15:43 -070031import android.graphics.Point;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080032import android.graphics.Rect;
Adam Cohen482ed822012-03-02 14:15:13 -080033import android.graphics.drawable.ColorDrawable;
Patrick Dubroy6569f2c2010-07-12 14:25:18 -070034import android.graphics.drawable.Drawable;
Adam Cohen1462de32012-07-24 22:34:36 -070035import android.os.Parcelable;
Sunny Goyalc13403c2016-11-18 23:44:48 -080036import android.support.annotation.IntDef;
Adam Cohenc9735cf2015-01-23 16:11:55 -080037import android.support.v4.view.ViewCompat;
Rajeev Kumar9962dbe2017-06-12 12:16:20 -070038import android.util.ArrayMap;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080039import android.util.AttributeSet;
Joe Onorato4be866d2010-10-10 11:26:02 -070040import android.util.Log;
Adam Cohen1462de32012-07-24 22:34:36 -070041import android.util.SparseArray;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080042import android.view.MotionEvent;
43import android.view.View;
44import android.view.ViewDebug;
45import android.view.ViewGroup;
Adam Cohenc9735cf2015-01-23 16:11:55 -080046import android.view.accessibility.AccessibilityEvent;
Winson Chung150fbab2010-09-29 17:14:26 -070047import android.view.animation.DecelerateInterpolator;
Sunny Goyal4b6eb262015-05-14 19:24:40 -070048import com.android.launcher3.BubbleTextView.BubbleTextShadowHandler;
Sunny Goyalaa8ef112015-06-12 20:04:41 -070049import com.android.launcher3.LauncherSettings.Favorites;
Sunny Goyale9b651e2015-04-24 11:44:51 -070050import com.android.launcher3.accessibility.DragAndDropAccessibilityDelegate;
51import com.android.launcher3.accessibility.FolderAccessibilityHelper;
52import com.android.launcher3.accessibility.WorkspaceAccessibilityHelper;
Sunny Goyal9e76f682017-02-13 12:13:43 -080053import com.android.launcher3.anim.PropertyListBuilder;
Sunny Goyal3d706ad2017-03-06 16:56:39 -080054import com.android.launcher3.config.FeatureFlags;
Sunny Goyal26119432016-02-18 22:09:23 +000055import com.android.launcher3.folder.FolderIcon;
Jon Mirandaa0233f72017-06-22 18:34:45 -070056import com.android.launcher3.folder.PreviewBackground;
Sunny Goyal06e21a22016-08-11 16:02:02 -070057import com.android.launcher3.graphics.DragPreviewProvider;
Sunny Goyalff4ba2d2016-04-02 14:12:34 -070058import com.android.launcher3.util.CellAndSpan;
59import com.android.launcher3.util.GridOccupancy;
Sunny Goyale2fd14b2015-08-27 17:45:46 -070060import com.android.launcher3.util.ParcelableSparseArray;
Sunny Goyal9b29ca52017-02-17 10:39:44 -080061import com.android.launcher3.util.Themes;
Adam Cohen091440a2015-03-18 14:16:05 -070062import com.android.launcher3.util.Thunk;
Sunny Goyalc13403c2016-11-18 23:44:48 -080063import java.lang.annotation.Retention;
64import java.lang.annotation.RetentionPolicy;
Adam Cohen69ce2e52011-07-03 19:25:21 -070065import java.util.ArrayList;
Adam Cohenc0dcf592011-06-01 15:30:43 -070066import java.util.Arrays;
Adam Cohenf3900c22012-11-16 18:28:11 -080067import java.util.Collections;
68import java.util.Comparator;
Adam Cohend41fbf52012-02-16 23:53:59 -080069import java.util.Stack;
Adam Cohenc0dcf592011-06-01 15:30:43 -070070
Sunny Goyal4b6eb262015-05-14 19:24:40 -070071public class CellLayout extends ViewGroup implements BubbleTextShadowHandler {
Sunny Goyale9b651e2015-04-24 11:44:51 -070072 public static final int WORKSPACE_ACCESSIBILITY_DRAG = 2;
73 public static final int FOLDER_ACCESSIBILITY_DRAG = 1;
74
Tony Wickhama0628cc2015-10-14 15:23:04 -070075 private static final String TAG = "CellLayout";
76 private static final boolean LOGD = false;
Winson Chungaafa03c2010-06-11 17:34:16 -070077
Rajeev Kumar9962dbe2017-06-12 12:16:20 -070078 private final Launcher mLauncher;
Sunny Goyal4ffec482016-02-09 11:28:52 -080079 @ViewDebug.ExportedProperty(category = "launcher")
Adam Cohen091440a2015-03-18 14:16:05 -070080 @Thunk int mCellWidth;
Sunny Goyal4ffec482016-02-09 11:28:52 -080081 @ViewDebug.ExportedProperty(category = "launcher")
Adam Cohen091440a2015-03-18 14:16:05 -070082 @Thunk int mCellHeight;
Winson Chung11a1a532013-09-13 11:14:45 -070083 private int mFixedCellWidth;
84 private int mFixedCellHeight;
Winson Chungaafa03c2010-06-11 17:34:16 -070085
Sunny Goyal4ffec482016-02-09 11:28:52 -080086 @ViewDebug.ExportedProperty(category = "launcher")
Sunny Goyalff4ba2d2016-04-02 14:12:34 -070087 private int mCountX;
Sunny Goyal4ffec482016-02-09 11:28:52 -080088 @ViewDebug.ExportedProperty(category = "launcher")
Sunny Goyalff4ba2d2016-04-02 14:12:34 -070089 private int mCountY;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080090
Adam Cohen917e3882013-10-31 15:03:35 -070091 private boolean mDropPending = false;
Adam Cohenc50438c2014-08-19 17:43:05 -070092 private boolean mIsDragTarget = true;
Sunny Goyale2fd14b2015-08-27 17:45:46 -070093 private boolean mJailContent = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080094
Patrick Dubroyde7658b2010-09-27 11:15:43 -070095 // These are temporary variables to prevent having to allocate a new object just to
96 // return an (x, y) value from helper functions. Do NOT use them to maintain other state.
Adam Cohen091440a2015-03-18 14:16:05 -070097 @Thunk final int[] mTmpPoint = new int[2];
Sunny Goyal2805e632015-05-20 15:35:32 -070098 @Thunk final int[] mTempLocation = new int[2];
Patrick Dubroy6569f2c2010-07-12 14:25:18 -070099
Sunny Goyalff4ba2d2016-04-02 14:12:34 -0700100 private GridOccupancy mOccupied;
101 private GridOccupancy mTmpOccupied;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800102
Michael Jurkadee05892010-07-27 10:01:56 -0700103 private OnTouchListener mInterceptTouchListener;
Rajeev Kumar9962dbe2017-06-12 12:16:20 -0700104 private final StylusEventHelper mStylusEventHelper;
Michael Jurkadee05892010-07-27 10:01:56 -0700105
Jon Mirandaa0233f72017-06-22 18:34:45 -0700106 private final ArrayList<PreviewBackground> mFolderBackgrounds = new ArrayList<>();
107 final PreviewBackground mFolderLeaveBehind = new PreviewBackground();
Adam Cohen69ce2e52011-07-03 19:25:21 -0700108
Michael Jurka5f1c5092010-09-03 14:15:02 -0700109 private float mBackgroundAlpha;
Adam Cohenf34bab52010-09-30 14:11:56 -0700110
Sunny Goyalf5440cb2016-12-14 15:13:00 -0800111 private static final int[] BACKGROUND_STATE_ACTIVE = new int[] { android.R.attr.state_active };
112 private static final int[] BACKGROUND_STATE_DEFAULT = new int[0];
113 private final Drawable mBackground;
Sunny Goyal2805e632015-05-20 15:35:32 -0700114
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700115 // These values allow a fixed measurement to be set on the CellLayout.
116 private int mFixedWidth = -1;
117 private int mFixedHeight = -1;
118
Michael Jurka33945b22010-12-21 18:19:38 -0800119 // If we're actively dragging something over this screen, mIsDragOverlapping is true
120 private boolean mIsDragOverlapping = false;
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700121
Winson Chung150fbab2010-09-29 17:14:26 -0700122 // These arrays are used to implement the drag visualization on x-large screens.
Joe Onorato4be866d2010-10-10 11:26:02 -0700123 // They are used as circular arrays, indexed by mDragOutlineCurrent.
Rajeev Kumar9962dbe2017-06-12 12:16:20 -0700124 @Thunk final Rect[] mDragOutlines = new Rect[4];
125 @Thunk final float[] mDragOutlineAlphas = new float[mDragOutlines.length];
126 private final InterruptibleInOutAnimator[] mDragOutlineAnims =
Joe Onorato4be866d2010-10-10 11:26:02 -0700127 new InterruptibleInOutAnimator[mDragOutlines.length];
Winson Chung150fbab2010-09-29 17:14:26 -0700128
129 // Used as an index into the above 3 arrays; indicates which is the most current value.
Joe Onorato4be866d2010-10-10 11:26:02 -0700130 private int mDragOutlineCurrent = 0;
Patrick Dubroy8e58e912010-10-14 13:21:48 -0700131 private final Paint mDragOutlinePaint = new Paint();
Winson Chung150fbab2010-09-29 17:14:26 -0700132
Sunny Goyal4fe5a372015-05-14 19:55:10 -0700133 private final ClickShadowView mTouchFeedbackView;
Patrick Dubroy96864c32011-03-10 17:17:23 -0800134
Rajeev Kumar9962dbe2017-06-12 12:16:20 -0700135 @Thunk final ArrayMap<LayoutParams, Animator> mReorderAnimators = new ArrayMap<>();
136 @Thunk final ArrayMap<View, ReorderPreviewAnimation> mShakeAnimators = new ArrayMap<>();
Adam Cohen19f37922012-03-21 11:59:11 -0700137
138 private boolean mItemPlacementDirty = false;
Adam Cohenbfbfd262011-06-13 16:55:12 -0700139
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700140 // When a drag operation is in progress, holds the nearest cell to the touch point
141 private final int[] mDragCell = new int[2];
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800142
Joe Onorato4be866d2010-10-10 11:26:02 -0700143 private boolean mDragging = false;
144
Rajeev Kumar9962dbe2017-06-12 12:16:20 -0700145 private final TimeInterpolator mEaseOutInterpolator;
146 private final ShortcutAndWidgetContainer mShortcutsAndWidgets;
Patrick Dubroyce34a972010-10-19 10:34:32 -0700147
Sunny Goyalc13403c2016-11-18 23:44:48 -0800148 @Retention(RetentionPolicy.SOURCE)
149 @IntDef({WORKSPACE, HOTSEAT, FOLDER})
150 public @interface ContainerType{}
151 public static final int WORKSPACE = 0;
152 public static final int HOTSEAT = 1;
153 public static final int FOLDER = 2;
154
155 @ContainerType private final int mContainerType;
156
157 private final float mChildScale;
Andrew Flynn0dca1ec2012-02-29 13:33:22 -0800158
Adam Cohenfa3c58f2013-12-06 16:10:55 -0800159 public static final int MODE_SHOW_REORDER_HINT = 0;
160 public static final int MODE_DRAG_OVER = 1;
161 public static final int MODE_ON_DROP = 2;
162 public static final int MODE_ON_DROP_EXTERNAL = 3;
163 public static final int MODE_ACCEPT_DROP = 4;
Adam Cohen19f37922012-03-21 11:59:11 -0700164 private static final boolean DESTRUCTIVE_REORDER = false;
Adam Cohen482ed822012-03-02 14:15:13 -0800165 private static final boolean DEBUG_VISUALIZE_OCCUPIED = false;
166
Adam Cohenfa3c58f2013-12-06 16:10:55 -0800167 private static final float REORDER_PREVIEW_MAGNITUDE = 0.12f;
Adam Cohen19f37922012-03-21 11:59:11 -0700168 private static final int REORDER_ANIMATION_DURATION = 150;
Sunny Goyalc13403c2016-11-18 23:44:48 -0800169 @Thunk final float mReorderPreviewAnimationMagnitude;
Adam Cohen19f37922012-03-21 11:59:11 -0700170
Rajeev Kumar9962dbe2017-06-12 12:16:20 -0700171 private final ArrayList<View> mIntersectingViews = new ArrayList<>();
172 private final Rect mOccupiedRect = new Rect();
173 private final int[] mDirectionVector = new int[2];
174 final int[] mPreviousReorderDirection = new int[2];
Adam Cohenb209e632012-03-27 17:09:36 -0700175 private static final int INVALID_DIRECTION = -100;
Adam Cohen482ed822012-03-02 14:15:13 -0800176
Sunny Goyal2805e632015-05-20 15:35:32 -0700177 private final Rect mTempRect = new Rect();
Winson Chung3a6e7f32013-10-09 15:50:52 -0700178
Michael Jurkaca993832012-06-29 15:17:04 -0700179 private final static Paint sPaint = new Paint();
Romain Guy8a0bff52012-05-06 13:14:33 -0700180
Adam Cohenc9735cf2015-01-23 16:11:55 -0800181 // Related to accessible drag and drop
Sunny Goyale9b651e2015-04-24 11:44:51 -0700182 private DragAndDropAccessibilityDelegate mTouchHelper;
Adam Cohenc9735cf2015-01-23 16:11:55 -0800183 private boolean mUseTouchHelper = false;
Adam Cohenc9735cf2015-01-23 16:11:55 -0800184
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800185 public CellLayout(Context context) {
186 this(context, null);
187 }
188
189 public CellLayout(Context context, AttributeSet attrs) {
190 this(context, attrs, 0);
191 }
192
193 public CellLayout(Context context, AttributeSet attrs, int defStyle) {
194 super(context, attrs, defStyle);
Sunny Goyalc13403c2016-11-18 23:44:48 -0800195 TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.CellLayout, defStyle, 0);
196 mContainerType = a.getInteger(R.styleable.CellLayout_containerType, WORKSPACE);
197 a.recycle();
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700198
199 // A ViewGroup usually does not draw, but CellLayout needs to draw a rectangle to show
200 // the user where a dragged item will land when dropped.
201 setWillNotDraw(false);
Romain Guyce3cbd12013-02-25 15:00:36 -0800202 setClipToPadding(false);
Tony2fd02082016-10-07 12:50:01 -0700203 mLauncher = Launcher.getLauncher(context);
Michael Jurkaa63c4522010-08-19 13:52:27 -0700204
Adam Cohen2e6da152015-05-06 11:42:25 -0700205 DeviceProfile grid = mLauncher.getDeviceProfile();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800206
Winson Chung11a1a532013-09-13 11:14:45 -0700207 mCellWidth = mCellHeight = -1;
Nilesh Agrawal5f7099a2014-01-02 15:54:57 -0800208 mFixedCellWidth = mFixedCellHeight = -1;
Sunny Goyalff4ba2d2016-04-02 14:12:34 -0700209
210 mCountX = grid.inv.numColumns;
211 mCountY = grid.inv.numRows;
212 mOccupied = new GridOccupancy(mCountX, mCountY);
213 mTmpOccupied = new GridOccupancy(mCountX, mCountY);
214
Adam Cohen5b53f292012-03-29 14:30:35 -0700215 mPreviousReorderDirection[0] = INVALID_DIRECTION;
216 mPreviousReorderDirection[1] = INVALID_DIRECTION;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800217
Adam Cohenefca0272016-02-24 19:19:06 -0800218 mFolderLeaveBehind.delegateCellX = -1;
219 mFolderLeaveBehind.delegateCellY = -1;
220
Sunny Goyalc13403c2016-11-18 23:44:48 -0800221 mChildScale = mContainerType == HOTSEAT ? grid.inv.hotseatScale : 1f;
222
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800223 setAlwaysDrawnWithCacheEnabled(false);
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700224 final Resources res = getResources();
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700225
Sunny Goyalf5440cb2016-12-14 15:13:00 -0800226 mBackground = res.getDrawable(R.drawable.bg_celllayout);
Sunny Goyal2805e632015-05-20 15:35:32 -0700227 mBackground.setCallback(this);
Winson Chunge8f1d042015-07-31 12:39:57 -0700228 mBackground.setAlpha((int) (mBackgroundAlpha * 255));
Michael Jurka33945b22010-12-21 18:19:38 -0800229
Sunny Goyalc13403c2016-11-18 23:44:48 -0800230 mReorderPreviewAnimationMagnitude = (REORDER_PREVIEW_MAGNITUDE * grid.iconSizePx);
Adam Cohen19f37922012-03-21 11:59:11 -0700231
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700232 // Initialize the data structures used for the drag visualization.
Patrick Dubroyce34a972010-10-19 10:34:32 -0700233 mEaseOutInterpolator = new DecelerateInterpolator(2.5f); // Quint ease out
Winson Chungb8c69f32011-10-19 21:36:08 -0700234 mDragCell[0] = mDragCell[1] = -1;
Joe Onorato4be866d2010-10-10 11:26:02 -0700235 for (int i = 0; i < mDragOutlines.length; i++) {
Adam Cohend41fbf52012-02-16 23:53:59 -0800236 mDragOutlines[i] = new Rect(-1, -1, -1, -1);
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700237 }
Mario Bertschler54ba6012017-06-08 10:53:53 -0700238 mDragOutlinePaint.setColor(Themes.getAttrColor(context, R.attr.workspaceTextColor));
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700239
240 // When dragging things around the home screens, we show a green outline of
241 // where the item will land. The outlines gradually fade out, leaving a trail
242 // behind the drag path.
243 // Set up all the animations that are used to implement this fading.
244 final int duration = res.getInteger(R.integer.config_dragOutlineFadeTime);
Chet Haase472b2812010-10-14 07:02:04 -0700245 final float fromAlphaValue = 0;
246 final float toAlphaValue = (float)res.getInteger(R.integer.config_dragOutlineMaxAlpha);
Joe Onorato4be866d2010-10-10 11:26:02 -0700247
Patrick Dubroy8e58e912010-10-14 13:21:48 -0700248 Arrays.fill(mDragOutlineAlphas, fromAlphaValue);
Joe Onorato4be866d2010-10-10 11:26:02 -0700249
250 for (int i = 0; i < mDragOutlineAnims.length; i++) {
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700251 final InterruptibleInOutAnimator anim =
Michael Jurkaf1ad6082013-03-13 12:55:46 +0100252 new InterruptibleInOutAnimator(this, duration, fromAlphaValue, toAlphaValue);
Patrick Dubroyce34a972010-10-19 10:34:32 -0700253 anim.getAnimator().setInterpolator(mEaseOutInterpolator);
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700254 final int thisIndex = i;
Chet Haase472b2812010-10-14 07:02:04 -0700255 anim.getAnimator().addUpdateListener(new AnimatorUpdateListener() {
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700256 public void onAnimationUpdate(ValueAnimator animation) {
Joe Onorato4be866d2010-10-10 11:26:02 -0700257 final Bitmap outline = (Bitmap)anim.getTag();
258
259 // If an animation is started and then stopped very quickly, we can still
260 // get spurious updates we've cleared the tag. Guard against this.
261 if (outline == null) {
Tony Wickhama0628cc2015-10-14 15:23:04 -0700262 if (LOGD) {
Patrick Dubroyfe6bd872010-10-13 17:32:10 -0700263 Object val = animation.getAnimatedValue();
264 Log.d(TAG, "anim " + thisIndex + " update: " + val +
265 ", isStopped " + anim.isStopped());
266 }
Joe Onorato4be866d2010-10-10 11:26:02 -0700267 // Try to prevent it from continuing to run
268 animation.cancel();
269 } else {
Chet Haase472b2812010-10-14 07:02:04 -0700270 mDragOutlineAlphas[thisIndex] = (Float) animation.getAnimatedValue();
Adam Cohend41fbf52012-02-16 23:53:59 -0800271 CellLayout.this.invalidate(mDragOutlines[thisIndex]);
Joe Onorato4be866d2010-10-10 11:26:02 -0700272 }
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700273 }
274 });
Joe Onorato4be866d2010-10-10 11:26:02 -0700275 // The animation holds a reference to the drag outline bitmap as long is it's
276 // running. This way the bitmap can be GCed when the animations are complete.
Chet Haase472b2812010-10-14 07:02:04 -0700277 anim.getAnimator().addListener(new AnimatorListenerAdapter() {
Michael Jurka3c4c20f2010-10-28 15:36:06 -0700278 @Override
Joe Onorato4be866d2010-10-10 11:26:02 -0700279 public void onAnimationEnd(Animator animation) {
Chet Haase472b2812010-10-14 07:02:04 -0700280 if ((Float) ((ValueAnimator) animation).getAnimatedValue() == 0f) {
Joe Onorato4be866d2010-10-10 11:26:02 -0700281 anim.setTag(null);
282 }
283 }
284 });
285 mDragOutlineAnims[i] = anim;
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700286 }
Patrick Dubroyce34a972010-10-19 10:34:32 -0700287
Sunny Goyalc13403c2016-11-18 23:44:48 -0800288 mShortcutsAndWidgets = new ShortcutAndWidgetContainer(context, mContainerType);
Sunny Goyalaa8a8712016-11-20 15:26:01 +0530289 mShortcutsAndWidgets.setCellDimensions(mCellWidth, mCellHeight, mCountX, mCountY);
Adam Cohen2374abf2013-04-16 14:56:57 -0700290
Mady Mellorbb835202015-07-15 16:34:34 -0700291 mStylusEventHelper = new StylusEventHelper(new SimpleOnStylusPressListener(this), this);
Mady Melloref044dd2015-06-02 15:35:07 -0700292
Sunny Goyal4fe5a372015-05-14 19:55:10 -0700293 mTouchFeedbackView = new ClickShadowView(context);
294 addView(mTouchFeedbackView);
Michael Jurkaa52570f2012-03-20 03:18:20 -0700295 addView(mShortcutsAndWidgets);
Michael Jurka18014792010-10-14 09:01:34 -0700296 }
297
Sunny Goyale9b651e2015-04-24 11:44:51 -0700298 public void enableAccessibleDrag(boolean enable, int dragType) {
Adam Cohenc9735cf2015-01-23 16:11:55 -0800299 mUseTouchHelper = enable;
300 if (!enable) {
301 ViewCompat.setAccessibilityDelegate(this, null);
302 setImportantForAccessibility(IMPORTANT_FOR_ACCESSIBILITY_NO);
303 getShortcutsAndWidgets().setImportantForAccessibility(IMPORTANT_FOR_ACCESSIBILITY_NO);
304 setOnClickListener(mLauncher);
305 } else {
Sunny Goyale9b651e2015-04-24 11:44:51 -0700306 if (dragType == WORKSPACE_ACCESSIBILITY_DRAG &&
307 !(mTouchHelper instanceof WorkspaceAccessibilityHelper)) {
308 mTouchHelper = new WorkspaceAccessibilityHelper(this);
309 } else if (dragType == FOLDER_ACCESSIBILITY_DRAG &&
310 !(mTouchHelper instanceof FolderAccessibilityHelper)) {
311 mTouchHelper = new FolderAccessibilityHelper(this);
312 }
Adam Cohenc9735cf2015-01-23 16:11:55 -0800313 ViewCompat.setAccessibilityDelegate(this, mTouchHelper);
314 setImportantForAccessibility(IMPORTANT_FOR_ACCESSIBILITY_YES);
315 getShortcutsAndWidgets().setImportantForAccessibility(IMPORTANT_FOR_ACCESSIBILITY_YES);
316 setOnClickListener(mTouchHelper);
317 }
318
319 // Invalidate the accessibility hierarchy
320 if (getParent() != null) {
321 getParent().notifySubtreeAccessibilityStateChanged(
322 this, this, AccessibilityEvent.CONTENT_CHANGE_TYPE_SUBTREE);
323 }
324 }
325
326 @Override
327 public boolean dispatchHoverEvent(MotionEvent event) {
328 // Always attempt to dispatch hover events to accessibility first.
329 if (mUseTouchHelper && mTouchHelper.dispatchHoverEvent(event)) {
330 return true;
331 }
332 return super.dispatchHoverEvent(event);
333 }
334
335 @Override
Adam Cohenc9735cf2015-01-23 16:11:55 -0800336 public boolean onInterceptTouchEvent(MotionEvent ev) {
337 if (mUseTouchHelper ||
338 (mInterceptTouchListener != null && mInterceptTouchListener.onTouch(this, ev))) {
339 return true;
340 }
341 return false;
342 }
343
Mady Melloref044dd2015-06-02 15:35:07 -0700344 @Override
345 public boolean onTouchEvent(MotionEvent ev) {
346 boolean handled = super.onTouchEvent(ev);
347 // Stylus button press on a home screen should not switch between overview mode and
348 // the home screen mode, however, once in overview mode stylus button press should be
349 // enabled to allow rearranging the different home screens. So check what mode
350 // the workspace is in, and only perform stylus button presses while in overview mode.
351 if (mLauncher.mWorkspace.isInOverviewMode()
Mady Mellorbb835202015-07-15 16:34:34 -0700352 && mStylusEventHelper.onMotionEvent(ev)) {
Mady Melloref044dd2015-06-02 15:35:07 -0700353 return true;
354 }
355 return handled;
356 }
357
Chris Craik01f2d7f2013-10-01 14:41:56 -0700358 public void enableHardwareLayer(boolean hasLayer) {
359 mShortcutsAndWidgets.setLayerType(hasLayer ? LAYER_TYPE_HARDWARE : LAYER_TYPE_NONE, sPaint);
Michael Jurkad51f33a2012-06-28 15:35:26 -0700360 }
361
362 public void buildHardwareLayer() {
363 mShortcutsAndWidgets.buildLayer();
Adam Cohen2801caf2011-05-13 20:57:39 -0700364 }
365
Winson Chung5f8afe62013-08-12 16:19:28 -0700366 public void setCellDimensions(int width, int height) {
Winson Chung11a1a532013-09-13 11:14:45 -0700367 mFixedCellWidth = mCellWidth = width;
368 mFixedCellHeight = mCellHeight = height;
Sunny Goyalaa8a8712016-11-20 15:26:01 +0530369 mShortcutsAndWidgets.setCellDimensions(mCellWidth, mCellHeight, mCountX, mCountY);
Winson Chung5f8afe62013-08-12 16:19:28 -0700370 }
371
Adam Cohen2801caf2011-05-13 20:57:39 -0700372 public void setGridSize(int x, int y) {
373 mCountX = x;
374 mCountY = y;
Sunny Goyalff4ba2d2016-04-02 14:12:34 -0700375 mOccupied = new GridOccupancy(mCountX, mCountY);
376 mTmpOccupied = new GridOccupancy(mCountX, mCountY);
Adam Cohen7fbec102012-03-27 12:42:19 -0700377 mTempRectStack.clear();
Sunny Goyalaa8a8712016-11-20 15:26:01 +0530378 mShortcutsAndWidgets.setCellDimensions(mCellWidth, mCellHeight, mCountX, mCountY);
Adam Cohen76fc0852011-06-17 13:26:23 -0700379 requestLayout();
Adam Cohen2801caf2011-05-13 20:57:39 -0700380 }
381
Adam Cohen2374abf2013-04-16 14:56:57 -0700382 // Set whether or not to invert the layout horizontally if the layout is in RTL mode.
383 public void setInvertIfRtl(boolean invert) {
384 mShortcutsAndWidgets.setInvertIfRtl(invert);
385 }
386
Adam Cohen917e3882013-10-31 15:03:35 -0700387 public void setDropPending(boolean pending) {
388 mDropPending = pending;
389 }
390
391 public boolean isDropPending() {
392 return mDropPending;
393 }
394
Sunny Goyal4b6eb262015-05-14 19:24:40 -0700395 @Override
396 public void setPressedIcon(BubbleTextView icon, Bitmap background) {
Sunny Goyal508da152014-08-14 10:53:27 -0700397 if (icon == null || background == null) {
398 mTouchFeedbackView.setBitmap(null);
399 mTouchFeedbackView.animate().cancel();
400 } else {
Sunny Goyal508da152014-08-14 10:53:27 -0700401 if (mTouchFeedbackView.setBitmap(background)) {
Winsonbe9798b2016-07-20 12:55:49 -0700402 mTouchFeedbackView.alignWithIconView(icon, mShortcutsAndWidgets,
403 null /* clipAgainstView */);
Sunny Goyal4fe5a372015-05-14 19:55:10 -0700404 mTouchFeedbackView.animateShadow();
Sunny Goyal508da152014-08-14 10:53:27 -0700405 }
Patrick Dubroy96864c32011-03-10 17:17:23 -0800406 }
407 }
408
Adam Cohenc50438c2014-08-19 17:43:05 -0700409 void disableDragTarget() {
410 mIsDragTarget = false;
411 }
412
Tony Wickham0f97b782015-12-02 17:55:07 -0800413 public boolean isDragTarget() {
414 return mIsDragTarget;
415 }
416
Adam Cohenc50438c2014-08-19 17:43:05 -0700417 void setIsDragOverlapping(boolean isDragOverlapping) {
418 if (mIsDragOverlapping != isDragOverlapping) {
419 mIsDragOverlapping = isDragOverlapping;
Sunny Goyalf5440cb2016-12-14 15:13:00 -0800420 mBackground.setState(mIsDragOverlapping
421 ? BACKGROUND_STATE_ACTIVE : BACKGROUND_STATE_DEFAULT);
Adam Cohenc50438c2014-08-19 17:43:05 -0700422 invalidate();
423 }
424 }
425
Sunny Goyale2fd14b2015-08-27 17:45:46 -0700426 public void disableJailContent() {
427 mJailContent = false;
428 }
429
430 @Override
431 protected void dispatchSaveInstanceState(SparseArray<Parcelable> container) {
432 if (mJailContent) {
433 ParcelableSparseArray jail = getJailedArray(container);
434 super.dispatchSaveInstanceState(jail);
435 container.put(R.id.cell_layout_jail_id, jail);
436 } else {
437 super.dispatchSaveInstanceState(container);
438 }
439 }
440
441 @Override
442 protected void dispatchRestoreInstanceState(SparseArray<Parcelable> container) {
443 super.dispatchRestoreInstanceState(mJailContent ? getJailedArray(container) : container);
444 }
445
446 private ParcelableSparseArray getJailedArray(SparseArray<Parcelable> container) {
447 final Parcelable parcelable = container.get(R.id.cell_layout_jail_id);
448 return parcelable instanceof ParcelableSparseArray ?
449 (ParcelableSparseArray) parcelable : new ParcelableSparseArray();
450 }
451
Tony Wickham0f97b782015-12-02 17:55:07 -0800452 public boolean getIsDragOverlapping() {
453 return mIsDragOverlapping;
454 }
455
Jeff Sharkey83f111d2009-04-20 21:03:13 -0700456 @Override
Patrick Dubroy1262e362010-10-06 15:49:50 -0700457 protected void onDraw(Canvas canvas) {
Sunny Goyal05739772015-05-19 19:59:09 -0700458 if (!mIsDragTarget) {
459 return;
460 }
461
Michael Jurka3e7c7632010-10-02 16:01:03 -0700462 // When we're large, we are either drawn in a "hover" state (ie when dragging an item to
463 // a neighboring page) or with just a normal background (if backgroundAlpha > 0.0f)
464 // When we're small, we are either drawn normally or in the "accepts drops" state (during
465 // a drag). However, we also drag the mini hover background *over* one of those two
466 // backgrounds
Sunny Goyal05739772015-05-19 19:59:09 -0700467 if (mBackgroundAlpha > 0.0f) {
Sunny Goyal2805e632015-05-20 15:35:32 -0700468 mBackground.draw(canvas);
Michael Jurkaa63c4522010-08-19 13:52:27 -0700469 }
Romain Guya6abce82009-11-10 02:54:41 -0800470
Patrick Dubroy8e58e912010-10-14 13:21:48 -0700471 final Paint paint = mDragOutlinePaint;
Joe Onorato4be866d2010-10-10 11:26:02 -0700472 for (int i = 0; i < mDragOutlines.length; i++) {
Chet Haase472b2812010-10-14 07:02:04 -0700473 final float alpha = mDragOutlineAlphas[i];
Joe Onorato4be866d2010-10-10 11:26:02 -0700474 if (alpha > 0) {
Joe Onorato4be866d2010-10-10 11:26:02 -0700475 final Bitmap b = (Bitmap) mDragOutlineAnims[i].getTag();
Chet Haase472b2812010-10-14 07:02:04 -0700476 paint.setAlpha((int)(alpha + .5f));
Sunny Goyal106bf642015-07-16 12:18:06 -0700477 canvas.drawBitmap(b, null, mDragOutlines[i], paint);
Winson Chung150fbab2010-09-29 17:14:26 -0700478 }
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700479 }
Patrick Dubroy96864c32011-03-10 17:17:23 -0800480
Adam Cohen482ed822012-03-02 14:15:13 -0800481 if (DEBUG_VISUALIZE_OCCUPIED) {
482 int[] pt = new int[2];
483 ColorDrawable cd = new ColorDrawable(Color.RED);
Adam Cohene7587d22012-05-24 18:50:02 -0700484 cd.setBounds(0, 0, mCellWidth, mCellHeight);
Adam Cohen482ed822012-03-02 14:15:13 -0800485 for (int i = 0; i < mCountX; i++) {
486 for (int j = 0; j < mCountY; j++) {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -0700487 if (mOccupied.cells[i][j]) {
Adam Cohen482ed822012-03-02 14:15:13 -0800488 cellToPoint(i, j, pt);
489 canvas.save();
490 canvas.translate(pt[0], pt[1]);
491 cd.draw(canvas);
492 canvas.restore();
493 }
494 }
495 }
496 }
497
Adam Cohenefca0272016-02-24 19:19:06 -0800498 for (int i = 0; i < mFolderBackgrounds.size(); i++) {
Jon Mirandaa0233f72017-06-22 18:34:45 -0700499 PreviewBackground bg = mFolderBackgrounds.get(i);
Adam Cohenefca0272016-02-24 19:19:06 -0800500 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.drawBackground(canvas);
Adam Cohenf172b742016-03-30 19:28:34 -0700504 if (!bg.isClipping) {
Sunny Goyal19b93b72017-02-19 20:21:37 -0800505 bg.drawBackgroundStroke(canvas);
Adam Cohenf172b742016-03-30 19:28:34 -0700506 }
Adam Cohenefca0272016-02-24 19:19:06 -0800507 canvas.restore();
Adam Cohen69ce2e52011-07-03 19:25:21 -0700508 }
Adam Cohenc51934b2011-07-26 21:07:43 -0700509
Adam Cohenefca0272016-02-24 19:19:06 -0800510 if (mFolderLeaveBehind.delegateCellX >= 0 && mFolderLeaveBehind.delegateCellY >= 0) {
511 cellToPoint(mFolderLeaveBehind.delegateCellX,
512 mFolderLeaveBehind.delegateCellY, mTempLocation);
513 canvas.save();
514 canvas.translate(mTempLocation[0], mTempLocation[1]);
Sunny Goyal19b93b72017-02-19 20:21:37 -0800515 mFolderLeaveBehind.drawLeaveBehind(canvas);
Adam Cohenefca0272016-02-24 19:19:06 -0800516 canvas.restore();
Adam Cohenc51934b2011-07-26 21:07:43 -0700517 }
Adam Cohen69ce2e52011-07-03 19:25:21 -0700518 }
519
Adam Cohenefca0272016-02-24 19:19:06 -0800520 @Override
521 protected void dispatchDraw(Canvas canvas) {
522 super.dispatchDraw(canvas);
523
524 for (int i = 0; i < mFolderBackgrounds.size(); i++) {
Jon Mirandaa0233f72017-06-22 18:34:45 -0700525 PreviewBackground bg = mFolderBackgrounds.get(i);
Adam Cohenf172b742016-03-30 19:28:34 -0700526 if (bg.isClipping) {
527 cellToPoint(bg.delegateCellX, bg.delegateCellY, mTempLocation);
528 canvas.save();
529 canvas.translate(mTempLocation[0], mTempLocation[1]);
Sunny Goyal19b93b72017-02-19 20:21:37 -0800530 bg.drawBackgroundStroke(canvas);
Adam Cohenf172b742016-03-30 19:28:34 -0700531 canvas.restore();
532 }
Adam Cohenefca0272016-02-24 19:19:06 -0800533 }
Adam Cohen69ce2e52011-07-03 19:25:21 -0700534 }
535
Jon Mirandaa0233f72017-06-22 18:34:45 -0700536 public void addFolderBackground(PreviewBackground bg) {
Adam Cohenefca0272016-02-24 19:19:06 -0800537 mFolderBackgrounds.add(bg);
538 }
Jon Mirandaa0233f72017-06-22 18:34:45 -0700539 public void removeFolderBackground(PreviewBackground bg) {
Adam Cohenefca0272016-02-24 19:19:06 -0800540 mFolderBackgrounds.remove(bg);
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700541 }
542
Adam Cohenc51934b2011-07-26 21:07:43 -0700543 public void setFolderLeaveBehindCell(int x, int y) {
Adam Cohenefca0272016-02-24 19:19:06 -0800544 View child = getChildAt(x, y);
Sunny Goyal368ae772017-05-24 13:19:15 -0700545 mFolderLeaveBehind.setup(mLauncher, null,
Adam Cohenefca0272016-02-24 19:19:06 -0800546 child.getMeasuredWidth(), child.getPaddingTop());
547
548 mFolderLeaveBehind.delegateCellX = x;
549 mFolderLeaveBehind.delegateCellY = y;
Adam Cohenc51934b2011-07-26 21:07:43 -0700550 invalidate();
551 }
552
553 public void clearFolderLeaveBehind() {
Adam Cohenefca0272016-02-24 19:19:06 -0800554 mFolderLeaveBehind.delegateCellX = -1;
555 mFolderLeaveBehind.delegateCellY = -1;
Adam Cohenc51934b2011-07-26 21:07:43 -0700556 invalidate();
557 }
558
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700559 @Override
Michael Jurkae6235dd2011-10-04 15:02:05 -0700560 public boolean shouldDelayChildPressedState() {
561 return false;
562 }
563
Adam Cohen1462de32012-07-24 22:34:36 -0700564 public void restoreInstanceState(SparseArray<Parcelable> states) {
Sunny Goyal33a152f2014-07-22 12:13:14 -0700565 try {
566 dispatchRestoreInstanceState(states);
567 } catch (IllegalArgumentException ex) {
Sunny Goyal3d706ad2017-03-06 16:56:39 -0800568 if (FeatureFlags.IS_DOGFOOD_BUILD) {
Sunny Goyal33a152f2014-07-22 12:13:14 -0700569 throw ex;
570 }
571 // Mismatched viewId / viewType preventing restore. Skip restore on production builds.
572 Log.e(TAG, "Ignoring an error while restoring a view instance state", ex);
573 }
Adam Cohen1462de32012-07-24 22:34:36 -0700574 }
575
Michael Jurkae6235dd2011-10-04 15:02:05 -0700576 @Override
Jeff Sharkey83f111d2009-04-20 21:03:13 -0700577 public void cancelLongPress() {
578 super.cancelLongPress();
579
580 // Cancel long press for all children
581 final int count = getChildCount();
582 for (int i = 0; i < count; i++) {
583 final View child = getChildAt(i);
584 child.cancelLongPress();
585 }
586 }
587
Michael Jurkadee05892010-07-27 10:01:56 -0700588 public void setOnInterceptTouchListener(View.OnTouchListener listener) {
589 mInterceptTouchListener = listener;
590 }
591
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800592 public int getCountX() {
Adam Cohend22015c2010-07-26 22:02:18 -0700593 return mCountX;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800594 }
595
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800596 public int getCountY() {
Adam Cohend22015c2010-07-26 22:02:18 -0700597 return mCountY;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800598 }
599
Sunny Goyalc13403c2016-11-18 23:44:48 -0800600 public boolean acceptsWidget() {
601 return mContainerType == WORKSPACE;
Sunny Goyale9b651e2015-04-24 11:44:51 -0700602 }
603
Andrew Flynn0dca1ec2012-02-29 13:33:22 -0800604 public boolean addViewToCellLayout(View child, int index, int childId, LayoutParams params,
Sunny Goyalf7a29e82015-04-24 15:20:43 -0700605 boolean markCells) {
Winson Chungaafa03c2010-06-11 17:34:16 -0700606 final LayoutParams lp = params;
607
Andrew Flynnde38e422012-05-08 11:22:15 -0700608 // Hotseat icons - remove text
Andrew Flynn0dca1ec2012-02-29 13:33:22 -0800609 if (child instanceof BubbleTextView) {
610 BubbleTextView bubbleChild = (BubbleTextView) child;
Sunny Goyalc13403c2016-11-18 23:44:48 -0800611 bubbleChild.setTextVisibility(mContainerType != HOTSEAT);
Andrew Flynn0dca1ec2012-02-29 13:33:22 -0800612 }
613
Sunny Goyalc13403c2016-11-18 23:44:48 -0800614 child.setScaleX(mChildScale);
615 child.setScaleY(mChildScale);
Adam Cohen307fe232012-08-16 17:55:58 -0700616
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800617 // Generate an id for each view, this assumes we have at most 256x256 cells
618 // per workspace screen
Adam Cohend22015c2010-07-26 22:02:18 -0700619 if (lp.cellX >= 0 && lp.cellX <= mCountX - 1 && lp.cellY >= 0 && lp.cellY <= mCountY - 1) {
Winson Chungaafa03c2010-06-11 17:34:16 -0700620 // If the horizontal or vertical span is set to -1, it is taken to
621 // mean that it spans the extent of the CellLayout
Adam Cohend22015c2010-07-26 22:02:18 -0700622 if (lp.cellHSpan < 0) lp.cellHSpan = mCountX;
623 if (lp.cellVSpan < 0) lp.cellVSpan = mCountY;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800624
Winson Chungaafa03c2010-06-11 17:34:16 -0700625 child.setId(childId);
Tony Wickhama0628cc2015-10-14 15:23:04 -0700626 if (LOGD) {
627 Log.d(TAG, "Adding view to ShortcutsAndWidgetsContainer: " + child);
628 }
Sunny Goyalf7a29e82015-04-24 15:20:43 -0700629 mShortcutsAndWidgets.addView(child, index, lp);
Michael Jurkadee05892010-07-27 10:01:56 -0700630
Michael Jurkaf3ca3ab2010-10-20 17:08:24 -0700631 if (markCells) markCellsAsOccupiedForView(child);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700632
Winson Chungaafa03c2010-06-11 17:34:16 -0700633 return true;
634 }
635 return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800636 }
Michael Jurka3e7c7632010-10-02 16:01:03 -0700637
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800638 @Override
Michael Jurka0280c3b2010-09-17 15:00:07 -0700639 public void removeAllViews() {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -0700640 mOccupied.clear();
Michael Jurkaa52570f2012-03-20 03:18:20 -0700641 mShortcutsAndWidgets.removeAllViews();
Michael Jurka0280c3b2010-09-17 15:00:07 -0700642 }
643
644 @Override
645 public void removeAllViewsInLayout() {
Michael Jurkaa52570f2012-03-20 03:18:20 -0700646 if (mShortcutsAndWidgets.getChildCount() > 0) {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -0700647 mOccupied.clear();
Michael Jurkaa52570f2012-03-20 03:18:20 -0700648 mShortcutsAndWidgets.removeAllViewsInLayout();
Michael Jurka7cfc2822011-08-02 20:19:24 -0700649 }
Michael Jurka0280c3b2010-09-17 15:00:07 -0700650 }
651
652 @Override
653 public void removeView(View view) {
654 markCellsAsUnoccupiedForView(view);
Michael Jurkaa52570f2012-03-20 03:18:20 -0700655 mShortcutsAndWidgets.removeView(view);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700656 }
657
658 @Override
659 public void removeViewAt(int index) {
Michael Jurkaa52570f2012-03-20 03:18:20 -0700660 markCellsAsUnoccupiedForView(mShortcutsAndWidgets.getChildAt(index));
661 mShortcutsAndWidgets.removeViewAt(index);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700662 }
663
664 @Override
665 public void removeViewInLayout(View view) {
666 markCellsAsUnoccupiedForView(view);
Michael Jurkaa52570f2012-03-20 03:18:20 -0700667 mShortcutsAndWidgets.removeViewInLayout(view);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700668 }
669
670 @Override
671 public void removeViews(int start, int count) {
672 for (int i = start; i < start + count; i++) {
Michael Jurkaa52570f2012-03-20 03:18:20 -0700673 markCellsAsUnoccupiedForView(mShortcutsAndWidgets.getChildAt(i));
Michael Jurka0280c3b2010-09-17 15:00:07 -0700674 }
Michael Jurkaa52570f2012-03-20 03:18:20 -0700675 mShortcutsAndWidgets.removeViews(start, count);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700676 }
677
678 @Override
679 public void removeViewsInLayout(int start, int count) {
680 for (int i = start; i < start + count; i++) {
Michael Jurkaa52570f2012-03-20 03:18:20 -0700681 markCellsAsUnoccupiedForView(mShortcutsAndWidgets.getChildAt(i));
Michael Jurka0280c3b2010-09-17 15:00:07 -0700682 }
Michael Jurkaa52570f2012-03-20 03:18:20 -0700683 mShortcutsAndWidgets.removeViewsInLayout(start, count);
Michael Jurkaabded662011-03-04 12:06:57 -0800684 }
685
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700686 /**
Winson Chungaafa03c2010-06-11 17:34:16 -0700687 * Given a point, return the cell that strictly encloses that point
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800688 * @param x X coordinate of the point
689 * @param y Y coordinate of the point
690 * @param result Array of 2 ints to hold the x and y coordinate of the cell
691 */
Sunny Goyale9b651e2015-04-24 11:44:51 -0700692 public void pointToCellExact(int x, int y, int[] result) {
Winson Chung4b825dcd2011-06-19 12:41:22 -0700693 final int hStartPadding = getPaddingLeft();
694 final int vStartPadding = getPaddingTop();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800695
Sunny Goyalaa8a8712016-11-20 15:26:01 +0530696 result[0] = (x - hStartPadding) / mCellWidth;
697 result[1] = (y - vStartPadding) / mCellHeight;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800698
Adam Cohend22015c2010-07-26 22:02:18 -0700699 final int xAxis = mCountX;
700 final int yAxis = mCountY;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800701
702 if (result[0] < 0) result[0] = 0;
703 if (result[0] >= xAxis) result[0] = xAxis - 1;
704 if (result[1] < 0) result[1] = 0;
705 if (result[1] >= yAxis) result[1] = yAxis - 1;
706 }
Winson Chungaafa03c2010-06-11 17:34:16 -0700707
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800708 /**
709 * Given a point, return the cell that most closely encloses that point
710 * @param x X coordinate of the point
711 * @param y Y coordinate of the point
712 * @param result Array of 2 ints to hold the x and y coordinate of the cell
713 */
714 void pointToCellRounded(int x, int y, int[] result) {
715 pointToCellExact(x + (mCellWidth / 2), y + (mCellHeight / 2), result);
716 }
717
718 /**
719 * Given a cell coordinate, return the point that represents the upper left corner of that cell
Winson Chungaafa03c2010-06-11 17:34:16 -0700720 *
721 * @param cellX X coordinate of the cell
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800722 * @param cellY Y coordinate of the cell
Winson Chungaafa03c2010-06-11 17:34:16 -0700723 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800724 * @param result Array of 2 ints to hold the x and y coordinate of the point
725 */
726 void cellToPoint(int cellX, int cellY, int[] result) {
Winson Chung4b825dcd2011-06-19 12:41:22 -0700727 final int hStartPadding = getPaddingLeft();
728 final int vStartPadding = getPaddingTop();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800729
Sunny Goyalaa8a8712016-11-20 15:26:01 +0530730 result[0] = hStartPadding + cellX * mCellWidth;
731 result[1] = vStartPadding + cellY * mCellHeight;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800732 }
733
Adam Cohene3e27a82011-04-15 12:07:39 -0700734 /**
Adam Cohen482ed822012-03-02 14:15:13 -0800735 * Given a cell coordinate, return the point that represents the center of the cell
Adam Cohene3e27a82011-04-15 12:07:39 -0700736 *
737 * @param cellX X coordinate of the cell
738 * @param cellY Y coordinate of the cell
739 *
740 * @param result Array of 2 ints to hold the x and y coordinate of the point
741 */
742 void cellToCenterPoint(int cellX, int cellY, int[] result) {
Adam Cohen47a876d2012-03-19 13:21:41 -0700743 regionToCenterPoint(cellX, cellY, 1, 1, result);
744 }
745
746 /**
747 * Given a cell coordinate and span return the point that represents the center of the regio
748 *
749 * @param cellX X coordinate of the cell
750 * @param cellY Y coordinate of the cell
751 *
752 * @param result Array of 2 ints to hold the x and y coordinate of the point
753 */
754 void regionToCenterPoint(int cellX, int cellY, int spanX, int spanY, int[] result) {
Winson Chung4b825dcd2011-06-19 12:41:22 -0700755 final int hStartPadding = getPaddingLeft();
756 final int vStartPadding = getPaddingTop();
Sunny Goyalaa8a8712016-11-20 15:26:01 +0530757 result[0] = hStartPadding + cellX * mCellWidth + (spanX * mCellWidth) / 2;
758 result[1] = vStartPadding + cellY * mCellHeight + (spanY * mCellHeight) / 2;
Adam Cohene3e27a82011-04-15 12:07:39 -0700759 }
760
Adam Cohen19f37922012-03-21 11:59:11 -0700761 /**
762 * Given a cell coordinate and span fills out a corresponding pixel rect
763 *
764 * @param cellX X coordinate of the cell
765 * @param cellY Y coordinate of the cell
766 * @param result Rect in which to write the result
767 */
768 void regionToRect(int cellX, int cellY, int spanX, int spanY, Rect result) {
769 final int hStartPadding = getPaddingLeft();
770 final int vStartPadding = getPaddingTop();
Sunny Goyalaa8a8712016-11-20 15:26:01 +0530771 final int left = hStartPadding + cellX * mCellWidth;
772 final int top = vStartPadding + cellY * mCellHeight;
773 result.set(left, top, left + (spanX * mCellWidth), top + (spanY * mCellHeight));
Adam Cohen19f37922012-03-21 11:59:11 -0700774 }
775
Adam Cohen482ed822012-03-02 14:15:13 -0800776 public float getDistanceFromCell(float x, float y, int[] cell) {
777 cellToCenterPoint(cell[0], cell[1], mTmpPoint);
Sunny Goyalf7a29e82015-04-24 15:20:43 -0700778 return (float) Math.hypot(x - mTmpPoint[0], y - mTmpPoint[1]);
Adam Cohen482ed822012-03-02 14:15:13 -0800779 }
780
Adam Cohenf9c184a2016-01-15 16:47:43 -0800781 public int getCellWidth() {
Romain Guy84f296c2009-11-04 15:00:44 -0800782 return mCellWidth;
783 }
784
785 int getCellHeight() {
786 return mCellHeight;
787 }
788
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700789 public void setFixedSize(int width, int height) {
790 mFixedWidth = width;
791 mFixedHeight = height;
792 }
793
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800794 @Override
795 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800796 int widthSpecMode = MeasureSpec.getMode(widthMeasureSpec);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800797 int heightSpecMode = MeasureSpec.getMode(heightMeasureSpec);
Winson Chung5f8afe62013-08-12 16:19:28 -0700798 int widthSize = MeasureSpec.getSize(widthMeasureSpec);
799 int heightSize = MeasureSpec.getSize(heightMeasureSpec);
Winson Chung2d75f122013-09-23 16:53:31 -0700800 int childWidthSize = widthSize - (getPaddingLeft() + getPaddingRight());
801 int childHeightSize = heightSize - (getPaddingTop() + getPaddingBottom());
Winson Chung11a1a532013-09-13 11:14:45 -0700802 if (mFixedCellWidth < 0 || mFixedCellHeight < 0) {
Sunny Goyalc6205602015-05-21 20:46:33 -0700803 int cw = DeviceProfile.calculateCellWidth(childWidthSize, mCountX);
804 int ch = DeviceProfile.calculateCellHeight(childHeightSize, mCountY);
Winson Chung11a1a532013-09-13 11:14:45 -0700805 if (cw != mCellWidth || ch != mCellHeight) {
806 mCellWidth = cw;
807 mCellHeight = ch;
Sunny Goyalaa8a8712016-11-20 15:26:01 +0530808 mShortcutsAndWidgets.setCellDimensions(mCellWidth, mCellHeight, mCountX, mCountY);
Winson Chung11a1a532013-09-13 11:14:45 -0700809 }
Winson Chung5f8afe62013-08-12 16:19:28 -0700810 }
Winson Chungaafa03c2010-06-11 17:34:16 -0700811
Winson Chung2d75f122013-09-23 16:53:31 -0700812 int newWidth = childWidthSize;
813 int newHeight = childHeightSize;
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700814 if (mFixedWidth > 0 && mFixedHeight > 0) {
815 newWidth = mFixedWidth;
816 newHeight = mFixedHeight;
817 } else if (widthSpecMode == MeasureSpec.UNSPECIFIED || heightSpecMode == MeasureSpec.UNSPECIFIED) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800818 throw new RuntimeException("CellLayout cannot have UNSPECIFIED dimensions");
819 }
820
Sunny Goyal4fe5a372015-05-14 19:55:10 -0700821 // Make the feedback view large enough to hold the blur bitmap.
822 mTouchFeedbackView.measure(
823 MeasureSpec.makeMeasureSpec(mCellWidth + mTouchFeedbackView.getExtraSize(),
824 MeasureSpec.EXACTLY),
825 MeasureSpec.makeMeasureSpec(mCellHeight + mTouchFeedbackView.getExtraSize(),
826 MeasureSpec.EXACTLY));
827
828 mShortcutsAndWidgets.measure(
829 MeasureSpec.makeMeasureSpec(newWidth, MeasureSpec.EXACTLY),
830 MeasureSpec.makeMeasureSpec(newHeight, MeasureSpec.EXACTLY));
831
832 int maxWidth = mShortcutsAndWidgets.getMeasuredWidth();
833 int maxHeight = mShortcutsAndWidgets.getMeasuredHeight();
Winson Chung2d75f122013-09-23 16:53:31 -0700834 if (mFixedWidth > 0 && mFixedHeight > 0) {
835 setMeasuredDimension(maxWidth, maxHeight);
836 } else {
837 setMeasuredDimension(widthSize, heightSize);
838 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800839 }
840
841 @Override
Michael Jurka28750fb2010-09-24 17:43:49 -0700842 protected void onLayout(boolean changed, int l, int t, int r, int b) {
Tony Wickham26b01422015-11-10 14:44:32 -0800843 boolean isFullscreen = mShortcutsAndWidgets.getChildCount() > 0 &&
844 ((LayoutParams) mShortcutsAndWidgets.getChildAt(0).getLayoutParams()).isFullscreen;
845 int left = getPaddingLeft();
846 if (!isFullscreen) {
Tony Wickhama501d492015-11-03 18:05:01 -0800847 left += (int) Math.ceil(getUnusedHorizontalSpace() / 2f);
Tony Wickham26b01422015-11-10 14:44:32 -0800848 }
Sunny Goyal7c786f72016-06-01 14:08:21 -0700849 int right = r - l - getPaddingRight();
850 if (!isFullscreen) {
851 right -= (int) Math.ceil(getUnusedHorizontalSpace() / 2f);
852 }
853
Winson Chung38848ca2013-10-08 12:03:44 -0700854 int top = getPaddingTop();
Sunny Goyal7c786f72016-06-01 14:08:21 -0700855 int bottom = b - t - getPaddingBottom();
Sunny Goyal4fe5a372015-05-14 19:55:10 -0700856
857 mTouchFeedbackView.layout(left, top,
858 left + mTouchFeedbackView.getMeasuredWidth(),
859 top + mTouchFeedbackView.getMeasuredHeight());
Sunny Goyal7c786f72016-06-01 14:08:21 -0700860 mShortcutsAndWidgets.layout(left, top, right, bottom);
861
862 // Expand the background drawing bounds by the padding baked into the background drawable
863 mBackground.getPadding(mTempRect);
864 mBackground.setBounds(
865 left - mTempRect.left,
866 top - mTempRect.top,
867 right + mTempRect.right,
868 bottom + mTempRect.bottom);
869 }
870
Tony Wickhama501d492015-11-03 18:05:01 -0800871 /**
872 * Returns the amount of space left over after subtracting padding and cells. This space will be
873 * very small, a few pixels at most, and is a result of rounding down when calculating the cell
874 * width in {@link DeviceProfile#calculateCellWidth(int, int)}.
875 */
876 public int getUnusedHorizontalSpace() {
877 return getMeasuredWidth() - getPaddingLeft() - getPaddingRight() - (mCountX * mCellWidth);
878 }
879
Michael Jurka5f1c5092010-09-03 14:15:02 -0700880 public float getBackgroundAlpha() {
881 return mBackgroundAlpha;
Michael Jurkadee05892010-07-27 10:01:56 -0700882 }
883
Michael Jurka5f1c5092010-09-03 14:15:02 -0700884 public void setBackgroundAlpha(float alpha) {
Michael Jurkaafaa0502011-12-13 18:22:50 -0800885 if (mBackgroundAlpha != alpha) {
886 mBackgroundAlpha = alpha;
Sunny Goyal2805e632015-05-20 15:35:32 -0700887 mBackground.setAlpha((int) (mBackgroundAlpha * 255));
Michael Jurkaafaa0502011-12-13 18:22:50 -0800888 }
Michael Jurkadee05892010-07-27 10:01:56 -0700889 }
890
Sunny Goyal2805e632015-05-20 15:35:32 -0700891 @Override
892 protected boolean verifyDrawable(Drawable who) {
893 return super.verifyDrawable(who) || (mIsDragTarget && who == mBackground);
894 }
895
Michael Jurkaa52570f2012-03-20 03:18:20 -0700896 public void setShortcutAndWidgetAlpha(float alpha) {
Sunny Goyal02b50812014-09-10 15:44:42 -0700897 mShortcutsAndWidgets.setAlpha(alpha);
Michael Jurkadee05892010-07-27 10:01:56 -0700898 }
899
Michael Jurkaa52570f2012-03-20 03:18:20 -0700900 public ShortcutAndWidgetContainer getShortcutsAndWidgets() {
Sunny Goyaldcbcc862014-08-12 15:58:36 -0700901 return mShortcutsAndWidgets;
Michael Jurkaa52570f2012-03-20 03:18:20 -0700902 }
903
Patrick Dubroy440c3602010-07-13 17:50:32 -0700904 public View getChildAt(int x, int y) {
Michael Jurkaa52570f2012-03-20 03:18:20 -0700905 return mShortcutsAndWidgets.getChildAt(x, y);
Patrick Dubroy440c3602010-07-13 17:50:32 -0700906 }
907
Adam Cohen76fc0852011-06-17 13:26:23 -0700908 public boolean animateChildToPosition(final View child, int cellX, int cellY, int duration,
Adam Cohen482ed822012-03-02 14:15:13 -0800909 int delay, boolean permanent, boolean adjustOccupied) {
Michael Jurkaa52570f2012-03-20 03:18:20 -0700910 ShortcutAndWidgetContainer clc = getShortcutsAndWidgets();
Adam Cohen482ed822012-03-02 14:15:13 -0800911
Adam Cohen19f37922012-03-21 11:59:11 -0700912 if (clc.indexOfChild(child) != -1) {
Adam Cohenbfbfd262011-06-13 16:55:12 -0700913 final LayoutParams lp = (LayoutParams) child.getLayoutParams();
914 final ItemInfo info = (ItemInfo) child.getTag();
915
916 // We cancel any existing animations
917 if (mReorderAnimators.containsKey(lp)) {
918 mReorderAnimators.get(lp).cancel();
919 mReorderAnimators.remove(lp);
920 }
921
Adam Cohen482ed822012-03-02 14:15:13 -0800922 final int oldX = lp.x;
923 final int oldY = lp.y;
924 if (adjustOccupied) {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -0700925 GridOccupancy occupied = permanent ? mOccupied : mTmpOccupied;
926 occupied.markCells(lp.cellX, lp.cellY, lp.cellHSpan, lp.cellVSpan, false);
927 occupied.markCells(cellX, cellY, lp.cellHSpan, lp.cellVSpan, true);
Adam Cohen482ed822012-03-02 14:15:13 -0800928 }
Adam Cohenbfbfd262011-06-13 16:55:12 -0700929 lp.isLockedToGrid = true;
Adam Cohen482ed822012-03-02 14:15:13 -0800930 if (permanent) {
931 lp.cellX = info.cellX = cellX;
932 lp.cellY = info.cellY = cellY;
933 } else {
934 lp.tmpCellX = cellX;
935 lp.tmpCellY = cellY;
936 }
Jon Mirandae96798e2016-12-07 12:10:44 -0800937 clc.setupLp(child);
Adam Cohenbfbfd262011-06-13 16:55:12 -0700938 lp.isLockedToGrid = false;
Adam Cohen482ed822012-03-02 14:15:13 -0800939 final int newX = lp.x;
940 final int newY = lp.y;
Adam Cohenbfbfd262011-06-13 16:55:12 -0700941
Adam Cohen76fc0852011-06-17 13:26:23 -0700942 lp.x = oldX;
943 lp.y = oldY;
Adam Cohen76fc0852011-06-17 13:26:23 -0700944
Adam Cohen482ed822012-03-02 14:15:13 -0800945 // Exit early if we're not actually moving the view
946 if (oldX == newX && oldY == newY) {
947 lp.isLockedToGrid = true;
948 return true;
949 }
950
Jon Mirandacda3bfb2017-02-06 15:54:41 -0800951 ValueAnimator va = LauncherAnimUtils.ofFloat(0f, 1f);
Adam Cohen482ed822012-03-02 14:15:13 -0800952 va.setDuration(duration);
953 mReorderAnimators.put(lp, va);
954
955 va.addUpdateListener(new AnimatorUpdateListener() {
956 @Override
Adam Cohenbfbfd262011-06-13 16:55:12 -0700957 public void onAnimationUpdate(ValueAnimator animation) {
Jon Mirandae96798e2016-12-07 12:10:44 -0800958 float r = (Float) animation.getAnimatedValue();
Adam Cohen19f37922012-03-21 11:59:11 -0700959 lp.x = (int) ((1 - r) * oldX + r * newX);
960 lp.y = (int) ((1 - r) * oldY + r * newY);
Adam Cohen6b8a02d2012-03-22 15:13:40 -0700961 child.requestLayout();
Adam Cohenbfbfd262011-06-13 16:55:12 -0700962 }
963 });
Adam Cohen482ed822012-03-02 14:15:13 -0800964 va.addListener(new AnimatorListenerAdapter() {
Adam Cohenbfbfd262011-06-13 16:55:12 -0700965 boolean cancelled = false;
966 public void onAnimationEnd(Animator animation) {
967 // If the animation was cancelled, it means that another animation
968 // has interrupted this one, and we don't want to lock the item into
969 // place just yet.
970 if (!cancelled) {
971 lp.isLockedToGrid = true;
Adam Cohen482ed822012-03-02 14:15:13 -0800972 child.requestLayout();
Adam Cohenbfbfd262011-06-13 16:55:12 -0700973 }
974 if (mReorderAnimators.containsKey(lp)) {
975 mReorderAnimators.remove(lp);
976 }
977 }
978 public void onAnimationCancel(Animator animation) {
979 cancelled = true;
980 }
981 });
Adam Cohen482ed822012-03-02 14:15:13 -0800982 va.setStartDelay(delay);
983 va.start();
Adam Cohenbfbfd262011-06-13 16:55:12 -0700984 return true;
985 }
986 return false;
987 }
988
Sunny Goyal06e21a22016-08-11 16:02:02 -0700989 void visualizeDropLocation(View v, DragPreviewProvider outlineProvider, int cellX, int cellY,
990 int spanX, int spanY, boolean resize, DropTarget.DragObject dragObject) {
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -0700991 final int oldDragCellX = mDragCell[0];
992 final int oldDragCellY = mDragCell[1];
Adam Cohen482ed822012-03-02 14:15:13 -0800993
Hyunyoung Song0de01172016-10-05 16:27:48 -0700994 if (outlineProvider == null || outlineProvider.generatedDragOutline == null) {
Adam Cohen2801caf2011-05-13 20:57:39 -0700995 return;
996 }
997
Hyunyoung Song0de01172016-10-05 16:27:48 -0700998 Bitmap dragOutline = outlineProvider.generatedDragOutline;
Adam Cohen482ed822012-03-02 14:15:13 -0800999 if (cellX != oldDragCellX || cellY != oldDragCellY) {
Sunny Goyale78e3d72015-09-24 11:23:31 -07001000 Point dragOffset = dragObject.dragView.getDragVisualizeOffset();
1001 Rect dragRegion = dragObject.dragView.getDragRegion();
1002
Adam Cohen482ed822012-03-02 14:15:13 -08001003 mDragCell[0] = cellX;
1004 mDragCell[1] = cellY;
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001005
Joe Onorato4be866d2010-10-10 11:26:02 -07001006 final int oldIndex = mDragOutlineCurrent;
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -07001007 mDragOutlineAnims[oldIndex].animateOut();
1008 mDragOutlineCurrent = (oldIndex + 1) % mDragOutlines.length;
Adam Cohend41fbf52012-02-16 23:53:59 -08001009 Rect r = mDragOutlines[mDragOutlineCurrent];
Sunny Goyal106bf642015-07-16 12:18:06 -07001010
Adam Cohend41fbf52012-02-16 23:53:59 -08001011 if (resize) {
Adam Cohen482ed822012-03-02 14:15:13 -08001012 cellToRect(cellX, cellY, spanX, spanY, r);
Jon Mirandae96798e2016-12-07 12:10:44 -08001013 if (v instanceof LauncherAppWidgetHostView) {
1014 DeviceProfile profile = mLauncher.getDeviceProfile();
Jon Miranda6f6a06a2016-12-15 11:24:18 -08001015 Utilities.shrinkRect(r, profile.appWidgetScale.x, profile.appWidgetScale.y);
Jon Mirandae96798e2016-12-07 12:10:44 -08001016 }
Sunny Goyal106bf642015-07-16 12:18:06 -07001017 } else {
1018 // Find the top left corner of the rect the object will occupy
1019 final int[] topLeft = mTmpPoint;
1020 cellToPoint(cellX, cellY, topLeft);
1021
1022 int left = topLeft[0];
1023 int top = topLeft[1];
1024
1025 if (v != null && dragOffset == null) {
1026 // When drawing the drag outline, it did not account for margin offsets
1027 // added by the view's parent.
1028 MarginLayoutParams lp = (MarginLayoutParams) v.getLayoutParams();
1029 left += lp.leftMargin;
1030 top += lp.topMargin;
1031
1032 // Offsets due to the size difference between the View and the dragOutline.
1033 // There is a size difference to account for the outer blur, which may lie
1034 // outside the bounds of the view.
Jon Mirandaf7ff3fe2016-12-05 12:04:44 -08001035 top += ((mCellHeight * spanY) - dragOutline.getHeight()) / 2;
Sunny Goyal106bf642015-07-16 12:18:06 -07001036 // We center about the x axis
Sunny Goyalaa8a8712016-11-20 15:26:01 +05301037 left += ((mCellWidth * spanX) - dragOutline.getWidth()) / 2;
Sunny Goyal106bf642015-07-16 12:18:06 -07001038 } else {
1039 if (dragOffset != null && dragRegion != null) {
1040 // Center the drag region *horizontally* in the cell and apply a drag
1041 // outline offset
Sunny Goyalaa8a8712016-11-20 15:26:01 +05301042 left += dragOffset.x + ((mCellWidth * spanX) - dragRegion.width()) / 2;
Sunny Goyal106bf642015-07-16 12:18:06 -07001043 int cHeight = getShortcutsAndWidgets().getCellContentHeight();
1044 int cellPaddingY = (int) Math.max(0, ((mCellHeight - cHeight) / 2f));
1045 top += dragOffset.y + cellPaddingY;
1046 } else {
1047 // Center the drag outline in the cell
Sunny Goyalaa8a8712016-11-20 15:26:01 +05301048 left += ((mCellWidth * spanX) - dragOutline.getWidth()) / 2;
1049 top += ((mCellHeight * spanY) - dragOutline.getHeight()) / 2;
Sunny Goyal106bf642015-07-16 12:18:06 -07001050 }
1051 }
1052 r.set(left, top, left + dragOutline.getWidth(), top + dragOutline.getHeight());
Adam Cohend41fbf52012-02-16 23:53:59 -08001053 }
Winson Chung150fbab2010-09-29 17:14:26 -07001054
Jon Miranda6f6a06a2016-12-15 11:24:18 -08001055 Utilities.scaleRectAboutCenter(r, mChildScale);
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -07001056 mDragOutlineAnims[mDragOutlineCurrent].setTag(dragOutline);
1057 mDragOutlineAnims[mDragOutlineCurrent].animateIn();
Sunny Goyale78e3d72015-09-24 11:23:31 -07001058
1059 if (dragObject.stateAnnouncer != null) {
Sunny Goyalc13403c2016-11-18 23:44:48 -08001060 dragObject.stateAnnouncer.announce(getItemMoveDescription(cellX, cellY));
Sunny Goyale78e3d72015-09-24 11:23:31 -07001061 }
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001062 }
1063 }
1064
Sunny Goyalc13403c2016-11-18 23:44:48 -08001065 public String getItemMoveDescription(int cellX, int cellY) {
1066 if (mContainerType == HOTSEAT) {
1067 return getContext().getString(R.string.move_to_hotseat_position,
1068 Math.max(cellX, cellY) + 1);
1069 } else {
1070 return getContext().getString(R.string.move_to_empty_cell,
1071 cellY + 1, cellX + 1);
1072 }
1073 }
1074
Adam Cohene0310962011-04-18 16:15:31 -07001075 public void clearDragOutlines() {
1076 final int oldIndex = mDragOutlineCurrent;
1077 mDragOutlineAnims[oldIndex].animateOut();
Adam Cohend41fbf52012-02-16 23:53:59 -08001078 mDragCell[0] = mDragCell[1] = -1;
Adam Cohene0310962011-04-18 16:15:31 -07001079 }
1080
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001081 /**
Jeff Sharkey70864282009-04-07 21:08:40 -07001082 * Find a vacant area that will fit the given bounds nearest the requested
1083 * cell location. Uses Euclidean distance to score multiple vacant areas.
Winson Chungaafa03c2010-06-11 17:34:16 -07001084 *
Romain Guy51afc022009-05-04 18:03:43 -07001085 * @param pixelX The X location at which you want to search for a vacant area.
1086 * @param pixelY The Y location at which you want to search for a vacant area.
Adam Cohend41fbf52012-02-16 23:53:59 -08001087 * @param minSpanX The minimum horizontal span required
1088 * @param minSpanY The minimum vertical span required
1089 * @param spanX Horizontal span of the object.
1090 * @param spanY Vertical span of the object.
1091 * @param result Array in which to place the result, or null (in which case a new array will
1092 * be allocated)
1093 * @return The X, Y cell of a vacant area that can contain this object,
1094 * nearest the requested location.
1095 */
1096 int[] findNearestVacantArea(int pixelX, int pixelY, int minSpanX, int minSpanY, int spanX,
1097 int spanY, int[] result, int[] resultSpan) {
Sunny Goyalf7a29e82015-04-24 15:20:43 -07001098 return findNearestArea(pixelX, pixelY, minSpanX, minSpanY, spanX, spanY, true,
Adam Cohend41fbf52012-02-16 23:53:59 -08001099 result, resultSpan);
1100 }
1101
Rajeev Kumar9962dbe2017-06-12 12:16:20 -07001102 private final Stack<Rect> mTempRectStack = new Stack<>();
Adam Cohend41fbf52012-02-16 23:53:59 -08001103 private void lazyInitTempRectStack() {
1104 if (mTempRectStack.isEmpty()) {
1105 for (int i = 0; i < mCountX * mCountY; i++) {
1106 mTempRectStack.push(new Rect());
1107 }
1108 }
1109 }
Adam Cohen482ed822012-03-02 14:15:13 -08001110
Adam Cohend41fbf52012-02-16 23:53:59 -08001111 private void recycleTempRects(Stack<Rect> used) {
1112 while (!used.isEmpty()) {
1113 mTempRectStack.push(used.pop());
1114 }
1115 }
1116
1117 /**
1118 * Find a vacant area that will fit the given bounds nearest the requested
1119 * cell location. Uses Euclidean distance to score multiple vacant areas.
1120 *
1121 * @param pixelX The X location at which you want to search for a vacant area.
1122 * @param pixelY The Y location at which you want to search for a vacant area.
1123 * @param minSpanX The minimum horizontal span required
1124 * @param minSpanY The minimum vertical span required
1125 * @param spanX Horizontal span of the object.
1126 * @param spanY Vertical span of the object.
1127 * @param ignoreOccupied If true, the result can be an occupied cell
1128 * @param result Array in which to place the result, or null (in which case a new array will
1129 * be allocated)
1130 * @return The X, Y cell of a vacant area that can contain this object,
1131 * nearest the requested location.
1132 */
Sunny Goyalf7a29e82015-04-24 15:20:43 -07001133 private int[] findNearestArea(int pixelX, int pixelY, int minSpanX, int minSpanY, int spanX,
1134 int spanY, boolean ignoreOccupied, int[] result, int[] resultSpan) {
Adam Cohend41fbf52012-02-16 23:53:59 -08001135 lazyInitTempRectStack();
Michael Jurkac6ee42e2010-09-30 12:04:50 -07001136
Adam Cohene3e27a82011-04-15 12:07:39 -07001137 // For items with a spanX / spanY > 1, the passed in point (pixelX, pixelY) corresponds
1138 // to the center of the item, but we are searching based on the top-left cell, so
1139 // we translate the point over to correspond to the top-left.
Sunny Goyalaa8a8712016-11-20 15:26:01 +05301140 pixelX -= mCellWidth * (spanX - 1) / 2f;
1141 pixelY -= mCellHeight * (spanY - 1) / 2f;
Adam Cohene3e27a82011-04-15 12:07:39 -07001142
Jeff Sharkey70864282009-04-07 21:08:40 -07001143 // Keep track of best-scoring drop area
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001144 final int[] bestXY = result != null ? result : new int[2];
Jeff Sharkey70864282009-04-07 21:08:40 -07001145 double bestDistance = Double.MAX_VALUE;
Adam Cohend41fbf52012-02-16 23:53:59 -08001146 final Rect bestRect = new Rect(-1, -1, -1, -1);
Rajeev Kumar9962dbe2017-06-12 12:16:20 -07001147 final Stack<Rect> validRegions = new Stack<>();
Winson Chungaafa03c2010-06-11 17:34:16 -07001148
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001149 final int countX = mCountX;
1150 final int countY = mCountY;
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001151
Adam Cohend41fbf52012-02-16 23:53:59 -08001152 if (minSpanX <= 0 || minSpanY <= 0 || spanX <= 0 || spanY <= 0 ||
1153 spanX < minSpanX || spanY < minSpanY) {
1154 return bestXY;
1155 }
1156
1157 for (int y = 0; y < countY - (minSpanY - 1); y++) {
Michael Jurkac28de512010-08-13 11:27:44 -07001158 inner:
Adam Cohend41fbf52012-02-16 23:53:59 -08001159 for (int x = 0; x < countX - (minSpanX - 1); x++) {
1160 int ySize = -1;
1161 int xSize = -1;
Adam Cohendf035382011-04-11 17:22:04 -07001162 if (ignoreOccupied) {
Adam Cohend41fbf52012-02-16 23:53:59 -08001163 // First, let's see if this thing fits anywhere
1164 for (int i = 0; i < minSpanX; i++) {
1165 for (int j = 0; j < minSpanY; j++) {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001166 if (mOccupied.cells[x + i][y + j]) {
Adam Cohendf035382011-04-11 17:22:04 -07001167 continue inner;
1168 }
Michael Jurkac28de512010-08-13 11:27:44 -07001169 }
1170 }
Adam Cohend41fbf52012-02-16 23:53:59 -08001171 xSize = minSpanX;
1172 ySize = minSpanY;
1173
1174 // We know that the item will fit at _some_ acceptable size, now let's see
1175 // how big we can make it. We'll alternate between incrementing x and y spans
1176 // until we hit a limit.
1177 boolean incX = true;
1178 boolean hitMaxX = xSize >= spanX;
1179 boolean hitMaxY = ySize >= spanY;
1180 while (!(hitMaxX && hitMaxY)) {
1181 if (incX && !hitMaxX) {
1182 for (int j = 0; j < ySize; j++) {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001183 if (x + xSize > countX -1 || mOccupied.cells[x + xSize][y + j]) {
Adam Cohend41fbf52012-02-16 23:53:59 -08001184 // We can't move out horizontally
1185 hitMaxX = true;
1186 }
1187 }
1188 if (!hitMaxX) {
1189 xSize++;
1190 }
1191 } else if (!hitMaxY) {
1192 for (int i = 0; i < xSize; i++) {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001193 if (y + ySize > countY - 1 || mOccupied.cells[x + i][y + ySize]) {
Adam Cohend41fbf52012-02-16 23:53:59 -08001194 // We can't move out vertically
1195 hitMaxY = true;
1196 }
1197 }
1198 if (!hitMaxY) {
1199 ySize++;
1200 }
1201 }
1202 hitMaxX |= xSize >= spanX;
1203 hitMaxY |= ySize >= spanY;
1204 incX = !incX;
1205 }
1206 incX = true;
1207 hitMaxX = xSize >= spanX;
1208 hitMaxY = ySize >= spanY;
Michael Jurkac28de512010-08-13 11:27:44 -07001209 }
Sunny Goyal2805e632015-05-20 15:35:32 -07001210 final int[] cellXY = mTmpPoint;
Adam Cohene3e27a82011-04-15 12:07:39 -07001211 cellToCenterPoint(x, y, cellXY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001212
Adam Cohend41fbf52012-02-16 23:53:59 -08001213 // We verify that the current rect is not a sub-rect of any of our previous
1214 // candidates. In this case, the current rect is disqualified in favour of the
1215 // containing rect.
1216 Rect currentRect = mTempRectStack.pop();
1217 currentRect.set(x, y, x + xSize, y + ySize);
1218 boolean contained = false;
1219 for (Rect r : validRegions) {
1220 if (r.contains(currentRect)) {
1221 contained = true;
1222 break;
1223 }
1224 }
1225 validRegions.push(currentRect);
Sunny Goyalf7a29e82015-04-24 15:20:43 -07001226 double distance = Math.hypot(cellXY[0] - pixelX, cellXY[1] - pixelY);
Adam Cohen482ed822012-03-02 14:15:13 -08001227
Adam Cohend41fbf52012-02-16 23:53:59 -08001228 if ((distance <= bestDistance && !contained) ||
1229 currentRect.contains(bestRect)) {
Michael Jurkac28de512010-08-13 11:27:44 -07001230 bestDistance = distance;
1231 bestXY[0] = x;
1232 bestXY[1] = y;
Adam Cohend41fbf52012-02-16 23:53:59 -08001233 if (resultSpan != null) {
1234 resultSpan[0] = xSize;
1235 resultSpan[1] = ySize;
1236 }
1237 bestRect.set(currentRect);
Michael Jurkac28de512010-08-13 11:27:44 -07001238 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001239 }
1240 }
1241
Adam Cohenc0dcf592011-06-01 15:30:43 -07001242 // Return -1, -1 if no suitable location found
1243 if (bestDistance == Double.MAX_VALUE) {
1244 bestXY[0] = -1;
1245 bestXY[1] = -1;
Jeff Sharkey70864282009-04-07 21:08:40 -07001246 }
Adam Cohend41fbf52012-02-16 23:53:59 -08001247 recycleTempRects(validRegions);
Adam Cohenc0dcf592011-06-01 15:30:43 -07001248 return bestXY;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001249 }
Winson Chungaafa03c2010-06-11 17:34:16 -07001250
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001251 /**
Adam Cohen482ed822012-03-02 14:15:13 -08001252 * Find a vacant area that will fit the given bounds nearest the requested
1253 * cell location, and will also weigh in a suggested direction vector of the
1254 * desired location. This method computers distance based on unit grid distances,
1255 * not pixel distances.
1256 *
Adam Cohen47a876d2012-03-19 13:21:41 -07001257 * @param cellX The X cell nearest to which you want to search for a vacant area.
1258 * @param cellY The Y cell nearest which you want to search for a vacant area.
Adam Cohen482ed822012-03-02 14:15:13 -08001259 * @param spanX Horizontal span of the object.
1260 * @param spanY Vertical span of the object.
Adam Cohen47a876d2012-03-19 13:21:41 -07001261 * @param direction The favored direction in which the views should move from x, y
Sunny Goyal9eba1fd2015-10-16 08:58:57 -07001262 * @param occupied The array which represents which cells in the CellLayout are occupied
Adam Cohen47a876d2012-03-19 13:21:41 -07001263 * @param blockOccupied The array which represents which cells in the specified block (cellX,
Winson Chung5f8afe62013-08-12 16:19:28 -07001264 * cellY, spanX, spanY) are occupied. This is used when try to move a group of views.
Adam Cohen482ed822012-03-02 14:15:13 -08001265 * @param result Array in which to place the result, or null (in which case a new array will
1266 * be allocated)
1267 * @return The X, Y cell of a vacant area that can contain this object,
1268 * nearest the requested location.
1269 */
1270 private int[] findNearestArea(int cellX, int cellY, int spanX, int spanY, int[] direction,
Adam Cohen47a876d2012-03-19 13:21:41 -07001271 boolean[][] occupied, boolean blockOccupied[][], int[] result) {
Adam Cohen482ed822012-03-02 14:15:13 -08001272 // Keep track of best-scoring drop area
1273 final int[] bestXY = result != null ? result : new int[2];
1274 float bestDistance = Float.MAX_VALUE;
1275 int bestDirectionScore = Integer.MIN_VALUE;
1276
1277 final int countX = mCountX;
1278 final int countY = mCountY;
1279
1280 for (int y = 0; y < countY - (spanY - 1); y++) {
1281 inner:
1282 for (int x = 0; x < countX - (spanX - 1); x++) {
1283 // First, let's see if this thing fits anywhere
1284 for (int i = 0; i < spanX; i++) {
1285 for (int j = 0; j < spanY; j++) {
Adam Cohen47a876d2012-03-19 13:21:41 -07001286 if (occupied[x + i][y + j] && (blockOccupied == null || blockOccupied[i][j])) {
Adam Cohen482ed822012-03-02 14:15:13 -08001287 continue inner;
1288 }
1289 }
1290 }
1291
Sunny Goyalf7a29e82015-04-24 15:20:43 -07001292 float distance = (float) Math.hypot(x - cellX, y - cellY);
Adam Cohen482ed822012-03-02 14:15:13 -08001293 int[] curDirection = mTmpPoint;
Adam Cohen47a876d2012-03-19 13:21:41 -07001294 computeDirectionVector(x - cellX, y - cellY, curDirection);
1295 // The direction score is just the dot product of the two candidate direction
1296 // and that passed in.
Adam Cohen482ed822012-03-02 14:15:13 -08001297 int curDirectionScore = direction[0] * curDirection[0] +
1298 direction[1] * curDirection[1];
Sunny Goyal8f90dcf2016-08-18 15:01:11 -07001299 if (Float.compare(distance, bestDistance) < 0 ||
1300 (Float.compare(distance, bestDistance) == 0
1301 && curDirectionScore > bestDirectionScore)) {
Adam Cohen482ed822012-03-02 14:15:13 -08001302 bestDistance = distance;
1303 bestDirectionScore = curDirectionScore;
1304 bestXY[0] = x;
1305 bestXY[1] = y;
1306 }
1307 }
1308 }
1309
1310 // Return -1, -1 if no suitable location found
1311 if (bestDistance == Float.MAX_VALUE) {
1312 bestXY[0] = -1;
1313 bestXY[1] = -1;
1314 }
1315 return bestXY;
1316 }
1317
1318 private boolean addViewToTempLocation(View v, Rect rectOccupiedByPotentialDrop,
Adam Cohen8baab352012-03-20 17:39:21 -07001319 int[] direction, ItemConfiguration currentState) {
1320 CellAndSpan c = currentState.map.get(v);
Adam Cohen482ed822012-03-02 14:15:13 -08001321 boolean success = false;
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001322 mTmpOccupied.markCells(c, false);
1323 mTmpOccupied.markCells(rectOccupiedByPotentialDrop, true);
Adam Cohen482ed822012-03-02 14:15:13 -08001324
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001325 findNearestArea(c.cellX, c.cellY, c.spanX, c.spanY, direction,
1326 mTmpOccupied.cells, null, mTempLocation);
Adam Cohen482ed822012-03-02 14:15:13 -08001327
1328 if (mTempLocation[0] >= 0 && mTempLocation[1] >= 0) {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001329 c.cellX = mTempLocation[0];
1330 c.cellY = mTempLocation[1];
Adam Cohen482ed822012-03-02 14:15:13 -08001331 success = true;
Adam Cohen482ed822012-03-02 14:15:13 -08001332 }
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001333 mTmpOccupied.markCells(c, true);
Adam Cohen482ed822012-03-02 14:15:13 -08001334 return success;
1335 }
1336
Adam Cohenf3900c22012-11-16 18:28:11 -08001337 /**
1338 * This helper class defines a cluster of views. It helps with defining complex edges
1339 * of the cluster and determining how those edges interact with other views. The edges
1340 * essentially define a fine-grained boundary around the cluster of views -- like a more
1341 * precise version of a bounding box.
1342 */
1343 private class ViewCluster {
Sunny Goyal6dc98b92016-04-03 15:25:29 -07001344 final static int LEFT = 1 << 0;
1345 final static int TOP = 1 << 1;
1346 final static int RIGHT = 1 << 2;
1347 final static int BOTTOM = 1 << 3;
Adam Cohen47a876d2012-03-19 13:21:41 -07001348
Rajeev Kumar9962dbe2017-06-12 12:16:20 -07001349 final ArrayList<View> views;
1350 final ItemConfiguration config;
1351 final Rect boundingRect = new Rect();
Adam Cohen47a876d2012-03-19 13:21:41 -07001352
Rajeev Kumar9962dbe2017-06-12 12:16:20 -07001353 final int[] leftEdge = new int[mCountY];
1354 final int[] rightEdge = new int[mCountY];
1355 final int[] topEdge = new int[mCountX];
1356 final int[] bottomEdge = new int[mCountX];
Sunny Goyal6dc98b92016-04-03 15:25:29 -07001357 int dirtyEdges;
1358 boolean boundingRectDirty;
Adam Cohenf3900c22012-11-16 18:28:11 -08001359
1360 @SuppressWarnings("unchecked")
1361 public ViewCluster(ArrayList<View> views, ItemConfiguration config) {
1362 this.views = (ArrayList<View>) views.clone();
1363 this.config = config;
1364 resetEdges();
Adam Cohen47a876d2012-03-19 13:21:41 -07001365 }
1366
Adam Cohenf3900c22012-11-16 18:28:11 -08001367 void resetEdges() {
1368 for (int i = 0; i < mCountX; i++) {
1369 topEdge[i] = -1;
1370 bottomEdge[i] = -1;
1371 }
1372 for (int i = 0; i < mCountY; i++) {
1373 leftEdge[i] = -1;
1374 rightEdge[i] = -1;
1375 }
Sunny Goyal6dc98b92016-04-03 15:25:29 -07001376 dirtyEdges = LEFT | TOP | RIGHT | BOTTOM;
Adam Cohenf3900c22012-11-16 18:28:11 -08001377 boundingRectDirty = true;
1378 }
Adam Cohen47a876d2012-03-19 13:21:41 -07001379
Sunny Goyal6dc98b92016-04-03 15:25:29 -07001380 void computeEdge(int which) {
Adam Cohenf3900c22012-11-16 18:28:11 -08001381 int count = views.size();
1382 for (int i = 0; i < count; i++) {
1383 CellAndSpan cs = config.map.get(views.get(i));
1384 switch (which) {
1385 case LEFT:
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001386 int left = cs.cellX;
1387 for (int j = cs.cellY; j < cs.cellY + cs.spanY; j++) {
Sunny Goyal6dc98b92016-04-03 15:25:29 -07001388 if (left < leftEdge[j] || leftEdge[j] < 0) {
1389 leftEdge[j] = left;
Adam Cohena56dc102012-07-13 13:41:42 -07001390 }
Adam Cohen47a876d2012-03-19 13:21:41 -07001391 }
Adam Cohenf3900c22012-11-16 18:28:11 -08001392 break;
1393 case RIGHT:
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001394 int right = cs.cellX + cs.spanX;
1395 for (int j = cs.cellY; j < cs.cellY + cs.spanY; j++) {
Sunny Goyal6dc98b92016-04-03 15:25:29 -07001396 if (right > rightEdge[j]) {
1397 rightEdge[j] = right;
Adam Cohenf3900c22012-11-16 18:28:11 -08001398 }
1399 }
1400 break;
1401 case TOP:
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001402 int top = cs.cellY;
1403 for (int j = cs.cellX; j < cs.cellX + cs.spanX; j++) {
Sunny Goyal6dc98b92016-04-03 15:25:29 -07001404 if (top < topEdge[j] || topEdge[j] < 0) {
1405 topEdge[j] = top;
Adam Cohenf3900c22012-11-16 18:28:11 -08001406 }
1407 }
1408 break;
1409 case BOTTOM:
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001410 int bottom = cs.cellY + cs.spanY;
1411 for (int j = cs.cellX; j < cs.cellX + cs.spanX; j++) {
Sunny Goyal6dc98b92016-04-03 15:25:29 -07001412 if (bottom > bottomEdge[j]) {
1413 bottomEdge[j] = bottom;
Adam Cohenf3900c22012-11-16 18:28:11 -08001414 }
1415 }
1416 break;
Adam Cohen47a876d2012-03-19 13:21:41 -07001417 }
1418 }
1419 }
Adam Cohenf3900c22012-11-16 18:28:11 -08001420
1421 boolean isViewTouchingEdge(View v, int whichEdge) {
1422 CellAndSpan cs = config.map.get(v);
1423
Sunny Goyal6dc98b92016-04-03 15:25:29 -07001424 if ((dirtyEdges & whichEdge) == whichEdge) {
1425 computeEdge(whichEdge);
1426 dirtyEdges &= ~whichEdge;
1427 }
Adam Cohenf3900c22012-11-16 18:28:11 -08001428
1429 switch (whichEdge) {
1430 case LEFT:
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001431 for (int i = cs.cellY; i < cs.cellY + cs.spanY; i++) {
1432 if (leftEdge[i] == cs.cellX + cs.spanX) {
Adam Cohenf3900c22012-11-16 18:28:11 -08001433 return true;
1434 }
1435 }
1436 break;
1437 case RIGHT:
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001438 for (int i = cs.cellY; i < cs.cellY + cs.spanY; i++) {
1439 if (rightEdge[i] == cs.cellX) {
Adam Cohenf3900c22012-11-16 18:28:11 -08001440 return true;
1441 }
1442 }
1443 break;
1444 case TOP:
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001445 for (int i = cs.cellX; i < cs.cellX + cs.spanX; i++) {
1446 if (topEdge[i] == cs.cellY + cs.spanY) {
Adam Cohenf3900c22012-11-16 18:28:11 -08001447 return true;
1448 }
1449 }
1450 break;
1451 case BOTTOM:
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001452 for (int i = cs.cellX; i < cs.cellX + cs.spanX; i++) {
1453 if (bottomEdge[i] == cs.cellY) {
Adam Cohenf3900c22012-11-16 18:28:11 -08001454 return true;
1455 }
1456 }
1457 break;
1458 }
1459 return false;
1460 }
1461
1462 void shift(int whichEdge, int delta) {
1463 for (View v: views) {
1464 CellAndSpan c = config.map.get(v);
1465 switch (whichEdge) {
1466 case LEFT:
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001467 c.cellX -= delta;
Adam Cohenf3900c22012-11-16 18:28:11 -08001468 break;
1469 case RIGHT:
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001470 c.cellX += delta;
Adam Cohenf3900c22012-11-16 18:28:11 -08001471 break;
1472 case TOP:
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001473 c.cellY -= delta;
Adam Cohenf3900c22012-11-16 18:28:11 -08001474 break;
1475 case BOTTOM:
1476 default:
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001477 c.cellY += delta;
Adam Cohenf3900c22012-11-16 18:28:11 -08001478 break;
1479 }
1480 }
1481 resetEdges();
1482 }
1483
1484 public void addView(View v) {
1485 views.add(v);
1486 resetEdges();
1487 }
1488
1489 public Rect getBoundingRect() {
1490 if (boundingRectDirty) {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001491 config.getBoundingRectForViews(views, boundingRect);
Adam Cohenf3900c22012-11-16 18:28:11 -08001492 }
1493 return boundingRect;
1494 }
1495
Rajeev Kumar9962dbe2017-06-12 12:16:20 -07001496 final PositionComparator comparator = new PositionComparator();
Adam Cohenf3900c22012-11-16 18:28:11 -08001497 class PositionComparator implements Comparator<View> {
1498 int whichEdge = 0;
1499 public int compare(View left, View right) {
1500 CellAndSpan l = config.map.get(left);
1501 CellAndSpan r = config.map.get(right);
1502 switch (whichEdge) {
1503 case LEFT:
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001504 return (r.cellX + r.spanX) - (l.cellX + l.spanX);
Adam Cohenf3900c22012-11-16 18:28:11 -08001505 case RIGHT:
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001506 return l.cellX - r.cellX;
Adam Cohenf3900c22012-11-16 18:28:11 -08001507 case TOP:
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001508 return (r.cellY + r.spanY) - (l.cellY + l.spanY);
Adam Cohenf3900c22012-11-16 18:28:11 -08001509 case BOTTOM:
1510 default:
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001511 return l.cellY - r.cellY;
Adam Cohenf3900c22012-11-16 18:28:11 -08001512 }
1513 }
1514 }
1515
1516 public void sortConfigurationForEdgePush(int edge) {
1517 comparator.whichEdge = edge;
1518 Collections.sort(config.sortedViews, comparator);
1519 }
Adam Cohen47a876d2012-03-19 13:21:41 -07001520 }
1521
Adam Cohenf3900c22012-11-16 18:28:11 -08001522 private boolean pushViewsToTempLocation(ArrayList<View> views, Rect rectOccupiedByPotentialDrop,
1523 int[] direction, View dragView, ItemConfiguration currentState) {
Adam Cohene0489502012-08-27 15:18:53 -07001524
Adam Cohenf3900c22012-11-16 18:28:11 -08001525 ViewCluster cluster = new ViewCluster(views, currentState);
1526 Rect clusterRect = cluster.getBoundingRect();
1527 int whichEdge;
1528 int pushDistance;
1529 boolean fail = false;
1530
1531 // Determine the edge of the cluster that will be leading the push and how far
1532 // the cluster must be shifted.
1533 if (direction[0] < 0) {
1534 whichEdge = ViewCluster.LEFT;
1535 pushDistance = clusterRect.right - rectOccupiedByPotentialDrop.left;
Adam Cohene0489502012-08-27 15:18:53 -07001536 } else if (direction[0] > 0) {
Adam Cohenf3900c22012-11-16 18:28:11 -08001537 whichEdge = ViewCluster.RIGHT;
1538 pushDistance = rectOccupiedByPotentialDrop.right - clusterRect.left;
1539 } else if (direction[1] < 0) {
1540 whichEdge = ViewCluster.TOP;
1541 pushDistance = clusterRect.bottom - rectOccupiedByPotentialDrop.top;
1542 } else {
1543 whichEdge = ViewCluster.BOTTOM;
1544 pushDistance = rectOccupiedByPotentialDrop.bottom - clusterRect.top;
Adam Cohene0489502012-08-27 15:18:53 -07001545 }
1546
Adam Cohenf3900c22012-11-16 18:28:11 -08001547 // Break early for invalid push distance.
1548 if (pushDistance <= 0) {
1549 return false;
Adam Cohene0489502012-08-27 15:18:53 -07001550 }
Adam Cohenf3900c22012-11-16 18:28:11 -08001551
1552 // Mark the occupied state as false for the group of views we want to move.
1553 for (View v: views) {
1554 CellAndSpan c = currentState.map.get(v);
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001555 mTmpOccupied.markCells(c, false);
Adam Cohenf3900c22012-11-16 18:28:11 -08001556 }
1557
1558 // We save the current configuration -- if we fail to find a solution we will revert
1559 // to the initial state. The process of finding a solution modifies the configuration
1560 // in place, hence the need for revert in the failure case.
1561 currentState.save();
1562
1563 // The pushing algorithm is simplified by considering the views in the order in which
1564 // they would be pushed by the cluster. For example, if the cluster is leading with its
1565 // left edge, we consider sort the views by their right edge, from right to left.
1566 cluster.sortConfigurationForEdgePush(whichEdge);
1567
1568 while (pushDistance > 0 && !fail) {
1569 for (View v: currentState.sortedViews) {
1570 // For each view that isn't in the cluster, we see if the leading edge of the
1571 // cluster is contacting the edge of that view. If so, we add that view to the
1572 // cluster.
1573 if (!cluster.views.contains(v) && v != dragView) {
1574 if (cluster.isViewTouchingEdge(v, whichEdge)) {
1575 LayoutParams lp = (LayoutParams) v.getLayoutParams();
1576 if (!lp.canReorder) {
1577 // The push solution includes the all apps button, this is not viable.
1578 fail = true;
1579 break;
1580 }
1581 cluster.addView(v);
1582 CellAndSpan c = currentState.map.get(v);
1583
1584 // Adding view to cluster, mark it as not occupied.
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001585 mTmpOccupied.markCells(c, false);
Adam Cohenf3900c22012-11-16 18:28:11 -08001586 }
1587 }
1588 }
1589 pushDistance--;
1590
1591 // The cluster has been completed, now we move the whole thing over in the appropriate
1592 // direction.
1593 cluster.shift(whichEdge, 1);
1594 }
1595
1596 boolean foundSolution = false;
1597 clusterRect = cluster.getBoundingRect();
1598
1599 // Due to the nature of the algorithm, the only check required to verify a valid solution
1600 // is to ensure that completed shifted cluster lies completely within the cell layout.
1601 if (!fail && clusterRect.left >= 0 && clusterRect.right <= mCountX && clusterRect.top >= 0 &&
1602 clusterRect.bottom <= mCountY) {
1603 foundSolution = true;
1604 } else {
1605 currentState.restore();
1606 }
1607
1608 // In either case, we set the occupied array as marked for the location of the views
1609 for (View v: cluster.views) {
1610 CellAndSpan c = currentState.map.get(v);
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001611 mTmpOccupied.markCells(c, true);
Adam Cohenf3900c22012-11-16 18:28:11 -08001612 }
1613
1614 return foundSolution;
Adam Cohene0489502012-08-27 15:18:53 -07001615 }
1616
Adam Cohen482ed822012-03-02 14:15:13 -08001617 private boolean addViewsToTempLocation(ArrayList<View> views, Rect rectOccupiedByPotentialDrop,
Adam Cohenf3900c22012-11-16 18:28:11 -08001618 int[] direction, View dragView, ItemConfiguration currentState) {
Adam Cohen482ed822012-03-02 14:15:13 -08001619 if (views.size() == 0) return true;
Adam Cohen482ed822012-03-02 14:15:13 -08001620
Adam Cohen8baab352012-03-20 17:39:21 -07001621 boolean success = false;
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001622 Rect boundingRect = new Rect();
Adam Cohen8baab352012-03-20 17:39:21 -07001623 // We construct a rect which represents the entire group of views passed in
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001624 currentState.getBoundingRectForViews(views, boundingRect);
Adam Cohen8baab352012-03-20 17:39:21 -07001625
Adam Cohen8baab352012-03-20 17:39:21 -07001626 // Mark the occupied state as false for the group of views we want to move.
Adam Cohenf3900c22012-11-16 18:28:11 -08001627 for (View v: views) {
Adam Cohen8baab352012-03-20 17:39:21 -07001628 CellAndSpan c = currentState.map.get(v);
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001629 mTmpOccupied.markCells(c, false);
Adam Cohen8baab352012-03-20 17:39:21 -07001630 }
1631
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001632 GridOccupancy blockOccupied = new GridOccupancy(boundingRect.width(), boundingRect.height());
Adam Cohen47a876d2012-03-19 13:21:41 -07001633 int top = boundingRect.top;
1634 int left = boundingRect.left;
Adam Cohen8baab352012-03-20 17:39:21 -07001635 // We mark more precisely which parts of the bounding rect are truly occupied, allowing
Adam Cohena56dc102012-07-13 13:41:42 -07001636 // for interlocking.
Adam Cohenf3900c22012-11-16 18:28:11 -08001637 for (View v: views) {
Adam Cohen8baab352012-03-20 17:39:21 -07001638 CellAndSpan c = currentState.map.get(v);
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001639 blockOccupied.markCells(c.cellX - left, c.cellY - top, c.spanX, c.spanY, true);
Adam Cohen47a876d2012-03-19 13:21:41 -07001640 }
1641
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001642 mTmpOccupied.markCells(rectOccupiedByPotentialDrop, true);
Adam Cohen482ed822012-03-02 14:15:13 -08001643
Adam Cohenf3900c22012-11-16 18:28:11 -08001644 findNearestArea(boundingRect.left, boundingRect.top, boundingRect.width(),
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001645 boundingRect.height(), direction,
1646 mTmpOccupied.cells, blockOccupied.cells, mTempLocation);
Adam Cohen482ed822012-03-02 14:15:13 -08001647
Adam Cohen8baab352012-03-20 17:39:21 -07001648 // If we successfuly found a location by pushing the block of views, we commit it
Adam Cohen482ed822012-03-02 14:15:13 -08001649 if (mTempLocation[0] >= 0 && mTempLocation[1] >= 0) {
Adam Cohen8baab352012-03-20 17:39:21 -07001650 int deltaX = mTempLocation[0] - boundingRect.left;
1651 int deltaY = mTempLocation[1] - boundingRect.top;
Adam Cohenf3900c22012-11-16 18:28:11 -08001652 for (View v: views) {
Adam Cohen8baab352012-03-20 17:39:21 -07001653 CellAndSpan c = currentState.map.get(v);
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001654 c.cellX += deltaX;
1655 c.cellY += deltaY;
Adam Cohen482ed822012-03-02 14:15:13 -08001656 }
1657 success = true;
1658 }
Adam Cohen8baab352012-03-20 17:39:21 -07001659
1660 // In either case, we set the occupied array as marked for the location of the views
Adam Cohenf3900c22012-11-16 18:28:11 -08001661 for (View v: views) {
Adam Cohen8baab352012-03-20 17:39:21 -07001662 CellAndSpan c = currentState.map.get(v);
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001663 mTmpOccupied.markCells(c, true);
Adam Cohen482ed822012-03-02 14:15:13 -08001664 }
1665 return success;
1666 }
1667
Adam Cohen4abc5bd2012-05-29 21:06:03 -07001668 // This method tries to find a reordering solution which satisfies the push mechanic by trying
1669 // to push items in each of the cardinal directions, in an order based on the direction vector
1670 // passed.
1671 private boolean attemptPushInDirection(ArrayList<View> intersectingViews, Rect occupied,
1672 int[] direction, View ignoreView, ItemConfiguration solution) {
1673 if ((Math.abs(direction[0]) + Math.abs(direction[1])) > 1) {
Winson Chung5f8afe62013-08-12 16:19:28 -07001674 // If the direction vector has two non-zero components, we try pushing
Adam Cohen4abc5bd2012-05-29 21:06:03 -07001675 // separately in each of the components.
1676 int temp = direction[1];
1677 direction[1] = 0;
Adam Cohenf3900c22012-11-16 18:28:11 -08001678
1679 if (pushViewsToTempLocation(intersectingViews, occupied, direction,
Adam Cohen4abc5bd2012-05-29 21:06:03 -07001680 ignoreView, solution)) {
1681 return true;
1682 }
1683 direction[1] = temp;
1684 temp = direction[0];
1685 direction[0] = 0;
Adam Cohenf3900c22012-11-16 18:28:11 -08001686
1687 if (pushViewsToTempLocation(intersectingViews, occupied, direction,
Adam Cohen4abc5bd2012-05-29 21:06:03 -07001688 ignoreView, solution)) {
1689 return true;
1690 }
1691 // Revert the direction
1692 direction[0] = temp;
1693
1694 // Now we try pushing in each component of the opposite direction
1695 direction[0] *= -1;
1696 direction[1] *= -1;
1697 temp = direction[1];
1698 direction[1] = 0;
Adam Cohenf3900c22012-11-16 18:28:11 -08001699 if (pushViewsToTempLocation(intersectingViews, occupied, direction,
Adam Cohen4abc5bd2012-05-29 21:06:03 -07001700 ignoreView, solution)) {
1701 return true;
1702 }
1703
1704 direction[1] = temp;
1705 temp = direction[0];
1706 direction[0] = 0;
Adam Cohenf3900c22012-11-16 18:28:11 -08001707 if (pushViewsToTempLocation(intersectingViews, occupied, direction,
Adam Cohen4abc5bd2012-05-29 21:06:03 -07001708 ignoreView, solution)) {
1709 return true;
1710 }
1711 // revert the direction
1712 direction[0] = temp;
1713 direction[0] *= -1;
1714 direction[1] *= -1;
Winson Chung5f8afe62013-08-12 16:19:28 -07001715
Adam Cohen4abc5bd2012-05-29 21:06:03 -07001716 } else {
1717 // If the direction vector has a single non-zero component, we push first in the
1718 // direction of the vector
Adam Cohenf3900c22012-11-16 18:28:11 -08001719 if (pushViewsToTempLocation(intersectingViews, occupied, direction,
Adam Cohen4abc5bd2012-05-29 21:06:03 -07001720 ignoreView, solution)) {
1721 return true;
1722 }
Adam Cohen4abc5bd2012-05-29 21:06:03 -07001723 // Then we try the opposite direction
1724 direction[0] *= -1;
1725 direction[1] *= -1;
Adam Cohenf3900c22012-11-16 18:28:11 -08001726 if (pushViewsToTempLocation(intersectingViews, occupied, direction,
Adam Cohen4abc5bd2012-05-29 21:06:03 -07001727 ignoreView, solution)) {
1728 return true;
1729 }
1730 // Switch the direction back
1731 direction[0] *= -1;
1732 direction[1] *= -1;
Winson Chung5f8afe62013-08-12 16:19:28 -07001733
1734 // If we have failed to find a push solution with the above, then we try
Adam Cohen4abc5bd2012-05-29 21:06:03 -07001735 // to find a solution by pushing along the perpendicular axis.
1736
1737 // Swap the components
1738 int temp = direction[1];
1739 direction[1] = direction[0];
1740 direction[0] = temp;
Adam Cohenf3900c22012-11-16 18:28:11 -08001741 if (pushViewsToTempLocation(intersectingViews, occupied, direction,
Adam Cohen4abc5bd2012-05-29 21:06:03 -07001742 ignoreView, solution)) {
1743 return true;
1744 }
1745
1746 // Then we try the opposite direction
1747 direction[0] *= -1;
1748 direction[1] *= -1;
Adam Cohenf3900c22012-11-16 18:28:11 -08001749 if (pushViewsToTempLocation(intersectingViews, occupied, direction,
Adam Cohen4abc5bd2012-05-29 21:06:03 -07001750 ignoreView, solution)) {
1751 return true;
1752 }
1753 // Switch the direction back
1754 direction[0] *= -1;
1755 direction[1] *= -1;
1756
1757 // Swap the components back
1758 temp = direction[1];
1759 direction[1] = direction[0];
1760 direction[0] = temp;
1761 }
1762 return false;
1763 }
1764
Adam Cohen482ed822012-03-02 14:15:13 -08001765 private boolean rearrangementExists(int cellX, int cellY, int spanX, int spanY, int[] direction,
Adam Cohen8baab352012-03-20 17:39:21 -07001766 View ignoreView, ItemConfiguration solution) {
Winson Chunge3e03bc2012-05-01 15:10:11 -07001767 // Return early if get invalid cell positions
1768 if (cellX < 0 || cellY < 0) return false;
Adam Cohen482ed822012-03-02 14:15:13 -08001769
Adam Cohen8baab352012-03-20 17:39:21 -07001770 mIntersectingViews.clear();
Adam Cohen482ed822012-03-02 14:15:13 -08001771 mOccupiedRect.set(cellX, cellY, cellX + spanX, cellY + spanY);
Adam Cohen482ed822012-03-02 14:15:13 -08001772
Adam Cohen8baab352012-03-20 17:39:21 -07001773 // Mark the desired location of the view currently being dragged.
Adam Cohen482ed822012-03-02 14:15:13 -08001774 if (ignoreView != null) {
Adam Cohen8baab352012-03-20 17:39:21 -07001775 CellAndSpan c = solution.map.get(ignoreView);
Adam Cohen19f37922012-03-21 11:59:11 -07001776 if (c != null) {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001777 c.cellX = cellX;
1778 c.cellY = cellY;
Adam Cohen19f37922012-03-21 11:59:11 -07001779 }
Adam Cohen482ed822012-03-02 14:15:13 -08001780 }
Adam Cohen482ed822012-03-02 14:15:13 -08001781 Rect r0 = new Rect(cellX, cellY, cellX + spanX, cellY + spanY);
1782 Rect r1 = new Rect();
Adam Cohen8baab352012-03-20 17:39:21 -07001783 for (View child: solution.map.keySet()) {
Adam Cohen482ed822012-03-02 14:15:13 -08001784 if (child == ignoreView) continue;
Adam Cohen8baab352012-03-20 17:39:21 -07001785 CellAndSpan c = solution.map.get(child);
Adam Cohen482ed822012-03-02 14:15:13 -08001786 LayoutParams lp = (LayoutParams) child.getLayoutParams();
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001787 r1.set(c.cellX, c.cellY, c.cellX + c.spanX, c.cellY + c.spanY);
Adam Cohen482ed822012-03-02 14:15:13 -08001788 if (Rect.intersects(r0, r1)) {
1789 if (!lp.canReorder) {
1790 return false;
1791 }
1792 mIntersectingViews.add(child);
1793 }
1794 }
Adam Cohen47a876d2012-03-19 13:21:41 -07001795
Rajeev Kumar9962dbe2017-06-12 12:16:20 -07001796 solution.intersectingViews = new ArrayList<>(mIntersectingViews);
Adam Cohenfa3c58f2013-12-06 16:10:55 -08001797
Winson Chung5f8afe62013-08-12 16:19:28 -07001798 // First we try to find a solution which respects the push mechanic. That is,
Adam Cohen4abc5bd2012-05-29 21:06:03 -07001799 // we try to find a solution such that no displaced item travels through another item
1800 // without also displacing that item.
1801 if (attemptPushInDirection(mIntersectingViews, mOccupiedRect, direction, ignoreView,
Adam Cohen19f37922012-03-21 11:59:11 -07001802 solution)) {
Adam Cohen47a876d2012-03-19 13:21:41 -07001803 return true;
1804 }
Adam Cohen47a876d2012-03-19 13:21:41 -07001805
Adam Cohen4abc5bd2012-05-29 21:06:03 -07001806 // Next we try moving the views as a block, but without requiring the push mechanic.
Adam Cohenf3900c22012-11-16 18:28:11 -08001807 if (addViewsToTempLocation(mIntersectingViews, mOccupiedRect, direction, ignoreView,
Adam Cohen19f37922012-03-21 11:59:11 -07001808 solution)) {
Adam Cohen482ed822012-03-02 14:15:13 -08001809 return true;
1810 }
Adam Cohen47a876d2012-03-19 13:21:41 -07001811
Adam Cohen482ed822012-03-02 14:15:13 -08001812 // Ok, they couldn't move as a block, let's move them individually
1813 for (View v : mIntersectingViews) {
Adam Cohen8baab352012-03-20 17:39:21 -07001814 if (!addViewToTempLocation(v, mOccupiedRect, direction, solution)) {
Adam Cohen482ed822012-03-02 14:15:13 -08001815 return false;
1816 }
1817 }
1818 return true;
1819 }
1820
1821 /*
1822 * Returns a pair (x, y), where x,y are in {-1, 0, 1} corresponding to vector between
1823 * the provided point and the provided cell
1824 */
Adam Cohen47a876d2012-03-19 13:21:41 -07001825 private void computeDirectionVector(float deltaX, float deltaY, int[] result) {
Jon Mirandae96798e2016-12-07 12:10:44 -08001826 double angle = Math.atan(deltaY / deltaX);
Adam Cohen482ed822012-03-02 14:15:13 -08001827
1828 result[0] = 0;
1829 result[1] = 0;
1830 if (Math.abs(Math.cos(angle)) > 0.5f) {
1831 result[0] = (int) Math.signum(deltaX);
1832 }
1833 if (Math.abs(Math.sin(angle)) > 0.5f) {
1834 result[1] = (int) Math.signum(deltaY);
1835 }
1836 }
1837
Sunny Goyalf7a29e82015-04-24 15:20:43 -07001838 private ItemConfiguration findReorderSolution(int pixelX, int pixelY, int minSpanX, int minSpanY,
Adam Cohenfa3c58f2013-12-06 16:10:55 -08001839 int spanX, int spanY, int[] direction, View dragView, boolean decX,
1840 ItemConfiguration solution) {
Adam Cohen8baab352012-03-20 17:39:21 -07001841 // Copy the current state into the solution. This solution will be manipulated as necessary.
1842 copyCurrentStateToSolution(solution, false);
1843 // Copy the current occupied array into the temporary occupied array. This array will be
1844 // manipulated as necessary to find a solution.
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001845 mOccupied.copyTo(mTmpOccupied);
Adam Cohen482ed822012-03-02 14:15:13 -08001846
1847 // We find the nearest cell into which we would place the dragged item, assuming there's
1848 // nothing in its way.
1849 int result[] = new int[2];
1850 result = findNearestArea(pixelX, pixelY, spanX, spanY, result);
1851
Rajeev Kumar9962dbe2017-06-12 12:16:20 -07001852 boolean success;
Adam Cohen482ed822012-03-02 14:15:13 -08001853 // First we try the exact nearest position of the item being dragged,
1854 // we will then want to try to move this around to other neighbouring positions
Adam Cohen8baab352012-03-20 17:39:21 -07001855 success = rearrangementExists(result[0], result[1], spanX, spanY, direction, dragView,
1856 solution);
Adam Cohen482ed822012-03-02 14:15:13 -08001857
1858 if (!success) {
1859 // We try shrinking the widget down to size in an alternating pattern, shrink 1 in
1860 // x, then 1 in y etc.
1861 if (spanX > minSpanX && (minSpanY == spanY || decX)) {
Adam Cohenfa3c58f2013-12-06 16:10:55 -08001862 return findReorderSolution(pixelX, pixelY, minSpanX, minSpanY, spanX - 1, spanY,
1863 direction, dragView, false, solution);
Adam Cohen482ed822012-03-02 14:15:13 -08001864 } else if (spanY > minSpanY) {
Adam Cohenfa3c58f2013-12-06 16:10:55 -08001865 return findReorderSolution(pixelX, pixelY, minSpanX, minSpanY, spanX, spanY - 1,
1866 direction, dragView, true, solution);
Adam Cohen482ed822012-03-02 14:15:13 -08001867 }
1868 solution.isSolution = false;
1869 } else {
1870 solution.isSolution = true;
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001871 solution.cellX = result[0];
1872 solution.cellY = result[1];
1873 solution.spanX = spanX;
1874 solution.spanY = spanY;
Adam Cohen482ed822012-03-02 14:15:13 -08001875 }
1876 return solution;
1877 }
1878
1879 private void copyCurrentStateToSolution(ItemConfiguration solution, boolean temp) {
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 LayoutParams lp = (LayoutParams) child.getLayoutParams();
Adam Cohen8baab352012-03-20 17:39:21 -07001884 CellAndSpan c;
Adam Cohen482ed822012-03-02 14:15:13 -08001885 if (temp) {
Adam Cohen8baab352012-03-20 17:39:21 -07001886 c = new CellAndSpan(lp.tmpCellX, lp.tmpCellY, lp.cellHSpan, lp.cellVSpan);
Adam Cohen482ed822012-03-02 14:15:13 -08001887 } else {
Adam Cohen8baab352012-03-20 17:39:21 -07001888 c = new CellAndSpan(lp.cellX, lp.cellY, lp.cellHSpan, lp.cellVSpan);
Adam Cohen482ed822012-03-02 14:15:13 -08001889 }
Adam Cohenf3900c22012-11-16 18:28:11 -08001890 solution.add(child, c);
Adam Cohen482ed822012-03-02 14:15:13 -08001891 }
1892 }
1893
1894 private void copySolutionToTempState(ItemConfiguration solution, View dragView) {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001895 mTmpOccupied.clear();
Adam Cohen482ed822012-03-02 14:15:13 -08001896
Michael Jurkaa52570f2012-03-20 03:18:20 -07001897 int childCount = mShortcutsAndWidgets.getChildCount();
Adam Cohen482ed822012-03-02 14:15:13 -08001898 for (int i = 0; i < childCount; i++) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07001899 View child = mShortcutsAndWidgets.getChildAt(i);
Adam Cohen482ed822012-03-02 14:15:13 -08001900 if (child == dragView) continue;
1901 LayoutParams lp = (LayoutParams) child.getLayoutParams();
Adam Cohen8baab352012-03-20 17:39:21 -07001902 CellAndSpan c = solution.map.get(child);
1903 if (c != null) {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001904 lp.tmpCellX = c.cellX;
1905 lp.tmpCellY = c.cellY;
Adam Cohen8baab352012-03-20 17:39:21 -07001906 lp.cellHSpan = c.spanX;
1907 lp.cellVSpan = c.spanY;
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001908 mTmpOccupied.markCells(c, true);
Adam Cohen482ed822012-03-02 14:15:13 -08001909 }
1910 }
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001911 mTmpOccupied.markCells(solution, true);
Adam Cohen482ed822012-03-02 14:15:13 -08001912 }
1913
1914 private void animateItemsToSolution(ItemConfiguration solution, View dragView, boolean
1915 commitDragView) {
1916
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001917 GridOccupancy occupied = DESTRUCTIVE_REORDER ? mOccupied : mTmpOccupied;
1918 occupied.clear();
Adam Cohen482ed822012-03-02 14:15:13 -08001919
Michael Jurkaa52570f2012-03-20 03:18:20 -07001920 int childCount = mShortcutsAndWidgets.getChildCount();
Adam Cohen482ed822012-03-02 14:15:13 -08001921 for (int i = 0; i < childCount; i++) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07001922 View child = mShortcutsAndWidgets.getChildAt(i);
Adam Cohen482ed822012-03-02 14:15:13 -08001923 if (child == dragView) continue;
Adam Cohen8baab352012-03-20 17:39:21 -07001924 CellAndSpan c = solution.map.get(child);
1925 if (c != null) {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001926 animateChildToPosition(child, c.cellX, c.cellY, REORDER_ANIMATION_DURATION, 0,
Adam Cohen19f37922012-03-21 11:59:11 -07001927 DESTRUCTIVE_REORDER, false);
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001928 occupied.markCells(c, true);
Adam Cohen482ed822012-03-02 14:15:13 -08001929 }
1930 }
1931 if (commitDragView) {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001932 occupied.markCells(solution, true);
Adam Cohen482ed822012-03-02 14:15:13 -08001933 }
1934 }
1935
Adam Cohenfa3c58f2013-12-06 16:10:55 -08001936
1937 // This method starts or changes the reorder preview animations
1938 private void beginOrAdjustReorderPreviewAnimations(ItemConfiguration solution,
1939 View dragView, int delay, int mode) {
Adam Cohen19f37922012-03-21 11:59:11 -07001940 int childCount = mShortcutsAndWidgets.getChildCount();
Adam Cohen19f37922012-03-21 11:59:11 -07001941 for (int i = 0; i < childCount; i++) {
1942 View child = mShortcutsAndWidgets.getChildAt(i);
1943 if (child == dragView) continue;
1944 CellAndSpan c = solution.map.get(child);
Adam Cohenfa3c58f2013-12-06 16:10:55 -08001945 boolean skip = mode == ReorderPreviewAnimation.MODE_HINT && solution.intersectingViews
1946 != null && !solution.intersectingViews.contains(child);
1947
Adam Cohen19f37922012-03-21 11:59:11 -07001948 LayoutParams lp = (LayoutParams) child.getLayoutParams();
Adam Cohenfa3c58f2013-12-06 16:10:55 -08001949 if (c != null && !skip) {
1950 ReorderPreviewAnimation rha = new ReorderPreviewAnimation(child, mode, lp.cellX,
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001951 lp.cellY, c.cellX, c.cellY, c.spanX, c.spanY);
Adam Cohend024f982012-05-23 18:26:45 -07001952 rha.animate();
Adam Cohen19f37922012-03-21 11:59:11 -07001953 }
1954 }
1955 }
1956
Adam Cohenfa3c58f2013-12-06 16:10:55 -08001957 // Class which represents the reorder preview animations. These animations show that an item is
Adam Cohen19f37922012-03-21 11:59:11 -07001958 // in a temporary state, and hint at where the item will return to.
Adam Cohenfa3c58f2013-12-06 16:10:55 -08001959 class ReorderPreviewAnimation {
Rajeev Kumar9962dbe2017-06-12 12:16:20 -07001960 final View child;
Adam Cohend024f982012-05-23 18:26:45 -07001961 float finalDeltaX;
1962 float finalDeltaY;
1963 float initDeltaX;
1964 float initDeltaY;
Rajeev Kumar9962dbe2017-06-12 12:16:20 -07001965 final float finalScale;
Adam Cohend024f982012-05-23 18:26:45 -07001966 float initScale;
Rajeev Kumar9962dbe2017-06-12 12:16:20 -07001967 final int mode;
Adam Cohenfa3c58f2013-12-06 16:10:55 -08001968 boolean repeating = false;
1969 private static final int PREVIEW_DURATION = 300;
1970 private static final int HINT_DURATION = Workspace.REORDER_TIMEOUT;
1971
Jon Miranda21266912016-12-19 14:12:05 -08001972 private static final float CHILD_DIVIDEND = 4.0f;
1973
Adam Cohenfa3c58f2013-12-06 16:10:55 -08001974 public static final int MODE_HINT = 0;
1975 public static final int MODE_PREVIEW = 1;
1976
Adam Cohene7587d22012-05-24 18:50:02 -07001977 Animator a;
Adam Cohen19f37922012-03-21 11:59:11 -07001978
Adam Cohenfa3c58f2013-12-06 16:10:55 -08001979 public ReorderPreviewAnimation(View child, int mode, int cellX0, int cellY0, int cellX1,
1980 int cellY1, int spanX, int spanY) {
Adam Cohen19f37922012-03-21 11:59:11 -07001981 regionToCenterPoint(cellX0, cellY0, spanX, spanY, mTmpPoint);
1982 final int x0 = mTmpPoint[0];
1983 final int y0 = mTmpPoint[1];
1984 regionToCenterPoint(cellX1, cellY1, spanX, spanY, mTmpPoint);
1985 final int x1 = mTmpPoint[0];
1986 final int y1 = mTmpPoint[1];
1987 final int dX = x1 - x0;
1988 final int dY = y1 - y0;
Jon Miranda21266912016-12-19 14:12:05 -08001989
1990 this.child = child;
1991 this.mode = mode;
1992 setInitialAnimationValues(false);
1993 finalScale = (mChildScale - (CHILD_DIVIDEND / child.getWidth())) * initScale;
1994 finalDeltaX = initDeltaX;
1995 finalDeltaY = initDeltaY;
Adam Cohenfa3c58f2013-12-06 16:10:55 -08001996 int dir = mode == MODE_HINT ? -1 : 1;
Adam Cohen19f37922012-03-21 11:59:11 -07001997 if (dX == dY && dX == 0) {
1998 } else {
1999 if (dY == 0) {
Jon Miranda21266912016-12-19 14:12:05 -08002000 finalDeltaX += - dir * Math.signum(dX) * mReorderPreviewAnimationMagnitude;
Adam Cohen19f37922012-03-21 11:59:11 -07002001 } else if (dX == 0) {
Jon Miranda21266912016-12-19 14:12:05 -08002002 finalDeltaY += - dir * Math.signum(dY) * mReorderPreviewAnimationMagnitude;
Adam Cohen19f37922012-03-21 11:59:11 -07002003 } else {
2004 double angle = Math.atan( (float) (dY) / dX);
Jon Miranda21266912016-12-19 14:12:05 -08002005 finalDeltaX += (int) (- dir * Math.signum(dX) *
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002006 Math.abs(Math.cos(angle) * mReorderPreviewAnimationMagnitude));
Jon Miranda21266912016-12-19 14:12:05 -08002007 finalDeltaY += (int) (- dir * Math.signum(dY) *
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002008 Math.abs(Math.sin(angle) * mReorderPreviewAnimationMagnitude));
Adam Cohen19f37922012-03-21 11:59:11 -07002009 }
2010 }
Jon Miranda21266912016-12-19 14:12:05 -08002011 }
2012
2013 void setInitialAnimationValues(boolean restoreOriginalValues) {
2014 if (restoreOriginalValues) {
2015 if (child instanceof LauncherAppWidgetHostView) {
2016 LauncherAppWidgetHostView lahv = (LauncherAppWidgetHostView) child;
2017 initScale = lahv.getScaleToFit();
2018 initDeltaX = lahv.getTranslationForCentering().x;
2019 initDeltaY = lahv.getTranslationForCentering().y;
2020 } else {
2021 initScale = mChildScale;
2022 initDeltaX = 0;
2023 initDeltaY = 0;
2024 }
2025 } else {
2026 initScale = child.getScaleX();
2027 initDeltaX = child.getTranslationX();
2028 initDeltaY = child.getTranslationY();
2029 }
Adam Cohen19f37922012-03-21 11:59:11 -07002030 }
2031
Adam Cohend024f982012-05-23 18:26:45 -07002032 void animate() {
Jon Miranda21266912016-12-19 14:12:05 -08002033 boolean noMovement = (finalDeltaX == initDeltaX) && (finalDeltaY == initDeltaY);
2034
Adam Cohen19f37922012-03-21 11:59:11 -07002035 if (mShakeAnimators.containsKey(child)) {
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002036 ReorderPreviewAnimation oldAnimation = mShakeAnimators.get(child);
Adam Cohend024f982012-05-23 18:26:45 -07002037 oldAnimation.cancel();
Adam Cohen19f37922012-03-21 11:59:11 -07002038 mShakeAnimators.remove(child);
Jon Miranda21266912016-12-19 14:12:05 -08002039 if (noMovement) {
Adam Cohene7587d22012-05-24 18:50:02 -07002040 completeAnimationImmediately();
2041 return;
2042 }
Adam Cohen19f37922012-03-21 11:59:11 -07002043 }
Jon Miranda21266912016-12-19 14:12:05 -08002044 if (noMovement) {
Adam Cohen19f37922012-03-21 11:59:11 -07002045 return;
2046 }
Jon Mirandacda3bfb2017-02-06 15:54:41 -08002047 ValueAnimator va = LauncherAnimUtils.ofFloat(0f, 1f);
Adam Cohene7587d22012-05-24 18:50:02 -07002048 a = va;
Tony Wickham9e0702f2015-09-02 14:45:39 -07002049
2050 // Animations are disabled in power save mode, causing the repeated animation to jump
2051 // spastically between beginning and end states. Since this looks bad, we don't repeat
2052 // the animation in power save mode.
Tony Wickham112ac952015-11-12 12:31:50 -08002053 if (!Utilities.isPowerSaverOn(getContext())) {
Tony Wickham9e0702f2015-09-02 14:45:39 -07002054 va.setRepeatMode(ValueAnimator.REVERSE);
2055 va.setRepeatCount(ValueAnimator.INFINITE);
2056 }
2057
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002058 va.setDuration(mode == MODE_HINT ? HINT_DURATION : PREVIEW_DURATION);
Adam Cohend024f982012-05-23 18:26:45 -07002059 va.setStartDelay((int) (Math.random() * 60));
Adam Cohen19f37922012-03-21 11:59:11 -07002060 va.addUpdateListener(new AnimatorUpdateListener() {
2061 @Override
2062 public void onAnimationUpdate(ValueAnimator animation) {
Jon Mirandae96798e2016-12-07 12:10:44 -08002063 float r = (Float) animation.getAnimatedValue();
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002064 float r1 = (mode == MODE_HINT && repeating) ? 1.0f : r;
2065 float x = r1 * finalDeltaX + (1 - r1) * initDeltaX;
2066 float y = r1 * finalDeltaY + (1 - r1) * initDeltaY;
Adam Cohen19f37922012-03-21 11:59:11 -07002067 child.setTranslationX(x);
2068 child.setTranslationY(y);
Adam Cohend024f982012-05-23 18:26:45 -07002069 float s = r * finalScale + (1 - r) * initScale;
Brandon Keely50e6e562012-05-08 16:28:49 -07002070 child.setScaleX(s);
2071 child.setScaleY(s);
Adam Cohen19f37922012-03-21 11:59:11 -07002072 }
2073 });
2074 va.addListener(new AnimatorListenerAdapter() {
2075 public void onAnimationRepeat(Animator animation) {
Adam Cohen19f37922012-03-21 11:59:11 -07002076 // We make sure to end only after a full period
Jon Miranda21266912016-12-19 14:12:05 -08002077 setInitialAnimationValues(true);
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002078 repeating = true;
Adam Cohen19f37922012-03-21 11:59:11 -07002079 }
2080 });
Adam Cohen19f37922012-03-21 11:59:11 -07002081 mShakeAnimators.put(child, this);
2082 va.start();
2083 }
2084
Adam Cohend024f982012-05-23 18:26:45 -07002085 private void cancel() {
Adam Cohene7587d22012-05-24 18:50:02 -07002086 if (a != null) {
2087 a.cancel();
2088 }
Adam Cohen19f37922012-03-21 11:59:11 -07002089 }
Adam Cohene7587d22012-05-24 18:50:02 -07002090
Adam Cohen091440a2015-03-18 14:16:05 -07002091 @Thunk void completeAnimationImmediately() {
Adam Cohene7587d22012-05-24 18:50:02 -07002092 if (a != null) {
2093 a.cancel();
2094 }
Brandon Keely50e6e562012-05-08 16:28:49 -07002095
Jon Miranda72dbd912017-01-04 14:03:07 -08002096 setInitialAnimationValues(true);
Sunny Goyal9e76f682017-02-13 12:13:43 -08002097 a = LauncherAnimUtils.ofPropertyValuesHolder(child,
2098 new PropertyListBuilder()
2099 .scale(initScale)
2100 .translationX(initDeltaX)
2101 .translationY(initDeltaY)
2102 .build())
2103 .setDuration(REORDER_ANIMATION_DURATION);
Sunny Goyal5d2fc322015-07-06 22:52:49 -07002104 a.setInterpolator(new android.view.animation.DecelerateInterpolator(1.5f));
2105 a.start();
Brandon Keely50e6e562012-05-08 16:28:49 -07002106 }
Adam Cohen19f37922012-03-21 11:59:11 -07002107 }
2108
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002109 private void completeAndClearReorderPreviewAnimations() {
2110 for (ReorderPreviewAnimation a: mShakeAnimators.values()) {
Brandon Keely50e6e562012-05-08 16:28:49 -07002111 a.completeAnimationImmediately();
Adam Cohen19f37922012-03-21 11:59:11 -07002112 }
2113 mShakeAnimators.clear();
2114 }
2115
Adam Cohen482ed822012-03-02 14:15:13 -08002116 private void commitTempPlacement() {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07002117 mTmpOccupied.copyTo(mOccupied);
Sunny Goyalaa8ef112015-06-12 20:04:41 -07002118
2119 long screenId = mLauncher.getWorkspace().getIdForScreen(this);
2120 int container = Favorites.CONTAINER_DESKTOP;
2121
Sunny Goyalc13403c2016-11-18 23:44:48 -08002122 if (mContainerType == HOTSEAT) {
Sunny Goyalaa8ef112015-06-12 20:04:41 -07002123 screenId = -1;
2124 container = Favorites.CONTAINER_HOTSEAT;
2125 }
2126
Michael Jurkaa52570f2012-03-20 03:18:20 -07002127 int childCount = mShortcutsAndWidgets.getChildCount();
Adam Cohen482ed822012-03-02 14:15:13 -08002128 for (int i = 0; i < childCount; i++) {
Adam Cohenea889a22012-03-27 16:45:39 -07002129 View child = mShortcutsAndWidgets.getChildAt(i);
2130 LayoutParams lp = (LayoutParams) child.getLayoutParams();
2131 ItemInfo info = (ItemInfo) child.getTag();
Adam Cohen2acce882012-03-28 19:03:19 -07002132 // We do a null check here because the item info can be null in the case of the
2133 // AllApps button in the hotseat.
2134 if (info != null) {
Sunny Goyalaa8ef112015-06-12 20:04:41 -07002135 final boolean requiresDbUpdate = (info.cellX != lp.tmpCellX
2136 || info.cellY != lp.tmpCellY || info.spanX != lp.cellHSpan
2137 || info.spanY != lp.cellVSpan);
2138
Adam Cohen2acce882012-03-28 19:03:19 -07002139 info.cellX = lp.cellX = lp.tmpCellX;
2140 info.cellY = lp.cellY = lp.tmpCellY;
Adam Cohenbebf0422012-04-11 18:06:28 -07002141 info.spanX = lp.cellHSpan;
2142 info.spanY = lp.cellVSpan;
Sunny Goyalaa8ef112015-06-12 20:04:41 -07002143
2144 if (requiresDbUpdate) {
Sunny Goyal43bf11d2017-02-02 13:52:53 -08002145 mLauncher.getModelWriter().modifyItemInDatabase(info, container, screenId,
Sunny Goyalaa8ef112015-06-12 20:04:41 -07002146 info.cellX, info.cellY, info.spanX, info.spanY);
2147 }
Adam Cohen2acce882012-03-28 19:03:19 -07002148 }
Adam Cohen482ed822012-03-02 14:15:13 -08002149 }
2150 }
2151
Sunny Goyalf7a29e82015-04-24 15:20:43 -07002152 private void setUseTempCoords(boolean useTempCoords) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07002153 int childCount = mShortcutsAndWidgets.getChildCount();
Adam Cohen482ed822012-03-02 14:15:13 -08002154 for (int i = 0; i < childCount; i++) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07002155 LayoutParams lp = (LayoutParams) mShortcutsAndWidgets.getChildAt(i).getLayoutParams();
Adam Cohen482ed822012-03-02 14:15:13 -08002156 lp.useTmpCoords = useTempCoords;
2157 }
2158 }
2159
Sunny Goyalf7a29e82015-04-24 15:20:43 -07002160 private ItemConfiguration findConfigurationNoShuffle(int pixelX, int pixelY, int minSpanX, int minSpanY,
Adam Cohen482ed822012-03-02 14:15:13 -08002161 int spanX, int spanY, View dragView, ItemConfiguration solution) {
2162 int[] result = new int[2];
2163 int[] resultSpan = new int[2];
Sunny Goyalf7a29e82015-04-24 15:20:43 -07002164 findNearestVacantArea(pixelX, pixelY, minSpanX, minSpanY, spanX, spanY, result,
Adam Cohen482ed822012-03-02 14:15:13 -08002165 resultSpan);
2166 if (result[0] >= 0 && result[1] >= 0) {
2167 copyCurrentStateToSolution(solution, false);
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07002168 solution.cellX = result[0];
2169 solution.cellY = result[1];
2170 solution.spanX = resultSpan[0];
2171 solution.spanY = resultSpan[1];
Adam Cohen482ed822012-03-02 14:15:13 -08002172 solution.isSolution = true;
2173 } else {
2174 solution.isSolution = false;
2175 }
2176 return solution;
2177 }
2178
Adam Cohen19f37922012-03-21 11:59:11 -07002179 /* This seems like it should be obvious and straight-forward, but when the direction vector
2180 needs to match with the notion of the dragView pushing other views, we have to employ
2181 a slightly more subtle notion of the direction vector. The question is what two points is
2182 the vector between? The center of the dragView and its desired destination? Not quite, as
2183 this doesn't necessarily coincide with the interaction of the dragView and items occupying
2184 those cells. Instead we use some heuristics to often lock the vector to up, down, left
2185 or right, which helps make pushing feel right.
2186 */
2187 private void getDirectionVectorForDrop(int dragViewCenterX, int dragViewCenterY, int spanX,
2188 int spanY, View dragView, int[] resultDirection) {
2189 int[] targetDestination = new int[2];
2190
2191 findNearestArea(dragViewCenterX, dragViewCenterY, spanX, spanY, targetDestination);
2192 Rect dragRect = new Rect();
2193 regionToRect(targetDestination[0], targetDestination[1], spanX, spanY, dragRect);
2194 dragRect.offset(dragViewCenterX - dragRect.centerX(), dragViewCenterY - dragRect.centerY());
2195
2196 Rect dropRegionRect = new Rect();
2197 getViewsIntersectingRegion(targetDestination[0], targetDestination[1], spanX, spanY,
2198 dragView, dropRegionRect, mIntersectingViews);
2199
2200 int dropRegionSpanX = dropRegionRect.width();
2201 int dropRegionSpanY = dropRegionRect.height();
2202
2203 regionToRect(dropRegionRect.left, dropRegionRect.top, dropRegionRect.width(),
2204 dropRegionRect.height(), dropRegionRect);
2205
2206 int deltaX = (dropRegionRect.centerX() - dragViewCenterX) / spanX;
2207 int deltaY = (dropRegionRect.centerY() - dragViewCenterY) / spanY;
2208
2209 if (dropRegionSpanX == mCountX || spanX == mCountX) {
2210 deltaX = 0;
2211 }
2212 if (dropRegionSpanY == mCountY || spanY == mCountY) {
2213 deltaY = 0;
2214 }
2215
2216 if (deltaX == 0 && deltaY == 0) {
2217 // No idea what to do, give a random direction.
2218 resultDirection[0] = 1;
2219 resultDirection[1] = 0;
2220 } else {
2221 computeDirectionVector(deltaX, deltaY, resultDirection);
2222 }
2223 }
2224
2225 // For a given cell and span, fetch the set of views intersecting the region.
2226 private void getViewsIntersectingRegion(int cellX, int cellY, int spanX, int spanY,
2227 View dragView, Rect boundingRect, ArrayList<View> intersectingViews) {
2228 if (boundingRect != null) {
2229 boundingRect.set(cellX, cellY, cellX + spanX, cellY + spanY);
2230 }
2231 intersectingViews.clear();
2232 Rect r0 = new Rect(cellX, cellY, cellX + spanX, cellY + spanY);
2233 Rect r1 = new Rect();
2234 final int count = mShortcutsAndWidgets.getChildCount();
2235 for (int i = 0; i < count; i++) {
2236 View child = mShortcutsAndWidgets.getChildAt(i);
2237 if (child == dragView) continue;
2238 LayoutParams lp = (LayoutParams) child.getLayoutParams();
2239 r1.set(lp.cellX, lp.cellY, lp.cellX + lp.cellHSpan, lp.cellY + lp.cellVSpan);
2240 if (Rect.intersects(r0, r1)) {
2241 mIntersectingViews.add(child);
2242 if (boundingRect != null) {
2243 boundingRect.union(r1);
2244 }
2245 }
2246 }
2247 }
2248
2249 boolean isNearestDropLocationOccupied(int pixelX, int pixelY, int spanX, int spanY,
2250 View dragView, int[] result) {
2251 result = findNearestArea(pixelX, pixelY, spanX, spanY, result);
2252 getViewsIntersectingRegion(result[0], result[1], spanX, spanY, dragView, null,
2253 mIntersectingViews);
2254 return !mIntersectingViews.isEmpty();
2255 }
2256
2257 void revertTempState() {
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002258 completeAndClearReorderPreviewAnimations();
2259 if (isItemPlacementDirty() && !DESTRUCTIVE_REORDER) {
2260 final int count = mShortcutsAndWidgets.getChildCount();
2261 for (int i = 0; i < count; i++) {
2262 View child = mShortcutsAndWidgets.getChildAt(i);
2263 LayoutParams lp = (LayoutParams) child.getLayoutParams();
2264 if (lp.tmpCellX != lp.cellX || lp.tmpCellY != lp.cellY) {
2265 lp.tmpCellX = lp.cellX;
2266 lp.tmpCellY = lp.cellY;
2267 animateChildToPosition(child, lp.cellX, lp.cellY, REORDER_ANIMATION_DURATION,
2268 0, false, false);
2269 }
Adam Cohen19f37922012-03-21 11:59:11 -07002270 }
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002271 setItemPlacementDirty(false);
Adam Cohen19f37922012-03-21 11:59:11 -07002272 }
Adam Cohen19f37922012-03-21 11:59:11 -07002273 }
2274
Adam Cohenbebf0422012-04-11 18:06:28 -07002275 boolean createAreaForResize(int cellX, int cellY, int spanX, int spanY,
2276 View dragView, int[] direction, boolean commit) {
2277 int[] pixelXY = new int[2];
2278 regionToCenterPoint(cellX, cellY, spanX, spanY, pixelXY);
2279
2280 // First we determine if things have moved enough to cause a different layout
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002281 ItemConfiguration swapSolution = findReorderSolution(pixelXY[0], pixelXY[1], spanX, spanY,
Adam Cohenbebf0422012-04-11 18:06:28 -07002282 spanX, spanY, direction, dragView, true, new ItemConfiguration());
2283
2284 setUseTempCoords(true);
2285 if (swapSolution != null && swapSolution.isSolution) {
2286 // If we're just testing for a possible location (MODE_ACCEPT_DROP), we don't bother
2287 // committing anything or animating anything as we just want to determine if a solution
2288 // exists
2289 copySolutionToTempState(swapSolution, dragView);
2290 setItemPlacementDirty(true);
2291 animateItemsToSolution(swapSolution, dragView, commit);
2292
2293 if (commit) {
2294 commitTempPlacement();
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002295 completeAndClearReorderPreviewAnimations();
Adam Cohenbebf0422012-04-11 18:06:28 -07002296 setItemPlacementDirty(false);
2297 } else {
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002298 beginOrAdjustReorderPreviewAnimations(swapSolution, dragView,
2299 REORDER_ANIMATION_DURATION, ReorderPreviewAnimation.MODE_PREVIEW);
Adam Cohenbebf0422012-04-11 18:06:28 -07002300 }
2301 mShortcutsAndWidgets.requestLayout();
2302 }
2303 return swapSolution.isSolution;
2304 }
2305
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002306 int[] performReorder(int pixelX, int pixelY, int minSpanX, int minSpanY, int spanX, int spanY,
Adam Cohen482ed822012-03-02 14:15:13 -08002307 View dragView, int[] result, int resultSpan[], int mode) {
Adam Cohen482ed822012-03-02 14:15:13 -08002308 // First we determine if things have moved enough to cause a different layout
Adam Cohen47a876d2012-03-19 13:21:41 -07002309 result = findNearestArea(pixelX, pixelY, spanX, spanY, result);
Adam Cohen482ed822012-03-02 14:15:13 -08002310
2311 if (resultSpan == null) {
2312 resultSpan = new int[2];
2313 }
2314
Adam Cohen19f37922012-03-21 11:59:11 -07002315 // When we are checking drop validity or actually dropping, we don't recompute the
2316 // direction vector, since we want the solution to match the preview, and it's possible
2317 // that the exact position of the item has changed to result in a new reordering outcome.
Adam Cohenb209e632012-03-27 17:09:36 -07002318 if ((mode == MODE_ON_DROP || mode == MODE_ON_DROP_EXTERNAL || mode == MODE_ACCEPT_DROP)
2319 && mPreviousReorderDirection[0] != INVALID_DIRECTION) {
Adam Cohen19f37922012-03-21 11:59:11 -07002320 mDirectionVector[0] = mPreviousReorderDirection[0];
2321 mDirectionVector[1] = mPreviousReorderDirection[1];
2322 // We reset this vector after drop
Adam Cohenb209e632012-03-27 17:09:36 -07002323 if (mode == MODE_ON_DROP || mode == MODE_ON_DROP_EXTERNAL) {
2324 mPreviousReorderDirection[0] = INVALID_DIRECTION;
2325 mPreviousReorderDirection[1] = INVALID_DIRECTION;
Adam Cohen19f37922012-03-21 11:59:11 -07002326 }
2327 } else {
2328 getDirectionVectorForDrop(pixelX, pixelY, spanX, spanY, dragView, mDirectionVector);
2329 mPreviousReorderDirection[0] = mDirectionVector[0];
2330 mPreviousReorderDirection[1] = mDirectionVector[1];
2331 }
2332
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002333 // Find a solution involving pushing / displacing any items in the way
2334 ItemConfiguration swapSolution = findReorderSolution(pixelX, pixelY, minSpanX, minSpanY,
Adam Cohen482ed822012-03-02 14:15:13 -08002335 spanX, spanY, mDirectionVector, dragView, true, new ItemConfiguration());
2336
2337 // We attempt the approach which doesn't shuffle views at all
2338 ItemConfiguration noShuffleSolution = findConfigurationNoShuffle(pixelX, pixelY, minSpanX,
2339 minSpanY, spanX, spanY, dragView, new ItemConfiguration());
2340
2341 ItemConfiguration finalSolution = null;
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002342
2343 // If the reorder solution requires resizing (shrinking) the item being dropped, we instead
2344 // favor a solution in which the item is not resized, but
Adam Cohen482ed822012-03-02 14:15:13 -08002345 if (swapSolution.isSolution && swapSolution.area() >= noShuffleSolution.area()) {
2346 finalSolution = swapSolution;
2347 } else if (noShuffleSolution.isSolution) {
2348 finalSolution = noShuffleSolution;
2349 }
2350
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002351 if (mode == MODE_SHOW_REORDER_HINT) {
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002352 if (finalSolution != null) {
Adam Cohenfe692872013-12-11 14:47:23 -08002353 beginOrAdjustReorderPreviewAnimations(finalSolution, dragView, 0,
2354 ReorderPreviewAnimation.MODE_HINT);
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07002355 result[0] = finalSolution.cellX;
2356 result[1] = finalSolution.cellY;
2357 resultSpan[0] = finalSolution.spanX;
2358 resultSpan[1] = finalSolution.spanY;
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002359 } else {
2360 result[0] = result[1] = resultSpan[0] = resultSpan[1] = -1;
2361 }
2362 return result;
2363 }
2364
Adam Cohen482ed822012-03-02 14:15:13 -08002365 boolean foundSolution = true;
2366 if (!DESTRUCTIVE_REORDER) {
2367 setUseTempCoords(true);
2368 }
2369
2370 if (finalSolution != null) {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07002371 result[0] = finalSolution.cellX;
2372 result[1] = finalSolution.cellY;
2373 resultSpan[0] = finalSolution.spanX;
2374 resultSpan[1] = finalSolution.spanY;
Adam Cohen482ed822012-03-02 14:15:13 -08002375
2376 // If we're just testing for a possible location (MODE_ACCEPT_DROP), we don't bother
2377 // committing anything or animating anything as we just want to determine if a solution
2378 // exists
2379 if (mode == MODE_DRAG_OVER || mode == MODE_ON_DROP || mode == MODE_ON_DROP_EXTERNAL) {
2380 if (!DESTRUCTIVE_REORDER) {
2381 copySolutionToTempState(finalSolution, dragView);
2382 }
2383 setItemPlacementDirty(true);
2384 animateItemsToSolution(finalSolution, dragView, mode == MODE_ON_DROP);
2385
Adam Cohen19f37922012-03-21 11:59:11 -07002386 if (!DESTRUCTIVE_REORDER &&
2387 (mode == MODE_ON_DROP || mode == MODE_ON_DROP_EXTERNAL)) {
Adam Cohen482ed822012-03-02 14:15:13 -08002388 commitTempPlacement();
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002389 completeAndClearReorderPreviewAnimations();
Adam Cohen19f37922012-03-21 11:59:11 -07002390 setItemPlacementDirty(false);
2391 } else {
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002392 beginOrAdjustReorderPreviewAnimations(finalSolution, dragView,
2393 REORDER_ANIMATION_DURATION, ReorderPreviewAnimation.MODE_PREVIEW);
Adam Cohen482ed822012-03-02 14:15:13 -08002394 }
2395 }
2396 } else {
2397 foundSolution = false;
2398 result[0] = result[1] = resultSpan[0] = resultSpan[1] = -1;
2399 }
2400
2401 if ((mode == MODE_ON_DROP || !foundSolution) && !DESTRUCTIVE_REORDER) {
2402 setUseTempCoords(false);
2403 }
Adam Cohen482ed822012-03-02 14:15:13 -08002404
Michael Jurkaa52570f2012-03-20 03:18:20 -07002405 mShortcutsAndWidgets.requestLayout();
Adam Cohen482ed822012-03-02 14:15:13 -08002406 return result;
2407 }
2408
Adam Cohen19f37922012-03-21 11:59:11 -07002409 void setItemPlacementDirty(boolean dirty) {
2410 mItemPlacementDirty = dirty;
Adam Cohen482ed822012-03-02 14:15:13 -08002411 }
Adam Cohen19f37922012-03-21 11:59:11 -07002412 boolean isItemPlacementDirty() {
2413 return mItemPlacementDirty;
Adam Cohen482ed822012-03-02 14:15:13 -08002414 }
2415
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07002416 private static class ItemConfiguration extends CellAndSpan {
Rajeev Kumar9962dbe2017-06-12 12:16:20 -07002417 final ArrayMap<View, CellAndSpan> map = new ArrayMap<>();
2418 private final ArrayMap<View, CellAndSpan> savedMap = new ArrayMap<>();
2419 final ArrayList<View> sortedViews = new ArrayList<>();
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002420 ArrayList<View> intersectingViews;
Adam Cohen482ed822012-03-02 14:15:13 -08002421 boolean isSolution = false;
Adam Cohen482ed822012-03-02 14:15:13 -08002422
Adam Cohenf3900c22012-11-16 18:28:11 -08002423 void save() {
2424 // Copy current state into savedMap
2425 for (View v: map.keySet()) {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07002426 savedMap.get(v).copyFrom(map.get(v));
Adam Cohenf3900c22012-11-16 18:28:11 -08002427 }
2428 }
2429
2430 void restore() {
2431 // Restore current state from savedMap
2432 for (View v: savedMap.keySet()) {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07002433 map.get(v).copyFrom(savedMap.get(v));
Adam Cohenf3900c22012-11-16 18:28:11 -08002434 }
2435 }
2436
2437 void add(View v, CellAndSpan cs) {
2438 map.put(v, cs);
2439 savedMap.put(v, new CellAndSpan());
2440 sortedViews.add(v);
2441 }
2442
Adam Cohen482ed822012-03-02 14:15:13 -08002443 int area() {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07002444 return spanX * spanY;
Adam Cohenf3900c22012-11-16 18:28:11 -08002445 }
2446
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07002447 void getBoundingRectForViews(ArrayList<View> views, Rect outRect) {
2448 boolean first = true;
2449 for (View v: views) {
2450 CellAndSpan c = map.get(v);
2451 if (first) {
2452 outRect.set(c.cellX, c.cellY, c.cellX + c.spanX, c.cellY + c.spanY);
2453 first = false;
2454 } else {
2455 outRect.union(c.cellX, c.cellY, c.cellX + c.spanX, c.cellY + c.spanY);
2456 }
2457 }
Adam Cohenf3900c22012-11-16 18:28:11 -08002458 }
Adam Cohen482ed822012-03-02 14:15:13 -08002459 }
2460
Adam Cohendf035382011-04-11 17:22:04 -07002461 /**
Adam Cohendf035382011-04-11 17:22:04 -07002462 * Find a starting cell position that will fit the given bounds nearest the requested
2463 * cell location. Uses Euclidean distance to score multiple vacant areas.
2464 *
2465 * @param pixelX The X location at which you want to search for a vacant area.
2466 * @param pixelY The Y location at which you want to search for a vacant area.
2467 * @param spanX Horizontal span of the object.
2468 * @param spanY Vertical span of the object.
Adam Cohendf035382011-04-11 17:22:04 -07002469 * @param result Previously returned value to possibly recycle.
2470 * @return The X, Y cell of a vacant area that can contain this object,
2471 * nearest the requested location.
2472 */
Adam Cohenf9c184a2016-01-15 16:47:43 -08002473 public int[] findNearestArea(int pixelX, int pixelY, int spanX, int spanY, int[] result) {
Sunny Goyalf7a29e82015-04-24 15:20:43 -07002474 return findNearestArea(pixelX, pixelY, spanX, spanY, spanX, spanY, false, result, null);
Adam Cohendf035382011-04-11 17:22:04 -07002475 }
2476
Michael Jurka0280c3b2010-09-17 15:00:07 -07002477 boolean existsEmptyCell() {
2478 return findCellForSpan(null, 1, 1);
2479 }
2480
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002481 /**
Michael Jurka0280c3b2010-09-17 15:00:07 -07002482 * Finds the upper-left coordinate of the first rectangle in the grid that can
2483 * hold a cell of the specified dimensions. If intersectX and intersectY are not -1,
2484 * then this method will only return coordinates for rectangles that contain the cell
2485 * (intersectX, intersectY)
2486 *
2487 * @param cellXY The array that will contain the position of a vacant cell if such a cell
2488 * can be found.
2489 * @param spanX The horizontal span of the cell we want to find.
2490 * @param spanY The vertical span of the cell we want to find.
2491 *
2492 * @return True if a vacant cell of the specified dimension was found, false otherwise.
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07002493 */
Hyunyoung Song3f471442015-04-08 19:01:34 -07002494 public boolean findCellForSpan(int[] cellXY, int spanX, int spanY) {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07002495 if (cellXY == null) {
2496 cellXY = new int[2];
Michael Jurka0280c3b2010-09-17 15:00:07 -07002497 }
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07002498 return mOccupied.findVacantCell(cellXY, spanX, spanY);
Michael Jurka0280c3b2010-09-17 15:00:07 -07002499 }
2500
2501 /**
Winson Chungc07918d2011-07-01 15:35:26 -07002502 * A drag event has begun over this layout.
2503 * It may have begun over this layout (in which case onDragChild is called first),
2504 * or it may have begun on another layout.
2505 */
2506 void onDragEnter() {
Winson Chungc07918d2011-07-01 15:35:26 -07002507 mDragging = true;
2508 }
2509
2510 /**
Michael Jurka0280c3b2010-09-17 15:00:07 -07002511 * Called when drag has left this CellLayout or has been completed (successfully or not)
2512 */
2513 void onDragExit() {
Joe Onorato4be866d2010-10-10 11:26:02 -07002514 // This can actually be called when we aren't in a drag, e.g. when adding a new
2515 // item to this layout via the customize drawer.
2516 // Guard against that case.
2517 if (mDragging) {
2518 mDragging = false;
Patrick Dubroyde7658b2010-09-27 11:15:43 -07002519 }
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -07002520
2521 // Invalidate the drag data
Adam Cohend41fbf52012-02-16 23:53:59 -08002522 mDragCell[0] = mDragCell[1] = -1;
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -07002523 mDragOutlineAnims[mDragOutlineCurrent].animateOut();
2524 mDragOutlineCurrent = (mDragOutlineCurrent + 1) % mDragOutlineAnims.length;
Adam Cohen19f37922012-03-21 11:59:11 -07002525 revertTempState();
Michael Jurka33945b22010-12-21 18:19:38 -08002526 setIsDragOverlapping(false);
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07002527 }
2528
2529 /**
Winson Chungaafa03c2010-06-11 17:34:16 -07002530 * Mark a child as having been dropped.
Patrick Dubroyde7658b2010-09-27 11:15:43 -07002531 * At the beginning of the drag operation, the child may have been on another
Patrick Dubroyce34a972010-10-19 10:34:32 -07002532 * screen, but it is re-parented before this method is called.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002533 *
2534 * @param child The child that is being dropped
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002535 */
Adam Cohen716b51e2011-06-30 12:09:54 -07002536 void onDropChild(View child) {
Romain Guyd94533d2009-08-17 10:01:15 -07002537 if (child != null) {
2538 LayoutParams lp = (LayoutParams) child.getLayoutParams();
Romain Guy84f296c2009-11-04 15:00:44 -08002539 lp.dropped = true;
Romain Guyd94533d2009-08-17 10:01:15 -07002540 child.requestLayout();
Tony Wickham1cdb6d02015-09-17 11:08:27 -07002541 markCellsAsOccupiedForView(child);
Romain Guyd94533d2009-08-17 10:01:15 -07002542 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002543 }
2544
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002545 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002546 * Computes a bounding rectangle for a range of cells
Winson Chungaafa03c2010-06-11 17:34:16 -07002547 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002548 * @param cellX X coordinate of upper left corner expressed as a cell position
2549 * @param cellY Y coordinate of upper left corner expressed as a cell position
Winson Chungaafa03c2010-06-11 17:34:16 -07002550 * @param cellHSpan Width in cells
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002551 * @param cellVSpan Height in cells
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07002552 * @param resultRect Rect into which to put the results
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002553 */
Adam Cohend41fbf52012-02-16 23:53:59 -08002554 public void cellToRect(int cellX, int cellY, int cellHSpan, int cellVSpan, Rect resultRect) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002555 final int cellWidth = mCellWidth;
2556 final int cellHeight = mCellHeight;
Winson Chungaafa03c2010-06-11 17:34:16 -07002557
Winson Chung4b825dcd2011-06-19 12:41:22 -07002558 final int hStartPadding = getPaddingLeft();
2559 final int vStartPadding = getPaddingTop();
Winson Chungaafa03c2010-06-11 17:34:16 -07002560
Sunny Goyalaa8a8712016-11-20 15:26:01 +05302561 int width = cellHSpan * cellWidth;
2562 int height = cellVSpan * cellHeight;
2563 int x = hStartPadding + cellX * cellWidth;
2564 int y = vStartPadding + cellY * cellHeight;
Winson Chungaafa03c2010-06-11 17:34:16 -07002565
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07002566 resultRect.set(x, y, x + width, y + height);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002567 }
Winson Chungaafa03c2010-06-11 17:34:16 -07002568
Adam Cohend4844c32011-02-18 19:25:06 -08002569 public void markCellsAsOccupiedForView(View view) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07002570 if (view == null || view.getParent() != mShortcutsAndWidgets) return;
Michael Jurka0280c3b2010-09-17 15:00:07 -07002571 LayoutParams lp = (LayoutParams) view.getLayoutParams();
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07002572 mOccupied.markCells(lp.cellX, lp.cellY, lp.cellHSpan, lp.cellVSpan, true);
Michael Jurka0280c3b2010-09-17 15:00:07 -07002573 }
2574
Adam Cohend4844c32011-02-18 19:25:06 -08002575 public void markCellsAsUnoccupiedForView(View view) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07002576 if (view == null || view.getParent() != mShortcutsAndWidgets) return;
Michael Jurka0280c3b2010-09-17 15:00:07 -07002577 LayoutParams lp = (LayoutParams) view.getLayoutParams();
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07002578 mOccupied.markCells(lp.cellX, lp.cellY, lp.cellHSpan, lp.cellVSpan, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002579 }
2580
Adam Cohen2801caf2011-05-13 20:57:39 -07002581 public int getDesiredWidth() {
Sunny Goyalaa8a8712016-11-20 15:26:01 +05302582 return getPaddingLeft() + getPaddingRight() + (mCountX * mCellWidth);
Adam Cohen2801caf2011-05-13 20:57:39 -07002583 }
2584
2585 public int getDesiredHeight() {
Sunny Goyalaa8a8712016-11-20 15:26:01 +05302586 return getPaddingTop() + getPaddingBottom() + (mCountY * mCellHeight);
Adam Cohen2801caf2011-05-13 20:57:39 -07002587 }
2588
Michael Jurka66d72172011-04-12 16:29:25 -07002589 public boolean isOccupied(int x, int y) {
2590 if (x < mCountX && y < mCountY) {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07002591 return mOccupied.cells[x][y];
Michael Jurka66d72172011-04-12 16:29:25 -07002592 } else {
2593 throw new RuntimeException("Position exceeds the bound of this CellLayout");
2594 }
2595 }
2596
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002597 @Override
2598 public ViewGroup.LayoutParams generateLayoutParams(AttributeSet attrs) {
2599 return new CellLayout.LayoutParams(getContext(), attrs);
2600 }
2601
2602 @Override
2603 protected boolean checkLayoutParams(ViewGroup.LayoutParams p) {
2604 return p instanceof CellLayout.LayoutParams;
2605 }
2606
2607 @Override
2608 protected ViewGroup.LayoutParams generateLayoutParams(ViewGroup.LayoutParams p) {
2609 return new CellLayout.LayoutParams(p);
2610 }
2611
2612 public static class LayoutParams extends ViewGroup.MarginLayoutParams {
2613 /**
2614 * Horizontal location of the item in the grid.
2615 */
2616 @ViewDebug.ExportedProperty
2617 public int cellX;
2618
2619 /**
2620 * Vertical location of the item in the grid.
2621 */
2622 @ViewDebug.ExportedProperty
2623 public int cellY;
2624
2625 /**
Adam Cohen482ed822012-03-02 14:15:13 -08002626 * Temporary horizontal location of the item in the grid during reorder
2627 */
2628 public int tmpCellX;
2629
2630 /**
2631 * Temporary vertical location of the item in the grid during reorder
2632 */
2633 public int tmpCellY;
2634
2635 /**
2636 * Indicates that the temporary coordinates should be used to layout the items
2637 */
2638 public boolean useTmpCoords;
2639
2640 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002641 * Number of cells spanned horizontally by the item.
2642 */
2643 @ViewDebug.ExportedProperty
2644 public int cellHSpan;
2645
2646 /**
2647 * Number of cells spanned vertically by the item.
2648 */
2649 @ViewDebug.ExportedProperty
2650 public int cellVSpan;
Winson Chungaafa03c2010-06-11 17:34:16 -07002651
Adam Cohen1b607ed2011-03-03 17:26:50 -08002652 /**
2653 * Indicates whether the item will set its x, y, width and height parameters freely,
2654 * or whether these will be computed based on cellX, cellY, cellHSpan and cellVSpan.
2655 */
Adam Cohend4844c32011-02-18 19:25:06 -08002656 public boolean isLockedToGrid = true;
2657
Adam Cohen482ed822012-03-02 14:15:13 -08002658 /**
Adam Cohenec40b2b2013-07-23 15:52:40 -07002659 * Indicates that this item should use the full extents of its parent.
2660 */
2661 public boolean isFullscreen = false;
2662
2663 /**
Adam Cohen482ed822012-03-02 14:15:13 -08002664 * Indicates whether this item can be reordered. Always true except in the case of the
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07002665 * the AllApps button and QSB place holder.
Adam Cohen482ed822012-03-02 14:15:13 -08002666 */
2667 public boolean canReorder = true;
2668
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002669 // X coordinate of the view in the layout.
2670 @ViewDebug.ExportedProperty
Vadim Tryshevfedca432015-08-19 17:55:02 -07002671 public int x;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002672 // Y coordinate of the view in the layout.
2673 @ViewDebug.ExportedProperty
Vadim Tryshevfedca432015-08-19 17:55:02 -07002674 public int y;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002675
Romain Guy84f296c2009-11-04 15:00:44 -08002676 boolean dropped;
Romain Guyfcb9e712009-10-02 16:06:52 -07002677
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002678 public LayoutParams(Context c, AttributeSet attrs) {
2679 super(c, attrs);
2680 cellHSpan = 1;
2681 cellVSpan = 1;
2682 }
2683
2684 public LayoutParams(ViewGroup.LayoutParams source) {
2685 super(source);
2686 cellHSpan = 1;
2687 cellVSpan = 1;
2688 }
Winson Chungaafa03c2010-06-11 17:34:16 -07002689
2690 public LayoutParams(LayoutParams source) {
2691 super(source);
2692 this.cellX = source.cellX;
2693 this.cellY = source.cellY;
2694 this.cellHSpan = source.cellHSpan;
2695 this.cellVSpan = source.cellVSpan;
2696 }
2697
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002698 public LayoutParams(int cellX, int cellY, int cellHSpan, int cellVSpan) {
Romain Guy8f19cdd2010-01-08 15:07:00 -08002699 super(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002700 this.cellX = cellX;
2701 this.cellY = cellY;
2702 this.cellHSpan = cellHSpan;
2703 this.cellVSpan = cellVSpan;
2704 }
2705
Sunny Goyalaa8a8712016-11-20 15:26:01 +05302706 public void setup(int cellWidth, int cellHeight, boolean invertHorizontally, int colCount) {
Jon Miranda7ae64ff2016-11-21 16:18:46 -08002707 setup(cellWidth, cellHeight, invertHorizontally, colCount, 1.0f, 1.0f);
2708 }
2709
2710 /**
2711 * Use this method, as opposed to {@link #setup(int, int, boolean, int)}, if the view needs
2712 * to be scaled.
2713 *
2714 * ie. In multi-window mode, we setup widgets so that they are measured and laid out
2715 * using their full/invariant device profile sizes.
2716 */
2717 public void setup(int cellWidth, int cellHeight, boolean invertHorizontally, int colCount,
2718 float cellScaleX, float cellScaleY) {
Adam Cohend4844c32011-02-18 19:25:06 -08002719 if (isLockedToGrid) {
2720 final int myCellHSpan = cellHSpan;
2721 final int myCellVSpan = cellVSpan;
Adam Cohen2374abf2013-04-16 14:56:57 -07002722 int myCellX = useTmpCoords ? tmpCellX : cellX;
2723 int myCellY = useTmpCoords ? tmpCellY : cellY;
2724
2725 if (invertHorizontally) {
2726 myCellX = colCount - myCellX - cellHSpan;
2727 }
Adam Cohen1b607ed2011-03-03 17:26:50 -08002728
Jon Miranda7ae64ff2016-11-21 16:18:46 -08002729 width = (int) (myCellHSpan * cellWidth / cellScaleX - leftMargin - rightMargin);
2730 height = (int) (myCellVSpan * cellHeight / cellScaleY - topMargin - bottomMargin);
Sunny Goyalaa8a8712016-11-20 15:26:01 +05302731 x = (myCellX * cellWidth + leftMargin);
2732 y = (myCellY * cellHeight + topMargin);
Adam Cohend4844c32011-02-18 19:25:06 -08002733 }
2734 }
Winson Chungaafa03c2010-06-11 17:34:16 -07002735
Winson Chungaafa03c2010-06-11 17:34:16 -07002736 public String toString() {
2737 return "(" + this.cellX + ", " + this.cellY + ")";
2738 }
Adam Cohen7f4eabe2011-04-21 16:19:16 -07002739
2740 public void setWidth(int width) {
2741 this.width = width;
2742 }
2743
2744 public int getWidth() {
2745 return width;
2746 }
2747
2748 public void setHeight(int height) {
2749 this.height = height;
2750 }
2751
2752 public int getHeight() {
2753 return height;
2754 }
2755
2756 public void setX(int x) {
2757 this.x = x;
2758 }
2759
2760 public int getX() {
2761 return x;
2762 }
2763
2764 public void setY(int y) {
2765 this.y = y;
2766 }
2767
2768 public int getY() {
2769 return y;
2770 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002771 }
2772
Michael Jurka0280c3b2010-09-17 15:00:07 -07002773 // This class stores info for two purposes:
2774 // 1. When dragging items (mDragInfo in Workspace), we store the View, its cellX & cellY,
2775 // its spanX, spanY, and the screen it is on
2776 // 2. When long clicking on an empty cell in a CellLayout, we save information about the
2777 // cellX and cellY coordinates and which page was clicked. We then set this as a tag on
2778 // the CellLayout that was long clicked
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07002779 public static final class CellInfo extends CellAndSpan {
Rajeev Kumar9962dbe2017-06-12 12:16:20 -07002780 public final View cell;
2781 final long screenId;
2782 final long container;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002783
Sunny Goyal83a8f042015-05-19 12:52:12 -07002784 public CellInfo(View v, ItemInfo info) {
Adam Cohene0aaa0d2014-05-12 12:44:22 -07002785 cellX = info.cellX;
2786 cellY = info.cellY;
2787 spanX = info.spanX;
2788 spanY = info.spanY;
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07002789 cell = v;
Adam Cohene0aaa0d2014-05-12 12:44:22 -07002790 screenId = info.screenId;
2791 container = info.container;
2792 }
2793
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002794 @Override
2795 public String toString() {
Winson Chungaafa03c2010-06-11 17:34:16 -07002796 return "Cell[view=" + (cell == null ? "null" : cell.getClass())
2797 + ", x=" + cellX + ", y=" + cellY + "]";
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002798 }
2799 }
Michael Jurkad771c962011-08-09 15:00:48 -07002800
Tony Wickham86930612015-09-09 13:50:40 -07002801 /**
2802 * Returns whether an item can be placed in this CellLayout (after rearranging and/or resizing
2803 * if necessary).
2804 */
2805 public boolean hasReorderSolution(ItemInfo itemInfo) {
2806 int[] cellPoint = new int[2];
2807 // Check for a solution starting at every cell.
2808 for (int cellX = 0; cellX < getCountX(); cellX++) {
2809 for (int cellY = 0; cellY < getCountY(); cellY++) {
2810 cellToPoint(cellX, cellY, cellPoint);
2811 if (findReorderSolution(cellPoint[0], cellPoint[1], itemInfo.minSpanX,
2812 itemInfo.minSpanY, itemInfo.spanX, itemInfo.spanY, mDirectionVector, null,
2813 true, new ItemConfiguration()).isSolution) {
2814 return true;
2815 }
2816 }
2817 }
2818 return false;
2819 }
2820
Sunny Goyal9ca9c132015-04-29 14:57:22 -07002821 public boolean isRegionVacant(int x, int y, int spanX, int spanY) {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07002822 return mOccupied.isRegionVacant(x, y, spanX, spanY);
Sunny Goyal9ca9c132015-04-29 14:57:22 -07002823 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002824}