blob: 6755ff70a1aa5507cac6a494ef2c4ea8065e746b [file] [log] [blame]
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001/*
2 * Copyright (C) 2008 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Daniel Sandler325dc232013-06-05 22:57:57 -040017package com.android.launcher3;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080018
Joe Onorato4be866d2010-10-10 11:26:02 -070019import android.animation.Animator;
Michael Jurka629758f2012-06-14 16:18:21 -070020import android.animation.AnimatorListenerAdapter;
Chet Haase00397b12010-10-07 11:13:10 -070021import android.animation.TimeInterpolator;
Patrick Dubroyde7658b2010-09-27 11:15:43 -070022import android.animation.ValueAnimator;
23import android.animation.ValueAnimator.AnimatorUpdateListener;
Adam Cohenc9735cf2015-01-23 16:11:55 -080024import android.annotation.TargetApi;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080025import android.content.Context;
Joe Onorato79e56262009-09-21 15:23:04 -040026import android.content.res.Resources;
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;
Sunny Goyal2805e632015-05-20 15:35:32 -070035import android.graphics.drawable.TransitionDrawable;
Adam Cohenc9735cf2015-01-23 16:11:55 -080036import android.os.Build;
Adam Cohen1462de32012-07-24 22:34:36 -070037import android.os.Parcelable;
Adam Cohenc9735cf2015-01-23 16:11:55 -080038import android.support.v4.view.ViewCompat;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080039import android.util.AttributeSet;
Joe Onorato4be866d2010-10-10 11:26:02 -070040import android.util.Log;
Adam Cohen1462de32012-07-24 22:34:36 -070041import android.util.SparseArray;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080042import android.view.MotionEvent;
43import android.view.View;
44import android.view.ViewDebug;
45import android.view.ViewGroup;
Adam Cohenc9735cf2015-01-23 16:11:55 -080046import android.view.accessibility.AccessibilityEvent;
Winson Chung150fbab2010-09-29 17:14:26 -070047import android.view.animation.DecelerateInterpolator;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080048
Sunny Goyal4b6eb262015-05-14 19:24:40 -070049import com.android.launcher3.BubbleTextView.BubbleTextShadowHandler;
Sunny Goyalaa8ef112015-06-12 20:04:41 -070050import com.android.launcher3.LauncherSettings.Favorites;
Sunny Goyale9b651e2015-04-24 11:44:51 -070051import com.android.launcher3.accessibility.DragAndDropAccessibilityDelegate;
52import com.android.launcher3.accessibility.FolderAccessibilityHelper;
53import com.android.launcher3.accessibility.WorkspaceAccessibilityHelper;
Tony Wickhame0c33232016-02-08 11:37:04 -080054import com.android.launcher3.config.FeatureFlags;
Sunny Goyal6c56c682015-07-16 14:09:05 -070055import com.android.launcher3.config.ProviderConfig;
Sunny Goyal26119432016-02-18 22:09:23 +000056import com.android.launcher3.folder.FolderIcon;
Sunny 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
Adam Cohen69ce2e52011-07-03 19:25:21 -070062import java.util.ArrayList;
Adam Cohenc0dcf592011-06-01 15:30:43 -070063import java.util.Arrays;
Adam Cohenf3900c22012-11-16 18:28:11 -080064import java.util.Collections;
65import java.util.Comparator;
Adam Cohenbfbfd262011-06-13 16:55:12 -070066import java.util.HashMap;
Adam Cohend41fbf52012-02-16 23:53:59 -080067import java.util.Stack;
Adam Cohenc0dcf592011-06-01 15:30:43 -070068
Sunny Goyal4b6eb262015-05-14 19:24:40 -070069public class CellLayout extends ViewGroup implements BubbleTextShadowHandler {
Sunny Goyale9b651e2015-04-24 11:44:51 -070070 public static final int WORKSPACE_ACCESSIBILITY_DRAG = 2;
71 public static final int FOLDER_ACCESSIBILITY_DRAG = 1;
72
Tony Wickhama0628cc2015-10-14 15:23:04 -070073 private static final String TAG = "CellLayout";
74 private static final boolean LOGD = false;
Winson Chungaafa03c2010-06-11 17:34:16 -070075
Adam Cohen2acce882012-03-28 19:03:19 -070076 private Launcher mLauncher;
Sunny Goyal4ffec482016-02-09 11:28:52 -080077 @ViewDebug.ExportedProperty(category = "launcher")
Adam Cohen091440a2015-03-18 14:16:05 -070078 @Thunk int mCellWidth;
Sunny Goyal4ffec482016-02-09 11:28:52 -080079 @ViewDebug.ExportedProperty(category = "launcher")
Adam Cohen091440a2015-03-18 14:16:05 -070080 @Thunk int mCellHeight;
Winson Chung11a1a532013-09-13 11:14:45 -070081 private int mFixedCellWidth;
82 private int mFixedCellHeight;
Winson Chungaafa03c2010-06-11 17:34:16 -070083
Sunny Goyal4ffec482016-02-09 11:28:52 -080084 @ViewDebug.ExportedProperty(category = "launcher")
Sunny Goyalff4ba2d2016-04-02 14:12:34 -070085 private int mCountX;
Sunny Goyal4ffec482016-02-09 11:28:52 -080086 @ViewDebug.ExportedProperty(category = "launcher")
Sunny Goyalff4ba2d2016-04-02 14:12:34 -070087 private int mCountY;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080088
Adam Cohen234c4cd2011-07-17 21:03:04 -070089 private int mOriginalWidthGap;
90 private int mOriginalHeightGap;
Sunny Goyal4ffec482016-02-09 11:28:52 -080091 @ViewDebug.ExportedProperty(category = "launcher")
Adam Cohen091440a2015-03-18 14:16:05 -070092 @Thunk int mWidthGap;
Sunny Goyal4ffec482016-02-09 11:28:52 -080093 @ViewDebug.ExportedProperty(category = "launcher")
Adam Cohen091440a2015-03-18 14:16:05 -070094 @Thunk int mHeightGap;
Winson Chung4b825dcd2011-06-19 12:41:22 -070095 private int mMaxGap;
Adam Cohen917e3882013-10-31 15:03:35 -070096 private boolean mDropPending = false;
Adam Cohenc50438c2014-08-19 17:43:05 -070097 private boolean mIsDragTarget = true;
Sunny Goyale2fd14b2015-08-27 17:45:46 -070098 private boolean mJailContent = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080099
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700100 // These are temporary variables to prevent having to allocate a new object just to
101 // return an (x, y) value from helper functions. Do NOT use them to maintain other state.
Adam Cohen091440a2015-03-18 14:16:05 -0700102 @Thunk final int[] mTmpPoint = new int[2];
Sunny Goyal2805e632015-05-20 15:35:32 -0700103 @Thunk final int[] mTempLocation = new int[2];
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700104
Sunny Goyalff4ba2d2016-04-02 14:12:34 -0700105 private GridOccupancy mOccupied;
106 private GridOccupancy mTmpOccupied;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800107
Michael Jurkadee05892010-07-27 10:01:56 -0700108 private OnTouchListener mInterceptTouchListener;
Mady Melloref044dd2015-06-02 15:35:07 -0700109 private StylusEventHelper mStylusEventHelper;
Michael Jurkadee05892010-07-27 10:01:56 -0700110
Adam Cohenefca0272016-02-24 19:19:06 -0800111 private ArrayList<FolderIcon.PreviewBackground> mFolderBackgrounds = new ArrayList<FolderIcon.PreviewBackground>();
112 FolderIcon.PreviewBackground mFolderLeaveBehind = new FolderIcon.PreviewBackground();
113 Paint mFolderBgPaint = new Paint();
Adam Cohen69ce2e52011-07-03 19:25:21 -0700114
Michael Jurka5f1c5092010-09-03 14:15:02 -0700115 private float mBackgroundAlpha;
Adam Cohenf34bab52010-09-30 14:11:56 -0700116
Tony Wickham33326072016-04-04 15:05:49 -0700117 private static final int BACKGROUND_ACTIVATE_DURATION =
118 FeatureFlags.LAUNCHER3_LEGACY_WORKSPACE_DND ? 120 : 0;
Sunny Goyal2805e632015-05-20 15:35:32 -0700119 private final TransitionDrawable mBackground;
120
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700121 // These values allow a fixed measurement to be set on the CellLayout.
122 private int mFixedWidth = -1;
123 private int mFixedHeight = -1;
124
Michael Jurka33945b22010-12-21 18:19:38 -0800125 // If we're actively dragging something over this screen, mIsDragOverlapping is true
126 private boolean mIsDragOverlapping = false;
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700127
Winson Chung150fbab2010-09-29 17:14:26 -0700128 // These arrays are used to implement the drag visualization on x-large screens.
Joe Onorato4be866d2010-10-10 11:26:02 -0700129 // They are used as circular arrays, indexed by mDragOutlineCurrent.
Adam Cohen091440a2015-03-18 14:16:05 -0700130 @Thunk Rect[] mDragOutlines = new Rect[4];
131 @Thunk float[] mDragOutlineAlphas = new float[mDragOutlines.length];
Joe Onorato4be866d2010-10-10 11:26:02 -0700132 private InterruptibleInOutAnimator[] mDragOutlineAnims =
133 new InterruptibleInOutAnimator[mDragOutlines.length];
Winson Chung150fbab2010-09-29 17:14:26 -0700134
135 // Used as an index into the above 3 arrays; indicates which is the most current value.
Joe Onorato4be866d2010-10-10 11:26:02 -0700136 private int mDragOutlineCurrent = 0;
Patrick Dubroy8e58e912010-10-14 13:21:48 -0700137 private final Paint mDragOutlinePaint = new Paint();
Winson Chung150fbab2010-09-29 17:14:26 -0700138
Sunny Goyal4fe5a372015-05-14 19:55:10 -0700139 private final ClickShadowView mTouchFeedbackView;
Patrick Dubroy96864c32011-03-10 17:17:23 -0800140
Sunny Goyal316490e2015-06-02 09:38:28 -0700141 @Thunk HashMap<CellLayout.LayoutParams, Animator> mReorderAnimators = new HashMap<>();
142 @Thunk HashMap<View, ReorderPreviewAnimation> mShakeAnimators = new HashMap<>();
Adam Cohen19f37922012-03-21 11:59:11 -0700143
144 private boolean mItemPlacementDirty = false;
Adam Cohenbfbfd262011-06-13 16:55:12 -0700145
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700146 // When a drag operation is in progress, holds the nearest cell to the touch point
147 private final int[] mDragCell = new int[2];
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800148
Joe Onorato4be866d2010-10-10 11:26:02 -0700149 private boolean mDragging = false;
150
Patrick Dubroyce34a972010-10-19 10:34:32 -0700151 private TimeInterpolator mEaseOutInterpolator;
Michael Jurkaa52570f2012-03-20 03:18:20 -0700152 private ShortcutAndWidgetContainer mShortcutsAndWidgets;
Patrick Dubroyce34a972010-10-19 10:34:32 -0700153
Andrew Flynn0dca1ec2012-02-29 13:33:22 -0800154 private boolean mIsHotseat = false;
Adam Cohen307fe232012-08-16 17:55:58 -0700155 private float mHotseatScale = 1f;
Andrew Flynn0dca1ec2012-02-29 13:33:22 -0800156
Adam Cohenfa3c58f2013-12-06 16:10:55 -0800157 public static final int MODE_SHOW_REORDER_HINT = 0;
158 public static final int MODE_DRAG_OVER = 1;
159 public static final int MODE_ON_DROP = 2;
160 public static final int MODE_ON_DROP_EXTERNAL = 3;
161 public static final int MODE_ACCEPT_DROP = 4;
Adam Cohen19f37922012-03-21 11:59:11 -0700162 private static final boolean DESTRUCTIVE_REORDER = false;
Adam Cohen482ed822012-03-02 14:15:13 -0800163 private static final boolean DEBUG_VISUALIZE_OCCUPIED = false;
164
Adam Cohenfa3c58f2013-12-06 16:10:55 -0800165 private static final float REORDER_PREVIEW_MAGNITUDE = 0.12f;
Adam Cohen19f37922012-03-21 11:59:11 -0700166 private static final int REORDER_ANIMATION_DURATION = 150;
Adam Cohen091440a2015-03-18 14:16:05 -0700167 @Thunk float mReorderPreviewAnimationMagnitude;
Adam Cohen19f37922012-03-21 11:59:11 -0700168
Adam Cohen482ed822012-03-02 14:15:13 -0800169 private ArrayList<View> mIntersectingViews = new ArrayList<View>();
170 private Rect mOccupiedRect = new Rect();
171 private int[] mDirectionVector = new int[2];
Adam Cohen19f37922012-03-21 11:59:11 -0700172 int[] mPreviousReorderDirection = new int[2];
Adam Cohenb209e632012-03-27 17:09:36 -0700173 private static final int INVALID_DIRECTION = -100;
Adam Cohen482ed822012-03-02 14:15:13 -0800174
Sunny Goyal2805e632015-05-20 15:35:32 -0700175 private final Rect mTempRect = new Rect();
Winson Chung3a6e7f32013-10-09 15:50:52 -0700176
Michael Jurkaca993832012-06-29 15:17:04 -0700177 private final static Paint sPaint = new Paint();
Romain Guy8a0bff52012-05-06 13:14:33 -0700178
Adam Cohenc9735cf2015-01-23 16:11:55 -0800179 // Related to accessible drag and drop
Sunny Goyale9b651e2015-04-24 11:44:51 -0700180 private DragAndDropAccessibilityDelegate mTouchHelper;
Adam Cohenc9735cf2015-01-23 16:11:55 -0800181 private boolean mUseTouchHelper = false;
Adam Cohenc9735cf2015-01-23 16:11:55 -0800182
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800183 public CellLayout(Context context) {
184 this(context, null);
185 }
186
187 public CellLayout(Context context, AttributeSet attrs) {
188 this(context, attrs, 0);
189 }
190
191 public CellLayout(Context context, AttributeSet attrs, int defStyle) {
192 super(context, attrs, defStyle);
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700193
194 // A ViewGroup usually does not draw, but CellLayout needs to draw a rectangle to show
195 // the user where a dragged item will land when dropped.
196 setWillNotDraw(false);
Romain Guyce3cbd12013-02-25 15:00:36 -0800197 setClipToPadding(false);
Adam Cohen2acce882012-03-28 19:03:19 -0700198 mLauncher = (Launcher) context;
Michael Jurkaa63c4522010-08-19 13:52:27 -0700199
Adam Cohen2e6da152015-05-06 11:42:25 -0700200 DeviceProfile grid = mLauncher.getDeviceProfile();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800201
Winson Chung11a1a532013-09-13 11:14:45 -0700202 mCellWidth = mCellHeight = -1;
Nilesh Agrawal5f7099a2014-01-02 15:54:57 -0800203 mFixedCellWidth = mFixedCellHeight = -1;
Winson Chung5f8afe62013-08-12 16:19:28 -0700204 mWidthGap = mOriginalWidthGap = 0;
205 mHeightGap = mOriginalHeightGap = 0;
206 mMaxGap = Integer.MAX_VALUE;
Sunny Goyalff4ba2d2016-04-02 14:12:34 -0700207
208 mCountX = grid.inv.numColumns;
209 mCountY = grid.inv.numRows;
210 mOccupied = new GridOccupancy(mCountX, mCountY);
211 mTmpOccupied = new GridOccupancy(mCountX, mCountY);
212
Adam Cohen5b53f292012-03-29 14:30:35 -0700213 mPreviousReorderDirection[0] = INVALID_DIRECTION;
214 mPreviousReorderDirection[1] = INVALID_DIRECTION;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800215
Adam Cohenefca0272016-02-24 19:19:06 -0800216 mFolderLeaveBehind.delegateCellX = -1;
217 mFolderLeaveBehind.delegateCellY = -1;
218
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800219 setAlwaysDrawnWithCacheEnabled(false);
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700220 final Resources res = getResources();
Winson Chung6e1c0d32013-10-25 15:24:24 -0700221 mHotseatScale = (float) grid.hotseatIconSizePx / grid.iconSizePx;
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700222
Tony Wickhame0c33232016-02-08 11:37:04 -0800223 mBackground = (TransitionDrawable) res.getDrawable(
224 FeatureFlags.LAUNCHER3_LEGACY_WORKSPACE_DND ? R.drawable.bg_screenpanel
225 : R.drawable.bg_celllayout);
Sunny Goyal2805e632015-05-20 15:35:32 -0700226 mBackground.setCallback(this);
Winson Chunge8f1d042015-07-31 12:39:57 -0700227 mBackground.setAlpha((int) (mBackgroundAlpha * 255));
Michael Jurka33945b22010-12-21 18:19:38 -0800228
Adam Cohenfa3c58f2013-12-06 16:10:55 -0800229 mReorderPreviewAnimationMagnitude = (REORDER_PREVIEW_MAGNITUDE *
Winson Chung5f8afe62013-08-12 16:19:28 -0700230 grid.iconSizePx);
Adam Cohen19f37922012-03-21 11:59:11 -0700231
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700232 // Initialize the data structures used for the drag visualization.
Patrick Dubroyce34a972010-10-19 10:34:32 -0700233 mEaseOutInterpolator = new DecelerateInterpolator(2.5f); // Quint ease out
Winson Chungb8c69f32011-10-19 21:36:08 -0700234 mDragCell[0] = mDragCell[1] = -1;
Joe Onorato4be866d2010-10-10 11:26:02 -0700235 for (int i = 0; i < mDragOutlines.length; i++) {
Adam Cohend41fbf52012-02-16 23:53:59 -0800236 mDragOutlines[i] = new Rect(-1, -1, -1, -1);
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700237 }
238
239 // When dragging things around the home screens, we show a green outline of
240 // where the item will land. The outlines gradually fade out, leaving a trail
241 // behind the drag path.
242 // Set up all the animations that are used to implement this fading.
243 final int duration = res.getInteger(R.integer.config_dragOutlineFadeTime);
Chet Haase472b2812010-10-14 07:02:04 -0700244 final float fromAlphaValue = 0;
245 final float toAlphaValue = (float)res.getInteger(R.integer.config_dragOutlineMaxAlpha);
Joe Onorato4be866d2010-10-10 11:26:02 -0700246
Patrick Dubroy8e58e912010-10-14 13:21:48 -0700247 Arrays.fill(mDragOutlineAlphas, fromAlphaValue);
Joe Onorato4be866d2010-10-10 11:26:02 -0700248
249 for (int i = 0; i < mDragOutlineAnims.length; i++) {
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700250 final InterruptibleInOutAnimator anim =
Michael Jurkaf1ad6082013-03-13 12:55:46 +0100251 new InterruptibleInOutAnimator(this, duration, fromAlphaValue, toAlphaValue);
Patrick Dubroyce34a972010-10-19 10:34:32 -0700252 anim.getAnimator().setInterpolator(mEaseOutInterpolator);
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700253 final int thisIndex = i;
Chet Haase472b2812010-10-14 07:02:04 -0700254 anim.getAnimator().addUpdateListener(new AnimatorUpdateListener() {
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700255 public void onAnimationUpdate(ValueAnimator animation) {
Joe Onorato4be866d2010-10-10 11:26:02 -0700256 final Bitmap outline = (Bitmap)anim.getTag();
257
258 // If an animation is started and then stopped very quickly, we can still
259 // get spurious updates we've cleared the tag. Guard against this.
260 if (outline == null) {
Tony Wickhama0628cc2015-10-14 15:23:04 -0700261 if (LOGD) {
Patrick Dubroyfe6bd872010-10-13 17:32:10 -0700262 Object val = animation.getAnimatedValue();
263 Log.d(TAG, "anim " + thisIndex + " update: " + val +
264 ", isStopped " + anim.isStopped());
265 }
Joe Onorato4be866d2010-10-10 11:26:02 -0700266 // Try to prevent it from continuing to run
267 animation.cancel();
268 } else {
Chet Haase472b2812010-10-14 07:02:04 -0700269 mDragOutlineAlphas[thisIndex] = (Float) animation.getAnimatedValue();
Adam Cohend41fbf52012-02-16 23:53:59 -0800270 CellLayout.this.invalidate(mDragOutlines[thisIndex]);
Joe Onorato4be866d2010-10-10 11:26:02 -0700271 }
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700272 }
273 });
Joe Onorato4be866d2010-10-10 11:26:02 -0700274 // The animation holds a reference to the drag outline bitmap as long is it's
275 // running. This way the bitmap can be GCed when the animations are complete.
Chet Haase472b2812010-10-14 07:02:04 -0700276 anim.getAnimator().addListener(new AnimatorListenerAdapter() {
Michael Jurka3c4c20f2010-10-28 15:36:06 -0700277 @Override
Joe Onorato4be866d2010-10-10 11:26:02 -0700278 public void onAnimationEnd(Animator animation) {
Chet Haase472b2812010-10-14 07:02:04 -0700279 if ((Float) ((ValueAnimator) animation).getAnimatedValue() == 0f) {
Joe Onorato4be866d2010-10-10 11:26:02 -0700280 anim.setTag(null);
281 }
282 }
283 });
284 mDragOutlineAnims[i] = anim;
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700285 }
Patrick Dubroyce34a972010-10-19 10:34:32 -0700286
Michael Jurkaa52570f2012-03-20 03:18:20 -0700287 mShortcutsAndWidgets = new ShortcutAndWidgetContainer(context);
Adam Cohen2374abf2013-04-16 14:56:57 -0700288 mShortcutsAndWidgets.setCellDimensions(mCellWidth, mCellHeight, mWidthGap, mHeightGap,
Winson Chung5f8afe62013-08-12 16:19:28 -0700289 mCountX, mCountY);
Adam Cohen2374abf2013-04-16 14:56:57 -0700290
Mady Mellorbb835202015-07-15 16:34:34 -0700291 mStylusEventHelper = new StylusEventHelper(new SimpleOnStylusPressListener(this), this);
Mady Melloref044dd2015-06-02 15:35:07 -0700292
Sunny Goyal4fe5a372015-05-14 19:55:10 -0700293 mTouchFeedbackView = new ClickShadowView(context);
294 addView(mTouchFeedbackView);
Michael Jurkaa52570f2012-03-20 03:18:20 -0700295 addView(mShortcutsAndWidgets);
Michael Jurka18014792010-10-14 09:01:34 -0700296 }
297
Adam Cohenc9735cf2015-01-23 16:11:55 -0800298 @TargetApi(Build.VERSION_CODES.LOLLIPOP)
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
Adam Cohen307fe232012-08-16 17:55:58 -0700367 public float getChildrenScale() {
368 return mIsHotseat ? mHotseatScale : 1.0f;
369 }
370
Winson Chung5f8afe62013-08-12 16:19:28 -0700371 public void setCellDimensions(int width, int height) {
Winson Chung11a1a532013-09-13 11:14:45 -0700372 mFixedCellWidth = mCellWidth = width;
373 mFixedCellHeight = mCellHeight = height;
Winson Chung5f8afe62013-08-12 16:19:28 -0700374 mShortcutsAndWidgets.setCellDimensions(mCellWidth, mCellHeight, mWidthGap, mHeightGap,
375 mCountX, mCountY);
376 }
377
Adam Cohen2801caf2011-05-13 20:57:39 -0700378 public void setGridSize(int x, int y) {
379 mCountX = x;
380 mCountY = y;
Sunny Goyalff4ba2d2016-04-02 14:12:34 -0700381 mOccupied = new GridOccupancy(mCountX, mCountY);
382 mTmpOccupied = new GridOccupancy(mCountX, mCountY);
Adam Cohen7fbec102012-03-27 12:42:19 -0700383 mTempRectStack.clear();
Adam Cohen2374abf2013-04-16 14:56:57 -0700384 mShortcutsAndWidgets.setCellDimensions(mCellWidth, mCellHeight, mWidthGap, mHeightGap,
Winson Chung5f8afe62013-08-12 16:19:28 -0700385 mCountX, mCountY);
Adam Cohen76fc0852011-06-17 13:26:23 -0700386 requestLayout();
Adam Cohen2801caf2011-05-13 20:57:39 -0700387 }
388
Adam Cohen2374abf2013-04-16 14:56:57 -0700389 // Set whether or not to invert the layout horizontally if the layout is in RTL mode.
390 public void setInvertIfRtl(boolean invert) {
391 mShortcutsAndWidgets.setInvertIfRtl(invert);
392 }
393
Adam Cohen917e3882013-10-31 15:03:35 -0700394 public void setDropPending(boolean pending) {
395 mDropPending = pending;
396 }
397
398 public boolean isDropPending() {
399 return mDropPending;
400 }
401
Sunny Goyal4b6eb262015-05-14 19:24:40 -0700402 @Override
403 public void setPressedIcon(BubbleTextView icon, Bitmap background) {
Sunny Goyal508da152014-08-14 10:53:27 -0700404 if (icon == null || background == null) {
405 mTouchFeedbackView.setBitmap(null);
406 mTouchFeedbackView.animate().cancel();
407 } else {
Sunny Goyal508da152014-08-14 10:53:27 -0700408 if (mTouchFeedbackView.setBitmap(background)) {
Sunny Goyal4fe5a372015-05-14 19:55:10 -0700409 mTouchFeedbackView.alignWithIconView(icon, mShortcutsAndWidgets);
410 mTouchFeedbackView.animateShadow();
Sunny Goyal508da152014-08-14 10:53:27 -0700411 }
Patrick Dubroy96864c32011-03-10 17:17:23 -0800412 }
413 }
414
Adam Cohenc50438c2014-08-19 17:43:05 -0700415 void disableDragTarget() {
416 mIsDragTarget = false;
417 }
418
Tony Wickham0f97b782015-12-02 17:55:07 -0800419 public boolean isDragTarget() {
420 return mIsDragTarget;
421 }
422
Adam Cohenc50438c2014-08-19 17:43:05 -0700423 void setIsDragOverlapping(boolean isDragOverlapping) {
424 if (mIsDragOverlapping != isDragOverlapping) {
425 mIsDragOverlapping = isDragOverlapping;
Sunny Goyal2805e632015-05-20 15:35:32 -0700426 if (mIsDragOverlapping) {
427 mBackground.startTransition(BACKGROUND_ACTIVATE_DURATION);
428 } else {
Winson Chunge8f1d042015-07-31 12:39:57 -0700429 if (mBackgroundAlpha > 0f) {
430 mBackground.reverseTransition(BACKGROUND_ACTIVATE_DURATION);
431 } else {
432 mBackground.resetTransition();
433 }
Sunny Goyal2805e632015-05-20 15:35:32 -0700434 }
Adam Cohenc50438c2014-08-19 17:43:05 -0700435 invalidate();
436 }
437 }
438
Sunny Goyale2fd14b2015-08-27 17:45:46 -0700439 public void disableJailContent() {
440 mJailContent = false;
441 }
442
443 @Override
444 protected void dispatchSaveInstanceState(SparseArray<Parcelable> container) {
445 if (mJailContent) {
446 ParcelableSparseArray jail = getJailedArray(container);
447 super.dispatchSaveInstanceState(jail);
448 container.put(R.id.cell_layout_jail_id, jail);
449 } else {
450 super.dispatchSaveInstanceState(container);
451 }
452 }
453
454 @Override
455 protected void dispatchRestoreInstanceState(SparseArray<Parcelable> container) {
456 super.dispatchRestoreInstanceState(mJailContent ? getJailedArray(container) : container);
457 }
458
459 private ParcelableSparseArray getJailedArray(SparseArray<Parcelable> container) {
460 final Parcelable parcelable = container.get(R.id.cell_layout_jail_id);
461 return parcelable instanceof ParcelableSparseArray ?
462 (ParcelableSparseArray) parcelable : new ParcelableSparseArray();
463 }
464
Tony Wickham0f97b782015-12-02 17:55:07 -0800465 public boolean getIsDragOverlapping() {
466 return mIsDragOverlapping;
467 }
468
Jeff Sharkey83f111d2009-04-20 21:03:13 -0700469 @Override
Patrick Dubroy1262e362010-10-06 15:49:50 -0700470 protected void onDraw(Canvas canvas) {
Sunny Goyal05739772015-05-19 19:59:09 -0700471 if (!mIsDragTarget) {
472 return;
473 }
474
Michael Jurka3e7c7632010-10-02 16:01:03 -0700475 // When we're large, we are either drawn in a "hover" state (ie when dragging an item to
476 // a neighboring page) or with just a normal background (if backgroundAlpha > 0.0f)
477 // When we're small, we are either drawn normally or in the "accepts drops" state (during
478 // a drag). However, we also drag the mini hover background *over* one of those two
479 // backgrounds
Sunny Goyal05739772015-05-19 19:59:09 -0700480 if (mBackgroundAlpha > 0.0f) {
Sunny Goyal2805e632015-05-20 15:35:32 -0700481 mBackground.draw(canvas);
Michael Jurkaa63c4522010-08-19 13:52:27 -0700482 }
Romain Guya6abce82009-11-10 02:54:41 -0800483
Patrick Dubroy8e58e912010-10-14 13:21:48 -0700484 final Paint paint = mDragOutlinePaint;
Joe Onorato4be866d2010-10-10 11:26:02 -0700485 for (int i = 0; i < mDragOutlines.length; i++) {
Chet Haase472b2812010-10-14 07:02:04 -0700486 final float alpha = mDragOutlineAlphas[i];
Joe Onorato4be866d2010-10-10 11:26:02 -0700487 if (alpha > 0) {
Joe Onorato4be866d2010-10-10 11:26:02 -0700488 final Bitmap b = (Bitmap) mDragOutlineAnims[i].getTag();
Chet Haase472b2812010-10-14 07:02:04 -0700489 paint.setAlpha((int)(alpha + .5f));
Sunny Goyal106bf642015-07-16 12:18:06 -0700490 canvas.drawBitmap(b, null, mDragOutlines[i], paint);
Winson Chung150fbab2010-09-29 17:14:26 -0700491 }
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700492 }
Patrick Dubroy96864c32011-03-10 17:17:23 -0800493
Adam Cohen482ed822012-03-02 14:15:13 -0800494 if (DEBUG_VISUALIZE_OCCUPIED) {
495 int[] pt = new int[2];
496 ColorDrawable cd = new ColorDrawable(Color.RED);
Adam Cohene7587d22012-05-24 18:50:02 -0700497 cd.setBounds(0, 0, mCellWidth, mCellHeight);
Adam Cohen482ed822012-03-02 14:15:13 -0800498 for (int i = 0; i < mCountX; i++) {
499 for (int j = 0; j < mCountY; j++) {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -0700500 if (mOccupied.cells[i][j]) {
Adam Cohen482ed822012-03-02 14:15:13 -0800501 cellToPoint(i, j, pt);
502 canvas.save();
503 canvas.translate(pt[0], pt[1]);
504 cd.draw(canvas);
505 canvas.restore();
506 }
507 }
508 }
509 }
510
Adam Cohenefca0272016-02-24 19:19:06 -0800511 for (int i = 0; i < mFolderBackgrounds.size(); i++) {
512 FolderIcon.PreviewBackground bg = mFolderBackgrounds.get(i);
513 cellToPoint(bg.delegateCellX, bg.delegateCellY, mTempLocation);
514 canvas.save();
515 canvas.translate(mTempLocation[0], mTempLocation[1]);
516 bg.drawBackground(canvas, mFolderBgPaint);
Adam Cohenf172b742016-03-30 19:28:34 -0700517 if (!bg.isClipping) {
518 bg.drawBackgroundStroke(canvas, mFolderBgPaint);
519 }
Adam Cohenefca0272016-02-24 19:19:06 -0800520 canvas.restore();
Adam Cohen69ce2e52011-07-03 19:25:21 -0700521 }
Adam Cohenc51934b2011-07-26 21:07:43 -0700522
Adam Cohenefca0272016-02-24 19:19:06 -0800523 if (mFolderLeaveBehind.delegateCellX >= 0 && mFolderLeaveBehind.delegateCellY >= 0) {
524 cellToPoint(mFolderLeaveBehind.delegateCellX,
525 mFolderLeaveBehind.delegateCellY, mTempLocation);
526 canvas.save();
527 canvas.translate(mTempLocation[0], mTempLocation[1]);
528 mFolderLeaveBehind.drawLeaveBehind(canvas, mFolderBgPaint);
529 canvas.restore();
Adam Cohenc51934b2011-07-26 21:07:43 -0700530 }
Adam Cohen69ce2e52011-07-03 19:25:21 -0700531 }
532
Adam Cohenefca0272016-02-24 19:19:06 -0800533 @Override
534 protected void dispatchDraw(Canvas canvas) {
535 super.dispatchDraw(canvas);
536
537 for (int i = 0; i < mFolderBackgrounds.size(); i++) {
538 FolderIcon.PreviewBackground bg = mFolderBackgrounds.get(i);
Adam Cohenf172b742016-03-30 19:28:34 -0700539 if (bg.isClipping) {
540 cellToPoint(bg.delegateCellX, bg.delegateCellY, mTempLocation);
541 canvas.save();
542 canvas.translate(mTempLocation[0], mTempLocation[1]);
543 bg.drawBackgroundStroke(canvas, mFolderBgPaint);
544 canvas.restore();
545 }
Adam Cohenefca0272016-02-24 19:19:06 -0800546 }
Adam Cohen69ce2e52011-07-03 19:25:21 -0700547 }
548
Adam Cohenefca0272016-02-24 19:19:06 -0800549 public void addFolderBackground(FolderIcon.PreviewBackground bg) {
550 mFolderBackgrounds.add(bg);
551 }
552 public void removeFolderBackground(FolderIcon.PreviewBackground bg) {
553 mFolderBackgrounds.remove(bg);
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700554 }
555
Adam Cohenc51934b2011-07-26 21:07:43 -0700556 public void setFolderLeaveBehindCell(int x, int y) {
Adam Cohenefca0272016-02-24 19:19:06 -0800557
558 DeviceProfile grid = mLauncher.getDeviceProfile();
559 View child = getChildAt(x, y);
560
561 mFolderLeaveBehind.setup(getResources().getDisplayMetrics(), grid, null,
562 child.getMeasuredWidth(), child.getPaddingTop());
563
564 mFolderLeaveBehind.delegateCellX = x;
565 mFolderLeaveBehind.delegateCellY = y;
Adam Cohenc51934b2011-07-26 21:07:43 -0700566 invalidate();
567 }
568
569 public void clearFolderLeaveBehind() {
Adam Cohenefca0272016-02-24 19:19:06 -0800570 mFolderLeaveBehind.delegateCellX = -1;
571 mFolderLeaveBehind.delegateCellY = -1;
Adam Cohenc51934b2011-07-26 21:07:43 -0700572 invalidate();
573 }
574
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700575 @Override
Michael Jurkae6235dd2011-10-04 15:02:05 -0700576 public boolean shouldDelayChildPressedState() {
577 return false;
578 }
579
Adam Cohen1462de32012-07-24 22:34:36 -0700580 public void restoreInstanceState(SparseArray<Parcelable> states) {
Sunny Goyal33a152f2014-07-22 12:13:14 -0700581 try {
582 dispatchRestoreInstanceState(states);
583 } catch (IllegalArgumentException ex) {
Sunny Goyal6c56c682015-07-16 14:09:05 -0700584 if (ProviderConfig.IS_DOGFOOD_BUILD) {
Sunny Goyal33a152f2014-07-22 12:13:14 -0700585 throw ex;
586 }
587 // Mismatched viewId / viewType preventing restore. Skip restore on production builds.
588 Log.e(TAG, "Ignoring an error while restoring a view instance state", ex);
589 }
Adam Cohen1462de32012-07-24 22:34:36 -0700590 }
591
Michael Jurkae6235dd2011-10-04 15:02:05 -0700592 @Override
Jeff Sharkey83f111d2009-04-20 21:03:13 -0700593 public void cancelLongPress() {
594 super.cancelLongPress();
595
596 // Cancel long press for all children
597 final int count = getChildCount();
598 for (int i = 0; i < count; i++) {
599 final View child = getChildAt(i);
600 child.cancelLongPress();
601 }
602 }
603
Michael Jurkadee05892010-07-27 10:01:56 -0700604 public void setOnInterceptTouchListener(View.OnTouchListener listener) {
605 mInterceptTouchListener = listener;
606 }
607
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800608 public int getCountX() {
Adam Cohend22015c2010-07-26 22:02:18 -0700609 return mCountX;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800610 }
611
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800612 public int getCountY() {
Adam Cohend22015c2010-07-26 22:02:18 -0700613 return mCountY;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800614 }
615
Andrew Flynn0dca1ec2012-02-29 13:33:22 -0800616 public void setIsHotseat(boolean isHotseat) {
617 mIsHotseat = isHotseat;
Winson Chung5f8afe62013-08-12 16:19:28 -0700618 mShortcutsAndWidgets.setIsHotseat(isHotseat);
Andrew Flynn0dca1ec2012-02-29 13:33:22 -0800619 }
620
Sunny Goyale9b651e2015-04-24 11:44:51 -0700621 public boolean isHotseat() {
622 return mIsHotseat;
623 }
624
Andrew Flynn0dca1ec2012-02-29 13:33:22 -0800625 public boolean addViewToCellLayout(View child, int index, int childId, LayoutParams params,
Sunny Goyalf7a29e82015-04-24 15:20:43 -0700626 boolean markCells) {
Winson Chungaafa03c2010-06-11 17:34:16 -0700627 final LayoutParams lp = params;
628
Andrew Flynnde38e422012-05-08 11:22:15 -0700629 // Hotseat icons - remove text
Andrew Flynn0dca1ec2012-02-29 13:33:22 -0800630 if (child instanceof BubbleTextView) {
631 BubbleTextView bubbleChild = (BubbleTextView) child;
Winson Chung5f8afe62013-08-12 16:19:28 -0700632 bubbleChild.setTextVisibility(!mIsHotseat);
Andrew Flynn0dca1ec2012-02-29 13:33:22 -0800633 }
634
Adam Cohen307fe232012-08-16 17:55:58 -0700635 child.setScaleX(getChildrenScale());
636 child.setScaleY(getChildrenScale());
637
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800638 // Generate an id for each view, this assumes we have at most 256x256 cells
639 // per workspace screen
Adam Cohend22015c2010-07-26 22:02:18 -0700640 if (lp.cellX >= 0 && lp.cellX <= mCountX - 1 && lp.cellY >= 0 && lp.cellY <= mCountY - 1) {
Winson Chungaafa03c2010-06-11 17:34:16 -0700641 // If the horizontal or vertical span is set to -1, it is taken to
642 // mean that it spans the extent of the CellLayout
Adam Cohend22015c2010-07-26 22:02:18 -0700643 if (lp.cellHSpan < 0) lp.cellHSpan = mCountX;
644 if (lp.cellVSpan < 0) lp.cellVSpan = mCountY;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800645
Winson Chungaafa03c2010-06-11 17:34:16 -0700646 child.setId(childId);
Tony Wickhama0628cc2015-10-14 15:23:04 -0700647 if (LOGD) {
648 Log.d(TAG, "Adding view to ShortcutsAndWidgetsContainer: " + child);
649 }
Sunny Goyalf7a29e82015-04-24 15:20:43 -0700650 mShortcutsAndWidgets.addView(child, index, lp);
Michael Jurkadee05892010-07-27 10:01:56 -0700651
Michael Jurkaf3ca3ab2010-10-20 17:08:24 -0700652 if (markCells) markCellsAsOccupiedForView(child);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700653
Winson Chungaafa03c2010-06-11 17:34:16 -0700654 return true;
655 }
656 return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800657 }
Michael Jurka3e7c7632010-10-02 16:01:03 -0700658
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800659 @Override
Michael Jurka0280c3b2010-09-17 15:00:07 -0700660 public void removeAllViews() {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -0700661 mOccupied.clear();
Michael Jurkaa52570f2012-03-20 03:18:20 -0700662 mShortcutsAndWidgets.removeAllViews();
Michael Jurka0280c3b2010-09-17 15:00:07 -0700663 }
664
665 @Override
666 public void removeAllViewsInLayout() {
Michael Jurkaa52570f2012-03-20 03:18:20 -0700667 if (mShortcutsAndWidgets.getChildCount() > 0) {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -0700668 mOccupied.clear();
Michael Jurkaa52570f2012-03-20 03:18:20 -0700669 mShortcutsAndWidgets.removeAllViewsInLayout();
Michael Jurka7cfc2822011-08-02 20:19:24 -0700670 }
Michael Jurka0280c3b2010-09-17 15:00:07 -0700671 }
672
673 @Override
674 public void removeView(View view) {
675 markCellsAsUnoccupiedForView(view);
Michael Jurkaa52570f2012-03-20 03:18:20 -0700676 mShortcutsAndWidgets.removeView(view);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700677 }
678
679 @Override
680 public void removeViewAt(int index) {
Michael Jurkaa52570f2012-03-20 03:18:20 -0700681 markCellsAsUnoccupiedForView(mShortcutsAndWidgets.getChildAt(index));
682 mShortcutsAndWidgets.removeViewAt(index);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700683 }
684
685 @Override
686 public void removeViewInLayout(View view) {
687 markCellsAsUnoccupiedForView(view);
Michael Jurkaa52570f2012-03-20 03:18:20 -0700688 mShortcutsAndWidgets.removeViewInLayout(view);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700689 }
690
691 @Override
692 public void removeViews(int start, int count) {
693 for (int i = start; i < start + count; i++) {
Michael Jurkaa52570f2012-03-20 03:18:20 -0700694 markCellsAsUnoccupiedForView(mShortcutsAndWidgets.getChildAt(i));
Michael Jurka0280c3b2010-09-17 15:00:07 -0700695 }
Michael Jurkaa52570f2012-03-20 03:18:20 -0700696 mShortcutsAndWidgets.removeViews(start, count);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700697 }
698
699 @Override
700 public void removeViewsInLayout(int start, int count) {
701 for (int i = start; i < start + count; i++) {
Michael Jurkaa52570f2012-03-20 03:18:20 -0700702 markCellsAsUnoccupiedForView(mShortcutsAndWidgets.getChildAt(i));
Michael Jurka0280c3b2010-09-17 15:00:07 -0700703 }
Michael Jurkaa52570f2012-03-20 03:18:20 -0700704 mShortcutsAndWidgets.removeViewsInLayout(start, count);
Michael Jurkaabded662011-03-04 12:06:57 -0800705 }
706
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700707 /**
Winson Chungaafa03c2010-06-11 17:34:16 -0700708 * Given a point, return the cell that strictly encloses that point
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800709 * @param x X coordinate of the point
710 * @param y Y coordinate of the point
711 * @param result Array of 2 ints to hold the x and y coordinate of the cell
712 */
Sunny Goyale9b651e2015-04-24 11:44:51 -0700713 public void pointToCellExact(int x, int y, int[] result) {
Winson Chung4b825dcd2011-06-19 12:41:22 -0700714 final int hStartPadding = getPaddingLeft();
715 final int vStartPadding = getPaddingTop();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800716
717 result[0] = (x - hStartPadding) / (mCellWidth + mWidthGap);
718 result[1] = (y - vStartPadding) / (mCellHeight + mHeightGap);
719
Adam Cohend22015c2010-07-26 22:02:18 -0700720 final int xAxis = mCountX;
721 final int yAxis = mCountY;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800722
723 if (result[0] < 0) result[0] = 0;
724 if (result[0] >= xAxis) result[0] = xAxis - 1;
725 if (result[1] < 0) result[1] = 0;
726 if (result[1] >= yAxis) result[1] = yAxis - 1;
727 }
Winson Chungaafa03c2010-06-11 17:34:16 -0700728
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800729 /**
730 * Given a point, return the cell that most closely encloses that point
731 * @param x X coordinate of the point
732 * @param y Y coordinate of the point
733 * @param result Array of 2 ints to hold the x and y coordinate of the cell
734 */
735 void pointToCellRounded(int x, int y, int[] result) {
736 pointToCellExact(x + (mCellWidth / 2), y + (mCellHeight / 2), result);
737 }
738
739 /**
740 * Given a cell coordinate, return the point that represents the upper left corner of that cell
Winson Chungaafa03c2010-06-11 17:34:16 -0700741 *
742 * @param cellX X coordinate of the cell
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800743 * @param cellY Y coordinate of the cell
Winson Chungaafa03c2010-06-11 17:34:16 -0700744 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800745 * @param result Array of 2 ints to hold the x and y coordinate of the point
746 */
747 void cellToPoint(int cellX, int cellY, int[] result) {
Winson Chung4b825dcd2011-06-19 12:41:22 -0700748 final int hStartPadding = getPaddingLeft();
749 final int vStartPadding = getPaddingTop();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800750
751 result[0] = hStartPadding + cellX * (mCellWidth + mWidthGap);
752 result[1] = vStartPadding + cellY * (mCellHeight + mHeightGap);
753 }
754
Adam Cohene3e27a82011-04-15 12:07:39 -0700755 /**
Adam Cohen482ed822012-03-02 14:15:13 -0800756 * Given a cell coordinate, return the point that represents the center of the cell
Adam Cohene3e27a82011-04-15 12:07:39 -0700757 *
758 * @param cellX X coordinate of the cell
759 * @param cellY Y coordinate of the cell
760 *
761 * @param result Array of 2 ints to hold the x and y coordinate of the point
762 */
763 void cellToCenterPoint(int cellX, int cellY, int[] result) {
Adam Cohen47a876d2012-03-19 13:21:41 -0700764 regionToCenterPoint(cellX, cellY, 1, 1, result);
765 }
766
767 /**
768 * Given a cell coordinate and span return the point that represents the center of the regio
769 *
770 * @param cellX X coordinate of the cell
771 * @param cellY Y coordinate of the cell
772 *
773 * @param result Array of 2 ints to hold the x and y coordinate of the point
774 */
775 void regionToCenterPoint(int cellX, int cellY, int spanX, int spanY, int[] result) {
Winson Chung4b825dcd2011-06-19 12:41:22 -0700776 final int hStartPadding = getPaddingLeft();
777 final int vStartPadding = getPaddingTop();
Adam Cohen47a876d2012-03-19 13:21:41 -0700778 result[0] = hStartPadding + cellX * (mCellWidth + mWidthGap) +
779 (spanX * mCellWidth + (spanX - 1) * mWidthGap) / 2;
780 result[1] = vStartPadding + cellY * (mCellHeight + mHeightGap) +
781 (spanY * mCellHeight + (spanY - 1) * mHeightGap) / 2;
Adam Cohene3e27a82011-04-15 12:07:39 -0700782 }
783
Adam Cohen19f37922012-03-21 11:59:11 -0700784 /**
785 * Given a cell coordinate and span fills out a corresponding pixel rect
786 *
787 * @param cellX X coordinate of the cell
788 * @param cellY Y coordinate of the cell
789 * @param result Rect in which to write the result
790 */
791 void regionToRect(int cellX, int cellY, int spanX, int spanY, Rect result) {
792 final int hStartPadding = getPaddingLeft();
793 final int vStartPadding = getPaddingTop();
794 final int left = hStartPadding + cellX * (mCellWidth + mWidthGap);
795 final int top = vStartPadding + cellY * (mCellHeight + mHeightGap);
796 result.set(left, top, left + (spanX * mCellWidth + (spanX - 1) * mWidthGap),
797 top + (spanY * mCellHeight + (spanY - 1) * mHeightGap));
798 }
799
Adam Cohen482ed822012-03-02 14:15:13 -0800800 public float getDistanceFromCell(float x, float y, int[] cell) {
801 cellToCenterPoint(cell[0], cell[1], mTmpPoint);
Sunny Goyalf7a29e82015-04-24 15:20:43 -0700802 return (float) Math.hypot(x - mTmpPoint[0], y - mTmpPoint[1]);
Adam Cohen482ed822012-03-02 14:15:13 -0800803 }
804
Adam Cohenf9c184a2016-01-15 16:47:43 -0800805 public int getCellWidth() {
Romain Guy84f296c2009-11-04 15:00:44 -0800806 return mCellWidth;
807 }
808
809 int getCellHeight() {
810 return mCellHeight;
811 }
812
Adam Cohend4844c32011-02-18 19:25:06 -0800813 int getWidthGap() {
814 return mWidthGap;
815 }
816
817 int getHeightGap() {
818 return mHeightGap;
819 }
820
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700821 public void setFixedSize(int width, int height) {
822 mFixedWidth = width;
823 mFixedHeight = height;
824 }
825
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800826 @Override
827 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800828 int widthSpecMode = MeasureSpec.getMode(widthMeasureSpec);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800829 int heightSpecMode = MeasureSpec.getMode(heightMeasureSpec);
Winson Chung5f8afe62013-08-12 16:19:28 -0700830 int widthSize = MeasureSpec.getSize(widthMeasureSpec);
831 int heightSize = MeasureSpec.getSize(heightMeasureSpec);
Winson Chung2d75f122013-09-23 16:53:31 -0700832 int childWidthSize = widthSize - (getPaddingLeft() + getPaddingRight());
833 int childHeightSize = heightSize - (getPaddingTop() + getPaddingBottom());
Winson Chung11a1a532013-09-13 11:14:45 -0700834 if (mFixedCellWidth < 0 || mFixedCellHeight < 0) {
Sunny Goyalc6205602015-05-21 20:46:33 -0700835 int cw = DeviceProfile.calculateCellWidth(childWidthSize, mCountX);
836 int ch = DeviceProfile.calculateCellHeight(childHeightSize, mCountY);
Winson Chung11a1a532013-09-13 11:14:45 -0700837 if (cw != mCellWidth || ch != mCellHeight) {
838 mCellWidth = cw;
839 mCellHeight = ch;
840 mShortcutsAndWidgets.setCellDimensions(mCellWidth, mCellHeight, mWidthGap,
841 mHeightGap, mCountX, mCountY);
842 }
Winson Chung5f8afe62013-08-12 16:19:28 -0700843 }
Winson Chungaafa03c2010-06-11 17:34:16 -0700844
Winson Chung2d75f122013-09-23 16:53:31 -0700845 int newWidth = childWidthSize;
846 int newHeight = childHeightSize;
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700847 if (mFixedWidth > 0 && mFixedHeight > 0) {
848 newWidth = mFixedWidth;
849 newHeight = mFixedHeight;
850 } else if (widthSpecMode == MeasureSpec.UNSPECIFIED || heightSpecMode == MeasureSpec.UNSPECIFIED) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800851 throw new RuntimeException("CellLayout cannot have UNSPECIFIED dimensions");
852 }
853
Adam Cohend22015c2010-07-26 22:02:18 -0700854 int numWidthGaps = mCountX - 1;
855 int numHeightGaps = mCountY - 1;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800856
Adam Cohen234c4cd2011-07-17 21:03:04 -0700857 if (mOriginalWidthGap < 0 || mOriginalHeightGap < 0) {
Winson Chung2d75f122013-09-23 16:53:31 -0700858 int hSpace = childWidthSize;
859 int vSpace = childHeightSize;
Adam Cohenf4bd5792012-04-27 11:35:29 -0700860 int hFreeSpace = hSpace - (mCountX * mCellWidth);
861 int vFreeSpace = vSpace - (mCountY * mCellHeight);
Winson Chung4b825dcd2011-06-19 12:41:22 -0700862 mWidthGap = Math.min(mMaxGap, numWidthGaps > 0 ? (hFreeSpace / numWidthGaps) : 0);
863 mHeightGap = Math.min(mMaxGap,numHeightGaps > 0 ? (vFreeSpace / numHeightGaps) : 0);
Winson Chung5f8afe62013-08-12 16:19:28 -0700864 mShortcutsAndWidgets.setCellDimensions(mCellWidth, mCellHeight, mWidthGap,
865 mHeightGap, mCountX, mCountY);
Adam Cohen234c4cd2011-07-17 21:03:04 -0700866 } else {
867 mWidthGap = mOriginalWidthGap;
868 mHeightGap = mOriginalHeightGap;
Winson Chungece7f5b2010-10-22 14:54:12 -0700869 }
Sunny Goyal4fe5a372015-05-14 19:55:10 -0700870
871 // Make the feedback view large enough to hold the blur bitmap.
872 mTouchFeedbackView.measure(
873 MeasureSpec.makeMeasureSpec(mCellWidth + mTouchFeedbackView.getExtraSize(),
874 MeasureSpec.EXACTLY),
875 MeasureSpec.makeMeasureSpec(mCellHeight + mTouchFeedbackView.getExtraSize(),
876 MeasureSpec.EXACTLY));
877
878 mShortcutsAndWidgets.measure(
879 MeasureSpec.makeMeasureSpec(newWidth, MeasureSpec.EXACTLY),
880 MeasureSpec.makeMeasureSpec(newHeight, MeasureSpec.EXACTLY));
881
882 int maxWidth = mShortcutsAndWidgets.getMeasuredWidth();
883 int maxHeight = mShortcutsAndWidgets.getMeasuredHeight();
Winson Chung2d75f122013-09-23 16:53:31 -0700884 if (mFixedWidth > 0 && mFixedHeight > 0) {
885 setMeasuredDimension(maxWidth, maxHeight);
886 } else {
887 setMeasuredDimension(widthSize, heightSize);
888 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800889 }
890
891 @Override
Michael Jurka28750fb2010-09-24 17:43:49 -0700892 protected void onLayout(boolean changed, int l, int t, int r, int b) {
Tony Wickham26b01422015-11-10 14:44:32 -0800893 boolean isFullscreen = mShortcutsAndWidgets.getChildCount() > 0 &&
894 ((LayoutParams) mShortcutsAndWidgets.getChildAt(0).getLayoutParams()).isFullscreen;
895 int left = getPaddingLeft();
896 if (!isFullscreen) {
Tony Wickhama501d492015-11-03 18:05:01 -0800897 left += (int) Math.ceil(getUnusedHorizontalSpace() / 2f);
Tony Wickham26b01422015-11-10 14:44:32 -0800898 }
Sunny Goyal7c786f72016-06-01 14:08:21 -0700899 int right = r - l - getPaddingRight();
900 if (!isFullscreen) {
901 right -= (int) Math.ceil(getUnusedHorizontalSpace() / 2f);
902 }
903
Winson Chung38848ca2013-10-08 12:03:44 -0700904 int top = getPaddingTop();
Sunny Goyal7c786f72016-06-01 14:08:21 -0700905 int bottom = b - t - getPaddingBottom();
Sunny Goyal4fe5a372015-05-14 19:55:10 -0700906
907 mTouchFeedbackView.layout(left, top,
908 left + mTouchFeedbackView.getMeasuredWidth(),
909 top + mTouchFeedbackView.getMeasuredHeight());
Sunny Goyal7c786f72016-06-01 14:08:21 -0700910 mShortcutsAndWidgets.layout(left, top, right, bottom);
911
912 // Expand the background drawing bounds by the padding baked into the background drawable
913 mBackground.getPadding(mTempRect);
914 mBackground.setBounds(
915 left - mTempRect.left,
916 top - mTempRect.top,
917 right + mTempRect.right,
918 bottom + mTempRect.bottom);
919 }
920
921 public Rect getBackgroundBounds() {
922 return mBackground.getBounds();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800923 }
924
Tony Wickhama501d492015-11-03 18:05:01 -0800925 /**
926 * Returns the amount of space left over after subtracting padding and cells. This space will be
927 * very small, a few pixels at most, and is a result of rounding down when calculating the cell
928 * width in {@link DeviceProfile#calculateCellWidth(int, int)}.
929 */
930 public int getUnusedHorizontalSpace() {
931 return getMeasuredWidth() - getPaddingLeft() - getPaddingRight() - (mCountX * mCellWidth);
932 }
933
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800934 @Override
935 protected void setChildrenDrawingCacheEnabled(boolean enabled) {
Michael Jurkaa52570f2012-03-20 03:18:20 -0700936 mShortcutsAndWidgets.setChildrenDrawingCacheEnabled(enabled);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800937 }
938
939 @Override
940 protected void setChildrenDrawnWithCacheEnabled(boolean enabled) {
Michael Jurkaa52570f2012-03-20 03:18:20 -0700941 mShortcutsAndWidgets.setChildrenDrawnWithCacheEnabled(enabled);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800942 }
943
Michael Jurka5f1c5092010-09-03 14:15:02 -0700944 public float getBackgroundAlpha() {
945 return mBackgroundAlpha;
Michael Jurkadee05892010-07-27 10:01:56 -0700946 }
947
Michael Jurka5f1c5092010-09-03 14:15:02 -0700948 public void setBackgroundAlpha(float alpha) {
Michael Jurkaafaa0502011-12-13 18:22:50 -0800949 if (mBackgroundAlpha != alpha) {
950 mBackgroundAlpha = alpha;
Sunny Goyal2805e632015-05-20 15:35:32 -0700951 mBackground.setAlpha((int) (mBackgroundAlpha * 255));
Michael Jurkaafaa0502011-12-13 18:22:50 -0800952 }
Michael Jurkadee05892010-07-27 10:01:56 -0700953 }
954
Sunny Goyal2805e632015-05-20 15:35:32 -0700955 @Override
956 protected boolean verifyDrawable(Drawable who) {
957 return super.verifyDrawable(who) || (mIsDragTarget && who == mBackground);
958 }
959
Michael Jurkaa52570f2012-03-20 03:18:20 -0700960 public void setShortcutAndWidgetAlpha(float alpha) {
Sunny Goyal02b50812014-09-10 15:44:42 -0700961 mShortcutsAndWidgets.setAlpha(alpha);
Michael Jurkadee05892010-07-27 10:01:56 -0700962 }
963
Michael Jurkaa52570f2012-03-20 03:18:20 -0700964 public ShortcutAndWidgetContainer getShortcutsAndWidgets() {
Sunny Goyaldcbcc862014-08-12 15:58:36 -0700965 return mShortcutsAndWidgets;
Michael Jurkaa52570f2012-03-20 03:18:20 -0700966 }
967
Patrick Dubroy440c3602010-07-13 17:50:32 -0700968 public View getChildAt(int x, int y) {
Michael Jurkaa52570f2012-03-20 03:18:20 -0700969 return mShortcutsAndWidgets.getChildAt(x, y);
Patrick Dubroy440c3602010-07-13 17:50:32 -0700970 }
971
Adam Cohen76fc0852011-06-17 13:26:23 -0700972 public boolean animateChildToPosition(final View child, int cellX, int cellY, int duration,
Adam Cohen482ed822012-03-02 14:15:13 -0800973 int delay, boolean permanent, boolean adjustOccupied) {
Michael Jurkaa52570f2012-03-20 03:18:20 -0700974 ShortcutAndWidgetContainer clc = getShortcutsAndWidgets();
Adam Cohen482ed822012-03-02 14:15:13 -0800975
Adam Cohen19f37922012-03-21 11:59:11 -0700976 if (clc.indexOfChild(child) != -1) {
Adam Cohenbfbfd262011-06-13 16:55:12 -0700977 final LayoutParams lp = (LayoutParams) child.getLayoutParams();
978 final ItemInfo info = (ItemInfo) child.getTag();
979
980 // We cancel any existing animations
981 if (mReorderAnimators.containsKey(lp)) {
982 mReorderAnimators.get(lp).cancel();
983 mReorderAnimators.remove(lp);
984 }
985
Adam Cohen482ed822012-03-02 14:15:13 -0800986 final int oldX = lp.x;
987 final int oldY = lp.y;
988 if (adjustOccupied) {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -0700989 GridOccupancy occupied = permanent ? mOccupied : mTmpOccupied;
990 occupied.markCells(lp.cellX, lp.cellY, lp.cellHSpan, lp.cellVSpan, false);
991 occupied.markCells(cellX, cellY, lp.cellHSpan, lp.cellVSpan, true);
Adam Cohen482ed822012-03-02 14:15:13 -0800992 }
Adam Cohenbfbfd262011-06-13 16:55:12 -0700993 lp.isLockedToGrid = true;
Adam Cohen482ed822012-03-02 14:15:13 -0800994 if (permanent) {
995 lp.cellX = info.cellX = cellX;
996 lp.cellY = info.cellY = cellY;
997 } else {
998 lp.tmpCellX = cellX;
999 lp.tmpCellY = cellY;
1000 }
Adam Cohenbfbfd262011-06-13 16:55:12 -07001001 clc.setupLp(lp);
1002 lp.isLockedToGrid = false;
Adam Cohen482ed822012-03-02 14:15:13 -08001003 final int newX = lp.x;
1004 final int newY = lp.y;
Adam Cohenbfbfd262011-06-13 16:55:12 -07001005
Adam Cohen76fc0852011-06-17 13:26:23 -07001006 lp.x = oldX;
1007 lp.y = oldY;
Adam Cohen76fc0852011-06-17 13:26:23 -07001008
Adam Cohen482ed822012-03-02 14:15:13 -08001009 // Exit early if we're not actually moving the view
1010 if (oldX == newX && oldY == newY) {
1011 lp.isLockedToGrid = true;
1012 return true;
1013 }
1014
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001015 ValueAnimator va = LauncherAnimUtils.ofFloat(child, 0f, 1f);
Adam Cohen482ed822012-03-02 14:15:13 -08001016 va.setDuration(duration);
1017 mReorderAnimators.put(lp, va);
1018
1019 va.addUpdateListener(new AnimatorUpdateListener() {
1020 @Override
Adam Cohenbfbfd262011-06-13 16:55:12 -07001021 public void onAnimationUpdate(ValueAnimator animation) {
Adam Cohen482ed822012-03-02 14:15:13 -08001022 float r = ((Float) animation.getAnimatedValue()).floatValue();
Adam Cohen19f37922012-03-21 11:59:11 -07001023 lp.x = (int) ((1 - r) * oldX + r * newX);
1024 lp.y = (int) ((1 - r) * oldY + r * newY);
Adam Cohen6b8a02d2012-03-22 15:13:40 -07001025 child.requestLayout();
Adam Cohenbfbfd262011-06-13 16:55:12 -07001026 }
1027 });
Adam Cohen482ed822012-03-02 14:15:13 -08001028 va.addListener(new AnimatorListenerAdapter() {
Adam Cohenbfbfd262011-06-13 16:55:12 -07001029 boolean cancelled = false;
1030 public void onAnimationEnd(Animator animation) {
1031 // If the animation was cancelled, it means that another animation
1032 // has interrupted this one, and we don't want to lock the item into
1033 // place just yet.
1034 if (!cancelled) {
1035 lp.isLockedToGrid = true;
Adam Cohen482ed822012-03-02 14:15:13 -08001036 child.requestLayout();
Adam Cohenbfbfd262011-06-13 16:55:12 -07001037 }
1038 if (mReorderAnimators.containsKey(lp)) {
1039 mReorderAnimators.remove(lp);
1040 }
1041 }
1042 public void onAnimationCancel(Animator animation) {
1043 cancelled = true;
1044 }
1045 });
Adam Cohen482ed822012-03-02 14:15:13 -08001046 va.setStartDelay(delay);
1047 va.start();
Adam Cohenbfbfd262011-06-13 16:55:12 -07001048 return true;
1049 }
1050 return false;
1051 }
1052
Tony Wickhama501d492015-11-03 18:05:01 -08001053 void visualizeDropLocation(View v, Bitmap dragOutline, int cellX, int cellY, int spanX,
1054 int spanY, boolean resize, DropTarget.DragObject dragObject) {
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -07001055 final int oldDragCellX = mDragCell[0];
1056 final int oldDragCellY = mDragCell[1];
Adam Cohen482ed822012-03-02 14:15:13 -08001057
Adam Cohen2801caf2011-05-13 20:57:39 -07001058 if (dragOutline == null && v == null) {
Adam Cohen2801caf2011-05-13 20:57:39 -07001059 return;
1060 }
1061
Adam Cohen482ed822012-03-02 14:15:13 -08001062 if (cellX != oldDragCellX || cellY != oldDragCellY) {
Sunny Goyale78e3d72015-09-24 11:23:31 -07001063 Point dragOffset = dragObject.dragView.getDragVisualizeOffset();
1064 Rect dragRegion = dragObject.dragView.getDragRegion();
1065
Adam Cohen482ed822012-03-02 14:15:13 -08001066 mDragCell[0] = cellX;
1067 mDragCell[1] = cellY;
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001068
Joe Onorato4be866d2010-10-10 11:26:02 -07001069 final int oldIndex = mDragOutlineCurrent;
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -07001070 mDragOutlineAnims[oldIndex].animateOut();
1071 mDragOutlineCurrent = (oldIndex + 1) % mDragOutlines.length;
Adam Cohend41fbf52012-02-16 23:53:59 -08001072 Rect r = mDragOutlines[mDragOutlineCurrent];
Sunny Goyal106bf642015-07-16 12:18:06 -07001073
Adam Cohend41fbf52012-02-16 23:53:59 -08001074 if (resize) {
Adam Cohen482ed822012-03-02 14:15:13 -08001075 cellToRect(cellX, cellY, spanX, spanY, r);
Sunny Goyal106bf642015-07-16 12:18:06 -07001076 } else {
1077 // Find the top left corner of the rect the object will occupy
1078 final int[] topLeft = mTmpPoint;
1079 cellToPoint(cellX, cellY, topLeft);
1080
1081 int left = topLeft[0];
1082 int top = topLeft[1];
1083
1084 if (v != null && dragOffset == null) {
1085 // When drawing the drag outline, it did not account for margin offsets
1086 // added by the view's parent.
1087 MarginLayoutParams lp = (MarginLayoutParams) v.getLayoutParams();
1088 left += lp.leftMargin;
1089 top += lp.topMargin;
1090
1091 // Offsets due to the size difference between the View and the dragOutline.
1092 // There is a size difference to account for the outer blur, which may lie
1093 // outside the bounds of the view.
1094 top += (v.getHeight() - dragOutline.getHeight()) / 2;
1095 // We center about the x axis
1096 left += ((mCellWidth * spanX) + ((spanX - 1) * mWidthGap)
1097 - dragOutline.getWidth()) / 2;
1098 } else {
1099 if (dragOffset != null && dragRegion != null) {
1100 // Center the drag region *horizontally* in the cell and apply a drag
1101 // outline offset
1102 left += dragOffset.x + ((mCellWidth * spanX) + ((spanX - 1) * mWidthGap)
1103 - dragRegion.width()) / 2;
1104 int cHeight = getShortcutsAndWidgets().getCellContentHeight();
1105 int cellPaddingY = (int) Math.max(0, ((mCellHeight - cHeight) / 2f));
1106 top += dragOffset.y + cellPaddingY;
1107 } else {
1108 // Center the drag outline in the cell
1109 left += ((mCellWidth * spanX) + ((spanX - 1) * mWidthGap)
1110 - dragOutline.getWidth()) / 2;
1111 top += ((mCellHeight * spanY) + ((spanY - 1) * mHeightGap)
1112 - dragOutline.getHeight()) / 2;
1113 }
1114 }
1115 r.set(left, top, left + dragOutline.getWidth(), top + dragOutline.getHeight());
Adam Cohend41fbf52012-02-16 23:53:59 -08001116 }
Winson Chung150fbab2010-09-29 17:14:26 -07001117
Sunny Goyal106bf642015-07-16 12:18:06 -07001118 Utilities.scaleRectAboutCenter(r, getChildrenScale());
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -07001119 mDragOutlineAnims[mDragOutlineCurrent].setTag(dragOutline);
1120 mDragOutlineAnims[mDragOutlineCurrent].animateIn();
Sunny Goyale78e3d72015-09-24 11:23:31 -07001121
1122 if (dragObject.stateAnnouncer != null) {
1123 String msg;
1124 if (isHotseat()) {
1125 msg = getContext().getString(R.string.move_to_hotseat_position,
1126 Math.max(cellX, cellY) + 1);
1127 } else {
1128 msg = getContext().getString(R.string.move_to_empty_cell,
1129 cellY + 1, cellX + 1);
1130 }
1131 dragObject.stateAnnouncer.announce(msg);
1132 }
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001133 }
1134 }
1135
Adam Cohene0310962011-04-18 16:15:31 -07001136 public void clearDragOutlines() {
1137 final int oldIndex = mDragOutlineCurrent;
1138 mDragOutlineAnims[oldIndex].animateOut();
Adam Cohend41fbf52012-02-16 23:53:59 -08001139 mDragCell[0] = mDragCell[1] = -1;
Adam Cohene0310962011-04-18 16:15:31 -07001140 }
1141
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001142 /**
Jeff Sharkey70864282009-04-07 21:08:40 -07001143 * Find a vacant area that will fit the given bounds nearest the requested
1144 * cell location. Uses Euclidean distance to score multiple vacant areas.
Winson Chungaafa03c2010-06-11 17:34:16 -07001145 *
Romain Guy51afc022009-05-04 18:03:43 -07001146 * @param pixelX The X location at which you want to search for a vacant area.
1147 * @param pixelY The Y location at which you want to search for a vacant area.
Adam Cohend41fbf52012-02-16 23:53:59 -08001148 * @param minSpanX The minimum horizontal span required
1149 * @param minSpanY The minimum vertical span required
1150 * @param spanX Horizontal span of the object.
1151 * @param spanY Vertical span of the object.
1152 * @param result Array in which to place the result, or null (in which case a new array will
1153 * be allocated)
1154 * @return The X, Y cell of a vacant area that can contain this object,
1155 * nearest the requested location.
1156 */
1157 int[] findNearestVacantArea(int pixelX, int pixelY, int minSpanX, int minSpanY, int spanX,
1158 int spanY, int[] result, int[] resultSpan) {
Sunny Goyalf7a29e82015-04-24 15:20:43 -07001159 return findNearestArea(pixelX, pixelY, minSpanX, minSpanY, spanX, spanY, true,
Adam Cohend41fbf52012-02-16 23:53:59 -08001160 result, resultSpan);
1161 }
1162
Adam Cohend41fbf52012-02-16 23:53:59 -08001163 private final Stack<Rect> mTempRectStack = new Stack<Rect>();
1164 private void lazyInitTempRectStack() {
1165 if (mTempRectStack.isEmpty()) {
1166 for (int i = 0; i < mCountX * mCountY; i++) {
1167 mTempRectStack.push(new Rect());
1168 }
1169 }
1170 }
Adam Cohen482ed822012-03-02 14:15:13 -08001171
Adam Cohend41fbf52012-02-16 23:53:59 -08001172 private void recycleTempRects(Stack<Rect> used) {
1173 while (!used.isEmpty()) {
1174 mTempRectStack.push(used.pop());
1175 }
1176 }
1177
1178 /**
1179 * Find a vacant area that will fit the given bounds nearest the requested
1180 * cell location. Uses Euclidean distance to score multiple vacant areas.
1181 *
1182 * @param pixelX The X location at which you want to search for a vacant area.
1183 * @param pixelY The Y location at which you want to search for a vacant area.
1184 * @param minSpanX The minimum horizontal span required
1185 * @param minSpanY The minimum vertical span required
1186 * @param spanX Horizontal span of the object.
1187 * @param spanY Vertical span of the object.
1188 * @param ignoreOccupied If true, the result can be an occupied cell
1189 * @param result Array in which to place the result, or null (in which case a new array will
1190 * be allocated)
1191 * @return The X, Y cell of a vacant area that can contain this object,
1192 * nearest the requested location.
1193 */
Sunny Goyalf7a29e82015-04-24 15:20:43 -07001194 private int[] findNearestArea(int pixelX, int pixelY, int minSpanX, int minSpanY, int spanX,
1195 int spanY, boolean ignoreOccupied, int[] result, int[] resultSpan) {
Adam Cohend41fbf52012-02-16 23:53:59 -08001196 lazyInitTempRectStack();
Michael Jurkac6ee42e2010-09-30 12:04:50 -07001197
Adam Cohene3e27a82011-04-15 12:07:39 -07001198 // For items with a spanX / spanY > 1, the passed in point (pixelX, pixelY) corresponds
1199 // to the center of the item, but we are searching based on the top-left cell, so
1200 // we translate the point over to correspond to the top-left.
1201 pixelX -= (mCellWidth + mWidthGap) * (spanX - 1) / 2f;
1202 pixelY -= (mCellHeight + mHeightGap) * (spanY - 1) / 2f;
1203
Jeff Sharkey70864282009-04-07 21:08:40 -07001204 // Keep track of best-scoring drop area
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001205 final int[] bestXY = result != null ? result : new int[2];
Jeff Sharkey70864282009-04-07 21:08:40 -07001206 double bestDistance = Double.MAX_VALUE;
Adam Cohend41fbf52012-02-16 23:53:59 -08001207 final Rect bestRect = new Rect(-1, -1, -1, -1);
1208 final Stack<Rect> validRegions = new Stack<Rect>();
Winson Chungaafa03c2010-06-11 17:34:16 -07001209
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001210 final int countX = mCountX;
1211 final int countY = mCountY;
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001212
Adam Cohend41fbf52012-02-16 23:53:59 -08001213 if (minSpanX <= 0 || minSpanY <= 0 || spanX <= 0 || spanY <= 0 ||
1214 spanX < minSpanX || spanY < minSpanY) {
1215 return bestXY;
1216 }
1217
1218 for (int y = 0; y < countY - (minSpanY - 1); y++) {
Michael Jurkac28de512010-08-13 11:27:44 -07001219 inner:
Adam Cohend41fbf52012-02-16 23:53:59 -08001220 for (int x = 0; x < countX - (minSpanX - 1); x++) {
1221 int ySize = -1;
1222 int xSize = -1;
Adam Cohendf035382011-04-11 17:22:04 -07001223 if (ignoreOccupied) {
Adam Cohend41fbf52012-02-16 23:53:59 -08001224 // First, let's see if this thing fits anywhere
1225 for (int i = 0; i < minSpanX; i++) {
1226 for (int j = 0; j < minSpanY; j++) {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001227 if (mOccupied.cells[x + i][y + j]) {
Adam Cohendf035382011-04-11 17:22:04 -07001228 continue inner;
1229 }
Michael Jurkac28de512010-08-13 11:27:44 -07001230 }
1231 }
Adam Cohend41fbf52012-02-16 23:53:59 -08001232 xSize = minSpanX;
1233 ySize = minSpanY;
1234
1235 // We know that the item will fit at _some_ acceptable size, now let's see
1236 // how big we can make it. We'll alternate between incrementing x and y spans
1237 // until we hit a limit.
1238 boolean incX = true;
1239 boolean hitMaxX = xSize >= spanX;
1240 boolean hitMaxY = ySize >= spanY;
1241 while (!(hitMaxX && hitMaxY)) {
1242 if (incX && !hitMaxX) {
1243 for (int j = 0; j < ySize; j++) {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001244 if (x + xSize > countX -1 || mOccupied.cells[x + xSize][y + j]) {
Adam Cohend41fbf52012-02-16 23:53:59 -08001245 // We can't move out horizontally
1246 hitMaxX = true;
1247 }
1248 }
1249 if (!hitMaxX) {
1250 xSize++;
1251 }
1252 } else if (!hitMaxY) {
1253 for (int i = 0; i < xSize; i++) {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001254 if (y + ySize > countY - 1 || mOccupied.cells[x + i][y + ySize]) {
Adam Cohend41fbf52012-02-16 23:53:59 -08001255 // We can't move out vertically
1256 hitMaxY = true;
1257 }
1258 }
1259 if (!hitMaxY) {
1260 ySize++;
1261 }
1262 }
1263 hitMaxX |= xSize >= spanX;
1264 hitMaxY |= ySize >= spanY;
1265 incX = !incX;
1266 }
1267 incX = true;
1268 hitMaxX = xSize >= spanX;
1269 hitMaxY = ySize >= spanY;
Michael Jurkac28de512010-08-13 11:27:44 -07001270 }
Sunny Goyal2805e632015-05-20 15:35:32 -07001271 final int[] cellXY = mTmpPoint;
Adam Cohene3e27a82011-04-15 12:07:39 -07001272 cellToCenterPoint(x, y, cellXY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001273
Adam Cohend41fbf52012-02-16 23:53:59 -08001274 // We verify that the current rect is not a sub-rect of any of our previous
1275 // candidates. In this case, the current rect is disqualified in favour of the
1276 // containing rect.
1277 Rect currentRect = mTempRectStack.pop();
1278 currentRect.set(x, y, x + xSize, y + ySize);
1279 boolean contained = false;
1280 for (Rect r : validRegions) {
1281 if (r.contains(currentRect)) {
1282 contained = true;
1283 break;
1284 }
1285 }
1286 validRegions.push(currentRect);
Sunny Goyalf7a29e82015-04-24 15:20:43 -07001287 double distance = Math.hypot(cellXY[0] - pixelX, cellXY[1] - pixelY);
Adam Cohen482ed822012-03-02 14:15:13 -08001288
Adam Cohend41fbf52012-02-16 23:53:59 -08001289 if ((distance <= bestDistance && !contained) ||
1290 currentRect.contains(bestRect)) {
Michael Jurkac28de512010-08-13 11:27:44 -07001291 bestDistance = distance;
1292 bestXY[0] = x;
1293 bestXY[1] = y;
Adam Cohend41fbf52012-02-16 23:53:59 -08001294 if (resultSpan != null) {
1295 resultSpan[0] = xSize;
1296 resultSpan[1] = ySize;
1297 }
1298 bestRect.set(currentRect);
Michael Jurkac28de512010-08-13 11:27:44 -07001299 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001300 }
1301 }
1302
Adam Cohenc0dcf592011-06-01 15:30:43 -07001303 // Return -1, -1 if no suitable location found
1304 if (bestDistance == Double.MAX_VALUE) {
1305 bestXY[0] = -1;
1306 bestXY[1] = -1;
Jeff Sharkey70864282009-04-07 21:08:40 -07001307 }
Adam Cohend41fbf52012-02-16 23:53:59 -08001308 recycleTempRects(validRegions);
Adam Cohenc0dcf592011-06-01 15:30:43 -07001309 return bestXY;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001310 }
Winson Chungaafa03c2010-06-11 17:34:16 -07001311
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001312 /**
Adam Cohen482ed822012-03-02 14:15:13 -08001313 * Find a vacant area that will fit the given bounds nearest the requested
1314 * cell location, and will also weigh in a suggested direction vector of the
1315 * desired location. This method computers distance based on unit grid distances,
1316 * not pixel distances.
1317 *
Adam Cohen47a876d2012-03-19 13:21:41 -07001318 * @param cellX The X cell nearest to which you want to search for a vacant area.
1319 * @param cellY The Y cell nearest which you want to search for a vacant area.
Adam Cohen482ed822012-03-02 14:15:13 -08001320 * @param spanX Horizontal span of the object.
1321 * @param spanY Vertical span of the object.
Adam Cohen47a876d2012-03-19 13:21:41 -07001322 * @param direction The favored direction in which the views should move from x, y
Sunny Goyal9eba1fd2015-10-16 08:58:57 -07001323 * @param occupied The array which represents which cells in the CellLayout are occupied
Adam Cohen47a876d2012-03-19 13:21:41 -07001324 * @param blockOccupied The array which represents which cells in the specified block (cellX,
Winson Chung5f8afe62013-08-12 16:19:28 -07001325 * cellY, spanX, spanY) are occupied. This is used when try to move a group of views.
Adam Cohen482ed822012-03-02 14:15:13 -08001326 * @param result Array in which to place the result, or null (in which case a new array will
1327 * be allocated)
1328 * @return The X, Y cell of a vacant area that can contain this object,
1329 * nearest the requested location.
1330 */
1331 private int[] findNearestArea(int cellX, int cellY, int spanX, int spanY, int[] direction,
Adam Cohen47a876d2012-03-19 13:21:41 -07001332 boolean[][] occupied, boolean blockOccupied[][], int[] result) {
Adam Cohen482ed822012-03-02 14:15:13 -08001333 // Keep track of best-scoring drop area
1334 final int[] bestXY = result != null ? result : new int[2];
1335 float bestDistance = Float.MAX_VALUE;
1336 int bestDirectionScore = Integer.MIN_VALUE;
1337
1338 final int countX = mCountX;
1339 final int countY = mCountY;
1340
1341 for (int y = 0; y < countY - (spanY - 1); y++) {
1342 inner:
1343 for (int x = 0; x < countX - (spanX - 1); x++) {
1344 // First, let's see if this thing fits anywhere
1345 for (int i = 0; i < spanX; i++) {
1346 for (int j = 0; j < spanY; j++) {
Adam Cohen47a876d2012-03-19 13:21:41 -07001347 if (occupied[x + i][y + j] && (blockOccupied == null || blockOccupied[i][j])) {
Adam Cohen482ed822012-03-02 14:15:13 -08001348 continue inner;
1349 }
1350 }
1351 }
1352
Sunny Goyalf7a29e82015-04-24 15:20:43 -07001353 float distance = (float) Math.hypot(x - cellX, y - cellY);
Adam Cohen482ed822012-03-02 14:15:13 -08001354 int[] curDirection = mTmpPoint;
Adam Cohen47a876d2012-03-19 13:21:41 -07001355 computeDirectionVector(x - cellX, y - cellY, curDirection);
1356 // The direction score is just the dot product of the two candidate direction
1357 // and that passed in.
Adam Cohen482ed822012-03-02 14:15:13 -08001358 int curDirectionScore = direction[0] * curDirection[0] +
1359 direction[1] * curDirection[1];
Adam Cohen47a876d2012-03-19 13:21:41 -07001360 boolean exactDirectionOnly = false;
1361 boolean directionMatches = direction[0] == curDirection[0] &&
1362 direction[0] == curDirection[0];
1363 if ((directionMatches || !exactDirectionOnly) &&
1364 Float.compare(distance, bestDistance) < 0 || (Float.compare(distance,
Adam Cohen482ed822012-03-02 14:15:13 -08001365 bestDistance) == 0 && curDirectionScore > bestDirectionScore)) {
1366 bestDistance = distance;
1367 bestDirectionScore = curDirectionScore;
1368 bestXY[0] = x;
1369 bestXY[1] = y;
1370 }
1371 }
1372 }
1373
1374 // Return -1, -1 if no suitable location found
1375 if (bestDistance == Float.MAX_VALUE) {
1376 bestXY[0] = -1;
1377 bestXY[1] = -1;
1378 }
1379 return bestXY;
1380 }
1381
1382 private boolean addViewToTempLocation(View v, Rect rectOccupiedByPotentialDrop,
Adam Cohen8baab352012-03-20 17:39:21 -07001383 int[] direction, ItemConfiguration currentState) {
1384 CellAndSpan c = currentState.map.get(v);
Adam Cohen482ed822012-03-02 14:15:13 -08001385 boolean success = false;
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001386 mTmpOccupied.markCells(c, false);
1387 mTmpOccupied.markCells(rectOccupiedByPotentialDrop, true);
Adam Cohen482ed822012-03-02 14:15:13 -08001388
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001389 findNearestArea(c.cellX, c.cellY, c.spanX, c.spanY, direction,
1390 mTmpOccupied.cells, null, mTempLocation);
Adam Cohen482ed822012-03-02 14:15:13 -08001391
1392 if (mTempLocation[0] >= 0 && mTempLocation[1] >= 0) {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001393 c.cellX = mTempLocation[0];
1394 c.cellY = mTempLocation[1];
Adam Cohen482ed822012-03-02 14:15:13 -08001395 success = true;
Adam Cohen482ed822012-03-02 14:15:13 -08001396 }
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001397 mTmpOccupied.markCells(c, true);
Adam Cohen482ed822012-03-02 14:15:13 -08001398 return success;
1399 }
1400
Adam Cohenf3900c22012-11-16 18:28:11 -08001401 /**
1402 * This helper class defines a cluster of views. It helps with defining complex edges
1403 * of the cluster and determining how those edges interact with other views. The edges
1404 * essentially define a fine-grained boundary around the cluster of views -- like a more
1405 * precise version of a bounding box.
1406 */
1407 private class ViewCluster {
Sunny Goyal6dc98b92016-04-03 15:25:29 -07001408 final static int LEFT = 1 << 0;
1409 final static int TOP = 1 << 1;
1410 final static int RIGHT = 1 << 2;
1411 final static int BOTTOM = 1 << 3;
Adam Cohen47a876d2012-03-19 13:21:41 -07001412
Adam Cohenf3900c22012-11-16 18:28:11 -08001413 ArrayList<View> views;
1414 ItemConfiguration config;
1415 Rect boundingRect = new Rect();
Adam Cohen47a876d2012-03-19 13:21:41 -07001416
Adam Cohenf3900c22012-11-16 18:28:11 -08001417 int[] leftEdge = new int[mCountY];
1418 int[] rightEdge = new int[mCountY];
1419 int[] topEdge = new int[mCountX];
1420 int[] bottomEdge = new int[mCountX];
Sunny Goyal6dc98b92016-04-03 15:25:29 -07001421 int dirtyEdges;
1422 boolean boundingRectDirty;
Adam Cohenf3900c22012-11-16 18:28:11 -08001423
1424 @SuppressWarnings("unchecked")
1425 public ViewCluster(ArrayList<View> views, ItemConfiguration config) {
1426 this.views = (ArrayList<View>) views.clone();
1427 this.config = config;
1428 resetEdges();
Adam Cohen47a876d2012-03-19 13:21:41 -07001429 }
1430
Adam Cohenf3900c22012-11-16 18:28:11 -08001431 void resetEdges() {
1432 for (int i = 0; i < mCountX; i++) {
1433 topEdge[i] = -1;
1434 bottomEdge[i] = -1;
1435 }
1436 for (int i = 0; i < mCountY; i++) {
1437 leftEdge[i] = -1;
1438 rightEdge[i] = -1;
1439 }
Sunny Goyal6dc98b92016-04-03 15:25:29 -07001440 dirtyEdges = LEFT | TOP | RIGHT | BOTTOM;
Adam Cohenf3900c22012-11-16 18:28:11 -08001441 boundingRectDirty = true;
1442 }
Adam Cohen47a876d2012-03-19 13:21:41 -07001443
Sunny Goyal6dc98b92016-04-03 15:25:29 -07001444 void computeEdge(int which) {
Adam Cohenf3900c22012-11-16 18:28:11 -08001445 int count = views.size();
1446 for (int i = 0; i < count; i++) {
1447 CellAndSpan cs = config.map.get(views.get(i));
1448 switch (which) {
1449 case LEFT:
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001450 int left = cs.cellX;
1451 for (int j = cs.cellY; j < cs.cellY + cs.spanY; j++) {
Sunny Goyal6dc98b92016-04-03 15:25:29 -07001452 if (left < leftEdge[j] || leftEdge[j] < 0) {
1453 leftEdge[j] = left;
Adam Cohena56dc102012-07-13 13:41:42 -07001454 }
Adam Cohen47a876d2012-03-19 13:21:41 -07001455 }
Adam Cohenf3900c22012-11-16 18:28:11 -08001456 break;
1457 case RIGHT:
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001458 int right = cs.cellX + cs.spanX;
1459 for (int j = cs.cellY; j < cs.cellY + cs.spanY; j++) {
Sunny Goyal6dc98b92016-04-03 15:25:29 -07001460 if (right > rightEdge[j]) {
1461 rightEdge[j] = right;
Adam Cohenf3900c22012-11-16 18:28:11 -08001462 }
1463 }
1464 break;
1465 case TOP:
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001466 int top = cs.cellY;
1467 for (int j = cs.cellX; j < cs.cellX + cs.spanX; j++) {
Sunny Goyal6dc98b92016-04-03 15:25:29 -07001468 if (top < topEdge[j] || topEdge[j] < 0) {
1469 topEdge[j] = top;
Adam Cohenf3900c22012-11-16 18:28:11 -08001470 }
1471 }
1472 break;
1473 case BOTTOM:
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001474 int bottom = cs.cellY + cs.spanY;
1475 for (int j = cs.cellX; j < cs.cellX + cs.spanX; j++) {
Sunny Goyal6dc98b92016-04-03 15:25:29 -07001476 if (bottom > bottomEdge[j]) {
1477 bottomEdge[j] = bottom;
Adam Cohenf3900c22012-11-16 18:28:11 -08001478 }
1479 }
1480 break;
Adam Cohen47a876d2012-03-19 13:21:41 -07001481 }
1482 }
1483 }
Adam Cohenf3900c22012-11-16 18:28:11 -08001484
1485 boolean isViewTouchingEdge(View v, int whichEdge) {
1486 CellAndSpan cs = config.map.get(v);
1487
Sunny Goyal6dc98b92016-04-03 15:25:29 -07001488 if ((dirtyEdges & whichEdge) == whichEdge) {
1489 computeEdge(whichEdge);
1490 dirtyEdges &= ~whichEdge;
1491 }
Adam Cohenf3900c22012-11-16 18:28:11 -08001492
1493 switch (whichEdge) {
1494 case LEFT:
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001495 for (int i = cs.cellY; i < cs.cellY + cs.spanY; i++) {
1496 if (leftEdge[i] == cs.cellX + cs.spanX) {
Adam Cohenf3900c22012-11-16 18:28:11 -08001497 return true;
1498 }
1499 }
1500 break;
1501 case RIGHT:
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001502 for (int i = cs.cellY; i < cs.cellY + cs.spanY; i++) {
1503 if (rightEdge[i] == cs.cellX) {
Adam Cohenf3900c22012-11-16 18:28:11 -08001504 return true;
1505 }
1506 }
1507 break;
1508 case TOP:
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001509 for (int i = cs.cellX; i < cs.cellX + cs.spanX; i++) {
1510 if (topEdge[i] == cs.cellY + cs.spanY) {
Adam Cohenf3900c22012-11-16 18:28:11 -08001511 return true;
1512 }
1513 }
1514 break;
1515 case BOTTOM:
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001516 for (int i = cs.cellX; i < cs.cellX + cs.spanX; i++) {
1517 if (bottomEdge[i] == cs.cellY) {
Adam Cohenf3900c22012-11-16 18:28:11 -08001518 return true;
1519 }
1520 }
1521 break;
1522 }
1523 return false;
1524 }
1525
1526 void shift(int whichEdge, int delta) {
1527 for (View v: views) {
1528 CellAndSpan c = config.map.get(v);
1529 switch (whichEdge) {
1530 case LEFT:
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001531 c.cellX -= delta;
Adam Cohenf3900c22012-11-16 18:28:11 -08001532 break;
1533 case RIGHT:
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001534 c.cellX += delta;
Adam Cohenf3900c22012-11-16 18:28:11 -08001535 break;
1536 case TOP:
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001537 c.cellY -= delta;
Adam Cohenf3900c22012-11-16 18:28:11 -08001538 break;
1539 case BOTTOM:
1540 default:
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001541 c.cellY += delta;
Adam Cohenf3900c22012-11-16 18:28:11 -08001542 break;
1543 }
1544 }
1545 resetEdges();
1546 }
1547
1548 public void addView(View v) {
1549 views.add(v);
1550 resetEdges();
1551 }
1552
1553 public Rect getBoundingRect() {
1554 if (boundingRectDirty) {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001555 config.getBoundingRectForViews(views, boundingRect);
Adam Cohenf3900c22012-11-16 18:28:11 -08001556 }
1557 return boundingRect;
1558 }
1559
Adam Cohenf3900c22012-11-16 18:28:11 -08001560 PositionComparator comparator = new PositionComparator();
1561 class PositionComparator implements Comparator<View> {
1562 int whichEdge = 0;
1563 public int compare(View left, View right) {
1564 CellAndSpan l = config.map.get(left);
1565 CellAndSpan r = config.map.get(right);
1566 switch (whichEdge) {
1567 case LEFT:
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001568 return (r.cellX + r.spanX) - (l.cellX + l.spanX);
Adam Cohenf3900c22012-11-16 18:28:11 -08001569 case RIGHT:
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001570 return l.cellX - r.cellX;
Adam Cohenf3900c22012-11-16 18:28:11 -08001571 case TOP:
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001572 return (r.cellY + r.spanY) - (l.cellY + l.spanY);
Adam Cohenf3900c22012-11-16 18:28:11 -08001573 case BOTTOM:
1574 default:
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001575 return l.cellY - r.cellY;
Adam Cohenf3900c22012-11-16 18:28:11 -08001576 }
1577 }
1578 }
1579
1580 public void sortConfigurationForEdgePush(int edge) {
1581 comparator.whichEdge = edge;
1582 Collections.sort(config.sortedViews, comparator);
1583 }
Adam Cohen47a876d2012-03-19 13:21:41 -07001584 }
1585
Adam Cohenf3900c22012-11-16 18:28:11 -08001586 private boolean pushViewsToTempLocation(ArrayList<View> views, Rect rectOccupiedByPotentialDrop,
1587 int[] direction, View dragView, ItemConfiguration currentState) {
Adam Cohene0489502012-08-27 15:18:53 -07001588
Adam Cohenf3900c22012-11-16 18:28:11 -08001589 ViewCluster cluster = new ViewCluster(views, currentState);
1590 Rect clusterRect = cluster.getBoundingRect();
1591 int whichEdge;
1592 int pushDistance;
1593 boolean fail = false;
1594
1595 // Determine the edge of the cluster that will be leading the push and how far
1596 // the cluster must be shifted.
1597 if (direction[0] < 0) {
1598 whichEdge = ViewCluster.LEFT;
1599 pushDistance = clusterRect.right - rectOccupiedByPotentialDrop.left;
Adam Cohene0489502012-08-27 15:18:53 -07001600 } else if (direction[0] > 0) {
Adam Cohenf3900c22012-11-16 18:28:11 -08001601 whichEdge = ViewCluster.RIGHT;
1602 pushDistance = rectOccupiedByPotentialDrop.right - clusterRect.left;
1603 } else if (direction[1] < 0) {
1604 whichEdge = ViewCluster.TOP;
1605 pushDistance = clusterRect.bottom - rectOccupiedByPotentialDrop.top;
1606 } else {
1607 whichEdge = ViewCluster.BOTTOM;
1608 pushDistance = rectOccupiedByPotentialDrop.bottom - clusterRect.top;
Adam Cohene0489502012-08-27 15:18:53 -07001609 }
1610
Adam Cohenf3900c22012-11-16 18:28:11 -08001611 // Break early for invalid push distance.
1612 if (pushDistance <= 0) {
1613 return false;
Adam Cohene0489502012-08-27 15:18:53 -07001614 }
Adam Cohenf3900c22012-11-16 18:28:11 -08001615
1616 // Mark the occupied state as false for the group of views we want to move.
1617 for (View v: views) {
1618 CellAndSpan c = currentState.map.get(v);
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001619 mTmpOccupied.markCells(c, false);
Adam Cohenf3900c22012-11-16 18:28:11 -08001620 }
1621
1622 // We save the current configuration -- if we fail to find a solution we will revert
1623 // to the initial state. The process of finding a solution modifies the configuration
1624 // in place, hence the need for revert in the failure case.
1625 currentState.save();
1626
1627 // The pushing algorithm is simplified by considering the views in the order in which
1628 // they would be pushed by the cluster. For example, if the cluster is leading with its
1629 // left edge, we consider sort the views by their right edge, from right to left.
1630 cluster.sortConfigurationForEdgePush(whichEdge);
1631
1632 while (pushDistance > 0 && !fail) {
1633 for (View v: currentState.sortedViews) {
1634 // For each view that isn't in the cluster, we see if the leading edge of the
1635 // cluster is contacting the edge of that view. If so, we add that view to the
1636 // cluster.
1637 if (!cluster.views.contains(v) && v != dragView) {
1638 if (cluster.isViewTouchingEdge(v, whichEdge)) {
1639 LayoutParams lp = (LayoutParams) v.getLayoutParams();
1640 if (!lp.canReorder) {
1641 // The push solution includes the all apps button, this is not viable.
1642 fail = true;
1643 break;
1644 }
1645 cluster.addView(v);
1646 CellAndSpan c = currentState.map.get(v);
1647
1648 // Adding view to cluster, mark it as not occupied.
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001649 mTmpOccupied.markCells(c, false);
Adam Cohenf3900c22012-11-16 18:28:11 -08001650 }
1651 }
1652 }
1653 pushDistance--;
1654
1655 // The cluster has been completed, now we move the whole thing over in the appropriate
1656 // direction.
1657 cluster.shift(whichEdge, 1);
1658 }
1659
1660 boolean foundSolution = false;
1661 clusterRect = cluster.getBoundingRect();
1662
1663 // Due to the nature of the algorithm, the only check required to verify a valid solution
1664 // is to ensure that completed shifted cluster lies completely within the cell layout.
1665 if (!fail && clusterRect.left >= 0 && clusterRect.right <= mCountX && clusterRect.top >= 0 &&
1666 clusterRect.bottom <= mCountY) {
1667 foundSolution = true;
1668 } else {
1669 currentState.restore();
1670 }
1671
1672 // In either case, we set the occupied array as marked for the location of the views
1673 for (View v: cluster.views) {
1674 CellAndSpan c = currentState.map.get(v);
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001675 mTmpOccupied.markCells(c, true);
Adam Cohenf3900c22012-11-16 18:28:11 -08001676 }
1677
1678 return foundSolution;
Adam Cohene0489502012-08-27 15:18:53 -07001679 }
1680
Adam Cohen482ed822012-03-02 14:15:13 -08001681 private boolean addViewsToTempLocation(ArrayList<View> views, Rect rectOccupiedByPotentialDrop,
Adam Cohenf3900c22012-11-16 18:28:11 -08001682 int[] direction, View dragView, ItemConfiguration currentState) {
Adam Cohen482ed822012-03-02 14:15:13 -08001683 if (views.size() == 0) return true;
Adam Cohen482ed822012-03-02 14:15:13 -08001684
Adam Cohen8baab352012-03-20 17:39:21 -07001685 boolean success = false;
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001686 Rect boundingRect = new Rect();
Adam Cohen8baab352012-03-20 17:39:21 -07001687 // We construct a rect which represents the entire group of views passed in
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001688 currentState.getBoundingRectForViews(views, boundingRect);
Adam Cohen8baab352012-03-20 17:39:21 -07001689
Adam Cohen8baab352012-03-20 17:39:21 -07001690 // Mark the occupied state as false for the group of views we want to move.
Adam Cohenf3900c22012-11-16 18:28:11 -08001691 for (View v: views) {
Adam Cohen8baab352012-03-20 17:39:21 -07001692 CellAndSpan c = currentState.map.get(v);
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001693 mTmpOccupied.markCells(c, false);
Adam Cohen8baab352012-03-20 17:39:21 -07001694 }
1695
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001696 GridOccupancy blockOccupied = new GridOccupancy(boundingRect.width(), boundingRect.height());
Adam Cohen47a876d2012-03-19 13:21:41 -07001697 int top = boundingRect.top;
1698 int left = boundingRect.left;
Adam Cohen8baab352012-03-20 17:39:21 -07001699 // We mark more precisely which parts of the bounding rect are truly occupied, allowing
Adam Cohena56dc102012-07-13 13:41:42 -07001700 // for interlocking.
Adam Cohenf3900c22012-11-16 18:28:11 -08001701 for (View v: views) {
Adam Cohen8baab352012-03-20 17:39:21 -07001702 CellAndSpan c = currentState.map.get(v);
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001703 blockOccupied.markCells(c.cellX - left, c.cellY - top, c.spanX, c.spanY, true);
Adam Cohen47a876d2012-03-19 13:21:41 -07001704 }
1705
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001706 mTmpOccupied.markCells(rectOccupiedByPotentialDrop, true);
Adam Cohen482ed822012-03-02 14:15:13 -08001707
Adam Cohenf3900c22012-11-16 18:28:11 -08001708 findNearestArea(boundingRect.left, boundingRect.top, boundingRect.width(),
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001709 boundingRect.height(), direction,
1710 mTmpOccupied.cells, blockOccupied.cells, mTempLocation);
Adam Cohen482ed822012-03-02 14:15:13 -08001711
Adam Cohen8baab352012-03-20 17:39:21 -07001712 // If we successfuly found a location by pushing the block of views, we commit it
Adam Cohen482ed822012-03-02 14:15:13 -08001713 if (mTempLocation[0] >= 0 && mTempLocation[1] >= 0) {
Adam Cohen8baab352012-03-20 17:39:21 -07001714 int deltaX = mTempLocation[0] - boundingRect.left;
1715 int deltaY = mTempLocation[1] - boundingRect.top;
Adam Cohenf3900c22012-11-16 18:28:11 -08001716 for (View v: views) {
Adam Cohen8baab352012-03-20 17:39:21 -07001717 CellAndSpan c = currentState.map.get(v);
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001718 c.cellX += deltaX;
1719 c.cellY += deltaY;
Adam Cohen482ed822012-03-02 14:15:13 -08001720 }
1721 success = true;
1722 }
Adam Cohen8baab352012-03-20 17:39:21 -07001723
1724 // In either case, we set the occupied array as marked for the location of the views
Adam Cohenf3900c22012-11-16 18:28:11 -08001725 for (View v: views) {
Adam Cohen8baab352012-03-20 17:39:21 -07001726 CellAndSpan c = currentState.map.get(v);
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001727 mTmpOccupied.markCells(c, true);
Adam Cohen482ed822012-03-02 14:15:13 -08001728 }
1729 return success;
1730 }
1731
Adam Cohen4abc5bd2012-05-29 21:06:03 -07001732 // This method tries to find a reordering solution which satisfies the push mechanic by trying
1733 // to push items in each of the cardinal directions, in an order based on the direction vector
1734 // passed.
1735 private boolean attemptPushInDirection(ArrayList<View> intersectingViews, Rect occupied,
1736 int[] direction, View ignoreView, ItemConfiguration solution) {
1737 if ((Math.abs(direction[0]) + Math.abs(direction[1])) > 1) {
Winson Chung5f8afe62013-08-12 16:19:28 -07001738 // If the direction vector has two non-zero components, we try pushing
Adam Cohen4abc5bd2012-05-29 21:06:03 -07001739 // separately in each of the components.
1740 int temp = direction[1];
1741 direction[1] = 0;
Adam Cohenf3900c22012-11-16 18:28:11 -08001742
1743 if (pushViewsToTempLocation(intersectingViews, occupied, direction,
Adam Cohen4abc5bd2012-05-29 21:06:03 -07001744 ignoreView, solution)) {
1745 return true;
1746 }
1747 direction[1] = temp;
1748 temp = direction[0];
1749 direction[0] = 0;
Adam Cohenf3900c22012-11-16 18:28:11 -08001750
1751 if (pushViewsToTempLocation(intersectingViews, occupied, direction,
Adam Cohen4abc5bd2012-05-29 21:06:03 -07001752 ignoreView, solution)) {
1753 return true;
1754 }
1755 // Revert the direction
1756 direction[0] = temp;
1757
1758 // Now we try pushing in each component of the opposite direction
1759 direction[0] *= -1;
1760 direction[1] *= -1;
1761 temp = direction[1];
1762 direction[1] = 0;
Adam Cohenf3900c22012-11-16 18:28:11 -08001763 if (pushViewsToTempLocation(intersectingViews, occupied, direction,
Adam Cohen4abc5bd2012-05-29 21:06:03 -07001764 ignoreView, solution)) {
1765 return true;
1766 }
1767
1768 direction[1] = temp;
1769 temp = direction[0];
1770 direction[0] = 0;
Adam Cohenf3900c22012-11-16 18:28:11 -08001771 if (pushViewsToTempLocation(intersectingViews, occupied, direction,
Adam Cohen4abc5bd2012-05-29 21:06:03 -07001772 ignoreView, solution)) {
1773 return true;
1774 }
1775 // revert the direction
1776 direction[0] = temp;
1777 direction[0] *= -1;
1778 direction[1] *= -1;
Winson Chung5f8afe62013-08-12 16:19:28 -07001779
Adam Cohen4abc5bd2012-05-29 21:06:03 -07001780 } else {
1781 // If the direction vector has a single non-zero component, we push first in the
1782 // direction of the vector
Adam Cohenf3900c22012-11-16 18:28:11 -08001783 if (pushViewsToTempLocation(intersectingViews, occupied, direction,
Adam Cohen4abc5bd2012-05-29 21:06:03 -07001784 ignoreView, solution)) {
1785 return true;
1786 }
Adam Cohen4abc5bd2012-05-29 21:06:03 -07001787 // Then we try the opposite direction
1788 direction[0] *= -1;
1789 direction[1] *= -1;
Adam Cohenf3900c22012-11-16 18:28:11 -08001790 if (pushViewsToTempLocation(intersectingViews, occupied, direction,
Adam Cohen4abc5bd2012-05-29 21:06:03 -07001791 ignoreView, solution)) {
1792 return true;
1793 }
1794 // Switch the direction back
1795 direction[0] *= -1;
1796 direction[1] *= -1;
Winson Chung5f8afe62013-08-12 16:19:28 -07001797
1798 // If we have failed to find a push solution with the above, then we try
Adam Cohen4abc5bd2012-05-29 21:06:03 -07001799 // to find a solution by pushing along the perpendicular axis.
1800
1801 // Swap the components
1802 int temp = direction[1];
1803 direction[1] = direction[0];
1804 direction[0] = temp;
Adam Cohenf3900c22012-11-16 18:28:11 -08001805 if (pushViewsToTempLocation(intersectingViews, occupied, direction,
Adam Cohen4abc5bd2012-05-29 21:06:03 -07001806 ignoreView, solution)) {
1807 return true;
1808 }
1809
1810 // Then we try the opposite direction
1811 direction[0] *= -1;
1812 direction[1] *= -1;
Adam Cohenf3900c22012-11-16 18:28:11 -08001813 if (pushViewsToTempLocation(intersectingViews, occupied, direction,
Adam Cohen4abc5bd2012-05-29 21:06:03 -07001814 ignoreView, solution)) {
1815 return true;
1816 }
1817 // Switch the direction back
1818 direction[0] *= -1;
1819 direction[1] *= -1;
1820
1821 // Swap the components back
1822 temp = direction[1];
1823 direction[1] = direction[0];
1824 direction[0] = temp;
1825 }
1826 return false;
1827 }
1828
Adam Cohen482ed822012-03-02 14:15:13 -08001829 private boolean rearrangementExists(int cellX, int cellY, int spanX, int spanY, int[] direction,
Adam Cohen8baab352012-03-20 17:39:21 -07001830 View ignoreView, ItemConfiguration solution) {
Winson Chunge3e03bc2012-05-01 15:10:11 -07001831 // Return early if get invalid cell positions
1832 if (cellX < 0 || cellY < 0) return false;
Adam Cohen482ed822012-03-02 14:15:13 -08001833
Adam Cohen8baab352012-03-20 17:39:21 -07001834 mIntersectingViews.clear();
Adam Cohen482ed822012-03-02 14:15:13 -08001835 mOccupiedRect.set(cellX, cellY, cellX + spanX, cellY + spanY);
Adam Cohen482ed822012-03-02 14:15:13 -08001836
Adam Cohen8baab352012-03-20 17:39:21 -07001837 // Mark the desired location of the view currently being dragged.
Adam Cohen482ed822012-03-02 14:15:13 -08001838 if (ignoreView != null) {
Adam Cohen8baab352012-03-20 17:39:21 -07001839 CellAndSpan c = solution.map.get(ignoreView);
Adam Cohen19f37922012-03-21 11:59:11 -07001840 if (c != null) {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001841 c.cellX = cellX;
1842 c.cellY = cellY;
Adam Cohen19f37922012-03-21 11:59:11 -07001843 }
Adam Cohen482ed822012-03-02 14:15:13 -08001844 }
Adam Cohen482ed822012-03-02 14:15:13 -08001845 Rect r0 = new Rect(cellX, cellY, cellX + spanX, cellY + spanY);
1846 Rect r1 = new Rect();
Adam Cohen8baab352012-03-20 17:39:21 -07001847 for (View child: solution.map.keySet()) {
Adam Cohen482ed822012-03-02 14:15:13 -08001848 if (child == ignoreView) continue;
Adam Cohen8baab352012-03-20 17:39:21 -07001849 CellAndSpan c = solution.map.get(child);
Adam Cohen482ed822012-03-02 14:15:13 -08001850 LayoutParams lp = (LayoutParams) child.getLayoutParams();
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001851 r1.set(c.cellX, c.cellY, c.cellX + c.spanX, c.cellY + c.spanY);
Adam Cohen482ed822012-03-02 14:15:13 -08001852 if (Rect.intersects(r0, r1)) {
1853 if (!lp.canReorder) {
1854 return false;
1855 }
1856 mIntersectingViews.add(child);
1857 }
1858 }
Adam Cohen47a876d2012-03-19 13:21:41 -07001859
Adam Cohenfa3c58f2013-12-06 16:10:55 -08001860 solution.intersectingViews = new ArrayList<View>(mIntersectingViews);
1861
Winson Chung5f8afe62013-08-12 16:19:28 -07001862 // First we try to find a solution which respects the push mechanic. That is,
Adam Cohen4abc5bd2012-05-29 21:06:03 -07001863 // we try to find a solution such that no displaced item travels through another item
1864 // without also displacing that item.
1865 if (attemptPushInDirection(mIntersectingViews, mOccupiedRect, direction, ignoreView,
Adam Cohen19f37922012-03-21 11:59:11 -07001866 solution)) {
Adam Cohen47a876d2012-03-19 13:21:41 -07001867 return true;
1868 }
Adam Cohen47a876d2012-03-19 13:21:41 -07001869
Adam Cohen4abc5bd2012-05-29 21:06:03 -07001870 // Next we try moving the views as a block, but without requiring the push mechanic.
Adam Cohenf3900c22012-11-16 18:28:11 -08001871 if (addViewsToTempLocation(mIntersectingViews, mOccupiedRect, direction, ignoreView,
Adam Cohen19f37922012-03-21 11:59:11 -07001872 solution)) {
Adam Cohen482ed822012-03-02 14:15:13 -08001873 return true;
1874 }
Adam Cohen47a876d2012-03-19 13:21:41 -07001875
Adam Cohen482ed822012-03-02 14:15:13 -08001876 // Ok, they couldn't move as a block, let's move them individually
1877 for (View v : mIntersectingViews) {
Adam Cohen8baab352012-03-20 17:39:21 -07001878 if (!addViewToTempLocation(v, mOccupiedRect, direction, solution)) {
Adam Cohen482ed822012-03-02 14:15:13 -08001879 return false;
1880 }
1881 }
1882 return true;
1883 }
1884
1885 /*
1886 * Returns a pair (x, y), where x,y are in {-1, 0, 1} corresponding to vector between
1887 * the provided point and the provided cell
1888 */
Adam Cohen47a876d2012-03-19 13:21:41 -07001889 private void computeDirectionVector(float deltaX, float deltaY, int[] result) {
Adam Cohen482ed822012-03-02 14:15:13 -08001890 double angle = Math.atan(((float) deltaY) / deltaX);
1891
1892 result[0] = 0;
1893 result[1] = 0;
1894 if (Math.abs(Math.cos(angle)) > 0.5f) {
1895 result[0] = (int) Math.signum(deltaX);
1896 }
1897 if (Math.abs(Math.sin(angle)) > 0.5f) {
1898 result[1] = (int) Math.signum(deltaY);
1899 }
1900 }
1901
Sunny Goyalf7a29e82015-04-24 15:20:43 -07001902 private ItemConfiguration findReorderSolution(int pixelX, int pixelY, int minSpanX, int minSpanY,
Adam Cohenfa3c58f2013-12-06 16:10:55 -08001903 int spanX, int spanY, int[] direction, View dragView, boolean decX,
1904 ItemConfiguration solution) {
Adam Cohen8baab352012-03-20 17:39:21 -07001905 // Copy the current state into the solution. This solution will be manipulated as necessary.
1906 copyCurrentStateToSolution(solution, false);
1907 // Copy the current occupied array into the temporary occupied array. This array will be
1908 // manipulated as necessary to find a solution.
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001909 mOccupied.copyTo(mTmpOccupied);
Adam Cohen482ed822012-03-02 14:15:13 -08001910
1911 // We find the nearest cell into which we would place the dragged item, assuming there's
1912 // nothing in its way.
1913 int result[] = new int[2];
1914 result = findNearestArea(pixelX, pixelY, spanX, spanY, result);
1915
1916 boolean success = false;
1917 // First we try the exact nearest position of the item being dragged,
1918 // we will then want to try to move this around to other neighbouring positions
Adam Cohen8baab352012-03-20 17:39:21 -07001919 success = rearrangementExists(result[0], result[1], spanX, spanY, direction, dragView,
1920 solution);
Adam Cohen482ed822012-03-02 14:15:13 -08001921
1922 if (!success) {
1923 // We try shrinking the widget down to size in an alternating pattern, shrink 1 in
1924 // x, then 1 in y etc.
1925 if (spanX > minSpanX && (minSpanY == spanY || decX)) {
Adam Cohenfa3c58f2013-12-06 16:10:55 -08001926 return findReorderSolution(pixelX, pixelY, minSpanX, minSpanY, spanX - 1, spanY,
1927 direction, dragView, false, solution);
Adam Cohen482ed822012-03-02 14:15:13 -08001928 } else if (spanY > minSpanY) {
Adam Cohenfa3c58f2013-12-06 16:10:55 -08001929 return findReorderSolution(pixelX, pixelY, minSpanX, minSpanY, spanX, spanY - 1,
1930 direction, dragView, true, solution);
Adam Cohen482ed822012-03-02 14:15:13 -08001931 }
1932 solution.isSolution = false;
1933 } else {
1934 solution.isSolution = true;
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001935 solution.cellX = result[0];
1936 solution.cellY = result[1];
1937 solution.spanX = spanX;
1938 solution.spanY = spanY;
Adam Cohen482ed822012-03-02 14:15:13 -08001939 }
1940 return solution;
1941 }
1942
1943 private void copyCurrentStateToSolution(ItemConfiguration solution, boolean temp) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07001944 int childCount = mShortcutsAndWidgets.getChildCount();
Adam Cohen482ed822012-03-02 14:15:13 -08001945 for (int i = 0; i < childCount; i++) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07001946 View child = mShortcutsAndWidgets.getChildAt(i);
Adam Cohen482ed822012-03-02 14:15:13 -08001947 LayoutParams lp = (LayoutParams) child.getLayoutParams();
Adam Cohen8baab352012-03-20 17:39:21 -07001948 CellAndSpan c;
Adam Cohen482ed822012-03-02 14:15:13 -08001949 if (temp) {
Adam Cohen8baab352012-03-20 17:39:21 -07001950 c = new CellAndSpan(lp.tmpCellX, lp.tmpCellY, lp.cellHSpan, lp.cellVSpan);
Adam Cohen482ed822012-03-02 14:15:13 -08001951 } else {
Adam Cohen8baab352012-03-20 17:39:21 -07001952 c = new CellAndSpan(lp.cellX, lp.cellY, lp.cellHSpan, lp.cellVSpan);
Adam Cohen482ed822012-03-02 14:15:13 -08001953 }
Adam Cohenf3900c22012-11-16 18:28:11 -08001954 solution.add(child, c);
Adam Cohen482ed822012-03-02 14:15:13 -08001955 }
1956 }
1957
1958 private void copySolutionToTempState(ItemConfiguration solution, View dragView) {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001959 mTmpOccupied.clear();
Adam Cohen482ed822012-03-02 14:15:13 -08001960
Michael Jurkaa52570f2012-03-20 03:18:20 -07001961 int childCount = mShortcutsAndWidgets.getChildCount();
Adam Cohen482ed822012-03-02 14:15:13 -08001962 for (int i = 0; i < childCount; i++) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07001963 View child = mShortcutsAndWidgets.getChildAt(i);
Adam Cohen482ed822012-03-02 14:15:13 -08001964 if (child == dragView) continue;
1965 LayoutParams lp = (LayoutParams) child.getLayoutParams();
Adam Cohen8baab352012-03-20 17:39:21 -07001966 CellAndSpan c = solution.map.get(child);
1967 if (c != null) {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001968 lp.tmpCellX = c.cellX;
1969 lp.tmpCellY = c.cellY;
Adam Cohen8baab352012-03-20 17:39:21 -07001970 lp.cellHSpan = c.spanX;
1971 lp.cellVSpan = c.spanY;
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001972 mTmpOccupied.markCells(c, true);
Adam Cohen482ed822012-03-02 14:15:13 -08001973 }
1974 }
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001975 mTmpOccupied.markCells(solution, true);
Adam Cohen482ed822012-03-02 14:15:13 -08001976 }
1977
1978 private void animateItemsToSolution(ItemConfiguration solution, View dragView, boolean
1979 commitDragView) {
1980
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001981 GridOccupancy occupied = DESTRUCTIVE_REORDER ? mOccupied : mTmpOccupied;
1982 occupied.clear();
Adam Cohen482ed822012-03-02 14:15:13 -08001983
Michael Jurkaa52570f2012-03-20 03:18:20 -07001984 int childCount = mShortcutsAndWidgets.getChildCount();
Adam Cohen482ed822012-03-02 14:15:13 -08001985 for (int i = 0; i < childCount; i++) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07001986 View child = mShortcutsAndWidgets.getChildAt(i);
Adam Cohen482ed822012-03-02 14:15:13 -08001987 if (child == dragView) continue;
Adam Cohen8baab352012-03-20 17:39:21 -07001988 CellAndSpan c = solution.map.get(child);
1989 if (c != null) {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001990 animateChildToPosition(child, c.cellX, c.cellY, REORDER_ANIMATION_DURATION, 0,
Adam Cohen19f37922012-03-21 11:59:11 -07001991 DESTRUCTIVE_REORDER, false);
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001992 occupied.markCells(c, true);
Adam Cohen482ed822012-03-02 14:15:13 -08001993 }
1994 }
1995 if (commitDragView) {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001996 occupied.markCells(solution, true);
Adam Cohen482ed822012-03-02 14:15:13 -08001997 }
1998 }
1999
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002000
2001 // This method starts or changes the reorder preview animations
2002 private void beginOrAdjustReorderPreviewAnimations(ItemConfiguration solution,
2003 View dragView, int delay, int mode) {
Adam Cohen19f37922012-03-21 11:59:11 -07002004 int childCount = mShortcutsAndWidgets.getChildCount();
Adam Cohen19f37922012-03-21 11:59:11 -07002005 for (int i = 0; i < childCount; i++) {
2006 View child = mShortcutsAndWidgets.getChildAt(i);
2007 if (child == dragView) continue;
2008 CellAndSpan c = solution.map.get(child);
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002009 boolean skip = mode == ReorderPreviewAnimation.MODE_HINT && solution.intersectingViews
2010 != null && !solution.intersectingViews.contains(child);
2011
Adam Cohen19f37922012-03-21 11:59:11 -07002012 LayoutParams lp = (LayoutParams) child.getLayoutParams();
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002013 if (c != null && !skip) {
2014 ReorderPreviewAnimation rha = new ReorderPreviewAnimation(child, mode, lp.cellX,
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07002015 lp.cellY, c.cellX, c.cellY, c.spanX, c.spanY);
Adam Cohend024f982012-05-23 18:26:45 -07002016 rha.animate();
Adam Cohen19f37922012-03-21 11:59:11 -07002017 }
2018 }
2019 }
2020
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002021 // Class which represents the reorder preview animations. These animations show that an item is
Adam Cohen19f37922012-03-21 11:59:11 -07002022 // in a temporary state, and hint at where the item will return to.
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002023 class ReorderPreviewAnimation {
Adam Cohen19f37922012-03-21 11:59:11 -07002024 View child;
Adam Cohend024f982012-05-23 18:26:45 -07002025 float finalDeltaX;
2026 float finalDeltaY;
2027 float initDeltaX;
2028 float initDeltaY;
2029 float finalScale;
2030 float initScale;
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002031 int mode;
2032 boolean repeating = false;
2033 private static final int PREVIEW_DURATION = 300;
2034 private static final int HINT_DURATION = Workspace.REORDER_TIMEOUT;
2035
2036 public static final int MODE_HINT = 0;
2037 public static final int MODE_PREVIEW = 1;
2038
Adam Cohene7587d22012-05-24 18:50:02 -07002039 Animator a;
Adam Cohen19f37922012-03-21 11:59:11 -07002040
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002041 public ReorderPreviewAnimation(View child, int mode, int cellX0, int cellY0, int cellX1,
2042 int cellY1, int spanX, int spanY) {
Adam Cohen19f37922012-03-21 11:59:11 -07002043 regionToCenterPoint(cellX0, cellY0, spanX, spanY, mTmpPoint);
2044 final int x0 = mTmpPoint[0];
2045 final int y0 = mTmpPoint[1];
2046 regionToCenterPoint(cellX1, cellY1, spanX, spanY, mTmpPoint);
2047 final int x1 = mTmpPoint[0];
2048 final int y1 = mTmpPoint[1];
2049 final int dX = x1 - x0;
2050 final int dY = y1 - y0;
Adam Cohend024f982012-05-23 18:26:45 -07002051 finalDeltaX = 0;
2052 finalDeltaY = 0;
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002053 int dir = mode == MODE_HINT ? -1 : 1;
Adam Cohen19f37922012-03-21 11:59:11 -07002054 if (dX == dY && dX == 0) {
2055 } else {
2056 if (dY == 0) {
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002057 finalDeltaX = - dir * Math.signum(dX) * mReorderPreviewAnimationMagnitude;
Adam Cohen19f37922012-03-21 11:59:11 -07002058 } else if (dX == 0) {
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002059 finalDeltaY = - dir * Math.signum(dY) * mReorderPreviewAnimationMagnitude;
Adam Cohen19f37922012-03-21 11:59:11 -07002060 } else {
2061 double angle = Math.atan( (float) (dY) / dX);
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002062 finalDeltaX = (int) (- dir * Math.signum(dX) *
2063 Math.abs(Math.cos(angle) * mReorderPreviewAnimationMagnitude));
2064 finalDeltaY = (int) (- dir * Math.signum(dY) *
2065 Math.abs(Math.sin(angle) * mReorderPreviewAnimationMagnitude));
Adam Cohen19f37922012-03-21 11:59:11 -07002066 }
2067 }
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002068 this.mode = mode;
Adam Cohend024f982012-05-23 18:26:45 -07002069 initDeltaX = child.getTranslationX();
2070 initDeltaY = child.getTranslationY();
Adam Cohen307fe232012-08-16 17:55:58 -07002071 finalScale = getChildrenScale() - 4.0f / child.getWidth();
Adam Cohend024f982012-05-23 18:26:45 -07002072 initScale = child.getScaleX();
Adam Cohen19f37922012-03-21 11:59:11 -07002073 this.child = child;
2074 }
2075
Adam Cohend024f982012-05-23 18:26:45 -07002076 void animate() {
Adam Cohen19f37922012-03-21 11:59:11 -07002077 if (mShakeAnimators.containsKey(child)) {
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002078 ReorderPreviewAnimation oldAnimation = mShakeAnimators.get(child);
Adam Cohend024f982012-05-23 18:26:45 -07002079 oldAnimation.cancel();
Adam Cohen19f37922012-03-21 11:59:11 -07002080 mShakeAnimators.remove(child);
Adam Cohene7587d22012-05-24 18:50:02 -07002081 if (finalDeltaX == 0 && finalDeltaY == 0) {
2082 completeAnimationImmediately();
2083 return;
2084 }
Adam Cohen19f37922012-03-21 11:59:11 -07002085 }
Adam Cohend024f982012-05-23 18:26:45 -07002086 if (finalDeltaX == 0 && finalDeltaY == 0) {
Adam Cohen19f37922012-03-21 11:59:11 -07002087 return;
2088 }
Michael Jurkaf1ad6082013-03-13 12:55:46 +01002089 ValueAnimator va = LauncherAnimUtils.ofFloat(child, 0f, 1f);
Adam Cohene7587d22012-05-24 18:50:02 -07002090 a = va;
Tony Wickham9e0702f2015-09-02 14:45:39 -07002091
2092 // Animations are disabled in power save mode, causing the repeated animation to jump
2093 // spastically between beginning and end states. Since this looks bad, we don't repeat
2094 // the animation in power save mode.
Tony Wickham112ac952015-11-12 12:31:50 -08002095 if (!Utilities.isPowerSaverOn(getContext())) {
Tony Wickham9e0702f2015-09-02 14:45:39 -07002096 va.setRepeatMode(ValueAnimator.REVERSE);
2097 va.setRepeatCount(ValueAnimator.INFINITE);
2098 }
2099
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002100 va.setDuration(mode == MODE_HINT ? HINT_DURATION : PREVIEW_DURATION);
Adam Cohend024f982012-05-23 18:26:45 -07002101 va.setStartDelay((int) (Math.random() * 60));
Adam Cohen19f37922012-03-21 11:59:11 -07002102 va.addUpdateListener(new AnimatorUpdateListener() {
2103 @Override
2104 public void onAnimationUpdate(ValueAnimator animation) {
2105 float r = ((Float) animation.getAnimatedValue()).floatValue();
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002106 float r1 = (mode == MODE_HINT && repeating) ? 1.0f : r;
2107 float x = r1 * finalDeltaX + (1 - r1) * initDeltaX;
2108 float y = r1 * finalDeltaY + (1 - r1) * initDeltaY;
Adam Cohen19f37922012-03-21 11:59:11 -07002109 child.setTranslationX(x);
2110 child.setTranslationY(y);
Adam Cohend024f982012-05-23 18:26:45 -07002111 float s = r * finalScale + (1 - r) * initScale;
Brandon Keely50e6e562012-05-08 16:28:49 -07002112 child.setScaleX(s);
2113 child.setScaleY(s);
Adam Cohen19f37922012-03-21 11:59:11 -07002114 }
2115 });
2116 va.addListener(new AnimatorListenerAdapter() {
2117 public void onAnimationRepeat(Animator animation) {
Adam Cohen19f37922012-03-21 11:59:11 -07002118 // We make sure to end only after a full period
Adam Cohend024f982012-05-23 18:26:45 -07002119 initDeltaX = 0;
2120 initDeltaY = 0;
Adam Cohen307fe232012-08-16 17:55:58 -07002121 initScale = getChildrenScale();
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002122 repeating = true;
Adam Cohen19f37922012-03-21 11:59:11 -07002123 }
2124 });
Adam Cohen19f37922012-03-21 11:59:11 -07002125 mShakeAnimators.put(child, this);
2126 va.start();
2127 }
2128
Adam Cohend024f982012-05-23 18:26:45 -07002129 private void cancel() {
Adam Cohene7587d22012-05-24 18:50:02 -07002130 if (a != null) {
2131 a.cancel();
2132 }
Adam Cohen19f37922012-03-21 11:59:11 -07002133 }
Adam Cohene7587d22012-05-24 18:50:02 -07002134
Adam Cohen091440a2015-03-18 14:16:05 -07002135 @Thunk void completeAnimationImmediately() {
Adam Cohene7587d22012-05-24 18:50:02 -07002136 if (a != null) {
2137 a.cancel();
2138 }
Brandon Keely50e6e562012-05-08 16:28:49 -07002139
Sunny Goyal5d2fc322015-07-06 22:52:49 -07002140 a = new LauncherViewPropertyAnimator(child)
2141 .scaleX(getChildrenScale())
2142 .scaleY(getChildrenScale())
2143 .translationX(0)
2144 .translationY(0)
2145 .setDuration(REORDER_ANIMATION_DURATION);
2146 a.setInterpolator(new android.view.animation.DecelerateInterpolator(1.5f));
2147 a.start();
Brandon Keely50e6e562012-05-08 16:28:49 -07002148 }
Adam Cohen19f37922012-03-21 11:59:11 -07002149 }
2150
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002151 private void completeAndClearReorderPreviewAnimations() {
2152 for (ReorderPreviewAnimation a: mShakeAnimators.values()) {
Brandon Keely50e6e562012-05-08 16:28:49 -07002153 a.completeAnimationImmediately();
Adam Cohen19f37922012-03-21 11:59:11 -07002154 }
2155 mShakeAnimators.clear();
2156 }
2157
Adam Cohen482ed822012-03-02 14:15:13 -08002158 private void commitTempPlacement() {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07002159 mTmpOccupied.copyTo(mOccupied);
Sunny Goyalaa8ef112015-06-12 20:04:41 -07002160
2161 long screenId = mLauncher.getWorkspace().getIdForScreen(this);
2162 int container = Favorites.CONTAINER_DESKTOP;
2163
2164 if (mLauncher.isHotseatLayout(this)) {
2165 screenId = -1;
2166 container = Favorites.CONTAINER_HOTSEAT;
2167 }
2168
Michael Jurkaa52570f2012-03-20 03:18:20 -07002169 int childCount = mShortcutsAndWidgets.getChildCount();
Adam Cohen482ed822012-03-02 14:15:13 -08002170 for (int i = 0; i < childCount; i++) {
Adam Cohenea889a22012-03-27 16:45:39 -07002171 View child = mShortcutsAndWidgets.getChildAt(i);
2172 LayoutParams lp = (LayoutParams) child.getLayoutParams();
2173 ItemInfo info = (ItemInfo) child.getTag();
Adam Cohen2acce882012-03-28 19:03:19 -07002174 // We do a null check here because the item info can be null in the case of the
2175 // AllApps button in the hotseat.
2176 if (info != null) {
Sunny Goyalaa8ef112015-06-12 20:04:41 -07002177 final boolean requiresDbUpdate = (info.cellX != lp.tmpCellX
2178 || info.cellY != lp.tmpCellY || info.spanX != lp.cellHSpan
2179 || info.spanY != lp.cellVSpan);
2180
Adam Cohen2acce882012-03-28 19:03:19 -07002181 info.cellX = lp.cellX = lp.tmpCellX;
2182 info.cellY = lp.cellY = lp.tmpCellY;
Adam Cohenbebf0422012-04-11 18:06:28 -07002183 info.spanX = lp.cellHSpan;
2184 info.spanY = lp.cellVSpan;
Sunny Goyalaa8ef112015-06-12 20:04:41 -07002185
2186 if (requiresDbUpdate) {
2187 LauncherModel.modifyItemInDatabase(mLauncher, info, container, screenId,
2188 info.cellX, info.cellY, info.spanX, info.spanY);
2189 }
Adam Cohen2acce882012-03-28 19:03:19 -07002190 }
Adam Cohen482ed822012-03-02 14:15:13 -08002191 }
2192 }
2193
Sunny Goyalf7a29e82015-04-24 15:20:43 -07002194 private void setUseTempCoords(boolean useTempCoords) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07002195 int childCount = mShortcutsAndWidgets.getChildCount();
Adam Cohen482ed822012-03-02 14:15:13 -08002196 for (int i = 0; i < childCount; i++) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07002197 LayoutParams lp = (LayoutParams) mShortcutsAndWidgets.getChildAt(i).getLayoutParams();
Adam Cohen482ed822012-03-02 14:15:13 -08002198 lp.useTmpCoords = useTempCoords;
2199 }
2200 }
2201
Sunny Goyalf7a29e82015-04-24 15:20:43 -07002202 private ItemConfiguration findConfigurationNoShuffle(int pixelX, int pixelY, int minSpanX, int minSpanY,
Adam Cohen482ed822012-03-02 14:15:13 -08002203 int spanX, int spanY, View dragView, ItemConfiguration solution) {
2204 int[] result = new int[2];
2205 int[] resultSpan = new int[2];
Sunny Goyalf7a29e82015-04-24 15:20:43 -07002206 findNearestVacantArea(pixelX, pixelY, minSpanX, minSpanY, spanX, spanY, result,
Adam Cohen482ed822012-03-02 14:15:13 -08002207 resultSpan);
2208 if (result[0] >= 0 && result[1] >= 0) {
2209 copyCurrentStateToSolution(solution, false);
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07002210 solution.cellX = result[0];
2211 solution.cellY = result[1];
2212 solution.spanX = resultSpan[0];
2213 solution.spanY = resultSpan[1];
Adam Cohen482ed822012-03-02 14:15:13 -08002214 solution.isSolution = true;
2215 } else {
2216 solution.isSolution = false;
2217 }
2218 return solution;
2219 }
2220
2221 public void prepareChildForDrag(View child) {
2222 markCellsAsUnoccupiedForView(child);
Adam Cohen482ed822012-03-02 14:15:13 -08002223 }
2224
Adam Cohen19f37922012-03-21 11:59:11 -07002225 /* This seems like it should be obvious and straight-forward, but when the direction vector
2226 needs to match with the notion of the dragView pushing other views, we have to employ
2227 a slightly more subtle notion of the direction vector. The question is what two points is
2228 the vector between? The center of the dragView and its desired destination? Not quite, as
2229 this doesn't necessarily coincide with the interaction of the dragView and items occupying
2230 those cells. Instead we use some heuristics to often lock the vector to up, down, left
2231 or right, which helps make pushing feel right.
2232 */
2233 private void getDirectionVectorForDrop(int dragViewCenterX, int dragViewCenterY, int spanX,
2234 int spanY, View dragView, int[] resultDirection) {
2235 int[] targetDestination = new int[2];
2236
2237 findNearestArea(dragViewCenterX, dragViewCenterY, spanX, spanY, targetDestination);
2238 Rect dragRect = new Rect();
2239 regionToRect(targetDestination[0], targetDestination[1], spanX, spanY, dragRect);
2240 dragRect.offset(dragViewCenterX - dragRect.centerX(), dragViewCenterY - dragRect.centerY());
2241
2242 Rect dropRegionRect = new Rect();
2243 getViewsIntersectingRegion(targetDestination[0], targetDestination[1], spanX, spanY,
2244 dragView, dropRegionRect, mIntersectingViews);
2245
2246 int dropRegionSpanX = dropRegionRect.width();
2247 int dropRegionSpanY = dropRegionRect.height();
2248
2249 regionToRect(dropRegionRect.left, dropRegionRect.top, dropRegionRect.width(),
2250 dropRegionRect.height(), dropRegionRect);
2251
2252 int deltaX = (dropRegionRect.centerX() - dragViewCenterX) / spanX;
2253 int deltaY = (dropRegionRect.centerY() - dragViewCenterY) / spanY;
2254
2255 if (dropRegionSpanX == mCountX || spanX == mCountX) {
2256 deltaX = 0;
2257 }
2258 if (dropRegionSpanY == mCountY || spanY == mCountY) {
2259 deltaY = 0;
2260 }
2261
2262 if (deltaX == 0 && deltaY == 0) {
2263 // No idea what to do, give a random direction.
2264 resultDirection[0] = 1;
2265 resultDirection[1] = 0;
2266 } else {
2267 computeDirectionVector(deltaX, deltaY, resultDirection);
2268 }
2269 }
2270
2271 // For a given cell and span, fetch the set of views intersecting the region.
2272 private void getViewsIntersectingRegion(int cellX, int cellY, int spanX, int spanY,
2273 View dragView, Rect boundingRect, ArrayList<View> intersectingViews) {
2274 if (boundingRect != null) {
2275 boundingRect.set(cellX, cellY, cellX + spanX, cellY + spanY);
2276 }
2277 intersectingViews.clear();
2278 Rect r0 = new Rect(cellX, cellY, cellX + spanX, cellY + spanY);
2279 Rect r1 = new Rect();
2280 final int count = mShortcutsAndWidgets.getChildCount();
2281 for (int i = 0; i < count; i++) {
2282 View child = mShortcutsAndWidgets.getChildAt(i);
2283 if (child == dragView) continue;
2284 LayoutParams lp = (LayoutParams) child.getLayoutParams();
2285 r1.set(lp.cellX, lp.cellY, lp.cellX + lp.cellHSpan, lp.cellY + lp.cellVSpan);
2286 if (Rect.intersects(r0, r1)) {
2287 mIntersectingViews.add(child);
2288 if (boundingRect != null) {
2289 boundingRect.union(r1);
2290 }
2291 }
2292 }
2293 }
2294
2295 boolean isNearestDropLocationOccupied(int pixelX, int pixelY, int spanX, int spanY,
2296 View dragView, int[] result) {
2297 result = findNearestArea(pixelX, pixelY, spanX, spanY, result);
2298 getViewsIntersectingRegion(result[0], result[1], spanX, spanY, dragView, null,
2299 mIntersectingViews);
2300 return !mIntersectingViews.isEmpty();
2301 }
2302
2303 void revertTempState() {
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002304 completeAndClearReorderPreviewAnimations();
2305 if (isItemPlacementDirty() && !DESTRUCTIVE_REORDER) {
2306 final int count = mShortcutsAndWidgets.getChildCount();
2307 for (int i = 0; i < count; i++) {
2308 View child = mShortcutsAndWidgets.getChildAt(i);
2309 LayoutParams lp = (LayoutParams) child.getLayoutParams();
2310 if (lp.tmpCellX != lp.cellX || lp.tmpCellY != lp.cellY) {
2311 lp.tmpCellX = lp.cellX;
2312 lp.tmpCellY = lp.cellY;
2313 animateChildToPosition(child, lp.cellX, lp.cellY, REORDER_ANIMATION_DURATION,
2314 0, false, false);
2315 }
Adam Cohen19f37922012-03-21 11:59:11 -07002316 }
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002317 setItemPlacementDirty(false);
Adam Cohen19f37922012-03-21 11:59:11 -07002318 }
Adam Cohen19f37922012-03-21 11:59:11 -07002319 }
2320
Adam Cohenbebf0422012-04-11 18:06:28 -07002321 boolean createAreaForResize(int cellX, int cellY, int spanX, int spanY,
2322 View dragView, int[] direction, boolean commit) {
2323 int[] pixelXY = new int[2];
2324 regionToCenterPoint(cellX, cellY, spanX, spanY, pixelXY);
2325
2326 // First we determine if things have moved enough to cause a different layout
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002327 ItemConfiguration swapSolution = findReorderSolution(pixelXY[0], pixelXY[1], spanX, spanY,
Adam Cohenbebf0422012-04-11 18:06:28 -07002328 spanX, spanY, direction, dragView, true, new ItemConfiguration());
2329
2330 setUseTempCoords(true);
2331 if (swapSolution != null && swapSolution.isSolution) {
2332 // If we're just testing for a possible location (MODE_ACCEPT_DROP), we don't bother
2333 // committing anything or animating anything as we just want to determine if a solution
2334 // exists
2335 copySolutionToTempState(swapSolution, dragView);
2336 setItemPlacementDirty(true);
2337 animateItemsToSolution(swapSolution, dragView, commit);
2338
2339 if (commit) {
2340 commitTempPlacement();
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002341 completeAndClearReorderPreviewAnimations();
Adam Cohenbebf0422012-04-11 18:06:28 -07002342 setItemPlacementDirty(false);
2343 } else {
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002344 beginOrAdjustReorderPreviewAnimations(swapSolution, dragView,
2345 REORDER_ANIMATION_DURATION, ReorderPreviewAnimation.MODE_PREVIEW);
Adam Cohenbebf0422012-04-11 18:06:28 -07002346 }
2347 mShortcutsAndWidgets.requestLayout();
2348 }
2349 return swapSolution.isSolution;
2350 }
2351
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002352 int[] performReorder(int pixelX, int pixelY, int minSpanX, int minSpanY, int spanX, int spanY,
Adam Cohen482ed822012-03-02 14:15:13 -08002353 View dragView, int[] result, int resultSpan[], int mode) {
Adam Cohen482ed822012-03-02 14:15:13 -08002354 // First we determine if things have moved enough to cause a different layout
Adam Cohen47a876d2012-03-19 13:21:41 -07002355 result = findNearestArea(pixelX, pixelY, spanX, spanY, result);
Adam Cohen482ed822012-03-02 14:15:13 -08002356
2357 if (resultSpan == null) {
2358 resultSpan = new int[2];
2359 }
2360
Adam Cohen19f37922012-03-21 11:59:11 -07002361 // When we are checking drop validity or actually dropping, we don't recompute the
2362 // direction vector, since we want the solution to match the preview, and it's possible
2363 // that the exact position of the item has changed to result in a new reordering outcome.
Adam Cohenb209e632012-03-27 17:09:36 -07002364 if ((mode == MODE_ON_DROP || mode == MODE_ON_DROP_EXTERNAL || mode == MODE_ACCEPT_DROP)
2365 && mPreviousReorderDirection[0] != INVALID_DIRECTION) {
Adam Cohen19f37922012-03-21 11:59:11 -07002366 mDirectionVector[0] = mPreviousReorderDirection[0];
2367 mDirectionVector[1] = mPreviousReorderDirection[1];
2368 // We reset this vector after drop
Adam Cohenb209e632012-03-27 17:09:36 -07002369 if (mode == MODE_ON_DROP || mode == MODE_ON_DROP_EXTERNAL) {
2370 mPreviousReorderDirection[0] = INVALID_DIRECTION;
2371 mPreviousReorderDirection[1] = INVALID_DIRECTION;
Adam Cohen19f37922012-03-21 11:59:11 -07002372 }
2373 } else {
2374 getDirectionVectorForDrop(pixelX, pixelY, spanX, spanY, dragView, mDirectionVector);
2375 mPreviousReorderDirection[0] = mDirectionVector[0];
2376 mPreviousReorderDirection[1] = mDirectionVector[1];
2377 }
2378
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002379 // Find a solution involving pushing / displacing any items in the way
2380 ItemConfiguration swapSolution = findReorderSolution(pixelX, pixelY, minSpanX, minSpanY,
Adam Cohen482ed822012-03-02 14:15:13 -08002381 spanX, spanY, mDirectionVector, dragView, true, new ItemConfiguration());
2382
2383 // We attempt the approach which doesn't shuffle views at all
2384 ItemConfiguration noShuffleSolution = findConfigurationNoShuffle(pixelX, pixelY, minSpanX,
2385 minSpanY, spanX, spanY, dragView, new ItemConfiguration());
2386
2387 ItemConfiguration finalSolution = null;
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002388
2389 // If the reorder solution requires resizing (shrinking) the item being dropped, we instead
2390 // favor a solution in which the item is not resized, but
Adam Cohen482ed822012-03-02 14:15:13 -08002391 if (swapSolution.isSolution && swapSolution.area() >= noShuffleSolution.area()) {
2392 finalSolution = swapSolution;
2393 } else if (noShuffleSolution.isSolution) {
2394 finalSolution = noShuffleSolution;
2395 }
2396
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002397 if (mode == MODE_SHOW_REORDER_HINT) {
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002398 if (finalSolution != null) {
Adam Cohenfe692872013-12-11 14:47:23 -08002399 beginOrAdjustReorderPreviewAnimations(finalSolution, dragView, 0,
2400 ReorderPreviewAnimation.MODE_HINT);
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07002401 result[0] = finalSolution.cellX;
2402 result[1] = finalSolution.cellY;
2403 resultSpan[0] = finalSolution.spanX;
2404 resultSpan[1] = finalSolution.spanY;
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002405 } else {
2406 result[0] = result[1] = resultSpan[0] = resultSpan[1] = -1;
2407 }
2408 return result;
2409 }
2410
Adam Cohen482ed822012-03-02 14:15:13 -08002411 boolean foundSolution = true;
2412 if (!DESTRUCTIVE_REORDER) {
2413 setUseTempCoords(true);
2414 }
2415
2416 if (finalSolution != null) {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07002417 result[0] = finalSolution.cellX;
2418 result[1] = finalSolution.cellY;
2419 resultSpan[0] = finalSolution.spanX;
2420 resultSpan[1] = finalSolution.spanY;
Adam Cohen482ed822012-03-02 14:15:13 -08002421
2422 // If we're just testing for a possible location (MODE_ACCEPT_DROP), we don't bother
2423 // committing anything or animating anything as we just want to determine if a solution
2424 // exists
2425 if (mode == MODE_DRAG_OVER || mode == MODE_ON_DROP || mode == MODE_ON_DROP_EXTERNAL) {
2426 if (!DESTRUCTIVE_REORDER) {
2427 copySolutionToTempState(finalSolution, dragView);
2428 }
2429 setItemPlacementDirty(true);
2430 animateItemsToSolution(finalSolution, dragView, mode == MODE_ON_DROP);
2431
Adam Cohen19f37922012-03-21 11:59:11 -07002432 if (!DESTRUCTIVE_REORDER &&
2433 (mode == MODE_ON_DROP || mode == MODE_ON_DROP_EXTERNAL)) {
Adam Cohen482ed822012-03-02 14:15:13 -08002434 commitTempPlacement();
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002435 completeAndClearReorderPreviewAnimations();
Adam Cohen19f37922012-03-21 11:59:11 -07002436 setItemPlacementDirty(false);
2437 } else {
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002438 beginOrAdjustReorderPreviewAnimations(finalSolution, dragView,
2439 REORDER_ANIMATION_DURATION, ReorderPreviewAnimation.MODE_PREVIEW);
Adam Cohen482ed822012-03-02 14:15:13 -08002440 }
2441 }
2442 } else {
2443 foundSolution = false;
2444 result[0] = result[1] = resultSpan[0] = resultSpan[1] = -1;
2445 }
2446
2447 if ((mode == MODE_ON_DROP || !foundSolution) && !DESTRUCTIVE_REORDER) {
2448 setUseTempCoords(false);
2449 }
Adam Cohen482ed822012-03-02 14:15:13 -08002450
Michael Jurkaa52570f2012-03-20 03:18:20 -07002451 mShortcutsAndWidgets.requestLayout();
Adam Cohen482ed822012-03-02 14:15:13 -08002452 return result;
2453 }
2454
Adam Cohen19f37922012-03-21 11:59:11 -07002455 void setItemPlacementDirty(boolean dirty) {
2456 mItemPlacementDirty = dirty;
Adam Cohen482ed822012-03-02 14:15:13 -08002457 }
Adam Cohen19f37922012-03-21 11:59:11 -07002458 boolean isItemPlacementDirty() {
2459 return mItemPlacementDirty;
Adam Cohen482ed822012-03-02 14:15:13 -08002460 }
2461
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07002462 private static class ItemConfiguration extends CellAndSpan {
Adam Cohen8baab352012-03-20 17:39:21 -07002463 HashMap<View, CellAndSpan> map = new HashMap<View, CellAndSpan>();
Adam Cohenf3900c22012-11-16 18:28:11 -08002464 private HashMap<View, CellAndSpan> savedMap = new HashMap<View, CellAndSpan>();
2465 ArrayList<View> sortedViews = new ArrayList<View>();
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002466 ArrayList<View> intersectingViews;
Adam Cohen482ed822012-03-02 14:15:13 -08002467 boolean isSolution = false;
Adam Cohen482ed822012-03-02 14:15:13 -08002468
Adam Cohenf3900c22012-11-16 18:28:11 -08002469 void save() {
2470 // Copy current state into savedMap
2471 for (View v: map.keySet()) {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07002472 savedMap.get(v).copyFrom(map.get(v));
Adam Cohenf3900c22012-11-16 18:28:11 -08002473 }
2474 }
2475
2476 void restore() {
2477 // Restore current state from savedMap
2478 for (View v: savedMap.keySet()) {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07002479 map.get(v).copyFrom(savedMap.get(v));
Adam Cohenf3900c22012-11-16 18:28:11 -08002480 }
2481 }
2482
2483 void add(View v, CellAndSpan cs) {
2484 map.put(v, cs);
2485 savedMap.put(v, new CellAndSpan());
2486 sortedViews.add(v);
2487 }
2488
Adam Cohen482ed822012-03-02 14:15:13 -08002489 int area() {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07002490 return spanX * spanY;
Adam Cohenf3900c22012-11-16 18:28:11 -08002491 }
2492
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07002493 void getBoundingRectForViews(ArrayList<View> views, Rect outRect) {
2494 boolean first = true;
2495 for (View v: views) {
2496 CellAndSpan c = map.get(v);
2497 if (first) {
2498 outRect.set(c.cellX, c.cellY, c.cellX + c.spanX, c.cellY + c.spanY);
2499 first = false;
2500 } else {
2501 outRect.union(c.cellX, c.cellY, c.cellX + c.spanX, c.cellY + c.spanY);
2502 }
2503 }
Adam Cohenf3900c22012-11-16 18:28:11 -08002504 }
Adam Cohen482ed822012-03-02 14:15:13 -08002505 }
2506
Adam Cohendf035382011-04-11 17:22:04 -07002507 /**
Adam Cohendf035382011-04-11 17:22:04 -07002508 * Find a starting cell position that will fit the given bounds nearest the requested
2509 * cell location. Uses Euclidean distance to score multiple vacant areas.
2510 *
2511 * @param pixelX The X location at which you want to search for a vacant area.
2512 * @param pixelY The Y location at which you want to search for a vacant area.
2513 * @param spanX Horizontal span of the object.
2514 * @param spanY Vertical span of the object.
2515 * @param ignoreView Considers space occupied by this view as unoccupied
2516 * @param result Previously returned value to possibly recycle.
2517 * @return The X, Y cell of a vacant area that can contain this object,
2518 * nearest the requested location.
2519 */
Adam Cohenf9c184a2016-01-15 16:47:43 -08002520 public int[] findNearestArea(int pixelX, int pixelY, int spanX, int spanY, int[] result) {
Sunny Goyalf7a29e82015-04-24 15:20:43 -07002521 return findNearestArea(pixelX, pixelY, spanX, spanY, spanX, spanY, false, result, null);
Adam Cohendf035382011-04-11 17:22:04 -07002522 }
2523
Michael Jurka0280c3b2010-09-17 15:00:07 -07002524 boolean existsEmptyCell() {
2525 return findCellForSpan(null, 1, 1);
2526 }
2527
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002528 /**
Michael Jurka0280c3b2010-09-17 15:00:07 -07002529 * Finds the upper-left coordinate of the first rectangle in the grid that can
2530 * hold a cell of the specified dimensions. If intersectX and intersectY are not -1,
2531 * then this method will only return coordinates for rectangles that contain the cell
2532 * (intersectX, intersectY)
2533 *
2534 * @param cellXY The array that will contain the position of a vacant cell if such a cell
2535 * can be found.
2536 * @param spanX The horizontal span of the cell we want to find.
2537 * @param spanY The vertical span of the cell we want to find.
2538 *
2539 * @return True if a vacant cell of the specified dimension was found, false otherwise.
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07002540 */
Hyunyoung Song3f471442015-04-08 19:01:34 -07002541 public boolean findCellForSpan(int[] cellXY, int spanX, int spanY) {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07002542 if (cellXY == null) {
2543 cellXY = new int[2];
Michael Jurka0280c3b2010-09-17 15:00:07 -07002544 }
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07002545 return mOccupied.findVacantCell(cellXY, spanX, spanY);
Michael Jurka0280c3b2010-09-17 15:00:07 -07002546 }
2547
2548 /**
Winson Chungc07918d2011-07-01 15:35:26 -07002549 * A drag event has begun over this layout.
2550 * It may have begun over this layout (in which case onDragChild is called first),
2551 * or it may have begun on another layout.
2552 */
2553 void onDragEnter() {
Winson Chungc07918d2011-07-01 15:35:26 -07002554 mDragging = true;
2555 }
2556
2557 /**
Michael Jurka0280c3b2010-09-17 15:00:07 -07002558 * Called when drag has left this CellLayout or has been completed (successfully or not)
2559 */
2560 void onDragExit() {
Joe Onorato4be866d2010-10-10 11:26:02 -07002561 // This can actually be called when we aren't in a drag, e.g. when adding a new
2562 // item to this layout via the customize drawer.
2563 // Guard against that case.
2564 if (mDragging) {
2565 mDragging = false;
Patrick Dubroyde7658b2010-09-27 11:15:43 -07002566 }
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -07002567
2568 // Invalidate the drag data
Adam Cohend41fbf52012-02-16 23:53:59 -08002569 mDragCell[0] = mDragCell[1] = -1;
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -07002570 mDragOutlineAnims[mDragOutlineCurrent].animateOut();
2571 mDragOutlineCurrent = (mDragOutlineCurrent + 1) % mDragOutlineAnims.length;
Adam Cohen19f37922012-03-21 11:59:11 -07002572 revertTempState();
Michael Jurka33945b22010-12-21 18:19:38 -08002573 setIsDragOverlapping(false);
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07002574 }
2575
2576 /**
Winson Chungaafa03c2010-06-11 17:34:16 -07002577 * Mark a child as having been dropped.
Patrick Dubroyde7658b2010-09-27 11:15:43 -07002578 * At the beginning of the drag operation, the child may have been on another
Patrick Dubroyce34a972010-10-19 10:34:32 -07002579 * screen, but it is re-parented before this method is called.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002580 *
2581 * @param child The child that is being dropped
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002582 */
Adam Cohen716b51e2011-06-30 12:09:54 -07002583 void onDropChild(View child) {
Romain Guyd94533d2009-08-17 10:01:15 -07002584 if (child != null) {
2585 LayoutParams lp = (LayoutParams) child.getLayoutParams();
Romain Guy84f296c2009-11-04 15:00:44 -08002586 lp.dropped = true;
Romain Guyd94533d2009-08-17 10:01:15 -07002587 child.requestLayout();
Tony Wickham1cdb6d02015-09-17 11:08:27 -07002588 markCellsAsOccupiedForView(child);
Romain Guyd94533d2009-08-17 10:01:15 -07002589 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002590 }
2591
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002592 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002593 * Computes a bounding rectangle for a range of cells
Winson Chungaafa03c2010-06-11 17:34:16 -07002594 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002595 * @param cellX X coordinate of upper left corner expressed as a cell position
2596 * @param cellY Y coordinate of upper left corner expressed as a cell position
Winson Chungaafa03c2010-06-11 17:34:16 -07002597 * @param cellHSpan Width in cells
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002598 * @param cellVSpan Height in cells
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07002599 * @param resultRect Rect into which to put the results
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002600 */
Adam Cohend41fbf52012-02-16 23:53:59 -08002601 public void cellToRect(int cellX, int cellY, int cellHSpan, int cellVSpan, Rect resultRect) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002602 final int cellWidth = mCellWidth;
2603 final int cellHeight = mCellHeight;
2604 final int widthGap = mWidthGap;
2605 final int heightGap = mHeightGap;
Winson Chungaafa03c2010-06-11 17:34:16 -07002606
Winson Chung4b825dcd2011-06-19 12:41:22 -07002607 final int hStartPadding = getPaddingLeft();
2608 final int vStartPadding = getPaddingTop();
Winson Chungaafa03c2010-06-11 17:34:16 -07002609
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002610 int width = cellHSpan * cellWidth + ((cellHSpan - 1) * widthGap);
2611 int height = cellVSpan * cellHeight + ((cellVSpan - 1) * heightGap);
2612
2613 int x = hStartPadding + cellX * (cellWidth + widthGap);
2614 int y = vStartPadding + cellY * (cellHeight + heightGap);
Winson Chungaafa03c2010-06-11 17:34:16 -07002615
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07002616 resultRect.set(x, y, x + width, y + height);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002617 }
Winson Chungaafa03c2010-06-11 17:34:16 -07002618
Adam Cohend4844c32011-02-18 19:25:06 -08002619 public void markCellsAsOccupiedForView(View view) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07002620 if (view == null || view.getParent() != mShortcutsAndWidgets) return;
Michael Jurka0280c3b2010-09-17 15:00:07 -07002621 LayoutParams lp = (LayoutParams) view.getLayoutParams();
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07002622 mOccupied.markCells(lp.cellX, lp.cellY, lp.cellHSpan, lp.cellVSpan, true);
Michael Jurka0280c3b2010-09-17 15:00:07 -07002623 }
2624
Adam Cohend4844c32011-02-18 19:25:06 -08002625 public void markCellsAsUnoccupiedForView(View view) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07002626 if (view == null || view.getParent() != mShortcutsAndWidgets) return;
Michael Jurka0280c3b2010-09-17 15:00:07 -07002627 LayoutParams lp = (LayoutParams) view.getLayoutParams();
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07002628 mOccupied.markCells(lp.cellX, lp.cellY, lp.cellHSpan, lp.cellVSpan, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002629 }
2630
Adam Cohen2801caf2011-05-13 20:57:39 -07002631 public int getDesiredWidth() {
Michael Jurka8b805b12012-04-18 14:23:14 -07002632 return getPaddingLeft() + getPaddingRight() + (mCountX * mCellWidth) +
Adam Cohen2801caf2011-05-13 20:57:39 -07002633 (Math.max((mCountX - 1), 0) * mWidthGap);
2634 }
2635
2636 public int getDesiredHeight() {
Michael Jurka8b805b12012-04-18 14:23:14 -07002637 return getPaddingTop() + getPaddingBottom() + (mCountY * mCellHeight) +
Adam Cohen2801caf2011-05-13 20:57:39 -07002638 (Math.max((mCountY - 1), 0) * mHeightGap);
2639 }
2640
Michael Jurka66d72172011-04-12 16:29:25 -07002641 public boolean isOccupied(int x, int y) {
2642 if (x < mCountX && y < mCountY) {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07002643 return mOccupied.cells[x][y];
Michael Jurka66d72172011-04-12 16:29:25 -07002644 } else {
2645 throw new RuntimeException("Position exceeds the bound of this CellLayout");
2646 }
2647 }
2648
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002649 @Override
2650 public ViewGroup.LayoutParams generateLayoutParams(AttributeSet attrs) {
2651 return new CellLayout.LayoutParams(getContext(), attrs);
2652 }
2653
2654 @Override
2655 protected boolean checkLayoutParams(ViewGroup.LayoutParams p) {
2656 return p instanceof CellLayout.LayoutParams;
2657 }
2658
2659 @Override
2660 protected ViewGroup.LayoutParams generateLayoutParams(ViewGroup.LayoutParams p) {
2661 return new CellLayout.LayoutParams(p);
2662 }
2663
2664 public static class LayoutParams extends ViewGroup.MarginLayoutParams {
2665 /**
2666 * Horizontal location of the item in the grid.
2667 */
2668 @ViewDebug.ExportedProperty
2669 public int cellX;
2670
2671 /**
2672 * Vertical location of the item in the grid.
2673 */
2674 @ViewDebug.ExportedProperty
2675 public int cellY;
2676
2677 /**
Adam Cohen482ed822012-03-02 14:15:13 -08002678 * Temporary horizontal location of the item in the grid during reorder
2679 */
2680 public int tmpCellX;
2681
2682 /**
2683 * Temporary vertical location of the item in the grid during reorder
2684 */
2685 public int tmpCellY;
2686
2687 /**
2688 * Indicates that the temporary coordinates should be used to layout the items
2689 */
2690 public boolean useTmpCoords;
2691
2692 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002693 * Number of cells spanned horizontally by the item.
2694 */
2695 @ViewDebug.ExportedProperty
2696 public int cellHSpan;
2697
2698 /**
2699 * Number of cells spanned vertically by the item.
2700 */
2701 @ViewDebug.ExportedProperty
2702 public int cellVSpan;
Winson Chungaafa03c2010-06-11 17:34:16 -07002703
Adam Cohen1b607ed2011-03-03 17:26:50 -08002704 /**
2705 * Indicates whether the item will set its x, y, width and height parameters freely,
2706 * or whether these will be computed based on cellX, cellY, cellHSpan and cellVSpan.
2707 */
Adam Cohend4844c32011-02-18 19:25:06 -08002708 public boolean isLockedToGrid = true;
2709
Adam Cohen482ed822012-03-02 14:15:13 -08002710 /**
Adam Cohenec40b2b2013-07-23 15:52:40 -07002711 * Indicates that this item should use the full extents of its parent.
2712 */
2713 public boolean isFullscreen = false;
2714
2715 /**
Adam Cohen482ed822012-03-02 14:15:13 -08002716 * Indicates whether this item can be reordered. Always true except in the case of the
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07002717 * the AllApps button and QSB place holder.
Adam Cohen482ed822012-03-02 14:15:13 -08002718 */
2719 public boolean canReorder = true;
2720
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002721 // X coordinate of the view in the layout.
2722 @ViewDebug.ExportedProperty
Vadim Tryshevfedca432015-08-19 17:55:02 -07002723 public int x;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002724 // Y coordinate of the view in the layout.
2725 @ViewDebug.ExportedProperty
Vadim Tryshevfedca432015-08-19 17:55:02 -07002726 public int y;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002727
Romain Guy84f296c2009-11-04 15:00:44 -08002728 boolean dropped;
Romain Guyfcb9e712009-10-02 16:06:52 -07002729
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002730 public LayoutParams(Context c, AttributeSet attrs) {
2731 super(c, attrs);
2732 cellHSpan = 1;
2733 cellVSpan = 1;
2734 }
2735
2736 public LayoutParams(ViewGroup.LayoutParams source) {
2737 super(source);
2738 cellHSpan = 1;
2739 cellVSpan = 1;
2740 }
Winson Chungaafa03c2010-06-11 17:34:16 -07002741
2742 public LayoutParams(LayoutParams source) {
2743 super(source);
2744 this.cellX = source.cellX;
2745 this.cellY = source.cellY;
2746 this.cellHSpan = source.cellHSpan;
2747 this.cellVSpan = source.cellVSpan;
2748 }
2749
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002750 public LayoutParams(int cellX, int cellY, int cellHSpan, int cellVSpan) {
Romain Guy8f19cdd2010-01-08 15:07:00 -08002751 super(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002752 this.cellX = cellX;
2753 this.cellY = cellY;
2754 this.cellHSpan = cellHSpan;
2755 this.cellVSpan = cellVSpan;
2756 }
2757
Adam Cohen2374abf2013-04-16 14:56:57 -07002758 public void setup(int cellWidth, int cellHeight, int widthGap, int heightGap,
2759 boolean invertHorizontally, int colCount) {
Adam Cohend4844c32011-02-18 19:25:06 -08002760 if (isLockedToGrid) {
2761 final int myCellHSpan = cellHSpan;
2762 final int myCellVSpan = cellVSpan;
Adam Cohen2374abf2013-04-16 14:56:57 -07002763 int myCellX = useTmpCoords ? tmpCellX : cellX;
2764 int myCellY = useTmpCoords ? tmpCellY : cellY;
2765
2766 if (invertHorizontally) {
2767 myCellX = colCount - myCellX - cellHSpan;
2768 }
Adam Cohen1b607ed2011-03-03 17:26:50 -08002769
Adam Cohend4844c32011-02-18 19:25:06 -08002770 width = myCellHSpan * cellWidth + ((myCellHSpan - 1) * widthGap) -
2771 leftMargin - rightMargin;
2772 height = myCellVSpan * cellHeight + ((myCellVSpan - 1) * heightGap) -
2773 topMargin - bottomMargin;
Winson Chungeecf02d2012-03-02 17:14:58 -08002774 x = (int) (myCellX * (cellWidth + widthGap) + leftMargin);
2775 y = (int) (myCellY * (cellHeight + heightGap) + topMargin);
Adam Cohend4844c32011-02-18 19:25:06 -08002776 }
2777 }
Winson Chungaafa03c2010-06-11 17:34:16 -07002778
Winson Chungaafa03c2010-06-11 17:34:16 -07002779 public String toString() {
2780 return "(" + this.cellX + ", " + this.cellY + ")";
2781 }
Adam Cohen7f4eabe2011-04-21 16:19:16 -07002782
2783 public void setWidth(int width) {
2784 this.width = width;
2785 }
2786
2787 public int getWidth() {
2788 return width;
2789 }
2790
2791 public void setHeight(int height) {
2792 this.height = height;
2793 }
2794
2795 public int getHeight() {
2796 return height;
2797 }
2798
2799 public void setX(int x) {
2800 this.x = x;
2801 }
2802
2803 public int getX() {
2804 return x;
2805 }
2806
2807 public void setY(int y) {
2808 this.y = y;
2809 }
2810
2811 public int getY() {
2812 return y;
2813 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002814 }
2815
Michael Jurka0280c3b2010-09-17 15:00:07 -07002816 // This class stores info for two purposes:
2817 // 1. When dragging items (mDragInfo in Workspace), we store the View, its cellX & cellY,
2818 // its spanX, spanY, and the screen it is on
2819 // 2. When long clicking on an empty cell in a CellLayout, we save information about the
2820 // cellX and cellY coordinates and which page was clicked. We then set this as a tag on
2821 // the CellLayout that was long clicked
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07002822 public static final class CellInfo extends CellAndSpan {
Adam Cohenf9c184a2016-01-15 16:47:43 -08002823 public View cell;
Adam Cohendcd297f2013-06-18 13:13:40 -07002824 long screenId;
Winson Chung3d503fb2011-07-13 17:25:49 -07002825 long container;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002826
Sunny Goyal83a8f042015-05-19 12:52:12 -07002827 public CellInfo(View v, ItemInfo info) {
Adam Cohene0aaa0d2014-05-12 12:44:22 -07002828 cellX = info.cellX;
2829 cellY = info.cellY;
2830 spanX = info.spanX;
2831 spanY = info.spanY;
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07002832 cell = v;
Adam Cohene0aaa0d2014-05-12 12:44:22 -07002833 screenId = info.screenId;
2834 container = info.container;
2835 }
2836
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002837 @Override
2838 public String toString() {
Winson Chungaafa03c2010-06-11 17:34:16 -07002839 return "Cell[view=" + (cell == null ? "null" : cell.getClass())
2840 + ", x=" + cellX + ", y=" + cellY + "]";
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002841 }
2842 }
Michael Jurkad771c962011-08-09 15:00:48 -07002843
Tony Wickham86930612015-09-09 13:50:40 -07002844 /**
2845 * Returns whether an item can be placed in this CellLayout (after rearranging and/or resizing
2846 * if necessary).
2847 */
2848 public boolean hasReorderSolution(ItemInfo itemInfo) {
2849 int[] cellPoint = new int[2];
2850 // Check for a solution starting at every cell.
2851 for (int cellX = 0; cellX < getCountX(); cellX++) {
2852 for (int cellY = 0; cellY < getCountY(); cellY++) {
2853 cellToPoint(cellX, cellY, cellPoint);
2854 if (findReorderSolution(cellPoint[0], cellPoint[1], itemInfo.minSpanX,
2855 itemInfo.minSpanY, itemInfo.spanX, itemInfo.spanY, mDirectionVector, null,
2856 true, new ItemConfiguration()).isSolution) {
2857 return true;
2858 }
2859 }
2860 }
2861 return false;
2862 }
2863
Sunny Goyal9ca9c132015-04-29 14:57:22 -07002864 public boolean isRegionVacant(int x, int y, int spanX, int spanY) {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07002865 return mOccupied.isRegionVacant(x, y, spanX, spanY);
Sunny Goyal9ca9c132015-04-29 14:57:22 -07002866 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002867}