blob: 1e212bfd8b06dd43156fb60db4acb8c7c8455337 [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;
Adam Cohenc9735cf2015-01-23 16:11:55 -080024import android.annotation.TargetApi;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080025import android.content.Context;
Joe Onorato79e56262009-09-21 15:23:04 -040026import android.content.res.Resources;
Sunny Goyalc13403c2016-11-18 23:44:48 -080027import android.content.res.TypedArray;
Joe Onorato4be866d2010-10-10 11:26:02 -070028import android.graphics.Bitmap;
Winson Chungaafa03c2010-06-11 17:34:16 -070029import android.graphics.Canvas;
Andrew Flynn0dca1ec2012-02-29 13:33:22 -080030import android.graphics.Color;
Joe Onorato4be866d2010-10-10 11:26:02 -070031import android.graphics.Paint;
Patrick Dubroyde7658b2010-09-27 11:15:43 -070032import android.graphics.Point;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080033import android.graphics.Rect;
Adam Cohen482ed822012-03-02 14:15:13 -080034import android.graphics.drawable.ColorDrawable;
Patrick Dubroy6569f2c2010-07-12 14:25:18 -070035import android.graphics.drawable.Drawable;
Adam Cohenc9735cf2015-01-23 16:11:55 -080036import android.os.Build;
Adam Cohen1462de32012-07-24 22:34:36 -070037import android.os.Parcelable;
Sunny Goyalc13403c2016-11-18 23:44:48 -080038import android.support.annotation.IntDef;
Adam Cohenc9735cf2015-01-23 16:11:55 -080039import android.support.v4.view.ViewCompat;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080040import android.util.AttributeSet;
Joe Onorato4be866d2010-10-10 11:26:02 -070041import android.util.Log;
Adam Cohen1462de32012-07-24 22:34:36 -070042import android.util.SparseArray;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080043import android.view.MotionEvent;
44import android.view.View;
45import android.view.ViewDebug;
46import android.view.ViewGroup;
Adam Cohenc9735cf2015-01-23 16:11:55 -080047import android.view.accessibility.AccessibilityEvent;
Winson Chung150fbab2010-09-29 17:14:26 -070048import android.view.animation.DecelerateInterpolator;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080049
Sunny Goyal4b6eb262015-05-14 19:24:40 -070050import com.android.launcher3.BubbleTextView.BubbleTextShadowHandler;
Sunny Goyalaa8ef112015-06-12 20:04:41 -070051import com.android.launcher3.LauncherSettings.Favorites;
Sunny Goyale9b651e2015-04-24 11:44:51 -070052import com.android.launcher3.accessibility.DragAndDropAccessibilityDelegate;
53import com.android.launcher3.accessibility.FolderAccessibilityHelper;
54import com.android.launcher3.accessibility.WorkspaceAccessibilityHelper;
Sunny Goyal6c56c682015-07-16 14:09:05 -070055import com.android.launcher3.config.ProviderConfig;
Sunny Goyal26119432016-02-18 22:09:23 +000056import com.android.launcher3.folder.FolderIcon;
Sunny Goyal06e21a22016-08-11 16:02:02 -070057import com.android.launcher3.graphics.DragPreviewProvider;
Sunny Goyalff4ba2d2016-04-02 14:12:34 -070058import com.android.launcher3.util.CellAndSpan;
59import com.android.launcher3.util.GridOccupancy;
Sunny Goyale2fd14b2015-08-27 17:45:46 -070060import com.android.launcher3.util.ParcelableSparseArray;
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();
109 Paint mFolderBgPaint = new Paint();
Adam Cohen69ce2e52011-07-03 19:25:21 -0700110
Michael Jurka5f1c5092010-09-03 14:15:02 -0700111 private float mBackgroundAlpha;
Adam Cohenf34bab52010-09-30 14:11:56 -0700112
Sunny Goyalf5440cb2016-12-14 15:13:00 -0800113 private static final int[] BACKGROUND_STATE_ACTIVE = new int[] { android.R.attr.state_active };
114 private static final int[] BACKGROUND_STATE_DEFAULT = new int[0];
115 private final Drawable mBackground;
Sunny Goyal2805e632015-05-20 15:35:32 -0700116
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700117 // These values allow a fixed measurement to be set on the CellLayout.
118 private int mFixedWidth = -1;
119 private int mFixedHeight = -1;
120
Michael Jurka33945b22010-12-21 18:19:38 -0800121 // If we're actively dragging something over this screen, mIsDragOverlapping is true
122 private boolean mIsDragOverlapping = false;
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700123
Winson Chung150fbab2010-09-29 17:14:26 -0700124 // These arrays are used to implement the drag visualization on x-large screens.
Joe Onorato4be866d2010-10-10 11:26:02 -0700125 // They are used as circular arrays, indexed by mDragOutlineCurrent.
Adam Cohen091440a2015-03-18 14:16:05 -0700126 @Thunk Rect[] mDragOutlines = new Rect[4];
127 @Thunk float[] mDragOutlineAlphas = new float[mDragOutlines.length];
Joe Onorato4be866d2010-10-10 11:26:02 -0700128 private InterruptibleInOutAnimator[] mDragOutlineAnims =
129 new InterruptibleInOutAnimator[mDragOutlines.length];
Winson Chung150fbab2010-09-29 17:14:26 -0700130
131 // Used as an index into the above 3 arrays; indicates which is the most current value.
Joe Onorato4be866d2010-10-10 11:26:02 -0700132 private int mDragOutlineCurrent = 0;
Patrick Dubroy8e58e912010-10-14 13:21:48 -0700133 private final Paint mDragOutlinePaint = new Paint();
Winson Chung150fbab2010-09-29 17:14:26 -0700134
Sunny Goyal4fe5a372015-05-14 19:55:10 -0700135 private final ClickShadowView mTouchFeedbackView;
Patrick Dubroy96864c32011-03-10 17:17:23 -0800136
Sunny Goyal316490e2015-06-02 09:38:28 -0700137 @Thunk HashMap<CellLayout.LayoutParams, Animator> mReorderAnimators = new HashMap<>();
138 @Thunk HashMap<View, ReorderPreviewAnimation> mShakeAnimators = new HashMap<>();
Adam Cohen19f37922012-03-21 11:59:11 -0700139
140 private boolean mItemPlacementDirty = false;
Adam Cohenbfbfd262011-06-13 16:55:12 -0700141
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700142 // When a drag operation is in progress, holds the nearest cell to the touch point
143 private final int[] mDragCell = new int[2];
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800144
Joe Onorato4be866d2010-10-10 11:26:02 -0700145 private boolean mDragging = false;
146
Patrick Dubroyce34a972010-10-19 10:34:32 -0700147 private TimeInterpolator mEaseOutInterpolator;
Michael Jurkaa52570f2012-03-20 03:18:20 -0700148 private ShortcutAndWidgetContainer mShortcutsAndWidgets;
Patrick Dubroyce34a972010-10-19 10:34:32 -0700149
Sunny Goyalc13403c2016-11-18 23:44:48 -0800150 @Retention(RetentionPolicy.SOURCE)
151 @IntDef({WORKSPACE, HOTSEAT, FOLDER})
152 public @interface ContainerType{}
153 public static final int WORKSPACE = 0;
154 public static final int HOTSEAT = 1;
155 public static final int FOLDER = 2;
156
157 @ContainerType private final int mContainerType;
158
159 private final float mChildScale;
Andrew Flynn0dca1ec2012-02-29 13:33:22 -0800160
Adam Cohenfa3c58f2013-12-06 16:10:55 -0800161 public static final int MODE_SHOW_REORDER_HINT = 0;
162 public static final int MODE_DRAG_OVER = 1;
163 public static final int MODE_ON_DROP = 2;
164 public static final int MODE_ON_DROP_EXTERNAL = 3;
165 public static final int MODE_ACCEPT_DROP = 4;
Adam Cohen19f37922012-03-21 11:59:11 -0700166 private static final boolean DESTRUCTIVE_REORDER = false;
Adam Cohen482ed822012-03-02 14:15:13 -0800167 private static final boolean DEBUG_VISUALIZE_OCCUPIED = false;
168
Adam Cohenfa3c58f2013-12-06 16:10:55 -0800169 private static final float REORDER_PREVIEW_MAGNITUDE = 0.12f;
Adam Cohen19f37922012-03-21 11:59:11 -0700170 private static final int REORDER_ANIMATION_DURATION = 150;
Sunny Goyalc13403c2016-11-18 23:44:48 -0800171 @Thunk final float mReorderPreviewAnimationMagnitude;
Adam Cohen19f37922012-03-21 11:59:11 -0700172
Adam Cohen482ed822012-03-02 14:15:13 -0800173 private ArrayList<View> mIntersectingViews = new ArrayList<View>();
174 private Rect mOccupiedRect = new Rect();
175 private int[] mDirectionVector = new int[2];
Adam Cohen19f37922012-03-21 11:59:11 -0700176 int[] mPreviousReorderDirection = new int[2];
Adam Cohenb209e632012-03-27 17:09:36 -0700177 private static final int INVALID_DIRECTION = -100;
Adam Cohen482ed822012-03-02 14:15:13 -0800178
Sunny Goyal2805e632015-05-20 15:35:32 -0700179 private final Rect mTempRect = new Rect();
Winson Chung3a6e7f32013-10-09 15:50:52 -0700180
Michael Jurkaca993832012-06-29 15:17:04 -0700181 private final static Paint sPaint = new Paint();
Romain Guy8a0bff52012-05-06 13:14:33 -0700182
Adam Cohenc9735cf2015-01-23 16:11:55 -0800183 // Related to accessible drag and drop
Sunny Goyale9b651e2015-04-24 11:44:51 -0700184 private DragAndDropAccessibilityDelegate mTouchHelper;
Adam Cohenc9735cf2015-01-23 16:11:55 -0800185 private boolean mUseTouchHelper = false;
Adam Cohenc9735cf2015-01-23 16:11:55 -0800186
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800187 public CellLayout(Context context) {
188 this(context, null);
189 }
190
191 public CellLayout(Context context, AttributeSet attrs) {
192 this(context, attrs, 0);
193 }
194
195 public CellLayout(Context context, AttributeSet attrs, int defStyle) {
196 super(context, attrs, defStyle);
Sunny Goyalc13403c2016-11-18 23:44:48 -0800197 TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.CellLayout, defStyle, 0);
198 mContainerType = a.getInteger(R.styleable.CellLayout_containerType, WORKSPACE);
199 a.recycle();
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700200
201 // A ViewGroup usually does not draw, but CellLayout needs to draw a rectangle to show
202 // the user where a dragged item will land when dropped.
203 setWillNotDraw(false);
Romain Guyce3cbd12013-02-25 15:00:36 -0800204 setClipToPadding(false);
Tony2fd02082016-10-07 12:50:01 -0700205 mLauncher = Launcher.getLauncher(context);
Michael Jurkaa63c4522010-08-19 13:52:27 -0700206
Adam Cohen2e6da152015-05-06 11:42:25 -0700207 DeviceProfile grid = mLauncher.getDeviceProfile();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800208
Winson Chung11a1a532013-09-13 11:14:45 -0700209 mCellWidth = mCellHeight = -1;
Nilesh Agrawal5f7099a2014-01-02 15:54:57 -0800210 mFixedCellWidth = mFixedCellHeight = -1;
Sunny Goyalff4ba2d2016-04-02 14:12:34 -0700211
212 mCountX = grid.inv.numColumns;
213 mCountY = grid.inv.numRows;
214 mOccupied = new GridOccupancy(mCountX, mCountY);
215 mTmpOccupied = new GridOccupancy(mCountX, mCountY);
216
Adam Cohen5b53f292012-03-29 14:30:35 -0700217 mPreviousReorderDirection[0] = INVALID_DIRECTION;
218 mPreviousReorderDirection[1] = INVALID_DIRECTION;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800219
Adam Cohenefca0272016-02-24 19:19:06 -0800220 mFolderLeaveBehind.delegateCellX = -1;
221 mFolderLeaveBehind.delegateCellY = -1;
222
Sunny Goyalc13403c2016-11-18 23:44:48 -0800223 mChildScale = mContainerType == HOTSEAT ? grid.inv.hotseatScale : 1f;
224
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800225 setAlwaysDrawnWithCacheEnabled(false);
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700226 final Resources res = getResources();
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700227
Sunny Goyalf5440cb2016-12-14 15:13:00 -0800228 mBackground = res.getDrawable(R.drawable.bg_celllayout);
Sunny Goyal2805e632015-05-20 15:35:32 -0700229 mBackground.setCallback(this);
Winson Chunge8f1d042015-07-31 12:39:57 -0700230 mBackground.setAlpha((int) (mBackgroundAlpha * 255));
Michael Jurka33945b22010-12-21 18:19:38 -0800231
Sunny Goyalc13403c2016-11-18 23:44:48 -0800232 mReorderPreviewAnimationMagnitude = (REORDER_PREVIEW_MAGNITUDE * grid.iconSizePx);
Adam Cohen19f37922012-03-21 11:59:11 -0700233
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700234 // Initialize the data structures used for the drag visualization.
Patrick Dubroyce34a972010-10-19 10:34:32 -0700235 mEaseOutInterpolator = new DecelerateInterpolator(2.5f); // Quint ease out
Winson Chungb8c69f32011-10-19 21:36:08 -0700236 mDragCell[0] = mDragCell[1] = -1;
Joe Onorato4be866d2010-10-10 11:26:02 -0700237 for (int i = 0; i < mDragOutlines.length; i++) {
Adam Cohend41fbf52012-02-16 23:53:59 -0800238 mDragOutlines[i] = new Rect(-1, -1, -1, -1);
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700239 }
Sunny Goyalf28e6af2016-08-31 16:02:40 -0700240 mDragOutlinePaint.setColor(getResources().getColor(R.color.outline_color));
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700241
242 // When dragging things around the home screens, we show a green outline of
243 // where the item will land. The outlines gradually fade out, leaving a trail
244 // behind the drag path.
245 // Set up all the animations that are used to implement this fading.
246 final int duration = res.getInteger(R.integer.config_dragOutlineFadeTime);
Chet Haase472b2812010-10-14 07:02:04 -0700247 final float fromAlphaValue = 0;
248 final float toAlphaValue = (float)res.getInteger(R.integer.config_dragOutlineMaxAlpha);
Joe Onorato4be866d2010-10-10 11:26:02 -0700249
Patrick Dubroy8e58e912010-10-14 13:21:48 -0700250 Arrays.fill(mDragOutlineAlphas, fromAlphaValue);
Joe Onorato4be866d2010-10-10 11:26:02 -0700251
252 for (int i = 0; i < mDragOutlineAnims.length; i++) {
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700253 final InterruptibleInOutAnimator anim =
Michael Jurkaf1ad6082013-03-13 12:55:46 +0100254 new InterruptibleInOutAnimator(this, duration, fromAlphaValue, toAlphaValue);
Patrick Dubroyce34a972010-10-19 10:34:32 -0700255 anim.getAnimator().setInterpolator(mEaseOutInterpolator);
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700256 final int thisIndex = i;
Chet Haase472b2812010-10-14 07:02:04 -0700257 anim.getAnimator().addUpdateListener(new AnimatorUpdateListener() {
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700258 public void onAnimationUpdate(ValueAnimator animation) {
Joe Onorato4be866d2010-10-10 11:26:02 -0700259 final Bitmap outline = (Bitmap)anim.getTag();
260
261 // If an animation is started and then stopped very quickly, we can still
262 // get spurious updates we've cleared the tag. Guard against this.
263 if (outline == null) {
Tony Wickhama0628cc2015-10-14 15:23:04 -0700264 if (LOGD) {
Patrick Dubroyfe6bd872010-10-13 17:32:10 -0700265 Object val = animation.getAnimatedValue();
266 Log.d(TAG, "anim " + thisIndex + " update: " + val +
267 ", isStopped " + anim.isStopped());
268 }
Joe Onorato4be866d2010-10-10 11:26:02 -0700269 // Try to prevent it from continuing to run
270 animation.cancel();
271 } else {
Chet Haase472b2812010-10-14 07:02:04 -0700272 mDragOutlineAlphas[thisIndex] = (Float) animation.getAnimatedValue();
Adam Cohend41fbf52012-02-16 23:53:59 -0800273 CellLayout.this.invalidate(mDragOutlines[thisIndex]);
Joe Onorato4be866d2010-10-10 11:26:02 -0700274 }
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700275 }
276 });
Joe Onorato4be866d2010-10-10 11:26:02 -0700277 // The animation holds a reference to the drag outline bitmap as long is it's
278 // running. This way the bitmap can be GCed when the animations are complete.
Chet Haase472b2812010-10-14 07:02:04 -0700279 anim.getAnimator().addListener(new AnimatorListenerAdapter() {
Michael Jurka3c4c20f2010-10-28 15:36:06 -0700280 @Override
Joe Onorato4be866d2010-10-10 11:26:02 -0700281 public void onAnimationEnd(Animator animation) {
Chet Haase472b2812010-10-14 07:02:04 -0700282 if ((Float) ((ValueAnimator) animation).getAnimatedValue() == 0f) {
Joe Onorato4be866d2010-10-10 11:26:02 -0700283 anim.setTag(null);
284 }
285 }
286 });
287 mDragOutlineAnims[i] = anim;
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700288 }
Patrick Dubroyce34a972010-10-19 10:34:32 -0700289
Sunny Goyalc13403c2016-11-18 23:44:48 -0800290 mShortcutsAndWidgets = new ShortcutAndWidgetContainer(context, mContainerType);
Sunny Goyalaa8a8712016-11-20 15:26:01 +0530291 mShortcutsAndWidgets.setCellDimensions(mCellWidth, mCellHeight, mCountX, mCountY);
Adam Cohen2374abf2013-04-16 14:56:57 -0700292
Mady Mellorbb835202015-07-15 16:34:34 -0700293 mStylusEventHelper = new StylusEventHelper(new SimpleOnStylusPressListener(this), this);
Mady Melloref044dd2015-06-02 15:35:07 -0700294
Sunny Goyal4fe5a372015-05-14 19:55:10 -0700295 mTouchFeedbackView = new ClickShadowView(context);
296 addView(mTouchFeedbackView);
Michael Jurkaa52570f2012-03-20 03:18:20 -0700297 addView(mShortcutsAndWidgets);
Michael Jurka18014792010-10-14 09:01:34 -0700298 }
299
Adam Cohenc9735cf2015-01-23 16:11:55 -0800300 @TargetApi(Build.VERSION_CODES.LOLLIPOP)
Sunny Goyale9b651e2015-04-24 11:44:51 -0700301 public void enableAccessibleDrag(boolean enable, int dragType) {
Adam Cohenc9735cf2015-01-23 16:11:55 -0800302 mUseTouchHelper = enable;
303 if (!enable) {
304 ViewCompat.setAccessibilityDelegate(this, null);
305 setImportantForAccessibility(IMPORTANT_FOR_ACCESSIBILITY_NO);
306 getShortcutsAndWidgets().setImportantForAccessibility(IMPORTANT_FOR_ACCESSIBILITY_NO);
307 setOnClickListener(mLauncher);
308 } else {
Sunny Goyale9b651e2015-04-24 11:44:51 -0700309 if (dragType == WORKSPACE_ACCESSIBILITY_DRAG &&
310 !(mTouchHelper instanceof WorkspaceAccessibilityHelper)) {
311 mTouchHelper = new WorkspaceAccessibilityHelper(this);
312 } else if (dragType == FOLDER_ACCESSIBILITY_DRAG &&
313 !(mTouchHelper instanceof FolderAccessibilityHelper)) {
314 mTouchHelper = new FolderAccessibilityHelper(this);
315 }
Adam Cohenc9735cf2015-01-23 16:11:55 -0800316 ViewCompat.setAccessibilityDelegate(this, mTouchHelper);
317 setImportantForAccessibility(IMPORTANT_FOR_ACCESSIBILITY_YES);
318 getShortcutsAndWidgets().setImportantForAccessibility(IMPORTANT_FOR_ACCESSIBILITY_YES);
319 setOnClickListener(mTouchHelper);
320 }
321
322 // Invalidate the accessibility hierarchy
323 if (getParent() != null) {
324 getParent().notifySubtreeAccessibilityStateChanged(
325 this, this, AccessibilityEvent.CONTENT_CHANGE_TYPE_SUBTREE);
326 }
327 }
328
329 @Override
330 public boolean dispatchHoverEvent(MotionEvent event) {
331 // Always attempt to dispatch hover events to accessibility first.
332 if (mUseTouchHelper && mTouchHelper.dispatchHoverEvent(event)) {
333 return true;
334 }
335 return super.dispatchHoverEvent(event);
336 }
337
338 @Override
Adam Cohenc9735cf2015-01-23 16:11:55 -0800339 public boolean onInterceptTouchEvent(MotionEvent ev) {
340 if (mUseTouchHelper ||
341 (mInterceptTouchListener != null && mInterceptTouchListener.onTouch(this, ev))) {
342 return true;
343 }
344 return false;
345 }
346
Mady Melloref044dd2015-06-02 15:35:07 -0700347 @Override
348 public boolean onTouchEvent(MotionEvent ev) {
349 boolean handled = super.onTouchEvent(ev);
350 // Stylus button press on a home screen should not switch between overview mode and
351 // the home screen mode, however, once in overview mode stylus button press should be
352 // enabled to allow rearranging the different home screens. So check what mode
353 // the workspace is in, and only perform stylus button presses while in overview mode.
354 if (mLauncher.mWorkspace.isInOverviewMode()
Mady Mellorbb835202015-07-15 16:34:34 -0700355 && mStylusEventHelper.onMotionEvent(ev)) {
Mady Melloref044dd2015-06-02 15:35:07 -0700356 return true;
357 }
358 return handled;
359 }
360
Chris Craik01f2d7f2013-10-01 14:41:56 -0700361 public void enableHardwareLayer(boolean hasLayer) {
362 mShortcutsAndWidgets.setLayerType(hasLayer ? LAYER_TYPE_HARDWARE : LAYER_TYPE_NONE, sPaint);
Michael Jurkad51f33a2012-06-28 15:35:26 -0700363 }
364
365 public void buildHardwareLayer() {
366 mShortcutsAndWidgets.buildLayer();
Adam Cohen2801caf2011-05-13 20:57:39 -0700367 }
368
Winson Chung5f8afe62013-08-12 16:19:28 -0700369 public void setCellDimensions(int width, int height) {
Winson Chung11a1a532013-09-13 11:14:45 -0700370 mFixedCellWidth = mCellWidth = width;
371 mFixedCellHeight = mCellHeight = height;
Sunny Goyalaa8a8712016-11-20 15:26:01 +0530372 mShortcutsAndWidgets.setCellDimensions(mCellWidth, mCellHeight, mCountX, mCountY);
Winson Chung5f8afe62013-08-12 16:19:28 -0700373 }
374
Adam Cohen2801caf2011-05-13 20:57:39 -0700375 public void setGridSize(int x, int y) {
376 mCountX = x;
377 mCountY = y;
Sunny Goyalff4ba2d2016-04-02 14:12:34 -0700378 mOccupied = new GridOccupancy(mCountX, mCountY);
379 mTmpOccupied = new GridOccupancy(mCountX, mCountY);
Adam Cohen7fbec102012-03-27 12:42:19 -0700380 mTempRectStack.clear();
Sunny Goyalaa8a8712016-11-20 15:26:01 +0530381 mShortcutsAndWidgets.setCellDimensions(mCellWidth, mCellHeight, mCountX, mCountY);
Adam Cohen76fc0852011-06-17 13:26:23 -0700382 requestLayout();
Adam Cohen2801caf2011-05-13 20:57:39 -0700383 }
384
Adam Cohen2374abf2013-04-16 14:56:57 -0700385 // Set whether or not to invert the layout horizontally if the layout is in RTL mode.
386 public void setInvertIfRtl(boolean invert) {
387 mShortcutsAndWidgets.setInvertIfRtl(invert);
388 }
389
Adam Cohen917e3882013-10-31 15:03:35 -0700390 public void setDropPending(boolean pending) {
391 mDropPending = pending;
392 }
393
394 public boolean isDropPending() {
395 return mDropPending;
396 }
397
Sunny Goyal4b6eb262015-05-14 19:24:40 -0700398 @Override
399 public void setPressedIcon(BubbleTextView icon, Bitmap background) {
Sunny Goyal508da152014-08-14 10:53:27 -0700400 if (icon == null || background == null) {
401 mTouchFeedbackView.setBitmap(null);
402 mTouchFeedbackView.animate().cancel();
403 } else {
Sunny Goyal508da152014-08-14 10:53:27 -0700404 if (mTouchFeedbackView.setBitmap(background)) {
Winsonbe9798b2016-07-20 12:55:49 -0700405 mTouchFeedbackView.alignWithIconView(icon, mShortcutsAndWidgets,
406 null /* clipAgainstView */);
Sunny Goyal4fe5a372015-05-14 19:55:10 -0700407 mTouchFeedbackView.animateShadow();
Sunny Goyal508da152014-08-14 10:53:27 -0700408 }
Patrick Dubroy96864c32011-03-10 17:17:23 -0800409 }
410 }
411
Adam Cohenc50438c2014-08-19 17:43:05 -0700412 void disableDragTarget() {
413 mIsDragTarget = false;
414 }
415
Tony Wickham0f97b782015-12-02 17:55:07 -0800416 public boolean isDragTarget() {
417 return mIsDragTarget;
418 }
419
Adam Cohenc50438c2014-08-19 17:43:05 -0700420 void setIsDragOverlapping(boolean isDragOverlapping) {
421 if (mIsDragOverlapping != isDragOverlapping) {
422 mIsDragOverlapping = isDragOverlapping;
Sunny Goyalf5440cb2016-12-14 15:13:00 -0800423 mBackground.setState(mIsDragOverlapping
424 ? BACKGROUND_STATE_ACTIVE : BACKGROUND_STATE_DEFAULT);
Adam Cohenc50438c2014-08-19 17:43:05 -0700425 invalidate();
426 }
427 }
428
Sunny Goyale2fd14b2015-08-27 17:45:46 -0700429 public void disableJailContent() {
430 mJailContent = false;
431 }
432
433 @Override
434 protected void dispatchSaveInstanceState(SparseArray<Parcelable> container) {
435 if (mJailContent) {
436 ParcelableSparseArray jail = getJailedArray(container);
437 super.dispatchSaveInstanceState(jail);
438 container.put(R.id.cell_layout_jail_id, jail);
439 } else {
440 super.dispatchSaveInstanceState(container);
441 }
442 }
443
444 @Override
445 protected void dispatchRestoreInstanceState(SparseArray<Parcelable> container) {
446 super.dispatchRestoreInstanceState(mJailContent ? getJailedArray(container) : container);
447 }
448
449 private ParcelableSparseArray getJailedArray(SparseArray<Parcelable> container) {
450 final Parcelable parcelable = container.get(R.id.cell_layout_jail_id);
451 return parcelable instanceof ParcelableSparseArray ?
452 (ParcelableSparseArray) parcelable : new ParcelableSparseArray();
453 }
454
Tony Wickham0f97b782015-12-02 17:55:07 -0800455 public boolean getIsDragOverlapping() {
456 return mIsDragOverlapping;
457 }
458
Jeff Sharkey83f111d2009-04-20 21:03:13 -0700459 @Override
Patrick Dubroy1262e362010-10-06 15:49:50 -0700460 protected void onDraw(Canvas canvas) {
Sunny Goyal05739772015-05-19 19:59:09 -0700461 if (!mIsDragTarget) {
462 return;
463 }
464
Michael Jurka3e7c7632010-10-02 16:01:03 -0700465 // When we're large, we are either drawn in a "hover" state (ie when dragging an item to
466 // a neighboring page) or with just a normal background (if backgroundAlpha > 0.0f)
467 // When we're small, we are either drawn normally or in the "accepts drops" state (during
468 // a drag). However, we also drag the mini hover background *over* one of those two
469 // backgrounds
Sunny Goyal05739772015-05-19 19:59:09 -0700470 if (mBackgroundAlpha > 0.0f) {
Sunny Goyal2805e632015-05-20 15:35:32 -0700471 mBackground.draw(canvas);
Michael Jurkaa63c4522010-08-19 13:52:27 -0700472 }
Romain Guya6abce82009-11-10 02:54:41 -0800473
Patrick Dubroy8e58e912010-10-14 13:21:48 -0700474 final Paint paint = mDragOutlinePaint;
Joe Onorato4be866d2010-10-10 11:26:02 -0700475 for (int i = 0; i < mDragOutlines.length; i++) {
Chet Haase472b2812010-10-14 07:02:04 -0700476 final float alpha = mDragOutlineAlphas[i];
Joe Onorato4be866d2010-10-10 11:26:02 -0700477 if (alpha > 0) {
Joe Onorato4be866d2010-10-10 11:26:02 -0700478 final Bitmap b = (Bitmap) mDragOutlineAnims[i].getTag();
Chet Haase472b2812010-10-14 07:02:04 -0700479 paint.setAlpha((int)(alpha + .5f));
Sunny Goyal106bf642015-07-16 12:18:06 -0700480 canvas.drawBitmap(b, null, mDragOutlines[i], paint);
Winson Chung150fbab2010-09-29 17:14:26 -0700481 }
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700482 }
Patrick Dubroy96864c32011-03-10 17:17:23 -0800483
Adam Cohen482ed822012-03-02 14:15:13 -0800484 if (DEBUG_VISUALIZE_OCCUPIED) {
485 int[] pt = new int[2];
486 ColorDrawable cd = new ColorDrawable(Color.RED);
Adam Cohene7587d22012-05-24 18:50:02 -0700487 cd.setBounds(0, 0, mCellWidth, mCellHeight);
Adam Cohen482ed822012-03-02 14:15:13 -0800488 for (int i = 0; i < mCountX; i++) {
489 for (int j = 0; j < mCountY; j++) {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -0700490 if (mOccupied.cells[i][j]) {
Adam Cohen482ed822012-03-02 14:15:13 -0800491 cellToPoint(i, j, pt);
492 canvas.save();
493 canvas.translate(pt[0], pt[1]);
494 cd.draw(canvas);
495 canvas.restore();
496 }
497 }
498 }
499 }
500
Adam Cohenefca0272016-02-24 19:19:06 -0800501 for (int i = 0; i < mFolderBackgrounds.size(); i++) {
502 FolderIcon.PreviewBackground bg = mFolderBackgrounds.get(i);
503 cellToPoint(bg.delegateCellX, bg.delegateCellY, mTempLocation);
504 canvas.save();
505 canvas.translate(mTempLocation[0], mTempLocation[1]);
506 bg.drawBackground(canvas, mFolderBgPaint);
Adam Cohenf172b742016-03-30 19:28:34 -0700507 if (!bg.isClipping) {
508 bg.drawBackgroundStroke(canvas, mFolderBgPaint);
509 }
Adam Cohenefca0272016-02-24 19:19:06 -0800510 canvas.restore();
Adam Cohen69ce2e52011-07-03 19:25:21 -0700511 }
Adam Cohenc51934b2011-07-26 21:07:43 -0700512
Adam Cohenefca0272016-02-24 19:19:06 -0800513 if (mFolderLeaveBehind.delegateCellX >= 0 && mFolderLeaveBehind.delegateCellY >= 0) {
514 cellToPoint(mFolderLeaveBehind.delegateCellX,
515 mFolderLeaveBehind.delegateCellY, mTempLocation);
516 canvas.save();
517 canvas.translate(mTempLocation[0], mTempLocation[1]);
518 mFolderLeaveBehind.drawLeaveBehind(canvas, mFolderBgPaint);
519 canvas.restore();
Adam Cohenc51934b2011-07-26 21:07:43 -0700520 }
Adam Cohen69ce2e52011-07-03 19:25:21 -0700521 }
522
Adam Cohenefca0272016-02-24 19:19:06 -0800523 @Override
524 protected void dispatchDraw(Canvas canvas) {
525 super.dispatchDraw(canvas);
526
527 for (int i = 0; i < mFolderBackgrounds.size(); i++) {
528 FolderIcon.PreviewBackground bg = mFolderBackgrounds.get(i);
Adam Cohenf172b742016-03-30 19:28:34 -0700529 if (bg.isClipping) {
530 cellToPoint(bg.delegateCellX, bg.delegateCellY, mTempLocation);
531 canvas.save();
532 canvas.translate(mTempLocation[0], mTempLocation[1]);
533 bg.drawBackgroundStroke(canvas, mFolderBgPaint);
534 canvas.restore();
535 }
Adam Cohenefca0272016-02-24 19:19:06 -0800536 }
Adam Cohen69ce2e52011-07-03 19:25:21 -0700537 }
538
Adam Cohenefca0272016-02-24 19:19:06 -0800539 public void addFolderBackground(FolderIcon.PreviewBackground bg) {
540 mFolderBackgrounds.add(bg);
541 }
542 public void removeFolderBackground(FolderIcon.PreviewBackground bg) {
543 mFolderBackgrounds.remove(bg);
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700544 }
545
Adam Cohenc51934b2011-07-26 21:07:43 -0700546 public void setFolderLeaveBehindCell(int x, int y) {
Adam Cohenefca0272016-02-24 19:19:06 -0800547
548 DeviceProfile grid = mLauncher.getDeviceProfile();
549 View child = getChildAt(x, y);
550
551 mFolderLeaveBehind.setup(getResources().getDisplayMetrics(), grid, null,
552 child.getMeasuredWidth(), child.getPaddingTop());
553
554 mFolderLeaveBehind.delegateCellX = x;
555 mFolderLeaveBehind.delegateCellY = y;
Adam Cohenc51934b2011-07-26 21:07:43 -0700556 invalidate();
557 }
558
559 public void clearFolderLeaveBehind() {
Adam Cohenefca0272016-02-24 19:19:06 -0800560 mFolderLeaveBehind.delegateCellX = -1;
561 mFolderLeaveBehind.delegateCellY = -1;
Adam Cohenc51934b2011-07-26 21:07:43 -0700562 invalidate();
563 }
564
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700565 @Override
Michael Jurkae6235dd2011-10-04 15:02:05 -0700566 public boolean shouldDelayChildPressedState() {
567 return false;
568 }
569
Adam Cohen1462de32012-07-24 22:34:36 -0700570 public void restoreInstanceState(SparseArray<Parcelable> states) {
Sunny Goyal33a152f2014-07-22 12:13:14 -0700571 try {
572 dispatchRestoreInstanceState(states);
573 } catch (IllegalArgumentException ex) {
Sunny Goyal6c56c682015-07-16 14:09:05 -0700574 if (ProviderConfig.IS_DOGFOOD_BUILD) {
Sunny Goyal33a152f2014-07-22 12:13:14 -0700575 throw ex;
576 }
577 // Mismatched viewId / viewType preventing restore. Skip restore on production builds.
578 Log.e(TAG, "Ignoring an error while restoring a view instance state", ex);
579 }
Adam Cohen1462de32012-07-24 22:34:36 -0700580 }
581
Michael Jurkae6235dd2011-10-04 15:02:05 -0700582 @Override
Jeff Sharkey83f111d2009-04-20 21:03:13 -0700583 public void cancelLongPress() {
584 super.cancelLongPress();
585
586 // Cancel long press for all children
587 final int count = getChildCount();
588 for (int i = 0; i < count; i++) {
589 final View child = getChildAt(i);
590 child.cancelLongPress();
591 }
592 }
593
Michael Jurkadee05892010-07-27 10:01:56 -0700594 public void setOnInterceptTouchListener(View.OnTouchListener listener) {
595 mInterceptTouchListener = listener;
596 }
597
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800598 public int getCountX() {
Adam Cohend22015c2010-07-26 22:02:18 -0700599 return mCountX;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800600 }
601
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800602 public int getCountY() {
Adam Cohend22015c2010-07-26 22:02:18 -0700603 return mCountY;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800604 }
605
Sunny Goyalc13403c2016-11-18 23:44:48 -0800606 public boolean acceptsWidget() {
607 return mContainerType == WORKSPACE;
Sunny Goyale9b651e2015-04-24 11:44:51 -0700608 }
609
Andrew Flynn0dca1ec2012-02-29 13:33:22 -0800610 public boolean addViewToCellLayout(View child, int index, int childId, LayoutParams params,
Sunny Goyalf7a29e82015-04-24 15:20:43 -0700611 boolean markCells) {
Winson Chungaafa03c2010-06-11 17:34:16 -0700612 final LayoutParams lp = params;
613
Andrew Flynnde38e422012-05-08 11:22:15 -0700614 // Hotseat icons - remove text
Andrew Flynn0dca1ec2012-02-29 13:33:22 -0800615 if (child instanceof BubbleTextView) {
616 BubbleTextView bubbleChild = (BubbleTextView) child;
Sunny Goyalc13403c2016-11-18 23:44:48 -0800617 bubbleChild.setTextVisibility(mContainerType != HOTSEAT);
Andrew Flynn0dca1ec2012-02-29 13:33:22 -0800618 }
619
Sunny Goyalc13403c2016-11-18 23:44:48 -0800620 child.setScaleX(mChildScale);
621 child.setScaleY(mChildScale);
Adam Cohen307fe232012-08-16 17:55:58 -0700622
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800623 // Generate an id for each view, this assumes we have at most 256x256 cells
624 // per workspace screen
Adam Cohend22015c2010-07-26 22:02:18 -0700625 if (lp.cellX >= 0 && lp.cellX <= mCountX - 1 && lp.cellY >= 0 && lp.cellY <= mCountY - 1) {
Winson Chungaafa03c2010-06-11 17:34:16 -0700626 // If the horizontal or vertical span is set to -1, it is taken to
627 // mean that it spans the extent of the CellLayout
Adam Cohend22015c2010-07-26 22:02:18 -0700628 if (lp.cellHSpan < 0) lp.cellHSpan = mCountX;
629 if (lp.cellVSpan < 0) lp.cellVSpan = mCountY;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800630
Winson Chungaafa03c2010-06-11 17:34:16 -0700631 child.setId(childId);
Tony Wickhama0628cc2015-10-14 15:23:04 -0700632 if (LOGD) {
633 Log.d(TAG, "Adding view to ShortcutsAndWidgetsContainer: " + child);
634 }
Sunny Goyalf7a29e82015-04-24 15:20:43 -0700635 mShortcutsAndWidgets.addView(child, index, lp);
Michael Jurkadee05892010-07-27 10:01:56 -0700636
Michael Jurkaf3ca3ab2010-10-20 17:08:24 -0700637 if (markCells) markCellsAsOccupiedForView(child);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700638
Winson Chungaafa03c2010-06-11 17:34:16 -0700639 return true;
640 }
641 return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800642 }
Michael Jurka3e7c7632010-10-02 16:01:03 -0700643
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800644 @Override
Michael Jurka0280c3b2010-09-17 15:00:07 -0700645 public void removeAllViews() {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -0700646 mOccupied.clear();
Michael Jurkaa52570f2012-03-20 03:18:20 -0700647 mShortcutsAndWidgets.removeAllViews();
Michael Jurka0280c3b2010-09-17 15:00:07 -0700648 }
649
650 @Override
651 public void removeAllViewsInLayout() {
Michael Jurkaa52570f2012-03-20 03:18:20 -0700652 if (mShortcutsAndWidgets.getChildCount() > 0) {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -0700653 mOccupied.clear();
Michael Jurkaa52570f2012-03-20 03:18:20 -0700654 mShortcutsAndWidgets.removeAllViewsInLayout();
Michael Jurka7cfc2822011-08-02 20:19:24 -0700655 }
Michael Jurka0280c3b2010-09-17 15:00:07 -0700656 }
657
658 @Override
659 public void removeView(View view) {
660 markCellsAsUnoccupiedForView(view);
Michael Jurkaa52570f2012-03-20 03:18:20 -0700661 mShortcutsAndWidgets.removeView(view);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700662 }
663
664 @Override
665 public void removeViewAt(int index) {
Michael Jurkaa52570f2012-03-20 03:18:20 -0700666 markCellsAsUnoccupiedForView(mShortcutsAndWidgets.getChildAt(index));
667 mShortcutsAndWidgets.removeViewAt(index);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700668 }
669
670 @Override
671 public void removeViewInLayout(View view) {
672 markCellsAsUnoccupiedForView(view);
Michael Jurkaa52570f2012-03-20 03:18:20 -0700673 mShortcutsAndWidgets.removeViewInLayout(view);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700674 }
675
676 @Override
677 public void removeViews(int start, int count) {
678 for (int i = start; i < start + count; i++) {
Michael Jurkaa52570f2012-03-20 03:18:20 -0700679 markCellsAsUnoccupiedForView(mShortcutsAndWidgets.getChildAt(i));
Michael Jurka0280c3b2010-09-17 15:00:07 -0700680 }
Michael Jurkaa52570f2012-03-20 03:18:20 -0700681 mShortcutsAndWidgets.removeViews(start, count);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700682 }
683
684 @Override
685 public void removeViewsInLayout(int start, int count) {
686 for (int i = start; i < start + count; i++) {
Michael Jurkaa52570f2012-03-20 03:18:20 -0700687 markCellsAsUnoccupiedForView(mShortcutsAndWidgets.getChildAt(i));
Michael Jurka0280c3b2010-09-17 15:00:07 -0700688 }
Michael Jurkaa52570f2012-03-20 03:18:20 -0700689 mShortcutsAndWidgets.removeViewsInLayout(start, count);
Michael Jurkaabded662011-03-04 12:06:57 -0800690 }
691
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700692 /**
Winson Chungaafa03c2010-06-11 17:34:16 -0700693 * Given a point, return the cell that strictly encloses that point
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800694 * @param x X coordinate of the point
695 * @param y Y coordinate of the point
696 * @param result Array of 2 ints to hold the x and y coordinate of the cell
697 */
Sunny Goyale9b651e2015-04-24 11:44:51 -0700698 public void pointToCellExact(int x, int y, int[] result) {
Winson Chung4b825dcd2011-06-19 12:41:22 -0700699 final int hStartPadding = getPaddingLeft();
700 final int vStartPadding = getPaddingTop();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800701
Sunny Goyalaa8a8712016-11-20 15:26:01 +0530702 result[0] = (x - hStartPadding) / mCellWidth;
703 result[1] = (y - vStartPadding) / mCellHeight;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800704
Adam Cohend22015c2010-07-26 22:02:18 -0700705 final int xAxis = mCountX;
706 final int yAxis = mCountY;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800707
708 if (result[0] < 0) result[0] = 0;
709 if (result[0] >= xAxis) result[0] = xAxis - 1;
710 if (result[1] < 0) result[1] = 0;
711 if (result[1] >= yAxis) result[1] = yAxis - 1;
712 }
Winson Chungaafa03c2010-06-11 17:34:16 -0700713
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800714 /**
715 * Given a point, return the cell that most closely encloses that point
716 * @param x X coordinate of the point
717 * @param y Y coordinate of the point
718 * @param result Array of 2 ints to hold the x and y coordinate of the cell
719 */
720 void pointToCellRounded(int x, int y, int[] result) {
721 pointToCellExact(x + (mCellWidth / 2), y + (mCellHeight / 2), result);
722 }
723
724 /**
725 * Given a cell coordinate, return the point that represents the upper left corner of that cell
Winson Chungaafa03c2010-06-11 17:34:16 -0700726 *
727 * @param cellX X coordinate of the cell
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800728 * @param cellY Y coordinate of the cell
Winson Chungaafa03c2010-06-11 17:34:16 -0700729 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800730 * @param result Array of 2 ints to hold the x and y coordinate of the point
731 */
732 void cellToPoint(int cellX, int cellY, int[] result) {
Winson Chung4b825dcd2011-06-19 12:41:22 -0700733 final int hStartPadding = getPaddingLeft();
734 final int vStartPadding = getPaddingTop();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800735
Sunny Goyalaa8a8712016-11-20 15:26:01 +0530736 result[0] = hStartPadding + cellX * mCellWidth;
737 result[1] = vStartPadding + cellY * mCellHeight;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800738 }
739
Adam Cohene3e27a82011-04-15 12:07:39 -0700740 /**
Adam Cohen482ed822012-03-02 14:15:13 -0800741 * Given a cell coordinate, return the point that represents the center of the cell
Adam Cohene3e27a82011-04-15 12:07:39 -0700742 *
743 * @param cellX X coordinate of the cell
744 * @param cellY Y coordinate of the cell
745 *
746 * @param result Array of 2 ints to hold the x and y coordinate of the point
747 */
748 void cellToCenterPoint(int cellX, int cellY, int[] result) {
Adam Cohen47a876d2012-03-19 13:21:41 -0700749 regionToCenterPoint(cellX, cellY, 1, 1, result);
750 }
751
752 /**
753 * Given a cell coordinate and span return the point that represents the center of the regio
754 *
755 * @param cellX X coordinate of the cell
756 * @param cellY Y coordinate of the cell
757 *
758 * @param result Array of 2 ints to hold the x and y coordinate of the point
759 */
760 void regionToCenterPoint(int cellX, int cellY, int spanX, int spanY, int[] result) {
Winson Chung4b825dcd2011-06-19 12:41:22 -0700761 final int hStartPadding = getPaddingLeft();
762 final int vStartPadding = getPaddingTop();
Sunny Goyalaa8a8712016-11-20 15:26:01 +0530763 result[0] = hStartPadding + cellX * mCellWidth + (spanX * mCellWidth) / 2;
764 result[1] = vStartPadding + cellY * mCellHeight + (spanY * mCellHeight) / 2;
Adam Cohene3e27a82011-04-15 12:07:39 -0700765 }
766
Adam Cohen19f37922012-03-21 11:59:11 -0700767 /**
768 * Given a cell coordinate and span fills out a corresponding pixel rect
769 *
770 * @param cellX X coordinate of the cell
771 * @param cellY Y coordinate of the cell
772 * @param result Rect in which to write the result
773 */
774 void regionToRect(int cellX, int cellY, int spanX, int spanY, Rect result) {
775 final int hStartPadding = getPaddingLeft();
776 final int vStartPadding = getPaddingTop();
Sunny Goyalaa8a8712016-11-20 15:26:01 +0530777 final int left = hStartPadding + cellX * mCellWidth;
778 final int top = vStartPadding + cellY * mCellHeight;
779 result.set(left, top, left + (spanX * mCellWidth), top + (spanY * mCellHeight));
Adam Cohen19f37922012-03-21 11:59:11 -0700780 }
781
Adam Cohen482ed822012-03-02 14:15:13 -0800782 public float getDistanceFromCell(float x, float y, int[] cell) {
783 cellToCenterPoint(cell[0], cell[1], mTmpPoint);
Sunny Goyalf7a29e82015-04-24 15:20:43 -0700784 return (float) Math.hypot(x - mTmpPoint[0], y - mTmpPoint[1]);
Adam Cohen482ed822012-03-02 14:15:13 -0800785 }
786
Adam Cohenf9c184a2016-01-15 16:47:43 -0800787 public int getCellWidth() {
Romain Guy84f296c2009-11-04 15:00:44 -0800788 return mCellWidth;
789 }
790
791 int getCellHeight() {
792 return mCellHeight;
793 }
794
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700795 public void setFixedSize(int width, int height) {
796 mFixedWidth = width;
797 mFixedHeight = height;
798 }
799
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800800 @Override
801 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800802 int widthSpecMode = MeasureSpec.getMode(widthMeasureSpec);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800803 int heightSpecMode = MeasureSpec.getMode(heightMeasureSpec);
Winson Chung5f8afe62013-08-12 16:19:28 -0700804 int widthSize = MeasureSpec.getSize(widthMeasureSpec);
805 int heightSize = MeasureSpec.getSize(heightMeasureSpec);
Winson Chung2d75f122013-09-23 16:53:31 -0700806 int childWidthSize = widthSize - (getPaddingLeft() + getPaddingRight());
807 int childHeightSize = heightSize - (getPaddingTop() + getPaddingBottom());
Winson Chung11a1a532013-09-13 11:14:45 -0700808 if (mFixedCellWidth < 0 || mFixedCellHeight < 0) {
Sunny Goyalc6205602015-05-21 20:46:33 -0700809 int cw = DeviceProfile.calculateCellWidth(childWidthSize, mCountX);
810 int ch = DeviceProfile.calculateCellHeight(childHeightSize, mCountY);
Winson Chung11a1a532013-09-13 11:14:45 -0700811 if (cw != mCellWidth || ch != mCellHeight) {
812 mCellWidth = cw;
813 mCellHeight = ch;
Sunny Goyalaa8a8712016-11-20 15:26:01 +0530814 mShortcutsAndWidgets.setCellDimensions(mCellWidth, mCellHeight, mCountX, mCountY);
Winson Chung11a1a532013-09-13 11:14:45 -0700815 }
Winson Chung5f8afe62013-08-12 16:19:28 -0700816 }
Winson Chungaafa03c2010-06-11 17:34:16 -0700817
Winson Chung2d75f122013-09-23 16:53:31 -0700818 int newWidth = childWidthSize;
819 int newHeight = childHeightSize;
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700820 if (mFixedWidth > 0 && mFixedHeight > 0) {
821 newWidth = mFixedWidth;
822 newHeight = mFixedHeight;
823 } else if (widthSpecMode == MeasureSpec.UNSPECIFIED || heightSpecMode == MeasureSpec.UNSPECIFIED) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800824 throw new RuntimeException("CellLayout cannot have UNSPECIFIED dimensions");
825 }
826
Sunny Goyal4fe5a372015-05-14 19:55:10 -0700827 // Make the feedback view large enough to hold the blur bitmap.
828 mTouchFeedbackView.measure(
829 MeasureSpec.makeMeasureSpec(mCellWidth + mTouchFeedbackView.getExtraSize(),
830 MeasureSpec.EXACTLY),
831 MeasureSpec.makeMeasureSpec(mCellHeight + mTouchFeedbackView.getExtraSize(),
832 MeasureSpec.EXACTLY));
833
834 mShortcutsAndWidgets.measure(
835 MeasureSpec.makeMeasureSpec(newWidth, MeasureSpec.EXACTLY),
836 MeasureSpec.makeMeasureSpec(newHeight, MeasureSpec.EXACTLY));
837
838 int maxWidth = mShortcutsAndWidgets.getMeasuredWidth();
839 int maxHeight = mShortcutsAndWidgets.getMeasuredHeight();
Winson Chung2d75f122013-09-23 16:53:31 -0700840 if (mFixedWidth > 0 && mFixedHeight > 0) {
841 setMeasuredDimension(maxWidth, maxHeight);
842 } else {
843 setMeasuredDimension(widthSize, heightSize);
844 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800845 }
846
847 @Override
Michael Jurka28750fb2010-09-24 17:43:49 -0700848 protected void onLayout(boolean changed, int l, int t, int r, int b) {
Tony Wickham26b01422015-11-10 14:44:32 -0800849 boolean isFullscreen = mShortcutsAndWidgets.getChildCount() > 0 &&
850 ((LayoutParams) mShortcutsAndWidgets.getChildAt(0).getLayoutParams()).isFullscreen;
851 int left = getPaddingLeft();
852 if (!isFullscreen) {
Tony Wickhama501d492015-11-03 18:05:01 -0800853 left += (int) Math.ceil(getUnusedHorizontalSpace() / 2f);
Tony Wickham26b01422015-11-10 14:44:32 -0800854 }
Sunny Goyal7c786f72016-06-01 14:08:21 -0700855 int right = r - l - getPaddingRight();
856 if (!isFullscreen) {
857 right -= (int) Math.ceil(getUnusedHorizontalSpace() / 2f);
858 }
859
Winson Chung38848ca2013-10-08 12:03:44 -0700860 int top = getPaddingTop();
Sunny Goyal7c786f72016-06-01 14:08:21 -0700861 int bottom = b - t - getPaddingBottom();
Sunny Goyal4fe5a372015-05-14 19:55:10 -0700862
863 mTouchFeedbackView.layout(left, top,
864 left + mTouchFeedbackView.getMeasuredWidth(),
865 top + mTouchFeedbackView.getMeasuredHeight());
Sunny Goyal7c786f72016-06-01 14:08:21 -0700866 mShortcutsAndWidgets.layout(left, top, right, bottom);
867
868 // Expand the background drawing bounds by the padding baked into the background drawable
869 mBackground.getPadding(mTempRect);
870 mBackground.setBounds(
871 left - mTempRect.left,
872 top - mTempRect.top,
873 right + mTempRect.right,
874 bottom + mTempRect.bottom);
875 }
876
Tony Wickhama501d492015-11-03 18:05:01 -0800877 /**
878 * Returns the amount of space left over after subtracting padding and cells. This space will be
879 * very small, a few pixels at most, and is a result of rounding down when calculating the cell
880 * width in {@link DeviceProfile#calculateCellWidth(int, int)}.
881 */
882 public int getUnusedHorizontalSpace() {
883 return getMeasuredWidth() - getPaddingLeft() - getPaddingRight() - (mCountX * mCellWidth);
884 }
885
Michael Jurka5f1c5092010-09-03 14:15:02 -0700886 public float getBackgroundAlpha() {
887 return mBackgroundAlpha;
Michael Jurkadee05892010-07-27 10:01:56 -0700888 }
889
Michael Jurka5f1c5092010-09-03 14:15:02 -0700890 public void setBackgroundAlpha(float alpha) {
Michael Jurkaafaa0502011-12-13 18:22:50 -0800891 if (mBackgroundAlpha != alpha) {
892 mBackgroundAlpha = alpha;
Sunny Goyal2805e632015-05-20 15:35:32 -0700893 mBackground.setAlpha((int) (mBackgroundAlpha * 255));
Michael Jurkaafaa0502011-12-13 18:22:50 -0800894 }
Michael Jurkadee05892010-07-27 10:01:56 -0700895 }
896
Sunny Goyal2805e632015-05-20 15:35:32 -0700897 @Override
898 protected boolean verifyDrawable(Drawable who) {
899 return super.verifyDrawable(who) || (mIsDragTarget && who == mBackground);
900 }
901
Michael Jurkaa52570f2012-03-20 03:18:20 -0700902 public void setShortcutAndWidgetAlpha(float alpha) {
Sunny Goyal02b50812014-09-10 15:44:42 -0700903 mShortcutsAndWidgets.setAlpha(alpha);
Michael Jurkadee05892010-07-27 10:01:56 -0700904 }
905
Michael Jurkaa52570f2012-03-20 03:18:20 -0700906 public ShortcutAndWidgetContainer getShortcutsAndWidgets() {
Sunny Goyaldcbcc862014-08-12 15:58:36 -0700907 return mShortcutsAndWidgets;
Michael Jurkaa52570f2012-03-20 03:18:20 -0700908 }
909
Patrick Dubroy440c3602010-07-13 17:50:32 -0700910 public View getChildAt(int x, int y) {
Michael Jurkaa52570f2012-03-20 03:18:20 -0700911 return mShortcutsAndWidgets.getChildAt(x, y);
Patrick Dubroy440c3602010-07-13 17:50:32 -0700912 }
913
Adam Cohen76fc0852011-06-17 13:26:23 -0700914 public boolean animateChildToPosition(final View child, int cellX, int cellY, int duration,
Adam Cohen482ed822012-03-02 14:15:13 -0800915 int delay, boolean permanent, boolean adjustOccupied) {
Michael Jurkaa52570f2012-03-20 03:18:20 -0700916 ShortcutAndWidgetContainer clc = getShortcutsAndWidgets();
Adam Cohen482ed822012-03-02 14:15:13 -0800917
Adam Cohen19f37922012-03-21 11:59:11 -0700918 if (clc.indexOfChild(child) != -1) {
Adam Cohenbfbfd262011-06-13 16:55:12 -0700919 final LayoutParams lp = (LayoutParams) child.getLayoutParams();
920 final ItemInfo info = (ItemInfo) child.getTag();
921
922 // We cancel any existing animations
923 if (mReorderAnimators.containsKey(lp)) {
924 mReorderAnimators.get(lp).cancel();
925 mReorderAnimators.remove(lp);
926 }
927
Adam Cohen482ed822012-03-02 14:15:13 -0800928 final int oldX = lp.x;
929 final int oldY = lp.y;
930 if (adjustOccupied) {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -0700931 GridOccupancy occupied = permanent ? mOccupied : mTmpOccupied;
932 occupied.markCells(lp.cellX, lp.cellY, lp.cellHSpan, lp.cellVSpan, false);
933 occupied.markCells(cellX, cellY, lp.cellHSpan, lp.cellVSpan, true);
Adam Cohen482ed822012-03-02 14:15:13 -0800934 }
Adam Cohenbfbfd262011-06-13 16:55:12 -0700935 lp.isLockedToGrid = true;
Adam Cohen482ed822012-03-02 14:15:13 -0800936 if (permanent) {
937 lp.cellX = info.cellX = cellX;
938 lp.cellY = info.cellY = cellY;
939 } else {
940 lp.tmpCellX = cellX;
941 lp.tmpCellY = cellY;
942 }
Jon Mirandae96798e2016-12-07 12:10:44 -0800943 clc.setupLp(child);
Adam Cohenbfbfd262011-06-13 16:55:12 -0700944 lp.isLockedToGrid = false;
Adam Cohen482ed822012-03-02 14:15:13 -0800945 final int newX = lp.x;
946 final int newY = lp.y;
Adam Cohenbfbfd262011-06-13 16:55:12 -0700947
Adam Cohen76fc0852011-06-17 13:26:23 -0700948 lp.x = oldX;
949 lp.y = oldY;
Adam Cohen76fc0852011-06-17 13:26:23 -0700950
Adam Cohen482ed822012-03-02 14:15:13 -0800951 // Exit early if we're not actually moving the view
952 if (oldX == newX && oldY == newY) {
953 lp.isLockedToGrid = true;
954 return true;
955 }
956
Michael Jurkaf1ad6082013-03-13 12:55:46 +0100957 ValueAnimator va = LauncherAnimUtils.ofFloat(child, 0f, 1f);
Adam Cohen482ed822012-03-02 14:15:13 -0800958 va.setDuration(duration);
959 mReorderAnimators.put(lp, va);
960
961 va.addUpdateListener(new AnimatorUpdateListener() {
962 @Override
Adam Cohenbfbfd262011-06-13 16:55:12 -0700963 public void onAnimationUpdate(ValueAnimator animation) {
Jon Mirandae96798e2016-12-07 12:10:44 -0800964 float r = (Float) animation.getAnimatedValue();
Adam Cohen19f37922012-03-21 11:59:11 -0700965 lp.x = (int) ((1 - r) * oldX + r * newX);
966 lp.y = (int) ((1 - r) * oldY + r * newY);
Adam Cohen6b8a02d2012-03-22 15:13:40 -0700967 child.requestLayout();
Adam Cohenbfbfd262011-06-13 16:55:12 -0700968 }
969 });
Adam Cohen482ed822012-03-02 14:15:13 -0800970 va.addListener(new AnimatorListenerAdapter() {
Adam Cohenbfbfd262011-06-13 16:55:12 -0700971 boolean cancelled = false;
972 public void onAnimationEnd(Animator animation) {
973 // If the animation was cancelled, it means that another animation
974 // has interrupted this one, and we don't want to lock the item into
975 // place just yet.
976 if (!cancelled) {
977 lp.isLockedToGrid = true;
Adam Cohen482ed822012-03-02 14:15:13 -0800978 child.requestLayout();
Adam Cohenbfbfd262011-06-13 16:55:12 -0700979 }
980 if (mReorderAnimators.containsKey(lp)) {
981 mReorderAnimators.remove(lp);
982 }
983 }
984 public void onAnimationCancel(Animator animation) {
985 cancelled = true;
986 }
987 });
Adam Cohen482ed822012-03-02 14:15:13 -0800988 va.setStartDelay(delay);
989 va.start();
Adam Cohenbfbfd262011-06-13 16:55:12 -0700990 return true;
991 }
992 return false;
993 }
994
Sunny Goyal06e21a22016-08-11 16:02:02 -0700995 void visualizeDropLocation(View v, DragPreviewProvider outlineProvider, int cellX, int cellY,
996 int spanX, int spanY, boolean resize, DropTarget.DragObject dragObject) {
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -0700997 final int oldDragCellX = mDragCell[0];
998 final int oldDragCellY = mDragCell[1];
Adam Cohen482ed822012-03-02 14:15:13 -0800999
Hyunyoung Song0de01172016-10-05 16:27:48 -07001000 if (outlineProvider == null || outlineProvider.generatedDragOutline == null) {
Adam Cohen2801caf2011-05-13 20:57:39 -07001001 return;
1002 }
1003
Hyunyoung Song0de01172016-10-05 16:27:48 -07001004 Bitmap dragOutline = outlineProvider.generatedDragOutline;
Adam Cohen482ed822012-03-02 14:15:13 -08001005 if (cellX != oldDragCellX || cellY != oldDragCellY) {
Sunny Goyale78e3d72015-09-24 11:23:31 -07001006 Point dragOffset = dragObject.dragView.getDragVisualizeOffset();
1007 Rect dragRegion = dragObject.dragView.getDragRegion();
1008
Adam Cohen482ed822012-03-02 14:15:13 -08001009 mDragCell[0] = cellX;
1010 mDragCell[1] = cellY;
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001011
Joe Onorato4be866d2010-10-10 11:26:02 -07001012 final int oldIndex = mDragOutlineCurrent;
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -07001013 mDragOutlineAnims[oldIndex].animateOut();
1014 mDragOutlineCurrent = (oldIndex + 1) % mDragOutlines.length;
Adam Cohend41fbf52012-02-16 23:53:59 -08001015 Rect r = mDragOutlines[mDragOutlineCurrent];
Sunny Goyal106bf642015-07-16 12:18:06 -07001016
Adam Cohend41fbf52012-02-16 23:53:59 -08001017 if (resize) {
Adam Cohen482ed822012-03-02 14:15:13 -08001018 cellToRect(cellX, cellY, spanX, spanY, r);
Jon Mirandae96798e2016-12-07 12:10:44 -08001019 if (v instanceof LauncherAppWidgetHostView) {
1020 DeviceProfile profile = mLauncher.getDeviceProfile();
Jon Miranda6f6a06a2016-12-15 11:24:18 -08001021 Utilities.shrinkRect(r, profile.appWidgetScale.x, profile.appWidgetScale.y);
Jon Mirandae96798e2016-12-07 12:10:44 -08001022 }
Sunny Goyal106bf642015-07-16 12:18:06 -07001023 } else {
1024 // Find the top left corner of the rect the object will occupy
1025 final int[] topLeft = mTmpPoint;
1026 cellToPoint(cellX, cellY, topLeft);
1027
1028 int left = topLeft[0];
1029 int top = topLeft[1];
1030
1031 if (v != null && dragOffset == null) {
1032 // When drawing the drag outline, it did not account for margin offsets
1033 // added by the view's parent.
1034 MarginLayoutParams lp = (MarginLayoutParams) v.getLayoutParams();
1035 left += lp.leftMargin;
1036 top += lp.topMargin;
1037
1038 // Offsets due to the size difference between the View and the dragOutline.
1039 // There is a size difference to account for the outer blur, which may lie
1040 // outside the bounds of the view.
Jon Mirandaf7ff3fe2016-12-05 12:04:44 -08001041 top += ((mCellHeight * spanY) - dragOutline.getHeight()) / 2;
Sunny Goyal106bf642015-07-16 12:18:06 -07001042 // We center about the x axis
Sunny Goyalaa8a8712016-11-20 15:26:01 +05301043 left += ((mCellWidth * spanX) - dragOutline.getWidth()) / 2;
Sunny Goyal106bf642015-07-16 12:18:06 -07001044 } else {
1045 if (dragOffset != null && dragRegion != null) {
1046 // Center the drag region *horizontally* in the cell and apply a drag
1047 // outline offset
Sunny Goyalaa8a8712016-11-20 15:26:01 +05301048 left += dragOffset.x + ((mCellWidth * spanX) - dragRegion.width()) / 2;
Sunny Goyal106bf642015-07-16 12:18:06 -07001049 int cHeight = getShortcutsAndWidgets().getCellContentHeight();
1050 int cellPaddingY = (int) Math.max(0, ((mCellHeight - cHeight) / 2f));
1051 top += dragOffset.y + cellPaddingY;
1052 } else {
1053 // Center the drag outline in the cell
Sunny Goyalaa8a8712016-11-20 15:26:01 +05301054 left += ((mCellWidth * spanX) - dragOutline.getWidth()) / 2;
1055 top += ((mCellHeight * spanY) - dragOutline.getHeight()) / 2;
Sunny Goyal106bf642015-07-16 12:18:06 -07001056 }
1057 }
1058 r.set(left, top, left + dragOutline.getWidth(), top + dragOutline.getHeight());
Adam Cohend41fbf52012-02-16 23:53:59 -08001059 }
Winson Chung150fbab2010-09-29 17:14:26 -07001060
Jon Miranda6f6a06a2016-12-15 11:24:18 -08001061 Utilities.scaleRectAboutCenter(r, mChildScale);
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -07001062 mDragOutlineAnims[mDragOutlineCurrent].setTag(dragOutline);
1063 mDragOutlineAnims[mDragOutlineCurrent].animateIn();
Sunny Goyale78e3d72015-09-24 11:23:31 -07001064
1065 if (dragObject.stateAnnouncer != null) {
Sunny Goyalc13403c2016-11-18 23:44:48 -08001066 dragObject.stateAnnouncer.announce(getItemMoveDescription(cellX, cellY));
Sunny Goyale78e3d72015-09-24 11:23:31 -07001067 }
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001068 }
1069 }
1070
Sunny Goyalc13403c2016-11-18 23:44:48 -08001071 public String getItemMoveDescription(int cellX, int cellY) {
1072 if (mContainerType == HOTSEAT) {
1073 return getContext().getString(R.string.move_to_hotseat_position,
1074 Math.max(cellX, cellY) + 1);
1075 } else {
1076 return getContext().getString(R.string.move_to_empty_cell,
1077 cellY + 1, cellX + 1);
1078 }
1079 }
1080
Adam Cohene0310962011-04-18 16:15:31 -07001081 public void clearDragOutlines() {
1082 final int oldIndex = mDragOutlineCurrent;
1083 mDragOutlineAnims[oldIndex].animateOut();
Adam Cohend41fbf52012-02-16 23:53:59 -08001084 mDragCell[0] = mDragCell[1] = -1;
Adam Cohene0310962011-04-18 16:15:31 -07001085 }
1086
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001087 /**
Jeff Sharkey70864282009-04-07 21:08:40 -07001088 * Find a vacant area that will fit the given bounds nearest the requested
1089 * cell location. Uses Euclidean distance to score multiple vacant areas.
Winson Chungaafa03c2010-06-11 17:34:16 -07001090 *
Romain Guy51afc022009-05-04 18:03:43 -07001091 * @param pixelX The X location at which you want to search for a vacant area.
1092 * @param pixelY The Y location at which you want to search for a vacant area.
Adam Cohend41fbf52012-02-16 23:53:59 -08001093 * @param minSpanX The minimum horizontal span required
1094 * @param minSpanY The minimum vertical span required
1095 * @param spanX Horizontal span of the object.
1096 * @param spanY Vertical span of the object.
1097 * @param result Array in which to place the result, or null (in which case a new array will
1098 * be allocated)
1099 * @return The X, Y cell of a vacant area that can contain this object,
1100 * nearest the requested location.
1101 */
1102 int[] findNearestVacantArea(int pixelX, int pixelY, int minSpanX, int minSpanY, int spanX,
1103 int spanY, int[] result, int[] resultSpan) {
Sunny Goyalf7a29e82015-04-24 15:20:43 -07001104 return findNearestArea(pixelX, pixelY, minSpanX, minSpanY, spanX, spanY, true,
Adam Cohend41fbf52012-02-16 23:53:59 -08001105 result, resultSpan);
1106 }
1107
Adam Cohend41fbf52012-02-16 23:53:59 -08001108 private final Stack<Rect> mTempRectStack = new Stack<Rect>();
1109 private void lazyInitTempRectStack() {
1110 if (mTempRectStack.isEmpty()) {
1111 for (int i = 0; i < mCountX * mCountY; i++) {
1112 mTempRectStack.push(new Rect());
1113 }
1114 }
1115 }
Adam Cohen482ed822012-03-02 14:15:13 -08001116
Adam Cohend41fbf52012-02-16 23:53:59 -08001117 private void recycleTempRects(Stack<Rect> used) {
1118 while (!used.isEmpty()) {
1119 mTempRectStack.push(used.pop());
1120 }
1121 }
1122
1123 /**
1124 * Find a vacant area that will fit the given bounds nearest the requested
1125 * cell location. Uses Euclidean distance to score multiple vacant areas.
1126 *
1127 * @param pixelX The X location at which you want to search for a vacant area.
1128 * @param pixelY The Y location at which you want to search for a vacant area.
1129 * @param minSpanX The minimum horizontal span required
1130 * @param minSpanY The minimum vertical span required
1131 * @param spanX Horizontal span of the object.
1132 * @param spanY Vertical span of the object.
1133 * @param ignoreOccupied If true, the result can be an occupied cell
1134 * @param result Array in which to place the result, or null (in which case a new array will
1135 * be allocated)
1136 * @return The X, Y cell of a vacant area that can contain this object,
1137 * nearest the requested location.
1138 */
Sunny Goyalf7a29e82015-04-24 15:20:43 -07001139 private int[] findNearestArea(int pixelX, int pixelY, int minSpanX, int minSpanY, int spanX,
1140 int spanY, boolean ignoreOccupied, int[] result, int[] resultSpan) {
Adam Cohend41fbf52012-02-16 23:53:59 -08001141 lazyInitTempRectStack();
Michael Jurkac6ee42e2010-09-30 12:04:50 -07001142
Adam Cohene3e27a82011-04-15 12:07:39 -07001143 // For items with a spanX / spanY > 1, the passed in point (pixelX, pixelY) corresponds
1144 // to the center of the item, but we are searching based on the top-left cell, so
1145 // we translate the point over to correspond to the top-left.
Sunny Goyalaa8a8712016-11-20 15:26:01 +05301146 pixelX -= mCellWidth * (spanX - 1) / 2f;
1147 pixelY -= mCellHeight * (spanY - 1) / 2f;
Adam Cohene3e27a82011-04-15 12:07:39 -07001148
Jeff Sharkey70864282009-04-07 21:08:40 -07001149 // Keep track of best-scoring drop area
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001150 final int[] bestXY = result != null ? result : new int[2];
Jeff Sharkey70864282009-04-07 21:08:40 -07001151 double bestDistance = Double.MAX_VALUE;
Adam Cohend41fbf52012-02-16 23:53:59 -08001152 final Rect bestRect = new Rect(-1, -1, -1, -1);
1153 final Stack<Rect> validRegions = new Stack<Rect>();
Winson Chungaafa03c2010-06-11 17:34:16 -07001154
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001155 final int countX = mCountX;
1156 final int countY = mCountY;
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001157
Adam Cohend41fbf52012-02-16 23:53:59 -08001158 if (minSpanX <= 0 || minSpanY <= 0 || spanX <= 0 || spanY <= 0 ||
1159 spanX < minSpanX || spanY < minSpanY) {
1160 return bestXY;
1161 }
1162
1163 for (int y = 0; y < countY - (minSpanY - 1); y++) {
Michael Jurkac28de512010-08-13 11:27:44 -07001164 inner:
Adam Cohend41fbf52012-02-16 23:53:59 -08001165 for (int x = 0; x < countX - (minSpanX - 1); x++) {
1166 int ySize = -1;
1167 int xSize = -1;
Adam Cohendf035382011-04-11 17:22:04 -07001168 if (ignoreOccupied) {
Adam Cohend41fbf52012-02-16 23:53:59 -08001169 // First, let's see if this thing fits anywhere
1170 for (int i = 0; i < minSpanX; i++) {
1171 for (int j = 0; j < minSpanY; j++) {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001172 if (mOccupied.cells[x + i][y + j]) {
Adam Cohendf035382011-04-11 17:22:04 -07001173 continue inner;
1174 }
Michael Jurkac28de512010-08-13 11:27:44 -07001175 }
1176 }
Adam Cohend41fbf52012-02-16 23:53:59 -08001177 xSize = minSpanX;
1178 ySize = minSpanY;
1179
1180 // We know that the item will fit at _some_ acceptable size, now let's see
1181 // how big we can make it. We'll alternate between incrementing x and y spans
1182 // until we hit a limit.
1183 boolean incX = true;
1184 boolean hitMaxX = xSize >= spanX;
1185 boolean hitMaxY = ySize >= spanY;
1186 while (!(hitMaxX && hitMaxY)) {
1187 if (incX && !hitMaxX) {
1188 for (int j = 0; j < ySize; j++) {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001189 if (x + xSize > countX -1 || mOccupied.cells[x + xSize][y + j]) {
Adam Cohend41fbf52012-02-16 23:53:59 -08001190 // We can't move out horizontally
1191 hitMaxX = true;
1192 }
1193 }
1194 if (!hitMaxX) {
1195 xSize++;
1196 }
1197 } else if (!hitMaxY) {
1198 for (int i = 0; i < xSize; i++) {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001199 if (y + ySize > countY - 1 || mOccupied.cells[x + i][y + ySize]) {
Adam Cohend41fbf52012-02-16 23:53:59 -08001200 // We can't move out vertically
1201 hitMaxY = true;
1202 }
1203 }
1204 if (!hitMaxY) {
1205 ySize++;
1206 }
1207 }
1208 hitMaxX |= xSize >= spanX;
1209 hitMaxY |= ySize >= spanY;
1210 incX = !incX;
1211 }
1212 incX = true;
1213 hitMaxX = xSize >= spanX;
1214 hitMaxY = ySize >= spanY;
Michael Jurkac28de512010-08-13 11:27:44 -07001215 }
Sunny Goyal2805e632015-05-20 15:35:32 -07001216 final int[] cellXY = mTmpPoint;
Adam Cohene3e27a82011-04-15 12:07:39 -07001217 cellToCenterPoint(x, y, cellXY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001218
Adam Cohend41fbf52012-02-16 23:53:59 -08001219 // We verify that the current rect is not a sub-rect of any of our previous
1220 // candidates. In this case, the current rect is disqualified in favour of the
1221 // containing rect.
1222 Rect currentRect = mTempRectStack.pop();
1223 currentRect.set(x, y, x + xSize, y + ySize);
1224 boolean contained = false;
1225 for (Rect r : validRegions) {
1226 if (r.contains(currentRect)) {
1227 contained = true;
1228 break;
1229 }
1230 }
1231 validRegions.push(currentRect);
Sunny Goyalf7a29e82015-04-24 15:20:43 -07001232 double distance = Math.hypot(cellXY[0] - pixelX, cellXY[1] - pixelY);
Adam Cohen482ed822012-03-02 14:15:13 -08001233
Adam Cohend41fbf52012-02-16 23:53:59 -08001234 if ((distance <= bestDistance && !contained) ||
1235 currentRect.contains(bestRect)) {
Michael Jurkac28de512010-08-13 11:27:44 -07001236 bestDistance = distance;
1237 bestXY[0] = x;
1238 bestXY[1] = y;
Adam Cohend41fbf52012-02-16 23:53:59 -08001239 if (resultSpan != null) {
1240 resultSpan[0] = xSize;
1241 resultSpan[1] = ySize;
1242 }
1243 bestRect.set(currentRect);
Michael Jurkac28de512010-08-13 11:27:44 -07001244 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001245 }
1246 }
1247
Adam Cohenc0dcf592011-06-01 15:30:43 -07001248 // Return -1, -1 if no suitable location found
1249 if (bestDistance == Double.MAX_VALUE) {
1250 bestXY[0] = -1;
1251 bestXY[1] = -1;
Jeff Sharkey70864282009-04-07 21:08:40 -07001252 }
Adam Cohend41fbf52012-02-16 23:53:59 -08001253 recycleTempRects(validRegions);
Adam Cohenc0dcf592011-06-01 15:30:43 -07001254 return bestXY;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001255 }
Winson Chungaafa03c2010-06-11 17:34:16 -07001256
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001257 /**
Adam Cohen482ed822012-03-02 14:15:13 -08001258 * Find a vacant area that will fit the given bounds nearest the requested
1259 * cell location, and will also weigh in a suggested direction vector of the
1260 * desired location. This method computers distance based on unit grid distances,
1261 * not pixel distances.
1262 *
Adam Cohen47a876d2012-03-19 13:21:41 -07001263 * @param cellX The X cell nearest to which you want to search for a vacant area.
1264 * @param cellY The Y cell nearest which you want to search for a vacant area.
Adam Cohen482ed822012-03-02 14:15:13 -08001265 * @param spanX Horizontal span of the object.
1266 * @param spanY Vertical span of the object.
Adam Cohen47a876d2012-03-19 13:21:41 -07001267 * @param direction The favored direction in which the views should move from x, y
Sunny Goyal9eba1fd2015-10-16 08:58:57 -07001268 * @param occupied The array which represents which cells in the CellLayout are occupied
Adam Cohen47a876d2012-03-19 13:21:41 -07001269 * @param blockOccupied The array which represents which cells in the specified block (cellX,
Winson Chung5f8afe62013-08-12 16:19:28 -07001270 * cellY, spanX, spanY) are occupied. This is used when try to move a group of views.
Adam Cohen482ed822012-03-02 14:15:13 -08001271 * @param result Array in which to place the result, or null (in which case a new array will
1272 * be allocated)
1273 * @return The X, Y cell of a vacant area that can contain this object,
1274 * nearest the requested location.
1275 */
1276 private int[] findNearestArea(int cellX, int cellY, int spanX, int spanY, int[] direction,
Adam Cohen47a876d2012-03-19 13:21:41 -07001277 boolean[][] occupied, boolean blockOccupied[][], int[] result) {
Adam Cohen482ed822012-03-02 14:15:13 -08001278 // Keep track of best-scoring drop area
1279 final int[] bestXY = result != null ? result : new int[2];
1280 float bestDistance = Float.MAX_VALUE;
1281 int bestDirectionScore = Integer.MIN_VALUE;
1282
1283 final int countX = mCountX;
1284 final int countY = mCountY;
1285
1286 for (int y = 0; y < countY - (spanY - 1); y++) {
1287 inner:
1288 for (int x = 0; x < countX - (spanX - 1); x++) {
1289 // First, let's see if this thing fits anywhere
1290 for (int i = 0; i < spanX; i++) {
1291 for (int j = 0; j < spanY; j++) {
Adam Cohen47a876d2012-03-19 13:21:41 -07001292 if (occupied[x + i][y + j] && (blockOccupied == null || blockOccupied[i][j])) {
Adam Cohen482ed822012-03-02 14:15:13 -08001293 continue inner;
1294 }
1295 }
1296 }
1297
Sunny Goyalf7a29e82015-04-24 15:20:43 -07001298 float distance = (float) Math.hypot(x - cellX, y - cellY);
Adam Cohen482ed822012-03-02 14:15:13 -08001299 int[] curDirection = mTmpPoint;
Adam Cohen47a876d2012-03-19 13:21:41 -07001300 computeDirectionVector(x - cellX, y - cellY, curDirection);
1301 // The direction score is just the dot product of the two candidate direction
1302 // and that passed in.
Adam Cohen482ed822012-03-02 14:15:13 -08001303 int curDirectionScore = direction[0] * curDirection[0] +
1304 direction[1] * curDirection[1];
Sunny Goyal8f90dcf2016-08-18 15:01:11 -07001305 if (Float.compare(distance, bestDistance) < 0 ||
1306 (Float.compare(distance, bestDistance) == 0
1307 && curDirectionScore > bestDirectionScore)) {
Adam Cohen482ed822012-03-02 14:15:13 -08001308 bestDistance = distance;
1309 bestDirectionScore = curDirectionScore;
1310 bestXY[0] = x;
1311 bestXY[1] = y;
1312 }
1313 }
1314 }
1315
1316 // Return -1, -1 if no suitable location found
1317 if (bestDistance == Float.MAX_VALUE) {
1318 bestXY[0] = -1;
1319 bestXY[1] = -1;
1320 }
1321 return bestXY;
1322 }
1323
1324 private boolean addViewToTempLocation(View v, Rect rectOccupiedByPotentialDrop,
Adam Cohen8baab352012-03-20 17:39:21 -07001325 int[] direction, ItemConfiguration currentState) {
1326 CellAndSpan c = currentState.map.get(v);
Adam Cohen482ed822012-03-02 14:15:13 -08001327 boolean success = false;
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001328 mTmpOccupied.markCells(c, false);
1329 mTmpOccupied.markCells(rectOccupiedByPotentialDrop, true);
Adam Cohen482ed822012-03-02 14:15:13 -08001330
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001331 findNearestArea(c.cellX, c.cellY, c.spanX, c.spanY, direction,
1332 mTmpOccupied.cells, null, mTempLocation);
Adam Cohen482ed822012-03-02 14:15:13 -08001333
1334 if (mTempLocation[0] >= 0 && mTempLocation[1] >= 0) {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001335 c.cellX = mTempLocation[0];
1336 c.cellY = mTempLocation[1];
Adam Cohen482ed822012-03-02 14:15:13 -08001337 success = true;
Adam Cohen482ed822012-03-02 14:15:13 -08001338 }
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001339 mTmpOccupied.markCells(c, true);
Adam Cohen482ed822012-03-02 14:15:13 -08001340 return success;
1341 }
1342
Adam Cohenf3900c22012-11-16 18:28:11 -08001343 /**
1344 * This helper class defines a cluster of views. It helps with defining complex edges
1345 * of the cluster and determining how those edges interact with other views. The edges
1346 * essentially define a fine-grained boundary around the cluster of views -- like a more
1347 * precise version of a bounding box.
1348 */
1349 private class ViewCluster {
Sunny Goyal6dc98b92016-04-03 15:25:29 -07001350 final static int LEFT = 1 << 0;
1351 final static int TOP = 1 << 1;
1352 final static int RIGHT = 1 << 2;
1353 final static int BOTTOM = 1 << 3;
Adam Cohen47a876d2012-03-19 13:21:41 -07001354
Adam Cohenf3900c22012-11-16 18:28:11 -08001355 ArrayList<View> views;
1356 ItemConfiguration config;
1357 Rect boundingRect = new Rect();
Adam Cohen47a876d2012-03-19 13:21:41 -07001358
Adam Cohenf3900c22012-11-16 18:28:11 -08001359 int[] leftEdge = new int[mCountY];
1360 int[] rightEdge = new int[mCountY];
1361 int[] topEdge = new int[mCountX];
1362 int[] bottomEdge = new int[mCountX];
Sunny Goyal6dc98b92016-04-03 15:25:29 -07001363 int dirtyEdges;
1364 boolean boundingRectDirty;
Adam Cohenf3900c22012-11-16 18:28:11 -08001365
1366 @SuppressWarnings("unchecked")
1367 public ViewCluster(ArrayList<View> views, ItemConfiguration config) {
1368 this.views = (ArrayList<View>) views.clone();
1369 this.config = config;
1370 resetEdges();
Adam Cohen47a876d2012-03-19 13:21:41 -07001371 }
1372
Adam Cohenf3900c22012-11-16 18:28:11 -08001373 void resetEdges() {
1374 for (int i = 0; i < mCountX; i++) {
1375 topEdge[i] = -1;
1376 bottomEdge[i] = -1;
1377 }
1378 for (int i = 0; i < mCountY; i++) {
1379 leftEdge[i] = -1;
1380 rightEdge[i] = -1;
1381 }
Sunny Goyal6dc98b92016-04-03 15:25:29 -07001382 dirtyEdges = LEFT | TOP | RIGHT | BOTTOM;
Adam Cohenf3900c22012-11-16 18:28:11 -08001383 boundingRectDirty = true;
1384 }
Adam Cohen47a876d2012-03-19 13:21:41 -07001385
Sunny Goyal6dc98b92016-04-03 15:25:29 -07001386 void computeEdge(int which) {
Adam Cohenf3900c22012-11-16 18:28:11 -08001387 int count = views.size();
1388 for (int i = 0; i < count; i++) {
1389 CellAndSpan cs = config.map.get(views.get(i));
1390 switch (which) {
1391 case LEFT:
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001392 int left = cs.cellX;
1393 for (int j = cs.cellY; j < cs.cellY + cs.spanY; j++) {
Sunny Goyal6dc98b92016-04-03 15:25:29 -07001394 if (left < leftEdge[j] || leftEdge[j] < 0) {
1395 leftEdge[j] = left;
Adam Cohena56dc102012-07-13 13:41:42 -07001396 }
Adam Cohen47a876d2012-03-19 13:21:41 -07001397 }
Adam Cohenf3900c22012-11-16 18:28:11 -08001398 break;
1399 case RIGHT:
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001400 int right = cs.cellX + cs.spanX;
1401 for (int j = cs.cellY; j < cs.cellY + cs.spanY; j++) {
Sunny Goyal6dc98b92016-04-03 15:25:29 -07001402 if (right > rightEdge[j]) {
1403 rightEdge[j] = right;
Adam Cohenf3900c22012-11-16 18:28:11 -08001404 }
1405 }
1406 break;
1407 case TOP:
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001408 int top = cs.cellY;
1409 for (int j = cs.cellX; j < cs.cellX + cs.spanX; j++) {
Sunny Goyal6dc98b92016-04-03 15:25:29 -07001410 if (top < topEdge[j] || topEdge[j] < 0) {
1411 topEdge[j] = top;
Adam Cohenf3900c22012-11-16 18:28:11 -08001412 }
1413 }
1414 break;
1415 case BOTTOM:
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001416 int bottom = cs.cellY + cs.spanY;
1417 for (int j = cs.cellX; j < cs.cellX + cs.spanX; j++) {
Sunny Goyal6dc98b92016-04-03 15:25:29 -07001418 if (bottom > bottomEdge[j]) {
1419 bottomEdge[j] = bottom;
Adam Cohenf3900c22012-11-16 18:28:11 -08001420 }
1421 }
1422 break;
Adam Cohen47a876d2012-03-19 13:21:41 -07001423 }
1424 }
1425 }
Adam Cohenf3900c22012-11-16 18:28:11 -08001426
1427 boolean isViewTouchingEdge(View v, int whichEdge) {
1428 CellAndSpan cs = config.map.get(v);
1429
Sunny Goyal6dc98b92016-04-03 15:25:29 -07001430 if ((dirtyEdges & whichEdge) == whichEdge) {
1431 computeEdge(whichEdge);
1432 dirtyEdges &= ~whichEdge;
1433 }
Adam Cohenf3900c22012-11-16 18:28:11 -08001434
1435 switch (whichEdge) {
1436 case LEFT:
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001437 for (int i = cs.cellY; i < cs.cellY + cs.spanY; i++) {
1438 if (leftEdge[i] == cs.cellX + cs.spanX) {
Adam Cohenf3900c22012-11-16 18:28:11 -08001439 return true;
1440 }
1441 }
1442 break;
1443 case RIGHT:
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001444 for (int i = cs.cellY; i < cs.cellY + cs.spanY; i++) {
1445 if (rightEdge[i] == cs.cellX) {
Adam Cohenf3900c22012-11-16 18:28:11 -08001446 return true;
1447 }
1448 }
1449 break;
1450 case TOP:
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001451 for (int i = cs.cellX; i < cs.cellX + cs.spanX; i++) {
1452 if (topEdge[i] == cs.cellY + cs.spanY) {
Adam Cohenf3900c22012-11-16 18:28:11 -08001453 return true;
1454 }
1455 }
1456 break;
1457 case BOTTOM:
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001458 for (int i = cs.cellX; i < cs.cellX + cs.spanX; i++) {
1459 if (bottomEdge[i] == cs.cellY) {
Adam Cohenf3900c22012-11-16 18:28:11 -08001460 return true;
1461 }
1462 }
1463 break;
1464 }
1465 return false;
1466 }
1467
1468 void shift(int whichEdge, int delta) {
1469 for (View v: views) {
1470 CellAndSpan c = config.map.get(v);
1471 switch (whichEdge) {
1472 case LEFT:
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001473 c.cellX -= delta;
Adam Cohenf3900c22012-11-16 18:28:11 -08001474 break;
1475 case RIGHT:
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001476 c.cellX += delta;
Adam Cohenf3900c22012-11-16 18:28:11 -08001477 break;
1478 case TOP:
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001479 c.cellY -= delta;
Adam Cohenf3900c22012-11-16 18:28:11 -08001480 break;
1481 case BOTTOM:
1482 default:
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001483 c.cellY += delta;
Adam Cohenf3900c22012-11-16 18:28:11 -08001484 break;
1485 }
1486 }
1487 resetEdges();
1488 }
1489
1490 public void addView(View v) {
1491 views.add(v);
1492 resetEdges();
1493 }
1494
1495 public Rect getBoundingRect() {
1496 if (boundingRectDirty) {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001497 config.getBoundingRectForViews(views, boundingRect);
Adam Cohenf3900c22012-11-16 18:28:11 -08001498 }
1499 return boundingRect;
1500 }
1501
Adam Cohenf3900c22012-11-16 18:28:11 -08001502 PositionComparator comparator = new PositionComparator();
1503 class PositionComparator implements Comparator<View> {
1504 int whichEdge = 0;
1505 public int compare(View left, View right) {
1506 CellAndSpan l = config.map.get(left);
1507 CellAndSpan r = config.map.get(right);
1508 switch (whichEdge) {
1509 case LEFT:
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001510 return (r.cellX + r.spanX) - (l.cellX + l.spanX);
Adam Cohenf3900c22012-11-16 18:28:11 -08001511 case RIGHT:
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001512 return l.cellX - r.cellX;
Adam Cohenf3900c22012-11-16 18:28:11 -08001513 case TOP:
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001514 return (r.cellY + r.spanY) - (l.cellY + l.spanY);
Adam Cohenf3900c22012-11-16 18:28:11 -08001515 case BOTTOM:
1516 default:
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001517 return l.cellY - r.cellY;
Adam Cohenf3900c22012-11-16 18:28:11 -08001518 }
1519 }
1520 }
1521
1522 public void sortConfigurationForEdgePush(int edge) {
1523 comparator.whichEdge = edge;
1524 Collections.sort(config.sortedViews, comparator);
1525 }
Adam Cohen47a876d2012-03-19 13:21:41 -07001526 }
1527
Adam Cohenf3900c22012-11-16 18:28:11 -08001528 private boolean pushViewsToTempLocation(ArrayList<View> views, Rect rectOccupiedByPotentialDrop,
1529 int[] direction, View dragView, ItemConfiguration currentState) {
Adam Cohene0489502012-08-27 15:18:53 -07001530
Adam Cohenf3900c22012-11-16 18:28:11 -08001531 ViewCluster cluster = new ViewCluster(views, currentState);
1532 Rect clusterRect = cluster.getBoundingRect();
1533 int whichEdge;
1534 int pushDistance;
1535 boolean fail = false;
1536
1537 // Determine the edge of the cluster that will be leading the push and how far
1538 // the cluster must be shifted.
1539 if (direction[0] < 0) {
1540 whichEdge = ViewCluster.LEFT;
1541 pushDistance = clusterRect.right - rectOccupiedByPotentialDrop.left;
Adam Cohene0489502012-08-27 15:18:53 -07001542 } else if (direction[0] > 0) {
Adam Cohenf3900c22012-11-16 18:28:11 -08001543 whichEdge = ViewCluster.RIGHT;
1544 pushDistance = rectOccupiedByPotentialDrop.right - clusterRect.left;
1545 } else if (direction[1] < 0) {
1546 whichEdge = ViewCluster.TOP;
1547 pushDistance = clusterRect.bottom - rectOccupiedByPotentialDrop.top;
1548 } else {
1549 whichEdge = ViewCluster.BOTTOM;
1550 pushDistance = rectOccupiedByPotentialDrop.bottom - clusterRect.top;
Adam Cohene0489502012-08-27 15:18:53 -07001551 }
1552
Adam Cohenf3900c22012-11-16 18:28:11 -08001553 // Break early for invalid push distance.
1554 if (pushDistance <= 0) {
1555 return false;
Adam Cohene0489502012-08-27 15:18:53 -07001556 }
Adam Cohenf3900c22012-11-16 18:28:11 -08001557
1558 // Mark the occupied state as false for the group of views we want to move.
1559 for (View v: views) {
1560 CellAndSpan c = currentState.map.get(v);
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001561 mTmpOccupied.markCells(c, false);
Adam Cohenf3900c22012-11-16 18:28:11 -08001562 }
1563
1564 // We save the current configuration -- if we fail to find a solution we will revert
1565 // to the initial state. The process of finding a solution modifies the configuration
1566 // in place, hence the need for revert in the failure case.
1567 currentState.save();
1568
1569 // The pushing algorithm is simplified by considering the views in the order in which
1570 // they would be pushed by the cluster. For example, if the cluster is leading with its
1571 // left edge, we consider sort the views by their right edge, from right to left.
1572 cluster.sortConfigurationForEdgePush(whichEdge);
1573
1574 while (pushDistance > 0 && !fail) {
1575 for (View v: currentState.sortedViews) {
1576 // For each view that isn't in the cluster, we see if the leading edge of the
1577 // cluster is contacting the edge of that view. If so, we add that view to the
1578 // cluster.
1579 if (!cluster.views.contains(v) && v != dragView) {
1580 if (cluster.isViewTouchingEdge(v, whichEdge)) {
1581 LayoutParams lp = (LayoutParams) v.getLayoutParams();
1582 if (!lp.canReorder) {
1583 // The push solution includes the all apps button, this is not viable.
1584 fail = true;
1585 break;
1586 }
1587 cluster.addView(v);
1588 CellAndSpan c = currentState.map.get(v);
1589
1590 // Adding view to cluster, mark it as not occupied.
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001591 mTmpOccupied.markCells(c, false);
Adam Cohenf3900c22012-11-16 18:28:11 -08001592 }
1593 }
1594 }
1595 pushDistance--;
1596
1597 // The cluster has been completed, now we move the whole thing over in the appropriate
1598 // direction.
1599 cluster.shift(whichEdge, 1);
1600 }
1601
1602 boolean foundSolution = false;
1603 clusterRect = cluster.getBoundingRect();
1604
1605 // Due to the nature of the algorithm, the only check required to verify a valid solution
1606 // is to ensure that completed shifted cluster lies completely within the cell layout.
1607 if (!fail && clusterRect.left >= 0 && clusterRect.right <= mCountX && clusterRect.top >= 0 &&
1608 clusterRect.bottom <= mCountY) {
1609 foundSolution = true;
1610 } else {
1611 currentState.restore();
1612 }
1613
1614 // In either case, we set the occupied array as marked for the location of the views
1615 for (View v: cluster.views) {
1616 CellAndSpan c = currentState.map.get(v);
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001617 mTmpOccupied.markCells(c, true);
Adam Cohenf3900c22012-11-16 18:28:11 -08001618 }
1619
1620 return foundSolution;
Adam Cohene0489502012-08-27 15:18:53 -07001621 }
1622
Adam Cohen482ed822012-03-02 14:15:13 -08001623 private boolean addViewsToTempLocation(ArrayList<View> views, Rect rectOccupiedByPotentialDrop,
Adam Cohenf3900c22012-11-16 18:28:11 -08001624 int[] direction, View dragView, ItemConfiguration currentState) {
Adam Cohen482ed822012-03-02 14:15:13 -08001625 if (views.size() == 0) return true;
Adam Cohen482ed822012-03-02 14:15:13 -08001626
Adam Cohen8baab352012-03-20 17:39:21 -07001627 boolean success = false;
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001628 Rect boundingRect = new Rect();
Adam Cohen8baab352012-03-20 17:39:21 -07001629 // We construct a rect which represents the entire group of views passed in
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001630 currentState.getBoundingRectForViews(views, boundingRect);
Adam Cohen8baab352012-03-20 17:39:21 -07001631
Adam Cohen8baab352012-03-20 17:39:21 -07001632 // Mark the occupied state as false for the group of views we want to move.
Adam Cohenf3900c22012-11-16 18:28:11 -08001633 for (View v: views) {
Adam Cohen8baab352012-03-20 17:39:21 -07001634 CellAndSpan c = currentState.map.get(v);
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001635 mTmpOccupied.markCells(c, false);
Adam Cohen8baab352012-03-20 17:39:21 -07001636 }
1637
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001638 GridOccupancy blockOccupied = new GridOccupancy(boundingRect.width(), boundingRect.height());
Adam Cohen47a876d2012-03-19 13:21:41 -07001639 int top = boundingRect.top;
1640 int left = boundingRect.left;
Adam Cohen8baab352012-03-20 17:39:21 -07001641 // We mark more precisely which parts of the bounding rect are truly occupied, allowing
Adam Cohena56dc102012-07-13 13:41:42 -07001642 // for interlocking.
Adam Cohenf3900c22012-11-16 18:28:11 -08001643 for (View v: views) {
Adam Cohen8baab352012-03-20 17:39:21 -07001644 CellAndSpan c = currentState.map.get(v);
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001645 blockOccupied.markCells(c.cellX - left, c.cellY - top, c.spanX, c.spanY, true);
Adam Cohen47a876d2012-03-19 13:21:41 -07001646 }
1647
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001648 mTmpOccupied.markCells(rectOccupiedByPotentialDrop, true);
Adam Cohen482ed822012-03-02 14:15:13 -08001649
Adam Cohenf3900c22012-11-16 18:28:11 -08001650 findNearestArea(boundingRect.left, boundingRect.top, boundingRect.width(),
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001651 boundingRect.height(), direction,
1652 mTmpOccupied.cells, blockOccupied.cells, mTempLocation);
Adam Cohen482ed822012-03-02 14:15:13 -08001653
Adam Cohen8baab352012-03-20 17:39:21 -07001654 // If we successfuly found a location by pushing the block of views, we commit it
Adam Cohen482ed822012-03-02 14:15:13 -08001655 if (mTempLocation[0] >= 0 && mTempLocation[1] >= 0) {
Adam Cohen8baab352012-03-20 17:39:21 -07001656 int deltaX = mTempLocation[0] - boundingRect.left;
1657 int deltaY = mTempLocation[1] - boundingRect.top;
Adam Cohenf3900c22012-11-16 18:28:11 -08001658 for (View v: views) {
Adam Cohen8baab352012-03-20 17:39:21 -07001659 CellAndSpan c = currentState.map.get(v);
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001660 c.cellX += deltaX;
1661 c.cellY += deltaY;
Adam Cohen482ed822012-03-02 14:15:13 -08001662 }
1663 success = true;
1664 }
Adam Cohen8baab352012-03-20 17:39:21 -07001665
1666 // In either case, we set the occupied array as marked for the location of the views
Adam Cohenf3900c22012-11-16 18:28:11 -08001667 for (View v: views) {
Adam Cohen8baab352012-03-20 17:39:21 -07001668 CellAndSpan c = currentState.map.get(v);
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001669 mTmpOccupied.markCells(c, true);
Adam Cohen482ed822012-03-02 14:15:13 -08001670 }
1671 return success;
1672 }
1673
Adam Cohen4abc5bd2012-05-29 21:06:03 -07001674 // This method tries to find a reordering solution which satisfies the push mechanic by trying
1675 // to push items in each of the cardinal directions, in an order based on the direction vector
1676 // passed.
1677 private boolean attemptPushInDirection(ArrayList<View> intersectingViews, Rect occupied,
1678 int[] direction, View ignoreView, ItemConfiguration solution) {
1679 if ((Math.abs(direction[0]) + Math.abs(direction[1])) > 1) {
Winson Chung5f8afe62013-08-12 16:19:28 -07001680 // If the direction vector has two non-zero components, we try pushing
Adam Cohen4abc5bd2012-05-29 21:06:03 -07001681 // separately in each of the components.
1682 int temp = direction[1];
1683 direction[1] = 0;
Adam Cohenf3900c22012-11-16 18:28:11 -08001684
1685 if (pushViewsToTempLocation(intersectingViews, occupied, direction,
Adam Cohen4abc5bd2012-05-29 21:06:03 -07001686 ignoreView, solution)) {
1687 return true;
1688 }
1689 direction[1] = temp;
1690 temp = direction[0];
1691 direction[0] = 0;
Adam Cohenf3900c22012-11-16 18:28:11 -08001692
1693 if (pushViewsToTempLocation(intersectingViews, occupied, direction,
Adam Cohen4abc5bd2012-05-29 21:06:03 -07001694 ignoreView, solution)) {
1695 return true;
1696 }
1697 // Revert the direction
1698 direction[0] = temp;
1699
1700 // Now we try pushing in each component of the opposite direction
1701 direction[0] *= -1;
1702 direction[1] *= -1;
1703 temp = direction[1];
1704 direction[1] = 0;
Adam Cohenf3900c22012-11-16 18:28:11 -08001705 if (pushViewsToTempLocation(intersectingViews, occupied, direction,
Adam Cohen4abc5bd2012-05-29 21:06:03 -07001706 ignoreView, solution)) {
1707 return true;
1708 }
1709
1710 direction[1] = temp;
1711 temp = direction[0];
1712 direction[0] = 0;
Adam Cohenf3900c22012-11-16 18:28:11 -08001713 if (pushViewsToTempLocation(intersectingViews, occupied, direction,
Adam Cohen4abc5bd2012-05-29 21:06:03 -07001714 ignoreView, solution)) {
1715 return true;
1716 }
1717 // revert the direction
1718 direction[0] = temp;
1719 direction[0] *= -1;
1720 direction[1] *= -1;
Winson Chung5f8afe62013-08-12 16:19:28 -07001721
Adam Cohen4abc5bd2012-05-29 21:06:03 -07001722 } else {
1723 // If the direction vector has a single non-zero component, we push first in the
1724 // direction of the vector
Adam Cohenf3900c22012-11-16 18:28:11 -08001725 if (pushViewsToTempLocation(intersectingViews, occupied, direction,
Adam Cohen4abc5bd2012-05-29 21:06:03 -07001726 ignoreView, solution)) {
1727 return true;
1728 }
Adam Cohen4abc5bd2012-05-29 21:06:03 -07001729 // Then we try the opposite direction
1730 direction[0] *= -1;
1731 direction[1] *= -1;
Adam Cohenf3900c22012-11-16 18:28:11 -08001732 if (pushViewsToTempLocation(intersectingViews, occupied, direction,
Adam Cohen4abc5bd2012-05-29 21:06:03 -07001733 ignoreView, solution)) {
1734 return true;
1735 }
1736 // Switch the direction back
1737 direction[0] *= -1;
1738 direction[1] *= -1;
Winson Chung5f8afe62013-08-12 16:19:28 -07001739
1740 // If we have failed to find a push solution with the above, then we try
Adam Cohen4abc5bd2012-05-29 21:06:03 -07001741 // to find a solution by pushing along the perpendicular axis.
1742
1743 // Swap the components
1744 int temp = direction[1];
1745 direction[1] = direction[0];
1746 direction[0] = temp;
Adam Cohenf3900c22012-11-16 18:28:11 -08001747 if (pushViewsToTempLocation(intersectingViews, occupied, direction,
Adam Cohen4abc5bd2012-05-29 21:06:03 -07001748 ignoreView, solution)) {
1749 return true;
1750 }
1751
1752 // Then we try the opposite direction
1753 direction[0] *= -1;
1754 direction[1] *= -1;
Adam Cohenf3900c22012-11-16 18:28:11 -08001755 if (pushViewsToTempLocation(intersectingViews, occupied, direction,
Adam Cohen4abc5bd2012-05-29 21:06:03 -07001756 ignoreView, solution)) {
1757 return true;
1758 }
1759 // Switch the direction back
1760 direction[0] *= -1;
1761 direction[1] *= -1;
1762
1763 // Swap the components back
1764 temp = direction[1];
1765 direction[1] = direction[0];
1766 direction[0] = temp;
1767 }
1768 return false;
1769 }
1770
Adam Cohen482ed822012-03-02 14:15:13 -08001771 private boolean rearrangementExists(int cellX, int cellY, int spanX, int spanY, int[] direction,
Adam Cohen8baab352012-03-20 17:39:21 -07001772 View ignoreView, ItemConfiguration solution) {
Winson Chunge3e03bc2012-05-01 15:10:11 -07001773 // Return early if get invalid cell positions
1774 if (cellX < 0 || cellY < 0) return false;
Adam Cohen482ed822012-03-02 14:15:13 -08001775
Adam Cohen8baab352012-03-20 17:39:21 -07001776 mIntersectingViews.clear();
Adam Cohen482ed822012-03-02 14:15:13 -08001777 mOccupiedRect.set(cellX, cellY, cellX + spanX, cellY + spanY);
Adam Cohen482ed822012-03-02 14:15:13 -08001778
Adam Cohen8baab352012-03-20 17:39:21 -07001779 // Mark the desired location of the view currently being dragged.
Adam Cohen482ed822012-03-02 14:15:13 -08001780 if (ignoreView != null) {
Adam Cohen8baab352012-03-20 17:39:21 -07001781 CellAndSpan c = solution.map.get(ignoreView);
Adam Cohen19f37922012-03-21 11:59:11 -07001782 if (c != null) {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001783 c.cellX = cellX;
1784 c.cellY = cellY;
Adam Cohen19f37922012-03-21 11:59:11 -07001785 }
Adam Cohen482ed822012-03-02 14:15:13 -08001786 }
Adam Cohen482ed822012-03-02 14:15:13 -08001787 Rect r0 = new Rect(cellX, cellY, cellX + spanX, cellY + spanY);
1788 Rect r1 = new Rect();
Adam Cohen8baab352012-03-20 17:39:21 -07001789 for (View child: solution.map.keySet()) {
Adam Cohen482ed822012-03-02 14:15:13 -08001790 if (child == ignoreView) continue;
Adam Cohen8baab352012-03-20 17:39:21 -07001791 CellAndSpan c = solution.map.get(child);
Adam Cohen482ed822012-03-02 14:15:13 -08001792 LayoutParams lp = (LayoutParams) child.getLayoutParams();
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001793 r1.set(c.cellX, c.cellY, c.cellX + c.spanX, c.cellY + c.spanY);
Adam Cohen482ed822012-03-02 14:15:13 -08001794 if (Rect.intersects(r0, r1)) {
1795 if (!lp.canReorder) {
1796 return false;
1797 }
1798 mIntersectingViews.add(child);
1799 }
1800 }
Adam Cohen47a876d2012-03-19 13:21:41 -07001801
Adam Cohenfa3c58f2013-12-06 16:10:55 -08001802 solution.intersectingViews = new ArrayList<View>(mIntersectingViews);
1803
Winson Chung5f8afe62013-08-12 16:19:28 -07001804 // First we try to find a solution which respects the push mechanic. That is,
Adam Cohen4abc5bd2012-05-29 21:06:03 -07001805 // we try to find a solution such that no displaced item travels through another item
1806 // without also displacing that item.
1807 if (attemptPushInDirection(mIntersectingViews, mOccupiedRect, direction, ignoreView,
Adam Cohen19f37922012-03-21 11:59:11 -07001808 solution)) {
Adam Cohen47a876d2012-03-19 13:21:41 -07001809 return true;
1810 }
Adam Cohen47a876d2012-03-19 13:21:41 -07001811
Adam Cohen4abc5bd2012-05-29 21:06:03 -07001812 // Next we try moving the views as a block, but without requiring the push mechanic.
Adam Cohenf3900c22012-11-16 18:28:11 -08001813 if (addViewsToTempLocation(mIntersectingViews, mOccupiedRect, direction, ignoreView,
Adam Cohen19f37922012-03-21 11:59:11 -07001814 solution)) {
Adam Cohen482ed822012-03-02 14:15:13 -08001815 return true;
1816 }
Adam Cohen47a876d2012-03-19 13:21:41 -07001817
Adam Cohen482ed822012-03-02 14:15:13 -08001818 // Ok, they couldn't move as a block, let's move them individually
1819 for (View v : mIntersectingViews) {
Adam Cohen8baab352012-03-20 17:39:21 -07001820 if (!addViewToTempLocation(v, mOccupiedRect, direction, solution)) {
Adam Cohen482ed822012-03-02 14:15:13 -08001821 return false;
1822 }
1823 }
1824 return true;
1825 }
1826
1827 /*
1828 * Returns a pair (x, y), where x,y are in {-1, 0, 1} corresponding to vector between
1829 * the provided point and the provided cell
1830 */
Adam Cohen47a876d2012-03-19 13:21:41 -07001831 private void computeDirectionVector(float deltaX, float deltaY, int[] result) {
Jon Mirandae96798e2016-12-07 12:10:44 -08001832 double angle = Math.atan(deltaY / deltaX);
Adam Cohen482ed822012-03-02 14:15:13 -08001833
1834 result[0] = 0;
1835 result[1] = 0;
1836 if (Math.abs(Math.cos(angle)) > 0.5f) {
1837 result[0] = (int) Math.signum(deltaX);
1838 }
1839 if (Math.abs(Math.sin(angle)) > 0.5f) {
1840 result[1] = (int) Math.signum(deltaY);
1841 }
1842 }
1843
Sunny Goyalf7a29e82015-04-24 15:20:43 -07001844 private ItemConfiguration findReorderSolution(int pixelX, int pixelY, int minSpanX, int minSpanY,
Adam Cohenfa3c58f2013-12-06 16:10:55 -08001845 int spanX, int spanY, int[] direction, View dragView, boolean decX,
1846 ItemConfiguration solution) {
Adam Cohen8baab352012-03-20 17:39:21 -07001847 // Copy the current state into the solution. This solution will be manipulated as necessary.
1848 copyCurrentStateToSolution(solution, false);
1849 // Copy the current occupied array into the temporary occupied array. This array will be
1850 // manipulated as necessary to find a solution.
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001851 mOccupied.copyTo(mTmpOccupied);
Adam Cohen482ed822012-03-02 14:15:13 -08001852
1853 // We find the nearest cell into which we would place the dragged item, assuming there's
1854 // nothing in its way.
1855 int result[] = new int[2];
1856 result = findNearestArea(pixelX, pixelY, spanX, spanY, result);
1857
1858 boolean success = false;
1859 // First we try the exact nearest position of the item being dragged,
1860 // we will then want to try to move this around to other neighbouring positions
Adam Cohen8baab352012-03-20 17:39:21 -07001861 success = rearrangementExists(result[0], result[1], spanX, spanY, direction, dragView,
1862 solution);
Adam Cohen482ed822012-03-02 14:15:13 -08001863
1864 if (!success) {
1865 // We try shrinking the widget down to size in an alternating pattern, shrink 1 in
1866 // x, then 1 in y etc.
1867 if (spanX > minSpanX && (minSpanY == spanY || decX)) {
Adam Cohenfa3c58f2013-12-06 16:10:55 -08001868 return findReorderSolution(pixelX, pixelY, minSpanX, minSpanY, spanX - 1, spanY,
1869 direction, dragView, false, solution);
Adam Cohen482ed822012-03-02 14:15:13 -08001870 } else if (spanY > minSpanY) {
Adam Cohenfa3c58f2013-12-06 16:10:55 -08001871 return findReorderSolution(pixelX, pixelY, minSpanX, minSpanY, spanX, spanY - 1,
1872 direction, dragView, true, solution);
Adam Cohen482ed822012-03-02 14:15:13 -08001873 }
1874 solution.isSolution = false;
1875 } else {
1876 solution.isSolution = true;
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001877 solution.cellX = result[0];
1878 solution.cellY = result[1];
1879 solution.spanX = spanX;
1880 solution.spanY = spanY;
Adam Cohen482ed822012-03-02 14:15:13 -08001881 }
1882 return solution;
1883 }
1884
1885 private void copyCurrentStateToSolution(ItemConfiguration solution, boolean temp) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07001886 int childCount = mShortcutsAndWidgets.getChildCount();
Adam Cohen482ed822012-03-02 14:15:13 -08001887 for (int i = 0; i < childCount; i++) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07001888 View child = mShortcutsAndWidgets.getChildAt(i);
Adam Cohen482ed822012-03-02 14:15:13 -08001889 LayoutParams lp = (LayoutParams) child.getLayoutParams();
Adam Cohen8baab352012-03-20 17:39:21 -07001890 CellAndSpan c;
Adam Cohen482ed822012-03-02 14:15:13 -08001891 if (temp) {
Adam Cohen8baab352012-03-20 17:39:21 -07001892 c = new CellAndSpan(lp.tmpCellX, lp.tmpCellY, lp.cellHSpan, lp.cellVSpan);
Adam Cohen482ed822012-03-02 14:15:13 -08001893 } else {
Adam Cohen8baab352012-03-20 17:39:21 -07001894 c = new CellAndSpan(lp.cellX, lp.cellY, lp.cellHSpan, lp.cellVSpan);
Adam Cohen482ed822012-03-02 14:15:13 -08001895 }
Adam Cohenf3900c22012-11-16 18:28:11 -08001896 solution.add(child, c);
Adam Cohen482ed822012-03-02 14:15:13 -08001897 }
1898 }
1899
1900 private void copySolutionToTempState(ItemConfiguration solution, View dragView) {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001901 mTmpOccupied.clear();
Adam Cohen482ed822012-03-02 14:15:13 -08001902
Michael Jurkaa52570f2012-03-20 03:18:20 -07001903 int childCount = mShortcutsAndWidgets.getChildCount();
Adam Cohen482ed822012-03-02 14:15:13 -08001904 for (int i = 0; i < childCount; i++) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07001905 View child = mShortcutsAndWidgets.getChildAt(i);
Adam Cohen482ed822012-03-02 14:15:13 -08001906 if (child == dragView) continue;
1907 LayoutParams lp = (LayoutParams) child.getLayoutParams();
Adam Cohen8baab352012-03-20 17:39:21 -07001908 CellAndSpan c = solution.map.get(child);
1909 if (c != null) {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001910 lp.tmpCellX = c.cellX;
1911 lp.tmpCellY = c.cellY;
Adam Cohen8baab352012-03-20 17:39:21 -07001912 lp.cellHSpan = c.spanX;
1913 lp.cellVSpan = c.spanY;
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001914 mTmpOccupied.markCells(c, true);
Adam Cohen482ed822012-03-02 14:15:13 -08001915 }
1916 }
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001917 mTmpOccupied.markCells(solution, true);
Adam Cohen482ed822012-03-02 14:15:13 -08001918 }
1919
1920 private void animateItemsToSolution(ItemConfiguration solution, View dragView, boolean
1921 commitDragView) {
1922
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001923 GridOccupancy occupied = DESTRUCTIVE_REORDER ? mOccupied : mTmpOccupied;
1924 occupied.clear();
Adam Cohen482ed822012-03-02 14:15:13 -08001925
Michael Jurkaa52570f2012-03-20 03:18:20 -07001926 int childCount = mShortcutsAndWidgets.getChildCount();
Adam Cohen482ed822012-03-02 14:15:13 -08001927 for (int i = 0; i < childCount; i++) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07001928 View child = mShortcutsAndWidgets.getChildAt(i);
Adam Cohen482ed822012-03-02 14:15:13 -08001929 if (child == dragView) continue;
Adam Cohen8baab352012-03-20 17:39:21 -07001930 CellAndSpan c = solution.map.get(child);
1931 if (c != null) {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001932 animateChildToPosition(child, c.cellX, c.cellY, REORDER_ANIMATION_DURATION, 0,
Adam Cohen19f37922012-03-21 11:59:11 -07001933 DESTRUCTIVE_REORDER, false);
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001934 occupied.markCells(c, true);
Adam Cohen482ed822012-03-02 14:15:13 -08001935 }
1936 }
1937 if (commitDragView) {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001938 occupied.markCells(solution, true);
Adam Cohen482ed822012-03-02 14:15:13 -08001939 }
1940 }
1941
Adam Cohenfa3c58f2013-12-06 16:10:55 -08001942
1943 // This method starts or changes the reorder preview animations
1944 private void beginOrAdjustReorderPreviewAnimations(ItemConfiguration solution,
1945 View dragView, int delay, int mode) {
Adam Cohen19f37922012-03-21 11:59:11 -07001946 int childCount = mShortcutsAndWidgets.getChildCount();
Adam Cohen19f37922012-03-21 11:59:11 -07001947 for (int i = 0; i < childCount; i++) {
1948 View child = mShortcutsAndWidgets.getChildAt(i);
1949 if (child == dragView) continue;
1950 CellAndSpan c = solution.map.get(child);
Adam Cohenfa3c58f2013-12-06 16:10:55 -08001951 boolean skip = mode == ReorderPreviewAnimation.MODE_HINT && solution.intersectingViews
1952 != null && !solution.intersectingViews.contains(child);
1953
Adam Cohen19f37922012-03-21 11:59:11 -07001954 LayoutParams lp = (LayoutParams) child.getLayoutParams();
Adam Cohenfa3c58f2013-12-06 16:10:55 -08001955 if (c != null && !skip) {
1956 ReorderPreviewAnimation rha = new ReorderPreviewAnimation(child, mode, lp.cellX,
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001957 lp.cellY, c.cellX, c.cellY, c.spanX, c.spanY);
Adam Cohend024f982012-05-23 18:26:45 -07001958 rha.animate();
Adam Cohen19f37922012-03-21 11:59:11 -07001959 }
1960 }
1961 }
1962
Adam Cohenfa3c58f2013-12-06 16:10:55 -08001963 // Class which represents the reorder preview animations. These animations show that an item is
Adam Cohen19f37922012-03-21 11:59:11 -07001964 // in a temporary state, and hint at where the item will return to.
Adam Cohenfa3c58f2013-12-06 16:10:55 -08001965 class ReorderPreviewAnimation {
Adam Cohen19f37922012-03-21 11:59:11 -07001966 View child;
Adam Cohend024f982012-05-23 18:26:45 -07001967 float finalDeltaX;
1968 float finalDeltaY;
1969 float initDeltaX;
1970 float initDeltaY;
1971 float finalScale;
1972 float initScale;
Adam Cohenfa3c58f2013-12-06 16:10:55 -08001973 int mode;
1974 boolean repeating = false;
1975 private static final int PREVIEW_DURATION = 300;
1976 private static final int HINT_DURATION = Workspace.REORDER_TIMEOUT;
1977
1978 public static final int MODE_HINT = 0;
1979 public static final int MODE_PREVIEW = 1;
1980
Adam Cohene7587d22012-05-24 18:50:02 -07001981 Animator a;
Adam Cohen19f37922012-03-21 11:59:11 -07001982
Adam Cohenfa3c58f2013-12-06 16:10:55 -08001983 public ReorderPreviewAnimation(View child, int mode, int cellX0, int cellY0, int cellX1,
1984 int cellY1, int spanX, int spanY) {
Adam Cohen19f37922012-03-21 11:59:11 -07001985 regionToCenterPoint(cellX0, cellY0, spanX, spanY, mTmpPoint);
1986 final int x0 = mTmpPoint[0];
1987 final int y0 = mTmpPoint[1];
1988 regionToCenterPoint(cellX1, cellY1, spanX, spanY, mTmpPoint);
1989 final int x1 = mTmpPoint[0];
1990 final int y1 = mTmpPoint[1];
1991 final int dX = x1 - x0;
1992 final int dY = y1 - y0;
Adam Cohend024f982012-05-23 18:26:45 -07001993 finalDeltaX = 0;
1994 finalDeltaY = 0;
Adam Cohenfa3c58f2013-12-06 16:10:55 -08001995 int dir = mode == MODE_HINT ? -1 : 1;
Adam Cohen19f37922012-03-21 11:59:11 -07001996 if (dX == dY && dX == 0) {
1997 } else {
1998 if (dY == 0) {
Adam Cohenfa3c58f2013-12-06 16:10:55 -08001999 finalDeltaX = - dir * Math.signum(dX) * mReorderPreviewAnimationMagnitude;
Adam Cohen19f37922012-03-21 11:59:11 -07002000 } else if (dX == 0) {
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002001 finalDeltaY = - dir * Math.signum(dY) * mReorderPreviewAnimationMagnitude;
Adam Cohen19f37922012-03-21 11:59:11 -07002002 } else {
2003 double angle = Math.atan( (float) (dY) / dX);
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002004 finalDeltaX = (int) (- dir * Math.signum(dX) *
2005 Math.abs(Math.cos(angle) * mReorderPreviewAnimationMagnitude));
2006 finalDeltaY = (int) (- dir * Math.signum(dY) *
2007 Math.abs(Math.sin(angle) * mReorderPreviewAnimationMagnitude));
Adam Cohen19f37922012-03-21 11:59:11 -07002008 }
2009 }
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002010 this.mode = mode;
Adam Cohend024f982012-05-23 18:26:45 -07002011 initDeltaX = child.getTranslationX();
2012 initDeltaY = child.getTranslationY();
Sunny Goyalc13403c2016-11-18 23:44:48 -08002013 finalScale = mChildScale - 4.0f / child.getWidth();
Adam Cohend024f982012-05-23 18:26:45 -07002014 initScale = child.getScaleX();
Adam Cohen19f37922012-03-21 11:59:11 -07002015 this.child = child;
2016 }
2017
Adam Cohend024f982012-05-23 18:26:45 -07002018 void animate() {
Adam Cohen19f37922012-03-21 11:59:11 -07002019 if (mShakeAnimators.containsKey(child)) {
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002020 ReorderPreviewAnimation oldAnimation = mShakeAnimators.get(child);
Adam Cohend024f982012-05-23 18:26:45 -07002021 oldAnimation.cancel();
Adam Cohen19f37922012-03-21 11:59:11 -07002022 mShakeAnimators.remove(child);
Adam Cohene7587d22012-05-24 18:50:02 -07002023 if (finalDeltaX == 0 && finalDeltaY == 0) {
2024 completeAnimationImmediately();
2025 return;
2026 }
Adam Cohen19f37922012-03-21 11:59:11 -07002027 }
Adam Cohend024f982012-05-23 18:26:45 -07002028 if (finalDeltaX == 0 && finalDeltaY == 0) {
Adam Cohen19f37922012-03-21 11:59:11 -07002029 return;
2030 }
Michael Jurkaf1ad6082013-03-13 12:55:46 +01002031 ValueAnimator va = LauncherAnimUtils.ofFloat(child, 0f, 1f);
Adam Cohene7587d22012-05-24 18:50:02 -07002032 a = va;
Tony Wickham9e0702f2015-09-02 14:45:39 -07002033
2034 // Animations are disabled in power save mode, causing the repeated animation to jump
2035 // spastically between beginning and end states. Since this looks bad, we don't repeat
2036 // the animation in power save mode.
Tony Wickham112ac952015-11-12 12:31:50 -08002037 if (!Utilities.isPowerSaverOn(getContext())) {
Tony Wickham9e0702f2015-09-02 14:45:39 -07002038 va.setRepeatMode(ValueAnimator.REVERSE);
2039 va.setRepeatCount(ValueAnimator.INFINITE);
2040 }
2041
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002042 va.setDuration(mode == MODE_HINT ? HINT_DURATION : PREVIEW_DURATION);
Adam Cohend024f982012-05-23 18:26:45 -07002043 va.setStartDelay((int) (Math.random() * 60));
Adam Cohen19f37922012-03-21 11:59:11 -07002044 va.addUpdateListener(new AnimatorUpdateListener() {
2045 @Override
2046 public void onAnimationUpdate(ValueAnimator animation) {
Jon Mirandae96798e2016-12-07 12:10:44 -08002047 float r = (Float) animation.getAnimatedValue();
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002048 float r1 = (mode == MODE_HINT && repeating) ? 1.0f : r;
2049 float x = r1 * finalDeltaX + (1 - r1) * initDeltaX;
2050 float y = r1 * finalDeltaY + (1 - r1) * initDeltaY;
Adam Cohen19f37922012-03-21 11:59:11 -07002051 child.setTranslationX(x);
2052 child.setTranslationY(y);
Adam Cohend024f982012-05-23 18:26:45 -07002053 float s = r * finalScale + (1 - r) * initScale;
Brandon Keely50e6e562012-05-08 16:28:49 -07002054 child.setScaleX(s);
2055 child.setScaleY(s);
Adam Cohen19f37922012-03-21 11:59:11 -07002056 }
2057 });
2058 va.addListener(new AnimatorListenerAdapter() {
2059 public void onAnimationRepeat(Animator animation) {
Adam Cohen19f37922012-03-21 11:59:11 -07002060 // We make sure to end only after a full period
Adam Cohend024f982012-05-23 18:26:45 -07002061 initDeltaX = 0;
2062 initDeltaY = 0;
Sunny Goyalc13403c2016-11-18 23:44:48 -08002063 initScale = mChildScale;
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002064 repeating = true;
Adam Cohen19f37922012-03-21 11:59:11 -07002065 }
2066 });
Adam Cohen19f37922012-03-21 11:59:11 -07002067 mShakeAnimators.put(child, this);
2068 va.start();
2069 }
2070
Adam Cohend024f982012-05-23 18:26:45 -07002071 private void cancel() {
Adam Cohene7587d22012-05-24 18:50:02 -07002072 if (a != null) {
2073 a.cancel();
2074 }
Adam Cohen19f37922012-03-21 11:59:11 -07002075 }
Adam Cohene7587d22012-05-24 18:50:02 -07002076
Adam Cohen091440a2015-03-18 14:16:05 -07002077 @Thunk void completeAnimationImmediately() {
Adam Cohene7587d22012-05-24 18:50:02 -07002078 if (a != null) {
2079 a.cancel();
2080 }
Brandon Keely50e6e562012-05-08 16:28:49 -07002081
Sunny Goyal5d2fc322015-07-06 22:52:49 -07002082 a = new LauncherViewPropertyAnimator(child)
Sunny Goyalc13403c2016-11-18 23:44:48 -08002083 .scaleX(mChildScale)
2084 .scaleY(mChildScale)
Sunny Goyal5d2fc322015-07-06 22:52:49 -07002085 .translationX(0)
2086 .translationY(0)
2087 .setDuration(REORDER_ANIMATION_DURATION);
2088 a.setInterpolator(new android.view.animation.DecelerateInterpolator(1.5f));
2089 a.start();
Brandon Keely50e6e562012-05-08 16:28:49 -07002090 }
Adam Cohen19f37922012-03-21 11:59:11 -07002091 }
2092
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002093 private void completeAndClearReorderPreviewAnimations() {
2094 for (ReorderPreviewAnimation a: mShakeAnimators.values()) {
Brandon Keely50e6e562012-05-08 16:28:49 -07002095 a.completeAnimationImmediately();
Adam Cohen19f37922012-03-21 11:59:11 -07002096 }
2097 mShakeAnimators.clear();
2098 }
2099
Adam Cohen482ed822012-03-02 14:15:13 -08002100 private void commitTempPlacement() {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07002101 mTmpOccupied.copyTo(mOccupied);
Sunny Goyalaa8ef112015-06-12 20:04:41 -07002102
2103 long screenId = mLauncher.getWorkspace().getIdForScreen(this);
2104 int container = Favorites.CONTAINER_DESKTOP;
2105
Sunny Goyalc13403c2016-11-18 23:44:48 -08002106 if (mContainerType == HOTSEAT) {
Sunny Goyalaa8ef112015-06-12 20:04:41 -07002107 screenId = -1;
2108 container = Favorites.CONTAINER_HOTSEAT;
2109 }
2110
Michael Jurkaa52570f2012-03-20 03:18:20 -07002111 int childCount = mShortcutsAndWidgets.getChildCount();
Adam Cohen482ed822012-03-02 14:15:13 -08002112 for (int i = 0; i < childCount; i++) {
Adam Cohenea889a22012-03-27 16:45:39 -07002113 View child = mShortcutsAndWidgets.getChildAt(i);
2114 LayoutParams lp = (LayoutParams) child.getLayoutParams();
2115 ItemInfo info = (ItemInfo) child.getTag();
Adam Cohen2acce882012-03-28 19:03:19 -07002116 // We do a null check here because the item info can be null in the case of the
2117 // AllApps button in the hotseat.
2118 if (info != null) {
Sunny Goyalaa8ef112015-06-12 20:04:41 -07002119 final boolean requiresDbUpdate = (info.cellX != lp.tmpCellX
2120 || info.cellY != lp.tmpCellY || info.spanX != lp.cellHSpan
2121 || info.spanY != lp.cellVSpan);
2122
Adam Cohen2acce882012-03-28 19:03:19 -07002123 info.cellX = lp.cellX = lp.tmpCellX;
2124 info.cellY = lp.cellY = lp.tmpCellY;
Adam Cohenbebf0422012-04-11 18:06:28 -07002125 info.spanX = lp.cellHSpan;
2126 info.spanY = lp.cellVSpan;
Sunny Goyalaa8ef112015-06-12 20:04:41 -07002127
2128 if (requiresDbUpdate) {
Sunny Goyalc13403c2016-11-18 23:44:48 -08002129 LauncherModel.modifyItemInDatabase(getContext(), info, container, screenId,
Sunny Goyalaa8ef112015-06-12 20:04:41 -07002130 info.cellX, info.cellY, info.spanX, info.spanY);
2131 }
Adam Cohen2acce882012-03-28 19:03:19 -07002132 }
Adam Cohen482ed822012-03-02 14:15:13 -08002133 }
2134 }
2135
Sunny Goyalf7a29e82015-04-24 15:20:43 -07002136 private void setUseTempCoords(boolean useTempCoords) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07002137 int childCount = mShortcutsAndWidgets.getChildCount();
Adam Cohen482ed822012-03-02 14:15:13 -08002138 for (int i = 0; i < childCount; i++) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07002139 LayoutParams lp = (LayoutParams) mShortcutsAndWidgets.getChildAt(i).getLayoutParams();
Adam Cohen482ed822012-03-02 14:15:13 -08002140 lp.useTmpCoords = useTempCoords;
2141 }
2142 }
2143
Sunny Goyalf7a29e82015-04-24 15:20:43 -07002144 private ItemConfiguration findConfigurationNoShuffle(int pixelX, int pixelY, int minSpanX, int minSpanY,
Adam Cohen482ed822012-03-02 14:15:13 -08002145 int spanX, int spanY, View dragView, ItemConfiguration solution) {
2146 int[] result = new int[2];
2147 int[] resultSpan = new int[2];
Sunny Goyalf7a29e82015-04-24 15:20:43 -07002148 findNearestVacantArea(pixelX, pixelY, minSpanX, minSpanY, spanX, spanY, result,
Adam Cohen482ed822012-03-02 14:15:13 -08002149 resultSpan);
2150 if (result[0] >= 0 && result[1] >= 0) {
2151 copyCurrentStateToSolution(solution, false);
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07002152 solution.cellX = result[0];
2153 solution.cellY = result[1];
2154 solution.spanX = resultSpan[0];
2155 solution.spanY = resultSpan[1];
Adam Cohen482ed822012-03-02 14:15:13 -08002156 solution.isSolution = true;
2157 } else {
2158 solution.isSolution = false;
2159 }
2160 return solution;
2161 }
2162
Adam Cohen19f37922012-03-21 11:59:11 -07002163 /* This seems like it should be obvious and straight-forward, but when the direction vector
2164 needs to match with the notion of the dragView pushing other views, we have to employ
2165 a slightly more subtle notion of the direction vector. The question is what two points is
2166 the vector between? The center of the dragView and its desired destination? Not quite, as
2167 this doesn't necessarily coincide with the interaction of the dragView and items occupying
2168 those cells. Instead we use some heuristics to often lock the vector to up, down, left
2169 or right, which helps make pushing feel right.
2170 */
2171 private void getDirectionVectorForDrop(int dragViewCenterX, int dragViewCenterY, int spanX,
2172 int spanY, View dragView, int[] resultDirection) {
2173 int[] targetDestination = new int[2];
2174
2175 findNearestArea(dragViewCenterX, dragViewCenterY, spanX, spanY, targetDestination);
2176 Rect dragRect = new Rect();
2177 regionToRect(targetDestination[0], targetDestination[1], spanX, spanY, dragRect);
2178 dragRect.offset(dragViewCenterX - dragRect.centerX(), dragViewCenterY - dragRect.centerY());
2179
2180 Rect dropRegionRect = new Rect();
2181 getViewsIntersectingRegion(targetDestination[0], targetDestination[1], spanX, spanY,
2182 dragView, dropRegionRect, mIntersectingViews);
2183
2184 int dropRegionSpanX = dropRegionRect.width();
2185 int dropRegionSpanY = dropRegionRect.height();
2186
2187 regionToRect(dropRegionRect.left, dropRegionRect.top, dropRegionRect.width(),
2188 dropRegionRect.height(), dropRegionRect);
2189
2190 int deltaX = (dropRegionRect.centerX() - dragViewCenterX) / spanX;
2191 int deltaY = (dropRegionRect.centerY() - dragViewCenterY) / spanY;
2192
2193 if (dropRegionSpanX == mCountX || spanX == mCountX) {
2194 deltaX = 0;
2195 }
2196 if (dropRegionSpanY == mCountY || spanY == mCountY) {
2197 deltaY = 0;
2198 }
2199
2200 if (deltaX == 0 && deltaY == 0) {
2201 // No idea what to do, give a random direction.
2202 resultDirection[0] = 1;
2203 resultDirection[1] = 0;
2204 } else {
2205 computeDirectionVector(deltaX, deltaY, resultDirection);
2206 }
2207 }
2208
2209 // For a given cell and span, fetch the set of views intersecting the region.
2210 private void getViewsIntersectingRegion(int cellX, int cellY, int spanX, int spanY,
2211 View dragView, Rect boundingRect, ArrayList<View> intersectingViews) {
2212 if (boundingRect != null) {
2213 boundingRect.set(cellX, cellY, cellX + spanX, cellY + spanY);
2214 }
2215 intersectingViews.clear();
2216 Rect r0 = new Rect(cellX, cellY, cellX + spanX, cellY + spanY);
2217 Rect r1 = new Rect();
2218 final int count = mShortcutsAndWidgets.getChildCount();
2219 for (int i = 0; i < count; i++) {
2220 View child = mShortcutsAndWidgets.getChildAt(i);
2221 if (child == dragView) continue;
2222 LayoutParams lp = (LayoutParams) child.getLayoutParams();
2223 r1.set(lp.cellX, lp.cellY, lp.cellX + lp.cellHSpan, lp.cellY + lp.cellVSpan);
2224 if (Rect.intersects(r0, r1)) {
2225 mIntersectingViews.add(child);
2226 if (boundingRect != null) {
2227 boundingRect.union(r1);
2228 }
2229 }
2230 }
2231 }
2232
2233 boolean isNearestDropLocationOccupied(int pixelX, int pixelY, int spanX, int spanY,
2234 View dragView, int[] result) {
2235 result = findNearestArea(pixelX, pixelY, spanX, spanY, result);
2236 getViewsIntersectingRegion(result[0], result[1], spanX, spanY, dragView, null,
2237 mIntersectingViews);
2238 return !mIntersectingViews.isEmpty();
2239 }
2240
2241 void revertTempState() {
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002242 completeAndClearReorderPreviewAnimations();
2243 if (isItemPlacementDirty() && !DESTRUCTIVE_REORDER) {
2244 final int count = mShortcutsAndWidgets.getChildCount();
2245 for (int i = 0; i < count; i++) {
2246 View child = mShortcutsAndWidgets.getChildAt(i);
2247 LayoutParams lp = (LayoutParams) child.getLayoutParams();
2248 if (lp.tmpCellX != lp.cellX || lp.tmpCellY != lp.cellY) {
2249 lp.tmpCellX = lp.cellX;
2250 lp.tmpCellY = lp.cellY;
2251 animateChildToPosition(child, lp.cellX, lp.cellY, REORDER_ANIMATION_DURATION,
2252 0, false, false);
2253 }
Adam Cohen19f37922012-03-21 11:59:11 -07002254 }
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002255 setItemPlacementDirty(false);
Adam Cohen19f37922012-03-21 11:59:11 -07002256 }
Adam Cohen19f37922012-03-21 11:59:11 -07002257 }
2258
Adam Cohenbebf0422012-04-11 18:06:28 -07002259 boolean createAreaForResize(int cellX, int cellY, int spanX, int spanY,
2260 View dragView, int[] direction, boolean commit) {
2261 int[] pixelXY = new int[2];
2262 regionToCenterPoint(cellX, cellY, spanX, spanY, pixelXY);
2263
2264 // First we determine if things have moved enough to cause a different layout
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002265 ItemConfiguration swapSolution = findReorderSolution(pixelXY[0], pixelXY[1], spanX, spanY,
Adam Cohenbebf0422012-04-11 18:06:28 -07002266 spanX, spanY, direction, dragView, true, new ItemConfiguration());
2267
2268 setUseTempCoords(true);
2269 if (swapSolution != null && swapSolution.isSolution) {
2270 // If we're just testing for a possible location (MODE_ACCEPT_DROP), we don't bother
2271 // committing anything or animating anything as we just want to determine if a solution
2272 // exists
2273 copySolutionToTempState(swapSolution, dragView);
2274 setItemPlacementDirty(true);
2275 animateItemsToSolution(swapSolution, dragView, commit);
2276
2277 if (commit) {
2278 commitTempPlacement();
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002279 completeAndClearReorderPreviewAnimations();
Adam Cohenbebf0422012-04-11 18:06:28 -07002280 setItemPlacementDirty(false);
2281 } else {
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002282 beginOrAdjustReorderPreviewAnimations(swapSolution, dragView,
2283 REORDER_ANIMATION_DURATION, ReorderPreviewAnimation.MODE_PREVIEW);
Adam Cohenbebf0422012-04-11 18:06:28 -07002284 }
2285 mShortcutsAndWidgets.requestLayout();
2286 }
2287 return swapSolution.isSolution;
2288 }
2289
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002290 int[] performReorder(int pixelX, int pixelY, int minSpanX, int minSpanY, int spanX, int spanY,
Adam Cohen482ed822012-03-02 14:15:13 -08002291 View dragView, int[] result, int resultSpan[], int mode) {
Adam Cohen482ed822012-03-02 14:15:13 -08002292 // First we determine if things have moved enough to cause a different layout
Adam Cohen47a876d2012-03-19 13:21:41 -07002293 result = findNearestArea(pixelX, pixelY, spanX, spanY, result);
Adam Cohen482ed822012-03-02 14:15:13 -08002294
2295 if (resultSpan == null) {
2296 resultSpan = new int[2];
2297 }
2298
Adam Cohen19f37922012-03-21 11:59:11 -07002299 // When we are checking drop validity or actually dropping, we don't recompute the
2300 // direction vector, since we want the solution to match the preview, and it's possible
2301 // that the exact position of the item has changed to result in a new reordering outcome.
Adam Cohenb209e632012-03-27 17:09:36 -07002302 if ((mode == MODE_ON_DROP || mode == MODE_ON_DROP_EXTERNAL || mode == MODE_ACCEPT_DROP)
2303 && mPreviousReorderDirection[0] != INVALID_DIRECTION) {
Adam Cohen19f37922012-03-21 11:59:11 -07002304 mDirectionVector[0] = mPreviousReorderDirection[0];
2305 mDirectionVector[1] = mPreviousReorderDirection[1];
2306 // We reset this vector after drop
Adam Cohenb209e632012-03-27 17:09:36 -07002307 if (mode == MODE_ON_DROP || mode == MODE_ON_DROP_EXTERNAL) {
2308 mPreviousReorderDirection[0] = INVALID_DIRECTION;
2309 mPreviousReorderDirection[1] = INVALID_DIRECTION;
Adam Cohen19f37922012-03-21 11:59:11 -07002310 }
2311 } else {
2312 getDirectionVectorForDrop(pixelX, pixelY, spanX, spanY, dragView, mDirectionVector);
2313 mPreviousReorderDirection[0] = mDirectionVector[0];
2314 mPreviousReorderDirection[1] = mDirectionVector[1];
2315 }
2316
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002317 // Find a solution involving pushing / displacing any items in the way
2318 ItemConfiguration swapSolution = findReorderSolution(pixelX, pixelY, minSpanX, minSpanY,
Adam Cohen482ed822012-03-02 14:15:13 -08002319 spanX, spanY, mDirectionVector, dragView, true, new ItemConfiguration());
2320
2321 // We attempt the approach which doesn't shuffle views at all
2322 ItemConfiguration noShuffleSolution = findConfigurationNoShuffle(pixelX, pixelY, minSpanX,
2323 minSpanY, spanX, spanY, dragView, new ItemConfiguration());
2324
2325 ItemConfiguration finalSolution = null;
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002326
2327 // If the reorder solution requires resizing (shrinking) the item being dropped, we instead
2328 // favor a solution in which the item is not resized, but
Adam Cohen482ed822012-03-02 14:15:13 -08002329 if (swapSolution.isSolution && swapSolution.area() >= noShuffleSolution.area()) {
2330 finalSolution = swapSolution;
2331 } else if (noShuffleSolution.isSolution) {
2332 finalSolution = noShuffleSolution;
2333 }
2334
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002335 if (mode == MODE_SHOW_REORDER_HINT) {
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002336 if (finalSolution != null) {
Adam Cohenfe692872013-12-11 14:47:23 -08002337 beginOrAdjustReorderPreviewAnimations(finalSolution, dragView, 0,
2338 ReorderPreviewAnimation.MODE_HINT);
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07002339 result[0] = finalSolution.cellX;
2340 result[1] = finalSolution.cellY;
2341 resultSpan[0] = finalSolution.spanX;
2342 resultSpan[1] = finalSolution.spanY;
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002343 } else {
2344 result[0] = result[1] = resultSpan[0] = resultSpan[1] = -1;
2345 }
2346 return result;
2347 }
2348
Adam Cohen482ed822012-03-02 14:15:13 -08002349 boolean foundSolution = true;
2350 if (!DESTRUCTIVE_REORDER) {
2351 setUseTempCoords(true);
2352 }
2353
2354 if (finalSolution != null) {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07002355 result[0] = finalSolution.cellX;
2356 result[1] = finalSolution.cellY;
2357 resultSpan[0] = finalSolution.spanX;
2358 resultSpan[1] = finalSolution.spanY;
Adam Cohen482ed822012-03-02 14:15:13 -08002359
2360 // If we're just testing for a possible location (MODE_ACCEPT_DROP), we don't bother
2361 // committing anything or animating anything as we just want to determine if a solution
2362 // exists
2363 if (mode == MODE_DRAG_OVER || mode == MODE_ON_DROP || mode == MODE_ON_DROP_EXTERNAL) {
2364 if (!DESTRUCTIVE_REORDER) {
2365 copySolutionToTempState(finalSolution, dragView);
2366 }
2367 setItemPlacementDirty(true);
2368 animateItemsToSolution(finalSolution, dragView, mode == MODE_ON_DROP);
2369
Adam Cohen19f37922012-03-21 11:59:11 -07002370 if (!DESTRUCTIVE_REORDER &&
2371 (mode == MODE_ON_DROP || mode == MODE_ON_DROP_EXTERNAL)) {
Adam Cohen482ed822012-03-02 14:15:13 -08002372 commitTempPlacement();
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002373 completeAndClearReorderPreviewAnimations();
Adam Cohen19f37922012-03-21 11:59:11 -07002374 setItemPlacementDirty(false);
2375 } else {
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002376 beginOrAdjustReorderPreviewAnimations(finalSolution, dragView,
2377 REORDER_ANIMATION_DURATION, ReorderPreviewAnimation.MODE_PREVIEW);
Adam Cohen482ed822012-03-02 14:15:13 -08002378 }
2379 }
2380 } else {
2381 foundSolution = false;
2382 result[0] = result[1] = resultSpan[0] = resultSpan[1] = -1;
2383 }
2384
2385 if ((mode == MODE_ON_DROP || !foundSolution) && !DESTRUCTIVE_REORDER) {
2386 setUseTempCoords(false);
2387 }
Adam Cohen482ed822012-03-02 14:15:13 -08002388
Michael Jurkaa52570f2012-03-20 03:18:20 -07002389 mShortcutsAndWidgets.requestLayout();
Adam Cohen482ed822012-03-02 14:15:13 -08002390 return result;
2391 }
2392
Adam Cohen19f37922012-03-21 11:59:11 -07002393 void setItemPlacementDirty(boolean dirty) {
2394 mItemPlacementDirty = dirty;
Adam Cohen482ed822012-03-02 14:15:13 -08002395 }
Adam Cohen19f37922012-03-21 11:59:11 -07002396 boolean isItemPlacementDirty() {
2397 return mItemPlacementDirty;
Adam Cohen482ed822012-03-02 14:15:13 -08002398 }
2399
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07002400 private static class ItemConfiguration extends CellAndSpan {
Adam Cohen8baab352012-03-20 17:39:21 -07002401 HashMap<View, CellAndSpan> map = new HashMap<View, CellAndSpan>();
Adam Cohenf3900c22012-11-16 18:28:11 -08002402 private HashMap<View, CellAndSpan> savedMap = new HashMap<View, CellAndSpan>();
2403 ArrayList<View> sortedViews = new ArrayList<View>();
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002404 ArrayList<View> intersectingViews;
Adam Cohen482ed822012-03-02 14:15:13 -08002405 boolean isSolution = false;
Adam Cohen482ed822012-03-02 14:15:13 -08002406
Adam Cohenf3900c22012-11-16 18:28:11 -08002407 void save() {
2408 // Copy current state into savedMap
2409 for (View v: map.keySet()) {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07002410 savedMap.get(v).copyFrom(map.get(v));
Adam Cohenf3900c22012-11-16 18:28:11 -08002411 }
2412 }
2413
2414 void restore() {
2415 // Restore current state from savedMap
2416 for (View v: savedMap.keySet()) {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07002417 map.get(v).copyFrom(savedMap.get(v));
Adam Cohenf3900c22012-11-16 18:28:11 -08002418 }
2419 }
2420
2421 void add(View v, CellAndSpan cs) {
2422 map.put(v, cs);
2423 savedMap.put(v, new CellAndSpan());
2424 sortedViews.add(v);
2425 }
2426
Adam Cohen482ed822012-03-02 14:15:13 -08002427 int area() {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07002428 return spanX * spanY;
Adam Cohenf3900c22012-11-16 18:28:11 -08002429 }
2430
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07002431 void getBoundingRectForViews(ArrayList<View> views, Rect outRect) {
2432 boolean first = true;
2433 for (View v: views) {
2434 CellAndSpan c = map.get(v);
2435 if (first) {
2436 outRect.set(c.cellX, c.cellY, c.cellX + c.spanX, c.cellY + c.spanY);
2437 first = false;
2438 } else {
2439 outRect.union(c.cellX, c.cellY, c.cellX + c.spanX, c.cellY + c.spanY);
2440 }
2441 }
Adam Cohenf3900c22012-11-16 18:28:11 -08002442 }
Adam Cohen482ed822012-03-02 14:15:13 -08002443 }
2444
Adam Cohendf035382011-04-11 17:22:04 -07002445 /**
Adam Cohendf035382011-04-11 17:22:04 -07002446 * Find a starting cell position that will fit the given bounds nearest the requested
2447 * cell location. Uses Euclidean distance to score multiple vacant areas.
2448 *
2449 * @param pixelX The X location at which you want to search for a vacant area.
2450 * @param pixelY The Y location at which you want to search for a vacant area.
2451 * @param spanX Horizontal span of the object.
2452 * @param spanY Vertical span of the object.
2453 * @param ignoreView Considers space occupied by this view as unoccupied
2454 * @param result Previously returned value to possibly recycle.
2455 * @return The X, Y cell of a vacant area that can contain this object,
2456 * nearest the requested location.
2457 */
Adam Cohenf9c184a2016-01-15 16:47:43 -08002458 public int[] findNearestArea(int pixelX, int pixelY, int spanX, int spanY, int[] result) {
Sunny Goyalf7a29e82015-04-24 15:20:43 -07002459 return findNearestArea(pixelX, pixelY, spanX, spanY, spanX, spanY, false, result, null);
Adam Cohendf035382011-04-11 17:22:04 -07002460 }
2461
Michael Jurka0280c3b2010-09-17 15:00:07 -07002462 boolean existsEmptyCell() {
2463 return findCellForSpan(null, 1, 1);
2464 }
2465
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002466 /**
Michael Jurka0280c3b2010-09-17 15:00:07 -07002467 * Finds the upper-left coordinate of the first rectangle in the grid that can
2468 * hold a cell of the specified dimensions. If intersectX and intersectY are not -1,
2469 * then this method will only return coordinates for rectangles that contain the cell
2470 * (intersectX, intersectY)
2471 *
2472 * @param cellXY The array that will contain the position of a vacant cell if such a cell
2473 * can be found.
2474 * @param spanX The horizontal span of the cell we want to find.
2475 * @param spanY The vertical span of the cell we want to find.
2476 *
2477 * @return True if a vacant cell of the specified dimension was found, false otherwise.
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07002478 */
Hyunyoung Song3f471442015-04-08 19:01:34 -07002479 public boolean findCellForSpan(int[] cellXY, int spanX, int spanY) {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07002480 if (cellXY == null) {
2481 cellXY = new int[2];
Michael Jurka0280c3b2010-09-17 15:00:07 -07002482 }
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07002483 return mOccupied.findVacantCell(cellXY, spanX, spanY);
Michael Jurka0280c3b2010-09-17 15:00:07 -07002484 }
2485
2486 /**
Winson Chungc07918d2011-07-01 15:35:26 -07002487 * A drag event has begun over this layout.
2488 * It may have begun over this layout (in which case onDragChild is called first),
2489 * or it may have begun on another layout.
2490 */
2491 void onDragEnter() {
Winson Chungc07918d2011-07-01 15:35:26 -07002492 mDragging = true;
2493 }
2494
2495 /**
Michael Jurka0280c3b2010-09-17 15:00:07 -07002496 * Called when drag has left this CellLayout or has been completed (successfully or not)
2497 */
2498 void onDragExit() {
Joe Onorato4be866d2010-10-10 11:26:02 -07002499 // This can actually be called when we aren't in a drag, e.g. when adding a new
2500 // item to this layout via the customize drawer.
2501 // Guard against that case.
2502 if (mDragging) {
2503 mDragging = false;
Patrick Dubroyde7658b2010-09-27 11:15:43 -07002504 }
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -07002505
2506 // Invalidate the drag data
Adam Cohend41fbf52012-02-16 23:53:59 -08002507 mDragCell[0] = mDragCell[1] = -1;
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -07002508 mDragOutlineAnims[mDragOutlineCurrent].animateOut();
2509 mDragOutlineCurrent = (mDragOutlineCurrent + 1) % mDragOutlineAnims.length;
Adam Cohen19f37922012-03-21 11:59:11 -07002510 revertTempState();
Michael Jurka33945b22010-12-21 18:19:38 -08002511 setIsDragOverlapping(false);
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07002512 }
2513
2514 /**
Winson Chungaafa03c2010-06-11 17:34:16 -07002515 * Mark a child as having been dropped.
Patrick Dubroyde7658b2010-09-27 11:15:43 -07002516 * At the beginning of the drag operation, the child may have been on another
Patrick Dubroyce34a972010-10-19 10:34:32 -07002517 * screen, but it is re-parented before this method is called.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002518 *
2519 * @param child The child that is being dropped
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002520 */
Adam Cohen716b51e2011-06-30 12:09:54 -07002521 void onDropChild(View child) {
Romain Guyd94533d2009-08-17 10:01:15 -07002522 if (child != null) {
2523 LayoutParams lp = (LayoutParams) child.getLayoutParams();
Romain Guy84f296c2009-11-04 15:00:44 -08002524 lp.dropped = true;
Romain Guyd94533d2009-08-17 10:01:15 -07002525 child.requestLayout();
Tony Wickham1cdb6d02015-09-17 11:08:27 -07002526 markCellsAsOccupiedForView(child);
Romain Guyd94533d2009-08-17 10:01:15 -07002527 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002528 }
2529
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002530 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002531 * Computes a bounding rectangle for a range of cells
Winson Chungaafa03c2010-06-11 17:34:16 -07002532 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002533 * @param cellX X coordinate of upper left corner expressed as a cell position
2534 * @param cellY Y coordinate of upper left corner expressed as a cell position
Winson Chungaafa03c2010-06-11 17:34:16 -07002535 * @param cellHSpan Width in cells
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002536 * @param cellVSpan Height in cells
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07002537 * @param resultRect Rect into which to put the results
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002538 */
Adam Cohend41fbf52012-02-16 23:53:59 -08002539 public void cellToRect(int cellX, int cellY, int cellHSpan, int cellVSpan, Rect resultRect) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002540 final int cellWidth = mCellWidth;
2541 final int cellHeight = mCellHeight;
Winson Chungaafa03c2010-06-11 17:34:16 -07002542
Winson Chung4b825dcd2011-06-19 12:41:22 -07002543 final int hStartPadding = getPaddingLeft();
2544 final int vStartPadding = getPaddingTop();
Winson Chungaafa03c2010-06-11 17:34:16 -07002545
Sunny Goyalaa8a8712016-11-20 15:26:01 +05302546 int width = cellHSpan * cellWidth;
2547 int height = cellVSpan * cellHeight;
2548 int x = hStartPadding + cellX * cellWidth;
2549 int y = vStartPadding + cellY * cellHeight;
Winson Chungaafa03c2010-06-11 17:34:16 -07002550
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07002551 resultRect.set(x, y, x + width, y + height);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002552 }
Winson Chungaafa03c2010-06-11 17:34:16 -07002553
Adam Cohend4844c32011-02-18 19:25:06 -08002554 public void markCellsAsOccupiedForView(View view) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07002555 if (view == null || view.getParent() != mShortcutsAndWidgets) return;
Michael Jurka0280c3b2010-09-17 15:00:07 -07002556 LayoutParams lp = (LayoutParams) view.getLayoutParams();
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07002557 mOccupied.markCells(lp.cellX, lp.cellY, lp.cellHSpan, lp.cellVSpan, true);
Michael Jurka0280c3b2010-09-17 15:00:07 -07002558 }
2559
Adam Cohend4844c32011-02-18 19:25:06 -08002560 public void markCellsAsUnoccupiedForView(View view) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07002561 if (view == null || view.getParent() != mShortcutsAndWidgets) return;
Michael Jurka0280c3b2010-09-17 15:00:07 -07002562 LayoutParams lp = (LayoutParams) view.getLayoutParams();
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07002563 mOccupied.markCells(lp.cellX, lp.cellY, lp.cellHSpan, lp.cellVSpan, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002564 }
2565
Adam Cohen2801caf2011-05-13 20:57:39 -07002566 public int getDesiredWidth() {
Sunny Goyalaa8a8712016-11-20 15:26:01 +05302567 return getPaddingLeft() + getPaddingRight() + (mCountX * mCellWidth);
Adam Cohen2801caf2011-05-13 20:57:39 -07002568 }
2569
2570 public int getDesiredHeight() {
Sunny Goyalaa8a8712016-11-20 15:26:01 +05302571 return getPaddingTop() + getPaddingBottom() + (mCountY * mCellHeight);
Adam Cohen2801caf2011-05-13 20:57:39 -07002572 }
2573
Michael Jurka66d72172011-04-12 16:29:25 -07002574 public boolean isOccupied(int x, int y) {
2575 if (x < mCountX && y < mCountY) {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07002576 return mOccupied.cells[x][y];
Michael Jurka66d72172011-04-12 16:29:25 -07002577 } else {
2578 throw new RuntimeException("Position exceeds the bound of this CellLayout");
2579 }
2580 }
2581
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002582 @Override
2583 public ViewGroup.LayoutParams generateLayoutParams(AttributeSet attrs) {
2584 return new CellLayout.LayoutParams(getContext(), attrs);
2585 }
2586
2587 @Override
2588 protected boolean checkLayoutParams(ViewGroup.LayoutParams p) {
2589 return p instanceof CellLayout.LayoutParams;
2590 }
2591
2592 @Override
2593 protected ViewGroup.LayoutParams generateLayoutParams(ViewGroup.LayoutParams p) {
2594 return new CellLayout.LayoutParams(p);
2595 }
2596
2597 public static class LayoutParams extends ViewGroup.MarginLayoutParams {
2598 /**
2599 * Horizontal location of the item in the grid.
2600 */
2601 @ViewDebug.ExportedProperty
2602 public int cellX;
2603
2604 /**
2605 * Vertical location of the item in the grid.
2606 */
2607 @ViewDebug.ExportedProperty
2608 public int cellY;
2609
2610 /**
Adam Cohen482ed822012-03-02 14:15:13 -08002611 * Temporary horizontal location of the item in the grid during reorder
2612 */
2613 public int tmpCellX;
2614
2615 /**
2616 * Temporary vertical location of the item in the grid during reorder
2617 */
2618 public int tmpCellY;
2619
2620 /**
2621 * Indicates that the temporary coordinates should be used to layout the items
2622 */
2623 public boolean useTmpCoords;
2624
2625 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002626 * Number of cells spanned horizontally by the item.
2627 */
2628 @ViewDebug.ExportedProperty
2629 public int cellHSpan;
2630
2631 /**
2632 * Number of cells spanned vertically by the item.
2633 */
2634 @ViewDebug.ExportedProperty
2635 public int cellVSpan;
Winson Chungaafa03c2010-06-11 17:34:16 -07002636
Adam Cohen1b607ed2011-03-03 17:26:50 -08002637 /**
2638 * Indicates whether the item will set its x, y, width and height parameters freely,
2639 * or whether these will be computed based on cellX, cellY, cellHSpan and cellVSpan.
2640 */
Adam Cohend4844c32011-02-18 19:25:06 -08002641 public boolean isLockedToGrid = true;
2642
Adam Cohen482ed822012-03-02 14:15:13 -08002643 /**
Adam Cohenec40b2b2013-07-23 15:52:40 -07002644 * Indicates that this item should use the full extents of its parent.
2645 */
2646 public boolean isFullscreen = false;
2647
2648 /**
Adam Cohen482ed822012-03-02 14:15:13 -08002649 * Indicates whether this item can be reordered. Always true except in the case of the
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07002650 * the AllApps button and QSB place holder.
Adam Cohen482ed822012-03-02 14:15:13 -08002651 */
2652 public boolean canReorder = true;
2653
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002654 // X coordinate of the view in the layout.
2655 @ViewDebug.ExportedProperty
Vadim Tryshevfedca432015-08-19 17:55:02 -07002656 public int x;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002657 // Y coordinate of the view in the layout.
2658 @ViewDebug.ExportedProperty
Vadim Tryshevfedca432015-08-19 17:55:02 -07002659 public int y;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002660
Romain Guy84f296c2009-11-04 15:00:44 -08002661 boolean dropped;
Romain Guyfcb9e712009-10-02 16:06:52 -07002662
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002663 public LayoutParams(Context c, AttributeSet attrs) {
2664 super(c, attrs);
2665 cellHSpan = 1;
2666 cellVSpan = 1;
2667 }
2668
2669 public LayoutParams(ViewGroup.LayoutParams source) {
2670 super(source);
2671 cellHSpan = 1;
2672 cellVSpan = 1;
2673 }
Winson Chungaafa03c2010-06-11 17:34:16 -07002674
2675 public LayoutParams(LayoutParams source) {
2676 super(source);
2677 this.cellX = source.cellX;
2678 this.cellY = source.cellY;
2679 this.cellHSpan = source.cellHSpan;
2680 this.cellVSpan = source.cellVSpan;
2681 }
2682
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002683 public LayoutParams(int cellX, int cellY, int cellHSpan, int cellVSpan) {
Romain Guy8f19cdd2010-01-08 15:07:00 -08002684 super(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002685 this.cellX = cellX;
2686 this.cellY = cellY;
2687 this.cellHSpan = cellHSpan;
2688 this.cellVSpan = cellVSpan;
2689 }
2690
Sunny Goyalaa8a8712016-11-20 15:26:01 +05302691 public void setup(int cellWidth, int cellHeight, boolean invertHorizontally, int colCount) {
Jon Miranda7ae64ff2016-11-21 16:18:46 -08002692 setup(cellWidth, cellHeight, invertHorizontally, colCount, 1.0f, 1.0f);
2693 }
2694
2695 /**
2696 * Use this method, as opposed to {@link #setup(int, int, boolean, int)}, if the view needs
2697 * to be scaled.
2698 *
2699 * ie. In multi-window mode, we setup widgets so that they are measured and laid out
2700 * using their full/invariant device profile sizes.
2701 */
2702 public void setup(int cellWidth, int cellHeight, boolean invertHorizontally, int colCount,
2703 float cellScaleX, float cellScaleY) {
Adam Cohend4844c32011-02-18 19:25:06 -08002704 if (isLockedToGrid) {
2705 final int myCellHSpan = cellHSpan;
2706 final int myCellVSpan = cellVSpan;
Adam Cohen2374abf2013-04-16 14:56:57 -07002707 int myCellX = useTmpCoords ? tmpCellX : cellX;
2708 int myCellY = useTmpCoords ? tmpCellY : cellY;
2709
2710 if (invertHorizontally) {
2711 myCellX = colCount - myCellX - cellHSpan;
2712 }
Adam Cohen1b607ed2011-03-03 17:26:50 -08002713
Jon Miranda7ae64ff2016-11-21 16:18:46 -08002714 width = (int) (myCellHSpan * cellWidth / cellScaleX - leftMargin - rightMargin);
2715 height = (int) (myCellVSpan * cellHeight / cellScaleY - topMargin - bottomMargin);
Sunny Goyalaa8a8712016-11-20 15:26:01 +05302716 x = (myCellX * cellWidth + leftMargin);
2717 y = (myCellY * cellHeight + topMargin);
Adam Cohend4844c32011-02-18 19:25:06 -08002718 }
2719 }
Winson Chungaafa03c2010-06-11 17:34:16 -07002720
Winson Chungaafa03c2010-06-11 17:34:16 -07002721 public String toString() {
2722 return "(" + this.cellX + ", " + this.cellY + ")";
2723 }
Adam Cohen7f4eabe2011-04-21 16:19:16 -07002724
2725 public void setWidth(int width) {
2726 this.width = width;
2727 }
2728
2729 public int getWidth() {
2730 return width;
2731 }
2732
2733 public void setHeight(int height) {
2734 this.height = height;
2735 }
2736
2737 public int getHeight() {
2738 return height;
2739 }
2740
2741 public void setX(int x) {
2742 this.x = x;
2743 }
2744
2745 public int getX() {
2746 return x;
2747 }
2748
2749 public void setY(int y) {
2750 this.y = y;
2751 }
2752
2753 public int getY() {
2754 return y;
2755 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002756 }
2757
Michael Jurka0280c3b2010-09-17 15:00:07 -07002758 // This class stores info for two purposes:
2759 // 1. When dragging items (mDragInfo in Workspace), we store the View, its cellX & cellY,
2760 // its spanX, spanY, and the screen it is on
2761 // 2. When long clicking on an empty cell in a CellLayout, we save information about the
2762 // cellX and cellY coordinates and which page was clicked. We then set this as a tag on
2763 // the CellLayout that was long clicked
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07002764 public static final class CellInfo extends CellAndSpan {
Adam Cohenf9c184a2016-01-15 16:47:43 -08002765 public View cell;
Adam Cohendcd297f2013-06-18 13:13:40 -07002766 long screenId;
Winson Chung3d503fb2011-07-13 17:25:49 -07002767 long container;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002768
Sunny Goyal83a8f042015-05-19 12:52:12 -07002769 public CellInfo(View v, ItemInfo info) {
Adam Cohene0aaa0d2014-05-12 12:44:22 -07002770 cellX = info.cellX;
2771 cellY = info.cellY;
2772 spanX = info.spanX;
2773 spanY = info.spanY;
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07002774 cell = v;
Adam Cohene0aaa0d2014-05-12 12:44:22 -07002775 screenId = info.screenId;
2776 container = info.container;
2777 }
2778
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002779 @Override
2780 public String toString() {
Winson Chungaafa03c2010-06-11 17:34:16 -07002781 return "Cell[view=" + (cell == null ? "null" : cell.getClass())
2782 + ", x=" + cellX + ", y=" + cellY + "]";
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002783 }
2784 }
Michael Jurkad771c962011-08-09 15:00:48 -07002785
Tony Wickham86930612015-09-09 13:50:40 -07002786 /**
2787 * Returns whether an item can be placed in this CellLayout (after rearranging and/or resizing
2788 * if necessary).
2789 */
2790 public boolean hasReorderSolution(ItemInfo itemInfo) {
2791 int[] cellPoint = new int[2];
2792 // Check for a solution starting at every cell.
2793 for (int cellX = 0; cellX < getCountX(); cellX++) {
2794 for (int cellY = 0; cellY < getCountY(); cellY++) {
2795 cellToPoint(cellX, cellY, cellPoint);
2796 if (findReorderSolution(cellPoint[0], cellPoint[1], itemInfo.minSpanX,
2797 itemInfo.minSpanY, itemInfo.spanX, itemInfo.spanY, mDirectionVector, null,
2798 true, new ItemConfiguration()).isSolution) {
2799 return true;
2800 }
2801 }
2802 }
2803 return false;
2804 }
2805
Sunny Goyal9ca9c132015-04-29 14:57:22 -07002806 public boolean isRegionVacant(int x, int y, int spanX, int spanY) {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07002807 return mOccupied.isRegionVacant(x, y, spanX, spanY);
Sunny Goyal9ca9c132015-04-29 14:57:22 -07002808 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002809}