blob: d0b3f437617cb9321226767701fc241d9d14afc5 [file] [log] [blame]
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001/*
2 * Copyright (C) 2008 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Daniel Sandler325dc232013-06-05 22:57:57 -040017package com.android.launcher3;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080018
Joe Onorato4be866d2010-10-10 11:26:02 -070019import android.animation.Animator;
Michael Jurka629758f2012-06-14 16:18:21 -070020import android.animation.AnimatorListenerAdapter;
Brandon Keely50e6e562012-05-08 16:28:49 -070021import android.animation.AnimatorSet;
Chet Haase00397b12010-10-07 11:13:10 -070022import android.animation.TimeInterpolator;
Patrick Dubroyde7658b2010-09-27 11:15:43 -070023import android.animation.ValueAnimator;
24import android.animation.ValueAnimator.AnimatorUpdateListener;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080025import android.content.Context;
Joe Onorato79e56262009-09-21 15:23:04 -040026import android.content.res.Resources;
Winson Chungaafa03c2010-06-11 17:34:16 -070027import android.content.res.TypedArray;
Joe Onorato4be866d2010-10-10 11:26:02 -070028import android.graphics.Bitmap;
Winson Chungaafa03c2010-06-11 17:34:16 -070029import android.graphics.Canvas;
Andrew Flynn0dca1ec2012-02-29 13:33:22 -080030import android.graphics.Color;
Joe Onorato4be866d2010-10-10 11:26:02 -070031import android.graphics.Paint;
Patrick Dubroyde7658b2010-09-27 11:15:43 -070032import android.graphics.Point;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080033import android.graphics.Rect;
Adam Cohen482ed822012-03-02 14:15:13 -080034import android.graphics.drawable.ColorDrawable;
Patrick Dubroy6569f2c2010-07-12 14:25:18 -070035import android.graphics.drawable.Drawable;
Adam Cohen1462de32012-07-24 22:34:36 -070036import android.os.Parcelable;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080037import android.util.AttributeSet;
Joe Onorato4be866d2010-10-10 11:26:02 -070038import android.util.Log;
Adam Cohen1462de32012-07-24 22:34:36 -070039import android.util.SparseArray;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080040import android.view.MotionEvent;
41import android.view.View;
42import android.view.ViewDebug;
43import android.view.ViewGroup;
Winson Chungaafa03c2010-06-11 17:34:16 -070044import android.view.animation.Animation;
Winson Chung150fbab2010-09-29 17:14:26 -070045import android.view.animation.DecelerateInterpolator;
Winson Chungaafa03c2010-06-11 17:34:16 -070046import android.view.animation.LayoutAnimationController;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080047
Daniel Sandler325dc232013-06-05 22:57:57 -040048import com.android.launcher3.FolderIcon.FolderRingAnimator;
Patrick Dubroy8e58e912010-10-14 13:21:48 -070049
Adam Cohen69ce2e52011-07-03 19:25:21 -070050import java.util.ArrayList;
Adam Cohenc0dcf592011-06-01 15:30:43 -070051import java.util.Arrays;
Adam Cohenf3900c22012-11-16 18:28:11 -080052import java.util.Collections;
53import java.util.Comparator;
Adam Cohenbfbfd262011-06-13 16:55:12 -070054import java.util.HashMap;
Adam Cohend41fbf52012-02-16 23:53:59 -080055import java.util.Stack;
Adam Cohenc0dcf592011-06-01 15:30:43 -070056
Michael Jurkabdb5c532011-02-01 15:05:06 -080057public class CellLayout extends ViewGroup {
Winson Chungaafa03c2010-06-11 17:34:16 -070058 static final String TAG = "CellLayout";
59
Adam Cohen2acce882012-03-28 19:03:19 -070060 private Launcher mLauncher;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080061 private int mCellWidth;
62 private int mCellHeight;
Winson Chung11a1a532013-09-13 11:14:45 -070063 private int mFixedCellWidth;
64 private int mFixedCellHeight;
Winson Chungaafa03c2010-06-11 17:34:16 -070065
Adam Cohend22015c2010-07-26 22:02:18 -070066 private int mCountX;
67 private int mCountY;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080068
Adam Cohen234c4cd2011-07-17 21:03:04 -070069 private int mOriginalWidthGap;
70 private int mOriginalHeightGap;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080071 private int mWidthGap;
72 private int mHeightGap;
Winson Chung4b825dcd2011-06-19 12:41:22 -070073 private int mMaxGap;
Adam Cohen917e3882013-10-31 15:03:35 -070074 private boolean mDropPending = false;
Adam Cohenc50438c2014-08-19 17:43:05 -070075 private boolean mIsDragTarget = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080076
Patrick Dubroyde7658b2010-09-27 11:15:43 -070077 // These are temporary variables to prevent having to allocate a new object just to
78 // return an (x, y) value from helper functions. Do NOT use them to maintain other state.
Winson Chung0be025d2011-05-23 17:45:09 -070079 private final int[] mTmpXY = new int[2];
Patrick Dubroyde7658b2010-09-27 11:15:43 -070080 private final int[] mTmpPoint = new int[2];
Adam Cohen69ce2e52011-07-03 19:25:21 -070081 int[] mTempLocation = new int[2];
Patrick Dubroy6569f2c2010-07-12 14:25:18 -070082
The Android Open Source Project31dd5032009-03-03 19:32:27 -080083 boolean[][] mOccupied;
Adam Cohen482ed822012-03-02 14:15:13 -080084 boolean[][] mTmpOccupied;
Michael Jurkad771c962011-08-09 15:00:48 -070085 private boolean mLastDownOnOccupiedCell = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080086
Michael Jurkadee05892010-07-27 10:01:56 -070087 private OnTouchListener mInterceptTouchListener;
88
Adam Cohen69ce2e52011-07-03 19:25:21 -070089 private ArrayList<FolderRingAnimator> mFolderOuterRings = new ArrayList<FolderRingAnimator>();
Adam Cohenc51934b2011-07-26 21:07:43 -070090 private int[] mFolderLeaveBehindCell = {-1, -1};
Adam Cohen69ce2e52011-07-03 19:25:21 -070091
Adam Cohen02dcfcc2013-10-01 12:37:33 -070092 private float FOREGROUND_ALPHA_DAMPER = 0.65f;
Adam Cohenb5ba0972011-09-07 18:02:31 -070093 private int mForegroundAlpha = 0;
Michael Jurka5f1c5092010-09-03 14:15:02 -070094 private float mBackgroundAlpha;
Adam Cohen1b0aaac2010-10-28 11:11:18 -070095 private float mBackgroundAlphaMultiplier = 1.0f;
Winson Chung59a488a2013-12-10 12:32:14 -080096 private boolean mDrawBackground = true;
Adam Cohenf34bab52010-09-30 14:11:56 -070097
Michael Jurka33945b22010-12-21 18:19:38 -080098 private Drawable mNormalBackground;
Michael Jurka33945b22010-12-21 18:19:38 -080099 private Drawable mActiveGlowBackground;
Adam Cohenb5ba0972011-09-07 18:02:31 -0700100 private Drawable mOverScrollForegroundDrawable;
101 private Drawable mOverScrollLeft;
102 private Drawable mOverScrollRight;
Michael Jurka18014792010-10-14 09:01:34 -0700103 private Rect mBackgroundRect;
Adam Cohenb5ba0972011-09-07 18:02:31 -0700104 private Rect mForegroundRect;
Adam Cohenb5ba0972011-09-07 18:02:31 -0700105 private int mForegroundPadding;
Patrick Dubroy1262e362010-10-06 15:49:50 -0700106
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700107 // These values allow a fixed measurement to be set on the CellLayout.
108 private int mFixedWidth = -1;
109 private int mFixedHeight = -1;
110
Michael Jurka33945b22010-12-21 18:19:38 -0800111 // If we're actively dragging something over this screen, mIsDragOverlapping is true
112 private boolean mIsDragOverlapping = false;
Adam Cohendedbd962013-07-11 14:21:49 -0700113 boolean mUseActiveGlowBackground = false;
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700114
Winson Chung150fbab2010-09-29 17:14:26 -0700115 // These arrays are used to implement the drag visualization on x-large screens.
Joe Onorato4be866d2010-10-10 11:26:02 -0700116 // They are used as circular arrays, indexed by mDragOutlineCurrent.
Adam Cohend41fbf52012-02-16 23:53:59 -0800117 private Rect[] mDragOutlines = new Rect[4];
Chet Haase472b2812010-10-14 07:02:04 -0700118 private float[] mDragOutlineAlphas = new float[mDragOutlines.length];
Joe Onorato4be866d2010-10-10 11:26:02 -0700119 private InterruptibleInOutAnimator[] mDragOutlineAnims =
120 new InterruptibleInOutAnimator[mDragOutlines.length];
Winson Chung150fbab2010-09-29 17:14:26 -0700121
122 // Used as an index into the above 3 arrays; indicates which is the most current value.
Joe Onorato4be866d2010-10-10 11:26:02 -0700123 private int mDragOutlineCurrent = 0;
Patrick Dubroy8e58e912010-10-14 13:21:48 -0700124 private final Paint mDragOutlinePaint = new Paint();
Winson Chung150fbab2010-09-29 17:14:26 -0700125
Sunny Goyal508da152014-08-14 10:53:27 -0700126 private final FastBitmapView mTouchFeedbackView;
Patrick Dubroy96864c32011-03-10 17:17:23 -0800127
Adam Cohen482ed822012-03-02 14:15:13 -0800128 private HashMap<CellLayout.LayoutParams, Animator> mReorderAnimators = new
129 HashMap<CellLayout.LayoutParams, Animator>();
Adam Cohenfa3c58f2013-12-06 16:10:55 -0800130 private HashMap<View, ReorderPreviewAnimation>
131 mShakeAnimators = new HashMap<View, ReorderPreviewAnimation>();
Adam Cohen19f37922012-03-21 11:59:11 -0700132
133 private boolean mItemPlacementDirty = false;
Adam Cohenbfbfd262011-06-13 16:55:12 -0700134
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700135 // When a drag operation is in progress, holds the nearest cell to the touch point
136 private final int[] mDragCell = new int[2];
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800137
Joe Onorato4be866d2010-10-10 11:26:02 -0700138 private boolean mDragging = false;
139
Patrick Dubroyce34a972010-10-19 10:34:32 -0700140 private TimeInterpolator mEaseOutInterpolator;
Michael Jurkaa52570f2012-03-20 03:18:20 -0700141 private ShortcutAndWidgetContainer mShortcutsAndWidgets;
Patrick Dubroyce34a972010-10-19 10:34:32 -0700142
Andrew Flynn0dca1ec2012-02-29 13:33:22 -0800143 private boolean mIsHotseat = false;
Adam Cohen307fe232012-08-16 17:55:58 -0700144 private float mHotseatScale = 1f;
Andrew Flynn0dca1ec2012-02-29 13:33:22 -0800145
Adam Cohenfa3c58f2013-12-06 16:10:55 -0800146 public static final int MODE_SHOW_REORDER_HINT = 0;
147 public static final int MODE_DRAG_OVER = 1;
148 public static final int MODE_ON_DROP = 2;
149 public static final int MODE_ON_DROP_EXTERNAL = 3;
150 public static final int MODE_ACCEPT_DROP = 4;
Adam Cohen19f37922012-03-21 11:59:11 -0700151 private static final boolean DESTRUCTIVE_REORDER = false;
Adam Cohen482ed822012-03-02 14:15:13 -0800152 private static final boolean DEBUG_VISUALIZE_OCCUPIED = false;
153
Adam Cohena897f392012-04-27 18:12:05 -0700154 static final int LANDSCAPE = 0;
155 static final int PORTRAIT = 1;
156
Adam Cohenfa3c58f2013-12-06 16:10:55 -0800157 private static final float REORDER_PREVIEW_MAGNITUDE = 0.12f;
Adam Cohen19f37922012-03-21 11:59:11 -0700158 private static final int REORDER_ANIMATION_DURATION = 150;
Adam Cohenfa3c58f2013-12-06 16:10:55 -0800159 private float mReorderPreviewAnimationMagnitude;
Adam Cohen19f37922012-03-21 11:59:11 -0700160
Adam Cohen482ed822012-03-02 14:15:13 -0800161 private ArrayList<View> mIntersectingViews = new ArrayList<View>();
162 private Rect mOccupiedRect = new Rect();
163 private int[] mDirectionVector = new int[2];
Adam Cohen19f37922012-03-21 11:59:11 -0700164 int[] mPreviousReorderDirection = new int[2];
Adam Cohenb209e632012-03-27 17:09:36 -0700165 private static final int INVALID_DIRECTION = -100;
Adam Cohenc6cc61d2012-04-04 12:47:08 -0700166 private DropTarget.DragEnforcer mDragEnforcer;
Adam Cohen482ed822012-03-02 14:15:13 -0800167
Winson Chung3a6e7f32013-10-09 15:50:52 -0700168 private Rect mTempRect = new Rect();
169
Michael Jurkaca993832012-06-29 15:17:04 -0700170 private final static Paint sPaint = new Paint();
Romain Guy8a0bff52012-05-06 13:14:33 -0700171
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800172 public CellLayout(Context context) {
173 this(context, null);
174 }
175
176 public CellLayout(Context context, AttributeSet attrs) {
177 this(context, attrs, 0);
178 }
179
180 public CellLayout(Context context, AttributeSet attrs, int defStyle) {
181 super(context, attrs, defStyle);
Michael Jurka8b805b12012-04-18 14:23:14 -0700182 mDragEnforcer = new DropTarget.DragEnforcer(context);
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700183
184 // A ViewGroup usually does not draw, but CellLayout needs to draw a rectangle to show
185 // the user where a dragged item will land when dropped.
186 setWillNotDraw(false);
Romain Guyce3cbd12013-02-25 15:00:36 -0800187 setClipToPadding(false);
Adam Cohen2acce882012-03-28 19:03:19 -0700188 mLauncher = (Launcher) context;
Michael Jurkaa63c4522010-08-19 13:52:27 -0700189
Winson Chung892c74d2013-08-22 16:15:50 -0700190 LauncherAppState app = LauncherAppState.getInstance();
191 DeviceProfile grid = app.getDynamicGrid().getDeviceProfile();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800192 TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.CellLayout, defStyle, 0);
193
Winson Chung11a1a532013-09-13 11:14:45 -0700194 mCellWidth = mCellHeight = -1;
Nilesh Agrawal5f7099a2014-01-02 15:54:57 -0800195 mFixedCellWidth = mFixedCellHeight = -1;
Winson Chung5f8afe62013-08-12 16:19:28 -0700196 mWidthGap = mOriginalWidthGap = 0;
197 mHeightGap = mOriginalHeightGap = 0;
198 mMaxGap = Integer.MAX_VALUE;
Winson Chung892c74d2013-08-22 16:15:50 -0700199 mCountX = (int) grid.numColumns;
200 mCountY = (int) grid.numRows;
Michael Jurka0280c3b2010-09-17 15:00:07 -0700201 mOccupied = new boolean[mCountX][mCountY];
Adam Cohen482ed822012-03-02 14:15:13 -0800202 mTmpOccupied = new boolean[mCountX][mCountY];
Adam Cohen5b53f292012-03-29 14:30:35 -0700203 mPreviousReorderDirection[0] = INVALID_DIRECTION;
204 mPreviousReorderDirection[1] = INVALID_DIRECTION;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800205
206 a.recycle();
207
208 setAlwaysDrawnWithCacheEnabled(false);
209
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700210 final Resources res = getResources();
Winson Chung6e1c0d32013-10-25 15:24:24 -0700211 mHotseatScale = (float) grid.hotseatIconSizePx / grid.iconSizePx;
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700212
Adam Cohen410f3cd2013-09-22 12:09:32 -0700213 mNormalBackground = res.getDrawable(R.drawable.screenpanel);
214 mActiveGlowBackground = res.getDrawable(R.drawable.screenpanel_hover);
Michael Jurka33945b22010-12-21 18:19:38 -0800215
Adam Cohenb5ba0972011-09-07 18:02:31 -0700216 mOverScrollLeft = res.getDrawable(R.drawable.overscroll_glow_left);
217 mOverScrollRight = res.getDrawable(R.drawable.overscroll_glow_right);
218 mForegroundPadding =
219 res.getDimensionPixelSize(R.dimen.workspace_overscroll_drawable_padding);
Michael Jurka33945b22010-12-21 18:19:38 -0800220
Adam Cohenfa3c58f2013-12-06 16:10:55 -0800221 mReorderPreviewAnimationMagnitude = (REORDER_PREVIEW_MAGNITUDE *
Winson Chung5f8afe62013-08-12 16:19:28 -0700222 grid.iconSizePx);
Adam Cohen19f37922012-03-21 11:59:11 -0700223
Winson Chungb26f3d62011-06-02 10:49:29 -0700224 mNormalBackground.setFilterBitmap(true);
Winson Chungb26f3d62011-06-02 10:49:29 -0700225 mActiveGlowBackground.setFilterBitmap(true);
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700226
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700227 // Initialize the data structures used for the drag visualization.
Patrick Dubroyce34a972010-10-19 10:34:32 -0700228 mEaseOutInterpolator = new DecelerateInterpolator(2.5f); // Quint ease out
Winson Chungb8c69f32011-10-19 21:36:08 -0700229 mDragCell[0] = mDragCell[1] = -1;
Joe Onorato4be866d2010-10-10 11:26:02 -0700230 for (int i = 0; i < mDragOutlines.length; i++) {
Adam Cohend41fbf52012-02-16 23:53:59 -0800231 mDragOutlines[i] = new Rect(-1, -1, -1, -1);
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700232 }
233
234 // When dragging things around the home screens, we show a green outline of
235 // where the item will land. The outlines gradually fade out, leaving a trail
236 // behind the drag path.
237 // Set up all the animations that are used to implement this fading.
238 final int duration = res.getInteger(R.integer.config_dragOutlineFadeTime);
Chet Haase472b2812010-10-14 07:02:04 -0700239 final float fromAlphaValue = 0;
240 final float toAlphaValue = (float)res.getInteger(R.integer.config_dragOutlineMaxAlpha);
Joe Onorato4be866d2010-10-10 11:26:02 -0700241
Patrick Dubroy8e58e912010-10-14 13:21:48 -0700242 Arrays.fill(mDragOutlineAlphas, fromAlphaValue);
Joe Onorato4be866d2010-10-10 11:26:02 -0700243
244 for (int i = 0; i < mDragOutlineAnims.length; i++) {
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700245 final InterruptibleInOutAnimator anim =
Michael Jurkaf1ad6082013-03-13 12:55:46 +0100246 new InterruptibleInOutAnimator(this, duration, fromAlphaValue, toAlphaValue);
Patrick Dubroyce34a972010-10-19 10:34:32 -0700247 anim.getAnimator().setInterpolator(mEaseOutInterpolator);
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700248 final int thisIndex = i;
Chet Haase472b2812010-10-14 07:02:04 -0700249 anim.getAnimator().addUpdateListener(new AnimatorUpdateListener() {
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700250 public void onAnimationUpdate(ValueAnimator animation) {
Joe Onorato4be866d2010-10-10 11:26:02 -0700251 final Bitmap outline = (Bitmap)anim.getTag();
252
253 // If an animation is started and then stopped very quickly, we can still
254 // get spurious updates we've cleared the tag. Guard against this.
255 if (outline == null) {
Michael Jurka3a9fced2012-04-13 14:44:29 -0700256 @SuppressWarnings("all") // suppress dead code warning
257 final boolean debug = false;
258 if (debug) {
Patrick Dubroyfe6bd872010-10-13 17:32:10 -0700259 Object val = animation.getAnimatedValue();
260 Log.d(TAG, "anim " + thisIndex + " update: " + val +
261 ", isStopped " + anim.isStopped());
262 }
Joe Onorato4be866d2010-10-10 11:26:02 -0700263 // Try to prevent it from continuing to run
264 animation.cancel();
265 } else {
Chet Haase472b2812010-10-14 07:02:04 -0700266 mDragOutlineAlphas[thisIndex] = (Float) animation.getAnimatedValue();
Adam Cohend41fbf52012-02-16 23:53:59 -0800267 CellLayout.this.invalidate(mDragOutlines[thisIndex]);
Joe Onorato4be866d2010-10-10 11:26:02 -0700268 }
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700269 }
270 });
Joe Onorato4be866d2010-10-10 11:26:02 -0700271 // The animation holds a reference to the drag outline bitmap as long is it's
272 // running. This way the bitmap can be GCed when the animations are complete.
Chet Haase472b2812010-10-14 07:02:04 -0700273 anim.getAnimator().addListener(new AnimatorListenerAdapter() {
Michael Jurka3c4c20f2010-10-28 15:36:06 -0700274 @Override
Joe Onorato4be866d2010-10-10 11:26:02 -0700275 public void onAnimationEnd(Animator animation) {
Chet Haase472b2812010-10-14 07:02:04 -0700276 if ((Float) ((ValueAnimator) animation).getAnimatedValue() == 0f) {
Joe Onorato4be866d2010-10-10 11:26:02 -0700277 anim.setTag(null);
278 }
279 }
280 });
281 mDragOutlineAnims[i] = anim;
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700282 }
Patrick Dubroyce34a972010-10-19 10:34:32 -0700283
Michael Jurka18014792010-10-14 09:01:34 -0700284 mBackgroundRect = new Rect();
Adam Cohenb5ba0972011-09-07 18:02:31 -0700285 mForegroundRect = new Rect();
Michael Jurkabea15192010-11-17 12:33:46 -0800286
Michael Jurkaa52570f2012-03-20 03:18:20 -0700287 mShortcutsAndWidgets = new ShortcutAndWidgetContainer(context);
Adam Cohen2374abf2013-04-16 14:56:57 -0700288 mShortcutsAndWidgets.setCellDimensions(mCellWidth, mCellHeight, mWidthGap, mHeightGap,
Winson Chung5f8afe62013-08-12 16:19:28 -0700289 mCountX, mCountY);
Adam Cohen2374abf2013-04-16 14:56:57 -0700290
Sunny Goyal508da152014-08-14 10:53:27 -0700291 mTouchFeedbackView = new FastBitmapView(context);
292 // Make the feedback view large enough to hold the blur bitmap.
293 addView(mTouchFeedbackView, (int) (grid.cellWidthPx * 1.5), (int) (grid.cellHeightPx * 1.5));
Michael Jurkaa52570f2012-03-20 03:18:20 -0700294 addView(mShortcutsAndWidgets);
Michael Jurka18014792010-10-14 09:01:34 -0700295 }
296
Chris Craik01f2d7f2013-10-01 14:41:56 -0700297 public void enableHardwareLayer(boolean hasLayer) {
298 mShortcutsAndWidgets.setLayerType(hasLayer ? LAYER_TYPE_HARDWARE : LAYER_TYPE_NONE, sPaint);
Michael Jurkad51f33a2012-06-28 15:35:26 -0700299 }
300
301 public void buildHardwareLayer() {
302 mShortcutsAndWidgets.buildLayer();
Adam Cohen2801caf2011-05-13 20:57:39 -0700303 }
304
Adam Cohen307fe232012-08-16 17:55:58 -0700305 public float getChildrenScale() {
306 return mIsHotseat ? mHotseatScale : 1.0f;
307 }
308
Winson Chung5f8afe62013-08-12 16:19:28 -0700309 public void setCellDimensions(int width, int height) {
Winson Chung11a1a532013-09-13 11:14:45 -0700310 mFixedCellWidth = mCellWidth = width;
311 mFixedCellHeight = mCellHeight = height;
Winson Chung5f8afe62013-08-12 16:19:28 -0700312 mShortcutsAndWidgets.setCellDimensions(mCellWidth, mCellHeight, mWidthGap, mHeightGap,
313 mCountX, mCountY);
314 }
315
Adam Cohen2801caf2011-05-13 20:57:39 -0700316 public void setGridSize(int x, int y) {
317 mCountX = x;
318 mCountY = y;
319 mOccupied = new boolean[mCountX][mCountY];
Adam Cohen482ed822012-03-02 14:15:13 -0800320 mTmpOccupied = new boolean[mCountX][mCountY];
Adam Cohen7fbec102012-03-27 12:42:19 -0700321 mTempRectStack.clear();
Adam Cohen2374abf2013-04-16 14:56:57 -0700322 mShortcutsAndWidgets.setCellDimensions(mCellWidth, mCellHeight, mWidthGap, mHeightGap,
Winson Chung5f8afe62013-08-12 16:19:28 -0700323 mCountX, mCountY);
Adam Cohen76fc0852011-06-17 13:26:23 -0700324 requestLayout();
Adam Cohen2801caf2011-05-13 20:57:39 -0700325 }
326
Adam Cohen2374abf2013-04-16 14:56:57 -0700327 // Set whether or not to invert the layout horizontally if the layout is in RTL mode.
328 public void setInvertIfRtl(boolean invert) {
329 mShortcutsAndWidgets.setInvertIfRtl(invert);
330 }
331
Adam Cohen917e3882013-10-31 15:03:35 -0700332 public void setDropPending(boolean pending) {
333 mDropPending = pending;
334 }
335
336 public boolean isDropPending() {
337 return mDropPending;
338 }
339
Adam Cohenb5ba0972011-09-07 18:02:31 -0700340 void setOverScrollAmount(float r, boolean left) {
341 if (left && mOverScrollForegroundDrawable != mOverScrollLeft) {
342 mOverScrollForegroundDrawable = mOverScrollLeft;
343 } else if (!left && mOverScrollForegroundDrawable != mOverScrollRight) {
344 mOverScrollForegroundDrawable = mOverScrollRight;
345 }
346
Adam Cohen02dcfcc2013-10-01 12:37:33 -0700347 r *= FOREGROUND_ALPHA_DAMPER;
Adam Cohenb5ba0972011-09-07 18:02:31 -0700348 mForegroundAlpha = (int) Math.round((r * 255));
349 mOverScrollForegroundDrawable.setAlpha(mForegroundAlpha);
350 invalidate();
351 }
352
Sunny Goyal508da152014-08-14 10:53:27 -0700353 void setPressedIcon(BubbleTextView icon, Bitmap background, int padding) {
354 if (icon == null || background == null) {
355 mTouchFeedbackView.setBitmap(null);
356 mTouchFeedbackView.animate().cancel();
357 } else {
358 int offset = getMeasuredWidth() - getPaddingLeft() - getPaddingRight()
359 - (mCountX * mCellWidth);
360 mTouchFeedbackView.setTranslationX(icon.getLeft() + (int) Math.ceil(offset / 2f)
361 - padding);
362 mTouchFeedbackView.setTranslationY(icon.getTop() - padding);
363 if (mTouchFeedbackView.setBitmap(background)) {
364 mTouchFeedbackView.setAlpha(0);
365 mTouchFeedbackView.animate().alpha(1)
366 .setDuration(FastBitmapDrawable.CLICK_FEEDBACK_DURATION)
367 .setInterpolator(FastBitmapDrawable.CLICK_FEEDBACK_INTERPOLATOR)
368 .start();
369 }
Patrick Dubroy96864c32011-03-10 17:17:23 -0800370 }
371 }
372
Adam Cohendedbd962013-07-11 14:21:49 -0700373 void setUseActiveGlowBackground(boolean use) {
374 mUseActiveGlowBackground = use;
375 }
376
Winson Chung59a488a2013-12-10 12:32:14 -0800377 void disableBackground() {
378 mDrawBackground = false;
379 }
380
Adam Cohenc50438c2014-08-19 17:43:05 -0700381 void disableDragTarget() {
382 mIsDragTarget = false;
383 }
384
385 boolean isDragTarget() {
386 return mIsDragTarget;
387 }
388
389 void setIsDragOverlapping(boolean isDragOverlapping) {
390 if (mIsDragOverlapping != isDragOverlapping) {
391 mIsDragOverlapping = isDragOverlapping;
392 setUseActiveGlowBackground(mIsDragOverlapping);
393 invalidate();
394 }
395 }
396
Michael Jurka33945b22010-12-21 18:19:38 -0800397 boolean getIsDragOverlapping() {
398 return mIsDragOverlapping;
399 }
400
Jeff Sharkey83f111d2009-04-20 21:03:13 -0700401 @Override
Patrick Dubroy1262e362010-10-06 15:49:50 -0700402 protected void onDraw(Canvas canvas) {
Michael Jurka3e7c7632010-10-02 16:01:03 -0700403 // When we're large, we are either drawn in a "hover" state (ie when dragging an item to
404 // a neighboring page) or with just a normal background (if backgroundAlpha > 0.0f)
405 // When we're small, we are either drawn normally or in the "accepts drops" state (during
406 // a drag). However, we also drag the mini hover background *over* one of those two
407 // backgrounds
Winson Chung59a488a2013-12-10 12:32:14 -0800408 if (mDrawBackground && mBackgroundAlpha > 0.0f) {
Adam Cohenf34bab52010-09-30 14:11:56 -0700409 Drawable bg;
Michael Jurka33945b22010-12-21 18:19:38 -0800410
Adam Cohendedbd962013-07-11 14:21:49 -0700411 if (mUseActiveGlowBackground) {
Michael Jurka33945b22010-12-21 18:19:38 -0800412 // In the mini case, we draw the active_glow bg *over* the active background
Michael Jurkabdf78552011-10-31 14:34:25 -0700413 bg = mActiveGlowBackground;
Adam Cohenf34bab52010-09-30 14:11:56 -0700414 } else {
Michael Jurkabdf78552011-10-31 14:34:25 -0700415 bg = mNormalBackground;
Adam Cohenf34bab52010-09-30 14:11:56 -0700416 }
Michael Jurka33945b22010-12-21 18:19:38 -0800417
418 bg.setAlpha((int) (mBackgroundAlpha * mBackgroundAlphaMultiplier * 255));
419 bg.setBounds(mBackgroundRect);
420 bg.draw(canvas);
Michael Jurkaa63c4522010-08-19 13:52:27 -0700421 }
Romain Guya6abce82009-11-10 02:54:41 -0800422
Patrick Dubroy8e58e912010-10-14 13:21:48 -0700423 final Paint paint = mDragOutlinePaint;
Joe Onorato4be866d2010-10-10 11:26:02 -0700424 for (int i = 0; i < mDragOutlines.length; i++) {
Chet Haase472b2812010-10-14 07:02:04 -0700425 final float alpha = mDragOutlineAlphas[i];
Joe Onorato4be866d2010-10-10 11:26:02 -0700426 if (alpha > 0) {
Adam Cohend41fbf52012-02-16 23:53:59 -0800427 final Rect r = mDragOutlines[i];
Winson Chung3a6e7f32013-10-09 15:50:52 -0700428 mTempRect.set(r);
429 Utilities.scaleRectAboutCenter(mTempRect, getChildrenScale());
Joe Onorato4be866d2010-10-10 11:26:02 -0700430 final Bitmap b = (Bitmap) mDragOutlineAnims[i].getTag();
Chet Haase472b2812010-10-14 07:02:04 -0700431 paint.setAlpha((int)(alpha + .5f));
Winson Chung3a6e7f32013-10-09 15:50:52 -0700432 canvas.drawBitmap(b, null, mTempRect, paint);
Winson Chung150fbab2010-09-29 17:14:26 -0700433 }
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700434 }
Patrick Dubroy96864c32011-03-10 17:17:23 -0800435
Adam Cohen482ed822012-03-02 14:15:13 -0800436 if (DEBUG_VISUALIZE_OCCUPIED) {
437 int[] pt = new int[2];
438 ColorDrawable cd = new ColorDrawable(Color.RED);
Adam Cohene7587d22012-05-24 18:50:02 -0700439 cd.setBounds(0, 0, mCellWidth, mCellHeight);
Adam Cohen482ed822012-03-02 14:15:13 -0800440 for (int i = 0; i < mCountX; i++) {
441 for (int j = 0; j < mCountY; j++) {
442 if (mOccupied[i][j]) {
443 cellToPoint(i, j, pt);
444 canvas.save();
445 canvas.translate(pt[0], pt[1]);
446 cd.draw(canvas);
447 canvas.restore();
448 }
449 }
450 }
451 }
452
Andrew Flynn850d2e72012-04-26 16:51:20 -0700453 int previewOffset = FolderRingAnimator.sPreviewSize;
454
Adam Cohen69ce2e52011-07-03 19:25:21 -0700455 // The folder outer / inner ring image(s)
Winson Chung5f8afe62013-08-12 16:19:28 -0700456 LauncherAppState app = LauncherAppState.getInstance();
457 DeviceProfile grid = app.getDynamicGrid().getDeviceProfile();
Adam Cohen69ce2e52011-07-03 19:25:21 -0700458 for (int i = 0; i < mFolderOuterRings.size(); i++) {
459 FolderRingAnimator fra = mFolderOuterRings.get(i);
460
Adam Cohen5108bc02013-09-20 17:04:51 -0700461 Drawable d;
462 int width, height;
Adam Cohen69ce2e52011-07-03 19:25:21 -0700463 cellToPoint(fra.mCellX, fra.mCellY, mTempLocation);
Winson Chung5f8afe62013-08-12 16:19:28 -0700464 View child = getChildAt(fra.mCellX, fra.mCellY);
Adam Cohen558f1c22013-10-09 15:15:24 -0700465
Winson Chung89f97052013-09-20 11:32:26 -0700466 if (child != null) {
Adam Cohen558f1c22013-10-09 15:15:24 -0700467 int centerX = mTempLocation[0] + mCellWidth / 2;
468 int centerY = mTempLocation[1] + previewOffset / 2 +
469 child.getPaddingTop() + grid.folderBackgroundOffset;
470
Adam Cohen5108bc02013-09-20 17:04:51 -0700471 // Draw outer ring, if it exists
472 if (FolderIcon.HAS_OUTER_RING) {
473 d = FolderRingAnimator.sSharedOuterRingDrawable;
474 width = (int) (fra.getOuterRingSize() * getChildrenScale());
475 height = width;
476 canvas.save();
477 canvas.translate(centerX - width / 2, centerY - height / 2);
478 d.setBounds(0, 0, width, height);
479 d.draw(canvas);
480 canvas.restore();
481 }
Adam Cohen69ce2e52011-07-03 19:25:21 -0700482
Winson Chung89f97052013-09-20 11:32:26 -0700483 // Draw inner ring
484 d = FolderRingAnimator.sSharedInnerRingDrawable;
485 width = (int) (fra.getInnerRingSize() * getChildrenScale());
486 height = width;
Winson Chung89f97052013-09-20 11:32:26 -0700487 canvas.save();
488 canvas.translate(centerX - width / 2, centerY - width / 2);
489 d.setBounds(0, 0, width, height);
490 d.draw(canvas);
491 canvas.restore();
492 }
Adam Cohen69ce2e52011-07-03 19:25:21 -0700493 }
Adam Cohenc51934b2011-07-26 21:07:43 -0700494
495 if (mFolderLeaveBehindCell[0] >= 0 && mFolderLeaveBehindCell[1] >= 0) {
496 Drawable d = FolderIcon.sSharedFolderLeaveBehind;
497 int width = d.getIntrinsicWidth();
498 int height = d.getIntrinsicHeight();
499
500 cellToPoint(mFolderLeaveBehindCell[0], mFolderLeaveBehindCell[1], mTempLocation);
Winson Chung5f8afe62013-08-12 16:19:28 -0700501 View child = getChildAt(mFolderLeaveBehindCell[0], mFolderLeaveBehindCell[1]);
Winson Chung89f97052013-09-20 11:32:26 -0700502 if (child != null) {
503 int centerX = mTempLocation[0] + mCellWidth / 2;
504 int centerY = mTempLocation[1] + previewOffset / 2 +
505 child.getPaddingTop() + grid.folderBackgroundOffset;
Adam Cohenc51934b2011-07-26 21:07:43 -0700506
Winson Chung89f97052013-09-20 11:32:26 -0700507 canvas.save();
508 canvas.translate(centerX - width / 2, centerY - width / 2);
509 d.setBounds(0, 0, width, height);
510 d.draw(canvas);
511 canvas.restore();
512 }
Adam Cohenc51934b2011-07-26 21:07:43 -0700513 }
Adam Cohen69ce2e52011-07-03 19:25:21 -0700514 }
515
Adam Cohenb5ba0972011-09-07 18:02:31 -0700516 @Override
517 protected void dispatchDraw(Canvas canvas) {
518 super.dispatchDraw(canvas);
519 if (mForegroundAlpha > 0) {
520 mOverScrollForegroundDrawable.setBounds(mForegroundRect);
Adam Cohenb5ba0972011-09-07 18:02:31 -0700521 mOverScrollForegroundDrawable.draw(canvas);
Adam Cohenb5ba0972011-09-07 18:02:31 -0700522 }
523 }
524
Adam Cohen69ce2e52011-07-03 19:25:21 -0700525 public void showFolderAccept(FolderRingAnimator fra) {
526 mFolderOuterRings.add(fra);
527 }
528
529 public void hideFolderAccept(FolderRingAnimator fra) {
530 if (mFolderOuterRings.contains(fra)) {
531 mFolderOuterRings.remove(fra);
532 }
533 invalidate();
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700534 }
535
Adam Cohenc51934b2011-07-26 21:07:43 -0700536 public void setFolderLeaveBehindCell(int x, int y) {
537 mFolderLeaveBehindCell[0] = x;
538 mFolderLeaveBehindCell[1] = y;
539 invalidate();
540 }
541
542 public void clearFolderLeaveBehind() {
543 mFolderLeaveBehindCell[0] = -1;
544 mFolderLeaveBehindCell[1] = -1;
545 invalidate();
546 }
547
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700548 @Override
Michael Jurkae6235dd2011-10-04 15:02:05 -0700549 public boolean shouldDelayChildPressedState() {
550 return false;
551 }
552
Adam Cohen1462de32012-07-24 22:34:36 -0700553 public void restoreInstanceState(SparseArray<Parcelable> states) {
Sunny Goyal33a152f2014-07-22 12:13:14 -0700554 try {
555 dispatchRestoreInstanceState(states);
556 } catch (IllegalArgumentException ex) {
557 if (LauncherAppState.isDogfoodBuild()) {
558 throw ex;
559 }
560 // Mismatched viewId / viewType preventing restore. Skip restore on production builds.
561 Log.e(TAG, "Ignoring an error while restoring a view instance state", ex);
562 }
Adam Cohen1462de32012-07-24 22:34:36 -0700563 }
564
Michael Jurkae6235dd2011-10-04 15:02:05 -0700565 @Override
Jeff Sharkey83f111d2009-04-20 21:03:13 -0700566 public void cancelLongPress() {
567 super.cancelLongPress();
568
569 // Cancel long press for all children
570 final int count = getChildCount();
571 for (int i = 0; i < count; i++) {
572 final View child = getChildAt(i);
573 child.cancelLongPress();
574 }
575 }
576
Michael Jurkadee05892010-07-27 10:01:56 -0700577 public void setOnInterceptTouchListener(View.OnTouchListener listener) {
578 mInterceptTouchListener = listener;
579 }
580
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800581 int getCountX() {
Adam Cohend22015c2010-07-26 22:02:18 -0700582 return mCountX;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800583 }
584
585 int getCountY() {
Adam Cohend22015c2010-07-26 22:02:18 -0700586 return mCountY;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800587 }
588
Andrew Flynn0dca1ec2012-02-29 13:33:22 -0800589 public void setIsHotseat(boolean isHotseat) {
590 mIsHotseat = isHotseat;
Winson Chung5f8afe62013-08-12 16:19:28 -0700591 mShortcutsAndWidgets.setIsHotseat(isHotseat);
Andrew Flynn0dca1ec2012-02-29 13:33:22 -0800592 }
593
Andrew Flynn0dca1ec2012-02-29 13:33:22 -0800594 public boolean addViewToCellLayout(View child, int index, int childId, LayoutParams params,
Andrew Flynn850d2e72012-04-26 16:51:20 -0700595 boolean markCells) {
Winson Chungaafa03c2010-06-11 17:34:16 -0700596 final LayoutParams lp = params;
597
Andrew Flynnde38e422012-05-08 11:22:15 -0700598 // Hotseat icons - remove text
Andrew Flynn0dca1ec2012-02-29 13:33:22 -0800599 if (child instanceof BubbleTextView) {
600 BubbleTextView bubbleChild = (BubbleTextView) child;
Winson Chung5f8afe62013-08-12 16:19:28 -0700601 bubbleChild.setTextVisibility(!mIsHotseat);
Andrew Flynn0dca1ec2012-02-29 13:33:22 -0800602 }
603
Adam Cohen307fe232012-08-16 17:55:58 -0700604 child.setScaleX(getChildrenScale());
605 child.setScaleY(getChildrenScale());
606
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800607 // Generate an id for each view, this assumes we have at most 256x256 cells
608 // per workspace screen
Adam Cohend22015c2010-07-26 22:02:18 -0700609 if (lp.cellX >= 0 && lp.cellX <= mCountX - 1 && lp.cellY >= 0 && lp.cellY <= mCountY - 1) {
Winson Chungaafa03c2010-06-11 17:34:16 -0700610 // If the horizontal or vertical span is set to -1, it is taken to
611 // mean that it spans the extent of the CellLayout
Adam Cohend22015c2010-07-26 22:02:18 -0700612 if (lp.cellHSpan < 0) lp.cellHSpan = mCountX;
613 if (lp.cellVSpan < 0) lp.cellVSpan = mCountY;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800614
Winson Chungaafa03c2010-06-11 17:34:16 -0700615 child.setId(childId);
616
Michael Jurkaa52570f2012-03-20 03:18:20 -0700617 mShortcutsAndWidgets.addView(child, index, lp);
Michael Jurkadee05892010-07-27 10:01:56 -0700618
Michael Jurkaf3ca3ab2010-10-20 17:08:24 -0700619 if (markCells) markCellsAsOccupiedForView(child);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700620
Winson Chungaafa03c2010-06-11 17:34:16 -0700621 return true;
622 }
623 return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800624 }
Michael Jurka3e7c7632010-10-02 16:01:03 -0700625
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800626 @Override
Michael Jurka0280c3b2010-09-17 15:00:07 -0700627 public void removeAllViews() {
628 clearOccupiedCells();
Michael Jurkaa52570f2012-03-20 03:18:20 -0700629 mShortcutsAndWidgets.removeAllViews();
Michael Jurka0280c3b2010-09-17 15:00:07 -0700630 }
631
632 @Override
633 public void removeAllViewsInLayout() {
Michael Jurkaa52570f2012-03-20 03:18:20 -0700634 if (mShortcutsAndWidgets.getChildCount() > 0) {
Michael Jurka7cfc2822011-08-02 20:19:24 -0700635 clearOccupiedCells();
Michael Jurkaa52570f2012-03-20 03:18:20 -0700636 mShortcutsAndWidgets.removeAllViewsInLayout();
Michael Jurka7cfc2822011-08-02 20:19:24 -0700637 }
Michael Jurka0280c3b2010-09-17 15:00:07 -0700638 }
639
Michael Jurkaf3ca3ab2010-10-20 17:08:24 -0700640 public void removeViewWithoutMarkingCells(View view) {
Michael Jurkaa52570f2012-03-20 03:18:20 -0700641 mShortcutsAndWidgets.removeView(view);
Michael Jurkaf3ca3ab2010-10-20 17:08:24 -0700642 }
643
Michael Jurka0280c3b2010-09-17 15:00:07 -0700644 @Override
645 public void removeView(View view) {
646 markCellsAsUnoccupiedForView(view);
Michael Jurkaa52570f2012-03-20 03:18:20 -0700647 mShortcutsAndWidgets.removeView(view);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700648 }
649
650 @Override
651 public void removeViewAt(int index) {
Michael Jurkaa52570f2012-03-20 03:18:20 -0700652 markCellsAsUnoccupiedForView(mShortcutsAndWidgets.getChildAt(index));
653 mShortcutsAndWidgets.removeViewAt(index);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700654 }
655
656 @Override
657 public void removeViewInLayout(View view) {
658 markCellsAsUnoccupiedForView(view);
Michael Jurkaa52570f2012-03-20 03:18:20 -0700659 mShortcutsAndWidgets.removeViewInLayout(view);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700660 }
661
662 @Override
663 public void removeViews(int start, int count) {
664 for (int i = start; i < start + count; i++) {
Michael Jurkaa52570f2012-03-20 03:18:20 -0700665 markCellsAsUnoccupiedForView(mShortcutsAndWidgets.getChildAt(i));
Michael Jurka0280c3b2010-09-17 15:00:07 -0700666 }
Michael Jurkaa52570f2012-03-20 03:18:20 -0700667 mShortcutsAndWidgets.removeViews(start, count);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700668 }
669
670 @Override
671 public void removeViewsInLayout(int start, int count) {
672 for (int i = start; i < start + count; i++) {
Michael Jurkaa52570f2012-03-20 03:18:20 -0700673 markCellsAsUnoccupiedForView(mShortcutsAndWidgets.getChildAt(i));
Michael Jurka0280c3b2010-09-17 15:00:07 -0700674 }
Michael Jurkaa52570f2012-03-20 03:18:20 -0700675 mShortcutsAndWidgets.removeViewsInLayout(start, count);
Michael Jurkaabded662011-03-04 12:06:57 -0800676 }
677
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800678 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800679 public boolean onInterceptTouchEvent(MotionEvent ev) {
Adam Cohenc1997fd2011-08-15 18:26:39 -0700680 // First we clear the tag to ensure that on every touch down we start with a fresh slate,
681 // even in the case where we return early. Not clearing here was causing bugs whereby on
682 // long-press we'd end up picking up an item from a previous drag operation.
Michael Jurkadee05892010-07-27 10:01:56 -0700683 if (mInterceptTouchListener != null && mInterceptTouchListener.onTouch(this, ev)) {
684 return true;
685 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800686
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800687 return false;
688 }
689
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700690 /**
Winson Chungaafa03c2010-06-11 17:34:16 -0700691 * Given a point, return the cell that strictly encloses that point
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800692 * @param x X coordinate of the point
693 * @param y Y coordinate of the point
694 * @param result Array of 2 ints to hold the x and y coordinate of the cell
695 */
696 void pointToCellExact(int x, int y, int[] result) {
Winson Chung4b825dcd2011-06-19 12:41:22 -0700697 final int hStartPadding = getPaddingLeft();
698 final int vStartPadding = getPaddingTop();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800699
700 result[0] = (x - hStartPadding) / (mCellWidth + mWidthGap);
701 result[1] = (y - vStartPadding) / (mCellHeight + mHeightGap);
702
Adam Cohend22015c2010-07-26 22:02:18 -0700703 final int xAxis = mCountX;
704 final int yAxis = mCountY;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800705
706 if (result[0] < 0) result[0] = 0;
707 if (result[0] >= xAxis) result[0] = xAxis - 1;
708 if (result[1] < 0) result[1] = 0;
709 if (result[1] >= yAxis) result[1] = yAxis - 1;
710 }
Winson Chungaafa03c2010-06-11 17:34:16 -0700711
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800712 /**
713 * Given a point, return the cell that most closely encloses that point
714 * @param x X coordinate of the point
715 * @param y Y coordinate of the point
716 * @param result Array of 2 ints to hold the x and y coordinate of the cell
717 */
718 void pointToCellRounded(int x, int y, int[] result) {
719 pointToCellExact(x + (mCellWidth / 2), y + (mCellHeight / 2), result);
720 }
721
722 /**
723 * Given a cell coordinate, return the point that represents the upper left corner of that cell
Winson Chungaafa03c2010-06-11 17:34:16 -0700724 *
725 * @param cellX X coordinate of the cell
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800726 * @param cellY Y coordinate of the cell
Winson Chungaafa03c2010-06-11 17:34:16 -0700727 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800728 * @param result Array of 2 ints to hold the x and y coordinate of the point
729 */
730 void cellToPoint(int cellX, int cellY, int[] result) {
Winson Chung4b825dcd2011-06-19 12:41:22 -0700731 final int hStartPadding = getPaddingLeft();
732 final int vStartPadding = getPaddingTop();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800733
734 result[0] = hStartPadding + cellX * (mCellWidth + mWidthGap);
735 result[1] = vStartPadding + cellY * (mCellHeight + mHeightGap);
736 }
737
Adam Cohene3e27a82011-04-15 12:07:39 -0700738 /**
Adam Cohen482ed822012-03-02 14:15:13 -0800739 * Given a cell coordinate, return the point that represents the center of the cell
Adam Cohene3e27a82011-04-15 12:07:39 -0700740 *
741 * @param cellX X coordinate of the cell
742 * @param cellY Y coordinate of the cell
743 *
744 * @param result Array of 2 ints to hold the x and y coordinate of the point
745 */
746 void cellToCenterPoint(int cellX, int cellY, int[] result) {
Adam Cohen47a876d2012-03-19 13:21:41 -0700747 regionToCenterPoint(cellX, cellY, 1, 1, result);
748 }
749
750 /**
751 * Given a cell coordinate and span return the point that represents the center of the regio
752 *
753 * @param cellX X coordinate of the cell
754 * @param cellY Y coordinate of the cell
755 *
756 * @param result Array of 2 ints to hold the x and y coordinate of the point
757 */
758 void regionToCenterPoint(int cellX, int cellY, int spanX, int spanY, int[] result) {
Winson Chung4b825dcd2011-06-19 12:41:22 -0700759 final int hStartPadding = getPaddingLeft();
760 final int vStartPadding = getPaddingTop();
Adam Cohen47a876d2012-03-19 13:21:41 -0700761 result[0] = hStartPadding + cellX * (mCellWidth + mWidthGap) +
762 (spanX * mCellWidth + (spanX - 1) * mWidthGap) / 2;
763 result[1] = vStartPadding + cellY * (mCellHeight + mHeightGap) +
764 (spanY * mCellHeight + (spanY - 1) * mHeightGap) / 2;
Adam Cohene3e27a82011-04-15 12:07:39 -0700765 }
766
Adam Cohen19f37922012-03-21 11:59:11 -0700767 /**
768 * Given a cell coordinate and span fills out a corresponding pixel rect
769 *
770 * @param cellX X coordinate of the cell
771 * @param cellY Y coordinate of the cell
772 * @param result Rect in which to write the result
773 */
774 void regionToRect(int cellX, int cellY, int spanX, int spanY, Rect result) {
775 final int hStartPadding = getPaddingLeft();
776 final int vStartPadding = getPaddingTop();
777 final int left = hStartPadding + cellX * (mCellWidth + mWidthGap);
778 final int top = vStartPadding + cellY * (mCellHeight + mHeightGap);
779 result.set(left, top, left + (spanX * mCellWidth + (spanX - 1) * mWidthGap),
780 top + (spanY * mCellHeight + (spanY - 1) * mHeightGap));
781 }
782
Adam Cohen482ed822012-03-02 14:15:13 -0800783 public float getDistanceFromCell(float x, float y, int[] cell) {
784 cellToCenterPoint(cell[0], cell[1], mTmpPoint);
785 float distance = (float) Math.sqrt( Math.pow(x - mTmpPoint[0], 2) +
786 Math.pow(y - mTmpPoint[1], 2));
787 return distance;
788 }
789
Romain Guy84f296c2009-11-04 15:00:44 -0800790 int getCellWidth() {
791 return mCellWidth;
792 }
793
794 int getCellHeight() {
795 return mCellHeight;
796 }
797
Adam Cohend4844c32011-02-18 19:25:06 -0800798 int getWidthGap() {
799 return mWidthGap;
800 }
801
802 int getHeightGap() {
803 return mHeightGap;
804 }
805
Adam Cohen7f4eabe2011-04-21 16:19:16 -0700806 Rect getContentRect(Rect r) {
807 if (r == null) {
808 r = new Rect();
809 }
810 int left = getPaddingLeft();
811 int top = getPaddingTop();
Michael Jurka8b805b12012-04-18 14:23:14 -0700812 int right = left + getWidth() - getPaddingLeft() - getPaddingRight();
813 int bottom = top + getHeight() - getPaddingTop() - getPaddingBottom();
Adam Cohen7f4eabe2011-04-21 16:19:16 -0700814 r.set(left, top, right, bottom);
815 return r;
816 }
817
Winson Chungfe411c82013-09-26 16:07:17 -0700818 /** Return a rect that has the cellWidth/cellHeight (left, top), and
819 * widthGap/heightGap (right, bottom) */
Winson Chung66700732013-08-20 16:56:15 -0700820 static void getMetrics(Rect metrics, int paddedMeasureWidth,
821 int paddedMeasureHeight, int countX, int countY) {
Winson Chung5f8afe62013-08-12 16:19:28 -0700822 LauncherAppState app = LauncherAppState.getInstance();
823 DeviceProfile grid = app.getDynamicGrid().getDeviceProfile();
Winson Chung66700732013-08-20 16:56:15 -0700824 metrics.set(grid.calculateCellWidth(paddedMeasureWidth, countX),
825 grid.calculateCellHeight(paddedMeasureHeight, countY), 0, 0);
Adam Cohenf4bd5792012-04-27 11:35:29 -0700826 }
827
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700828 public void setFixedSize(int width, int height) {
829 mFixedWidth = width;
830 mFixedHeight = height;
831 }
832
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800833 @Override
834 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
Winson Chung5f8afe62013-08-12 16:19:28 -0700835 LauncherAppState app = LauncherAppState.getInstance();
836 DeviceProfile grid = app.getDynamicGrid().getDeviceProfile();
837
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800838 int widthSpecMode = MeasureSpec.getMode(widthMeasureSpec);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800839 int heightSpecMode = MeasureSpec.getMode(heightMeasureSpec);
Winson Chung5f8afe62013-08-12 16:19:28 -0700840 int widthSize = MeasureSpec.getSize(widthMeasureSpec);
841 int heightSize = MeasureSpec.getSize(heightMeasureSpec);
Winson Chung2d75f122013-09-23 16:53:31 -0700842 int childWidthSize = widthSize - (getPaddingLeft() + getPaddingRight());
843 int childHeightSize = heightSize - (getPaddingTop() + getPaddingBottom());
Winson Chung11a1a532013-09-13 11:14:45 -0700844 if (mFixedCellWidth < 0 || mFixedCellHeight < 0) {
Winson Chung2d75f122013-09-23 16:53:31 -0700845 int cw = grid.calculateCellWidth(childWidthSize, mCountX);
846 int ch = grid.calculateCellHeight(childHeightSize, mCountY);
Winson Chung11a1a532013-09-13 11:14:45 -0700847 if (cw != mCellWidth || ch != mCellHeight) {
848 mCellWidth = cw;
849 mCellHeight = ch;
850 mShortcutsAndWidgets.setCellDimensions(mCellWidth, mCellHeight, mWidthGap,
851 mHeightGap, mCountX, mCountY);
852 }
Winson Chung5f8afe62013-08-12 16:19:28 -0700853 }
Winson Chungaafa03c2010-06-11 17:34:16 -0700854
Winson Chung2d75f122013-09-23 16:53:31 -0700855 int newWidth = childWidthSize;
856 int newHeight = childHeightSize;
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700857 if (mFixedWidth > 0 && mFixedHeight > 0) {
858 newWidth = mFixedWidth;
859 newHeight = mFixedHeight;
860 } else if (widthSpecMode == MeasureSpec.UNSPECIFIED || heightSpecMode == MeasureSpec.UNSPECIFIED) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800861 throw new RuntimeException("CellLayout cannot have UNSPECIFIED dimensions");
862 }
863
Adam Cohend22015c2010-07-26 22:02:18 -0700864 int numWidthGaps = mCountX - 1;
865 int numHeightGaps = mCountY - 1;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800866
Adam Cohen234c4cd2011-07-17 21:03:04 -0700867 if (mOriginalWidthGap < 0 || mOriginalHeightGap < 0) {
Winson Chung2d75f122013-09-23 16:53:31 -0700868 int hSpace = childWidthSize;
869 int vSpace = childHeightSize;
Adam Cohenf4bd5792012-04-27 11:35:29 -0700870 int hFreeSpace = hSpace - (mCountX * mCellWidth);
871 int vFreeSpace = vSpace - (mCountY * mCellHeight);
Winson Chung4b825dcd2011-06-19 12:41:22 -0700872 mWidthGap = Math.min(mMaxGap, numWidthGaps > 0 ? (hFreeSpace / numWidthGaps) : 0);
873 mHeightGap = Math.min(mMaxGap,numHeightGaps > 0 ? (vFreeSpace / numHeightGaps) : 0);
Winson Chung5f8afe62013-08-12 16:19:28 -0700874 mShortcutsAndWidgets.setCellDimensions(mCellWidth, mCellHeight, mWidthGap,
875 mHeightGap, mCountX, mCountY);
Adam Cohen234c4cd2011-07-17 21:03:04 -0700876 } else {
877 mWidthGap = mOriginalWidthGap;
878 mHeightGap = mOriginalHeightGap;
Winson Chungece7f5b2010-10-22 14:54:12 -0700879 }
Michael Jurka8c920dd2011-01-20 14:16:56 -0800880 int count = getChildCount();
Winson Chung5f8afe62013-08-12 16:19:28 -0700881 int maxWidth = 0;
882 int maxHeight = 0;
Michael Jurka8c920dd2011-01-20 14:16:56 -0800883 for (int i = 0; i < count; i++) {
884 View child = getChildAt(i);
Winson Chung2d75f122013-09-23 16:53:31 -0700885 int childWidthMeasureSpec = MeasureSpec.makeMeasureSpec(newWidth,
886 MeasureSpec.EXACTLY);
887 int childheightMeasureSpec = MeasureSpec.makeMeasureSpec(newHeight,
888 MeasureSpec.EXACTLY);
Michael Jurka8c920dd2011-01-20 14:16:56 -0800889 child.measure(childWidthMeasureSpec, childheightMeasureSpec);
Winson Chung5f8afe62013-08-12 16:19:28 -0700890 maxWidth = Math.max(maxWidth, child.getMeasuredWidth());
891 maxHeight = Math.max(maxHeight, child.getMeasuredHeight());
Michael Jurka8c920dd2011-01-20 14:16:56 -0800892 }
Winson Chung2d75f122013-09-23 16:53:31 -0700893 if (mFixedWidth > 0 && mFixedHeight > 0) {
894 setMeasuredDimension(maxWidth, maxHeight);
895 } else {
896 setMeasuredDimension(widthSize, heightSize);
897 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800898 }
899
900 @Override
Michael Jurka28750fb2010-09-24 17:43:49 -0700901 protected void onLayout(boolean changed, int l, int t, int r, int b) {
Winson Chung38848ca2013-10-08 12:03:44 -0700902 int offset = getMeasuredWidth() - getPaddingLeft() - getPaddingRight() -
903 (mCountX * mCellWidth);
904 int left = getPaddingLeft() + (int) Math.ceil(offset / 2f);
905 int top = getPaddingTop();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800906 int count = getChildCount();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800907 for (int i = 0; i < count; i++) {
Michael Jurka8c920dd2011-01-20 14:16:56 -0800908 View child = getChildAt(i);
Winson Chung2d75f122013-09-23 16:53:31 -0700909 child.layout(left, top,
910 left + r - l,
911 top + b - t);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800912 }
913 }
914
915 @Override
Michael Jurkadee05892010-07-27 10:01:56 -0700916 protected void onSizeChanged(int w, int h, int oldw, int oldh) {
917 super.onSizeChanged(w, h, oldw, oldh);
Winson Chung82a9bd22013-10-08 16:02:34 -0700918
919 // Expand the background drawing bounds by the padding baked into the background drawable
920 Rect padding = new Rect();
921 mNormalBackground.getPadding(padding);
922 mBackgroundRect.set(-padding.left, -padding.top, w + padding.right, h + padding.bottom);
923
Adam Cohenb5ba0972011-09-07 18:02:31 -0700924 mForegroundRect.set(mForegroundPadding, mForegroundPadding,
Adam Cohen215b4162012-08-30 13:14:08 -0700925 w - mForegroundPadding, h - mForegroundPadding);
Michael Jurkadee05892010-07-27 10:01:56 -0700926 }
927
928 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800929 protected void setChildrenDrawingCacheEnabled(boolean enabled) {
Michael Jurkaa52570f2012-03-20 03:18:20 -0700930 mShortcutsAndWidgets.setChildrenDrawingCacheEnabled(enabled);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800931 }
932
933 @Override
934 protected void setChildrenDrawnWithCacheEnabled(boolean enabled) {
Michael Jurkaa52570f2012-03-20 03:18:20 -0700935 mShortcutsAndWidgets.setChildrenDrawnWithCacheEnabled(enabled);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800936 }
937
Michael Jurka5f1c5092010-09-03 14:15:02 -0700938 public float getBackgroundAlpha() {
939 return mBackgroundAlpha;
Michael Jurkadee05892010-07-27 10:01:56 -0700940 }
941
Adam Cohen1b0aaac2010-10-28 11:11:18 -0700942 public void setBackgroundAlphaMultiplier(float multiplier) {
Adam Cohenc50438c2014-08-19 17:43:05 -0700943
Michael Jurkaa3d30ad2012-05-08 13:43:43 -0700944 if (mBackgroundAlphaMultiplier != multiplier) {
945 mBackgroundAlphaMultiplier = multiplier;
946 invalidate();
947 }
Adam Cohen1b0aaac2010-10-28 11:11:18 -0700948 }
949
Adam Cohenddb82192010-11-10 16:32:54 -0800950 public float getBackgroundAlphaMultiplier() {
951 return mBackgroundAlphaMultiplier;
952 }
953
Michael Jurka5f1c5092010-09-03 14:15:02 -0700954 public void setBackgroundAlpha(float alpha) {
Michael Jurkaafaa0502011-12-13 18:22:50 -0800955 if (mBackgroundAlpha != alpha) {
956 mBackgroundAlpha = alpha;
957 invalidate();
958 }
Michael Jurkadee05892010-07-27 10:01:56 -0700959 }
960
Michael Jurkaa52570f2012-03-20 03:18:20 -0700961 public void setShortcutAndWidgetAlpha(float alpha) {
Michael Jurka0142d492010-08-25 17:46:15 -0700962 final int childCount = getChildCount();
963 for (int i = 0; i < childCount; i++) {
Michael Jurkadee05892010-07-27 10:01:56 -0700964 getChildAt(i).setAlpha(alpha);
965 }
966 }
967
Michael Jurkaa52570f2012-03-20 03:18:20 -0700968 public ShortcutAndWidgetContainer getShortcutsAndWidgets() {
Sunny Goyaldcbcc862014-08-12 15:58:36 -0700969 return mShortcutsAndWidgets;
Michael Jurkaa52570f2012-03-20 03:18:20 -0700970 }
971
Patrick Dubroy440c3602010-07-13 17:50:32 -0700972 public View getChildAt(int x, int y) {
Michael Jurkaa52570f2012-03-20 03:18:20 -0700973 return mShortcutsAndWidgets.getChildAt(x, y);
Patrick Dubroy440c3602010-07-13 17:50:32 -0700974 }
975
Adam Cohen76fc0852011-06-17 13:26:23 -0700976 public boolean animateChildToPosition(final View child, int cellX, int cellY, int duration,
Adam Cohen482ed822012-03-02 14:15:13 -0800977 int delay, boolean permanent, boolean adjustOccupied) {
Michael Jurkaa52570f2012-03-20 03:18:20 -0700978 ShortcutAndWidgetContainer clc = getShortcutsAndWidgets();
Adam Cohen482ed822012-03-02 14:15:13 -0800979 boolean[][] occupied = mOccupied;
980 if (!permanent) {
981 occupied = mTmpOccupied;
982 }
983
Adam Cohen19f37922012-03-21 11:59:11 -0700984 if (clc.indexOfChild(child) != -1) {
Adam Cohenbfbfd262011-06-13 16:55:12 -0700985 final LayoutParams lp = (LayoutParams) child.getLayoutParams();
986 final ItemInfo info = (ItemInfo) child.getTag();
987
988 // We cancel any existing animations
989 if (mReorderAnimators.containsKey(lp)) {
990 mReorderAnimators.get(lp).cancel();
991 mReorderAnimators.remove(lp);
992 }
993
Adam Cohen482ed822012-03-02 14:15:13 -0800994 final int oldX = lp.x;
995 final int oldY = lp.y;
996 if (adjustOccupied) {
997 occupied[lp.cellX][lp.cellY] = false;
998 occupied[cellX][cellY] = true;
999 }
Adam Cohenbfbfd262011-06-13 16:55:12 -07001000 lp.isLockedToGrid = true;
Adam Cohen482ed822012-03-02 14:15:13 -08001001 if (permanent) {
1002 lp.cellX = info.cellX = cellX;
1003 lp.cellY = info.cellY = cellY;
1004 } else {
1005 lp.tmpCellX = cellX;
1006 lp.tmpCellY = cellY;
1007 }
Adam Cohenbfbfd262011-06-13 16:55:12 -07001008 clc.setupLp(lp);
1009 lp.isLockedToGrid = false;
Adam Cohen482ed822012-03-02 14:15:13 -08001010 final int newX = lp.x;
1011 final int newY = lp.y;
Adam Cohenbfbfd262011-06-13 16:55:12 -07001012
Adam Cohen76fc0852011-06-17 13:26:23 -07001013 lp.x = oldX;
1014 lp.y = oldY;
Adam Cohen76fc0852011-06-17 13:26:23 -07001015
Adam Cohen482ed822012-03-02 14:15:13 -08001016 // Exit early if we're not actually moving the view
1017 if (oldX == newX && oldY == newY) {
1018 lp.isLockedToGrid = true;
1019 return true;
1020 }
1021
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001022 ValueAnimator va = LauncherAnimUtils.ofFloat(child, 0f, 1f);
Adam Cohen482ed822012-03-02 14:15:13 -08001023 va.setDuration(duration);
1024 mReorderAnimators.put(lp, va);
1025
1026 va.addUpdateListener(new AnimatorUpdateListener() {
1027 @Override
Adam Cohenbfbfd262011-06-13 16:55:12 -07001028 public void onAnimationUpdate(ValueAnimator animation) {
Adam Cohen482ed822012-03-02 14:15:13 -08001029 float r = ((Float) animation.getAnimatedValue()).floatValue();
Adam Cohen19f37922012-03-21 11:59:11 -07001030 lp.x = (int) ((1 - r) * oldX + r * newX);
1031 lp.y = (int) ((1 - r) * oldY + r * newY);
Adam Cohen6b8a02d2012-03-22 15:13:40 -07001032 child.requestLayout();
Adam Cohenbfbfd262011-06-13 16:55:12 -07001033 }
1034 });
Adam Cohen482ed822012-03-02 14:15:13 -08001035 va.addListener(new AnimatorListenerAdapter() {
Adam Cohenbfbfd262011-06-13 16:55:12 -07001036 boolean cancelled = false;
1037 public void onAnimationEnd(Animator animation) {
1038 // If the animation was cancelled, it means that another animation
1039 // has interrupted this one, and we don't want to lock the item into
1040 // place just yet.
1041 if (!cancelled) {
1042 lp.isLockedToGrid = true;
Adam Cohen482ed822012-03-02 14:15:13 -08001043 child.requestLayout();
Adam Cohenbfbfd262011-06-13 16:55:12 -07001044 }
1045 if (mReorderAnimators.containsKey(lp)) {
1046 mReorderAnimators.remove(lp);
1047 }
1048 }
1049 public void onAnimationCancel(Animator animation) {
1050 cancelled = true;
1051 }
1052 });
Adam Cohen482ed822012-03-02 14:15:13 -08001053 va.setStartDelay(delay);
1054 va.start();
Adam Cohenbfbfd262011-06-13 16:55:12 -07001055 return true;
1056 }
1057 return false;
1058 }
1059
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001060 /**
1061 * Estimate where the top left cell of the dragged item will land if it is dropped.
1062 *
1063 * @param originX The X value of the top left corner of the item
1064 * @param originY The Y value of the top left corner of the item
1065 * @param spanX The number of horizontal cells that the item spans
1066 * @param spanY The number of vertical cells that the item spans
1067 * @param result The estimated drop cell X and Y.
1068 */
1069 void estimateDropCell(int originX, int originY, int spanX, int spanY, int[] result) {
Adam Cohend22015c2010-07-26 22:02:18 -07001070 final int countX = mCountX;
1071 final int countY = mCountY;
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001072
Michael Jurkaa63c4522010-08-19 13:52:27 -07001073 // pointToCellRounded takes the top left of a cell but will pad that with
1074 // cellWidth/2 and cellHeight/2 when finding the matching cell
1075 pointToCellRounded(originX, originY, result);
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001076
1077 // If the item isn't fully on this screen, snap to the edges
1078 int rightOverhang = result[0] + spanX - countX;
1079 if (rightOverhang > 0) {
1080 result[0] -= rightOverhang; // Snap to right
1081 }
1082 result[0] = Math.max(0, result[0]); // Snap to left
1083 int bottomOverhang = result[1] + spanY - countY;
1084 if (bottomOverhang > 0) {
1085 result[1] -= bottomOverhang; // Snap to bottom
1086 }
1087 result[1] = Math.max(0, result[1]); // Snap to top
1088 }
1089
Adam Cohen482ed822012-03-02 14:15:13 -08001090 void visualizeDropLocation(View v, Bitmap dragOutline, int originX, int originY, int cellX,
1091 int cellY, int spanX, int spanY, boolean resize, Point dragOffset, Rect dragRegion) {
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -07001092 final int oldDragCellX = mDragCell[0];
1093 final int oldDragCellY = mDragCell[1];
Adam Cohen482ed822012-03-02 14:15:13 -08001094
Adam Cohen2801caf2011-05-13 20:57:39 -07001095 if (dragOutline == null && v == null) {
Adam Cohen2801caf2011-05-13 20:57:39 -07001096 return;
1097 }
1098
Adam Cohen482ed822012-03-02 14:15:13 -08001099 if (cellX != oldDragCellX || cellY != oldDragCellY) {
1100 mDragCell[0] = cellX;
1101 mDragCell[1] = cellY;
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001102 // Find the top left corner of the rect the object will occupy
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001103 final int[] topLeft = mTmpPoint;
Adam Cohen482ed822012-03-02 14:15:13 -08001104 cellToPoint(cellX, cellY, topLeft);
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001105
Joe Onorato4be866d2010-10-10 11:26:02 -07001106 int left = topLeft[0];
1107 int top = topLeft[1];
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001108
Winson Chungb8c69f32011-10-19 21:36:08 -07001109 if (v != null && dragOffset == null) {
Adam Cohen99e8b402011-03-25 19:23:43 -07001110 // When drawing the drag outline, it did not account for margin offsets
1111 // added by the view's parent.
1112 MarginLayoutParams lp = (MarginLayoutParams) v.getLayoutParams();
1113 left += lp.leftMargin;
1114 top += lp.topMargin;
Winson Chung150fbab2010-09-29 17:14:26 -07001115
Adam Cohen99e8b402011-03-25 19:23:43 -07001116 // Offsets due to the size difference between the View and the dragOutline.
1117 // There is a size difference to account for the outer blur, which may lie
1118 // outside the bounds of the view.
Winson Chunga9abd0e2010-10-27 17:18:37 -07001119 top += (v.getHeight() - dragOutline.getHeight()) / 2;
Adam Cohenae915ce2011-08-25 13:47:22 -07001120 // We center about the x axis
1121 left += ((mCellWidth * spanX) + ((spanX - 1) * mWidthGap)
1122 - dragOutline.getWidth()) / 2;
Adam Cohen66396872011-04-15 17:50:36 -07001123 } else {
Winson Chungb8c69f32011-10-19 21:36:08 -07001124 if (dragOffset != null && dragRegion != null) {
1125 // Center the drag region *horizontally* in the cell and apply a drag
1126 // outline offset
1127 left += dragOffset.x + ((mCellWidth * spanX) + ((spanX - 1) * mWidthGap)
1128 - dragRegion.width()) / 2;
Winson Chung69737c32013-10-08 17:00:19 -07001129 int cHeight = getShortcutsAndWidgets().getCellContentHeight();
1130 int cellPaddingY = (int) Math.max(0, ((mCellHeight - cHeight) / 2f));
1131 top += dragOffset.y + cellPaddingY;
Winson Chungb8c69f32011-10-19 21:36:08 -07001132 } else {
1133 // Center the drag outline in the cell
1134 left += ((mCellWidth * spanX) + ((spanX - 1) * mWidthGap)
1135 - dragOutline.getWidth()) / 2;
1136 top += ((mCellHeight * spanY) + ((spanY - 1) * mHeightGap)
1137 - dragOutline.getHeight()) / 2;
1138 }
Winson Chunga9abd0e2010-10-27 17:18:37 -07001139 }
Joe Onorato4be866d2010-10-10 11:26:02 -07001140 final int oldIndex = mDragOutlineCurrent;
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -07001141 mDragOutlineAnims[oldIndex].animateOut();
1142 mDragOutlineCurrent = (oldIndex + 1) % mDragOutlines.length;
Adam Cohend41fbf52012-02-16 23:53:59 -08001143 Rect r = mDragOutlines[mDragOutlineCurrent];
1144 r.set(left, top, left + dragOutline.getWidth(), top + dragOutline.getHeight());
1145 if (resize) {
Adam Cohen482ed822012-03-02 14:15:13 -08001146 cellToRect(cellX, cellY, spanX, spanY, r);
Adam Cohend41fbf52012-02-16 23:53:59 -08001147 }
Winson Chung150fbab2010-09-29 17:14:26 -07001148
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -07001149 mDragOutlineAnims[mDragOutlineCurrent].setTag(dragOutline);
1150 mDragOutlineAnims[mDragOutlineCurrent].animateIn();
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001151 }
1152 }
1153
Adam Cohene0310962011-04-18 16:15:31 -07001154 public void clearDragOutlines() {
1155 final int oldIndex = mDragOutlineCurrent;
1156 mDragOutlineAnims[oldIndex].animateOut();
Adam Cohend41fbf52012-02-16 23:53:59 -08001157 mDragCell[0] = mDragCell[1] = -1;
Adam Cohene0310962011-04-18 16:15:31 -07001158 }
1159
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001160 /**
Jeff Sharkey70864282009-04-07 21:08:40 -07001161 * Find a vacant area that will fit the given bounds nearest the requested
1162 * cell location. Uses Euclidean distance to score multiple vacant areas.
Winson Chungaafa03c2010-06-11 17:34:16 -07001163 *
Romain Guy51afc022009-05-04 18:03:43 -07001164 * @param pixelX The X location at which you want to search for a vacant area.
1165 * @param pixelY The Y location at which you want to search for a vacant area.
Jeff Sharkey70864282009-04-07 21:08:40 -07001166 * @param spanX Horizontal span of the object.
1167 * @param spanY Vertical span of the object.
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001168 * @param result Array in which to place the result, or null (in which case a new array will
1169 * be allocated)
Jeff Sharkey70864282009-04-07 21:08:40 -07001170 * @return The X, Y cell of a vacant area that can contain this object,
1171 * nearest the requested location.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001172 */
Adam Cohend41fbf52012-02-16 23:53:59 -08001173 int[] findNearestVacantArea(int pixelX, int pixelY, int spanX, int spanY,
1174 int[] result) {
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001175 return findNearestVacantArea(pixelX, pixelY, spanX, spanY, null, result);
Michael Jurka6a1435d2010-09-27 17:35:12 -07001176 }
Winson Chungaafa03c2010-06-11 17:34:16 -07001177
Michael Jurka6a1435d2010-09-27 17:35:12 -07001178 /**
1179 * Find a vacant area that will fit the given bounds nearest the requested
1180 * cell location. Uses Euclidean distance to score multiple vacant areas.
1181 *
1182 * @param pixelX The X location at which you want to search for a vacant area.
1183 * @param pixelY The Y location at which you want to search for a vacant area.
Adam Cohend41fbf52012-02-16 23:53:59 -08001184 * @param minSpanX The minimum horizontal span required
1185 * @param minSpanY The minimum vertical span required
1186 * @param spanX Horizontal span of the object.
1187 * @param spanY Vertical span of the object.
1188 * @param result Array in which to place the result, or null (in which case a new array will
1189 * be allocated)
1190 * @return The X, Y cell of a vacant area that can contain this object,
1191 * nearest the requested location.
1192 */
1193 int[] findNearestVacantArea(int pixelX, int pixelY, int minSpanX, int minSpanY, int spanX,
1194 int spanY, int[] result, int[] resultSpan) {
1195 return findNearestVacantArea(pixelX, pixelY, minSpanX, minSpanY, spanX, spanY, null,
1196 result, resultSpan);
1197 }
1198
1199 /**
1200 * Find a vacant area that will fit the given bounds nearest the requested
1201 * cell location. Uses Euclidean distance to score multiple vacant areas.
1202 *
1203 * @param pixelX The X location at which you want to search for a vacant area.
1204 * @param pixelY The Y location at which you want to search for a vacant area.
Michael Jurka6a1435d2010-09-27 17:35:12 -07001205 * @param spanX Horizontal span of the object.
1206 * @param spanY Vertical span of the object.
Adam Cohendf035382011-04-11 17:22:04 -07001207 * @param ignoreOccupied If true, the result can be an occupied cell
1208 * @param result Array in which to place the result, or null (in which case a new array will
1209 * be allocated)
Michael Jurka6a1435d2010-09-27 17:35:12 -07001210 * @return The X, Y cell of a vacant area that can contain this object,
1211 * nearest the requested location.
1212 */
Adam Cohendf035382011-04-11 17:22:04 -07001213 int[] findNearestArea(int pixelX, int pixelY, int spanX, int spanY, View ignoreView,
1214 boolean ignoreOccupied, int[] result) {
Adam Cohend41fbf52012-02-16 23:53:59 -08001215 return findNearestArea(pixelX, pixelY, spanX, spanY,
Adam Cohen482ed822012-03-02 14:15:13 -08001216 spanX, spanY, ignoreView, ignoreOccupied, result, null, mOccupied);
Adam Cohend41fbf52012-02-16 23:53:59 -08001217 }
1218
1219 private final Stack<Rect> mTempRectStack = new Stack<Rect>();
1220 private void lazyInitTempRectStack() {
1221 if (mTempRectStack.isEmpty()) {
1222 for (int i = 0; i < mCountX * mCountY; i++) {
1223 mTempRectStack.push(new Rect());
1224 }
1225 }
1226 }
Adam Cohen482ed822012-03-02 14:15:13 -08001227
Adam Cohend41fbf52012-02-16 23:53:59 -08001228 private void recycleTempRects(Stack<Rect> used) {
1229 while (!used.isEmpty()) {
1230 mTempRectStack.push(used.pop());
1231 }
1232 }
1233
1234 /**
1235 * Find a vacant area that will fit the given bounds nearest the requested
1236 * cell location. Uses Euclidean distance to score multiple vacant areas.
1237 *
1238 * @param pixelX The X location at which you want to search for a vacant area.
1239 * @param pixelY The Y location at which you want to search for a vacant area.
1240 * @param minSpanX The minimum horizontal span required
1241 * @param minSpanY The minimum vertical span required
1242 * @param spanX Horizontal span of the object.
1243 * @param spanY Vertical span of the object.
1244 * @param ignoreOccupied If true, the result can be an occupied cell
1245 * @param result Array in which to place the result, or null (in which case a new array will
1246 * be allocated)
1247 * @return The X, Y cell of a vacant area that can contain this object,
1248 * nearest the requested location.
1249 */
1250 int[] findNearestArea(int pixelX, int pixelY, int minSpanX, int minSpanY, int spanX, int spanY,
Adam Cohen482ed822012-03-02 14:15:13 -08001251 View ignoreView, boolean ignoreOccupied, int[] result, int[] resultSpan,
1252 boolean[][] occupied) {
Adam Cohend41fbf52012-02-16 23:53:59 -08001253 lazyInitTempRectStack();
Michael Jurkac6ee42e2010-09-30 12:04:50 -07001254 // mark space take by ignoreView as available (method checks if ignoreView is null)
Adam Cohen482ed822012-03-02 14:15:13 -08001255 markCellsAsUnoccupiedForView(ignoreView, occupied);
Michael Jurkac6ee42e2010-09-30 12:04:50 -07001256
Adam Cohene3e27a82011-04-15 12:07:39 -07001257 // For items with a spanX / spanY > 1, the passed in point (pixelX, pixelY) corresponds
1258 // to the center of the item, but we are searching based on the top-left cell, so
1259 // we translate the point over to correspond to the top-left.
1260 pixelX -= (mCellWidth + mWidthGap) * (spanX - 1) / 2f;
1261 pixelY -= (mCellHeight + mHeightGap) * (spanY - 1) / 2f;
1262
Jeff Sharkey70864282009-04-07 21:08:40 -07001263 // Keep track of best-scoring drop area
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001264 final int[] bestXY = result != null ? result : new int[2];
Jeff Sharkey70864282009-04-07 21:08:40 -07001265 double bestDistance = Double.MAX_VALUE;
Adam Cohend41fbf52012-02-16 23:53:59 -08001266 final Rect bestRect = new Rect(-1, -1, -1, -1);
1267 final Stack<Rect> validRegions = new Stack<Rect>();
Winson Chungaafa03c2010-06-11 17:34:16 -07001268
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001269 final int countX = mCountX;
1270 final int countY = mCountY;
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001271
Adam Cohend41fbf52012-02-16 23:53:59 -08001272 if (minSpanX <= 0 || minSpanY <= 0 || spanX <= 0 || spanY <= 0 ||
1273 spanX < minSpanX || spanY < minSpanY) {
1274 return bestXY;
1275 }
1276
1277 for (int y = 0; y < countY - (minSpanY - 1); y++) {
Michael Jurkac28de512010-08-13 11:27:44 -07001278 inner:
Adam Cohend41fbf52012-02-16 23:53:59 -08001279 for (int x = 0; x < countX - (minSpanX - 1); x++) {
1280 int ySize = -1;
1281 int xSize = -1;
Adam Cohendf035382011-04-11 17:22:04 -07001282 if (ignoreOccupied) {
Adam Cohend41fbf52012-02-16 23:53:59 -08001283 // First, let's see if this thing fits anywhere
1284 for (int i = 0; i < minSpanX; i++) {
1285 for (int j = 0; j < minSpanY; j++) {
Adam Cohendf035382011-04-11 17:22:04 -07001286 if (occupied[x + i][y + j]) {
Adam Cohendf035382011-04-11 17:22:04 -07001287 continue inner;
1288 }
Michael Jurkac28de512010-08-13 11:27:44 -07001289 }
1290 }
Adam Cohend41fbf52012-02-16 23:53:59 -08001291 xSize = minSpanX;
1292 ySize = minSpanY;
1293
1294 // We know that the item will fit at _some_ acceptable size, now let's see
1295 // how big we can make it. We'll alternate between incrementing x and y spans
1296 // until we hit a limit.
1297 boolean incX = true;
1298 boolean hitMaxX = xSize >= spanX;
1299 boolean hitMaxY = ySize >= spanY;
1300 while (!(hitMaxX && hitMaxY)) {
1301 if (incX && !hitMaxX) {
1302 for (int j = 0; j < ySize; j++) {
1303 if (x + xSize > countX -1 || occupied[x + xSize][y + j]) {
1304 // We can't move out horizontally
1305 hitMaxX = true;
1306 }
1307 }
1308 if (!hitMaxX) {
1309 xSize++;
1310 }
1311 } else if (!hitMaxY) {
1312 for (int i = 0; i < xSize; i++) {
1313 if (y + ySize > countY - 1 || occupied[x + i][y + ySize]) {
1314 // We can't move out vertically
1315 hitMaxY = true;
1316 }
1317 }
1318 if (!hitMaxY) {
1319 ySize++;
1320 }
1321 }
1322 hitMaxX |= xSize >= spanX;
1323 hitMaxY |= ySize >= spanY;
1324 incX = !incX;
1325 }
1326 incX = true;
1327 hitMaxX = xSize >= spanX;
1328 hitMaxY = ySize >= spanY;
Michael Jurkac28de512010-08-13 11:27:44 -07001329 }
Winson Chung0be025d2011-05-23 17:45:09 -07001330 final int[] cellXY = mTmpXY;
Adam Cohene3e27a82011-04-15 12:07:39 -07001331 cellToCenterPoint(x, y, cellXY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001332
Adam Cohend41fbf52012-02-16 23:53:59 -08001333 // We verify that the current rect is not a sub-rect of any of our previous
1334 // candidates. In this case, the current rect is disqualified in favour of the
1335 // containing rect.
1336 Rect currentRect = mTempRectStack.pop();
1337 currentRect.set(x, y, x + xSize, y + ySize);
1338 boolean contained = false;
1339 for (Rect r : validRegions) {
1340 if (r.contains(currentRect)) {
1341 contained = true;
1342 break;
1343 }
1344 }
1345 validRegions.push(currentRect);
Michael Jurkac28de512010-08-13 11:27:44 -07001346 double distance = Math.sqrt(Math.pow(cellXY[0] - pixelX, 2)
1347 + Math.pow(cellXY[1] - pixelY, 2));
Adam Cohen482ed822012-03-02 14:15:13 -08001348
Adam Cohend41fbf52012-02-16 23:53:59 -08001349 if ((distance <= bestDistance && !contained) ||
1350 currentRect.contains(bestRect)) {
Michael Jurkac28de512010-08-13 11:27:44 -07001351 bestDistance = distance;
1352 bestXY[0] = x;
1353 bestXY[1] = y;
Adam Cohend41fbf52012-02-16 23:53:59 -08001354 if (resultSpan != null) {
1355 resultSpan[0] = xSize;
1356 resultSpan[1] = ySize;
1357 }
1358 bestRect.set(currentRect);
Michael Jurkac28de512010-08-13 11:27:44 -07001359 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001360 }
1361 }
Michael Jurkac6ee42e2010-09-30 12:04:50 -07001362 // re-mark space taken by ignoreView as occupied
Adam Cohen482ed822012-03-02 14:15:13 -08001363 markCellsAsOccupiedForView(ignoreView, occupied);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001364
Adam Cohenc0dcf592011-06-01 15:30:43 -07001365 // Return -1, -1 if no suitable location found
1366 if (bestDistance == Double.MAX_VALUE) {
1367 bestXY[0] = -1;
1368 bestXY[1] = -1;
Jeff Sharkey70864282009-04-07 21:08:40 -07001369 }
Adam Cohend41fbf52012-02-16 23:53:59 -08001370 recycleTempRects(validRegions);
Adam Cohenc0dcf592011-06-01 15:30:43 -07001371 return bestXY;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001372 }
Winson Chungaafa03c2010-06-11 17:34:16 -07001373
Adam Cohen482ed822012-03-02 14:15:13 -08001374 /**
1375 * Find a vacant area that will fit the given bounds nearest the requested
1376 * cell location, and will also weigh in a suggested direction vector of the
1377 * desired location. This method computers distance based on unit grid distances,
1378 * not pixel distances.
1379 *
Adam Cohen47a876d2012-03-19 13:21:41 -07001380 * @param cellX The X cell nearest to which you want to search for a vacant area.
1381 * @param cellY The Y cell nearest which you want to search for a vacant area.
Adam Cohen482ed822012-03-02 14:15:13 -08001382 * @param spanX Horizontal span of the object.
1383 * @param spanY Vertical span of the object.
Adam Cohen47a876d2012-03-19 13:21:41 -07001384 * @param direction The favored direction in which the views should move from x, y
1385 * @param exactDirectionOnly If this parameter is true, then only solutions where the direction
1386 * matches exactly. Otherwise we find the best matching direction.
1387 * @param occoupied The array which represents which cells in the CellLayout are occupied
1388 * @param blockOccupied The array which represents which cells in the specified block (cellX,
Winson Chung5f8afe62013-08-12 16:19:28 -07001389 * cellY, spanX, spanY) are occupied. This is used when try to move a group of views.
Adam Cohen482ed822012-03-02 14:15:13 -08001390 * @param result Array in which to place the result, or null (in which case a new array will
1391 * be allocated)
1392 * @return The X, Y cell of a vacant area that can contain this object,
1393 * nearest the requested location.
1394 */
1395 private int[] findNearestArea(int cellX, int cellY, int spanX, int spanY, int[] direction,
Adam Cohen47a876d2012-03-19 13:21:41 -07001396 boolean[][] occupied, boolean blockOccupied[][], int[] result) {
Adam Cohen482ed822012-03-02 14:15:13 -08001397 // Keep track of best-scoring drop area
1398 final int[] bestXY = result != null ? result : new int[2];
1399 float bestDistance = Float.MAX_VALUE;
1400 int bestDirectionScore = Integer.MIN_VALUE;
1401
1402 final int countX = mCountX;
1403 final int countY = mCountY;
1404
1405 for (int y = 0; y < countY - (spanY - 1); y++) {
1406 inner:
1407 for (int x = 0; x < countX - (spanX - 1); x++) {
1408 // First, let's see if this thing fits anywhere
1409 for (int i = 0; i < spanX; i++) {
1410 for (int j = 0; j < spanY; j++) {
Adam Cohen47a876d2012-03-19 13:21:41 -07001411 if (occupied[x + i][y + j] && (blockOccupied == null || blockOccupied[i][j])) {
Adam Cohen482ed822012-03-02 14:15:13 -08001412 continue inner;
1413 }
1414 }
1415 }
1416
1417 float distance = (float)
1418 Math.sqrt((x - cellX) * (x - cellX) + (y - cellY) * (y - cellY));
1419 int[] curDirection = mTmpPoint;
Adam Cohen47a876d2012-03-19 13:21:41 -07001420 computeDirectionVector(x - cellX, y - cellY, curDirection);
1421 // The direction score is just the dot product of the two candidate direction
1422 // and that passed in.
Adam Cohen482ed822012-03-02 14:15:13 -08001423 int curDirectionScore = direction[0] * curDirection[0] +
1424 direction[1] * curDirection[1];
Adam Cohen47a876d2012-03-19 13:21:41 -07001425 boolean exactDirectionOnly = false;
1426 boolean directionMatches = direction[0] == curDirection[0] &&
1427 direction[0] == curDirection[0];
1428 if ((directionMatches || !exactDirectionOnly) &&
1429 Float.compare(distance, bestDistance) < 0 || (Float.compare(distance,
Adam Cohen482ed822012-03-02 14:15:13 -08001430 bestDistance) == 0 && curDirectionScore > bestDirectionScore)) {
1431 bestDistance = distance;
1432 bestDirectionScore = curDirectionScore;
1433 bestXY[0] = x;
1434 bestXY[1] = y;
1435 }
1436 }
1437 }
1438
1439 // Return -1, -1 if no suitable location found
1440 if (bestDistance == Float.MAX_VALUE) {
1441 bestXY[0] = -1;
1442 bestXY[1] = -1;
1443 }
1444 return bestXY;
1445 }
1446
1447 private boolean addViewToTempLocation(View v, Rect rectOccupiedByPotentialDrop,
Adam Cohen8baab352012-03-20 17:39:21 -07001448 int[] direction, ItemConfiguration currentState) {
1449 CellAndSpan c = currentState.map.get(v);
Adam Cohen482ed822012-03-02 14:15:13 -08001450 boolean success = false;
Adam Cohen8baab352012-03-20 17:39:21 -07001451 markCellsForView(c.x, c.y, c.spanX, c.spanY, mTmpOccupied, false);
Adam Cohen482ed822012-03-02 14:15:13 -08001452 markCellsForRect(rectOccupiedByPotentialDrop, mTmpOccupied, true);
1453
Adam Cohen8baab352012-03-20 17:39:21 -07001454 findNearestArea(c.x, c.y, c.spanX, c.spanY, direction, mTmpOccupied, null, mTempLocation);
Adam Cohen482ed822012-03-02 14:15:13 -08001455
1456 if (mTempLocation[0] >= 0 && mTempLocation[1] >= 0) {
Adam Cohen8baab352012-03-20 17:39:21 -07001457 c.x = mTempLocation[0];
1458 c.y = mTempLocation[1];
Adam Cohen482ed822012-03-02 14:15:13 -08001459 success = true;
Adam Cohen482ed822012-03-02 14:15:13 -08001460 }
Adam Cohen8baab352012-03-20 17:39:21 -07001461 markCellsForView(c.x, c.y, c.spanX, c.spanY, mTmpOccupied, true);
Adam Cohen482ed822012-03-02 14:15:13 -08001462 return success;
1463 }
1464
Adam Cohenf3900c22012-11-16 18:28:11 -08001465 /**
1466 * This helper class defines a cluster of views. It helps with defining complex edges
1467 * of the cluster and determining how those edges interact with other views. The edges
1468 * essentially define a fine-grained boundary around the cluster of views -- like a more
1469 * precise version of a bounding box.
1470 */
1471 private class ViewCluster {
1472 final static int LEFT = 0;
1473 final static int TOP = 1;
1474 final static int RIGHT = 2;
1475 final static int BOTTOM = 3;
Adam Cohen47a876d2012-03-19 13:21:41 -07001476
Adam Cohenf3900c22012-11-16 18:28:11 -08001477 ArrayList<View> views;
1478 ItemConfiguration config;
1479 Rect boundingRect = new Rect();
Adam Cohen47a876d2012-03-19 13:21:41 -07001480
Adam Cohenf3900c22012-11-16 18:28:11 -08001481 int[] leftEdge = new int[mCountY];
1482 int[] rightEdge = new int[mCountY];
1483 int[] topEdge = new int[mCountX];
1484 int[] bottomEdge = new int[mCountX];
1485 boolean leftEdgeDirty, rightEdgeDirty, topEdgeDirty, bottomEdgeDirty, boundingRectDirty;
1486
1487 @SuppressWarnings("unchecked")
1488 public ViewCluster(ArrayList<View> views, ItemConfiguration config) {
1489 this.views = (ArrayList<View>) views.clone();
1490 this.config = config;
1491 resetEdges();
Adam Cohen47a876d2012-03-19 13:21:41 -07001492 }
1493
Adam Cohenf3900c22012-11-16 18:28:11 -08001494 void resetEdges() {
1495 for (int i = 0; i < mCountX; i++) {
1496 topEdge[i] = -1;
1497 bottomEdge[i] = -1;
1498 }
1499 for (int i = 0; i < mCountY; i++) {
1500 leftEdge[i] = -1;
1501 rightEdge[i] = -1;
1502 }
1503 leftEdgeDirty = true;
1504 rightEdgeDirty = true;
1505 bottomEdgeDirty = true;
1506 topEdgeDirty = true;
1507 boundingRectDirty = true;
1508 }
Adam Cohen47a876d2012-03-19 13:21:41 -07001509
Adam Cohenf3900c22012-11-16 18:28:11 -08001510 void computeEdge(int which, int[] edge) {
1511 int count = views.size();
1512 for (int i = 0; i < count; i++) {
1513 CellAndSpan cs = config.map.get(views.get(i));
1514 switch (which) {
1515 case LEFT:
1516 int left = cs.x;
1517 for (int j = cs.y; j < cs.y + cs.spanY; j++) {
1518 if (left < edge[j] || edge[j] < 0) {
1519 edge[j] = left;
Adam Cohena56dc102012-07-13 13:41:42 -07001520 }
Adam Cohen47a876d2012-03-19 13:21:41 -07001521 }
Adam Cohenf3900c22012-11-16 18:28:11 -08001522 break;
1523 case RIGHT:
1524 int right = cs.x + cs.spanX;
1525 for (int j = cs.y; j < cs.y + cs.spanY; j++) {
1526 if (right > edge[j]) {
1527 edge[j] = right;
1528 }
1529 }
1530 break;
1531 case TOP:
1532 int top = cs.y;
1533 for (int j = cs.x; j < cs.x + cs.spanX; j++) {
1534 if (top < edge[j] || edge[j] < 0) {
1535 edge[j] = top;
1536 }
1537 }
1538 break;
1539 case BOTTOM:
1540 int bottom = cs.y + cs.spanY;
1541 for (int j = cs.x; j < cs.x + cs.spanX; j++) {
1542 if (bottom > edge[j]) {
1543 edge[j] = bottom;
1544 }
1545 }
1546 break;
Adam Cohen47a876d2012-03-19 13:21:41 -07001547 }
1548 }
1549 }
Adam Cohenf3900c22012-11-16 18:28:11 -08001550
1551 boolean isViewTouchingEdge(View v, int whichEdge) {
1552 CellAndSpan cs = config.map.get(v);
1553
1554 int[] edge = getEdge(whichEdge);
1555
1556 switch (whichEdge) {
1557 case LEFT:
1558 for (int i = cs.y; i < cs.y + cs.spanY; i++) {
1559 if (edge[i] == cs.x + cs.spanX) {
1560 return true;
1561 }
1562 }
1563 break;
1564 case RIGHT:
1565 for (int i = cs.y; i < cs.y + cs.spanY; i++) {
1566 if (edge[i] == cs.x) {
1567 return true;
1568 }
1569 }
1570 break;
1571 case TOP:
1572 for (int i = cs.x; i < cs.x + cs.spanX; i++) {
1573 if (edge[i] == cs.y + cs.spanY) {
1574 return true;
1575 }
1576 }
1577 break;
1578 case BOTTOM:
1579 for (int i = cs.x; i < cs.x + cs.spanX; i++) {
1580 if (edge[i] == cs.y) {
1581 return true;
1582 }
1583 }
1584 break;
1585 }
1586 return false;
1587 }
1588
1589 void shift(int whichEdge, int delta) {
1590 for (View v: views) {
1591 CellAndSpan c = config.map.get(v);
1592 switch (whichEdge) {
1593 case LEFT:
1594 c.x -= delta;
1595 break;
1596 case RIGHT:
1597 c.x += delta;
1598 break;
1599 case TOP:
1600 c.y -= delta;
1601 break;
1602 case BOTTOM:
1603 default:
1604 c.y += delta;
1605 break;
1606 }
1607 }
1608 resetEdges();
1609 }
1610
1611 public void addView(View v) {
1612 views.add(v);
1613 resetEdges();
1614 }
1615
1616 public Rect getBoundingRect() {
1617 if (boundingRectDirty) {
1618 boolean first = true;
1619 for (View v: views) {
1620 CellAndSpan c = config.map.get(v);
1621 if (first) {
1622 boundingRect.set(c.x, c.y, c.x + c.spanX, c.y + c.spanY);
1623 first = false;
1624 } else {
1625 boundingRect.union(c.x, c.y, c.x + c.spanX, c.y + c.spanY);
1626 }
1627 }
1628 }
1629 return boundingRect;
1630 }
1631
1632 public int[] getEdge(int which) {
1633 switch (which) {
1634 case LEFT:
1635 return getLeftEdge();
1636 case RIGHT:
1637 return getRightEdge();
1638 case TOP:
1639 return getTopEdge();
1640 case BOTTOM:
1641 default:
1642 return getBottomEdge();
1643 }
1644 }
1645
1646 public int[] getLeftEdge() {
1647 if (leftEdgeDirty) {
1648 computeEdge(LEFT, leftEdge);
1649 }
1650 return leftEdge;
1651 }
1652
1653 public int[] getRightEdge() {
1654 if (rightEdgeDirty) {
1655 computeEdge(RIGHT, rightEdge);
1656 }
1657 return rightEdge;
1658 }
1659
1660 public int[] getTopEdge() {
1661 if (topEdgeDirty) {
1662 computeEdge(TOP, topEdge);
1663 }
1664 return topEdge;
1665 }
1666
1667 public int[] getBottomEdge() {
1668 if (bottomEdgeDirty) {
1669 computeEdge(BOTTOM, bottomEdge);
1670 }
1671 return bottomEdge;
1672 }
1673
1674 PositionComparator comparator = new PositionComparator();
1675 class PositionComparator implements Comparator<View> {
1676 int whichEdge = 0;
1677 public int compare(View left, View right) {
1678 CellAndSpan l = config.map.get(left);
1679 CellAndSpan r = config.map.get(right);
1680 switch (whichEdge) {
1681 case LEFT:
1682 return (r.x + r.spanX) - (l.x + l.spanX);
1683 case RIGHT:
1684 return l.x - r.x;
1685 case TOP:
1686 return (r.y + r.spanY) - (l.y + l.spanY);
1687 case BOTTOM:
1688 default:
1689 return l.y - r.y;
1690 }
1691 }
1692 }
1693
1694 public void sortConfigurationForEdgePush(int edge) {
1695 comparator.whichEdge = edge;
1696 Collections.sort(config.sortedViews, comparator);
1697 }
Adam Cohen47a876d2012-03-19 13:21:41 -07001698 }
1699
Adam Cohenf3900c22012-11-16 18:28:11 -08001700 private boolean pushViewsToTempLocation(ArrayList<View> views, Rect rectOccupiedByPotentialDrop,
1701 int[] direction, View dragView, ItemConfiguration currentState) {
Adam Cohene0489502012-08-27 15:18:53 -07001702
Adam Cohenf3900c22012-11-16 18:28:11 -08001703 ViewCluster cluster = new ViewCluster(views, currentState);
1704 Rect clusterRect = cluster.getBoundingRect();
1705 int whichEdge;
1706 int pushDistance;
1707 boolean fail = false;
1708
1709 // Determine the edge of the cluster that will be leading the push and how far
1710 // the cluster must be shifted.
1711 if (direction[0] < 0) {
1712 whichEdge = ViewCluster.LEFT;
1713 pushDistance = clusterRect.right - rectOccupiedByPotentialDrop.left;
Adam Cohene0489502012-08-27 15:18:53 -07001714 } else if (direction[0] > 0) {
Adam Cohenf3900c22012-11-16 18:28:11 -08001715 whichEdge = ViewCluster.RIGHT;
1716 pushDistance = rectOccupiedByPotentialDrop.right - clusterRect.left;
1717 } else if (direction[1] < 0) {
1718 whichEdge = ViewCluster.TOP;
1719 pushDistance = clusterRect.bottom - rectOccupiedByPotentialDrop.top;
1720 } else {
1721 whichEdge = ViewCluster.BOTTOM;
1722 pushDistance = rectOccupiedByPotentialDrop.bottom - clusterRect.top;
Adam Cohene0489502012-08-27 15:18:53 -07001723 }
1724
Adam Cohenf3900c22012-11-16 18:28:11 -08001725 // Break early for invalid push distance.
1726 if (pushDistance <= 0) {
1727 return false;
Adam Cohene0489502012-08-27 15:18:53 -07001728 }
Adam Cohenf3900c22012-11-16 18:28:11 -08001729
1730 // Mark the occupied state as false for the group of views we want to move.
1731 for (View v: views) {
1732 CellAndSpan c = currentState.map.get(v);
1733 markCellsForView(c.x, c.y, c.spanX, c.spanY, mTmpOccupied, false);
1734 }
1735
1736 // We save the current configuration -- if we fail to find a solution we will revert
1737 // to the initial state. The process of finding a solution modifies the configuration
1738 // in place, hence the need for revert in the failure case.
1739 currentState.save();
1740
1741 // The pushing algorithm is simplified by considering the views in the order in which
1742 // they would be pushed by the cluster. For example, if the cluster is leading with its
1743 // left edge, we consider sort the views by their right edge, from right to left.
1744 cluster.sortConfigurationForEdgePush(whichEdge);
1745
1746 while (pushDistance > 0 && !fail) {
1747 for (View v: currentState.sortedViews) {
1748 // For each view that isn't in the cluster, we see if the leading edge of the
1749 // cluster is contacting the edge of that view. If so, we add that view to the
1750 // cluster.
1751 if (!cluster.views.contains(v) && v != dragView) {
1752 if (cluster.isViewTouchingEdge(v, whichEdge)) {
1753 LayoutParams lp = (LayoutParams) v.getLayoutParams();
1754 if (!lp.canReorder) {
1755 // The push solution includes the all apps button, this is not viable.
1756 fail = true;
1757 break;
1758 }
1759 cluster.addView(v);
1760 CellAndSpan c = currentState.map.get(v);
1761
1762 // Adding view to cluster, mark it as not occupied.
1763 markCellsForView(c.x, c.y, c.spanX, c.spanY, mTmpOccupied, false);
1764 }
1765 }
1766 }
1767 pushDistance--;
1768
1769 // The cluster has been completed, now we move the whole thing over in the appropriate
1770 // direction.
1771 cluster.shift(whichEdge, 1);
1772 }
1773
1774 boolean foundSolution = false;
1775 clusterRect = cluster.getBoundingRect();
1776
1777 // Due to the nature of the algorithm, the only check required to verify a valid solution
1778 // is to ensure that completed shifted cluster lies completely within the cell layout.
1779 if (!fail && clusterRect.left >= 0 && clusterRect.right <= mCountX && clusterRect.top >= 0 &&
1780 clusterRect.bottom <= mCountY) {
1781 foundSolution = true;
1782 } else {
1783 currentState.restore();
1784 }
1785
1786 // In either case, we set the occupied array as marked for the location of the views
1787 for (View v: cluster.views) {
1788 CellAndSpan c = currentState.map.get(v);
1789 markCellsForView(c.x, c.y, c.spanX, c.spanY, mTmpOccupied, true);
1790 }
1791
1792 return foundSolution;
Adam Cohene0489502012-08-27 15:18:53 -07001793 }
1794
Adam Cohen482ed822012-03-02 14:15:13 -08001795 private boolean addViewsToTempLocation(ArrayList<View> views, Rect rectOccupiedByPotentialDrop,
Adam Cohenf3900c22012-11-16 18:28:11 -08001796 int[] direction, View dragView, ItemConfiguration currentState) {
Adam Cohen482ed822012-03-02 14:15:13 -08001797 if (views.size() == 0) return true;
Adam Cohen482ed822012-03-02 14:15:13 -08001798
Adam Cohen8baab352012-03-20 17:39:21 -07001799 boolean success = false;
Adam Cohen482ed822012-03-02 14:15:13 -08001800 Rect boundingRect = null;
Adam Cohen8baab352012-03-20 17:39:21 -07001801 // We construct a rect which represents the entire group of views passed in
Adam Cohen482ed822012-03-02 14:15:13 -08001802 for (View v: views) {
Adam Cohen8baab352012-03-20 17:39:21 -07001803 CellAndSpan c = currentState.map.get(v);
Adam Cohen482ed822012-03-02 14:15:13 -08001804 if (boundingRect == null) {
Adam Cohen8baab352012-03-20 17:39:21 -07001805 boundingRect = new Rect(c.x, c.y, c.x + c.spanX, c.y + c.spanY);
Adam Cohen482ed822012-03-02 14:15:13 -08001806 } else {
Adam Cohen8baab352012-03-20 17:39:21 -07001807 boundingRect.union(c.x, c.y, c.x + c.spanX, c.y + c.spanY);
Adam Cohen482ed822012-03-02 14:15:13 -08001808 }
1809 }
Adam Cohen8baab352012-03-20 17:39:21 -07001810
Adam Cohen8baab352012-03-20 17:39:21 -07001811 // Mark the occupied state as false for the group of views we want to move.
Adam Cohenf3900c22012-11-16 18:28:11 -08001812 for (View v: views) {
Adam Cohen8baab352012-03-20 17:39:21 -07001813 CellAndSpan c = currentState.map.get(v);
1814 markCellsForView(c.x, c.y, c.spanX, c.spanY, mTmpOccupied, false);
1815 }
1816
Adam Cohen47a876d2012-03-19 13:21:41 -07001817 boolean[][] blockOccupied = new boolean[boundingRect.width()][boundingRect.height()];
1818 int top = boundingRect.top;
1819 int left = boundingRect.left;
Adam Cohen8baab352012-03-20 17:39:21 -07001820 // We mark more precisely which parts of the bounding rect are truly occupied, allowing
Adam Cohena56dc102012-07-13 13:41:42 -07001821 // for interlocking.
Adam Cohenf3900c22012-11-16 18:28:11 -08001822 for (View v: views) {
Adam Cohen8baab352012-03-20 17:39:21 -07001823 CellAndSpan c = currentState.map.get(v);
1824 markCellsForView(c.x - left, c.y - top, c.spanX, c.spanY, blockOccupied, true);
Adam Cohen47a876d2012-03-19 13:21:41 -07001825 }
1826
Adam Cohen482ed822012-03-02 14:15:13 -08001827 markCellsForRect(rectOccupiedByPotentialDrop, mTmpOccupied, true);
1828
Adam Cohenf3900c22012-11-16 18:28:11 -08001829 findNearestArea(boundingRect.left, boundingRect.top, boundingRect.width(),
1830 boundingRect.height(), direction, mTmpOccupied, blockOccupied, mTempLocation);
Adam Cohen482ed822012-03-02 14:15:13 -08001831
Adam Cohen8baab352012-03-20 17:39:21 -07001832 // If we successfuly found a location by pushing the block of views, we commit it
Adam Cohen482ed822012-03-02 14:15:13 -08001833 if (mTempLocation[0] >= 0 && mTempLocation[1] >= 0) {
Adam Cohen8baab352012-03-20 17:39:21 -07001834 int deltaX = mTempLocation[0] - boundingRect.left;
1835 int deltaY = mTempLocation[1] - boundingRect.top;
Adam Cohenf3900c22012-11-16 18:28:11 -08001836 for (View v: views) {
Adam Cohen8baab352012-03-20 17:39:21 -07001837 CellAndSpan c = currentState.map.get(v);
1838 c.x += deltaX;
1839 c.y += deltaY;
Adam Cohen482ed822012-03-02 14:15:13 -08001840 }
1841 success = true;
1842 }
Adam Cohen8baab352012-03-20 17:39:21 -07001843
1844 // In either case, we set the occupied array as marked for the location of the views
Adam Cohenf3900c22012-11-16 18:28:11 -08001845 for (View v: views) {
Adam Cohen8baab352012-03-20 17:39:21 -07001846 CellAndSpan c = currentState.map.get(v);
1847 markCellsForView(c.x, c.y, c.spanX, c.spanY, mTmpOccupied, true);
Adam Cohen482ed822012-03-02 14:15:13 -08001848 }
1849 return success;
1850 }
1851
1852 private void markCellsForRect(Rect r, boolean[][] occupied, boolean value) {
1853 markCellsForView(r.left, r.top, r.width(), r.height(), occupied, value);
1854 }
1855
Adam Cohen4abc5bd2012-05-29 21:06:03 -07001856 // This method tries to find a reordering solution which satisfies the push mechanic by trying
1857 // to push items in each of the cardinal directions, in an order based on the direction vector
1858 // passed.
1859 private boolean attemptPushInDirection(ArrayList<View> intersectingViews, Rect occupied,
1860 int[] direction, View ignoreView, ItemConfiguration solution) {
1861 if ((Math.abs(direction[0]) + Math.abs(direction[1])) > 1) {
Winson Chung5f8afe62013-08-12 16:19:28 -07001862 // If the direction vector has two non-zero components, we try pushing
Adam Cohen4abc5bd2012-05-29 21:06:03 -07001863 // separately in each of the components.
1864 int temp = direction[1];
1865 direction[1] = 0;
Adam Cohenf3900c22012-11-16 18:28:11 -08001866
1867 if (pushViewsToTempLocation(intersectingViews, occupied, direction,
Adam Cohen4abc5bd2012-05-29 21:06:03 -07001868 ignoreView, solution)) {
1869 return true;
1870 }
1871 direction[1] = temp;
1872 temp = direction[0];
1873 direction[0] = 0;
Adam Cohenf3900c22012-11-16 18:28:11 -08001874
1875 if (pushViewsToTempLocation(intersectingViews, occupied, direction,
Adam Cohen4abc5bd2012-05-29 21:06:03 -07001876 ignoreView, solution)) {
1877 return true;
1878 }
1879 // Revert the direction
1880 direction[0] = temp;
1881
1882 // Now we try pushing in each component of the opposite direction
1883 direction[0] *= -1;
1884 direction[1] *= -1;
1885 temp = direction[1];
1886 direction[1] = 0;
Adam Cohenf3900c22012-11-16 18:28:11 -08001887 if (pushViewsToTempLocation(intersectingViews, occupied, direction,
Adam Cohen4abc5bd2012-05-29 21:06:03 -07001888 ignoreView, solution)) {
1889 return true;
1890 }
1891
1892 direction[1] = temp;
1893 temp = direction[0];
1894 direction[0] = 0;
Adam Cohenf3900c22012-11-16 18:28:11 -08001895 if (pushViewsToTempLocation(intersectingViews, occupied, direction,
Adam Cohen4abc5bd2012-05-29 21:06:03 -07001896 ignoreView, solution)) {
1897 return true;
1898 }
1899 // revert the direction
1900 direction[0] = temp;
1901 direction[0] *= -1;
1902 direction[1] *= -1;
Winson Chung5f8afe62013-08-12 16:19:28 -07001903
Adam Cohen4abc5bd2012-05-29 21:06:03 -07001904 } else {
1905 // If the direction vector has a single non-zero component, we push first in the
1906 // direction of the vector
Adam Cohenf3900c22012-11-16 18:28:11 -08001907 if (pushViewsToTempLocation(intersectingViews, occupied, direction,
Adam Cohen4abc5bd2012-05-29 21:06:03 -07001908 ignoreView, solution)) {
1909 return true;
1910 }
Adam Cohen4abc5bd2012-05-29 21:06:03 -07001911 // Then we try the opposite direction
1912 direction[0] *= -1;
1913 direction[1] *= -1;
Adam Cohenf3900c22012-11-16 18:28:11 -08001914 if (pushViewsToTempLocation(intersectingViews, occupied, direction,
Adam Cohen4abc5bd2012-05-29 21:06:03 -07001915 ignoreView, solution)) {
1916 return true;
1917 }
1918 // Switch the direction back
1919 direction[0] *= -1;
1920 direction[1] *= -1;
Winson Chung5f8afe62013-08-12 16:19:28 -07001921
1922 // If we have failed to find a push solution with the above, then we try
Adam Cohen4abc5bd2012-05-29 21:06:03 -07001923 // to find a solution by pushing along the perpendicular axis.
1924
1925 // Swap the components
1926 int temp = direction[1];
1927 direction[1] = direction[0];
1928 direction[0] = temp;
Adam Cohenf3900c22012-11-16 18:28:11 -08001929 if (pushViewsToTempLocation(intersectingViews, occupied, direction,
Adam Cohen4abc5bd2012-05-29 21:06:03 -07001930 ignoreView, solution)) {
1931 return true;
1932 }
1933
1934 // Then we try the opposite direction
1935 direction[0] *= -1;
1936 direction[1] *= -1;
Adam Cohenf3900c22012-11-16 18:28:11 -08001937 if (pushViewsToTempLocation(intersectingViews, occupied, direction,
Adam Cohen4abc5bd2012-05-29 21:06:03 -07001938 ignoreView, solution)) {
1939 return true;
1940 }
1941 // Switch the direction back
1942 direction[0] *= -1;
1943 direction[1] *= -1;
1944
1945 // Swap the components back
1946 temp = direction[1];
1947 direction[1] = direction[0];
1948 direction[0] = temp;
1949 }
1950 return false;
1951 }
1952
Adam Cohen482ed822012-03-02 14:15:13 -08001953 private boolean rearrangementExists(int cellX, int cellY, int spanX, int spanY, int[] direction,
Adam Cohen8baab352012-03-20 17:39:21 -07001954 View ignoreView, ItemConfiguration solution) {
Winson Chunge3e03bc2012-05-01 15:10:11 -07001955 // Return early if get invalid cell positions
1956 if (cellX < 0 || cellY < 0) return false;
Adam Cohen482ed822012-03-02 14:15:13 -08001957
Adam Cohen8baab352012-03-20 17:39:21 -07001958 mIntersectingViews.clear();
Adam Cohen482ed822012-03-02 14:15:13 -08001959 mOccupiedRect.set(cellX, cellY, cellX + spanX, cellY + spanY);
Adam Cohen482ed822012-03-02 14:15:13 -08001960
Adam Cohen8baab352012-03-20 17:39:21 -07001961 // Mark the desired location of the view currently being dragged.
Adam Cohen482ed822012-03-02 14:15:13 -08001962 if (ignoreView != null) {
Adam Cohen8baab352012-03-20 17:39:21 -07001963 CellAndSpan c = solution.map.get(ignoreView);
Adam Cohen19f37922012-03-21 11:59:11 -07001964 if (c != null) {
1965 c.x = cellX;
1966 c.y = cellY;
1967 }
Adam Cohen482ed822012-03-02 14:15:13 -08001968 }
Adam Cohen482ed822012-03-02 14:15:13 -08001969 Rect r0 = new Rect(cellX, cellY, cellX + spanX, cellY + spanY);
1970 Rect r1 = new Rect();
Adam Cohen8baab352012-03-20 17:39:21 -07001971 for (View child: solution.map.keySet()) {
Adam Cohen482ed822012-03-02 14:15:13 -08001972 if (child == ignoreView) continue;
Adam Cohen8baab352012-03-20 17:39:21 -07001973 CellAndSpan c = solution.map.get(child);
Adam Cohen482ed822012-03-02 14:15:13 -08001974 LayoutParams lp = (LayoutParams) child.getLayoutParams();
Adam Cohen8baab352012-03-20 17:39:21 -07001975 r1.set(c.x, c.y, c.x + c.spanX, c.y + c.spanY);
Adam Cohen482ed822012-03-02 14:15:13 -08001976 if (Rect.intersects(r0, r1)) {
1977 if (!lp.canReorder) {
1978 return false;
1979 }
1980 mIntersectingViews.add(child);
1981 }
1982 }
Adam Cohen47a876d2012-03-19 13:21:41 -07001983
Adam Cohenfa3c58f2013-12-06 16:10:55 -08001984 solution.intersectingViews = new ArrayList<View>(mIntersectingViews);
1985
Winson Chung5f8afe62013-08-12 16:19:28 -07001986 // First we try to find a solution which respects the push mechanic. That is,
Adam Cohen4abc5bd2012-05-29 21:06:03 -07001987 // we try to find a solution such that no displaced item travels through another item
1988 // without also displacing that item.
1989 if (attemptPushInDirection(mIntersectingViews, mOccupiedRect, direction, ignoreView,
Adam Cohen19f37922012-03-21 11:59:11 -07001990 solution)) {
Adam Cohen47a876d2012-03-19 13:21:41 -07001991 return true;
1992 }
Adam Cohen47a876d2012-03-19 13:21:41 -07001993
Adam Cohen4abc5bd2012-05-29 21:06:03 -07001994 // Next we try moving the views as a block, but without requiring the push mechanic.
Adam Cohenf3900c22012-11-16 18:28:11 -08001995 if (addViewsToTempLocation(mIntersectingViews, mOccupiedRect, direction, ignoreView,
Adam Cohen19f37922012-03-21 11:59:11 -07001996 solution)) {
Adam Cohen482ed822012-03-02 14:15:13 -08001997 return true;
1998 }
Adam Cohen47a876d2012-03-19 13:21:41 -07001999
Adam Cohen482ed822012-03-02 14:15:13 -08002000 // Ok, they couldn't move as a block, let's move them individually
2001 for (View v : mIntersectingViews) {
Adam Cohen8baab352012-03-20 17:39:21 -07002002 if (!addViewToTempLocation(v, mOccupiedRect, direction, solution)) {
Adam Cohen482ed822012-03-02 14:15:13 -08002003 return false;
2004 }
2005 }
2006 return true;
2007 }
2008
2009 /*
2010 * Returns a pair (x, y), where x,y are in {-1, 0, 1} corresponding to vector between
2011 * the provided point and the provided cell
2012 */
Adam Cohen47a876d2012-03-19 13:21:41 -07002013 private void computeDirectionVector(float deltaX, float deltaY, int[] result) {
Adam Cohen482ed822012-03-02 14:15:13 -08002014 double angle = Math.atan(((float) deltaY) / deltaX);
2015
2016 result[0] = 0;
2017 result[1] = 0;
2018 if (Math.abs(Math.cos(angle)) > 0.5f) {
2019 result[0] = (int) Math.signum(deltaX);
2020 }
2021 if (Math.abs(Math.sin(angle)) > 0.5f) {
2022 result[1] = (int) Math.signum(deltaY);
2023 }
2024 }
2025
Adam Cohen8baab352012-03-20 17:39:21 -07002026 private void copyOccupiedArray(boolean[][] occupied) {
2027 for (int i = 0; i < mCountX; i++) {
2028 for (int j = 0; j < mCountY; j++) {
2029 occupied[i][j] = mOccupied[i][j];
2030 }
2031 }
2032 }
2033
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002034 ItemConfiguration findReorderSolution(int pixelX, int pixelY, int minSpanX, int minSpanY,
2035 int spanX, int spanY, int[] direction, View dragView, boolean decX,
2036 ItemConfiguration solution) {
Adam Cohen8baab352012-03-20 17:39:21 -07002037 // Copy the current state into the solution. This solution will be manipulated as necessary.
2038 copyCurrentStateToSolution(solution, false);
2039 // Copy the current occupied array into the temporary occupied array. This array will be
2040 // manipulated as necessary to find a solution.
2041 copyOccupiedArray(mTmpOccupied);
Adam Cohen482ed822012-03-02 14:15:13 -08002042
2043 // We find the nearest cell into which we would place the dragged item, assuming there's
2044 // nothing in its way.
2045 int result[] = new int[2];
2046 result = findNearestArea(pixelX, pixelY, spanX, spanY, result);
2047
2048 boolean success = false;
2049 // First we try the exact nearest position of the item being dragged,
2050 // we will then want to try to move this around to other neighbouring positions
Adam Cohen8baab352012-03-20 17:39:21 -07002051 success = rearrangementExists(result[0], result[1], spanX, spanY, direction, dragView,
2052 solution);
Adam Cohen482ed822012-03-02 14:15:13 -08002053
2054 if (!success) {
2055 // We try shrinking the widget down to size in an alternating pattern, shrink 1 in
2056 // x, then 1 in y etc.
2057 if (spanX > minSpanX && (minSpanY == spanY || decX)) {
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002058 return findReorderSolution(pixelX, pixelY, minSpanX, minSpanY, spanX - 1, spanY,
2059 direction, dragView, false, solution);
Adam Cohen482ed822012-03-02 14:15:13 -08002060 } else if (spanY > minSpanY) {
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002061 return findReorderSolution(pixelX, pixelY, minSpanX, minSpanY, spanX, spanY - 1,
2062 direction, dragView, true, solution);
Adam Cohen482ed822012-03-02 14:15:13 -08002063 }
2064 solution.isSolution = false;
2065 } else {
2066 solution.isSolution = true;
2067 solution.dragViewX = result[0];
2068 solution.dragViewY = result[1];
2069 solution.dragViewSpanX = spanX;
2070 solution.dragViewSpanY = spanY;
Adam Cohen482ed822012-03-02 14:15:13 -08002071 }
2072 return solution;
2073 }
2074
2075 private void copyCurrentStateToSolution(ItemConfiguration solution, boolean temp) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07002076 int childCount = mShortcutsAndWidgets.getChildCount();
Adam Cohen482ed822012-03-02 14:15:13 -08002077 for (int i = 0; i < childCount; i++) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07002078 View child = mShortcutsAndWidgets.getChildAt(i);
Adam Cohen482ed822012-03-02 14:15:13 -08002079 LayoutParams lp = (LayoutParams) child.getLayoutParams();
Adam Cohen8baab352012-03-20 17:39:21 -07002080 CellAndSpan c;
Adam Cohen482ed822012-03-02 14:15:13 -08002081 if (temp) {
Adam Cohen8baab352012-03-20 17:39:21 -07002082 c = new CellAndSpan(lp.tmpCellX, lp.tmpCellY, lp.cellHSpan, lp.cellVSpan);
Adam Cohen482ed822012-03-02 14:15:13 -08002083 } else {
Adam Cohen8baab352012-03-20 17:39:21 -07002084 c = new CellAndSpan(lp.cellX, lp.cellY, lp.cellHSpan, lp.cellVSpan);
Adam Cohen482ed822012-03-02 14:15:13 -08002085 }
Adam Cohenf3900c22012-11-16 18:28:11 -08002086 solution.add(child, c);
Adam Cohen482ed822012-03-02 14:15:13 -08002087 }
2088 }
2089
2090 private void copySolutionToTempState(ItemConfiguration solution, View dragView) {
2091 for (int i = 0; i < mCountX; i++) {
2092 for (int j = 0; j < mCountY; j++) {
2093 mTmpOccupied[i][j] = false;
2094 }
2095 }
2096
Michael Jurkaa52570f2012-03-20 03:18:20 -07002097 int childCount = mShortcutsAndWidgets.getChildCount();
Adam Cohen482ed822012-03-02 14:15:13 -08002098 for (int i = 0; i < childCount; i++) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07002099 View child = mShortcutsAndWidgets.getChildAt(i);
Adam Cohen482ed822012-03-02 14:15:13 -08002100 if (child == dragView) continue;
2101 LayoutParams lp = (LayoutParams) child.getLayoutParams();
Adam Cohen8baab352012-03-20 17:39:21 -07002102 CellAndSpan c = solution.map.get(child);
2103 if (c != null) {
2104 lp.tmpCellX = c.x;
2105 lp.tmpCellY = c.y;
2106 lp.cellHSpan = c.spanX;
2107 lp.cellVSpan = c.spanY;
2108 markCellsForView(c.x, c.y, c.spanX, c.spanY, mTmpOccupied, true);
Adam Cohen482ed822012-03-02 14:15:13 -08002109 }
2110 }
2111 markCellsForView(solution.dragViewX, solution.dragViewY, solution.dragViewSpanX,
2112 solution.dragViewSpanY, mTmpOccupied, true);
2113 }
2114
2115 private void animateItemsToSolution(ItemConfiguration solution, View dragView, boolean
2116 commitDragView) {
2117
2118 boolean[][] occupied = DESTRUCTIVE_REORDER ? mOccupied : mTmpOccupied;
2119 for (int i = 0; i < mCountX; i++) {
2120 for (int j = 0; j < mCountY; j++) {
2121 occupied[i][j] = false;
2122 }
2123 }
2124
Michael Jurkaa52570f2012-03-20 03:18:20 -07002125 int childCount = mShortcutsAndWidgets.getChildCount();
Adam Cohen482ed822012-03-02 14:15:13 -08002126 for (int i = 0; i < childCount; i++) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07002127 View child = mShortcutsAndWidgets.getChildAt(i);
Adam Cohen482ed822012-03-02 14:15:13 -08002128 if (child == dragView) continue;
Adam Cohen8baab352012-03-20 17:39:21 -07002129 CellAndSpan c = solution.map.get(child);
2130 if (c != null) {
Adam Cohen19f37922012-03-21 11:59:11 -07002131 animateChildToPosition(child, c.x, c.y, REORDER_ANIMATION_DURATION, 0,
2132 DESTRUCTIVE_REORDER, false);
Adam Cohen8baab352012-03-20 17:39:21 -07002133 markCellsForView(c.x, c.y, c.spanX, c.spanY, occupied, true);
Adam Cohen482ed822012-03-02 14:15:13 -08002134 }
2135 }
2136 if (commitDragView) {
2137 markCellsForView(solution.dragViewX, solution.dragViewY, solution.dragViewSpanX,
2138 solution.dragViewSpanY, occupied, true);
2139 }
2140 }
2141
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002142
2143 // This method starts or changes the reorder preview animations
2144 private void beginOrAdjustReorderPreviewAnimations(ItemConfiguration solution,
2145 View dragView, int delay, int mode) {
Adam Cohen19f37922012-03-21 11:59:11 -07002146 int childCount = mShortcutsAndWidgets.getChildCount();
Adam Cohen19f37922012-03-21 11:59:11 -07002147 for (int i = 0; i < childCount; i++) {
2148 View child = mShortcutsAndWidgets.getChildAt(i);
2149 if (child == dragView) continue;
2150 CellAndSpan c = solution.map.get(child);
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002151 boolean skip = mode == ReorderPreviewAnimation.MODE_HINT && solution.intersectingViews
2152 != null && !solution.intersectingViews.contains(child);
2153
Adam Cohen19f37922012-03-21 11:59:11 -07002154 LayoutParams lp = (LayoutParams) child.getLayoutParams();
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002155 if (c != null && !skip) {
2156 ReorderPreviewAnimation rha = new ReorderPreviewAnimation(child, mode, lp.cellX,
2157 lp.cellY, c.x, c.y, c.spanX, c.spanY);
Adam Cohend024f982012-05-23 18:26:45 -07002158 rha.animate();
Adam Cohen19f37922012-03-21 11:59:11 -07002159 }
2160 }
2161 }
2162
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002163 // Class which represents the reorder preview animations. These animations show that an item is
Adam Cohen19f37922012-03-21 11:59:11 -07002164 // in a temporary state, and hint at where the item will return to.
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002165 class ReorderPreviewAnimation {
Adam Cohen19f37922012-03-21 11:59:11 -07002166 View child;
Adam Cohend024f982012-05-23 18:26:45 -07002167 float finalDeltaX;
2168 float finalDeltaY;
2169 float initDeltaX;
2170 float initDeltaY;
2171 float finalScale;
2172 float initScale;
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002173 int mode;
2174 boolean repeating = false;
2175 private static final int PREVIEW_DURATION = 300;
2176 private static final int HINT_DURATION = Workspace.REORDER_TIMEOUT;
2177
2178 public static final int MODE_HINT = 0;
2179 public static final int MODE_PREVIEW = 1;
2180
Adam Cohene7587d22012-05-24 18:50:02 -07002181 Animator a;
Adam Cohen19f37922012-03-21 11:59:11 -07002182
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002183 public ReorderPreviewAnimation(View child, int mode, int cellX0, int cellY0, int cellX1,
2184 int cellY1, int spanX, int spanY) {
Adam Cohen19f37922012-03-21 11:59:11 -07002185 regionToCenterPoint(cellX0, cellY0, spanX, spanY, mTmpPoint);
2186 final int x0 = mTmpPoint[0];
2187 final int y0 = mTmpPoint[1];
2188 regionToCenterPoint(cellX1, cellY1, spanX, spanY, mTmpPoint);
2189 final int x1 = mTmpPoint[0];
2190 final int y1 = mTmpPoint[1];
2191 final int dX = x1 - x0;
2192 final int dY = y1 - y0;
Adam Cohend024f982012-05-23 18:26:45 -07002193 finalDeltaX = 0;
2194 finalDeltaY = 0;
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002195 int dir = mode == MODE_HINT ? -1 : 1;
Adam Cohen19f37922012-03-21 11:59:11 -07002196 if (dX == dY && dX == 0) {
2197 } else {
2198 if (dY == 0) {
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002199 finalDeltaX = - dir * Math.signum(dX) * mReorderPreviewAnimationMagnitude;
Adam Cohen19f37922012-03-21 11:59:11 -07002200 } else if (dX == 0) {
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002201 finalDeltaY = - dir * Math.signum(dY) * mReorderPreviewAnimationMagnitude;
Adam Cohen19f37922012-03-21 11:59:11 -07002202 } else {
2203 double angle = Math.atan( (float) (dY) / dX);
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002204 finalDeltaX = (int) (- dir * Math.signum(dX) *
2205 Math.abs(Math.cos(angle) * mReorderPreviewAnimationMagnitude));
2206 finalDeltaY = (int) (- dir * Math.signum(dY) *
2207 Math.abs(Math.sin(angle) * mReorderPreviewAnimationMagnitude));
Adam Cohen19f37922012-03-21 11:59:11 -07002208 }
2209 }
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002210 this.mode = mode;
Adam Cohend024f982012-05-23 18:26:45 -07002211 initDeltaX = child.getTranslationX();
2212 initDeltaY = child.getTranslationY();
Adam Cohen307fe232012-08-16 17:55:58 -07002213 finalScale = getChildrenScale() - 4.0f / child.getWidth();
Adam Cohend024f982012-05-23 18:26:45 -07002214 initScale = child.getScaleX();
Adam Cohen19f37922012-03-21 11:59:11 -07002215 this.child = child;
2216 }
2217
Adam Cohend024f982012-05-23 18:26:45 -07002218 void animate() {
Adam Cohen19f37922012-03-21 11:59:11 -07002219 if (mShakeAnimators.containsKey(child)) {
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002220 ReorderPreviewAnimation oldAnimation = mShakeAnimators.get(child);
Adam Cohend024f982012-05-23 18:26:45 -07002221 oldAnimation.cancel();
Adam Cohen19f37922012-03-21 11:59:11 -07002222 mShakeAnimators.remove(child);
Adam Cohene7587d22012-05-24 18:50:02 -07002223 if (finalDeltaX == 0 && finalDeltaY == 0) {
2224 completeAnimationImmediately();
2225 return;
2226 }
Adam Cohen19f37922012-03-21 11:59:11 -07002227 }
Adam Cohend024f982012-05-23 18:26:45 -07002228 if (finalDeltaX == 0 && finalDeltaY == 0) {
Adam Cohen19f37922012-03-21 11:59:11 -07002229 return;
2230 }
Michael Jurkaf1ad6082013-03-13 12:55:46 +01002231 ValueAnimator va = LauncherAnimUtils.ofFloat(child, 0f, 1f);
Adam Cohene7587d22012-05-24 18:50:02 -07002232 a = va;
Adam Cohen19f37922012-03-21 11:59:11 -07002233 va.setRepeatMode(ValueAnimator.REVERSE);
2234 va.setRepeatCount(ValueAnimator.INFINITE);
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002235 va.setDuration(mode == MODE_HINT ? HINT_DURATION : PREVIEW_DURATION);
Adam Cohend024f982012-05-23 18:26:45 -07002236 va.setStartDelay((int) (Math.random() * 60));
Adam Cohen19f37922012-03-21 11:59:11 -07002237 va.addUpdateListener(new AnimatorUpdateListener() {
2238 @Override
2239 public void onAnimationUpdate(ValueAnimator animation) {
2240 float r = ((Float) animation.getAnimatedValue()).floatValue();
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002241 float r1 = (mode == MODE_HINT && repeating) ? 1.0f : r;
2242 float x = r1 * finalDeltaX + (1 - r1) * initDeltaX;
2243 float y = r1 * finalDeltaY + (1 - r1) * initDeltaY;
Adam Cohen19f37922012-03-21 11:59:11 -07002244 child.setTranslationX(x);
2245 child.setTranslationY(y);
Adam Cohend024f982012-05-23 18:26:45 -07002246 float s = r * finalScale + (1 - r) * initScale;
Brandon Keely50e6e562012-05-08 16:28:49 -07002247 child.setScaleX(s);
2248 child.setScaleY(s);
Adam Cohen19f37922012-03-21 11:59:11 -07002249 }
2250 });
2251 va.addListener(new AnimatorListenerAdapter() {
2252 public void onAnimationRepeat(Animator animation) {
Adam Cohen19f37922012-03-21 11:59:11 -07002253 // We make sure to end only after a full period
Adam Cohend024f982012-05-23 18:26:45 -07002254 initDeltaX = 0;
2255 initDeltaY = 0;
Adam Cohen307fe232012-08-16 17:55:58 -07002256 initScale = getChildrenScale();
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002257 repeating = true;
Adam Cohen19f37922012-03-21 11:59:11 -07002258 }
2259 });
Adam Cohen19f37922012-03-21 11:59:11 -07002260 mShakeAnimators.put(child, this);
2261 va.start();
2262 }
2263
Adam Cohend024f982012-05-23 18:26:45 -07002264 private void cancel() {
Adam Cohene7587d22012-05-24 18:50:02 -07002265 if (a != null) {
2266 a.cancel();
2267 }
Adam Cohen19f37922012-03-21 11:59:11 -07002268 }
Adam Cohene7587d22012-05-24 18:50:02 -07002269
Brandon Keely50e6e562012-05-08 16:28:49 -07002270 private void completeAnimationImmediately() {
Adam Cohene7587d22012-05-24 18:50:02 -07002271 if (a != null) {
2272 a.cancel();
2273 }
Brandon Keely50e6e562012-05-08 16:28:49 -07002274
Michael Jurka2ecf9952012-06-18 12:52:28 -07002275 AnimatorSet s = LauncherAnimUtils.createAnimatorSet();
Adam Cohene7587d22012-05-24 18:50:02 -07002276 a = s;
Brandon Keely50e6e562012-05-08 16:28:49 -07002277 s.playTogether(
Adam Cohen307fe232012-08-16 17:55:58 -07002278 LauncherAnimUtils.ofFloat(child, "scaleX", getChildrenScale()),
2279 LauncherAnimUtils.ofFloat(child, "scaleY", getChildrenScale()),
Michael Jurka2ecf9952012-06-18 12:52:28 -07002280 LauncherAnimUtils.ofFloat(child, "translationX", 0f),
2281 LauncherAnimUtils.ofFloat(child, "translationY", 0f)
Brandon Keely50e6e562012-05-08 16:28:49 -07002282 );
2283 s.setDuration(REORDER_ANIMATION_DURATION);
2284 s.setInterpolator(new android.view.animation.DecelerateInterpolator(1.5f));
2285 s.start();
2286 }
Adam Cohen19f37922012-03-21 11:59:11 -07002287 }
2288
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002289 private void completeAndClearReorderPreviewAnimations() {
2290 for (ReorderPreviewAnimation a: mShakeAnimators.values()) {
Brandon Keely50e6e562012-05-08 16:28:49 -07002291 a.completeAnimationImmediately();
Adam Cohen19f37922012-03-21 11:59:11 -07002292 }
2293 mShakeAnimators.clear();
2294 }
2295
Adam Cohen482ed822012-03-02 14:15:13 -08002296 private void commitTempPlacement() {
2297 for (int i = 0; i < mCountX; i++) {
2298 for (int j = 0; j < mCountY; j++) {
2299 mOccupied[i][j] = mTmpOccupied[i][j];
2300 }
2301 }
Michael Jurkaa52570f2012-03-20 03:18:20 -07002302 int childCount = mShortcutsAndWidgets.getChildCount();
Adam Cohen482ed822012-03-02 14:15:13 -08002303 for (int i = 0; i < childCount; i++) {
Adam Cohenea889a22012-03-27 16:45:39 -07002304 View child = mShortcutsAndWidgets.getChildAt(i);
2305 LayoutParams lp = (LayoutParams) child.getLayoutParams();
2306 ItemInfo info = (ItemInfo) child.getTag();
Adam Cohen2acce882012-03-28 19:03:19 -07002307 // We do a null check here because the item info can be null in the case of the
2308 // AllApps button in the hotseat.
2309 if (info != null) {
Adam Cohen487f7dd2012-06-28 18:12:10 -07002310 if (info.cellX != lp.tmpCellX || info.cellY != lp.tmpCellY ||
2311 info.spanX != lp.cellHSpan || info.spanY != lp.cellVSpan) {
2312 info.requiresDbUpdate = true;
2313 }
Adam Cohen2acce882012-03-28 19:03:19 -07002314 info.cellX = lp.cellX = lp.tmpCellX;
2315 info.cellY = lp.cellY = lp.tmpCellY;
Adam Cohenbebf0422012-04-11 18:06:28 -07002316 info.spanX = lp.cellHSpan;
2317 info.spanY = lp.cellVSpan;
Adam Cohen2acce882012-03-28 19:03:19 -07002318 }
Adam Cohen482ed822012-03-02 14:15:13 -08002319 }
Adam Cohen2acce882012-03-28 19:03:19 -07002320 mLauncher.getWorkspace().updateItemLocationsInDatabase(this);
Adam Cohen482ed822012-03-02 14:15:13 -08002321 }
2322
2323 public void setUseTempCoords(boolean useTempCoords) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07002324 int childCount = mShortcutsAndWidgets.getChildCount();
Adam Cohen482ed822012-03-02 14:15:13 -08002325 for (int i = 0; i < childCount; i++) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07002326 LayoutParams lp = (LayoutParams) mShortcutsAndWidgets.getChildAt(i).getLayoutParams();
Adam Cohen482ed822012-03-02 14:15:13 -08002327 lp.useTmpCoords = useTempCoords;
2328 }
2329 }
2330
Adam Cohen482ed822012-03-02 14:15:13 -08002331 ItemConfiguration findConfigurationNoShuffle(int pixelX, int pixelY, int minSpanX, int minSpanY,
2332 int spanX, int spanY, View dragView, ItemConfiguration solution) {
2333 int[] result = new int[2];
2334 int[] resultSpan = new int[2];
2335 findNearestVacantArea(pixelX, pixelY, minSpanX, minSpanY, spanX, spanY, null, result,
2336 resultSpan);
2337 if (result[0] >= 0 && result[1] >= 0) {
2338 copyCurrentStateToSolution(solution, false);
2339 solution.dragViewX = result[0];
2340 solution.dragViewY = result[1];
2341 solution.dragViewSpanX = resultSpan[0];
2342 solution.dragViewSpanY = resultSpan[1];
2343 solution.isSolution = true;
2344 } else {
2345 solution.isSolution = false;
2346 }
2347 return solution;
2348 }
2349
2350 public void prepareChildForDrag(View child) {
2351 markCellsAsUnoccupiedForView(child);
Adam Cohen482ed822012-03-02 14:15:13 -08002352 }
2353
Adam Cohen19f37922012-03-21 11:59:11 -07002354 /* This seems like it should be obvious and straight-forward, but when the direction vector
2355 needs to match with the notion of the dragView pushing other views, we have to employ
2356 a slightly more subtle notion of the direction vector. The question is what two points is
2357 the vector between? The center of the dragView and its desired destination? Not quite, as
2358 this doesn't necessarily coincide with the interaction of the dragView and items occupying
2359 those cells. Instead we use some heuristics to often lock the vector to up, down, left
2360 or right, which helps make pushing feel right.
2361 */
2362 private void getDirectionVectorForDrop(int dragViewCenterX, int dragViewCenterY, int spanX,
2363 int spanY, View dragView, int[] resultDirection) {
2364 int[] targetDestination = new int[2];
2365
2366 findNearestArea(dragViewCenterX, dragViewCenterY, spanX, spanY, targetDestination);
2367 Rect dragRect = new Rect();
2368 regionToRect(targetDestination[0], targetDestination[1], spanX, spanY, dragRect);
2369 dragRect.offset(dragViewCenterX - dragRect.centerX(), dragViewCenterY - dragRect.centerY());
2370
2371 Rect dropRegionRect = new Rect();
2372 getViewsIntersectingRegion(targetDestination[0], targetDestination[1], spanX, spanY,
2373 dragView, dropRegionRect, mIntersectingViews);
2374
2375 int dropRegionSpanX = dropRegionRect.width();
2376 int dropRegionSpanY = dropRegionRect.height();
2377
2378 regionToRect(dropRegionRect.left, dropRegionRect.top, dropRegionRect.width(),
2379 dropRegionRect.height(), dropRegionRect);
2380
2381 int deltaX = (dropRegionRect.centerX() - dragViewCenterX) / spanX;
2382 int deltaY = (dropRegionRect.centerY() - dragViewCenterY) / spanY;
2383
2384 if (dropRegionSpanX == mCountX || spanX == mCountX) {
2385 deltaX = 0;
2386 }
2387 if (dropRegionSpanY == mCountY || spanY == mCountY) {
2388 deltaY = 0;
2389 }
2390
2391 if (deltaX == 0 && deltaY == 0) {
2392 // No idea what to do, give a random direction.
2393 resultDirection[0] = 1;
2394 resultDirection[1] = 0;
2395 } else {
2396 computeDirectionVector(deltaX, deltaY, resultDirection);
2397 }
2398 }
2399
2400 // For a given cell and span, fetch the set of views intersecting the region.
2401 private void getViewsIntersectingRegion(int cellX, int cellY, int spanX, int spanY,
2402 View dragView, Rect boundingRect, ArrayList<View> intersectingViews) {
2403 if (boundingRect != null) {
2404 boundingRect.set(cellX, cellY, cellX + spanX, cellY + spanY);
2405 }
2406 intersectingViews.clear();
2407 Rect r0 = new Rect(cellX, cellY, cellX + spanX, cellY + spanY);
2408 Rect r1 = new Rect();
2409 final int count = mShortcutsAndWidgets.getChildCount();
2410 for (int i = 0; i < count; i++) {
2411 View child = mShortcutsAndWidgets.getChildAt(i);
2412 if (child == dragView) continue;
2413 LayoutParams lp = (LayoutParams) child.getLayoutParams();
2414 r1.set(lp.cellX, lp.cellY, lp.cellX + lp.cellHSpan, lp.cellY + lp.cellVSpan);
2415 if (Rect.intersects(r0, r1)) {
2416 mIntersectingViews.add(child);
2417 if (boundingRect != null) {
2418 boundingRect.union(r1);
2419 }
2420 }
2421 }
2422 }
2423
2424 boolean isNearestDropLocationOccupied(int pixelX, int pixelY, int spanX, int spanY,
2425 View dragView, int[] result) {
2426 result = findNearestArea(pixelX, pixelY, spanX, spanY, result);
2427 getViewsIntersectingRegion(result[0], result[1], spanX, spanY, dragView, null,
2428 mIntersectingViews);
2429 return !mIntersectingViews.isEmpty();
2430 }
2431
2432 void revertTempState() {
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002433 completeAndClearReorderPreviewAnimations();
2434 if (isItemPlacementDirty() && !DESTRUCTIVE_REORDER) {
2435 final int count = mShortcutsAndWidgets.getChildCount();
2436 for (int i = 0; i < count; i++) {
2437 View child = mShortcutsAndWidgets.getChildAt(i);
2438 LayoutParams lp = (LayoutParams) child.getLayoutParams();
2439 if (lp.tmpCellX != lp.cellX || lp.tmpCellY != lp.cellY) {
2440 lp.tmpCellX = lp.cellX;
2441 lp.tmpCellY = lp.cellY;
2442 animateChildToPosition(child, lp.cellX, lp.cellY, REORDER_ANIMATION_DURATION,
2443 0, false, false);
2444 }
Adam Cohen19f37922012-03-21 11:59:11 -07002445 }
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002446 setItemPlacementDirty(false);
Adam Cohen19f37922012-03-21 11:59:11 -07002447 }
Adam Cohen19f37922012-03-21 11:59:11 -07002448 }
2449
Adam Cohenbebf0422012-04-11 18:06:28 -07002450 boolean createAreaForResize(int cellX, int cellY, int spanX, int spanY,
2451 View dragView, int[] direction, boolean commit) {
2452 int[] pixelXY = new int[2];
2453 regionToCenterPoint(cellX, cellY, spanX, spanY, pixelXY);
2454
2455 // First we determine if things have moved enough to cause a different layout
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002456 ItemConfiguration swapSolution = findReorderSolution(pixelXY[0], pixelXY[1], spanX, spanY,
Adam Cohenbebf0422012-04-11 18:06:28 -07002457 spanX, spanY, direction, dragView, true, new ItemConfiguration());
2458
2459 setUseTempCoords(true);
2460 if (swapSolution != null && swapSolution.isSolution) {
2461 // If we're just testing for a possible location (MODE_ACCEPT_DROP), we don't bother
2462 // committing anything or animating anything as we just want to determine if a solution
2463 // exists
2464 copySolutionToTempState(swapSolution, dragView);
2465 setItemPlacementDirty(true);
2466 animateItemsToSolution(swapSolution, dragView, commit);
2467
2468 if (commit) {
2469 commitTempPlacement();
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002470 completeAndClearReorderPreviewAnimations();
Adam Cohenbebf0422012-04-11 18:06:28 -07002471 setItemPlacementDirty(false);
2472 } else {
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002473 beginOrAdjustReorderPreviewAnimations(swapSolution, dragView,
2474 REORDER_ANIMATION_DURATION, ReorderPreviewAnimation.MODE_PREVIEW);
Adam Cohenbebf0422012-04-11 18:06:28 -07002475 }
2476 mShortcutsAndWidgets.requestLayout();
2477 }
2478 return swapSolution.isSolution;
2479 }
2480
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002481 int[] performReorder(int pixelX, int pixelY, int minSpanX, int minSpanY, int spanX, int spanY,
Adam Cohen482ed822012-03-02 14:15:13 -08002482 View dragView, int[] result, int resultSpan[], int mode) {
Adam Cohen482ed822012-03-02 14:15:13 -08002483 // First we determine if things have moved enough to cause a different layout
Adam Cohen47a876d2012-03-19 13:21:41 -07002484 result = findNearestArea(pixelX, pixelY, spanX, spanY, result);
Adam Cohen482ed822012-03-02 14:15:13 -08002485
2486 if (resultSpan == null) {
2487 resultSpan = new int[2];
2488 }
2489
Adam Cohen19f37922012-03-21 11:59:11 -07002490 // When we are checking drop validity or actually dropping, we don't recompute the
2491 // direction vector, since we want the solution to match the preview, and it's possible
2492 // that the exact position of the item has changed to result in a new reordering outcome.
Adam Cohenb209e632012-03-27 17:09:36 -07002493 if ((mode == MODE_ON_DROP || mode == MODE_ON_DROP_EXTERNAL || mode == MODE_ACCEPT_DROP)
2494 && mPreviousReorderDirection[0] != INVALID_DIRECTION) {
Adam Cohen19f37922012-03-21 11:59:11 -07002495 mDirectionVector[0] = mPreviousReorderDirection[0];
2496 mDirectionVector[1] = mPreviousReorderDirection[1];
2497 // We reset this vector after drop
Adam Cohenb209e632012-03-27 17:09:36 -07002498 if (mode == MODE_ON_DROP || mode == MODE_ON_DROP_EXTERNAL) {
2499 mPreviousReorderDirection[0] = INVALID_DIRECTION;
2500 mPreviousReorderDirection[1] = INVALID_DIRECTION;
Adam Cohen19f37922012-03-21 11:59:11 -07002501 }
2502 } else {
2503 getDirectionVectorForDrop(pixelX, pixelY, spanX, spanY, dragView, mDirectionVector);
2504 mPreviousReorderDirection[0] = mDirectionVector[0];
2505 mPreviousReorderDirection[1] = mDirectionVector[1];
2506 }
2507
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002508 // Find a solution involving pushing / displacing any items in the way
2509 ItemConfiguration swapSolution = findReorderSolution(pixelX, pixelY, minSpanX, minSpanY,
Adam Cohen482ed822012-03-02 14:15:13 -08002510 spanX, spanY, mDirectionVector, dragView, true, new ItemConfiguration());
2511
2512 // We attempt the approach which doesn't shuffle views at all
2513 ItemConfiguration noShuffleSolution = findConfigurationNoShuffle(pixelX, pixelY, minSpanX,
2514 minSpanY, spanX, spanY, dragView, new ItemConfiguration());
2515
2516 ItemConfiguration finalSolution = null;
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002517
2518 // If the reorder solution requires resizing (shrinking) the item being dropped, we instead
2519 // favor a solution in which the item is not resized, but
Adam Cohen482ed822012-03-02 14:15:13 -08002520 if (swapSolution.isSolution && swapSolution.area() >= noShuffleSolution.area()) {
2521 finalSolution = swapSolution;
2522 } else if (noShuffleSolution.isSolution) {
2523 finalSolution = noShuffleSolution;
2524 }
2525
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002526 if (mode == MODE_SHOW_REORDER_HINT) {
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002527 if (finalSolution != null) {
Adam Cohenfe692872013-12-11 14:47:23 -08002528 beginOrAdjustReorderPreviewAnimations(finalSolution, dragView, 0,
2529 ReorderPreviewAnimation.MODE_HINT);
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002530 result[0] = finalSolution.dragViewX;
2531 result[1] = finalSolution.dragViewY;
2532 resultSpan[0] = finalSolution.dragViewSpanX;
2533 resultSpan[1] = finalSolution.dragViewSpanY;
2534 } else {
2535 result[0] = result[1] = resultSpan[0] = resultSpan[1] = -1;
2536 }
2537 return result;
2538 }
2539
Adam Cohen482ed822012-03-02 14:15:13 -08002540 boolean foundSolution = true;
2541 if (!DESTRUCTIVE_REORDER) {
2542 setUseTempCoords(true);
2543 }
2544
2545 if (finalSolution != null) {
2546 result[0] = finalSolution.dragViewX;
2547 result[1] = finalSolution.dragViewY;
2548 resultSpan[0] = finalSolution.dragViewSpanX;
2549 resultSpan[1] = finalSolution.dragViewSpanY;
2550
2551 // If we're just testing for a possible location (MODE_ACCEPT_DROP), we don't bother
2552 // committing anything or animating anything as we just want to determine if a solution
2553 // exists
2554 if (mode == MODE_DRAG_OVER || mode == MODE_ON_DROP || mode == MODE_ON_DROP_EXTERNAL) {
2555 if (!DESTRUCTIVE_REORDER) {
2556 copySolutionToTempState(finalSolution, dragView);
2557 }
2558 setItemPlacementDirty(true);
2559 animateItemsToSolution(finalSolution, dragView, mode == MODE_ON_DROP);
2560
Adam Cohen19f37922012-03-21 11:59:11 -07002561 if (!DESTRUCTIVE_REORDER &&
2562 (mode == MODE_ON_DROP || mode == MODE_ON_DROP_EXTERNAL)) {
Adam Cohen482ed822012-03-02 14:15:13 -08002563 commitTempPlacement();
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002564 completeAndClearReorderPreviewAnimations();
Adam Cohen19f37922012-03-21 11:59:11 -07002565 setItemPlacementDirty(false);
2566 } else {
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002567 beginOrAdjustReorderPreviewAnimations(finalSolution, dragView,
2568 REORDER_ANIMATION_DURATION, ReorderPreviewAnimation.MODE_PREVIEW);
Adam Cohen482ed822012-03-02 14:15:13 -08002569 }
2570 }
2571 } else {
2572 foundSolution = false;
2573 result[0] = result[1] = resultSpan[0] = resultSpan[1] = -1;
2574 }
2575
2576 if ((mode == MODE_ON_DROP || !foundSolution) && !DESTRUCTIVE_REORDER) {
2577 setUseTempCoords(false);
2578 }
Adam Cohen482ed822012-03-02 14:15:13 -08002579
Michael Jurkaa52570f2012-03-20 03:18:20 -07002580 mShortcutsAndWidgets.requestLayout();
Adam Cohen482ed822012-03-02 14:15:13 -08002581 return result;
2582 }
2583
Adam Cohen19f37922012-03-21 11:59:11 -07002584 void setItemPlacementDirty(boolean dirty) {
2585 mItemPlacementDirty = dirty;
Adam Cohen482ed822012-03-02 14:15:13 -08002586 }
Adam Cohen19f37922012-03-21 11:59:11 -07002587 boolean isItemPlacementDirty() {
2588 return mItemPlacementDirty;
Adam Cohen482ed822012-03-02 14:15:13 -08002589 }
2590
2591 private class ItemConfiguration {
Adam Cohen8baab352012-03-20 17:39:21 -07002592 HashMap<View, CellAndSpan> map = new HashMap<View, CellAndSpan>();
Adam Cohenf3900c22012-11-16 18:28:11 -08002593 private HashMap<View, CellAndSpan> savedMap = new HashMap<View, CellAndSpan>();
2594 ArrayList<View> sortedViews = new ArrayList<View>();
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002595 ArrayList<View> intersectingViews;
Adam Cohen482ed822012-03-02 14:15:13 -08002596 boolean isSolution = false;
2597 int dragViewX, dragViewY, dragViewSpanX, dragViewSpanY;
2598
Adam Cohenf3900c22012-11-16 18:28:11 -08002599 void save() {
2600 // Copy current state into savedMap
2601 for (View v: map.keySet()) {
2602 map.get(v).copy(savedMap.get(v));
2603 }
2604 }
2605
2606 void restore() {
2607 // Restore current state from savedMap
2608 for (View v: savedMap.keySet()) {
2609 savedMap.get(v).copy(map.get(v));
2610 }
2611 }
2612
2613 void add(View v, CellAndSpan cs) {
2614 map.put(v, cs);
2615 savedMap.put(v, new CellAndSpan());
2616 sortedViews.add(v);
2617 }
2618
Adam Cohen482ed822012-03-02 14:15:13 -08002619 int area() {
2620 return dragViewSpanX * dragViewSpanY;
2621 }
Adam Cohen8baab352012-03-20 17:39:21 -07002622 }
2623
2624 private class CellAndSpan {
2625 int x, y;
2626 int spanX, spanY;
2627
Adam Cohenf3900c22012-11-16 18:28:11 -08002628 public CellAndSpan() {
2629 }
2630
2631 public void copy(CellAndSpan copy) {
2632 copy.x = x;
2633 copy.y = y;
2634 copy.spanX = spanX;
2635 copy.spanY = spanY;
2636 }
2637
Adam Cohen8baab352012-03-20 17:39:21 -07002638 public CellAndSpan(int x, int y, int spanX, int spanY) {
2639 this.x = x;
2640 this.y = y;
2641 this.spanX = spanX;
2642 this.spanY = spanY;
Adam Cohen482ed822012-03-02 14:15:13 -08002643 }
Adam Cohenf3900c22012-11-16 18:28:11 -08002644
2645 public String toString() {
2646 return "(" + x + ", " + y + ": " + spanX + ", " + spanY + ")";
2647 }
2648
Adam Cohen482ed822012-03-02 14:15:13 -08002649 }
2650
Adam Cohendf035382011-04-11 17:22:04 -07002651 /**
2652 * Find a vacant area that will fit the given bounds nearest the requested
2653 * cell location. Uses Euclidean distance to score multiple vacant areas.
2654 *
2655 * @param pixelX The X location at which you want to search for a vacant area.
2656 * @param pixelY The Y location at which you want to search for a vacant area.
2657 * @param spanX Horizontal span of the object.
2658 * @param spanY Vertical span of the object.
2659 * @param ignoreView Considers space occupied by this view as unoccupied
2660 * @param result Previously returned value to possibly recycle.
2661 * @return The X, Y cell of a vacant area that can contain this object,
2662 * nearest the requested location.
2663 */
2664 int[] findNearestVacantArea(
2665 int pixelX, int pixelY, int spanX, int spanY, View ignoreView, int[] result) {
2666 return findNearestArea(pixelX, pixelY, spanX, spanY, ignoreView, true, result);
2667 }
2668
2669 /**
Adam Cohend41fbf52012-02-16 23:53:59 -08002670 * Find a vacant area that will fit the given bounds nearest the requested
2671 * cell location. Uses Euclidean distance to score multiple vacant areas.
2672 *
2673 * @param pixelX The X location at which you want to search for a vacant area.
2674 * @param pixelY The Y location at which you want to search for a vacant area.
2675 * @param minSpanX The minimum horizontal span required
2676 * @param minSpanY The minimum vertical span required
2677 * @param spanX Horizontal span of the object.
2678 * @param spanY Vertical span of the object.
2679 * @param ignoreView Considers space occupied by this view as unoccupied
2680 * @param result Previously returned value to possibly recycle.
2681 * @return The X, Y cell of a vacant area that can contain this object,
2682 * nearest the requested location.
2683 */
2684 int[] findNearestVacantArea(int pixelX, int pixelY, int minSpanX, int minSpanY,
2685 int spanX, int spanY, View ignoreView, int[] result, int[] resultSpan) {
Adam Cohen482ed822012-03-02 14:15:13 -08002686 return findNearestArea(pixelX, pixelY, minSpanX, minSpanY, spanX, spanY, ignoreView, true,
2687 result, resultSpan, mOccupied);
Adam Cohend41fbf52012-02-16 23:53:59 -08002688 }
2689
2690 /**
Adam Cohendf035382011-04-11 17:22:04 -07002691 * Find a starting cell position that will fit the given bounds nearest the requested
2692 * cell location. Uses Euclidean distance to score multiple vacant areas.
2693 *
2694 * @param pixelX The X location at which you want to search for a vacant area.
2695 * @param pixelY The Y location at which you want to search for a vacant area.
2696 * @param spanX Horizontal span of the object.
2697 * @param spanY Vertical span of the object.
2698 * @param ignoreView Considers space occupied by this view as unoccupied
2699 * @param result Previously returned value to possibly recycle.
2700 * @return The X, Y cell of a vacant area that can contain this object,
2701 * nearest the requested location.
2702 */
2703 int[] findNearestArea(
2704 int pixelX, int pixelY, int spanX, int spanY, int[] result) {
2705 return findNearestArea(pixelX, pixelY, spanX, spanY, null, false, result);
2706 }
2707
Michael Jurka0280c3b2010-09-17 15:00:07 -07002708 boolean existsEmptyCell() {
2709 return findCellForSpan(null, 1, 1);
2710 }
2711
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002712 /**
Michael Jurka0280c3b2010-09-17 15:00:07 -07002713 * Finds the upper-left coordinate of the first rectangle in the grid that can
2714 * hold a cell of the specified dimensions. If intersectX and intersectY are not -1,
2715 * then this method will only return coordinates for rectangles that contain the cell
2716 * (intersectX, intersectY)
2717 *
2718 * @param cellXY The array that will contain the position of a vacant cell if such a cell
2719 * can be found.
2720 * @param spanX The horizontal span of the cell we want to find.
2721 * @param spanY The vertical span of the cell we want to find.
2722 *
2723 * @return True if a vacant cell of the specified dimension was found, false otherwise.
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07002724 */
Michael Jurka0280c3b2010-09-17 15:00:07 -07002725 boolean findCellForSpan(int[] cellXY, int spanX, int spanY) {
Adam Cohen482ed822012-03-02 14:15:13 -08002726 return findCellForSpanThatIntersectsIgnoring(cellXY, spanX, spanY, -1, -1, null, mOccupied);
Michael Jurka0280c3b2010-09-17 15:00:07 -07002727 }
2728
2729 /**
2730 * Like above, but ignores any cells occupied by the item "ignoreView"
2731 *
2732 * @param cellXY The array that will contain the position of a vacant cell if such a cell
2733 * can be found.
2734 * @param spanX The horizontal span of the cell we want to find.
2735 * @param spanY The vertical span of the cell we want to find.
2736 * @param ignoreView The home screen item we should treat as not occupying any space
2737 * @return
2738 */
2739 boolean findCellForSpanIgnoring(int[] cellXY, int spanX, int spanY, View ignoreView) {
Adam Cohen482ed822012-03-02 14:15:13 -08002740 return findCellForSpanThatIntersectsIgnoring(cellXY, spanX, spanY, -1, -1,
2741 ignoreView, mOccupied);
Michael Jurka0280c3b2010-09-17 15:00:07 -07002742 }
2743
2744 /**
2745 * Like above, but if intersectX and intersectY are not -1, then this method will try to
2746 * return coordinates for rectangles that contain the cell [intersectX, intersectY]
2747 *
2748 * @param spanX The horizontal span of the cell we want to find.
2749 * @param spanY The vertical span of the cell we want to find.
2750 * @param ignoreView The home screen item we should treat as not occupying any space
2751 * @param intersectX The X coordinate of the cell that we should try to overlap
2752 * @param intersectX The Y coordinate of the cell that we should try to overlap
2753 *
2754 * @return True if a vacant cell of the specified dimension was found, false otherwise.
2755 */
2756 boolean findCellForSpanThatIntersects(int[] cellXY, int spanX, int spanY,
2757 int intersectX, int intersectY) {
2758 return findCellForSpanThatIntersectsIgnoring(
Adam Cohen482ed822012-03-02 14:15:13 -08002759 cellXY, spanX, spanY, intersectX, intersectY, null, mOccupied);
Michael Jurka0280c3b2010-09-17 15:00:07 -07002760 }
2761
2762 /**
2763 * The superset of the above two methods
2764 */
2765 boolean findCellForSpanThatIntersectsIgnoring(int[] cellXY, int spanX, int spanY,
Adam Cohen482ed822012-03-02 14:15:13 -08002766 int intersectX, int intersectY, View ignoreView, boolean occupied[][]) {
Michael Jurkac6ee42e2010-09-30 12:04:50 -07002767 // mark space take by ignoreView as available (method checks if ignoreView is null)
Adam Cohen482ed822012-03-02 14:15:13 -08002768 markCellsAsUnoccupiedForView(ignoreView, occupied);
Michael Jurka0280c3b2010-09-17 15:00:07 -07002769
Michael Jurka28750fb2010-09-24 17:43:49 -07002770 boolean foundCell = false;
Michael Jurka0280c3b2010-09-17 15:00:07 -07002771 while (true) {
2772 int startX = 0;
2773 if (intersectX >= 0) {
2774 startX = Math.max(startX, intersectX - (spanX - 1));
2775 }
2776 int endX = mCountX - (spanX - 1);
2777 if (intersectX >= 0) {
2778 endX = Math.min(endX, intersectX + (spanX - 1) + (spanX == 1 ? 1 : 0));
2779 }
2780 int startY = 0;
2781 if (intersectY >= 0) {
2782 startY = Math.max(startY, intersectY - (spanY - 1));
2783 }
2784 int endY = mCountY - (spanY - 1);
2785 if (intersectY >= 0) {
2786 endY = Math.min(endY, intersectY + (spanY - 1) + (spanY == 1 ? 1 : 0));
2787 }
2788
Winson Chungbbc60d82010-11-11 16:34:41 -08002789 for (int y = startY; y < endY && !foundCell; y++) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002790 inner:
Winson Chungbbc60d82010-11-11 16:34:41 -08002791 for (int x = startX; x < endX; x++) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002792 for (int i = 0; i < spanX; i++) {
2793 for (int j = 0; j < spanY; j++) {
Adam Cohen482ed822012-03-02 14:15:13 -08002794 if (occupied[x + i][y + j]) {
Winson Chungbbc60d82010-11-11 16:34:41 -08002795 // small optimization: we can skip to after the column we just found
Michael Jurka0280c3b2010-09-17 15:00:07 -07002796 // an occupied cell
Winson Chungbbc60d82010-11-11 16:34:41 -08002797 x += i;
Michael Jurka0280c3b2010-09-17 15:00:07 -07002798 continue inner;
2799 }
2800 }
2801 }
2802 if (cellXY != null) {
2803 cellXY[0] = x;
2804 cellXY[1] = y;
2805 }
Michael Jurka28750fb2010-09-24 17:43:49 -07002806 foundCell = true;
2807 break;
Michael Jurka0280c3b2010-09-17 15:00:07 -07002808 }
2809 }
2810 if (intersectX == -1 && intersectY == -1) {
2811 break;
2812 } else {
2813 // if we failed to find anything, try again but without any requirements of
2814 // intersecting
2815 intersectX = -1;
2816 intersectY = -1;
2817 continue;
2818 }
2819 }
2820
Michael Jurkac6ee42e2010-09-30 12:04:50 -07002821 // re-mark space taken by ignoreView as occupied
Adam Cohen482ed822012-03-02 14:15:13 -08002822 markCellsAsOccupiedForView(ignoreView, occupied);
Michael Jurka28750fb2010-09-24 17:43:49 -07002823 return foundCell;
Michael Jurka0280c3b2010-09-17 15:00:07 -07002824 }
2825
2826 /**
Winson Chungc07918d2011-07-01 15:35:26 -07002827 * A drag event has begun over this layout.
2828 * It may have begun over this layout (in which case onDragChild is called first),
2829 * or it may have begun on another layout.
2830 */
2831 void onDragEnter() {
Adam Cohenc6cc61d2012-04-04 12:47:08 -07002832 mDragEnforcer.onDragEnter();
Winson Chungc07918d2011-07-01 15:35:26 -07002833 mDragging = true;
2834 }
2835
2836 /**
Michael Jurka0280c3b2010-09-17 15:00:07 -07002837 * Called when drag has left this CellLayout or has been completed (successfully or not)
2838 */
2839 void onDragExit() {
Adam Cohenc6cc61d2012-04-04 12:47:08 -07002840 mDragEnforcer.onDragExit();
Joe Onorato4be866d2010-10-10 11:26:02 -07002841 // This can actually be called when we aren't in a drag, e.g. when adding a new
2842 // item to this layout via the customize drawer.
2843 // Guard against that case.
2844 if (mDragging) {
2845 mDragging = false;
Patrick Dubroyde7658b2010-09-27 11:15:43 -07002846 }
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -07002847
2848 // Invalidate the drag data
Adam Cohend41fbf52012-02-16 23:53:59 -08002849 mDragCell[0] = mDragCell[1] = -1;
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -07002850 mDragOutlineAnims[mDragOutlineCurrent].animateOut();
2851 mDragOutlineCurrent = (mDragOutlineCurrent + 1) % mDragOutlineAnims.length;
Adam Cohen19f37922012-03-21 11:59:11 -07002852 revertTempState();
Michael Jurka33945b22010-12-21 18:19:38 -08002853 setIsDragOverlapping(false);
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07002854 }
2855
2856 /**
Winson Chungaafa03c2010-06-11 17:34:16 -07002857 * Mark a child as having been dropped.
Patrick Dubroyde7658b2010-09-27 11:15:43 -07002858 * At the beginning of the drag operation, the child may have been on another
Patrick Dubroyce34a972010-10-19 10:34:32 -07002859 * screen, but it is re-parented before this method is called.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002860 *
2861 * @param child The child that is being dropped
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002862 */
Adam Cohen716b51e2011-06-30 12:09:54 -07002863 void onDropChild(View child) {
Romain Guyd94533d2009-08-17 10:01:15 -07002864 if (child != null) {
2865 LayoutParams lp = (LayoutParams) child.getLayoutParams();
Romain Guy84f296c2009-11-04 15:00:44 -08002866 lp.dropped = true;
Romain Guyd94533d2009-08-17 10:01:15 -07002867 child.requestLayout();
Romain Guyd94533d2009-08-17 10:01:15 -07002868 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002869 }
2870
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002871 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002872 * Computes a bounding rectangle for a range of cells
Winson Chungaafa03c2010-06-11 17:34:16 -07002873 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002874 * @param cellX X coordinate of upper left corner expressed as a cell position
2875 * @param cellY Y coordinate of upper left corner expressed as a cell position
Winson Chungaafa03c2010-06-11 17:34:16 -07002876 * @param cellHSpan Width in cells
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002877 * @param cellVSpan Height in cells
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07002878 * @param resultRect Rect into which to put the results
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002879 */
Adam Cohend41fbf52012-02-16 23:53:59 -08002880 public void cellToRect(int cellX, int cellY, int cellHSpan, int cellVSpan, Rect resultRect) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002881 final int cellWidth = mCellWidth;
2882 final int cellHeight = mCellHeight;
2883 final int widthGap = mWidthGap;
2884 final int heightGap = mHeightGap;
Winson Chungaafa03c2010-06-11 17:34:16 -07002885
Winson Chung4b825dcd2011-06-19 12:41:22 -07002886 final int hStartPadding = getPaddingLeft();
2887 final int vStartPadding = getPaddingTop();
Winson Chungaafa03c2010-06-11 17:34:16 -07002888
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002889 int width = cellHSpan * cellWidth + ((cellHSpan - 1) * widthGap);
2890 int height = cellVSpan * cellHeight + ((cellVSpan - 1) * heightGap);
2891
2892 int x = hStartPadding + cellX * (cellWidth + widthGap);
2893 int y = vStartPadding + cellY * (cellHeight + heightGap);
Winson Chungaafa03c2010-06-11 17:34:16 -07002894
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07002895 resultRect.set(x, y, x + width, y + height);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002896 }
Winson Chungaafa03c2010-06-11 17:34:16 -07002897
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002898 /**
Winson Chungaafa03c2010-06-11 17:34:16 -07002899 * Computes the required horizontal and vertical cell spans to always
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002900 * fit the given rectangle.
Winson Chungaafa03c2010-06-11 17:34:16 -07002901 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002902 * @param width Width in pixels
2903 * @param height Height in pixels
Patrick Dubroy8f86ddc2010-07-16 13:55:32 -07002904 * @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 -08002905 */
Winson Chung66700732013-08-20 16:56:15 -07002906 public static int[] rectToCell(int width, int height, int[] result) {
Winson Chung5f8afe62013-08-12 16:19:28 -07002907 LauncherAppState app = LauncherAppState.getInstance();
2908 DeviceProfile grid = app.getDynamicGrid().getDeviceProfile();
Winson Chung66700732013-08-20 16:56:15 -07002909 Rect padding = grid.getWorkspacePadding(grid.isLandscape ?
2910 CellLayout.LANDSCAPE : CellLayout.PORTRAIT);
Winson Chung5f8afe62013-08-12 16:19:28 -07002911
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002912 // Always assume we're working with the smallest span to make sure we
2913 // reserve enough space in both orientations.
Winson Chung66700732013-08-20 16:56:15 -07002914 int parentWidth = grid.calculateCellWidth(grid.widthPx
2915 - padding.left - padding.right, (int) grid.numColumns);
2916 int parentHeight = grid.calculateCellHeight(grid.heightPx
2917 - padding.top - padding.bottom, (int) grid.numRows);
2918 int smallerSize = Math.min(parentWidth, parentHeight);
Joe Onorato79e56262009-09-21 15:23:04 -04002919
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002920 // Always round up to next largest cell
Winson Chung54c725c2011-08-03 12:03:40 -07002921 int spanX = (int) Math.ceil(width / (float) smallerSize);
2922 int spanY = (int) Math.ceil(height / (float) smallerSize);
Joe Onorato79e56262009-09-21 15:23:04 -04002923
Patrick Dubroy8f86ddc2010-07-16 13:55:32 -07002924 if (result == null) {
2925 return new int[] { spanX, spanY };
2926 }
2927 result[0] = spanX;
2928 result[1] = spanY;
2929 return result;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002930 }
2931
Michael Jurkaf12c75c2011-01-25 22:41:40 -08002932 public int[] cellSpansToSize(int hSpans, int vSpans) {
2933 int[] size = new int[2];
2934 size[0] = hSpans * mCellWidth + (hSpans - 1) * mWidthGap;
2935 size[1] = vSpans * mCellHeight + (vSpans - 1) * mHeightGap;
2936 return size;
2937 }
2938
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002939 /**
Patrick Dubroy047379a2010-12-19 22:02:04 -08002940 * Calculate the grid spans needed to fit given item
2941 */
2942 public void calculateSpans(ItemInfo info) {
2943 final int minWidth;
2944 final int minHeight;
2945
2946 if (info instanceof LauncherAppWidgetInfo) {
2947 minWidth = ((LauncherAppWidgetInfo) info).minWidth;
2948 minHeight = ((LauncherAppWidgetInfo) info).minHeight;
2949 } else if (info instanceof PendingAddWidgetInfo) {
2950 minWidth = ((PendingAddWidgetInfo) info).minWidth;
2951 minHeight = ((PendingAddWidgetInfo) info).minHeight;
2952 } else {
2953 // It's not a widget, so it must be 1x1
2954 info.spanX = info.spanY = 1;
2955 return;
2956 }
2957 int[] spans = rectToCell(minWidth, minHeight, null);
2958 info.spanX = spans[0];
2959 info.spanY = spans[1];
2960 }
2961
2962 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002963 * Find the first vacant cell, if there is one.
2964 *
2965 * @param vacant Holds the x and y coordinate of the vacant cell
2966 * @param spanX Horizontal cell span.
2967 * @param spanY Vertical cell span.
Winson Chungaafa03c2010-06-11 17:34:16 -07002968 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002969 * @return True if a vacant cell was found
2970 */
2971 public boolean getVacantCell(int[] vacant, int spanX, int spanY) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002972
Michael Jurka0280c3b2010-09-17 15:00:07 -07002973 return findVacantCell(vacant, spanX, spanY, mCountX, mCountY, mOccupied);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002974 }
2975
2976 static boolean findVacantCell(int[] vacant, int spanX, int spanY,
2977 int xCount, int yCount, boolean[][] occupied) {
2978
Adam Cohen2801caf2011-05-13 20:57:39 -07002979 for (int y = 0; y < yCount; y++) {
2980 for (int x = 0; x < xCount; x++) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002981 boolean available = !occupied[x][y];
2982out: for (int i = x; i < x + spanX - 1 && x < xCount; i++) {
2983 for (int j = y; j < y + spanY - 1 && y < yCount; j++) {
2984 available = available && !occupied[i][j];
2985 if (!available) break out;
2986 }
2987 }
2988
2989 if (available) {
2990 vacant[0] = x;
2991 vacant[1] = y;
2992 return true;
2993 }
2994 }
2995 }
2996
2997 return false;
2998 }
2999
Michael Jurka0280c3b2010-09-17 15:00:07 -07003000 private void clearOccupiedCells() {
3001 for (int x = 0; x < mCountX; x++) {
3002 for (int y = 0; y < mCountY; y++) {
3003 mOccupied[x][y] = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003004 }
3005 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07003006 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003007
Adam Cohend41fbf52012-02-16 23:53:59 -08003008 public void onMove(View view, int newCellX, int newCellY, int newSpanX, int newSpanY) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07003009 markCellsAsUnoccupiedForView(view);
Adam Cohen482ed822012-03-02 14:15:13 -08003010 markCellsForView(newCellX, newCellY, newSpanX, newSpanY, mOccupied, true);
Michael Jurka0280c3b2010-09-17 15:00:07 -07003011 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003012
Adam Cohend4844c32011-02-18 19:25:06 -08003013 public void markCellsAsOccupiedForView(View view) {
Adam Cohen482ed822012-03-02 14:15:13 -08003014 markCellsAsOccupiedForView(view, mOccupied);
3015 }
3016 public void markCellsAsOccupiedForView(View view, boolean[][] occupied) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07003017 if (view == null || view.getParent() != mShortcutsAndWidgets) return;
Michael Jurka0280c3b2010-09-17 15:00:07 -07003018 LayoutParams lp = (LayoutParams) view.getLayoutParams();
Adam Cohen482ed822012-03-02 14:15:13 -08003019 markCellsForView(lp.cellX, lp.cellY, lp.cellHSpan, lp.cellVSpan, occupied, true);
Michael Jurka0280c3b2010-09-17 15:00:07 -07003020 }
3021
Adam Cohend4844c32011-02-18 19:25:06 -08003022 public void markCellsAsUnoccupiedForView(View view) {
Adam Cohen482ed822012-03-02 14:15:13 -08003023 markCellsAsUnoccupiedForView(view, mOccupied);
3024 }
3025 public void markCellsAsUnoccupiedForView(View view, boolean occupied[][]) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07003026 if (view == null || view.getParent() != mShortcutsAndWidgets) return;
Michael Jurka0280c3b2010-09-17 15:00:07 -07003027 LayoutParams lp = (LayoutParams) view.getLayoutParams();
Adam Cohen482ed822012-03-02 14:15:13 -08003028 markCellsForView(lp.cellX, lp.cellY, lp.cellHSpan, lp.cellVSpan, occupied, false);
Michael Jurka0280c3b2010-09-17 15:00:07 -07003029 }
3030
Adam Cohen482ed822012-03-02 14:15:13 -08003031 private void markCellsForView(int cellX, int cellY, int spanX, int spanY, boolean[][] occupied,
3032 boolean value) {
3033 if (cellX < 0 || cellY < 0) return;
Michael Jurka0280c3b2010-09-17 15:00:07 -07003034 for (int x = cellX; x < cellX + spanX && x < mCountX; x++) {
3035 for (int y = cellY; y < cellY + spanY && y < mCountY; y++) {
Adam Cohen482ed822012-03-02 14:15:13 -08003036 occupied[x][y] = value;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003037 }
3038 }
3039 }
3040
Adam Cohen2801caf2011-05-13 20:57:39 -07003041 public int getDesiredWidth() {
Michael Jurka8b805b12012-04-18 14:23:14 -07003042 return getPaddingLeft() + getPaddingRight() + (mCountX * mCellWidth) +
Adam Cohen2801caf2011-05-13 20:57:39 -07003043 (Math.max((mCountX - 1), 0) * mWidthGap);
3044 }
3045
3046 public int getDesiredHeight() {
Michael Jurka8b805b12012-04-18 14:23:14 -07003047 return getPaddingTop() + getPaddingBottom() + (mCountY * mCellHeight) +
Adam Cohen2801caf2011-05-13 20:57:39 -07003048 (Math.max((mCountY - 1), 0) * mHeightGap);
3049 }
3050
Michael Jurka66d72172011-04-12 16:29:25 -07003051 public boolean isOccupied(int x, int y) {
3052 if (x < mCountX && y < mCountY) {
3053 return mOccupied[x][y];
3054 } else {
3055 throw new RuntimeException("Position exceeds the bound of this CellLayout");
3056 }
3057 }
3058
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003059 @Override
3060 public ViewGroup.LayoutParams generateLayoutParams(AttributeSet attrs) {
3061 return new CellLayout.LayoutParams(getContext(), attrs);
3062 }
3063
3064 @Override
3065 protected boolean checkLayoutParams(ViewGroup.LayoutParams p) {
3066 return p instanceof CellLayout.LayoutParams;
3067 }
3068
3069 @Override
3070 protected ViewGroup.LayoutParams generateLayoutParams(ViewGroup.LayoutParams p) {
3071 return new CellLayout.LayoutParams(p);
3072 }
3073
Winson Chungaafa03c2010-06-11 17:34:16 -07003074 public static class CellLayoutAnimationController extends LayoutAnimationController {
3075 public CellLayoutAnimationController(Animation animation, float delay) {
3076 super(animation, delay);
3077 }
3078
3079 @Override
3080 protected long getDelayForView(View view) {
3081 return (int) (Math.random() * 150);
3082 }
3083 }
3084
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003085 public static class LayoutParams extends ViewGroup.MarginLayoutParams {
3086 /**
3087 * Horizontal location of the item in the grid.
3088 */
3089 @ViewDebug.ExportedProperty
3090 public int cellX;
3091
3092 /**
3093 * Vertical location of the item in the grid.
3094 */
3095 @ViewDebug.ExportedProperty
3096 public int cellY;
3097
3098 /**
Adam Cohen482ed822012-03-02 14:15:13 -08003099 * Temporary horizontal location of the item in the grid during reorder
3100 */
3101 public int tmpCellX;
3102
3103 /**
3104 * Temporary vertical location of the item in the grid during reorder
3105 */
3106 public int tmpCellY;
3107
3108 /**
3109 * Indicates that the temporary coordinates should be used to layout the items
3110 */
3111 public boolean useTmpCoords;
3112
3113 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003114 * Number of cells spanned horizontally by the item.
3115 */
3116 @ViewDebug.ExportedProperty
3117 public int cellHSpan;
3118
3119 /**
3120 * Number of cells spanned vertically by the item.
3121 */
3122 @ViewDebug.ExportedProperty
3123 public int cellVSpan;
Winson Chungaafa03c2010-06-11 17:34:16 -07003124
Adam Cohen1b607ed2011-03-03 17:26:50 -08003125 /**
3126 * Indicates whether the item will set its x, y, width and height parameters freely,
3127 * or whether these will be computed based on cellX, cellY, cellHSpan and cellVSpan.
3128 */
Adam Cohend4844c32011-02-18 19:25:06 -08003129 public boolean isLockedToGrid = true;
3130
Adam Cohen482ed822012-03-02 14:15:13 -08003131 /**
Adam Cohenec40b2b2013-07-23 15:52:40 -07003132 * Indicates that this item should use the full extents of its parent.
3133 */
3134 public boolean isFullscreen = false;
3135
3136 /**
Adam Cohen482ed822012-03-02 14:15:13 -08003137 * Indicates whether this item can be reordered. Always true except in the case of the
3138 * the AllApps button.
3139 */
3140 public boolean canReorder = true;
3141
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003142 // X coordinate of the view in the layout.
3143 @ViewDebug.ExportedProperty
3144 int x;
3145 // Y coordinate of the view in the layout.
3146 @ViewDebug.ExportedProperty
3147 int y;
3148
Romain Guy84f296c2009-11-04 15:00:44 -08003149 boolean dropped;
Romain Guyfcb9e712009-10-02 16:06:52 -07003150
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003151 public LayoutParams(Context c, AttributeSet attrs) {
3152 super(c, attrs);
3153 cellHSpan = 1;
3154 cellVSpan = 1;
3155 }
3156
3157 public LayoutParams(ViewGroup.LayoutParams source) {
3158 super(source);
3159 cellHSpan = 1;
3160 cellVSpan = 1;
3161 }
Winson Chungaafa03c2010-06-11 17:34:16 -07003162
3163 public LayoutParams(LayoutParams source) {
3164 super(source);
3165 this.cellX = source.cellX;
3166 this.cellY = source.cellY;
3167 this.cellHSpan = source.cellHSpan;
3168 this.cellVSpan = source.cellVSpan;
3169 }
3170
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003171 public LayoutParams(int cellX, int cellY, int cellHSpan, int cellVSpan) {
Romain Guy8f19cdd2010-01-08 15:07:00 -08003172 super(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003173 this.cellX = cellX;
3174 this.cellY = cellY;
3175 this.cellHSpan = cellHSpan;
3176 this.cellVSpan = cellVSpan;
3177 }
3178
Adam Cohen2374abf2013-04-16 14:56:57 -07003179 public void setup(int cellWidth, int cellHeight, int widthGap, int heightGap,
3180 boolean invertHorizontally, int colCount) {
Adam Cohend4844c32011-02-18 19:25:06 -08003181 if (isLockedToGrid) {
3182 final int myCellHSpan = cellHSpan;
3183 final int myCellVSpan = cellVSpan;
Adam Cohen2374abf2013-04-16 14:56:57 -07003184 int myCellX = useTmpCoords ? tmpCellX : cellX;
3185 int myCellY = useTmpCoords ? tmpCellY : cellY;
3186
3187 if (invertHorizontally) {
3188 myCellX = colCount - myCellX - cellHSpan;
3189 }
Adam Cohen1b607ed2011-03-03 17:26:50 -08003190
Adam Cohend4844c32011-02-18 19:25:06 -08003191 width = myCellHSpan * cellWidth + ((myCellHSpan - 1) * widthGap) -
3192 leftMargin - rightMargin;
3193 height = myCellVSpan * cellHeight + ((myCellVSpan - 1) * heightGap) -
3194 topMargin - bottomMargin;
Winson Chungeecf02d2012-03-02 17:14:58 -08003195 x = (int) (myCellX * (cellWidth + widthGap) + leftMargin);
3196 y = (int) (myCellY * (cellHeight + heightGap) + topMargin);
Adam Cohend4844c32011-02-18 19:25:06 -08003197 }
3198 }
Winson Chungaafa03c2010-06-11 17:34:16 -07003199
Winson Chungaafa03c2010-06-11 17:34:16 -07003200 public String toString() {
3201 return "(" + this.cellX + ", " + this.cellY + ")";
3202 }
Adam Cohen7f4eabe2011-04-21 16:19:16 -07003203
3204 public void setWidth(int width) {
3205 this.width = width;
3206 }
3207
3208 public int getWidth() {
3209 return width;
3210 }
3211
3212 public void setHeight(int height) {
3213 this.height = height;
3214 }
3215
3216 public int getHeight() {
3217 return height;
3218 }
3219
3220 public void setX(int x) {
3221 this.x = x;
3222 }
3223
3224 public int getX() {
3225 return x;
3226 }
3227
3228 public void setY(int y) {
3229 this.y = y;
3230 }
3231
3232 public int getY() {
3233 return y;
3234 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003235 }
3236
Michael Jurka0280c3b2010-09-17 15:00:07 -07003237 // This class stores info for two purposes:
3238 // 1. When dragging items (mDragInfo in Workspace), we store the View, its cellX & cellY,
3239 // its spanX, spanY, and the screen it is on
3240 // 2. When long clicking on an empty cell in a CellLayout, we save information about the
3241 // cellX and cellY coordinates and which page was clicked. We then set this as a tag on
3242 // the CellLayout that was long clicked
Michael Jurkae5fb0f22011-04-11 13:27:46 -07003243 static final class CellInfo {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003244 View cell;
Michael Jurkaa63c4522010-08-19 13:52:27 -07003245 int cellX = -1;
3246 int cellY = -1;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003247 int spanX;
3248 int spanY;
Adam Cohendcd297f2013-06-18 13:13:40 -07003249 long screenId;
Winson Chung3d503fb2011-07-13 17:25:49 -07003250 long container;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003251
Adam Cohene0aaa0d2014-05-12 12:44:22 -07003252 CellInfo(View v, ItemInfo info) {
3253 cell = v;
3254 cellX = info.cellX;
3255 cellY = info.cellY;
3256 spanX = info.spanX;
3257 spanY = info.spanY;
3258 screenId = info.screenId;
3259 container = info.container;
3260 }
3261
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003262 @Override
3263 public String toString() {
Winson Chungaafa03c2010-06-11 17:34:16 -07003264 return "Cell[view=" + (cell == null ? "null" : cell.getClass())
3265 + ", x=" + cellX + ", y=" + cellY + "]";
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003266 }
3267 }
Michael Jurkad771c962011-08-09 15:00:48 -07003268
3269 public boolean lastDownOnOccupiedCell() {
3270 return mLastDownOnOccupiedCell;
3271 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003272}