blob: e6865b2e6da1fa41b82838077912117356960e15 [file] [log] [blame]
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001/*
2 * Copyright (C) 2008 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Daniel Sandler325dc232013-06-05 22:57:57 -040017package com.android.launcher3;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080018
Joe Onorato4be866d2010-10-10 11:26:02 -070019import android.animation.Animator;
Michael Jurka629758f2012-06-14 16:18:21 -070020import android.animation.AnimatorListenerAdapter;
Brandon Keely50e6e562012-05-08 16:28:49 -070021import android.animation.AnimatorSet;
Chet Haase00397b12010-10-07 11:13:10 -070022import android.animation.TimeInterpolator;
Patrick Dubroyde7658b2010-09-27 11:15:43 -070023import android.animation.ValueAnimator;
24import android.animation.ValueAnimator.AnimatorUpdateListener;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080025import android.content.Context;
Joe Onorato79e56262009-09-21 15:23:04 -040026import android.content.res.Resources;
Winson Chungaafa03c2010-06-11 17:34:16 -070027import android.content.res.TypedArray;
Joe Onorato4be866d2010-10-10 11:26:02 -070028import android.graphics.Bitmap;
Winson Chungaafa03c2010-06-11 17:34:16 -070029import android.graphics.Canvas;
Andrew Flynn0dca1ec2012-02-29 13:33:22 -080030import android.graphics.Color;
Joe Onorato4be866d2010-10-10 11:26:02 -070031import android.graphics.Paint;
Patrick Dubroyde7658b2010-09-27 11:15:43 -070032import android.graphics.Point;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080033import android.graphics.Rect;
Adam Cohen482ed822012-03-02 14:15:13 -080034import android.graphics.drawable.ColorDrawable;
Patrick Dubroy6569f2c2010-07-12 14:25:18 -070035import android.graphics.drawable.Drawable;
Adam Cohen1462de32012-07-24 22:34:36 -070036import android.os.Parcelable;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080037import android.util.AttributeSet;
Joe Onorato4be866d2010-10-10 11:26:02 -070038import android.util.Log;
Adam Cohen1462de32012-07-24 22:34:36 -070039import android.util.SparseArray;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080040import android.view.MotionEvent;
41import android.view.View;
42import android.view.ViewDebug;
43import android.view.ViewGroup;
Winson Chungaafa03c2010-06-11 17:34:16 -070044import android.view.animation.Animation;
Winson Chung150fbab2010-09-29 17:14:26 -070045import android.view.animation.DecelerateInterpolator;
Winson Chungaafa03c2010-06-11 17:34:16 -070046import android.view.animation.LayoutAnimationController;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080047
Daniel Sandler325dc232013-06-05 22:57:57 -040048import com.android.launcher3.FolderIcon.FolderRingAnimator;
Patrick Dubroy8e58e912010-10-14 13:21:48 -070049
Adam Cohen69ce2e52011-07-03 19:25:21 -070050import java.util.ArrayList;
Adam Cohenc0dcf592011-06-01 15:30:43 -070051import java.util.Arrays;
Adam Cohenf3900c22012-11-16 18:28:11 -080052import java.util.Collections;
53import java.util.Comparator;
Adam Cohenbfbfd262011-06-13 16:55:12 -070054import java.util.HashMap;
Adam Cohend41fbf52012-02-16 23:53:59 -080055import java.util.Stack;
Adam Cohenc0dcf592011-06-01 15:30:43 -070056
Michael Jurkabdb5c532011-02-01 15:05:06 -080057public class CellLayout extends ViewGroup {
Winson Chungaafa03c2010-06-11 17:34:16 -070058 static final String TAG = "CellLayout";
59
Adam Cohen2acce882012-03-28 19:03:19 -070060 private Launcher mLauncher;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080061 private int mCellWidth;
62 private int mCellHeight;
Winson Chung11a1a532013-09-13 11:14:45 -070063 private int mFixedCellWidth;
64 private int mFixedCellHeight;
Winson Chungaafa03c2010-06-11 17:34:16 -070065
Adam Cohend22015c2010-07-26 22:02:18 -070066 private int mCountX;
67 private int mCountY;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080068
Adam Cohen234c4cd2011-07-17 21:03:04 -070069 private int mOriginalWidthGap;
70 private int mOriginalHeightGap;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080071 private int mWidthGap;
72 private int mHeightGap;
Winson Chung4b825dcd2011-06-19 12:41:22 -070073 private int mMaxGap;
Adam Cohen917e3882013-10-31 15:03:35 -070074 private boolean mDropPending = false;
Adam Cohenc50438c2014-08-19 17:43:05 -070075 private boolean mIsDragTarget = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080076
Patrick Dubroyde7658b2010-09-27 11:15:43 -070077 // These are temporary variables to prevent having to allocate a new object just to
78 // return an (x, y) value from helper functions. Do NOT use them to maintain other state.
Winson Chung0be025d2011-05-23 17:45:09 -070079 private final int[] mTmpXY = new int[2];
Patrick Dubroyde7658b2010-09-27 11:15:43 -070080 private final int[] mTmpPoint = new int[2];
Adam Cohen69ce2e52011-07-03 19:25:21 -070081 int[] mTempLocation = new int[2];
Patrick Dubroy6569f2c2010-07-12 14:25:18 -070082
The Android Open Source Project31dd5032009-03-03 19:32:27 -080083 boolean[][] mOccupied;
Adam Cohen482ed822012-03-02 14:15:13 -080084 boolean[][] mTmpOccupied;
Michael Jurkad771c962011-08-09 15:00:48 -070085 private boolean mLastDownOnOccupiedCell = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080086
Michael Jurkadee05892010-07-27 10:01:56 -070087 private OnTouchListener mInterceptTouchListener;
88
Adam Cohen69ce2e52011-07-03 19:25:21 -070089 private ArrayList<FolderRingAnimator> mFolderOuterRings = new ArrayList<FolderRingAnimator>();
Adam Cohenc51934b2011-07-26 21:07:43 -070090 private int[] mFolderLeaveBehindCell = {-1, -1};
Adam Cohen69ce2e52011-07-03 19:25:21 -070091
Adam Cohen02dcfcc2013-10-01 12:37:33 -070092 private float FOREGROUND_ALPHA_DAMPER = 0.65f;
Adam Cohenb5ba0972011-09-07 18:02:31 -070093 private int mForegroundAlpha = 0;
Michael Jurka5f1c5092010-09-03 14:15:02 -070094 private float mBackgroundAlpha;
Adam Cohen1b0aaac2010-10-28 11:11:18 -070095 private float mBackgroundAlphaMultiplier = 1.0f;
Winson Chung59a488a2013-12-10 12:32:14 -080096 private boolean mDrawBackground = true;
Adam Cohenf34bab52010-09-30 14:11:56 -070097
Michael Jurka33945b22010-12-21 18:19:38 -080098 private Drawable mNormalBackground;
Michael Jurka33945b22010-12-21 18:19:38 -080099 private Drawable mActiveGlowBackground;
Adam Cohenb5ba0972011-09-07 18:02:31 -0700100 private Drawable mOverScrollForegroundDrawable;
101 private Drawable mOverScrollLeft;
102 private Drawable mOverScrollRight;
Michael Jurka18014792010-10-14 09:01:34 -0700103 private Rect mBackgroundRect;
Adam Cohenb5ba0972011-09-07 18:02:31 -0700104 private Rect mForegroundRect;
Adam Cohenb5ba0972011-09-07 18:02:31 -0700105 private int mForegroundPadding;
Patrick Dubroy1262e362010-10-06 15:49:50 -0700106
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700107 // These values allow a fixed measurement to be set on the CellLayout.
108 private int mFixedWidth = -1;
109 private int mFixedHeight = -1;
110
Michael Jurka33945b22010-12-21 18:19:38 -0800111 // If we're actively dragging something over this screen, mIsDragOverlapping is true
112 private boolean mIsDragOverlapping = false;
Adam Cohendedbd962013-07-11 14:21:49 -0700113 boolean mUseActiveGlowBackground = false;
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700114
Winson Chung150fbab2010-09-29 17:14:26 -0700115 // These arrays are used to implement the drag visualization on x-large screens.
Joe Onorato4be866d2010-10-10 11:26:02 -0700116 // They are used as circular arrays, indexed by mDragOutlineCurrent.
Adam Cohend41fbf52012-02-16 23:53:59 -0800117 private Rect[] mDragOutlines = new Rect[4];
Chet Haase472b2812010-10-14 07:02:04 -0700118 private float[] mDragOutlineAlphas = new float[mDragOutlines.length];
Joe Onorato4be866d2010-10-10 11:26:02 -0700119 private InterruptibleInOutAnimator[] mDragOutlineAnims =
120 new InterruptibleInOutAnimator[mDragOutlines.length];
Winson Chung150fbab2010-09-29 17:14:26 -0700121
122 // Used as an index into the above 3 arrays; indicates which is the most current value.
Joe Onorato4be866d2010-10-10 11:26:02 -0700123 private int mDragOutlineCurrent = 0;
Patrick Dubroy8e58e912010-10-14 13:21:48 -0700124 private final Paint mDragOutlinePaint = new Paint();
Winson Chung150fbab2010-09-29 17:14:26 -0700125
Sunny Goyal508da152014-08-14 10:53:27 -0700126 private final FastBitmapView mTouchFeedbackView;
Patrick Dubroy96864c32011-03-10 17:17:23 -0800127
Adam Cohen482ed822012-03-02 14:15:13 -0800128 private HashMap<CellLayout.LayoutParams, Animator> mReorderAnimators = new
129 HashMap<CellLayout.LayoutParams, Animator>();
Adam Cohenfa3c58f2013-12-06 16:10:55 -0800130 private HashMap<View, ReorderPreviewAnimation>
131 mShakeAnimators = new HashMap<View, ReorderPreviewAnimation>();
Adam Cohen19f37922012-03-21 11:59:11 -0700132
133 private boolean mItemPlacementDirty = false;
Adam Cohenbfbfd262011-06-13 16:55:12 -0700134
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700135 // When a drag operation is in progress, holds the nearest cell to the touch point
136 private final int[] mDragCell = new int[2];
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800137
Joe Onorato4be866d2010-10-10 11:26:02 -0700138 private boolean mDragging = false;
139
Patrick Dubroyce34a972010-10-19 10:34:32 -0700140 private TimeInterpolator mEaseOutInterpolator;
Michael Jurkaa52570f2012-03-20 03:18:20 -0700141 private ShortcutAndWidgetContainer mShortcutsAndWidgets;
Patrick Dubroyce34a972010-10-19 10:34:32 -0700142
Andrew Flynn0dca1ec2012-02-29 13:33:22 -0800143 private boolean mIsHotseat = false;
Adam Cohen307fe232012-08-16 17:55:58 -0700144 private float mHotseatScale = 1f;
Andrew Flynn0dca1ec2012-02-29 13:33:22 -0800145
Adam Cohenfa3c58f2013-12-06 16:10:55 -0800146 public static final int MODE_SHOW_REORDER_HINT = 0;
147 public static final int MODE_DRAG_OVER = 1;
148 public static final int MODE_ON_DROP = 2;
149 public static final int MODE_ON_DROP_EXTERNAL = 3;
150 public static final int MODE_ACCEPT_DROP = 4;
Adam Cohen19f37922012-03-21 11:59:11 -0700151 private static final boolean DESTRUCTIVE_REORDER = false;
Adam Cohen482ed822012-03-02 14:15:13 -0800152 private static final boolean DEBUG_VISUALIZE_OCCUPIED = false;
153
Adam Cohena897f392012-04-27 18:12:05 -0700154 static final int LANDSCAPE = 0;
155 static final int PORTRAIT = 1;
156
Adam Cohenfa3c58f2013-12-06 16:10:55 -0800157 private static final float REORDER_PREVIEW_MAGNITUDE = 0.12f;
Adam Cohen19f37922012-03-21 11:59:11 -0700158 private static final int REORDER_ANIMATION_DURATION = 150;
Adam Cohenfa3c58f2013-12-06 16:10:55 -0800159 private float mReorderPreviewAnimationMagnitude;
Adam Cohen19f37922012-03-21 11:59:11 -0700160
Adam Cohen482ed822012-03-02 14:15:13 -0800161 private ArrayList<View> mIntersectingViews = new ArrayList<View>();
162 private Rect mOccupiedRect = new Rect();
163 private int[] mDirectionVector = new int[2];
Adam Cohen19f37922012-03-21 11:59:11 -0700164 int[] mPreviousReorderDirection = new int[2];
Adam Cohenb209e632012-03-27 17:09:36 -0700165 private static final int INVALID_DIRECTION = -100;
Adam Cohenc6cc61d2012-04-04 12:47:08 -0700166 private DropTarget.DragEnforcer mDragEnforcer;
Adam Cohen482ed822012-03-02 14:15:13 -0800167
Winson Chung3a6e7f32013-10-09 15:50:52 -0700168 private Rect mTempRect = new Rect();
169
Michael Jurkaca993832012-06-29 15:17:04 -0700170 private final static Paint sPaint = new Paint();
Romain Guy8a0bff52012-05-06 13:14:33 -0700171
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800172 public CellLayout(Context context) {
173 this(context, null);
174 }
175
176 public CellLayout(Context context, AttributeSet attrs) {
177 this(context, attrs, 0);
178 }
179
180 public CellLayout(Context context, AttributeSet attrs, int defStyle) {
181 super(context, attrs, defStyle);
Michael Jurka8b805b12012-04-18 14:23:14 -0700182 mDragEnforcer = new DropTarget.DragEnforcer(context);
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700183
184 // A ViewGroup usually does not draw, but CellLayout needs to draw a rectangle to show
185 // the user where a dragged item will land when dropped.
186 setWillNotDraw(false);
Romain Guyce3cbd12013-02-25 15:00:36 -0800187 setClipToPadding(false);
Adam Cohen2acce882012-03-28 19:03:19 -0700188 mLauncher = (Launcher) context;
Michael Jurkaa63c4522010-08-19 13:52:27 -0700189
Winson Chung892c74d2013-08-22 16:15:50 -0700190 LauncherAppState app = LauncherAppState.getInstance();
191 DeviceProfile grid = app.getDynamicGrid().getDeviceProfile();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800192 TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.CellLayout, defStyle, 0);
193
Winson Chung11a1a532013-09-13 11:14:45 -0700194 mCellWidth = mCellHeight = -1;
Nilesh Agrawal5f7099a2014-01-02 15:54:57 -0800195 mFixedCellWidth = mFixedCellHeight = -1;
Winson Chung5f8afe62013-08-12 16:19:28 -0700196 mWidthGap = mOriginalWidthGap = 0;
197 mHeightGap = mOriginalHeightGap = 0;
198 mMaxGap = Integer.MAX_VALUE;
Winson Chung892c74d2013-08-22 16:15:50 -0700199 mCountX = (int) grid.numColumns;
200 mCountY = (int) grid.numRows;
Michael Jurka0280c3b2010-09-17 15:00:07 -0700201 mOccupied = new boolean[mCountX][mCountY];
Adam Cohen482ed822012-03-02 14:15:13 -0800202 mTmpOccupied = new boolean[mCountX][mCountY];
Adam Cohen5b53f292012-03-29 14:30:35 -0700203 mPreviousReorderDirection[0] = INVALID_DIRECTION;
204 mPreviousReorderDirection[1] = INVALID_DIRECTION;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800205
206 a.recycle();
207
208 setAlwaysDrawnWithCacheEnabled(false);
209
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700210 final Resources res = getResources();
Winson Chung6e1c0d32013-10-25 15:24:24 -0700211 mHotseatScale = (float) grid.hotseatIconSizePx / grid.iconSizePx;
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700212
Adam Cohen410f3cd2013-09-22 12:09:32 -0700213 mNormalBackground = res.getDrawable(R.drawable.screenpanel);
214 mActiveGlowBackground = res.getDrawable(R.drawable.screenpanel_hover);
Michael Jurka33945b22010-12-21 18:19:38 -0800215
Adam Cohenb5ba0972011-09-07 18:02:31 -0700216 mOverScrollLeft = res.getDrawable(R.drawable.overscroll_glow_left);
217 mOverScrollRight = res.getDrawable(R.drawable.overscroll_glow_right);
218 mForegroundPadding =
219 res.getDimensionPixelSize(R.dimen.workspace_overscroll_drawable_padding);
Michael Jurka33945b22010-12-21 18:19:38 -0800220
Adam Cohenfa3c58f2013-12-06 16:10:55 -0800221 mReorderPreviewAnimationMagnitude = (REORDER_PREVIEW_MAGNITUDE *
Winson Chung5f8afe62013-08-12 16:19:28 -0700222 grid.iconSizePx);
Adam Cohen19f37922012-03-21 11:59:11 -0700223
Winson Chungb26f3d62011-06-02 10:49:29 -0700224 mNormalBackground.setFilterBitmap(true);
Winson Chungb26f3d62011-06-02 10:49:29 -0700225 mActiveGlowBackground.setFilterBitmap(true);
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700226
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700227 // Initialize the data structures used for the drag visualization.
Patrick Dubroyce34a972010-10-19 10:34:32 -0700228 mEaseOutInterpolator = new DecelerateInterpolator(2.5f); // Quint ease out
Winson Chungb8c69f32011-10-19 21:36:08 -0700229 mDragCell[0] = mDragCell[1] = -1;
Joe Onorato4be866d2010-10-10 11:26:02 -0700230 for (int i = 0; i < mDragOutlines.length; i++) {
Adam Cohend41fbf52012-02-16 23:53:59 -0800231 mDragOutlines[i] = new Rect(-1, -1, -1, -1);
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700232 }
233
234 // When dragging things around the home screens, we show a green outline of
235 // where the item will land. The outlines gradually fade out, leaving a trail
236 // behind the drag path.
237 // Set up all the animations that are used to implement this fading.
238 final int duration = res.getInteger(R.integer.config_dragOutlineFadeTime);
Chet Haase472b2812010-10-14 07:02:04 -0700239 final float fromAlphaValue = 0;
240 final float toAlphaValue = (float)res.getInteger(R.integer.config_dragOutlineMaxAlpha);
Joe Onorato4be866d2010-10-10 11:26:02 -0700241
Patrick Dubroy8e58e912010-10-14 13:21:48 -0700242 Arrays.fill(mDragOutlineAlphas, fromAlphaValue);
Joe Onorato4be866d2010-10-10 11:26:02 -0700243
244 for (int i = 0; i < mDragOutlineAnims.length; i++) {
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700245 final InterruptibleInOutAnimator anim =
Michael Jurkaf1ad6082013-03-13 12:55:46 +0100246 new InterruptibleInOutAnimator(this, duration, fromAlphaValue, toAlphaValue);
Patrick Dubroyce34a972010-10-19 10:34:32 -0700247 anim.getAnimator().setInterpolator(mEaseOutInterpolator);
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700248 final int thisIndex = i;
Chet Haase472b2812010-10-14 07:02:04 -0700249 anim.getAnimator().addUpdateListener(new AnimatorUpdateListener() {
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700250 public void onAnimationUpdate(ValueAnimator animation) {
Joe Onorato4be866d2010-10-10 11:26:02 -0700251 final Bitmap outline = (Bitmap)anim.getTag();
252
253 // If an animation is started and then stopped very quickly, we can still
254 // get spurious updates we've cleared the tag. Guard against this.
255 if (outline == null) {
Michael Jurka3a9fced2012-04-13 14:44:29 -0700256 @SuppressWarnings("all") // suppress dead code warning
257 final boolean debug = false;
258 if (debug) {
Patrick Dubroyfe6bd872010-10-13 17:32:10 -0700259 Object val = animation.getAnimatedValue();
260 Log.d(TAG, "anim " + thisIndex + " update: " + val +
261 ", isStopped " + anim.isStopped());
262 }
Joe Onorato4be866d2010-10-10 11:26:02 -0700263 // Try to prevent it from continuing to run
264 animation.cancel();
265 } else {
Chet Haase472b2812010-10-14 07:02:04 -0700266 mDragOutlineAlphas[thisIndex] = (Float) animation.getAnimatedValue();
Adam Cohend41fbf52012-02-16 23:53:59 -0800267 CellLayout.this.invalidate(mDragOutlines[thisIndex]);
Joe Onorato4be866d2010-10-10 11:26:02 -0700268 }
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700269 }
270 });
Joe Onorato4be866d2010-10-10 11:26:02 -0700271 // The animation holds a reference to the drag outline bitmap as long is it's
272 // running. This way the bitmap can be GCed when the animations are complete.
Chet Haase472b2812010-10-14 07:02:04 -0700273 anim.getAnimator().addListener(new AnimatorListenerAdapter() {
Michael Jurka3c4c20f2010-10-28 15:36:06 -0700274 @Override
Joe Onorato4be866d2010-10-10 11:26:02 -0700275 public void onAnimationEnd(Animator animation) {
Chet Haase472b2812010-10-14 07:02:04 -0700276 if ((Float) ((ValueAnimator) animation).getAnimatedValue() == 0f) {
Joe Onorato4be866d2010-10-10 11:26:02 -0700277 anim.setTag(null);
278 }
279 }
280 });
281 mDragOutlineAnims[i] = anim;
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700282 }
Patrick Dubroyce34a972010-10-19 10:34:32 -0700283
Michael Jurka18014792010-10-14 09:01:34 -0700284 mBackgroundRect = new Rect();
Adam Cohenb5ba0972011-09-07 18:02:31 -0700285 mForegroundRect = new Rect();
Michael Jurkabea15192010-11-17 12:33:46 -0800286
Michael Jurkaa52570f2012-03-20 03:18:20 -0700287 mShortcutsAndWidgets = new ShortcutAndWidgetContainer(context);
Adam Cohen2374abf2013-04-16 14:56:57 -0700288 mShortcutsAndWidgets.setCellDimensions(mCellWidth, mCellHeight, mWidthGap, mHeightGap,
Winson Chung5f8afe62013-08-12 16:19:28 -0700289 mCountX, mCountY);
Adam Cohen2374abf2013-04-16 14:56:57 -0700290
Sunny Goyal508da152014-08-14 10:53:27 -0700291 mTouchFeedbackView = new FastBitmapView(context);
292 // Make the feedback view large enough to hold the blur bitmap.
293 addView(mTouchFeedbackView, (int) (grid.cellWidthPx * 1.5), (int) (grid.cellHeightPx * 1.5));
Michael Jurkaa52570f2012-03-20 03:18:20 -0700294 addView(mShortcutsAndWidgets);
Michael Jurka18014792010-10-14 09:01:34 -0700295 }
296
Chris Craik01f2d7f2013-10-01 14:41:56 -0700297 public void enableHardwareLayer(boolean hasLayer) {
298 mShortcutsAndWidgets.setLayerType(hasLayer ? LAYER_TYPE_HARDWARE : LAYER_TYPE_NONE, sPaint);
Michael Jurkad51f33a2012-06-28 15:35:26 -0700299 }
300
301 public void buildHardwareLayer() {
302 mShortcutsAndWidgets.buildLayer();
Adam Cohen2801caf2011-05-13 20:57:39 -0700303 }
304
Adam Cohen307fe232012-08-16 17:55:58 -0700305 public float getChildrenScale() {
306 return mIsHotseat ? mHotseatScale : 1.0f;
307 }
308
Winson Chung5f8afe62013-08-12 16:19:28 -0700309 public void setCellDimensions(int width, int height) {
Winson Chung11a1a532013-09-13 11:14:45 -0700310 mFixedCellWidth = mCellWidth = width;
311 mFixedCellHeight = mCellHeight = height;
Winson Chung5f8afe62013-08-12 16:19:28 -0700312 mShortcutsAndWidgets.setCellDimensions(mCellWidth, mCellHeight, mWidthGap, mHeightGap,
313 mCountX, mCountY);
314 }
315
Adam Cohen2801caf2011-05-13 20:57:39 -0700316 public void setGridSize(int x, int y) {
317 mCountX = x;
318 mCountY = y;
319 mOccupied = new boolean[mCountX][mCountY];
Adam Cohen482ed822012-03-02 14:15:13 -0800320 mTmpOccupied = new boolean[mCountX][mCountY];
Adam Cohen7fbec102012-03-27 12:42:19 -0700321 mTempRectStack.clear();
Adam Cohen2374abf2013-04-16 14:56:57 -0700322 mShortcutsAndWidgets.setCellDimensions(mCellWidth, mCellHeight, mWidthGap, mHeightGap,
Winson Chung5f8afe62013-08-12 16:19:28 -0700323 mCountX, mCountY);
Adam Cohen76fc0852011-06-17 13:26:23 -0700324 requestLayout();
Adam Cohen2801caf2011-05-13 20:57:39 -0700325 }
326
Adam Cohen2374abf2013-04-16 14:56:57 -0700327 // Set whether or not to invert the layout horizontally if the layout is in RTL mode.
328 public void setInvertIfRtl(boolean invert) {
329 mShortcutsAndWidgets.setInvertIfRtl(invert);
330 }
331
Adam Cohen917e3882013-10-31 15:03:35 -0700332 public void setDropPending(boolean pending) {
333 mDropPending = pending;
334 }
335
336 public boolean isDropPending() {
337 return mDropPending;
338 }
339
Adam Cohenb5ba0972011-09-07 18:02:31 -0700340 void setOverScrollAmount(float r, boolean left) {
341 if (left && mOverScrollForegroundDrawable != mOverScrollLeft) {
342 mOverScrollForegroundDrawable = mOverScrollLeft;
343 } else if (!left && mOverScrollForegroundDrawable != mOverScrollRight) {
344 mOverScrollForegroundDrawable = mOverScrollRight;
345 }
346
Adam Cohen02dcfcc2013-10-01 12:37:33 -0700347 r *= FOREGROUND_ALPHA_DAMPER;
Adam Cohenb5ba0972011-09-07 18:02:31 -0700348 mForegroundAlpha = (int) Math.round((r * 255));
349 mOverScrollForegroundDrawable.setAlpha(mForegroundAlpha);
350 invalidate();
351 }
352
Sunny Goyal508da152014-08-14 10:53:27 -0700353 void setPressedIcon(BubbleTextView icon, Bitmap background, int padding) {
354 if (icon == null || background == null) {
355 mTouchFeedbackView.setBitmap(null);
356 mTouchFeedbackView.animate().cancel();
357 } else {
358 int offset = getMeasuredWidth() - getPaddingLeft() - getPaddingRight()
359 - (mCountX * mCellWidth);
360 mTouchFeedbackView.setTranslationX(icon.getLeft() + (int) Math.ceil(offset / 2f)
361 - padding);
362 mTouchFeedbackView.setTranslationY(icon.getTop() - padding);
363 if (mTouchFeedbackView.setBitmap(background)) {
364 mTouchFeedbackView.setAlpha(0);
365 mTouchFeedbackView.animate().alpha(1)
366 .setDuration(FastBitmapDrawable.CLICK_FEEDBACK_DURATION)
367 .setInterpolator(FastBitmapDrawable.CLICK_FEEDBACK_INTERPOLATOR)
368 .start();
369 }
Patrick Dubroy96864c32011-03-10 17:17:23 -0800370 }
371 }
372
Adam Cohendedbd962013-07-11 14:21:49 -0700373 void setUseActiveGlowBackground(boolean use) {
374 mUseActiveGlowBackground = use;
375 }
376
Winson Chung59a488a2013-12-10 12:32:14 -0800377 void disableBackground() {
378 mDrawBackground = false;
379 }
380
Adam Cohenc50438c2014-08-19 17:43:05 -0700381 void disableDragTarget() {
382 mIsDragTarget = false;
383 }
384
385 boolean isDragTarget() {
386 return mIsDragTarget;
387 }
388
389 void setIsDragOverlapping(boolean isDragOverlapping) {
390 if (mIsDragOverlapping != isDragOverlapping) {
391 mIsDragOverlapping = isDragOverlapping;
392 setUseActiveGlowBackground(mIsDragOverlapping);
393 invalidate();
394 }
395 }
396
Michael Jurka33945b22010-12-21 18:19:38 -0800397 boolean getIsDragOverlapping() {
398 return mIsDragOverlapping;
399 }
400
Jeff Sharkey83f111d2009-04-20 21:03:13 -0700401 @Override
Patrick Dubroy1262e362010-10-06 15:49:50 -0700402 protected void onDraw(Canvas canvas) {
Michael Jurka3e7c7632010-10-02 16:01:03 -0700403 // When we're large, we are either drawn in a "hover" state (ie when dragging an item to
404 // a neighboring page) or with just a normal background (if backgroundAlpha > 0.0f)
405 // When we're small, we are either drawn normally or in the "accepts drops" state (during
406 // a drag). However, we also drag the mini hover background *over* one of those two
407 // backgrounds
Winson Chung59a488a2013-12-10 12:32:14 -0800408 if (mDrawBackground && mBackgroundAlpha > 0.0f) {
Adam Cohenf34bab52010-09-30 14:11:56 -0700409 Drawable bg;
Michael Jurka33945b22010-12-21 18:19:38 -0800410
Adam Cohendedbd962013-07-11 14:21:49 -0700411 if (mUseActiveGlowBackground) {
Michael Jurka33945b22010-12-21 18:19:38 -0800412 // In the mini case, we draw the active_glow bg *over* the active background
Michael Jurkabdf78552011-10-31 14:34:25 -0700413 bg = mActiveGlowBackground;
Adam Cohenf34bab52010-09-30 14:11:56 -0700414 } else {
Michael Jurkabdf78552011-10-31 14:34:25 -0700415 bg = mNormalBackground;
Adam Cohenf34bab52010-09-30 14:11:56 -0700416 }
Michael Jurka33945b22010-12-21 18:19:38 -0800417
418 bg.setAlpha((int) (mBackgroundAlpha * mBackgroundAlphaMultiplier * 255));
419 bg.setBounds(mBackgroundRect);
420 bg.draw(canvas);
Michael Jurkaa63c4522010-08-19 13:52:27 -0700421 }
Romain Guya6abce82009-11-10 02:54:41 -0800422
Patrick Dubroy8e58e912010-10-14 13:21:48 -0700423 final Paint paint = mDragOutlinePaint;
Joe Onorato4be866d2010-10-10 11:26:02 -0700424 for (int i = 0; i < mDragOutlines.length; i++) {
Chet Haase472b2812010-10-14 07:02:04 -0700425 final float alpha = mDragOutlineAlphas[i];
Joe Onorato4be866d2010-10-10 11:26:02 -0700426 if (alpha > 0) {
Adam Cohend41fbf52012-02-16 23:53:59 -0800427 final Rect r = mDragOutlines[i];
Winson Chung3a6e7f32013-10-09 15:50:52 -0700428 mTempRect.set(r);
429 Utilities.scaleRectAboutCenter(mTempRect, getChildrenScale());
Joe Onorato4be866d2010-10-10 11:26:02 -0700430 final Bitmap b = (Bitmap) mDragOutlineAnims[i].getTag();
Chet Haase472b2812010-10-14 07:02:04 -0700431 paint.setAlpha((int)(alpha + .5f));
Winson Chung3a6e7f32013-10-09 15:50:52 -0700432 canvas.drawBitmap(b, null, mTempRect, paint);
Winson Chung150fbab2010-09-29 17:14:26 -0700433 }
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700434 }
Patrick Dubroy96864c32011-03-10 17:17:23 -0800435
Adam Cohen482ed822012-03-02 14:15:13 -0800436 if (DEBUG_VISUALIZE_OCCUPIED) {
437 int[] pt = new int[2];
438 ColorDrawable cd = new ColorDrawable(Color.RED);
Adam Cohene7587d22012-05-24 18:50:02 -0700439 cd.setBounds(0, 0, mCellWidth, mCellHeight);
Adam Cohen482ed822012-03-02 14:15:13 -0800440 for (int i = 0; i < mCountX; i++) {
441 for (int j = 0; j < mCountY; j++) {
442 if (mOccupied[i][j]) {
443 cellToPoint(i, j, pt);
444 canvas.save();
445 canvas.translate(pt[0], pt[1]);
446 cd.draw(canvas);
447 canvas.restore();
448 }
449 }
450 }
451 }
452
Andrew Flynn850d2e72012-04-26 16:51:20 -0700453 int previewOffset = FolderRingAnimator.sPreviewSize;
454
Adam Cohen69ce2e52011-07-03 19:25:21 -0700455 // The folder outer / inner ring image(s)
Winson Chung5f8afe62013-08-12 16:19:28 -0700456 LauncherAppState app = LauncherAppState.getInstance();
457 DeviceProfile grid = app.getDynamicGrid().getDeviceProfile();
Adam Cohen69ce2e52011-07-03 19:25:21 -0700458 for (int i = 0; i < mFolderOuterRings.size(); i++) {
459 FolderRingAnimator fra = mFolderOuterRings.get(i);
460
Adam Cohen5108bc02013-09-20 17:04:51 -0700461 Drawable d;
462 int width, height;
Adam Cohen69ce2e52011-07-03 19:25:21 -0700463 cellToPoint(fra.mCellX, fra.mCellY, mTempLocation);
Winson Chung5f8afe62013-08-12 16:19:28 -0700464 View child = getChildAt(fra.mCellX, fra.mCellY);
Adam Cohen558f1c22013-10-09 15:15:24 -0700465
Winson Chung89f97052013-09-20 11:32:26 -0700466 if (child != null) {
Adam Cohen558f1c22013-10-09 15:15:24 -0700467 int centerX = mTempLocation[0] + mCellWidth / 2;
468 int centerY = mTempLocation[1] + previewOffset / 2 +
469 child.getPaddingTop() + grid.folderBackgroundOffset;
470
Adam Cohen5108bc02013-09-20 17:04:51 -0700471 // Draw outer ring, if it exists
472 if (FolderIcon.HAS_OUTER_RING) {
473 d = FolderRingAnimator.sSharedOuterRingDrawable;
474 width = (int) (fra.getOuterRingSize() * getChildrenScale());
475 height = width;
476 canvas.save();
477 canvas.translate(centerX - width / 2, centerY - height / 2);
478 d.setBounds(0, 0, width, height);
479 d.draw(canvas);
480 canvas.restore();
481 }
Adam Cohen69ce2e52011-07-03 19:25:21 -0700482
Winson Chung89f97052013-09-20 11:32:26 -0700483 // Draw inner ring
484 d = FolderRingAnimator.sSharedInnerRingDrawable;
485 width = (int) (fra.getInnerRingSize() * getChildrenScale());
486 height = width;
Winson Chung89f97052013-09-20 11:32:26 -0700487 canvas.save();
488 canvas.translate(centerX - width / 2, centerY - width / 2);
489 d.setBounds(0, 0, width, height);
490 d.draw(canvas);
491 canvas.restore();
492 }
Adam Cohen69ce2e52011-07-03 19:25:21 -0700493 }
Adam Cohenc51934b2011-07-26 21:07:43 -0700494
495 if (mFolderLeaveBehindCell[0] >= 0 && mFolderLeaveBehindCell[1] >= 0) {
496 Drawable d = FolderIcon.sSharedFolderLeaveBehind;
497 int width = d.getIntrinsicWidth();
498 int height = d.getIntrinsicHeight();
499
500 cellToPoint(mFolderLeaveBehindCell[0], mFolderLeaveBehindCell[1], mTempLocation);
Winson Chung5f8afe62013-08-12 16:19:28 -0700501 View child = getChildAt(mFolderLeaveBehindCell[0], mFolderLeaveBehindCell[1]);
Winson Chung89f97052013-09-20 11:32:26 -0700502 if (child != null) {
503 int centerX = mTempLocation[0] + mCellWidth / 2;
504 int centerY = mTempLocation[1] + previewOffset / 2 +
505 child.getPaddingTop() + grid.folderBackgroundOffset;
Adam Cohenc51934b2011-07-26 21:07:43 -0700506
Winson Chung89f97052013-09-20 11:32:26 -0700507 canvas.save();
508 canvas.translate(centerX - width / 2, centerY - width / 2);
509 d.setBounds(0, 0, width, height);
510 d.draw(canvas);
511 canvas.restore();
512 }
Adam Cohenc51934b2011-07-26 21:07:43 -0700513 }
Adam Cohen69ce2e52011-07-03 19:25:21 -0700514 }
515
Adam Cohenb5ba0972011-09-07 18:02:31 -0700516 @Override
517 protected void dispatchDraw(Canvas canvas) {
518 super.dispatchDraw(canvas);
519 if (mForegroundAlpha > 0) {
520 mOverScrollForegroundDrawable.setBounds(mForegroundRect);
Adam Cohenb5ba0972011-09-07 18:02:31 -0700521 mOverScrollForegroundDrawable.draw(canvas);
Adam Cohenb5ba0972011-09-07 18:02:31 -0700522 }
523 }
524
Adam Cohen69ce2e52011-07-03 19:25:21 -0700525 public void showFolderAccept(FolderRingAnimator fra) {
526 mFolderOuterRings.add(fra);
527 }
528
529 public void hideFolderAccept(FolderRingAnimator fra) {
530 if (mFolderOuterRings.contains(fra)) {
531 mFolderOuterRings.remove(fra);
532 }
533 invalidate();
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700534 }
535
Adam Cohenc51934b2011-07-26 21:07:43 -0700536 public void setFolderLeaveBehindCell(int x, int y) {
537 mFolderLeaveBehindCell[0] = x;
538 mFolderLeaveBehindCell[1] = y;
539 invalidate();
540 }
541
542 public void clearFolderLeaveBehind() {
543 mFolderLeaveBehindCell[0] = -1;
544 mFolderLeaveBehindCell[1] = -1;
545 invalidate();
546 }
547
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700548 @Override
Michael Jurkae6235dd2011-10-04 15:02:05 -0700549 public boolean shouldDelayChildPressedState() {
550 return false;
551 }
552
Adam Cohen1462de32012-07-24 22:34:36 -0700553 public void restoreInstanceState(SparseArray<Parcelable> states) {
Sunny Goyal33a152f2014-07-22 12:13:14 -0700554 try {
555 dispatchRestoreInstanceState(states);
556 } catch (IllegalArgumentException ex) {
557 if (LauncherAppState.isDogfoodBuild()) {
558 throw ex;
559 }
560 // Mismatched viewId / viewType preventing restore. Skip restore on production builds.
561 Log.e(TAG, "Ignoring an error while restoring a view instance state", ex);
562 }
Adam Cohen1462de32012-07-24 22:34:36 -0700563 }
564
Michael Jurkae6235dd2011-10-04 15:02:05 -0700565 @Override
Jeff Sharkey83f111d2009-04-20 21:03:13 -0700566 public void cancelLongPress() {
567 super.cancelLongPress();
568
569 // Cancel long press for all children
570 final int count = getChildCount();
571 for (int i = 0; i < count; i++) {
572 final View child = getChildAt(i);
573 child.cancelLongPress();
574 }
575 }
576
Michael Jurkadee05892010-07-27 10:01:56 -0700577 public void setOnInterceptTouchListener(View.OnTouchListener listener) {
578 mInterceptTouchListener = listener;
579 }
580
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800581 int getCountX() {
Adam Cohend22015c2010-07-26 22:02:18 -0700582 return mCountX;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800583 }
584
585 int getCountY() {
Adam Cohend22015c2010-07-26 22:02:18 -0700586 return mCountY;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800587 }
588
Andrew Flynn0dca1ec2012-02-29 13:33:22 -0800589 public void setIsHotseat(boolean isHotseat) {
590 mIsHotseat = isHotseat;
Winson Chung5f8afe62013-08-12 16:19:28 -0700591 mShortcutsAndWidgets.setIsHotseat(isHotseat);
Andrew Flynn0dca1ec2012-02-29 13:33:22 -0800592 }
593
Andrew Flynn0dca1ec2012-02-29 13:33:22 -0800594 public boolean addViewToCellLayout(View child, int index, int childId, LayoutParams params,
Adam Cohen59400422014-03-05 18:07:04 -0800595 boolean markCells, boolean inLayout) {
Winson Chungaafa03c2010-06-11 17:34:16 -0700596 final LayoutParams lp = params;
597
Andrew Flynnde38e422012-05-08 11:22:15 -0700598 // Hotseat icons - remove text
Andrew Flynn0dca1ec2012-02-29 13:33:22 -0800599 if (child instanceof BubbleTextView) {
600 BubbleTextView bubbleChild = (BubbleTextView) child;
Winson Chung5f8afe62013-08-12 16:19:28 -0700601 bubbleChild.setTextVisibility(!mIsHotseat);
Andrew Flynn0dca1ec2012-02-29 13:33:22 -0800602 }
603
Adam Cohen307fe232012-08-16 17:55:58 -0700604 child.setScaleX(getChildrenScale());
605 child.setScaleY(getChildrenScale());
606
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800607 // Generate an id for each view, this assumes we have at most 256x256 cells
608 // per workspace screen
Adam Cohend22015c2010-07-26 22:02:18 -0700609 if (lp.cellX >= 0 && lp.cellX <= mCountX - 1 && lp.cellY >= 0 && lp.cellY <= mCountY - 1) {
Winson Chungaafa03c2010-06-11 17:34:16 -0700610 // If the horizontal or vertical span is set to -1, it is taken to
611 // mean that it spans the extent of the CellLayout
Adam Cohend22015c2010-07-26 22:02:18 -0700612 if (lp.cellHSpan < 0) lp.cellHSpan = mCountX;
613 if (lp.cellVSpan < 0) lp.cellVSpan = mCountY;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800614
Winson Chungaafa03c2010-06-11 17:34:16 -0700615 child.setId(childId);
Adam Cohen59400422014-03-05 18:07:04 -0800616 if (inLayout) {
617 mShortcutsAndWidgets.addView(child, index, lp, true);
618 } else {
619 mShortcutsAndWidgets.addView(child, index, lp, false);
620 }
Michael Jurkadee05892010-07-27 10:01:56 -0700621
Michael Jurkaf3ca3ab2010-10-20 17:08:24 -0700622 if (markCells) markCellsAsOccupiedForView(child);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700623
Winson Chungaafa03c2010-06-11 17:34:16 -0700624 return true;
625 }
626 return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800627 }
Michael Jurka3e7c7632010-10-02 16:01:03 -0700628
Adam Cohen59400422014-03-05 18:07:04 -0800629 public boolean addViewToCellLayout(View child, int index, int childId, LayoutParams params,
630 boolean markCells) {
631 return addViewToCellLayout(child, index, childId, params, markCells, false);
632 }
633
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800634 @Override
Michael Jurka0280c3b2010-09-17 15:00:07 -0700635 public void removeAllViews() {
636 clearOccupiedCells();
Michael Jurkaa52570f2012-03-20 03:18:20 -0700637 mShortcutsAndWidgets.removeAllViews();
Michael Jurka0280c3b2010-09-17 15:00:07 -0700638 }
639
640 @Override
641 public void removeAllViewsInLayout() {
Michael Jurkaa52570f2012-03-20 03:18:20 -0700642 if (mShortcutsAndWidgets.getChildCount() > 0) {
Michael Jurka7cfc2822011-08-02 20:19:24 -0700643 clearOccupiedCells();
Michael Jurkaa52570f2012-03-20 03:18:20 -0700644 mShortcutsAndWidgets.removeAllViewsInLayout();
Michael Jurka7cfc2822011-08-02 20:19:24 -0700645 }
Michael Jurka0280c3b2010-09-17 15:00:07 -0700646 }
647
Michael Jurkaf3ca3ab2010-10-20 17:08:24 -0700648 public void removeViewWithoutMarkingCells(View view) {
Michael Jurkaa52570f2012-03-20 03:18:20 -0700649 mShortcutsAndWidgets.removeView(view);
Michael Jurkaf3ca3ab2010-10-20 17:08:24 -0700650 }
651
Michael Jurka0280c3b2010-09-17 15:00:07 -0700652 @Override
653 public void removeView(View view) {
654 markCellsAsUnoccupiedForView(view);
Michael Jurkaa52570f2012-03-20 03:18:20 -0700655 mShortcutsAndWidgets.removeView(view);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700656 }
657
658 @Override
659 public void removeViewAt(int index) {
Michael Jurkaa52570f2012-03-20 03:18:20 -0700660 markCellsAsUnoccupiedForView(mShortcutsAndWidgets.getChildAt(index));
661 mShortcutsAndWidgets.removeViewAt(index);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700662 }
663
664 @Override
665 public void removeViewInLayout(View view) {
666 markCellsAsUnoccupiedForView(view);
Michael Jurkaa52570f2012-03-20 03:18:20 -0700667 mShortcutsAndWidgets.removeViewInLayout(view);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700668 }
669
670 @Override
671 public void removeViews(int start, int count) {
672 for (int i = start; i < start + count; i++) {
Michael Jurkaa52570f2012-03-20 03:18:20 -0700673 markCellsAsUnoccupiedForView(mShortcutsAndWidgets.getChildAt(i));
Michael Jurka0280c3b2010-09-17 15:00:07 -0700674 }
Michael Jurkaa52570f2012-03-20 03:18:20 -0700675 mShortcutsAndWidgets.removeViews(start, count);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700676 }
677
678 @Override
679 public void removeViewsInLayout(int start, int count) {
680 for (int i = start; i < start + count; i++) {
Michael Jurkaa52570f2012-03-20 03:18:20 -0700681 markCellsAsUnoccupiedForView(mShortcutsAndWidgets.getChildAt(i));
Michael Jurka0280c3b2010-09-17 15:00:07 -0700682 }
Michael Jurkaa52570f2012-03-20 03:18:20 -0700683 mShortcutsAndWidgets.removeViewsInLayout(start, count);
Michael Jurkaabded662011-03-04 12:06:57 -0800684 }
685
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800686 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800687 public boolean onInterceptTouchEvent(MotionEvent ev) {
Adam Cohenc1997fd2011-08-15 18:26:39 -0700688 // First we clear the tag to ensure that on every touch down we start with a fresh slate,
689 // even in the case where we return early. Not clearing here was causing bugs whereby on
690 // long-press we'd end up picking up an item from a previous drag operation.
Michael Jurkadee05892010-07-27 10:01:56 -0700691 if (mInterceptTouchListener != null && mInterceptTouchListener.onTouch(this, ev)) {
692 return true;
693 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800694
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800695 return false;
696 }
697
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700698 /**
Winson Chungaafa03c2010-06-11 17:34:16 -0700699 * Given a point, return the cell that strictly encloses that point
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800700 * @param x X coordinate of the point
701 * @param y Y coordinate of the point
702 * @param result Array of 2 ints to hold the x and y coordinate of the cell
703 */
704 void pointToCellExact(int x, int y, int[] result) {
Winson Chung4b825dcd2011-06-19 12:41:22 -0700705 final int hStartPadding = getPaddingLeft();
706 final int vStartPadding = getPaddingTop();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800707
708 result[0] = (x - hStartPadding) / (mCellWidth + mWidthGap);
709 result[1] = (y - vStartPadding) / (mCellHeight + mHeightGap);
710
Adam Cohend22015c2010-07-26 22:02:18 -0700711 final int xAxis = mCountX;
712 final int yAxis = mCountY;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800713
714 if (result[0] < 0) result[0] = 0;
715 if (result[0] >= xAxis) result[0] = xAxis - 1;
716 if (result[1] < 0) result[1] = 0;
717 if (result[1] >= yAxis) result[1] = yAxis - 1;
718 }
Winson Chungaafa03c2010-06-11 17:34:16 -0700719
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800720 /**
721 * Given a point, return the cell that most closely encloses that point
722 * @param x X coordinate of the point
723 * @param y Y coordinate of the point
724 * @param result Array of 2 ints to hold the x and y coordinate of the cell
725 */
726 void pointToCellRounded(int x, int y, int[] result) {
727 pointToCellExact(x + (mCellWidth / 2), y + (mCellHeight / 2), result);
728 }
729
730 /**
731 * Given a cell coordinate, return the point that represents the upper left corner of that cell
Winson Chungaafa03c2010-06-11 17:34:16 -0700732 *
733 * @param cellX X coordinate of the cell
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800734 * @param cellY Y coordinate of the cell
Winson Chungaafa03c2010-06-11 17:34:16 -0700735 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800736 * @param result Array of 2 ints to hold the x and y coordinate of the point
737 */
738 void cellToPoint(int cellX, int cellY, int[] result) {
Winson Chung4b825dcd2011-06-19 12:41:22 -0700739 final int hStartPadding = getPaddingLeft();
740 final int vStartPadding = getPaddingTop();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800741
742 result[0] = hStartPadding + cellX * (mCellWidth + mWidthGap);
743 result[1] = vStartPadding + cellY * (mCellHeight + mHeightGap);
744 }
745
Adam Cohene3e27a82011-04-15 12:07:39 -0700746 /**
Adam Cohen482ed822012-03-02 14:15:13 -0800747 * Given a cell coordinate, return the point that represents the center of the cell
Adam Cohene3e27a82011-04-15 12:07:39 -0700748 *
749 * @param cellX X coordinate of the cell
750 * @param cellY Y coordinate of the cell
751 *
752 * @param result Array of 2 ints to hold the x and y coordinate of the point
753 */
754 void cellToCenterPoint(int cellX, int cellY, int[] result) {
Adam Cohen47a876d2012-03-19 13:21:41 -0700755 regionToCenterPoint(cellX, cellY, 1, 1, result);
756 }
757
758 /**
759 * Given a cell coordinate and span return the point that represents the center of the regio
760 *
761 * @param cellX X coordinate of the cell
762 * @param cellY Y coordinate of the cell
763 *
764 * @param result Array of 2 ints to hold the x and y coordinate of the point
765 */
766 void regionToCenterPoint(int cellX, int cellY, int spanX, int spanY, int[] result) {
Winson Chung4b825dcd2011-06-19 12:41:22 -0700767 final int hStartPadding = getPaddingLeft();
768 final int vStartPadding = getPaddingTop();
Adam Cohen47a876d2012-03-19 13:21:41 -0700769 result[0] = hStartPadding + cellX * (mCellWidth + mWidthGap) +
770 (spanX * mCellWidth + (spanX - 1) * mWidthGap) / 2;
771 result[1] = vStartPadding + cellY * (mCellHeight + mHeightGap) +
772 (spanY * mCellHeight + (spanY - 1) * mHeightGap) / 2;
Adam Cohene3e27a82011-04-15 12:07:39 -0700773 }
774
Adam Cohen19f37922012-03-21 11:59:11 -0700775 /**
776 * Given a cell coordinate and span fills out a corresponding pixel rect
777 *
778 * @param cellX X coordinate of the cell
779 * @param cellY Y coordinate of the cell
780 * @param result Rect in which to write the result
781 */
782 void regionToRect(int cellX, int cellY, int spanX, int spanY, Rect result) {
783 final int hStartPadding = getPaddingLeft();
784 final int vStartPadding = getPaddingTop();
785 final int left = hStartPadding + cellX * (mCellWidth + mWidthGap);
786 final int top = vStartPadding + cellY * (mCellHeight + mHeightGap);
787 result.set(left, top, left + (spanX * mCellWidth + (spanX - 1) * mWidthGap),
788 top + (spanY * mCellHeight + (spanY - 1) * mHeightGap));
789 }
790
Adam Cohen482ed822012-03-02 14:15:13 -0800791 public float getDistanceFromCell(float x, float y, int[] cell) {
792 cellToCenterPoint(cell[0], cell[1], mTmpPoint);
793 float distance = (float) Math.sqrt( Math.pow(x - mTmpPoint[0], 2) +
794 Math.pow(y - mTmpPoint[1], 2));
795 return distance;
796 }
797
Romain Guy84f296c2009-11-04 15:00:44 -0800798 int getCellWidth() {
799 return mCellWidth;
800 }
801
802 int getCellHeight() {
803 return mCellHeight;
804 }
805
Adam Cohend4844c32011-02-18 19:25:06 -0800806 int getWidthGap() {
807 return mWidthGap;
808 }
809
810 int getHeightGap() {
811 return mHeightGap;
812 }
813
Adam Cohen7f4eabe2011-04-21 16:19:16 -0700814 Rect getContentRect(Rect r) {
815 if (r == null) {
816 r = new Rect();
817 }
818 int left = getPaddingLeft();
819 int top = getPaddingTop();
Michael Jurka8b805b12012-04-18 14:23:14 -0700820 int right = left + getWidth() - getPaddingLeft() - getPaddingRight();
821 int bottom = top + getHeight() - getPaddingTop() - getPaddingBottom();
Adam Cohen7f4eabe2011-04-21 16:19:16 -0700822 r.set(left, top, right, bottom);
823 return r;
824 }
825
Winson Chungfe411c82013-09-26 16:07:17 -0700826 /** Return a rect that has the cellWidth/cellHeight (left, top), and
827 * widthGap/heightGap (right, bottom) */
Winson Chung66700732013-08-20 16:56:15 -0700828 static void getMetrics(Rect metrics, int paddedMeasureWidth,
829 int paddedMeasureHeight, int countX, int countY) {
Winson Chung5f8afe62013-08-12 16:19:28 -0700830 LauncherAppState app = LauncherAppState.getInstance();
831 DeviceProfile grid = app.getDynamicGrid().getDeviceProfile();
Winson Chung66700732013-08-20 16:56:15 -0700832 metrics.set(grid.calculateCellWidth(paddedMeasureWidth, countX),
833 grid.calculateCellHeight(paddedMeasureHeight, countY), 0, 0);
Adam Cohenf4bd5792012-04-27 11:35:29 -0700834 }
835
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700836 public void setFixedSize(int width, int height) {
837 mFixedWidth = width;
838 mFixedHeight = height;
839 }
840
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800841 @Override
842 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
Winson Chung5f8afe62013-08-12 16:19:28 -0700843 LauncherAppState app = LauncherAppState.getInstance();
844 DeviceProfile grid = app.getDynamicGrid().getDeviceProfile();
845
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800846 int widthSpecMode = MeasureSpec.getMode(widthMeasureSpec);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800847 int heightSpecMode = MeasureSpec.getMode(heightMeasureSpec);
Winson Chung5f8afe62013-08-12 16:19:28 -0700848 int widthSize = MeasureSpec.getSize(widthMeasureSpec);
849 int heightSize = MeasureSpec.getSize(heightMeasureSpec);
Winson Chung2d75f122013-09-23 16:53:31 -0700850 int childWidthSize = widthSize - (getPaddingLeft() + getPaddingRight());
851 int childHeightSize = heightSize - (getPaddingTop() + getPaddingBottom());
Winson Chung11a1a532013-09-13 11:14:45 -0700852 if (mFixedCellWidth < 0 || mFixedCellHeight < 0) {
Winson Chung2d75f122013-09-23 16:53:31 -0700853 int cw = grid.calculateCellWidth(childWidthSize, mCountX);
854 int ch = grid.calculateCellHeight(childHeightSize, mCountY);
Winson Chung11a1a532013-09-13 11:14:45 -0700855 if (cw != mCellWidth || ch != mCellHeight) {
856 mCellWidth = cw;
857 mCellHeight = ch;
858 mShortcutsAndWidgets.setCellDimensions(mCellWidth, mCellHeight, mWidthGap,
859 mHeightGap, mCountX, mCountY);
860 }
Winson Chung5f8afe62013-08-12 16:19:28 -0700861 }
Winson Chungaafa03c2010-06-11 17:34:16 -0700862
Winson Chung2d75f122013-09-23 16:53:31 -0700863 int newWidth = childWidthSize;
864 int newHeight = childHeightSize;
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700865 if (mFixedWidth > 0 && mFixedHeight > 0) {
866 newWidth = mFixedWidth;
867 newHeight = mFixedHeight;
868 } else if (widthSpecMode == MeasureSpec.UNSPECIFIED || heightSpecMode == MeasureSpec.UNSPECIFIED) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800869 throw new RuntimeException("CellLayout cannot have UNSPECIFIED dimensions");
870 }
871
Adam Cohend22015c2010-07-26 22:02:18 -0700872 int numWidthGaps = mCountX - 1;
873 int numHeightGaps = mCountY - 1;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800874
Adam Cohen234c4cd2011-07-17 21:03:04 -0700875 if (mOriginalWidthGap < 0 || mOriginalHeightGap < 0) {
Winson Chung2d75f122013-09-23 16:53:31 -0700876 int hSpace = childWidthSize;
877 int vSpace = childHeightSize;
Adam Cohenf4bd5792012-04-27 11:35:29 -0700878 int hFreeSpace = hSpace - (mCountX * mCellWidth);
879 int vFreeSpace = vSpace - (mCountY * mCellHeight);
Winson Chung4b825dcd2011-06-19 12:41:22 -0700880 mWidthGap = Math.min(mMaxGap, numWidthGaps > 0 ? (hFreeSpace / numWidthGaps) : 0);
881 mHeightGap = Math.min(mMaxGap,numHeightGaps > 0 ? (vFreeSpace / numHeightGaps) : 0);
Winson Chung5f8afe62013-08-12 16:19:28 -0700882 mShortcutsAndWidgets.setCellDimensions(mCellWidth, mCellHeight, mWidthGap,
883 mHeightGap, mCountX, mCountY);
Adam Cohen234c4cd2011-07-17 21:03:04 -0700884 } else {
885 mWidthGap = mOriginalWidthGap;
886 mHeightGap = mOriginalHeightGap;
Winson Chungece7f5b2010-10-22 14:54:12 -0700887 }
Michael Jurka8c920dd2011-01-20 14:16:56 -0800888 int count = getChildCount();
Winson Chung5f8afe62013-08-12 16:19:28 -0700889 int maxWidth = 0;
890 int maxHeight = 0;
Michael Jurka8c920dd2011-01-20 14:16:56 -0800891 for (int i = 0; i < count; i++) {
892 View child = getChildAt(i);
Winson Chung2d75f122013-09-23 16:53:31 -0700893 int childWidthMeasureSpec = MeasureSpec.makeMeasureSpec(newWidth,
894 MeasureSpec.EXACTLY);
895 int childheightMeasureSpec = MeasureSpec.makeMeasureSpec(newHeight,
896 MeasureSpec.EXACTLY);
Michael Jurka8c920dd2011-01-20 14:16:56 -0800897 child.measure(childWidthMeasureSpec, childheightMeasureSpec);
Winson Chung5f8afe62013-08-12 16:19:28 -0700898 maxWidth = Math.max(maxWidth, child.getMeasuredWidth());
899 maxHeight = Math.max(maxHeight, child.getMeasuredHeight());
Michael Jurka8c920dd2011-01-20 14:16:56 -0800900 }
Winson Chung2d75f122013-09-23 16:53:31 -0700901 if (mFixedWidth > 0 && mFixedHeight > 0) {
902 setMeasuredDimension(maxWidth, maxHeight);
903 } else {
904 setMeasuredDimension(widthSize, heightSize);
905 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800906 }
907
908 @Override
Michael Jurka28750fb2010-09-24 17:43:49 -0700909 protected void onLayout(boolean changed, int l, int t, int r, int b) {
Winson Chung38848ca2013-10-08 12:03:44 -0700910 int offset = getMeasuredWidth() - getPaddingLeft() - getPaddingRight() -
911 (mCountX * mCellWidth);
912 int left = getPaddingLeft() + (int) Math.ceil(offset / 2f);
913 int top = getPaddingTop();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800914 int count = getChildCount();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800915 for (int i = 0; i < count; i++) {
Michael Jurka8c920dd2011-01-20 14:16:56 -0800916 View child = getChildAt(i);
Winson Chung2d75f122013-09-23 16:53:31 -0700917 child.layout(left, top,
918 left + r - l,
919 top + b - t);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800920 }
921 }
922
923 @Override
Michael Jurkadee05892010-07-27 10:01:56 -0700924 protected void onSizeChanged(int w, int h, int oldw, int oldh) {
925 super.onSizeChanged(w, h, oldw, oldh);
Winson Chung82a9bd22013-10-08 16:02:34 -0700926
927 // Expand the background drawing bounds by the padding baked into the background drawable
928 Rect padding = new Rect();
929 mNormalBackground.getPadding(padding);
930 mBackgroundRect.set(-padding.left, -padding.top, w + padding.right, h + padding.bottom);
931
Adam Cohenb5ba0972011-09-07 18:02:31 -0700932 mForegroundRect.set(mForegroundPadding, mForegroundPadding,
Adam Cohen215b4162012-08-30 13:14:08 -0700933 w - mForegroundPadding, h - mForegroundPadding);
Michael Jurkadee05892010-07-27 10:01:56 -0700934 }
935
936 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800937 protected void setChildrenDrawingCacheEnabled(boolean enabled) {
Michael Jurkaa52570f2012-03-20 03:18:20 -0700938 mShortcutsAndWidgets.setChildrenDrawingCacheEnabled(enabled);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800939 }
940
941 @Override
942 protected void setChildrenDrawnWithCacheEnabled(boolean enabled) {
Michael Jurkaa52570f2012-03-20 03:18:20 -0700943 mShortcutsAndWidgets.setChildrenDrawnWithCacheEnabled(enabled);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800944 }
945
Michael Jurka5f1c5092010-09-03 14:15:02 -0700946 public float getBackgroundAlpha() {
947 return mBackgroundAlpha;
Michael Jurkadee05892010-07-27 10:01:56 -0700948 }
949
Adam Cohen1b0aaac2010-10-28 11:11:18 -0700950 public void setBackgroundAlphaMultiplier(float multiplier) {
Adam Cohenc50438c2014-08-19 17:43:05 -0700951
Michael Jurkaa3d30ad2012-05-08 13:43:43 -0700952 if (mBackgroundAlphaMultiplier != multiplier) {
953 mBackgroundAlphaMultiplier = multiplier;
954 invalidate();
955 }
Adam Cohen1b0aaac2010-10-28 11:11:18 -0700956 }
957
Adam Cohenddb82192010-11-10 16:32:54 -0800958 public float getBackgroundAlphaMultiplier() {
959 return mBackgroundAlphaMultiplier;
960 }
961
Michael Jurka5f1c5092010-09-03 14:15:02 -0700962 public void setBackgroundAlpha(float alpha) {
Michael Jurkaafaa0502011-12-13 18:22:50 -0800963 if (mBackgroundAlpha != alpha) {
964 mBackgroundAlpha = alpha;
965 invalidate();
966 }
Michael Jurkadee05892010-07-27 10:01:56 -0700967 }
968
Michael Jurkaa52570f2012-03-20 03:18:20 -0700969 public void setShortcutAndWidgetAlpha(float alpha) {
Sunny Goyal02b50812014-09-10 15:44:42 -0700970 mShortcutsAndWidgets.setAlpha(alpha);
Michael Jurkadee05892010-07-27 10:01:56 -0700971 }
972
Michael Jurkaa52570f2012-03-20 03:18:20 -0700973 public ShortcutAndWidgetContainer getShortcutsAndWidgets() {
Sunny Goyaldcbcc862014-08-12 15:58:36 -0700974 return mShortcutsAndWidgets;
Michael Jurkaa52570f2012-03-20 03:18:20 -0700975 }
976
Patrick Dubroy440c3602010-07-13 17:50:32 -0700977 public View getChildAt(int x, int y) {
Michael Jurkaa52570f2012-03-20 03:18:20 -0700978 return mShortcutsAndWidgets.getChildAt(x, y);
Patrick Dubroy440c3602010-07-13 17:50:32 -0700979 }
980
Adam Cohen76fc0852011-06-17 13:26:23 -0700981 public boolean animateChildToPosition(final View child, int cellX, int cellY, int duration,
Adam Cohen482ed822012-03-02 14:15:13 -0800982 int delay, boolean permanent, boolean adjustOccupied) {
Michael Jurkaa52570f2012-03-20 03:18:20 -0700983 ShortcutAndWidgetContainer clc = getShortcutsAndWidgets();
Adam Cohen482ed822012-03-02 14:15:13 -0800984 boolean[][] occupied = mOccupied;
985 if (!permanent) {
986 occupied = mTmpOccupied;
987 }
988
Adam Cohen19f37922012-03-21 11:59:11 -0700989 if (clc.indexOfChild(child) != -1) {
Adam Cohenbfbfd262011-06-13 16:55:12 -0700990 final LayoutParams lp = (LayoutParams) child.getLayoutParams();
991 final ItemInfo info = (ItemInfo) child.getTag();
992
993 // We cancel any existing animations
994 if (mReorderAnimators.containsKey(lp)) {
995 mReorderAnimators.get(lp).cancel();
996 mReorderAnimators.remove(lp);
997 }
998
Adam Cohen482ed822012-03-02 14:15:13 -0800999 final int oldX = lp.x;
1000 final int oldY = lp.y;
1001 if (adjustOccupied) {
1002 occupied[lp.cellX][lp.cellY] = false;
1003 occupied[cellX][cellY] = true;
1004 }
Adam Cohenbfbfd262011-06-13 16:55:12 -07001005 lp.isLockedToGrid = true;
Adam Cohen482ed822012-03-02 14:15:13 -08001006 if (permanent) {
1007 lp.cellX = info.cellX = cellX;
1008 lp.cellY = info.cellY = cellY;
1009 } else {
1010 lp.tmpCellX = cellX;
1011 lp.tmpCellY = cellY;
1012 }
Adam Cohenbfbfd262011-06-13 16:55:12 -07001013 clc.setupLp(lp);
1014 lp.isLockedToGrid = false;
Adam Cohen482ed822012-03-02 14:15:13 -08001015 final int newX = lp.x;
1016 final int newY = lp.y;
Adam Cohenbfbfd262011-06-13 16:55:12 -07001017
Adam Cohen76fc0852011-06-17 13:26:23 -07001018 lp.x = oldX;
1019 lp.y = oldY;
Adam Cohen76fc0852011-06-17 13:26:23 -07001020
Adam Cohen482ed822012-03-02 14:15:13 -08001021 // Exit early if we're not actually moving the view
1022 if (oldX == newX && oldY == newY) {
1023 lp.isLockedToGrid = true;
1024 return true;
1025 }
1026
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001027 ValueAnimator va = LauncherAnimUtils.ofFloat(child, 0f, 1f);
Adam Cohen482ed822012-03-02 14:15:13 -08001028 va.setDuration(duration);
1029 mReorderAnimators.put(lp, va);
1030
1031 va.addUpdateListener(new AnimatorUpdateListener() {
1032 @Override
Adam Cohenbfbfd262011-06-13 16:55:12 -07001033 public void onAnimationUpdate(ValueAnimator animation) {
Adam Cohen482ed822012-03-02 14:15:13 -08001034 float r = ((Float) animation.getAnimatedValue()).floatValue();
Adam Cohen19f37922012-03-21 11:59:11 -07001035 lp.x = (int) ((1 - r) * oldX + r * newX);
1036 lp.y = (int) ((1 - r) * oldY + r * newY);
Adam Cohen6b8a02d2012-03-22 15:13:40 -07001037 child.requestLayout();
Adam Cohenbfbfd262011-06-13 16:55:12 -07001038 }
1039 });
Adam Cohen482ed822012-03-02 14:15:13 -08001040 va.addListener(new AnimatorListenerAdapter() {
Adam Cohenbfbfd262011-06-13 16:55:12 -07001041 boolean cancelled = false;
1042 public void onAnimationEnd(Animator animation) {
1043 // If the animation was cancelled, it means that another animation
1044 // has interrupted this one, and we don't want to lock the item into
1045 // place just yet.
1046 if (!cancelled) {
1047 lp.isLockedToGrid = true;
Adam Cohen482ed822012-03-02 14:15:13 -08001048 child.requestLayout();
Adam Cohenbfbfd262011-06-13 16:55:12 -07001049 }
1050 if (mReorderAnimators.containsKey(lp)) {
1051 mReorderAnimators.remove(lp);
1052 }
1053 }
1054 public void onAnimationCancel(Animator animation) {
1055 cancelled = true;
1056 }
1057 });
Adam Cohen482ed822012-03-02 14:15:13 -08001058 va.setStartDelay(delay);
1059 va.start();
Adam Cohenbfbfd262011-06-13 16:55:12 -07001060 return true;
1061 }
1062 return false;
1063 }
1064
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001065 /**
1066 * Estimate where the top left cell of the dragged item will land if it is dropped.
1067 *
1068 * @param originX The X value of the top left corner of the item
1069 * @param originY The Y value of the top left corner of the item
1070 * @param spanX The number of horizontal cells that the item spans
1071 * @param spanY The number of vertical cells that the item spans
1072 * @param result The estimated drop cell X and Y.
1073 */
1074 void estimateDropCell(int originX, int originY, int spanX, int spanY, int[] result) {
Adam Cohend22015c2010-07-26 22:02:18 -07001075 final int countX = mCountX;
1076 final int countY = mCountY;
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001077
Michael Jurkaa63c4522010-08-19 13:52:27 -07001078 // pointToCellRounded takes the top left of a cell but will pad that with
1079 // cellWidth/2 and cellHeight/2 when finding the matching cell
1080 pointToCellRounded(originX, originY, result);
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001081
1082 // If the item isn't fully on this screen, snap to the edges
1083 int rightOverhang = result[0] + spanX - countX;
1084 if (rightOverhang > 0) {
1085 result[0] -= rightOverhang; // Snap to right
1086 }
1087 result[0] = Math.max(0, result[0]); // Snap to left
1088 int bottomOverhang = result[1] + spanY - countY;
1089 if (bottomOverhang > 0) {
1090 result[1] -= bottomOverhang; // Snap to bottom
1091 }
1092 result[1] = Math.max(0, result[1]); // Snap to top
1093 }
1094
Adam Cohen482ed822012-03-02 14:15:13 -08001095 void visualizeDropLocation(View v, Bitmap dragOutline, int originX, int originY, int cellX,
1096 int cellY, int spanX, int spanY, boolean resize, Point dragOffset, Rect dragRegion) {
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -07001097 final int oldDragCellX = mDragCell[0];
1098 final int oldDragCellY = mDragCell[1];
Adam Cohen482ed822012-03-02 14:15:13 -08001099
Adam Cohen2801caf2011-05-13 20:57:39 -07001100 if (dragOutline == null && v == null) {
Adam Cohen2801caf2011-05-13 20:57:39 -07001101 return;
1102 }
1103
Adam Cohen482ed822012-03-02 14:15:13 -08001104 if (cellX != oldDragCellX || cellY != oldDragCellY) {
1105 mDragCell[0] = cellX;
1106 mDragCell[1] = cellY;
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001107 // Find the top left corner of the rect the object will occupy
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001108 final int[] topLeft = mTmpPoint;
Adam Cohen482ed822012-03-02 14:15:13 -08001109 cellToPoint(cellX, cellY, topLeft);
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001110
Joe Onorato4be866d2010-10-10 11:26:02 -07001111 int left = topLeft[0];
1112 int top = topLeft[1];
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001113
Winson Chungb8c69f32011-10-19 21:36:08 -07001114 if (v != null && dragOffset == null) {
Adam Cohen99e8b402011-03-25 19:23:43 -07001115 // When drawing the drag outline, it did not account for margin offsets
1116 // added by the view's parent.
1117 MarginLayoutParams lp = (MarginLayoutParams) v.getLayoutParams();
1118 left += lp.leftMargin;
1119 top += lp.topMargin;
Winson Chung150fbab2010-09-29 17:14:26 -07001120
Adam Cohen99e8b402011-03-25 19:23:43 -07001121 // Offsets due to the size difference between the View and the dragOutline.
1122 // There is a size difference to account for the outer blur, which may lie
1123 // outside the bounds of the view.
Winson Chunga9abd0e2010-10-27 17:18:37 -07001124 top += (v.getHeight() - dragOutline.getHeight()) / 2;
Adam Cohenae915ce2011-08-25 13:47:22 -07001125 // We center about the x axis
1126 left += ((mCellWidth * spanX) + ((spanX - 1) * mWidthGap)
1127 - dragOutline.getWidth()) / 2;
Adam Cohen66396872011-04-15 17:50:36 -07001128 } else {
Winson Chungb8c69f32011-10-19 21:36:08 -07001129 if (dragOffset != null && dragRegion != null) {
1130 // Center the drag region *horizontally* in the cell and apply a drag
1131 // outline offset
1132 left += dragOffset.x + ((mCellWidth * spanX) + ((spanX - 1) * mWidthGap)
1133 - dragRegion.width()) / 2;
Winson Chung69737c32013-10-08 17:00:19 -07001134 int cHeight = getShortcutsAndWidgets().getCellContentHeight();
1135 int cellPaddingY = (int) Math.max(0, ((mCellHeight - cHeight) / 2f));
1136 top += dragOffset.y + cellPaddingY;
Winson Chungb8c69f32011-10-19 21:36:08 -07001137 } else {
1138 // Center the drag outline in the cell
1139 left += ((mCellWidth * spanX) + ((spanX - 1) * mWidthGap)
1140 - dragOutline.getWidth()) / 2;
1141 top += ((mCellHeight * spanY) + ((spanY - 1) * mHeightGap)
1142 - dragOutline.getHeight()) / 2;
1143 }
Winson Chunga9abd0e2010-10-27 17:18:37 -07001144 }
Joe Onorato4be866d2010-10-10 11:26:02 -07001145 final int oldIndex = mDragOutlineCurrent;
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -07001146 mDragOutlineAnims[oldIndex].animateOut();
1147 mDragOutlineCurrent = (oldIndex + 1) % mDragOutlines.length;
Adam Cohend41fbf52012-02-16 23:53:59 -08001148 Rect r = mDragOutlines[mDragOutlineCurrent];
1149 r.set(left, top, left + dragOutline.getWidth(), top + dragOutline.getHeight());
1150 if (resize) {
Adam Cohen482ed822012-03-02 14:15:13 -08001151 cellToRect(cellX, cellY, spanX, spanY, r);
Adam Cohend41fbf52012-02-16 23:53:59 -08001152 }
Winson Chung150fbab2010-09-29 17:14:26 -07001153
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -07001154 mDragOutlineAnims[mDragOutlineCurrent].setTag(dragOutline);
1155 mDragOutlineAnims[mDragOutlineCurrent].animateIn();
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001156 }
1157 }
1158
Adam Cohene0310962011-04-18 16:15:31 -07001159 public void clearDragOutlines() {
1160 final int oldIndex = mDragOutlineCurrent;
1161 mDragOutlineAnims[oldIndex].animateOut();
Adam Cohend41fbf52012-02-16 23:53:59 -08001162 mDragCell[0] = mDragCell[1] = -1;
Adam Cohene0310962011-04-18 16:15:31 -07001163 }
1164
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001165 /**
Jeff Sharkey70864282009-04-07 21:08:40 -07001166 * Find a vacant area that will fit the given bounds nearest the requested
1167 * cell location. Uses Euclidean distance to score multiple vacant areas.
Winson Chungaafa03c2010-06-11 17:34:16 -07001168 *
Romain Guy51afc022009-05-04 18:03:43 -07001169 * @param pixelX The X location at which you want to search for a vacant area.
1170 * @param pixelY The Y location at which you want to search for a vacant area.
Jeff Sharkey70864282009-04-07 21:08:40 -07001171 * @param spanX Horizontal span of the object.
1172 * @param spanY Vertical span of the object.
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001173 * @param result Array in which to place the result, or null (in which case a new array will
1174 * be allocated)
Jeff Sharkey70864282009-04-07 21:08:40 -07001175 * @return The X, Y cell of a vacant area that can contain this object,
1176 * nearest the requested location.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001177 */
Adam Cohend41fbf52012-02-16 23:53:59 -08001178 int[] findNearestVacantArea(int pixelX, int pixelY, int spanX, int spanY,
1179 int[] result) {
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001180 return findNearestVacantArea(pixelX, pixelY, spanX, spanY, null, result);
Michael Jurka6a1435d2010-09-27 17:35:12 -07001181 }
Winson Chungaafa03c2010-06-11 17:34:16 -07001182
Michael Jurka6a1435d2010-09-27 17:35:12 -07001183 /**
1184 * Find a vacant area that will fit the given bounds nearest the requested
1185 * cell location. Uses Euclidean distance to score multiple vacant areas.
1186 *
1187 * @param pixelX The X location at which you want to search for a vacant area.
1188 * @param pixelY The Y location at which you want to search for a vacant area.
Adam Cohend41fbf52012-02-16 23:53:59 -08001189 * @param minSpanX The minimum horizontal span required
1190 * @param minSpanY The minimum vertical span required
1191 * @param spanX Horizontal span of the object.
1192 * @param spanY Vertical span of the object.
1193 * @param result Array in which to place the result, or null (in which case a new array will
1194 * be allocated)
1195 * @return The X, Y cell of a vacant area that can contain this object,
1196 * nearest the requested location.
1197 */
1198 int[] findNearestVacantArea(int pixelX, int pixelY, int minSpanX, int minSpanY, int spanX,
1199 int spanY, int[] result, int[] resultSpan) {
1200 return findNearestVacantArea(pixelX, pixelY, minSpanX, minSpanY, spanX, spanY, null,
1201 result, resultSpan);
1202 }
1203
1204 /**
1205 * Find a vacant area that will fit the given bounds nearest the requested
1206 * cell location. Uses Euclidean distance to score multiple vacant areas.
1207 *
1208 * @param pixelX The X location at which you want to search for a vacant area.
1209 * @param pixelY The Y location at which you want to search for a vacant area.
Michael Jurka6a1435d2010-09-27 17:35:12 -07001210 * @param spanX Horizontal span of the object.
1211 * @param spanY Vertical span of the object.
Adam Cohendf035382011-04-11 17:22:04 -07001212 * @param ignoreOccupied If true, the result can be an occupied cell
1213 * @param result Array in which to place the result, or null (in which case a new array will
1214 * be allocated)
Michael Jurka6a1435d2010-09-27 17:35:12 -07001215 * @return The X, Y cell of a vacant area that can contain this object,
1216 * nearest the requested location.
1217 */
Adam Cohendf035382011-04-11 17:22:04 -07001218 int[] findNearestArea(int pixelX, int pixelY, int spanX, int spanY, View ignoreView,
1219 boolean ignoreOccupied, int[] result) {
Adam Cohend41fbf52012-02-16 23:53:59 -08001220 return findNearestArea(pixelX, pixelY, spanX, spanY,
Adam Cohen482ed822012-03-02 14:15:13 -08001221 spanX, spanY, ignoreView, ignoreOccupied, result, null, mOccupied);
Adam Cohend41fbf52012-02-16 23:53:59 -08001222 }
1223
1224 private final Stack<Rect> mTempRectStack = new Stack<Rect>();
1225 private void lazyInitTempRectStack() {
1226 if (mTempRectStack.isEmpty()) {
1227 for (int i = 0; i < mCountX * mCountY; i++) {
1228 mTempRectStack.push(new Rect());
1229 }
1230 }
1231 }
Adam Cohen482ed822012-03-02 14:15:13 -08001232
Adam Cohend41fbf52012-02-16 23:53:59 -08001233 private void recycleTempRects(Stack<Rect> used) {
1234 while (!used.isEmpty()) {
1235 mTempRectStack.push(used.pop());
1236 }
1237 }
1238
1239 /**
1240 * Find a vacant area that will fit the given bounds nearest the requested
1241 * cell location. Uses Euclidean distance to score multiple vacant areas.
1242 *
1243 * @param pixelX The X location at which you want to search for a vacant area.
1244 * @param pixelY The Y location at which you want to search for a vacant area.
1245 * @param minSpanX The minimum horizontal span required
1246 * @param minSpanY The minimum vertical span required
1247 * @param spanX Horizontal span of the object.
1248 * @param spanY Vertical span of the object.
1249 * @param ignoreOccupied If true, the result can be an occupied cell
1250 * @param result Array in which to place the result, or null (in which case a new array will
1251 * be allocated)
1252 * @return The X, Y cell of a vacant area that can contain this object,
1253 * nearest the requested location.
1254 */
1255 int[] findNearestArea(int pixelX, int pixelY, int minSpanX, int minSpanY, int spanX, int spanY,
Adam Cohen482ed822012-03-02 14:15:13 -08001256 View ignoreView, boolean ignoreOccupied, int[] result, int[] resultSpan,
1257 boolean[][] occupied) {
Adam Cohend41fbf52012-02-16 23:53:59 -08001258 lazyInitTempRectStack();
Michael Jurkac6ee42e2010-09-30 12:04:50 -07001259 // mark space take by ignoreView as available (method checks if ignoreView is null)
Adam Cohen482ed822012-03-02 14:15:13 -08001260 markCellsAsUnoccupiedForView(ignoreView, occupied);
Michael Jurkac6ee42e2010-09-30 12:04:50 -07001261
Adam Cohene3e27a82011-04-15 12:07:39 -07001262 // For items with a spanX / spanY > 1, the passed in point (pixelX, pixelY) corresponds
1263 // to the center of the item, but we are searching based on the top-left cell, so
1264 // we translate the point over to correspond to the top-left.
1265 pixelX -= (mCellWidth + mWidthGap) * (spanX - 1) / 2f;
1266 pixelY -= (mCellHeight + mHeightGap) * (spanY - 1) / 2f;
1267
Jeff Sharkey70864282009-04-07 21:08:40 -07001268 // Keep track of best-scoring drop area
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001269 final int[] bestXY = result != null ? result : new int[2];
Jeff Sharkey70864282009-04-07 21:08:40 -07001270 double bestDistance = Double.MAX_VALUE;
Adam Cohend41fbf52012-02-16 23:53:59 -08001271 final Rect bestRect = new Rect(-1, -1, -1, -1);
1272 final Stack<Rect> validRegions = new Stack<Rect>();
Winson Chungaafa03c2010-06-11 17:34:16 -07001273
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001274 final int countX = mCountX;
1275 final int countY = mCountY;
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001276
Adam Cohend41fbf52012-02-16 23:53:59 -08001277 if (minSpanX <= 0 || minSpanY <= 0 || spanX <= 0 || spanY <= 0 ||
1278 spanX < minSpanX || spanY < minSpanY) {
1279 return bestXY;
1280 }
1281
1282 for (int y = 0; y < countY - (minSpanY - 1); y++) {
Michael Jurkac28de512010-08-13 11:27:44 -07001283 inner:
Adam Cohend41fbf52012-02-16 23:53:59 -08001284 for (int x = 0; x < countX - (minSpanX - 1); x++) {
1285 int ySize = -1;
1286 int xSize = -1;
Adam Cohendf035382011-04-11 17:22:04 -07001287 if (ignoreOccupied) {
Adam Cohend41fbf52012-02-16 23:53:59 -08001288 // First, let's see if this thing fits anywhere
1289 for (int i = 0; i < minSpanX; i++) {
1290 for (int j = 0; j < minSpanY; j++) {
Adam Cohendf035382011-04-11 17:22:04 -07001291 if (occupied[x + i][y + j]) {
Adam Cohendf035382011-04-11 17:22:04 -07001292 continue inner;
1293 }
Michael Jurkac28de512010-08-13 11:27:44 -07001294 }
1295 }
Adam Cohend41fbf52012-02-16 23:53:59 -08001296 xSize = minSpanX;
1297 ySize = minSpanY;
1298
1299 // We know that the item will fit at _some_ acceptable size, now let's see
1300 // how big we can make it. We'll alternate between incrementing x and y spans
1301 // until we hit a limit.
1302 boolean incX = true;
1303 boolean hitMaxX = xSize >= spanX;
1304 boolean hitMaxY = ySize >= spanY;
1305 while (!(hitMaxX && hitMaxY)) {
1306 if (incX && !hitMaxX) {
1307 for (int j = 0; j < ySize; j++) {
1308 if (x + xSize > countX -1 || occupied[x + xSize][y + j]) {
1309 // We can't move out horizontally
1310 hitMaxX = true;
1311 }
1312 }
1313 if (!hitMaxX) {
1314 xSize++;
1315 }
1316 } else if (!hitMaxY) {
1317 for (int i = 0; i < xSize; i++) {
1318 if (y + ySize > countY - 1 || occupied[x + i][y + ySize]) {
1319 // We can't move out vertically
1320 hitMaxY = true;
1321 }
1322 }
1323 if (!hitMaxY) {
1324 ySize++;
1325 }
1326 }
1327 hitMaxX |= xSize >= spanX;
1328 hitMaxY |= ySize >= spanY;
1329 incX = !incX;
1330 }
1331 incX = true;
1332 hitMaxX = xSize >= spanX;
1333 hitMaxY = ySize >= spanY;
Michael Jurkac28de512010-08-13 11:27:44 -07001334 }
Winson Chung0be025d2011-05-23 17:45:09 -07001335 final int[] cellXY = mTmpXY;
Adam Cohene3e27a82011-04-15 12:07:39 -07001336 cellToCenterPoint(x, y, cellXY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001337
Adam Cohend41fbf52012-02-16 23:53:59 -08001338 // We verify that the current rect is not a sub-rect of any of our previous
1339 // candidates. In this case, the current rect is disqualified in favour of the
1340 // containing rect.
1341 Rect currentRect = mTempRectStack.pop();
1342 currentRect.set(x, y, x + xSize, y + ySize);
1343 boolean contained = false;
1344 for (Rect r : validRegions) {
1345 if (r.contains(currentRect)) {
1346 contained = true;
1347 break;
1348 }
1349 }
1350 validRegions.push(currentRect);
Michael Jurkac28de512010-08-13 11:27:44 -07001351 double distance = Math.sqrt(Math.pow(cellXY[0] - pixelX, 2)
1352 + Math.pow(cellXY[1] - pixelY, 2));
Adam Cohen482ed822012-03-02 14:15:13 -08001353
Adam Cohend41fbf52012-02-16 23:53:59 -08001354 if ((distance <= bestDistance && !contained) ||
1355 currentRect.contains(bestRect)) {
Michael Jurkac28de512010-08-13 11:27:44 -07001356 bestDistance = distance;
1357 bestXY[0] = x;
1358 bestXY[1] = y;
Adam Cohend41fbf52012-02-16 23:53:59 -08001359 if (resultSpan != null) {
1360 resultSpan[0] = xSize;
1361 resultSpan[1] = ySize;
1362 }
1363 bestRect.set(currentRect);
Michael Jurkac28de512010-08-13 11:27:44 -07001364 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001365 }
1366 }
Michael Jurkac6ee42e2010-09-30 12:04:50 -07001367 // re-mark space taken by ignoreView as occupied
Adam Cohen482ed822012-03-02 14:15:13 -08001368 markCellsAsOccupiedForView(ignoreView, occupied);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001369
Adam Cohenc0dcf592011-06-01 15:30:43 -07001370 // Return -1, -1 if no suitable location found
1371 if (bestDistance == Double.MAX_VALUE) {
1372 bestXY[0] = -1;
1373 bestXY[1] = -1;
Jeff Sharkey70864282009-04-07 21:08:40 -07001374 }
Adam Cohend41fbf52012-02-16 23:53:59 -08001375 recycleTempRects(validRegions);
Adam Cohenc0dcf592011-06-01 15:30:43 -07001376 return bestXY;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001377 }
Winson Chungaafa03c2010-06-11 17:34:16 -07001378
Adam Cohen482ed822012-03-02 14:15:13 -08001379 /**
1380 * Find a vacant area that will fit the given bounds nearest the requested
1381 * cell location, and will also weigh in a suggested direction vector of the
1382 * desired location. This method computers distance based on unit grid distances,
1383 * not pixel distances.
1384 *
Adam Cohen47a876d2012-03-19 13:21:41 -07001385 * @param cellX The X cell nearest to which you want to search for a vacant area.
1386 * @param cellY The Y cell nearest which you want to search for a vacant area.
Adam Cohen482ed822012-03-02 14:15:13 -08001387 * @param spanX Horizontal span of the object.
1388 * @param spanY Vertical span of the object.
Adam Cohen47a876d2012-03-19 13:21:41 -07001389 * @param direction The favored direction in which the views should move from x, y
1390 * @param exactDirectionOnly If this parameter is true, then only solutions where the direction
1391 * matches exactly. Otherwise we find the best matching direction.
1392 * @param occoupied The array which represents which cells in the CellLayout are occupied
1393 * @param blockOccupied The array which represents which cells in the specified block (cellX,
Winson Chung5f8afe62013-08-12 16:19:28 -07001394 * cellY, spanX, spanY) are occupied. This is used when try to move a group of views.
Adam Cohen482ed822012-03-02 14:15:13 -08001395 * @param result Array in which to place the result, or null (in which case a new array will
1396 * be allocated)
1397 * @return The X, Y cell of a vacant area that can contain this object,
1398 * nearest the requested location.
1399 */
1400 private int[] findNearestArea(int cellX, int cellY, int spanX, int spanY, int[] direction,
Adam Cohen47a876d2012-03-19 13:21:41 -07001401 boolean[][] occupied, boolean blockOccupied[][], int[] result) {
Adam Cohen482ed822012-03-02 14:15:13 -08001402 // Keep track of best-scoring drop area
1403 final int[] bestXY = result != null ? result : new int[2];
1404 float bestDistance = Float.MAX_VALUE;
1405 int bestDirectionScore = Integer.MIN_VALUE;
1406
1407 final int countX = mCountX;
1408 final int countY = mCountY;
1409
1410 for (int y = 0; y < countY - (spanY - 1); y++) {
1411 inner:
1412 for (int x = 0; x < countX - (spanX - 1); x++) {
1413 // First, let's see if this thing fits anywhere
1414 for (int i = 0; i < spanX; i++) {
1415 for (int j = 0; j < spanY; j++) {
Adam Cohen47a876d2012-03-19 13:21:41 -07001416 if (occupied[x + i][y + j] && (blockOccupied == null || blockOccupied[i][j])) {
Adam Cohen482ed822012-03-02 14:15:13 -08001417 continue inner;
1418 }
1419 }
1420 }
1421
1422 float distance = (float)
1423 Math.sqrt((x - cellX) * (x - cellX) + (y - cellY) * (y - cellY));
1424 int[] curDirection = mTmpPoint;
Adam Cohen47a876d2012-03-19 13:21:41 -07001425 computeDirectionVector(x - cellX, y - cellY, curDirection);
1426 // The direction score is just the dot product of the two candidate direction
1427 // and that passed in.
Adam Cohen482ed822012-03-02 14:15:13 -08001428 int curDirectionScore = direction[0] * curDirection[0] +
1429 direction[1] * curDirection[1];
Adam Cohen47a876d2012-03-19 13:21:41 -07001430 boolean exactDirectionOnly = false;
1431 boolean directionMatches = direction[0] == curDirection[0] &&
1432 direction[0] == curDirection[0];
1433 if ((directionMatches || !exactDirectionOnly) &&
1434 Float.compare(distance, bestDistance) < 0 || (Float.compare(distance,
Adam Cohen482ed822012-03-02 14:15:13 -08001435 bestDistance) == 0 && curDirectionScore > bestDirectionScore)) {
1436 bestDistance = distance;
1437 bestDirectionScore = curDirectionScore;
1438 bestXY[0] = x;
1439 bestXY[1] = y;
1440 }
1441 }
1442 }
1443
1444 // Return -1, -1 if no suitable location found
1445 if (bestDistance == Float.MAX_VALUE) {
1446 bestXY[0] = -1;
1447 bestXY[1] = -1;
1448 }
1449 return bestXY;
1450 }
1451
1452 private boolean addViewToTempLocation(View v, Rect rectOccupiedByPotentialDrop,
Adam Cohen8baab352012-03-20 17:39:21 -07001453 int[] direction, ItemConfiguration currentState) {
1454 CellAndSpan c = currentState.map.get(v);
Adam Cohen482ed822012-03-02 14:15:13 -08001455 boolean success = false;
Adam Cohen8baab352012-03-20 17:39:21 -07001456 markCellsForView(c.x, c.y, c.spanX, c.spanY, mTmpOccupied, false);
Adam Cohen482ed822012-03-02 14:15:13 -08001457 markCellsForRect(rectOccupiedByPotentialDrop, mTmpOccupied, true);
1458
Adam Cohen8baab352012-03-20 17:39:21 -07001459 findNearestArea(c.x, c.y, c.spanX, c.spanY, direction, mTmpOccupied, null, mTempLocation);
Adam Cohen482ed822012-03-02 14:15:13 -08001460
1461 if (mTempLocation[0] >= 0 && mTempLocation[1] >= 0) {
Adam Cohen8baab352012-03-20 17:39:21 -07001462 c.x = mTempLocation[0];
1463 c.y = mTempLocation[1];
Adam Cohen482ed822012-03-02 14:15:13 -08001464 success = true;
Adam Cohen482ed822012-03-02 14:15:13 -08001465 }
Adam Cohen8baab352012-03-20 17:39:21 -07001466 markCellsForView(c.x, c.y, c.spanX, c.spanY, mTmpOccupied, true);
Adam Cohen482ed822012-03-02 14:15:13 -08001467 return success;
1468 }
1469
Adam Cohenf3900c22012-11-16 18:28:11 -08001470 /**
1471 * This helper class defines a cluster of views. It helps with defining complex edges
1472 * of the cluster and determining how those edges interact with other views. The edges
1473 * essentially define a fine-grained boundary around the cluster of views -- like a more
1474 * precise version of a bounding box.
1475 */
1476 private class ViewCluster {
1477 final static int LEFT = 0;
1478 final static int TOP = 1;
1479 final static int RIGHT = 2;
1480 final static int BOTTOM = 3;
Adam Cohen47a876d2012-03-19 13:21:41 -07001481
Adam Cohenf3900c22012-11-16 18:28:11 -08001482 ArrayList<View> views;
1483 ItemConfiguration config;
1484 Rect boundingRect = new Rect();
Adam Cohen47a876d2012-03-19 13:21:41 -07001485
Adam Cohenf3900c22012-11-16 18:28:11 -08001486 int[] leftEdge = new int[mCountY];
1487 int[] rightEdge = new int[mCountY];
1488 int[] topEdge = new int[mCountX];
1489 int[] bottomEdge = new int[mCountX];
1490 boolean leftEdgeDirty, rightEdgeDirty, topEdgeDirty, bottomEdgeDirty, boundingRectDirty;
1491
1492 @SuppressWarnings("unchecked")
1493 public ViewCluster(ArrayList<View> views, ItemConfiguration config) {
1494 this.views = (ArrayList<View>) views.clone();
1495 this.config = config;
1496 resetEdges();
Adam Cohen47a876d2012-03-19 13:21:41 -07001497 }
1498
Adam Cohenf3900c22012-11-16 18:28:11 -08001499 void resetEdges() {
1500 for (int i = 0; i < mCountX; i++) {
1501 topEdge[i] = -1;
1502 bottomEdge[i] = -1;
1503 }
1504 for (int i = 0; i < mCountY; i++) {
1505 leftEdge[i] = -1;
1506 rightEdge[i] = -1;
1507 }
1508 leftEdgeDirty = true;
1509 rightEdgeDirty = true;
1510 bottomEdgeDirty = true;
1511 topEdgeDirty = true;
1512 boundingRectDirty = true;
1513 }
Adam Cohen47a876d2012-03-19 13:21:41 -07001514
Adam Cohenf3900c22012-11-16 18:28:11 -08001515 void computeEdge(int which, int[] edge) {
1516 int count = views.size();
1517 for (int i = 0; i < count; i++) {
1518 CellAndSpan cs = config.map.get(views.get(i));
1519 switch (which) {
1520 case LEFT:
1521 int left = cs.x;
1522 for (int j = cs.y; j < cs.y + cs.spanY; j++) {
1523 if (left < edge[j] || edge[j] < 0) {
1524 edge[j] = left;
Adam Cohena56dc102012-07-13 13:41:42 -07001525 }
Adam Cohen47a876d2012-03-19 13:21:41 -07001526 }
Adam Cohenf3900c22012-11-16 18:28:11 -08001527 break;
1528 case RIGHT:
1529 int right = cs.x + cs.spanX;
1530 for (int j = cs.y; j < cs.y + cs.spanY; j++) {
1531 if (right > edge[j]) {
1532 edge[j] = right;
1533 }
1534 }
1535 break;
1536 case TOP:
1537 int top = cs.y;
1538 for (int j = cs.x; j < cs.x + cs.spanX; j++) {
1539 if (top < edge[j] || edge[j] < 0) {
1540 edge[j] = top;
1541 }
1542 }
1543 break;
1544 case BOTTOM:
1545 int bottom = cs.y + cs.spanY;
1546 for (int j = cs.x; j < cs.x + cs.spanX; j++) {
1547 if (bottom > edge[j]) {
1548 edge[j] = bottom;
1549 }
1550 }
1551 break;
Adam Cohen47a876d2012-03-19 13:21:41 -07001552 }
1553 }
1554 }
Adam Cohenf3900c22012-11-16 18:28:11 -08001555
1556 boolean isViewTouchingEdge(View v, int whichEdge) {
1557 CellAndSpan cs = config.map.get(v);
1558
1559 int[] edge = getEdge(whichEdge);
1560
1561 switch (whichEdge) {
1562 case LEFT:
1563 for (int i = cs.y; i < cs.y + cs.spanY; i++) {
1564 if (edge[i] == cs.x + cs.spanX) {
1565 return true;
1566 }
1567 }
1568 break;
1569 case RIGHT:
1570 for (int i = cs.y; i < cs.y + cs.spanY; i++) {
1571 if (edge[i] == cs.x) {
1572 return true;
1573 }
1574 }
1575 break;
1576 case TOP:
1577 for (int i = cs.x; i < cs.x + cs.spanX; i++) {
1578 if (edge[i] == cs.y + cs.spanY) {
1579 return true;
1580 }
1581 }
1582 break;
1583 case BOTTOM:
1584 for (int i = cs.x; i < cs.x + cs.spanX; i++) {
1585 if (edge[i] == cs.y) {
1586 return true;
1587 }
1588 }
1589 break;
1590 }
1591 return false;
1592 }
1593
1594 void shift(int whichEdge, int delta) {
1595 for (View v: views) {
1596 CellAndSpan c = config.map.get(v);
1597 switch (whichEdge) {
1598 case LEFT:
1599 c.x -= delta;
1600 break;
1601 case RIGHT:
1602 c.x += delta;
1603 break;
1604 case TOP:
1605 c.y -= delta;
1606 break;
1607 case BOTTOM:
1608 default:
1609 c.y += delta;
1610 break;
1611 }
1612 }
1613 resetEdges();
1614 }
1615
1616 public void addView(View v) {
1617 views.add(v);
1618 resetEdges();
1619 }
1620
1621 public Rect getBoundingRect() {
1622 if (boundingRectDirty) {
1623 boolean first = true;
1624 for (View v: views) {
1625 CellAndSpan c = config.map.get(v);
1626 if (first) {
1627 boundingRect.set(c.x, c.y, c.x + c.spanX, c.y + c.spanY);
1628 first = false;
1629 } else {
1630 boundingRect.union(c.x, c.y, c.x + c.spanX, c.y + c.spanY);
1631 }
1632 }
1633 }
1634 return boundingRect;
1635 }
1636
1637 public int[] getEdge(int which) {
1638 switch (which) {
1639 case LEFT:
1640 return getLeftEdge();
1641 case RIGHT:
1642 return getRightEdge();
1643 case TOP:
1644 return getTopEdge();
1645 case BOTTOM:
1646 default:
1647 return getBottomEdge();
1648 }
1649 }
1650
1651 public int[] getLeftEdge() {
1652 if (leftEdgeDirty) {
1653 computeEdge(LEFT, leftEdge);
1654 }
1655 return leftEdge;
1656 }
1657
1658 public int[] getRightEdge() {
1659 if (rightEdgeDirty) {
1660 computeEdge(RIGHT, rightEdge);
1661 }
1662 return rightEdge;
1663 }
1664
1665 public int[] getTopEdge() {
1666 if (topEdgeDirty) {
1667 computeEdge(TOP, topEdge);
1668 }
1669 return topEdge;
1670 }
1671
1672 public int[] getBottomEdge() {
1673 if (bottomEdgeDirty) {
1674 computeEdge(BOTTOM, bottomEdge);
1675 }
1676 return bottomEdge;
1677 }
1678
1679 PositionComparator comparator = new PositionComparator();
1680 class PositionComparator implements Comparator<View> {
1681 int whichEdge = 0;
1682 public int compare(View left, View right) {
1683 CellAndSpan l = config.map.get(left);
1684 CellAndSpan r = config.map.get(right);
1685 switch (whichEdge) {
1686 case LEFT:
1687 return (r.x + r.spanX) - (l.x + l.spanX);
1688 case RIGHT:
1689 return l.x - r.x;
1690 case TOP:
1691 return (r.y + r.spanY) - (l.y + l.spanY);
1692 case BOTTOM:
1693 default:
1694 return l.y - r.y;
1695 }
1696 }
1697 }
1698
1699 public void sortConfigurationForEdgePush(int edge) {
1700 comparator.whichEdge = edge;
1701 Collections.sort(config.sortedViews, comparator);
1702 }
Adam Cohen47a876d2012-03-19 13:21:41 -07001703 }
1704
Adam Cohenf3900c22012-11-16 18:28:11 -08001705 private boolean pushViewsToTempLocation(ArrayList<View> views, Rect rectOccupiedByPotentialDrop,
1706 int[] direction, View dragView, ItemConfiguration currentState) {
Adam Cohene0489502012-08-27 15:18:53 -07001707
Adam Cohenf3900c22012-11-16 18:28:11 -08001708 ViewCluster cluster = new ViewCluster(views, currentState);
1709 Rect clusterRect = cluster.getBoundingRect();
1710 int whichEdge;
1711 int pushDistance;
1712 boolean fail = false;
1713
1714 // Determine the edge of the cluster that will be leading the push and how far
1715 // the cluster must be shifted.
1716 if (direction[0] < 0) {
1717 whichEdge = ViewCluster.LEFT;
1718 pushDistance = clusterRect.right - rectOccupiedByPotentialDrop.left;
Adam Cohene0489502012-08-27 15:18:53 -07001719 } else if (direction[0] > 0) {
Adam Cohenf3900c22012-11-16 18:28:11 -08001720 whichEdge = ViewCluster.RIGHT;
1721 pushDistance = rectOccupiedByPotentialDrop.right - clusterRect.left;
1722 } else if (direction[1] < 0) {
1723 whichEdge = ViewCluster.TOP;
1724 pushDistance = clusterRect.bottom - rectOccupiedByPotentialDrop.top;
1725 } else {
1726 whichEdge = ViewCluster.BOTTOM;
1727 pushDistance = rectOccupiedByPotentialDrop.bottom - clusterRect.top;
Adam Cohene0489502012-08-27 15:18:53 -07001728 }
1729
Adam Cohenf3900c22012-11-16 18:28:11 -08001730 // Break early for invalid push distance.
1731 if (pushDistance <= 0) {
1732 return false;
Adam Cohene0489502012-08-27 15:18:53 -07001733 }
Adam Cohenf3900c22012-11-16 18:28:11 -08001734
1735 // Mark the occupied state as false for the group of views we want to move.
1736 for (View v: views) {
1737 CellAndSpan c = currentState.map.get(v);
1738 markCellsForView(c.x, c.y, c.spanX, c.spanY, mTmpOccupied, false);
1739 }
1740
1741 // We save the current configuration -- if we fail to find a solution we will revert
1742 // to the initial state. The process of finding a solution modifies the configuration
1743 // in place, hence the need for revert in the failure case.
1744 currentState.save();
1745
1746 // The pushing algorithm is simplified by considering the views in the order in which
1747 // they would be pushed by the cluster. For example, if the cluster is leading with its
1748 // left edge, we consider sort the views by their right edge, from right to left.
1749 cluster.sortConfigurationForEdgePush(whichEdge);
1750
1751 while (pushDistance > 0 && !fail) {
1752 for (View v: currentState.sortedViews) {
1753 // For each view that isn't in the cluster, we see if the leading edge of the
1754 // cluster is contacting the edge of that view. If so, we add that view to the
1755 // cluster.
1756 if (!cluster.views.contains(v) && v != dragView) {
1757 if (cluster.isViewTouchingEdge(v, whichEdge)) {
1758 LayoutParams lp = (LayoutParams) v.getLayoutParams();
1759 if (!lp.canReorder) {
1760 // The push solution includes the all apps button, this is not viable.
1761 fail = true;
1762 break;
1763 }
1764 cluster.addView(v);
1765 CellAndSpan c = currentState.map.get(v);
1766
1767 // Adding view to cluster, mark it as not occupied.
1768 markCellsForView(c.x, c.y, c.spanX, c.spanY, mTmpOccupied, false);
1769 }
1770 }
1771 }
1772 pushDistance--;
1773
1774 // The cluster has been completed, now we move the whole thing over in the appropriate
1775 // direction.
1776 cluster.shift(whichEdge, 1);
1777 }
1778
1779 boolean foundSolution = false;
1780 clusterRect = cluster.getBoundingRect();
1781
1782 // Due to the nature of the algorithm, the only check required to verify a valid solution
1783 // is to ensure that completed shifted cluster lies completely within the cell layout.
1784 if (!fail && clusterRect.left >= 0 && clusterRect.right <= mCountX && clusterRect.top >= 0 &&
1785 clusterRect.bottom <= mCountY) {
1786 foundSolution = true;
1787 } else {
1788 currentState.restore();
1789 }
1790
1791 // In either case, we set the occupied array as marked for the location of the views
1792 for (View v: cluster.views) {
1793 CellAndSpan c = currentState.map.get(v);
1794 markCellsForView(c.x, c.y, c.spanX, c.spanY, mTmpOccupied, true);
1795 }
1796
1797 return foundSolution;
Adam Cohene0489502012-08-27 15:18:53 -07001798 }
1799
Adam Cohen482ed822012-03-02 14:15:13 -08001800 private boolean addViewsToTempLocation(ArrayList<View> views, Rect rectOccupiedByPotentialDrop,
Adam Cohenf3900c22012-11-16 18:28:11 -08001801 int[] direction, View dragView, ItemConfiguration currentState) {
Adam Cohen482ed822012-03-02 14:15:13 -08001802 if (views.size() == 0) return true;
Adam Cohen482ed822012-03-02 14:15:13 -08001803
Adam Cohen8baab352012-03-20 17:39:21 -07001804 boolean success = false;
Adam Cohen482ed822012-03-02 14:15:13 -08001805 Rect boundingRect = null;
Adam Cohen8baab352012-03-20 17:39:21 -07001806 // We construct a rect which represents the entire group of views passed in
Adam Cohen482ed822012-03-02 14:15:13 -08001807 for (View v: views) {
Adam Cohen8baab352012-03-20 17:39:21 -07001808 CellAndSpan c = currentState.map.get(v);
Adam Cohen482ed822012-03-02 14:15:13 -08001809 if (boundingRect == null) {
Adam Cohen8baab352012-03-20 17:39:21 -07001810 boundingRect = new Rect(c.x, c.y, c.x + c.spanX, c.y + c.spanY);
Adam Cohen482ed822012-03-02 14:15:13 -08001811 } else {
Adam Cohen8baab352012-03-20 17:39:21 -07001812 boundingRect.union(c.x, c.y, c.x + c.spanX, c.y + c.spanY);
Adam Cohen482ed822012-03-02 14:15:13 -08001813 }
1814 }
Adam Cohen8baab352012-03-20 17:39:21 -07001815
Adam Cohen8baab352012-03-20 17:39:21 -07001816 // Mark the occupied state as false for the group of views we want to move.
Adam Cohenf3900c22012-11-16 18:28:11 -08001817 for (View v: views) {
Adam Cohen8baab352012-03-20 17:39:21 -07001818 CellAndSpan c = currentState.map.get(v);
1819 markCellsForView(c.x, c.y, c.spanX, c.spanY, mTmpOccupied, false);
1820 }
1821
Adam Cohen47a876d2012-03-19 13:21:41 -07001822 boolean[][] blockOccupied = new boolean[boundingRect.width()][boundingRect.height()];
1823 int top = boundingRect.top;
1824 int left = boundingRect.left;
Adam Cohen8baab352012-03-20 17:39:21 -07001825 // We mark more precisely which parts of the bounding rect are truly occupied, allowing
Adam Cohena56dc102012-07-13 13:41:42 -07001826 // for interlocking.
Adam Cohenf3900c22012-11-16 18:28:11 -08001827 for (View v: views) {
Adam Cohen8baab352012-03-20 17:39:21 -07001828 CellAndSpan c = currentState.map.get(v);
1829 markCellsForView(c.x - left, c.y - top, c.spanX, c.spanY, blockOccupied, true);
Adam Cohen47a876d2012-03-19 13:21:41 -07001830 }
1831
Adam Cohen482ed822012-03-02 14:15:13 -08001832 markCellsForRect(rectOccupiedByPotentialDrop, mTmpOccupied, true);
1833
Adam Cohenf3900c22012-11-16 18:28:11 -08001834 findNearestArea(boundingRect.left, boundingRect.top, boundingRect.width(),
1835 boundingRect.height(), direction, mTmpOccupied, blockOccupied, mTempLocation);
Adam Cohen482ed822012-03-02 14:15:13 -08001836
Adam Cohen8baab352012-03-20 17:39:21 -07001837 // If we successfuly found a location by pushing the block of views, we commit it
Adam Cohen482ed822012-03-02 14:15:13 -08001838 if (mTempLocation[0] >= 0 && mTempLocation[1] >= 0) {
Adam Cohen8baab352012-03-20 17:39:21 -07001839 int deltaX = mTempLocation[0] - boundingRect.left;
1840 int deltaY = mTempLocation[1] - boundingRect.top;
Adam Cohenf3900c22012-11-16 18:28:11 -08001841 for (View v: views) {
Adam Cohen8baab352012-03-20 17:39:21 -07001842 CellAndSpan c = currentState.map.get(v);
1843 c.x += deltaX;
1844 c.y += deltaY;
Adam Cohen482ed822012-03-02 14:15:13 -08001845 }
1846 success = true;
1847 }
Adam Cohen8baab352012-03-20 17:39:21 -07001848
1849 // In either case, we set the occupied array as marked for the location of the views
Adam Cohenf3900c22012-11-16 18:28:11 -08001850 for (View v: views) {
Adam Cohen8baab352012-03-20 17:39:21 -07001851 CellAndSpan c = currentState.map.get(v);
1852 markCellsForView(c.x, c.y, c.spanX, c.spanY, mTmpOccupied, true);
Adam Cohen482ed822012-03-02 14:15:13 -08001853 }
1854 return success;
1855 }
1856
1857 private void markCellsForRect(Rect r, boolean[][] occupied, boolean value) {
1858 markCellsForView(r.left, r.top, r.width(), r.height(), occupied, value);
1859 }
1860
Adam Cohen4abc5bd2012-05-29 21:06:03 -07001861 // This method tries to find a reordering solution which satisfies the push mechanic by trying
1862 // to push items in each of the cardinal directions, in an order based on the direction vector
1863 // passed.
1864 private boolean attemptPushInDirection(ArrayList<View> intersectingViews, Rect occupied,
1865 int[] direction, View ignoreView, ItemConfiguration solution) {
1866 if ((Math.abs(direction[0]) + Math.abs(direction[1])) > 1) {
Winson Chung5f8afe62013-08-12 16:19:28 -07001867 // If the direction vector has two non-zero components, we try pushing
Adam Cohen4abc5bd2012-05-29 21:06:03 -07001868 // separately in each of the components.
1869 int temp = direction[1];
1870 direction[1] = 0;
Adam Cohenf3900c22012-11-16 18:28:11 -08001871
1872 if (pushViewsToTempLocation(intersectingViews, occupied, direction,
Adam Cohen4abc5bd2012-05-29 21:06:03 -07001873 ignoreView, solution)) {
1874 return true;
1875 }
1876 direction[1] = temp;
1877 temp = direction[0];
1878 direction[0] = 0;
Adam Cohenf3900c22012-11-16 18:28:11 -08001879
1880 if (pushViewsToTempLocation(intersectingViews, occupied, direction,
Adam Cohen4abc5bd2012-05-29 21:06:03 -07001881 ignoreView, solution)) {
1882 return true;
1883 }
1884 // Revert the direction
1885 direction[0] = temp;
1886
1887 // Now we try pushing in each component of the opposite direction
1888 direction[0] *= -1;
1889 direction[1] *= -1;
1890 temp = direction[1];
1891 direction[1] = 0;
Adam Cohenf3900c22012-11-16 18:28:11 -08001892 if (pushViewsToTempLocation(intersectingViews, occupied, direction,
Adam Cohen4abc5bd2012-05-29 21:06:03 -07001893 ignoreView, solution)) {
1894 return true;
1895 }
1896
1897 direction[1] = temp;
1898 temp = direction[0];
1899 direction[0] = 0;
Adam Cohenf3900c22012-11-16 18:28:11 -08001900 if (pushViewsToTempLocation(intersectingViews, occupied, direction,
Adam Cohen4abc5bd2012-05-29 21:06:03 -07001901 ignoreView, solution)) {
1902 return true;
1903 }
1904 // revert the direction
1905 direction[0] = temp;
1906 direction[0] *= -1;
1907 direction[1] *= -1;
Winson Chung5f8afe62013-08-12 16:19:28 -07001908
Adam Cohen4abc5bd2012-05-29 21:06:03 -07001909 } else {
1910 // If the direction vector has a single non-zero component, we push first in the
1911 // direction of the vector
Adam Cohenf3900c22012-11-16 18:28:11 -08001912 if (pushViewsToTempLocation(intersectingViews, occupied, direction,
Adam Cohen4abc5bd2012-05-29 21:06:03 -07001913 ignoreView, solution)) {
1914 return true;
1915 }
Adam Cohen4abc5bd2012-05-29 21:06:03 -07001916 // Then we try the opposite direction
1917 direction[0] *= -1;
1918 direction[1] *= -1;
Adam Cohenf3900c22012-11-16 18:28:11 -08001919 if (pushViewsToTempLocation(intersectingViews, occupied, direction,
Adam Cohen4abc5bd2012-05-29 21:06:03 -07001920 ignoreView, solution)) {
1921 return true;
1922 }
1923 // Switch the direction back
1924 direction[0] *= -1;
1925 direction[1] *= -1;
Winson Chung5f8afe62013-08-12 16:19:28 -07001926
1927 // If we have failed to find a push solution with the above, then we try
Adam Cohen4abc5bd2012-05-29 21:06:03 -07001928 // to find a solution by pushing along the perpendicular axis.
1929
1930 // Swap the components
1931 int temp = direction[1];
1932 direction[1] = direction[0];
1933 direction[0] = temp;
Adam Cohenf3900c22012-11-16 18:28:11 -08001934 if (pushViewsToTempLocation(intersectingViews, occupied, direction,
Adam Cohen4abc5bd2012-05-29 21:06:03 -07001935 ignoreView, solution)) {
1936 return true;
1937 }
1938
1939 // Then we try the opposite direction
1940 direction[0] *= -1;
1941 direction[1] *= -1;
Adam Cohenf3900c22012-11-16 18:28:11 -08001942 if (pushViewsToTempLocation(intersectingViews, occupied, direction,
Adam Cohen4abc5bd2012-05-29 21:06:03 -07001943 ignoreView, solution)) {
1944 return true;
1945 }
1946 // Switch the direction back
1947 direction[0] *= -1;
1948 direction[1] *= -1;
1949
1950 // Swap the components back
1951 temp = direction[1];
1952 direction[1] = direction[0];
1953 direction[0] = temp;
1954 }
1955 return false;
1956 }
1957
Adam Cohen482ed822012-03-02 14:15:13 -08001958 private boolean rearrangementExists(int cellX, int cellY, int spanX, int spanY, int[] direction,
Adam Cohen8baab352012-03-20 17:39:21 -07001959 View ignoreView, ItemConfiguration solution) {
Winson Chunge3e03bc2012-05-01 15:10:11 -07001960 // Return early if get invalid cell positions
1961 if (cellX < 0 || cellY < 0) return false;
Adam Cohen482ed822012-03-02 14:15:13 -08001962
Adam Cohen8baab352012-03-20 17:39:21 -07001963 mIntersectingViews.clear();
Adam Cohen482ed822012-03-02 14:15:13 -08001964 mOccupiedRect.set(cellX, cellY, cellX + spanX, cellY + spanY);
Adam Cohen482ed822012-03-02 14:15:13 -08001965
Adam Cohen8baab352012-03-20 17:39:21 -07001966 // Mark the desired location of the view currently being dragged.
Adam Cohen482ed822012-03-02 14:15:13 -08001967 if (ignoreView != null) {
Adam Cohen8baab352012-03-20 17:39:21 -07001968 CellAndSpan c = solution.map.get(ignoreView);
Adam Cohen19f37922012-03-21 11:59:11 -07001969 if (c != null) {
1970 c.x = cellX;
1971 c.y = cellY;
1972 }
Adam Cohen482ed822012-03-02 14:15:13 -08001973 }
Adam Cohen482ed822012-03-02 14:15:13 -08001974 Rect r0 = new Rect(cellX, cellY, cellX + spanX, cellY + spanY);
1975 Rect r1 = new Rect();
Adam Cohen8baab352012-03-20 17:39:21 -07001976 for (View child: solution.map.keySet()) {
Adam Cohen482ed822012-03-02 14:15:13 -08001977 if (child == ignoreView) continue;
Adam Cohen8baab352012-03-20 17:39:21 -07001978 CellAndSpan c = solution.map.get(child);
Adam Cohen482ed822012-03-02 14:15:13 -08001979 LayoutParams lp = (LayoutParams) child.getLayoutParams();
Adam Cohen8baab352012-03-20 17:39:21 -07001980 r1.set(c.x, c.y, c.x + c.spanX, c.y + c.spanY);
Adam Cohen482ed822012-03-02 14:15:13 -08001981 if (Rect.intersects(r0, r1)) {
1982 if (!lp.canReorder) {
1983 return false;
1984 }
1985 mIntersectingViews.add(child);
1986 }
1987 }
Adam Cohen47a876d2012-03-19 13:21:41 -07001988
Adam Cohenfa3c58f2013-12-06 16:10:55 -08001989 solution.intersectingViews = new ArrayList<View>(mIntersectingViews);
1990
Winson Chung5f8afe62013-08-12 16:19:28 -07001991 // First we try to find a solution which respects the push mechanic. That is,
Adam Cohen4abc5bd2012-05-29 21:06:03 -07001992 // we try to find a solution such that no displaced item travels through another item
1993 // without also displacing that item.
1994 if (attemptPushInDirection(mIntersectingViews, mOccupiedRect, direction, ignoreView,
Adam Cohen19f37922012-03-21 11:59:11 -07001995 solution)) {
Adam Cohen47a876d2012-03-19 13:21:41 -07001996 return true;
1997 }
Adam Cohen47a876d2012-03-19 13:21:41 -07001998
Adam Cohen4abc5bd2012-05-29 21:06:03 -07001999 // Next we try moving the views as a block, but without requiring the push mechanic.
Adam Cohenf3900c22012-11-16 18:28:11 -08002000 if (addViewsToTempLocation(mIntersectingViews, mOccupiedRect, direction, ignoreView,
Adam Cohen19f37922012-03-21 11:59:11 -07002001 solution)) {
Adam Cohen482ed822012-03-02 14:15:13 -08002002 return true;
2003 }
Adam Cohen47a876d2012-03-19 13:21:41 -07002004
Adam Cohen482ed822012-03-02 14:15:13 -08002005 // Ok, they couldn't move as a block, let's move them individually
2006 for (View v : mIntersectingViews) {
Adam Cohen8baab352012-03-20 17:39:21 -07002007 if (!addViewToTempLocation(v, mOccupiedRect, direction, solution)) {
Adam Cohen482ed822012-03-02 14:15:13 -08002008 return false;
2009 }
2010 }
2011 return true;
2012 }
2013
2014 /*
2015 * Returns a pair (x, y), where x,y are in {-1, 0, 1} corresponding to vector between
2016 * the provided point and the provided cell
2017 */
Adam Cohen47a876d2012-03-19 13:21:41 -07002018 private void computeDirectionVector(float deltaX, float deltaY, int[] result) {
Adam Cohen482ed822012-03-02 14:15:13 -08002019 double angle = Math.atan(((float) deltaY) / deltaX);
2020
2021 result[0] = 0;
2022 result[1] = 0;
2023 if (Math.abs(Math.cos(angle)) > 0.5f) {
2024 result[0] = (int) Math.signum(deltaX);
2025 }
2026 if (Math.abs(Math.sin(angle)) > 0.5f) {
2027 result[1] = (int) Math.signum(deltaY);
2028 }
2029 }
2030
Adam Cohen8baab352012-03-20 17:39:21 -07002031 private void copyOccupiedArray(boolean[][] occupied) {
2032 for (int i = 0; i < mCountX; i++) {
2033 for (int j = 0; j < mCountY; j++) {
2034 occupied[i][j] = mOccupied[i][j];
2035 }
2036 }
2037 }
2038
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002039 ItemConfiguration findReorderSolution(int pixelX, int pixelY, int minSpanX, int minSpanY,
2040 int spanX, int spanY, int[] direction, View dragView, boolean decX,
2041 ItemConfiguration solution) {
Adam Cohen8baab352012-03-20 17:39:21 -07002042 // Copy the current state into the solution. This solution will be manipulated as necessary.
2043 copyCurrentStateToSolution(solution, false);
2044 // Copy the current occupied array into the temporary occupied array. This array will be
2045 // manipulated as necessary to find a solution.
2046 copyOccupiedArray(mTmpOccupied);
Adam Cohen482ed822012-03-02 14:15:13 -08002047
2048 // We find the nearest cell into which we would place the dragged item, assuming there's
2049 // nothing in its way.
2050 int result[] = new int[2];
2051 result = findNearestArea(pixelX, pixelY, spanX, spanY, result);
2052
2053 boolean success = false;
2054 // First we try the exact nearest position of the item being dragged,
2055 // we will then want to try to move this around to other neighbouring positions
Adam Cohen8baab352012-03-20 17:39:21 -07002056 success = rearrangementExists(result[0], result[1], spanX, spanY, direction, dragView,
2057 solution);
Adam Cohen482ed822012-03-02 14:15:13 -08002058
2059 if (!success) {
2060 // We try shrinking the widget down to size in an alternating pattern, shrink 1 in
2061 // x, then 1 in y etc.
2062 if (spanX > minSpanX && (minSpanY == spanY || decX)) {
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002063 return findReorderSolution(pixelX, pixelY, minSpanX, minSpanY, spanX - 1, spanY,
2064 direction, dragView, false, solution);
Adam Cohen482ed822012-03-02 14:15:13 -08002065 } else if (spanY > minSpanY) {
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002066 return findReorderSolution(pixelX, pixelY, minSpanX, minSpanY, spanX, spanY - 1,
2067 direction, dragView, true, solution);
Adam Cohen482ed822012-03-02 14:15:13 -08002068 }
2069 solution.isSolution = false;
2070 } else {
2071 solution.isSolution = true;
2072 solution.dragViewX = result[0];
2073 solution.dragViewY = result[1];
2074 solution.dragViewSpanX = spanX;
2075 solution.dragViewSpanY = spanY;
Adam Cohen482ed822012-03-02 14:15:13 -08002076 }
2077 return solution;
2078 }
2079
2080 private void copyCurrentStateToSolution(ItemConfiguration solution, boolean temp) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07002081 int childCount = mShortcutsAndWidgets.getChildCount();
Adam Cohen482ed822012-03-02 14:15:13 -08002082 for (int i = 0; i < childCount; i++) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07002083 View child = mShortcutsAndWidgets.getChildAt(i);
Adam Cohen482ed822012-03-02 14:15:13 -08002084 LayoutParams lp = (LayoutParams) child.getLayoutParams();
Adam Cohen8baab352012-03-20 17:39:21 -07002085 CellAndSpan c;
Adam Cohen482ed822012-03-02 14:15:13 -08002086 if (temp) {
Adam Cohen8baab352012-03-20 17:39:21 -07002087 c = new CellAndSpan(lp.tmpCellX, lp.tmpCellY, lp.cellHSpan, lp.cellVSpan);
Adam Cohen482ed822012-03-02 14:15:13 -08002088 } else {
Adam Cohen8baab352012-03-20 17:39:21 -07002089 c = new CellAndSpan(lp.cellX, lp.cellY, lp.cellHSpan, lp.cellVSpan);
Adam Cohen482ed822012-03-02 14:15:13 -08002090 }
Adam Cohenf3900c22012-11-16 18:28:11 -08002091 solution.add(child, c);
Adam Cohen482ed822012-03-02 14:15:13 -08002092 }
2093 }
2094
2095 private void copySolutionToTempState(ItemConfiguration solution, View dragView) {
2096 for (int i = 0; i < mCountX; i++) {
2097 for (int j = 0; j < mCountY; j++) {
2098 mTmpOccupied[i][j] = false;
2099 }
2100 }
2101
Michael Jurkaa52570f2012-03-20 03:18:20 -07002102 int childCount = mShortcutsAndWidgets.getChildCount();
Adam Cohen482ed822012-03-02 14:15:13 -08002103 for (int i = 0; i < childCount; i++) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07002104 View child = mShortcutsAndWidgets.getChildAt(i);
Adam Cohen482ed822012-03-02 14:15:13 -08002105 if (child == dragView) continue;
2106 LayoutParams lp = (LayoutParams) child.getLayoutParams();
Adam Cohen8baab352012-03-20 17:39:21 -07002107 CellAndSpan c = solution.map.get(child);
2108 if (c != null) {
2109 lp.tmpCellX = c.x;
2110 lp.tmpCellY = c.y;
2111 lp.cellHSpan = c.spanX;
2112 lp.cellVSpan = c.spanY;
2113 markCellsForView(c.x, c.y, c.spanX, c.spanY, mTmpOccupied, true);
Adam Cohen482ed822012-03-02 14:15:13 -08002114 }
2115 }
2116 markCellsForView(solution.dragViewX, solution.dragViewY, solution.dragViewSpanX,
2117 solution.dragViewSpanY, mTmpOccupied, true);
2118 }
2119
2120 private void animateItemsToSolution(ItemConfiguration solution, View dragView, boolean
2121 commitDragView) {
2122
2123 boolean[][] occupied = DESTRUCTIVE_REORDER ? mOccupied : mTmpOccupied;
2124 for (int i = 0; i < mCountX; i++) {
2125 for (int j = 0; j < mCountY; j++) {
2126 occupied[i][j] = false;
2127 }
2128 }
2129
Michael Jurkaa52570f2012-03-20 03:18:20 -07002130 int childCount = mShortcutsAndWidgets.getChildCount();
Adam Cohen482ed822012-03-02 14:15:13 -08002131 for (int i = 0; i < childCount; i++) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07002132 View child = mShortcutsAndWidgets.getChildAt(i);
Adam Cohen482ed822012-03-02 14:15:13 -08002133 if (child == dragView) continue;
Adam Cohen8baab352012-03-20 17:39:21 -07002134 CellAndSpan c = solution.map.get(child);
2135 if (c != null) {
Adam Cohen19f37922012-03-21 11:59:11 -07002136 animateChildToPosition(child, c.x, c.y, REORDER_ANIMATION_DURATION, 0,
2137 DESTRUCTIVE_REORDER, false);
Adam Cohen8baab352012-03-20 17:39:21 -07002138 markCellsForView(c.x, c.y, c.spanX, c.spanY, occupied, true);
Adam Cohen482ed822012-03-02 14:15:13 -08002139 }
2140 }
2141 if (commitDragView) {
2142 markCellsForView(solution.dragViewX, solution.dragViewY, solution.dragViewSpanX,
2143 solution.dragViewSpanY, occupied, true);
2144 }
2145 }
2146
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002147
2148 // This method starts or changes the reorder preview animations
2149 private void beginOrAdjustReorderPreviewAnimations(ItemConfiguration solution,
2150 View dragView, int delay, int mode) {
Adam Cohen19f37922012-03-21 11:59:11 -07002151 int childCount = mShortcutsAndWidgets.getChildCount();
Adam Cohen19f37922012-03-21 11:59:11 -07002152 for (int i = 0; i < childCount; i++) {
2153 View child = mShortcutsAndWidgets.getChildAt(i);
2154 if (child == dragView) continue;
2155 CellAndSpan c = solution.map.get(child);
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002156 boolean skip = mode == ReorderPreviewAnimation.MODE_HINT && solution.intersectingViews
2157 != null && !solution.intersectingViews.contains(child);
2158
Adam Cohen19f37922012-03-21 11:59:11 -07002159 LayoutParams lp = (LayoutParams) child.getLayoutParams();
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002160 if (c != null && !skip) {
2161 ReorderPreviewAnimation rha = new ReorderPreviewAnimation(child, mode, lp.cellX,
2162 lp.cellY, c.x, c.y, c.spanX, c.spanY);
Adam Cohend024f982012-05-23 18:26:45 -07002163 rha.animate();
Adam Cohen19f37922012-03-21 11:59:11 -07002164 }
2165 }
2166 }
2167
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002168 // Class which represents the reorder preview animations. These animations show that an item is
Adam Cohen19f37922012-03-21 11:59:11 -07002169 // in a temporary state, and hint at where the item will return to.
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002170 class ReorderPreviewAnimation {
Adam Cohen19f37922012-03-21 11:59:11 -07002171 View child;
Adam Cohend024f982012-05-23 18:26:45 -07002172 float finalDeltaX;
2173 float finalDeltaY;
2174 float initDeltaX;
2175 float initDeltaY;
2176 float finalScale;
2177 float initScale;
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002178 int mode;
2179 boolean repeating = false;
2180 private static final int PREVIEW_DURATION = 300;
2181 private static final int HINT_DURATION = Workspace.REORDER_TIMEOUT;
2182
2183 public static final int MODE_HINT = 0;
2184 public static final int MODE_PREVIEW = 1;
2185
Adam Cohene7587d22012-05-24 18:50:02 -07002186 Animator a;
Adam Cohen19f37922012-03-21 11:59:11 -07002187
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002188 public ReorderPreviewAnimation(View child, int mode, int cellX0, int cellY0, int cellX1,
2189 int cellY1, int spanX, int spanY) {
Adam Cohen19f37922012-03-21 11:59:11 -07002190 regionToCenterPoint(cellX0, cellY0, spanX, spanY, mTmpPoint);
2191 final int x0 = mTmpPoint[0];
2192 final int y0 = mTmpPoint[1];
2193 regionToCenterPoint(cellX1, cellY1, spanX, spanY, mTmpPoint);
2194 final int x1 = mTmpPoint[0];
2195 final int y1 = mTmpPoint[1];
2196 final int dX = x1 - x0;
2197 final int dY = y1 - y0;
Adam Cohend024f982012-05-23 18:26:45 -07002198 finalDeltaX = 0;
2199 finalDeltaY = 0;
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002200 int dir = mode == MODE_HINT ? -1 : 1;
Adam Cohen19f37922012-03-21 11:59:11 -07002201 if (dX == dY && dX == 0) {
2202 } else {
2203 if (dY == 0) {
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002204 finalDeltaX = - dir * Math.signum(dX) * mReorderPreviewAnimationMagnitude;
Adam Cohen19f37922012-03-21 11:59:11 -07002205 } else if (dX == 0) {
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002206 finalDeltaY = - dir * Math.signum(dY) * mReorderPreviewAnimationMagnitude;
Adam Cohen19f37922012-03-21 11:59:11 -07002207 } else {
2208 double angle = Math.atan( (float) (dY) / dX);
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002209 finalDeltaX = (int) (- dir * Math.signum(dX) *
2210 Math.abs(Math.cos(angle) * mReorderPreviewAnimationMagnitude));
2211 finalDeltaY = (int) (- dir * Math.signum(dY) *
2212 Math.abs(Math.sin(angle) * mReorderPreviewAnimationMagnitude));
Adam Cohen19f37922012-03-21 11:59:11 -07002213 }
2214 }
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002215 this.mode = mode;
Adam Cohend024f982012-05-23 18:26:45 -07002216 initDeltaX = child.getTranslationX();
2217 initDeltaY = child.getTranslationY();
Adam Cohen307fe232012-08-16 17:55:58 -07002218 finalScale = getChildrenScale() - 4.0f / child.getWidth();
Adam Cohend024f982012-05-23 18:26:45 -07002219 initScale = child.getScaleX();
Adam Cohen19f37922012-03-21 11:59:11 -07002220 this.child = child;
2221 }
2222
Adam Cohend024f982012-05-23 18:26:45 -07002223 void animate() {
Adam Cohen19f37922012-03-21 11:59:11 -07002224 if (mShakeAnimators.containsKey(child)) {
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002225 ReorderPreviewAnimation oldAnimation = mShakeAnimators.get(child);
Adam Cohend024f982012-05-23 18:26:45 -07002226 oldAnimation.cancel();
Adam Cohen19f37922012-03-21 11:59:11 -07002227 mShakeAnimators.remove(child);
Adam Cohene7587d22012-05-24 18:50:02 -07002228 if (finalDeltaX == 0 && finalDeltaY == 0) {
2229 completeAnimationImmediately();
2230 return;
2231 }
Adam Cohen19f37922012-03-21 11:59:11 -07002232 }
Adam Cohend024f982012-05-23 18:26:45 -07002233 if (finalDeltaX == 0 && finalDeltaY == 0) {
Adam Cohen19f37922012-03-21 11:59:11 -07002234 return;
2235 }
Michael Jurkaf1ad6082013-03-13 12:55:46 +01002236 ValueAnimator va = LauncherAnimUtils.ofFloat(child, 0f, 1f);
Adam Cohene7587d22012-05-24 18:50:02 -07002237 a = va;
Adam Cohen19f37922012-03-21 11:59:11 -07002238 va.setRepeatMode(ValueAnimator.REVERSE);
2239 va.setRepeatCount(ValueAnimator.INFINITE);
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002240 va.setDuration(mode == MODE_HINT ? HINT_DURATION : PREVIEW_DURATION);
Adam Cohend024f982012-05-23 18:26:45 -07002241 va.setStartDelay((int) (Math.random() * 60));
Adam Cohen19f37922012-03-21 11:59:11 -07002242 va.addUpdateListener(new AnimatorUpdateListener() {
2243 @Override
2244 public void onAnimationUpdate(ValueAnimator animation) {
2245 float r = ((Float) animation.getAnimatedValue()).floatValue();
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002246 float r1 = (mode == MODE_HINT && repeating) ? 1.0f : r;
2247 float x = r1 * finalDeltaX + (1 - r1) * initDeltaX;
2248 float y = r1 * finalDeltaY + (1 - r1) * initDeltaY;
Adam Cohen19f37922012-03-21 11:59:11 -07002249 child.setTranslationX(x);
2250 child.setTranslationY(y);
Adam Cohend024f982012-05-23 18:26:45 -07002251 float s = r * finalScale + (1 - r) * initScale;
Brandon Keely50e6e562012-05-08 16:28:49 -07002252 child.setScaleX(s);
2253 child.setScaleY(s);
Adam Cohen19f37922012-03-21 11:59:11 -07002254 }
2255 });
2256 va.addListener(new AnimatorListenerAdapter() {
2257 public void onAnimationRepeat(Animator animation) {
Adam Cohen19f37922012-03-21 11:59:11 -07002258 // We make sure to end only after a full period
Adam Cohend024f982012-05-23 18:26:45 -07002259 initDeltaX = 0;
2260 initDeltaY = 0;
Adam Cohen307fe232012-08-16 17:55:58 -07002261 initScale = getChildrenScale();
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002262 repeating = true;
Adam Cohen19f37922012-03-21 11:59:11 -07002263 }
2264 });
Adam Cohen19f37922012-03-21 11:59:11 -07002265 mShakeAnimators.put(child, this);
2266 va.start();
2267 }
2268
Adam Cohend024f982012-05-23 18:26:45 -07002269 private void cancel() {
Adam Cohene7587d22012-05-24 18:50:02 -07002270 if (a != null) {
2271 a.cancel();
2272 }
Adam Cohen19f37922012-03-21 11:59:11 -07002273 }
Adam Cohene7587d22012-05-24 18:50:02 -07002274
Brandon Keely50e6e562012-05-08 16:28:49 -07002275 private void completeAnimationImmediately() {
Adam Cohene7587d22012-05-24 18:50:02 -07002276 if (a != null) {
2277 a.cancel();
2278 }
Brandon Keely50e6e562012-05-08 16:28:49 -07002279
Michael Jurka2ecf9952012-06-18 12:52:28 -07002280 AnimatorSet s = LauncherAnimUtils.createAnimatorSet();
Adam Cohene7587d22012-05-24 18:50:02 -07002281 a = s;
Brandon Keely50e6e562012-05-08 16:28:49 -07002282 s.playTogether(
Adam Cohen307fe232012-08-16 17:55:58 -07002283 LauncherAnimUtils.ofFloat(child, "scaleX", getChildrenScale()),
2284 LauncherAnimUtils.ofFloat(child, "scaleY", getChildrenScale()),
Michael Jurka2ecf9952012-06-18 12:52:28 -07002285 LauncherAnimUtils.ofFloat(child, "translationX", 0f),
2286 LauncherAnimUtils.ofFloat(child, "translationY", 0f)
Brandon Keely50e6e562012-05-08 16:28:49 -07002287 );
2288 s.setDuration(REORDER_ANIMATION_DURATION);
2289 s.setInterpolator(new android.view.animation.DecelerateInterpolator(1.5f));
2290 s.start();
2291 }
Adam Cohen19f37922012-03-21 11:59:11 -07002292 }
2293
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002294 private void completeAndClearReorderPreviewAnimations() {
2295 for (ReorderPreviewAnimation a: mShakeAnimators.values()) {
Brandon Keely50e6e562012-05-08 16:28:49 -07002296 a.completeAnimationImmediately();
Adam Cohen19f37922012-03-21 11:59:11 -07002297 }
2298 mShakeAnimators.clear();
2299 }
2300
Adam Cohen482ed822012-03-02 14:15:13 -08002301 private void commitTempPlacement() {
2302 for (int i = 0; i < mCountX; i++) {
2303 for (int j = 0; j < mCountY; j++) {
2304 mOccupied[i][j] = mTmpOccupied[i][j];
2305 }
2306 }
Michael Jurkaa52570f2012-03-20 03:18:20 -07002307 int childCount = mShortcutsAndWidgets.getChildCount();
Adam Cohen482ed822012-03-02 14:15:13 -08002308 for (int i = 0; i < childCount; i++) {
Adam Cohenea889a22012-03-27 16:45:39 -07002309 View child = mShortcutsAndWidgets.getChildAt(i);
2310 LayoutParams lp = (LayoutParams) child.getLayoutParams();
2311 ItemInfo info = (ItemInfo) child.getTag();
Adam Cohen2acce882012-03-28 19:03:19 -07002312 // We do a null check here because the item info can be null in the case of the
2313 // AllApps button in the hotseat.
2314 if (info != null) {
Adam Cohen487f7dd2012-06-28 18:12:10 -07002315 if (info.cellX != lp.tmpCellX || info.cellY != lp.tmpCellY ||
2316 info.spanX != lp.cellHSpan || info.spanY != lp.cellVSpan) {
2317 info.requiresDbUpdate = true;
2318 }
Adam Cohen2acce882012-03-28 19:03:19 -07002319 info.cellX = lp.cellX = lp.tmpCellX;
2320 info.cellY = lp.cellY = lp.tmpCellY;
Adam Cohenbebf0422012-04-11 18:06:28 -07002321 info.spanX = lp.cellHSpan;
2322 info.spanY = lp.cellVSpan;
Adam Cohen2acce882012-03-28 19:03:19 -07002323 }
Adam Cohen482ed822012-03-02 14:15:13 -08002324 }
Adam Cohen2acce882012-03-28 19:03:19 -07002325 mLauncher.getWorkspace().updateItemLocationsInDatabase(this);
Adam Cohen482ed822012-03-02 14:15:13 -08002326 }
2327
2328 public void setUseTempCoords(boolean useTempCoords) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07002329 int childCount = mShortcutsAndWidgets.getChildCount();
Adam Cohen482ed822012-03-02 14:15:13 -08002330 for (int i = 0; i < childCount; i++) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07002331 LayoutParams lp = (LayoutParams) mShortcutsAndWidgets.getChildAt(i).getLayoutParams();
Adam Cohen482ed822012-03-02 14:15:13 -08002332 lp.useTmpCoords = useTempCoords;
2333 }
2334 }
2335
Adam Cohen482ed822012-03-02 14:15:13 -08002336 ItemConfiguration findConfigurationNoShuffle(int pixelX, int pixelY, int minSpanX, int minSpanY,
2337 int spanX, int spanY, View dragView, ItemConfiguration solution) {
2338 int[] result = new int[2];
2339 int[] resultSpan = new int[2];
2340 findNearestVacantArea(pixelX, pixelY, minSpanX, minSpanY, spanX, spanY, null, result,
2341 resultSpan);
2342 if (result[0] >= 0 && result[1] >= 0) {
2343 copyCurrentStateToSolution(solution, false);
2344 solution.dragViewX = result[0];
2345 solution.dragViewY = result[1];
2346 solution.dragViewSpanX = resultSpan[0];
2347 solution.dragViewSpanY = resultSpan[1];
2348 solution.isSolution = true;
2349 } else {
2350 solution.isSolution = false;
2351 }
2352 return solution;
2353 }
2354
2355 public void prepareChildForDrag(View child) {
2356 markCellsAsUnoccupiedForView(child);
Adam Cohen482ed822012-03-02 14:15:13 -08002357 }
2358
Adam Cohen19f37922012-03-21 11:59:11 -07002359 /* This seems like it should be obvious and straight-forward, but when the direction vector
2360 needs to match with the notion of the dragView pushing other views, we have to employ
2361 a slightly more subtle notion of the direction vector. The question is what two points is
2362 the vector between? The center of the dragView and its desired destination? Not quite, as
2363 this doesn't necessarily coincide with the interaction of the dragView and items occupying
2364 those cells. Instead we use some heuristics to often lock the vector to up, down, left
2365 or right, which helps make pushing feel right.
2366 */
2367 private void getDirectionVectorForDrop(int dragViewCenterX, int dragViewCenterY, int spanX,
2368 int spanY, View dragView, int[] resultDirection) {
2369 int[] targetDestination = new int[2];
2370
2371 findNearestArea(dragViewCenterX, dragViewCenterY, spanX, spanY, targetDestination);
2372 Rect dragRect = new Rect();
2373 regionToRect(targetDestination[0], targetDestination[1], spanX, spanY, dragRect);
2374 dragRect.offset(dragViewCenterX - dragRect.centerX(), dragViewCenterY - dragRect.centerY());
2375
2376 Rect dropRegionRect = new Rect();
2377 getViewsIntersectingRegion(targetDestination[0], targetDestination[1], spanX, spanY,
2378 dragView, dropRegionRect, mIntersectingViews);
2379
2380 int dropRegionSpanX = dropRegionRect.width();
2381 int dropRegionSpanY = dropRegionRect.height();
2382
2383 regionToRect(dropRegionRect.left, dropRegionRect.top, dropRegionRect.width(),
2384 dropRegionRect.height(), dropRegionRect);
2385
2386 int deltaX = (dropRegionRect.centerX() - dragViewCenterX) / spanX;
2387 int deltaY = (dropRegionRect.centerY() - dragViewCenterY) / spanY;
2388
2389 if (dropRegionSpanX == mCountX || spanX == mCountX) {
2390 deltaX = 0;
2391 }
2392 if (dropRegionSpanY == mCountY || spanY == mCountY) {
2393 deltaY = 0;
2394 }
2395
2396 if (deltaX == 0 && deltaY == 0) {
2397 // No idea what to do, give a random direction.
2398 resultDirection[0] = 1;
2399 resultDirection[1] = 0;
2400 } else {
2401 computeDirectionVector(deltaX, deltaY, resultDirection);
2402 }
2403 }
2404
2405 // For a given cell and span, fetch the set of views intersecting the region.
2406 private void getViewsIntersectingRegion(int cellX, int cellY, int spanX, int spanY,
2407 View dragView, Rect boundingRect, ArrayList<View> intersectingViews) {
2408 if (boundingRect != null) {
2409 boundingRect.set(cellX, cellY, cellX + spanX, cellY + spanY);
2410 }
2411 intersectingViews.clear();
2412 Rect r0 = new Rect(cellX, cellY, cellX + spanX, cellY + spanY);
2413 Rect r1 = new Rect();
2414 final int count = mShortcutsAndWidgets.getChildCount();
2415 for (int i = 0; i < count; i++) {
2416 View child = mShortcutsAndWidgets.getChildAt(i);
2417 if (child == dragView) continue;
2418 LayoutParams lp = (LayoutParams) child.getLayoutParams();
2419 r1.set(lp.cellX, lp.cellY, lp.cellX + lp.cellHSpan, lp.cellY + lp.cellVSpan);
2420 if (Rect.intersects(r0, r1)) {
2421 mIntersectingViews.add(child);
2422 if (boundingRect != null) {
2423 boundingRect.union(r1);
2424 }
2425 }
2426 }
2427 }
2428
2429 boolean isNearestDropLocationOccupied(int pixelX, int pixelY, int spanX, int spanY,
2430 View dragView, int[] result) {
2431 result = findNearestArea(pixelX, pixelY, spanX, spanY, result);
2432 getViewsIntersectingRegion(result[0], result[1], spanX, spanY, dragView, null,
2433 mIntersectingViews);
2434 return !mIntersectingViews.isEmpty();
2435 }
2436
2437 void revertTempState() {
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002438 completeAndClearReorderPreviewAnimations();
2439 if (isItemPlacementDirty() && !DESTRUCTIVE_REORDER) {
2440 final int count = mShortcutsAndWidgets.getChildCount();
2441 for (int i = 0; i < count; i++) {
2442 View child = mShortcutsAndWidgets.getChildAt(i);
2443 LayoutParams lp = (LayoutParams) child.getLayoutParams();
2444 if (lp.tmpCellX != lp.cellX || lp.tmpCellY != lp.cellY) {
2445 lp.tmpCellX = lp.cellX;
2446 lp.tmpCellY = lp.cellY;
2447 animateChildToPosition(child, lp.cellX, lp.cellY, REORDER_ANIMATION_DURATION,
2448 0, false, false);
2449 }
Adam Cohen19f37922012-03-21 11:59:11 -07002450 }
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002451 setItemPlacementDirty(false);
Adam Cohen19f37922012-03-21 11:59:11 -07002452 }
Adam Cohen19f37922012-03-21 11:59:11 -07002453 }
2454
Adam Cohenbebf0422012-04-11 18:06:28 -07002455 boolean createAreaForResize(int cellX, int cellY, int spanX, int spanY,
2456 View dragView, int[] direction, boolean commit) {
2457 int[] pixelXY = new int[2];
2458 regionToCenterPoint(cellX, cellY, spanX, spanY, pixelXY);
2459
2460 // First we determine if things have moved enough to cause a different layout
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002461 ItemConfiguration swapSolution = findReorderSolution(pixelXY[0], pixelXY[1], spanX, spanY,
Adam Cohenbebf0422012-04-11 18:06:28 -07002462 spanX, spanY, direction, dragView, true, new ItemConfiguration());
2463
2464 setUseTempCoords(true);
2465 if (swapSolution != null && swapSolution.isSolution) {
2466 // If we're just testing for a possible location (MODE_ACCEPT_DROP), we don't bother
2467 // committing anything or animating anything as we just want to determine if a solution
2468 // exists
2469 copySolutionToTempState(swapSolution, dragView);
2470 setItemPlacementDirty(true);
2471 animateItemsToSolution(swapSolution, dragView, commit);
2472
2473 if (commit) {
2474 commitTempPlacement();
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002475 completeAndClearReorderPreviewAnimations();
Adam Cohenbebf0422012-04-11 18:06:28 -07002476 setItemPlacementDirty(false);
2477 } else {
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002478 beginOrAdjustReorderPreviewAnimations(swapSolution, dragView,
2479 REORDER_ANIMATION_DURATION, ReorderPreviewAnimation.MODE_PREVIEW);
Adam Cohenbebf0422012-04-11 18:06:28 -07002480 }
2481 mShortcutsAndWidgets.requestLayout();
2482 }
2483 return swapSolution.isSolution;
2484 }
2485
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002486 int[] performReorder(int pixelX, int pixelY, int minSpanX, int minSpanY, int spanX, int spanY,
Adam Cohen482ed822012-03-02 14:15:13 -08002487 View dragView, int[] result, int resultSpan[], int mode) {
Adam Cohen482ed822012-03-02 14:15:13 -08002488 // First we determine if things have moved enough to cause a different layout
Adam Cohen47a876d2012-03-19 13:21:41 -07002489 result = findNearestArea(pixelX, pixelY, spanX, spanY, result);
Adam Cohen482ed822012-03-02 14:15:13 -08002490
2491 if (resultSpan == null) {
2492 resultSpan = new int[2];
2493 }
2494
Adam Cohen19f37922012-03-21 11:59:11 -07002495 // When we are checking drop validity or actually dropping, we don't recompute the
2496 // direction vector, since we want the solution to match the preview, and it's possible
2497 // that the exact position of the item has changed to result in a new reordering outcome.
Adam Cohenb209e632012-03-27 17:09:36 -07002498 if ((mode == MODE_ON_DROP || mode == MODE_ON_DROP_EXTERNAL || mode == MODE_ACCEPT_DROP)
2499 && mPreviousReorderDirection[0] != INVALID_DIRECTION) {
Adam Cohen19f37922012-03-21 11:59:11 -07002500 mDirectionVector[0] = mPreviousReorderDirection[0];
2501 mDirectionVector[1] = mPreviousReorderDirection[1];
2502 // We reset this vector after drop
Adam Cohenb209e632012-03-27 17:09:36 -07002503 if (mode == MODE_ON_DROP || mode == MODE_ON_DROP_EXTERNAL) {
2504 mPreviousReorderDirection[0] = INVALID_DIRECTION;
2505 mPreviousReorderDirection[1] = INVALID_DIRECTION;
Adam Cohen19f37922012-03-21 11:59:11 -07002506 }
2507 } else {
2508 getDirectionVectorForDrop(pixelX, pixelY, spanX, spanY, dragView, mDirectionVector);
2509 mPreviousReorderDirection[0] = mDirectionVector[0];
2510 mPreviousReorderDirection[1] = mDirectionVector[1];
2511 }
2512
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002513 // Find a solution involving pushing / displacing any items in the way
2514 ItemConfiguration swapSolution = findReorderSolution(pixelX, pixelY, minSpanX, minSpanY,
Adam Cohen482ed822012-03-02 14:15:13 -08002515 spanX, spanY, mDirectionVector, dragView, true, new ItemConfiguration());
2516
2517 // We attempt the approach which doesn't shuffle views at all
2518 ItemConfiguration noShuffleSolution = findConfigurationNoShuffle(pixelX, pixelY, minSpanX,
2519 minSpanY, spanX, spanY, dragView, new ItemConfiguration());
2520
2521 ItemConfiguration finalSolution = null;
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002522
2523 // If the reorder solution requires resizing (shrinking) the item being dropped, we instead
2524 // favor a solution in which the item is not resized, but
Adam Cohen482ed822012-03-02 14:15:13 -08002525 if (swapSolution.isSolution && swapSolution.area() >= noShuffleSolution.area()) {
2526 finalSolution = swapSolution;
2527 } else if (noShuffleSolution.isSolution) {
2528 finalSolution = noShuffleSolution;
2529 }
2530
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002531 if (mode == MODE_SHOW_REORDER_HINT) {
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002532 if (finalSolution != null) {
Adam Cohenfe692872013-12-11 14:47:23 -08002533 beginOrAdjustReorderPreviewAnimations(finalSolution, dragView, 0,
2534 ReorderPreviewAnimation.MODE_HINT);
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002535 result[0] = finalSolution.dragViewX;
2536 result[1] = finalSolution.dragViewY;
2537 resultSpan[0] = finalSolution.dragViewSpanX;
2538 resultSpan[1] = finalSolution.dragViewSpanY;
2539 } else {
2540 result[0] = result[1] = resultSpan[0] = resultSpan[1] = -1;
2541 }
2542 return result;
2543 }
2544
Adam Cohen482ed822012-03-02 14:15:13 -08002545 boolean foundSolution = true;
2546 if (!DESTRUCTIVE_REORDER) {
2547 setUseTempCoords(true);
2548 }
2549
2550 if (finalSolution != null) {
2551 result[0] = finalSolution.dragViewX;
2552 result[1] = finalSolution.dragViewY;
2553 resultSpan[0] = finalSolution.dragViewSpanX;
2554 resultSpan[1] = finalSolution.dragViewSpanY;
2555
2556 // If we're just testing for a possible location (MODE_ACCEPT_DROP), we don't bother
2557 // committing anything or animating anything as we just want to determine if a solution
2558 // exists
2559 if (mode == MODE_DRAG_OVER || mode == MODE_ON_DROP || mode == MODE_ON_DROP_EXTERNAL) {
2560 if (!DESTRUCTIVE_REORDER) {
2561 copySolutionToTempState(finalSolution, dragView);
2562 }
2563 setItemPlacementDirty(true);
2564 animateItemsToSolution(finalSolution, dragView, mode == MODE_ON_DROP);
2565
Adam Cohen19f37922012-03-21 11:59:11 -07002566 if (!DESTRUCTIVE_REORDER &&
2567 (mode == MODE_ON_DROP || mode == MODE_ON_DROP_EXTERNAL)) {
Adam Cohen482ed822012-03-02 14:15:13 -08002568 commitTempPlacement();
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002569 completeAndClearReorderPreviewAnimations();
Adam Cohen19f37922012-03-21 11:59:11 -07002570 setItemPlacementDirty(false);
2571 } else {
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002572 beginOrAdjustReorderPreviewAnimations(finalSolution, dragView,
2573 REORDER_ANIMATION_DURATION, ReorderPreviewAnimation.MODE_PREVIEW);
Adam Cohen482ed822012-03-02 14:15:13 -08002574 }
2575 }
2576 } else {
2577 foundSolution = false;
2578 result[0] = result[1] = resultSpan[0] = resultSpan[1] = -1;
2579 }
2580
2581 if ((mode == MODE_ON_DROP || !foundSolution) && !DESTRUCTIVE_REORDER) {
2582 setUseTempCoords(false);
2583 }
Adam Cohen482ed822012-03-02 14:15:13 -08002584
Michael Jurkaa52570f2012-03-20 03:18:20 -07002585 mShortcutsAndWidgets.requestLayout();
Adam Cohen482ed822012-03-02 14:15:13 -08002586 return result;
2587 }
2588
Adam Cohen19f37922012-03-21 11:59:11 -07002589 void setItemPlacementDirty(boolean dirty) {
2590 mItemPlacementDirty = dirty;
Adam Cohen482ed822012-03-02 14:15:13 -08002591 }
Adam Cohen19f37922012-03-21 11:59:11 -07002592 boolean isItemPlacementDirty() {
2593 return mItemPlacementDirty;
Adam Cohen482ed822012-03-02 14:15:13 -08002594 }
2595
2596 private class ItemConfiguration {
Adam Cohen8baab352012-03-20 17:39:21 -07002597 HashMap<View, CellAndSpan> map = new HashMap<View, CellAndSpan>();
Adam Cohenf3900c22012-11-16 18:28:11 -08002598 private HashMap<View, CellAndSpan> savedMap = new HashMap<View, CellAndSpan>();
2599 ArrayList<View> sortedViews = new ArrayList<View>();
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002600 ArrayList<View> intersectingViews;
Adam Cohen482ed822012-03-02 14:15:13 -08002601 boolean isSolution = false;
2602 int dragViewX, dragViewY, dragViewSpanX, dragViewSpanY;
2603
Adam Cohenf3900c22012-11-16 18:28:11 -08002604 void save() {
2605 // Copy current state into savedMap
2606 for (View v: map.keySet()) {
2607 map.get(v).copy(savedMap.get(v));
2608 }
2609 }
2610
2611 void restore() {
2612 // Restore current state from savedMap
2613 for (View v: savedMap.keySet()) {
2614 savedMap.get(v).copy(map.get(v));
2615 }
2616 }
2617
2618 void add(View v, CellAndSpan cs) {
2619 map.put(v, cs);
2620 savedMap.put(v, new CellAndSpan());
2621 sortedViews.add(v);
2622 }
2623
Adam Cohen482ed822012-03-02 14:15:13 -08002624 int area() {
2625 return dragViewSpanX * dragViewSpanY;
2626 }
Adam Cohen8baab352012-03-20 17:39:21 -07002627 }
2628
2629 private class CellAndSpan {
2630 int x, y;
2631 int spanX, spanY;
2632
Adam Cohenf3900c22012-11-16 18:28:11 -08002633 public CellAndSpan() {
2634 }
2635
2636 public void copy(CellAndSpan copy) {
2637 copy.x = x;
2638 copy.y = y;
2639 copy.spanX = spanX;
2640 copy.spanY = spanY;
2641 }
2642
Adam Cohen8baab352012-03-20 17:39:21 -07002643 public CellAndSpan(int x, int y, int spanX, int spanY) {
2644 this.x = x;
2645 this.y = y;
2646 this.spanX = spanX;
2647 this.spanY = spanY;
Adam Cohen482ed822012-03-02 14:15:13 -08002648 }
Adam Cohenf3900c22012-11-16 18:28:11 -08002649
2650 public String toString() {
2651 return "(" + x + ", " + y + ": " + spanX + ", " + spanY + ")";
2652 }
2653
Adam Cohen482ed822012-03-02 14:15:13 -08002654 }
2655
Adam Cohendf035382011-04-11 17:22:04 -07002656 /**
2657 * Find a vacant area that will fit the given bounds nearest the requested
2658 * cell location. Uses Euclidean distance to score multiple vacant areas.
2659 *
2660 * @param pixelX The X location at which you want to search for a vacant area.
2661 * @param pixelY The Y location at which you want to search for a vacant area.
2662 * @param spanX Horizontal span of the object.
2663 * @param spanY Vertical span of the object.
2664 * @param ignoreView Considers space occupied by this view as unoccupied
2665 * @param result Previously returned value to possibly recycle.
2666 * @return The X, Y cell of a vacant area that can contain this object,
2667 * nearest the requested location.
2668 */
2669 int[] findNearestVacantArea(
2670 int pixelX, int pixelY, int spanX, int spanY, View ignoreView, int[] result) {
2671 return findNearestArea(pixelX, pixelY, spanX, spanY, ignoreView, true, result);
2672 }
2673
2674 /**
Adam Cohend41fbf52012-02-16 23:53:59 -08002675 * Find a vacant area that will fit the given bounds nearest the requested
2676 * cell location. Uses Euclidean distance to score multiple vacant areas.
2677 *
2678 * @param pixelX The X location at which you want to search for a vacant area.
2679 * @param pixelY The Y location at which you want to search for a vacant area.
2680 * @param minSpanX The minimum horizontal span required
2681 * @param minSpanY The minimum vertical span required
2682 * @param spanX Horizontal span of the object.
2683 * @param spanY Vertical span of the object.
2684 * @param ignoreView Considers space occupied by this view as unoccupied
2685 * @param result Previously returned value to possibly recycle.
2686 * @return The X, Y cell of a vacant area that can contain this object,
2687 * nearest the requested location.
2688 */
2689 int[] findNearestVacantArea(int pixelX, int pixelY, int minSpanX, int minSpanY,
2690 int spanX, int spanY, View ignoreView, int[] result, int[] resultSpan) {
Adam Cohen482ed822012-03-02 14:15:13 -08002691 return findNearestArea(pixelX, pixelY, minSpanX, minSpanY, spanX, spanY, ignoreView, true,
2692 result, resultSpan, mOccupied);
Adam Cohend41fbf52012-02-16 23:53:59 -08002693 }
2694
2695 /**
Adam Cohendf035382011-04-11 17:22:04 -07002696 * Find a starting cell position that will fit the given bounds nearest the requested
2697 * cell location. Uses Euclidean distance to score multiple vacant areas.
2698 *
2699 * @param pixelX The X location at which you want to search for a vacant area.
2700 * @param pixelY The Y location at which you want to search for a vacant area.
2701 * @param spanX Horizontal span of the object.
2702 * @param spanY Vertical span of the object.
2703 * @param ignoreView Considers space occupied by this view as unoccupied
2704 * @param result Previously returned value to possibly recycle.
2705 * @return The X, Y cell of a vacant area that can contain this object,
2706 * nearest the requested location.
2707 */
2708 int[] findNearestArea(
2709 int pixelX, int pixelY, int spanX, int spanY, int[] result) {
2710 return findNearestArea(pixelX, pixelY, spanX, spanY, null, false, result);
2711 }
2712
Michael Jurka0280c3b2010-09-17 15:00:07 -07002713 boolean existsEmptyCell() {
2714 return findCellForSpan(null, 1, 1);
2715 }
2716
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002717 /**
Michael Jurka0280c3b2010-09-17 15:00:07 -07002718 * Finds the upper-left coordinate of the first rectangle in the grid that can
2719 * hold a cell of the specified dimensions. If intersectX and intersectY are not -1,
2720 * then this method will only return coordinates for rectangles that contain the cell
2721 * (intersectX, intersectY)
2722 *
2723 * @param cellXY The array that will contain the position of a vacant cell if such a cell
2724 * can be found.
2725 * @param spanX The horizontal span of the cell we want to find.
2726 * @param spanY The vertical span of the cell we want to find.
2727 *
2728 * @return True if a vacant cell of the specified dimension was found, false otherwise.
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07002729 */
Michael Jurka0280c3b2010-09-17 15:00:07 -07002730 boolean findCellForSpan(int[] cellXY, int spanX, int spanY) {
Adam Cohen482ed822012-03-02 14:15:13 -08002731 return findCellForSpanThatIntersectsIgnoring(cellXY, spanX, spanY, -1, -1, null, mOccupied);
Michael Jurka0280c3b2010-09-17 15:00:07 -07002732 }
2733
2734 /**
2735 * Like above, but ignores any cells occupied by the item "ignoreView"
2736 *
2737 * @param cellXY The array that will contain the position of a vacant cell if such a cell
2738 * can be found.
2739 * @param spanX The horizontal span of the cell we want to find.
2740 * @param spanY The vertical span of the cell we want to find.
2741 * @param ignoreView The home screen item we should treat as not occupying any space
2742 * @return
2743 */
2744 boolean findCellForSpanIgnoring(int[] cellXY, int spanX, int spanY, View ignoreView) {
Adam Cohen482ed822012-03-02 14:15:13 -08002745 return findCellForSpanThatIntersectsIgnoring(cellXY, spanX, spanY, -1, -1,
2746 ignoreView, mOccupied);
Michael Jurka0280c3b2010-09-17 15:00:07 -07002747 }
2748
2749 /**
2750 * Like above, but if intersectX and intersectY are not -1, then this method will try to
2751 * return coordinates for rectangles that contain the cell [intersectX, intersectY]
2752 *
2753 * @param spanX The horizontal span of the cell we want to find.
2754 * @param spanY The vertical span of the cell we want to find.
2755 * @param ignoreView The home screen item we should treat as not occupying any space
2756 * @param intersectX The X coordinate of the cell that we should try to overlap
2757 * @param intersectX The Y coordinate of the cell that we should try to overlap
2758 *
2759 * @return True if a vacant cell of the specified dimension was found, false otherwise.
2760 */
2761 boolean findCellForSpanThatIntersects(int[] cellXY, int spanX, int spanY,
2762 int intersectX, int intersectY) {
2763 return findCellForSpanThatIntersectsIgnoring(
Adam Cohen482ed822012-03-02 14:15:13 -08002764 cellXY, spanX, spanY, intersectX, intersectY, null, mOccupied);
Michael Jurka0280c3b2010-09-17 15:00:07 -07002765 }
2766
2767 /**
2768 * The superset of the above two methods
2769 */
2770 boolean findCellForSpanThatIntersectsIgnoring(int[] cellXY, int spanX, int spanY,
Adam Cohen482ed822012-03-02 14:15:13 -08002771 int intersectX, int intersectY, View ignoreView, boolean occupied[][]) {
Michael Jurkac6ee42e2010-09-30 12:04:50 -07002772 // mark space take by ignoreView as available (method checks if ignoreView is null)
Adam Cohen482ed822012-03-02 14:15:13 -08002773 markCellsAsUnoccupiedForView(ignoreView, occupied);
Michael Jurka0280c3b2010-09-17 15:00:07 -07002774
Michael Jurka28750fb2010-09-24 17:43:49 -07002775 boolean foundCell = false;
Michael Jurka0280c3b2010-09-17 15:00:07 -07002776 while (true) {
2777 int startX = 0;
2778 if (intersectX >= 0) {
2779 startX = Math.max(startX, intersectX - (spanX - 1));
2780 }
2781 int endX = mCountX - (spanX - 1);
2782 if (intersectX >= 0) {
2783 endX = Math.min(endX, intersectX + (spanX - 1) + (spanX == 1 ? 1 : 0));
2784 }
2785 int startY = 0;
2786 if (intersectY >= 0) {
2787 startY = Math.max(startY, intersectY - (spanY - 1));
2788 }
2789 int endY = mCountY - (spanY - 1);
2790 if (intersectY >= 0) {
2791 endY = Math.min(endY, intersectY + (spanY - 1) + (spanY == 1 ? 1 : 0));
2792 }
2793
Winson Chungbbc60d82010-11-11 16:34:41 -08002794 for (int y = startY; y < endY && !foundCell; y++) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002795 inner:
Winson Chungbbc60d82010-11-11 16:34:41 -08002796 for (int x = startX; x < endX; x++) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002797 for (int i = 0; i < spanX; i++) {
2798 for (int j = 0; j < spanY; j++) {
Adam Cohen482ed822012-03-02 14:15:13 -08002799 if (occupied[x + i][y + j]) {
Winson Chungbbc60d82010-11-11 16:34:41 -08002800 // small optimization: we can skip to after the column we just found
Michael Jurka0280c3b2010-09-17 15:00:07 -07002801 // an occupied cell
Winson Chungbbc60d82010-11-11 16:34:41 -08002802 x += i;
Michael Jurka0280c3b2010-09-17 15:00:07 -07002803 continue inner;
2804 }
2805 }
2806 }
2807 if (cellXY != null) {
2808 cellXY[0] = x;
2809 cellXY[1] = y;
2810 }
Michael Jurka28750fb2010-09-24 17:43:49 -07002811 foundCell = true;
2812 break;
Michael Jurka0280c3b2010-09-17 15:00:07 -07002813 }
2814 }
2815 if (intersectX == -1 && intersectY == -1) {
2816 break;
2817 } else {
2818 // if we failed to find anything, try again but without any requirements of
2819 // intersecting
2820 intersectX = -1;
2821 intersectY = -1;
2822 continue;
2823 }
2824 }
2825
Michael Jurkac6ee42e2010-09-30 12:04:50 -07002826 // re-mark space taken by ignoreView as occupied
Adam Cohen482ed822012-03-02 14:15:13 -08002827 markCellsAsOccupiedForView(ignoreView, occupied);
Michael Jurka28750fb2010-09-24 17:43:49 -07002828 return foundCell;
Michael Jurka0280c3b2010-09-17 15:00:07 -07002829 }
2830
2831 /**
Winson Chungc07918d2011-07-01 15:35:26 -07002832 * A drag event has begun over this layout.
2833 * It may have begun over this layout (in which case onDragChild is called first),
2834 * or it may have begun on another layout.
2835 */
2836 void onDragEnter() {
Adam Cohenc6cc61d2012-04-04 12:47:08 -07002837 mDragEnforcer.onDragEnter();
Winson Chungc07918d2011-07-01 15:35:26 -07002838 mDragging = true;
2839 }
2840
2841 /**
Michael Jurka0280c3b2010-09-17 15:00:07 -07002842 * Called when drag has left this CellLayout or has been completed (successfully or not)
2843 */
2844 void onDragExit() {
Adam Cohenc6cc61d2012-04-04 12:47:08 -07002845 mDragEnforcer.onDragExit();
Joe Onorato4be866d2010-10-10 11:26:02 -07002846 // This can actually be called when we aren't in a drag, e.g. when adding a new
2847 // item to this layout via the customize drawer.
2848 // Guard against that case.
2849 if (mDragging) {
2850 mDragging = false;
Patrick Dubroyde7658b2010-09-27 11:15:43 -07002851 }
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -07002852
2853 // Invalidate the drag data
Adam Cohend41fbf52012-02-16 23:53:59 -08002854 mDragCell[0] = mDragCell[1] = -1;
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -07002855 mDragOutlineAnims[mDragOutlineCurrent].animateOut();
2856 mDragOutlineCurrent = (mDragOutlineCurrent + 1) % mDragOutlineAnims.length;
Adam Cohen19f37922012-03-21 11:59:11 -07002857 revertTempState();
Michael Jurka33945b22010-12-21 18:19:38 -08002858 setIsDragOverlapping(false);
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07002859 }
2860
2861 /**
Winson Chungaafa03c2010-06-11 17:34:16 -07002862 * Mark a child as having been dropped.
Patrick Dubroyde7658b2010-09-27 11:15:43 -07002863 * At the beginning of the drag operation, the child may have been on another
Patrick Dubroyce34a972010-10-19 10:34:32 -07002864 * screen, but it is re-parented before this method is called.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002865 *
2866 * @param child The child that is being dropped
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002867 */
Adam Cohen716b51e2011-06-30 12:09:54 -07002868 void onDropChild(View child) {
Romain Guyd94533d2009-08-17 10:01:15 -07002869 if (child != null) {
2870 LayoutParams lp = (LayoutParams) child.getLayoutParams();
Romain Guy84f296c2009-11-04 15:00:44 -08002871 lp.dropped = true;
Romain Guyd94533d2009-08-17 10:01:15 -07002872 child.requestLayout();
Romain Guyd94533d2009-08-17 10:01:15 -07002873 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002874 }
2875
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002876 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002877 * Computes a bounding rectangle for a range of cells
Winson Chungaafa03c2010-06-11 17:34:16 -07002878 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002879 * @param cellX X coordinate of upper left corner expressed as a cell position
2880 * @param cellY Y coordinate of upper left corner expressed as a cell position
Winson Chungaafa03c2010-06-11 17:34:16 -07002881 * @param cellHSpan Width in cells
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002882 * @param cellVSpan Height in cells
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07002883 * @param resultRect Rect into which to put the results
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002884 */
Adam Cohend41fbf52012-02-16 23:53:59 -08002885 public void cellToRect(int cellX, int cellY, int cellHSpan, int cellVSpan, Rect resultRect) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002886 final int cellWidth = mCellWidth;
2887 final int cellHeight = mCellHeight;
2888 final int widthGap = mWidthGap;
2889 final int heightGap = mHeightGap;
Winson Chungaafa03c2010-06-11 17:34:16 -07002890
Winson Chung4b825dcd2011-06-19 12:41:22 -07002891 final int hStartPadding = getPaddingLeft();
2892 final int vStartPadding = getPaddingTop();
Winson Chungaafa03c2010-06-11 17:34:16 -07002893
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002894 int width = cellHSpan * cellWidth + ((cellHSpan - 1) * widthGap);
2895 int height = cellVSpan * cellHeight + ((cellVSpan - 1) * heightGap);
2896
2897 int x = hStartPadding + cellX * (cellWidth + widthGap);
2898 int y = vStartPadding + cellY * (cellHeight + heightGap);
Winson Chungaafa03c2010-06-11 17:34:16 -07002899
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07002900 resultRect.set(x, y, x + width, y + height);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002901 }
Winson Chungaafa03c2010-06-11 17:34:16 -07002902
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002903 /**
Winson Chungaafa03c2010-06-11 17:34:16 -07002904 * Computes the required horizontal and vertical cell spans to always
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002905 * fit the given rectangle.
Winson Chungaafa03c2010-06-11 17:34:16 -07002906 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002907 * @param width Width in pixels
2908 * @param height Height in pixels
Patrick Dubroy8f86ddc2010-07-16 13:55:32 -07002909 * @param result An array of length 2 in which to store the result (may be null).
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002910 */
Winson Chung66700732013-08-20 16:56:15 -07002911 public static int[] rectToCell(int width, int height, int[] result) {
Winson Chung5f8afe62013-08-12 16:19:28 -07002912 LauncherAppState app = LauncherAppState.getInstance();
2913 DeviceProfile grid = app.getDynamicGrid().getDeviceProfile();
Winson Chung66700732013-08-20 16:56:15 -07002914 Rect padding = grid.getWorkspacePadding(grid.isLandscape ?
2915 CellLayout.LANDSCAPE : CellLayout.PORTRAIT);
Winson Chung5f8afe62013-08-12 16:19:28 -07002916
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002917 // Always assume we're working with the smallest span to make sure we
2918 // reserve enough space in both orientations.
Winson Chung66700732013-08-20 16:56:15 -07002919 int parentWidth = grid.calculateCellWidth(grid.widthPx
2920 - padding.left - padding.right, (int) grid.numColumns);
2921 int parentHeight = grid.calculateCellHeight(grid.heightPx
2922 - padding.top - padding.bottom, (int) grid.numRows);
2923 int smallerSize = Math.min(parentWidth, parentHeight);
Joe Onorato79e56262009-09-21 15:23:04 -04002924
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002925 // Always round up to next largest cell
Winson Chung54c725c2011-08-03 12:03:40 -07002926 int spanX = (int) Math.ceil(width / (float) smallerSize);
2927 int spanY = (int) Math.ceil(height / (float) smallerSize);
Joe Onorato79e56262009-09-21 15:23:04 -04002928
Patrick Dubroy8f86ddc2010-07-16 13:55:32 -07002929 if (result == null) {
2930 return new int[] { spanX, spanY };
2931 }
2932 result[0] = spanX;
2933 result[1] = spanY;
2934 return result;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002935 }
2936
Michael Jurkaf12c75c2011-01-25 22:41:40 -08002937 public int[] cellSpansToSize(int hSpans, int vSpans) {
2938 int[] size = new int[2];
2939 size[0] = hSpans * mCellWidth + (hSpans - 1) * mWidthGap;
2940 size[1] = vSpans * mCellHeight + (vSpans - 1) * mHeightGap;
2941 return size;
2942 }
2943
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002944 /**
Patrick Dubroy047379a2010-12-19 22:02:04 -08002945 * Calculate the grid spans needed to fit given item
2946 */
2947 public void calculateSpans(ItemInfo info) {
2948 final int minWidth;
2949 final int minHeight;
2950
2951 if (info instanceof LauncherAppWidgetInfo) {
2952 minWidth = ((LauncherAppWidgetInfo) info).minWidth;
2953 minHeight = ((LauncherAppWidgetInfo) info).minHeight;
2954 } else if (info instanceof PendingAddWidgetInfo) {
2955 minWidth = ((PendingAddWidgetInfo) info).minWidth;
2956 minHeight = ((PendingAddWidgetInfo) info).minHeight;
2957 } else {
2958 // It's not a widget, so it must be 1x1
2959 info.spanX = info.spanY = 1;
2960 return;
2961 }
2962 int[] spans = rectToCell(minWidth, minHeight, null);
2963 info.spanX = spans[0];
2964 info.spanY = spans[1];
2965 }
2966
2967 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002968 * Find the first vacant cell, if there is one.
2969 *
2970 * @param vacant Holds the x and y coordinate of the vacant cell
2971 * @param spanX Horizontal cell span.
2972 * @param spanY Vertical cell span.
Winson Chungaafa03c2010-06-11 17:34:16 -07002973 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002974 * @return True if a vacant cell was found
2975 */
2976 public boolean getVacantCell(int[] vacant, int spanX, int spanY) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002977
Michael Jurka0280c3b2010-09-17 15:00:07 -07002978 return findVacantCell(vacant, spanX, spanY, mCountX, mCountY, mOccupied);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002979 }
2980
2981 static boolean findVacantCell(int[] vacant, int spanX, int spanY,
2982 int xCount, int yCount, boolean[][] occupied) {
2983
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08002984 for (int y = 0; (y + spanY) <= yCount; y++) {
2985 for (int x = 0; (x + spanX) <= xCount; x++) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002986 boolean available = !occupied[x][y];
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08002987out: for (int i = x; i < x + spanX; i++) {
2988 for (int j = y; j < y + spanY; j++) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002989 available = available && !occupied[i][j];
2990 if (!available) break out;
2991 }
2992 }
2993
2994 if (available) {
2995 vacant[0] = x;
2996 vacant[1] = y;
2997 return true;
2998 }
2999 }
3000 }
3001
3002 return false;
3003 }
3004
Michael Jurka0280c3b2010-09-17 15:00:07 -07003005 private void clearOccupiedCells() {
3006 for (int x = 0; x < mCountX; x++) {
3007 for (int y = 0; y < mCountY; y++) {
3008 mOccupied[x][y] = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003009 }
3010 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07003011 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003012
Adam Cohend41fbf52012-02-16 23:53:59 -08003013 public void onMove(View view, int newCellX, int newCellY, int newSpanX, int newSpanY) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07003014 markCellsAsUnoccupiedForView(view);
Adam Cohen482ed822012-03-02 14:15:13 -08003015 markCellsForView(newCellX, newCellY, newSpanX, newSpanY, mOccupied, true);
Michael Jurka0280c3b2010-09-17 15:00:07 -07003016 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003017
Adam Cohend4844c32011-02-18 19:25:06 -08003018 public void markCellsAsOccupiedForView(View view) {
Adam Cohen482ed822012-03-02 14:15:13 -08003019 markCellsAsOccupiedForView(view, mOccupied);
3020 }
3021 public void markCellsAsOccupiedForView(View view, boolean[][] occupied) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07003022 if (view == null || view.getParent() != mShortcutsAndWidgets) return;
Michael Jurka0280c3b2010-09-17 15:00:07 -07003023 LayoutParams lp = (LayoutParams) view.getLayoutParams();
Adam Cohen482ed822012-03-02 14:15:13 -08003024 markCellsForView(lp.cellX, lp.cellY, lp.cellHSpan, lp.cellVSpan, occupied, true);
Michael Jurka0280c3b2010-09-17 15:00:07 -07003025 }
3026
Adam Cohend4844c32011-02-18 19:25:06 -08003027 public void markCellsAsUnoccupiedForView(View view) {
Adam Cohen482ed822012-03-02 14:15:13 -08003028 markCellsAsUnoccupiedForView(view, mOccupied);
3029 }
3030 public void markCellsAsUnoccupiedForView(View view, boolean occupied[][]) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07003031 if (view == null || view.getParent() != mShortcutsAndWidgets) return;
Michael Jurka0280c3b2010-09-17 15:00:07 -07003032 LayoutParams lp = (LayoutParams) view.getLayoutParams();
Adam Cohen482ed822012-03-02 14:15:13 -08003033 markCellsForView(lp.cellX, lp.cellY, lp.cellHSpan, lp.cellVSpan, occupied, false);
Michael Jurka0280c3b2010-09-17 15:00:07 -07003034 }
3035
Adam Cohen482ed822012-03-02 14:15:13 -08003036 private void markCellsForView(int cellX, int cellY, int spanX, int spanY, boolean[][] occupied,
3037 boolean value) {
3038 if (cellX < 0 || cellY < 0) return;
Michael Jurka0280c3b2010-09-17 15:00:07 -07003039 for (int x = cellX; x < cellX + spanX && x < mCountX; x++) {
3040 for (int y = cellY; y < cellY + spanY && y < mCountY; y++) {
Adam Cohen482ed822012-03-02 14:15:13 -08003041 occupied[x][y] = value;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003042 }
3043 }
3044 }
3045
Adam Cohen2801caf2011-05-13 20:57:39 -07003046 public int getDesiredWidth() {
Michael Jurka8b805b12012-04-18 14:23:14 -07003047 return getPaddingLeft() + getPaddingRight() + (mCountX * mCellWidth) +
Adam Cohen2801caf2011-05-13 20:57:39 -07003048 (Math.max((mCountX - 1), 0) * mWidthGap);
3049 }
3050
3051 public int getDesiredHeight() {
Michael Jurka8b805b12012-04-18 14:23:14 -07003052 return getPaddingTop() + getPaddingBottom() + (mCountY * mCellHeight) +
Adam Cohen2801caf2011-05-13 20:57:39 -07003053 (Math.max((mCountY - 1), 0) * mHeightGap);
3054 }
3055
Michael Jurka66d72172011-04-12 16:29:25 -07003056 public boolean isOccupied(int x, int y) {
3057 if (x < mCountX && y < mCountY) {
3058 return mOccupied[x][y];
3059 } else {
3060 throw new RuntimeException("Position exceeds the bound of this CellLayout");
3061 }
3062 }
3063
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003064 @Override
3065 public ViewGroup.LayoutParams generateLayoutParams(AttributeSet attrs) {
3066 return new CellLayout.LayoutParams(getContext(), attrs);
3067 }
3068
3069 @Override
3070 protected boolean checkLayoutParams(ViewGroup.LayoutParams p) {
3071 return p instanceof CellLayout.LayoutParams;
3072 }
3073
3074 @Override
3075 protected ViewGroup.LayoutParams generateLayoutParams(ViewGroup.LayoutParams p) {
3076 return new CellLayout.LayoutParams(p);
3077 }
3078
Winson Chungaafa03c2010-06-11 17:34:16 -07003079 public static class CellLayoutAnimationController extends LayoutAnimationController {
3080 public CellLayoutAnimationController(Animation animation, float delay) {
3081 super(animation, delay);
3082 }
3083
3084 @Override
3085 protected long getDelayForView(View view) {
3086 return (int) (Math.random() * 150);
3087 }
3088 }
3089
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003090 public static class LayoutParams extends ViewGroup.MarginLayoutParams {
3091 /**
3092 * Horizontal location of the item in the grid.
3093 */
3094 @ViewDebug.ExportedProperty
3095 public int cellX;
3096
3097 /**
3098 * Vertical location of the item in the grid.
3099 */
3100 @ViewDebug.ExportedProperty
3101 public int cellY;
3102
3103 /**
Adam Cohen482ed822012-03-02 14:15:13 -08003104 * Temporary horizontal location of the item in the grid during reorder
3105 */
3106 public int tmpCellX;
3107
3108 /**
3109 * Temporary vertical location of the item in the grid during reorder
3110 */
3111 public int tmpCellY;
3112
3113 /**
3114 * Indicates that the temporary coordinates should be used to layout the items
3115 */
3116 public boolean useTmpCoords;
3117
3118 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003119 * Number of cells spanned horizontally by the item.
3120 */
3121 @ViewDebug.ExportedProperty
3122 public int cellHSpan;
3123
3124 /**
3125 * Number of cells spanned vertically by the item.
3126 */
3127 @ViewDebug.ExportedProperty
3128 public int cellVSpan;
Winson Chungaafa03c2010-06-11 17:34:16 -07003129
Adam Cohen1b607ed2011-03-03 17:26:50 -08003130 /**
3131 * Indicates whether the item will set its x, y, width and height parameters freely,
3132 * or whether these will be computed based on cellX, cellY, cellHSpan and cellVSpan.
3133 */
Adam Cohend4844c32011-02-18 19:25:06 -08003134 public boolean isLockedToGrid = true;
3135
Adam Cohen482ed822012-03-02 14:15:13 -08003136 /**
Adam Cohenec40b2b2013-07-23 15:52:40 -07003137 * Indicates that this item should use the full extents of its parent.
3138 */
3139 public boolean isFullscreen = false;
3140
3141 /**
Adam Cohen482ed822012-03-02 14:15:13 -08003142 * Indicates whether this item can be reordered. Always true except in the case of the
3143 * the AllApps button.
3144 */
3145 public boolean canReorder = true;
3146
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003147 // X coordinate of the view in the layout.
3148 @ViewDebug.ExportedProperty
3149 int x;
3150 // Y coordinate of the view in the layout.
3151 @ViewDebug.ExportedProperty
3152 int y;
3153
Romain Guy84f296c2009-11-04 15:00:44 -08003154 boolean dropped;
Romain Guyfcb9e712009-10-02 16:06:52 -07003155
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003156 public LayoutParams(Context c, AttributeSet attrs) {
3157 super(c, attrs);
3158 cellHSpan = 1;
3159 cellVSpan = 1;
3160 }
3161
3162 public LayoutParams(ViewGroup.LayoutParams source) {
3163 super(source);
3164 cellHSpan = 1;
3165 cellVSpan = 1;
3166 }
Winson Chungaafa03c2010-06-11 17:34:16 -07003167
3168 public LayoutParams(LayoutParams source) {
3169 super(source);
3170 this.cellX = source.cellX;
3171 this.cellY = source.cellY;
3172 this.cellHSpan = source.cellHSpan;
3173 this.cellVSpan = source.cellVSpan;
3174 }
3175
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003176 public LayoutParams(int cellX, int cellY, int cellHSpan, int cellVSpan) {
Romain Guy8f19cdd2010-01-08 15:07:00 -08003177 super(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003178 this.cellX = cellX;
3179 this.cellY = cellY;
3180 this.cellHSpan = cellHSpan;
3181 this.cellVSpan = cellVSpan;
3182 }
3183
Adam Cohen2374abf2013-04-16 14:56:57 -07003184 public void setup(int cellWidth, int cellHeight, int widthGap, int heightGap,
3185 boolean invertHorizontally, int colCount) {
Adam Cohend4844c32011-02-18 19:25:06 -08003186 if (isLockedToGrid) {
3187 final int myCellHSpan = cellHSpan;
3188 final int myCellVSpan = cellVSpan;
Adam Cohen2374abf2013-04-16 14:56:57 -07003189 int myCellX = useTmpCoords ? tmpCellX : cellX;
3190 int myCellY = useTmpCoords ? tmpCellY : cellY;
3191
3192 if (invertHorizontally) {
3193 myCellX = colCount - myCellX - cellHSpan;
3194 }
Adam Cohen1b607ed2011-03-03 17:26:50 -08003195
Adam Cohend4844c32011-02-18 19:25:06 -08003196 width = myCellHSpan * cellWidth + ((myCellHSpan - 1) * widthGap) -
3197 leftMargin - rightMargin;
3198 height = myCellVSpan * cellHeight + ((myCellVSpan - 1) * heightGap) -
3199 topMargin - bottomMargin;
Winson Chungeecf02d2012-03-02 17:14:58 -08003200 x = (int) (myCellX * (cellWidth + widthGap) + leftMargin);
3201 y = (int) (myCellY * (cellHeight + heightGap) + topMargin);
Adam Cohend4844c32011-02-18 19:25:06 -08003202 }
3203 }
Winson Chungaafa03c2010-06-11 17:34:16 -07003204
Winson Chungaafa03c2010-06-11 17:34:16 -07003205 public String toString() {
3206 return "(" + this.cellX + ", " + this.cellY + ")";
3207 }
Adam Cohen7f4eabe2011-04-21 16:19:16 -07003208
3209 public void setWidth(int width) {
3210 this.width = width;
3211 }
3212
3213 public int getWidth() {
3214 return width;
3215 }
3216
3217 public void setHeight(int height) {
3218 this.height = height;
3219 }
3220
3221 public int getHeight() {
3222 return height;
3223 }
3224
3225 public void setX(int x) {
3226 this.x = x;
3227 }
3228
3229 public int getX() {
3230 return x;
3231 }
3232
3233 public void setY(int y) {
3234 this.y = y;
3235 }
3236
3237 public int getY() {
3238 return y;
3239 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003240 }
3241
Michael Jurka0280c3b2010-09-17 15:00:07 -07003242 // This class stores info for two purposes:
3243 // 1. When dragging items (mDragInfo in Workspace), we store the View, its cellX & cellY,
3244 // its spanX, spanY, and the screen it is on
3245 // 2. When long clicking on an empty cell in a CellLayout, we save information about the
3246 // cellX and cellY coordinates and which page was clicked. We then set this as a tag on
3247 // the CellLayout that was long clicked
Michael Jurkae5fb0f22011-04-11 13:27:46 -07003248 static final class CellInfo {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003249 View cell;
Michael Jurkaa63c4522010-08-19 13:52:27 -07003250 int cellX = -1;
3251 int cellY = -1;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003252 int spanX;
3253 int spanY;
Adam Cohendcd297f2013-06-18 13:13:40 -07003254 long screenId;
Winson Chung3d503fb2011-07-13 17:25:49 -07003255 long container;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003256
Adam Cohene0aaa0d2014-05-12 12:44:22 -07003257 CellInfo(View v, ItemInfo info) {
3258 cell = v;
3259 cellX = info.cellX;
3260 cellY = info.cellY;
3261 spanX = info.spanX;
3262 spanY = info.spanY;
3263 screenId = info.screenId;
3264 container = info.container;
3265 }
3266
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003267 @Override
3268 public String toString() {
Winson Chungaafa03c2010-06-11 17:34:16 -07003269 return "Cell[view=" + (cell == null ? "null" : cell.getClass())
3270 + ", x=" + cellX + ", y=" + cellY + "]";
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003271 }
3272 }
Michael Jurkad771c962011-08-09 15:00:48 -07003273
3274 public boolean lastDownOnOccupiedCell() {
3275 return mLastDownOnOccupiedCell;
3276 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003277}