blob: 77f6612c1c1a177706056776deefe04352484644 [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)) {
Winsonbe9798b2016-07-20 12:55:49 -0700409 mTouchFeedbackView.alignWithIconView(icon, mShortcutsAndWidgets,
410 null /* clipAgainstView */);
Sunny Goyal4fe5a372015-05-14 19:55:10 -0700411 mTouchFeedbackView.animateShadow();
Sunny Goyal508da152014-08-14 10:53:27 -0700412 }
Patrick Dubroy96864c32011-03-10 17:17:23 -0800413 }
414 }
415
Adam Cohenc50438c2014-08-19 17:43:05 -0700416 void disableDragTarget() {
417 mIsDragTarget = false;
418 }
419
Tony Wickham0f97b782015-12-02 17:55:07 -0800420 public boolean isDragTarget() {
421 return mIsDragTarget;
422 }
423
Adam Cohenc50438c2014-08-19 17:43:05 -0700424 void setIsDragOverlapping(boolean isDragOverlapping) {
425 if (mIsDragOverlapping != isDragOverlapping) {
426 mIsDragOverlapping = isDragOverlapping;
Sunny Goyal2805e632015-05-20 15:35:32 -0700427 if (mIsDragOverlapping) {
428 mBackground.startTransition(BACKGROUND_ACTIVATE_DURATION);
429 } else {
Winson Chunge8f1d042015-07-31 12:39:57 -0700430 if (mBackgroundAlpha > 0f) {
431 mBackground.reverseTransition(BACKGROUND_ACTIVATE_DURATION);
432 } else {
433 mBackground.resetTransition();
434 }
Sunny Goyal2805e632015-05-20 15:35:32 -0700435 }
Adam Cohenc50438c2014-08-19 17:43:05 -0700436 invalidate();
437 }
438 }
439
Sunny Goyale2fd14b2015-08-27 17:45:46 -0700440 public void disableJailContent() {
441 mJailContent = false;
442 }
443
444 @Override
445 protected void dispatchSaveInstanceState(SparseArray<Parcelable> container) {
446 if (mJailContent) {
447 ParcelableSparseArray jail = getJailedArray(container);
448 super.dispatchSaveInstanceState(jail);
449 container.put(R.id.cell_layout_jail_id, jail);
450 } else {
451 super.dispatchSaveInstanceState(container);
452 }
453 }
454
455 @Override
456 protected void dispatchRestoreInstanceState(SparseArray<Parcelable> container) {
457 super.dispatchRestoreInstanceState(mJailContent ? getJailedArray(container) : container);
458 }
459
460 private ParcelableSparseArray getJailedArray(SparseArray<Parcelable> container) {
461 final Parcelable parcelable = container.get(R.id.cell_layout_jail_id);
462 return parcelable instanceof ParcelableSparseArray ?
463 (ParcelableSparseArray) parcelable : new ParcelableSparseArray();
464 }
465
Tony Wickham0f97b782015-12-02 17:55:07 -0800466 public boolean getIsDragOverlapping() {
467 return mIsDragOverlapping;
468 }
469
Jeff Sharkey83f111d2009-04-20 21:03:13 -0700470 @Override
Patrick Dubroy1262e362010-10-06 15:49:50 -0700471 protected void onDraw(Canvas canvas) {
Sunny Goyal05739772015-05-19 19:59:09 -0700472 if (!mIsDragTarget) {
473 return;
474 }
475
Michael Jurka3e7c7632010-10-02 16:01:03 -0700476 // When we're large, we are either drawn in a "hover" state (ie when dragging an item to
477 // a neighboring page) or with just a normal background (if backgroundAlpha > 0.0f)
478 // When we're small, we are either drawn normally or in the "accepts drops" state (during
479 // a drag). However, we also drag the mini hover background *over* one of those two
480 // backgrounds
Sunny Goyal05739772015-05-19 19:59:09 -0700481 if (mBackgroundAlpha > 0.0f) {
Sunny Goyal2805e632015-05-20 15:35:32 -0700482 mBackground.draw(canvas);
Michael Jurkaa63c4522010-08-19 13:52:27 -0700483 }
Romain Guya6abce82009-11-10 02:54:41 -0800484
Patrick Dubroy8e58e912010-10-14 13:21:48 -0700485 final Paint paint = mDragOutlinePaint;
Joe Onorato4be866d2010-10-10 11:26:02 -0700486 for (int i = 0; i < mDragOutlines.length; i++) {
Chet Haase472b2812010-10-14 07:02:04 -0700487 final float alpha = mDragOutlineAlphas[i];
Joe Onorato4be866d2010-10-10 11:26:02 -0700488 if (alpha > 0) {
Joe Onorato4be866d2010-10-10 11:26:02 -0700489 final Bitmap b = (Bitmap) mDragOutlineAnims[i].getTag();
Chet Haase472b2812010-10-14 07:02:04 -0700490 paint.setAlpha((int)(alpha + .5f));
Sunny Goyal106bf642015-07-16 12:18:06 -0700491 canvas.drawBitmap(b, null, mDragOutlines[i], paint);
Winson Chung150fbab2010-09-29 17:14:26 -0700492 }
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700493 }
Patrick Dubroy96864c32011-03-10 17:17:23 -0800494
Adam Cohen482ed822012-03-02 14:15:13 -0800495 if (DEBUG_VISUALIZE_OCCUPIED) {
496 int[] pt = new int[2];
497 ColorDrawable cd = new ColorDrawable(Color.RED);
Adam Cohene7587d22012-05-24 18:50:02 -0700498 cd.setBounds(0, 0, mCellWidth, mCellHeight);
Adam Cohen482ed822012-03-02 14:15:13 -0800499 for (int i = 0; i < mCountX; i++) {
500 for (int j = 0; j < mCountY; j++) {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -0700501 if (mOccupied.cells[i][j]) {
Adam Cohen482ed822012-03-02 14:15:13 -0800502 cellToPoint(i, j, pt);
503 canvas.save();
504 canvas.translate(pt[0], pt[1]);
505 cd.draw(canvas);
506 canvas.restore();
507 }
508 }
509 }
510 }
511
Adam Cohenefca0272016-02-24 19:19:06 -0800512 for (int i = 0; i < mFolderBackgrounds.size(); i++) {
513 FolderIcon.PreviewBackground bg = mFolderBackgrounds.get(i);
514 cellToPoint(bg.delegateCellX, bg.delegateCellY, mTempLocation);
515 canvas.save();
516 canvas.translate(mTempLocation[0], mTempLocation[1]);
517 bg.drawBackground(canvas, mFolderBgPaint);
Adam Cohenf172b742016-03-30 19:28:34 -0700518 if (!bg.isClipping) {
519 bg.drawBackgroundStroke(canvas, mFolderBgPaint);
520 }
Adam Cohenefca0272016-02-24 19:19:06 -0800521 canvas.restore();
Adam Cohen69ce2e52011-07-03 19:25:21 -0700522 }
Adam Cohenc51934b2011-07-26 21:07:43 -0700523
Adam Cohenefca0272016-02-24 19:19:06 -0800524 if (mFolderLeaveBehind.delegateCellX >= 0 && mFolderLeaveBehind.delegateCellY >= 0) {
525 cellToPoint(mFolderLeaveBehind.delegateCellX,
526 mFolderLeaveBehind.delegateCellY, mTempLocation);
527 canvas.save();
528 canvas.translate(mTempLocation[0], mTempLocation[1]);
529 mFolderLeaveBehind.drawLeaveBehind(canvas, mFolderBgPaint);
530 canvas.restore();
Adam Cohenc51934b2011-07-26 21:07:43 -0700531 }
Adam Cohen69ce2e52011-07-03 19:25:21 -0700532 }
533
Adam Cohenefca0272016-02-24 19:19:06 -0800534 @Override
535 protected void dispatchDraw(Canvas canvas) {
536 super.dispatchDraw(canvas);
537
538 for (int i = 0; i < mFolderBackgrounds.size(); i++) {
539 FolderIcon.PreviewBackground bg = mFolderBackgrounds.get(i);
Adam Cohenf172b742016-03-30 19:28:34 -0700540 if (bg.isClipping) {
541 cellToPoint(bg.delegateCellX, bg.delegateCellY, mTempLocation);
542 canvas.save();
543 canvas.translate(mTempLocation[0], mTempLocation[1]);
544 bg.drawBackgroundStroke(canvas, mFolderBgPaint);
545 canvas.restore();
546 }
Adam Cohenefca0272016-02-24 19:19:06 -0800547 }
Adam Cohen69ce2e52011-07-03 19:25:21 -0700548 }
549
Adam Cohenefca0272016-02-24 19:19:06 -0800550 public void addFolderBackground(FolderIcon.PreviewBackground bg) {
551 mFolderBackgrounds.add(bg);
552 }
553 public void removeFolderBackground(FolderIcon.PreviewBackground bg) {
554 mFolderBackgrounds.remove(bg);
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700555 }
556
Adam Cohenc51934b2011-07-26 21:07:43 -0700557 public void setFolderLeaveBehindCell(int x, int y) {
Adam Cohenefca0272016-02-24 19:19:06 -0800558
559 DeviceProfile grid = mLauncher.getDeviceProfile();
560 View child = getChildAt(x, y);
561
562 mFolderLeaveBehind.setup(getResources().getDisplayMetrics(), grid, null,
563 child.getMeasuredWidth(), child.getPaddingTop());
564
565 mFolderLeaveBehind.delegateCellX = x;
566 mFolderLeaveBehind.delegateCellY = y;
Adam Cohenc51934b2011-07-26 21:07:43 -0700567 invalidate();
568 }
569
570 public void clearFolderLeaveBehind() {
Adam Cohenefca0272016-02-24 19:19:06 -0800571 mFolderLeaveBehind.delegateCellX = -1;
572 mFolderLeaveBehind.delegateCellY = -1;
Adam Cohenc51934b2011-07-26 21:07:43 -0700573 invalidate();
574 }
575
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700576 @Override
Michael Jurkae6235dd2011-10-04 15:02:05 -0700577 public boolean shouldDelayChildPressedState() {
578 return false;
579 }
580
Adam Cohen1462de32012-07-24 22:34:36 -0700581 public void restoreInstanceState(SparseArray<Parcelable> states) {
Sunny Goyal33a152f2014-07-22 12:13:14 -0700582 try {
583 dispatchRestoreInstanceState(states);
584 } catch (IllegalArgumentException ex) {
Sunny Goyal6c56c682015-07-16 14:09:05 -0700585 if (ProviderConfig.IS_DOGFOOD_BUILD) {
Sunny Goyal33a152f2014-07-22 12:13:14 -0700586 throw ex;
587 }
588 // Mismatched viewId / viewType preventing restore. Skip restore on production builds.
589 Log.e(TAG, "Ignoring an error while restoring a view instance state", ex);
590 }
Adam Cohen1462de32012-07-24 22:34:36 -0700591 }
592
Michael Jurkae6235dd2011-10-04 15:02:05 -0700593 @Override
Jeff Sharkey83f111d2009-04-20 21:03:13 -0700594 public void cancelLongPress() {
595 super.cancelLongPress();
596
597 // Cancel long press for all children
598 final int count = getChildCount();
599 for (int i = 0; i < count; i++) {
600 final View child = getChildAt(i);
601 child.cancelLongPress();
602 }
603 }
604
Michael Jurkadee05892010-07-27 10:01:56 -0700605 public void setOnInterceptTouchListener(View.OnTouchListener listener) {
606 mInterceptTouchListener = listener;
607 }
608
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800609 public int getCountX() {
Adam Cohend22015c2010-07-26 22:02:18 -0700610 return mCountX;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800611 }
612
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800613 public int getCountY() {
Adam Cohend22015c2010-07-26 22:02:18 -0700614 return mCountY;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800615 }
616
Andrew Flynn0dca1ec2012-02-29 13:33:22 -0800617 public void setIsHotseat(boolean isHotseat) {
618 mIsHotseat = isHotseat;
Winson Chung5f8afe62013-08-12 16:19:28 -0700619 mShortcutsAndWidgets.setIsHotseat(isHotseat);
Andrew Flynn0dca1ec2012-02-29 13:33:22 -0800620 }
621
Sunny Goyale9b651e2015-04-24 11:44:51 -0700622 public boolean isHotseat() {
623 return mIsHotseat;
624 }
625
Andrew Flynn0dca1ec2012-02-29 13:33:22 -0800626 public boolean addViewToCellLayout(View child, int index, int childId, LayoutParams params,
Sunny Goyalf7a29e82015-04-24 15:20:43 -0700627 boolean markCells) {
Winson Chungaafa03c2010-06-11 17:34:16 -0700628 final LayoutParams lp = params;
629
Andrew Flynnde38e422012-05-08 11:22:15 -0700630 // Hotseat icons - remove text
Andrew Flynn0dca1ec2012-02-29 13:33:22 -0800631 if (child instanceof BubbleTextView) {
632 BubbleTextView bubbleChild = (BubbleTextView) child;
Winson Chung5f8afe62013-08-12 16:19:28 -0700633 bubbleChild.setTextVisibility(!mIsHotseat);
Andrew Flynn0dca1ec2012-02-29 13:33:22 -0800634 }
635
Adam Cohen307fe232012-08-16 17:55:58 -0700636 child.setScaleX(getChildrenScale());
637 child.setScaleY(getChildrenScale());
638
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800639 // Generate an id for each view, this assumes we have at most 256x256 cells
640 // per workspace screen
Adam Cohend22015c2010-07-26 22:02:18 -0700641 if (lp.cellX >= 0 && lp.cellX <= mCountX - 1 && lp.cellY >= 0 && lp.cellY <= mCountY - 1) {
Winson Chungaafa03c2010-06-11 17:34:16 -0700642 // If the horizontal or vertical span is set to -1, it is taken to
643 // mean that it spans the extent of the CellLayout
Adam Cohend22015c2010-07-26 22:02:18 -0700644 if (lp.cellHSpan < 0) lp.cellHSpan = mCountX;
645 if (lp.cellVSpan < 0) lp.cellVSpan = mCountY;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800646
Winson Chungaafa03c2010-06-11 17:34:16 -0700647 child.setId(childId);
Tony Wickhama0628cc2015-10-14 15:23:04 -0700648 if (LOGD) {
649 Log.d(TAG, "Adding view to ShortcutsAndWidgetsContainer: " + child);
650 }
Sunny Goyalf7a29e82015-04-24 15:20:43 -0700651 mShortcutsAndWidgets.addView(child, index, lp);
Michael Jurkadee05892010-07-27 10:01:56 -0700652
Michael Jurkaf3ca3ab2010-10-20 17:08:24 -0700653 if (markCells) markCellsAsOccupiedForView(child);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700654
Winson Chungaafa03c2010-06-11 17:34:16 -0700655 return true;
656 }
657 return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800658 }
Michael Jurka3e7c7632010-10-02 16:01:03 -0700659
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800660 @Override
Michael Jurka0280c3b2010-09-17 15:00:07 -0700661 public void removeAllViews() {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -0700662 mOccupied.clear();
Michael Jurkaa52570f2012-03-20 03:18:20 -0700663 mShortcutsAndWidgets.removeAllViews();
Michael Jurka0280c3b2010-09-17 15:00:07 -0700664 }
665
666 @Override
667 public void removeAllViewsInLayout() {
Michael Jurkaa52570f2012-03-20 03:18:20 -0700668 if (mShortcutsAndWidgets.getChildCount() > 0) {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -0700669 mOccupied.clear();
Michael Jurkaa52570f2012-03-20 03:18:20 -0700670 mShortcutsAndWidgets.removeAllViewsInLayout();
Michael Jurka7cfc2822011-08-02 20:19:24 -0700671 }
Michael Jurka0280c3b2010-09-17 15:00:07 -0700672 }
673
674 @Override
675 public void removeView(View view) {
676 markCellsAsUnoccupiedForView(view);
Michael Jurkaa52570f2012-03-20 03:18:20 -0700677 mShortcutsAndWidgets.removeView(view);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700678 }
679
680 @Override
681 public void removeViewAt(int index) {
Michael Jurkaa52570f2012-03-20 03:18:20 -0700682 markCellsAsUnoccupiedForView(mShortcutsAndWidgets.getChildAt(index));
683 mShortcutsAndWidgets.removeViewAt(index);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700684 }
685
686 @Override
687 public void removeViewInLayout(View view) {
688 markCellsAsUnoccupiedForView(view);
Michael Jurkaa52570f2012-03-20 03:18:20 -0700689 mShortcutsAndWidgets.removeViewInLayout(view);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700690 }
691
692 @Override
693 public void removeViews(int start, int count) {
694 for (int i = start; i < start + count; i++) {
Michael Jurkaa52570f2012-03-20 03:18:20 -0700695 markCellsAsUnoccupiedForView(mShortcutsAndWidgets.getChildAt(i));
Michael Jurka0280c3b2010-09-17 15:00:07 -0700696 }
Michael Jurkaa52570f2012-03-20 03:18:20 -0700697 mShortcutsAndWidgets.removeViews(start, count);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700698 }
699
700 @Override
701 public void removeViewsInLayout(int start, int count) {
702 for (int i = start; i < start + count; i++) {
Michael Jurkaa52570f2012-03-20 03:18:20 -0700703 markCellsAsUnoccupiedForView(mShortcutsAndWidgets.getChildAt(i));
Michael Jurka0280c3b2010-09-17 15:00:07 -0700704 }
Michael Jurkaa52570f2012-03-20 03:18:20 -0700705 mShortcutsAndWidgets.removeViewsInLayout(start, count);
Michael Jurkaabded662011-03-04 12:06:57 -0800706 }
707
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700708 /**
Winson Chungaafa03c2010-06-11 17:34:16 -0700709 * Given a point, return the cell that strictly encloses that point
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800710 * @param x X coordinate of the point
711 * @param y Y coordinate of the point
712 * @param result Array of 2 ints to hold the x and y coordinate of the cell
713 */
Sunny Goyale9b651e2015-04-24 11:44:51 -0700714 public void pointToCellExact(int x, int y, int[] result) {
Winson Chung4b825dcd2011-06-19 12:41:22 -0700715 final int hStartPadding = getPaddingLeft();
716 final int vStartPadding = getPaddingTop();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800717
718 result[0] = (x - hStartPadding) / (mCellWidth + mWidthGap);
719 result[1] = (y - vStartPadding) / (mCellHeight + mHeightGap);
720
Adam Cohend22015c2010-07-26 22:02:18 -0700721 final int xAxis = mCountX;
722 final int yAxis = mCountY;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800723
724 if (result[0] < 0) result[0] = 0;
725 if (result[0] >= xAxis) result[0] = xAxis - 1;
726 if (result[1] < 0) result[1] = 0;
727 if (result[1] >= yAxis) result[1] = yAxis - 1;
728 }
Winson Chungaafa03c2010-06-11 17:34:16 -0700729
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800730 /**
731 * Given a point, return the cell that most closely encloses that point
732 * @param x X coordinate of the point
733 * @param y Y coordinate of the point
734 * @param result Array of 2 ints to hold the x and y coordinate of the cell
735 */
736 void pointToCellRounded(int x, int y, int[] result) {
737 pointToCellExact(x + (mCellWidth / 2), y + (mCellHeight / 2), result);
738 }
739
740 /**
741 * Given a cell coordinate, return the point that represents the upper left corner of that cell
Winson Chungaafa03c2010-06-11 17:34:16 -0700742 *
743 * @param cellX X coordinate of the cell
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800744 * @param cellY Y coordinate of the cell
Winson Chungaafa03c2010-06-11 17:34:16 -0700745 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800746 * @param result Array of 2 ints to hold the x and y coordinate of the point
747 */
748 void cellToPoint(int cellX, int cellY, int[] result) {
Winson Chung4b825dcd2011-06-19 12:41:22 -0700749 final int hStartPadding = getPaddingLeft();
750 final int vStartPadding = getPaddingTop();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800751
752 result[0] = hStartPadding + cellX * (mCellWidth + mWidthGap);
753 result[1] = vStartPadding + cellY * (mCellHeight + mHeightGap);
754 }
755
Adam Cohene3e27a82011-04-15 12:07:39 -0700756 /**
Adam Cohen482ed822012-03-02 14:15:13 -0800757 * Given a cell coordinate, return the point that represents the center of the cell
Adam Cohene3e27a82011-04-15 12:07:39 -0700758 *
759 * @param cellX X coordinate of the cell
760 * @param cellY Y coordinate of the cell
761 *
762 * @param result Array of 2 ints to hold the x and y coordinate of the point
763 */
764 void cellToCenterPoint(int cellX, int cellY, int[] result) {
Adam Cohen47a876d2012-03-19 13:21:41 -0700765 regionToCenterPoint(cellX, cellY, 1, 1, result);
766 }
767
768 /**
769 * Given a cell coordinate and span return the point that represents the center of the regio
770 *
771 * @param cellX X coordinate of the cell
772 * @param cellY Y coordinate of the cell
773 *
774 * @param result Array of 2 ints to hold the x and y coordinate of the point
775 */
776 void regionToCenterPoint(int cellX, int cellY, int spanX, int spanY, int[] result) {
Winson Chung4b825dcd2011-06-19 12:41:22 -0700777 final int hStartPadding = getPaddingLeft();
778 final int vStartPadding = getPaddingTop();
Adam Cohen47a876d2012-03-19 13:21:41 -0700779 result[0] = hStartPadding + cellX * (mCellWidth + mWidthGap) +
780 (spanX * mCellWidth + (spanX - 1) * mWidthGap) / 2;
781 result[1] = vStartPadding + cellY * (mCellHeight + mHeightGap) +
782 (spanY * mCellHeight + (spanY - 1) * mHeightGap) / 2;
Adam Cohene3e27a82011-04-15 12:07:39 -0700783 }
784
Adam Cohen19f37922012-03-21 11:59:11 -0700785 /**
786 * Given a cell coordinate and span fills out a corresponding pixel rect
787 *
788 * @param cellX X coordinate of the cell
789 * @param cellY Y coordinate of the cell
790 * @param result Rect in which to write the result
791 */
792 void regionToRect(int cellX, int cellY, int spanX, int spanY, Rect result) {
793 final int hStartPadding = getPaddingLeft();
794 final int vStartPadding = getPaddingTop();
795 final int left = hStartPadding + cellX * (mCellWidth + mWidthGap);
796 final int top = vStartPadding + cellY * (mCellHeight + mHeightGap);
797 result.set(left, top, left + (spanX * mCellWidth + (spanX - 1) * mWidthGap),
798 top + (spanY * mCellHeight + (spanY - 1) * mHeightGap));
799 }
800
Adam Cohen482ed822012-03-02 14:15:13 -0800801 public float getDistanceFromCell(float x, float y, int[] cell) {
802 cellToCenterPoint(cell[0], cell[1], mTmpPoint);
Sunny Goyalf7a29e82015-04-24 15:20:43 -0700803 return (float) Math.hypot(x - mTmpPoint[0], y - mTmpPoint[1]);
Adam Cohen482ed822012-03-02 14:15:13 -0800804 }
805
Adam Cohenf9c184a2016-01-15 16:47:43 -0800806 public int getCellWidth() {
Romain Guy84f296c2009-11-04 15:00:44 -0800807 return mCellWidth;
808 }
809
810 int getCellHeight() {
811 return mCellHeight;
812 }
813
Adam Cohend4844c32011-02-18 19:25:06 -0800814 int getWidthGap() {
815 return mWidthGap;
816 }
817
818 int getHeightGap() {
819 return mHeightGap;
820 }
821
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700822 public void setFixedSize(int width, int height) {
823 mFixedWidth = width;
824 mFixedHeight = height;
825 }
826
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800827 @Override
828 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800829 int widthSpecMode = MeasureSpec.getMode(widthMeasureSpec);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800830 int heightSpecMode = MeasureSpec.getMode(heightMeasureSpec);
Winson Chung5f8afe62013-08-12 16:19:28 -0700831 int widthSize = MeasureSpec.getSize(widthMeasureSpec);
832 int heightSize = MeasureSpec.getSize(heightMeasureSpec);
Winson Chung2d75f122013-09-23 16:53:31 -0700833 int childWidthSize = widthSize - (getPaddingLeft() + getPaddingRight());
834 int childHeightSize = heightSize - (getPaddingTop() + getPaddingBottom());
Winson Chung11a1a532013-09-13 11:14:45 -0700835 if (mFixedCellWidth < 0 || mFixedCellHeight < 0) {
Sunny Goyalc6205602015-05-21 20:46:33 -0700836 int cw = DeviceProfile.calculateCellWidth(childWidthSize, mCountX);
837 int ch = DeviceProfile.calculateCellHeight(childHeightSize, mCountY);
Winson Chung11a1a532013-09-13 11:14:45 -0700838 if (cw != mCellWidth || ch != mCellHeight) {
839 mCellWidth = cw;
840 mCellHeight = ch;
841 mShortcutsAndWidgets.setCellDimensions(mCellWidth, mCellHeight, mWidthGap,
842 mHeightGap, mCountX, mCountY);
843 }
Winson Chung5f8afe62013-08-12 16:19:28 -0700844 }
Winson Chungaafa03c2010-06-11 17:34:16 -0700845
Winson Chung2d75f122013-09-23 16:53:31 -0700846 int newWidth = childWidthSize;
847 int newHeight = childHeightSize;
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700848 if (mFixedWidth > 0 && mFixedHeight > 0) {
849 newWidth = mFixedWidth;
850 newHeight = mFixedHeight;
851 } else if (widthSpecMode == MeasureSpec.UNSPECIFIED || heightSpecMode == MeasureSpec.UNSPECIFIED) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800852 throw new RuntimeException("CellLayout cannot have UNSPECIFIED dimensions");
853 }
854
Adam Cohend22015c2010-07-26 22:02:18 -0700855 int numWidthGaps = mCountX - 1;
856 int numHeightGaps = mCountY - 1;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800857
Adam Cohen234c4cd2011-07-17 21:03:04 -0700858 if (mOriginalWidthGap < 0 || mOriginalHeightGap < 0) {
Winson Chung2d75f122013-09-23 16:53:31 -0700859 int hSpace = childWidthSize;
860 int vSpace = childHeightSize;
Adam Cohenf4bd5792012-04-27 11:35:29 -0700861 int hFreeSpace = hSpace - (mCountX * mCellWidth);
862 int vFreeSpace = vSpace - (mCountY * mCellHeight);
Winson Chung4b825dcd2011-06-19 12:41:22 -0700863 mWidthGap = Math.min(mMaxGap, numWidthGaps > 0 ? (hFreeSpace / numWidthGaps) : 0);
864 mHeightGap = Math.min(mMaxGap,numHeightGaps > 0 ? (vFreeSpace / numHeightGaps) : 0);
Winson Chung5f8afe62013-08-12 16:19:28 -0700865 mShortcutsAndWidgets.setCellDimensions(mCellWidth, mCellHeight, mWidthGap,
866 mHeightGap, mCountX, mCountY);
Adam Cohen234c4cd2011-07-17 21:03:04 -0700867 } else {
868 mWidthGap = mOriginalWidthGap;
869 mHeightGap = mOriginalHeightGap;
Winson Chungece7f5b2010-10-22 14:54:12 -0700870 }
Sunny Goyal4fe5a372015-05-14 19:55:10 -0700871
872 // Make the feedback view large enough to hold the blur bitmap.
873 mTouchFeedbackView.measure(
874 MeasureSpec.makeMeasureSpec(mCellWidth + mTouchFeedbackView.getExtraSize(),
875 MeasureSpec.EXACTLY),
876 MeasureSpec.makeMeasureSpec(mCellHeight + mTouchFeedbackView.getExtraSize(),
877 MeasureSpec.EXACTLY));
878
879 mShortcutsAndWidgets.measure(
880 MeasureSpec.makeMeasureSpec(newWidth, MeasureSpec.EXACTLY),
881 MeasureSpec.makeMeasureSpec(newHeight, MeasureSpec.EXACTLY));
882
883 int maxWidth = mShortcutsAndWidgets.getMeasuredWidth();
884 int maxHeight = mShortcutsAndWidgets.getMeasuredHeight();
Winson Chung2d75f122013-09-23 16:53:31 -0700885 if (mFixedWidth > 0 && mFixedHeight > 0) {
886 setMeasuredDimension(maxWidth, maxHeight);
887 } else {
888 setMeasuredDimension(widthSize, heightSize);
889 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800890 }
891
892 @Override
Michael Jurka28750fb2010-09-24 17:43:49 -0700893 protected void onLayout(boolean changed, int l, int t, int r, int b) {
Tony Wickham26b01422015-11-10 14:44:32 -0800894 boolean isFullscreen = mShortcutsAndWidgets.getChildCount() > 0 &&
895 ((LayoutParams) mShortcutsAndWidgets.getChildAt(0).getLayoutParams()).isFullscreen;
896 int left = getPaddingLeft();
897 if (!isFullscreen) {
Tony Wickhama501d492015-11-03 18:05:01 -0800898 left += (int) Math.ceil(getUnusedHorizontalSpace() / 2f);
Tony Wickham26b01422015-11-10 14:44:32 -0800899 }
Sunny Goyal7c786f72016-06-01 14:08:21 -0700900 int right = r - l - getPaddingRight();
901 if (!isFullscreen) {
902 right -= (int) Math.ceil(getUnusedHorizontalSpace() / 2f);
903 }
904
Winson Chung38848ca2013-10-08 12:03:44 -0700905 int top = getPaddingTop();
Sunny Goyal7c786f72016-06-01 14:08:21 -0700906 int bottom = b - t - getPaddingBottom();
Sunny Goyal4fe5a372015-05-14 19:55:10 -0700907
908 mTouchFeedbackView.layout(left, top,
909 left + mTouchFeedbackView.getMeasuredWidth(),
910 top + mTouchFeedbackView.getMeasuredHeight());
Sunny Goyal7c786f72016-06-01 14:08:21 -0700911 mShortcutsAndWidgets.layout(left, top, right, bottom);
912
913 // Expand the background drawing bounds by the padding baked into the background drawable
914 mBackground.getPadding(mTempRect);
915 mBackground.setBounds(
916 left - mTempRect.left,
917 top - mTempRect.top,
918 right + mTempRect.right,
919 bottom + mTempRect.bottom);
920 }
921
Tony Wickhama501d492015-11-03 18:05:01 -0800922 /**
923 * Returns the amount of space left over after subtracting padding and cells. This space will be
924 * very small, a few pixels at most, and is a result of rounding down when calculating the cell
925 * width in {@link DeviceProfile#calculateCellWidth(int, int)}.
926 */
927 public int getUnusedHorizontalSpace() {
928 return getMeasuredWidth() - getPaddingLeft() - getPaddingRight() - (mCountX * mCellWidth);
929 }
930
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800931 @Override
932 protected void setChildrenDrawingCacheEnabled(boolean enabled) {
Michael Jurkaa52570f2012-03-20 03:18:20 -0700933 mShortcutsAndWidgets.setChildrenDrawingCacheEnabled(enabled);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800934 }
935
936 @Override
937 protected void setChildrenDrawnWithCacheEnabled(boolean enabled) {
Michael Jurkaa52570f2012-03-20 03:18:20 -0700938 mShortcutsAndWidgets.setChildrenDrawnWithCacheEnabled(enabled);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800939 }
940
Michael Jurka5f1c5092010-09-03 14:15:02 -0700941 public float getBackgroundAlpha() {
942 return mBackgroundAlpha;
Michael Jurkadee05892010-07-27 10:01:56 -0700943 }
944
Michael Jurka5f1c5092010-09-03 14:15:02 -0700945 public void setBackgroundAlpha(float alpha) {
Michael Jurkaafaa0502011-12-13 18:22:50 -0800946 if (mBackgroundAlpha != alpha) {
947 mBackgroundAlpha = alpha;
Sunny Goyal2805e632015-05-20 15:35:32 -0700948 mBackground.setAlpha((int) (mBackgroundAlpha * 255));
Michael Jurkaafaa0502011-12-13 18:22:50 -0800949 }
Michael Jurkadee05892010-07-27 10:01:56 -0700950 }
951
Sunny Goyal2805e632015-05-20 15:35:32 -0700952 @Override
953 protected boolean verifyDrawable(Drawable who) {
954 return super.verifyDrawable(who) || (mIsDragTarget && who == mBackground);
955 }
956
Michael Jurkaa52570f2012-03-20 03:18:20 -0700957 public void setShortcutAndWidgetAlpha(float alpha) {
Sunny Goyal02b50812014-09-10 15:44:42 -0700958 mShortcutsAndWidgets.setAlpha(alpha);
Michael Jurkadee05892010-07-27 10:01:56 -0700959 }
960
Michael Jurkaa52570f2012-03-20 03:18:20 -0700961 public ShortcutAndWidgetContainer getShortcutsAndWidgets() {
Sunny Goyaldcbcc862014-08-12 15:58:36 -0700962 return mShortcutsAndWidgets;
Michael Jurkaa52570f2012-03-20 03:18:20 -0700963 }
964
Patrick Dubroy440c3602010-07-13 17:50:32 -0700965 public View getChildAt(int x, int y) {
Michael Jurkaa52570f2012-03-20 03:18:20 -0700966 return mShortcutsAndWidgets.getChildAt(x, y);
Patrick Dubroy440c3602010-07-13 17:50:32 -0700967 }
968
Adam Cohen76fc0852011-06-17 13:26:23 -0700969 public boolean animateChildToPosition(final View child, int cellX, int cellY, int duration,
Adam Cohen482ed822012-03-02 14:15:13 -0800970 int delay, boolean permanent, boolean adjustOccupied) {
Michael Jurkaa52570f2012-03-20 03:18:20 -0700971 ShortcutAndWidgetContainer clc = getShortcutsAndWidgets();
Adam Cohen482ed822012-03-02 14:15:13 -0800972
Adam Cohen19f37922012-03-21 11:59:11 -0700973 if (clc.indexOfChild(child) != -1) {
Adam Cohenbfbfd262011-06-13 16:55:12 -0700974 final LayoutParams lp = (LayoutParams) child.getLayoutParams();
975 final ItemInfo info = (ItemInfo) child.getTag();
976
977 // We cancel any existing animations
978 if (mReorderAnimators.containsKey(lp)) {
979 mReorderAnimators.get(lp).cancel();
980 mReorderAnimators.remove(lp);
981 }
982
Adam Cohen482ed822012-03-02 14:15:13 -0800983 final int oldX = lp.x;
984 final int oldY = lp.y;
985 if (adjustOccupied) {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -0700986 GridOccupancy occupied = permanent ? mOccupied : mTmpOccupied;
987 occupied.markCells(lp.cellX, lp.cellY, lp.cellHSpan, lp.cellVSpan, false);
988 occupied.markCells(cellX, cellY, lp.cellHSpan, lp.cellVSpan, true);
Adam Cohen482ed822012-03-02 14:15:13 -0800989 }
Adam Cohenbfbfd262011-06-13 16:55:12 -0700990 lp.isLockedToGrid = true;
Adam Cohen482ed822012-03-02 14:15:13 -0800991 if (permanent) {
992 lp.cellX = info.cellX = cellX;
993 lp.cellY = info.cellY = cellY;
994 } else {
995 lp.tmpCellX = cellX;
996 lp.tmpCellY = cellY;
997 }
Adam Cohenbfbfd262011-06-13 16:55:12 -0700998 clc.setupLp(lp);
999 lp.isLockedToGrid = false;
Adam Cohen482ed822012-03-02 14:15:13 -08001000 final int newX = lp.x;
1001 final int newY = lp.y;
Adam Cohenbfbfd262011-06-13 16:55:12 -07001002
Adam Cohen76fc0852011-06-17 13:26:23 -07001003 lp.x = oldX;
1004 lp.y = oldY;
Adam Cohen76fc0852011-06-17 13:26:23 -07001005
Adam Cohen482ed822012-03-02 14:15:13 -08001006 // Exit early if we're not actually moving the view
1007 if (oldX == newX && oldY == newY) {
1008 lp.isLockedToGrid = true;
1009 return true;
1010 }
1011
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001012 ValueAnimator va = LauncherAnimUtils.ofFloat(child, 0f, 1f);
Adam Cohen482ed822012-03-02 14:15:13 -08001013 va.setDuration(duration);
1014 mReorderAnimators.put(lp, va);
1015
1016 va.addUpdateListener(new AnimatorUpdateListener() {
1017 @Override
Adam Cohenbfbfd262011-06-13 16:55:12 -07001018 public void onAnimationUpdate(ValueAnimator animation) {
Adam Cohen482ed822012-03-02 14:15:13 -08001019 float r = ((Float) animation.getAnimatedValue()).floatValue();
Adam Cohen19f37922012-03-21 11:59:11 -07001020 lp.x = (int) ((1 - r) * oldX + r * newX);
1021 lp.y = (int) ((1 - r) * oldY + r * newY);
Adam Cohen6b8a02d2012-03-22 15:13:40 -07001022 child.requestLayout();
Adam Cohenbfbfd262011-06-13 16:55:12 -07001023 }
1024 });
Adam Cohen482ed822012-03-02 14:15:13 -08001025 va.addListener(new AnimatorListenerAdapter() {
Adam Cohenbfbfd262011-06-13 16:55:12 -07001026 boolean cancelled = false;
1027 public void onAnimationEnd(Animator animation) {
1028 // If the animation was cancelled, it means that another animation
1029 // has interrupted this one, and we don't want to lock the item into
1030 // place just yet.
1031 if (!cancelled) {
1032 lp.isLockedToGrid = true;
Adam Cohen482ed822012-03-02 14:15:13 -08001033 child.requestLayout();
Adam Cohenbfbfd262011-06-13 16:55:12 -07001034 }
1035 if (mReorderAnimators.containsKey(lp)) {
1036 mReorderAnimators.remove(lp);
1037 }
1038 }
1039 public void onAnimationCancel(Animator animation) {
1040 cancelled = true;
1041 }
1042 });
Adam Cohen482ed822012-03-02 14:15:13 -08001043 va.setStartDelay(delay);
1044 va.start();
Adam Cohenbfbfd262011-06-13 16:55:12 -07001045 return true;
1046 }
1047 return false;
1048 }
1049
Tony Wickhama501d492015-11-03 18:05:01 -08001050 void visualizeDropLocation(View v, Bitmap dragOutline, int cellX, int cellY, int spanX,
1051 int spanY, boolean resize, DropTarget.DragObject dragObject) {
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -07001052 final int oldDragCellX = mDragCell[0];
1053 final int oldDragCellY = mDragCell[1];
Adam Cohen482ed822012-03-02 14:15:13 -08001054
Adam Cohen2801caf2011-05-13 20:57:39 -07001055 if (dragOutline == null && v == null) {
Adam Cohen2801caf2011-05-13 20:57:39 -07001056 return;
1057 }
1058
Adam Cohen482ed822012-03-02 14:15:13 -08001059 if (cellX != oldDragCellX || cellY != oldDragCellY) {
Sunny Goyale78e3d72015-09-24 11:23:31 -07001060 Point dragOffset = dragObject.dragView.getDragVisualizeOffset();
1061 Rect dragRegion = dragObject.dragView.getDragRegion();
1062
Adam Cohen482ed822012-03-02 14:15:13 -08001063 mDragCell[0] = cellX;
1064 mDragCell[1] = cellY;
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001065
Joe Onorato4be866d2010-10-10 11:26:02 -07001066 final int oldIndex = mDragOutlineCurrent;
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -07001067 mDragOutlineAnims[oldIndex].animateOut();
1068 mDragOutlineCurrent = (oldIndex + 1) % mDragOutlines.length;
Adam Cohend41fbf52012-02-16 23:53:59 -08001069 Rect r = mDragOutlines[mDragOutlineCurrent];
Sunny Goyal106bf642015-07-16 12:18:06 -07001070
Adam Cohend41fbf52012-02-16 23:53:59 -08001071 if (resize) {
Adam Cohen482ed822012-03-02 14:15:13 -08001072 cellToRect(cellX, cellY, spanX, spanY, r);
Sunny Goyal106bf642015-07-16 12:18:06 -07001073 } else {
1074 // Find the top left corner of the rect the object will occupy
1075 final int[] topLeft = mTmpPoint;
1076 cellToPoint(cellX, cellY, topLeft);
1077
1078 int left = topLeft[0];
1079 int top = topLeft[1];
1080
1081 if (v != null && dragOffset == null) {
1082 // When drawing the drag outline, it did not account for margin offsets
1083 // added by the view's parent.
1084 MarginLayoutParams lp = (MarginLayoutParams) v.getLayoutParams();
1085 left += lp.leftMargin;
1086 top += lp.topMargin;
1087
1088 // Offsets due to the size difference between the View and the dragOutline.
1089 // There is a size difference to account for the outer blur, which may lie
1090 // outside the bounds of the view.
1091 top += (v.getHeight() - dragOutline.getHeight()) / 2;
1092 // We center about the x axis
1093 left += ((mCellWidth * spanX) + ((spanX - 1) * mWidthGap)
1094 - dragOutline.getWidth()) / 2;
1095 } else {
1096 if (dragOffset != null && dragRegion != null) {
1097 // Center the drag region *horizontally* in the cell and apply a drag
1098 // outline offset
1099 left += dragOffset.x + ((mCellWidth * spanX) + ((spanX - 1) * mWidthGap)
1100 - dragRegion.width()) / 2;
1101 int cHeight = getShortcutsAndWidgets().getCellContentHeight();
1102 int cellPaddingY = (int) Math.max(0, ((mCellHeight - cHeight) / 2f));
1103 top += dragOffset.y + cellPaddingY;
1104 } else {
1105 // Center the drag outline in the cell
1106 left += ((mCellWidth * spanX) + ((spanX - 1) * mWidthGap)
1107 - dragOutline.getWidth()) / 2;
1108 top += ((mCellHeight * spanY) + ((spanY - 1) * mHeightGap)
1109 - dragOutline.getHeight()) / 2;
1110 }
1111 }
1112 r.set(left, top, left + dragOutline.getWidth(), top + dragOutline.getHeight());
Adam Cohend41fbf52012-02-16 23:53:59 -08001113 }
Winson Chung150fbab2010-09-29 17:14:26 -07001114
Sunny Goyal106bf642015-07-16 12:18:06 -07001115 Utilities.scaleRectAboutCenter(r, getChildrenScale());
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -07001116 mDragOutlineAnims[mDragOutlineCurrent].setTag(dragOutline);
1117 mDragOutlineAnims[mDragOutlineCurrent].animateIn();
Sunny Goyale78e3d72015-09-24 11:23:31 -07001118
1119 if (dragObject.stateAnnouncer != null) {
1120 String msg;
1121 if (isHotseat()) {
1122 msg = getContext().getString(R.string.move_to_hotseat_position,
1123 Math.max(cellX, cellY) + 1);
1124 } else {
1125 msg = getContext().getString(R.string.move_to_empty_cell,
1126 cellY + 1, cellX + 1);
1127 }
1128 dragObject.stateAnnouncer.announce(msg);
1129 }
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001130 }
1131 }
1132
Adam Cohene0310962011-04-18 16:15:31 -07001133 public void clearDragOutlines() {
1134 final int oldIndex = mDragOutlineCurrent;
1135 mDragOutlineAnims[oldIndex].animateOut();
Adam Cohend41fbf52012-02-16 23:53:59 -08001136 mDragCell[0] = mDragCell[1] = -1;
Adam Cohene0310962011-04-18 16:15:31 -07001137 }
1138
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001139 /**
Jeff Sharkey70864282009-04-07 21:08:40 -07001140 * Find a vacant area that will fit the given bounds nearest the requested
1141 * cell location. Uses Euclidean distance to score multiple vacant areas.
Winson Chungaafa03c2010-06-11 17:34:16 -07001142 *
Romain Guy51afc022009-05-04 18:03:43 -07001143 * @param pixelX The X location at which you want to search for a vacant area.
1144 * @param pixelY The Y location at which you want to search for a vacant area.
Adam Cohend41fbf52012-02-16 23:53:59 -08001145 * @param minSpanX The minimum horizontal span required
1146 * @param minSpanY The minimum vertical span required
1147 * @param spanX Horizontal span of the object.
1148 * @param spanY Vertical span of the object.
1149 * @param result Array in which to place the result, or null (in which case a new array will
1150 * be allocated)
1151 * @return The X, Y cell of a vacant area that can contain this object,
1152 * nearest the requested location.
1153 */
1154 int[] findNearestVacantArea(int pixelX, int pixelY, int minSpanX, int minSpanY, int spanX,
1155 int spanY, int[] result, int[] resultSpan) {
Sunny Goyalf7a29e82015-04-24 15:20:43 -07001156 return findNearestArea(pixelX, pixelY, minSpanX, minSpanY, spanX, spanY, true,
Adam Cohend41fbf52012-02-16 23:53:59 -08001157 result, resultSpan);
1158 }
1159
Adam Cohend41fbf52012-02-16 23:53:59 -08001160 private final Stack<Rect> mTempRectStack = new Stack<Rect>();
1161 private void lazyInitTempRectStack() {
1162 if (mTempRectStack.isEmpty()) {
1163 for (int i = 0; i < mCountX * mCountY; i++) {
1164 mTempRectStack.push(new Rect());
1165 }
1166 }
1167 }
Adam Cohen482ed822012-03-02 14:15:13 -08001168
Adam Cohend41fbf52012-02-16 23:53:59 -08001169 private void recycleTempRects(Stack<Rect> used) {
1170 while (!used.isEmpty()) {
1171 mTempRectStack.push(used.pop());
1172 }
1173 }
1174
1175 /**
1176 * Find a vacant area that will fit the given bounds nearest the requested
1177 * cell location. Uses Euclidean distance to score multiple vacant areas.
1178 *
1179 * @param pixelX The X location at which you want to search for a vacant area.
1180 * @param pixelY The Y location at which you want to search for a vacant area.
1181 * @param minSpanX The minimum horizontal span required
1182 * @param minSpanY The minimum vertical span required
1183 * @param spanX Horizontal span of the object.
1184 * @param spanY Vertical span of the object.
1185 * @param ignoreOccupied If true, the result can be an occupied cell
1186 * @param result Array in which to place the result, or null (in which case a new array will
1187 * be allocated)
1188 * @return The X, Y cell of a vacant area that can contain this object,
1189 * nearest the requested location.
1190 */
Sunny Goyalf7a29e82015-04-24 15:20:43 -07001191 private int[] findNearestArea(int pixelX, int pixelY, int minSpanX, int minSpanY, int spanX,
1192 int spanY, boolean ignoreOccupied, int[] result, int[] resultSpan) {
Adam Cohend41fbf52012-02-16 23:53:59 -08001193 lazyInitTempRectStack();
Michael Jurkac6ee42e2010-09-30 12:04:50 -07001194
Adam Cohene3e27a82011-04-15 12:07:39 -07001195 // For items with a spanX / spanY > 1, the passed in point (pixelX, pixelY) corresponds
1196 // to the center of the item, but we are searching based on the top-left cell, so
1197 // we translate the point over to correspond to the top-left.
1198 pixelX -= (mCellWidth + mWidthGap) * (spanX - 1) / 2f;
1199 pixelY -= (mCellHeight + mHeightGap) * (spanY - 1) / 2f;
1200
Jeff Sharkey70864282009-04-07 21:08:40 -07001201 // Keep track of best-scoring drop area
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001202 final int[] bestXY = result != null ? result : new int[2];
Jeff Sharkey70864282009-04-07 21:08:40 -07001203 double bestDistance = Double.MAX_VALUE;
Adam Cohend41fbf52012-02-16 23:53:59 -08001204 final Rect bestRect = new Rect(-1, -1, -1, -1);
1205 final Stack<Rect> validRegions = new Stack<Rect>();
Winson Chungaafa03c2010-06-11 17:34:16 -07001206
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001207 final int countX = mCountX;
1208 final int countY = mCountY;
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001209
Adam Cohend41fbf52012-02-16 23:53:59 -08001210 if (minSpanX <= 0 || minSpanY <= 0 || spanX <= 0 || spanY <= 0 ||
1211 spanX < minSpanX || spanY < minSpanY) {
1212 return bestXY;
1213 }
1214
1215 for (int y = 0; y < countY - (minSpanY - 1); y++) {
Michael Jurkac28de512010-08-13 11:27:44 -07001216 inner:
Adam Cohend41fbf52012-02-16 23:53:59 -08001217 for (int x = 0; x < countX - (minSpanX - 1); x++) {
1218 int ySize = -1;
1219 int xSize = -1;
Adam Cohendf035382011-04-11 17:22:04 -07001220 if (ignoreOccupied) {
Adam Cohend41fbf52012-02-16 23:53:59 -08001221 // First, let's see if this thing fits anywhere
1222 for (int i = 0; i < minSpanX; i++) {
1223 for (int j = 0; j < minSpanY; j++) {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001224 if (mOccupied.cells[x + i][y + j]) {
Adam Cohendf035382011-04-11 17:22:04 -07001225 continue inner;
1226 }
Michael Jurkac28de512010-08-13 11:27:44 -07001227 }
1228 }
Adam Cohend41fbf52012-02-16 23:53:59 -08001229 xSize = minSpanX;
1230 ySize = minSpanY;
1231
1232 // We know that the item will fit at _some_ acceptable size, now let's see
1233 // how big we can make it. We'll alternate between incrementing x and y spans
1234 // until we hit a limit.
1235 boolean incX = true;
1236 boolean hitMaxX = xSize >= spanX;
1237 boolean hitMaxY = ySize >= spanY;
1238 while (!(hitMaxX && hitMaxY)) {
1239 if (incX && !hitMaxX) {
1240 for (int j = 0; j < ySize; j++) {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001241 if (x + xSize > countX -1 || mOccupied.cells[x + xSize][y + j]) {
Adam Cohend41fbf52012-02-16 23:53:59 -08001242 // We can't move out horizontally
1243 hitMaxX = true;
1244 }
1245 }
1246 if (!hitMaxX) {
1247 xSize++;
1248 }
1249 } else if (!hitMaxY) {
1250 for (int i = 0; i < xSize; i++) {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001251 if (y + ySize > countY - 1 || mOccupied.cells[x + i][y + ySize]) {
Adam Cohend41fbf52012-02-16 23:53:59 -08001252 // We can't move out vertically
1253 hitMaxY = true;
1254 }
1255 }
1256 if (!hitMaxY) {
1257 ySize++;
1258 }
1259 }
1260 hitMaxX |= xSize >= spanX;
1261 hitMaxY |= ySize >= spanY;
1262 incX = !incX;
1263 }
1264 incX = true;
1265 hitMaxX = xSize >= spanX;
1266 hitMaxY = ySize >= spanY;
Michael Jurkac28de512010-08-13 11:27:44 -07001267 }
Sunny Goyal2805e632015-05-20 15:35:32 -07001268 final int[] cellXY = mTmpPoint;
Adam Cohene3e27a82011-04-15 12:07:39 -07001269 cellToCenterPoint(x, y, cellXY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001270
Adam Cohend41fbf52012-02-16 23:53:59 -08001271 // We verify that the current rect is not a sub-rect of any of our previous
1272 // candidates. In this case, the current rect is disqualified in favour of the
1273 // containing rect.
1274 Rect currentRect = mTempRectStack.pop();
1275 currentRect.set(x, y, x + xSize, y + ySize);
1276 boolean contained = false;
1277 for (Rect r : validRegions) {
1278 if (r.contains(currentRect)) {
1279 contained = true;
1280 break;
1281 }
1282 }
1283 validRegions.push(currentRect);
Sunny Goyalf7a29e82015-04-24 15:20:43 -07001284 double distance = Math.hypot(cellXY[0] - pixelX, cellXY[1] - pixelY);
Adam Cohen482ed822012-03-02 14:15:13 -08001285
Adam Cohend41fbf52012-02-16 23:53:59 -08001286 if ((distance <= bestDistance && !contained) ||
1287 currentRect.contains(bestRect)) {
Michael Jurkac28de512010-08-13 11:27:44 -07001288 bestDistance = distance;
1289 bestXY[0] = x;
1290 bestXY[1] = y;
Adam Cohend41fbf52012-02-16 23:53:59 -08001291 if (resultSpan != null) {
1292 resultSpan[0] = xSize;
1293 resultSpan[1] = ySize;
1294 }
1295 bestRect.set(currentRect);
Michael Jurkac28de512010-08-13 11:27:44 -07001296 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001297 }
1298 }
1299
Adam Cohenc0dcf592011-06-01 15:30:43 -07001300 // Return -1, -1 if no suitable location found
1301 if (bestDistance == Double.MAX_VALUE) {
1302 bestXY[0] = -1;
1303 bestXY[1] = -1;
Jeff Sharkey70864282009-04-07 21:08:40 -07001304 }
Adam Cohend41fbf52012-02-16 23:53:59 -08001305 recycleTempRects(validRegions);
Adam Cohenc0dcf592011-06-01 15:30:43 -07001306 return bestXY;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001307 }
Winson Chungaafa03c2010-06-11 17:34:16 -07001308
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001309 /**
Adam Cohen482ed822012-03-02 14:15:13 -08001310 * Find a vacant area that will fit the given bounds nearest the requested
1311 * cell location, and will also weigh in a suggested direction vector of the
1312 * desired location. This method computers distance based on unit grid distances,
1313 * not pixel distances.
1314 *
Adam Cohen47a876d2012-03-19 13:21:41 -07001315 * @param cellX The X cell nearest to which you want to search for a vacant area.
1316 * @param cellY The Y cell nearest which you want to search for a vacant area.
Adam Cohen482ed822012-03-02 14:15:13 -08001317 * @param spanX Horizontal span of the object.
1318 * @param spanY Vertical span of the object.
Adam Cohen47a876d2012-03-19 13:21:41 -07001319 * @param direction The favored direction in which the views should move from x, y
Sunny Goyal9eba1fd2015-10-16 08:58:57 -07001320 * @param occupied The array which represents which cells in the CellLayout are occupied
Adam Cohen47a876d2012-03-19 13:21:41 -07001321 * @param blockOccupied The array which represents which cells in the specified block (cellX,
Winson Chung5f8afe62013-08-12 16:19:28 -07001322 * cellY, spanX, spanY) are occupied. This is used when try to move a group of views.
Adam Cohen482ed822012-03-02 14:15:13 -08001323 * @param result Array in which to place the result, or null (in which case a new array will
1324 * be allocated)
1325 * @return The X, Y cell of a vacant area that can contain this object,
1326 * nearest the requested location.
1327 */
1328 private int[] findNearestArea(int cellX, int cellY, int spanX, int spanY, int[] direction,
Adam Cohen47a876d2012-03-19 13:21:41 -07001329 boolean[][] occupied, boolean blockOccupied[][], int[] result) {
Adam Cohen482ed822012-03-02 14:15:13 -08001330 // Keep track of best-scoring drop area
1331 final int[] bestXY = result != null ? result : new int[2];
1332 float bestDistance = Float.MAX_VALUE;
1333 int bestDirectionScore = Integer.MIN_VALUE;
1334
1335 final int countX = mCountX;
1336 final int countY = mCountY;
1337
1338 for (int y = 0; y < countY - (spanY - 1); y++) {
1339 inner:
1340 for (int x = 0; x < countX - (spanX - 1); x++) {
1341 // First, let's see if this thing fits anywhere
1342 for (int i = 0; i < spanX; i++) {
1343 for (int j = 0; j < spanY; j++) {
Adam Cohen47a876d2012-03-19 13:21:41 -07001344 if (occupied[x + i][y + j] && (blockOccupied == null || blockOccupied[i][j])) {
Adam Cohen482ed822012-03-02 14:15:13 -08001345 continue inner;
1346 }
1347 }
1348 }
1349
Sunny Goyalf7a29e82015-04-24 15:20:43 -07001350 float distance = (float) Math.hypot(x - cellX, y - cellY);
Adam Cohen482ed822012-03-02 14:15:13 -08001351 int[] curDirection = mTmpPoint;
Adam Cohen47a876d2012-03-19 13:21:41 -07001352 computeDirectionVector(x - cellX, y - cellY, curDirection);
1353 // The direction score is just the dot product of the two candidate direction
1354 // and that passed in.
Adam Cohen482ed822012-03-02 14:15:13 -08001355 int curDirectionScore = direction[0] * curDirection[0] +
1356 direction[1] * curDirection[1];
Adam Cohen47a876d2012-03-19 13:21:41 -07001357 boolean exactDirectionOnly = false;
1358 boolean directionMatches = direction[0] == curDirection[0] &&
1359 direction[0] == curDirection[0];
1360 if ((directionMatches || !exactDirectionOnly) &&
1361 Float.compare(distance, bestDistance) < 0 || (Float.compare(distance,
Adam Cohen482ed822012-03-02 14:15:13 -08001362 bestDistance) == 0 && curDirectionScore > bestDirectionScore)) {
1363 bestDistance = distance;
1364 bestDirectionScore = curDirectionScore;
1365 bestXY[0] = x;
1366 bestXY[1] = y;
1367 }
1368 }
1369 }
1370
1371 // Return -1, -1 if no suitable location found
1372 if (bestDistance == Float.MAX_VALUE) {
1373 bestXY[0] = -1;
1374 bestXY[1] = -1;
1375 }
1376 return bestXY;
1377 }
1378
1379 private boolean addViewToTempLocation(View v, Rect rectOccupiedByPotentialDrop,
Adam Cohen8baab352012-03-20 17:39:21 -07001380 int[] direction, ItemConfiguration currentState) {
1381 CellAndSpan c = currentState.map.get(v);
Adam Cohen482ed822012-03-02 14:15:13 -08001382 boolean success = false;
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001383 mTmpOccupied.markCells(c, false);
1384 mTmpOccupied.markCells(rectOccupiedByPotentialDrop, true);
Adam Cohen482ed822012-03-02 14:15:13 -08001385
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001386 findNearestArea(c.cellX, c.cellY, c.spanX, c.spanY, direction,
1387 mTmpOccupied.cells, null, mTempLocation);
Adam Cohen482ed822012-03-02 14:15:13 -08001388
1389 if (mTempLocation[0] >= 0 && mTempLocation[1] >= 0) {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001390 c.cellX = mTempLocation[0];
1391 c.cellY = mTempLocation[1];
Adam Cohen482ed822012-03-02 14:15:13 -08001392 success = true;
Adam Cohen482ed822012-03-02 14:15:13 -08001393 }
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001394 mTmpOccupied.markCells(c, true);
Adam Cohen482ed822012-03-02 14:15:13 -08001395 return success;
1396 }
1397
Adam Cohenf3900c22012-11-16 18:28:11 -08001398 /**
1399 * This helper class defines a cluster of views. It helps with defining complex edges
1400 * of the cluster and determining how those edges interact with other views. The edges
1401 * essentially define a fine-grained boundary around the cluster of views -- like a more
1402 * precise version of a bounding box.
1403 */
1404 private class ViewCluster {
Sunny Goyal6dc98b92016-04-03 15:25:29 -07001405 final static int LEFT = 1 << 0;
1406 final static int TOP = 1 << 1;
1407 final static int RIGHT = 1 << 2;
1408 final static int BOTTOM = 1 << 3;
Adam Cohen47a876d2012-03-19 13:21:41 -07001409
Adam Cohenf3900c22012-11-16 18:28:11 -08001410 ArrayList<View> views;
1411 ItemConfiguration config;
1412 Rect boundingRect = new Rect();
Adam Cohen47a876d2012-03-19 13:21:41 -07001413
Adam Cohenf3900c22012-11-16 18:28:11 -08001414 int[] leftEdge = new int[mCountY];
1415 int[] rightEdge = new int[mCountY];
1416 int[] topEdge = new int[mCountX];
1417 int[] bottomEdge = new int[mCountX];
Sunny Goyal6dc98b92016-04-03 15:25:29 -07001418 int dirtyEdges;
1419 boolean boundingRectDirty;
Adam Cohenf3900c22012-11-16 18:28:11 -08001420
1421 @SuppressWarnings("unchecked")
1422 public ViewCluster(ArrayList<View> views, ItemConfiguration config) {
1423 this.views = (ArrayList<View>) views.clone();
1424 this.config = config;
1425 resetEdges();
Adam Cohen47a876d2012-03-19 13:21:41 -07001426 }
1427
Adam Cohenf3900c22012-11-16 18:28:11 -08001428 void resetEdges() {
1429 for (int i = 0; i < mCountX; i++) {
1430 topEdge[i] = -1;
1431 bottomEdge[i] = -1;
1432 }
1433 for (int i = 0; i < mCountY; i++) {
1434 leftEdge[i] = -1;
1435 rightEdge[i] = -1;
1436 }
Sunny Goyal6dc98b92016-04-03 15:25:29 -07001437 dirtyEdges = LEFT | TOP | RIGHT | BOTTOM;
Adam Cohenf3900c22012-11-16 18:28:11 -08001438 boundingRectDirty = true;
1439 }
Adam Cohen47a876d2012-03-19 13:21:41 -07001440
Sunny Goyal6dc98b92016-04-03 15:25:29 -07001441 void computeEdge(int which) {
Adam Cohenf3900c22012-11-16 18:28:11 -08001442 int count = views.size();
1443 for (int i = 0; i < count; i++) {
1444 CellAndSpan cs = config.map.get(views.get(i));
1445 switch (which) {
1446 case LEFT:
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001447 int left = cs.cellX;
1448 for (int j = cs.cellY; j < cs.cellY + cs.spanY; j++) {
Sunny Goyal6dc98b92016-04-03 15:25:29 -07001449 if (left < leftEdge[j] || leftEdge[j] < 0) {
1450 leftEdge[j] = left;
Adam Cohena56dc102012-07-13 13:41:42 -07001451 }
Adam Cohen47a876d2012-03-19 13:21:41 -07001452 }
Adam Cohenf3900c22012-11-16 18:28:11 -08001453 break;
1454 case RIGHT:
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001455 int right = cs.cellX + cs.spanX;
1456 for (int j = cs.cellY; j < cs.cellY + cs.spanY; j++) {
Sunny Goyal6dc98b92016-04-03 15:25:29 -07001457 if (right > rightEdge[j]) {
1458 rightEdge[j] = right;
Adam Cohenf3900c22012-11-16 18:28:11 -08001459 }
1460 }
1461 break;
1462 case TOP:
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001463 int top = cs.cellY;
1464 for (int j = cs.cellX; j < cs.cellX + cs.spanX; j++) {
Sunny Goyal6dc98b92016-04-03 15:25:29 -07001465 if (top < topEdge[j] || topEdge[j] < 0) {
1466 topEdge[j] = top;
Adam Cohenf3900c22012-11-16 18:28:11 -08001467 }
1468 }
1469 break;
1470 case BOTTOM:
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001471 int bottom = cs.cellY + cs.spanY;
1472 for (int j = cs.cellX; j < cs.cellX + cs.spanX; j++) {
Sunny Goyal6dc98b92016-04-03 15:25:29 -07001473 if (bottom > bottomEdge[j]) {
1474 bottomEdge[j] = bottom;
Adam Cohenf3900c22012-11-16 18:28:11 -08001475 }
1476 }
1477 break;
Adam Cohen47a876d2012-03-19 13:21:41 -07001478 }
1479 }
1480 }
Adam Cohenf3900c22012-11-16 18:28:11 -08001481
1482 boolean isViewTouchingEdge(View v, int whichEdge) {
1483 CellAndSpan cs = config.map.get(v);
1484
Sunny Goyal6dc98b92016-04-03 15:25:29 -07001485 if ((dirtyEdges & whichEdge) == whichEdge) {
1486 computeEdge(whichEdge);
1487 dirtyEdges &= ~whichEdge;
1488 }
Adam Cohenf3900c22012-11-16 18:28:11 -08001489
1490 switch (whichEdge) {
1491 case LEFT:
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001492 for (int i = cs.cellY; i < cs.cellY + cs.spanY; i++) {
1493 if (leftEdge[i] == cs.cellX + cs.spanX) {
Adam Cohenf3900c22012-11-16 18:28:11 -08001494 return true;
1495 }
1496 }
1497 break;
1498 case RIGHT:
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001499 for (int i = cs.cellY; i < cs.cellY + cs.spanY; i++) {
1500 if (rightEdge[i] == cs.cellX) {
Adam Cohenf3900c22012-11-16 18:28:11 -08001501 return true;
1502 }
1503 }
1504 break;
1505 case TOP:
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001506 for (int i = cs.cellX; i < cs.cellX + cs.spanX; i++) {
1507 if (topEdge[i] == cs.cellY + cs.spanY) {
Adam Cohenf3900c22012-11-16 18:28:11 -08001508 return true;
1509 }
1510 }
1511 break;
1512 case BOTTOM:
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001513 for (int i = cs.cellX; i < cs.cellX + cs.spanX; i++) {
1514 if (bottomEdge[i] == cs.cellY) {
Adam Cohenf3900c22012-11-16 18:28:11 -08001515 return true;
1516 }
1517 }
1518 break;
1519 }
1520 return false;
1521 }
1522
1523 void shift(int whichEdge, int delta) {
1524 for (View v: views) {
1525 CellAndSpan c = config.map.get(v);
1526 switch (whichEdge) {
1527 case LEFT:
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001528 c.cellX -= delta;
Adam Cohenf3900c22012-11-16 18:28:11 -08001529 break;
1530 case RIGHT:
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001531 c.cellX += delta;
Adam Cohenf3900c22012-11-16 18:28:11 -08001532 break;
1533 case TOP:
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001534 c.cellY -= delta;
Adam Cohenf3900c22012-11-16 18:28:11 -08001535 break;
1536 case BOTTOM:
1537 default:
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001538 c.cellY += delta;
Adam Cohenf3900c22012-11-16 18:28:11 -08001539 break;
1540 }
1541 }
1542 resetEdges();
1543 }
1544
1545 public void addView(View v) {
1546 views.add(v);
1547 resetEdges();
1548 }
1549
1550 public Rect getBoundingRect() {
1551 if (boundingRectDirty) {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001552 config.getBoundingRectForViews(views, boundingRect);
Adam Cohenf3900c22012-11-16 18:28:11 -08001553 }
1554 return boundingRect;
1555 }
1556
Adam Cohenf3900c22012-11-16 18:28:11 -08001557 PositionComparator comparator = new PositionComparator();
1558 class PositionComparator implements Comparator<View> {
1559 int whichEdge = 0;
1560 public int compare(View left, View right) {
1561 CellAndSpan l = config.map.get(left);
1562 CellAndSpan r = config.map.get(right);
1563 switch (whichEdge) {
1564 case LEFT:
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001565 return (r.cellX + r.spanX) - (l.cellX + l.spanX);
Adam Cohenf3900c22012-11-16 18:28:11 -08001566 case RIGHT:
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001567 return l.cellX - r.cellX;
Adam Cohenf3900c22012-11-16 18:28:11 -08001568 case TOP:
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001569 return (r.cellY + r.spanY) - (l.cellY + l.spanY);
Adam Cohenf3900c22012-11-16 18:28:11 -08001570 case BOTTOM:
1571 default:
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001572 return l.cellY - r.cellY;
Adam Cohenf3900c22012-11-16 18:28:11 -08001573 }
1574 }
1575 }
1576
1577 public void sortConfigurationForEdgePush(int edge) {
1578 comparator.whichEdge = edge;
1579 Collections.sort(config.sortedViews, comparator);
1580 }
Adam Cohen47a876d2012-03-19 13:21:41 -07001581 }
1582
Adam Cohenf3900c22012-11-16 18:28:11 -08001583 private boolean pushViewsToTempLocation(ArrayList<View> views, Rect rectOccupiedByPotentialDrop,
1584 int[] direction, View dragView, ItemConfiguration currentState) {
Adam Cohene0489502012-08-27 15:18:53 -07001585
Adam Cohenf3900c22012-11-16 18:28:11 -08001586 ViewCluster cluster = new ViewCluster(views, currentState);
1587 Rect clusterRect = cluster.getBoundingRect();
1588 int whichEdge;
1589 int pushDistance;
1590 boolean fail = false;
1591
1592 // Determine the edge of the cluster that will be leading the push and how far
1593 // the cluster must be shifted.
1594 if (direction[0] < 0) {
1595 whichEdge = ViewCluster.LEFT;
1596 pushDistance = clusterRect.right - rectOccupiedByPotentialDrop.left;
Adam Cohene0489502012-08-27 15:18:53 -07001597 } else if (direction[0] > 0) {
Adam Cohenf3900c22012-11-16 18:28:11 -08001598 whichEdge = ViewCluster.RIGHT;
1599 pushDistance = rectOccupiedByPotentialDrop.right - clusterRect.left;
1600 } else if (direction[1] < 0) {
1601 whichEdge = ViewCluster.TOP;
1602 pushDistance = clusterRect.bottom - rectOccupiedByPotentialDrop.top;
1603 } else {
1604 whichEdge = ViewCluster.BOTTOM;
1605 pushDistance = rectOccupiedByPotentialDrop.bottom - clusterRect.top;
Adam Cohene0489502012-08-27 15:18:53 -07001606 }
1607
Adam Cohenf3900c22012-11-16 18:28:11 -08001608 // Break early for invalid push distance.
1609 if (pushDistance <= 0) {
1610 return false;
Adam Cohene0489502012-08-27 15:18:53 -07001611 }
Adam Cohenf3900c22012-11-16 18:28:11 -08001612
1613 // Mark the occupied state as false for the group of views we want to move.
1614 for (View v: views) {
1615 CellAndSpan c = currentState.map.get(v);
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001616 mTmpOccupied.markCells(c, false);
Adam Cohenf3900c22012-11-16 18:28:11 -08001617 }
1618
1619 // We save the current configuration -- if we fail to find a solution we will revert
1620 // to the initial state. The process of finding a solution modifies the configuration
1621 // in place, hence the need for revert in the failure case.
1622 currentState.save();
1623
1624 // The pushing algorithm is simplified by considering the views in the order in which
1625 // they would be pushed by the cluster. For example, if the cluster is leading with its
1626 // left edge, we consider sort the views by their right edge, from right to left.
1627 cluster.sortConfigurationForEdgePush(whichEdge);
1628
1629 while (pushDistance > 0 && !fail) {
1630 for (View v: currentState.sortedViews) {
1631 // For each view that isn't in the cluster, we see if the leading edge of the
1632 // cluster is contacting the edge of that view. If so, we add that view to the
1633 // cluster.
1634 if (!cluster.views.contains(v) && v != dragView) {
1635 if (cluster.isViewTouchingEdge(v, whichEdge)) {
1636 LayoutParams lp = (LayoutParams) v.getLayoutParams();
1637 if (!lp.canReorder) {
1638 // The push solution includes the all apps button, this is not viable.
1639 fail = true;
1640 break;
1641 }
1642 cluster.addView(v);
1643 CellAndSpan c = currentState.map.get(v);
1644
1645 // Adding view to cluster, mark it as not occupied.
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001646 mTmpOccupied.markCells(c, false);
Adam Cohenf3900c22012-11-16 18:28:11 -08001647 }
1648 }
1649 }
1650 pushDistance--;
1651
1652 // The cluster has been completed, now we move the whole thing over in the appropriate
1653 // direction.
1654 cluster.shift(whichEdge, 1);
1655 }
1656
1657 boolean foundSolution = false;
1658 clusterRect = cluster.getBoundingRect();
1659
1660 // Due to the nature of the algorithm, the only check required to verify a valid solution
1661 // is to ensure that completed shifted cluster lies completely within the cell layout.
1662 if (!fail && clusterRect.left >= 0 && clusterRect.right <= mCountX && clusterRect.top >= 0 &&
1663 clusterRect.bottom <= mCountY) {
1664 foundSolution = true;
1665 } else {
1666 currentState.restore();
1667 }
1668
1669 // In either case, we set the occupied array as marked for the location of the views
1670 for (View v: cluster.views) {
1671 CellAndSpan c = currentState.map.get(v);
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001672 mTmpOccupied.markCells(c, true);
Adam Cohenf3900c22012-11-16 18:28:11 -08001673 }
1674
1675 return foundSolution;
Adam Cohene0489502012-08-27 15:18:53 -07001676 }
1677
Adam Cohen482ed822012-03-02 14:15:13 -08001678 private boolean addViewsToTempLocation(ArrayList<View> views, Rect rectOccupiedByPotentialDrop,
Adam Cohenf3900c22012-11-16 18:28:11 -08001679 int[] direction, View dragView, ItemConfiguration currentState) {
Adam Cohen482ed822012-03-02 14:15:13 -08001680 if (views.size() == 0) return true;
Adam Cohen482ed822012-03-02 14:15:13 -08001681
Adam Cohen8baab352012-03-20 17:39:21 -07001682 boolean success = false;
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001683 Rect boundingRect = new Rect();
Adam Cohen8baab352012-03-20 17:39:21 -07001684 // We construct a rect which represents the entire group of views passed in
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001685 currentState.getBoundingRectForViews(views, boundingRect);
Adam Cohen8baab352012-03-20 17:39:21 -07001686
Adam Cohen8baab352012-03-20 17:39:21 -07001687 // Mark the occupied state as false for the group of views we want to move.
Adam Cohenf3900c22012-11-16 18:28:11 -08001688 for (View v: views) {
Adam Cohen8baab352012-03-20 17:39:21 -07001689 CellAndSpan c = currentState.map.get(v);
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001690 mTmpOccupied.markCells(c, false);
Adam Cohen8baab352012-03-20 17:39:21 -07001691 }
1692
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001693 GridOccupancy blockOccupied = new GridOccupancy(boundingRect.width(), boundingRect.height());
Adam Cohen47a876d2012-03-19 13:21:41 -07001694 int top = boundingRect.top;
1695 int left = boundingRect.left;
Adam Cohen8baab352012-03-20 17:39:21 -07001696 // We mark more precisely which parts of the bounding rect are truly occupied, allowing
Adam Cohena56dc102012-07-13 13:41:42 -07001697 // for interlocking.
Adam Cohenf3900c22012-11-16 18:28:11 -08001698 for (View v: views) {
Adam Cohen8baab352012-03-20 17:39:21 -07001699 CellAndSpan c = currentState.map.get(v);
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001700 blockOccupied.markCells(c.cellX - left, c.cellY - top, c.spanX, c.spanY, true);
Adam Cohen47a876d2012-03-19 13:21:41 -07001701 }
1702
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001703 mTmpOccupied.markCells(rectOccupiedByPotentialDrop, true);
Adam Cohen482ed822012-03-02 14:15:13 -08001704
Adam Cohenf3900c22012-11-16 18:28:11 -08001705 findNearestArea(boundingRect.left, boundingRect.top, boundingRect.width(),
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001706 boundingRect.height(), direction,
1707 mTmpOccupied.cells, blockOccupied.cells, mTempLocation);
Adam Cohen482ed822012-03-02 14:15:13 -08001708
Adam Cohen8baab352012-03-20 17:39:21 -07001709 // If we successfuly found a location by pushing the block of views, we commit it
Adam Cohen482ed822012-03-02 14:15:13 -08001710 if (mTempLocation[0] >= 0 && mTempLocation[1] >= 0) {
Adam Cohen8baab352012-03-20 17:39:21 -07001711 int deltaX = mTempLocation[0] - boundingRect.left;
1712 int deltaY = mTempLocation[1] - boundingRect.top;
Adam Cohenf3900c22012-11-16 18:28:11 -08001713 for (View v: views) {
Adam Cohen8baab352012-03-20 17:39:21 -07001714 CellAndSpan c = currentState.map.get(v);
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001715 c.cellX += deltaX;
1716 c.cellY += deltaY;
Adam Cohen482ed822012-03-02 14:15:13 -08001717 }
1718 success = true;
1719 }
Adam Cohen8baab352012-03-20 17:39:21 -07001720
1721 // In either case, we set the occupied array as marked for the location of the views
Adam Cohenf3900c22012-11-16 18:28:11 -08001722 for (View v: views) {
Adam Cohen8baab352012-03-20 17:39:21 -07001723 CellAndSpan c = currentState.map.get(v);
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001724 mTmpOccupied.markCells(c, true);
Adam Cohen482ed822012-03-02 14:15:13 -08001725 }
1726 return success;
1727 }
1728
Adam Cohen4abc5bd2012-05-29 21:06:03 -07001729 // This method tries to find a reordering solution which satisfies the push mechanic by trying
1730 // to push items in each of the cardinal directions, in an order based on the direction vector
1731 // passed.
1732 private boolean attemptPushInDirection(ArrayList<View> intersectingViews, Rect occupied,
1733 int[] direction, View ignoreView, ItemConfiguration solution) {
1734 if ((Math.abs(direction[0]) + Math.abs(direction[1])) > 1) {
Winson Chung5f8afe62013-08-12 16:19:28 -07001735 // If the direction vector has two non-zero components, we try pushing
Adam Cohen4abc5bd2012-05-29 21:06:03 -07001736 // separately in each of the components.
1737 int temp = direction[1];
1738 direction[1] = 0;
Adam Cohenf3900c22012-11-16 18:28:11 -08001739
1740 if (pushViewsToTempLocation(intersectingViews, occupied, direction,
Adam Cohen4abc5bd2012-05-29 21:06:03 -07001741 ignoreView, solution)) {
1742 return true;
1743 }
1744 direction[1] = temp;
1745 temp = direction[0];
1746 direction[0] = 0;
Adam Cohenf3900c22012-11-16 18:28:11 -08001747
1748 if (pushViewsToTempLocation(intersectingViews, occupied, direction,
Adam Cohen4abc5bd2012-05-29 21:06:03 -07001749 ignoreView, solution)) {
1750 return true;
1751 }
1752 // Revert the direction
1753 direction[0] = temp;
1754
1755 // Now we try pushing in each component of the opposite direction
1756 direction[0] *= -1;
1757 direction[1] *= -1;
1758 temp = direction[1];
1759 direction[1] = 0;
Adam Cohenf3900c22012-11-16 18:28:11 -08001760 if (pushViewsToTempLocation(intersectingViews, occupied, direction,
Adam Cohen4abc5bd2012-05-29 21:06:03 -07001761 ignoreView, solution)) {
1762 return true;
1763 }
1764
1765 direction[1] = temp;
1766 temp = direction[0];
1767 direction[0] = 0;
Adam Cohenf3900c22012-11-16 18:28:11 -08001768 if (pushViewsToTempLocation(intersectingViews, occupied, direction,
Adam Cohen4abc5bd2012-05-29 21:06:03 -07001769 ignoreView, solution)) {
1770 return true;
1771 }
1772 // revert the direction
1773 direction[0] = temp;
1774 direction[0] *= -1;
1775 direction[1] *= -1;
Winson Chung5f8afe62013-08-12 16:19:28 -07001776
Adam Cohen4abc5bd2012-05-29 21:06:03 -07001777 } else {
1778 // If the direction vector has a single non-zero component, we push first in the
1779 // direction of the vector
Adam Cohenf3900c22012-11-16 18:28:11 -08001780 if (pushViewsToTempLocation(intersectingViews, occupied, direction,
Adam Cohen4abc5bd2012-05-29 21:06:03 -07001781 ignoreView, solution)) {
1782 return true;
1783 }
Adam Cohen4abc5bd2012-05-29 21:06:03 -07001784 // Then we try the opposite direction
1785 direction[0] *= -1;
1786 direction[1] *= -1;
Adam Cohenf3900c22012-11-16 18:28:11 -08001787 if (pushViewsToTempLocation(intersectingViews, occupied, direction,
Adam Cohen4abc5bd2012-05-29 21:06:03 -07001788 ignoreView, solution)) {
1789 return true;
1790 }
1791 // Switch the direction back
1792 direction[0] *= -1;
1793 direction[1] *= -1;
Winson Chung5f8afe62013-08-12 16:19:28 -07001794
1795 // If we have failed to find a push solution with the above, then we try
Adam Cohen4abc5bd2012-05-29 21:06:03 -07001796 // to find a solution by pushing along the perpendicular axis.
1797
1798 // Swap the components
1799 int temp = direction[1];
1800 direction[1] = direction[0];
1801 direction[0] = temp;
Adam Cohenf3900c22012-11-16 18:28:11 -08001802 if (pushViewsToTempLocation(intersectingViews, occupied, direction,
Adam Cohen4abc5bd2012-05-29 21:06:03 -07001803 ignoreView, solution)) {
1804 return true;
1805 }
1806
1807 // Then we try the opposite direction
1808 direction[0] *= -1;
1809 direction[1] *= -1;
Adam Cohenf3900c22012-11-16 18:28:11 -08001810 if (pushViewsToTempLocation(intersectingViews, occupied, direction,
Adam Cohen4abc5bd2012-05-29 21:06:03 -07001811 ignoreView, solution)) {
1812 return true;
1813 }
1814 // Switch the direction back
1815 direction[0] *= -1;
1816 direction[1] *= -1;
1817
1818 // Swap the components back
1819 temp = direction[1];
1820 direction[1] = direction[0];
1821 direction[0] = temp;
1822 }
1823 return false;
1824 }
1825
Adam Cohen482ed822012-03-02 14:15:13 -08001826 private boolean rearrangementExists(int cellX, int cellY, int spanX, int spanY, int[] direction,
Adam Cohen8baab352012-03-20 17:39:21 -07001827 View ignoreView, ItemConfiguration solution) {
Winson Chunge3e03bc2012-05-01 15:10:11 -07001828 // Return early if get invalid cell positions
1829 if (cellX < 0 || cellY < 0) return false;
Adam Cohen482ed822012-03-02 14:15:13 -08001830
Adam Cohen8baab352012-03-20 17:39:21 -07001831 mIntersectingViews.clear();
Adam Cohen482ed822012-03-02 14:15:13 -08001832 mOccupiedRect.set(cellX, cellY, cellX + spanX, cellY + spanY);
Adam Cohen482ed822012-03-02 14:15:13 -08001833
Adam Cohen8baab352012-03-20 17:39:21 -07001834 // Mark the desired location of the view currently being dragged.
Adam Cohen482ed822012-03-02 14:15:13 -08001835 if (ignoreView != null) {
Adam Cohen8baab352012-03-20 17:39:21 -07001836 CellAndSpan c = solution.map.get(ignoreView);
Adam Cohen19f37922012-03-21 11:59:11 -07001837 if (c != null) {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001838 c.cellX = cellX;
1839 c.cellY = cellY;
Adam Cohen19f37922012-03-21 11:59:11 -07001840 }
Adam Cohen482ed822012-03-02 14:15:13 -08001841 }
Adam Cohen482ed822012-03-02 14:15:13 -08001842 Rect r0 = new Rect(cellX, cellY, cellX + spanX, cellY + spanY);
1843 Rect r1 = new Rect();
Adam Cohen8baab352012-03-20 17:39:21 -07001844 for (View child: solution.map.keySet()) {
Adam Cohen482ed822012-03-02 14:15:13 -08001845 if (child == ignoreView) continue;
Adam Cohen8baab352012-03-20 17:39:21 -07001846 CellAndSpan c = solution.map.get(child);
Adam Cohen482ed822012-03-02 14:15:13 -08001847 LayoutParams lp = (LayoutParams) child.getLayoutParams();
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001848 r1.set(c.cellX, c.cellY, c.cellX + c.spanX, c.cellY + c.spanY);
Adam Cohen482ed822012-03-02 14:15:13 -08001849 if (Rect.intersects(r0, r1)) {
1850 if (!lp.canReorder) {
1851 return false;
1852 }
1853 mIntersectingViews.add(child);
1854 }
1855 }
Adam Cohen47a876d2012-03-19 13:21:41 -07001856
Adam Cohenfa3c58f2013-12-06 16:10:55 -08001857 solution.intersectingViews = new ArrayList<View>(mIntersectingViews);
1858
Winson Chung5f8afe62013-08-12 16:19:28 -07001859 // First we try to find a solution which respects the push mechanic. That is,
Adam Cohen4abc5bd2012-05-29 21:06:03 -07001860 // we try to find a solution such that no displaced item travels through another item
1861 // without also displacing that item.
1862 if (attemptPushInDirection(mIntersectingViews, mOccupiedRect, direction, ignoreView,
Adam Cohen19f37922012-03-21 11:59:11 -07001863 solution)) {
Adam Cohen47a876d2012-03-19 13:21:41 -07001864 return true;
1865 }
Adam Cohen47a876d2012-03-19 13:21:41 -07001866
Adam Cohen4abc5bd2012-05-29 21:06:03 -07001867 // Next we try moving the views as a block, but without requiring the push mechanic.
Adam Cohenf3900c22012-11-16 18:28:11 -08001868 if (addViewsToTempLocation(mIntersectingViews, mOccupiedRect, direction, ignoreView,
Adam Cohen19f37922012-03-21 11:59:11 -07001869 solution)) {
Adam Cohen482ed822012-03-02 14:15:13 -08001870 return true;
1871 }
Adam Cohen47a876d2012-03-19 13:21:41 -07001872
Adam Cohen482ed822012-03-02 14:15:13 -08001873 // Ok, they couldn't move as a block, let's move them individually
1874 for (View v : mIntersectingViews) {
Adam Cohen8baab352012-03-20 17:39:21 -07001875 if (!addViewToTempLocation(v, mOccupiedRect, direction, solution)) {
Adam Cohen482ed822012-03-02 14:15:13 -08001876 return false;
1877 }
1878 }
1879 return true;
1880 }
1881
1882 /*
1883 * Returns a pair (x, y), where x,y are in {-1, 0, 1} corresponding to vector between
1884 * the provided point and the provided cell
1885 */
Adam Cohen47a876d2012-03-19 13:21:41 -07001886 private void computeDirectionVector(float deltaX, float deltaY, int[] result) {
Adam Cohen482ed822012-03-02 14:15:13 -08001887 double angle = Math.atan(((float) deltaY) / deltaX);
1888
1889 result[0] = 0;
1890 result[1] = 0;
1891 if (Math.abs(Math.cos(angle)) > 0.5f) {
1892 result[0] = (int) Math.signum(deltaX);
1893 }
1894 if (Math.abs(Math.sin(angle)) > 0.5f) {
1895 result[1] = (int) Math.signum(deltaY);
1896 }
1897 }
1898
Sunny Goyalf7a29e82015-04-24 15:20:43 -07001899 private ItemConfiguration findReorderSolution(int pixelX, int pixelY, int minSpanX, int minSpanY,
Adam Cohenfa3c58f2013-12-06 16:10:55 -08001900 int spanX, int spanY, int[] direction, View dragView, boolean decX,
1901 ItemConfiguration solution) {
Adam Cohen8baab352012-03-20 17:39:21 -07001902 // Copy the current state into the solution. This solution will be manipulated as necessary.
1903 copyCurrentStateToSolution(solution, false);
1904 // Copy the current occupied array into the temporary occupied array. This array will be
1905 // manipulated as necessary to find a solution.
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001906 mOccupied.copyTo(mTmpOccupied);
Adam Cohen482ed822012-03-02 14:15:13 -08001907
1908 // We find the nearest cell into which we would place the dragged item, assuming there's
1909 // nothing in its way.
1910 int result[] = new int[2];
1911 result = findNearestArea(pixelX, pixelY, spanX, spanY, result);
1912
1913 boolean success = false;
1914 // First we try the exact nearest position of the item being dragged,
1915 // we will then want to try to move this around to other neighbouring positions
Adam Cohen8baab352012-03-20 17:39:21 -07001916 success = rearrangementExists(result[0], result[1], spanX, spanY, direction, dragView,
1917 solution);
Adam Cohen482ed822012-03-02 14:15:13 -08001918
1919 if (!success) {
1920 // We try shrinking the widget down to size in an alternating pattern, shrink 1 in
1921 // x, then 1 in y etc.
1922 if (spanX > minSpanX && (minSpanY == spanY || decX)) {
Adam Cohenfa3c58f2013-12-06 16:10:55 -08001923 return findReorderSolution(pixelX, pixelY, minSpanX, minSpanY, spanX - 1, spanY,
1924 direction, dragView, false, solution);
Adam Cohen482ed822012-03-02 14:15:13 -08001925 } else if (spanY > minSpanY) {
Adam Cohenfa3c58f2013-12-06 16:10:55 -08001926 return findReorderSolution(pixelX, pixelY, minSpanX, minSpanY, spanX, spanY - 1,
1927 direction, dragView, true, solution);
Adam Cohen482ed822012-03-02 14:15:13 -08001928 }
1929 solution.isSolution = false;
1930 } else {
1931 solution.isSolution = true;
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001932 solution.cellX = result[0];
1933 solution.cellY = result[1];
1934 solution.spanX = spanX;
1935 solution.spanY = spanY;
Adam Cohen482ed822012-03-02 14:15:13 -08001936 }
1937 return solution;
1938 }
1939
1940 private void copyCurrentStateToSolution(ItemConfiguration solution, boolean temp) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07001941 int childCount = mShortcutsAndWidgets.getChildCount();
Adam Cohen482ed822012-03-02 14:15:13 -08001942 for (int i = 0; i < childCount; i++) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07001943 View child = mShortcutsAndWidgets.getChildAt(i);
Adam Cohen482ed822012-03-02 14:15:13 -08001944 LayoutParams lp = (LayoutParams) child.getLayoutParams();
Adam Cohen8baab352012-03-20 17:39:21 -07001945 CellAndSpan c;
Adam Cohen482ed822012-03-02 14:15:13 -08001946 if (temp) {
Adam Cohen8baab352012-03-20 17:39:21 -07001947 c = new CellAndSpan(lp.tmpCellX, lp.tmpCellY, lp.cellHSpan, lp.cellVSpan);
Adam Cohen482ed822012-03-02 14:15:13 -08001948 } else {
Adam Cohen8baab352012-03-20 17:39:21 -07001949 c = new CellAndSpan(lp.cellX, lp.cellY, lp.cellHSpan, lp.cellVSpan);
Adam Cohen482ed822012-03-02 14:15:13 -08001950 }
Adam Cohenf3900c22012-11-16 18:28:11 -08001951 solution.add(child, c);
Adam Cohen482ed822012-03-02 14:15:13 -08001952 }
1953 }
1954
1955 private void copySolutionToTempState(ItemConfiguration solution, View dragView) {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001956 mTmpOccupied.clear();
Adam Cohen482ed822012-03-02 14:15:13 -08001957
Michael Jurkaa52570f2012-03-20 03:18:20 -07001958 int childCount = mShortcutsAndWidgets.getChildCount();
Adam Cohen482ed822012-03-02 14:15:13 -08001959 for (int i = 0; i < childCount; i++) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07001960 View child = mShortcutsAndWidgets.getChildAt(i);
Adam Cohen482ed822012-03-02 14:15:13 -08001961 if (child == dragView) continue;
1962 LayoutParams lp = (LayoutParams) child.getLayoutParams();
Adam Cohen8baab352012-03-20 17:39:21 -07001963 CellAndSpan c = solution.map.get(child);
1964 if (c != null) {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001965 lp.tmpCellX = c.cellX;
1966 lp.tmpCellY = c.cellY;
Adam Cohen8baab352012-03-20 17:39:21 -07001967 lp.cellHSpan = c.spanX;
1968 lp.cellVSpan = c.spanY;
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001969 mTmpOccupied.markCells(c, true);
Adam Cohen482ed822012-03-02 14:15:13 -08001970 }
1971 }
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001972 mTmpOccupied.markCells(solution, true);
Adam Cohen482ed822012-03-02 14:15:13 -08001973 }
1974
1975 private void animateItemsToSolution(ItemConfiguration solution, View dragView, boolean
1976 commitDragView) {
1977
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001978 GridOccupancy occupied = DESTRUCTIVE_REORDER ? mOccupied : mTmpOccupied;
1979 occupied.clear();
Adam Cohen482ed822012-03-02 14:15:13 -08001980
Michael Jurkaa52570f2012-03-20 03:18:20 -07001981 int childCount = mShortcutsAndWidgets.getChildCount();
Adam Cohen482ed822012-03-02 14:15:13 -08001982 for (int i = 0; i < childCount; i++) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07001983 View child = mShortcutsAndWidgets.getChildAt(i);
Adam Cohen482ed822012-03-02 14:15:13 -08001984 if (child == dragView) continue;
Adam Cohen8baab352012-03-20 17:39:21 -07001985 CellAndSpan c = solution.map.get(child);
1986 if (c != null) {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001987 animateChildToPosition(child, c.cellX, c.cellY, REORDER_ANIMATION_DURATION, 0,
Adam Cohen19f37922012-03-21 11:59:11 -07001988 DESTRUCTIVE_REORDER, false);
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001989 occupied.markCells(c, true);
Adam Cohen482ed822012-03-02 14:15:13 -08001990 }
1991 }
1992 if (commitDragView) {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001993 occupied.markCells(solution, true);
Adam Cohen482ed822012-03-02 14:15:13 -08001994 }
1995 }
1996
Adam Cohenfa3c58f2013-12-06 16:10:55 -08001997
1998 // This method starts or changes the reorder preview animations
1999 private void beginOrAdjustReorderPreviewAnimations(ItemConfiguration solution,
2000 View dragView, int delay, int mode) {
Adam Cohen19f37922012-03-21 11:59:11 -07002001 int childCount = mShortcutsAndWidgets.getChildCount();
Adam Cohen19f37922012-03-21 11:59:11 -07002002 for (int i = 0; i < childCount; i++) {
2003 View child = mShortcutsAndWidgets.getChildAt(i);
2004 if (child == dragView) continue;
2005 CellAndSpan c = solution.map.get(child);
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002006 boolean skip = mode == ReorderPreviewAnimation.MODE_HINT && solution.intersectingViews
2007 != null && !solution.intersectingViews.contains(child);
2008
Adam Cohen19f37922012-03-21 11:59:11 -07002009 LayoutParams lp = (LayoutParams) child.getLayoutParams();
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002010 if (c != null && !skip) {
2011 ReorderPreviewAnimation rha = new ReorderPreviewAnimation(child, mode, lp.cellX,
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07002012 lp.cellY, c.cellX, c.cellY, c.spanX, c.spanY);
Adam Cohend024f982012-05-23 18:26:45 -07002013 rha.animate();
Adam Cohen19f37922012-03-21 11:59:11 -07002014 }
2015 }
2016 }
2017
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002018 // Class which represents the reorder preview animations. These animations show that an item is
Adam Cohen19f37922012-03-21 11:59:11 -07002019 // in a temporary state, and hint at where the item will return to.
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002020 class ReorderPreviewAnimation {
Adam Cohen19f37922012-03-21 11:59:11 -07002021 View child;
Adam Cohend024f982012-05-23 18:26:45 -07002022 float finalDeltaX;
2023 float finalDeltaY;
2024 float initDeltaX;
2025 float initDeltaY;
2026 float finalScale;
2027 float initScale;
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002028 int mode;
2029 boolean repeating = false;
2030 private static final int PREVIEW_DURATION = 300;
2031 private static final int HINT_DURATION = Workspace.REORDER_TIMEOUT;
2032
2033 public static final int MODE_HINT = 0;
2034 public static final int MODE_PREVIEW = 1;
2035
Adam Cohene7587d22012-05-24 18:50:02 -07002036 Animator a;
Adam Cohen19f37922012-03-21 11:59:11 -07002037
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002038 public ReorderPreviewAnimation(View child, int mode, int cellX0, int cellY0, int cellX1,
2039 int cellY1, int spanX, int spanY) {
Adam Cohen19f37922012-03-21 11:59:11 -07002040 regionToCenterPoint(cellX0, cellY0, spanX, spanY, mTmpPoint);
2041 final int x0 = mTmpPoint[0];
2042 final int y0 = mTmpPoint[1];
2043 regionToCenterPoint(cellX1, cellY1, spanX, spanY, mTmpPoint);
2044 final int x1 = mTmpPoint[0];
2045 final int y1 = mTmpPoint[1];
2046 final int dX = x1 - x0;
2047 final int dY = y1 - y0;
Adam Cohend024f982012-05-23 18:26:45 -07002048 finalDeltaX = 0;
2049 finalDeltaY = 0;
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002050 int dir = mode == MODE_HINT ? -1 : 1;
Adam Cohen19f37922012-03-21 11:59:11 -07002051 if (dX == dY && dX == 0) {
2052 } else {
2053 if (dY == 0) {
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002054 finalDeltaX = - dir * Math.signum(dX) * mReorderPreviewAnimationMagnitude;
Adam Cohen19f37922012-03-21 11:59:11 -07002055 } else if (dX == 0) {
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002056 finalDeltaY = - dir * Math.signum(dY) * mReorderPreviewAnimationMagnitude;
Adam Cohen19f37922012-03-21 11:59:11 -07002057 } else {
2058 double angle = Math.atan( (float) (dY) / dX);
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002059 finalDeltaX = (int) (- dir * Math.signum(dX) *
2060 Math.abs(Math.cos(angle) * mReorderPreviewAnimationMagnitude));
2061 finalDeltaY = (int) (- dir * Math.signum(dY) *
2062 Math.abs(Math.sin(angle) * mReorderPreviewAnimationMagnitude));
Adam Cohen19f37922012-03-21 11:59:11 -07002063 }
2064 }
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002065 this.mode = mode;
Adam Cohend024f982012-05-23 18:26:45 -07002066 initDeltaX = child.getTranslationX();
2067 initDeltaY = child.getTranslationY();
Adam Cohen307fe232012-08-16 17:55:58 -07002068 finalScale = getChildrenScale() - 4.0f / child.getWidth();
Adam Cohend024f982012-05-23 18:26:45 -07002069 initScale = child.getScaleX();
Adam Cohen19f37922012-03-21 11:59:11 -07002070 this.child = child;
2071 }
2072
Adam Cohend024f982012-05-23 18:26:45 -07002073 void animate() {
Adam Cohen19f37922012-03-21 11:59:11 -07002074 if (mShakeAnimators.containsKey(child)) {
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002075 ReorderPreviewAnimation oldAnimation = mShakeAnimators.get(child);
Adam Cohend024f982012-05-23 18:26:45 -07002076 oldAnimation.cancel();
Adam Cohen19f37922012-03-21 11:59:11 -07002077 mShakeAnimators.remove(child);
Adam Cohene7587d22012-05-24 18:50:02 -07002078 if (finalDeltaX == 0 && finalDeltaY == 0) {
2079 completeAnimationImmediately();
2080 return;
2081 }
Adam Cohen19f37922012-03-21 11:59:11 -07002082 }
Adam Cohend024f982012-05-23 18:26:45 -07002083 if (finalDeltaX == 0 && finalDeltaY == 0) {
Adam Cohen19f37922012-03-21 11:59:11 -07002084 return;
2085 }
Michael Jurkaf1ad6082013-03-13 12:55:46 +01002086 ValueAnimator va = LauncherAnimUtils.ofFloat(child, 0f, 1f);
Adam Cohene7587d22012-05-24 18:50:02 -07002087 a = va;
Tony Wickham9e0702f2015-09-02 14:45:39 -07002088
2089 // Animations are disabled in power save mode, causing the repeated animation to jump
2090 // spastically between beginning and end states. Since this looks bad, we don't repeat
2091 // the animation in power save mode.
Tony Wickham112ac952015-11-12 12:31:50 -08002092 if (!Utilities.isPowerSaverOn(getContext())) {
Tony Wickham9e0702f2015-09-02 14:45:39 -07002093 va.setRepeatMode(ValueAnimator.REVERSE);
2094 va.setRepeatCount(ValueAnimator.INFINITE);
2095 }
2096
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002097 va.setDuration(mode == MODE_HINT ? HINT_DURATION : PREVIEW_DURATION);
Adam Cohend024f982012-05-23 18:26:45 -07002098 va.setStartDelay((int) (Math.random() * 60));
Adam Cohen19f37922012-03-21 11:59:11 -07002099 va.addUpdateListener(new AnimatorUpdateListener() {
2100 @Override
2101 public void onAnimationUpdate(ValueAnimator animation) {
2102 float r = ((Float) animation.getAnimatedValue()).floatValue();
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002103 float r1 = (mode == MODE_HINT && repeating) ? 1.0f : r;
2104 float x = r1 * finalDeltaX + (1 - r1) * initDeltaX;
2105 float y = r1 * finalDeltaY + (1 - r1) * initDeltaY;
Adam Cohen19f37922012-03-21 11:59:11 -07002106 child.setTranslationX(x);
2107 child.setTranslationY(y);
Adam Cohend024f982012-05-23 18:26:45 -07002108 float s = r * finalScale + (1 - r) * initScale;
Brandon Keely50e6e562012-05-08 16:28:49 -07002109 child.setScaleX(s);
2110 child.setScaleY(s);
Adam Cohen19f37922012-03-21 11:59:11 -07002111 }
2112 });
2113 va.addListener(new AnimatorListenerAdapter() {
2114 public void onAnimationRepeat(Animator animation) {
Adam Cohen19f37922012-03-21 11:59:11 -07002115 // We make sure to end only after a full period
Adam Cohend024f982012-05-23 18:26:45 -07002116 initDeltaX = 0;
2117 initDeltaY = 0;
Adam Cohen307fe232012-08-16 17:55:58 -07002118 initScale = getChildrenScale();
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002119 repeating = true;
Adam Cohen19f37922012-03-21 11:59:11 -07002120 }
2121 });
Adam Cohen19f37922012-03-21 11:59:11 -07002122 mShakeAnimators.put(child, this);
2123 va.start();
2124 }
2125
Adam Cohend024f982012-05-23 18:26:45 -07002126 private void cancel() {
Adam Cohene7587d22012-05-24 18:50:02 -07002127 if (a != null) {
2128 a.cancel();
2129 }
Adam Cohen19f37922012-03-21 11:59:11 -07002130 }
Adam Cohene7587d22012-05-24 18:50:02 -07002131
Adam Cohen091440a2015-03-18 14:16:05 -07002132 @Thunk void completeAnimationImmediately() {
Adam Cohene7587d22012-05-24 18:50:02 -07002133 if (a != null) {
2134 a.cancel();
2135 }
Brandon Keely50e6e562012-05-08 16:28:49 -07002136
Sunny Goyal5d2fc322015-07-06 22:52:49 -07002137 a = new LauncherViewPropertyAnimator(child)
2138 .scaleX(getChildrenScale())
2139 .scaleY(getChildrenScale())
2140 .translationX(0)
2141 .translationY(0)
2142 .setDuration(REORDER_ANIMATION_DURATION);
2143 a.setInterpolator(new android.view.animation.DecelerateInterpolator(1.5f));
2144 a.start();
Brandon Keely50e6e562012-05-08 16:28:49 -07002145 }
Adam Cohen19f37922012-03-21 11:59:11 -07002146 }
2147
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002148 private void completeAndClearReorderPreviewAnimations() {
2149 for (ReorderPreviewAnimation a: mShakeAnimators.values()) {
Brandon Keely50e6e562012-05-08 16:28:49 -07002150 a.completeAnimationImmediately();
Adam Cohen19f37922012-03-21 11:59:11 -07002151 }
2152 mShakeAnimators.clear();
2153 }
2154
Adam Cohen482ed822012-03-02 14:15:13 -08002155 private void commitTempPlacement() {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07002156 mTmpOccupied.copyTo(mOccupied);
Sunny Goyalaa8ef112015-06-12 20:04:41 -07002157
2158 long screenId = mLauncher.getWorkspace().getIdForScreen(this);
2159 int container = Favorites.CONTAINER_DESKTOP;
2160
2161 if (mLauncher.isHotseatLayout(this)) {
2162 screenId = -1;
2163 container = Favorites.CONTAINER_HOTSEAT;
2164 }
2165
Michael Jurkaa52570f2012-03-20 03:18:20 -07002166 int childCount = mShortcutsAndWidgets.getChildCount();
Adam Cohen482ed822012-03-02 14:15:13 -08002167 for (int i = 0; i < childCount; i++) {
Adam Cohenea889a22012-03-27 16:45:39 -07002168 View child = mShortcutsAndWidgets.getChildAt(i);
2169 LayoutParams lp = (LayoutParams) child.getLayoutParams();
2170 ItemInfo info = (ItemInfo) child.getTag();
Adam Cohen2acce882012-03-28 19:03:19 -07002171 // We do a null check here because the item info can be null in the case of the
2172 // AllApps button in the hotseat.
2173 if (info != null) {
Sunny Goyalaa8ef112015-06-12 20:04:41 -07002174 final boolean requiresDbUpdate = (info.cellX != lp.tmpCellX
2175 || info.cellY != lp.tmpCellY || info.spanX != lp.cellHSpan
2176 || info.spanY != lp.cellVSpan);
2177
Adam Cohen2acce882012-03-28 19:03:19 -07002178 info.cellX = lp.cellX = lp.tmpCellX;
2179 info.cellY = lp.cellY = lp.tmpCellY;
Adam Cohenbebf0422012-04-11 18:06:28 -07002180 info.spanX = lp.cellHSpan;
2181 info.spanY = lp.cellVSpan;
Sunny Goyalaa8ef112015-06-12 20:04:41 -07002182
2183 if (requiresDbUpdate) {
2184 LauncherModel.modifyItemInDatabase(mLauncher, info, container, screenId,
2185 info.cellX, info.cellY, info.spanX, info.spanY);
2186 }
Adam Cohen2acce882012-03-28 19:03:19 -07002187 }
Adam Cohen482ed822012-03-02 14:15:13 -08002188 }
2189 }
2190
Sunny Goyalf7a29e82015-04-24 15:20:43 -07002191 private void setUseTempCoords(boolean useTempCoords) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07002192 int childCount = mShortcutsAndWidgets.getChildCount();
Adam Cohen482ed822012-03-02 14:15:13 -08002193 for (int i = 0; i < childCount; i++) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07002194 LayoutParams lp = (LayoutParams) mShortcutsAndWidgets.getChildAt(i).getLayoutParams();
Adam Cohen482ed822012-03-02 14:15:13 -08002195 lp.useTmpCoords = useTempCoords;
2196 }
2197 }
2198
Sunny Goyalf7a29e82015-04-24 15:20:43 -07002199 private ItemConfiguration findConfigurationNoShuffle(int pixelX, int pixelY, int minSpanX, int minSpanY,
Adam Cohen482ed822012-03-02 14:15:13 -08002200 int spanX, int spanY, View dragView, ItemConfiguration solution) {
2201 int[] result = new int[2];
2202 int[] resultSpan = new int[2];
Sunny Goyalf7a29e82015-04-24 15:20:43 -07002203 findNearestVacantArea(pixelX, pixelY, minSpanX, minSpanY, spanX, spanY, result,
Adam Cohen482ed822012-03-02 14:15:13 -08002204 resultSpan);
2205 if (result[0] >= 0 && result[1] >= 0) {
2206 copyCurrentStateToSolution(solution, false);
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07002207 solution.cellX = result[0];
2208 solution.cellY = result[1];
2209 solution.spanX = resultSpan[0];
2210 solution.spanY = resultSpan[1];
Adam Cohen482ed822012-03-02 14:15:13 -08002211 solution.isSolution = true;
2212 } else {
2213 solution.isSolution = false;
2214 }
2215 return solution;
2216 }
2217
2218 public void prepareChildForDrag(View child) {
2219 markCellsAsUnoccupiedForView(child);
Adam Cohen482ed822012-03-02 14:15:13 -08002220 }
2221
Adam Cohen19f37922012-03-21 11:59:11 -07002222 /* This seems like it should be obvious and straight-forward, but when the direction vector
2223 needs to match with the notion of the dragView pushing other views, we have to employ
2224 a slightly more subtle notion of the direction vector. The question is what two points is
2225 the vector between? The center of the dragView and its desired destination? Not quite, as
2226 this doesn't necessarily coincide with the interaction of the dragView and items occupying
2227 those cells. Instead we use some heuristics to often lock the vector to up, down, left
2228 or right, which helps make pushing feel right.
2229 */
2230 private void getDirectionVectorForDrop(int dragViewCenterX, int dragViewCenterY, int spanX,
2231 int spanY, View dragView, int[] resultDirection) {
2232 int[] targetDestination = new int[2];
2233
2234 findNearestArea(dragViewCenterX, dragViewCenterY, spanX, spanY, targetDestination);
2235 Rect dragRect = new Rect();
2236 regionToRect(targetDestination[0], targetDestination[1], spanX, spanY, dragRect);
2237 dragRect.offset(dragViewCenterX - dragRect.centerX(), dragViewCenterY - dragRect.centerY());
2238
2239 Rect dropRegionRect = new Rect();
2240 getViewsIntersectingRegion(targetDestination[0], targetDestination[1], spanX, spanY,
2241 dragView, dropRegionRect, mIntersectingViews);
2242
2243 int dropRegionSpanX = dropRegionRect.width();
2244 int dropRegionSpanY = dropRegionRect.height();
2245
2246 regionToRect(dropRegionRect.left, dropRegionRect.top, dropRegionRect.width(),
2247 dropRegionRect.height(), dropRegionRect);
2248
2249 int deltaX = (dropRegionRect.centerX() - dragViewCenterX) / spanX;
2250 int deltaY = (dropRegionRect.centerY() - dragViewCenterY) / spanY;
2251
2252 if (dropRegionSpanX == mCountX || spanX == mCountX) {
2253 deltaX = 0;
2254 }
2255 if (dropRegionSpanY == mCountY || spanY == mCountY) {
2256 deltaY = 0;
2257 }
2258
2259 if (deltaX == 0 && deltaY == 0) {
2260 // No idea what to do, give a random direction.
2261 resultDirection[0] = 1;
2262 resultDirection[1] = 0;
2263 } else {
2264 computeDirectionVector(deltaX, deltaY, resultDirection);
2265 }
2266 }
2267
2268 // For a given cell and span, fetch the set of views intersecting the region.
2269 private void getViewsIntersectingRegion(int cellX, int cellY, int spanX, int spanY,
2270 View dragView, Rect boundingRect, ArrayList<View> intersectingViews) {
2271 if (boundingRect != null) {
2272 boundingRect.set(cellX, cellY, cellX + spanX, cellY + spanY);
2273 }
2274 intersectingViews.clear();
2275 Rect r0 = new Rect(cellX, cellY, cellX + spanX, cellY + spanY);
2276 Rect r1 = new Rect();
2277 final int count = mShortcutsAndWidgets.getChildCount();
2278 for (int i = 0; i < count; i++) {
2279 View child = mShortcutsAndWidgets.getChildAt(i);
2280 if (child == dragView) continue;
2281 LayoutParams lp = (LayoutParams) child.getLayoutParams();
2282 r1.set(lp.cellX, lp.cellY, lp.cellX + lp.cellHSpan, lp.cellY + lp.cellVSpan);
2283 if (Rect.intersects(r0, r1)) {
2284 mIntersectingViews.add(child);
2285 if (boundingRect != null) {
2286 boundingRect.union(r1);
2287 }
2288 }
2289 }
2290 }
2291
2292 boolean isNearestDropLocationOccupied(int pixelX, int pixelY, int spanX, int spanY,
2293 View dragView, int[] result) {
2294 result = findNearestArea(pixelX, pixelY, spanX, spanY, result);
2295 getViewsIntersectingRegion(result[0], result[1], spanX, spanY, dragView, null,
2296 mIntersectingViews);
2297 return !mIntersectingViews.isEmpty();
2298 }
2299
2300 void revertTempState() {
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002301 completeAndClearReorderPreviewAnimations();
2302 if (isItemPlacementDirty() && !DESTRUCTIVE_REORDER) {
2303 final int count = mShortcutsAndWidgets.getChildCount();
2304 for (int i = 0; i < count; i++) {
2305 View child = mShortcutsAndWidgets.getChildAt(i);
2306 LayoutParams lp = (LayoutParams) child.getLayoutParams();
2307 if (lp.tmpCellX != lp.cellX || lp.tmpCellY != lp.cellY) {
2308 lp.tmpCellX = lp.cellX;
2309 lp.tmpCellY = lp.cellY;
2310 animateChildToPosition(child, lp.cellX, lp.cellY, REORDER_ANIMATION_DURATION,
2311 0, false, false);
2312 }
Adam Cohen19f37922012-03-21 11:59:11 -07002313 }
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002314 setItemPlacementDirty(false);
Adam Cohen19f37922012-03-21 11:59:11 -07002315 }
Adam Cohen19f37922012-03-21 11:59:11 -07002316 }
2317
Adam Cohenbebf0422012-04-11 18:06:28 -07002318 boolean createAreaForResize(int cellX, int cellY, int spanX, int spanY,
2319 View dragView, int[] direction, boolean commit) {
2320 int[] pixelXY = new int[2];
2321 regionToCenterPoint(cellX, cellY, spanX, spanY, pixelXY);
2322
2323 // First we determine if things have moved enough to cause a different layout
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002324 ItemConfiguration swapSolution = findReorderSolution(pixelXY[0], pixelXY[1], spanX, spanY,
Adam Cohenbebf0422012-04-11 18:06:28 -07002325 spanX, spanY, direction, dragView, true, new ItemConfiguration());
2326
2327 setUseTempCoords(true);
2328 if (swapSolution != null && swapSolution.isSolution) {
2329 // If we're just testing for a possible location (MODE_ACCEPT_DROP), we don't bother
2330 // committing anything or animating anything as we just want to determine if a solution
2331 // exists
2332 copySolutionToTempState(swapSolution, dragView);
2333 setItemPlacementDirty(true);
2334 animateItemsToSolution(swapSolution, dragView, commit);
2335
2336 if (commit) {
2337 commitTempPlacement();
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002338 completeAndClearReorderPreviewAnimations();
Adam Cohenbebf0422012-04-11 18:06:28 -07002339 setItemPlacementDirty(false);
2340 } else {
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002341 beginOrAdjustReorderPreviewAnimations(swapSolution, dragView,
2342 REORDER_ANIMATION_DURATION, ReorderPreviewAnimation.MODE_PREVIEW);
Adam Cohenbebf0422012-04-11 18:06:28 -07002343 }
2344 mShortcutsAndWidgets.requestLayout();
2345 }
2346 return swapSolution.isSolution;
2347 }
2348
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002349 int[] performReorder(int pixelX, int pixelY, int minSpanX, int minSpanY, int spanX, int spanY,
Adam Cohen482ed822012-03-02 14:15:13 -08002350 View dragView, int[] result, int resultSpan[], int mode) {
Adam Cohen482ed822012-03-02 14:15:13 -08002351 // First we determine if things have moved enough to cause a different layout
Adam Cohen47a876d2012-03-19 13:21:41 -07002352 result = findNearestArea(pixelX, pixelY, spanX, spanY, result);
Adam Cohen482ed822012-03-02 14:15:13 -08002353
2354 if (resultSpan == null) {
2355 resultSpan = new int[2];
2356 }
2357
Adam Cohen19f37922012-03-21 11:59:11 -07002358 // When we are checking drop validity or actually dropping, we don't recompute the
2359 // direction vector, since we want the solution to match the preview, and it's possible
2360 // that the exact position of the item has changed to result in a new reordering outcome.
Adam Cohenb209e632012-03-27 17:09:36 -07002361 if ((mode == MODE_ON_DROP || mode == MODE_ON_DROP_EXTERNAL || mode == MODE_ACCEPT_DROP)
2362 && mPreviousReorderDirection[0] != INVALID_DIRECTION) {
Adam Cohen19f37922012-03-21 11:59:11 -07002363 mDirectionVector[0] = mPreviousReorderDirection[0];
2364 mDirectionVector[1] = mPreviousReorderDirection[1];
2365 // We reset this vector after drop
Adam Cohenb209e632012-03-27 17:09:36 -07002366 if (mode == MODE_ON_DROP || mode == MODE_ON_DROP_EXTERNAL) {
2367 mPreviousReorderDirection[0] = INVALID_DIRECTION;
2368 mPreviousReorderDirection[1] = INVALID_DIRECTION;
Adam Cohen19f37922012-03-21 11:59:11 -07002369 }
2370 } else {
2371 getDirectionVectorForDrop(pixelX, pixelY, spanX, spanY, dragView, mDirectionVector);
2372 mPreviousReorderDirection[0] = mDirectionVector[0];
2373 mPreviousReorderDirection[1] = mDirectionVector[1];
2374 }
2375
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002376 // Find a solution involving pushing / displacing any items in the way
2377 ItemConfiguration swapSolution = findReorderSolution(pixelX, pixelY, minSpanX, minSpanY,
Adam Cohen482ed822012-03-02 14:15:13 -08002378 spanX, spanY, mDirectionVector, dragView, true, new ItemConfiguration());
2379
2380 // We attempt the approach which doesn't shuffle views at all
2381 ItemConfiguration noShuffleSolution = findConfigurationNoShuffle(pixelX, pixelY, minSpanX,
2382 minSpanY, spanX, spanY, dragView, new ItemConfiguration());
2383
2384 ItemConfiguration finalSolution = null;
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002385
2386 // If the reorder solution requires resizing (shrinking) the item being dropped, we instead
2387 // favor a solution in which the item is not resized, but
Adam Cohen482ed822012-03-02 14:15:13 -08002388 if (swapSolution.isSolution && swapSolution.area() >= noShuffleSolution.area()) {
2389 finalSolution = swapSolution;
2390 } else if (noShuffleSolution.isSolution) {
2391 finalSolution = noShuffleSolution;
2392 }
2393
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002394 if (mode == MODE_SHOW_REORDER_HINT) {
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002395 if (finalSolution != null) {
Adam Cohenfe692872013-12-11 14:47:23 -08002396 beginOrAdjustReorderPreviewAnimations(finalSolution, dragView, 0,
2397 ReorderPreviewAnimation.MODE_HINT);
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07002398 result[0] = finalSolution.cellX;
2399 result[1] = finalSolution.cellY;
2400 resultSpan[0] = finalSolution.spanX;
2401 resultSpan[1] = finalSolution.spanY;
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002402 } else {
2403 result[0] = result[1] = resultSpan[0] = resultSpan[1] = -1;
2404 }
2405 return result;
2406 }
2407
Adam Cohen482ed822012-03-02 14:15:13 -08002408 boolean foundSolution = true;
2409 if (!DESTRUCTIVE_REORDER) {
2410 setUseTempCoords(true);
2411 }
2412
2413 if (finalSolution != null) {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07002414 result[0] = finalSolution.cellX;
2415 result[1] = finalSolution.cellY;
2416 resultSpan[0] = finalSolution.spanX;
2417 resultSpan[1] = finalSolution.spanY;
Adam Cohen482ed822012-03-02 14:15:13 -08002418
2419 // If we're just testing for a possible location (MODE_ACCEPT_DROP), we don't bother
2420 // committing anything or animating anything as we just want to determine if a solution
2421 // exists
2422 if (mode == MODE_DRAG_OVER || mode == MODE_ON_DROP || mode == MODE_ON_DROP_EXTERNAL) {
2423 if (!DESTRUCTIVE_REORDER) {
2424 copySolutionToTempState(finalSolution, dragView);
2425 }
2426 setItemPlacementDirty(true);
2427 animateItemsToSolution(finalSolution, dragView, mode == MODE_ON_DROP);
2428
Adam Cohen19f37922012-03-21 11:59:11 -07002429 if (!DESTRUCTIVE_REORDER &&
2430 (mode == MODE_ON_DROP || mode == MODE_ON_DROP_EXTERNAL)) {
Adam Cohen482ed822012-03-02 14:15:13 -08002431 commitTempPlacement();
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002432 completeAndClearReorderPreviewAnimations();
Adam Cohen19f37922012-03-21 11:59:11 -07002433 setItemPlacementDirty(false);
2434 } else {
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002435 beginOrAdjustReorderPreviewAnimations(finalSolution, dragView,
2436 REORDER_ANIMATION_DURATION, ReorderPreviewAnimation.MODE_PREVIEW);
Adam Cohen482ed822012-03-02 14:15:13 -08002437 }
2438 }
2439 } else {
2440 foundSolution = false;
2441 result[0] = result[1] = resultSpan[0] = resultSpan[1] = -1;
2442 }
2443
2444 if ((mode == MODE_ON_DROP || !foundSolution) && !DESTRUCTIVE_REORDER) {
2445 setUseTempCoords(false);
2446 }
Adam Cohen482ed822012-03-02 14:15:13 -08002447
Michael Jurkaa52570f2012-03-20 03:18:20 -07002448 mShortcutsAndWidgets.requestLayout();
Adam Cohen482ed822012-03-02 14:15:13 -08002449 return result;
2450 }
2451
Adam Cohen19f37922012-03-21 11:59:11 -07002452 void setItemPlacementDirty(boolean dirty) {
2453 mItemPlacementDirty = dirty;
Adam Cohen482ed822012-03-02 14:15:13 -08002454 }
Adam Cohen19f37922012-03-21 11:59:11 -07002455 boolean isItemPlacementDirty() {
2456 return mItemPlacementDirty;
Adam Cohen482ed822012-03-02 14:15:13 -08002457 }
2458
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07002459 private static class ItemConfiguration extends CellAndSpan {
Adam Cohen8baab352012-03-20 17:39:21 -07002460 HashMap<View, CellAndSpan> map = new HashMap<View, CellAndSpan>();
Adam Cohenf3900c22012-11-16 18:28:11 -08002461 private HashMap<View, CellAndSpan> savedMap = new HashMap<View, CellAndSpan>();
2462 ArrayList<View> sortedViews = new ArrayList<View>();
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002463 ArrayList<View> intersectingViews;
Adam Cohen482ed822012-03-02 14:15:13 -08002464 boolean isSolution = false;
Adam Cohen482ed822012-03-02 14:15:13 -08002465
Adam Cohenf3900c22012-11-16 18:28:11 -08002466 void save() {
2467 // Copy current state into savedMap
2468 for (View v: map.keySet()) {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07002469 savedMap.get(v).copyFrom(map.get(v));
Adam Cohenf3900c22012-11-16 18:28:11 -08002470 }
2471 }
2472
2473 void restore() {
2474 // Restore current state from savedMap
2475 for (View v: savedMap.keySet()) {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07002476 map.get(v).copyFrom(savedMap.get(v));
Adam Cohenf3900c22012-11-16 18:28:11 -08002477 }
2478 }
2479
2480 void add(View v, CellAndSpan cs) {
2481 map.put(v, cs);
2482 savedMap.put(v, new CellAndSpan());
2483 sortedViews.add(v);
2484 }
2485
Adam Cohen482ed822012-03-02 14:15:13 -08002486 int area() {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07002487 return spanX * spanY;
Adam Cohenf3900c22012-11-16 18:28:11 -08002488 }
2489
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07002490 void getBoundingRectForViews(ArrayList<View> views, Rect outRect) {
2491 boolean first = true;
2492 for (View v: views) {
2493 CellAndSpan c = map.get(v);
2494 if (first) {
2495 outRect.set(c.cellX, c.cellY, c.cellX + c.spanX, c.cellY + c.spanY);
2496 first = false;
2497 } else {
2498 outRect.union(c.cellX, c.cellY, c.cellX + c.spanX, c.cellY + c.spanY);
2499 }
2500 }
Adam Cohenf3900c22012-11-16 18:28:11 -08002501 }
Adam Cohen482ed822012-03-02 14:15:13 -08002502 }
2503
Adam Cohendf035382011-04-11 17:22:04 -07002504 /**
Adam Cohendf035382011-04-11 17:22:04 -07002505 * Find a starting cell position that will fit the given bounds nearest the requested
2506 * cell location. Uses Euclidean distance to score multiple vacant areas.
2507 *
2508 * @param pixelX The X location at which you want to search for a vacant area.
2509 * @param pixelY The Y location at which you want to search for a vacant area.
2510 * @param spanX Horizontal span of the object.
2511 * @param spanY Vertical span of the object.
2512 * @param ignoreView Considers space occupied by this view as unoccupied
2513 * @param result Previously returned value to possibly recycle.
2514 * @return The X, Y cell of a vacant area that can contain this object,
2515 * nearest the requested location.
2516 */
Adam Cohenf9c184a2016-01-15 16:47:43 -08002517 public int[] findNearestArea(int pixelX, int pixelY, int spanX, int spanY, int[] result) {
Sunny Goyalf7a29e82015-04-24 15:20:43 -07002518 return findNearestArea(pixelX, pixelY, spanX, spanY, spanX, spanY, false, result, null);
Adam Cohendf035382011-04-11 17:22:04 -07002519 }
2520
Michael Jurka0280c3b2010-09-17 15:00:07 -07002521 boolean existsEmptyCell() {
2522 return findCellForSpan(null, 1, 1);
2523 }
2524
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002525 /**
Michael Jurka0280c3b2010-09-17 15:00:07 -07002526 * Finds the upper-left coordinate of the first rectangle in the grid that can
2527 * hold a cell of the specified dimensions. If intersectX and intersectY are not -1,
2528 * then this method will only return coordinates for rectangles that contain the cell
2529 * (intersectX, intersectY)
2530 *
2531 * @param cellXY The array that will contain the position of a vacant cell if such a cell
2532 * can be found.
2533 * @param spanX The horizontal span of the cell we want to find.
2534 * @param spanY The vertical span of the cell we want to find.
2535 *
2536 * @return True if a vacant cell of the specified dimension was found, false otherwise.
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07002537 */
Hyunyoung Song3f471442015-04-08 19:01:34 -07002538 public boolean findCellForSpan(int[] cellXY, int spanX, int spanY) {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07002539 if (cellXY == null) {
2540 cellXY = new int[2];
Michael Jurka0280c3b2010-09-17 15:00:07 -07002541 }
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07002542 return mOccupied.findVacantCell(cellXY, spanX, spanY);
Michael Jurka0280c3b2010-09-17 15:00:07 -07002543 }
2544
2545 /**
Winson Chungc07918d2011-07-01 15:35:26 -07002546 * A drag event has begun over this layout.
2547 * It may have begun over this layout (in which case onDragChild is called first),
2548 * or it may have begun on another layout.
2549 */
2550 void onDragEnter() {
Winson Chungc07918d2011-07-01 15:35:26 -07002551 mDragging = true;
2552 }
2553
2554 /**
Michael Jurka0280c3b2010-09-17 15:00:07 -07002555 * Called when drag has left this CellLayout or has been completed (successfully or not)
2556 */
2557 void onDragExit() {
Joe Onorato4be866d2010-10-10 11:26:02 -07002558 // This can actually be called when we aren't in a drag, e.g. when adding a new
2559 // item to this layout via the customize drawer.
2560 // Guard against that case.
2561 if (mDragging) {
2562 mDragging = false;
Patrick Dubroyde7658b2010-09-27 11:15:43 -07002563 }
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -07002564
2565 // Invalidate the drag data
Adam Cohend41fbf52012-02-16 23:53:59 -08002566 mDragCell[0] = mDragCell[1] = -1;
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -07002567 mDragOutlineAnims[mDragOutlineCurrent].animateOut();
2568 mDragOutlineCurrent = (mDragOutlineCurrent + 1) % mDragOutlineAnims.length;
Adam Cohen19f37922012-03-21 11:59:11 -07002569 revertTempState();
Michael Jurka33945b22010-12-21 18:19:38 -08002570 setIsDragOverlapping(false);
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07002571 }
2572
2573 /**
Winson Chungaafa03c2010-06-11 17:34:16 -07002574 * Mark a child as having been dropped.
Patrick Dubroyde7658b2010-09-27 11:15:43 -07002575 * At the beginning of the drag operation, the child may have been on another
Patrick Dubroyce34a972010-10-19 10:34:32 -07002576 * screen, but it is re-parented before this method is called.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002577 *
2578 * @param child The child that is being dropped
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002579 */
Adam Cohen716b51e2011-06-30 12:09:54 -07002580 void onDropChild(View child) {
Romain Guyd94533d2009-08-17 10:01:15 -07002581 if (child != null) {
2582 LayoutParams lp = (LayoutParams) child.getLayoutParams();
Romain Guy84f296c2009-11-04 15:00:44 -08002583 lp.dropped = true;
Romain Guyd94533d2009-08-17 10:01:15 -07002584 child.requestLayout();
Tony Wickham1cdb6d02015-09-17 11:08:27 -07002585 markCellsAsOccupiedForView(child);
Romain Guyd94533d2009-08-17 10:01:15 -07002586 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002587 }
2588
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002589 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002590 * Computes a bounding rectangle for a range of cells
Winson Chungaafa03c2010-06-11 17:34:16 -07002591 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002592 * @param cellX X coordinate of upper left corner expressed as a cell position
2593 * @param cellY Y coordinate of upper left corner expressed as a cell position
Winson Chungaafa03c2010-06-11 17:34:16 -07002594 * @param cellHSpan Width in cells
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002595 * @param cellVSpan Height in cells
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07002596 * @param resultRect Rect into which to put the results
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002597 */
Adam Cohend41fbf52012-02-16 23:53:59 -08002598 public void cellToRect(int cellX, int cellY, int cellHSpan, int cellVSpan, Rect resultRect) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002599 final int cellWidth = mCellWidth;
2600 final int cellHeight = mCellHeight;
2601 final int widthGap = mWidthGap;
2602 final int heightGap = mHeightGap;
Winson Chungaafa03c2010-06-11 17:34:16 -07002603
Winson Chung4b825dcd2011-06-19 12:41:22 -07002604 final int hStartPadding = getPaddingLeft();
2605 final int vStartPadding = getPaddingTop();
Winson Chungaafa03c2010-06-11 17:34:16 -07002606
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002607 int width = cellHSpan * cellWidth + ((cellHSpan - 1) * widthGap);
2608 int height = cellVSpan * cellHeight + ((cellVSpan - 1) * heightGap);
2609
2610 int x = hStartPadding + cellX * (cellWidth + widthGap);
2611 int y = vStartPadding + cellY * (cellHeight + heightGap);
Winson Chungaafa03c2010-06-11 17:34:16 -07002612
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07002613 resultRect.set(x, y, x + width, y + height);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002614 }
Winson Chungaafa03c2010-06-11 17:34:16 -07002615
Adam Cohend4844c32011-02-18 19:25:06 -08002616 public void markCellsAsOccupiedForView(View view) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07002617 if (view == null || view.getParent() != mShortcutsAndWidgets) return;
Michael Jurka0280c3b2010-09-17 15:00:07 -07002618 LayoutParams lp = (LayoutParams) view.getLayoutParams();
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07002619 mOccupied.markCells(lp.cellX, lp.cellY, lp.cellHSpan, lp.cellVSpan, true);
Michael Jurka0280c3b2010-09-17 15:00:07 -07002620 }
2621
Adam Cohend4844c32011-02-18 19:25:06 -08002622 public void markCellsAsUnoccupiedForView(View view) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07002623 if (view == null || view.getParent() != mShortcutsAndWidgets) return;
Michael Jurka0280c3b2010-09-17 15:00:07 -07002624 LayoutParams lp = (LayoutParams) view.getLayoutParams();
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07002625 mOccupied.markCells(lp.cellX, lp.cellY, lp.cellHSpan, lp.cellVSpan, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002626 }
2627
Adam Cohen2801caf2011-05-13 20:57:39 -07002628 public int getDesiredWidth() {
Michael Jurka8b805b12012-04-18 14:23:14 -07002629 return getPaddingLeft() + getPaddingRight() + (mCountX * mCellWidth) +
Adam Cohen2801caf2011-05-13 20:57:39 -07002630 (Math.max((mCountX - 1), 0) * mWidthGap);
2631 }
2632
2633 public int getDesiredHeight() {
Michael Jurka8b805b12012-04-18 14:23:14 -07002634 return getPaddingTop() + getPaddingBottom() + (mCountY * mCellHeight) +
Adam Cohen2801caf2011-05-13 20:57:39 -07002635 (Math.max((mCountY - 1), 0) * mHeightGap);
2636 }
2637
Michael Jurka66d72172011-04-12 16:29:25 -07002638 public boolean isOccupied(int x, int y) {
2639 if (x < mCountX && y < mCountY) {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07002640 return mOccupied.cells[x][y];
Michael Jurka66d72172011-04-12 16:29:25 -07002641 } else {
2642 throw new RuntimeException("Position exceeds the bound of this CellLayout");
2643 }
2644 }
2645
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002646 @Override
2647 public ViewGroup.LayoutParams generateLayoutParams(AttributeSet attrs) {
2648 return new CellLayout.LayoutParams(getContext(), attrs);
2649 }
2650
2651 @Override
2652 protected boolean checkLayoutParams(ViewGroup.LayoutParams p) {
2653 return p instanceof CellLayout.LayoutParams;
2654 }
2655
2656 @Override
2657 protected ViewGroup.LayoutParams generateLayoutParams(ViewGroup.LayoutParams p) {
2658 return new CellLayout.LayoutParams(p);
2659 }
2660
2661 public static class LayoutParams extends ViewGroup.MarginLayoutParams {
2662 /**
2663 * Horizontal location of the item in the grid.
2664 */
2665 @ViewDebug.ExportedProperty
2666 public int cellX;
2667
2668 /**
2669 * Vertical location of the item in the grid.
2670 */
2671 @ViewDebug.ExportedProperty
2672 public int cellY;
2673
2674 /**
Adam Cohen482ed822012-03-02 14:15:13 -08002675 * Temporary horizontal location of the item in the grid during reorder
2676 */
2677 public int tmpCellX;
2678
2679 /**
2680 * Temporary vertical location of the item in the grid during reorder
2681 */
2682 public int tmpCellY;
2683
2684 /**
2685 * Indicates that the temporary coordinates should be used to layout the items
2686 */
2687 public boolean useTmpCoords;
2688
2689 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002690 * Number of cells spanned horizontally by the item.
2691 */
2692 @ViewDebug.ExportedProperty
2693 public int cellHSpan;
2694
2695 /**
2696 * Number of cells spanned vertically by the item.
2697 */
2698 @ViewDebug.ExportedProperty
2699 public int cellVSpan;
Winson Chungaafa03c2010-06-11 17:34:16 -07002700
Adam Cohen1b607ed2011-03-03 17:26:50 -08002701 /**
2702 * Indicates whether the item will set its x, y, width and height parameters freely,
2703 * or whether these will be computed based on cellX, cellY, cellHSpan and cellVSpan.
2704 */
Adam Cohend4844c32011-02-18 19:25:06 -08002705 public boolean isLockedToGrid = true;
2706
Adam Cohen482ed822012-03-02 14:15:13 -08002707 /**
Adam Cohenec40b2b2013-07-23 15:52:40 -07002708 * Indicates that this item should use the full extents of its parent.
2709 */
2710 public boolean isFullscreen = false;
2711
2712 /**
Adam Cohen482ed822012-03-02 14:15:13 -08002713 * Indicates whether this item can be reordered. Always true except in the case of the
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07002714 * the AllApps button and QSB place holder.
Adam Cohen482ed822012-03-02 14:15:13 -08002715 */
2716 public boolean canReorder = true;
2717
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002718 // X coordinate of the view in the layout.
2719 @ViewDebug.ExportedProperty
Vadim Tryshevfedca432015-08-19 17:55:02 -07002720 public int x;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002721 // Y coordinate of the view in the layout.
2722 @ViewDebug.ExportedProperty
Vadim Tryshevfedca432015-08-19 17:55:02 -07002723 public int y;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002724
Romain Guy84f296c2009-11-04 15:00:44 -08002725 boolean dropped;
Romain Guyfcb9e712009-10-02 16:06:52 -07002726
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002727 public LayoutParams(Context c, AttributeSet attrs) {
2728 super(c, attrs);
2729 cellHSpan = 1;
2730 cellVSpan = 1;
2731 }
2732
2733 public LayoutParams(ViewGroup.LayoutParams source) {
2734 super(source);
2735 cellHSpan = 1;
2736 cellVSpan = 1;
2737 }
Winson Chungaafa03c2010-06-11 17:34:16 -07002738
2739 public LayoutParams(LayoutParams source) {
2740 super(source);
2741 this.cellX = source.cellX;
2742 this.cellY = source.cellY;
2743 this.cellHSpan = source.cellHSpan;
2744 this.cellVSpan = source.cellVSpan;
2745 }
2746
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002747 public LayoutParams(int cellX, int cellY, int cellHSpan, int cellVSpan) {
Romain Guy8f19cdd2010-01-08 15:07:00 -08002748 super(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002749 this.cellX = cellX;
2750 this.cellY = cellY;
2751 this.cellHSpan = cellHSpan;
2752 this.cellVSpan = cellVSpan;
2753 }
2754
Adam Cohen2374abf2013-04-16 14:56:57 -07002755 public void setup(int cellWidth, int cellHeight, int widthGap, int heightGap,
2756 boolean invertHorizontally, int colCount) {
Adam Cohend4844c32011-02-18 19:25:06 -08002757 if (isLockedToGrid) {
2758 final int myCellHSpan = cellHSpan;
2759 final int myCellVSpan = cellVSpan;
Adam Cohen2374abf2013-04-16 14:56:57 -07002760 int myCellX = useTmpCoords ? tmpCellX : cellX;
2761 int myCellY = useTmpCoords ? tmpCellY : cellY;
2762
2763 if (invertHorizontally) {
2764 myCellX = colCount - myCellX - cellHSpan;
2765 }
Adam Cohen1b607ed2011-03-03 17:26:50 -08002766
Adam Cohend4844c32011-02-18 19:25:06 -08002767 width = myCellHSpan * cellWidth + ((myCellHSpan - 1) * widthGap) -
2768 leftMargin - rightMargin;
2769 height = myCellVSpan * cellHeight + ((myCellVSpan - 1) * heightGap) -
2770 topMargin - bottomMargin;
Winson Chungeecf02d2012-03-02 17:14:58 -08002771 x = (int) (myCellX * (cellWidth + widthGap) + leftMargin);
2772 y = (int) (myCellY * (cellHeight + heightGap) + topMargin);
Adam Cohend4844c32011-02-18 19:25:06 -08002773 }
2774 }
Winson Chungaafa03c2010-06-11 17:34:16 -07002775
Winson Chungaafa03c2010-06-11 17:34:16 -07002776 public String toString() {
2777 return "(" + this.cellX + ", " + this.cellY + ")";
2778 }
Adam Cohen7f4eabe2011-04-21 16:19:16 -07002779
2780 public void setWidth(int width) {
2781 this.width = width;
2782 }
2783
2784 public int getWidth() {
2785 return width;
2786 }
2787
2788 public void setHeight(int height) {
2789 this.height = height;
2790 }
2791
2792 public int getHeight() {
2793 return height;
2794 }
2795
2796 public void setX(int x) {
2797 this.x = x;
2798 }
2799
2800 public int getX() {
2801 return x;
2802 }
2803
2804 public void setY(int y) {
2805 this.y = y;
2806 }
2807
2808 public int getY() {
2809 return y;
2810 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002811 }
2812
Michael Jurka0280c3b2010-09-17 15:00:07 -07002813 // This class stores info for two purposes:
2814 // 1. When dragging items (mDragInfo in Workspace), we store the View, its cellX & cellY,
2815 // its spanX, spanY, and the screen it is on
2816 // 2. When long clicking on an empty cell in a CellLayout, we save information about the
2817 // cellX and cellY coordinates and which page was clicked. We then set this as a tag on
2818 // the CellLayout that was long clicked
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07002819 public static final class CellInfo extends CellAndSpan {
Adam Cohenf9c184a2016-01-15 16:47:43 -08002820 public View cell;
Adam Cohendcd297f2013-06-18 13:13:40 -07002821 long screenId;
Winson Chung3d503fb2011-07-13 17:25:49 -07002822 long container;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002823
Sunny Goyal83a8f042015-05-19 12:52:12 -07002824 public CellInfo(View v, ItemInfo info) {
Adam Cohene0aaa0d2014-05-12 12:44:22 -07002825 cellX = info.cellX;
2826 cellY = info.cellY;
2827 spanX = info.spanX;
2828 spanY = info.spanY;
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07002829 cell = v;
Adam Cohene0aaa0d2014-05-12 12:44:22 -07002830 screenId = info.screenId;
2831 container = info.container;
2832 }
2833
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002834 @Override
2835 public String toString() {
Winson Chungaafa03c2010-06-11 17:34:16 -07002836 return "Cell[view=" + (cell == null ? "null" : cell.getClass())
2837 + ", x=" + cellX + ", y=" + cellY + "]";
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002838 }
2839 }
Michael Jurkad771c962011-08-09 15:00:48 -07002840
Tony Wickham86930612015-09-09 13:50:40 -07002841 /**
2842 * Returns whether an item can be placed in this CellLayout (after rearranging and/or resizing
2843 * if necessary).
2844 */
2845 public boolean hasReorderSolution(ItemInfo itemInfo) {
2846 int[] cellPoint = new int[2];
2847 // Check for a solution starting at every cell.
2848 for (int cellX = 0; cellX < getCountX(); cellX++) {
2849 for (int cellY = 0; cellY < getCountY(); cellY++) {
2850 cellToPoint(cellX, cellY, cellPoint);
2851 if (findReorderSolution(cellPoint[0], cellPoint[1], itemInfo.minSpanX,
2852 itemInfo.minSpanY, itemInfo.spanX, itemInfo.spanY, mDirectionVector, null,
2853 true, new ItemConfiguration()).isSolution) {
2854 return true;
2855 }
2856 }
2857 }
2858 return false;
2859 }
2860
Sunny Goyal9ca9c132015-04-29 14:57:22 -07002861 public boolean isRegionVacant(int x, int y, int spanX, int spanY) {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07002862 return mOccupied.isRegionVacant(x, y, spanX, spanY);
Sunny Goyal9ca9c132015-04-29 14:57:22 -07002863 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002864}