blob: 772bb7be227d950acd66b7a0e360d3919c64260e [file] [log] [blame]
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001/*
2 * Copyright (C) 2008 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Daniel Sandler325dc232013-06-05 22:57:57 -040017package com.android.launcher3;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080018
Joe Onorato4be866d2010-10-10 11:26:02 -070019import android.animation.Animator;
Michael Jurka629758f2012-06-14 16:18:21 -070020import android.animation.AnimatorListenerAdapter;
Chet Haase00397b12010-10-07 11:13:10 -070021import android.animation.TimeInterpolator;
Patrick Dubroyde7658b2010-09-27 11:15:43 -070022import android.animation.ValueAnimator;
23import android.animation.ValueAnimator.AnimatorUpdateListener;
Adam Cohenc9735cf2015-01-23 16:11:55 -080024import android.annotation.TargetApi;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080025import android.content.Context;
Joe Onorato79e56262009-09-21 15:23:04 -040026import android.content.res.Resources;
Winson Chungaafa03c2010-06-11 17:34:16 -070027import android.content.res.TypedArray;
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 Goyalaa8ef112015-06-12 20:04:41 -070052import com.android.launcher3.LauncherSettings.Favorites;
Sunny Goyale9b651e2015-04-24 11:44:51 -070053import com.android.launcher3.accessibility.DragAndDropAccessibilityDelegate;
54import com.android.launcher3.accessibility.FolderAccessibilityHelper;
55import com.android.launcher3.accessibility.WorkspaceAccessibilityHelper;
Sunny Goyal6c56c682015-07-16 14:09:05 -070056import com.android.launcher3.config.ProviderConfig;
Adam Cohen091440a2015-03-18 14:16:05 -070057import com.android.launcher3.util.Thunk;
Patrick Dubroy8e58e912010-10-14 13:21:48 -070058
Adam Cohen69ce2e52011-07-03 19:25:21 -070059import java.util.ArrayList;
Adam Cohenc0dcf592011-06-01 15:30:43 -070060import java.util.Arrays;
Adam Cohenf3900c22012-11-16 18:28:11 -080061import java.util.Collections;
62import java.util.Comparator;
Adam Cohenbfbfd262011-06-13 16:55:12 -070063import java.util.HashMap;
Adam Cohend41fbf52012-02-16 23:53:59 -080064import java.util.Stack;
Adam Cohenc0dcf592011-06-01 15:30:43 -070065
Sunny Goyal4b6eb262015-05-14 19:24:40 -070066public class CellLayout extends ViewGroup implements BubbleTextShadowHandler {
Sunny Goyale9b651e2015-04-24 11:44:51 -070067 public static final int WORKSPACE_ACCESSIBILITY_DRAG = 2;
68 public static final int FOLDER_ACCESSIBILITY_DRAG = 1;
69
Winson Chungaafa03c2010-06-11 17:34:16 -070070 static final String TAG = "CellLayout";
71
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;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080088
Patrick Dubroyde7658b2010-09-27 11:15:43 -070089 // These are temporary variables to prevent having to allocate a new object just to
90 // return an (x, y) value from helper functions. Do NOT use them to maintain other state.
Adam Cohen091440a2015-03-18 14:16:05 -070091 @Thunk final int[] mTmpPoint = new int[2];
Sunny Goyal2805e632015-05-20 15:35:32 -070092 @Thunk final int[] mTempLocation = new int[2];
Patrick Dubroy6569f2c2010-07-12 14:25:18 -070093
The Android Open Source Project31dd5032009-03-03 19:32:27 -080094 boolean[][] mOccupied;
Adam Cohen482ed822012-03-02 14:15:13 -080095 boolean[][] mTmpOccupied;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080096
Michael Jurkadee05892010-07-27 10:01:56 -070097 private OnTouchListener mInterceptTouchListener;
Mady Melloref044dd2015-06-02 15:35:07 -070098 private StylusEventHelper mStylusEventHelper;
Michael Jurkadee05892010-07-27 10:01:56 -070099
Adam Cohen69ce2e52011-07-03 19:25:21 -0700100 private ArrayList<FolderRingAnimator> mFolderOuterRings = new ArrayList<FolderRingAnimator>();
Adam Cohenc51934b2011-07-26 21:07:43 -0700101 private int[] mFolderLeaveBehindCell = {-1, -1};
Adam Cohen69ce2e52011-07-03 19:25:21 -0700102
Michael Jurka5f1c5092010-09-03 14:15:02 -0700103 private float mBackgroundAlpha;
Adam Cohenf34bab52010-09-30 14:11:56 -0700104
Sunny Goyal2805e632015-05-20 15:35:32 -0700105 private static final int BACKGROUND_ACTIVATE_DURATION = 120;
106 private final TransitionDrawable mBackground;
107
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700108 // These values allow a fixed measurement to be set on the CellLayout.
109 private int mFixedWidth = -1;
110 private int mFixedHeight = -1;
111
Michael Jurka33945b22010-12-21 18:19:38 -0800112 // If we're actively dragging something over this screen, mIsDragOverlapping is true
113 private boolean mIsDragOverlapping = false;
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700114
Winson Chung150fbab2010-09-29 17:14:26 -0700115 // These arrays are used to implement the drag visualization on x-large screens.
Joe Onorato4be866d2010-10-10 11:26:02 -0700116 // They are used as circular arrays, indexed by mDragOutlineCurrent.
Adam Cohen091440a2015-03-18 14:16:05 -0700117 @Thunk Rect[] mDragOutlines = new Rect[4];
118 @Thunk float[] mDragOutlineAlphas = new float[mDragOutlines.length];
Joe Onorato4be866d2010-10-10 11:26:02 -0700119 private InterruptibleInOutAnimator[] mDragOutlineAnims =
120 new InterruptibleInOutAnimator[mDragOutlines.length];
Winson Chung150fbab2010-09-29 17:14:26 -0700121
122 // Used as an index into the above 3 arrays; indicates which is the most current value.
Joe Onorato4be866d2010-10-10 11:26:02 -0700123 private int mDragOutlineCurrent = 0;
Patrick Dubroy8e58e912010-10-14 13:21:48 -0700124 private final Paint mDragOutlinePaint = new Paint();
Winson Chung150fbab2010-09-29 17:14:26 -0700125
Sunny Goyal4fe5a372015-05-14 19:55:10 -0700126 private final ClickShadowView mTouchFeedbackView;
Patrick Dubroy96864c32011-03-10 17:17:23 -0800127
Sunny Goyal316490e2015-06-02 09:38:28 -0700128 @Thunk HashMap<CellLayout.LayoutParams, Animator> mReorderAnimators = new HashMap<>();
129 @Thunk HashMap<View, ReorderPreviewAnimation> mShakeAnimators = new HashMap<>();
Adam Cohen19f37922012-03-21 11:59:11 -0700130
131 private boolean mItemPlacementDirty = false;
Adam Cohenbfbfd262011-06-13 16:55:12 -0700132
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700133 // When a drag operation is in progress, holds the nearest cell to the touch point
134 private final int[] mDragCell = new int[2];
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800135
Joe Onorato4be866d2010-10-10 11:26:02 -0700136 private boolean mDragging = false;
137
Patrick Dubroyce34a972010-10-19 10:34:32 -0700138 private TimeInterpolator mEaseOutInterpolator;
Michael Jurkaa52570f2012-03-20 03:18:20 -0700139 private ShortcutAndWidgetContainer mShortcutsAndWidgets;
Patrick Dubroyce34a972010-10-19 10:34:32 -0700140
Andrew Flynn0dca1ec2012-02-29 13:33:22 -0800141 private boolean mIsHotseat = false;
Adam Cohen307fe232012-08-16 17:55:58 -0700142 private float mHotseatScale = 1f;
Andrew Flynn0dca1ec2012-02-29 13:33:22 -0800143
Adam Cohenfa3c58f2013-12-06 16:10:55 -0800144 public static final int MODE_SHOW_REORDER_HINT = 0;
145 public static final int MODE_DRAG_OVER = 1;
146 public static final int MODE_ON_DROP = 2;
147 public static final int MODE_ON_DROP_EXTERNAL = 3;
148 public static final int MODE_ACCEPT_DROP = 4;
Adam Cohen19f37922012-03-21 11:59:11 -0700149 private static final boolean DESTRUCTIVE_REORDER = false;
Adam Cohen482ed822012-03-02 14:15:13 -0800150 private static final boolean DEBUG_VISUALIZE_OCCUPIED = false;
151
Adam Cohena897f392012-04-27 18:12:05 -0700152 static final int LANDSCAPE = 0;
153 static final int PORTRAIT = 1;
154
Adam Cohenfa3c58f2013-12-06 16:10:55 -0800155 private static final float REORDER_PREVIEW_MAGNITUDE = 0.12f;
Adam Cohen19f37922012-03-21 11:59:11 -0700156 private static final int REORDER_ANIMATION_DURATION = 150;
Adam Cohen091440a2015-03-18 14:16:05 -0700157 @Thunk float mReorderPreviewAnimationMagnitude;
Adam Cohen19f37922012-03-21 11:59:11 -0700158
Adam Cohen482ed822012-03-02 14:15:13 -0800159 private ArrayList<View> mIntersectingViews = new ArrayList<View>();
160 private Rect mOccupiedRect = new Rect();
161 private int[] mDirectionVector = new int[2];
Adam Cohen19f37922012-03-21 11:59:11 -0700162 int[] mPreviousReorderDirection = new int[2];
Adam Cohenb209e632012-03-27 17:09:36 -0700163 private static final int INVALID_DIRECTION = -100;
Adam Cohen482ed822012-03-02 14:15:13 -0800164
Sunny Goyal2805e632015-05-20 15:35:32 -0700165 private final Rect mTempRect = new Rect();
Winson Chung3a6e7f32013-10-09 15:50:52 -0700166
Michael Jurkaca993832012-06-29 15:17:04 -0700167 private final static Paint sPaint = new Paint();
Romain Guy8a0bff52012-05-06 13:14:33 -0700168
Adam Cohenc9735cf2015-01-23 16:11:55 -0800169 // Related to accessible drag and drop
Sunny Goyale9b651e2015-04-24 11:44:51 -0700170 private DragAndDropAccessibilityDelegate mTouchHelper;
Adam Cohenc9735cf2015-01-23 16:11:55 -0800171 private boolean mUseTouchHelper = false;
Adam Cohenc9735cf2015-01-23 16:11:55 -0800172
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800173 public CellLayout(Context context) {
174 this(context, null);
175 }
176
177 public CellLayout(Context context, AttributeSet attrs) {
178 this(context, attrs, 0);
179 }
180
181 public CellLayout(Context context, AttributeSet attrs, int defStyle) {
182 super(context, attrs, defStyle);
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700183
184 // A ViewGroup usually does not draw, but CellLayout needs to draw a rectangle to show
185 // the user where a dragged item will land when dropped.
186 setWillNotDraw(false);
Romain Guyce3cbd12013-02-25 15:00:36 -0800187 setClipToPadding(false);
Adam Cohen2acce882012-03-28 19:03:19 -0700188 mLauncher = (Launcher) context;
Michael Jurkaa63c4522010-08-19 13:52:27 -0700189
Adam Cohen2e6da152015-05-06 11:42:25 -0700190 DeviceProfile grid = mLauncher.getDeviceProfile();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800191 TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.CellLayout, defStyle, 0);
192
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
205 a.recycle();
206
207 setAlwaysDrawnWithCacheEnabled(false);
208
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700209 final Resources res = getResources();
Winson Chung6e1c0d32013-10-25 15:24:24 -0700210 mHotseatScale = (float) grid.hotseatIconSizePx / grid.iconSizePx;
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700211
Sunny Goyal2805e632015-05-20 15:35:32 -0700212 mBackground = (TransitionDrawable) res.getDrawable(R.drawable.bg_screenpanel);
213 mBackground.setCallback(this);
Winson Chunge8f1d042015-07-31 12:39:57 -0700214 mBackground.setAlpha((int) (mBackgroundAlpha * 255));
Michael Jurka33945b22010-12-21 18:19:38 -0800215
Adam Cohenfa3c58f2013-12-06 16:10:55 -0800216 mReorderPreviewAnimationMagnitude = (REORDER_PREVIEW_MAGNITUDE *
Winson Chung5f8afe62013-08-12 16:19:28 -0700217 grid.iconSizePx);
Adam Cohen19f37922012-03-21 11:59:11 -0700218
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700219 // Initialize the data structures used for the drag visualization.
Patrick Dubroyce34a972010-10-19 10:34:32 -0700220 mEaseOutInterpolator = new DecelerateInterpolator(2.5f); // Quint ease out
Winson Chungb8c69f32011-10-19 21:36:08 -0700221 mDragCell[0] = mDragCell[1] = -1;
Joe Onorato4be866d2010-10-10 11:26:02 -0700222 for (int i = 0; i < mDragOutlines.length; i++) {
Adam Cohend41fbf52012-02-16 23:53:59 -0800223 mDragOutlines[i] = new Rect(-1, -1, -1, -1);
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700224 }
225
226 // When dragging things around the home screens, we show a green outline of
227 // where the item will land. The outlines gradually fade out, leaving a trail
228 // behind the drag path.
229 // Set up all the animations that are used to implement this fading.
230 final int duration = res.getInteger(R.integer.config_dragOutlineFadeTime);
Chet Haase472b2812010-10-14 07:02:04 -0700231 final float fromAlphaValue = 0;
232 final float toAlphaValue = (float)res.getInteger(R.integer.config_dragOutlineMaxAlpha);
Joe Onorato4be866d2010-10-10 11:26:02 -0700233
Patrick Dubroy8e58e912010-10-14 13:21:48 -0700234 Arrays.fill(mDragOutlineAlphas, fromAlphaValue);
Joe Onorato4be866d2010-10-10 11:26:02 -0700235
236 for (int i = 0; i < mDragOutlineAnims.length; i++) {
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700237 final InterruptibleInOutAnimator anim =
Michael Jurkaf1ad6082013-03-13 12:55:46 +0100238 new InterruptibleInOutAnimator(this, duration, fromAlphaValue, toAlphaValue);
Patrick Dubroyce34a972010-10-19 10:34:32 -0700239 anim.getAnimator().setInterpolator(mEaseOutInterpolator);
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700240 final int thisIndex = i;
Chet Haase472b2812010-10-14 07:02:04 -0700241 anim.getAnimator().addUpdateListener(new AnimatorUpdateListener() {
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700242 public void onAnimationUpdate(ValueAnimator animation) {
Joe Onorato4be866d2010-10-10 11:26:02 -0700243 final Bitmap outline = (Bitmap)anim.getTag();
244
245 // If an animation is started and then stopped very quickly, we can still
246 // get spurious updates we've cleared the tag. Guard against this.
247 if (outline == null) {
Michael Jurka3a9fced2012-04-13 14:44:29 -0700248 @SuppressWarnings("all") // suppress dead code warning
249 final boolean debug = false;
250 if (debug) {
Patrick Dubroyfe6bd872010-10-13 17:32:10 -0700251 Object val = animation.getAnimatedValue();
252 Log.d(TAG, "anim " + thisIndex + " update: " + val +
253 ", isStopped " + anim.isStopped());
254 }
Joe Onorato4be866d2010-10-10 11:26:02 -0700255 // Try to prevent it from continuing to run
256 animation.cancel();
257 } else {
Chet Haase472b2812010-10-14 07:02:04 -0700258 mDragOutlineAlphas[thisIndex] = (Float) animation.getAnimatedValue();
Adam Cohend41fbf52012-02-16 23:53:59 -0800259 CellLayout.this.invalidate(mDragOutlines[thisIndex]);
Joe Onorato4be866d2010-10-10 11:26:02 -0700260 }
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700261 }
262 });
Joe Onorato4be866d2010-10-10 11:26:02 -0700263 // The animation holds a reference to the drag outline bitmap as long is it's
264 // running. This way the bitmap can be GCed when the animations are complete.
Chet Haase472b2812010-10-14 07:02:04 -0700265 anim.getAnimator().addListener(new AnimatorListenerAdapter() {
Michael Jurka3c4c20f2010-10-28 15:36:06 -0700266 @Override
Joe Onorato4be866d2010-10-10 11:26:02 -0700267 public void onAnimationEnd(Animator animation) {
Chet Haase472b2812010-10-14 07:02:04 -0700268 if ((Float) ((ValueAnimator) animation).getAnimatedValue() == 0f) {
Joe Onorato4be866d2010-10-10 11:26:02 -0700269 anim.setTag(null);
270 }
271 }
272 });
273 mDragOutlineAnims[i] = anim;
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700274 }
Patrick Dubroyce34a972010-10-19 10:34:32 -0700275
Michael Jurkaa52570f2012-03-20 03:18:20 -0700276 mShortcutsAndWidgets = new ShortcutAndWidgetContainer(context);
Adam Cohen2374abf2013-04-16 14:56:57 -0700277 mShortcutsAndWidgets.setCellDimensions(mCellWidth, mCellHeight, mWidthGap, mHeightGap,
Winson Chung5f8afe62013-08-12 16:19:28 -0700278 mCountX, mCountY);
Adam Cohen2374abf2013-04-16 14:56:57 -0700279
Mady Melloref044dd2015-06-02 15:35:07 -0700280 mStylusEventHelper = new StylusEventHelper(this);
281
Sunny Goyal4fe5a372015-05-14 19:55:10 -0700282 mTouchFeedbackView = new ClickShadowView(context);
283 addView(mTouchFeedbackView);
Michael Jurkaa52570f2012-03-20 03:18:20 -0700284 addView(mShortcutsAndWidgets);
Michael Jurka18014792010-10-14 09:01:34 -0700285 }
286
Adam Cohenc9735cf2015-01-23 16:11:55 -0800287 @TargetApi(Build.VERSION_CODES.LOLLIPOP)
Sunny Goyale9b651e2015-04-24 11:44:51 -0700288 public void enableAccessibleDrag(boolean enable, int dragType) {
Adam Cohenc9735cf2015-01-23 16:11:55 -0800289 mUseTouchHelper = enable;
290 if (!enable) {
291 ViewCompat.setAccessibilityDelegate(this, null);
292 setImportantForAccessibility(IMPORTANT_FOR_ACCESSIBILITY_NO);
293 getShortcutsAndWidgets().setImportantForAccessibility(IMPORTANT_FOR_ACCESSIBILITY_NO);
294 setOnClickListener(mLauncher);
295 } else {
Sunny Goyale9b651e2015-04-24 11:44:51 -0700296 if (dragType == WORKSPACE_ACCESSIBILITY_DRAG &&
297 !(mTouchHelper instanceof WorkspaceAccessibilityHelper)) {
298 mTouchHelper = new WorkspaceAccessibilityHelper(this);
299 } else if (dragType == FOLDER_ACCESSIBILITY_DRAG &&
300 !(mTouchHelper instanceof FolderAccessibilityHelper)) {
301 mTouchHelper = new FolderAccessibilityHelper(this);
302 }
Adam Cohenc9735cf2015-01-23 16:11:55 -0800303 ViewCompat.setAccessibilityDelegate(this, mTouchHelper);
304 setImportantForAccessibility(IMPORTANT_FOR_ACCESSIBILITY_YES);
305 getShortcutsAndWidgets().setImportantForAccessibility(IMPORTANT_FOR_ACCESSIBILITY_YES);
306 setOnClickListener(mTouchHelper);
307 }
308
309 // Invalidate the accessibility hierarchy
310 if (getParent() != null) {
311 getParent().notifySubtreeAccessibilityStateChanged(
312 this, this, AccessibilityEvent.CONTENT_CHANGE_TYPE_SUBTREE);
313 }
314 }
315
316 @Override
317 public boolean dispatchHoverEvent(MotionEvent event) {
318 // Always attempt to dispatch hover events to accessibility first.
319 if (mUseTouchHelper && mTouchHelper.dispatchHoverEvent(event)) {
320 return true;
321 }
322 return super.dispatchHoverEvent(event);
323 }
324
325 @Override
Adam Cohenc9735cf2015-01-23 16:11:55 -0800326 public boolean onInterceptTouchEvent(MotionEvent ev) {
327 if (mUseTouchHelper ||
328 (mInterceptTouchListener != null && mInterceptTouchListener.onTouch(this, ev))) {
329 return true;
330 }
331 return false;
332 }
333
Mady Melloref044dd2015-06-02 15:35:07 -0700334 @Override
335 public boolean onTouchEvent(MotionEvent ev) {
336 boolean handled = super.onTouchEvent(ev);
337 // Stylus button press on a home screen should not switch between overview mode and
338 // the home screen mode, however, once in overview mode stylus button press should be
339 // enabled to allow rearranging the different home screens. So check what mode
340 // the workspace is in, and only perform stylus button presses while in overview mode.
341 if (mLauncher.mWorkspace.isInOverviewMode()
342 && mStylusEventHelper.checkAndPerformStylusEvent(ev)) {
343 return true;
344 }
345 return handled;
346 }
347
Chris Craik01f2d7f2013-10-01 14:41:56 -0700348 public void enableHardwareLayer(boolean hasLayer) {
349 mShortcutsAndWidgets.setLayerType(hasLayer ? LAYER_TYPE_HARDWARE : LAYER_TYPE_NONE, sPaint);
Michael Jurkad51f33a2012-06-28 15:35:26 -0700350 }
351
352 public void buildHardwareLayer() {
353 mShortcutsAndWidgets.buildLayer();
Adam Cohen2801caf2011-05-13 20:57:39 -0700354 }
355
Adam Cohen307fe232012-08-16 17:55:58 -0700356 public float getChildrenScale() {
357 return mIsHotseat ? mHotseatScale : 1.0f;
358 }
359
Winson Chung5f8afe62013-08-12 16:19:28 -0700360 public void setCellDimensions(int width, int height) {
Winson Chung11a1a532013-09-13 11:14:45 -0700361 mFixedCellWidth = mCellWidth = width;
362 mFixedCellHeight = mCellHeight = height;
Winson Chung5f8afe62013-08-12 16:19:28 -0700363 mShortcutsAndWidgets.setCellDimensions(mCellWidth, mCellHeight, mWidthGap, mHeightGap,
364 mCountX, mCountY);
365 }
366
Adam Cohen2801caf2011-05-13 20:57:39 -0700367 public void setGridSize(int x, int y) {
368 mCountX = x;
369 mCountY = y;
370 mOccupied = new boolean[mCountX][mCountY];
Adam Cohen482ed822012-03-02 14:15:13 -0800371 mTmpOccupied = new boolean[mCountX][mCountY];
Adam Cohen7fbec102012-03-27 12:42:19 -0700372 mTempRectStack.clear();
Adam Cohen2374abf2013-04-16 14:56:57 -0700373 mShortcutsAndWidgets.setCellDimensions(mCellWidth, mCellHeight, mWidthGap, mHeightGap,
Winson Chung5f8afe62013-08-12 16:19:28 -0700374 mCountX, mCountY);
Adam Cohen76fc0852011-06-17 13:26:23 -0700375 requestLayout();
Adam Cohen2801caf2011-05-13 20:57:39 -0700376 }
377
Adam Cohen2374abf2013-04-16 14:56:57 -0700378 // Set whether or not to invert the layout horizontally if the layout is in RTL mode.
379 public void setInvertIfRtl(boolean invert) {
380 mShortcutsAndWidgets.setInvertIfRtl(invert);
381 }
382
Adam Cohen917e3882013-10-31 15:03:35 -0700383 public void setDropPending(boolean pending) {
384 mDropPending = pending;
385 }
386
387 public boolean isDropPending() {
388 return mDropPending;
389 }
390
Sunny Goyal4b6eb262015-05-14 19:24:40 -0700391 @Override
392 public void setPressedIcon(BubbleTextView icon, Bitmap background) {
Sunny Goyal508da152014-08-14 10:53:27 -0700393 if (icon == null || background == null) {
394 mTouchFeedbackView.setBitmap(null);
395 mTouchFeedbackView.animate().cancel();
396 } else {
Sunny Goyal508da152014-08-14 10:53:27 -0700397 if (mTouchFeedbackView.setBitmap(background)) {
Sunny Goyal4fe5a372015-05-14 19:55:10 -0700398 mTouchFeedbackView.alignWithIconView(icon, mShortcutsAndWidgets);
399 mTouchFeedbackView.animateShadow();
Sunny Goyal508da152014-08-14 10:53:27 -0700400 }
Patrick Dubroy96864c32011-03-10 17:17:23 -0800401 }
402 }
403
Adam Cohenc50438c2014-08-19 17:43:05 -0700404 void disableDragTarget() {
405 mIsDragTarget = false;
406 }
407
Vadim Tryshevfedca432015-08-19 17:55:02 -0700408 public boolean isDragTarget() {
Adam Cohenc50438c2014-08-19 17:43:05 -0700409 return mIsDragTarget;
410 }
411
412 void setIsDragOverlapping(boolean isDragOverlapping) {
413 if (mIsDragOverlapping != isDragOverlapping) {
414 mIsDragOverlapping = isDragOverlapping;
Sunny Goyal2805e632015-05-20 15:35:32 -0700415 if (mIsDragOverlapping) {
416 mBackground.startTransition(BACKGROUND_ACTIVATE_DURATION);
417 } else {
Winson Chunge8f1d042015-07-31 12:39:57 -0700418 if (mBackgroundAlpha > 0f) {
419 mBackground.reverseTransition(BACKGROUND_ACTIVATE_DURATION);
420 } else {
421 mBackground.resetTransition();
422 }
Sunny Goyal2805e632015-05-20 15:35:32 -0700423 }
Adam Cohenc50438c2014-08-19 17:43:05 -0700424 invalidate();
425 }
426 }
427
Vadim Tryshevfedca432015-08-19 17:55:02 -0700428 public boolean getIsDragOverlapping() {
Michael Jurka33945b22010-12-21 18:19:38 -0800429 return mIsDragOverlapping;
430 }
431
Jeff Sharkey83f111d2009-04-20 21:03:13 -0700432 @Override
Patrick Dubroy1262e362010-10-06 15:49:50 -0700433 protected void onDraw(Canvas canvas) {
Sunny Goyal05739772015-05-19 19:59:09 -0700434 if (!mIsDragTarget) {
435 return;
436 }
437
Michael Jurka3e7c7632010-10-02 16:01:03 -0700438 // When we're large, we are either drawn in a "hover" state (ie when dragging an item to
439 // a neighboring page) or with just a normal background (if backgroundAlpha > 0.0f)
440 // When we're small, we are either drawn normally or in the "accepts drops" state (during
441 // a drag). However, we also drag the mini hover background *over* one of those two
442 // backgrounds
Sunny Goyal05739772015-05-19 19:59:09 -0700443 if (mBackgroundAlpha > 0.0f) {
Sunny Goyal2805e632015-05-20 15:35:32 -0700444 mBackground.draw(canvas);
Michael Jurkaa63c4522010-08-19 13:52:27 -0700445 }
Romain Guya6abce82009-11-10 02:54:41 -0800446
Patrick Dubroy8e58e912010-10-14 13:21:48 -0700447 final Paint paint = mDragOutlinePaint;
Joe Onorato4be866d2010-10-10 11:26:02 -0700448 for (int i = 0; i < mDragOutlines.length; i++) {
Chet Haase472b2812010-10-14 07:02:04 -0700449 final float alpha = mDragOutlineAlphas[i];
Joe Onorato4be866d2010-10-10 11:26:02 -0700450 if (alpha > 0) {
Joe Onorato4be866d2010-10-10 11:26:02 -0700451 final Bitmap b = (Bitmap) mDragOutlineAnims[i].getTag();
Chet Haase472b2812010-10-14 07:02:04 -0700452 paint.setAlpha((int)(alpha + .5f));
Sunny Goyal106bf642015-07-16 12:18:06 -0700453 canvas.drawBitmap(b, null, mDragOutlines[i], paint);
Winson Chung150fbab2010-09-29 17:14:26 -0700454 }
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700455 }
Patrick Dubroy96864c32011-03-10 17:17:23 -0800456
Adam Cohen482ed822012-03-02 14:15:13 -0800457 if (DEBUG_VISUALIZE_OCCUPIED) {
458 int[] pt = new int[2];
459 ColorDrawable cd = new ColorDrawable(Color.RED);
Adam Cohene7587d22012-05-24 18:50:02 -0700460 cd.setBounds(0, 0, mCellWidth, mCellHeight);
Adam Cohen482ed822012-03-02 14:15:13 -0800461 for (int i = 0; i < mCountX; i++) {
462 for (int j = 0; j < mCountY; j++) {
463 if (mOccupied[i][j]) {
464 cellToPoint(i, j, pt);
465 canvas.save();
466 canvas.translate(pt[0], pt[1]);
467 cd.draw(canvas);
468 canvas.restore();
469 }
470 }
471 }
472 }
473
Andrew Flynn850d2e72012-04-26 16:51:20 -0700474 int previewOffset = FolderRingAnimator.sPreviewSize;
475
Adam Cohen69ce2e52011-07-03 19:25:21 -0700476 // The folder outer / inner ring image(s)
Adam Cohen2e6da152015-05-06 11:42:25 -0700477 DeviceProfile grid = mLauncher.getDeviceProfile();
Adam Cohen69ce2e52011-07-03 19:25:21 -0700478 for (int i = 0; i < mFolderOuterRings.size(); i++) {
479 FolderRingAnimator fra = mFolderOuterRings.get(i);
480
Adam Cohen5108bc02013-09-20 17:04:51 -0700481 Drawable d;
482 int width, height;
Adam Cohen69ce2e52011-07-03 19:25:21 -0700483 cellToPoint(fra.mCellX, fra.mCellY, mTempLocation);
Winson Chung5f8afe62013-08-12 16:19:28 -0700484 View child = getChildAt(fra.mCellX, fra.mCellY);
Adam Cohen558f1c22013-10-09 15:15:24 -0700485
Winson Chung89f97052013-09-20 11:32:26 -0700486 if (child != null) {
Adam Cohen558f1c22013-10-09 15:15:24 -0700487 int centerX = mTempLocation[0] + mCellWidth / 2;
488 int centerY = mTempLocation[1] + previewOffset / 2 +
489 child.getPaddingTop() + grid.folderBackgroundOffset;
490
Adam Cohen5108bc02013-09-20 17:04:51 -0700491 // Draw outer ring, if it exists
492 if (FolderIcon.HAS_OUTER_RING) {
493 d = FolderRingAnimator.sSharedOuterRingDrawable;
494 width = (int) (fra.getOuterRingSize() * getChildrenScale());
495 height = width;
496 canvas.save();
497 canvas.translate(centerX - width / 2, centerY - height / 2);
498 d.setBounds(0, 0, width, height);
499 d.draw(canvas);
500 canvas.restore();
501 }
Adam Cohen69ce2e52011-07-03 19:25:21 -0700502
Winson Chung89f97052013-09-20 11:32:26 -0700503 // Draw inner ring
504 d = FolderRingAnimator.sSharedInnerRingDrawable;
505 width = (int) (fra.getInnerRingSize() * getChildrenScale());
506 height = width;
Winson Chung89f97052013-09-20 11:32:26 -0700507 canvas.save();
508 canvas.translate(centerX - width / 2, centerY - width / 2);
509 d.setBounds(0, 0, width, height);
510 d.draw(canvas);
511 canvas.restore();
512 }
Adam Cohen69ce2e52011-07-03 19:25:21 -0700513 }
Adam Cohenc51934b2011-07-26 21:07:43 -0700514
515 if (mFolderLeaveBehindCell[0] >= 0 && mFolderLeaveBehindCell[1] >= 0) {
516 Drawable d = FolderIcon.sSharedFolderLeaveBehind;
517 int width = d.getIntrinsicWidth();
518 int height = d.getIntrinsicHeight();
519
520 cellToPoint(mFolderLeaveBehindCell[0], mFolderLeaveBehindCell[1], mTempLocation);
Winson Chung5f8afe62013-08-12 16:19:28 -0700521 View child = getChildAt(mFolderLeaveBehindCell[0], mFolderLeaveBehindCell[1]);
Winson Chung89f97052013-09-20 11:32:26 -0700522 if (child != null) {
523 int centerX = mTempLocation[0] + mCellWidth / 2;
524 int centerY = mTempLocation[1] + previewOffset / 2 +
525 child.getPaddingTop() + grid.folderBackgroundOffset;
Adam Cohenc51934b2011-07-26 21:07:43 -0700526
Winson Chung89f97052013-09-20 11:32:26 -0700527 canvas.save();
528 canvas.translate(centerX - width / 2, centerY - width / 2);
529 d.setBounds(0, 0, width, height);
530 d.draw(canvas);
531 canvas.restore();
532 }
Adam Cohenc51934b2011-07-26 21:07:43 -0700533 }
Adam Cohen69ce2e52011-07-03 19:25:21 -0700534 }
535
536 public void showFolderAccept(FolderRingAnimator fra) {
537 mFolderOuterRings.add(fra);
538 }
539
540 public void hideFolderAccept(FolderRingAnimator fra) {
541 if (mFolderOuterRings.contains(fra)) {
542 mFolderOuterRings.remove(fra);
543 }
544 invalidate();
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700545 }
546
Adam Cohenc51934b2011-07-26 21:07:43 -0700547 public void setFolderLeaveBehindCell(int x, int y) {
548 mFolderLeaveBehindCell[0] = x;
549 mFolderLeaveBehindCell[1] = y;
550 invalidate();
551 }
552
553 public void clearFolderLeaveBehind() {
554 mFolderLeaveBehindCell[0] = -1;
555 mFolderLeaveBehindCell[1] = -1;
556 invalidate();
557 }
558
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700559 @Override
Michael Jurkae6235dd2011-10-04 15:02:05 -0700560 public boolean shouldDelayChildPressedState() {
561 return false;
562 }
563
Adam Cohen1462de32012-07-24 22:34:36 -0700564 public void restoreInstanceState(SparseArray<Parcelable> states) {
Sunny Goyal33a152f2014-07-22 12:13:14 -0700565 try {
566 dispatchRestoreInstanceState(states);
567 } catch (IllegalArgumentException ex) {
Sunny Goyal6c56c682015-07-16 14:09:05 -0700568 if (ProviderConfig.IS_DOGFOOD_BUILD) {
Sunny Goyal33a152f2014-07-22 12:13:14 -0700569 throw ex;
570 }
571 // Mismatched viewId / viewType preventing restore. Skip restore on production builds.
572 Log.e(TAG, "Ignoring an error while restoring a view instance state", ex);
573 }
Adam Cohen1462de32012-07-24 22:34:36 -0700574 }
575
Michael Jurkae6235dd2011-10-04 15:02:05 -0700576 @Override
Jeff Sharkey83f111d2009-04-20 21:03:13 -0700577 public void cancelLongPress() {
578 super.cancelLongPress();
579
580 // Cancel long press for all children
581 final int count = getChildCount();
582 for (int i = 0; i < count; i++) {
583 final View child = getChildAt(i);
584 child.cancelLongPress();
585 }
586 }
587
Michael Jurkadee05892010-07-27 10:01:56 -0700588 public void setOnInterceptTouchListener(View.OnTouchListener listener) {
589 mInterceptTouchListener = listener;
590 }
591
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800592 public int getCountX() {
Adam Cohend22015c2010-07-26 22:02:18 -0700593 return mCountX;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800594 }
595
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800596 public int getCountY() {
Adam Cohend22015c2010-07-26 22:02:18 -0700597 return mCountY;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800598 }
599
Andrew Flynn0dca1ec2012-02-29 13:33:22 -0800600 public void setIsHotseat(boolean isHotseat) {
601 mIsHotseat = isHotseat;
Winson Chung5f8afe62013-08-12 16:19:28 -0700602 mShortcutsAndWidgets.setIsHotseat(isHotseat);
Andrew Flynn0dca1ec2012-02-29 13:33:22 -0800603 }
604
Sunny Goyale9b651e2015-04-24 11:44:51 -0700605 public boolean isHotseat() {
606 return mIsHotseat;
607 }
608
Andrew Flynn0dca1ec2012-02-29 13:33:22 -0800609 public boolean addViewToCellLayout(View child, int index, int childId, LayoutParams params,
Sunny Goyalf7a29e82015-04-24 15:20:43 -0700610 boolean markCells) {
Winson Chungaafa03c2010-06-11 17:34:16 -0700611 final LayoutParams lp = params;
612
Andrew Flynnde38e422012-05-08 11:22:15 -0700613 // Hotseat icons - remove text
Andrew Flynn0dca1ec2012-02-29 13:33:22 -0800614 if (child instanceof BubbleTextView) {
615 BubbleTextView bubbleChild = (BubbleTextView) child;
Winson Chung5f8afe62013-08-12 16:19:28 -0700616 bubbleChild.setTextVisibility(!mIsHotseat);
Andrew Flynn0dca1ec2012-02-29 13:33:22 -0800617 }
618
Adam Cohen307fe232012-08-16 17:55:58 -0700619 child.setScaleX(getChildrenScale());
620 child.setScaleY(getChildrenScale());
621
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800622 // Generate an id for each view, this assumes we have at most 256x256 cells
623 // per workspace screen
Adam Cohend22015c2010-07-26 22:02:18 -0700624 if (lp.cellX >= 0 && lp.cellX <= mCountX - 1 && lp.cellY >= 0 && lp.cellY <= mCountY - 1) {
Winson Chungaafa03c2010-06-11 17:34:16 -0700625 // If the horizontal or vertical span is set to -1, it is taken to
626 // mean that it spans the extent of the CellLayout
Adam Cohend22015c2010-07-26 22:02:18 -0700627 if (lp.cellHSpan < 0) lp.cellHSpan = mCountX;
628 if (lp.cellVSpan < 0) lp.cellVSpan = mCountY;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800629
Winson Chungaafa03c2010-06-11 17:34:16 -0700630 child.setId(childId);
Sunny Goyalf7a29e82015-04-24 15:20:43 -0700631 mShortcutsAndWidgets.addView(child, index, lp);
Michael Jurkadee05892010-07-27 10:01:56 -0700632
Michael Jurkaf3ca3ab2010-10-20 17:08:24 -0700633 if (markCells) markCellsAsOccupiedForView(child);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700634
Winson Chungaafa03c2010-06-11 17:34:16 -0700635 return true;
636 }
637 return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800638 }
Michael Jurka3e7c7632010-10-02 16:01:03 -0700639
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800640 @Override
Michael Jurka0280c3b2010-09-17 15:00:07 -0700641 public void removeAllViews() {
642 clearOccupiedCells();
Michael Jurkaa52570f2012-03-20 03:18:20 -0700643 mShortcutsAndWidgets.removeAllViews();
Michael Jurka0280c3b2010-09-17 15:00:07 -0700644 }
645
646 @Override
647 public void removeAllViewsInLayout() {
Michael Jurkaa52570f2012-03-20 03:18:20 -0700648 if (mShortcutsAndWidgets.getChildCount() > 0) {
Michael Jurka7cfc2822011-08-02 20:19:24 -0700649 clearOccupiedCells();
Michael Jurkaa52570f2012-03-20 03:18:20 -0700650 mShortcutsAndWidgets.removeAllViewsInLayout();
Michael Jurka7cfc2822011-08-02 20:19:24 -0700651 }
Michael Jurka0280c3b2010-09-17 15:00:07 -0700652 }
653
654 @Override
655 public void removeView(View view) {
656 markCellsAsUnoccupiedForView(view);
Michael Jurkaa52570f2012-03-20 03:18:20 -0700657 mShortcutsAndWidgets.removeView(view);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700658 }
659
660 @Override
661 public void removeViewAt(int index) {
Michael Jurkaa52570f2012-03-20 03:18:20 -0700662 markCellsAsUnoccupiedForView(mShortcutsAndWidgets.getChildAt(index));
663 mShortcutsAndWidgets.removeViewAt(index);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700664 }
665
666 @Override
667 public void removeViewInLayout(View view) {
668 markCellsAsUnoccupiedForView(view);
Michael Jurkaa52570f2012-03-20 03:18:20 -0700669 mShortcutsAndWidgets.removeViewInLayout(view);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700670 }
671
672 @Override
673 public void removeViews(int start, int count) {
674 for (int i = start; i < start + count; i++) {
Michael Jurkaa52570f2012-03-20 03:18:20 -0700675 markCellsAsUnoccupiedForView(mShortcutsAndWidgets.getChildAt(i));
Michael Jurka0280c3b2010-09-17 15:00:07 -0700676 }
Michael Jurkaa52570f2012-03-20 03:18:20 -0700677 mShortcutsAndWidgets.removeViews(start, count);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700678 }
679
680 @Override
681 public void removeViewsInLayout(int start, int count) {
682 for (int i = start; i < start + count; i++) {
Michael Jurkaa52570f2012-03-20 03:18:20 -0700683 markCellsAsUnoccupiedForView(mShortcutsAndWidgets.getChildAt(i));
Michael Jurka0280c3b2010-09-17 15:00:07 -0700684 }
Michael Jurkaa52570f2012-03-20 03:18:20 -0700685 mShortcutsAndWidgets.removeViewsInLayout(start, count);
Michael Jurkaabded662011-03-04 12:06:57 -0800686 }
687
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700688 /**
Winson Chungaafa03c2010-06-11 17:34:16 -0700689 * Given a point, return the cell that strictly encloses that point
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800690 * @param x X coordinate of the point
691 * @param y Y coordinate of the point
692 * @param result Array of 2 ints to hold the x and y coordinate of the cell
693 */
Sunny Goyale9b651e2015-04-24 11:44:51 -0700694 public void pointToCellExact(int x, int y, int[] result) {
Winson Chung4b825dcd2011-06-19 12:41:22 -0700695 final int hStartPadding = getPaddingLeft();
696 final int vStartPadding = getPaddingTop();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800697
698 result[0] = (x - hStartPadding) / (mCellWidth + mWidthGap);
699 result[1] = (y - vStartPadding) / (mCellHeight + mHeightGap);
700
Adam Cohend22015c2010-07-26 22:02:18 -0700701 final int xAxis = mCountX;
702 final int yAxis = mCountY;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800703
704 if (result[0] < 0) result[0] = 0;
705 if (result[0] >= xAxis) result[0] = xAxis - 1;
706 if (result[1] < 0) result[1] = 0;
707 if (result[1] >= yAxis) result[1] = yAxis - 1;
708 }
Winson Chungaafa03c2010-06-11 17:34:16 -0700709
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800710 /**
711 * Given a point, return the cell that most closely encloses that point
712 * @param x X coordinate of the point
713 * @param y Y coordinate of the point
714 * @param result Array of 2 ints to hold the x and y coordinate of the cell
715 */
716 void pointToCellRounded(int x, int y, int[] result) {
717 pointToCellExact(x + (mCellWidth / 2), y + (mCellHeight / 2), result);
718 }
719
720 /**
721 * Given a cell coordinate, return the point that represents the upper left corner of that cell
Winson Chungaafa03c2010-06-11 17:34:16 -0700722 *
723 * @param cellX X coordinate of the cell
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800724 * @param cellY Y coordinate of the cell
Winson Chungaafa03c2010-06-11 17:34:16 -0700725 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800726 * @param result Array of 2 ints to hold the x and y coordinate of the point
727 */
728 void cellToPoint(int cellX, int cellY, int[] result) {
Winson Chung4b825dcd2011-06-19 12:41:22 -0700729 final int hStartPadding = getPaddingLeft();
730 final int vStartPadding = getPaddingTop();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800731
732 result[0] = hStartPadding + cellX * (mCellWidth + mWidthGap);
733 result[1] = vStartPadding + cellY * (mCellHeight + mHeightGap);
734 }
735
Adam Cohene3e27a82011-04-15 12:07:39 -0700736 /**
Adam Cohen482ed822012-03-02 14:15:13 -0800737 * Given a cell coordinate, return the point that represents the center of the cell
Adam Cohene3e27a82011-04-15 12:07:39 -0700738 *
739 * @param cellX X coordinate of the cell
740 * @param cellY Y coordinate of the cell
741 *
742 * @param result Array of 2 ints to hold the x and y coordinate of the point
743 */
744 void cellToCenterPoint(int cellX, int cellY, int[] result) {
Adam Cohen47a876d2012-03-19 13:21:41 -0700745 regionToCenterPoint(cellX, cellY, 1, 1, result);
746 }
747
748 /**
749 * Given a cell coordinate and span return the point that represents the center of the regio
750 *
751 * @param cellX X coordinate of the cell
752 * @param cellY Y coordinate of the cell
753 *
754 * @param result Array of 2 ints to hold the x and y coordinate of the point
755 */
756 void regionToCenterPoint(int cellX, int cellY, int spanX, int spanY, int[] result) {
Winson Chung4b825dcd2011-06-19 12:41:22 -0700757 final int hStartPadding = getPaddingLeft();
758 final int vStartPadding = getPaddingTop();
Adam Cohen47a876d2012-03-19 13:21:41 -0700759 result[0] = hStartPadding + cellX * (mCellWidth + mWidthGap) +
760 (spanX * mCellWidth + (spanX - 1) * mWidthGap) / 2;
761 result[1] = vStartPadding + cellY * (mCellHeight + mHeightGap) +
762 (spanY * mCellHeight + (spanY - 1) * mHeightGap) / 2;
Adam Cohene3e27a82011-04-15 12:07:39 -0700763 }
764
Adam Cohen19f37922012-03-21 11:59:11 -0700765 /**
766 * Given a cell coordinate and span fills out a corresponding pixel rect
767 *
768 * @param cellX X coordinate of the cell
769 * @param cellY Y coordinate of the cell
770 * @param result Rect in which to write the result
771 */
772 void regionToRect(int cellX, int cellY, int spanX, int spanY, Rect result) {
773 final int hStartPadding = getPaddingLeft();
774 final int vStartPadding = getPaddingTop();
775 final int left = hStartPadding + cellX * (mCellWidth + mWidthGap);
776 final int top = vStartPadding + cellY * (mCellHeight + mHeightGap);
777 result.set(left, top, left + (spanX * mCellWidth + (spanX - 1) * mWidthGap),
778 top + (spanY * mCellHeight + (spanY - 1) * mHeightGap));
779 }
780
Adam Cohen482ed822012-03-02 14:15:13 -0800781 public float getDistanceFromCell(float x, float y, int[] cell) {
782 cellToCenterPoint(cell[0], cell[1], mTmpPoint);
Sunny Goyalf7a29e82015-04-24 15:20:43 -0700783 return (float) Math.hypot(x - mTmpPoint[0], y - mTmpPoint[1]);
Adam Cohen482ed822012-03-02 14:15:13 -0800784 }
785
Romain Guy84f296c2009-11-04 15:00:44 -0800786 int getCellWidth() {
787 return mCellWidth;
788 }
789
790 int getCellHeight() {
791 return mCellHeight;
792 }
793
Adam Cohend4844c32011-02-18 19:25:06 -0800794 int getWidthGap() {
795 return mWidthGap;
796 }
797
798 int getHeightGap() {
799 return mHeightGap;
800 }
801
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700802 public void setFixedSize(int width, int height) {
803 mFixedWidth = width;
804 mFixedHeight = height;
805 }
806
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800807 @Override
808 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800809 int widthSpecMode = MeasureSpec.getMode(widthMeasureSpec);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800810 int heightSpecMode = MeasureSpec.getMode(heightMeasureSpec);
Winson Chung5f8afe62013-08-12 16:19:28 -0700811 int widthSize = MeasureSpec.getSize(widthMeasureSpec);
812 int heightSize = MeasureSpec.getSize(heightMeasureSpec);
Winson Chung2d75f122013-09-23 16:53:31 -0700813 int childWidthSize = widthSize - (getPaddingLeft() + getPaddingRight());
814 int childHeightSize = heightSize - (getPaddingTop() + getPaddingBottom());
Winson Chung11a1a532013-09-13 11:14:45 -0700815 if (mFixedCellWidth < 0 || mFixedCellHeight < 0) {
Sunny Goyalc6205602015-05-21 20:46:33 -0700816 int cw = DeviceProfile.calculateCellWidth(childWidthSize, mCountX);
817 int ch = DeviceProfile.calculateCellHeight(childHeightSize, mCountY);
Winson Chung11a1a532013-09-13 11:14:45 -0700818 if (cw != mCellWidth || ch != mCellHeight) {
819 mCellWidth = cw;
820 mCellHeight = ch;
821 mShortcutsAndWidgets.setCellDimensions(mCellWidth, mCellHeight, mWidthGap,
822 mHeightGap, mCountX, mCountY);
823 }
Winson Chung5f8afe62013-08-12 16:19:28 -0700824 }
Winson Chungaafa03c2010-06-11 17:34:16 -0700825
Winson Chung2d75f122013-09-23 16:53:31 -0700826 int newWidth = childWidthSize;
827 int newHeight = childHeightSize;
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700828 if (mFixedWidth > 0 && mFixedHeight > 0) {
829 newWidth = mFixedWidth;
830 newHeight = mFixedHeight;
831 } else if (widthSpecMode == MeasureSpec.UNSPECIFIED || heightSpecMode == MeasureSpec.UNSPECIFIED) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800832 throw new RuntimeException("CellLayout cannot have UNSPECIFIED dimensions");
833 }
834
Adam Cohend22015c2010-07-26 22:02:18 -0700835 int numWidthGaps = mCountX - 1;
836 int numHeightGaps = mCountY - 1;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800837
Adam Cohen234c4cd2011-07-17 21:03:04 -0700838 if (mOriginalWidthGap < 0 || mOriginalHeightGap < 0) {
Winson Chung2d75f122013-09-23 16:53:31 -0700839 int hSpace = childWidthSize;
840 int vSpace = childHeightSize;
Adam Cohenf4bd5792012-04-27 11:35:29 -0700841 int hFreeSpace = hSpace - (mCountX * mCellWidth);
842 int vFreeSpace = vSpace - (mCountY * mCellHeight);
Winson Chung4b825dcd2011-06-19 12:41:22 -0700843 mWidthGap = Math.min(mMaxGap, numWidthGaps > 0 ? (hFreeSpace / numWidthGaps) : 0);
844 mHeightGap = Math.min(mMaxGap,numHeightGaps > 0 ? (vFreeSpace / numHeightGaps) : 0);
Winson Chung5f8afe62013-08-12 16:19:28 -0700845 mShortcutsAndWidgets.setCellDimensions(mCellWidth, mCellHeight, mWidthGap,
846 mHeightGap, mCountX, mCountY);
Adam Cohen234c4cd2011-07-17 21:03:04 -0700847 } else {
848 mWidthGap = mOriginalWidthGap;
849 mHeightGap = mOriginalHeightGap;
Winson Chungece7f5b2010-10-22 14:54:12 -0700850 }
Sunny Goyal4fe5a372015-05-14 19:55:10 -0700851
852 // Make the feedback view large enough to hold the blur bitmap.
853 mTouchFeedbackView.measure(
854 MeasureSpec.makeMeasureSpec(mCellWidth + mTouchFeedbackView.getExtraSize(),
855 MeasureSpec.EXACTLY),
856 MeasureSpec.makeMeasureSpec(mCellHeight + mTouchFeedbackView.getExtraSize(),
857 MeasureSpec.EXACTLY));
858
859 mShortcutsAndWidgets.measure(
860 MeasureSpec.makeMeasureSpec(newWidth, MeasureSpec.EXACTLY),
861 MeasureSpec.makeMeasureSpec(newHeight, MeasureSpec.EXACTLY));
862
863 int maxWidth = mShortcutsAndWidgets.getMeasuredWidth();
864 int maxHeight = mShortcutsAndWidgets.getMeasuredHeight();
Winson Chung2d75f122013-09-23 16:53:31 -0700865 if (mFixedWidth > 0 && mFixedHeight > 0) {
866 setMeasuredDimension(maxWidth, maxHeight);
867 } else {
868 setMeasuredDimension(widthSize, heightSize);
869 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800870 }
871
872 @Override
Michael Jurka28750fb2010-09-24 17:43:49 -0700873 protected void onLayout(boolean changed, int l, int t, int r, int b) {
Winson Chung38848ca2013-10-08 12:03:44 -0700874 int offset = getMeasuredWidth() - getPaddingLeft() - getPaddingRight() -
875 (mCountX * mCellWidth);
876 int left = getPaddingLeft() + (int) Math.ceil(offset / 2f);
877 int top = getPaddingTop();
Sunny Goyal4fe5a372015-05-14 19:55:10 -0700878
879 mTouchFeedbackView.layout(left, top,
880 left + mTouchFeedbackView.getMeasuredWidth(),
881 top + mTouchFeedbackView.getMeasuredHeight());
882 mShortcutsAndWidgets.layout(left, top,
883 left + r - l,
884 top + b - t);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800885 }
886
887 @Override
Michael Jurkadee05892010-07-27 10:01:56 -0700888 protected void onSizeChanged(int w, int h, int oldw, int oldh) {
889 super.onSizeChanged(w, h, oldw, oldh);
Winson Chung82a9bd22013-10-08 16:02:34 -0700890
891 // Expand the background drawing bounds by the padding baked into the background drawable
Sunny Goyal2805e632015-05-20 15:35:32 -0700892 mBackground.getPadding(mTempRect);
893 mBackground.setBounds(-mTempRect.left, -mTempRect.top,
894 w + mTempRect.right, h + mTempRect.bottom);
Michael Jurkadee05892010-07-27 10:01:56 -0700895 }
896
897 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800898 protected void setChildrenDrawingCacheEnabled(boolean enabled) {
Michael Jurkaa52570f2012-03-20 03:18:20 -0700899 mShortcutsAndWidgets.setChildrenDrawingCacheEnabled(enabled);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800900 }
901
902 @Override
903 protected void setChildrenDrawnWithCacheEnabled(boolean enabled) {
Michael Jurkaa52570f2012-03-20 03:18:20 -0700904 mShortcutsAndWidgets.setChildrenDrawnWithCacheEnabled(enabled);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800905 }
906
Michael Jurka5f1c5092010-09-03 14:15:02 -0700907 public float getBackgroundAlpha() {
908 return mBackgroundAlpha;
Michael Jurkadee05892010-07-27 10:01:56 -0700909 }
910
Michael Jurka5f1c5092010-09-03 14:15:02 -0700911 public void setBackgroundAlpha(float alpha) {
Michael Jurkaafaa0502011-12-13 18:22:50 -0800912 if (mBackgroundAlpha != alpha) {
913 mBackgroundAlpha = alpha;
Sunny Goyal2805e632015-05-20 15:35:32 -0700914 mBackground.setAlpha((int) (mBackgroundAlpha * 255));
Michael Jurkaafaa0502011-12-13 18:22:50 -0800915 }
Michael Jurkadee05892010-07-27 10:01:56 -0700916 }
917
Sunny Goyal2805e632015-05-20 15:35:32 -0700918 @Override
919 protected boolean verifyDrawable(Drawable who) {
920 return super.verifyDrawable(who) || (mIsDragTarget && who == mBackground);
921 }
922
Michael Jurkaa52570f2012-03-20 03:18:20 -0700923 public void setShortcutAndWidgetAlpha(float alpha) {
Sunny Goyal02b50812014-09-10 15:44:42 -0700924 mShortcutsAndWidgets.setAlpha(alpha);
Michael Jurkadee05892010-07-27 10:01:56 -0700925 }
926
Michael Jurkaa52570f2012-03-20 03:18:20 -0700927 public ShortcutAndWidgetContainer getShortcutsAndWidgets() {
Sunny Goyaldcbcc862014-08-12 15:58:36 -0700928 return mShortcutsAndWidgets;
Michael Jurkaa52570f2012-03-20 03:18:20 -0700929 }
930
Patrick Dubroy440c3602010-07-13 17:50:32 -0700931 public View getChildAt(int x, int y) {
Michael Jurkaa52570f2012-03-20 03:18:20 -0700932 return mShortcutsAndWidgets.getChildAt(x, y);
Patrick Dubroy440c3602010-07-13 17:50:32 -0700933 }
934
Adam Cohen76fc0852011-06-17 13:26:23 -0700935 public boolean animateChildToPosition(final View child, int cellX, int cellY, int duration,
Adam Cohen482ed822012-03-02 14:15:13 -0800936 int delay, boolean permanent, boolean adjustOccupied) {
Michael Jurkaa52570f2012-03-20 03:18:20 -0700937 ShortcutAndWidgetContainer clc = getShortcutsAndWidgets();
Adam Cohen482ed822012-03-02 14:15:13 -0800938 boolean[][] occupied = mOccupied;
939 if (!permanent) {
940 occupied = mTmpOccupied;
941 }
942
Adam Cohen19f37922012-03-21 11:59:11 -0700943 if (clc.indexOfChild(child) != -1) {
Adam Cohenbfbfd262011-06-13 16:55:12 -0700944 final LayoutParams lp = (LayoutParams) child.getLayoutParams();
945 final ItemInfo info = (ItemInfo) child.getTag();
946
947 // We cancel any existing animations
948 if (mReorderAnimators.containsKey(lp)) {
949 mReorderAnimators.get(lp).cancel();
950 mReorderAnimators.remove(lp);
951 }
952
Adam Cohen482ed822012-03-02 14:15:13 -0800953 final int oldX = lp.x;
954 final int oldY = lp.y;
955 if (adjustOccupied) {
956 occupied[lp.cellX][lp.cellY] = false;
957 occupied[cellX][cellY] = true;
958 }
Adam Cohenbfbfd262011-06-13 16:55:12 -0700959 lp.isLockedToGrid = true;
Adam Cohen482ed822012-03-02 14:15:13 -0800960 if (permanent) {
961 lp.cellX = info.cellX = cellX;
962 lp.cellY = info.cellY = cellY;
963 } else {
964 lp.tmpCellX = cellX;
965 lp.tmpCellY = cellY;
966 }
Adam Cohenbfbfd262011-06-13 16:55:12 -0700967 clc.setupLp(lp);
968 lp.isLockedToGrid = false;
Adam Cohen482ed822012-03-02 14:15:13 -0800969 final int newX = lp.x;
970 final int newY = lp.y;
Adam Cohenbfbfd262011-06-13 16:55:12 -0700971
Adam Cohen76fc0852011-06-17 13:26:23 -0700972 lp.x = oldX;
973 lp.y = oldY;
Adam Cohen76fc0852011-06-17 13:26:23 -0700974
Adam Cohen482ed822012-03-02 14:15:13 -0800975 // Exit early if we're not actually moving the view
976 if (oldX == newX && oldY == newY) {
977 lp.isLockedToGrid = true;
978 return true;
979 }
980
Michael Jurkaf1ad6082013-03-13 12:55:46 +0100981 ValueAnimator va = LauncherAnimUtils.ofFloat(child, 0f, 1f);
Adam Cohen482ed822012-03-02 14:15:13 -0800982 va.setDuration(duration);
983 mReorderAnimators.put(lp, va);
984
985 va.addUpdateListener(new AnimatorUpdateListener() {
986 @Override
Adam Cohenbfbfd262011-06-13 16:55:12 -0700987 public void onAnimationUpdate(ValueAnimator animation) {
Adam Cohen482ed822012-03-02 14:15:13 -0800988 float r = ((Float) animation.getAnimatedValue()).floatValue();
Adam Cohen19f37922012-03-21 11:59:11 -0700989 lp.x = (int) ((1 - r) * oldX + r * newX);
990 lp.y = (int) ((1 - r) * oldY + r * newY);
Adam Cohen6b8a02d2012-03-22 15:13:40 -0700991 child.requestLayout();
Adam Cohenbfbfd262011-06-13 16:55:12 -0700992 }
993 });
Adam Cohen482ed822012-03-02 14:15:13 -0800994 va.addListener(new AnimatorListenerAdapter() {
Adam Cohenbfbfd262011-06-13 16:55:12 -0700995 boolean cancelled = false;
996 public void onAnimationEnd(Animator animation) {
997 // If the animation was cancelled, it means that another animation
998 // has interrupted this one, and we don't want to lock the item into
999 // place just yet.
1000 if (!cancelled) {
1001 lp.isLockedToGrid = true;
Adam Cohen482ed822012-03-02 14:15:13 -08001002 child.requestLayout();
Adam Cohenbfbfd262011-06-13 16:55:12 -07001003 }
1004 if (mReorderAnimators.containsKey(lp)) {
1005 mReorderAnimators.remove(lp);
1006 }
1007 }
1008 public void onAnimationCancel(Animator animation) {
1009 cancelled = true;
1010 }
1011 });
Adam Cohen482ed822012-03-02 14:15:13 -08001012 va.setStartDelay(delay);
1013 va.start();
Adam Cohenbfbfd262011-06-13 16:55:12 -07001014 return true;
1015 }
1016 return false;
1017 }
1018
Adam Cohen482ed822012-03-02 14:15:13 -08001019 void visualizeDropLocation(View v, Bitmap dragOutline, int originX, int originY, int cellX,
1020 int cellY, int spanX, int spanY, boolean resize, Point dragOffset, Rect dragRegion) {
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -07001021 final int oldDragCellX = mDragCell[0];
1022 final int oldDragCellY = mDragCell[1];
Adam Cohen482ed822012-03-02 14:15:13 -08001023
Adam Cohen2801caf2011-05-13 20:57:39 -07001024 if (dragOutline == null && v == null) {
Adam Cohen2801caf2011-05-13 20:57:39 -07001025 return;
1026 }
1027
Adam Cohen482ed822012-03-02 14:15:13 -08001028 if (cellX != oldDragCellX || cellY != oldDragCellY) {
1029 mDragCell[0] = cellX;
1030 mDragCell[1] = cellY;
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001031
Joe Onorato4be866d2010-10-10 11:26:02 -07001032 final int oldIndex = mDragOutlineCurrent;
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -07001033 mDragOutlineAnims[oldIndex].animateOut();
1034 mDragOutlineCurrent = (oldIndex + 1) % mDragOutlines.length;
Adam Cohend41fbf52012-02-16 23:53:59 -08001035 Rect r = mDragOutlines[mDragOutlineCurrent];
Sunny Goyal106bf642015-07-16 12:18:06 -07001036
Adam Cohend41fbf52012-02-16 23:53:59 -08001037 if (resize) {
Adam Cohen482ed822012-03-02 14:15:13 -08001038 cellToRect(cellX, cellY, spanX, spanY, r);
Sunny Goyal106bf642015-07-16 12:18:06 -07001039 } else {
1040 // Find the top left corner of the rect the object will occupy
1041 final int[] topLeft = mTmpPoint;
1042 cellToPoint(cellX, cellY, topLeft);
1043
1044 int left = topLeft[0];
1045 int top = topLeft[1];
1046
1047 if (v != null && dragOffset == null) {
1048 // When drawing the drag outline, it did not account for margin offsets
1049 // added by the view's parent.
1050 MarginLayoutParams lp = (MarginLayoutParams) v.getLayoutParams();
1051 left += lp.leftMargin;
1052 top += lp.topMargin;
1053
1054 // Offsets due to the size difference between the View and the dragOutline.
1055 // There is a size difference to account for the outer blur, which may lie
1056 // outside the bounds of the view.
1057 top += (v.getHeight() - dragOutline.getHeight()) / 2;
1058 // We center about the x axis
1059 left += ((mCellWidth * spanX) + ((spanX - 1) * mWidthGap)
1060 - dragOutline.getWidth()) / 2;
1061 } else {
1062 if (dragOffset != null && dragRegion != null) {
1063 // Center the drag region *horizontally* in the cell and apply a drag
1064 // outline offset
1065 left += dragOffset.x + ((mCellWidth * spanX) + ((spanX - 1) * mWidthGap)
1066 - dragRegion.width()) / 2;
1067 int cHeight = getShortcutsAndWidgets().getCellContentHeight();
1068 int cellPaddingY = (int) Math.max(0, ((mCellHeight - cHeight) / 2f));
1069 top += dragOffset.y + cellPaddingY;
1070 } else {
1071 // Center the drag outline in the cell
1072 left += ((mCellWidth * spanX) + ((spanX - 1) * mWidthGap)
1073 - dragOutline.getWidth()) / 2;
1074 top += ((mCellHeight * spanY) + ((spanY - 1) * mHeightGap)
1075 - dragOutline.getHeight()) / 2;
1076 }
1077 }
1078 r.set(left, top, left + dragOutline.getWidth(), top + dragOutline.getHeight());
Adam Cohend41fbf52012-02-16 23:53:59 -08001079 }
Winson Chung150fbab2010-09-29 17:14:26 -07001080
Sunny Goyal106bf642015-07-16 12:18:06 -07001081 Utilities.scaleRectAboutCenter(r, getChildrenScale());
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -07001082 mDragOutlineAnims[mDragOutlineCurrent].setTag(dragOutline);
1083 mDragOutlineAnims[mDragOutlineCurrent].animateIn();
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001084 }
1085 }
1086
Adam Cohene0310962011-04-18 16:15:31 -07001087 public void clearDragOutlines() {
1088 final int oldIndex = mDragOutlineCurrent;
1089 mDragOutlineAnims[oldIndex].animateOut();
Adam Cohend41fbf52012-02-16 23:53:59 -08001090 mDragCell[0] = mDragCell[1] = -1;
Adam Cohene0310962011-04-18 16:15:31 -07001091 }
1092
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001093 /**
Jeff Sharkey70864282009-04-07 21:08:40 -07001094 * Find a vacant area that will fit the given bounds nearest the requested
1095 * cell location. Uses Euclidean distance to score multiple vacant areas.
Winson Chungaafa03c2010-06-11 17:34:16 -07001096 *
Romain Guy51afc022009-05-04 18:03:43 -07001097 * @param pixelX The X location at which you want to search for a vacant area.
1098 * @param pixelY The Y location at which you want to search for a vacant area.
Adam Cohend41fbf52012-02-16 23:53:59 -08001099 * @param minSpanX The minimum horizontal span required
1100 * @param minSpanY The minimum vertical span required
1101 * @param spanX Horizontal span of the object.
1102 * @param spanY Vertical span of the object.
1103 * @param result Array in which to place the result, or null (in which case a new array will
1104 * be allocated)
1105 * @return The X, Y cell of a vacant area that can contain this object,
1106 * nearest the requested location.
1107 */
1108 int[] findNearestVacantArea(int pixelX, int pixelY, int minSpanX, int minSpanY, int spanX,
1109 int spanY, int[] result, int[] resultSpan) {
Sunny Goyalf7a29e82015-04-24 15:20:43 -07001110 return findNearestArea(pixelX, pixelY, minSpanX, minSpanY, spanX, spanY, true,
Adam Cohend41fbf52012-02-16 23:53:59 -08001111 result, resultSpan);
1112 }
1113
Adam Cohend41fbf52012-02-16 23:53:59 -08001114 private final Stack<Rect> mTempRectStack = new Stack<Rect>();
1115 private void lazyInitTempRectStack() {
1116 if (mTempRectStack.isEmpty()) {
1117 for (int i = 0; i < mCountX * mCountY; i++) {
1118 mTempRectStack.push(new Rect());
1119 }
1120 }
1121 }
Adam Cohen482ed822012-03-02 14:15:13 -08001122
Adam Cohend41fbf52012-02-16 23:53:59 -08001123 private void recycleTempRects(Stack<Rect> used) {
1124 while (!used.isEmpty()) {
1125 mTempRectStack.push(used.pop());
1126 }
1127 }
1128
1129 /**
1130 * Find a vacant area that will fit the given bounds nearest the requested
1131 * cell location. Uses Euclidean distance to score multiple vacant areas.
1132 *
1133 * @param pixelX The X location at which you want to search for a vacant area.
1134 * @param pixelY The Y location at which you want to search for a vacant area.
1135 * @param minSpanX The minimum horizontal span required
1136 * @param minSpanY The minimum vertical span required
1137 * @param spanX Horizontal span of the object.
1138 * @param spanY Vertical span of the object.
1139 * @param ignoreOccupied If true, the result can be an occupied cell
1140 * @param result Array in which to place the result, or null (in which case a new array will
1141 * be allocated)
1142 * @return The X, Y cell of a vacant area that can contain this object,
1143 * nearest the requested location.
1144 */
Sunny Goyalf7a29e82015-04-24 15:20:43 -07001145 private int[] findNearestArea(int pixelX, int pixelY, int minSpanX, int minSpanY, int spanX,
1146 int spanY, boolean ignoreOccupied, int[] result, int[] resultSpan) {
Adam Cohend41fbf52012-02-16 23:53:59 -08001147 lazyInitTempRectStack();
Michael Jurkac6ee42e2010-09-30 12:04:50 -07001148
Adam Cohene3e27a82011-04-15 12:07:39 -07001149 // For items with a spanX / spanY > 1, the passed in point (pixelX, pixelY) corresponds
1150 // to the center of the item, but we are searching based on the top-left cell, so
1151 // we translate the point over to correspond to the top-left.
1152 pixelX -= (mCellWidth + mWidthGap) * (spanX - 1) / 2f;
1153 pixelY -= (mCellHeight + mHeightGap) * (spanY - 1) / 2f;
1154
Jeff Sharkey70864282009-04-07 21:08:40 -07001155 // Keep track of best-scoring drop area
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001156 final int[] bestXY = result != null ? result : new int[2];
Jeff Sharkey70864282009-04-07 21:08:40 -07001157 double bestDistance = Double.MAX_VALUE;
Adam Cohend41fbf52012-02-16 23:53:59 -08001158 final Rect bestRect = new Rect(-1, -1, -1, -1);
1159 final Stack<Rect> validRegions = new Stack<Rect>();
Winson Chungaafa03c2010-06-11 17:34:16 -07001160
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001161 final int countX = mCountX;
1162 final int countY = mCountY;
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001163
Adam Cohend41fbf52012-02-16 23:53:59 -08001164 if (minSpanX <= 0 || minSpanY <= 0 || spanX <= 0 || spanY <= 0 ||
1165 spanX < minSpanX || spanY < minSpanY) {
1166 return bestXY;
1167 }
1168
1169 for (int y = 0; y < countY - (minSpanY - 1); y++) {
Michael Jurkac28de512010-08-13 11:27:44 -07001170 inner:
Adam Cohend41fbf52012-02-16 23:53:59 -08001171 for (int x = 0; x < countX - (minSpanX - 1); x++) {
1172 int ySize = -1;
1173 int xSize = -1;
Adam Cohendf035382011-04-11 17:22:04 -07001174 if (ignoreOccupied) {
Adam Cohend41fbf52012-02-16 23:53:59 -08001175 // First, let's see if this thing fits anywhere
1176 for (int i = 0; i < minSpanX; i++) {
1177 for (int j = 0; j < minSpanY; j++) {
Sunny Goyalf7a29e82015-04-24 15:20:43 -07001178 if (mOccupied[x + i][y + j]) {
Adam Cohendf035382011-04-11 17:22:04 -07001179 continue inner;
1180 }
Michael Jurkac28de512010-08-13 11:27:44 -07001181 }
1182 }
Adam Cohend41fbf52012-02-16 23:53:59 -08001183 xSize = minSpanX;
1184 ySize = minSpanY;
1185
1186 // We know that the item will fit at _some_ acceptable size, now let's see
1187 // how big we can make it. We'll alternate between incrementing x and y spans
1188 // until we hit a limit.
1189 boolean incX = true;
1190 boolean hitMaxX = xSize >= spanX;
1191 boolean hitMaxY = ySize >= spanY;
1192 while (!(hitMaxX && hitMaxY)) {
1193 if (incX && !hitMaxX) {
1194 for (int j = 0; j < ySize; j++) {
Sunny Goyalf7a29e82015-04-24 15:20:43 -07001195 if (x + xSize > countX -1 || mOccupied[x + xSize][y + j]) {
Adam Cohend41fbf52012-02-16 23:53:59 -08001196 // We can't move out horizontally
1197 hitMaxX = true;
1198 }
1199 }
1200 if (!hitMaxX) {
1201 xSize++;
1202 }
1203 } else if (!hitMaxY) {
1204 for (int i = 0; i < xSize; i++) {
Sunny Goyalf7a29e82015-04-24 15:20:43 -07001205 if (y + ySize > countY - 1 || mOccupied[x + i][y + ySize]) {
Adam Cohend41fbf52012-02-16 23:53:59 -08001206 // We can't move out vertically
1207 hitMaxY = true;
1208 }
1209 }
1210 if (!hitMaxY) {
1211 ySize++;
1212 }
1213 }
1214 hitMaxX |= xSize >= spanX;
1215 hitMaxY |= ySize >= spanY;
1216 incX = !incX;
1217 }
1218 incX = true;
1219 hitMaxX = xSize >= spanX;
1220 hitMaxY = ySize >= spanY;
Michael Jurkac28de512010-08-13 11:27:44 -07001221 }
Sunny Goyal2805e632015-05-20 15:35:32 -07001222 final int[] cellXY = mTmpPoint;
Adam Cohene3e27a82011-04-15 12:07:39 -07001223 cellToCenterPoint(x, y, cellXY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001224
Adam Cohend41fbf52012-02-16 23:53:59 -08001225 // We verify that the current rect is not a sub-rect of any of our previous
1226 // candidates. In this case, the current rect is disqualified in favour of the
1227 // containing rect.
1228 Rect currentRect = mTempRectStack.pop();
1229 currentRect.set(x, y, x + xSize, y + ySize);
1230 boolean contained = false;
1231 for (Rect r : validRegions) {
1232 if (r.contains(currentRect)) {
1233 contained = true;
1234 break;
1235 }
1236 }
1237 validRegions.push(currentRect);
Sunny Goyalf7a29e82015-04-24 15:20:43 -07001238 double distance = Math.hypot(cellXY[0] - pixelX, cellXY[1] - pixelY);
Adam Cohen482ed822012-03-02 14:15:13 -08001239
Adam Cohend41fbf52012-02-16 23:53:59 -08001240 if ((distance <= bestDistance && !contained) ||
1241 currentRect.contains(bestRect)) {
Michael Jurkac28de512010-08-13 11:27:44 -07001242 bestDistance = distance;
1243 bestXY[0] = x;
1244 bestXY[1] = y;
Adam Cohend41fbf52012-02-16 23:53:59 -08001245 if (resultSpan != null) {
1246 resultSpan[0] = xSize;
1247 resultSpan[1] = ySize;
1248 }
1249 bestRect.set(currentRect);
Michael Jurkac28de512010-08-13 11:27:44 -07001250 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001251 }
1252 }
1253
Adam Cohenc0dcf592011-06-01 15:30:43 -07001254 // Return -1, -1 if no suitable location found
1255 if (bestDistance == Double.MAX_VALUE) {
1256 bestXY[0] = -1;
1257 bestXY[1] = -1;
Jeff Sharkey70864282009-04-07 21:08:40 -07001258 }
Adam Cohend41fbf52012-02-16 23:53:59 -08001259 recycleTempRects(validRegions);
Adam Cohenc0dcf592011-06-01 15:30:43 -07001260 return bestXY;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001261 }
Winson Chungaafa03c2010-06-11 17:34:16 -07001262
Adam Cohen482ed822012-03-02 14:15:13 -08001263 /**
1264 * Find a vacant area that will fit the given bounds nearest the requested
1265 * cell location, and will also weigh in a suggested direction vector of the
1266 * desired location. This method computers distance based on unit grid distances,
1267 * not pixel distances.
1268 *
Adam Cohen47a876d2012-03-19 13:21:41 -07001269 * @param cellX The X cell nearest to which you want to search for a vacant area.
1270 * @param cellY The Y cell nearest which you want to search for a vacant area.
Adam Cohen482ed822012-03-02 14:15:13 -08001271 * @param spanX Horizontal span of the object.
1272 * @param spanY Vertical span of the object.
Adam Cohen47a876d2012-03-19 13:21:41 -07001273 * @param direction The favored direction in which the views should move from x, y
1274 * @param exactDirectionOnly If this parameter is true, then only solutions where the direction
1275 * matches exactly. Otherwise we find the best matching direction.
1276 * @param occoupied The array which represents which cells in the CellLayout are occupied
1277 * @param blockOccupied The array which represents which cells in the specified block (cellX,
Winson Chung5f8afe62013-08-12 16:19:28 -07001278 * cellY, spanX, spanY) are occupied. This is used when try to move a group of views.
Adam Cohen482ed822012-03-02 14:15:13 -08001279 * @param result Array in which to place the result, or null (in which case a new array will
1280 * be allocated)
1281 * @return The X, Y cell of a vacant area that can contain this object,
1282 * nearest the requested location.
1283 */
1284 private int[] findNearestArea(int cellX, int cellY, int spanX, int spanY, int[] direction,
Adam Cohen47a876d2012-03-19 13:21:41 -07001285 boolean[][] occupied, boolean blockOccupied[][], int[] result) {
Adam Cohen482ed822012-03-02 14:15:13 -08001286 // Keep track of best-scoring drop area
1287 final int[] bestXY = result != null ? result : new int[2];
1288 float bestDistance = Float.MAX_VALUE;
1289 int bestDirectionScore = Integer.MIN_VALUE;
1290
1291 final int countX = mCountX;
1292 final int countY = mCountY;
1293
1294 for (int y = 0; y < countY - (spanY - 1); y++) {
1295 inner:
1296 for (int x = 0; x < countX - (spanX - 1); x++) {
1297 // First, let's see if this thing fits anywhere
1298 for (int i = 0; i < spanX; i++) {
1299 for (int j = 0; j < spanY; j++) {
Adam Cohen47a876d2012-03-19 13:21:41 -07001300 if (occupied[x + i][y + j] && (blockOccupied == null || blockOccupied[i][j])) {
Adam Cohen482ed822012-03-02 14:15:13 -08001301 continue inner;
1302 }
1303 }
1304 }
1305
Sunny Goyalf7a29e82015-04-24 15:20:43 -07001306 float distance = (float) Math.hypot(x - cellX, y - cellY);
Adam Cohen482ed822012-03-02 14:15:13 -08001307 int[] curDirection = mTmpPoint;
Adam Cohen47a876d2012-03-19 13:21:41 -07001308 computeDirectionVector(x - cellX, y - cellY, curDirection);
1309 // The direction score is just the dot product of the two candidate direction
1310 // and that passed in.
Adam Cohen482ed822012-03-02 14:15:13 -08001311 int curDirectionScore = direction[0] * curDirection[0] +
1312 direction[1] * curDirection[1];
Adam Cohen47a876d2012-03-19 13:21:41 -07001313 boolean exactDirectionOnly = false;
1314 boolean directionMatches = direction[0] == curDirection[0] &&
1315 direction[0] == curDirection[0];
1316 if ((directionMatches || !exactDirectionOnly) &&
1317 Float.compare(distance, bestDistance) < 0 || (Float.compare(distance,
Adam Cohen482ed822012-03-02 14:15:13 -08001318 bestDistance) == 0 && curDirectionScore > bestDirectionScore)) {
1319 bestDistance = distance;
1320 bestDirectionScore = curDirectionScore;
1321 bestXY[0] = x;
1322 bestXY[1] = y;
1323 }
1324 }
1325 }
1326
1327 // Return -1, -1 if no suitable location found
1328 if (bestDistance == Float.MAX_VALUE) {
1329 bestXY[0] = -1;
1330 bestXY[1] = -1;
1331 }
1332 return bestXY;
1333 }
1334
1335 private boolean addViewToTempLocation(View v, Rect rectOccupiedByPotentialDrop,
Adam Cohen8baab352012-03-20 17:39:21 -07001336 int[] direction, ItemConfiguration currentState) {
1337 CellAndSpan c = currentState.map.get(v);
Adam Cohen482ed822012-03-02 14:15:13 -08001338 boolean success = false;
Adam Cohen8baab352012-03-20 17:39:21 -07001339 markCellsForView(c.x, c.y, c.spanX, c.spanY, mTmpOccupied, false);
Adam Cohen482ed822012-03-02 14:15:13 -08001340 markCellsForRect(rectOccupiedByPotentialDrop, mTmpOccupied, true);
1341
Adam Cohen8baab352012-03-20 17:39:21 -07001342 findNearestArea(c.x, c.y, c.spanX, c.spanY, direction, mTmpOccupied, null, mTempLocation);
Adam Cohen482ed822012-03-02 14:15:13 -08001343
1344 if (mTempLocation[0] >= 0 && mTempLocation[1] >= 0) {
Adam Cohen8baab352012-03-20 17:39:21 -07001345 c.x = mTempLocation[0];
1346 c.y = mTempLocation[1];
Adam Cohen482ed822012-03-02 14:15:13 -08001347 success = true;
Adam Cohen482ed822012-03-02 14:15:13 -08001348 }
Adam Cohen8baab352012-03-20 17:39:21 -07001349 markCellsForView(c.x, c.y, c.spanX, c.spanY, mTmpOccupied, true);
Adam Cohen482ed822012-03-02 14:15:13 -08001350 return success;
1351 }
1352
Adam Cohenf3900c22012-11-16 18:28:11 -08001353 /**
1354 * This helper class defines a cluster of views. It helps with defining complex edges
1355 * of the cluster and determining how those edges interact with other views. The edges
1356 * essentially define a fine-grained boundary around the cluster of views -- like a more
1357 * precise version of a bounding box.
1358 */
1359 private class ViewCluster {
1360 final static int LEFT = 0;
1361 final static int TOP = 1;
1362 final static int RIGHT = 2;
1363 final static int BOTTOM = 3;
Adam Cohen47a876d2012-03-19 13:21:41 -07001364
Adam Cohenf3900c22012-11-16 18:28:11 -08001365 ArrayList<View> views;
1366 ItemConfiguration config;
1367 Rect boundingRect = new Rect();
Adam Cohen47a876d2012-03-19 13:21:41 -07001368
Adam Cohenf3900c22012-11-16 18:28:11 -08001369 int[] leftEdge = new int[mCountY];
1370 int[] rightEdge = new int[mCountY];
1371 int[] topEdge = new int[mCountX];
1372 int[] bottomEdge = new int[mCountX];
1373 boolean leftEdgeDirty, rightEdgeDirty, topEdgeDirty, bottomEdgeDirty, boundingRectDirty;
1374
1375 @SuppressWarnings("unchecked")
1376 public ViewCluster(ArrayList<View> views, ItemConfiguration config) {
1377 this.views = (ArrayList<View>) views.clone();
1378 this.config = config;
1379 resetEdges();
Adam Cohen47a876d2012-03-19 13:21:41 -07001380 }
1381
Adam Cohenf3900c22012-11-16 18:28:11 -08001382 void resetEdges() {
1383 for (int i = 0; i < mCountX; i++) {
1384 topEdge[i] = -1;
1385 bottomEdge[i] = -1;
1386 }
1387 for (int i = 0; i < mCountY; i++) {
1388 leftEdge[i] = -1;
1389 rightEdge[i] = -1;
1390 }
1391 leftEdgeDirty = true;
1392 rightEdgeDirty = true;
1393 bottomEdgeDirty = true;
1394 topEdgeDirty = true;
1395 boundingRectDirty = true;
1396 }
Adam Cohen47a876d2012-03-19 13:21:41 -07001397
Adam Cohenf3900c22012-11-16 18:28:11 -08001398 void computeEdge(int which, int[] edge) {
1399 int count = views.size();
1400 for (int i = 0; i < count; i++) {
1401 CellAndSpan cs = config.map.get(views.get(i));
1402 switch (which) {
1403 case LEFT:
1404 int left = cs.x;
1405 for (int j = cs.y; j < cs.y + cs.spanY; j++) {
1406 if (left < edge[j] || edge[j] < 0) {
1407 edge[j] = left;
Adam Cohena56dc102012-07-13 13:41:42 -07001408 }
Adam Cohen47a876d2012-03-19 13:21:41 -07001409 }
Adam Cohenf3900c22012-11-16 18:28:11 -08001410 break;
1411 case RIGHT:
1412 int right = cs.x + cs.spanX;
1413 for (int j = cs.y; j < cs.y + cs.spanY; j++) {
1414 if (right > edge[j]) {
1415 edge[j] = right;
1416 }
1417 }
1418 break;
1419 case TOP:
1420 int top = cs.y;
1421 for (int j = cs.x; j < cs.x + cs.spanX; j++) {
1422 if (top < edge[j] || edge[j] < 0) {
1423 edge[j] = top;
1424 }
1425 }
1426 break;
1427 case BOTTOM:
1428 int bottom = cs.y + cs.spanY;
1429 for (int j = cs.x; j < cs.x + cs.spanX; j++) {
1430 if (bottom > edge[j]) {
1431 edge[j] = bottom;
1432 }
1433 }
1434 break;
Adam Cohen47a876d2012-03-19 13:21:41 -07001435 }
1436 }
1437 }
Adam Cohenf3900c22012-11-16 18:28:11 -08001438
1439 boolean isViewTouchingEdge(View v, int whichEdge) {
1440 CellAndSpan cs = config.map.get(v);
1441
1442 int[] edge = getEdge(whichEdge);
1443
1444 switch (whichEdge) {
1445 case LEFT:
1446 for (int i = cs.y; i < cs.y + cs.spanY; i++) {
1447 if (edge[i] == cs.x + cs.spanX) {
1448 return true;
1449 }
1450 }
1451 break;
1452 case RIGHT:
1453 for (int i = cs.y; i < cs.y + cs.spanY; i++) {
1454 if (edge[i] == cs.x) {
1455 return true;
1456 }
1457 }
1458 break;
1459 case TOP:
1460 for (int i = cs.x; i < cs.x + cs.spanX; i++) {
1461 if (edge[i] == cs.y + cs.spanY) {
1462 return true;
1463 }
1464 }
1465 break;
1466 case BOTTOM:
1467 for (int i = cs.x; i < cs.x + cs.spanX; i++) {
1468 if (edge[i] == cs.y) {
1469 return true;
1470 }
1471 }
1472 break;
1473 }
1474 return false;
1475 }
1476
1477 void shift(int whichEdge, int delta) {
1478 for (View v: views) {
1479 CellAndSpan c = config.map.get(v);
1480 switch (whichEdge) {
1481 case LEFT:
1482 c.x -= delta;
1483 break;
1484 case RIGHT:
1485 c.x += delta;
1486 break;
1487 case TOP:
1488 c.y -= delta;
1489 break;
1490 case BOTTOM:
1491 default:
1492 c.y += delta;
1493 break;
1494 }
1495 }
1496 resetEdges();
1497 }
1498
1499 public void addView(View v) {
1500 views.add(v);
1501 resetEdges();
1502 }
1503
1504 public Rect getBoundingRect() {
1505 if (boundingRectDirty) {
1506 boolean first = true;
1507 for (View v: views) {
1508 CellAndSpan c = config.map.get(v);
1509 if (first) {
1510 boundingRect.set(c.x, c.y, c.x + c.spanX, c.y + c.spanY);
1511 first = false;
1512 } else {
1513 boundingRect.union(c.x, c.y, c.x + c.spanX, c.y + c.spanY);
1514 }
1515 }
1516 }
1517 return boundingRect;
1518 }
1519
1520 public int[] getEdge(int which) {
1521 switch (which) {
1522 case LEFT:
1523 return getLeftEdge();
1524 case RIGHT:
1525 return getRightEdge();
1526 case TOP:
1527 return getTopEdge();
1528 case BOTTOM:
1529 default:
1530 return getBottomEdge();
1531 }
1532 }
1533
1534 public int[] getLeftEdge() {
1535 if (leftEdgeDirty) {
1536 computeEdge(LEFT, leftEdge);
1537 }
1538 return leftEdge;
1539 }
1540
1541 public int[] getRightEdge() {
1542 if (rightEdgeDirty) {
1543 computeEdge(RIGHT, rightEdge);
1544 }
1545 return rightEdge;
1546 }
1547
1548 public int[] getTopEdge() {
1549 if (topEdgeDirty) {
1550 computeEdge(TOP, topEdge);
1551 }
1552 return topEdge;
1553 }
1554
1555 public int[] getBottomEdge() {
1556 if (bottomEdgeDirty) {
1557 computeEdge(BOTTOM, bottomEdge);
1558 }
1559 return bottomEdge;
1560 }
1561
1562 PositionComparator comparator = new PositionComparator();
1563 class PositionComparator implements Comparator<View> {
1564 int whichEdge = 0;
1565 public int compare(View left, View right) {
1566 CellAndSpan l = config.map.get(left);
1567 CellAndSpan r = config.map.get(right);
1568 switch (whichEdge) {
1569 case LEFT:
1570 return (r.x + r.spanX) - (l.x + l.spanX);
1571 case RIGHT:
1572 return l.x - r.x;
1573 case TOP:
1574 return (r.y + r.spanY) - (l.y + l.spanY);
1575 case BOTTOM:
1576 default:
1577 return l.y - r.y;
1578 }
1579 }
1580 }
1581
1582 public void sortConfigurationForEdgePush(int edge) {
1583 comparator.whichEdge = edge;
1584 Collections.sort(config.sortedViews, comparator);
1585 }
Adam Cohen47a876d2012-03-19 13:21:41 -07001586 }
1587
Adam Cohenf3900c22012-11-16 18:28:11 -08001588 private boolean pushViewsToTempLocation(ArrayList<View> views, Rect rectOccupiedByPotentialDrop,
1589 int[] direction, View dragView, ItemConfiguration currentState) {
Adam Cohene0489502012-08-27 15:18:53 -07001590
Adam Cohenf3900c22012-11-16 18:28:11 -08001591 ViewCluster cluster = new ViewCluster(views, currentState);
1592 Rect clusterRect = cluster.getBoundingRect();
1593 int whichEdge;
1594 int pushDistance;
1595 boolean fail = false;
1596
1597 // Determine the edge of the cluster that will be leading the push and how far
1598 // the cluster must be shifted.
1599 if (direction[0] < 0) {
1600 whichEdge = ViewCluster.LEFT;
1601 pushDistance = clusterRect.right - rectOccupiedByPotentialDrop.left;
Adam Cohene0489502012-08-27 15:18:53 -07001602 } else if (direction[0] > 0) {
Adam Cohenf3900c22012-11-16 18:28:11 -08001603 whichEdge = ViewCluster.RIGHT;
1604 pushDistance = rectOccupiedByPotentialDrop.right - clusterRect.left;
1605 } else if (direction[1] < 0) {
1606 whichEdge = ViewCluster.TOP;
1607 pushDistance = clusterRect.bottom - rectOccupiedByPotentialDrop.top;
1608 } else {
1609 whichEdge = ViewCluster.BOTTOM;
1610 pushDistance = rectOccupiedByPotentialDrop.bottom - clusterRect.top;
Adam Cohene0489502012-08-27 15:18:53 -07001611 }
1612
Adam Cohenf3900c22012-11-16 18:28:11 -08001613 // Break early for invalid push distance.
1614 if (pushDistance <= 0) {
1615 return false;
Adam Cohene0489502012-08-27 15:18:53 -07001616 }
Adam Cohenf3900c22012-11-16 18:28:11 -08001617
1618 // Mark the occupied state as false for the group of views we want to move.
1619 for (View v: views) {
1620 CellAndSpan c = currentState.map.get(v);
1621 markCellsForView(c.x, c.y, c.spanX, c.spanY, mTmpOccupied, false);
1622 }
1623
1624 // We save the current configuration -- if we fail to find a solution we will revert
1625 // to the initial state. The process of finding a solution modifies the configuration
1626 // in place, hence the need for revert in the failure case.
1627 currentState.save();
1628
1629 // The pushing algorithm is simplified by considering the views in the order in which
1630 // they would be pushed by the cluster. For example, if the cluster is leading with its
1631 // left edge, we consider sort the views by their right edge, from right to left.
1632 cluster.sortConfigurationForEdgePush(whichEdge);
1633
1634 while (pushDistance > 0 && !fail) {
1635 for (View v: currentState.sortedViews) {
1636 // For each view that isn't in the cluster, we see if the leading edge of the
1637 // cluster is contacting the edge of that view. If so, we add that view to the
1638 // cluster.
1639 if (!cluster.views.contains(v) && v != dragView) {
1640 if (cluster.isViewTouchingEdge(v, whichEdge)) {
1641 LayoutParams lp = (LayoutParams) v.getLayoutParams();
1642 if (!lp.canReorder) {
1643 // The push solution includes the all apps button, this is not viable.
1644 fail = true;
1645 break;
1646 }
1647 cluster.addView(v);
1648 CellAndSpan c = currentState.map.get(v);
1649
1650 // Adding view to cluster, mark it as not occupied.
1651 markCellsForView(c.x, c.y, c.spanX, c.spanY, mTmpOccupied, false);
1652 }
1653 }
1654 }
1655 pushDistance--;
1656
1657 // The cluster has been completed, now we move the whole thing over in the appropriate
1658 // direction.
1659 cluster.shift(whichEdge, 1);
1660 }
1661
1662 boolean foundSolution = false;
1663 clusterRect = cluster.getBoundingRect();
1664
1665 // Due to the nature of the algorithm, the only check required to verify a valid solution
1666 // is to ensure that completed shifted cluster lies completely within the cell layout.
1667 if (!fail && clusterRect.left >= 0 && clusterRect.right <= mCountX && clusterRect.top >= 0 &&
1668 clusterRect.bottom <= mCountY) {
1669 foundSolution = true;
1670 } else {
1671 currentState.restore();
1672 }
1673
1674 // In either case, we set the occupied array as marked for the location of the views
1675 for (View v: cluster.views) {
1676 CellAndSpan c = currentState.map.get(v);
1677 markCellsForView(c.x, c.y, c.spanX, c.spanY, mTmpOccupied, true);
1678 }
1679
1680 return foundSolution;
Adam Cohene0489502012-08-27 15:18:53 -07001681 }
1682
Adam Cohen482ed822012-03-02 14:15:13 -08001683 private boolean addViewsToTempLocation(ArrayList<View> views, Rect rectOccupiedByPotentialDrop,
Adam Cohenf3900c22012-11-16 18:28:11 -08001684 int[] direction, View dragView, ItemConfiguration currentState) {
Adam Cohen482ed822012-03-02 14:15:13 -08001685 if (views.size() == 0) return true;
Adam Cohen482ed822012-03-02 14:15:13 -08001686
Adam Cohen8baab352012-03-20 17:39:21 -07001687 boolean success = false;
Adam Cohen482ed822012-03-02 14:15:13 -08001688 Rect boundingRect = null;
Adam Cohen8baab352012-03-20 17:39:21 -07001689 // We construct a rect which represents the entire group of views passed in
Adam Cohen482ed822012-03-02 14:15:13 -08001690 for (View v: views) {
Adam Cohen8baab352012-03-20 17:39:21 -07001691 CellAndSpan c = currentState.map.get(v);
Adam Cohen482ed822012-03-02 14:15:13 -08001692 if (boundingRect == null) {
Adam Cohen8baab352012-03-20 17:39:21 -07001693 boundingRect = new Rect(c.x, c.y, c.x + c.spanX, c.y + c.spanY);
Adam Cohen482ed822012-03-02 14:15:13 -08001694 } else {
Adam Cohen8baab352012-03-20 17:39:21 -07001695 boundingRect.union(c.x, c.y, c.x + c.spanX, c.y + c.spanY);
Adam Cohen482ed822012-03-02 14:15:13 -08001696 }
1697 }
Adam Cohen8baab352012-03-20 17:39:21 -07001698
Adam Cohen8baab352012-03-20 17:39:21 -07001699 // Mark the occupied state as false for the group of views we want to move.
Adam Cohenf3900c22012-11-16 18:28:11 -08001700 for (View v: views) {
Adam Cohen8baab352012-03-20 17:39:21 -07001701 CellAndSpan c = currentState.map.get(v);
1702 markCellsForView(c.x, c.y, c.spanX, c.spanY, mTmpOccupied, false);
1703 }
1704
Adam Cohen47a876d2012-03-19 13:21:41 -07001705 boolean[][] blockOccupied = new boolean[boundingRect.width()][boundingRect.height()];
1706 int top = boundingRect.top;
1707 int left = boundingRect.left;
Adam Cohen8baab352012-03-20 17:39:21 -07001708 // We mark more precisely which parts of the bounding rect are truly occupied, allowing
Adam Cohena56dc102012-07-13 13:41:42 -07001709 // for interlocking.
Adam Cohenf3900c22012-11-16 18:28:11 -08001710 for (View v: views) {
Adam Cohen8baab352012-03-20 17:39:21 -07001711 CellAndSpan c = currentState.map.get(v);
1712 markCellsForView(c.x - left, c.y - top, c.spanX, c.spanY, blockOccupied, true);
Adam Cohen47a876d2012-03-19 13:21:41 -07001713 }
1714
Adam Cohen482ed822012-03-02 14:15:13 -08001715 markCellsForRect(rectOccupiedByPotentialDrop, mTmpOccupied, true);
1716
Adam Cohenf3900c22012-11-16 18:28:11 -08001717 findNearestArea(boundingRect.left, boundingRect.top, boundingRect.width(),
1718 boundingRect.height(), direction, mTmpOccupied, blockOccupied, mTempLocation);
Adam Cohen482ed822012-03-02 14:15:13 -08001719
Adam Cohen8baab352012-03-20 17:39:21 -07001720 // If we successfuly found a location by pushing the block of views, we commit it
Adam Cohen482ed822012-03-02 14:15:13 -08001721 if (mTempLocation[0] >= 0 && mTempLocation[1] >= 0) {
Adam Cohen8baab352012-03-20 17:39:21 -07001722 int deltaX = mTempLocation[0] - boundingRect.left;
1723 int deltaY = mTempLocation[1] - boundingRect.top;
Adam Cohenf3900c22012-11-16 18:28:11 -08001724 for (View v: views) {
Adam Cohen8baab352012-03-20 17:39:21 -07001725 CellAndSpan c = currentState.map.get(v);
1726 c.x += deltaX;
1727 c.y += deltaY;
Adam Cohen482ed822012-03-02 14:15:13 -08001728 }
1729 success = true;
1730 }
Adam Cohen8baab352012-03-20 17:39:21 -07001731
1732 // In either case, we set the occupied array as marked for the location of the views
Adam Cohenf3900c22012-11-16 18:28:11 -08001733 for (View v: views) {
Adam Cohen8baab352012-03-20 17:39:21 -07001734 CellAndSpan c = currentState.map.get(v);
1735 markCellsForView(c.x, c.y, c.spanX, c.spanY, mTmpOccupied, true);
Adam Cohen482ed822012-03-02 14:15:13 -08001736 }
1737 return success;
1738 }
1739
1740 private void markCellsForRect(Rect r, boolean[][] occupied, boolean value) {
1741 markCellsForView(r.left, r.top, r.width(), r.height(), occupied, value);
1742 }
1743
Adam Cohen4abc5bd2012-05-29 21:06:03 -07001744 // This method tries to find a reordering solution which satisfies the push mechanic by trying
1745 // to push items in each of the cardinal directions, in an order based on the direction vector
1746 // passed.
1747 private boolean attemptPushInDirection(ArrayList<View> intersectingViews, Rect occupied,
1748 int[] direction, View ignoreView, ItemConfiguration solution) {
1749 if ((Math.abs(direction[0]) + Math.abs(direction[1])) > 1) {
Winson Chung5f8afe62013-08-12 16:19:28 -07001750 // If the direction vector has two non-zero components, we try pushing
Adam Cohen4abc5bd2012-05-29 21:06:03 -07001751 // separately in each of the components.
1752 int temp = direction[1];
1753 direction[1] = 0;
Adam Cohenf3900c22012-11-16 18:28:11 -08001754
1755 if (pushViewsToTempLocation(intersectingViews, occupied, direction,
Adam Cohen4abc5bd2012-05-29 21:06:03 -07001756 ignoreView, solution)) {
1757 return true;
1758 }
1759 direction[1] = temp;
1760 temp = direction[0];
1761 direction[0] = 0;
Adam Cohenf3900c22012-11-16 18:28:11 -08001762
1763 if (pushViewsToTempLocation(intersectingViews, occupied, direction,
Adam Cohen4abc5bd2012-05-29 21:06:03 -07001764 ignoreView, solution)) {
1765 return true;
1766 }
1767 // Revert the direction
1768 direction[0] = temp;
1769
1770 // Now we try pushing in each component of the opposite direction
1771 direction[0] *= -1;
1772 direction[1] *= -1;
1773 temp = direction[1];
1774 direction[1] = 0;
Adam Cohenf3900c22012-11-16 18:28:11 -08001775 if (pushViewsToTempLocation(intersectingViews, occupied, direction,
Adam Cohen4abc5bd2012-05-29 21:06:03 -07001776 ignoreView, solution)) {
1777 return true;
1778 }
1779
1780 direction[1] = temp;
1781 temp = direction[0];
1782 direction[0] = 0;
Adam Cohenf3900c22012-11-16 18:28:11 -08001783 if (pushViewsToTempLocation(intersectingViews, occupied, direction,
Adam Cohen4abc5bd2012-05-29 21:06:03 -07001784 ignoreView, solution)) {
1785 return true;
1786 }
1787 // revert the direction
1788 direction[0] = temp;
1789 direction[0] *= -1;
1790 direction[1] *= -1;
Winson Chung5f8afe62013-08-12 16:19:28 -07001791
Adam Cohen4abc5bd2012-05-29 21:06:03 -07001792 } else {
1793 // If the direction vector has a single non-zero component, we push first in the
1794 // direction of the vector
Adam Cohenf3900c22012-11-16 18:28:11 -08001795 if (pushViewsToTempLocation(intersectingViews, occupied, direction,
Adam Cohen4abc5bd2012-05-29 21:06:03 -07001796 ignoreView, solution)) {
1797 return true;
1798 }
Adam Cohen4abc5bd2012-05-29 21:06:03 -07001799 // Then we try the opposite direction
1800 direction[0] *= -1;
1801 direction[1] *= -1;
Adam Cohenf3900c22012-11-16 18:28:11 -08001802 if (pushViewsToTempLocation(intersectingViews, occupied, direction,
Adam Cohen4abc5bd2012-05-29 21:06:03 -07001803 ignoreView, solution)) {
1804 return true;
1805 }
1806 // Switch the direction back
1807 direction[0] *= -1;
1808 direction[1] *= -1;
Winson Chung5f8afe62013-08-12 16:19:28 -07001809
1810 // If we have failed to find a push solution with the above, then we try
Adam Cohen4abc5bd2012-05-29 21:06:03 -07001811 // to find a solution by pushing along the perpendicular axis.
1812
1813 // Swap the components
1814 int temp = direction[1];
1815 direction[1] = direction[0];
1816 direction[0] = temp;
Adam Cohenf3900c22012-11-16 18:28:11 -08001817 if (pushViewsToTempLocation(intersectingViews, occupied, direction,
Adam Cohen4abc5bd2012-05-29 21:06:03 -07001818 ignoreView, solution)) {
1819 return true;
1820 }
1821
1822 // Then we try the opposite direction
1823 direction[0] *= -1;
1824 direction[1] *= -1;
Adam Cohenf3900c22012-11-16 18:28:11 -08001825 if (pushViewsToTempLocation(intersectingViews, occupied, direction,
Adam Cohen4abc5bd2012-05-29 21:06:03 -07001826 ignoreView, solution)) {
1827 return true;
1828 }
1829 // Switch the direction back
1830 direction[0] *= -1;
1831 direction[1] *= -1;
1832
1833 // Swap the components back
1834 temp = direction[1];
1835 direction[1] = direction[0];
1836 direction[0] = temp;
1837 }
1838 return false;
1839 }
1840
Adam Cohen482ed822012-03-02 14:15:13 -08001841 private boolean rearrangementExists(int cellX, int cellY, int spanX, int spanY, int[] direction,
Adam Cohen8baab352012-03-20 17:39:21 -07001842 View ignoreView, ItemConfiguration solution) {
Winson Chunge3e03bc2012-05-01 15:10:11 -07001843 // Return early if get invalid cell positions
1844 if (cellX < 0 || cellY < 0) return false;
Adam Cohen482ed822012-03-02 14:15:13 -08001845
Adam Cohen8baab352012-03-20 17:39:21 -07001846 mIntersectingViews.clear();
Adam Cohen482ed822012-03-02 14:15:13 -08001847 mOccupiedRect.set(cellX, cellY, cellX + spanX, cellY + spanY);
Adam Cohen482ed822012-03-02 14:15:13 -08001848
Adam Cohen8baab352012-03-20 17:39:21 -07001849 // Mark the desired location of the view currently being dragged.
Adam Cohen482ed822012-03-02 14:15:13 -08001850 if (ignoreView != null) {
Adam Cohen8baab352012-03-20 17:39:21 -07001851 CellAndSpan c = solution.map.get(ignoreView);
Adam Cohen19f37922012-03-21 11:59:11 -07001852 if (c != null) {
1853 c.x = cellX;
1854 c.y = cellY;
1855 }
Adam Cohen482ed822012-03-02 14:15:13 -08001856 }
Adam Cohen482ed822012-03-02 14:15:13 -08001857 Rect r0 = new Rect(cellX, cellY, cellX + spanX, cellY + spanY);
1858 Rect r1 = new Rect();
Adam Cohen8baab352012-03-20 17:39:21 -07001859 for (View child: solution.map.keySet()) {
Adam Cohen482ed822012-03-02 14:15:13 -08001860 if (child == ignoreView) continue;
Adam Cohen8baab352012-03-20 17:39:21 -07001861 CellAndSpan c = solution.map.get(child);
Adam Cohen482ed822012-03-02 14:15:13 -08001862 LayoutParams lp = (LayoutParams) child.getLayoutParams();
Adam Cohen8baab352012-03-20 17:39:21 -07001863 r1.set(c.x, c.y, c.x + c.spanX, c.y + c.spanY);
Adam Cohen482ed822012-03-02 14:15:13 -08001864 if (Rect.intersects(r0, r1)) {
1865 if (!lp.canReorder) {
1866 return false;
1867 }
1868 mIntersectingViews.add(child);
1869 }
1870 }
Adam Cohen47a876d2012-03-19 13:21:41 -07001871
Adam Cohenfa3c58f2013-12-06 16:10:55 -08001872 solution.intersectingViews = new ArrayList<View>(mIntersectingViews);
1873
Winson Chung5f8afe62013-08-12 16:19:28 -07001874 // First we try to find a solution which respects the push mechanic. That is,
Adam Cohen4abc5bd2012-05-29 21:06:03 -07001875 // we try to find a solution such that no displaced item travels through another item
1876 // without also displacing that item.
1877 if (attemptPushInDirection(mIntersectingViews, mOccupiedRect, direction, ignoreView,
Adam Cohen19f37922012-03-21 11:59:11 -07001878 solution)) {
Adam Cohen47a876d2012-03-19 13:21:41 -07001879 return true;
1880 }
Adam Cohen47a876d2012-03-19 13:21:41 -07001881
Adam Cohen4abc5bd2012-05-29 21:06:03 -07001882 // Next we try moving the views as a block, but without requiring the push mechanic.
Adam Cohenf3900c22012-11-16 18:28:11 -08001883 if (addViewsToTempLocation(mIntersectingViews, mOccupiedRect, direction, ignoreView,
Adam Cohen19f37922012-03-21 11:59:11 -07001884 solution)) {
Adam Cohen482ed822012-03-02 14:15:13 -08001885 return true;
1886 }
Adam Cohen47a876d2012-03-19 13:21:41 -07001887
Adam Cohen482ed822012-03-02 14:15:13 -08001888 // Ok, they couldn't move as a block, let's move them individually
1889 for (View v : mIntersectingViews) {
Adam Cohen8baab352012-03-20 17:39:21 -07001890 if (!addViewToTempLocation(v, mOccupiedRect, direction, solution)) {
Adam Cohen482ed822012-03-02 14:15:13 -08001891 return false;
1892 }
1893 }
1894 return true;
1895 }
1896
1897 /*
1898 * Returns a pair (x, y), where x,y are in {-1, 0, 1} corresponding to vector between
1899 * the provided point and the provided cell
1900 */
Adam Cohen47a876d2012-03-19 13:21:41 -07001901 private void computeDirectionVector(float deltaX, float deltaY, int[] result) {
Adam Cohen482ed822012-03-02 14:15:13 -08001902 double angle = Math.atan(((float) deltaY) / deltaX);
1903
1904 result[0] = 0;
1905 result[1] = 0;
1906 if (Math.abs(Math.cos(angle)) > 0.5f) {
1907 result[0] = (int) Math.signum(deltaX);
1908 }
1909 if (Math.abs(Math.sin(angle)) > 0.5f) {
1910 result[1] = (int) Math.signum(deltaY);
1911 }
1912 }
1913
Adam Cohen8baab352012-03-20 17:39:21 -07001914 private void copyOccupiedArray(boolean[][] occupied) {
1915 for (int i = 0; i < mCountX; i++) {
1916 for (int j = 0; j < mCountY; j++) {
1917 occupied[i][j] = mOccupied[i][j];
1918 }
1919 }
1920 }
1921
Sunny Goyalf7a29e82015-04-24 15:20:43 -07001922 private ItemConfiguration findReorderSolution(int pixelX, int pixelY, int minSpanX, int minSpanY,
Adam Cohenfa3c58f2013-12-06 16:10:55 -08001923 int spanX, int spanY, int[] direction, View dragView, boolean decX,
1924 ItemConfiguration solution) {
Adam Cohen8baab352012-03-20 17:39:21 -07001925 // Copy the current state into the solution. This solution will be manipulated as necessary.
1926 copyCurrentStateToSolution(solution, false);
1927 // Copy the current occupied array into the temporary occupied array. This array will be
1928 // manipulated as necessary to find a solution.
1929 copyOccupiedArray(mTmpOccupied);
Adam Cohen482ed822012-03-02 14:15:13 -08001930
1931 // We find the nearest cell into which we would place the dragged item, assuming there's
1932 // nothing in its way.
1933 int result[] = new int[2];
1934 result = findNearestArea(pixelX, pixelY, spanX, spanY, result);
1935
1936 boolean success = false;
1937 // First we try the exact nearest position of the item being dragged,
1938 // we will then want to try to move this around to other neighbouring positions
Adam Cohen8baab352012-03-20 17:39:21 -07001939 success = rearrangementExists(result[0], result[1], spanX, spanY, direction, dragView,
1940 solution);
Adam Cohen482ed822012-03-02 14:15:13 -08001941
1942 if (!success) {
1943 // We try shrinking the widget down to size in an alternating pattern, shrink 1 in
1944 // x, then 1 in y etc.
1945 if (spanX > minSpanX && (minSpanY == spanY || decX)) {
Adam Cohenfa3c58f2013-12-06 16:10:55 -08001946 return findReorderSolution(pixelX, pixelY, minSpanX, minSpanY, spanX - 1, spanY,
1947 direction, dragView, false, solution);
Adam Cohen482ed822012-03-02 14:15:13 -08001948 } else if (spanY > minSpanY) {
Adam Cohenfa3c58f2013-12-06 16:10:55 -08001949 return findReorderSolution(pixelX, pixelY, minSpanX, minSpanY, spanX, spanY - 1,
1950 direction, dragView, true, solution);
Adam Cohen482ed822012-03-02 14:15:13 -08001951 }
1952 solution.isSolution = false;
1953 } else {
1954 solution.isSolution = true;
1955 solution.dragViewX = result[0];
1956 solution.dragViewY = result[1];
1957 solution.dragViewSpanX = spanX;
1958 solution.dragViewSpanY = spanY;
Adam Cohen482ed822012-03-02 14:15:13 -08001959 }
1960 return solution;
1961 }
1962
1963 private void copyCurrentStateToSolution(ItemConfiguration solution, boolean temp) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07001964 int childCount = mShortcutsAndWidgets.getChildCount();
Adam Cohen482ed822012-03-02 14:15:13 -08001965 for (int i = 0; i < childCount; i++) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07001966 View child = mShortcutsAndWidgets.getChildAt(i);
Adam Cohen482ed822012-03-02 14:15:13 -08001967 LayoutParams lp = (LayoutParams) child.getLayoutParams();
Adam Cohen8baab352012-03-20 17:39:21 -07001968 CellAndSpan c;
Adam Cohen482ed822012-03-02 14:15:13 -08001969 if (temp) {
Adam Cohen8baab352012-03-20 17:39:21 -07001970 c = new CellAndSpan(lp.tmpCellX, lp.tmpCellY, lp.cellHSpan, lp.cellVSpan);
Adam Cohen482ed822012-03-02 14:15:13 -08001971 } else {
Adam Cohen8baab352012-03-20 17:39:21 -07001972 c = new CellAndSpan(lp.cellX, lp.cellY, lp.cellHSpan, lp.cellVSpan);
Adam Cohen482ed822012-03-02 14:15:13 -08001973 }
Adam Cohenf3900c22012-11-16 18:28:11 -08001974 solution.add(child, c);
Adam Cohen482ed822012-03-02 14:15:13 -08001975 }
1976 }
1977
1978 private void copySolutionToTempState(ItemConfiguration solution, View dragView) {
1979 for (int i = 0; i < mCountX; i++) {
1980 for (int j = 0; j < mCountY; j++) {
1981 mTmpOccupied[i][j] = false;
1982 }
1983 }
1984
Michael Jurkaa52570f2012-03-20 03:18:20 -07001985 int childCount = mShortcutsAndWidgets.getChildCount();
Adam Cohen482ed822012-03-02 14:15:13 -08001986 for (int i = 0; i < childCount; i++) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07001987 View child = mShortcutsAndWidgets.getChildAt(i);
Adam Cohen482ed822012-03-02 14:15:13 -08001988 if (child == dragView) continue;
1989 LayoutParams lp = (LayoutParams) child.getLayoutParams();
Adam Cohen8baab352012-03-20 17:39:21 -07001990 CellAndSpan c = solution.map.get(child);
1991 if (c != null) {
1992 lp.tmpCellX = c.x;
1993 lp.tmpCellY = c.y;
1994 lp.cellHSpan = c.spanX;
1995 lp.cellVSpan = c.spanY;
1996 markCellsForView(c.x, c.y, c.spanX, c.spanY, mTmpOccupied, true);
Adam Cohen482ed822012-03-02 14:15:13 -08001997 }
1998 }
1999 markCellsForView(solution.dragViewX, solution.dragViewY, solution.dragViewSpanX,
2000 solution.dragViewSpanY, mTmpOccupied, true);
2001 }
2002
2003 private void animateItemsToSolution(ItemConfiguration solution, View dragView, boolean
2004 commitDragView) {
2005
2006 boolean[][] occupied = DESTRUCTIVE_REORDER ? mOccupied : mTmpOccupied;
2007 for (int i = 0; i < mCountX; i++) {
2008 for (int j = 0; j < mCountY; j++) {
2009 occupied[i][j] = false;
2010 }
2011 }
2012
Michael Jurkaa52570f2012-03-20 03:18:20 -07002013 int childCount = mShortcutsAndWidgets.getChildCount();
Adam Cohen482ed822012-03-02 14:15:13 -08002014 for (int i = 0; i < childCount; i++) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07002015 View child = mShortcutsAndWidgets.getChildAt(i);
Adam Cohen482ed822012-03-02 14:15:13 -08002016 if (child == dragView) continue;
Adam Cohen8baab352012-03-20 17:39:21 -07002017 CellAndSpan c = solution.map.get(child);
2018 if (c != null) {
Adam Cohen19f37922012-03-21 11:59:11 -07002019 animateChildToPosition(child, c.x, c.y, REORDER_ANIMATION_DURATION, 0,
2020 DESTRUCTIVE_REORDER, false);
Adam Cohen8baab352012-03-20 17:39:21 -07002021 markCellsForView(c.x, c.y, c.spanX, c.spanY, occupied, true);
Adam Cohen482ed822012-03-02 14:15:13 -08002022 }
2023 }
2024 if (commitDragView) {
2025 markCellsForView(solution.dragViewX, solution.dragViewY, solution.dragViewSpanX,
2026 solution.dragViewSpanY, occupied, true);
2027 }
2028 }
2029
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002030
2031 // This method starts or changes the reorder preview animations
2032 private void beginOrAdjustReorderPreviewAnimations(ItemConfiguration solution,
2033 View dragView, int delay, int mode) {
Adam Cohen19f37922012-03-21 11:59:11 -07002034 int childCount = mShortcutsAndWidgets.getChildCount();
Adam Cohen19f37922012-03-21 11:59:11 -07002035 for (int i = 0; i < childCount; i++) {
2036 View child = mShortcutsAndWidgets.getChildAt(i);
2037 if (child == dragView) continue;
2038 CellAndSpan c = solution.map.get(child);
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002039 boolean skip = mode == ReorderPreviewAnimation.MODE_HINT && solution.intersectingViews
2040 != null && !solution.intersectingViews.contains(child);
2041
Adam Cohen19f37922012-03-21 11:59:11 -07002042 LayoutParams lp = (LayoutParams) child.getLayoutParams();
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002043 if (c != null && !skip) {
2044 ReorderPreviewAnimation rha = new ReorderPreviewAnimation(child, mode, lp.cellX,
2045 lp.cellY, c.x, c.y, c.spanX, c.spanY);
Adam Cohend024f982012-05-23 18:26:45 -07002046 rha.animate();
Adam Cohen19f37922012-03-21 11:59:11 -07002047 }
2048 }
2049 }
2050
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002051 // Class which represents the reorder preview animations. These animations show that an item is
Adam Cohen19f37922012-03-21 11:59:11 -07002052 // in a temporary state, and hint at where the item will return to.
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002053 class ReorderPreviewAnimation {
Adam Cohen19f37922012-03-21 11:59:11 -07002054 View child;
Adam Cohend024f982012-05-23 18:26:45 -07002055 float finalDeltaX;
2056 float finalDeltaY;
2057 float initDeltaX;
2058 float initDeltaY;
2059 float finalScale;
2060 float initScale;
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002061 int mode;
2062 boolean repeating = false;
2063 private static final int PREVIEW_DURATION = 300;
2064 private static final int HINT_DURATION = Workspace.REORDER_TIMEOUT;
2065
2066 public static final int MODE_HINT = 0;
2067 public static final int MODE_PREVIEW = 1;
2068
Adam Cohene7587d22012-05-24 18:50:02 -07002069 Animator a;
Adam Cohen19f37922012-03-21 11:59:11 -07002070
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002071 public ReorderPreviewAnimation(View child, int mode, int cellX0, int cellY0, int cellX1,
2072 int cellY1, int spanX, int spanY) {
Adam Cohen19f37922012-03-21 11:59:11 -07002073 regionToCenterPoint(cellX0, cellY0, spanX, spanY, mTmpPoint);
2074 final int x0 = mTmpPoint[0];
2075 final int y0 = mTmpPoint[1];
2076 regionToCenterPoint(cellX1, cellY1, spanX, spanY, mTmpPoint);
2077 final int x1 = mTmpPoint[0];
2078 final int y1 = mTmpPoint[1];
2079 final int dX = x1 - x0;
2080 final int dY = y1 - y0;
Adam Cohend024f982012-05-23 18:26:45 -07002081 finalDeltaX = 0;
2082 finalDeltaY = 0;
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002083 int dir = mode == MODE_HINT ? -1 : 1;
Adam Cohen19f37922012-03-21 11:59:11 -07002084 if (dX == dY && dX == 0) {
2085 } else {
2086 if (dY == 0) {
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002087 finalDeltaX = - dir * Math.signum(dX) * mReorderPreviewAnimationMagnitude;
Adam Cohen19f37922012-03-21 11:59:11 -07002088 } else if (dX == 0) {
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002089 finalDeltaY = - dir * Math.signum(dY) * mReorderPreviewAnimationMagnitude;
Adam Cohen19f37922012-03-21 11:59:11 -07002090 } else {
2091 double angle = Math.atan( (float) (dY) / dX);
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002092 finalDeltaX = (int) (- dir * Math.signum(dX) *
2093 Math.abs(Math.cos(angle) * mReorderPreviewAnimationMagnitude));
2094 finalDeltaY = (int) (- dir * Math.signum(dY) *
2095 Math.abs(Math.sin(angle) * mReorderPreviewAnimationMagnitude));
Adam Cohen19f37922012-03-21 11:59:11 -07002096 }
2097 }
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002098 this.mode = mode;
Adam Cohend024f982012-05-23 18:26:45 -07002099 initDeltaX = child.getTranslationX();
2100 initDeltaY = child.getTranslationY();
Adam Cohen307fe232012-08-16 17:55:58 -07002101 finalScale = getChildrenScale() - 4.0f / child.getWidth();
Adam Cohend024f982012-05-23 18:26:45 -07002102 initScale = child.getScaleX();
Adam Cohen19f37922012-03-21 11:59:11 -07002103 this.child = child;
2104 }
2105
Adam Cohend024f982012-05-23 18:26:45 -07002106 void animate() {
Adam Cohen19f37922012-03-21 11:59:11 -07002107 if (mShakeAnimators.containsKey(child)) {
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002108 ReorderPreviewAnimation oldAnimation = mShakeAnimators.get(child);
Adam Cohend024f982012-05-23 18:26:45 -07002109 oldAnimation.cancel();
Adam Cohen19f37922012-03-21 11:59:11 -07002110 mShakeAnimators.remove(child);
Adam Cohene7587d22012-05-24 18:50:02 -07002111 if (finalDeltaX == 0 && finalDeltaY == 0) {
2112 completeAnimationImmediately();
2113 return;
2114 }
Adam Cohen19f37922012-03-21 11:59:11 -07002115 }
Adam Cohend024f982012-05-23 18:26:45 -07002116 if (finalDeltaX == 0 && finalDeltaY == 0) {
Adam Cohen19f37922012-03-21 11:59:11 -07002117 return;
2118 }
Michael Jurkaf1ad6082013-03-13 12:55:46 +01002119 ValueAnimator va = LauncherAnimUtils.ofFloat(child, 0f, 1f);
Adam Cohene7587d22012-05-24 18:50:02 -07002120 a = va;
Adam Cohen19f37922012-03-21 11:59:11 -07002121 va.setRepeatMode(ValueAnimator.REVERSE);
2122 va.setRepeatCount(ValueAnimator.INFINITE);
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002123 va.setDuration(mode == MODE_HINT ? HINT_DURATION : PREVIEW_DURATION);
Adam Cohend024f982012-05-23 18:26:45 -07002124 va.setStartDelay((int) (Math.random() * 60));
Adam Cohen19f37922012-03-21 11:59:11 -07002125 va.addUpdateListener(new AnimatorUpdateListener() {
2126 @Override
2127 public void onAnimationUpdate(ValueAnimator animation) {
2128 float r = ((Float) animation.getAnimatedValue()).floatValue();
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002129 float r1 = (mode == MODE_HINT && repeating) ? 1.0f : r;
2130 float x = r1 * finalDeltaX + (1 - r1) * initDeltaX;
2131 float y = r1 * finalDeltaY + (1 - r1) * initDeltaY;
Adam Cohen19f37922012-03-21 11:59:11 -07002132 child.setTranslationX(x);
2133 child.setTranslationY(y);
Adam Cohend024f982012-05-23 18:26:45 -07002134 float s = r * finalScale + (1 - r) * initScale;
Brandon Keely50e6e562012-05-08 16:28:49 -07002135 child.setScaleX(s);
2136 child.setScaleY(s);
Adam Cohen19f37922012-03-21 11:59:11 -07002137 }
2138 });
2139 va.addListener(new AnimatorListenerAdapter() {
2140 public void onAnimationRepeat(Animator animation) {
Adam Cohen19f37922012-03-21 11:59:11 -07002141 // We make sure to end only after a full period
Adam Cohend024f982012-05-23 18:26:45 -07002142 initDeltaX = 0;
2143 initDeltaY = 0;
Adam Cohen307fe232012-08-16 17:55:58 -07002144 initScale = getChildrenScale();
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002145 repeating = true;
Adam Cohen19f37922012-03-21 11:59:11 -07002146 }
2147 });
Adam Cohen19f37922012-03-21 11:59:11 -07002148 mShakeAnimators.put(child, this);
2149 va.start();
2150 }
2151
Adam Cohend024f982012-05-23 18:26:45 -07002152 private void cancel() {
Adam Cohene7587d22012-05-24 18:50:02 -07002153 if (a != null) {
2154 a.cancel();
2155 }
Adam Cohen19f37922012-03-21 11:59:11 -07002156 }
Adam Cohene7587d22012-05-24 18:50:02 -07002157
Adam Cohen091440a2015-03-18 14:16:05 -07002158 @Thunk void completeAnimationImmediately() {
Adam Cohene7587d22012-05-24 18:50:02 -07002159 if (a != null) {
2160 a.cancel();
2161 }
Brandon Keely50e6e562012-05-08 16:28:49 -07002162
Sunny Goyal5d2fc322015-07-06 22:52:49 -07002163 a = new LauncherViewPropertyAnimator(child)
2164 .scaleX(getChildrenScale())
2165 .scaleY(getChildrenScale())
2166 .translationX(0)
2167 .translationY(0)
2168 .setDuration(REORDER_ANIMATION_DURATION);
2169 a.setInterpolator(new android.view.animation.DecelerateInterpolator(1.5f));
2170 a.start();
Brandon Keely50e6e562012-05-08 16:28:49 -07002171 }
Adam Cohen19f37922012-03-21 11:59:11 -07002172 }
2173
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002174 private void completeAndClearReorderPreviewAnimations() {
2175 for (ReorderPreviewAnimation a: mShakeAnimators.values()) {
Brandon Keely50e6e562012-05-08 16:28:49 -07002176 a.completeAnimationImmediately();
Adam Cohen19f37922012-03-21 11:59:11 -07002177 }
2178 mShakeAnimators.clear();
2179 }
2180
Adam Cohen482ed822012-03-02 14:15:13 -08002181 private void commitTempPlacement() {
2182 for (int i = 0; i < mCountX; i++) {
2183 for (int j = 0; j < mCountY; j++) {
2184 mOccupied[i][j] = mTmpOccupied[i][j];
2185 }
2186 }
Sunny Goyalaa8ef112015-06-12 20:04:41 -07002187
2188 long screenId = mLauncher.getWorkspace().getIdForScreen(this);
2189 int container = Favorites.CONTAINER_DESKTOP;
2190
2191 if (mLauncher.isHotseatLayout(this)) {
2192 screenId = -1;
2193 container = Favorites.CONTAINER_HOTSEAT;
2194 }
2195
Michael Jurkaa52570f2012-03-20 03:18:20 -07002196 int childCount = mShortcutsAndWidgets.getChildCount();
Adam Cohen482ed822012-03-02 14:15:13 -08002197 for (int i = 0; i < childCount; i++) {
Adam Cohenea889a22012-03-27 16:45:39 -07002198 View child = mShortcutsAndWidgets.getChildAt(i);
2199 LayoutParams lp = (LayoutParams) child.getLayoutParams();
2200 ItemInfo info = (ItemInfo) child.getTag();
Adam Cohen2acce882012-03-28 19:03:19 -07002201 // We do a null check here because the item info can be null in the case of the
2202 // AllApps button in the hotseat.
2203 if (info != null) {
Sunny Goyalaa8ef112015-06-12 20:04:41 -07002204 final boolean requiresDbUpdate = (info.cellX != lp.tmpCellX
2205 || info.cellY != lp.tmpCellY || info.spanX != lp.cellHSpan
2206 || info.spanY != lp.cellVSpan);
2207
Adam Cohen2acce882012-03-28 19:03:19 -07002208 info.cellX = lp.cellX = lp.tmpCellX;
2209 info.cellY = lp.cellY = lp.tmpCellY;
Adam Cohenbebf0422012-04-11 18:06:28 -07002210 info.spanX = lp.cellHSpan;
2211 info.spanY = lp.cellVSpan;
Sunny Goyalaa8ef112015-06-12 20:04:41 -07002212
2213 if (requiresDbUpdate) {
2214 LauncherModel.modifyItemInDatabase(mLauncher, info, container, screenId,
2215 info.cellX, info.cellY, info.spanX, info.spanY);
2216 }
Adam Cohen2acce882012-03-28 19:03:19 -07002217 }
Adam Cohen482ed822012-03-02 14:15:13 -08002218 }
2219 }
2220
Sunny Goyalf7a29e82015-04-24 15:20:43 -07002221 private void setUseTempCoords(boolean useTempCoords) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07002222 int childCount = mShortcutsAndWidgets.getChildCount();
Adam Cohen482ed822012-03-02 14:15:13 -08002223 for (int i = 0; i < childCount; i++) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07002224 LayoutParams lp = (LayoutParams) mShortcutsAndWidgets.getChildAt(i).getLayoutParams();
Adam Cohen482ed822012-03-02 14:15:13 -08002225 lp.useTmpCoords = useTempCoords;
2226 }
2227 }
2228
Sunny Goyalf7a29e82015-04-24 15:20:43 -07002229 private ItemConfiguration findConfigurationNoShuffle(int pixelX, int pixelY, int minSpanX, int minSpanY,
Adam Cohen482ed822012-03-02 14:15:13 -08002230 int spanX, int spanY, View dragView, ItemConfiguration solution) {
2231 int[] result = new int[2];
2232 int[] resultSpan = new int[2];
Sunny Goyalf7a29e82015-04-24 15:20:43 -07002233 findNearestVacantArea(pixelX, pixelY, minSpanX, minSpanY, spanX, spanY, result,
Adam Cohen482ed822012-03-02 14:15:13 -08002234 resultSpan);
2235 if (result[0] >= 0 && result[1] >= 0) {
2236 copyCurrentStateToSolution(solution, false);
2237 solution.dragViewX = result[0];
2238 solution.dragViewY = result[1];
2239 solution.dragViewSpanX = resultSpan[0];
2240 solution.dragViewSpanY = resultSpan[1];
2241 solution.isSolution = true;
2242 } else {
2243 solution.isSolution = false;
2244 }
2245 return solution;
2246 }
2247
2248 public void prepareChildForDrag(View child) {
2249 markCellsAsUnoccupiedForView(child);
Adam Cohen482ed822012-03-02 14:15:13 -08002250 }
2251
Adam Cohen19f37922012-03-21 11:59:11 -07002252 /* This seems like it should be obvious and straight-forward, but when the direction vector
2253 needs to match with the notion of the dragView pushing other views, we have to employ
2254 a slightly more subtle notion of the direction vector. The question is what two points is
2255 the vector between? The center of the dragView and its desired destination? Not quite, as
2256 this doesn't necessarily coincide with the interaction of the dragView and items occupying
2257 those cells. Instead we use some heuristics to often lock the vector to up, down, left
2258 or right, which helps make pushing feel right.
2259 */
2260 private void getDirectionVectorForDrop(int dragViewCenterX, int dragViewCenterY, int spanX,
2261 int spanY, View dragView, int[] resultDirection) {
2262 int[] targetDestination = new int[2];
2263
2264 findNearestArea(dragViewCenterX, dragViewCenterY, spanX, spanY, targetDestination);
2265 Rect dragRect = new Rect();
2266 regionToRect(targetDestination[0], targetDestination[1], spanX, spanY, dragRect);
2267 dragRect.offset(dragViewCenterX - dragRect.centerX(), dragViewCenterY - dragRect.centerY());
2268
2269 Rect dropRegionRect = new Rect();
2270 getViewsIntersectingRegion(targetDestination[0], targetDestination[1], spanX, spanY,
2271 dragView, dropRegionRect, mIntersectingViews);
2272
2273 int dropRegionSpanX = dropRegionRect.width();
2274 int dropRegionSpanY = dropRegionRect.height();
2275
2276 regionToRect(dropRegionRect.left, dropRegionRect.top, dropRegionRect.width(),
2277 dropRegionRect.height(), dropRegionRect);
2278
2279 int deltaX = (dropRegionRect.centerX() - dragViewCenterX) / spanX;
2280 int deltaY = (dropRegionRect.centerY() - dragViewCenterY) / spanY;
2281
2282 if (dropRegionSpanX == mCountX || spanX == mCountX) {
2283 deltaX = 0;
2284 }
2285 if (dropRegionSpanY == mCountY || spanY == mCountY) {
2286 deltaY = 0;
2287 }
2288
2289 if (deltaX == 0 && deltaY == 0) {
2290 // No idea what to do, give a random direction.
2291 resultDirection[0] = 1;
2292 resultDirection[1] = 0;
2293 } else {
2294 computeDirectionVector(deltaX, deltaY, resultDirection);
2295 }
2296 }
2297
2298 // For a given cell and span, fetch the set of views intersecting the region.
2299 private void getViewsIntersectingRegion(int cellX, int cellY, int spanX, int spanY,
2300 View dragView, Rect boundingRect, ArrayList<View> intersectingViews) {
2301 if (boundingRect != null) {
2302 boundingRect.set(cellX, cellY, cellX + spanX, cellY + spanY);
2303 }
2304 intersectingViews.clear();
2305 Rect r0 = new Rect(cellX, cellY, cellX + spanX, cellY + spanY);
2306 Rect r1 = new Rect();
2307 final int count = mShortcutsAndWidgets.getChildCount();
2308 for (int i = 0; i < count; i++) {
2309 View child = mShortcutsAndWidgets.getChildAt(i);
2310 if (child == dragView) continue;
2311 LayoutParams lp = (LayoutParams) child.getLayoutParams();
2312 r1.set(lp.cellX, lp.cellY, lp.cellX + lp.cellHSpan, lp.cellY + lp.cellVSpan);
2313 if (Rect.intersects(r0, r1)) {
2314 mIntersectingViews.add(child);
2315 if (boundingRect != null) {
2316 boundingRect.union(r1);
2317 }
2318 }
2319 }
2320 }
2321
2322 boolean isNearestDropLocationOccupied(int pixelX, int pixelY, int spanX, int spanY,
2323 View dragView, int[] result) {
2324 result = findNearestArea(pixelX, pixelY, spanX, spanY, result);
2325 getViewsIntersectingRegion(result[0], result[1], spanX, spanY, dragView, null,
2326 mIntersectingViews);
2327 return !mIntersectingViews.isEmpty();
2328 }
2329
2330 void revertTempState() {
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002331 completeAndClearReorderPreviewAnimations();
2332 if (isItemPlacementDirty() && !DESTRUCTIVE_REORDER) {
2333 final int count = mShortcutsAndWidgets.getChildCount();
2334 for (int i = 0; i < count; i++) {
2335 View child = mShortcutsAndWidgets.getChildAt(i);
2336 LayoutParams lp = (LayoutParams) child.getLayoutParams();
2337 if (lp.tmpCellX != lp.cellX || lp.tmpCellY != lp.cellY) {
2338 lp.tmpCellX = lp.cellX;
2339 lp.tmpCellY = lp.cellY;
2340 animateChildToPosition(child, lp.cellX, lp.cellY, REORDER_ANIMATION_DURATION,
2341 0, false, false);
2342 }
Adam Cohen19f37922012-03-21 11:59:11 -07002343 }
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002344 setItemPlacementDirty(false);
Adam Cohen19f37922012-03-21 11:59:11 -07002345 }
Adam Cohen19f37922012-03-21 11:59:11 -07002346 }
2347
Adam Cohenbebf0422012-04-11 18:06:28 -07002348 boolean createAreaForResize(int cellX, int cellY, int spanX, int spanY,
2349 View dragView, int[] direction, boolean commit) {
2350 int[] pixelXY = new int[2];
2351 regionToCenterPoint(cellX, cellY, spanX, spanY, pixelXY);
2352
2353 // First we determine if things have moved enough to cause a different layout
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002354 ItemConfiguration swapSolution = findReorderSolution(pixelXY[0], pixelXY[1], spanX, spanY,
Adam Cohenbebf0422012-04-11 18:06:28 -07002355 spanX, spanY, direction, dragView, true, new ItemConfiguration());
2356
2357 setUseTempCoords(true);
2358 if (swapSolution != null && swapSolution.isSolution) {
2359 // If we're just testing for a possible location (MODE_ACCEPT_DROP), we don't bother
2360 // committing anything or animating anything as we just want to determine if a solution
2361 // exists
2362 copySolutionToTempState(swapSolution, dragView);
2363 setItemPlacementDirty(true);
2364 animateItemsToSolution(swapSolution, dragView, commit);
2365
2366 if (commit) {
2367 commitTempPlacement();
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002368 completeAndClearReorderPreviewAnimations();
Adam Cohenbebf0422012-04-11 18:06:28 -07002369 setItemPlacementDirty(false);
2370 } else {
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002371 beginOrAdjustReorderPreviewAnimations(swapSolution, dragView,
2372 REORDER_ANIMATION_DURATION, ReorderPreviewAnimation.MODE_PREVIEW);
Adam Cohenbebf0422012-04-11 18:06:28 -07002373 }
2374 mShortcutsAndWidgets.requestLayout();
2375 }
2376 return swapSolution.isSolution;
2377 }
2378
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002379 int[] performReorder(int pixelX, int pixelY, int minSpanX, int minSpanY, int spanX, int spanY,
Adam Cohen482ed822012-03-02 14:15:13 -08002380 View dragView, int[] result, int resultSpan[], int mode) {
Adam Cohen482ed822012-03-02 14:15:13 -08002381 // First we determine if things have moved enough to cause a different layout
Adam Cohen47a876d2012-03-19 13:21:41 -07002382 result = findNearestArea(pixelX, pixelY, spanX, spanY, result);
Adam Cohen482ed822012-03-02 14:15:13 -08002383
2384 if (resultSpan == null) {
2385 resultSpan = new int[2];
2386 }
2387
Adam Cohen19f37922012-03-21 11:59:11 -07002388 // When we are checking drop validity or actually dropping, we don't recompute the
2389 // direction vector, since we want the solution to match the preview, and it's possible
2390 // that the exact position of the item has changed to result in a new reordering outcome.
Adam Cohenb209e632012-03-27 17:09:36 -07002391 if ((mode == MODE_ON_DROP || mode == MODE_ON_DROP_EXTERNAL || mode == MODE_ACCEPT_DROP)
2392 && mPreviousReorderDirection[0] != INVALID_DIRECTION) {
Adam Cohen19f37922012-03-21 11:59:11 -07002393 mDirectionVector[0] = mPreviousReorderDirection[0];
2394 mDirectionVector[1] = mPreviousReorderDirection[1];
2395 // We reset this vector after drop
Adam Cohenb209e632012-03-27 17:09:36 -07002396 if (mode == MODE_ON_DROP || mode == MODE_ON_DROP_EXTERNAL) {
2397 mPreviousReorderDirection[0] = INVALID_DIRECTION;
2398 mPreviousReorderDirection[1] = INVALID_DIRECTION;
Adam Cohen19f37922012-03-21 11:59:11 -07002399 }
2400 } else {
2401 getDirectionVectorForDrop(pixelX, pixelY, spanX, spanY, dragView, mDirectionVector);
2402 mPreviousReorderDirection[0] = mDirectionVector[0];
2403 mPreviousReorderDirection[1] = mDirectionVector[1];
2404 }
2405
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002406 // Find a solution involving pushing / displacing any items in the way
2407 ItemConfiguration swapSolution = findReorderSolution(pixelX, pixelY, minSpanX, minSpanY,
Adam Cohen482ed822012-03-02 14:15:13 -08002408 spanX, spanY, mDirectionVector, dragView, true, new ItemConfiguration());
2409
2410 // We attempt the approach which doesn't shuffle views at all
2411 ItemConfiguration noShuffleSolution = findConfigurationNoShuffle(pixelX, pixelY, minSpanX,
2412 minSpanY, spanX, spanY, dragView, new ItemConfiguration());
2413
2414 ItemConfiguration finalSolution = null;
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002415
2416 // If the reorder solution requires resizing (shrinking) the item being dropped, we instead
2417 // favor a solution in which the item is not resized, but
Adam Cohen482ed822012-03-02 14:15:13 -08002418 if (swapSolution.isSolution && swapSolution.area() >= noShuffleSolution.area()) {
2419 finalSolution = swapSolution;
2420 } else if (noShuffleSolution.isSolution) {
2421 finalSolution = noShuffleSolution;
2422 }
2423
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002424 if (mode == MODE_SHOW_REORDER_HINT) {
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002425 if (finalSolution != null) {
Adam Cohenfe692872013-12-11 14:47:23 -08002426 beginOrAdjustReorderPreviewAnimations(finalSolution, dragView, 0,
2427 ReorderPreviewAnimation.MODE_HINT);
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002428 result[0] = finalSolution.dragViewX;
2429 result[1] = finalSolution.dragViewY;
2430 resultSpan[0] = finalSolution.dragViewSpanX;
2431 resultSpan[1] = finalSolution.dragViewSpanY;
2432 } else {
2433 result[0] = result[1] = resultSpan[0] = resultSpan[1] = -1;
2434 }
2435 return result;
2436 }
2437
Adam Cohen482ed822012-03-02 14:15:13 -08002438 boolean foundSolution = true;
2439 if (!DESTRUCTIVE_REORDER) {
2440 setUseTempCoords(true);
2441 }
2442
2443 if (finalSolution != null) {
2444 result[0] = finalSolution.dragViewX;
2445 result[1] = finalSolution.dragViewY;
2446 resultSpan[0] = finalSolution.dragViewSpanX;
2447 resultSpan[1] = finalSolution.dragViewSpanY;
2448
2449 // If we're just testing for a possible location (MODE_ACCEPT_DROP), we don't bother
2450 // committing anything or animating anything as we just want to determine if a solution
2451 // exists
2452 if (mode == MODE_DRAG_OVER || mode == MODE_ON_DROP || mode == MODE_ON_DROP_EXTERNAL) {
2453 if (!DESTRUCTIVE_REORDER) {
2454 copySolutionToTempState(finalSolution, dragView);
2455 }
2456 setItemPlacementDirty(true);
2457 animateItemsToSolution(finalSolution, dragView, mode == MODE_ON_DROP);
2458
Adam Cohen19f37922012-03-21 11:59:11 -07002459 if (!DESTRUCTIVE_REORDER &&
2460 (mode == MODE_ON_DROP || mode == MODE_ON_DROP_EXTERNAL)) {
Adam Cohen482ed822012-03-02 14:15:13 -08002461 commitTempPlacement();
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002462 completeAndClearReorderPreviewAnimations();
Adam Cohen19f37922012-03-21 11:59:11 -07002463 setItemPlacementDirty(false);
2464 } else {
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002465 beginOrAdjustReorderPreviewAnimations(finalSolution, dragView,
2466 REORDER_ANIMATION_DURATION, ReorderPreviewAnimation.MODE_PREVIEW);
Adam Cohen482ed822012-03-02 14:15:13 -08002467 }
2468 }
2469 } else {
2470 foundSolution = false;
2471 result[0] = result[1] = resultSpan[0] = resultSpan[1] = -1;
2472 }
2473
2474 if ((mode == MODE_ON_DROP || !foundSolution) && !DESTRUCTIVE_REORDER) {
2475 setUseTempCoords(false);
2476 }
Adam Cohen482ed822012-03-02 14:15:13 -08002477
Michael Jurkaa52570f2012-03-20 03:18:20 -07002478 mShortcutsAndWidgets.requestLayout();
Adam Cohen482ed822012-03-02 14:15:13 -08002479 return result;
2480 }
2481
Adam Cohen19f37922012-03-21 11:59:11 -07002482 void setItemPlacementDirty(boolean dirty) {
2483 mItemPlacementDirty = dirty;
Adam Cohen482ed822012-03-02 14:15:13 -08002484 }
Adam Cohen19f37922012-03-21 11:59:11 -07002485 boolean isItemPlacementDirty() {
2486 return mItemPlacementDirty;
Adam Cohen482ed822012-03-02 14:15:13 -08002487 }
2488
Adam Cohen091440a2015-03-18 14:16:05 -07002489 @Thunk class ItemConfiguration {
Adam Cohen8baab352012-03-20 17:39:21 -07002490 HashMap<View, CellAndSpan> map = new HashMap<View, CellAndSpan>();
Adam Cohenf3900c22012-11-16 18:28:11 -08002491 private HashMap<View, CellAndSpan> savedMap = new HashMap<View, CellAndSpan>();
2492 ArrayList<View> sortedViews = new ArrayList<View>();
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002493 ArrayList<View> intersectingViews;
Adam Cohen482ed822012-03-02 14:15:13 -08002494 boolean isSolution = false;
2495 int dragViewX, dragViewY, dragViewSpanX, dragViewSpanY;
2496
Adam Cohenf3900c22012-11-16 18:28:11 -08002497 void save() {
2498 // Copy current state into savedMap
2499 for (View v: map.keySet()) {
2500 map.get(v).copy(savedMap.get(v));
2501 }
2502 }
2503
2504 void restore() {
2505 // Restore current state from savedMap
2506 for (View v: savedMap.keySet()) {
2507 savedMap.get(v).copy(map.get(v));
2508 }
2509 }
2510
2511 void add(View v, CellAndSpan cs) {
2512 map.put(v, cs);
2513 savedMap.put(v, new CellAndSpan());
2514 sortedViews.add(v);
2515 }
2516
Adam Cohen482ed822012-03-02 14:15:13 -08002517 int area() {
2518 return dragViewSpanX * dragViewSpanY;
2519 }
Adam Cohen8baab352012-03-20 17:39:21 -07002520 }
2521
2522 private class CellAndSpan {
2523 int x, y;
2524 int spanX, spanY;
2525
Adam Cohenf3900c22012-11-16 18:28:11 -08002526 public CellAndSpan() {
2527 }
2528
2529 public void copy(CellAndSpan copy) {
2530 copy.x = x;
2531 copy.y = y;
2532 copy.spanX = spanX;
2533 copy.spanY = spanY;
2534 }
2535
Adam Cohen8baab352012-03-20 17:39:21 -07002536 public CellAndSpan(int x, int y, int spanX, int spanY) {
2537 this.x = x;
2538 this.y = y;
2539 this.spanX = spanX;
2540 this.spanY = spanY;
Adam Cohen482ed822012-03-02 14:15:13 -08002541 }
Adam Cohenf3900c22012-11-16 18:28:11 -08002542
2543 public String toString() {
2544 return "(" + x + ", " + y + ": " + spanX + ", " + spanY + ")";
2545 }
2546
Adam Cohen482ed822012-03-02 14:15:13 -08002547 }
2548
Adam Cohendf035382011-04-11 17:22:04 -07002549 /**
Adam Cohendf035382011-04-11 17:22:04 -07002550 * Find a starting cell position that will fit the given bounds nearest the requested
2551 * cell location. Uses Euclidean distance to score multiple vacant areas.
2552 *
2553 * @param pixelX The X location at which you want to search for a vacant area.
2554 * @param pixelY The Y location at which you want to search for a vacant area.
2555 * @param spanX Horizontal span of the object.
2556 * @param spanY Vertical span of the object.
2557 * @param ignoreView Considers space occupied by this view as unoccupied
2558 * @param result Previously returned value to possibly recycle.
2559 * @return The X, Y cell of a vacant area that can contain this object,
2560 * nearest the requested location.
2561 */
Sunny Goyalf7a29e82015-04-24 15:20:43 -07002562 int[] findNearestArea(int pixelX, int pixelY, int spanX, int spanY, int[] result) {
2563 return findNearestArea(pixelX, pixelY, spanX, spanY, spanX, spanY, false, result, null);
Adam Cohendf035382011-04-11 17:22:04 -07002564 }
2565
Michael Jurka0280c3b2010-09-17 15:00:07 -07002566 boolean existsEmptyCell() {
2567 return findCellForSpan(null, 1, 1);
2568 }
2569
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002570 /**
Michael Jurka0280c3b2010-09-17 15:00:07 -07002571 * Finds the upper-left coordinate of the first rectangle in the grid that can
2572 * hold a cell of the specified dimensions. If intersectX and intersectY are not -1,
2573 * then this method will only return coordinates for rectangles that contain the cell
2574 * (intersectX, intersectY)
2575 *
2576 * @param cellXY The array that will contain the position of a vacant cell if such a cell
2577 * can be found.
2578 * @param spanX The horizontal span of the cell we want to find.
2579 * @param spanY The vertical span of the cell we want to find.
2580 *
2581 * @return True if a vacant cell of the specified dimension was found, false otherwise.
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07002582 */
Hyunyoung Song3f471442015-04-08 19:01:34 -07002583 public boolean findCellForSpan(int[] cellXY, int spanX, int spanY) {
Michael Jurka28750fb2010-09-24 17:43:49 -07002584 boolean foundCell = false;
Sunny Goyalf7a29e82015-04-24 15:20:43 -07002585 final int endX = mCountX - (spanX - 1);
2586 final int endY = mCountY - (spanY - 1);
Michael Jurka0280c3b2010-09-17 15:00:07 -07002587
Sunny Goyalf7a29e82015-04-24 15:20:43 -07002588 for (int y = 0; y < endY && !foundCell; y++) {
2589 inner:
2590 for (int x = 0; x < endX; x++) {
2591 for (int i = 0; i < spanX; i++) {
2592 for (int j = 0; j < spanY; j++) {
2593 if (mOccupied[x + i][y + j]) {
2594 // small optimization: we can skip to after the column we just found
2595 // an occupied cell
2596 x += i;
2597 continue inner;
Michael Jurka0280c3b2010-09-17 15:00:07 -07002598 }
2599 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07002600 }
Sunny Goyalf7a29e82015-04-24 15:20:43 -07002601 if (cellXY != null) {
2602 cellXY[0] = x;
2603 cellXY[1] = y;
2604 }
2605 foundCell = true;
Michael Jurka0280c3b2010-09-17 15:00:07 -07002606 break;
Michael Jurka0280c3b2010-09-17 15:00:07 -07002607 }
2608 }
2609
Michael Jurka28750fb2010-09-24 17:43:49 -07002610 return foundCell;
Michael Jurka0280c3b2010-09-17 15:00:07 -07002611 }
2612
2613 /**
Winson Chungc07918d2011-07-01 15:35:26 -07002614 * A drag event has begun over this layout.
2615 * It may have begun over this layout (in which case onDragChild is called first),
2616 * or it may have begun on another layout.
2617 */
2618 void onDragEnter() {
Winson Chungc07918d2011-07-01 15:35:26 -07002619 mDragging = true;
2620 }
2621
2622 /**
Michael Jurka0280c3b2010-09-17 15:00:07 -07002623 * Called when drag has left this CellLayout or has been completed (successfully or not)
2624 */
2625 void onDragExit() {
Joe Onorato4be866d2010-10-10 11:26:02 -07002626 // This can actually be called when we aren't in a drag, e.g. when adding a new
2627 // item to this layout via the customize drawer.
2628 // Guard against that case.
2629 if (mDragging) {
2630 mDragging = false;
Patrick Dubroyde7658b2010-09-27 11:15:43 -07002631 }
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -07002632
2633 // Invalidate the drag data
Adam Cohend41fbf52012-02-16 23:53:59 -08002634 mDragCell[0] = mDragCell[1] = -1;
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -07002635 mDragOutlineAnims[mDragOutlineCurrent].animateOut();
2636 mDragOutlineCurrent = (mDragOutlineCurrent + 1) % mDragOutlineAnims.length;
Adam Cohen19f37922012-03-21 11:59:11 -07002637 revertTempState();
Michael Jurka33945b22010-12-21 18:19:38 -08002638 setIsDragOverlapping(false);
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07002639 }
2640
2641 /**
Winson Chungaafa03c2010-06-11 17:34:16 -07002642 * Mark a child as having been dropped.
Patrick Dubroyde7658b2010-09-27 11:15:43 -07002643 * At the beginning of the drag operation, the child may have been on another
Patrick Dubroyce34a972010-10-19 10:34:32 -07002644 * screen, but it is re-parented before this method is called.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002645 *
2646 * @param child The child that is being dropped
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002647 */
Adam Cohen716b51e2011-06-30 12:09:54 -07002648 void onDropChild(View child) {
Romain Guyd94533d2009-08-17 10:01:15 -07002649 if (child != null) {
2650 LayoutParams lp = (LayoutParams) child.getLayoutParams();
Romain Guy84f296c2009-11-04 15:00:44 -08002651 lp.dropped = true;
Romain Guyd94533d2009-08-17 10:01:15 -07002652 child.requestLayout();
Romain Guyd94533d2009-08-17 10:01:15 -07002653 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002654 }
2655
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002656 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002657 * Computes a bounding rectangle for a range of cells
Winson Chungaafa03c2010-06-11 17:34:16 -07002658 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002659 * @param cellX X coordinate of upper left corner expressed as a cell position
2660 * @param cellY Y coordinate of upper left corner expressed as a cell position
Winson Chungaafa03c2010-06-11 17:34:16 -07002661 * @param cellHSpan Width in cells
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002662 * @param cellVSpan Height in cells
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07002663 * @param resultRect Rect into which to put the results
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002664 */
Adam Cohend41fbf52012-02-16 23:53:59 -08002665 public void cellToRect(int cellX, int cellY, int cellHSpan, int cellVSpan, Rect resultRect) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002666 final int cellWidth = mCellWidth;
2667 final int cellHeight = mCellHeight;
2668 final int widthGap = mWidthGap;
2669 final int heightGap = mHeightGap;
Winson Chungaafa03c2010-06-11 17:34:16 -07002670
Winson Chung4b825dcd2011-06-19 12:41:22 -07002671 final int hStartPadding = getPaddingLeft();
2672 final int vStartPadding = getPaddingTop();
Winson Chungaafa03c2010-06-11 17:34:16 -07002673
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002674 int width = cellHSpan * cellWidth + ((cellHSpan - 1) * widthGap);
2675 int height = cellVSpan * cellHeight + ((cellVSpan - 1) * heightGap);
2676
2677 int x = hStartPadding + cellX * (cellWidth + widthGap);
2678 int y = vStartPadding + cellY * (cellHeight + heightGap);
Winson Chungaafa03c2010-06-11 17:34:16 -07002679
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07002680 resultRect.set(x, y, x + width, y + height);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002681 }
Winson Chungaafa03c2010-06-11 17:34:16 -07002682
Michael Jurka0280c3b2010-09-17 15:00:07 -07002683 private void clearOccupiedCells() {
2684 for (int x = 0; x < mCountX; x++) {
2685 for (int y = 0; y < mCountY; y++) {
2686 mOccupied[x][y] = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002687 }
2688 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07002689 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002690
Adam Cohend4844c32011-02-18 19:25:06 -08002691 public void markCellsAsOccupiedForView(View view) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07002692 if (view == null || view.getParent() != mShortcutsAndWidgets) return;
Michael Jurka0280c3b2010-09-17 15:00:07 -07002693 LayoutParams lp = (LayoutParams) view.getLayoutParams();
Sunny Goyalf7a29e82015-04-24 15:20:43 -07002694 markCellsForView(lp.cellX, lp.cellY, lp.cellHSpan, lp.cellVSpan, mOccupied, true);
Michael Jurka0280c3b2010-09-17 15:00:07 -07002695 }
2696
Adam Cohend4844c32011-02-18 19:25:06 -08002697 public void markCellsAsUnoccupiedForView(View view) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07002698 if (view == null || view.getParent() != mShortcutsAndWidgets) return;
Michael Jurka0280c3b2010-09-17 15:00:07 -07002699 LayoutParams lp = (LayoutParams) view.getLayoutParams();
Sunny Goyalf7a29e82015-04-24 15:20:43 -07002700 markCellsForView(lp.cellX, lp.cellY, lp.cellHSpan, lp.cellVSpan, mOccupied, false);
Michael Jurka0280c3b2010-09-17 15:00:07 -07002701 }
2702
Adam Cohen482ed822012-03-02 14:15:13 -08002703 private void markCellsForView(int cellX, int cellY, int spanX, int spanY, boolean[][] occupied,
2704 boolean value) {
2705 if (cellX < 0 || cellY < 0) return;
Michael Jurka0280c3b2010-09-17 15:00:07 -07002706 for (int x = cellX; x < cellX + spanX && x < mCountX; x++) {
2707 for (int y = cellY; y < cellY + spanY && y < mCountY; y++) {
Adam Cohen482ed822012-03-02 14:15:13 -08002708 occupied[x][y] = value;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002709 }
2710 }
2711 }
2712
Adam Cohen2801caf2011-05-13 20:57:39 -07002713 public int getDesiredWidth() {
Michael Jurka8b805b12012-04-18 14:23:14 -07002714 return getPaddingLeft() + getPaddingRight() + (mCountX * mCellWidth) +
Adam Cohen2801caf2011-05-13 20:57:39 -07002715 (Math.max((mCountX - 1), 0) * mWidthGap);
2716 }
2717
2718 public int getDesiredHeight() {
Michael Jurka8b805b12012-04-18 14:23:14 -07002719 return getPaddingTop() + getPaddingBottom() + (mCountY * mCellHeight) +
Adam Cohen2801caf2011-05-13 20:57:39 -07002720 (Math.max((mCountY - 1), 0) * mHeightGap);
2721 }
2722
Michael Jurka66d72172011-04-12 16:29:25 -07002723 public boolean isOccupied(int x, int y) {
2724 if (x < mCountX && y < mCountY) {
2725 return mOccupied[x][y];
2726 } else {
2727 throw new RuntimeException("Position exceeds the bound of this CellLayout");
2728 }
2729 }
2730
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002731 @Override
2732 public ViewGroup.LayoutParams generateLayoutParams(AttributeSet attrs) {
2733 return new CellLayout.LayoutParams(getContext(), attrs);
2734 }
2735
2736 @Override
2737 protected boolean checkLayoutParams(ViewGroup.LayoutParams p) {
2738 return p instanceof CellLayout.LayoutParams;
2739 }
2740
2741 @Override
2742 protected ViewGroup.LayoutParams generateLayoutParams(ViewGroup.LayoutParams p) {
2743 return new CellLayout.LayoutParams(p);
2744 }
2745
2746 public static class LayoutParams extends ViewGroup.MarginLayoutParams {
2747 /**
2748 * Horizontal location of the item in the grid.
2749 */
2750 @ViewDebug.ExportedProperty
2751 public int cellX;
2752
2753 /**
2754 * Vertical location of the item in the grid.
2755 */
2756 @ViewDebug.ExportedProperty
2757 public int cellY;
2758
2759 /**
Adam Cohen482ed822012-03-02 14:15:13 -08002760 * Temporary horizontal location of the item in the grid during reorder
2761 */
2762 public int tmpCellX;
2763
2764 /**
2765 * Temporary vertical location of the item in the grid during reorder
2766 */
2767 public int tmpCellY;
2768
2769 /**
2770 * Indicates that the temporary coordinates should be used to layout the items
2771 */
2772 public boolean useTmpCoords;
2773
2774 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002775 * Number of cells spanned horizontally by the item.
2776 */
2777 @ViewDebug.ExportedProperty
2778 public int cellHSpan;
2779
2780 /**
2781 * Number of cells spanned vertically by the item.
2782 */
2783 @ViewDebug.ExportedProperty
2784 public int cellVSpan;
Winson Chungaafa03c2010-06-11 17:34:16 -07002785
Adam Cohen1b607ed2011-03-03 17:26:50 -08002786 /**
2787 * Indicates whether the item will set its x, y, width and height parameters freely,
2788 * or whether these will be computed based on cellX, cellY, cellHSpan and cellVSpan.
2789 */
Adam Cohend4844c32011-02-18 19:25:06 -08002790 public boolean isLockedToGrid = true;
2791
Adam Cohen482ed822012-03-02 14:15:13 -08002792 /**
Adam Cohenec40b2b2013-07-23 15:52:40 -07002793 * Indicates that this item should use the full extents of its parent.
2794 */
2795 public boolean isFullscreen = false;
2796
2797 /**
Adam Cohen482ed822012-03-02 14:15:13 -08002798 * Indicates whether this item can be reordered. Always true except in the case of the
2799 * the AllApps button.
2800 */
2801 public boolean canReorder = true;
2802
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002803 // X coordinate of the view in the layout.
2804 @ViewDebug.ExportedProperty
Vadim Tryshevfedca432015-08-19 17:55:02 -07002805 public int x;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002806 // Y coordinate of the view in the layout.
2807 @ViewDebug.ExportedProperty
Vadim Tryshevfedca432015-08-19 17:55:02 -07002808 public int y;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002809
Romain Guy84f296c2009-11-04 15:00:44 -08002810 boolean dropped;
Romain Guyfcb9e712009-10-02 16:06:52 -07002811
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002812 public LayoutParams(Context c, AttributeSet attrs) {
2813 super(c, attrs);
2814 cellHSpan = 1;
2815 cellVSpan = 1;
2816 }
2817
2818 public LayoutParams(ViewGroup.LayoutParams source) {
2819 super(source);
2820 cellHSpan = 1;
2821 cellVSpan = 1;
2822 }
Winson Chungaafa03c2010-06-11 17:34:16 -07002823
2824 public LayoutParams(LayoutParams source) {
2825 super(source);
2826 this.cellX = source.cellX;
2827 this.cellY = source.cellY;
2828 this.cellHSpan = source.cellHSpan;
2829 this.cellVSpan = source.cellVSpan;
2830 }
2831
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002832 public LayoutParams(int cellX, int cellY, int cellHSpan, int cellVSpan) {
Romain Guy8f19cdd2010-01-08 15:07:00 -08002833 super(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002834 this.cellX = cellX;
2835 this.cellY = cellY;
2836 this.cellHSpan = cellHSpan;
2837 this.cellVSpan = cellVSpan;
2838 }
2839
Adam Cohen2374abf2013-04-16 14:56:57 -07002840 public void setup(int cellWidth, int cellHeight, int widthGap, int heightGap,
2841 boolean invertHorizontally, int colCount) {
Adam Cohend4844c32011-02-18 19:25:06 -08002842 if (isLockedToGrid) {
2843 final int myCellHSpan = cellHSpan;
2844 final int myCellVSpan = cellVSpan;
Adam Cohen2374abf2013-04-16 14:56:57 -07002845 int myCellX = useTmpCoords ? tmpCellX : cellX;
2846 int myCellY = useTmpCoords ? tmpCellY : cellY;
2847
2848 if (invertHorizontally) {
2849 myCellX = colCount - myCellX - cellHSpan;
2850 }
Adam Cohen1b607ed2011-03-03 17:26:50 -08002851
Adam Cohend4844c32011-02-18 19:25:06 -08002852 width = myCellHSpan * cellWidth + ((myCellHSpan - 1) * widthGap) -
2853 leftMargin - rightMargin;
2854 height = myCellVSpan * cellHeight + ((myCellVSpan - 1) * heightGap) -
2855 topMargin - bottomMargin;
Winson Chungeecf02d2012-03-02 17:14:58 -08002856 x = (int) (myCellX * (cellWidth + widthGap) + leftMargin);
2857 y = (int) (myCellY * (cellHeight + heightGap) + topMargin);
Adam Cohend4844c32011-02-18 19:25:06 -08002858 }
2859 }
Winson Chungaafa03c2010-06-11 17:34:16 -07002860
Winson Chungaafa03c2010-06-11 17:34:16 -07002861 public String toString() {
2862 return "(" + this.cellX + ", " + this.cellY + ")";
2863 }
Adam Cohen7f4eabe2011-04-21 16:19:16 -07002864
2865 public void setWidth(int width) {
2866 this.width = width;
2867 }
2868
2869 public int getWidth() {
2870 return width;
2871 }
2872
2873 public void setHeight(int height) {
2874 this.height = height;
2875 }
2876
2877 public int getHeight() {
2878 return height;
2879 }
2880
2881 public void setX(int x) {
2882 this.x = x;
2883 }
2884
2885 public int getX() {
2886 return x;
2887 }
2888
2889 public void setY(int y) {
2890 this.y = y;
2891 }
2892
2893 public int getY() {
2894 return y;
2895 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002896 }
2897
Michael Jurka0280c3b2010-09-17 15:00:07 -07002898 // This class stores info for two purposes:
2899 // 1. When dragging items (mDragInfo in Workspace), we store the View, its cellX & cellY,
2900 // its spanX, spanY, and the screen it is on
2901 // 2. When long clicking on an empty cell in a CellLayout, we save information about the
2902 // cellX and cellY coordinates and which page was clicked. We then set this as a tag on
2903 // the CellLayout that was long clicked
Sunny Goyal83a8f042015-05-19 12:52:12 -07002904 public static final class CellInfo {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002905 View cell;
Michael Jurkaa63c4522010-08-19 13:52:27 -07002906 int cellX = -1;
2907 int cellY = -1;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002908 int spanX;
2909 int spanY;
Adam Cohendcd297f2013-06-18 13:13:40 -07002910 long screenId;
Winson Chung3d503fb2011-07-13 17:25:49 -07002911 long container;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002912
Sunny Goyal83a8f042015-05-19 12:52:12 -07002913 public CellInfo(View v, ItemInfo info) {
Adam Cohene0aaa0d2014-05-12 12:44:22 -07002914 cell = v;
2915 cellX = info.cellX;
2916 cellY = info.cellY;
2917 spanX = info.spanX;
2918 spanY = info.spanY;
2919 screenId = info.screenId;
2920 container = info.container;
2921 }
2922
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002923 @Override
2924 public String toString() {
Winson Chungaafa03c2010-06-11 17:34:16 -07002925 return "Cell[view=" + (cell == null ? "null" : cell.getClass())
2926 + ", x=" + cellX + ", y=" + cellY + "]";
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002927 }
2928 }
Michael Jurkad771c962011-08-09 15:00:48 -07002929
Sunny Goyala9116722015-04-29 13:55:58 -07002930 public boolean findVacantCell(int spanX, int spanY, int[] outXY) {
2931 return Utilities.findVacantCell(outXY, spanX, spanY, mCountX, mCountY, mOccupied);
2932 }
Sunny Goyal9ca9c132015-04-29 14:57:22 -07002933
2934 public boolean isRegionVacant(int x, int y, int spanX, int spanY) {
2935 int x2 = x + spanX - 1;
2936 int y2 = y + spanY - 1;
2937 if (x < 0 || y < 0 || x2 >= mCountX || y2 >= mCountY) {
2938 return false;
2939 }
2940 for (int i = x; i <= x2; i++) {
2941 for (int j = y; j <= y2; j++) {
2942 if (mOccupied[i][j]) {
2943 return false;
2944 }
2945 }
2946 }
2947
2948 return true;
2949 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002950}