blob: abc057c8d91fadb393acddf1f0e341011b7bf5b4 [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;
Adam Cohenb5ba0972011-09-07 18:02:31 -070033import android.graphics.PorterDuff;
34import android.graphics.PorterDuffXfermode;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080035import android.graphics.Rect;
Adam Cohen482ed822012-03-02 14:15:13 -080036import android.graphics.drawable.ColorDrawable;
Patrick Dubroy6569f2c2010-07-12 14:25:18 -070037import android.graphics.drawable.Drawable;
Adam Cohenb5ba0972011-09-07 18:02:31 -070038import android.graphics.drawable.NinePatchDrawable;
Adam Cohen1462de32012-07-24 22:34:36 -070039import android.os.Parcelable;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080040import android.util.AttributeSet;
Joe Onorato4be866d2010-10-10 11:26:02 -070041import android.util.Log;
Adam Cohen1462de32012-07-24 22:34:36 -070042import android.util.SparseArray;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080043import android.view.MotionEvent;
44import android.view.View;
45import android.view.ViewDebug;
46import android.view.ViewGroup;
Winson Chungaafa03c2010-06-11 17:34:16 -070047import android.view.animation.Animation;
Winson Chung150fbab2010-09-29 17:14:26 -070048import android.view.animation.DecelerateInterpolator;
Winson Chungaafa03c2010-06-11 17:34:16 -070049import android.view.animation.LayoutAnimationController;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080050
Daniel Sandler325dc232013-06-05 22:57:57 -040051import com.android.launcher3.R;
52import com.android.launcher3.FolderIcon.FolderRingAnimator;
Patrick Dubroy8e58e912010-10-14 13:21:48 -070053
Adam Cohen69ce2e52011-07-03 19:25:21 -070054import java.util.ArrayList;
Adam Cohenc0dcf592011-06-01 15:30:43 -070055import java.util.Arrays;
Adam Cohenf3900c22012-11-16 18:28:11 -080056import java.util.Collections;
57import java.util.Comparator;
Adam Cohenbfbfd262011-06-13 16:55:12 -070058import java.util.HashMap;
Adam Cohend41fbf52012-02-16 23:53:59 -080059import java.util.Stack;
Adam Cohenc0dcf592011-06-01 15:30:43 -070060
Michael Jurkabdb5c532011-02-01 15:05:06 -080061public class CellLayout extends ViewGroup {
Winson Chungaafa03c2010-06-11 17:34:16 -070062 static final String TAG = "CellLayout";
63
Adam Cohen2acce882012-03-28 19:03:19 -070064 private Launcher mLauncher;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080065 private int mCellWidth;
66 private int mCellHeight;
Winson Chungaafa03c2010-06-11 17:34:16 -070067
Adam Cohend22015c2010-07-26 22:02:18 -070068 private int mCountX;
69 private int mCountY;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080070
Adam Cohen234c4cd2011-07-17 21:03:04 -070071 private int mOriginalWidthGap;
72 private int mOriginalHeightGap;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080073 private int mWidthGap;
74 private int mHeightGap;
Winson Chung4b825dcd2011-06-19 12:41:22 -070075 private int mMaxGap;
Adam Cohenebea84d2011-11-09 17:20:41 -080076 private boolean mScrollingTransformsDirty = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080077
78 private final Rect mRect = new Rect();
79 private final CellInfo mCellInfo = new CellInfo();
Winson Chungaafa03c2010-06-11 17:34:16 -070080
Patrick Dubroyde7658b2010-09-27 11:15:43 -070081 // These are temporary variables to prevent having to allocate a new object just to
82 // return an (x, y) value from helper functions. Do NOT use them to maintain other state.
Winson Chung0be025d2011-05-23 17:45:09 -070083 private final int[] mTmpXY = new int[2];
Patrick Dubroyde7658b2010-09-27 11:15:43 -070084 private final int[] mTmpPoint = new int[2];
Adam Cohen69ce2e52011-07-03 19:25:21 -070085 int[] mTempLocation = new int[2];
Patrick Dubroy6569f2c2010-07-12 14:25:18 -070086
The Android Open Source Project31dd5032009-03-03 19:32:27 -080087 boolean[][] mOccupied;
Adam Cohen482ed822012-03-02 14:15:13 -080088 boolean[][] mTmpOccupied;
Michael Jurkad771c962011-08-09 15:00:48 -070089 private boolean mLastDownOnOccupiedCell = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080090
Michael Jurkadee05892010-07-27 10:01:56 -070091 private OnTouchListener mInterceptTouchListener;
92
Adam Cohen69ce2e52011-07-03 19:25:21 -070093 private ArrayList<FolderRingAnimator> mFolderOuterRings = new ArrayList<FolderRingAnimator>();
Adam Cohenc51934b2011-07-26 21:07:43 -070094 private int[] mFolderLeaveBehindCell = {-1, -1};
Adam Cohen69ce2e52011-07-03 19:25:21 -070095
Adam Cohenb5ba0972011-09-07 18:02:31 -070096 private int mForegroundAlpha = 0;
Michael Jurka5f1c5092010-09-03 14:15:02 -070097 private float mBackgroundAlpha;
Adam Cohen1b0aaac2010-10-28 11:11:18 -070098 private float mBackgroundAlphaMultiplier = 1.0f;
Adam Cohenf34bab52010-09-30 14:11:56 -070099
Michael Jurka33945b22010-12-21 18:19:38 -0800100 private Drawable mNormalBackground;
Michael Jurka33945b22010-12-21 18:19:38 -0800101 private Drawable mActiveGlowBackground;
Adam Cohenb5ba0972011-09-07 18:02:31 -0700102 private Drawable mOverScrollForegroundDrawable;
103 private Drawable mOverScrollLeft;
104 private Drawable mOverScrollRight;
Michael Jurka18014792010-10-14 09:01:34 -0700105 private Rect mBackgroundRect;
Adam Cohenb5ba0972011-09-07 18:02:31 -0700106 private Rect mForegroundRect;
Adam Cohenb5ba0972011-09-07 18:02:31 -0700107 private int mForegroundPadding;
Patrick Dubroy1262e362010-10-06 15:49:50 -0700108
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700109 // These values allow a fixed measurement to be set on the CellLayout.
110 private int mFixedWidth = -1;
111 private int mFixedHeight = -1;
112
Michael Jurka33945b22010-12-21 18:19:38 -0800113 // If we're actively dragging something over this screen, mIsDragOverlapping is true
114 private boolean mIsDragOverlapping = false;
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700115 private final Point mDragCenter = new Point();
Adam Cohendedbd962013-07-11 14:21:49 -0700116 boolean mUseActiveGlowBackground = false;
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700117
Winson Chung150fbab2010-09-29 17:14:26 -0700118 // These arrays are used to implement the drag visualization on x-large screens.
Joe Onorato4be866d2010-10-10 11:26:02 -0700119 // They are used as circular arrays, indexed by mDragOutlineCurrent.
Adam Cohend41fbf52012-02-16 23:53:59 -0800120 private Rect[] mDragOutlines = new Rect[4];
Chet Haase472b2812010-10-14 07:02:04 -0700121 private float[] mDragOutlineAlphas = new float[mDragOutlines.length];
Joe Onorato4be866d2010-10-10 11:26:02 -0700122 private InterruptibleInOutAnimator[] mDragOutlineAnims =
123 new InterruptibleInOutAnimator[mDragOutlines.length];
Winson Chung150fbab2010-09-29 17:14:26 -0700124
125 // Used as an index into the above 3 arrays; indicates which is the most current value.
Joe Onorato4be866d2010-10-10 11:26:02 -0700126 private int mDragOutlineCurrent = 0;
Patrick Dubroy8e58e912010-10-14 13:21:48 -0700127 private final Paint mDragOutlinePaint = new Paint();
Winson Chung150fbab2010-09-29 17:14:26 -0700128
Patrick Dubroy96864c32011-03-10 17:17:23 -0800129 private BubbleTextView mPressedOrFocusedIcon;
130
Adam Cohen482ed822012-03-02 14:15:13 -0800131 private HashMap<CellLayout.LayoutParams, Animator> mReorderAnimators = new
132 HashMap<CellLayout.LayoutParams, Animator>();
Adam Cohen19f37922012-03-21 11:59:11 -0700133 private HashMap<View, ReorderHintAnimation>
134 mShakeAnimators = new HashMap<View, ReorderHintAnimation>();
135
136 private boolean mItemPlacementDirty = false;
Adam Cohenbfbfd262011-06-13 16:55:12 -0700137
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700138 // When a drag operation is in progress, holds the nearest cell to the touch point
139 private final int[] mDragCell = new int[2];
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800140
Joe Onorato4be866d2010-10-10 11:26:02 -0700141 private boolean mDragging = false;
142
Patrick Dubroyce34a972010-10-19 10:34:32 -0700143 private TimeInterpolator mEaseOutInterpolator;
Michael Jurkaa52570f2012-03-20 03:18:20 -0700144 private ShortcutAndWidgetContainer mShortcutsAndWidgets;
Patrick Dubroyce34a972010-10-19 10:34:32 -0700145
Andrew Flynn0dca1ec2012-02-29 13:33:22 -0800146 private boolean mIsHotseat = false;
Adam Cohen307fe232012-08-16 17:55:58 -0700147 private float mHotseatScale = 1f;
Andrew Flynn0dca1ec2012-02-29 13:33:22 -0800148
Adam Cohen482ed822012-03-02 14:15:13 -0800149 public static final int MODE_DRAG_OVER = 0;
150 public static final int MODE_ON_DROP = 1;
151 public static final int MODE_ON_DROP_EXTERNAL = 2;
152 public static final int MODE_ACCEPT_DROP = 3;
Adam Cohen19f37922012-03-21 11:59:11 -0700153 private static final boolean DESTRUCTIVE_REORDER = false;
Adam Cohen482ed822012-03-02 14:15:13 -0800154 private static final boolean DEBUG_VISUALIZE_OCCUPIED = false;
155
Adam Cohena897f392012-04-27 18:12:05 -0700156 static final int LANDSCAPE = 0;
157 static final int PORTRAIT = 1;
158
Adam Cohen7bdfc972012-05-22 16:50:35 -0700159 private static final float REORDER_HINT_MAGNITUDE = 0.12f;
Adam Cohen19f37922012-03-21 11:59:11 -0700160 private static final int REORDER_ANIMATION_DURATION = 150;
161 private float mReorderHintAnimationMagnitude;
162
Adam Cohen482ed822012-03-02 14:15:13 -0800163 private ArrayList<View> mIntersectingViews = new ArrayList<View>();
164 private Rect mOccupiedRect = new Rect();
165 private int[] mDirectionVector = new int[2];
Adam Cohen19f37922012-03-21 11:59:11 -0700166 int[] mPreviousReorderDirection = new int[2];
Adam Cohenb209e632012-03-27 17:09:36 -0700167 private static final int INVALID_DIRECTION = -100;
Adam Cohenc6cc61d2012-04-04 12:47:08 -0700168 private DropTarget.DragEnforcer mDragEnforcer;
Adam Cohen482ed822012-03-02 14:15:13 -0800169
Romain Guy8a0bff52012-05-06 13:14:33 -0700170 private final static PorterDuffXfermode sAddBlendMode =
171 new PorterDuffXfermode(PorterDuff.Mode.ADD);
Michael Jurkaca993832012-06-29 15:17:04 -0700172 private final static Paint sPaint = new Paint();
Romain Guy8a0bff52012-05-06 13:14:33 -0700173
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800174 public CellLayout(Context context) {
175 this(context, null);
176 }
177
178 public CellLayout(Context context, AttributeSet attrs) {
179 this(context, attrs, 0);
180 }
181
182 public CellLayout(Context context, AttributeSet attrs, int defStyle) {
183 super(context, attrs, defStyle);
Michael Jurka8b805b12012-04-18 14:23:14 -0700184 mDragEnforcer = new DropTarget.DragEnforcer(context);
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700185
186 // A ViewGroup usually does not draw, but CellLayout needs to draw a rectangle to show
187 // the user where a dragged item will land when dropped.
188 setWillNotDraw(false);
Romain Guyce3cbd12013-02-25 15:00:36 -0800189 setClipToPadding(false);
Adam Cohen2acce882012-03-28 19:03:19 -0700190 mLauncher = (Launcher) context;
Michael Jurkaa63c4522010-08-19 13:52:27 -0700191
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800192 TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.CellLayout, defStyle, 0);
193
Winson Chung5f8afe62013-08-12 16:19:28 -0700194 mCellWidth = -1;
195 mCellHeight = -1;
196 mWidthGap = mOriginalWidthGap = 0;
197 mHeightGap = mOriginalHeightGap = 0;
198 mMaxGap = Integer.MAX_VALUE;
Adam Cohend22015c2010-07-26 22:02:18 -0700199 mCountX = LauncherModel.getCellCountX();
200 mCountY = LauncherModel.getCellCountY();
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 Chung5f8afe62013-08-12 16:19:28 -0700211 LauncherAppState app = LauncherAppState.getInstance();
212 DeviceProfile grid = app.getDynamicGrid().getDeviceProfile();
213 mHotseatScale = (float) grid.hotseatIconSize / grid.iconSize;
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700214
Winson Chung967289b2011-06-30 18:09:30 -0700215 mNormalBackground = res.getDrawable(R.drawable.homescreen_blue_normal_holo);
Winson Chungdea74b72011-09-13 18:06:43 -0700216 mActiveGlowBackground = res.getDrawable(R.drawable.homescreen_blue_strong_holo);
Michael Jurka33945b22010-12-21 18:19:38 -0800217
Adam Cohenb5ba0972011-09-07 18:02:31 -0700218 mOverScrollLeft = res.getDrawable(R.drawable.overscroll_glow_left);
219 mOverScrollRight = res.getDrawable(R.drawable.overscroll_glow_right);
220 mForegroundPadding =
221 res.getDimensionPixelSize(R.dimen.workspace_overscroll_drawable_padding);
Michael Jurka33945b22010-12-21 18:19:38 -0800222
Adam Cohen19f37922012-03-21 11:59:11 -0700223 mReorderHintAnimationMagnitude = (REORDER_HINT_MAGNITUDE *
Winson Chung5f8afe62013-08-12 16:19:28 -0700224 grid.iconSizePx);
Adam Cohen19f37922012-03-21 11:59:11 -0700225
Winson Chungb26f3d62011-06-02 10:49:29 -0700226 mNormalBackground.setFilterBitmap(true);
Winson Chungb26f3d62011-06-02 10:49:29 -0700227 mActiveGlowBackground.setFilterBitmap(true);
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700228
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700229 // Initialize the data structures used for the drag visualization.
Patrick Dubroyce34a972010-10-19 10:34:32 -0700230 mEaseOutInterpolator = new DecelerateInterpolator(2.5f); // Quint ease out
Winson Chungb8c69f32011-10-19 21:36:08 -0700231 mDragCell[0] = mDragCell[1] = -1;
Joe Onorato4be866d2010-10-10 11:26:02 -0700232 for (int i = 0; i < mDragOutlines.length; i++) {
Adam Cohend41fbf52012-02-16 23:53:59 -0800233 mDragOutlines[i] = new Rect(-1, -1, -1, -1);
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700234 }
235
236 // When dragging things around the home screens, we show a green outline of
237 // where the item will land. The outlines gradually fade out, leaving a trail
238 // behind the drag path.
239 // Set up all the animations that are used to implement this fading.
240 final int duration = res.getInteger(R.integer.config_dragOutlineFadeTime);
Chet Haase472b2812010-10-14 07:02:04 -0700241 final float fromAlphaValue = 0;
242 final float toAlphaValue = (float)res.getInteger(R.integer.config_dragOutlineMaxAlpha);
Joe Onorato4be866d2010-10-10 11:26:02 -0700243
Patrick Dubroy8e58e912010-10-14 13:21:48 -0700244 Arrays.fill(mDragOutlineAlphas, fromAlphaValue);
Joe Onorato4be866d2010-10-10 11:26:02 -0700245
246 for (int i = 0; i < mDragOutlineAnims.length; i++) {
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700247 final InterruptibleInOutAnimator anim =
Michael Jurkaf1ad6082013-03-13 12:55:46 +0100248 new InterruptibleInOutAnimator(this, duration, fromAlphaValue, toAlphaValue);
Patrick Dubroyce34a972010-10-19 10:34:32 -0700249 anim.getAnimator().setInterpolator(mEaseOutInterpolator);
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700250 final int thisIndex = i;
Chet Haase472b2812010-10-14 07:02:04 -0700251 anim.getAnimator().addUpdateListener(new AnimatorUpdateListener() {
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700252 public void onAnimationUpdate(ValueAnimator animation) {
Joe Onorato4be866d2010-10-10 11:26:02 -0700253 final Bitmap outline = (Bitmap)anim.getTag();
254
255 // If an animation is started and then stopped very quickly, we can still
256 // get spurious updates we've cleared the tag. Guard against this.
257 if (outline == null) {
Michael Jurka3a9fced2012-04-13 14:44:29 -0700258 @SuppressWarnings("all") // suppress dead code warning
259 final boolean debug = false;
260 if (debug) {
Patrick Dubroyfe6bd872010-10-13 17:32:10 -0700261 Object val = animation.getAnimatedValue();
262 Log.d(TAG, "anim " + thisIndex + " update: " + val +
263 ", isStopped " + anim.isStopped());
264 }
Joe Onorato4be866d2010-10-10 11:26:02 -0700265 // Try to prevent it from continuing to run
266 animation.cancel();
267 } else {
Chet Haase472b2812010-10-14 07:02:04 -0700268 mDragOutlineAlphas[thisIndex] = (Float) animation.getAnimatedValue();
Adam Cohend41fbf52012-02-16 23:53:59 -0800269 CellLayout.this.invalidate(mDragOutlines[thisIndex]);
Joe Onorato4be866d2010-10-10 11:26:02 -0700270 }
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700271 }
272 });
Joe Onorato4be866d2010-10-10 11:26:02 -0700273 // The animation holds a reference to the drag outline bitmap as long is it's
274 // running. This way the bitmap can be GCed when the animations are complete.
Chet Haase472b2812010-10-14 07:02:04 -0700275 anim.getAnimator().addListener(new AnimatorListenerAdapter() {
Michael Jurka3c4c20f2010-10-28 15:36:06 -0700276 @Override
Joe Onorato4be866d2010-10-10 11:26:02 -0700277 public void onAnimationEnd(Animator animation) {
Chet Haase472b2812010-10-14 07:02:04 -0700278 if ((Float) ((ValueAnimator) animation).getAnimatedValue() == 0f) {
Joe Onorato4be866d2010-10-10 11:26:02 -0700279 anim.setTag(null);
280 }
281 }
282 });
283 mDragOutlineAnims[i] = anim;
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700284 }
Patrick Dubroyce34a972010-10-19 10:34:32 -0700285
Michael Jurka18014792010-10-14 09:01:34 -0700286 mBackgroundRect = new Rect();
Adam Cohenb5ba0972011-09-07 18:02:31 -0700287 mForegroundRect = new Rect();
Michael Jurkabea15192010-11-17 12:33:46 -0800288
Michael Jurkaa52570f2012-03-20 03:18:20 -0700289 mShortcutsAndWidgets = new ShortcutAndWidgetContainer(context);
Adam Cohen2374abf2013-04-16 14:56:57 -0700290 mShortcutsAndWidgets.setCellDimensions(mCellWidth, mCellHeight, mWidthGap, mHeightGap,
Winson Chung5f8afe62013-08-12 16:19:28 -0700291 mCountX, mCountY);
Adam Cohen2374abf2013-04-16 14:56:57 -0700292
Michael Jurkaa52570f2012-03-20 03:18:20 -0700293 addView(mShortcutsAndWidgets);
Michael Jurka18014792010-10-14 09:01:34 -0700294 }
295
Adam Cohen2801caf2011-05-13 20:57:39 -0700296 public void enableHardwareLayers() {
Michael Jurkaca993832012-06-29 15:17:04 -0700297 mShortcutsAndWidgets.setLayerType(LAYER_TYPE_HARDWARE, sPaint);
Michael Jurkad51f33a2012-06-28 15:35:26 -0700298 }
299
300 public void disableHardwareLayers() {
Michael Jurkaca993832012-06-29 15:17:04 -0700301 mShortcutsAndWidgets.setLayerType(LAYER_TYPE_NONE, sPaint);
Michael Jurkad51f33a2012-06-28 15:35:26 -0700302 }
303
304 public void buildHardwareLayer() {
305 mShortcutsAndWidgets.buildLayer();
Adam Cohen2801caf2011-05-13 20:57:39 -0700306 }
307
Adam Cohen307fe232012-08-16 17:55:58 -0700308 public float getChildrenScale() {
309 return mIsHotseat ? mHotseatScale : 1.0f;
310 }
311
Winson Chung5f8afe62013-08-12 16:19:28 -0700312 public void setCellDimensions(int width, int height) {
313 mCellWidth = width;
314 mCellHeight = height;
315 mShortcutsAndWidgets.setCellDimensions(mCellWidth, mCellHeight, mWidthGap, mHeightGap,
316 mCountX, mCountY);
317 }
318
Adam Cohen2801caf2011-05-13 20:57:39 -0700319 public void setGridSize(int x, int y) {
320 mCountX = x;
321 mCountY = y;
322 mOccupied = new boolean[mCountX][mCountY];
Adam Cohen482ed822012-03-02 14:15:13 -0800323 mTmpOccupied = new boolean[mCountX][mCountY];
Adam Cohen7fbec102012-03-27 12:42:19 -0700324 mTempRectStack.clear();
Adam Cohen2374abf2013-04-16 14:56:57 -0700325 mShortcutsAndWidgets.setCellDimensions(mCellWidth, mCellHeight, mWidthGap, mHeightGap,
Winson Chung5f8afe62013-08-12 16:19:28 -0700326 mCountX, mCountY);
Adam Cohen76fc0852011-06-17 13:26:23 -0700327 requestLayout();
Adam Cohen2801caf2011-05-13 20:57:39 -0700328 }
329
Adam Cohen2374abf2013-04-16 14:56:57 -0700330 // Set whether or not to invert the layout horizontally if the layout is in RTL mode.
331 public void setInvertIfRtl(boolean invert) {
332 mShortcutsAndWidgets.setInvertIfRtl(invert);
333 }
334
Patrick Dubroy96864c32011-03-10 17:17:23 -0800335 private void invalidateBubbleTextView(BubbleTextView icon) {
336 final int padding = icon.getPressedOrFocusedBackgroundPadding();
Winson Chung4b825dcd2011-06-19 12:41:22 -0700337 invalidate(icon.getLeft() + getPaddingLeft() - padding,
338 icon.getTop() + getPaddingTop() - padding,
339 icon.getRight() + getPaddingLeft() + padding,
340 icon.getBottom() + getPaddingTop() + padding);
Patrick Dubroy96864c32011-03-10 17:17:23 -0800341 }
342
Adam Cohenb5ba0972011-09-07 18:02:31 -0700343 void setOverScrollAmount(float r, boolean left) {
344 if (left && mOverScrollForegroundDrawable != mOverScrollLeft) {
345 mOverScrollForegroundDrawable = mOverScrollLeft;
346 } else if (!left && mOverScrollForegroundDrawable != mOverScrollRight) {
347 mOverScrollForegroundDrawable = mOverScrollRight;
348 }
349
350 mForegroundAlpha = (int) Math.round((r * 255));
351 mOverScrollForegroundDrawable.setAlpha(mForegroundAlpha);
352 invalidate();
353 }
354
Patrick Dubroy96864c32011-03-10 17:17:23 -0800355 void setPressedOrFocusedIcon(BubbleTextView icon) {
356 // We draw the pressed or focused BubbleTextView's background in CellLayout because it
357 // requires an expanded clip rect (due to the glow's blur radius)
358 BubbleTextView oldIcon = mPressedOrFocusedIcon;
359 mPressedOrFocusedIcon = icon;
360 if (oldIcon != null) {
361 invalidateBubbleTextView(oldIcon);
362 }
363 if (mPressedOrFocusedIcon != null) {
364 invalidateBubbleTextView(mPressedOrFocusedIcon);
365 }
366 }
367
Michael Jurka33945b22010-12-21 18:19:38 -0800368 void setIsDragOverlapping(boolean isDragOverlapping) {
369 if (mIsDragOverlapping != isDragOverlapping) {
370 mIsDragOverlapping = isDragOverlapping;
Adam Cohendedbd962013-07-11 14:21:49 -0700371 setUseActiveGlowBackground(mIsDragOverlapping);
Michael Jurka33945b22010-12-21 18:19:38 -0800372 invalidate();
373 }
374 }
375
Adam Cohendedbd962013-07-11 14:21:49 -0700376 void setUseActiveGlowBackground(boolean use) {
377 mUseActiveGlowBackground = use;
378 }
379
Michael Jurka33945b22010-12-21 18:19:38 -0800380 boolean getIsDragOverlapping() {
381 return mIsDragOverlapping;
382 }
383
Adam Cohenebea84d2011-11-09 17:20:41 -0800384 protected void setOverscrollTransformsDirty(boolean dirty) {
385 mScrollingTransformsDirty = dirty;
386 }
387
388 protected void resetOverscrollTransforms() {
389 if (mScrollingTransformsDirty) {
390 setOverscrollTransformsDirty(false);
391 setTranslationX(0);
392 setRotationY(0);
393 // It doesn't matter if we pass true or false here, the important thing is that we
394 // pass 0, which results in the overscroll drawable not being drawn any more.
395 setOverScrollAmount(0, false);
396 setPivotX(getMeasuredWidth() / 2);
397 setPivotY(getMeasuredHeight() / 2);
398 }
399 }
400
Adam Cohen307fe232012-08-16 17:55:58 -0700401 public void scaleRect(Rect r, float scale) {
402 if (scale != 1.0f) {
403 r.left = (int) (r.left * scale + 0.5f);
404 r.top = (int) (r.top * scale + 0.5f);
405 r.right = (int) (r.right * scale + 0.5f);
406 r.bottom = (int) (r.bottom * scale + 0.5f);
407 }
408 }
409
410 Rect temp = new Rect();
411 void scaleRectAboutCenter(Rect in, Rect out, float scale) {
412 int cx = in.centerX();
413 int cy = in.centerY();
414 out.set(in);
415 out.offset(-cx, -cy);
416 scaleRect(out, scale);
417 out.offset(cx, cy);
418 }
419
Jeff Sharkey83f111d2009-04-20 21:03:13 -0700420 @Override
Patrick Dubroy1262e362010-10-06 15:49:50 -0700421 protected void onDraw(Canvas canvas) {
Michael Jurka3e7c7632010-10-02 16:01:03 -0700422 // When we're large, we are either drawn in a "hover" state (ie when dragging an item to
423 // a neighboring page) or with just a normal background (if backgroundAlpha > 0.0f)
424 // When we're small, we are either drawn normally or in the "accepts drops" state (during
425 // a drag). However, we also drag the mini hover background *over* one of those two
426 // backgrounds
Winson Chungb26f3d62011-06-02 10:49:29 -0700427 if (mBackgroundAlpha > 0.0f) {
Adam Cohenf34bab52010-09-30 14:11:56 -0700428 Drawable bg;
Michael Jurka33945b22010-12-21 18:19:38 -0800429
Adam Cohendedbd962013-07-11 14:21:49 -0700430 if (mUseActiveGlowBackground) {
Michael Jurka33945b22010-12-21 18:19:38 -0800431 // In the mini case, we draw the active_glow bg *over* the active background
Michael Jurkabdf78552011-10-31 14:34:25 -0700432 bg = mActiveGlowBackground;
Adam Cohenf34bab52010-09-30 14:11:56 -0700433 } else {
Michael Jurkabdf78552011-10-31 14:34:25 -0700434 bg = mNormalBackground;
Adam Cohenf34bab52010-09-30 14:11:56 -0700435 }
Michael Jurka33945b22010-12-21 18:19:38 -0800436
437 bg.setAlpha((int) (mBackgroundAlpha * mBackgroundAlphaMultiplier * 255));
438 bg.setBounds(mBackgroundRect);
439 bg.draw(canvas);
Michael Jurkaa63c4522010-08-19 13:52:27 -0700440 }
Romain Guya6abce82009-11-10 02:54:41 -0800441
Patrick Dubroy8e58e912010-10-14 13:21:48 -0700442 final Paint paint = mDragOutlinePaint;
Joe Onorato4be866d2010-10-10 11:26:02 -0700443 for (int i = 0; i < mDragOutlines.length; i++) {
Chet Haase472b2812010-10-14 07:02:04 -0700444 final float alpha = mDragOutlineAlphas[i];
Joe Onorato4be866d2010-10-10 11:26:02 -0700445 if (alpha > 0) {
Adam Cohend41fbf52012-02-16 23:53:59 -0800446 final Rect r = mDragOutlines[i];
Adam Cohen307fe232012-08-16 17:55:58 -0700447 scaleRectAboutCenter(r, temp, getChildrenScale());
Joe Onorato4be866d2010-10-10 11:26:02 -0700448 final Bitmap b = (Bitmap) mDragOutlineAnims[i].getTag();
Chet Haase472b2812010-10-14 07:02:04 -0700449 paint.setAlpha((int)(alpha + .5f));
Adam Cohen307fe232012-08-16 17:55:58 -0700450 canvas.drawBitmap(b, null, temp, paint);
Winson Chung150fbab2010-09-29 17:14:26 -0700451 }
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700452 }
Patrick Dubroy96864c32011-03-10 17:17:23 -0800453
454 // We draw the pressed or focused BubbleTextView's background in CellLayout because it
455 // requires an expanded clip rect (due to the glow's blur radius)
456 if (mPressedOrFocusedIcon != null) {
457 final int padding = mPressedOrFocusedIcon.getPressedOrFocusedBackgroundPadding();
458 final Bitmap b = mPressedOrFocusedIcon.getPressedOrFocusedBackground();
459 if (b != null) {
460 canvas.drawBitmap(b,
Winson Chung4b825dcd2011-06-19 12:41:22 -0700461 mPressedOrFocusedIcon.getLeft() + getPaddingLeft() - padding,
462 mPressedOrFocusedIcon.getTop() + getPaddingTop() - padding,
Patrick Dubroy96864c32011-03-10 17:17:23 -0800463 null);
464 }
465 }
Adam Cohen69ce2e52011-07-03 19:25:21 -0700466
Adam Cohen482ed822012-03-02 14:15:13 -0800467 if (DEBUG_VISUALIZE_OCCUPIED) {
468 int[] pt = new int[2];
469 ColorDrawable cd = new ColorDrawable(Color.RED);
Adam Cohene7587d22012-05-24 18:50:02 -0700470 cd.setBounds(0, 0, mCellWidth, mCellHeight);
Adam Cohen482ed822012-03-02 14:15:13 -0800471 for (int i = 0; i < mCountX; i++) {
472 for (int j = 0; j < mCountY; j++) {
473 if (mOccupied[i][j]) {
474 cellToPoint(i, j, pt);
475 canvas.save();
476 canvas.translate(pt[0], pt[1]);
477 cd.draw(canvas);
478 canvas.restore();
479 }
480 }
481 }
482 }
483
Andrew Flynn850d2e72012-04-26 16:51:20 -0700484 int previewOffset = FolderRingAnimator.sPreviewSize;
485
Adam Cohen69ce2e52011-07-03 19:25:21 -0700486 // The folder outer / inner ring image(s)
Winson Chung5f8afe62013-08-12 16:19:28 -0700487 LauncherAppState app = LauncherAppState.getInstance();
488 DeviceProfile grid = app.getDynamicGrid().getDeviceProfile();
Adam Cohen69ce2e52011-07-03 19:25:21 -0700489 for (int i = 0; i < mFolderOuterRings.size(); i++) {
490 FolderRingAnimator fra = mFolderOuterRings.get(i);
491
492 // Draw outer ring
493 Drawable d = FolderRingAnimator.sSharedOuterRingDrawable;
Winson Chung5f8afe62013-08-12 16:19:28 -0700494 int width = (int) (fra.getOuterRingSize() * getChildrenScale());
Adam Cohen69ce2e52011-07-03 19:25:21 -0700495 int height = width;
496 cellToPoint(fra.mCellX, fra.mCellY, mTempLocation);
Winson Chung5f8afe62013-08-12 16:19:28 -0700497 View child = getChildAt(fra.mCellX, fra.mCellY);
Adam Cohen69ce2e52011-07-03 19:25:21 -0700498
499 int centerX = mTempLocation[0] + mCellWidth / 2;
Winson Chung5f8afe62013-08-12 16:19:28 -0700500 int centerY = mTempLocation[1] + previewOffset / 2 +
501 child.getPaddingTop() + grid.folderBackgroundOffset;
Adam Cohen69ce2e52011-07-03 19:25:21 -0700502
503 canvas.save();
504 canvas.translate(centerX - width / 2, centerY - height / 2);
505 d.setBounds(0, 0, width, height);
506 d.draw(canvas);
507 canvas.restore();
508
509 // Draw inner ring
510 d = FolderRingAnimator.sSharedInnerRingDrawable;
Winson Chung5f8afe62013-08-12 16:19:28 -0700511 width = (int) (fra.getInnerRingSize() * getChildrenScale());
Adam Cohen69ce2e52011-07-03 19:25:21 -0700512 height = width;
513 cellToPoint(fra.mCellX, fra.mCellY, mTempLocation);
Winson Chung5f8afe62013-08-12 16:19:28 -0700514 child = getChildAt(fra.mCellX, fra.mCellY);
Adam Cohen69ce2e52011-07-03 19:25:21 -0700515
516 centerX = mTempLocation[0] + mCellWidth / 2;
Winson Chung5f8afe62013-08-12 16:19:28 -0700517 centerY = mTempLocation[1] + previewOffset / 2 +
518 child.getPaddingTop() + grid.folderBackgroundOffset;
Adam Cohen69ce2e52011-07-03 19:25:21 -0700519 canvas.save();
520 canvas.translate(centerX - width / 2, centerY - width / 2);
521 d.setBounds(0, 0, width, height);
522 d.draw(canvas);
523 canvas.restore();
524 }
Adam Cohenc51934b2011-07-26 21:07:43 -0700525
526 if (mFolderLeaveBehindCell[0] >= 0 && mFolderLeaveBehindCell[1] >= 0) {
527 Drawable d = FolderIcon.sSharedFolderLeaveBehind;
528 int width = d.getIntrinsicWidth();
529 int height = d.getIntrinsicHeight();
530
531 cellToPoint(mFolderLeaveBehindCell[0], mFolderLeaveBehindCell[1], mTempLocation);
Winson Chung5f8afe62013-08-12 16:19:28 -0700532 View child = getChildAt(mFolderLeaveBehindCell[0], mFolderLeaveBehindCell[1]);
Adam Cohenc51934b2011-07-26 21:07:43 -0700533 int centerX = mTempLocation[0] + mCellWidth / 2;
Winson Chung5f8afe62013-08-12 16:19:28 -0700534 int centerY = mTempLocation[1] + previewOffset / 2 +
535 child.getPaddingTop() + grid.folderBackgroundOffset;
Adam Cohenc51934b2011-07-26 21:07:43 -0700536
537 canvas.save();
538 canvas.translate(centerX - width / 2, centerY - width / 2);
539 d.setBounds(0, 0, width, height);
540 d.draw(canvas);
541 canvas.restore();
542 }
Adam Cohen69ce2e52011-07-03 19:25:21 -0700543 }
544
Adam Cohenb5ba0972011-09-07 18:02:31 -0700545 @Override
546 protected void dispatchDraw(Canvas canvas) {
547 super.dispatchDraw(canvas);
548 if (mForegroundAlpha > 0) {
549 mOverScrollForegroundDrawable.setBounds(mForegroundRect);
550 Paint p = ((NinePatchDrawable) mOverScrollForegroundDrawable).getPaint();
Romain Guy8a0bff52012-05-06 13:14:33 -0700551 p.setXfermode(sAddBlendMode);
Adam Cohenb5ba0972011-09-07 18:02:31 -0700552 mOverScrollForegroundDrawable.draw(canvas);
553 p.setXfermode(null);
554 }
555 }
556
Adam Cohen69ce2e52011-07-03 19:25:21 -0700557 public void showFolderAccept(FolderRingAnimator fra) {
558 mFolderOuterRings.add(fra);
559 }
560
561 public void hideFolderAccept(FolderRingAnimator fra) {
562 if (mFolderOuterRings.contains(fra)) {
563 mFolderOuterRings.remove(fra);
564 }
565 invalidate();
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700566 }
567
Adam Cohenc51934b2011-07-26 21:07:43 -0700568 public void setFolderLeaveBehindCell(int x, int y) {
569 mFolderLeaveBehindCell[0] = x;
570 mFolderLeaveBehindCell[1] = y;
571 invalidate();
572 }
573
574 public void clearFolderLeaveBehind() {
575 mFolderLeaveBehindCell[0] = -1;
576 mFolderLeaveBehindCell[1] = -1;
577 invalidate();
578 }
579
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700580 @Override
Michael Jurkae6235dd2011-10-04 15:02:05 -0700581 public boolean shouldDelayChildPressedState() {
582 return false;
583 }
584
Adam Cohen1462de32012-07-24 22:34:36 -0700585 public void restoreInstanceState(SparseArray<Parcelable> states) {
586 dispatchRestoreInstanceState(states);
587 }
588
Michael Jurkae6235dd2011-10-04 15:02:05 -0700589 @Override
Jeff Sharkey83f111d2009-04-20 21:03:13 -0700590 public void cancelLongPress() {
591 super.cancelLongPress();
592
593 // Cancel long press for all children
594 final int count = getChildCount();
595 for (int i = 0; i < count; i++) {
596 final View child = getChildAt(i);
597 child.cancelLongPress();
598 }
599 }
600
Michael Jurkadee05892010-07-27 10:01:56 -0700601 public void setOnInterceptTouchListener(View.OnTouchListener listener) {
602 mInterceptTouchListener = listener;
603 }
604
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800605 int getCountX() {
Adam Cohend22015c2010-07-26 22:02:18 -0700606 return mCountX;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800607 }
608
609 int getCountY() {
Adam Cohend22015c2010-07-26 22:02:18 -0700610 return mCountY;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800611 }
612
Andrew Flynn0dca1ec2012-02-29 13:33:22 -0800613 public void setIsHotseat(boolean isHotseat) {
614 mIsHotseat = isHotseat;
Winson Chung5f8afe62013-08-12 16:19:28 -0700615 mShortcutsAndWidgets.setIsHotseat(isHotseat);
Andrew Flynn0dca1ec2012-02-29 13:33:22 -0800616 }
617
Andrew Flynn0dca1ec2012-02-29 13:33:22 -0800618 public boolean addViewToCellLayout(View child, int index, int childId, LayoutParams params,
Andrew Flynn850d2e72012-04-26 16:51:20 -0700619 boolean markCells) {
Winson Chungaafa03c2010-06-11 17:34:16 -0700620 final LayoutParams lp = params;
621
Andrew Flynnde38e422012-05-08 11:22:15 -0700622 // Hotseat icons - remove text
Andrew Flynn0dca1ec2012-02-29 13:33:22 -0800623 if (child instanceof BubbleTextView) {
624 BubbleTextView bubbleChild = (BubbleTextView) child;
625
Andrew Flynnde38e422012-05-08 11:22:15 -0700626 Resources res = getResources();
Winson Chung5f8afe62013-08-12 16:19:28 -0700627 bubbleChild.setTextVisibility(!mIsHotseat);
Andrew Flynn0dca1ec2012-02-29 13:33:22 -0800628 }
629
Adam Cohen307fe232012-08-16 17:55:58 -0700630 child.setScaleX(getChildrenScale());
631 child.setScaleY(getChildrenScale());
632
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800633 // Generate an id for each view, this assumes we have at most 256x256 cells
634 // per workspace screen
Adam Cohend22015c2010-07-26 22:02:18 -0700635 if (lp.cellX >= 0 && lp.cellX <= mCountX - 1 && lp.cellY >= 0 && lp.cellY <= mCountY - 1) {
Winson Chungaafa03c2010-06-11 17:34:16 -0700636 // If the horizontal or vertical span is set to -1, it is taken to
637 // mean that it spans the extent of the CellLayout
Adam Cohend22015c2010-07-26 22:02:18 -0700638 if (lp.cellHSpan < 0) lp.cellHSpan = mCountX;
639 if (lp.cellVSpan < 0) lp.cellVSpan = mCountY;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800640
Winson Chungaafa03c2010-06-11 17:34:16 -0700641 child.setId(childId);
642
Michael Jurkaa52570f2012-03-20 03:18:20 -0700643 mShortcutsAndWidgets.addView(child, index, lp);
Michael Jurkadee05892010-07-27 10:01:56 -0700644
Michael Jurkaf3ca3ab2010-10-20 17:08:24 -0700645 if (markCells) markCellsAsOccupiedForView(child);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700646
Winson Chungaafa03c2010-06-11 17:34:16 -0700647 return true;
648 }
649 return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800650 }
Michael Jurka3e7c7632010-10-02 16:01:03 -0700651
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800652 @Override
Michael Jurka0280c3b2010-09-17 15:00:07 -0700653 public void removeAllViews() {
654 clearOccupiedCells();
Michael Jurkaa52570f2012-03-20 03:18:20 -0700655 mShortcutsAndWidgets.removeAllViews();
Michael Jurka0280c3b2010-09-17 15:00:07 -0700656 }
657
658 @Override
659 public void removeAllViewsInLayout() {
Michael Jurkaa52570f2012-03-20 03:18:20 -0700660 if (mShortcutsAndWidgets.getChildCount() > 0) {
Michael Jurka7cfc2822011-08-02 20:19:24 -0700661 clearOccupiedCells();
Michael Jurkaa52570f2012-03-20 03:18:20 -0700662 mShortcutsAndWidgets.removeAllViewsInLayout();
Michael Jurka7cfc2822011-08-02 20:19:24 -0700663 }
Michael Jurka0280c3b2010-09-17 15:00:07 -0700664 }
665
Michael Jurkaf3ca3ab2010-10-20 17:08:24 -0700666 public void removeViewWithoutMarkingCells(View view) {
Michael Jurkaa52570f2012-03-20 03:18:20 -0700667 mShortcutsAndWidgets.removeView(view);
Michael Jurkaf3ca3ab2010-10-20 17:08:24 -0700668 }
669
Michael Jurka0280c3b2010-09-17 15:00:07 -0700670 @Override
671 public void removeView(View view) {
672 markCellsAsUnoccupiedForView(view);
Michael Jurkaa52570f2012-03-20 03:18:20 -0700673 mShortcutsAndWidgets.removeView(view);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700674 }
675
676 @Override
677 public void removeViewAt(int index) {
Michael Jurkaa52570f2012-03-20 03:18:20 -0700678 markCellsAsUnoccupiedForView(mShortcutsAndWidgets.getChildAt(index));
679 mShortcutsAndWidgets.removeViewAt(index);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700680 }
681
682 @Override
683 public void removeViewInLayout(View view) {
684 markCellsAsUnoccupiedForView(view);
Michael Jurkaa52570f2012-03-20 03:18:20 -0700685 mShortcutsAndWidgets.removeViewInLayout(view);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700686 }
687
688 @Override
689 public void removeViews(int start, int count) {
690 for (int i = start; i < start + count; i++) {
Michael Jurkaa52570f2012-03-20 03:18:20 -0700691 markCellsAsUnoccupiedForView(mShortcutsAndWidgets.getChildAt(i));
Michael Jurka0280c3b2010-09-17 15:00:07 -0700692 }
Michael Jurkaa52570f2012-03-20 03:18:20 -0700693 mShortcutsAndWidgets.removeViews(start, count);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700694 }
695
696 @Override
697 public void removeViewsInLayout(int start, int count) {
698 for (int i = start; i < start + count; i++) {
Michael Jurkaa52570f2012-03-20 03:18:20 -0700699 markCellsAsUnoccupiedForView(mShortcutsAndWidgets.getChildAt(i));
Michael Jurka0280c3b2010-09-17 15:00:07 -0700700 }
Michael Jurkaa52570f2012-03-20 03:18:20 -0700701 mShortcutsAndWidgets.removeViewsInLayout(start, count);
Michael Jurkaabded662011-03-04 12:06:57 -0800702 }
703
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800704 @Override
705 protected void onAttachedToWindow() {
706 super.onAttachedToWindow();
Adam Cohendcd297f2013-06-18 13:13:40 -0700707 if (getParent() instanceof Workspace) {
708 Workspace workspace = (Workspace) getParent();
709 mCellInfo.screenId = workspace.getIdForScreen(this);
710 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800711 }
712
Michael Jurkaaf442092010-06-10 17:01:57 -0700713 public void setTagToCellInfoForPoint(int touchX, int touchY) {
714 final CellInfo cellInfo = mCellInfo;
Winson Chungeecf02d2012-03-02 17:14:58 -0800715 Rect frame = mRect;
Michael Jurka8b805b12012-04-18 14:23:14 -0700716 final int x = touchX + getScrollX();
717 final int y = touchY + getScrollY();
Michael Jurkaa52570f2012-03-20 03:18:20 -0700718 final int count = mShortcutsAndWidgets.getChildCount();
Michael Jurkaaf442092010-06-10 17:01:57 -0700719
720 boolean found = false;
721 for (int i = count - 1; i >= 0; i--) {
Michael Jurkaa52570f2012-03-20 03:18:20 -0700722 final View child = mShortcutsAndWidgets.getChildAt(i);
Adam Cohend4844c32011-02-18 19:25:06 -0800723 final LayoutParams lp = (LayoutParams) child.getLayoutParams();
Michael Jurkaaf442092010-06-10 17:01:57 -0700724
Adam Cohen1b607ed2011-03-03 17:26:50 -0800725 if ((child.getVisibility() == VISIBLE || child.getAnimation() != null) &&
726 lp.isLockedToGrid) {
Michael Jurkaaf442092010-06-10 17:01:57 -0700727 child.getHitRect(frame);
Winson Chung0be025d2011-05-23 17:45:09 -0700728
Winson Chungeecf02d2012-03-02 17:14:58 -0800729 float scale = child.getScaleX();
730 frame = new Rect(child.getLeft(), child.getTop(), child.getRight(),
731 child.getBottom());
Winson Chung0be025d2011-05-23 17:45:09 -0700732 // The child hit rect is relative to the CellLayoutChildren parent, so we need to
733 // offset that by this CellLayout's padding to test an (x,y) point that is relative
734 // to this view.
Michael Jurka8b805b12012-04-18 14:23:14 -0700735 frame.offset(getPaddingLeft(), getPaddingTop());
Winson Chungeecf02d2012-03-02 17:14:58 -0800736 frame.inset((int) (frame.width() * (1f - scale) / 2),
737 (int) (frame.height() * (1f - scale) / 2));
Winson Chung0be025d2011-05-23 17:45:09 -0700738
Michael Jurkaaf442092010-06-10 17:01:57 -0700739 if (frame.contains(x, y)) {
Michael Jurkaaf442092010-06-10 17:01:57 -0700740 cellInfo.cell = child;
741 cellInfo.cellX = lp.cellX;
742 cellInfo.cellY = lp.cellY;
743 cellInfo.spanX = lp.cellHSpan;
744 cellInfo.spanY = lp.cellVSpan;
Michael Jurkaaf442092010-06-10 17:01:57 -0700745 found = true;
Michael Jurkaaf442092010-06-10 17:01:57 -0700746 break;
747 }
748 }
749 }
Winson Chungaafa03c2010-06-11 17:34:16 -0700750
Michael Jurkad771c962011-08-09 15:00:48 -0700751 mLastDownOnOccupiedCell = found;
752
Michael Jurkaaf442092010-06-10 17:01:57 -0700753 if (!found) {
Winson Chung0be025d2011-05-23 17:45:09 -0700754 final int cellXY[] = mTmpXY;
Michael Jurkaaf442092010-06-10 17:01:57 -0700755 pointToCellExact(x, y, cellXY);
756
Michael Jurkaaf442092010-06-10 17:01:57 -0700757 cellInfo.cell = null;
758 cellInfo.cellX = cellXY[0];
759 cellInfo.cellY = cellXY[1];
760 cellInfo.spanX = 1;
761 cellInfo.spanY = 1;
Michael Jurkaaf442092010-06-10 17:01:57 -0700762 }
763 setTag(cellInfo);
764 }
765
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800766 @Override
767 public boolean onInterceptTouchEvent(MotionEvent ev) {
Adam Cohenc1997fd2011-08-15 18:26:39 -0700768 // First we clear the tag to ensure that on every touch down we start with a fresh slate,
769 // even in the case where we return early. Not clearing here was causing bugs whereby on
770 // long-press we'd end up picking up an item from a previous drag operation.
771 final int action = ev.getAction();
772
773 if (action == MotionEvent.ACTION_DOWN) {
774 clearTagCellInfo();
775 }
776
Michael Jurkadee05892010-07-27 10:01:56 -0700777 if (mInterceptTouchListener != null && mInterceptTouchListener.onTouch(this, ev)) {
778 return true;
779 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800780
781 if (action == MotionEvent.ACTION_DOWN) {
Michael Jurkaaf442092010-06-10 17:01:57 -0700782 setTagToCellInfoForPoint((int) ev.getX(), (int) ev.getY());
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800783 }
Winson Chungeecf02d2012-03-02 17:14:58 -0800784
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800785 return false;
786 }
787
Adam Cohenc1997fd2011-08-15 18:26:39 -0700788 private void clearTagCellInfo() {
789 final CellInfo cellInfo = mCellInfo;
790 cellInfo.cell = null;
791 cellInfo.cellX = -1;
792 cellInfo.cellY = -1;
793 cellInfo.spanX = 0;
794 cellInfo.spanY = 0;
795 setTag(cellInfo);
796 }
797
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800798 public CellInfo getTag() {
Michael Jurka0280c3b2010-09-17 15:00:07 -0700799 return (CellInfo) super.getTag();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800800 }
801
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700802 /**
Winson Chungaafa03c2010-06-11 17:34:16 -0700803 * Given a point, return the cell that strictly encloses that point
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800804 * @param x X coordinate of the point
805 * @param y Y coordinate of the point
806 * @param result Array of 2 ints to hold the x and y coordinate of the cell
807 */
808 void pointToCellExact(int x, int y, int[] result) {
Winson Chung4b825dcd2011-06-19 12:41:22 -0700809 final int hStartPadding = getPaddingLeft();
810 final int vStartPadding = getPaddingTop();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800811
812 result[0] = (x - hStartPadding) / (mCellWidth + mWidthGap);
813 result[1] = (y - vStartPadding) / (mCellHeight + mHeightGap);
814
Adam Cohend22015c2010-07-26 22:02:18 -0700815 final int xAxis = mCountX;
816 final int yAxis = mCountY;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800817
818 if (result[0] < 0) result[0] = 0;
819 if (result[0] >= xAxis) result[0] = xAxis - 1;
820 if (result[1] < 0) result[1] = 0;
821 if (result[1] >= yAxis) result[1] = yAxis - 1;
822 }
Winson Chungaafa03c2010-06-11 17:34:16 -0700823
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800824 /**
825 * Given a point, return the cell that most closely encloses that point
826 * @param x X coordinate of the point
827 * @param y Y coordinate of the point
828 * @param result Array of 2 ints to hold the x and y coordinate of the cell
829 */
830 void pointToCellRounded(int x, int y, int[] result) {
831 pointToCellExact(x + (mCellWidth / 2), y + (mCellHeight / 2), result);
832 }
833
834 /**
835 * Given a cell coordinate, return the point that represents the upper left corner of that cell
Winson Chungaafa03c2010-06-11 17:34:16 -0700836 *
837 * @param cellX X coordinate of the cell
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800838 * @param cellY Y coordinate of the cell
Winson Chungaafa03c2010-06-11 17:34:16 -0700839 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800840 * @param result Array of 2 ints to hold the x and y coordinate of the point
841 */
842 void cellToPoint(int cellX, int cellY, int[] result) {
Winson Chung4b825dcd2011-06-19 12:41:22 -0700843 final int hStartPadding = getPaddingLeft();
844 final int vStartPadding = getPaddingTop();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800845
846 result[0] = hStartPadding + cellX * (mCellWidth + mWidthGap);
847 result[1] = vStartPadding + cellY * (mCellHeight + mHeightGap);
848 }
849
Adam Cohene3e27a82011-04-15 12:07:39 -0700850 /**
Adam Cohen482ed822012-03-02 14:15:13 -0800851 * Given a cell coordinate, return the point that represents the center of the cell
Adam Cohene3e27a82011-04-15 12:07:39 -0700852 *
853 * @param cellX X coordinate of the cell
854 * @param cellY Y coordinate of the cell
855 *
856 * @param result Array of 2 ints to hold the x and y coordinate of the point
857 */
858 void cellToCenterPoint(int cellX, int cellY, int[] result) {
Adam Cohen47a876d2012-03-19 13:21:41 -0700859 regionToCenterPoint(cellX, cellY, 1, 1, result);
860 }
861
862 /**
863 * Given a cell coordinate and span return the point that represents the center of the regio
864 *
865 * @param cellX X coordinate of the cell
866 * @param cellY Y coordinate of the cell
867 *
868 * @param result Array of 2 ints to hold the x and y coordinate of the point
869 */
870 void regionToCenterPoint(int cellX, int cellY, int spanX, int spanY, int[] result) {
Winson Chung4b825dcd2011-06-19 12:41:22 -0700871 final int hStartPadding = getPaddingLeft();
872 final int vStartPadding = getPaddingTop();
Adam Cohen47a876d2012-03-19 13:21:41 -0700873 result[0] = hStartPadding + cellX * (mCellWidth + mWidthGap) +
874 (spanX * mCellWidth + (spanX - 1) * mWidthGap) / 2;
875 result[1] = vStartPadding + cellY * (mCellHeight + mHeightGap) +
876 (spanY * mCellHeight + (spanY - 1) * mHeightGap) / 2;
Adam Cohene3e27a82011-04-15 12:07:39 -0700877 }
878
Adam Cohen19f37922012-03-21 11:59:11 -0700879 /**
880 * Given a cell coordinate and span fills out a corresponding pixel rect
881 *
882 * @param cellX X coordinate of the cell
883 * @param cellY Y coordinate of the cell
884 * @param result Rect in which to write the result
885 */
886 void regionToRect(int cellX, int cellY, int spanX, int spanY, Rect result) {
887 final int hStartPadding = getPaddingLeft();
888 final int vStartPadding = getPaddingTop();
889 final int left = hStartPadding + cellX * (mCellWidth + mWidthGap);
890 final int top = vStartPadding + cellY * (mCellHeight + mHeightGap);
891 result.set(left, top, left + (spanX * mCellWidth + (spanX - 1) * mWidthGap),
892 top + (spanY * mCellHeight + (spanY - 1) * mHeightGap));
893 }
894
Adam Cohen482ed822012-03-02 14:15:13 -0800895 public float getDistanceFromCell(float x, float y, int[] cell) {
896 cellToCenterPoint(cell[0], cell[1], mTmpPoint);
897 float distance = (float) Math.sqrt( Math.pow(x - mTmpPoint[0], 2) +
898 Math.pow(y - mTmpPoint[1], 2));
899 return distance;
900 }
901
Romain Guy84f296c2009-11-04 15:00:44 -0800902 int getCellWidth() {
903 return mCellWidth;
904 }
905
906 int getCellHeight() {
907 return mCellHeight;
908 }
909
Adam Cohend4844c32011-02-18 19:25:06 -0800910 int getWidthGap() {
911 return mWidthGap;
912 }
913
914 int getHeightGap() {
915 return mHeightGap;
916 }
917
Adam Cohen7f4eabe2011-04-21 16:19:16 -0700918 Rect getContentRect(Rect r) {
919 if (r == null) {
920 r = new Rect();
921 }
922 int left = getPaddingLeft();
923 int top = getPaddingTop();
Michael Jurka8b805b12012-04-18 14:23:14 -0700924 int right = left + getWidth() - getPaddingLeft() - getPaddingRight();
925 int bottom = top + getHeight() - getPaddingTop() - getPaddingBottom();
Adam Cohen7f4eabe2011-04-21 16:19:16 -0700926 r.set(left, top, right, bottom);
927 return r;
928 }
929
Adam Cohena897f392012-04-27 18:12:05 -0700930 static void getMetrics(Rect metrics, Resources res, int measureWidth, int measureHeight,
931 int countX, int countY, int orientation) {
Winson Chung5f8afe62013-08-12 16:19:28 -0700932 LauncherAppState app = LauncherAppState.getInstance();
933 DeviceProfile grid = app.getDynamicGrid().getDeviceProfile();
934 metrics.set(grid.calculateCellWidth(measureWidth, countX),
935 grid.calculateCellHeight(measureHeight, countY), 0, 0);
Adam Cohenf4bd5792012-04-27 11:35:29 -0700936 }
937
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700938 public void setFixedSize(int width, int height) {
939 mFixedWidth = width;
940 mFixedHeight = height;
941 }
942
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800943 @Override
944 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
Winson Chung5f8afe62013-08-12 16:19:28 -0700945 LauncherAppState app = LauncherAppState.getInstance();
946 DeviceProfile grid = app.getDynamicGrid().getDeviceProfile();
947
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800948 int widthSpecMode = MeasureSpec.getMode(widthMeasureSpec);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800949 int heightSpecMode = MeasureSpec.getMode(heightMeasureSpec);
Winson Chung5f8afe62013-08-12 16:19:28 -0700950 int widthSize = MeasureSpec.getSize(widthMeasureSpec);
951 int heightSize = MeasureSpec.getSize(heightMeasureSpec);
952 if (mCellWidth < 0 || mCellHeight < 0) {
953 mCellWidth = grid.calculateCellWidth(widthSize, mCountX);
954 mCellHeight = grid.calculateCellHeight(heightSize, mCountY);
955 mShortcutsAndWidgets.setCellDimensions(mCellWidth, mCellHeight, mWidthGap,
956 mHeightGap, mCountX, mCountY);
957 }
Winson Chungaafa03c2010-06-11 17:34:16 -0700958
Winson Chung5f8afe62013-08-12 16:19:28 -0700959 int newWidth = widthSize;
960 int newHeight = heightSize;
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700961 if (mFixedWidth > 0 && mFixedHeight > 0) {
962 newWidth = mFixedWidth;
963 newHeight = mFixedHeight;
964 } else if (widthSpecMode == MeasureSpec.UNSPECIFIED || heightSpecMode == MeasureSpec.UNSPECIFIED) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800965 throw new RuntimeException("CellLayout cannot have UNSPECIFIED dimensions");
966 }
967
Adam Cohend22015c2010-07-26 22:02:18 -0700968 int numWidthGaps = mCountX - 1;
969 int numHeightGaps = mCountY - 1;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800970
Adam Cohen234c4cd2011-07-17 21:03:04 -0700971 if (mOriginalWidthGap < 0 || mOriginalHeightGap < 0) {
Winson Chung5f8afe62013-08-12 16:19:28 -0700972 int hSpace = widthSize - getPaddingLeft() - getPaddingRight();
973 int vSpace = heightSize - getPaddingTop() - getPaddingBottom();
Adam Cohenf4bd5792012-04-27 11:35:29 -0700974 int hFreeSpace = hSpace - (mCountX * mCellWidth);
975 int vFreeSpace = vSpace - (mCountY * mCellHeight);
Winson Chung4b825dcd2011-06-19 12:41:22 -0700976 mWidthGap = Math.min(mMaxGap, numWidthGaps > 0 ? (hFreeSpace / numWidthGaps) : 0);
977 mHeightGap = Math.min(mMaxGap,numHeightGaps > 0 ? (vFreeSpace / numHeightGaps) : 0);
Winson Chung5f8afe62013-08-12 16:19:28 -0700978 mShortcutsAndWidgets.setCellDimensions(mCellWidth, mCellHeight, mWidthGap,
979 mHeightGap, mCountX, mCountY);
Adam Cohen234c4cd2011-07-17 21:03:04 -0700980 } else {
981 mWidthGap = mOriginalWidthGap;
982 mHeightGap = mOriginalHeightGap;
Winson Chungece7f5b2010-10-22 14:54:12 -0700983 }
Michael Jurka8c920dd2011-01-20 14:16:56 -0800984 int count = getChildCount();
Winson Chung5f8afe62013-08-12 16:19:28 -0700985 int maxWidth = 0;
986 int maxHeight = 0;
Michael Jurka8c920dd2011-01-20 14:16:56 -0800987 for (int i = 0; i < count; i++) {
988 View child = getChildAt(i);
Michael Jurka8b805b12012-04-18 14:23:14 -0700989 int childWidthMeasureSpec = MeasureSpec.makeMeasureSpec(newWidth - getPaddingLeft() -
990 getPaddingRight(), MeasureSpec.EXACTLY);
991 int childheightMeasureSpec = MeasureSpec.makeMeasureSpec(newHeight - getPaddingTop() -
992 getPaddingBottom(), MeasureSpec.EXACTLY);
Michael Jurka8c920dd2011-01-20 14:16:56 -0800993 child.measure(childWidthMeasureSpec, childheightMeasureSpec);
Winson Chung5f8afe62013-08-12 16:19:28 -0700994 maxWidth = Math.max(maxWidth, child.getMeasuredWidth());
995 maxHeight = Math.max(maxHeight, child.getMeasuredHeight());
Michael Jurka8c920dd2011-01-20 14:16:56 -0800996 }
Winson Chung5f8afe62013-08-12 16:19:28 -0700997 setMeasuredDimension(maxWidth, maxHeight);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800998 }
999
1000 @Override
Michael Jurka28750fb2010-09-24 17:43:49 -07001001 protected void onLayout(boolean changed, int l, int t, int r, int b) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001002 int count = getChildCount();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001003 for (int i = 0; i < count; i++) {
Michael Jurka8c920dd2011-01-20 14:16:56 -08001004 View child = getChildAt(i);
Michael Jurka8b805b12012-04-18 14:23:14 -07001005 child.layout(getPaddingLeft(), getPaddingTop(),
1006 r - l - getPaddingRight(), b - t - getPaddingBottom());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001007 }
1008 }
1009
1010 @Override
Michael Jurkadee05892010-07-27 10:01:56 -07001011 protected void onSizeChanged(int w, int h, int oldw, int oldh) {
1012 super.onSizeChanged(w, h, oldw, oldh);
Michael Jurka18014792010-10-14 09:01:34 -07001013 mBackgroundRect.set(0, 0, w, h);
Adam Cohenb5ba0972011-09-07 18:02:31 -07001014 mForegroundRect.set(mForegroundPadding, mForegroundPadding,
Adam Cohen215b4162012-08-30 13:14:08 -07001015 w - mForegroundPadding, h - mForegroundPadding);
Michael Jurkadee05892010-07-27 10:01:56 -07001016 }
1017
1018 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001019 protected void setChildrenDrawingCacheEnabled(boolean enabled) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07001020 mShortcutsAndWidgets.setChildrenDrawingCacheEnabled(enabled);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001021 }
1022
1023 @Override
1024 protected void setChildrenDrawnWithCacheEnabled(boolean enabled) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07001025 mShortcutsAndWidgets.setChildrenDrawnWithCacheEnabled(enabled);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001026 }
1027
Michael Jurka5f1c5092010-09-03 14:15:02 -07001028 public float getBackgroundAlpha() {
1029 return mBackgroundAlpha;
Michael Jurkadee05892010-07-27 10:01:56 -07001030 }
1031
Adam Cohen1b0aaac2010-10-28 11:11:18 -07001032 public void setBackgroundAlphaMultiplier(float multiplier) {
Michael Jurkaa3d30ad2012-05-08 13:43:43 -07001033 if (mBackgroundAlphaMultiplier != multiplier) {
1034 mBackgroundAlphaMultiplier = multiplier;
1035 invalidate();
1036 }
Adam Cohen1b0aaac2010-10-28 11:11:18 -07001037 }
1038
Adam Cohenddb82192010-11-10 16:32:54 -08001039 public float getBackgroundAlphaMultiplier() {
1040 return mBackgroundAlphaMultiplier;
1041 }
1042
Michael Jurka5f1c5092010-09-03 14:15:02 -07001043 public void setBackgroundAlpha(float alpha) {
Michael Jurkaafaa0502011-12-13 18:22:50 -08001044 if (mBackgroundAlpha != alpha) {
1045 mBackgroundAlpha = alpha;
1046 invalidate();
1047 }
Michael Jurkadee05892010-07-27 10:01:56 -07001048 }
1049
Michael Jurkaa52570f2012-03-20 03:18:20 -07001050 public void setShortcutAndWidgetAlpha(float alpha) {
Michael Jurka0142d492010-08-25 17:46:15 -07001051 final int childCount = getChildCount();
1052 for (int i = 0; i < childCount; i++) {
Michael Jurkadee05892010-07-27 10:01:56 -07001053 getChildAt(i).setAlpha(alpha);
1054 }
1055 }
1056
Michael Jurkaa52570f2012-03-20 03:18:20 -07001057 public ShortcutAndWidgetContainer getShortcutsAndWidgets() {
1058 if (getChildCount() > 0) {
1059 return (ShortcutAndWidgetContainer) getChildAt(0);
1060 }
1061 return null;
1062 }
1063
Patrick Dubroy440c3602010-07-13 17:50:32 -07001064 public View getChildAt(int x, int y) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07001065 return mShortcutsAndWidgets.getChildAt(x, y);
Patrick Dubroy440c3602010-07-13 17:50:32 -07001066 }
1067
Adam Cohen76fc0852011-06-17 13:26:23 -07001068 public boolean animateChildToPosition(final View child, int cellX, int cellY, int duration,
Adam Cohen482ed822012-03-02 14:15:13 -08001069 int delay, boolean permanent, boolean adjustOccupied) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07001070 ShortcutAndWidgetContainer clc = getShortcutsAndWidgets();
Adam Cohen482ed822012-03-02 14:15:13 -08001071 boolean[][] occupied = mOccupied;
1072 if (!permanent) {
1073 occupied = mTmpOccupied;
1074 }
1075
Adam Cohen19f37922012-03-21 11:59:11 -07001076 if (clc.indexOfChild(child) != -1) {
Adam Cohenbfbfd262011-06-13 16:55:12 -07001077 final LayoutParams lp = (LayoutParams) child.getLayoutParams();
1078 final ItemInfo info = (ItemInfo) child.getTag();
1079
1080 // We cancel any existing animations
1081 if (mReorderAnimators.containsKey(lp)) {
1082 mReorderAnimators.get(lp).cancel();
1083 mReorderAnimators.remove(lp);
1084 }
1085
Adam Cohen482ed822012-03-02 14:15:13 -08001086 final int oldX = lp.x;
1087 final int oldY = lp.y;
1088 if (adjustOccupied) {
1089 occupied[lp.cellX][lp.cellY] = false;
1090 occupied[cellX][cellY] = true;
1091 }
Adam Cohenbfbfd262011-06-13 16:55:12 -07001092 lp.isLockedToGrid = true;
Adam Cohen482ed822012-03-02 14:15:13 -08001093 if (permanent) {
1094 lp.cellX = info.cellX = cellX;
1095 lp.cellY = info.cellY = cellY;
1096 } else {
1097 lp.tmpCellX = cellX;
1098 lp.tmpCellY = cellY;
1099 }
Adam Cohenbfbfd262011-06-13 16:55:12 -07001100 clc.setupLp(lp);
1101 lp.isLockedToGrid = false;
Adam Cohen482ed822012-03-02 14:15:13 -08001102 final int newX = lp.x;
1103 final int newY = lp.y;
Adam Cohenbfbfd262011-06-13 16:55:12 -07001104
Adam Cohen76fc0852011-06-17 13:26:23 -07001105 lp.x = oldX;
1106 lp.y = oldY;
Adam Cohen76fc0852011-06-17 13:26:23 -07001107
Adam Cohen482ed822012-03-02 14:15:13 -08001108 // Exit early if we're not actually moving the view
1109 if (oldX == newX && oldY == newY) {
1110 lp.isLockedToGrid = true;
1111 return true;
1112 }
1113
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001114 ValueAnimator va = LauncherAnimUtils.ofFloat(child, 0f, 1f);
Adam Cohen482ed822012-03-02 14:15:13 -08001115 va.setDuration(duration);
1116 mReorderAnimators.put(lp, va);
1117
1118 va.addUpdateListener(new AnimatorUpdateListener() {
1119 @Override
Adam Cohenbfbfd262011-06-13 16:55:12 -07001120 public void onAnimationUpdate(ValueAnimator animation) {
Adam Cohen482ed822012-03-02 14:15:13 -08001121 float r = ((Float) animation.getAnimatedValue()).floatValue();
Adam Cohen19f37922012-03-21 11:59:11 -07001122 lp.x = (int) ((1 - r) * oldX + r * newX);
1123 lp.y = (int) ((1 - r) * oldY + r * newY);
Adam Cohen6b8a02d2012-03-22 15:13:40 -07001124 child.requestLayout();
Adam Cohenbfbfd262011-06-13 16:55:12 -07001125 }
1126 });
Adam Cohen482ed822012-03-02 14:15:13 -08001127 va.addListener(new AnimatorListenerAdapter() {
Adam Cohenbfbfd262011-06-13 16:55:12 -07001128 boolean cancelled = false;
1129 public void onAnimationEnd(Animator animation) {
1130 // If the animation was cancelled, it means that another animation
1131 // has interrupted this one, and we don't want to lock the item into
1132 // place just yet.
1133 if (!cancelled) {
1134 lp.isLockedToGrid = true;
Adam Cohen482ed822012-03-02 14:15:13 -08001135 child.requestLayout();
Adam Cohenbfbfd262011-06-13 16:55:12 -07001136 }
1137 if (mReorderAnimators.containsKey(lp)) {
1138 mReorderAnimators.remove(lp);
1139 }
1140 }
1141 public void onAnimationCancel(Animator animation) {
1142 cancelled = true;
1143 }
1144 });
Adam Cohen482ed822012-03-02 14:15:13 -08001145 va.setStartDelay(delay);
1146 va.start();
Adam Cohenbfbfd262011-06-13 16:55:12 -07001147 return true;
1148 }
1149 return false;
1150 }
1151
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001152 /**
1153 * Estimate where the top left cell of the dragged item will land if it is dropped.
1154 *
1155 * @param originX The X value of the top left corner of the item
1156 * @param originY The Y value of the top left corner of the item
1157 * @param spanX The number of horizontal cells that the item spans
1158 * @param spanY The number of vertical cells that the item spans
1159 * @param result The estimated drop cell X and Y.
1160 */
1161 void estimateDropCell(int originX, int originY, int spanX, int spanY, int[] result) {
Adam Cohend22015c2010-07-26 22:02:18 -07001162 final int countX = mCountX;
1163 final int countY = mCountY;
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001164
Michael Jurkaa63c4522010-08-19 13:52:27 -07001165 // pointToCellRounded takes the top left of a cell but will pad that with
1166 // cellWidth/2 and cellHeight/2 when finding the matching cell
1167 pointToCellRounded(originX, originY, result);
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001168
1169 // If the item isn't fully on this screen, snap to the edges
1170 int rightOverhang = result[0] + spanX - countX;
1171 if (rightOverhang > 0) {
1172 result[0] -= rightOverhang; // Snap to right
1173 }
1174 result[0] = Math.max(0, result[0]); // Snap to left
1175 int bottomOverhang = result[1] + spanY - countY;
1176 if (bottomOverhang > 0) {
1177 result[1] -= bottomOverhang; // Snap to bottom
1178 }
1179 result[1] = Math.max(0, result[1]); // Snap to top
1180 }
1181
Adam Cohen482ed822012-03-02 14:15:13 -08001182 void visualizeDropLocation(View v, Bitmap dragOutline, int originX, int originY, int cellX,
1183 int cellY, int spanX, int spanY, boolean resize, Point dragOffset, Rect dragRegion) {
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -07001184 final int oldDragCellX = mDragCell[0];
1185 final int oldDragCellY = mDragCell[1];
Adam Cohen482ed822012-03-02 14:15:13 -08001186
Winson Chungb8c69f32011-10-19 21:36:08 -07001187 if (v != null && dragOffset == null) {
Winson Chunga9abd0e2010-10-27 17:18:37 -07001188 mDragCenter.set(originX + (v.getWidth() / 2), originY + (v.getHeight() / 2));
1189 } else {
1190 mDragCenter.set(originX, originY);
1191 }
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001192
Adam Cohen2801caf2011-05-13 20:57:39 -07001193 if (dragOutline == null && v == null) {
Adam Cohen2801caf2011-05-13 20:57:39 -07001194 return;
1195 }
1196
Adam Cohen482ed822012-03-02 14:15:13 -08001197 if (cellX != oldDragCellX || cellY != oldDragCellY) {
1198 mDragCell[0] = cellX;
1199 mDragCell[1] = cellY;
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001200 // Find the top left corner of the rect the object will occupy
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001201 final int[] topLeft = mTmpPoint;
Adam Cohen482ed822012-03-02 14:15:13 -08001202 cellToPoint(cellX, cellY, topLeft);
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001203
Joe Onorato4be866d2010-10-10 11:26:02 -07001204 int left = topLeft[0];
1205 int top = topLeft[1];
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001206
Winson Chungb8c69f32011-10-19 21:36:08 -07001207 if (v != null && dragOffset == null) {
Adam Cohen99e8b402011-03-25 19:23:43 -07001208 // When drawing the drag outline, it did not account for margin offsets
1209 // added by the view's parent.
1210 MarginLayoutParams lp = (MarginLayoutParams) v.getLayoutParams();
1211 left += lp.leftMargin;
1212 top += lp.topMargin;
Winson Chung150fbab2010-09-29 17:14:26 -07001213
Adam Cohen99e8b402011-03-25 19:23:43 -07001214 // Offsets due to the size difference between the View and the dragOutline.
1215 // There is a size difference to account for the outer blur, which may lie
1216 // outside the bounds of the view.
Winson Chunga9abd0e2010-10-27 17:18:37 -07001217 top += (v.getHeight() - dragOutline.getHeight()) / 2;
Adam Cohenae915ce2011-08-25 13:47:22 -07001218 // We center about the x axis
1219 left += ((mCellWidth * spanX) + ((spanX - 1) * mWidthGap)
1220 - dragOutline.getWidth()) / 2;
Adam Cohen66396872011-04-15 17:50:36 -07001221 } else {
Winson Chungb8c69f32011-10-19 21:36:08 -07001222 if (dragOffset != null && dragRegion != null) {
1223 // Center the drag region *horizontally* in the cell and apply a drag
1224 // outline offset
1225 left += dragOffset.x + ((mCellWidth * spanX) + ((spanX - 1) * mWidthGap)
1226 - dragRegion.width()) / 2;
1227 top += dragOffset.y;
1228 } else {
1229 // Center the drag outline in the cell
1230 left += ((mCellWidth * spanX) + ((spanX - 1) * mWidthGap)
1231 - dragOutline.getWidth()) / 2;
1232 top += ((mCellHeight * spanY) + ((spanY - 1) * mHeightGap)
1233 - dragOutline.getHeight()) / 2;
1234 }
Winson Chunga9abd0e2010-10-27 17:18:37 -07001235 }
Joe Onorato4be866d2010-10-10 11:26:02 -07001236 final int oldIndex = mDragOutlineCurrent;
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -07001237 mDragOutlineAnims[oldIndex].animateOut();
1238 mDragOutlineCurrent = (oldIndex + 1) % mDragOutlines.length;
Adam Cohend41fbf52012-02-16 23:53:59 -08001239 Rect r = mDragOutlines[mDragOutlineCurrent];
1240 r.set(left, top, left + dragOutline.getWidth(), top + dragOutline.getHeight());
1241 if (resize) {
Adam Cohen482ed822012-03-02 14:15:13 -08001242 cellToRect(cellX, cellY, spanX, spanY, r);
Adam Cohend41fbf52012-02-16 23:53:59 -08001243 }
Winson Chung150fbab2010-09-29 17:14:26 -07001244
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -07001245 mDragOutlineAnims[mDragOutlineCurrent].setTag(dragOutline);
1246 mDragOutlineAnims[mDragOutlineCurrent].animateIn();
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001247 }
1248 }
1249
Adam Cohene0310962011-04-18 16:15:31 -07001250 public void clearDragOutlines() {
1251 final int oldIndex = mDragOutlineCurrent;
1252 mDragOutlineAnims[oldIndex].animateOut();
Adam Cohend41fbf52012-02-16 23:53:59 -08001253 mDragCell[0] = mDragCell[1] = -1;
Adam Cohene0310962011-04-18 16:15:31 -07001254 }
1255
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001256 /**
Jeff Sharkey70864282009-04-07 21:08:40 -07001257 * Find a vacant area that will fit the given bounds nearest the requested
1258 * cell location. Uses Euclidean distance to score multiple vacant areas.
Winson Chungaafa03c2010-06-11 17:34:16 -07001259 *
Romain Guy51afc022009-05-04 18:03:43 -07001260 * @param pixelX The X location at which you want to search for a vacant area.
1261 * @param pixelY The Y location at which you want to search for a vacant area.
Jeff Sharkey70864282009-04-07 21:08:40 -07001262 * @param spanX Horizontal span of the object.
1263 * @param spanY Vertical span of the object.
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001264 * @param result Array in which to place the result, or null (in which case a new array will
1265 * be allocated)
Jeff Sharkey70864282009-04-07 21:08:40 -07001266 * @return The X, Y cell of a vacant area that can contain this object,
1267 * nearest the requested location.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001268 */
Adam Cohend41fbf52012-02-16 23:53:59 -08001269 int[] findNearestVacantArea(int pixelX, int pixelY, int spanX, int spanY,
1270 int[] result) {
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001271 return findNearestVacantArea(pixelX, pixelY, spanX, spanY, null, result);
Michael Jurka6a1435d2010-09-27 17:35:12 -07001272 }
Winson Chungaafa03c2010-06-11 17:34:16 -07001273
Michael Jurka6a1435d2010-09-27 17:35:12 -07001274 /**
1275 * Find a vacant area that will fit the given bounds nearest the requested
1276 * cell location. Uses Euclidean distance to score multiple vacant areas.
1277 *
1278 * @param pixelX The X location at which you want to search for a vacant area.
1279 * @param pixelY The Y location at which you want to search for a vacant area.
Adam Cohend41fbf52012-02-16 23:53:59 -08001280 * @param minSpanX The minimum horizontal span required
1281 * @param minSpanY The minimum vertical span required
1282 * @param spanX Horizontal span of the object.
1283 * @param spanY Vertical span of the object.
1284 * @param result Array in which to place the result, or null (in which case a new array will
1285 * be allocated)
1286 * @return The X, Y cell of a vacant area that can contain this object,
1287 * nearest the requested location.
1288 */
1289 int[] findNearestVacantArea(int pixelX, int pixelY, int minSpanX, int minSpanY, int spanX,
1290 int spanY, int[] result, int[] resultSpan) {
1291 return findNearestVacantArea(pixelX, pixelY, minSpanX, minSpanY, spanX, spanY, null,
1292 result, resultSpan);
1293 }
1294
1295 /**
1296 * Find a vacant area that will fit the given bounds nearest the requested
1297 * cell location. Uses Euclidean distance to score multiple vacant areas.
1298 *
1299 * @param pixelX The X location at which you want to search for a vacant area.
1300 * @param pixelY The Y location at which you want to search for a vacant area.
Michael Jurka6a1435d2010-09-27 17:35:12 -07001301 * @param spanX Horizontal span of the object.
1302 * @param spanY Vertical span of the object.
Adam Cohendf035382011-04-11 17:22:04 -07001303 * @param ignoreOccupied If true, the result can be an occupied cell
1304 * @param result Array in which to place the result, or null (in which case a new array will
1305 * be allocated)
Michael Jurka6a1435d2010-09-27 17:35:12 -07001306 * @return The X, Y cell of a vacant area that can contain this object,
1307 * nearest the requested location.
1308 */
Adam Cohendf035382011-04-11 17:22:04 -07001309 int[] findNearestArea(int pixelX, int pixelY, int spanX, int spanY, View ignoreView,
1310 boolean ignoreOccupied, int[] result) {
Adam Cohend41fbf52012-02-16 23:53:59 -08001311 return findNearestArea(pixelX, pixelY, spanX, spanY,
Adam Cohen482ed822012-03-02 14:15:13 -08001312 spanX, spanY, ignoreView, ignoreOccupied, result, null, mOccupied);
Adam Cohend41fbf52012-02-16 23:53:59 -08001313 }
1314
1315 private final Stack<Rect> mTempRectStack = new Stack<Rect>();
1316 private void lazyInitTempRectStack() {
1317 if (mTempRectStack.isEmpty()) {
1318 for (int i = 0; i < mCountX * mCountY; i++) {
1319 mTempRectStack.push(new Rect());
1320 }
1321 }
1322 }
Adam Cohen482ed822012-03-02 14:15:13 -08001323
Adam Cohend41fbf52012-02-16 23:53:59 -08001324 private void recycleTempRects(Stack<Rect> used) {
1325 while (!used.isEmpty()) {
1326 mTempRectStack.push(used.pop());
1327 }
1328 }
1329
1330 /**
1331 * Find a vacant area that will fit the given bounds nearest the requested
1332 * cell location. Uses Euclidean distance to score multiple vacant areas.
1333 *
1334 * @param pixelX The X location at which you want to search for a vacant area.
1335 * @param pixelY The Y location at which you want to search for a vacant area.
1336 * @param minSpanX The minimum horizontal span required
1337 * @param minSpanY The minimum vertical span required
1338 * @param spanX Horizontal span of the object.
1339 * @param spanY Vertical span of the object.
1340 * @param ignoreOccupied If true, the result can be an occupied cell
1341 * @param result Array in which to place the result, or null (in which case a new array will
1342 * be allocated)
1343 * @return The X, Y cell of a vacant area that can contain this object,
1344 * nearest the requested location.
1345 */
1346 int[] findNearestArea(int pixelX, int pixelY, int minSpanX, int minSpanY, int spanX, int spanY,
Adam Cohen482ed822012-03-02 14:15:13 -08001347 View ignoreView, boolean ignoreOccupied, int[] result, int[] resultSpan,
1348 boolean[][] occupied) {
Adam Cohend41fbf52012-02-16 23:53:59 -08001349 lazyInitTempRectStack();
Michael Jurkac6ee42e2010-09-30 12:04:50 -07001350 // mark space take by ignoreView as available (method checks if ignoreView is null)
Adam Cohen482ed822012-03-02 14:15:13 -08001351 markCellsAsUnoccupiedForView(ignoreView, occupied);
Michael Jurkac6ee42e2010-09-30 12:04:50 -07001352
Adam Cohene3e27a82011-04-15 12:07:39 -07001353 // For items with a spanX / spanY > 1, the passed in point (pixelX, pixelY) corresponds
1354 // to the center of the item, but we are searching based on the top-left cell, so
1355 // we translate the point over to correspond to the top-left.
1356 pixelX -= (mCellWidth + mWidthGap) * (spanX - 1) / 2f;
1357 pixelY -= (mCellHeight + mHeightGap) * (spanY - 1) / 2f;
1358
Jeff Sharkey70864282009-04-07 21:08:40 -07001359 // Keep track of best-scoring drop area
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001360 final int[] bestXY = result != null ? result : new int[2];
Jeff Sharkey70864282009-04-07 21:08:40 -07001361 double bestDistance = Double.MAX_VALUE;
Adam Cohend41fbf52012-02-16 23:53:59 -08001362 final Rect bestRect = new Rect(-1, -1, -1, -1);
1363 final Stack<Rect> validRegions = new Stack<Rect>();
Winson Chungaafa03c2010-06-11 17:34:16 -07001364
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001365 final int countX = mCountX;
1366 final int countY = mCountY;
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001367
Adam Cohend41fbf52012-02-16 23:53:59 -08001368 if (minSpanX <= 0 || minSpanY <= 0 || spanX <= 0 || spanY <= 0 ||
1369 spanX < minSpanX || spanY < minSpanY) {
1370 return bestXY;
1371 }
1372
1373 for (int y = 0; y < countY - (minSpanY - 1); y++) {
Michael Jurkac28de512010-08-13 11:27:44 -07001374 inner:
Adam Cohend41fbf52012-02-16 23:53:59 -08001375 for (int x = 0; x < countX - (minSpanX - 1); x++) {
1376 int ySize = -1;
1377 int xSize = -1;
Adam Cohendf035382011-04-11 17:22:04 -07001378 if (ignoreOccupied) {
Adam Cohend41fbf52012-02-16 23:53:59 -08001379 // First, let's see if this thing fits anywhere
1380 for (int i = 0; i < minSpanX; i++) {
1381 for (int j = 0; j < minSpanY; j++) {
Adam Cohendf035382011-04-11 17:22:04 -07001382 if (occupied[x + i][y + j]) {
Adam Cohendf035382011-04-11 17:22:04 -07001383 continue inner;
1384 }
Michael Jurkac28de512010-08-13 11:27:44 -07001385 }
1386 }
Adam Cohend41fbf52012-02-16 23:53:59 -08001387 xSize = minSpanX;
1388 ySize = minSpanY;
1389
1390 // We know that the item will fit at _some_ acceptable size, now let's see
1391 // how big we can make it. We'll alternate between incrementing x and y spans
1392 // until we hit a limit.
1393 boolean incX = true;
1394 boolean hitMaxX = xSize >= spanX;
1395 boolean hitMaxY = ySize >= spanY;
1396 while (!(hitMaxX && hitMaxY)) {
1397 if (incX && !hitMaxX) {
1398 for (int j = 0; j < ySize; j++) {
1399 if (x + xSize > countX -1 || occupied[x + xSize][y + j]) {
1400 // We can't move out horizontally
1401 hitMaxX = true;
1402 }
1403 }
1404 if (!hitMaxX) {
1405 xSize++;
1406 }
1407 } else if (!hitMaxY) {
1408 for (int i = 0; i < xSize; i++) {
1409 if (y + ySize > countY - 1 || occupied[x + i][y + ySize]) {
1410 // We can't move out vertically
1411 hitMaxY = true;
1412 }
1413 }
1414 if (!hitMaxY) {
1415 ySize++;
1416 }
1417 }
1418 hitMaxX |= xSize >= spanX;
1419 hitMaxY |= ySize >= spanY;
1420 incX = !incX;
1421 }
1422 incX = true;
1423 hitMaxX = xSize >= spanX;
1424 hitMaxY = ySize >= spanY;
Michael Jurkac28de512010-08-13 11:27:44 -07001425 }
Winson Chung0be025d2011-05-23 17:45:09 -07001426 final int[] cellXY = mTmpXY;
Adam Cohene3e27a82011-04-15 12:07:39 -07001427 cellToCenterPoint(x, y, cellXY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001428
Adam Cohend41fbf52012-02-16 23:53:59 -08001429 // We verify that the current rect is not a sub-rect of any of our previous
1430 // candidates. In this case, the current rect is disqualified in favour of the
1431 // containing rect.
1432 Rect currentRect = mTempRectStack.pop();
1433 currentRect.set(x, y, x + xSize, y + ySize);
1434 boolean contained = false;
1435 for (Rect r : validRegions) {
1436 if (r.contains(currentRect)) {
1437 contained = true;
1438 break;
1439 }
1440 }
1441 validRegions.push(currentRect);
Michael Jurkac28de512010-08-13 11:27:44 -07001442 double distance = Math.sqrt(Math.pow(cellXY[0] - pixelX, 2)
1443 + Math.pow(cellXY[1] - pixelY, 2));
Adam Cohen482ed822012-03-02 14:15:13 -08001444
Adam Cohend41fbf52012-02-16 23:53:59 -08001445 if ((distance <= bestDistance && !contained) ||
1446 currentRect.contains(bestRect)) {
Michael Jurkac28de512010-08-13 11:27:44 -07001447 bestDistance = distance;
1448 bestXY[0] = x;
1449 bestXY[1] = y;
Adam Cohend41fbf52012-02-16 23:53:59 -08001450 if (resultSpan != null) {
1451 resultSpan[0] = xSize;
1452 resultSpan[1] = ySize;
1453 }
1454 bestRect.set(currentRect);
Michael Jurkac28de512010-08-13 11:27:44 -07001455 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001456 }
1457 }
Michael Jurkac6ee42e2010-09-30 12:04:50 -07001458 // re-mark space taken by ignoreView as occupied
Adam Cohen482ed822012-03-02 14:15:13 -08001459 markCellsAsOccupiedForView(ignoreView, occupied);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001460
Adam Cohenc0dcf592011-06-01 15:30:43 -07001461 // Return -1, -1 if no suitable location found
1462 if (bestDistance == Double.MAX_VALUE) {
1463 bestXY[0] = -1;
1464 bestXY[1] = -1;
Jeff Sharkey70864282009-04-07 21:08:40 -07001465 }
Adam Cohend41fbf52012-02-16 23:53:59 -08001466 recycleTempRects(validRegions);
Adam Cohenc0dcf592011-06-01 15:30:43 -07001467 return bestXY;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001468 }
Winson Chungaafa03c2010-06-11 17:34:16 -07001469
Adam Cohen482ed822012-03-02 14:15:13 -08001470 /**
1471 * Find a vacant area that will fit the given bounds nearest the requested
1472 * cell location, and will also weigh in a suggested direction vector of the
1473 * desired location. This method computers distance based on unit grid distances,
1474 * not pixel distances.
1475 *
Adam Cohen47a876d2012-03-19 13:21:41 -07001476 * @param cellX The X cell nearest to which you want to search for a vacant area.
1477 * @param cellY The Y cell nearest which you want to search for a vacant area.
Adam Cohen482ed822012-03-02 14:15:13 -08001478 * @param spanX Horizontal span of the object.
1479 * @param spanY Vertical span of the object.
Adam Cohen47a876d2012-03-19 13:21:41 -07001480 * @param direction The favored direction in which the views should move from x, y
1481 * @param exactDirectionOnly If this parameter is true, then only solutions where the direction
1482 * matches exactly. Otherwise we find the best matching direction.
1483 * @param occoupied The array which represents which cells in the CellLayout are occupied
1484 * @param blockOccupied The array which represents which cells in the specified block (cellX,
Winson Chung5f8afe62013-08-12 16:19:28 -07001485 * cellY, spanX, spanY) are occupied. This is used when try to move a group of views.
Adam Cohen482ed822012-03-02 14:15:13 -08001486 * @param result Array in which to place the result, or null (in which case a new array will
1487 * be allocated)
1488 * @return The X, Y cell of a vacant area that can contain this object,
1489 * nearest the requested location.
1490 */
1491 private int[] findNearestArea(int cellX, int cellY, int spanX, int spanY, int[] direction,
Adam Cohen47a876d2012-03-19 13:21:41 -07001492 boolean[][] occupied, boolean blockOccupied[][], int[] result) {
Adam Cohen482ed822012-03-02 14:15:13 -08001493 // Keep track of best-scoring drop area
1494 final int[] bestXY = result != null ? result : new int[2];
1495 float bestDistance = Float.MAX_VALUE;
1496 int bestDirectionScore = Integer.MIN_VALUE;
1497
1498 final int countX = mCountX;
1499 final int countY = mCountY;
1500
1501 for (int y = 0; y < countY - (spanY - 1); y++) {
1502 inner:
1503 for (int x = 0; x < countX - (spanX - 1); x++) {
1504 // First, let's see if this thing fits anywhere
1505 for (int i = 0; i < spanX; i++) {
1506 for (int j = 0; j < spanY; j++) {
Adam Cohen47a876d2012-03-19 13:21:41 -07001507 if (occupied[x + i][y + j] && (blockOccupied == null || blockOccupied[i][j])) {
Adam Cohen482ed822012-03-02 14:15:13 -08001508 continue inner;
1509 }
1510 }
1511 }
1512
1513 float distance = (float)
1514 Math.sqrt((x - cellX) * (x - cellX) + (y - cellY) * (y - cellY));
1515 int[] curDirection = mTmpPoint;
Adam Cohen47a876d2012-03-19 13:21:41 -07001516 computeDirectionVector(x - cellX, y - cellY, curDirection);
1517 // The direction score is just the dot product of the two candidate direction
1518 // and that passed in.
Adam Cohen482ed822012-03-02 14:15:13 -08001519 int curDirectionScore = direction[0] * curDirection[0] +
1520 direction[1] * curDirection[1];
Adam Cohen47a876d2012-03-19 13:21:41 -07001521 boolean exactDirectionOnly = false;
1522 boolean directionMatches = direction[0] == curDirection[0] &&
1523 direction[0] == curDirection[0];
1524 if ((directionMatches || !exactDirectionOnly) &&
1525 Float.compare(distance, bestDistance) < 0 || (Float.compare(distance,
Adam Cohen482ed822012-03-02 14:15:13 -08001526 bestDistance) == 0 && curDirectionScore > bestDirectionScore)) {
1527 bestDistance = distance;
1528 bestDirectionScore = curDirectionScore;
1529 bestXY[0] = x;
1530 bestXY[1] = y;
1531 }
1532 }
1533 }
1534
1535 // Return -1, -1 if no suitable location found
1536 if (bestDistance == Float.MAX_VALUE) {
1537 bestXY[0] = -1;
1538 bestXY[1] = -1;
1539 }
1540 return bestXY;
1541 }
1542
1543 private boolean addViewToTempLocation(View v, Rect rectOccupiedByPotentialDrop,
Adam Cohen8baab352012-03-20 17:39:21 -07001544 int[] direction, ItemConfiguration currentState) {
1545 CellAndSpan c = currentState.map.get(v);
Adam Cohen482ed822012-03-02 14:15:13 -08001546 boolean success = false;
Adam Cohen8baab352012-03-20 17:39:21 -07001547 markCellsForView(c.x, c.y, c.spanX, c.spanY, mTmpOccupied, false);
Adam Cohen482ed822012-03-02 14:15:13 -08001548 markCellsForRect(rectOccupiedByPotentialDrop, mTmpOccupied, true);
1549
Adam Cohen8baab352012-03-20 17:39:21 -07001550 findNearestArea(c.x, c.y, c.spanX, c.spanY, direction, mTmpOccupied, null, mTempLocation);
Adam Cohen482ed822012-03-02 14:15:13 -08001551
1552 if (mTempLocation[0] >= 0 && mTempLocation[1] >= 0) {
Adam Cohen8baab352012-03-20 17:39:21 -07001553 c.x = mTempLocation[0];
1554 c.y = mTempLocation[1];
Adam Cohen482ed822012-03-02 14:15:13 -08001555 success = true;
Adam Cohen482ed822012-03-02 14:15:13 -08001556 }
Adam Cohen8baab352012-03-20 17:39:21 -07001557 markCellsForView(c.x, c.y, c.spanX, c.spanY, mTmpOccupied, true);
Adam Cohen482ed822012-03-02 14:15:13 -08001558 return success;
1559 }
1560
Adam Cohenf3900c22012-11-16 18:28:11 -08001561 /**
1562 * This helper class defines a cluster of views. It helps with defining complex edges
1563 * of the cluster and determining how those edges interact with other views. The edges
1564 * essentially define a fine-grained boundary around the cluster of views -- like a more
1565 * precise version of a bounding box.
1566 */
1567 private class ViewCluster {
1568 final static int LEFT = 0;
1569 final static int TOP = 1;
1570 final static int RIGHT = 2;
1571 final static int BOTTOM = 3;
Adam Cohen47a876d2012-03-19 13:21:41 -07001572
Adam Cohenf3900c22012-11-16 18:28:11 -08001573 ArrayList<View> views;
1574 ItemConfiguration config;
1575 Rect boundingRect = new Rect();
Adam Cohen47a876d2012-03-19 13:21:41 -07001576
Adam Cohenf3900c22012-11-16 18:28:11 -08001577 int[] leftEdge = new int[mCountY];
1578 int[] rightEdge = new int[mCountY];
1579 int[] topEdge = new int[mCountX];
1580 int[] bottomEdge = new int[mCountX];
1581 boolean leftEdgeDirty, rightEdgeDirty, topEdgeDirty, bottomEdgeDirty, boundingRectDirty;
1582
1583 @SuppressWarnings("unchecked")
1584 public ViewCluster(ArrayList<View> views, ItemConfiguration config) {
1585 this.views = (ArrayList<View>) views.clone();
1586 this.config = config;
1587 resetEdges();
Adam Cohen47a876d2012-03-19 13:21:41 -07001588 }
1589
Adam Cohenf3900c22012-11-16 18:28:11 -08001590 void resetEdges() {
1591 for (int i = 0; i < mCountX; i++) {
1592 topEdge[i] = -1;
1593 bottomEdge[i] = -1;
1594 }
1595 for (int i = 0; i < mCountY; i++) {
1596 leftEdge[i] = -1;
1597 rightEdge[i] = -1;
1598 }
1599 leftEdgeDirty = true;
1600 rightEdgeDirty = true;
1601 bottomEdgeDirty = true;
1602 topEdgeDirty = true;
1603 boundingRectDirty = true;
1604 }
Adam Cohen47a876d2012-03-19 13:21:41 -07001605
Adam Cohenf3900c22012-11-16 18:28:11 -08001606 void computeEdge(int which, int[] edge) {
1607 int count = views.size();
1608 for (int i = 0; i < count; i++) {
1609 CellAndSpan cs = config.map.get(views.get(i));
1610 switch (which) {
1611 case LEFT:
1612 int left = cs.x;
1613 for (int j = cs.y; j < cs.y + cs.spanY; j++) {
1614 if (left < edge[j] || edge[j] < 0) {
1615 edge[j] = left;
Adam Cohena56dc102012-07-13 13:41:42 -07001616 }
Adam Cohen47a876d2012-03-19 13:21:41 -07001617 }
Adam Cohenf3900c22012-11-16 18:28:11 -08001618 break;
1619 case RIGHT:
1620 int right = cs.x + cs.spanX;
1621 for (int j = cs.y; j < cs.y + cs.spanY; j++) {
1622 if (right > edge[j]) {
1623 edge[j] = right;
1624 }
1625 }
1626 break;
1627 case TOP:
1628 int top = cs.y;
1629 for (int j = cs.x; j < cs.x + cs.spanX; j++) {
1630 if (top < edge[j] || edge[j] < 0) {
1631 edge[j] = top;
1632 }
1633 }
1634 break;
1635 case BOTTOM:
1636 int bottom = cs.y + cs.spanY;
1637 for (int j = cs.x; j < cs.x + cs.spanX; j++) {
1638 if (bottom > edge[j]) {
1639 edge[j] = bottom;
1640 }
1641 }
1642 break;
Adam Cohen47a876d2012-03-19 13:21:41 -07001643 }
1644 }
1645 }
Adam Cohenf3900c22012-11-16 18:28:11 -08001646
1647 boolean isViewTouchingEdge(View v, int whichEdge) {
1648 CellAndSpan cs = config.map.get(v);
1649
1650 int[] edge = getEdge(whichEdge);
1651
1652 switch (whichEdge) {
1653 case LEFT:
1654 for (int i = cs.y; i < cs.y + cs.spanY; i++) {
1655 if (edge[i] == cs.x + cs.spanX) {
1656 return true;
1657 }
1658 }
1659 break;
1660 case RIGHT:
1661 for (int i = cs.y; i < cs.y + cs.spanY; i++) {
1662 if (edge[i] == cs.x) {
1663 return true;
1664 }
1665 }
1666 break;
1667 case TOP:
1668 for (int i = cs.x; i < cs.x + cs.spanX; i++) {
1669 if (edge[i] == cs.y + cs.spanY) {
1670 return true;
1671 }
1672 }
1673 break;
1674 case BOTTOM:
1675 for (int i = cs.x; i < cs.x + cs.spanX; i++) {
1676 if (edge[i] == cs.y) {
1677 return true;
1678 }
1679 }
1680 break;
1681 }
1682 return false;
1683 }
1684
1685 void shift(int whichEdge, int delta) {
1686 for (View v: views) {
1687 CellAndSpan c = config.map.get(v);
1688 switch (whichEdge) {
1689 case LEFT:
1690 c.x -= delta;
1691 break;
1692 case RIGHT:
1693 c.x += delta;
1694 break;
1695 case TOP:
1696 c.y -= delta;
1697 break;
1698 case BOTTOM:
1699 default:
1700 c.y += delta;
1701 break;
1702 }
1703 }
1704 resetEdges();
1705 }
1706
1707 public void addView(View v) {
1708 views.add(v);
1709 resetEdges();
1710 }
1711
1712 public Rect getBoundingRect() {
1713 if (boundingRectDirty) {
1714 boolean first = true;
1715 for (View v: views) {
1716 CellAndSpan c = config.map.get(v);
1717 if (first) {
1718 boundingRect.set(c.x, c.y, c.x + c.spanX, c.y + c.spanY);
1719 first = false;
1720 } else {
1721 boundingRect.union(c.x, c.y, c.x + c.spanX, c.y + c.spanY);
1722 }
1723 }
1724 }
1725 return boundingRect;
1726 }
1727
1728 public int[] getEdge(int which) {
1729 switch (which) {
1730 case LEFT:
1731 return getLeftEdge();
1732 case RIGHT:
1733 return getRightEdge();
1734 case TOP:
1735 return getTopEdge();
1736 case BOTTOM:
1737 default:
1738 return getBottomEdge();
1739 }
1740 }
1741
1742 public int[] getLeftEdge() {
1743 if (leftEdgeDirty) {
1744 computeEdge(LEFT, leftEdge);
1745 }
1746 return leftEdge;
1747 }
1748
1749 public int[] getRightEdge() {
1750 if (rightEdgeDirty) {
1751 computeEdge(RIGHT, rightEdge);
1752 }
1753 return rightEdge;
1754 }
1755
1756 public int[] getTopEdge() {
1757 if (topEdgeDirty) {
1758 computeEdge(TOP, topEdge);
1759 }
1760 return topEdge;
1761 }
1762
1763 public int[] getBottomEdge() {
1764 if (bottomEdgeDirty) {
1765 computeEdge(BOTTOM, bottomEdge);
1766 }
1767 return bottomEdge;
1768 }
1769
1770 PositionComparator comparator = new PositionComparator();
1771 class PositionComparator implements Comparator<View> {
1772 int whichEdge = 0;
1773 public int compare(View left, View right) {
1774 CellAndSpan l = config.map.get(left);
1775 CellAndSpan r = config.map.get(right);
1776 switch (whichEdge) {
1777 case LEFT:
1778 return (r.x + r.spanX) - (l.x + l.spanX);
1779 case RIGHT:
1780 return l.x - r.x;
1781 case TOP:
1782 return (r.y + r.spanY) - (l.y + l.spanY);
1783 case BOTTOM:
1784 default:
1785 return l.y - r.y;
1786 }
1787 }
1788 }
1789
1790 public void sortConfigurationForEdgePush(int edge) {
1791 comparator.whichEdge = edge;
1792 Collections.sort(config.sortedViews, comparator);
1793 }
Adam Cohen47a876d2012-03-19 13:21:41 -07001794 }
1795
Adam Cohenf3900c22012-11-16 18:28:11 -08001796 private boolean pushViewsToTempLocation(ArrayList<View> views, Rect rectOccupiedByPotentialDrop,
1797 int[] direction, View dragView, ItemConfiguration currentState) {
Adam Cohene0489502012-08-27 15:18:53 -07001798
Adam Cohenf3900c22012-11-16 18:28:11 -08001799 ViewCluster cluster = new ViewCluster(views, currentState);
1800 Rect clusterRect = cluster.getBoundingRect();
1801 int whichEdge;
1802 int pushDistance;
1803 boolean fail = false;
1804
1805 // Determine the edge of the cluster that will be leading the push and how far
1806 // the cluster must be shifted.
1807 if (direction[0] < 0) {
1808 whichEdge = ViewCluster.LEFT;
1809 pushDistance = clusterRect.right - rectOccupiedByPotentialDrop.left;
Adam Cohene0489502012-08-27 15:18:53 -07001810 } else if (direction[0] > 0) {
Adam Cohenf3900c22012-11-16 18:28:11 -08001811 whichEdge = ViewCluster.RIGHT;
1812 pushDistance = rectOccupiedByPotentialDrop.right - clusterRect.left;
1813 } else if (direction[1] < 0) {
1814 whichEdge = ViewCluster.TOP;
1815 pushDistance = clusterRect.bottom - rectOccupiedByPotentialDrop.top;
1816 } else {
1817 whichEdge = ViewCluster.BOTTOM;
1818 pushDistance = rectOccupiedByPotentialDrop.bottom - clusterRect.top;
Adam Cohene0489502012-08-27 15:18:53 -07001819 }
1820
Adam Cohenf3900c22012-11-16 18:28:11 -08001821 // Break early for invalid push distance.
1822 if (pushDistance <= 0) {
1823 return false;
Adam Cohene0489502012-08-27 15:18:53 -07001824 }
Adam Cohenf3900c22012-11-16 18:28:11 -08001825
1826 // Mark the occupied state as false for the group of views we want to move.
1827 for (View v: views) {
1828 CellAndSpan c = currentState.map.get(v);
1829 markCellsForView(c.x, c.y, c.spanX, c.spanY, mTmpOccupied, false);
1830 }
1831
1832 // We save the current configuration -- if we fail to find a solution we will revert
1833 // to the initial state. The process of finding a solution modifies the configuration
1834 // in place, hence the need for revert in the failure case.
1835 currentState.save();
1836
1837 // The pushing algorithm is simplified by considering the views in the order in which
1838 // they would be pushed by the cluster. For example, if the cluster is leading with its
1839 // left edge, we consider sort the views by their right edge, from right to left.
1840 cluster.sortConfigurationForEdgePush(whichEdge);
1841
1842 while (pushDistance > 0 && !fail) {
1843 for (View v: currentState.sortedViews) {
1844 // For each view that isn't in the cluster, we see if the leading edge of the
1845 // cluster is contacting the edge of that view. If so, we add that view to the
1846 // cluster.
1847 if (!cluster.views.contains(v) && v != dragView) {
1848 if (cluster.isViewTouchingEdge(v, whichEdge)) {
1849 LayoutParams lp = (LayoutParams) v.getLayoutParams();
1850 if (!lp.canReorder) {
1851 // The push solution includes the all apps button, this is not viable.
1852 fail = true;
1853 break;
1854 }
1855 cluster.addView(v);
1856 CellAndSpan c = currentState.map.get(v);
1857
1858 // Adding view to cluster, mark it as not occupied.
1859 markCellsForView(c.x, c.y, c.spanX, c.spanY, mTmpOccupied, false);
1860 }
1861 }
1862 }
1863 pushDistance--;
1864
1865 // The cluster has been completed, now we move the whole thing over in the appropriate
1866 // direction.
1867 cluster.shift(whichEdge, 1);
1868 }
1869
1870 boolean foundSolution = false;
1871 clusterRect = cluster.getBoundingRect();
1872
1873 // Due to the nature of the algorithm, the only check required to verify a valid solution
1874 // is to ensure that completed shifted cluster lies completely within the cell layout.
1875 if (!fail && clusterRect.left >= 0 && clusterRect.right <= mCountX && clusterRect.top >= 0 &&
1876 clusterRect.bottom <= mCountY) {
1877 foundSolution = true;
1878 } else {
1879 currentState.restore();
1880 }
1881
1882 // In either case, we set the occupied array as marked for the location of the views
1883 for (View v: cluster.views) {
1884 CellAndSpan c = currentState.map.get(v);
1885 markCellsForView(c.x, c.y, c.spanX, c.spanY, mTmpOccupied, true);
1886 }
1887
1888 return foundSolution;
Adam Cohene0489502012-08-27 15:18:53 -07001889 }
1890
Adam Cohen482ed822012-03-02 14:15:13 -08001891 private boolean addViewsToTempLocation(ArrayList<View> views, Rect rectOccupiedByPotentialDrop,
Adam Cohenf3900c22012-11-16 18:28:11 -08001892 int[] direction, View dragView, ItemConfiguration currentState) {
Adam Cohen482ed822012-03-02 14:15:13 -08001893 if (views.size() == 0) return true;
Adam Cohen482ed822012-03-02 14:15:13 -08001894
Adam Cohen8baab352012-03-20 17:39:21 -07001895 boolean success = false;
Adam Cohen482ed822012-03-02 14:15:13 -08001896 Rect boundingRect = null;
Adam Cohen8baab352012-03-20 17:39:21 -07001897 // We construct a rect which represents the entire group of views passed in
Adam Cohen482ed822012-03-02 14:15:13 -08001898 for (View v: views) {
Adam Cohen8baab352012-03-20 17:39:21 -07001899 CellAndSpan c = currentState.map.get(v);
Adam Cohen482ed822012-03-02 14:15:13 -08001900 if (boundingRect == null) {
Adam Cohen8baab352012-03-20 17:39:21 -07001901 boundingRect = new Rect(c.x, c.y, c.x + c.spanX, c.y + c.spanY);
Adam Cohen482ed822012-03-02 14:15:13 -08001902 } else {
Adam Cohen8baab352012-03-20 17:39:21 -07001903 boundingRect.union(c.x, c.y, c.x + c.spanX, c.y + c.spanY);
Adam Cohen482ed822012-03-02 14:15:13 -08001904 }
1905 }
Adam Cohen8baab352012-03-20 17:39:21 -07001906
Adam Cohen8baab352012-03-20 17:39:21 -07001907 // Mark the occupied state as false for the group of views we want to move.
Adam Cohenf3900c22012-11-16 18:28:11 -08001908 for (View v: views) {
Adam Cohen8baab352012-03-20 17:39:21 -07001909 CellAndSpan c = currentState.map.get(v);
1910 markCellsForView(c.x, c.y, c.spanX, c.spanY, mTmpOccupied, false);
1911 }
1912
Adam Cohen47a876d2012-03-19 13:21:41 -07001913 boolean[][] blockOccupied = new boolean[boundingRect.width()][boundingRect.height()];
1914 int top = boundingRect.top;
1915 int left = boundingRect.left;
Adam Cohen8baab352012-03-20 17:39:21 -07001916 // We mark more precisely which parts of the bounding rect are truly occupied, allowing
Adam Cohena56dc102012-07-13 13:41:42 -07001917 // for interlocking.
Adam Cohenf3900c22012-11-16 18:28:11 -08001918 for (View v: views) {
Adam Cohen8baab352012-03-20 17:39:21 -07001919 CellAndSpan c = currentState.map.get(v);
1920 markCellsForView(c.x - left, c.y - top, c.spanX, c.spanY, blockOccupied, true);
Adam Cohen47a876d2012-03-19 13:21:41 -07001921 }
1922
Adam Cohen482ed822012-03-02 14:15:13 -08001923 markCellsForRect(rectOccupiedByPotentialDrop, mTmpOccupied, true);
1924
Adam Cohenf3900c22012-11-16 18:28:11 -08001925 findNearestArea(boundingRect.left, boundingRect.top, boundingRect.width(),
1926 boundingRect.height(), direction, mTmpOccupied, blockOccupied, mTempLocation);
Adam Cohen482ed822012-03-02 14:15:13 -08001927
Adam Cohen8baab352012-03-20 17:39:21 -07001928 // If we successfuly found a location by pushing the block of views, we commit it
Adam Cohen482ed822012-03-02 14:15:13 -08001929 if (mTempLocation[0] >= 0 && mTempLocation[1] >= 0) {
Adam Cohen8baab352012-03-20 17:39:21 -07001930 int deltaX = mTempLocation[0] - boundingRect.left;
1931 int deltaY = mTempLocation[1] - boundingRect.top;
Adam Cohenf3900c22012-11-16 18:28:11 -08001932 for (View v: views) {
Adam Cohen8baab352012-03-20 17:39:21 -07001933 CellAndSpan c = currentState.map.get(v);
1934 c.x += deltaX;
1935 c.y += deltaY;
Adam Cohen482ed822012-03-02 14:15:13 -08001936 }
1937 success = true;
1938 }
Adam Cohen8baab352012-03-20 17:39:21 -07001939
1940 // In either case, we set the occupied array as marked for the location of the views
Adam Cohenf3900c22012-11-16 18:28:11 -08001941 for (View v: views) {
Adam Cohen8baab352012-03-20 17:39:21 -07001942 CellAndSpan c = currentState.map.get(v);
1943 markCellsForView(c.x, c.y, c.spanX, c.spanY, mTmpOccupied, true);
Adam Cohen482ed822012-03-02 14:15:13 -08001944 }
1945 return success;
1946 }
1947
1948 private void markCellsForRect(Rect r, boolean[][] occupied, boolean value) {
1949 markCellsForView(r.left, r.top, r.width(), r.height(), occupied, value);
1950 }
1951
Adam Cohen4abc5bd2012-05-29 21:06:03 -07001952 // This method tries to find a reordering solution which satisfies the push mechanic by trying
1953 // to push items in each of the cardinal directions, in an order based on the direction vector
1954 // passed.
1955 private boolean attemptPushInDirection(ArrayList<View> intersectingViews, Rect occupied,
1956 int[] direction, View ignoreView, ItemConfiguration solution) {
1957 if ((Math.abs(direction[0]) + Math.abs(direction[1])) > 1) {
Winson Chung5f8afe62013-08-12 16:19:28 -07001958 // If the direction vector has two non-zero components, we try pushing
Adam Cohen4abc5bd2012-05-29 21:06:03 -07001959 // separately in each of the components.
1960 int temp = direction[1];
1961 direction[1] = 0;
Adam Cohenf3900c22012-11-16 18:28:11 -08001962
1963 if (pushViewsToTempLocation(intersectingViews, occupied, direction,
Adam Cohen4abc5bd2012-05-29 21:06:03 -07001964 ignoreView, solution)) {
1965 return true;
1966 }
1967 direction[1] = temp;
1968 temp = direction[0];
1969 direction[0] = 0;
Adam Cohenf3900c22012-11-16 18:28:11 -08001970
1971 if (pushViewsToTempLocation(intersectingViews, occupied, direction,
Adam Cohen4abc5bd2012-05-29 21:06:03 -07001972 ignoreView, solution)) {
1973 return true;
1974 }
1975 // Revert the direction
1976 direction[0] = temp;
1977
1978 // Now we try pushing in each component of the opposite direction
1979 direction[0] *= -1;
1980 direction[1] *= -1;
1981 temp = direction[1];
1982 direction[1] = 0;
Adam Cohenf3900c22012-11-16 18:28:11 -08001983 if (pushViewsToTempLocation(intersectingViews, occupied, direction,
Adam Cohen4abc5bd2012-05-29 21:06:03 -07001984 ignoreView, solution)) {
1985 return true;
1986 }
1987
1988 direction[1] = temp;
1989 temp = direction[0];
1990 direction[0] = 0;
Adam Cohenf3900c22012-11-16 18:28:11 -08001991 if (pushViewsToTempLocation(intersectingViews, occupied, direction,
Adam Cohen4abc5bd2012-05-29 21:06:03 -07001992 ignoreView, solution)) {
1993 return true;
1994 }
1995 // revert the direction
1996 direction[0] = temp;
1997 direction[0] *= -1;
1998 direction[1] *= -1;
Winson Chung5f8afe62013-08-12 16:19:28 -07001999
Adam Cohen4abc5bd2012-05-29 21:06:03 -07002000 } else {
2001 // If the direction vector has a single non-zero component, we push first in the
2002 // direction of the vector
Adam Cohenf3900c22012-11-16 18:28:11 -08002003 if (pushViewsToTempLocation(intersectingViews, occupied, direction,
Adam Cohen4abc5bd2012-05-29 21:06:03 -07002004 ignoreView, solution)) {
2005 return true;
2006 }
Adam Cohen4abc5bd2012-05-29 21:06:03 -07002007 // Then we try the opposite direction
2008 direction[0] *= -1;
2009 direction[1] *= -1;
Adam Cohenf3900c22012-11-16 18:28:11 -08002010 if (pushViewsToTempLocation(intersectingViews, occupied, direction,
Adam Cohen4abc5bd2012-05-29 21:06:03 -07002011 ignoreView, solution)) {
2012 return true;
2013 }
2014 // Switch the direction back
2015 direction[0] *= -1;
2016 direction[1] *= -1;
Winson Chung5f8afe62013-08-12 16:19:28 -07002017
2018 // If we have failed to find a push solution with the above, then we try
Adam Cohen4abc5bd2012-05-29 21:06:03 -07002019 // to find a solution by pushing along the perpendicular axis.
2020
2021 // Swap the components
2022 int temp = direction[1];
2023 direction[1] = direction[0];
2024 direction[0] = temp;
Adam Cohenf3900c22012-11-16 18:28:11 -08002025 if (pushViewsToTempLocation(intersectingViews, occupied, direction,
Adam Cohen4abc5bd2012-05-29 21:06:03 -07002026 ignoreView, solution)) {
2027 return true;
2028 }
2029
2030 // Then we try the opposite direction
2031 direction[0] *= -1;
2032 direction[1] *= -1;
Adam Cohenf3900c22012-11-16 18:28:11 -08002033 if (pushViewsToTempLocation(intersectingViews, occupied, direction,
Adam Cohen4abc5bd2012-05-29 21:06:03 -07002034 ignoreView, solution)) {
2035 return true;
2036 }
2037 // Switch the direction back
2038 direction[0] *= -1;
2039 direction[1] *= -1;
2040
2041 // Swap the components back
2042 temp = direction[1];
2043 direction[1] = direction[0];
2044 direction[0] = temp;
2045 }
2046 return false;
2047 }
2048
Adam Cohen482ed822012-03-02 14:15:13 -08002049 private boolean rearrangementExists(int cellX, int cellY, int spanX, int spanY, int[] direction,
Adam Cohen8baab352012-03-20 17:39:21 -07002050 View ignoreView, ItemConfiguration solution) {
Winson Chunge3e03bc2012-05-01 15:10:11 -07002051 // Return early if get invalid cell positions
2052 if (cellX < 0 || cellY < 0) return false;
Adam Cohen482ed822012-03-02 14:15:13 -08002053
Adam Cohen8baab352012-03-20 17:39:21 -07002054 mIntersectingViews.clear();
Adam Cohen482ed822012-03-02 14:15:13 -08002055 mOccupiedRect.set(cellX, cellY, cellX + spanX, cellY + spanY);
Adam Cohen482ed822012-03-02 14:15:13 -08002056
Adam Cohen8baab352012-03-20 17:39:21 -07002057 // Mark the desired location of the view currently being dragged.
Adam Cohen482ed822012-03-02 14:15:13 -08002058 if (ignoreView != null) {
Adam Cohen8baab352012-03-20 17:39:21 -07002059 CellAndSpan c = solution.map.get(ignoreView);
Adam Cohen19f37922012-03-21 11:59:11 -07002060 if (c != null) {
2061 c.x = cellX;
2062 c.y = cellY;
2063 }
Adam Cohen482ed822012-03-02 14:15:13 -08002064 }
Adam Cohen482ed822012-03-02 14:15:13 -08002065 Rect r0 = new Rect(cellX, cellY, cellX + spanX, cellY + spanY);
2066 Rect r1 = new Rect();
Adam Cohen8baab352012-03-20 17:39:21 -07002067 for (View child: solution.map.keySet()) {
Adam Cohen482ed822012-03-02 14:15:13 -08002068 if (child == ignoreView) continue;
Adam Cohen8baab352012-03-20 17:39:21 -07002069 CellAndSpan c = solution.map.get(child);
Adam Cohen482ed822012-03-02 14:15:13 -08002070 LayoutParams lp = (LayoutParams) child.getLayoutParams();
Adam Cohen8baab352012-03-20 17:39:21 -07002071 r1.set(c.x, c.y, c.x + c.spanX, c.y + c.spanY);
Adam Cohen482ed822012-03-02 14:15:13 -08002072 if (Rect.intersects(r0, r1)) {
2073 if (!lp.canReorder) {
2074 return false;
2075 }
2076 mIntersectingViews.add(child);
2077 }
2078 }
Adam Cohen47a876d2012-03-19 13:21:41 -07002079
Winson Chung5f8afe62013-08-12 16:19:28 -07002080 // First we try to find a solution which respects the push mechanic. That is,
Adam Cohen4abc5bd2012-05-29 21:06:03 -07002081 // we try to find a solution such that no displaced item travels through another item
2082 // without also displacing that item.
2083 if (attemptPushInDirection(mIntersectingViews, mOccupiedRect, direction, ignoreView,
Adam Cohen19f37922012-03-21 11:59:11 -07002084 solution)) {
Adam Cohen47a876d2012-03-19 13:21:41 -07002085 return true;
2086 }
Adam Cohen47a876d2012-03-19 13:21:41 -07002087
Adam Cohen4abc5bd2012-05-29 21:06:03 -07002088 // Next we try moving the views as a block, but without requiring the push mechanic.
Adam Cohenf3900c22012-11-16 18:28:11 -08002089 if (addViewsToTempLocation(mIntersectingViews, mOccupiedRect, direction, ignoreView,
Adam Cohen19f37922012-03-21 11:59:11 -07002090 solution)) {
Adam Cohen482ed822012-03-02 14:15:13 -08002091 return true;
2092 }
Adam Cohen47a876d2012-03-19 13:21:41 -07002093
Adam Cohen482ed822012-03-02 14:15:13 -08002094 // Ok, they couldn't move as a block, let's move them individually
2095 for (View v : mIntersectingViews) {
Adam Cohen8baab352012-03-20 17:39:21 -07002096 if (!addViewToTempLocation(v, mOccupiedRect, direction, solution)) {
Adam Cohen482ed822012-03-02 14:15:13 -08002097 return false;
2098 }
2099 }
2100 return true;
2101 }
2102
2103 /*
2104 * Returns a pair (x, y), where x,y are in {-1, 0, 1} corresponding to vector between
2105 * the provided point and the provided cell
2106 */
Adam Cohen47a876d2012-03-19 13:21:41 -07002107 private void computeDirectionVector(float deltaX, float deltaY, int[] result) {
Adam Cohen482ed822012-03-02 14:15:13 -08002108 double angle = Math.atan(((float) deltaY) / deltaX);
2109
2110 result[0] = 0;
2111 result[1] = 0;
2112 if (Math.abs(Math.cos(angle)) > 0.5f) {
2113 result[0] = (int) Math.signum(deltaX);
2114 }
2115 if (Math.abs(Math.sin(angle)) > 0.5f) {
2116 result[1] = (int) Math.signum(deltaY);
2117 }
2118 }
2119
Adam Cohen8baab352012-03-20 17:39:21 -07002120 private void copyOccupiedArray(boolean[][] occupied) {
2121 for (int i = 0; i < mCountX; i++) {
2122 for (int j = 0; j < mCountY; j++) {
2123 occupied[i][j] = mOccupied[i][j];
2124 }
2125 }
2126 }
2127
Adam Cohen482ed822012-03-02 14:15:13 -08002128 ItemConfiguration simpleSwap(int pixelX, int pixelY, int minSpanX, int minSpanY, int spanX,
2129 int spanY, int[] direction, View dragView, boolean decX, ItemConfiguration solution) {
Adam Cohen8baab352012-03-20 17:39:21 -07002130 // Copy the current state into the solution. This solution will be manipulated as necessary.
2131 copyCurrentStateToSolution(solution, false);
2132 // Copy the current occupied array into the temporary occupied array. This array will be
2133 // manipulated as necessary to find a solution.
2134 copyOccupiedArray(mTmpOccupied);
Adam Cohen482ed822012-03-02 14:15:13 -08002135
2136 // We find the nearest cell into which we would place the dragged item, assuming there's
2137 // nothing in its way.
2138 int result[] = new int[2];
2139 result = findNearestArea(pixelX, pixelY, spanX, spanY, result);
2140
2141 boolean success = false;
2142 // First we try the exact nearest position of the item being dragged,
2143 // we will then want to try to move this around to other neighbouring positions
Adam Cohen8baab352012-03-20 17:39:21 -07002144 success = rearrangementExists(result[0], result[1], spanX, spanY, direction, dragView,
2145 solution);
Adam Cohen482ed822012-03-02 14:15:13 -08002146
2147 if (!success) {
2148 // We try shrinking the widget down to size in an alternating pattern, shrink 1 in
2149 // x, then 1 in y etc.
2150 if (spanX > minSpanX && (minSpanY == spanY || decX)) {
2151 return simpleSwap(pixelX, pixelY, minSpanX, minSpanY, spanX - 1, spanY, direction,
2152 dragView, false, solution);
2153 } else if (spanY > minSpanY) {
2154 return simpleSwap(pixelX, pixelY, minSpanX, minSpanY, spanX, spanY - 1, direction,
2155 dragView, true, solution);
2156 }
2157 solution.isSolution = false;
2158 } else {
2159 solution.isSolution = true;
2160 solution.dragViewX = result[0];
2161 solution.dragViewY = result[1];
2162 solution.dragViewSpanX = spanX;
2163 solution.dragViewSpanY = spanY;
Adam Cohen482ed822012-03-02 14:15:13 -08002164 }
2165 return solution;
2166 }
2167
2168 private void copyCurrentStateToSolution(ItemConfiguration solution, boolean temp) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07002169 int childCount = mShortcutsAndWidgets.getChildCount();
Adam Cohen482ed822012-03-02 14:15:13 -08002170 for (int i = 0; i < childCount; i++) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07002171 View child = mShortcutsAndWidgets.getChildAt(i);
Adam Cohen482ed822012-03-02 14:15:13 -08002172 LayoutParams lp = (LayoutParams) child.getLayoutParams();
Adam Cohen8baab352012-03-20 17:39:21 -07002173 CellAndSpan c;
Adam Cohen482ed822012-03-02 14:15:13 -08002174 if (temp) {
Adam Cohen8baab352012-03-20 17:39:21 -07002175 c = new CellAndSpan(lp.tmpCellX, lp.tmpCellY, lp.cellHSpan, lp.cellVSpan);
Adam Cohen482ed822012-03-02 14:15:13 -08002176 } else {
Adam Cohen8baab352012-03-20 17:39:21 -07002177 c = new CellAndSpan(lp.cellX, lp.cellY, lp.cellHSpan, lp.cellVSpan);
Adam Cohen482ed822012-03-02 14:15:13 -08002178 }
Adam Cohenf3900c22012-11-16 18:28:11 -08002179 solution.add(child, c);
Adam Cohen482ed822012-03-02 14:15:13 -08002180 }
2181 }
2182
2183 private void copySolutionToTempState(ItemConfiguration solution, View dragView) {
2184 for (int i = 0; i < mCountX; i++) {
2185 for (int j = 0; j < mCountY; j++) {
2186 mTmpOccupied[i][j] = false;
2187 }
2188 }
2189
Michael Jurkaa52570f2012-03-20 03:18:20 -07002190 int childCount = mShortcutsAndWidgets.getChildCount();
Adam Cohen482ed822012-03-02 14:15:13 -08002191 for (int i = 0; i < childCount; i++) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07002192 View child = mShortcutsAndWidgets.getChildAt(i);
Adam Cohen482ed822012-03-02 14:15:13 -08002193 if (child == dragView) continue;
2194 LayoutParams lp = (LayoutParams) child.getLayoutParams();
Adam Cohen8baab352012-03-20 17:39:21 -07002195 CellAndSpan c = solution.map.get(child);
2196 if (c != null) {
2197 lp.tmpCellX = c.x;
2198 lp.tmpCellY = c.y;
2199 lp.cellHSpan = c.spanX;
2200 lp.cellVSpan = c.spanY;
2201 markCellsForView(c.x, c.y, c.spanX, c.spanY, mTmpOccupied, true);
Adam Cohen482ed822012-03-02 14:15:13 -08002202 }
2203 }
2204 markCellsForView(solution.dragViewX, solution.dragViewY, solution.dragViewSpanX,
2205 solution.dragViewSpanY, mTmpOccupied, true);
2206 }
2207
2208 private void animateItemsToSolution(ItemConfiguration solution, View dragView, boolean
2209 commitDragView) {
2210
2211 boolean[][] occupied = DESTRUCTIVE_REORDER ? mOccupied : mTmpOccupied;
2212 for (int i = 0; i < mCountX; i++) {
2213 for (int j = 0; j < mCountY; j++) {
2214 occupied[i][j] = false;
2215 }
2216 }
2217
Michael Jurkaa52570f2012-03-20 03:18:20 -07002218 int childCount = mShortcutsAndWidgets.getChildCount();
Adam Cohen482ed822012-03-02 14:15:13 -08002219 for (int i = 0; i < childCount; i++) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07002220 View child = mShortcutsAndWidgets.getChildAt(i);
Adam Cohen482ed822012-03-02 14:15:13 -08002221 if (child == dragView) continue;
Adam Cohen8baab352012-03-20 17:39:21 -07002222 CellAndSpan c = solution.map.get(child);
2223 if (c != null) {
Adam Cohen19f37922012-03-21 11:59:11 -07002224 animateChildToPosition(child, c.x, c.y, REORDER_ANIMATION_DURATION, 0,
2225 DESTRUCTIVE_REORDER, false);
Adam Cohen8baab352012-03-20 17:39:21 -07002226 markCellsForView(c.x, c.y, c.spanX, c.spanY, occupied, true);
Adam Cohen482ed822012-03-02 14:15:13 -08002227 }
2228 }
2229 if (commitDragView) {
2230 markCellsForView(solution.dragViewX, solution.dragViewY, solution.dragViewSpanX,
2231 solution.dragViewSpanY, occupied, true);
2232 }
2233 }
2234
Adam Cohen19f37922012-03-21 11:59:11 -07002235 // This method starts or changes the reorder hint animations
2236 private void beginOrAdjustHintAnimations(ItemConfiguration solution, View dragView, int delay) {
2237 int childCount = mShortcutsAndWidgets.getChildCount();
Adam Cohen19f37922012-03-21 11:59:11 -07002238 for (int i = 0; i < childCount; i++) {
2239 View child = mShortcutsAndWidgets.getChildAt(i);
2240 if (child == dragView) continue;
2241 CellAndSpan c = solution.map.get(child);
2242 LayoutParams lp = (LayoutParams) child.getLayoutParams();
2243 if (c != null) {
2244 ReorderHintAnimation rha = new ReorderHintAnimation(child, lp.cellX, lp.cellY,
2245 c.x, c.y, c.spanX, c.spanY);
Adam Cohend024f982012-05-23 18:26:45 -07002246 rha.animate();
Adam Cohen19f37922012-03-21 11:59:11 -07002247 }
2248 }
2249 }
2250
2251 // Class which represents the reorder hint animations. These animations show that an item is
2252 // in a temporary state, and hint at where the item will return to.
2253 class ReorderHintAnimation {
2254 View child;
Adam Cohend024f982012-05-23 18:26:45 -07002255 float finalDeltaX;
2256 float finalDeltaY;
2257 float initDeltaX;
2258 float initDeltaY;
2259 float finalScale;
2260 float initScale;
Brandon Keely50e6e562012-05-08 16:28:49 -07002261 private static final int DURATION = 300;
Adam Cohene7587d22012-05-24 18:50:02 -07002262 Animator a;
Adam Cohen19f37922012-03-21 11:59:11 -07002263
2264 public ReorderHintAnimation(View child, int cellX0, int cellY0, int cellX1, int cellY1,
2265 int spanX, int spanY) {
2266 regionToCenterPoint(cellX0, cellY0, spanX, spanY, mTmpPoint);
2267 final int x0 = mTmpPoint[0];
2268 final int y0 = mTmpPoint[1];
2269 regionToCenterPoint(cellX1, cellY1, spanX, spanY, mTmpPoint);
2270 final int x1 = mTmpPoint[0];
2271 final int y1 = mTmpPoint[1];
2272 final int dX = x1 - x0;
2273 final int dY = y1 - y0;
Adam Cohend024f982012-05-23 18:26:45 -07002274 finalDeltaX = 0;
2275 finalDeltaY = 0;
Adam Cohen19f37922012-03-21 11:59:11 -07002276 if (dX == dY && dX == 0) {
2277 } else {
2278 if (dY == 0) {
Adam Cohend024f982012-05-23 18:26:45 -07002279 finalDeltaX = - Math.signum(dX) * mReorderHintAnimationMagnitude;
Adam Cohen19f37922012-03-21 11:59:11 -07002280 } else if (dX == 0) {
Adam Cohend024f982012-05-23 18:26:45 -07002281 finalDeltaY = - Math.signum(dY) * mReorderHintAnimationMagnitude;
Adam Cohen19f37922012-03-21 11:59:11 -07002282 } else {
2283 double angle = Math.atan( (float) (dY) / dX);
Adam Cohend024f982012-05-23 18:26:45 -07002284 finalDeltaX = (int) (- Math.signum(dX) *
Adam Cohenfe41ac62012-05-23 14:00:37 -07002285 Math.abs(Math.cos(angle) * mReorderHintAnimationMagnitude));
Adam Cohend024f982012-05-23 18:26:45 -07002286 finalDeltaY = (int) (- Math.signum(dY) *
Adam Cohenfe41ac62012-05-23 14:00:37 -07002287 Math.abs(Math.sin(angle) * mReorderHintAnimationMagnitude));
Adam Cohen19f37922012-03-21 11:59:11 -07002288 }
2289 }
Adam Cohend024f982012-05-23 18:26:45 -07002290 initDeltaX = child.getTranslationX();
2291 initDeltaY = child.getTranslationY();
Adam Cohen307fe232012-08-16 17:55:58 -07002292 finalScale = getChildrenScale() - 4.0f / child.getWidth();
Adam Cohend024f982012-05-23 18:26:45 -07002293 initScale = child.getScaleX();
Adam Cohen19f37922012-03-21 11:59:11 -07002294 this.child = child;
2295 }
2296
Adam Cohend024f982012-05-23 18:26:45 -07002297 void animate() {
Adam Cohen19f37922012-03-21 11:59:11 -07002298 if (mShakeAnimators.containsKey(child)) {
2299 ReorderHintAnimation oldAnimation = mShakeAnimators.get(child);
Adam Cohend024f982012-05-23 18:26:45 -07002300 oldAnimation.cancel();
Adam Cohen19f37922012-03-21 11:59:11 -07002301 mShakeAnimators.remove(child);
Adam Cohene7587d22012-05-24 18:50:02 -07002302 if (finalDeltaX == 0 && finalDeltaY == 0) {
2303 completeAnimationImmediately();
2304 return;
2305 }
Adam Cohen19f37922012-03-21 11:59:11 -07002306 }
Adam Cohend024f982012-05-23 18:26:45 -07002307 if (finalDeltaX == 0 && finalDeltaY == 0) {
Adam Cohen19f37922012-03-21 11:59:11 -07002308 return;
2309 }
Michael Jurkaf1ad6082013-03-13 12:55:46 +01002310 ValueAnimator va = LauncherAnimUtils.ofFloat(child, 0f, 1f);
Adam Cohene7587d22012-05-24 18:50:02 -07002311 a = va;
Adam Cohen19f37922012-03-21 11:59:11 -07002312 va.setRepeatMode(ValueAnimator.REVERSE);
2313 va.setRepeatCount(ValueAnimator.INFINITE);
Adam Cohen7bdfc972012-05-22 16:50:35 -07002314 va.setDuration(DURATION);
Adam Cohend024f982012-05-23 18:26:45 -07002315 va.setStartDelay((int) (Math.random() * 60));
Adam Cohen19f37922012-03-21 11:59:11 -07002316 va.addUpdateListener(new AnimatorUpdateListener() {
2317 @Override
2318 public void onAnimationUpdate(ValueAnimator animation) {
2319 float r = ((Float) animation.getAnimatedValue()).floatValue();
Adam Cohend024f982012-05-23 18:26:45 -07002320 float x = r * finalDeltaX + (1 - r) * initDeltaX;
2321 float y = r * finalDeltaY + (1 - r) * initDeltaY;
Adam Cohen19f37922012-03-21 11:59:11 -07002322 child.setTranslationX(x);
2323 child.setTranslationY(y);
Adam Cohend024f982012-05-23 18:26:45 -07002324 float s = r * finalScale + (1 - r) * initScale;
Brandon Keely50e6e562012-05-08 16:28:49 -07002325 child.setScaleX(s);
2326 child.setScaleY(s);
Adam Cohen19f37922012-03-21 11:59:11 -07002327 }
2328 });
2329 va.addListener(new AnimatorListenerAdapter() {
2330 public void onAnimationRepeat(Animator animation) {
Adam Cohen19f37922012-03-21 11:59:11 -07002331 // We make sure to end only after a full period
Adam Cohend024f982012-05-23 18:26:45 -07002332 initDeltaX = 0;
2333 initDeltaY = 0;
Adam Cohen307fe232012-08-16 17:55:58 -07002334 initScale = getChildrenScale();
Adam Cohen19f37922012-03-21 11:59:11 -07002335 }
2336 });
Adam Cohen19f37922012-03-21 11:59:11 -07002337 mShakeAnimators.put(child, this);
2338 va.start();
2339 }
2340
Adam Cohend024f982012-05-23 18:26:45 -07002341 private void cancel() {
Adam Cohene7587d22012-05-24 18:50:02 -07002342 if (a != null) {
2343 a.cancel();
2344 }
Adam Cohen19f37922012-03-21 11:59:11 -07002345 }
Adam Cohene7587d22012-05-24 18:50:02 -07002346
Brandon Keely50e6e562012-05-08 16:28:49 -07002347 private void completeAnimationImmediately() {
Adam Cohene7587d22012-05-24 18:50:02 -07002348 if (a != null) {
2349 a.cancel();
2350 }
Brandon Keely50e6e562012-05-08 16:28:49 -07002351
Michael Jurka2ecf9952012-06-18 12:52:28 -07002352 AnimatorSet s = LauncherAnimUtils.createAnimatorSet();
Adam Cohene7587d22012-05-24 18:50:02 -07002353 a = s;
Brandon Keely50e6e562012-05-08 16:28:49 -07002354 s.playTogether(
Adam Cohen307fe232012-08-16 17:55:58 -07002355 LauncherAnimUtils.ofFloat(child, "scaleX", getChildrenScale()),
2356 LauncherAnimUtils.ofFloat(child, "scaleY", getChildrenScale()),
Michael Jurka2ecf9952012-06-18 12:52:28 -07002357 LauncherAnimUtils.ofFloat(child, "translationX", 0f),
2358 LauncherAnimUtils.ofFloat(child, "translationY", 0f)
Brandon Keely50e6e562012-05-08 16:28:49 -07002359 );
2360 s.setDuration(REORDER_ANIMATION_DURATION);
2361 s.setInterpolator(new android.view.animation.DecelerateInterpolator(1.5f));
2362 s.start();
2363 }
Adam Cohen19f37922012-03-21 11:59:11 -07002364 }
2365
2366 private void completeAndClearReorderHintAnimations() {
2367 for (ReorderHintAnimation a: mShakeAnimators.values()) {
Brandon Keely50e6e562012-05-08 16:28:49 -07002368 a.completeAnimationImmediately();
Adam Cohen19f37922012-03-21 11:59:11 -07002369 }
2370 mShakeAnimators.clear();
2371 }
2372
Adam Cohen482ed822012-03-02 14:15:13 -08002373 private void commitTempPlacement() {
2374 for (int i = 0; i < mCountX; i++) {
2375 for (int j = 0; j < mCountY; j++) {
2376 mOccupied[i][j] = mTmpOccupied[i][j];
2377 }
2378 }
Michael Jurkaa52570f2012-03-20 03:18:20 -07002379 int childCount = mShortcutsAndWidgets.getChildCount();
Adam Cohen482ed822012-03-02 14:15:13 -08002380 for (int i = 0; i < childCount; i++) {
Adam Cohenea889a22012-03-27 16:45:39 -07002381 View child = mShortcutsAndWidgets.getChildAt(i);
2382 LayoutParams lp = (LayoutParams) child.getLayoutParams();
2383 ItemInfo info = (ItemInfo) child.getTag();
Adam Cohen2acce882012-03-28 19:03:19 -07002384 // We do a null check here because the item info can be null in the case of the
2385 // AllApps button in the hotseat.
2386 if (info != null) {
Adam Cohen487f7dd2012-06-28 18:12:10 -07002387 if (info.cellX != lp.tmpCellX || info.cellY != lp.tmpCellY ||
2388 info.spanX != lp.cellHSpan || info.spanY != lp.cellVSpan) {
2389 info.requiresDbUpdate = true;
2390 }
Adam Cohen2acce882012-03-28 19:03:19 -07002391 info.cellX = lp.cellX = lp.tmpCellX;
2392 info.cellY = lp.cellY = lp.tmpCellY;
Adam Cohenbebf0422012-04-11 18:06:28 -07002393 info.spanX = lp.cellHSpan;
2394 info.spanY = lp.cellVSpan;
Adam Cohen2acce882012-03-28 19:03:19 -07002395 }
Adam Cohen482ed822012-03-02 14:15:13 -08002396 }
Adam Cohen2acce882012-03-28 19:03:19 -07002397 mLauncher.getWorkspace().updateItemLocationsInDatabase(this);
Adam Cohen482ed822012-03-02 14:15:13 -08002398 }
2399
2400 public void setUseTempCoords(boolean useTempCoords) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07002401 int childCount = mShortcutsAndWidgets.getChildCount();
Adam Cohen482ed822012-03-02 14:15:13 -08002402 for (int i = 0; i < childCount; i++) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07002403 LayoutParams lp = (LayoutParams) mShortcutsAndWidgets.getChildAt(i).getLayoutParams();
Adam Cohen482ed822012-03-02 14:15:13 -08002404 lp.useTmpCoords = useTempCoords;
2405 }
2406 }
2407
Adam Cohen482ed822012-03-02 14:15:13 -08002408 ItemConfiguration findConfigurationNoShuffle(int pixelX, int pixelY, int minSpanX, int minSpanY,
2409 int spanX, int spanY, View dragView, ItemConfiguration solution) {
2410 int[] result = new int[2];
2411 int[] resultSpan = new int[2];
2412 findNearestVacantArea(pixelX, pixelY, minSpanX, minSpanY, spanX, spanY, null, result,
2413 resultSpan);
2414 if (result[0] >= 0 && result[1] >= 0) {
2415 copyCurrentStateToSolution(solution, false);
2416 solution.dragViewX = result[0];
2417 solution.dragViewY = result[1];
2418 solution.dragViewSpanX = resultSpan[0];
2419 solution.dragViewSpanY = resultSpan[1];
2420 solution.isSolution = true;
2421 } else {
2422 solution.isSolution = false;
2423 }
2424 return solution;
2425 }
2426
2427 public void prepareChildForDrag(View child) {
2428 markCellsAsUnoccupiedForView(child);
Adam Cohen482ed822012-03-02 14:15:13 -08002429 }
2430
Adam Cohen19f37922012-03-21 11:59:11 -07002431 /* This seems like it should be obvious and straight-forward, but when the direction vector
2432 needs to match with the notion of the dragView pushing other views, we have to employ
2433 a slightly more subtle notion of the direction vector. The question is what two points is
2434 the vector between? The center of the dragView and its desired destination? Not quite, as
2435 this doesn't necessarily coincide with the interaction of the dragView and items occupying
2436 those cells. Instead we use some heuristics to often lock the vector to up, down, left
2437 or right, which helps make pushing feel right.
2438 */
2439 private void getDirectionVectorForDrop(int dragViewCenterX, int dragViewCenterY, int spanX,
2440 int spanY, View dragView, int[] resultDirection) {
2441 int[] targetDestination = new int[2];
2442
2443 findNearestArea(dragViewCenterX, dragViewCenterY, spanX, spanY, targetDestination);
2444 Rect dragRect = new Rect();
2445 regionToRect(targetDestination[0], targetDestination[1], spanX, spanY, dragRect);
2446 dragRect.offset(dragViewCenterX - dragRect.centerX(), dragViewCenterY - dragRect.centerY());
2447
2448 Rect dropRegionRect = new Rect();
2449 getViewsIntersectingRegion(targetDestination[0], targetDestination[1], spanX, spanY,
2450 dragView, dropRegionRect, mIntersectingViews);
2451
2452 int dropRegionSpanX = dropRegionRect.width();
2453 int dropRegionSpanY = dropRegionRect.height();
2454
2455 regionToRect(dropRegionRect.left, dropRegionRect.top, dropRegionRect.width(),
2456 dropRegionRect.height(), dropRegionRect);
2457
2458 int deltaX = (dropRegionRect.centerX() - dragViewCenterX) / spanX;
2459 int deltaY = (dropRegionRect.centerY() - dragViewCenterY) / spanY;
2460
2461 if (dropRegionSpanX == mCountX || spanX == mCountX) {
2462 deltaX = 0;
2463 }
2464 if (dropRegionSpanY == mCountY || spanY == mCountY) {
2465 deltaY = 0;
2466 }
2467
2468 if (deltaX == 0 && deltaY == 0) {
2469 // No idea what to do, give a random direction.
2470 resultDirection[0] = 1;
2471 resultDirection[1] = 0;
2472 } else {
2473 computeDirectionVector(deltaX, deltaY, resultDirection);
2474 }
2475 }
2476
2477 // For a given cell and span, fetch the set of views intersecting the region.
2478 private void getViewsIntersectingRegion(int cellX, int cellY, int spanX, int spanY,
2479 View dragView, Rect boundingRect, ArrayList<View> intersectingViews) {
2480 if (boundingRect != null) {
2481 boundingRect.set(cellX, cellY, cellX + spanX, cellY + spanY);
2482 }
2483 intersectingViews.clear();
2484 Rect r0 = new Rect(cellX, cellY, cellX + spanX, cellY + spanY);
2485 Rect r1 = new Rect();
2486 final int count = mShortcutsAndWidgets.getChildCount();
2487 for (int i = 0; i < count; i++) {
2488 View child = mShortcutsAndWidgets.getChildAt(i);
2489 if (child == dragView) continue;
2490 LayoutParams lp = (LayoutParams) child.getLayoutParams();
2491 r1.set(lp.cellX, lp.cellY, lp.cellX + lp.cellHSpan, lp.cellY + lp.cellVSpan);
2492 if (Rect.intersects(r0, r1)) {
2493 mIntersectingViews.add(child);
2494 if (boundingRect != null) {
2495 boundingRect.union(r1);
2496 }
2497 }
2498 }
2499 }
2500
2501 boolean isNearestDropLocationOccupied(int pixelX, int pixelY, int spanX, int spanY,
2502 View dragView, int[] result) {
2503 result = findNearestArea(pixelX, pixelY, spanX, spanY, result);
2504 getViewsIntersectingRegion(result[0], result[1], spanX, spanY, dragView, null,
2505 mIntersectingViews);
2506 return !mIntersectingViews.isEmpty();
2507 }
2508
2509 void revertTempState() {
2510 if (!isItemPlacementDirty() || DESTRUCTIVE_REORDER) return;
2511 final int count = mShortcutsAndWidgets.getChildCount();
2512 for (int i = 0; i < count; i++) {
2513 View child = mShortcutsAndWidgets.getChildAt(i);
2514 LayoutParams lp = (LayoutParams) child.getLayoutParams();
2515 if (lp.tmpCellX != lp.cellX || lp.tmpCellY != lp.cellY) {
2516 lp.tmpCellX = lp.cellX;
2517 lp.tmpCellY = lp.cellY;
2518 animateChildToPosition(child, lp.cellX, lp.cellY, REORDER_ANIMATION_DURATION,
2519 0, false, false);
2520 }
2521 }
2522 completeAndClearReorderHintAnimations();
2523 setItemPlacementDirty(false);
2524 }
2525
Adam Cohenbebf0422012-04-11 18:06:28 -07002526 boolean createAreaForResize(int cellX, int cellY, int spanX, int spanY,
2527 View dragView, int[] direction, boolean commit) {
2528 int[] pixelXY = new int[2];
2529 regionToCenterPoint(cellX, cellY, spanX, spanY, pixelXY);
2530
2531 // First we determine if things have moved enough to cause a different layout
2532 ItemConfiguration swapSolution = simpleSwap(pixelXY[0], pixelXY[1], spanX, spanY,
2533 spanX, spanY, direction, dragView, true, new ItemConfiguration());
2534
2535 setUseTempCoords(true);
2536 if (swapSolution != null && swapSolution.isSolution) {
2537 // If we're just testing for a possible location (MODE_ACCEPT_DROP), we don't bother
2538 // committing anything or animating anything as we just want to determine if a solution
2539 // exists
2540 copySolutionToTempState(swapSolution, dragView);
2541 setItemPlacementDirty(true);
2542 animateItemsToSolution(swapSolution, dragView, commit);
2543
2544 if (commit) {
2545 commitTempPlacement();
2546 completeAndClearReorderHintAnimations();
2547 setItemPlacementDirty(false);
2548 } else {
2549 beginOrAdjustHintAnimations(swapSolution, dragView,
2550 REORDER_ANIMATION_DURATION);
2551 }
2552 mShortcutsAndWidgets.requestLayout();
2553 }
2554 return swapSolution.isSolution;
2555 }
2556
Adam Cohen482ed822012-03-02 14:15:13 -08002557 int[] createArea(int pixelX, int pixelY, int minSpanX, int minSpanY, int spanX, int spanY,
2558 View dragView, int[] result, int resultSpan[], int mode) {
Adam Cohen482ed822012-03-02 14:15:13 -08002559 // First we determine if things have moved enough to cause a different layout
Adam Cohen47a876d2012-03-19 13:21:41 -07002560 result = findNearestArea(pixelX, pixelY, spanX, spanY, result);
Adam Cohen482ed822012-03-02 14:15:13 -08002561
2562 if (resultSpan == null) {
2563 resultSpan = new int[2];
2564 }
2565
Adam Cohen19f37922012-03-21 11:59:11 -07002566 // When we are checking drop validity or actually dropping, we don't recompute the
2567 // direction vector, since we want the solution to match the preview, and it's possible
2568 // that the exact position of the item has changed to result in a new reordering outcome.
Adam Cohenb209e632012-03-27 17:09:36 -07002569 if ((mode == MODE_ON_DROP || mode == MODE_ON_DROP_EXTERNAL || mode == MODE_ACCEPT_DROP)
2570 && mPreviousReorderDirection[0] != INVALID_DIRECTION) {
Adam Cohen19f37922012-03-21 11:59:11 -07002571 mDirectionVector[0] = mPreviousReorderDirection[0];
2572 mDirectionVector[1] = mPreviousReorderDirection[1];
2573 // We reset this vector after drop
Adam Cohenb209e632012-03-27 17:09:36 -07002574 if (mode == MODE_ON_DROP || mode == MODE_ON_DROP_EXTERNAL) {
2575 mPreviousReorderDirection[0] = INVALID_DIRECTION;
2576 mPreviousReorderDirection[1] = INVALID_DIRECTION;
Adam Cohen19f37922012-03-21 11:59:11 -07002577 }
2578 } else {
2579 getDirectionVectorForDrop(pixelX, pixelY, spanX, spanY, dragView, mDirectionVector);
2580 mPreviousReorderDirection[0] = mDirectionVector[0];
2581 mPreviousReorderDirection[1] = mDirectionVector[1];
2582 }
2583
Adam Cohen482ed822012-03-02 14:15:13 -08002584 ItemConfiguration swapSolution = simpleSwap(pixelX, pixelY, minSpanX, minSpanY,
2585 spanX, spanY, mDirectionVector, dragView, true, new ItemConfiguration());
2586
2587 // We attempt the approach which doesn't shuffle views at all
2588 ItemConfiguration noShuffleSolution = findConfigurationNoShuffle(pixelX, pixelY, minSpanX,
2589 minSpanY, spanX, spanY, dragView, new ItemConfiguration());
2590
2591 ItemConfiguration finalSolution = null;
2592 if (swapSolution.isSolution && swapSolution.area() >= noShuffleSolution.area()) {
2593 finalSolution = swapSolution;
2594 } else if (noShuffleSolution.isSolution) {
2595 finalSolution = noShuffleSolution;
2596 }
2597
2598 boolean foundSolution = true;
2599 if (!DESTRUCTIVE_REORDER) {
2600 setUseTempCoords(true);
2601 }
2602
2603 if (finalSolution != null) {
2604 result[0] = finalSolution.dragViewX;
2605 result[1] = finalSolution.dragViewY;
2606 resultSpan[0] = finalSolution.dragViewSpanX;
2607 resultSpan[1] = finalSolution.dragViewSpanY;
2608
2609 // If we're just testing for a possible location (MODE_ACCEPT_DROP), we don't bother
2610 // committing anything or animating anything as we just want to determine if a solution
2611 // exists
2612 if (mode == MODE_DRAG_OVER || mode == MODE_ON_DROP || mode == MODE_ON_DROP_EXTERNAL) {
2613 if (!DESTRUCTIVE_REORDER) {
2614 copySolutionToTempState(finalSolution, dragView);
2615 }
2616 setItemPlacementDirty(true);
2617 animateItemsToSolution(finalSolution, dragView, mode == MODE_ON_DROP);
2618
Adam Cohen19f37922012-03-21 11:59:11 -07002619 if (!DESTRUCTIVE_REORDER &&
2620 (mode == MODE_ON_DROP || mode == MODE_ON_DROP_EXTERNAL)) {
Adam Cohen482ed822012-03-02 14:15:13 -08002621 commitTempPlacement();
Adam Cohen19f37922012-03-21 11:59:11 -07002622 completeAndClearReorderHintAnimations();
2623 setItemPlacementDirty(false);
2624 } else {
2625 beginOrAdjustHintAnimations(finalSolution, dragView,
2626 REORDER_ANIMATION_DURATION);
Adam Cohen482ed822012-03-02 14:15:13 -08002627 }
2628 }
2629 } else {
2630 foundSolution = false;
2631 result[0] = result[1] = resultSpan[0] = resultSpan[1] = -1;
2632 }
2633
2634 if ((mode == MODE_ON_DROP || !foundSolution) && !DESTRUCTIVE_REORDER) {
2635 setUseTempCoords(false);
2636 }
Adam Cohen482ed822012-03-02 14:15:13 -08002637
Michael Jurkaa52570f2012-03-20 03:18:20 -07002638 mShortcutsAndWidgets.requestLayout();
Adam Cohen482ed822012-03-02 14:15:13 -08002639 return result;
2640 }
2641
Adam Cohen19f37922012-03-21 11:59:11 -07002642 void setItemPlacementDirty(boolean dirty) {
2643 mItemPlacementDirty = dirty;
Adam Cohen482ed822012-03-02 14:15:13 -08002644 }
Adam Cohen19f37922012-03-21 11:59:11 -07002645 boolean isItemPlacementDirty() {
2646 return mItemPlacementDirty;
Adam Cohen482ed822012-03-02 14:15:13 -08002647 }
2648
2649 private class ItemConfiguration {
Adam Cohen8baab352012-03-20 17:39:21 -07002650 HashMap<View, CellAndSpan> map = new HashMap<View, CellAndSpan>();
Adam Cohenf3900c22012-11-16 18:28:11 -08002651 private HashMap<View, CellAndSpan> savedMap = new HashMap<View, CellAndSpan>();
2652 ArrayList<View> sortedViews = new ArrayList<View>();
Adam Cohen482ed822012-03-02 14:15:13 -08002653 boolean isSolution = false;
2654 int dragViewX, dragViewY, dragViewSpanX, dragViewSpanY;
2655
Adam Cohenf3900c22012-11-16 18:28:11 -08002656 void save() {
2657 // Copy current state into savedMap
2658 for (View v: map.keySet()) {
2659 map.get(v).copy(savedMap.get(v));
2660 }
2661 }
2662
2663 void restore() {
2664 // Restore current state from savedMap
2665 for (View v: savedMap.keySet()) {
2666 savedMap.get(v).copy(map.get(v));
2667 }
2668 }
2669
2670 void add(View v, CellAndSpan cs) {
2671 map.put(v, cs);
2672 savedMap.put(v, new CellAndSpan());
2673 sortedViews.add(v);
2674 }
2675
Adam Cohen482ed822012-03-02 14:15:13 -08002676 int area() {
2677 return dragViewSpanX * dragViewSpanY;
2678 }
Adam Cohen8baab352012-03-20 17:39:21 -07002679 }
2680
2681 private class CellAndSpan {
2682 int x, y;
2683 int spanX, spanY;
2684
Adam Cohenf3900c22012-11-16 18:28:11 -08002685 public CellAndSpan() {
2686 }
2687
2688 public void copy(CellAndSpan copy) {
2689 copy.x = x;
2690 copy.y = y;
2691 copy.spanX = spanX;
2692 copy.spanY = spanY;
2693 }
2694
Adam Cohen8baab352012-03-20 17:39:21 -07002695 public CellAndSpan(int x, int y, int spanX, int spanY) {
2696 this.x = x;
2697 this.y = y;
2698 this.spanX = spanX;
2699 this.spanY = spanY;
Adam Cohen482ed822012-03-02 14:15:13 -08002700 }
Adam Cohenf3900c22012-11-16 18:28:11 -08002701
2702 public String toString() {
2703 return "(" + x + ", " + y + ": " + spanX + ", " + spanY + ")";
2704 }
2705
Adam Cohen482ed822012-03-02 14:15:13 -08002706 }
2707
Adam Cohendf035382011-04-11 17:22:04 -07002708 /**
2709 * Find a vacant area that will fit the given bounds nearest the requested
2710 * cell location. Uses Euclidean distance to score multiple vacant areas.
2711 *
2712 * @param pixelX The X location at which you want to search for a vacant area.
2713 * @param pixelY The Y location at which you want to search for a vacant area.
2714 * @param spanX Horizontal span of the object.
2715 * @param spanY Vertical span of the object.
2716 * @param ignoreView Considers space occupied by this view as unoccupied
2717 * @param result Previously returned value to possibly recycle.
2718 * @return The X, Y cell of a vacant area that can contain this object,
2719 * nearest the requested location.
2720 */
2721 int[] findNearestVacantArea(
2722 int pixelX, int pixelY, int spanX, int spanY, View ignoreView, int[] result) {
2723 return findNearestArea(pixelX, pixelY, spanX, spanY, ignoreView, true, result);
2724 }
2725
2726 /**
Adam Cohend41fbf52012-02-16 23:53:59 -08002727 * Find a vacant area that will fit the given bounds nearest the requested
2728 * cell location. Uses Euclidean distance to score multiple vacant areas.
2729 *
2730 * @param pixelX The X location at which you want to search for a vacant area.
2731 * @param pixelY The Y location at which you want to search for a vacant area.
2732 * @param minSpanX The minimum horizontal span required
2733 * @param minSpanY The minimum vertical span required
2734 * @param spanX Horizontal span of the object.
2735 * @param spanY Vertical span of the object.
2736 * @param ignoreView Considers space occupied by this view as unoccupied
2737 * @param result Previously returned value to possibly recycle.
2738 * @return The X, Y cell of a vacant area that can contain this object,
2739 * nearest the requested location.
2740 */
2741 int[] findNearestVacantArea(int pixelX, int pixelY, int minSpanX, int minSpanY,
2742 int spanX, int spanY, View ignoreView, int[] result, int[] resultSpan) {
Adam Cohen482ed822012-03-02 14:15:13 -08002743 return findNearestArea(pixelX, pixelY, minSpanX, minSpanY, spanX, spanY, ignoreView, true,
2744 result, resultSpan, mOccupied);
Adam Cohend41fbf52012-02-16 23:53:59 -08002745 }
2746
2747 /**
Adam Cohendf035382011-04-11 17:22:04 -07002748 * Find a starting cell position that will fit the given bounds nearest the requested
2749 * cell location. Uses Euclidean distance to score multiple vacant areas.
2750 *
2751 * @param pixelX The X location at which you want to search for a vacant area.
2752 * @param pixelY The Y location at which you want to search for a vacant area.
2753 * @param spanX Horizontal span of the object.
2754 * @param spanY Vertical span of the object.
2755 * @param ignoreView Considers space occupied by this view as unoccupied
2756 * @param result Previously returned value to possibly recycle.
2757 * @return The X, Y cell of a vacant area that can contain this object,
2758 * nearest the requested location.
2759 */
2760 int[] findNearestArea(
2761 int pixelX, int pixelY, int spanX, int spanY, int[] result) {
2762 return findNearestArea(pixelX, pixelY, spanX, spanY, null, false, result);
2763 }
2764
Michael Jurka0280c3b2010-09-17 15:00:07 -07002765 boolean existsEmptyCell() {
2766 return findCellForSpan(null, 1, 1);
2767 }
2768
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002769 /**
Michael Jurka0280c3b2010-09-17 15:00:07 -07002770 * Finds the upper-left coordinate of the first rectangle in the grid that can
2771 * hold a cell of the specified dimensions. If intersectX and intersectY are not -1,
2772 * then this method will only return coordinates for rectangles that contain the cell
2773 * (intersectX, intersectY)
2774 *
2775 * @param cellXY The array that will contain the position of a vacant cell if such a cell
2776 * can be found.
2777 * @param spanX The horizontal span of the cell we want to find.
2778 * @param spanY The vertical span of the cell we want to find.
2779 *
2780 * @return True if a vacant cell of the specified dimension was found, false otherwise.
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07002781 */
Michael Jurka0280c3b2010-09-17 15:00:07 -07002782 boolean findCellForSpan(int[] cellXY, int spanX, int spanY) {
Adam Cohen482ed822012-03-02 14:15:13 -08002783 return findCellForSpanThatIntersectsIgnoring(cellXY, spanX, spanY, -1, -1, null, mOccupied);
Michael Jurka0280c3b2010-09-17 15:00:07 -07002784 }
2785
2786 /**
2787 * Like above, but ignores any cells occupied by the item "ignoreView"
2788 *
2789 * @param cellXY The array that will contain the position of a vacant cell if such a cell
2790 * can be found.
2791 * @param spanX The horizontal span of the cell we want to find.
2792 * @param spanY The vertical span of the cell we want to find.
2793 * @param ignoreView The home screen item we should treat as not occupying any space
2794 * @return
2795 */
2796 boolean findCellForSpanIgnoring(int[] cellXY, int spanX, int spanY, View ignoreView) {
Adam Cohen482ed822012-03-02 14:15:13 -08002797 return findCellForSpanThatIntersectsIgnoring(cellXY, spanX, spanY, -1, -1,
2798 ignoreView, mOccupied);
Michael Jurka0280c3b2010-09-17 15:00:07 -07002799 }
2800
2801 /**
2802 * Like above, but if intersectX and intersectY are not -1, then this method will try to
2803 * return coordinates for rectangles that contain the cell [intersectX, intersectY]
2804 *
2805 * @param spanX The horizontal span of the cell we want to find.
2806 * @param spanY The vertical span of the cell we want to find.
2807 * @param ignoreView The home screen item we should treat as not occupying any space
2808 * @param intersectX The X coordinate of the cell that we should try to overlap
2809 * @param intersectX The Y coordinate of the cell that we should try to overlap
2810 *
2811 * @return True if a vacant cell of the specified dimension was found, false otherwise.
2812 */
2813 boolean findCellForSpanThatIntersects(int[] cellXY, int spanX, int spanY,
2814 int intersectX, int intersectY) {
2815 return findCellForSpanThatIntersectsIgnoring(
Adam Cohen482ed822012-03-02 14:15:13 -08002816 cellXY, spanX, spanY, intersectX, intersectY, null, mOccupied);
Michael Jurka0280c3b2010-09-17 15:00:07 -07002817 }
2818
2819 /**
2820 * The superset of the above two methods
2821 */
2822 boolean findCellForSpanThatIntersectsIgnoring(int[] cellXY, int spanX, int spanY,
Adam Cohen482ed822012-03-02 14:15:13 -08002823 int intersectX, int intersectY, View ignoreView, boolean occupied[][]) {
Michael Jurkac6ee42e2010-09-30 12:04:50 -07002824 // mark space take by ignoreView as available (method checks if ignoreView is null)
Adam Cohen482ed822012-03-02 14:15:13 -08002825 markCellsAsUnoccupiedForView(ignoreView, occupied);
Michael Jurka0280c3b2010-09-17 15:00:07 -07002826
Michael Jurka28750fb2010-09-24 17:43:49 -07002827 boolean foundCell = false;
Michael Jurka0280c3b2010-09-17 15:00:07 -07002828 while (true) {
2829 int startX = 0;
2830 if (intersectX >= 0) {
2831 startX = Math.max(startX, intersectX - (spanX - 1));
2832 }
2833 int endX = mCountX - (spanX - 1);
2834 if (intersectX >= 0) {
2835 endX = Math.min(endX, intersectX + (spanX - 1) + (spanX == 1 ? 1 : 0));
2836 }
2837 int startY = 0;
2838 if (intersectY >= 0) {
2839 startY = Math.max(startY, intersectY - (spanY - 1));
2840 }
2841 int endY = mCountY - (spanY - 1);
2842 if (intersectY >= 0) {
2843 endY = Math.min(endY, intersectY + (spanY - 1) + (spanY == 1 ? 1 : 0));
2844 }
2845
Winson Chungbbc60d82010-11-11 16:34:41 -08002846 for (int y = startY; y < endY && !foundCell; y++) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002847 inner:
Winson Chungbbc60d82010-11-11 16:34:41 -08002848 for (int x = startX; x < endX; x++) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002849 for (int i = 0; i < spanX; i++) {
2850 for (int j = 0; j < spanY; j++) {
Adam Cohen482ed822012-03-02 14:15:13 -08002851 if (occupied[x + i][y + j]) {
Winson Chungbbc60d82010-11-11 16:34:41 -08002852 // small optimization: we can skip to after the column we just found
Michael Jurka0280c3b2010-09-17 15:00:07 -07002853 // an occupied cell
Winson Chungbbc60d82010-11-11 16:34:41 -08002854 x += i;
Michael Jurka0280c3b2010-09-17 15:00:07 -07002855 continue inner;
2856 }
2857 }
2858 }
2859 if (cellXY != null) {
2860 cellXY[0] = x;
2861 cellXY[1] = y;
2862 }
Michael Jurka28750fb2010-09-24 17:43:49 -07002863 foundCell = true;
2864 break;
Michael Jurka0280c3b2010-09-17 15:00:07 -07002865 }
2866 }
2867 if (intersectX == -1 && intersectY == -1) {
2868 break;
2869 } else {
2870 // if we failed to find anything, try again but without any requirements of
2871 // intersecting
2872 intersectX = -1;
2873 intersectY = -1;
2874 continue;
2875 }
2876 }
2877
Michael Jurkac6ee42e2010-09-30 12:04:50 -07002878 // re-mark space taken by ignoreView as occupied
Adam Cohen482ed822012-03-02 14:15:13 -08002879 markCellsAsOccupiedForView(ignoreView, occupied);
Michael Jurka28750fb2010-09-24 17:43:49 -07002880 return foundCell;
Michael Jurka0280c3b2010-09-17 15:00:07 -07002881 }
2882
2883 /**
Winson Chungc07918d2011-07-01 15:35:26 -07002884 * A drag event has begun over this layout.
2885 * It may have begun over this layout (in which case onDragChild is called first),
2886 * or it may have begun on another layout.
2887 */
2888 void onDragEnter() {
Adam Cohenc6cc61d2012-04-04 12:47:08 -07002889 mDragEnforcer.onDragEnter();
Winson Chungc07918d2011-07-01 15:35:26 -07002890 mDragging = true;
2891 }
2892
2893 /**
Michael Jurka0280c3b2010-09-17 15:00:07 -07002894 * Called when drag has left this CellLayout or has been completed (successfully or not)
2895 */
2896 void onDragExit() {
Adam Cohenc6cc61d2012-04-04 12:47:08 -07002897 mDragEnforcer.onDragExit();
Joe Onorato4be866d2010-10-10 11:26:02 -07002898 // This can actually be called when we aren't in a drag, e.g. when adding a new
2899 // item to this layout via the customize drawer.
2900 // Guard against that case.
2901 if (mDragging) {
2902 mDragging = false;
Patrick Dubroyde7658b2010-09-27 11:15:43 -07002903 }
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -07002904
2905 // Invalidate the drag data
Adam Cohend41fbf52012-02-16 23:53:59 -08002906 mDragCell[0] = mDragCell[1] = -1;
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -07002907 mDragOutlineAnims[mDragOutlineCurrent].animateOut();
2908 mDragOutlineCurrent = (mDragOutlineCurrent + 1) % mDragOutlineAnims.length;
Adam Cohen19f37922012-03-21 11:59:11 -07002909 revertTempState();
Michael Jurka33945b22010-12-21 18:19:38 -08002910 setIsDragOverlapping(false);
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07002911 }
2912
2913 /**
Winson Chungaafa03c2010-06-11 17:34:16 -07002914 * Mark a child as having been dropped.
Patrick Dubroyde7658b2010-09-27 11:15:43 -07002915 * At the beginning of the drag operation, the child may have been on another
Patrick Dubroyce34a972010-10-19 10:34:32 -07002916 * screen, but it is re-parented before this method is called.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002917 *
2918 * @param child The child that is being dropped
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002919 */
Adam Cohen716b51e2011-06-30 12:09:54 -07002920 void onDropChild(View child) {
Romain Guyd94533d2009-08-17 10:01:15 -07002921 if (child != null) {
2922 LayoutParams lp = (LayoutParams) child.getLayoutParams();
Romain Guy84f296c2009-11-04 15:00:44 -08002923 lp.dropped = true;
Romain Guyd94533d2009-08-17 10:01:15 -07002924 child.requestLayout();
Romain Guyd94533d2009-08-17 10:01:15 -07002925 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002926 }
2927
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002928 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002929 * Computes a bounding rectangle for a range of cells
Winson Chungaafa03c2010-06-11 17:34:16 -07002930 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002931 * @param cellX X coordinate of upper left corner expressed as a cell position
2932 * @param cellY Y coordinate of upper left corner expressed as a cell position
Winson Chungaafa03c2010-06-11 17:34:16 -07002933 * @param cellHSpan Width in cells
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002934 * @param cellVSpan Height in cells
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07002935 * @param resultRect Rect into which to put the results
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002936 */
Adam Cohend41fbf52012-02-16 23:53:59 -08002937 public void cellToRect(int cellX, int cellY, int cellHSpan, int cellVSpan, Rect resultRect) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002938 final int cellWidth = mCellWidth;
2939 final int cellHeight = mCellHeight;
2940 final int widthGap = mWidthGap;
2941 final int heightGap = mHeightGap;
Winson Chungaafa03c2010-06-11 17:34:16 -07002942
Winson Chung4b825dcd2011-06-19 12:41:22 -07002943 final int hStartPadding = getPaddingLeft();
2944 final int vStartPadding = getPaddingTop();
Winson Chungaafa03c2010-06-11 17:34:16 -07002945
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002946 int width = cellHSpan * cellWidth + ((cellHSpan - 1) * widthGap);
2947 int height = cellVSpan * cellHeight + ((cellVSpan - 1) * heightGap);
2948
2949 int x = hStartPadding + cellX * (cellWidth + widthGap);
2950 int y = vStartPadding + cellY * (cellHeight + heightGap);
Winson Chungaafa03c2010-06-11 17:34:16 -07002951
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07002952 resultRect.set(x, y, x + width, y + height);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002953 }
Winson Chungaafa03c2010-06-11 17:34:16 -07002954
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002955 /**
Winson Chungaafa03c2010-06-11 17:34:16 -07002956 * Computes the required horizontal and vertical cell spans to always
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002957 * fit the given rectangle.
Winson Chungaafa03c2010-06-11 17:34:16 -07002958 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002959 * @param width Width in pixels
2960 * @param height Height in pixels
Patrick Dubroy8f86ddc2010-07-16 13:55:32 -07002961 * @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 -08002962 */
Patrick Dubroy8f86ddc2010-07-16 13:55:32 -07002963 public int[] rectToCell(int width, int height, int[] result) {
Michael Jurka9987a5c2010-10-08 16:58:12 -07002964 return rectToCell(getResources(), width, height, result);
2965 }
2966
2967 public static int[] rectToCell(Resources resources, int width, int height, int[] result) {
Winson Chung5f8afe62013-08-12 16:19:28 -07002968 LauncherAppState app = LauncherAppState.getInstance();
2969 DeviceProfile grid = app.getDynamicGrid().getDeviceProfile();
2970
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002971 // Always assume we're working with the smallest span to make sure we
2972 // reserve enough space in both orientations.
Winson Chung5f8afe62013-08-12 16:19:28 -07002973 int actualWidth = grid.cellWidthPx;
2974 int actualHeight = grid.cellHeightPx;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002975 int smallerSize = Math.min(actualWidth, actualHeight);
Joe Onorato79e56262009-09-21 15:23:04 -04002976
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002977 // Always round up to next largest cell
Winson Chung54c725c2011-08-03 12:03:40 -07002978 int spanX = (int) Math.ceil(width / (float) smallerSize);
2979 int spanY = (int) Math.ceil(height / (float) smallerSize);
Joe Onorato79e56262009-09-21 15:23:04 -04002980
Patrick Dubroy8f86ddc2010-07-16 13:55:32 -07002981 if (result == null) {
2982 return new int[] { spanX, spanY };
2983 }
2984 result[0] = spanX;
2985 result[1] = spanY;
2986 return result;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002987 }
2988
Michael Jurkaf12c75c2011-01-25 22:41:40 -08002989 public int[] cellSpansToSize(int hSpans, int vSpans) {
2990 int[] size = new int[2];
2991 size[0] = hSpans * mCellWidth + (hSpans - 1) * mWidthGap;
2992 size[1] = vSpans * mCellHeight + (vSpans - 1) * mHeightGap;
2993 return size;
2994 }
2995
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002996 /**
Patrick Dubroy047379a2010-12-19 22:02:04 -08002997 * Calculate the grid spans needed to fit given item
2998 */
2999 public void calculateSpans(ItemInfo info) {
3000 final int minWidth;
3001 final int minHeight;
3002
3003 if (info instanceof LauncherAppWidgetInfo) {
3004 minWidth = ((LauncherAppWidgetInfo) info).minWidth;
3005 minHeight = ((LauncherAppWidgetInfo) info).minHeight;
3006 } else if (info instanceof PendingAddWidgetInfo) {
3007 minWidth = ((PendingAddWidgetInfo) info).minWidth;
3008 minHeight = ((PendingAddWidgetInfo) info).minHeight;
3009 } else {
3010 // It's not a widget, so it must be 1x1
3011 info.spanX = info.spanY = 1;
3012 return;
3013 }
3014 int[] spans = rectToCell(minWidth, minHeight, null);
3015 info.spanX = spans[0];
3016 info.spanY = spans[1];
3017 }
3018
3019 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003020 * Find the first vacant cell, if there is one.
3021 *
3022 * @param vacant Holds the x and y coordinate of the vacant cell
3023 * @param spanX Horizontal cell span.
3024 * @param spanY Vertical cell span.
Winson Chungaafa03c2010-06-11 17:34:16 -07003025 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003026 * @return True if a vacant cell was found
3027 */
3028 public boolean getVacantCell(int[] vacant, int spanX, int spanY) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003029
Michael Jurka0280c3b2010-09-17 15:00:07 -07003030 return findVacantCell(vacant, spanX, spanY, mCountX, mCountY, mOccupied);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003031 }
3032
3033 static boolean findVacantCell(int[] vacant, int spanX, int spanY,
3034 int xCount, int yCount, boolean[][] occupied) {
3035
Adam Cohen2801caf2011-05-13 20:57:39 -07003036 for (int y = 0; y < yCount; y++) {
3037 for (int x = 0; x < xCount; x++) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003038 boolean available = !occupied[x][y];
3039out: for (int i = x; i < x + spanX - 1 && x < xCount; i++) {
3040 for (int j = y; j < y + spanY - 1 && y < yCount; j++) {
3041 available = available && !occupied[i][j];
3042 if (!available) break out;
3043 }
3044 }
3045
3046 if (available) {
3047 vacant[0] = x;
3048 vacant[1] = y;
3049 return true;
3050 }
3051 }
3052 }
3053
3054 return false;
3055 }
3056
Michael Jurka0280c3b2010-09-17 15:00:07 -07003057 private void clearOccupiedCells() {
3058 for (int x = 0; x < mCountX; x++) {
3059 for (int y = 0; y < mCountY; y++) {
3060 mOccupied[x][y] = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003061 }
3062 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07003063 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003064
Adam Cohend41fbf52012-02-16 23:53:59 -08003065 public void onMove(View view, int newCellX, int newCellY, int newSpanX, int newSpanY) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07003066 markCellsAsUnoccupiedForView(view);
Adam Cohen482ed822012-03-02 14:15:13 -08003067 markCellsForView(newCellX, newCellY, newSpanX, newSpanY, mOccupied, true);
Michael Jurka0280c3b2010-09-17 15:00:07 -07003068 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003069
Adam Cohend4844c32011-02-18 19:25:06 -08003070 public void markCellsAsOccupiedForView(View view) {
Adam Cohen482ed822012-03-02 14:15:13 -08003071 markCellsAsOccupiedForView(view, mOccupied);
3072 }
3073 public void markCellsAsOccupiedForView(View view, boolean[][] occupied) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07003074 if (view == null || view.getParent() != mShortcutsAndWidgets) return;
Michael Jurka0280c3b2010-09-17 15:00:07 -07003075 LayoutParams lp = (LayoutParams) view.getLayoutParams();
Adam Cohen482ed822012-03-02 14:15:13 -08003076 markCellsForView(lp.cellX, lp.cellY, lp.cellHSpan, lp.cellVSpan, occupied, true);
Michael Jurka0280c3b2010-09-17 15:00:07 -07003077 }
3078
Adam Cohend4844c32011-02-18 19:25:06 -08003079 public void markCellsAsUnoccupiedForView(View view) {
Adam Cohen482ed822012-03-02 14:15:13 -08003080 markCellsAsUnoccupiedForView(view, mOccupied);
3081 }
3082 public void markCellsAsUnoccupiedForView(View view, boolean occupied[][]) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07003083 if (view == null || view.getParent() != mShortcutsAndWidgets) return;
Michael Jurka0280c3b2010-09-17 15:00:07 -07003084 LayoutParams lp = (LayoutParams) view.getLayoutParams();
Adam Cohen482ed822012-03-02 14:15:13 -08003085 markCellsForView(lp.cellX, lp.cellY, lp.cellHSpan, lp.cellVSpan, occupied, false);
Michael Jurka0280c3b2010-09-17 15:00:07 -07003086 }
3087
Adam Cohen482ed822012-03-02 14:15:13 -08003088 private void markCellsForView(int cellX, int cellY, int spanX, int spanY, boolean[][] occupied,
3089 boolean value) {
3090 if (cellX < 0 || cellY < 0) return;
Michael Jurka0280c3b2010-09-17 15:00:07 -07003091 for (int x = cellX; x < cellX + spanX && x < mCountX; x++) {
3092 for (int y = cellY; y < cellY + spanY && y < mCountY; y++) {
Adam Cohen482ed822012-03-02 14:15:13 -08003093 occupied[x][y] = value;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003094 }
3095 }
3096 }
3097
Adam Cohen2801caf2011-05-13 20:57:39 -07003098 public int getDesiredWidth() {
Michael Jurka8b805b12012-04-18 14:23:14 -07003099 return getPaddingLeft() + getPaddingRight() + (mCountX * mCellWidth) +
Adam Cohen2801caf2011-05-13 20:57:39 -07003100 (Math.max((mCountX - 1), 0) * mWidthGap);
3101 }
3102
3103 public int getDesiredHeight() {
Michael Jurka8b805b12012-04-18 14:23:14 -07003104 return getPaddingTop() + getPaddingBottom() + (mCountY * mCellHeight) +
Adam Cohen2801caf2011-05-13 20:57:39 -07003105 (Math.max((mCountY - 1), 0) * mHeightGap);
3106 }
3107
Michael Jurka66d72172011-04-12 16:29:25 -07003108 public boolean isOccupied(int x, int y) {
3109 if (x < mCountX && y < mCountY) {
3110 return mOccupied[x][y];
3111 } else {
3112 throw new RuntimeException("Position exceeds the bound of this CellLayout");
3113 }
3114 }
3115
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003116 @Override
3117 public ViewGroup.LayoutParams generateLayoutParams(AttributeSet attrs) {
3118 return new CellLayout.LayoutParams(getContext(), attrs);
3119 }
3120
3121 @Override
3122 protected boolean checkLayoutParams(ViewGroup.LayoutParams p) {
3123 return p instanceof CellLayout.LayoutParams;
3124 }
3125
3126 @Override
3127 protected ViewGroup.LayoutParams generateLayoutParams(ViewGroup.LayoutParams p) {
3128 return new CellLayout.LayoutParams(p);
3129 }
3130
Winson Chungaafa03c2010-06-11 17:34:16 -07003131 public static class CellLayoutAnimationController extends LayoutAnimationController {
3132 public CellLayoutAnimationController(Animation animation, float delay) {
3133 super(animation, delay);
3134 }
3135
3136 @Override
3137 protected long getDelayForView(View view) {
3138 return (int) (Math.random() * 150);
3139 }
3140 }
3141
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003142 public static class LayoutParams extends ViewGroup.MarginLayoutParams {
3143 /**
3144 * Horizontal location of the item in the grid.
3145 */
3146 @ViewDebug.ExportedProperty
3147 public int cellX;
3148
3149 /**
3150 * Vertical location of the item in the grid.
3151 */
3152 @ViewDebug.ExportedProperty
3153 public int cellY;
3154
3155 /**
Adam Cohen482ed822012-03-02 14:15:13 -08003156 * Temporary horizontal location of the item in the grid during reorder
3157 */
3158 public int tmpCellX;
3159
3160 /**
3161 * Temporary vertical location of the item in the grid during reorder
3162 */
3163 public int tmpCellY;
3164
3165 /**
3166 * Indicates that the temporary coordinates should be used to layout the items
3167 */
3168 public boolean useTmpCoords;
3169
3170 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003171 * Number of cells spanned horizontally by the item.
3172 */
3173 @ViewDebug.ExportedProperty
3174 public int cellHSpan;
3175
3176 /**
3177 * Number of cells spanned vertically by the item.
3178 */
3179 @ViewDebug.ExportedProperty
3180 public int cellVSpan;
Winson Chungaafa03c2010-06-11 17:34:16 -07003181
Adam Cohen1b607ed2011-03-03 17:26:50 -08003182 /**
3183 * Indicates whether the item will set its x, y, width and height parameters freely,
3184 * or whether these will be computed based on cellX, cellY, cellHSpan and cellVSpan.
3185 */
Adam Cohend4844c32011-02-18 19:25:06 -08003186 public boolean isLockedToGrid = true;
3187
Adam Cohen482ed822012-03-02 14:15:13 -08003188 /**
Adam Cohenec40b2b2013-07-23 15:52:40 -07003189 * Indicates that this item should use the full extents of its parent.
3190 */
3191 public boolean isFullscreen = false;
3192
3193 /**
Adam Cohen482ed822012-03-02 14:15:13 -08003194 * Indicates whether this item can be reordered. Always true except in the case of the
3195 * the AllApps button.
3196 */
3197 public boolean canReorder = true;
3198
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003199 // X coordinate of the view in the layout.
3200 @ViewDebug.ExportedProperty
3201 int x;
3202 // Y coordinate of the view in the layout.
3203 @ViewDebug.ExportedProperty
3204 int y;
3205
Romain Guy84f296c2009-11-04 15:00:44 -08003206 boolean dropped;
Romain Guyfcb9e712009-10-02 16:06:52 -07003207
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003208 public LayoutParams(Context c, AttributeSet attrs) {
3209 super(c, attrs);
3210 cellHSpan = 1;
3211 cellVSpan = 1;
3212 }
3213
3214 public LayoutParams(ViewGroup.LayoutParams source) {
3215 super(source);
3216 cellHSpan = 1;
3217 cellVSpan = 1;
3218 }
Winson Chungaafa03c2010-06-11 17:34:16 -07003219
3220 public LayoutParams(LayoutParams source) {
3221 super(source);
3222 this.cellX = source.cellX;
3223 this.cellY = source.cellY;
3224 this.cellHSpan = source.cellHSpan;
3225 this.cellVSpan = source.cellVSpan;
3226 }
3227
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003228 public LayoutParams(int cellX, int cellY, int cellHSpan, int cellVSpan) {
Romain Guy8f19cdd2010-01-08 15:07:00 -08003229 super(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003230 this.cellX = cellX;
3231 this.cellY = cellY;
3232 this.cellHSpan = cellHSpan;
3233 this.cellVSpan = cellVSpan;
3234 }
3235
Adam Cohen2374abf2013-04-16 14:56:57 -07003236 public void setup(int cellWidth, int cellHeight, int widthGap, int heightGap,
3237 boolean invertHorizontally, int colCount) {
Adam Cohend4844c32011-02-18 19:25:06 -08003238 if (isLockedToGrid) {
3239 final int myCellHSpan = cellHSpan;
3240 final int myCellVSpan = cellVSpan;
Adam Cohen2374abf2013-04-16 14:56:57 -07003241 int myCellX = useTmpCoords ? tmpCellX : cellX;
3242 int myCellY = useTmpCoords ? tmpCellY : cellY;
3243
3244 if (invertHorizontally) {
3245 myCellX = colCount - myCellX - cellHSpan;
3246 }
Adam Cohen1b607ed2011-03-03 17:26:50 -08003247
Adam Cohend4844c32011-02-18 19:25:06 -08003248 width = myCellHSpan * cellWidth + ((myCellHSpan - 1) * widthGap) -
3249 leftMargin - rightMargin;
3250 height = myCellVSpan * cellHeight + ((myCellVSpan - 1) * heightGap) -
3251 topMargin - bottomMargin;
Winson Chungeecf02d2012-03-02 17:14:58 -08003252 x = (int) (myCellX * (cellWidth + widthGap) + leftMargin);
3253 y = (int) (myCellY * (cellHeight + heightGap) + topMargin);
Adam Cohend4844c32011-02-18 19:25:06 -08003254 }
3255 }
Winson Chungaafa03c2010-06-11 17:34:16 -07003256
Winson Chungaafa03c2010-06-11 17:34:16 -07003257 public String toString() {
3258 return "(" + this.cellX + ", " + this.cellY + ")";
3259 }
Adam Cohen7f4eabe2011-04-21 16:19:16 -07003260
3261 public void setWidth(int width) {
3262 this.width = width;
3263 }
3264
3265 public int getWidth() {
3266 return width;
3267 }
3268
3269 public void setHeight(int height) {
3270 this.height = height;
3271 }
3272
3273 public int getHeight() {
3274 return height;
3275 }
3276
3277 public void setX(int x) {
3278 this.x = x;
3279 }
3280
3281 public int getX() {
3282 return x;
3283 }
3284
3285 public void setY(int y) {
3286 this.y = y;
3287 }
3288
3289 public int getY() {
3290 return y;
3291 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003292 }
3293
Michael Jurka0280c3b2010-09-17 15:00:07 -07003294 // This class stores info for two purposes:
3295 // 1. When dragging items (mDragInfo in Workspace), we store the View, its cellX & cellY,
3296 // its spanX, spanY, and the screen it is on
3297 // 2. When long clicking on an empty cell in a CellLayout, we save information about the
3298 // cellX and cellY coordinates and which page was clicked. We then set this as a tag on
3299 // the CellLayout that was long clicked
Michael Jurkae5fb0f22011-04-11 13:27:46 -07003300 static final class CellInfo {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003301 View cell;
Michael Jurkaa63c4522010-08-19 13:52:27 -07003302 int cellX = -1;
3303 int cellY = -1;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003304 int spanX;
3305 int spanY;
Adam Cohendcd297f2013-06-18 13:13:40 -07003306 long screenId;
Winson Chung3d503fb2011-07-13 17:25:49 -07003307 long container;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003308
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003309 @Override
3310 public String toString() {
Winson Chungaafa03c2010-06-11 17:34:16 -07003311 return "Cell[view=" + (cell == null ? "null" : cell.getClass())
3312 + ", x=" + cellX + ", y=" + cellY + "]";
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003313 }
3314 }
Michael Jurkad771c962011-08-09 15:00:48 -07003315
3316 public boolean lastDownOnOccupiedCell() {
3317 return mLastDownOnOccupiedCell;
3318 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003319}