blob: e0fcbf04d5dafcad9cd161cd86779b90b4b0aca9 [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;
Adam Cohen091440a2015-03-18 14:16:05 -070060import com.android.launcher3.util.Thunk;
Patrick Dubroy8e58e912010-10-14 13:21:48 -070061
Sunny Goyalc13403c2016-11-18 23:44:48 -080062import java.lang.annotation.Retention;
63import java.lang.annotation.RetentionPolicy;
Adam Cohen69ce2e52011-07-03 19:25:21 -070064import java.util.ArrayList;
Adam Cohenc0dcf592011-06-01 15:30:43 -070065import java.util.Arrays;
Adam Cohenf3900c22012-11-16 18:28:11 -080066import java.util.Collections;
67import java.util.Comparator;
Adam Cohenbfbfd262011-06-13 16:55:12 -070068import java.util.HashMap;
Adam Cohend41fbf52012-02-16 23:53:59 -080069import java.util.Stack;
Adam Cohenc0dcf592011-06-01 15:30:43 -070070
Sunny Goyal4b6eb262015-05-14 19:24:40 -070071public class CellLayout extends ViewGroup implements BubbleTextShadowHandler {
Sunny Goyale9b651e2015-04-24 11:44:51 -070072 public static final int WORKSPACE_ACCESSIBILITY_DRAG = 2;
73 public static final int FOLDER_ACCESSIBILITY_DRAG = 1;
74
Tony Wickhama0628cc2015-10-14 15:23:04 -070075 private static final String TAG = "CellLayout";
76 private static final boolean LOGD = false;
Winson Chungaafa03c2010-06-11 17:34:16 -070077
Adam Cohen2acce882012-03-28 19:03:19 -070078 private Launcher mLauncher;
Sunny Goyal4ffec482016-02-09 11:28:52 -080079 @ViewDebug.ExportedProperty(category = "launcher")
Adam Cohen091440a2015-03-18 14:16:05 -070080 @Thunk int mCellWidth;
Sunny Goyal4ffec482016-02-09 11:28:52 -080081 @ViewDebug.ExportedProperty(category = "launcher")
Adam Cohen091440a2015-03-18 14:16:05 -070082 @Thunk int mCellHeight;
Winson Chung11a1a532013-09-13 11:14:45 -070083 private int mFixedCellWidth;
84 private int mFixedCellHeight;
Winson Chungaafa03c2010-06-11 17:34:16 -070085
Sunny Goyal4ffec482016-02-09 11:28:52 -080086 @ViewDebug.ExportedProperty(category = "launcher")
Sunny Goyalff4ba2d2016-04-02 14:12:34 -070087 private int mCountX;
Sunny Goyal4ffec482016-02-09 11:28:52 -080088 @ViewDebug.ExportedProperty(category = "launcher")
Sunny Goyalff4ba2d2016-04-02 14:12:34 -070089 private int mCountY;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080090
Adam Cohen917e3882013-10-31 15:03:35 -070091 private boolean mDropPending = false;
Adam Cohenc50438c2014-08-19 17:43:05 -070092 private boolean mIsDragTarget = true;
Sunny Goyale2fd14b2015-08-27 17:45:46 -070093 private boolean mJailContent = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080094
Patrick Dubroyde7658b2010-09-27 11:15:43 -070095 // These are temporary variables to prevent having to allocate a new object just to
96 // return an (x, y) value from helper functions. Do NOT use them to maintain other state.
Adam Cohen091440a2015-03-18 14:16:05 -070097 @Thunk final int[] mTmpPoint = new int[2];
Sunny Goyal2805e632015-05-20 15:35:32 -070098 @Thunk final int[] mTempLocation = new int[2];
Patrick Dubroy6569f2c2010-07-12 14:25:18 -070099
Sunny Goyalff4ba2d2016-04-02 14:12:34 -0700100 private GridOccupancy mOccupied;
101 private GridOccupancy mTmpOccupied;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800102
Michael Jurkadee05892010-07-27 10:01:56 -0700103 private OnTouchListener mInterceptTouchListener;
Mady Melloref044dd2015-06-02 15:35:07 -0700104 private StylusEventHelper mStylusEventHelper;
Michael Jurkadee05892010-07-27 10:01:56 -0700105
Adam Cohenefca0272016-02-24 19:19:06 -0800106 private ArrayList<FolderIcon.PreviewBackground> mFolderBackgrounds = new ArrayList<FolderIcon.PreviewBackground>();
107 FolderIcon.PreviewBackground mFolderLeaveBehind = new FolderIcon.PreviewBackground();
108 Paint mFolderBgPaint = new Paint();
Adam Cohen69ce2e52011-07-03 19:25:21 -0700109
Michael Jurka5f1c5092010-09-03 14:15:02 -0700110 private float mBackgroundAlpha;
Adam Cohenf34bab52010-09-30 14:11:56 -0700111
Sunny Goyalf5440cb2016-12-14 15:13:00 -0800112 private static final int[] BACKGROUND_STATE_ACTIVE = new int[] { android.R.attr.state_active };
113 private static final int[] BACKGROUND_STATE_DEFAULT = new int[0];
114 private final Drawable mBackground;
Sunny Goyal2805e632015-05-20 15:35:32 -0700115
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700116 // These values allow a fixed measurement to be set on the CellLayout.
117 private int mFixedWidth = -1;
118 private int mFixedHeight = -1;
119
Michael Jurka33945b22010-12-21 18:19:38 -0800120 // If we're actively dragging something over this screen, mIsDragOverlapping is true
121 private boolean mIsDragOverlapping = false;
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700122
Winson Chung150fbab2010-09-29 17:14:26 -0700123 // These arrays are used to implement the drag visualization on x-large screens.
Joe Onorato4be866d2010-10-10 11:26:02 -0700124 // They are used as circular arrays, indexed by mDragOutlineCurrent.
Adam Cohen091440a2015-03-18 14:16:05 -0700125 @Thunk Rect[] mDragOutlines = new Rect[4];
126 @Thunk float[] mDragOutlineAlphas = new float[mDragOutlines.length];
Joe Onorato4be866d2010-10-10 11:26:02 -0700127 private InterruptibleInOutAnimator[] mDragOutlineAnims =
128 new InterruptibleInOutAnimator[mDragOutlines.length];
Winson Chung150fbab2010-09-29 17:14:26 -0700129
130 // Used as an index into the above 3 arrays; indicates which is the most current value.
Joe Onorato4be866d2010-10-10 11:26:02 -0700131 private int mDragOutlineCurrent = 0;
Patrick Dubroy8e58e912010-10-14 13:21:48 -0700132 private final Paint mDragOutlinePaint = new Paint();
Winson Chung150fbab2010-09-29 17:14:26 -0700133
Sunny Goyal4fe5a372015-05-14 19:55:10 -0700134 private final ClickShadowView mTouchFeedbackView;
Patrick Dubroy96864c32011-03-10 17:17:23 -0800135
Sunny Goyal316490e2015-06-02 09:38:28 -0700136 @Thunk HashMap<CellLayout.LayoutParams, Animator> mReorderAnimators = new HashMap<>();
137 @Thunk HashMap<View, ReorderPreviewAnimation> mShakeAnimators = new HashMap<>();
Adam Cohen19f37922012-03-21 11:59:11 -0700138
139 private boolean mItemPlacementDirty = false;
Adam Cohenbfbfd262011-06-13 16:55:12 -0700140
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700141 // When a drag operation is in progress, holds the nearest cell to the touch point
142 private final int[] mDragCell = new int[2];
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800143
Joe Onorato4be866d2010-10-10 11:26:02 -0700144 private boolean mDragging = false;
145
Patrick Dubroyce34a972010-10-19 10:34:32 -0700146 private TimeInterpolator mEaseOutInterpolator;
Michael Jurkaa52570f2012-03-20 03:18:20 -0700147 private ShortcutAndWidgetContainer mShortcutsAndWidgets;
Patrick Dubroyce34a972010-10-19 10:34:32 -0700148
Sunny Goyalc13403c2016-11-18 23:44:48 -0800149 @Retention(RetentionPolicy.SOURCE)
150 @IntDef({WORKSPACE, HOTSEAT, FOLDER})
151 public @interface ContainerType{}
152 public static final int WORKSPACE = 0;
153 public static final int HOTSEAT = 1;
154 public static final int FOLDER = 2;
155
156 @ContainerType private final int mContainerType;
157
158 private final float mChildScale;
Andrew Flynn0dca1ec2012-02-29 13:33:22 -0800159
Adam Cohenfa3c58f2013-12-06 16:10:55 -0800160 public static final int MODE_SHOW_REORDER_HINT = 0;
161 public static final int MODE_DRAG_OVER = 1;
162 public static final int MODE_ON_DROP = 2;
163 public static final int MODE_ON_DROP_EXTERNAL = 3;
164 public static final int MODE_ACCEPT_DROP = 4;
Adam Cohen19f37922012-03-21 11:59:11 -0700165 private static final boolean DESTRUCTIVE_REORDER = false;
Adam Cohen482ed822012-03-02 14:15:13 -0800166 private static final boolean DEBUG_VISUALIZE_OCCUPIED = false;
167
Adam Cohenfa3c58f2013-12-06 16:10:55 -0800168 private static final float REORDER_PREVIEW_MAGNITUDE = 0.12f;
Adam Cohen19f37922012-03-21 11:59:11 -0700169 private static final int REORDER_ANIMATION_DURATION = 150;
Sunny Goyalc13403c2016-11-18 23:44:48 -0800170 @Thunk final float mReorderPreviewAnimationMagnitude;
Adam Cohen19f37922012-03-21 11:59:11 -0700171
Adam Cohen482ed822012-03-02 14:15:13 -0800172 private ArrayList<View> mIntersectingViews = new ArrayList<View>();
173 private Rect mOccupiedRect = new Rect();
174 private int[] mDirectionVector = new int[2];
Adam Cohen19f37922012-03-21 11:59:11 -0700175 int[] mPreviousReorderDirection = new int[2];
Adam Cohenb209e632012-03-27 17:09:36 -0700176 private static final int INVALID_DIRECTION = -100;
Adam Cohen482ed822012-03-02 14:15:13 -0800177
Sunny Goyal2805e632015-05-20 15:35:32 -0700178 private final Rect mTempRect = new Rect();
Winson Chung3a6e7f32013-10-09 15:50:52 -0700179
Michael Jurkaca993832012-06-29 15:17:04 -0700180 private final static Paint sPaint = new Paint();
Romain Guy8a0bff52012-05-06 13:14:33 -0700181
Adam Cohenc9735cf2015-01-23 16:11:55 -0800182 // Related to accessible drag and drop
Sunny Goyale9b651e2015-04-24 11:44:51 -0700183 private DragAndDropAccessibilityDelegate mTouchHelper;
Adam Cohenc9735cf2015-01-23 16:11:55 -0800184 private boolean mUseTouchHelper = false;
Adam Cohenc9735cf2015-01-23 16:11:55 -0800185
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800186 public CellLayout(Context context) {
187 this(context, null);
188 }
189
190 public CellLayout(Context context, AttributeSet attrs) {
191 this(context, attrs, 0);
192 }
193
194 public CellLayout(Context context, AttributeSet attrs, int defStyle) {
195 super(context, attrs, defStyle);
Sunny Goyalc13403c2016-11-18 23:44:48 -0800196 TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.CellLayout, defStyle, 0);
197 mContainerType = a.getInteger(R.styleable.CellLayout_containerType, WORKSPACE);
198 a.recycle();
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700199
200 // A ViewGroup usually does not draw, but CellLayout needs to draw a rectangle to show
201 // the user where a dragged item will land when dropped.
202 setWillNotDraw(false);
Romain Guyce3cbd12013-02-25 15:00:36 -0800203 setClipToPadding(false);
Tony2fd02082016-10-07 12:50:01 -0700204 mLauncher = Launcher.getLauncher(context);
Michael Jurkaa63c4522010-08-19 13:52:27 -0700205
Adam Cohen2e6da152015-05-06 11:42:25 -0700206 DeviceProfile grid = mLauncher.getDeviceProfile();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800207
Winson Chung11a1a532013-09-13 11:14:45 -0700208 mCellWidth = mCellHeight = -1;
Nilesh Agrawal5f7099a2014-01-02 15:54:57 -0800209 mFixedCellWidth = mFixedCellHeight = -1;
Sunny Goyalff4ba2d2016-04-02 14:12:34 -0700210
211 mCountX = grid.inv.numColumns;
212 mCountY = grid.inv.numRows;
213 mOccupied = new GridOccupancy(mCountX, mCountY);
214 mTmpOccupied = new GridOccupancy(mCountX, mCountY);
215
Adam Cohen5b53f292012-03-29 14:30:35 -0700216 mPreviousReorderDirection[0] = INVALID_DIRECTION;
217 mPreviousReorderDirection[1] = INVALID_DIRECTION;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800218
Adam Cohenefca0272016-02-24 19:19:06 -0800219 mFolderLeaveBehind.delegateCellX = -1;
220 mFolderLeaveBehind.delegateCellY = -1;
221
Sunny Goyalc13403c2016-11-18 23:44:48 -0800222 mChildScale = mContainerType == HOTSEAT ? grid.inv.hotseatScale : 1f;
223
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800224 setAlwaysDrawnWithCacheEnabled(false);
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700225 final Resources res = getResources();
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700226
Sunny Goyalf5440cb2016-12-14 15:13:00 -0800227 mBackground = res.getDrawable(R.drawable.bg_celllayout);
Sunny Goyal2805e632015-05-20 15:35:32 -0700228 mBackground.setCallback(this);
Winson Chunge8f1d042015-07-31 12:39:57 -0700229 mBackground.setAlpha((int) (mBackgroundAlpha * 255));
Michael Jurka33945b22010-12-21 18:19:38 -0800230
Sunny Goyalc13403c2016-11-18 23:44:48 -0800231 mReorderPreviewAnimationMagnitude = (REORDER_PREVIEW_MAGNITUDE * grid.iconSizePx);
Adam Cohen19f37922012-03-21 11:59:11 -0700232
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700233 // Initialize the data structures used for the drag visualization.
Patrick Dubroyce34a972010-10-19 10:34:32 -0700234 mEaseOutInterpolator = new DecelerateInterpolator(2.5f); // Quint ease out
Winson Chungb8c69f32011-10-19 21:36:08 -0700235 mDragCell[0] = mDragCell[1] = -1;
Joe Onorato4be866d2010-10-10 11:26:02 -0700236 for (int i = 0; i < mDragOutlines.length; i++) {
Adam Cohend41fbf52012-02-16 23:53:59 -0800237 mDragOutlines[i] = new Rect(-1, -1, -1, -1);
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700238 }
Sunny Goyalf28e6af2016-08-31 16:02:40 -0700239 mDragOutlinePaint.setColor(getResources().getColor(R.color.outline_color));
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700240
241 // When dragging things around the home screens, we show a green outline of
242 // where the item will land. The outlines gradually fade out, leaving a trail
243 // behind the drag path.
244 // Set up all the animations that are used to implement this fading.
245 final int duration = res.getInteger(R.integer.config_dragOutlineFadeTime);
Chet Haase472b2812010-10-14 07:02:04 -0700246 final float fromAlphaValue = 0;
247 final float toAlphaValue = (float)res.getInteger(R.integer.config_dragOutlineMaxAlpha);
Joe Onorato4be866d2010-10-10 11:26:02 -0700248
Patrick Dubroy8e58e912010-10-14 13:21:48 -0700249 Arrays.fill(mDragOutlineAlphas, fromAlphaValue);
Joe Onorato4be866d2010-10-10 11:26:02 -0700250
251 for (int i = 0; i < mDragOutlineAnims.length; i++) {
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700252 final InterruptibleInOutAnimator anim =
Michael Jurkaf1ad6082013-03-13 12:55:46 +0100253 new InterruptibleInOutAnimator(this, duration, fromAlphaValue, toAlphaValue);
Patrick Dubroyce34a972010-10-19 10:34:32 -0700254 anim.getAnimator().setInterpolator(mEaseOutInterpolator);
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700255 final int thisIndex = i;
Chet Haase472b2812010-10-14 07:02:04 -0700256 anim.getAnimator().addUpdateListener(new AnimatorUpdateListener() {
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700257 public void onAnimationUpdate(ValueAnimator animation) {
Joe Onorato4be866d2010-10-10 11:26:02 -0700258 final Bitmap outline = (Bitmap)anim.getTag();
259
260 // If an animation is started and then stopped very quickly, we can still
261 // get spurious updates we've cleared the tag. Guard against this.
262 if (outline == null) {
Tony Wickhama0628cc2015-10-14 15:23:04 -0700263 if (LOGD) {
Patrick Dubroyfe6bd872010-10-13 17:32:10 -0700264 Object val = animation.getAnimatedValue();
265 Log.d(TAG, "anim " + thisIndex + " update: " + val +
266 ", isStopped " + anim.isStopped());
267 }
Joe Onorato4be866d2010-10-10 11:26:02 -0700268 // Try to prevent it from continuing to run
269 animation.cancel();
270 } else {
Chet Haase472b2812010-10-14 07:02:04 -0700271 mDragOutlineAlphas[thisIndex] = (Float) animation.getAnimatedValue();
Adam Cohend41fbf52012-02-16 23:53:59 -0800272 CellLayout.this.invalidate(mDragOutlines[thisIndex]);
Joe Onorato4be866d2010-10-10 11:26:02 -0700273 }
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700274 }
275 });
Joe Onorato4be866d2010-10-10 11:26:02 -0700276 // The animation holds a reference to the drag outline bitmap as long is it's
277 // running. This way the bitmap can be GCed when the animations are complete.
Chet Haase472b2812010-10-14 07:02:04 -0700278 anim.getAnimator().addListener(new AnimatorListenerAdapter() {
Michael Jurka3c4c20f2010-10-28 15:36:06 -0700279 @Override
Joe Onorato4be866d2010-10-10 11:26:02 -0700280 public void onAnimationEnd(Animator animation) {
Chet Haase472b2812010-10-14 07:02:04 -0700281 if ((Float) ((ValueAnimator) animation).getAnimatedValue() == 0f) {
Joe Onorato4be866d2010-10-10 11:26:02 -0700282 anim.setTag(null);
283 }
284 }
285 });
286 mDragOutlineAnims[i] = anim;
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700287 }
Patrick Dubroyce34a972010-10-19 10:34:32 -0700288
Sunny Goyalc13403c2016-11-18 23:44:48 -0800289 mShortcutsAndWidgets = new ShortcutAndWidgetContainer(context, mContainerType);
Sunny Goyalaa8a8712016-11-20 15:26:01 +0530290 mShortcutsAndWidgets.setCellDimensions(mCellWidth, mCellHeight, mCountX, mCountY);
Adam Cohen2374abf2013-04-16 14:56:57 -0700291
Mady Mellorbb835202015-07-15 16:34:34 -0700292 mStylusEventHelper = new StylusEventHelper(new SimpleOnStylusPressListener(this), this);
Mady Melloref044dd2015-06-02 15:35:07 -0700293
Sunny Goyal4fe5a372015-05-14 19:55:10 -0700294 mTouchFeedbackView = new ClickShadowView(context);
295 addView(mTouchFeedbackView);
Michael Jurkaa52570f2012-03-20 03:18:20 -0700296 addView(mShortcutsAndWidgets);
Michael Jurka18014792010-10-14 09:01:34 -0700297 }
298
Sunny Goyale9b651e2015-04-24 11:44:51 -0700299 public void enableAccessibleDrag(boolean enable, int dragType) {
Adam Cohenc9735cf2015-01-23 16:11:55 -0800300 mUseTouchHelper = enable;
301 if (!enable) {
302 ViewCompat.setAccessibilityDelegate(this, null);
303 setImportantForAccessibility(IMPORTANT_FOR_ACCESSIBILITY_NO);
304 getShortcutsAndWidgets().setImportantForAccessibility(IMPORTANT_FOR_ACCESSIBILITY_NO);
305 setOnClickListener(mLauncher);
306 } else {
Sunny Goyale9b651e2015-04-24 11:44:51 -0700307 if (dragType == WORKSPACE_ACCESSIBILITY_DRAG &&
308 !(mTouchHelper instanceof WorkspaceAccessibilityHelper)) {
309 mTouchHelper = new WorkspaceAccessibilityHelper(this);
310 } else if (dragType == FOLDER_ACCESSIBILITY_DRAG &&
311 !(mTouchHelper instanceof FolderAccessibilityHelper)) {
312 mTouchHelper = new FolderAccessibilityHelper(this);
313 }
Adam Cohenc9735cf2015-01-23 16:11:55 -0800314 ViewCompat.setAccessibilityDelegate(this, mTouchHelper);
315 setImportantForAccessibility(IMPORTANT_FOR_ACCESSIBILITY_YES);
316 getShortcutsAndWidgets().setImportantForAccessibility(IMPORTANT_FOR_ACCESSIBILITY_YES);
317 setOnClickListener(mTouchHelper);
318 }
319
320 // Invalidate the accessibility hierarchy
321 if (getParent() != null) {
322 getParent().notifySubtreeAccessibilityStateChanged(
323 this, this, AccessibilityEvent.CONTENT_CHANGE_TYPE_SUBTREE);
324 }
325 }
326
327 @Override
328 public boolean dispatchHoverEvent(MotionEvent event) {
329 // Always attempt to dispatch hover events to accessibility first.
330 if (mUseTouchHelper && mTouchHelper.dispatchHoverEvent(event)) {
331 return true;
332 }
333 return super.dispatchHoverEvent(event);
334 }
335
336 @Override
Adam Cohenc9735cf2015-01-23 16:11:55 -0800337 public boolean onInterceptTouchEvent(MotionEvent ev) {
338 if (mUseTouchHelper ||
339 (mInterceptTouchListener != null && mInterceptTouchListener.onTouch(this, ev))) {
340 return true;
341 }
342 return false;
343 }
344
Mady Melloref044dd2015-06-02 15:35:07 -0700345 @Override
346 public boolean onTouchEvent(MotionEvent ev) {
347 boolean handled = super.onTouchEvent(ev);
348 // Stylus button press on a home screen should not switch between overview mode and
349 // the home screen mode, however, once in overview mode stylus button press should be
350 // enabled to allow rearranging the different home screens. So check what mode
351 // the workspace is in, and only perform stylus button presses while in overview mode.
352 if (mLauncher.mWorkspace.isInOverviewMode()
Mady Mellorbb835202015-07-15 16:34:34 -0700353 && mStylusEventHelper.onMotionEvent(ev)) {
Mady Melloref044dd2015-06-02 15:35:07 -0700354 return true;
355 }
356 return handled;
357 }
358
Chris Craik01f2d7f2013-10-01 14:41:56 -0700359 public void enableHardwareLayer(boolean hasLayer) {
360 mShortcutsAndWidgets.setLayerType(hasLayer ? LAYER_TYPE_HARDWARE : LAYER_TYPE_NONE, sPaint);
Michael Jurkad51f33a2012-06-28 15:35:26 -0700361 }
362
363 public void buildHardwareLayer() {
364 mShortcutsAndWidgets.buildLayer();
Adam Cohen2801caf2011-05-13 20:57:39 -0700365 }
366
Winson Chung5f8afe62013-08-12 16:19:28 -0700367 public void setCellDimensions(int width, int height) {
Winson Chung11a1a532013-09-13 11:14:45 -0700368 mFixedCellWidth = mCellWidth = width;
369 mFixedCellHeight = mCellHeight = height;
Sunny Goyalaa8a8712016-11-20 15:26:01 +0530370 mShortcutsAndWidgets.setCellDimensions(mCellWidth, mCellHeight, mCountX, mCountY);
Winson Chung5f8afe62013-08-12 16:19:28 -0700371 }
372
Adam Cohen2801caf2011-05-13 20:57:39 -0700373 public void setGridSize(int x, int y) {
374 mCountX = x;
375 mCountY = y;
Sunny Goyalff4ba2d2016-04-02 14:12:34 -0700376 mOccupied = new GridOccupancy(mCountX, mCountY);
377 mTmpOccupied = new GridOccupancy(mCountX, mCountY);
Adam Cohen7fbec102012-03-27 12:42:19 -0700378 mTempRectStack.clear();
Sunny Goyalaa8a8712016-11-20 15:26:01 +0530379 mShortcutsAndWidgets.setCellDimensions(mCellWidth, mCellHeight, mCountX, mCountY);
Adam Cohen76fc0852011-06-17 13:26:23 -0700380 requestLayout();
Adam Cohen2801caf2011-05-13 20:57:39 -0700381 }
382
Adam Cohen2374abf2013-04-16 14:56:57 -0700383 // Set whether or not to invert the layout horizontally if the layout is in RTL mode.
384 public void setInvertIfRtl(boolean invert) {
385 mShortcutsAndWidgets.setInvertIfRtl(invert);
386 }
387
Adam Cohen917e3882013-10-31 15:03:35 -0700388 public void setDropPending(boolean pending) {
389 mDropPending = pending;
390 }
391
392 public boolean isDropPending() {
393 return mDropPending;
394 }
395
Sunny Goyal4b6eb262015-05-14 19:24:40 -0700396 @Override
397 public void setPressedIcon(BubbleTextView icon, Bitmap background) {
Sunny Goyal508da152014-08-14 10:53:27 -0700398 if (icon == null || background == null) {
399 mTouchFeedbackView.setBitmap(null);
400 mTouchFeedbackView.animate().cancel();
401 } else {
Sunny Goyal508da152014-08-14 10:53:27 -0700402 if (mTouchFeedbackView.setBitmap(background)) {
Winsonbe9798b2016-07-20 12:55:49 -0700403 mTouchFeedbackView.alignWithIconView(icon, mShortcutsAndWidgets,
404 null /* clipAgainstView */);
Sunny Goyal4fe5a372015-05-14 19:55:10 -0700405 mTouchFeedbackView.animateShadow();
Sunny Goyal508da152014-08-14 10:53:27 -0700406 }
Patrick Dubroy96864c32011-03-10 17:17:23 -0800407 }
408 }
409
Adam Cohenc50438c2014-08-19 17:43:05 -0700410 void disableDragTarget() {
411 mIsDragTarget = false;
412 }
413
Tony Wickham0f97b782015-12-02 17:55:07 -0800414 public boolean isDragTarget() {
415 return mIsDragTarget;
416 }
417
Adam Cohenc50438c2014-08-19 17:43:05 -0700418 void setIsDragOverlapping(boolean isDragOverlapping) {
419 if (mIsDragOverlapping != isDragOverlapping) {
420 mIsDragOverlapping = isDragOverlapping;
Sunny Goyalf5440cb2016-12-14 15:13:00 -0800421 mBackground.setState(mIsDragOverlapping
422 ? BACKGROUND_STATE_ACTIVE : BACKGROUND_STATE_DEFAULT);
Adam Cohenc50438c2014-08-19 17:43:05 -0700423 invalidate();
424 }
425 }
426
Sunny Goyale2fd14b2015-08-27 17:45:46 -0700427 public void disableJailContent() {
428 mJailContent = false;
429 }
430
431 @Override
432 protected void dispatchSaveInstanceState(SparseArray<Parcelable> container) {
433 if (mJailContent) {
434 ParcelableSparseArray jail = getJailedArray(container);
435 super.dispatchSaveInstanceState(jail);
436 container.put(R.id.cell_layout_jail_id, jail);
437 } else {
438 super.dispatchSaveInstanceState(container);
439 }
440 }
441
442 @Override
443 protected void dispatchRestoreInstanceState(SparseArray<Parcelable> container) {
444 super.dispatchRestoreInstanceState(mJailContent ? getJailedArray(container) : container);
445 }
446
447 private ParcelableSparseArray getJailedArray(SparseArray<Parcelable> container) {
448 final Parcelable parcelable = container.get(R.id.cell_layout_jail_id);
449 return parcelable instanceof ParcelableSparseArray ?
450 (ParcelableSparseArray) parcelable : new ParcelableSparseArray();
451 }
452
Tony Wickham0f97b782015-12-02 17:55:07 -0800453 public boolean getIsDragOverlapping() {
454 return mIsDragOverlapping;
455 }
456
Jeff Sharkey83f111d2009-04-20 21:03:13 -0700457 @Override
Patrick Dubroy1262e362010-10-06 15:49:50 -0700458 protected void onDraw(Canvas canvas) {
Sunny Goyal05739772015-05-19 19:59:09 -0700459 if (!mIsDragTarget) {
460 return;
461 }
462
Michael Jurka3e7c7632010-10-02 16:01:03 -0700463 // When we're large, we are either drawn in a "hover" state (ie when dragging an item to
464 // a neighboring page) or with just a normal background (if backgroundAlpha > 0.0f)
465 // When we're small, we are either drawn normally or in the "accepts drops" state (during
466 // a drag). However, we also drag the mini hover background *over* one of those two
467 // backgrounds
Sunny Goyal05739772015-05-19 19:59:09 -0700468 if (mBackgroundAlpha > 0.0f) {
Sunny Goyal2805e632015-05-20 15:35:32 -0700469 mBackground.draw(canvas);
Michael Jurkaa63c4522010-08-19 13:52:27 -0700470 }
Romain Guya6abce82009-11-10 02:54:41 -0800471
Patrick Dubroy8e58e912010-10-14 13:21:48 -0700472 final Paint paint = mDragOutlinePaint;
Joe Onorato4be866d2010-10-10 11:26:02 -0700473 for (int i = 0; i < mDragOutlines.length; i++) {
Chet Haase472b2812010-10-14 07:02:04 -0700474 final float alpha = mDragOutlineAlphas[i];
Joe Onorato4be866d2010-10-10 11:26:02 -0700475 if (alpha > 0) {
Joe Onorato4be866d2010-10-10 11:26:02 -0700476 final Bitmap b = (Bitmap) mDragOutlineAnims[i].getTag();
Chet Haase472b2812010-10-14 07:02:04 -0700477 paint.setAlpha((int)(alpha + .5f));
Sunny Goyal106bf642015-07-16 12:18:06 -0700478 canvas.drawBitmap(b, null, mDragOutlines[i], paint);
Winson Chung150fbab2010-09-29 17:14:26 -0700479 }
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700480 }
Patrick Dubroy96864c32011-03-10 17:17:23 -0800481
Adam Cohen482ed822012-03-02 14:15:13 -0800482 if (DEBUG_VISUALIZE_OCCUPIED) {
483 int[] pt = new int[2];
484 ColorDrawable cd = new ColorDrawable(Color.RED);
Adam Cohene7587d22012-05-24 18:50:02 -0700485 cd.setBounds(0, 0, mCellWidth, mCellHeight);
Adam Cohen482ed822012-03-02 14:15:13 -0800486 for (int i = 0; i < mCountX; i++) {
487 for (int j = 0; j < mCountY; j++) {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -0700488 if (mOccupied.cells[i][j]) {
Adam Cohen482ed822012-03-02 14:15:13 -0800489 cellToPoint(i, j, pt);
490 canvas.save();
491 canvas.translate(pt[0], pt[1]);
492 cd.draw(canvas);
493 canvas.restore();
494 }
495 }
496 }
497 }
498
Adam Cohenefca0272016-02-24 19:19:06 -0800499 for (int i = 0; i < mFolderBackgrounds.size(); i++) {
500 FolderIcon.PreviewBackground bg = mFolderBackgrounds.get(i);
501 cellToPoint(bg.delegateCellX, bg.delegateCellY, mTempLocation);
502 canvas.save();
503 canvas.translate(mTempLocation[0], mTempLocation[1]);
504 bg.drawBackground(canvas, mFolderBgPaint);
Adam Cohenf172b742016-03-30 19:28:34 -0700505 if (!bg.isClipping) {
506 bg.drawBackgroundStroke(canvas, mFolderBgPaint);
507 }
Adam Cohenefca0272016-02-24 19:19:06 -0800508 canvas.restore();
Adam Cohen69ce2e52011-07-03 19:25:21 -0700509 }
Adam Cohenc51934b2011-07-26 21:07:43 -0700510
Adam Cohenefca0272016-02-24 19:19:06 -0800511 if (mFolderLeaveBehind.delegateCellX >= 0 && mFolderLeaveBehind.delegateCellY >= 0) {
512 cellToPoint(mFolderLeaveBehind.delegateCellX,
513 mFolderLeaveBehind.delegateCellY, mTempLocation);
514 canvas.save();
515 canvas.translate(mTempLocation[0], mTempLocation[1]);
516 mFolderLeaveBehind.drawLeaveBehind(canvas, mFolderBgPaint);
517 canvas.restore();
Adam Cohenc51934b2011-07-26 21:07:43 -0700518 }
Adam Cohen69ce2e52011-07-03 19:25:21 -0700519 }
520
Adam Cohenefca0272016-02-24 19:19:06 -0800521 @Override
522 protected void dispatchDraw(Canvas canvas) {
523 super.dispatchDraw(canvas);
524
525 for (int i = 0; i < mFolderBackgrounds.size(); i++) {
526 FolderIcon.PreviewBackground bg = mFolderBackgrounds.get(i);
Adam Cohenf172b742016-03-30 19:28:34 -0700527 if (bg.isClipping) {
528 cellToPoint(bg.delegateCellX, bg.delegateCellY, mTempLocation);
529 canvas.save();
530 canvas.translate(mTempLocation[0], mTempLocation[1]);
531 bg.drawBackgroundStroke(canvas, mFolderBgPaint);
532 canvas.restore();
533 }
Adam Cohenefca0272016-02-24 19:19:06 -0800534 }
Adam Cohen69ce2e52011-07-03 19:25:21 -0700535 }
536
Adam Cohenefca0272016-02-24 19:19:06 -0800537 public void addFolderBackground(FolderIcon.PreviewBackground bg) {
538 mFolderBackgrounds.add(bg);
539 }
540 public void removeFolderBackground(FolderIcon.PreviewBackground bg) {
541 mFolderBackgrounds.remove(bg);
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700542 }
543
Adam Cohenc51934b2011-07-26 21:07:43 -0700544 public void setFolderLeaveBehindCell(int x, int y) {
Adam Cohenefca0272016-02-24 19:19:06 -0800545
546 DeviceProfile grid = mLauncher.getDeviceProfile();
547 View child = getChildAt(x, y);
548
549 mFolderLeaveBehind.setup(getResources().getDisplayMetrics(), grid, null,
550 child.getMeasuredWidth(), child.getPaddingTop());
551
552 mFolderLeaveBehind.delegateCellX = x;
553 mFolderLeaveBehind.delegateCellY = y;
Adam Cohenc51934b2011-07-26 21:07:43 -0700554 invalidate();
555 }
556
557 public void clearFolderLeaveBehind() {
Adam Cohenefca0272016-02-24 19:19:06 -0800558 mFolderLeaveBehind.delegateCellX = -1;
559 mFolderLeaveBehind.delegateCellY = -1;
Adam Cohenc51934b2011-07-26 21:07:43 -0700560 invalidate();
561 }
562
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700563 @Override
Michael Jurkae6235dd2011-10-04 15:02:05 -0700564 public boolean shouldDelayChildPressedState() {
565 return false;
566 }
567
Adam Cohen1462de32012-07-24 22:34:36 -0700568 public void restoreInstanceState(SparseArray<Parcelable> states) {
Sunny Goyal33a152f2014-07-22 12:13:14 -0700569 try {
570 dispatchRestoreInstanceState(states);
571 } catch (IllegalArgumentException ex) {
Sunny Goyal6c56c682015-07-16 14:09:05 -0700572 if (ProviderConfig.IS_DOGFOOD_BUILD) {
Sunny Goyal33a152f2014-07-22 12:13:14 -0700573 throw ex;
574 }
575 // Mismatched viewId / viewType preventing restore. Skip restore on production builds.
576 Log.e(TAG, "Ignoring an error while restoring a view instance state", ex);
577 }
Adam Cohen1462de32012-07-24 22:34:36 -0700578 }
579
Michael Jurkae6235dd2011-10-04 15:02:05 -0700580 @Override
Jeff Sharkey83f111d2009-04-20 21:03:13 -0700581 public void cancelLongPress() {
582 super.cancelLongPress();
583
584 // Cancel long press for all children
585 final int count = getChildCount();
586 for (int i = 0; i < count; i++) {
587 final View child = getChildAt(i);
588 child.cancelLongPress();
589 }
590 }
591
Michael Jurkadee05892010-07-27 10:01:56 -0700592 public void setOnInterceptTouchListener(View.OnTouchListener listener) {
593 mInterceptTouchListener = listener;
594 }
595
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800596 public int getCountX() {
Adam Cohend22015c2010-07-26 22:02:18 -0700597 return mCountX;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800598 }
599
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800600 public int getCountY() {
Adam Cohend22015c2010-07-26 22:02:18 -0700601 return mCountY;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800602 }
603
Sunny Goyalc13403c2016-11-18 23:44:48 -0800604 public boolean acceptsWidget() {
605 return mContainerType == WORKSPACE;
Sunny Goyale9b651e2015-04-24 11:44:51 -0700606 }
607
Andrew Flynn0dca1ec2012-02-29 13:33:22 -0800608 public boolean addViewToCellLayout(View child, int index, int childId, LayoutParams params,
Sunny Goyalf7a29e82015-04-24 15:20:43 -0700609 boolean markCells) {
Winson Chungaafa03c2010-06-11 17:34:16 -0700610 final LayoutParams lp = params;
611
Andrew Flynnde38e422012-05-08 11:22:15 -0700612 // Hotseat icons - remove text
Andrew Flynn0dca1ec2012-02-29 13:33:22 -0800613 if (child instanceof BubbleTextView) {
614 BubbleTextView bubbleChild = (BubbleTextView) child;
Sunny Goyalc13403c2016-11-18 23:44:48 -0800615 bubbleChild.setTextVisibility(mContainerType != HOTSEAT);
Andrew Flynn0dca1ec2012-02-29 13:33:22 -0800616 }
617
Sunny Goyalc13403c2016-11-18 23:44:48 -0800618 child.setScaleX(mChildScale);
619 child.setScaleY(mChildScale);
Adam Cohen307fe232012-08-16 17:55:58 -0700620
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800621 // Generate an id for each view, this assumes we have at most 256x256 cells
622 // per workspace screen
Adam Cohend22015c2010-07-26 22:02:18 -0700623 if (lp.cellX >= 0 && lp.cellX <= mCountX - 1 && lp.cellY >= 0 && lp.cellY <= mCountY - 1) {
Winson Chungaafa03c2010-06-11 17:34:16 -0700624 // If the horizontal or vertical span is set to -1, it is taken to
625 // mean that it spans the extent of the CellLayout
Adam Cohend22015c2010-07-26 22:02:18 -0700626 if (lp.cellHSpan < 0) lp.cellHSpan = mCountX;
627 if (lp.cellVSpan < 0) lp.cellVSpan = mCountY;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800628
Winson Chungaafa03c2010-06-11 17:34:16 -0700629 child.setId(childId);
Tony Wickhama0628cc2015-10-14 15:23:04 -0700630 if (LOGD) {
631 Log.d(TAG, "Adding view to ShortcutsAndWidgetsContainer: " + child);
632 }
Sunny Goyalf7a29e82015-04-24 15:20:43 -0700633 mShortcutsAndWidgets.addView(child, index, lp);
Michael Jurkadee05892010-07-27 10:01:56 -0700634
Michael Jurkaf3ca3ab2010-10-20 17:08:24 -0700635 if (markCells) markCellsAsOccupiedForView(child);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700636
Winson Chungaafa03c2010-06-11 17:34:16 -0700637 return true;
638 }
639 return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800640 }
Michael Jurka3e7c7632010-10-02 16:01:03 -0700641
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800642 @Override
Michael Jurka0280c3b2010-09-17 15:00:07 -0700643 public void removeAllViews() {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -0700644 mOccupied.clear();
Michael Jurkaa52570f2012-03-20 03:18:20 -0700645 mShortcutsAndWidgets.removeAllViews();
Michael Jurka0280c3b2010-09-17 15:00:07 -0700646 }
647
648 @Override
649 public void removeAllViewsInLayout() {
Michael Jurkaa52570f2012-03-20 03:18:20 -0700650 if (mShortcutsAndWidgets.getChildCount() > 0) {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -0700651 mOccupied.clear();
Michael Jurkaa52570f2012-03-20 03:18:20 -0700652 mShortcutsAndWidgets.removeAllViewsInLayout();
Michael Jurka7cfc2822011-08-02 20:19:24 -0700653 }
Michael Jurka0280c3b2010-09-17 15:00:07 -0700654 }
655
656 @Override
657 public void removeView(View view) {
658 markCellsAsUnoccupiedForView(view);
Michael Jurkaa52570f2012-03-20 03:18:20 -0700659 mShortcutsAndWidgets.removeView(view);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700660 }
661
662 @Override
663 public void removeViewAt(int index) {
Michael Jurkaa52570f2012-03-20 03:18:20 -0700664 markCellsAsUnoccupiedForView(mShortcutsAndWidgets.getChildAt(index));
665 mShortcutsAndWidgets.removeViewAt(index);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700666 }
667
668 @Override
669 public void removeViewInLayout(View view) {
670 markCellsAsUnoccupiedForView(view);
Michael Jurkaa52570f2012-03-20 03:18:20 -0700671 mShortcutsAndWidgets.removeViewInLayout(view);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700672 }
673
674 @Override
675 public void removeViews(int start, int count) {
676 for (int i = start; i < start + count; i++) {
Michael Jurkaa52570f2012-03-20 03:18:20 -0700677 markCellsAsUnoccupiedForView(mShortcutsAndWidgets.getChildAt(i));
Michael Jurka0280c3b2010-09-17 15:00:07 -0700678 }
Michael Jurkaa52570f2012-03-20 03:18:20 -0700679 mShortcutsAndWidgets.removeViews(start, count);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700680 }
681
682 @Override
683 public void removeViewsInLayout(int start, int count) {
684 for (int i = start; i < start + count; i++) {
Michael Jurkaa52570f2012-03-20 03:18:20 -0700685 markCellsAsUnoccupiedForView(mShortcutsAndWidgets.getChildAt(i));
Michael Jurka0280c3b2010-09-17 15:00:07 -0700686 }
Michael Jurkaa52570f2012-03-20 03:18:20 -0700687 mShortcutsAndWidgets.removeViewsInLayout(start, count);
Michael Jurkaabded662011-03-04 12:06:57 -0800688 }
689
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700690 /**
Winson Chungaafa03c2010-06-11 17:34:16 -0700691 * Given a point, return the cell that strictly encloses that point
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800692 * @param x X coordinate of the point
693 * @param y Y coordinate of the point
694 * @param result Array of 2 ints to hold the x and y coordinate of the cell
695 */
Sunny Goyale9b651e2015-04-24 11:44:51 -0700696 public void pointToCellExact(int x, int y, int[] result) {
Winson Chung4b825dcd2011-06-19 12:41:22 -0700697 final int hStartPadding = getPaddingLeft();
698 final int vStartPadding = getPaddingTop();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800699
Sunny Goyalaa8a8712016-11-20 15:26:01 +0530700 result[0] = (x - hStartPadding) / mCellWidth;
701 result[1] = (y - vStartPadding) / mCellHeight;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800702
Adam Cohend22015c2010-07-26 22:02:18 -0700703 final int xAxis = mCountX;
704 final int yAxis = mCountY;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800705
706 if (result[0] < 0) result[0] = 0;
707 if (result[0] >= xAxis) result[0] = xAxis - 1;
708 if (result[1] < 0) result[1] = 0;
709 if (result[1] >= yAxis) result[1] = yAxis - 1;
710 }
Winson Chungaafa03c2010-06-11 17:34:16 -0700711
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800712 /**
713 * Given a point, return the cell that most closely encloses that point
714 * @param x X coordinate of the point
715 * @param y Y coordinate of the point
716 * @param result Array of 2 ints to hold the x and y coordinate of the cell
717 */
718 void pointToCellRounded(int x, int y, int[] result) {
719 pointToCellExact(x + (mCellWidth / 2), y + (mCellHeight / 2), result);
720 }
721
722 /**
723 * Given a cell coordinate, return the point that represents the upper left corner of that cell
Winson Chungaafa03c2010-06-11 17:34:16 -0700724 *
725 * @param cellX X coordinate of the cell
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800726 * @param cellY Y coordinate of the cell
Winson Chungaafa03c2010-06-11 17:34:16 -0700727 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800728 * @param result Array of 2 ints to hold the x and y coordinate of the point
729 */
730 void cellToPoint(int cellX, int cellY, int[] result) {
Winson Chung4b825dcd2011-06-19 12:41:22 -0700731 final int hStartPadding = getPaddingLeft();
732 final int vStartPadding = getPaddingTop();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800733
Sunny Goyalaa8a8712016-11-20 15:26:01 +0530734 result[0] = hStartPadding + cellX * mCellWidth;
735 result[1] = vStartPadding + cellY * mCellHeight;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800736 }
737
Adam Cohene3e27a82011-04-15 12:07:39 -0700738 /**
Adam Cohen482ed822012-03-02 14:15:13 -0800739 * Given a cell coordinate, return the point that represents the center of the cell
Adam Cohene3e27a82011-04-15 12:07:39 -0700740 *
741 * @param cellX X coordinate of the cell
742 * @param cellY Y coordinate of the cell
743 *
744 * @param result Array of 2 ints to hold the x and y coordinate of the point
745 */
746 void cellToCenterPoint(int cellX, int cellY, int[] result) {
Adam Cohen47a876d2012-03-19 13:21:41 -0700747 regionToCenterPoint(cellX, cellY, 1, 1, result);
748 }
749
750 /**
751 * Given a cell coordinate and span return the point that represents the center of the regio
752 *
753 * @param cellX X coordinate of the cell
754 * @param cellY Y coordinate of the cell
755 *
756 * @param result Array of 2 ints to hold the x and y coordinate of the point
757 */
758 void regionToCenterPoint(int cellX, int cellY, int spanX, int spanY, int[] result) {
Winson Chung4b825dcd2011-06-19 12:41:22 -0700759 final int hStartPadding = getPaddingLeft();
760 final int vStartPadding = getPaddingTop();
Sunny Goyalaa8a8712016-11-20 15:26:01 +0530761 result[0] = hStartPadding + cellX * mCellWidth + (spanX * mCellWidth) / 2;
762 result[1] = vStartPadding + cellY * mCellHeight + (spanY * mCellHeight) / 2;
Adam Cohene3e27a82011-04-15 12:07:39 -0700763 }
764
Adam Cohen19f37922012-03-21 11:59:11 -0700765 /**
766 * Given a cell coordinate and span fills out a corresponding pixel rect
767 *
768 * @param cellX X coordinate of the cell
769 * @param cellY Y coordinate of the cell
770 * @param result Rect in which to write the result
771 */
772 void regionToRect(int cellX, int cellY, int spanX, int spanY, Rect result) {
773 final int hStartPadding = getPaddingLeft();
774 final int vStartPadding = getPaddingTop();
Sunny Goyalaa8a8712016-11-20 15:26:01 +0530775 final int left = hStartPadding + cellX * mCellWidth;
776 final int top = vStartPadding + cellY * mCellHeight;
777 result.set(left, top, left + (spanX * mCellWidth), top + (spanY * mCellHeight));
Adam Cohen19f37922012-03-21 11:59:11 -0700778 }
779
Adam Cohen482ed822012-03-02 14:15:13 -0800780 public float getDistanceFromCell(float x, float y, int[] cell) {
781 cellToCenterPoint(cell[0], cell[1], mTmpPoint);
Sunny Goyalf7a29e82015-04-24 15:20:43 -0700782 return (float) Math.hypot(x - mTmpPoint[0], y - mTmpPoint[1]);
Adam Cohen482ed822012-03-02 14:15:13 -0800783 }
784
Adam Cohenf9c184a2016-01-15 16:47:43 -0800785 public int getCellWidth() {
Romain Guy84f296c2009-11-04 15:00:44 -0800786 return mCellWidth;
787 }
788
789 int getCellHeight() {
790 return mCellHeight;
791 }
792
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700793 public void setFixedSize(int width, int height) {
794 mFixedWidth = width;
795 mFixedHeight = height;
796 }
797
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800798 @Override
799 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800800 int widthSpecMode = MeasureSpec.getMode(widthMeasureSpec);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800801 int heightSpecMode = MeasureSpec.getMode(heightMeasureSpec);
Winson Chung5f8afe62013-08-12 16:19:28 -0700802 int widthSize = MeasureSpec.getSize(widthMeasureSpec);
803 int heightSize = MeasureSpec.getSize(heightMeasureSpec);
Winson Chung2d75f122013-09-23 16:53:31 -0700804 int childWidthSize = widthSize - (getPaddingLeft() + getPaddingRight());
805 int childHeightSize = heightSize - (getPaddingTop() + getPaddingBottom());
Winson Chung11a1a532013-09-13 11:14:45 -0700806 if (mFixedCellWidth < 0 || mFixedCellHeight < 0) {
Sunny Goyalc6205602015-05-21 20:46:33 -0700807 int cw = DeviceProfile.calculateCellWidth(childWidthSize, mCountX);
808 int ch = DeviceProfile.calculateCellHeight(childHeightSize, mCountY);
Winson Chung11a1a532013-09-13 11:14:45 -0700809 if (cw != mCellWidth || ch != mCellHeight) {
810 mCellWidth = cw;
811 mCellHeight = ch;
Sunny Goyalaa8a8712016-11-20 15:26:01 +0530812 mShortcutsAndWidgets.setCellDimensions(mCellWidth, mCellHeight, mCountX, mCountY);
Winson Chung11a1a532013-09-13 11:14:45 -0700813 }
Winson Chung5f8afe62013-08-12 16:19:28 -0700814 }
Winson Chungaafa03c2010-06-11 17:34:16 -0700815
Winson Chung2d75f122013-09-23 16:53:31 -0700816 int newWidth = childWidthSize;
817 int newHeight = childHeightSize;
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700818 if (mFixedWidth > 0 && mFixedHeight > 0) {
819 newWidth = mFixedWidth;
820 newHeight = mFixedHeight;
821 } else if (widthSpecMode == MeasureSpec.UNSPECIFIED || heightSpecMode == MeasureSpec.UNSPECIFIED) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800822 throw new RuntimeException("CellLayout cannot have UNSPECIFIED dimensions");
823 }
824
Sunny Goyal4fe5a372015-05-14 19:55:10 -0700825 // Make the feedback view large enough to hold the blur bitmap.
826 mTouchFeedbackView.measure(
827 MeasureSpec.makeMeasureSpec(mCellWidth + mTouchFeedbackView.getExtraSize(),
828 MeasureSpec.EXACTLY),
829 MeasureSpec.makeMeasureSpec(mCellHeight + mTouchFeedbackView.getExtraSize(),
830 MeasureSpec.EXACTLY));
831
832 mShortcutsAndWidgets.measure(
833 MeasureSpec.makeMeasureSpec(newWidth, MeasureSpec.EXACTLY),
834 MeasureSpec.makeMeasureSpec(newHeight, MeasureSpec.EXACTLY));
835
836 int maxWidth = mShortcutsAndWidgets.getMeasuredWidth();
837 int maxHeight = mShortcutsAndWidgets.getMeasuredHeight();
Winson Chung2d75f122013-09-23 16:53:31 -0700838 if (mFixedWidth > 0 && mFixedHeight > 0) {
839 setMeasuredDimension(maxWidth, maxHeight);
840 } else {
841 setMeasuredDimension(widthSize, heightSize);
842 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800843 }
844
845 @Override
Michael Jurka28750fb2010-09-24 17:43:49 -0700846 protected void onLayout(boolean changed, int l, int t, int r, int b) {
Tony Wickham26b01422015-11-10 14:44:32 -0800847 boolean isFullscreen = mShortcutsAndWidgets.getChildCount() > 0 &&
848 ((LayoutParams) mShortcutsAndWidgets.getChildAt(0).getLayoutParams()).isFullscreen;
849 int left = getPaddingLeft();
850 if (!isFullscreen) {
Tony Wickhama501d492015-11-03 18:05:01 -0800851 left += (int) Math.ceil(getUnusedHorizontalSpace() / 2f);
Tony Wickham26b01422015-11-10 14:44:32 -0800852 }
Sunny Goyal7c786f72016-06-01 14:08:21 -0700853 int right = r - l - getPaddingRight();
854 if (!isFullscreen) {
855 right -= (int) Math.ceil(getUnusedHorizontalSpace() / 2f);
856 }
857
Winson Chung38848ca2013-10-08 12:03:44 -0700858 int top = getPaddingTop();
Sunny Goyal7c786f72016-06-01 14:08:21 -0700859 int bottom = b - t - getPaddingBottom();
Sunny Goyal4fe5a372015-05-14 19:55:10 -0700860
861 mTouchFeedbackView.layout(left, top,
862 left + mTouchFeedbackView.getMeasuredWidth(),
863 top + mTouchFeedbackView.getMeasuredHeight());
Sunny Goyal7c786f72016-06-01 14:08:21 -0700864 mShortcutsAndWidgets.layout(left, top, right, bottom);
865
866 // Expand the background drawing bounds by the padding baked into the background drawable
867 mBackground.getPadding(mTempRect);
868 mBackground.setBounds(
869 left - mTempRect.left,
870 top - mTempRect.top,
871 right + mTempRect.right,
872 bottom + mTempRect.bottom);
873 }
874
Tony Wickhama501d492015-11-03 18:05:01 -0800875 /**
876 * Returns the amount of space left over after subtracting padding and cells. This space will be
877 * very small, a few pixels at most, and is a result of rounding down when calculating the cell
878 * width in {@link DeviceProfile#calculateCellWidth(int, int)}.
879 */
880 public int getUnusedHorizontalSpace() {
881 return getMeasuredWidth() - getPaddingLeft() - getPaddingRight() - (mCountX * mCellWidth);
882 }
883
Michael Jurka5f1c5092010-09-03 14:15:02 -0700884 public float getBackgroundAlpha() {
885 return mBackgroundAlpha;
Michael Jurkadee05892010-07-27 10:01:56 -0700886 }
887
Michael Jurka5f1c5092010-09-03 14:15:02 -0700888 public void setBackgroundAlpha(float alpha) {
Michael Jurkaafaa0502011-12-13 18:22:50 -0800889 if (mBackgroundAlpha != alpha) {
890 mBackgroundAlpha = alpha;
Sunny Goyal2805e632015-05-20 15:35:32 -0700891 mBackground.setAlpha((int) (mBackgroundAlpha * 255));
Michael Jurkaafaa0502011-12-13 18:22:50 -0800892 }
Michael Jurkadee05892010-07-27 10:01:56 -0700893 }
894
Sunny Goyal2805e632015-05-20 15:35:32 -0700895 @Override
896 protected boolean verifyDrawable(Drawable who) {
897 return super.verifyDrawable(who) || (mIsDragTarget && who == mBackground);
898 }
899
Michael Jurkaa52570f2012-03-20 03:18:20 -0700900 public void setShortcutAndWidgetAlpha(float alpha) {
Sunny Goyal02b50812014-09-10 15:44:42 -0700901 mShortcutsAndWidgets.setAlpha(alpha);
Michael Jurkadee05892010-07-27 10:01:56 -0700902 }
903
Michael Jurkaa52570f2012-03-20 03:18:20 -0700904 public ShortcutAndWidgetContainer getShortcutsAndWidgets() {
Sunny Goyaldcbcc862014-08-12 15:58:36 -0700905 return mShortcutsAndWidgets;
Michael Jurkaa52570f2012-03-20 03:18:20 -0700906 }
907
Patrick Dubroy440c3602010-07-13 17:50:32 -0700908 public View getChildAt(int x, int y) {
Michael Jurkaa52570f2012-03-20 03:18:20 -0700909 return mShortcutsAndWidgets.getChildAt(x, y);
Patrick Dubroy440c3602010-07-13 17:50:32 -0700910 }
911
Adam Cohen76fc0852011-06-17 13:26:23 -0700912 public boolean animateChildToPosition(final View child, int cellX, int cellY, int duration,
Adam Cohen482ed822012-03-02 14:15:13 -0800913 int delay, boolean permanent, boolean adjustOccupied) {
Michael Jurkaa52570f2012-03-20 03:18:20 -0700914 ShortcutAndWidgetContainer clc = getShortcutsAndWidgets();
Adam Cohen482ed822012-03-02 14:15:13 -0800915
Adam Cohen19f37922012-03-21 11:59:11 -0700916 if (clc.indexOfChild(child) != -1) {
Adam Cohenbfbfd262011-06-13 16:55:12 -0700917 final LayoutParams lp = (LayoutParams) child.getLayoutParams();
918 final ItemInfo info = (ItemInfo) child.getTag();
919
920 // We cancel any existing animations
921 if (mReorderAnimators.containsKey(lp)) {
922 mReorderAnimators.get(lp).cancel();
923 mReorderAnimators.remove(lp);
924 }
925
Adam Cohen482ed822012-03-02 14:15:13 -0800926 final int oldX = lp.x;
927 final int oldY = lp.y;
928 if (adjustOccupied) {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -0700929 GridOccupancy occupied = permanent ? mOccupied : mTmpOccupied;
930 occupied.markCells(lp.cellX, lp.cellY, lp.cellHSpan, lp.cellVSpan, false);
931 occupied.markCells(cellX, cellY, lp.cellHSpan, lp.cellVSpan, true);
Adam Cohen482ed822012-03-02 14:15:13 -0800932 }
Adam Cohenbfbfd262011-06-13 16:55:12 -0700933 lp.isLockedToGrid = true;
Adam Cohen482ed822012-03-02 14:15:13 -0800934 if (permanent) {
935 lp.cellX = info.cellX = cellX;
936 lp.cellY = info.cellY = cellY;
937 } else {
938 lp.tmpCellX = cellX;
939 lp.tmpCellY = cellY;
940 }
Jon Mirandae96798e2016-12-07 12:10:44 -0800941 clc.setupLp(child);
Adam Cohenbfbfd262011-06-13 16:55:12 -0700942 lp.isLockedToGrid = false;
Adam Cohen482ed822012-03-02 14:15:13 -0800943 final int newX = lp.x;
944 final int newY = lp.y;
Adam Cohenbfbfd262011-06-13 16:55:12 -0700945
Adam Cohen76fc0852011-06-17 13:26:23 -0700946 lp.x = oldX;
947 lp.y = oldY;
Adam Cohen76fc0852011-06-17 13:26:23 -0700948
Adam Cohen482ed822012-03-02 14:15:13 -0800949 // Exit early if we're not actually moving the view
950 if (oldX == newX && oldY == newY) {
951 lp.isLockedToGrid = true;
952 return true;
953 }
954
Jon Mirandacda3bfb2017-02-06 15:54:41 -0800955 ValueAnimator va = LauncherAnimUtils.ofFloat(0f, 1f);
Adam Cohen482ed822012-03-02 14:15:13 -0800956 va.setDuration(duration);
957 mReorderAnimators.put(lp, va);
958
959 va.addUpdateListener(new AnimatorUpdateListener() {
960 @Override
Adam Cohenbfbfd262011-06-13 16:55:12 -0700961 public void onAnimationUpdate(ValueAnimator animation) {
Jon Mirandae96798e2016-12-07 12:10:44 -0800962 float r = (Float) animation.getAnimatedValue();
Adam Cohen19f37922012-03-21 11:59:11 -0700963 lp.x = (int) ((1 - r) * oldX + r * newX);
964 lp.y = (int) ((1 - r) * oldY + r * newY);
Adam Cohen6b8a02d2012-03-22 15:13:40 -0700965 child.requestLayout();
Adam Cohenbfbfd262011-06-13 16:55:12 -0700966 }
967 });
Adam Cohen482ed822012-03-02 14:15:13 -0800968 va.addListener(new AnimatorListenerAdapter() {
Adam Cohenbfbfd262011-06-13 16:55:12 -0700969 boolean cancelled = false;
970 public void onAnimationEnd(Animator animation) {
971 // If the animation was cancelled, it means that another animation
972 // has interrupted this one, and we don't want to lock the item into
973 // place just yet.
974 if (!cancelled) {
975 lp.isLockedToGrid = true;
Adam Cohen482ed822012-03-02 14:15:13 -0800976 child.requestLayout();
Adam Cohenbfbfd262011-06-13 16:55:12 -0700977 }
978 if (mReorderAnimators.containsKey(lp)) {
979 mReorderAnimators.remove(lp);
980 }
981 }
982 public void onAnimationCancel(Animator animation) {
983 cancelled = true;
984 }
985 });
Adam Cohen482ed822012-03-02 14:15:13 -0800986 va.setStartDelay(delay);
987 va.start();
Adam Cohenbfbfd262011-06-13 16:55:12 -0700988 return true;
989 }
990 return false;
991 }
992
Sunny Goyal06e21a22016-08-11 16:02:02 -0700993 void visualizeDropLocation(View v, DragPreviewProvider outlineProvider, int cellX, int cellY,
994 int spanX, int spanY, boolean resize, DropTarget.DragObject dragObject) {
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -0700995 final int oldDragCellX = mDragCell[0];
996 final int oldDragCellY = mDragCell[1];
Adam Cohen482ed822012-03-02 14:15:13 -0800997
Hyunyoung Song0de01172016-10-05 16:27:48 -0700998 if (outlineProvider == null || outlineProvider.generatedDragOutline == null) {
Adam Cohen2801caf2011-05-13 20:57:39 -0700999 return;
1000 }
1001
Hyunyoung Song0de01172016-10-05 16:27:48 -07001002 Bitmap dragOutline = outlineProvider.generatedDragOutline;
Adam Cohen482ed822012-03-02 14:15:13 -08001003 if (cellX != oldDragCellX || cellY != oldDragCellY) {
Sunny Goyale78e3d72015-09-24 11:23:31 -07001004 Point dragOffset = dragObject.dragView.getDragVisualizeOffset();
1005 Rect dragRegion = dragObject.dragView.getDragRegion();
1006
Adam Cohen482ed822012-03-02 14:15:13 -08001007 mDragCell[0] = cellX;
1008 mDragCell[1] = cellY;
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001009
Joe Onorato4be866d2010-10-10 11:26:02 -07001010 final int oldIndex = mDragOutlineCurrent;
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -07001011 mDragOutlineAnims[oldIndex].animateOut();
1012 mDragOutlineCurrent = (oldIndex + 1) % mDragOutlines.length;
Adam Cohend41fbf52012-02-16 23:53:59 -08001013 Rect r = mDragOutlines[mDragOutlineCurrent];
Sunny Goyal106bf642015-07-16 12:18:06 -07001014
Adam Cohend41fbf52012-02-16 23:53:59 -08001015 if (resize) {
Adam Cohen482ed822012-03-02 14:15:13 -08001016 cellToRect(cellX, cellY, spanX, spanY, r);
Jon Mirandae96798e2016-12-07 12:10:44 -08001017 if (v instanceof LauncherAppWidgetHostView) {
1018 DeviceProfile profile = mLauncher.getDeviceProfile();
Jon Miranda6f6a06a2016-12-15 11:24:18 -08001019 Utilities.shrinkRect(r, profile.appWidgetScale.x, profile.appWidgetScale.y);
Jon Mirandae96798e2016-12-07 12:10:44 -08001020 }
Sunny Goyal106bf642015-07-16 12:18:06 -07001021 } else {
1022 // Find the top left corner of the rect the object will occupy
1023 final int[] topLeft = mTmpPoint;
1024 cellToPoint(cellX, cellY, topLeft);
1025
1026 int left = topLeft[0];
1027 int top = topLeft[1];
1028
1029 if (v != null && dragOffset == null) {
1030 // When drawing the drag outline, it did not account for margin offsets
1031 // added by the view's parent.
1032 MarginLayoutParams lp = (MarginLayoutParams) v.getLayoutParams();
1033 left += lp.leftMargin;
1034 top += lp.topMargin;
1035
1036 // Offsets due to the size difference between the View and the dragOutline.
1037 // There is a size difference to account for the outer blur, which may lie
1038 // outside the bounds of the view.
Jon Mirandaf7ff3fe2016-12-05 12:04:44 -08001039 top += ((mCellHeight * spanY) - dragOutline.getHeight()) / 2;
Sunny Goyal106bf642015-07-16 12:18:06 -07001040 // We center about the x axis
Sunny Goyalaa8a8712016-11-20 15:26:01 +05301041 left += ((mCellWidth * spanX) - dragOutline.getWidth()) / 2;
Sunny Goyal106bf642015-07-16 12:18:06 -07001042 } else {
1043 if (dragOffset != null && dragRegion != null) {
1044 // Center the drag region *horizontally* in the cell and apply a drag
1045 // outline offset
Sunny Goyalaa8a8712016-11-20 15:26:01 +05301046 left += dragOffset.x + ((mCellWidth * spanX) - dragRegion.width()) / 2;
Sunny Goyal106bf642015-07-16 12:18:06 -07001047 int cHeight = getShortcutsAndWidgets().getCellContentHeight();
1048 int cellPaddingY = (int) Math.max(0, ((mCellHeight - cHeight) / 2f));
1049 top += dragOffset.y + cellPaddingY;
1050 } else {
1051 // Center the drag outline in the cell
Sunny Goyalaa8a8712016-11-20 15:26:01 +05301052 left += ((mCellWidth * spanX) - dragOutline.getWidth()) / 2;
1053 top += ((mCellHeight * spanY) - dragOutline.getHeight()) / 2;
Sunny Goyal106bf642015-07-16 12:18:06 -07001054 }
1055 }
1056 r.set(left, top, left + dragOutline.getWidth(), top + dragOutline.getHeight());
Adam Cohend41fbf52012-02-16 23:53:59 -08001057 }
Winson Chung150fbab2010-09-29 17:14:26 -07001058
Jon Miranda6f6a06a2016-12-15 11:24:18 -08001059 Utilities.scaleRectAboutCenter(r, mChildScale);
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -07001060 mDragOutlineAnims[mDragOutlineCurrent].setTag(dragOutline);
1061 mDragOutlineAnims[mDragOutlineCurrent].animateIn();
Sunny Goyale78e3d72015-09-24 11:23:31 -07001062
1063 if (dragObject.stateAnnouncer != null) {
Sunny Goyalc13403c2016-11-18 23:44:48 -08001064 dragObject.stateAnnouncer.announce(getItemMoveDescription(cellX, cellY));
Sunny Goyale78e3d72015-09-24 11:23:31 -07001065 }
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001066 }
1067 }
1068
Sunny Goyalc13403c2016-11-18 23:44:48 -08001069 public String getItemMoveDescription(int cellX, int cellY) {
1070 if (mContainerType == HOTSEAT) {
1071 return getContext().getString(R.string.move_to_hotseat_position,
1072 Math.max(cellX, cellY) + 1);
1073 } else {
1074 return getContext().getString(R.string.move_to_empty_cell,
1075 cellY + 1, cellX + 1);
1076 }
1077 }
1078
Adam Cohene0310962011-04-18 16:15:31 -07001079 public void clearDragOutlines() {
1080 final int oldIndex = mDragOutlineCurrent;
1081 mDragOutlineAnims[oldIndex].animateOut();
Adam Cohend41fbf52012-02-16 23:53:59 -08001082 mDragCell[0] = mDragCell[1] = -1;
Adam Cohene0310962011-04-18 16:15:31 -07001083 }
1084
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001085 /**
Jeff Sharkey70864282009-04-07 21:08:40 -07001086 * Find a vacant area that will fit the given bounds nearest the requested
1087 * cell location. Uses Euclidean distance to score multiple vacant areas.
Winson Chungaafa03c2010-06-11 17:34:16 -07001088 *
Romain Guy51afc022009-05-04 18:03:43 -07001089 * @param pixelX The X location at which you want to search for a vacant area.
1090 * @param pixelY The Y location at which you want to search for a vacant area.
Adam Cohend41fbf52012-02-16 23:53:59 -08001091 * @param minSpanX The minimum horizontal span required
1092 * @param minSpanY The minimum vertical span required
1093 * @param spanX Horizontal span of the object.
1094 * @param spanY Vertical span of the object.
1095 * @param result Array in which to place the result, or null (in which case a new array will
1096 * be allocated)
1097 * @return The X, Y cell of a vacant area that can contain this object,
1098 * nearest the requested location.
1099 */
1100 int[] findNearestVacantArea(int pixelX, int pixelY, int minSpanX, int minSpanY, int spanX,
1101 int spanY, int[] result, int[] resultSpan) {
Sunny Goyalf7a29e82015-04-24 15:20:43 -07001102 return findNearestArea(pixelX, pixelY, minSpanX, minSpanY, spanX, spanY, true,
Adam Cohend41fbf52012-02-16 23:53:59 -08001103 result, resultSpan);
1104 }
1105
Adam Cohend41fbf52012-02-16 23:53:59 -08001106 private final Stack<Rect> mTempRectStack = new Stack<Rect>();
1107 private void lazyInitTempRectStack() {
1108 if (mTempRectStack.isEmpty()) {
1109 for (int i = 0; i < mCountX * mCountY; i++) {
1110 mTempRectStack.push(new Rect());
1111 }
1112 }
1113 }
Adam Cohen482ed822012-03-02 14:15:13 -08001114
Adam Cohend41fbf52012-02-16 23:53:59 -08001115 private void recycleTempRects(Stack<Rect> used) {
1116 while (!used.isEmpty()) {
1117 mTempRectStack.push(used.pop());
1118 }
1119 }
1120
1121 /**
1122 * Find a vacant area that will fit the given bounds nearest the requested
1123 * cell location. Uses Euclidean distance to score multiple vacant areas.
1124 *
1125 * @param pixelX The X location at which you want to search for a vacant area.
1126 * @param pixelY The Y location at which you want to search for a vacant area.
1127 * @param minSpanX The minimum horizontal span required
1128 * @param minSpanY The minimum vertical span required
1129 * @param spanX Horizontal span of the object.
1130 * @param spanY Vertical span of the object.
1131 * @param ignoreOccupied If true, the result can be an occupied cell
1132 * @param result Array in which to place the result, or null (in which case a new array will
1133 * be allocated)
1134 * @return The X, Y cell of a vacant area that can contain this object,
1135 * nearest the requested location.
1136 */
Sunny Goyalf7a29e82015-04-24 15:20:43 -07001137 private int[] findNearestArea(int pixelX, int pixelY, int minSpanX, int minSpanY, int spanX,
1138 int spanY, boolean ignoreOccupied, int[] result, int[] resultSpan) {
Adam Cohend41fbf52012-02-16 23:53:59 -08001139 lazyInitTempRectStack();
Michael Jurkac6ee42e2010-09-30 12:04:50 -07001140
Adam Cohene3e27a82011-04-15 12:07:39 -07001141 // For items with a spanX / spanY > 1, the passed in point (pixelX, pixelY) corresponds
1142 // to the center of the item, but we are searching based on the top-left cell, so
1143 // we translate the point over to correspond to the top-left.
Sunny Goyalaa8a8712016-11-20 15:26:01 +05301144 pixelX -= mCellWidth * (spanX - 1) / 2f;
1145 pixelY -= mCellHeight * (spanY - 1) / 2f;
Adam Cohene3e27a82011-04-15 12:07:39 -07001146
Jeff Sharkey70864282009-04-07 21:08:40 -07001147 // Keep track of best-scoring drop area
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001148 final int[] bestXY = result != null ? result : new int[2];
Jeff Sharkey70864282009-04-07 21:08:40 -07001149 double bestDistance = Double.MAX_VALUE;
Adam Cohend41fbf52012-02-16 23:53:59 -08001150 final Rect bestRect = new Rect(-1, -1, -1, -1);
1151 final Stack<Rect> validRegions = new Stack<Rect>();
Winson Chungaafa03c2010-06-11 17:34:16 -07001152
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001153 final int countX = mCountX;
1154 final int countY = mCountY;
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001155
Adam Cohend41fbf52012-02-16 23:53:59 -08001156 if (minSpanX <= 0 || minSpanY <= 0 || spanX <= 0 || spanY <= 0 ||
1157 spanX < minSpanX || spanY < minSpanY) {
1158 return bestXY;
1159 }
1160
1161 for (int y = 0; y < countY - (minSpanY - 1); y++) {
Michael Jurkac28de512010-08-13 11:27:44 -07001162 inner:
Adam Cohend41fbf52012-02-16 23:53:59 -08001163 for (int x = 0; x < countX - (minSpanX - 1); x++) {
1164 int ySize = -1;
1165 int xSize = -1;
Adam Cohendf035382011-04-11 17:22:04 -07001166 if (ignoreOccupied) {
Adam Cohend41fbf52012-02-16 23:53:59 -08001167 // First, let's see if this thing fits anywhere
1168 for (int i = 0; i < minSpanX; i++) {
1169 for (int j = 0; j < minSpanY; j++) {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001170 if (mOccupied.cells[x + i][y + j]) {
Adam Cohendf035382011-04-11 17:22:04 -07001171 continue inner;
1172 }
Michael Jurkac28de512010-08-13 11:27:44 -07001173 }
1174 }
Adam Cohend41fbf52012-02-16 23:53:59 -08001175 xSize = minSpanX;
1176 ySize = minSpanY;
1177
1178 // We know that the item will fit at _some_ acceptable size, now let's see
1179 // how big we can make it. We'll alternate between incrementing x and y spans
1180 // until we hit a limit.
1181 boolean incX = true;
1182 boolean hitMaxX = xSize >= spanX;
1183 boolean hitMaxY = ySize >= spanY;
1184 while (!(hitMaxX && hitMaxY)) {
1185 if (incX && !hitMaxX) {
1186 for (int j = 0; j < ySize; j++) {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001187 if (x + xSize > countX -1 || mOccupied.cells[x + xSize][y + j]) {
Adam Cohend41fbf52012-02-16 23:53:59 -08001188 // We can't move out horizontally
1189 hitMaxX = true;
1190 }
1191 }
1192 if (!hitMaxX) {
1193 xSize++;
1194 }
1195 } else if (!hitMaxY) {
1196 for (int i = 0; i < xSize; i++) {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001197 if (y + ySize > countY - 1 || mOccupied.cells[x + i][y + ySize]) {
Adam Cohend41fbf52012-02-16 23:53:59 -08001198 // We can't move out vertically
1199 hitMaxY = true;
1200 }
1201 }
1202 if (!hitMaxY) {
1203 ySize++;
1204 }
1205 }
1206 hitMaxX |= xSize >= spanX;
1207 hitMaxY |= ySize >= spanY;
1208 incX = !incX;
1209 }
1210 incX = true;
1211 hitMaxX = xSize >= spanX;
1212 hitMaxY = ySize >= spanY;
Michael Jurkac28de512010-08-13 11:27:44 -07001213 }
Sunny Goyal2805e632015-05-20 15:35:32 -07001214 final int[] cellXY = mTmpPoint;
Adam Cohene3e27a82011-04-15 12:07:39 -07001215 cellToCenterPoint(x, y, cellXY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001216
Adam Cohend41fbf52012-02-16 23:53:59 -08001217 // We verify that the current rect is not a sub-rect of any of our previous
1218 // candidates. In this case, the current rect is disqualified in favour of the
1219 // containing rect.
1220 Rect currentRect = mTempRectStack.pop();
1221 currentRect.set(x, y, x + xSize, y + ySize);
1222 boolean contained = false;
1223 for (Rect r : validRegions) {
1224 if (r.contains(currentRect)) {
1225 contained = true;
1226 break;
1227 }
1228 }
1229 validRegions.push(currentRect);
Sunny Goyalf7a29e82015-04-24 15:20:43 -07001230 double distance = Math.hypot(cellXY[0] - pixelX, cellXY[1] - pixelY);
Adam Cohen482ed822012-03-02 14:15:13 -08001231
Adam Cohend41fbf52012-02-16 23:53:59 -08001232 if ((distance <= bestDistance && !contained) ||
1233 currentRect.contains(bestRect)) {
Michael Jurkac28de512010-08-13 11:27:44 -07001234 bestDistance = distance;
1235 bestXY[0] = x;
1236 bestXY[1] = y;
Adam Cohend41fbf52012-02-16 23:53:59 -08001237 if (resultSpan != null) {
1238 resultSpan[0] = xSize;
1239 resultSpan[1] = ySize;
1240 }
1241 bestRect.set(currentRect);
Michael Jurkac28de512010-08-13 11:27:44 -07001242 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001243 }
1244 }
1245
Adam Cohenc0dcf592011-06-01 15:30:43 -07001246 // Return -1, -1 if no suitable location found
1247 if (bestDistance == Double.MAX_VALUE) {
1248 bestXY[0] = -1;
1249 bestXY[1] = -1;
Jeff Sharkey70864282009-04-07 21:08:40 -07001250 }
Adam Cohend41fbf52012-02-16 23:53:59 -08001251 recycleTempRects(validRegions);
Adam Cohenc0dcf592011-06-01 15:30:43 -07001252 return bestXY;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001253 }
Winson Chungaafa03c2010-06-11 17:34:16 -07001254
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001255 /**
Adam Cohen482ed822012-03-02 14:15:13 -08001256 * Find a vacant area that will fit the given bounds nearest the requested
1257 * cell location, and will also weigh in a suggested direction vector of the
1258 * desired location. This method computers distance based on unit grid distances,
1259 * not pixel distances.
1260 *
Adam Cohen47a876d2012-03-19 13:21:41 -07001261 * @param cellX The X cell nearest to which you want to search for a vacant area.
1262 * @param cellY The Y cell nearest which you want to search for a vacant area.
Adam Cohen482ed822012-03-02 14:15:13 -08001263 * @param spanX Horizontal span of the object.
1264 * @param spanY Vertical span of the object.
Adam Cohen47a876d2012-03-19 13:21:41 -07001265 * @param direction The favored direction in which the views should move from x, y
Sunny Goyal9eba1fd2015-10-16 08:58:57 -07001266 * @param occupied The array which represents which cells in the CellLayout are occupied
Adam Cohen47a876d2012-03-19 13:21:41 -07001267 * @param blockOccupied The array which represents which cells in the specified block (cellX,
Winson Chung5f8afe62013-08-12 16:19:28 -07001268 * cellY, spanX, spanY) are occupied. This is used when try to move a group of views.
Adam Cohen482ed822012-03-02 14:15:13 -08001269 * @param result Array in which to place the result, or null (in which case a new array will
1270 * be allocated)
1271 * @return The X, Y cell of a vacant area that can contain this object,
1272 * nearest the requested location.
1273 */
1274 private int[] findNearestArea(int cellX, int cellY, int spanX, int spanY, int[] direction,
Adam Cohen47a876d2012-03-19 13:21:41 -07001275 boolean[][] occupied, boolean blockOccupied[][], int[] result) {
Adam Cohen482ed822012-03-02 14:15:13 -08001276 // Keep track of best-scoring drop area
1277 final int[] bestXY = result != null ? result : new int[2];
1278 float bestDistance = Float.MAX_VALUE;
1279 int bestDirectionScore = Integer.MIN_VALUE;
1280
1281 final int countX = mCountX;
1282 final int countY = mCountY;
1283
1284 for (int y = 0; y < countY - (spanY - 1); y++) {
1285 inner:
1286 for (int x = 0; x < countX - (spanX - 1); x++) {
1287 // First, let's see if this thing fits anywhere
1288 for (int i = 0; i < spanX; i++) {
1289 for (int j = 0; j < spanY; j++) {
Adam Cohen47a876d2012-03-19 13:21:41 -07001290 if (occupied[x + i][y + j] && (blockOccupied == null || blockOccupied[i][j])) {
Adam Cohen482ed822012-03-02 14:15:13 -08001291 continue inner;
1292 }
1293 }
1294 }
1295
Sunny Goyalf7a29e82015-04-24 15:20:43 -07001296 float distance = (float) Math.hypot(x - cellX, y - cellY);
Adam Cohen482ed822012-03-02 14:15:13 -08001297 int[] curDirection = mTmpPoint;
Adam Cohen47a876d2012-03-19 13:21:41 -07001298 computeDirectionVector(x - cellX, y - cellY, curDirection);
1299 // The direction score is just the dot product of the two candidate direction
1300 // and that passed in.
Adam Cohen482ed822012-03-02 14:15:13 -08001301 int curDirectionScore = direction[0] * curDirection[0] +
1302 direction[1] * curDirection[1];
Sunny Goyal8f90dcf2016-08-18 15:01:11 -07001303 if (Float.compare(distance, bestDistance) < 0 ||
1304 (Float.compare(distance, bestDistance) == 0
1305 && curDirectionScore > bestDirectionScore)) {
Adam Cohen482ed822012-03-02 14:15:13 -08001306 bestDistance = distance;
1307 bestDirectionScore = curDirectionScore;
1308 bestXY[0] = x;
1309 bestXY[1] = y;
1310 }
1311 }
1312 }
1313
1314 // Return -1, -1 if no suitable location found
1315 if (bestDistance == Float.MAX_VALUE) {
1316 bestXY[0] = -1;
1317 bestXY[1] = -1;
1318 }
1319 return bestXY;
1320 }
1321
1322 private boolean addViewToTempLocation(View v, Rect rectOccupiedByPotentialDrop,
Adam Cohen8baab352012-03-20 17:39:21 -07001323 int[] direction, ItemConfiguration currentState) {
1324 CellAndSpan c = currentState.map.get(v);
Adam Cohen482ed822012-03-02 14:15:13 -08001325 boolean success = false;
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001326 mTmpOccupied.markCells(c, false);
1327 mTmpOccupied.markCells(rectOccupiedByPotentialDrop, true);
Adam Cohen482ed822012-03-02 14:15:13 -08001328
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001329 findNearestArea(c.cellX, c.cellY, c.spanX, c.spanY, direction,
1330 mTmpOccupied.cells, null, mTempLocation);
Adam Cohen482ed822012-03-02 14:15:13 -08001331
1332 if (mTempLocation[0] >= 0 && mTempLocation[1] >= 0) {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001333 c.cellX = mTempLocation[0];
1334 c.cellY = mTempLocation[1];
Adam Cohen482ed822012-03-02 14:15:13 -08001335 success = true;
Adam Cohen482ed822012-03-02 14:15:13 -08001336 }
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001337 mTmpOccupied.markCells(c, true);
Adam Cohen482ed822012-03-02 14:15:13 -08001338 return success;
1339 }
1340
Adam Cohenf3900c22012-11-16 18:28:11 -08001341 /**
1342 * This helper class defines a cluster of views. It helps with defining complex edges
1343 * of the cluster and determining how those edges interact with other views. The edges
1344 * essentially define a fine-grained boundary around the cluster of views -- like a more
1345 * precise version of a bounding box.
1346 */
1347 private class ViewCluster {
Sunny Goyal6dc98b92016-04-03 15:25:29 -07001348 final static int LEFT = 1 << 0;
1349 final static int TOP = 1 << 1;
1350 final static int RIGHT = 1 << 2;
1351 final static int BOTTOM = 1 << 3;
Adam Cohen47a876d2012-03-19 13:21:41 -07001352
Adam Cohenf3900c22012-11-16 18:28:11 -08001353 ArrayList<View> views;
1354 ItemConfiguration config;
1355 Rect boundingRect = new Rect();
Adam Cohen47a876d2012-03-19 13:21:41 -07001356
Adam Cohenf3900c22012-11-16 18:28:11 -08001357 int[] leftEdge = new int[mCountY];
1358 int[] rightEdge = new int[mCountY];
1359 int[] topEdge = new int[mCountX];
1360 int[] bottomEdge = new int[mCountX];
Sunny Goyal6dc98b92016-04-03 15:25:29 -07001361 int dirtyEdges;
1362 boolean boundingRectDirty;
Adam Cohenf3900c22012-11-16 18:28:11 -08001363
1364 @SuppressWarnings("unchecked")
1365 public ViewCluster(ArrayList<View> views, ItemConfiguration config) {
1366 this.views = (ArrayList<View>) views.clone();
1367 this.config = config;
1368 resetEdges();
Adam Cohen47a876d2012-03-19 13:21:41 -07001369 }
1370
Adam Cohenf3900c22012-11-16 18:28:11 -08001371 void resetEdges() {
1372 for (int i = 0; i < mCountX; i++) {
1373 topEdge[i] = -1;
1374 bottomEdge[i] = -1;
1375 }
1376 for (int i = 0; i < mCountY; i++) {
1377 leftEdge[i] = -1;
1378 rightEdge[i] = -1;
1379 }
Sunny Goyal6dc98b92016-04-03 15:25:29 -07001380 dirtyEdges = LEFT | TOP | RIGHT | BOTTOM;
Adam Cohenf3900c22012-11-16 18:28:11 -08001381 boundingRectDirty = true;
1382 }
Adam Cohen47a876d2012-03-19 13:21:41 -07001383
Sunny Goyal6dc98b92016-04-03 15:25:29 -07001384 void computeEdge(int which) {
Adam Cohenf3900c22012-11-16 18:28:11 -08001385 int count = views.size();
1386 for (int i = 0; i < count; i++) {
1387 CellAndSpan cs = config.map.get(views.get(i));
1388 switch (which) {
1389 case LEFT:
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001390 int left = cs.cellX;
1391 for (int j = cs.cellY; j < cs.cellY + cs.spanY; j++) {
Sunny Goyal6dc98b92016-04-03 15:25:29 -07001392 if (left < leftEdge[j] || leftEdge[j] < 0) {
1393 leftEdge[j] = left;
Adam Cohena56dc102012-07-13 13:41:42 -07001394 }
Adam Cohen47a876d2012-03-19 13:21:41 -07001395 }
Adam Cohenf3900c22012-11-16 18:28:11 -08001396 break;
1397 case RIGHT:
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001398 int right = cs.cellX + cs.spanX;
1399 for (int j = cs.cellY; j < cs.cellY + cs.spanY; j++) {
Sunny Goyal6dc98b92016-04-03 15:25:29 -07001400 if (right > rightEdge[j]) {
1401 rightEdge[j] = right;
Adam Cohenf3900c22012-11-16 18:28:11 -08001402 }
1403 }
1404 break;
1405 case TOP:
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001406 int top = cs.cellY;
1407 for (int j = cs.cellX; j < cs.cellX + cs.spanX; j++) {
Sunny Goyal6dc98b92016-04-03 15:25:29 -07001408 if (top < topEdge[j] || topEdge[j] < 0) {
1409 topEdge[j] = top;
Adam Cohenf3900c22012-11-16 18:28:11 -08001410 }
1411 }
1412 break;
1413 case BOTTOM:
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001414 int bottom = cs.cellY + cs.spanY;
1415 for (int j = cs.cellX; j < cs.cellX + cs.spanX; j++) {
Sunny Goyal6dc98b92016-04-03 15:25:29 -07001416 if (bottom > bottomEdge[j]) {
1417 bottomEdge[j] = bottom;
Adam Cohenf3900c22012-11-16 18:28:11 -08001418 }
1419 }
1420 break;
Adam Cohen47a876d2012-03-19 13:21:41 -07001421 }
1422 }
1423 }
Adam Cohenf3900c22012-11-16 18:28:11 -08001424
1425 boolean isViewTouchingEdge(View v, int whichEdge) {
1426 CellAndSpan cs = config.map.get(v);
1427
Sunny Goyal6dc98b92016-04-03 15:25:29 -07001428 if ((dirtyEdges & whichEdge) == whichEdge) {
1429 computeEdge(whichEdge);
1430 dirtyEdges &= ~whichEdge;
1431 }
Adam Cohenf3900c22012-11-16 18:28:11 -08001432
1433 switch (whichEdge) {
1434 case LEFT:
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001435 for (int i = cs.cellY; i < cs.cellY + cs.spanY; i++) {
1436 if (leftEdge[i] == cs.cellX + cs.spanX) {
Adam Cohenf3900c22012-11-16 18:28:11 -08001437 return true;
1438 }
1439 }
1440 break;
1441 case RIGHT:
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001442 for (int i = cs.cellY; i < cs.cellY + cs.spanY; i++) {
1443 if (rightEdge[i] == cs.cellX) {
Adam Cohenf3900c22012-11-16 18:28:11 -08001444 return true;
1445 }
1446 }
1447 break;
1448 case TOP:
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001449 for (int i = cs.cellX; i < cs.cellX + cs.spanX; i++) {
1450 if (topEdge[i] == cs.cellY + cs.spanY) {
Adam Cohenf3900c22012-11-16 18:28:11 -08001451 return true;
1452 }
1453 }
1454 break;
1455 case BOTTOM:
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001456 for (int i = cs.cellX; i < cs.cellX + cs.spanX; i++) {
1457 if (bottomEdge[i] == cs.cellY) {
Adam Cohenf3900c22012-11-16 18:28:11 -08001458 return true;
1459 }
1460 }
1461 break;
1462 }
1463 return false;
1464 }
1465
1466 void shift(int whichEdge, int delta) {
1467 for (View v: views) {
1468 CellAndSpan c = config.map.get(v);
1469 switch (whichEdge) {
1470 case LEFT:
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001471 c.cellX -= delta;
Adam Cohenf3900c22012-11-16 18:28:11 -08001472 break;
1473 case RIGHT:
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001474 c.cellX += delta;
Adam Cohenf3900c22012-11-16 18:28:11 -08001475 break;
1476 case TOP:
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001477 c.cellY -= delta;
Adam Cohenf3900c22012-11-16 18:28:11 -08001478 break;
1479 case BOTTOM:
1480 default:
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001481 c.cellY += delta;
Adam Cohenf3900c22012-11-16 18:28:11 -08001482 break;
1483 }
1484 }
1485 resetEdges();
1486 }
1487
1488 public void addView(View v) {
1489 views.add(v);
1490 resetEdges();
1491 }
1492
1493 public Rect getBoundingRect() {
1494 if (boundingRectDirty) {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001495 config.getBoundingRectForViews(views, boundingRect);
Adam Cohenf3900c22012-11-16 18:28:11 -08001496 }
1497 return boundingRect;
1498 }
1499
Adam Cohenf3900c22012-11-16 18:28:11 -08001500 PositionComparator comparator = new PositionComparator();
1501 class PositionComparator implements Comparator<View> {
1502 int whichEdge = 0;
1503 public int compare(View left, View right) {
1504 CellAndSpan l = config.map.get(left);
1505 CellAndSpan r = config.map.get(right);
1506 switch (whichEdge) {
1507 case LEFT:
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001508 return (r.cellX + r.spanX) - (l.cellX + l.spanX);
Adam Cohenf3900c22012-11-16 18:28:11 -08001509 case RIGHT:
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001510 return l.cellX - r.cellX;
Adam Cohenf3900c22012-11-16 18:28:11 -08001511 case TOP:
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001512 return (r.cellY + r.spanY) - (l.cellY + l.spanY);
Adam Cohenf3900c22012-11-16 18:28:11 -08001513 case BOTTOM:
1514 default:
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001515 return l.cellY - r.cellY;
Adam Cohenf3900c22012-11-16 18:28:11 -08001516 }
1517 }
1518 }
1519
1520 public void sortConfigurationForEdgePush(int edge) {
1521 comparator.whichEdge = edge;
1522 Collections.sort(config.sortedViews, comparator);
1523 }
Adam Cohen47a876d2012-03-19 13:21:41 -07001524 }
1525
Adam Cohenf3900c22012-11-16 18:28:11 -08001526 private boolean pushViewsToTempLocation(ArrayList<View> views, Rect rectOccupiedByPotentialDrop,
1527 int[] direction, View dragView, ItemConfiguration currentState) {
Adam Cohene0489502012-08-27 15:18:53 -07001528
Adam Cohenf3900c22012-11-16 18:28:11 -08001529 ViewCluster cluster = new ViewCluster(views, currentState);
1530 Rect clusterRect = cluster.getBoundingRect();
1531 int whichEdge;
1532 int pushDistance;
1533 boolean fail = false;
1534
1535 // Determine the edge of the cluster that will be leading the push and how far
1536 // the cluster must be shifted.
1537 if (direction[0] < 0) {
1538 whichEdge = ViewCluster.LEFT;
1539 pushDistance = clusterRect.right - rectOccupiedByPotentialDrop.left;
Adam Cohene0489502012-08-27 15:18:53 -07001540 } else if (direction[0] > 0) {
Adam Cohenf3900c22012-11-16 18:28:11 -08001541 whichEdge = ViewCluster.RIGHT;
1542 pushDistance = rectOccupiedByPotentialDrop.right - clusterRect.left;
1543 } else if (direction[1] < 0) {
1544 whichEdge = ViewCluster.TOP;
1545 pushDistance = clusterRect.bottom - rectOccupiedByPotentialDrop.top;
1546 } else {
1547 whichEdge = ViewCluster.BOTTOM;
1548 pushDistance = rectOccupiedByPotentialDrop.bottom - clusterRect.top;
Adam Cohene0489502012-08-27 15:18:53 -07001549 }
1550
Adam Cohenf3900c22012-11-16 18:28:11 -08001551 // Break early for invalid push distance.
1552 if (pushDistance <= 0) {
1553 return false;
Adam Cohene0489502012-08-27 15:18:53 -07001554 }
Adam Cohenf3900c22012-11-16 18:28:11 -08001555
1556 // Mark the occupied state as false for the group of views we want to move.
1557 for (View v: views) {
1558 CellAndSpan c = currentState.map.get(v);
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001559 mTmpOccupied.markCells(c, false);
Adam Cohenf3900c22012-11-16 18:28:11 -08001560 }
1561
1562 // We save the current configuration -- if we fail to find a solution we will revert
1563 // to the initial state. The process of finding a solution modifies the configuration
1564 // in place, hence the need for revert in the failure case.
1565 currentState.save();
1566
1567 // The pushing algorithm is simplified by considering the views in the order in which
1568 // they would be pushed by the cluster. For example, if the cluster is leading with its
1569 // left edge, we consider sort the views by their right edge, from right to left.
1570 cluster.sortConfigurationForEdgePush(whichEdge);
1571
1572 while (pushDistance > 0 && !fail) {
1573 for (View v: currentState.sortedViews) {
1574 // For each view that isn't in the cluster, we see if the leading edge of the
1575 // cluster is contacting the edge of that view. If so, we add that view to the
1576 // cluster.
1577 if (!cluster.views.contains(v) && v != dragView) {
1578 if (cluster.isViewTouchingEdge(v, whichEdge)) {
1579 LayoutParams lp = (LayoutParams) v.getLayoutParams();
1580 if (!lp.canReorder) {
1581 // The push solution includes the all apps button, this is not viable.
1582 fail = true;
1583 break;
1584 }
1585 cluster.addView(v);
1586 CellAndSpan c = currentState.map.get(v);
1587
1588 // Adding view to cluster, mark it as not occupied.
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001589 mTmpOccupied.markCells(c, false);
Adam Cohenf3900c22012-11-16 18:28:11 -08001590 }
1591 }
1592 }
1593 pushDistance--;
1594
1595 // The cluster has been completed, now we move the whole thing over in the appropriate
1596 // direction.
1597 cluster.shift(whichEdge, 1);
1598 }
1599
1600 boolean foundSolution = false;
1601 clusterRect = cluster.getBoundingRect();
1602
1603 // Due to the nature of the algorithm, the only check required to verify a valid solution
1604 // is to ensure that completed shifted cluster lies completely within the cell layout.
1605 if (!fail && clusterRect.left >= 0 && clusterRect.right <= mCountX && clusterRect.top >= 0 &&
1606 clusterRect.bottom <= mCountY) {
1607 foundSolution = true;
1608 } else {
1609 currentState.restore();
1610 }
1611
1612 // In either case, we set the occupied array as marked for the location of the views
1613 for (View v: cluster.views) {
1614 CellAndSpan c = currentState.map.get(v);
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001615 mTmpOccupied.markCells(c, true);
Adam Cohenf3900c22012-11-16 18:28:11 -08001616 }
1617
1618 return foundSolution;
Adam Cohene0489502012-08-27 15:18:53 -07001619 }
1620
Adam Cohen482ed822012-03-02 14:15:13 -08001621 private boolean addViewsToTempLocation(ArrayList<View> views, Rect rectOccupiedByPotentialDrop,
Adam Cohenf3900c22012-11-16 18:28:11 -08001622 int[] direction, View dragView, ItemConfiguration currentState) {
Adam Cohen482ed822012-03-02 14:15:13 -08001623 if (views.size() == 0) return true;
Adam Cohen482ed822012-03-02 14:15:13 -08001624
Adam Cohen8baab352012-03-20 17:39:21 -07001625 boolean success = false;
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001626 Rect boundingRect = new Rect();
Adam Cohen8baab352012-03-20 17:39:21 -07001627 // We construct a rect which represents the entire group of views passed in
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001628 currentState.getBoundingRectForViews(views, boundingRect);
Adam Cohen8baab352012-03-20 17:39:21 -07001629
Adam Cohen8baab352012-03-20 17:39:21 -07001630 // Mark the occupied state as false for the group of views we want to move.
Adam Cohenf3900c22012-11-16 18:28:11 -08001631 for (View v: views) {
Adam Cohen8baab352012-03-20 17:39:21 -07001632 CellAndSpan c = currentState.map.get(v);
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001633 mTmpOccupied.markCells(c, false);
Adam Cohen8baab352012-03-20 17:39:21 -07001634 }
1635
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001636 GridOccupancy blockOccupied = new GridOccupancy(boundingRect.width(), boundingRect.height());
Adam Cohen47a876d2012-03-19 13:21:41 -07001637 int top = boundingRect.top;
1638 int left = boundingRect.left;
Adam Cohen8baab352012-03-20 17:39:21 -07001639 // We mark more precisely which parts of the bounding rect are truly occupied, allowing
Adam Cohena56dc102012-07-13 13:41:42 -07001640 // for interlocking.
Adam Cohenf3900c22012-11-16 18:28:11 -08001641 for (View v: views) {
Adam Cohen8baab352012-03-20 17:39:21 -07001642 CellAndSpan c = currentState.map.get(v);
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001643 blockOccupied.markCells(c.cellX - left, c.cellY - top, c.spanX, c.spanY, true);
Adam Cohen47a876d2012-03-19 13:21:41 -07001644 }
1645
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001646 mTmpOccupied.markCells(rectOccupiedByPotentialDrop, true);
Adam Cohen482ed822012-03-02 14:15:13 -08001647
Adam Cohenf3900c22012-11-16 18:28:11 -08001648 findNearestArea(boundingRect.left, boundingRect.top, boundingRect.width(),
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001649 boundingRect.height(), direction,
1650 mTmpOccupied.cells, blockOccupied.cells, mTempLocation);
Adam Cohen482ed822012-03-02 14:15:13 -08001651
Adam Cohen8baab352012-03-20 17:39:21 -07001652 // If we successfuly found a location by pushing the block of views, we commit it
Adam Cohen482ed822012-03-02 14:15:13 -08001653 if (mTempLocation[0] >= 0 && mTempLocation[1] >= 0) {
Adam Cohen8baab352012-03-20 17:39:21 -07001654 int deltaX = mTempLocation[0] - boundingRect.left;
1655 int deltaY = mTempLocation[1] - boundingRect.top;
Adam Cohenf3900c22012-11-16 18:28:11 -08001656 for (View v: views) {
Adam Cohen8baab352012-03-20 17:39:21 -07001657 CellAndSpan c = currentState.map.get(v);
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001658 c.cellX += deltaX;
1659 c.cellY += deltaY;
Adam Cohen482ed822012-03-02 14:15:13 -08001660 }
1661 success = true;
1662 }
Adam Cohen8baab352012-03-20 17:39:21 -07001663
1664 // In either case, we set the occupied array as marked for the location of the views
Adam Cohenf3900c22012-11-16 18:28:11 -08001665 for (View v: views) {
Adam Cohen8baab352012-03-20 17:39:21 -07001666 CellAndSpan c = currentState.map.get(v);
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001667 mTmpOccupied.markCells(c, true);
Adam Cohen482ed822012-03-02 14:15:13 -08001668 }
1669 return success;
1670 }
1671
Adam Cohen4abc5bd2012-05-29 21:06:03 -07001672 // This method tries to find a reordering solution which satisfies the push mechanic by trying
1673 // to push items in each of the cardinal directions, in an order based on the direction vector
1674 // passed.
1675 private boolean attemptPushInDirection(ArrayList<View> intersectingViews, Rect occupied,
1676 int[] direction, View ignoreView, ItemConfiguration solution) {
1677 if ((Math.abs(direction[0]) + Math.abs(direction[1])) > 1) {
Winson Chung5f8afe62013-08-12 16:19:28 -07001678 // If the direction vector has two non-zero components, we try pushing
Adam Cohen4abc5bd2012-05-29 21:06:03 -07001679 // separately in each of the components.
1680 int temp = direction[1];
1681 direction[1] = 0;
Adam Cohenf3900c22012-11-16 18:28:11 -08001682
1683 if (pushViewsToTempLocation(intersectingViews, occupied, direction,
Adam Cohen4abc5bd2012-05-29 21:06:03 -07001684 ignoreView, solution)) {
1685 return true;
1686 }
1687 direction[1] = temp;
1688 temp = direction[0];
1689 direction[0] = 0;
Adam Cohenf3900c22012-11-16 18:28:11 -08001690
1691 if (pushViewsToTempLocation(intersectingViews, occupied, direction,
Adam Cohen4abc5bd2012-05-29 21:06:03 -07001692 ignoreView, solution)) {
1693 return true;
1694 }
1695 // Revert the direction
1696 direction[0] = temp;
1697
1698 // Now we try pushing in each component of the opposite direction
1699 direction[0] *= -1;
1700 direction[1] *= -1;
1701 temp = direction[1];
1702 direction[1] = 0;
Adam Cohenf3900c22012-11-16 18:28:11 -08001703 if (pushViewsToTempLocation(intersectingViews, occupied, direction,
Adam Cohen4abc5bd2012-05-29 21:06:03 -07001704 ignoreView, solution)) {
1705 return true;
1706 }
1707
1708 direction[1] = temp;
1709 temp = direction[0];
1710 direction[0] = 0;
Adam Cohenf3900c22012-11-16 18:28:11 -08001711 if (pushViewsToTempLocation(intersectingViews, occupied, direction,
Adam Cohen4abc5bd2012-05-29 21:06:03 -07001712 ignoreView, solution)) {
1713 return true;
1714 }
1715 // revert the direction
1716 direction[0] = temp;
1717 direction[0] *= -1;
1718 direction[1] *= -1;
Winson Chung5f8afe62013-08-12 16:19:28 -07001719
Adam Cohen4abc5bd2012-05-29 21:06:03 -07001720 } else {
1721 // If the direction vector has a single non-zero component, we push first in the
1722 // direction of the vector
Adam Cohenf3900c22012-11-16 18:28:11 -08001723 if (pushViewsToTempLocation(intersectingViews, occupied, direction,
Adam Cohen4abc5bd2012-05-29 21:06:03 -07001724 ignoreView, solution)) {
1725 return true;
1726 }
Adam Cohen4abc5bd2012-05-29 21:06:03 -07001727 // Then we try the opposite direction
1728 direction[0] *= -1;
1729 direction[1] *= -1;
Adam Cohenf3900c22012-11-16 18:28:11 -08001730 if (pushViewsToTempLocation(intersectingViews, occupied, direction,
Adam Cohen4abc5bd2012-05-29 21:06:03 -07001731 ignoreView, solution)) {
1732 return true;
1733 }
1734 // Switch the direction back
1735 direction[0] *= -1;
1736 direction[1] *= -1;
Winson Chung5f8afe62013-08-12 16:19:28 -07001737
1738 // If we have failed to find a push solution with the above, then we try
Adam Cohen4abc5bd2012-05-29 21:06:03 -07001739 // to find a solution by pushing along the perpendicular axis.
1740
1741 // Swap the components
1742 int temp = direction[1];
1743 direction[1] = direction[0];
1744 direction[0] = temp;
Adam Cohenf3900c22012-11-16 18:28:11 -08001745 if (pushViewsToTempLocation(intersectingViews, occupied, direction,
Adam Cohen4abc5bd2012-05-29 21:06:03 -07001746 ignoreView, solution)) {
1747 return true;
1748 }
1749
1750 // Then we try the opposite direction
1751 direction[0] *= -1;
1752 direction[1] *= -1;
Adam Cohenf3900c22012-11-16 18:28:11 -08001753 if (pushViewsToTempLocation(intersectingViews, occupied, direction,
Adam Cohen4abc5bd2012-05-29 21:06:03 -07001754 ignoreView, solution)) {
1755 return true;
1756 }
1757 // Switch the direction back
1758 direction[0] *= -1;
1759 direction[1] *= -1;
1760
1761 // Swap the components back
1762 temp = direction[1];
1763 direction[1] = direction[0];
1764 direction[0] = temp;
1765 }
1766 return false;
1767 }
1768
Adam Cohen482ed822012-03-02 14:15:13 -08001769 private boolean rearrangementExists(int cellX, int cellY, int spanX, int spanY, int[] direction,
Adam Cohen8baab352012-03-20 17:39:21 -07001770 View ignoreView, ItemConfiguration solution) {
Winson Chunge3e03bc2012-05-01 15:10:11 -07001771 // Return early if get invalid cell positions
1772 if (cellX < 0 || cellY < 0) return false;
Adam Cohen482ed822012-03-02 14:15:13 -08001773
Adam Cohen8baab352012-03-20 17:39:21 -07001774 mIntersectingViews.clear();
Adam Cohen482ed822012-03-02 14:15:13 -08001775 mOccupiedRect.set(cellX, cellY, cellX + spanX, cellY + spanY);
Adam Cohen482ed822012-03-02 14:15:13 -08001776
Adam Cohen8baab352012-03-20 17:39:21 -07001777 // Mark the desired location of the view currently being dragged.
Adam Cohen482ed822012-03-02 14:15:13 -08001778 if (ignoreView != null) {
Adam Cohen8baab352012-03-20 17:39:21 -07001779 CellAndSpan c = solution.map.get(ignoreView);
Adam Cohen19f37922012-03-21 11:59:11 -07001780 if (c != null) {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001781 c.cellX = cellX;
1782 c.cellY = cellY;
Adam Cohen19f37922012-03-21 11:59:11 -07001783 }
Adam Cohen482ed822012-03-02 14:15:13 -08001784 }
Adam Cohen482ed822012-03-02 14:15:13 -08001785 Rect r0 = new Rect(cellX, cellY, cellX + spanX, cellY + spanY);
1786 Rect r1 = new Rect();
Adam Cohen8baab352012-03-20 17:39:21 -07001787 for (View child: solution.map.keySet()) {
Adam Cohen482ed822012-03-02 14:15:13 -08001788 if (child == ignoreView) continue;
Adam Cohen8baab352012-03-20 17:39:21 -07001789 CellAndSpan c = solution.map.get(child);
Adam Cohen482ed822012-03-02 14:15:13 -08001790 LayoutParams lp = (LayoutParams) child.getLayoutParams();
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001791 r1.set(c.cellX, c.cellY, c.cellX + c.spanX, c.cellY + c.spanY);
Adam Cohen482ed822012-03-02 14:15:13 -08001792 if (Rect.intersects(r0, r1)) {
1793 if (!lp.canReorder) {
1794 return false;
1795 }
1796 mIntersectingViews.add(child);
1797 }
1798 }
Adam Cohen47a876d2012-03-19 13:21:41 -07001799
Adam Cohenfa3c58f2013-12-06 16:10:55 -08001800 solution.intersectingViews = new ArrayList<View>(mIntersectingViews);
1801
Winson Chung5f8afe62013-08-12 16:19:28 -07001802 // First we try to find a solution which respects the push mechanic. That is,
Adam Cohen4abc5bd2012-05-29 21:06:03 -07001803 // we try to find a solution such that no displaced item travels through another item
1804 // without also displacing that item.
1805 if (attemptPushInDirection(mIntersectingViews, mOccupiedRect, direction, ignoreView,
Adam Cohen19f37922012-03-21 11:59:11 -07001806 solution)) {
Adam Cohen47a876d2012-03-19 13:21:41 -07001807 return true;
1808 }
Adam Cohen47a876d2012-03-19 13:21:41 -07001809
Adam Cohen4abc5bd2012-05-29 21:06:03 -07001810 // Next we try moving the views as a block, but without requiring the push mechanic.
Adam Cohenf3900c22012-11-16 18:28:11 -08001811 if (addViewsToTempLocation(mIntersectingViews, mOccupiedRect, direction, ignoreView,
Adam Cohen19f37922012-03-21 11:59:11 -07001812 solution)) {
Adam Cohen482ed822012-03-02 14:15:13 -08001813 return true;
1814 }
Adam Cohen47a876d2012-03-19 13:21:41 -07001815
Adam Cohen482ed822012-03-02 14:15:13 -08001816 // Ok, they couldn't move as a block, let's move them individually
1817 for (View v : mIntersectingViews) {
Adam Cohen8baab352012-03-20 17:39:21 -07001818 if (!addViewToTempLocation(v, mOccupiedRect, direction, solution)) {
Adam Cohen482ed822012-03-02 14:15:13 -08001819 return false;
1820 }
1821 }
1822 return true;
1823 }
1824
1825 /*
1826 * Returns a pair (x, y), where x,y are in {-1, 0, 1} corresponding to vector between
1827 * the provided point and the provided cell
1828 */
Adam Cohen47a876d2012-03-19 13:21:41 -07001829 private void computeDirectionVector(float deltaX, float deltaY, int[] result) {
Jon Mirandae96798e2016-12-07 12:10:44 -08001830 double angle = Math.atan(deltaY / deltaX);
Adam Cohen482ed822012-03-02 14:15:13 -08001831
1832 result[0] = 0;
1833 result[1] = 0;
1834 if (Math.abs(Math.cos(angle)) > 0.5f) {
1835 result[0] = (int) Math.signum(deltaX);
1836 }
1837 if (Math.abs(Math.sin(angle)) > 0.5f) {
1838 result[1] = (int) Math.signum(deltaY);
1839 }
1840 }
1841
Sunny Goyalf7a29e82015-04-24 15:20:43 -07001842 private ItemConfiguration findReorderSolution(int pixelX, int pixelY, int minSpanX, int minSpanY,
Adam Cohenfa3c58f2013-12-06 16:10:55 -08001843 int spanX, int spanY, int[] direction, View dragView, boolean decX,
1844 ItemConfiguration solution) {
Adam Cohen8baab352012-03-20 17:39:21 -07001845 // Copy the current state into the solution. This solution will be manipulated as necessary.
1846 copyCurrentStateToSolution(solution, false);
1847 // Copy the current occupied array into the temporary occupied array. This array will be
1848 // manipulated as necessary to find a solution.
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001849 mOccupied.copyTo(mTmpOccupied);
Adam Cohen482ed822012-03-02 14:15:13 -08001850
1851 // We find the nearest cell into which we would place the dragged item, assuming there's
1852 // nothing in its way.
1853 int result[] = new int[2];
1854 result = findNearestArea(pixelX, pixelY, spanX, spanY, result);
1855
1856 boolean success = false;
1857 // First we try the exact nearest position of the item being dragged,
1858 // we will then want to try to move this around to other neighbouring positions
Adam Cohen8baab352012-03-20 17:39:21 -07001859 success = rearrangementExists(result[0], result[1], spanX, spanY, direction, dragView,
1860 solution);
Adam Cohen482ed822012-03-02 14:15:13 -08001861
1862 if (!success) {
1863 // We try shrinking the widget down to size in an alternating pattern, shrink 1 in
1864 // x, then 1 in y etc.
1865 if (spanX > minSpanX && (minSpanY == spanY || decX)) {
Adam Cohenfa3c58f2013-12-06 16:10:55 -08001866 return findReorderSolution(pixelX, pixelY, minSpanX, minSpanY, spanX - 1, spanY,
1867 direction, dragView, false, solution);
Adam Cohen482ed822012-03-02 14:15:13 -08001868 } else if (spanY > minSpanY) {
Adam Cohenfa3c58f2013-12-06 16:10:55 -08001869 return findReorderSolution(pixelX, pixelY, minSpanX, minSpanY, spanX, spanY - 1,
1870 direction, dragView, true, solution);
Adam Cohen482ed822012-03-02 14:15:13 -08001871 }
1872 solution.isSolution = false;
1873 } else {
1874 solution.isSolution = true;
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001875 solution.cellX = result[0];
1876 solution.cellY = result[1];
1877 solution.spanX = spanX;
1878 solution.spanY = spanY;
Adam Cohen482ed822012-03-02 14:15:13 -08001879 }
1880 return solution;
1881 }
1882
1883 private void copyCurrentStateToSolution(ItemConfiguration solution, boolean temp) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07001884 int childCount = mShortcutsAndWidgets.getChildCount();
Adam Cohen482ed822012-03-02 14:15:13 -08001885 for (int i = 0; i < childCount; i++) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07001886 View child = mShortcutsAndWidgets.getChildAt(i);
Adam Cohen482ed822012-03-02 14:15:13 -08001887 LayoutParams lp = (LayoutParams) child.getLayoutParams();
Adam Cohen8baab352012-03-20 17:39:21 -07001888 CellAndSpan c;
Adam Cohen482ed822012-03-02 14:15:13 -08001889 if (temp) {
Adam Cohen8baab352012-03-20 17:39:21 -07001890 c = new CellAndSpan(lp.tmpCellX, lp.tmpCellY, lp.cellHSpan, lp.cellVSpan);
Adam Cohen482ed822012-03-02 14:15:13 -08001891 } else {
Adam Cohen8baab352012-03-20 17:39:21 -07001892 c = new CellAndSpan(lp.cellX, lp.cellY, lp.cellHSpan, lp.cellVSpan);
Adam Cohen482ed822012-03-02 14:15:13 -08001893 }
Adam Cohenf3900c22012-11-16 18:28:11 -08001894 solution.add(child, c);
Adam Cohen482ed822012-03-02 14:15:13 -08001895 }
1896 }
1897
1898 private void copySolutionToTempState(ItemConfiguration solution, View dragView) {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001899 mTmpOccupied.clear();
Adam Cohen482ed822012-03-02 14:15:13 -08001900
Michael Jurkaa52570f2012-03-20 03:18:20 -07001901 int childCount = mShortcutsAndWidgets.getChildCount();
Adam Cohen482ed822012-03-02 14:15:13 -08001902 for (int i = 0; i < childCount; i++) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07001903 View child = mShortcutsAndWidgets.getChildAt(i);
Adam Cohen482ed822012-03-02 14:15:13 -08001904 if (child == dragView) continue;
1905 LayoutParams lp = (LayoutParams) child.getLayoutParams();
Adam Cohen8baab352012-03-20 17:39:21 -07001906 CellAndSpan c = solution.map.get(child);
1907 if (c != null) {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001908 lp.tmpCellX = c.cellX;
1909 lp.tmpCellY = c.cellY;
Adam Cohen8baab352012-03-20 17:39:21 -07001910 lp.cellHSpan = c.spanX;
1911 lp.cellVSpan = c.spanY;
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001912 mTmpOccupied.markCells(c, true);
Adam Cohen482ed822012-03-02 14:15:13 -08001913 }
1914 }
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001915 mTmpOccupied.markCells(solution, true);
Adam Cohen482ed822012-03-02 14:15:13 -08001916 }
1917
1918 private void animateItemsToSolution(ItemConfiguration solution, View dragView, boolean
1919 commitDragView) {
1920
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001921 GridOccupancy occupied = DESTRUCTIVE_REORDER ? mOccupied : mTmpOccupied;
1922 occupied.clear();
Adam Cohen482ed822012-03-02 14:15:13 -08001923
Michael Jurkaa52570f2012-03-20 03:18:20 -07001924 int childCount = mShortcutsAndWidgets.getChildCount();
Adam Cohen482ed822012-03-02 14:15:13 -08001925 for (int i = 0; i < childCount; i++) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07001926 View child = mShortcutsAndWidgets.getChildAt(i);
Adam Cohen482ed822012-03-02 14:15:13 -08001927 if (child == dragView) continue;
Adam Cohen8baab352012-03-20 17:39:21 -07001928 CellAndSpan c = solution.map.get(child);
1929 if (c != null) {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001930 animateChildToPosition(child, c.cellX, c.cellY, REORDER_ANIMATION_DURATION, 0,
Adam Cohen19f37922012-03-21 11:59:11 -07001931 DESTRUCTIVE_REORDER, false);
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001932 occupied.markCells(c, true);
Adam Cohen482ed822012-03-02 14:15:13 -08001933 }
1934 }
1935 if (commitDragView) {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001936 occupied.markCells(solution, true);
Adam Cohen482ed822012-03-02 14:15:13 -08001937 }
1938 }
1939
Adam Cohenfa3c58f2013-12-06 16:10:55 -08001940
1941 // This method starts or changes the reorder preview animations
1942 private void beginOrAdjustReorderPreviewAnimations(ItemConfiguration solution,
1943 View dragView, int delay, int mode) {
Adam Cohen19f37922012-03-21 11:59:11 -07001944 int childCount = mShortcutsAndWidgets.getChildCount();
Adam Cohen19f37922012-03-21 11:59:11 -07001945 for (int i = 0; i < childCount; i++) {
1946 View child = mShortcutsAndWidgets.getChildAt(i);
1947 if (child == dragView) continue;
1948 CellAndSpan c = solution.map.get(child);
Adam Cohenfa3c58f2013-12-06 16:10:55 -08001949 boolean skip = mode == ReorderPreviewAnimation.MODE_HINT && solution.intersectingViews
1950 != null && !solution.intersectingViews.contains(child);
1951
Adam Cohen19f37922012-03-21 11:59:11 -07001952 LayoutParams lp = (LayoutParams) child.getLayoutParams();
Adam Cohenfa3c58f2013-12-06 16:10:55 -08001953 if (c != null && !skip) {
1954 ReorderPreviewAnimation rha = new ReorderPreviewAnimation(child, mode, lp.cellX,
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001955 lp.cellY, c.cellX, c.cellY, c.spanX, c.spanY);
Adam Cohend024f982012-05-23 18:26:45 -07001956 rha.animate();
Adam Cohen19f37922012-03-21 11:59:11 -07001957 }
1958 }
1959 }
1960
Adam Cohenfa3c58f2013-12-06 16:10:55 -08001961 // Class which represents the reorder preview animations. These animations show that an item is
Adam Cohen19f37922012-03-21 11:59:11 -07001962 // in a temporary state, and hint at where the item will return to.
Adam Cohenfa3c58f2013-12-06 16:10:55 -08001963 class ReorderPreviewAnimation {
Adam Cohen19f37922012-03-21 11:59:11 -07001964 View child;
Adam Cohend024f982012-05-23 18:26:45 -07001965 float finalDeltaX;
1966 float finalDeltaY;
1967 float initDeltaX;
1968 float initDeltaY;
1969 float finalScale;
1970 float initScale;
Adam Cohenfa3c58f2013-12-06 16:10:55 -08001971 int mode;
1972 boolean repeating = false;
1973 private static final int PREVIEW_DURATION = 300;
1974 private static final int HINT_DURATION = Workspace.REORDER_TIMEOUT;
1975
Jon Miranda21266912016-12-19 14:12:05 -08001976 private static final float CHILD_DIVIDEND = 4.0f;
1977
Adam Cohenfa3c58f2013-12-06 16:10:55 -08001978 public static final int MODE_HINT = 0;
1979 public static final int MODE_PREVIEW = 1;
1980
Adam Cohene7587d22012-05-24 18:50:02 -07001981 Animator a;
Adam Cohen19f37922012-03-21 11:59:11 -07001982
Adam Cohenfa3c58f2013-12-06 16:10:55 -08001983 public ReorderPreviewAnimation(View child, int mode, int cellX0, int cellY0, int cellX1,
1984 int cellY1, int spanX, int spanY) {
Adam Cohen19f37922012-03-21 11:59:11 -07001985 regionToCenterPoint(cellX0, cellY0, spanX, spanY, mTmpPoint);
1986 final int x0 = mTmpPoint[0];
1987 final int y0 = mTmpPoint[1];
1988 regionToCenterPoint(cellX1, cellY1, spanX, spanY, mTmpPoint);
1989 final int x1 = mTmpPoint[0];
1990 final int y1 = mTmpPoint[1];
1991 final int dX = x1 - x0;
1992 final int dY = y1 - y0;
Jon Miranda21266912016-12-19 14:12:05 -08001993
1994 this.child = child;
1995 this.mode = mode;
1996 setInitialAnimationValues(false);
1997 finalScale = (mChildScale - (CHILD_DIVIDEND / child.getWidth())) * initScale;
1998 finalDeltaX = initDeltaX;
1999 finalDeltaY = initDeltaY;
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002000 int dir = mode == MODE_HINT ? -1 : 1;
Adam Cohen19f37922012-03-21 11:59:11 -07002001 if (dX == dY && dX == 0) {
2002 } else {
2003 if (dY == 0) {
Jon Miranda21266912016-12-19 14:12:05 -08002004 finalDeltaX += - dir * Math.signum(dX) * mReorderPreviewAnimationMagnitude;
Adam Cohen19f37922012-03-21 11:59:11 -07002005 } else if (dX == 0) {
Jon Miranda21266912016-12-19 14:12:05 -08002006 finalDeltaY += - dir * Math.signum(dY) * mReorderPreviewAnimationMagnitude;
Adam Cohen19f37922012-03-21 11:59:11 -07002007 } else {
2008 double angle = Math.atan( (float) (dY) / dX);
Jon Miranda21266912016-12-19 14:12:05 -08002009 finalDeltaX += (int) (- dir * Math.signum(dX) *
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002010 Math.abs(Math.cos(angle) * mReorderPreviewAnimationMagnitude));
Jon Miranda21266912016-12-19 14:12:05 -08002011 finalDeltaY += (int) (- dir * Math.signum(dY) *
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002012 Math.abs(Math.sin(angle) * mReorderPreviewAnimationMagnitude));
Adam Cohen19f37922012-03-21 11:59:11 -07002013 }
2014 }
Jon Miranda21266912016-12-19 14:12:05 -08002015 }
2016
2017 void setInitialAnimationValues(boolean restoreOriginalValues) {
2018 if (restoreOriginalValues) {
2019 if (child instanceof LauncherAppWidgetHostView) {
2020 LauncherAppWidgetHostView lahv = (LauncherAppWidgetHostView) child;
2021 initScale = lahv.getScaleToFit();
2022 initDeltaX = lahv.getTranslationForCentering().x;
2023 initDeltaY = lahv.getTranslationForCentering().y;
2024 } else {
2025 initScale = mChildScale;
2026 initDeltaX = 0;
2027 initDeltaY = 0;
2028 }
2029 } else {
2030 initScale = child.getScaleX();
2031 initDeltaX = child.getTranslationX();
2032 initDeltaY = child.getTranslationY();
2033 }
Adam Cohen19f37922012-03-21 11:59:11 -07002034 }
2035
Adam Cohend024f982012-05-23 18:26:45 -07002036 void animate() {
Jon Miranda21266912016-12-19 14:12:05 -08002037 boolean noMovement = (finalDeltaX == initDeltaX) && (finalDeltaY == initDeltaY);
2038
Adam Cohen19f37922012-03-21 11:59:11 -07002039 if (mShakeAnimators.containsKey(child)) {
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002040 ReorderPreviewAnimation oldAnimation = mShakeAnimators.get(child);
Adam Cohend024f982012-05-23 18:26:45 -07002041 oldAnimation.cancel();
Adam Cohen19f37922012-03-21 11:59:11 -07002042 mShakeAnimators.remove(child);
Jon Miranda21266912016-12-19 14:12:05 -08002043 if (noMovement) {
Adam Cohene7587d22012-05-24 18:50:02 -07002044 completeAnimationImmediately();
2045 return;
2046 }
Adam Cohen19f37922012-03-21 11:59:11 -07002047 }
Jon Miranda21266912016-12-19 14:12:05 -08002048 if (noMovement) {
Adam Cohen19f37922012-03-21 11:59:11 -07002049 return;
2050 }
Jon Mirandacda3bfb2017-02-06 15:54:41 -08002051 ValueAnimator va = LauncherAnimUtils.ofFloat(0f, 1f);
Adam Cohene7587d22012-05-24 18:50:02 -07002052 a = va;
Tony Wickham9e0702f2015-09-02 14:45:39 -07002053
2054 // Animations are disabled in power save mode, causing the repeated animation to jump
2055 // spastically between beginning and end states. Since this looks bad, we don't repeat
2056 // the animation in power save mode.
Tony Wickham112ac952015-11-12 12:31:50 -08002057 if (!Utilities.isPowerSaverOn(getContext())) {
Tony Wickham9e0702f2015-09-02 14:45:39 -07002058 va.setRepeatMode(ValueAnimator.REVERSE);
2059 va.setRepeatCount(ValueAnimator.INFINITE);
2060 }
2061
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002062 va.setDuration(mode == MODE_HINT ? HINT_DURATION : PREVIEW_DURATION);
Adam Cohend024f982012-05-23 18:26:45 -07002063 va.setStartDelay((int) (Math.random() * 60));
Adam Cohen19f37922012-03-21 11:59:11 -07002064 va.addUpdateListener(new AnimatorUpdateListener() {
2065 @Override
2066 public void onAnimationUpdate(ValueAnimator animation) {
Jon Mirandae96798e2016-12-07 12:10:44 -08002067 float r = (Float) animation.getAnimatedValue();
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002068 float r1 = (mode == MODE_HINT && repeating) ? 1.0f : r;
2069 float x = r1 * finalDeltaX + (1 - r1) * initDeltaX;
2070 float y = r1 * finalDeltaY + (1 - r1) * initDeltaY;
Adam Cohen19f37922012-03-21 11:59:11 -07002071 child.setTranslationX(x);
2072 child.setTranslationY(y);
Adam Cohend024f982012-05-23 18:26:45 -07002073 float s = r * finalScale + (1 - r) * initScale;
Brandon Keely50e6e562012-05-08 16:28:49 -07002074 child.setScaleX(s);
2075 child.setScaleY(s);
Adam Cohen19f37922012-03-21 11:59:11 -07002076 }
2077 });
2078 va.addListener(new AnimatorListenerAdapter() {
2079 public void onAnimationRepeat(Animator animation) {
Adam Cohen19f37922012-03-21 11:59:11 -07002080 // We make sure to end only after a full period
Jon Miranda21266912016-12-19 14:12:05 -08002081 setInitialAnimationValues(true);
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002082 repeating = true;
Adam Cohen19f37922012-03-21 11:59:11 -07002083 }
2084 });
Adam Cohen19f37922012-03-21 11:59:11 -07002085 mShakeAnimators.put(child, this);
2086 va.start();
2087 }
2088
Adam Cohend024f982012-05-23 18:26:45 -07002089 private void cancel() {
Adam Cohene7587d22012-05-24 18:50:02 -07002090 if (a != null) {
2091 a.cancel();
2092 }
Adam Cohen19f37922012-03-21 11:59:11 -07002093 }
Adam Cohene7587d22012-05-24 18:50:02 -07002094
Adam Cohen091440a2015-03-18 14:16:05 -07002095 @Thunk void completeAnimationImmediately() {
Adam Cohene7587d22012-05-24 18:50:02 -07002096 if (a != null) {
2097 a.cancel();
2098 }
Brandon Keely50e6e562012-05-08 16:28:49 -07002099
Jon Miranda72dbd912017-01-04 14:03:07 -08002100 setInitialAnimationValues(true);
Sunny Goyal9e76f682017-02-13 12:13:43 -08002101 a = LauncherAnimUtils.ofPropertyValuesHolder(child,
2102 new PropertyListBuilder()
2103 .scale(initScale)
2104 .translationX(initDeltaX)
2105 .translationY(initDeltaY)
2106 .build())
2107 .setDuration(REORDER_ANIMATION_DURATION);
Sunny Goyal5d2fc322015-07-06 22:52:49 -07002108 a.setInterpolator(new android.view.animation.DecelerateInterpolator(1.5f));
2109 a.start();
Brandon Keely50e6e562012-05-08 16:28:49 -07002110 }
Adam Cohen19f37922012-03-21 11:59:11 -07002111 }
2112
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002113 private void completeAndClearReorderPreviewAnimations() {
2114 for (ReorderPreviewAnimation a: mShakeAnimators.values()) {
Brandon Keely50e6e562012-05-08 16:28:49 -07002115 a.completeAnimationImmediately();
Adam Cohen19f37922012-03-21 11:59:11 -07002116 }
2117 mShakeAnimators.clear();
2118 }
2119
Adam Cohen482ed822012-03-02 14:15:13 -08002120 private void commitTempPlacement() {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07002121 mTmpOccupied.copyTo(mOccupied);
Sunny Goyalaa8ef112015-06-12 20:04:41 -07002122
2123 long screenId = mLauncher.getWorkspace().getIdForScreen(this);
2124 int container = Favorites.CONTAINER_DESKTOP;
2125
Sunny Goyalc13403c2016-11-18 23:44:48 -08002126 if (mContainerType == HOTSEAT) {
Sunny Goyalaa8ef112015-06-12 20:04:41 -07002127 screenId = -1;
2128 container = Favorites.CONTAINER_HOTSEAT;
2129 }
2130
Michael Jurkaa52570f2012-03-20 03:18:20 -07002131 int childCount = mShortcutsAndWidgets.getChildCount();
Adam Cohen482ed822012-03-02 14:15:13 -08002132 for (int i = 0; i < childCount; i++) {
Adam Cohenea889a22012-03-27 16:45:39 -07002133 View child = mShortcutsAndWidgets.getChildAt(i);
2134 LayoutParams lp = (LayoutParams) child.getLayoutParams();
2135 ItemInfo info = (ItemInfo) child.getTag();
Adam Cohen2acce882012-03-28 19:03:19 -07002136 // We do a null check here because the item info can be null in the case of the
2137 // AllApps button in the hotseat.
2138 if (info != null) {
Sunny Goyalaa8ef112015-06-12 20:04:41 -07002139 final boolean requiresDbUpdate = (info.cellX != lp.tmpCellX
2140 || info.cellY != lp.tmpCellY || info.spanX != lp.cellHSpan
2141 || info.spanY != lp.cellVSpan);
2142
Adam Cohen2acce882012-03-28 19:03:19 -07002143 info.cellX = lp.cellX = lp.tmpCellX;
2144 info.cellY = lp.cellY = lp.tmpCellY;
Adam Cohenbebf0422012-04-11 18:06:28 -07002145 info.spanX = lp.cellHSpan;
2146 info.spanY = lp.cellVSpan;
Sunny Goyalaa8ef112015-06-12 20:04:41 -07002147
2148 if (requiresDbUpdate) {
Sunny Goyal43bf11d2017-02-02 13:52:53 -08002149 mLauncher.getModelWriter().modifyItemInDatabase(info, container, screenId,
Sunny Goyalaa8ef112015-06-12 20:04:41 -07002150 info.cellX, info.cellY, info.spanX, info.spanY);
2151 }
Adam Cohen2acce882012-03-28 19:03:19 -07002152 }
Adam Cohen482ed822012-03-02 14:15:13 -08002153 }
2154 }
2155
Sunny Goyalf7a29e82015-04-24 15:20:43 -07002156 private void setUseTempCoords(boolean useTempCoords) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07002157 int childCount = mShortcutsAndWidgets.getChildCount();
Adam Cohen482ed822012-03-02 14:15:13 -08002158 for (int i = 0; i < childCount; i++) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07002159 LayoutParams lp = (LayoutParams) mShortcutsAndWidgets.getChildAt(i).getLayoutParams();
Adam Cohen482ed822012-03-02 14:15:13 -08002160 lp.useTmpCoords = useTempCoords;
2161 }
2162 }
2163
Sunny Goyalf7a29e82015-04-24 15:20:43 -07002164 private ItemConfiguration findConfigurationNoShuffle(int pixelX, int pixelY, int minSpanX, int minSpanY,
Adam Cohen482ed822012-03-02 14:15:13 -08002165 int spanX, int spanY, View dragView, ItemConfiguration solution) {
2166 int[] result = new int[2];
2167 int[] resultSpan = new int[2];
Sunny Goyalf7a29e82015-04-24 15:20:43 -07002168 findNearestVacantArea(pixelX, pixelY, minSpanX, minSpanY, spanX, spanY, result,
Adam Cohen482ed822012-03-02 14:15:13 -08002169 resultSpan);
2170 if (result[0] >= 0 && result[1] >= 0) {
2171 copyCurrentStateToSolution(solution, false);
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07002172 solution.cellX = result[0];
2173 solution.cellY = result[1];
2174 solution.spanX = resultSpan[0];
2175 solution.spanY = resultSpan[1];
Adam Cohen482ed822012-03-02 14:15:13 -08002176 solution.isSolution = true;
2177 } else {
2178 solution.isSolution = false;
2179 }
2180 return solution;
2181 }
2182
Adam Cohen19f37922012-03-21 11:59:11 -07002183 /* This seems like it should be obvious and straight-forward, but when the direction vector
2184 needs to match with the notion of the dragView pushing other views, we have to employ
2185 a slightly more subtle notion of the direction vector. The question is what two points is
2186 the vector between? The center of the dragView and its desired destination? Not quite, as
2187 this doesn't necessarily coincide with the interaction of the dragView and items occupying
2188 those cells. Instead we use some heuristics to often lock the vector to up, down, left
2189 or right, which helps make pushing feel right.
2190 */
2191 private void getDirectionVectorForDrop(int dragViewCenterX, int dragViewCenterY, int spanX,
2192 int spanY, View dragView, int[] resultDirection) {
2193 int[] targetDestination = new int[2];
2194
2195 findNearestArea(dragViewCenterX, dragViewCenterY, spanX, spanY, targetDestination);
2196 Rect dragRect = new Rect();
2197 regionToRect(targetDestination[0], targetDestination[1], spanX, spanY, dragRect);
2198 dragRect.offset(dragViewCenterX - dragRect.centerX(), dragViewCenterY - dragRect.centerY());
2199
2200 Rect dropRegionRect = new Rect();
2201 getViewsIntersectingRegion(targetDestination[0], targetDestination[1], spanX, spanY,
2202 dragView, dropRegionRect, mIntersectingViews);
2203
2204 int dropRegionSpanX = dropRegionRect.width();
2205 int dropRegionSpanY = dropRegionRect.height();
2206
2207 regionToRect(dropRegionRect.left, dropRegionRect.top, dropRegionRect.width(),
2208 dropRegionRect.height(), dropRegionRect);
2209
2210 int deltaX = (dropRegionRect.centerX() - dragViewCenterX) / spanX;
2211 int deltaY = (dropRegionRect.centerY() - dragViewCenterY) / spanY;
2212
2213 if (dropRegionSpanX == mCountX || spanX == mCountX) {
2214 deltaX = 0;
2215 }
2216 if (dropRegionSpanY == mCountY || spanY == mCountY) {
2217 deltaY = 0;
2218 }
2219
2220 if (deltaX == 0 && deltaY == 0) {
2221 // No idea what to do, give a random direction.
2222 resultDirection[0] = 1;
2223 resultDirection[1] = 0;
2224 } else {
2225 computeDirectionVector(deltaX, deltaY, resultDirection);
2226 }
2227 }
2228
2229 // For a given cell and span, fetch the set of views intersecting the region.
2230 private void getViewsIntersectingRegion(int cellX, int cellY, int spanX, int spanY,
2231 View dragView, Rect boundingRect, ArrayList<View> intersectingViews) {
2232 if (boundingRect != null) {
2233 boundingRect.set(cellX, cellY, cellX + spanX, cellY + spanY);
2234 }
2235 intersectingViews.clear();
2236 Rect r0 = new Rect(cellX, cellY, cellX + spanX, cellY + spanY);
2237 Rect r1 = new Rect();
2238 final int count = mShortcutsAndWidgets.getChildCount();
2239 for (int i = 0; i < count; i++) {
2240 View child = mShortcutsAndWidgets.getChildAt(i);
2241 if (child == dragView) continue;
2242 LayoutParams lp = (LayoutParams) child.getLayoutParams();
2243 r1.set(lp.cellX, lp.cellY, lp.cellX + lp.cellHSpan, lp.cellY + lp.cellVSpan);
2244 if (Rect.intersects(r0, r1)) {
2245 mIntersectingViews.add(child);
2246 if (boundingRect != null) {
2247 boundingRect.union(r1);
2248 }
2249 }
2250 }
2251 }
2252
2253 boolean isNearestDropLocationOccupied(int pixelX, int pixelY, int spanX, int spanY,
2254 View dragView, int[] result) {
2255 result = findNearestArea(pixelX, pixelY, spanX, spanY, result);
2256 getViewsIntersectingRegion(result[0], result[1], spanX, spanY, dragView, null,
2257 mIntersectingViews);
2258 return !mIntersectingViews.isEmpty();
2259 }
2260
2261 void revertTempState() {
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002262 completeAndClearReorderPreviewAnimations();
2263 if (isItemPlacementDirty() && !DESTRUCTIVE_REORDER) {
2264 final int count = mShortcutsAndWidgets.getChildCount();
2265 for (int i = 0; i < count; i++) {
2266 View child = mShortcutsAndWidgets.getChildAt(i);
2267 LayoutParams lp = (LayoutParams) child.getLayoutParams();
2268 if (lp.tmpCellX != lp.cellX || lp.tmpCellY != lp.cellY) {
2269 lp.tmpCellX = lp.cellX;
2270 lp.tmpCellY = lp.cellY;
2271 animateChildToPosition(child, lp.cellX, lp.cellY, REORDER_ANIMATION_DURATION,
2272 0, false, false);
2273 }
Adam Cohen19f37922012-03-21 11:59:11 -07002274 }
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002275 setItemPlacementDirty(false);
Adam Cohen19f37922012-03-21 11:59:11 -07002276 }
Adam Cohen19f37922012-03-21 11:59:11 -07002277 }
2278
Adam Cohenbebf0422012-04-11 18:06:28 -07002279 boolean createAreaForResize(int cellX, int cellY, int spanX, int spanY,
2280 View dragView, int[] direction, boolean commit) {
2281 int[] pixelXY = new int[2];
2282 regionToCenterPoint(cellX, cellY, spanX, spanY, pixelXY);
2283
2284 // First we determine if things have moved enough to cause a different layout
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002285 ItemConfiguration swapSolution = findReorderSolution(pixelXY[0], pixelXY[1], spanX, spanY,
Adam Cohenbebf0422012-04-11 18:06:28 -07002286 spanX, spanY, direction, dragView, true, new ItemConfiguration());
2287
2288 setUseTempCoords(true);
2289 if (swapSolution != null && swapSolution.isSolution) {
2290 // If we're just testing for a possible location (MODE_ACCEPT_DROP), we don't bother
2291 // committing anything or animating anything as we just want to determine if a solution
2292 // exists
2293 copySolutionToTempState(swapSolution, dragView);
2294 setItemPlacementDirty(true);
2295 animateItemsToSolution(swapSolution, dragView, commit);
2296
2297 if (commit) {
2298 commitTempPlacement();
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002299 completeAndClearReorderPreviewAnimations();
Adam Cohenbebf0422012-04-11 18:06:28 -07002300 setItemPlacementDirty(false);
2301 } else {
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002302 beginOrAdjustReorderPreviewAnimations(swapSolution, dragView,
2303 REORDER_ANIMATION_DURATION, ReorderPreviewAnimation.MODE_PREVIEW);
Adam Cohenbebf0422012-04-11 18:06:28 -07002304 }
2305 mShortcutsAndWidgets.requestLayout();
2306 }
2307 return swapSolution.isSolution;
2308 }
2309
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002310 int[] performReorder(int pixelX, int pixelY, int minSpanX, int minSpanY, int spanX, int spanY,
Adam Cohen482ed822012-03-02 14:15:13 -08002311 View dragView, int[] result, int resultSpan[], int mode) {
Adam Cohen482ed822012-03-02 14:15:13 -08002312 // First we determine if things have moved enough to cause a different layout
Adam Cohen47a876d2012-03-19 13:21:41 -07002313 result = findNearestArea(pixelX, pixelY, spanX, spanY, result);
Adam Cohen482ed822012-03-02 14:15:13 -08002314
2315 if (resultSpan == null) {
2316 resultSpan = new int[2];
2317 }
2318
Adam Cohen19f37922012-03-21 11:59:11 -07002319 // When we are checking drop validity or actually dropping, we don't recompute the
2320 // direction vector, since we want the solution to match the preview, and it's possible
2321 // that the exact position of the item has changed to result in a new reordering outcome.
Adam Cohenb209e632012-03-27 17:09:36 -07002322 if ((mode == MODE_ON_DROP || mode == MODE_ON_DROP_EXTERNAL || mode == MODE_ACCEPT_DROP)
2323 && mPreviousReorderDirection[0] != INVALID_DIRECTION) {
Adam Cohen19f37922012-03-21 11:59:11 -07002324 mDirectionVector[0] = mPreviousReorderDirection[0];
2325 mDirectionVector[1] = mPreviousReorderDirection[1];
2326 // We reset this vector after drop
Adam Cohenb209e632012-03-27 17:09:36 -07002327 if (mode == MODE_ON_DROP || mode == MODE_ON_DROP_EXTERNAL) {
2328 mPreviousReorderDirection[0] = INVALID_DIRECTION;
2329 mPreviousReorderDirection[1] = INVALID_DIRECTION;
Adam Cohen19f37922012-03-21 11:59:11 -07002330 }
2331 } else {
2332 getDirectionVectorForDrop(pixelX, pixelY, spanX, spanY, dragView, mDirectionVector);
2333 mPreviousReorderDirection[0] = mDirectionVector[0];
2334 mPreviousReorderDirection[1] = mDirectionVector[1];
2335 }
2336
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002337 // Find a solution involving pushing / displacing any items in the way
2338 ItemConfiguration swapSolution = findReorderSolution(pixelX, pixelY, minSpanX, minSpanY,
Adam Cohen482ed822012-03-02 14:15:13 -08002339 spanX, spanY, mDirectionVector, dragView, true, new ItemConfiguration());
2340
2341 // We attempt the approach which doesn't shuffle views at all
2342 ItemConfiguration noShuffleSolution = findConfigurationNoShuffle(pixelX, pixelY, minSpanX,
2343 minSpanY, spanX, spanY, dragView, new ItemConfiguration());
2344
2345 ItemConfiguration finalSolution = null;
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002346
2347 // If the reorder solution requires resizing (shrinking) the item being dropped, we instead
2348 // favor a solution in which the item is not resized, but
Adam Cohen482ed822012-03-02 14:15:13 -08002349 if (swapSolution.isSolution && swapSolution.area() >= noShuffleSolution.area()) {
2350 finalSolution = swapSolution;
2351 } else if (noShuffleSolution.isSolution) {
2352 finalSolution = noShuffleSolution;
2353 }
2354
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002355 if (mode == MODE_SHOW_REORDER_HINT) {
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002356 if (finalSolution != null) {
Adam Cohenfe692872013-12-11 14:47:23 -08002357 beginOrAdjustReorderPreviewAnimations(finalSolution, dragView, 0,
2358 ReorderPreviewAnimation.MODE_HINT);
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07002359 result[0] = finalSolution.cellX;
2360 result[1] = finalSolution.cellY;
2361 resultSpan[0] = finalSolution.spanX;
2362 resultSpan[1] = finalSolution.spanY;
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002363 } else {
2364 result[0] = result[1] = resultSpan[0] = resultSpan[1] = -1;
2365 }
2366 return result;
2367 }
2368
Adam Cohen482ed822012-03-02 14:15:13 -08002369 boolean foundSolution = true;
2370 if (!DESTRUCTIVE_REORDER) {
2371 setUseTempCoords(true);
2372 }
2373
2374 if (finalSolution != null) {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07002375 result[0] = finalSolution.cellX;
2376 result[1] = finalSolution.cellY;
2377 resultSpan[0] = finalSolution.spanX;
2378 resultSpan[1] = finalSolution.spanY;
Adam Cohen482ed822012-03-02 14:15:13 -08002379
2380 // If we're just testing for a possible location (MODE_ACCEPT_DROP), we don't bother
2381 // committing anything or animating anything as we just want to determine if a solution
2382 // exists
2383 if (mode == MODE_DRAG_OVER || mode == MODE_ON_DROP || mode == MODE_ON_DROP_EXTERNAL) {
2384 if (!DESTRUCTIVE_REORDER) {
2385 copySolutionToTempState(finalSolution, dragView);
2386 }
2387 setItemPlacementDirty(true);
2388 animateItemsToSolution(finalSolution, dragView, mode == MODE_ON_DROP);
2389
Adam Cohen19f37922012-03-21 11:59:11 -07002390 if (!DESTRUCTIVE_REORDER &&
2391 (mode == MODE_ON_DROP || mode == MODE_ON_DROP_EXTERNAL)) {
Adam Cohen482ed822012-03-02 14:15:13 -08002392 commitTempPlacement();
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002393 completeAndClearReorderPreviewAnimations();
Adam Cohen19f37922012-03-21 11:59:11 -07002394 setItemPlacementDirty(false);
2395 } else {
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002396 beginOrAdjustReorderPreviewAnimations(finalSolution, dragView,
2397 REORDER_ANIMATION_DURATION, ReorderPreviewAnimation.MODE_PREVIEW);
Adam Cohen482ed822012-03-02 14:15:13 -08002398 }
2399 }
2400 } else {
2401 foundSolution = false;
2402 result[0] = result[1] = resultSpan[0] = resultSpan[1] = -1;
2403 }
2404
2405 if ((mode == MODE_ON_DROP || !foundSolution) && !DESTRUCTIVE_REORDER) {
2406 setUseTempCoords(false);
2407 }
Adam Cohen482ed822012-03-02 14:15:13 -08002408
Michael Jurkaa52570f2012-03-20 03:18:20 -07002409 mShortcutsAndWidgets.requestLayout();
Adam Cohen482ed822012-03-02 14:15:13 -08002410 return result;
2411 }
2412
Adam Cohen19f37922012-03-21 11:59:11 -07002413 void setItemPlacementDirty(boolean dirty) {
2414 mItemPlacementDirty = dirty;
Adam Cohen482ed822012-03-02 14:15:13 -08002415 }
Adam Cohen19f37922012-03-21 11:59:11 -07002416 boolean isItemPlacementDirty() {
2417 return mItemPlacementDirty;
Adam Cohen482ed822012-03-02 14:15:13 -08002418 }
2419
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07002420 private static class ItemConfiguration extends CellAndSpan {
Adam Cohen8baab352012-03-20 17:39:21 -07002421 HashMap<View, CellAndSpan> map = new HashMap<View, CellAndSpan>();
Adam Cohenf3900c22012-11-16 18:28:11 -08002422 private HashMap<View, CellAndSpan> savedMap = new HashMap<View, CellAndSpan>();
2423 ArrayList<View> sortedViews = new ArrayList<View>();
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002424 ArrayList<View> intersectingViews;
Adam Cohen482ed822012-03-02 14:15:13 -08002425 boolean isSolution = false;
Adam Cohen482ed822012-03-02 14:15:13 -08002426
Adam Cohenf3900c22012-11-16 18:28:11 -08002427 void save() {
2428 // Copy current state into savedMap
2429 for (View v: map.keySet()) {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07002430 savedMap.get(v).copyFrom(map.get(v));
Adam Cohenf3900c22012-11-16 18:28:11 -08002431 }
2432 }
2433
2434 void restore() {
2435 // Restore current state from savedMap
2436 for (View v: savedMap.keySet()) {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07002437 map.get(v).copyFrom(savedMap.get(v));
Adam Cohenf3900c22012-11-16 18:28:11 -08002438 }
2439 }
2440
2441 void add(View v, CellAndSpan cs) {
2442 map.put(v, cs);
2443 savedMap.put(v, new CellAndSpan());
2444 sortedViews.add(v);
2445 }
2446
Adam Cohen482ed822012-03-02 14:15:13 -08002447 int area() {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07002448 return spanX * spanY;
Adam Cohenf3900c22012-11-16 18:28:11 -08002449 }
2450
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07002451 void getBoundingRectForViews(ArrayList<View> views, Rect outRect) {
2452 boolean first = true;
2453 for (View v: views) {
2454 CellAndSpan c = map.get(v);
2455 if (first) {
2456 outRect.set(c.cellX, c.cellY, c.cellX + c.spanX, c.cellY + c.spanY);
2457 first = false;
2458 } else {
2459 outRect.union(c.cellX, c.cellY, c.cellX + c.spanX, c.cellY + c.spanY);
2460 }
2461 }
Adam Cohenf3900c22012-11-16 18:28:11 -08002462 }
Adam Cohen482ed822012-03-02 14:15:13 -08002463 }
2464
Adam Cohendf035382011-04-11 17:22:04 -07002465 /**
Adam Cohendf035382011-04-11 17:22:04 -07002466 * Find a starting cell position that will fit the given bounds nearest the requested
2467 * cell location. Uses Euclidean distance to score multiple vacant areas.
2468 *
2469 * @param pixelX The X location at which you want to search for a vacant area.
2470 * @param pixelY The Y location at which you want to search for a vacant area.
2471 * @param spanX Horizontal span of the object.
2472 * @param spanY Vertical span of the object.
2473 * @param ignoreView Considers space occupied by this view as unoccupied
2474 * @param result Previously returned value to possibly recycle.
2475 * @return The X, Y cell of a vacant area that can contain this object,
2476 * nearest the requested location.
2477 */
Adam Cohenf9c184a2016-01-15 16:47:43 -08002478 public int[] findNearestArea(int pixelX, int pixelY, int spanX, int spanY, int[] result) {
Sunny Goyalf7a29e82015-04-24 15:20:43 -07002479 return findNearestArea(pixelX, pixelY, spanX, spanY, spanX, spanY, false, result, null);
Adam Cohendf035382011-04-11 17:22:04 -07002480 }
2481
Michael Jurka0280c3b2010-09-17 15:00:07 -07002482 boolean existsEmptyCell() {
2483 return findCellForSpan(null, 1, 1);
2484 }
2485
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002486 /**
Michael Jurka0280c3b2010-09-17 15:00:07 -07002487 * Finds the upper-left coordinate of the first rectangle in the grid that can
2488 * hold a cell of the specified dimensions. If intersectX and intersectY are not -1,
2489 * then this method will only return coordinates for rectangles that contain the cell
2490 * (intersectX, intersectY)
2491 *
2492 * @param cellXY The array that will contain the position of a vacant cell if such a cell
2493 * can be found.
2494 * @param spanX The horizontal span of the cell we want to find.
2495 * @param spanY The vertical span of the cell we want to find.
2496 *
2497 * @return True if a vacant cell of the specified dimension was found, false otherwise.
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07002498 */
Hyunyoung Song3f471442015-04-08 19:01:34 -07002499 public boolean findCellForSpan(int[] cellXY, int spanX, int spanY) {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07002500 if (cellXY == null) {
2501 cellXY = new int[2];
Michael Jurka0280c3b2010-09-17 15:00:07 -07002502 }
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07002503 return mOccupied.findVacantCell(cellXY, spanX, spanY);
Michael Jurka0280c3b2010-09-17 15:00:07 -07002504 }
2505
2506 /**
Winson Chungc07918d2011-07-01 15:35:26 -07002507 * A drag event has begun over this layout.
2508 * It may have begun over this layout (in which case onDragChild is called first),
2509 * or it may have begun on another layout.
2510 */
2511 void onDragEnter() {
Winson Chungc07918d2011-07-01 15:35:26 -07002512 mDragging = true;
2513 }
2514
2515 /**
Michael Jurka0280c3b2010-09-17 15:00:07 -07002516 * Called when drag has left this CellLayout or has been completed (successfully or not)
2517 */
2518 void onDragExit() {
Joe Onorato4be866d2010-10-10 11:26:02 -07002519 // This can actually be called when we aren't in a drag, e.g. when adding a new
2520 // item to this layout via the customize drawer.
2521 // Guard against that case.
2522 if (mDragging) {
2523 mDragging = false;
Patrick Dubroyde7658b2010-09-27 11:15:43 -07002524 }
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -07002525
2526 // Invalidate the drag data
Adam Cohend41fbf52012-02-16 23:53:59 -08002527 mDragCell[0] = mDragCell[1] = -1;
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -07002528 mDragOutlineAnims[mDragOutlineCurrent].animateOut();
2529 mDragOutlineCurrent = (mDragOutlineCurrent + 1) % mDragOutlineAnims.length;
Adam Cohen19f37922012-03-21 11:59:11 -07002530 revertTempState();
Michael Jurka33945b22010-12-21 18:19:38 -08002531 setIsDragOverlapping(false);
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07002532 }
2533
2534 /**
Winson Chungaafa03c2010-06-11 17:34:16 -07002535 * Mark a child as having been dropped.
Patrick Dubroyde7658b2010-09-27 11:15:43 -07002536 * At the beginning of the drag operation, the child may have been on another
Patrick Dubroyce34a972010-10-19 10:34:32 -07002537 * screen, but it is re-parented before this method is called.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002538 *
2539 * @param child The child that is being dropped
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002540 */
Adam Cohen716b51e2011-06-30 12:09:54 -07002541 void onDropChild(View child) {
Romain Guyd94533d2009-08-17 10:01:15 -07002542 if (child != null) {
2543 LayoutParams lp = (LayoutParams) child.getLayoutParams();
Romain Guy84f296c2009-11-04 15:00:44 -08002544 lp.dropped = true;
Romain Guyd94533d2009-08-17 10:01:15 -07002545 child.requestLayout();
Tony Wickham1cdb6d02015-09-17 11:08:27 -07002546 markCellsAsOccupiedForView(child);
Romain Guyd94533d2009-08-17 10:01:15 -07002547 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002548 }
2549
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002550 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002551 * Computes a bounding rectangle for a range of cells
Winson Chungaafa03c2010-06-11 17:34:16 -07002552 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002553 * @param cellX X coordinate of upper left corner expressed as a cell position
2554 * @param cellY Y coordinate of upper left corner expressed as a cell position
Winson Chungaafa03c2010-06-11 17:34:16 -07002555 * @param cellHSpan Width in cells
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002556 * @param cellVSpan Height in cells
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07002557 * @param resultRect Rect into which to put the results
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002558 */
Adam Cohend41fbf52012-02-16 23:53:59 -08002559 public void cellToRect(int cellX, int cellY, int cellHSpan, int cellVSpan, Rect resultRect) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002560 final int cellWidth = mCellWidth;
2561 final int cellHeight = mCellHeight;
Winson Chungaafa03c2010-06-11 17:34:16 -07002562
Winson Chung4b825dcd2011-06-19 12:41:22 -07002563 final int hStartPadding = getPaddingLeft();
2564 final int vStartPadding = getPaddingTop();
Winson Chungaafa03c2010-06-11 17:34:16 -07002565
Sunny Goyalaa8a8712016-11-20 15:26:01 +05302566 int width = cellHSpan * cellWidth;
2567 int height = cellVSpan * cellHeight;
2568 int x = hStartPadding + cellX * cellWidth;
2569 int y = vStartPadding + cellY * cellHeight;
Winson Chungaafa03c2010-06-11 17:34:16 -07002570
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07002571 resultRect.set(x, y, x + width, y + height);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002572 }
Winson Chungaafa03c2010-06-11 17:34:16 -07002573
Adam Cohend4844c32011-02-18 19:25:06 -08002574 public void markCellsAsOccupiedForView(View view) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07002575 if (view == null || view.getParent() != mShortcutsAndWidgets) return;
Michael Jurka0280c3b2010-09-17 15:00:07 -07002576 LayoutParams lp = (LayoutParams) view.getLayoutParams();
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07002577 mOccupied.markCells(lp.cellX, lp.cellY, lp.cellHSpan, lp.cellVSpan, true);
Michael Jurka0280c3b2010-09-17 15:00:07 -07002578 }
2579
Adam Cohend4844c32011-02-18 19:25:06 -08002580 public void markCellsAsUnoccupiedForView(View view) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07002581 if (view == null || view.getParent() != mShortcutsAndWidgets) return;
Michael Jurka0280c3b2010-09-17 15:00:07 -07002582 LayoutParams lp = (LayoutParams) view.getLayoutParams();
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07002583 mOccupied.markCells(lp.cellX, lp.cellY, lp.cellHSpan, lp.cellVSpan, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002584 }
2585
Adam Cohen2801caf2011-05-13 20:57:39 -07002586 public int getDesiredWidth() {
Sunny Goyalaa8a8712016-11-20 15:26:01 +05302587 return getPaddingLeft() + getPaddingRight() + (mCountX * mCellWidth);
Adam Cohen2801caf2011-05-13 20:57:39 -07002588 }
2589
2590 public int getDesiredHeight() {
Sunny Goyalaa8a8712016-11-20 15:26:01 +05302591 return getPaddingTop() + getPaddingBottom() + (mCountY * mCellHeight);
Adam Cohen2801caf2011-05-13 20:57:39 -07002592 }
2593
Michael Jurka66d72172011-04-12 16:29:25 -07002594 public boolean isOccupied(int x, int y) {
2595 if (x < mCountX && y < mCountY) {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07002596 return mOccupied.cells[x][y];
Michael Jurka66d72172011-04-12 16:29:25 -07002597 } else {
2598 throw new RuntimeException("Position exceeds the bound of this CellLayout");
2599 }
2600 }
2601
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002602 @Override
2603 public ViewGroup.LayoutParams generateLayoutParams(AttributeSet attrs) {
2604 return new CellLayout.LayoutParams(getContext(), attrs);
2605 }
2606
2607 @Override
2608 protected boolean checkLayoutParams(ViewGroup.LayoutParams p) {
2609 return p instanceof CellLayout.LayoutParams;
2610 }
2611
2612 @Override
2613 protected ViewGroup.LayoutParams generateLayoutParams(ViewGroup.LayoutParams p) {
2614 return new CellLayout.LayoutParams(p);
2615 }
2616
2617 public static class LayoutParams extends ViewGroup.MarginLayoutParams {
2618 /**
2619 * Horizontal location of the item in the grid.
2620 */
2621 @ViewDebug.ExportedProperty
2622 public int cellX;
2623
2624 /**
2625 * Vertical location of the item in the grid.
2626 */
2627 @ViewDebug.ExportedProperty
2628 public int cellY;
2629
2630 /**
Adam Cohen482ed822012-03-02 14:15:13 -08002631 * Temporary horizontal location of the item in the grid during reorder
2632 */
2633 public int tmpCellX;
2634
2635 /**
2636 * Temporary vertical location of the item in the grid during reorder
2637 */
2638 public int tmpCellY;
2639
2640 /**
2641 * Indicates that the temporary coordinates should be used to layout the items
2642 */
2643 public boolean useTmpCoords;
2644
2645 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002646 * Number of cells spanned horizontally by the item.
2647 */
2648 @ViewDebug.ExportedProperty
2649 public int cellHSpan;
2650
2651 /**
2652 * Number of cells spanned vertically by the item.
2653 */
2654 @ViewDebug.ExportedProperty
2655 public int cellVSpan;
Winson Chungaafa03c2010-06-11 17:34:16 -07002656
Adam Cohen1b607ed2011-03-03 17:26:50 -08002657 /**
2658 * Indicates whether the item will set its x, y, width and height parameters freely,
2659 * or whether these will be computed based on cellX, cellY, cellHSpan and cellVSpan.
2660 */
Adam Cohend4844c32011-02-18 19:25:06 -08002661 public boolean isLockedToGrid = true;
2662
Adam Cohen482ed822012-03-02 14:15:13 -08002663 /**
Adam Cohenec40b2b2013-07-23 15:52:40 -07002664 * Indicates that this item should use the full extents of its parent.
2665 */
2666 public boolean isFullscreen = false;
2667
2668 /**
Adam Cohen482ed822012-03-02 14:15:13 -08002669 * Indicates whether this item can be reordered. Always true except in the case of the
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07002670 * the AllApps button and QSB place holder.
Adam Cohen482ed822012-03-02 14:15:13 -08002671 */
2672 public boolean canReorder = true;
2673
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002674 // X coordinate of the view in the layout.
2675 @ViewDebug.ExportedProperty
Vadim Tryshevfedca432015-08-19 17:55:02 -07002676 public int x;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002677 // Y coordinate of the view in the layout.
2678 @ViewDebug.ExportedProperty
Vadim Tryshevfedca432015-08-19 17:55:02 -07002679 public int y;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002680
Romain Guy84f296c2009-11-04 15:00:44 -08002681 boolean dropped;
Romain Guyfcb9e712009-10-02 16:06:52 -07002682
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002683 public LayoutParams(Context c, AttributeSet attrs) {
2684 super(c, attrs);
2685 cellHSpan = 1;
2686 cellVSpan = 1;
2687 }
2688
2689 public LayoutParams(ViewGroup.LayoutParams source) {
2690 super(source);
2691 cellHSpan = 1;
2692 cellVSpan = 1;
2693 }
Winson Chungaafa03c2010-06-11 17:34:16 -07002694
2695 public LayoutParams(LayoutParams source) {
2696 super(source);
2697 this.cellX = source.cellX;
2698 this.cellY = source.cellY;
2699 this.cellHSpan = source.cellHSpan;
2700 this.cellVSpan = source.cellVSpan;
2701 }
2702
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002703 public LayoutParams(int cellX, int cellY, int cellHSpan, int cellVSpan) {
Romain Guy8f19cdd2010-01-08 15:07:00 -08002704 super(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002705 this.cellX = cellX;
2706 this.cellY = cellY;
2707 this.cellHSpan = cellHSpan;
2708 this.cellVSpan = cellVSpan;
2709 }
2710
Sunny Goyalaa8a8712016-11-20 15:26:01 +05302711 public void setup(int cellWidth, int cellHeight, boolean invertHorizontally, int colCount) {
Jon Miranda7ae64ff2016-11-21 16:18:46 -08002712 setup(cellWidth, cellHeight, invertHorizontally, colCount, 1.0f, 1.0f);
2713 }
2714
2715 /**
2716 * Use this method, as opposed to {@link #setup(int, int, boolean, int)}, if the view needs
2717 * to be scaled.
2718 *
2719 * ie. In multi-window mode, we setup widgets so that they are measured and laid out
2720 * using their full/invariant device profile sizes.
2721 */
2722 public void setup(int cellWidth, int cellHeight, boolean invertHorizontally, int colCount,
2723 float cellScaleX, float cellScaleY) {
Adam Cohend4844c32011-02-18 19:25:06 -08002724 if (isLockedToGrid) {
2725 final int myCellHSpan = cellHSpan;
2726 final int myCellVSpan = cellVSpan;
Adam Cohen2374abf2013-04-16 14:56:57 -07002727 int myCellX = useTmpCoords ? tmpCellX : cellX;
2728 int myCellY = useTmpCoords ? tmpCellY : cellY;
2729
2730 if (invertHorizontally) {
2731 myCellX = colCount - myCellX - cellHSpan;
2732 }
Adam Cohen1b607ed2011-03-03 17:26:50 -08002733
Jon Miranda7ae64ff2016-11-21 16:18:46 -08002734 width = (int) (myCellHSpan * cellWidth / cellScaleX - leftMargin - rightMargin);
2735 height = (int) (myCellVSpan * cellHeight / cellScaleY - topMargin - bottomMargin);
Sunny Goyalaa8a8712016-11-20 15:26:01 +05302736 x = (myCellX * cellWidth + leftMargin);
2737 y = (myCellY * cellHeight + topMargin);
Adam Cohend4844c32011-02-18 19:25:06 -08002738 }
2739 }
Winson Chungaafa03c2010-06-11 17:34:16 -07002740
Winson Chungaafa03c2010-06-11 17:34:16 -07002741 public String toString() {
2742 return "(" + this.cellX + ", " + this.cellY + ")";
2743 }
Adam Cohen7f4eabe2011-04-21 16:19:16 -07002744
2745 public void setWidth(int width) {
2746 this.width = width;
2747 }
2748
2749 public int getWidth() {
2750 return width;
2751 }
2752
2753 public void setHeight(int height) {
2754 this.height = height;
2755 }
2756
2757 public int getHeight() {
2758 return height;
2759 }
2760
2761 public void setX(int x) {
2762 this.x = x;
2763 }
2764
2765 public int getX() {
2766 return x;
2767 }
2768
2769 public void setY(int y) {
2770 this.y = y;
2771 }
2772
2773 public int getY() {
2774 return y;
2775 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002776 }
2777
Michael Jurka0280c3b2010-09-17 15:00:07 -07002778 // This class stores info for two purposes:
2779 // 1. When dragging items (mDragInfo in Workspace), we store the View, its cellX & cellY,
2780 // its spanX, spanY, and the screen it is on
2781 // 2. When long clicking on an empty cell in a CellLayout, we save information about the
2782 // cellX and cellY coordinates and which page was clicked. We then set this as a tag on
2783 // the CellLayout that was long clicked
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07002784 public static final class CellInfo extends CellAndSpan {
Adam Cohenf9c184a2016-01-15 16:47:43 -08002785 public View cell;
Adam Cohendcd297f2013-06-18 13:13:40 -07002786 long screenId;
Winson Chung3d503fb2011-07-13 17:25:49 -07002787 long container;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002788
Sunny Goyal83a8f042015-05-19 12:52:12 -07002789 public CellInfo(View v, ItemInfo info) {
Adam Cohene0aaa0d2014-05-12 12:44:22 -07002790 cellX = info.cellX;
2791 cellY = info.cellY;
2792 spanX = info.spanX;
2793 spanY = info.spanY;
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07002794 cell = v;
Adam Cohene0aaa0d2014-05-12 12:44:22 -07002795 screenId = info.screenId;
2796 container = info.container;
2797 }
2798
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002799 @Override
2800 public String toString() {
Winson Chungaafa03c2010-06-11 17:34:16 -07002801 return "Cell[view=" + (cell == null ? "null" : cell.getClass())
2802 + ", x=" + cellX + ", y=" + cellY + "]";
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002803 }
2804 }
Michael Jurkad771c962011-08-09 15:00:48 -07002805
Tony Wickham86930612015-09-09 13:50:40 -07002806 /**
2807 * Returns whether an item can be placed in this CellLayout (after rearranging and/or resizing
2808 * if necessary).
2809 */
2810 public boolean hasReorderSolution(ItemInfo itemInfo) {
2811 int[] cellPoint = new int[2];
2812 // Check for a solution starting at every cell.
2813 for (int cellX = 0; cellX < getCountX(); cellX++) {
2814 for (int cellY = 0; cellY < getCountY(); cellY++) {
2815 cellToPoint(cellX, cellY, cellPoint);
2816 if (findReorderSolution(cellPoint[0], cellPoint[1], itemInfo.minSpanX,
2817 itemInfo.minSpanY, itemInfo.spanX, itemInfo.spanY, mDirectionVector, null,
2818 true, new ItemConfiguration()).isSolution) {
2819 return true;
2820 }
2821 }
2822 }
2823 return false;
2824 }
2825
Sunny Goyal9ca9c132015-04-29 14:57:22 -07002826 public boolean isRegionVacant(int x, int y, int spanX, int spanY) {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07002827 return mOccupied.isRegionVacant(x, y, spanX, spanY);
Sunny Goyal9ca9c132015-04-29 14:57:22 -07002828 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002829}