blob: 10737648268f9abf84a42efd21ea6668e2e5f4a7 [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 Cohenebea84d2011-11-09 17:20:41 -080074 private boolean mScrollingTransformsDirty = false;
Adam Cohen917e3882013-10-31 15:03:35 -070075 private boolean mDropPending = false;
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
Patrick Dubroy96864c32011-03-10 17:17:23 -0800126 private BubbleTextView mPressedOrFocusedIcon;
127
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
Michael Jurkaa52570f2012-03-20 03:18:20 -0700291 addView(mShortcutsAndWidgets);
Michael Jurka18014792010-10-14 09:01:34 -0700292 }
293
Chris Craik01f2d7f2013-10-01 14:41:56 -0700294 public void enableHardwareLayer(boolean hasLayer) {
295 mShortcutsAndWidgets.setLayerType(hasLayer ? LAYER_TYPE_HARDWARE : LAYER_TYPE_NONE, sPaint);
Michael Jurkad51f33a2012-06-28 15:35:26 -0700296 }
297
298 public void buildHardwareLayer() {
299 mShortcutsAndWidgets.buildLayer();
Adam Cohen2801caf2011-05-13 20:57:39 -0700300 }
301
Adam Cohen307fe232012-08-16 17:55:58 -0700302 public float getChildrenScale() {
303 return mIsHotseat ? mHotseatScale : 1.0f;
304 }
305
Winson Chung5f8afe62013-08-12 16:19:28 -0700306 public void setCellDimensions(int width, int height) {
Winson Chung11a1a532013-09-13 11:14:45 -0700307 mFixedCellWidth = mCellWidth = width;
308 mFixedCellHeight = mCellHeight = height;
Winson Chung5f8afe62013-08-12 16:19:28 -0700309 mShortcutsAndWidgets.setCellDimensions(mCellWidth, mCellHeight, mWidthGap, mHeightGap,
310 mCountX, mCountY);
311 }
312
Adam Cohen2801caf2011-05-13 20:57:39 -0700313 public void setGridSize(int x, int y) {
314 mCountX = x;
315 mCountY = y;
316 mOccupied = new boolean[mCountX][mCountY];
Adam Cohen482ed822012-03-02 14:15:13 -0800317 mTmpOccupied = new boolean[mCountX][mCountY];
Adam Cohen7fbec102012-03-27 12:42:19 -0700318 mTempRectStack.clear();
Adam Cohen2374abf2013-04-16 14:56:57 -0700319 mShortcutsAndWidgets.setCellDimensions(mCellWidth, mCellHeight, mWidthGap, mHeightGap,
Winson Chung5f8afe62013-08-12 16:19:28 -0700320 mCountX, mCountY);
Adam Cohen76fc0852011-06-17 13:26:23 -0700321 requestLayout();
Adam Cohen2801caf2011-05-13 20:57:39 -0700322 }
323
Adam Cohen2374abf2013-04-16 14:56:57 -0700324 // Set whether or not to invert the layout horizontally if the layout is in RTL mode.
325 public void setInvertIfRtl(boolean invert) {
326 mShortcutsAndWidgets.setInvertIfRtl(invert);
327 }
328
Adam Cohen917e3882013-10-31 15:03:35 -0700329 public void setDropPending(boolean pending) {
330 mDropPending = pending;
331 }
332
333 public boolean isDropPending() {
334 return mDropPending;
335 }
336
Patrick Dubroy96864c32011-03-10 17:17:23 -0800337 private void invalidateBubbleTextView(BubbleTextView icon) {
338 final int padding = icon.getPressedOrFocusedBackgroundPadding();
Winson Chung4b825dcd2011-06-19 12:41:22 -0700339 invalidate(icon.getLeft() + getPaddingLeft() - padding,
340 icon.getTop() + getPaddingTop() - padding,
341 icon.getRight() + getPaddingLeft() + padding,
342 icon.getBottom() + getPaddingTop() + padding);
Patrick Dubroy96864c32011-03-10 17:17:23 -0800343 }
344
Adam Cohenb5ba0972011-09-07 18:02:31 -0700345 void setOverScrollAmount(float r, boolean left) {
346 if (left && mOverScrollForegroundDrawable != mOverScrollLeft) {
347 mOverScrollForegroundDrawable = mOverScrollLeft;
348 } else if (!left && mOverScrollForegroundDrawable != mOverScrollRight) {
349 mOverScrollForegroundDrawable = mOverScrollRight;
350 }
351
Adam Cohen02dcfcc2013-10-01 12:37:33 -0700352 r *= FOREGROUND_ALPHA_DAMPER;
Adam Cohenb5ba0972011-09-07 18:02:31 -0700353 mForegroundAlpha = (int) Math.round((r * 255));
354 mOverScrollForegroundDrawable.setAlpha(mForegroundAlpha);
355 invalidate();
356 }
357
Patrick Dubroy96864c32011-03-10 17:17:23 -0800358 void setPressedOrFocusedIcon(BubbleTextView icon) {
359 // We draw the pressed or focused BubbleTextView's background in CellLayout because it
360 // requires an expanded clip rect (due to the glow's blur radius)
361 BubbleTextView oldIcon = mPressedOrFocusedIcon;
362 mPressedOrFocusedIcon = icon;
363 if (oldIcon != null) {
364 invalidateBubbleTextView(oldIcon);
365 }
366 if (mPressedOrFocusedIcon != null) {
367 invalidateBubbleTextView(mPressedOrFocusedIcon);
368 }
369 }
370
Michael Jurka33945b22010-12-21 18:19:38 -0800371 void setIsDragOverlapping(boolean isDragOverlapping) {
372 if (mIsDragOverlapping != isDragOverlapping) {
373 mIsDragOverlapping = isDragOverlapping;
Adam Cohendedbd962013-07-11 14:21:49 -0700374 setUseActiveGlowBackground(mIsDragOverlapping);
Michael Jurka33945b22010-12-21 18:19:38 -0800375 invalidate();
376 }
377 }
378
Adam Cohendedbd962013-07-11 14:21:49 -0700379 void setUseActiveGlowBackground(boolean use) {
380 mUseActiveGlowBackground = use;
381 }
382
Winson Chung59a488a2013-12-10 12:32:14 -0800383 void disableBackground() {
384 mDrawBackground = false;
385 }
386
Michael Jurka33945b22010-12-21 18:19:38 -0800387 boolean getIsDragOverlapping() {
388 return mIsDragOverlapping;
389 }
390
Adam Cohenebea84d2011-11-09 17:20:41 -0800391 protected void setOverscrollTransformsDirty(boolean dirty) {
392 mScrollingTransformsDirty = dirty;
393 }
394
395 protected void resetOverscrollTransforms() {
396 if (mScrollingTransformsDirty) {
397 setOverscrollTransformsDirty(false);
398 setTranslationX(0);
399 setRotationY(0);
400 // It doesn't matter if we pass true or false here, the important thing is that we
401 // pass 0, which results in the overscroll drawable not being drawn any more.
402 setOverScrollAmount(0, false);
403 setPivotX(getMeasuredWidth() / 2);
404 setPivotY(getMeasuredHeight() / 2);
405 }
406 }
407
Jeff Sharkey83f111d2009-04-20 21:03:13 -0700408 @Override
Patrick Dubroy1262e362010-10-06 15:49:50 -0700409 protected void onDraw(Canvas canvas) {
Michael Jurka3e7c7632010-10-02 16:01:03 -0700410 // When we're large, we are either drawn in a "hover" state (ie when dragging an item to
411 // a neighboring page) or with just a normal background (if backgroundAlpha > 0.0f)
412 // When we're small, we are either drawn normally or in the "accepts drops" state (during
413 // a drag). However, we also drag the mini hover background *over* one of those two
414 // backgrounds
Winson Chung59a488a2013-12-10 12:32:14 -0800415 if (mDrawBackground && mBackgroundAlpha > 0.0f) {
Adam Cohenf34bab52010-09-30 14:11:56 -0700416 Drawable bg;
Michael Jurka33945b22010-12-21 18:19:38 -0800417
Adam Cohendedbd962013-07-11 14:21:49 -0700418 if (mUseActiveGlowBackground) {
Michael Jurka33945b22010-12-21 18:19:38 -0800419 // In the mini case, we draw the active_glow bg *over* the active background
Michael Jurkabdf78552011-10-31 14:34:25 -0700420 bg = mActiveGlowBackground;
Adam Cohenf34bab52010-09-30 14:11:56 -0700421 } else {
Michael Jurkabdf78552011-10-31 14:34:25 -0700422 bg = mNormalBackground;
Adam Cohenf34bab52010-09-30 14:11:56 -0700423 }
Michael Jurka33945b22010-12-21 18:19:38 -0800424
425 bg.setAlpha((int) (mBackgroundAlpha * mBackgroundAlphaMultiplier * 255));
426 bg.setBounds(mBackgroundRect);
427 bg.draw(canvas);
Michael Jurkaa63c4522010-08-19 13:52:27 -0700428 }
Romain Guya6abce82009-11-10 02:54:41 -0800429
Patrick Dubroy8e58e912010-10-14 13:21:48 -0700430 final Paint paint = mDragOutlinePaint;
Joe Onorato4be866d2010-10-10 11:26:02 -0700431 for (int i = 0; i < mDragOutlines.length; i++) {
Chet Haase472b2812010-10-14 07:02:04 -0700432 final float alpha = mDragOutlineAlphas[i];
Joe Onorato4be866d2010-10-10 11:26:02 -0700433 if (alpha > 0) {
Adam Cohend41fbf52012-02-16 23:53:59 -0800434 final Rect r = mDragOutlines[i];
Winson Chung3a6e7f32013-10-09 15:50:52 -0700435 mTempRect.set(r);
436 Utilities.scaleRectAboutCenter(mTempRect, getChildrenScale());
Joe Onorato4be866d2010-10-10 11:26:02 -0700437 final Bitmap b = (Bitmap) mDragOutlineAnims[i].getTag();
Chet Haase472b2812010-10-14 07:02:04 -0700438 paint.setAlpha((int)(alpha + .5f));
Winson Chung3a6e7f32013-10-09 15:50:52 -0700439 canvas.drawBitmap(b, null, mTempRect, paint);
Winson Chung150fbab2010-09-29 17:14:26 -0700440 }
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700441 }
Patrick Dubroy96864c32011-03-10 17:17:23 -0800442
443 // We draw the pressed or focused BubbleTextView's background in CellLayout because it
444 // requires an expanded clip rect (due to the glow's blur radius)
445 if (mPressedOrFocusedIcon != null) {
446 final int padding = mPressedOrFocusedIcon.getPressedOrFocusedBackgroundPadding();
447 final Bitmap b = mPressedOrFocusedIcon.getPressedOrFocusedBackground();
448 if (b != null) {
Winson Chung3a6e7f32013-10-09 15:50:52 -0700449 int offset = getMeasuredWidth() - getPaddingLeft() - getPaddingRight() -
450 (mCountX * mCellWidth);
451 int left = getPaddingLeft() + (int) Math.ceil(offset / 2f);
452 int top = getPaddingTop();
Patrick Dubroy96864c32011-03-10 17:17:23 -0800453 canvas.drawBitmap(b,
Winson Chung3a6e7f32013-10-09 15:50:52 -0700454 mPressedOrFocusedIcon.getLeft() + left - padding,
455 mPressedOrFocusedIcon.getTop() + top - padding,
Patrick Dubroy96864c32011-03-10 17:17:23 -0800456 null);
457 }
458 }
Adam Cohen69ce2e52011-07-03 19:25:21 -0700459
Adam Cohen482ed822012-03-02 14:15:13 -0800460 if (DEBUG_VISUALIZE_OCCUPIED) {
461 int[] pt = new int[2];
462 ColorDrawable cd = new ColorDrawable(Color.RED);
Adam Cohene7587d22012-05-24 18:50:02 -0700463 cd.setBounds(0, 0, mCellWidth, mCellHeight);
Adam Cohen482ed822012-03-02 14:15:13 -0800464 for (int i = 0; i < mCountX; i++) {
465 for (int j = 0; j < mCountY; j++) {
466 if (mOccupied[i][j]) {
467 cellToPoint(i, j, pt);
468 canvas.save();
469 canvas.translate(pt[0], pt[1]);
470 cd.draw(canvas);
471 canvas.restore();
472 }
473 }
474 }
475 }
476
Andrew Flynn850d2e72012-04-26 16:51:20 -0700477 int previewOffset = FolderRingAnimator.sPreviewSize;
478
Adam Cohen69ce2e52011-07-03 19:25:21 -0700479 // The folder outer / inner ring image(s)
Winson Chung5f8afe62013-08-12 16:19:28 -0700480 LauncherAppState app = LauncherAppState.getInstance();
481 DeviceProfile grid = app.getDynamicGrid().getDeviceProfile();
Adam Cohen69ce2e52011-07-03 19:25:21 -0700482 for (int i = 0; i < mFolderOuterRings.size(); i++) {
483 FolderRingAnimator fra = mFolderOuterRings.get(i);
484
Adam Cohen5108bc02013-09-20 17:04:51 -0700485 Drawable d;
486 int width, height;
Adam Cohen69ce2e52011-07-03 19:25:21 -0700487 cellToPoint(fra.mCellX, fra.mCellY, mTempLocation);
Winson Chung5f8afe62013-08-12 16:19:28 -0700488 View child = getChildAt(fra.mCellX, fra.mCellY);
Adam Cohen558f1c22013-10-09 15:15:24 -0700489
Winson Chung89f97052013-09-20 11:32:26 -0700490 if (child != null) {
Adam Cohen558f1c22013-10-09 15:15:24 -0700491 int centerX = mTempLocation[0] + mCellWidth / 2;
492 int centerY = mTempLocation[1] + previewOffset / 2 +
493 child.getPaddingTop() + grid.folderBackgroundOffset;
494
Adam Cohen5108bc02013-09-20 17:04:51 -0700495 // Draw outer ring, if it exists
496 if (FolderIcon.HAS_OUTER_RING) {
497 d = FolderRingAnimator.sSharedOuterRingDrawable;
498 width = (int) (fra.getOuterRingSize() * getChildrenScale());
499 height = width;
500 canvas.save();
501 canvas.translate(centerX - width / 2, centerY - height / 2);
502 d.setBounds(0, 0, width, height);
503 d.draw(canvas);
504 canvas.restore();
505 }
Adam Cohen69ce2e52011-07-03 19:25:21 -0700506
Winson Chung89f97052013-09-20 11:32:26 -0700507 // Draw inner ring
508 d = FolderRingAnimator.sSharedInnerRingDrawable;
509 width = (int) (fra.getInnerRingSize() * getChildrenScale());
510 height = width;
Winson Chung89f97052013-09-20 11:32:26 -0700511 canvas.save();
512 canvas.translate(centerX - width / 2, centerY - width / 2);
513 d.setBounds(0, 0, width, height);
514 d.draw(canvas);
515 canvas.restore();
516 }
Adam Cohen69ce2e52011-07-03 19:25:21 -0700517 }
Adam Cohenc51934b2011-07-26 21:07:43 -0700518
519 if (mFolderLeaveBehindCell[0] >= 0 && mFolderLeaveBehindCell[1] >= 0) {
520 Drawable d = FolderIcon.sSharedFolderLeaveBehind;
521 int width = d.getIntrinsicWidth();
522 int height = d.getIntrinsicHeight();
523
524 cellToPoint(mFolderLeaveBehindCell[0], mFolderLeaveBehindCell[1], mTempLocation);
Winson Chung5f8afe62013-08-12 16:19:28 -0700525 View child = getChildAt(mFolderLeaveBehindCell[0], mFolderLeaveBehindCell[1]);
Winson Chung89f97052013-09-20 11:32:26 -0700526 if (child != null) {
527 int centerX = mTempLocation[0] + mCellWidth / 2;
528 int centerY = mTempLocation[1] + previewOffset / 2 +
529 child.getPaddingTop() + grid.folderBackgroundOffset;
Adam Cohenc51934b2011-07-26 21:07:43 -0700530
Winson Chung89f97052013-09-20 11:32:26 -0700531 canvas.save();
532 canvas.translate(centerX - width / 2, centerY - width / 2);
533 d.setBounds(0, 0, width, height);
534 d.draw(canvas);
535 canvas.restore();
536 }
Adam Cohenc51934b2011-07-26 21:07:43 -0700537 }
Adam Cohen69ce2e52011-07-03 19:25:21 -0700538 }
539
Adam Cohenb5ba0972011-09-07 18:02:31 -0700540 @Override
541 protected void dispatchDraw(Canvas canvas) {
542 super.dispatchDraw(canvas);
543 if (mForegroundAlpha > 0) {
544 mOverScrollForegroundDrawable.setBounds(mForegroundRect);
Adam Cohenb5ba0972011-09-07 18:02:31 -0700545 mOverScrollForegroundDrawable.draw(canvas);
Adam Cohenb5ba0972011-09-07 18:02:31 -0700546 }
547 }
548
Adam Cohen69ce2e52011-07-03 19:25:21 -0700549 public void showFolderAccept(FolderRingAnimator fra) {
550 mFolderOuterRings.add(fra);
551 }
552
553 public void hideFolderAccept(FolderRingAnimator fra) {
554 if (mFolderOuterRings.contains(fra)) {
555 mFolderOuterRings.remove(fra);
556 }
557 invalidate();
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700558 }
559
Adam Cohenc51934b2011-07-26 21:07:43 -0700560 public void setFolderLeaveBehindCell(int x, int y) {
561 mFolderLeaveBehindCell[0] = x;
562 mFolderLeaveBehindCell[1] = y;
563 invalidate();
564 }
565
566 public void clearFolderLeaveBehind() {
567 mFolderLeaveBehindCell[0] = -1;
568 mFolderLeaveBehindCell[1] = -1;
569 invalidate();
570 }
571
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700572 @Override
Michael Jurkae6235dd2011-10-04 15:02:05 -0700573 public boolean shouldDelayChildPressedState() {
574 return false;
575 }
576
Adam Cohen1462de32012-07-24 22:34:36 -0700577 public void restoreInstanceState(SparseArray<Parcelable> states) {
Sunny Goyal33a152f2014-07-22 12:13:14 -0700578 try {
579 dispatchRestoreInstanceState(states);
580 } catch (IllegalArgumentException ex) {
581 if (LauncherAppState.isDogfoodBuild()) {
582 throw ex;
583 }
584 // Mismatched viewId / viewType preventing restore. Skip restore on production builds.
585 Log.e(TAG, "Ignoring an error while restoring a view instance state", ex);
586 }
Adam Cohen1462de32012-07-24 22:34:36 -0700587 }
588
Michael Jurkae6235dd2011-10-04 15:02:05 -0700589 @Override
Jeff Sharkey83f111d2009-04-20 21:03:13 -0700590 public void cancelLongPress() {
591 super.cancelLongPress();
592
593 // Cancel long press for all children
594 final int count = getChildCount();
595 for (int i = 0; i < count; i++) {
596 final View child = getChildAt(i);
597 child.cancelLongPress();
598 }
599 }
600
Michael Jurkadee05892010-07-27 10:01:56 -0700601 public void setOnInterceptTouchListener(View.OnTouchListener listener) {
602 mInterceptTouchListener = listener;
603 }
604
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800605 int getCountX() {
Adam Cohend22015c2010-07-26 22:02:18 -0700606 return mCountX;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800607 }
608
609 int getCountY() {
Adam Cohend22015c2010-07-26 22:02:18 -0700610 return mCountY;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800611 }
612
Andrew Flynn0dca1ec2012-02-29 13:33:22 -0800613 public void setIsHotseat(boolean isHotseat) {
614 mIsHotseat = isHotseat;
Winson Chung5f8afe62013-08-12 16:19:28 -0700615 mShortcutsAndWidgets.setIsHotseat(isHotseat);
Andrew Flynn0dca1ec2012-02-29 13:33:22 -0800616 }
617
Andrew Flynn0dca1ec2012-02-29 13:33:22 -0800618 public boolean addViewToCellLayout(View child, int index, int childId, LayoutParams params,
Andrew Flynn850d2e72012-04-26 16:51:20 -0700619 boolean markCells) {
Winson Chungaafa03c2010-06-11 17:34:16 -0700620 final LayoutParams lp = params;
621
Andrew Flynnde38e422012-05-08 11:22:15 -0700622 // Hotseat icons - remove text
Andrew Flynn0dca1ec2012-02-29 13:33:22 -0800623 if (child instanceof BubbleTextView) {
624 BubbleTextView bubbleChild = (BubbleTextView) child;
Winson Chung5f8afe62013-08-12 16:19:28 -0700625 bubbleChild.setTextVisibility(!mIsHotseat);
Andrew Flynn0dca1ec2012-02-29 13:33:22 -0800626 }
627
Adam Cohen307fe232012-08-16 17:55:58 -0700628 child.setScaleX(getChildrenScale());
629 child.setScaleY(getChildrenScale());
630
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800631 // Generate an id for each view, this assumes we have at most 256x256 cells
632 // per workspace screen
Adam Cohend22015c2010-07-26 22:02:18 -0700633 if (lp.cellX >= 0 && lp.cellX <= mCountX - 1 && lp.cellY >= 0 && lp.cellY <= mCountY - 1) {
Winson Chungaafa03c2010-06-11 17:34:16 -0700634 // If the horizontal or vertical span is set to -1, it is taken to
635 // mean that it spans the extent of the CellLayout
Adam Cohend22015c2010-07-26 22:02:18 -0700636 if (lp.cellHSpan < 0) lp.cellHSpan = mCountX;
637 if (lp.cellVSpan < 0) lp.cellVSpan = mCountY;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800638
Winson Chungaafa03c2010-06-11 17:34:16 -0700639 child.setId(childId);
640
Michael Jurkaa52570f2012-03-20 03:18:20 -0700641 mShortcutsAndWidgets.addView(child, index, lp);
Michael Jurkadee05892010-07-27 10:01:56 -0700642
Michael Jurkaf3ca3ab2010-10-20 17:08:24 -0700643 if (markCells) markCellsAsOccupiedForView(child);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700644
Winson Chungaafa03c2010-06-11 17:34:16 -0700645 return true;
646 }
647 return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800648 }
Michael Jurka3e7c7632010-10-02 16:01:03 -0700649
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800650 @Override
Michael Jurka0280c3b2010-09-17 15:00:07 -0700651 public void removeAllViews() {
652 clearOccupiedCells();
Michael Jurkaa52570f2012-03-20 03:18:20 -0700653 mShortcutsAndWidgets.removeAllViews();
Michael Jurka0280c3b2010-09-17 15:00:07 -0700654 }
655
656 @Override
657 public void removeAllViewsInLayout() {
Michael Jurkaa52570f2012-03-20 03:18:20 -0700658 if (mShortcutsAndWidgets.getChildCount() > 0) {
Michael Jurka7cfc2822011-08-02 20:19:24 -0700659 clearOccupiedCells();
Michael Jurkaa52570f2012-03-20 03:18:20 -0700660 mShortcutsAndWidgets.removeAllViewsInLayout();
Michael Jurka7cfc2822011-08-02 20:19:24 -0700661 }
Michael Jurka0280c3b2010-09-17 15:00:07 -0700662 }
663
Michael Jurkaf3ca3ab2010-10-20 17:08:24 -0700664 public void removeViewWithoutMarkingCells(View view) {
Michael Jurkaa52570f2012-03-20 03:18:20 -0700665 mShortcutsAndWidgets.removeView(view);
Michael Jurkaf3ca3ab2010-10-20 17:08:24 -0700666 }
667
Michael Jurka0280c3b2010-09-17 15:00:07 -0700668 @Override
669 public void removeView(View view) {
670 markCellsAsUnoccupiedForView(view);
Michael Jurkaa52570f2012-03-20 03:18:20 -0700671 mShortcutsAndWidgets.removeView(view);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700672 }
673
674 @Override
675 public void removeViewAt(int index) {
Michael Jurkaa52570f2012-03-20 03:18:20 -0700676 markCellsAsUnoccupiedForView(mShortcutsAndWidgets.getChildAt(index));
677 mShortcutsAndWidgets.removeViewAt(index);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700678 }
679
680 @Override
681 public void removeViewInLayout(View view) {
682 markCellsAsUnoccupiedForView(view);
Michael Jurkaa52570f2012-03-20 03:18:20 -0700683 mShortcutsAndWidgets.removeViewInLayout(view);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700684 }
685
686 @Override
687 public void removeViews(int start, int count) {
688 for (int i = start; i < start + count; i++) {
Michael Jurkaa52570f2012-03-20 03:18:20 -0700689 markCellsAsUnoccupiedForView(mShortcutsAndWidgets.getChildAt(i));
Michael Jurka0280c3b2010-09-17 15:00:07 -0700690 }
Michael Jurkaa52570f2012-03-20 03:18:20 -0700691 mShortcutsAndWidgets.removeViews(start, count);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700692 }
693
694 @Override
695 public void removeViewsInLayout(int start, int count) {
696 for (int i = start; i < start + count; i++) {
Michael Jurkaa52570f2012-03-20 03:18:20 -0700697 markCellsAsUnoccupiedForView(mShortcutsAndWidgets.getChildAt(i));
Michael Jurka0280c3b2010-09-17 15:00:07 -0700698 }
Michael Jurkaa52570f2012-03-20 03:18:20 -0700699 mShortcutsAndWidgets.removeViewsInLayout(start, count);
Michael Jurkaabded662011-03-04 12:06:57 -0800700 }
701
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800702 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800703 public boolean onInterceptTouchEvent(MotionEvent ev) {
Adam Cohenc1997fd2011-08-15 18:26:39 -0700704 // First we clear the tag to ensure that on every touch down we start with a fresh slate,
705 // even in the case where we return early. Not clearing here was causing bugs whereby on
706 // long-press we'd end up picking up an item from a previous drag operation.
Michael Jurkadee05892010-07-27 10:01:56 -0700707 if (mInterceptTouchListener != null && mInterceptTouchListener.onTouch(this, ev)) {
708 return true;
709 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800710
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800711 return false;
712 }
713
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700714 /**
Winson Chungaafa03c2010-06-11 17:34:16 -0700715 * Given a point, return the cell that strictly encloses that point
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800716 * @param x X coordinate of the point
717 * @param y Y coordinate of the point
718 * @param result Array of 2 ints to hold the x and y coordinate of the cell
719 */
720 void pointToCellExact(int x, int y, int[] result) {
Winson Chung4b825dcd2011-06-19 12:41:22 -0700721 final int hStartPadding = getPaddingLeft();
722 final int vStartPadding = getPaddingTop();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800723
724 result[0] = (x - hStartPadding) / (mCellWidth + mWidthGap);
725 result[1] = (y - vStartPadding) / (mCellHeight + mHeightGap);
726
Adam Cohend22015c2010-07-26 22:02:18 -0700727 final int xAxis = mCountX;
728 final int yAxis = mCountY;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800729
730 if (result[0] < 0) result[0] = 0;
731 if (result[0] >= xAxis) result[0] = xAxis - 1;
732 if (result[1] < 0) result[1] = 0;
733 if (result[1] >= yAxis) result[1] = yAxis - 1;
734 }
Winson Chungaafa03c2010-06-11 17:34:16 -0700735
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800736 /**
737 * Given a point, return the cell that most closely encloses that point
738 * @param x X coordinate of the point
739 * @param y Y coordinate of the point
740 * @param result Array of 2 ints to hold the x and y coordinate of the cell
741 */
742 void pointToCellRounded(int x, int y, int[] result) {
743 pointToCellExact(x + (mCellWidth / 2), y + (mCellHeight / 2), result);
744 }
745
746 /**
747 * Given a cell coordinate, return the point that represents the upper left corner of that cell
Winson Chungaafa03c2010-06-11 17:34:16 -0700748 *
749 * @param cellX X coordinate of the cell
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800750 * @param cellY Y coordinate of the cell
Winson Chungaafa03c2010-06-11 17:34:16 -0700751 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800752 * @param result Array of 2 ints to hold the x and y coordinate of the point
753 */
754 void cellToPoint(int cellX, int cellY, int[] result) {
Winson Chung4b825dcd2011-06-19 12:41:22 -0700755 final int hStartPadding = getPaddingLeft();
756 final int vStartPadding = getPaddingTop();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800757
758 result[0] = hStartPadding + cellX * (mCellWidth + mWidthGap);
759 result[1] = vStartPadding + cellY * (mCellHeight + mHeightGap);
760 }
761
Adam Cohene3e27a82011-04-15 12:07:39 -0700762 /**
Adam Cohen482ed822012-03-02 14:15:13 -0800763 * Given a cell coordinate, return the point that represents the center of the cell
Adam Cohene3e27a82011-04-15 12:07:39 -0700764 *
765 * @param cellX X coordinate of the cell
766 * @param cellY Y coordinate of the cell
767 *
768 * @param result Array of 2 ints to hold the x and y coordinate of the point
769 */
770 void cellToCenterPoint(int cellX, int cellY, int[] result) {
Adam Cohen47a876d2012-03-19 13:21:41 -0700771 regionToCenterPoint(cellX, cellY, 1, 1, result);
772 }
773
774 /**
775 * Given a cell coordinate and span return the point that represents the center of the regio
776 *
777 * @param cellX X coordinate of the cell
778 * @param cellY Y coordinate of the cell
779 *
780 * @param result Array of 2 ints to hold the x and y coordinate of the point
781 */
782 void regionToCenterPoint(int cellX, int cellY, int spanX, int spanY, int[] result) {
Winson Chung4b825dcd2011-06-19 12:41:22 -0700783 final int hStartPadding = getPaddingLeft();
784 final int vStartPadding = getPaddingTop();
Adam Cohen47a876d2012-03-19 13:21:41 -0700785 result[0] = hStartPadding + cellX * (mCellWidth + mWidthGap) +
786 (spanX * mCellWidth + (spanX - 1) * mWidthGap) / 2;
787 result[1] = vStartPadding + cellY * (mCellHeight + mHeightGap) +
788 (spanY * mCellHeight + (spanY - 1) * mHeightGap) / 2;
Adam Cohene3e27a82011-04-15 12:07:39 -0700789 }
790
Adam Cohen19f37922012-03-21 11:59:11 -0700791 /**
792 * Given a cell coordinate and span fills out a corresponding pixel rect
793 *
794 * @param cellX X coordinate of the cell
795 * @param cellY Y coordinate of the cell
796 * @param result Rect in which to write the result
797 */
798 void regionToRect(int cellX, int cellY, int spanX, int spanY, Rect result) {
799 final int hStartPadding = getPaddingLeft();
800 final int vStartPadding = getPaddingTop();
801 final int left = hStartPadding + cellX * (mCellWidth + mWidthGap);
802 final int top = vStartPadding + cellY * (mCellHeight + mHeightGap);
803 result.set(left, top, left + (spanX * mCellWidth + (spanX - 1) * mWidthGap),
804 top + (spanY * mCellHeight + (spanY - 1) * mHeightGap));
805 }
806
Adam Cohen482ed822012-03-02 14:15:13 -0800807 public float getDistanceFromCell(float x, float y, int[] cell) {
808 cellToCenterPoint(cell[0], cell[1], mTmpPoint);
809 float distance = (float) Math.sqrt( Math.pow(x - mTmpPoint[0], 2) +
810 Math.pow(y - mTmpPoint[1], 2));
811 return distance;
812 }
813
Romain Guy84f296c2009-11-04 15:00:44 -0800814 int getCellWidth() {
815 return mCellWidth;
816 }
817
818 int getCellHeight() {
819 return mCellHeight;
820 }
821
Adam Cohend4844c32011-02-18 19:25:06 -0800822 int getWidthGap() {
823 return mWidthGap;
824 }
825
826 int getHeightGap() {
827 return mHeightGap;
828 }
829
Adam Cohen7f4eabe2011-04-21 16:19:16 -0700830 Rect getContentRect(Rect r) {
831 if (r == null) {
832 r = new Rect();
833 }
834 int left = getPaddingLeft();
835 int top = getPaddingTop();
Michael Jurka8b805b12012-04-18 14:23:14 -0700836 int right = left + getWidth() - getPaddingLeft() - getPaddingRight();
837 int bottom = top + getHeight() - getPaddingTop() - getPaddingBottom();
Adam Cohen7f4eabe2011-04-21 16:19:16 -0700838 r.set(left, top, right, bottom);
839 return r;
840 }
841
Winson Chungfe411c82013-09-26 16:07:17 -0700842 /** Return a rect that has the cellWidth/cellHeight (left, top), and
843 * widthGap/heightGap (right, bottom) */
Winson Chung66700732013-08-20 16:56:15 -0700844 static void getMetrics(Rect metrics, int paddedMeasureWidth,
845 int paddedMeasureHeight, int countX, int countY) {
Winson Chung5f8afe62013-08-12 16:19:28 -0700846 LauncherAppState app = LauncherAppState.getInstance();
847 DeviceProfile grid = app.getDynamicGrid().getDeviceProfile();
Winson Chung66700732013-08-20 16:56:15 -0700848 metrics.set(grid.calculateCellWidth(paddedMeasureWidth, countX),
849 grid.calculateCellHeight(paddedMeasureHeight, countY), 0, 0);
Adam Cohenf4bd5792012-04-27 11:35:29 -0700850 }
851
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700852 public void setFixedSize(int width, int height) {
853 mFixedWidth = width;
854 mFixedHeight = height;
855 }
856
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800857 @Override
858 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
Winson Chung5f8afe62013-08-12 16:19:28 -0700859 LauncherAppState app = LauncherAppState.getInstance();
860 DeviceProfile grid = app.getDynamicGrid().getDeviceProfile();
861
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800862 int widthSpecMode = MeasureSpec.getMode(widthMeasureSpec);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800863 int heightSpecMode = MeasureSpec.getMode(heightMeasureSpec);
Winson Chung5f8afe62013-08-12 16:19:28 -0700864 int widthSize = MeasureSpec.getSize(widthMeasureSpec);
865 int heightSize = MeasureSpec.getSize(heightMeasureSpec);
Winson Chung2d75f122013-09-23 16:53:31 -0700866 int childWidthSize = widthSize - (getPaddingLeft() + getPaddingRight());
867 int childHeightSize = heightSize - (getPaddingTop() + getPaddingBottom());
Winson Chung11a1a532013-09-13 11:14:45 -0700868 if (mFixedCellWidth < 0 || mFixedCellHeight < 0) {
Winson Chung2d75f122013-09-23 16:53:31 -0700869 int cw = grid.calculateCellWidth(childWidthSize, mCountX);
870 int ch = grid.calculateCellHeight(childHeightSize, mCountY);
Winson Chung11a1a532013-09-13 11:14:45 -0700871 if (cw != mCellWidth || ch != mCellHeight) {
872 mCellWidth = cw;
873 mCellHeight = ch;
874 mShortcutsAndWidgets.setCellDimensions(mCellWidth, mCellHeight, mWidthGap,
875 mHeightGap, mCountX, mCountY);
876 }
Winson Chung5f8afe62013-08-12 16:19:28 -0700877 }
Winson Chungaafa03c2010-06-11 17:34:16 -0700878
Winson Chung2d75f122013-09-23 16:53:31 -0700879 int newWidth = childWidthSize;
880 int newHeight = childHeightSize;
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700881 if (mFixedWidth > 0 && mFixedHeight > 0) {
882 newWidth = mFixedWidth;
883 newHeight = mFixedHeight;
884 } else if (widthSpecMode == MeasureSpec.UNSPECIFIED || heightSpecMode == MeasureSpec.UNSPECIFIED) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800885 throw new RuntimeException("CellLayout cannot have UNSPECIFIED dimensions");
886 }
887
Adam Cohend22015c2010-07-26 22:02:18 -0700888 int numWidthGaps = mCountX - 1;
889 int numHeightGaps = mCountY - 1;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800890
Adam Cohen234c4cd2011-07-17 21:03:04 -0700891 if (mOriginalWidthGap < 0 || mOriginalHeightGap < 0) {
Winson Chung2d75f122013-09-23 16:53:31 -0700892 int hSpace = childWidthSize;
893 int vSpace = childHeightSize;
Adam Cohenf4bd5792012-04-27 11:35:29 -0700894 int hFreeSpace = hSpace - (mCountX * mCellWidth);
895 int vFreeSpace = vSpace - (mCountY * mCellHeight);
Winson Chung4b825dcd2011-06-19 12:41:22 -0700896 mWidthGap = Math.min(mMaxGap, numWidthGaps > 0 ? (hFreeSpace / numWidthGaps) : 0);
897 mHeightGap = Math.min(mMaxGap,numHeightGaps > 0 ? (vFreeSpace / numHeightGaps) : 0);
Winson Chung5f8afe62013-08-12 16:19:28 -0700898 mShortcutsAndWidgets.setCellDimensions(mCellWidth, mCellHeight, mWidthGap,
899 mHeightGap, mCountX, mCountY);
Adam Cohen234c4cd2011-07-17 21:03:04 -0700900 } else {
901 mWidthGap = mOriginalWidthGap;
902 mHeightGap = mOriginalHeightGap;
Winson Chungece7f5b2010-10-22 14:54:12 -0700903 }
Michael Jurka8c920dd2011-01-20 14:16:56 -0800904 int count = getChildCount();
Winson Chung5f8afe62013-08-12 16:19:28 -0700905 int maxWidth = 0;
906 int maxHeight = 0;
Michael Jurka8c920dd2011-01-20 14:16:56 -0800907 for (int i = 0; i < count; i++) {
908 View child = getChildAt(i);
Winson Chung2d75f122013-09-23 16:53:31 -0700909 int childWidthMeasureSpec = MeasureSpec.makeMeasureSpec(newWidth,
910 MeasureSpec.EXACTLY);
911 int childheightMeasureSpec = MeasureSpec.makeMeasureSpec(newHeight,
912 MeasureSpec.EXACTLY);
Michael Jurka8c920dd2011-01-20 14:16:56 -0800913 child.measure(childWidthMeasureSpec, childheightMeasureSpec);
Winson Chung5f8afe62013-08-12 16:19:28 -0700914 maxWidth = Math.max(maxWidth, child.getMeasuredWidth());
915 maxHeight = Math.max(maxHeight, child.getMeasuredHeight());
Michael Jurka8c920dd2011-01-20 14:16:56 -0800916 }
Winson Chung2d75f122013-09-23 16:53:31 -0700917 if (mFixedWidth > 0 && mFixedHeight > 0) {
918 setMeasuredDimension(maxWidth, maxHeight);
919 } else {
920 setMeasuredDimension(widthSize, heightSize);
921 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800922 }
923
924 @Override
Michael Jurka28750fb2010-09-24 17:43:49 -0700925 protected void onLayout(boolean changed, int l, int t, int r, int b) {
Winson Chung38848ca2013-10-08 12:03:44 -0700926 int offset = getMeasuredWidth() - getPaddingLeft() - getPaddingRight() -
927 (mCountX * mCellWidth);
928 int left = getPaddingLeft() + (int) Math.ceil(offset / 2f);
929 int top = getPaddingTop();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800930 int count = getChildCount();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800931 for (int i = 0; i < count; i++) {
Michael Jurka8c920dd2011-01-20 14:16:56 -0800932 View child = getChildAt(i);
Winson Chung2d75f122013-09-23 16:53:31 -0700933 child.layout(left, top,
934 left + r - l,
935 top + b - t);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800936 }
937 }
938
939 @Override
Michael Jurkadee05892010-07-27 10:01:56 -0700940 protected void onSizeChanged(int w, int h, int oldw, int oldh) {
941 super.onSizeChanged(w, h, oldw, oldh);
Winson Chung82a9bd22013-10-08 16:02:34 -0700942
943 // Expand the background drawing bounds by the padding baked into the background drawable
944 Rect padding = new Rect();
945 mNormalBackground.getPadding(padding);
946 mBackgroundRect.set(-padding.left, -padding.top, w + padding.right, h + padding.bottom);
947
Adam Cohenb5ba0972011-09-07 18:02:31 -0700948 mForegroundRect.set(mForegroundPadding, mForegroundPadding,
Adam Cohen215b4162012-08-30 13:14:08 -0700949 w - mForegroundPadding, h - mForegroundPadding);
Michael Jurkadee05892010-07-27 10:01:56 -0700950 }
951
952 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800953 protected void setChildrenDrawingCacheEnabled(boolean enabled) {
Michael Jurkaa52570f2012-03-20 03:18:20 -0700954 mShortcutsAndWidgets.setChildrenDrawingCacheEnabled(enabled);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800955 }
956
957 @Override
958 protected void setChildrenDrawnWithCacheEnabled(boolean enabled) {
Michael Jurkaa52570f2012-03-20 03:18:20 -0700959 mShortcutsAndWidgets.setChildrenDrawnWithCacheEnabled(enabled);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800960 }
961
Michael Jurka5f1c5092010-09-03 14:15:02 -0700962 public float getBackgroundAlpha() {
963 return mBackgroundAlpha;
Michael Jurkadee05892010-07-27 10:01:56 -0700964 }
965
Adam Cohen1b0aaac2010-10-28 11:11:18 -0700966 public void setBackgroundAlphaMultiplier(float multiplier) {
Michael Jurkaa3d30ad2012-05-08 13:43:43 -0700967 if (mBackgroundAlphaMultiplier != multiplier) {
968 mBackgroundAlphaMultiplier = multiplier;
969 invalidate();
970 }
Adam Cohen1b0aaac2010-10-28 11:11:18 -0700971 }
972
Adam Cohenddb82192010-11-10 16:32:54 -0800973 public float getBackgroundAlphaMultiplier() {
974 return mBackgroundAlphaMultiplier;
975 }
976
Michael Jurka5f1c5092010-09-03 14:15:02 -0700977 public void setBackgroundAlpha(float alpha) {
Michael Jurkaafaa0502011-12-13 18:22:50 -0800978 if (mBackgroundAlpha != alpha) {
979 mBackgroundAlpha = alpha;
980 invalidate();
981 }
Michael Jurkadee05892010-07-27 10:01:56 -0700982 }
983
Michael Jurkaa52570f2012-03-20 03:18:20 -0700984 public void setShortcutAndWidgetAlpha(float alpha) {
Michael Jurka0142d492010-08-25 17:46:15 -0700985 final int childCount = getChildCount();
986 for (int i = 0; i < childCount; i++) {
Michael Jurkadee05892010-07-27 10:01:56 -0700987 getChildAt(i).setAlpha(alpha);
988 }
989 }
990
Michael Jurkaa52570f2012-03-20 03:18:20 -0700991 public ShortcutAndWidgetContainer getShortcutsAndWidgets() {
992 if (getChildCount() > 0) {
993 return (ShortcutAndWidgetContainer) getChildAt(0);
994 }
995 return null;
996 }
997
Patrick Dubroy440c3602010-07-13 17:50:32 -0700998 public View getChildAt(int x, int y) {
Michael Jurkaa52570f2012-03-20 03:18:20 -0700999 return mShortcutsAndWidgets.getChildAt(x, y);
Patrick Dubroy440c3602010-07-13 17:50:32 -07001000 }
1001
Adam Cohen76fc0852011-06-17 13:26:23 -07001002 public boolean animateChildToPosition(final View child, int cellX, int cellY, int duration,
Adam Cohen482ed822012-03-02 14:15:13 -08001003 int delay, boolean permanent, boolean adjustOccupied) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07001004 ShortcutAndWidgetContainer clc = getShortcutsAndWidgets();
Adam Cohen482ed822012-03-02 14:15:13 -08001005 boolean[][] occupied = mOccupied;
1006 if (!permanent) {
1007 occupied = mTmpOccupied;
1008 }
1009
Adam Cohen19f37922012-03-21 11:59:11 -07001010 if (clc.indexOfChild(child) != -1) {
Adam Cohenbfbfd262011-06-13 16:55:12 -07001011 final LayoutParams lp = (LayoutParams) child.getLayoutParams();
1012 final ItemInfo info = (ItemInfo) child.getTag();
1013
1014 // We cancel any existing animations
1015 if (mReorderAnimators.containsKey(lp)) {
1016 mReorderAnimators.get(lp).cancel();
1017 mReorderAnimators.remove(lp);
1018 }
1019
Adam Cohen482ed822012-03-02 14:15:13 -08001020 final int oldX = lp.x;
1021 final int oldY = lp.y;
1022 if (adjustOccupied) {
1023 occupied[lp.cellX][lp.cellY] = false;
1024 occupied[cellX][cellY] = true;
1025 }
Adam Cohenbfbfd262011-06-13 16:55:12 -07001026 lp.isLockedToGrid = true;
Adam Cohen482ed822012-03-02 14:15:13 -08001027 if (permanent) {
1028 lp.cellX = info.cellX = cellX;
1029 lp.cellY = info.cellY = cellY;
1030 } else {
1031 lp.tmpCellX = cellX;
1032 lp.tmpCellY = cellY;
1033 }
Adam Cohenbfbfd262011-06-13 16:55:12 -07001034 clc.setupLp(lp);
1035 lp.isLockedToGrid = false;
Adam Cohen482ed822012-03-02 14:15:13 -08001036 final int newX = lp.x;
1037 final int newY = lp.y;
Adam Cohenbfbfd262011-06-13 16:55:12 -07001038
Adam Cohen76fc0852011-06-17 13:26:23 -07001039 lp.x = oldX;
1040 lp.y = oldY;
Adam Cohen76fc0852011-06-17 13:26:23 -07001041
Adam Cohen482ed822012-03-02 14:15:13 -08001042 // Exit early if we're not actually moving the view
1043 if (oldX == newX && oldY == newY) {
1044 lp.isLockedToGrid = true;
1045 return true;
1046 }
1047
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001048 ValueAnimator va = LauncherAnimUtils.ofFloat(child, 0f, 1f);
Adam Cohen482ed822012-03-02 14:15:13 -08001049 va.setDuration(duration);
1050 mReorderAnimators.put(lp, va);
1051
1052 va.addUpdateListener(new AnimatorUpdateListener() {
1053 @Override
Adam Cohenbfbfd262011-06-13 16:55:12 -07001054 public void onAnimationUpdate(ValueAnimator animation) {
Adam Cohen482ed822012-03-02 14:15:13 -08001055 float r = ((Float) animation.getAnimatedValue()).floatValue();
Adam Cohen19f37922012-03-21 11:59:11 -07001056 lp.x = (int) ((1 - r) * oldX + r * newX);
1057 lp.y = (int) ((1 - r) * oldY + r * newY);
Adam Cohen6b8a02d2012-03-22 15:13:40 -07001058 child.requestLayout();
Adam Cohenbfbfd262011-06-13 16:55:12 -07001059 }
1060 });
Adam Cohen482ed822012-03-02 14:15:13 -08001061 va.addListener(new AnimatorListenerAdapter() {
Adam Cohenbfbfd262011-06-13 16:55:12 -07001062 boolean cancelled = false;
1063 public void onAnimationEnd(Animator animation) {
1064 // If the animation was cancelled, it means that another animation
1065 // has interrupted this one, and we don't want to lock the item into
1066 // place just yet.
1067 if (!cancelled) {
1068 lp.isLockedToGrid = true;
Adam Cohen482ed822012-03-02 14:15:13 -08001069 child.requestLayout();
Adam Cohenbfbfd262011-06-13 16:55:12 -07001070 }
1071 if (mReorderAnimators.containsKey(lp)) {
1072 mReorderAnimators.remove(lp);
1073 }
1074 }
1075 public void onAnimationCancel(Animator animation) {
1076 cancelled = true;
1077 }
1078 });
Adam Cohen482ed822012-03-02 14:15:13 -08001079 va.setStartDelay(delay);
1080 va.start();
Adam Cohenbfbfd262011-06-13 16:55:12 -07001081 return true;
1082 }
1083 return false;
1084 }
1085
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001086 /**
1087 * Estimate where the top left cell of the dragged item will land if it is dropped.
1088 *
1089 * @param originX The X value of the top left corner of the item
1090 * @param originY The Y value of the top left corner of the item
1091 * @param spanX The number of horizontal cells that the item spans
1092 * @param spanY The number of vertical cells that the item spans
1093 * @param result The estimated drop cell X and Y.
1094 */
1095 void estimateDropCell(int originX, int originY, int spanX, int spanY, int[] result) {
Adam Cohend22015c2010-07-26 22:02:18 -07001096 final int countX = mCountX;
1097 final int countY = mCountY;
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001098
Michael Jurkaa63c4522010-08-19 13:52:27 -07001099 // pointToCellRounded takes the top left of a cell but will pad that with
1100 // cellWidth/2 and cellHeight/2 when finding the matching cell
1101 pointToCellRounded(originX, originY, result);
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001102
1103 // If the item isn't fully on this screen, snap to the edges
1104 int rightOverhang = result[0] + spanX - countX;
1105 if (rightOverhang > 0) {
1106 result[0] -= rightOverhang; // Snap to right
1107 }
1108 result[0] = Math.max(0, result[0]); // Snap to left
1109 int bottomOverhang = result[1] + spanY - countY;
1110 if (bottomOverhang > 0) {
1111 result[1] -= bottomOverhang; // Snap to bottom
1112 }
1113 result[1] = Math.max(0, result[1]); // Snap to top
1114 }
1115
Adam Cohen482ed822012-03-02 14:15:13 -08001116 void visualizeDropLocation(View v, Bitmap dragOutline, int originX, int originY, int cellX,
1117 int cellY, int spanX, int spanY, boolean resize, Point dragOffset, Rect dragRegion) {
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -07001118 final int oldDragCellX = mDragCell[0];
1119 final int oldDragCellY = mDragCell[1];
Adam Cohen482ed822012-03-02 14:15:13 -08001120
Adam Cohen2801caf2011-05-13 20:57:39 -07001121 if (dragOutline == null && v == null) {
Adam Cohen2801caf2011-05-13 20:57:39 -07001122 return;
1123 }
1124
Adam Cohen482ed822012-03-02 14:15:13 -08001125 if (cellX != oldDragCellX || cellY != oldDragCellY) {
1126 mDragCell[0] = cellX;
1127 mDragCell[1] = cellY;
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001128 // Find the top left corner of the rect the object will occupy
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001129 final int[] topLeft = mTmpPoint;
Adam Cohen482ed822012-03-02 14:15:13 -08001130 cellToPoint(cellX, cellY, topLeft);
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001131
Joe Onorato4be866d2010-10-10 11:26:02 -07001132 int left = topLeft[0];
1133 int top = topLeft[1];
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001134
Winson Chungb8c69f32011-10-19 21:36:08 -07001135 if (v != null && dragOffset == null) {
Adam Cohen99e8b402011-03-25 19:23:43 -07001136 // When drawing the drag outline, it did not account for margin offsets
1137 // added by the view's parent.
1138 MarginLayoutParams lp = (MarginLayoutParams) v.getLayoutParams();
1139 left += lp.leftMargin;
1140 top += lp.topMargin;
Winson Chung150fbab2010-09-29 17:14:26 -07001141
Adam Cohen99e8b402011-03-25 19:23:43 -07001142 // Offsets due to the size difference between the View and the dragOutline.
1143 // There is a size difference to account for the outer blur, which may lie
1144 // outside the bounds of the view.
Winson Chunga9abd0e2010-10-27 17:18:37 -07001145 top += (v.getHeight() - dragOutline.getHeight()) / 2;
Adam Cohenae915ce2011-08-25 13:47:22 -07001146 // We center about the x axis
1147 left += ((mCellWidth * spanX) + ((spanX - 1) * mWidthGap)
1148 - dragOutline.getWidth()) / 2;
Adam Cohen66396872011-04-15 17:50:36 -07001149 } else {
Winson Chungb8c69f32011-10-19 21:36:08 -07001150 if (dragOffset != null && dragRegion != null) {
1151 // Center the drag region *horizontally* in the cell and apply a drag
1152 // outline offset
1153 left += dragOffset.x + ((mCellWidth * spanX) + ((spanX - 1) * mWidthGap)
1154 - dragRegion.width()) / 2;
Winson Chung69737c32013-10-08 17:00:19 -07001155 int cHeight = getShortcutsAndWidgets().getCellContentHeight();
1156 int cellPaddingY = (int) Math.max(0, ((mCellHeight - cHeight) / 2f));
1157 top += dragOffset.y + cellPaddingY;
Winson Chungb8c69f32011-10-19 21:36:08 -07001158 } else {
1159 // Center the drag outline in the cell
1160 left += ((mCellWidth * spanX) + ((spanX - 1) * mWidthGap)
1161 - dragOutline.getWidth()) / 2;
1162 top += ((mCellHeight * spanY) + ((spanY - 1) * mHeightGap)
1163 - dragOutline.getHeight()) / 2;
1164 }
Winson Chunga9abd0e2010-10-27 17:18:37 -07001165 }
Joe Onorato4be866d2010-10-10 11:26:02 -07001166 final int oldIndex = mDragOutlineCurrent;
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -07001167 mDragOutlineAnims[oldIndex].animateOut();
1168 mDragOutlineCurrent = (oldIndex + 1) % mDragOutlines.length;
Adam Cohend41fbf52012-02-16 23:53:59 -08001169 Rect r = mDragOutlines[mDragOutlineCurrent];
1170 r.set(left, top, left + dragOutline.getWidth(), top + dragOutline.getHeight());
1171 if (resize) {
Adam Cohen482ed822012-03-02 14:15:13 -08001172 cellToRect(cellX, cellY, spanX, spanY, r);
Adam Cohend41fbf52012-02-16 23:53:59 -08001173 }
Winson Chung150fbab2010-09-29 17:14:26 -07001174
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -07001175 mDragOutlineAnims[mDragOutlineCurrent].setTag(dragOutline);
1176 mDragOutlineAnims[mDragOutlineCurrent].animateIn();
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001177 }
1178 }
1179
Adam Cohene0310962011-04-18 16:15:31 -07001180 public void clearDragOutlines() {
1181 final int oldIndex = mDragOutlineCurrent;
1182 mDragOutlineAnims[oldIndex].animateOut();
Adam Cohend41fbf52012-02-16 23:53:59 -08001183 mDragCell[0] = mDragCell[1] = -1;
Adam Cohene0310962011-04-18 16:15:31 -07001184 }
1185
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001186 /**
Jeff Sharkey70864282009-04-07 21:08:40 -07001187 * Find a vacant area that will fit the given bounds nearest the requested
1188 * cell location. Uses Euclidean distance to score multiple vacant areas.
Winson Chungaafa03c2010-06-11 17:34:16 -07001189 *
Romain Guy51afc022009-05-04 18:03:43 -07001190 * @param pixelX The X location at which you want to search for a vacant area.
1191 * @param pixelY The Y location at which you want to search for a vacant area.
Jeff Sharkey70864282009-04-07 21:08:40 -07001192 * @param spanX Horizontal span of the object.
1193 * @param spanY Vertical span of the object.
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001194 * @param result Array in which to place the result, or null (in which case a new array will
1195 * be allocated)
Jeff Sharkey70864282009-04-07 21:08:40 -07001196 * @return The X, Y cell of a vacant area that can contain this object,
1197 * nearest the requested location.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001198 */
Adam Cohend41fbf52012-02-16 23:53:59 -08001199 int[] findNearestVacantArea(int pixelX, int pixelY, int spanX, int spanY,
1200 int[] result) {
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001201 return findNearestVacantArea(pixelX, pixelY, spanX, spanY, null, result);
Michael Jurka6a1435d2010-09-27 17:35:12 -07001202 }
Winson Chungaafa03c2010-06-11 17:34:16 -07001203
Michael Jurka6a1435d2010-09-27 17:35:12 -07001204 /**
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.
Adam Cohend41fbf52012-02-16 23:53:59 -08001210 * @param minSpanX The minimum horizontal span required
1211 * @param minSpanY The minimum vertical span required
1212 * @param spanX Horizontal span of the object.
1213 * @param spanY Vertical span of the object.
1214 * @param result Array in which to place the result, or null (in which case a new array will
1215 * be allocated)
1216 * @return The X, Y cell of a vacant area that can contain this object,
1217 * nearest the requested location.
1218 */
1219 int[] findNearestVacantArea(int pixelX, int pixelY, int minSpanX, int minSpanY, int spanX,
1220 int spanY, int[] result, int[] resultSpan) {
1221 return findNearestVacantArea(pixelX, pixelY, minSpanX, minSpanY, spanX, spanY, null,
1222 result, resultSpan);
1223 }
1224
1225 /**
1226 * Find a vacant area that will fit the given bounds nearest the requested
1227 * cell location. Uses Euclidean distance to score multiple vacant areas.
1228 *
1229 * @param pixelX The X location at which you want to search for a vacant area.
1230 * @param pixelY The Y location at which you want to search for a vacant area.
Michael Jurka6a1435d2010-09-27 17:35:12 -07001231 * @param spanX Horizontal span of the object.
1232 * @param spanY Vertical span of the object.
Adam Cohendf035382011-04-11 17:22:04 -07001233 * @param ignoreOccupied If true, the result can be an occupied cell
1234 * @param result Array in which to place the result, or null (in which case a new array will
1235 * be allocated)
Michael Jurka6a1435d2010-09-27 17:35:12 -07001236 * @return The X, Y cell of a vacant area that can contain this object,
1237 * nearest the requested location.
1238 */
Adam Cohendf035382011-04-11 17:22:04 -07001239 int[] findNearestArea(int pixelX, int pixelY, int spanX, int spanY, View ignoreView,
1240 boolean ignoreOccupied, int[] result) {
Adam Cohend41fbf52012-02-16 23:53:59 -08001241 return findNearestArea(pixelX, pixelY, spanX, spanY,
Adam Cohen482ed822012-03-02 14:15:13 -08001242 spanX, spanY, ignoreView, ignoreOccupied, result, null, mOccupied);
Adam Cohend41fbf52012-02-16 23:53:59 -08001243 }
1244
1245 private final Stack<Rect> mTempRectStack = new Stack<Rect>();
1246 private void lazyInitTempRectStack() {
1247 if (mTempRectStack.isEmpty()) {
1248 for (int i = 0; i < mCountX * mCountY; i++) {
1249 mTempRectStack.push(new Rect());
1250 }
1251 }
1252 }
Adam Cohen482ed822012-03-02 14:15:13 -08001253
Adam Cohend41fbf52012-02-16 23:53:59 -08001254 private void recycleTempRects(Stack<Rect> used) {
1255 while (!used.isEmpty()) {
1256 mTempRectStack.push(used.pop());
1257 }
1258 }
1259
1260 /**
1261 * Find a vacant area that will fit the given bounds nearest the requested
1262 * cell location. Uses Euclidean distance to score multiple vacant areas.
1263 *
1264 * @param pixelX The X location at which you want to search for a vacant area.
1265 * @param pixelY The Y location at which you want to search for a vacant area.
1266 * @param minSpanX The minimum horizontal span required
1267 * @param minSpanY The minimum vertical span required
1268 * @param spanX Horizontal span of the object.
1269 * @param spanY Vertical span of the object.
1270 * @param ignoreOccupied If true, the result can be an occupied cell
1271 * @param result Array in which to place the result, or null (in which case a new array will
1272 * be allocated)
1273 * @return The X, Y cell of a vacant area that can contain this object,
1274 * nearest the requested location.
1275 */
1276 int[] findNearestArea(int pixelX, int pixelY, int minSpanX, int minSpanY, int spanX, int spanY,
Adam Cohen482ed822012-03-02 14:15:13 -08001277 View ignoreView, boolean ignoreOccupied, int[] result, int[] resultSpan,
1278 boolean[][] occupied) {
Adam Cohend41fbf52012-02-16 23:53:59 -08001279 lazyInitTempRectStack();
Michael Jurkac6ee42e2010-09-30 12:04:50 -07001280 // mark space take by ignoreView as available (method checks if ignoreView is null)
Adam Cohen482ed822012-03-02 14:15:13 -08001281 markCellsAsUnoccupiedForView(ignoreView, occupied);
Michael Jurkac6ee42e2010-09-30 12:04:50 -07001282
Adam Cohene3e27a82011-04-15 12:07:39 -07001283 // For items with a spanX / spanY > 1, the passed in point (pixelX, pixelY) corresponds
1284 // to the center of the item, but we are searching based on the top-left cell, so
1285 // we translate the point over to correspond to the top-left.
1286 pixelX -= (mCellWidth + mWidthGap) * (spanX - 1) / 2f;
1287 pixelY -= (mCellHeight + mHeightGap) * (spanY - 1) / 2f;
1288
Jeff Sharkey70864282009-04-07 21:08:40 -07001289 // Keep track of best-scoring drop area
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001290 final int[] bestXY = result != null ? result : new int[2];
Jeff Sharkey70864282009-04-07 21:08:40 -07001291 double bestDistance = Double.MAX_VALUE;
Adam Cohend41fbf52012-02-16 23:53:59 -08001292 final Rect bestRect = new Rect(-1, -1, -1, -1);
1293 final Stack<Rect> validRegions = new Stack<Rect>();
Winson Chungaafa03c2010-06-11 17:34:16 -07001294
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001295 final int countX = mCountX;
1296 final int countY = mCountY;
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001297
Adam Cohend41fbf52012-02-16 23:53:59 -08001298 if (minSpanX <= 0 || minSpanY <= 0 || spanX <= 0 || spanY <= 0 ||
1299 spanX < minSpanX || spanY < minSpanY) {
1300 return bestXY;
1301 }
1302
1303 for (int y = 0; y < countY - (minSpanY - 1); y++) {
Michael Jurkac28de512010-08-13 11:27:44 -07001304 inner:
Adam Cohend41fbf52012-02-16 23:53:59 -08001305 for (int x = 0; x < countX - (minSpanX - 1); x++) {
1306 int ySize = -1;
1307 int xSize = -1;
Adam Cohendf035382011-04-11 17:22:04 -07001308 if (ignoreOccupied) {
Adam Cohend41fbf52012-02-16 23:53:59 -08001309 // First, let's see if this thing fits anywhere
1310 for (int i = 0; i < minSpanX; i++) {
1311 for (int j = 0; j < minSpanY; j++) {
Adam Cohendf035382011-04-11 17:22:04 -07001312 if (occupied[x + i][y + j]) {
Adam Cohendf035382011-04-11 17:22:04 -07001313 continue inner;
1314 }
Michael Jurkac28de512010-08-13 11:27:44 -07001315 }
1316 }
Adam Cohend41fbf52012-02-16 23:53:59 -08001317 xSize = minSpanX;
1318 ySize = minSpanY;
1319
1320 // We know that the item will fit at _some_ acceptable size, now let's see
1321 // how big we can make it. We'll alternate between incrementing x and y spans
1322 // until we hit a limit.
1323 boolean incX = true;
1324 boolean hitMaxX = xSize >= spanX;
1325 boolean hitMaxY = ySize >= spanY;
1326 while (!(hitMaxX && hitMaxY)) {
1327 if (incX && !hitMaxX) {
1328 for (int j = 0; j < ySize; j++) {
1329 if (x + xSize > countX -1 || occupied[x + xSize][y + j]) {
1330 // We can't move out horizontally
1331 hitMaxX = true;
1332 }
1333 }
1334 if (!hitMaxX) {
1335 xSize++;
1336 }
1337 } else if (!hitMaxY) {
1338 for (int i = 0; i < xSize; i++) {
1339 if (y + ySize > countY - 1 || occupied[x + i][y + ySize]) {
1340 // We can't move out vertically
1341 hitMaxY = true;
1342 }
1343 }
1344 if (!hitMaxY) {
1345 ySize++;
1346 }
1347 }
1348 hitMaxX |= xSize >= spanX;
1349 hitMaxY |= ySize >= spanY;
1350 incX = !incX;
1351 }
1352 incX = true;
1353 hitMaxX = xSize >= spanX;
1354 hitMaxY = ySize >= spanY;
Michael Jurkac28de512010-08-13 11:27:44 -07001355 }
Winson Chung0be025d2011-05-23 17:45:09 -07001356 final int[] cellXY = mTmpXY;
Adam Cohene3e27a82011-04-15 12:07:39 -07001357 cellToCenterPoint(x, y, cellXY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001358
Adam Cohend41fbf52012-02-16 23:53:59 -08001359 // We verify that the current rect is not a sub-rect of any of our previous
1360 // candidates. In this case, the current rect is disqualified in favour of the
1361 // containing rect.
1362 Rect currentRect = mTempRectStack.pop();
1363 currentRect.set(x, y, x + xSize, y + ySize);
1364 boolean contained = false;
1365 for (Rect r : validRegions) {
1366 if (r.contains(currentRect)) {
1367 contained = true;
1368 break;
1369 }
1370 }
1371 validRegions.push(currentRect);
Michael Jurkac28de512010-08-13 11:27:44 -07001372 double distance = Math.sqrt(Math.pow(cellXY[0] - pixelX, 2)
1373 + Math.pow(cellXY[1] - pixelY, 2));
Adam Cohen482ed822012-03-02 14:15:13 -08001374
Adam Cohend41fbf52012-02-16 23:53:59 -08001375 if ((distance <= bestDistance && !contained) ||
1376 currentRect.contains(bestRect)) {
Michael Jurkac28de512010-08-13 11:27:44 -07001377 bestDistance = distance;
1378 bestXY[0] = x;
1379 bestXY[1] = y;
Adam Cohend41fbf52012-02-16 23:53:59 -08001380 if (resultSpan != null) {
1381 resultSpan[0] = xSize;
1382 resultSpan[1] = ySize;
1383 }
1384 bestRect.set(currentRect);
Michael Jurkac28de512010-08-13 11:27:44 -07001385 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001386 }
1387 }
Michael Jurkac6ee42e2010-09-30 12:04:50 -07001388 // re-mark space taken by ignoreView as occupied
Adam Cohen482ed822012-03-02 14:15:13 -08001389 markCellsAsOccupiedForView(ignoreView, occupied);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001390
Adam Cohenc0dcf592011-06-01 15:30:43 -07001391 // Return -1, -1 if no suitable location found
1392 if (bestDistance == Double.MAX_VALUE) {
1393 bestXY[0] = -1;
1394 bestXY[1] = -1;
Jeff Sharkey70864282009-04-07 21:08:40 -07001395 }
Adam Cohend41fbf52012-02-16 23:53:59 -08001396 recycleTempRects(validRegions);
Adam Cohenc0dcf592011-06-01 15:30:43 -07001397 return bestXY;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001398 }
Winson Chungaafa03c2010-06-11 17:34:16 -07001399
Adam Cohen482ed822012-03-02 14:15:13 -08001400 /**
1401 * Find a vacant area that will fit the given bounds nearest the requested
1402 * cell location, and will also weigh in a suggested direction vector of the
1403 * desired location. This method computers distance based on unit grid distances,
1404 * not pixel distances.
1405 *
Adam Cohen47a876d2012-03-19 13:21:41 -07001406 * @param cellX The X cell nearest to which you want to search for a vacant area.
1407 * @param cellY The Y cell nearest which you want to search for a vacant area.
Adam Cohen482ed822012-03-02 14:15:13 -08001408 * @param spanX Horizontal span of the object.
1409 * @param spanY Vertical span of the object.
Adam Cohen47a876d2012-03-19 13:21:41 -07001410 * @param direction The favored direction in which the views should move from x, y
1411 * @param exactDirectionOnly If this parameter is true, then only solutions where the direction
1412 * matches exactly. Otherwise we find the best matching direction.
1413 * @param occoupied The array which represents which cells in the CellLayout are occupied
1414 * @param blockOccupied The array which represents which cells in the specified block (cellX,
Winson Chung5f8afe62013-08-12 16:19:28 -07001415 * cellY, spanX, spanY) are occupied. This is used when try to move a group of views.
Adam Cohen482ed822012-03-02 14:15:13 -08001416 * @param result Array in which to place the result, or null (in which case a new array will
1417 * be allocated)
1418 * @return The X, Y cell of a vacant area that can contain this object,
1419 * nearest the requested location.
1420 */
1421 private int[] findNearestArea(int cellX, int cellY, int spanX, int spanY, int[] direction,
Adam Cohen47a876d2012-03-19 13:21:41 -07001422 boolean[][] occupied, boolean blockOccupied[][], int[] result) {
Adam Cohen482ed822012-03-02 14:15:13 -08001423 // Keep track of best-scoring drop area
1424 final int[] bestXY = result != null ? result : new int[2];
1425 float bestDistance = Float.MAX_VALUE;
1426 int bestDirectionScore = Integer.MIN_VALUE;
1427
1428 final int countX = mCountX;
1429 final int countY = mCountY;
1430
1431 for (int y = 0; y < countY - (spanY - 1); y++) {
1432 inner:
1433 for (int x = 0; x < countX - (spanX - 1); x++) {
1434 // First, let's see if this thing fits anywhere
1435 for (int i = 0; i < spanX; i++) {
1436 for (int j = 0; j < spanY; j++) {
Adam Cohen47a876d2012-03-19 13:21:41 -07001437 if (occupied[x + i][y + j] && (blockOccupied == null || blockOccupied[i][j])) {
Adam Cohen482ed822012-03-02 14:15:13 -08001438 continue inner;
1439 }
1440 }
1441 }
1442
1443 float distance = (float)
1444 Math.sqrt((x - cellX) * (x - cellX) + (y - cellY) * (y - cellY));
1445 int[] curDirection = mTmpPoint;
Adam Cohen47a876d2012-03-19 13:21:41 -07001446 computeDirectionVector(x - cellX, y - cellY, curDirection);
1447 // The direction score is just the dot product of the two candidate direction
1448 // and that passed in.
Adam Cohen482ed822012-03-02 14:15:13 -08001449 int curDirectionScore = direction[0] * curDirection[0] +
1450 direction[1] * curDirection[1];
Adam Cohen47a876d2012-03-19 13:21:41 -07001451 boolean exactDirectionOnly = false;
1452 boolean directionMatches = direction[0] == curDirection[0] &&
1453 direction[0] == curDirection[0];
1454 if ((directionMatches || !exactDirectionOnly) &&
1455 Float.compare(distance, bestDistance) < 0 || (Float.compare(distance,
Adam Cohen482ed822012-03-02 14:15:13 -08001456 bestDistance) == 0 && curDirectionScore > bestDirectionScore)) {
1457 bestDistance = distance;
1458 bestDirectionScore = curDirectionScore;
1459 bestXY[0] = x;
1460 bestXY[1] = y;
1461 }
1462 }
1463 }
1464
1465 // Return -1, -1 if no suitable location found
1466 if (bestDistance == Float.MAX_VALUE) {
1467 bestXY[0] = -1;
1468 bestXY[1] = -1;
1469 }
1470 return bestXY;
1471 }
1472
1473 private boolean addViewToTempLocation(View v, Rect rectOccupiedByPotentialDrop,
Adam Cohen8baab352012-03-20 17:39:21 -07001474 int[] direction, ItemConfiguration currentState) {
1475 CellAndSpan c = currentState.map.get(v);
Adam Cohen482ed822012-03-02 14:15:13 -08001476 boolean success = false;
Adam Cohen8baab352012-03-20 17:39:21 -07001477 markCellsForView(c.x, c.y, c.spanX, c.spanY, mTmpOccupied, false);
Adam Cohen482ed822012-03-02 14:15:13 -08001478 markCellsForRect(rectOccupiedByPotentialDrop, mTmpOccupied, true);
1479
Adam Cohen8baab352012-03-20 17:39:21 -07001480 findNearestArea(c.x, c.y, c.spanX, c.spanY, direction, mTmpOccupied, null, mTempLocation);
Adam Cohen482ed822012-03-02 14:15:13 -08001481
1482 if (mTempLocation[0] >= 0 && mTempLocation[1] >= 0) {
Adam Cohen8baab352012-03-20 17:39:21 -07001483 c.x = mTempLocation[0];
1484 c.y = mTempLocation[1];
Adam Cohen482ed822012-03-02 14:15:13 -08001485 success = true;
Adam Cohen482ed822012-03-02 14:15:13 -08001486 }
Adam Cohen8baab352012-03-20 17:39:21 -07001487 markCellsForView(c.x, c.y, c.spanX, c.spanY, mTmpOccupied, true);
Adam Cohen482ed822012-03-02 14:15:13 -08001488 return success;
1489 }
1490
Adam Cohenf3900c22012-11-16 18:28:11 -08001491 /**
1492 * This helper class defines a cluster of views. It helps with defining complex edges
1493 * of the cluster and determining how those edges interact with other views. The edges
1494 * essentially define a fine-grained boundary around the cluster of views -- like a more
1495 * precise version of a bounding box.
1496 */
1497 private class ViewCluster {
1498 final static int LEFT = 0;
1499 final static int TOP = 1;
1500 final static int RIGHT = 2;
1501 final static int BOTTOM = 3;
Adam Cohen47a876d2012-03-19 13:21:41 -07001502
Adam Cohenf3900c22012-11-16 18:28:11 -08001503 ArrayList<View> views;
1504 ItemConfiguration config;
1505 Rect boundingRect = new Rect();
Adam Cohen47a876d2012-03-19 13:21:41 -07001506
Adam Cohenf3900c22012-11-16 18:28:11 -08001507 int[] leftEdge = new int[mCountY];
1508 int[] rightEdge = new int[mCountY];
1509 int[] topEdge = new int[mCountX];
1510 int[] bottomEdge = new int[mCountX];
1511 boolean leftEdgeDirty, rightEdgeDirty, topEdgeDirty, bottomEdgeDirty, boundingRectDirty;
1512
1513 @SuppressWarnings("unchecked")
1514 public ViewCluster(ArrayList<View> views, ItemConfiguration config) {
1515 this.views = (ArrayList<View>) views.clone();
1516 this.config = config;
1517 resetEdges();
Adam Cohen47a876d2012-03-19 13:21:41 -07001518 }
1519
Adam Cohenf3900c22012-11-16 18:28:11 -08001520 void resetEdges() {
1521 for (int i = 0; i < mCountX; i++) {
1522 topEdge[i] = -1;
1523 bottomEdge[i] = -1;
1524 }
1525 for (int i = 0; i < mCountY; i++) {
1526 leftEdge[i] = -1;
1527 rightEdge[i] = -1;
1528 }
1529 leftEdgeDirty = true;
1530 rightEdgeDirty = true;
1531 bottomEdgeDirty = true;
1532 topEdgeDirty = true;
1533 boundingRectDirty = true;
1534 }
Adam Cohen47a876d2012-03-19 13:21:41 -07001535
Adam Cohenf3900c22012-11-16 18:28:11 -08001536 void computeEdge(int which, int[] edge) {
1537 int count = views.size();
1538 for (int i = 0; i < count; i++) {
1539 CellAndSpan cs = config.map.get(views.get(i));
1540 switch (which) {
1541 case LEFT:
1542 int left = cs.x;
1543 for (int j = cs.y; j < cs.y + cs.spanY; j++) {
1544 if (left < edge[j] || edge[j] < 0) {
1545 edge[j] = left;
Adam Cohena56dc102012-07-13 13:41:42 -07001546 }
Adam Cohen47a876d2012-03-19 13:21:41 -07001547 }
Adam Cohenf3900c22012-11-16 18:28:11 -08001548 break;
1549 case RIGHT:
1550 int right = cs.x + cs.spanX;
1551 for (int j = cs.y; j < cs.y + cs.spanY; j++) {
1552 if (right > edge[j]) {
1553 edge[j] = right;
1554 }
1555 }
1556 break;
1557 case TOP:
1558 int top = cs.y;
1559 for (int j = cs.x; j < cs.x + cs.spanX; j++) {
1560 if (top < edge[j] || edge[j] < 0) {
1561 edge[j] = top;
1562 }
1563 }
1564 break;
1565 case BOTTOM:
1566 int bottom = cs.y + cs.spanY;
1567 for (int j = cs.x; j < cs.x + cs.spanX; j++) {
1568 if (bottom > edge[j]) {
1569 edge[j] = bottom;
1570 }
1571 }
1572 break;
Adam Cohen47a876d2012-03-19 13:21:41 -07001573 }
1574 }
1575 }
Adam Cohenf3900c22012-11-16 18:28:11 -08001576
1577 boolean isViewTouchingEdge(View v, int whichEdge) {
1578 CellAndSpan cs = config.map.get(v);
1579
1580 int[] edge = getEdge(whichEdge);
1581
1582 switch (whichEdge) {
1583 case LEFT:
1584 for (int i = cs.y; i < cs.y + cs.spanY; i++) {
1585 if (edge[i] == cs.x + cs.spanX) {
1586 return true;
1587 }
1588 }
1589 break;
1590 case RIGHT:
1591 for (int i = cs.y; i < cs.y + cs.spanY; i++) {
1592 if (edge[i] == cs.x) {
1593 return true;
1594 }
1595 }
1596 break;
1597 case TOP:
1598 for (int i = cs.x; i < cs.x + cs.spanX; i++) {
1599 if (edge[i] == cs.y + cs.spanY) {
1600 return true;
1601 }
1602 }
1603 break;
1604 case BOTTOM:
1605 for (int i = cs.x; i < cs.x + cs.spanX; i++) {
1606 if (edge[i] == cs.y) {
1607 return true;
1608 }
1609 }
1610 break;
1611 }
1612 return false;
1613 }
1614
1615 void shift(int whichEdge, int delta) {
1616 for (View v: views) {
1617 CellAndSpan c = config.map.get(v);
1618 switch (whichEdge) {
1619 case LEFT:
1620 c.x -= delta;
1621 break;
1622 case RIGHT:
1623 c.x += delta;
1624 break;
1625 case TOP:
1626 c.y -= delta;
1627 break;
1628 case BOTTOM:
1629 default:
1630 c.y += delta;
1631 break;
1632 }
1633 }
1634 resetEdges();
1635 }
1636
1637 public void addView(View v) {
1638 views.add(v);
1639 resetEdges();
1640 }
1641
1642 public Rect getBoundingRect() {
1643 if (boundingRectDirty) {
1644 boolean first = true;
1645 for (View v: views) {
1646 CellAndSpan c = config.map.get(v);
1647 if (first) {
1648 boundingRect.set(c.x, c.y, c.x + c.spanX, c.y + c.spanY);
1649 first = false;
1650 } else {
1651 boundingRect.union(c.x, c.y, c.x + c.spanX, c.y + c.spanY);
1652 }
1653 }
1654 }
1655 return boundingRect;
1656 }
1657
1658 public int[] getEdge(int which) {
1659 switch (which) {
1660 case LEFT:
1661 return getLeftEdge();
1662 case RIGHT:
1663 return getRightEdge();
1664 case TOP:
1665 return getTopEdge();
1666 case BOTTOM:
1667 default:
1668 return getBottomEdge();
1669 }
1670 }
1671
1672 public int[] getLeftEdge() {
1673 if (leftEdgeDirty) {
1674 computeEdge(LEFT, leftEdge);
1675 }
1676 return leftEdge;
1677 }
1678
1679 public int[] getRightEdge() {
1680 if (rightEdgeDirty) {
1681 computeEdge(RIGHT, rightEdge);
1682 }
1683 return rightEdge;
1684 }
1685
1686 public int[] getTopEdge() {
1687 if (topEdgeDirty) {
1688 computeEdge(TOP, topEdge);
1689 }
1690 return topEdge;
1691 }
1692
1693 public int[] getBottomEdge() {
1694 if (bottomEdgeDirty) {
1695 computeEdge(BOTTOM, bottomEdge);
1696 }
1697 return bottomEdge;
1698 }
1699
1700 PositionComparator comparator = new PositionComparator();
1701 class PositionComparator implements Comparator<View> {
1702 int whichEdge = 0;
1703 public int compare(View left, View right) {
1704 CellAndSpan l = config.map.get(left);
1705 CellAndSpan r = config.map.get(right);
1706 switch (whichEdge) {
1707 case LEFT:
1708 return (r.x + r.spanX) - (l.x + l.spanX);
1709 case RIGHT:
1710 return l.x - r.x;
1711 case TOP:
1712 return (r.y + r.spanY) - (l.y + l.spanY);
1713 case BOTTOM:
1714 default:
1715 return l.y - r.y;
1716 }
1717 }
1718 }
1719
1720 public void sortConfigurationForEdgePush(int edge) {
1721 comparator.whichEdge = edge;
1722 Collections.sort(config.sortedViews, comparator);
1723 }
Adam Cohen47a876d2012-03-19 13:21:41 -07001724 }
1725
Adam Cohenf3900c22012-11-16 18:28:11 -08001726 private boolean pushViewsToTempLocation(ArrayList<View> views, Rect rectOccupiedByPotentialDrop,
1727 int[] direction, View dragView, ItemConfiguration currentState) {
Adam Cohene0489502012-08-27 15:18:53 -07001728
Adam Cohenf3900c22012-11-16 18:28:11 -08001729 ViewCluster cluster = new ViewCluster(views, currentState);
1730 Rect clusterRect = cluster.getBoundingRect();
1731 int whichEdge;
1732 int pushDistance;
1733 boolean fail = false;
1734
1735 // Determine the edge of the cluster that will be leading the push and how far
1736 // the cluster must be shifted.
1737 if (direction[0] < 0) {
1738 whichEdge = ViewCluster.LEFT;
1739 pushDistance = clusterRect.right - rectOccupiedByPotentialDrop.left;
Adam Cohene0489502012-08-27 15:18:53 -07001740 } else if (direction[0] > 0) {
Adam Cohenf3900c22012-11-16 18:28:11 -08001741 whichEdge = ViewCluster.RIGHT;
1742 pushDistance = rectOccupiedByPotentialDrop.right - clusterRect.left;
1743 } else if (direction[1] < 0) {
1744 whichEdge = ViewCluster.TOP;
1745 pushDistance = clusterRect.bottom - rectOccupiedByPotentialDrop.top;
1746 } else {
1747 whichEdge = ViewCluster.BOTTOM;
1748 pushDistance = rectOccupiedByPotentialDrop.bottom - clusterRect.top;
Adam Cohene0489502012-08-27 15:18:53 -07001749 }
1750
Adam Cohenf3900c22012-11-16 18:28:11 -08001751 // Break early for invalid push distance.
1752 if (pushDistance <= 0) {
1753 return false;
Adam Cohene0489502012-08-27 15:18:53 -07001754 }
Adam Cohenf3900c22012-11-16 18:28:11 -08001755
1756 // Mark the occupied state as false for the group of views we want to move.
1757 for (View v: views) {
1758 CellAndSpan c = currentState.map.get(v);
1759 markCellsForView(c.x, c.y, c.spanX, c.spanY, mTmpOccupied, false);
1760 }
1761
1762 // We save the current configuration -- if we fail to find a solution we will revert
1763 // to the initial state. The process of finding a solution modifies the configuration
1764 // in place, hence the need for revert in the failure case.
1765 currentState.save();
1766
1767 // The pushing algorithm is simplified by considering the views in the order in which
1768 // they would be pushed by the cluster. For example, if the cluster is leading with its
1769 // left edge, we consider sort the views by their right edge, from right to left.
1770 cluster.sortConfigurationForEdgePush(whichEdge);
1771
1772 while (pushDistance > 0 && !fail) {
1773 for (View v: currentState.sortedViews) {
1774 // For each view that isn't in the cluster, we see if the leading edge of the
1775 // cluster is contacting the edge of that view. If so, we add that view to the
1776 // cluster.
1777 if (!cluster.views.contains(v) && v != dragView) {
1778 if (cluster.isViewTouchingEdge(v, whichEdge)) {
1779 LayoutParams lp = (LayoutParams) v.getLayoutParams();
1780 if (!lp.canReorder) {
1781 // The push solution includes the all apps button, this is not viable.
1782 fail = true;
1783 break;
1784 }
1785 cluster.addView(v);
1786 CellAndSpan c = currentState.map.get(v);
1787
1788 // Adding view to cluster, mark it as not occupied.
1789 markCellsForView(c.x, c.y, c.spanX, c.spanY, mTmpOccupied, false);
1790 }
1791 }
1792 }
1793 pushDistance--;
1794
1795 // The cluster has been completed, now we move the whole thing over in the appropriate
1796 // direction.
1797 cluster.shift(whichEdge, 1);
1798 }
1799
1800 boolean foundSolution = false;
1801 clusterRect = cluster.getBoundingRect();
1802
1803 // Due to the nature of the algorithm, the only check required to verify a valid solution
1804 // is to ensure that completed shifted cluster lies completely within the cell layout.
1805 if (!fail && clusterRect.left >= 0 && clusterRect.right <= mCountX && clusterRect.top >= 0 &&
1806 clusterRect.bottom <= mCountY) {
1807 foundSolution = true;
1808 } else {
1809 currentState.restore();
1810 }
1811
1812 // In either case, we set the occupied array as marked for the location of the views
1813 for (View v: cluster.views) {
1814 CellAndSpan c = currentState.map.get(v);
1815 markCellsForView(c.x, c.y, c.spanX, c.spanY, mTmpOccupied, true);
1816 }
1817
1818 return foundSolution;
Adam Cohene0489502012-08-27 15:18:53 -07001819 }
1820
Adam Cohen482ed822012-03-02 14:15:13 -08001821 private boolean addViewsToTempLocation(ArrayList<View> views, Rect rectOccupiedByPotentialDrop,
Adam Cohenf3900c22012-11-16 18:28:11 -08001822 int[] direction, View dragView, ItemConfiguration currentState) {
Adam Cohen482ed822012-03-02 14:15:13 -08001823 if (views.size() == 0) return true;
Adam Cohen482ed822012-03-02 14:15:13 -08001824
Adam Cohen8baab352012-03-20 17:39:21 -07001825 boolean success = false;
Adam Cohen482ed822012-03-02 14:15:13 -08001826 Rect boundingRect = null;
Adam Cohen8baab352012-03-20 17:39:21 -07001827 // We construct a rect which represents the entire group of views passed in
Adam Cohen482ed822012-03-02 14:15:13 -08001828 for (View v: views) {
Adam Cohen8baab352012-03-20 17:39:21 -07001829 CellAndSpan c = currentState.map.get(v);
Adam Cohen482ed822012-03-02 14:15:13 -08001830 if (boundingRect == null) {
Adam Cohen8baab352012-03-20 17:39:21 -07001831 boundingRect = new Rect(c.x, c.y, c.x + c.spanX, c.y + c.spanY);
Adam Cohen482ed822012-03-02 14:15:13 -08001832 } else {
Adam Cohen8baab352012-03-20 17:39:21 -07001833 boundingRect.union(c.x, c.y, c.x + c.spanX, c.y + c.spanY);
Adam Cohen482ed822012-03-02 14:15:13 -08001834 }
1835 }
Adam Cohen8baab352012-03-20 17:39:21 -07001836
Adam Cohen8baab352012-03-20 17:39:21 -07001837 // Mark the occupied state as false for the group of views we want to move.
Adam Cohenf3900c22012-11-16 18:28:11 -08001838 for (View v: views) {
Adam Cohen8baab352012-03-20 17:39:21 -07001839 CellAndSpan c = currentState.map.get(v);
1840 markCellsForView(c.x, c.y, c.spanX, c.spanY, mTmpOccupied, false);
1841 }
1842
Adam Cohen47a876d2012-03-19 13:21:41 -07001843 boolean[][] blockOccupied = new boolean[boundingRect.width()][boundingRect.height()];
1844 int top = boundingRect.top;
1845 int left = boundingRect.left;
Adam Cohen8baab352012-03-20 17:39:21 -07001846 // We mark more precisely which parts of the bounding rect are truly occupied, allowing
Adam Cohena56dc102012-07-13 13:41:42 -07001847 // for interlocking.
Adam Cohenf3900c22012-11-16 18:28:11 -08001848 for (View v: views) {
Adam Cohen8baab352012-03-20 17:39:21 -07001849 CellAndSpan c = currentState.map.get(v);
1850 markCellsForView(c.x - left, c.y - top, c.spanX, c.spanY, blockOccupied, true);
Adam Cohen47a876d2012-03-19 13:21:41 -07001851 }
1852
Adam Cohen482ed822012-03-02 14:15:13 -08001853 markCellsForRect(rectOccupiedByPotentialDrop, mTmpOccupied, true);
1854
Adam Cohenf3900c22012-11-16 18:28:11 -08001855 findNearestArea(boundingRect.left, boundingRect.top, boundingRect.width(),
1856 boundingRect.height(), direction, mTmpOccupied, blockOccupied, mTempLocation);
Adam Cohen482ed822012-03-02 14:15:13 -08001857
Adam Cohen8baab352012-03-20 17:39:21 -07001858 // If we successfuly found a location by pushing the block of views, we commit it
Adam Cohen482ed822012-03-02 14:15:13 -08001859 if (mTempLocation[0] >= 0 && mTempLocation[1] >= 0) {
Adam Cohen8baab352012-03-20 17:39:21 -07001860 int deltaX = mTempLocation[0] - boundingRect.left;
1861 int deltaY = mTempLocation[1] - boundingRect.top;
Adam Cohenf3900c22012-11-16 18:28:11 -08001862 for (View v: views) {
Adam Cohen8baab352012-03-20 17:39:21 -07001863 CellAndSpan c = currentState.map.get(v);
1864 c.x += deltaX;
1865 c.y += deltaY;
Adam Cohen482ed822012-03-02 14:15:13 -08001866 }
1867 success = true;
1868 }
Adam Cohen8baab352012-03-20 17:39:21 -07001869
1870 // In either case, we set the occupied array as marked for the location of the views
Adam Cohenf3900c22012-11-16 18:28:11 -08001871 for (View v: views) {
Adam Cohen8baab352012-03-20 17:39:21 -07001872 CellAndSpan c = currentState.map.get(v);
1873 markCellsForView(c.x, c.y, c.spanX, c.spanY, mTmpOccupied, true);
Adam Cohen482ed822012-03-02 14:15:13 -08001874 }
1875 return success;
1876 }
1877
1878 private void markCellsForRect(Rect r, boolean[][] occupied, boolean value) {
1879 markCellsForView(r.left, r.top, r.width(), r.height(), occupied, value);
1880 }
1881
Adam Cohen4abc5bd2012-05-29 21:06:03 -07001882 // This method tries to find a reordering solution which satisfies the push mechanic by trying
1883 // to push items in each of the cardinal directions, in an order based on the direction vector
1884 // passed.
1885 private boolean attemptPushInDirection(ArrayList<View> intersectingViews, Rect occupied,
1886 int[] direction, View ignoreView, ItemConfiguration solution) {
1887 if ((Math.abs(direction[0]) + Math.abs(direction[1])) > 1) {
Winson Chung5f8afe62013-08-12 16:19:28 -07001888 // If the direction vector has two non-zero components, we try pushing
Adam Cohen4abc5bd2012-05-29 21:06:03 -07001889 // separately in each of the components.
1890 int temp = direction[1];
1891 direction[1] = 0;
Adam Cohenf3900c22012-11-16 18:28:11 -08001892
1893 if (pushViewsToTempLocation(intersectingViews, occupied, direction,
Adam Cohen4abc5bd2012-05-29 21:06:03 -07001894 ignoreView, solution)) {
1895 return true;
1896 }
1897 direction[1] = temp;
1898 temp = direction[0];
1899 direction[0] = 0;
Adam Cohenf3900c22012-11-16 18:28:11 -08001900
1901 if (pushViewsToTempLocation(intersectingViews, occupied, direction,
Adam Cohen4abc5bd2012-05-29 21:06:03 -07001902 ignoreView, solution)) {
1903 return true;
1904 }
1905 // Revert the direction
1906 direction[0] = temp;
1907
1908 // Now we try pushing in each component of the opposite direction
1909 direction[0] *= -1;
1910 direction[1] *= -1;
1911 temp = direction[1];
1912 direction[1] = 0;
Adam Cohenf3900c22012-11-16 18:28:11 -08001913 if (pushViewsToTempLocation(intersectingViews, occupied, direction,
Adam Cohen4abc5bd2012-05-29 21:06:03 -07001914 ignoreView, solution)) {
1915 return true;
1916 }
1917
1918 direction[1] = temp;
1919 temp = direction[0];
1920 direction[0] = 0;
Adam Cohenf3900c22012-11-16 18:28:11 -08001921 if (pushViewsToTempLocation(intersectingViews, occupied, direction,
Adam Cohen4abc5bd2012-05-29 21:06:03 -07001922 ignoreView, solution)) {
1923 return true;
1924 }
1925 // revert the direction
1926 direction[0] = temp;
1927 direction[0] *= -1;
1928 direction[1] *= -1;
Winson Chung5f8afe62013-08-12 16:19:28 -07001929
Adam Cohen4abc5bd2012-05-29 21:06:03 -07001930 } else {
1931 // If the direction vector has a single non-zero component, we push first in the
1932 // direction of the vector
Adam Cohenf3900c22012-11-16 18:28:11 -08001933 if (pushViewsToTempLocation(intersectingViews, occupied, direction,
Adam Cohen4abc5bd2012-05-29 21:06:03 -07001934 ignoreView, solution)) {
1935 return true;
1936 }
Adam Cohen4abc5bd2012-05-29 21:06:03 -07001937 // Then we try the opposite direction
1938 direction[0] *= -1;
1939 direction[1] *= -1;
Adam Cohenf3900c22012-11-16 18:28:11 -08001940 if (pushViewsToTempLocation(intersectingViews, occupied, direction,
Adam Cohen4abc5bd2012-05-29 21:06:03 -07001941 ignoreView, solution)) {
1942 return true;
1943 }
1944 // Switch the direction back
1945 direction[0] *= -1;
1946 direction[1] *= -1;
Winson Chung5f8afe62013-08-12 16:19:28 -07001947
1948 // If we have failed to find a push solution with the above, then we try
Adam Cohen4abc5bd2012-05-29 21:06:03 -07001949 // to find a solution by pushing along the perpendicular axis.
1950
1951 // Swap the components
1952 int temp = direction[1];
1953 direction[1] = direction[0];
1954 direction[0] = temp;
Adam Cohenf3900c22012-11-16 18:28:11 -08001955 if (pushViewsToTempLocation(intersectingViews, occupied, direction,
Adam Cohen4abc5bd2012-05-29 21:06:03 -07001956 ignoreView, solution)) {
1957 return true;
1958 }
1959
1960 // Then we try the opposite direction
1961 direction[0] *= -1;
1962 direction[1] *= -1;
Adam Cohenf3900c22012-11-16 18:28:11 -08001963 if (pushViewsToTempLocation(intersectingViews, occupied, direction,
Adam Cohen4abc5bd2012-05-29 21:06:03 -07001964 ignoreView, solution)) {
1965 return true;
1966 }
1967 // Switch the direction back
1968 direction[0] *= -1;
1969 direction[1] *= -1;
1970
1971 // Swap the components back
1972 temp = direction[1];
1973 direction[1] = direction[0];
1974 direction[0] = temp;
1975 }
1976 return false;
1977 }
1978
Adam Cohen482ed822012-03-02 14:15:13 -08001979 private boolean rearrangementExists(int cellX, int cellY, int spanX, int spanY, int[] direction,
Adam Cohen8baab352012-03-20 17:39:21 -07001980 View ignoreView, ItemConfiguration solution) {
Winson Chunge3e03bc2012-05-01 15:10:11 -07001981 // Return early if get invalid cell positions
1982 if (cellX < 0 || cellY < 0) return false;
Adam Cohen482ed822012-03-02 14:15:13 -08001983
Adam Cohen8baab352012-03-20 17:39:21 -07001984 mIntersectingViews.clear();
Adam Cohen482ed822012-03-02 14:15:13 -08001985 mOccupiedRect.set(cellX, cellY, cellX + spanX, cellY + spanY);
Adam Cohen482ed822012-03-02 14:15:13 -08001986
Adam Cohen8baab352012-03-20 17:39:21 -07001987 // Mark the desired location of the view currently being dragged.
Adam Cohen482ed822012-03-02 14:15:13 -08001988 if (ignoreView != null) {
Adam Cohen8baab352012-03-20 17:39:21 -07001989 CellAndSpan c = solution.map.get(ignoreView);
Adam Cohen19f37922012-03-21 11:59:11 -07001990 if (c != null) {
1991 c.x = cellX;
1992 c.y = cellY;
1993 }
Adam Cohen482ed822012-03-02 14:15:13 -08001994 }
Adam Cohen482ed822012-03-02 14:15:13 -08001995 Rect r0 = new Rect(cellX, cellY, cellX + spanX, cellY + spanY);
1996 Rect r1 = new Rect();
Adam Cohen8baab352012-03-20 17:39:21 -07001997 for (View child: solution.map.keySet()) {
Adam Cohen482ed822012-03-02 14:15:13 -08001998 if (child == ignoreView) continue;
Adam Cohen8baab352012-03-20 17:39:21 -07001999 CellAndSpan c = solution.map.get(child);
Adam Cohen482ed822012-03-02 14:15:13 -08002000 LayoutParams lp = (LayoutParams) child.getLayoutParams();
Adam Cohen8baab352012-03-20 17:39:21 -07002001 r1.set(c.x, c.y, c.x + c.spanX, c.y + c.spanY);
Adam Cohen482ed822012-03-02 14:15:13 -08002002 if (Rect.intersects(r0, r1)) {
2003 if (!lp.canReorder) {
2004 return false;
2005 }
2006 mIntersectingViews.add(child);
2007 }
2008 }
Adam Cohen47a876d2012-03-19 13:21:41 -07002009
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002010 solution.intersectingViews = new ArrayList<View>(mIntersectingViews);
2011
Winson Chung5f8afe62013-08-12 16:19:28 -07002012 // First we try to find a solution which respects the push mechanic. That is,
Adam Cohen4abc5bd2012-05-29 21:06:03 -07002013 // we try to find a solution such that no displaced item travels through another item
2014 // without also displacing that item.
2015 if (attemptPushInDirection(mIntersectingViews, mOccupiedRect, direction, ignoreView,
Adam Cohen19f37922012-03-21 11:59:11 -07002016 solution)) {
Adam Cohen47a876d2012-03-19 13:21:41 -07002017 return true;
2018 }
Adam Cohen47a876d2012-03-19 13:21:41 -07002019
Adam Cohen4abc5bd2012-05-29 21:06:03 -07002020 // Next we try moving the views as a block, but without requiring the push mechanic.
Adam Cohenf3900c22012-11-16 18:28:11 -08002021 if (addViewsToTempLocation(mIntersectingViews, mOccupiedRect, direction, ignoreView,
Adam Cohen19f37922012-03-21 11:59:11 -07002022 solution)) {
Adam Cohen482ed822012-03-02 14:15:13 -08002023 return true;
2024 }
Adam Cohen47a876d2012-03-19 13:21:41 -07002025
Adam Cohen482ed822012-03-02 14:15:13 -08002026 // Ok, they couldn't move as a block, let's move them individually
2027 for (View v : mIntersectingViews) {
Adam Cohen8baab352012-03-20 17:39:21 -07002028 if (!addViewToTempLocation(v, mOccupiedRect, direction, solution)) {
Adam Cohen482ed822012-03-02 14:15:13 -08002029 return false;
2030 }
2031 }
2032 return true;
2033 }
2034
2035 /*
2036 * Returns a pair (x, y), where x,y are in {-1, 0, 1} corresponding to vector between
2037 * the provided point and the provided cell
2038 */
Adam Cohen47a876d2012-03-19 13:21:41 -07002039 private void computeDirectionVector(float deltaX, float deltaY, int[] result) {
Adam Cohen482ed822012-03-02 14:15:13 -08002040 double angle = Math.atan(((float) deltaY) / deltaX);
2041
2042 result[0] = 0;
2043 result[1] = 0;
2044 if (Math.abs(Math.cos(angle)) > 0.5f) {
2045 result[0] = (int) Math.signum(deltaX);
2046 }
2047 if (Math.abs(Math.sin(angle)) > 0.5f) {
2048 result[1] = (int) Math.signum(deltaY);
2049 }
2050 }
2051
Adam Cohen8baab352012-03-20 17:39:21 -07002052 private void copyOccupiedArray(boolean[][] occupied) {
2053 for (int i = 0; i < mCountX; i++) {
2054 for (int j = 0; j < mCountY; j++) {
2055 occupied[i][j] = mOccupied[i][j];
2056 }
2057 }
2058 }
2059
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002060 ItemConfiguration findReorderSolution(int pixelX, int pixelY, int minSpanX, int minSpanY,
2061 int spanX, int spanY, int[] direction, View dragView, boolean decX,
2062 ItemConfiguration solution) {
Adam Cohen8baab352012-03-20 17:39:21 -07002063 // Copy the current state into the solution. This solution will be manipulated as necessary.
2064 copyCurrentStateToSolution(solution, false);
2065 // Copy the current occupied array into the temporary occupied array. This array will be
2066 // manipulated as necessary to find a solution.
2067 copyOccupiedArray(mTmpOccupied);
Adam Cohen482ed822012-03-02 14:15:13 -08002068
2069 // We find the nearest cell into which we would place the dragged item, assuming there's
2070 // nothing in its way.
2071 int result[] = new int[2];
2072 result = findNearestArea(pixelX, pixelY, spanX, spanY, result);
2073
2074 boolean success = false;
2075 // First we try the exact nearest position of the item being dragged,
2076 // we will then want to try to move this around to other neighbouring positions
Adam Cohen8baab352012-03-20 17:39:21 -07002077 success = rearrangementExists(result[0], result[1], spanX, spanY, direction, dragView,
2078 solution);
Adam Cohen482ed822012-03-02 14:15:13 -08002079
2080 if (!success) {
2081 // We try shrinking the widget down to size in an alternating pattern, shrink 1 in
2082 // x, then 1 in y etc.
2083 if (spanX > minSpanX && (minSpanY == spanY || decX)) {
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002084 return findReorderSolution(pixelX, pixelY, minSpanX, minSpanY, spanX - 1, spanY,
2085 direction, dragView, false, solution);
Adam Cohen482ed822012-03-02 14:15:13 -08002086 } else if (spanY > minSpanY) {
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002087 return findReorderSolution(pixelX, pixelY, minSpanX, minSpanY, spanX, spanY - 1,
2088 direction, dragView, true, solution);
Adam Cohen482ed822012-03-02 14:15:13 -08002089 }
2090 solution.isSolution = false;
2091 } else {
2092 solution.isSolution = true;
2093 solution.dragViewX = result[0];
2094 solution.dragViewY = result[1];
2095 solution.dragViewSpanX = spanX;
2096 solution.dragViewSpanY = spanY;
Adam Cohen482ed822012-03-02 14:15:13 -08002097 }
2098 return solution;
2099 }
2100
2101 private void copyCurrentStateToSolution(ItemConfiguration solution, boolean temp) {
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 LayoutParams lp = (LayoutParams) child.getLayoutParams();
Adam Cohen8baab352012-03-20 17:39:21 -07002106 CellAndSpan c;
Adam Cohen482ed822012-03-02 14:15:13 -08002107 if (temp) {
Adam Cohen8baab352012-03-20 17:39:21 -07002108 c = new CellAndSpan(lp.tmpCellX, lp.tmpCellY, lp.cellHSpan, lp.cellVSpan);
Adam Cohen482ed822012-03-02 14:15:13 -08002109 } else {
Adam Cohen8baab352012-03-20 17:39:21 -07002110 c = new CellAndSpan(lp.cellX, lp.cellY, lp.cellHSpan, lp.cellVSpan);
Adam Cohen482ed822012-03-02 14:15:13 -08002111 }
Adam Cohenf3900c22012-11-16 18:28:11 -08002112 solution.add(child, c);
Adam Cohen482ed822012-03-02 14:15:13 -08002113 }
2114 }
2115
2116 private void copySolutionToTempState(ItemConfiguration solution, View dragView) {
2117 for (int i = 0; i < mCountX; i++) {
2118 for (int j = 0; j < mCountY; j++) {
2119 mTmpOccupied[i][j] = false;
2120 }
2121 }
2122
Michael Jurkaa52570f2012-03-20 03:18:20 -07002123 int childCount = mShortcutsAndWidgets.getChildCount();
Adam Cohen482ed822012-03-02 14:15:13 -08002124 for (int i = 0; i < childCount; i++) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07002125 View child = mShortcutsAndWidgets.getChildAt(i);
Adam Cohen482ed822012-03-02 14:15:13 -08002126 if (child == dragView) continue;
2127 LayoutParams lp = (LayoutParams) child.getLayoutParams();
Adam Cohen8baab352012-03-20 17:39:21 -07002128 CellAndSpan c = solution.map.get(child);
2129 if (c != null) {
2130 lp.tmpCellX = c.x;
2131 lp.tmpCellY = c.y;
2132 lp.cellHSpan = c.spanX;
2133 lp.cellVSpan = c.spanY;
2134 markCellsForView(c.x, c.y, c.spanX, c.spanY, mTmpOccupied, true);
Adam Cohen482ed822012-03-02 14:15:13 -08002135 }
2136 }
2137 markCellsForView(solution.dragViewX, solution.dragViewY, solution.dragViewSpanX,
2138 solution.dragViewSpanY, mTmpOccupied, true);
2139 }
2140
2141 private void animateItemsToSolution(ItemConfiguration solution, View dragView, boolean
2142 commitDragView) {
2143
2144 boolean[][] occupied = DESTRUCTIVE_REORDER ? mOccupied : mTmpOccupied;
2145 for (int i = 0; i < mCountX; i++) {
2146 for (int j = 0; j < mCountY; j++) {
2147 occupied[i][j] = false;
2148 }
2149 }
2150
Michael Jurkaa52570f2012-03-20 03:18:20 -07002151 int childCount = mShortcutsAndWidgets.getChildCount();
Adam Cohen482ed822012-03-02 14:15:13 -08002152 for (int i = 0; i < childCount; i++) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07002153 View child = mShortcutsAndWidgets.getChildAt(i);
Adam Cohen482ed822012-03-02 14:15:13 -08002154 if (child == dragView) continue;
Adam Cohen8baab352012-03-20 17:39:21 -07002155 CellAndSpan c = solution.map.get(child);
2156 if (c != null) {
Adam Cohen19f37922012-03-21 11:59:11 -07002157 animateChildToPosition(child, c.x, c.y, REORDER_ANIMATION_DURATION, 0,
2158 DESTRUCTIVE_REORDER, false);
Adam Cohen8baab352012-03-20 17:39:21 -07002159 markCellsForView(c.x, c.y, c.spanX, c.spanY, occupied, true);
Adam Cohen482ed822012-03-02 14:15:13 -08002160 }
2161 }
2162 if (commitDragView) {
2163 markCellsForView(solution.dragViewX, solution.dragViewY, solution.dragViewSpanX,
2164 solution.dragViewSpanY, occupied, true);
2165 }
2166 }
2167
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002168
2169 // This method starts or changes the reorder preview animations
2170 private void beginOrAdjustReorderPreviewAnimations(ItemConfiguration solution,
2171 View dragView, int delay, int mode) {
Adam Cohen19f37922012-03-21 11:59:11 -07002172 int childCount = mShortcutsAndWidgets.getChildCount();
Adam Cohen19f37922012-03-21 11:59:11 -07002173 for (int i = 0; i < childCount; i++) {
2174 View child = mShortcutsAndWidgets.getChildAt(i);
2175 if (child == dragView) continue;
2176 CellAndSpan c = solution.map.get(child);
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002177 boolean skip = mode == ReorderPreviewAnimation.MODE_HINT && solution.intersectingViews
2178 != null && !solution.intersectingViews.contains(child);
2179
Adam Cohen19f37922012-03-21 11:59:11 -07002180 LayoutParams lp = (LayoutParams) child.getLayoutParams();
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002181 if (c != null && !skip) {
2182 ReorderPreviewAnimation rha = new ReorderPreviewAnimation(child, mode, lp.cellX,
2183 lp.cellY, c.x, c.y, c.spanX, c.spanY);
Adam Cohend024f982012-05-23 18:26:45 -07002184 rha.animate();
Adam Cohen19f37922012-03-21 11:59:11 -07002185 }
2186 }
2187 }
2188
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002189 // Class which represents the reorder preview animations. These animations show that an item is
Adam Cohen19f37922012-03-21 11:59:11 -07002190 // in a temporary state, and hint at where the item will return to.
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002191 class ReorderPreviewAnimation {
Adam Cohen19f37922012-03-21 11:59:11 -07002192 View child;
Adam Cohend024f982012-05-23 18:26:45 -07002193 float finalDeltaX;
2194 float finalDeltaY;
2195 float initDeltaX;
2196 float initDeltaY;
2197 float finalScale;
2198 float initScale;
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002199 int mode;
2200 boolean repeating = false;
2201 private static final int PREVIEW_DURATION = 300;
2202 private static final int HINT_DURATION = Workspace.REORDER_TIMEOUT;
2203
2204 public static final int MODE_HINT = 0;
2205 public static final int MODE_PREVIEW = 1;
2206
Adam Cohene7587d22012-05-24 18:50:02 -07002207 Animator a;
Adam Cohen19f37922012-03-21 11:59:11 -07002208
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002209 public ReorderPreviewAnimation(View child, int mode, int cellX0, int cellY0, int cellX1,
2210 int cellY1, int spanX, int spanY) {
Adam Cohen19f37922012-03-21 11:59:11 -07002211 regionToCenterPoint(cellX0, cellY0, spanX, spanY, mTmpPoint);
2212 final int x0 = mTmpPoint[0];
2213 final int y0 = mTmpPoint[1];
2214 regionToCenterPoint(cellX1, cellY1, spanX, spanY, mTmpPoint);
2215 final int x1 = mTmpPoint[0];
2216 final int y1 = mTmpPoint[1];
2217 final int dX = x1 - x0;
2218 final int dY = y1 - y0;
Adam Cohend024f982012-05-23 18:26:45 -07002219 finalDeltaX = 0;
2220 finalDeltaY = 0;
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002221 int dir = mode == MODE_HINT ? -1 : 1;
Adam Cohen19f37922012-03-21 11:59:11 -07002222 if (dX == dY && dX == 0) {
2223 } else {
2224 if (dY == 0) {
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002225 finalDeltaX = - dir * Math.signum(dX) * mReorderPreviewAnimationMagnitude;
Adam Cohen19f37922012-03-21 11:59:11 -07002226 } else if (dX == 0) {
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002227 finalDeltaY = - dir * Math.signum(dY) * mReorderPreviewAnimationMagnitude;
Adam Cohen19f37922012-03-21 11:59:11 -07002228 } else {
2229 double angle = Math.atan( (float) (dY) / dX);
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002230 finalDeltaX = (int) (- dir * Math.signum(dX) *
2231 Math.abs(Math.cos(angle) * mReorderPreviewAnimationMagnitude));
2232 finalDeltaY = (int) (- dir * Math.signum(dY) *
2233 Math.abs(Math.sin(angle) * mReorderPreviewAnimationMagnitude));
Adam Cohen19f37922012-03-21 11:59:11 -07002234 }
2235 }
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002236 this.mode = mode;
Adam Cohend024f982012-05-23 18:26:45 -07002237 initDeltaX = child.getTranslationX();
2238 initDeltaY = child.getTranslationY();
Adam Cohen307fe232012-08-16 17:55:58 -07002239 finalScale = getChildrenScale() - 4.0f / child.getWidth();
Adam Cohend024f982012-05-23 18:26:45 -07002240 initScale = child.getScaleX();
Adam Cohen19f37922012-03-21 11:59:11 -07002241 this.child = child;
2242 }
2243
Adam Cohend024f982012-05-23 18:26:45 -07002244 void animate() {
Adam Cohen19f37922012-03-21 11:59:11 -07002245 if (mShakeAnimators.containsKey(child)) {
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002246 ReorderPreviewAnimation oldAnimation = mShakeAnimators.get(child);
Adam Cohend024f982012-05-23 18:26:45 -07002247 oldAnimation.cancel();
Adam Cohen19f37922012-03-21 11:59:11 -07002248 mShakeAnimators.remove(child);
Adam Cohene7587d22012-05-24 18:50:02 -07002249 if (finalDeltaX == 0 && finalDeltaY == 0) {
2250 completeAnimationImmediately();
2251 return;
2252 }
Adam Cohen19f37922012-03-21 11:59:11 -07002253 }
Adam Cohend024f982012-05-23 18:26:45 -07002254 if (finalDeltaX == 0 && finalDeltaY == 0) {
Adam Cohen19f37922012-03-21 11:59:11 -07002255 return;
2256 }
Michael Jurkaf1ad6082013-03-13 12:55:46 +01002257 ValueAnimator va = LauncherAnimUtils.ofFloat(child, 0f, 1f);
Adam Cohene7587d22012-05-24 18:50:02 -07002258 a = va;
Adam Cohen19f37922012-03-21 11:59:11 -07002259 va.setRepeatMode(ValueAnimator.REVERSE);
2260 va.setRepeatCount(ValueAnimator.INFINITE);
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002261 va.setDuration(mode == MODE_HINT ? HINT_DURATION : PREVIEW_DURATION);
Adam Cohend024f982012-05-23 18:26:45 -07002262 va.setStartDelay((int) (Math.random() * 60));
Adam Cohen19f37922012-03-21 11:59:11 -07002263 va.addUpdateListener(new AnimatorUpdateListener() {
2264 @Override
2265 public void onAnimationUpdate(ValueAnimator animation) {
2266 float r = ((Float) animation.getAnimatedValue()).floatValue();
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002267 float r1 = (mode == MODE_HINT && repeating) ? 1.0f : r;
2268 float x = r1 * finalDeltaX + (1 - r1) * initDeltaX;
2269 float y = r1 * finalDeltaY + (1 - r1) * initDeltaY;
Adam Cohen19f37922012-03-21 11:59:11 -07002270 child.setTranslationX(x);
2271 child.setTranslationY(y);
Adam Cohend024f982012-05-23 18:26:45 -07002272 float s = r * finalScale + (1 - r) * initScale;
Brandon Keely50e6e562012-05-08 16:28:49 -07002273 child.setScaleX(s);
2274 child.setScaleY(s);
Adam Cohen19f37922012-03-21 11:59:11 -07002275 }
2276 });
2277 va.addListener(new AnimatorListenerAdapter() {
2278 public void onAnimationRepeat(Animator animation) {
Adam Cohen19f37922012-03-21 11:59:11 -07002279 // We make sure to end only after a full period
Adam Cohend024f982012-05-23 18:26:45 -07002280 initDeltaX = 0;
2281 initDeltaY = 0;
Adam Cohen307fe232012-08-16 17:55:58 -07002282 initScale = getChildrenScale();
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002283 repeating = true;
Adam Cohen19f37922012-03-21 11:59:11 -07002284 }
2285 });
Adam Cohen19f37922012-03-21 11:59:11 -07002286 mShakeAnimators.put(child, this);
2287 va.start();
2288 }
2289
Adam Cohend024f982012-05-23 18:26:45 -07002290 private void cancel() {
Adam Cohene7587d22012-05-24 18:50:02 -07002291 if (a != null) {
2292 a.cancel();
2293 }
Adam Cohen19f37922012-03-21 11:59:11 -07002294 }
Adam Cohene7587d22012-05-24 18:50:02 -07002295
Brandon Keely50e6e562012-05-08 16:28:49 -07002296 private void completeAnimationImmediately() {
Adam Cohene7587d22012-05-24 18:50:02 -07002297 if (a != null) {
2298 a.cancel();
2299 }
Brandon Keely50e6e562012-05-08 16:28:49 -07002300
Michael Jurka2ecf9952012-06-18 12:52:28 -07002301 AnimatorSet s = LauncherAnimUtils.createAnimatorSet();
Adam Cohene7587d22012-05-24 18:50:02 -07002302 a = s;
Brandon Keely50e6e562012-05-08 16:28:49 -07002303 s.playTogether(
Adam Cohen307fe232012-08-16 17:55:58 -07002304 LauncherAnimUtils.ofFloat(child, "scaleX", getChildrenScale()),
2305 LauncherAnimUtils.ofFloat(child, "scaleY", getChildrenScale()),
Michael Jurka2ecf9952012-06-18 12:52:28 -07002306 LauncherAnimUtils.ofFloat(child, "translationX", 0f),
2307 LauncherAnimUtils.ofFloat(child, "translationY", 0f)
Brandon Keely50e6e562012-05-08 16:28:49 -07002308 );
2309 s.setDuration(REORDER_ANIMATION_DURATION);
2310 s.setInterpolator(new android.view.animation.DecelerateInterpolator(1.5f));
2311 s.start();
2312 }
Adam Cohen19f37922012-03-21 11:59:11 -07002313 }
2314
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002315 private void completeAndClearReorderPreviewAnimations() {
2316 for (ReorderPreviewAnimation a: mShakeAnimators.values()) {
Brandon Keely50e6e562012-05-08 16:28:49 -07002317 a.completeAnimationImmediately();
Adam Cohen19f37922012-03-21 11:59:11 -07002318 }
2319 mShakeAnimators.clear();
2320 }
2321
Adam Cohen482ed822012-03-02 14:15:13 -08002322 private void commitTempPlacement() {
2323 for (int i = 0; i < mCountX; i++) {
2324 for (int j = 0; j < mCountY; j++) {
2325 mOccupied[i][j] = mTmpOccupied[i][j];
2326 }
2327 }
Michael Jurkaa52570f2012-03-20 03:18:20 -07002328 int childCount = mShortcutsAndWidgets.getChildCount();
Adam Cohen482ed822012-03-02 14:15:13 -08002329 for (int i = 0; i < childCount; i++) {
Adam Cohenea889a22012-03-27 16:45:39 -07002330 View child = mShortcutsAndWidgets.getChildAt(i);
2331 LayoutParams lp = (LayoutParams) child.getLayoutParams();
2332 ItemInfo info = (ItemInfo) child.getTag();
Adam Cohen2acce882012-03-28 19:03:19 -07002333 // We do a null check here because the item info can be null in the case of the
2334 // AllApps button in the hotseat.
2335 if (info != null) {
Adam Cohen487f7dd2012-06-28 18:12:10 -07002336 if (info.cellX != lp.tmpCellX || info.cellY != lp.tmpCellY ||
2337 info.spanX != lp.cellHSpan || info.spanY != lp.cellVSpan) {
2338 info.requiresDbUpdate = true;
2339 }
Adam Cohen2acce882012-03-28 19:03:19 -07002340 info.cellX = lp.cellX = lp.tmpCellX;
2341 info.cellY = lp.cellY = lp.tmpCellY;
Adam Cohenbebf0422012-04-11 18:06:28 -07002342 info.spanX = lp.cellHSpan;
2343 info.spanY = lp.cellVSpan;
Adam Cohen2acce882012-03-28 19:03:19 -07002344 }
Adam Cohen482ed822012-03-02 14:15:13 -08002345 }
Adam Cohen2acce882012-03-28 19:03:19 -07002346 mLauncher.getWorkspace().updateItemLocationsInDatabase(this);
Adam Cohen482ed822012-03-02 14:15:13 -08002347 }
2348
2349 public void setUseTempCoords(boolean useTempCoords) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07002350 int childCount = mShortcutsAndWidgets.getChildCount();
Adam Cohen482ed822012-03-02 14:15:13 -08002351 for (int i = 0; i < childCount; i++) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07002352 LayoutParams lp = (LayoutParams) mShortcutsAndWidgets.getChildAt(i).getLayoutParams();
Adam Cohen482ed822012-03-02 14:15:13 -08002353 lp.useTmpCoords = useTempCoords;
2354 }
2355 }
2356
Adam Cohen482ed822012-03-02 14:15:13 -08002357 ItemConfiguration findConfigurationNoShuffle(int pixelX, int pixelY, int minSpanX, int minSpanY,
2358 int spanX, int spanY, View dragView, ItemConfiguration solution) {
2359 int[] result = new int[2];
2360 int[] resultSpan = new int[2];
2361 findNearestVacantArea(pixelX, pixelY, minSpanX, minSpanY, spanX, spanY, null, result,
2362 resultSpan);
2363 if (result[0] >= 0 && result[1] >= 0) {
2364 copyCurrentStateToSolution(solution, false);
2365 solution.dragViewX = result[0];
2366 solution.dragViewY = result[1];
2367 solution.dragViewSpanX = resultSpan[0];
2368 solution.dragViewSpanY = resultSpan[1];
2369 solution.isSolution = true;
2370 } else {
2371 solution.isSolution = false;
2372 }
2373 return solution;
2374 }
2375
2376 public void prepareChildForDrag(View child) {
2377 markCellsAsUnoccupiedForView(child);
Adam Cohen482ed822012-03-02 14:15:13 -08002378 }
2379
Adam Cohen19f37922012-03-21 11:59:11 -07002380 /* This seems like it should be obvious and straight-forward, but when the direction vector
2381 needs to match with the notion of the dragView pushing other views, we have to employ
2382 a slightly more subtle notion of the direction vector. The question is what two points is
2383 the vector between? The center of the dragView and its desired destination? Not quite, as
2384 this doesn't necessarily coincide with the interaction of the dragView and items occupying
2385 those cells. Instead we use some heuristics to often lock the vector to up, down, left
2386 or right, which helps make pushing feel right.
2387 */
2388 private void getDirectionVectorForDrop(int dragViewCenterX, int dragViewCenterY, int spanX,
2389 int spanY, View dragView, int[] resultDirection) {
2390 int[] targetDestination = new int[2];
2391
2392 findNearestArea(dragViewCenterX, dragViewCenterY, spanX, spanY, targetDestination);
2393 Rect dragRect = new Rect();
2394 regionToRect(targetDestination[0], targetDestination[1], spanX, spanY, dragRect);
2395 dragRect.offset(dragViewCenterX - dragRect.centerX(), dragViewCenterY - dragRect.centerY());
2396
2397 Rect dropRegionRect = new Rect();
2398 getViewsIntersectingRegion(targetDestination[0], targetDestination[1], spanX, spanY,
2399 dragView, dropRegionRect, mIntersectingViews);
2400
2401 int dropRegionSpanX = dropRegionRect.width();
2402 int dropRegionSpanY = dropRegionRect.height();
2403
2404 regionToRect(dropRegionRect.left, dropRegionRect.top, dropRegionRect.width(),
2405 dropRegionRect.height(), dropRegionRect);
2406
2407 int deltaX = (dropRegionRect.centerX() - dragViewCenterX) / spanX;
2408 int deltaY = (dropRegionRect.centerY() - dragViewCenterY) / spanY;
2409
2410 if (dropRegionSpanX == mCountX || spanX == mCountX) {
2411 deltaX = 0;
2412 }
2413 if (dropRegionSpanY == mCountY || spanY == mCountY) {
2414 deltaY = 0;
2415 }
2416
2417 if (deltaX == 0 && deltaY == 0) {
2418 // No idea what to do, give a random direction.
2419 resultDirection[0] = 1;
2420 resultDirection[1] = 0;
2421 } else {
2422 computeDirectionVector(deltaX, deltaY, resultDirection);
2423 }
2424 }
2425
2426 // For a given cell and span, fetch the set of views intersecting the region.
2427 private void getViewsIntersectingRegion(int cellX, int cellY, int spanX, int spanY,
2428 View dragView, Rect boundingRect, ArrayList<View> intersectingViews) {
2429 if (boundingRect != null) {
2430 boundingRect.set(cellX, cellY, cellX + spanX, cellY + spanY);
2431 }
2432 intersectingViews.clear();
2433 Rect r0 = new Rect(cellX, cellY, cellX + spanX, cellY + spanY);
2434 Rect r1 = new Rect();
2435 final int count = mShortcutsAndWidgets.getChildCount();
2436 for (int i = 0; i < count; i++) {
2437 View child = mShortcutsAndWidgets.getChildAt(i);
2438 if (child == dragView) continue;
2439 LayoutParams lp = (LayoutParams) child.getLayoutParams();
2440 r1.set(lp.cellX, lp.cellY, lp.cellX + lp.cellHSpan, lp.cellY + lp.cellVSpan);
2441 if (Rect.intersects(r0, r1)) {
2442 mIntersectingViews.add(child);
2443 if (boundingRect != null) {
2444 boundingRect.union(r1);
2445 }
2446 }
2447 }
2448 }
2449
2450 boolean isNearestDropLocationOccupied(int pixelX, int pixelY, int spanX, int spanY,
2451 View dragView, int[] result) {
2452 result = findNearestArea(pixelX, pixelY, spanX, spanY, result);
2453 getViewsIntersectingRegion(result[0], result[1], spanX, spanY, dragView, null,
2454 mIntersectingViews);
2455 return !mIntersectingViews.isEmpty();
2456 }
2457
2458 void revertTempState() {
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002459 completeAndClearReorderPreviewAnimations();
2460 if (isItemPlacementDirty() && !DESTRUCTIVE_REORDER) {
2461 final int count = mShortcutsAndWidgets.getChildCount();
2462 for (int i = 0; i < count; i++) {
2463 View child = mShortcutsAndWidgets.getChildAt(i);
2464 LayoutParams lp = (LayoutParams) child.getLayoutParams();
2465 if (lp.tmpCellX != lp.cellX || lp.tmpCellY != lp.cellY) {
2466 lp.tmpCellX = lp.cellX;
2467 lp.tmpCellY = lp.cellY;
2468 animateChildToPosition(child, lp.cellX, lp.cellY, REORDER_ANIMATION_DURATION,
2469 0, false, false);
2470 }
Adam Cohen19f37922012-03-21 11:59:11 -07002471 }
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002472 setItemPlacementDirty(false);
Adam Cohen19f37922012-03-21 11:59:11 -07002473 }
Adam Cohen19f37922012-03-21 11:59:11 -07002474 }
2475
Adam Cohenbebf0422012-04-11 18:06:28 -07002476 boolean createAreaForResize(int cellX, int cellY, int spanX, int spanY,
2477 View dragView, int[] direction, boolean commit) {
2478 int[] pixelXY = new int[2];
2479 regionToCenterPoint(cellX, cellY, spanX, spanY, pixelXY);
2480
2481 // First we determine if things have moved enough to cause a different layout
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002482 ItemConfiguration swapSolution = findReorderSolution(pixelXY[0], pixelXY[1], spanX, spanY,
Adam Cohenbebf0422012-04-11 18:06:28 -07002483 spanX, spanY, direction, dragView, true, new ItemConfiguration());
2484
2485 setUseTempCoords(true);
2486 if (swapSolution != null && swapSolution.isSolution) {
2487 // If we're just testing for a possible location (MODE_ACCEPT_DROP), we don't bother
2488 // committing anything or animating anything as we just want to determine if a solution
2489 // exists
2490 copySolutionToTempState(swapSolution, dragView);
2491 setItemPlacementDirty(true);
2492 animateItemsToSolution(swapSolution, dragView, commit);
2493
2494 if (commit) {
2495 commitTempPlacement();
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002496 completeAndClearReorderPreviewAnimations();
Adam Cohenbebf0422012-04-11 18:06:28 -07002497 setItemPlacementDirty(false);
2498 } else {
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002499 beginOrAdjustReorderPreviewAnimations(swapSolution, dragView,
2500 REORDER_ANIMATION_DURATION, ReorderPreviewAnimation.MODE_PREVIEW);
Adam Cohenbebf0422012-04-11 18:06:28 -07002501 }
2502 mShortcutsAndWidgets.requestLayout();
2503 }
2504 return swapSolution.isSolution;
2505 }
2506
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002507 int[] performReorder(int pixelX, int pixelY, int minSpanX, int minSpanY, int spanX, int spanY,
Adam Cohen482ed822012-03-02 14:15:13 -08002508 View dragView, int[] result, int resultSpan[], int mode) {
Adam Cohen482ed822012-03-02 14:15:13 -08002509 // First we determine if things have moved enough to cause a different layout
Adam Cohen47a876d2012-03-19 13:21:41 -07002510 result = findNearestArea(pixelX, pixelY, spanX, spanY, result);
Adam Cohen482ed822012-03-02 14:15:13 -08002511
2512 if (resultSpan == null) {
2513 resultSpan = new int[2];
2514 }
2515
Adam Cohen19f37922012-03-21 11:59:11 -07002516 // When we are checking drop validity or actually dropping, we don't recompute the
2517 // direction vector, since we want the solution to match the preview, and it's possible
2518 // that the exact position of the item has changed to result in a new reordering outcome.
Adam Cohenb209e632012-03-27 17:09:36 -07002519 if ((mode == MODE_ON_DROP || mode == MODE_ON_DROP_EXTERNAL || mode == MODE_ACCEPT_DROP)
2520 && mPreviousReorderDirection[0] != INVALID_DIRECTION) {
Adam Cohen19f37922012-03-21 11:59:11 -07002521 mDirectionVector[0] = mPreviousReorderDirection[0];
2522 mDirectionVector[1] = mPreviousReorderDirection[1];
2523 // We reset this vector after drop
Adam Cohenb209e632012-03-27 17:09:36 -07002524 if (mode == MODE_ON_DROP || mode == MODE_ON_DROP_EXTERNAL) {
2525 mPreviousReorderDirection[0] = INVALID_DIRECTION;
2526 mPreviousReorderDirection[1] = INVALID_DIRECTION;
Adam Cohen19f37922012-03-21 11:59:11 -07002527 }
2528 } else {
2529 getDirectionVectorForDrop(pixelX, pixelY, spanX, spanY, dragView, mDirectionVector);
2530 mPreviousReorderDirection[0] = mDirectionVector[0];
2531 mPreviousReorderDirection[1] = mDirectionVector[1];
2532 }
2533
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002534 // Find a solution involving pushing / displacing any items in the way
2535 ItemConfiguration swapSolution = findReorderSolution(pixelX, pixelY, minSpanX, minSpanY,
Adam Cohen482ed822012-03-02 14:15:13 -08002536 spanX, spanY, mDirectionVector, dragView, true, new ItemConfiguration());
2537
2538 // We attempt the approach which doesn't shuffle views at all
2539 ItemConfiguration noShuffleSolution = findConfigurationNoShuffle(pixelX, pixelY, minSpanX,
2540 minSpanY, spanX, spanY, dragView, new ItemConfiguration());
2541
2542 ItemConfiguration finalSolution = null;
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002543
2544 // If the reorder solution requires resizing (shrinking) the item being dropped, we instead
2545 // favor a solution in which the item is not resized, but
Adam Cohen482ed822012-03-02 14:15:13 -08002546 if (swapSolution.isSolution && swapSolution.area() >= noShuffleSolution.area()) {
2547 finalSolution = swapSolution;
2548 } else if (noShuffleSolution.isSolution) {
2549 finalSolution = noShuffleSolution;
2550 }
2551
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002552 if (mode == MODE_SHOW_REORDER_HINT) {
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002553 if (finalSolution != null) {
Adam Cohenfe692872013-12-11 14:47:23 -08002554 beginOrAdjustReorderPreviewAnimations(finalSolution, dragView, 0,
2555 ReorderPreviewAnimation.MODE_HINT);
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002556 result[0] = finalSolution.dragViewX;
2557 result[1] = finalSolution.dragViewY;
2558 resultSpan[0] = finalSolution.dragViewSpanX;
2559 resultSpan[1] = finalSolution.dragViewSpanY;
2560 } else {
2561 result[0] = result[1] = resultSpan[0] = resultSpan[1] = -1;
2562 }
2563 return result;
2564 }
2565
Adam Cohen482ed822012-03-02 14:15:13 -08002566 boolean foundSolution = true;
2567 if (!DESTRUCTIVE_REORDER) {
2568 setUseTempCoords(true);
2569 }
2570
2571 if (finalSolution != null) {
2572 result[0] = finalSolution.dragViewX;
2573 result[1] = finalSolution.dragViewY;
2574 resultSpan[0] = finalSolution.dragViewSpanX;
2575 resultSpan[1] = finalSolution.dragViewSpanY;
2576
2577 // If we're just testing for a possible location (MODE_ACCEPT_DROP), we don't bother
2578 // committing anything or animating anything as we just want to determine if a solution
2579 // exists
2580 if (mode == MODE_DRAG_OVER || mode == MODE_ON_DROP || mode == MODE_ON_DROP_EXTERNAL) {
2581 if (!DESTRUCTIVE_REORDER) {
2582 copySolutionToTempState(finalSolution, dragView);
2583 }
2584 setItemPlacementDirty(true);
2585 animateItemsToSolution(finalSolution, dragView, mode == MODE_ON_DROP);
2586
Adam Cohen19f37922012-03-21 11:59:11 -07002587 if (!DESTRUCTIVE_REORDER &&
2588 (mode == MODE_ON_DROP || mode == MODE_ON_DROP_EXTERNAL)) {
Adam Cohen482ed822012-03-02 14:15:13 -08002589 commitTempPlacement();
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002590 completeAndClearReorderPreviewAnimations();
Adam Cohen19f37922012-03-21 11:59:11 -07002591 setItemPlacementDirty(false);
2592 } else {
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002593 beginOrAdjustReorderPreviewAnimations(finalSolution, dragView,
2594 REORDER_ANIMATION_DURATION, ReorderPreviewAnimation.MODE_PREVIEW);
Adam Cohen482ed822012-03-02 14:15:13 -08002595 }
2596 }
2597 } else {
2598 foundSolution = false;
2599 result[0] = result[1] = resultSpan[0] = resultSpan[1] = -1;
2600 }
2601
2602 if ((mode == MODE_ON_DROP || !foundSolution) && !DESTRUCTIVE_REORDER) {
2603 setUseTempCoords(false);
2604 }
Adam Cohen482ed822012-03-02 14:15:13 -08002605
Michael Jurkaa52570f2012-03-20 03:18:20 -07002606 mShortcutsAndWidgets.requestLayout();
Adam Cohen482ed822012-03-02 14:15:13 -08002607 return result;
2608 }
2609
Adam Cohen19f37922012-03-21 11:59:11 -07002610 void setItemPlacementDirty(boolean dirty) {
2611 mItemPlacementDirty = dirty;
Adam Cohen482ed822012-03-02 14:15:13 -08002612 }
Adam Cohen19f37922012-03-21 11:59:11 -07002613 boolean isItemPlacementDirty() {
2614 return mItemPlacementDirty;
Adam Cohen482ed822012-03-02 14:15:13 -08002615 }
2616
2617 private class ItemConfiguration {
Adam Cohen8baab352012-03-20 17:39:21 -07002618 HashMap<View, CellAndSpan> map = new HashMap<View, CellAndSpan>();
Adam Cohenf3900c22012-11-16 18:28:11 -08002619 private HashMap<View, CellAndSpan> savedMap = new HashMap<View, CellAndSpan>();
2620 ArrayList<View> sortedViews = new ArrayList<View>();
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002621 ArrayList<View> intersectingViews;
Adam Cohen482ed822012-03-02 14:15:13 -08002622 boolean isSolution = false;
2623 int dragViewX, dragViewY, dragViewSpanX, dragViewSpanY;
2624
Adam Cohenf3900c22012-11-16 18:28:11 -08002625 void save() {
2626 // Copy current state into savedMap
2627 for (View v: map.keySet()) {
2628 map.get(v).copy(savedMap.get(v));
2629 }
2630 }
2631
2632 void restore() {
2633 // Restore current state from savedMap
2634 for (View v: savedMap.keySet()) {
2635 savedMap.get(v).copy(map.get(v));
2636 }
2637 }
2638
2639 void add(View v, CellAndSpan cs) {
2640 map.put(v, cs);
2641 savedMap.put(v, new CellAndSpan());
2642 sortedViews.add(v);
2643 }
2644
Adam Cohen482ed822012-03-02 14:15:13 -08002645 int area() {
2646 return dragViewSpanX * dragViewSpanY;
2647 }
Adam Cohen8baab352012-03-20 17:39:21 -07002648 }
2649
2650 private class CellAndSpan {
2651 int x, y;
2652 int spanX, spanY;
2653
Adam Cohenf3900c22012-11-16 18:28:11 -08002654 public CellAndSpan() {
2655 }
2656
2657 public void copy(CellAndSpan copy) {
2658 copy.x = x;
2659 copy.y = y;
2660 copy.spanX = spanX;
2661 copy.spanY = spanY;
2662 }
2663
Adam Cohen8baab352012-03-20 17:39:21 -07002664 public CellAndSpan(int x, int y, int spanX, int spanY) {
2665 this.x = x;
2666 this.y = y;
2667 this.spanX = spanX;
2668 this.spanY = spanY;
Adam Cohen482ed822012-03-02 14:15:13 -08002669 }
Adam Cohenf3900c22012-11-16 18:28:11 -08002670
2671 public String toString() {
2672 return "(" + x + ", " + y + ": " + spanX + ", " + spanY + ")";
2673 }
2674
Adam Cohen482ed822012-03-02 14:15:13 -08002675 }
2676
Adam Cohendf035382011-04-11 17:22:04 -07002677 /**
2678 * Find a vacant area that will fit the given bounds nearest the requested
2679 * cell location. Uses Euclidean distance to score multiple vacant areas.
2680 *
2681 * @param pixelX The X location at which you want to search for a vacant area.
2682 * @param pixelY The Y location at which you want to search for a vacant area.
2683 * @param spanX Horizontal span of the object.
2684 * @param spanY Vertical span of the object.
2685 * @param ignoreView Considers space occupied by this view as unoccupied
2686 * @param result Previously returned value to possibly recycle.
2687 * @return The X, Y cell of a vacant area that can contain this object,
2688 * nearest the requested location.
2689 */
2690 int[] findNearestVacantArea(
2691 int pixelX, int pixelY, int spanX, int spanY, View ignoreView, int[] result) {
2692 return findNearestArea(pixelX, pixelY, spanX, spanY, ignoreView, true, result);
2693 }
2694
2695 /**
Adam Cohend41fbf52012-02-16 23:53:59 -08002696 * Find a vacant area 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 minSpanX The minimum horizontal span required
2702 * @param minSpanY The minimum vertical span required
2703 * @param spanX Horizontal span of the object.
2704 * @param spanY Vertical span of the object.
2705 * @param ignoreView Considers space occupied by this view as unoccupied
2706 * @param result Previously returned value to possibly recycle.
2707 * @return The X, Y cell of a vacant area that can contain this object,
2708 * nearest the requested location.
2709 */
2710 int[] findNearestVacantArea(int pixelX, int pixelY, int minSpanX, int minSpanY,
2711 int spanX, int spanY, View ignoreView, int[] result, int[] resultSpan) {
Adam Cohen482ed822012-03-02 14:15:13 -08002712 return findNearestArea(pixelX, pixelY, minSpanX, minSpanY, spanX, spanY, ignoreView, true,
2713 result, resultSpan, mOccupied);
Adam Cohend41fbf52012-02-16 23:53:59 -08002714 }
2715
2716 /**
Adam Cohendf035382011-04-11 17:22:04 -07002717 * Find a starting cell position that will fit the given bounds nearest the requested
2718 * cell location. Uses Euclidean distance to score multiple vacant areas.
2719 *
2720 * @param pixelX The X location at which you want to search for a vacant area.
2721 * @param pixelY The Y location at which you want to search for a vacant area.
2722 * @param spanX Horizontal span of the object.
2723 * @param spanY Vertical span of the object.
2724 * @param ignoreView Considers space occupied by this view as unoccupied
2725 * @param result Previously returned value to possibly recycle.
2726 * @return The X, Y cell of a vacant area that can contain this object,
2727 * nearest the requested location.
2728 */
2729 int[] findNearestArea(
2730 int pixelX, int pixelY, int spanX, int spanY, int[] result) {
2731 return findNearestArea(pixelX, pixelY, spanX, spanY, null, false, result);
2732 }
2733
Michael Jurka0280c3b2010-09-17 15:00:07 -07002734 boolean existsEmptyCell() {
2735 return findCellForSpan(null, 1, 1);
2736 }
2737
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002738 /**
Michael Jurka0280c3b2010-09-17 15:00:07 -07002739 * Finds the upper-left coordinate of the first rectangle in the grid that can
2740 * hold a cell of the specified dimensions. If intersectX and intersectY are not -1,
2741 * then this method will only return coordinates for rectangles that contain the cell
2742 * (intersectX, intersectY)
2743 *
2744 * @param cellXY The array that will contain the position of a vacant cell if such a cell
2745 * can be found.
2746 * @param spanX The horizontal span of the cell we want to find.
2747 * @param spanY The vertical span of the cell we want to find.
2748 *
2749 * @return True if a vacant cell of the specified dimension was found, false otherwise.
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07002750 */
Michael Jurka0280c3b2010-09-17 15:00:07 -07002751 boolean findCellForSpan(int[] cellXY, int spanX, int spanY) {
Adam Cohen482ed822012-03-02 14:15:13 -08002752 return findCellForSpanThatIntersectsIgnoring(cellXY, spanX, spanY, -1, -1, null, mOccupied);
Michael Jurka0280c3b2010-09-17 15:00:07 -07002753 }
2754
2755 /**
2756 * Like above, but ignores any cells occupied by the item "ignoreView"
2757 *
2758 * @param cellXY The array that will contain the position of a vacant cell if such a cell
2759 * can be found.
2760 * @param spanX The horizontal span of the cell we want to find.
2761 * @param spanY The vertical span of the cell we want to find.
2762 * @param ignoreView The home screen item we should treat as not occupying any space
2763 * @return
2764 */
2765 boolean findCellForSpanIgnoring(int[] cellXY, int spanX, int spanY, View ignoreView) {
Adam Cohen482ed822012-03-02 14:15:13 -08002766 return findCellForSpanThatIntersectsIgnoring(cellXY, spanX, spanY, -1, -1,
2767 ignoreView, mOccupied);
Michael Jurka0280c3b2010-09-17 15:00:07 -07002768 }
2769
2770 /**
2771 * Like above, but if intersectX and intersectY are not -1, then this method will try to
2772 * return coordinates for rectangles that contain the cell [intersectX, intersectY]
2773 *
2774 * @param spanX The horizontal span of the cell we want to find.
2775 * @param spanY The vertical span of the cell we want to find.
2776 * @param ignoreView The home screen item we should treat as not occupying any space
2777 * @param intersectX The X coordinate of the cell that we should try to overlap
2778 * @param intersectX The Y coordinate of the cell that we should try to overlap
2779 *
2780 * @return True if a vacant cell of the specified dimension was found, false otherwise.
2781 */
2782 boolean findCellForSpanThatIntersects(int[] cellXY, int spanX, int spanY,
2783 int intersectX, int intersectY) {
2784 return findCellForSpanThatIntersectsIgnoring(
Adam Cohen482ed822012-03-02 14:15:13 -08002785 cellXY, spanX, spanY, intersectX, intersectY, null, mOccupied);
Michael Jurka0280c3b2010-09-17 15:00:07 -07002786 }
2787
2788 /**
2789 * The superset of the above two methods
2790 */
2791 boolean findCellForSpanThatIntersectsIgnoring(int[] cellXY, int spanX, int spanY,
Adam Cohen482ed822012-03-02 14:15:13 -08002792 int intersectX, int intersectY, View ignoreView, boolean occupied[][]) {
Michael Jurkac6ee42e2010-09-30 12:04:50 -07002793 // mark space take by ignoreView as available (method checks if ignoreView is null)
Adam Cohen482ed822012-03-02 14:15:13 -08002794 markCellsAsUnoccupiedForView(ignoreView, occupied);
Michael Jurka0280c3b2010-09-17 15:00:07 -07002795
Michael Jurka28750fb2010-09-24 17:43:49 -07002796 boolean foundCell = false;
Michael Jurka0280c3b2010-09-17 15:00:07 -07002797 while (true) {
2798 int startX = 0;
2799 if (intersectX >= 0) {
2800 startX = Math.max(startX, intersectX - (spanX - 1));
2801 }
2802 int endX = mCountX - (spanX - 1);
2803 if (intersectX >= 0) {
2804 endX = Math.min(endX, intersectX + (spanX - 1) + (spanX == 1 ? 1 : 0));
2805 }
2806 int startY = 0;
2807 if (intersectY >= 0) {
2808 startY = Math.max(startY, intersectY - (spanY - 1));
2809 }
2810 int endY = mCountY - (spanY - 1);
2811 if (intersectY >= 0) {
2812 endY = Math.min(endY, intersectY + (spanY - 1) + (spanY == 1 ? 1 : 0));
2813 }
2814
Winson Chungbbc60d82010-11-11 16:34:41 -08002815 for (int y = startY; y < endY && !foundCell; y++) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002816 inner:
Winson Chungbbc60d82010-11-11 16:34:41 -08002817 for (int x = startX; x < endX; x++) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002818 for (int i = 0; i < spanX; i++) {
2819 for (int j = 0; j < spanY; j++) {
Adam Cohen482ed822012-03-02 14:15:13 -08002820 if (occupied[x + i][y + j]) {
Winson Chungbbc60d82010-11-11 16:34:41 -08002821 // small optimization: we can skip to after the column we just found
Michael Jurka0280c3b2010-09-17 15:00:07 -07002822 // an occupied cell
Winson Chungbbc60d82010-11-11 16:34:41 -08002823 x += i;
Michael Jurka0280c3b2010-09-17 15:00:07 -07002824 continue inner;
2825 }
2826 }
2827 }
2828 if (cellXY != null) {
2829 cellXY[0] = x;
2830 cellXY[1] = y;
2831 }
Michael Jurka28750fb2010-09-24 17:43:49 -07002832 foundCell = true;
2833 break;
Michael Jurka0280c3b2010-09-17 15:00:07 -07002834 }
2835 }
2836 if (intersectX == -1 && intersectY == -1) {
2837 break;
2838 } else {
2839 // if we failed to find anything, try again but without any requirements of
2840 // intersecting
2841 intersectX = -1;
2842 intersectY = -1;
2843 continue;
2844 }
2845 }
2846
Michael Jurkac6ee42e2010-09-30 12:04:50 -07002847 // re-mark space taken by ignoreView as occupied
Adam Cohen482ed822012-03-02 14:15:13 -08002848 markCellsAsOccupiedForView(ignoreView, occupied);
Michael Jurka28750fb2010-09-24 17:43:49 -07002849 return foundCell;
Michael Jurka0280c3b2010-09-17 15:00:07 -07002850 }
2851
2852 /**
Winson Chungc07918d2011-07-01 15:35:26 -07002853 * A drag event has begun over this layout.
2854 * It may have begun over this layout (in which case onDragChild is called first),
2855 * or it may have begun on another layout.
2856 */
2857 void onDragEnter() {
Adam Cohenc6cc61d2012-04-04 12:47:08 -07002858 mDragEnforcer.onDragEnter();
Winson Chungc07918d2011-07-01 15:35:26 -07002859 mDragging = true;
2860 }
2861
2862 /**
Michael Jurka0280c3b2010-09-17 15:00:07 -07002863 * Called when drag has left this CellLayout or has been completed (successfully or not)
2864 */
2865 void onDragExit() {
Adam Cohenc6cc61d2012-04-04 12:47:08 -07002866 mDragEnforcer.onDragExit();
Joe Onorato4be866d2010-10-10 11:26:02 -07002867 // This can actually be called when we aren't in a drag, e.g. when adding a new
2868 // item to this layout via the customize drawer.
2869 // Guard against that case.
2870 if (mDragging) {
2871 mDragging = false;
Patrick Dubroyde7658b2010-09-27 11:15:43 -07002872 }
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -07002873
2874 // Invalidate the drag data
Adam Cohend41fbf52012-02-16 23:53:59 -08002875 mDragCell[0] = mDragCell[1] = -1;
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -07002876 mDragOutlineAnims[mDragOutlineCurrent].animateOut();
2877 mDragOutlineCurrent = (mDragOutlineCurrent + 1) % mDragOutlineAnims.length;
Adam Cohen19f37922012-03-21 11:59:11 -07002878 revertTempState();
Michael Jurka33945b22010-12-21 18:19:38 -08002879 setIsDragOverlapping(false);
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07002880 }
2881
2882 /**
Winson Chungaafa03c2010-06-11 17:34:16 -07002883 * Mark a child as having been dropped.
Patrick Dubroyde7658b2010-09-27 11:15:43 -07002884 * At the beginning of the drag operation, the child may have been on another
Patrick Dubroyce34a972010-10-19 10:34:32 -07002885 * screen, but it is re-parented before this method is called.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002886 *
2887 * @param child The child that is being dropped
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002888 */
Adam Cohen716b51e2011-06-30 12:09:54 -07002889 void onDropChild(View child) {
Romain Guyd94533d2009-08-17 10:01:15 -07002890 if (child != null) {
2891 LayoutParams lp = (LayoutParams) child.getLayoutParams();
Romain Guy84f296c2009-11-04 15:00:44 -08002892 lp.dropped = true;
Romain Guyd94533d2009-08-17 10:01:15 -07002893 child.requestLayout();
Romain Guyd94533d2009-08-17 10:01:15 -07002894 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002895 }
2896
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002897 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002898 * Computes a bounding rectangle for a range of cells
Winson Chungaafa03c2010-06-11 17:34:16 -07002899 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002900 * @param cellX X coordinate of upper left corner expressed as a cell position
2901 * @param cellY Y coordinate of upper left corner expressed as a cell position
Winson Chungaafa03c2010-06-11 17:34:16 -07002902 * @param cellHSpan Width in cells
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002903 * @param cellVSpan Height in cells
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07002904 * @param resultRect Rect into which to put the results
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002905 */
Adam Cohend41fbf52012-02-16 23:53:59 -08002906 public void cellToRect(int cellX, int cellY, int cellHSpan, int cellVSpan, Rect resultRect) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002907 final int cellWidth = mCellWidth;
2908 final int cellHeight = mCellHeight;
2909 final int widthGap = mWidthGap;
2910 final int heightGap = mHeightGap;
Winson Chungaafa03c2010-06-11 17:34:16 -07002911
Winson Chung4b825dcd2011-06-19 12:41:22 -07002912 final int hStartPadding = getPaddingLeft();
2913 final int vStartPadding = getPaddingTop();
Winson Chungaafa03c2010-06-11 17:34:16 -07002914
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002915 int width = cellHSpan * cellWidth + ((cellHSpan - 1) * widthGap);
2916 int height = cellVSpan * cellHeight + ((cellVSpan - 1) * heightGap);
2917
2918 int x = hStartPadding + cellX * (cellWidth + widthGap);
2919 int y = vStartPadding + cellY * (cellHeight + heightGap);
Winson Chungaafa03c2010-06-11 17:34:16 -07002920
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07002921 resultRect.set(x, y, x + width, y + height);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002922 }
Winson Chungaafa03c2010-06-11 17:34:16 -07002923
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002924 /**
Winson Chungaafa03c2010-06-11 17:34:16 -07002925 * Computes the required horizontal and vertical cell spans to always
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002926 * fit the given rectangle.
Winson Chungaafa03c2010-06-11 17:34:16 -07002927 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002928 * @param width Width in pixels
2929 * @param height Height in pixels
Patrick Dubroy8f86ddc2010-07-16 13:55:32 -07002930 * @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 -08002931 */
Winson Chung66700732013-08-20 16:56:15 -07002932 public static int[] rectToCell(int width, int height, int[] result) {
Winson Chung5f8afe62013-08-12 16:19:28 -07002933 LauncherAppState app = LauncherAppState.getInstance();
2934 DeviceProfile grid = app.getDynamicGrid().getDeviceProfile();
Winson Chung66700732013-08-20 16:56:15 -07002935 Rect padding = grid.getWorkspacePadding(grid.isLandscape ?
2936 CellLayout.LANDSCAPE : CellLayout.PORTRAIT);
Winson Chung5f8afe62013-08-12 16:19:28 -07002937
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002938 // Always assume we're working with the smallest span to make sure we
2939 // reserve enough space in both orientations.
Winson Chung66700732013-08-20 16:56:15 -07002940 int parentWidth = grid.calculateCellWidth(grid.widthPx
2941 - padding.left - padding.right, (int) grid.numColumns);
2942 int parentHeight = grid.calculateCellHeight(grid.heightPx
2943 - padding.top - padding.bottom, (int) grid.numRows);
2944 int smallerSize = Math.min(parentWidth, parentHeight);
Joe Onorato79e56262009-09-21 15:23:04 -04002945
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002946 // Always round up to next largest cell
Winson Chung54c725c2011-08-03 12:03:40 -07002947 int spanX = (int) Math.ceil(width / (float) smallerSize);
2948 int spanY = (int) Math.ceil(height / (float) smallerSize);
Joe Onorato79e56262009-09-21 15:23:04 -04002949
Patrick Dubroy8f86ddc2010-07-16 13:55:32 -07002950 if (result == null) {
2951 return new int[] { spanX, spanY };
2952 }
2953 result[0] = spanX;
2954 result[1] = spanY;
2955 return result;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002956 }
2957
Michael Jurkaf12c75c2011-01-25 22:41:40 -08002958 public int[] cellSpansToSize(int hSpans, int vSpans) {
2959 int[] size = new int[2];
2960 size[0] = hSpans * mCellWidth + (hSpans - 1) * mWidthGap;
2961 size[1] = vSpans * mCellHeight + (vSpans - 1) * mHeightGap;
2962 return size;
2963 }
2964
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002965 /**
Patrick Dubroy047379a2010-12-19 22:02:04 -08002966 * Calculate the grid spans needed to fit given item
2967 */
2968 public void calculateSpans(ItemInfo info) {
2969 final int minWidth;
2970 final int minHeight;
2971
2972 if (info instanceof LauncherAppWidgetInfo) {
2973 minWidth = ((LauncherAppWidgetInfo) info).minWidth;
2974 minHeight = ((LauncherAppWidgetInfo) info).minHeight;
2975 } else if (info instanceof PendingAddWidgetInfo) {
2976 minWidth = ((PendingAddWidgetInfo) info).minWidth;
2977 minHeight = ((PendingAddWidgetInfo) info).minHeight;
2978 } else {
2979 // It's not a widget, so it must be 1x1
2980 info.spanX = info.spanY = 1;
2981 return;
2982 }
2983 int[] spans = rectToCell(minWidth, minHeight, null);
2984 info.spanX = spans[0];
2985 info.spanY = spans[1];
2986 }
2987
2988 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002989 * Find the first vacant cell, if there is one.
2990 *
2991 * @param vacant Holds the x and y coordinate of the vacant cell
2992 * @param spanX Horizontal cell span.
2993 * @param spanY Vertical cell span.
Winson Chungaafa03c2010-06-11 17:34:16 -07002994 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002995 * @return True if a vacant cell was found
2996 */
2997 public boolean getVacantCell(int[] vacant, int spanX, int spanY) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002998
Michael Jurka0280c3b2010-09-17 15:00:07 -07002999 return findVacantCell(vacant, spanX, spanY, mCountX, mCountY, mOccupied);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003000 }
3001
3002 static boolean findVacantCell(int[] vacant, int spanX, int spanY,
3003 int xCount, int yCount, boolean[][] occupied) {
3004
Adam Cohen2801caf2011-05-13 20:57:39 -07003005 for (int y = 0; y < yCount; y++) {
3006 for (int x = 0; x < xCount; x++) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003007 boolean available = !occupied[x][y];
3008out: for (int i = x; i < x + spanX - 1 && x < xCount; i++) {
3009 for (int j = y; j < y + spanY - 1 && y < yCount; j++) {
3010 available = available && !occupied[i][j];
3011 if (!available) break out;
3012 }
3013 }
3014
3015 if (available) {
3016 vacant[0] = x;
3017 vacant[1] = y;
3018 return true;
3019 }
3020 }
3021 }
3022
3023 return false;
3024 }
3025
Michael Jurka0280c3b2010-09-17 15:00:07 -07003026 private void clearOccupiedCells() {
3027 for (int x = 0; x < mCountX; x++) {
3028 for (int y = 0; y < mCountY; y++) {
3029 mOccupied[x][y] = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003030 }
3031 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07003032 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003033
Adam Cohend41fbf52012-02-16 23:53:59 -08003034 public void onMove(View view, int newCellX, int newCellY, int newSpanX, int newSpanY) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07003035 markCellsAsUnoccupiedForView(view);
Adam Cohen482ed822012-03-02 14:15:13 -08003036 markCellsForView(newCellX, newCellY, newSpanX, newSpanY, mOccupied, true);
Michael Jurka0280c3b2010-09-17 15:00:07 -07003037 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003038
Adam Cohend4844c32011-02-18 19:25:06 -08003039 public void markCellsAsOccupiedForView(View view) {
Adam Cohen482ed822012-03-02 14:15:13 -08003040 markCellsAsOccupiedForView(view, mOccupied);
3041 }
3042 public void markCellsAsOccupiedForView(View view, boolean[][] occupied) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07003043 if (view == null || view.getParent() != mShortcutsAndWidgets) return;
Michael Jurka0280c3b2010-09-17 15:00:07 -07003044 LayoutParams lp = (LayoutParams) view.getLayoutParams();
Adam Cohen482ed822012-03-02 14:15:13 -08003045 markCellsForView(lp.cellX, lp.cellY, lp.cellHSpan, lp.cellVSpan, occupied, true);
Michael Jurka0280c3b2010-09-17 15:00:07 -07003046 }
3047
Adam Cohend4844c32011-02-18 19:25:06 -08003048 public void markCellsAsUnoccupiedForView(View view) {
Adam Cohen482ed822012-03-02 14:15:13 -08003049 markCellsAsUnoccupiedForView(view, mOccupied);
3050 }
3051 public void markCellsAsUnoccupiedForView(View view, boolean occupied[][]) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07003052 if (view == null || view.getParent() != mShortcutsAndWidgets) return;
Michael Jurka0280c3b2010-09-17 15:00:07 -07003053 LayoutParams lp = (LayoutParams) view.getLayoutParams();
Adam Cohen482ed822012-03-02 14:15:13 -08003054 markCellsForView(lp.cellX, lp.cellY, lp.cellHSpan, lp.cellVSpan, occupied, false);
Michael Jurka0280c3b2010-09-17 15:00:07 -07003055 }
3056
Adam Cohen482ed822012-03-02 14:15:13 -08003057 private void markCellsForView(int cellX, int cellY, int spanX, int spanY, boolean[][] occupied,
3058 boolean value) {
3059 if (cellX < 0 || cellY < 0) return;
Michael Jurka0280c3b2010-09-17 15:00:07 -07003060 for (int x = cellX; x < cellX + spanX && x < mCountX; x++) {
3061 for (int y = cellY; y < cellY + spanY && y < mCountY; y++) {
Adam Cohen482ed822012-03-02 14:15:13 -08003062 occupied[x][y] = value;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003063 }
3064 }
3065 }
3066
Adam Cohen2801caf2011-05-13 20:57:39 -07003067 public int getDesiredWidth() {
Michael Jurka8b805b12012-04-18 14:23:14 -07003068 return getPaddingLeft() + getPaddingRight() + (mCountX * mCellWidth) +
Adam Cohen2801caf2011-05-13 20:57:39 -07003069 (Math.max((mCountX - 1), 0) * mWidthGap);
3070 }
3071
3072 public int getDesiredHeight() {
Michael Jurka8b805b12012-04-18 14:23:14 -07003073 return getPaddingTop() + getPaddingBottom() + (mCountY * mCellHeight) +
Adam Cohen2801caf2011-05-13 20:57:39 -07003074 (Math.max((mCountY - 1), 0) * mHeightGap);
3075 }
3076
Michael Jurka66d72172011-04-12 16:29:25 -07003077 public boolean isOccupied(int x, int y) {
3078 if (x < mCountX && y < mCountY) {
3079 return mOccupied[x][y];
3080 } else {
3081 throw new RuntimeException("Position exceeds the bound of this CellLayout");
3082 }
3083 }
3084
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003085 @Override
3086 public ViewGroup.LayoutParams generateLayoutParams(AttributeSet attrs) {
3087 return new CellLayout.LayoutParams(getContext(), attrs);
3088 }
3089
3090 @Override
3091 protected boolean checkLayoutParams(ViewGroup.LayoutParams p) {
3092 return p instanceof CellLayout.LayoutParams;
3093 }
3094
3095 @Override
3096 protected ViewGroup.LayoutParams generateLayoutParams(ViewGroup.LayoutParams p) {
3097 return new CellLayout.LayoutParams(p);
3098 }
3099
Winson Chungaafa03c2010-06-11 17:34:16 -07003100 public static class CellLayoutAnimationController extends LayoutAnimationController {
3101 public CellLayoutAnimationController(Animation animation, float delay) {
3102 super(animation, delay);
3103 }
3104
3105 @Override
3106 protected long getDelayForView(View view) {
3107 return (int) (Math.random() * 150);
3108 }
3109 }
3110
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003111 public static class LayoutParams extends ViewGroup.MarginLayoutParams {
3112 /**
3113 * Horizontal location of the item in the grid.
3114 */
3115 @ViewDebug.ExportedProperty
3116 public int cellX;
3117
3118 /**
3119 * Vertical location of the item in the grid.
3120 */
3121 @ViewDebug.ExportedProperty
3122 public int cellY;
3123
3124 /**
Adam Cohen482ed822012-03-02 14:15:13 -08003125 * Temporary horizontal location of the item in the grid during reorder
3126 */
3127 public int tmpCellX;
3128
3129 /**
3130 * Temporary vertical location of the item in the grid during reorder
3131 */
3132 public int tmpCellY;
3133
3134 /**
3135 * Indicates that the temporary coordinates should be used to layout the items
3136 */
3137 public boolean useTmpCoords;
3138
3139 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003140 * Number of cells spanned horizontally by the item.
3141 */
3142 @ViewDebug.ExportedProperty
3143 public int cellHSpan;
3144
3145 /**
3146 * Number of cells spanned vertically by the item.
3147 */
3148 @ViewDebug.ExportedProperty
3149 public int cellVSpan;
Winson Chungaafa03c2010-06-11 17:34:16 -07003150
Adam Cohen1b607ed2011-03-03 17:26:50 -08003151 /**
3152 * Indicates whether the item will set its x, y, width and height parameters freely,
3153 * or whether these will be computed based on cellX, cellY, cellHSpan and cellVSpan.
3154 */
Adam Cohend4844c32011-02-18 19:25:06 -08003155 public boolean isLockedToGrid = true;
3156
Adam Cohen482ed822012-03-02 14:15:13 -08003157 /**
Adam Cohenec40b2b2013-07-23 15:52:40 -07003158 * Indicates that this item should use the full extents of its parent.
3159 */
3160 public boolean isFullscreen = false;
3161
3162 /**
Adam Cohen482ed822012-03-02 14:15:13 -08003163 * Indicates whether this item can be reordered. Always true except in the case of the
3164 * the AllApps button.
3165 */
3166 public boolean canReorder = true;
3167
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003168 // X coordinate of the view in the layout.
3169 @ViewDebug.ExportedProperty
3170 int x;
3171 // Y coordinate of the view in the layout.
3172 @ViewDebug.ExportedProperty
3173 int y;
3174
Romain Guy84f296c2009-11-04 15:00:44 -08003175 boolean dropped;
Romain Guyfcb9e712009-10-02 16:06:52 -07003176
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003177 public LayoutParams(Context c, AttributeSet attrs) {
3178 super(c, attrs);
3179 cellHSpan = 1;
3180 cellVSpan = 1;
3181 }
3182
3183 public LayoutParams(ViewGroup.LayoutParams source) {
3184 super(source);
3185 cellHSpan = 1;
3186 cellVSpan = 1;
3187 }
Winson Chungaafa03c2010-06-11 17:34:16 -07003188
3189 public LayoutParams(LayoutParams source) {
3190 super(source);
3191 this.cellX = source.cellX;
3192 this.cellY = source.cellY;
3193 this.cellHSpan = source.cellHSpan;
3194 this.cellVSpan = source.cellVSpan;
3195 }
3196
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003197 public LayoutParams(int cellX, int cellY, int cellHSpan, int cellVSpan) {
Romain Guy8f19cdd2010-01-08 15:07:00 -08003198 super(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003199 this.cellX = cellX;
3200 this.cellY = cellY;
3201 this.cellHSpan = cellHSpan;
3202 this.cellVSpan = cellVSpan;
3203 }
3204
Adam Cohen2374abf2013-04-16 14:56:57 -07003205 public void setup(int cellWidth, int cellHeight, int widthGap, int heightGap,
3206 boolean invertHorizontally, int colCount) {
Adam Cohend4844c32011-02-18 19:25:06 -08003207 if (isLockedToGrid) {
3208 final int myCellHSpan = cellHSpan;
3209 final int myCellVSpan = cellVSpan;
Adam Cohen2374abf2013-04-16 14:56:57 -07003210 int myCellX = useTmpCoords ? tmpCellX : cellX;
3211 int myCellY = useTmpCoords ? tmpCellY : cellY;
3212
3213 if (invertHorizontally) {
3214 myCellX = colCount - myCellX - cellHSpan;
3215 }
Adam Cohen1b607ed2011-03-03 17:26:50 -08003216
Adam Cohend4844c32011-02-18 19:25:06 -08003217 width = myCellHSpan * cellWidth + ((myCellHSpan - 1) * widthGap) -
3218 leftMargin - rightMargin;
3219 height = myCellVSpan * cellHeight + ((myCellVSpan - 1) * heightGap) -
3220 topMargin - bottomMargin;
Winson Chungeecf02d2012-03-02 17:14:58 -08003221 x = (int) (myCellX * (cellWidth + widthGap) + leftMargin);
3222 y = (int) (myCellY * (cellHeight + heightGap) + topMargin);
Adam Cohend4844c32011-02-18 19:25:06 -08003223 }
3224 }
Winson Chungaafa03c2010-06-11 17:34:16 -07003225
Winson Chungaafa03c2010-06-11 17:34:16 -07003226 public String toString() {
3227 return "(" + this.cellX + ", " + this.cellY + ")";
3228 }
Adam Cohen7f4eabe2011-04-21 16:19:16 -07003229
3230 public void setWidth(int width) {
3231 this.width = width;
3232 }
3233
3234 public int getWidth() {
3235 return width;
3236 }
3237
3238 public void setHeight(int height) {
3239 this.height = height;
3240 }
3241
3242 public int getHeight() {
3243 return height;
3244 }
3245
3246 public void setX(int x) {
3247 this.x = x;
3248 }
3249
3250 public int getX() {
3251 return x;
3252 }
3253
3254 public void setY(int y) {
3255 this.y = y;
3256 }
3257
3258 public int getY() {
3259 return y;
3260 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003261 }
3262
Michael Jurka0280c3b2010-09-17 15:00:07 -07003263 // This class stores info for two purposes:
3264 // 1. When dragging items (mDragInfo in Workspace), we store the View, its cellX & cellY,
3265 // its spanX, spanY, and the screen it is on
3266 // 2. When long clicking on an empty cell in a CellLayout, we save information about the
3267 // cellX and cellY coordinates and which page was clicked. We then set this as a tag on
3268 // the CellLayout that was long clicked
Michael Jurkae5fb0f22011-04-11 13:27:46 -07003269 static final class CellInfo {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003270 View cell;
Michael Jurkaa63c4522010-08-19 13:52:27 -07003271 int cellX = -1;
3272 int cellY = -1;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003273 int spanX;
3274 int spanY;
Adam Cohendcd297f2013-06-18 13:13:40 -07003275 long screenId;
Winson Chung3d503fb2011-07-13 17:25:49 -07003276 long container;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003277
Adam Cohene0aaa0d2014-05-12 12:44:22 -07003278 CellInfo(View v, ItemInfo info) {
3279 cell = v;
3280 cellX = info.cellX;
3281 cellY = info.cellY;
3282 spanX = info.spanX;
3283 spanY = info.spanY;
3284 screenId = info.screenId;
3285 container = info.container;
3286 }
3287
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003288 @Override
3289 public String toString() {
Winson Chungaafa03c2010-06-11 17:34:16 -07003290 return "Cell[view=" + (cell == null ? "null" : cell.getClass())
3291 + ", x=" + cellX + ", y=" + cellY + "]";
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003292 }
3293 }
Michael Jurkad771c962011-08-09 15:00:48 -07003294
3295 public boolean lastDownOnOccupiedCell() {
3296 return mLastDownOnOccupiedCell;
3297 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003298}