blob: 907ded0814cc26201d029fcf809985421ff2bb69 [file] [log] [blame]
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001/*
2 * Copyright (C) 2008 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Daniel Sandler325dc232013-06-05 22:57:57 -040017package com.android.launcher3;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080018
Joe Onorato4be866d2010-10-10 11:26:02 -070019import android.animation.Animator;
Michael Jurka629758f2012-06-14 16:18:21 -070020import android.animation.AnimatorListenerAdapter;
Chet Haase00397b12010-10-07 11:13:10 -070021import android.animation.TimeInterpolator;
Patrick Dubroyde7658b2010-09-27 11:15:43 -070022import android.animation.ValueAnimator;
23import android.animation.ValueAnimator.AnimatorUpdateListener;
Adam Cohenc9735cf2015-01-23 16:11:55 -080024import android.annotation.TargetApi;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080025import android.content.Context;
Joe Onorato79e56262009-09-21 15:23:04 -040026import android.content.res.Resources;
Joe Onorato4be866d2010-10-10 11:26:02 -070027import android.graphics.Bitmap;
Winson Chungaafa03c2010-06-11 17:34:16 -070028import android.graphics.Canvas;
Andrew Flynn0dca1ec2012-02-29 13:33:22 -080029import android.graphics.Color;
Joe Onorato4be866d2010-10-10 11:26:02 -070030import android.graphics.Paint;
Patrick Dubroyde7658b2010-09-27 11:15:43 -070031import android.graphics.Point;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080032import android.graphics.Rect;
Adam Cohen482ed822012-03-02 14:15:13 -080033import android.graphics.drawable.ColorDrawable;
Patrick Dubroy6569f2c2010-07-12 14:25:18 -070034import android.graphics.drawable.Drawable;
Sunny Goyal2805e632015-05-20 15:35:32 -070035import android.graphics.drawable.TransitionDrawable;
Adam Cohenc9735cf2015-01-23 16:11:55 -080036import android.os.Build;
Adam Cohen1462de32012-07-24 22:34:36 -070037import android.os.Parcelable;
Adam Cohenc9735cf2015-01-23 16:11:55 -080038import android.support.v4.view.ViewCompat;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080039import android.util.AttributeSet;
Joe Onorato4be866d2010-10-10 11:26:02 -070040import android.util.Log;
Adam Cohen1462de32012-07-24 22:34:36 -070041import android.util.SparseArray;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080042import android.view.MotionEvent;
43import android.view.View;
44import android.view.ViewDebug;
45import android.view.ViewGroup;
Adam Cohenc9735cf2015-01-23 16:11:55 -080046import android.view.accessibility.AccessibilityEvent;
Winson Chung150fbab2010-09-29 17:14:26 -070047import android.view.animation.DecelerateInterpolator;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080048
Sunny Goyal4b6eb262015-05-14 19:24:40 -070049import com.android.launcher3.BubbleTextView.BubbleTextShadowHandler;
Daniel Sandler325dc232013-06-05 22:57:57 -040050import com.android.launcher3.FolderIcon.FolderRingAnimator;
Sunny Goyalaa8ef112015-06-12 20:04:41 -070051import com.android.launcher3.LauncherSettings.Favorites;
Sunny Goyale9b651e2015-04-24 11:44:51 -070052import com.android.launcher3.accessibility.DragAndDropAccessibilityDelegate;
53import com.android.launcher3.accessibility.FolderAccessibilityHelper;
54import com.android.launcher3.accessibility.WorkspaceAccessibilityHelper;
Sunny Goyal6c56c682015-07-16 14:09:05 -070055import com.android.launcher3.config.ProviderConfig;
Sunny Goyale2fd14b2015-08-27 17:45:46 -070056import com.android.launcher3.util.ParcelableSparseArray;
Adam Cohen091440a2015-03-18 14:16:05 -070057import com.android.launcher3.util.Thunk;
Patrick Dubroy8e58e912010-10-14 13:21:48 -070058
Adam Cohen69ce2e52011-07-03 19:25:21 -070059import java.util.ArrayList;
Adam Cohenc0dcf592011-06-01 15:30:43 -070060import java.util.Arrays;
Adam Cohenf3900c22012-11-16 18:28:11 -080061import java.util.Collections;
62import java.util.Comparator;
Adam Cohenbfbfd262011-06-13 16:55:12 -070063import java.util.HashMap;
Adam Cohend41fbf52012-02-16 23:53:59 -080064import java.util.Stack;
Adam Cohenc0dcf592011-06-01 15:30:43 -070065
Sunny Goyal4b6eb262015-05-14 19:24:40 -070066public class CellLayout extends ViewGroup implements BubbleTextShadowHandler {
Sunny Goyale9b651e2015-04-24 11:44:51 -070067 public static final int WORKSPACE_ACCESSIBILITY_DRAG = 2;
68 public static final int FOLDER_ACCESSIBILITY_DRAG = 1;
69
Winson Chungaafa03c2010-06-11 17:34:16 -070070 static final String TAG = "CellLayout";
71
Adam Cohen2acce882012-03-28 19:03:19 -070072 private Launcher mLauncher;
Adam Cohen091440a2015-03-18 14:16:05 -070073 @Thunk int mCellWidth;
74 @Thunk int mCellHeight;
Winson Chung11a1a532013-09-13 11:14:45 -070075 private int mFixedCellWidth;
76 private int mFixedCellHeight;
Winson Chungaafa03c2010-06-11 17:34:16 -070077
Adam Cohen091440a2015-03-18 14:16:05 -070078 @Thunk int mCountX;
79 @Thunk int mCountY;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080080
Adam Cohen234c4cd2011-07-17 21:03:04 -070081 private int mOriginalWidthGap;
82 private int mOriginalHeightGap;
Adam Cohen091440a2015-03-18 14:16:05 -070083 @Thunk int mWidthGap;
84 @Thunk int mHeightGap;
Winson Chung4b825dcd2011-06-19 12:41:22 -070085 private int mMaxGap;
Adam Cohen917e3882013-10-31 15:03:35 -070086 private boolean mDropPending = false;
Adam Cohenc50438c2014-08-19 17:43:05 -070087 private boolean mIsDragTarget = true;
Sunny Goyale2fd14b2015-08-27 17:45:46 -070088 private boolean mJailContent = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080089
Patrick Dubroyde7658b2010-09-27 11:15:43 -070090 // These are temporary variables to prevent having to allocate a new object just to
91 // return an (x, y) value from helper functions. Do NOT use them to maintain other state.
Adam Cohen091440a2015-03-18 14:16:05 -070092 @Thunk final int[] mTmpPoint = new int[2];
Sunny Goyal2805e632015-05-20 15:35:32 -070093 @Thunk final int[] mTempLocation = new int[2];
Patrick Dubroy6569f2c2010-07-12 14:25:18 -070094
The Android Open Source Project31dd5032009-03-03 19:32:27 -080095 boolean[][] mOccupied;
Adam Cohen482ed822012-03-02 14:15:13 -080096 boolean[][] mTmpOccupied;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080097
Michael Jurkadee05892010-07-27 10:01:56 -070098 private OnTouchListener mInterceptTouchListener;
Mady Melloref044dd2015-06-02 15:35:07 -070099 private StylusEventHelper mStylusEventHelper;
Michael Jurkadee05892010-07-27 10:01:56 -0700100
Adam Cohen69ce2e52011-07-03 19:25:21 -0700101 private ArrayList<FolderRingAnimator> mFolderOuterRings = new ArrayList<FolderRingAnimator>();
Adam Cohenc51934b2011-07-26 21:07:43 -0700102 private int[] mFolderLeaveBehindCell = {-1, -1};
Adam Cohen69ce2e52011-07-03 19:25:21 -0700103
Michael Jurka5f1c5092010-09-03 14:15:02 -0700104 private float mBackgroundAlpha;
Adam Cohenf34bab52010-09-30 14:11:56 -0700105
Sunny Goyal2805e632015-05-20 15:35:32 -0700106 private static final int BACKGROUND_ACTIVATE_DURATION = 120;
107 private final TransitionDrawable mBackground;
108
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700109 // These values allow a fixed measurement to be set on the CellLayout.
110 private int mFixedWidth = -1;
111 private int mFixedHeight = -1;
112
Michael Jurka33945b22010-12-21 18:19:38 -0800113 // If we're actively dragging something over this screen, mIsDragOverlapping is true
114 private boolean mIsDragOverlapping = false;
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700115
Winson Chung150fbab2010-09-29 17:14:26 -0700116 // These arrays are used to implement the drag visualization on x-large screens.
Joe Onorato4be866d2010-10-10 11:26:02 -0700117 // They are used as circular arrays, indexed by mDragOutlineCurrent.
Adam Cohen091440a2015-03-18 14:16:05 -0700118 @Thunk Rect[] mDragOutlines = new Rect[4];
119 @Thunk float[] mDragOutlineAlphas = new float[mDragOutlines.length];
Joe Onorato4be866d2010-10-10 11:26:02 -0700120 private InterruptibleInOutAnimator[] mDragOutlineAnims =
121 new InterruptibleInOutAnimator[mDragOutlines.length];
Winson Chung150fbab2010-09-29 17:14:26 -0700122
123 // Used as an index into the above 3 arrays; indicates which is the most current value.
Joe Onorato4be866d2010-10-10 11:26:02 -0700124 private int mDragOutlineCurrent = 0;
Patrick Dubroy8e58e912010-10-14 13:21:48 -0700125 private final Paint mDragOutlinePaint = new Paint();
Winson Chung150fbab2010-09-29 17:14:26 -0700126
Sunny Goyal4fe5a372015-05-14 19:55:10 -0700127 private final ClickShadowView mTouchFeedbackView;
Patrick Dubroy96864c32011-03-10 17:17:23 -0800128
Sunny Goyal316490e2015-06-02 09:38:28 -0700129 @Thunk HashMap<CellLayout.LayoutParams, Animator> mReorderAnimators = new HashMap<>();
130 @Thunk HashMap<View, ReorderPreviewAnimation> mShakeAnimators = new HashMap<>();
Adam Cohen19f37922012-03-21 11:59:11 -0700131
132 private boolean mItemPlacementDirty = false;
Adam Cohenbfbfd262011-06-13 16:55:12 -0700133
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700134 // When a drag operation is in progress, holds the nearest cell to the touch point
135 private final int[] mDragCell = new int[2];
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800136
Joe Onorato4be866d2010-10-10 11:26:02 -0700137 private boolean mDragging = false;
138
Patrick Dubroyce34a972010-10-19 10:34:32 -0700139 private TimeInterpolator mEaseOutInterpolator;
Michael Jurkaa52570f2012-03-20 03:18:20 -0700140 private ShortcutAndWidgetContainer mShortcutsAndWidgets;
Patrick Dubroyce34a972010-10-19 10:34:32 -0700141
Andrew Flynn0dca1ec2012-02-29 13:33:22 -0800142 private boolean mIsHotseat = false;
Adam Cohen307fe232012-08-16 17:55:58 -0700143 private float mHotseatScale = 1f;
Andrew Flynn0dca1ec2012-02-29 13:33:22 -0800144
Adam Cohenfa3c58f2013-12-06 16:10:55 -0800145 public static final int MODE_SHOW_REORDER_HINT = 0;
146 public static final int MODE_DRAG_OVER = 1;
147 public static final int MODE_ON_DROP = 2;
148 public static final int MODE_ON_DROP_EXTERNAL = 3;
149 public static final int MODE_ACCEPT_DROP = 4;
Adam Cohen19f37922012-03-21 11:59:11 -0700150 private static final boolean DESTRUCTIVE_REORDER = false;
Adam Cohen482ed822012-03-02 14:15:13 -0800151 private static final boolean DEBUG_VISUALIZE_OCCUPIED = false;
152
Adam Cohena897f392012-04-27 18:12:05 -0700153 static final int LANDSCAPE = 0;
154 static final int PORTRAIT = 1;
155
Adam Cohenfa3c58f2013-12-06 16:10:55 -0800156 private static final float REORDER_PREVIEW_MAGNITUDE = 0.12f;
Adam Cohen19f37922012-03-21 11:59:11 -0700157 private static final int REORDER_ANIMATION_DURATION = 150;
Adam Cohen091440a2015-03-18 14:16:05 -0700158 @Thunk float mReorderPreviewAnimationMagnitude;
Adam Cohen19f37922012-03-21 11:59:11 -0700159
Adam Cohen482ed822012-03-02 14:15:13 -0800160 private ArrayList<View> mIntersectingViews = new ArrayList<View>();
161 private Rect mOccupiedRect = new Rect();
162 private int[] mDirectionVector = new int[2];
Adam Cohen19f37922012-03-21 11:59:11 -0700163 int[] mPreviousReorderDirection = new int[2];
Adam Cohenb209e632012-03-27 17:09:36 -0700164 private static final int INVALID_DIRECTION = -100;
Adam Cohen482ed822012-03-02 14:15:13 -0800165
Sunny Goyal2805e632015-05-20 15:35:32 -0700166 private final Rect mTempRect = new Rect();
Winson Chung3a6e7f32013-10-09 15:50:52 -0700167
Michael Jurkaca993832012-06-29 15:17:04 -0700168 private final static Paint sPaint = new Paint();
Romain Guy8a0bff52012-05-06 13:14:33 -0700169
Adam Cohenc9735cf2015-01-23 16:11:55 -0800170 // Related to accessible drag and drop
Sunny Goyale9b651e2015-04-24 11:44:51 -0700171 private DragAndDropAccessibilityDelegate mTouchHelper;
Adam Cohenc9735cf2015-01-23 16:11:55 -0800172 private boolean mUseTouchHelper = false;
Adam Cohenc9735cf2015-01-23 16:11:55 -0800173
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800174 public CellLayout(Context context) {
175 this(context, null);
176 }
177
178 public CellLayout(Context context, AttributeSet attrs) {
179 this(context, attrs, 0);
180 }
181
182 public CellLayout(Context context, AttributeSet attrs, int defStyle) {
183 super(context, attrs, defStyle);
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700184
185 // A ViewGroup usually does not draw, but CellLayout needs to draw a rectangle to show
186 // the user where a dragged item will land when dropped.
187 setWillNotDraw(false);
Romain Guyce3cbd12013-02-25 15:00:36 -0800188 setClipToPadding(false);
Adam Cohen2acce882012-03-28 19:03:19 -0700189 mLauncher = (Launcher) context;
Michael Jurkaa63c4522010-08-19 13:52:27 -0700190
Adam Cohen2e6da152015-05-06 11:42:25 -0700191 DeviceProfile grid = mLauncher.getDeviceProfile();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800192
Winson Chung11a1a532013-09-13 11:14:45 -0700193 mCellWidth = mCellHeight = -1;
Nilesh Agrawal5f7099a2014-01-02 15:54:57 -0800194 mFixedCellWidth = mFixedCellHeight = -1;
Winson Chung5f8afe62013-08-12 16:19:28 -0700195 mWidthGap = mOriginalWidthGap = 0;
196 mHeightGap = mOriginalHeightGap = 0;
197 mMaxGap = Integer.MAX_VALUE;
Adam Cohen2e6da152015-05-06 11:42:25 -0700198 mCountX = (int) grid.inv.numColumns;
199 mCountY = (int) grid.inv.numRows;
Michael Jurka0280c3b2010-09-17 15:00:07 -0700200 mOccupied = new boolean[mCountX][mCountY];
Adam Cohen482ed822012-03-02 14:15:13 -0800201 mTmpOccupied = new boolean[mCountX][mCountY];
Adam Cohen5b53f292012-03-29 14:30:35 -0700202 mPreviousReorderDirection[0] = INVALID_DIRECTION;
203 mPreviousReorderDirection[1] = INVALID_DIRECTION;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800204
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800205 setAlwaysDrawnWithCacheEnabled(false);
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700206 final Resources res = getResources();
Winson Chung6e1c0d32013-10-25 15:24:24 -0700207 mHotseatScale = (float) grid.hotseatIconSizePx / grid.iconSizePx;
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700208
Sunny Goyal2805e632015-05-20 15:35:32 -0700209 mBackground = (TransitionDrawable) res.getDrawable(R.drawable.bg_screenpanel);
210 mBackground.setCallback(this);
Winson Chunge8f1d042015-07-31 12:39:57 -0700211 mBackground.setAlpha((int) (mBackgroundAlpha * 255));
Michael Jurka33945b22010-12-21 18:19:38 -0800212
Adam Cohenfa3c58f2013-12-06 16:10:55 -0800213 mReorderPreviewAnimationMagnitude = (REORDER_PREVIEW_MAGNITUDE *
Winson Chung5f8afe62013-08-12 16:19:28 -0700214 grid.iconSizePx);
Adam Cohen19f37922012-03-21 11:59:11 -0700215
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700216 // Initialize the data structures used for the drag visualization.
Patrick Dubroyce34a972010-10-19 10:34:32 -0700217 mEaseOutInterpolator = new DecelerateInterpolator(2.5f); // Quint ease out
Winson Chungb8c69f32011-10-19 21:36:08 -0700218 mDragCell[0] = mDragCell[1] = -1;
Joe Onorato4be866d2010-10-10 11:26:02 -0700219 for (int i = 0; i < mDragOutlines.length; i++) {
Adam Cohend41fbf52012-02-16 23:53:59 -0800220 mDragOutlines[i] = new Rect(-1, -1, -1, -1);
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700221 }
222
223 // When dragging things around the home screens, we show a green outline of
224 // where the item will land. The outlines gradually fade out, leaving a trail
225 // behind the drag path.
226 // Set up all the animations that are used to implement this fading.
227 final int duration = res.getInteger(R.integer.config_dragOutlineFadeTime);
Chet Haase472b2812010-10-14 07:02:04 -0700228 final float fromAlphaValue = 0;
229 final float toAlphaValue = (float)res.getInteger(R.integer.config_dragOutlineMaxAlpha);
Joe Onorato4be866d2010-10-10 11:26:02 -0700230
Patrick Dubroy8e58e912010-10-14 13:21:48 -0700231 Arrays.fill(mDragOutlineAlphas, fromAlphaValue);
Joe Onorato4be866d2010-10-10 11:26:02 -0700232
233 for (int i = 0; i < mDragOutlineAnims.length; i++) {
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700234 final InterruptibleInOutAnimator anim =
Michael Jurkaf1ad6082013-03-13 12:55:46 +0100235 new InterruptibleInOutAnimator(this, duration, fromAlphaValue, toAlphaValue);
Patrick Dubroyce34a972010-10-19 10:34:32 -0700236 anim.getAnimator().setInterpolator(mEaseOutInterpolator);
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700237 final int thisIndex = i;
Chet Haase472b2812010-10-14 07:02:04 -0700238 anim.getAnimator().addUpdateListener(new AnimatorUpdateListener() {
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700239 public void onAnimationUpdate(ValueAnimator animation) {
Joe Onorato4be866d2010-10-10 11:26:02 -0700240 final Bitmap outline = (Bitmap)anim.getTag();
241
242 // If an animation is started and then stopped very quickly, we can still
243 // get spurious updates we've cleared the tag. Guard against this.
244 if (outline == null) {
Michael Jurka3a9fced2012-04-13 14:44:29 -0700245 @SuppressWarnings("all") // suppress dead code warning
246 final boolean debug = false;
247 if (debug) {
Patrick Dubroyfe6bd872010-10-13 17:32:10 -0700248 Object val = animation.getAnimatedValue();
249 Log.d(TAG, "anim " + thisIndex + " update: " + val +
250 ", isStopped " + anim.isStopped());
251 }
Joe Onorato4be866d2010-10-10 11:26:02 -0700252 // Try to prevent it from continuing to run
253 animation.cancel();
254 } else {
Chet Haase472b2812010-10-14 07:02:04 -0700255 mDragOutlineAlphas[thisIndex] = (Float) animation.getAnimatedValue();
Adam Cohend41fbf52012-02-16 23:53:59 -0800256 CellLayout.this.invalidate(mDragOutlines[thisIndex]);
Joe Onorato4be866d2010-10-10 11:26:02 -0700257 }
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700258 }
259 });
Joe Onorato4be866d2010-10-10 11:26:02 -0700260 // The animation holds a reference to the drag outline bitmap as long is it's
261 // running. This way the bitmap can be GCed when the animations are complete.
Chet Haase472b2812010-10-14 07:02:04 -0700262 anim.getAnimator().addListener(new AnimatorListenerAdapter() {
Michael Jurka3c4c20f2010-10-28 15:36:06 -0700263 @Override
Joe Onorato4be866d2010-10-10 11:26:02 -0700264 public void onAnimationEnd(Animator animation) {
Chet Haase472b2812010-10-14 07:02:04 -0700265 if ((Float) ((ValueAnimator) animation).getAnimatedValue() == 0f) {
Joe Onorato4be866d2010-10-10 11:26:02 -0700266 anim.setTag(null);
267 }
268 }
269 });
270 mDragOutlineAnims[i] = anim;
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700271 }
Patrick Dubroyce34a972010-10-19 10:34:32 -0700272
Michael Jurkaa52570f2012-03-20 03:18:20 -0700273 mShortcutsAndWidgets = new ShortcutAndWidgetContainer(context);
Adam Cohen2374abf2013-04-16 14:56:57 -0700274 mShortcutsAndWidgets.setCellDimensions(mCellWidth, mCellHeight, mWidthGap, mHeightGap,
Winson Chung5f8afe62013-08-12 16:19:28 -0700275 mCountX, mCountY);
Adam Cohen2374abf2013-04-16 14:56:57 -0700276
Mady Mellorbb835202015-07-15 16:34:34 -0700277 mStylusEventHelper = new StylusEventHelper(new SimpleOnStylusPressListener(this), this);
Mady Melloref044dd2015-06-02 15:35:07 -0700278
Sunny Goyal4fe5a372015-05-14 19:55:10 -0700279 mTouchFeedbackView = new ClickShadowView(context);
280 addView(mTouchFeedbackView);
Michael Jurkaa52570f2012-03-20 03:18:20 -0700281 addView(mShortcutsAndWidgets);
Michael Jurka18014792010-10-14 09:01:34 -0700282 }
283
Adam Cohenc9735cf2015-01-23 16:11:55 -0800284 @TargetApi(Build.VERSION_CODES.LOLLIPOP)
Sunny Goyale9b651e2015-04-24 11:44:51 -0700285 public void enableAccessibleDrag(boolean enable, int dragType) {
Adam Cohenc9735cf2015-01-23 16:11:55 -0800286 mUseTouchHelper = enable;
287 if (!enable) {
288 ViewCompat.setAccessibilityDelegate(this, null);
289 setImportantForAccessibility(IMPORTANT_FOR_ACCESSIBILITY_NO);
290 getShortcutsAndWidgets().setImportantForAccessibility(IMPORTANT_FOR_ACCESSIBILITY_NO);
291 setOnClickListener(mLauncher);
292 } else {
Sunny Goyale9b651e2015-04-24 11:44:51 -0700293 if (dragType == WORKSPACE_ACCESSIBILITY_DRAG &&
294 !(mTouchHelper instanceof WorkspaceAccessibilityHelper)) {
295 mTouchHelper = new WorkspaceAccessibilityHelper(this);
296 } else if (dragType == FOLDER_ACCESSIBILITY_DRAG &&
297 !(mTouchHelper instanceof FolderAccessibilityHelper)) {
298 mTouchHelper = new FolderAccessibilityHelper(this);
299 }
Adam Cohenc9735cf2015-01-23 16:11:55 -0800300 ViewCompat.setAccessibilityDelegate(this, mTouchHelper);
301 setImportantForAccessibility(IMPORTANT_FOR_ACCESSIBILITY_YES);
302 getShortcutsAndWidgets().setImportantForAccessibility(IMPORTANT_FOR_ACCESSIBILITY_YES);
303 setOnClickListener(mTouchHelper);
304 }
305
306 // Invalidate the accessibility hierarchy
307 if (getParent() != null) {
308 getParent().notifySubtreeAccessibilityStateChanged(
309 this, this, AccessibilityEvent.CONTENT_CHANGE_TYPE_SUBTREE);
310 }
311 }
312
313 @Override
314 public boolean dispatchHoverEvent(MotionEvent event) {
315 // Always attempt to dispatch hover events to accessibility first.
316 if (mUseTouchHelper && mTouchHelper.dispatchHoverEvent(event)) {
317 return true;
318 }
319 return super.dispatchHoverEvent(event);
320 }
321
322 @Override
Adam Cohenc9735cf2015-01-23 16:11:55 -0800323 public boolean onInterceptTouchEvent(MotionEvent ev) {
324 if (mUseTouchHelper ||
325 (mInterceptTouchListener != null && mInterceptTouchListener.onTouch(this, ev))) {
326 return true;
327 }
328 return false;
329 }
330
Mady Melloref044dd2015-06-02 15:35:07 -0700331 @Override
332 public boolean onTouchEvent(MotionEvent ev) {
333 boolean handled = super.onTouchEvent(ev);
334 // Stylus button press on a home screen should not switch between overview mode and
335 // the home screen mode, however, once in overview mode stylus button press should be
336 // enabled to allow rearranging the different home screens. So check what mode
337 // the workspace is in, and only perform stylus button presses while in overview mode.
338 if (mLauncher.mWorkspace.isInOverviewMode()
Mady Mellorbb835202015-07-15 16:34:34 -0700339 && mStylusEventHelper.onMotionEvent(ev)) {
Mady Melloref044dd2015-06-02 15:35:07 -0700340 return true;
341 }
342 return handled;
343 }
344
Chris Craik01f2d7f2013-10-01 14:41:56 -0700345 public void enableHardwareLayer(boolean hasLayer) {
346 mShortcutsAndWidgets.setLayerType(hasLayer ? LAYER_TYPE_HARDWARE : LAYER_TYPE_NONE, sPaint);
Michael Jurkad51f33a2012-06-28 15:35:26 -0700347 }
348
349 public void buildHardwareLayer() {
350 mShortcutsAndWidgets.buildLayer();
Adam Cohen2801caf2011-05-13 20:57:39 -0700351 }
352
Adam Cohen307fe232012-08-16 17:55:58 -0700353 public float getChildrenScale() {
354 return mIsHotseat ? mHotseatScale : 1.0f;
355 }
356
Winson Chung5f8afe62013-08-12 16:19:28 -0700357 public void setCellDimensions(int width, int height) {
Winson Chung11a1a532013-09-13 11:14:45 -0700358 mFixedCellWidth = mCellWidth = width;
359 mFixedCellHeight = mCellHeight = height;
Winson Chung5f8afe62013-08-12 16:19:28 -0700360 mShortcutsAndWidgets.setCellDimensions(mCellWidth, mCellHeight, mWidthGap, mHeightGap,
361 mCountX, mCountY);
362 }
363
Adam Cohen2801caf2011-05-13 20:57:39 -0700364 public void setGridSize(int x, int y) {
365 mCountX = x;
366 mCountY = y;
367 mOccupied = new boolean[mCountX][mCountY];
Adam Cohen482ed822012-03-02 14:15:13 -0800368 mTmpOccupied = new boolean[mCountX][mCountY];
Adam Cohen7fbec102012-03-27 12:42:19 -0700369 mTempRectStack.clear();
Adam Cohen2374abf2013-04-16 14:56:57 -0700370 mShortcutsAndWidgets.setCellDimensions(mCellWidth, mCellHeight, mWidthGap, mHeightGap,
Winson Chung5f8afe62013-08-12 16:19:28 -0700371 mCountX, mCountY);
Adam Cohen76fc0852011-06-17 13:26:23 -0700372 requestLayout();
Adam Cohen2801caf2011-05-13 20:57:39 -0700373 }
374
Adam Cohen2374abf2013-04-16 14:56:57 -0700375 // Set whether or not to invert the layout horizontally if the layout is in RTL mode.
376 public void setInvertIfRtl(boolean invert) {
377 mShortcutsAndWidgets.setInvertIfRtl(invert);
378 }
379
Adam Cohen917e3882013-10-31 15:03:35 -0700380 public void setDropPending(boolean pending) {
381 mDropPending = pending;
382 }
383
384 public boolean isDropPending() {
385 return mDropPending;
386 }
387
Sunny Goyal4b6eb262015-05-14 19:24:40 -0700388 @Override
389 public void setPressedIcon(BubbleTextView icon, Bitmap background) {
Sunny Goyal508da152014-08-14 10:53:27 -0700390 if (icon == null || background == null) {
391 mTouchFeedbackView.setBitmap(null);
392 mTouchFeedbackView.animate().cancel();
393 } else {
Sunny Goyal508da152014-08-14 10:53:27 -0700394 if (mTouchFeedbackView.setBitmap(background)) {
Sunny Goyal4fe5a372015-05-14 19:55:10 -0700395 mTouchFeedbackView.alignWithIconView(icon, mShortcutsAndWidgets);
396 mTouchFeedbackView.animateShadow();
Sunny Goyal508da152014-08-14 10:53:27 -0700397 }
Patrick Dubroy96864c32011-03-10 17:17:23 -0800398 }
399 }
400
Adam Cohenc50438c2014-08-19 17:43:05 -0700401 void disableDragTarget() {
402 mIsDragTarget = false;
403 }
404
Vadim Tryshevfedca432015-08-19 17:55:02 -0700405 public boolean isDragTarget() {
Adam Cohenc50438c2014-08-19 17:43:05 -0700406 return mIsDragTarget;
407 }
408
409 void setIsDragOverlapping(boolean isDragOverlapping) {
410 if (mIsDragOverlapping != isDragOverlapping) {
411 mIsDragOverlapping = isDragOverlapping;
Sunny Goyal2805e632015-05-20 15:35:32 -0700412 if (mIsDragOverlapping) {
413 mBackground.startTransition(BACKGROUND_ACTIVATE_DURATION);
414 } else {
Winson Chunge8f1d042015-07-31 12:39:57 -0700415 if (mBackgroundAlpha > 0f) {
416 mBackground.reverseTransition(BACKGROUND_ACTIVATE_DURATION);
417 } else {
418 mBackground.resetTransition();
419 }
Sunny Goyal2805e632015-05-20 15:35:32 -0700420 }
Adam Cohenc50438c2014-08-19 17:43:05 -0700421 invalidate();
422 }
423 }
424
Sunny Goyale2fd14b2015-08-27 17:45:46 -0700425 public void disableJailContent() {
426 mJailContent = false;
427 }
428
429 @Override
430 protected void dispatchSaveInstanceState(SparseArray<Parcelable> container) {
431 if (mJailContent) {
432 ParcelableSparseArray jail = getJailedArray(container);
433 super.dispatchSaveInstanceState(jail);
434 container.put(R.id.cell_layout_jail_id, jail);
435 } else {
436 super.dispatchSaveInstanceState(container);
437 }
438 }
439
440 @Override
441 protected void dispatchRestoreInstanceState(SparseArray<Parcelable> container) {
442 super.dispatchRestoreInstanceState(mJailContent ? getJailedArray(container) : container);
443 }
444
445 private ParcelableSparseArray getJailedArray(SparseArray<Parcelable> container) {
446 final Parcelable parcelable = container.get(R.id.cell_layout_jail_id);
447 return parcelable instanceof ParcelableSparseArray ?
448 (ParcelableSparseArray) parcelable : new ParcelableSparseArray();
449 }
450
Vadim Tryshevfedca432015-08-19 17:55:02 -0700451 public boolean getIsDragOverlapping() {
Michael Jurka33945b22010-12-21 18:19:38 -0800452 return mIsDragOverlapping;
453 }
454
Jeff Sharkey83f111d2009-04-20 21:03:13 -0700455 @Override
Patrick Dubroy1262e362010-10-06 15:49:50 -0700456 protected void onDraw(Canvas canvas) {
Sunny Goyal05739772015-05-19 19:59:09 -0700457 if (!mIsDragTarget) {
458 return;
459 }
460
Michael Jurka3e7c7632010-10-02 16:01:03 -0700461 // When we're large, we are either drawn in a "hover" state (ie when dragging an item to
462 // a neighboring page) or with just a normal background (if backgroundAlpha > 0.0f)
463 // When we're small, we are either drawn normally or in the "accepts drops" state (during
464 // a drag). However, we also drag the mini hover background *over* one of those two
465 // backgrounds
Sunny Goyal05739772015-05-19 19:59:09 -0700466 if (mBackgroundAlpha > 0.0f) {
Sunny Goyal2805e632015-05-20 15:35:32 -0700467 mBackground.draw(canvas);
Michael Jurkaa63c4522010-08-19 13:52:27 -0700468 }
Romain Guya6abce82009-11-10 02:54:41 -0800469
Patrick Dubroy8e58e912010-10-14 13:21:48 -0700470 final Paint paint = mDragOutlinePaint;
Joe Onorato4be866d2010-10-10 11:26:02 -0700471 for (int i = 0; i < mDragOutlines.length; i++) {
Chet Haase472b2812010-10-14 07:02:04 -0700472 final float alpha = mDragOutlineAlphas[i];
Joe Onorato4be866d2010-10-10 11:26:02 -0700473 if (alpha > 0) {
Joe Onorato4be866d2010-10-10 11:26:02 -0700474 final Bitmap b = (Bitmap) mDragOutlineAnims[i].getTag();
Chet Haase472b2812010-10-14 07:02:04 -0700475 paint.setAlpha((int)(alpha + .5f));
Sunny Goyal106bf642015-07-16 12:18:06 -0700476 canvas.drawBitmap(b, null, mDragOutlines[i], paint);
Winson Chung150fbab2010-09-29 17:14:26 -0700477 }
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700478 }
Patrick Dubroy96864c32011-03-10 17:17:23 -0800479
Adam Cohen482ed822012-03-02 14:15:13 -0800480 if (DEBUG_VISUALIZE_OCCUPIED) {
481 int[] pt = new int[2];
482 ColorDrawable cd = new ColorDrawable(Color.RED);
Adam Cohene7587d22012-05-24 18:50:02 -0700483 cd.setBounds(0, 0, mCellWidth, mCellHeight);
Adam Cohen482ed822012-03-02 14:15:13 -0800484 for (int i = 0; i < mCountX; i++) {
485 for (int j = 0; j < mCountY; j++) {
486 if (mOccupied[i][j]) {
487 cellToPoint(i, j, pt);
488 canvas.save();
489 canvas.translate(pt[0], pt[1]);
490 cd.draw(canvas);
491 canvas.restore();
492 }
493 }
494 }
495 }
496
Andrew Flynn850d2e72012-04-26 16:51:20 -0700497 int previewOffset = FolderRingAnimator.sPreviewSize;
498
Adam Cohen69ce2e52011-07-03 19:25:21 -0700499 // The folder outer / inner ring image(s)
Adam Cohen2e6da152015-05-06 11:42:25 -0700500 DeviceProfile grid = mLauncher.getDeviceProfile();
Adam Cohen69ce2e52011-07-03 19:25:21 -0700501 for (int i = 0; i < mFolderOuterRings.size(); i++) {
502 FolderRingAnimator fra = mFolderOuterRings.get(i);
503
Adam Cohen5108bc02013-09-20 17:04:51 -0700504 Drawable d;
505 int width, height;
Adam Cohen69ce2e52011-07-03 19:25:21 -0700506 cellToPoint(fra.mCellX, fra.mCellY, mTempLocation);
Winson Chung5f8afe62013-08-12 16:19:28 -0700507 View child = getChildAt(fra.mCellX, fra.mCellY);
Adam Cohen558f1c22013-10-09 15:15:24 -0700508
Winson Chung89f97052013-09-20 11:32:26 -0700509 if (child != null) {
Adam Cohen558f1c22013-10-09 15:15:24 -0700510 int centerX = mTempLocation[0] + mCellWidth / 2;
511 int centerY = mTempLocation[1] + previewOffset / 2 +
512 child.getPaddingTop() + grid.folderBackgroundOffset;
513
Adam Cohen5108bc02013-09-20 17:04:51 -0700514 // Draw outer ring, if it exists
515 if (FolderIcon.HAS_OUTER_RING) {
516 d = FolderRingAnimator.sSharedOuterRingDrawable;
517 width = (int) (fra.getOuterRingSize() * getChildrenScale());
518 height = width;
519 canvas.save();
520 canvas.translate(centerX - width / 2, centerY - height / 2);
521 d.setBounds(0, 0, width, height);
522 d.draw(canvas);
523 canvas.restore();
524 }
Adam Cohen69ce2e52011-07-03 19:25:21 -0700525
Winson Chung89f97052013-09-20 11:32:26 -0700526 // Draw inner ring
527 d = FolderRingAnimator.sSharedInnerRingDrawable;
528 width = (int) (fra.getInnerRingSize() * getChildrenScale());
529 height = width;
Winson Chung89f97052013-09-20 11:32:26 -0700530 canvas.save();
531 canvas.translate(centerX - width / 2, centerY - width / 2);
532 d.setBounds(0, 0, width, height);
533 d.draw(canvas);
534 canvas.restore();
535 }
Adam Cohen69ce2e52011-07-03 19:25:21 -0700536 }
Adam Cohenc51934b2011-07-26 21:07:43 -0700537
538 if (mFolderLeaveBehindCell[0] >= 0 && mFolderLeaveBehindCell[1] >= 0) {
539 Drawable d = FolderIcon.sSharedFolderLeaveBehind;
540 int width = d.getIntrinsicWidth();
541 int height = d.getIntrinsicHeight();
542
543 cellToPoint(mFolderLeaveBehindCell[0], mFolderLeaveBehindCell[1], mTempLocation);
Winson Chung5f8afe62013-08-12 16:19:28 -0700544 View child = getChildAt(mFolderLeaveBehindCell[0], mFolderLeaveBehindCell[1]);
Winson Chung89f97052013-09-20 11:32:26 -0700545 if (child != null) {
546 int centerX = mTempLocation[0] + mCellWidth / 2;
547 int centerY = mTempLocation[1] + previewOffset / 2 +
548 child.getPaddingTop() + grid.folderBackgroundOffset;
Adam Cohenc51934b2011-07-26 21:07:43 -0700549
Winson Chung89f97052013-09-20 11:32:26 -0700550 canvas.save();
551 canvas.translate(centerX - width / 2, centerY - width / 2);
552 d.setBounds(0, 0, width, height);
553 d.draw(canvas);
554 canvas.restore();
555 }
Adam Cohenc51934b2011-07-26 21:07:43 -0700556 }
Adam Cohen69ce2e52011-07-03 19:25:21 -0700557 }
558
559 public void showFolderAccept(FolderRingAnimator fra) {
560 mFolderOuterRings.add(fra);
561 }
562
563 public void hideFolderAccept(FolderRingAnimator fra) {
564 if (mFolderOuterRings.contains(fra)) {
565 mFolderOuterRings.remove(fra);
566 }
567 invalidate();
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700568 }
569
Adam Cohenc51934b2011-07-26 21:07:43 -0700570 public void setFolderLeaveBehindCell(int x, int y) {
571 mFolderLeaveBehindCell[0] = x;
572 mFolderLeaveBehindCell[1] = y;
573 invalidate();
574 }
575
576 public void clearFolderLeaveBehind() {
577 mFolderLeaveBehindCell[0] = -1;
578 mFolderLeaveBehindCell[1] = -1;
579 invalidate();
580 }
581
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700582 @Override
Michael Jurkae6235dd2011-10-04 15:02:05 -0700583 public boolean shouldDelayChildPressedState() {
584 return false;
585 }
586
Adam Cohen1462de32012-07-24 22:34:36 -0700587 public void restoreInstanceState(SparseArray<Parcelable> states) {
Sunny Goyal33a152f2014-07-22 12:13:14 -0700588 try {
589 dispatchRestoreInstanceState(states);
590 } catch (IllegalArgumentException ex) {
Sunny Goyal6c56c682015-07-16 14:09:05 -0700591 if (ProviderConfig.IS_DOGFOOD_BUILD) {
Sunny Goyal33a152f2014-07-22 12:13:14 -0700592 throw ex;
593 }
594 // Mismatched viewId / viewType preventing restore. Skip restore on production builds.
595 Log.e(TAG, "Ignoring an error while restoring a view instance state", ex);
596 }
Adam Cohen1462de32012-07-24 22:34:36 -0700597 }
598
Michael Jurkae6235dd2011-10-04 15:02:05 -0700599 @Override
Jeff Sharkey83f111d2009-04-20 21:03:13 -0700600 public void cancelLongPress() {
601 super.cancelLongPress();
602
603 // Cancel long press for all children
604 final int count = getChildCount();
605 for (int i = 0; i < count; i++) {
606 final View child = getChildAt(i);
607 child.cancelLongPress();
608 }
609 }
610
Michael Jurkadee05892010-07-27 10:01:56 -0700611 public void setOnInterceptTouchListener(View.OnTouchListener listener) {
612 mInterceptTouchListener = listener;
613 }
614
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800615 public int getCountX() {
Adam Cohend22015c2010-07-26 22:02:18 -0700616 return mCountX;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800617 }
618
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800619 public int getCountY() {
Adam Cohend22015c2010-07-26 22:02:18 -0700620 return mCountY;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800621 }
622
Andrew Flynn0dca1ec2012-02-29 13:33:22 -0800623 public void setIsHotseat(boolean isHotseat) {
624 mIsHotseat = isHotseat;
Winson Chung5f8afe62013-08-12 16:19:28 -0700625 mShortcutsAndWidgets.setIsHotseat(isHotseat);
Andrew Flynn0dca1ec2012-02-29 13:33:22 -0800626 }
627
Sunny Goyale9b651e2015-04-24 11:44:51 -0700628 public boolean isHotseat() {
629 return mIsHotseat;
630 }
631
Andrew Flynn0dca1ec2012-02-29 13:33:22 -0800632 public boolean addViewToCellLayout(View child, int index, int childId, LayoutParams params,
Sunny Goyalf7a29e82015-04-24 15:20:43 -0700633 boolean markCells) {
Winson Chungaafa03c2010-06-11 17:34:16 -0700634 final LayoutParams lp = params;
635
Andrew Flynnde38e422012-05-08 11:22:15 -0700636 // Hotseat icons - remove text
Andrew Flynn0dca1ec2012-02-29 13:33:22 -0800637 if (child instanceof BubbleTextView) {
638 BubbleTextView bubbleChild = (BubbleTextView) child;
Winson Chung5f8afe62013-08-12 16:19:28 -0700639 bubbleChild.setTextVisibility(!mIsHotseat);
Andrew Flynn0dca1ec2012-02-29 13:33:22 -0800640 }
641
Adam Cohen307fe232012-08-16 17:55:58 -0700642 child.setScaleX(getChildrenScale());
643 child.setScaleY(getChildrenScale());
644
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800645 // Generate an id for each view, this assumes we have at most 256x256 cells
646 // per workspace screen
Adam Cohend22015c2010-07-26 22:02:18 -0700647 if (lp.cellX >= 0 && lp.cellX <= mCountX - 1 && lp.cellY >= 0 && lp.cellY <= mCountY - 1) {
Winson Chungaafa03c2010-06-11 17:34:16 -0700648 // If the horizontal or vertical span is set to -1, it is taken to
649 // mean that it spans the extent of the CellLayout
Adam Cohend22015c2010-07-26 22:02:18 -0700650 if (lp.cellHSpan < 0) lp.cellHSpan = mCountX;
651 if (lp.cellVSpan < 0) lp.cellVSpan = mCountY;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800652
Winson Chungaafa03c2010-06-11 17:34:16 -0700653 child.setId(childId);
Sunny Goyalf7a29e82015-04-24 15:20:43 -0700654 mShortcutsAndWidgets.addView(child, index, lp);
Michael Jurkadee05892010-07-27 10:01:56 -0700655
Michael Jurkaf3ca3ab2010-10-20 17:08:24 -0700656 if (markCells) markCellsAsOccupiedForView(child);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700657
Winson Chungaafa03c2010-06-11 17:34:16 -0700658 return true;
659 }
660 return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800661 }
Michael Jurka3e7c7632010-10-02 16:01:03 -0700662
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800663 @Override
Michael Jurka0280c3b2010-09-17 15:00:07 -0700664 public void removeAllViews() {
665 clearOccupiedCells();
Michael Jurkaa52570f2012-03-20 03:18:20 -0700666 mShortcutsAndWidgets.removeAllViews();
Michael Jurka0280c3b2010-09-17 15:00:07 -0700667 }
668
669 @Override
670 public void removeAllViewsInLayout() {
Michael Jurkaa52570f2012-03-20 03:18:20 -0700671 if (mShortcutsAndWidgets.getChildCount() > 0) {
Michael Jurka7cfc2822011-08-02 20:19:24 -0700672 clearOccupiedCells();
Michael Jurkaa52570f2012-03-20 03:18:20 -0700673 mShortcutsAndWidgets.removeAllViewsInLayout();
Michael Jurka7cfc2822011-08-02 20:19:24 -0700674 }
Michael Jurka0280c3b2010-09-17 15:00:07 -0700675 }
676
677 @Override
678 public void removeView(View view) {
679 markCellsAsUnoccupiedForView(view);
Michael Jurkaa52570f2012-03-20 03:18:20 -0700680 mShortcutsAndWidgets.removeView(view);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700681 }
682
683 @Override
684 public void removeViewAt(int index) {
Michael Jurkaa52570f2012-03-20 03:18:20 -0700685 markCellsAsUnoccupiedForView(mShortcutsAndWidgets.getChildAt(index));
686 mShortcutsAndWidgets.removeViewAt(index);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700687 }
688
689 @Override
690 public void removeViewInLayout(View view) {
691 markCellsAsUnoccupiedForView(view);
Michael Jurkaa52570f2012-03-20 03:18:20 -0700692 mShortcutsAndWidgets.removeViewInLayout(view);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700693 }
694
695 @Override
696 public void removeViews(int start, int count) {
697 for (int i = start; i < start + count; i++) {
Michael Jurkaa52570f2012-03-20 03:18:20 -0700698 markCellsAsUnoccupiedForView(mShortcutsAndWidgets.getChildAt(i));
Michael Jurka0280c3b2010-09-17 15:00:07 -0700699 }
Michael Jurkaa52570f2012-03-20 03:18:20 -0700700 mShortcutsAndWidgets.removeViews(start, count);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700701 }
702
703 @Override
704 public void removeViewsInLayout(int start, int count) {
705 for (int i = start; i < start + count; i++) {
Michael Jurkaa52570f2012-03-20 03:18:20 -0700706 markCellsAsUnoccupiedForView(mShortcutsAndWidgets.getChildAt(i));
Michael Jurka0280c3b2010-09-17 15:00:07 -0700707 }
Michael Jurkaa52570f2012-03-20 03:18:20 -0700708 mShortcutsAndWidgets.removeViewsInLayout(start, count);
Michael Jurkaabded662011-03-04 12:06:57 -0800709 }
710
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700711 /**
Winson Chungaafa03c2010-06-11 17:34:16 -0700712 * Given a point, return the cell that strictly encloses that point
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800713 * @param x X coordinate of the point
714 * @param y Y coordinate of the point
715 * @param result Array of 2 ints to hold the x and y coordinate of the cell
716 */
Sunny Goyale9b651e2015-04-24 11:44:51 -0700717 public void pointToCellExact(int x, int y, int[] result) {
Winson Chung4b825dcd2011-06-19 12:41:22 -0700718 final int hStartPadding = getPaddingLeft();
719 final int vStartPadding = getPaddingTop();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800720
721 result[0] = (x - hStartPadding) / (mCellWidth + mWidthGap);
722 result[1] = (y - vStartPadding) / (mCellHeight + mHeightGap);
723
Adam Cohend22015c2010-07-26 22:02:18 -0700724 final int xAxis = mCountX;
725 final int yAxis = mCountY;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800726
727 if (result[0] < 0) result[0] = 0;
728 if (result[0] >= xAxis) result[0] = xAxis - 1;
729 if (result[1] < 0) result[1] = 0;
730 if (result[1] >= yAxis) result[1] = yAxis - 1;
731 }
Winson Chungaafa03c2010-06-11 17:34:16 -0700732
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800733 /**
734 * Given a point, return the cell that most closely encloses that point
735 * @param x X coordinate of the point
736 * @param y Y coordinate of the point
737 * @param result Array of 2 ints to hold the x and y coordinate of the cell
738 */
739 void pointToCellRounded(int x, int y, int[] result) {
740 pointToCellExact(x + (mCellWidth / 2), y + (mCellHeight / 2), result);
741 }
742
743 /**
744 * Given a cell coordinate, return the point that represents the upper left corner of that cell
Winson Chungaafa03c2010-06-11 17:34:16 -0700745 *
746 * @param cellX X coordinate of the cell
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800747 * @param cellY Y coordinate of the cell
Winson Chungaafa03c2010-06-11 17:34:16 -0700748 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800749 * @param result Array of 2 ints to hold the x and y coordinate of the point
750 */
751 void cellToPoint(int cellX, int cellY, int[] result) {
Winson Chung4b825dcd2011-06-19 12:41:22 -0700752 final int hStartPadding = getPaddingLeft();
753 final int vStartPadding = getPaddingTop();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800754
755 result[0] = hStartPadding + cellX * (mCellWidth + mWidthGap);
756 result[1] = vStartPadding + cellY * (mCellHeight + mHeightGap);
757 }
758
Adam Cohene3e27a82011-04-15 12:07:39 -0700759 /**
Adam Cohen482ed822012-03-02 14:15:13 -0800760 * Given a cell coordinate, return the point that represents the center of the cell
Adam Cohene3e27a82011-04-15 12:07:39 -0700761 *
762 * @param cellX X coordinate of the cell
763 * @param cellY Y coordinate of the cell
764 *
765 * @param result Array of 2 ints to hold the x and y coordinate of the point
766 */
767 void cellToCenterPoint(int cellX, int cellY, int[] result) {
Adam Cohen47a876d2012-03-19 13:21:41 -0700768 regionToCenterPoint(cellX, cellY, 1, 1, result);
769 }
770
771 /**
772 * Given a cell coordinate and span return the point that represents the center of the regio
773 *
774 * @param cellX X coordinate of the cell
775 * @param cellY Y coordinate of the cell
776 *
777 * @param result Array of 2 ints to hold the x and y coordinate of the point
778 */
779 void regionToCenterPoint(int cellX, int cellY, int spanX, int spanY, int[] result) {
Winson Chung4b825dcd2011-06-19 12:41:22 -0700780 final int hStartPadding = getPaddingLeft();
781 final int vStartPadding = getPaddingTop();
Adam Cohen47a876d2012-03-19 13:21:41 -0700782 result[0] = hStartPadding + cellX * (mCellWidth + mWidthGap) +
783 (spanX * mCellWidth + (spanX - 1) * mWidthGap) / 2;
784 result[1] = vStartPadding + cellY * (mCellHeight + mHeightGap) +
785 (spanY * mCellHeight + (spanY - 1) * mHeightGap) / 2;
Adam Cohene3e27a82011-04-15 12:07:39 -0700786 }
787
Adam Cohen19f37922012-03-21 11:59:11 -0700788 /**
789 * Given a cell coordinate and span fills out a corresponding pixel rect
790 *
791 * @param cellX X coordinate of the cell
792 * @param cellY Y coordinate of the cell
793 * @param result Rect in which to write the result
794 */
795 void regionToRect(int cellX, int cellY, int spanX, int spanY, Rect result) {
796 final int hStartPadding = getPaddingLeft();
797 final int vStartPadding = getPaddingTop();
798 final int left = hStartPadding + cellX * (mCellWidth + mWidthGap);
799 final int top = vStartPadding + cellY * (mCellHeight + mHeightGap);
800 result.set(left, top, left + (spanX * mCellWidth + (spanX - 1) * mWidthGap),
801 top + (spanY * mCellHeight + (spanY - 1) * mHeightGap));
802 }
803
Adam Cohen482ed822012-03-02 14:15:13 -0800804 public float getDistanceFromCell(float x, float y, int[] cell) {
805 cellToCenterPoint(cell[0], cell[1], mTmpPoint);
Sunny Goyalf7a29e82015-04-24 15:20:43 -0700806 return (float) Math.hypot(x - mTmpPoint[0], y - mTmpPoint[1]);
Adam Cohen482ed822012-03-02 14:15:13 -0800807 }
808
Romain Guy84f296c2009-11-04 15:00:44 -0800809 int getCellWidth() {
810 return mCellWidth;
811 }
812
813 int getCellHeight() {
814 return mCellHeight;
815 }
816
Adam Cohend4844c32011-02-18 19:25:06 -0800817 int getWidthGap() {
818 return mWidthGap;
819 }
820
821 int getHeightGap() {
822 return mHeightGap;
823 }
824
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700825 public void setFixedSize(int width, int height) {
826 mFixedWidth = width;
827 mFixedHeight = height;
828 }
829
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800830 @Override
831 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800832 int widthSpecMode = MeasureSpec.getMode(widthMeasureSpec);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800833 int heightSpecMode = MeasureSpec.getMode(heightMeasureSpec);
Winson Chung5f8afe62013-08-12 16:19:28 -0700834 int widthSize = MeasureSpec.getSize(widthMeasureSpec);
835 int heightSize = MeasureSpec.getSize(heightMeasureSpec);
Winson Chung2d75f122013-09-23 16:53:31 -0700836 int childWidthSize = widthSize - (getPaddingLeft() + getPaddingRight());
837 int childHeightSize = heightSize - (getPaddingTop() + getPaddingBottom());
Winson Chung11a1a532013-09-13 11:14:45 -0700838 if (mFixedCellWidth < 0 || mFixedCellHeight < 0) {
Sunny Goyalc6205602015-05-21 20:46:33 -0700839 int cw = DeviceProfile.calculateCellWidth(childWidthSize, mCountX);
840 int ch = DeviceProfile.calculateCellHeight(childHeightSize, mCountY);
Winson Chung11a1a532013-09-13 11:14:45 -0700841 if (cw != mCellWidth || ch != mCellHeight) {
842 mCellWidth = cw;
843 mCellHeight = ch;
844 mShortcutsAndWidgets.setCellDimensions(mCellWidth, mCellHeight, mWidthGap,
845 mHeightGap, mCountX, mCountY);
846 }
Winson Chung5f8afe62013-08-12 16:19:28 -0700847 }
Winson Chungaafa03c2010-06-11 17:34:16 -0700848
Winson Chung2d75f122013-09-23 16:53:31 -0700849 int newWidth = childWidthSize;
850 int newHeight = childHeightSize;
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700851 if (mFixedWidth > 0 && mFixedHeight > 0) {
852 newWidth = mFixedWidth;
853 newHeight = mFixedHeight;
854 } else if (widthSpecMode == MeasureSpec.UNSPECIFIED || heightSpecMode == MeasureSpec.UNSPECIFIED) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800855 throw new RuntimeException("CellLayout cannot have UNSPECIFIED dimensions");
856 }
857
Adam Cohend22015c2010-07-26 22:02:18 -0700858 int numWidthGaps = mCountX - 1;
859 int numHeightGaps = mCountY - 1;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800860
Adam Cohen234c4cd2011-07-17 21:03:04 -0700861 if (mOriginalWidthGap < 0 || mOriginalHeightGap < 0) {
Winson Chung2d75f122013-09-23 16:53:31 -0700862 int hSpace = childWidthSize;
863 int vSpace = childHeightSize;
Adam Cohenf4bd5792012-04-27 11:35:29 -0700864 int hFreeSpace = hSpace - (mCountX * mCellWidth);
865 int vFreeSpace = vSpace - (mCountY * mCellHeight);
Winson Chung4b825dcd2011-06-19 12:41:22 -0700866 mWidthGap = Math.min(mMaxGap, numWidthGaps > 0 ? (hFreeSpace / numWidthGaps) : 0);
867 mHeightGap = Math.min(mMaxGap,numHeightGaps > 0 ? (vFreeSpace / numHeightGaps) : 0);
Winson Chung5f8afe62013-08-12 16:19:28 -0700868 mShortcutsAndWidgets.setCellDimensions(mCellWidth, mCellHeight, mWidthGap,
869 mHeightGap, mCountX, mCountY);
Adam Cohen234c4cd2011-07-17 21:03:04 -0700870 } else {
871 mWidthGap = mOriginalWidthGap;
872 mHeightGap = mOriginalHeightGap;
Winson Chungece7f5b2010-10-22 14:54:12 -0700873 }
Sunny Goyal4fe5a372015-05-14 19:55:10 -0700874
875 // Make the feedback view large enough to hold the blur bitmap.
876 mTouchFeedbackView.measure(
877 MeasureSpec.makeMeasureSpec(mCellWidth + mTouchFeedbackView.getExtraSize(),
878 MeasureSpec.EXACTLY),
879 MeasureSpec.makeMeasureSpec(mCellHeight + mTouchFeedbackView.getExtraSize(),
880 MeasureSpec.EXACTLY));
881
882 mShortcutsAndWidgets.measure(
883 MeasureSpec.makeMeasureSpec(newWidth, MeasureSpec.EXACTLY),
884 MeasureSpec.makeMeasureSpec(newHeight, MeasureSpec.EXACTLY));
885
886 int maxWidth = mShortcutsAndWidgets.getMeasuredWidth();
887 int maxHeight = mShortcutsAndWidgets.getMeasuredHeight();
Winson Chung2d75f122013-09-23 16:53:31 -0700888 if (mFixedWidth > 0 && mFixedHeight > 0) {
889 setMeasuredDimension(maxWidth, maxHeight);
890 } else {
891 setMeasuredDimension(widthSize, heightSize);
892 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800893 }
894
895 @Override
Michael Jurka28750fb2010-09-24 17:43:49 -0700896 protected void onLayout(boolean changed, int l, int t, int r, int b) {
Winson Chung38848ca2013-10-08 12:03:44 -0700897 int offset = getMeasuredWidth() - getPaddingLeft() - getPaddingRight() -
898 (mCountX * mCellWidth);
899 int left = getPaddingLeft() + (int) Math.ceil(offset / 2f);
900 int top = getPaddingTop();
Sunny Goyal4fe5a372015-05-14 19:55:10 -0700901
902 mTouchFeedbackView.layout(left, top,
903 left + mTouchFeedbackView.getMeasuredWidth(),
904 top + mTouchFeedbackView.getMeasuredHeight());
905 mShortcutsAndWidgets.layout(left, top,
906 left + r - l,
907 top + b - t);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800908 }
909
910 @Override
Michael Jurkadee05892010-07-27 10:01:56 -0700911 protected void onSizeChanged(int w, int h, int oldw, int oldh) {
912 super.onSizeChanged(w, h, oldw, oldh);
Winson Chung82a9bd22013-10-08 16:02:34 -0700913
914 // Expand the background drawing bounds by the padding baked into the background drawable
Sunny Goyal2805e632015-05-20 15:35:32 -0700915 mBackground.getPadding(mTempRect);
916 mBackground.setBounds(-mTempRect.left, -mTempRect.top,
917 w + mTempRect.right, h + mTempRect.bottom);
Michael Jurkadee05892010-07-27 10:01:56 -0700918 }
919
920 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800921 protected void setChildrenDrawingCacheEnabled(boolean enabled) {
Michael Jurkaa52570f2012-03-20 03:18:20 -0700922 mShortcutsAndWidgets.setChildrenDrawingCacheEnabled(enabled);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800923 }
924
925 @Override
926 protected void setChildrenDrawnWithCacheEnabled(boolean enabled) {
Michael Jurkaa52570f2012-03-20 03:18:20 -0700927 mShortcutsAndWidgets.setChildrenDrawnWithCacheEnabled(enabled);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800928 }
929
Michael Jurka5f1c5092010-09-03 14:15:02 -0700930 public float getBackgroundAlpha() {
931 return mBackgroundAlpha;
Michael Jurkadee05892010-07-27 10:01:56 -0700932 }
933
Michael Jurka5f1c5092010-09-03 14:15:02 -0700934 public void setBackgroundAlpha(float alpha) {
Michael Jurkaafaa0502011-12-13 18:22:50 -0800935 if (mBackgroundAlpha != alpha) {
936 mBackgroundAlpha = alpha;
Sunny Goyal2805e632015-05-20 15:35:32 -0700937 mBackground.setAlpha((int) (mBackgroundAlpha * 255));
Michael Jurkaafaa0502011-12-13 18:22:50 -0800938 }
Michael Jurkadee05892010-07-27 10:01:56 -0700939 }
940
Sunny Goyal2805e632015-05-20 15:35:32 -0700941 @Override
942 protected boolean verifyDrawable(Drawable who) {
943 return super.verifyDrawable(who) || (mIsDragTarget && who == mBackground);
944 }
945
Michael Jurkaa52570f2012-03-20 03:18:20 -0700946 public void setShortcutAndWidgetAlpha(float alpha) {
Sunny Goyal02b50812014-09-10 15:44:42 -0700947 mShortcutsAndWidgets.setAlpha(alpha);
Michael Jurkadee05892010-07-27 10:01:56 -0700948 }
949
Michael Jurkaa52570f2012-03-20 03:18:20 -0700950 public ShortcutAndWidgetContainer getShortcutsAndWidgets() {
Sunny Goyaldcbcc862014-08-12 15:58:36 -0700951 return mShortcutsAndWidgets;
Michael Jurkaa52570f2012-03-20 03:18:20 -0700952 }
953
Patrick Dubroy440c3602010-07-13 17:50:32 -0700954 public View getChildAt(int x, int y) {
Michael Jurkaa52570f2012-03-20 03:18:20 -0700955 return mShortcutsAndWidgets.getChildAt(x, y);
Patrick Dubroy440c3602010-07-13 17:50:32 -0700956 }
957
Adam Cohen76fc0852011-06-17 13:26:23 -0700958 public boolean animateChildToPosition(final View child, int cellX, int cellY, int duration,
Adam Cohen482ed822012-03-02 14:15:13 -0800959 int delay, boolean permanent, boolean adjustOccupied) {
Michael Jurkaa52570f2012-03-20 03:18:20 -0700960 ShortcutAndWidgetContainer clc = getShortcutsAndWidgets();
Adam Cohen482ed822012-03-02 14:15:13 -0800961 boolean[][] occupied = mOccupied;
962 if (!permanent) {
963 occupied = mTmpOccupied;
964 }
965
Adam Cohen19f37922012-03-21 11:59:11 -0700966 if (clc.indexOfChild(child) != -1) {
Adam Cohenbfbfd262011-06-13 16:55:12 -0700967 final LayoutParams lp = (LayoutParams) child.getLayoutParams();
968 final ItemInfo info = (ItemInfo) child.getTag();
969
970 // We cancel any existing animations
971 if (mReorderAnimators.containsKey(lp)) {
972 mReorderAnimators.get(lp).cancel();
973 mReorderAnimators.remove(lp);
974 }
975
Adam Cohen482ed822012-03-02 14:15:13 -0800976 final int oldX = lp.x;
977 final int oldY = lp.y;
978 if (adjustOccupied) {
979 occupied[lp.cellX][lp.cellY] = false;
980 occupied[cellX][cellY] = true;
981 }
Adam Cohenbfbfd262011-06-13 16:55:12 -0700982 lp.isLockedToGrid = true;
Adam Cohen482ed822012-03-02 14:15:13 -0800983 if (permanent) {
984 lp.cellX = info.cellX = cellX;
985 lp.cellY = info.cellY = cellY;
986 } else {
987 lp.tmpCellX = cellX;
988 lp.tmpCellY = cellY;
989 }
Adam Cohenbfbfd262011-06-13 16:55:12 -0700990 clc.setupLp(lp);
991 lp.isLockedToGrid = false;
Adam Cohen482ed822012-03-02 14:15:13 -0800992 final int newX = lp.x;
993 final int newY = lp.y;
Adam Cohenbfbfd262011-06-13 16:55:12 -0700994
Adam Cohen76fc0852011-06-17 13:26:23 -0700995 lp.x = oldX;
996 lp.y = oldY;
Adam Cohen76fc0852011-06-17 13:26:23 -0700997
Adam Cohen482ed822012-03-02 14:15:13 -0800998 // Exit early if we're not actually moving the view
999 if (oldX == newX && oldY == newY) {
1000 lp.isLockedToGrid = true;
1001 return true;
1002 }
1003
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001004 ValueAnimator va = LauncherAnimUtils.ofFloat(child, 0f, 1f);
Adam Cohen482ed822012-03-02 14:15:13 -08001005 va.setDuration(duration);
1006 mReorderAnimators.put(lp, va);
1007
1008 va.addUpdateListener(new AnimatorUpdateListener() {
1009 @Override
Adam Cohenbfbfd262011-06-13 16:55:12 -07001010 public void onAnimationUpdate(ValueAnimator animation) {
Adam Cohen482ed822012-03-02 14:15:13 -08001011 float r = ((Float) animation.getAnimatedValue()).floatValue();
Adam Cohen19f37922012-03-21 11:59:11 -07001012 lp.x = (int) ((1 - r) * oldX + r * newX);
1013 lp.y = (int) ((1 - r) * oldY + r * newY);
Adam Cohen6b8a02d2012-03-22 15:13:40 -07001014 child.requestLayout();
Adam Cohenbfbfd262011-06-13 16:55:12 -07001015 }
1016 });
Adam Cohen482ed822012-03-02 14:15:13 -08001017 va.addListener(new AnimatorListenerAdapter() {
Adam Cohenbfbfd262011-06-13 16:55:12 -07001018 boolean cancelled = false;
1019 public void onAnimationEnd(Animator animation) {
1020 // If the animation was cancelled, it means that another animation
1021 // has interrupted this one, and we don't want to lock the item into
1022 // place just yet.
1023 if (!cancelled) {
1024 lp.isLockedToGrid = true;
Adam Cohen482ed822012-03-02 14:15:13 -08001025 child.requestLayout();
Adam Cohenbfbfd262011-06-13 16:55:12 -07001026 }
1027 if (mReorderAnimators.containsKey(lp)) {
1028 mReorderAnimators.remove(lp);
1029 }
1030 }
1031 public void onAnimationCancel(Animator animation) {
1032 cancelled = true;
1033 }
1034 });
Adam Cohen482ed822012-03-02 14:15:13 -08001035 va.setStartDelay(delay);
1036 va.start();
Adam Cohenbfbfd262011-06-13 16:55:12 -07001037 return true;
1038 }
1039 return false;
1040 }
1041
Adam Cohen482ed822012-03-02 14:15:13 -08001042 void visualizeDropLocation(View v, Bitmap dragOutline, int originX, int originY, int cellX,
1043 int cellY, int spanX, int spanY, boolean resize, Point dragOffset, Rect dragRegion) {
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -07001044 final int oldDragCellX = mDragCell[0];
1045 final int oldDragCellY = mDragCell[1];
Adam Cohen482ed822012-03-02 14:15:13 -08001046
Adam Cohen2801caf2011-05-13 20:57:39 -07001047 if (dragOutline == null && v == null) {
Adam Cohen2801caf2011-05-13 20:57:39 -07001048 return;
1049 }
1050
Adam Cohen482ed822012-03-02 14:15:13 -08001051 if (cellX != oldDragCellX || cellY != oldDragCellY) {
1052 mDragCell[0] = cellX;
1053 mDragCell[1] = cellY;
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001054
Joe Onorato4be866d2010-10-10 11:26:02 -07001055 final int oldIndex = mDragOutlineCurrent;
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -07001056 mDragOutlineAnims[oldIndex].animateOut();
1057 mDragOutlineCurrent = (oldIndex + 1) % mDragOutlines.length;
Adam Cohend41fbf52012-02-16 23:53:59 -08001058 Rect r = mDragOutlines[mDragOutlineCurrent];
Sunny Goyal106bf642015-07-16 12:18:06 -07001059
Adam Cohend41fbf52012-02-16 23:53:59 -08001060 if (resize) {
Adam Cohen482ed822012-03-02 14:15:13 -08001061 cellToRect(cellX, cellY, spanX, spanY, r);
Sunny Goyal106bf642015-07-16 12:18:06 -07001062 } else {
1063 // Find the top left corner of the rect the object will occupy
1064 final int[] topLeft = mTmpPoint;
1065 cellToPoint(cellX, cellY, topLeft);
1066
1067 int left = topLeft[0];
1068 int top = topLeft[1];
1069
1070 if (v != null && dragOffset == null) {
1071 // When drawing the drag outline, it did not account for margin offsets
1072 // added by the view's parent.
1073 MarginLayoutParams lp = (MarginLayoutParams) v.getLayoutParams();
1074 left += lp.leftMargin;
1075 top += lp.topMargin;
1076
1077 // Offsets due to the size difference between the View and the dragOutline.
1078 // There is a size difference to account for the outer blur, which may lie
1079 // outside the bounds of the view.
1080 top += (v.getHeight() - dragOutline.getHeight()) / 2;
1081 // We center about the x axis
1082 left += ((mCellWidth * spanX) + ((spanX - 1) * mWidthGap)
1083 - dragOutline.getWidth()) / 2;
1084 } else {
1085 if (dragOffset != null && dragRegion != null) {
1086 // Center the drag region *horizontally* in the cell and apply a drag
1087 // outline offset
1088 left += dragOffset.x + ((mCellWidth * spanX) + ((spanX - 1) * mWidthGap)
1089 - dragRegion.width()) / 2;
1090 int cHeight = getShortcutsAndWidgets().getCellContentHeight();
1091 int cellPaddingY = (int) Math.max(0, ((mCellHeight - cHeight) / 2f));
1092 top += dragOffset.y + cellPaddingY;
1093 } else {
1094 // Center the drag outline in the cell
1095 left += ((mCellWidth * spanX) + ((spanX - 1) * mWidthGap)
1096 - dragOutline.getWidth()) / 2;
1097 top += ((mCellHeight * spanY) + ((spanY - 1) * mHeightGap)
1098 - dragOutline.getHeight()) / 2;
1099 }
1100 }
1101 r.set(left, top, left + dragOutline.getWidth(), top + dragOutline.getHeight());
Adam Cohend41fbf52012-02-16 23:53:59 -08001102 }
Winson Chung150fbab2010-09-29 17:14:26 -07001103
Sunny Goyal106bf642015-07-16 12:18:06 -07001104 Utilities.scaleRectAboutCenter(r, getChildrenScale());
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -07001105 mDragOutlineAnims[mDragOutlineCurrent].setTag(dragOutline);
1106 mDragOutlineAnims[mDragOutlineCurrent].animateIn();
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001107 }
1108 }
1109
Adam Cohene0310962011-04-18 16:15:31 -07001110 public void clearDragOutlines() {
1111 final int oldIndex = mDragOutlineCurrent;
1112 mDragOutlineAnims[oldIndex].animateOut();
Adam Cohend41fbf52012-02-16 23:53:59 -08001113 mDragCell[0] = mDragCell[1] = -1;
Adam Cohene0310962011-04-18 16:15:31 -07001114 }
1115
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001116 /**
Jeff Sharkey70864282009-04-07 21:08:40 -07001117 * Find a vacant area that will fit the given bounds nearest the requested
1118 * cell location. Uses Euclidean distance to score multiple vacant areas.
Winson Chungaafa03c2010-06-11 17:34:16 -07001119 *
Romain Guy51afc022009-05-04 18:03:43 -07001120 * @param pixelX The X location at which you want to search for a vacant area.
1121 * @param pixelY The Y location at which you want to search for a vacant area.
Adam Cohend41fbf52012-02-16 23:53:59 -08001122 * @param minSpanX The minimum horizontal span required
1123 * @param minSpanY The minimum vertical span required
1124 * @param spanX Horizontal span of the object.
1125 * @param spanY Vertical span of the object.
1126 * @param result Array in which to place the result, or null (in which case a new array will
1127 * be allocated)
1128 * @return The X, Y cell of a vacant area that can contain this object,
1129 * nearest the requested location.
1130 */
1131 int[] findNearestVacantArea(int pixelX, int pixelY, int minSpanX, int minSpanY, int spanX,
1132 int spanY, int[] result, int[] resultSpan) {
Sunny Goyalf7a29e82015-04-24 15:20:43 -07001133 return findNearestArea(pixelX, pixelY, minSpanX, minSpanY, spanX, spanY, true,
Adam Cohend41fbf52012-02-16 23:53:59 -08001134 result, resultSpan);
1135 }
1136
Adam Cohend41fbf52012-02-16 23:53:59 -08001137 private final Stack<Rect> mTempRectStack = new Stack<Rect>();
1138 private void lazyInitTempRectStack() {
1139 if (mTempRectStack.isEmpty()) {
1140 for (int i = 0; i < mCountX * mCountY; i++) {
1141 mTempRectStack.push(new Rect());
1142 }
1143 }
1144 }
Adam Cohen482ed822012-03-02 14:15:13 -08001145
Adam Cohend41fbf52012-02-16 23:53:59 -08001146 private void recycleTempRects(Stack<Rect> used) {
1147 while (!used.isEmpty()) {
1148 mTempRectStack.push(used.pop());
1149 }
1150 }
1151
1152 /**
1153 * Find a vacant area that will fit the given bounds nearest the requested
1154 * cell location. Uses Euclidean distance to score multiple vacant areas.
1155 *
1156 * @param pixelX The X location at which you want to search for a vacant area.
1157 * @param pixelY The Y location at which you want to search for a vacant area.
1158 * @param minSpanX The minimum horizontal span required
1159 * @param minSpanY The minimum vertical span required
1160 * @param spanX Horizontal span of the object.
1161 * @param spanY Vertical span of the object.
1162 * @param ignoreOccupied If true, the result can be an occupied cell
1163 * @param result Array in which to place the result, or null (in which case a new array will
1164 * be allocated)
1165 * @return The X, Y cell of a vacant area that can contain this object,
1166 * nearest the requested location.
1167 */
Sunny Goyalf7a29e82015-04-24 15:20:43 -07001168 private int[] findNearestArea(int pixelX, int pixelY, int minSpanX, int minSpanY, int spanX,
1169 int spanY, boolean ignoreOccupied, int[] result, int[] resultSpan) {
Adam Cohend41fbf52012-02-16 23:53:59 -08001170 lazyInitTempRectStack();
Michael Jurkac6ee42e2010-09-30 12:04:50 -07001171
Adam Cohene3e27a82011-04-15 12:07:39 -07001172 // For items with a spanX / spanY > 1, the passed in point (pixelX, pixelY) corresponds
1173 // to the center of the item, but we are searching based on the top-left cell, so
1174 // we translate the point over to correspond to the top-left.
1175 pixelX -= (mCellWidth + mWidthGap) * (spanX - 1) / 2f;
1176 pixelY -= (mCellHeight + mHeightGap) * (spanY - 1) / 2f;
1177
Jeff Sharkey70864282009-04-07 21:08:40 -07001178 // Keep track of best-scoring drop area
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001179 final int[] bestXY = result != null ? result : new int[2];
Jeff Sharkey70864282009-04-07 21:08:40 -07001180 double bestDistance = Double.MAX_VALUE;
Adam Cohend41fbf52012-02-16 23:53:59 -08001181 final Rect bestRect = new Rect(-1, -1, -1, -1);
1182 final Stack<Rect> validRegions = new Stack<Rect>();
Winson Chungaafa03c2010-06-11 17:34:16 -07001183
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001184 final int countX = mCountX;
1185 final int countY = mCountY;
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001186
Adam Cohend41fbf52012-02-16 23:53:59 -08001187 if (minSpanX <= 0 || minSpanY <= 0 || spanX <= 0 || spanY <= 0 ||
1188 spanX < minSpanX || spanY < minSpanY) {
1189 return bestXY;
1190 }
1191
1192 for (int y = 0; y < countY - (minSpanY - 1); y++) {
Michael Jurkac28de512010-08-13 11:27:44 -07001193 inner:
Adam Cohend41fbf52012-02-16 23:53:59 -08001194 for (int x = 0; x < countX - (minSpanX - 1); x++) {
1195 int ySize = -1;
1196 int xSize = -1;
Adam Cohendf035382011-04-11 17:22:04 -07001197 if (ignoreOccupied) {
Adam Cohend41fbf52012-02-16 23:53:59 -08001198 // First, let's see if this thing fits anywhere
1199 for (int i = 0; i < minSpanX; i++) {
1200 for (int j = 0; j < minSpanY; j++) {
Sunny Goyalf7a29e82015-04-24 15:20:43 -07001201 if (mOccupied[x + i][y + j]) {
Adam Cohendf035382011-04-11 17:22:04 -07001202 continue inner;
1203 }
Michael Jurkac28de512010-08-13 11:27:44 -07001204 }
1205 }
Adam Cohend41fbf52012-02-16 23:53:59 -08001206 xSize = minSpanX;
1207 ySize = minSpanY;
1208
1209 // We know that the item will fit at _some_ acceptable size, now let's see
1210 // how big we can make it. We'll alternate between incrementing x and y spans
1211 // until we hit a limit.
1212 boolean incX = true;
1213 boolean hitMaxX = xSize >= spanX;
1214 boolean hitMaxY = ySize >= spanY;
1215 while (!(hitMaxX && hitMaxY)) {
1216 if (incX && !hitMaxX) {
1217 for (int j = 0; j < ySize; j++) {
Sunny Goyalf7a29e82015-04-24 15:20:43 -07001218 if (x + xSize > countX -1 || mOccupied[x + xSize][y + j]) {
Adam Cohend41fbf52012-02-16 23:53:59 -08001219 // We can't move out horizontally
1220 hitMaxX = true;
1221 }
1222 }
1223 if (!hitMaxX) {
1224 xSize++;
1225 }
1226 } else if (!hitMaxY) {
1227 for (int i = 0; i < xSize; i++) {
Sunny Goyalf7a29e82015-04-24 15:20:43 -07001228 if (y + ySize > countY - 1 || mOccupied[x + i][y + ySize]) {
Adam Cohend41fbf52012-02-16 23:53:59 -08001229 // We can't move out vertically
1230 hitMaxY = true;
1231 }
1232 }
1233 if (!hitMaxY) {
1234 ySize++;
1235 }
1236 }
1237 hitMaxX |= xSize >= spanX;
1238 hitMaxY |= ySize >= spanY;
1239 incX = !incX;
1240 }
1241 incX = true;
1242 hitMaxX = xSize >= spanX;
1243 hitMaxY = ySize >= spanY;
Michael Jurkac28de512010-08-13 11:27:44 -07001244 }
Sunny Goyal2805e632015-05-20 15:35:32 -07001245 final int[] cellXY = mTmpPoint;
Adam Cohene3e27a82011-04-15 12:07:39 -07001246 cellToCenterPoint(x, y, cellXY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001247
Adam Cohend41fbf52012-02-16 23:53:59 -08001248 // We verify that the current rect is not a sub-rect of any of our previous
1249 // candidates. In this case, the current rect is disqualified in favour of the
1250 // containing rect.
1251 Rect currentRect = mTempRectStack.pop();
1252 currentRect.set(x, y, x + xSize, y + ySize);
1253 boolean contained = false;
1254 for (Rect r : validRegions) {
1255 if (r.contains(currentRect)) {
1256 contained = true;
1257 break;
1258 }
1259 }
1260 validRegions.push(currentRect);
Sunny Goyalf7a29e82015-04-24 15:20:43 -07001261 double distance = Math.hypot(cellXY[0] - pixelX, cellXY[1] - pixelY);
Adam Cohen482ed822012-03-02 14:15:13 -08001262
Adam Cohend41fbf52012-02-16 23:53:59 -08001263 if ((distance <= bestDistance && !contained) ||
1264 currentRect.contains(bestRect)) {
Michael Jurkac28de512010-08-13 11:27:44 -07001265 bestDistance = distance;
1266 bestXY[0] = x;
1267 bestXY[1] = y;
Adam Cohend41fbf52012-02-16 23:53:59 -08001268 if (resultSpan != null) {
1269 resultSpan[0] = xSize;
1270 resultSpan[1] = ySize;
1271 }
1272 bestRect.set(currentRect);
Michael Jurkac28de512010-08-13 11:27:44 -07001273 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001274 }
1275 }
1276
Adam Cohenc0dcf592011-06-01 15:30:43 -07001277 // Return -1, -1 if no suitable location found
1278 if (bestDistance == Double.MAX_VALUE) {
1279 bestXY[0] = -1;
1280 bestXY[1] = -1;
Jeff Sharkey70864282009-04-07 21:08:40 -07001281 }
Adam Cohend41fbf52012-02-16 23:53:59 -08001282 recycleTempRects(validRegions);
Adam Cohenc0dcf592011-06-01 15:30:43 -07001283 return bestXY;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001284 }
Winson Chungaafa03c2010-06-11 17:34:16 -07001285
Adam Cohen482ed822012-03-02 14:15:13 -08001286 /**
1287 * Find a vacant area that will fit the given bounds nearest the requested
1288 * cell location, and will also weigh in a suggested direction vector of the
1289 * desired location. This method computers distance based on unit grid distances,
1290 * not pixel distances.
1291 *
Adam Cohen47a876d2012-03-19 13:21:41 -07001292 * @param cellX The X cell nearest to which you want to search for a vacant area.
1293 * @param cellY The Y cell nearest which you want to search for a vacant area.
Adam Cohen482ed822012-03-02 14:15:13 -08001294 * @param spanX Horizontal span of the object.
1295 * @param spanY Vertical span of the object.
Adam Cohen47a876d2012-03-19 13:21:41 -07001296 * @param direction The favored direction in which the views should move from x, y
1297 * @param exactDirectionOnly If this parameter is true, then only solutions where the direction
1298 * matches exactly. Otherwise we find the best matching direction.
1299 * @param occoupied The array which represents which cells in the CellLayout are occupied
1300 * @param blockOccupied The array which represents which cells in the specified block (cellX,
Winson Chung5f8afe62013-08-12 16:19:28 -07001301 * cellY, spanX, spanY) are occupied. This is used when try to move a group of views.
Adam Cohen482ed822012-03-02 14:15:13 -08001302 * @param result Array in which to place the result, or null (in which case a new array will
1303 * be allocated)
1304 * @return The X, Y cell of a vacant area that can contain this object,
1305 * nearest the requested location.
1306 */
1307 private int[] findNearestArea(int cellX, int cellY, int spanX, int spanY, int[] direction,
Adam Cohen47a876d2012-03-19 13:21:41 -07001308 boolean[][] occupied, boolean blockOccupied[][], int[] result) {
Adam Cohen482ed822012-03-02 14:15:13 -08001309 // Keep track of best-scoring drop area
1310 final int[] bestXY = result != null ? result : new int[2];
1311 float bestDistance = Float.MAX_VALUE;
1312 int bestDirectionScore = Integer.MIN_VALUE;
1313
1314 final int countX = mCountX;
1315 final int countY = mCountY;
1316
1317 for (int y = 0; y < countY - (spanY - 1); y++) {
1318 inner:
1319 for (int x = 0; x < countX - (spanX - 1); x++) {
1320 // First, let's see if this thing fits anywhere
1321 for (int i = 0; i < spanX; i++) {
1322 for (int j = 0; j < spanY; j++) {
Adam Cohen47a876d2012-03-19 13:21:41 -07001323 if (occupied[x + i][y + j] && (blockOccupied == null || blockOccupied[i][j])) {
Adam Cohen482ed822012-03-02 14:15:13 -08001324 continue inner;
1325 }
1326 }
1327 }
1328
Sunny Goyalf7a29e82015-04-24 15:20:43 -07001329 float distance = (float) Math.hypot(x - cellX, y - cellY);
Adam Cohen482ed822012-03-02 14:15:13 -08001330 int[] curDirection = mTmpPoint;
Adam Cohen47a876d2012-03-19 13:21:41 -07001331 computeDirectionVector(x - cellX, y - cellY, curDirection);
1332 // The direction score is just the dot product of the two candidate direction
1333 // and that passed in.
Adam Cohen482ed822012-03-02 14:15:13 -08001334 int curDirectionScore = direction[0] * curDirection[0] +
1335 direction[1] * curDirection[1];
Adam Cohen47a876d2012-03-19 13:21:41 -07001336 boolean exactDirectionOnly = false;
1337 boolean directionMatches = direction[0] == curDirection[0] &&
1338 direction[0] == curDirection[0];
1339 if ((directionMatches || !exactDirectionOnly) &&
1340 Float.compare(distance, bestDistance) < 0 || (Float.compare(distance,
Adam Cohen482ed822012-03-02 14:15:13 -08001341 bestDistance) == 0 && curDirectionScore > bestDirectionScore)) {
1342 bestDistance = distance;
1343 bestDirectionScore = curDirectionScore;
1344 bestXY[0] = x;
1345 bestXY[1] = y;
1346 }
1347 }
1348 }
1349
1350 // Return -1, -1 if no suitable location found
1351 if (bestDistance == Float.MAX_VALUE) {
1352 bestXY[0] = -1;
1353 bestXY[1] = -1;
1354 }
1355 return bestXY;
1356 }
1357
1358 private boolean addViewToTempLocation(View v, Rect rectOccupiedByPotentialDrop,
Adam Cohen8baab352012-03-20 17:39:21 -07001359 int[] direction, ItemConfiguration currentState) {
1360 CellAndSpan c = currentState.map.get(v);
Adam Cohen482ed822012-03-02 14:15:13 -08001361 boolean success = false;
Adam Cohen8baab352012-03-20 17:39:21 -07001362 markCellsForView(c.x, c.y, c.spanX, c.spanY, mTmpOccupied, false);
Adam Cohen482ed822012-03-02 14:15:13 -08001363 markCellsForRect(rectOccupiedByPotentialDrop, mTmpOccupied, true);
1364
Adam Cohen8baab352012-03-20 17:39:21 -07001365 findNearestArea(c.x, c.y, c.spanX, c.spanY, direction, mTmpOccupied, null, mTempLocation);
Adam Cohen482ed822012-03-02 14:15:13 -08001366
1367 if (mTempLocation[0] >= 0 && mTempLocation[1] >= 0) {
Adam Cohen8baab352012-03-20 17:39:21 -07001368 c.x = mTempLocation[0];
1369 c.y = mTempLocation[1];
Adam Cohen482ed822012-03-02 14:15:13 -08001370 success = true;
Adam Cohen482ed822012-03-02 14:15:13 -08001371 }
Adam Cohen8baab352012-03-20 17:39:21 -07001372 markCellsForView(c.x, c.y, c.spanX, c.spanY, mTmpOccupied, true);
Adam Cohen482ed822012-03-02 14:15:13 -08001373 return success;
1374 }
1375
Adam Cohenf3900c22012-11-16 18:28:11 -08001376 /**
1377 * This helper class defines a cluster of views. It helps with defining complex edges
1378 * of the cluster and determining how those edges interact with other views. The edges
1379 * essentially define a fine-grained boundary around the cluster of views -- like a more
1380 * precise version of a bounding box.
1381 */
1382 private class ViewCluster {
1383 final static int LEFT = 0;
1384 final static int TOP = 1;
1385 final static int RIGHT = 2;
1386 final static int BOTTOM = 3;
Adam Cohen47a876d2012-03-19 13:21:41 -07001387
Adam Cohenf3900c22012-11-16 18:28:11 -08001388 ArrayList<View> views;
1389 ItemConfiguration config;
1390 Rect boundingRect = new Rect();
Adam Cohen47a876d2012-03-19 13:21:41 -07001391
Adam Cohenf3900c22012-11-16 18:28:11 -08001392 int[] leftEdge = new int[mCountY];
1393 int[] rightEdge = new int[mCountY];
1394 int[] topEdge = new int[mCountX];
1395 int[] bottomEdge = new int[mCountX];
1396 boolean leftEdgeDirty, rightEdgeDirty, topEdgeDirty, bottomEdgeDirty, boundingRectDirty;
1397
1398 @SuppressWarnings("unchecked")
1399 public ViewCluster(ArrayList<View> views, ItemConfiguration config) {
1400 this.views = (ArrayList<View>) views.clone();
1401 this.config = config;
1402 resetEdges();
Adam Cohen47a876d2012-03-19 13:21:41 -07001403 }
1404
Adam Cohenf3900c22012-11-16 18:28:11 -08001405 void resetEdges() {
1406 for (int i = 0; i < mCountX; i++) {
1407 topEdge[i] = -1;
1408 bottomEdge[i] = -1;
1409 }
1410 for (int i = 0; i < mCountY; i++) {
1411 leftEdge[i] = -1;
1412 rightEdge[i] = -1;
1413 }
1414 leftEdgeDirty = true;
1415 rightEdgeDirty = true;
1416 bottomEdgeDirty = true;
1417 topEdgeDirty = true;
1418 boundingRectDirty = true;
1419 }
Adam Cohen47a876d2012-03-19 13:21:41 -07001420
Adam Cohenf3900c22012-11-16 18:28:11 -08001421 void computeEdge(int which, int[] edge) {
1422 int count = views.size();
1423 for (int i = 0; i < count; i++) {
1424 CellAndSpan cs = config.map.get(views.get(i));
1425 switch (which) {
1426 case LEFT:
1427 int left = cs.x;
1428 for (int j = cs.y; j < cs.y + cs.spanY; j++) {
1429 if (left < edge[j] || edge[j] < 0) {
1430 edge[j] = left;
Adam Cohena56dc102012-07-13 13:41:42 -07001431 }
Adam Cohen47a876d2012-03-19 13:21:41 -07001432 }
Adam Cohenf3900c22012-11-16 18:28:11 -08001433 break;
1434 case RIGHT:
1435 int right = cs.x + cs.spanX;
1436 for (int j = cs.y; j < cs.y + cs.spanY; j++) {
1437 if (right > edge[j]) {
1438 edge[j] = right;
1439 }
1440 }
1441 break;
1442 case TOP:
1443 int top = cs.y;
1444 for (int j = cs.x; j < cs.x + cs.spanX; j++) {
1445 if (top < edge[j] || edge[j] < 0) {
1446 edge[j] = top;
1447 }
1448 }
1449 break;
1450 case BOTTOM:
1451 int bottom = cs.y + cs.spanY;
1452 for (int j = cs.x; j < cs.x + cs.spanX; j++) {
1453 if (bottom > edge[j]) {
1454 edge[j] = bottom;
1455 }
1456 }
1457 break;
Adam Cohen47a876d2012-03-19 13:21:41 -07001458 }
1459 }
1460 }
Adam Cohenf3900c22012-11-16 18:28:11 -08001461
1462 boolean isViewTouchingEdge(View v, int whichEdge) {
1463 CellAndSpan cs = config.map.get(v);
1464
1465 int[] edge = getEdge(whichEdge);
1466
1467 switch (whichEdge) {
1468 case LEFT:
1469 for (int i = cs.y; i < cs.y + cs.spanY; i++) {
1470 if (edge[i] == cs.x + cs.spanX) {
1471 return true;
1472 }
1473 }
1474 break;
1475 case RIGHT:
1476 for (int i = cs.y; i < cs.y + cs.spanY; i++) {
1477 if (edge[i] == cs.x) {
1478 return true;
1479 }
1480 }
1481 break;
1482 case TOP:
1483 for (int i = cs.x; i < cs.x + cs.spanX; i++) {
1484 if (edge[i] == cs.y + cs.spanY) {
1485 return true;
1486 }
1487 }
1488 break;
1489 case BOTTOM:
1490 for (int i = cs.x; i < cs.x + cs.spanX; i++) {
1491 if (edge[i] == cs.y) {
1492 return true;
1493 }
1494 }
1495 break;
1496 }
1497 return false;
1498 }
1499
1500 void shift(int whichEdge, int delta) {
1501 for (View v: views) {
1502 CellAndSpan c = config.map.get(v);
1503 switch (whichEdge) {
1504 case LEFT:
1505 c.x -= delta;
1506 break;
1507 case RIGHT:
1508 c.x += delta;
1509 break;
1510 case TOP:
1511 c.y -= delta;
1512 break;
1513 case BOTTOM:
1514 default:
1515 c.y += delta;
1516 break;
1517 }
1518 }
1519 resetEdges();
1520 }
1521
1522 public void addView(View v) {
1523 views.add(v);
1524 resetEdges();
1525 }
1526
1527 public Rect getBoundingRect() {
1528 if (boundingRectDirty) {
1529 boolean first = true;
1530 for (View v: views) {
1531 CellAndSpan c = config.map.get(v);
1532 if (first) {
1533 boundingRect.set(c.x, c.y, c.x + c.spanX, c.y + c.spanY);
1534 first = false;
1535 } else {
1536 boundingRect.union(c.x, c.y, c.x + c.spanX, c.y + c.spanY);
1537 }
1538 }
1539 }
1540 return boundingRect;
1541 }
1542
1543 public int[] getEdge(int which) {
1544 switch (which) {
1545 case LEFT:
1546 return getLeftEdge();
1547 case RIGHT:
1548 return getRightEdge();
1549 case TOP:
1550 return getTopEdge();
1551 case BOTTOM:
1552 default:
1553 return getBottomEdge();
1554 }
1555 }
1556
1557 public int[] getLeftEdge() {
1558 if (leftEdgeDirty) {
1559 computeEdge(LEFT, leftEdge);
1560 }
1561 return leftEdge;
1562 }
1563
1564 public int[] getRightEdge() {
1565 if (rightEdgeDirty) {
1566 computeEdge(RIGHT, rightEdge);
1567 }
1568 return rightEdge;
1569 }
1570
1571 public int[] getTopEdge() {
1572 if (topEdgeDirty) {
1573 computeEdge(TOP, topEdge);
1574 }
1575 return topEdge;
1576 }
1577
1578 public int[] getBottomEdge() {
1579 if (bottomEdgeDirty) {
1580 computeEdge(BOTTOM, bottomEdge);
1581 }
1582 return bottomEdge;
1583 }
1584
1585 PositionComparator comparator = new PositionComparator();
1586 class PositionComparator implements Comparator<View> {
1587 int whichEdge = 0;
1588 public int compare(View left, View right) {
1589 CellAndSpan l = config.map.get(left);
1590 CellAndSpan r = config.map.get(right);
1591 switch (whichEdge) {
1592 case LEFT:
1593 return (r.x + r.spanX) - (l.x + l.spanX);
1594 case RIGHT:
1595 return l.x - r.x;
1596 case TOP:
1597 return (r.y + r.spanY) - (l.y + l.spanY);
1598 case BOTTOM:
1599 default:
1600 return l.y - r.y;
1601 }
1602 }
1603 }
1604
1605 public void sortConfigurationForEdgePush(int edge) {
1606 comparator.whichEdge = edge;
1607 Collections.sort(config.sortedViews, comparator);
1608 }
Adam Cohen47a876d2012-03-19 13:21:41 -07001609 }
1610
Adam Cohenf3900c22012-11-16 18:28:11 -08001611 private boolean pushViewsToTempLocation(ArrayList<View> views, Rect rectOccupiedByPotentialDrop,
1612 int[] direction, View dragView, ItemConfiguration currentState) {
Adam Cohene0489502012-08-27 15:18:53 -07001613
Adam Cohenf3900c22012-11-16 18:28:11 -08001614 ViewCluster cluster = new ViewCluster(views, currentState);
1615 Rect clusterRect = cluster.getBoundingRect();
1616 int whichEdge;
1617 int pushDistance;
1618 boolean fail = false;
1619
1620 // Determine the edge of the cluster that will be leading the push and how far
1621 // the cluster must be shifted.
1622 if (direction[0] < 0) {
1623 whichEdge = ViewCluster.LEFT;
1624 pushDistance = clusterRect.right - rectOccupiedByPotentialDrop.left;
Adam Cohene0489502012-08-27 15:18:53 -07001625 } else if (direction[0] > 0) {
Adam Cohenf3900c22012-11-16 18:28:11 -08001626 whichEdge = ViewCluster.RIGHT;
1627 pushDistance = rectOccupiedByPotentialDrop.right - clusterRect.left;
1628 } else if (direction[1] < 0) {
1629 whichEdge = ViewCluster.TOP;
1630 pushDistance = clusterRect.bottom - rectOccupiedByPotentialDrop.top;
1631 } else {
1632 whichEdge = ViewCluster.BOTTOM;
1633 pushDistance = rectOccupiedByPotentialDrop.bottom - clusterRect.top;
Adam Cohene0489502012-08-27 15:18:53 -07001634 }
1635
Adam Cohenf3900c22012-11-16 18:28:11 -08001636 // Break early for invalid push distance.
1637 if (pushDistance <= 0) {
1638 return false;
Adam Cohene0489502012-08-27 15:18:53 -07001639 }
Adam Cohenf3900c22012-11-16 18:28:11 -08001640
1641 // Mark the occupied state as false for the group of views we want to move.
1642 for (View v: views) {
1643 CellAndSpan c = currentState.map.get(v);
1644 markCellsForView(c.x, c.y, c.spanX, c.spanY, mTmpOccupied, false);
1645 }
1646
1647 // We save the current configuration -- if we fail to find a solution we will revert
1648 // to the initial state. The process of finding a solution modifies the configuration
1649 // in place, hence the need for revert in the failure case.
1650 currentState.save();
1651
1652 // The pushing algorithm is simplified by considering the views in the order in which
1653 // they would be pushed by the cluster. For example, if the cluster is leading with its
1654 // left edge, we consider sort the views by their right edge, from right to left.
1655 cluster.sortConfigurationForEdgePush(whichEdge);
1656
1657 while (pushDistance > 0 && !fail) {
1658 for (View v: currentState.sortedViews) {
1659 // For each view that isn't in the cluster, we see if the leading edge of the
1660 // cluster is contacting the edge of that view. If so, we add that view to the
1661 // cluster.
1662 if (!cluster.views.contains(v) && v != dragView) {
1663 if (cluster.isViewTouchingEdge(v, whichEdge)) {
1664 LayoutParams lp = (LayoutParams) v.getLayoutParams();
1665 if (!lp.canReorder) {
1666 // The push solution includes the all apps button, this is not viable.
1667 fail = true;
1668 break;
1669 }
1670 cluster.addView(v);
1671 CellAndSpan c = currentState.map.get(v);
1672
1673 // Adding view to cluster, mark it as not occupied.
1674 markCellsForView(c.x, c.y, c.spanX, c.spanY, mTmpOccupied, false);
1675 }
1676 }
1677 }
1678 pushDistance--;
1679
1680 // The cluster has been completed, now we move the whole thing over in the appropriate
1681 // direction.
1682 cluster.shift(whichEdge, 1);
1683 }
1684
1685 boolean foundSolution = false;
1686 clusterRect = cluster.getBoundingRect();
1687
1688 // Due to the nature of the algorithm, the only check required to verify a valid solution
1689 // is to ensure that completed shifted cluster lies completely within the cell layout.
1690 if (!fail && clusterRect.left >= 0 && clusterRect.right <= mCountX && clusterRect.top >= 0 &&
1691 clusterRect.bottom <= mCountY) {
1692 foundSolution = true;
1693 } else {
1694 currentState.restore();
1695 }
1696
1697 // In either case, we set the occupied array as marked for the location of the views
1698 for (View v: cluster.views) {
1699 CellAndSpan c = currentState.map.get(v);
1700 markCellsForView(c.x, c.y, c.spanX, c.spanY, mTmpOccupied, true);
1701 }
1702
1703 return foundSolution;
Adam Cohene0489502012-08-27 15:18:53 -07001704 }
1705
Adam Cohen482ed822012-03-02 14:15:13 -08001706 private boolean addViewsToTempLocation(ArrayList<View> views, Rect rectOccupiedByPotentialDrop,
Adam Cohenf3900c22012-11-16 18:28:11 -08001707 int[] direction, View dragView, ItemConfiguration currentState) {
Adam Cohen482ed822012-03-02 14:15:13 -08001708 if (views.size() == 0) return true;
Adam Cohen482ed822012-03-02 14:15:13 -08001709
Adam Cohen8baab352012-03-20 17:39:21 -07001710 boolean success = false;
Adam Cohen482ed822012-03-02 14:15:13 -08001711 Rect boundingRect = null;
Adam Cohen8baab352012-03-20 17:39:21 -07001712 // We construct a rect which represents the entire group of views passed in
Adam Cohen482ed822012-03-02 14:15:13 -08001713 for (View v: views) {
Adam Cohen8baab352012-03-20 17:39:21 -07001714 CellAndSpan c = currentState.map.get(v);
Adam Cohen482ed822012-03-02 14:15:13 -08001715 if (boundingRect == null) {
Adam Cohen8baab352012-03-20 17:39:21 -07001716 boundingRect = new Rect(c.x, c.y, c.x + c.spanX, c.y + c.spanY);
Adam Cohen482ed822012-03-02 14:15:13 -08001717 } else {
Adam Cohen8baab352012-03-20 17:39:21 -07001718 boundingRect.union(c.x, c.y, c.x + c.spanX, c.y + c.spanY);
Adam Cohen482ed822012-03-02 14:15:13 -08001719 }
1720 }
Adam Cohen8baab352012-03-20 17:39:21 -07001721
Adam Cohen8baab352012-03-20 17:39:21 -07001722 // Mark the occupied state as false for the group of views we want to move.
Adam Cohenf3900c22012-11-16 18:28:11 -08001723 for (View v: views) {
Adam Cohen8baab352012-03-20 17:39:21 -07001724 CellAndSpan c = currentState.map.get(v);
1725 markCellsForView(c.x, c.y, c.spanX, c.spanY, mTmpOccupied, false);
1726 }
1727
Adam Cohen47a876d2012-03-19 13:21:41 -07001728 boolean[][] blockOccupied = new boolean[boundingRect.width()][boundingRect.height()];
1729 int top = boundingRect.top;
1730 int left = boundingRect.left;
Adam Cohen8baab352012-03-20 17:39:21 -07001731 // We mark more precisely which parts of the bounding rect are truly occupied, allowing
Adam Cohena56dc102012-07-13 13:41:42 -07001732 // for interlocking.
Adam Cohenf3900c22012-11-16 18:28:11 -08001733 for (View v: views) {
Adam Cohen8baab352012-03-20 17:39:21 -07001734 CellAndSpan c = currentState.map.get(v);
1735 markCellsForView(c.x - left, c.y - top, c.spanX, c.spanY, blockOccupied, true);
Adam Cohen47a876d2012-03-19 13:21:41 -07001736 }
1737
Adam Cohen482ed822012-03-02 14:15:13 -08001738 markCellsForRect(rectOccupiedByPotentialDrop, mTmpOccupied, true);
1739
Adam Cohenf3900c22012-11-16 18:28:11 -08001740 findNearestArea(boundingRect.left, boundingRect.top, boundingRect.width(),
1741 boundingRect.height(), direction, mTmpOccupied, blockOccupied, mTempLocation);
Adam Cohen482ed822012-03-02 14:15:13 -08001742
Adam Cohen8baab352012-03-20 17:39:21 -07001743 // If we successfuly found a location by pushing the block of views, we commit it
Adam Cohen482ed822012-03-02 14:15:13 -08001744 if (mTempLocation[0] >= 0 && mTempLocation[1] >= 0) {
Adam Cohen8baab352012-03-20 17:39:21 -07001745 int deltaX = mTempLocation[0] - boundingRect.left;
1746 int deltaY = mTempLocation[1] - boundingRect.top;
Adam Cohenf3900c22012-11-16 18:28:11 -08001747 for (View v: views) {
Adam Cohen8baab352012-03-20 17:39:21 -07001748 CellAndSpan c = currentState.map.get(v);
1749 c.x += deltaX;
1750 c.y += deltaY;
Adam Cohen482ed822012-03-02 14:15:13 -08001751 }
1752 success = true;
1753 }
Adam Cohen8baab352012-03-20 17:39:21 -07001754
1755 // In either case, we set the occupied array as marked for the location of the views
Adam Cohenf3900c22012-11-16 18:28:11 -08001756 for (View v: views) {
Adam Cohen8baab352012-03-20 17:39:21 -07001757 CellAndSpan c = currentState.map.get(v);
1758 markCellsForView(c.x, c.y, c.spanX, c.spanY, mTmpOccupied, true);
Adam Cohen482ed822012-03-02 14:15:13 -08001759 }
1760 return success;
1761 }
1762
1763 private void markCellsForRect(Rect r, boolean[][] occupied, boolean value) {
1764 markCellsForView(r.left, r.top, r.width(), r.height(), occupied, value);
1765 }
1766
Adam Cohen4abc5bd2012-05-29 21:06:03 -07001767 // This method tries to find a reordering solution which satisfies the push mechanic by trying
1768 // to push items in each of the cardinal directions, in an order based on the direction vector
1769 // passed.
1770 private boolean attemptPushInDirection(ArrayList<View> intersectingViews, Rect occupied,
1771 int[] direction, View ignoreView, ItemConfiguration solution) {
1772 if ((Math.abs(direction[0]) + Math.abs(direction[1])) > 1) {
Winson Chung5f8afe62013-08-12 16:19:28 -07001773 // If the direction vector has two non-zero components, we try pushing
Adam Cohen4abc5bd2012-05-29 21:06:03 -07001774 // separately in each of the components.
1775 int temp = direction[1];
1776 direction[1] = 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 direction[1] = temp;
1783 temp = direction[0];
1784 direction[0] = 0;
Adam Cohenf3900c22012-11-16 18:28:11 -08001785
1786 if (pushViewsToTempLocation(intersectingViews, occupied, direction,
Adam Cohen4abc5bd2012-05-29 21:06:03 -07001787 ignoreView, solution)) {
1788 return true;
1789 }
1790 // Revert the direction
1791 direction[0] = temp;
1792
1793 // Now we try pushing in each component of the opposite direction
1794 direction[0] *= -1;
1795 direction[1] *= -1;
1796 temp = direction[1];
1797 direction[1] = 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
1803 direction[1] = temp;
1804 temp = direction[0];
1805 direction[0] = 0;
Adam Cohenf3900c22012-11-16 18:28:11 -08001806 if (pushViewsToTempLocation(intersectingViews, occupied, direction,
Adam Cohen4abc5bd2012-05-29 21:06:03 -07001807 ignoreView, solution)) {
1808 return true;
1809 }
1810 // revert the direction
1811 direction[0] = temp;
1812 direction[0] *= -1;
1813 direction[1] *= -1;
Winson Chung5f8afe62013-08-12 16:19:28 -07001814
Adam Cohen4abc5bd2012-05-29 21:06:03 -07001815 } else {
1816 // If the direction vector has a single non-zero component, we push first in the
1817 // direction of the vector
Adam Cohenf3900c22012-11-16 18:28:11 -08001818 if (pushViewsToTempLocation(intersectingViews, occupied, direction,
Adam Cohen4abc5bd2012-05-29 21:06:03 -07001819 ignoreView, solution)) {
1820 return true;
1821 }
Adam Cohen4abc5bd2012-05-29 21:06:03 -07001822 // Then we try the opposite direction
1823 direction[0] *= -1;
1824 direction[1] *= -1;
Adam Cohenf3900c22012-11-16 18:28:11 -08001825 if (pushViewsToTempLocation(intersectingViews, occupied, direction,
Adam Cohen4abc5bd2012-05-29 21:06:03 -07001826 ignoreView, solution)) {
1827 return true;
1828 }
1829 // Switch the direction back
1830 direction[0] *= -1;
1831 direction[1] *= -1;
Winson Chung5f8afe62013-08-12 16:19:28 -07001832
1833 // If we have failed to find a push solution with the above, then we try
Adam Cohen4abc5bd2012-05-29 21:06:03 -07001834 // to find a solution by pushing along the perpendicular axis.
1835
1836 // Swap the components
1837 int temp = direction[1];
1838 direction[1] = direction[0];
1839 direction[0] = temp;
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
1845 // Then we try the opposite direction
1846 direction[0] *= -1;
1847 direction[1] *= -1;
Adam Cohenf3900c22012-11-16 18:28:11 -08001848 if (pushViewsToTempLocation(intersectingViews, occupied, direction,
Adam Cohen4abc5bd2012-05-29 21:06:03 -07001849 ignoreView, solution)) {
1850 return true;
1851 }
1852 // Switch the direction back
1853 direction[0] *= -1;
1854 direction[1] *= -1;
1855
1856 // Swap the components back
1857 temp = direction[1];
1858 direction[1] = direction[0];
1859 direction[0] = temp;
1860 }
1861 return false;
1862 }
1863
Adam Cohen482ed822012-03-02 14:15:13 -08001864 private boolean rearrangementExists(int cellX, int cellY, int spanX, int spanY, int[] direction,
Adam Cohen8baab352012-03-20 17:39:21 -07001865 View ignoreView, ItemConfiguration solution) {
Winson Chunge3e03bc2012-05-01 15:10:11 -07001866 // Return early if get invalid cell positions
1867 if (cellX < 0 || cellY < 0) return false;
Adam Cohen482ed822012-03-02 14:15:13 -08001868
Adam Cohen8baab352012-03-20 17:39:21 -07001869 mIntersectingViews.clear();
Adam Cohen482ed822012-03-02 14:15:13 -08001870 mOccupiedRect.set(cellX, cellY, cellX + spanX, cellY + spanY);
Adam Cohen482ed822012-03-02 14:15:13 -08001871
Adam Cohen8baab352012-03-20 17:39:21 -07001872 // Mark the desired location of the view currently being dragged.
Adam Cohen482ed822012-03-02 14:15:13 -08001873 if (ignoreView != null) {
Adam Cohen8baab352012-03-20 17:39:21 -07001874 CellAndSpan c = solution.map.get(ignoreView);
Adam Cohen19f37922012-03-21 11:59:11 -07001875 if (c != null) {
1876 c.x = cellX;
1877 c.y = cellY;
1878 }
Adam Cohen482ed822012-03-02 14:15:13 -08001879 }
Adam Cohen482ed822012-03-02 14:15:13 -08001880 Rect r0 = new Rect(cellX, cellY, cellX + spanX, cellY + spanY);
1881 Rect r1 = new Rect();
Adam Cohen8baab352012-03-20 17:39:21 -07001882 for (View child: solution.map.keySet()) {
Adam Cohen482ed822012-03-02 14:15:13 -08001883 if (child == ignoreView) continue;
Adam Cohen8baab352012-03-20 17:39:21 -07001884 CellAndSpan c = solution.map.get(child);
Adam Cohen482ed822012-03-02 14:15:13 -08001885 LayoutParams lp = (LayoutParams) child.getLayoutParams();
Adam Cohen8baab352012-03-20 17:39:21 -07001886 r1.set(c.x, c.y, c.x + c.spanX, c.y + c.spanY);
Adam Cohen482ed822012-03-02 14:15:13 -08001887 if (Rect.intersects(r0, r1)) {
1888 if (!lp.canReorder) {
1889 return false;
1890 }
1891 mIntersectingViews.add(child);
1892 }
1893 }
Adam Cohen47a876d2012-03-19 13:21:41 -07001894
Adam Cohenfa3c58f2013-12-06 16:10:55 -08001895 solution.intersectingViews = new ArrayList<View>(mIntersectingViews);
1896
Winson Chung5f8afe62013-08-12 16:19:28 -07001897 // First we try to find a solution which respects the push mechanic. That is,
Adam Cohen4abc5bd2012-05-29 21:06:03 -07001898 // we try to find a solution such that no displaced item travels through another item
1899 // without also displacing that item.
1900 if (attemptPushInDirection(mIntersectingViews, mOccupiedRect, direction, ignoreView,
Adam Cohen19f37922012-03-21 11:59:11 -07001901 solution)) {
Adam Cohen47a876d2012-03-19 13:21:41 -07001902 return true;
1903 }
Adam Cohen47a876d2012-03-19 13:21:41 -07001904
Adam Cohen4abc5bd2012-05-29 21:06:03 -07001905 // Next we try moving the views as a block, but without requiring the push mechanic.
Adam Cohenf3900c22012-11-16 18:28:11 -08001906 if (addViewsToTempLocation(mIntersectingViews, mOccupiedRect, direction, ignoreView,
Adam Cohen19f37922012-03-21 11:59:11 -07001907 solution)) {
Adam Cohen482ed822012-03-02 14:15:13 -08001908 return true;
1909 }
Adam Cohen47a876d2012-03-19 13:21:41 -07001910
Adam Cohen482ed822012-03-02 14:15:13 -08001911 // Ok, they couldn't move as a block, let's move them individually
1912 for (View v : mIntersectingViews) {
Adam Cohen8baab352012-03-20 17:39:21 -07001913 if (!addViewToTempLocation(v, mOccupiedRect, direction, solution)) {
Adam Cohen482ed822012-03-02 14:15:13 -08001914 return false;
1915 }
1916 }
1917 return true;
1918 }
1919
1920 /*
1921 * Returns a pair (x, y), where x,y are in {-1, 0, 1} corresponding to vector between
1922 * the provided point and the provided cell
1923 */
Adam Cohen47a876d2012-03-19 13:21:41 -07001924 private void computeDirectionVector(float deltaX, float deltaY, int[] result) {
Adam Cohen482ed822012-03-02 14:15:13 -08001925 double angle = Math.atan(((float) deltaY) / deltaX);
1926
1927 result[0] = 0;
1928 result[1] = 0;
1929 if (Math.abs(Math.cos(angle)) > 0.5f) {
1930 result[0] = (int) Math.signum(deltaX);
1931 }
1932 if (Math.abs(Math.sin(angle)) > 0.5f) {
1933 result[1] = (int) Math.signum(deltaY);
1934 }
1935 }
1936
Adam Cohen8baab352012-03-20 17:39:21 -07001937 private void copyOccupiedArray(boolean[][] occupied) {
1938 for (int i = 0; i < mCountX; i++) {
1939 for (int j = 0; j < mCountY; j++) {
1940 occupied[i][j] = mOccupied[i][j];
1941 }
1942 }
1943 }
1944
Sunny Goyalf7a29e82015-04-24 15:20:43 -07001945 private ItemConfiguration findReorderSolution(int pixelX, int pixelY, int minSpanX, int minSpanY,
Adam Cohenfa3c58f2013-12-06 16:10:55 -08001946 int spanX, int spanY, int[] direction, View dragView, boolean decX,
1947 ItemConfiguration solution) {
Adam Cohen8baab352012-03-20 17:39:21 -07001948 // Copy the current state into the solution. This solution will be manipulated as necessary.
1949 copyCurrentStateToSolution(solution, false);
1950 // Copy the current occupied array into the temporary occupied array. This array will be
1951 // manipulated as necessary to find a solution.
1952 copyOccupiedArray(mTmpOccupied);
Adam Cohen482ed822012-03-02 14:15:13 -08001953
1954 // We find the nearest cell into which we would place the dragged item, assuming there's
1955 // nothing in its way.
1956 int result[] = new int[2];
1957 result = findNearestArea(pixelX, pixelY, spanX, spanY, result);
1958
1959 boolean success = false;
1960 // First we try the exact nearest position of the item being dragged,
1961 // we will then want to try to move this around to other neighbouring positions
Adam Cohen8baab352012-03-20 17:39:21 -07001962 success = rearrangementExists(result[0], result[1], spanX, spanY, direction, dragView,
1963 solution);
Adam Cohen482ed822012-03-02 14:15:13 -08001964
1965 if (!success) {
1966 // We try shrinking the widget down to size in an alternating pattern, shrink 1 in
1967 // x, then 1 in y etc.
1968 if (spanX > minSpanX && (minSpanY == spanY || decX)) {
Adam Cohenfa3c58f2013-12-06 16:10:55 -08001969 return findReorderSolution(pixelX, pixelY, minSpanX, minSpanY, spanX - 1, spanY,
1970 direction, dragView, false, solution);
Adam Cohen482ed822012-03-02 14:15:13 -08001971 } else if (spanY > minSpanY) {
Adam Cohenfa3c58f2013-12-06 16:10:55 -08001972 return findReorderSolution(pixelX, pixelY, minSpanX, minSpanY, spanX, spanY - 1,
1973 direction, dragView, true, solution);
Adam Cohen482ed822012-03-02 14:15:13 -08001974 }
1975 solution.isSolution = false;
1976 } else {
1977 solution.isSolution = true;
1978 solution.dragViewX = result[0];
1979 solution.dragViewY = result[1];
1980 solution.dragViewSpanX = spanX;
1981 solution.dragViewSpanY = spanY;
Adam Cohen482ed822012-03-02 14:15:13 -08001982 }
1983 return solution;
1984 }
1985
1986 private void copyCurrentStateToSolution(ItemConfiguration solution, boolean temp) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07001987 int childCount = mShortcutsAndWidgets.getChildCount();
Adam Cohen482ed822012-03-02 14:15:13 -08001988 for (int i = 0; i < childCount; i++) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07001989 View child = mShortcutsAndWidgets.getChildAt(i);
Adam Cohen482ed822012-03-02 14:15:13 -08001990 LayoutParams lp = (LayoutParams) child.getLayoutParams();
Adam Cohen8baab352012-03-20 17:39:21 -07001991 CellAndSpan c;
Adam Cohen482ed822012-03-02 14:15:13 -08001992 if (temp) {
Adam Cohen8baab352012-03-20 17:39:21 -07001993 c = new CellAndSpan(lp.tmpCellX, lp.tmpCellY, lp.cellHSpan, lp.cellVSpan);
Adam Cohen482ed822012-03-02 14:15:13 -08001994 } else {
Adam Cohen8baab352012-03-20 17:39:21 -07001995 c = new CellAndSpan(lp.cellX, lp.cellY, lp.cellHSpan, lp.cellVSpan);
Adam Cohen482ed822012-03-02 14:15:13 -08001996 }
Adam Cohenf3900c22012-11-16 18:28:11 -08001997 solution.add(child, c);
Adam Cohen482ed822012-03-02 14:15:13 -08001998 }
1999 }
2000
2001 private void copySolutionToTempState(ItemConfiguration solution, View dragView) {
2002 for (int i = 0; i < mCountX; i++) {
2003 for (int j = 0; j < mCountY; j++) {
2004 mTmpOccupied[i][j] = false;
2005 }
2006 }
2007
Michael Jurkaa52570f2012-03-20 03:18:20 -07002008 int childCount = mShortcutsAndWidgets.getChildCount();
Adam Cohen482ed822012-03-02 14:15:13 -08002009 for (int i = 0; i < childCount; i++) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07002010 View child = mShortcutsAndWidgets.getChildAt(i);
Adam Cohen482ed822012-03-02 14:15:13 -08002011 if (child == dragView) continue;
2012 LayoutParams lp = (LayoutParams) child.getLayoutParams();
Adam Cohen8baab352012-03-20 17:39:21 -07002013 CellAndSpan c = solution.map.get(child);
2014 if (c != null) {
2015 lp.tmpCellX = c.x;
2016 lp.tmpCellY = c.y;
2017 lp.cellHSpan = c.spanX;
2018 lp.cellVSpan = c.spanY;
2019 markCellsForView(c.x, c.y, c.spanX, c.spanY, mTmpOccupied, true);
Adam Cohen482ed822012-03-02 14:15:13 -08002020 }
2021 }
2022 markCellsForView(solution.dragViewX, solution.dragViewY, solution.dragViewSpanX,
2023 solution.dragViewSpanY, mTmpOccupied, true);
2024 }
2025
2026 private void animateItemsToSolution(ItemConfiguration solution, View dragView, boolean
2027 commitDragView) {
2028
2029 boolean[][] occupied = DESTRUCTIVE_REORDER ? mOccupied : mTmpOccupied;
2030 for (int i = 0; i < mCountX; i++) {
2031 for (int j = 0; j < mCountY; j++) {
2032 occupied[i][j] = false;
2033 }
2034 }
2035
Michael Jurkaa52570f2012-03-20 03:18:20 -07002036 int childCount = mShortcutsAndWidgets.getChildCount();
Adam Cohen482ed822012-03-02 14:15:13 -08002037 for (int i = 0; i < childCount; i++) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07002038 View child = mShortcutsAndWidgets.getChildAt(i);
Adam Cohen482ed822012-03-02 14:15:13 -08002039 if (child == dragView) continue;
Adam Cohen8baab352012-03-20 17:39:21 -07002040 CellAndSpan c = solution.map.get(child);
2041 if (c != null) {
Adam Cohen19f37922012-03-21 11:59:11 -07002042 animateChildToPosition(child, c.x, c.y, REORDER_ANIMATION_DURATION, 0,
2043 DESTRUCTIVE_REORDER, false);
Adam Cohen8baab352012-03-20 17:39:21 -07002044 markCellsForView(c.x, c.y, c.spanX, c.spanY, occupied, true);
Adam Cohen482ed822012-03-02 14:15:13 -08002045 }
2046 }
2047 if (commitDragView) {
2048 markCellsForView(solution.dragViewX, solution.dragViewY, solution.dragViewSpanX,
2049 solution.dragViewSpanY, occupied, true);
2050 }
2051 }
2052
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002053
2054 // This method starts or changes the reorder preview animations
2055 private void beginOrAdjustReorderPreviewAnimations(ItemConfiguration solution,
2056 View dragView, int delay, int mode) {
Adam Cohen19f37922012-03-21 11:59:11 -07002057 int childCount = mShortcutsAndWidgets.getChildCount();
Adam Cohen19f37922012-03-21 11:59:11 -07002058 for (int i = 0; i < childCount; i++) {
2059 View child = mShortcutsAndWidgets.getChildAt(i);
2060 if (child == dragView) continue;
2061 CellAndSpan c = solution.map.get(child);
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002062 boolean skip = mode == ReorderPreviewAnimation.MODE_HINT && solution.intersectingViews
2063 != null && !solution.intersectingViews.contains(child);
2064
Adam Cohen19f37922012-03-21 11:59:11 -07002065 LayoutParams lp = (LayoutParams) child.getLayoutParams();
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002066 if (c != null && !skip) {
2067 ReorderPreviewAnimation rha = new ReorderPreviewAnimation(child, mode, lp.cellX,
2068 lp.cellY, c.x, c.y, c.spanX, c.spanY);
Adam Cohend024f982012-05-23 18:26:45 -07002069 rha.animate();
Adam Cohen19f37922012-03-21 11:59:11 -07002070 }
2071 }
2072 }
2073
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002074 // Class which represents the reorder preview animations. These animations show that an item is
Adam Cohen19f37922012-03-21 11:59:11 -07002075 // in a temporary state, and hint at where the item will return to.
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002076 class ReorderPreviewAnimation {
Adam Cohen19f37922012-03-21 11:59:11 -07002077 View child;
Adam Cohend024f982012-05-23 18:26:45 -07002078 float finalDeltaX;
2079 float finalDeltaY;
2080 float initDeltaX;
2081 float initDeltaY;
2082 float finalScale;
2083 float initScale;
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002084 int mode;
2085 boolean repeating = false;
2086 private static final int PREVIEW_DURATION = 300;
2087 private static final int HINT_DURATION = Workspace.REORDER_TIMEOUT;
2088
2089 public static final int MODE_HINT = 0;
2090 public static final int MODE_PREVIEW = 1;
2091
Adam Cohene7587d22012-05-24 18:50:02 -07002092 Animator a;
Adam Cohen19f37922012-03-21 11:59:11 -07002093
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002094 public ReorderPreviewAnimation(View child, int mode, int cellX0, int cellY0, int cellX1,
2095 int cellY1, int spanX, int spanY) {
Adam Cohen19f37922012-03-21 11:59:11 -07002096 regionToCenterPoint(cellX0, cellY0, spanX, spanY, mTmpPoint);
2097 final int x0 = mTmpPoint[0];
2098 final int y0 = mTmpPoint[1];
2099 regionToCenterPoint(cellX1, cellY1, spanX, spanY, mTmpPoint);
2100 final int x1 = mTmpPoint[0];
2101 final int y1 = mTmpPoint[1];
2102 final int dX = x1 - x0;
2103 final int dY = y1 - y0;
Adam Cohend024f982012-05-23 18:26:45 -07002104 finalDeltaX = 0;
2105 finalDeltaY = 0;
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002106 int dir = mode == MODE_HINT ? -1 : 1;
Adam Cohen19f37922012-03-21 11:59:11 -07002107 if (dX == dY && dX == 0) {
2108 } else {
2109 if (dY == 0) {
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002110 finalDeltaX = - dir * Math.signum(dX) * mReorderPreviewAnimationMagnitude;
Adam Cohen19f37922012-03-21 11:59:11 -07002111 } else if (dX == 0) {
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002112 finalDeltaY = - dir * Math.signum(dY) * mReorderPreviewAnimationMagnitude;
Adam Cohen19f37922012-03-21 11:59:11 -07002113 } else {
2114 double angle = Math.atan( (float) (dY) / dX);
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002115 finalDeltaX = (int) (- dir * Math.signum(dX) *
2116 Math.abs(Math.cos(angle) * mReorderPreviewAnimationMagnitude));
2117 finalDeltaY = (int) (- dir * Math.signum(dY) *
2118 Math.abs(Math.sin(angle) * mReorderPreviewAnimationMagnitude));
Adam Cohen19f37922012-03-21 11:59:11 -07002119 }
2120 }
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002121 this.mode = mode;
Adam Cohend024f982012-05-23 18:26:45 -07002122 initDeltaX = child.getTranslationX();
2123 initDeltaY = child.getTranslationY();
Adam Cohen307fe232012-08-16 17:55:58 -07002124 finalScale = getChildrenScale() - 4.0f / child.getWidth();
Adam Cohend024f982012-05-23 18:26:45 -07002125 initScale = child.getScaleX();
Adam Cohen19f37922012-03-21 11:59:11 -07002126 this.child = child;
2127 }
2128
Adam Cohend024f982012-05-23 18:26:45 -07002129 void animate() {
Adam Cohen19f37922012-03-21 11:59:11 -07002130 if (mShakeAnimators.containsKey(child)) {
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002131 ReorderPreviewAnimation oldAnimation = mShakeAnimators.get(child);
Adam Cohend024f982012-05-23 18:26:45 -07002132 oldAnimation.cancel();
Adam Cohen19f37922012-03-21 11:59:11 -07002133 mShakeAnimators.remove(child);
Adam Cohene7587d22012-05-24 18:50:02 -07002134 if (finalDeltaX == 0 && finalDeltaY == 0) {
2135 completeAnimationImmediately();
2136 return;
2137 }
Adam Cohen19f37922012-03-21 11:59:11 -07002138 }
Adam Cohend024f982012-05-23 18:26:45 -07002139 if (finalDeltaX == 0 && finalDeltaY == 0) {
Adam Cohen19f37922012-03-21 11:59:11 -07002140 return;
2141 }
Michael Jurkaf1ad6082013-03-13 12:55:46 +01002142 ValueAnimator va = LauncherAnimUtils.ofFloat(child, 0f, 1f);
Adam Cohene7587d22012-05-24 18:50:02 -07002143 a = va;
Adam Cohen19f37922012-03-21 11:59:11 -07002144 va.setRepeatMode(ValueAnimator.REVERSE);
2145 va.setRepeatCount(ValueAnimator.INFINITE);
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002146 va.setDuration(mode == MODE_HINT ? HINT_DURATION : PREVIEW_DURATION);
Adam Cohend024f982012-05-23 18:26:45 -07002147 va.setStartDelay((int) (Math.random() * 60));
Adam Cohen19f37922012-03-21 11:59:11 -07002148 va.addUpdateListener(new AnimatorUpdateListener() {
2149 @Override
2150 public void onAnimationUpdate(ValueAnimator animation) {
2151 float r = ((Float) animation.getAnimatedValue()).floatValue();
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002152 float r1 = (mode == MODE_HINT && repeating) ? 1.0f : r;
2153 float x = r1 * finalDeltaX + (1 - r1) * initDeltaX;
2154 float y = r1 * finalDeltaY + (1 - r1) * initDeltaY;
Adam Cohen19f37922012-03-21 11:59:11 -07002155 child.setTranslationX(x);
2156 child.setTranslationY(y);
Adam Cohend024f982012-05-23 18:26:45 -07002157 float s = r * finalScale + (1 - r) * initScale;
Brandon Keely50e6e562012-05-08 16:28:49 -07002158 child.setScaleX(s);
2159 child.setScaleY(s);
Adam Cohen19f37922012-03-21 11:59:11 -07002160 }
2161 });
2162 va.addListener(new AnimatorListenerAdapter() {
2163 public void onAnimationRepeat(Animator animation) {
Adam Cohen19f37922012-03-21 11:59:11 -07002164 // We make sure to end only after a full period
Adam Cohend024f982012-05-23 18:26:45 -07002165 initDeltaX = 0;
2166 initDeltaY = 0;
Adam Cohen307fe232012-08-16 17:55:58 -07002167 initScale = getChildrenScale();
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002168 repeating = true;
Adam Cohen19f37922012-03-21 11:59:11 -07002169 }
2170 });
Adam Cohen19f37922012-03-21 11:59:11 -07002171 mShakeAnimators.put(child, this);
2172 va.start();
2173 }
2174
Adam Cohend024f982012-05-23 18:26:45 -07002175 private void cancel() {
Adam Cohene7587d22012-05-24 18:50:02 -07002176 if (a != null) {
2177 a.cancel();
2178 }
Adam Cohen19f37922012-03-21 11:59:11 -07002179 }
Adam Cohene7587d22012-05-24 18:50:02 -07002180
Adam Cohen091440a2015-03-18 14:16:05 -07002181 @Thunk void completeAnimationImmediately() {
Adam Cohene7587d22012-05-24 18:50:02 -07002182 if (a != null) {
2183 a.cancel();
2184 }
Brandon Keely50e6e562012-05-08 16:28:49 -07002185
Sunny Goyal5d2fc322015-07-06 22:52:49 -07002186 a = new LauncherViewPropertyAnimator(child)
2187 .scaleX(getChildrenScale())
2188 .scaleY(getChildrenScale())
2189 .translationX(0)
2190 .translationY(0)
2191 .setDuration(REORDER_ANIMATION_DURATION);
2192 a.setInterpolator(new android.view.animation.DecelerateInterpolator(1.5f));
2193 a.start();
Brandon Keely50e6e562012-05-08 16:28:49 -07002194 }
Adam Cohen19f37922012-03-21 11:59:11 -07002195 }
2196
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002197 private void completeAndClearReorderPreviewAnimations() {
2198 for (ReorderPreviewAnimation a: mShakeAnimators.values()) {
Brandon Keely50e6e562012-05-08 16:28:49 -07002199 a.completeAnimationImmediately();
Adam Cohen19f37922012-03-21 11:59:11 -07002200 }
2201 mShakeAnimators.clear();
2202 }
2203
Adam Cohen482ed822012-03-02 14:15:13 -08002204 private void commitTempPlacement() {
2205 for (int i = 0; i < mCountX; i++) {
2206 for (int j = 0; j < mCountY; j++) {
2207 mOccupied[i][j] = mTmpOccupied[i][j];
2208 }
2209 }
Sunny Goyalaa8ef112015-06-12 20:04:41 -07002210
2211 long screenId = mLauncher.getWorkspace().getIdForScreen(this);
2212 int container = Favorites.CONTAINER_DESKTOP;
2213
2214 if (mLauncher.isHotseatLayout(this)) {
2215 screenId = -1;
2216 container = Favorites.CONTAINER_HOTSEAT;
2217 }
2218
Michael Jurkaa52570f2012-03-20 03:18:20 -07002219 int childCount = mShortcutsAndWidgets.getChildCount();
Adam Cohen482ed822012-03-02 14:15:13 -08002220 for (int i = 0; i < childCount; i++) {
Adam Cohenea889a22012-03-27 16:45:39 -07002221 View child = mShortcutsAndWidgets.getChildAt(i);
2222 LayoutParams lp = (LayoutParams) child.getLayoutParams();
2223 ItemInfo info = (ItemInfo) child.getTag();
Adam Cohen2acce882012-03-28 19:03:19 -07002224 // We do a null check here because the item info can be null in the case of the
2225 // AllApps button in the hotseat.
2226 if (info != null) {
Sunny Goyalaa8ef112015-06-12 20:04:41 -07002227 final boolean requiresDbUpdate = (info.cellX != lp.tmpCellX
2228 || info.cellY != lp.tmpCellY || info.spanX != lp.cellHSpan
2229 || info.spanY != lp.cellVSpan);
2230
Adam Cohen2acce882012-03-28 19:03:19 -07002231 info.cellX = lp.cellX = lp.tmpCellX;
2232 info.cellY = lp.cellY = lp.tmpCellY;
Adam Cohenbebf0422012-04-11 18:06:28 -07002233 info.spanX = lp.cellHSpan;
2234 info.spanY = lp.cellVSpan;
Sunny Goyalaa8ef112015-06-12 20:04:41 -07002235
2236 if (requiresDbUpdate) {
2237 LauncherModel.modifyItemInDatabase(mLauncher, info, container, screenId,
2238 info.cellX, info.cellY, info.spanX, info.spanY);
2239 }
Adam Cohen2acce882012-03-28 19:03:19 -07002240 }
Adam Cohen482ed822012-03-02 14:15:13 -08002241 }
2242 }
2243
Sunny Goyalf7a29e82015-04-24 15:20:43 -07002244 private void setUseTempCoords(boolean useTempCoords) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07002245 int childCount = mShortcutsAndWidgets.getChildCount();
Adam Cohen482ed822012-03-02 14:15:13 -08002246 for (int i = 0; i < childCount; i++) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07002247 LayoutParams lp = (LayoutParams) mShortcutsAndWidgets.getChildAt(i).getLayoutParams();
Adam Cohen482ed822012-03-02 14:15:13 -08002248 lp.useTmpCoords = useTempCoords;
2249 }
2250 }
2251
Sunny Goyalf7a29e82015-04-24 15:20:43 -07002252 private ItemConfiguration findConfigurationNoShuffle(int pixelX, int pixelY, int minSpanX, int minSpanY,
Adam Cohen482ed822012-03-02 14:15:13 -08002253 int spanX, int spanY, View dragView, ItemConfiguration solution) {
2254 int[] result = new int[2];
2255 int[] resultSpan = new int[2];
Sunny Goyalf7a29e82015-04-24 15:20:43 -07002256 findNearestVacantArea(pixelX, pixelY, minSpanX, minSpanY, spanX, spanY, result,
Adam Cohen482ed822012-03-02 14:15:13 -08002257 resultSpan);
2258 if (result[0] >= 0 && result[1] >= 0) {
2259 copyCurrentStateToSolution(solution, false);
2260 solution.dragViewX = result[0];
2261 solution.dragViewY = result[1];
2262 solution.dragViewSpanX = resultSpan[0];
2263 solution.dragViewSpanY = resultSpan[1];
2264 solution.isSolution = true;
2265 } else {
2266 solution.isSolution = false;
2267 }
2268 return solution;
2269 }
2270
2271 public void prepareChildForDrag(View child) {
2272 markCellsAsUnoccupiedForView(child);
Adam Cohen482ed822012-03-02 14:15:13 -08002273 }
2274
Adam Cohen19f37922012-03-21 11:59:11 -07002275 /* This seems like it should be obvious and straight-forward, but when the direction vector
2276 needs to match with the notion of the dragView pushing other views, we have to employ
2277 a slightly more subtle notion of the direction vector. The question is what two points is
2278 the vector between? The center of the dragView and its desired destination? Not quite, as
2279 this doesn't necessarily coincide with the interaction of the dragView and items occupying
2280 those cells. Instead we use some heuristics to often lock the vector to up, down, left
2281 or right, which helps make pushing feel right.
2282 */
2283 private void getDirectionVectorForDrop(int dragViewCenterX, int dragViewCenterY, int spanX,
2284 int spanY, View dragView, int[] resultDirection) {
2285 int[] targetDestination = new int[2];
2286
2287 findNearestArea(dragViewCenterX, dragViewCenterY, spanX, spanY, targetDestination);
2288 Rect dragRect = new Rect();
2289 regionToRect(targetDestination[0], targetDestination[1], spanX, spanY, dragRect);
2290 dragRect.offset(dragViewCenterX - dragRect.centerX(), dragViewCenterY - dragRect.centerY());
2291
2292 Rect dropRegionRect = new Rect();
2293 getViewsIntersectingRegion(targetDestination[0], targetDestination[1], spanX, spanY,
2294 dragView, dropRegionRect, mIntersectingViews);
2295
2296 int dropRegionSpanX = dropRegionRect.width();
2297 int dropRegionSpanY = dropRegionRect.height();
2298
2299 regionToRect(dropRegionRect.left, dropRegionRect.top, dropRegionRect.width(),
2300 dropRegionRect.height(), dropRegionRect);
2301
2302 int deltaX = (dropRegionRect.centerX() - dragViewCenterX) / spanX;
2303 int deltaY = (dropRegionRect.centerY() - dragViewCenterY) / spanY;
2304
2305 if (dropRegionSpanX == mCountX || spanX == mCountX) {
2306 deltaX = 0;
2307 }
2308 if (dropRegionSpanY == mCountY || spanY == mCountY) {
2309 deltaY = 0;
2310 }
2311
2312 if (deltaX == 0 && deltaY == 0) {
2313 // No idea what to do, give a random direction.
2314 resultDirection[0] = 1;
2315 resultDirection[1] = 0;
2316 } else {
2317 computeDirectionVector(deltaX, deltaY, resultDirection);
2318 }
2319 }
2320
2321 // For a given cell and span, fetch the set of views intersecting the region.
2322 private void getViewsIntersectingRegion(int cellX, int cellY, int spanX, int spanY,
2323 View dragView, Rect boundingRect, ArrayList<View> intersectingViews) {
2324 if (boundingRect != null) {
2325 boundingRect.set(cellX, cellY, cellX + spanX, cellY + spanY);
2326 }
2327 intersectingViews.clear();
2328 Rect r0 = new Rect(cellX, cellY, cellX + spanX, cellY + spanY);
2329 Rect r1 = new Rect();
2330 final int count = mShortcutsAndWidgets.getChildCount();
2331 for (int i = 0; i < count; i++) {
2332 View child = mShortcutsAndWidgets.getChildAt(i);
2333 if (child == dragView) continue;
2334 LayoutParams lp = (LayoutParams) child.getLayoutParams();
2335 r1.set(lp.cellX, lp.cellY, lp.cellX + lp.cellHSpan, lp.cellY + lp.cellVSpan);
2336 if (Rect.intersects(r0, r1)) {
2337 mIntersectingViews.add(child);
2338 if (boundingRect != null) {
2339 boundingRect.union(r1);
2340 }
2341 }
2342 }
2343 }
2344
2345 boolean isNearestDropLocationOccupied(int pixelX, int pixelY, int spanX, int spanY,
2346 View dragView, int[] result) {
2347 result = findNearestArea(pixelX, pixelY, spanX, spanY, result);
2348 getViewsIntersectingRegion(result[0], result[1], spanX, spanY, dragView, null,
2349 mIntersectingViews);
2350 return !mIntersectingViews.isEmpty();
2351 }
2352
2353 void revertTempState() {
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002354 completeAndClearReorderPreviewAnimations();
2355 if (isItemPlacementDirty() && !DESTRUCTIVE_REORDER) {
2356 final int count = mShortcutsAndWidgets.getChildCount();
2357 for (int i = 0; i < count; i++) {
2358 View child = mShortcutsAndWidgets.getChildAt(i);
2359 LayoutParams lp = (LayoutParams) child.getLayoutParams();
2360 if (lp.tmpCellX != lp.cellX || lp.tmpCellY != lp.cellY) {
2361 lp.tmpCellX = lp.cellX;
2362 lp.tmpCellY = lp.cellY;
2363 animateChildToPosition(child, lp.cellX, lp.cellY, REORDER_ANIMATION_DURATION,
2364 0, false, false);
2365 }
Adam Cohen19f37922012-03-21 11:59:11 -07002366 }
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002367 setItemPlacementDirty(false);
Adam Cohen19f37922012-03-21 11:59:11 -07002368 }
Adam Cohen19f37922012-03-21 11:59:11 -07002369 }
2370
Adam Cohenbebf0422012-04-11 18:06:28 -07002371 boolean createAreaForResize(int cellX, int cellY, int spanX, int spanY,
2372 View dragView, int[] direction, boolean commit) {
2373 int[] pixelXY = new int[2];
2374 regionToCenterPoint(cellX, cellY, spanX, spanY, pixelXY);
2375
2376 // First we determine if things have moved enough to cause a different layout
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002377 ItemConfiguration swapSolution = findReorderSolution(pixelXY[0], pixelXY[1], spanX, spanY,
Adam Cohenbebf0422012-04-11 18:06:28 -07002378 spanX, spanY, direction, dragView, true, new ItemConfiguration());
2379
2380 setUseTempCoords(true);
2381 if (swapSolution != null && swapSolution.isSolution) {
2382 // If we're just testing for a possible location (MODE_ACCEPT_DROP), we don't bother
2383 // committing anything or animating anything as we just want to determine if a solution
2384 // exists
2385 copySolutionToTempState(swapSolution, dragView);
2386 setItemPlacementDirty(true);
2387 animateItemsToSolution(swapSolution, dragView, commit);
2388
2389 if (commit) {
2390 commitTempPlacement();
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002391 completeAndClearReorderPreviewAnimations();
Adam Cohenbebf0422012-04-11 18:06:28 -07002392 setItemPlacementDirty(false);
2393 } else {
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002394 beginOrAdjustReorderPreviewAnimations(swapSolution, dragView,
2395 REORDER_ANIMATION_DURATION, ReorderPreviewAnimation.MODE_PREVIEW);
Adam Cohenbebf0422012-04-11 18:06:28 -07002396 }
2397 mShortcutsAndWidgets.requestLayout();
2398 }
2399 return swapSolution.isSolution;
2400 }
2401
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002402 int[] performReorder(int pixelX, int pixelY, int minSpanX, int minSpanY, int spanX, int spanY,
Adam Cohen482ed822012-03-02 14:15:13 -08002403 View dragView, int[] result, int resultSpan[], int mode) {
Adam Cohen482ed822012-03-02 14:15:13 -08002404 // First we determine if things have moved enough to cause a different layout
Adam Cohen47a876d2012-03-19 13:21:41 -07002405 result = findNearestArea(pixelX, pixelY, spanX, spanY, result);
Adam Cohen482ed822012-03-02 14:15:13 -08002406
2407 if (resultSpan == null) {
2408 resultSpan = new int[2];
2409 }
2410
Adam Cohen19f37922012-03-21 11:59:11 -07002411 // When we are checking drop validity or actually dropping, we don't recompute the
2412 // direction vector, since we want the solution to match the preview, and it's possible
2413 // that the exact position of the item has changed to result in a new reordering outcome.
Adam Cohenb209e632012-03-27 17:09:36 -07002414 if ((mode == MODE_ON_DROP || mode == MODE_ON_DROP_EXTERNAL || mode == MODE_ACCEPT_DROP)
2415 && mPreviousReorderDirection[0] != INVALID_DIRECTION) {
Adam Cohen19f37922012-03-21 11:59:11 -07002416 mDirectionVector[0] = mPreviousReorderDirection[0];
2417 mDirectionVector[1] = mPreviousReorderDirection[1];
2418 // We reset this vector after drop
Adam Cohenb209e632012-03-27 17:09:36 -07002419 if (mode == MODE_ON_DROP || mode == MODE_ON_DROP_EXTERNAL) {
2420 mPreviousReorderDirection[0] = INVALID_DIRECTION;
2421 mPreviousReorderDirection[1] = INVALID_DIRECTION;
Adam Cohen19f37922012-03-21 11:59:11 -07002422 }
2423 } else {
2424 getDirectionVectorForDrop(pixelX, pixelY, spanX, spanY, dragView, mDirectionVector);
2425 mPreviousReorderDirection[0] = mDirectionVector[0];
2426 mPreviousReorderDirection[1] = mDirectionVector[1];
2427 }
2428
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002429 // Find a solution involving pushing / displacing any items in the way
2430 ItemConfiguration swapSolution = findReorderSolution(pixelX, pixelY, minSpanX, minSpanY,
Adam Cohen482ed822012-03-02 14:15:13 -08002431 spanX, spanY, mDirectionVector, dragView, true, new ItemConfiguration());
2432
2433 // We attempt the approach which doesn't shuffle views at all
2434 ItemConfiguration noShuffleSolution = findConfigurationNoShuffle(pixelX, pixelY, minSpanX,
2435 minSpanY, spanX, spanY, dragView, new ItemConfiguration());
2436
2437 ItemConfiguration finalSolution = null;
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002438
2439 // If the reorder solution requires resizing (shrinking) the item being dropped, we instead
2440 // favor a solution in which the item is not resized, but
Adam Cohen482ed822012-03-02 14:15:13 -08002441 if (swapSolution.isSolution && swapSolution.area() >= noShuffleSolution.area()) {
2442 finalSolution = swapSolution;
2443 } else if (noShuffleSolution.isSolution) {
2444 finalSolution = noShuffleSolution;
2445 }
2446
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002447 if (mode == MODE_SHOW_REORDER_HINT) {
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002448 if (finalSolution != null) {
Adam Cohenfe692872013-12-11 14:47:23 -08002449 beginOrAdjustReorderPreviewAnimations(finalSolution, dragView, 0,
2450 ReorderPreviewAnimation.MODE_HINT);
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002451 result[0] = finalSolution.dragViewX;
2452 result[1] = finalSolution.dragViewY;
2453 resultSpan[0] = finalSolution.dragViewSpanX;
2454 resultSpan[1] = finalSolution.dragViewSpanY;
2455 } else {
2456 result[0] = result[1] = resultSpan[0] = resultSpan[1] = -1;
2457 }
2458 return result;
2459 }
2460
Adam Cohen482ed822012-03-02 14:15:13 -08002461 boolean foundSolution = true;
2462 if (!DESTRUCTIVE_REORDER) {
2463 setUseTempCoords(true);
2464 }
2465
2466 if (finalSolution != null) {
2467 result[0] = finalSolution.dragViewX;
2468 result[1] = finalSolution.dragViewY;
2469 resultSpan[0] = finalSolution.dragViewSpanX;
2470 resultSpan[1] = finalSolution.dragViewSpanY;
2471
2472 // If we're just testing for a possible location (MODE_ACCEPT_DROP), we don't bother
2473 // committing anything or animating anything as we just want to determine if a solution
2474 // exists
2475 if (mode == MODE_DRAG_OVER || mode == MODE_ON_DROP || mode == MODE_ON_DROP_EXTERNAL) {
2476 if (!DESTRUCTIVE_REORDER) {
2477 copySolutionToTempState(finalSolution, dragView);
2478 }
2479 setItemPlacementDirty(true);
2480 animateItemsToSolution(finalSolution, dragView, mode == MODE_ON_DROP);
2481
Adam Cohen19f37922012-03-21 11:59:11 -07002482 if (!DESTRUCTIVE_REORDER &&
2483 (mode == MODE_ON_DROP || mode == MODE_ON_DROP_EXTERNAL)) {
Adam Cohen482ed822012-03-02 14:15:13 -08002484 commitTempPlacement();
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002485 completeAndClearReorderPreviewAnimations();
Adam Cohen19f37922012-03-21 11:59:11 -07002486 setItemPlacementDirty(false);
2487 } else {
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002488 beginOrAdjustReorderPreviewAnimations(finalSolution, dragView,
2489 REORDER_ANIMATION_DURATION, ReorderPreviewAnimation.MODE_PREVIEW);
Adam Cohen482ed822012-03-02 14:15:13 -08002490 }
2491 }
2492 } else {
2493 foundSolution = false;
2494 result[0] = result[1] = resultSpan[0] = resultSpan[1] = -1;
2495 }
2496
2497 if ((mode == MODE_ON_DROP || !foundSolution) && !DESTRUCTIVE_REORDER) {
2498 setUseTempCoords(false);
2499 }
Adam Cohen482ed822012-03-02 14:15:13 -08002500
Michael Jurkaa52570f2012-03-20 03:18:20 -07002501 mShortcutsAndWidgets.requestLayout();
Adam Cohen482ed822012-03-02 14:15:13 -08002502 return result;
2503 }
2504
Adam Cohen19f37922012-03-21 11:59:11 -07002505 void setItemPlacementDirty(boolean dirty) {
2506 mItemPlacementDirty = dirty;
Adam Cohen482ed822012-03-02 14:15:13 -08002507 }
Adam Cohen19f37922012-03-21 11:59:11 -07002508 boolean isItemPlacementDirty() {
2509 return mItemPlacementDirty;
Adam Cohen482ed822012-03-02 14:15:13 -08002510 }
2511
Adam Cohen091440a2015-03-18 14:16:05 -07002512 @Thunk class ItemConfiguration {
Adam Cohen8baab352012-03-20 17:39:21 -07002513 HashMap<View, CellAndSpan> map = new HashMap<View, CellAndSpan>();
Adam Cohenf3900c22012-11-16 18:28:11 -08002514 private HashMap<View, CellAndSpan> savedMap = new HashMap<View, CellAndSpan>();
2515 ArrayList<View> sortedViews = new ArrayList<View>();
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002516 ArrayList<View> intersectingViews;
Adam Cohen482ed822012-03-02 14:15:13 -08002517 boolean isSolution = false;
2518 int dragViewX, dragViewY, dragViewSpanX, dragViewSpanY;
2519
Adam Cohenf3900c22012-11-16 18:28:11 -08002520 void save() {
2521 // Copy current state into savedMap
2522 for (View v: map.keySet()) {
2523 map.get(v).copy(savedMap.get(v));
2524 }
2525 }
2526
2527 void restore() {
2528 // Restore current state from savedMap
2529 for (View v: savedMap.keySet()) {
2530 savedMap.get(v).copy(map.get(v));
2531 }
2532 }
2533
2534 void add(View v, CellAndSpan cs) {
2535 map.put(v, cs);
2536 savedMap.put(v, new CellAndSpan());
2537 sortedViews.add(v);
2538 }
2539
Adam Cohen482ed822012-03-02 14:15:13 -08002540 int area() {
2541 return dragViewSpanX * dragViewSpanY;
2542 }
Adam Cohen8baab352012-03-20 17:39:21 -07002543 }
2544
2545 private class CellAndSpan {
2546 int x, y;
2547 int spanX, spanY;
2548
Adam Cohenf3900c22012-11-16 18:28:11 -08002549 public CellAndSpan() {
2550 }
2551
2552 public void copy(CellAndSpan copy) {
2553 copy.x = x;
2554 copy.y = y;
2555 copy.spanX = spanX;
2556 copy.spanY = spanY;
2557 }
2558
Adam Cohen8baab352012-03-20 17:39:21 -07002559 public CellAndSpan(int x, int y, int spanX, int spanY) {
2560 this.x = x;
2561 this.y = y;
2562 this.spanX = spanX;
2563 this.spanY = spanY;
Adam Cohen482ed822012-03-02 14:15:13 -08002564 }
Adam Cohenf3900c22012-11-16 18:28:11 -08002565
2566 public String toString() {
2567 return "(" + x + ", " + y + ": " + spanX + ", " + spanY + ")";
2568 }
2569
Adam Cohen482ed822012-03-02 14:15:13 -08002570 }
2571
Adam Cohendf035382011-04-11 17:22:04 -07002572 /**
Adam Cohendf035382011-04-11 17:22:04 -07002573 * Find a starting cell position that will fit the given bounds nearest the requested
2574 * cell location. Uses Euclidean distance to score multiple vacant areas.
2575 *
2576 * @param pixelX The X location at which you want to search for a vacant area.
2577 * @param pixelY The Y location at which you want to search for a vacant area.
2578 * @param spanX Horizontal span of the object.
2579 * @param spanY Vertical span of the object.
2580 * @param ignoreView Considers space occupied by this view as unoccupied
2581 * @param result Previously returned value to possibly recycle.
2582 * @return The X, Y cell of a vacant area that can contain this object,
2583 * nearest the requested location.
2584 */
Sunny Goyalf7a29e82015-04-24 15:20:43 -07002585 int[] findNearestArea(int pixelX, int pixelY, int spanX, int spanY, int[] result) {
2586 return findNearestArea(pixelX, pixelY, spanX, spanY, spanX, spanY, false, result, null);
Adam Cohendf035382011-04-11 17:22:04 -07002587 }
2588
Michael Jurka0280c3b2010-09-17 15:00:07 -07002589 boolean existsEmptyCell() {
2590 return findCellForSpan(null, 1, 1);
2591 }
2592
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002593 /**
Michael Jurka0280c3b2010-09-17 15:00:07 -07002594 * Finds the upper-left coordinate of the first rectangle in the grid that can
2595 * hold a cell of the specified dimensions. If intersectX and intersectY are not -1,
2596 * then this method will only return coordinates for rectangles that contain the cell
2597 * (intersectX, intersectY)
2598 *
2599 * @param cellXY The array that will contain the position of a vacant cell if such a cell
2600 * can be found.
2601 * @param spanX The horizontal span of the cell we want to find.
2602 * @param spanY The vertical span of the cell we want to find.
2603 *
2604 * @return True if a vacant cell of the specified dimension was found, false otherwise.
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07002605 */
Hyunyoung Song3f471442015-04-08 19:01:34 -07002606 public boolean findCellForSpan(int[] cellXY, int spanX, int spanY) {
Michael Jurka28750fb2010-09-24 17:43:49 -07002607 boolean foundCell = false;
Sunny Goyalf7a29e82015-04-24 15:20:43 -07002608 final int endX = mCountX - (spanX - 1);
2609 final int endY = mCountY - (spanY - 1);
Michael Jurka0280c3b2010-09-17 15:00:07 -07002610
Sunny Goyalf7a29e82015-04-24 15:20:43 -07002611 for (int y = 0; y < endY && !foundCell; y++) {
2612 inner:
2613 for (int x = 0; x < endX; x++) {
2614 for (int i = 0; i < spanX; i++) {
2615 for (int j = 0; j < spanY; j++) {
2616 if (mOccupied[x + i][y + j]) {
2617 // small optimization: we can skip to after the column we just found
2618 // an occupied cell
2619 x += i;
2620 continue inner;
Michael Jurka0280c3b2010-09-17 15:00:07 -07002621 }
2622 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07002623 }
Sunny Goyalf7a29e82015-04-24 15:20:43 -07002624 if (cellXY != null) {
2625 cellXY[0] = x;
2626 cellXY[1] = y;
2627 }
2628 foundCell = true;
Michael Jurka0280c3b2010-09-17 15:00:07 -07002629 break;
Michael Jurka0280c3b2010-09-17 15:00:07 -07002630 }
2631 }
2632
Michael Jurka28750fb2010-09-24 17:43:49 -07002633 return foundCell;
Michael Jurka0280c3b2010-09-17 15:00:07 -07002634 }
2635
2636 /**
Winson Chungc07918d2011-07-01 15:35:26 -07002637 * A drag event has begun over this layout.
2638 * It may have begun over this layout (in which case onDragChild is called first),
2639 * or it may have begun on another layout.
2640 */
2641 void onDragEnter() {
Winson Chungc07918d2011-07-01 15:35:26 -07002642 mDragging = true;
2643 }
2644
2645 /**
Michael Jurka0280c3b2010-09-17 15:00:07 -07002646 * Called when drag has left this CellLayout or has been completed (successfully or not)
2647 */
2648 void onDragExit() {
Joe Onorato4be866d2010-10-10 11:26:02 -07002649 // This can actually be called when we aren't in a drag, e.g. when adding a new
2650 // item to this layout via the customize drawer.
2651 // Guard against that case.
2652 if (mDragging) {
2653 mDragging = false;
Patrick Dubroyde7658b2010-09-27 11:15:43 -07002654 }
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -07002655
2656 // Invalidate the drag data
Adam Cohend41fbf52012-02-16 23:53:59 -08002657 mDragCell[0] = mDragCell[1] = -1;
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -07002658 mDragOutlineAnims[mDragOutlineCurrent].animateOut();
2659 mDragOutlineCurrent = (mDragOutlineCurrent + 1) % mDragOutlineAnims.length;
Adam Cohen19f37922012-03-21 11:59:11 -07002660 revertTempState();
Michael Jurka33945b22010-12-21 18:19:38 -08002661 setIsDragOverlapping(false);
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07002662 }
2663
2664 /**
Winson Chungaafa03c2010-06-11 17:34:16 -07002665 * Mark a child as having been dropped.
Patrick Dubroyde7658b2010-09-27 11:15:43 -07002666 * At the beginning of the drag operation, the child may have been on another
Patrick Dubroyce34a972010-10-19 10:34:32 -07002667 * screen, but it is re-parented before this method is called.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002668 *
2669 * @param child The child that is being dropped
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002670 */
Adam Cohen716b51e2011-06-30 12:09:54 -07002671 void onDropChild(View child) {
Romain Guyd94533d2009-08-17 10:01:15 -07002672 if (child != null) {
2673 LayoutParams lp = (LayoutParams) child.getLayoutParams();
Romain Guy84f296c2009-11-04 15:00:44 -08002674 lp.dropped = true;
Romain Guyd94533d2009-08-17 10:01:15 -07002675 child.requestLayout();
Romain Guyd94533d2009-08-17 10:01:15 -07002676 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002677 }
2678
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002679 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002680 * Computes a bounding rectangle for a range of cells
Winson Chungaafa03c2010-06-11 17:34:16 -07002681 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002682 * @param cellX X coordinate of upper left corner expressed as a cell position
2683 * @param cellY Y coordinate of upper left corner expressed as a cell position
Winson Chungaafa03c2010-06-11 17:34:16 -07002684 * @param cellHSpan Width in cells
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002685 * @param cellVSpan Height in cells
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07002686 * @param resultRect Rect into which to put the results
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002687 */
Adam Cohend41fbf52012-02-16 23:53:59 -08002688 public void cellToRect(int cellX, int cellY, int cellHSpan, int cellVSpan, Rect resultRect) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002689 final int cellWidth = mCellWidth;
2690 final int cellHeight = mCellHeight;
2691 final int widthGap = mWidthGap;
2692 final int heightGap = mHeightGap;
Winson Chungaafa03c2010-06-11 17:34:16 -07002693
Winson Chung4b825dcd2011-06-19 12:41:22 -07002694 final int hStartPadding = getPaddingLeft();
2695 final int vStartPadding = getPaddingTop();
Winson Chungaafa03c2010-06-11 17:34:16 -07002696
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002697 int width = cellHSpan * cellWidth + ((cellHSpan - 1) * widthGap);
2698 int height = cellVSpan * cellHeight + ((cellVSpan - 1) * heightGap);
2699
2700 int x = hStartPadding + cellX * (cellWidth + widthGap);
2701 int y = vStartPadding + cellY * (cellHeight + heightGap);
Winson Chungaafa03c2010-06-11 17:34:16 -07002702
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07002703 resultRect.set(x, y, x + width, y + height);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002704 }
Winson Chungaafa03c2010-06-11 17:34:16 -07002705
Michael Jurka0280c3b2010-09-17 15:00:07 -07002706 private void clearOccupiedCells() {
2707 for (int x = 0; x < mCountX; x++) {
2708 for (int y = 0; y < mCountY; y++) {
2709 mOccupied[x][y] = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002710 }
2711 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07002712 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002713
Adam Cohend4844c32011-02-18 19:25:06 -08002714 public void markCellsAsOccupiedForView(View view) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07002715 if (view == null || view.getParent() != mShortcutsAndWidgets) return;
Michael Jurka0280c3b2010-09-17 15:00:07 -07002716 LayoutParams lp = (LayoutParams) view.getLayoutParams();
Sunny Goyalf7a29e82015-04-24 15:20:43 -07002717 markCellsForView(lp.cellX, lp.cellY, lp.cellHSpan, lp.cellVSpan, mOccupied, true);
Michael Jurka0280c3b2010-09-17 15:00:07 -07002718 }
2719
Adam Cohend4844c32011-02-18 19:25:06 -08002720 public void markCellsAsUnoccupiedForView(View view) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07002721 if (view == null || view.getParent() != mShortcutsAndWidgets) return;
Michael Jurka0280c3b2010-09-17 15:00:07 -07002722 LayoutParams lp = (LayoutParams) view.getLayoutParams();
Sunny Goyalf7a29e82015-04-24 15:20:43 -07002723 markCellsForView(lp.cellX, lp.cellY, lp.cellHSpan, lp.cellVSpan, mOccupied, false);
Michael Jurka0280c3b2010-09-17 15:00:07 -07002724 }
2725
Adam Cohen482ed822012-03-02 14:15:13 -08002726 private void markCellsForView(int cellX, int cellY, int spanX, int spanY, boolean[][] occupied,
2727 boolean value) {
2728 if (cellX < 0 || cellY < 0) return;
Michael Jurka0280c3b2010-09-17 15:00:07 -07002729 for (int x = cellX; x < cellX + spanX && x < mCountX; x++) {
2730 for (int y = cellY; y < cellY + spanY && y < mCountY; y++) {
Adam Cohen482ed822012-03-02 14:15:13 -08002731 occupied[x][y] = value;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002732 }
2733 }
2734 }
2735
Adam Cohen2801caf2011-05-13 20:57:39 -07002736 public int getDesiredWidth() {
Michael Jurka8b805b12012-04-18 14:23:14 -07002737 return getPaddingLeft() + getPaddingRight() + (mCountX * mCellWidth) +
Adam Cohen2801caf2011-05-13 20:57:39 -07002738 (Math.max((mCountX - 1), 0) * mWidthGap);
2739 }
2740
2741 public int getDesiredHeight() {
Michael Jurka8b805b12012-04-18 14:23:14 -07002742 return getPaddingTop() + getPaddingBottom() + (mCountY * mCellHeight) +
Adam Cohen2801caf2011-05-13 20:57:39 -07002743 (Math.max((mCountY - 1), 0) * mHeightGap);
2744 }
2745
Michael Jurka66d72172011-04-12 16:29:25 -07002746 public boolean isOccupied(int x, int y) {
2747 if (x < mCountX && y < mCountY) {
2748 return mOccupied[x][y];
2749 } else {
2750 throw new RuntimeException("Position exceeds the bound of this CellLayout");
2751 }
2752 }
2753
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002754 @Override
2755 public ViewGroup.LayoutParams generateLayoutParams(AttributeSet attrs) {
2756 return new CellLayout.LayoutParams(getContext(), attrs);
2757 }
2758
2759 @Override
2760 protected boolean checkLayoutParams(ViewGroup.LayoutParams p) {
2761 return p instanceof CellLayout.LayoutParams;
2762 }
2763
2764 @Override
2765 protected ViewGroup.LayoutParams generateLayoutParams(ViewGroup.LayoutParams p) {
2766 return new CellLayout.LayoutParams(p);
2767 }
2768
2769 public static class LayoutParams extends ViewGroup.MarginLayoutParams {
2770 /**
2771 * Horizontal location of the item in the grid.
2772 */
2773 @ViewDebug.ExportedProperty
2774 public int cellX;
2775
2776 /**
2777 * Vertical location of the item in the grid.
2778 */
2779 @ViewDebug.ExportedProperty
2780 public int cellY;
2781
2782 /**
Adam Cohen482ed822012-03-02 14:15:13 -08002783 * Temporary horizontal location of the item in the grid during reorder
2784 */
2785 public int tmpCellX;
2786
2787 /**
2788 * Temporary vertical location of the item in the grid during reorder
2789 */
2790 public int tmpCellY;
2791
2792 /**
2793 * Indicates that the temporary coordinates should be used to layout the items
2794 */
2795 public boolean useTmpCoords;
2796
2797 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002798 * Number of cells spanned horizontally by the item.
2799 */
2800 @ViewDebug.ExportedProperty
2801 public int cellHSpan;
2802
2803 /**
2804 * Number of cells spanned vertically by the item.
2805 */
2806 @ViewDebug.ExportedProperty
2807 public int cellVSpan;
Winson Chungaafa03c2010-06-11 17:34:16 -07002808
Adam Cohen1b607ed2011-03-03 17:26:50 -08002809 /**
2810 * Indicates whether the item will set its x, y, width and height parameters freely,
2811 * or whether these will be computed based on cellX, cellY, cellHSpan and cellVSpan.
2812 */
Adam Cohend4844c32011-02-18 19:25:06 -08002813 public boolean isLockedToGrid = true;
2814
Adam Cohen482ed822012-03-02 14:15:13 -08002815 /**
Adam Cohenec40b2b2013-07-23 15:52:40 -07002816 * Indicates that this item should use the full extents of its parent.
2817 */
2818 public boolean isFullscreen = false;
2819
2820 /**
Adam Cohen482ed822012-03-02 14:15:13 -08002821 * Indicates whether this item can be reordered. Always true except in the case of the
2822 * the AllApps button.
2823 */
2824 public boolean canReorder = true;
2825
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002826 // X coordinate of the view in the layout.
2827 @ViewDebug.ExportedProperty
Vadim Tryshevfedca432015-08-19 17:55:02 -07002828 public int x;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002829 // Y coordinate of the view in the layout.
2830 @ViewDebug.ExportedProperty
Vadim Tryshevfedca432015-08-19 17:55:02 -07002831 public int y;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002832
Romain Guy84f296c2009-11-04 15:00:44 -08002833 boolean dropped;
Romain Guyfcb9e712009-10-02 16:06:52 -07002834
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002835 public LayoutParams(Context c, AttributeSet attrs) {
2836 super(c, attrs);
2837 cellHSpan = 1;
2838 cellVSpan = 1;
2839 }
2840
2841 public LayoutParams(ViewGroup.LayoutParams source) {
2842 super(source);
2843 cellHSpan = 1;
2844 cellVSpan = 1;
2845 }
Winson Chungaafa03c2010-06-11 17:34:16 -07002846
2847 public LayoutParams(LayoutParams source) {
2848 super(source);
2849 this.cellX = source.cellX;
2850 this.cellY = source.cellY;
2851 this.cellHSpan = source.cellHSpan;
2852 this.cellVSpan = source.cellVSpan;
2853 }
2854
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002855 public LayoutParams(int cellX, int cellY, int cellHSpan, int cellVSpan) {
Romain Guy8f19cdd2010-01-08 15:07:00 -08002856 super(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002857 this.cellX = cellX;
2858 this.cellY = cellY;
2859 this.cellHSpan = cellHSpan;
2860 this.cellVSpan = cellVSpan;
2861 }
2862
Adam Cohen2374abf2013-04-16 14:56:57 -07002863 public void setup(int cellWidth, int cellHeight, int widthGap, int heightGap,
2864 boolean invertHorizontally, int colCount) {
Adam Cohend4844c32011-02-18 19:25:06 -08002865 if (isLockedToGrid) {
2866 final int myCellHSpan = cellHSpan;
2867 final int myCellVSpan = cellVSpan;
Adam Cohen2374abf2013-04-16 14:56:57 -07002868 int myCellX = useTmpCoords ? tmpCellX : cellX;
2869 int myCellY = useTmpCoords ? tmpCellY : cellY;
2870
2871 if (invertHorizontally) {
2872 myCellX = colCount - myCellX - cellHSpan;
2873 }
Adam Cohen1b607ed2011-03-03 17:26:50 -08002874
Adam Cohend4844c32011-02-18 19:25:06 -08002875 width = myCellHSpan * cellWidth + ((myCellHSpan - 1) * widthGap) -
2876 leftMargin - rightMargin;
2877 height = myCellVSpan * cellHeight + ((myCellVSpan - 1) * heightGap) -
2878 topMargin - bottomMargin;
Winson Chungeecf02d2012-03-02 17:14:58 -08002879 x = (int) (myCellX * (cellWidth + widthGap) + leftMargin);
2880 y = (int) (myCellY * (cellHeight + heightGap) + topMargin);
Adam Cohend4844c32011-02-18 19:25:06 -08002881 }
2882 }
Winson Chungaafa03c2010-06-11 17:34:16 -07002883
Winson Chungaafa03c2010-06-11 17:34:16 -07002884 public String toString() {
2885 return "(" + this.cellX + ", " + this.cellY + ")";
2886 }
Adam Cohen7f4eabe2011-04-21 16:19:16 -07002887
2888 public void setWidth(int width) {
2889 this.width = width;
2890 }
2891
2892 public int getWidth() {
2893 return width;
2894 }
2895
2896 public void setHeight(int height) {
2897 this.height = height;
2898 }
2899
2900 public int getHeight() {
2901 return height;
2902 }
2903
2904 public void setX(int x) {
2905 this.x = x;
2906 }
2907
2908 public int getX() {
2909 return x;
2910 }
2911
2912 public void setY(int y) {
2913 this.y = y;
2914 }
2915
2916 public int getY() {
2917 return y;
2918 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002919 }
2920
Michael Jurka0280c3b2010-09-17 15:00:07 -07002921 // This class stores info for two purposes:
2922 // 1. When dragging items (mDragInfo in Workspace), we store the View, its cellX & cellY,
2923 // its spanX, spanY, and the screen it is on
2924 // 2. When long clicking on an empty cell in a CellLayout, we save information about the
2925 // cellX and cellY coordinates and which page was clicked. We then set this as a tag on
2926 // the CellLayout that was long clicked
Sunny Goyal83a8f042015-05-19 12:52:12 -07002927 public static final class CellInfo {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002928 View cell;
Michael Jurkaa63c4522010-08-19 13:52:27 -07002929 int cellX = -1;
2930 int cellY = -1;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002931 int spanX;
2932 int spanY;
Adam Cohendcd297f2013-06-18 13:13:40 -07002933 long screenId;
Winson Chung3d503fb2011-07-13 17:25:49 -07002934 long container;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002935
Sunny Goyal83a8f042015-05-19 12:52:12 -07002936 public CellInfo(View v, ItemInfo info) {
Adam Cohene0aaa0d2014-05-12 12:44:22 -07002937 cell = v;
2938 cellX = info.cellX;
2939 cellY = info.cellY;
2940 spanX = info.spanX;
2941 spanY = info.spanY;
2942 screenId = info.screenId;
2943 container = info.container;
2944 }
2945
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002946 @Override
2947 public String toString() {
Winson Chungaafa03c2010-06-11 17:34:16 -07002948 return "Cell[view=" + (cell == null ? "null" : cell.getClass())
2949 + ", x=" + cellX + ", y=" + cellY + "]";
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002950 }
2951 }
Michael Jurkad771c962011-08-09 15:00:48 -07002952
Sunny Goyala9116722015-04-29 13:55:58 -07002953 public boolean findVacantCell(int spanX, int spanY, int[] outXY) {
2954 return Utilities.findVacantCell(outXY, spanX, spanY, mCountX, mCountY, mOccupied);
2955 }
Sunny Goyal9ca9c132015-04-29 14:57:22 -07002956
2957 public boolean isRegionVacant(int x, int y, int spanX, int spanY) {
2958 int x2 = x + spanX - 1;
2959 int y2 = y + spanY - 1;
2960 if (x < 0 || y < 0 || x2 >= mCountX || y2 >= mCountY) {
2961 return false;
2962 }
2963 for (int i = x; i <= x2; i++) {
2964 for (int j = y; j <= y2; j++) {
2965 if (mOccupied[i][j]) {
2966 return false;
2967 }
2968 }
2969 }
2970
2971 return true;
2972 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002973}