blob: c2c5c27db1a68fcf767bd63579b6c44116105081 [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;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080038import android.util.AttributeSet;
Joe Onorato4be866d2010-10-10 11:26:02 -070039import android.util.Log;
Adam Cohen1462de32012-07-24 22:34:36 -070040import android.util.SparseArray;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080041import android.view.MotionEvent;
42import android.view.View;
43import android.view.ViewDebug;
44import android.view.ViewGroup;
Adam Cohenc9735cf2015-01-23 16:11:55 -080045import android.view.accessibility.AccessibilityEvent;
Winson Chung150fbab2010-09-29 17:14:26 -070046import android.view.animation.DecelerateInterpolator;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080047
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;
Sunny Goyal06e21a22016-08-11 16:02:02 -070056import com.android.launcher3.graphics.DragPreviewProvider;
Sunny Goyalff4ba2d2016-04-02 14:12:34 -070057import com.android.launcher3.util.CellAndSpan;
58import com.android.launcher3.util.GridOccupancy;
Sunny Goyale2fd14b2015-08-27 17:45:46 -070059import com.android.launcher3.util.ParcelableSparseArray;
Sunny Goyal9b29ca52017-02-17 10:39:44 -080060import com.android.launcher3.util.Themes;
Adam Cohen091440a2015-03-18 14:16:05 -070061import com.android.launcher3.util.Thunk;
Patrick Dubroy8e58e912010-10-14 13:21:48 -070062
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 Cohenbfbfd262011-06-13 16:55:12 -070069import java.util.HashMap;
Adam Cohend41fbf52012-02-16 23:53:59 -080070import java.util.Stack;
Adam Cohenc0dcf592011-06-01 15:30:43 -070071
Sunny Goyal4b6eb262015-05-14 19:24:40 -070072public class CellLayout extends ViewGroup implements BubbleTextShadowHandler {
Sunny Goyale9b651e2015-04-24 11:44:51 -070073 public static final int WORKSPACE_ACCESSIBILITY_DRAG = 2;
74 public static final int FOLDER_ACCESSIBILITY_DRAG = 1;
75
Tony Wickhama0628cc2015-10-14 15:23:04 -070076 private static final String TAG = "CellLayout";
77 private static final boolean LOGD = false;
Winson Chungaafa03c2010-06-11 17:34:16 -070078
Adam Cohen2acce882012-03-28 19:03:19 -070079 private Launcher mLauncher;
Sunny Goyal4ffec482016-02-09 11:28:52 -080080 @ViewDebug.ExportedProperty(category = "launcher")
Adam Cohen091440a2015-03-18 14:16:05 -070081 @Thunk int mCellWidth;
Sunny Goyal4ffec482016-02-09 11:28:52 -080082 @ViewDebug.ExportedProperty(category = "launcher")
Adam Cohen091440a2015-03-18 14:16:05 -070083 @Thunk int mCellHeight;
Winson Chung11a1a532013-09-13 11:14:45 -070084 private int mFixedCellWidth;
85 private int mFixedCellHeight;
Winson Chungaafa03c2010-06-11 17:34:16 -070086
Sunny Goyal4ffec482016-02-09 11:28:52 -080087 @ViewDebug.ExportedProperty(category = "launcher")
Sunny Goyalff4ba2d2016-04-02 14:12:34 -070088 private int mCountX;
Sunny Goyal4ffec482016-02-09 11:28:52 -080089 @ViewDebug.ExportedProperty(category = "launcher")
Sunny Goyalff4ba2d2016-04-02 14:12:34 -070090 private int mCountY;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080091
Adam Cohen917e3882013-10-31 15:03:35 -070092 private boolean mDropPending = false;
Adam Cohenc50438c2014-08-19 17:43:05 -070093 private boolean mIsDragTarget = true;
Sunny Goyale2fd14b2015-08-27 17:45:46 -070094 private boolean mJailContent = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080095
Patrick Dubroyde7658b2010-09-27 11:15:43 -070096 // These are temporary variables to prevent having to allocate a new object just to
97 // return an (x, y) value from helper functions. Do NOT use them to maintain other state.
Adam Cohen091440a2015-03-18 14:16:05 -070098 @Thunk final int[] mTmpPoint = new int[2];
Sunny Goyal2805e632015-05-20 15:35:32 -070099 @Thunk final int[] mTempLocation = new int[2];
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700100
Sunny Goyalff4ba2d2016-04-02 14:12:34 -0700101 private GridOccupancy mOccupied;
102 private GridOccupancy mTmpOccupied;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800103
Michael Jurkadee05892010-07-27 10:01:56 -0700104 private OnTouchListener mInterceptTouchListener;
Mady Melloref044dd2015-06-02 15:35:07 -0700105 private StylusEventHelper mStylusEventHelper;
Michael Jurkadee05892010-07-27 10:01:56 -0700106
Adam Cohenefca0272016-02-24 19:19:06 -0800107 private ArrayList<FolderIcon.PreviewBackground> mFolderBackgrounds = new ArrayList<FolderIcon.PreviewBackground>();
108 FolderIcon.PreviewBackground mFolderLeaveBehind = new FolderIcon.PreviewBackground();
Adam Cohen69ce2e52011-07-03 19:25:21 -0700109
Michael Jurka5f1c5092010-09-03 14:15:02 -0700110 private float mBackgroundAlpha;
Adam Cohenf34bab52010-09-30 14:11:56 -0700111
Sunny Goyalf5440cb2016-12-14 15:13:00 -0800112 private static final int[] BACKGROUND_STATE_ACTIVE = new int[] { android.R.attr.state_active };
113 private static final int[] BACKGROUND_STATE_DEFAULT = new int[0];
114 private final Drawable mBackground;
Sunny Goyal2805e632015-05-20 15:35:32 -0700115
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700116 // These values allow a fixed measurement to be set on the CellLayout.
117 private int mFixedWidth = -1;
118 private int mFixedHeight = -1;
119
Michael Jurka33945b22010-12-21 18:19:38 -0800120 // If we're actively dragging something over this screen, mIsDragOverlapping is true
121 private boolean mIsDragOverlapping = false;
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700122
Winson Chung150fbab2010-09-29 17:14:26 -0700123 // These arrays are used to implement the drag visualization on x-large screens.
Joe Onorato4be866d2010-10-10 11:26:02 -0700124 // They are used as circular arrays, indexed by mDragOutlineCurrent.
Adam Cohen091440a2015-03-18 14:16:05 -0700125 @Thunk Rect[] mDragOutlines = new Rect[4];
126 @Thunk float[] mDragOutlineAlphas = new float[mDragOutlines.length];
Joe Onorato4be866d2010-10-10 11:26:02 -0700127 private InterruptibleInOutAnimator[] mDragOutlineAnims =
128 new InterruptibleInOutAnimator[mDragOutlines.length];
Winson Chung150fbab2010-09-29 17:14:26 -0700129
130 // Used as an index into the above 3 arrays; indicates which is the most current value.
Joe Onorato4be866d2010-10-10 11:26:02 -0700131 private int mDragOutlineCurrent = 0;
Patrick Dubroy8e58e912010-10-14 13:21:48 -0700132 private final Paint mDragOutlinePaint = new Paint();
Winson Chung150fbab2010-09-29 17:14:26 -0700133
Sunny Goyal4fe5a372015-05-14 19:55:10 -0700134 private final ClickShadowView mTouchFeedbackView;
Patrick Dubroy96864c32011-03-10 17:17:23 -0800135
Sunny Goyal316490e2015-06-02 09:38:28 -0700136 @Thunk HashMap<CellLayout.LayoutParams, Animator> mReorderAnimators = new HashMap<>();
137 @Thunk HashMap<View, ReorderPreviewAnimation> mShakeAnimators = new HashMap<>();
Adam Cohen19f37922012-03-21 11:59:11 -0700138
139 private boolean mItemPlacementDirty = false;
Adam Cohenbfbfd262011-06-13 16:55:12 -0700140
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700141 // When a drag operation is in progress, holds the nearest cell to the touch point
142 private final int[] mDragCell = new int[2];
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800143
Joe Onorato4be866d2010-10-10 11:26:02 -0700144 private boolean mDragging = false;
145
Patrick Dubroyce34a972010-10-19 10:34:32 -0700146 private TimeInterpolator mEaseOutInterpolator;
Michael Jurkaa52570f2012-03-20 03:18:20 -0700147 private ShortcutAndWidgetContainer mShortcutsAndWidgets;
Patrick Dubroyce34a972010-10-19 10:34:32 -0700148
Sunny Goyalc13403c2016-11-18 23:44:48 -0800149 @Retention(RetentionPolicy.SOURCE)
150 @IntDef({WORKSPACE, HOTSEAT, FOLDER})
151 public @interface ContainerType{}
152 public static final int WORKSPACE = 0;
153 public static final int HOTSEAT = 1;
154 public static final int FOLDER = 2;
155
156 @ContainerType private final int mContainerType;
157
158 private final float mChildScale;
Andrew Flynn0dca1ec2012-02-29 13:33:22 -0800159
Adam Cohenfa3c58f2013-12-06 16:10:55 -0800160 public static final int MODE_SHOW_REORDER_HINT = 0;
161 public static final int MODE_DRAG_OVER = 1;
162 public static final int MODE_ON_DROP = 2;
163 public static final int MODE_ON_DROP_EXTERNAL = 3;
164 public static final int MODE_ACCEPT_DROP = 4;
Adam Cohen19f37922012-03-21 11:59:11 -0700165 private static final boolean DESTRUCTIVE_REORDER = false;
Adam Cohen482ed822012-03-02 14:15:13 -0800166 private static final boolean DEBUG_VISUALIZE_OCCUPIED = false;
167
Adam Cohenfa3c58f2013-12-06 16:10:55 -0800168 private static final float REORDER_PREVIEW_MAGNITUDE = 0.12f;
Adam Cohen19f37922012-03-21 11:59:11 -0700169 private static final int REORDER_ANIMATION_DURATION = 150;
Sunny Goyalc13403c2016-11-18 23:44:48 -0800170 @Thunk final float mReorderPreviewAnimationMagnitude;
Adam Cohen19f37922012-03-21 11:59:11 -0700171
Adam Cohen482ed822012-03-02 14:15:13 -0800172 private ArrayList<View> mIntersectingViews = new ArrayList<View>();
173 private Rect mOccupiedRect = new Rect();
174 private int[] mDirectionVector = new int[2];
Adam Cohen19f37922012-03-21 11:59:11 -0700175 int[] mPreviousReorderDirection = new int[2];
Adam Cohenb209e632012-03-27 17:09:36 -0700176 private static final int INVALID_DIRECTION = -100;
Adam Cohen482ed822012-03-02 14:15:13 -0800177
Sunny Goyal2805e632015-05-20 15:35:32 -0700178 private final Rect mTempRect = new Rect();
Winson Chung3a6e7f32013-10-09 15:50:52 -0700179
Michael Jurkaca993832012-06-29 15:17:04 -0700180 private final static Paint sPaint = new Paint();
Romain Guy8a0bff52012-05-06 13:14:33 -0700181
Adam Cohenc9735cf2015-01-23 16:11:55 -0800182 // Related to accessible drag and drop
Sunny Goyale9b651e2015-04-24 11:44:51 -0700183 private DragAndDropAccessibilityDelegate mTouchHelper;
Adam Cohenc9735cf2015-01-23 16:11:55 -0800184 private boolean mUseTouchHelper = false;
Adam Cohenc9735cf2015-01-23 16:11:55 -0800185
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800186 public CellLayout(Context context) {
187 this(context, null);
188 }
189
190 public CellLayout(Context context, AttributeSet attrs) {
191 this(context, attrs, 0);
192 }
193
194 public CellLayout(Context context, AttributeSet attrs, int defStyle) {
195 super(context, attrs, defStyle);
Sunny Goyalc13403c2016-11-18 23:44:48 -0800196 TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.CellLayout, defStyle, 0);
197 mContainerType = a.getInteger(R.styleable.CellLayout_containerType, WORKSPACE);
198 a.recycle();
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700199
200 // A ViewGroup usually does not draw, but CellLayout needs to draw a rectangle to show
201 // the user where a dragged item will land when dropped.
202 setWillNotDraw(false);
Romain Guyce3cbd12013-02-25 15:00:36 -0800203 setClipToPadding(false);
Tony2fd02082016-10-07 12:50:01 -0700204 mLauncher = Launcher.getLauncher(context);
Michael Jurkaa63c4522010-08-19 13:52:27 -0700205
Adam Cohen2e6da152015-05-06 11:42:25 -0700206 DeviceProfile grid = mLauncher.getDeviceProfile();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800207
Winson Chung11a1a532013-09-13 11:14:45 -0700208 mCellWidth = mCellHeight = -1;
Nilesh Agrawal5f7099a2014-01-02 15:54:57 -0800209 mFixedCellWidth = mFixedCellHeight = -1;
Sunny Goyalff4ba2d2016-04-02 14:12:34 -0700210
211 mCountX = grid.inv.numColumns;
212 mCountY = grid.inv.numRows;
213 mOccupied = new GridOccupancy(mCountX, mCountY);
214 mTmpOccupied = new GridOccupancy(mCountX, mCountY);
215
Adam Cohen5b53f292012-03-29 14:30:35 -0700216 mPreviousReorderDirection[0] = INVALID_DIRECTION;
217 mPreviousReorderDirection[1] = INVALID_DIRECTION;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800218
Adam Cohenefca0272016-02-24 19:19:06 -0800219 mFolderLeaveBehind.delegateCellX = -1;
220 mFolderLeaveBehind.delegateCellY = -1;
221
Sunny Goyalc13403c2016-11-18 23:44:48 -0800222 mChildScale = mContainerType == HOTSEAT ? grid.inv.hotseatScale : 1f;
223
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800224 setAlwaysDrawnWithCacheEnabled(false);
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700225 final Resources res = getResources();
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700226
Sunny Goyalf5440cb2016-12-14 15:13:00 -0800227 mBackground = res.getDrawable(R.drawable.bg_celllayout);
Sunny Goyal2805e632015-05-20 15:35:32 -0700228 mBackground.setCallback(this);
Winson Chunge8f1d042015-07-31 12:39:57 -0700229 mBackground.setAlpha((int) (mBackgroundAlpha * 255));
Michael Jurka33945b22010-12-21 18:19:38 -0800230
Sunny Goyalc13403c2016-11-18 23:44:48 -0800231 mReorderPreviewAnimationMagnitude = (REORDER_PREVIEW_MAGNITUDE * grid.iconSizePx);
Adam Cohen19f37922012-03-21 11:59:11 -0700232
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700233 // Initialize the data structures used for the drag visualization.
Patrick Dubroyce34a972010-10-19 10:34:32 -0700234 mEaseOutInterpolator = new DecelerateInterpolator(2.5f); // Quint ease out
Winson Chungb8c69f32011-10-19 21:36:08 -0700235 mDragCell[0] = mDragCell[1] = -1;
Joe Onorato4be866d2010-10-10 11:26:02 -0700236 for (int i = 0; i < mDragOutlines.length; i++) {
Adam Cohend41fbf52012-02-16 23:53:59 -0800237 mDragOutlines[i] = new Rect(-1, -1, -1, -1);
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700238 }
Mario Bertschler54ba6012017-06-08 10:53:53 -0700239 mDragOutlinePaint.setColor(Themes.getAttrColor(context, R.attr.workspaceTextColor));
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700240
241 // When dragging things around the home screens, we show a green outline of
242 // where the item will land. The outlines gradually fade out, leaving a trail
243 // behind the drag path.
244 // Set up all the animations that are used to implement this fading.
245 final int duration = res.getInteger(R.integer.config_dragOutlineFadeTime);
Chet Haase472b2812010-10-14 07:02:04 -0700246 final float fromAlphaValue = 0;
247 final float toAlphaValue = (float)res.getInteger(R.integer.config_dragOutlineMaxAlpha);
Joe Onorato4be866d2010-10-10 11:26:02 -0700248
Patrick Dubroy8e58e912010-10-14 13:21:48 -0700249 Arrays.fill(mDragOutlineAlphas, fromAlphaValue);
Joe Onorato4be866d2010-10-10 11:26:02 -0700250
251 for (int i = 0; i < mDragOutlineAnims.length; i++) {
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700252 final InterruptibleInOutAnimator anim =
Michael Jurkaf1ad6082013-03-13 12:55:46 +0100253 new InterruptibleInOutAnimator(this, duration, fromAlphaValue, toAlphaValue);
Patrick Dubroyce34a972010-10-19 10:34:32 -0700254 anim.getAnimator().setInterpolator(mEaseOutInterpolator);
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700255 final int thisIndex = i;
Chet Haase472b2812010-10-14 07:02:04 -0700256 anim.getAnimator().addUpdateListener(new AnimatorUpdateListener() {
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700257 public void onAnimationUpdate(ValueAnimator animation) {
Joe Onorato4be866d2010-10-10 11:26:02 -0700258 final Bitmap outline = (Bitmap)anim.getTag();
259
260 // If an animation is started and then stopped very quickly, we can still
261 // get spurious updates we've cleared the tag. Guard against this.
262 if (outline == null) {
Tony Wickhama0628cc2015-10-14 15:23:04 -0700263 if (LOGD) {
Patrick Dubroyfe6bd872010-10-13 17:32:10 -0700264 Object val = animation.getAnimatedValue();
265 Log.d(TAG, "anim " + thisIndex + " update: " + val +
266 ", isStopped " + anim.isStopped());
267 }
Joe Onorato4be866d2010-10-10 11:26:02 -0700268 // Try to prevent it from continuing to run
269 animation.cancel();
270 } else {
Chet Haase472b2812010-10-14 07:02:04 -0700271 mDragOutlineAlphas[thisIndex] = (Float) animation.getAnimatedValue();
Adam Cohend41fbf52012-02-16 23:53:59 -0800272 CellLayout.this.invalidate(mDragOutlines[thisIndex]);
Joe Onorato4be866d2010-10-10 11:26:02 -0700273 }
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700274 }
275 });
Joe Onorato4be866d2010-10-10 11:26:02 -0700276 // The animation holds a reference to the drag outline bitmap as long is it's
277 // running. This way the bitmap can be GCed when the animations are complete.
Chet Haase472b2812010-10-14 07:02:04 -0700278 anim.getAnimator().addListener(new AnimatorListenerAdapter() {
Michael Jurka3c4c20f2010-10-28 15:36:06 -0700279 @Override
Joe Onorato4be866d2010-10-10 11:26:02 -0700280 public void onAnimationEnd(Animator animation) {
Chet Haase472b2812010-10-14 07:02:04 -0700281 if ((Float) ((ValueAnimator) animation).getAnimatedValue() == 0f) {
Joe Onorato4be866d2010-10-10 11:26:02 -0700282 anim.setTag(null);
283 }
284 }
285 });
286 mDragOutlineAnims[i] = anim;
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700287 }
Patrick Dubroyce34a972010-10-19 10:34:32 -0700288
Sunny Goyalc13403c2016-11-18 23:44:48 -0800289 mShortcutsAndWidgets = new ShortcutAndWidgetContainer(context, mContainerType);
Sunny Goyalaa8a8712016-11-20 15:26:01 +0530290 mShortcutsAndWidgets.setCellDimensions(mCellWidth, mCellHeight, mCountX, mCountY);
Adam Cohen2374abf2013-04-16 14:56:57 -0700291
Mady Mellorbb835202015-07-15 16:34:34 -0700292 mStylusEventHelper = new StylusEventHelper(new SimpleOnStylusPressListener(this), this);
Mady Melloref044dd2015-06-02 15:35:07 -0700293
Sunny Goyal4fe5a372015-05-14 19:55:10 -0700294 mTouchFeedbackView = new ClickShadowView(context);
295 addView(mTouchFeedbackView);
Michael Jurkaa52570f2012-03-20 03:18:20 -0700296 addView(mShortcutsAndWidgets);
Michael Jurka18014792010-10-14 09:01:34 -0700297 }
298
Sunny Goyale9b651e2015-04-24 11:44:51 -0700299 public void enableAccessibleDrag(boolean enable, int dragType) {
Adam Cohenc9735cf2015-01-23 16:11:55 -0800300 mUseTouchHelper = enable;
301 if (!enable) {
302 ViewCompat.setAccessibilityDelegate(this, null);
303 setImportantForAccessibility(IMPORTANT_FOR_ACCESSIBILITY_NO);
304 getShortcutsAndWidgets().setImportantForAccessibility(IMPORTANT_FOR_ACCESSIBILITY_NO);
305 setOnClickListener(mLauncher);
306 } else {
Sunny Goyale9b651e2015-04-24 11:44:51 -0700307 if (dragType == WORKSPACE_ACCESSIBILITY_DRAG &&
308 !(mTouchHelper instanceof WorkspaceAccessibilityHelper)) {
309 mTouchHelper = new WorkspaceAccessibilityHelper(this);
310 } else if (dragType == FOLDER_ACCESSIBILITY_DRAG &&
311 !(mTouchHelper instanceof FolderAccessibilityHelper)) {
312 mTouchHelper = new FolderAccessibilityHelper(this);
313 }
Adam Cohenc9735cf2015-01-23 16:11:55 -0800314 ViewCompat.setAccessibilityDelegate(this, mTouchHelper);
315 setImportantForAccessibility(IMPORTANT_FOR_ACCESSIBILITY_YES);
316 getShortcutsAndWidgets().setImportantForAccessibility(IMPORTANT_FOR_ACCESSIBILITY_YES);
317 setOnClickListener(mTouchHelper);
318 }
319
320 // Invalidate the accessibility hierarchy
321 if (getParent() != null) {
322 getParent().notifySubtreeAccessibilityStateChanged(
323 this, this, AccessibilityEvent.CONTENT_CHANGE_TYPE_SUBTREE);
324 }
325 }
326
327 @Override
328 public boolean dispatchHoverEvent(MotionEvent event) {
329 // Always attempt to dispatch hover events to accessibility first.
330 if (mUseTouchHelper && mTouchHelper.dispatchHoverEvent(event)) {
331 return true;
332 }
333 return super.dispatchHoverEvent(event);
334 }
335
336 @Override
Adam Cohenc9735cf2015-01-23 16:11:55 -0800337 public boolean onInterceptTouchEvent(MotionEvent ev) {
338 if (mUseTouchHelper ||
339 (mInterceptTouchListener != null && mInterceptTouchListener.onTouch(this, ev))) {
340 return true;
341 }
342 return false;
343 }
344
Mady Melloref044dd2015-06-02 15:35:07 -0700345 @Override
346 public boolean onTouchEvent(MotionEvent ev) {
347 boolean handled = super.onTouchEvent(ev);
348 // Stylus button press on a home screen should not switch between overview mode and
349 // the home screen mode, however, once in overview mode stylus button press should be
350 // enabled to allow rearranging the different home screens. So check what mode
351 // the workspace is in, and only perform stylus button presses while in overview mode.
352 if (mLauncher.mWorkspace.isInOverviewMode()
Mady Mellorbb835202015-07-15 16:34:34 -0700353 && mStylusEventHelper.onMotionEvent(ev)) {
Mady Melloref044dd2015-06-02 15:35:07 -0700354 return true;
355 }
356 return handled;
357 }
358
Chris Craik01f2d7f2013-10-01 14:41:56 -0700359 public void enableHardwareLayer(boolean hasLayer) {
360 mShortcutsAndWidgets.setLayerType(hasLayer ? LAYER_TYPE_HARDWARE : LAYER_TYPE_NONE, sPaint);
Michael Jurkad51f33a2012-06-28 15:35:26 -0700361 }
362
363 public void buildHardwareLayer() {
364 mShortcutsAndWidgets.buildLayer();
Adam Cohen2801caf2011-05-13 20:57:39 -0700365 }
366
Winson Chung5f8afe62013-08-12 16:19:28 -0700367 public void setCellDimensions(int width, int height) {
Winson Chung11a1a532013-09-13 11:14:45 -0700368 mFixedCellWidth = mCellWidth = width;
369 mFixedCellHeight = mCellHeight = height;
Sunny Goyalaa8a8712016-11-20 15:26:01 +0530370 mShortcutsAndWidgets.setCellDimensions(mCellWidth, mCellHeight, mCountX, mCountY);
Winson Chung5f8afe62013-08-12 16:19:28 -0700371 }
372
Adam Cohen2801caf2011-05-13 20:57:39 -0700373 public void setGridSize(int x, int y) {
374 mCountX = x;
375 mCountY = y;
Sunny Goyalff4ba2d2016-04-02 14:12:34 -0700376 mOccupied = new GridOccupancy(mCountX, mCountY);
377 mTmpOccupied = new GridOccupancy(mCountX, mCountY);
Adam Cohen7fbec102012-03-27 12:42:19 -0700378 mTempRectStack.clear();
Sunny Goyalaa8a8712016-11-20 15:26:01 +0530379 mShortcutsAndWidgets.setCellDimensions(mCellWidth, mCellHeight, mCountX, mCountY);
Adam Cohen76fc0852011-06-17 13:26:23 -0700380 requestLayout();
Adam Cohen2801caf2011-05-13 20:57:39 -0700381 }
382
Adam Cohen2374abf2013-04-16 14:56:57 -0700383 // Set whether or not to invert the layout horizontally if the layout is in RTL mode.
384 public void setInvertIfRtl(boolean invert) {
385 mShortcutsAndWidgets.setInvertIfRtl(invert);
386 }
387
Adam Cohen917e3882013-10-31 15:03:35 -0700388 public void setDropPending(boolean pending) {
389 mDropPending = pending;
390 }
391
392 public boolean isDropPending() {
393 return mDropPending;
394 }
395
Sunny Goyal4b6eb262015-05-14 19:24:40 -0700396 @Override
397 public void setPressedIcon(BubbleTextView icon, Bitmap background) {
Sunny Goyal508da152014-08-14 10:53:27 -0700398 if (icon == null || background == null) {
399 mTouchFeedbackView.setBitmap(null);
400 mTouchFeedbackView.animate().cancel();
401 } else {
Sunny Goyal508da152014-08-14 10:53:27 -0700402 if (mTouchFeedbackView.setBitmap(background)) {
Winsonbe9798b2016-07-20 12:55:49 -0700403 mTouchFeedbackView.alignWithIconView(icon, mShortcutsAndWidgets,
404 null /* clipAgainstView */);
Sunny Goyal4fe5a372015-05-14 19:55:10 -0700405 mTouchFeedbackView.animateShadow();
Sunny Goyal508da152014-08-14 10:53:27 -0700406 }
Patrick Dubroy96864c32011-03-10 17:17:23 -0800407 }
408 }
409
Adam Cohenc50438c2014-08-19 17:43:05 -0700410 void disableDragTarget() {
411 mIsDragTarget = false;
412 }
413
Tony Wickham0f97b782015-12-02 17:55:07 -0800414 public boolean isDragTarget() {
415 return mIsDragTarget;
416 }
417
Adam Cohenc50438c2014-08-19 17:43:05 -0700418 void setIsDragOverlapping(boolean isDragOverlapping) {
419 if (mIsDragOverlapping != isDragOverlapping) {
420 mIsDragOverlapping = isDragOverlapping;
Sunny Goyalf5440cb2016-12-14 15:13:00 -0800421 mBackground.setState(mIsDragOverlapping
422 ? BACKGROUND_STATE_ACTIVE : BACKGROUND_STATE_DEFAULT);
Adam Cohenc50438c2014-08-19 17:43:05 -0700423 invalidate();
424 }
425 }
426
Sunny Goyale2fd14b2015-08-27 17:45:46 -0700427 public void disableJailContent() {
428 mJailContent = false;
429 }
430
431 @Override
432 protected void dispatchSaveInstanceState(SparseArray<Parcelable> container) {
433 if (mJailContent) {
434 ParcelableSparseArray jail = getJailedArray(container);
435 super.dispatchSaveInstanceState(jail);
436 container.put(R.id.cell_layout_jail_id, jail);
437 } else {
438 super.dispatchSaveInstanceState(container);
439 }
440 }
441
442 @Override
443 protected void dispatchRestoreInstanceState(SparseArray<Parcelable> container) {
444 super.dispatchRestoreInstanceState(mJailContent ? getJailedArray(container) : container);
445 }
446
447 private ParcelableSparseArray getJailedArray(SparseArray<Parcelable> container) {
448 final Parcelable parcelable = container.get(R.id.cell_layout_jail_id);
449 return parcelable instanceof ParcelableSparseArray ?
450 (ParcelableSparseArray) parcelable : new ParcelableSparseArray();
451 }
452
Tony Wickham0f97b782015-12-02 17:55:07 -0800453 public boolean getIsDragOverlapping() {
454 return mIsDragOverlapping;
455 }
456
Jeff Sharkey83f111d2009-04-20 21:03:13 -0700457 @Override
Patrick Dubroy1262e362010-10-06 15:49:50 -0700458 protected void onDraw(Canvas canvas) {
Sunny Goyal05739772015-05-19 19:59:09 -0700459 if (!mIsDragTarget) {
460 return;
461 }
462
Michael Jurka3e7c7632010-10-02 16:01:03 -0700463 // When we're large, we are either drawn in a "hover" state (ie when dragging an item to
464 // a neighboring page) or with just a normal background (if backgroundAlpha > 0.0f)
465 // When we're small, we are either drawn normally or in the "accepts drops" state (during
466 // a drag). However, we also drag the mini hover background *over* one of those two
467 // backgrounds
Sunny Goyal05739772015-05-19 19:59:09 -0700468 if (mBackgroundAlpha > 0.0f) {
Sunny Goyal2805e632015-05-20 15:35:32 -0700469 mBackground.draw(canvas);
Michael Jurkaa63c4522010-08-19 13:52:27 -0700470 }
Romain Guya6abce82009-11-10 02:54:41 -0800471
Patrick Dubroy8e58e912010-10-14 13:21:48 -0700472 final Paint paint = mDragOutlinePaint;
Joe Onorato4be866d2010-10-10 11:26:02 -0700473 for (int i = 0; i < mDragOutlines.length; i++) {
Chet Haase472b2812010-10-14 07:02:04 -0700474 final float alpha = mDragOutlineAlphas[i];
Joe Onorato4be866d2010-10-10 11:26:02 -0700475 if (alpha > 0) {
Joe Onorato4be866d2010-10-10 11:26:02 -0700476 final Bitmap b = (Bitmap) mDragOutlineAnims[i].getTag();
Chet Haase472b2812010-10-14 07:02:04 -0700477 paint.setAlpha((int)(alpha + .5f));
Sunny Goyal106bf642015-07-16 12:18:06 -0700478 canvas.drawBitmap(b, null, mDragOutlines[i], paint);
Winson Chung150fbab2010-09-29 17:14:26 -0700479 }
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700480 }
Patrick Dubroy96864c32011-03-10 17:17:23 -0800481
Adam Cohen482ed822012-03-02 14:15:13 -0800482 if (DEBUG_VISUALIZE_OCCUPIED) {
483 int[] pt = new int[2];
484 ColorDrawable cd = new ColorDrawable(Color.RED);
Adam Cohene7587d22012-05-24 18:50:02 -0700485 cd.setBounds(0, 0, mCellWidth, mCellHeight);
Adam Cohen482ed822012-03-02 14:15:13 -0800486 for (int i = 0; i < mCountX; i++) {
487 for (int j = 0; j < mCountY; j++) {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -0700488 if (mOccupied.cells[i][j]) {
Adam Cohen482ed822012-03-02 14:15:13 -0800489 cellToPoint(i, j, pt);
490 canvas.save();
491 canvas.translate(pt[0], pt[1]);
492 cd.draw(canvas);
493 canvas.restore();
494 }
495 }
496 }
497 }
498
Adam Cohenefca0272016-02-24 19:19:06 -0800499 for (int i = 0; i < mFolderBackgrounds.size(); i++) {
500 FolderIcon.PreviewBackground bg = mFolderBackgrounds.get(i);
501 cellToPoint(bg.delegateCellX, bg.delegateCellY, mTempLocation);
502 canvas.save();
503 canvas.translate(mTempLocation[0], mTempLocation[1]);
Sunny Goyal19b93b72017-02-19 20:21:37 -0800504 bg.drawBackground(canvas);
Adam Cohenf172b742016-03-30 19:28:34 -0700505 if (!bg.isClipping) {
Sunny Goyal19b93b72017-02-19 20:21:37 -0800506 bg.drawBackgroundStroke(canvas);
Adam Cohenf172b742016-03-30 19:28:34 -0700507 }
Adam Cohenefca0272016-02-24 19:19:06 -0800508 canvas.restore();
Adam Cohen69ce2e52011-07-03 19:25:21 -0700509 }
Adam Cohenc51934b2011-07-26 21:07:43 -0700510
Adam Cohenefca0272016-02-24 19:19:06 -0800511 if (mFolderLeaveBehind.delegateCellX >= 0 && mFolderLeaveBehind.delegateCellY >= 0) {
512 cellToPoint(mFolderLeaveBehind.delegateCellX,
513 mFolderLeaveBehind.delegateCellY, mTempLocation);
514 canvas.save();
515 canvas.translate(mTempLocation[0], mTempLocation[1]);
Sunny Goyal19b93b72017-02-19 20:21:37 -0800516 mFolderLeaveBehind.drawLeaveBehind(canvas);
Adam Cohenefca0272016-02-24 19:19:06 -0800517 canvas.restore();
Adam Cohenc51934b2011-07-26 21:07:43 -0700518 }
Adam Cohen69ce2e52011-07-03 19:25:21 -0700519 }
520
Adam Cohenefca0272016-02-24 19:19:06 -0800521 @Override
522 protected void dispatchDraw(Canvas canvas) {
523 super.dispatchDraw(canvas);
524
525 for (int i = 0; i < mFolderBackgrounds.size(); i++) {
526 FolderIcon.PreviewBackground bg = mFolderBackgrounds.get(i);
Adam Cohenf172b742016-03-30 19:28:34 -0700527 if (bg.isClipping) {
528 cellToPoint(bg.delegateCellX, bg.delegateCellY, mTempLocation);
529 canvas.save();
530 canvas.translate(mTempLocation[0], mTempLocation[1]);
Sunny Goyal19b93b72017-02-19 20:21:37 -0800531 bg.drawBackgroundStroke(canvas);
Adam Cohenf172b742016-03-30 19:28:34 -0700532 canvas.restore();
533 }
Adam Cohenefca0272016-02-24 19:19:06 -0800534 }
Adam Cohen69ce2e52011-07-03 19:25:21 -0700535 }
536
Adam Cohenefca0272016-02-24 19:19:06 -0800537 public void addFolderBackground(FolderIcon.PreviewBackground bg) {
538 mFolderBackgrounds.add(bg);
539 }
540 public void removeFolderBackground(FolderIcon.PreviewBackground bg) {
541 mFolderBackgrounds.remove(bg);
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700542 }
543
Adam Cohenc51934b2011-07-26 21:07:43 -0700544 public void setFolderLeaveBehindCell(int x, int y) {
Adam Cohenefca0272016-02-24 19:19:06 -0800545 View child = getChildAt(x, y);
Sunny Goyal368ae772017-05-24 13:19:15 -0700546 mFolderLeaveBehind.setup(mLauncher, null,
Adam Cohenefca0272016-02-24 19:19:06 -0800547 child.getMeasuredWidth(), child.getPaddingTop());
548
549 mFolderLeaveBehind.delegateCellX = x;
550 mFolderLeaveBehind.delegateCellY = y;
Adam Cohenc51934b2011-07-26 21:07:43 -0700551 invalidate();
552 }
553
554 public void clearFolderLeaveBehind() {
Adam Cohenefca0272016-02-24 19:19:06 -0800555 mFolderLeaveBehind.delegateCellX = -1;
556 mFolderLeaveBehind.delegateCellY = -1;
Adam Cohenc51934b2011-07-26 21:07:43 -0700557 invalidate();
558 }
559
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700560 @Override
Michael Jurkae6235dd2011-10-04 15:02:05 -0700561 public boolean shouldDelayChildPressedState() {
562 return false;
563 }
564
Adam Cohen1462de32012-07-24 22:34:36 -0700565 public void restoreInstanceState(SparseArray<Parcelable> states) {
Sunny Goyal33a152f2014-07-22 12:13:14 -0700566 try {
567 dispatchRestoreInstanceState(states);
568 } catch (IllegalArgumentException ex) {
Sunny Goyal3d706ad2017-03-06 16:56:39 -0800569 if (FeatureFlags.IS_DOGFOOD_BUILD) {
Sunny Goyal33a152f2014-07-22 12:13:14 -0700570 throw ex;
571 }
572 // Mismatched viewId / viewType preventing restore. Skip restore on production builds.
573 Log.e(TAG, "Ignoring an error while restoring a view instance state", ex);
574 }
Adam Cohen1462de32012-07-24 22:34:36 -0700575 }
576
Michael Jurkae6235dd2011-10-04 15:02:05 -0700577 @Override
Jeff Sharkey83f111d2009-04-20 21:03:13 -0700578 public void cancelLongPress() {
579 super.cancelLongPress();
580
581 // Cancel long press for all children
582 final int count = getChildCount();
583 for (int i = 0; i < count; i++) {
584 final View child = getChildAt(i);
585 child.cancelLongPress();
586 }
587 }
588
Michael Jurkadee05892010-07-27 10:01:56 -0700589 public void setOnInterceptTouchListener(View.OnTouchListener listener) {
590 mInterceptTouchListener = listener;
591 }
592
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800593 public int getCountX() {
Adam Cohend22015c2010-07-26 22:02:18 -0700594 return mCountX;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800595 }
596
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800597 public int getCountY() {
Adam Cohend22015c2010-07-26 22:02:18 -0700598 return mCountY;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800599 }
600
Sunny Goyalc13403c2016-11-18 23:44:48 -0800601 public boolean acceptsWidget() {
602 return mContainerType == WORKSPACE;
Sunny Goyale9b651e2015-04-24 11:44:51 -0700603 }
604
Andrew Flynn0dca1ec2012-02-29 13:33:22 -0800605 public boolean addViewToCellLayout(View child, int index, int childId, LayoutParams params,
Sunny Goyalf7a29e82015-04-24 15:20:43 -0700606 boolean markCells) {
Winson Chungaafa03c2010-06-11 17:34:16 -0700607 final LayoutParams lp = params;
608
Andrew Flynnde38e422012-05-08 11:22:15 -0700609 // Hotseat icons - remove text
Andrew Flynn0dca1ec2012-02-29 13:33:22 -0800610 if (child instanceof BubbleTextView) {
611 BubbleTextView bubbleChild = (BubbleTextView) child;
Sunny Goyalc13403c2016-11-18 23:44:48 -0800612 bubbleChild.setTextVisibility(mContainerType != HOTSEAT);
Andrew Flynn0dca1ec2012-02-29 13:33:22 -0800613 }
614
Sunny Goyalc13403c2016-11-18 23:44:48 -0800615 child.setScaleX(mChildScale);
616 child.setScaleY(mChildScale);
Adam Cohen307fe232012-08-16 17:55:58 -0700617
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800618 // Generate an id for each view, this assumes we have at most 256x256 cells
619 // per workspace screen
Adam Cohend22015c2010-07-26 22:02:18 -0700620 if (lp.cellX >= 0 && lp.cellX <= mCountX - 1 && lp.cellY >= 0 && lp.cellY <= mCountY - 1) {
Winson Chungaafa03c2010-06-11 17:34:16 -0700621 // If the horizontal or vertical span is set to -1, it is taken to
622 // mean that it spans the extent of the CellLayout
Adam Cohend22015c2010-07-26 22:02:18 -0700623 if (lp.cellHSpan < 0) lp.cellHSpan = mCountX;
624 if (lp.cellVSpan < 0) lp.cellVSpan = mCountY;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800625
Winson Chungaafa03c2010-06-11 17:34:16 -0700626 child.setId(childId);
Tony Wickhama0628cc2015-10-14 15:23:04 -0700627 if (LOGD) {
628 Log.d(TAG, "Adding view to ShortcutsAndWidgetsContainer: " + child);
629 }
Sunny Goyalf7a29e82015-04-24 15:20:43 -0700630 mShortcutsAndWidgets.addView(child, index, lp);
Michael Jurkadee05892010-07-27 10:01:56 -0700631
Michael Jurkaf3ca3ab2010-10-20 17:08:24 -0700632 if (markCells) markCellsAsOccupiedForView(child);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700633
Winson Chungaafa03c2010-06-11 17:34:16 -0700634 return true;
635 }
636 return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800637 }
Michael Jurka3e7c7632010-10-02 16:01:03 -0700638
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800639 @Override
Michael Jurka0280c3b2010-09-17 15:00:07 -0700640 public void removeAllViews() {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -0700641 mOccupied.clear();
Michael Jurkaa52570f2012-03-20 03:18:20 -0700642 mShortcutsAndWidgets.removeAllViews();
Michael Jurka0280c3b2010-09-17 15:00:07 -0700643 }
644
645 @Override
646 public void removeAllViewsInLayout() {
Michael Jurkaa52570f2012-03-20 03:18:20 -0700647 if (mShortcutsAndWidgets.getChildCount() > 0) {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -0700648 mOccupied.clear();
Michael Jurkaa52570f2012-03-20 03:18:20 -0700649 mShortcutsAndWidgets.removeAllViewsInLayout();
Michael Jurka7cfc2822011-08-02 20:19:24 -0700650 }
Michael Jurka0280c3b2010-09-17 15:00:07 -0700651 }
652
653 @Override
654 public void removeView(View view) {
655 markCellsAsUnoccupiedForView(view);
Michael Jurkaa52570f2012-03-20 03:18:20 -0700656 mShortcutsAndWidgets.removeView(view);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700657 }
658
659 @Override
660 public void removeViewAt(int index) {
Michael Jurkaa52570f2012-03-20 03:18:20 -0700661 markCellsAsUnoccupiedForView(mShortcutsAndWidgets.getChildAt(index));
662 mShortcutsAndWidgets.removeViewAt(index);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700663 }
664
665 @Override
666 public void removeViewInLayout(View view) {
667 markCellsAsUnoccupiedForView(view);
Michael Jurkaa52570f2012-03-20 03:18:20 -0700668 mShortcutsAndWidgets.removeViewInLayout(view);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700669 }
670
671 @Override
672 public void removeViews(int start, int count) {
673 for (int i = start; i < start + count; i++) {
Michael Jurkaa52570f2012-03-20 03:18:20 -0700674 markCellsAsUnoccupiedForView(mShortcutsAndWidgets.getChildAt(i));
Michael Jurka0280c3b2010-09-17 15:00:07 -0700675 }
Michael Jurkaa52570f2012-03-20 03:18:20 -0700676 mShortcutsAndWidgets.removeViews(start, count);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700677 }
678
679 @Override
680 public void removeViewsInLayout(int start, int count) {
681 for (int i = start; i < start + count; i++) {
Michael Jurkaa52570f2012-03-20 03:18:20 -0700682 markCellsAsUnoccupiedForView(mShortcutsAndWidgets.getChildAt(i));
Michael Jurka0280c3b2010-09-17 15:00:07 -0700683 }
Michael Jurkaa52570f2012-03-20 03:18:20 -0700684 mShortcutsAndWidgets.removeViewsInLayout(start, count);
Michael Jurkaabded662011-03-04 12:06:57 -0800685 }
686
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700687 /**
Winson Chungaafa03c2010-06-11 17:34:16 -0700688 * Given a point, return the cell that strictly encloses that point
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800689 * @param x X coordinate of the point
690 * @param y Y coordinate of the point
691 * @param result Array of 2 ints to hold the x and y coordinate of the cell
692 */
Sunny Goyale9b651e2015-04-24 11:44:51 -0700693 public void pointToCellExact(int x, int y, int[] result) {
Winson Chung4b825dcd2011-06-19 12:41:22 -0700694 final int hStartPadding = getPaddingLeft();
695 final int vStartPadding = getPaddingTop();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800696
Sunny Goyalaa8a8712016-11-20 15:26:01 +0530697 result[0] = (x - hStartPadding) / mCellWidth;
698 result[1] = (y - vStartPadding) / mCellHeight;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800699
Adam Cohend22015c2010-07-26 22:02:18 -0700700 final int xAxis = mCountX;
701 final int yAxis = mCountY;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800702
703 if (result[0] < 0) result[0] = 0;
704 if (result[0] >= xAxis) result[0] = xAxis - 1;
705 if (result[1] < 0) result[1] = 0;
706 if (result[1] >= yAxis) result[1] = yAxis - 1;
707 }
Winson Chungaafa03c2010-06-11 17:34:16 -0700708
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800709 /**
710 * Given a point, return the cell that most closely encloses that point
711 * @param x X coordinate of the point
712 * @param y Y coordinate of the point
713 * @param result Array of 2 ints to hold the x and y coordinate of the cell
714 */
715 void pointToCellRounded(int x, int y, int[] result) {
716 pointToCellExact(x + (mCellWidth / 2), y + (mCellHeight / 2), result);
717 }
718
719 /**
720 * Given a cell coordinate, return the point that represents the upper left corner of that cell
Winson Chungaafa03c2010-06-11 17:34:16 -0700721 *
722 * @param cellX X coordinate of the cell
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800723 * @param cellY Y coordinate of the cell
Winson Chungaafa03c2010-06-11 17:34:16 -0700724 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800725 * @param result Array of 2 ints to hold the x and y coordinate of the point
726 */
727 void cellToPoint(int cellX, int cellY, int[] result) {
Winson Chung4b825dcd2011-06-19 12:41:22 -0700728 final int hStartPadding = getPaddingLeft();
729 final int vStartPadding = getPaddingTop();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800730
Sunny Goyalaa8a8712016-11-20 15:26:01 +0530731 result[0] = hStartPadding + cellX * mCellWidth;
732 result[1] = vStartPadding + cellY * mCellHeight;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800733 }
734
Adam Cohene3e27a82011-04-15 12:07:39 -0700735 /**
Adam Cohen482ed822012-03-02 14:15:13 -0800736 * Given a cell coordinate, return the point that represents the center of the cell
Adam Cohene3e27a82011-04-15 12:07:39 -0700737 *
738 * @param cellX X coordinate of the cell
739 * @param cellY Y coordinate of the cell
740 *
741 * @param result Array of 2 ints to hold the x and y coordinate of the point
742 */
743 void cellToCenterPoint(int cellX, int cellY, int[] result) {
Adam Cohen47a876d2012-03-19 13:21:41 -0700744 regionToCenterPoint(cellX, cellY, 1, 1, result);
745 }
746
747 /**
748 * Given a cell coordinate and span return the point that represents the center of the regio
749 *
750 * @param cellX X coordinate of the cell
751 * @param cellY Y coordinate of the cell
752 *
753 * @param result Array of 2 ints to hold the x and y coordinate of the point
754 */
755 void regionToCenterPoint(int cellX, int cellY, int spanX, int spanY, int[] result) {
Winson Chung4b825dcd2011-06-19 12:41:22 -0700756 final int hStartPadding = getPaddingLeft();
757 final int vStartPadding = getPaddingTop();
Sunny Goyalaa8a8712016-11-20 15:26:01 +0530758 result[0] = hStartPadding + cellX * mCellWidth + (spanX * mCellWidth) / 2;
759 result[1] = vStartPadding + cellY * mCellHeight + (spanY * mCellHeight) / 2;
Adam Cohene3e27a82011-04-15 12:07:39 -0700760 }
761
Adam Cohen19f37922012-03-21 11:59:11 -0700762 /**
763 * Given a cell coordinate and span fills out a corresponding pixel rect
764 *
765 * @param cellX X coordinate of the cell
766 * @param cellY Y coordinate of the cell
767 * @param result Rect in which to write the result
768 */
769 void regionToRect(int cellX, int cellY, int spanX, int spanY, Rect result) {
770 final int hStartPadding = getPaddingLeft();
771 final int vStartPadding = getPaddingTop();
Sunny Goyalaa8a8712016-11-20 15:26:01 +0530772 final int left = hStartPadding + cellX * mCellWidth;
773 final int top = vStartPadding + cellY * mCellHeight;
774 result.set(left, top, left + (spanX * mCellWidth), top + (spanY * mCellHeight));
Adam Cohen19f37922012-03-21 11:59:11 -0700775 }
776
Adam Cohen482ed822012-03-02 14:15:13 -0800777 public float getDistanceFromCell(float x, float y, int[] cell) {
778 cellToCenterPoint(cell[0], cell[1], mTmpPoint);
Sunny Goyalf7a29e82015-04-24 15:20:43 -0700779 return (float) Math.hypot(x - mTmpPoint[0], y - mTmpPoint[1]);
Adam Cohen482ed822012-03-02 14:15:13 -0800780 }
781
Adam Cohenf9c184a2016-01-15 16:47:43 -0800782 public int getCellWidth() {
Romain Guy84f296c2009-11-04 15:00:44 -0800783 return mCellWidth;
784 }
785
786 int getCellHeight() {
787 return mCellHeight;
788 }
789
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700790 public void setFixedSize(int width, int height) {
791 mFixedWidth = width;
792 mFixedHeight = height;
793 }
794
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800795 @Override
796 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800797 int widthSpecMode = MeasureSpec.getMode(widthMeasureSpec);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800798 int heightSpecMode = MeasureSpec.getMode(heightMeasureSpec);
Winson Chung5f8afe62013-08-12 16:19:28 -0700799 int widthSize = MeasureSpec.getSize(widthMeasureSpec);
800 int heightSize = MeasureSpec.getSize(heightMeasureSpec);
Winson Chung2d75f122013-09-23 16:53:31 -0700801 int childWidthSize = widthSize - (getPaddingLeft() + getPaddingRight());
802 int childHeightSize = heightSize - (getPaddingTop() + getPaddingBottom());
Winson Chung11a1a532013-09-13 11:14:45 -0700803 if (mFixedCellWidth < 0 || mFixedCellHeight < 0) {
Sunny Goyalc6205602015-05-21 20:46:33 -0700804 int cw = DeviceProfile.calculateCellWidth(childWidthSize, mCountX);
805 int ch = DeviceProfile.calculateCellHeight(childHeightSize, mCountY);
Winson Chung11a1a532013-09-13 11:14:45 -0700806 if (cw != mCellWidth || ch != mCellHeight) {
807 mCellWidth = cw;
808 mCellHeight = ch;
Sunny Goyalaa8a8712016-11-20 15:26:01 +0530809 mShortcutsAndWidgets.setCellDimensions(mCellWidth, mCellHeight, mCountX, mCountY);
Winson Chung11a1a532013-09-13 11:14:45 -0700810 }
Winson Chung5f8afe62013-08-12 16:19:28 -0700811 }
Winson Chungaafa03c2010-06-11 17:34:16 -0700812
Winson Chung2d75f122013-09-23 16:53:31 -0700813 int newWidth = childWidthSize;
814 int newHeight = childHeightSize;
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700815 if (mFixedWidth > 0 && mFixedHeight > 0) {
816 newWidth = mFixedWidth;
817 newHeight = mFixedHeight;
818 } else if (widthSpecMode == MeasureSpec.UNSPECIFIED || heightSpecMode == MeasureSpec.UNSPECIFIED) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800819 throw new RuntimeException("CellLayout cannot have UNSPECIFIED dimensions");
820 }
821
Sunny Goyal4fe5a372015-05-14 19:55:10 -0700822 // Make the feedback view large enough to hold the blur bitmap.
823 mTouchFeedbackView.measure(
824 MeasureSpec.makeMeasureSpec(mCellWidth + mTouchFeedbackView.getExtraSize(),
825 MeasureSpec.EXACTLY),
826 MeasureSpec.makeMeasureSpec(mCellHeight + mTouchFeedbackView.getExtraSize(),
827 MeasureSpec.EXACTLY));
828
829 mShortcutsAndWidgets.measure(
830 MeasureSpec.makeMeasureSpec(newWidth, MeasureSpec.EXACTLY),
831 MeasureSpec.makeMeasureSpec(newHeight, MeasureSpec.EXACTLY));
832
833 int maxWidth = mShortcutsAndWidgets.getMeasuredWidth();
834 int maxHeight = mShortcutsAndWidgets.getMeasuredHeight();
Winson Chung2d75f122013-09-23 16:53:31 -0700835 if (mFixedWidth > 0 && mFixedHeight > 0) {
836 setMeasuredDimension(maxWidth, maxHeight);
837 } else {
838 setMeasuredDimension(widthSize, heightSize);
839 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800840 }
841
842 @Override
Michael Jurka28750fb2010-09-24 17:43:49 -0700843 protected void onLayout(boolean changed, int l, int t, int r, int b) {
Tony Wickham26b01422015-11-10 14:44:32 -0800844 boolean isFullscreen = mShortcutsAndWidgets.getChildCount() > 0 &&
845 ((LayoutParams) mShortcutsAndWidgets.getChildAt(0).getLayoutParams()).isFullscreen;
846 int left = getPaddingLeft();
847 if (!isFullscreen) {
Tony Wickhama501d492015-11-03 18:05:01 -0800848 left += (int) Math.ceil(getUnusedHorizontalSpace() / 2f);
Tony Wickham26b01422015-11-10 14:44:32 -0800849 }
Sunny Goyal7c786f72016-06-01 14:08:21 -0700850 int right = r - l - getPaddingRight();
851 if (!isFullscreen) {
852 right -= (int) Math.ceil(getUnusedHorizontalSpace() / 2f);
853 }
854
Winson Chung38848ca2013-10-08 12:03:44 -0700855 int top = getPaddingTop();
Sunny Goyal7c786f72016-06-01 14:08:21 -0700856 int bottom = b - t - getPaddingBottom();
Sunny Goyal4fe5a372015-05-14 19:55:10 -0700857
858 mTouchFeedbackView.layout(left, top,
859 left + mTouchFeedbackView.getMeasuredWidth(),
860 top + mTouchFeedbackView.getMeasuredHeight());
Sunny Goyal7c786f72016-06-01 14:08:21 -0700861 mShortcutsAndWidgets.layout(left, top, right, bottom);
862
863 // Expand the background drawing bounds by the padding baked into the background drawable
864 mBackground.getPadding(mTempRect);
865 mBackground.setBounds(
866 left - mTempRect.left,
867 top - mTempRect.top,
868 right + mTempRect.right,
869 bottom + mTempRect.bottom);
870 }
871
Tony Wickhama501d492015-11-03 18:05:01 -0800872 /**
873 * Returns the amount of space left over after subtracting padding and cells. This space will be
874 * very small, a few pixels at most, and is a result of rounding down when calculating the cell
875 * width in {@link DeviceProfile#calculateCellWidth(int, int)}.
876 */
877 public int getUnusedHorizontalSpace() {
878 return getMeasuredWidth() - getPaddingLeft() - getPaddingRight() - (mCountX * mCellWidth);
879 }
880
Michael Jurka5f1c5092010-09-03 14:15:02 -0700881 public float getBackgroundAlpha() {
882 return mBackgroundAlpha;
Michael Jurkadee05892010-07-27 10:01:56 -0700883 }
884
Michael Jurka5f1c5092010-09-03 14:15:02 -0700885 public void setBackgroundAlpha(float alpha) {
Michael Jurkaafaa0502011-12-13 18:22:50 -0800886 if (mBackgroundAlpha != alpha) {
887 mBackgroundAlpha = alpha;
Sunny Goyal2805e632015-05-20 15:35:32 -0700888 mBackground.setAlpha((int) (mBackgroundAlpha * 255));
Michael Jurkaafaa0502011-12-13 18:22:50 -0800889 }
Michael Jurkadee05892010-07-27 10:01:56 -0700890 }
891
Sunny Goyal2805e632015-05-20 15:35:32 -0700892 @Override
893 protected boolean verifyDrawable(Drawable who) {
894 return super.verifyDrawable(who) || (mIsDragTarget && who == mBackground);
895 }
896
Michael Jurkaa52570f2012-03-20 03:18:20 -0700897 public void setShortcutAndWidgetAlpha(float alpha) {
Sunny Goyal02b50812014-09-10 15:44:42 -0700898 mShortcutsAndWidgets.setAlpha(alpha);
Michael Jurkadee05892010-07-27 10:01:56 -0700899 }
900
Michael Jurkaa52570f2012-03-20 03:18:20 -0700901 public ShortcutAndWidgetContainer getShortcutsAndWidgets() {
Sunny Goyaldcbcc862014-08-12 15:58:36 -0700902 return mShortcutsAndWidgets;
Michael Jurkaa52570f2012-03-20 03:18:20 -0700903 }
904
Patrick Dubroy440c3602010-07-13 17:50:32 -0700905 public View getChildAt(int x, int y) {
Michael Jurkaa52570f2012-03-20 03:18:20 -0700906 return mShortcutsAndWidgets.getChildAt(x, y);
Patrick Dubroy440c3602010-07-13 17:50:32 -0700907 }
908
Adam Cohen76fc0852011-06-17 13:26:23 -0700909 public boolean animateChildToPosition(final View child, int cellX, int cellY, int duration,
Adam Cohen482ed822012-03-02 14:15:13 -0800910 int delay, boolean permanent, boolean adjustOccupied) {
Michael Jurkaa52570f2012-03-20 03:18:20 -0700911 ShortcutAndWidgetContainer clc = getShortcutsAndWidgets();
Adam Cohen482ed822012-03-02 14:15:13 -0800912
Adam Cohen19f37922012-03-21 11:59:11 -0700913 if (clc.indexOfChild(child) != -1) {
Adam Cohenbfbfd262011-06-13 16:55:12 -0700914 final LayoutParams lp = (LayoutParams) child.getLayoutParams();
915 final ItemInfo info = (ItemInfo) child.getTag();
916
917 // We cancel any existing animations
918 if (mReorderAnimators.containsKey(lp)) {
919 mReorderAnimators.get(lp).cancel();
920 mReorderAnimators.remove(lp);
921 }
922
Adam Cohen482ed822012-03-02 14:15:13 -0800923 final int oldX = lp.x;
924 final int oldY = lp.y;
925 if (adjustOccupied) {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -0700926 GridOccupancy occupied = permanent ? mOccupied : mTmpOccupied;
927 occupied.markCells(lp.cellX, lp.cellY, lp.cellHSpan, lp.cellVSpan, false);
928 occupied.markCells(cellX, cellY, lp.cellHSpan, lp.cellVSpan, true);
Adam Cohen482ed822012-03-02 14:15:13 -0800929 }
Adam Cohenbfbfd262011-06-13 16:55:12 -0700930 lp.isLockedToGrid = true;
Adam Cohen482ed822012-03-02 14:15:13 -0800931 if (permanent) {
932 lp.cellX = info.cellX = cellX;
933 lp.cellY = info.cellY = cellY;
934 } else {
935 lp.tmpCellX = cellX;
936 lp.tmpCellY = cellY;
937 }
Jon Mirandae96798e2016-12-07 12:10:44 -0800938 clc.setupLp(child);
Adam Cohenbfbfd262011-06-13 16:55:12 -0700939 lp.isLockedToGrid = false;
Adam Cohen482ed822012-03-02 14:15:13 -0800940 final int newX = lp.x;
941 final int newY = lp.y;
Adam Cohenbfbfd262011-06-13 16:55:12 -0700942
Adam Cohen76fc0852011-06-17 13:26:23 -0700943 lp.x = oldX;
944 lp.y = oldY;
Adam Cohen76fc0852011-06-17 13:26:23 -0700945
Adam Cohen482ed822012-03-02 14:15:13 -0800946 // Exit early if we're not actually moving the view
947 if (oldX == newX && oldY == newY) {
948 lp.isLockedToGrid = true;
949 return true;
950 }
951
Jon Mirandacda3bfb2017-02-06 15:54:41 -0800952 ValueAnimator va = LauncherAnimUtils.ofFloat(0f, 1f);
Adam Cohen482ed822012-03-02 14:15:13 -0800953 va.setDuration(duration);
954 mReorderAnimators.put(lp, va);
955
956 va.addUpdateListener(new AnimatorUpdateListener() {
957 @Override
Adam Cohenbfbfd262011-06-13 16:55:12 -0700958 public void onAnimationUpdate(ValueAnimator animation) {
Jon Mirandae96798e2016-12-07 12:10:44 -0800959 float r = (Float) animation.getAnimatedValue();
Adam Cohen19f37922012-03-21 11:59:11 -0700960 lp.x = (int) ((1 - r) * oldX + r * newX);
961 lp.y = (int) ((1 - r) * oldY + r * newY);
Adam Cohen6b8a02d2012-03-22 15:13:40 -0700962 child.requestLayout();
Adam Cohenbfbfd262011-06-13 16:55:12 -0700963 }
964 });
Adam Cohen482ed822012-03-02 14:15:13 -0800965 va.addListener(new AnimatorListenerAdapter() {
Adam Cohenbfbfd262011-06-13 16:55:12 -0700966 boolean cancelled = false;
967 public void onAnimationEnd(Animator animation) {
968 // If the animation was cancelled, it means that another animation
969 // has interrupted this one, and we don't want to lock the item into
970 // place just yet.
971 if (!cancelled) {
972 lp.isLockedToGrid = true;
Adam Cohen482ed822012-03-02 14:15:13 -0800973 child.requestLayout();
Adam Cohenbfbfd262011-06-13 16:55:12 -0700974 }
975 if (mReorderAnimators.containsKey(lp)) {
976 mReorderAnimators.remove(lp);
977 }
978 }
979 public void onAnimationCancel(Animator animation) {
980 cancelled = true;
981 }
982 });
Adam Cohen482ed822012-03-02 14:15:13 -0800983 va.setStartDelay(delay);
984 va.start();
Adam Cohenbfbfd262011-06-13 16:55:12 -0700985 return true;
986 }
987 return false;
988 }
989
Sunny Goyal06e21a22016-08-11 16:02:02 -0700990 void visualizeDropLocation(View v, DragPreviewProvider outlineProvider, int cellX, int cellY,
991 int spanX, int spanY, boolean resize, DropTarget.DragObject dragObject) {
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -0700992 final int oldDragCellX = mDragCell[0];
993 final int oldDragCellY = mDragCell[1];
Adam Cohen482ed822012-03-02 14:15:13 -0800994
Hyunyoung Song0de01172016-10-05 16:27:48 -0700995 if (outlineProvider == null || outlineProvider.generatedDragOutline == null) {
Adam Cohen2801caf2011-05-13 20:57:39 -0700996 return;
997 }
998
Hyunyoung Song0de01172016-10-05 16:27:48 -0700999 Bitmap dragOutline = outlineProvider.generatedDragOutline;
Adam Cohen482ed822012-03-02 14:15:13 -08001000 if (cellX != oldDragCellX || cellY != oldDragCellY) {
Sunny Goyale78e3d72015-09-24 11:23:31 -07001001 Point dragOffset = dragObject.dragView.getDragVisualizeOffset();
1002 Rect dragRegion = dragObject.dragView.getDragRegion();
1003
Adam Cohen482ed822012-03-02 14:15:13 -08001004 mDragCell[0] = cellX;
1005 mDragCell[1] = cellY;
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001006
Joe Onorato4be866d2010-10-10 11:26:02 -07001007 final int oldIndex = mDragOutlineCurrent;
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -07001008 mDragOutlineAnims[oldIndex].animateOut();
1009 mDragOutlineCurrent = (oldIndex + 1) % mDragOutlines.length;
Adam Cohend41fbf52012-02-16 23:53:59 -08001010 Rect r = mDragOutlines[mDragOutlineCurrent];
Sunny Goyal106bf642015-07-16 12:18:06 -07001011
Adam Cohend41fbf52012-02-16 23:53:59 -08001012 if (resize) {
Adam Cohen482ed822012-03-02 14:15:13 -08001013 cellToRect(cellX, cellY, spanX, spanY, r);
Jon Mirandae96798e2016-12-07 12:10:44 -08001014 if (v instanceof LauncherAppWidgetHostView) {
1015 DeviceProfile profile = mLauncher.getDeviceProfile();
Jon Miranda6f6a06a2016-12-15 11:24:18 -08001016 Utilities.shrinkRect(r, profile.appWidgetScale.x, profile.appWidgetScale.y);
Jon Mirandae96798e2016-12-07 12:10:44 -08001017 }
Sunny Goyal106bf642015-07-16 12:18:06 -07001018 } else {
1019 // Find the top left corner of the rect the object will occupy
1020 final int[] topLeft = mTmpPoint;
1021 cellToPoint(cellX, cellY, topLeft);
1022
1023 int left = topLeft[0];
1024 int top = topLeft[1];
1025
1026 if (v != null && dragOffset == null) {
1027 // When drawing the drag outline, it did not account for margin offsets
1028 // added by the view's parent.
1029 MarginLayoutParams lp = (MarginLayoutParams) v.getLayoutParams();
1030 left += lp.leftMargin;
1031 top += lp.topMargin;
1032
1033 // Offsets due to the size difference between the View and the dragOutline.
1034 // There is a size difference to account for the outer blur, which may lie
1035 // outside the bounds of the view.
Jon Mirandaf7ff3fe2016-12-05 12:04:44 -08001036 top += ((mCellHeight * spanY) - dragOutline.getHeight()) / 2;
Sunny Goyal106bf642015-07-16 12:18:06 -07001037 // We center about the x axis
Sunny Goyalaa8a8712016-11-20 15:26:01 +05301038 left += ((mCellWidth * spanX) - dragOutline.getWidth()) / 2;
Sunny Goyal106bf642015-07-16 12:18:06 -07001039 } else {
1040 if (dragOffset != null && dragRegion != null) {
1041 // Center the drag region *horizontally* in the cell and apply a drag
1042 // outline offset
Sunny Goyalaa8a8712016-11-20 15:26:01 +05301043 left += dragOffset.x + ((mCellWidth * spanX) - dragRegion.width()) / 2;
Sunny Goyal106bf642015-07-16 12:18:06 -07001044 int cHeight = getShortcutsAndWidgets().getCellContentHeight();
1045 int cellPaddingY = (int) Math.max(0, ((mCellHeight - cHeight) / 2f));
1046 top += dragOffset.y + cellPaddingY;
1047 } else {
1048 // Center the drag outline in the cell
Sunny Goyalaa8a8712016-11-20 15:26:01 +05301049 left += ((mCellWidth * spanX) - dragOutline.getWidth()) / 2;
1050 top += ((mCellHeight * spanY) - dragOutline.getHeight()) / 2;
Sunny Goyal106bf642015-07-16 12:18:06 -07001051 }
1052 }
1053 r.set(left, top, left + dragOutline.getWidth(), top + dragOutline.getHeight());
Adam Cohend41fbf52012-02-16 23:53:59 -08001054 }
Winson Chung150fbab2010-09-29 17:14:26 -07001055
Jon Miranda6f6a06a2016-12-15 11:24:18 -08001056 Utilities.scaleRectAboutCenter(r, mChildScale);
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -07001057 mDragOutlineAnims[mDragOutlineCurrent].setTag(dragOutline);
1058 mDragOutlineAnims[mDragOutlineCurrent].animateIn();
Sunny Goyale78e3d72015-09-24 11:23:31 -07001059
1060 if (dragObject.stateAnnouncer != null) {
Sunny Goyalc13403c2016-11-18 23:44:48 -08001061 dragObject.stateAnnouncer.announce(getItemMoveDescription(cellX, cellY));
Sunny Goyale78e3d72015-09-24 11:23:31 -07001062 }
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001063 }
1064 }
1065
Sunny Goyalc13403c2016-11-18 23:44:48 -08001066 public String getItemMoveDescription(int cellX, int cellY) {
1067 if (mContainerType == HOTSEAT) {
1068 return getContext().getString(R.string.move_to_hotseat_position,
1069 Math.max(cellX, cellY) + 1);
1070 } else {
1071 return getContext().getString(R.string.move_to_empty_cell,
1072 cellY + 1, cellX + 1);
1073 }
1074 }
1075
Adam Cohene0310962011-04-18 16:15:31 -07001076 public void clearDragOutlines() {
1077 final int oldIndex = mDragOutlineCurrent;
1078 mDragOutlineAnims[oldIndex].animateOut();
Adam Cohend41fbf52012-02-16 23:53:59 -08001079 mDragCell[0] = mDragCell[1] = -1;
Adam Cohene0310962011-04-18 16:15:31 -07001080 }
1081
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001082 /**
Jeff Sharkey70864282009-04-07 21:08:40 -07001083 * Find a vacant area that will fit the given bounds nearest the requested
1084 * cell location. Uses Euclidean distance to score multiple vacant areas.
Winson Chungaafa03c2010-06-11 17:34:16 -07001085 *
Romain Guy51afc022009-05-04 18:03:43 -07001086 * @param pixelX The X location at which you want to search for a vacant area.
1087 * @param pixelY The Y location at which you want to search for a vacant area.
Adam Cohend41fbf52012-02-16 23:53:59 -08001088 * @param minSpanX The minimum horizontal span required
1089 * @param minSpanY The minimum vertical span required
1090 * @param spanX Horizontal span of the object.
1091 * @param spanY Vertical span of the object.
1092 * @param result Array in which to place the result, or null (in which case a new array will
1093 * be allocated)
1094 * @return The X, Y cell of a vacant area that can contain this object,
1095 * nearest the requested location.
1096 */
1097 int[] findNearestVacantArea(int pixelX, int pixelY, int minSpanX, int minSpanY, int spanX,
1098 int spanY, int[] result, int[] resultSpan) {
Sunny Goyalf7a29e82015-04-24 15:20:43 -07001099 return findNearestArea(pixelX, pixelY, minSpanX, minSpanY, spanX, spanY, true,
Adam Cohend41fbf52012-02-16 23:53:59 -08001100 result, resultSpan);
1101 }
1102
Adam Cohend41fbf52012-02-16 23:53:59 -08001103 private final Stack<Rect> mTempRectStack = new Stack<Rect>();
1104 private void lazyInitTempRectStack() {
1105 if (mTempRectStack.isEmpty()) {
1106 for (int i = 0; i < mCountX * mCountY; i++) {
1107 mTempRectStack.push(new Rect());
1108 }
1109 }
1110 }
Adam Cohen482ed822012-03-02 14:15:13 -08001111
Adam Cohend41fbf52012-02-16 23:53:59 -08001112 private void recycleTempRects(Stack<Rect> used) {
1113 while (!used.isEmpty()) {
1114 mTempRectStack.push(used.pop());
1115 }
1116 }
1117
1118 /**
1119 * Find a vacant area that will fit the given bounds nearest the requested
1120 * cell location. Uses Euclidean distance to score multiple vacant areas.
1121 *
1122 * @param pixelX The X location at which you want to search for a vacant area.
1123 * @param pixelY The Y location at which you want to search for a vacant area.
1124 * @param minSpanX The minimum horizontal span required
1125 * @param minSpanY The minimum vertical span required
1126 * @param spanX Horizontal span of the object.
1127 * @param spanY Vertical span of the object.
1128 * @param ignoreOccupied If true, the result can be an occupied cell
1129 * @param result Array in which to place the result, or null (in which case a new array will
1130 * be allocated)
1131 * @return The X, Y cell of a vacant area that can contain this object,
1132 * nearest the requested location.
1133 */
Sunny Goyalf7a29e82015-04-24 15:20:43 -07001134 private int[] findNearestArea(int pixelX, int pixelY, int minSpanX, int minSpanY, int spanX,
1135 int spanY, boolean ignoreOccupied, int[] result, int[] resultSpan) {
Adam Cohend41fbf52012-02-16 23:53:59 -08001136 lazyInitTempRectStack();
Michael Jurkac6ee42e2010-09-30 12:04:50 -07001137
Adam Cohene3e27a82011-04-15 12:07:39 -07001138 // For items with a spanX / spanY > 1, the passed in point (pixelX, pixelY) corresponds
1139 // to the center of the item, but we are searching based on the top-left cell, so
1140 // we translate the point over to correspond to the top-left.
Sunny Goyalaa8a8712016-11-20 15:26:01 +05301141 pixelX -= mCellWidth * (spanX - 1) / 2f;
1142 pixelY -= mCellHeight * (spanY - 1) / 2f;
Adam Cohene3e27a82011-04-15 12:07:39 -07001143
Jeff Sharkey70864282009-04-07 21:08:40 -07001144 // Keep track of best-scoring drop area
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001145 final int[] bestXY = result != null ? result : new int[2];
Jeff Sharkey70864282009-04-07 21:08:40 -07001146 double bestDistance = Double.MAX_VALUE;
Adam Cohend41fbf52012-02-16 23:53:59 -08001147 final Rect bestRect = new Rect(-1, -1, -1, -1);
1148 final Stack<Rect> validRegions = new Stack<Rect>();
Winson Chungaafa03c2010-06-11 17:34:16 -07001149
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001150 final int countX = mCountX;
1151 final int countY = mCountY;
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001152
Adam Cohend41fbf52012-02-16 23:53:59 -08001153 if (minSpanX <= 0 || minSpanY <= 0 || spanX <= 0 || spanY <= 0 ||
1154 spanX < minSpanX || spanY < minSpanY) {
1155 return bestXY;
1156 }
1157
1158 for (int y = 0; y < countY - (minSpanY - 1); y++) {
Michael Jurkac28de512010-08-13 11:27:44 -07001159 inner:
Adam Cohend41fbf52012-02-16 23:53:59 -08001160 for (int x = 0; x < countX - (minSpanX - 1); x++) {
1161 int ySize = -1;
1162 int xSize = -1;
Adam Cohendf035382011-04-11 17:22:04 -07001163 if (ignoreOccupied) {
Adam Cohend41fbf52012-02-16 23:53:59 -08001164 // First, let's see if this thing fits anywhere
1165 for (int i = 0; i < minSpanX; i++) {
1166 for (int j = 0; j < minSpanY; j++) {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001167 if (mOccupied.cells[x + i][y + j]) {
Adam Cohendf035382011-04-11 17:22:04 -07001168 continue inner;
1169 }
Michael Jurkac28de512010-08-13 11:27:44 -07001170 }
1171 }
Adam Cohend41fbf52012-02-16 23:53:59 -08001172 xSize = minSpanX;
1173 ySize = minSpanY;
1174
1175 // We know that the item will fit at _some_ acceptable size, now let's see
1176 // how big we can make it. We'll alternate between incrementing x and y spans
1177 // until we hit a limit.
1178 boolean incX = true;
1179 boolean hitMaxX = xSize >= spanX;
1180 boolean hitMaxY = ySize >= spanY;
1181 while (!(hitMaxX && hitMaxY)) {
1182 if (incX && !hitMaxX) {
1183 for (int j = 0; j < ySize; j++) {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001184 if (x + xSize > countX -1 || mOccupied.cells[x + xSize][y + j]) {
Adam Cohend41fbf52012-02-16 23:53:59 -08001185 // We can't move out horizontally
1186 hitMaxX = true;
1187 }
1188 }
1189 if (!hitMaxX) {
1190 xSize++;
1191 }
1192 } else if (!hitMaxY) {
1193 for (int i = 0; i < xSize; i++) {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001194 if (y + ySize > countY - 1 || mOccupied.cells[x + i][y + ySize]) {
Adam Cohend41fbf52012-02-16 23:53:59 -08001195 // We can't move out vertically
1196 hitMaxY = true;
1197 }
1198 }
1199 if (!hitMaxY) {
1200 ySize++;
1201 }
1202 }
1203 hitMaxX |= xSize >= spanX;
1204 hitMaxY |= ySize >= spanY;
1205 incX = !incX;
1206 }
1207 incX = true;
1208 hitMaxX = xSize >= spanX;
1209 hitMaxY = ySize >= spanY;
Michael Jurkac28de512010-08-13 11:27:44 -07001210 }
Sunny Goyal2805e632015-05-20 15:35:32 -07001211 final int[] cellXY = mTmpPoint;
Adam Cohene3e27a82011-04-15 12:07:39 -07001212 cellToCenterPoint(x, y, cellXY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001213
Adam Cohend41fbf52012-02-16 23:53:59 -08001214 // We verify that the current rect is not a sub-rect of any of our previous
1215 // candidates. In this case, the current rect is disqualified in favour of the
1216 // containing rect.
1217 Rect currentRect = mTempRectStack.pop();
1218 currentRect.set(x, y, x + xSize, y + ySize);
1219 boolean contained = false;
1220 for (Rect r : validRegions) {
1221 if (r.contains(currentRect)) {
1222 contained = true;
1223 break;
1224 }
1225 }
1226 validRegions.push(currentRect);
Sunny Goyalf7a29e82015-04-24 15:20:43 -07001227 double distance = Math.hypot(cellXY[0] - pixelX, cellXY[1] - pixelY);
Adam Cohen482ed822012-03-02 14:15:13 -08001228
Adam Cohend41fbf52012-02-16 23:53:59 -08001229 if ((distance <= bestDistance && !contained) ||
1230 currentRect.contains(bestRect)) {
Michael Jurkac28de512010-08-13 11:27:44 -07001231 bestDistance = distance;
1232 bestXY[0] = x;
1233 bestXY[1] = y;
Adam Cohend41fbf52012-02-16 23:53:59 -08001234 if (resultSpan != null) {
1235 resultSpan[0] = xSize;
1236 resultSpan[1] = ySize;
1237 }
1238 bestRect.set(currentRect);
Michael Jurkac28de512010-08-13 11:27:44 -07001239 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001240 }
1241 }
1242
Adam Cohenc0dcf592011-06-01 15:30:43 -07001243 // Return -1, -1 if no suitable location found
1244 if (bestDistance == Double.MAX_VALUE) {
1245 bestXY[0] = -1;
1246 bestXY[1] = -1;
Jeff Sharkey70864282009-04-07 21:08:40 -07001247 }
Adam Cohend41fbf52012-02-16 23:53:59 -08001248 recycleTempRects(validRegions);
Adam Cohenc0dcf592011-06-01 15:30:43 -07001249 return bestXY;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001250 }
Winson Chungaafa03c2010-06-11 17:34:16 -07001251
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001252 /**
Adam Cohen482ed822012-03-02 14:15:13 -08001253 * Find a vacant area that will fit the given bounds nearest the requested
1254 * cell location, and will also weigh in a suggested direction vector of the
1255 * desired location. This method computers distance based on unit grid distances,
1256 * not pixel distances.
1257 *
Adam Cohen47a876d2012-03-19 13:21:41 -07001258 * @param cellX The X cell nearest to which you want to search for a vacant area.
1259 * @param cellY The Y cell nearest which you want to search for a vacant area.
Adam Cohen482ed822012-03-02 14:15:13 -08001260 * @param spanX Horizontal span of the object.
1261 * @param spanY Vertical span of the object.
Adam Cohen47a876d2012-03-19 13:21:41 -07001262 * @param direction The favored direction in which the views should move from x, y
Sunny Goyal9eba1fd2015-10-16 08:58:57 -07001263 * @param occupied The array which represents which cells in the CellLayout are occupied
Adam Cohen47a876d2012-03-19 13:21:41 -07001264 * @param blockOccupied The array which represents which cells in the specified block (cellX,
Winson Chung5f8afe62013-08-12 16:19:28 -07001265 * cellY, spanX, spanY) are occupied. This is used when try to move a group of views.
Adam Cohen482ed822012-03-02 14:15:13 -08001266 * @param result Array in which to place the result, or null (in which case a new array will
1267 * be allocated)
1268 * @return The X, Y cell of a vacant area that can contain this object,
1269 * nearest the requested location.
1270 */
1271 private int[] findNearestArea(int cellX, int cellY, int spanX, int spanY, int[] direction,
Adam Cohen47a876d2012-03-19 13:21:41 -07001272 boolean[][] occupied, boolean blockOccupied[][], int[] result) {
Adam Cohen482ed822012-03-02 14:15:13 -08001273 // Keep track of best-scoring drop area
1274 final int[] bestXY = result != null ? result : new int[2];
1275 float bestDistance = Float.MAX_VALUE;
1276 int bestDirectionScore = Integer.MIN_VALUE;
1277
1278 final int countX = mCountX;
1279 final int countY = mCountY;
1280
1281 for (int y = 0; y < countY - (spanY - 1); y++) {
1282 inner:
1283 for (int x = 0; x < countX - (spanX - 1); x++) {
1284 // First, let's see if this thing fits anywhere
1285 for (int i = 0; i < spanX; i++) {
1286 for (int j = 0; j < spanY; j++) {
Adam Cohen47a876d2012-03-19 13:21:41 -07001287 if (occupied[x + i][y + j] && (blockOccupied == null || blockOccupied[i][j])) {
Adam Cohen482ed822012-03-02 14:15:13 -08001288 continue inner;
1289 }
1290 }
1291 }
1292
Sunny Goyalf7a29e82015-04-24 15:20:43 -07001293 float distance = (float) Math.hypot(x - cellX, y - cellY);
Adam Cohen482ed822012-03-02 14:15:13 -08001294 int[] curDirection = mTmpPoint;
Adam Cohen47a876d2012-03-19 13:21:41 -07001295 computeDirectionVector(x - cellX, y - cellY, curDirection);
1296 // The direction score is just the dot product of the two candidate direction
1297 // and that passed in.
Adam Cohen482ed822012-03-02 14:15:13 -08001298 int curDirectionScore = direction[0] * curDirection[0] +
1299 direction[1] * curDirection[1];
Sunny Goyal8f90dcf2016-08-18 15:01:11 -07001300 if (Float.compare(distance, bestDistance) < 0 ||
1301 (Float.compare(distance, bestDistance) == 0
1302 && curDirectionScore > bestDirectionScore)) {
Adam Cohen482ed822012-03-02 14:15:13 -08001303 bestDistance = distance;
1304 bestDirectionScore = curDirectionScore;
1305 bestXY[0] = x;
1306 bestXY[1] = y;
1307 }
1308 }
1309 }
1310
1311 // Return -1, -1 if no suitable location found
1312 if (bestDistance == Float.MAX_VALUE) {
1313 bestXY[0] = -1;
1314 bestXY[1] = -1;
1315 }
1316 return bestXY;
1317 }
1318
1319 private boolean addViewToTempLocation(View v, Rect rectOccupiedByPotentialDrop,
Adam Cohen8baab352012-03-20 17:39:21 -07001320 int[] direction, ItemConfiguration currentState) {
1321 CellAndSpan c = currentState.map.get(v);
Adam Cohen482ed822012-03-02 14:15:13 -08001322 boolean success = false;
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001323 mTmpOccupied.markCells(c, false);
1324 mTmpOccupied.markCells(rectOccupiedByPotentialDrop, true);
Adam Cohen482ed822012-03-02 14:15:13 -08001325
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001326 findNearestArea(c.cellX, c.cellY, c.spanX, c.spanY, direction,
1327 mTmpOccupied.cells, null, mTempLocation);
Adam Cohen482ed822012-03-02 14:15:13 -08001328
1329 if (mTempLocation[0] >= 0 && mTempLocation[1] >= 0) {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001330 c.cellX = mTempLocation[0];
1331 c.cellY = mTempLocation[1];
Adam Cohen482ed822012-03-02 14:15:13 -08001332 success = true;
Adam Cohen482ed822012-03-02 14:15:13 -08001333 }
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001334 mTmpOccupied.markCells(c, true);
Adam Cohen482ed822012-03-02 14:15:13 -08001335 return success;
1336 }
1337
Adam Cohenf3900c22012-11-16 18:28:11 -08001338 /**
1339 * This helper class defines a cluster of views. It helps with defining complex edges
1340 * of the cluster and determining how those edges interact with other views. The edges
1341 * essentially define a fine-grained boundary around the cluster of views -- like a more
1342 * precise version of a bounding box.
1343 */
1344 private class ViewCluster {
Sunny Goyal6dc98b92016-04-03 15:25:29 -07001345 final static int LEFT = 1 << 0;
1346 final static int TOP = 1 << 1;
1347 final static int RIGHT = 1 << 2;
1348 final static int BOTTOM = 1 << 3;
Adam Cohen47a876d2012-03-19 13:21:41 -07001349
Adam Cohenf3900c22012-11-16 18:28:11 -08001350 ArrayList<View> views;
1351 ItemConfiguration config;
1352 Rect boundingRect = new Rect();
Adam Cohen47a876d2012-03-19 13:21:41 -07001353
Adam Cohenf3900c22012-11-16 18:28:11 -08001354 int[] leftEdge = new int[mCountY];
1355 int[] rightEdge = new int[mCountY];
1356 int[] topEdge = new int[mCountX];
1357 int[] bottomEdge = new int[mCountX];
Sunny Goyal6dc98b92016-04-03 15:25:29 -07001358 int dirtyEdges;
1359 boolean boundingRectDirty;
Adam Cohenf3900c22012-11-16 18:28:11 -08001360
1361 @SuppressWarnings("unchecked")
1362 public ViewCluster(ArrayList<View> views, ItemConfiguration config) {
1363 this.views = (ArrayList<View>) views.clone();
1364 this.config = config;
1365 resetEdges();
Adam Cohen47a876d2012-03-19 13:21:41 -07001366 }
1367
Adam Cohenf3900c22012-11-16 18:28:11 -08001368 void resetEdges() {
1369 for (int i = 0; i < mCountX; i++) {
1370 topEdge[i] = -1;
1371 bottomEdge[i] = -1;
1372 }
1373 for (int i = 0; i < mCountY; i++) {
1374 leftEdge[i] = -1;
1375 rightEdge[i] = -1;
1376 }
Sunny Goyal6dc98b92016-04-03 15:25:29 -07001377 dirtyEdges = LEFT | TOP | RIGHT | BOTTOM;
Adam Cohenf3900c22012-11-16 18:28:11 -08001378 boundingRectDirty = true;
1379 }
Adam Cohen47a876d2012-03-19 13:21:41 -07001380
Sunny Goyal6dc98b92016-04-03 15:25:29 -07001381 void computeEdge(int which) {
Adam Cohenf3900c22012-11-16 18:28:11 -08001382 int count = views.size();
1383 for (int i = 0; i < count; i++) {
1384 CellAndSpan cs = config.map.get(views.get(i));
1385 switch (which) {
1386 case LEFT:
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001387 int left = cs.cellX;
1388 for (int j = cs.cellY; j < cs.cellY + cs.spanY; j++) {
Sunny Goyal6dc98b92016-04-03 15:25:29 -07001389 if (left < leftEdge[j] || leftEdge[j] < 0) {
1390 leftEdge[j] = left;
Adam Cohena56dc102012-07-13 13:41:42 -07001391 }
Adam Cohen47a876d2012-03-19 13:21:41 -07001392 }
Adam Cohenf3900c22012-11-16 18:28:11 -08001393 break;
1394 case RIGHT:
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001395 int right = cs.cellX + cs.spanX;
1396 for (int j = cs.cellY; j < cs.cellY + cs.spanY; j++) {
Sunny Goyal6dc98b92016-04-03 15:25:29 -07001397 if (right > rightEdge[j]) {
1398 rightEdge[j] = right;
Adam Cohenf3900c22012-11-16 18:28:11 -08001399 }
1400 }
1401 break;
1402 case TOP:
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001403 int top = cs.cellY;
1404 for (int j = cs.cellX; j < cs.cellX + cs.spanX; j++) {
Sunny Goyal6dc98b92016-04-03 15:25:29 -07001405 if (top < topEdge[j] || topEdge[j] < 0) {
1406 topEdge[j] = top;
Adam Cohenf3900c22012-11-16 18:28:11 -08001407 }
1408 }
1409 break;
1410 case BOTTOM:
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001411 int bottom = cs.cellY + cs.spanY;
1412 for (int j = cs.cellX; j < cs.cellX + cs.spanX; j++) {
Sunny Goyal6dc98b92016-04-03 15:25:29 -07001413 if (bottom > bottomEdge[j]) {
1414 bottomEdge[j] = bottom;
Adam Cohenf3900c22012-11-16 18:28:11 -08001415 }
1416 }
1417 break;
Adam Cohen47a876d2012-03-19 13:21:41 -07001418 }
1419 }
1420 }
Adam Cohenf3900c22012-11-16 18:28:11 -08001421
1422 boolean isViewTouchingEdge(View v, int whichEdge) {
1423 CellAndSpan cs = config.map.get(v);
1424
Sunny Goyal6dc98b92016-04-03 15:25:29 -07001425 if ((dirtyEdges & whichEdge) == whichEdge) {
1426 computeEdge(whichEdge);
1427 dirtyEdges &= ~whichEdge;
1428 }
Adam Cohenf3900c22012-11-16 18:28:11 -08001429
1430 switch (whichEdge) {
1431 case LEFT:
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001432 for (int i = cs.cellY; i < cs.cellY + cs.spanY; i++) {
1433 if (leftEdge[i] == cs.cellX + cs.spanX) {
Adam Cohenf3900c22012-11-16 18:28:11 -08001434 return true;
1435 }
1436 }
1437 break;
1438 case RIGHT:
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001439 for (int i = cs.cellY; i < cs.cellY + cs.spanY; i++) {
1440 if (rightEdge[i] == cs.cellX) {
Adam Cohenf3900c22012-11-16 18:28:11 -08001441 return true;
1442 }
1443 }
1444 break;
1445 case TOP:
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001446 for (int i = cs.cellX; i < cs.cellX + cs.spanX; i++) {
1447 if (topEdge[i] == cs.cellY + cs.spanY) {
Adam Cohenf3900c22012-11-16 18:28:11 -08001448 return true;
1449 }
1450 }
1451 break;
1452 case BOTTOM:
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001453 for (int i = cs.cellX; i < cs.cellX + cs.spanX; i++) {
1454 if (bottomEdge[i] == cs.cellY) {
Adam Cohenf3900c22012-11-16 18:28:11 -08001455 return true;
1456 }
1457 }
1458 break;
1459 }
1460 return false;
1461 }
1462
1463 void shift(int whichEdge, int delta) {
1464 for (View v: views) {
1465 CellAndSpan c = config.map.get(v);
1466 switch (whichEdge) {
1467 case LEFT:
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001468 c.cellX -= delta;
Adam Cohenf3900c22012-11-16 18:28:11 -08001469 break;
1470 case RIGHT:
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001471 c.cellX += delta;
Adam Cohenf3900c22012-11-16 18:28:11 -08001472 break;
1473 case TOP:
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001474 c.cellY -= delta;
Adam Cohenf3900c22012-11-16 18:28:11 -08001475 break;
1476 case BOTTOM:
1477 default:
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001478 c.cellY += delta;
Adam Cohenf3900c22012-11-16 18:28:11 -08001479 break;
1480 }
1481 }
1482 resetEdges();
1483 }
1484
1485 public void addView(View v) {
1486 views.add(v);
1487 resetEdges();
1488 }
1489
1490 public Rect getBoundingRect() {
1491 if (boundingRectDirty) {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001492 config.getBoundingRectForViews(views, boundingRect);
Adam Cohenf3900c22012-11-16 18:28:11 -08001493 }
1494 return boundingRect;
1495 }
1496
Adam Cohenf3900c22012-11-16 18:28:11 -08001497 PositionComparator comparator = new PositionComparator();
1498 class PositionComparator implements Comparator<View> {
1499 int whichEdge = 0;
1500 public int compare(View left, View right) {
1501 CellAndSpan l = config.map.get(left);
1502 CellAndSpan r = config.map.get(right);
1503 switch (whichEdge) {
1504 case LEFT:
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001505 return (r.cellX + r.spanX) - (l.cellX + l.spanX);
Adam Cohenf3900c22012-11-16 18:28:11 -08001506 case RIGHT:
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001507 return l.cellX - r.cellX;
Adam Cohenf3900c22012-11-16 18:28:11 -08001508 case TOP:
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001509 return (r.cellY + r.spanY) - (l.cellY + l.spanY);
Adam Cohenf3900c22012-11-16 18:28:11 -08001510 case BOTTOM:
1511 default:
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001512 return l.cellY - r.cellY;
Adam Cohenf3900c22012-11-16 18:28:11 -08001513 }
1514 }
1515 }
1516
1517 public void sortConfigurationForEdgePush(int edge) {
1518 comparator.whichEdge = edge;
1519 Collections.sort(config.sortedViews, comparator);
1520 }
Adam Cohen47a876d2012-03-19 13:21:41 -07001521 }
1522
Adam Cohenf3900c22012-11-16 18:28:11 -08001523 private boolean pushViewsToTempLocation(ArrayList<View> views, Rect rectOccupiedByPotentialDrop,
1524 int[] direction, View dragView, ItemConfiguration currentState) {
Adam Cohene0489502012-08-27 15:18:53 -07001525
Adam Cohenf3900c22012-11-16 18:28:11 -08001526 ViewCluster cluster = new ViewCluster(views, currentState);
1527 Rect clusterRect = cluster.getBoundingRect();
1528 int whichEdge;
1529 int pushDistance;
1530 boolean fail = false;
1531
1532 // Determine the edge of the cluster that will be leading the push and how far
1533 // the cluster must be shifted.
1534 if (direction[0] < 0) {
1535 whichEdge = ViewCluster.LEFT;
1536 pushDistance = clusterRect.right - rectOccupiedByPotentialDrop.left;
Adam Cohene0489502012-08-27 15:18:53 -07001537 } else if (direction[0] > 0) {
Adam Cohenf3900c22012-11-16 18:28:11 -08001538 whichEdge = ViewCluster.RIGHT;
1539 pushDistance = rectOccupiedByPotentialDrop.right - clusterRect.left;
1540 } else if (direction[1] < 0) {
1541 whichEdge = ViewCluster.TOP;
1542 pushDistance = clusterRect.bottom - rectOccupiedByPotentialDrop.top;
1543 } else {
1544 whichEdge = ViewCluster.BOTTOM;
1545 pushDistance = rectOccupiedByPotentialDrop.bottom - clusterRect.top;
Adam Cohene0489502012-08-27 15:18:53 -07001546 }
1547
Adam Cohenf3900c22012-11-16 18:28:11 -08001548 // Break early for invalid push distance.
1549 if (pushDistance <= 0) {
1550 return false;
Adam Cohene0489502012-08-27 15:18:53 -07001551 }
Adam Cohenf3900c22012-11-16 18:28:11 -08001552
1553 // Mark the occupied state as false for the group of views we want to move.
1554 for (View v: views) {
1555 CellAndSpan c = currentState.map.get(v);
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001556 mTmpOccupied.markCells(c, false);
Adam Cohenf3900c22012-11-16 18:28:11 -08001557 }
1558
1559 // We save the current configuration -- if we fail to find a solution we will revert
1560 // to the initial state. The process of finding a solution modifies the configuration
1561 // in place, hence the need for revert in the failure case.
1562 currentState.save();
1563
1564 // The pushing algorithm is simplified by considering the views in the order in which
1565 // they would be pushed by the cluster. For example, if the cluster is leading with its
1566 // left edge, we consider sort the views by their right edge, from right to left.
1567 cluster.sortConfigurationForEdgePush(whichEdge);
1568
1569 while (pushDistance > 0 && !fail) {
1570 for (View v: currentState.sortedViews) {
1571 // For each view that isn't in the cluster, we see if the leading edge of the
1572 // cluster is contacting the edge of that view. If so, we add that view to the
1573 // cluster.
1574 if (!cluster.views.contains(v) && v != dragView) {
1575 if (cluster.isViewTouchingEdge(v, whichEdge)) {
1576 LayoutParams lp = (LayoutParams) v.getLayoutParams();
1577 if (!lp.canReorder) {
1578 // The push solution includes the all apps button, this is not viable.
1579 fail = true;
1580 break;
1581 }
1582 cluster.addView(v);
1583 CellAndSpan c = currentState.map.get(v);
1584
1585 // Adding view to cluster, mark it as not occupied.
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001586 mTmpOccupied.markCells(c, false);
Adam Cohenf3900c22012-11-16 18:28:11 -08001587 }
1588 }
1589 }
1590 pushDistance--;
1591
1592 // The cluster has been completed, now we move the whole thing over in the appropriate
1593 // direction.
1594 cluster.shift(whichEdge, 1);
1595 }
1596
1597 boolean foundSolution = false;
1598 clusterRect = cluster.getBoundingRect();
1599
1600 // Due to the nature of the algorithm, the only check required to verify a valid solution
1601 // is to ensure that completed shifted cluster lies completely within the cell layout.
1602 if (!fail && clusterRect.left >= 0 && clusterRect.right <= mCountX && clusterRect.top >= 0 &&
1603 clusterRect.bottom <= mCountY) {
1604 foundSolution = true;
1605 } else {
1606 currentState.restore();
1607 }
1608
1609 // In either case, we set the occupied array as marked for the location of the views
1610 for (View v: cluster.views) {
1611 CellAndSpan c = currentState.map.get(v);
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001612 mTmpOccupied.markCells(c, true);
Adam Cohenf3900c22012-11-16 18:28:11 -08001613 }
1614
1615 return foundSolution;
Adam Cohene0489502012-08-27 15:18:53 -07001616 }
1617
Adam Cohen482ed822012-03-02 14:15:13 -08001618 private boolean addViewsToTempLocation(ArrayList<View> views, Rect rectOccupiedByPotentialDrop,
Adam Cohenf3900c22012-11-16 18:28:11 -08001619 int[] direction, View dragView, ItemConfiguration currentState) {
Adam Cohen482ed822012-03-02 14:15:13 -08001620 if (views.size() == 0) return true;
Adam Cohen482ed822012-03-02 14:15:13 -08001621
Adam Cohen8baab352012-03-20 17:39:21 -07001622 boolean success = false;
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001623 Rect boundingRect = new Rect();
Adam Cohen8baab352012-03-20 17:39:21 -07001624 // We construct a rect which represents the entire group of views passed in
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001625 currentState.getBoundingRectForViews(views, boundingRect);
Adam Cohen8baab352012-03-20 17:39:21 -07001626
Adam Cohen8baab352012-03-20 17:39:21 -07001627 // Mark the occupied state as false for the group of views we want to move.
Adam Cohenf3900c22012-11-16 18:28:11 -08001628 for (View v: views) {
Adam Cohen8baab352012-03-20 17:39:21 -07001629 CellAndSpan c = currentState.map.get(v);
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001630 mTmpOccupied.markCells(c, false);
Adam Cohen8baab352012-03-20 17:39:21 -07001631 }
1632
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001633 GridOccupancy blockOccupied = new GridOccupancy(boundingRect.width(), boundingRect.height());
Adam Cohen47a876d2012-03-19 13:21:41 -07001634 int top = boundingRect.top;
1635 int left = boundingRect.left;
Adam Cohen8baab352012-03-20 17:39:21 -07001636 // We mark more precisely which parts of the bounding rect are truly occupied, allowing
Adam Cohena56dc102012-07-13 13:41:42 -07001637 // for interlocking.
Adam Cohenf3900c22012-11-16 18:28:11 -08001638 for (View v: views) {
Adam Cohen8baab352012-03-20 17:39:21 -07001639 CellAndSpan c = currentState.map.get(v);
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001640 blockOccupied.markCells(c.cellX - left, c.cellY - top, c.spanX, c.spanY, true);
Adam Cohen47a876d2012-03-19 13:21:41 -07001641 }
1642
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001643 mTmpOccupied.markCells(rectOccupiedByPotentialDrop, true);
Adam Cohen482ed822012-03-02 14:15:13 -08001644
Adam Cohenf3900c22012-11-16 18:28:11 -08001645 findNearestArea(boundingRect.left, boundingRect.top, boundingRect.width(),
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001646 boundingRect.height(), direction,
1647 mTmpOccupied.cells, blockOccupied.cells, mTempLocation);
Adam Cohen482ed822012-03-02 14:15:13 -08001648
Adam Cohen8baab352012-03-20 17:39:21 -07001649 // If we successfuly found a location by pushing the block of views, we commit it
Adam Cohen482ed822012-03-02 14:15:13 -08001650 if (mTempLocation[0] >= 0 && mTempLocation[1] >= 0) {
Adam Cohen8baab352012-03-20 17:39:21 -07001651 int deltaX = mTempLocation[0] - boundingRect.left;
1652 int deltaY = mTempLocation[1] - boundingRect.top;
Adam Cohenf3900c22012-11-16 18:28:11 -08001653 for (View v: views) {
Adam Cohen8baab352012-03-20 17:39:21 -07001654 CellAndSpan c = currentState.map.get(v);
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001655 c.cellX += deltaX;
1656 c.cellY += deltaY;
Adam Cohen482ed822012-03-02 14:15:13 -08001657 }
1658 success = true;
1659 }
Adam Cohen8baab352012-03-20 17:39:21 -07001660
1661 // In either case, we set the occupied array as marked for the location of the views
Adam Cohenf3900c22012-11-16 18:28:11 -08001662 for (View v: views) {
Adam Cohen8baab352012-03-20 17:39:21 -07001663 CellAndSpan c = currentState.map.get(v);
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001664 mTmpOccupied.markCells(c, true);
Adam Cohen482ed822012-03-02 14:15:13 -08001665 }
1666 return success;
1667 }
1668
Adam Cohen4abc5bd2012-05-29 21:06:03 -07001669 // This method tries to find a reordering solution which satisfies the push mechanic by trying
1670 // to push items in each of the cardinal directions, in an order based on the direction vector
1671 // passed.
1672 private boolean attemptPushInDirection(ArrayList<View> intersectingViews, Rect occupied,
1673 int[] direction, View ignoreView, ItemConfiguration solution) {
1674 if ((Math.abs(direction[0]) + Math.abs(direction[1])) > 1) {
Winson Chung5f8afe62013-08-12 16:19:28 -07001675 // If the direction vector has two non-zero components, we try pushing
Adam Cohen4abc5bd2012-05-29 21:06:03 -07001676 // separately in each of the components.
1677 int temp = direction[1];
1678 direction[1] = 0;
Adam Cohenf3900c22012-11-16 18:28:11 -08001679
1680 if (pushViewsToTempLocation(intersectingViews, occupied, direction,
Adam Cohen4abc5bd2012-05-29 21:06:03 -07001681 ignoreView, solution)) {
1682 return true;
1683 }
1684 direction[1] = temp;
1685 temp = direction[0];
1686 direction[0] = 0;
Adam Cohenf3900c22012-11-16 18:28:11 -08001687
1688 if (pushViewsToTempLocation(intersectingViews, occupied, direction,
Adam Cohen4abc5bd2012-05-29 21:06:03 -07001689 ignoreView, solution)) {
1690 return true;
1691 }
1692 // Revert the direction
1693 direction[0] = temp;
1694
1695 // Now we try pushing in each component of the opposite direction
1696 direction[0] *= -1;
1697 direction[1] *= -1;
1698 temp = direction[1];
1699 direction[1] = 0;
Adam Cohenf3900c22012-11-16 18:28:11 -08001700 if (pushViewsToTempLocation(intersectingViews, occupied, direction,
Adam Cohen4abc5bd2012-05-29 21:06:03 -07001701 ignoreView, solution)) {
1702 return true;
1703 }
1704
1705 direction[1] = temp;
1706 temp = direction[0];
1707 direction[0] = 0;
Adam Cohenf3900c22012-11-16 18:28:11 -08001708 if (pushViewsToTempLocation(intersectingViews, occupied, direction,
Adam Cohen4abc5bd2012-05-29 21:06:03 -07001709 ignoreView, solution)) {
1710 return true;
1711 }
1712 // revert the direction
1713 direction[0] = temp;
1714 direction[0] *= -1;
1715 direction[1] *= -1;
Winson Chung5f8afe62013-08-12 16:19:28 -07001716
Adam Cohen4abc5bd2012-05-29 21:06:03 -07001717 } else {
1718 // If the direction vector has a single non-zero component, we push first in the
1719 // direction of the vector
Adam Cohenf3900c22012-11-16 18:28:11 -08001720 if (pushViewsToTempLocation(intersectingViews, occupied, direction,
Adam Cohen4abc5bd2012-05-29 21:06:03 -07001721 ignoreView, solution)) {
1722 return true;
1723 }
Adam Cohen4abc5bd2012-05-29 21:06:03 -07001724 // Then we try the opposite direction
1725 direction[0] *= -1;
1726 direction[1] *= -1;
Adam Cohenf3900c22012-11-16 18:28:11 -08001727 if (pushViewsToTempLocation(intersectingViews, occupied, direction,
Adam Cohen4abc5bd2012-05-29 21:06:03 -07001728 ignoreView, solution)) {
1729 return true;
1730 }
1731 // Switch the direction back
1732 direction[0] *= -1;
1733 direction[1] *= -1;
Winson Chung5f8afe62013-08-12 16:19:28 -07001734
1735 // If we have failed to find a push solution with the above, then we try
Adam Cohen4abc5bd2012-05-29 21:06:03 -07001736 // to find a solution by pushing along the perpendicular axis.
1737
1738 // Swap the components
1739 int temp = direction[1];
1740 direction[1] = direction[0];
1741 direction[0] = temp;
Adam Cohenf3900c22012-11-16 18:28:11 -08001742 if (pushViewsToTempLocation(intersectingViews, occupied, direction,
Adam Cohen4abc5bd2012-05-29 21:06:03 -07001743 ignoreView, solution)) {
1744 return true;
1745 }
1746
1747 // Then we try the opposite direction
1748 direction[0] *= -1;
1749 direction[1] *= -1;
Adam Cohenf3900c22012-11-16 18:28:11 -08001750 if (pushViewsToTempLocation(intersectingViews, occupied, direction,
Adam Cohen4abc5bd2012-05-29 21:06:03 -07001751 ignoreView, solution)) {
1752 return true;
1753 }
1754 // Switch the direction back
1755 direction[0] *= -1;
1756 direction[1] *= -1;
1757
1758 // Swap the components back
1759 temp = direction[1];
1760 direction[1] = direction[0];
1761 direction[0] = temp;
1762 }
1763 return false;
1764 }
1765
Adam Cohen482ed822012-03-02 14:15:13 -08001766 private boolean rearrangementExists(int cellX, int cellY, int spanX, int spanY, int[] direction,
Adam Cohen8baab352012-03-20 17:39:21 -07001767 View ignoreView, ItemConfiguration solution) {
Winson Chunge3e03bc2012-05-01 15:10:11 -07001768 // Return early if get invalid cell positions
1769 if (cellX < 0 || cellY < 0) return false;
Adam Cohen482ed822012-03-02 14:15:13 -08001770
Adam Cohen8baab352012-03-20 17:39:21 -07001771 mIntersectingViews.clear();
Adam Cohen482ed822012-03-02 14:15:13 -08001772 mOccupiedRect.set(cellX, cellY, cellX + spanX, cellY + spanY);
Adam Cohen482ed822012-03-02 14:15:13 -08001773
Adam Cohen8baab352012-03-20 17:39:21 -07001774 // Mark the desired location of the view currently being dragged.
Adam Cohen482ed822012-03-02 14:15:13 -08001775 if (ignoreView != null) {
Adam Cohen8baab352012-03-20 17:39:21 -07001776 CellAndSpan c = solution.map.get(ignoreView);
Adam Cohen19f37922012-03-21 11:59:11 -07001777 if (c != null) {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001778 c.cellX = cellX;
1779 c.cellY = cellY;
Adam Cohen19f37922012-03-21 11:59:11 -07001780 }
Adam Cohen482ed822012-03-02 14:15:13 -08001781 }
Adam Cohen482ed822012-03-02 14:15:13 -08001782 Rect r0 = new Rect(cellX, cellY, cellX + spanX, cellY + spanY);
1783 Rect r1 = new Rect();
Adam Cohen8baab352012-03-20 17:39:21 -07001784 for (View child: solution.map.keySet()) {
Adam Cohen482ed822012-03-02 14:15:13 -08001785 if (child == ignoreView) continue;
Adam Cohen8baab352012-03-20 17:39:21 -07001786 CellAndSpan c = solution.map.get(child);
Adam Cohen482ed822012-03-02 14:15:13 -08001787 LayoutParams lp = (LayoutParams) child.getLayoutParams();
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001788 r1.set(c.cellX, c.cellY, c.cellX + c.spanX, c.cellY + c.spanY);
Adam Cohen482ed822012-03-02 14:15:13 -08001789 if (Rect.intersects(r0, r1)) {
1790 if (!lp.canReorder) {
1791 return false;
1792 }
1793 mIntersectingViews.add(child);
1794 }
1795 }
Adam Cohen47a876d2012-03-19 13:21:41 -07001796
Adam Cohenfa3c58f2013-12-06 16:10:55 -08001797 solution.intersectingViews = new ArrayList<View>(mIntersectingViews);
1798
Winson Chung5f8afe62013-08-12 16:19:28 -07001799 // First we try to find a solution which respects the push mechanic. That is,
Adam Cohen4abc5bd2012-05-29 21:06:03 -07001800 // we try to find a solution such that no displaced item travels through another item
1801 // without also displacing that item.
1802 if (attemptPushInDirection(mIntersectingViews, mOccupiedRect, direction, ignoreView,
Adam Cohen19f37922012-03-21 11:59:11 -07001803 solution)) {
Adam Cohen47a876d2012-03-19 13:21:41 -07001804 return true;
1805 }
Adam Cohen47a876d2012-03-19 13:21:41 -07001806
Adam Cohen4abc5bd2012-05-29 21:06:03 -07001807 // Next we try moving the views as a block, but without requiring the push mechanic.
Adam Cohenf3900c22012-11-16 18:28:11 -08001808 if (addViewsToTempLocation(mIntersectingViews, mOccupiedRect, direction, ignoreView,
Adam Cohen19f37922012-03-21 11:59:11 -07001809 solution)) {
Adam Cohen482ed822012-03-02 14:15:13 -08001810 return true;
1811 }
Adam Cohen47a876d2012-03-19 13:21:41 -07001812
Adam Cohen482ed822012-03-02 14:15:13 -08001813 // Ok, they couldn't move as a block, let's move them individually
1814 for (View v : mIntersectingViews) {
Adam Cohen8baab352012-03-20 17:39:21 -07001815 if (!addViewToTempLocation(v, mOccupiedRect, direction, solution)) {
Adam Cohen482ed822012-03-02 14:15:13 -08001816 return false;
1817 }
1818 }
1819 return true;
1820 }
1821
1822 /*
1823 * Returns a pair (x, y), where x,y are in {-1, 0, 1} corresponding to vector between
1824 * the provided point and the provided cell
1825 */
Adam Cohen47a876d2012-03-19 13:21:41 -07001826 private void computeDirectionVector(float deltaX, float deltaY, int[] result) {
Jon Mirandae96798e2016-12-07 12:10:44 -08001827 double angle = Math.atan(deltaY / deltaX);
Adam Cohen482ed822012-03-02 14:15:13 -08001828
1829 result[0] = 0;
1830 result[1] = 0;
1831 if (Math.abs(Math.cos(angle)) > 0.5f) {
1832 result[0] = (int) Math.signum(deltaX);
1833 }
1834 if (Math.abs(Math.sin(angle)) > 0.5f) {
1835 result[1] = (int) Math.signum(deltaY);
1836 }
1837 }
1838
Sunny Goyalf7a29e82015-04-24 15:20:43 -07001839 private ItemConfiguration findReorderSolution(int pixelX, int pixelY, int minSpanX, int minSpanY,
Adam Cohenfa3c58f2013-12-06 16:10:55 -08001840 int spanX, int spanY, int[] direction, View dragView, boolean decX,
1841 ItemConfiguration solution) {
Adam Cohen8baab352012-03-20 17:39:21 -07001842 // Copy the current state into the solution. This solution will be manipulated as necessary.
1843 copyCurrentStateToSolution(solution, false);
1844 // Copy the current occupied array into the temporary occupied array. This array will be
1845 // manipulated as necessary to find a solution.
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001846 mOccupied.copyTo(mTmpOccupied);
Adam Cohen482ed822012-03-02 14:15:13 -08001847
1848 // We find the nearest cell into which we would place the dragged item, assuming there's
1849 // nothing in its way.
1850 int result[] = new int[2];
1851 result = findNearestArea(pixelX, pixelY, spanX, spanY, result);
1852
1853 boolean success = false;
1854 // First we try the exact nearest position of the item being dragged,
1855 // we will then want to try to move this around to other neighbouring positions
Adam Cohen8baab352012-03-20 17:39:21 -07001856 success = rearrangementExists(result[0], result[1], spanX, spanY, direction, dragView,
1857 solution);
Adam Cohen482ed822012-03-02 14:15:13 -08001858
1859 if (!success) {
1860 // We try shrinking the widget down to size in an alternating pattern, shrink 1 in
1861 // x, then 1 in y etc.
1862 if (spanX > minSpanX && (minSpanY == spanY || decX)) {
Adam Cohenfa3c58f2013-12-06 16:10:55 -08001863 return findReorderSolution(pixelX, pixelY, minSpanX, minSpanY, spanX - 1, spanY,
1864 direction, dragView, false, solution);
Adam Cohen482ed822012-03-02 14:15:13 -08001865 } else if (spanY > minSpanY) {
Adam Cohenfa3c58f2013-12-06 16:10:55 -08001866 return findReorderSolution(pixelX, pixelY, minSpanX, minSpanY, spanX, spanY - 1,
1867 direction, dragView, true, solution);
Adam Cohen482ed822012-03-02 14:15:13 -08001868 }
1869 solution.isSolution = false;
1870 } else {
1871 solution.isSolution = true;
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001872 solution.cellX = result[0];
1873 solution.cellY = result[1];
1874 solution.spanX = spanX;
1875 solution.spanY = spanY;
Adam Cohen482ed822012-03-02 14:15:13 -08001876 }
1877 return solution;
1878 }
1879
1880 private void copyCurrentStateToSolution(ItemConfiguration solution, boolean temp) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07001881 int childCount = mShortcutsAndWidgets.getChildCount();
Adam Cohen482ed822012-03-02 14:15:13 -08001882 for (int i = 0; i < childCount; i++) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07001883 View child = mShortcutsAndWidgets.getChildAt(i);
Adam Cohen482ed822012-03-02 14:15:13 -08001884 LayoutParams lp = (LayoutParams) child.getLayoutParams();
Adam Cohen8baab352012-03-20 17:39:21 -07001885 CellAndSpan c;
Adam Cohen482ed822012-03-02 14:15:13 -08001886 if (temp) {
Adam Cohen8baab352012-03-20 17:39:21 -07001887 c = new CellAndSpan(lp.tmpCellX, lp.tmpCellY, lp.cellHSpan, lp.cellVSpan);
Adam Cohen482ed822012-03-02 14:15:13 -08001888 } else {
Adam Cohen8baab352012-03-20 17:39:21 -07001889 c = new CellAndSpan(lp.cellX, lp.cellY, lp.cellHSpan, lp.cellVSpan);
Adam Cohen482ed822012-03-02 14:15:13 -08001890 }
Adam Cohenf3900c22012-11-16 18:28:11 -08001891 solution.add(child, c);
Adam Cohen482ed822012-03-02 14:15:13 -08001892 }
1893 }
1894
1895 private void copySolutionToTempState(ItemConfiguration solution, View dragView) {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001896 mTmpOccupied.clear();
Adam Cohen482ed822012-03-02 14:15:13 -08001897
Michael Jurkaa52570f2012-03-20 03:18:20 -07001898 int childCount = mShortcutsAndWidgets.getChildCount();
Adam Cohen482ed822012-03-02 14:15:13 -08001899 for (int i = 0; i < childCount; i++) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07001900 View child = mShortcutsAndWidgets.getChildAt(i);
Adam Cohen482ed822012-03-02 14:15:13 -08001901 if (child == dragView) continue;
1902 LayoutParams lp = (LayoutParams) child.getLayoutParams();
Adam Cohen8baab352012-03-20 17:39:21 -07001903 CellAndSpan c = solution.map.get(child);
1904 if (c != null) {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001905 lp.tmpCellX = c.cellX;
1906 lp.tmpCellY = c.cellY;
Adam Cohen8baab352012-03-20 17:39:21 -07001907 lp.cellHSpan = c.spanX;
1908 lp.cellVSpan = c.spanY;
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001909 mTmpOccupied.markCells(c, true);
Adam Cohen482ed822012-03-02 14:15:13 -08001910 }
1911 }
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001912 mTmpOccupied.markCells(solution, true);
Adam Cohen482ed822012-03-02 14:15:13 -08001913 }
1914
1915 private void animateItemsToSolution(ItemConfiguration solution, View dragView, boolean
1916 commitDragView) {
1917
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001918 GridOccupancy occupied = DESTRUCTIVE_REORDER ? mOccupied : mTmpOccupied;
1919 occupied.clear();
Adam Cohen482ed822012-03-02 14:15:13 -08001920
Michael Jurkaa52570f2012-03-20 03:18:20 -07001921 int childCount = mShortcutsAndWidgets.getChildCount();
Adam Cohen482ed822012-03-02 14:15:13 -08001922 for (int i = 0; i < childCount; i++) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07001923 View child = mShortcutsAndWidgets.getChildAt(i);
Adam Cohen482ed822012-03-02 14:15:13 -08001924 if (child == dragView) continue;
Adam Cohen8baab352012-03-20 17:39:21 -07001925 CellAndSpan c = solution.map.get(child);
1926 if (c != null) {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001927 animateChildToPosition(child, c.cellX, c.cellY, REORDER_ANIMATION_DURATION, 0,
Adam Cohen19f37922012-03-21 11:59:11 -07001928 DESTRUCTIVE_REORDER, false);
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001929 occupied.markCells(c, true);
Adam Cohen482ed822012-03-02 14:15:13 -08001930 }
1931 }
1932 if (commitDragView) {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001933 occupied.markCells(solution, true);
Adam Cohen482ed822012-03-02 14:15:13 -08001934 }
1935 }
1936
Adam Cohenfa3c58f2013-12-06 16:10:55 -08001937
1938 // This method starts or changes the reorder preview animations
1939 private void beginOrAdjustReorderPreviewAnimations(ItemConfiguration solution,
1940 View dragView, int delay, int mode) {
Adam Cohen19f37922012-03-21 11:59:11 -07001941 int childCount = mShortcutsAndWidgets.getChildCount();
Adam Cohen19f37922012-03-21 11:59:11 -07001942 for (int i = 0; i < childCount; i++) {
1943 View child = mShortcutsAndWidgets.getChildAt(i);
1944 if (child == dragView) continue;
1945 CellAndSpan c = solution.map.get(child);
Adam Cohenfa3c58f2013-12-06 16:10:55 -08001946 boolean skip = mode == ReorderPreviewAnimation.MODE_HINT && solution.intersectingViews
1947 != null && !solution.intersectingViews.contains(child);
1948
Adam Cohen19f37922012-03-21 11:59:11 -07001949 LayoutParams lp = (LayoutParams) child.getLayoutParams();
Adam Cohenfa3c58f2013-12-06 16:10:55 -08001950 if (c != null && !skip) {
1951 ReorderPreviewAnimation rha = new ReorderPreviewAnimation(child, mode, lp.cellX,
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001952 lp.cellY, c.cellX, c.cellY, c.spanX, c.spanY);
Adam Cohend024f982012-05-23 18:26:45 -07001953 rha.animate();
Adam Cohen19f37922012-03-21 11:59:11 -07001954 }
1955 }
1956 }
1957
Adam Cohenfa3c58f2013-12-06 16:10:55 -08001958 // Class which represents the reorder preview animations. These animations show that an item is
Adam Cohen19f37922012-03-21 11:59:11 -07001959 // in a temporary state, and hint at where the item will return to.
Adam Cohenfa3c58f2013-12-06 16:10:55 -08001960 class ReorderPreviewAnimation {
Adam Cohen19f37922012-03-21 11:59:11 -07001961 View child;
Adam Cohend024f982012-05-23 18:26:45 -07001962 float finalDeltaX;
1963 float finalDeltaY;
1964 float initDeltaX;
1965 float initDeltaY;
1966 float finalScale;
1967 float initScale;
Adam Cohenfa3c58f2013-12-06 16:10:55 -08001968 int mode;
1969 boolean repeating = false;
1970 private static final int PREVIEW_DURATION = 300;
1971 private static final int HINT_DURATION = Workspace.REORDER_TIMEOUT;
1972
Jon Miranda21266912016-12-19 14:12:05 -08001973 private static final float CHILD_DIVIDEND = 4.0f;
1974
Adam Cohenfa3c58f2013-12-06 16:10:55 -08001975 public static final int MODE_HINT = 0;
1976 public static final int MODE_PREVIEW = 1;
1977
Adam Cohene7587d22012-05-24 18:50:02 -07001978 Animator a;
Adam Cohen19f37922012-03-21 11:59:11 -07001979
Adam Cohenfa3c58f2013-12-06 16:10:55 -08001980 public ReorderPreviewAnimation(View child, int mode, int cellX0, int cellY0, int cellX1,
1981 int cellY1, int spanX, int spanY) {
Adam Cohen19f37922012-03-21 11:59:11 -07001982 regionToCenterPoint(cellX0, cellY0, spanX, spanY, mTmpPoint);
1983 final int x0 = mTmpPoint[0];
1984 final int y0 = mTmpPoint[1];
1985 regionToCenterPoint(cellX1, cellY1, spanX, spanY, mTmpPoint);
1986 final int x1 = mTmpPoint[0];
1987 final int y1 = mTmpPoint[1];
1988 final int dX = x1 - x0;
1989 final int dY = y1 - y0;
Jon Miranda21266912016-12-19 14:12:05 -08001990
1991 this.child = child;
1992 this.mode = mode;
1993 setInitialAnimationValues(false);
1994 finalScale = (mChildScale - (CHILD_DIVIDEND / child.getWidth())) * initScale;
1995 finalDeltaX = initDeltaX;
1996 finalDeltaY = initDeltaY;
Adam Cohenfa3c58f2013-12-06 16:10:55 -08001997 int dir = mode == MODE_HINT ? -1 : 1;
Adam Cohen19f37922012-03-21 11:59:11 -07001998 if (dX == dY && dX == 0) {
1999 } else {
2000 if (dY == 0) {
Jon Miranda21266912016-12-19 14:12:05 -08002001 finalDeltaX += - dir * Math.signum(dX) * mReorderPreviewAnimationMagnitude;
Adam Cohen19f37922012-03-21 11:59:11 -07002002 } else if (dX == 0) {
Jon Miranda21266912016-12-19 14:12:05 -08002003 finalDeltaY += - dir * Math.signum(dY) * mReorderPreviewAnimationMagnitude;
Adam Cohen19f37922012-03-21 11:59:11 -07002004 } else {
2005 double angle = Math.atan( (float) (dY) / dX);
Jon Miranda21266912016-12-19 14:12:05 -08002006 finalDeltaX += (int) (- dir * Math.signum(dX) *
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002007 Math.abs(Math.cos(angle) * mReorderPreviewAnimationMagnitude));
Jon Miranda21266912016-12-19 14:12:05 -08002008 finalDeltaY += (int) (- dir * Math.signum(dY) *
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002009 Math.abs(Math.sin(angle) * mReorderPreviewAnimationMagnitude));
Adam Cohen19f37922012-03-21 11:59:11 -07002010 }
2011 }
Jon Miranda21266912016-12-19 14:12:05 -08002012 }
2013
2014 void setInitialAnimationValues(boolean restoreOriginalValues) {
2015 if (restoreOriginalValues) {
2016 if (child instanceof LauncherAppWidgetHostView) {
2017 LauncherAppWidgetHostView lahv = (LauncherAppWidgetHostView) child;
2018 initScale = lahv.getScaleToFit();
2019 initDeltaX = lahv.getTranslationForCentering().x;
2020 initDeltaY = lahv.getTranslationForCentering().y;
2021 } else {
2022 initScale = mChildScale;
2023 initDeltaX = 0;
2024 initDeltaY = 0;
2025 }
2026 } else {
2027 initScale = child.getScaleX();
2028 initDeltaX = child.getTranslationX();
2029 initDeltaY = child.getTranslationY();
2030 }
Adam Cohen19f37922012-03-21 11:59:11 -07002031 }
2032
Adam Cohend024f982012-05-23 18:26:45 -07002033 void animate() {
Jon Miranda21266912016-12-19 14:12:05 -08002034 boolean noMovement = (finalDeltaX == initDeltaX) && (finalDeltaY == initDeltaY);
2035
Adam Cohen19f37922012-03-21 11:59:11 -07002036 if (mShakeAnimators.containsKey(child)) {
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002037 ReorderPreviewAnimation oldAnimation = mShakeAnimators.get(child);
Adam Cohend024f982012-05-23 18:26:45 -07002038 oldAnimation.cancel();
Adam Cohen19f37922012-03-21 11:59:11 -07002039 mShakeAnimators.remove(child);
Jon Miranda21266912016-12-19 14:12:05 -08002040 if (noMovement) {
Adam Cohene7587d22012-05-24 18:50:02 -07002041 completeAnimationImmediately();
2042 return;
2043 }
Adam Cohen19f37922012-03-21 11:59:11 -07002044 }
Jon Miranda21266912016-12-19 14:12:05 -08002045 if (noMovement) {
Adam Cohen19f37922012-03-21 11:59:11 -07002046 return;
2047 }
Jon Mirandacda3bfb2017-02-06 15:54:41 -08002048 ValueAnimator va = LauncherAnimUtils.ofFloat(0f, 1f);
Adam Cohene7587d22012-05-24 18:50:02 -07002049 a = va;
Tony Wickham9e0702f2015-09-02 14:45:39 -07002050
2051 // Animations are disabled in power save mode, causing the repeated animation to jump
2052 // spastically between beginning and end states. Since this looks bad, we don't repeat
2053 // the animation in power save mode.
Tony Wickham112ac952015-11-12 12:31:50 -08002054 if (!Utilities.isPowerSaverOn(getContext())) {
Tony Wickham9e0702f2015-09-02 14:45:39 -07002055 va.setRepeatMode(ValueAnimator.REVERSE);
2056 va.setRepeatCount(ValueAnimator.INFINITE);
2057 }
2058
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002059 va.setDuration(mode == MODE_HINT ? HINT_DURATION : PREVIEW_DURATION);
Adam Cohend024f982012-05-23 18:26:45 -07002060 va.setStartDelay((int) (Math.random() * 60));
Adam Cohen19f37922012-03-21 11:59:11 -07002061 va.addUpdateListener(new AnimatorUpdateListener() {
2062 @Override
2063 public void onAnimationUpdate(ValueAnimator animation) {
Jon Mirandae96798e2016-12-07 12:10:44 -08002064 float r = (Float) animation.getAnimatedValue();
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002065 float r1 = (mode == MODE_HINT && repeating) ? 1.0f : r;
2066 float x = r1 * finalDeltaX + (1 - r1) * initDeltaX;
2067 float y = r1 * finalDeltaY + (1 - r1) * initDeltaY;
Adam Cohen19f37922012-03-21 11:59:11 -07002068 child.setTranslationX(x);
2069 child.setTranslationY(y);
Adam Cohend024f982012-05-23 18:26:45 -07002070 float s = r * finalScale + (1 - r) * initScale;
Brandon Keely50e6e562012-05-08 16:28:49 -07002071 child.setScaleX(s);
2072 child.setScaleY(s);
Adam Cohen19f37922012-03-21 11:59:11 -07002073 }
2074 });
2075 va.addListener(new AnimatorListenerAdapter() {
2076 public void onAnimationRepeat(Animator animation) {
Adam Cohen19f37922012-03-21 11:59:11 -07002077 // We make sure to end only after a full period
Jon Miranda21266912016-12-19 14:12:05 -08002078 setInitialAnimationValues(true);
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002079 repeating = true;
Adam Cohen19f37922012-03-21 11:59:11 -07002080 }
2081 });
Adam Cohen19f37922012-03-21 11:59:11 -07002082 mShakeAnimators.put(child, this);
2083 va.start();
2084 }
2085
Adam Cohend024f982012-05-23 18:26:45 -07002086 private void cancel() {
Adam Cohene7587d22012-05-24 18:50:02 -07002087 if (a != null) {
2088 a.cancel();
2089 }
Adam Cohen19f37922012-03-21 11:59:11 -07002090 }
Adam Cohene7587d22012-05-24 18:50:02 -07002091
Adam Cohen091440a2015-03-18 14:16:05 -07002092 @Thunk void completeAnimationImmediately() {
Adam Cohene7587d22012-05-24 18:50:02 -07002093 if (a != null) {
2094 a.cancel();
2095 }
Brandon Keely50e6e562012-05-08 16:28:49 -07002096
Jon Miranda72dbd912017-01-04 14:03:07 -08002097 setInitialAnimationValues(true);
Sunny Goyal9e76f682017-02-13 12:13:43 -08002098 a = LauncherAnimUtils.ofPropertyValuesHolder(child,
2099 new PropertyListBuilder()
2100 .scale(initScale)
2101 .translationX(initDeltaX)
2102 .translationY(initDeltaY)
2103 .build())
2104 .setDuration(REORDER_ANIMATION_DURATION);
Sunny Goyal5d2fc322015-07-06 22:52:49 -07002105 a.setInterpolator(new android.view.animation.DecelerateInterpolator(1.5f));
2106 a.start();
Brandon Keely50e6e562012-05-08 16:28:49 -07002107 }
Adam Cohen19f37922012-03-21 11:59:11 -07002108 }
2109
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002110 private void completeAndClearReorderPreviewAnimations() {
2111 for (ReorderPreviewAnimation a: mShakeAnimators.values()) {
Brandon Keely50e6e562012-05-08 16:28:49 -07002112 a.completeAnimationImmediately();
Adam Cohen19f37922012-03-21 11:59:11 -07002113 }
2114 mShakeAnimators.clear();
2115 }
2116
Adam Cohen482ed822012-03-02 14:15:13 -08002117 private void commitTempPlacement() {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07002118 mTmpOccupied.copyTo(mOccupied);
Sunny Goyalaa8ef112015-06-12 20:04:41 -07002119
2120 long screenId = mLauncher.getWorkspace().getIdForScreen(this);
2121 int container = Favorites.CONTAINER_DESKTOP;
2122
Sunny Goyalc13403c2016-11-18 23:44:48 -08002123 if (mContainerType == HOTSEAT) {
Sunny Goyalaa8ef112015-06-12 20:04:41 -07002124 screenId = -1;
2125 container = Favorites.CONTAINER_HOTSEAT;
2126 }
2127
Michael Jurkaa52570f2012-03-20 03:18:20 -07002128 int childCount = mShortcutsAndWidgets.getChildCount();
Adam Cohen482ed822012-03-02 14:15:13 -08002129 for (int i = 0; i < childCount; i++) {
Adam Cohenea889a22012-03-27 16:45:39 -07002130 View child = mShortcutsAndWidgets.getChildAt(i);
2131 LayoutParams lp = (LayoutParams) child.getLayoutParams();
2132 ItemInfo info = (ItemInfo) child.getTag();
Adam Cohen2acce882012-03-28 19:03:19 -07002133 // We do a null check here because the item info can be null in the case of the
2134 // AllApps button in the hotseat.
2135 if (info != null) {
Sunny Goyalaa8ef112015-06-12 20:04:41 -07002136 final boolean requiresDbUpdate = (info.cellX != lp.tmpCellX
2137 || info.cellY != lp.tmpCellY || info.spanX != lp.cellHSpan
2138 || info.spanY != lp.cellVSpan);
2139
Adam Cohen2acce882012-03-28 19:03:19 -07002140 info.cellX = lp.cellX = lp.tmpCellX;
2141 info.cellY = lp.cellY = lp.tmpCellY;
Adam Cohenbebf0422012-04-11 18:06:28 -07002142 info.spanX = lp.cellHSpan;
2143 info.spanY = lp.cellVSpan;
Sunny Goyalaa8ef112015-06-12 20:04:41 -07002144
2145 if (requiresDbUpdate) {
Sunny Goyal43bf11d2017-02-02 13:52:53 -08002146 mLauncher.getModelWriter().modifyItemInDatabase(info, container, screenId,
Sunny Goyalaa8ef112015-06-12 20:04:41 -07002147 info.cellX, info.cellY, info.spanX, info.spanY);
2148 }
Adam Cohen2acce882012-03-28 19:03:19 -07002149 }
Adam Cohen482ed822012-03-02 14:15:13 -08002150 }
2151 }
2152
Sunny Goyalf7a29e82015-04-24 15:20:43 -07002153 private void setUseTempCoords(boolean useTempCoords) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07002154 int childCount = mShortcutsAndWidgets.getChildCount();
Adam Cohen482ed822012-03-02 14:15:13 -08002155 for (int i = 0; i < childCount; i++) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07002156 LayoutParams lp = (LayoutParams) mShortcutsAndWidgets.getChildAt(i).getLayoutParams();
Adam Cohen482ed822012-03-02 14:15:13 -08002157 lp.useTmpCoords = useTempCoords;
2158 }
2159 }
2160
Sunny Goyalf7a29e82015-04-24 15:20:43 -07002161 private ItemConfiguration findConfigurationNoShuffle(int pixelX, int pixelY, int minSpanX, int minSpanY,
Adam Cohen482ed822012-03-02 14:15:13 -08002162 int spanX, int spanY, View dragView, ItemConfiguration solution) {
2163 int[] result = new int[2];
2164 int[] resultSpan = new int[2];
Sunny Goyalf7a29e82015-04-24 15:20:43 -07002165 findNearestVacantArea(pixelX, pixelY, minSpanX, minSpanY, spanX, spanY, result,
Adam Cohen482ed822012-03-02 14:15:13 -08002166 resultSpan);
2167 if (result[0] >= 0 && result[1] >= 0) {
2168 copyCurrentStateToSolution(solution, false);
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07002169 solution.cellX = result[0];
2170 solution.cellY = result[1];
2171 solution.spanX = resultSpan[0];
2172 solution.spanY = resultSpan[1];
Adam Cohen482ed822012-03-02 14:15:13 -08002173 solution.isSolution = true;
2174 } else {
2175 solution.isSolution = false;
2176 }
2177 return solution;
2178 }
2179
Adam Cohen19f37922012-03-21 11:59:11 -07002180 /* This seems like it should be obvious and straight-forward, but when the direction vector
2181 needs to match with the notion of the dragView pushing other views, we have to employ
2182 a slightly more subtle notion of the direction vector. The question is what two points is
2183 the vector between? The center of the dragView and its desired destination? Not quite, as
2184 this doesn't necessarily coincide with the interaction of the dragView and items occupying
2185 those cells. Instead we use some heuristics to often lock the vector to up, down, left
2186 or right, which helps make pushing feel right.
2187 */
2188 private void getDirectionVectorForDrop(int dragViewCenterX, int dragViewCenterY, int spanX,
2189 int spanY, View dragView, int[] resultDirection) {
2190 int[] targetDestination = new int[2];
2191
2192 findNearestArea(dragViewCenterX, dragViewCenterY, spanX, spanY, targetDestination);
2193 Rect dragRect = new Rect();
2194 regionToRect(targetDestination[0], targetDestination[1], spanX, spanY, dragRect);
2195 dragRect.offset(dragViewCenterX - dragRect.centerX(), dragViewCenterY - dragRect.centerY());
2196
2197 Rect dropRegionRect = new Rect();
2198 getViewsIntersectingRegion(targetDestination[0], targetDestination[1], spanX, spanY,
2199 dragView, dropRegionRect, mIntersectingViews);
2200
2201 int dropRegionSpanX = dropRegionRect.width();
2202 int dropRegionSpanY = dropRegionRect.height();
2203
2204 regionToRect(dropRegionRect.left, dropRegionRect.top, dropRegionRect.width(),
2205 dropRegionRect.height(), dropRegionRect);
2206
2207 int deltaX = (dropRegionRect.centerX() - dragViewCenterX) / spanX;
2208 int deltaY = (dropRegionRect.centerY() - dragViewCenterY) / spanY;
2209
2210 if (dropRegionSpanX == mCountX || spanX == mCountX) {
2211 deltaX = 0;
2212 }
2213 if (dropRegionSpanY == mCountY || spanY == mCountY) {
2214 deltaY = 0;
2215 }
2216
2217 if (deltaX == 0 && deltaY == 0) {
2218 // No idea what to do, give a random direction.
2219 resultDirection[0] = 1;
2220 resultDirection[1] = 0;
2221 } else {
2222 computeDirectionVector(deltaX, deltaY, resultDirection);
2223 }
2224 }
2225
2226 // For a given cell and span, fetch the set of views intersecting the region.
2227 private void getViewsIntersectingRegion(int cellX, int cellY, int spanX, int spanY,
2228 View dragView, Rect boundingRect, ArrayList<View> intersectingViews) {
2229 if (boundingRect != null) {
2230 boundingRect.set(cellX, cellY, cellX + spanX, cellY + spanY);
2231 }
2232 intersectingViews.clear();
2233 Rect r0 = new Rect(cellX, cellY, cellX + spanX, cellY + spanY);
2234 Rect r1 = new Rect();
2235 final int count = mShortcutsAndWidgets.getChildCount();
2236 for (int i = 0; i < count; i++) {
2237 View child = mShortcutsAndWidgets.getChildAt(i);
2238 if (child == dragView) continue;
2239 LayoutParams lp = (LayoutParams) child.getLayoutParams();
2240 r1.set(lp.cellX, lp.cellY, lp.cellX + lp.cellHSpan, lp.cellY + lp.cellVSpan);
2241 if (Rect.intersects(r0, r1)) {
2242 mIntersectingViews.add(child);
2243 if (boundingRect != null) {
2244 boundingRect.union(r1);
2245 }
2246 }
2247 }
2248 }
2249
2250 boolean isNearestDropLocationOccupied(int pixelX, int pixelY, int spanX, int spanY,
2251 View dragView, int[] result) {
2252 result = findNearestArea(pixelX, pixelY, spanX, spanY, result);
2253 getViewsIntersectingRegion(result[0], result[1], spanX, spanY, dragView, null,
2254 mIntersectingViews);
2255 return !mIntersectingViews.isEmpty();
2256 }
2257
2258 void revertTempState() {
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002259 completeAndClearReorderPreviewAnimations();
2260 if (isItemPlacementDirty() && !DESTRUCTIVE_REORDER) {
2261 final int count = mShortcutsAndWidgets.getChildCount();
2262 for (int i = 0; i < count; i++) {
2263 View child = mShortcutsAndWidgets.getChildAt(i);
2264 LayoutParams lp = (LayoutParams) child.getLayoutParams();
2265 if (lp.tmpCellX != lp.cellX || lp.tmpCellY != lp.cellY) {
2266 lp.tmpCellX = lp.cellX;
2267 lp.tmpCellY = lp.cellY;
2268 animateChildToPosition(child, lp.cellX, lp.cellY, REORDER_ANIMATION_DURATION,
2269 0, false, false);
2270 }
Adam Cohen19f37922012-03-21 11:59:11 -07002271 }
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002272 setItemPlacementDirty(false);
Adam Cohen19f37922012-03-21 11:59:11 -07002273 }
Adam Cohen19f37922012-03-21 11:59:11 -07002274 }
2275
Adam Cohenbebf0422012-04-11 18:06:28 -07002276 boolean createAreaForResize(int cellX, int cellY, int spanX, int spanY,
2277 View dragView, int[] direction, boolean commit) {
2278 int[] pixelXY = new int[2];
2279 regionToCenterPoint(cellX, cellY, spanX, spanY, pixelXY);
2280
2281 // First we determine if things have moved enough to cause a different layout
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002282 ItemConfiguration swapSolution = findReorderSolution(pixelXY[0], pixelXY[1], spanX, spanY,
Adam Cohenbebf0422012-04-11 18:06:28 -07002283 spanX, spanY, direction, dragView, true, new ItemConfiguration());
2284
2285 setUseTempCoords(true);
2286 if (swapSolution != null && swapSolution.isSolution) {
2287 // If we're just testing for a possible location (MODE_ACCEPT_DROP), we don't bother
2288 // committing anything or animating anything as we just want to determine if a solution
2289 // exists
2290 copySolutionToTempState(swapSolution, dragView);
2291 setItemPlacementDirty(true);
2292 animateItemsToSolution(swapSolution, dragView, commit);
2293
2294 if (commit) {
2295 commitTempPlacement();
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002296 completeAndClearReorderPreviewAnimations();
Adam Cohenbebf0422012-04-11 18:06:28 -07002297 setItemPlacementDirty(false);
2298 } else {
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002299 beginOrAdjustReorderPreviewAnimations(swapSolution, dragView,
2300 REORDER_ANIMATION_DURATION, ReorderPreviewAnimation.MODE_PREVIEW);
Adam Cohenbebf0422012-04-11 18:06:28 -07002301 }
2302 mShortcutsAndWidgets.requestLayout();
2303 }
2304 return swapSolution.isSolution;
2305 }
2306
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002307 int[] performReorder(int pixelX, int pixelY, int minSpanX, int minSpanY, int spanX, int spanY,
Adam Cohen482ed822012-03-02 14:15:13 -08002308 View dragView, int[] result, int resultSpan[], int mode) {
Adam Cohen482ed822012-03-02 14:15:13 -08002309 // First we determine if things have moved enough to cause a different layout
Adam Cohen47a876d2012-03-19 13:21:41 -07002310 result = findNearestArea(pixelX, pixelY, spanX, spanY, result);
Adam Cohen482ed822012-03-02 14:15:13 -08002311
2312 if (resultSpan == null) {
2313 resultSpan = new int[2];
2314 }
2315
Adam Cohen19f37922012-03-21 11:59:11 -07002316 // When we are checking drop validity or actually dropping, we don't recompute the
2317 // direction vector, since we want the solution to match the preview, and it's possible
2318 // that the exact position of the item has changed to result in a new reordering outcome.
Adam Cohenb209e632012-03-27 17:09:36 -07002319 if ((mode == MODE_ON_DROP || mode == MODE_ON_DROP_EXTERNAL || mode == MODE_ACCEPT_DROP)
2320 && mPreviousReorderDirection[0] != INVALID_DIRECTION) {
Adam Cohen19f37922012-03-21 11:59:11 -07002321 mDirectionVector[0] = mPreviousReorderDirection[0];
2322 mDirectionVector[1] = mPreviousReorderDirection[1];
2323 // We reset this vector after drop
Adam Cohenb209e632012-03-27 17:09:36 -07002324 if (mode == MODE_ON_DROP || mode == MODE_ON_DROP_EXTERNAL) {
2325 mPreviousReorderDirection[0] = INVALID_DIRECTION;
2326 mPreviousReorderDirection[1] = INVALID_DIRECTION;
Adam Cohen19f37922012-03-21 11:59:11 -07002327 }
2328 } else {
2329 getDirectionVectorForDrop(pixelX, pixelY, spanX, spanY, dragView, mDirectionVector);
2330 mPreviousReorderDirection[0] = mDirectionVector[0];
2331 mPreviousReorderDirection[1] = mDirectionVector[1];
2332 }
2333
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002334 // Find a solution involving pushing / displacing any items in the way
2335 ItemConfiguration swapSolution = findReorderSolution(pixelX, pixelY, minSpanX, minSpanY,
Adam Cohen482ed822012-03-02 14:15:13 -08002336 spanX, spanY, mDirectionVector, dragView, true, new ItemConfiguration());
2337
2338 // We attempt the approach which doesn't shuffle views at all
2339 ItemConfiguration noShuffleSolution = findConfigurationNoShuffle(pixelX, pixelY, minSpanX,
2340 minSpanY, spanX, spanY, dragView, new ItemConfiguration());
2341
2342 ItemConfiguration finalSolution = null;
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002343
2344 // If the reorder solution requires resizing (shrinking) the item being dropped, we instead
2345 // favor a solution in which the item is not resized, but
Adam Cohen482ed822012-03-02 14:15:13 -08002346 if (swapSolution.isSolution && swapSolution.area() >= noShuffleSolution.area()) {
2347 finalSolution = swapSolution;
2348 } else if (noShuffleSolution.isSolution) {
2349 finalSolution = noShuffleSolution;
2350 }
2351
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002352 if (mode == MODE_SHOW_REORDER_HINT) {
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002353 if (finalSolution != null) {
Adam Cohenfe692872013-12-11 14:47:23 -08002354 beginOrAdjustReorderPreviewAnimations(finalSolution, dragView, 0,
2355 ReorderPreviewAnimation.MODE_HINT);
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07002356 result[0] = finalSolution.cellX;
2357 result[1] = finalSolution.cellY;
2358 resultSpan[0] = finalSolution.spanX;
2359 resultSpan[1] = finalSolution.spanY;
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002360 } else {
2361 result[0] = result[1] = resultSpan[0] = resultSpan[1] = -1;
2362 }
2363 return result;
2364 }
2365
Adam Cohen482ed822012-03-02 14:15:13 -08002366 boolean foundSolution = true;
2367 if (!DESTRUCTIVE_REORDER) {
2368 setUseTempCoords(true);
2369 }
2370
2371 if (finalSolution != null) {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07002372 result[0] = finalSolution.cellX;
2373 result[1] = finalSolution.cellY;
2374 resultSpan[0] = finalSolution.spanX;
2375 resultSpan[1] = finalSolution.spanY;
Adam Cohen482ed822012-03-02 14:15:13 -08002376
2377 // If we're just testing for a possible location (MODE_ACCEPT_DROP), we don't bother
2378 // committing anything or animating anything as we just want to determine if a solution
2379 // exists
2380 if (mode == MODE_DRAG_OVER || mode == MODE_ON_DROP || mode == MODE_ON_DROP_EXTERNAL) {
2381 if (!DESTRUCTIVE_REORDER) {
2382 copySolutionToTempState(finalSolution, dragView);
2383 }
2384 setItemPlacementDirty(true);
2385 animateItemsToSolution(finalSolution, dragView, mode == MODE_ON_DROP);
2386
Adam Cohen19f37922012-03-21 11:59:11 -07002387 if (!DESTRUCTIVE_REORDER &&
2388 (mode == MODE_ON_DROP || mode == MODE_ON_DROP_EXTERNAL)) {
Adam Cohen482ed822012-03-02 14:15:13 -08002389 commitTempPlacement();
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002390 completeAndClearReorderPreviewAnimations();
Adam Cohen19f37922012-03-21 11:59:11 -07002391 setItemPlacementDirty(false);
2392 } else {
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002393 beginOrAdjustReorderPreviewAnimations(finalSolution, dragView,
2394 REORDER_ANIMATION_DURATION, ReorderPreviewAnimation.MODE_PREVIEW);
Adam Cohen482ed822012-03-02 14:15:13 -08002395 }
2396 }
2397 } else {
2398 foundSolution = false;
2399 result[0] = result[1] = resultSpan[0] = resultSpan[1] = -1;
2400 }
2401
2402 if ((mode == MODE_ON_DROP || !foundSolution) && !DESTRUCTIVE_REORDER) {
2403 setUseTempCoords(false);
2404 }
Adam Cohen482ed822012-03-02 14:15:13 -08002405
Michael Jurkaa52570f2012-03-20 03:18:20 -07002406 mShortcutsAndWidgets.requestLayout();
Adam Cohen482ed822012-03-02 14:15:13 -08002407 return result;
2408 }
2409
Adam Cohen19f37922012-03-21 11:59:11 -07002410 void setItemPlacementDirty(boolean dirty) {
2411 mItemPlacementDirty = dirty;
Adam Cohen482ed822012-03-02 14:15:13 -08002412 }
Adam Cohen19f37922012-03-21 11:59:11 -07002413 boolean isItemPlacementDirty() {
2414 return mItemPlacementDirty;
Adam Cohen482ed822012-03-02 14:15:13 -08002415 }
2416
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07002417 private static class ItemConfiguration extends CellAndSpan {
Adam Cohen8baab352012-03-20 17:39:21 -07002418 HashMap<View, CellAndSpan> map = new HashMap<View, CellAndSpan>();
Adam Cohenf3900c22012-11-16 18:28:11 -08002419 private HashMap<View, CellAndSpan> savedMap = new HashMap<View, CellAndSpan>();
2420 ArrayList<View> sortedViews = new ArrayList<View>();
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002421 ArrayList<View> intersectingViews;
Adam Cohen482ed822012-03-02 14:15:13 -08002422 boolean isSolution = false;
Adam Cohen482ed822012-03-02 14:15:13 -08002423
Adam Cohenf3900c22012-11-16 18:28:11 -08002424 void save() {
2425 // Copy current state into savedMap
2426 for (View v: map.keySet()) {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07002427 savedMap.get(v).copyFrom(map.get(v));
Adam Cohenf3900c22012-11-16 18:28:11 -08002428 }
2429 }
2430
2431 void restore() {
2432 // Restore current state from savedMap
2433 for (View v: savedMap.keySet()) {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07002434 map.get(v).copyFrom(savedMap.get(v));
Adam Cohenf3900c22012-11-16 18:28:11 -08002435 }
2436 }
2437
2438 void add(View v, CellAndSpan cs) {
2439 map.put(v, cs);
2440 savedMap.put(v, new CellAndSpan());
2441 sortedViews.add(v);
2442 }
2443
Adam Cohen482ed822012-03-02 14:15:13 -08002444 int area() {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07002445 return spanX * spanY;
Adam Cohenf3900c22012-11-16 18:28:11 -08002446 }
2447
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07002448 void getBoundingRectForViews(ArrayList<View> views, Rect outRect) {
2449 boolean first = true;
2450 for (View v: views) {
2451 CellAndSpan c = map.get(v);
2452 if (first) {
2453 outRect.set(c.cellX, c.cellY, c.cellX + c.spanX, c.cellY + c.spanY);
2454 first = false;
2455 } else {
2456 outRect.union(c.cellX, c.cellY, c.cellX + c.spanX, c.cellY + c.spanY);
2457 }
2458 }
Adam Cohenf3900c22012-11-16 18:28:11 -08002459 }
Adam Cohen482ed822012-03-02 14:15:13 -08002460 }
2461
Adam Cohendf035382011-04-11 17:22:04 -07002462 /**
Adam Cohendf035382011-04-11 17:22:04 -07002463 * Find a starting cell position that will fit the given bounds nearest the requested
2464 * cell location. Uses Euclidean distance to score multiple vacant areas.
2465 *
2466 * @param pixelX The X location at which you want to search for a vacant area.
2467 * @param pixelY The Y location at which you want to search for a vacant area.
2468 * @param spanX Horizontal span of the object.
2469 * @param spanY Vertical span of the object.
2470 * @param ignoreView Considers space occupied by this view as unoccupied
2471 * @param result Previously returned value to possibly recycle.
2472 * @return The X, Y cell of a vacant area that can contain this object,
2473 * nearest the requested location.
2474 */
Adam Cohenf9c184a2016-01-15 16:47:43 -08002475 public int[] findNearestArea(int pixelX, int pixelY, int spanX, int spanY, int[] result) {
Sunny Goyalf7a29e82015-04-24 15:20:43 -07002476 return findNearestArea(pixelX, pixelY, spanX, spanY, spanX, spanY, false, result, null);
Adam Cohendf035382011-04-11 17:22:04 -07002477 }
2478
Michael Jurka0280c3b2010-09-17 15:00:07 -07002479 boolean existsEmptyCell() {
2480 return findCellForSpan(null, 1, 1);
2481 }
2482
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002483 /**
Michael Jurka0280c3b2010-09-17 15:00:07 -07002484 * Finds the upper-left coordinate of the first rectangle in the grid that can
2485 * hold a cell of the specified dimensions. If intersectX and intersectY are not -1,
2486 * then this method will only return coordinates for rectangles that contain the cell
2487 * (intersectX, intersectY)
2488 *
2489 * @param cellXY The array that will contain the position of a vacant cell if such a cell
2490 * can be found.
2491 * @param spanX The horizontal span of the cell we want to find.
2492 * @param spanY The vertical span of the cell we want to find.
2493 *
2494 * @return True if a vacant cell of the specified dimension was found, false otherwise.
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07002495 */
Hyunyoung Song3f471442015-04-08 19:01:34 -07002496 public boolean findCellForSpan(int[] cellXY, int spanX, int spanY) {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07002497 if (cellXY == null) {
2498 cellXY = new int[2];
Michael Jurka0280c3b2010-09-17 15:00:07 -07002499 }
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07002500 return mOccupied.findVacantCell(cellXY, spanX, spanY);
Michael Jurka0280c3b2010-09-17 15:00:07 -07002501 }
2502
2503 /**
Winson Chungc07918d2011-07-01 15:35:26 -07002504 * A drag event has begun over this layout.
2505 * It may have begun over this layout (in which case onDragChild is called first),
2506 * or it may have begun on another layout.
2507 */
2508 void onDragEnter() {
Winson Chungc07918d2011-07-01 15:35:26 -07002509 mDragging = true;
2510 }
2511
2512 /**
Michael Jurka0280c3b2010-09-17 15:00:07 -07002513 * Called when drag has left this CellLayout or has been completed (successfully or not)
2514 */
2515 void onDragExit() {
Joe Onorato4be866d2010-10-10 11:26:02 -07002516 // This can actually be called when we aren't in a drag, e.g. when adding a new
2517 // item to this layout via the customize drawer.
2518 // Guard against that case.
2519 if (mDragging) {
2520 mDragging = false;
Patrick Dubroyde7658b2010-09-27 11:15:43 -07002521 }
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -07002522
2523 // Invalidate the drag data
Adam Cohend41fbf52012-02-16 23:53:59 -08002524 mDragCell[0] = mDragCell[1] = -1;
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -07002525 mDragOutlineAnims[mDragOutlineCurrent].animateOut();
2526 mDragOutlineCurrent = (mDragOutlineCurrent + 1) % mDragOutlineAnims.length;
Adam Cohen19f37922012-03-21 11:59:11 -07002527 revertTempState();
Michael Jurka33945b22010-12-21 18:19:38 -08002528 setIsDragOverlapping(false);
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07002529 }
2530
2531 /**
Winson Chungaafa03c2010-06-11 17:34:16 -07002532 * Mark a child as having been dropped.
Patrick Dubroyde7658b2010-09-27 11:15:43 -07002533 * At the beginning of the drag operation, the child may have been on another
Patrick Dubroyce34a972010-10-19 10:34:32 -07002534 * screen, but it is re-parented before this method is called.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002535 *
2536 * @param child The child that is being dropped
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002537 */
Adam Cohen716b51e2011-06-30 12:09:54 -07002538 void onDropChild(View child) {
Romain Guyd94533d2009-08-17 10:01:15 -07002539 if (child != null) {
2540 LayoutParams lp = (LayoutParams) child.getLayoutParams();
Romain Guy84f296c2009-11-04 15:00:44 -08002541 lp.dropped = true;
Romain Guyd94533d2009-08-17 10:01:15 -07002542 child.requestLayout();
Tony Wickham1cdb6d02015-09-17 11:08:27 -07002543 markCellsAsOccupiedForView(child);
Romain Guyd94533d2009-08-17 10:01:15 -07002544 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002545 }
2546
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002547 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002548 * Computes a bounding rectangle for a range of cells
Winson Chungaafa03c2010-06-11 17:34:16 -07002549 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002550 * @param cellX X coordinate of upper left corner expressed as a cell position
2551 * @param cellY Y coordinate of upper left corner expressed as a cell position
Winson Chungaafa03c2010-06-11 17:34:16 -07002552 * @param cellHSpan Width in cells
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002553 * @param cellVSpan Height in cells
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07002554 * @param resultRect Rect into which to put the results
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002555 */
Adam Cohend41fbf52012-02-16 23:53:59 -08002556 public void cellToRect(int cellX, int cellY, int cellHSpan, int cellVSpan, Rect resultRect) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002557 final int cellWidth = mCellWidth;
2558 final int cellHeight = mCellHeight;
Winson Chungaafa03c2010-06-11 17:34:16 -07002559
Winson Chung4b825dcd2011-06-19 12:41:22 -07002560 final int hStartPadding = getPaddingLeft();
2561 final int vStartPadding = getPaddingTop();
Winson Chungaafa03c2010-06-11 17:34:16 -07002562
Sunny Goyalaa8a8712016-11-20 15:26:01 +05302563 int width = cellHSpan * cellWidth;
2564 int height = cellVSpan * cellHeight;
2565 int x = hStartPadding + cellX * cellWidth;
2566 int y = vStartPadding + cellY * cellHeight;
Winson Chungaafa03c2010-06-11 17:34:16 -07002567
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07002568 resultRect.set(x, y, x + width, y + height);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002569 }
Winson Chungaafa03c2010-06-11 17:34:16 -07002570
Adam Cohend4844c32011-02-18 19:25:06 -08002571 public void markCellsAsOccupiedForView(View view) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07002572 if (view == null || view.getParent() != mShortcutsAndWidgets) return;
Michael Jurka0280c3b2010-09-17 15:00:07 -07002573 LayoutParams lp = (LayoutParams) view.getLayoutParams();
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07002574 mOccupied.markCells(lp.cellX, lp.cellY, lp.cellHSpan, lp.cellVSpan, true);
Michael Jurka0280c3b2010-09-17 15:00:07 -07002575 }
2576
Adam Cohend4844c32011-02-18 19:25:06 -08002577 public void markCellsAsUnoccupiedForView(View view) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07002578 if (view == null || view.getParent() != mShortcutsAndWidgets) return;
Michael Jurka0280c3b2010-09-17 15:00:07 -07002579 LayoutParams lp = (LayoutParams) view.getLayoutParams();
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07002580 mOccupied.markCells(lp.cellX, lp.cellY, lp.cellHSpan, lp.cellVSpan, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002581 }
2582
Adam Cohen2801caf2011-05-13 20:57:39 -07002583 public int getDesiredWidth() {
Sunny Goyalaa8a8712016-11-20 15:26:01 +05302584 return getPaddingLeft() + getPaddingRight() + (mCountX * mCellWidth);
Adam Cohen2801caf2011-05-13 20:57:39 -07002585 }
2586
2587 public int getDesiredHeight() {
Sunny Goyalaa8a8712016-11-20 15:26:01 +05302588 return getPaddingTop() + getPaddingBottom() + (mCountY * mCellHeight);
Adam Cohen2801caf2011-05-13 20:57:39 -07002589 }
2590
Michael Jurka66d72172011-04-12 16:29:25 -07002591 public boolean isOccupied(int x, int y) {
2592 if (x < mCountX && y < mCountY) {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07002593 return mOccupied.cells[x][y];
Michael Jurka66d72172011-04-12 16:29:25 -07002594 } else {
2595 throw new RuntimeException("Position exceeds the bound of this CellLayout");
2596 }
2597 }
2598
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002599 @Override
2600 public ViewGroup.LayoutParams generateLayoutParams(AttributeSet attrs) {
2601 return new CellLayout.LayoutParams(getContext(), attrs);
2602 }
2603
2604 @Override
2605 protected boolean checkLayoutParams(ViewGroup.LayoutParams p) {
2606 return p instanceof CellLayout.LayoutParams;
2607 }
2608
2609 @Override
2610 protected ViewGroup.LayoutParams generateLayoutParams(ViewGroup.LayoutParams p) {
2611 return new CellLayout.LayoutParams(p);
2612 }
2613
2614 public static class LayoutParams extends ViewGroup.MarginLayoutParams {
2615 /**
2616 * Horizontal location of the item in the grid.
2617 */
2618 @ViewDebug.ExportedProperty
2619 public int cellX;
2620
2621 /**
2622 * Vertical location of the item in the grid.
2623 */
2624 @ViewDebug.ExportedProperty
2625 public int cellY;
2626
2627 /**
Adam Cohen482ed822012-03-02 14:15:13 -08002628 * Temporary horizontal location of the item in the grid during reorder
2629 */
2630 public int tmpCellX;
2631
2632 /**
2633 * Temporary vertical location of the item in the grid during reorder
2634 */
2635 public int tmpCellY;
2636
2637 /**
2638 * Indicates that the temporary coordinates should be used to layout the items
2639 */
2640 public boolean useTmpCoords;
2641
2642 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002643 * Number of cells spanned horizontally by the item.
2644 */
2645 @ViewDebug.ExportedProperty
2646 public int cellHSpan;
2647
2648 /**
2649 * Number of cells spanned vertically by the item.
2650 */
2651 @ViewDebug.ExportedProperty
2652 public int cellVSpan;
Winson Chungaafa03c2010-06-11 17:34:16 -07002653
Adam Cohen1b607ed2011-03-03 17:26:50 -08002654 /**
2655 * Indicates whether the item will set its x, y, width and height parameters freely,
2656 * or whether these will be computed based on cellX, cellY, cellHSpan and cellVSpan.
2657 */
Adam Cohend4844c32011-02-18 19:25:06 -08002658 public boolean isLockedToGrid = true;
2659
Adam Cohen482ed822012-03-02 14:15:13 -08002660 /**
Adam Cohenec40b2b2013-07-23 15:52:40 -07002661 * Indicates that this item should use the full extents of its parent.
2662 */
2663 public boolean isFullscreen = false;
2664
2665 /**
Adam Cohen482ed822012-03-02 14:15:13 -08002666 * Indicates whether this item can be reordered. Always true except in the case of the
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07002667 * the AllApps button and QSB place holder.
Adam Cohen482ed822012-03-02 14:15:13 -08002668 */
2669 public boolean canReorder = true;
2670
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002671 // X coordinate of the view in the layout.
2672 @ViewDebug.ExportedProperty
Vadim Tryshevfedca432015-08-19 17:55:02 -07002673 public int x;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002674 // Y coordinate of the view in the layout.
2675 @ViewDebug.ExportedProperty
Vadim Tryshevfedca432015-08-19 17:55:02 -07002676 public int y;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002677
Romain Guy84f296c2009-11-04 15:00:44 -08002678 boolean dropped;
Romain Guyfcb9e712009-10-02 16:06:52 -07002679
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002680 public LayoutParams(Context c, AttributeSet attrs) {
2681 super(c, attrs);
2682 cellHSpan = 1;
2683 cellVSpan = 1;
2684 }
2685
2686 public LayoutParams(ViewGroup.LayoutParams source) {
2687 super(source);
2688 cellHSpan = 1;
2689 cellVSpan = 1;
2690 }
Winson Chungaafa03c2010-06-11 17:34:16 -07002691
2692 public LayoutParams(LayoutParams source) {
2693 super(source);
2694 this.cellX = source.cellX;
2695 this.cellY = source.cellY;
2696 this.cellHSpan = source.cellHSpan;
2697 this.cellVSpan = source.cellVSpan;
2698 }
2699
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002700 public LayoutParams(int cellX, int cellY, int cellHSpan, int cellVSpan) {
Romain Guy8f19cdd2010-01-08 15:07:00 -08002701 super(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002702 this.cellX = cellX;
2703 this.cellY = cellY;
2704 this.cellHSpan = cellHSpan;
2705 this.cellVSpan = cellVSpan;
2706 }
2707
Sunny Goyalaa8a8712016-11-20 15:26:01 +05302708 public void setup(int cellWidth, int cellHeight, boolean invertHorizontally, int colCount) {
Jon Miranda7ae64ff2016-11-21 16:18:46 -08002709 setup(cellWidth, cellHeight, invertHorizontally, colCount, 1.0f, 1.0f);
2710 }
2711
2712 /**
2713 * Use this method, as opposed to {@link #setup(int, int, boolean, int)}, if the view needs
2714 * to be scaled.
2715 *
2716 * ie. In multi-window mode, we setup widgets so that they are measured and laid out
2717 * using their full/invariant device profile sizes.
2718 */
2719 public void setup(int cellWidth, int cellHeight, boolean invertHorizontally, int colCount,
2720 float cellScaleX, float cellScaleY) {
Adam Cohend4844c32011-02-18 19:25:06 -08002721 if (isLockedToGrid) {
2722 final int myCellHSpan = cellHSpan;
2723 final int myCellVSpan = cellVSpan;
Adam Cohen2374abf2013-04-16 14:56:57 -07002724 int myCellX = useTmpCoords ? tmpCellX : cellX;
2725 int myCellY = useTmpCoords ? tmpCellY : cellY;
2726
2727 if (invertHorizontally) {
2728 myCellX = colCount - myCellX - cellHSpan;
2729 }
Adam Cohen1b607ed2011-03-03 17:26:50 -08002730
Jon Miranda7ae64ff2016-11-21 16:18:46 -08002731 width = (int) (myCellHSpan * cellWidth / cellScaleX - leftMargin - rightMargin);
2732 height = (int) (myCellVSpan * cellHeight / cellScaleY - topMargin - bottomMargin);
Sunny Goyalaa8a8712016-11-20 15:26:01 +05302733 x = (myCellX * cellWidth + leftMargin);
2734 y = (myCellY * cellHeight + topMargin);
Adam Cohend4844c32011-02-18 19:25:06 -08002735 }
2736 }
Winson Chungaafa03c2010-06-11 17:34:16 -07002737
Winson Chungaafa03c2010-06-11 17:34:16 -07002738 public String toString() {
2739 return "(" + this.cellX + ", " + this.cellY + ")";
2740 }
Adam Cohen7f4eabe2011-04-21 16:19:16 -07002741
2742 public void setWidth(int width) {
2743 this.width = width;
2744 }
2745
2746 public int getWidth() {
2747 return width;
2748 }
2749
2750 public void setHeight(int height) {
2751 this.height = height;
2752 }
2753
2754 public int getHeight() {
2755 return height;
2756 }
2757
2758 public void setX(int x) {
2759 this.x = x;
2760 }
2761
2762 public int getX() {
2763 return x;
2764 }
2765
2766 public void setY(int y) {
2767 this.y = y;
2768 }
2769
2770 public int getY() {
2771 return y;
2772 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002773 }
2774
Michael Jurka0280c3b2010-09-17 15:00:07 -07002775 // This class stores info for two purposes:
2776 // 1. When dragging items (mDragInfo in Workspace), we store the View, its cellX & cellY,
2777 // its spanX, spanY, and the screen it is on
2778 // 2. When long clicking on an empty cell in a CellLayout, we save information about the
2779 // cellX and cellY coordinates and which page was clicked. We then set this as a tag on
2780 // the CellLayout that was long clicked
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07002781 public static final class CellInfo extends CellAndSpan {
Adam Cohenf9c184a2016-01-15 16:47:43 -08002782 public View cell;
Adam Cohendcd297f2013-06-18 13:13:40 -07002783 long screenId;
Winson Chung3d503fb2011-07-13 17:25:49 -07002784 long container;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002785
Sunny Goyal83a8f042015-05-19 12:52:12 -07002786 public CellInfo(View v, ItemInfo info) {
Adam Cohene0aaa0d2014-05-12 12:44:22 -07002787 cellX = info.cellX;
2788 cellY = info.cellY;
2789 spanX = info.spanX;
2790 spanY = info.spanY;
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07002791 cell = v;
Adam Cohene0aaa0d2014-05-12 12:44:22 -07002792 screenId = info.screenId;
2793 container = info.container;
2794 }
2795
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002796 @Override
2797 public String toString() {
Winson Chungaafa03c2010-06-11 17:34:16 -07002798 return "Cell[view=" + (cell == null ? "null" : cell.getClass())
2799 + ", x=" + cellX + ", y=" + cellY + "]";
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002800 }
2801 }
Michael Jurkad771c962011-08-09 15:00:48 -07002802
Tony Wickham86930612015-09-09 13:50:40 -07002803 /**
2804 * Returns whether an item can be placed in this CellLayout (after rearranging and/or resizing
2805 * if necessary).
2806 */
2807 public boolean hasReorderSolution(ItemInfo itemInfo) {
2808 int[] cellPoint = new int[2];
2809 // Check for a solution starting at every cell.
2810 for (int cellX = 0; cellX < getCountX(); cellX++) {
2811 for (int cellY = 0; cellY < getCountY(); cellY++) {
2812 cellToPoint(cellX, cellY, cellPoint);
2813 if (findReorderSolution(cellPoint[0], cellPoint[1], itemInfo.minSpanX,
2814 itemInfo.minSpanY, itemInfo.spanX, itemInfo.spanY, mDirectionVector, null,
2815 true, new ItemConfiguration()).isSolution) {
2816 return true;
2817 }
2818 }
2819 }
2820 return false;
2821 }
2822
Sunny Goyal9ca9c132015-04-29 14:57:22 -07002823 public boolean isRegionVacant(int x, int y, int spanX, int spanY) {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07002824 return mOccupied.isRegionVacant(x, y, spanX, spanY);
Sunny Goyal9ca9c132015-04-29 14:57:22 -07002825 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002826}