blob: cac6c065a9dd93af778610bf55f9ddba96581ad2 [file] [log] [blame]
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001/*
2 * Copyright (C) 2008 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Daniel Sandler325dc232013-06-05 22:57:57 -040017package com.android.launcher3;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080018
Joe Onorato4be866d2010-10-10 11:26:02 -070019import android.animation.Animator;
Michael Jurka629758f2012-06-14 16:18:21 -070020import android.animation.AnimatorListenerAdapter;
Chet Haase00397b12010-10-07 11:13:10 -070021import android.animation.TimeInterpolator;
Patrick Dubroyde7658b2010-09-27 11:15:43 -070022import android.animation.ValueAnimator;
23import android.animation.ValueAnimator.AnimatorUpdateListener;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080024import android.content.Context;
Joe Onorato79e56262009-09-21 15:23:04 -040025import android.content.res.Resources;
Sunny Goyalc13403c2016-11-18 23:44:48 -080026import android.content.res.TypedArray;
Joe Onorato4be866d2010-10-10 11:26:02 -070027import android.graphics.Bitmap;
Winson Chungaafa03c2010-06-11 17:34:16 -070028import android.graphics.Canvas;
Andrew Flynn0dca1ec2012-02-29 13:33:22 -080029import android.graphics.Color;
Joe Onorato4be866d2010-10-10 11:26:02 -070030import android.graphics.Paint;
Patrick Dubroyde7658b2010-09-27 11:15:43 -070031import android.graphics.Point;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080032import android.graphics.Rect;
Adam Cohen482ed822012-03-02 14:15:13 -080033import android.graphics.drawable.ColorDrawable;
Patrick Dubroy6569f2c2010-07-12 14:25:18 -070034import android.graphics.drawable.Drawable;
Adam Cohen1462de32012-07-24 22:34:36 -070035import android.os.Parcelable;
Sunny Goyalc13403c2016-11-18 23:44:48 -080036import android.support.annotation.IntDef;
Adam Cohenc9735cf2015-01-23 16:11:55 -080037import android.support.v4.view.ViewCompat;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080038import android.util.AttributeSet;
Joe Onorato4be866d2010-10-10 11:26:02 -070039import android.util.Log;
Adam Cohen1462de32012-07-24 22:34:36 -070040import android.util.SparseArray;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080041import android.view.MotionEvent;
42import android.view.View;
43import android.view.ViewDebug;
44import android.view.ViewGroup;
Adam Cohenc9735cf2015-01-23 16:11:55 -080045import android.view.accessibility.AccessibilityEvent;
Winson Chung150fbab2010-09-29 17:14:26 -070046import android.view.animation.DecelerateInterpolator;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080047
Sunny Goyal4b6eb262015-05-14 19:24:40 -070048import com.android.launcher3.BubbleTextView.BubbleTextShadowHandler;
Sunny Goyalaa8ef112015-06-12 20:04:41 -070049import com.android.launcher3.LauncherSettings.Favorites;
Sunny Goyale9b651e2015-04-24 11:44:51 -070050import com.android.launcher3.accessibility.DragAndDropAccessibilityDelegate;
51import com.android.launcher3.accessibility.FolderAccessibilityHelper;
52import com.android.launcher3.accessibility.WorkspaceAccessibilityHelper;
Sunny Goyal9e76f682017-02-13 12:13:43 -080053import com.android.launcher3.anim.PropertyListBuilder;
Sunny Goyal6c56c682015-07-16 14:09:05 -070054import com.android.launcher3.config.ProviderConfig;
Sunny Goyal26119432016-02-18 22:09:23 +000055import com.android.launcher3.folder.FolderIcon;
Sunny Goyal06e21a22016-08-11 16:02:02 -070056import com.android.launcher3.graphics.DragPreviewProvider;
Sunny Goyalff4ba2d2016-04-02 14:12:34 -070057import com.android.launcher3.util.CellAndSpan;
58import com.android.launcher3.util.GridOccupancy;
Sunny Goyale2fd14b2015-08-27 17:45:46 -070059import com.android.launcher3.util.ParcelableSparseArray;
Sunny Goyal9b29ca52017-02-17 10:39:44 -080060import com.android.launcher3.util.Themes;
Adam Cohen091440a2015-03-18 14:16:05 -070061import com.android.launcher3.util.Thunk;
Patrick Dubroy8e58e912010-10-14 13:21:48 -070062
Sunny Goyalc13403c2016-11-18 23:44:48 -080063import java.lang.annotation.Retention;
64import java.lang.annotation.RetentionPolicy;
Adam Cohen69ce2e52011-07-03 19:25:21 -070065import java.util.ArrayList;
Adam Cohenc0dcf592011-06-01 15:30:43 -070066import java.util.Arrays;
Adam Cohenf3900c22012-11-16 18:28:11 -080067import java.util.Collections;
68import java.util.Comparator;
Adam Cohenbfbfd262011-06-13 16:55:12 -070069import java.util.HashMap;
Adam Cohend41fbf52012-02-16 23:53:59 -080070import java.util.Stack;
Adam Cohenc0dcf592011-06-01 15:30:43 -070071
Sunny Goyal4b6eb262015-05-14 19:24:40 -070072public class CellLayout extends ViewGroup implements BubbleTextShadowHandler {
Sunny Goyale9b651e2015-04-24 11:44:51 -070073 public static final int WORKSPACE_ACCESSIBILITY_DRAG = 2;
74 public static final int FOLDER_ACCESSIBILITY_DRAG = 1;
75
Tony Wickhama0628cc2015-10-14 15:23:04 -070076 private static final String TAG = "CellLayout";
77 private static final boolean LOGD = false;
Winson Chungaafa03c2010-06-11 17:34:16 -070078
Adam Cohen2acce882012-03-28 19:03:19 -070079 private Launcher mLauncher;
Sunny Goyal4ffec482016-02-09 11:28:52 -080080 @ViewDebug.ExportedProperty(category = "launcher")
Adam Cohen091440a2015-03-18 14:16:05 -070081 @Thunk int mCellWidth;
Sunny Goyal4ffec482016-02-09 11:28:52 -080082 @ViewDebug.ExportedProperty(category = "launcher")
Adam Cohen091440a2015-03-18 14:16:05 -070083 @Thunk int mCellHeight;
Winson Chung11a1a532013-09-13 11:14:45 -070084 private int mFixedCellWidth;
85 private int mFixedCellHeight;
Winson Chungaafa03c2010-06-11 17:34:16 -070086
Sunny Goyal4ffec482016-02-09 11:28:52 -080087 @ViewDebug.ExportedProperty(category = "launcher")
Sunny Goyalff4ba2d2016-04-02 14:12:34 -070088 private int mCountX;
Sunny Goyal4ffec482016-02-09 11:28:52 -080089 @ViewDebug.ExportedProperty(category = "launcher")
Sunny Goyalff4ba2d2016-04-02 14:12:34 -070090 private int mCountY;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080091
Adam Cohen917e3882013-10-31 15:03:35 -070092 private boolean mDropPending = false;
Adam Cohenc50438c2014-08-19 17:43:05 -070093 private boolean mIsDragTarget = true;
Sunny Goyale2fd14b2015-08-27 17:45:46 -070094 private boolean mJailContent = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080095
Patrick Dubroyde7658b2010-09-27 11:15:43 -070096 // These are temporary variables to prevent having to allocate a new object just to
97 // return an (x, y) value from helper functions. Do NOT use them to maintain other state.
Adam Cohen091440a2015-03-18 14:16:05 -070098 @Thunk final int[] mTmpPoint = new int[2];
Sunny Goyal2805e632015-05-20 15:35:32 -070099 @Thunk final int[] mTempLocation = new int[2];
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700100
Sunny Goyalff4ba2d2016-04-02 14:12:34 -0700101 private GridOccupancy mOccupied;
102 private GridOccupancy mTmpOccupied;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800103
Michael Jurkadee05892010-07-27 10:01:56 -0700104 private OnTouchListener mInterceptTouchListener;
Mady Melloref044dd2015-06-02 15:35:07 -0700105 private StylusEventHelper mStylusEventHelper;
Michael Jurkadee05892010-07-27 10:01:56 -0700106
Adam Cohenefca0272016-02-24 19:19:06 -0800107 private ArrayList<FolderIcon.PreviewBackground> mFolderBackgrounds = new ArrayList<FolderIcon.PreviewBackground>();
108 FolderIcon.PreviewBackground mFolderLeaveBehind = new FolderIcon.PreviewBackground();
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 Goyal9b29ca52017-02-17 10:39:44 -0800240 mDragOutlinePaint.setColor(Themes.getAttrColor(context, android.R.attr.textColorPrimary));
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
Sunny Goyale9b651e2015-04-24 11:44:51 -0700300 public void enableAccessibleDrag(boolean enable, int dragType) {
Adam Cohenc9735cf2015-01-23 16:11:55 -0800301 mUseTouchHelper = enable;
302 if (!enable) {
303 ViewCompat.setAccessibilityDelegate(this, null);
304 setImportantForAccessibility(IMPORTANT_FOR_ACCESSIBILITY_NO);
305 getShortcutsAndWidgets().setImportantForAccessibility(IMPORTANT_FOR_ACCESSIBILITY_NO);
306 setOnClickListener(mLauncher);
307 } else {
Sunny Goyale9b651e2015-04-24 11:44:51 -0700308 if (dragType == WORKSPACE_ACCESSIBILITY_DRAG &&
309 !(mTouchHelper instanceof WorkspaceAccessibilityHelper)) {
310 mTouchHelper = new WorkspaceAccessibilityHelper(this);
311 } else if (dragType == FOLDER_ACCESSIBILITY_DRAG &&
312 !(mTouchHelper instanceof FolderAccessibilityHelper)) {
313 mTouchHelper = new FolderAccessibilityHelper(this);
314 }
Adam Cohenc9735cf2015-01-23 16:11:55 -0800315 ViewCompat.setAccessibilityDelegate(this, mTouchHelper);
316 setImportantForAccessibility(IMPORTANT_FOR_ACCESSIBILITY_YES);
317 getShortcutsAndWidgets().setImportantForAccessibility(IMPORTANT_FOR_ACCESSIBILITY_YES);
318 setOnClickListener(mTouchHelper);
319 }
320
321 // Invalidate the accessibility hierarchy
322 if (getParent() != null) {
323 getParent().notifySubtreeAccessibilityStateChanged(
324 this, this, AccessibilityEvent.CONTENT_CHANGE_TYPE_SUBTREE);
325 }
326 }
327
328 @Override
329 public boolean dispatchHoverEvent(MotionEvent event) {
330 // Always attempt to dispatch hover events to accessibility first.
331 if (mUseTouchHelper && mTouchHelper.dispatchHoverEvent(event)) {
332 return true;
333 }
334 return super.dispatchHoverEvent(event);
335 }
336
337 @Override
Adam Cohenc9735cf2015-01-23 16:11:55 -0800338 public boolean onInterceptTouchEvent(MotionEvent ev) {
339 if (mUseTouchHelper ||
340 (mInterceptTouchListener != null && mInterceptTouchListener.onTouch(this, ev))) {
341 return true;
342 }
343 return false;
344 }
345
Mady Melloref044dd2015-06-02 15:35:07 -0700346 @Override
347 public boolean onTouchEvent(MotionEvent ev) {
348 boolean handled = super.onTouchEvent(ev);
349 // Stylus button press on a home screen should not switch between overview mode and
350 // the home screen mode, however, once in overview mode stylus button press should be
351 // enabled to allow rearranging the different home screens. So check what mode
352 // the workspace is in, and only perform stylus button presses while in overview mode.
353 if (mLauncher.mWorkspace.isInOverviewMode()
Mady Mellorbb835202015-07-15 16:34:34 -0700354 && mStylusEventHelper.onMotionEvent(ev)) {
Mady Melloref044dd2015-06-02 15:35:07 -0700355 return true;
356 }
357 return handled;
358 }
359
Chris Craik01f2d7f2013-10-01 14:41:56 -0700360 public void enableHardwareLayer(boolean hasLayer) {
361 mShortcutsAndWidgets.setLayerType(hasLayer ? LAYER_TYPE_HARDWARE : LAYER_TYPE_NONE, sPaint);
Michael Jurkad51f33a2012-06-28 15:35:26 -0700362 }
363
364 public void buildHardwareLayer() {
365 mShortcutsAndWidgets.buildLayer();
Adam Cohen2801caf2011-05-13 20:57:39 -0700366 }
367
Winson Chung5f8afe62013-08-12 16:19:28 -0700368 public void setCellDimensions(int width, int height) {
Winson Chung11a1a532013-09-13 11:14:45 -0700369 mFixedCellWidth = mCellWidth = width;
370 mFixedCellHeight = mCellHeight = height;
Sunny Goyalaa8a8712016-11-20 15:26:01 +0530371 mShortcutsAndWidgets.setCellDimensions(mCellWidth, mCellHeight, mCountX, mCountY);
Winson Chung5f8afe62013-08-12 16:19:28 -0700372 }
373
Adam Cohen2801caf2011-05-13 20:57:39 -0700374 public void setGridSize(int x, int y) {
375 mCountX = x;
376 mCountY = y;
Sunny Goyalff4ba2d2016-04-02 14:12:34 -0700377 mOccupied = new GridOccupancy(mCountX, mCountY);
378 mTmpOccupied = new GridOccupancy(mCountX, mCountY);
Adam Cohen7fbec102012-03-27 12:42:19 -0700379 mTempRectStack.clear();
Sunny Goyalaa8a8712016-11-20 15:26:01 +0530380 mShortcutsAndWidgets.setCellDimensions(mCellWidth, mCellHeight, mCountX, mCountY);
Adam Cohen76fc0852011-06-17 13:26:23 -0700381 requestLayout();
Adam Cohen2801caf2011-05-13 20:57:39 -0700382 }
383
Adam Cohen2374abf2013-04-16 14:56:57 -0700384 // Set whether or not to invert the layout horizontally if the layout is in RTL mode.
385 public void setInvertIfRtl(boolean invert) {
386 mShortcutsAndWidgets.setInvertIfRtl(invert);
387 }
388
Adam Cohen917e3882013-10-31 15:03:35 -0700389 public void setDropPending(boolean pending) {
390 mDropPending = pending;
391 }
392
393 public boolean isDropPending() {
394 return mDropPending;
395 }
396
Sunny Goyal4b6eb262015-05-14 19:24:40 -0700397 @Override
398 public void setPressedIcon(BubbleTextView icon, Bitmap background) {
Sunny Goyal508da152014-08-14 10:53:27 -0700399 if (icon == null || background == null) {
400 mTouchFeedbackView.setBitmap(null);
401 mTouchFeedbackView.animate().cancel();
402 } else {
Sunny Goyal508da152014-08-14 10:53:27 -0700403 if (mTouchFeedbackView.setBitmap(background)) {
Winsonbe9798b2016-07-20 12:55:49 -0700404 mTouchFeedbackView.alignWithIconView(icon, mShortcutsAndWidgets,
405 null /* clipAgainstView */);
Sunny Goyal4fe5a372015-05-14 19:55:10 -0700406 mTouchFeedbackView.animateShadow();
Sunny Goyal508da152014-08-14 10:53:27 -0700407 }
Patrick Dubroy96864c32011-03-10 17:17:23 -0800408 }
409 }
410
Adam Cohenc50438c2014-08-19 17:43:05 -0700411 void disableDragTarget() {
412 mIsDragTarget = false;
413 }
414
Tony Wickham0f97b782015-12-02 17:55:07 -0800415 public boolean isDragTarget() {
416 return mIsDragTarget;
417 }
418
Adam Cohenc50438c2014-08-19 17:43:05 -0700419 void setIsDragOverlapping(boolean isDragOverlapping) {
420 if (mIsDragOverlapping != isDragOverlapping) {
421 mIsDragOverlapping = isDragOverlapping;
Sunny Goyalf5440cb2016-12-14 15:13:00 -0800422 mBackground.setState(mIsDragOverlapping
423 ? BACKGROUND_STATE_ACTIVE : BACKGROUND_STATE_DEFAULT);
Adam Cohenc50438c2014-08-19 17:43:05 -0700424 invalidate();
425 }
426 }
427
Sunny Goyale2fd14b2015-08-27 17:45:46 -0700428 public void disableJailContent() {
429 mJailContent = false;
430 }
431
432 @Override
433 protected void dispatchSaveInstanceState(SparseArray<Parcelable> container) {
434 if (mJailContent) {
435 ParcelableSparseArray jail = getJailedArray(container);
436 super.dispatchSaveInstanceState(jail);
437 container.put(R.id.cell_layout_jail_id, jail);
438 } else {
439 super.dispatchSaveInstanceState(container);
440 }
441 }
442
443 @Override
444 protected void dispatchRestoreInstanceState(SparseArray<Parcelable> container) {
445 super.dispatchRestoreInstanceState(mJailContent ? getJailedArray(container) : container);
446 }
447
448 private ParcelableSparseArray getJailedArray(SparseArray<Parcelable> container) {
449 final Parcelable parcelable = container.get(R.id.cell_layout_jail_id);
450 return parcelable instanceof ParcelableSparseArray ?
451 (ParcelableSparseArray) parcelable : new ParcelableSparseArray();
452 }
453
Tony Wickham0f97b782015-12-02 17:55:07 -0800454 public boolean getIsDragOverlapping() {
455 return mIsDragOverlapping;
456 }
457
Jeff Sharkey83f111d2009-04-20 21:03:13 -0700458 @Override
Patrick Dubroy1262e362010-10-06 15:49:50 -0700459 protected void onDraw(Canvas canvas) {
Sunny Goyal05739772015-05-19 19:59:09 -0700460 if (!mIsDragTarget) {
461 return;
462 }
463
Michael Jurka3e7c7632010-10-02 16:01:03 -0700464 // When we're large, we are either drawn in a "hover" state (ie when dragging an item to
465 // a neighboring page) or with just a normal background (if backgroundAlpha > 0.0f)
466 // When we're small, we are either drawn normally or in the "accepts drops" state (during
467 // a drag). However, we also drag the mini hover background *over* one of those two
468 // backgrounds
Sunny Goyal05739772015-05-19 19:59:09 -0700469 if (mBackgroundAlpha > 0.0f) {
Sunny Goyal2805e632015-05-20 15:35:32 -0700470 mBackground.draw(canvas);
Michael Jurkaa63c4522010-08-19 13:52:27 -0700471 }
Romain Guya6abce82009-11-10 02:54:41 -0800472
Patrick Dubroy8e58e912010-10-14 13:21:48 -0700473 final Paint paint = mDragOutlinePaint;
Joe Onorato4be866d2010-10-10 11:26:02 -0700474 for (int i = 0; i < mDragOutlines.length; i++) {
Chet Haase472b2812010-10-14 07:02:04 -0700475 final float alpha = mDragOutlineAlphas[i];
Joe Onorato4be866d2010-10-10 11:26:02 -0700476 if (alpha > 0) {
Joe Onorato4be866d2010-10-10 11:26:02 -0700477 final Bitmap b = (Bitmap) mDragOutlineAnims[i].getTag();
Chet Haase472b2812010-10-14 07:02:04 -0700478 paint.setAlpha((int)(alpha + .5f));
Sunny Goyal106bf642015-07-16 12:18:06 -0700479 canvas.drawBitmap(b, null, mDragOutlines[i], paint);
Winson Chung150fbab2010-09-29 17:14:26 -0700480 }
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700481 }
Patrick Dubroy96864c32011-03-10 17:17:23 -0800482
Adam Cohen482ed822012-03-02 14:15:13 -0800483 if (DEBUG_VISUALIZE_OCCUPIED) {
484 int[] pt = new int[2];
485 ColorDrawable cd = new ColorDrawable(Color.RED);
Adam Cohene7587d22012-05-24 18:50:02 -0700486 cd.setBounds(0, 0, mCellWidth, mCellHeight);
Adam Cohen482ed822012-03-02 14:15:13 -0800487 for (int i = 0; i < mCountX; i++) {
488 for (int j = 0; j < mCountY; j++) {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -0700489 if (mOccupied.cells[i][j]) {
Adam Cohen482ed822012-03-02 14:15:13 -0800490 cellToPoint(i, j, pt);
491 canvas.save();
492 canvas.translate(pt[0], pt[1]);
493 cd.draw(canvas);
494 canvas.restore();
495 }
496 }
497 }
498 }
499
Adam Cohenefca0272016-02-24 19:19:06 -0800500 for (int i = 0; i < mFolderBackgrounds.size(); i++) {
501 FolderIcon.PreviewBackground bg = mFolderBackgrounds.get(i);
502 cellToPoint(bg.delegateCellX, bg.delegateCellY, mTempLocation);
503 canvas.save();
504 canvas.translate(mTempLocation[0], mTempLocation[1]);
505 bg.drawBackground(canvas, mFolderBgPaint);
Adam Cohenf172b742016-03-30 19:28:34 -0700506 if (!bg.isClipping) {
507 bg.drawBackgroundStroke(canvas, mFolderBgPaint);
508 }
Adam Cohenefca0272016-02-24 19:19:06 -0800509 canvas.restore();
Adam Cohen69ce2e52011-07-03 19:25:21 -0700510 }
Adam Cohenc51934b2011-07-26 21:07:43 -0700511
Adam Cohenefca0272016-02-24 19:19:06 -0800512 if (mFolderLeaveBehind.delegateCellX >= 0 && mFolderLeaveBehind.delegateCellY >= 0) {
513 cellToPoint(mFolderLeaveBehind.delegateCellX,
514 mFolderLeaveBehind.delegateCellY, mTempLocation);
515 canvas.save();
516 canvas.translate(mTempLocation[0], mTempLocation[1]);
517 mFolderLeaveBehind.drawLeaveBehind(canvas, mFolderBgPaint);
518 canvas.restore();
Adam Cohenc51934b2011-07-26 21:07:43 -0700519 }
Adam Cohen69ce2e52011-07-03 19:25:21 -0700520 }
521
Adam Cohenefca0272016-02-24 19:19:06 -0800522 @Override
523 protected void dispatchDraw(Canvas canvas) {
524 super.dispatchDraw(canvas);
525
526 for (int i = 0; i < mFolderBackgrounds.size(); i++) {
527 FolderIcon.PreviewBackground bg = mFolderBackgrounds.get(i);
Adam Cohenf172b742016-03-30 19:28:34 -0700528 if (bg.isClipping) {
529 cellToPoint(bg.delegateCellX, bg.delegateCellY, mTempLocation);
530 canvas.save();
531 canvas.translate(mTempLocation[0], mTempLocation[1]);
532 bg.drawBackgroundStroke(canvas, mFolderBgPaint);
533 canvas.restore();
534 }
Adam Cohenefca0272016-02-24 19:19:06 -0800535 }
Adam Cohen69ce2e52011-07-03 19:25:21 -0700536 }
537
Adam Cohenefca0272016-02-24 19:19:06 -0800538 public void addFolderBackground(FolderIcon.PreviewBackground bg) {
539 mFolderBackgrounds.add(bg);
540 }
541 public void removeFolderBackground(FolderIcon.PreviewBackground bg) {
542 mFolderBackgrounds.remove(bg);
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700543 }
544
Adam Cohenc51934b2011-07-26 21:07:43 -0700545 public void setFolderLeaveBehindCell(int x, int y) {
Adam Cohenefca0272016-02-24 19:19:06 -0800546
547 DeviceProfile grid = mLauncher.getDeviceProfile();
548 View child = getChildAt(x, y);
549
550 mFolderLeaveBehind.setup(getResources().getDisplayMetrics(), grid, null,
551 child.getMeasuredWidth(), child.getPaddingTop());
552
553 mFolderLeaveBehind.delegateCellX = x;
554 mFolderLeaveBehind.delegateCellY = y;
Adam Cohenc51934b2011-07-26 21:07:43 -0700555 invalidate();
556 }
557
558 public void clearFolderLeaveBehind() {
Adam Cohenefca0272016-02-24 19:19:06 -0800559 mFolderLeaveBehind.delegateCellX = -1;
560 mFolderLeaveBehind.delegateCellY = -1;
Adam Cohenc51934b2011-07-26 21:07:43 -0700561 invalidate();
562 }
563
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700564 @Override
Michael Jurkae6235dd2011-10-04 15:02:05 -0700565 public boolean shouldDelayChildPressedState() {
566 return false;
567 }
568
Adam Cohen1462de32012-07-24 22:34:36 -0700569 public void restoreInstanceState(SparseArray<Parcelable> states) {
Sunny Goyal33a152f2014-07-22 12:13:14 -0700570 try {
571 dispatchRestoreInstanceState(states);
572 } catch (IllegalArgumentException ex) {
Sunny Goyal6c56c682015-07-16 14:09:05 -0700573 if (ProviderConfig.IS_DOGFOOD_BUILD) {
Sunny Goyal33a152f2014-07-22 12:13:14 -0700574 throw ex;
575 }
576 // Mismatched viewId / viewType preventing restore. Skip restore on production builds.
577 Log.e(TAG, "Ignoring an error while restoring a view instance state", ex);
578 }
Adam Cohen1462de32012-07-24 22:34:36 -0700579 }
580
Michael Jurkae6235dd2011-10-04 15:02:05 -0700581 @Override
Jeff Sharkey83f111d2009-04-20 21:03:13 -0700582 public void cancelLongPress() {
583 super.cancelLongPress();
584
585 // Cancel long press for all children
586 final int count = getChildCount();
587 for (int i = 0; i < count; i++) {
588 final View child = getChildAt(i);
589 child.cancelLongPress();
590 }
591 }
592
Michael Jurkadee05892010-07-27 10:01:56 -0700593 public void setOnInterceptTouchListener(View.OnTouchListener listener) {
594 mInterceptTouchListener = listener;
595 }
596
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800597 public int getCountX() {
Adam Cohend22015c2010-07-26 22:02:18 -0700598 return mCountX;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800599 }
600
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800601 public int getCountY() {
Adam Cohend22015c2010-07-26 22:02:18 -0700602 return mCountY;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800603 }
604
Sunny Goyalc13403c2016-11-18 23:44:48 -0800605 public boolean acceptsWidget() {
606 return mContainerType == WORKSPACE;
Sunny Goyale9b651e2015-04-24 11:44:51 -0700607 }
608
Andrew Flynn0dca1ec2012-02-29 13:33:22 -0800609 public boolean addViewToCellLayout(View child, int index, int childId, LayoutParams params,
Sunny Goyalf7a29e82015-04-24 15:20:43 -0700610 boolean markCells) {
Winson Chungaafa03c2010-06-11 17:34:16 -0700611 final LayoutParams lp = params;
612
Andrew Flynnde38e422012-05-08 11:22:15 -0700613 // Hotseat icons - remove text
Andrew Flynn0dca1ec2012-02-29 13:33:22 -0800614 if (child instanceof BubbleTextView) {
615 BubbleTextView bubbleChild = (BubbleTextView) child;
Sunny Goyalc13403c2016-11-18 23:44:48 -0800616 bubbleChild.setTextVisibility(mContainerType != HOTSEAT);
Andrew Flynn0dca1ec2012-02-29 13:33:22 -0800617 }
618
Sunny Goyalc13403c2016-11-18 23:44:48 -0800619 child.setScaleX(mChildScale);
620 child.setScaleY(mChildScale);
Adam Cohen307fe232012-08-16 17:55:58 -0700621
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800622 // Generate an id for each view, this assumes we have at most 256x256 cells
623 // per workspace screen
Adam Cohend22015c2010-07-26 22:02:18 -0700624 if (lp.cellX >= 0 && lp.cellX <= mCountX - 1 && lp.cellY >= 0 && lp.cellY <= mCountY - 1) {
Winson Chungaafa03c2010-06-11 17:34:16 -0700625 // If the horizontal or vertical span is set to -1, it is taken to
626 // mean that it spans the extent of the CellLayout
Adam Cohend22015c2010-07-26 22:02:18 -0700627 if (lp.cellHSpan < 0) lp.cellHSpan = mCountX;
628 if (lp.cellVSpan < 0) lp.cellVSpan = mCountY;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800629
Winson Chungaafa03c2010-06-11 17:34:16 -0700630 child.setId(childId);
Tony Wickhama0628cc2015-10-14 15:23:04 -0700631 if (LOGD) {
632 Log.d(TAG, "Adding view to ShortcutsAndWidgetsContainer: " + child);
633 }
Sunny Goyalf7a29e82015-04-24 15:20:43 -0700634 mShortcutsAndWidgets.addView(child, index, lp);
Michael Jurkadee05892010-07-27 10:01:56 -0700635
Michael Jurkaf3ca3ab2010-10-20 17:08:24 -0700636 if (markCells) markCellsAsOccupiedForView(child);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700637
Winson Chungaafa03c2010-06-11 17:34:16 -0700638 return true;
639 }
640 return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800641 }
Michael Jurka3e7c7632010-10-02 16:01:03 -0700642
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800643 @Override
Michael Jurka0280c3b2010-09-17 15:00:07 -0700644 public void removeAllViews() {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -0700645 mOccupied.clear();
Michael Jurkaa52570f2012-03-20 03:18:20 -0700646 mShortcutsAndWidgets.removeAllViews();
Michael Jurka0280c3b2010-09-17 15:00:07 -0700647 }
648
649 @Override
650 public void removeAllViewsInLayout() {
Michael Jurkaa52570f2012-03-20 03:18:20 -0700651 if (mShortcutsAndWidgets.getChildCount() > 0) {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -0700652 mOccupied.clear();
Michael Jurkaa52570f2012-03-20 03:18:20 -0700653 mShortcutsAndWidgets.removeAllViewsInLayout();
Michael Jurka7cfc2822011-08-02 20:19:24 -0700654 }
Michael Jurka0280c3b2010-09-17 15:00:07 -0700655 }
656
657 @Override
658 public void removeView(View view) {
659 markCellsAsUnoccupiedForView(view);
Michael Jurkaa52570f2012-03-20 03:18:20 -0700660 mShortcutsAndWidgets.removeView(view);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700661 }
662
663 @Override
664 public void removeViewAt(int index) {
Michael Jurkaa52570f2012-03-20 03:18:20 -0700665 markCellsAsUnoccupiedForView(mShortcutsAndWidgets.getChildAt(index));
666 mShortcutsAndWidgets.removeViewAt(index);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700667 }
668
669 @Override
670 public void removeViewInLayout(View view) {
671 markCellsAsUnoccupiedForView(view);
Michael Jurkaa52570f2012-03-20 03:18:20 -0700672 mShortcutsAndWidgets.removeViewInLayout(view);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700673 }
674
675 @Override
676 public void removeViews(int start, int count) {
677 for (int i = start; i < start + count; i++) {
Michael Jurkaa52570f2012-03-20 03:18:20 -0700678 markCellsAsUnoccupiedForView(mShortcutsAndWidgets.getChildAt(i));
Michael Jurka0280c3b2010-09-17 15:00:07 -0700679 }
Michael Jurkaa52570f2012-03-20 03:18:20 -0700680 mShortcutsAndWidgets.removeViews(start, count);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700681 }
682
683 @Override
684 public void removeViewsInLayout(int start, int count) {
685 for (int i = start; i < start + count; i++) {
Michael Jurkaa52570f2012-03-20 03:18:20 -0700686 markCellsAsUnoccupiedForView(mShortcutsAndWidgets.getChildAt(i));
Michael Jurka0280c3b2010-09-17 15:00:07 -0700687 }
Michael Jurkaa52570f2012-03-20 03:18:20 -0700688 mShortcutsAndWidgets.removeViewsInLayout(start, count);
Michael Jurkaabded662011-03-04 12:06:57 -0800689 }
690
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700691 /**
Winson Chungaafa03c2010-06-11 17:34:16 -0700692 * Given a point, return the cell that strictly encloses that point
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800693 * @param x X coordinate of the point
694 * @param y Y coordinate of the point
695 * @param result Array of 2 ints to hold the x and y coordinate of the cell
696 */
Sunny Goyale9b651e2015-04-24 11:44:51 -0700697 public void pointToCellExact(int x, int y, int[] result) {
Winson Chung4b825dcd2011-06-19 12:41:22 -0700698 final int hStartPadding = getPaddingLeft();
699 final int vStartPadding = getPaddingTop();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800700
Sunny Goyalaa8a8712016-11-20 15:26:01 +0530701 result[0] = (x - hStartPadding) / mCellWidth;
702 result[1] = (y - vStartPadding) / mCellHeight;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800703
Adam Cohend22015c2010-07-26 22:02:18 -0700704 final int xAxis = mCountX;
705 final int yAxis = mCountY;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800706
707 if (result[0] < 0) result[0] = 0;
708 if (result[0] >= xAxis) result[0] = xAxis - 1;
709 if (result[1] < 0) result[1] = 0;
710 if (result[1] >= yAxis) result[1] = yAxis - 1;
711 }
Winson Chungaafa03c2010-06-11 17:34:16 -0700712
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800713 /**
714 * Given a point, return the cell that most closely encloses that point
715 * @param x X coordinate of the point
716 * @param y Y coordinate of the point
717 * @param result Array of 2 ints to hold the x and y coordinate of the cell
718 */
719 void pointToCellRounded(int x, int y, int[] result) {
720 pointToCellExact(x + (mCellWidth / 2), y + (mCellHeight / 2), result);
721 }
722
723 /**
724 * Given a cell coordinate, return the point that represents the upper left corner of that cell
Winson Chungaafa03c2010-06-11 17:34:16 -0700725 *
726 * @param cellX X coordinate of the cell
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800727 * @param cellY Y coordinate of the cell
Winson Chungaafa03c2010-06-11 17:34:16 -0700728 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800729 * @param result Array of 2 ints to hold the x and y coordinate of the point
730 */
731 void cellToPoint(int cellX, int cellY, int[] result) {
Winson Chung4b825dcd2011-06-19 12:41:22 -0700732 final int hStartPadding = getPaddingLeft();
733 final int vStartPadding = getPaddingTop();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800734
Sunny Goyalaa8a8712016-11-20 15:26:01 +0530735 result[0] = hStartPadding + cellX * mCellWidth;
736 result[1] = vStartPadding + cellY * mCellHeight;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800737 }
738
Adam Cohene3e27a82011-04-15 12:07:39 -0700739 /**
Adam Cohen482ed822012-03-02 14:15:13 -0800740 * Given a cell coordinate, return the point that represents the center of the cell
Adam Cohene3e27a82011-04-15 12:07:39 -0700741 *
742 * @param cellX X coordinate of the cell
743 * @param cellY Y coordinate of the cell
744 *
745 * @param result Array of 2 ints to hold the x and y coordinate of the point
746 */
747 void cellToCenterPoint(int cellX, int cellY, int[] result) {
Adam Cohen47a876d2012-03-19 13:21:41 -0700748 regionToCenterPoint(cellX, cellY, 1, 1, result);
749 }
750
751 /**
752 * Given a cell coordinate and span return the point that represents the center of the regio
753 *
754 * @param cellX X coordinate of the cell
755 * @param cellY Y coordinate of the cell
756 *
757 * @param result Array of 2 ints to hold the x and y coordinate of the point
758 */
759 void regionToCenterPoint(int cellX, int cellY, int spanX, int spanY, int[] result) {
Winson Chung4b825dcd2011-06-19 12:41:22 -0700760 final int hStartPadding = getPaddingLeft();
761 final int vStartPadding = getPaddingTop();
Sunny Goyalaa8a8712016-11-20 15:26:01 +0530762 result[0] = hStartPadding + cellX * mCellWidth + (spanX * mCellWidth) / 2;
763 result[1] = vStartPadding + cellY * mCellHeight + (spanY * mCellHeight) / 2;
Adam Cohene3e27a82011-04-15 12:07:39 -0700764 }
765
Adam Cohen19f37922012-03-21 11:59:11 -0700766 /**
767 * Given a cell coordinate and span fills out a corresponding pixel rect
768 *
769 * @param cellX X coordinate of the cell
770 * @param cellY Y coordinate of the cell
771 * @param result Rect in which to write the result
772 */
773 void regionToRect(int cellX, int cellY, int spanX, int spanY, Rect result) {
774 final int hStartPadding = getPaddingLeft();
775 final int vStartPadding = getPaddingTop();
Sunny Goyalaa8a8712016-11-20 15:26:01 +0530776 final int left = hStartPadding + cellX * mCellWidth;
777 final int top = vStartPadding + cellY * mCellHeight;
778 result.set(left, top, left + (spanX * mCellWidth), top + (spanY * mCellHeight));
Adam Cohen19f37922012-03-21 11:59:11 -0700779 }
780
Adam Cohen482ed822012-03-02 14:15:13 -0800781 public float getDistanceFromCell(float x, float y, int[] cell) {
782 cellToCenterPoint(cell[0], cell[1], mTmpPoint);
Sunny Goyalf7a29e82015-04-24 15:20:43 -0700783 return (float) Math.hypot(x - mTmpPoint[0], y - mTmpPoint[1]);
Adam Cohen482ed822012-03-02 14:15:13 -0800784 }
785
Adam Cohenf9c184a2016-01-15 16:47:43 -0800786 public int getCellWidth() {
Romain Guy84f296c2009-11-04 15:00:44 -0800787 return mCellWidth;
788 }
789
790 int getCellHeight() {
791 return mCellHeight;
792 }
793
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700794 public void setFixedSize(int width, int height) {
795 mFixedWidth = width;
796 mFixedHeight = height;
797 }
798
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800799 @Override
800 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800801 int widthSpecMode = MeasureSpec.getMode(widthMeasureSpec);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800802 int heightSpecMode = MeasureSpec.getMode(heightMeasureSpec);
Winson Chung5f8afe62013-08-12 16:19:28 -0700803 int widthSize = MeasureSpec.getSize(widthMeasureSpec);
804 int heightSize = MeasureSpec.getSize(heightMeasureSpec);
Winson Chung2d75f122013-09-23 16:53:31 -0700805 int childWidthSize = widthSize - (getPaddingLeft() + getPaddingRight());
806 int childHeightSize = heightSize - (getPaddingTop() + getPaddingBottom());
Winson Chung11a1a532013-09-13 11:14:45 -0700807 if (mFixedCellWidth < 0 || mFixedCellHeight < 0) {
Sunny Goyalc6205602015-05-21 20:46:33 -0700808 int cw = DeviceProfile.calculateCellWidth(childWidthSize, mCountX);
809 int ch = DeviceProfile.calculateCellHeight(childHeightSize, mCountY);
Winson Chung11a1a532013-09-13 11:14:45 -0700810 if (cw != mCellWidth || ch != mCellHeight) {
811 mCellWidth = cw;
812 mCellHeight = ch;
Sunny Goyalaa8a8712016-11-20 15:26:01 +0530813 mShortcutsAndWidgets.setCellDimensions(mCellWidth, mCellHeight, mCountX, mCountY);
Winson Chung11a1a532013-09-13 11:14:45 -0700814 }
Winson Chung5f8afe62013-08-12 16:19:28 -0700815 }
Winson Chungaafa03c2010-06-11 17:34:16 -0700816
Winson Chung2d75f122013-09-23 16:53:31 -0700817 int newWidth = childWidthSize;
818 int newHeight = childHeightSize;
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700819 if (mFixedWidth > 0 && mFixedHeight > 0) {
820 newWidth = mFixedWidth;
821 newHeight = mFixedHeight;
822 } else if (widthSpecMode == MeasureSpec.UNSPECIFIED || heightSpecMode == MeasureSpec.UNSPECIFIED) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800823 throw new RuntimeException("CellLayout cannot have UNSPECIFIED dimensions");
824 }
825
Sunny Goyal4fe5a372015-05-14 19:55:10 -0700826 // Make the feedback view large enough to hold the blur bitmap.
827 mTouchFeedbackView.measure(
828 MeasureSpec.makeMeasureSpec(mCellWidth + mTouchFeedbackView.getExtraSize(),
829 MeasureSpec.EXACTLY),
830 MeasureSpec.makeMeasureSpec(mCellHeight + mTouchFeedbackView.getExtraSize(),
831 MeasureSpec.EXACTLY));
832
833 mShortcutsAndWidgets.measure(
834 MeasureSpec.makeMeasureSpec(newWidth, MeasureSpec.EXACTLY),
835 MeasureSpec.makeMeasureSpec(newHeight, MeasureSpec.EXACTLY));
836
837 int maxWidth = mShortcutsAndWidgets.getMeasuredWidth();
838 int maxHeight = mShortcutsAndWidgets.getMeasuredHeight();
Winson Chung2d75f122013-09-23 16:53:31 -0700839 if (mFixedWidth > 0 && mFixedHeight > 0) {
840 setMeasuredDimension(maxWidth, maxHeight);
841 } else {
842 setMeasuredDimension(widthSize, heightSize);
843 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800844 }
845
846 @Override
Michael Jurka28750fb2010-09-24 17:43:49 -0700847 protected void onLayout(boolean changed, int l, int t, int r, int b) {
Tony Wickham26b01422015-11-10 14:44:32 -0800848 boolean isFullscreen = mShortcutsAndWidgets.getChildCount() > 0 &&
849 ((LayoutParams) mShortcutsAndWidgets.getChildAt(0).getLayoutParams()).isFullscreen;
850 int left = getPaddingLeft();
851 if (!isFullscreen) {
Tony Wickhama501d492015-11-03 18:05:01 -0800852 left += (int) Math.ceil(getUnusedHorizontalSpace() / 2f);
Tony Wickham26b01422015-11-10 14:44:32 -0800853 }
Sunny Goyal7c786f72016-06-01 14:08:21 -0700854 int right = r - l - getPaddingRight();
855 if (!isFullscreen) {
856 right -= (int) Math.ceil(getUnusedHorizontalSpace() / 2f);
857 }
858
Winson Chung38848ca2013-10-08 12:03:44 -0700859 int top = getPaddingTop();
Sunny Goyal7c786f72016-06-01 14:08:21 -0700860 int bottom = b - t - getPaddingBottom();
Sunny Goyal4fe5a372015-05-14 19:55:10 -0700861
862 mTouchFeedbackView.layout(left, top,
863 left + mTouchFeedbackView.getMeasuredWidth(),
864 top + mTouchFeedbackView.getMeasuredHeight());
Sunny Goyal7c786f72016-06-01 14:08:21 -0700865 mShortcutsAndWidgets.layout(left, top, right, bottom);
866
867 // Expand the background drawing bounds by the padding baked into the background drawable
868 mBackground.getPadding(mTempRect);
869 mBackground.setBounds(
870 left - mTempRect.left,
871 top - mTempRect.top,
872 right + mTempRect.right,
873 bottom + mTempRect.bottom);
874 }
875
Tony Wickhama501d492015-11-03 18:05:01 -0800876 /**
877 * Returns the amount of space left over after subtracting padding and cells. This space will be
878 * very small, a few pixels at most, and is a result of rounding down when calculating the cell
879 * width in {@link DeviceProfile#calculateCellWidth(int, int)}.
880 */
881 public int getUnusedHorizontalSpace() {
882 return getMeasuredWidth() - getPaddingLeft() - getPaddingRight() - (mCountX * mCellWidth);
883 }
884
Michael Jurka5f1c5092010-09-03 14:15:02 -0700885 public float getBackgroundAlpha() {
886 return mBackgroundAlpha;
Michael Jurkadee05892010-07-27 10:01:56 -0700887 }
888
Michael Jurka5f1c5092010-09-03 14:15:02 -0700889 public void setBackgroundAlpha(float alpha) {
Michael Jurkaafaa0502011-12-13 18:22:50 -0800890 if (mBackgroundAlpha != alpha) {
891 mBackgroundAlpha = alpha;
Sunny Goyal2805e632015-05-20 15:35:32 -0700892 mBackground.setAlpha((int) (mBackgroundAlpha * 255));
Michael Jurkaafaa0502011-12-13 18:22:50 -0800893 }
Michael Jurkadee05892010-07-27 10:01:56 -0700894 }
895
Sunny Goyal2805e632015-05-20 15:35:32 -0700896 @Override
897 protected boolean verifyDrawable(Drawable who) {
898 return super.verifyDrawable(who) || (mIsDragTarget && who == mBackground);
899 }
900
Michael Jurkaa52570f2012-03-20 03:18:20 -0700901 public void setShortcutAndWidgetAlpha(float alpha) {
Sunny Goyal02b50812014-09-10 15:44:42 -0700902 mShortcutsAndWidgets.setAlpha(alpha);
Michael Jurkadee05892010-07-27 10:01:56 -0700903 }
904
Michael Jurkaa52570f2012-03-20 03:18:20 -0700905 public ShortcutAndWidgetContainer getShortcutsAndWidgets() {
Sunny Goyaldcbcc862014-08-12 15:58:36 -0700906 return mShortcutsAndWidgets;
Michael Jurkaa52570f2012-03-20 03:18:20 -0700907 }
908
Patrick Dubroy440c3602010-07-13 17:50:32 -0700909 public View getChildAt(int x, int y) {
Michael Jurkaa52570f2012-03-20 03:18:20 -0700910 return mShortcutsAndWidgets.getChildAt(x, y);
Patrick Dubroy440c3602010-07-13 17:50:32 -0700911 }
912
Adam Cohen76fc0852011-06-17 13:26:23 -0700913 public boolean animateChildToPosition(final View child, int cellX, int cellY, int duration,
Adam Cohen482ed822012-03-02 14:15:13 -0800914 int delay, boolean permanent, boolean adjustOccupied) {
Michael Jurkaa52570f2012-03-20 03:18:20 -0700915 ShortcutAndWidgetContainer clc = getShortcutsAndWidgets();
Adam Cohen482ed822012-03-02 14:15:13 -0800916
Adam Cohen19f37922012-03-21 11:59:11 -0700917 if (clc.indexOfChild(child) != -1) {
Adam Cohenbfbfd262011-06-13 16:55:12 -0700918 final LayoutParams lp = (LayoutParams) child.getLayoutParams();
919 final ItemInfo info = (ItemInfo) child.getTag();
920
921 // We cancel any existing animations
922 if (mReorderAnimators.containsKey(lp)) {
923 mReorderAnimators.get(lp).cancel();
924 mReorderAnimators.remove(lp);
925 }
926
Adam Cohen482ed822012-03-02 14:15:13 -0800927 final int oldX = lp.x;
928 final int oldY = lp.y;
929 if (adjustOccupied) {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -0700930 GridOccupancy occupied = permanent ? mOccupied : mTmpOccupied;
931 occupied.markCells(lp.cellX, lp.cellY, lp.cellHSpan, lp.cellVSpan, false);
932 occupied.markCells(cellX, cellY, lp.cellHSpan, lp.cellVSpan, true);
Adam Cohen482ed822012-03-02 14:15:13 -0800933 }
Adam Cohenbfbfd262011-06-13 16:55:12 -0700934 lp.isLockedToGrid = true;
Adam Cohen482ed822012-03-02 14:15:13 -0800935 if (permanent) {
936 lp.cellX = info.cellX = cellX;
937 lp.cellY = info.cellY = cellY;
938 } else {
939 lp.tmpCellX = cellX;
940 lp.tmpCellY = cellY;
941 }
Jon Mirandae96798e2016-12-07 12:10:44 -0800942 clc.setupLp(child);
Adam Cohenbfbfd262011-06-13 16:55:12 -0700943 lp.isLockedToGrid = false;
Adam Cohen482ed822012-03-02 14:15:13 -0800944 final int newX = lp.x;
945 final int newY = lp.y;
Adam Cohenbfbfd262011-06-13 16:55:12 -0700946
Adam Cohen76fc0852011-06-17 13:26:23 -0700947 lp.x = oldX;
948 lp.y = oldY;
Adam Cohen76fc0852011-06-17 13:26:23 -0700949
Adam Cohen482ed822012-03-02 14:15:13 -0800950 // Exit early if we're not actually moving the view
951 if (oldX == newX && oldY == newY) {
952 lp.isLockedToGrid = true;
953 return true;
954 }
955
Jon Mirandacda3bfb2017-02-06 15:54:41 -0800956 ValueAnimator va = LauncherAnimUtils.ofFloat(0f, 1f);
Adam Cohen482ed822012-03-02 14:15:13 -0800957 va.setDuration(duration);
958 mReorderAnimators.put(lp, va);
959
960 va.addUpdateListener(new AnimatorUpdateListener() {
961 @Override
Adam Cohenbfbfd262011-06-13 16:55:12 -0700962 public void onAnimationUpdate(ValueAnimator animation) {
Jon Mirandae96798e2016-12-07 12:10:44 -0800963 float r = (Float) animation.getAnimatedValue();
Adam Cohen19f37922012-03-21 11:59:11 -0700964 lp.x = (int) ((1 - r) * oldX + r * newX);
965 lp.y = (int) ((1 - r) * oldY + r * newY);
Adam Cohen6b8a02d2012-03-22 15:13:40 -0700966 child.requestLayout();
Adam Cohenbfbfd262011-06-13 16:55:12 -0700967 }
968 });
Adam Cohen482ed822012-03-02 14:15:13 -0800969 va.addListener(new AnimatorListenerAdapter() {
Adam Cohenbfbfd262011-06-13 16:55:12 -0700970 boolean cancelled = false;
971 public void onAnimationEnd(Animator animation) {
972 // If the animation was cancelled, it means that another animation
973 // has interrupted this one, and we don't want to lock the item into
974 // place just yet.
975 if (!cancelled) {
976 lp.isLockedToGrid = true;
Adam Cohen482ed822012-03-02 14:15:13 -0800977 child.requestLayout();
Adam Cohenbfbfd262011-06-13 16:55:12 -0700978 }
979 if (mReorderAnimators.containsKey(lp)) {
980 mReorderAnimators.remove(lp);
981 }
982 }
983 public void onAnimationCancel(Animator animation) {
984 cancelled = true;
985 }
986 });
Adam Cohen482ed822012-03-02 14:15:13 -0800987 va.setStartDelay(delay);
988 va.start();
Adam Cohenbfbfd262011-06-13 16:55:12 -0700989 return true;
990 }
991 return false;
992 }
993
Sunny Goyal06e21a22016-08-11 16:02:02 -0700994 void visualizeDropLocation(View v, DragPreviewProvider outlineProvider, int cellX, int cellY,
995 int spanX, int spanY, boolean resize, DropTarget.DragObject dragObject) {
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -0700996 final int oldDragCellX = mDragCell[0];
997 final int oldDragCellY = mDragCell[1];
Adam Cohen482ed822012-03-02 14:15:13 -0800998
Hyunyoung Song0de01172016-10-05 16:27:48 -0700999 if (outlineProvider == null || outlineProvider.generatedDragOutline == null) {
Adam Cohen2801caf2011-05-13 20:57:39 -07001000 return;
1001 }
1002
Hyunyoung Song0de01172016-10-05 16:27:48 -07001003 Bitmap dragOutline = outlineProvider.generatedDragOutline;
Adam Cohen482ed822012-03-02 14:15:13 -08001004 if (cellX != oldDragCellX || cellY != oldDragCellY) {
Sunny Goyale78e3d72015-09-24 11:23:31 -07001005 Point dragOffset = dragObject.dragView.getDragVisualizeOffset();
1006 Rect dragRegion = dragObject.dragView.getDragRegion();
1007
Adam Cohen482ed822012-03-02 14:15:13 -08001008 mDragCell[0] = cellX;
1009 mDragCell[1] = cellY;
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001010
Joe Onorato4be866d2010-10-10 11:26:02 -07001011 final int oldIndex = mDragOutlineCurrent;
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -07001012 mDragOutlineAnims[oldIndex].animateOut();
1013 mDragOutlineCurrent = (oldIndex + 1) % mDragOutlines.length;
Adam Cohend41fbf52012-02-16 23:53:59 -08001014 Rect r = mDragOutlines[mDragOutlineCurrent];
Sunny Goyal106bf642015-07-16 12:18:06 -07001015
Adam Cohend41fbf52012-02-16 23:53:59 -08001016 if (resize) {
Adam Cohen482ed822012-03-02 14:15:13 -08001017 cellToRect(cellX, cellY, spanX, spanY, r);
Jon Mirandae96798e2016-12-07 12:10:44 -08001018 if (v instanceof LauncherAppWidgetHostView) {
1019 DeviceProfile profile = mLauncher.getDeviceProfile();
Jon Miranda6f6a06a2016-12-15 11:24:18 -08001020 Utilities.shrinkRect(r, profile.appWidgetScale.x, profile.appWidgetScale.y);
Jon Mirandae96798e2016-12-07 12:10:44 -08001021 }
Sunny Goyal106bf642015-07-16 12:18:06 -07001022 } else {
1023 // Find the top left corner of the rect the object will occupy
1024 final int[] topLeft = mTmpPoint;
1025 cellToPoint(cellX, cellY, topLeft);
1026
1027 int left = topLeft[0];
1028 int top = topLeft[1];
1029
1030 if (v != null && dragOffset == null) {
1031 // When drawing the drag outline, it did not account for margin offsets
1032 // added by the view's parent.
1033 MarginLayoutParams lp = (MarginLayoutParams) v.getLayoutParams();
1034 left += lp.leftMargin;
1035 top += lp.topMargin;
1036
1037 // Offsets due to the size difference between the View and the dragOutline.
1038 // There is a size difference to account for the outer blur, which may lie
1039 // outside the bounds of the view.
Jon Mirandaf7ff3fe2016-12-05 12:04:44 -08001040 top += ((mCellHeight * spanY) - dragOutline.getHeight()) / 2;
Sunny Goyal106bf642015-07-16 12:18:06 -07001041 // We center about the x axis
Sunny Goyalaa8a8712016-11-20 15:26:01 +05301042 left += ((mCellWidth * spanX) - dragOutline.getWidth()) / 2;
Sunny Goyal106bf642015-07-16 12:18:06 -07001043 } else {
1044 if (dragOffset != null && dragRegion != null) {
1045 // Center the drag region *horizontally* in the cell and apply a drag
1046 // outline offset
Sunny Goyalaa8a8712016-11-20 15:26:01 +05301047 left += dragOffset.x + ((mCellWidth * spanX) - dragRegion.width()) / 2;
Sunny Goyal106bf642015-07-16 12:18:06 -07001048 int cHeight = getShortcutsAndWidgets().getCellContentHeight();
1049 int cellPaddingY = (int) Math.max(0, ((mCellHeight - cHeight) / 2f));
1050 top += dragOffset.y + cellPaddingY;
1051 } else {
1052 // Center the drag outline in the cell
Sunny Goyalaa8a8712016-11-20 15:26:01 +05301053 left += ((mCellWidth * spanX) - dragOutline.getWidth()) / 2;
1054 top += ((mCellHeight * spanY) - dragOutline.getHeight()) / 2;
Sunny Goyal106bf642015-07-16 12:18:06 -07001055 }
1056 }
1057 r.set(left, top, left + dragOutline.getWidth(), top + dragOutline.getHeight());
Adam Cohend41fbf52012-02-16 23:53:59 -08001058 }
Winson Chung150fbab2010-09-29 17:14:26 -07001059
Jon Miranda6f6a06a2016-12-15 11:24:18 -08001060 Utilities.scaleRectAboutCenter(r, mChildScale);
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -07001061 mDragOutlineAnims[mDragOutlineCurrent].setTag(dragOutline);
1062 mDragOutlineAnims[mDragOutlineCurrent].animateIn();
Sunny Goyale78e3d72015-09-24 11:23:31 -07001063
1064 if (dragObject.stateAnnouncer != null) {
Sunny Goyalc13403c2016-11-18 23:44:48 -08001065 dragObject.stateAnnouncer.announce(getItemMoveDescription(cellX, cellY));
Sunny Goyale78e3d72015-09-24 11:23:31 -07001066 }
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001067 }
1068 }
1069
Sunny Goyalc13403c2016-11-18 23:44:48 -08001070 public String getItemMoveDescription(int cellX, int cellY) {
1071 if (mContainerType == HOTSEAT) {
1072 return getContext().getString(R.string.move_to_hotseat_position,
1073 Math.max(cellX, cellY) + 1);
1074 } else {
1075 return getContext().getString(R.string.move_to_empty_cell,
1076 cellY + 1, cellX + 1);
1077 }
1078 }
1079
Adam Cohene0310962011-04-18 16:15:31 -07001080 public void clearDragOutlines() {
1081 final int oldIndex = mDragOutlineCurrent;
1082 mDragOutlineAnims[oldIndex].animateOut();
Adam Cohend41fbf52012-02-16 23:53:59 -08001083 mDragCell[0] = mDragCell[1] = -1;
Adam Cohene0310962011-04-18 16:15:31 -07001084 }
1085
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001086 /**
Jeff Sharkey70864282009-04-07 21:08:40 -07001087 * Find a vacant area that will fit the given bounds nearest the requested
1088 * cell location. Uses Euclidean distance to score multiple vacant areas.
Winson Chungaafa03c2010-06-11 17:34:16 -07001089 *
Romain Guy51afc022009-05-04 18:03:43 -07001090 * @param pixelX The X location at which you want to search for a vacant area.
1091 * @param pixelY The Y location at which you want to search for a vacant area.
Adam Cohend41fbf52012-02-16 23:53:59 -08001092 * @param minSpanX The minimum horizontal span required
1093 * @param minSpanY The minimum vertical span required
1094 * @param spanX Horizontal span of the object.
1095 * @param spanY Vertical span of the object.
1096 * @param result Array in which to place the result, or null (in which case a new array will
1097 * be allocated)
1098 * @return The X, Y cell of a vacant area that can contain this object,
1099 * nearest the requested location.
1100 */
1101 int[] findNearestVacantArea(int pixelX, int pixelY, int minSpanX, int minSpanY, int spanX,
1102 int spanY, int[] result, int[] resultSpan) {
Sunny Goyalf7a29e82015-04-24 15:20:43 -07001103 return findNearestArea(pixelX, pixelY, minSpanX, minSpanY, spanX, spanY, true,
Adam Cohend41fbf52012-02-16 23:53:59 -08001104 result, resultSpan);
1105 }
1106
Adam Cohend41fbf52012-02-16 23:53:59 -08001107 private final Stack<Rect> mTempRectStack = new Stack<Rect>();
1108 private void lazyInitTempRectStack() {
1109 if (mTempRectStack.isEmpty()) {
1110 for (int i = 0; i < mCountX * mCountY; i++) {
1111 mTempRectStack.push(new Rect());
1112 }
1113 }
1114 }
Adam Cohen482ed822012-03-02 14:15:13 -08001115
Adam Cohend41fbf52012-02-16 23:53:59 -08001116 private void recycleTempRects(Stack<Rect> used) {
1117 while (!used.isEmpty()) {
1118 mTempRectStack.push(used.pop());
1119 }
1120 }
1121
1122 /**
1123 * Find a vacant area that will fit the given bounds nearest the requested
1124 * cell location. Uses Euclidean distance to score multiple vacant areas.
1125 *
1126 * @param pixelX The X location at which you want to search for a vacant area.
1127 * @param pixelY The Y location at which you want to search for a vacant area.
1128 * @param minSpanX The minimum horizontal span required
1129 * @param minSpanY The minimum vertical span required
1130 * @param spanX Horizontal span of the object.
1131 * @param spanY Vertical span of the object.
1132 * @param ignoreOccupied If true, the result can be an occupied cell
1133 * @param result Array in which to place the result, or null (in which case a new array will
1134 * be allocated)
1135 * @return The X, Y cell of a vacant area that can contain this object,
1136 * nearest the requested location.
1137 */
Sunny Goyalf7a29e82015-04-24 15:20:43 -07001138 private int[] findNearestArea(int pixelX, int pixelY, int minSpanX, int minSpanY, int spanX,
1139 int spanY, boolean ignoreOccupied, int[] result, int[] resultSpan) {
Adam Cohend41fbf52012-02-16 23:53:59 -08001140 lazyInitTempRectStack();
Michael Jurkac6ee42e2010-09-30 12:04:50 -07001141
Adam Cohene3e27a82011-04-15 12:07:39 -07001142 // For items with a spanX / spanY > 1, the passed in point (pixelX, pixelY) corresponds
1143 // to the center of the item, but we are searching based on the top-left cell, so
1144 // we translate the point over to correspond to the top-left.
Sunny Goyalaa8a8712016-11-20 15:26:01 +05301145 pixelX -= mCellWidth * (spanX - 1) / 2f;
1146 pixelY -= mCellHeight * (spanY - 1) / 2f;
Adam Cohene3e27a82011-04-15 12:07:39 -07001147
Jeff Sharkey70864282009-04-07 21:08:40 -07001148 // Keep track of best-scoring drop area
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001149 final int[] bestXY = result != null ? result : new int[2];
Jeff Sharkey70864282009-04-07 21:08:40 -07001150 double bestDistance = Double.MAX_VALUE;
Adam Cohend41fbf52012-02-16 23:53:59 -08001151 final Rect bestRect = new Rect(-1, -1, -1, -1);
1152 final Stack<Rect> validRegions = new Stack<Rect>();
Winson Chungaafa03c2010-06-11 17:34:16 -07001153
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001154 final int countX = mCountX;
1155 final int countY = mCountY;
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001156
Adam Cohend41fbf52012-02-16 23:53:59 -08001157 if (minSpanX <= 0 || minSpanY <= 0 || spanX <= 0 || spanY <= 0 ||
1158 spanX < minSpanX || spanY < minSpanY) {
1159 return bestXY;
1160 }
1161
1162 for (int y = 0; y < countY - (minSpanY - 1); y++) {
Michael Jurkac28de512010-08-13 11:27:44 -07001163 inner:
Adam Cohend41fbf52012-02-16 23:53:59 -08001164 for (int x = 0; x < countX - (minSpanX - 1); x++) {
1165 int ySize = -1;
1166 int xSize = -1;
Adam Cohendf035382011-04-11 17:22:04 -07001167 if (ignoreOccupied) {
Adam Cohend41fbf52012-02-16 23:53:59 -08001168 // First, let's see if this thing fits anywhere
1169 for (int i = 0; i < minSpanX; i++) {
1170 for (int j = 0; j < minSpanY; j++) {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001171 if (mOccupied.cells[x + i][y + j]) {
Adam Cohendf035382011-04-11 17:22:04 -07001172 continue inner;
1173 }
Michael Jurkac28de512010-08-13 11:27:44 -07001174 }
1175 }
Adam Cohend41fbf52012-02-16 23:53:59 -08001176 xSize = minSpanX;
1177 ySize = minSpanY;
1178
1179 // We know that the item will fit at _some_ acceptable size, now let's see
1180 // how big we can make it. We'll alternate between incrementing x and y spans
1181 // until we hit a limit.
1182 boolean incX = true;
1183 boolean hitMaxX = xSize >= spanX;
1184 boolean hitMaxY = ySize >= spanY;
1185 while (!(hitMaxX && hitMaxY)) {
1186 if (incX && !hitMaxX) {
1187 for (int j = 0; j < ySize; j++) {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001188 if (x + xSize > countX -1 || mOccupied.cells[x + xSize][y + j]) {
Adam Cohend41fbf52012-02-16 23:53:59 -08001189 // We can't move out horizontally
1190 hitMaxX = true;
1191 }
1192 }
1193 if (!hitMaxX) {
1194 xSize++;
1195 }
1196 } else if (!hitMaxY) {
1197 for (int i = 0; i < xSize; i++) {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001198 if (y + ySize > countY - 1 || mOccupied.cells[x + i][y + ySize]) {
Adam Cohend41fbf52012-02-16 23:53:59 -08001199 // We can't move out vertically
1200 hitMaxY = true;
1201 }
1202 }
1203 if (!hitMaxY) {
1204 ySize++;
1205 }
1206 }
1207 hitMaxX |= xSize >= spanX;
1208 hitMaxY |= ySize >= spanY;
1209 incX = !incX;
1210 }
1211 incX = true;
1212 hitMaxX = xSize >= spanX;
1213 hitMaxY = ySize >= spanY;
Michael Jurkac28de512010-08-13 11:27:44 -07001214 }
Sunny Goyal2805e632015-05-20 15:35:32 -07001215 final int[] cellXY = mTmpPoint;
Adam Cohene3e27a82011-04-15 12:07:39 -07001216 cellToCenterPoint(x, y, cellXY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001217
Adam Cohend41fbf52012-02-16 23:53:59 -08001218 // We verify that the current rect is not a sub-rect of any of our previous
1219 // candidates. In this case, the current rect is disqualified in favour of the
1220 // containing rect.
1221 Rect currentRect = mTempRectStack.pop();
1222 currentRect.set(x, y, x + xSize, y + ySize);
1223 boolean contained = false;
1224 for (Rect r : validRegions) {
1225 if (r.contains(currentRect)) {
1226 contained = true;
1227 break;
1228 }
1229 }
1230 validRegions.push(currentRect);
Sunny Goyalf7a29e82015-04-24 15:20:43 -07001231 double distance = Math.hypot(cellXY[0] - pixelX, cellXY[1] - pixelY);
Adam Cohen482ed822012-03-02 14:15:13 -08001232
Adam Cohend41fbf52012-02-16 23:53:59 -08001233 if ((distance <= bestDistance && !contained) ||
1234 currentRect.contains(bestRect)) {
Michael Jurkac28de512010-08-13 11:27:44 -07001235 bestDistance = distance;
1236 bestXY[0] = x;
1237 bestXY[1] = y;
Adam Cohend41fbf52012-02-16 23:53:59 -08001238 if (resultSpan != null) {
1239 resultSpan[0] = xSize;
1240 resultSpan[1] = ySize;
1241 }
1242 bestRect.set(currentRect);
Michael Jurkac28de512010-08-13 11:27:44 -07001243 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001244 }
1245 }
1246
Adam Cohenc0dcf592011-06-01 15:30:43 -07001247 // Return -1, -1 if no suitable location found
1248 if (bestDistance == Double.MAX_VALUE) {
1249 bestXY[0] = -1;
1250 bestXY[1] = -1;
Jeff Sharkey70864282009-04-07 21:08:40 -07001251 }
Adam Cohend41fbf52012-02-16 23:53:59 -08001252 recycleTempRects(validRegions);
Adam Cohenc0dcf592011-06-01 15:30:43 -07001253 return bestXY;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001254 }
Winson Chungaafa03c2010-06-11 17:34:16 -07001255
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001256 /**
Adam Cohen482ed822012-03-02 14:15:13 -08001257 * Find a vacant area that will fit the given bounds nearest the requested
1258 * cell location, and will also weigh in a suggested direction vector of the
1259 * desired location. This method computers distance based on unit grid distances,
1260 * not pixel distances.
1261 *
Adam Cohen47a876d2012-03-19 13:21:41 -07001262 * @param cellX The X cell nearest to which you want to search for a vacant area.
1263 * @param cellY The Y cell nearest which you want to search for a vacant area.
Adam Cohen482ed822012-03-02 14:15:13 -08001264 * @param spanX Horizontal span of the object.
1265 * @param spanY Vertical span of the object.
Adam Cohen47a876d2012-03-19 13:21:41 -07001266 * @param direction The favored direction in which the views should move from x, y
Sunny Goyal9eba1fd2015-10-16 08:58:57 -07001267 * @param occupied The array which represents which cells in the CellLayout are occupied
Adam Cohen47a876d2012-03-19 13:21:41 -07001268 * @param blockOccupied The array which represents which cells in the specified block (cellX,
Winson Chung5f8afe62013-08-12 16:19:28 -07001269 * cellY, spanX, spanY) are occupied. This is used when try to move a group of views.
Adam Cohen482ed822012-03-02 14:15:13 -08001270 * @param result Array in which to place the result, or null (in which case a new array will
1271 * be allocated)
1272 * @return The X, Y cell of a vacant area that can contain this object,
1273 * nearest the requested location.
1274 */
1275 private int[] findNearestArea(int cellX, int cellY, int spanX, int spanY, int[] direction,
Adam Cohen47a876d2012-03-19 13:21:41 -07001276 boolean[][] occupied, boolean blockOccupied[][], int[] result) {
Adam Cohen482ed822012-03-02 14:15:13 -08001277 // Keep track of best-scoring drop area
1278 final int[] bestXY = result != null ? result : new int[2];
1279 float bestDistance = Float.MAX_VALUE;
1280 int bestDirectionScore = Integer.MIN_VALUE;
1281
1282 final int countX = mCountX;
1283 final int countY = mCountY;
1284
1285 for (int y = 0; y < countY - (spanY - 1); y++) {
1286 inner:
1287 for (int x = 0; x < countX - (spanX - 1); x++) {
1288 // First, let's see if this thing fits anywhere
1289 for (int i = 0; i < spanX; i++) {
1290 for (int j = 0; j < spanY; j++) {
Adam Cohen47a876d2012-03-19 13:21:41 -07001291 if (occupied[x + i][y + j] && (blockOccupied == null || blockOccupied[i][j])) {
Adam Cohen482ed822012-03-02 14:15:13 -08001292 continue inner;
1293 }
1294 }
1295 }
1296
Sunny Goyalf7a29e82015-04-24 15:20:43 -07001297 float distance = (float) Math.hypot(x - cellX, y - cellY);
Adam Cohen482ed822012-03-02 14:15:13 -08001298 int[] curDirection = mTmpPoint;
Adam Cohen47a876d2012-03-19 13:21:41 -07001299 computeDirectionVector(x - cellX, y - cellY, curDirection);
1300 // The direction score is just the dot product of the two candidate direction
1301 // and that passed in.
Adam Cohen482ed822012-03-02 14:15:13 -08001302 int curDirectionScore = direction[0] * curDirection[0] +
1303 direction[1] * curDirection[1];
Sunny Goyal8f90dcf2016-08-18 15:01:11 -07001304 if (Float.compare(distance, bestDistance) < 0 ||
1305 (Float.compare(distance, bestDistance) == 0
1306 && curDirectionScore > bestDirectionScore)) {
Adam Cohen482ed822012-03-02 14:15:13 -08001307 bestDistance = distance;
1308 bestDirectionScore = curDirectionScore;
1309 bestXY[0] = x;
1310 bestXY[1] = y;
1311 }
1312 }
1313 }
1314
1315 // Return -1, -1 if no suitable location found
1316 if (bestDistance == Float.MAX_VALUE) {
1317 bestXY[0] = -1;
1318 bestXY[1] = -1;
1319 }
1320 return bestXY;
1321 }
1322
1323 private boolean addViewToTempLocation(View v, Rect rectOccupiedByPotentialDrop,
Adam Cohen8baab352012-03-20 17:39:21 -07001324 int[] direction, ItemConfiguration currentState) {
1325 CellAndSpan c = currentState.map.get(v);
Adam Cohen482ed822012-03-02 14:15:13 -08001326 boolean success = false;
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001327 mTmpOccupied.markCells(c, false);
1328 mTmpOccupied.markCells(rectOccupiedByPotentialDrop, true);
Adam Cohen482ed822012-03-02 14:15:13 -08001329
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001330 findNearestArea(c.cellX, c.cellY, c.spanX, c.spanY, direction,
1331 mTmpOccupied.cells, null, mTempLocation);
Adam Cohen482ed822012-03-02 14:15:13 -08001332
1333 if (mTempLocation[0] >= 0 && mTempLocation[1] >= 0) {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001334 c.cellX = mTempLocation[0];
1335 c.cellY = mTempLocation[1];
Adam Cohen482ed822012-03-02 14:15:13 -08001336 success = true;
Adam Cohen482ed822012-03-02 14:15:13 -08001337 }
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001338 mTmpOccupied.markCells(c, true);
Adam Cohen482ed822012-03-02 14:15:13 -08001339 return success;
1340 }
1341
Adam Cohenf3900c22012-11-16 18:28:11 -08001342 /**
1343 * This helper class defines a cluster of views. It helps with defining complex edges
1344 * of the cluster and determining how those edges interact with other views. The edges
1345 * essentially define a fine-grained boundary around the cluster of views -- like a more
1346 * precise version of a bounding box.
1347 */
1348 private class ViewCluster {
Sunny Goyal6dc98b92016-04-03 15:25:29 -07001349 final static int LEFT = 1 << 0;
1350 final static int TOP = 1 << 1;
1351 final static int RIGHT = 1 << 2;
1352 final static int BOTTOM = 1 << 3;
Adam Cohen47a876d2012-03-19 13:21:41 -07001353
Adam Cohenf3900c22012-11-16 18:28:11 -08001354 ArrayList<View> views;
1355 ItemConfiguration config;
1356 Rect boundingRect = new Rect();
Adam Cohen47a876d2012-03-19 13:21:41 -07001357
Adam Cohenf3900c22012-11-16 18:28:11 -08001358 int[] leftEdge = new int[mCountY];
1359 int[] rightEdge = new int[mCountY];
1360 int[] topEdge = new int[mCountX];
1361 int[] bottomEdge = new int[mCountX];
Sunny Goyal6dc98b92016-04-03 15:25:29 -07001362 int dirtyEdges;
1363 boolean boundingRectDirty;
Adam Cohenf3900c22012-11-16 18:28:11 -08001364
1365 @SuppressWarnings("unchecked")
1366 public ViewCluster(ArrayList<View> views, ItemConfiguration config) {
1367 this.views = (ArrayList<View>) views.clone();
1368 this.config = config;
1369 resetEdges();
Adam Cohen47a876d2012-03-19 13:21:41 -07001370 }
1371
Adam Cohenf3900c22012-11-16 18:28:11 -08001372 void resetEdges() {
1373 for (int i = 0; i < mCountX; i++) {
1374 topEdge[i] = -1;
1375 bottomEdge[i] = -1;
1376 }
1377 for (int i = 0; i < mCountY; i++) {
1378 leftEdge[i] = -1;
1379 rightEdge[i] = -1;
1380 }
Sunny Goyal6dc98b92016-04-03 15:25:29 -07001381 dirtyEdges = LEFT | TOP | RIGHT | BOTTOM;
Adam Cohenf3900c22012-11-16 18:28:11 -08001382 boundingRectDirty = true;
1383 }
Adam Cohen47a876d2012-03-19 13:21:41 -07001384
Sunny Goyal6dc98b92016-04-03 15:25:29 -07001385 void computeEdge(int which) {
Adam Cohenf3900c22012-11-16 18:28:11 -08001386 int count = views.size();
1387 for (int i = 0; i < count; i++) {
1388 CellAndSpan cs = config.map.get(views.get(i));
1389 switch (which) {
1390 case LEFT:
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001391 int left = cs.cellX;
1392 for (int j = cs.cellY; j < cs.cellY + cs.spanY; j++) {
Sunny Goyal6dc98b92016-04-03 15:25:29 -07001393 if (left < leftEdge[j] || leftEdge[j] < 0) {
1394 leftEdge[j] = left;
Adam Cohena56dc102012-07-13 13:41:42 -07001395 }
Adam Cohen47a876d2012-03-19 13:21:41 -07001396 }
Adam Cohenf3900c22012-11-16 18:28:11 -08001397 break;
1398 case RIGHT:
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001399 int right = cs.cellX + cs.spanX;
1400 for (int j = cs.cellY; j < cs.cellY + cs.spanY; j++) {
Sunny Goyal6dc98b92016-04-03 15:25:29 -07001401 if (right > rightEdge[j]) {
1402 rightEdge[j] = right;
Adam Cohenf3900c22012-11-16 18:28:11 -08001403 }
1404 }
1405 break;
1406 case TOP:
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001407 int top = cs.cellY;
1408 for (int j = cs.cellX; j < cs.cellX + cs.spanX; j++) {
Sunny Goyal6dc98b92016-04-03 15:25:29 -07001409 if (top < topEdge[j] || topEdge[j] < 0) {
1410 topEdge[j] = top;
Adam Cohenf3900c22012-11-16 18:28:11 -08001411 }
1412 }
1413 break;
1414 case BOTTOM:
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001415 int bottom = cs.cellY + cs.spanY;
1416 for (int j = cs.cellX; j < cs.cellX + cs.spanX; j++) {
Sunny Goyal6dc98b92016-04-03 15:25:29 -07001417 if (bottom > bottomEdge[j]) {
1418 bottomEdge[j] = bottom;
Adam Cohenf3900c22012-11-16 18:28:11 -08001419 }
1420 }
1421 break;
Adam Cohen47a876d2012-03-19 13:21:41 -07001422 }
1423 }
1424 }
Adam Cohenf3900c22012-11-16 18:28:11 -08001425
1426 boolean isViewTouchingEdge(View v, int whichEdge) {
1427 CellAndSpan cs = config.map.get(v);
1428
Sunny Goyal6dc98b92016-04-03 15:25:29 -07001429 if ((dirtyEdges & whichEdge) == whichEdge) {
1430 computeEdge(whichEdge);
1431 dirtyEdges &= ~whichEdge;
1432 }
Adam Cohenf3900c22012-11-16 18:28:11 -08001433
1434 switch (whichEdge) {
1435 case LEFT:
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001436 for (int i = cs.cellY; i < cs.cellY + cs.spanY; i++) {
1437 if (leftEdge[i] == cs.cellX + cs.spanX) {
Adam Cohenf3900c22012-11-16 18:28:11 -08001438 return true;
1439 }
1440 }
1441 break;
1442 case RIGHT:
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001443 for (int i = cs.cellY; i < cs.cellY + cs.spanY; i++) {
1444 if (rightEdge[i] == cs.cellX) {
Adam Cohenf3900c22012-11-16 18:28:11 -08001445 return true;
1446 }
1447 }
1448 break;
1449 case TOP:
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001450 for (int i = cs.cellX; i < cs.cellX + cs.spanX; i++) {
1451 if (topEdge[i] == cs.cellY + cs.spanY) {
Adam Cohenf3900c22012-11-16 18:28:11 -08001452 return true;
1453 }
1454 }
1455 break;
1456 case BOTTOM:
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001457 for (int i = cs.cellX; i < cs.cellX + cs.spanX; i++) {
1458 if (bottomEdge[i] == cs.cellY) {
Adam Cohenf3900c22012-11-16 18:28:11 -08001459 return true;
1460 }
1461 }
1462 break;
1463 }
1464 return false;
1465 }
1466
1467 void shift(int whichEdge, int delta) {
1468 for (View v: views) {
1469 CellAndSpan c = config.map.get(v);
1470 switch (whichEdge) {
1471 case LEFT:
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001472 c.cellX -= delta;
Adam Cohenf3900c22012-11-16 18:28:11 -08001473 break;
1474 case RIGHT:
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001475 c.cellX += delta;
Adam Cohenf3900c22012-11-16 18:28:11 -08001476 break;
1477 case TOP:
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001478 c.cellY -= delta;
Adam Cohenf3900c22012-11-16 18:28:11 -08001479 break;
1480 case BOTTOM:
1481 default:
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001482 c.cellY += delta;
Adam Cohenf3900c22012-11-16 18:28:11 -08001483 break;
1484 }
1485 }
1486 resetEdges();
1487 }
1488
1489 public void addView(View v) {
1490 views.add(v);
1491 resetEdges();
1492 }
1493
1494 public Rect getBoundingRect() {
1495 if (boundingRectDirty) {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001496 config.getBoundingRectForViews(views, boundingRect);
Adam Cohenf3900c22012-11-16 18:28:11 -08001497 }
1498 return boundingRect;
1499 }
1500
Adam Cohenf3900c22012-11-16 18:28:11 -08001501 PositionComparator comparator = new PositionComparator();
1502 class PositionComparator implements Comparator<View> {
1503 int whichEdge = 0;
1504 public int compare(View left, View right) {
1505 CellAndSpan l = config.map.get(left);
1506 CellAndSpan r = config.map.get(right);
1507 switch (whichEdge) {
1508 case LEFT:
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001509 return (r.cellX + r.spanX) - (l.cellX + l.spanX);
Adam Cohenf3900c22012-11-16 18:28:11 -08001510 case RIGHT:
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001511 return l.cellX - r.cellX;
Adam Cohenf3900c22012-11-16 18:28:11 -08001512 case TOP:
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001513 return (r.cellY + r.spanY) - (l.cellY + l.spanY);
Adam Cohenf3900c22012-11-16 18:28:11 -08001514 case BOTTOM:
1515 default:
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001516 return l.cellY - r.cellY;
Adam Cohenf3900c22012-11-16 18:28:11 -08001517 }
1518 }
1519 }
1520
1521 public void sortConfigurationForEdgePush(int edge) {
1522 comparator.whichEdge = edge;
1523 Collections.sort(config.sortedViews, comparator);
1524 }
Adam Cohen47a876d2012-03-19 13:21:41 -07001525 }
1526
Adam Cohenf3900c22012-11-16 18:28:11 -08001527 private boolean pushViewsToTempLocation(ArrayList<View> views, Rect rectOccupiedByPotentialDrop,
1528 int[] direction, View dragView, ItemConfiguration currentState) {
Adam Cohene0489502012-08-27 15:18:53 -07001529
Adam Cohenf3900c22012-11-16 18:28:11 -08001530 ViewCluster cluster = new ViewCluster(views, currentState);
1531 Rect clusterRect = cluster.getBoundingRect();
1532 int whichEdge;
1533 int pushDistance;
1534 boolean fail = false;
1535
1536 // Determine the edge of the cluster that will be leading the push and how far
1537 // the cluster must be shifted.
1538 if (direction[0] < 0) {
1539 whichEdge = ViewCluster.LEFT;
1540 pushDistance = clusterRect.right - rectOccupiedByPotentialDrop.left;
Adam Cohene0489502012-08-27 15:18:53 -07001541 } else if (direction[0] > 0) {
Adam Cohenf3900c22012-11-16 18:28:11 -08001542 whichEdge = ViewCluster.RIGHT;
1543 pushDistance = rectOccupiedByPotentialDrop.right - clusterRect.left;
1544 } else if (direction[1] < 0) {
1545 whichEdge = ViewCluster.TOP;
1546 pushDistance = clusterRect.bottom - rectOccupiedByPotentialDrop.top;
1547 } else {
1548 whichEdge = ViewCluster.BOTTOM;
1549 pushDistance = rectOccupiedByPotentialDrop.bottom - clusterRect.top;
Adam Cohene0489502012-08-27 15:18:53 -07001550 }
1551
Adam Cohenf3900c22012-11-16 18:28:11 -08001552 // Break early for invalid push distance.
1553 if (pushDistance <= 0) {
1554 return false;
Adam Cohene0489502012-08-27 15:18:53 -07001555 }
Adam Cohenf3900c22012-11-16 18:28:11 -08001556
1557 // Mark the occupied state as false for the group of views we want to move.
1558 for (View v: views) {
1559 CellAndSpan c = currentState.map.get(v);
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001560 mTmpOccupied.markCells(c, false);
Adam Cohenf3900c22012-11-16 18:28:11 -08001561 }
1562
1563 // We save the current configuration -- if we fail to find a solution we will revert
1564 // to the initial state. The process of finding a solution modifies the configuration
1565 // in place, hence the need for revert in the failure case.
1566 currentState.save();
1567
1568 // The pushing algorithm is simplified by considering the views in the order in which
1569 // they would be pushed by the cluster. For example, if the cluster is leading with its
1570 // left edge, we consider sort the views by their right edge, from right to left.
1571 cluster.sortConfigurationForEdgePush(whichEdge);
1572
1573 while (pushDistance > 0 && !fail) {
1574 for (View v: currentState.sortedViews) {
1575 // For each view that isn't in the cluster, we see if the leading edge of the
1576 // cluster is contacting the edge of that view. If so, we add that view to the
1577 // cluster.
1578 if (!cluster.views.contains(v) && v != dragView) {
1579 if (cluster.isViewTouchingEdge(v, whichEdge)) {
1580 LayoutParams lp = (LayoutParams) v.getLayoutParams();
1581 if (!lp.canReorder) {
1582 // The push solution includes the all apps button, this is not viable.
1583 fail = true;
1584 break;
1585 }
1586 cluster.addView(v);
1587 CellAndSpan c = currentState.map.get(v);
1588
1589 // Adding view to cluster, mark it as not occupied.
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001590 mTmpOccupied.markCells(c, false);
Adam Cohenf3900c22012-11-16 18:28:11 -08001591 }
1592 }
1593 }
1594 pushDistance--;
1595
1596 // The cluster has been completed, now we move the whole thing over in the appropriate
1597 // direction.
1598 cluster.shift(whichEdge, 1);
1599 }
1600
1601 boolean foundSolution = false;
1602 clusterRect = cluster.getBoundingRect();
1603
1604 // Due to the nature of the algorithm, the only check required to verify a valid solution
1605 // is to ensure that completed shifted cluster lies completely within the cell layout.
1606 if (!fail && clusterRect.left >= 0 && clusterRect.right <= mCountX && clusterRect.top >= 0 &&
1607 clusterRect.bottom <= mCountY) {
1608 foundSolution = true;
1609 } else {
1610 currentState.restore();
1611 }
1612
1613 // In either case, we set the occupied array as marked for the location of the views
1614 for (View v: cluster.views) {
1615 CellAndSpan c = currentState.map.get(v);
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001616 mTmpOccupied.markCells(c, true);
Adam Cohenf3900c22012-11-16 18:28:11 -08001617 }
1618
1619 return foundSolution;
Adam Cohene0489502012-08-27 15:18:53 -07001620 }
1621
Adam Cohen482ed822012-03-02 14:15:13 -08001622 private boolean addViewsToTempLocation(ArrayList<View> views, Rect rectOccupiedByPotentialDrop,
Adam Cohenf3900c22012-11-16 18:28:11 -08001623 int[] direction, View dragView, ItemConfiguration currentState) {
Adam Cohen482ed822012-03-02 14:15:13 -08001624 if (views.size() == 0) return true;
Adam Cohen482ed822012-03-02 14:15:13 -08001625
Adam Cohen8baab352012-03-20 17:39:21 -07001626 boolean success = false;
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001627 Rect boundingRect = new Rect();
Adam Cohen8baab352012-03-20 17:39:21 -07001628 // We construct a rect which represents the entire group of views passed in
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001629 currentState.getBoundingRectForViews(views, boundingRect);
Adam Cohen8baab352012-03-20 17:39:21 -07001630
Adam Cohen8baab352012-03-20 17:39:21 -07001631 // Mark the occupied state as false for the group of views we want to move.
Adam Cohenf3900c22012-11-16 18:28:11 -08001632 for (View v: views) {
Adam Cohen8baab352012-03-20 17:39:21 -07001633 CellAndSpan c = currentState.map.get(v);
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001634 mTmpOccupied.markCells(c, false);
Adam Cohen8baab352012-03-20 17:39:21 -07001635 }
1636
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001637 GridOccupancy blockOccupied = new GridOccupancy(boundingRect.width(), boundingRect.height());
Adam Cohen47a876d2012-03-19 13:21:41 -07001638 int top = boundingRect.top;
1639 int left = boundingRect.left;
Adam Cohen8baab352012-03-20 17:39:21 -07001640 // We mark more precisely which parts of the bounding rect are truly occupied, allowing
Adam Cohena56dc102012-07-13 13:41:42 -07001641 // for interlocking.
Adam Cohenf3900c22012-11-16 18:28:11 -08001642 for (View v: views) {
Adam Cohen8baab352012-03-20 17:39:21 -07001643 CellAndSpan c = currentState.map.get(v);
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001644 blockOccupied.markCells(c.cellX - left, c.cellY - top, c.spanX, c.spanY, true);
Adam Cohen47a876d2012-03-19 13:21:41 -07001645 }
1646
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001647 mTmpOccupied.markCells(rectOccupiedByPotentialDrop, true);
Adam Cohen482ed822012-03-02 14:15:13 -08001648
Adam Cohenf3900c22012-11-16 18:28:11 -08001649 findNearestArea(boundingRect.left, boundingRect.top, boundingRect.width(),
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001650 boundingRect.height(), direction,
1651 mTmpOccupied.cells, blockOccupied.cells, mTempLocation);
Adam Cohen482ed822012-03-02 14:15:13 -08001652
Adam Cohen8baab352012-03-20 17:39:21 -07001653 // If we successfuly found a location by pushing the block of views, we commit it
Adam Cohen482ed822012-03-02 14:15:13 -08001654 if (mTempLocation[0] >= 0 && mTempLocation[1] >= 0) {
Adam Cohen8baab352012-03-20 17:39:21 -07001655 int deltaX = mTempLocation[0] - boundingRect.left;
1656 int deltaY = mTempLocation[1] - boundingRect.top;
Adam Cohenf3900c22012-11-16 18:28:11 -08001657 for (View v: views) {
Adam Cohen8baab352012-03-20 17:39:21 -07001658 CellAndSpan c = currentState.map.get(v);
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001659 c.cellX += deltaX;
1660 c.cellY += deltaY;
Adam Cohen482ed822012-03-02 14:15:13 -08001661 }
1662 success = true;
1663 }
Adam Cohen8baab352012-03-20 17:39:21 -07001664
1665 // In either case, we set the occupied array as marked for the location of the views
Adam Cohenf3900c22012-11-16 18:28:11 -08001666 for (View v: views) {
Adam Cohen8baab352012-03-20 17:39:21 -07001667 CellAndSpan c = currentState.map.get(v);
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001668 mTmpOccupied.markCells(c, true);
Adam Cohen482ed822012-03-02 14:15:13 -08001669 }
1670 return success;
1671 }
1672
Adam Cohen4abc5bd2012-05-29 21:06:03 -07001673 // This method tries to find a reordering solution which satisfies the push mechanic by trying
1674 // to push items in each of the cardinal directions, in an order based on the direction vector
1675 // passed.
1676 private boolean attemptPushInDirection(ArrayList<View> intersectingViews, Rect occupied,
1677 int[] direction, View ignoreView, ItemConfiguration solution) {
1678 if ((Math.abs(direction[0]) + Math.abs(direction[1])) > 1) {
Winson Chung5f8afe62013-08-12 16:19:28 -07001679 // If the direction vector has two non-zero components, we try pushing
Adam Cohen4abc5bd2012-05-29 21:06:03 -07001680 // separately in each of the components.
1681 int temp = direction[1];
1682 direction[1] = 0;
Adam Cohenf3900c22012-11-16 18:28:11 -08001683
1684 if (pushViewsToTempLocation(intersectingViews, occupied, direction,
Adam Cohen4abc5bd2012-05-29 21:06:03 -07001685 ignoreView, solution)) {
1686 return true;
1687 }
1688 direction[1] = temp;
1689 temp = direction[0];
1690 direction[0] = 0;
Adam Cohenf3900c22012-11-16 18:28:11 -08001691
1692 if (pushViewsToTempLocation(intersectingViews, occupied, direction,
Adam Cohen4abc5bd2012-05-29 21:06:03 -07001693 ignoreView, solution)) {
1694 return true;
1695 }
1696 // Revert the direction
1697 direction[0] = temp;
1698
1699 // Now we try pushing in each component of the opposite direction
1700 direction[0] *= -1;
1701 direction[1] *= -1;
1702 temp = direction[1];
1703 direction[1] = 0;
Adam Cohenf3900c22012-11-16 18:28:11 -08001704 if (pushViewsToTempLocation(intersectingViews, occupied, direction,
Adam Cohen4abc5bd2012-05-29 21:06:03 -07001705 ignoreView, solution)) {
1706 return true;
1707 }
1708
1709 direction[1] = temp;
1710 temp = direction[0];
1711 direction[0] = 0;
Adam Cohenf3900c22012-11-16 18:28:11 -08001712 if (pushViewsToTempLocation(intersectingViews, occupied, direction,
Adam Cohen4abc5bd2012-05-29 21:06:03 -07001713 ignoreView, solution)) {
1714 return true;
1715 }
1716 // revert the direction
1717 direction[0] = temp;
1718 direction[0] *= -1;
1719 direction[1] *= -1;
Winson Chung5f8afe62013-08-12 16:19:28 -07001720
Adam Cohen4abc5bd2012-05-29 21:06:03 -07001721 } else {
1722 // If the direction vector has a single non-zero component, we push first in the
1723 // direction of the vector
Adam Cohenf3900c22012-11-16 18:28:11 -08001724 if (pushViewsToTempLocation(intersectingViews, occupied, direction,
Adam Cohen4abc5bd2012-05-29 21:06:03 -07001725 ignoreView, solution)) {
1726 return true;
1727 }
Adam Cohen4abc5bd2012-05-29 21:06:03 -07001728 // Then we try the opposite direction
1729 direction[0] *= -1;
1730 direction[1] *= -1;
Adam Cohenf3900c22012-11-16 18:28:11 -08001731 if (pushViewsToTempLocation(intersectingViews, occupied, direction,
Adam Cohen4abc5bd2012-05-29 21:06:03 -07001732 ignoreView, solution)) {
1733 return true;
1734 }
1735 // Switch the direction back
1736 direction[0] *= -1;
1737 direction[1] *= -1;
Winson Chung5f8afe62013-08-12 16:19:28 -07001738
1739 // If we have failed to find a push solution with the above, then we try
Adam Cohen4abc5bd2012-05-29 21:06:03 -07001740 // to find a solution by pushing along the perpendicular axis.
1741
1742 // Swap the components
1743 int temp = direction[1];
1744 direction[1] = direction[0];
1745 direction[0] = temp;
Adam Cohenf3900c22012-11-16 18:28:11 -08001746 if (pushViewsToTempLocation(intersectingViews, occupied, direction,
Adam Cohen4abc5bd2012-05-29 21:06:03 -07001747 ignoreView, solution)) {
1748 return true;
1749 }
1750
1751 // Then we try the opposite direction
1752 direction[0] *= -1;
1753 direction[1] *= -1;
Adam Cohenf3900c22012-11-16 18:28:11 -08001754 if (pushViewsToTempLocation(intersectingViews, occupied, direction,
Adam Cohen4abc5bd2012-05-29 21:06:03 -07001755 ignoreView, solution)) {
1756 return true;
1757 }
1758 // Switch the direction back
1759 direction[0] *= -1;
1760 direction[1] *= -1;
1761
1762 // Swap the components back
1763 temp = direction[1];
1764 direction[1] = direction[0];
1765 direction[0] = temp;
1766 }
1767 return false;
1768 }
1769
Adam Cohen482ed822012-03-02 14:15:13 -08001770 private boolean rearrangementExists(int cellX, int cellY, int spanX, int spanY, int[] direction,
Adam Cohen8baab352012-03-20 17:39:21 -07001771 View ignoreView, ItemConfiguration solution) {
Winson Chunge3e03bc2012-05-01 15:10:11 -07001772 // Return early if get invalid cell positions
1773 if (cellX < 0 || cellY < 0) return false;
Adam Cohen482ed822012-03-02 14:15:13 -08001774
Adam Cohen8baab352012-03-20 17:39:21 -07001775 mIntersectingViews.clear();
Adam Cohen482ed822012-03-02 14:15:13 -08001776 mOccupiedRect.set(cellX, cellY, cellX + spanX, cellY + spanY);
Adam Cohen482ed822012-03-02 14:15:13 -08001777
Adam Cohen8baab352012-03-20 17:39:21 -07001778 // Mark the desired location of the view currently being dragged.
Adam Cohen482ed822012-03-02 14:15:13 -08001779 if (ignoreView != null) {
Adam Cohen8baab352012-03-20 17:39:21 -07001780 CellAndSpan c = solution.map.get(ignoreView);
Adam Cohen19f37922012-03-21 11:59:11 -07001781 if (c != null) {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001782 c.cellX = cellX;
1783 c.cellY = cellY;
Adam Cohen19f37922012-03-21 11:59:11 -07001784 }
Adam Cohen482ed822012-03-02 14:15:13 -08001785 }
Adam Cohen482ed822012-03-02 14:15:13 -08001786 Rect r0 = new Rect(cellX, cellY, cellX + spanX, cellY + spanY);
1787 Rect r1 = new Rect();
Adam Cohen8baab352012-03-20 17:39:21 -07001788 for (View child: solution.map.keySet()) {
Adam Cohen482ed822012-03-02 14:15:13 -08001789 if (child == ignoreView) continue;
Adam Cohen8baab352012-03-20 17:39:21 -07001790 CellAndSpan c = solution.map.get(child);
Adam Cohen482ed822012-03-02 14:15:13 -08001791 LayoutParams lp = (LayoutParams) child.getLayoutParams();
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001792 r1.set(c.cellX, c.cellY, c.cellX + c.spanX, c.cellY + c.spanY);
Adam Cohen482ed822012-03-02 14:15:13 -08001793 if (Rect.intersects(r0, r1)) {
1794 if (!lp.canReorder) {
1795 return false;
1796 }
1797 mIntersectingViews.add(child);
1798 }
1799 }
Adam Cohen47a876d2012-03-19 13:21:41 -07001800
Adam Cohenfa3c58f2013-12-06 16:10:55 -08001801 solution.intersectingViews = new ArrayList<View>(mIntersectingViews);
1802
Winson Chung5f8afe62013-08-12 16:19:28 -07001803 // First we try to find a solution which respects the push mechanic. That is,
Adam Cohen4abc5bd2012-05-29 21:06:03 -07001804 // we try to find a solution such that no displaced item travels through another item
1805 // without also displacing that item.
1806 if (attemptPushInDirection(mIntersectingViews, mOccupiedRect, direction, ignoreView,
Adam Cohen19f37922012-03-21 11:59:11 -07001807 solution)) {
Adam Cohen47a876d2012-03-19 13:21:41 -07001808 return true;
1809 }
Adam Cohen47a876d2012-03-19 13:21:41 -07001810
Adam Cohen4abc5bd2012-05-29 21:06:03 -07001811 // Next we try moving the views as a block, but without requiring the push mechanic.
Adam Cohenf3900c22012-11-16 18:28:11 -08001812 if (addViewsToTempLocation(mIntersectingViews, mOccupiedRect, direction, ignoreView,
Adam Cohen19f37922012-03-21 11:59:11 -07001813 solution)) {
Adam Cohen482ed822012-03-02 14:15:13 -08001814 return true;
1815 }
Adam Cohen47a876d2012-03-19 13:21:41 -07001816
Adam Cohen482ed822012-03-02 14:15:13 -08001817 // Ok, they couldn't move as a block, let's move them individually
1818 for (View v : mIntersectingViews) {
Adam Cohen8baab352012-03-20 17:39:21 -07001819 if (!addViewToTempLocation(v, mOccupiedRect, direction, solution)) {
Adam Cohen482ed822012-03-02 14:15:13 -08001820 return false;
1821 }
1822 }
1823 return true;
1824 }
1825
1826 /*
1827 * Returns a pair (x, y), where x,y are in {-1, 0, 1} corresponding to vector between
1828 * the provided point and the provided cell
1829 */
Adam Cohen47a876d2012-03-19 13:21:41 -07001830 private void computeDirectionVector(float deltaX, float deltaY, int[] result) {
Jon Mirandae96798e2016-12-07 12:10:44 -08001831 double angle = Math.atan(deltaY / deltaX);
Adam Cohen482ed822012-03-02 14:15:13 -08001832
1833 result[0] = 0;
1834 result[1] = 0;
1835 if (Math.abs(Math.cos(angle)) > 0.5f) {
1836 result[0] = (int) Math.signum(deltaX);
1837 }
1838 if (Math.abs(Math.sin(angle)) > 0.5f) {
1839 result[1] = (int) Math.signum(deltaY);
1840 }
1841 }
1842
Sunny Goyalf7a29e82015-04-24 15:20:43 -07001843 private ItemConfiguration findReorderSolution(int pixelX, int pixelY, int minSpanX, int minSpanY,
Adam Cohenfa3c58f2013-12-06 16:10:55 -08001844 int spanX, int spanY, int[] direction, View dragView, boolean decX,
1845 ItemConfiguration solution) {
Adam Cohen8baab352012-03-20 17:39:21 -07001846 // Copy the current state into the solution. This solution will be manipulated as necessary.
1847 copyCurrentStateToSolution(solution, false);
1848 // Copy the current occupied array into the temporary occupied array. This array will be
1849 // manipulated as necessary to find a solution.
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001850 mOccupied.copyTo(mTmpOccupied);
Adam Cohen482ed822012-03-02 14:15:13 -08001851
1852 // We find the nearest cell into which we would place the dragged item, assuming there's
1853 // nothing in its way.
1854 int result[] = new int[2];
1855 result = findNearestArea(pixelX, pixelY, spanX, spanY, result);
1856
1857 boolean success = false;
1858 // First we try the exact nearest position of the item being dragged,
1859 // we will then want to try to move this around to other neighbouring positions
Adam Cohen8baab352012-03-20 17:39:21 -07001860 success = rearrangementExists(result[0], result[1], spanX, spanY, direction, dragView,
1861 solution);
Adam Cohen482ed822012-03-02 14:15:13 -08001862
1863 if (!success) {
1864 // We try shrinking the widget down to size in an alternating pattern, shrink 1 in
1865 // x, then 1 in y etc.
1866 if (spanX > minSpanX && (minSpanY == spanY || decX)) {
Adam Cohenfa3c58f2013-12-06 16:10:55 -08001867 return findReorderSolution(pixelX, pixelY, minSpanX, minSpanY, spanX - 1, spanY,
1868 direction, dragView, false, solution);
Adam Cohen482ed822012-03-02 14:15:13 -08001869 } else if (spanY > minSpanY) {
Adam Cohenfa3c58f2013-12-06 16:10:55 -08001870 return findReorderSolution(pixelX, pixelY, minSpanX, minSpanY, spanX, spanY - 1,
1871 direction, dragView, true, solution);
Adam Cohen482ed822012-03-02 14:15:13 -08001872 }
1873 solution.isSolution = false;
1874 } else {
1875 solution.isSolution = true;
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001876 solution.cellX = result[0];
1877 solution.cellY = result[1];
1878 solution.spanX = spanX;
1879 solution.spanY = spanY;
Adam Cohen482ed822012-03-02 14:15:13 -08001880 }
1881 return solution;
1882 }
1883
1884 private void copyCurrentStateToSolution(ItemConfiguration solution, boolean temp) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07001885 int childCount = mShortcutsAndWidgets.getChildCount();
Adam Cohen482ed822012-03-02 14:15:13 -08001886 for (int i = 0; i < childCount; i++) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07001887 View child = mShortcutsAndWidgets.getChildAt(i);
Adam Cohen482ed822012-03-02 14:15:13 -08001888 LayoutParams lp = (LayoutParams) child.getLayoutParams();
Adam Cohen8baab352012-03-20 17:39:21 -07001889 CellAndSpan c;
Adam Cohen482ed822012-03-02 14:15:13 -08001890 if (temp) {
Adam Cohen8baab352012-03-20 17:39:21 -07001891 c = new CellAndSpan(lp.tmpCellX, lp.tmpCellY, lp.cellHSpan, lp.cellVSpan);
Adam Cohen482ed822012-03-02 14:15:13 -08001892 } else {
Adam Cohen8baab352012-03-20 17:39:21 -07001893 c = new CellAndSpan(lp.cellX, lp.cellY, lp.cellHSpan, lp.cellVSpan);
Adam Cohen482ed822012-03-02 14:15:13 -08001894 }
Adam Cohenf3900c22012-11-16 18:28:11 -08001895 solution.add(child, c);
Adam Cohen482ed822012-03-02 14:15:13 -08001896 }
1897 }
1898
1899 private void copySolutionToTempState(ItemConfiguration solution, View dragView) {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001900 mTmpOccupied.clear();
Adam Cohen482ed822012-03-02 14:15:13 -08001901
Michael Jurkaa52570f2012-03-20 03:18:20 -07001902 int childCount = mShortcutsAndWidgets.getChildCount();
Adam Cohen482ed822012-03-02 14:15:13 -08001903 for (int i = 0; i < childCount; i++) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07001904 View child = mShortcutsAndWidgets.getChildAt(i);
Adam Cohen482ed822012-03-02 14:15:13 -08001905 if (child == dragView) continue;
1906 LayoutParams lp = (LayoutParams) child.getLayoutParams();
Adam Cohen8baab352012-03-20 17:39:21 -07001907 CellAndSpan c = solution.map.get(child);
1908 if (c != null) {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001909 lp.tmpCellX = c.cellX;
1910 lp.tmpCellY = c.cellY;
Adam Cohen8baab352012-03-20 17:39:21 -07001911 lp.cellHSpan = c.spanX;
1912 lp.cellVSpan = c.spanY;
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001913 mTmpOccupied.markCells(c, true);
Adam Cohen482ed822012-03-02 14:15:13 -08001914 }
1915 }
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001916 mTmpOccupied.markCells(solution, true);
Adam Cohen482ed822012-03-02 14:15:13 -08001917 }
1918
1919 private void animateItemsToSolution(ItemConfiguration solution, View dragView, boolean
1920 commitDragView) {
1921
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001922 GridOccupancy occupied = DESTRUCTIVE_REORDER ? mOccupied : mTmpOccupied;
1923 occupied.clear();
Adam Cohen482ed822012-03-02 14:15:13 -08001924
Michael Jurkaa52570f2012-03-20 03:18:20 -07001925 int childCount = mShortcutsAndWidgets.getChildCount();
Adam Cohen482ed822012-03-02 14:15:13 -08001926 for (int i = 0; i < childCount; i++) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07001927 View child = mShortcutsAndWidgets.getChildAt(i);
Adam Cohen482ed822012-03-02 14:15:13 -08001928 if (child == dragView) continue;
Adam Cohen8baab352012-03-20 17:39:21 -07001929 CellAndSpan c = solution.map.get(child);
1930 if (c != null) {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001931 animateChildToPosition(child, c.cellX, c.cellY, REORDER_ANIMATION_DURATION, 0,
Adam Cohen19f37922012-03-21 11:59:11 -07001932 DESTRUCTIVE_REORDER, false);
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001933 occupied.markCells(c, true);
Adam Cohen482ed822012-03-02 14:15:13 -08001934 }
1935 }
1936 if (commitDragView) {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001937 occupied.markCells(solution, true);
Adam Cohen482ed822012-03-02 14:15:13 -08001938 }
1939 }
1940
Adam Cohenfa3c58f2013-12-06 16:10:55 -08001941
1942 // This method starts or changes the reorder preview animations
1943 private void beginOrAdjustReorderPreviewAnimations(ItemConfiguration solution,
1944 View dragView, int delay, int mode) {
Adam Cohen19f37922012-03-21 11:59:11 -07001945 int childCount = mShortcutsAndWidgets.getChildCount();
Adam Cohen19f37922012-03-21 11:59:11 -07001946 for (int i = 0; i < childCount; i++) {
1947 View child = mShortcutsAndWidgets.getChildAt(i);
1948 if (child == dragView) continue;
1949 CellAndSpan c = solution.map.get(child);
Adam Cohenfa3c58f2013-12-06 16:10:55 -08001950 boolean skip = mode == ReorderPreviewAnimation.MODE_HINT && solution.intersectingViews
1951 != null && !solution.intersectingViews.contains(child);
1952
Adam Cohen19f37922012-03-21 11:59:11 -07001953 LayoutParams lp = (LayoutParams) child.getLayoutParams();
Adam Cohenfa3c58f2013-12-06 16:10:55 -08001954 if (c != null && !skip) {
1955 ReorderPreviewAnimation rha = new ReorderPreviewAnimation(child, mode, lp.cellX,
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001956 lp.cellY, c.cellX, c.cellY, c.spanX, c.spanY);
Adam Cohend024f982012-05-23 18:26:45 -07001957 rha.animate();
Adam Cohen19f37922012-03-21 11:59:11 -07001958 }
1959 }
1960 }
1961
Adam Cohenfa3c58f2013-12-06 16:10:55 -08001962 // Class which represents the reorder preview animations. These animations show that an item is
Adam Cohen19f37922012-03-21 11:59:11 -07001963 // in a temporary state, and hint at where the item will return to.
Adam Cohenfa3c58f2013-12-06 16:10:55 -08001964 class ReorderPreviewAnimation {
Adam Cohen19f37922012-03-21 11:59:11 -07001965 View child;
Adam Cohend024f982012-05-23 18:26:45 -07001966 float finalDeltaX;
1967 float finalDeltaY;
1968 float initDeltaX;
1969 float initDeltaY;
1970 float finalScale;
1971 float initScale;
Adam Cohenfa3c58f2013-12-06 16:10:55 -08001972 int mode;
1973 boolean repeating = false;
1974 private static final int PREVIEW_DURATION = 300;
1975 private static final int HINT_DURATION = Workspace.REORDER_TIMEOUT;
1976
Jon Miranda21266912016-12-19 14:12:05 -08001977 private static final float CHILD_DIVIDEND = 4.0f;
1978
Adam Cohenfa3c58f2013-12-06 16:10:55 -08001979 public static final int MODE_HINT = 0;
1980 public static final int MODE_PREVIEW = 1;
1981
Adam Cohene7587d22012-05-24 18:50:02 -07001982 Animator a;
Adam Cohen19f37922012-03-21 11:59:11 -07001983
Adam Cohenfa3c58f2013-12-06 16:10:55 -08001984 public ReorderPreviewAnimation(View child, int mode, int cellX0, int cellY0, int cellX1,
1985 int cellY1, int spanX, int spanY) {
Adam Cohen19f37922012-03-21 11:59:11 -07001986 regionToCenterPoint(cellX0, cellY0, spanX, spanY, mTmpPoint);
1987 final int x0 = mTmpPoint[0];
1988 final int y0 = mTmpPoint[1];
1989 regionToCenterPoint(cellX1, cellY1, spanX, spanY, mTmpPoint);
1990 final int x1 = mTmpPoint[0];
1991 final int y1 = mTmpPoint[1];
1992 final int dX = x1 - x0;
1993 final int dY = y1 - y0;
Jon Miranda21266912016-12-19 14:12:05 -08001994
1995 this.child = child;
1996 this.mode = mode;
1997 setInitialAnimationValues(false);
1998 finalScale = (mChildScale - (CHILD_DIVIDEND / child.getWidth())) * initScale;
1999 finalDeltaX = initDeltaX;
2000 finalDeltaY = initDeltaY;
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002001 int dir = mode == MODE_HINT ? -1 : 1;
Adam Cohen19f37922012-03-21 11:59:11 -07002002 if (dX == dY && dX == 0) {
2003 } else {
2004 if (dY == 0) {
Jon Miranda21266912016-12-19 14:12:05 -08002005 finalDeltaX += - dir * Math.signum(dX) * mReorderPreviewAnimationMagnitude;
Adam Cohen19f37922012-03-21 11:59:11 -07002006 } else if (dX == 0) {
Jon Miranda21266912016-12-19 14:12:05 -08002007 finalDeltaY += - dir * Math.signum(dY) * mReorderPreviewAnimationMagnitude;
Adam Cohen19f37922012-03-21 11:59:11 -07002008 } else {
2009 double angle = Math.atan( (float) (dY) / dX);
Jon Miranda21266912016-12-19 14:12:05 -08002010 finalDeltaX += (int) (- dir * Math.signum(dX) *
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002011 Math.abs(Math.cos(angle) * mReorderPreviewAnimationMagnitude));
Jon Miranda21266912016-12-19 14:12:05 -08002012 finalDeltaY += (int) (- dir * Math.signum(dY) *
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002013 Math.abs(Math.sin(angle) * mReorderPreviewAnimationMagnitude));
Adam Cohen19f37922012-03-21 11:59:11 -07002014 }
2015 }
Jon Miranda21266912016-12-19 14:12:05 -08002016 }
2017
2018 void setInitialAnimationValues(boolean restoreOriginalValues) {
2019 if (restoreOriginalValues) {
2020 if (child instanceof LauncherAppWidgetHostView) {
2021 LauncherAppWidgetHostView lahv = (LauncherAppWidgetHostView) child;
2022 initScale = lahv.getScaleToFit();
2023 initDeltaX = lahv.getTranslationForCentering().x;
2024 initDeltaY = lahv.getTranslationForCentering().y;
2025 } else {
2026 initScale = mChildScale;
2027 initDeltaX = 0;
2028 initDeltaY = 0;
2029 }
2030 } else {
2031 initScale = child.getScaleX();
2032 initDeltaX = child.getTranslationX();
2033 initDeltaY = child.getTranslationY();
2034 }
Adam Cohen19f37922012-03-21 11:59:11 -07002035 }
2036
Adam Cohend024f982012-05-23 18:26:45 -07002037 void animate() {
Jon Miranda21266912016-12-19 14:12:05 -08002038 boolean noMovement = (finalDeltaX == initDeltaX) && (finalDeltaY == initDeltaY);
2039
Adam Cohen19f37922012-03-21 11:59:11 -07002040 if (mShakeAnimators.containsKey(child)) {
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002041 ReorderPreviewAnimation oldAnimation = mShakeAnimators.get(child);
Adam Cohend024f982012-05-23 18:26:45 -07002042 oldAnimation.cancel();
Adam Cohen19f37922012-03-21 11:59:11 -07002043 mShakeAnimators.remove(child);
Jon Miranda21266912016-12-19 14:12:05 -08002044 if (noMovement) {
Adam Cohene7587d22012-05-24 18:50:02 -07002045 completeAnimationImmediately();
2046 return;
2047 }
Adam Cohen19f37922012-03-21 11:59:11 -07002048 }
Jon Miranda21266912016-12-19 14:12:05 -08002049 if (noMovement) {
Adam Cohen19f37922012-03-21 11:59:11 -07002050 return;
2051 }
Jon Mirandacda3bfb2017-02-06 15:54:41 -08002052 ValueAnimator va = LauncherAnimUtils.ofFloat(0f, 1f);
Adam Cohene7587d22012-05-24 18:50:02 -07002053 a = va;
Tony Wickham9e0702f2015-09-02 14:45:39 -07002054
2055 // Animations are disabled in power save mode, causing the repeated animation to jump
2056 // spastically between beginning and end states. Since this looks bad, we don't repeat
2057 // the animation in power save mode.
Tony Wickham112ac952015-11-12 12:31:50 -08002058 if (!Utilities.isPowerSaverOn(getContext())) {
Tony Wickham9e0702f2015-09-02 14:45:39 -07002059 va.setRepeatMode(ValueAnimator.REVERSE);
2060 va.setRepeatCount(ValueAnimator.INFINITE);
2061 }
2062
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002063 va.setDuration(mode == MODE_HINT ? HINT_DURATION : PREVIEW_DURATION);
Adam Cohend024f982012-05-23 18:26:45 -07002064 va.setStartDelay((int) (Math.random() * 60));
Adam Cohen19f37922012-03-21 11:59:11 -07002065 va.addUpdateListener(new AnimatorUpdateListener() {
2066 @Override
2067 public void onAnimationUpdate(ValueAnimator animation) {
Jon Mirandae96798e2016-12-07 12:10:44 -08002068 float r = (Float) animation.getAnimatedValue();
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002069 float r1 = (mode == MODE_HINT && repeating) ? 1.0f : r;
2070 float x = r1 * finalDeltaX + (1 - r1) * initDeltaX;
2071 float y = r1 * finalDeltaY + (1 - r1) * initDeltaY;
Adam Cohen19f37922012-03-21 11:59:11 -07002072 child.setTranslationX(x);
2073 child.setTranslationY(y);
Adam Cohend024f982012-05-23 18:26:45 -07002074 float s = r * finalScale + (1 - r) * initScale;
Brandon Keely50e6e562012-05-08 16:28:49 -07002075 child.setScaleX(s);
2076 child.setScaleY(s);
Adam Cohen19f37922012-03-21 11:59:11 -07002077 }
2078 });
2079 va.addListener(new AnimatorListenerAdapter() {
2080 public void onAnimationRepeat(Animator animation) {
Adam Cohen19f37922012-03-21 11:59:11 -07002081 // We make sure to end only after a full period
Jon Miranda21266912016-12-19 14:12:05 -08002082 setInitialAnimationValues(true);
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002083 repeating = true;
Adam Cohen19f37922012-03-21 11:59:11 -07002084 }
2085 });
Adam Cohen19f37922012-03-21 11:59:11 -07002086 mShakeAnimators.put(child, this);
2087 va.start();
2088 }
2089
Adam Cohend024f982012-05-23 18:26:45 -07002090 private void cancel() {
Adam Cohene7587d22012-05-24 18:50:02 -07002091 if (a != null) {
2092 a.cancel();
2093 }
Adam Cohen19f37922012-03-21 11:59:11 -07002094 }
Adam Cohene7587d22012-05-24 18:50:02 -07002095
Adam Cohen091440a2015-03-18 14:16:05 -07002096 @Thunk void completeAnimationImmediately() {
Adam Cohene7587d22012-05-24 18:50:02 -07002097 if (a != null) {
2098 a.cancel();
2099 }
Brandon Keely50e6e562012-05-08 16:28:49 -07002100
Jon Miranda72dbd912017-01-04 14:03:07 -08002101 setInitialAnimationValues(true);
Sunny Goyal9e76f682017-02-13 12:13:43 -08002102 a = LauncherAnimUtils.ofPropertyValuesHolder(child,
2103 new PropertyListBuilder()
2104 .scale(initScale)
2105 .translationX(initDeltaX)
2106 .translationY(initDeltaY)
2107 .build())
2108 .setDuration(REORDER_ANIMATION_DURATION);
Sunny Goyal5d2fc322015-07-06 22:52:49 -07002109 a.setInterpolator(new android.view.animation.DecelerateInterpolator(1.5f));
2110 a.start();
Brandon Keely50e6e562012-05-08 16:28:49 -07002111 }
Adam Cohen19f37922012-03-21 11:59:11 -07002112 }
2113
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002114 private void completeAndClearReorderPreviewAnimations() {
2115 for (ReorderPreviewAnimation a: mShakeAnimators.values()) {
Brandon Keely50e6e562012-05-08 16:28:49 -07002116 a.completeAnimationImmediately();
Adam Cohen19f37922012-03-21 11:59:11 -07002117 }
2118 mShakeAnimators.clear();
2119 }
2120
Adam Cohen482ed822012-03-02 14:15:13 -08002121 private void commitTempPlacement() {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07002122 mTmpOccupied.copyTo(mOccupied);
Sunny Goyalaa8ef112015-06-12 20:04:41 -07002123
2124 long screenId = mLauncher.getWorkspace().getIdForScreen(this);
2125 int container = Favorites.CONTAINER_DESKTOP;
2126
Sunny Goyalc13403c2016-11-18 23:44:48 -08002127 if (mContainerType == HOTSEAT) {
Sunny Goyalaa8ef112015-06-12 20:04:41 -07002128 screenId = -1;
2129 container = Favorites.CONTAINER_HOTSEAT;
2130 }
2131
Michael Jurkaa52570f2012-03-20 03:18:20 -07002132 int childCount = mShortcutsAndWidgets.getChildCount();
Adam Cohen482ed822012-03-02 14:15:13 -08002133 for (int i = 0; i < childCount; i++) {
Adam Cohenea889a22012-03-27 16:45:39 -07002134 View child = mShortcutsAndWidgets.getChildAt(i);
2135 LayoutParams lp = (LayoutParams) child.getLayoutParams();
2136 ItemInfo info = (ItemInfo) child.getTag();
Adam Cohen2acce882012-03-28 19:03:19 -07002137 // We do a null check here because the item info can be null in the case of the
2138 // AllApps button in the hotseat.
2139 if (info != null) {
Sunny Goyalaa8ef112015-06-12 20:04:41 -07002140 final boolean requiresDbUpdate = (info.cellX != lp.tmpCellX
2141 || info.cellY != lp.tmpCellY || info.spanX != lp.cellHSpan
2142 || info.spanY != lp.cellVSpan);
2143
Adam Cohen2acce882012-03-28 19:03:19 -07002144 info.cellX = lp.cellX = lp.tmpCellX;
2145 info.cellY = lp.cellY = lp.tmpCellY;
Adam Cohenbebf0422012-04-11 18:06:28 -07002146 info.spanX = lp.cellHSpan;
2147 info.spanY = lp.cellVSpan;
Sunny Goyalaa8ef112015-06-12 20:04:41 -07002148
2149 if (requiresDbUpdate) {
Sunny Goyal43bf11d2017-02-02 13:52:53 -08002150 mLauncher.getModelWriter().modifyItemInDatabase(info, container, screenId,
Sunny Goyalaa8ef112015-06-12 20:04:41 -07002151 info.cellX, info.cellY, info.spanX, info.spanY);
2152 }
Adam Cohen2acce882012-03-28 19:03:19 -07002153 }
Adam Cohen482ed822012-03-02 14:15:13 -08002154 }
2155 }
2156
Sunny Goyalf7a29e82015-04-24 15:20:43 -07002157 private void setUseTempCoords(boolean useTempCoords) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07002158 int childCount = mShortcutsAndWidgets.getChildCount();
Adam Cohen482ed822012-03-02 14:15:13 -08002159 for (int i = 0; i < childCount; i++) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07002160 LayoutParams lp = (LayoutParams) mShortcutsAndWidgets.getChildAt(i).getLayoutParams();
Adam Cohen482ed822012-03-02 14:15:13 -08002161 lp.useTmpCoords = useTempCoords;
2162 }
2163 }
2164
Sunny Goyalf7a29e82015-04-24 15:20:43 -07002165 private ItemConfiguration findConfigurationNoShuffle(int pixelX, int pixelY, int minSpanX, int minSpanY,
Adam Cohen482ed822012-03-02 14:15:13 -08002166 int spanX, int spanY, View dragView, ItemConfiguration solution) {
2167 int[] result = new int[2];
2168 int[] resultSpan = new int[2];
Sunny Goyalf7a29e82015-04-24 15:20:43 -07002169 findNearestVacantArea(pixelX, pixelY, minSpanX, minSpanY, spanX, spanY, result,
Adam Cohen482ed822012-03-02 14:15:13 -08002170 resultSpan);
2171 if (result[0] >= 0 && result[1] >= 0) {
2172 copyCurrentStateToSolution(solution, false);
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07002173 solution.cellX = result[0];
2174 solution.cellY = result[1];
2175 solution.spanX = resultSpan[0];
2176 solution.spanY = resultSpan[1];
Adam Cohen482ed822012-03-02 14:15:13 -08002177 solution.isSolution = true;
2178 } else {
2179 solution.isSolution = false;
2180 }
2181 return solution;
2182 }
2183
Adam Cohen19f37922012-03-21 11:59:11 -07002184 /* This seems like it should be obvious and straight-forward, but when the direction vector
2185 needs to match with the notion of the dragView pushing other views, we have to employ
2186 a slightly more subtle notion of the direction vector. The question is what two points is
2187 the vector between? The center of the dragView and its desired destination? Not quite, as
2188 this doesn't necessarily coincide with the interaction of the dragView and items occupying
2189 those cells. Instead we use some heuristics to often lock the vector to up, down, left
2190 or right, which helps make pushing feel right.
2191 */
2192 private void getDirectionVectorForDrop(int dragViewCenterX, int dragViewCenterY, int spanX,
2193 int spanY, View dragView, int[] resultDirection) {
2194 int[] targetDestination = new int[2];
2195
2196 findNearestArea(dragViewCenterX, dragViewCenterY, spanX, spanY, targetDestination);
2197 Rect dragRect = new Rect();
2198 regionToRect(targetDestination[0], targetDestination[1], spanX, spanY, dragRect);
2199 dragRect.offset(dragViewCenterX - dragRect.centerX(), dragViewCenterY - dragRect.centerY());
2200
2201 Rect dropRegionRect = new Rect();
2202 getViewsIntersectingRegion(targetDestination[0], targetDestination[1], spanX, spanY,
2203 dragView, dropRegionRect, mIntersectingViews);
2204
2205 int dropRegionSpanX = dropRegionRect.width();
2206 int dropRegionSpanY = dropRegionRect.height();
2207
2208 regionToRect(dropRegionRect.left, dropRegionRect.top, dropRegionRect.width(),
2209 dropRegionRect.height(), dropRegionRect);
2210
2211 int deltaX = (dropRegionRect.centerX() - dragViewCenterX) / spanX;
2212 int deltaY = (dropRegionRect.centerY() - dragViewCenterY) / spanY;
2213
2214 if (dropRegionSpanX == mCountX || spanX == mCountX) {
2215 deltaX = 0;
2216 }
2217 if (dropRegionSpanY == mCountY || spanY == mCountY) {
2218 deltaY = 0;
2219 }
2220
2221 if (deltaX == 0 && deltaY == 0) {
2222 // No idea what to do, give a random direction.
2223 resultDirection[0] = 1;
2224 resultDirection[1] = 0;
2225 } else {
2226 computeDirectionVector(deltaX, deltaY, resultDirection);
2227 }
2228 }
2229
2230 // For a given cell and span, fetch the set of views intersecting the region.
2231 private void getViewsIntersectingRegion(int cellX, int cellY, int spanX, int spanY,
2232 View dragView, Rect boundingRect, ArrayList<View> intersectingViews) {
2233 if (boundingRect != null) {
2234 boundingRect.set(cellX, cellY, cellX + spanX, cellY + spanY);
2235 }
2236 intersectingViews.clear();
2237 Rect r0 = new Rect(cellX, cellY, cellX + spanX, cellY + spanY);
2238 Rect r1 = new Rect();
2239 final int count = mShortcutsAndWidgets.getChildCount();
2240 for (int i = 0; i < count; i++) {
2241 View child = mShortcutsAndWidgets.getChildAt(i);
2242 if (child == dragView) continue;
2243 LayoutParams lp = (LayoutParams) child.getLayoutParams();
2244 r1.set(lp.cellX, lp.cellY, lp.cellX + lp.cellHSpan, lp.cellY + lp.cellVSpan);
2245 if (Rect.intersects(r0, r1)) {
2246 mIntersectingViews.add(child);
2247 if (boundingRect != null) {
2248 boundingRect.union(r1);
2249 }
2250 }
2251 }
2252 }
2253
2254 boolean isNearestDropLocationOccupied(int pixelX, int pixelY, int spanX, int spanY,
2255 View dragView, int[] result) {
2256 result = findNearestArea(pixelX, pixelY, spanX, spanY, result);
2257 getViewsIntersectingRegion(result[0], result[1], spanX, spanY, dragView, null,
2258 mIntersectingViews);
2259 return !mIntersectingViews.isEmpty();
2260 }
2261
2262 void revertTempState() {
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002263 completeAndClearReorderPreviewAnimations();
2264 if (isItemPlacementDirty() && !DESTRUCTIVE_REORDER) {
2265 final int count = mShortcutsAndWidgets.getChildCount();
2266 for (int i = 0; i < count; i++) {
2267 View child = mShortcutsAndWidgets.getChildAt(i);
2268 LayoutParams lp = (LayoutParams) child.getLayoutParams();
2269 if (lp.tmpCellX != lp.cellX || lp.tmpCellY != lp.cellY) {
2270 lp.tmpCellX = lp.cellX;
2271 lp.tmpCellY = lp.cellY;
2272 animateChildToPosition(child, lp.cellX, lp.cellY, REORDER_ANIMATION_DURATION,
2273 0, false, false);
2274 }
Adam Cohen19f37922012-03-21 11:59:11 -07002275 }
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002276 setItemPlacementDirty(false);
Adam Cohen19f37922012-03-21 11:59:11 -07002277 }
Adam Cohen19f37922012-03-21 11:59:11 -07002278 }
2279
Adam Cohenbebf0422012-04-11 18:06:28 -07002280 boolean createAreaForResize(int cellX, int cellY, int spanX, int spanY,
2281 View dragView, int[] direction, boolean commit) {
2282 int[] pixelXY = new int[2];
2283 regionToCenterPoint(cellX, cellY, spanX, spanY, pixelXY);
2284
2285 // First we determine if things have moved enough to cause a different layout
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002286 ItemConfiguration swapSolution = findReorderSolution(pixelXY[0], pixelXY[1], spanX, spanY,
Adam Cohenbebf0422012-04-11 18:06:28 -07002287 spanX, spanY, direction, dragView, true, new ItemConfiguration());
2288
2289 setUseTempCoords(true);
2290 if (swapSolution != null && swapSolution.isSolution) {
2291 // If we're just testing for a possible location (MODE_ACCEPT_DROP), we don't bother
2292 // committing anything or animating anything as we just want to determine if a solution
2293 // exists
2294 copySolutionToTempState(swapSolution, dragView);
2295 setItemPlacementDirty(true);
2296 animateItemsToSolution(swapSolution, dragView, commit);
2297
2298 if (commit) {
2299 commitTempPlacement();
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002300 completeAndClearReorderPreviewAnimations();
Adam Cohenbebf0422012-04-11 18:06:28 -07002301 setItemPlacementDirty(false);
2302 } else {
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002303 beginOrAdjustReorderPreviewAnimations(swapSolution, dragView,
2304 REORDER_ANIMATION_DURATION, ReorderPreviewAnimation.MODE_PREVIEW);
Adam Cohenbebf0422012-04-11 18:06:28 -07002305 }
2306 mShortcutsAndWidgets.requestLayout();
2307 }
2308 return swapSolution.isSolution;
2309 }
2310
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002311 int[] performReorder(int pixelX, int pixelY, int minSpanX, int minSpanY, int spanX, int spanY,
Adam Cohen482ed822012-03-02 14:15:13 -08002312 View dragView, int[] result, int resultSpan[], int mode) {
Adam Cohen482ed822012-03-02 14:15:13 -08002313 // First we determine if things have moved enough to cause a different layout
Adam Cohen47a876d2012-03-19 13:21:41 -07002314 result = findNearestArea(pixelX, pixelY, spanX, spanY, result);
Adam Cohen482ed822012-03-02 14:15:13 -08002315
2316 if (resultSpan == null) {
2317 resultSpan = new int[2];
2318 }
2319
Adam Cohen19f37922012-03-21 11:59:11 -07002320 // When we are checking drop validity or actually dropping, we don't recompute the
2321 // direction vector, since we want the solution to match the preview, and it's possible
2322 // that the exact position of the item has changed to result in a new reordering outcome.
Adam Cohenb209e632012-03-27 17:09:36 -07002323 if ((mode == MODE_ON_DROP || mode == MODE_ON_DROP_EXTERNAL || mode == MODE_ACCEPT_DROP)
2324 && mPreviousReorderDirection[0] != INVALID_DIRECTION) {
Adam Cohen19f37922012-03-21 11:59:11 -07002325 mDirectionVector[0] = mPreviousReorderDirection[0];
2326 mDirectionVector[1] = mPreviousReorderDirection[1];
2327 // We reset this vector after drop
Adam Cohenb209e632012-03-27 17:09:36 -07002328 if (mode == MODE_ON_DROP || mode == MODE_ON_DROP_EXTERNAL) {
2329 mPreviousReorderDirection[0] = INVALID_DIRECTION;
2330 mPreviousReorderDirection[1] = INVALID_DIRECTION;
Adam Cohen19f37922012-03-21 11:59:11 -07002331 }
2332 } else {
2333 getDirectionVectorForDrop(pixelX, pixelY, spanX, spanY, dragView, mDirectionVector);
2334 mPreviousReorderDirection[0] = mDirectionVector[0];
2335 mPreviousReorderDirection[1] = mDirectionVector[1];
2336 }
2337
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002338 // Find a solution involving pushing / displacing any items in the way
2339 ItemConfiguration swapSolution = findReorderSolution(pixelX, pixelY, minSpanX, minSpanY,
Adam Cohen482ed822012-03-02 14:15:13 -08002340 spanX, spanY, mDirectionVector, dragView, true, new ItemConfiguration());
2341
2342 // We attempt the approach which doesn't shuffle views at all
2343 ItemConfiguration noShuffleSolution = findConfigurationNoShuffle(pixelX, pixelY, minSpanX,
2344 minSpanY, spanX, spanY, dragView, new ItemConfiguration());
2345
2346 ItemConfiguration finalSolution = null;
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002347
2348 // If the reorder solution requires resizing (shrinking) the item being dropped, we instead
2349 // favor a solution in which the item is not resized, but
Adam Cohen482ed822012-03-02 14:15:13 -08002350 if (swapSolution.isSolution && swapSolution.area() >= noShuffleSolution.area()) {
2351 finalSolution = swapSolution;
2352 } else if (noShuffleSolution.isSolution) {
2353 finalSolution = noShuffleSolution;
2354 }
2355
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002356 if (mode == MODE_SHOW_REORDER_HINT) {
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002357 if (finalSolution != null) {
Adam Cohenfe692872013-12-11 14:47:23 -08002358 beginOrAdjustReorderPreviewAnimations(finalSolution, dragView, 0,
2359 ReorderPreviewAnimation.MODE_HINT);
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07002360 result[0] = finalSolution.cellX;
2361 result[1] = finalSolution.cellY;
2362 resultSpan[0] = finalSolution.spanX;
2363 resultSpan[1] = finalSolution.spanY;
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002364 } else {
2365 result[0] = result[1] = resultSpan[0] = resultSpan[1] = -1;
2366 }
2367 return result;
2368 }
2369
Adam Cohen482ed822012-03-02 14:15:13 -08002370 boolean foundSolution = true;
2371 if (!DESTRUCTIVE_REORDER) {
2372 setUseTempCoords(true);
2373 }
2374
2375 if (finalSolution != null) {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07002376 result[0] = finalSolution.cellX;
2377 result[1] = finalSolution.cellY;
2378 resultSpan[0] = finalSolution.spanX;
2379 resultSpan[1] = finalSolution.spanY;
Adam Cohen482ed822012-03-02 14:15:13 -08002380
2381 // If we're just testing for a possible location (MODE_ACCEPT_DROP), we don't bother
2382 // committing anything or animating anything as we just want to determine if a solution
2383 // exists
2384 if (mode == MODE_DRAG_OVER || mode == MODE_ON_DROP || mode == MODE_ON_DROP_EXTERNAL) {
2385 if (!DESTRUCTIVE_REORDER) {
2386 copySolutionToTempState(finalSolution, dragView);
2387 }
2388 setItemPlacementDirty(true);
2389 animateItemsToSolution(finalSolution, dragView, mode == MODE_ON_DROP);
2390
Adam Cohen19f37922012-03-21 11:59:11 -07002391 if (!DESTRUCTIVE_REORDER &&
2392 (mode == MODE_ON_DROP || mode == MODE_ON_DROP_EXTERNAL)) {
Adam Cohen482ed822012-03-02 14:15:13 -08002393 commitTempPlacement();
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002394 completeAndClearReorderPreviewAnimations();
Adam Cohen19f37922012-03-21 11:59:11 -07002395 setItemPlacementDirty(false);
2396 } else {
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002397 beginOrAdjustReorderPreviewAnimations(finalSolution, dragView,
2398 REORDER_ANIMATION_DURATION, ReorderPreviewAnimation.MODE_PREVIEW);
Adam Cohen482ed822012-03-02 14:15:13 -08002399 }
2400 }
2401 } else {
2402 foundSolution = false;
2403 result[0] = result[1] = resultSpan[0] = resultSpan[1] = -1;
2404 }
2405
2406 if ((mode == MODE_ON_DROP || !foundSolution) && !DESTRUCTIVE_REORDER) {
2407 setUseTempCoords(false);
2408 }
Adam Cohen482ed822012-03-02 14:15:13 -08002409
Michael Jurkaa52570f2012-03-20 03:18:20 -07002410 mShortcutsAndWidgets.requestLayout();
Adam Cohen482ed822012-03-02 14:15:13 -08002411 return result;
2412 }
2413
Adam Cohen19f37922012-03-21 11:59:11 -07002414 void setItemPlacementDirty(boolean dirty) {
2415 mItemPlacementDirty = dirty;
Adam Cohen482ed822012-03-02 14:15:13 -08002416 }
Adam Cohen19f37922012-03-21 11:59:11 -07002417 boolean isItemPlacementDirty() {
2418 return mItemPlacementDirty;
Adam Cohen482ed822012-03-02 14:15:13 -08002419 }
2420
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07002421 private static class ItemConfiguration extends CellAndSpan {
Adam Cohen8baab352012-03-20 17:39:21 -07002422 HashMap<View, CellAndSpan> map = new HashMap<View, CellAndSpan>();
Adam Cohenf3900c22012-11-16 18:28:11 -08002423 private HashMap<View, CellAndSpan> savedMap = new HashMap<View, CellAndSpan>();
2424 ArrayList<View> sortedViews = new ArrayList<View>();
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002425 ArrayList<View> intersectingViews;
Adam Cohen482ed822012-03-02 14:15:13 -08002426 boolean isSolution = false;
Adam Cohen482ed822012-03-02 14:15:13 -08002427
Adam Cohenf3900c22012-11-16 18:28:11 -08002428 void save() {
2429 // Copy current state into savedMap
2430 for (View v: map.keySet()) {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07002431 savedMap.get(v).copyFrom(map.get(v));
Adam Cohenf3900c22012-11-16 18:28:11 -08002432 }
2433 }
2434
2435 void restore() {
2436 // Restore current state from savedMap
2437 for (View v: savedMap.keySet()) {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07002438 map.get(v).copyFrom(savedMap.get(v));
Adam Cohenf3900c22012-11-16 18:28:11 -08002439 }
2440 }
2441
2442 void add(View v, CellAndSpan cs) {
2443 map.put(v, cs);
2444 savedMap.put(v, new CellAndSpan());
2445 sortedViews.add(v);
2446 }
2447
Adam Cohen482ed822012-03-02 14:15:13 -08002448 int area() {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07002449 return spanX * spanY;
Adam Cohenf3900c22012-11-16 18:28:11 -08002450 }
2451
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07002452 void getBoundingRectForViews(ArrayList<View> views, Rect outRect) {
2453 boolean first = true;
2454 for (View v: views) {
2455 CellAndSpan c = map.get(v);
2456 if (first) {
2457 outRect.set(c.cellX, c.cellY, c.cellX + c.spanX, c.cellY + c.spanY);
2458 first = false;
2459 } else {
2460 outRect.union(c.cellX, c.cellY, c.cellX + c.spanX, c.cellY + c.spanY);
2461 }
2462 }
Adam Cohenf3900c22012-11-16 18:28:11 -08002463 }
Adam Cohen482ed822012-03-02 14:15:13 -08002464 }
2465
Adam Cohendf035382011-04-11 17:22:04 -07002466 /**
Adam Cohendf035382011-04-11 17:22:04 -07002467 * Find a starting cell position that will fit the given bounds nearest the requested
2468 * cell location. Uses Euclidean distance to score multiple vacant areas.
2469 *
2470 * @param pixelX The X location at which you want to search for a vacant area.
2471 * @param pixelY The Y location at which you want to search for a vacant area.
2472 * @param spanX Horizontal span of the object.
2473 * @param spanY Vertical span of the object.
2474 * @param ignoreView Considers space occupied by this view as unoccupied
2475 * @param result Previously returned value to possibly recycle.
2476 * @return The X, Y cell of a vacant area that can contain this object,
2477 * nearest the requested location.
2478 */
Adam Cohenf9c184a2016-01-15 16:47:43 -08002479 public int[] findNearestArea(int pixelX, int pixelY, int spanX, int spanY, int[] result) {
Sunny Goyalf7a29e82015-04-24 15:20:43 -07002480 return findNearestArea(pixelX, pixelY, spanX, spanY, spanX, spanY, false, result, null);
Adam Cohendf035382011-04-11 17:22:04 -07002481 }
2482
Michael Jurka0280c3b2010-09-17 15:00:07 -07002483 boolean existsEmptyCell() {
2484 return findCellForSpan(null, 1, 1);
2485 }
2486
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002487 /**
Michael Jurka0280c3b2010-09-17 15:00:07 -07002488 * Finds the upper-left coordinate of the first rectangle in the grid that can
2489 * hold a cell of the specified dimensions. If intersectX and intersectY are not -1,
2490 * then this method will only return coordinates for rectangles that contain the cell
2491 * (intersectX, intersectY)
2492 *
2493 * @param cellXY The array that will contain the position of a vacant cell if such a cell
2494 * can be found.
2495 * @param spanX The horizontal span of the cell we want to find.
2496 * @param spanY The vertical span of the cell we want to find.
2497 *
2498 * @return True if a vacant cell of the specified dimension was found, false otherwise.
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07002499 */
Hyunyoung Song3f471442015-04-08 19:01:34 -07002500 public boolean findCellForSpan(int[] cellXY, int spanX, int spanY) {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07002501 if (cellXY == null) {
2502 cellXY = new int[2];
Michael Jurka0280c3b2010-09-17 15:00:07 -07002503 }
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07002504 return mOccupied.findVacantCell(cellXY, spanX, spanY);
Michael Jurka0280c3b2010-09-17 15:00:07 -07002505 }
2506
2507 /**
Winson Chungc07918d2011-07-01 15:35:26 -07002508 * A drag event has begun over this layout.
2509 * It may have begun over this layout (in which case onDragChild is called first),
2510 * or it may have begun on another layout.
2511 */
2512 void onDragEnter() {
Winson Chungc07918d2011-07-01 15:35:26 -07002513 mDragging = true;
2514 }
2515
2516 /**
Michael Jurka0280c3b2010-09-17 15:00:07 -07002517 * Called when drag has left this CellLayout or has been completed (successfully or not)
2518 */
2519 void onDragExit() {
Joe Onorato4be866d2010-10-10 11:26:02 -07002520 // This can actually be called when we aren't in a drag, e.g. when adding a new
2521 // item to this layout via the customize drawer.
2522 // Guard against that case.
2523 if (mDragging) {
2524 mDragging = false;
Patrick Dubroyde7658b2010-09-27 11:15:43 -07002525 }
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -07002526
2527 // Invalidate the drag data
Adam Cohend41fbf52012-02-16 23:53:59 -08002528 mDragCell[0] = mDragCell[1] = -1;
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -07002529 mDragOutlineAnims[mDragOutlineCurrent].animateOut();
2530 mDragOutlineCurrent = (mDragOutlineCurrent + 1) % mDragOutlineAnims.length;
Adam Cohen19f37922012-03-21 11:59:11 -07002531 revertTempState();
Michael Jurka33945b22010-12-21 18:19:38 -08002532 setIsDragOverlapping(false);
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07002533 }
2534
2535 /**
Winson Chungaafa03c2010-06-11 17:34:16 -07002536 * Mark a child as having been dropped.
Patrick Dubroyde7658b2010-09-27 11:15:43 -07002537 * At the beginning of the drag operation, the child may have been on another
Patrick Dubroyce34a972010-10-19 10:34:32 -07002538 * screen, but it is re-parented before this method is called.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002539 *
2540 * @param child The child that is being dropped
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002541 */
Adam Cohen716b51e2011-06-30 12:09:54 -07002542 void onDropChild(View child) {
Romain Guyd94533d2009-08-17 10:01:15 -07002543 if (child != null) {
2544 LayoutParams lp = (LayoutParams) child.getLayoutParams();
Romain Guy84f296c2009-11-04 15:00:44 -08002545 lp.dropped = true;
Romain Guyd94533d2009-08-17 10:01:15 -07002546 child.requestLayout();
Tony Wickham1cdb6d02015-09-17 11:08:27 -07002547 markCellsAsOccupiedForView(child);
Romain Guyd94533d2009-08-17 10:01:15 -07002548 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002549 }
2550
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002551 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002552 * Computes a bounding rectangle for a range of cells
Winson Chungaafa03c2010-06-11 17:34:16 -07002553 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002554 * @param cellX X coordinate of upper left corner expressed as a cell position
2555 * @param cellY Y coordinate of upper left corner expressed as a cell position
Winson Chungaafa03c2010-06-11 17:34:16 -07002556 * @param cellHSpan Width in cells
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002557 * @param cellVSpan Height in cells
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07002558 * @param resultRect Rect into which to put the results
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002559 */
Adam Cohend41fbf52012-02-16 23:53:59 -08002560 public void cellToRect(int cellX, int cellY, int cellHSpan, int cellVSpan, Rect resultRect) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002561 final int cellWidth = mCellWidth;
2562 final int cellHeight = mCellHeight;
Winson Chungaafa03c2010-06-11 17:34:16 -07002563
Winson Chung4b825dcd2011-06-19 12:41:22 -07002564 final int hStartPadding = getPaddingLeft();
2565 final int vStartPadding = getPaddingTop();
Winson Chungaafa03c2010-06-11 17:34:16 -07002566
Sunny Goyalaa8a8712016-11-20 15:26:01 +05302567 int width = cellHSpan * cellWidth;
2568 int height = cellVSpan * cellHeight;
2569 int x = hStartPadding + cellX * cellWidth;
2570 int y = vStartPadding + cellY * cellHeight;
Winson Chungaafa03c2010-06-11 17:34:16 -07002571
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07002572 resultRect.set(x, y, x + width, y + height);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002573 }
Winson Chungaafa03c2010-06-11 17:34:16 -07002574
Adam Cohend4844c32011-02-18 19:25:06 -08002575 public void markCellsAsOccupiedForView(View view) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07002576 if (view == null || view.getParent() != mShortcutsAndWidgets) return;
Michael Jurka0280c3b2010-09-17 15:00:07 -07002577 LayoutParams lp = (LayoutParams) view.getLayoutParams();
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07002578 mOccupied.markCells(lp.cellX, lp.cellY, lp.cellHSpan, lp.cellVSpan, true);
Michael Jurka0280c3b2010-09-17 15:00:07 -07002579 }
2580
Adam Cohend4844c32011-02-18 19:25:06 -08002581 public void markCellsAsUnoccupiedForView(View view) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07002582 if (view == null || view.getParent() != mShortcutsAndWidgets) return;
Michael Jurka0280c3b2010-09-17 15:00:07 -07002583 LayoutParams lp = (LayoutParams) view.getLayoutParams();
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07002584 mOccupied.markCells(lp.cellX, lp.cellY, lp.cellHSpan, lp.cellVSpan, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002585 }
2586
Adam Cohen2801caf2011-05-13 20:57:39 -07002587 public int getDesiredWidth() {
Sunny Goyalaa8a8712016-11-20 15:26:01 +05302588 return getPaddingLeft() + getPaddingRight() + (mCountX * mCellWidth);
Adam Cohen2801caf2011-05-13 20:57:39 -07002589 }
2590
2591 public int getDesiredHeight() {
Sunny Goyalaa8a8712016-11-20 15:26:01 +05302592 return getPaddingTop() + getPaddingBottom() + (mCountY * mCellHeight);
Adam Cohen2801caf2011-05-13 20:57:39 -07002593 }
2594
Michael Jurka66d72172011-04-12 16:29:25 -07002595 public boolean isOccupied(int x, int y) {
2596 if (x < mCountX && y < mCountY) {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07002597 return mOccupied.cells[x][y];
Michael Jurka66d72172011-04-12 16:29:25 -07002598 } else {
2599 throw new RuntimeException("Position exceeds the bound of this CellLayout");
2600 }
2601 }
2602
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002603 @Override
2604 public ViewGroup.LayoutParams generateLayoutParams(AttributeSet attrs) {
2605 return new CellLayout.LayoutParams(getContext(), attrs);
2606 }
2607
2608 @Override
2609 protected boolean checkLayoutParams(ViewGroup.LayoutParams p) {
2610 return p instanceof CellLayout.LayoutParams;
2611 }
2612
2613 @Override
2614 protected ViewGroup.LayoutParams generateLayoutParams(ViewGroup.LayoutParams p) {
2615 return new CellLayout.LayoutParams(p);
2616 }
2617
2618 public static class LayoutParams extends ViewGroup.MarginLayoutParams {
2619 /**
2620 * Horizontal location of the item in the grid.
2621 */
2622 @ViewDebug.ExportedProperty
2623 public int cellX;
2624
2625 /**
2626 * Vertical location of the item in the grid.
2627 */
2628 @ViewDebug.ExportedProperty
2629 public int cellY;
2630
2631 /**
Adam Cohen482ed822012-03-02 14:15:13 -08002632 * Temporary horizontal location of the item in the grid during reorder
2633 */
2634 public int tmpCellX;
2635
2636 /**
2637 * Temporary vertical location of the item in the grid during reorder
2638 */
2639 public int tmpCellY;
2640
2641 /**
2642 * Indicates that the temporary coordinates should be used to layout the items
2643 */
2644 public boolean useTmpCoords;
2645
2646 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002647 * Number of cells spanned horizontally by the item.
2648 */
2649 @ViewDebug.ExportedProperty
2650 public int cellHSpan;
2651
2652 /**
2653 * Number of cells spanned vertically by the item.
2654 */
2655 @ViewDebug.ExportedProperty
2656 public int cellVSpan;
Winson Chungaafa03c2010-06-11 17:34:16 -07002657
Adam Cohen1b607ed2011-03-03 17:26:50 -08002658 /**
2659 * Indicates whether the item will set its x, y, width and height parameters freely,
2660 * or whether these will be computed based on cellX, cellY, cellHSpan and cellVSpan.
2661 */
Adam Cohend4844c32011-02-18 19:25:06 -08002662 public boolean isLockedToGrid = true;
2663
Adam Cohen482ed822012-03-02 14:15:13 -08002664 /**
Adam Cohenec40b2b2013-07-23 15:52:40 -07002665 * Indicates that this item should use the full extents of its parent.
2666 */
2667 public boolean isFullscreen = false;
2668
2669 /**
Adam Cohen482ed822012-03-02 14:15:13 -08002670 * Indicates whether this item can be reordered. Always true except in the case of the
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07002671 * the AllApps button and QSB place holder.
Adam Cohen482ed822012-03-02 14:15:13 -08002672 */
2673 public boolean canReorder = true;
2674
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002675 // X coordinate of the view in the layout.
2676 @ViewDebug.ExportedProperty
Vadim Tryshevfedca432015-08-19 17:55:02 -07002677 public int x;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002678 // Y coordinate of the view in the layout.
2679 @ViewDebug.ExportedProperty
Vadim Tryshevfedca432015-08-19 17:55:02 -07002680 public int y;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002681
Romain Guy84f296c2009-11-04 15:00:44 -08002682 boolean dropped;
Romain Guyfcb9e712009-10-02 16:06:52 -07002683
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002684 public LayoutParams(Context c, AttributeSet attrs) {
2685 super(c, attrs);
2686 cellHSpan = 1;
2687 cellVSpan = 1;
2688 }
2689
2690 public LayoutParams(ViewGroup.LayoutParams source) {
2691 super(source);
2692 cellHSpan = 1;
2693 cellVSpan = 1;
2694 }
Winson Chungaafa03c2010-06-11 17:34:16 -07002695
2696 public LayoutParams(LayoutParams source) {
2697 super(source);
2698 this.cellX = source.cellX;
2699 this.cellY = source.cellY;
2700 this.cellHSpan = source.cellHSpan;
2701 this.cellVSpan = source.cellVSpan;
2702 }
2703
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002704 public LayoutParams(int cellX, int cellY, int cellHSpan, int cellVSpan) {
Romain Guy8f19cdd2010-01-08 15:07:00 -08002705 super(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002706 this.cellX = cellX;
2707 this.cellY = cellY;
2708 this.cellHSpan = cellHSpan;
2709 this.cellVSpan = cellVSpan;
2710 }
2711
Sunny Goyalaa8a8712016-11-20 15:26:01 +05302712 public void setup(int cellWidth, int cellHeight, boolean invertHorizontally, int colCount) {
Jon Miranda7ae64ff2016-11-21 16:18:46 -08002713 setup(cellWidth, cellHeight, invertHorizontally, colCount, 1.0f, 1.0f);
2714 }
2715
2716 /**
2717 * Use this method, as opposed to {@link #setup(int, int, boolean, int)}, if the view needs
2718 * to be scaled.
2719 *
2720 * ie. In multi-window mode, we setup widgets so that they are measured and laid out
2721 * using their full/invariant device profile sizes.
2722 */
2723 public void setup(int cellWidth, int cellHeight, boolean invertHorizontally, int colCount,
2724 float cellScaleX, float cellScaleY) {
Adam Cohend4844c32011-02-18 19:25:06 -08002725 if (isLockedToGrid) {
2726 final int myCellHSpan = cellHSpan;
2727 final int myCellVSpan = cellVSpan;
Adam Cohen2374abf2013-04-16 14:56:57 -07002728 int myCellX = useTmpCoords ? tmpCellX : cellX;
2729 int myCellY = useTmpCoords ? tmpCellY : cellY;
2730
2731 if (invertHorizontally) {
2732 myCellX = colCount - myCellX - cellHSpan;
2733 }
Adam Cohen1b607ed2011-03-03 17:26:50 -08002734
Jon Miranda7ae64ff2016-11-21 16:18:46 -08002735 width = (int) (myCellHSpan * cellWidth / cellScaleX - leftMargin - rightMargin);
2736 height = (int) (myCellVSpan * cellHeight / cellScaleY - topMargin - bottomMargin);
Sunny Goyalaa8a8712016-11-20 15:26:01 +05302737 x = (myCellX * cellWidth + leftMargin);
2738 y = (myCellY * cellHeight + topMargin);
Adam Cohend4844c32011-02-18 19:25:06 -08002739 }
2740 }
Winson Chungaafa03c2010-06-11 17:34:16 -07002741
Winson Chungaafa03c2010-06-11 17:34:16 -07002742 public String toString() {
2743 return "(" + this.cellX + ", " + this.cellY + ")";
2744 }
Adam Cohen7f4eabe2011-04-21 16:19:16 -07002745
2746 public void setWidth(int width) {
2747 this.width = width;
2748 }
2749
2750 public int getWidth() {
2751 return width;
2752 }
2753
2754 public void setHeight(int height) {
2755 this.height = height;
2756 }
2757
2758 public int getHeight() {
2759 return height;
2760 }
2761
2762 public void setX(int x) {
2763 this.x = x;
2764 }
2765
2766 public int getX() {
2767 return x;
2768 }
2769
2770 public void setY(int y) {
2771 this.y = y;
2772 }
2773
2774 public int getY() {
2775 return y;
2776 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002777 }
2778
Michael Jurka0280c3b2010-09-17 15:00:07 -07002779 // This class stores info for two purposes:
2780 // 1. When dragging items (mDragInfo in Workspace), we store the View, its cellX & cellY,
2781 // its spanX, spanY, and the screen it is on
2782 // 2. When long clicking on an empty cell in a CellLayout, we save information about the
2783 // cellX and cellY coordinates and which page was clicked. We then set this as a tag on
2784 // the CellLayout that was long clicked
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07002785 public static final class CellInfo extends CellAndSpan {
Adam Cohenf9c184a2016-01-15 16:47:43 -08002786 public View cell;
Adam Cohendcd297f2013-06-18 13:13:40 -07002787 long screenId;
Winson Chung3d503fb2011-07-13 17:25:49 -07002788 long container;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002789
Sunny Goyal83a8f042015-05-19 12:52:12 -07002790 public CellInfo(View v, ItemInfo info) {
Adam Cohene0aaa0d2014-05-12 12:44:22 -07002791 cellX = info.cellX;
2792 cellY = info.cellY;
2793 spanX = info.spanX;
2794 spanY = info.spanY;
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07002795 cell = v;
Adam Cohene0aaa0d2014-05-12 12:44:22 -07002796 screenId = info.screenId;
2797 container = info.container;
2798 }
2799
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002800 @Override
2801 public String toString() {
Winson Chungaafa03c2010-06-11 17:34:16 -07002802 return "Cell[view=" + (cell == null ? "null" : cell.getClass())
2803 + ", x=" + cellX + ", y=" + cellY + "]";
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002804 }
2805 }
Michael Jurkad771c962011-08-09 15:00:48 -07002806
Tony Wickham86930612015-09-09 13:50:40 -07002807 /**
2808 * Returns whether an item can be placed in this CellLayout (after rearranging and/or resizing
2809 * if necessary).
2810 */
2811 public boolean hasReorderSolution(ItemInfo itemInfo) {
2812 int[] cellPoint = new int[2];
2813 // Check for a solution starting at every cell.
2814 for (int cellX = 0; cellX < getCountX(); cellX++) {
2815 for (int cellY = 0; cellY < getCountY(); cellY++) {
2816 cellToPoint(cellX, cellY, cellPoint);
2817 if (findReorderSolution(cellPoint[0], cellPoint[1], itemInfo.minSpanX,
2818 itemInfo.minSpanY, itemInfo.spanX, itemInfo.spanY, mDirectionVector, null,
2819 true, new ItemConfiguration()).isSolution) {
2820 return true;
2821 }
2822 }
2823 }
2824 return false;
2825 }
2826
Sunny Goyal9ca9c132015-04-29 14:57:22 -07002827 public boolean isRegionVacant(int x, int y, int spanX, int spanY) {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07002828 return mOccupied.isRegionVacant(x, y, spanX, spanY);
Sunny Goyal9ca9c132015-04-29 14:57:22 -07002829 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002830}