blob: 0a2a01728ebe1e82eb4c7c4dff8f5bb310deb6eb [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;
Brandon Keely50e6e562012-05-08 16:28:49 -070021import android.animation.AnimatorSet;
Chet Haase00397b12010-10-07 11:13:10 -070022import android.animation.TimeInterpolator;
Patrick Dubroyde7658b2010-09-27 11:15:43 -070023import android.animation.ValueAnimator;
24import android.animation.ValueAnimator.AnimatorUpdateListener;
Adam Cohenc9735cf2015-01-23 16:11:55 -080025import android.annotation.TargetApi;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080026import android.content.Context;
Joe Onorato79e56262009-09-21 15:23:04 -040027import android.content.res.Resources;
Joe Onorato4be866d2010-10-10 11:26:02 -070028import android.graphics.Bitmap;
Winson Chungaafa03c2010-06-11 17:34:16 -070029import android.graphics.Canvas;
Andrew Flynn0dca1ec2012-02-29 13:33:22 -080030import android.graphics.Color;
Joe Onorato4be866d2010-10-10 11:26:02 -070031import android.graphics.Paint;
Patrick Dubroyde7658b2010-09-27 11:15:43 -070032import android.graphics.Point;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080033import android.graphics.Rect;
Adam Cohen482ed822012-03-02 14:15:13 -080034import android.graphics.drawable.ColorDrawable;
Patrick Dubroy6569f2c2010-07-12 14:25:18 -070035import android.graphics.drawable.Drawable;
Sunny Goyal2805e632015-05-20 15:35:32 -070036import android.graphics.drawable.TransitionDrawable;
Adam Cohenc9735cf2015-01-23 16:11:55 -080037import android.os.Build;
Adam Cohen1462de32012-07-24 22:34:36 -070038import android.os.Parcelable;
Adam Cohenc9735cf2015-01-23 16:11:55 -080039import android.support.v4.view.ViewCompat;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080040import android.util.AttributeSet;
Joe Onorato4be866d2010-10-10 11:26:02 -070041import android.util.Log;
Adam Cohen1462de32012-07-24 22:34:36 -070042import android.util.SparseArray;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080043import android.view.MotionEvent;
44import android.view.View;
45import android.view.ViewDebug;
46import android.view.ViewGroup;
Adam Cohenc9735cf2015-01-23 16:11:55 -080047import android.view.accessibility.AccessibilityEvent;
Winson Chung150fbab2010-09-29 17:14:26 -070048import android.view.animation.DecelerateInterpolator;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080049
Sunny Goyal4b6eb262015-05-14 19:24:40 -070050import com.android.launcher3.BubbleTextView.BubbleTextShadowHandler;
Daniel Sandler325dc232013-06-05 22:57:57 -040051import com.android.launcher3.FolderIcon.FolderRingAnimator;
Sunny Goyale9b651e2015-04-24 11:44:51 -070052import com.android.launcher3.accessibility.DragAndDropAccessibilityDelegate;
53import com.android.launcher3.accessibility.FolderAccessibilityHelper;
54import com.android.launcher3.accessibility.WorkspaceAccessibilityHelper;
Sunny Goyald1a0e8b2015-08-27 17:45:46 -070055import com.android.launcher3.util.ParcelableSparseArray;
Adam Cohen091440a2015-03-18 14:16:05 -070056import com.android.launcher3.util.Thunk;
Patrick Dubroy8e58e912010-10-14 13:21:48 -070057
Adam Cohen69ce2e52011-07-03 19:25:21 -070058import java.util.ArrayList;
Adam Cohenc0dcf592011-06-01 15:30:43 -070059import java.util.Arrays;
Adam Cohenf3900c22012-11-16 18:28:11 -080060import java.util.Collections;
61import java.util.Comparator;
Adam Cohenbfbfd262011-06-13 16:55:12 -070062import java.util.HashMap;
Adam Cohend41fbf52012-02-16 23:53:59 -080063import java.util.Stack;
Adam Cohenc0dcf592011-06-01 15:30:43 -070064
Sunny Goyal4b6eb262015-05-14 19:24:40 -070065public class CellLayout extends ViewGroup implements BubbleTextShadowHandler {
Sunny Goyale9b651e2015-04-24 11:44:51 -070066 public static final int WORKSPACE_ACCESSIBILITY_DRAG = 2;
67 public static final int FOLDER_ACCESSIBILITY_DRAG = 1;
68
Tony Wickhama0628cc2015-10-14 15:23:04 -070069 private static final String TAG = "CellLayout";
70 private static final boolean LOGD = false;
Winson Chungaafa03c2010-06-11 17:34:16 -070071
Adam Cohen2acce882012-03-28 19:03:19 -070072 private Launcher mLauncher;
Adam Cohen091440a2015-03-18 14:16:05 -070073 @Thunk int mCellWidth;
74 @Thunk int mCellHeight;
Winson Chung11a1a532013-09-13 11:14:45 -070075 private int mFixedCellWidth;
76 private int mFixedCellHeight;
Winson Chungaafa03c2010-06-11 17:34:16 -070077
Adam Cohen091440a2015-03-18 14:16:05 -070078 @Thunk int mCountX;
79 @Thunk int mCountY;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080080
Adam Cohen234c4cd2011-07-17 21:03:04 -070081 private int mOriginalWidthGap;
82 private int mOriginalHeightGap;
Adam Cohen091440a2015-03-18 14:16:05 -070083 @Thunk int mWidthGap;
84 @Thunk int mHeightGap;
Winson Chung4b825dcd2011-06-19 12:41:22 -070085 private int mMaxGap;
Adam Cohen917e3882013-10-31 15:03:35 -070086 private boolean mDropPending = false;
Adam Cohenc50438c2014-08-19 17:43:05 -070087 private boolean mIsDragTarget = true;
Sunny Goyald1a0e8b2015-08-27 17:45:46 -070088 private boolean mJailContent = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080089
Patrick Dubroyde7658b2010-09-27 11:15:43 -070090 // These are temporary variables to prevent having to allocate a new object just to
91 // return an (x, y) value from helper functions. Do NOT use them to maintain other state.
Adam Cohen091440a2015-03-18 14:16:05 -070092 @Thunk final int[] mTmpPoint = new int[2];
Sunny Goyal2805e632015-05-20 15:35:32 -070093 @Thunk final int[] mTempLocation = new int[2];
Patrick Dubroy6569f2c2010-07-12 14:25:18 -070094
The Android Open Source Project31dd5032009-03-03 19:32:27 -080095 boolean[][] mOccupied;
Adam Cohen482ed822012-03-02 14:15:13 -080096 boolean[][] mTmpOccupied;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080097
Michael Jurkadee05892010-07-27 10:01:56 -070098 private OnTouchListener mInterceptTouchListener;
Mady Melloref044dd2015-06-02 15:35:07 -070099 private StylusEventHelper mStylusEventHelper;
Michael Jurkadee05892010-07-27 10:01:56 -0700100
Adam Cohen69ce2e52011-07-03 19:25:21 -0700101 private ArrayList<FolderRingAnimator> mFolderOuterRings = new ArrayList<FolderRingAnimator>();
Adam Cohenc51934b2011-07-26 21:07:43 -0700102 private int[] mFolderLeaveBehindCell = {-1, -1};
Adam Cohen69ce2e52011-07-03 19:25:21 -0700103
Michael Jurka5f1c5092010-09-03 14:15:02 -0700104 private float mBackgroundAlpha;
Adam Cohenf34bab52010-09-30 14:11:56 -0700105
Sunny Goyal2805e632015-05-20 15:35:32 -0700106 private static final int BACKGROUND_ACTIVATE_DURATION = 120;
107 private final TransitionDrawable mBackground;
108
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700109 // These values allow a fixed measurement to be set on the CellLayout.
110 private int mFixedWidth = -1;
111 private int mFixedHeight = -1;
112
Michael Jurka33945b22010-12-21 18:19:38 -0800113 // If we're actively dragging something over this screen, mIsDragOverlapping is true
114 private boolean mIsDragOverlapping = false;
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700115
Winson Chung150fbab2010-09-29 17:14:26 -0700116 // These arrays are used to implement the drag visualization on x-large screens.
Joe Onorato4be866d2010-10-10 11:26:02 -0700117 // They are used as circular arrays, indexed by mDragOutlineCurrent.
Adam Cohen091440a2015-03-18 14:16:05 -0700118 @Thunk Rect[] mDragOutlines = new Rect[4];
119 @Thunk float[] mDragOutlineAlphas = new float[mDragOutlines.length];
Joe Onorato4be866d2010-10-10 11:26:02 -0700120 private InterruptibleInOutAnimator[] mDragOutlineAnims =
121 new InterruptibleInOutAnimator[mDragOutlines.length];
Winson Chung150fbab2010-09-29 17:14:26 -0700122
123 // Used as an index into the above 3 arrays; indicates which is the most current value.
Joe Onorato4be866d2010-10-10 11:26:02 -0700124 private int mDragOutlineCurrent = 0;
Patrick Dubroy8e58e912010-10-14 13:21:48 -0700125 private final Paint mDragOutlinePaint = new Paint();
Winson Chung150fbab2010-09-29 17:14:26 -0700126
Sunny Goyal4fe5a372015-05-14 19:55:10 -0700127 private final ClickShadowView mTouchFeedbackView;
Patrick Dubroy96864c32011-03-10 17:17:23 -0800128
Sunny Goyal316490e2015-06-02 09:38:28 -0700129 @Thunk HashMap<CellLayout.LayoutParams, Animator> mReorderAnimators = new HashMap<>();
130 @Thunk HashMap<View, ReorderPreviewAnimation> mShakeAnimators = new HashMap<>();
Adam Cohen19f37922012-03-21 11:59:11 -0700131
132 private boolean mItemPlacementDirty = false;
Adam Cohenbfbfd262011-06-13 16:55:12 -0700133
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700134 // When a drag operation is in progress, holds the nearest cell to the touch point
135 private final int[] mDragCell = new int[2];
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800136
Joe Onorato4be866d2010-10-10 11:26:02 -0700137 private boolean mDragging = false;
138
Patrick Dubroyce34a972010-10-19 10:34:32 -0700139 private TimeInterpolator mEaseOutInterpolator;
Michael Jurkaa52570f2012-03-20 03:18:20 -0700140 private ShortcutAndWidgetContainer mShortcutsAndWidgets;
Patrick Dubroyce34a972010-10-19 10:34:32 -0700141
Andrew Flynn0dca1ec2012-02-29 13:33:22 -0800142 private boolean mIsHotseat = false;
Adam Cohen307fe232012-08-16 17:55:58 -0700143 private float mHotseatScale = 1f;
Andrew Flynn0dca1ec2012-02-29 13:33:22 -0800144
Adam Cohenfa3c58f2013-12-06 16:10:55 -0800145 public static final int MODE_SHOW_REORDER_HINT = 0;
146 public static final int MODE_DRAG_OVER = 1;
147 public static final int MODE_ON_DROP = 2;
148 public static final int MODE_ON_DROP_EXTERNAL = 3;
149 public static final int MODE_ACCEPT_DROP = 4;
Adam Cohen19f37922012-03-21 11:59:11 -0700150 private static final boolean DESTRUCTIVE_REORDER = false;
Adam Cohen482ed822012-03-02 14:15:13 -0800151 private static final boolean DEBUG_VISUALIZE_OCCUPIED = false;
152
Adam Cohena897f392012-04-27 18:12:05 -0700153 static final int LANDSCAPE = 0;
154 static final int PORTRAIT = 1;
155
Adam Cohenfa3c58f2013-12-06 16:10:55 -0800156 private static final float REORDER_PREVIEW_MAGNITUDE = 0.12f;
Adam Cohen19f37922012-03-21 11:59:11 -0700157 private static final int REORDER_ANIMATION_DURATION = 150;
Adam Cohen091440a2015-03-18 14:16:05 -0700158 @Thunk float mReorderPreviewAnimationMagnitude;
Adam Cohen19f37922012-03-21 11:59:11 -0700159
Adam Cohen482ed822012-03-02 14:15:13 -0800160 private ArrayList<View> mIntersectingViews = new ArrayList<View>();
161 private Rect mOccupiedRect = new Rect();
162 private int[] mDirectionVector = new int[2];
Adam Cohen19f37922012-03-21 11:59:11 -0700163 int[] mPreviousReorderDirection = new int[2];
Adam Cohenb209e632012-03-27 17:09:36 -0700164 private static final int INVALID_DIRECTION = -100;
Adam Cohen482ed822012-03-02 14:15:13 -0800165
Sunny Goyal2805e632015-05-20 15:35:32 -0700166 private final Rect mTempRect = new Rect();
Winson Chung3a6e7f32013-10-09 15:50:52 -0700167
Michael Jurkaca993832012-06-29 15:17:04 -0700168 private final static Paint sPaint = new Paint();
Romain Guy8a0bff52012-05-06 13:14:33 -0700169
Adam Cohenc9735cf2015-01-23 16:11:55 -0800170 // Related to accessible drag and drop
Sunny Goyale9b651e2015-04-24 11:44:51 -0700171 private DragAndDropAccessibilityDelegate mTouchHelper;
Adam Cohenc9735cf2015-01-23 16:11:55 -0800172 private boolean mUseTouchHelper = false;
Adam Cohenc9735cf2015-01-23 16:11:55 -0800173
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800174 public CellLayout(Context context) {
175 this(context, null);
176 }
177
178 public CellLayout(Context context, AttributeSet attrs) {
179 this(context, attrs, 0);
180 }
181
182 public CellLayout(Context context, AttributeSet attrs, int defStyle) {
183 super(context, attrs, defStyle);
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700184
185 // A ViewGroup usually does not draw, but CellLayout needs to draw a rectangle to show
186 // the user where a dragged item will land when dropped.
187 setWillNotDraw(false);
Romain Guyce3cbd12013-02-25 15:00:36 -0800188 setClipToPadding(false);
Adam Cohen2acce882012-03-28 19:03:19 -0700189 mLauncher = (Launcher) context;
Michael Jurkaa63c4522010-08-19 13:52:27 -0700190
Adam Cohen2e6da152015-05-06 11:42:25 -0700191 DeviceProfile grid = mLauncher.getDeviceProfile();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800192
Winson Chung11a1a532013-09-13 11:14:45 -0700193 mCellWidth = mCellHeight = -1;
Nilesh Agrawal5f7099a2014-01-02 15:54:57 -0800194 mFixedCellWidth = mFixedCellHeight = -1;
Winson Chung5f8afe62013-08-12 16:19:28 -0700195 mWidthGap = mOriginalWidthGap = 0;
196 mHeightGap = mOriginalHeightGap = 0;
197 mMaxGap = Integer.MAX_VALUE;
Adam Cohen2e6da152015-05-06 11:42:25 -0700198 mCountX = (int) grid.inv.numColumns;
199 mCountY = (int) grid.inv.numRows;
Michael Jurka0280c3b2010-09-17 15:00:07 -0700200 mOccupied = new boolean[mCountX][mCountY];
Adam Cohen482ed822012-03-02 14:15:13 -0800201 mTmpOccupied = new boolean[mCountX][mCountY];
Adam Cohen5b53f292012-03-29 14:30:35 -0700202 mPreviousReorderDirection[0] = INVALID_DIRECTION;
203 mPreviousReorderDirection[1] = INVALID_DIRECTION;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800204
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800205 setAlwaysDrawnWithCacheEnabled(false);
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700206 final Resources res = getResources();
Winson Chung6e1c0d32013-10-25 15:24:24 -0700207 mHotseatScale = (float) grid.hotseatIconSizePx / grid.iconSizePx;
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700208
Sunny Goyal2805e632015-05-20 15:35:32 -0700209 mBackground = (TransitionDrawable) res.getDrawable(R.drawable.bg_screenpanel);
210 mBackground.setCallback(this);
Winson Chunge8f1d042015-07-31 12:39:57 -0700211 mBackground.setAlpha((int) (mBackgroundAlpha * 255));
Michael Jurka33945b22010-12-21 18:19:38 -0800212
Adam Cohenfa3c58f2013-12-06 16:10:55 -0800213 mReorderPreviewAnimationMagnitude = (REORDER_PREVIEW_MAGNITUDE *
Winson Chung5f8afe62013-08-12 16:19:28 -0700214 grid.iconSizePx);
Adam Cohen19f37922012-03-21 11:59:11 -0700215
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700216 // Initialize the data structures used for the drag visualization.
Patrick Dubroyce34a972010-10-19 10:34:32 -0700217 mEaseOutInterpolator = new DecelerateInterpolator(2.5f); // Quint ease out
Winson Chungb8c69f32011-10-19 21:36:08 -0700218 mDragCell[0] = mDragCell[1] = -1;
Joe Onorato4be866d2010-10-10 11:26:02 -0700219 for (int i = 0; i < mDragOutlines.length; i++) {
Adam Cohend41fbf52012-02-16 23:53:59 -0800220 mDragOutlines[i] = new Rect(-1, -1, -1, -1);
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700221 }
222
223 // When dragging things around the home screens, we show a green outline of
224 // where the item will land. The outlines gradually fade out, leaving a trail
225 // behind the drag path.
226 // Set up all the animations that are used to implement this fading.
227 final int duration = res.getInteger(R.integer.config_dragOutlineFadeTime);
Chet Haase472b2812010-10-14 07:02:04 -0700228 final float fromAlphaValue = 0;
229 final float toAlphaValue = (float)res.getInteger(R.integer.config_dragOutlineMaxAlpha);
Joe Onorato4be866d2010-10-10 11:26:02 -0700230
Patrick Dubroy8e58e912010-10-14 13:21:48 -0700231 Arrays.fill(mDragOutlineAlphas, fromAlphaValue);
Joe Onorato4be866d2010-10-10 11:26:02 -0700232
233 for (int i = 0; i < mDragOutlineAnims.length; i++) {
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700234 final InterruptibleInOutAnimator anim =
Michael Jurkaf1ad6082013-03-13 12:55:46 +0100235 new InterruptibleInOutAnimator(this, duration, fromAlphaValue, toAlphaValue);
Patrick Dubroyce34a972010-10-19 10:34:32 -0700236 anim.getAnimator().setInterpolator(mEaseOutInterpolator);
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700237 final int thisIndex = i;
Chet Haase472b2812010-10-14 07:02:04 -0700238 anim.getAnimator().addUpdateListener(new AnimatorUpdateListener() {
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700239 public void onAnimationUpdate(ValueAnimator animation) {
Joe Onorato4be866d2010-10-10 11:26:02 -0700240 final Bitmap outline = (Bitmap)anim.getTag();
241
242 // If an animation is started and then stopped very quickly, we can still
243 // get spurious updates we've cleared the tag. Guard against this.
244 if (outline == null) {
Tony Wickhama0628cc2015-10-14 15:23:04 -0700245 if (LOGD) {
Patrick Dubroyfe6bd872010-10-13 17:32:10 -0700246 Object val = animation.getAnimatedValue();
247 Log.d(TAG, "anim " + thisIndex + " update: " + val +
248 ", isStopped " + anim.isStopped());
249 }
Joe Onorato4be866d2010-10-10 11:26:02 -0700250 // Try to prevent it from continuing to run
251 animation.cancel();
252 } else {
Chet Haase472b2812010-10-14 07:02:04 -0700253 mDragOutlineAlphas[thisIndex] = (Float) animation.getAnimatedValue();
Adam Cohend41fbf52012-02-16 23:53:59 -0800254 CellLayout.this.invalidate(mDragOutlines[thisIndex]);
Joe Onorato4be866d2010-10-10 11:26:02 -0700255 }
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700256 }
257 });
Joe Onorato4be866d2010-10-10 11:26:02 -0700258 // The animation holds a reference to the drag outline bitmap as long is it's
259 // running. This way the bitmap can be GCed when the animations are complete.
Chet Haase472b2812010-10-14 07:02:04 -0700260 anim.getAnimator().addListener(new AnimatorListenerAdapter() {
Michael Jurka3c4c20f2010-10-28 15:36:06 -0700261 @Override
Joe Onorato4be866d2010-10-10 11:26:02 -0700262 public void onAnimationEnd(Animator animation) {
Chet Haase472b2812010-10-14 07:02:04 -0700263 if ((Float) ((ValueAnimator) animation).getAnimatedValue() == 0f) {
Joe Onorato4be866d2010-10-10 11:26:02 -0700264 anim.setTag(null);
265 }
266 }
267 });
268 mDragOutlineAnims[i] = anim;
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700269 }
Patrick Dubroyce34a972010-10-19 10:34:32 -0700270
Michael Jurkaa52570f2012-03-20 03:18:20 -0700271 mShortcutsAndWidgets = new ShortcutAndWidgetContainer(context);
Adam Cohen2374abf2013-04-16 14:56:57 -0700272 mShortcutsAndWidgets.setCellDimensions(mCellWidth, mCellHeight, mWidthGap, mHeightGap,
Winson Chung5f8afe62013-08-12 16:19:28 -0700273 mCountX, mCountY);
Adam Cohen2374abf2013-04-16 14:56:57 -0700274
Mady Melloref044dd2015-06-02 15:35:07 -0700275 mStylusEventHelper = new StylusEventHelper(this);
276
Sunny Goyal4fe5a372015-05-14 19:55:10 -0700277 mTouchFeedbackView = new ClickShadowView(context);
278 addView(mTouchFeedbackView);
Michael Jurkaa52570f2012-03-20 03:18:20 -0700279 addView(mShortcutsAndWidgets);
Michael Jurka18014792010-10-14 09:01:34 -0700280 }
281
Adam Cohenc9735cf2015-01-23 16:11:55 -0800282 @TargetApi(Build.VERSION_CODES.LOLLIPOP)
Sunny Goyale9b651e2015-04-24 11:44:51 -0700283 public void enableAccessibleDrag(boolean enable, int dragType) {
Adam Cohenc9735cf2015-01-23 16:11:55 -0800284 mUseTouchHelper = enable;
285 if (!enable) {
286 ViewCompat.setAccessibilityDelegate(this, null);
287 setImportantForAccessibility(IMPORTANT_FOR_ACCESSIBILITY_NO);
288 getShortcutsAndWidgets().setImportantForAccessibility(IMPORTANT_FOR_ACCESSIBILITY_NO);
289 setOnClickListener(mLauncher);
290 } else {
Sunny Goyale9b651e2015-04-24 11:44:51 -0700291 if (dragType == WORKSPACE_ACCESSIBILITY_DRAG &&
292 !(mTouchHelper instanceof WorkspaceAccessibilityHelper)) {
293 mTouchHelper = new WorkspaceAccessibilityHelper(this);
294 } else if (dragType == FOLDER_ACCESSIBILITY_DRAG &&
295 !(mTouchHelper instanceof FolderAccessibilityHelper)) {
296 mTouchHelper = new FolderAccessibilityHelper(this);
297 }
Adam Cohenc9735cf2015-01-23 16:11:55 -0800298 ViewCompat.setAccessibilityDelegate(this, mTouchHelper);
299 setImportantForAccessibility(IMPORTANT_FOR_ACCESSIBILITY_YES);
300 getShortcutsAndWidgets().setImportantForAccessibility(IMPORTANT_FOR_ACCESSIBILITY_YES);
301 setOnClickListener(mTouchHelper);
302 }
303
304 // Invalidate the accessibility hierarchy
305 if (getParent() != null) {
306 getParent().notifySubtreeAccessibilityStateChanged(
307 this, this, AccessibilityEvent.CONTENT_CHANGE_TYPE_SUBTREE);
308 }
309 }
310
311 @Override
312 public boolean dispatchHoverEvent(MotionEvent event) {
313 // Always attempt to dispatch hover events to accessibility first.
314 if (mUseTouchHelper && mTouchHelper.dispatchHoverEvent(event)) {
315 return true;
316 }
317 return super.dispatchHoverEvent(event);
318 }
319
320 @Override
Adam Cohenc9735cf2015-01-23 16:11:55 -0800321 public boolean onInterceptTouchEvent(MotionEvent ev) {
322 if (mUseTouchHelper ||
323 (mInterceptTouchListener != null && mInterceptTouchListener.onTouch(this, ev))) {
324 return true;
325 }
326 return false;
327 }
328
Mady Melloref044dd2015-06-02 15:35:07 -0700329 @Override
330 public boolean onTouchEvent(MotionEvent ev) {
331 boolean handled = super.onTouchEvent(ev);
332 // Stylus button press on a home screen should not switch between overview mode and
333 // the home screen mode, however, once in overview mode stylus button press should be
334 // enabled to allow rearranging the different home screens. So check what mode
335 // the workspace is in, and only perform stylus button presses while in overview mode.
336 if (mLauncher.mWorkspace.isInOverviewMode()
337 && mStylusEventHelper.checkAndPerformStylusEvent(ev)) {
338 return true;
339 }
340 return handled;
341 }
342
Chris Craik01f2d7f2013-10-01 14:41:56 -0700343 public void enableHardwareLayer(boolean hasLayer) {
344 mShortcutsAndWidgets.setLayerType(hasLayer ? LAYER_TYPE_HARDWARE : LAYER_TYPE_NONE, sPaint);
Michael Jurkad51f33a2012-06-28 15:35:26 -0700345 }
346
347 public void buildHardwareLayer() {
348 mShortcutsAndWidgets.buildLayer();
Adam Cohen2801caf2011-05-13 20:57:39 -0700349 }
350
Adam Cohen307fe232012-08-16 17:55:58 -0700351 public float getChildrenScale() {
352 return mIsHotseat ? mHotseatScale : 1.0f;
353 }
354
Winson Chung5f8afe62013-08-12 16:19:28 -0700355 public void setCellDimensions(int width, int height) {
Winson Chung11a1a532013-09-13 11:14:45 -0700356 mFixedCellWidth = mCellWidth = width;
357 mFixedCellHeight = mCellHeight = height;
Winson Chung5f8afe62013-08-12 16:19:28 -0700358 mShortcutsAndWidgets.setCellDimensions(mCellWidth, mCellHeight, mWidthGap, mHeightGap,
359 mCountX, mCountY);
360 }
361
Adam Cohen2801caf2011-05-13 20:57:39 -0700362 public void setGridSize(int x, int y) {
363 mCountX = x;
364 mCountY = y;
365 mOccupied = new boolean[mCountX][mCountY];
Adam Cohen482ed822012-03-02 14:15:13 -0800366 mTmpOccupied = new boolean[mCountX][mCountY];
Adam Cohen7fbec102012-03-27 12:42:19 -0700367 mTempRectStack.clear();
Adam Cohen2374abf2013-04-16 14:56:57 -0700368 mShortcutsAndWidgets.setCellDimensions(mCellWidth, mCellHeight, mWidthGap, mHeightGap,
Winson Chung5f8afe62013-08-12 16:19:28 -0700369 mCountX, mCountY);
Adam Cohen76fc0852011-06-17 13:26:23 -0700370 requestLayout();
Adam Cohen2801caf2011-05-13 20:57:39 -0700371 }
372
Adam Cohen2374abf2013-04-16 14:56:57 -0700373 // Set whether or not to invert the layout horizontally if the layout is in RTL mode.
374 public void setInvertIfRtl(boolean invert) {
375 mShortcutsAndWidgets.setInvertIfRtl(invert);
376 }
377
Adam Cohen917e3882013-10-31 15:03:35 -0700378 public void setDropPending(boolean pending) {
379 mDropPending = pending;
380 }
381
382 public boolean isDropPending() {
383 return mDropPending;
384 }
385
Sunny Goyal4b6eb262015-05-14 19:24:40 -0700386 @Override
387 public void setPressedIcon(BubbleTextView icon, Bitmap background) {
Sunny Goyal508da152014-08-14 10:53:27 -0700388 if (icon == null || background == null) {
389 mTouchFeedbackView.setBitmap(null);
390 mTouchFeedbackView.animate().cancel();
391 } else {
Sunny Goyal508da152014-08-14 10:53:27 -0700392 if (mTouchFeedbackView.setBitmap(background)) {
Sunny Goyal4fe5a372015-05-14 19:55:10 -0700393 mTouchFeedbackView.alignWithIconView(icon, mShortcutsAndWidgets);
394 mTouchFeedbackView.animateShadow();
Sunny Goyal508da152014-08-14 10:53:27 -0700395 }
Patrick Dubroy96864c32011-03-10 17:17:23 -0800396 }
397 }
398
Adam Cohenc50438c2014-08-19 17:43:05 -0700399 void disableDragTarget() {
400 mIsDragTarget = false;
401 }
402
403 boolean isDragTarget() {
404 return mIsDragTarget;
405 }
406
407 void setIsDragOverlapping(boolean isDragOverlapping) {
408 if (mIsDragOverlapping != isDragOverlapping) {
409 mIsDragOverlapping = isDragOverlapping;
Sunny Goyal2805e632015-05-20 15:35:32 -0700410 if (mIsDragOverlapping) {
411 mBackground.startTransition(BACKGROUND_ACTIVATE_DURATION);
412 } else {
Winson Chunge8f1d042015-07-31 12:39:57 -0700413 if (mBackgroundAlpha > 0f) {
414 mBackground.reverseTransition(BACKGROUND_ACTIVATE_DURATION);
415 } else {
416 mBackground.resetTransition();
417 }
Sunny Goyal2805e632015-05-20 15:35:32 -0700418 }
Adam Cohenc50438c2014-08-19 17:43:05 -0700419 invalidate();
420 }
421 }
422
Sunny Goyald1a0e8b2015-08-27 17:45:46 -0700423 public boolean getIsDragOverlapping() {
Michael Jurka33945b22010-12-21 18:19:38 -0800424 return mIsDragOverlapping;
425 }
426
Sunny Goyald1a0e8b2015-08-27 17:45:46 -0700427 public void disableJailContent() {
428 mJailContent = false;
429 }
430
431 @Override
432 protected void dispatchSaveInstanceState(SparseArray<Parcelable> container) {
433 if (mJailContent) {
434 ParcelableSparseArray jail = getJailedArray(container);
435 super.dispatchSaveInstanceState(jail);
436 container.put(R.id.cell_layout_jail_id, jail);
437 } else {
438 super.dispatchSaveInstanceState(container);
439 }
440 }
441
442 @Override
443 protected void dispatchRestoreInstanceState(SparseArray<Parcelable> container) {
444 super.dispatchRestoreInstanceState(mJailContent ? getJailedArray(container) : container);
445 }
446
447 private ParcelableSparseArray getJailedArray(SparseArray<Parcelable> container) {
448 final Parcelable parcelable = container.get(R.id.cell_layout_jail_id);
449 return parcelable instanceof ParcelableSparseArray ?
450 (ParcelableSparseArray) parcelable : new ParcelableSparseArray();
451 }
452
Jeff Sharkey83f111d2009-04-20 21:03:13 -0700453 @Override
Patrick Dubroy1262e362010-10-06 15:49:50 -0700454 protected void onDraw(Canvas canvas) {
Sunny Goyal05739772015-05-19 19:59:09 -0700455 if (!mIsDragTarget) {
456 return;
457 }
458
Michael Jurka3e7c7632010-10-02 16:01:03 -0700459 // When we're large, we are either drawn in a "hover" state (ie when dragging an item to
460 // a neighboring page) or with just a normal background (if backgroundAlpha > 0.0f)
461 // When we're small, we are either drawn normally or in the "accepts drops" state (during
462 // a drag). However, we also drag the mini hover background *over* one of those two
463 // backgrounds
Sunny Goyal05739772015-05-19 19:59:09 -0700464 if (mBackgroundAlpha > 0.0f) {
Sunny Goyal2805e632015-05-20 15:35:32 -0700465 mBackground.draw(canvas);
Michael Jurkaa63c4522010-08-19 13:52:27 -0700466 }
Romain Guya6abce82009-11-10 02:54:41 -0800467
Patrick Dubroy8e58e912010-10-14 13:21:48 -0700468 final Paint paint = mDragOutlinePaint;
Joe Onorato4be866d2010-10-10 11:26:02 -0700469 for (int i = 0; i < mDragOutlines.length; i++) {
Chet Haase472b2812010-10-14 07:02:04 -0700470 final float alpha = mDragOutlineAlphas[i];
Joe Onorato4be866d2010-10-10 11:26:02 -0700471 if (alpha > 0) {
Adam Cohend41fbf52012-02-16 23:53:59 -0800472 final Rect r = mDragOutlines[i];
Winson Chung3a6e7f32013-10-09 15:50:52 -0700473 mTempRect.set(r);
474 Utilities.scaleRectAboutCenter(mTempRect, getChildrenScale());
Joe Onorato4be866d2010-10-10 11:26:02 -0700475 final Bitmap b = (Bitmap) mDragOutlineAnims[i].getTag();
Chet Haase472b2812010-10-14 07:02:04 -0700476 paint.setAlpha((int)(alpha + .5f));
Winson Chung3a6e7f32013-10-09 15:50:52 -0700477 canvas.drawBitmap(b, null, mTempRect, paint);
Winson Chung150fbab2010-09-29 17:14:26 -0700478 }
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700479 }
Patrick Dubroy96864c32011-03-10 17:17:23 -0800480
Adam Cohen482ed822012-03-02 14:15:13 -0800481 if (DEBUG_VISUALIZE_OCCUPIED) {
482 int[] pt = new int[2];
483 ColorDrawable cd = new ColorDrawable(Color.RED);
Adam Cohene7587d22012-05-24 18:50:02 -0700484 cd.setBounds(0, 0, mCellWidth, mCellHeight);
Adam Cohen482ed822012-03-02 14:15:13 -0800485 for (int i = 0; i < mCountX; i++) {
486 for (int j = 0; j < mCountY; j++) {
487 if (mOccupied[i][j]) {
488 cellToPoint(i, j, pt);
489 canvas.save();
490 canvas.translate(pt[0], pt[1]);
491 cd.draw(canvas);
492 canvas.restore();
493 }
494 }
495 }
496 }
497
Andrew Flynn850d2e72012-04-26 16:51:20 -0700498 int previewOffset = FolderRingAnimator.sPreviewSize;
499
Adam Cohen69ce2e52011-07-03 19:25:21 -0700500 // The folder outer / inner ring image(s)
Adam Cohen2e6da152015-05-06 11:42:25 -0700501 DeviceProfile grid = mLauncher.getDeviceProfile();
Adam Cohen69ce2e52011-07-03 19:25:21 -0700502 for (int i = 0; i < mFolderOuterRings.size(); i++) {
503 FolderRingAnimator fra = mFolderOuterRings.get(i);
504
Adam Cohen5108bc02013-09-20 17:04:51 -0700505 Drawable d;
506 int width, height;
Adam Cohen69ce2e52011-07-03 19:25:21 -0700507 cellToPoint(fra.mCellX, fra.mCellY, mTempLocation);
Winson Chung5f8afe62013-08-12 16:19:28 -0700508 View child = getChildAt(fra.mCellX, fra.mCellY);
Adam Cohen558f1c22013-10-09 15:15:24 -0700509
Winson Chung89f97052013-09-20 11:32:26 -0700510 if (child != null) {
Adam Cohen558f1c22013-10-09 15:15:24 -0700511 int centerX = mTempLocation[0] + mCellWidth / 2;
512 int centerY = mTempLocation[1] + previewOffset / 2 +
513 child.getPaddingTop() + grid.folderBackgroundOffset;
514
Adam Cohen5108bc02013-09-20 17:04:51 -0700515 // Draw outer ring, if it exists
516 if (FolderIcon.HAS_OUTER_RING) {
517 d = FolderRingAnimator.sSharedOuterRingDrawable;
518 width = (int) (fra.getOuterRingSize() * getChildrenScale());
519 height = width;
520 canvas.save();
521 canvas.translate(centerX - width / 2, centerY - height / 2);
522 d.setBounds(0, 0, width, height);
523 d.draw(canvas);
524 canvas.restore();
525 }
Adam Cohen69ce2e52011-07-03 19:25:21 -0700526
Winson Chung89f97052013-09-20 11:32:26 -0700527 // Draw inner ring
528 d = FolderRingAnimator.sSharedInnerRingDrawable;
529 width = (int) (fra.getInnerRingSize() * getChildrenScale());
530 height = width;
Winson Chung89f97052013-09-20 11:32:26 -0700531 canvas.save();
532 canvas.translate(centerX - width / 2, centerY - width / 2);
533 d.setBounds(0, 0, width, height);
534 d.draw(canvas);
535 canvas.restore();
536 }
Adam Cohen69ce2e52011-07-03 19:25:21 -0700537 }
Adam Cohenc51934b2011-07-26 21:07:43 -0700538
539 if (mFolderLeaveBehindCell[0] >= 0 && mFolderLeaveBehindCell[1] >= 0) {
540 Drawable d = FolderIcon.sSharedFolderLeaveBehind;
541 int width = d.getIntrinsicWidth();
542 int height = d.getIntrinsicHeight();
543
544 cellToPoint(mFolderLeaveBehindCell[0], mFolderLeaveBehindCell[1], mTempLocation);
Winson Chung5f8afe62013-08-12 16:19:28 -0700545 View child = getChildAt(mFolderLeaveBehindCell[0], mFolderLeaveBehindCell[1]);
Winson Chung89f97052013-09-20 11:32:26 -0700546 if (child != null) {
547 int centerX = mTempLocation[0] + mCellWidth / 2;
548 int centerY = mTempLocation[1] + previewOffset / 2 +
549 child.getPaddingTop() + grid.folderBackgroundOffset;
Adam Cohenc51934b2011-07-26 21:07:43 -0700550
Winson Chung89f97052013-09-20 11:32:26 -0700551 canvas.save();
552 canvas.translate(centerX - width / 2, centerY - width / 2);
553 d.setBounds(0, 0, width, height);
554 d.draw(canvas);
555 canvas.restore();
556 }
Adam Cohenc51934b2011-07-26 21:07:43 -0700557 }
Adam Cohen69ce2e52011-07-03 19:25:21 -0700558 }
559
560 public void showFolderAccept(FolderRingAnimator fra) {
561 mFolderOuterRings.add(fra);
562 }
563
564 public void hideFolderAccept(FolderRingAnimator fra) {
565 if (mFolderOuterRings.contains(fra)) {
566 mFolderOuterRings.remove(fra);
567 }
568 invalidate();
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700569 }
570
Adam Cohenc51934b2011-07-26 21:07:43 -0700571 public void setFolderLeaveBehindCell(int x, int y) {
572 mFolderLeaveBehindCell[0] = x;
573 mFolderLeaveBehindCell[1] = y;
574 invalidate();
575 }
576
577 public void clearFolderLeaveBehind() {
578 mFolderLeaveBehindCell[0] = -1;
579 mFolderLeaveBehindCell[1] = -1;
580 invalidate();
581 }
582
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700583 @Override
Michael Jurkae6235dd2011-10-04 15:02:05 -0700584 public boolean shouldDelayChildPressedState() {
585 return false;
586 }
587
Adam Cohen1462de32012-07-24 22:34:36 -0700588 public void restoreInstanceState(SparseArray<Parcelable> states) {
Sunny Goyal33a152f2014-07-22 12:13:14 -0700589 try {
590 dispatchRestoreInstanceState(states);
591 } catch (IllegalArgumentException ex) {
592 if (LauncherAppState.isDogfoodBuild()) {
593 throw ex;
594 }
595 // Mismatched viewId / viewType preventing restore. Skip restore on production builds.
596 Log.e(TAG, "Ignoring an error while restoring a view instance state", ex);
597 }
Adam Cohen1462de32012-07-24 22:34:36 -0700598 }
599
Michael Jurkae6235dd2011-10-04 15:02:05 -0700600 @Override
Jeff Sharkey83f111d2009-04-20 21:03:13 -0700601 public void cancelLongPress() {
602 super.cancelLongPress();
603
604 // Cancel long press for all children
605 final int count = getChildCount();
606 for (int i = 0; i < count; i++) {
607 final View child = getChildAt(i);
608 child.cancelLongPress();
609 }
610 }
611
Michael Jurkadee05892010-07-27 10:01:56 -0700612 public void setOnInterceptTouchListener(View.OnTouchListener listener) {
613 mInterceptTouchListener = listener;
614 }
615
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800616 public int getCountX() {
Adam Cohend22015c2010-07-26 22:02:18 -0700617 return mCountX;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800618 }
619
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800620 public int getCountY() {
Adam Cohend22015c2010-07-26 22:02:18 -0700621 return mCountY;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800622 }
623
Andrew Flynn0dca1ec2012-02-29 13:33:22 -0800624 public void setIsHotseat(boolean isHotseat) {
625 mIsHotseat = isHotseat;
Winson Chung5f8afe62013-08-12 16:19:28 -0700626 mShortcutsAndWidgets.setIsHotseat(isHotseat);
Andrew Flynn0dca1ec2012-02-29 13:33:22 -0800627 }
628
Sunny Goyale9b651e2015-04-24 11:44:51 -0700629 public boolean isHotseat() {
630 return mIsHotseat;
631 }
632
Andrew Flynn0dca1ec2012-02-29 13:33:22 -0800633 public boolean addViewToCellLayout(View child, int index, int childId, LayoutParams params,
Sunny Goyalf7a29e82015-04-24 15:20:43 -0700634 boolean markCells) {
Winson Chungaafa03c2010-06-11 17:34:16 -0700635 final LayoutParams lp = params;
636
Andrew Flynnde38e422012-05-08 11:22:15 -0700637 // Hotseat icons - remove text
Andrew Flynn0dca1ec2012-02-29 13:33:22 -0800638 if (child instanceof BubbleTextView) {
639 BubbleTextView bubbleChild = (BubbleTextView) child;
Winson Chung5f8afe62013-08-12 16:19:28 -0700640 bubbleChild.setTextVisibility(!mIsHotseat);
Andrew Flynn0dca1ec2012-02-29 13:33:22 -0800641 }
642
Adam Cohen307fe232012-08-16 17:55:58 -0700643 child.setScaleX(getChildrenScale());
644 child.setScaleY(getChildrenScale());
645
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800646 // Generate an id for each view, this assumes we have at most 256x256 cells
647 // per workspace screen
Adam Cohend22015c2010-07-26 22:02:18 -0700648 if (lp.cellX >= 0 && lp.cellX <= mCountX - 1 && lp.cellY >= 0 && lp.cellY <= mCountY - 1) {
Winson Chungaafa03c2010-06-11 17:34:16 -0700649 // If the horizontal or vertical span is set to -1, it is taken to
650 // mean that it spans the extent of the CellLayout
Adam Cohend22015c2010-07-26 22:02:18 -0700651 if (lp.cellHSpan < 0) lp.cellHSpan = mCountX;
652 if (lp.cellVSpan < 0) lp.cellVSpan = mCountY;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800653
Winson Chungaafa03c2010-06-11 17:34:16 -0700654 child.setId(childId);
Tony Wickhama0628cc2015-10-14 15:23:04 -0700655 if (LOGD) {
656 Log.d(TAG, "Adding view to ShortcutsAndWidgetsContainer: " + child);
657 }
Sunny Goyalf7a29e82015-04-24 15:20:43 -0700658 mShortcutsAndWidgets.addView(child, index, lp);
Michael Jurkadee05892010-07-27 10:01:56 -0700659
Michael Jurkaf3ca3ab2010-10-20 17:08:24 -0700660 if (markCells) markCellsAsOccupiedForView(child);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700661
Winson Chungaafa03c2010-06-11 17:34:16 -0700662 return true;
663 }
664 return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800665 }
Michael Jurka3e7c7632010-10-02 16:01:03 -0700666
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800667 @Override
Michael Jurka0280c3b2010-09-17 15:00:07 -0700668 public void removeAllViews() {
669 clearOccupiedCells();
Michael Jurkaa52570f2012-03-20 03:18:20 -0700670 mShortcutsAndWidgets.removeAllViews();
Michael Jurka0280c3b2010-09-17 15:00:07 -0700671 }
672
673 @Override
674 public void removeAllViewsInLayout() {
Michael Jurkaa52570f2012-03-20 03:18:20 -0700675 if (mShortcutsAndWidgets.getChildCount() > 0) {
Michael Jurka7cfc2822011-08-02 20:19:24 -0700676 clearOccupiedCells();
Michael Jurkaa52570f2012-03-20 03:18:20 -0700677 mShortcutsAndWidgets.removeAllViewsInLayout();
Michael Jurka7cfc2822011-08-02 20:19:24 -0700678 }
Michael Jurka0280c3b2010-09-17 15:00:07 -0700679 }
680
681 @Override
682 public void removeView(View view) {
683 markCellsAsUnoccupiedForView(view);
Michael Jurkaa52570f2012-03-20 03:18:20 -0700684 mShortcutsAndWidgets.removeView(view);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700685 }
686
687 @Override
688 public void removeViewAt(int index) {
Michael Jurkaa52570f2012-03-20 03:18:20 -0700689 markCellsAsUnoccupiedForView(mShortcutsAndWidgets.getChildAt(index));
690 mShortcutsAndWidgets.removeViewAt(index);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700691 }
692
693 @Override
694 public void removeViewInLayout(View view) {
695 markCellsAsUnoccupiedForView(view);
Michael Jurkaa52570f2012-03-20 03:18:20 -0700696 mShortcutsAndWidgets.removeViewInLayout(view);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700697 }
698
699 @Override
700 public void removeViews(int start, int count) {
701 for (int i = start; i < start + count; i++) {
Michael Jurkaa52570f2012-03-20 03:18:20 -0700702 markCellsAsUnoccupiedForView(mShortcutsAndWidgets.getChildAt(i));
Michael Jurka0280c3b2010-09-17 15:00:07 -0700703 }
Michael Jurkaa52570f2012-03-20 03:18:20 -0700704 mShortcutsAndWidgets.removeViews(start, count);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700705 }
706
707 @Override
708 public void removeViewsInLayout(int start, int count) {
709 for (int i = start; i < start + count; i++) {
Michael Jurkaa52570f2012-03-20 03:18:20 -0700710 markCellsAsUnoccupiedForView(mShortcutsAndWidgets.getChildAt(i));
Michael Jurka0280c3b2010-09-17 15:00:07 -0700711 }
Michael Jurkaa52570f2012-03-20 03:18:20 -0700712 mShortcutsAndWidgets.removeViewsInLayout(start, count);
Michael Jurkaabded662011-03-04 12:06:57 -0800713 }
714
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700715 /**
Winson Chungaafa03c2010-06-11 17:34:16 -0700716 * Given a point, return the cell that strictly encloses that point
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800717 * @param x X coordinate of the point
718 * @param y Y coordinate of the point
719 * @param result Array of 2 ints to hold the x and y coordinate of the cell
720 */
Sunny Goyale9b651e2015-04-24 11:44:51 -0700721 public void pointToCellExact(int x, int y, int[] result) {
Winson Chung4b825dcd2011-06-19 12:41:22 -0700722 final int hStartPadding = getPaddingLeft();
723 final int vStartPadding = getPaddingTop();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800724
725 result[0] = (x - hStartPadding) / (mCellWidth + mWidthGap);
726 result[1] = (y - vStartPadding) / (mCellHeight + mHeightGap);
727
Adam Cohend22015c2010-07-26 22:02:18 -0700728 final int xAxis = mCountX;
729 final int yAxis = mCountY;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800730
731 if (result[0] < 0) result[0] = 0;
732 if (result[0] >= xAxis) result[0] = xAxis - 1;
733 if (result[1] < 0) result[1] = 0;
734 if (result[1] >= yAxis) result[1] = yAxis - 1;
735 }
Winson Chungaafa03c2010-06-11 17:34:16 -0700736
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800737 /**
738 * Given a point, return the cell that most closely encloses that point
739 * @param x X coordinate of the point
740 * @param y Y coordinate of the point
741 * @param result Array of 2 ints to hold the x and y coordinate of the cell
742 */
743 void pointToCellRounded(int x, int y, int[] result) {
744 pointToCellExact(x + (mCellWidth / 2), y + (mCellHeight / 2), result);
745 }
746
747 /**
748 * Given a cell coordinate, return the point that represents the upper left corner of that cell
Winson Chungaafa03c2010-06-11 17:34:16 -0700749 *
750 * @param cellX X coordinate of the cell
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800751 * @param cellY Y coordinate of the cell
Winson Chungaafa03c2010-06-11 17:34:16 -0700752 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800753 * @param result Array of 2 ints to hold the x and y coordinate of the point
754 */
755 void cellToPoint(int cellX, int cellY, int[] result) {
Winson Chung4b825dcd2011-06-19 12:41:22 -0700756 final int hStartPadding = getPaddingLeft();
757 final int vStartPadding = getPaddingTop();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800758
759 result[0] = hStartPadding + cellX * (mCellWidth + mWidthGap);
760 result[1] = vStartPadding + cellY * (mCellHeight + mHeightGap);
761 }
762
Adam Cohene3e27a82011-04-15 12:07:39 -0700763 /**
Adam Cohen482ed822012-03-02 14:15:13 -0800764 * Given a cell coordinate, return the point that represents the center of the cell
Adam Cohene3e27a82011-04-15 12:07:39 -0700765 *
766 * @param cellX X coordinate of the cell
767 * @param cellY Y coordinate of the cell
768 *
769 * @param result Array of 2 ints to hold the x and y coordinate of the point
770 */
771 void cellToCenterPoint(int cellX, int cellY, int[] result) {
Adam Cohen47a876d2012-03-19 13:21:41 -0700772 regionToCenterPoint(cellX, cellY, 1, 1, result);
773 }
774
775 /**
776 * Given a cell coordinate and span return the point that represents the center of the regio
777 *
778 * @param cellX X coordinate of the cell
779 * @param cellY Y coordinate of the cell
780 *
781 * @param result Array of 2 ints to hold the x and y coordinate of the point
782 */
783 void regionToCenterPoint(int cellX, int cellY, int spanX, int spanY, int[] result) {
Winson Chung4b825dcd2011-06-19 12:41:22 -0700784 final int hStartPadding = getPaddingLeft();
785 final int vStartPadding = getPaddingTop();
Adam Cohen47a876d2012-03-19 13:21:41 -0700786 result[0] = hStartPadding + cellX * (mCellWidth + mWidthGap) +
787 (spanX * mCellWidth + (spanX - 1) * mWidthGap) / 2;
788 result[1] = vStartPadding + cellY * (mCellHeight + mHeightGap) +
789 (spanY * mCellHeight + (spanY - 1) * mHeightGap) / 2;
Adam Cohene3e27a82011-04-15 12:07:39 -0700790 }
791
Adam Cohen19f37922012-03-21 11:59:11 -0700792 /**
793 * Given a cell coordinate and span fills out a corresponding pixel rect
794 *
795 * @param cellX X coordinate of the cell
796 * @param cellY Y coordinate of the cell
797 * @param result Rect in which to write the result
798 */
799 void regionToRect(int cellX, int cellY, int spanX, int spanY, Rect result) {
800 final int hStartPadding = getPaddingLeft();
801 final int vStartPadding = getPaddingTop();
802 final int left = hStartPadding + cellX * (mCellWidth + mWidthGap);
803 final int top = vStartPadding + cellY * (mCellHeight + mHeightGap);
804 result.set(left, top, left + (spanX * mCellWidth + (spanX - 1) * mWidthGap),
805 top + (spanY * mCellHeight + (spanY - 1) * mHeightGap));
806 }
807
Adam Cohen482ed822012-03-02 14:15:13 -0800808 public float getDistanceFromCell(float x, float y, int[] cell) {
809 cellToCenterPoint(cell[0], cell[1], mTmpPoint);
Sunny Goyalf7a29e82015-04-24 15:20:43 -0700810 return (float) Math.hypot(x - mTmpPoint[0], y - mTmpPoint[1]);
Adam Cohen482ed822012-03-02 14:15:13 -0800811 }
812
Romain Guy84f296c2009-11-04 15:00:44 -0800813 int getCellWidth() {
814 return mCellWidth;
815 }
816
817 int getCellHeight() {
818 return mCellHeight;
819 }
820
Adam Cohend4844c32011-02-18 19:25:06 -0800821 int getWidthGap() {
822 return mWidthGap;
823 }
824
825 int getHeightGap() {
826 return mHeightGap;
827 }
828
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700829 public void setFixedSize(int width, int height) {
830 mFixedWidth = width;
831 mFixedHeight = height;
832 }
833
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800834 @Override
835 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800836 int widthSpecMode = MeasureSpec.getMode(widthMeasureSpec);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800837 int heightSpecMode = MeasureSpec.getMode(heightMeasureSpec);
Winson Chung5f8afe62013-08-12 16:19:28 -0700838 int widthSize = MeasureSpec.getSize(widthMeasureSpec);
839 int heightSize = MeasureSpec.getSize(heightMeasureSpec);
Winson Chung2d75f122013-09-23 16:53:31 -0700840 int childWidthSize = widthSize - (getPaddingLeft() + getPaddingRight());
841 int childHeightSize = heightSize - (getPaddingTop() + getPaddingBottom());
Winson Chung11a1a532013-09-13 11:14:45 -0700842 if (mFixedCellWidth < 0 || mFixedCellHeight < 0) {
Sunny Goyalc6205602015-05-21 20:46:33 -0700843 int cw = DeviceProfile.calculateCellWidth(childWidthSize, mCountX);
844 int ch = DeviceProfile.calculateCellHeight(childHeightSize, mCountY);
Winson Chung11a1a532013-09-13 11:14:45 -0700845 if (cw != mCellWidth || ch != mCellHeight) {
846 mCellWidth = cw;
847 mCellHeight = ch;
848 mShortcutsAndWidgets.setCellDimensions(mCellWidth, mCellHeight, mWidthGap,
849 mHeightGap, mCountX, mCountY);
850 }
Winson Chung5f8afe62013-08-12 16:19:28 -0700851 }
Winson Chungaafa03c2010-06-11 17:34:16 -0700852
Winson Chung2d75f122013-09-23 16:53:31 -0700853 int newWidth = childWidthSize;
854 int newHeight = childHeightSize;
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700855 if (mFixedWidth > 0 && mFixedHeight > 0) {
856 newWidth = mFixedWidth;
857 newHeight = mFixedHeight;
858 } else if (widthSpecMode == MeasureSpec.UNSPECIFIED || heightSpecMode == MeasureSpec.UNSPECIFIED) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800859 throw new RuntimeException("CellLayout cannot have UNSPECIFIED dimensions");
860 }
861
Adam Cohend22015c2010-07-26 22:02:18 -0700862 int numWidthGaps = mCountX - 1;
863 int numHeightGaps = mCountY - 1;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800864
Adam Cohen234c4cd2011-07-17 21:03:04 -0700865 if (mOriginalWidthGap < 0 || mOriginalHeightGap < 0) {
Winson Chung2d75f122013-09-23 16:53:31 -0700866 int hSpace = childWidthSize;
867 int vSpace = childHeightSize;
Adam Cohenf4bd5792012-04-27 11:35:29 -0700868 int hFreeSpace = hSpace - (mCountX * mCellWidth);
869 int vFreeSpace = vSpace - (mCountY * mCellHeight);
Winson Chung4b825dcd2011-06-19 12:41:22 -0700870 mWidthGap = Math.min(mMaxGap, numWidthGaps > 0 ? (hFreeSpace / numWidthGaps) : 0);
871 mHeightGap = Math.min(mMaxGap,numHeightGaps > 0 ? (vFreeSpace / numHeightGaps) : 0);
Winson Chung5f8afe62013-08-12 16:19:28 -0700872 mShortcutsAndWidgets.setCellDimensions(mCellWidth, mCellHeight, mWidthGap,
873 mHeightGap, mCountX, mCountY);
Adam Cohen234c4cd2011-07-17 21:03:04 -0700874 } else {
875 mWidthGap = mOriginalWidthGap;
876 mHeightGap = mOriginalHeightGap;
Winson Chungece7f5b2010-10-22 14:54:12 -0700877 }
Sunny Goyal4fe5a372015-05-14 19:55:10 -0700878
879 // Make the feedback view large enough to hold the blur bitmap.
880 mTouchFeedbackView.measure(
881 MeasureSpec.makeMeasureSpec(mCellWidth + mTouchFeedbackView.getExtraSize(),
882 MeasureSpec.EXACTLY),
883 MeasureSpec.makeMeasureSpec(mCellHeight + mTouchFeedbackView.getExtraSize(),
884 MeasureSpec.EXACTLY));
885
886 mShortcutsAndWidgets.measure(
887 MeasureSpec.makeMeasureSpec(newWidth, MeasureSpec.EXACTLY),
888 MeasureSpec.makeMeasureSpec(newHeight, MeasureSpec.EXACTLY));
889
890 int maxWidth = mShortcutsAndWidgets.getMeasuredWidth();
891 int maxHeight = mShortcutsAndWidgets.getMeasuredHeight();
Winson Chung2d75f122013-09-23 16:53:31 -0700892 if (mFixedWidth > 0 && mFixedHeight > 0) {
893 setMeasuredDimension(maxWidth, maxHeight);
894 } else {
895 setMeasuredDimension(widthSize, heightSize);
896 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800897 }
898
899 @Override
Michael Jurka28750fb2010-09-24 17:43:49 -0700900 protected void onLayout(boolean changed, int l, int t, int r, int b) {
Tony Wickham26b01422015-11-10 14:44:32 -0800901 boolean isFullscreen = mShortcutsAndWidgets.getChildCount() > 0 &&
902 ((LayoutParams) mShortcutsAndWidgets.getChildAt(0).getLayoutParams()).isFullscreen;
903 int left = getPaddingLeft();
904 if (!isFullscreen) {
905 int offset = getMeasuredWidth() - getPaddingLeft() - getPaddingRight() -
906 (mCountX * mCellWidth);
907 left += (int) Math.ceil(offset / 2f);
908 }
Winson Chung38848ca2013-10-08 12:03:44 -0700909 int top = getPaddingTop();
Sunny Goyal4fe5a372015-05-14 19:55:10 -0700910
911 mTouchFeedbackView.layout(left, top,
912 left + mTouchFeedbackView.getMeasuredWidth(),
913 top + mTouchFeedbackView.getMeasuredHeight());
914 mShortcutsAndWidgets.layout(left, top,
915 left + r - l,
916 top + b - t);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800917 }
918
919 @Override
Michael Jurkadee05892010-07-27 10:01:56 -0700920 protected void onSizeChanged(int w, int h, int oldw, int oldh) {
921 super.onSizeChanged(w, h, oldw, oldh);
Winson Chung82a9bd22013-10-08 16:02:34 -0700922
923 // Expand the background drawing bounds by the padding baked into the background drawable
Sunny Goyal2805e632015-05-20 15:35:32 -0700924 mBackground.getPadding(mTempRect);
925 mBackground.setBounds(-mTempRect.left, -mTempRect.top,
926 w + mTempRect.right, h + mTempRect.bottom);
Michael Jurkadee05892010-07-27 10:01:56 -0700927 }
928
929 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800930 protected void setChildrenDrawingCacheEnabled(boolean enabled) {
Michael Jurkaa52570f2012-03-20 03:18:20 -0700931 mShortcutsAndWidgets.setChildrenDrawingCacheEnabled(enabled);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800932 }
933
934 @Override
935 protected void setChildrenDrawnWithCacheEnabled(boolean enabled) {
Michael Jurkaa52570f2012-03-20 03:18:20 -0700936 mShortcutsAndWidgets.setChildrenDrawnWithCacheEnabled(enabled);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800937 }
938
Michael Jurka5f1c5092010-09-03 14:15:02 -0700939 public float getBackgroundAlpha() {
940 return mBackgroundAlpha;
Michael Jurkadee05892010-07-27 10:01:56 -0700941 }
942
Michael Jurka5f1c5092010-09-03 14:15:02 -0700943 public void setBackgroundAlpha(float alpha) {
Michael Jurkaafaa0502011-12-13 18:22:50 -0800944 if (mBackgroundAlpha != alpha) {
945 mBackgroundAlpha = alpha;
Sunny Goyal2805e632015-05-20 15:35:32 -0700946 mBackground.setAlpha((int) (mBackgroundAlpha * 255));
Michael Jurkaafaa0502011-12-13 18:22:50 -0800947 }
Michael Jurkadee05892010-07-27 10:01:56 -0700948 }
949
Sunny Goyal2805e632015-05-20 15:35:32 -0700950 @Override
951 protected boolean verifyDrawable(Drawable who) {
952 return super.verifyDrawable(who) || (mIsDragTarget && who == mBackground);
953 }
954
Michael Jurkaa52570f2012-03-20 03:18:20 -0700955 public void setShortcutAndWidgetAlpha(float alpha) {
Sunny Goyal02b50812014-09-10 15:44:42 -0700956 mShortcutsAndWidgets.setAlpha(alpha);
Michael Jurkadee05892010-07-27 10:01:56 -0700957 }
958
Michael Jurkaa52570f2012-03-20 03:18:20 -0700959 public ShortcutAndWidgetContainer getShortcutsAndWidgets() {
Sunny Goyaldcbcc862014-08-12 15:58:36 -0700960 return mShortcutsAndWidgets;
Michael Jurkaa52570f2012-03-20 03:18:20 -0700961 }
962
Patrick Dubroy440c3602010-07-13 17:50:32 -0700963 public View getChildAt(int x, int y) {
Michael Jurkaa52570f2012-03-20 03:18:20 -0700964 return mShortcutsAndWidgets.getChildAt(x, y);
Patrick Dubroy440c3602010-07-13 17:50:32 -0700965 }
966
Adam Cohen76fc0852011-06-17 13:26:23 -0700967 public boolean animateChildToPosition(final View child, int cellX, int cellY, int duration,
Adam Cohen482ed822012-03-02 14:15:13 -0800968 int delay, boolean permanent, boolean adjustOccupied) {
Michael Jurkaa52570f2012-03-20 03:18:20 -0700969 ShortcutAndWidgetContainer clc = getShortcutsAndWidgets();
Adam Cohen482ed822012-03-02 14:15:13 -0800970 boolean[][] occupied = mOccupied;
971 if (!permanent) {
972 occupied = mTmpOccupied;
973 }
974
Adam Cohen19f37922012-03-21 11:59:11 -0700975 if (clc.indexOfChild(child) != -1) {
Adam Cohenbfbfd262011-06-13 16:55:12 -0700976 final LayoutParams lp = (LayoutParams) child.getLayoutParams();
977 final ItemInfo info = (ItemInfo) child.getTag();
978
979 // We cancel any existing animations
980 if (mReorderAnimators.containsKey(lp)) {
981 mReorderAnimators.get(lp).cancel();
982 mReorderAnimators.remove(lp);
983 }
984
Adam Cohen482ed822012-03-02 14:15:13 -0800985 final int oldX = lp.x;
986 final int oldY = lp.y;
987 if (adjustOccupied) {
988 occupied[lp.cellX][lp.cellY] = false;
989 occupied[cellX][cellY] = true;
990 }
Adam Cohenbfbfd262011-06-13 16:55:12 -0700991 lp.isLockedToGrid = true;
Adam Cohen482ed822012-03-02 14:15:13 -0800992 if (permanent) {
993 lp.cellX = info.cellX = cellX;
994 lp.cellY = info.cellY = cellY;
995 } else {
996 lp.tmpCellX = cellX;
997 lp.tmpCellY = cellY;
998 }
Adam Cohenbfbfd262011-06-13 16:55:12 -0700999 clc.setupLp(lp);
1000 lp.isLockedToGrid = false;
Adam Cohen482ed822012-03-02 14:15:13 -08001001 final int newX = lp.x;
1002 final int newY = lp.y;
Adam Cohenbfbfd262011-06-13 16:55:12 -07001003
Adam Cohen76fc0852011-06-17 13:26:23 -07001004 lp.x = oldX;
1005 lp.y = oldY;
Adam Cohen76fc0852011-06-17 13:26:23 -07001006
Adam Cohen482ed822012-03-02 14:15:13 -08001007 // Exit early if we're not actually moving the view
1008 if (oldX == newX && oldY == newY) {
1009 lp.isLockedToGrid = true;
1010 return true;
1011 }
1012
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001013 ValueAnimator va = LauncherAnimUtils.ofFloat(child, 0f, 1f);
Adam Cohen482ed822012-03-02 14:15:13 -08001014 va.setDuration(duration);
1015 mReorderAnimators.put(lp, va);
1016
1017 va.addUpdateListener(new AnimatorUpdateListener() {
1018 @Override
Adam Cohenbfbfd262011-06-13 16:55:12 -07001019 public void onAnimationUpdate(ValueAnimator animation) {
Adam Cohen482ed822012-03-02 14:15:13 -08001020 float r = ((Float) animation.getAnimatedValue()).floatValue();
Adam Cohen19f37922012-03-21 11:59:11 -07001021 lp.x = (int) ((1 - r) * oldX + r * newX);
1022 lp.y = (int) ((1 - r) * oldY + r * newY);
Adam Cohen6b8a02d2012-03-22 15:13:40 -07001023 child.requestLayout();
Adam Cohenbfbfd262011-06-13 16:55:12 -07001024 }
1025 });
Adam Cohen482ed822012-03-02 14:15:13 -08001026 va.addListener(new AnimatorListenerAdapter() {
Adam Cohenbfbfd262011-06-13 16:55:12 -07001027 boolean cancelled = false;
1028 public void onAnimationEnd(Animator animation) {
1029 // If the animation was cancelled, it means that another animation
1030 // has interrupted this one, and we don't want to lock the item into
1031 // place just yet.
1032 if (!cancelled) {
1033 lp.isLockedToGrid = true;
Adam Cohen482ed822012-03-02 14:15:13 -08001034 child.requestLayout();
Adam Cohenbfbfd262011-06-13 16:55:12 -07001035 }
1036 if (mReorderAnimators.containsKey(lp)) {
1037 mReorderAnimators.remove(lp);
1038 }
1039 }
1040 public void onAnimationCancel(Animator animation) {
1041 cancelled = true;
1042 }
1043 });
Adam Cohen482ed822012-03-02 14:15:13 -08001044 va.setStartDelay(delay);
1045 va.start();
Adam Cohenbfbfd262011-06-13 16:55:12 -07001046 return true;
1047 }
1048 return false;
1049 }
1050
Adam Cohen482ed822012-03-02 14:15:13 -08001051 void visualizeDropLocation(View v, Bitmap dragOutline, int originX, int originY, int cellX,
Sunny Goyale78e3d72015-09-24 11:23:31 -07001052 int cellY, int spanX, int spanY, boolean resize, DropTarget.DragObject dragObject) {
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -07001053 final int oldDragCellX = mDragCell[0];
1054 final int oldDragCellY = mDragCell[1];
Adam Cohen482ed822012-03-02 14:15:13 -08001055
Adam Cohen2801caf2011-05-13 20:57:39 -07001056 if (dragOutline == null && v == null) {
Adam Cohen2801caf2011-05-13 20:57:39 -07001057 return;
1058 }
1059
Adam Cohen482ed822012-03-02 14:15:13 -08001060 if (cellX != oldDragCellX || cellY != oldDragCellY) {
Sunny Goyale78e3d72015-09-24 11:23:31 -07001061 Point dragOffset = dragObject.dragView.getDragVisualizeOffset();
1062 Rect dragRegion = dragObject.dragView.getDragRegion();
1063
Adam Cohen482ed822012-03-02 14:15:13 -08001064 mDragCell[0] = cellX;
1065 mDragCell[1] = cellY;
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001066 // Find the top left corner of the rect the object will occupy
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001067 final int[] topLeft = mTmpPoint;
Adam Cohen482ed822012-03-02 14:15:13 -08001068 cellToPoint(cellX, cellY, topLeft);
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001069
Joe Onorato4be866d2010-10-10 11:26:02 -07001070 int left = topLeft[0];
1071 int top = topLeft[1];
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001072
Winson Chungb8c69f32011-10-19 21:36:08 -07001073 if (v != null && dragOffset == null) {
Adam Cohen99e8b402011-03-25 19:23:43 -07001074 // When drawing the drag outline, it did not account for margin offsets
1075 // added by the view's parent.
1076 MarginLayoutParams lp = (MarginLayoutParams) v.getLayoutParams();
1077 left += lp.leftMargin;
1078 top += lp.topMargin;
Winson Chung150fbab2010-09-29 17:14:26 -07001079
Adam Cohen99e8b402011-03-25 19:23:43 -07001080 // Offsets due to the size difference between the View and the dragOutline.
1081 // There is a size difference to account for the outer blur, which may lie
1082 // outside the bounds of the view.
Winson Chunga9abd0e2010-10-27 17:18:37 -07001083 top += (v.getHeight() - dragOutline.getHeight()) / 2;
Adam Cohenae915ce2011-08-25 13:47:22 -07001084 // We center about the x axis
1085 left += ((mCellWidth * spanX) + ((spanX - 1) * mWidthGap)
1086 - dragOutline.getWidth()) / 2;
Adam Cohen66396872011-04-15 17:50:36 -07001087 } else {
Winson Chungb8c69f32011-10-19 21:36:08 -07001088 if (dragOffset != null && dragRegion != null) {
1089 // Center the drag region *horizontally* in the cell and apply a drag
1090 // outline offset
1091 left += dragOffset.x + ((mCellWidth * spanX) + ((spanX - 1) * mWidthGap)
1092 - dragRegion.width()) / 2;
Winson Chung69737c32013-10-08 17:00:19 -07001093 int cHeight = getShortcutsAndWidgets().getCellContentHeight();
1094 int cellPaddingY = (int) Math.max(0, ((mCellHeight - cHeight) / 2f));
1095 top += dragOffset.y + cellPaddingY;
Winson Chungb8c69f32011-10-19 21:36:08 -07001096 } else {
1097 // Center the drag outline in the cell
1098 left += ((mCellWidth * spanX) + ((spanX - 1) * mWidthGap)
1099 - dragOutline.getWidth()) / 2;
1100 top += ((mCellHeight * spanY) + ((spanY - 1) * mHeightGap)
1101 - dragOutline.getHeight()) / 2;
1102 }
Winson Chunga9abd0e2010-10-27 17:18:37 -07001103 }
Joe Onorato4be866d2010-10-10 11:26:02 -07001104 final int oldIndex = mDragOutlineCurrent;
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -07001105 mDragOutlineAnims[oldIndex].animateOut();
1106 mDragOutlineCurrent = (oldIndex + 1) % mDragOutlines.length;
Adam Cohend41fbf52012-02-16 23:53:59 -08001107 Rect r = mDragOutlines[mDragOutlineCurrent];
1108 r.set(left, top, left + dragOutline.getWidth(), top + dragOutline.getHeight());
1109 if (resize) {
Adam Cohen482ed822012-03-02 14:15:13 -08001110 cellToRect(cellX, cellY, spanX, spanY, r);
Adam Cohend41fbf52012-02-16 23:53:59 -08001111 }
Winson Chung150fbab2010-09-29 17:14:26 -07001112
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -07001113 mDragOutlineAnims[mDragOutlineCurrent].setTag(dragOutline);
1114 mDragOutlineAnims[mDragOutlineCurrent].animateIn();
Sunny Goyale78e3d72015-09-24 11:23:31 -07001115
1116 if (dragObject.stateAnnouncer != null) {
1117 String msg;
1118 if (isHotseat()) {
1119 msg = getContext().getString(R.string.move_to_hotseat_position,
1120 Math.max(cellX, cellY) + 1);
1121 } else {
1122 msg = getContext().getString(R.string.move_to_empty_cell,
1123 cellY + 1, cellX + 1);
1124 }
1125 dragObject.stateAnnouncer.announce(msg);
1126 }
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001127 }
1128 }
1129
Adam Cohene0310962011-04-18 16:15:31 -07001130 public void clearDragOutlines() {
1131 final int oldIndex = mDragOutlineCurrent;
1132 mDragOutlineAnims[oldIndex].animateOut();
Adam Cohend41fbf52012-02-16 23:53:59 -08001133 mDragCell[0] = mDragCell[1] = -1;
Adam Cohene0310962011-04-18 16:15:31 -07001134 }
1135
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001136 /**
Jeff Sharkey70864282009-04-07 21:08:40 -07001137 * Find a vacant area that will fit the given bounds nearest the requested
1138 * cell location. Uses Euclidean distance to score multiple vacant areas.
Winson Chungaafa03c2010-06-11 17:34:16 -07001139 *
Romain Guy51afc022009-05-04 18:03:43 -07001140 * @param pixelX The X location at which you want to search for a vacant area.
1141 * @param pixelY The Y location at which you want to search for a vacant area.
Jeff Sharkey70864282009-04-07 21:08:40 -07001142 * @param spanX Horizontal span of the object.
1143 * @param spanY Vertical span of the object.
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001144 * @param result Array in which to place the result, or null (in which case a new array will
1145 * be allocated)
Jeff Sharkey70864282009-04-07 21:08:40 -07001146 * @return The X, Y cell of a vacant area that can contain this object,
1147 * nearest the requested location.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001148 */
Sunny Goyalf7a29e82015-04-24 15:20:43 -07001149 int[] findNearestVacantArea(int pixelX, int pixelY, int spanX, int spanY, int[] result) {
1150 return findNearestVacantArea(pixelX, pixelY, spanX, spanY, spanX, spanY, result, null);
Michael Jurka6a1435d2010-09-27 17:35:12 -07001151 }
Winson Chungaafa03c2010-06-11 17:34:16 -07001152
Michael Jurka6a1435d2010-09-27 17:35:12 -07001153 /**
1154 * Find a vacant area that will fit the given bounds nearest the requested
1155 * cell location. Uses Euclidean distance to score multiple vacant areas.
1156 *
1157 * @param pixelX The X location at which you want to search for a vacant area.
1158 * @param pixelY The Y location at which you want to search for a vacant area.
Adam Cohend41fbf52012-02-16 23:53:59 -08001159 * @param minSpanX The minimum horizontal span required
1160 * @param minSpanY The minimum vertical span required
1161 * @param spanX Horizontal span of the object.
1162 * @param spanY Vertical span of the object.
1163 * @param result Array in which to place the result, or null (in which case a new array will
1164 * be allocated)
1165 * @return The X, Y cell of a vacant area that can contain this object,
1166 * nearest the requested location.
1167 */
1168 int[] findNearestVacantArea(int pixelX, int pixelY, int minSpanX, int minSpanY, int spanX,
1169 int spanY, int[] result, int[] resultSpan) {
Sunny Goyalf7a29e82015-04-24 15:20:43 -07001170 return findNearestArea(pixelX, pixelY, minSpanX, minSpanY, spanX, spanY, true,
Adam Cohend41fbf52012-02-16 23:53:59 -08001171 result, resultSpan);
1172 }
1173
Adam Cohend41fbf52012-02-16 23:53:59 -08001174 private final Stack<Rect> mTempRectStack = new Stack<Rect>();
1175 private void lazyInitTempRectStack() {
1176 if (mTempRectStack.isEmpty()) {
1177 for (int i = 0; i < mCountX * mCountY; i++) {
1178 mTempRectStack.push(new Rect());
1179 }
1180 }
1181 }
Adam Cohen482ed822012-03-02 14:15:13 -08001182
Adam Cohend41fbf52012-02-16 23:53:59 -08001183 private void recycleTempRects(Stack<Rect> used) {
1184 while (!used.isEmpty()) {
1185 mTempRectStack.push(used.pop());
1186 }
1187 }
1188
1189 /**
1190 * Find a vacant area that will fit the given bounds nearest the requested
1191 * cell location. Uses Euclidean distance to score multiple vacant areas.
1192 *
1193 * @param pixelX The X location at which you want to search for a vacant area.
1194 * @param pixelY The Y location at which you want to search for a vacant area.
1195 * @param minSpanX The minimum horizontal span required
1196 * @param minSpanY The minimum vertical span required
1197 * @param spanX Horizontal span of the object.
1198 * @param spanY Vertical span of the object.
1199 * @param ignoreOccupied If true, the result can be an occupied cell
1200 * @param result Array in which to place the result, or null (in which case a new array will
1201 * be allocated)
1202 * @return The X, Y cell of a vacant area that can contain this object,
1203 * nearest the requested location.
1204 */
Sunny Goyalf7a29e82015-04-24 15:20:43 -07001205 private int[] findNearestArea(int pixelX, int pixelY, int minSpanX, int minSpanY, int spanX,
1206 int spanY, boolean ignoreOccupied, int[] result, int[] resultSpan) {
Adam Cohend41fbf52012-02-16 23:53:59 -08001207 lazyInitTempRectStack();
Michael Jurkac6ee42e2010-09-30 12:04:50 -07001208
Adam Cohene3e27a82011-04-15 12:07:39 -07001209 // For items with a spanX / spanY > 1, the passed in point (pixelX, pixelY) corresponds
1210 // to the center of the item, but we are searching based on the top-left cell, so
1211 // we translate the point over to correspond to the top-left.
1212 pixelX -= (mCellWidth + mWidthGap) * (spanX - 1) / 2f;
1213 pixelY -= (mCellHeight + mHeightGap) * (spanY - 1) / 2f;
1214
Jeff Sharkey70864282009-04-07 21:08:40 -07001215 // Keep track of best-scoring drop area
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001216 final int[] bestXY = result != null ? result : new int[2];
Jeff Sharkey70864282009-04-07 21:08:40 -07001217 double bestDistance = Double.MAX_VALUE;
Adam Cohend41fbf52012-02-16 23:53:59 -08001218 final Rect bestRect = new Rect(-1, -1, -1, -1);
1219 final Stack<Rect> validRegions = new Stack<Rect>();
Winson Chungaafa03c2010-06-11 17:34:16 -07001220
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001221 final int countX = mCountX;
1222 final int countY = mCountY;
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001223
Adam Cohend41fbf52012-02-16 23:53:59 -08001224 if (minSpanX <= 0 || minSpanY <= 0 || spanX <= 0 || spanY <= 0 ||
1225 spanX < minSpanX || spanY < minSpanY) {
1226 return bestXY;
1227 }
1228
1229 for (int y = 0; y < countY - (minSpanY - 1); y++) {
Michael Jurkac28de512010-08-13 11:27:44 -07001230 inner:
Adam Cohend41fbf52012-02-16 23:53:59 -08001231 for (int x = 0; x < countX - (minSpanX - 1); x++) {
1232 int ySize = -1;
1233 int xSize = -1;
Adam Cohendf035382011-04-11 17:22:04 -07001234 if (ignoreOccupied) {
Adam Cohend41fbf52012-02-16 23:53:59 -08001235 // First, let's see if this thing fits anywhere
1236 for (int i = 0; i < minSpanX; i++) {
1237 for (int j = 0; j < minSpanY; j++) {
Sunny Goyalf7a29e82015-04-24 15:20:43 -07001238 if (mOccupied[x + i][y + j]) {
Adam Cohendf035382011-04-11 17:22:04 -07001239 continue inner;
1240 }
Michael Jurkac28de512010-08-13 11:27:44 -07001241 }
1242 }
Adam Cohend41fbf52012-02-16 23:53:59 -08001243 xSize = minSpanX;
1244 ySize = minSpanY;
1245
1246 // We know that the item will fit at _some_ acceptable size, now let's see
1247 // how big we can make it. We'll alternate between incrementing x and y spans
1248 // until we hit a limit.
1249 boolean incX = true;
1250 boolean hitMaxX = xSize >= spanX;
1251 boolean hitMaxY = ySize >= spanY;
1252 while (!(hitMaxX && hitMaxY)) {
1253 if (incX && !hitMaxX) {
1254 for (int j = 0; j < ySize; j++) {
Sunny Goyalf7a29e82015-04-24 15:20:43 -07001255 if (x + xSize > countX -1 || mOccupied[x + xSize][y + j]) {
Adam Cohend41fbf52012-02-16 23:53:59 -08001256 // We can't move out horizontally
1257 hitMaxX = true;
1258 }
1259 }
1260 if (!hitMaxX) {
1261 xSize++;
1262 }
1263 } else if (!hitMaxY) {
1264 for (int i = 0; i < xSize; i++) {
Sunny Goyalf7a29e82015-04-24 15:20:43 -07001265 if (y + ySize > countY - 1 || mOccupied[x + i][y + ySize]) {
Adam Cohend41fbf52012-02-16 23:53:59 -08001266 // We can't move out vertically
1267 hitMaxY = true;
1268 }
1269 }
1270 if (!hitMaxY) {
1271 ySize++;
1272 }
1273 }
1274 hitMaxX |= xSize >= spanX;
1275 hitMaxY |= ySize >= spanY;
1276 incX = !incX;
1277 }
1278 incX = true;
1279 hitMaxX = xSize >= spanX;
1280 hitMaxY = ySize >= spanY;
Michael Jurkac28de512010-08-13 11:27:44 -07001281 }
Sunny Goyal2805e632015-05-20 15:35:32 -07001282 final int[] cellXY = mTmpPoint;
Adam Cohene3e27a82011-04-15 12:07:39 -07001283 cellToCenterPoint(x, y, cellXY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001284
Adam Cohend41fbf52012-02-16 23:53:59 -08001285 // We verify that the current rect is not a sub-rect of any of our previous
1286 // candidates. In this case, the current rect is disqualified in favour of the
1287 // containing rect.
1288 Rect currentRect = mTempRectStack.pop();
1289 currentRect.set(x, y, x + xSize, y + ySize);
1290 boolean contained = false;
1291 for (Rect r : validRegions) {
1292 if (r.contains(currentRect)) {
1293 contained = true;
1294 break;
1295 }
1296 }
1297 validRegions.push(currentRect);
Sunny Goyalf7a29e82015-04-24 15:20:43 -07001298 double distance = Math.hypot(cellXY[0] - pixelX, cellXY[1] - pixelY);
Adam Cohen482ed822012-03-02 14:15:13 -08001299
Adam Cohend41fbf52012-02-16 23:53:59 -08001300 if ((distance <= bestDistance && !contained) ||
1301 currentRect.contains(bestRect)) {
Michael Jurkac28de512010-08-13 11:27:44 -07001302 bestDistance = distance;
1303 bestXY[0] = x;
1304 bestXY[1] = y;
Adam Cohend41fbf52012-02-16 23:53:59 -08001305 if (resultSpan != null) {
1306 resultSpan[0] = xSize;
1307 resultSpan[1] = ySize;
1308 }
1309 bestRect.set(currentRect);
Michael Jurkac28de512010-08-13 11:27:44 -07001310 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001311 }
1312 }
1313
Adam Cohenc0dcf592011-06-01 15:30:43 -07001314 // Return -1, -1 if no suitable location found
1315 if (bestDistance == Double.MAX_VALUE) {
1316 bestXY[0] = -1;
1317 bestXY[1] = -1;
Jeff Sharkey70864282009-04-07 21:08:40 -07001318 }
Adam Cohend41fbf52012-02-16 23:53:59 -08001319 recycleTempRects(validRegions);
Adam Cohenc0dcf592011-06-01 15:30:43 -07001320 return bestXY;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001321 }
Winson Chungaafa03c2010-06-11 17:34:16 -07001322
Adam Cohen482ed822012-03-02 14:15:13 -08001323 /**
1324 * Find a vacant area that will fit the given bounds nearest the requested
1325 * cell location, and will also weigh in a suggested direction vector of the
1326 * desired location. This method computers distance based on unit grid distances,
1327 * not pixel distances.
1328 *
Adam Cohen47a876d2012-03-19 13:21:41 -07001329 * @param cellX The X cell nearest to which you want to search for a vacant area.
1330 * @param cellY The Y cell nearest which you want to search for a vacant area.
Adam Cohen482ed822012-03-02 14:15:13 -08001331 * @param spanX Horizontal span of the object.
1332 * @param spanY Vertical span of the object.
Adam Cohen47a876d2012-03-19 13:21:41 -07001333 * @param direction The favored direction in which the views should move from x, y
1334 * @param exactDirectionOnly If this parameter is true, then only solutions where the direction
1335 * matches exactly. Otherwise we find the best matching direction.
1336 * @param occoupied The array which represents which cells in the CellLayout are occupied
1337 * @param blockOccupied The array which represents which cells in the specified block (cellX,
Winson Chung5f8afe62013-08-12 16:19:28 -07001338 * cellY, spanX, spanY) are occupied. This is used when try to move a group of views.
Adam Cohen482ed822012-03-02 14:15:13 -08001339 * @param result Array in which to place the result, or null (in which case a new array will
1340 * be allocated)
1341 * @return The X, Y cell of a vacant area that can contain this object,
1342 * nearest the requested location.
1343 */
1344 private int[] findNearestArea(int cellX, int cellY, int spanX, int spanY, int[] direction,
Adam Cohen47a876d2012-03-19 13:21:41 -07001345 boolean[][] occupied, boolean blockOccupied[][], int[] result) {
Adam Cohen482ed822012-03-02 14:15:13 -08001346 // Keep track of best-scoring drop area
1347 final int[] bestXY = result != null ? result : new int[2];
1348 float bestDistance = Float.MAX_VALUE;
1349 int bestDirectionScore = Integer.MIN_VALUE;
1350
1351 final int countX = mCountX;
1352 final int countY = mCountY;
1353
1354 for (int y = 0; y < countY - (spanY - 1); y++) {
1355 inner:
1356 for (int x = 0; x < countX - (spanX - 1); x++) {
1357 // First, let's see if this thing fits anywhere
1358 for (int i = 0; i < spanX; i++) {
1359 for (int j = 0; j < spanY; j++) {
Adam Cohen47a876d2012-03-19 13:21:41 -07001360 if (occupied[x + i][y + j] && (blockOccupied == null || blockOccupied[i][j])) {
Adam Cohen482ed822012-03-02 14:15:13 -08001361 continue inner;
1362 }
1363 }
1364 }
1365
Sunny Goyalf7a29e82015-04-24 15:20:43 -07001366 float distance = (float) Math.hypot(x - cellX, y - cellY);
Adam Cohen482ed822012-03-02 14:15:13 -08001367 int[] curDirection = mTmpPoint;
Adam Cohen47a876d2012-03-19 13:21:41 -07001368 computeDirectionVector(x - cellX, y - cellY, curDirection);
1369 // The direction score is just the dot product of the two candidate direction
1370 // and that passed in.
Adam Cohen482ed822012-03-02 14:15:13 -08001371 int curDirectionScore = direction[0] * curDirection[0] +
1372 direction[1] * curDirection[1];
Adam Cohen47a876d2012-03-19 13:21:41 -07001373 boolean exactDirectionOnly = false;
1374 boolean directionMatches = direction[0] == curDirection[0] &&
1375 direction[0] == curDirection[0];
1376 if ((directionMatches || !exactDirectionOnly) &&
1377 Float.compare(distance, bestDistance) < 0 || (Float.compare(distance,
Adam Cohen482ed822012-03-02 14:15:13 -08001378 bestDistance) == 0 && curDirectionScore > bestDirectionScore)) {
1379 bestDistance = distance;
1380 bestDirectionScore = curDirectionScore;
1381 bestXY[0] = x;
1382 bestXY[1] = y;
1383 }
1384 }
1385 }
1386
1387 // Return -1, -1 if no suitable location found
1388 if (bestDistance == Float.MAX_VALUE) {
1389 bestXY[0] = -1;
1390 bestXY[1] = -1;
1391 }
1392 return bestXY;
1393 }
1394
1395 private boolean addViewToTempLocation(View v, Rect rectOccupiedByPotentialDrop,
Adam Cohen8baab352012-03-20 17:39:21 -07001396 int[] direction, ItemConfiguration currentState) {
1397 CellAndSpan c = currentState.map.get(v);
Adam Cohen482ed822012-03-02 14:15:13 -08001398 boolean success = false;
Adam Cohen8baab352012-03-20 17:39:21 -07001399 markCellsForView(c.x, c.y, c.spanX, c.spanY, mTmpOccupied, false);
Adam Cohen482ed822012-03-02 14:15:13 -08001400 markCellsForRect(rectOccupiedByPotentialDrop, mTmpOccupied, true);
1401
Adam Cohen8baab352012-03-20 17:39:21 -07001402 findNearestArea(c.x, c.y, c.spanX, c.spanY, direction, mTmpOccupied, null, mTempLocation);
Adam Cohen482ed822012-03-02 14:15:13 -08001403
1404 if (mTempLocation[0] >= 0 && mTempLocation[1] >= 0) {
Adam Cohen8baab352012-03-20 17:39:21 -07001405 c.x = mTempLocation[0];
1406 c.y = mTempLocation[1];
Adam Cohen482ed822012-03-02 14:15:13 -08001407 success = true;
Adam Cohen482ed822012-03-02 14:15:13 -08001408 }
Adam Cohen8baab352012-03-20 17:39:21 -07001409 markCellsForView(c.x, c.y, c.spanX, c.spanY, mTmpOccupied, true);
Adam Cohen482ed822012-03-02 14:15:13 -08001410 return success;
1411 }
1412
Adam Cohenf3900c22012-11-16 18:28:11 -08001413 /**
1414 * This helper class defines a cluster of views. It helps with defining complex edges
1415 * of the cluster and determining how those edges interact with other views. The edges
1416 * essentially define a fine-grained boundary around the cluster of views -- like a more
1417 * precise version of a bounding box.
1418 */
1419 private class ViewCluster {
1420 final static int LEFT = 0;
1421 final static int TOP = 1;
1422 final static int RIGHT = 2;
1423 final static int BOTTOM = 3;
Adam Cohen47a876d2012-03-19 13:21:41 -07001424
Adam Cohenf3900c22012-11-16 18:28:11 -08001425 ArrayList<View> views;
1426 ItemConfiguration config;
1427 Rect boundingRect = new Rect();
Adam Cohen47a876d2012-03-19 13:21:41 -07001428
Adam Cohenf3900c22012-11-16 18:28:11 -08001429 int[] leftEdge = new int[mCountY];
1430 int[] rightEdge = new int[mCountY];
1431 int[] topEdge = new int[mCountX];
1432 int[] bottomEdge = new int[mCountX];
1433 boolean leftEdgeDirty, rightEdgeDirty, topEdgeDirty, bottomEdgeDirty, boundingRectDirty;
1434
1435 @SuppressWarnings("unchecked")
1436 public ViewCluster(ArrayList<View> views, ItemConfiguration config) {
1437 this.views = (ArrayList<View>) views.clone();
1438 this.config = config;
1439 resetEdges();
Adam Cohen47a876d2012-03-19 13:21:41 -07001440 }
1441
Adam Cohenf3900c22012-11-16 18:28:11 -08001442 void resetEdges() {
1443 for (int i = 0; i < mCountX; i++) {
1444 topEdge[i] = -1;
1445 bottomEdge[i] = -1;
1446 }
1447 for (int i = 0; i < mCountY; i++) {
1448 leftEdge[i] = -1;
1449 rightEdge[i] = -1;
1450 }
1451 leftEdgeDirty = true;
1452 rightEdgeDirty = true;
1453 bottomEdgeDirty = true;
1454 topEdgeDirty = true;
1455 boundingRectDirty = true;
1456 }
Adam Cohen47a876d2012-03-19 13:21:41 -07001457
Adam Cohenf3900c22012-11-16 18:28:11 -08001458 void computeEdge(int which, int[] edge) {
1459 int count = views.size();
1460 for (int i = 0; i < count; i++) {
1461 CellAndSpan cs = config.map.get(views.get(i));
1462 switch (which) {
1463 case LEFT:
1464 int left = cs.x;
1465 for (int j = cs.y; j < cs.y + cs.spanY; j++) {
1466 if (left < edge[j] || edge[j] < 0) {
1467 edge[j] = left;
Adam Cohena56dc102012-07-13 13:41:42 -07001468 }
Adam Cohen47a876d2012-03-19 13:21:41 -07001469 }
Adam Cohenf3900c22012-11-16 18:28:11 -08001470 break;
1471 case RIGHT:
1472 int right = cs.x + cs.spanX;
1473 for (int j = cs.y; j < cs.y + cs.spanY; j++) {
1474 if (right > edge[j]) {
1475 edge[j] = right;
1476 }
1477 }
1478 break;
1479 case TOP:
1480 int top = cs.y;
1481 for (int j = cs.x; j < cs.x + cs.spanX; j++) {
1482 if (top < edge[j] || edge[j] < 0) {
1483 edge[j] = top;
1484 }
1485 }
1486 break;
1487 case BOTTOM:
1488 int bottom = cs.y + cs.spanY;
1489 for (int j = cs.x; j < cs.x + cs.spanX; j++) {
1490 if (bottom > edge[j]) {
1491 edge[j] = bottom;
1492 }
1493 }
1494 break;
Adam Cohen47a876d2012-03-19 13:21:41 -07001495 }
1496 }
1497 }
Adam Cohenf3900c22012-11-16 18:28:11 -08001498
1499 boolean isViewTouchingEdge(View v, int whichEdge) {
1500 CellAndSpan cs = config.map.get(v);
1501
1502 int[] edge = getEdge(whichEdge);
1503
1504 switch (whichEdge) {
1505 case LEFT:
1506 for (int i = cs.y; i < cs.y + cs.spanY; i++) {
1507 if (edge[i] == cs.x + cs.spanX) {
1508 return true;
1509 }
1510 }
1511 break;
1512 case RIGHT:
1513 for (int i = cs.y; i < cs.y + cs.spanY; i++) {
1514 if (edge[i] == cs.x) {
1515 return true;
1516 }
1517 }
1518 break;
1519 case TOP:
1520 for (int i = cs.x; i < cs.x + cs.spanX; i++) {
1521 if (edge[i] == cs.y + cs.spanY) {
1522 return true;
1523 }
1524 }
1525 break;
1526 case BOTTOM:
1527 for (int i = cs.x; i < cs.x + cs.spanX; i++) {
1528 if (edge[i] == cs.y) {
1529 return true;
1530 }
1531 }
1532 break;
1533 }
1534 return false;
1535 }
1536
1537 void shift(int whichEdge, int delta) {
1538 for (View v: views) {
1539 CellAndSpan c = config.map.get(v);
1540 switch (whichEdge) {
1541 case LEFT:
1542 c.x -= delta;
1543 break;
1544 case RIGHT:
1545 c.x += delta;
1546 break;
1547 case TOP:
1548 c.y -= delta;
1549 break;
1550 case BOTTOM:
1551 default:
1552 c.y += delta;
1553 break;
1554 }
1555 }
1556 resetEdges();
1557 }
1558
1559 public void addView(View v) {
1560 views.add(v);
1561 resetEdges();
1562 }
1563
1564 public Rect getBoundingRect() {
1565 if (boundingRectDirty) {
1566 boolean first = true;
1567 for (View v: views) {
1568 CellAndSpan c = config.map.get(v);
1569 if (first) {
1570 boundingRect.set(c.x, c.y, c.x + c.spanX, c.y + c.spanY);
1571 first = false;
1572 } else {
1573 boundingRect.union(c.x, c.y, c.x + c.spanX, c.y + c.spanY);
1574 }
1575 }
1576 }
1577 return boundingRect;
1578 }
1579
1580 public int[] getEdge(int which) {
1581 switch (which) {
1582 case LEFT:
1583 return getLeftEdge();
1584 case RIGHT:
1585 return getRightEdge();
1586 case TOP:
1587 return getTopEdge();
1588 case BOTTOM:
1589 default:
1590 return getBottomEdge();
1591 }
1592 }
1593
1594 public int[] getLeftEdge() {
1595 if (leftEdgeDirty) {
1596 computeEdge(LEFT, leftEdge);
1597 }
1598 return leftEdge;
1599 }
1600
1601 public int[] getRightEdge() {
1602 if (rightEdgeDirty) {
1603 computeEdge(RIGHT, rightEdge);
1604 }
1605 return rightEdge;
1606 }
1607
1608 public int[] getTopEdge() {
1609 if (topEdgeDirty) {
1610 computeEdge(TOP, topEdge);
1611 }
1612 return topEdge;
1613 }
1614
1615 public int[] getBottomEdge() {
1616 if (bottomEdgeDirty) {
1617 computeEdge(BOTTOM, bottomEdge);
1618 }
1619 return bottomEdge;
1620 }
1621
1622 PositionComparator comparator = new PositionComparator();
1623 class PositionComparator implements Comparator<View> {
1624 int whichEdge = 0;
1625 public int compare(View left, View right) {
1626 CellAndSpan l = config.map.get(left);
1627 CellAndSpan r = config.map.get(right);
1628 switch (whichEdge) {
1629 case LEFT:
1630 return (r.x + r.spanX) - (l.x + l.spanX);
1631 case RIGHT:
1632 return l.x - r.x;
1633 case TOP:
1634 return (r.y + r.spanY) - (l.y + l.spanY);
1635 case BOTTOM:
1636 default:
1637 return l.y - r.y;
1638 }
1639 }
1640 }
1641
1642 public void sortConfigurationForEdgePush(int edge) {
1643 comparator.whichEdge = edge;
1644 Collections.sort(config.sortedViews, comparator);
1645 }
Adam Cohen47a876d2012-03-19 13:21:41 -07001646 }
1647
Adam Cohenf3900c22012-11-16 18:28:11 -08001648 private boolean pushViewsToTempLocation(ArrayList<View> views, Rect rectOccupiedByPotentialDrop,
1649 int[] direction, View dragView, ItemConfiguration currentState) {
Adam Cohene0489502012-08-27 15:18:53 -07001650
Adam Cohenf3900c22012-11-16 18:28:11 -08001651 ViewCluster cluster = new ViewCluster(views, currentState);
1652 Rect clusterRect = cluster.getBoundingRect();
1653 int whichEdge;
1654 int pushDistance;
1655 boolean fail = false;
1656
1657 // Determine the edge of the cluster that will be leading the push and how far
1658 // the cluster must be shifted.
1659 if (direction[0] < 0) {
1660 whichEdge = ViewCluster.LEFT;
1661 pushDistance = clusterRect.right - rectOccupiedByPotentialDrop.left;
Adam Cohene0489502012-08-27 15:18:53 -07001662 } else if (direction[0] > 0) {
Adam Cohenf3900c22012-11-16 18:28:11 -08001663 whichEdge = ViewCluster.RIGHT;
1664 pushDistance = rectOccupiedByPotentialDrop.right - clusterRect.left;
1665 } else if (direction[1] < 0) {
1666 whichEdge = ViewCluster.TOP;
1667 pushDistance = clusterRect.bottom - rectOccupiedByPotentialDrop.top;
1668 } else {
1669 whichEdge = ViewCluster.BOTTOM;
1670 pushDistance = rectOccupiedByPotentialDrop.bottom - clusterRect.top;
Adam Cohene0489502012-08-27 15:18:53 -07001671 }
1672
Adam Cohenf3900c22012-11-16 18:28:11 -08001673 // Break early for invalid push distance.
1674 if (pushDistance <= 0) {
1675 return false;
Adam Cohene0489502012-08-27 15:18:53 -07001676 }
Adam Cohenf3900c22012-11-16 18:28:11 -08001677
1678 // Mark the occupied state as false for the group of views we want to move.
1679 for (View v: views) {
1680 CellAndSpan c = currentState.map.get(v);
1681 markCellsForView(c.x, c.y, c.spanX, c.spanY, mTmpOccupied, false);
1682 }
1683
1684 // We save the current configuration -- if we fail to find a solution we will revert
1685 // to the initial state. The process of finding a solution modifies the configuration
1686 // in place, hence the need for revert in the failure case.
1687 currentState.save();
1688
1689 // The pushing algorithm is simplified by considering the views in the order in which
1690 // they would be pushed by the cluster. For example, if the cluster is leading with its
1691 // left edge, we consider sort the views by their right edge, from right to left.
1692 cluster.sortConfigurationForEdgePush(whichEdge);
1693
1694 while (pushDistance > 0 && !fail) {
1695 for (View v: currentState.sortedViews) {
1696 // For each view that isn't in the cluster, we see if the leading edge of the
1697 // cluster is contacting the edge of that view. If so, we add that view to the
1698 // cluster.
1699 if (!cluster.views.contains(v) && v != dragView) {
1700 if (cluster.isViewTouchingEdge(v, whichEdge)) {
1701 LayoutParams lp = (LayoutParams) v.getLayoutParams();
1702 if (!lp.canReorder) {
1703 // The push solution includes the all apps button, this is not viable.
1704 fail = true;
1705 break;
1706 }
1707 cluster.addView(v);
1708 CellAndSpan c = currentState.map.get(v);
1709
1710 // Adding view to cluster, mark it as not occupied.
1711 markCellsForView(c.x, c.y, c.spanX, c.spanY, mTmpOccupied, false);
1712 }
1713 }
1714 }
1715 pushDistance--;
1716
1717 // The cluster has been completed, now we move the whole thing over in the appropriate
1718 // direction.
1719 cluster.shift(whichEdge, 1);
1720 }
1721
1722 boolean foundSolution = false;
1723 clusterRect = cluster.getBoundingRect();
1724
1725 // Due to the nature of the algorithm, the only check required to verify a valid solution
1726 // is to ensure that completed shifted cluster lies completely within the cell layout.
1727 if (!fail && clusterRect.left >= 0 && clusterRect.right <= mCountX && clusterRect.top >= 0 &&
1728 clusterRect.bottom <= mCountY) {
1729 foundSolution = true;
1730 } else {
1731 currentState.restore();
1732 }
1733
1734 // In either case, we set the occupied array as marked for the location of the views
1735 for (View v: cluster.views) {
1736 CellAndSpan c = currentState.map.get(v);
1737 markCellsForView(c.x, c.y, c.spanX, c.spanY, mTmpOccupied, true);
1738 }
1739
1740 return foundSolution;
Adam Cohene0489502012-08-27 15:18:53 -07001741 }
1742
Adam Cohen482ed822012-03-02 14:15:13 -08001743 private boolean addViewsToTempLocation(ArrayList<View> views, Rect rectOccupiedByPotentialDrop,
Adam Cohenf3900c22012-11-16 18:28:11 -08001744 int[] direction, View dragView, ItemConfiguration currentState) {
Adam Cohen482ed822012-03-02 14:15:13 -08001745 if (views.size() == 0) return true;
Adam Cohen482ed822012-03-02 14:15:13 -08001746
Adam Cohen8baab352012-03-20 17:39:21 -07001747 boolean success = false;
Adam Cohen482ed822012-03-02 14:15:13 -08001748 Rect boundingRect = null;
Adam Cohen8baab352012-03-20 17:39:21 -07001749 // We construct a rect which represents the entire group of views passed in
Adam Cohen482ed822012-03-02 14:15:13 -08001750 for (View v: views) {
Adam Cohen8baab352012-03-20 17:39:21 -07001751 CellAndSpan c = currentState.map.get(v);
Adam Cohen482ed822012-03-02 14:15:13 -08001752 if (boundingRect == null) {
Adam Cohen8baab352012-03-20 17:39:21 -07001753 boundingRect = new Rect(c.x, c.y, c.x + c.spanX, c.y + c.spanY);
Adam Cohen482ed822012-03-02 14:15:13 -08001754 } else {
Adam Cohen8baab352012-03-20 17:39:21 -07001755 boundingRect.union(c.x, c.y, c.x + c.spanX, c.y + c.spanY);
Adam Cohen482ed822012-03-02 14:15:13 -08001756 }
1757 }
Adam Cohen8baab352012-03-20 17:39:21 -07001758
Adam Cohen8baab352012-03-20 17:39:21 -07001759 // Mark the occupied state as false for the group of views we want to move.
Adam Cohenf3900c22012-11-16 18:28:11 -08001760 for (View v: views) {
Adam Cohen8baab352012-03-20 17:39:21 -07001761 CellAndSpan c = currentState.map.get(v);
1762 markCellsForView(c.x, c.y, c.spanX, c.spanY, mTmpOccupied, false);
1763 }
1764
Adam Cohen47a876d2012-03-19 13:21:41 -07001765 boolean[][] blockOccupied = new boolean[boundingRect.width()][boundingRect.height()];
1766 int top = boundingRect.top;
1767 int left = boundingRect.left;
Adam Cohen8baab352012-03-20 17:39:21 -07001768 // We mark more precisely which parts of the bounding rect are truly occupied, allowing
Adam Cohena56dc102012-07-13 13:41:42 -07001769 // for interlocking.
Adam Cohenf3900c22012-11-16 18:28:11 -08001770 for (View v: views) {
Adam Cohen8baab352012-03-20 17:39:21 -07001771 CellAndSpan c = currentState.map.get(v);
1772 markCellsForView(c.x - left, c.y - top, c.spanX, c.spanY, blockOccupied, true);
Adam Cohen47a876d2012-03-19 13:21:41 -07001773 }
1774
Adam Cohen482ed822012-03-02 14:15:13 -08001775 markCellsForRect(rectOccupiedByPotentialDrop, mTmpOccupied, true);
1776
Adam Cohenf3900c22012-11-16 18:28:11 -08001777 findNearestArea(boundingRect.left, boundingRect.top, boundingRect.width(),
1778 boundingRect.height(), direction, mTmpOccupied, blockOccupied, mTempLocation);
Adam Cohen482ed822012-03-02 14:15:13 -08001779
Adam Cohen8baab352012-03-20 17:39:21 -07001780 // If we successfuly found a location by pushing the block of views, we commit it
Adam Cohen482ed822012-03-02 14:15:13 -08001781 if (mTempLocation[0] >= 0 && mTempLocation[1] >= 0) {
Adam Cohen8baab352012-03-20 17:39:21 -07001782 int deltaX = mTempLocation[0] - boundingRect.left;
1783 int deltaY = mTempLocation[1] - boundingRect.top;
Adam Cohenf3900c22012-11-16 18:28:11 -08001784 for (View v: views) {
Adam Cohen8baab352012-03-20 17:39:21 -07001785 CellAndSpan c = currentState.map.get(v);
1786 c.x += deltaX;
1787 c.y += deltaY;
Adam Cohen482ed822012-03-02 14:15:13 -08001788 }
1789 success = true;
1790 }
Adam Cohen8baab352012-03-20 17:39:21 -07001791
1792 // In either case, we set the occupied array as marked for the location of the views
Adam Cohenf3900c22012-11-16 18:28:11 -08001793 for (View v: views) {
Adam Cohen8baab352012-03-20 17:39:21 -07001794 CellAndSpan c = currentState.map.get(v);
1795 markCellsForView(c.x, c.y, c.spanX, c.spanY, mTmpOccupied, true);
Adam Cohen482ed822012-03-02 14:15:13 -08001796 }
1797 return success;
1798 }
1799
1800 private void markCellsForRect(Rect r, boolean[][] occupied, boolean value) {
1801 markCellsForView(r.left, r.top, r.width(), r.height(), occupied, value);
1802 }
1803
Adam Cohen4abc5bd2012-05-29 21:06:03 -07001804 // This method tries to find a reordering solution which satisfies the push mechanic by trying
1805 // to push items in each of the cardinal directions, in an order based on the direction vector
1806 // passed.
1807 private boolean attemptPushInDirection(ArrayList<View> intersectingViews, Rect occupied,
1808 int[] direction, View ignoreView, ItemConfiguration solution) {
1809 if ((Math.abs(direction[0]) + Math.abs(direction[1])) > 1) {
Winson Chung5f8afe62013-08-12 16:19:28 -07001810 // If the direction vector has two non-zero components, we try pushing
Adam Cohen4abc5bd2012-05-29 21:06:03 -07001811 // separately in each of the components.
1812 int temp = direction[1];
1813 direction[1] = 0;
Adam Cohenf3900c22012-11-16 18:28:11 -08001814
1815 if (pushViewsToTempLocation(intersectingViews, occupied, direction,
Adam Cohen4abc5bd2012-05-29 21:06:03 -07001816 ignoreView, solution)) {
1817 return true;
1818 }
1819 direction[1] = temp;
1820 temp = direction[0];
1821 direction[0] = 0;
Adam Cohenf3900c22012-11-16 18:28:11 -08001822
1823 if (pushViewsToTempLocation(intersectingViews, occupied, direction,
Adam Cohen4abc5bd2012-05-29 21:06:03 -07001824 ignoreView, solution)) {
1825 return true;
1826 }
1827 // Revert the direction
1828 direction[0] = temp;
1829
1830 // Now we try pushing in each component of the opposite direction
1831 direction[0] *= -1;
1832 direction[1] *= -1;
1833 temp = direction[1];
1834 direction[1] = 0;
Adam Cohenf3900c22012-11-16 18:28:11 -08001835 if (pushViewsToTempLocation(intersectingViews, occupied, direction,
Adam Cohen4abc5bd2012-05-29 21:06:03 -07001836 ignoreView, solution)) {
1837 return true;
1838 }
1839
1840 direction[1] = temp;
1841 temp = direction[0];
1842 direction[0] = 0;
Adam Cohenf3900c22012-11-16 18:28:11 -08001843 if (pushViewsToTempLocation(intersectingViews, occupied, direction,
Adam Cohen4abc5bd2012-05-29 21:06:03 -07001844 ignoreView, solution)) {
1845 return true;
1846 }
1847 // revert the direction
1848 direction[0] = temp;
1849 direction[0] *= -1;
1850 direction[1] *= -1;
Winson Chung5f8afe62013-08-12 16:19:28 -07001851
Adam Cohen4abc5bd2012-05-29 21:06:03 -07001852 } else {
1853 // If the direction vector has a single non-zero component, we push first in the
1854 // direction of the vector
Adam Cohenf3900c22012-11-16 18:28:11 -08001855 if (pushViewsToTempLocation(intersectingViews, occupied, direction,
Adam Cohen4abc5bd2012-05-29 21:06:03 -07001856 ignoreView, solution)) {
1857 return true;
1858 }
Adam Cohen4abc5bd2012-05-29 21:06:03 -07001859 // Then we try the opposite direction
1860 direction[0] *= -1;
1861 direction[1] *= -1;
Adam Cohenf3900c22012-11-16 18:28:11 -08001862 if (pushViewsToTempLocation(intersectingViews, occupied, direction,
Adam Cohen4abc5bd2012-05-29 21:06:03 -07001863 ignoreView, solution)) {
1864 return true;
1865 }
1866 // Switch the direction back
1867 direction[0] *= -1;
1868 direction[1] *= -1;
Winson Chung5f8afe62013-08-12 16:19:28 -07001869
1870 // If we have failed to find a push solution with the above, then we try
Adam Cohen4abc5bd2012-05-29 21:06:03 -07001871 // to find a solution by pushing along the perpendicular axis.
1872
1873 // Swap the components
1874 int temp = direction[1];
1875 direction[1] = direction[0];
1876 direction[0] = temp;
Adam Cohenf3900c22012-11-16 18:28:11 -08001877 if (pushViewsToTempLocation(intersectingViews, occupied, direction,
Adam Cohen4abc5bd2012-05-29 21:06:03 -07001878 ignoreView, solution)) {
1879 return true;
1880 }
1881
1882 // Then we try the opposite direction
1883 direction[0] *= -1;
1884 direction[1] *= -1;
Adam Cohenf3900c22012-11-16 18:28:11 -08001885 if (pushViewsToTempLocation(intersectingViews, occupied, direction,
Adam Cohen4abc5bd2012-05-29 21:06:03 -07001886 ignoreView, solution)) {
1887 return true;
1888 }
1889 // Switch the direction back
1890 direction[0] *= -1;
1891 direction[1] *= -1;
1892
1893 // Swap the components back
1894 temp = direction[1];
1895 direction[1] = direction[0];
1896 direction[0] = temp;
1897 }
1898 return false;
1899 }
1900
Adam Cohen482ed822012-03-02 14:15:13 -08001901 private boolean rearrangementExists(int cellX, int cellY, int spanX, int spanY, int[] direction,
Adam Cohen8baab352012-03-20 17:39:21 -07001902 View ignoreView, ItemConfiguration solution) {
Winson Chunge3e03bc2012-05-01 15:10:11 -07001903 // Return early if get invalid cell positions
1904 if (cellX < 0 || cellY < 0) return false;
Adam Cohen482ed822012-03-02 14:15:13 -08001905
Adam Cohen8baab352012-03-20 17:39:21 -07001906 mIntersectingViews.clear();
Adam Cohen482ed822012-03-02 14:15:13 -08001907 mOccupiedRect.set(cellX, cellY, cellX + spanX, cellY + spanY);
Adam Cohen482ed822012-03-02 14:15:13 -08001908
Adam Cohen8baab352012-03-20 17:39:21 -07001909 // Mark the desired location of the view currently being dragged.
Adam Cohen482ed822012-03-02 14:15:13 -08001910 if (ignoreView != null) {
Adam Cohen8baab352012-03-20 17:39:21 -07001911 CellAndSpan c = solution.map.get(ignoreView);
Adam Cohen19f37922012-03-21 11:59:11 -07001912 if (c != null) {
1913 c.x = cellX;
1914 c.y = cellY;
1915 }
Adam Cohen482ed822012-03-02 14:15:13 -08001916 }
Adam Cohen482ed822012-03-02 14:15:13 -08001917 Rect r0 = new Rect(cellX, cellY, cellX + spanX, cellY + spanY);
1918 Rect r1 = new Rect();
Adam Cohen8baab352012-03-20 17:39:21 -07001919 for (View child: solution.map.keySet()) {
Adam Cohen482ed822012-03-02 14:15:13 -08001920 if (child == ignoreView) continue;
Adam Cohen8baab352012-03-20 17:39:21 -07001921 CellAndSpan c = solution.map.get(child);
Adam Cohen482ed822012-03-02 14:15:13 -08001922 LayoutParams lp = (LayoutParams) child.getLayoutParams();
Adam Cohen8baab352012-03-20 17:39:21 -07001923 r1.set(c.x, c.y, c.x + c.spanX, c.y + c.spanY);
Adam Cohen482ed822012-03-02 14:15:13 -08001924 if (Rect.intersects(r0, r1)) {
1925 if (!lp.canReorder) {
1926 return false;
1927 }
1928 mIntersectingViews.add(child);
1929 }
1930 }
Adam Cohen47a876d2012-03-19 13:21:41 -07001931
Adam Cohenfa3c58f2013-12-06 16:10:55 -08001932 solution.intersectingViews = new ArrayList<View>(mIntersectingViews);
1933
Winson Chung5f8afe62013-08-12 16:19:28 -07001934 // First we try to find a solution which respects the push mechanic. That is,
Adam Cohen4abc5bd2012-05-29 21:06:03 -07001935 // we try to find a solution such that no displaced item travels through another item
1936 // without also displacing that item.
1937 if (attemptPushInDirection(mIntersectingViews, mOccupiedRect, direction, ignoreView,
Adam Cohen19f37922012-03-21 11:59:11 -07001938 solution)) {
Adam Cohen47a876d2012-03-19 13:21:41 -07001939 return true;
1940 }
Adam Cohen47a876d2012-03-19 13:21:41 -07001941
Adam Cohen4abc5bd2012-05-29 21:06:03 -07001942 // Next we try moving the views as a block, but without requiring the push mechanic.
Adam Cohenf3900c22012-11-16 18:28:11 -08001943 if (addViewsToTempLocation(mIntersectingViews, mOccupiedRect, direction, ignoreView,
Adam Cohen19f37922012-03-21 11:59:11 -07001944 solution)) {
Adam Cohen482ed822012-03-02 14:15:13 -08001945 return true;
1946 }
Adam Cohen47a876d2012-03-19 13:21:41 -07001947
Adam Cohen482ed822012-03-02 14:15:13 -08001948 // Ok, they couldn't move as a block, let's move them individually
1949 for (View v : mIntersectingViews) {
Adam Cohen8baab352012-03-20 17:39:21 -07001950 if (!addViewToTempLocation(v, mOccupiedRect, direction, solution)) {
Adam Cohen482ed822012-03-02 14:15:13 -08001951 return false;
1952 }
1953 }
1954 return true;
1955 }
1956
1957 /*
1958 * Returns a pair (x, y), where x,y are in {-1, 0, 1} corresponding to vector between
1959 * the provided point and the provided cell
1960 */
Adam Cohen47a876d2012-03-19 13:21:41 -07001961 private void computeDirectionVector(float deltaX, float deltaY, int[] result) {
Adam Cohen482ed822012-03-02 14:15:13 -08001962 double angle = Math.atan(((float) deltaY) / deltaX);
1963
1964 result[0] = 0;
1965 result[1] = 0;
1966 if (Math.abs(Math.cos(angle)) > 0.5f) {
1967 result[0] = (int) Math.signum(deltaX);
1968 }
1969 if (Math.abs(Math.sin(angle)) > 0.5f) {
1970 result[1] = (int) Math.signum(deltaY);
1971 }
1972 }
1973
Adam Cohen8baab352012-03-20 17:39:21 -07001974 private void copyOccupiedArray(boolean[][] occupied) {
1975 for (int i = 0; i < mCountX; i++) {
1976 for (int j = 0; j < mCountY; j++) {
1977 occupied[i][j] = mOccupied[i][j];
1978 }
1979 }
1980 }
1981
Sunny Goyalf7a29e82015-04-24 15:20:43 -07001982 private ItemConfiguration findReorderSolution(int pixelX, int pixelY, int minSpanX, int minSpanY,
Adam Cohenfa3c58f2013-12-06 16:10:55 -08001983 int spanX, int spanY, int[] direction, View dragView, boolean decX,
1984 ItemConfiguration solution) {
Adam Cohen8baab352012-03-20 17:39:21 -07001985 // Copy the current state into the solution. This solution will be manipulated as necessary.
1986 copyCurrentStateToSolution(solution, false);
1987 // Copy the current occupied array into the temporary occupied array. This array will be
1988 // manipulated as necessary to find a solution.
1989 copyOccupiedArray(mTmpOccupied);
Adam Cohen482ed822012-03-02 14:15:13 -08001990
1991 // We find the nearest cell into which we would place the dragged item, assuming there's
1992 // nothing in its way.
1993 int result[] = new int[2];
1994 result = findNearestArea(pixelX, pixelY, spanX, spanY, result);
1995
1996 boolean success = false;
1997 // First we try the exact nearest position of the item being dragged,
1998 // we will then want to try to move this around to other neighbouring positions
Adam Cohen8baab352012-03-20 17:39:21 -07001999 success = rearrangementExists(result[0], result[1], spanX, spanY, direction, dragView,
2000 solution);
Adam Cohen482ed822012-03-02 14:15:13 -08002001
2002 if (!success) {
2003 // We try shrinking the widget down to size in an alternating pattern, shrink 1 in
2004 // x, then 1 in y etc.
2005 if (spanX > minSpanX && (minSpanY == spanY || decX)) {
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002006 return findReorderSolution(pixelX, pixelY, minSpanX, minSpanY, spanX - 1, spanY,
2007 direction, dragView, false, solution);
Adam Cohen482ed822012-03-02 14:15:13 -08002008 } else if (spanY > minSpanY) {
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002009 return findReorderSolution(pixelX, pixelY, minSpanX, minSpanY, spanX, spanY - 1,
2010 direction, dragView, true, solution);
Adam Cohen482ed822012-03-02 14:15:13 -08002011 }
2012 solution.isSolution = false;
2013 } else {
2014 solution.isSolution = true;
2015 solution.dragViewX = result[0];
2016 solution.dragViewY = result[1];
2017 solution.dragViewSpanX = spanX;
2018 solution.dragViewSpanY = spanY;
Adam Cohen482ed822012-03-02 14:15:13 -08002019 }
2020 return solution;
2021 }
2022
2023 private void copyCurrentStateToSolution(ItemConfiguration solution, boolean temp) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07002024 int childCount = mShortcutsAndWidgets.getChildCount();
Adam Cohen482ed822012-03-02 14:15:13 -08002025 for (int i = 0; i < childCount; i++) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07002026 View child = mShortcutsAndWidgets.getChildAt(i);
Adam Cohen482ed822012-03-02 14:15:13 -08002027 LayoutParams lp = (LayoutParams) child.getLayoutParams();
Adam Cohen8baab352012-03-20 17:39:21 -07002028 CellAndSpan c;
Adam Cohen482ed822012-03-02 14:15:13 -08002029 if (temp) {
Adam Cohen8baab352012-03-20 17:39:21 -07002030 c = new CellAndSpan(lp.tmpCellX, lp.tmpCellY, lp.cellHSpan, lp.cellVSpan);
Adam Cohen482ed822012-03-02 14:15:13 -08002031 } else {
Adam Cohen8baab352012-03-20 17:39:21 -07002032 c = new CellAndSpan(lp.cellX, lp.cellY, lp.cellHSpan, lp.cellVSpan);
Adam Cohen482ed822012-03-02 14:15:13 -08002033 }
Adam Cohenf3900c22012-11-16 18:28:11 -08002034 solution.add(child, c);
Adam Cohen482ed822012-03-02 14:15:13 -08002035 }
2036 }
2037
2038 private void copySolutionToTempState(ItemConfiguration solution, View dragView) {
2039 for (int i = 0; i < mCountX; i++) {
2040 for (int j = 0; j < mCountY; j++) {
2041 mTmpOccupied[i][j] = false;
2042 }
2043 }
2044
Michael Jurkaa52570f2012-03-20 03:18:20 -07002045 int childCount = mShortcutsAndWidgets.getChildCount();
Adam Cohen482ed822012-03-02 14:15:13 -08002046 for (int i = 0; i < childCount; i++) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07002047 View child = mShortcutsAndWidgets.getChildAt(i);
Adam Cohen482ed822012-03-02 14:15:13 -08002048 if (child == dragView) continue;
2049 LayoutParams lp = (LayoutParams) child.getLayoutParams();
Adam Cohen8baab352012-03-20 17:39:21 -07002050 CellAndSpan c = solution.map.get(child);
2051 if (c != null) {
2052 lp.tmpCellX = c.x;
2053 lp.tmpCellY = c.y;
2054 lp.cellHSpan = c.spanX;
2055 lp.cellVSpan = c.spanY;
2056 markCellsForView(c.x, c.y, c.spanX, c.spanY, mTmpOccupied, true);
Adam Cohen482ed822012-03-02 14:15:13 -08002057 }
2058 }
2059 markCellsForView(solution.dragViewX, solution.dragViewY, solution.dragViewSpanX,
2060 solution.dragViewSpanY, mTmpOccupied, true);
2061 }
2062
2063 private void animateItemsToSolution(ItemConfiguration solution, View dragView, boolean
2064 commitDragView) {
2065
2066 boolean[][] occupied = DESTRUCTIVE_REORDER ? mOccupied : mTmpOccupied;
2067 for (int i = 0; i < mCountX; i++) {
2068 for (int j = 0; j < mCountY; j++) {
2069 occupied[i][j] = false;
2070 }
2071 }
2072
Michael Jurkaa52570f2012-03-20 03:18:20 -07002073 int childCount = mShortcutsAndWidgets.getChildCount();
Adam Cohen482ed822012-03-02 14:15:13 -08002074 for (int i = 0; i < childCount; i++) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07002075 View child = mShortcutsAndWidgets.getChildAt(i);
Adam Cohen482ed822012-03-02 14:15:13 -08002076 if (child == dragView) continue;
Adam Cohen8baab352012-03-20 17:39:21 -07002077 CellAndSpan c = solution.map.get(child);
2078 if (c != null) {
Adam Cohen19f37922012-03-21 11:59:11 -07002079 animateChildToPosition(child, c.x, c.y, REORDER_ANIMATION_DURATION, 0,
2080 DESTRUCTIVE_REORDER, false);
Adam Cohen8baab352012-03-20 17:39:21 -07002081 markCellsForView(c.x, c.y, c.spanX, c.spanY, occupied, true);
Adam Cohen482ed822012-03-02 14:15:13 -08002082 }
2083 }
2084 if (commitDragView) {
2085 markCellsForView(solution.dragViewX, solution.dragViewY, solution.dragViewSpanX,
2086 solution.dragViewSpanY, occupied, true);
2087 }
2088 }
2089
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002090
2091 // This method starts or changes the reorder preview animations
2092 private void beginOrAdjustReorderPreviewAnimations(ItemConfiguration solution,
2093 View dragView, int delay, int mode) {
Adam Cohen19f37922012-03-21 11:59:11 -07002094 int childCount = mShortcutsAndWidgets.getChildCount();
Adam Cohen19f37922012-03-21 11:59:11 -07002095 for (int i = 0; i < childCount; i++) {
2096 View child = mShortcutsAndWidgets.getChildAt(i);
2097 if (child == dragView) continue;
2098 CellAndSpan c = solution.map.get(child);
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002099 boolean skip = mode == ReorderPreviewAnimation.MODE_HINT && solution.intersectingViews
2100 != null && !solution.intersectingViews.contains(child);
2101
Adam Cohen19f37922012-03-21 11:59:11 -07002102 LayoutParams lp = (LayoutParams) child.getLayoutParams();
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002103 if (c != null && !skip) {
2104 ReorderPreviewAnimation rha = new ReorderPreviewAnimation(child, mode, lp.cellX,
2105 lp.cellY, c.x, c.y, c.spanX, c.spanY);
Adam Cohend024f982012-05-23 18:26:45 -07002106 rha.animate();
Adam Cohen19f37922012-03-21 11:59:11 -07002107 }
2108 }
2109 }
2110
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002111 // Class which represents the reorder preview animations. These animations show that an item is
Adam Cohen19f37922012-03-21 11:59:11 -07002112 // in a temporary state, and hint at where the item will return to.
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002113 class ReorderPreviewAnimation {
Adam Cohen19f37922012-03-21 11:59:11 -07002114 View child;
Adam Cohend024f982012-05-23 18:26:45 -07002115 float finalDeltaX;
2116 float finalDeltaY;
2117 float initDeltaX;
2118 float initDeltaY;
2119 float finalScale;
2120 float initScale;
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002121 int mode;
2122 boolean repeating = false;
2123 private static final int PREVIEW_DURATION = 300;
2124 private static final int HINT_DURATION = Workspace.REORDER_TIMEOUT;
2125
2126 public static final int MODE_HINT = 0;
2127 public static final int MODE_PREVIEW = 1;
2128
Adam Cohene7587d22012-05-24 18:50:02 -07002129 Animator a;
Adam Cohen19f37922012-03-21 11:59:11 -07002130
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002131 public ReorderPreviewAnimation(View child, int mode, int cellX0, int cellY0, int cellX1,
2132 int cellY1, int spanX, int spanY) {
Adam Cohen19f37922012-03-21 11:59:11 -07002133 regionToCenterPoint(cellX0, cellY0, spanX, spanY, mTmpPoint);
2134 final int x0 = mTmpPoint[0];
2135 final int y0 = mTmpPoint[1];
2136 regionToCenterPoint(cellX1, cellY1, spanX, spanY, mTmpPoint);
2137 final int x1 = mTmpPoint[0];
2138 final int y1 = mTmpPoint[1];
2139 final int dX = x1 - x0;
2140 final int dY = y1 - y0;
Adam Cohend024f982012-05-23 18:26:45 -07002141 finalDeltaX = 0;
2142 finalDeltaY = 0;
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002143 int dir = mode == MODE_HINT ? -1 : 1;
Adam Cohen19f37922012-03-21 11:59:11 -07002144 if (dX == dY && dX == 0) {
2145 } else {
2146 if (dY == 0) {
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002147 finalDeltaX = - dir * Math.signum(dX) * mReorderPreviewAnimationMagnitude;
Adam Cohen19f37922012-03-21 11:59:11 -07002148 } else if (dX == 0) {
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002149 finalDeltaY = - dir * Math.signum(dY) * mReorderPreviewAnimationMagnitude;
Adam Cohen19f37922012-03-21 11:59:11 -07002150 } else {
2151 double angle = Math.atan( (float) (dY) / dX);
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002152 finalDeltaX = (int) (- dir * Math.signum(dX) *
2153 Math.abs(Math.cos(angle) * mReorderPreviewAnimationMagnitude));
2154 finalDeltaY = (int) (- dir * Math.signum(dY) *
2155 Math.abs(Math.sin(angle) * mReorderPreviewAnimationMagnitude));
Adam Cohen19f37922012-03-21 11:59:11 -07002156 }
2157 }
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002158 this.mode = mode;
Adam Cohend024f982012-05-23 18:26:45 -07002159 initDeltaX = child.getTranslationX();
2160 initDeltaY = child.getTranslationY();
Adam Cohen307fe232012-08-16 17:55:58 -07002161 finalScale = getChildrenScale() - 4.0f / child.getWidth();
Adam Cohend024f982012-05-23 18:26:45 -07002162 initScale = child.getScaleX();
Adam Cohen19f37922012-03-21 11:59:11 -07002163 this.child = child;
2164 }
2165
Adam Cohend024f982012-05-23 18:26:45 -07002166 void animate() {
Adam Cohen19f37922012-03-21 11:59:11 -07002167 if (mShakeAnimators.containsKey(child)) {
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002168 ReorderPreviewAnimation oldAnimation = mShakeAnimators.get(child);
Adam Cohend024f982012-05-23 18:26:45 -07002169 oldAnimation.cancel();
Adam Cohen19f37922012-03-21 11:59:11 -07002170 mShakeAnimators.remove(child);
Adam Cohene7587d22012-05-24 18:50:02 -07002171 if (finalDeltaX == 0 && finalDeltaY == 0) {
2172 completeAnimationImmediately();
2173 return;
2174 }
Adam Cohen19f37922012-03-21 11:59:11 -07002175 }
Adam Cohend024f982012-05-23 18:26:45 -07002176 if (finalDeltaX == 0 && finalDeltaY == 0) {
Adam Cohen19f37922012-03-21 11:59:11 -07002177 return;
2178 }
Michael Jurkaf1ad6082013-03-13 12:55:46 +01002179 ValueAnimator va = LauncherAnimUtils.ofFloat(child, 0f, 1f);
Adam Cohene7587d22012-05-24 18:50:02 -07002180 a = va;
Tony Wickham489fc562015-09-02 14:45:39 -07002181
2182 // Animations are disabled in power save mode, causing the repeated animation to jump
2183 // spastically between beginning and end states. Since this looks bad, we don't repeat
2184 // the animation in power save mode.
Tony Wickham112ac952015-11-12 12:31:50 -08002185 if (!Utilities.isPowerSaverOn(getContext())) {
Tony Wickham489fc562015-09-02 14:45:39 -07002186 va.setRepeatMode(ValueAnimator.REVERSE);
2187 va.setRepeatCount(ValueAnimator.INFINITE);
2188 }
2189
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002190 va.setDuration(mode == MODE_HINT ? HINT_DURATION : PREVIEW_DURATION);
Adam Cohend024f982012-05-23 18:26:45 -07002191 va.setStartDelay((int) (Math.random() * 60));
Adam Cohen19f37922012-03-21 11:59:11 -07002192 va.addUpdateListener(new AnimatorUpdateListener() {
2193 @Override
2194 public void onAnimationUpdate(ValueAnimator animation) {
2195 float r = ((Float) animation.getAnimatedValue()).floatValue();
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002196 float r1 = (mode == MODE_HINT && repeating) ? 1.0f : r;
2197 float x = r1 * finalDeltaX + (1 - r1) * initDeltaX;
2198 float y = r1 * finalDeltaY + (1 - r1) * initDeltaY;
Adam Cohen19f37922012-03-21 11:59:11 -07002199 child.setTranslationX(x);
2200 child.setTranslationY(y);
Adam Cohend024f982012-05-23 18:26:45 -07002201 float s = r * finalScale + (1 - r) * initScale;
Brandon Keely50e6e562012-05-08 16:28:49 -07002202 child.setScaleX(s);
2203 child.setScaleY(s);
Adam Cohen19f37922012-03-21 11:59:11 -07002204 }
2205 });
2206 va.addListener(new AnimatorListenerAdapter() {
2207 public void onAnimationRepeat(Animator animation) {
Adam Cohen19f37922012-03-21 11:59:11 -07002208 // We make sure to end only after a full period
Adam Cohend024f982012-05-23 18:26:45 -07002209 initDeltaX = 0;
2210 initDeltaY = 0;
Adam Cohen307fe232012-08-16 17:55:58 -07002211 initScale = getChildrenScale();
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002212 repeating = true;
Adam Cohen19f37922012-03-21 11:59:11 -07002213 }
2214 });
Adam Cohen19f37922012-03-21 11:59:11 -07002215 mShakeAnimators.put(child, this);
2216 va.start();
2217 }
2218
Adam Cohend024f982012-05-23 18:26:45 -07002219 private void cancel() {
Adam Cohene7587d22012-05-24 18:50:02 -07002220 if (a != null) {
2221 a.cancel();
2222 }
Adam Cohen19f37922012-03-21 11:59:11 -07002223 }
Adam Cohene7587d22012-05-24 18:50:02 -07002224
Adam Cohen091440a2015-03-18 14:16:05 -07002225 @Thunk void completeAnimationImmediately() {
Adam Cohene7587d22012-05-24 18:50:02 -07002226 if (a != null) {
2227 a.cancel();
2228 }
Brandon Keely50e6e562012-05-08 16:28:49 -07002229
Michael Jurka2ecf9952012-06-18 12:52:28 -07002230 AnimatorSet s = LauncherAnimUtils.createAnimatorSet();
Adam Cohene7587d22012-05-24 18:50:02 -07002231 a = s;
Brandon Keely50e6e562012-05-08 16:28:49 -07002232 s.playTogether(
Adam Cohen307fe232012-08-16 17:55:58 -07002233 LauncherAnimUtils.ofFloat(child, "scaleX", getChildrenScale()),
2234 LauncherAnimUtils.ofFloat(child, "scaleY", getChildrenScale()),
Michael Jurka2ecf9952012-06-18 12:52:28 -07002235 LauncherAnimUtils.ofFloat(child, "translationX", 0f),
2236 LauncherAnimUtils.ofFloat(child, "translationY", 0f)
Brandon Keely50e6e562012-05-08 16:28:49 -07002237 );
2238 s.setDuration(REORDER_ANIMATION_DURATION);
2239 s.setInterpolator(new android.view.animation.DecelerateInterpolator(1.5f));
2240 s.start();
2241 }
Adam Cohen19f37922012-03-21 11:59:11 -07002242 }
2243
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002244 private void completeAndClearReorderPreviewAnimations() {
2245 for (ReorderPreviewAnimation a: mShakeAnimators.values()) {
Brandon Keely50e6e562012-05-08 16:28:49 -07002246 a.completeAnimationImmediately();
Adam Cohen19f37922012-03-21 11:59:11 -07002247 }
2248 mShakeAnimators.clear();
2249 }
2250
Adam Cohen482ed822012-03-02 14:15:13 -08002251 private void commitTempPlacement() {
2252 for (int i = 0; i < mCountX; i++) {
2253 for (int j = 0; j < mCountY; j++) {
2254 mOccupied[i][j] = mTmpOccupied[i][j];
2255 }
2256 }
Michael Jurkaa52570f2012-03-20 03:18:20 -07002257 int childCount = mShortcutsAndWidgets.getChildCount();
Adam Cohen482ed822012-03-02 14:15:13 -08002258 for (int i = 0; i < childCount; i++) {
Adam Cohenea889a22012-03-27 16:45:39 -07002259 View child = mShortcutsAndWidgets.getChildAt(i);
2260 LayoutParams lp = (LayoutParams) child.getLayoutParams();
2261 ItemInfo info = (ItemInfo) child.getTag();
Adam Cohen2acce882012-03-28 19:03:19 -07002262 // We do a null check here because the item info can be null in the case of the
2263 // AllApps button in the hotseat.
2264 if (info != null) {
Adam Cohen487f7dd2012-06-28 18:12:10 -07002265 if (info.cellX != lp.tmpCellX || info.cellY != lp.tmpCellY ||
2266 info.spanX != lp.cellHSpan || info.spanY != lp.cellVSpan) {
2267 info.requiresDbUpdate = true;
2268 }
Adam Cohen2acce882012-03-28 19:03:19 -07002269 info.cellX = lp.cellX = lp.tmpCellX;
2270 info.cellY = lp.cellY = lp.tmpCellY;
Adam Cohenbebf0422012-04-11 18:06:28 -07002271 info.spanX = lp.cellHSpan;
2272 info.spanY = lp.cellVSpan;
Adam Cohen2acce882012-03-28 19:03:19 -07002273 }
Adam Cohen482ed822012-03-02 14:15:13 -08002274 }
Adam Cohen2acce882012-03-28 19:03:19 -07002275 mLauncher.getWorkspace().updateItemLocationsInDatabase(this);
Adam Cohen482ed822012-03-02 14:15:13 -08002276 }
2277
Sunny Goyalf7a29e82015-04-24 15:20:43 -07002278 private void setUseTempCoords(boolean useTempCoords) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07002279 int childCount = mShortcutsAndWidgets.getChildCount();
Adam Cohen482ed822012-03-02 14:15:13 -08002280 for (int i = 0; i < childCount; i++) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07002281 LayoutParams lp = (LayoutParams) mShortcutsAndWidgets.getChildAt(i).getLayoutParams();
Adam Cohen482ed822012-03-02 14:15:13 -08002282 lp.useTmpCoords = useTempCoords;
2283 }
2284 }
2285
Sunny Goyalf7a29e82015-04-24 15:20:43 -07002286 private ItemConfiguration findConfigurationNoShuffle(int pixelX, int pixelY, int minSpanX, int minSpanY,
Adam Cohen482ed822012-03-02 14:15:13 -08002287 int spanX, int spanY, View dragView, ItemConfiguration solution) {
2288 int[] result = new int[2];
2289 int[] resultSpan = new int[2];
Sunny Goyalf7a29e82015-04-24 15:20:43 -07002290 findNearestVacantArea(pixelX, pixelY, minSpanX, minSpanY, spanX, spanY, result,
Adam Cohen482ed822012-03-02 14:15:13 -08002291 resultSpan);
2292 if (result[0] >= 0 && result[1] >= 0) {
2293 copyCurrentStateToSolution(solution, false);
2294 solution.dragViewX = result[0];
2295 solution.dragViewY = result[1];
2296 solution.dragViewSpanX = resultSpan[0];
2297 solution.dragViewSpanY = resultSpan[1];
2298 solution.isSolution = true;
2299 } else {
2300 solution.isSolution = false;
2301 }
2302 return solution;
2303 }
2304
2305 public void prepareChildForDrag(View child) {
2306 markCellsAsUnoccupiedForView(child);
Adam Cohen482ed822012-03-02 14:15:13 -08002307 }
2308
Adam Cohen19f37922012-03-21 11:59:11 -07002309 /* This seems like it should be obvious and straight-forward, but when the direction vector
2310 needs to match with the notion of the dragView pushing other views, we have to employ
2311 a slightly more subtle notion of the direction vector. The question is what two points is
2312 the vector between? The center of the dragView and its desired destination? Not quite, as
2313 this doesn't necessarily coincide with the interaction of the dragView and items occupying
2314 those cells. Instead we use some heuristics to often lock the vector to up, down, left
2315 or right, which helps make pushing feel right.
2316 */
2317 private void getDirectionVectorForDrop(int dragViewCenterX, int dragViewCenterY, int spanX,
2318 int spanY, View dragView, int[] resultDirection) {
2319 int[] targetDestination = new int[2];
2320
2321 findNearestArea(dragViewCenterX, dragViewCenterY, spanX, spanY, targetDestination);
2322 Rect dragRect = new Rect();
2323 regionToRect(targetDestination[0], targetDestination[1], spanX, spanY, dragRect);
2324 dragRect.offset(dragViewCenterX - dragRect.centerX(), dragViewCenterY - dragRect.centerY());
2325
2326 Rect dropRegionRect = new Rect();
2327 getViewsIntersectingRegion(targetDestination[0], targetDestination[1], spanX, spanY,
2328 dragView, dropRegionRect, mIntersectingViews);
2329
2330 int dropRegionSpanX = dropRegionRect.width();
2331 int dropRegionSpanY = dropRegionRect.height();
2332
2333 regionToRect(dropRegionRect.left, dropRegionRect.top, dropRegionRect.width(),
2334 dropRegionRect.height(), dropRegionRect);
2335
2336 int deltaX = (dropRegionRect.centerX() - dragViewCenterX) / spanX;
2337 int deltaY = (dropRegionRect.centerY() - dragViewCenterY) / spanY;
2338
2339 if (dropRegionSpanX == mCountX || spanX == mCountX) {
2340 deltaX = 0;
2341 }
2342 if (dropRegionSpanY == mCountY || spanY == mCountY) {
2343 deltaY = 0;
2344 }
2345
2346 if (deltaX == 0 && deltaY == 0) {
2347 // No idea what to do, give a random direction.
2348 resultDirection[0] = 1;
2349 resultDirection[1] = 0;
2350 } else {
2351 computeDirectionVector(deltaX, deltaY, resultDirection);
2352 }
2353 }
2354
2355 // For a given cell and span, fetch the set of views intersecting the region.
2356 private void getViewsIntersectingRegion(int cellX, int cellY, int spanX, int spanY,
2357 View dragView, Rect boundingRect, ArrayList<View> intersectingViews) {
2358 if (boundingRect != null) {
2359 boundingRect.set(cellX, cellY, cellX + spanX, cellY + spanY);
2360 }
2361 intersectingViews.clear();
2362 Rect r0 = new Rect(cellX, cellY, cellX + spanX, cellY + spanY);
2363 Rect r1 = new Rect();
2364 final int count = mShortcutsAndWidgets.getChildCount();
2365 for (int i = 0; i < count; i++) {
2366 View child = mShortcutsAndWidgets.getChildAt(i);
2367 if (child == dragView) continue;
2368 LayoutParams lp = (LayoutParams) child.getLayoutParams();
2369 r1.set(lp.cellX, lp.cellY, lp.cellX + lp.cellHSpan, lp.cellY + lp.cellVSpan);
2370 if (Rect.intersects(r0, r1)) {
2371 mIntersectingViews.add(child);
2372 if (boundingRect != null) {
2373 boundingRect.union(r1);
2374 }
2375 }
2376 }
2377 }
2378
2379 boolean isNearestDropLocationOccupied(int pixelX, int pixelY, int spanX, int spanY,
2380 View dragView, int[] result) {
2381 result = findNearestArea(pixelX, pixelY, spanX, spanY, result);
2382 getViewsIntersectingRegion(result[0], result[1], spanX, spanY, dragView, null,
2383 mIntersectingViews);
2384 return !mIntersectingViews.isEmpty();
2385 }
2386
2387 void revertTempState() {
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002388 completeAndClearReorderPreviewAnimations();
2389 if (isItemPlacementDirty() && !DESTRUCTIVE_REORDER) {
2390 final int count = mShortcutsAndWidgets.getChildCount();
2391 for (int i = 0; i < count; i++) {
2392 View child = mShortcutsAndWidgets.getChildAt(i);
2393 LayoutParams lp = (LayoutParams) child.getLayoutParams();
2394 if (lp.tmpCellX != lp.cellX || lp.tmpCellY != lp.cellY) {
2395 lp.tmpCellX = lp.cellX;
2396 lp.tmpCellY = lp.cellY;
2397 animateChildToPosition(child, lp.cellX, lp.cellY, REORDER_ANIMATION_DURATION,
2398 0, false, false);
2399 }
Adam Cohen19f37922012-03-21 11:59:11 -07002400 }
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002401 setItemPlacementDirty(false);
Adam Cohen19f37922012-03-21 11:59:11 -07002402 }
Adam Cohen19f37922012-03-21 11:59:11 -07002403 }
2404
Adam Cohenbebf0422012-04-11 18:06:28 -07002405 boolean createAreaForResize(int cellX, int cellY, int spanX, int spanY,
2406 View dragView, int[] direction, boolean commit) {
2407 int[] pixelXY = new int[2];
2408 regionToCenterPoint(cellX, cellY, spanX, spanY, pixelXY);
2409
2410 // First we determine if things have moved enough to cause a different layout
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002411 ItemConfiguration swapSolution = findReorderSolution(pixelXY[0], pixelXY[1], spanX, spanY,
Adam Cohenbebf0422012-04-11 18:06:28 -07002412 spanX, spanY, direction, dragView, true, new ItemConfiguration());
2413
2414 setUseTempCoords(true);
2415 if (swapSolution != null && swapSolution.isSolution) {
2416 // If we're just testing for a possible location (MODE_ACCEPT_DROP), we don't bother
2417 // committing anything or animating anything as we just want to determine if a solution
2418 // exists
2419 copySolutionToTempState(swapSolution, dragView);
2420 setItemPlacementDirty(true);
2421 animateItemsToSolution(swapSolution, dragView, commit);
2422
2423 if (commit) {
2424 commitTempPlacement();
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002425 completeAndClearReorderPreviewAnimations();
Adam Cohenbebf0422012-04-11 18:06:28 -07002426 setItemPlacementDirty(false);
2427 } else {
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002428 beginOrAdjustReorderPreviewAnimations(swapSolution, dragView,
2429 REORDER_ANIMATION_DURATION, ReorderPreviewAnimation.MODE_PREVIEW);
Adam Cohenbebf0422012-04-11 18:06:28 -07002430 }
2431 mShortcutsAndWidgets.requestLayout();
2432 }
2433 return swapSolution.isSolution;
2434 }
2435
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002436 int[] performReorder(int pixelX, int pixelY, int minSpanX, int minSpanY, int spanX, int spanY,
Adam Cohen482ed822012-03-02 14:15:13 -08002437 View dragView, int[] result, int resultSpan[], int mode) {
Adam Cohen482ed822012-03-02 14:15:13 -08002438 // First we determine if things have moved enough to cause a different layout
Adam Cohen47a876d2012-03-19 13:21:41 -07002439 result = findNearestArea(pixelX, pixelY, spanX, spanY, result);
Adam Cohen482ed822012-03-02 14:15:13 -08002440
2441 if (resultSpan == null) {
2442 resultSpan = new int[2];
2443 }
2444
Adam Cohen19f37922012-03-21 11:59:11 -07002445 // When we are checking drop validity or actually dropping, we don't recompute the
2446 // direction vector, since we want the solution to match the preview, and it's possible
2447 // that the exact position of the item has changed to result in a new reordering outcome.
Adam Cohenb209e632012-03-27 17:09:36 -07002448 if ((mode == MODE_ON_DROP || mode == MODE_ON_DROP_EXTERNAL || mode == MODE_ACCEPT_DROP)
2449 && mPreviousReorderDirection[0] != INVALID_DIRECTION) {
Adam Cohen19f37922012-03-21 11:59:11 -07002450 mDirectionVector[0] = mPreviousReorderDirection[0];
2451 mDirectionVector[1] = mPreviousReorderDirection[1];
2452 // We reset this vector after drop
Adam Cohenb209e632012-03-27 17:09:36 -07002453 if (mode == MODE_ON_DROP || mode == MODE_ON_DROP_EXTERNAL) {
2454 mPreviousReorderDirection[0] = INVALID_DIRECTION;
2455 mPreviousReorderDirection[1] = INVALID_DIRECTION;
Adam Cohen19f37922012-03-21 11:59:11 -07002456 }
2457 } else {
2458 getDirectionVectorForDrop(pixelX, pixelY, spanX, spanY, dragView, mDirectionVector);
2459 mPreviousReorderDirection[0] = mDirectionVector[0];
2460 mPreviousReorderDirection[1] = mDirectionVector[1];
2461 }
2462
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002463 // Find a solution involving pushing / displacing any items in the way
2464 ItemConfiguration swapSolution = findReorderSolution(pixelX, pixelY, minSpanX, minSpanY,
Adam Cohen482ed822012-03-02 14:15:13 -08002465 spanX, spanY, mDirectionVector, dragView, true, new ItemConfiguration());
2466
2467 // We attempt the approach which doesn't shuffle views at all
2468 ItemConfiguration noShuffleSolution = findConfigurationNoShuffle(pixelX, pixelY, minSpanX,
2469 minSpanY, spanX, spanY, dragView, new ItemConfiguration());
2470
2471 ItemConfiguration finalSolution = null;
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002472
2473 // If the reorder solution requires resizing (shrinking) the item being dropped, we instead
2474 // favor a solution in which the item is not resized, but
Adam Cohen482ed822012-03-02 14:15:13 -08002475 if (swapSolution.isSolution && swapSolution.area() >= noShuffleSolution.area()) {
2476 finalSolution = swapSolution;
2477 } else if (noShuffleSolution.isSolution) {
2478 finalSolution = noShuffleSolution;
2479 }
2480
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002481 if (mode == MODE_SHOW_REORDER_HINT) {
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002482 if (finalSolution != null) {
Adam Cohenfe692872013-12-11 14:47:23 -08002483 beginOrAdjustReorderPreviewAnimations(finalSolution, dragView, 0,
2484 ReorderPreviewAnimation.MODE_HINT);
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002485 result[0] = finalSolution.dragViewX;
2486 result[1] = finalSolution.dragViewY;
2487 resultSpan[0] = finalSolution.dragViewSpanX;
2488 resultSpan[1] = finalSolution.dragViewSpanY;
2489 } else {
2490 result[0] = result[1] = resultSpan[0] = resultSpan[1] = -1;
2491 }
2492 return result;
2493 }
2494
Adam Cohen482ed822012-03-02 14:15:13 -08002495 boolean foundSolution = true;
2496 if (!DESTRUCTIVE_REORDER) {
2497 setUseTempCoords(true);
2498 }
2499
2500 if (finalSolution != null) {
2501 result[0] = finalSolution.dragViewX;
2502 result[1] = finalSolution.dragViewY;
2503 resultSpan[0] = finalSolution.dragViewSpanX;
2504 resultSpan[1] = finalSolution.dragViewSpanY;
2505
2506 // If we're just testing for a possible location (MODE_ACCEPT_DROP), we don't bother
2507 // committing anything or animating anything as we just want to determine if a solution
2508 // exists
2509 if (mode == MODE_DRAG_OVER || mode == MODE_ON_DROP || mode == MODE_ON_DROP_EXTERNAL) {
2510 if (!DESTRUCTIVE_REORDER) {
2511 copySolutionToTempState(finalSolution, dragView);
2512 }
2513 setItemPlacementDirty(true);
2514 animateItemsToSolution(finalSolution, dragView, mode == MODE_ON_DROP);
2515
Adam Cohen19f37922012-03-21 11:59:11 -07002516 if (!DESTRUCTIVE_REORDER &&
2517 (mode == MODE_ON_DROP || mode == MODE_ON_DROP_EXTERNAL)) {
Adam Cohen482ed822012-03-02 14:15:13 -08002518 commitTempPlacement();
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002519 completeAndClearReorderPreviewAnimations();
Adam Cohen19f37922012-03-21 11:59:11 -07002520 setItemPlacementDirty(false);
2521 } else {
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002522 beginOrAdjustReorderPreviewAnimations(finalSolution, dragView,
2523 REORDER_ANIMATION_DURATION, ReorderPreviewAnimation.MODE_PREVIEW);
Adam Cohen482ed822012-03-02 14:15:13 -08002524 }
2525 }
2526 } else {
2527 foundSolution = false;
2528 result[0] = result[1] = resultSpan[0] = resultSpan[1] = -1;
2529 }
2530
2531 if ((mode == MODE_ON_DROP || !foundSolution) && !DESTRUCTIVE_REORDER) {
2532 setUseTempCoords(false);
2533 }
Adam Cohen482ed822012-03-02 14:15:13 -08002534
Michael Jurkaa52570f2012-03-20 03:18:20 -07002535 mShortcutsAndWidgets.requestLayout();
Adam Cohen482ed822012-03-02 14:15:13 -08002536 return result;
2537 }
2538
Adam Cohen19f37922012-03-21 11:59:11 -07002539 void setItemPlacementDirty(boolean dirty) {
2540 mItemPlacementDirty = dirty;
Adam Cohen482ed822012-03-02 14:15:13 -08002541 }
Adam Cohen19f37922012-03-21 11:59:11 -07002542 boolean isItemPlacementDirty() {
2543 return mItemPlacementDirty;
Adam Cohen482ed822012-03-02 14:15:13 -08002544 }
2545
Adam Cohen091440a2015-03-18 14:16:05 -07002546 @Thunk class ItemConfiguration {
Adam Cohen8baab352012-03-20 17:39:21 -07002547 HashMap<View, CellAndSpan> map = new HashMap<View, CellAndSpan>();
Adam Cohenf3900c22012-11-16 18:28:11 -08002548 private HashMap<View, CellAndSpan> savedMap = new HashMap<View, CellAndSpan>();
2549 ArrayList<View> sortedViews = new ArrayList<View>();
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002550 ArrayList<View> intersectingViews;
Adam Cohen482ed822012-03-02 14:15:13 -08002551 boolean isSolution = false;
2552 int dragViewX, dragViewY, dragViewSpanX, dragViewSpanY;
2553
Adam Cohenf3900c22012-11-16 18:28:11 -08002554 void save() {
2555 // Copy current state into savedMap
2556 for (View v: map.keySet()) {
2557 map.get(v).copy(savedMap.get(v));
2558 }
2559 }
2560
2561 void restore() {
2562 // Restore current state from savedMap
2563 for (View v: savedMap.keySet()) {
2564 savedMap.get(v).copy(map.get(v));
2565 }
2566 }
2567
2568 void add(View v, CellAndSpan cs) {
2569 map.put(v, cs);
2570 savedMap.put(v, new CellAndSpan());
2571 sortedViews.add(v);
2572 }
2573
Adam Cohen482ed822012-03-02 14:15:13 -08002574 int area() {
2575 return dragViewSpanX * dragViewSpanY;
2576 }
Adam Cohen8baab352012-03-20 17:39:21 -07002577 }
2578
2579 private class CellAndSpan {
2580 int x, y;
2581 int spanX, spanY;
2582
Adam Cohenf3900c22012-11-16 18:28:11 -08002583 public CellAndSpan() {
2584 }
2585
2586 public void copy(CellAndSpan copy) {
2587 copy.x = x;
2588 copy.y = y;
2589 copy.spanX = spanX;
2590 copy.spanY = spanY;
2591 }
2592
Adam Cohen8baab352012-03-20 17:39:21 -07002593 public CellAndSpan(int x, int y, int spanX, int spanY) {
2594 this.x = x;
2595 this.y = y;
2596 this.spanX = spanX;
2597 this.spanY = spanY;
Adam Cohen482ed822012-03-02 14:15:13 -08002598 }
Adam Cohenf3900c22012-11-16 18:28:11 -08002599
2600 public String toString() {
2601 return "(" + x + ", " + y + ": " + spanX + ", " + spanY + ")";
2602 }
2603
Adam Cohen482ed822012-03-02 14:15:13 -08002604 }
2605
Adam Cohendf035382011-04-11 17:22:04 -07002606 /**
Adam Cohendf035382011-04-11 17:22:04 -07002607 * Find a starting cell position that will fit the given bounds nearest the requested
2608 * cell location. Uses Euclidean distance to score multiple vacant areas.
2609 *
2610 * @param pixelX The X location at which you want to search for a vacant area.
2611 * @param pixelY The Y location at which you want to search for a vacant area.
2612 * @param spanX Horizontal span of the object.
2613 * @param spanY Vertical span of the object.
2614 * @param ignoreView Considers space occupied by this view as unoccupied
2615 * @param result Previously returned value to possibly recycle.
2616 * @return The X, Y cell of a vacant area that can contain this object,
2617 * nearest the requested location.
2618 */
Sunny Goyalf7a29e82015-04-24 15:20:43 -07002619 int[] findNearestArea(int pixelX, int pixelY, int spanX, int spanY, int[] result) {
2620 return findNearestArea(pixelX, pixelY, spanX, spanY, spanX, spanY, false, result, null);
Adam Cohendf035382011-04-11 17:22:04 -07002621 }
2622
Michael Jurka0280c3b2010-09-17 15:00:07 -07002623 boolean existsEmptyCell() {
2624 return findCellForSpan(null, 1, 1);
2625 }
2626
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002627 /**
Michael Jurka0280c3b2010-09-17 15:00:07 -07002628 * Finds the upper-left coordinate of the first rectangle in the grid that can
2629 * hold a cell of the specified dimensions. If intersectX and intersectY are not -1,
2630 * then this method will only return coordinates for rectangles that contain the cell
2631 * (intersectX, intersectY)
2632 *
2633 * @param cellXY The array that will contain the position of a vacant cell if such a cell
2634 * can be found.
2635 * @param spanX The horizontal span of the cell we want to find.
2636 * @param spanY The vertical span of the cell we want to find.
2637 *
2638 * @return True if a vacant cell of the specified dimension was found, false otherwise.
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07002639 */
Hyunyoung Song3f471442015-04-08 19:01:34 -07002640 public boolean findCellForSpan(int[] cellXY, int spanX, int spanY) {
Michael Jurka28750fb2010-09-24 17:43:49 -07002641 boolean foundCell = false;
Sunny Goyalf7a29e82015-04-24 15:20:43 -07002642 final int endX = mCountX - (spanX - 1);
2643 final int endY = mCountY - (spanY - 1);
Michael Jurka0280c3b2010-09-17 15:00:07 -07002644
Sunny Goyalf7a29e82015-04-24 15:20:43 -07002645 for (int y = 0; y < endY && !foundCell; y++) {
2646 inner:
2647 for (int x = 0; x < endX; x++) {
2648 for (int i = 0; i < spanX; i++) {
2649 for (int j = 0; j < spanY; j++) {
2650 if (mOccupied[x + i][y + j]) {
2651 // small optimization: we can skip to after the column we just found
2652 // an occupied cell
2653 x += i;
2654 continue inner;
Michael Jurka0280c3b2010-09-17 15:00:07 -07002655 }
2656 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07002657 }
Sunny Goyalf7a29e82015-04-24 15:20:43 -07002658 if (cellXY != null) {
2659 cellXY[0] = x;
2660 cellXY[1] = y;
2661 }
2662 foundCell = true;
Michael Jurka0280c3b2010-09-17 15:00:07 -07002663 break;
Michael Jurka0280c3b2010-09-17 15:00:07 -07002664 }
2665 }
2666
Michael Jurka28750fb2010-09-24 17:43:49 -07002667 return foundCell;
Michael Jurka0280c3b2010-09-17 15:00:07 -07002668 }
2669
2670 /**
Winson Chungc07918d2011-07-01 15:35:26 -07002671 * A drag event has begun over this layout.
2672 * It may have begun over this layout (in which case onDragChild is called first),
2673 * or it may have begun on another layout.
2674 */
2675 void onDragEnter() {
Winson Chungc07918d2011-07-01 15:35:26 -07002676 mDragging = true;
2677 }
2678
2679 /**
Michael Jurka0280c3b2010-09-17 15:00:07 -07002680 * Called when drag has left this CellLayout or has been completed (successfully or not)
2681 */
2682 void onDragExit() {
Joe Onorato4be866d2010-10-10 11:26:02 -07002683 // This can actually be called when we aren't in a drag, e.g. when adding a new
2684 // item to this layout via the customize drawer.
2685 // Guard against that case.
2686 if (mDragging) {
2687 mDragging = false;
Patrick Dubroyde7658b2010-09-27 11:15:43 -07002688 }
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -07002689
2690 // Invalidate the drag data
Adam Cohend41fbf52012-02-16 23:53:59 -08002691 mDragCell[0] = mDragCell[1] = -1;
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -07002692 mDragOutlineAnims[mDragOutlineCurrent].animateOut();
2693 mDragOutlineCurrent = (mDragOutlineCurrent + 1) % mDragOutlineAnims.length;
Adam Cohen19f37922012-03-21 11:59:11 -07002694 revertTempState();
Michael Jurka33945b22010-12-21 18:19:38 -08002695 setIsDragOverlapping(false);
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07002696 }
2697
2698 /**
Winson Chungaafa03c2010-06-11 17:34:16 -07002699 * Mark a child as having been dropped.
Patrick Dubroyde7658b2010-09-27 11:15:43 -07002700 * At the beginning of the drag operation, the child may have been on another
Patrick Dubroyce34a972010-10-19 10:34:32 -07002701 * screen, but it is re-parented before this method is called.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002702 *
2703 * @param child The child that is being dropped
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002704 */
Adam Cohen716b51e2011-06-30 12:09:54 -07002705 void onDropChild(View child) {
Romain Guyd94533d2009-08-17 10:01:15 -07002706 if (child != null) {
2707 LayoutParams lp = (LayoutParams) child.getLayoutParams();
Romain Guy84f296c2009-11-04 15:00:44 -08002708 lp.dropped = true;
Romain Guyd94533d2009-08-17 10:01:15 -07002709 child.requestLayout();
Tony Wickham1cdb6d02015-09-17 11:08:27 -07002710 markCellsAsOccupiedForView(child);
Romain Guyd94533d2009-08-17 10:01:15 -07002711 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002712 }
2713
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002714 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002715 * Computes a bounding rectangle for a range of cells
Winson Chungaafa03c2010-06-11 17:34:16 -07002716 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002717 * @param cellX X coordinate of upper left corner expressed as a cell position
2718 * @param cellY Y coordinate of upper left corner expressed as a cell position
Winson Chungaafa03c2010-06-11 17:34:16 -07002719 * @param cellHSpan Width in cells
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002720 * @param cellVSpan Height in cells
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07002721 * @param resultRect Rect into which to put the results
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002722 */
Adam Cohend41fbf52012-02-16 23:53:59 -08002723 public void cellToRect(int cellX, int cellY, int cellHSpan, int cellVSpan, Rect resultRect) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002724 final int cellWidth = mCellWidth;
2725 final int cellHeight = mCellHeight;
2726 final int widthGap = mWidthGap;
2727 final int heightGap = mHeightGap;
Winson Chungaafa03c2010-06-11 17:34:16 -07002728
Winson Chung4b825dcd2011-06-19 12:41:22 -07002729 final int hStartPadding = getPaddingLeft();
2730 final int vStartPadding = getPaddingTop();
Winson Chungaafa03c2010-06-11 17:34:16 -07002731
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002732 int width = cellHSpan * cellWidth + ((cellHSpan - 1) * widthGap);
2733 int height = cellVSpan * cellHeight + ((cellVSpan - 1) * heightGap);
2734
2735 int x = hStartPadding + cellX * (cellWidth + widthGap);
2736 int y = vStartPadding + cellY * (cellHeight + heightGap);
Winson Chungaafa03c2010-06-11 17:34:16 -07002737
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07002738 resultRect.set(x, y, x + width, y + height);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002739 }
Winson Chungaafa03c2010-06-11 17:34:16 -07002740
Michael Jurka0280c3b2010-09-17 15:00:07 -07002741 private void clearOccupiedCells() {
2742 for (int x = 0; x < mCountX; x++) {
2743 for (int y = 0; y < mCountY; y++) {
2744 mOccupied[x][y] = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002745 }
2746 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07002747 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002748
Adam Cohend4844c32011-02-18 19:25:06 -08002749 public void markCellsAsOccupiedForView(View view) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07002750 if (view == null || view.getParent() != mShortcutsAndWidgets) return;
Michael Jurka0280c3b2010-09-17 15:00:07 -07002751 LayoutParams lp = (LayoutParams) view.getLayoutParams();
Sunny Goyalf7a29e82015-04-24 15:20:43 -07002752 markCellsForView(lp.cellX, lp.cellY, lp.cellHSpan, lp.cellVSpan, mOccupied, true);
Michael Jurka0280c3b2010-09-17 15:00:07 -07002753 }
2754
Adam Cohend4844c32011-02-18 19:25:06 -08002755 public void markCellsAsUnoccupiedForView(View view) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07002756 if (view == null || view.getParent() != mShortcutsAndWidgets) return;
Michael Jurka0280c3b2010-09-17 15:00:07 -07002757 LayoutParams lp = (LayoutParams) view.getLayoutParams();
Sunny Goyalf7a29e82015-04-24 15:20:43 -07002758 markCellsForView(lp.cellX, lp.cellY, lp.cellHSpan, lp.cellVSpan, mOccupied, false);
Michael Jurka0280c3b2010-09-17 15:00:07 -07002759 }
2760
Adam Cohen482ed822012-03-02 14:15:13 -08002761 private void markCellsForView(int cellX, int cellY, int spanX, int spanY, boolean[][] occupied,
2762 boolean value) {
2763 if (cellX < 0 || cellY < 0) return;
Michael Jurka0280c3b2010-09-17 15:00:07 -07002764 for (int x = cellX; x < cellX + spanX && x < mCountX; x++) {
2765 for (int y = cellY; y < cellY + spanY && y < mCountY; y++) {
Adam Cohen482ed822012-03-02 14:15:13 -08002766 occupied[x][y] = value;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002767 }
2768 }
2769 }
2770
Adam Cohen2801caf2011-05-13 20:57:39 -07002771 public int getDesiredWidth() {
Michael Jurka8b805b12012-04-18 14:23:14 -07002772 return getPaddingLeft() + getPaddingRight() + (mCountX * mCellWidth) +
Adam Cohen2801caf2011-05-13 20:57:39 -07002773 (Math.max((mCountX - 1), 0) * mWidthGap);
2774 }
2775
2776 public int getDesiredHeight() {
Michael Jurka8b805b12012-04-18 14:23:14 -07002777 return getPaddingTop() + getPaddingBottom() + (mCountY * mCellHeight) +
Adam Cohen2801caf2011-05-13 20:57:39 -07002778 (Math.max((mCountY - 1), 0) * mHeightGap);
2779 }
2780
Michael Jurka66d72172011-04-12 16:29:25 -07002781 public boolean isOccupied(int x, int y) {
2782 if (x < mCountX && y < mCountY) {
2783 return mOccupied[x][y];
2784 } else {
2785 throw new RuntimeException("Position exceeds the bound of this CellLayout");
2786 }
2787 }
2788
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002789 @Override
2790 public ViewGroup.LayoutParams generateLayoutParams(AttributeSet attrs) {
2791 return new CellLayout.LayoutParams(getContext(), attrs);
2792 }
2793
2794 @Override
2795 protected boolean checkLayoutParams(ViewGroup.LayoutParams p) {
2796 return p instanceof CellLayout.LayoutParams;
2797 }
2798
2799 @Override
2800 protected ViewGroup.LayoutParams generateLayoutParams(ViewGroup.LayoutParams p) {
2801 return new CellLayout.LayoutParams(p);
2802 }
2803
2804 public static class LayoutParams extends ViewGroup.MarginLayoutParams {
2805 /**
2806 * Horizontal location of the item in the grid.
2807 */
2808 @ViewDebug.ExportedProperty
2809 public int cellX;
2810
2811 /**
2812 * Vertical location of the item in the grid.
2813 */
2814 @ViewDebug.ExportedProperty
2815 public int cellY;
2816
2817 /**
Adam Cohen482ed822012-03-02 14:15:13 -08002818 * Temporary horizontal location of the item in the grid during reorder
2819 */
2820 public int tmpCellX;
2821
2822 /**
2823 * Temporary vertical location of the item in the grid during reorder
2824 */
2825 public int tmpCellY;
2826
2827 /**
2828 * Indicates that the temporary coordinates should be used to layout the items
2829 */
2830 public boolean useTmpCoords;
2831
2832 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002833 * Number of cells spanned horizontally by the item.
2834 */
2835 @ViewDebug.ExportedProperty
2836 public int cellHSpan;
2837
2838 /**
2839 * Number of cells spanned vertically by the item.
2840 */
2841 @ViewDebug.ExportedProperty
2842 public int cellVSpan;
Winson Chungaafa03c2010-06-11 17:34:16 -07002843
Adam Cohen1b607ed2011-03-03 17:26:50 -08002844 /**
2845 * Indicates whether the item will set its x, y, width and height parameters freely,
2846 * or whether these will be computed based on cellX, cellY, cellHSpan and cellVSpan.
2847 */
Adam Cohend4844c32011-02-18 19:25:06 -08002848 public boolean isLockedToGrid = true;
2849
Adam Cohen482ed822012-03-02 14:15:13 -08002850 /**
Adam Cohenec40b2b2013-07-23 15:52:40 -07002851 * Indicates that this item should use the full extents of its parent.
2852 */
2853 public boolean isFullscreen = false;
2854
2855 /**
Adam Cohen482ed822012-03-02 14:15:13 -08002856 * Indicates whether this item can be reordered. Always true except in the case of the
2857 * the AllApps button.
2858 */
2859 public boolean canReorder = true;
2860
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002861 // X coordinate of the view in the layout.
2862 @ViewDebug.ExportedProperty
2863 int x;
2864 // Y coordinate of the view in the layout.
2865 @ViewDebug.ExportedProperty
2866 int y;
2867
Romain Guy84f296c2009-11-04 15:00:44 -08002868 boolean dropped;
Romain Guyfcb9e712009-10-02 16:06:52 -07002869
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002870 public LayoutParams(Context c, AttributeSet attrs) {
2871 super(c, attrs);
2872 cellHSpan = 1;
2873 cellVSpan = 1;
2874 }
2875
2876 public LayoutParams(ViewGroup.LayoutParams source) {
2877 super(source);
2878 cellHSpan = 1;
2879 cellVSpan = 1;
2880 }
Winson Chungaafa03c2010-06-11 17:34:16 -07002881
2882 public LayoutParams(LayoutParams source) {
2883 super(source);
2884 this.cellX = source.cellX;
2885 this.cellY = source.cellY;
2886 this.cellHSpan = source.cellHSpan;
2887 this.cellVSpan = source.cellVSpan;
2888 }
2889
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002890 public LayoutParams(int cellX, int cellY, int cellHSpan, int cellVSpan) {
Romain Guy8f19cdd2010-01-08 15:07:00 -08002891 super(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002892 this.cellX = cellX;
2893 this.cellY = cellY;
2894 this.cellHSpan = cellHSpan;
2895 this.cellVSpan = cellVSpan;
2896 }
2897
Adam Cohen2374abf2013-04-16 14:56:57 -07002898 public void setup(int cellWidth, int cellHeight, int widthGap, int heightGap,
2899 boolean invertHorizontally, int colCount) {
Adam Cohend4844c32011-02-18 19:25:06 -08002900 if (isLockedToGrid) {
2901 final int myCellHSpan = cellHSpan;
2902 final int myCellVSpan = cellVSpan;
Adam Cohen2374abf2013-04-16 14:56:57 -07002903 int myCellX = useTmpCoords ? tmpCellX : cellX;
2904 int myCellY = useTmpCoords ? tmpCellY : cellY;
2905
2906 if (invertHorizontally) {
2907 myCellX = colCount - myCellX - cellHSpan;
2908 }
Adam Cohen1b607ed2011-03-03 17:26:50 -08002909
Adam Cohend4844c32011-02-18 19:25:06 -08002910 width = myCellHSpan * cellWidth + ((myCellHSpan - 1) * widthGap) -
2911 leftMargin - rightMargin;
2912 height = myCellVSpan * cellHeight + ((myCellVSpan - 1) * heightGap) -
2913 topMargin - bottomMargin;
Winson Chungeecf02d2012-03-02 17:14:58 -08002914 x = (int) (myCellX * (cellWidth + widthGap) + leftMargin);
2915 y = (int) (myCellY * (cellHeight + heightGap) + topMargin);
Adam Cohend4844c32011-02-18 19:25:06 -08002916 }
2917 }
Winson Chungaafa03c2010-06-11 17:34:16 -07002918
Winson Chungaafa03c2010-06-11 17:34:16 -07002919 public String toString() {
2920 return "(" + this.cellX + ", " + this.cellY + ")";
2921 }
Adam Cohen7f4eabe2011-04-21 16:19:16 -07002922
2923 public void setWidth(int width) {
2924 this.width = width;
2925 }
2926
2927 public int getWidth() {
2928 return width;
2929 }
2930
2931 public void setHeight(int height) {
2932 this.height = height;
2933 }
2934
2935 public int getHeight() {
2936 return height;
2937 }
2938
2939 public void setX(int x) {
2940 this.x = x;
2941 }
2942
2943 public int getX() {
2944 return x;
2945 }
2946
2947 public void setY(int y) {
2948 this.y = y;
2949 }
2950
2951 public int getY() {
2952 return y;
2953 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002954 }
2955
Michael Jurka0280c3b2010-09-17 15:00:07 -07002956 // This class stores info for two purposes:
2957 // 1. When dragging items (mDragInfo in Workspace), we store the View, its cellX & cellY,
2958 // its spanX, spanY, and the screen it is on
2959 // 2. When long clicking on an empty cell in a CellLayout, we save information about the
2960 // cellX and cellY coordinates and which page was clicked. We then set this as a tag on
2961 // the CellLayout that was long clicked
Sunny Goyal83a8f042015-05-19 12:52:12 -07002962 public static final class CellInfo {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002963 View cell;
Michael Jurkaa63c4522010-08-19 13:52:27 -07002964 int cellX = -1;
2965 int cellY = -1;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002966 int spanX;
2967 int spanY;
Adam Cohendcd297f2013-06-18 13:13:40 -07002968 long screenId;
Winson Chung3d503fb2011-07-13 17:25:49 -07002969 long container;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002970
Sunny Goyal83a8f042015-05-19 12:52:12 -07002971 public CellInfo(View v, ItemInfo info) {
Adam Cohene0aaa0d2014-05-12 12:44:22 -07002972 cell = v;
2973 cellX = info.cellX;
2974 cellY = info.cellY;
2975 spanX = info.spanX;
2976 spanY = info.spanY;
2977 screenId = info.screenId;
2978 container = info.container;
2979 }
2980
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002981 @Override
2982 public String toString() {
Winson Chungaafa03c2010-06-11 17:34:16 -07002983 return "Cell[view=" + (cell == null ? "null" : cell.getClass())
2984 + ", x=" + cellX + ", y=" + cellY + "]";
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002985 }
2986 }
Michael Jurkad771c962011-08-09 15:00:48 -07002987
Sunny Goyala9116722015-04-29 13:55:58 -07002988 public boolean findVacantCell(int spanX, int spanY, int[] outXY) {
2989 return Utilities.findVacantCell(outXY, spanX, spanY, mCountX, mCountY, mOccupied);
2990 }
Sunny Goyal9ca9c132015-04-29 14:57:22 -07002991
2992 public boolean isRegionVacant(int x, int y, int spanX, int spanY) {
2993 int x2 = x + spanX - 1;
2994 int y2 = y + spanY - 1;
2995 if (x < 0 || y < 0 || x2 >= mCountX || y2 >= mCountY) {
2996 return false;
2997 }
2998 for (int i = x; i <= x2; i++) {
2999 for (int j = y; j <= y2; j++) {
3000 if (mOccupied[i][j]) {
3001 return false;
3002 }
3003 }
3004 }
3005
3006 return true;
3007 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003008}