blob: d6c8575d638af56a361cadf434ff80b5b82ec3eb [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;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080092
Patrick Dubroyde7658b2010-09-27 11:15:43 -070093 // These are temporary variables to prevent having to allocate a new object just to
94 // return an (x, y) value from helper functions. Do NOT use them to maintain other state.
Adam Cohen091440a2015-03-18 14:16:05 -070095 @Thunk final int[] mTmpPoint = new int[2];
Sunny Goyal2805e632015-05-20 15:35:32 -070096 @Thunk final int[] mTempLocation = new int[2];
Patrick Dubroy6569f2c2010-07-12 14:25:18 -070097
Sunny Goyalff4ba2d2016-04-02 14:12:34 -070098 private GridOccupancy mOccupied;
99 private GridOccupancy mTmpOccupied;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800100
Michael Jurkadee05892010-07-27 10:01:56 -0700101 private OnTouchListener mInterceptTouchListener;
Rajeev Kumar9962dbe2017-06-12 12:16:20 -0700102 private final StylusEventHelper mStylusEventHelper;
Michael Jurkadee05892010-07-27 10:01:56 -0700103
Jon Mirandaa0233f72017-06-22 18:34:45 -0700104 private final ArrayList<PreviewBackground> mFolderBackgrounds = new ArrayList<>();
105 final PreviewBackground mFolderLeaveBehind = new PreviewBackground();
Adam Cohen69ce2e52011-07-03 19:25:21 -0700106
Michael Jurka5f1c5092010-09-03 14:15:02 -0700107 private float mBackgroundAlpha;
Adam Cohenf34bab52010-09-30 14:11:56 -0700108
Sunny Goyalf5440cb2016-12-14 15:13:00 -0800109 private static final int[] BACKGROUND_STATE_ACTIVE = new int[] { android.R.attr.state_active };
110 private static final int[] BACKGROUND_STATE_DEFAULT = new int[0];
111 private final Drawable mBackground;
Sunny Goyal2805e632015-05-20 15:35:32 -0700112
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700113 // These values allow a fixed measurement to be set on the CellLayout.
114 private int mFixedWidth = -1;
115 private int mFixedHeight = -1;
116
Michael Jurka33945b22010-12-21 18:19:38 -0800117 // If we're actively dragging something over this screen, mIsDragOverlapping is true
118 private boolean mIsDragOverlapping = false;
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700119
Winson Chung150fbab2010-09-29 17:14:26 -0700120 // These arrays are used to implement the drag visualization on x-large screens.
Joe Onorato4be866d2010-10-10 11:26:02 -0700121 // They are used as circular arrays, indexed by mDragOutlineCurrent.
Rajeev Kumar9962dbe2017-06-12 12:16:20 -0700122 @Thunk final Rect[] mDragOutlines = new Rect[4];
123 @Thunk final float[] mDragOutlineAlphas = new float[mDragOutlines.length];
124 private final InterruptibleInOutAnimator[] mDragOutlineAnims =
Joe Onorato4be866d2010-10-10 11:26:02 -0700125 new InterruptibleInOutAnimator[mDragOutlines.length];
Winson Chung150fbab2010-09-29 17:14:26 -0700126
127 // Used as an index into the above 3 arrays; indicates which is the most current value.
Joe Onorato4be866d2010-10-10 11:26:02 -0700128 private int mDragOutlineCurrent = 0;
Patrick Dubroy8e58e912010-10-14 13:21:48 -0700129 private final Paint mDragOutlinePaint = new Paint();
Winson Chung150fbab2010-09-29 17:14:26 -0700130
Sunny Goyal4fe5a372015-05-14 19:55:10 -0700131 private final ClickShadowView mTouchFeedbackView;
Patrick Dubroy96864c32011-03-10 17:17:23 -0800132
Rajeev Kumar9962dbe2017-06-12 12:16:20 -0700133 @Thunk final ArrayMap<LayoutParams, Animator> mReorderAnimators = new ArrayMap<>();
134 @Thunk final ArrayMap<View, ReorderPreviewAnimation> mShakeAnimators = new ArrayMap<>();
Adam Cohen19f37922012-03-21 11:59:11 -0700135
136 private boolean mItemPlacementDirty = false;
Adam Cohenbfbfd262011-06-13 16:55:12 -0700137
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700138 // When a drag operation is in progress, holds the nearest cell to the touch point
139 private final int[] mDragCell = new int[2];
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800140
Joe Onorato4be866d2010-10-10 11:26:02 -0700141 private boolean mDragging = false;
142
Rajeev Kumar9962dbe2017-06-12 12:16:20 -0700143 private final TimeInterpolator mEaseOutInterpolator;
144 private final ShortcutAndWidgetContainer mShortcutsAndWidgets;
Patrick Dubroyce34a972010-10-19 10:34:32 -0700145
Sunny Goyalc13403c2016-11-18 23:44:48 -0800146 @Retention(RetentionPolicy.SOURCE)
147 @IntDef({WORKSPACE, HOTSEAT, FOLDER})
148 public @interface ContainerType{}
149 public static final int WORKSPACE = 0;
150 public static final int HOTSEAT = 1;
151 public static final int FOLDER = 2;
152
153 @ContainerType private final int mContainerType;
154
Jon Mirandab28c4fc2017-06-20 10:58:36 -0700155 private final float mChildScale = 1f;
Andrew Flynn0dca1ec2012-02-29 13:33:22 -0800156
Adam Cohenfa3c58f2013-12-06 16:10:55 -0800157 public static final int MODE_SHOW_REORDER_HINT = 0;
158 public static final int MODE_DRAG_OVER = 1;
159 public static final int MODE_ON_DROP = 2;
160 public static final int MODE_ON_DROP_EXTERNAL = 3;
161 public static final int MODE_ACCEPT_DROP = 4;
Adam Cohen19f37922012-03-21 11:59:11 -0700162 private static final boolean DESTRUCTIVE_REORDER = false;
Adam Cohen482ed822012-03-02 14:15:13 -0800163 private static final boolean DEBUG_VISUALIZE_OCCUPIED = false;
164
Adam Cohenfa3c58f2013-12-06 16:10:55 -0800165 private static final float REORDER_PREVIEW_MAGNITUDE = 0.12f;
Adam Cohen19f37922012-03-21 11:59:11 -0700166 private static final int REORDER_ANIMATION_DURATION = 150;
Sunny Goyalc13403c2016-11-18 23:44:48 -0800167 @Thunk final float mReorderPreviewAnimationMagnitude;
Adam Cohen19f37922012-03-21 11:59:11 -0700168
Rajeev Kumar9962dbe2017-06-12 12:16:20 -0700169 private final ArrayList<View> mIntersectingViews = new ArrayList<>();
170 private final Rect mOccupiedRect = new Rect();
171 private final int[] mDirectionVector = new int[2];
172 final int[] mPreviousReorderDirection = new int[2];
Adam Cohenb209e632012-03-27 17:09:36 -0700173 private static final int INVALID_DIRECTION = -100;
Adam Cohen482ed822012-03-02 14:15:13 -0800174
Sunny Goyal2805e632015-05-20 15:35:32 -0700175 private final Rect mTempRect = new Rect();
Winson Chung3a6e7f32013-10-09 15:50:52 -0700176
Michael Jurkaca993832012-06-29 15:17:04 -0700177 private final static Paint sPaint = new Paint();
Romain Guy8a0bff52012-05-06 13:14:33 -0700178
Adam Cohenc9735cf2015-01-23 16:11:55 -0800179 // Related to accessible drag and drop
Sunny Goyale9b651e2015-04-24 11:44:51 -0700180 private DragAndDropAccessibilityDelegate mTouchHelper;
Adam Cohenc9735cf2015-01-23 16:11:55 -0800181 private boolean mUseTouchHelper = false;
Adam Cohenc9735cf2015-01-23 16:11:55 -0800182
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800183 public CellLayout(Context context) {
184 this(context, null);
185 }
186
187 public CellLayout(Context context, AttributeSet attrs) {
188 this(context, attrs, 0);
189 }
190
191 public CellLayout(Context context, AttributeSet attrs, int defStyle) {
192 super(context, attrs, defStyle);
Sunny Goyalc13403c2016-11-18 23:44:48 -0800193 TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.CellLayout, defStyle, 0);
194 mContainerType = a.getInteger(R.styleable.CellLayout_containerType, WORKSPACE);
195 a.recycle();
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700196
197 // A ViewGroup usually does not draw, but CellLayout needs to draw a rectangle to show
198 // the user where a dragged item will land when dropped.
199 setWillNotDraw(false);
Romain Guyce3cbd12013-02-25 15:00:36 -0800200 setClipToPadding(false);
Tony2fd02082016-10-07 12:50:01 -0700201 mLauncher = Launcher.getLauncher(context);
Michael Jurkaa63c4522010-08-19 13:52:27 -0700202
Adam Cohen2e6da152015-05-06 11:42:25 -0700203 DeviceProfile grid = mLauncher.getDeviceProfile();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800204
Winson Chung11a1a532013-09-13 11:14:45 -0700205 mCellWidth = mCellHeight = -1;
Nilesh Agrawal5f7099a2014-01-02 15:54:57 -0800206 mFixedCellWidth = mFixedCellHeight = -1;
Sunny Goyalff4ba2d2016-04-02 14:12:34 -0700207
208 mCountX = grid.inv.numColumns;
209 mCountY = grid.inv.numRows;
210 mOccupied = new GridOccupancy(mCountX, mCountY);
211 mTmpOccupied = new GridOccupancy(mCountX, mCountY);
212
Adam Cohen5b53f292012-03-29 14:30:35 -0700213 mPreviousReorderDirection[0] = INVALID_DIRECTION;
214 mPreviousReorderDirection[1] = INVALID_DIRECTION;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800215
Adam Cohenefca0272016-02-24 19:19:06 -0800216 mFolderLeaveBehind.delegateCellX = -1;
217 mFolderLeaveBehind.delegateCellY = -1;
218
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800219 setAlwaysDrawnWithCacheEnabled(false);
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700220 final Resources res = getResources();
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700221
Sunny Goyalf5440cb2016-12-14 15:13:00 -0800222 mBackground = res.getDrawable(R.drawable.bg_celllayout);
Sunny Goyal2805e632015-05-20 15:35:32 -0700223 mBackground.setCallback(this);
Winson Chunge8f1d042015-07-31 12:39:57 -0700224 mBackground.setAlpha((int) (mBackgroundAlpha * 255));
Michael Jurka33945b22010-12-21 18:19:38 -0800225
Sunny Goyalc13403c2016-11-18 23:44:48 -0800226 mReorderPreviewAnimationMagnitude = (REORDER_PREVIEW_MAGNITUDE * grid.iconSizePx);
Adam Cohen19f37922012-03-21 11:59:11 -0700227
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700228 // Initialize the data structures used for the drag visualization.
Patrick Dubroyce34a972010-10-19 10:34:32 -0700229 mEaseOutInterpolator = new DecelerateInterpolator(2.5f); // Quint ease out
Winson Chungb8c69f32011-10-19 21:36:08 -0700230 mDragCell[0] = mDragCell[1] = -1;
Joe Onorato4be866d2010-10-10 11:26:02 -0700231 for (int i = 0; i < mDragOutlines.length; i++) {
Adam Cohend41fbf52012-02-16 23:53:59 -0800232 mDragOutlines[i] = new Rect(-1, -1, -1, -1);
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700233 }
Mario Bertschler54ba6012017-06-08 10:53:53 -0700234 mDragOutlinePaint.setColor(Themes.getAttrColor(context, R.attr.workspaceTextColor));
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700235
236 // When dragging things around the home screens, we show a green outline of
237 // where the item will land. The outlines gradually fade out, leaving a trail
238 // behind the drag path.
239 // Set up all the animations that are used to implement this fading.
240 final int duration = res.getInteger(R.integer.config_dragOutlineFadeTime);
Chet Haase472b2812010-10-14 07:02:04 -0700241 final float fromAlphaValue = 0;
242 final float toAlphaValue = (float)res.getInteger(R.integer.config_dragOutlineMaxAlpha);
Joe Onorato4be866d2010-10-10 11:26:02 -0700243
Patrick Dubroy8e58e912010-10-14 13:21:48 -0700244 Arrays.fill(mDragOutlineAlphas, fromAlphaValue);
Joe Onorato4be866d2010-10-10 11:26:02 -0700245
246 for (int i = 0; i < mDragOutlineAnims.length; i++) {
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700247 final InterruptibleInOutAnimator anim =
Michael Jurkaf1ad6082013-03-13 12:55:46 +0100248 new InterruptibleInOutAnimator(this, duration, fromAlphaValue, toAlphaValue);
Patrick Dubroyce34a972010-10-19 10:34:32 -0700249 anim.getAnimator().setInterpolator(mEaseOutInterpolator);
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700250 final int thisIndex = i;
Chet Haase472b2812010-10-14 07:02:04 -0700251 anim.getAnimator().addUpdateListener(new AnimatorUpdateListener() {
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700252 public void onAnimationUpdate(ValueAnimator animation) {
Joe Onorato4be866d2010-10-10 11:26:02 -0700253 final Bitmap outline = (Bitmap)anim.getTag();
254
255 // If an animation is started and then stopped very quickly, we can still
256 // get spurious updates we've cleared the tag. Guard against this.
257 if (outline == null) {
Tony Wickhama0628cc2015-10-14 15:23:04 -0700258 if (LOGD) {
Patrick Dubroyfe6bd872010-10-13 17:32:10 -0700259 Object val = animation.getAnimatedValue();
260 Log.d(TAG, "anim " + thisIndex + " update: " + val +
261 ", isStopped " + anim.isStopped());
262 }
Joe Onorato4be866d2010-10-10 11:26:02 -0700263 // Try to prevent it from continuing to run
264 animation.cancel();
265 } else {
Chet Haase472b2812010-10-14 07:02:04 -0700266 mDragOutlineAlphas[thisIndex] = (Float) animation.getAnimatedValue();
Adam Cohend41fbf52012-02-16 23:53:59 -0800267 CellLayout.this.invalidate(mDragOutlines[thisIndex]);
Joe Onorato4be866d2010-10-10 11:26:02 -0700268 }
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700269 }
270 });
Joe Onorato4be866d2010-10-10 11:26:02 -0700271 // The animation holds a reference to the drag outline bitmap as long is it's
272 // running. This way the bitmap can be GCed when the animations are complete.
Chet Haase472b2812010-10-14 07:02:04 -0700273 anim.getAnimator().addListener(new AnimatorListenerAdapter() {
Michael Jurka3c4c20f2010-10-28 15:36:06 -0700274 @Override
Joe Onorato4be866d2010-10-10 11:26:02 -0700275 public void onAnimationEnd(Animator animation) {
Chet Haase472b2812010-10-14 07:02:04 -0700276 if ((Float) ((ValueAnimator) animation).getAnimatedValue() == 0f) {
Joe Onorato4be866d2010-10-10 11:26:02 -0700277 anim.setTag(null);
278 }
279 }
280 });
281 mDragOutlineAnims[i] = anim;
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700282 }
Patrick Dubroyce34a972010-10-19 10:34:32 -0700283
Sunny Goyalc13403c2016-11-18 23:44:48 -0800284 mShortcutsAndWidgets = new ShortcutAndWidgetContainer(context, mContainerType);
Sunny Goyalaa8a8712016-11-20 15:26:01 +0530285 mShortcutsAndWidgets.setCellDimensions(mCellWidth, mCellHeight, mCountX, mCountY);
Adam Cohen2374abf2013-04-16 14:56:57 -0700286
Mady Mellorbb835202015-07-15 16:34:34 -0700287 mStylusEventHelper = new StylusEventHelper(new SimpleOnStylusPressListener(this), this);
Mady Melloref044dd2015-06-02 15:35:07 -0700288
Sunny Goyal4fe5a372015-05-14 19:55:10 -0700289 mTouchFeedbackView = new ClickShadowView(context);
290 addView(mTouchFeedbackView);
Michael Jurkaa52570f2012-03-20 03:18:20 -0700291 addView(mShortcutsAndWidgets);
Michael Jurka18014792010-10-14 09:01:34 -0700292 }
293
Sunny Goyale9b651e2015-04-24 11:44:51 -0700294 public void enableAccessibleDrag(boolean enable, int dragType) {
Adam Cohenc9735cf2015-01-23 16:11:55 -0800295 mUseTouchHelper = enable;
296 if (!enable) {
297 ViewCompat.setAccessibilityDelegate(this, null);
298 setImportantForAccessibility(IMPORTANT_FOR_ACCESSIBILITY_NO);
299 getShortcutsAndWidgets().setImportantForAccessibility(IMPORTANT_FOR_ACCESSIBILITY_NO);
300 setOnClickListener(mLauncher);
301 } else {
Sunny Goyale9b651e2015-04-24 11:44:51 -0700302 if (dragType == WORKSPACE_ACCESSIBILITY_DRAG &&
303 !(mTouchHelper instanceof WorkspaceAccessibilityHelper)) {
304 mTouchHelper = new WorkspaceAccessibilityHelper(this);
305 } else if (dragType == FOLDER_ACCESSIBILITY_DRAG &&
306 !(mTouchHelper instanceof FolderAccessibilityHelper)) {
307 mTouchHelper = new FolderAccessibilityHelper(this);
308 }
Adam Cohenc9735cf2015-01-23 16:11:55 -0800309 ViewCompat.setAccessibilityDelegate(this, mTouchHelper);
310 setImportantForAccessibility(IMPORTANT_FOR_ACCESSIBILITY_YES);
311 getShortcutsAndWidgets().setImportantForAccessibility(IMPORTANT_FOR_ACCESSIBILITY_YES);
312 setOnClickListener(mTouchHelper);
313 }
314
315 // Invalidate the accessibility hierarchy
316 if (getParent() != null) {
317 getParent().notifySubtreeAccessibilityStateChanged(
318 this, this, AccessibilityEvent.CONTENT_CHANGE_TYPE_SUBTREE);
319 }
320 }
321
322 @Override
323 public boolean dispatchHoverEvent(MotionEvent event) {
324 // Always attempt to dispatch hover events to accessibility first.
325 if (mUseTouchHelper && mTouchHelper.dispatchHoverEvent(event)) {
326 return true;
327 }
328 return super.dispatchHoverEvent(event);
329 }
330
331 @Override
Adam Cohenc9735cf2015-01-23 16:11:55 -0800332 public boolean onInterceptTouchEvent(MotionEvent ev) {
333 if (mUseTouchHelper ||
334 (mInterceptTouchListener != null && mInterceptTouchListener.onTouch(this, ev))) {
335 return true;
336 }
337 return false;
338 }
339
Mady Melloref044dd2015-06-02 15:35:07 -0700340 @Override
341 public boolean onTouchEvent(MotionEvent ev) {
342 boolean handled = super.onTouchEvent(ev);
343 // Stylus button press on a home screen should not switch between overview mode and
344 // the home screen mode, however, once in overview mode stylus button press should be
345 // enabled to allow rearranging the different home screens. So check what mode
346 // the workspace is in, and only perform stylus button presses while in overview mode.
347 if (mLauncher.mWorkspace.isInOverviewMode()
Mady Mellorbb835202015-07-15 16:34:34 -0700348 && mStylusEventHelper.onMotionEvent(ev)) {
Mady Melloref044dd2015-06-02 15:35:07 -0700349 return true;
350 }
351 return handled;
352 }
353
Chris Craik01f2d7f2013-10-01 14:41:56 -0700354 public void enableHardwareLayer(boolean hasLayer) {
355 mShortcutsAndWidgets.setLayerType(hasLayer ? LAYER_TYPE_HARDWARE : LAYER_TYPE_NONE, sPaint);
Michael Jurkad51f33a2012-06-28 15:35:26 -0700356 }
357
Winson Chung5f8afe62013-08-12 16:19:28 -0700358 public void setCellDimensions(int width, int height) {
Winson Chung11a1a532013-09-13 11:14:45 -0700359 mFixedCellWidth = mCellWidth = width;
360 mFixedCellHeight = mCellHeight = height;
Sunny Goyalaa8a8712016-11-20 15:26:01 +0530361 mShortcutsAndWidgets.setCellDimensions(mCellWidth, mCellHeight, mCountX, mCountY);
Winson Chung5f8afe62013-08-12 16:19:28 -0700362 }
363
Adam Cohen2801caf2011-05-13 20:57:39 -0700364 public void setGridSize(int x, int y) {
365 mCountX = x;
366 mCountY = y;
Sunny Goyalff4ba2d2016-04-02 14:12:34 -0700367 mOccupied = new GridOccupancy(mCountX, mCountY);
368 mTmpOccupied = new GridOccupancy(mCountX, mCountY);
Adam Cohen7fbec102012-03-27 12:42:19 -0700369 mTempRectStack.clear();
Sunny Goyalaa8a8712016-11-20 15:26:01 +0530370 mShortcutsAndWidgets.setCellDimensions(mCellWidth, mCellHeight, mCountX, mCountY);
Adam Cohen76fc0852011-06-17 13:26:23 -0700371 requestLayout();
Adam Cohen2801caf2011-05-13 20:57:39 -0700372 }
373
Adam Cohen2374abf2013-04-16 14:56:57 -0700374 // Set whether or not to invert the layout horizontally if the layout is in RTL mode.
375 public void setInvertIfRtl(boolean invert) {
376 mShortcutsAndWidgets.setInvertIfRtl(invert);
377 }
378
Adam Cohen917e3882013-10-31 15:03:35 -0700379 public void setDropPending(boolean pending) {
380 mDropPending = pending;
381 }
382
383 public boolean isDropPending() {
384 return mDropPending;
385 }
386
Sunny Goyal4b6eb262015-05-14 19:24:40 -0700387 @Override
388 public void setPressedIcon(BubbleTextView icon, Bitmap background) {
Sunny Goyal508da152014-08-14 10:53:27 -0700389 if (icon == null || background == null) {
390 mTouchFeedbackView.setBitmap(null);
391 mTouchFeedbackView.animate().cancel();
392 } else {
Sunny Goyal508da152014-08-14 10:53:27 -0700393 if (mTouchFeedbackView.setBitmap(background)) {
Winsonbe9798b2016-07-20 12:55:49 -0700394 mTouchFeedbackView.alignWithIconView(icon, mShortcutsAndWidgets,
395 null /* clipAgainstView */);
Sunny Goyal4fe5a372015-05-14 19:55:10 -0700396 mTouchFeedbackView.animateShadow();
Sunny Goyal508da152014-08-14 10:53:27 -0700397 }
Patrick Dubroy96864c32011-03-10 17:17:23 -0800398 }
399 }
400
Adam Cohenc50438c2014-08-19 17:43:05 -0700401 void setIsDragOverlapping(boolean isDragOverlapping) {
402 if (mIsDragOverlapping != isDragOverlapping) {
403 mIsDragOverlapping = isDragOverlapping;
Sunny Goyalf5440cb2016-12-14 15:13:00 -0800404 mBackground.setState(mIsDragOverlapping
405 ? BACKGROUND_STATE_ACTIVE : BACKGROUND_STATE_DEFAULT);
Adam Cohenc50438c2014-08-19 17:43:05 -0700406 invalidate();
407 }
408 }
409
Sunny Goyale2fd14b2015-08-27 17:45:46 -0700410 @Override
411 protected void dispatchSaveInstanceState(SparseArray<Parcelable> container) {
Sunny Goyal7ce471b2017-08-02 03:37:39 -0700412 ParcelableSparseArray jail = getJailedArray(container);
413 super.dispatchSaveInstanceState(jail);
414 container.put(R.id.cell_layout_jail_id, jail);
Sunny Goyale2fd14b2015-08-27 17:45:46 -0700415 }
416
417 @Override
418 protected void dispatchRestoreInstanceState(SparseArray<Parcelable> container) {
Sunny Goyal7ce471b2017-08-02 03:37:39 -0700419 super.dispatchRestoreInstanceState(getJailedArray(container));
Sunny Goyale2fd14b2015-08-27 17:45:46 -0700420 }
421
Sunny Goyal7ce471b2017-08-02 03:37:39 -0700422 /**
423 * Wrap the SparseArray in another Parcelable so that the item ids do not conflict with our
424 * our internal resource ids
425 */
Sunny Goyale2fd14b2015-08-27 17:45:46 -0700426 private ParcelableSparseArray getJailedArray(SparseArray<Parcelable> container) {
427 final Parcelable parcelable = container.get(R.id.cell_layout_jail_id);
428 return parcelable instanceof ParcelableSparseArray ?
429 (ParcelableSparseArray) parcelable : new ParcelableSparseArray();
430 }
431
Tony Wickham0f97b782015-12-02 17:55:07 -0800432 public boolean getIsDragOverlapping() {
433 return mIsDragOverlapping;
434 }
435
Jeff Sharkey83f111d2009-04-20 21:03:13 -0700436 @Override
Patrick Dubroy1262e362010-10-06 15:49:50 -0700437 protected void onDraw(Canvas canvas) {
Michael Jurka3e7c7632010-10-02 16:01:03 -0700438 // When we're large, we are either drawn in a "hover" state (ie when dragging an item to
439 // a neighboring page) or with just a normal background (if backgroundAlpha > 0.0f)
440 // When we're small, we are either drawn normally or in the "accepts drops" state (during
441 // a drag). However, we also drag the mini hover background *over* one of those two
442 // backgrounds
Sunny Goyal05739772015-05-19 19:59:09 -0700443 if (mBackgroundAlpha > 0.0f) {
Sunny Goyal2805e632015-05-20 15:35:32 -0700444 mBackground.draw(canvas);
Michael Jurkaa63c4522010-08-19 13:52:27 -0700445 }
Romain Guya6abce82009-11-10 02:54:41 -0800446
Patrick Dubroy8e58e912010-10-14 13:21:48 -0700447 final Paint paint = mDragOutlinePaint;
Joe Onorato4be866d2010-10-10 11:26:02 -0700448 for (int i = 0; i < mDragOutlines.length; i++) {
Chet Haase472b2812010-10-14 07:02:04 -0700449 final float alpha = mDragOutlineAlphas[i];
Joe Onorato4be866d2010-10-10 11:26:02 -0700450 if (alpha > 0) {
Joe Onorato4be866d2010-10-10 11:26:02 -0700451 final Bitmap b = (Bitmap) mDragOutlineAnims[i].getTag();
Chet Haase472b2812010-10-14 07:02:04 -0700452 paint.setAlpha((int)(alpha + .5f));
Sunny Goyal106bf642015-07-16 12:18:06 -0700453 canvas.drawBitmap(b, null, mDragOutlines[i], paint);
Winson Chung150fbab2010-09-29 17:14:26 -0700454 }
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700455 }
Patrick Dubroy96864c32011-03-10 17:17:23 -0800456
Adam Cohen482ed822012-03-02 14:15:13 -0800457 if (DEBUG_VISUALIZE_OCCUPIED) {
458 int[] pt = new int[2];
459 ColorDrawable cd = new ColorDrawable(Color.RED);
Adam Cohene7587d22012-05-24 18:50:02 -0700460 cd.setBounds(0, 0, mCellWidth, mCellHeight);
Adam Cohen482ed822012-03-02 14:15:13 -0800461 for (int i = 0; i < mCountX; i++) {
462 for (int j = 0; j < mCountY; j++) {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -0700463 if (mOccupied.cells[i][j]) {
Adam Cohen482ed822012-03-02 14:15:13 -0800464 cellToPoint(i, j, pt);
465 canvas.save();
466 canvas.translate(pt[0], pt[1]);
467 cd.draw(canvas);
468 canvas.restore();
469 }
470 }
471 }
472 }
473
Adam Cohenefca0272016-02-24 19:19:06 -0800474 for (int i = 0; i < mFolderBackgrounds.size(); i++) {
Jon Mirandaa0233f72017-06-22 18:34:45 -0700475 PreviewBackground bg = mFolderBackgrounds.get(i);
Adam Cohenefca0272016-02-24 19:19:06 -0800476 cellToPoint(bg.delegateCellX, bg.delegateCellY, mTempLocation);
477 canvas.save();
478 canvas.translate(mTempLocation[0], mTempLocation[1]);
Sunny Goyal19b93b72017-02-19 20:21:37 -0800479 bg.drawBackground(canvas);
Adam Cohenf172b742016-03-30 19:28:34 -0700480 if (!bg.isClipping) {
Sunny Goyal19b93b72017-02-19 20:21:37 -0800481 bg.drawBackgroundStroke(canvas);
Adam Cohenf172b742016-03-30 19:28:34 -0700482 }
Adam Cohenefca0272016-02-24 19:19:06 -0800483 canvas.restore();
Adam Cohen69ce2e52011-07-03 19:25:21 -0700484 }
Adam Cohenc51934b2011-07-26 21:07:43 -0700485
Adam Cohenefca0272016-02-24 19:19:06 -0800486 if (mFolderLeaveBehind.delegateCellX >= 0 && mFolderLeaveBehind.delegateCellY >= 0) {
487 cellToPoint(mFolderLeaveBehind.delegateCellX,
488 mFolderLeaveBehind.delegateCellY, mTempLocation);
489 canvas.save();
490 canvas.translate(mTempLocation[0], mTempLocation[1]);
Sunny Goyal19b93b72017-02-19 20:21:37 -0800491 mFolderLeaveBehind.drawLeaveBehind(canvas);
Adam Cohenefca0272016-02-24 19:19:06 -0800492 canvas.restore();
Adam Cohenc51934b2011-07-26 21:07:43 -0700493 }
Adam Cohen69ce2e52011-07-03 19:25:21 -0700494 }
495
Adam Cohenefca0272016-02-24 19:19:06 -0800496 @Override
497 protected void dispatchDraw(Canvas canvas) {
498 super.dispatchDraw(canvas);
499
500 for (int i = 0; i < mFolderBackgrounds.size(); i++) {
Jon Mirandaa0233f72017-06-22 18:34:45 -0700501 PreviewBackground bg = mFolderBackgrounds.get(i);
Adam Cohenf172b742016-03-30 19:28:34 -0700502 if (bg.isClipping) {
503 cellToPoint(bg.delegateCellX, bg.delegateCellY, mTempLocation);
504 canvas.save();
505 canvas.translate(mTempLocation[0], mTempLocation[1]);
Sunny Goyal19b93b72017-02-19 20:21:37 -0800506 bg.drawBackgroundStroke(canvas);
Adam Cohenf172b742016-03-30 19:28:34 -0700507 canvas.restore();
508 }
Adam Cohenefca0272016-02-24 19:19:06 -0800509 }
Adam Cohen69ce2e52011-07-03 19:25:21 -0700510 }
511
Jon Mirandaa0233f72017-06-22 18:34:45 -0700512 public void addFolderBackground(PreviewBackground bg) {
Adam Cohenefca0272016-02-24 19:19:06 -0800513 mFolderBackgrounds.add(bg);
514 }
Jon Mirandaa0233f72017-06-22 18:34:45 -0700515 public void removeFolderBackground(PreviewBackground bg) {
Adam Cohenefca0272016-02-24 19:19:06 -0800516 mFolderBackgrounds.remove(bg);
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700517 }
518
Adam Cohenc51934b2011-07-26 21:07:43 -0700519 public void setFolderLeaveBehindCell(int x, int y) {
Adam Cohenefca0272016-02-24 19:19:06 -0800520 View child = getChildAt(x, y);
Sunny Goyal368ae772017-05-24 13:19:15 -0700521 mFolderLeaveBehind.setup(mLauncher, null,
Adam Cohenefca0272016-02-24 19:19:06 -0800522 child.getMeasuredWidth(), child.getPaddingTop());
523
524 mFolderLeaveBehind.delegateCellX = x;
525 mFolderLeaveBehind.delegateCellY = y;
Adam Cohenc51934b2011-07-26 21:07:43 -0700526 invalidate();
527 }
528
529 public void clearFolderLeaveBehind() {
Adam Cohenefca0272016-02-24 19:19:06 -0800530 mFolderLeaveBehind.delegateCellX = -1;
531 mFolderLeaveBehind.delegateCellY = -1;
Adam Cohenc51934b2011-07-26 21:07:43 -0700532 invalidate();
533 }
534
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700535 @Override
Michael Jurkae6235dd2011-10-04 15:02:05 -0700536 public boolean shouldDelayChildPressedState() {
537 return false;
538 }
539
Adam Cohen1462de32012-07-24 22:34:36 -0700540 public void restoreInstanceState(SparseArray<Parcelable> states) {
Sunny Goyal33a152f2014-07-22 12:13:14 -0700541 try {
542 dispatchRestoreInstanceState(states);
543 } catch (IllegalArgumentException ex) {
Sunny Goyal3d706ad2017-03-06 16:56:39 -0800544 if (FeatureFlags.IS_DOGFOOD_BUILD) {
Sunny Goyal33a152f2014-07-22 12:13:14 -0700545 throw ex;
546 }
547 // Mismatched viewId / viewType preventing restore. Skip restore on production builds.
548 Log.e(TAG, "Ignoring an error while restoring a view instance state", ex);
549 }
Adam Cohen1462de32012-07-24 22:34:36 -0700550 }
551
Michael Jurkae6235dd2011-10-04 15:02:05 -0700552 @Override
Jeff Sharkey83f111d2009-04-20 21:03:13 -0700553 public void cancelLongPress() {
554 super.cancelLongPress();
555
556 // Cancel long press for all children
557 final int count = getChildCount();
558 for (int i = 0; i < count; i++) {
559 final View child = getChildAt(i);
560 child.cancelLongPress();
561 }
562 }
563
Michael Jurkadee05892010-07-27 10:01:56 -0700564 public void setOnInterceptTouchListener(View.OnTouchListener listener) {
565 mInterceptTouchListener = listener;
566 }
567
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800568 public int getCountX() {
Adam Cohend22015c2010-07-26 22:02:18 -0700569 return mCountX;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800570 }
571
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800572 public int getCountY() {
Adam Cohend22015c2010-07-26 22:02:18 -0700573 return mCountY;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800574 }
575
Sunny Goyalc13403c2016-11-18 23:44:48 -0800576 public boolean acceptsWidget() {
577 return mContainerType == WORKSPACE;
Sunny Goyale9b651e2015-04-24 11:44:51 -0700578 }
579
Andrew Flynn0dca1ec2012-02-29 13:33:22 -0800580 public boolean addViewToCellLayout(View child, int index, int childId, LayoutParams params,
Sunny Goyalf7a29e82015-04-24 15:20:43 -0700581 boolean markCells) {
Winson Chungaafa03c2010-06-11 17:34:16 -0700582 final LayoutParams lp = params;
583
Andrew Flynnde38e422012-05-08 11:22:15 -0700584 // Hotseat icons - remove text
Andrew Flynn0dca1ec2012-02-29 13:33:22 -0800585 if (child instanceof BubbleTextView) {
586 BubbleTextView bubbleChild = (BubbleTextView) child;
Jon Mirandaf1eae802017-10-04 11:23:33 -0700587 bubbleChild.setTextVisibility(mContainerType != HOTSEAT);
Andrew Flynn0dca1ec2012-02-29 13:33:22 -0800588 }
589
Sunny Goyalc13403c2016-11-18 23:44:48 -0800590 child.setScaleX(mChildScale);
591 child.setScaleY(mChildScale);
Adam Cohen307fe232012-08-16 17:55:58 -0700592
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800593 // Generate an id for each view, this assumes we have at most 256x256 cells
594 // per workspace screen
Adam Cohend22015c2010-07-26 22:02:18 -0700595 if (lp.cellX >= 0 && lp.cellX <= mCountX - 1 && lp.cellY >= 0 && lp.cellY <= mCountY - 1) {
Winson Chungaafa03c2010-06-11 17:34:16 -0700596 // If the horizontal or vertical span is set to -1, it is taken to
597 // mean that it spans the extent of the CellLayout
Adam Cohend22015c2010-07-26 22:02:18 -0700598 if (lp.cellHSpan < 0) lp.cellHSpan = mCountX;
599 if (lp.cellVSpan < 0) lp.cellVSpan = mCountY;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800600
Winson Chungaafa03c2010-06-11 17:34:16 -0700601 child.setId(childId);
Tony Wickhama0628cc2015-10-14 15:23:04 -0700602 if (LOGD) {
603 Log.d(TAG, "Adding view to ShortcutsAndWidgetsContainer: " + child);
604 }
Sunny Goyalf7a29e82015-04-24 15:20:43 -0700605 mShortcutsAndWidgets.addView(child, index, lp);
Michael Jurkadee05892010-07-27 10:01:56 -0700606
Michael Jurkaf3ca3ab2010-10-20 17:08:24 -0700607 if (markCells) markCellsAsOccupiedForView(child);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700608
Winson Chungaafa03c2010-06-11 17:34:16 -0700609 return true;
610 }
611 return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800612 }
Michael Jurka3e7c7632010-10-02 16:01:03 -0700613
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800614 @Override
Michael Jurka0280c3b2010-09-17 15:00:07 -0700615 public void removeAllViews() {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -0700616 mOccupied.clear();
Michael Jurkaa52570f2012-03-20 03:18:20 -0700617 mShortcutsAndWidgets.removeAllViews();
Michael Jurka0280c3b2010-09-17 15:00:07 -0700618 }
619
620 @Override
621 public void removeAllViewsInLayout() {
Michael Jurkaa52570f2012-03-20 03:18:20 -0700622 if (mShortcutsAndWidgets.getChildCount() > 0) {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -0700623 mOccupied.clear();
Michael Jurkaa52570f2012-03-20 03:18:20 -0700624 mShortcutsAndWidgets.removeAllViewsInLayout();
Michael Jurka7cfc2822011-08-02 20:19:24 -0700625 }
Michael Jurka0280c3b2010-09-17 15:00:07 -0700626 }
627
628 @Override
629 public void removeView(View view) {
630 markCellsAsUnoccupiedForView(view);
Michael Jurkaa52570f2012-03-20 03:18:20 -0700631 mShortcutsAndWidgets.removeView(view);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700632 }
633
634 @Override
635 public void removeViewAt(int index) {
Michael Jurkaa52570f2012-03-20 03:18:20 -0700636 markCellsAsUnoccupiedForView(mShortcutsAndWidgets.getChildAt(index));
637 mShortcutsAndWidgets.removeViewAt(index);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700638 }
639
640 @Override
641 public void removeViewInLayout(View view) {
642 markCellsAsUnoccupiedForView(view);
Michael Jurkaa52570f2012-03-20 03:18:20 -0700643 mShortcutsAndWidgets.removeViewInLayout(view);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700644 }
645
646 @Override
647 public void removeViews(int start, int count) {
648 for (int i = start; i < start + count; i++) {
Michael Jurkaa52570f2012-03-20 03:18:20 -0700649 markCellsAsUnoccupiedForView(mShortcutsAndWidgets.getChildAt(i));
Michael Jurka0280c3b2010-09-17 15:00:07 -0700650 }
Michael Jurkaa52570f2012-03-20 03:18:20 -0700651 mShortcutsAndWidgets.removeViews(start, count);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700652 }
653
654 @Override
655 public void removeViewsInLayout(int start, int count) {
656 for (int i = start; i < start + count; i++) {
Michael Jurkaa52570f2012-03-20 03:18:20 -0700657 markCellsAsUnoccupiedForView(mShortcutsAndWidgets.getChildAt(i));
Michael Jurka0280c3b2010-09-17 15:00:07 -0700658 }
Michael Jurkaa52570f2012-03-20 03:18:20 -0700659 mShortcutsAndWidgets.removeViewsInLayout(start, count);
Michael Jurkaabded662011-03-04 12:06:57 -0800660 }
661
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700662 /**
Winson Chungaafa03c2010-06-11 17:34:16 -0700663 * Given a point, return the cell that strictly encloses that point
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800664 * @param x X coordinate of the point
665 * @param y Y coordinate of the point
666 * @param result Array of 2 ints to hold the x and y coordinate of the cell
667 */
Sunny Goyale9b651e2015-04-24 11:44:51 -0700668 public void pointToCellExact(int x, int y, int[] result) {
Winson Chung4b825dcd2011-06-19 12:41:22 -0700669 final int hStartPadding = getPaddingLeft();
670 final int vStartPadding = getPaddingTop();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800671
Sunny Goyalaa8a8712016-11-20 15:26:01 +0530672 result[0] = (x - hStartPadding) / mCellWidth;
673 result[1] = (y - vStartPadding) / mCellHeight;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800674
Adam Cohend22015c2010-07-26 22:02:18 -0700675 final int xAxis = mCountX;
676 final int yAxis = mCountY;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800677
678 if (result[0] < 0) result[0] = 0;
679 if (result[0] >= xAxis) result[0] = xAxis - 1;
680 if (result[1] < 0) result[1] = 0;
681 if (result[1] >= yAxis) result[1] = yAxis - 1;
682 }
Winson Chungaafa03c2010-06-11 17:34:16 -0700683
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800684 /**
685 * Given a point, return the cell that most closely encloses that point
686 * @param x X coordinate of the point
687 * @param y Y coordinate of the point
688 * @param result Array of 2 ints to hold the x and y coordinate of the cell
689 */
690 void pointToCellRounded(int x, int y, int[] result) {
691 pointToCellExact(x + (mCellWidth / 2), y + (mCellHeight / 2), result);
692 }
693
694 /**
695 * Given a cell coordinate, return the point that represents the upper left corner of that cell
Winson Chungaafa03c2010-06-11 17:34:16 -0700696 *
697 * @param cellX X coordinate of the cell
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800698 * @param cellY Y coordinate of the cell
Winson Chungaafa03c2010-06-11 17:34:16 -0700699 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800700 * @param result Array of 2 ints to hold the x and y coordinate of the point
701 */
702 void cellToPoint(int cellX, int cellY, int[] result) {
Winson Chung4b825dcd2011-06-19 12:41:22 -0700703 final int hStartPadding = getPaddingLeft();
704 final int vStartPadding = getPaddingTop();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800705
Sunny Goyalaa8a8712016-11-20 15:26:01 +0530706 result[0] = hStartPadding + cellX * mCellWidth;
707 result[1] = vStartPadding + cellY * mCellHeight;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800708 }
709
Adam Cohene3e27a82011-04-15 12:07:39 -0700710 /**
Adam Cohen482ed822012-03-02 14:15:13 -0800711 * Given a cell coordinate, return the point that represents the center of the cell
Adam Cohene3e27a82011-04-15 12:07:39 -0700712 *
713 * @param cellX X coordinate of the cell
714 * @param cellY Y coordinate of the cell
715 *
716 * @param result Array of 2 ints to hold the x and y coordinate of the point
717 */
718 void cellToCenterPoint(int cellX, int cellY, int[] result) {
Adam Cohen47a876d2012-03-19 13:21:41 -0700719 regionToCenterPoint(cellX, cellY, 1, 1, result);
720 }
721
722 /**
723 * Given a cell coordinate and span return the point that represents the center of the regio
724 *
725 * @param cellX X coordinate of the cell
726 * @param cellY Y coordinate of the cell
727 *
728 * @param result Array of 2 ints to hold the x and y coordinate of the point
729 */
730 void regionToCenterPoint(int cellX, int cellY, int spanX, int spanY, int[] result) {
Winson Chung4b825dcd2011-06-19 12:41:22 -0700731 final int hStartPadding = getPaddingLeft();
732 final int vStartPadding = getPaddingTop();
Sunny Goyalaa8a8712016-11-20 15:26:01 +0530733 result[0] = hStartPadding + cellX * mCellWidth + (spanX * mCellWidth) / 2;
734 result[1] = vStartPadding + cellY * mCellHeight + (spanY * mCellHeight) / 2;
Adam Cohene3e27a82011-04-15 12:07:39 -0700735 }
736
Adam Cohen19f37922012-03-21 11:59:11 -0700737 /**
738 * Given a cell coordinate and span fills out a corresponding pixel rect
739 *
740 * @param cellX X coordinate of the cell
741 * @param cellY Y coordinate of the cell
742 * @param result Rect in which to write the result
743 */
744 void regionToRect(int cellX, int cellY, int spanX, int spanY, Rect result) {
745 final int hStartPadding = getPaddingLeft();
746 final int vStartPadding = getPaddingTop();
Sunny Goyalaa8a8712016-11-20 15:26:01 +0530747 final int left = hStartPadding + cellX * mCellWidth;
748 final int top = vStartPadding + cellY * mCellHeight;
749 result.set(left, top, left + (spanX * mCellWidth), top + (spanY * mCellHeight));
Adam Cohen19f37922012-03-21 11:59:11 -0700750 }
751
Adam Cohen482ed822012-03-02 14:15:13 -0800752 public float getDistanceFromCell(float x, float y, int[] cell) {
753 cellToCenterPoint(cell[0], cell[1], mTmpPoint);
Sunny Goyalf7a29e82015-04-24 15:20:43 -0700754 return (float) Math.hypot(x - mTmpPoint[0], y - mTmpPoint[1]);
Adam Cohen482ed822012-03-02 14:15:13 -0800755 }
756
Adam Cohenf9c184a2016-01-15 16:47:43 -0800757 public int getCellWidth() {
Romain Guy84f296c2009-11-04 15:00:44 -0800758 return mCellWidth;
759 }
760
Sunny Goyal0b754e52017-08-07 07:42:45 -0700761 public int getCellHeight() {
Romain Guy84f296c2009-11-04 15:00:44 -0800762 return mCellHeight;
763 }
764
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700765 public void setFixedSize(int width, int height) {
766 mFixedWidth = width;
767 mFixedHeight = height;
768 }
769
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800770 @Override
771 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800772 int widthSpecMode = MeasureSpec.getMode(widthMeasureSpec);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800773 int heightSpecMode = MeasureSpec.getMode(heightMeasureSpec);
Winson Chung5f8afe62013-08-12 16:19:28 -0700774 int widthSize = MeasureSpec.getSize(widthMeasureSpec);
775 int heightSize = MeasureSpec.getSize(heightMeasureSpec);
Winson Chung2d75f122013-09-23 16:53:31 -0700776 int childWidthSize = widthSize - (getPaddingLeft() + getPaddingRight());
777 int childHeightSize = heightSize - (getPaddingTop() + getPaddingBottom());
Winson Chung11a1a532013-09-13 11:14:45 -0700778 if (mFixedCellWidth < 0 || mFixedCellHeight < 0) {
Sunny Goyalc6205602015-05-21 20:46:33 -0700779 int cw = DeviceProfile.calculateCellWidth(childWidthSize, mCountX);
780 int ch = DeviceProfile.calculateCellHeight(childHeightSize, mCountY);
Winson Chung11a1a532013-09-13 11:14:45 -0700781 if (cw != mCellWidth || ch != mCellHeight) {
782 mCellWidth = cw;
783 mCellHeight = ch;
Sunny Goyalaa8a8712016-11-20 15:26:01 +0530784 mShortcutsAndWidgets.setCellDimensions(mCellWidth, mCellHeight, mCountX, mCountY);
Winson Chung11a1a532013-09-13 11:14:45 -0700785 }
Winson Chung5f8afe62013-08-12 16:19:28 -0700786 }
Winson Chungaafa03c2010-06-11 17:34:16 -0700787
Winson Chung2d75f122013-09-23 16:53:31 -0700788 int newWidth = childWidthSize;
789 int newHeight = childHeightSize;
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700790 if (mFixedWidth > 0 && mFixedHeight > 0) {
791 newWidth = mFixedWidth;
792 newHeight = mFixedHeight;
793 } else if (widthSpecMode == MeasureSpec.UNSPECIFIED || heightSpecMode == MeasureSpec.UNSPECIFIED) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800794 throw new RuntimeException("CellLayout cannot have UNSPECIFIED dimensions");
795 }
796
Sunny Goyal4fe5a372015-05-14 19:55:10 -0700797 // Make the feedback view large enough to hold the blur bitmap.
798 mTouchFeedbackView.measure(
799 MeasureSpec.makeMeasureSpec(mCellWidth + mTouchFeedbackView.getExtraSize(),
800 MeasureSpec.EXACTLY),
801 MeasureSpec.makeMeasureSpec(mCellHeight + mTouchFeedbackView.getExtraSize(),
802 MeasureSpec.EXACTLY));
803
804 mShortcutsAndWidgets.measure(
805 MeasureSpec.makeMeasureSpec(newWidth, MeasureSpec.EXACTLY),
806 MeasureSpec.makeMeasureSpec(newHeight, MeasureSpec.EXACTLY));
807
808 int maxWidth = mShortcutsAndWidgets.getMeasuredWidth();
809 int maxHeight = mShortcutsAndWidgets.getMeasuredHeight();
Winson Chung2d75f122013-09-23 16:53:31 -0700810 if (mFixedWidth > 0 && mFixedHeight > 0) {
811 setMeasuredDimension(maxWidth, maxHeight);
812 } else {
813 setMeasuredDimension(widthSize, heightSize);
814 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800815 }
816
817 @Override
Michael Jurka28750fb2010-09-24 17:43:49 -0700818 protected void onLayout(boolean changed, int l, int t, int r, int b) {
Tony Wickham26b01422015-11-10 14:44:32 -0800819 int left = getPaddingLeft();
Sunny Goyal7ce471b2017-08-02 03:37:39 -0700820 left += (int) Math.ceil(getUnusedHorizontalSpace() / 2f);
Sunny Goyal7c786f72016-06-01 14:08:21 -0700821 int right = r - l - getPaddingRight();
Sunny Goyal7ce471b2017-08-02 03:37:39 -0700822 right -= (int) Math.ceil(getUnusedHorizontalSpace() / 2f);
Sunny Goyal7c786f72016-06-01 14:08:21 -0700823
Winson Chung38848ca2013-10-08 12:03:44 -0700824 int top = getPaddingTop();
Sunny Goyal7c786f72016-06-01 14:08:21 -0700825 int bottom = b - t - getPaddingBottom();
Sunny Goyal4fe5a372015-05-14 19:55:10 -0700826
827 mTouchFeedbackView.layout(left, top,
828 left + mTouchFeedbackView.getMeasuredWidth(),
829 top + mTouchFeedbackView.getMeasuredHeight());
Sunny Goyal7c786f72016-06-01 14:08:21 -0700830 mShortcutsAndWidgets.layout(left, top, right, bottom);
831
832 // Expand the background drawing bounds by the padding baked into the background drawable
833 mBackground.getPadding(mTempRect);
834 mBackground.setBounds(
Jon Miranda28032002017-07-13 16:18:56 -0700835 left - mTempRect.left - getPaddingLeft(),
836 top - mTempRect.top - getPaddingTop(),
837 right + mTempRect.right + getPaddingRight(),
838 bottom + mTempRect.bottom + getPaddingBottom());
Sunny Goyal7c786f72016-06-01 14:08:21 -0700839 }
840
Tony Wickhama501d492015-11-03 18:05:01 -0800841 /**
842 * Returns the amount of space left over after subtracting padding and cells. This space will be
843 * very small, a few pixels at most, and is a result of rounding down when calculating the cell
844 * width in {@link DeviceProfile#calculateCellWidth(int, int)}.
845 */
846 public int getUnusedHorizontalSpace() {
847 return getMeasuredWidth() - getPaddingLeft() - getPaddingRight() - (mCountX * mCellWidth);
848 }
849
Michael Jurka5f1c5092010-09-03 14:15:02 -0700850 public float getBackgroundAlpha() {
851 return mBackgroundAlpha;
Michael Jurkadee05892010-07-27 10:01:56 -0700852 }
853
Michael Jurka5f1c5092010-09-03 14:15:02 -0700854 public void setBackgroundAlpha(float alpha) {
Michael Jurkaafaa0502011-12-13 18:22:50 -0800855 if (mBackgroundAlpha != alpha) {
856 mBackgroundAlpha = alpha;
Sunny Goyal2805e632015-05-20 15:35:32 -0700857 mBackground.setAlpha((int) (mBackgroundAlpha * 255));
Michael Jurkaafaa0502011-12-13 18:22:50 -0800858 }
Michael Jurkadee05892010-07-27 10:01:56 -0700859 }
860
Sunny Goyal2805e632015-05-20 15:35:32 -0700861 @Override
862 protected boolean verifyDrawable(Drawable who) {
Sunny Goyal7ce471b2017-08-02 03:37:39 -0700863 return super.verifyDrawable(who) || (who == mBackground);
Sunny Goyal2805e632015-05-20 15:35:32 -0700864 }
865
Michael Jurkaa52570f2012-03-20 03:18:20 -0700866 public void setShortcutAndWidgetAlpha(float alpha) {
Sunny Goyal02b50812014-09-10 15:44:42 -0700867 mShortcutsAndWidgets.setAlpha(alpha);
Michael Jurkadee05892010-07-27 10:01:56 -0700868 }
869
Michael Jurkaa52570f2012-03-20 03:18:20 -0700870 public ShortcutAndWidgetContainer getShortcutsAndWidgets() {
Sunny Goyaldcbcc862014-08-12 15:58:36 -0700871 return mShortcutsAndWidgets;
Michael Jurkaa52570f2012-03-20 03:18:20 -0700872 }
873
Patrick Dubroy440c3602010-07-13 17:50:32 -0700874 public View getChildAt(int x, int y) {
Michael Jurkaa52570f2012-03-20 03:18:20 -0700875 return mShortcutsAndWidgets.getChildAt(x, y);
Patrick Dubroy440c3602010-07-13 17:50:32 -0700876 }
877
Adam Cohen76fc0852011-06-17 13:26:23 -0700878 public boolean animateChildToPosition(final View child, int cellX, int cellY, int duration,
Adam Cohen482ed822012-03-02 14:15:13 -0800879 int delay, boolean permanent, boolean adjustOccupied) {
Michael Jurkaa52570f2012-03-20 03:18:20 -0700880 ShortcutAndWidgetContainer clc = getShortcutsAndWidgets();
Adam Cohen482ed822012-03-02 14:15:13 -0800881
Adam Cohen19f37922012-03-21 11:59:11 -0700882 if (clc.indexOfChild(child) != -1) {
Adam Cohenbfbfd262011-06-13 16:55:12 -0700883 final LayoutParams lp = (LayoutParams) child.getLayoutParams();
884 final ItemInfo info = (ItemInfo) child.getTag();
885
886 // We cancel any existing animations
887 if (mReorderAnimators.containsKey(lp)) {
888 mReorderAnimators.get(lp).cancel();
889 mReorderAnimators.remove(lp);
890 }
891
Adam Cohen482ed822012-03-02 14:15:13 -0800892 final int oldX = lp.x;
893 final int oldY = lp.y;
894 if (adjustOccupied) {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -0700895 GridOccupancy occupied = permanent ? mOccupied : mTmpOccupied;
896 occupied.markCells(lp.cellX, lp.cellY, lp.cellHSpan, lp.cellVSpan, false);
897 occupied.markCells(cellX, cellY, lp.cellHSpan, lp.cellVSpan, true);
Adam Cohen482ed822012-03-02 14:15:13 -0800898 }
Adam Cohenbfbfd262011-06-13 16:55:12 -0700899 lp.isLockedToGrid = true;
Adam Cohen482ed822012-03-02 14:15:13 -0800900 if (permanent) {
901 lp.cellX = info.cellX = cellX;
902 lp.cellY = info.cellY = cellY;
903 } else {
904 lp.tmpCellX = cellX;
905 lp.tmpCellY = cellY;
906 }
Jon Mirandae96798e2016-12-07 12:10:44 -0800907 clc.setupLp(child);
Adam Cohenbfbfd262011-06-13 16:55:12 -0700908 lp.isLockedToGrid = false;
Adam Cohen482ed822012-03-02 14:15:13 -0800909 final int newX = lp.x;
910 final int newY = lp.y;
Adam Cohenbfbfd262011-06-13 16:55:12 -0700911
Adam Cohen76fc0852011-06-17 13:26:23 -0700912 lp.x = oldX;
913 lp.y = oldY;
Adam Cohen76fc0852011-06-17 13:26:23 -0700914
Adam Cohen482ed822012-03-02 14:15:13 -0800915 // Exit early if we're not actually moving the view
916 if (oldX == newX && oldY == newY) {
917 lp.isLockedToGrid = true;
918 return true;
919 }
920
Jon Mirandacda3bfb2017-02-06 15:54:41 -0800921 ValueAnimator va = LauncherAnimUtils.ofFloat(0f, 1f);
Adam Cohen482ed822012-03-02 14:15:13 -0800922 va.setDuration(duration);
923 mReorderAnimators.put(lp, va);
924
925 va.addUpdateListener(new AnimatorUpdateListener() {
926 @Override
Adam Cohenbfbfd262011-06-13 16:55:12 -0700927 public void onAnimationUpdate(ValueAnimator animation) {
Jon Mirandae96798e2016-12-07 12:10:44 -0800928 float r = (Float) animation.getAnimatedValue();
Adam Cohen19f37922012-03-21 11:59:11 -0700929 lp.x = (int) ((1 - r) * oldX + r * newX);
930 lp.y = (int) ((1 - r) * oldY + r * newY);
Adam Cohen6b8a02d2012-03-22 15:13:40 -0700931 child.requestLayout();
Adam Cohenbfbfd262011-06-13 16:55:12 -0700932 }
933 });
Adam Cohen482ed822012-03-02 14:15:13 -0800934 va.addListener(new AnimatorListenerAdapter() {
Adam Cohenbfbfd262011-06-13 16:55:12 -0700935 boolean cancelled = false;
936 public void onAnimationEnd(Animator animation) {
937 // If the animation was cancelled, it means that another animation
938 // has interrupted this one, and we don't want to lock the item into
939 // place just yet.
940 if (!cancelled) {
941 lp.isLockedToGrid = true;
Adam Cohen482ed822012-03-02 14:15:13 -0800942 child.requestLayout();
Adam Cohenbfbfd262011-06-13 16:55:12 -0700943 }
944 if (mReorderAnimators.containsKey(lp)) {
945 mReorderAnimators.remove(lp);
946 }
947 }
948 public void onAnimationCancel(Animator animation) {
949 cancelled = true;
950 }
951 });
Adam Cohen482ed822012-03-02 14:15:13 -0800952 va.setStartDelay(delay);
953 va.start();
Adam Cohenbfbfd262011-06-13 16:55:12 -0700954 return true;
955 }
956 return false;
957 }
958
Sunny Goyal06e21a22016-08-11 16:02:02 -0700959 void visualizeDropLocation(View v, DragPreviewProvider outlineProvider, int cellX, int cellY,
960 int spanX, int spanY, boolean resize, DropTarget.DragObject dragObject) {
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -0700961 final int oldDragCellX = mDragCell[0];
962 final int oldDragCellY = mDragCell[1];
Adam Cohen482ed822012-03-02 14:15:13 -0800963
Hyunyoung Song0de01172016-10-05 16:27:48 -0700964 if (outlineProvider == null || outlineProvider.generatedDragOutline == null) {
Adam Cohen2801caf2011-05-13 20:57:39 -0700965 return;
966 }
967
Hyunyoung Song0de01172016-10-05 16:27:48 -0700968 Bitmap dragOutline = outlineProvider.generatedDragOutline;
Adam Cohen482ed822012-03-02 14:15:13 -0800969 if (cellX != oldDragCellX || cellY != oldDragCellY) {
Sunny Goyale78e3d72015-09-24 11:23:31 -0700970 Point dragOffset = dragObject.dragView.getDragVisualizeOffset();
971 Rect dragRegion = dragObject.dragView.getDragRegion();
972
Adam Cohen482ed822012-03-02 14:15:13 -0800973 mDragCell[0] = cellX;
974 mDragCell[1] = cellY;
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700975
Joe Onorato4be866d2010-10-10 11:26:02 -0700976 final int oldIndex = mDragOutlineCurrent;
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -0700977 mDragOutlineAnims[oldIndex].animateOut();
978 mDragOutlineCurrent = (oldIndex + 1) % mDragOutlines.length;
Adam Cohend41fbf52012-02-16 23:53:59 -0800979 Rect r = mDragOutlines[mDragOutlineCurrent];
Sunny Goyal106bf642015-07-16 12:18:06 -0700980
Adam Cohend41fbf52012-02-16 23:53:59 -0800981 if (resize) {
Adam Cohen482ed822012-03-02 14:15:13 -0800982 cellToRect(cellX, cellY, spanX, spanY, r);
Jon Mirandae96798e2016-12-07 12:10:44 -0800983 if (v instanceof LauncherAppWidgetHostView) {
984 DeviceProfile profile = mLauncher.getDeviceProfile();
Jon Miranda6f6a06a2016-12-15 11:24:18 -0800985 Utilities.shrinkRect(r, profile.appWidgetScale.x, profile.appWidgetScale.y);
Jon Mirandae96798e2016-12-07 12:10:44 -0800986 }
Sunny Goyal106bf642015-07-16 12:18:06 -0700987 } else {
988 // Find the top left corner of the rect the object will occupy
989 final int[] topLeft = mTmpPoint;
990 cellToPoint(cellX, cellY, topLeft);
991
992 int left = topLeft[0];
993 int top = topLeft[1];
994
995 if (v != null && dragOffset == null) {
996 // When drawing the drag outline, it did not account for margin offsets
997 // added by the view's parent.
998 MarginLayoutParams lp = (MarginLayoutParams) v.getLayoutParams();
999 left += lp.leftMargin;
1000 top += lp.topMargin;
1001
1002 // Offsets due to the size difference between the View and the dragOutline.
1003 // There is a size difference to account for the outer blur, which may lie
1004 // outside the bounds of the view.
Jon Mirandaf7ff3fe2016-12-05 12:04:44 -08001005 top += ((mCellHeight * spanY) - dragOutline.getHeight()) / 2;
Sunny Goyal106bf642015-07-16 12:18:06 -07001006 // We center about the x axis
Sunny Goyalaa8a8712016-11-20 15:26:01 +05301007 left += ((mCellWidth * spanX) - dragOutline.getWidth()) / 2;
Sunny Goyal106bf642015-07-16 12:18:06 -07001008 } else {
1009 if (dragOffset != null && dragRegion != null) {
1010 // Center the drag region *horizontally* in the cell and apply a drag
1011 // outline offset
Sunny Goyalaa8a8712016-11-20 15:26:01 +05301012 left += dragOffset.x + ((mCellWidth * spanX) - dragRegion.width()) / 2;
Sunny Goyal106bf642015-07-16 12:18:06 -07001013 int cHeight = getShortcutsAndWidgets().getCellContentHeight();
1014 int cellPaddingY = (int) Math.max(0, ((mCellHeight - cHeight) / 2f));
1015 top += dragOffset.y + cellPaddingY;
1016 } else {
1017 // Center the drag outline in the cell
Sunny Goyalaa8a8712016-11-20 15:26:01 +05301018 left += ((mCellWidth * spanX) - dragOutline.getWidth()) / 2;
1019 top += ((mCellHeight * spanY) - dragOutline.getHeight()) / 2;
Sunny Goyal106bf642015-07-16 12:18:06 -07001020 }
1021 }
1022 r.set(left, top, left + dragOutline.getWidth(), top + dragOutline.getHeight());
Adam Cohend41fbf52012-02-16 23:53:59 -08001023 }
Winson Chung150fbab2010-09-29 17:14:26 -07001024
Jon Miranda6f6a06a2016-12-15 11:24:18 -08001025 Utilities.scaleRectAboutCenter(r, mChildScale);
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -07001026 mDragOutlineAnims[mDragOutlineCurrent].setTag(dragOutline);
1027 mDragOutlineAnims[mDragOutlineCurrent].animateIn();
Sunny Goyale78e3d72015-09-24 11:23:31 -07001028
1029 if (dragObject.stateAnnouncer != null) {
Sunny Goyalc13403c2016-11-18 23:44:48 -08001030 dragObject.stateAnnouncer.announce(getItemMoveDescription(cellX, cellY));
Sunny Goyale78e3d72015-09-24 11:23:31 -07001031 }
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001032 }
1033 }
1034
Sunny Goyalc13403c2016-11-18 23:44:48 -08001035 public String getItemMoveDescription(int cellX, int cellY) {
1036 if (mContainerType == HOTSEAT) {
1037 return getContext().getString(R.string.move_to_hotseat_position,
1038 Math.max(cellX, cellY) + 1);
1039 } else {
1040 return getContext().getString(R.string.move_to_empty_cell,
1041 cellY + 1, cellX + 1);
1042 }
1043 }
1044
Adam Cohene0310962011-04-18 16:15:31 -07001045 public void clearDragOutlines() {
1046 final int oldIndex = mDragOutlineCurrent;
1047 mDragOutlineAnims[oldIndex].animateOut();
Adam Cohend41fbf52012-02-16 23:53:59 -08001048 mDragCell[0] = mDragCell[1] = -1;
Adam Cohene0310962011-04-18 16:15:31 -07001049 }
1050
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001051 /**
Jeff Sharkey70864282009-04-07 21:08:40 -07001052 * Find a vacant area that will fit the given bounds nearest the requested
1053 * cell location. Uses Euclidean distance to score multiple vacant areas.
Winson Chungaafa03c2010-06-11 17:34:16 -07001054 *
Romain Guy51afc022009-05-04 18:03:43 -07001055 * @param pixelX The X location at which you want to search for a vacant area.
1056 * @param pixelY The Y location at which you want to search for a vacant area.
Adam Cohend41fbf52012-02-16 23:53:59 -08001057 * @param minSpanX The minimum horizontal span required
1058 * @param minSpanY The minimum vertical span required
1059 * @param spanX Horizontal span of the object.
1060 * @param spanY Vertical span of the object.
1061 * @param result Array in which to place the result, or null (in which case a new array will
1062 * be allocated)
1063 * @return The X, Y cell of a vacant area that can contain this object,
1064 * nearest the requested location.
1065 */
1066 int[] findNearestVacantArea(int pixelX, int pixelY, int minSpanX, int minSpanY, int spanX,
1067 int spanY, int[] result, int[] resultSpan) {
Sunny Goyalf7a29e82015-04-24 15:20:43 -07001068 return findNearestArea(pixelX, pixelY, minSpanX, minSpanY, spanX, spanY, true,
Adam Cohend41fbf52012-02-16 23:53:59 -08001069 result, resultSpan);
1070 }
1071
Rajeev Kumar9962dbe2017-06-12 12:16:20 -07001072 private final Stack<Rect> mTempRectStack = new Stack<>();
Adam Cohend41fbf52012-02-16 23:53:59 -08001073 private void lazyInitTempRectStack() {
1074 if (mTempRectStack.isEmpty()) {
1075 for (int i = 0; i < mCountX * mCountY; i++) {
1076 mTempRectStack.push(new Rect());
1077 }
1078 }
1079 }
Adam Cohen482ed822012-03-02 14:15:13 -08001080
Adam Cohend41fbf52012-02-16 23:53:59 -08001081 private void recycleTempRects(Stack<Rect> used) {
1082 while (!used.isEmpty()) {
1083 mTempRectStack.push(used.pop());
1084 }
1085 }
1086
1087 /**
1088 * Find a vacant area that will fit the given bounds nearest the requested
1089 * cell location. Uses Euclidean distance to score multiple vacant areas.
1090 *
1091 * @param pixelX The X location at which you want to search for a vacant area.
1092 * @param pixelY The Y location at which you want to search for a vacant area.
1093 * @param minSpanX The minimum horizontal span required
1094 * @param minSpanY The minimum vertical span required
1095 * @param spanX Horizontal span of the object.
1096 * @param spanY Vertical span of the object.
1097 * @param ignoreOccupied If true, the result can be an occupied cell
1098 * @param result Array in which to place the result, or null (in which case a new array will
1099 * be allocated)
1100 * @return The X, Y cell of a vacant area that can contain this object,
1101 * nearest the requested location.
1102 */
Sunny Goyalf7a29e82015-04-24 15:20:43 -07001103 private int[] findNearestArea(int pixelX, int pixelY, int minSpanX, int minSpanY, int spanX,
1104 int spanY, boolean ignoreOccupied, int[] result, int[] resultSpan) {
Adam Cohend41fbf52012-02-16 23:53:59 -08001105 lazyInitTempRectStack();
Michael Jurkac6ee42e2010-09-30 12:04:50 -07001106
Adam Cohene3e27a82011-04-15 12:07:39 -07001107 // For items with a spanX / spanY > 1, the passed in point (pixelX, pixelY) corresponds
1108 // to the center of the item, but we are searching based on the top-left cell, so
1109 // we translate the point over to correspond to the top-left.
Sunny Goyalaa8a8712016-11-20 15:26:01 +05301110 pixelX -= mCellWidth * (spanX - 1) / 2f;
1111 pixelY -= mCellHeight * (spanY - 1) / 2f;
Adam Cohene3e27a82011-04-15 12:07:39 -07001112
Jeff Sharkey70864282009-04-07 21:08:40 -07001113 // Keep track of best-scoring drop area
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001114 final int[] bestXY = result != null ? result : new int[2];
Jeff Sharkey70864282009-04-07 21:08:40 -07001115 double bestDistance = Double.MAX_VALUE;
Adam Cohend41fbf52012-02-16 23:53:59 -08001116 final Rect bestRect = new Rect(-1, -1, -1, -1);
Rajeev Kumar9962dbe2017-06-12 12:16:20 -07001117 final Stack<Rect> validRegions = new Stack<>();
Winson Chungaafa03c2010-06-11 17:34:16 -07001118
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001119 final int countX = mCountX;
1120 final int countY = mCountY;
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001121
Adam Cohend41fbf52012-02-16 23:53:59 -08001122 if (minSpanX <= 0 || minSpanY <= 0 || spanX <= 0 || spanY <= 0 ||
1123 spanX < minSpanX || spanY < minSpanY) {
1124 return bestXY;
1125 }
1126
1127 for (int y = 0; y < countY - (minSpanY - 1); y++) {
Michael Jurkac28de512010-08-13 11:27:44 -07001128 inner:
Adam Cohend41fbf52012-02-16 23:53:59 -08001129 for (int x = 0; x < countX - (minSpanX - 1); x++) {
1130 int ySize = -1;
1131 int xSize = -1;
Adam Cohendf035382011-04-11 17:22:04 -07001132 if (ignoreOccupied) {
Adam Cohend41fbf52012-02-16 23:53:59 -08001133 // First, let's see if this thing fits anywhere
1134 for (int i = 0; i < minSpanX; i++) {
1135 for (int j = 0; j < minSpanY; j++) {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001136 if (mOccupied.cells[x + i][y + j]) {
Adam Cohendf035382011-04-11 17:22:04 -07001137 continue inner;
1138 }
Michael Jurkac28de512010-08-13 11:27:44 -07001139 }
1140 }
Adam Cohend41fbf52012-02-16 23:53:59 -08001141 xSize = minSpanX;
1142 ySize = minSpanY;
1143
1144 // We know that the item will fit at _some_ acceptable size, now let's see
1145 // how big we can make it. We'll alternate between incrementing x and y spans
1146 // until we hit a limit.
1147 boolean incX = true;
1148 boolean hitMaxX = xSize >= spanX;
1149 boolean hitMaxY = ySize >= spanY;
1150 while (!(hitMaxX && hitMaxY)) {
1151 if (incX && !hitMaxX) {
1152 for (int j = 0; j < ySize; j++) {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001153 if (x + xSize > countX -1 || mOccupied.cells[x + xSize][y + j]) {
Adam Cohend41fbf52012-02-16 23:53:59 -08001154 // We can't move out horizontally
1155 hitMaxX = true;
1156 }
1157 }
1158 if (!hitMaxX) {
1159 xSize++;
1160 }
1161 } else if (!hitMaxY) {
1162 for (int i = 0; i < xSize; i++) {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001163 if (y + ySize > countY - 1 || mOccupied.cells[x + i][y + ySize]) {
Adam Cohend41fbf52012-02-16 23:53:59 -08001164 // We can't move out vertically
1165 hitMaxY = true;
1166 }
1167 }
1168 if (!hitMaxY) {
1169 ySize++;
1170 }
1171 }
1172 hitMaxX |= xSize >= spanX;
1173 hitMaxY |= ySize >= spanY;
1174 incX = !incX;
1175 }
1176 incX = true;
1177 hitMaxX = xSize >= spanX;
1178 hitMaxY = ySize >= spanY;
Michael Jurkac28de512010-08-13 11:27:44 -07001179 }
Sunny Goyal2805e632015-05-20 15:35:32 -07001180 final int[] cellXY = mTmpPoint;
Adam Cohene3e27a82011-04-15 12:07:39 -07001181 cellToCenterPoint(x, y, cellXY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001182
Adam Cohend41fbf52012-02-16 23:53:59 -08001183 // We verify that the current rect is not a sub-rect of any of our previous
1184 // candidates. In this case, the current rect is disqualified in favour of the
1185 // containing rect.
1186 Rect currentRect = mTempRectStack.pop();
1187 currentRect.set(x, y, x + xSize, y + ySize);
1188 boolean contained = false;
1189 for (Rect r : validRegions) {
1190 if (r.contains(currentRect)) {
1191 contained = true;
1192 break;
1193 }
1194 }
1195 validRegions.push(currentRect);
Sunny Goyalf7a29e82015-04-24 15:20:43 -07001196 double distance = Math.hypot(cellXY[0] - pixelX, cellXY[1] - pixelY);
Adam Cohen482ed822012-03-02 14:15:13 -08001197
Adam Cohend41fbf52012-02-16 23:53:59 -08001198 if ((distance <= bestDistance && !contained) ||
1199 currentRect.contains(bestRect)) {
Michael Jurkac28de512010-08-13 11:27:44 -07001200 bestDistance = distance;
1201 bestXY[0] = x;
1202 bestXY[1] = y;
Adam Cohend41fbf52012-02-16 23:53:59 -08001203 if (resultSpan != null) {
1204 resultSpan[0] = xSize;
1205 resultSpan[1] = ySize;
1206 }
1207 bestRect.set(currentRect);
Michael Jurkac28de512010-08-13 11:27:44 -07001208 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001209 }
1210 }
1211
Adam Cohenc0dcf592011-06-01 15:30:43 -07001212 // Return -1, -1 if no suitable location found
1213 if (bestDistance == Double.MAX_VALUE) {
1214 bestXY[0] = -1;
1215 bestXY[1] = -1;
Jeff Sharkey70864282009-04-07 21:08:40 -07001216 }
Adam Cohend41fbf52012-02-16 23:53:59 -08001217 recycleTempRects(validRegions);
Adam Cohenc0dcf592011-06-01 15:30:43 -07001218 return bestXY;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001219 }
Winson Chungaafa03c2010-06-11 17:34:16 -07001220
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001221 /**
Adam Cohen482ed822012-03-02 14:15:13 -08001222 * Find a vacant area that will fit the given bounds nearest the requested
1223 * cell location, and will also weigh in a suggested direction vector of the
1224 * desired location. This method computers distance based on unit grid distances,
1225 * not pixel distances.
1226 *
Adam Cohen47a876d2012-03-19 13:21:41 -07001227 * @param cellX The X cell nearest to which you want to search for a vacant area.
1228 * @param cellY The Y cell nearest which you want to search for a vacant area.
Adam Cohen482ed822012-03-02 14:15:13 -08001229 * @param spanX Horizontal span of the object.
1230 * @param spanY Vertical span of the object.
Adam Cohen47a876d2012-03-19 13:21:41 -07001231 * @param direction The favored direction in which the views should move from x, y
Sunny Goyal9eba1fd2015-10-16 08:58:57 -07001232 * @param occupied The array which represents which cells in the CellLayout are occupied
Adam Cohen47a876d2012-03-19 13:21:41 -07001233 * @param blockOccupied The array which represents which cells in the specified block (cellX,
Winson Chung5f8afe62013-08-12 16:19:28 -07001234 * cellY, spanX, spanY) are occupied. This is used when try to move a group of views.
Adam Cohen482ed822012-03-02 14:15:13 -08001235 * @param result Array in which to place the result, or null (in which case a new array will
1236 * be allocated)
1237 * @return The X, Y cell of a vacant area that can contain this object,
1238 * nearest the requested location.
1239 */
1240 private int[] findNearestArea(int cellX, int cellY, int spanX, int spanY, int[] direction,
Adam Cohen47a876d2012-03-19 13:21:41 -07001241 boolean[][] occupied, boolean blockOccupied[][], int[] result) {
Adam Cohen482ed822012-03-02 14:15:13 -08001242 // Keep track of best-scoring drop area
1243 final int[] bestXY = result != null ? result : new int[2];
1244 float bestDistance = Float.MAX_VALUE;
1245 int bestDirectionScore = Integer.MIN_VALUE;
1246
1247 final int countX = mCountX;
1248 final int countY = mCountY;
1249
1250 for (int y = 0; y < countY - (spanY - 1); y++) {
1251 inner:
1252 for (int x = 0; x < countX - (spanX - 1); x++) {
1253 // First, let's see if this thing fits anywhere
1254 for (int i = 0; i < spanX; i++) {
1255 for (int j = 0; j < spanY; j++) {
Adam Cohen47a876d2012-03-19 13:21:41 -07001256 if (occupied[x + i][y + j] && (blockOccupied == null || blockOccupied[i][j])) {
Adam Cohen482ed822012-03-02 14:15:13 -08001257 continue inner;
1258 }
1259 }
1260 }
1261
Sunny Goyalf7a29e82015-04-24 15:20:43 -07001262 float distance = (float) Math.hypot(x - cellX, y - cellY);
Adam Cohen482ed822012-03-02 14:15:13 -08001263 int[] curDirection = mTmpPoint;
Adam Cohen47a876d2012-03-19 13:21:41 -07001264 computeDirectionVector(x - cellX, y - cellY, curDirection);
1265 // The direction score is just the dot product of the two candidate direction
1266 // and that passed in.
Adam Cohen482ed822012-03-02 14:15:13 -08001267 int curDirectionScore = direction[0] * curDirection[0] +
1268 direction[1] * curDirection[1];
Sunny Goyal8f90dcf2016-08-18 15:01:11 -07001269 if (Float.compare(distance, bestDistance) < 0 ||
1270 (Float.compare(distance, bestDistance) == 0
1271 && curDirectionScore > bestDirectionScore)) {
Adam Cohen482ed822012-03-02 14:15:13 -08001272 bestDistance = distance;
1273 bestDirectionScore = curDirectionScore;
1274 bestXY[0] = x;
1275 bestXY[1] = y;
1276 }
1277 }
1278 }
1279
1280 // Return -1, -1 if no suitable location found
1281 if (bestDistance == Float.MAX_VALUE) {
1282 bestXY[0] = -1;
1283 bestXY[1] = -1;
1284 }
1285 return bestXY;
1286 }
1287
1288 private boolean addViewToTempLocation(View v, Rect rectOccupiedByPotentialDrop,
Adam Cohen8baab352012-03-20 17:39:21 -07001289 int[] direction, ItemConfiguration currentState) {
1290 CellAndSpan c = currentState.map.get(v);
Adam Cohen482ed822012-03-02 14:15:13 -08001291 boolean success = false;
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001292 mTmpOccupied.markCells(c, false);
1293 mTmpOccupied.markCells(rectOccupiedByPotentialDrop, true);
Adam Cohen482ed822012-03-02 14:15:13 -08001294
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001295 findNearestArea(c.cellX, c.cellY, c.spanX, c.spanY, direction,
1296 mTmpOccupied.cells, null, mTempLocation);
Adam Cohen482ed822012-03-02 14:15:13 -08001297
1298 if (mTempLocation[0] >= 0 && mTempLocation[1] >= 0) {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001299 c.cellX = mTempLocation[0];
1300 c.cellY = mTempLocation[1];
Adam Cohen482ed822012-03-02 14:15:13 -08001301 success = true;
Adam Cohen482ed822012-03-02 14:15:13 -08001302 }
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001303 mTmpOccupied.markCells(c, true);
Adam Cohen482ed822012-03-02 14:15:13 -08001304 return success;
1305 }
1306
Adam Cohenf3900c22012-11-16 18:28:11 -08001307 /**
1308 * This helper class defines a cluster of views. It helps with defining complex edges
1309 * of the cluster and determining how those edges interact with other views. The edges
1310 * essentially define a fine-grained boundary around the cluster of views -- like a more
1311 * precise version of a bounding box.
1312 */
1313 private class ViewCluster {
Sunny Goyal6dc98b92016-04-03 15:25:29 -07001314 final static int LEFT = 1 << 0;
1315 final static int TOP = 1 << 1;
1316 final static int RIGHT = 1 << 2;
1317 final static int BOTTOM = 1 << 3;
Adam Cohen47a876d2012-03-19 13:21:41 -07001318
Rajeev Kumar9962dbe2017-06-12 12:16:20 -07001319 final ArrayList<View> views;
1320 final ItemConfiguration config;
1321 final Rect boundingRect = new Rect();
Adam Cohen47a876d2012-03-19 13:21:41 -07001322
Rajeev Kumar9962dbe2017-06-12 12:16:20 -07001323 final int[] leftEdge = new int[mCountY];
1324 final int[] rightEdge = new int[mCountY];
1325 final int[] topEdge = new int[mCountX];
1326 final int[] bottomEdge = new int[mCountX];
Sunny Goyal6dc98b92016-04-03 15:25:29 -07001327 int dirtyEdges;
1328 boolean boundingRectDirty;
Adam Cohenf3900c22012-11-16 18:28:11 -08001329
1330 @SuppressWarnings("unchecked")
1331 public ViewCluster(ArrayList<View> views, ItemConfiguration config) {
1332 this.views = (ArrayList<View>) views.clone();
1333 this.config = config;
1334 resetEdges();
Adam Cohen47a876d2012-03-19 13:21:41 -07001335 }
1336
Adam Cohenf3900c22012-11-16 18:28:11 -08001337 void resetEdges() {
1338 for (int i = 0; i < mCountX; i++) {
1339 topEdge[i] = -1;
1340 bottomEdge[i] = -1;
1341 }
1342 for (int i = 0; i < mCountY; i++) {
1343 leftEdge[i] = -1;
1344 rightEdge[i] = -1;
1345 }
Sunny Goyal6dc98b92016-04-03 15:25:29 -07001346 dirtyEdges = LEFT | TOP | RIGHT | BOTTOM;
Adam Cohenf3900c22012-11-16 18:28:11 -08001347 boundingRectDirty = true;
1348 }
Adam Cohen47a876d2012-03-19 13:21:41 -07001349
Sunny Goyal6dc98b92016-04-03 15:25:29 -07001350 void computeEdge(int which) {
Adam Cohenf3900c22012-11-16 18:28:11 -08001351 int count = views.size();
1352 for (int i = 0; i < count; i++) {
1353 CellAndSpan cs = config.map.get(views.get(i));
1354 switch (which) {
1355 case LEFT:
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001356 int left = cs.cellX;
1357 for (int j = cs.cellY; j < cs.cellY + cs.spanY; j++) {
Sunny Goyal6dc98b92016-04-03 15:25:29 -07001358 if (left < leftEdge[j] || leftEdge[j] < 0) {
1359 leftEdge[j] = left;
Adam Cohena56dc102012-07-13 13:41:42 -07001360 }
Adam Cohen47a876d2012-03-19 13:21:41 -07001361 }
Adam Cohenf3900c22012-11-16 18:28:11 -08001362 break;
1363 case RIGHT:
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001364 int right = cs.cellX + cs.spanX;
1365 for (int j = cs.cellY; j < cs.cellY + cs.spanY; j++) {
Sunny Goyal6dc98b92016-04-03 15:25:29 -07001366 if (right > rightEdge[j]) {
1367 rightEdge[j] = right;
Adam Cohenf3900c22012-11-16 18:28:11 -08001368 }
1369 }
1370 break;
1371 case TOP:
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001372 int top = cs.cellY;
1373 for (int j = cs.cellX; j < cs.cellX + cs.spanX; j++) {
Sunny Goyal6dc98b92016-04-03 15:25:29 -07001374 if (top < topEdge[j] || topEdge[j] < 0) {
1375 topEdge[j] = top;
Adam Cohenf3900c22012-11-16 18:28:11 -08001376 }
1377 }
1378 break;
1379 case BOTTOM:
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001380 int bottom = cs.cellY + cs.spanY;
1381 for (int j = cs.cellX; j < cs.cellX + cs.spanX; j++) {
Sunny Goyal6dc98b92016-04-03 15:25:29 -07001382 if (bottom > bottomEdge[j]) {
1383 bottomEdge[j] = bottom;
Adam Cohenf3900c22012-11-16 18:28:11 -08001384 }
1385 }
1386 break;
Adam Cohen47a876d2012-03-19 13:21:41 -07001387 }
1388 }
1389 }
Adam Cohenf3900c22012-11-16 18:28:11 -08001390
1391 boolean isViewTouchingEdge(View v, int whichEdge) {
1392 CellAndSpan cs = config.map.get(v);
1393
Sunny Goyal6dc98b92016-04-03 15:25:29 -07001394 if ((dirtyEdges & whichEdge) == whichEdge) {
1395 computeEdge(whichEdge);
1396 dirtyEdges &= ~whichEdge;
1397 }
Adam Cohenf3900c22012-11-16 18:28:11 -08001398
1399 switch (whichEdge) {
1400 case LEFT:
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001401 for (int i = cs.cellY; i < cs.cellY + cs.spanY; i++) {
1402 if (leftEdge[i] == cs.cellX + cs.spanX) {
Adam Cohenf3900c22012-11-16 18:28:11 -08001403 return true;
1404 }
1405 }
1406 break;
1407 case RIGHT:
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001408 for (int i = cs.cellY; i < cs.cellY + cs.spanY; i++) {
1409 if (rightEdge[i] == cs.cellX) {
Adam Cohenf3900c22012-11-16 18:28:11 -08001410 return true;
1411 }
1412 }
1413 break;
1414 case TOP:
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001415 for (int i = cs.cellX; i < cs.cellX + cs.spanX; i++) {
1416 if (topEdge[i] == cs.cellY + cs.spanY) {
Adam Cohenf3900c22012-11-16 18:28:11 -08001417 return true;
1418 }
1419 }
1420 break;
1421 case BOTTOM:
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001422 for (int i = cs.cellX; i < cs.cellX + cs.spanX; i++) {
1423 if (bottomEdge[i] == cs.cellY) {
Adam Cohenf3900c22012-11-16 18:28:11 -08001424 return true;
1425 }
1426 }
1427 break;
1428 }
1429 return false;
1430 }
1431
1432 void shift(int whichEdge, int delta) {
1433 for (View v: views) {
1434 CellAndSpan c = config.map.get(v);
1435 switch (whichEdge) {
1436 case LEFT:
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001437 c.cellX -= delta;
Adam Cohenf3900c22012-11-16 18:28:11 -08001438 break;
1439 case RIGHT:
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001440 c.cellX += delta;
Adam Cohenf3900c22012-11-16 18:28:11 -08001441 break;
1442 case TOP:
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001443 c.cellY -= delta;
Adam Cohenf3900c22012-11-16 18:28:11 -08001444 break;
1445 case BOTTOM:
1446 default:
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001447 c.cellY += delta;
Adam Cohenf3900c22012-11-16 18:28:11 -08001448 break;
1449 }
1450 }
1451 resetEdges();
1452 }
1453
1454 public void addView(View v) {
1455 views.add(v);
1456 resetEdges();
1457 }
1458
1459 public Rect getBoundingRect() {
1460 if (boundingRectDirty) {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001461 config.getBoundingRectForViews(views, boundingRect);
Adam Cohenf3900c22012-11-16 18:28:11 -08001462 }
1463 return boundingRect;
1464 }
1465
Rajeev Kumar9962dbe2017-06-12 12:16:20 -07001466 final PositionComparator comparator = new PositionComparator();
Adam Cohenf3900c22012-11-16 18:28:11 -08001467 class PositionComparator implements Comparator<View> {
1468 int whichEdge = 0;
1469 public int compare(View left, View right) {
1470 CellAndSpan l = config.map.get(left);
1471 CellAndSpan r = config.map.get(right);
1472 switch (whichEdge) {
1473 case LEFT:
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001474 return (r.cellX + r.spanX) - (l.cellX + l.spanX);
Adam Cohenf3900c22012-11-16 18:28:11 -08001475 case RIGHT:
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001476 return l.cellX - r.cellX;
Adam Cohenf3900c22012-11-16 18:28:11 -08001477 case TOP:
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001478 return (r.cellY + r.spanY) - (l.cellY + l.spanY);
Adam Cohenf3900c22012-11-16 18:28:11 -08001479 case BOTTOM:
1480 default:
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001481 return l.cellY - r.cellY;
Adam Cohenf3900c22012-11-16 18:28:11 -08001482 }
1483 }
1484 }
1485
1486 public void sortConfigurationForEdgePush(int edge) {
1487 comparator.whichEdge = edge;
1488 Collections.sort(config.sortedViews, comparator);
1489 }
Adam Cohen47a876d2012-03-19 13:21:41 -07001490 }
1491
Adam Cohenf3900c22012-11-16 18:28:11 -08001492 private boolean pushViewsToTempLocation(ArrayList<View> views, Rect rectOccupiedByPotentialDrop,
1493 int[] direction, View dragView, ItemConfiguration currentState) {
Adam Cohene0489502012-08-27 15:18:53 -07001494
Adam Cohenf3900c22012-11-16 18:28:11 -08001495 ViewCluster cluster = new ViewCluster(views, currentState);
1496 Rect clusterRect = cluster.getBoundingRect();
1497 int whichEdge;
1498 int pushDistance;
1499 boolean fail = false;
1500
1501 // Determine the edge of the cluster that will be leading the push and how far
1502 // the cluster must be shifted.
1503 if (direction[0] < 0) {
1504 whichEdge = ViewCluster.LEFT;
1505 pushDistance = clusterRect.right - rectOccupiedByPotentialDrop.left;
Adam Cohene0489502012-08-27 15:18:53 -07001506 } else if (direction[0] > 0) {
Adam Cohenf3900c22012-11-16 18:28:11 -08001507 whichEdge = ViewCluster.RIGHT;
1508 pushDistance = rectOccupiedByPotentialDrop.right - clusterRect.left;
1509 } else if (direction[1] < 0) {
1510 whichEdge = ViewCluster.TOP;
1511 pushDistance = clusterRect.bottom - rectOccupiedByPotentialDrop.top;
1512 } else {
1513 whichEdge = ViewCluster.BOTTOM;
1514 pushDistance = rectOccupiedByPotentialDrop.bottom - clusterRect.top;
Adam Cohene0489502012-08-27 15:18:53 -07001515 }
1516
Adam Cohenf3900c22012-11-16 18:28:11 -08001517 // Break early for invalid push distance.
1518 if (pushDistance <= 0) {
1519 return false;
Adam Cohene0489502012-08-27 15:18:53 -07001520 }
Adam Cohenf3900c22012-11-16 18:28:11 -08001521
1522 // Mark the occupied state as false for the group of views we want to move.
1523 for (View v: views) {
1524 CellAndSpan c = currentState.map.get(v);
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001525 mTmpOccupied.markCells(c, false);
Adam Cohenf3900c22012-11-16 18:28:11 -08001526 }
1527
1528 // We save the current configuration -- if we fail to find a solution we will revert
1529 // to the initial state. The process of finding a solution modifies the configuration
1530 // in place, hence the need for revert in the failure case.
1531 currentState.save();
1532
1533 // The pushing algorithm is simplified by considering the views in the order in which
1534 // they would be pushed by the cluster. For example, if the cluster is leading with its
1535 // left edge, we consider sort the views by their right edge, from right to left.
1536 cluster.sortConfigurationForEdgePush(whichEdge);
1537
1538 while (pushDistance > 0 && !fail) {
1539 for (View v: currentState.sortedViews) {
1540 // For each view that isn't in the cluster, we see if the leading edge of the
1541 // cluster is contacting the edge of that view. If so, we add that view to the
1542 // cluster.
1543 if (!cluster.views.contains(v) && v != dragView) {
1544 if (cluster.isViewTouchingEdge(v, whichEdge)) {
1545 LayoutParams lp = (LayoutParams) v.getLayoutParams();
1546 if (!lp.canReorder) {
1547 // The push solution includes the all apps button, this is not viable.
1548 fail = true;
1549 break;
1550 }
1551 cluster.addView(v);
1552 CellAndSpan c = currentState.map.get(v);
1553
1554 // Adding view to cluster, mark it as not occupied.
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001555 mTmpOccupied.markCells(c, false);
Adam Cohenf3900c22012-11-16 18:28:11 -08001556 }
1557 }
1558 }
1559 pushDistance--;
1560
1561 // The cluster has been completed, now we move the whole thing over in the appropriate
1562 // direction.
1563 cluster.shift(whichEdge, 1);
1564 }
1565
1566 boolean foundSolution = false;
1567 clusterRect = cluster.getBoundingRect();
1568
1569 // Due to the nature of the algorithm, the only check required to verify a valid solution
1570 // is to ensure that completed shifted cluster lies completely within the cell layout.
1571 if (!fail && clusterRect.left >= 0 && clusterRect.right <= mCountX && clusterRect.top >= 0 &&
1572 clusterRect.bottom <= mCountY) {
1573 foundSolution = true;
1574 } else {
1575 currentState.restore();
1576 }
1577
1578 // In either case, we set the occupied array as marked for the location of the views
1579 for (View v: cluster.views) {
1580 CellAndSpan c = currentState.map.get(v);
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001581 mTmpOccupied.markCells(c, true);
Adam Cohenf3900c22012-11-16 18:28:11 -08001582 }
1583
1584 return foundSolution;
Adam Cohene0489502012-08-27 15:18:53 -07001585 }
1586
Adam Cohen482ed822012-03-02 14:15:13 -08001587 private boolean addViewsToTempLocation(ArrayList<View> views, Rect rectOccupiedByPotentialDrop,
Adam Cohenf3900c22012-11-16 18:28:11 -08001588 int[] direction, View dragView, ItemConfiguration currentState) {
Adam Cohen482ed822012-03-02 14:15:13 -08001589 if (views.size() == 0) return true;
Adam Cohen482ed822012-03-02 14:15:13 -08001590
Adam Cohen8baab352012-03-20 17:39:21 -07001591 boolean success = false;
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001592 Rect boundingRect = new Rect();
Adam Cohen8baab352012-03-20 17:39:21 -07001593 // We construct a rect which represents the entire group of views passed in
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001594 currentState.getBoundingRectForViews(views, boundingRect);
Adam Cohen8baab352012-03-20 17:39:21 -07001595
Adam Cohen8baab352012-03-20 17:39:21 -07001596 // Mark the occupied state as false for the group of views we want to move.
Adam Cohenf3900c22012-11-16 18:28:11 -08001597 for (View v: views) {
Adam Cohen8baab352012-03-20 17:39:21 -07001598 CellAndSpan c = currentState.map.get(v);
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001599 mTmpOccupied.markCells(c, false);
Adam Cohen8baab352012-03-20 17:39:21 -07001600 }
1601
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001602 GridOccupancy blockOccupied = new GridOccupancy(boundingRect.width(), boundingRect.height());
Adam Cohen47a876d2012-03-19 13:21:41 -07001603 int top = boundingRect.top;
1604 int left = boundingRect.left;
Adam Cohen8baab352012-03-20 17:39:21 -07001605 // We mark more precisely which parts of the bounding rect are truly occupied, allowing
Adam Cohena56dc102012-07-13 13:41:42 -07001606 // for interlocking.
Adam Cohenf3900c22012-11-16 18:28:11 -08001607 for (View v: views) {
Adam Cohen8baab352012-03-20 17:39:21 -07001608 CellAndSpan c = currentState.map.get(v);
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001609 blockOccupied.markCells(c.cellX - left, c.cellY - top, c.spanX, c.spanY, true);
Adam Cohen47a876d2012-03-19 13:21:41 -07001610 }
1611
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001612 mTmpOccupied.markCells(rectOccupiedByPotentialDrop, true);
Adam Cohen482ed822012-03-02 14:15:13 -08001613
Adam Cohenf3900c22012-11-16 18:28:11 -08001614 findNearestArea(boundingRect.left, boundingRect.top, boundingRect.width(),
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001615 boundingRect.height(), direction,
1616 mTmpOccupied.cells, blockOccupied.cells, mTempLocation);
Adam Cohen482ed822012-03-02 14:15:13 -08001617
Adam Cohen8baab352012-03-20 17:39:21 -07001618 // If we successfuly found a location by pushing the block of views, we commit it
Adam Cohen482ed822012-03-02 14:15:13 -08001619 if (mTempLocation[0] >= 0 && mTempLocation[1] >= 0) {
Adam Cohen8baab352012-03-20 17:39:21 -07001620 int deltaX = mTempLocation[0] - boundingRect.left;
1621 int deltaY = mTempLocation[1] - boundingRect.top;
Adam Cohenf3900c22012-11-16 18:28:11 -08001622 for (View v: views) {
Adam Cohen8baab352012-03-20 17:39:21 -07001623 CellAndSpan c = currentState.map.get(v);
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001624 c.cellX += deltaX;
1625 c.cellY += deltaY;
Adam Cohen482ed822012-03-02 14:15:13 -08001626 }
1627 success = true;
1628 }
Adam Cohen8baab352012-03-20 17:39:21 -07001629
1630 // In either case, we set the occupied array as marked for the location of the views
Adam Cohenf3900c22012-11-16 18:28:11 -08001631 for (View v: views) {
Adam Cohen8baab352012-03-20 17:39:21 -07001632 CellAndSpan c = currentState.map.get(v);
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001633 mTmpOccupied.markCells(c, true);
Adam Cohen482ed822012-03-02 14:15:13 -08001634 }
1635 return success;
1636 }
1637
Adam Cohen4abc5bd2012-05-29 21:06:03 -07001638 // This method tries to find a reordering solution which satisfies the push mechanic by trying
1639 // to push items in each of the cardinal directions, in an order based on the direction vector
1640 // passed.
1641 private boolean attemptPushInDirection(ArrayList<View> intersectingViews, Rect occupied,
1642 int[] direction, View ignoreView, ItemConfiguration solution) {
1643 if ((Math.abs(direction[0]) + Math.abs(direction[1])) > 1) {
Winson Chung5f8afe62013-08-12 16:19:28 -07001644 // If the direction vector has two non-zero components, we try pushing
Adam Cohen4abc5bd2012-05-29 21:06:03 -07001645 // separately in each of the components.
1646 int temp = direction[1];
1647 direction[1] = 0;
Adam Cohenf3900c22012-11-16 18:28:11 -08001648
1649 if (pushViewsToTempLocation(intersectingViews, occupied, direction,
Adam Cohen4abc5bd2012-05-29 21:06:03 -07001650 ignoreView, solution)) {
1651 return true;
1652 }
1653 direction[1] = temp;
1654 temp = direction[0];
1655 direction[0] = 0;
Adam Cohenf3900c22012-11-16 18:28:11 -08001656
1657 if (pushViewsToTempLocation(intersectingViews, occupied, direction,
Adam Cohen4abc5bd2012-05-29 21:06:03 -07001658 ignoreView, solution)) {
1659 return true;
1660 }
1661 // Revert the direction
1662 direction[0] = temp;
1663
1664 // Now we try pushing in each component of the opposite direction
1665 direction[0] *= -1;
1666 direction[1] *= -1;
1667 temp = direction[1];
1668 direction[1] = 0;
Adam Cohenf3900c22012-11-16 18:28:11 -08001669 if (pushViewsToTempLocation(intersectingViews, occupied, direction,
Adam Cohen4abc5bd2012-05-29 21:06:03 -07001670 ignoreView, solution)) {
1671 return true;
1672 }
1673
1674 direction[1] = temp;
1675 temp = direction[0];
1676 direction[0] = 0;
Adam Cohenf3900c22012-11-16 18:28:11 -08001677 if (pushViewsToTempLocation(intersectingViews, occupied, direction,
Adam Cohen4abc5bd2012-05-29 21:06:03 -07001678 ignoreView, solution)) {
1679 return true;
1680 }
1681 // revert the direction
1682 direction[0] = temp;
1683 direction[0] *= -1;
1684 direction[1] *= -1;
Winson Chung5f8afe62013-08-12 16:19:28 -07001685
Adam Cohen4abc5bd2012-05-29 21:06:03 -07001686 } else {
1687 // If the direction vector has a single non-zero component, we push first in the
1688 // direction of the vector
Adam Cohenf3900c22012-11-16 18:28:11 -08001689 if (pushViewsToTempLocation(intersectingViews, occupied, direction,
Adam Cohen4abc5bd2012-05-29 21:06:03 -07001690 ignoreView, solution)) {
1691 return true;
1692 }
Adam Cohen4abc5bd2012-05-29 21:06:03 -07001693 // Then we try the opposite direction
1694 direction[0] *= -1;
1695 direction[1] *= -1;
Adam Cohenf3900c22012-11-16 18:28:11 -08001696 if (pushViewsToTempLocation(intersectingViews, occupied, direction,
Adam Cohen4abc5bd2012-05-29 21:06:03 -07001697 ignoreView, solution)) {
1698 return true;
1699 }
1700 // Switch the direction back
1701 direction[0] *= -1;
1702 direction[1] *= -1;
Winson Chung5f8afe62013-08-12 16:19:28 -07001703
1704 // If we have failed to find a push solution with the above, then we try
Adam Cohen4abc5bd2012-05-29 21:06:03 -07001705 // to find a solution by pushing along the perpendicular axis.
1706
1707 // Swap the components
1708 int temp = direction[1];
1709 direction[1] = direction[0];
1710 direction[0] = temp;
Adam Cohenf3900c22012-11-16 18:28:11 -08001711 if (pushViewsToTempLocation(intersectingViews, occupied, direction,
Adam Cohen4abc5bd2012-05-29 21:06:03 -07001712 ignoreView, solution)) {
1713 return true;
1714 }
1715
1716 // Then we try the opposite direction
1717 direction[0] *= -1;
1718 direction[1] *= -1;
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 }
1723 // Switch the direction back
1724 direction[0] *= -1;
1725 direction[1] *= -1;
1726
1727 // Swap the components back
1728 temp = direction[1];
1729 direction[1] = direction[0];
1730 direction[0] = temp;
1731 }
1732 return false;
1733 }
1734
Adam Cohen482ed822012-03-02 14:15:13 -08001735 private boolean rearrangementExists(int cellX, int cellY, int spanX, int spanY, int[] direction,
Adam Cohen8baab352012-03-20 17:39:21 -07001736 View ignoreView, ItemConfiguration solution) {
Winson Chunge3e03bc2012-05-01 15:10:11 -07001737 // Return early if get invalid cell positions
1738 if (cellX < 0 || cellY < 0) return false;
Adam Cohen482ed822012-03-02 14:15:13 -08001739
Adam Cohen8baab352012-03-20 17:39:21 -07001740 mIntersectingViews.clear();
Adam Cohen482ed822012-03-02 14:15:13 -08001741 mOccupiedRect.set(cellX, cellY, cellX + spanX, cellY + spanY);
Adam Cohen482ed822012-03-02 14:15:13 -08001742
Adam Cohen8baab352012-03-20 17:39:21 -07001743 // Mark the desired location of the view currently being dragged.
Adam Cohen482ed822012-03-02 14:15:13 -08001744 if (ignoreView != null) {
Adam Cohen8baab352012-03-20 17:39:21 -07001745 CellAndSpan c = solution.map.get(ignoreView);
Adam Cohen19f37922012-03-21 11:59:11 -07001746 if (c != null) {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001747 c.cellX = cellX;
1748 c.cellY = cellY;
Adam Cohen19f37922012-03-21 11:59:11 -07001749 }
Adam Cohen482ed822012-03-02 14:15:13 -08001750 }
Adam Cohen482ed822012-03-02 14:15:13 -08001751 Rect r0 = new Rect(cellX, cellY, cellX + spanX, cellY + spanY);
1752 Rect r1 = new Rect();
Adam Cohen8baab352012-03-20 17:39:21 -07001753 for (View child: solution.map.keySet()) {
Adam Cohen482ed822012-03-02 14:15:13 -08001754 if (child == ignoreView) continue;
Adam Cohen8baab352012-03-20 17:39:21 -07001755 CellAndSpan c = solution.map.get(child);
Adam Cohen482ed822012-03-02 14:15:13 -08001756 LayoutParams lp = (LayoutParams) child.getLayoutParams();
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001757 r1.set(c.cellX, c.cellY, c.cellX + c.spanX, c.cellY + c.spanY);
Adam Cohen482ed822012-03-02 14:15:13 -08001758 if (Rect.intersects(r0, r1)) {
1759 if (!lp.canReorder) {
1760 return false;
1761 }
1762 mIntersectingViews.add(child);
1763 }
1764 }
Adam Cohen47a876d2012-03-19 13:21:41 -07001765
Rajeev Kumar9962dbe2017-06-12 12:16:20 -07001766 solution.intersectingViews = new ArrayList<>(mIntersectingViews);
Adam Cohenfa3c58f2013-12-06 16:10:55 -08001767
Winson Chung5f8afe62013-08-12 16:19:28 -07001768 // First we try to find a solution which respects the push mechanic. That is,
Adam Cohen4abc5bd2012-05-29 21:06:03 -07001769 // we try to find a solution such that no displaced item travels through another item
1770 // without also displacing that item.
1771 if (attemptPushInDirection(mIntersectingViews, mOccupiedRect, direction, ignoreView,
Adam Cohen19f37922012-03-21 11:59:11 -07001772 solution)) {
Adam Cohen47a876d2012-03-19 13:21:41 -07001773 return true;
1774 }
Adam Cohen47a876d2012-03-19 13:21:41 -07001775
Adam Cohen4abc5bd2012-05-29 21:06:03 -07001776 // Next we try moving the views as a block, but without requiring the push mechanic.
Adam Cohenf3900c22012-11-16 18:28:11 -08001777 if (addViewsToTempLocation(mIntersectingViews, mOccupiedRect, direction, ignoreView,
Adam Cohen19f37922012-03-21 11:59:11 -07001778 solution)) {
Adam Cohen482ed822012-03-02 14:15:13 -08001779 return true;
1780 }
Adam Cohen47a876d2012-03-19 13:21:41 -07001781
Adam Cohen482ed822012-03-02 14:15:13 -08001782 // Ok, they couldn't move as a block, let's move them individually
1783 for (View v : mIntersectingViews) {
Adam Cohen8baab352012-03-20 17:39:21 -07001784 if (!addViewToTempLocation(v, mOccupiedRect, direction, solution)) {
Adam Cohen482ed822012-03-02 14:15:13 -08001785 return false;
1786 }
1787 }
1788 return true;
1789 }
1790
1791 /*
1792 * Returns a pair (x, y), where x,y are in {-1, 0, 1} corresponding to vector between
1793 * the provided point and the provided cell
1794 */
Adam Cohen47a876d2012-03-19 13:21:41 -07001795 private void computeDirectionVector(float deltaX, float deltaY, int[] result) {
Jon Mirandae96798e2016-12-07 12:10:44 -08001796 double angle = Math.atan(deltaY / deltaX);
Adam Cohen482ed822012-03-02 14:15:13 -08001797
1798 result[0] = 0;
1799 result[1] = 0;
1800 if (Math.abs(Math.cos(angle)) > 0.5f) {
1801 result[0] = (int) Math.signum(deltaX);
1802 }
1803 if (Math.abs(Math.sin(angle)) > 0.5f) {
1804 result[1] = (int) Math.signum(deltaY);
1805 }
1806 }
1807
Sunny Goyalf7a29e82015-04-24 15:20:43 -07001808 private ItemConfiguration findReorderSolution(int pixelX, int pixelY, int minSpanX, int minSpanY,
Adam Cohenfa3c58f2013-12-06 16:10:55 -08001809 int spanX, int spanY, int[] direction, View dragView, boolean decX,
1810 ItemConfiguration solution) {
Adam Cohen8baab352012-03-20 17:39:21 -07001811 // Copy the current state into the solution. This solution will be manipulated as necessary.
1812 copyCurrentStateToSolution(solution, false);
1813 // Copy the current occupied array into the temporary occupied array. This array will be
1814 // manipulated as necessary to find a solution.
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001815 mOccupied.copyTo(mTmpOccupied);
Adam Cohen482ed822012-03-02 14:15:13 -08001816
1817 // We find the nearest cell into which we would place the dragged item, assuming there's
1818 // nothing in its way.
1819 int result[] = new int[2];
1820 result = findNearestArea(pixelX, pixelY, spanX, spanY, result);
1821
Rajeev Kumar9962dbe2017-06-12 12:16:20 -07001822 boolean success;
Adam Cohen482ed822012-03-02 14:15:13 -08001823 // First we try the exact nearest position of the item being dragged,
1824 // we will then want to try to move this around to other neighbouring positions
Adam Cohen8baab352012-03-20 17:39:21 -07001825 success = rearrangementExists(result[0], result[1], spanX, spanY, direction, dragView,
1826 solution);
Adam Cohen482ed822012-03-02 14:15:13 -08001827
1828 if (!success) {
1829 // We try shrinking the widget down to size in an alternating pattern, shrink 1 in
1830 // x, then 1 in y etc.
1831 if (spanX > minSpanX && (minSpanY == spanY || decX)) {
Adam Cohenfa3c58f2013-12-06 16:10:55 -08001832 return findReorderSolution(pixelX, pixelY, minSpanX, minSpanY, spanX - 1, spanY,
1833 direction, dragView, false, solution);
Adam Cohen482ed822012-03-02 14:15:13 -08001834 } else if (spanY > minSpanY) {
Adam Cohenfa3c58f2013-12-06 16:10:55 -08001835 return findReorderSolution(pixelX, pixelY, minSpanX, minSpanY, spanX, spanY - 1,
1836 direction, dragView, true, solution);
Adam Cohen482ed822012-03-02 14:15:13 -08001837 }
1838 solution.isSolution = false;
1839 } else {
1840 solution.isSolution = true;
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001841 solution.cellX = result[0];
1842 solution.cellY = result[1];
1843 solution.spanX = spanX;
1844 solution.spanY = spanY;
Adam Cohen482ed822012-03-02 14:15:13 -08001845 }
1846 return solution;
1847 }
1848
1849 private void copyCurrentStateToSolution(ItemConfiguration solution, boolean temp) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07001850 int childCount = mShortcutsAndWidgets.getChildCount();
Adam Cohen482ed822012-03-02 14:15:13 -08001851 for (int i = 0; i < childCount; i++) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07001852 View child = mShortcutsAndWidgets.getChildAt(i);
Adam Cohen482ed822012-03-02 14:15:13 -08001853 LayoutParams lp = (LayoutParams) child.getLayoutParams();
Adam Cohen8baab352012-03-20 17:39:21 -07001854 CellAndSpan c;
Adam Cohen482ed822012-03-02 14:15:13 -08001855 if (temp) {
Adam Cohen8baab352012-03-20 17:39:21 -07001856 c = new CellAndSpan(lp.tmpCellX, lp.tmpCellY, lp.cellHSpan, lp.cellVSpan);
Adam Cohen482ed822012-03-02 14:15:13 -08001857 } else {
Adam Cohen8baab352012-03-20 17:39:21 -07001858 c = new CellAndSpan(lp.cellX, lp.cellY, lp.cellHSpan, lp.cellVSpan);
Adam Cohen482ed822012-03-02 14:15:13 -08001859 }
Adam Cohenf3900c22012-11-16 18:28:11 -08001860 solution.add(child, c);
Adam Cohen482ed822012-03-02 14:15:13 -08001861 }
1862 }
1863
1864 private void copySolutionToTempState(ItemConfiguration solution, View dragView) {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001865 mTmpOccupied.clear();
Adam Cohen482ed822012-03-02 14:15:13 -08001866
Michael Jurkaa52570f2012-03-20 03:18:20 -07001867 int childCount = mShortcutsAndWidgets.getChildCount();
Adam Cohen482ed822012-03-02 14:15:13 -08001868 for (int i = 0; i < childCount; i++) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07001869 View child = mShortcutsAndWidgets.getChildAt(i);
Adam Cohen482ed822012-03-02 14:15:13 -08001870 if (child == dragView) continue;
1871 LayoutParams lp = (LayoutParams) child.getLayoutParams();
Adam Cohen8baab352012-03-20 17:39:21 -07001872 CellAndSpan c = solution.map.get(child);
1873 if (c != null) {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001874 lp.tmpCellX = c.cellX;
1875 lp.tmpCellY = c.cellY;
Adam Cohen8baab352012-03-20 17:39:21 -07001876 lp.cellHSpan = c.spanX;
1877 lp.cellVSpan = c.spanY;
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001878 mTmpOccupied.markCells(c, true);
Adam Cohen482ed822012-03-02 14:15:13 -08001879 }
1880 }
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001881 mTmpOccupied.markCells(solution, true);
Adam Cohen482ed822012-03-02 14:15:13 -08001882 }
1883
1884 private void animateItemsToSolution(ItemConfiguration solution, View dragView, boolean
1885 commitDragView) {
1886
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001887 GridOccupancy occupied = DESTRUCTIVE_REORDER ? mOccupied : mTmpOccupied;
1888 occupied.clear();
Adam Cohen482ed822012-03-02 14:15:13 -08001889
Michael Jurkaa52570f2012-03-20 03:18:20 -07001890 int childCount = mShortcutsAndWidgets.getChildCount();
Adam Cohen482ed822012-03-02 14:15:13 -08001891 for (int i = 0; i < childCount; i++) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07001892 View child = mShortcutsAndWidgets.getChildAt(i);
Adam Cohen482ed822012-03-02 14:15:13 -08001893 if (child == dragView) continue;
Adam Cohen8baab352012-03-20 17:39:21 -07001894 CellAndSpan c = solution.map.get(child);
1895 if (c != null) {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001896 animateChildToPosition(child, c.cellX, c.cellY, REORDER_ANIMATION_DURATION, 0,
Adam Cohen19f37922012-03-21 11:59:11 -07001897 DESTRUCTIVE_REORDER, false);
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001898 occupied.markCells(c, true);
Adam Cohen482ed822012-03-02 14:15:13 -08001899 }
1900 }
1901 if (commitDragView) {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001902 occupied.markCells(solution, true);
Adam Cohen482ed822012-03-02 14:15:13 -08001903 }
1904 }
1905
Adam Cohenfa3c58f2013-12-06 16:10:55 -08001906
1907 // This method starts or changes the reorder preview animations
1908 private void beginOrAdjustReorderPreviewAnimations(ItemConfiguration solution,
1909 View dragView, int delay, int mode) {
Adam Cohen19f37922012-03-21 11:59:11 -07001910 int childCount = mShortcutsAndWidgets.getChildCount();
Adam Cohen19f37922012-03-21 11:59:11 -07001911 for (int i = 0; i < childCount; i++) {
1912 View child = mShortcutsAndWidgets.getChildAt(i);
1913 if (child == dragView) continue;
1914 CellAndSpan c = solution.map.get(child);
Adam Cohenfa3c58f2013-12-06 16:10:55 -08001915 boolean skip = mode == ReorderPreviewAnimation.MODE_HINT && solution.intersectingViews
1916 != null && !solution.intersectingViews.contains(child);
1917
Adam Cohen19f37922012-03-21 11:59:11 -07001918 LayoutParams lp = (LayoutParams) child.getLayoutParams();
Adam Cohenfa3c58f2013-12-06 16:10:55 -08001919 if (c != null && !skip) {
1920 ReorderPreviewAnimation rha = new ReorderPreviewAnimation(child, mode, lp.cellX,
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001921 lp.cellY, c.cellX, c.cellY, c.spanX, c.spanY);
Adam Cohend024f982012-05-23 18:26:45 -07001922 rha.animate();
Adam Cohen19f37922012-03-21 11:59:11 -07001923 }
1924 }
1925 }
1926
Adam Cohenfa3c58f2013-12-06 16:10:55 -08001927 // Class which represents the reorder preview animations. These animations show that an item is
Adam Cohen19f37922012-03-21 11:59:11 -07001928 // in a temporary state, and hint at where the item will return to.
Adam Cohenfa3c58f2013-12-06 16:10:55 -08001929 class ReorderPreviewAnimation {
Rajeev Kumar9962dbe2017-06-12 12:16:20 -07001930 final View child;
Adam Cohend024f982012-05-23 18:26:45 -07001931 float finalDeltaX;
1932 float finalDeltaY;
1933 float initDeltaX;
1934 float initDeltaY;
Rajeev Kumar9962dbe2017-06-12 12:16:20 -07001935 final float finalScale;
Adam Cohend024f982012-05-23 18:26:45 -07001936 float initScale;
Rajeev Kumar9962dbe2017-06-12 12:16:20 -07001937 final int mode;
Adam Cohenfa3c58f2013-12-06 16:10:55 -08001938 boolean repeating = false;
1939 private static final int PREVIEW_DURATION = 300;
1940 private static final int HINT_DURATION = Workspace.REORDER_TIMEOUT;
1941
Jon Miranda21266912016-12-19 14:12:05 -08001942 private static final float CHILD_DIVIDEND = 4.0f;
1943
Adam Cohenfa3c58f2013-12-06 16:10:55 -08001944 public static final int MODE_HINT = 0;
1945 public static final int MODE_PREVIEW = 1;
1946
Adam Cohene7587d22012-05-24 18:50:02 -07001947 Animator a;
Adam Cohen19f37922012-03-21 11:59:11 -07001948
Adam Cohenfa3c58f2013-12-06 16:10:55 -08001949 public ReorderPreviewAnimation(View child, int mode, int cellX0, int cellY0, int cellX1,
1950 int cellY1, int spanX, int spanY) {
Adam Cohen19f37922012-03-21 11:59:11 -07001951 regionToCenterPoint(cellX0, cellY0, spanX, spanY, mTmpPoint);
1952 final int x0 = mTmpPoint[0];
1953 final int y0 = mTmpPoint[1];
1954 regionToCenterPoint(cellX1, cellY1, spanX, spanY, mTmpPoint);
1955 final int x1 = mTmpPoint[0];
1956 final int y1 = mTmpPoint[1];
1957 final int dX = x1 - x0;
1958 final int dY = y1 - y0;
Jon Miranda21266912016-12-19 14:12:05 -08001959
1960 this.child = child;
1961 this.mode = mode;
1962 setInitialAnimationValues(false);
1963 finalScale = (mChildScale - (CHILD_DIVIDEND / child.getWidth())) * initScale;
1964 finalDeltaX = initDeltaX;
1965 finalDeltaY = initDeltaY;
Adam Cohenfa3c58f2013-12-06 16:10:55 -08001966 int dir = mode == MODE_HINT ? -1 : 1;
Adam Cohen19f37922012-03-21 11:59:11 -07001967 if (dX == dY && dX == 0) {
1968 } else {
1969 if (dY == 0) {
Jon Miranda21266912016-12-19 14:12:05 -08001970 finalDeltaX += - dir * Math.signum(dX) * mReorderPreviewAnimationMagnitude;
Adam Cohen19f37922012-03-21 11:59:11 -07001971 } else if (dX == 0) {
Jon Miranda21266912016-12-19 14:12:05 -08001972 finalDeltaY += - dir * Math.signum(dY) * mReorderPreviewAnimationMagnitude;
Adam Cohen19f37922012-03-21 11:59:11 -07001973 } else {
1974 double angle = Math.atan( (float) (dY) / dX);
Jon Miranda21266912016-12-19 14:12:05 -08001975 finalDeltaX += (int) (- dir * Math.signum(dX) *
Adam Cohenfa3c58f2013-12-06 16:10:55 -08001976 Math.abs(Math.cos(angle) * mReorderPreviewAnimationMagnitude));
Jon Miranda21266912016-12-19 14:12:05 -08001977 finalDeltaY += (int) (- dir * Math.signum(dY) *
Adam Cohenfa3c58f2013-12-06 16:10:55 -08001978 Math.abs(Math.sin(angle) * mReorderPreviewAnimationMagnitude));
Adam Cohen19f37922012-03-21 11:59:11 -07001979 }
1980 }
Jon Miranda21266912016-12-19 14:12:05 -08001981 }
1982
1983 void setInitialAnimationValues(boolean restoreOriginalValues) {
1984 if (restoreOriginalValues) {
1985 if (child instanceof LauncherAppWidgetHostView) {
1986 LauncherAppWidgetHostView lahv = (LauncherAppWidgetHostView) child;
1987 initScale = lahv.getScaleToFit();
1988 initDeltaX = lahv.getTranslationForCentering().x;
1989 initDeltaY = lahv.getTranslationForCentering().y;
1990 } else {
1991 initScale = mChildScale;
1992 initDeltaX = 0;
1993 initDeltaY = 0;
1994 }
1995 } else {
1996 initScale = child.getScaleX();
1997 initDeltaX = child.getTranslationX();
1998 initDeltaY = child.getTranslationY();
1999 }
Adam Cohen19f37922012-03-21 11:59:11 -07002000 }
2001
Adam Cohend024f982012-05-23 18:26:45 -07002002 void animate() {
Jon Miranda21266912016-12-19 14:12:05 -08002003 boolean noMovement = (finalDeltaX == initDeltaX) && (finalDeltaY == initDeltaY);
2004
Adam Cohen19f37922012-03-21 11:59:11 -07002005 if (mShakeAnimators.containsKey(child)) {
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002006 ReorderPreviewAnimation oldAnimation = mShakeAnimators.get(child);
Adam Cohend024f982012-05-23 18:26:45 -07002007 oldAnimation.cancel();
Adam Cohen19f37922012-03-21 11:59:11 -07002008 mShakeAnimators.remove(child);
Jon Miranda21266912016-12-19 14:12:05 -08002009 if (noMovement) {
Adam Cohene7587d22012-05-24 18:50:02 -07002010 completeAnimationImmediately();
2011 return;
2012 }
Adam Cohen19f37922012-03-21 11:59:11 -07002013 }
Jon Miranda21266912016-12-19 14:12:05 -08002014 if (noMovement) {
Adam Cohen19f37922012-03-21 11:59:11 -07002015 return;
2016 }
Jon Mirandacda3bfb2017-02-06 15:54:41 -08002017 ValueAnimator va = LauncherAnimUtils.ofFloat(0f, 1f);
Adam Cohene7587d22012-05-24 18:50:02 -07002018 a = va;
Tony Wickham9e0702f2015-09-02 14:45:39 -07002019
2020 // Animations are disabled in power save mode, causing the repeated animation to jump
2021 // spastically between beginning and end states. Since this looks bad, we don't repeat
2022 // the animation in power save mode.
Tony Wickham112ac952015-11-12 12:31:50 -08002023 if (!Utilities.isPowerSaverOn(getContext())) {
Tony Wickham9e0702f2015-09-02 14:45:39 -07002024 va.setRepeatMode(ValueAnimator.REVERSE);
2025 va.setRepeatCount(ValueAnimator.INFINITE);
2026 }
2027
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002028 va.setDuration(mode == MODE_HINT ? HINT_DURATION : PREVIEW_DURATION);
Adam Cohend024f982012-05-23 18:26:45 -07002029 va.setStartDelay((int) (Math.random() * 60));
Adam Cohen19f37922012-03-21 11:59:11 -07002030 va.addUpdateListener(new AnimatorUpdateListener() {
2031 @Override
2032 public void onAnimationUpdate(ValueAnimator animation) {
Jon Mirandae96798e2016-12-07 12:10:44 -08002033 float r = (Float) animation.getAnimatedValue();
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002034 float r1 = (mode == MODE_HINT && repeating) ? 1.0f : r;
2035 float x = r1 * finalDeltaX + (1 - r1) * initDeltaX;
2036 float y = r1 * finalDeltaY + (1 - r1) * initDeltaY;
Adam Cohen19f37922012-03-21 11:59:11 -07002037 child.setTranslationX(x);
2038 child.setTranslationY(y);
Adam Cohend024f982012-05-23 18:26:45 -07002039 float s = r * finalScale + (1 - r) * initScale;
Brandon Keely50e6e562012-05-08 16:28:49 -07002040 child.setScaleX(s);
2041 child.setScaleY(s);
Adam Cohen19f37922012-03-21 11:59:11 -07002042 }
2043 });
2044 va.addListener(new AnimatorListenerAdapter() {
2045 public void onAnimationRepeat(Animator animation) {
Adam Cohen19f37922012-03-21 11:59:11 -07002046 // We make sure to end only after a full period
Jon Miranda21266912016-12-19 14:12:05 -08002047 setInitialAnimationValues(true);
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002048 repeating = true;
Adam Cohen19f37922012-03-21 11:59:11 -07002049 }
2050 });
Adam Cohen19f37922012-03-21 11:59:11 -07002051 mShakeAnimators.put(child, this);
2052 va.start();
2053 }
2054
Adam Cohend024f982012-05-23 18:26:45 -07002055 private void cancel() {
Adam Cohene7587d22012-05-24 18:50:02 -07002056 if (a != null) {
2057 a.cancel();
2058 }
Adam Cohen19f37922012-03-21 11:59:11 -07002059 }
Adam Cohene7587d22012-05-24 18:50:02 -07002060
Adam Cohen091440a2015-03-18 14:16:05 -07002061 @Thunk void completeAnimationImmediately() {
Adam Cohene7587d22012-05-24 18:50:02 -07002062 if (a != null) {
2063 a.cancel();
2064 }
Brandon Keely50e6e562012-05-08 16:28:49 -07002065
Jon Miranda72dbd912017-01-04 14:03:07 -08002066 setInitialAnimationValues(true);
Sunny Goyal9e76f682017-02-13 12:13:43 -08002067 a = LauncherAnimUtils.ofPropertyValuesHolder(child,
2068 new PropertyListBuilder()
2069 .scale(initScale)
2070 .translationX(initDeltaX)
2071 .translationY(initDeltaY)
2072 .build())
2073 .setDuration(REORDER_ANIMATION_DURATION);
Sunny Goyal5d2fc322015-07-06 22:52:49 -07002074 a.setInterpolator(new android.view.animation.DecelerateInterpolator(1.5f));
2075 a.start();
Brandon Keely50e6e562012-05-08 16:28:49 -07002076 }
Adam Cohen19f37922012-03-21 11:59:11 -07002077 }
2078
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002079 private void completeAndClearReorderPreviewAnimations() {
2080 for (ReorderPreviewAnimation a: mShakeAnimators.values()) {
Brandon Keely50e6e562012-05-08 16:28:49 -07002081 a.completeAnimationImmediately();
Adam Cohen19f37922012-03-21 11:59:11 -07002082 }
2083 mShakeAnimators.clear();
2084 }
2085
Adam Cohen482ed822012-03-02 14:15:13 -08002086 private void commitTempPlacement() {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07002087 mTmpOccupied.copyTo(mOccupied);
Sunny Goyalaa8ef112015-06-12 20:04:41 -07002088
2089 long screenId = mLauncher.getWorkspace().getIdForScreen(this);
2090 int container = Favorites.CONTAINER_DESKTOP;
2091
Sunny Goyalc13403c2016-11-18 23:44:48 -08002092 if (mContainerType == HOTSEAT) {
Sunny Goyalaa8ef112015-06-12 20:04:41 -07002093 screenId = -1;
2094 container = Favorites.CONTAINER_HOTSEAT;
2095 }
2096
Michael Jurkaa52570f2012-03-20 03:18:20 -07002097 int childCount = mShortcutsAndWidgets.getChildCount();
Adam Cohen482ed822012-03-02 14:15:13 -08002098 for (int i = 0; i < childCount; i++) {
Adam Cohenea889a22012-03-27 16:45:39 -07002099 View child = mShortcutsAndWidgets.getChildAt(i);
2100 LayoutParams lp = (LayoutParams) child.getLayoutParams();
2101 ItemInfo info = (ItemInfo) child.getTag();
Adam Cohen2acce882012-03-28 19:03:19 -07002102 // We do a null check here because the item info can be null in the case of the
2103 // AllApps button in the hotseat.
2104 if (info != null) {
Sunny Goyalaa8ef112015-06-12 20:04:41 -07002105 final boolean requiresDbUpdate = (info.cellX != lp.tmpCellX
2106 || info.cellY != lp.tmpCellY || info.spanX != lp.cellHSpan
2107 || info.spanY != lp.cellVSpan);
2108
Adam Cohen2acce882012-03-28 19:03:19 -07002109 info.cellX = lp.cellX = lp.tmpCellX;
2110 info.cellY = lp.cellY = lp.tmpCellY;
Adam Cohenbebf0422012-04-11 18:06:28 -07002111 info.spanX = lp.cellHSpan;
2112 info.spanY = lp.cellVSpan;
Sunny Goyalaa8ef112015-06-12 20:04:41 -07002113
2114 if (requiresDbUpdate) {
Sunny Goyal43bf11d2017-02-02 13:52:53 -08002115 mLauncher.getModelWriter().modifyItemInDatabase(info, container, screenId,
Sunny Goyalaa8ef112015-06-12 20:04:41 -07002116 info.cellX, info.cellY, info.spanX, info.spanY);
2117 }
Adam Cohen2acce882012-03-28 19:03:19 -07002118 }
Adam Cohen482ed822012-03-02 14:15:13 -08002119 }
2120 }
2121
Sunny Goyalf7a29e82015-04-24 15:20:43 -07002122 private void setUseTempCoords(boolean useTempCoords) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07002123 int childCount = mShortcutsAndWidgets.getChildCount();
Adam Cohen482ed822012-03-02 14:15:13 -08002124 for (int i = 0; i < childCount; i++) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07002125 LayoutParams lp = (LayoutParams) mShortcutsAndWidgets.getChildAt(i).getLayoutParams();
Adam Cohen482ed822012-03-02 14:15:13 -08002126 lp.useTmpCoords = useTempCoords;
2127 }
2128 }
2129
Sunny Goyalf7a29e82015-04-24 15:20:43 -07002130 private ItemConfiguration findConfigurationNoShuffle(int pixelX, int pixelY, int minSpanX, int minSpanY,
Adam Cohen482ed822012-03-02 14:15:13 -08002131 int spanX, int spanY, View dragView, ItemConfiguration solution) {
2132 int[] result = new int[2];
2133 int[] resultSpan = new int[2];
Sunny Goyalf7a29e82015-04-24 15:20:43 -07002134 findNearestVacantArea(pixelX, pixelY, minSpanX, minSpanY, spanX, spanY, result,
Adam Cohen482ed822012-03-02 14:15:13 -08002135 resultSpan);
2136 if (result[0] >= 0 && result[1] >= 0) {
2137 copyCurrentStateToSolution(solution, false);
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07002138 solution.cellX = result[0];
2139 solution.cellY = result[1];
2140 solution.spanX = resultSpan[0];
2141 solution.spanY = resultSpan[1];
Adam Cohen482ed822012-03-02 14:15:13 -08002142 solution.isSolution = true;
2143 } else {
2144 solution.isSolution = false;
2145 }
2146 return solution;
2147 }
2148
Adam Cohen19f37922012-03-21 11:59:11 -07002149 /* This seems like it should be obvious and straight-forward, but when the direction vector
2150 needs to match with the notion of the dragView pushing other views, we have to employ
2151 a slightly more subtle notion of the direction vector. The question is what two points is
2152 the vector between? The center of the dragView and its desired destination? Not quite, as
2153 this doesn't necessarily coincide with the interaction of the dragView and items occupying
2154 those cells. Instead we use some heuristics to often lock the vector to up, down, left
2155 or right, which helps make pushing feel right.
2156 */
2157 private void getDirectionVectorForDrop(int dragViewCenterX, int dragViewCenterY, int spanX,
2158 int spanY, View dragView, int[] resultDirection) {
2159 int[] targetDestination = new int[2];
2160
2161 findNearestArea(dragViewCenterX, dragViewCenterY, spanX, spanY, targetDestination);
2162 Rect dragRect = new Rect();
2163 regionToRect(targetDestination[0], targetDestination[1], spanX, spanY, dragRect);
2164 dragRect.offset(dragViewCenterX - dragRect.centerX(), dragViewCenterY - dragRect.centerY());
2165
2166 Rect dropRegionRect = new Rect();
2167 getViewsIntersectingRegion(targetDestination[0], targetDestination[1], spanX, spanY,
2168 dragView, dropRegionRect, mIntersectingViews);
2169
2170 int dropRegionSpanX = dropRegionRect.width();
2171 int dropRegionSpanY = dropRegionRect.height();
2172
2173 regionToRect(dropRegionRect.left, dropRegionRect.top, dropRegionRect.width(),
2174 dropRegionRect.height(), dropRegionRect);
2175
2176 int deltaX = (dropRegionRect.centerX() - dragViewCenterX) / spanX;
2177 int deltaY = (dropRegionRect.centerY() - dragViewCenterY) / spanY;
2178
2179 if (dropRegionSpanX == mCountX || spanX == mCountX) {
2180 deltaX = 0;
2181 }
2182 if (dropRegionSpanY == mCountY || spanY == mCountY) {
2183 deltaY = 0;
2184 }
2185
2186 if (deltaX == 0 && deltaY == 0) {
2187 // No idea what to do, give a random direction.
2188 resultDirection[0] = 1;
2189 resultDirection[1] = 0;
2190 } else {
2191 computeDirectionVector(deltaX, deltaY, resultDirection);
2192 }
2193 }
2194
2195 // For a given cell and span, fetch the set of views intersecting the region.
2196 private void getViewsIntersectingRegion(int cellX, int cellY, int spanX, int spanY,
2197 View dragView, Rect boundingRect, ArrayList<View> intersectingViews) {
2198 if (boundingRect != null) {
2199 boundingRect.set(cellX, cellY, cellX + spanX, cellY + spanY);
2200 }
2201 intersectingViews.clear();
2202 Rect r0 = new Rect(cellX, cellY, cellX + spanX, cellY + spanY);
2203 Rect r1 = new Rect();
2204 final int count = mShortcutsAndWidgets.getChildCount();
2205 for (int i = 0; i < count; i++) {
2206 View child = mShortcutsAndWidgets.getChildAt(i);
2207 if (child == dragView) continue;
2208 LayoutParams lp = (LayoutParams) child.getLayoutParams();
2209 r1.set(lp.cellX, lp.cellY, lp.cellX + lp.cellHSpan, lp.cellY + lp.cellVSpan);
2210 if (Rect.intersects(r0, r1)) {
2211 mIntersectingViews.add(child);
2212 if (boundingRect != null) {
2213 boundingRect.union(r1);
2214 }
2215 }
2216 }
2217 }
2218
2219 boolean isNearestDropLocationOccupied(int pixelX, int pixelY, int spanX, int spanY,
2220 View dragView, int[] result) {
2221 result = findNearestArea(pixelX, pixelY, spanX, spanY, result);
2222 getViewsIntersectingRegion(result[0], result[1], spanX, spanY, dragView, null,
2223 mIntersectingViews);
2224 return !mIntersectingViews.isEmpty();
2225 }
2226
2227 void revertTempState() {
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002228 completeAndClearReorderPreviewAnimations();
2229 if (isItemPlacementDirty() && !DESTRUCTIVE_REORDER) {
2230 final int count = mShortcutsAndWidgets.getChildCount();
2231 for (int i = 0; i < count; i++) {
2232 View child = mShortcutsAndWidgets.getChildAt(i);
2233 LayoutParams lp = (LayoutParams) child.getLayoutParams();
2234 if (lp.tmpCellX != lp.cellX || lp.tmpCellY != lp.cellY) {
2235 lp.tmpCellX = lp.cellX;
2236 lp.tmpCellY = lp.cellY;
2237 animateChildToPosition(child, lp.cellX, lp.cellY, REORDER_ANIMATION_DURATION,
2238 0, false, false);
2239 }
Adam Cohen19f37922012-03-21 11:59:11 -07002240 }
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002241 setItemPlacementDirty(false);
Adam Cohen19f37922012-03-21 11:59:11 -07002242 }
Adam Cohen19f37922012-03-21 11:59:11 -07002243 }
2244
Adam Cohenbebf0422012-04-11 18:06:28 -07002245 boolean createAreaForResize(int cellX, int cellY, int spanX, int spanY,
2246 View dragView, int[] direction, boolean commit) {
2247 int[] pixelXY = new int[2];
2248 regionToCenterPoint(cellX, cellY, spanX, spanY, pixelXY);
2249
2250 // First we determine if things have moved enough to cause a different layout
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002251 ItemConfiguration swapSolution = findReorderSolution(pixelXY[0], pixelXY[1], spanX, spanY,
Adam Cohenbebf0422012-04-11 18:06:28 -07002252 spanX, spanY, direction, dragView, true, new ItemConfiguration());
2253
2254 setUseTempCoords(true);
2255 if (swapSolution != null && swapSolution.isSolution) {
2256 // If we're just testing for a possible location (MODE_ACCEPT_DROP), we don't bother
2257 // committing anything or animating anything as we just want to determine if a solution
2258 // exists
2259 copySolutionToTempState(swapSolution, dragView);
2260 setItemPlacementDirty(true);
2261 animateItemsToSolution(swapSolution, dragView, commit);
2262
2263 if (commit) {
2264 commitTempPlacement();
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002265 completeAndClearReorderPreviewAnimations();
Adam Cohenbebf0422012-04-11 18:06:28 -07002266 setItemPlacementDirty(false);
2267 } else {
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002268 beginOrAdjustReorderPreviewAnimations(swapSolution, dragView,
2269 REORDER_ANIMATION_DURATION, ReorderPreviewAnimation.MODE_PREVIEW);
Adam Cohenbebf0422012-04-11 18:06:28 -07002270 }
2271 mShortcutsAndWidgets.requestLayout();
2272 }
2273 return swapSolution.isSolution;
2274 }
2275
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002276 int[] performReorder(int pixelX, int pixelY, int minSpanX, int minSpanY, int spanX, int spanY,
Adam Cohen482ed822012-03-02 14:15:13 -08002277 View dragView, int[] result, int resultSpan[], int mode) {
Adam Cohen482ed822012-03-02 14:15:13 -08002278 // First we determine if things have moved enough to cause a different layout
Adam Cohen47a876d2012-03-19 13:21:41 -07002279 result = findNearestArea(pixelX, pixelY, spanX, spanY, result);
Adam Cohen482ed822012-03-02 14:15:13 -08002280
2281 if (resultSpan == null) {
2282 resultSpan = new int[2];
2283 }
2284
Adam Cohen19f37922012-03-21 11:59:11 -07002285 // When we are checking drop validity or actually dropping, we don't recompute the
2286 // direction vector, since we want the solution to match the preview, and it's possible
2287 // that the exact position of the item has changed to result in a new reordering outcome.
Adam Cohenb209e632012-03-27 17:09:36 -07002288 if ((mode == MODE_ON_DROP || mode == MODE_ON_DROP_EXTERNAL || mode == MODE_ACCEPT_DROP)
2289 && mPreviousReorderDirection[0] != INVALID_DIRECTION) {
Adam Cohen19f37922012-03-21 11:59:11 -07002290 mDirectionVector[0] = mPreviousReorderDirection[0];
2291 mDirectionVector[1] = mPreviousReorderDirection[1];
2292 // We reset this vector after drop
Adam Cohenb209e632012-03-27 17:09:36 -07002293 if (mode == MODE_ON_DROP || mode == MODE_ON_DROP_EXTERNAL) {
2294 mPreviousReorderDirection[0] = INVALID_DIRECTION;
2295 mPreviousReorderDirection[1] = INVALID_DIRECTION;
Adam Cohen19f37922012-03-21 11:59:11 -07002296 }
2297 } else {
2298 getDirectionVectorForDrop(pixelX, pixelY, spanX, spanY, dragView, mDirectionVector);
2299 mPreviousReorderDirection[0] = mDirectionVector[0];
2300 mPreviousReorderDirection[1] = mDirectionVector[1];
2301 }
2302
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002303 // Find a solution involving pushing / displacing any items in the way
2304 ItemConfiguration swapSolution = findReorderSolution(pixelX, pixelY, minSpanX, minSpanY,
Adam Cohen482ed822012-03-02 14:15:13 -08002305 spanX, spanY, mDirectionVector, dragView, true, new ItemConfiguration());
2306
2307 // We attempt the approach which doesn't shuffle views at all
2308 ItemConfiguration noShuffleSolution = findConfigurationNoShuffle(pixelX, pixelY, minSpanX,
2309 minSpanY, spanX, spanY, dragView, new ItemConfiguration());
2310
2311 ItemConfiguration finalSolution = null;
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002312
2313 // If the reorder solution requires resizing (shrinking) the item being dropped, we instead
2314 // favor a solution in which the item is not resized, but
Adam Cohen482ed822012-03-02 14:15:13 -08002315 if (swapSolution.isSolution && swapSolution.area() >= noShuffleSolution.area()) {
2316 finalSolution = swapSolution;
2317 } else if (noShuffleSolution.isSolution) {
2318 finalSolution = noShuffleSolution;
2319 }
2320
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002321 if (mode == MODE_SHOW_REORDER_HINT) {
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002322 if (finalSolution != null) {
Adam Cohenfe692872013-12-11 14:47:23 -08002323 beginOrAdjustReorderPreviewAnimations(finalSolution, dragView, 0,
2324 ReorderPreviewAnimation.MODE_HINT);
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07002325 result[0] = finalSolution.cellX;
2326 result[1] = finalSolution.cellY;
2327 resultSpan[0] = finalSolution.spanX;
2328 resultSpan[1] = finalSolution.spanY;
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002329 } else {
2330 result[0] = result[1] = resultSpan[0] = resultSpan[1] = -1;
2331 }
2332 return result;
2333 }
2334
Adam Cohen482ed822012-03-02 14:15:13 -08002335 boolean foundSolution = true;
2336 if (!DESTRUCTIVE_REORDER) {
2337 setUseTempCoords(true);
2338 }
2339
2340 if (finalSolution != null) {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07002341 result[0] = finalSolution.cellX;
2342 result[1] = finalSolution.cellY;
2343 resultSpan[0] = finalSolution.spanX;
2344 resultSpan[1] = finalSolution.spanY;
Adam Cohen482ed822012-03-02 14:15:13 -08002345
2346 // If we're just testing for a possible location (MODE_ACCEPT_DROP), we don't bother
2347 // committing anything or animating anything as we just want to determine if a solution
2348 // exists
2349 if (mode == MODE_DRAG_OVER || mode == MODE_ON_DROP || mode == MODE_ON_DROP_EXTERNAL) {
2350 if (!DESTRUCTIVE_REORDER) {
2351 copySolutionToTempState(finalSolution, dragView);
2352 }
2353 setItemPlacementDirty(true);
2354 animateItemsToSolution(finalSolution, dragView, mode == MODE_ON_DROP);
2355
Adam Cohen19f37922012-03-21 11:59:11 -07002356 if (!DESTRUCTIVE_REORDER &&
2357 (mode == MODE_ON_DROP || mode == MODE_ON_DROP_EXTERNAL)) {
Adam Cohen482ed822012-03-02 14:15:13 -08002358 commitTempPlacement();
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002359 completeAndClearReorderPreviewAnimations();
Adam Cohen19f37922012-03-21 11:59:11 -07002360 setItemPlacementDirty(false);
2361 } else {
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002362 beginOrAdjustReorderPreviewAnimations(finalSolution, dragView,
2363 REORDER_ANIMATION_DURATION, ReorderPreviewAnimation.MODE_PREVIEW);
Adam Cohen482ed822012-03-02 14:15:13 -08002364 }
2365 }
2366 } else {
2367 foundSolution = false;
2368 result[0] = result[1] = resultSpan[0] = resultSpan[1] = -1;
2369 }
2370
2371 if ((mode == MODE_ON_DROP || !foundSolution) && !DESTRUCTIVE_REORDER) {
2372 setUseTempCoords(false);
2373 }
Adam Cohen482ed822012-03-02 14:15:13 -08002374
Michael Jurkaa52570f2012-03-20 03:18:20 -07002375 mShortcutsAndWidgets.requestLayout();
Adam Cohen482ed822012-03-02 14:15:13 -08002376 return result;
2377 }
2378
Adam Cohen19f37922012-03-21 11:59:11 -07002379 void setItemPlacementDirty(boolean dirty) {
2380 mItemPlacementDirty = dirty;
Adam Cohen482ed822012-03-02 14:15:13 -08002381 }
Adam Cohen19f37922012-03-21 11:59:11 -07002382 boolean isItemPlacementDirty() {
2383 return mItemPlacementDirty;
Adam Cohen482ed822012-03-02 14:15:13 -08002384 }
2385
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07002386 private static class ItemConfiguration extends CellAndSpan {
Rajeev Kumar9962dbe2017-06-12 12:16:20 -07002387 final ArrayMap<View, CellAndSpan> map = new ArrayMap<>();
2388 private final ArrayMap<View, CellAndSpan> savedMap = new ArrayMap<>();
2389 final ArrayList<View> sortedViews = new ArrayList<>();
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002390 ArrayList<View> intersectingViews;
Adam Cohen482ed822012-03-02 14:15:13 -08002391 boolean isSolution = false;
Adam Cohen482ed822012-03-02 14:15:13 -08002392
Adam Cohenf3900c22012-11-16 18:28:11 -08002393 void save() {
2394 // Copy current state into savedMap
2395 for (View v: map.keySet()) {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07002396 savedMap.get(v).copyFrom(map.get(v));
Adam Cohenf3900c22012-11-16 18:28:11 -08002397 }
2398 }
2399
2400 void restore() {
2401 // Restore current state from savedMap
2402 for (View v: savedMap.keySet()) {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07002403 map.get(v).copyFrom(savedMap.get(v));
Adam Cohenf3900c22012-11-16 18:28:11 -08002404 }
2405 }
2406
2407 void add(View v, CellAndSpan cs) {
2408 map.put(v, cs);
2409 savedMap.put(v, new CellAndSpan());
2410 sortedViews.add(v);
2411 }
2412
Adam Cohen482ed822012-03-02 14:15:13 -08002413 int area() {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07002414 return spanX * spanY;
Adam Cohenf3900c22012-11-16 18:28:11 -08002415 }
2416
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07002417 void getBoundingRectForViews(ArrayList<View> views, Rect outRect) {
2418 boolean first = true;
2419 for (View v: views) {
2420 CellAndSpan c = map.get(v);
2421 if (first) {
2422 outRect.set(c.cellX, c.cellY, c.cellX + c.spanX, c.cellY + c.spanY);
2423 first = false;
2424 } else {
2425 outRect.union(c.cellX, c.cellY, c.cellX + c.spanX, c.cellY + c.spanY);
2426 }
2427 }
Adam Cohenf3900c22012-11-16 18:28:11 -08002428 }
Adam Cohen482ed822012-03-02 14:15:13 -08002429 }
2430
Adam Cohendf035382011-04-11 17:22:04 -07002431 /**
Adam Cohendf035382011-04-11 17:22:04 -07002432 * Find a starting cell position that will fit the given bounds nearest the requested
2433 * cell location. Uses Euclidean distance to score multiple vacant areas.
2434 *
2435 * @param pixelX The X location at which you want to search for a vacant area.
2436 * @param pixelY The Y location at which you want to search for a vacant area.
2437 * @param spanX Horizontal span of the object.
2438 * @param spanY Vertical span of the object.
Adam Cohendf035382011-04-11 17:22:04 -07002439 * @param result Previously returned value to possibly recycle.
2440 * @return The X, Y cell of a vacant area that can contain this object,
2441 * nearest the requested location.
2442 */
Adam Cohenf9c184a2016-01-15 16:47:43 -08002443 public int[] findNearestArea(int pixelX, int pixelY, int spanX, int spanY, int[] result) {
Sunny Goyalf7a29e82015-04-24 15:20:43 -07002444 return findNearestArea(pixelX, pixelY, spanX, spanY, spanX, spanY, false, result, null);
Adam Cohendf035382011-04-11 17:22:04 -07002445 }
2446
Michael Jurka0280c3b2010-09-17 15:00:07 -07002447 boolean existsEmptyCell() {
2448 return findCellForSpan(null, 1, 1);
2449 }
2450
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002451 /**
Michael Jurka0280c3b2010-09-17 15:00:07 -07002452 * Finds the upper-left coordinate of the first rectangle in the grid that can
2453 * hold a cell of the specified dimensions. If intersectX and intersectY are not -1,
2454 * then this method will only return coordinates for rectangles that contain the cell
2455 * (intersectX, intersectY)
2456 *
2457 * @param cellXY The array that will contain the position of a vacant cell if such a cell
2458 * can be found.
2459 * @param spanX The horizontal span of the cell we want to find.
2460 * @param spanY The vertical span of the cell we want to find.
2461 *
2462 * @return True if a vacant cell of the specified dimension was found, false otherwise.
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07002463 */
Hyunyoung Song3f471442015-04-08 19:01:34 -07002464 public boolean findCellForSpan(int[] cellXY, int spanX, int spanY) {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07002465 if (cellXY == null) {
2466 cellXY = new int[2];
Michael Jurka0280c3b2010-09-17 15:00:07 -07002467 }
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07002468 return mOccupied.findVacantCell(cellXY, spanX, spanY);
Michael Jurka0280c3b2010-09-17 15:00:07 -07002469 }
2470
2471 /**
Winson Chungc07918d2011-07-01 15:35:26 -07002472 * A drag event has begun over this layout.
2473 * It may have begun over this layout (in which case onDragChild is called first),
2474 * or it may have begun on another layout.
2475 */
2476 void onDragEnter() {
Winson Chungc07918d2011-07-01 15:35:26 -07002477 mDragging = true;
2478 }
2479
2480 /**
Michael Jurka0280c3b2010-09-17 15:00:07 -07002481 * Called when drag has left this CellLayout or has been completed (successfully or not)
2482 */
2483 void onDragExit() {
Joe Onorato4be866d2010-10-10 11:26:02 -07002484 // This can actually be called when we aren't in a drag, e.g. when adding a new
2485 // item to this layout via the customize drawer.
2486 // Guard against that case.
2487 if (mDragging) {
2488 mDragging = false;
Patrick Dubroyde7658b2010-09-27 11:15:43 -07002489 }
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -07002490
2491 // Invalidate the drag data
Adam Cohend41fbf52012-02-16 23:53:59 -08002492 mDragCell[0] = mDragCell[1] = -1;
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -07002493 mDragOutlineAnims[mDragOutlineCurrent].animateOut();
2494 mDragOutlineCurrent = (mDragOutlineCurrent + 1) % mDragOutlineAnims.length;
Adam Cohen19f37922012-03-21 11:59:11 -07002495 revertTempState();
Michael Jurka33945b22010-12-21 18:19:38 -08002496 setIsDragOverlapping(false);
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07002497 }
2498
2499 /**
Winson Chungaafa03c2010-06-11 17:34:16 -07002500 * Mark a child as having been dropped.
Patrick Dubroyde7658b2010-09-27 11:15:43 -07002501 * At the beginning of the drag operation, the child may have been on another
Patrick Dubroyce34a972010-10-19 10:34:32 -07002502 * screen, but it is re-parented before this method is called.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002503 *
2504 * @param child The child that is being dropped
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002505 */
Adam Cohen716b51e2011-06-30 12:09:54 -07002506 void onDropChild(View child) {
Romain Guyd94533d2009-08-17 10:01:15 -07002507 if (child != null) {
2508 LayoutParams lp = (LayoutParams) child.getLayoutParams();
Romain Guy84f296c2009-11-04 15:00:44 -08002509 lp.dropped = true;
Romain Guyd94533d2009-08-17 10:01:15 -07002510 child.requestLayout();
Tony Wickham1cdb6d02015-09-17 11:08:27 -07002511 markCellsAsOccupiedForView(child);
Romain Guyd94533d2009-08-17 10:01:15 -07002512 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002513 }
2514
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002515 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002516 * Computes a bounding rectangle for a range of cells
Winson Chungaafa03c2010-06-11 17:34:16 -07002517 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002518 * @param cellX X coordinate of upper left corner expressed as a cell position
2519 * @param cellY Y coordinate of upper left corner expressed as a cell position
Winson Chungaafa03c2010-06-11 17:34:16 -07002520 * @param cellHSpan Width in cells
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002521 * @param cellVSpan Height in cells
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07002522 * @param resultRect Rect into which to put the results
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002523 */
Adam Cohend41fbf52012-02-16 23:53:59 -08002524 public void cellToRect(int cellX, int cellY, int cellHSpan, int cellVSpan, Rect resultRect) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002525 final int cellWidth = mCellWidth;
2526 final int cellHeight = mCellHeight;
Winson Chungaafa03c2010-06-11 17:34:16 -07002527
Winson Chung4b825dcd2011-06-19 12:41:22 -07002528 final int hStartPadding = getPaddingLeft();
2529 final int vStartPadding = getPaddingTop();
Winson Chungaafa03c2010-06-11 17:34:16 -07002530
Sunny Goyalaa8a8712016-11-20 15:26:01 +05302531 int width = cellHSpan * cellWidth;
2532 int height = cellVSpan * cellHeight;
2533 int x = hStartPadding + cellX * cellWidth;
2534 int y = vStartPadding + cellY * cellHeight;
Winson Chungaafa03c2010-06-11 17:34:16 -07002535
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07002536 resultRect.set(x, y, x + width, y + height);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002537 }
Winson Chungaafa03c2010-06-11 17:34:16 -07002538
Adam Cohend4844c32011-02-18 19:25:06 -08002539 public void markCellsAsOccupiedForView(View view) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07002540 if (view == null || view.getParent() != mShortcutsAndWidgets) return;
Michael Jurka0280c3b2010-09-17 15:00:07 -07002541 LayoutParams lp = (LayoutParams) view.getLayoutParams();
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07002542 mOccupied.markCells(lp.cellX, lp.cellY, lp.cellHSpan, lp.cellVSpan, true);
Michael Jurka0280c3b2010-09-17 15:00:07 -07002543 }
2544
Adam Cohend4844c32011-02-18 19:25:06 -08002545 public void markCellsAsUnoccupiedForView(View view) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07002546 if (view == null || view.getParent() != mShortcutsAndWidgets) return;
Michael Jurka0280c3b2010-09-17 15:00:07 -07002547 LayoutParams lp = (LayoutParams) view.getLayoutParams();
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07002548 mOccupied.markCells(lp.cellX, lp.cellY, lp.cellHSpan, lp.cellVSpan, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002549 }
2550
Adam Cohen2801caf2011-05-13 20:57:39 -07002551 public int getDesiredWidth() {
Sunny Goyalaa8a8712016-11-20 15:26:01 +05302552 return getPaddingLeft() + getPaddingRight() + (mCountX * mCellWidth);
Adam Cohen2801caf2011-05-13 20:57:39 -07002553 }
2554
2555 public int getDesiredHeight() {
Sunny Goyalaa8a8712016-11-20 15:26:01 +05302556 return getPaddingTop() + getPaddingBottom() + (mCountY * mCellHeight);
Adam Cohen2801caf2011-05-13 20:57:39 -07002557 }
2558
Michael Jurka66d72172011-04-12 16:29:25 -07002559 public boolean isOccupied(int x, int y) {
2560 if (x < mCountX && y < mCountY) {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07002561 return mOccupied.cells[x][y];
Michael Jurka66d72172011-04-12 16:29:25 -07002562 } else {
2563 throw new RuntimeException("Position exceeds the bound of this CellLayout");
2564 }
2565 }
2566
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002567 @Override
2568 public ViewGroup.LayoutParams generateLayoutParams(AttributeSet attrs) {
2569 return new CellLayout.LayoutParams(getContext(), attrs);
2570 }
2571
2572 @Override
2573 protected boolean checkLayoutParams(ViewGroup.LayoutParams p) {
2574 return p instanceof CellLayout.LayoutParams;
2575 }
2576
2577 @Override
2578 protected ViewGroup.LayoutParams generateLayoutParams(ViewGroup.LayoutParams p) {
2579 return new CellLayout.LayoutParams(p);
2580 }
2581
2582 public static class LayoutParams extends ViewGroup.MarginLayoutParams {
2583 /**
2584 * Horizontal location of the item in the grid.
2585 */
2586 @ViewDebug.ExportedProperty
2587 public int cellX;
2588
2589 /**
2590 * Vertical location of the item in the grid.
2591 */
2592 @ViewDebug.ExportedProperty
2593 public int cellY;
2594
2595 /**
Adam Cohen482ed822012-03-02 14:15:13 -08002596 * Temporary horizontal location of the item in the grid during reorder
2597 */
2598 public int tmpCellX;
2599
2600 /**
2601 * Temporary vertical location of the item in the grid during reorder
2602 */
2603 public int tmpCellY;
2604
2605 /**
2606 * Indicates that the temporary coordinates should be used to layout the items
2607 */
2608 public boolean useTmpCoords;
2609
2610 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002611 * Number of cells spanned horizontally by the item.
2612 */
2613 @ViewDebug.ExportedProperty
2614 public int cellHSpan;
2615
2616 /**
2617 * Number of cells spanned vertically by the item.
2618 */
2619 @ViewDebug.ExportedProperty
2620 public int cellVSpan;
Winson Chungaafa03c2010-06-11 17:34:16 -07002621
Adam Cohen1b607ed2011-03-03 17:26:50 -08002622 /**
2623 * Indicates whether the item will set its x, y, width and height parameters freely,
2624 * or whether these will be computed based on cellX, cellY, cellHSpan and cellVSpan.
2625 */
Adam Cohend4844c32011-02-18 19:25:06 -08002626 public boolean isLockedToGrid = true;
2627
Adam Cohen482ed822012-03-02 14:15:13 -08002628 /**
2629 * Indicates whether this item can be reordered. Always true except in the case of the
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07002630 * the AllApps button and QSB place holder.
Adam Cohen482ed822012-03-02 14:15:13 -08002631 */
2632 public boolean canReorder = true;
2633
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002634 // X coordinate of the view in the layout.
2635 @ViewDebug.ExportedProperty
Vadim Tryshevfedca432015-08-19 17:55:02 -07002636 public int x;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002637 // Y coordinate of the view in the layout.
2638 @ViewDebug.ExportedProperty
Vadim Tryshevfedca432015-08-19 17:55:02 -07002639 public int y;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002640
Romain Guy84f296c2009-11-04 15:00:44 -08002641 boolean dropped;
Romain Guyfcb9e712009-10-02 16:06:52 -07002642
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002643 public LayoutParams(Context c, AttributeSet attrs) {
2644 super(c, attrs);
2645 cellHSpan = 1;
2646 cellVSpan = 1;
2647 }
2648
2649 public LayoutParams(ViewGroup.LayoutParams source) {
2650 super(source);
2651 cellHSpan = 1;
2652 cellVSpan = 1;
2653 }
Winson Chungaafa03c2010-06-11 17:34:16 -07002654
2655 public LayoutParams(LayoutParams source) {
2656 super(source);
2657 this.cellX = source.cellX;
2658 this.cellY = source.cellY;
2659 this.cellHSpan = source.cellHSpan;
2660 this.cellVSpan = source.cellVSpan;
2661 }
2662
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002663 public LayoutParams(int cellX, int cellY, int cellHSpan, int cellVSpan) {
Romain Guy8f19cdd2010-01-08 15:07:00 -08002664 super(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002665 this.cellX = cellX;
2666 this.cellY = cellY;
2667 this.cellHSpan = cellHSpan;
2668 this.cellVSpan = cellVSpan;
2669 }
2670
Sunny Goyalaa8a8712016-11-20 15:26:01 +05302671 public void setup(int cellWidth, int cellHeight, boolean invertHorizontally, int colCount) {
Jon Miranda7ae64ff2016-11-21 16:18:46 -08002672 setup(cellWidth, cellHeight, invertHorizontally, colCount, 1.0f, 1.0f);
2673 }
2674
2675 /**
2676 * Use this method, as opposed to {@link #setup(int, int, boolean, int)}, if the view needs
2677 * to be scaled.
2678 *
2679 * ie. In multi-window mode, we setup widgets so that they are measured and laid out
2680 * using their full/invariant device profile sizes.
2681 */
2682 public void setup(int cellWidth, int cellHeight, boolean invertHorizontally, int colCount,
2683 float cellScaleX, float cellScaleY) {
Adam Cohend4844c32011-02-18 19:25:06 -08002684 if (isLockedToGrid) {
2685 final int myCellHSpan = cellHSpan;
2686 final int myCellVSpan = cellVSpan;
Adam Cohen2374abf2013-04-16 14:56:57 -07002687 int myCellX = useTmpCoords ? tmpCellX : cellX;
2688 int myCellY = useTmpCoords ? tmpCellY : cellY;
2689
2690 if (invertHorizontally) {
2691 myCellX = colCount - myCellX - cellHSpan;
2692 }
Adam Cohen1b607ed2011-03-03 17:26:50 -08002693
Jon Miranda7ae64ff2016-11-21 16:18:46 -08002694 width = (int) (myCellHSpan * cellWidth / cellScaleX - leftMargin - rightMargin);
2695 height = (int) (myCellVSpan * cellHeight / cellScaleY - topMargin - bottomMargin);
Sunny Goyalaa8a8712016-11-20 15:26:01 +05302696 x = (myCellX * cellWidth + leftMargin);
2697 y = (myCellY * cellHeight + topMargin);
Adam Cohend4844c32011-02-18 19:25:06 -08002698 }
2699 }
Winson Chungaafa03c2010-06-11 17:34:16 -07002700
Winson Chungaafa03c2010-06-11 17:34:16 -07002701 public String toString() {
2702 return "(" + this.cellX + ", " + this.cellY + ")";
2703 }
Adam Cohen7f4eabe2011-04-21 16:19:16 -07002704
2705 public void setWidth(int width) {
2706 this.width = width;
2707 }
2708
2709 public int getWidth() {
2710 return width;
2711 }
2712
2713 public void setHeight(int height) {
2714 this.height = height;
2715 }
2716
2717 public int getHeight() {
2718 return height;
2719 }
2720
2721 public void setX(int x) {
2722 this.x = x;
2723 }
2724
2725 public int getX() {
2726 return x;
2727 }
2728
2729 public void setY(int y) {
2730 this.y = y;
2731 }
2732
2733 public int getY() {
2734 return y;
2735 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002736 }
2737
Michael Jurka0280c3b2010-09-17 15:00:07 -07002738 // This class stores info for two purposes:
2739 // 1. When dragging items (mDragInfo in Workspace), we store the View, its cellX & cellY,
2740 // its spanX, spanY, and the screen it is on
2741 // 2. When long clicking on an empty cell in a CellLayout, we save information about the
2742 // cellX and cellY coordinates and which page was clicked. We then set this as a tag on
2743 // the CellLayout that was long clicked
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07002744 public static final class CellInfo extends CellAndSpan {
Rajeev Kumar9962dbe2017-06-12 12:16:20 -07002745 public final View cell;
2746 final long screenId;
2747 final long container;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002748
Sunny Goyal83a8f042015-05-19 12:52:12 -07002749 public CellInfo(View v, ItemInfo info) {
Adam Cohene0aaa0d2014-05-12 12:44:22 -07002750 cellX = info.cellX;
2751 cellY = info.cellY;
2752 spanX = info.spanX;
2753 spanY = info.spanY;
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07002754 cell = v;
Adam Cohene0aaa0d2014-05-12 12:44:22 -07002755 screenId = info.screenId;
2756 container = info.container;
2757 }
2758
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002759 @Override
2760 public String toString() {
Winson Chungaafa03c2010-06-11 17:34:16 -07002761 return "Cell[view=" + (cell == null ? "null" : cell.getClass())
2762 + ", x=" + cellX + ", y=" + cellY + "]";
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002763 }
2764 }
Michael Jurkad771c962011-08-09 15:00:48 -07002765
Tony Wickham86930612015-09-09 13:50:40 -07002766 /**
2767 * Returns whether an item can be placed in this CellLayout (after rearranging and/or resizing
2768 * if necessary).
2769 */
2770 public boolean hasReorderSolution(ItemInfo itemInfo) {
2771 int[] cellPoint = new int[2];
2772 // Check for a solution starting at every cell.
2773 for (int cellX = 0; cellX < getCountX(); cellX++) {
2774 for (int cellY = 0; cellY < getCountY(); cellY++) {
2775 cellToPoint(cellX, cellY, cellPoint);
2776 if (findReorderSolution(cellPoint[0], cellPoint[1], itemInfo.minSpanX,
2777 itemInfo.minSpanY, itemInfo.spanX, itemInfo.spanY, mDirectionVector, null,
2778 true, new ItemConfiguration()).isSolution) {
2779 return true;
2780 }
2781 }
2782 }
2783 return false;
2784 }
2785
Sunny Goyal9ca9c132015-04-29 14:57:22 -07002786 public boolean isRegionVacant(int x, int y, int spanX, int spanY) {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07002787 return mOccupied.isRegionVacant(x, y, spanX, spanY);
Sunny Goyal9ca9c132015-04-29 14:57:22 -07002788 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002789}