blob: 84e2d49c233c0342e5bd2506e614fc0133a0edef [file] [log] [blame]
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001/*
2 * Copyright (C) 2008 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Daniel Sandler325dc232013-06-05 22:57:57 -040017package com.android.launcher3;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080018
Joe Onorato4be866d2010-10-10 11:26:02 -070019import android.animation.Animator;
Michael Jurka629758f2012-06-14 16:18:21 -070020import android.animation.AnimatorListenerAdapter;
Brandon Keely50e6e562012-05-08 16:28:49 -070021import android.animation.AnimatorSet;
Chet Haase00397b12010-10-07 11:13:10 -070022import android.animation.TimeInterpolator;
Patrick Dubroyde7658b2010-09-27 11:15:43 -070023import android.animation.ValueAnimator;
24import android.animation.ValueAnimator.AnimatorUpdateListener;
Adam Cohenc9735cf2015-01-23 16:11:55 -080025import android.annotation.TargetApi;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080026import android.content.Context;
Joe Onorato79e56262009-09-21 15:23:04 -040027import android.content.res.Resources;
Winson Chungaafa03c2010-06-11 17:34:16 -070028import android.content.res.TypedArray;
Joe Onorato4be866d2010-10-10 11:26:02 -070029import android.graphics.Bitmap;
Winson Chungaafa03c2010-06-11 17:34:16 -070030import android.graphics.Canvas;
Andrew Flynn0dca1ec2012-02-29 13:33:22 -080031import android.graphics.Color;
Joe Onorato4be866d2010-10-10 11:26:02 -070032import android.graphics.Paint;
Patrick Dubroyde7658b2010-09-27 11:15:43 -070033import android.graphics.Point;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080034import android.graphics.Rect;
Adam Cohen482ed822012-03-02 14:15:13 -080035import android.graphics.drawable.ColorDrawable;
Patrick Dubroy6569f2c2010-07-12 14:25:18 -070036import android.graphics.drawable.Drawable;
Sunny Goyal2805e632015-05-20 15:35:32 -070037import android.graphics.drawable.TransitionDrawable;
Adam Cohenc9735cf2015-01-23 16:11:55 -080038import android.os.Build;
Adam Cohen1462de32012-07-24 22:34:36 -070039import android.os.Parcelable;
Adam Cohenc9735cf2015-01-23 16:11:55 -080040import android.support.v4.view.ViewCompat;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080041import android.util.AttributeSet;
Joe Onorato4be866d2010-10-10 11:26:02 -070042import android.util.Log;
Adam Cohen1462de32012-07-24 22:34:36 -070043import android.util.SparseArray;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080044import android.view.MotionEvent;
45import android.view.View;
46import android.view.ViewDebug;
47import android.view.ViewGroup;
Adam Cohenc9735cf2015-01-23 16:11:55 -080048import android.view.accessibility.AccessibilityEvent;
Winson Chung150fbab2010-09-29 17:14:26 -070049import android.view.animation.DecelerateInterpolator;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080050
Sunny Goyal4b6eb262015-05-14 19:24:40 -070051import com.android.launcher3.BubbleTextView.BubbleTextShadowHandler;
Daniel Sandler325dc232013-06-05 22:57:57 -040052import com.android.launcher3.FolderIcon.FolderRingAnimator;
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;
Adam Cohen091440a2015-03-18 14:16:05 -070056import com.android.launcher3.util.Thunk;
Patrick Dubroy8e58e912010-10-14 13:21:48 -070057
Adam Cohen69ce2e52011-07-03 19:25:21 -070058import java.util.ArrayList;
Adam Cohenc0dcf592011-06-01 15:30:43 -070059import java.util.Arrays;
Adam Cohenf3900c22012-11-16 18:28:11 -080060import java.util.Collections;
61import java.util.Comparator;
Adam Cohenbfbfd262011-06-13 16:55:12 -070062import java.util.HashMap;
Adam Cohend41fbf52012-02-16 23:53:59 -080063import java.util.Stack;
Adam Cohenc0dcf592011-06-01 15:30:43 -070064
Sunny Goyal4b6eb262015-05-14 19:24:40 -070065public class CellLayout extends ViewGroup implements BubbleTextShadowHandler {
Sunny Goyale9b651e2015-04-24 11:44:51 -070066 public static final int WORKSPACE_ACCESSIBILITY_DRAG = 2;
67 public static final int FOLDER_ACCESSIBILITY_DRAG = 1;
68
Winson Chungaafa03c2010-06-11 17:34:16 -070069 static final String TAG = "CellLayout";
70
Adam Cohen2acce882012-03-28 19:03:19 -070071 private Launcher mLauncher;
Adam Cohen091440a2015-03-18 14:16:05 -070072 @Thunk int mCellWidth;
73 @Thunk int mCellHeight;
Winson Chung11a1a532013-09-13 11:14:45 -070074 private int mFixedCellWidth;
75 private int mFixedCellHeight;
Winson Chungaafa03c2010-06-11 17:34:16 -070076
Adam Cohen091440a2015-03-18 14:16:05 -070077 @Thunk int mCountX;
78 @Thunk int mCountY;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080079
Adam Cohen234c4cd2011-07-17 21:03:04 -070080 private int mOriginalWidthGap;
81 private int mOriginalHeightGap;
Adam Cohen091440a2015-03-18 14:16:05 -070082 @Thunk int mWidthGap;
83 @Thunk int mHeightGap;
Winson Chung4b825dcd2011-06-19 12:41:22 -070084 private int mMaxGap;
Adam Cohen917e3882013-10-31 15:03:35 -070085 private boolean mDropPending = false;
Adam Cohenc50438c2014-08-19 17:43:05 -070086 private boolean mIsDragTarget = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080087
Patrick Dubroyde7658b2010-09-27 11:15:43 -070088 // These are temporary variables to prevent having to allocate a new object just to
89 // return an (x, y) value from helper functions. Do NOT use them to maintain other state.
Adam Cohen091440a2015-03-18 14:16:05 -070090 @Thunk final int[] mTmpPoint = new int[2];
Sunny Goyal2805e632015-05-20 15:35:32 -070091 @Thunk final int[] mTempLocation = new int[2];
Patrick Dubroy6569f2c2010-07-12 14:25:18 -070092
The Android Open Source Project31dd5032009-03-03 19:32:27 -080093 boolean[][] mOccupied;
Adam Cohen482ed822012-03-02 14:15:13 -080094 boolean[][] mTmpOccupied;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080095
Michael Jurkadee05892010-07-27 10:01:56 -070096 private OnTouchListener mInterceptTouchListener;
Mady Melloref044dd2015-06-02 15:35:07 -070097 private StylusEventHelper mStylusEventHelper;
Michael Jurkadee05892010-07-27 10:01:56 -070098
Adam Cohen69ce2e52011-07-03 19:25:21 -070099 private ArrayList<FolderRingAnimator> mFolderOuterRings = new ArrayList<FolderRingAnimator>();
Adam Cohenc51934b2011-07-26 21:07:43 -0700100 private int[] mFolderLeaveBehindCell = {-1, -1};
Adam Cohen69ce2e52011-07-03 19:25:21 -0700101
Michael Jurka5f1c5092010-09-03 14:15:02 -0700102 private float mBackgroundAlpha;
Adam Cohenf34bab52010-09-30 14:11:56 -0700103
Sunny Goyal2805e632015-05-20 15:35:32 -0700104 private static final int BACKGROUND_ACTIVATE_DURATION = 120;
105 private final TransitionDrawable mBackground;
106
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700107 // These values allow a fixed measurement to be set on the CellLayout.
108 private int mFixedWidth = -1;
109 private int mFixedHeight = -1;
110
Michael Jurka33945b22010-12-21 18:19:38 -0800111 // If we're actively dragging something over this screen, mIsDragOverlapping is true
112 private boolean mIsDragOverlapping = false;
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700113
Winson Chung150fbab2010-09-29 17:14:26 -0700114 // These arrays are used to implement the drag visualization on x-large screens.
Joe Onorato4be866d2010-10-10 11:26:02 -0700115 // They are used as circular arrays, indexed by mDragOutlineCurrent.
Adam Cohen091440a2015-03-18 14:16:05 -0700116 @Thunk Rect[] mDragOutlines = new Rect[4];
117 @Thunk float[] mDragOutlineAlphas = new float[mDragOutlines.length];
Joe Onorato4be866d2010-10-10 11:26:02 -0700118 private InterruptibleInOutAnimator[] mDragOutlineAnims =
119 new InterruptibleInOutAnimator[mDragOutlines.length];
Winson Chung150fbab2010-09-29 17:14:26 -0700120
121 // Used as an index into the above 3 arrays; indicates which is the most current value.
Joe Onorato4be866d2010-10-10 11:26:02 -0700122 private int mDragOutlineCurrent = 0;
Patrick Dubroy8e58e912010-10-14 13:21:48 -0700123 private final Paint mDragOutlinePaint = new Paint();
Winson Chung150fbab2010-09-29 17:14:26 -0700124
Sunny Goyal4fe5a372015-05-14 19:55:10 -0700125 private final ClickShadowView mTouchFeedbackView;
Patrick Dubroy96864c32011-03-10 17:17:23 -0800126
Sunny Goyal316490e2015-06-02 09:38:28 -0700127 @Thunk HashMap<CellLayout.LayoutParams, Animator> mReorderAnimators = new HashMap<>();
128 @Thunk HashMap<View, ReorderPreviewAnimation> mShakeAnimators = new HashMap<>();
Adam Cohen19f37922012-03-21 11:59:11 -0700129
130 private boolean mItemPlacementDirty = false;
Adam Cohenbfbfd262011-06-13 16:55:12 -0700131
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700132 // When a drag operation is in progress, holds the nearest cell to the touch point
133 private final int[] mDragCell = new int[2];
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800134
Joe Onorato4be866d2010-10-10 11:26:02 -0700135 private boolean mDragging = false;
136
Patrick Dubroyce34a972010-10-19 10:34:32 -0700137 private TimeInterpolator mEaseOutInterpolator;
Michael Jurkaa52570f2012-03-20 03:18:20 -0700138 private ShortcutAndWidgetContainer mShortcutsAndWidgets;
Patrick Dubroyce34a972010-10-19 10:34:32 -0700139
Andrew Flynn0dca1ec2012-02-29 13:33:22 -0800140 private boolean mIsHotseat = false;
Adam Cohen307fe232012-08-16 17:55:58 -0700141 private float mHotseatScale = 1f;
Andrew Flynn0dca1ec2012-02-29 13:33:22 -0800142
Adam Cohenfa3c58f2013-12-06 16:10:55 -0800143 public static final int MODE_SHOW_REORDER_HINT = 0;
144 public static final int MODE_DRAG_OVER = 1;
145 public static final int MODE_ON_DROP = 2;
146 public static final int MODE_ON_DROP_EXTERNAL = 3;
147 public static final int MODE_ACCEPT_DROP = 4;
Adam Cohen19f37922012-03-21 11:59:11 -0700148 private static final boolean DESTRUCTIVE_REORDER = false;
Adam Cohen482ed822012-03-02 14:15:13 -0800149 private static final boolean DEBUG_VISUALIZE_OCCUPIED = false;
150
Adam Cohena897f392012-04-27 18:12:05 -0700151 static final int LANDSCAPE = 0;
152 static final int PORTRAIT = 1;
153
Adam Cohenfa3c58f2013-12-06 16:10:55 -0800154 private static final float REORDER_PREVIEW_MAGNITUDE = 0.12f;
Adam Cohen19f37922012-03-21 11:59:11 -0700155 private static final int REORDER_ANIMATION_DURATION = 150;
Adam Cohen091440a2015-03-18 14:16:05 -0700156 @Thunk float mReorderPreviewAnimationMagnitude;
Adam Cohen19f37922012-03-21 11:59:11 -0700157
Adam Cohen482ed822012-03-02 14:15:13 -0800158 private ArrayList<View> mIntersectingViews = new ArrayList<View>();
159 private Rect mOccupiedRect = new Rect();
160 private int[] mDirectionVector = new int[2];
Adam Cohen19f37922012-03-21 11:59:11 -0700161 int[] mPreviousReorderDirection = new int[2];
Adam Cohenb209e632012-03-27 17:09:36 -0700162 private static final int INVALID_DIRECTION = -100;
Adam Cohen482ed822012-03-02 14:15:13 -0800163
Sunny Goyal2805e632015-05-20 15:35:32 -0700164 private final Rect mTempRect = new Rect();
Winson Chung3a6e7f32013-10-09 15:50:52 -0700165
Michael Jurkaca993832012-06-29 15:17:04 -0700166 private final static Paint sPaint = new Paint();
Romain Guy8a0bff52012-05-06 13:14:33 -0700167
Adam Cohenc9735cf2015-01-23 16:11:55 -0800168 // Related to accessible drag and drop
Sunny Goyale9b651e2015-04-24 11:44:51 -0700169 private DragAndDropAccessibilityDelegate mTouchHelper;
Adam Cohenc9735cf2015-01-23 16:11:55 -0800170 private boolean mUseTouchHelper = false;
Adam Cohenc9735cf2015-01-23 16:11:55 -0800171
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800172 public CellLayout(Context context) {
173 this(context, null);
174 }
175
176 public CellLayout(Context context, AttributeSet attrs) {
177 this(context, attrs, 0);
178 }
179
180 public CellLayout(Context context, AttributeSet attrs, int defStyle) {
181 super(context, attrs, defStyle);
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700182
183 // A ViewGroup usually does not draw, but CellLayout needs to draw a rectangle to show
184 // the user where a dragged item will land when dropped.
185 setWillNotDraw(false);
Romain Guyce3cbd12013-02-25 15:00:36 -0800186 setClipToPadding(false);
Adam Cohen2acce882012-03-28 19:03:19 -0700187 mLauncher = (Launcher) context;
Michael Jurkaa63c4522010-08-19 13:52:27 -0700188
Adam Cohen2e6da152015-05-06 11:42:25 -0700189 DeviceProfile grid = mLauncher.getDeviceProfile();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800190 TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.CellLayout, defStyle, 0);
191
Winson Chung11a1a532013-09-13 11:14:45 -0700192 mCellWidth = mCellHeight = -1;
Nilesh Agrawal5f7099a2014-01-02 15:54:57 -0800193 mFixedCellWidth = mFixedCellHeight = -1;
Winson Chung5f8afe62013-08-12 16:19:28 -0700194 mWidthGap = mOriginalWidthGap = 0;
195 mHeightGap = mOriginalHeightGap = 0;
196 mMaxGap = Integer.MAX_VALUE;
Adam Cohen2e6da152015-05-06 11:42:25 -0700197 mCountX = (int) grid.inv.numColumns;
198 mCountY = (int) grid.inv.numRows;
Michael Jurka0280c3b2010-09-17 15:00:07 -0700199 mOccupied = new boolean[mCountX][mCountY];
Adam Cohen482ed822012-03-02 14:15:13 -0800200 mTmpOccupied = new boolean[mCountX][mCountY];
Adam Cohen5b53f292012-03-29 14:30:35 -0700201 mPreviousReorderDirection[0] = INVALID_DIRECTION;
202 mPreviousReorderDirection[1] = INVALID_DIRECTION;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800203
204 a.recycle();
205
206 setAlwaysDrawnWithCacheEnabled(false);
207
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700208 final Resources res = getResources();
Winson Chung6e1c0d32013-10-25 15:24:24 -0700209 mHotseatScale = (float) grid.hotseatIconSizePx / grid.iconSizePx;
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700210
Sunny Goyal2805e632015-05-20 15:35:32 -0700211 mBackground = (TransitionDrawable) res.getDrawable(R.drawable.bg_screenpanel);
212 mBackground.setCallback(this);
Winson Chunge8f1d042015-07-31 12:39:57 -0700213 mBackground.setAlpha((int) (mBackgroundAlpha * 255));
Michael Jurka33945b22010-12-21 18:19:38 -0800214
Adam Cohenfa3c58f2013-12-06 16:10:55 -0800215 mReorderPreviewAnimationMagnitude = (REORDER_PREVIEW_MAGNITUDE *
Winson Chung5f8afe62013-08-12 16:19:28 -0700216 grid.iconSizePx);
Adam Cohen19f37922012-03-21 11:59:11 -0700217
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700218 // Initialize the data structures used for the drag visualization.
Patrick Dubroyce34a972010-10-19 10:34:32 -0700219 mEaseOutInterpolator = new DecelerateInterpolator(2.5f); // Quint ease out
Winson Chungb8c69f32011-10-19 21:36:08 -0700220 mDragCell[0] = mDragCell[1] = -1;
Joe Onorato4be866d2010-10-10 11:26:02 -0700221 for (int i = 0; i < mDragOutlines.length; i++) {
Adam Cohend41fbf52012-02-16 23:53:59 -0800222 mDragOutlines[i] = new Rect(-1, -1, -1, -1);
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700223 }
224
225 // When dragging things around the home screens, we show a green outline of
226 // where the item will land. The outlines gradually fade out, leaving a trail
227 // behind the drag path.
228 // Set up all the animations that are used to implement this fading.
229 final int duration = res.getInteger(R.integer.config_dragOutlineFadeTime);
Chet Haase472b2812010-10-14 07:02:04 -0700230 final float fromAlphaValue = 0;
231 final float toAlphaValue = (float)res.getInteger(R.integer.config_dragOutlineMaxAlpha);
Joe Onorato4be866d2010-10-10 11:26:02 -0700232
Patrick Dubroy8e58e912010-10-14 13:21:48 -0700233 Arrays.fill(mDragOutlineAlphas, fromAlphaValue);
Joe Onorato4be866d2010-10-10 11:26:02 -0700234
235 for (int i = 0; i < mDragOutlineAnims.length; i++) {
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700236 final InterruptibleInOutAnimator anim =
Michael Jurkaf1ad6082013-03-13 12:55:46 +0100237 new InterruptibleInOutAnimator(this, duration, fromAlphaValue, toAlphaValue);
Patrick Dubroyce34a972010-10-19 10:34:32 -0700238 anim.getAnimator().setInterpolator(mEaseOutInterpolator);
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700239 final int thisIndex = i;
Chet Haase472b2812010-10-14 07:02:04 -0700240 anim.getAnimator().addUpdateListener(new AnimatorUpdateListener() {
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700241 public void onAnimationUpdate(ValueAnimator animation) {
Joe Onorato4be866d2010-10-10 11:26:02 -0700242 final Bitmap outline = (Bitmap)anim.getTag();
243
244 // If an animation is started and then stopped very quickly, we can still
245 // get spurious updates we've cleared the tag. Guard against this.
246 if (outline == null) {
Michael Jurka3a9fced2012-04-13 14:44:29 -0700247 @SuppressWarnings("all") // suppress dead code warning
248 final boolean debug = false;
249 if (debug) {
Patrick Dubroyfe6bd872010-10-13 17:32:10 -0700250 Object val = animation.getAnimatedValue();
251 Log.d(TAG, "anim " + thisIndex + " update: " + val +
252 ", isStopped " + anim.isStopped());
253 }
Joe Onorato4be866d2010-10-10 11:26:02 -0700254 // Try to prevent it from continuing to run
255 animation.cancel();
256 } else {
Chet Haase472b2812010-10-14 07:02:04 -0700257 mDragOutlineAlphas[thisIndex] = (Float) animation.getAnimatedValue();
Adam Cohend41fbf52012-02-16 23:53:59 -0800258 CellLayout.this.invalidate(mDragOutlines[thisIndex]);
Joe Onorato4be866d2010-10-10 11:26:02 -0700259 }
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700260 }
261 });
Joe Onorato4be866d2010-10-10 11:26:02 -0700262 // The animation holds a reference to the drag outline bitmap as long is it's
263 // running. This way the bitmap can be GCed when the animations are complete.
Chet Haase472b2812010-10-14 07:02:04 -0700264 anim.getAnimator().addListener(new AnimatorListenerAdapter() {
Michael Jurka3c4c20f2010-10-28 15:36:06 -0700265 @Override
Joe Onorato4be866d2010-10-10 11:26:02 -0700266 public void onAnimationEnd(Animator animation) {
Chet Haase472b2812010-10-14 07:02:04 -0700267 if ((Float) ((ValueAnimator) animation).getAnimatedValue() == 0f) {
Joe Onorato4be866d2010-10-10 11:26:02 -0700268 anim.setTag(null);
269 }
270 }
271 });
272 mDragOutlineAnims[i] = anim;
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700273 }
Patrick Dubroyce34a972010-10-19 10:34:32 -0700274
Michael Jurkaa52570f2012-03-20 03:18:20 -0700275 mShortcutsAndWidgets = new ShortcutAndWidgetContainer(context);
Adam Cohen2374abf2013-04-16 14:56:57 -0700276 mShortcutsAndWidgets.setCellDimensions(mCellWidth, mCellHeight, mWidthGap, mHeightGap,
Winson Chung5f8afe62013-08-12 16:19:28 -0700277 mCountX, mCountY);
Adam Cohen2374abf2013-04-16 14:56:57 -0700278
Mady Melloref044dd2015-06-02 15:35:07 -0700279 mStylusEventHelper = new StylusEventHelper(this);
280
Sunny Goyal4fe5a372015-05-14 19:55:10 -0700281 mTouchFeedbackView = new ClickShadowView(context);
282 addView(mTouchFeedbackView);
Michael Jurkaa52570f2012-03-20 03:18:20 -0700283 addView(mShortcutsAndWidgets);
Michael Jurka18014792010-10-14 09:01:34 -0700284 }
285
Adam Cohenc9735cf2015-01-23 16:11:55 -0800286 @TargetApi(Build.VERSION_CODES.LOLLIPOP)
Sunny Goyale9b651e2015-04-24 11:44:51 -0700287 public void enableAccessibleDrag(boolean enable, int dragType) {
Adam Cohenc9735cf2015-01-23 16:11:55 -0800288 mUseTouchHelper = enable;
289 if (!enable) {
290 ViewCompat.setAccessibilityDelegate(this, null);
291 setImportantForAccessibility(IMPORTANT_FOR_ACCESSIBILITY_NO);
292 getShortcutsAndWidgets().setImportantForAccessibility(IMPORTANT_FOR_ACCESSIBILITY_NO);
293 setOnClickListener(mLauncher);
294 } else {
Sunny Goyale9b651e2015-04-24 11:44:51 -0700295 if (dragType == WORKSPACE_ACCESSIBILITY_DRAG &&
296 !(mTouchHelper instanceof WorkspaceAccessibilityHelper)) {
297 mTouchHelper = new WorkspaceAccessibilityHelper(this);
298 } else if (dragType == FOLDER_ACCESSIBILITY_DRAG &&
299 !(mTouchHelper instanceof FolderAccessibilityHelper)) {
300 mTouchHelper = new FolderAccessibilityHelper(this);
301 }
Adam Cohenc9735cf2015-01-23 16:11:55 -0800302 ViewCompat.setAccessibilityDelegate(this, mTouchHelper);
303 setImportantForAccessibility(IMPORTANT_FOR_ACCESSIBILITY_YES);
304 getShortcutsAndWidgets().setImportantForAccessibility(IMPORTANT_FOR_ACCESSIBILITY_YES);
305 setOnClickListener(mTouchHelper);
306 }
307
308 // Invalidate the accessibility hierarchy
309 if (getParent() != null) {
310 getParent().notifySubtreeAccessibilityStateChanged(
311 this, this, AccessibilityEvent.CONTENT_CHANGE_TYPE_SUBTREE);
312 }
313 }
314
315 @Override
316 public boolean dispatchHoverEvent(MotionEvent event) {
317 // Always attempt to dispatch hover events to accessibility first.
318 if (mUseTouchHelper && mTouchHelper.dispatchHoverEvent(event)) {
319 return true;
320 }
321 return super.dispatchHoverEvent(event);
322 }
323
324 @Override
Adam Cohenc9735cf2015-01-23 16:11:55 -0800325 public boolean onInterceptTouchEvent(MotionEvent ev) {
326 if (mUseTouchHelper ||
327 (mInterceptTouchListener != null && mInterceptTouchListener.onTouch(this, ev))) {
328 return true;
329 }
330 return false;
331 }
332
Mady Melloref044dd2015-06-02 15:35:07 -0700333 @Override
334 public boolean onTouchEvent(MotionEvent ev) {
335 boolean handled = super.onTouchEvent(ev);
336 // Stylus button press on a home screen should not switch between overview mode and
337 // the home screen mode, however, once in overview mode stylus button press should be
338 // enabled to allow rearranging the different home screens. So check what mode
339 // the workspace is in, and only perform stylus button presses while in overview mode.
340 if (mLauncher.mWorkspace.isInOverviewMode()
341 && mStylusEventHelper.checkAndPerformStylusEvent(ev)) {
342 return true;
343 }
344 return handled;
345 }
346
Chris Craik01f2d7f2013-10-01 14:41:56 -0700347 public void enableHardwareLayer(boolean hasLayer) {
348 mShortcutsAndWidgets.setLayerType(hasLayer ? LAYER_TYPE_HARDWARE : LAYER_TYPE_NONE, sPaint);
Michael Jurkad51f33a2012-06-28 15:35:26 -0700349 }
350
351 public void buildHardwareLayer() {
352 mShortcutsAndWidgets.buildLayer();
Adam Cohen2801caf2011-05-13 20:57:39 -0700353 }
354
Adam Cohen307fe232012-08-16 17:55:58 -0700355 public float getChildrenScale() {
356 return mIsHotseat ? mHotseatScale : 1.0f;
357 }
358
Winson Chung5f8afe62013-08-12 16:19:28 -0700359 public void setCellDimensions(int width, int height) {
Winson Chung11a1a532013-09-13 11:14:45 -0700360 mFixedCellWidth = mCellWidth = width;
361 mFixedCellHeight = mCellHeight = height;
Winson Chung5f8afe62013-08-12 16:19:28 -0700362 mShortcutsAndWidgets.setCellDimensions(mCellWidth, mCellHeight, mWidthGap, mHeightGap,
363 mCountX, mCountY);
364 }
365
Adam Cohen2801caf2011-05-13 20:57:39 -0700366 public void setGridSize(int x, int y) {
367 mCountX = x;
368 mCountY = y;
369 mOccupied = new boolean[mCountX][mCountY];
Adam Cohen482ed822012-03-02 14:15:13 -0800370 mTmpOccupied = new boolean[mCountX][mCountY];
Adam Cohen7fbec102012-03-27 12:42:19 -0700371 mTempRectStack.clear();
Adam Cohen2374abf2013-04-16 14:56:57 -0700372 mShortcutsAndWidgets.setCellDimensions(mCellWidth, mCellHeight, mWidthGap, mHeightGap,
Winson Chung5f8afe62013-08-12 16:19:28 -0700373 mCountX, mCountY);
Adam Cohen76fc0852011-06-17 13:26:23 -0700374 requestLayout();
Adam Cohen2801caf2011-05-13 20:57:39 -0700375 }
376
Adam Cohen2374abf2013-04-16 14:56:57 -0700377 // Set whether or not to invert the layout horizontally if the layout is in RTL mode.
378 public void setInvertIfRtl(boolean invert) {
379 mShortcutsAndWidgets.setInvertIfRtl(invert);
380 }
381
Adam Cohen917e3882013-10-31 15:03:35 -0700382 public void setDropPending(boolean pending) {
383 mDropPending = pending;
384 }
385
386 public boolean isDropPending() {
387 return mDropPending;
388 }
389
Sunny Goyal4b6eb262015-05-14 19:24:40 -0700390 @Override
391 public void setPressedIcon(BubbleTextView icon, Bitmap background) {
Sunny Goyal508da152014-08-14 10:53:27 -0700392 if (icon == null || background == null) {
393 mTouchFeedbackView.setBitmap(null);
394 mTouchFeedbackView.animate().cancel();
395 } else {
Sunny Goyal508da152014-08-14 10:53:27 -0700396 if (mTouchFeedbackView.setBitmap(background)) {
Sunny Goyal4fe5a372015-05-14 19:55:10 -0700397 mTouchFeedbackView.alignWithIconView(icon, mShortcutsAndWidgets);
398 mTouchFeedbackView.animateShadow();
Sunny Goyal508da152014-08-14 10:53:27 -0700399 }
Patrick Dubroy96864c32011-03-10 17:17:23 -0800400 }
401 }
402
Adam Cohenc50438c2014-08-19 17:43:05 -0700403 void disableDragTarget() {
404 mIsDragTarget = false;
405 }
406
407 boolean isDragTarget() {
408 return mIsDragTarget;
409 }
410
411 void setIsDragOverlapping(boolean isDragOverlapping) {
412 if (mIsDragOverlapping != isDragOverlapping) {
413 mIsDragOverlapping = isDragOverlapping;
Sunny Goyal2805e632015-05-20 15:35:32 -0700414 if (mIsDragOverlapping) {
415 mBackground.startTransition(BACKGROUND_ACTIVATE_DURATION);
416 } else {
Winson Chunge8f1d042015-07-31 12:39:57 -0700417 if (mBackgroundAlpha > 0f) {
418 mBackground.reverseTransition(BACKGROUND_ACTIVATE_DURATION);
419 } else {
420 mBackground.resetTransition();
421 }
Sunny Goyal2805e632015-05-20 15:35:32 -0700422 }
Adam Cohenc50438c2014-08-19 17:43:05 -0700423 invalidate();
424 }
425 }
426
Michael Jurka33945b22010-12-21 18:19:38 -0800427 boolean getIsDragOverlapping() {
428 return mIsDragOverlapping;
429 }
430
Jeff Sharkey83f111d2009-04-20 21:03:13 -0700431 @Override
Patrick Dubroy1262e362010-10-06 15:49:50 -0700432 protected void onDraw(Canvas canvas) {
Sunny Goyal05739772015-05-19 19:59:09 -0700433 if (!mIsDragTarget) {
434 return;
435 }
436
Michael Jurka3e7c7632010-10-02 16:01:03 -0700437 // When we're large, we are either drawn in a "hover" state (ie when dragging an item to
438 // a neighboring page) or with just a normal background (if backgroundAlpha > 0.0f)
439 // When we're small, we are either drawn normally or in the "accepts drops" state (during
440 // a drag). However, we also drag the mini hover background *over* one of those two
441 // backgrounds
Sunny Goyal05739772015-05-19 19:59:09 -0700442 if (mBackgroundAlpha > 0.0f) {
Sunny Goyal2805e632015-05-20 15:35:32 -0700443 mBackground.draw(canvas);
Michael Jurkaa63c4522010-08-19 13:52:27 -0700444 }
Romain Guya6abce82009-11-10 02:54:41 -0800445
Patrick Dubroy8e58e912010-10-14 13:21:48 -0700446 final Paint paint = mDragOutlinePaint;
Joe Onorato4be866d2010-10-10 11:26:02 -0700447 for (int i = 0; i < mDragOutlines.length; i++) {
Chet Haase472b2812010-10-14 07:02:04 -0700448 final float alpha = mDragOutlineAlphas[i];
Joe Onorato4be866d2010-10-10 11:26:02 -0700449 if (alpha > 0) {
Adam Cohend41fbf52012-02-16 23:53:59 -0800450 final Rect r = mDragOutlines[i];
Winson Chung3a6e7f32013-10-09 15:50:52 -0700451 mTempRect.set(r);
452 Utilities.scaleRectAboutCenter(mTempRect, getChildrenScale());
Joe Onorato4be866d2010-10-10 11:26:02 -0700453 final Bitmap b = (Bitmap) mDragOutlineAnims[i].getTag();
Chet Haase472b2812010-10-14 07:02:04 -0700454 paint.setAlpha((int)(alpha + .5f));
Winson Chung3a6e7f32013-10-09 15:50:52 -0700455 canvas.drawBitmap(b, null, mTempRect, paint);
Winson Chung150fbab2010-09-29 17:14:26 -0700456 }
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700457 }
Patrick Dubroy96864c32011-03-10 17:17:23 -0800458
Adam Cohen482ed822012-03-02 14:15:13 -0800459 if (DEBUG_VISUALIZE_OCCUPIED) {
460 int[] pt = new int[2];
461 ColorDrawable cd = new ColorDrawable(Color.RED);
Adam Cohene7587d22012-05-24 18:50:02 -0700462 cd.setBounds(0, 0, mCellWidth, mCellHeight);
Adam Cohen482ed822012-03-02 14:15:13 -0800463 for (int i = 0; i < mCountX; i++) {
464 for (int j = 0; j < mCountY; j++) {
465 if (mOccupied[i][j]) {
466 cellToPoint(i, j, pt);
467 canvas.save();
468 canvas.translate(pt[0], pt[1]);
469 cd.draw(canvas);
470 canvas.restore();
471 }
472 }
473 }
474 }
475
Andrew Flynn850d2e72012-04-26 16:51:20 -0700476 int previewOffset = FolderRingAnimator.sPreviewSize;
477
Adam Cohen69ce2e52011-07-03 19:25:21 -0700478 // The folder outer / inner ring image(s)
Adam Cohen2e6da152015-05-06 11:42:25 -0700479 DeviceProfile grid = mLauncher.getDeviceProfile();
Adam Cohen69ce2e52011-07-03 19:25:21 -0700480 for (int i = 0; i < mFolderOuterRings.size(); i++) {
481 FolderRingAnimator fra = mFolderOuterRings.get(i);
482
Adam Cohen5108bc02013-09-20 17:04:51 -0700483 Drawable d;
484 int width, height;
Adam Cohen69ce2e52011-07-03 19:25:21 -0700485 cellToPoint(fra.mCellX, fra.mCellY, mTempLocation);
Winson Chung5f8afe62013-08-12 16:19:28 -0700486 View child = getChildAt(fra.mCellX, fra.mCellY);
Adam Cohen558f1c22013-10-09 15:15:24 -0700487
Winson Chung89f97052013-09-20 11:32:26 -0700488 if (child != null) {
Adam Cohen558f1c22013-10-09 15:15:24 -0700489 int centerX = mTempLocation[0] + mCellWidth / 2;
490 int centerY = mTempLocation[1] + previewOffset / 2 +
491 child.getPaddingTop() + grid.folderBackgroundOffset;
492
Adam Cohen5108bc02013-09-20 17:04:51 -0700493 // Draw outer ring, if it exists
494 if (FolderIcon.HAS_OUTER_RING) {
495 d = FolderRingAnimator.sSharedOuterRingDrawable;
496 width = (int) (fra.getOuterRingSize() * getChildrenScale());
497 height = width;
498 canvas.save();
499 canvas.translate(centerX - width / 2, centerY - height / 2);
500 d.setBounds(0, 0, width, height);
501 d.draw(canvas);
502 canvas.restore();
503 }
Adam Cohen69ce2e52011-07-03 19:25:21 -0700504
Winson Chung89f97052013-09-20 11:32:26 -0700505 // Draw inner ring
506 d = FolderRingAnimator.sSharedInnerRingDrawable;
507 width = (int) (fra.getInnerRingSize() * getChildrenScale());
508 height = width;
Winson Chung89f97052013-09-20 11:32:26 -0700509 canvas.save();
510 canvas.translate(centerX - width / 2, centerY - width / 2);
511 d.setBounds(0, 0, width, height);
512 d.draw(canvas);
513 canvas.restore();
514 }
Adam Cohen69ce2e52011-07-03 19:25:21 -0700515 }
Adam Cohenc51934b2011-07-26 21:07:43 -0700516
517 if (mFolderLeaveBehindCell[0] >= 0 && mFolderLeaveBehindCell[1] >= 0) {
518 Drawable d = FolderIcon.sSharedFolderLeaveBehind;
519 int width = d.getIntrinsicWidth();
520 int height = d.getIntrinsicHeight();
521
522 cellToPoint(mFolderLeaveBehindCell[0], mFolderLeaveBehindCell[1], mTempLocation);
Winson Chung5f8afe62013-08-12 16:19:28 -0700523 View child = getChildAt(mFolderLeaveBehindCell[0], mFolderLeaveBehindCell[1]);
Winson Chung89f97052013-09-20 11:32:26 -0700524 if (child != null) {
525 int centerX = mTempLocation[0] + mCellWidth / 2;
526 int centerY = mTempLocation[1] + previewOffset / 2 +
527 child.getPaddingTop() + grid.folderBackgroundOffset;
Adam Cohenc51934b2011-07-26 21:07:43 -0700528
Winson Chung89f97052013-09-20 11:32:26 -0700529 canvas.save();
530 canvas.translate(centerX - width / 2, centerY - width / 2);
531 d.setBounds(0, 0, width, height);
532 d.draw(canvas);
533 canvas.restore();
534 }
Adam Cohenc51934b2011-07-26 21:07:43 -0700535 }
Adam Cohen69ce2e52011-07-03 19:25:21 -0700536 }
537
538 public void showFolderAccept(FolderRingAnimator fra) {
539 mFolderOuterRings.add(fra);
540 }
541
542 public void hideFolderAccept(FolderRingAnimator fra) {
543 if (mFolderOuterRings.contains(fra)) {
544 mFolderOuterRings.remove(fra);
545 }
546 invalidate();
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700547 }
548
Adam Cohenc51934b2011-07-26 21:07:43 -0700549 public void setFolderLeaveBehindCell(int x, int y) {
550 mFolderLeaveBehindCell[0] = x;
551 mFolderLeaveBehindCell[1] = y;
552 invalidate();
553 }
554
555 public void clearFolderLeaveBehind() {
556 mFolderLeaveBehindCell[0] = -1;
557 mFolderLeaveBehindCell[1] = -1;
558 invalidate();
559 }
560
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700561 @Override
Michael Jurkae6235dd2011-10-04 15:02:05 -0700562 public boolean shouldDelayChildPressedState() {
563 return false;
564 }
565
Adam Cohen1462de32012-07-24 22:34:36 -0700566 public void restoreInstanceState(SparseArray<Parcelable> states) {
Sunny Goyal33a152f2014-07-22 12:13:14 -0700567 try {
568 dispatchRestoreInstanceState(states);
569 } catch (IllegalArgumentException ex) {
570 if (LauncherAppState.isDogfoodBuild()) {
571 throw ex;
572 }
573 // Mismatched viewId / viewType preventing restore. Skip restore on production builds.
574 Log.e(TAG, "Ignoring an error while restoring a view instance state", ex);
575 }
Adam Cohen1462de32012-07-24 22:34:36 -0700576 }
577
Michael Jurkae6235dd2011-10-04 15:02:05 -0700578 @Override
Jeff Sharkey83f111d2009-04-20 21:03:13 -0700579 public void cancelLongPress() {
580 super.cancelLongPress();
581
582 // Cancel long press for all children
583 final int count = getChildCount();
584 for (int i = 0; i < count; i++) {
585 final View child = getChildAt(i);
586 child.cancelLongPress();
587 }
588 }
589
Michael Jurkadee05892010-07-27 10:01:56 -0700590 public void setOnInterceptTouchListener(View.OnTouchListener listener) {
591 mInterceptTouchListener = listener;
592 }
593
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800594 public int getCountX() {
Adam Cohend22015c2010-07-26 22:02:18 -0700595 return mCountX;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800596 }
597
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800598 public int getCountY() {
Adam Cohend22015c2010-07-26 22:02:18 -0700599 return mCountY;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800600 }
601
Andrew Flynn0dca1ec2012-02-29 13:33:22 -0800602 public void setIsHotseat(boolean isHotseat) {
603 mIsHotseat = isHotseat;
Winson Chung5f8afe62013-08-12 16:19:28 -0700604 mShortcutsAndWidgets.setIsHotseat(isHotseat);
Andrew Flynn0dca1ec2012-02-29 13:33:22 -0800605 }
606
Sunny Goyale9b651e2015-04-24 11:44:51 -0700607 public boolean isHotseat() {
608 return mIsHotseat;
609 }
610
Andrew Flynn0dca1ec2012-02-29 13:33:22 -0800611 public boolean addViewToCellLayout(View child, int index, int childId, LayoutParams params,
Sunny Goyalf7a29e82015-04-24 15:20:43 -0700612 boolean markCells) {
Winson Chungaafa03c2010-06-11 17:34:16 -0700613 final LayoutParams lp = params;
614
Andrew Flynnde38e422012-05-08 11:22:15 -0700615 // Hotseat icons - remove text
Andrew Flynn0dca1ec2012-02-29 13:33:22 -0800616 if (child instanceof BubbleTextView) {
617 BubbleTextView bubbleChild = (BubbleTextView) child;
Winson Chung5f8afe62013-08-12 16:19:28 -0700618 bubbleChild.setTextVisibility(!mIsHotseat);
Andrew Flynn0dca1ec2012-02-29 13:33:22 -0800619 }
620
Adam Cohen307fe232012-08-16 17:55:58 -0700621 child.setScaleX(getChildrenScale());
622 child.setScaleY(getChildrenScale());
623
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800624 // Generate an id for each view, this assumes we have at most 256x256 cells
625 // per workspace screen
Adam Cohend22015c2010-07-26 22:02:18 -0700626 if (lp.cellX >= 0 && lp.cellX <= mCountX - 1 && lp.cellY >= 0 && lp.cellY <= mCountY - 1) {
Winson Chungaafa03c2010-06-11 17:34:16 -0700627 // If the horizontal or vertical span is set to -1, it is taken to
628 // mean that it spans the extent of the CellLayout
Adam Cohend22015c2010-07-26 22:02:18 -0700629 if (lp.cellHSpan < 0) lp.cellHSpan = mCountX;
630 if (lp.cellVSpan < 0) lp.cellVSpan = mCountY;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800631
Winson Chungaafa03c2010-06-11 17:34:16 -0700632 child.setId(childId);
Sunny Goyalf7a29e82015-04-24 15:20:43 -0700633 mShortcutsAndWidgets.addView(child, index, lp);
Michael Jurkadee05892010-07-27 10:01:56 -0700634
Michael Jurkaf3ca3ab2010-10-20 17:08:24 -0700635 if (markCells) markCellsAsOccupiedForView(child);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700636
Winson Chungaafa03c2010-06-11 17:34:16 -0700637 return true;
638 }
639 return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800640 }
Michael Jurka3e7c7632010-10-02 16:01:03 -0700641
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800642 @Override
Michael Jurka0280c3b2010-09-17 15:00:07 -0700643 public void removeAllViews() {
644 clearOccupiedCells();
Michael Jurkaa52570f2012-03-20 03:18:20 -0700645 mShortcutsAndWidgets.removeAllViews();
Michael Jurka0280c3b2010-09-17 15:00:07 -0700646 }
647
648 @Override
649 public void removeAllViewsInLayout() {
Michael Jurkaa52570f2012-03-20 03:18:20 -0700650 if (mShortcutsAndWidgets.getChildCount() > 0) {
Michael Jurka7cfc2822011-08-02 20:19:24 -0700651 clearOccupiedCells();
Michael Jurkaa52570f2012-03-20 03:18:20 -0700652 mShortcutsAndWidgets.removeAllViewsInLayout();
Michael Jurka7cfc2822011-08-02 20:19:24 -0700653 }
Michael Jurka0280c3b2010-09-17 15:00:07 -0700654 }
655
656 @Override
657 public void removeView(View view) {
658 markCellsAsUnoccupiedForView(view);
Michael Jurkaa52570f2012-03-20 03:18:20 -0700659 mShortcutsAndWidgets.removeView(view);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700660 }
661
662 @Override
663 public void removeViewAt(int index) {
Michael Jurkaa52570f2012-03-20 03:18:20 -0700664 markCellsAsUnoccupiedForView(mShortcutsAndWidgets.getChildAt(index));
665 mShortcutsAndWidgets.removeViewAt(index);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700666 }
667
668 @Override
669 public void removeViewInLayout(View view) {
670 markCellsAsUnoccupiedForView(view);
Michael Jurkaa52570f2012-03-20 03:18:20 -0700671 mShortcutsAndWidgets.removeViewInLayout(view);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700672 }
673
674 @Override
675 public void removeViews(int start, int count) {
676 for (int i = start; i < start + count; i++) {
Michael Jurkaa52570f2012-03-20 03:18:20 -0700677 markCellsAsUnoccupiedForView(mShortcutsAndWidgets.getChildAt(i));
Michael Jurka0280c3b2010-09-17 15:00:07 -0700678 }
Michael Jurkaa52570f2012-03-20 03:18:20 -0700679 mShortcutsAndWidgets.removeViews(start, count);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700680 }
681
682 @Override
683 public void removeViewsInLayout(int start, int count) {
684 for (int i = start; i < start + count; i++) {
Michael Jurkaa52570f2012-03-20 03:18:20 -0700685 markCellsAsUnoccupiedForView(mShortcutsAndWidgets.getChildAt(i));
Michael Jurka0280c3b2010-09-17 15:00:07 -0700686 }
Michael Jurkaa52570f2012-03-20 03:18:20 -0700687 mShortcutsAndWidgets.removeViewsInLayout(start, count);
Michael Jurkaabded662011-03-04 12:06:57 -0800688 }
689
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700690 /**
Winson Chungaafa03c2010-06-11 17:34:16 -0700691 * Given a point, return the cell that strictly encloses that point
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800692 * @param x X coordinate of the point
693 * @param y Y coordinate of the point
694 * @param result Array of 2 ints to hold the x and y coordinate of the cell
695 */
Sunny Goyale9b651e2015-04-24 11:44:51 -0700696 public void pointToCellExact(int x, int y, int[] result) {
Winson Chung4b825dcd2011-06-19 12:41:22 -0700697 final int hStartPadding = getPaddingLeft();
698 final int vStartPadding = getPaddingTop();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800699
700 result[0] = (x - hStartPadding) / (mCellWidth + mWidthGap);
701 result[1] = (y - vStartPadding) / (mCellHeight + mHeightGap);
702
Adam Cohend22015c2010-07-26 22:02:18 -0700703 final int xAxis = mCountX;
704 final int yAxis = mCountY;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800705
706 if (result[0] < 0) result[0] = 0;
707 if (result[0] >= xAxis) result[0] = xAxis - 1;
708 if (result[1] < 0) result[1] = 0;
709 if (result[1] >= yAxis) result[1] = yAxis - 1;
710 }
Winson Chungaafa03c2010-06-11 17:34:16 -0700711
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800712 /**
713 * Given a point, return the cell that most closely encloses that point
714 * @param x X coordinate of the point
715 * @param y Y coordinate of the point
716 * @param result Array of 2 ints to hold the x and y coordinate of the cell
717 */
718 void pointToCellRounded(int x, int y, int[] result) {
719 pointToCellExact(x + (mCellWidth / 2), y + (mCellHeight / 2), result);
720 }
721
722 /**
723 * Given a cell coordinate, return the point that represents the upper left corner of that cell
Winson Chungaafa03c2010-06-11 17:34:16 -0700724 *
725 * @param cellX X coordinate of the cell
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800726 * @param cellY Y coordinate of the cell
Winson Chungaafa03c2010-06-11 17:34:16 -0700727 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800728 * @param result Array of 2 ints to hold the x and y coordinate of the point
729 */
730 void cellToPoint(int cellX, int cellY, int[] result) {
Winson Chung4b825dcd2011-06-19 12:41:22 -0700731 final int hStartPadding = getPaddingLeft();
732 final int vStartPadding = getPaddingTop();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800733
734 result[0] = hStartPadding + cellX * (mCellWidth + mWidthGap);
735 result[1] = vStartPadding + cellY * (mCellHeight + mHeightGap);
736 }
737
Adam Cohene3e27a82011-04-15 12:07:39 -0700738 /**
Adam Cohen482ed822012-03-02 14:15:13 -0800739 * Given a cell coordinate, return the point that represents the center of the cell
Adam Cohene3e27a82011-04-15 12:07:39 -0700740 *
741 * @param cellX X coordinate of the cell
742 * @param cellY Y coordinate of the cell
743 *
744 * @param result Array of 2 ints to hold the x and y coordinate of the point
745 */
746 void cellToCenterPoint(int cellX, int cellY, int[] result) {
Adam Cohen47a876d2012-03-19 13:21:41 -0700747 regionToCenterPoint(cellX, cellY, 1, 1, result);
748 }
749
750 /**
751 * Given a cell coordinate and span return the point that represents the center of the regio
752 *
753 * @param cellX X coordinate of the cell
754 * @param cellY Y coordinate of the cell
755 *
756 * @param result Array of 2 ints to hold the x and y coordinate of the point
757 */
758 void regionToCenterPoint(int cellX, int cellY, int spanX, int spanY, int[] result) {
Winson Chung4b825dcd2011-06-19 12:41:22 -0700759 final int hStartPadding = getPaddingLeft();
760 final int vStartPadding = getPaddingTop();
Adam Cohen47a876d2012-03-19 13:21:41 -0700761 result[0] = hStartPadding + cellX * (mCellWidth + mWidthGap) +
762 (spanX * mCellWidth + (spanX - 1) * mWidthGap) / 2;
763 result[1] = vStartPadding + cellY * (mCellHeight + mHeightGap) +
764 (spanY * mCellHeight + (spanY - 1) * mHeightGap) / 2;
Adam Cohene3e27a82011-04-15 12:07:39 -0700765 }
766
Adam Cohen19f37922012-03-21 11:59:11 -0700767 /**
768 * Given a cell coordinate and span fills out a corresponding pixel rect
769 *
770 * @param cellX X coordinate of the cell
771 * @param cellY Y coordinate of the cell
772 * @param result Rect in which to write the result
773 */
774 void regionToRect(int cellX, int cellY, int spanX, int spanY, Rect result) {
775 final int hStartPadding = getPaddingLeft();
776 final int vStartPadding = getPaddingTop();
777 final int left = hStartPadding + cellX * (mCellWidth + mWidthGap);
778 final int top = vStartPadding + cellY * (mCellHeight + mHeightGap);
779 result.set(left, top, left + (spanX * mCellWidth + (spanX - 1) * mWidthGap),
780 top + (spanY * mCellHeight + (spanY - 1) * mHeightGap));
781 }
782
Adam Cohen482ed822012-03-02 14:15:13 -0800783 public float getDistanceFromCell(float x, float y, int[] cell) {
784 cellToCenterPoint(cell[0], cell[1], mTmpPoint);
Sunny Goyalf7a29e82015-04-24 15:20:43 -0700785 return (float) Math.hypot(x - mTmpPoint[0], y - mTmpPoint[1]);
Adam Cohen482ed822012-03-02 14:15:13 -0800786 }
787
Romain Guy84f296c2009-11-04 15:00:44 -0800788 int getCellWidth() {
789 return mCellWidth;
790 }
791
792 int getCellHeight() {
793 return mCellHeight;
794 }
795
Adam Cohend4844c32011-02-18 19:25:06 -0800796 int getWidthGap() {
797 return mWidthGap;
798 }
799
800 int getHeightGap() {
801 return mHeightGap;
802 }
803
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700804 public void setFixedSize(int width, int height) {
805 mFixedWidth = width;
806 mFixedHeight = height;
807 }
808
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800809 @Override
810 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800811 int widthSpecMode = MeasureSpec.getMode(widthMeasureSpec);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800812 int heightSpecMode = MeasureSpec.getMode(heightMeasureSpec);
Winson Chung5f8afe62013-08-12 16:19:28 -0700813 int widthSize = MeasureSpec.getSize(widthMeasureSpec);
814 int heightSize = MeasureSpec.getSize(heightMeasureSpec);
Winson Chung2d75f122013-09-23 16:53:31 -0700815 int childWidthSize = widthSize - (getPaddingLeft() + getPaddingRight());
816 int childHeightSize = heightSize - (getPaddingTop() + getPaddingBottom());
Winson Chung11a1a532013-09-13 11:14:45 -0700817 if (mFixedCellWidth < 0 || mFixedCellHeight < 0) {
Sunny Goyalc6205602015-05-21 20:46:33 -0700818 int cw = DeviceProfile.calculateCellWidth(childWidthSize, mCountX);
819 int ch = DeviceProfile.calculateCellHeight(childHeightSize, mCountY);
Winson Chung11a1a532013-09-13 11:14:45 -0700820 if (cw != mCellWidth || ch != mCellHeight) {
821 mCellWidth = cw;
822 mCellHeight = ch;
823 mShortcutsAndWidgets.setCellDimensions(mCellWidth, mCellHeight, mWidthGap,
824 mHeightGap, mCountX, mCountY);
825 }
Winson Chung5f8afe62013-08-12 16:19:28 -0700826 }
Winson Chungaafa03c2010-06-11 17:34:16 -0700827
Winson Chung2d75f122013-09-23 16:53:31 -0700828 int newWidth = childWidthSize;
829 int newHeight = childHeightSize;
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700830 if (mFixedWidth > 0 && mFixedHeight > 0) {
831 newWidth = mFixedWidth;
832 newHeight = mFixedHeight;
833 } else if (widthSpecMode == MeasureSpec.UNSPECIFIED || heightSpecMode == MeasureSpec.UNSPECIFIED) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800834 throw new RuntimeException("CellLayout cannot have UNSPECIFIED dimensions");
835 }
836
Adam Cohend22015c2010-07-26 22:02:18 -0700837 int numWidthGaps = mCountX - 1;
838 int numHeightGaps = mCountY - 1;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800839
Adam Cohen234c4cd2011-07-17 21:03:04 -0700840 if (mOriginalWidthGap < 0 || mOriginalHeightGap < 0) {
Winson Chung2d75f122013-09-23 16:53:31 -0700841 int hSpace = childWidthSize;
842 int vSpace = childHeightSize;
Adam Cohenf4bd5792012-04-27 11:35:29 -0700843 int hFreeSpace = hSpace - (mCountX * mCellWidth);
844 int vFreeSpace = vSpace - (mCountY * mCellHeight);
Winson Chung4b825dcd2011-06-19 12:41:22 -0700845 mWidthGap = Math.min(mMaxGap, numWidthGaps > 0 ? (hFreeSpace / numWidthGaps) : 0);
846 mHeightGap = Math.min(mMaxGap,numHeightGaps > 0 ? (vFreeSpace / numHeightGaps) : 0);
Winson Chung5f8afe62013-08-12 16:19:28 -0700847 mShortcutsAndWidgets.setCellDimensions(mCellWidth, mCellHeight, mWidthGap,
848 mHeightGap, mCountX, mCountY);
Adam Cohen234c4cd2011-07-17 21:03:04 -0700849 } else {
850 mWidthGap = mOriginalWidthGap;
851 mHeightGap = mOriginalHeightGap;
Winson Chungece7f5b2010-10-22 14:54:12 -0700852 }
Sunny Goyal4fe5a372015-05-14 19:55:10 -0700853
854 // Make the feedback view large enough to hold the blur bitmap.
855 mTouchFeedbackView.measure(
856 MeasureSpec.makeMeasureSpec(mCellWidth + mTouchFeedbackView.getExtraSize(),
857 MeasureSpec.EXACTLY),
858 MeasureSpec.makeMeasureSpec(mCellHeight + mTouchFeedbackView.getExtraSize(),
859 MeasureSpec.EXACTLY));
860
861 mShortcutsAndWidgets.measure(
862 MeasureSpec.makeMeasureSpec(newWidth, MeasureSpec.EXACTLY),
863 MeasureSpec.makeMeasureSpec(newHeight, MeasureSpec.EXACTLY));
864
865 int maxWidth = mShortcutsAndWidgets.getMeasuredWidth();
866 int maxHeight = mShortcutsAndWidgets.getMeasuredHeight();
Winson Chung2d75f122013-09-23 16:53:31 -0700867 if (mFixedWidth > 0 && mFixedHeight > 0) {
868 setMeasuredDimension(maxWidth, maxHeight);
869 } else {
870 setMeasuredDimension(widthSize, heightSize);
871 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800872 }
873
874 @Override
Michael Jurka28750fb2010-09-24 17:43:49 -0700875 protected void onLayout(boolean changed, int l, int t, int r, int b) {
Winson Chung38848ca2013-10-08 12:03:44 -0700876 int offset = getMeasuredWidth() - getPaddingLeft() - getPaddingRight() -
877 (mCountX * mCellWidth);
878 int left = getPaddingLeft() + (int) Math.ceil(offset / 2f);
879 int top = getPaddingTop();
Sunny Goyal4fe5a372015-05-14 19:55:10 -0700880
881 mTouchFeedbackView.layout(left, top,
882 left + mTouchFeedbackView.getMeasuredWidth(),
883 top + mTouchFeedbackView.getMeasuredHeight());
884 mShortcutsAndWidgets.layout(left, top,
885 left + r - l,
886 top + b - t);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800887 }
888
889 @Override
Michael Jurkadee05892010-07-27 10:01:56 -0700890 protected void onSizeChanged(int w, int h, int oldw, int oldh) {
891 super.onSizeChanged(w, h, oldw, oldh);
Winson Chung82a9bd22013-10-08 16:02:34 -0700892
893 // Expand the background drawing bounds by the padding baked into the background drawable
Sunny Goyal2805e632015-05-20 15:35:32 -0700894 mBackground.getPadding(mTempRect);
895 mBackground.setBounds(-mTempRect.left, -mTempRect.top,
896 w + mTempRect.right, h + mTempRect.bottom);
Michael Jurkadee05892010-07-27 10:01:56 -0700897 }
898
899 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800900 protected void setChildrenDrawingCacheEnabled(boolean enabled) {
Michael Jurkaa52570f2012-03-20 03:18:20 -0700901 mShortcutsAndWidgets.setChildrenDrawingCacheEnabled(enabled);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800902 }
903
904 @Override
905 protected void setChildrenDrawnWithCacheEnabled(boolean enabled) {
Michael Jurkaa52570f2012-03-20 03:18:20 -0700906 mShortcutsAndWidgets.setChildrenDrawnWithCacheEnabled(enabled);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800907 }
908
Michael Jurka5f1c5092010-09-03 14:15:02 -0700909 public float getBackgroundAlpha() {
910 return mBackgroundAlpha;
Michael Jurkadee05892010-07-27 10:01:56 -0700911 }
912
Michael Jurka5f1c5092010-09-03 14:15:02 -0700913 public void setBackgroundAlpha(float alpha) {
Michael Jurkaafaa0502011-12-13 18:22:50 -0800914 if (mBackgroundAlpha != alpha) {
915 mBackgroundAlpha = alpha;
Sunny Goyal2805e632015-05-20 15:35:32 -0700916 mBackground.setAlpha((int) (mBackgroundAlpha * 255));
Michael Jurkaafaa0502011-12-13 18:22:50 -0800917 }
Michael Jurkadee05892010-07-27 10:01:56 -0700918 }
919
Sunny Goyal2805e632015-05-20 15:35:32 -0700920 @Override
921 protected boolean verifyDrawable(Drawable who) {
922 return super.verifyDrawable(who) || (mIsDragTarget && who == mBackground);
923 }
924
Michael Jurkaa52570f2012-03-20 03:18:20 -0700925 public void setShortcutAndWidgetAlpha(float alpha) {
Sunny Goyal02b50812014-09-10 15:44:42 -0700926 mShortcutsAndWidgets.setAlpha(alpha);
Michael Jurkadee05892010-07-27 10:01:56 -0700927 }
928
Michael Jurkaa52570f2012-03-20 03:18:20 -0700929 public ShortcutAndWidgetContainer getShortcutsAndWidgets() {
Sunny Goyaldcbcc862014-08-12 15:58:36 -0700930 return mShortcutsAndWidgets;
Michael Jurkaa52570f2012-03-20 03:18:20 -0700931 }
932
Patrick Dubroy440c3602010-07-13 17:50:32 -0700933 public View getChildAt(int x, int y) {
Michael Jurkaa52570f2012-03-20 03:18:20 -0700934 return mShortcutsAndWidgets.getChildAt(x, y);
Patrick Dubroy440c3602010-07-13 17:50:32 -0700935 }
936
Adam Cohen76fc0852011-06-17 13:26:23 -0700937 public boolean animateChildToPosition(final View child, int cellX, int cellY, int duration,
Adam Cohen482ed822012-03-02 14:15:13 -0800938 int delay, boolean permanent, boolean adjustOccupied) {
Michael Jurkaa52570f2012-03-20 03:18:20 -0700939 ShortcutAndWidgetContainer clc = getShortcutsAndWidgets();
Adam Cohen482ed822012-03-02 14:15:13 -0800940 boolean[][] occupied = mOccupied;
941 if (!permanent) {
942 occupied = mTmpOccupied;
943 }
944
Adam Cohen19f37922012-03-21 11:59:11 -0700945 if (clc.indexOfChild(child) != -1) {
Adam Cohenbfbfd262011-06-13 16:55:12 -0700946 final LayoutParams lp = (LayoutParams) child.getLayoutParams();
947 final ItemInfo info = (ItemInfo) child.getTag();
948
949 // We cancel any existing animations
950 if (mReorderAnimators.containsKey(lp)) {
951 mReorderAnimators.get(lp).cancel();
952 mReorderAnimators.remove(lp);
953 }
954
Adam Cohen482ed822012-03-02 14:15:13 -0800955 final int oldX = lp.x;
956 final int oldY = lp.y;
957 if (adjustOccupied) {
958 occupied[lp.cellX][lp.cellY] = false;
959 occupied[cellX][cellY] = true;
960 }
Adam Cohenbfbfd262011-06-13 16:55:12 -0700961 lp.isLockedToGrid = true;
Adam Cohen482ed822012-03-02 14:15:13 -0800962 if (permanent) {
963 lp.cellX = info.cellX = cellX;
964 lp.cellY = info.cellY = cellY;
965 } else {
966 lp.tmpCellX = cellX;
967 lp.tmpCellY = cellY;
968 }
Adam Cohenbfbfd262011-06-13 16:55:12 -0700969 clc.setupLp(lp);
970 lp.isLockedToGrid = false;
Adam Cohen482ed822012-03-02 14:15:13 -0800971 final int newX = lp.x;
972 final int newY = lp.y;
Adam Cohenbfbfd262011-06-13 16:55:12 -0700973
Adam Cohen76fc0852011-06-17 13:26:23 -0700974 lp.x = oldX;
975 lp.y = oldY;
Adam Cohen76fc0852011-06-17 13:26:23 -0700976
Adam Cohen482ed822012-03-02 14:15:13 -0800977 // Exit early if we're not actually moving the view
978 if (oldX == newX && oldY == newY) {
979 lp.isLockedToGrid = true;
980 return true;
981 }
982
Michael Jurkaf1ad6082013-03-13 12:55:46 +0100983 ValueAnimator va = LauncherAnimUtils.ofFloat(child, 0f, 1f);
Adam Cohen482ed822012-03-02 14:15:13 -0800984 va.setDuration(duration);
985 mReorderAnimators.put(lp, va);
986
987 va.addUpdateListener(new AnimatorUpdateListener() {
988 @Override
Adam Cohenbfbfd262011-06-13 16:55:12 -0700989 public void onAnimationUpdate(ValueAnimator animation) {
Adam Cohen482ed822012-03-02 14:15:13 -0800990 float r = ((Float) animation.getAnimatedValue()).floatValue();
Adam Cohen19f37922012-03-21 11:59:11 -0700991 lp.x = (int) ((1 - r) * oldX + r * newX);
992 lp.y = (int) ((1 - r) * oldY + r * newY);
Adam Cohen6b8a02d2012-03-22 15:13:40 -0700993 child.requestLayout();
Adam Cohenbfbfd262011-06-13 16:55:12 -0700994 }
995 });
Adam Cohen482ed822012-03-02 14:15:13 -0800996 va.addListener(new AnimatorListenerAdapter() {
Adam Cohenbfbfd262011-06-13 16:55:12 -0700997 boolean cancelled = false;
998 public void onAnimationEnd(Animator animation) {
999 // If the animation was cancelled, it means that another animation
1000 // has interrupted this one, and we don't want to lock the item into
1001 // place just yet.
1002 if (!cancelled) {
1003 lp.isLockedToGrid = true;
Adam Cohen482ed822012-03-02 14:15:13 -08001004 child.requestLayout();
Adam Cohenbfbfd262011-06-13 16:55:12 -07001005 }
1006 if (mReorderAnimators.containsKey(lp)) {
1007 mReorderAnimators.remove(lp);
1008 }
1009 }
1010 public void onAnimationCancel(Animator animation) {
1011 cancelled = true;
1012 }
1013 });
Adam Cohen482ed822012-03-02 14:15:13 -08001014 va.setStartDelay(delay);
1015 va.start();
Adam Cohenbfbfd262011-06-13 16:55:12 -07001016 return true;
1017 }
1018 return false;
1019 }
1020
Adam Cohen482ed822012-03-02 14:15:13 -08001021 void visualizeDropLocation(View v, Bitmap dragOutline, int originX, int originY, int cellX,
1022 int cellY, int spanX, int spanY, boolean resize, Point dragOffset, Rect dragRegion) {
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -07001023 final int oldDragCellX = mDragCell[0];
1024 final int oldDragCellY = mDragCell[1];
Adam Cohen482ed822012-03-02 14:15:13 -08001025
Adam Cohen2801caf2011-05-13 20:57:39 -07001026 if (dragOutline == null && v == null) {
Adam Cohen2801caf2011-05-13 20:57:39 -07001027 return;
1028 }
1029
Adam Cohen482ed822012-03-02 14:15:13 -08001030 if (cellX != oldDragCellX || cellY != oldDragCellY) {
1031 mDragCell[0] = cellX;
1032 mDragCell[1] = cellY;
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001033 // Find the top left corner of the rect the object will occupy
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001034 final int[] topLeft = mTmpPoint;
Adam Cohen482ed822012-03-02 14:15:13 -08001035 cellToPoint(cellX, cellY, topLeft);
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001036
Joe Onorato4be866d2010-10-10 11:26:02 -07001037 int left = topLeft[0];
1038 int top = topLeft[1];
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001039
Winson Chungb8c69f32011-10-19 21:36:08 -07001040 if (v != null && dragOffset == null) {
Adam Cohen99e8b402011-03-25 19:23:43 -07001041 // When drawing the drag outline, it did not account for margin offsets
1042 // added by the view's parent.
1043 MarginLayoutParams lp = (MarginLayoutParams) v.getLayoutParams();
1044 left += lp.leftMargin;
1045 top += lp.topMargin;
Winson Chung150fbab2010-09-29 17:14:26 -07001046
Adam Cohen99e8b402011-03-25 19:23:43 -07001047 // Offsets due to the size difference between the View and the dragOutline.
1048 // There is a size difference to account for the outer blur, which may lie
1049 // outside the bounds of the view.
Winson Chunga9abd0e2010-10-27 17:18:37 -07001050 top += (v.getHeight() - dragOutline.getHeight()) / 2;
Adam Cohenae915ce2011-08-25 13:47:22 -07001051 // We center about the x axis
1052 left += ((mCellWidth * spanX) + ((spanX - 1) * mWidthGap)
1053 - dragOutline.getWidth()) / 2;
Adam Cohen66396872011-04-15 17:50:36 -07001054 } else {
Winson Chungb8c69f32011-10-19 21:36:08 -07001055 if (dragOffset != null && dragRegion != null) {
1056 // Center the drag region *horizontally* in the cell and apply a drag
1057 // outline offset
1058 left += dragOffset.x + ((mCellWidth * spanX) + ((spanX - 1) * mWidthGap)
1059 - dragRegion.width()) / 2;
Winson Chung69737c32013-10-08 17:00:19 -07001060 int cHeight = getShortcutsAndWidgets().getCellContentHeight();
1061 int cellPaddingY = (int) Math.max(0, ((mCellHeight - cHeight) / 2f));
1062 top += dragOffset.y + cellPaddingY;
Winson Chungb8c69f32011-10-19 21:36:08 -07001063 } else {
1064 // Center the drag outline in the cell
1065 left += ((mCellWidth * spanX) + ((spanX - 1) * mWidthGap)
1066 - dragOutline.getWidth()) / 2;
1067 top += ((mCellHeight * spanY) + ((spanY - 1) * mHeightGap)
1068 - dragOutline.getHeight()) / 2;
1069 }
Winson Chunga9abd0e2010-10-27 17:18:37 -07001070 }
Joe Onorato4be866d2010-10-10 11:26:02 -07001071 final int oldIndex = mDragOutlineCurrent;
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -07001072 mDragOutlineAnims[oldIndex].animateOut();
1073 mDragOutlineCurrent = (oldIndex + 1) % mDragOutlines.length;
Adam Cohend41fbf52012-02-16 23:53:59 -08001074 Rect r = mDragOutlines[mDragOutlineCurrent];
1075 r.set(left, top, left + dragOutline.getWidth(), top + dragOutline.getHeight());
1076 if (resize) {
Adam Cohen482ed822012-03-02 14:15:13 -08001077 cellToRect(cellX, cellY, spanX, spanY, r);
Adam Cohend41fbf52012-02-16 23:53:59 -08001078 }
Winson Chung150fbab2010-09-29 17:14:26 -07001079
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -07001080 mDragOutlineAnims[mDragOutlineCurrent].setTag(dragOutline);
1081 mDragOutlineAnims[mDragOutlineCurrent].animateIn();
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001082 }
1083 }
1084
Adam Cohene0310962011-04-18 16:15:31 -07001085 public void clearDragOutlines() {
1086 final int oldIndex = mDragOutlineCurrent;
1087 mDragOutlineAnims[oldIndex].animateOut();
Adam Cohend41fbf52012-02-16 23:53:59 -08001088 mDragCell[0] = mDragCell[1] = -1;
Adam Cohene0310962011-04-18 16:15:31 -07001089 }
1090
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001091 /**
Jeff Sharkey70864282009-04-07 21:08:40 -07001092 * Find a vacant area that will fit the given bounds nearest the requested
1093 * cell location. Uses Euclidean distance to score multiple vacant areas.
Winson Chungaafa03c2010-06-11 17:34:16 -07001094 *
Romain Guy51afc022009-05-04 18:03:43 -07001095 * @param pixelX The X location at which you want to search for a vacant area.
1096 * @param pixelY The Y location at which you want to search for a vacant area.
Jeff Sharkey70864282009-04-07 21:08:40 -07001097 * @param spanX Horizontal span of the object.
1098 * @param spanY Vertical span of the object.
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001099 * @param result Array in which to place the result, or null (in which case a new array will
1100 * be allocated)
Jeff Sharkey70864282009-04-07 21:08:40 -07001101 * @return The X, Y cell of a vacant area that can contain this object,
1102 * nearest the requested location.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001103 */
Sunny Goyalf7a29e82015-04-24 15:20:43 -07001104 int[] findNearestVacantArea(int pixelX, int pixelY, int spanX, int spanY, int[] result) {
1105 return findNearestVacantArea(pixelX, pixelY, spanX, spanY, spanX, spanY, result, null);
Michael Jurka6a1435d2010-09-27 17:35:12 -07001106 }
Winson Chungaafa03c2010-06-11 17:34:16 -07001107
Michael Jurka6a1435d2010-09-27 17:35:12 -07001108 /**
1109 * Find a vacant area that will fit the given bounds nearest the requested
1110 * cell location. Uses Euclidean distance to score multiple vacant areas.
1111 *
1112 * @param pixelX The X location at which you want to search for a vacant area.
1113 * @param pixelY The Y location at which you want to search for a vacant area.
Adam Cohend41fbf52012-02-16 23:53:59 -08001114 * @param minSpanX The minimum horizontal span required
1115 * @param minSpanY The minimum vertical span required
1116 * @param spanX Horizontal span of the object.
1117 * @param spanY Vertical span of the object.
1118 * @param result Array in which to place the result, or null (in which case a new array will
1119 * be allocated)
1120 * @return The X, Y cell of a vacant area that can contain this object,
1121 * nearest the requested location.
1122 */
1123 int[] findNearestVacantArea(int pixelX, int pixelY, int minSpanX, int minSpanY, int spanX,
1124 int spanY, int[] result, int[] resultSpan) {
Sunny Goyalf7a29e82015-04-24 15:20:43 -07001125 return findNearestArea(pixelX, pixelY, minSpanX, minSpanY, spanX, spanY, true,
Adam Cohend41fbf52012-02-16 23:53:59 -08001126 result, resultSpan);
1127 }
1128
Adam Cohend41fbf52012-02-16 23:53:59 -08001129 private final Stack<Rect> mTempRectStack = new Stack<Rect>();
1130 private void lazyInitTempRectStack() {
1131 if (mTempRectStack.isEmpty()) {
1132 for (int i = 0; i < mCountX * mCountY; i++) {
1133 mTempRectStack.push(new Rect());
1134 }
1135 }
1136 }
Adam Cohen482ed822012-03-02 14:15:13 -08001137
Adam Cohend41fbf52012-02-16 23:53:59 -08001138 private void recycleTempRects(Stack<Rect> used) {
1139 while (!used.isEmpty()) {
1140 mTempRectStack.push(used.pop());
1141 }
1142 }
1143
1144 /**
1145 * Find a vacant area that will fit the given bounds nearest the requested
1146 * cell location. Uses Euclidean distance to score multiple vacant areas.
1147 *
1148 * @param pixelX The X location at which you want to search for a vacant area.
1149 * @param pixelY The Y location at which you want to search for a vacant area.
1150 * @param minSpanX The minimum horizontal span required
1151 * @param minSpanY The minimum vertical span required
1152 * @param spanX Horizontal span of the object.
1153 * @param spanY Vertical span of the object.
1154 * @param ignoreOccupied If true, the result can be an occupied cell
1155 * @param result Array in which to place the result, or null (in which case a new array will
1156 * be allocated)
1157 * @return The X, Y cell of a vacant area that can contain this object,
1158 * nearest the requested location.
1159 */
Sunny Goyalf7a29e82015-04-24 15:20:43 -07001160 private int[] findNearestArea(int pixelX, int pixelY, int minSpanX, int minSpanY, int spanX,
1161 int spanY, boolean ignoreOccupied, int[] result, int[] resultSpan) {
Adam Cohend41fbf52012-02-16 23:53:59 -08001162 lazyInitTempRectStack();
Michael Jurkac6ee42e2010-09-30 12:04:50 -07001163
Adam Cohene3e27a82011-04-15 12:07:39 -07001164 // For items with a spanX / spanY > 1, the passed in point (pixelX, pixelY) corresponds
1165 // to the center of the item, but we are searching based on the top-left cell, so
1166 // we translate the point over to correspond to the top-left.
1167 pixelX -= (mCellWidth + mWidthGap) * (spanX - 1) / 2f;
1168 pixelY -= (mCellHeight + mHeightGap) * (spanY - 1) / 2f;
1169
Jeff Sharkey70864282009-04-07 21:08:40 -07001170 // Keep track of best-scoring drop area
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001171 final int[] bestXY = result != null ? result : new int[2];
Jeff Sharkey70864282009-04-07 21:08:40 -07001172 double bestDistance = Double.MAX_VALUE;
Adam Cohend41fbf52012-02-16 23:53:59 -08001173 final Rect bestRect = new Rect(-1, -1, -1, -1);
1174 final Stack<Rect> validRegions = new Stack<Rect>();
Winson Chungaafa03c2010-06-11 17:34:16 -07001175
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001176 final int countX = mCountX;
1177 final int countY = mCountY;
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001178
Adam Cohend41fbf52012-02-16 23:53:59 -08001179 if (minSpanX <= 0 || minSpanY <= 0 || spanX <= 0 || spanY <= 0 ||
1180 spanX < minSpanX || spanY < minSpanY) {
1181 return bestXY;
1182 }
1183
1184 for (int y = 0; y < countY - (minSpanY - 1); y++) {
Michael Jurkac28de512010-08-13 11:27:44 -07001185 inner:
Adam Cohend41fbf52012-02-16 23:53:59 -08001186 for (int x = 0; x < countX - (minSpanX - 1); x++) {
1187 int ySize = -1;
1188 int xSize = -1;
Adam Cohendf035382011-04-11 17:22:04 -07001189 if (ignoreOccupied) {
Adam Cohend41fbf52012-02-16 23:53:59 -08001190 // First, let's see if this thing fits anywhere
1191 for (int i = 0; i < minSpanX; i++) {
1192 for (int j = 0; j < minSpanY; j++) {
Sunny Goyalf7a29e82015-04-24 15:20:43 -07001193 if (mOccupied[x + i][y + j]) {
Adam Cohendf035382011-04-11 17:22:04 -07001194 continue inner;
1195 }
Michael Jurkac28de512010-08-13 11:27:44 -07001196 }
1197 }
Adam Cohend41fbf52012-02-16 23:53:59 -08001198 xSize = minSpanX;
1199 ySize = minSpanY;
1200
1201 // We know that the item will fit at _some_ acceptable size, now let's see
1202 // how big we can make it. We'll alternate between incrementing x and y spans
1203 // until we hit a limit.
1204 boolean incX = true;
1205 boolean hitMaxX = xSize >= spanX;
1206 boolean hitMaxY = ySize >= spanY;
1207 while (!(hitMaxX && hitMaxY)) {
1208 if (incX && !hitMaxX) {
1209 for (int j = 0; j < ySize; j++) {
Sunny Goyalf7a29e82015-04-24 15:20:43 -07001210 if (x + xSize > countX -1 || mOccupied[x + xSize][y + j]) {
Adam Cohend41fbf52012-02-16 23:53:59 -08001211 // We can't move out horizontally
1212 hitMaxX = true;
1213 }
1214 }
1215 if (!hitMaxX) {
1216 xSize++;
1217 }
1218 } else if (!hitMaxY) {
1219 for (int i = 0; i < xSize; i++) {
Sunny Goyalf7a29e82015-04-24 15:20:43 -07001220 if (y + ySize > countY - 1 || mOccupied[x + i][y + ySize]) {
Adam Cohend41fbf52012-02-16 23:53:59 -08001221 // We can't move out vertically
1222 hitMaxY = true;
1223 }
1224 }
1225 if (!hitMaxY) {
1226 ySize++;
1227 }
1228 }
1229 hitMaxX |= xSize >= spanX;
1230 hitMaxY |= ySize >= spanY;
1231 incX = !incX;
1232 }
1233 incX = true;
1234 hitMaxX = xSize >= spanX;
1235 hitMaxY = ySize >= spanY;
Michael Jurkac28de512010-08-13 11:27:44 -07001236 }
Sunny Goyal2805e632015-05-20 15:35:32 -07001237 final int[] cellXY = mTmpPoint;
Adam Cohene3e27a82011-04-15 12:07:39 -07001238 cellToCenterPoint(x, y, cellXY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001239
Adam Cohend41fbf52012-02-16 23:53:59 -08001240 // We verify that the current rect is not a sub-rect of any of our previous
1241 // candidates. In this case, the current rect is disqualified in favour of the
1242 // containing rect.
1243 Rect currentRect = mTempRectStack.pop();
1244 currentRect.set(x, y, x + xSize, y + ySize);
1245 boolean contained = false;
1246 for (Rect r : validRegions) {
1247 if (r.contains(currentRect)) {
1248 contained = true;
1249 break;
1250 }
1251 }
1252 validRegions.push(currentRect);
Sunny Goyalf7a29e82015-04-24 15:20:43 -07001253 double distance = Math.hypot(cellXY[0] - pixelX, cellXY[1] - pixelY);
Adam Cohen482ed822012-03-02 14:15:13 -08001254
Adam Cohend41fbf52012-02-16 23:53:59 -08001255 if ((distance <= bestDistance && !contained) ||
1256 currentRect.contains(bestRect)) {
Michael Jurkac28de512010-08-13 11:27:44 -07001257 bestDistance = distance;
1258 bestXY[0] = x;
1259 bestXY[1] = y;
Adam Cohend41fbf52012-02-16 23:53:59 -08001260 if (resultSpan != null) {
1261 resultSpan[0] = xSize;
1262 resultSpan[1] = ySize;
1263 }
1264 bestRect.set(currentRect);
Michael Jurkac28de512010-08-13 11:27:44 -07001265 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001266 }
1267 }
1268
Adam Cohenc0dcf592011-06-01 15:30:43 -07001269 // Return -1, -1 if no suitable location found
1270 if (bestDistance == Double.MAX_VALUE) {
1271 bestXY[0] = -1;
1272 bestXY[1] = -1;
Jeff Sharkey70864282009-04-07 21:08:40 -07001273 }
Adam Cohend41fbf52012-02-16 23:53:59 -08001274 recycleTempRects(validRegions);
Adam Cohenc0dcf592011-06-01 15:30:43 -07001275 return bestXY;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001276 }
Winson Chungaafa03c2010-06-11 17:34:16 -07001277
Adam Cohen482ed822012-03-02 14:15:13 -08001278 /**
1279 * Find a vacant area that will fit the given bounds nearest the requested
1280 * cell location, and will also weigh in a suggested direction vector of the
1281 * desired location. This method computers distance based on unit grid distances,
1282 * not pixel distances.
1283 *
Adam Cohen47a876d2012-03-19 13:21:41 -07001284 * @param cellX The X cell nearest to which you want to search for a vacant area.
1285 * @param cellY The Y cell nearest which you want to search for a vacant area.
Adam Cohen482ed822012-03-02 14:15:13 -08001286 * @param spanX Horizontal span of the object.
1287 * @param spanY Vertical span of the object.
Adam Cohen47a876d2012-03-19 13:21:41 -07001288 * @param direction The favored direction in which the views should move from x, y
1289 * @param exactDirectionOnly If this parameter is true, then only solutions where the direction
1290 * matches exactly. Otherwise we find the best matching direction.
1291 * @param occoupied The array which represents which cells in the CellLayout are occupied
1292 * @param blockOccupied The array which represents which cells in the specified block (cellX,
Winson Chung5f8afe62013-08-12 16:19:28 -07001293 * cellY, spanX, spanY) are occupied. This is used when try to move a group of views.
Adam Cohen482ed822012-03-02 14:15:13 -08001294 * @param result Array in which to place the result, or null (in which case a new array will
1295 * be allocated)
1296 * @return The X, Y cell of a vacant area that can contain this object,
1297 * nearest the requested location.
1298 */
1299 private int[] findNearestArea(int cellX, int cellY, int spanX, int spanY, int[] direction,
Adam Cohen47a876d2012-03-19 13:21:41 -07001300 boolean[][] occupied, boolean blockOccupied[][], int[] result) {
Adam Cohen482ed822012-03-02 14:15:13 -08001301 // Keep track of best-scoring drop area
1302 final int[] bestXY = result != null ? result : new int[2];
1303 float bestDistance = Float.MAX_VALUE;
1304 int bestDirectionScore = Integer.MIN_VALUE;
1305
1306 final int countX = mCountX;
1307 final int countY = mCountY;
1308
1309 for (int y = 0; y < countY - (spanY - 1); y++) {
1310 inner:
1311 for (int x = 0; x < countX - (spanX - 1); x++) {
1312 // First, let's see if this thing fits anywhere
1313 for (int i = 0; i < spanX; i++) {
1314 for (int j = 0; j < spanY; j++) {
Adam Cohen47a876d2012-03-19 13:21:41 -07001315 if (occupied[x + i][y + j] && (blockOccupied == null || blockOccupied[i][j])) {
Adam Cohen482ed822012-03-02 14:15:13 -08001316 continue inner;
1317 }
1318 }
1319 }
1320
Sunny Goyalf7a29e82015-04-24 15:20:43 -07001321 float distance = (float) Math.hypot(x - cellX, y - cellY);
Adam Cohen482ed822012-03-02 14:15:13 -08001322 int[] curDirection = mTmpPoint;
Adam Cohen47a876d2012-03-19 13:21:41 -07001323 computeDirectionVector(x - cellX, y - cellY, curDirection);
1324 // The direction score is just the dot product of the two candidate direction
1325 // and that passed in.
Adam Cohen482ed822012-03-02 14:15:13 -08001326 int curDirectionScore = direction[0] * curDirection[0] +
1327 direction[1] * curDirection[1];
Adam Cohen47a876d2012-03-19 13:21:41 -07001328 boolean exactDirectionOnly = false;
1329 boolean directionMatches = direction[0] == curDirection[0] &&
1330 direction[0] == curDirection[0];
1331 if ((directionMatches || !exactDirectionOnly) &&
1332 Float.compare(distance, bestDistance) < 0 || (Float.compare(distance,
Adam Cohen482ed822012-03-02 14:15:13 -08001333 bestDistance) == 0 && curDirectionScore > bestDirectionScore)) {
1334 bestDistance = distance;
1335 bestDirectionScore = curDirectionScore;
1336 bestXY[0] = x;
1337 bestXY[1] = y;
1338 }
1339 }
1340 }
1341
1342 // Return -1, -1 if no suitable location found
1343 if (bestDistance == Float.MAX_VALUE) {
1344 bestXY[0] = -1;
1345 bestXY[1] = -1;
1346 }
1347 return bestXY;
1348 }
1349
1350 private boolean addViewToTempLocation(View v, Rect rectOccupiedByPotentialDrop,
Adam Cohen8baab352012-03-20 17:39:21 -07001351 int[] direction, ItemConfiguration currentState) {
1352 CellAndSpan c = currentState.map.get(v);
Adam Cohen482ed822012-03-02 14:15:13 -08001353 boolean success = false;
Adam Cohen8baab352012-03-20 17:39:21 -07001354 markCellsForView(c.x, c.y, c.spanX, c.spanY, mTmpOccupied, false);
Adam Cohen482ed822012-03-02 14:15:13 -08001355 markCellsForRect(rectOccupiedByPotentialDrop, mTmpOccupied, true);
1356
Adam Cohen8baab352012-03-20 17:39:21 -07001357 findNearestArea(c.x, c.y, c.spanX, c.spanY, direction, mTmpOccupied, null, mTempLocation);
Adam Cohen482ed822012-03-02 14:15:13 -08001358
1359 if (mTempLocation[0] >= 0 && mTempLocation[1] >= 0) {
Adam Cohen8baab352012-03-20 17:39:21 -07001360 c.x = mTempLocation[0];
1361 c.y = mTempLocation[1];
Adam Cohen482ed822012-03-02 14:15:13 -08001362 success = true;
Adam Cohen482ed822012-03-02 14:15:13 -08001363 }
Adam Cohen8baab352012-03-20 17:39:21 -07001364 markCellsForView(c.x, c.y, c.spanX, c.spanY, mTmpOccupied, true);
Adam Cohen482ed822012-03-02 14:15:13 -08001365 return success;
1366 }
1367
Adam Cohenf3900c22012-11-16 18:28:11 -08001368 /**
1369 * This helper class defines a cluster of views. It helps with defining complex edges
1370 * of the cluster and determining how those edges interact with other views. The edges
1371 * essentially define a fine-grained boundary around the cluster of views -- like a more
1372 * precise version of a bounding box.
1373 */
1374 private class ViewCluster {
1375 final static int LEFT = 0;
1376 final static int TOP = 1;
1377 final static int RIGHT = 2;
1378 final static int BOTTOM = 3;
Adam Cohen47a876d2012-03-19 13:21:41 -07001379
Adam Cohenf3900c22012-11-16 18:28:11 -08001380 ArrayList<View> views;
1381 ItemConfiguration config;
1382 Rect boundingRect = new Rect();
Adam Cohen47a876d2012-03-19 13:21:41 -07001383
Adam Cohenf3900c22012-11-16 18:28:11 -08001384 int[] leftEdge = new int[mCountY];
1385 int[] rightEdge = new int[mCountY];
1386 int[] topEdge = new int[mCountX];
1387 int[] bottomEdge = new int[mCountX];
1388 boolean leftEdgeDirty, rightEdgeDirty, topEdgeDirty, bottomEdgeDirty, boundingRectDirty;
1389
1390 @SuppressWarnings("unchecked")
1391 public ViewCluster(ArrayList<View> views, ItemConfiguration config) {
1392 this.views = (ArrayList<View>) views.clone();
1393 this.config = config;
1394 resetEdges();
Adam Cohen47a876d2012-03-19 13:21:41 -07001395 }
1396
Adam Cohenf3900c22012-11-16 18:28:11 -08001397 void resetEdges() {
1398 for (int i = 0; i < mCountX; i++) {
1399 topEdge[i] = -1;
1400 bottomEdge[i] = -1;
1401 }
1402 for (int i = 0; i < mCountY; i++) {
1403 leftEdge[i] = -1;
1404 rightEdge[i] = -1;
1405 }
1406 leftEdgeDirty = true;
1407 rightEdgeDirty = true;
1408 bottomEdgeDirty = true;
1409 topEdgeDirty = true;
1410 boundingRectDirty = true;
1411 }
Adam Cohen47a876d2012-03-19 13:21:41 -07001412
Adam Cohenf3900c22012-11-16 18:28:11 -08001413 void computeEdge(int which, int[] edge) {
1414 int count = views.size();
1415 for (int i = 0; i < count; i++) {
1416 CellAndSpan cs = config.map.get(views.get(i));
1417 switch (which) {
1418 case LEFT:
1419 int left = cs.x;
1420 for (int j = cs.y; j < cs.y + cs.spanY; j++) {
1421 if (left < edge[j] || edge[j] < 0) {
1422 edge[j] = left;
Adam Cohena56dc102012-07-13 13:41:42 -07001423 }
Adam Cohen47a876d2012-03-19 13:21:41 -07001424 }
Adam Cohenf3900c22012-11-16 18:28:11 -08001425 break;
1426 case RIGHT:
1427 int right = cs.x + cs.spanX;
1428 for (int j = cs.y; j < cs.y + cs.spanY; j++) {
1429 if (right > edge[j]) {
1430 edge[j] = right;
1431 }
1432 }
1433 break;
1434 case TOP:
1435 int top = cs.y;
1436 for (int j = cs.x; j < cs.x + cs.spanX; j++) {
1437 if (top < edge[j] || edge[j] < 0) {
1438 edge[j] = top;
1439 }
1440 }
1441 break;
1442 case BOTTOM:
1443 int bottom = cs.y + cs.spanY;
1444 for (int j = cs.x; j < cs.x + cs.spanX; j++) {
1445 if (bottom > edge[j]) {
1446 edge[j] = bottom;
1447 }
1448 }
1449 break;
Adam Cohen47a876d2012-03-19 13:21:41 -07001450 }
1451 }
1452 }
Adam Cohenf3900c22012-11-16 18:28:11 -08001453
1454 boolean isViewTouchingEdge(View v, int whichEdge) {
1455 CellAndSpan cs = config.map.get(v);
1456
1457 int[] edge = getEdge(whichEdge);
1458
1459 switch (whichEdge) {
1460 case LEFT:
1461 for (int i = cs.y; i < cs.y + cs.spanY; i++) {
1462 if (edge[i] == cs.x + cs.spanX) {
1463 return true;
1464 }
1465 }
1466 break;
1467 case RIGHT:
1468 for (int i = cs.y; i < cs.y + cs.spanY; i++) {
1469 if (edge[i] == cs.x) {
1470 return true;
1471 }
1472 }
1473 break;
1474 case TOP:
1475 for (int i = cs.x; i < cs.x + cs.spanX; i++) {
1476 if (edge[i] == cs.y + cs.spanY) {
1477 return true;
1478 }
1479 }
1480 break;
1481 case BOTTOM:
1482 for (int i = cs.x; i < cs.x + cs.spanX; i++) {
1483 if (edge[i] == cs.y) {
1484 return true;
1485 }
1486 }
1487 break;
1488 }
1489 return false;
1490 }
1491
1492 void shift(int whichEdge, int delta) {
1493 for (View v: views) {
1494 CellAndSpan c = config.map.get(v);
1495 switch (whichEdge) {
1496 case LEFT:
1497 c.x -= delta;
1498 break;
1499 case RIGHT:
1500 c.x += delta;
1501 break;
1502 case TOP:
1503 c.y -= delta;
1504 break;
1505 case BOTTOM:
1506 default:
1507 c.y += delta;
1508 break;
1509 }
1510 }
1511 resetEdges();
1512 }
1513
1514 public void addView(View v) {
1515 views.add(v);
1516 resetEdges();
1517 }
1518
1519 public Rect getBoundingRect() {
1520 if (boundingRectDirty) {
1521 boolean first = true;
1522 for (View v: views) {
1523 CellAndSpan c = config.map.get(v);
1524 if (first) {
1525 boundingRect.set(c.x, c.y, c.x + c.spanX, c.y + c.spanY);
1526 first = false;
1527 } else {
1528 boundingRect.union(c.x, c.y, c.x + c.spanX, c.y + c.spanY);
1529 }
1530 }
1531 }
1532 return boundingRect;
1533 }
1534
1535 public int[] getEdge(int which) {
1536 switch (which) {
1537 case LEFT:
1538 return getLeftEdge();
1539 case RIGHT:
1540 return getRightEdge();
1541 case TOP:
1542 return getTopEdge();
1543 case BOTTOM:
1544 default:
1545 return getBottomEdge();
1546 }
1547 }
1548
1549 public int[] getLeftEdge() {
1550 if (leftEdgeDirty) {
1551 computeEdge(LEFT, leftEdge);
1552 }
1553 return leftEdge;
1554 }
1555
1556 public int[] getRightEdge() {
1557 if (rightEdgeDirty) {
1558 computeEdge(RIGHT, rightEdge);
1559 }
1560 return rightEdge;
1561 }
1562
1563 public int[] getTopEdge() {
1564 if (topEdgeDirty) {
1565 computeEdge(TOP, topEdge);
1566 }
1567 return topEdge;
1568 }
1569
1570 public int[] getBottomEdge() {
1571 if (bottomEdgeDirty) {
1572 computeEdge(BOTTOM, bottomEdge);
1573 }
1574 return bottomEdge;
1575 }
1576
1577 PositionComparator comparator = new PositionComparator();
1578 class PositionComparator implements Comparator<View> {
1579 int whichEdge = 0;
1580 public int compare(View left, View right) {
1581 CellAndSpan l = config.map.get(left);
1582 CellAndSpan r = config.map.get(right);
1583 switch (whichEdge) {
1584 case LEFT:
1585 return (r.x + r.spanX) - (l.x + l.spanX);
1586 case RIGHT:
1587 return l.x - r.x;
1588 case TOP:
1589 return (r.y + r.spanY) - (l.y + l.spanY);
1590 case BOTTOM:
1591 default:
1592 return l.y - r.y;
1593 }
1594 }
1595 }
1596
1597 public void sortConfigurationForEdgePush(int edge) {
1598 comparator.whichEdge = edge;
1599 Collections.sort(config.sortedViews, comparator);
1600 }
Adam Cohen47a876d2012-03-19 13:21:41 -07001601 }
1602
Adam Cohenf3900c22012-11-16 18:28:11 -08001603 private boolean pushViewsToTempLocation(ArrayList<View> views, Rect rectOccupiedByPotentialDrop,
1604 int[] direction, View dragView, ItemConfiguration currentState) {
Adam Cohene0489502012-08-27 15:18:53 -07001605
Adam Cohenf3900c22012-11-16 18:28:11 -08001606 ViewCluster cluster = new ViewCluster(views, currentState);
1607 Rect clusterRect = cluster.getBoundingRect();
1608 int whichEdge;
1609 int pushDistance;
1610 boolean fail = false;
1611
1612 // Determine the edge of the cluster that will be leading the push and how far
1613 // the cluster must be shifted.
1614 if (direction[0] < 0) {
1615 whichEdge = ViewCluster.LEFT;
1616 pushDistance = clusterRect.right - rectOccupiedByPotentialDrop.left;
Adam Cohene0489502012-08-27 15:18:53 -07001617 } else if (direction[0] > 0) {
Adam Cohenf3900c22012-11-16 18:28:11 -08001618 whichEdge = ViewCluster.RIGHT;
1619 pushDistance = rectOccupiedByPotentialDrop.right - clusterRect.left;
1620 } else if (direction[1] < 0) {
1621 whichEdge = ViewCluster.TOP;
1622 pushDistance = clusterRect.bottom - rectOccupiedByPotentialDrop.top;
1623 } else {
1624 whichEdge = ViewCluster.BOTTOM;
1625 pushDistance = rectOccupiedByPotentialDrop.bottom - clusterRect.top;
Adam Cohene0489502012-08-27 15:18:53 -07001626 }
1627
Adam Cohenf3900c22012-11-16 18:28:11 -08001628 // Break early for invalid push distance.
1629 if (pushDistance <= 0) {
1630 return false;
Adam Cohene0489502012-08-27 15:18:53 -07001631 }
Adam Cohenf3900c22012-11-16 18:28:11 -08001632
1633 // Mark the occupied state as false for the group of views we want to move.
1634 for (View v: views) {
1635 CellAndSpan c = currentState.map.get(v);
1636 markCellsForView(c.x, c.y, c.spanX, c.spanY, mTmpOccupied, false);
1637 }
1638
1639 // We save the current configuration -- if we fail to find a solution we will revert
1640 // to the initial state. The process of finding a solution modifies the configuration
1641 // in place, hence the need for revert in the failure case.
1642 currentState.save();
1643
1644 // The pushing algorithm is simplified by considering the views in the order in which
1645 // they would be pushed by the cluster. For example, if the cluster is leading with its
1646 // left edge, we consider sort the views by their right edge, from right to left.
1647 cluster.sortConfigurationForEdgePush(whichEdge);
1648
1649 while (pushDistance > 0 && !fail) {
1650 for (View v: currentState.sortedViews) {
1651 // For each view that isn't in the cluster, we see if the leading edge of the
1652 // cluster is contacting the edge of that view. If so, we add that view to the
1653 // cluster.
1654 if (!cluster.views.contains(v) && v != dragView) {
1655 if (cluster.isViewTouchingEdge(v, whichEdge)) {
1656 LayoutParams lp = (LayoutParams) v.getLayoutParams();
1657 if (!lp.canReorder) {
1658 // The push solution includes the all apps button, this is not viable.
1659 fail = true;
1660 break;
1661 }
1662 cluster.addView(v);
1663 CellAndSpan c = currentState.map.get(v);
1664
1665 // Adding view to cluster, mark it as not occupied.
1666 markCellsForView(c.x, c.y, c.spanX, c.spanY, mTmpOccupied, false);
1667 }
1668 }
1669 }
1670 pushDistance--;
1671
1672 // The cluster has been completed, now we move the whole thing over in the appropriate
1673 // direction.
1674 cluster.shift(whichEdge, 1);
1675 }
1676
1677 boolean foundSolution = false;
1678 clusterRect = cluster.getBoundingRect();
1679
1680 // Due to the nature of the algorithm, the only check required to verify a valid solution
1681 // is to ensure that completed shifted cluster lies completely within the cell layout.
1682 if (!fail && clusterRect.left >= 0 && clusterRect.right <= mCountX && clusterRect.top >= 0 &&
1683 clusterRect.bottom <= mCountY) {
1684 foundSolution = true;
1685 } else {
1686 currentState.restore();
1687 }
1688
1689 // In either case, we set the occupied array as marked for the location of the views
1690 for (View v: cluster.views) {
1691 CellAndSpan c = currentState.map.get(v);
1692 markCellsForView(c.x, c.y, c.spanX, c.spanY, mTmpOccupied, true);
1693 }
1694
1695 return foundSolution;
Adam Cohene0489502012-08-27 15:18:53 -07001696 }
1697
Adam Cohen482ed822012-03-02 14:15:13 -08001698 private boolean addViewsToTempLocation(ArrayList<View> views, Rect rectOccupiedByPotentialDrop,
Adam Cohenf3900c22012-11-16 18:28:11 -08001699 int[] direction, View dragView, ItemConfiguration currentState) {
Adam Cohen482ed822012-03-02 14:15:13 -08001700 if (views.size() == 0) return true;
Adam Cohen482ed822012-03-02 14:15:13 -08001701
Adam Cohen8baab352012-03-20 17:39:21 -07001702 boolean success = false;
Adam Cohen482ed822012-03-02 14:15:13 -08001703 Rect boundingRect = null;
Adam Cohen8baab352012-03-20 17:39:21 -07001704 // We construct a rect which represents the entire group of views passed in
Adam Cohen482ed822012-03-02 14:15:13 -08001705 for (View v: views) {
Adam Cohen8baab352012-03-20 17:39:21 -07001706 CellAndSpan c = currentState.map.get(v);
Adam Cohen482ed822012-03-02 14:15:13 -08001707 if (boundingRect == null) {
Adam Cohen8baab352012-03-20 17:39:21 -07001708 boundingRect = new Rect(c.x, c.y, c.x + c.spanX, c.y + c.spanY);
Adam Cohen482ed822012-03-02 14:15:13 -08001709 } else {
Adam Cohen8baab352012-03-20 17:39:21 -07001710 boundingRect.union(c.x, c.y, c.x + c.spanX, c.y + c.spanY);
Adam Cohen482ed822012-03-02 14:15:13 -08001711 }
1712 }
Adam Cohen8baab352012-03-20 17:39:21 -07001713
Adam Cohen8baab352012-03-20 17:39:21 -07001714 // Mark the occupied state as false for the group of views we want to move.
Adam Cohenf3900c22012-11-16 18:28:11 -08001715 for (View v: views) {
Adam Cohen8baab352012-03-20 17:39:21 -07001716 CellAndSpan c = currentState.map.get(v);
1717 markCellsForView(c.x, c.y, c.spanX, c.spanY, mTmpOccupied, false);
1718 }
1719
Adam Cohen47a876d2012-03-19 13:21:41 -07001720 boolean[][] blockOccupied = new boolean[boundingRect.width()][boundingRect.height()];
1721 int top = boundingRect.top;
1722 int left = boundingRect.left;
Adam Cohen8baab352012-03-20 17:39:21 -07001723 // We mark more precisely which parts of the bounding rect are truly occupied, allowing
Adam Cohena56dc102012-07-13 13:41:42 -07001724 // for interlocking.
Adam Cohenf3900c22012-11-16 18:28:11 -08001725 for (View v: views) {
Adam Cohen8baab352012-03-20 17:39:21 -07001726 CellAndSpan c = currentState.map.get(v);
1727 markCellsForView(c.x - left, c.y - top, c.spanX, c.spanY, blockOccupied, true);
Adam Cohen47a876d2012-03-19 13:21:41 -07001728 }
1729
Adam Cohen482ed822012-03-02 14:15:13 -08001730 markCellsForRect(rectOccupiedByPotentialDrop, mTmpOccupied, true);
1731
Adam Cohenf3900c22012-11-16 18:28:11 -08001732 findNearestArea(boundingRect.left, boundingRect.top, boundingRect.width(),
1733 boundingRect.height(), direction, mTmpOccupied, blockOccupied, mTempLocation);
Adam Cohen482ed822012-03-02 14:15:13 -08001734
Adam Cohen8baab352012-03-20 17:39:21 -07001735 // If we successfuly found a location by pushing the block of views, we commit it
Adam Cohen482ed822012-03-02 14:15:13 -08001736 if (mTempLocation[0] >= 0 && mTempLocation[1] >= 0) {
Adam Cohen8baab352012-03-20 17:39:21 -07001737 int deltaX = mTempLocation[0] - boundingRect.left;
1738 int deltaY = mTempLocation[1] - boundingRect.top;
Adam Cohenf3900c22012-11-16 18:28:11 -08001739 for (View v: views) {
Adam Cohen8baab352012-03-20 17:39:21 -07001740 CellAndSpan c = currentState.map.get(v);
1741 c.x += deltaX;
1742 c.y += deltaY;
Adam Cohen482ed822012-03-02 14:15:13 -08001743 }
1744 success = true;
1745 }
Adam Cohen8baab352012-03-20 17:39:21 -07001746
1747 // In either case, we set the occupied array as marked for the location of the views
Adam Cohenf3900c22012-11-16 18:28:11 -08001748 for (View v: views) {
Adam Cohen8baab352012-03-20 17:39:21 -07001749 CellAndSpan c = currentState.map.get(v);
1750 markCellsForView(c.x, c.y, c.spanX, c.spanY, mTmpOccupied, true);
Adam Cohen482ed822012-03-02 14:15:13 -08001751 }
1752 return success;
1753 }
1754
1755 private void markCellsForRect(Rect r, boolean[][] occupied, boolean value) {
1756 markCellsForView(r.left, r.top, r.width(), r.height(), occupied, value);
1757 }
1758
Adam Cohen4abc5bd2012-05-29 21:06:03 -07001759 // This method tries to find a reordering solution which satisfies the push mechanic by trying
1760 // to push items in each of the cardinal directions, in an order based on the direction vector
1761 // passed.
1762 private boolean attemptPushInDirection(ArrayList<View> intersectingViews, Rect occupied,
1763 int[] direction, View ignoreView, ItemConfiguration solution) {
1764 if ((Math.abs(direction[0]) + Math.abs(direction[1])) > 1) {
Winson Chung5f8afe62013-08-12 16:19:28 -07001765 // If the direction vector has two non-zero components, we try pushing
Adam Cohen4abc5bd2012-05-29 21:06:03 -07001766 // separately in each of the components.
1767 int temp = direction[1];
1768 direction[1] = 0;
Adam Cohenf3900c22012-11-16 18:28:11 -08001769
1770 if (pushViewsToTempLocation(intersectingViews, occupied, direction,
Adam Cohen4abc5bd2012-05-29 21:06:03 -07001771 ignoreView, solution)) {
1772 return true;
1773 }
1774 direction[1] = temp;
1775 temp = direction[0];
1776 direction[0] = 0;
Adam Cohenf3900c22012-11-16 18:28:11 -08001777
1778 if (pushViewsToTempLocation(intersectingViews, occupied, direction,
Adam Cohen4abc5bd2012-05-29 21:06:03 -07001779 ignoreView, solution)) {
1780 return true;
1781 }
1782 // Revert the direction
1783 direction[0] = temp;
1784
1785 // Now we try pushing in each component of the opposite direction
1786 direction[0] *= -1;
1787 direction[1] *= -1;
1788 temp = direction[1];
1789 direction[1] = 0;
Adam Cohenf3900c22012-11-16 18:28:11 -08001790 if (pushViewsToTempLocation(intersectingViews, occupied, direction,
Adam Cohen4abc5bd2012-05-29 21:06:03 -07001791 ignoreView, solution)) {
1792 return true;
1793 }
1794
1795 direction[1] = temp;
1796 temp = direction[0];
1797 direction[0] = 0;
Adam Cohenf3900c22012-11-16 18:28:11 -08001798 if (pushViewsToTempLocation(intersectingViews, occupied, direction,
Adam Cohen4abc5bd2012-05-29 21:06:03 -07001799 ignoreView, solution)) {
1800 return true;
1801 }
1802 // revert the direction
1803 direction[0] = temp;
1804 direction[0] *= -1;
1805 direction[1] *= -1;
Winson Chung5f8afe62013-08-12 16:19:28 -07001806
Adam Cohen4abc5bd2012-05-29 21:06:03 -07001807 } else {
1808 // If the direction vector has a single non-zero component, we push first in the
1809 // direction of the vector
Adam Cohenf3900c22012-11-16 18:28:11 -08001810 if (pushViewsToTempLocation(intersectingViews, occupied, direction,
Adam Cohen4abc5bd2012-05-29 21:06:03 -07001811 ignoreView, solution)) {
1812 return true;
1813 }
Adam Cohen4abc5bd2012-05-29 21:06:03 -07001814 // Then we try the opposite direction
1815 direction[0] *= -1;
1816 direction[1] *= -1;
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 // Switch the direction back
1822 direction[0] *= -1;
1823 direction[1] *= -1;
Winson Chung5f8afe62013-08-12 16:19:28 -07001824
1825 // If we have failed to find a push solution with the above, then we try
Adam Cohen4abc5bd2012-05-29 21:06:03 -07001826 // to find a solution by pushing along the perpendicular axis.
1827
1828 // Swap the components
1829 int temp = direction[1];
1830 direction[1] = direction[0];
1831 direction[0] = temp;
Adam Cohenf3900c22012-11-16 18:28:11 -08001832 if (pushViewsToTempLocation(intersectingViews, occupied, direction,
Adam Cohen4abc5bd2012-05-29 21:06:03 -07001833 ignoreView, solution)) {
1834 return true;
1835 }
1836
1837 // Then we try the opposite direction
1838 direction[0] *= -1;
1839 direction[1] *= -1;
Adam Cohenf3900c22012-11-16 18:28:11 -08001840 if (pushViewsToTempLocation(intersectingViews, occupied, direction,
Adam Cohen4abc5bd2012-05-29 21:06:03 -07001841 ignoreView, solution)) {
1842 return true;
1843 }
1844 // Switch the direction back
1845 direction[0] *= -1;
1846 direction[1] *= -1;
1847
1848 // Swap the components back
1849 temp = direction[1];
1850 direction[1] = direction[0];
1851 direction[0] = temp;
1852 }
1853 return false;
1854 }
1855
Adam Cohen482ed822012-03-02 14:15:13 -08001856 private boolean rearrangementExists(int cellX, int cellY, int spanX, int spanY, int[] direction,
Adam Cohen8baab352012-03-20 17:39:21 -07001857 View ignoreView, ItemConfiguration solution) {
Winson Chunge3e03bc2012-05-01 15:10:11 -07001858 // Return early if get invalid cell positions
1859 if (cellX < 0 || cellY < 0) return false;
Adam Cohen482ed822012-03-02 14:15:13 -08001860
Adam Cohen8baab352012-03-20 17:39:21 -07001861 mIntersectingViews.clear();
Adam Cohen482ed822012-03-02 14:15:13 -08001862 mOccupiedRect.set(cellX, cellY, cellX + spanX, cellY + spanY);
Adam Cohen482ed822012-03-02 14:15:13 -08001863
Adam Cohen8baab352012-03-20 17:39:21 -07001864 // Mark the desired location of the view currently being dragged.
Adam Cohen482ed822012-03-02 14:15:13 -08001865 if (ignoreView != null) {
Adam Cohen8baab352012-03-20 17:39:21 -07001866 CellAndSpan c = solution.map.get(ignoreView);
Adam Cohen19f37922012-03-21 11:59:11 -07001867 if (c != null) {
1868 c.x = cellX;
1869 c.y = cellY;
1870 }
Adam Cohen482ed822012-03-02 14:15:13 -08001871 }
Adam Cohen482ed822012-03-02 14:15:13 -08001872 Rect r0 = new Rect(cellX, cellY, cellX + spanX, cellY + spanY);
1873 Rect r1 = new Rect();
Adam Cohen8baab352012-03-20 17:39:21 -07001874 for (View child: solution.map.keySet()) {
Adam Cohen482ed822012-03-02 14:15:13 -08001875 if (child == ignoreView) continue;
Adam Cohen8baab352012-03-20 17:39:21 -07001876 CellAndSpan c = solution.map.get(child);
Adam Cohen482ed822012-03-02 14:15:13 -08001877 LayoutParams lp = (LayoutParams) child.getLayoutParams();
Adam Cohen8baab352012-03-20 17:39:21 -07001878 r1.set(c.x, c.y, c.x + c.spanX, c.y + c.spanY);
Adam Cohen482ed822012-03-02 14:15:13 -08001879 if (Rect.intersects(r0, r1)) {
1880 if (!lp.canReorder) {
1881 return false;
1882 }
1883 mIntersectingViews.add(child);
1884 }
1885 }
Adam Cohen47a876d2012-03-19 13:21:41 -07001886
Adam Cohenfa3c58f2013-12-06 16:10:55 -08001887 solution.intersectingViews = new ArrayList<View>(mIntersectingViews);
1888
Winson Chung5f8afe62013-08-12 16:19:28 -07001889 // First we try to find a solution which respects the push mechanic. That is,
Adam Cohen4abc5bd2012-05-29 21:06:03 -07001890 // we try to find a solution such that no displaced item travels through another item
1891 // without also displacing that item.
1892 if (attemptPushInDirection(mIntersectingViews, mOccupiedRect, direction, ignoreView,
Adam Cohen19f37922012-03-21 11:59:11 -07001893 solution)) {
Adam Cohen47a876d2012-03-19 13:21:41 -07001894 return true;
1895 }
Adam Cohen47a876d2012-03-19 13:21:41 -07001896
Adam Cohen4abc5bd2012-05-29 21:06:03 -07001897 // Next we try moving the views as a block, but without requiring the push mechanic.
Adam Cohenf3900c22012-11-16 18:28:11 -08001898 if (addViewsToTempLocation(mIntersectingViews, mOccupiedRect, direction, ignoreView,
Adam Cohen19f37922012-03-21 11:59:11 -07001899 solution)) {
Adam Cohen482ed822012-03-02 14:15:13 -08001900 return true;
1901 }
Adam Cohen47a876d2012-03-19 13:21:41 -07001902
Adam Cohen482ed822012-03-02 14:15:13 -08001903 // Ok, they couldn't move as a block, let's move them individually
1904 for (View v : mIntersectingViews) {
Adam Cohen8baab352012-03-20 17:39:21 -07001905 if (!addViewToTempLocation(v, mOccupiedRect, direction, solution)) {
Adam Cohen482ed822012-03-02 14:15:13 -08001906 return false;
1907 }
1908 }
1909 return true;
1910 }
1911
1912 /*
1913 * Returns a pair (x, y), where x,y are in {-1, 0, 1} corresponding to vector between
1914 * the provided point and the provided cell
1915 */
Adam Cohen47a876d2012-03-19 13:21:41 -07001916 private void computeDirectionVector(float deltaX, float deltaY, int[] result) {
Adam Cohen482ed822012-03-02 14:15:13 -08001917 double angle = Math.atan(((float) deltaY) / deltaX);
1918
1919 result[0] = 0;
1920 result[1] = 0;
1921 if (Math.abs(Math.cos(angle)) > 0.5f) {
1922 result[0] = (int) Math.signum(deltaX);
1923 }
1924 if (Math.abs(Math.sin(angle)) > 0.5f) {
1925 result[1] = (int) Math.signum(deltaY);
1926 }
1927 }
1928
Adam Cohen8baab352012-03-20 17:39:21 -07001929 private void copyOccupiedArray(boolean[][] occupied) {
1930 for (int i = 0; i < mCountX; i++) {
1931 for (int j = 0; j < mCountY; j++) {
1932 occupied[i][j] = mOccupied[i][j];
1933 }
1934 }
1935 }
1936
Sunny Goyalf7a29e82015-04-24 15:20:43 -07001937 private ItemConfiguration findReorderSolution(int pixelX, int pixelY, int minSpanX, int minSpanY,
Adam Cohenfa3c58f2013-12-06 16:10:55 -08001938 int spanX, int spanY, int[] direction, View dragView, boolean decX,
1939 ItemConfiguration solution) {
Adam Cohen8baab352012-03-20 17:39:21 -07001940 // Copy the current state into the solution. This solution will be manipulated as necessary.
1941 copyCurrentStateToSolution(solution, false);
1942 // Copy the current occupied array into the temporary occupied array. This array will be
1943 // manipulated as necessary to find a solution.
1944 copyOccupiedArray(mTmpOccupied);
Adam Cohen482ed822012-03-02 14:15:13 -08001945
1946 // We find the nearest cell into which we would place the dragged item, assuming there's
1947 // nothing in its way.
1948 int result[] = new int[2];
1949 result = findNearestArea(pixelX, pixelY, spanX, spanY, result);
1950
1951 boolean success = false;
1952 // First we try the exact nearest position of the item being dragged,
1953 // we will then want to try to move this around to other neighbouring positions
Adam Cohen8baab352012-03-20 17:39:21 -07001954 success = rearrangementExists(result[0], result[1], spanX, spanY, direction, dragView,
1955 solution);
Adam Cohen482ed822012-03-02 14:15:13 -08001956
1957 if (!success) {
1958 // We try shrinking the widget down to size in an alternating pattern, shrink 1 in
1959 // x, then 1 in y etc.
1960 if (spanX > minSpanX && (minSpanY == spanY || decX)) {
Adam Cohenfa3c58f2013-12-06 16:10:55 -08001961 return findReorderSolution(pixelX, pixelY, minSpanX, minSpanY, spanX - 1, spanY,
1962 direction, dragView, false, solution);
Adam Cohen482ed822012-03-02 14:15:13 -08001963 } else if (spanY > minSpanY) {
Adam Cohenfa3c58f2013-12-06 16:10:55 -08001964 return findReorderSolution(pixelX, pixelY, minSpanX, minSpanY, spanX, spanY - 1,
1965 direction, dragView, true, solution);
Adam Cohen482ed822012-03-02 14:15:13 -08001966 }
1967 solution.isSolution = false;
1968 } else {
1969 solution.isSolution = true;
1970 solution.dragViewX = result[0];
1971 solution.dragViewY = result[1];
1972 solution.dragViewSpanX = spanX;
1973 solution.dragViewSpanY = spanY;
Adam Cohen482ed822012-03-02 14:15:13 -08001974 }
1975 return solution;
1976 }
1977
1978 private void copyCurrentStateToSolution(ItemConfiguration solution, boolean temp) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07001979 int childCount = mShortcutsAndWidgets.getChildCount();
Adam Cohen482ed822012-03-02 14:15:13 -08001980 for (int i = 0; i < childCount; i++) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07001981 View child = mShortcutsAndWidgets.getChildAt(i);
Adam Cohen482ed822012-03-02 14:15:13 -08001982 LayoutParams lp = (LayoutParams) child.getLayoutParams();
Adam Cohen8baab352012-03-20 17:39:21 -07001983 CellAndSpan c;
Adam Cohen482ed822012-03-02 14:15:13 -08001984 if (temp) {
Adam Cohen8baab352012-03-20 17:39:21 -07001985 c = new CellAndSpan(lp.tmpCellX, lp.tmpCellY, lp.cellHSpan, lp.cellVSpan);
Adam Cohen482ed822012-03-02 14:15:13 -08001986 } else {
Adam Cohen8baab352012-03-20 17:39:21 -07001987 c = new CellAndSpan(lp.cellX, lp.cellY, lp.cellHSpan, lp.cellVSpan);
Adam Cohen482ed822012-03-02 14:15:13 -08001988 }
Adam Cohenf3900c22012-11-16 18:28:11 -08001989 solution.add(child, c);
Adam Cohen482ed822012-03-02 14:15:13 -08001990 }
1991 }
1992
1993 private void copySolutionToTempState(ItemConfiguration solution, View dragView) {
1994 for (int i = 0; i < mCountX; i++) {
1995 for (int j = 0; j < mCountY; j++) {
1996 mTmpOccupied[i][j] = false;
1997 }
1998 }
1999
Michael Jurkaa52570f2012-03-20 03:18:20 -07002000 int childCount = mShortcutsAndWidgets.getChildCount();
Adam Cohen482ed822012-03-02 14:15:13 -08002001 for (int i = 0; i < childCount; i++) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07002002 View child = mShortcutsAndWidgets.getChildAt(i);
Adam Cohen482ed822012-03-02 14:15:13 -08002003 if (child == dragView) continue;
2004 LayoutParams lp = (LayoutParams) child.getLayoutParams();
Adam Cohen8baab352012-03-20 17:39:21 -07002005 CellAndSpan c = solution.map.get(child);
2006 if (c != null) {
2007 lp.tmpCellX = c.x;
2008 lp.tmpCellY = c.y;
2009 lp.cellHSpan = c.spanX;
2010 lp.cellVSpan = c.spanY;
2011 markCellsForView(c.x, c.y, c.spanX, c.spanY, mTmpOccupied, true);
Adam Cohen482ed822012-03-02 14:15:13 -08002012 }
2013 }
2014 markCellsForView(solution.dragViewX, solution.dragViewY, solution.dragViewSpanX,
2015 solution.dragViewSpanY, mTmpOccupied, true);
2016 }
2017
2018 private void animateItemsToSolution(ItemConfiguration solution, View dragView, boolean
2019 commitDragView) {
2020
2021 boolean[][] occupied = DESTRUCTIVE_REORDER ? mOccupied : mTmpOccupied;
2022 for (int i = 0; i < mCountX; i++) {
2023 for (int j = 0; j < mCountY; j++) {
2024 occupied[i][j] = false;
2025 }
2026 }
2027
Michael Jurkaa52570f2012-03-20 03:18:20 -07002028 int childCount = mShortcutsAndWidgets.getChildCount();
Adam Cohen482ed822012-03-02 14:15:13 -08002029 for (int i = 0; i < childCount; i++) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07002030 View child = mShortcutsAndWidgets.getChildAt(i);
Adam Cohen482ed822012-03-02 14:15:13 -08002031 if (child == dragView) continue;
Adam Cohen8baab352012-03-20 17:39:21 -07002032 CellAndSpan c = solution.map.get(child);
2033 if (c != null) {
Adam Cohen19f37922012-03-21 11:59:11 -07002034 animateChildToPosition(child, c.x, c.y, REORDER_ANIMATION_DURATION, 0,
2035 DESTRUCTIVE_REORDER, false);
Adam Cohen8baab352012-03-20 17:39:21 -07002036 markCellsForView(c.x, c.y, c.spanX, c.spanY, occupied, true);
Adam Cohen482ed822012-03-02 14:15:13 -08002037 }
2038 }
2039 if (commitDragView) {
2040 markCellsForView(solution.dragViewX, solution.dragViewY, solution.dragViewSpanX,
2041 solution.dragViewSpanY, occupied, true);
2042 }
2043 }
2044
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002045
2046 // This method starts or changes the reorder preview animations
2047 private void beginOrAdjustReorderPreviewAnimations(ItemConfiguration solution,
2048 View dragView, int delay, int mode) {
Adam Cohen19f37922012-03-21 11:59:11 -07002049 int childCount = mShortcutsAndWidgets.getChildCount();
Adam Cohen19f37922012-03-21 11:59:11 -07002050 for (int i = 0; i < childCount; i++) {
2051 View child = mShortcutsAndWidgets.getChildAt(i);
2052 if (child == dragView) continue;
2053 CellAndSpan c = solution.map.get(child);
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002054 boolean skip = mode == ReorderPreviewAnimation.MODE_HINT && solution.intersectingViews
2055 != null && !solution.intersectingViews.contains(child);
2056
Adam Cohen19f37922012-03-21 11:59:11 -07002057 LayoutParams lp = (LayoutParams) child.getLayoutParams();
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002058 if (c != null && !skip) {
2059 ReorderPreviewAnimation rha = new ReorderPreviewAnimation(child, mode, lp.cellX,
2060 lp.cellY, c.x, c.y, c.spanX, c.spanY);
Adam Cohend024f982012-05-23 18:26:45 -07002061 rha.animate();
Adam Cohen19f37922012-03-21 11:59:11 -07002062 }
2063 }
2064 }
2065
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002066 // Class which represents the reorder preview animations. These animations show that an item is
Adam Cohen19f37922012-03-21 11:59:11 -07002067 // in a temporary state, and hint at where the item will return to.
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002068 class ReorderPreviewAnimation {
Adam Cohen19f37922012-03-21 11:59:11 -07002069 View child;
Adam Cohend024f982012-05-23 18:26:45 -07002070 float finalDeltaX;
2071 float finalDeltaY;
2072 float initDeltaX;
2073 float initDeltaY;
2074 float finalScale;
2075 float initScale;
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002076 int mode;
2077 boolean repeating = false;
2078 private static final int PREVIEW_DURATION = 300;
2079 private static final int HINT_DURATION = Workspace.REORDER_TIMEOUT;
2080
2081 public static final int MODE_HINT = 0;
2082 public static final int MODE_PREVIEW = 1;
2083
Adam Cohene7587d22012-05-24 18:50:02 -07002084 Animator a;
Adam Cohen19f37922012-03-21 11:59:11 -07002085
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002086 public ReorderPreviewAnimation(View child, int mode, int cellX0, int cellY0, int cellX1,
2087 int cellY1, int spanX, int spanY) {
Adam Cohen19f37922012-03-21 11:59:11 -07002088 regionToCenterPoint(cellX0, cellY0, spanX, spanY, mTmpPoint);
2089 final int x0 = mTmpPoint[0];
2090 final int y0 = mTmpPoint[1];
2091 regionToCenterPoint(cellX1, cellY1, spanX, spanY, mTmpPoint);
2092 final int x1 = mTmpPoint[0];
2093 final int y1 = mTmpPoint[1];
2094 final int dX = x1 - x0;
2095 final int dY = y1 - y0;
Adam Cohend024f982012-05-23 18:26:45 -07002096 finalDeltaX = 0;
2097 finalDeltaY = 0;
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002098 int dir = mode == MODE_HINT ? -1 : 1;
Adam Cohen19f37922012-03-21 11:59:11 -07002099 if (dX == dY && dX == 0) {
2100 } else {
2101 if (dY == 0) {
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002102 finalDeltaX = - dir * Math.signum(dX) * mReorderPreviewAnimationMagnitude;
Adam Cohen19f37922012-03-21 11:59:11 -07002103 } else if (dX == 0) {
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002104 finalDeltaY = - dir * Math.signum(dY) * mReorderPreviewAnimationMagnitude;
Adam Cohen19f37922012-03-21 11:59:11 -07002105 } else {
2106 double angle = Math.atan( (float) (dY) / dX);
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002107 finalDeltaX = (int) (- dir * Math.signum(dX) *
2108 Math.abs(Math.cos(angle) * mReorderPreviewAnimationMagnitude));
2109 finalDeltaY = (int) (- dir * Math.signum(dY) *
2110 Math.abs(Math.sin(angle) * mReorderPreviewAnimationMagnitude));
Adam Cohen19f37922012-03-21 11:59:11 -07002111 }
2112 }
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002113 this.mode = mode;
Adam Cohend024f982012-05-23 18:26:45 -07002114 initDeltaX = child.getTranslationX();
2115 initDeltaY = child.getTranslationY();
Adam Cohen307fe232012-08-16 17:55:58 -07002116 finalScale = getChildrenScale() - 4.0f / child.getWidth();
Adam Cohend024f982012-05-23 18:26:45 -07002117 initScale = child.getScaleX();
Adam Cohen19f37922012-03-21 11:59:11 -07002118 this.child = child;
2119 }
2120
Adam Cohend024f982012-05-23 18:26:45 -07002121 void animate() {
Adam Cohen19f37922012-03-21 11:59:11 -07002122 if (mShakeAnimators.containsKey(child)) {
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002123 ReorderPreviewAnimation oldAnimation = mShakeAnimators.get(child);
Adam Cohend024f982012-05-23 18:26:45 -07002124 oldAnimation.cancel();
Adam Cohen19f37922012-03-21 11:59:11 -07002125 mShakeAnimators.remove(child);
Adam Cohene7587d22012-05-24 18:50:02 -07002126 if (finalDeltaX == 0 && finalDeltaY == 0) {
2127 completeAnimationImmediately();
2128 return;
2129 }
Adam Cohen19f37922012-03-21 11:59:11 -07002130 }
Adam Cohend024f982012-05-23 18:26:45 -07002131 if (finalDeltaX == 0 && finalDeltaY == 0) {
Adam Cohen19f37922012-03-21 11:59:11 -07002132 return;
2133 }
Michael Jurkaf1ad6082013-03-13 12:55:46 +01002134 ValueAnimator va = LauncherAnimUtils.ofFloat(child, 0f, 1f);
Adam Cohene7587d22012-05-24 18:50:02 -07002135 a = va;
Adam Cohen19f37922012-03-21 11:59:11 -07002136 va.setRepeatMode(ValueAnimator.REVERSE);
2137 va.setRepeatCount(ValueAnimator.INFINITE);
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002138 va.setDuration(mode == MODE_HINT ? HINT_DURATION : PREVIEW_DURATION);
Adam Cohend024f982012-05-23 18:26:45 -07002139 va.setStartDelay((int) (Math.random() * 60));
Adam Cohen19f37922012-03-21 11:59:11 -07002140 va.addUpdateListener(new AnimatorUpdateListener() {
2141 @Override
2142 public void onAnimationUpdate(ValueAnimator animation) {
2143 float r = ((Float) animation.getAnimatedValue()).floatValue();
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002144 float r1 = (mode == MODE_HINT && repeating) ? 1.0f : r;
2145 float x = r1 * finalDeltaX + (1 - r1) * initDeltaX;
2146 float y = r1 * finalDeltaY + (1 - r1) * initDeltaY;
Adam Cohen19f37922012-03-21 11:59:11 -07002147 child.setTranslationX(x);
2148 child.setTranslationY(y);
Adam Cohend024f982012-05-23 18:26:45 -07002149 float s = r * finalScale + (1 - r) * initScale;
Brandon Keely50e6e562012-05-08 16:28:49 -07002150 child.setScaleX(s);
2151 child.setScaleY(s);
Adam Cohen19f37922012-03-21 11:59:11 -07002152 }
2153 });
2154 va.addListener(new AnimatorListenerAdapter() {
2155 public void onAnimationRepeat(Animator animation) {
Adam Cohen19f37922012-03-21 11:59:11 -07002156 // We make sure to end only after a full period
Adam Cohend024f982012-05-23 18:26:45 -07002157 initDeltaX = 0;
2158 initDeltaY = 0;
Adam Cohen307fe232012-08-16 17:55:58 -07002159 initScale = getChildrenScale();
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002160 repeating = true;
Adam Cohen19f37922012-03-21 11:59:11 -07002161 }
2162 });
Adam Cohen19f37922012-03-21 11:59:11 -07002163 mShakeAnimators.put(child, this);
2164 va.start();
2165 }
2166
Adam Cohend024f982012-05-23 18:26:45 -07002167 private void cancel() {
Adam Cohene7587d22012-05-24 18:50:02 -07002168 if (a != null) {
2169 a.cancel();
2170 }
Adam Cohen19f37922012-03-21 11:59:11 -07002171 }
Adam Cohene7587d22012-05-24 18:50:02 -07002172
Adam Cohen091440a2015-03-18 14:16:05 -07002173 @Thunk void completeAnimationImmediately() {
Adam Cohene7587d22012-05-24 18:50:02 -07002174 if (a != null) {
2175 a.cancel();
2176 }
Brandon Keely50e6e562012-05-08 16:28:49 -07002177
Michael Jurka2ecf9952012-06-18 12:52:28 -07002178 AnimatorSet s = LauncherAnimUtils.createAnimatorSet();
Adam Cohene7587d22012-05-24 18:50:02 -07002179 a = s;
Brandon Keely50e6e562012-05-08 16:28:49 -07002180 s.playTogether(
Adam Cohen307fe232012-08-16 17:55:58 -07002181 LauncherAnimUtils.ofFloat(child, "scaleX", getChildrenScale()),
2182 LauncherAnimUtils.ofFloat(child, "scaleY", getChildrenScale()),
Michael Jurka2ecf9952012-06-18 12:52:28 -07002183 LauncherAnimUtils.ofFloat(child, "translationX", 0f),
2184 LauncherAnimUtils.ofFloat(child, "translationY", 0f)
Brandon Keely50e6e562012-05-08 16:28:49 -07002185 );
2186 s.setDuration(REORDER_ANIMATION_DURATION);
2187 s.setInterpolator(new android.view.animation.DecelerateInterpolator(1.5f));
2188 s.start();
2189 }
Adam Cohen19f37922012-03-21 11:59:11 -07002190 }
2191
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002192 private void completeAndClearReorderPreviewAnimations() {
2193 for (ReorderPreviewAnimation a: mShakeAnimators.values()) {
Brandon Keely50e6e562012-05-08 16:28:49 -07002194 a.completeAnimationImmediately();
Adam Cohen19f37922012-03-21 11:59:11 -07002195 }
2196 mShakeAnimators.clear();
2197 }
2198
Adam Cohen482ed822012-03-02 14:15:13 -08002199 private void commitTempPlacement() {
2200 for (int i = 0; i < mCountX; i++) {
2201 for (int j = 0; j < mCountY; j++) {
2202 mOccupied[i][j] = mTmpOccupied[i][j];
2203 }
2204 }
Michael Jurkaa52570f2012-03-20 03:18:20 -07002205 int childCount = mShortcutsAndWidgets.getChildCount();
Adam Cohen482ed822012-03-02 14:15:13 -08002206 for (int i = 0; i < childCount; i++) {
Adam Cohenea889a22012-03-27 16:45:39 -07002207 View child = mShortcutsAndWidgets.getChildAt(i);
2208 LayoutParams lp = (LayoutParams) child.getLayoutParams();
2209 ItemInfo info = (ItemInfo) child.getTag();
Adam Cohen2acce882012-03-28 19:03:19 -07002210 // We do a null check here because the item info can be null in the case of the
2211 // AllApps button in the hotseat.
2212 if (info != null) {
Adam Cohen487f7dd2012-06-28 18:12:10 -07002213 if (info.cellX != lp.tmpCellX || info.cellY != lp.tmpCellY ||
2214 info.spanX != lp.cellHSpan || info.spanY != lp.cellVSpan) {
2215 info.requiresDbUpdate = true;
2216 }
Adam Cohen2acce882012-03-28 19:03:19 -07002217 info.cellX = lp.cellX = lp.tmpCellX;
2218 info.cellY = lp.cellY = lp.tmpCellY;
Adam Cohenbebf0422012-04-11 18:06:28 -07002219 info.spanX = lp.cellHSpan;
2220 info.spanY = lp.cellVSpan;
Adam Cohen2acce882012-03-28 19:03:19 -07002221 }
Adam Cohen482ed822012-03-02 14:15:13 -08002222 }
Adam Cohen2acce882012-03-28 19:03:19 -07002223 mLauncher.getWorkspace().updateItemLocationsInDatabase(this);
Adam Cohen482ed822012-03-02 14:15:13 -08002224 }
2225
Sunny Goyalf7a29e82015-04-24 15:20:43 -07002226 private void setUseTempCoords(boolean useTempCoords) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07002227 int childCount = mShortcutsAndWidgets.getChildCount();
Adam Cohen482ed822012-03-02 14:15:13 -08002228 for (int i = 0; i < childCount; i++) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07002229 LayoutParams lp = (LayoutParams) mShortcutsAndWidgets.getChildAt(i).getLayoutParams();
Adam Cohen482ed822012-03-02 14:15:13 -08002230 lp.useTmpCoords = useTempCoords;
2231 }
2232 }
2233
Sunny Goyalf7a29e82015-04-24 15:20:43 -07002234 private ItemConfiguration findConfigurationNoShuffle(int pixelX, int pixelY, int minSpanX, int minSpanY,
Adam Cohen482ed822012-03-02 14:15:13 -08002235 int spanX, int spanY, View dragView, ItemConfiguration solution) {
2236 int[] result = new int[2];
2237 int[] resultSpan = new int[2];
Sunny Goyalf7a29e82015-04-24 15:20:43 -07002238 findNearestVacantArea(pixelX, pixelY, minSpanX, minSpanY, spanX, spanY, result,
Adam Cohen482ed822012-03-02 14:15:13 -08002239 resultSpan);
2240 if (result[0] >= 0 && result[1] >= 0) {
2241 copyCurrentStateToSolution(solution, false);
2242 solution.dragViewX = result[0];
2243 solution.dragViewY = result[1];
2244 solution.dragViewSpanX = resultSpan[0];
2245 solution.dragViewSpanY = resultSpan[1];
2246 solution.isSolution = true;
2247 } else {
2248 solution.isSolution = false;
2249 }
2250 return solution;
2251 }
2252
2253 public void prepareChildForDrag(View child) {
2254 markCellsAsUnoccupiedForView(child);
Adam Cohen482ed822012-03-02 14:15:13 -08002255 }
2256
Adam Cohen19f37922012-03-21 11:59:11 -07002257 /* This seems like it should be obvious and straight-forward, but when the direction vector
2258 needs to match with the notion of the dragView pushing other views, we have to employ
2259 a slightly more subtle notion of the direction vector. The question is what two points is
2260 the vector between? The center of the dragView and its desired destination? Not quite, as
2261 this doesn't necessarily coincide with the interaction of the dragView and items occupying
2262 those cells. Instead we use some heuristics to often lock the vector to up, down, left
2263 or right, which helps make pushing feel right.
2264 */
2265 private void getDirectionVectorForDrop(int dragViewCenterX, int dragViewCenterY, int spanX,
2266 int spanY, View dragView, int[] resultDirection) {
2267 int[] targetDestination = new int[2];
2268
2269 findNearestArea(dragViewCenterX, dragViewCenterY, spanX, spanY, targetDestination);
2270 Rect dragRect = new Rect();
2271 regionToRect(targetDestination[0], targetDestination[1], spanX, spanY, dragRect);
2272 dragRect.offset(dragViewCenterX - dragRect.centerX(), dragViewCenterY - dragRect.centerY());
2273
2274 Rect dropRegionRect = new Rect();
2275 getViewsIntersectingRegion(targetDestination[0], targetDestination[1], spanX, spanY,
2276 dragView, dropRegionRect, mIntersectingViews);
2277
2278 int dropRegionSpanX = dropRegionRect.width();
2279 int dropRegionSpanY = dropRegionRect.height();
2280
2281 regionToRect(dropRegionRect.left, dropRegionRect.top, dropRegionRect.width(),
2282 dropRegionRect.height(), dropRegionRect);
2283
2284 int deltaX = (dropRegionRect.centerX() - dragViewCenterX) / spanX;
2285 int deltaY = (dropRegionRect.centerY() - dragViewCenterY) / spanY;
2286
2287 if (dropRegionSpanX == mCountX || spanX == mCountX) {
2288 deltaX = 0;
2289 }
2290 if (dropRegionSpanY == mCountY || spanY == mCountY) {
2291 deltaY = 0;
2292 }
2293
2294 if (deltaX == 0 && deltaY == 0) {
2295 // No idea what to do, give a random direction.
2296 resultDirection[0] = 1;
2297 resultDirection[1] = 0;
2298 } else {
2299 computeDirectionVector(deltaX, deltaY, resultDirection);
2300 }
2301 }
2302
2303 // For a given cell and span, fetch the set of views intersecting the region.
2304 private void getViewsIntersectingRegion(int cellX, int cellY, int spanX, int spanY,
2305 View dragView, Rect boundingRect, ArrayList<View> intersectingViews) {
2306 if (boundingRect != null) {
2307 boundingRect.set(cellX, cellY, cellX + spanX, cellY + spanY);
2308 }
2309 intersectingViews.clear();
2310 Rect r0 = new Rect(cellX, cellY, cellX + spanX, cellY + spanY);
2311 Rect r1 = new Rect();
2312 final int count = mShortcutsAndWidgets.getChildCount();
2313 for (int i = 0; i < count; i++) {
2314 View child = mShortcutsAndWidgets.getChildAt(i);
2315 if (child == dragView) continue;
2316 LayoutParams lp = (LayoutParams) child.getLayoutParams();
2317 r1.set(lp.cellX, lp.cellY, lp.cellX + lp.cellHSpan, lp.cellY + lp.cellVSpan);
2318 if (Rect.intersects(r0, r1)) {
2319 mIntersectingViews.add(child);
2320 if (boundingRect != null) {
2321 boundingRect.union(r1);
2322 }
2323 }
2324 }
2325 }
2326
2327 boolean isNearestDropLocationOccupied(int pixelX, int pixelY, int spanX, int spanY,
2328 View dragView, int[] result) {
2329 result = findNearestArea(pixelX, pixelY, spanX, spanY, result);
2330 getViewsIntersectingRegion(result[0], result[1], spanX, spanY, dragView, null,
2331 mIntersectingViews);
2332 return !mIntersectingViews.isEmpty();
2333 }
2334
2335 void revertTempState() {
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002336 completeAndClearReorderPreviewAnimations();
2337 if (isItemPlacementDirty() && !DESTRUCTIVE_REORDER) {
2338 final int count = mShortcutsAndWidgets.getChildCount();
2339 for (int i = 0; i < count; i++) {
2340 View child = mShortcutsAndWidgets.getChildAt(i);
2341 LayoutParams lp = (LayoutParams) child.getLayoutParams();
2342 if (lp.tmpCellX != lp.cellX || lp.tmpCellY != lp.cellY) {
2343 lp.tmpCellX = lp.cellX;
2344 lp.tmpCellY = lp.cellY;
2345 animateChildToPosition(child, lp.cellX, lp.cellY, REORDER_ANIMATION_DURATION,
2346 0, false, false);
2347 }
Adam Cohen19f37922012-03-21 11:59:11 -07002348 }
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002349 setItemPlacementDirty(false);
Adam Cohen19f37922012-03-21 11:59:11 -07002350 }
Adam Cohen19f37922012-03-21 11:59:11 -07002351 }
2352
Adam Cohenbebf0422012-04-11 18:06:28 -07002353 boolean createAreaForResize(int cellX, int cellY, int spanX, int spanY,
2354 View dragView, int[] direction, boolean commit) {
2355 int[] pixelXY = new int[2];
2356 regionToCenterPoint(cellX, cellY, spanX, spanY, pixelXY);
2357
2358 // First we determine if things have moved enough to cause a different layout
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002359 ItemConfiguration swapSolution = findReorderSolution(pixelXY[0], pixelXY[1], spanX, spanY,
Adam Cohenbebf0422012-04-11 18:06:28 -07002360 spanX, spanY, direction, dragView, true, new ItemConfiguration());
2361
2362 setUseTempCoords(true);
2363 if (swapSolution != null && swapSolution.isSolution) {
2364 // If we're just testing for a possible location (MODE_ACCEPT_DROP), we don't bother
2365 // committing anything or animating anything as we just want to determine if a solution
2366 // exists
2367 copySolutionToTempState(swapSolution, dragView);
2368 setItemPlacementDirty(true);
2369 animateItemsToSolution(swapSolution, dragView, commit);
2370
2371 if (commit) {
2372 commitTempPlacement();
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002373 completeAndClearReorderPreviewAnimations();
Adam Cohenbebf0422012-04-11 18:06:28 -07002374 setItemPlacementDirty(false);
2375 } else {
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002376 beginOrAdjustReorderPreviewAnimations(swapSolution, dragView,
2377 REORDER_ANIMATION_DURATION, ReorderPreviewAnimation.MODE_PREVIEW);
Adam Cohenbebf0422012-04-11 18:06:28 -07002378 }
2379 mShortcutsAndWidgets.requestLayout();
2380 }
2381 return swapSolution.isSolution;
2382 }
2383
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002384 int[] performReorder(int pixelX, int pixelY, int minSpanX, int minSpanY, int spanX, int spanY,
Adam Cohen482ed822012-03-02 14:15:13 -08002385 View dragView, int[] result, int resultSpan[], int mode) {
Adam Cohen482ed822012-03-02 14:15:13 -08002386 // First we determine if things have moved enough to cause a different layout
Adam Cohen47a876d2012-03-19 13:21:41 -07002387 result = findNearestArea(pixelX, pixelY, spanX, spanY, result);
Adam Cohen482ed822012-03-02 14:15:13 -08002388
2389 if (resultSpan == null) {
2390 resultSpan = new int[2];
2391 }
2392
Adam Cohen19f37922012-03-21 11:59:11 -07002393 // When we are checking drop validity or actually dropping, we don't recompute the
2394 // direction vector, since we want the solution to match the preview, and it's possible
2395 // that the exact position of the item has changed to result in a new reordering outcome.
Adam Cohenb209e632012-03-27 17:09:36 -07002396 if ((mode == MODE_ON_DROP || mode == MODE_ON_DROP_EXTERNAL || mode == MODE_ACCEPT_DROP)
2397 && mPreviousReorderDirection[0] != INVALID_DIRECTION) {
Adam Cohen19f37922012-03-21 11:59:11 -07002398 mDirectionVector[0] = mPreviousReorderDirection[0];
2399 mDirectionVector[1] = mPreviousReorderDirection[1];
2400 // We reset this vector after drop
Adam Cohenb209e632012-03-27 17:09:36 -07002401 if (mode == MODE_ON_DROP || mode == MODE_ON_DROP_EXTERNAL) {
2402 mPreviousReorderDirection[0] = INVALID_DIRECTION;
2403 mPreviousReorderDirection[1] = INVALID_DIRECTION;
Adam Cohen19f37922012-03-21 11:59:11 -07002404 }
2405 } else {
2406 getDirectionVectorForDrop(pixelX, pixelY, spanX, spanY, dragView, mDirectionVector);
2407 mPreviousReorderDirection[0] = mDirectionVector[0];
2408 mPreviousReorderDirection[1] = mDirectionVector[1];
2409 }
2410
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002411 // Find a solution involving pushing / displacing any items in the way
2412 ItemConfiguration swapSolution = findReorderSolution(pixelX, pixelY, minSpanX, minSpanY,
Adam Cohen482ed822012-03-02 14:15:13 -08002413 spanX, spanY, mDirectionVector, dragView, true, new ItemConfiguration());
2414
2415 // We attempt the approach which doesn't shuffle views at all
2416 ItemConfiguration noShuffleSolution = findConfigurationNoShuffle(pixelX, pixelY, minSpanX,
2417 minSpanY, spanX, spanY, dragView, new ItemConfiguration());
2418
2419 ItemConfiguration finalSolution = null;
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002420
2421 // If the reorder solution requires resizing (shrinking) the item being dropped, we instead
2422 // favor a solution in which the item is not resized, but
Adam Cohen482ed822012-03-02 14:15:13 -08002423 if (swapSolution.isSolution && swapSolution.area() >= noShuffleSolution.area()) {
2424 finalSolution = swapSolution;
2425 } else if (noShuffleSolution.isSolution) {
2426 finalSolution = noShuffleSolution;
2427 }
2428
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002429 if (mode == MODE_SHOW_REORDER_HINT) {
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002430 if (finalSolution != null) {
Adam Cohenfe692872013-12-11 14:47:23 -08002431 beginOrAdjustReorderPreviewAnimations(finalSolution, dragView, 0,
2432 ReorderPreviewAnimation.MODE_HINT);
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002433 result[0] = finalSolution.dragViewX;
2434 result[1] = finalSolution.dragViewY;
2435 resultSpan[0] = finalSolution.dragViewSpanX;
2436 resultSpan[1] = finalSolution.dragViewSpanY;
2437 } else {
2438 result[0] = result[1] = resultSpan[0] = resultSpan[1] = -1;
2439 }
2440 return result;
2441 }
2442
Adam Cohen482ed822012-03-02 14:15:13 -08002443 boolean foundSolution = true;
2444 if (!DESTRUCTIVE_REORDER) {
2445 setUseTempCoords(true);
2446 }
2447
2448 if (finalSolution != null) {
2449 result[0] = finalSolution.dragViewX;
2450 result[1] = finalSolution.dragViewY;
2451 resultSpan[0] = finalSolution.dragViewSpanX;
2452 resultSpan[1] = finalSolution.dragViewSpanY;
2453
2454 // If we're just testing for a possible location (MODE_ACCEPT_DROP), we don't bother
2455 // committing anything or animating anything as we just want to determine if a solution
2456 // exists
2457 if (mode == MODE_DRAG_OVER || mode == MODE_ON_DROP || mode == MODE_ON_DROP_EXTERNAL) {
2458 if (!DESTRUCTIVE_REORDER) {
2459 copySolutionToTempState(finalSolution, dragView);
2460 }
2461 setItemPlacementDirty(true);
2462 animateItemsToSolution(finalSolution, dragView, mode == MODE_ON_DROP);
2463
Adam Cohen19f37922012-03-21 11:59:11 -07002464 if (!DESTRUCTIVE_REORDER &&
2465 (mode == MODE_ON_DROP || mode == MODE_ON_DROP_EXTERNAL)) {
Adam Cohen482ed822012-03-02 14:15:13 -08002466 commitTempPlacement();
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002467 completeAndClearReorderPreviewAnimations();
Adam Cohen19f37922012-03-21 11:59:11 -07002468 setItemPlacementDirty(false);
2469 } else {
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002470 beginOrAdjustReorderPreviewAnimations(finalSolution, dragView,
2471 REORDER_ANIMATION_DURATION, ReorderPreviewAnimation.MODE_PREVIEW);
Adam Cohen482ed822012-03-02 14:15:13 -08002472 }
2473 }
2474 } else {
2475 foundSolution = false;
2476 result[0] = result[1] = resultSpan[0] = resultSpan[1] = -1;
2477 }
2478
2479 if ((mode == MODE_ON_DROP || !foundSolution) && !DESTRUCTIVE_REORDER) {
2480 setUseTempCoords(false);
2481 }
Adam Cohen482ed822012-03-02 14:15:13 -08002482
Michael Jurkaa52570f2012-03-20 03:18:20 -07002483 mShortcutsAndWidgets.requestLayout();
Adam Cohen482ed822012-03-02 14:15:13 -08002484 return result;
2485 }
2486
Adam Cohen19f37922012-03-21 11:59:11 -07002487 void setItemPlacementDirty(boolean dirty) {
2488 mItemPlacementDirty = dirty;
Adam Cohen482ed822012-03-02 14:15:13 -08002489 }
Adam Cohen19f37922012-03-21 11:59:11 -07002490 boolean isItemPlacementDirty() {
2491 return mItemPlacementDirty;
Adam Cohen482ed822012-03-02 14:15:13 -08002492 }
2493
Adam Cohen091440a2015-03-18 14:16:05 -07002494 @Thunk class ItemConfiguration {
Adam Cohen8baab352012-03-20 17:39:21 -07002495 HashMap<View, CellAndSpan> map = new HashMap<View, CellAndSpan>();
Adam Cohenf3900c22012-11-16 18:28:11 -08002496 private HashMap<View, CellAndSpan> savedMap = new HashMap<View, CellAndSpan>();
2497 ArrayList<View> sortedViews = new ArrayList<View>();
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002498 ArrayList<View> intersectingViews;
Adam Cohen482ed822012-03-02 14:15:13 -08002499 boolean isSolution = false;
2500 int dragViewX, dragViewY, dragViewSpanX, dragViewSpanY;
2501
Adam Cohenf3900c22012-11-16 18:28:11 -08002502 void save() {
2503 // Copy current state into savedMap
2504 for (View v: map.keySet()) {
2505 map.get(v).copy(savedMap.get(v));
2506 }
2507 }
2508
2509 void restore() {
2510 // Restore current state from savedMap
2511 for (View v: savedMap.keySet()) {
2512 savedMap.get(v).copy(map.get(v));
2513 }
2514 }
2515
2516 void add(View v, CellAndSpan cs) {
2517 map.put(v, cs);
2518 savedMap.put(v, new CellAndSpan());
2519 sortedViews.add(v);
2520 }
2521
Adam Cohen482ed822012-03-02 14:15:13 -08002522 int area() {
2523 return dragViewSpanX * dragViewSpanY;
2524 }
Adam Cohen8baab352012-03-20 17:39:21 -07002525 }
2526
2527 private class CellAndSpan {
2528 int x, y;
2529 int spanX, spanY;
2530
Adam Cohenf3900c22012-11-16 18:28:11 -08002531 public CellAndSpan() {
2532 }
2533
2534 public void copy(CellAndSpan copy) {
2535 copy.x = x;
2536 copy.y = y;
2537 copy.spanX = spanX;
2538 copy.spanY = spanY;
2539 }
2540
Adam Cohen8baab352012-03-20 17:39:21 -07002541 public CellAndSpan(int x, int y, int spanX, int spanY) {
2542 this.x = x;
2543 this.y = y;
2544 this.spanX = spanX;
2545 this.spanY = spanY;
Adam Cohen482ed822012-03-02 14:15:13 -08002546 }
Adam Cohenf3900c22012-11-16 18:28:11 -08002547
2548 public String toString() {
2549 return "(" + x + ", " + y + ": " + spanX + ", " + spanY + ")";
2550 }
2551
Adam Cohen482ed822012-03-02 14:15:13 -08002552 }
2553
Adam Cohendf035382011-04-11 17:22:04 -07002554 /**
Adam Cohendf035382011-04-11 17:22:04 -07002555 * Find a starting cell position that will fit the given bounds nearest the requested
2556 * cell location. Uses Euclidean distance to score multiple vacant areas.
2557 *
2558 * @param pixelX The X location at which you want to search for a vacant area.
2559 * @param pixelY The Y location at which you want to search for a vacant area.
2560 * @param spanX Horizontal span of the object.
2561 * @param spanY Vertical span of the object.
2562 * @param ignoreView Considers space occupied by this view as unoccupied
2563 * @param result Previously returned value to possibly recycle.
2564 * @return The X, Y cell of a vacant area that can contain this object,
2565 * nearest the requested location.
2566 */
Sunny Goyalf7a29e82015-04-24 15:20:43 -07002567 int[] findNearestArea(int pixelX, int pixelY, int spanX, int spanY, int[] result) {
2568 return findNearestArea(pixelX, pixelY, spanX, spanY, spanX, spanY, false, result, null);
Adam Cohendf035382011-04-11 17:22:04 -07002569 }
2570
Michael Jurka0280c3b2010-09-17 15:00:07 -07002571 boolean existsEmptyCell() {
2572 return findCellForSpan(null, 1, 1);
2573 }
2574
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002575 /**
Michael Jurka0280c3b2010-09-17 15:00:07 -07002576 * Finds the upper-left coordinate of the first rectangle in the grid that can
2577 * hold a cell of the specified dimensions. If intersectX and intersectY are not -1,
2578 * then this method will only return coordinates for rectangles that contain the cell
2579 * (intersectX, intersectY)
2580 *
2581 * @param cellXY The array that will contain the position of a vacant cell if such a cell
2582 * can be found.
2583 * @param spanX The horizontal span of the cell we want to find.
2584 * @param spanY The vertical span of the cell we want to find.
2585 *
2586 * @return True if a vacant cell of the specified dimension was found, false otherwise.
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07002587 */
Hyunyoung Song3f471442015-04-08 19:01:34 -07002588 public boolean findCellForSpan(int[] cellXY, int spanX, int spanY) {
Michael Jurka28750fb2010-09-24 17:43:49 -07002589 boolean foundCell = false;
Sunny Goyalf7a29e82015-04-24 15:20:43 -07002590 final int endX = mCountX - (spanX - 1);
2591 final int endY = mCountY - (spanY - 1);
Michael Jurka0280c3b2010-09-17 15:00:07 -07002592
Sunny Goyalf7a29e82015-04-24 15:20:43 -07002593 for (int y = 0; y < endY && !foundCell; y++) {
2594 inner:
2595 for (int x = 0; x < endX; x++) {
2596 for (int i = 0; i < spanX; i++) {
2597 for (int j = 0; j < spanY; j++) {
2598 if (mOccupied[x + i][y + j]) {
2599 // small optimization: we can skip to after the column we just found
2600 // an occupied cell
2601 x += i;
2602 continue inner;
Michael Jurka0280c3b2010-09-17 15:00:07 -07002603 }
2604 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07002605 }
Sunny Goyalf7a29e82015-04-24 15:20:43 -07002606 if (cellXY != null) {
2607 cellXY[0] = x;
2608 cellXY[1] = y;
2609 }
2610 foundCell = true;
Michael Jurka0280c3b2010-09-17 15:00:07 -07002611 break;
Michael Jurka0280c3b2010-09-17 15:00:07 -07002612 }
2613 }
2614
Michael Jurka28750fb2010-09-24 17:43:49 -07002615 return foundCell;
Michael Jurka0280c3b2010-09-17 15:00:07 -07002616 }
2617
2618 /**
Winson Chungc07918d2011-07-01 15:35:26 -07002619 * A drag event has begun over this layout.
2620 * It may have begun over this layout (in which case onDragChild is called first),
2621 * or it may have begun on another layout.
2622 */
2623 void onDragEnter() {
Winson Chungc07918d2011-07-01 15:35:26 -07002624 mDragging = true;
2625 }
2626
2627 /**
Michael Jurka0280c3b2010-09-17 15:00:07 -07002628 * Called when drag has left this CellLayout or has been completed (successfully or not)
2629 */
2630 void onDragExit() {
Joe Onorato4be866d2010-10-10 11:26:02 -07002631 // This can actually be called when we aren't in a drag, e.g. when adding a new
2632 // item to this layout via the customize drawer.
2633 // Guard against that case.
2634 if (mDragging) {
2635 mDragging = false;
Patrick Dubroyde7658b2010-09-27 11:15:43 -07002636 }
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -07002637
2638 // Invalidate the drag data
Adam Cohend41fbf52012-02-16 23:53:59 -08002639 mDragCell[0] = mDragCell[1] = -1;
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -07002640 mDragOutlineAnims[mDragOutlineCurrent].animateOut();
2641 mDragOutlineCurrent = (mDragOutlineCurrent + 1) % mDragOutlineAnims.length;
Adam Cohen19f37922012-03-21 11:59:11 -07002642 revertTempState();
Michael Jurka33945b22010-12-21 18:19:38 -08002643 setIsDragOverlapping(false);
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07002644 }
2645
2646 /**
Winson Chungaafa03c2010-06-11 17:34:16 -07002647 * Mark a child as having been dropped.
Patrick Dubroyde7658b2010-09-27 11:15:43 -07002648 * At the beginning of the drag operation, the child may have been on another
Patrick Dubroyce34a972010-10-19 10:34:32 -07002649 * screen, but it is re-parented before this method is called.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002650 *
2651 * @param child The child that is being dropped
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002652 */
Adam Cohen716b51e2011-06-30 12:09:54 -07002653 void onDropChild(View child) {
Romain Guyd94533d2009-08-17 10:01:15 -07002654 if (child != null) {
2655 LayoutParams lp = (LayoutParams) child.getLayoutParams();
Romain Guy84f296c2009-11-04 15:00:44 -08002656 lp.dropped = true;
Romain Guyd94533d2009-08-17 10:01:15 -07002657 child.requestLayout();
Romain Guyd94533d2009-08-17 10:01:15 -07002658 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002659 }
2660
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002661 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002662 * Computes a bounding rectangle for a range of cells
Winson Chungaafa03c2010-06-11 17:34:16 -07002663 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002664 * @param cellX X coordinate of upper left corner expressed as a cell position
2665 * @param cellY Y coordinate of upper left corner expressed as a cell position
Winson Chungaafa03c2010-06-11 17:34:16 -07002666 * @param cellHSpan Width in cells
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002667 * @param cellVSpan Height in cells
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07002668 * @param resultRect Rect into which to put the results
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002669 */
Adam Cohend41fbf52012-02-16 23:53:59 -08002670 public void cellToRect(int cellX, int cellY, int cellHSpan, int cellVSpan, Rect resultRect) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002671 final int cellWidth = mCellWidth;
2672 final int cellHeight = mCellHeight;
2673 final int widthGap = mWidthGap;
2674 final int heightGap = mHeightGap;
Winson Chungaafa03c2010-06-11 17:34:16 -07002675
Winson Chung4b825dcd2011-06-19 12:41:22 -07002676 final int hStartPadding = getPaddingLeft();
2677 final int vStartPadding = getPaddingTop();
Winson Chungaafa03c2010-06-11 17:34:16 -07002678
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002679 int width = cellHSpan * cellWidth + ((cellHSpan - 1) * widthGap);
2680 int height = cellVSpan * cellHeight + ((cellVSpan - 1) * heightGap);
2681
2682 int x = hStartPadding + cellX * (cellWidth + widthGap);
2683 int y = vStartPadding + cellY * (cellHeight + heightGap);
Winson Chungaafa03c2010-06-11 17:34:16 -07002684
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07002685 resultRect.set(x, y, x + width, y + height);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002686 }
Winson Chungaafa03c2010-06-11 17:34:16 -07002687
Michael Jurka0280c3b2010-09-17 15:00:07 -07002688 private void clearOccupiedCells() {
2689 for (int x = 0; x < mCountX; x++) {
2690 for (int y = 0; y < mCountY; y++) {
2691 mOccupied[x][y] = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002692 }
2693 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07002694 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002695
Adam Cohend4844c32011-02-18 19:25:06 -08002696 public void markCellsAsOccupiedForView(View view) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07002697 if (view == null || view.getParent() != mShortcutsAndWidgets) return;
Michael Jurka0280c3b2010-09-17 15:00:07 -07002698 LayoutParams lp = (LayoutParams) view.getLayoutParams();
Sunny Goyalf7a29e82015-04-24 15:20:43 -07002699 markCellsForView(lp.cellX, lp.cellY, lp.cellHSpan, lp.cellVSpan, mOccupied, true);
Michael Jurka0280c3b2010-09-17 15:00:07 -07002700 }
2701
Adam Cohend4844c32011-02-18 19:25:06 -08002702 public void markCellsAsUnoccupiedForView(View view) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07002703 if (view == null || view.getParent() != mShortcutsAndWidgets) return;
Michael Jurka0280c3b2010-09-17 15:00:07 -07002704 LayoutParams lp = (LayoutParams) view.getLayoutParams();
Sunny Goyalf7a29e82015-04-24 15:20:43 -07002705 markCellsForView(lp.cellX, lp.cellY, lp.cellHSpan, lp.cellVSpan, mOccupied, false);
Michael Jurka0280c3b2010-09-17 15:00:07 -07002706 }
2707
Adam Cohen482ed822012-03-02 14:15:13 -08002708 private void markCellsForView(int cellX, int cellY, int spanX, int spanY, boolean[][] occupied,
2709 boolean value) {
2710 if (cellX < 0 || cellY < 0) return;
Michael Jurka0280c3b2010-09-17 15:00:07 -07002711 for (int x = cellX; x < cellX + spanX && x < mCountX; x++) {
2712 for (int y = cellY; y < cellY + spanY && y < mCountY; y++) {
Adam Cohen482ed822012-03-02 14:15:13 -08002713 occupied[x][y] = value;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002714 }
2715 }
2716 }
2717
Adam Cohen2801caf2011-05-13 20:57:39 -07002718 public int getDesiredWidth() {
Michael Jurka8b805b12012-04-18 14:23:14 -07002719 return getPaddingLeft() + getPaddingRight() + (mCountX * mCellWidth) +
Adam Cohen2801caf2011-05-13 20:57:39 -07002720 (Math.max((mCountX - 1), 0) * mWidthGap);
2721 }
2722
2723 public int getDesiredHeight() {
Michael Jurka8b805b12012-04-18 14:23:14 -07002724 return getPaddingTop() + getPaddingBottom() + (mCountY * mCellHeight) +
Adam Cohen2801caf2011-05-13 20:57:39 -07002725 (Math.max((mCountY - 1), 0) * mHeightGap);
2726 }
2727
Michael Jurka66d72172011-04-12 16:29:25 -07002728 public boolean isOccupied(int x, int y) {
2729 if (x < mCountX && y < mCountY) {
2730 return mOccupied[x][y];
2731 } else {
2732 throw new RuntimeException("Position exceeds the bound of this CellLayout");
2733 }
2734 }
2735
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002736 @Override
2737 public ViewGroup.LayoutParams generateLayoutParams(AttributeSet attrs) {
2738 return new CellLayout.LayoutParams(getContext(), attrs);
2739 }
2740
2741 @Override
2742 protected boolean checkLayoutParams(ViewGroup.LayoutParams p) {
2743 return p instanceof CellLayout.LayoutParams;
2744 }
2745
2746 @Override
2747 protected ViewGroup.LayoutParams generateLayoutParams(ViewGroup.LayoutParams p) {
2748 return new CellLayout.LayoutParams(p);
2749 }
2750
2751 public static class LayoutParams extends ViewGroup.MarginLayoutParams {
2752 /**
2753 * Horizontal location of the item in the grid.
2754 */
2755 @ViewDebug.ExportedProperty
2756 public int cellX;
2757
2758 /**
2759 * Vertical location of the item in the grid.
2760 */
2761 @ViewDebug.ExportedProperty
2762 public int cellY;
2763
2764 /**
Adam Cohen482ed822012-03-02 14:15:13 -08002765 * Temporary horizontal location of the item in the grid during reorder
2766 */
2767 public int tmpCellX;
2768
2769 /**
2770 * Temporary vertical location of the item in the grid during reorder
2771 */
2772 public int tmpCellY;
2773
2774 /**
2775 * Indicates that the temporary coordinates should be used to layout the items
2776 */
2777 public boolean useTmpCoords;
2778
2779 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002780 * Number of cells spanned horizontally by the item.
2781 */
2782 @ViewDebug.ExportedProperty
2783 public int cellHSpan;
2784
2785 /**
2786 * Number of cells spanned vertically by the item.
2787 */
2788 @ViewDebug.ExportedProperty
2789 public int cellVSpan;
Winson Chungaafa03c2010-06-11 17:34:16 -07002790
Adam Cohen1b607ed2011-03-03 17:26:50 -08002791 /**
2792 * Indicates whether the item will set its x, y, width and height parameters freely,
2793 * or whether these will be computed based on cellX, cellY, cellHSpan and cellVSpan.
2794 */
Adam Cohend4844c32011-02-18 19:25:06 -08002795 public boolean isLockedToGrid = true;
2796
Adam Cohen482ed822012-03-02 14:15:13 -08002797 /**
Adam Cohenec40b2b2013-07-23 15:52:40 -07002798 * Indicates that this item should use the full extents of its parent.
2799 */
2800 public boolean isFullscreen = false;
2801
2802 /**
Adam Cohen482ed822012-03-02 14:15:13 -08002803 * Indicates whether this item can be reordered. Always true except in the case of the
2804 * the AllApps button.
2805 */
2806 public boolean canReorder = true;
2807
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002808 // X coordinate of the view in the layout.
2809 @ViewDebug.ExportedProperty
2810 int x;
2811 // Y coordinate of the view in the layout.
2812 @ViewDebug.ExportedProperty
2813 int y;
2814
Romain Guy84f296c2009-11-04 15:00:44 -08002815 boolean dropped;
Romain Guyfcb9e712009-10-02 16:06:52 -07002816
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002817 public LayoutParams(Context c, AttributeSet attrs) {
2818 super(c, attrs);
2819 cellHSpan = 1;
2820 cellVSpan = 1;
2821 }
2822
2823 public LayoutParams(ViewGroup.LayoutParams source) {
2824 super(source);
2825 cellHSpan = 1;
2826 cellVSpan = 1;
2827 }
Winson Chungaafa03c2010-06-11 17:34:16 -07002828
2829 public LayoutParams(LayoutParams source) {
2830 super(source);
2831 this.cellX = source.cellX;
2832 this.cellY = source.cellY;
2833 this.cellHSpan = source.cellHSpan;
2834 this.cellVSpan = source.cellVSpan;
2835 }
2836
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002837 public LayoutParams(int cellX, int cellY, int cellHSpan, int cellVSpan) {
Romain Guy8f19cdd2010-01-08 15:07:00 -08002838 super(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002839 this.cellX = cellX;
2840 this.cellY = cellY;
2841 this.cellHSpan = cellHSpan;
2842 this.cellVSpan = cellVSpan;
2843 }
2844
Adam Cohen2374abf2013-04-16 14:56:57 -07002845 public void setup(int cellWidth, int cellHeight, int widthGap, int heightGap,
2846 boolean invertHorizontally, int colCount) {
Adam Cohend4844c32011-02-18 19:25:06 -08002847 if (isLockedToGrid) {
2848 final int myCellHSpan = cellHSpan;
2849 final int myCellVSpan = cellVSpan;
Adam Cohen2374abf2013-04-16 14:56:57 -07002850 int myCellX = useTmpCoords ? tmpCellX : cellX;
2851 int myCellY = useTmpCoords ? tmpCellY : cellY;
2852
2853 if (invertHorizontally) {
2854 myCellX = colCount - myCellX - cellHSpan;
2855 }
Adam Cohen1b607ed2011-03-03 17:26:50 -08002856
Adam Cohend4844c32011-02-18 19:25:06 -08002857 width = myCellHSpan * cellWidth + ((myCellHSpan - 1) * widthGap) -
2858 leftMargin - rightMargin;
2859 height = myCellVSpan * cellHeight + ((myCellVSpan - 1) * heightGap) -
2860 topMargin - bottomMargin;
Winson Chungeecf02d2012-03-02 17:14:58 -08002861 x = (int) (myCellX * (cellWidth + widthGap) + leftMargin);
2862 y = (int) (myCellY * (cellHeight + heightGap) + topMargin);
Adam Cohend4844c32011-02-18 19:25:06 -08002863 }
2864 }
Winson Chungaafa03c2010-06-11 17:34:16 -07002865
Winson Chungaafa03c2010-06-11 17:34:16 -07002866 public String toString() {
2867 return "(" + this.cellX + ", " + this.cellY + ")";
2868 }
Adam Cohen7f4eabe2011-04-21 16:19:16 -07002869
2870 public void setWidth(int width) {
2871 this.width = width;
2872 }
2873
2874 public int getWidth() {
2875 return width;
2876 }
2877
2878 public void setHeight(int height) {
2879 this.height = height;
2880 }
2881
2882 public int getHeight() {
2883 return height;
2884 }
2885
2886 public void setX(int x) {
2887 this.x = x;
2888 }
2889
2890 public int getX() {
2891 return x;
2892 }
2893
2894 public void setY(int y) {
2895 this.y = y;
2896 }
2897
2898 public int getY() {
2899 return y;
2900 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002901 }
2902
Michael Jurka0280c3b2010-09-17 15:00:07 -07002903 // This class stores info for two purposes:
2904 // 1. When dragging items (mDragInfo in Workspace), we store the View, its cellX & cellY,
2905 // its spanX, spanY, and the screen it is on
2906 // 2. When long clicking on an empty cell in a CellLayout, we save information about the
2907 // cellX and cellY coordinates and which page was clicked. We then set this as a tag on
2908 // the CellLayout that was long clicked
Sunny Goyal83a8f042015-05-19 12:52:12 -07002909 public static final class CellInfo {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002910 View cell;
Michael Jurkaa63c4522010-08-19 13:52:27 -07002911 int cellX = -1;
2912 int cellY = -1;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002913 int spanX;
2914 int spanY;
Adam Cohendcd297f2013-06-18 13:13:40 -07002915 long screenId;
Winson Chung3d503fb2011-07-13 17:25:49 -07002916 long container;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002917
Sunny Goyal83a8f042015-05-19 12:52:12 -07002918 public CellInfo(View v, ItemInfo info) {
Adam Cohene0aaa0d2014-05-12 12:44:22 -07002919 cell = v;
2920 cellX = info.cellX;
2921 cellY = info.cellY;
2922 spanX = info.spanX;
2923 spanY = info.spanY;
2924 screenId = info.screenId;
2925 container = info.container;
2926 }
2927
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002928 @Override
2929 public String toString() {
Winson Chungaafa03c2010-06-11 17:34:16 -07002930 return "Cell[view=" + (cell == null ? "null" : cell.getClass())
2931 + ", x=" + cellX + ", y=" + cellY + "]";
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002932 }
2933 }
Michael Jurkad771c962011-08-09 15:00:48 -07002934
Sunny Goyala9116722015-04-29 13:55:58 -07002935 public boolean findVacantCell(int spanX, int spanY, int[] outXY) {
2936 return Utilities.findVacantCell(outXY, spanX, spanY, mCountX, mCountY, mOccupied);
2937 }
Sunny Goyal9ca9c132015-04-29 14:57:22 -07002938
2939 public boolean isRegionVacant(int x, int y, int spanX, int spanY) {
2940 int x2 = x + spanX - 1;
2941 int y2 = y + spanY - 1;
2942 if (x < 0 || y < 0 || x2 >= mCountX || y2 >= mCountY) {
2943 return false;
2944 }
2945 for (int i = x; i <= x2; i++) {
2946 for (int j = y; j <= y2; j++) {
2947 if (mOccupied[i][j]) {
2948 return false;
2949 }
2950 }
2951 }
2952
2953 return true;
2954 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002955}