blob: 0a0452daf9b8dbaf28cd1fe35ab47f3d19b7ff3a [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
Winson Chung892c74d2013-08-22 16:15:50 -0700192 LauncherAppState app = LauncherAppState.getInstance();
193 DeviceProfile grid = app.getDynamicGrid().getDeviceProfile();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800194 TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.CellLayout, defStyle, 0);
195
Winson Chung5f8afe62013-08-12 16:19:28 -0700196 mCellWidth = -1;
197 mCellHeight = -1;
198 mWidthGap = mOriginalWidthGap = 0;
199 mHeightGap = mOriginalHeightGap = 0;
200 mMaxGap = Integer.MAX_VALUE;
Winson Chung892c74d2013-08-22 16:15:50 -0700201 mCountX = (int) grid.numColumns;
202 mCountY = (int) grid.numRows;
Michael Jurka0280c3b2010-09-17 15:00:07 -0700203 mOccupied = new boolean[mCountX][mCountY];
Adam Cohen482ed822012-03-02 14:15:13 -0800204 mTmpOccupied = new boolean[mCountX][mCountY];
Adam Cohen5b53f292012-03-29 14:30:35 -0700205 mPreviousReorderDirection[0] = INVALID_DIRECTION;
206 mPreviousReorderDirection[1] = INVALID_DIRECTION;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800207
208 a.recycle();
209
210 setAlwaysDrawnWithCacheEnabled(false);
211
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700212 final Resources res = getResources();
Winson Chung5f8afe62013-08-12 16:19:28 -0700213 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;
Winson Chung5f8afe62013-08-12 16:19:28 -0700625 bubbleChild.setTextVisibility(!mIsHotseat);
Andrew Flynn0dca1ec2012-02-29 13:33:22 -0800626 }
627
Adam Cohen307fe232012-08-16 17:55:58 -0700628 child.setScaleX(getChildrenScale());
629 child.setScaleY(getChildrenScale());
630
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800631 // Generate an id for each view, this assumes we have at most 256x256 cells
632 // per workspace screen
Adam Cohend22015c2010-07-26 22:02:18 -0700633 if (lp.cellX >= 0 && lp.cellX <= mCountX - 1 && lp.cellY >= 0 && lp.cellY <= mCountY - 1) {
Winson Chungaafa03c2010-06-11 17:34:16 -0700634 // If the horizontal or vertical span is set to -1, it is taken to
635 // mean that it spans the extent of the CellLayout
Adam Cohend22015c2010-07-26 22:02:18 -0700636 if (lp.cellHSpan < 0) lp.cellHSpan = mCountX;
637 if (lp.cellVSpan < 0) lp.cellVSpan = mCountY;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800638
Winson Chungaafa03c2010-06-11 17:34:16 -0700639 child.setId(childId);
640
Michael Jurkaa52570f2012-03-20 03:18:20 -0700641 mShortcutsAndWidgets.addView(child, index, lp);
Michael Jurkadee05892010-07-27 10:01:56 -0700642
Michael Jurkaf3ca3ab2010-10-20 17:08:24 -0700643 if (markCells) markCellsAsOccupiedForView(child);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700644
Winson Chungaafa03c2010-06-11 17:34:16 -0700645 return true;
646 }
647 return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800648 }
Michael Jurka3e7c7632010-10-02 16:01:03 -0700649
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800650 @Override
Michael Jurka0280c3b2010-09-17 15:00:07 -0700651 public void removeAllViews() {
652 clearOccupiedCells();
Michael Jurkaa52570f2012-03-20 03:18:20 -0700653 mShortcutsAndWidgets.removeAllViews();
Michael Jurka0280c3b2010-09-17 15:00:07 -0700654 }
655
656 @Override
657 public void removeAllViewsInLayout() {
Michael Jurkaa52570f2012-03-20 03:18:20 -0700658 if (mShortcutsAndWidgets.getChildCount() > 0) {
Michael Jurka7cfc2822011-08-02 20:19:24 -0700659 clearOccupiedCells();
Michael Jurkaa52570f2012-03-20 03:18:20 -0700660 mShortcutsAndWidgets.removeAllViewsInLayout();
Michael Jurka7cfc2822011-08-02 20:19:24 -0700661 }
Michael Jurka0280c3b2010-09-17 15:00:07 -0700662 }
663
Michael Jurkaf3ca3ab2010-10-20 17:08:24 -0700664 public void removeViewWithoutMarkingCells(View view) {
Michael Jurkaa52570f2012-03-20 03:18:20 -0700665 mShortcutsAndWidgets.removeView(view);
Michael Jurkaf3ca3ab2010-10-20 17:08:24 -0700666 }
667
Michael Jurka0280c3b2010-09-17 15:00:07 -0700668 @Override
669 public void removeView(View view) {
670 markCellsAsUnoccupiedForView(view);
Michael Jurkaa52570f2012-03-20 03:18:20 -0700671 mShortcutsAndWidgets.removeView(view);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700672 }
673
674 @Override
675 public void removeViewAt(int index) {
Michael Jurkaa52570f2012-03-20 03:18:20 -0700676 markCellsAsUnoccupiedForView(mShortcutsAndWidgets.getChildAt(index));
677 mShortcutsAndWidgets.removeViewAt(index);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700678 }
679
680 @Override
681 public void removeViewInLayout(View view) {
682 markCellsAsUnoccupiedForView(view);
Michael Jurkaa52570f2012-03-20 03:18:20 -0700683 mShortcutsAndWidgets.removeViewInLayout(view);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700684 }
685
686 @Override
687 public void removeViews(int start, int count) {
688 for (int i = start; i < start + count; i++) {
Michael Jurkaa52570f2012-03-20 03:18:20 -0700689 markCellsAsUnoccupiedForView(mShortcutsAndWidgets.getChildAt(i));
Michael Jurka0280c3b2010-09-17 15:00:07 -0700690 }
Michael Jurkaa52570f2012-03-20 03:18:20 -0700691 mShortcutsAndWidgets.removeViews(start, count);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700692 }
693
694 @Override
695 public void removeViewsInLayout(int start, int count) {
696 for (int i = start; i < start + count; i++) {
Michael Jurkaa52570f2012-03-20 03:18:20 -0700697 markCellsAsUnoccupiedForView(mShortcutsAndWidgets.getChildAt(i));
Michael Jurka0280c3b2010-09-17 15:00:07 -0700698 }
Michael Jurkaa52570f2012-03-20 03:18:20 -0700699 mShortcutsAndWidgets.removeViewsInLayout(start, count);
Michael Jurkaabded662011-03-04 12:06:57 -0800700 }
701
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800702 @Override
703 protected void onAttachedToWindow() {
704 super.onAttachedToWindow();
Adam Cohendcd297f2013-06-18 13:13:40 -0700705 if (getParent() instanceof Workspace) {
706 Workspace workspace = (Workspace) getParent();
707 mCellInfo.screenId = workspace.getIdForScreen(this);
708 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800709 }
710
Michael Jurkaaf442092010-06-10 17:01:57 -0700711 public void setTagToCellInfoForPoint(int touchX, int touchY) {
712 final CellInfo cellInfo = mCellInfo;
Winson Chungeecf02d2012-03-02 17:14:58 -0800713 Rect frame = mRect;
Michael Jurka8b805b12012-04-18 14:23:14 -0700714 final int x = touchX + getScrollX();
715 final int y = touchY + getScrollY();
Michael Jurkaa52570f2012-03-20 03:18:20 -0700716 final int count = mShortcutsAndWidgets.getChildCount();
Michael Jurkaaf442092010-06-10 17:01:57 -0700717
718 boolean found = false;
719 for (int i = count - 1; i >= 0; i--) {
Michael Jurkaa52570f2012-03-20 03:18:20 -0700720 final View child = mShortcutsAndWidgets.getChildAt(i);
Adam Cohend4844c32011-02-18 19:25:06 -0800721 final LayoutParams lp = (LayoutParams) child.getLayoutParams();
Michael Jurkaaf442092010-06-10 17:01:57 -0700722
Adam Cohen1b607ed2011-03-03 17:26:50 -0800723 if ((child.getVisibility() == VISIBLE || child.getAnimation() != null) &&
724 lp.isLockedToGrid) {
Michael Jurkaaf442092010-06-10 17:01:57 -0700725 child.getHitRect(frame);
Winson Chung0be025d2011-05-23 17:45:09 -0700726
Winson Chungeecf02d2012-03-02 17:14:58 -0800727 float scale = child.getScaleX();
728 frame = new Rect(child.getLeft(), child.getTop(), child.getRight(),
729 child.getBottom());
Winson Chung0be025d2011-05-23 17:45:09 -0700730 // The child hit rect is relative to the CellLayoutChildren parent, so we need to
731 // offset that by this CellLayout's padding to test an (x,y) point that is relative
732 // to this view.
Michael Jurka8b805b12012-04-18 14:23:14 -0700733 frame.offset(getPaddingLeft(), getPaddingTop());
Winson Chungeecf02d2012-03-02 17:14:58 -0800734 frame.inset((int) (frame.width() * (1f - scale) / 2),
735 (int) (frame.height() * (1f - scale) / 2));
Winson Chung0be025d2011-05-23 17:45:09 -0700736
Michael Jurkaaf442092010-06-10 17:01:57 -0700737 if (frame.contains(x, y)) {
Michael Jurkaaf442092010-06-10 17:01:57 -0700738 cellInfo.cell = child;
739 cellInfo.cellX = lp.cellX;
740 cellInfo.cellY = lp.cellY;
741 cellInfo.spanX = lp.cellHSpan;
742 cellInfo.spanY = lp.cellVSpan;
Michael Jurkaaf442092010-06-10 17:01:57 -0700743 found = true;
Michael Jurkaaf442092010-06-10 17:01:57 -0700744 break;
745 }
746 }
747 }
Winson Chungaafa03c2010-06-11 17:34:16 -0700748
Michael Jurkad771c962011-08-09 15:00:48 -0700749 mLastDownOnOccupiedCell = found;
750
Michael Jurkaaf442092010-06-10 17:01:57 -0700751 if (!found) {
Winson Chung0be025d2011-05-23 17:45:09 -0700752 final int cellXY[] = mTmpXY;
Michael Jurkaaf442092010-06-10 17:01:57 -0700753 pointToCellExact(x, y, cellXY);
754
Michael Jurkaaf442092010-06-10 17:01:57 -0700755 cellInfo.cell = null;
756 cellInfo.cellX = cellXY[0];
757 cellInfo.cellY = cellXY[1];
758 cellInfo.spanX = 1;
759 cellInfo.spanY = 1;
Michael Jurkaaf442092010-06-10 17:01:57 -0700760 }
761 setTag(cellInfo);
762 }
763
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800764 @Override
765 public boolean onInterceptTouchEvent(MotionEvent ev) {
Adam Cohenc1997fd2011-08-15 18:26:39 -0700766 // First we clear the tag to ensure that on every touch down we start with a fresh slate,
767 // even in the case where we return early. Not clearing here was causing bugs whereby on
768 // long-press we'd end up picking up an item from a previous drag operation.
769 final int action = ev.getAction();
770
771 if (action == MotionEvent.ACTION_DOWN) {
772 clearTagCellInfo();
773 }
774
Michael Jurkadee05892010-07-27 10:01:56 -0700775 if (mInterceptTouchListener != null && mInterceptTouchListener.onTouch(this, ev)) {
776 return true;
777 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800778
779 if (action == MotionEvent.ACTION_DOWN) {
Michael Jurkaaf442092010-06-10 17:01:57 -0700780 setTagToCellInfoForPoint((int) ev.getX(), (int) ev.getY());
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800781 }
Winson Chungeecf02d2012-03-02 17:14:58 -0800782
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800783 return false;
784 }
785
Adam Cohenc1997fd2011-08-15 18:26:39 -0700786 private void clearTagCellInfo() {
787 final CellInfo cellInfo = mCellInfo;
788 cellInfo.cell = null;
789 cellInfo.cellX = -1;
790 cellInfo.cellY = -1;
791 cellInfo.spanX = 0;
792 cellInfo.spanY = 0;
793 setTag(cellInfo);
794 }
795
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800796 public CellInfo getTag() {
Michael Jurka0280c3b2010-09-17 15:00:07 -0700797 return (CellInfo) super.getTag();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800798 }
799
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700800 /**
Winson Chungaafa03c2010-06-11 17:34:16 -0700801 * Given a point, return the cell that strictly encloses that point
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800802 * @param x X coordinate of the point
803 * @param y Y coordinate of the point
804 * @param result Array of 2 ints to hold the x and y coordinate of the cell
805 */
806 void pointToCellExact(int x, int y, int[] result) {
Winson Chung4b825dcd2011-06-19 12:41:22 -0700807 final int hStartPadding = getPaddingLeft();
808 final int vStartPadding = getPaddingTop();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800809
810 result[0] = (x - hStartPadding) / (mCellWidth + mWidthGap);
811 result[1] = (y - vStartPadding) / (mCellHeight + mHeightGap);
812
Adam Cohend22015c2010-07-26 22:02:18 -0700813 final int xAxis = mCountX;
814 final int yAxis = mCountY;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800815
816 if (result[0] < 0) result[0] = 0;
817 if (result[0] >= xAxis) result[0] = xAxis - 1;
818 if (result[1] < 0) result[1] = 0;
819 if (result[1] >= yAxis) result[1] = yAxis - 1;
820 }
Winson Chungaafa03c2010-06-11 17:34:16 -0700821
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800822 /**
823 * Given a point, return the cell that most closely encloses that point
824 * @param x X coordinate of the point
825 * @param y Y coordinate of the point
826 * @param result Array of 2 ints to hold the x and y coordinate of the cell
827 */
828 void pointToCellRounded(int x, int y, int[] result) {
829 pointToCellExact(x + (mCellWidth / 2), y + (mCellHeight / 2), result);
830 }
831
832 /**
833 * Given a cell coordinate, return the point that represents the upper left corner of that cell
Winson Chungaafa03c2010-06-11 17:34:16 -0700834 *
835 * @param cellX X coordinate of the cell
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800836 * @param cellY Y coordinate of the cell
Winson Chungaafa03c2010-06-11 17:34:16 -0700837 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800838 * @param result Array of 2 ints to hold the x and y coordinate of the point
839 */
840 void cellToPoint(int cellX, int cellY, int[] result) {
Winson Chung4b825dcd2011-06-19 12:41:22 -0700841 final int hStartPadding = getPaddingLeft();
842 final int vStartPadding = getPaddingTop();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800843
844 result[0] = hStartPadding + cellX * (mCellWidth + mWidthGap);
845 result[1] = vStartPadding + cellY * (mCellHeight + mHeightGap);
846 }
847
Adam Cohene3e27a82011-04-15 12:07:39 -0700848 /**
Adam Cohen482ed822012-03-02 14:15:13 -0800849 * Given a cell coordinate, return the point that represents the center of the cell
Adam Cohene3e27a82011-04-15 12:07:39 -0700850 *
851 * @param cellX X coordinate of the cell
852 * @param cellY Y coordinate of the cell
853 *
854 * @param result Array of 2 ints to hold the x and y coordinate of the point
855 */
856 void cellToCenterPoint(int cellX, int cellY, int[] result) {
Adam Cohen47a876d2012-03-19 13:21:41 -0700857 regionToCenterPoint(cellX, cellY, 1, 1, result);
858 }
859
860 /**
861 * Given a cell coordinate and span return the point that represents the center of the regio
862 *
863 * @param cellX X coordinate of the cell
864 * @param cellY Y coordinate of the cell
865 *
866 * @param result Array of 2 ints to hold the x and y coordinate of the point
867 */
868 void regionToCenterPoint(int cellX, int cellY, int spanX, int spanY, int[] result) {
Winson Chung4b825dcd2011-06-19 12:41:22 -0700869 final int hStartPadding = getPaddingLeft();
870 final int vStartPadding = getPaddingTop();
Adam Cohen47a876d2012-03-19 13:21:41 -0700871 result[0] = hStartPadding + cellX * (mCellWidth + mWidthGap) +
872 (spanX * mCellWidth + (spanX - 1) * mWidthGap) / 2;
873 result[1] = vStartPadding + cellY * (mCellHeight + mHeightGap) +
874 (spanY * mCellHeight + (spanY - 1) * mHeightGap) / 2;
Adam Cohene3e27a82011-04-15 12:07:39 -0700875 }
876
Adam Cohen19f37922012-03-21 11:59:11 -0700877 /**
878 * Given a cell coordinate and span fills out a corresponding pixel rect
879 *
880 * @param cellX X coordinate of the cell
881 * @param cellY Y coordinate of the cell
882 * @param result Rect in which to write the result
883 */
884 void regionToRect(int cellX, int cellY, int spanX, int spanY, Rect result) {
885 final int hStartPadding = getPaddingLeft();
886 final int vStartPadding = getPaddingTop();
887 final int left = hStartPadding + cellX * (mCellWidth + mWidthGap);
888 final int top = vStartPadding + cellY * (mCellHeight + mHeightGap);
889 result.set(left, top, left + (spanX * mCellWidth + (spanX - 1) * mWidthGap),
890 top + (spanY * mCellHeight + (spanY - 1) * mHeightGap));
891 }
892
Adam Cohen482ed822012-03-02 14:15:13 -0800893 public float getDistanceFromCell(float x, float y, int[] cell) {
894 cellToCenterPoint(cell[0], cell[1], mTmpPoint);
895 float distance = (float) Math.sqrt( Math.pow(x - mTmpPoint[0], 2) +
896 Math.pow(y - mTmpPoint[1], 2));
897 return distance;
898 }
899
Romain Guy84f296c2009-11-04 15:00:44 -0800900 int getCellWidth() {
901 return mCellWidth;
902 }
903
904 int getCellHeight() {
905 return mCellHeight;
906 }
907
Adam Cohend4844c32011-02-18 19:25:06 -0800908 int getWidthGap() {
909 return mWidthGap;
910 }
911
912 int getHeightGap() {
913 return mHeightGap;
914 }
915
Adam Cohen7f4eabe2011-04-21 16:19:16 -0700916 Rect getContentRect(Rect r) {
917 if (r == null) {
918 r = new Rect();
919 }
920 int left = getPaddingLeft();
921 int top = getPaddingTop();
Michael Jurka8b805b12012-04-18 14:23:14 -0700922 int right = left + getWidth() - getPaddingLeft() - getPaddingRight();
923 int bottom = top + getHeight() - getPaddingTop() - getPaddingBottom();
Adam Cohen7f4eabe2011-04-21 16:19:16 -0700924 r.set(left, top, right, bottom);
925 return r;
926 }
927
Winson Chung66700732013-08-20 16:56:15 -0700928 static void getMetrics(Rect metrics, int paddedMeasureWidth,
929 int paddedMeasureHeight, int countX, int countY) {
Winson Chung5f8afe62013-08-12 16:19:28 -0700930 LauncherAppState app = LauncherAppState.getInstance();
931 DeviceProfile grid = app.getDynamicGrid().getDeviceProfile();
Winson Chung66700732013-08-20 16:56:15 -0700932 metrics.set(grid.calculateCellWidth(paddedMeasureWidth, countX),
933 grid.calculateCellHeight(paddedMeasureHeight, countY), 0, 0);
Adam Cohenf4bd5792012-04-27 11:35:29 -0700934 }
935
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700936 public void setFixedSize(int width, int height) {
937 mFixedWidth = width;
938 mFixedHeight = height;
939 }
940
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800941 @Override
942 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
Winson Chung5f8afe62013-08-12 16:19:28 -0700943 LauncherAppState app = LauncherAppState.getInstance();
944 DeviceProfile grid = app.getDynamicGrid().getDeviceProfile();
945
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800946 int widthSpecMode = MeasureSpec.getMode(widthMeasureSpec);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800947 int heightSpecMode = MeasureSpec.getMode(heightMeasureSpec);
Winson Chung5f8afe62013-08-12 16:19:28 -0700948 int widthSize = MeasureSpec.getSize(widthMeasureSpec);
949 int heightSize = MeasureSpec.getSize(heightMeasureSpec);
950 if (mCellWidth < 0 || mCellHeight < 0) {
951 mCellWidth = grid.calculateCellWidth(widthSize, mCountX);
952 mCellHeight = grid.calculateCellHeight(heightSize, mCountY);
953 mShortcutsAndWidgets.setCellDimensions(mCellWidth, mCellHeight, mWidthGap,
954 mHeightGap, mCountX, mCountY);
955 }
Winson Chungaafa03c2010-06-11 17:34:16 -0700956
Winson Chung5f8afe62013-08-12 16:19:28 -0700957 int newWidth = widthSize;
958 int newHeight = heightSize;
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700959 if (mFixedWidth > 0 && mFixedHeight > 0) {
960 newWidth = mFixedWidth;
961 newHeight = mFixedHeight;
962 } else if (widthSpecMode == MeasureSpec.UNSPECIFIED || heightSpecMode == MeasureSpec.UNSPECIFIED) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800963 throw new RuntimeException("CellLayout cannot have UNSPECIFIED dimensions");
964 }
965
Adam Cohend22015c2010-07-26 22:02:18 -0700966 int numWidthGaps = mCountX - 1;
967 int numHeightGaps = mCountY - 1;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800968
Adam Cohen234c4cd2011-07-17 21:03:04 -0700969 if (mOriginalWidthGap < 0 || mOriginalHeightGap < 0) {
Winson Chung5f8afe62013-08-12 16:19:28 -0700970 int hSpace = widthSize - getPaddingLeft() - getPaddingRight();
971 int vSpace = heightSize - getPaddingTop() - getPaddingBottom();
Adam Cohenf4bd5792012-04-27 11:35:29 -0700972 int hFreeSpace = hSpace - (mCountX * mCellWidth);
973 int vFreeSpace = vSpace - (mCountY * mCellHeight);
Winson Chung4b825dcd2011-06-19 12:41:22 -0700974 mWidthGap = Math.min(mMaxGap, numWidthGaps > 0 ? (hFreeSpace / numWidthGaps) : 0);
975 mHeightGap = Math.min(mMaxGap,numHeightGaps > 0 ? (vFreeSpace / numHeightGaps) : 0);
Winson Chung5f8afe62013-08-12 16:19:28 -0700976 mShortcutsAndWidgets.setCellDimensions(mCellWidth, mCellHeight, mWidthGap,
977 mHeightGap, mCountX, mCountY);
Adam Cohen234c4cd2011-07-17 21:03:04 -0700978 } else {
979 mWidthGap = mOriginalWidthGap;
980 mHeightGap = mOriginalHeightGap;
Winson Chungece7f5b2010-10-22 14:54:12 -0700981 }
Michael Jurka8c920dd2011-01-20 14:16:56 -0800982 int count = getChildCount();
Winson Chung5f8afe62013-08-12 16:19:28 -0700983 int maxWidth = 0;
984 int maxHeight = 0;
Michael Jurka8c920dd2011-01-20 14:16:56 -0800985 for (int i = 0; i < count; i++) {
986 View child = getChildAt(i);
Michael Jurka8b805b12012-04-18 14:23:14 -0700987 int childWidthMeasureSpec = MeasureSpec.makeMeasureSpec(newWidth - getPaddingLeft() -
988 getPaddingRight(), MeasureSpec.EXACTLY);
989 int childheightMeasureSpec = MeasureSpec.makeMeasureSpec(newHeight - getPaddingTop() -
990 getPaddingBottom(), MeasureSpec.EXACTLY);
Michael Jurka8c920dd2011-01-20 14:16:56 -0800991 child.measure(childWidthMeasureSpec, childheightMeasureSpec);
Winson Chung5f8afe62013-08-12 16:19:28 -0700992 maxWidth = Math.max(maxWidth, child.getMeasuredWidth());
993 maxHeight = Math.max(maxHeight, child.getMeasuredHeight());
Michael Jurka8c920dd2011-01-20 14:16:56 -0800994 }
Winson Chung5f8afe62013-08-12 16:19:28 -0700995 setMeasuredDimension(maxWidth, maxHeight);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800996 }
997
998 @Override
Michael Jurka28750fb2010-09-24 17:43:49 -0700999 protected void onLayout(boolean changed, int l, int t, int r, int b) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001000 int count = getChildCount();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001001 for (int i = 0; i < count; i++) {
Michael Jurka8c920dd2011-01-20 14:16:56 -08001002 View child = getChildAt(i);
Michael Jurka8b805b12012-04-18 14:23:14 -07001003 child.layout(getPaddingLeft(), getPaddingTop(),
1004 r - l - getPaddingRight(), b - t - getPaddingBottom());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001005 }
1006 }
1007
1008 @Override
Michael Jurkadee05892010-07-27 10:01:56 -07001009 protected void onSizeChanged(int w, int h, int oldw, int oldh) {
1010 super.onSizeChanged(w, h, oldw, oldh);
Michael Jurka18014792010-10-14 09:01:34 -07001011 mBackgroundRect.set(0, 0, w, h);
Adam Cohenb5ba0972011-09-07 18:02:31 -07001012 mForegroundRect.set(mForegroundPadding, mForegroundPadding,
Adam Cohen215b4162012-08-30 13:14:08 -07001013 w - mForegroundPadding, h - mForegroundPadding);
Michael Jurkadee05892010-07-27 10:01:56 -07001014 }
1015
1016 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001017 protected void setChildrenDrawingCacheEnabled(boolean enabled) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07001018 mShortcutsAndWidgets.setChildrenDrawingCacheEnabled(enabled);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001019 }
1020
1021 @Override
1022 protected void setChildrenDrawnWithCacheEnabled(boolean enabled) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07001023 mShortcutsAndWidgets.setChildrenDrawnWithCacheEnabled(enabled);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001024 }
1025
Michael Jurka5f1c5092010-09-03 14:15:02 -07001026 public float getBackgroundAlpha() {
1027 return mBackgroundAlpha;
Michael Jurkadee05892010-07-27 10:01:56 -07001028 }
1029
Adam Cohen1b0aaac2010-10-28 11:11:18 -07001030 public void setBackgroundAlphaMultiplier(float multiplier) {
Michael Jurkaa3d30ad2012-05-08 13:43:43 -07001031 if (mBackgroundAlphaMultiplier != multiplier) {
1032 mBackgroundAlphaMultiplier = multiplier;
1033 invalidate();
1034 }
Adam Cohen1b0aaac2010-10-28 11:11:18 -07001035 }
1036
Adam Cohenddb82192010-11-10 16:32:54 -08001037 public float getBackgroundAlphaMultiplier() {
1038 return mBackgroundAlphaMultiplier;
1039 }
1040
Michael Jurka5f1c5092010-09-03 14:15:02 -07001041 public void setBackgroundAlpha(float alpha) {
Michael Jurkaafaa0502011-12-13 18:22:50 -08001042 if (mBackgroundAlpha != alpha) {
1043 mBackgroundAlpha = alpha;
1044 invalidate();
1045 }
Michael Jurkadee05892010-07-27 10:01:56 -07001046 }
1047
Michael Jurkaa52570f2012-03-20 03:18:20 -07001048 public void setShortcutAndWidgetAlpha(float alpha) {
Michael Jurka0142d492010-08-25 17:46:15 -07001049 final int childCount = getChildCount();
1050 for (int i = 0; i < childCount; i++) {
Michael Jurkadee05892010-07-27 10:01:56 -07001051 getChildAt(i).setAlpha(alpha);
1052 }
1053 }
1054
Michael Jurkaa52570f2012-03-20 03:18:20 -07001055 public ShortcutAndWidgetContainer getShortcutsAndWidgets() {
1056 if (getChildCount() > 0) {
1057 return (ShortcutAndWidgetContainer) getChildAt(0);
1058 }
1059 return null;
1060 }
1061
Patrick Dubroy440c3602010-07-13 17:50:32 -07001062 public View getChildAt(int x, int y) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07001063 return mShortcutsAndWidgets.getChildAt(x, y);
Patrick Dubroy440c3602010-07-13 17:50:32 -07001064 }
1065
Adam Cohen76fc0852011-06-17 13:26:23 -07001066 public boolean animateChildToPosition(final View child, int cellX, int cellY, int duration,
Adam Cohen482ed822012-03-02 14:15:13 -08001067 int delay, boolean permanent, boolean adjustOccupied) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07001068 ShortcutAndWidgetContainer clc = getShortcutsAndWidgets();
Adam Cohen482ed822012-03-02 14:15:13 -08001069 boolean[][] occupied = mOccupied;
1070 if (!permanent) {
1071 occupied = mTmpOccupied;
1072 }
1073
Adam Cohen19f37922012-03-21 11:59:11 -07001074 if (clc.indexOfChild(child) != -1) {
Adam Cohenbfbfd262011-06-13 16:55:12 -07001075 final LayoutParams lp = (LayoutParams) child.getLayoutParams();
1076 final ItemInfo info = (ItemInfo) child.getTag();
1077
1078 // We cancel any existing animations
1079 if (mReorderAnimators.containsKey(lp)) {
1080 mReorderAnimators.get(lp).cancel();
1081 mReorderAnimators.remove(lp);
1082 }
1083
Adam Cohen482ed822012-03-02 14:15:13 -08001084 final int oldX = lp.x;
1085 final int oldY = lp.y;
1086 if (adjustOccupied) {
1087 occupied[lp.cellX][lp.cellY] = false;
1088 occupied[cellX][cellY] = true;
1089 }
Adam Cohenbfbfd262011-06-13 16:55:12 -07001090 lp.isLockedToGrid = true;
Adam Cohen482ed822012-03-02 14:15:13 -08001091 if (permanent) {
1092 lp.cellX = info.cellX = cellX;
1093 lp.cellY = info.cellY = cellY;
1094 } else {
1095 lp.tmpCellX = cellX;
1096 lp.tmpCellY = cellY;
1097 }
Adam Cohenbfbfd262011-06-13 16:55:12 -07001098 clc.setupLp(lp);
1099 lp.isLockedToGrid = false;
Adam Cohen482ed822012-03-02 14:15:13 -08001100 final int newX = lp.x;
1101 final int newY = lp.y;
Adam Cohenbfbfd262011-06-13 16:55:12 -07001102
Adam Cohen76fc0852011-06-17 13:26:23 -07001103 lp.x = oldX;
1104 lp.y = oldY;
Adam Cohen76fc0852011-06-17 13:26:23 -07001105
Adam Cohen482ed822012-03-02 14:15:13 -08001106 // Exit early if we're not actually moving the view
1107 if (oldX == newX && oldY == newY) {
1108 lp.isLockedToGrid = true;
1109 return true;
1110 }
1111
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001112 ValueAnimator va = LauncherAnimUtils.ofFloat(child, 0f, 1f);
Adam Cohen482ed822012-03-02 14:15:13 -08001113 va.setDuration(duration);
1114 mReorderAnimators.put(lp, va);
1115
1116 va.addUpdateListener(new AnimatorUpdateListener() {
1117 @Override
Adam Cohenbfbfd262011-06-13 16:55:12 -07001118 public void onAnimationUpdate(ValueAnimator animation) {
Adam Cohen482ed822012-03-02 14:15:13 -08001119 float r = ((Float) animation.getAnimatedValue()).floatValue();
Adam Cohen19f37922012-03-21 11:59:11 -07001120 lp.x = (int) ((1 - r) * oldX + r * newX);
1121 lp.y = (int) ((1 - r) * oldY + r * newY);
Adam Cohen6b8a02d2012-03-22 15:13:40 -07001122 child.requestLayout();
Adam Cohenbfbfd262011-06-13 16:55:12 -07001123 }
1124 });
Adam Cohen482ed822012-03-02 14:15:13 -08001125 va.addListener(new AnimatorListenerAdapter() {
Adam Cohenbfbfd262011-06-13 16:55:12 -07001126 boolean cancelled = false;
1127 public void onAnimationEnd(Animator animation) {
1128 // If the animation was cancelled, it means that another animation
1129 // has interrupted this one, and we don't want to lock the item into
1130 // place just yet.
1131 if (!cancelled) {
1132 lp.isLockedToGrid = true;
Adam Cohen482ed822012-03-02 14:15:13 -08001133 child.requestLayout();
Adam Cohenbfbfd262011-06-13 16:55:12 -07001134 }
1135 if (mReorderAnimators.containsKey(lp)) {
1136 mReorderAnimators.remove(lp);
1137 }
1138 }
1139 public void onAnimationCancel(Animator animation) {
1140 cancelled = true;
1141 }
1142 });
Adam Cohen482ed822012-03-02 14:15:13 -08001143 va.setStartDelay(delay);
1144 va.start();
Adam Cohenbfbfd262011-06-13 16:55:12 -07001145 return true;
1146 }
1147 return false;
1148 }
1149
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001150 /**
1151 * Estimate where the top left cell of the dragged item will land if it is dropped.
1152 *
1153 * @param originX The X value of the top left corner of the item
1154 * @param originY The Y value of the top left corner of the item
1155 * @param spanX The number of horizontal cells that the item spans
1156 * @param spanY The number of vertical cells that the item spans
1157 * @param result The estimated drop cell X and Y.
1158 */
1159 void estimateDropCell(int originX, int originY, int spanX, int spanY, int[] result) {
Adam Cohend22015c2010-07-26 22:02:18 -07001160 final int countX = mCountX;
1161 final int countY = mCountY;
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001162
Michael Jurkaa63c4522010-08-19 13:52:27 -07001163 // pointToCellRounded takes the top left of a cell but will pad that with
1164 // cellWidth/2 and cellHeight/2 when finding the matching cell
1165 pointToCellRounded(originX, originY, result);
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001166
1167 // If the item isn't fully on this screen, snap to the edges
1168 int rightOverhang = result[0] + spanX - countX;
1169 if (rightOverhang > 0) {
1170 result[0] -= rightOverhang; // Snap to right
1171 }
1172 result[0] = Math.max(0, result[0]); // Snap to left
1173 int bottomOverhang = result[1] + spanY - countY;
1174 if (bottomOverhang > 0) {
1175 result[1] -= bottomOverhang; // Snap to bottom
1176 }
1177 result[1] = Math.max(0, result[1]); // Snap to top
1178 }
1179
Adam Cohen482ed822012-03-02 14:15:13 -08001180 void visualizeDropLocation(View v, Bitmap dragOutline, int originX, int originY, int cellX,
1181 int cellY, int spanX, int spanY, boolean resize, Point dragOffset, Rect dragRegion) {
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -07001182 final int oldDragCellX = mDragCell[0];
1183 final int oldDragCellY = mDragCell[1];
Adam Cohen482ed822012-03-02 14:15:13 -08001184
Winson Chungb8c69f32011-10-19 21:36:08 -07001185 if (v != null && dragOffset == null) {
Winson Chunga9abd0e2010-10-27 17:18:37 -07001186 mDragCenter.set(originX + (v.getWidth() / 2), originY + (v.getHeight() / 2));
1187 } else {
1188 mDragCenter.set(originX, originY);
1189 }
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001190
Adam Cohen2801caf2011-05-13 20:57:39 -07001191 if (dragOutline == null && v == null) {
Adam Cohen2801caf2011-05-13 20:57:39 -07001192 return;
1193 }
1194
Adam Cohen482ed822012-03-02 14:15:13 -08001195 if (cellX != oldDragCellX || cellY != oldDragCellY) {
1196 mDragCell[0] = cellX;
1197 mDragCell[1] = cellY;
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001198 // Find the top left corner of the rect the object will occupy
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001199 final int[] topLeft = mTmpPoint;
Adam Cohen482ed822012-03-02 14:15:13 -08001200 cellToPoint(cellX, cellY, topLeft);
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001201
Joe Onorato4be866d2010-10-10 11:26:02 -07001202 int left = topLeft[0];
1203 int top = topLeft[1];
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001204
Winson Chung892c74d2013-08-22 16:15:50 -07001205 // Offset icons by their padding
1206 if (v instanceof BubbleTextView) {
1207 top += v.getPaddingTop();
1208 }
1209
Winson Chungb8c69f32011-10-19 21:36:08 -07001210 if (v != null && dragOffset == null) {
Adam Cohen99e8b402011-03-25 19:23:43 -07001211 // When drawing the drag outline, it did not account for margin offsets
1212 // added by the view's parent.
1213 MarginLayoutParams lp = (MarginLayoutParams) v.getLayoutParams();
1214 left += lp.leftMargin;
1215 top += lp.topMargin;
Winson Chung150fbab2010-09-29 17:14:26 -07001216
Adam Cohen99e8b402011-03-25 19:23:43 -07001217 // Offsets due to the size difference between the View and the dragOutline.
1218 // There is a size difference to account for the outer blur, which may lie
1219 // outside the bounds of the view.
Winson Chunga9abd0e2010-10-27 17:18:37 -07001220 top += (v.getHeight() - dragOutline.getHeight()) / 2;
Adam Cohenae915ce2011-08-25 13:47:22 -07001221 // We center about the x axis
1222 left += ((mCellWidth * spanX) + ((spanX - 1) * mWidthGap)
1223 - dragOutline.getWidth()) / 2;
Adam Cohen66396872011-04-15 17:50:36 -07001224 } else {
Winson Chungb8c69f32011-10-19 21:36:08 -07001225 if (dragOffset != null && dragRegion != null) {
1226 // Center the drag region *horizontally* in the cell and apply a drag
1227 // outline offset
1228 left += dragOffset.x + ((mCellWidth * spanX) + ((spanX - 1) * mWidthGap)
1229 - dragRegion.width()) / 2;
1230 top += dragOffset.y;
1231 } else {
1232 // Center the drag outline in the cell
1233 left += ((mCellWidth * spanX) + ((spanX - 1) * mWidthGap)
1234 - dragOutline.getWidth()) / 2;
1235 top += ((mCellHeight * spanY) + ((spanY - 1) * mHeightGap)
1236 - dragOutline.getHeight()) / 2;
1237 }
Winson Chunga9abd0e2010-10-27 17:18:37 -07001238 }
Joe Onorato4be866d2010-10-10 11:26:02 -07001239 final int oldIndex = mDragOutlineCurrent;
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -07001240 mDragOutlineAnims[oldIndex].animateOut();
1241 mDragOutlineCurrent = (oldIndex + 1) % mDragOutlines.length;
Adam Cohend41fbf52012-02-16 23:53:59 -08001242 Rect r = mDragOutlines[mDragOutlineCurrent];
1243 r.set(left, top, left + dragOutline.getWidth(), top + dragOutline.getHeight());
1244 if (resize) {
Adam Cohen482ed822012-03-02 14:15:13 -08001245 cellToRect(cellX, cellY, spanX, spanY, r);
Adam Cohend41fbf52012-02-16 23:53:59 -08001246 }
Winson Chung150fbab2010-09-29 17:14:26 -07001247
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -07001248 mDragOutlineAnims[mDragOutlineCurrent].setTag(dragOutline);
1249 mDragOutlineAnims[mDragOutlineCurrent].animateIn();
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001250 }
1251 }
1252
Adam Cohene0310962011-04-18 16:15:31 -07001253 public void clearDragOutlines() {
1254 final int oldIndex = mDragOutlineCurrent;
1255 mDragOutlineAnims[oldIndex].animateOut();
Adam Cohend41fbf52012-02-16 23:53:59 -08001256 mDragCell[0] = mDragCell[1] = -1;
Adam Cohene0310962011-04-18 16:15:31 -07001257 }
1258
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001259 /**
Jeff Sharkey70864282009-04-07 21:08:40 -07001260 * Find a vacant area that will fit the given bounds nearest the requested
1261 * cell location. Uses Euclidean distance to score multiple vacant areas.
Winson Chungaafa03c2010-06-11 17:34:16 -07001262 *
Romain Guy51afc022009-05-04 18:03:43 -07001263 * @param pixelX The X location at which you want to search for a vacant area.
1264 * @param pixelY The Y location at which you want to search for a vacant area.
Jeff Sharkey70864282009-04-07 21:08:40 -07001265 * @param spanX Horizontal span of the object.
1266 * @param spanY Vertical span of the object.
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001267 * @param result Array in which to place the result, or null (in which case a new array will
1268 * be allocated)
Jeff Sharkey70864282009-04-07 21:08:40 -07001269 * @return The X, Y cell of a vacant area that can contain this object,
1270 * nearest the requested location.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001271 */
Adam Cohend41fbf52012-02-16 23:53:59 -08001272 int[] findNearestVacantArea(int pixelX, int pixelY, int spanX, int spanY,
1273 int[] result) {
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001274 return findNearestVacantArea(pixelX, pixelY, spanX, spanY, null, result);
Michael Jurka6a1435d2010-09-27 17:35:12 -07001275 }
Winson Chungaafa03c2010-06-11 17:34:16 -07001276
Michael Jurka6a1435d2010-09-27 17:35:12 -07001277 /**
1278 * Find a vacant area that will fit the given bounds nearest the requested
1279 * cell location. Uses Euclidean distance to score multiple vacant areas.
1280 *
1281 * @param pixelX The X location at which you want to search for a vacant area.
1282 * @param pixelY The Y location at which you want to search for a vacant area.
Adam Cohend41fbf52012-02-16 23:53:59 -08001283 * @param minSpanX The minimum horizontal span required
1284 * @param minSpanY The minimum vertical span required
1285 * @param spanX Horizontal span of the object.
1286 * @param spanY Vertical span of the object.
1287 * @param result Array in which to place the result, or null (in which case a new array will
1288 * be allocated)
1289 * @return The X, Y cell of a vacant area that can contain this object,
1290 * nearest the requested location.
1291 */
1292 int[] findNearestVacantArea(int pixelX, int pixelY, int minSpanX, int minSpanY, int spanX,
1293 int spanY, int[] result, int[] resultSpan) {
1294 return findNearestVacantArea(pixelX, pixelY, minSpanX, minSpanY, spanX, spanY, null,
1295 result, resultSpan);
1296 }
1297
1298 /**
1299 * Find a vacant area that will fit the given bounds nearest the requested
1300 * cell location. Uses Euclidean distance to score multiple vacant areas.
1301 *
1302 * @param pixelX The X location at which you want to search for a vacant area.
1303 * @param pixelY The Y location at which you want to search for a vacant area.
Michael Jurka6a1435d2010-09-27 17:35:12 -07001304 * @param spanX Horizontal span of the object.
1305 * @param spanY Vertical span of the object.
Adam Cohendf035382011-04-11 17:22:04 -07001306 * @param ignoreOccupied If true, the result can be an occupied cell
1307 * @param result Array in which to place the result, or null (in which case a new array will
1308 * be allocated)
Michael Jurka6a1435d2010-09-27 17:35:12 -07001309 * @return The X, Y cell of a vacant area that can contain this object,
1310 * nearest the requested location.
1311 */
Adam Cohendf035382011-04-11 17:22:04 -07001312 int[] findNearestArea(int pixelX, int pixelY, int spanX, int spanY, View ignoreView,
1313 boolean ignoreOccupied, int[] result) {
Adam Cohend41fbf52012-02-16 23:53:59 -08001314 return findNearestArea(pixelX, pixelY, spanX, spanY,
Adam Cohen482ed822012-03-02 14:15:13 -08001315 spanX, spanY, ignoreView, ignoreOccupied, result, null, mOccupied);
Adam Cohend41fbf52012-02-16 23:53:59 -08001316 }
1317
1318 private final Stack<Rect> mTempRectStack = new Stack<Rect>();
1319 private void lazyInitTempRectStack() {
1320 if (mTempRectStack.isEmpty()) {
1321 for (int i = 0; i < mCountX * mCountY; i++) {
1322 mTempRectStack.push(new Rect());
1323 }
1324 }
1325 }
Adam Cohen482ed822012-03-02 14:15:13 -08001326
Adam Cohend41fbf52012-02-16 23:53:59 -08001327 private void recycleTempRects(Stack<Rect> used) {
1328 while (!used.isEmpty()) {
1329 mTempRectStack.push(used.pop());
1330 }
1331 }
1332
1333 /**
1334 * Find a vacant area that will fit the given bounds nearest the requested
1335 * cell location. Uses Euclidean distance to score multiple vacant areas.
1336 *
1337 * @param pixelX The X location at which you want to search for a vacant area.
1338 * @param pixelY The Y location at which you want to search for a vacant area.
1339 * @param minSpanX The minimum horizontal span required
1340 * @param minSpanY The minimum vertical span required
1341 * @param spanX Horizontal span of the object.
1342 * @param spanY Vertical span of the object.
1343 * @param ignoreOccupied If true, the result can be an occupied cell
1344 * @param result Array in which to place the result, or null (in which case a new array will
1345 * be allocated)
1346 * @return The X, Y cell of a vacant area that can contain this object,
1347 * nearest the requested location.
1348 */
1349 int[] findNearestArea(int pixelX, int pixelY, int minSpanX, int minSpanY, int spanX, int spanY,
Adam Cohen482ed822012-03-02 14:15:13 -08001350 View ignoreView, boolean ignoreOccupied, int[] result, int[] resultSpan,
1351 boolean[][] occupied) {
Adam Cohend41fbf52012-02-16 23:53:59 -08001352 lazyInitTempRectStack();
Michael Jurkac6ee42e2010-09-30 12:04:50 -07001353 // mark space take by ignoreView as available (method checks if ignoreView is null)
Adam Cohen482ed822012-03-02 14:15:13 -08001354 markCellsAsUnoccupiedForView(ignoreView, occupied);
Michael Jurkac6ee42e2010-09-30 12:04:50 -07001355
Adam Cohene3e27a82011-04-15 12:07:39 -07001356 // For items with a spanX / spanY > 1, the passed in point (pixelX, pixelY) corresponds
1357 // to the center of the item, but we are searching based on the top-left cell, so
1358 // we translate the point over to correspond to the top-left.
1359 pixelX -= (mCellWidth + mWidthGap) * (spanX - 1) / 2f;
1360 pixelY -= (mCellHeight + mHeightGap) * (spanY - 1) / 2f;
1361
Jeff Sharkey70864282009-04-07 21:08:40 -07001362 // Keep track of best-scoring drop area
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001363 final int[] bestXY = result != null ? result : new int[2];
Jeff Sharkey70864282009-04-07 21:08:40 -07001364 double bestDistance = Double.MAX_VALUE;
Adam Cohend41fbf52012-02-16 23:53:59 -08001365 final Rect bestRect = new Rect(-1, -1, -1, -1);
1366 final Stack<Rect> validRegions = new Stack<Rect>();
Winson Chungaafa03c2010-06-11 17:34:16 -07001367
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001368 final int countX = mCountX;
1369 final int countY = mCountY;
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001370
Adam Cohend41fbf52012-02-16 23:53:59 -08001371 if (minSpanX <= 0 || minSpanY <= 0 || spanX <= 0 || spanY <= 0 ||
1372 spanX < minSpanX || spanY < minSpanY) {
1373 return bestXY;
1374 }
1375
1376 for (int y = 0; y < countY - (minSpanY - 1); y++) {
Michael Jurkac28de512010-08-13 11:27:44 -07001377 inner:
Adam Cohend41fbf52012-02-16 23:53:59 -08001378 for (int x = 0; x < countX - (minSpanX - 1); x++) {
1379 int ySize = -1;
1380 int xSize = -1;
Adam Cohendf035382011-04-11 17:22:04 -07001381 if (ignoreOccupied) {
Adam Cohend41fbf52012-02-16 23:53:59 -08001382 // First, let's see if this thing fits anywhere
1383 for (int i = 0; i < minSpanX; i++) {
1384 for (int j = 0; j < minSpanY; j++) {
Adam Cohendf035382011-04-11 17:22:04 -07001385 if (occupied[x + i][y + j]) {
Adam Cohendf035382011-04-11 17:22:04 -07001386 continue inner;
1387 }
Michael Jurkac28de512010-08-13 11:27:44 -07001388 }
1389 }
Adam Cohend41fbf52012-02-16 23:53:59 -08001390 xSize = minSpanX;
1391 ySize = minSpanY;
1392
1393 // We know that the item will fit at _some_ acceptable size, now let's see
1394 // how big we can make it. We'll alternate between incrementing x and y spans
1395 // until we hit a limit.
1396 boolean incX = true;
1397 boolean hitMaxX = xSize >= spanX;
1398 boolean hitMaxY = ySize >= spanY;
1399 while (!(hitMaxX && hitMaxY)) {
1400 if (incX && !hitMaxX) {
1401 for (int j = 0; j < ySize; j++) {
1402 if (x + xSize > countX -1 || occupied[x + xSize][y + j]) {
1403 // We can't move out horizontally
1404 hitMaxX = true;
1405 }
1406 }
1407 if (!hitMaxX) {
1408 xSize++;
1409 }
1410 } else if (!hitMaxY) {
1411 for (int i = 0; i < xSize; i++) {
1412 if (y + ySize > countY - 1 || occupied[x + i][y + ySize]) {
1413 // We can't move out vertically
1414 hitMaxY = true;
1415 }
1416 }
1417 if (!hitMaxY) {
1418 ySize++;
1419 }
1420 }
1421 hitMaxX |= xSize >= spanX;
1422 hitMaxY |= ySize >= spanY;
1423 incX = !incX;
1424 }
1425 incX = true;
1426 hitMaxX = xSize >= spanX;
1427 hitMaxY = ySize >= spanY;
Michael Jurkac28de512010-08-13 11:27:44 -07001428 }
Winson Chung0be025d2011-05-23 17:45:09 -07001429 final int[] cellXY = mTmpXY;
Adam Cohene3e27a82011-04-15 12:07:39 -07001430 cellToCenterPoint(x, y, cellXY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001431
Adam Cohend41fbf52012-02-16 23:53:59 -08001432 // We verify that the current rect is not a sub-rect of any of our previous
1433 // candidates. In this case, the current rect is disqualified in favour of the
1434 // containing rect.
1435 Rect currentRect = mTempRectStack.pop();
1436 currentRect.set(x, y, x + xSize, y + ySize);
1437 boolean contained = false;
1438 for (Rect r : validRegions) {
1439 if (r.contains(currentRect)) {
1440 contained = true;
1441 break;
1442 }
1443 }
1444 validRegions.push(currentRect);
Michael Jurkac28de512010-08-13 11:27:44 -07001445 double distance = Math.sqrt(Math.pow(cellXY[0] - pixelX, 2)
1446 + Math.pow(cellXY[1] - pixelY, 2));
Adam Cohen482ed822012-03-02 14:15:13 -08001447
Adam Cohend41fbf52012-02-16 23:53:59 -08001448 if ((distance <= bestDistance && !contained) ||
1449 currentRect.contains(bestRect)) {
Michael Jurkac28de512010-08-13 11:27:44 -07001450 bestDistance = distance;
1451 bestXY[0] = x;
1452 bestXY[1] = y;
Adam Cohend41fbf52012-02-16 23:53:59 -08001453 if (resultSpan != null) {
1454 resultSpan[0] = xSize;
1455 resultSpan[1] = ySize;
1456 }
1457 bestRect.set(currentRect);
Michael Jurkac28de512010-08-13 11:27:44 -07001458 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001459 }
1460 }
Michael Jurkac6ee42e2010-09-30 12:04:50 -07001461 // re-mark space taken by ignoreView as occupied
Adam Cohen482ed822012-03-02 14:15:13 -08001462 markCellsAsOccupiedForView(ignoreView, occupied);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001463
Adam Cohenc0dcf592011-06-01 15:30:43 -07001464 // Return -1, -1 if no suitable location found
1465 if (bestDistance == Double.MAX_VALUE) {
1466 bestXY[0] = -1;
1467 bestXY[1] = -1;
Jeff Sharkey70864282009-04-07 21:08:40 -07001468 }
Adam Cohend41fbf52012-02-16 23:53:59 -08001469 recycleTempRects(validRegions);
Adam Cohenc0dcf592011-06-01 15:30:43 -07001470 return bestXY;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001471 }
Winson Chungaafa03c2010-06-11 17:34:16 -07001472
Adam Cohen482ed822012-03-02 14:15:13 -08001473 /**
1474 * Find a vacant area that will fit the given bounds nearest the requested
1475 * cell location, and will also weigh in a suggested direction vector of the
1476 * desired location. This method computers distance based on unit grid distances,
1477 * not pixel distances.
1478 *
Adam Cohen47a876d2012-03-19 13:21:41 -07001479 * @param cellX The X cell nearest to which you want to search for a vacant area.
1480 * @param cellY The Y cell nearest which you want to search for a vacant area.
Adam Cohen482ed822012-03-02 14:15:13 -08001481 * @param spanX Horizontal span of the object.
1482 * @param spanY Vertical span of the object.
Adam Cohen47a876d2012-03-19 13:21:41 -07001483 * @param direction The favored direction in which the views should move from x, y
1484 * @param exactDirectionOnly If this parameter is true, then only solutions where the direction
1485 * matches exactly. Otherwise we find the best matching direction.
1486 * @param occoupied The array which represents which cells in the CellLayout are occupied
1487 * @param blockOccupied The array which represents which cells in the specified block (cellX,
Winson Chung5f8afe62013-08-12 16:19:28 -07001488 * cellY, spanX, spanY) are occupied. This is used when try to move a group of views.
Adam Cohen482ed822012-03-02 14:15:13 -08001489 * @param result Array in which to place the result, or null (in which case a new array will
1490 * be allocated)
1491 * @return The X, Y cell of a vacant area that can contain this object,
1492 * nearest the requested location.
1493 */
1494 private int[] findNearestArea(int cellX, int cellY, int spanX, int spanY, int[] direction,
Adam Cohen47a876d2012-03-19 13:21:41 -07001495 boolean[][] occupied, boolean blockOccupied[][], int[] result) {
Adam Cohen482ed822012-03-02 14:15:13 -08001496 // Keep track of best-scoring drop area
1497 final int[] bestXY = result != null ? result : new int[2];
1498 float bestDistance = Float.MAX_VALUE;
1499 int bestDirectionScore = Integer.MIN_VALUE;
1500
1501 final int countX = mCountX;
1502 final int countY = mCountY;
1503
1504 for (int y = 0; y < countY - (spanY - 1); y++) {
1505 inner:
1506 for (int x = 0; x < countX - (spanX - 1); x++) {
1507 // First, let's see if this thing fits anywhere
1508 for (int i = 0; i < spanX; i++) {
1509 for (int j = 0; j < spanY; j++) {
Adam Cohen47a876d2012-03-19 13:21:41 -07001510 if (occupied[x + i][y + j] && (blockOccupied == null || blockOccupied[i][j])) {
Adam Cohen482ed822012-03-02 14:15:13 -08001511 continue inner;
1512 }
1513 }
1514 }
1515
1516 float distance = (float)
1517 Math.sqrt((x - cellX) * (x - cellX) + (y - cellY) * (y - cellY));
1518 int[] curDirection = mTmpPoint;
Adam Cohen47a876d2012-03-19 13:21:41 -07001519 computeDirectionVector(x - cellX, y - cellY, curDirection);
1520 // The direction score is just the dot product of the two candidate direction
1521 // and that passed in.
Adam Cohen482ed822012-03-02 14:15:13 -08001522 int curDirectionScore = direction[0] * curDirection[0] +
1523 direction[1] * curDirection[1];
Adam Cohen47a876d2012-03-19 13:21:41 -07001524 boolean exactDirectionOnly = false;
1525 boolean directionMatches = direction[0] == curDirection[0] &&
1526 direction[0] == curDirection[0];
1527 if ((directionMatches || !exactDirectionOnly) &&
1528 Float.compare(distance, bestDistance) < 0 || (Float.compare(distance,
Adam Cohen482ed822012-03-02 14:15:13 -08001529 bestDistance) == 0 && curDirectionScore > bestDirectionScore)) {
1530 bestDistance = distance;
1531 bestDirectionScore = curDirectionScore;
1532 bestXY[0] = x;
1533 bestXY[1] = y;
1534 }
1535 }
1536 }
1537
1538 // Return -1, -1 if no suitable location found
1539 if (bestDistance == Float.MAX_VALUE) {
1540 bestXY[0] = -1;
1541 bestXY[1] = -1;
1542 }
1543 return bestXY;
1544 }
1545
1546 private boolean addViewToTempLocation(View v, Rect rectOccupiedByPotentialDrop,
Adam Cohen8baab352012-03-20 17:39:21 -07001547 int[] direction, ItemConfiguration currentState) {
1548 CellAndSpan c = currentState.map.get(v);
Adam Cohen482ed822012-03-02 14:15:13 -08001549 boolean success = false;
Adam Cohen8baab352012-03-20 17:39:21 -07001550 markCellsForView(c.x, c.y, c.spanX, c.spanY, mTmpOccupied, false);
Adam Cohen482ed822012-03-02 14:15:13 -08001551 markCellsForRect(rectOccupiedByPotentialDrop, mTmpOccupied, true);
1552
Adam Cohen8baab352012-03-20 17:39:21 -07001553 findNearestArea(c.x, c.y, c.spanX, c.spanY, direction, mTmpOccupied, null, mTempLocation);
Adam Cohen482ed822012-03-02 14:15:13 -08001554
1555 if (mTempLocation[0] >= 0 && mTempLocation[1] >= 0) {
Adam Cohen8baab352012-03-20 17:39:21 -07001556 c.x = mTempLocation[0];
1557 c.y = mTempLocation[1];
Adam Cohen482ed822012-03-02 14:15:13 -08001558 success = true;
Adam Cohen482ed822012-03-02 14:15:13 -08001559 }
Adam Cohen8baab352012-03-20 17:39:21 -07001560 markCellsForView(c.x, c.y, c.spanX, c.spanY, mTmpOccupied, true);
Adam Cohen482ed822012-03-02 14:15:13 -08001561 return success;
1562 }
1563
Adam Cohenf3900c22012-11-16 18:28:11 -08001564 /**
1565 * This helper class defines a cluster of views. It helps with defining complex edges
1566 * of the cluster and determining how those edges interact with other views. The edges
1567 * essentially define a fine-grained boundary around the cluster of views -- like a more
1568 * precise version of a bounding box.
1569 */
1570 private class ViewCluster {
1571 final static int LEFT = 0;
1572 final static int TOP = 1;
1573 final static int RIGHT = 2;
1574 final static int BOTTOM = 3;
Adam Cohen47a876d2012-03-19 13:21:41 -07001575
Adam Cohenf3900c22012-11-16 18:28:11 -08001576 ArrayList<View> views;
1577 ItemConfiguration config;
1578 Rect boundingRect = new Rect();
Adam Cohen47a876d2012-03-19 13:21:41 -07001579
Adam Cohenf3900c22012-11-16 18:28:11 -08001580 int[] leftEdge = new int[mCountY];
1581 int[] rightEdge = new int[mCountY];
1582 int[] topEdge = new int[mCountX];
1583 int[] bottomEdge = new int[mCountX];
1584 boolean leftEdgeDirty, rightEdgeDirty, topEdgeDirty, bottomEdgeDirty, boundingRectDirty;
1585
1586 @SuppressWarnings("unchecked")
1587 public ViewCluster(ArrayList<View> views, ItemConfiguration config) {
1588 this.views = (ArrayList<View>) views.clone();
1589 this.config = config;
1590 resetEdges();
Adam Cohen47a876d2012-03-19 13:21:41 -07001591 }
1592
Adam Cohenf3900c22012-11-16 18:28:11 -08001593 void resetEdges() {
1594 for (int i = 0; i < mCountX; i++) {
1595 topEdge[i] = -1;
1596 bottomEdge[i] = -1;
1597 }
1598 for (int i = 0; i < mCountY; i++) {
1599 leftEdge[i] = -1;
1600 rightEdge[i] = -1;
1601 }
1602 leftEdgeDirty = true;
1603 rightEdgeDirty = true;
1604 bottomEdgeDirty = true;
1605 topEdgeDirty = true;
1606 boundingRectDirty = true;
1607 }
Adam Cohen47a876d2012-03-19 13:21:41 -07001608
Adam Cohenf3900c22012-11-16 18:28:11 -08001609 void computeEdge(int which, int[] edge) {
1610 int count = views.size();
1611 for (int i = 0; i < count; i++) {
1612 CellAndSpan cs = config.map.get(views.get(i));
1613 switch (which) {
1614 case LEFT:
1615 int left = cs.x;
1616 for (int j = cs.y; j < cs.y + cs.spanY; j++) {
1617 if (left < edge[j] || edge[j] < 0) {
1618 edge[j] = left;
Adam Cohena56dc102012-07-13 13:41:42 -07001619 }
Adam Cohen47a876d2012-03-19 13:21:41 -07001620 }
Adam Cohenf3900c22012-11-16 18:28:11 -08001621 break;
1622 case RIGHT:
1623 int right = cs.x + cs.spanX;
1624 for (int j = cs.y; j < cs.y + cs.spanY; j++) {
1625 if (right > edge[j]) {
1626 edge[j] = right;
1627 }
1628 }
1629 break;
1630 case TOP:
1631 int top = cs.y;
1632 for (int j = cs.x; j < cs.x + cs.spanX; j++) {
1633 if (top < edge[j] || edge[j] < 0) {
1634 edge[j] = top;
1635 }
1636 }
1637 break;
1638 case BOTTOM:
1639 int bottom = cs.y + cs.spanY;
1640 for (int j = cs.x; j < cs.x + cs.spanX; j++) {
1641 if (bottom > edge[j]) {
1642 edge[j] = bottom;
1643 }
1644 }
1645 break;
Adam Cohen47a876d2012-03-19 13:21:41 -07001646 }
1647 }
1648 }
Adam Cohenf3900c22012-11-16 18:28:11 -08001649
1650 boolean isViewTouchingEdge(View v, int whichEdge) {
1651 CellAndSpan cs = config.map.get(v);
1652
1653 int[] edge = getEdge(whichEdge);
1654
1655 switch (whichEdge) {
1656 case LEFT:
1657 for (int i = cs.y; i < cs.y + cs.spanY; i++) {
1658 if (edge[i] == cs.x + cs.spanX) {
1659 return true;
1660 }
1661 }
1662 break;
1663 case RIGHT:
1664 for (int i = cs.y; i < cs.y + cs.spanY; i++) {
1665 if (edge[i] == cs.x) {
1666 return true;
1667 }
1668 }
1669 break;
1670 case TOP:
1671 for (int i = cs.x; i < cs.x + cs.spanX; i++) {
1672 if (edge[i] == cs.y + cs.spanY) {
1673 return true;
1674 }
1675 }
1676 break;
1677 case BOTTOM:
1678 for (int i = cs.x; i < cs.x + cs.spanX; i++) {
1679 if (edge[i] == cs.y) {
1680 return true;
1681 }
1682 }
1683 break;
1684 }
1685 return false;
1686 }
1687
1688 void shift(int whichEdge, int delta) {
1689 for (View v: views) {
1690 CellAndSpan c = config.map.get(v);
1691 switch (whichEdge) {
1692 case LEFT:
1693 c.x -= delta;
1694 break;
1695 case RIGHT:
1696 c.x += delta;
1697 break;
1698 case TOP:
1699 c.y -= delta;
1700 break;
1701 case BOTTOM:
1702 default:
1703 c.y += delta;
1704 break;
1705 }
1706 }
1707 resetEdges();
1708 }
1709
1710 public void addView(View v) {
1711 views.add(v);
1712 resetEdges();
1713 }
1714
1715 public Rect getBoundingRect() {
1716 if (boundingRectDirty) {
1717 boolean first = true;
1718 for (View v: views) {
1719 CellAndSpan c = config.map.get(v);
1720 if (first) {
1721 boundingRect.set(c.x, c.y, c.x + c.spanX, c.y + c.spanY);
1722 first = false;
1723 } else {
1724 boundingRect.union(c.x, c.y, c.x + c.spanX, c.y + c.spanY);
1725 }
1726 }
1727 }
1728 return boundingRect;
1729 }
1730
1731 public int[] getEdge(int which) {
1732 switch (which) {
1733 case LEFT:
1734 return getLeftEdge();
1735 case RIGHT:
1736 return getRightEdge();
1737 case TOP:
1738 return getTopEdge();
1739 case BOTTOM:
1740 default:
1741 return getBottomEdge();
1742 }
1743 }
1744
1745 public int[] getLeftEdge() {
1746 if (leftEdgeDirty) {
1747 computeEdge(LEFT, leftEdge);
1748 }
1749 return leftEdge;
1750 }
1751
1752 public int[] getRightEdge() {
1753 if (rightEdgeDirty) {
1754 computeEdge(RIGHT, rightEdge);
1755 }
1756 return rightEdge;
1757 }
1758
1759 public int[] getTopEdge() {
1760 if (topEdgeDirty) {
1761 computeEdge(TOP, topEdge);
1762 }
1763 return topEdge;
1764 }
1765
1766 public int[] getBottomEdge() {
1767 if (bottomEdgeDirty) {
1768 computeEdge(BOTTOM, bottomEdge);
1769 }
1770 return bottomEdge;
1771 }
1772
1773 PositionComparator comparator = new PositionComparator();
1774 class PositionComparator implements Comparator<View> {
1775 int whichEdge = 0;
1776 public int compare(View left, View right) {
1777 CellAndSpan l = config.map.get(left);
1778 CellAndSpan r = config.map.get(right);
1779 switch (whichEdge) {
1780 case LEFT:
1781 return (r.x + r.spanX) - (l.x + l.spanX);
1782 case RIGHT:
1783 return l.x - r.x;
1784 case TOP:
1785 return (r.y + r.spanY) - (l.y + l.spanY);
1786 case BOTTOM:
1787 default:
1788 return l.y - r.y;
1789 }
1790 }
1791 }
1792
1793 public void sortConfigurationForEdgePush(int edge) {
1794 comparator.whichEdge = edge;
1795 Collections.sort(config.sortedViews, comparator);
1796 }
Adam Cohen47a876d2012-03-19 13:21:41 -07001797 }
1798
Adam Cohenf3900c22012-11-16 18:28:11 -08001799 private boolean pushViewsToTempLocation(ArrayList<View> views, Rect rectOccupiedByPotentialDrop,
1800 int[] direction, View dragView, ItemConfiguration currentState) {
Adam Cohene0489502012-08-27 15:18:53 -07001801
Adam Cohenf3900c22012-11-16 18:28:11 -08001802 ViewCluster cluster = new ViewCluster(views, currentState);
1803 Rect clusterRect = cluster.getBoundingRect();
1804 int whichEdge;
1805 int pushDistance;
1806 boolean fail = false;
1807
1808 // Determine the edge of the cluster that will be leading the push and how far
1809 // the cluster must be shifted.
1810 if (direction[0] < 0) {
1811 whichEdge = ViewCluster.LEFT;
1812 pushDistance = clusterRect.right - rectOccupiedByPotentialDrop.left;
Adam Cohene0489502012-08-27 15:18:53 -07001813 } else if (direction[0] > 0) {
Adam Cohenf3900c22012-11-16 18:28:11 -08001814 whichEdge = ViewCluster.RIGHT;
1815 pushDistance = rectOccupiedByPotentialDrop.right - clusterRect.left;
1816 } else if (direction[1] < 0) {
1817 whichEdge = ViewCluster.TOP;
1818 pushDistance = clusterRect.bottom - rectOccupiedByPotentialDrop.top;
1819 } else {
1820 whichEdge = ViewCluster.BOTTOM;
1821 pushDistance = rectOccupiedByPotentialDrop.bottom - clusterRect.top;
Adam Cohene0489502012-08-27 15:18:53 -07001822 }
1823
Adam Cohenf3900c22012-11-16 18:28:11 -08001824 // Break early for invalid push distance.
1825 if (pushDistance <= 0) {
1826 return false;
Adam Cohene0489502012-08-27 15:18:53 -07001827 }
Adam Cohenf3900c22012-11-16 18:28:11 -08001828
1829 // Mark the occupied state as false for the group of views we want to move.
1830 for (View v: views) {
1831 CellAndSpan c = currentState.map.get(v);
1832 markCellsForView(c.x, c.y, c.spanX, c.spanY, mTmpOccupied, false);
1833 }
1834
1835 // We save the current configuration -- if we fail to find a solution we will revert
1836 // to the initial state. The process of finding a solution modifies the configuration
1837 // in place, hence the need for revert in the failure case.
1838 currentState.save();
1839
1840 // The pushing algorithm is simplified by considering the views in the order in which
1841 // they would be pushed by the cluster. For example, if the cluster is leading with its
1842 // left edge, we consider sort the views by their right edge, from right to left.
1843 cluster.sortConfigurationForEdgePush(whichEdge);
1844
1845 while (pushDistance > 0 && !fail) {
1846 for (View v: currentState.sortedViews) {
1847 // For each view that isn't in the cluster, we see if the leading edge of the
1848 // cluster is contacting the edge of that view. If so, we add that view to the
1849 // cluster.
1850 if (!cluster.views.contains(v) && v != dragView) {
1851 if (cluster.isViewTouchingEdge(v, whichEdge)) {
1852 LayoutParams lp = (LayoutParams) v.getLayoutParams();
1853 if (!lp.canReorder) {
1854 // The push solution includes the all apps button, this is not viable.
1855 fail = true;
1856 break;
1857 }
1858 cluster.addView(v);
1859 CellAndSpan c = currentState.map.get(v);
1860
1861 // Adding view to cluster, mark it as not occupied.
1862 markCellsForView(c.x, c.y, c.spanX, c.spanY, mTmpOccupied, false);
1863 }
1864 }
1865 }
1866 pushDistance--;
1867
1868 // The cluster has been completed, now we move the whole thing over in the appropriate
1869 // direction.
1870 cluster.shift(whichEdge, 1);
1871 }
1872
1873 boolean foundSolution = false;
1874 clusterRect = cluster.getBoundingRect();
1875
1876 // Due to the nature of the algorithm, the only check required to verify a valid solution
1877 // is to ensure that completed shifted cluster lies completely within the cell layout.
1878 if (!fail && clusterRect.left >= 0 && clusterRect.right <= mCountX && clusterRect.top >= 0 &&
1879 clusterRect.bottom <= mCountY) {
1880 foundSolution = true;
1881 } else {
1882 currentState.restore();
1883 }
1884
1885 // In either case, we set the occupied array as marked for the location of the views
1886 for (View v: cluster.views) {
1887 CellAndSpan c = currentState.map.get(v);
1888 markCellsForView(c.x, c.y, c.spanX, c.spanY, mTmpOccupied, true);
1889 }
1890
1891 return foundSolution;
Adam Cohene0489502012-08-27 15:18:53 -07001892 }
1893
Adam Cohen482ed822012-03-02 14:15:13 -08001894 private boolean addViewsToTempLocation(ArrayList<View> views, Rect rectOccupiedByPotentialDrop,
Adam Cohenf3900c22012-11-16 18:28:11 -08001895 int[] direction, View dragView, ItemConfiguration currentState) {
Adam Cohen482ed822012-03-02 14:15:13 -08001896 if (views.size() == 0) return true;
Adam Cohen482ed822012-03-02 14:15:13 -08001897
Adam Cohen8baab352012-03-20 17:39:21 -07001898 boolean success = false;
Adam Cohen482ed822012-03-02 14:15:13 -08001899 Rect boundingRect = null;
Adam Cohen8baab352012-03-20 17:39:21 -07001900 // We construct a rect which represents the entire group of views passed in
Adam Cohen482ed822012-03-02 14:15:13 -08001901 for (View v: views) {
Adam Cohen8baab352012-03-20 17:39:21 -07001902 CellAndSpan c = currentState.map.get(v);
Adam Cohen482ed822012-03-02 14:15:13 -08001903 if (boundingRect == null) {
Adam Cohen8baab352012-03-20 17:39:21 -07001904 boundingRect = new Rect(c.x, c.y, c.x + c.spanX, c.y + c.spanY);
Adam Cohen482ed822012-03-02 14:15:13 -08001905 } else {
Adam Cohen8baab352012-03-20 17:39:21 -07001906 boundingRect.union(c.x, c.y, c.x + c.spanX, c.y + c.spanY);
Adam Cohen482ed822012-03-02 14:15:13 -08001907 }
1908 }
Adam Cohen8baab352012-03-20 17:39:21 -07001909
Adam Cohen8baab352012-03-20 17:39:21 -07001910 // Mark the occupied state as false for the group of views we want to move.
Adam Cohenf3900c22012-11-16 18:28:11 -08001911 for (View v: views) {
Adam Cohen8baab352012-03-20 17:39:21 -07001912 CellAndSpan c = currentState.map.get(v);
1913 markCellsForView(c.x, c.y, c.spanX, c.spanY, mTmpOccupied, false);
1914 }
1915
Adam Cohen47a876d2012-03-19 13:21:41 -07001916 boolean[][] blockOccupied = new boolean[boundingRect.width()][boundingRect.height()];
1917 int top = boundingRect.top;
1918 int left = boundingRect.left;
Adam Cohen8baab352012-03-20 17:39:21 -07001919 // We mark more precisely which parts of the bounding rect are truly occupied, allowing
Adam Cohena56dc102012-07-13 13:41:42 -07001920 // for interlocking.
Adam Cohenf3900c22012-11-16 18:28:11 -08001921 for (View v: views) {
Adam Cohen8baab352012-03-20 17:39:21 -07001922 CellAndSpan c = currentState.map.get(v);
1923 markCellsForView(c.x - left, c.y - top, c.spanX, c.spanY, blockOccupied, true);
Adam Cohen47a876d2012-03-19 13:21:41 -07001924 }
1925
Adam Cohen482ed822012-03-02 14:15:13 -08001926 markCellsForRect(rectOccupiedByPotentialDrop, mTmpOccupied, true);
1927
Adam Cohenf3900c22012-11-16 18:28:11 -08001928 findNearestArea(boundingRect.left, boundingRect.top, boundingRect.width(),
1929 boundingRect.height(), direction, mTmpOccupied, blockOccupied, mTempLocation);
Adam Cohen482ed822012-03-02 14:15:13 -08001930
Adam Cohen8baab352012-03-20 17:39:21 -07001931 // If we successfuly found a location by pushing the block of views, we commit it
Adam Cohen482ed822012-03-02 14:15:13 -08001932 if (mTempLocation[0] >= 0 && mTempLocation[1] >= 0) {
Adam Cohen8baab352012-03-20 17:39:21 -07001933 int deltaX = mTempLocation[0] - boundingRect.left;
1934 int deltaY = mTempLocation[1] - boundingRect.top;
Adam Cohenf3900c22012-11-16 18:28:11 -08001935 for (View v: views) {
Adam Cohen8baab352012-03-20 17:39:21 -07001936 CellAndSpan c = currentState.map.get(v);
1937 c.x += deltaX;
1938 c.y += deltaY;
Adam Cohen482ed822012-03-02 14:15:13 -08001939 }
1940 success = true;
1941 }
Adam Cohen8baab352012-03-20 17:39:21 -07001942
1943 // In either case, we set the occupied array as marked for the location of the views
Adam Cohenf3900c22012-11-16 18:28:11 -08001944 for (View v: views) {
Adam Cohen8baab352012-03-20 17:39:21 -07001945 CellAndSpan c = currentState.map.get(v);
1946 markCellsForView(c.x, c.y, c.spanX, c.spanY, mTmpOccupied, true);
Adam Cohen482ed822012-03-02 14:15:13 -08001947 }
1948 return success;
1949 }
1950
1951 private void markCellsForRect(Rect r, boolean[][] occupied, boolean value) {
1952 markCellsForView(r.left, r.top, r.width(), r.height(), occupied, value);
1953 }
1954
Adam Cohen4abc5bd2012-05-29 21:06:03 -07001955 // This method tries to find a reordering solution which satisfies the push mechanic by trying
1956 // to push items in each of the cardinal directions, in an order based on the direction vector
1957 // passed.
1958 private boolean attemptPushInDirection(ArrayList<View> intersectingViews, Rect occupied,
1959 int[] direction, View ignoreView, ItemConfiguration solution) {
1960 if ((Math.abs(direction[0]) + Math.abs(direction[1])) > 1) {
Winson Chung5f8afe62013-08-12 16:19:28 -07001961 // If the direction vector has two non-zero components, we try pushing
Adam Cohen4abc5bd2012-05-29 21:06:03 -07001962 // separately in each of the components.
1963 int temp = direction[1];
1964 direction[1] = 0;
Adam Cohenf3900c22012-11-16 18:28:11 -08001965
1966 if (pushViewsToTempLocation(intersectingViews, occupied, direction,
Adam Cohen4abc5bd2012-05-29 21:06:03 -07001967 ignoreView, solution)) {
1968 return true;
1969 }
1970 direction[1] = temp;
1971 temp = direction[0];
1972 direction[0] = 0;
Adam Cohenf3900c22012-11-16 18:28:11 -08001973
1974 if (pushViewsToTempLocation(intersectingViews, occupied, direction,
Adam Cohen4abc5bd2012-05-29 21:06:03 -07001975 ignoreView, solution)) {
1976 return true;
1977 }
1978 // Revert the direction
1979 direction[0] = temp;
1980
1981 // Now we try pushing in each component of the opposite direction
1982 direction[0] *= -1;
1983 direction[1] *= -1;
1984 temp = direction[1];
1985 direction[1] = 0;
Adam Cohenf3900c22012-11-16 18:28:11 -08001986 if (pushViewsToTempLocation(intersectingViews, occupied, direction,
Adam Cohen4abc5bd2012-05-29 21:06:03 -07001987 ignoreView, solution)) {
1988 return true;
1989 }
1990
1991 direction[1] = temp;
1992 temp = direction[0];
1993 direction[0] = 0;
Adam Cohenf3900c22012-11-16 18:28:11 -08001994 if (pushViewsToTempLocation(intersectingViews, occupied, direction,
Adam Cohen4abc5bd2012-05-29 21:06:03 -07001995 ignoreView, solution)) {
1996 return true;
1997 }
1998 // revert the direction
1999 direction[0] = temp;
2000 direction[0] *= -1;
2001 direction[1] *= -1;
Winson Chung5f8afe62013-08-12 16:19:28 -07002002
Adam Cohen4abc5bd2012-05-29 21:06:03 -07002003 } else {
2004 // If the direction vector has a single non-zero component, we push first in the
2005 // direction of the vector
Adam Cohenf3900c22012-11-16 18:28:11 -08002006 if (pushViewsToTempLocation(intersectingViews, occupied, direction,
Adam Cohen4abc5bd2012-05-29 21:06:03 -07002007 ignoreView, solution)) {
2008 return true;
2009 }
Adam Cohen4abc5bd2012-05-29 21:06:03 -07002010 // Then we try the opposite direction
2011 direction[0] *= -1;
2012 direction[1] *= -1;
Adam Cohenf3900c22012-11-16 18:28:11 -08002013 if (pushViewsToTempLocation(intersectingViews, occupied, direction,
Adam Cohen4abc5bd2012-05-29 21:06:03 -07002014 ignoreView, solution)) {
2015 return true;
2016 }
2017 // Switch the direction back
2018 direction[0] *= -1;
2019 direction[1] *= -1;
Winson Chung5f8afe62013-08-12 16:19:28 -07002020
2021 // If we have failed to find a push solution with the above, then we try
Adam Cohen4abc5bd2012-05-29 21:06:03 -07002022 // to find a solution by pushing along the perpendicular axis.
2023
2024 // Swap the components
2025 int temp = direction[1];
2026 direction[1] = direction[0];
2027 direction[0] = temp;
Adam Cohenf3900c22012-11-16 18:28:11 -08002028 if (pushViewsToTempLocation(intersectingViews, occupied, direction,
Adam Cohen4abc5bd2012-05-29 21:06:03 -07002029 ignoreView, solution)) {
2030 return true;
2031 }
2032
2033 // Then we try the opposite direction
2034 direction[0] *= -1;
2035 direction[1] *= -1;
Adam Cohenf3900c22012-11-16 18:28:11 -08002036 if (pushViewsToTempLocation(intersectingViews, occupied, direction,
Adam Cohen4abc5bd2012-05-29 21:06:03 -07002037 ignoreView, solution)) {
2038 return true;
2039 }
2040 // Switch the direction back
2041 direction[0] *= -1;
2042 direction[1] *= -1;
2043
2044 // Swap the components back
2045 temp = direction[1];
2046 direction[1] = direction[0];
2047 direction[0] = temp;
2048 }
2049 return false;
2050 }
2051
Adam Cohen482ed822012-03-02 14:15:13 -08002052 private boolean rearrangementExists(int cellX, int cellY, int spanX, int spanY, int[] direction,
Adam Cohen8baab352012-03-20 17:39:21 -07002053 View ignoreView, ItemConfiguration solution) {
Winson Chunge3e03bc2012-05-01 15:10:11 -07002054 // Return early if get invalid cell positions
2055 if (cellX < 0 || cellY < 0) return false;
Adam Cohen482ed822012-03-02 14:15:13 -08002056
Adam Cohen8baab352012-03-20 17:39:21 -07002057 mIntersectingViews.clear();
Adam Cohen482ed822012-03-02 14:15:13 -08002058 mOccupiedRect.set(cellX, cellY, cellX + spanX, cellY + spanY);
Adam Cohen482ed822012-03-02 14:15:13 -08002059
Adam Cohen8baab352012-03-20 17:39:21 -07002060 // Mark the desired location of the view currently being dragged.
Adam Cohen482ed822012-03-02 14:15:13 -08002061 if (ignoreView != null) {
Adam Cohen8baab352012-03-20 17:39:21 -07002062 CellAndSpan c = solution.map.get(ignoreView);
Adam Cohen19f37922012-03-21 11:59:11 -07002063 if (c != null) {
2064 c.x = cellX;
2065 c.y = cellY;
2066 }
Adam Cohen482ed822012-03-02 14:15:13 -08002067 }
Adam Cohen482ed822012-03-02 14:15:13 -08002068 Rect r0 = new Rect(cellX, cellY, cellX + spanX, cellY + spanY);
2069 Rect r1 = new Rect();
Adam Cohen8baab352012-03-20 17:39:21 -07002070 for (View child: solution.map.keySet()) {
Adam Cohen482ed822012-03-02 14:15:13 -08002071 if (child == ignoreView) continue;
Adam Cohen8baab352012-03-20 17:39:21 -07002072 CellAndSpan c = solution.map.get(child);
Adam Cohen482ed822012-03-02 14:15:13 -08002073 LayoutParams lp = (LayoutParams) child.getLayoutParams();
Adam Cohen8baab352012-03-20 17:39:21 -07002074 r1.set(c.x, c.y, c.x + c.spanX, c.y + c.spanY);
Adam Cohen482ed822012-03-02 14:15:13 -08002075 if (Rect.intersects(r0, r1)) {
2076 if (!lp.canReorder) {
2077 return false;
2078 }
2079 mIntersectingViews.add(child);
2080 }
2081 }
Adam Cohen47a876d2012-03-19 13:21:41 -07002082
Winson Chung5f8afe62013-08-12 16:19:28 -07002083 // First we try to find a solution which respects the push mechanic. That is,
Adam Cohen4abc5bd2012-05-29 21:06:03 -07002084 // we try to find a solution such that no displaced item travels through another item
2085 // without also displacing that item.
2086 if (attemptPushInDirection(mIntersectingViews, mOccupiedRect, direction, ignoreView,
Adam Cohen19f37922012-03-21 11:59:11 -07002087 solution)) {
Adam Cohen47a876d2012-03-19 13:21:41 -07002088 return true;
2089 }
Adam Cohen47a876d2012-03-19 13:21:41 -07002090
Adam Cohen4abc5bd2012-05-29 21:06:03 -07002091 // Next we try moving the views as a block, but without requiring the push mechanic.
Adam Cohenf3900c22012-11-16 18:28:11 -08002092 if (addViewsToTempLocation(mIntersectingViews, mOccupiedRect, direction, ignoreView,
Adam Cohen19f37922012-03-21 11:59:11 -07002093 solution)) {
Adam Cohen482ed822012-03-02 14:15:13 -08002094 return true;
2095 }
Adam Cohen47a876d2012-03-19 13:21:41 -07002096
Adam Cohen482ed822012-03-02 14:15:13 -08002097 // Ok, they couldn't move as a block, let's move them individually
2098 for (View v : mIntersectingViews) {
Adam Cohen8baab352012-03-20 17:39:21 -07002099 if (!addViewToTempLocation(v, mOccupiedRect, direction, solution)) {
Adam Cohen482ed822012-03-02 14:15:13 -08002100 return false;
2101 }
2102 }
2103 return true;
2104 }
2105
2106 /*
2107 * Returns a pair (x, y), where x,y are in {-1, 0, 1} corresponding to vector between
2108 * the provided point and the provided cell
2109 */
Adam Cohen47a876d2012-03-19 13:21:41 -07002110 private void computeDirectionVector(float deltaX, float deltaY, int[] result) {
Adam Cohen482ed822012-03-02 14:15:13 -08002111 double angle = Math.atan(((float) deltaY) / deltaX);
2112
2113 result[0] = 0;
2114 result[1] = 0;
2115 if (Math.abs(Math.cos(angle)) > 0.5f) {
2116 result[0] = (int) Math.signum(deltaX);
2117 }
2118 if (Math.abs(Math.sin(angle)) > 0.5f) {
2119 result[1] = (int) Math.signum(deltaY);
2120 }
2121 }
2122
Adam Cohen8baab352012-03-20 17:39:21 -07002123 private void copyOccupiedArray(boolean[][] occupied) {
2124 for (int i = 0; i < mCountX; i++) {
2125 for (int j = 0; j < mCountY; j++) {
2126 occupied[i][j] = mOccupied[i][j];
2127 }
2128 }
2129 }
2130
Adam Cohen482ed822012-03-02 14:15:13 -08002131 ItemConfiguration simpleSwap(int pixelX, int pixelY, int minSpanX, int minSpanY, int spanX,
2132 int spanY, int[] direction, View dragView, boolean decX, ItemConfiguration solution) {
Adam Cohen8baab352012-03-20 17:39:21 -07002133 // Copy the current state into the solution. This solution will be manipulated as necessary.
2134 copyCurrentStateToSolution(solution, false);
2135 // Copy the current occupied array into the temporary occupied array. This array will be
2136 // manipulated as necessary to find a solution.
2137 copyOccupiedArray(mTmpOccupied);
Adam Cohen482ed822012-03-02 14:15:13 -08002138
2139 // We find the nearest cell into which we would place the dragged item, assuming there's
2140 // nothing in its way.
2141 int result[] = new int[2];
2142 result = findNearestArea(pixelX, pixelY, spanX, spanY, result);
2143
2144 boolean success = false;
2145 // First we try the exact nearest position of the item being dragged,
2146 // we will then want to try to move this around to other neighbouring positions
Adam Cohen8baab352012-03-20 17:39:21 -07002147 success = rearrangementExists(result[0], result[1], spanX, spanY, direction, dragView,
2148 solution);
Adam Cohen482ed822012-03-02 14:15:13 -08002149
2150 if (!success) {
2151 // We try shrinking the widget down to size in an alternating pattern, shrink 1 in
2152 // x, then 1 in y etc.
2153 if (spanX > minSpanX && (minSpanY == spanY || decX)) {
2154 return simpleSwap(pixelX, pixelY, minSpanX, minSpanY, spanX - 1, spanY, direction,
2155 dragView, false, solution);
2156 } else if (spanY > minSpanY) {
2157 return simpleSwap(pixelX, pixelY, minSpanX, minSpanY, spanX, spanY - 1, direction,
2158 dragView, true, solution);
2159 }
2160 solution.isSolution = false;
2161 } else {
2162 solution.isSolution = true;
2163 solution.dragViewX = result[0];
2164 solution.dragViewY = result[1];
2165 solution.dragViewSpanX = spanX;
2166 solution.dragViewSpanY = spanY;
Adam Cohen482ed822012-03-02 14:15:13 -08002167 }
2168 return solution;
2169 }
2170
2171 private void copyCurrentStateToSolution(ItemConfiguration solution, boolean temp) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07002172 int childCount = mShortcutsAndWidgets.getChildCount();
Adam Cohen482ed822012-03-02 14:15:13 -08002173 for (int i = 0; i < childCount; i++) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07002174 View child = mShortcutsAndWidgets.getChildAt(i);
Adam Cohen482ed822012-03-02 14:15:13 -08002175 LayoutParams lp = (LayoutParams) child.getLayoutParams();
Adam Cohen8baab352012-03-20 17:39:21 -07002176 CellAndSpan c;
Adam Cohen482ed822012-03-02 14:15:13 -08002177 if (temp) {
Adam Cohen8baab352012-03-20 17:39:21 -07002178 c = new CellAndSpan(lp.tmpCellX, lp.tmpCellY, lp.cellHSpan, lp.cellVSpan);
Adam Cohen482ed822012-03-02 14:15:13 -08002179 } else {
Adam Cohen8baab352012-03-20 17:39:21 -07002180 c = new CellAndSpan(lp.cellX, lp.cellY, lp.cellHSpan, lp.cellVSpan);
Adam Cohen482ed822012-03-02 14:15:13 -08002181 }
Adam Cohenf3900c22012-11-16 18:28:11 -08002182 solution.add(child, c);
Adam Cohen482ed822012-03-02 14:15:13 -08002183 }
2184 }
2185
2186 private void copySolutionToTempState(ItemConfiguration solution, View dragView) {
2187 for (int i = 0; i < mCountX; i++) {
2188 for (int j = 0; j < mCountY; j++) {
2189 mTmpOccupied[i][j] = false;
2190 }
2191 }
2192
Michael Jurkaa52570f2012-03-20 03:18:20 -07002193 int childCount = mShortcutsAndWidgets.getChildCount();
Adam Cohen482ed822012-03-02 14:15:13 -08002194 for (int i = 0; i < childCount; i++) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07002195 View child = mShortcutsAndWidgets.getChildAt(i);
Adam Cohen482ed822012-03-02 14:15:13 -08002196 if (child == dragView) continue;
2197 LayoutParams lp = (LayoutParams) child.getLayoutParams();
Adam Cohen8baab352012-03-20 17:39:21 -07002198 CellAndSpan c = solution.map.get(child);
2199 if (c != null) {
2200 lp.tmpCellX = c.x;
2201 lp.tmpCellY = c.y;
2202 lp.cellHSpan = c.spanX;
2203 lp.cellVSpan = c.spanY;
2204 markCellsForView(c.x, c.y, c.spanX, c.spanY, mTmpOccupied, true);
Adam Cohen482ed822012-03-02 14:15:13 -08002205 }
2206 }
2207 markCellsForView(solution.dragViewX, solution.dragViewY, solution.dragViewSpanX,
2208 solution.dragViewSpanY, mTmpOccupied, true);
2209 }
2210
2211 private void animateItemsToSolution(ItemConfiguration solution, View dragView, boolean
2212 commitDragView) {
2213
2214 boolean[][] occupied = DESTRUCTIVE_REORDER ? mOccupied : mTmpOccupied;
2215 for (int i = 0; i < mCountX; i++) {
2216 for (int j = 0; j < mCountY; j++) {
2217 occupied[i][j] = false;
2218 }
2219 }
2220
Michael Jurkaa52570f2012-03-20 03:18:20 -07002221 int childCount = mShortcutsAndWidgets.getChildCount();
Adam Cohen482ed822012-03-02 14:15:13 -08002222 for (int i = 0; i < childCount; i++) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07002223 View child = mShortcutsAndWidgets.getChildAt(i);
Adam Cohen482ed822012-03-02 14:15:13 -08002224 if (child == dragView) continue;
Adam Cohen8baab352012-03-20 17:39:21 -07002225 CellAndSpan c = solution.map.get(child);
2226 if (c != null) {
Adam Cohen19f37922012-03-21 11:59:11 -07002227 animateChildToPosition(child, c.x, c.y, REORDER_ANIMATION_DURATION, 0,
2228 DESTRUCTIVE_REORDER, false);
Adam Cohen8baab352012-03-20 17:39:21 -07002229 markCellsForView(c.x, c.y, c.spanX, c.spanY, occupied, true);
Adam Cohen482ed822012-03-02 14:15:13 -08002230 }
2231 }
2232 if (commitDragView) {
2233 markCellsForView(solution.dragViewX, solution.dragViewY, solution.dragViewSpanX,
2234 solution.dragViewSpanY, occupied, true);
2235 }
2236 }
2237
Adam Cohen19f37922012-03-21 11:59:11 -07002238 // This method starts or changes the reorder hint animations
2239 private void beginOrAdjustHintAnimations(ItemConfiguration solution, View dragView, int delay) {
2240 int childCount = mShortcutsAndWidgets.getChildCount();
Adam Cohen19f37922012-03-21 11:59:11 -07002241 for (int i = 0; i < childCount; i++) {
2242 View child = mShortcutsAndWidgets.getChildAt(i);
2243 if (child == dragView) continue;
2244 CellAndSpan c = solution.map.get(child);
2245 LayoutParams lp = (LayoutParams) child.getLayoutParams();
2246 if (c != null) {
2247 ReorderHintAnimation rha = new ReorderHintAnimation(child, lp.cellX, lp.cellY,
2248 c.x, c.y, c.spanX, c.spanY);
Adam Cohend024f982012-05-23 18:26:45 -07002249 rha.animate();
Adam Cohen19f37922012-03-21 11:59:11 -07002250 }
2251 }
2252 }
2253
2254 // Class which represents the reorder hint animations. These animations show that an item is
2255 // in a temporary state, and hint at where the item will return to.
2256 class ReorderHintAnimation {
2257 View child;
Adam Cohend024f982012-05-23 18:26:45 -07002258 float finalDeltaX;
2259 float finalDeltaY;
2260 float initDeltaX;
2261 float initDeltaY;
2262 float finalScale;
2263 float initScale;
Brandon Keely50e6e562012-05-08 16:28:49 -07002264 private static final int DURATION = 300;
Adam Cohene7587d22012-05-24 18:50:02 -07002265 Animator a;
Adam Cohen19f37922012-03-21 11:59:11 -07002266
2267 public ReorderHintAnimation(View child, int cellX0, int cellY0, int cellX1, int cellY1,
2268 int spanX, int spanY) {
2269 regionToCenterPoint(cellX0, cellY0, spanX, spanY, mTmpPoint);
2270 final int x0 = mTmpPoint[0];
2271 final int y0 = mTmpPoint[1];
2272 regionToCenterPoint(cellX1, cellY1, spanX, spanY, mTmpPoint);
2273 final int x1 = mTmpPoint[0];
2274 final int y1 = mTmpPoint[1];
2275 final int dX = x1 - x0;
2276 final int dY = y1 - y0;
Adam Cohend024f982012-05-23 18:26:45 -07002277 finalDeltaX = 0;
2278 finalDeltaY = 0;
Adam Cohen19f37922012-03-21 11:59:11 -07002279 if (dX == dY && dX == 0) {
2280 } else {
2281 if (dY == 0) {
Adam Cohend024f982012-05-23 18:26:45 -07002282 finalDeltaX = - Math.signum(dX) * mReorderHintAnimationMagnitude;
Adam Cohen19f37922012-03-21 11:59:11 -07002283 } else if (dX == 0) {
Adam Cohend024f982012-05-23 18:26:45 -07002284 finalDeltaY = - Math.signum(dY) * mReorderHintAnimationMagnitude;
Adam Cohen19f37922012-03-21 11:59:11 -07002285 } else {
2286 double angle = Math.atan( (float) (dY) / dX);
Adam Cohend024f982012-05-23 18:26:45 -07002287 finalDeltaX = (int) (- Math.signum(dX) *
Adam Cohenfe41ac62012-05-23 14:00:37 -07002288 Math.abs(Math.cos(angle) * mReorderHintAnimationMagnitude));
Adam Cohend024f982012-05-23 18:26:45 -07002289 finalDeltaY = (int) (- Math.signum(dY) *
Adam Cohenfe41ac62012-05-23 14:00:37 -07002290 Math.abs(Math.sin(angle) * mReorderHintAnimationMagnitude));
Adam Cohen19f37922012-03-21 11:59:11 -07002291 }
2292 }
Adam Cohend024f982012-05-23 18:26:45 -07002293 initDeltaX = child.getTranslationX();
2294 initDeltaY = child.getTranslationY();
Adam Cohen307fe232012-08-16 17:55:58 -07002295 finalScale = getChildrenScale() - 4.0f / child.getWidth();
Adam Cohend024f982012-05-23 18:26:45 -07002296 initScale = child.getScaleX();
Adam Cohen19f37922012-03-21 11:59:11 -07002297 this.child = child;
2298 }
2299
Adam Cohend024f982012-05-23 18:26:45 -07002300 void animate() {
Adam Cohen19f37922012-03-21 11:59:11 -07002301 if (mShakeAnimators.containsKey(child)) {
2302 ReorderHintAnimation oldAnimation = mShakeAnimators.get(child);
Adam Cohend024f982012-05-23 18:26:45 -07002303 oldAnimation.cancel();
Adam Cohen19f37922012-03-21 11:59:11 -07002304 mShakeAnimators.remove(child);
Adam Cohene7587d22012-05-24 18:50:02 -07002305 if (finalDeltaX == 0 && finalDeltaY == 0) {
2306 completeAnimationImmediately();
2307 return;
2308 }
Adam Cohen19f37922012-03-21 11:59:11 -07002309 }
Adam Cohend024f982012-05-23 18:26:45 -07002310 if (finalDeltaX == 0 && finalDeltaY == 0) {
Adam Cohen19f37922012-03-21 11:59:11 -07002311 return;
2312 }
Michael Jurkaf1ad6082013-03-13 12:55:46 +01002313 ValueAnimator va = LauncherAnimUtils.ofFloat(child, 0f, 1f);
Adam Cohene7587d22012-05-24 18:50:02 -07002314 a = va;
Adam Cohen19f37922012-03-21 11:59:11 -07002315 va.setRepeatMode(ValueAnimator.REVERSE);
2316 va.setRepeatCount(ValueAnimator.INFINITE);
Adam Cohen7bdfc972012-05-22 16:50:35 -07002317 va.setDuration(DURATION);
Adam Cohend024f982012-05-23 18:26:45 -07002318 va.setStartDelay((int) (Math.random() * 60));
Adam Cohen19f37922012-03-21 11:59:11 -07002319 va.addUpdateListener(new AnimatorUpdateListener() {
2320 @Override
2321 public void onAnimationUpdate(ValueAnimator animation) {
2322 float r = ((Float) animation.getAnimatedValue()).floatValue();
Adam Cohend024f982012-05-23 18:26:45 -07002323 float x = r * finalDeltaX + (1 - r) * initDeltaX;
2324 float y = r * finalDeltaY + (1 - r) * initDeltaY;
Adam Cohen19f37922012-03-21 11:59:11 -07002325 child.setTranslationX(x);
2326 child.setTranslationY(y);
Adam Cohend024f982012-05-23 18:26:45 -07002327 float s = r * finalScale + (1 - r) * initScale;
Brandon Keely50e6e562012-05-08 16:28:49 -07002328 child.setScaleX(s);
2329 child.setScaleY(s);
Adam Cohen19f37922012-03-21 11:59:11 -07002330 }
2331 });
2332 va.addListener(new AnimatorListenerAdapter() {
2333 public void onAnimationRepeat(Animator animation) {
Adam Cohen19f37922012-03-21 11:59:11 -07002334 // We make sure to end only after a full period
Adam Cohend024f982012-05-23 18:26:45 -07002335 initDeltaX = 0;
2336 initDeltaY = 0;
Adam Cohen307fe232012-08-16 17:55:58 -07002337 initScale = getChildrenScale();
Adam Cohen19f37922012-03-21 11:59:11 -07002338 }
2339 });
Adam Cohen19f37922012-03-21 11:59:11 -07002340 mShakeAnimators.put(child, this);
2341 va.start();
2342 }
2343
Adam Cohend024f982012-05-23 18:26:45 -07002344 private void cancel() {
Adam Cohene7587d22012-05-24 18:50:02 -07002345 if (a != null) {
2346 a.cancel();
2347 }
Adam Cohen19f37922012-03-21 11:59:11 -07002348 }
Adam Cohene7587d22012-05-24 18:50:02 -07002349
Brandon Keely50e6e562012-05-08 16:28:49 -07002350 private void completeAnimationImmediately() {
Adam Cohene7587d22012-05-24 18:50:02 -07002351 if (a != null) {
2352 a.cancel();
2353 }
Brandon Keely50e6e562012-05-08 16:28:49 -07002354
Michael Jurka2ecf9952012-06-18 12:52:28 -07002355 AnimatorSet s = LauncherAnimUtils.createAnimatorSet();
Adam Cohene7587d22012-05-24 18:50:02 -07002356 a = s;
Brandon Keely50e6e562012-05-08 16:28:49 -07002357 s.playTogether(
Adam Cohen307fe232012-08-16 17:55:58 -07002358 LauncherAnimUtils.ofFloat(child, "scaleX", getChildrenScale()),
2359 LauncherAnimUtils.ofFloat(child, "scaleY", getChildrenScale()),
Michael Jurka2ecf9952012-06-18 12:52:28 -07002360 LauncherAnimUtils.ofFloat(child, "translationX", 0f),
2361 LauncherAnimUtils.ofFloat(child, "translationY", 0f)
Brandon Keely50e6e562012-05-08 16:28:49 -07002362 );
2363 s.setDuration(REORDER_ANIMATION_DURATION);
2364 s.setInterpolator(new android.view.animation.DecelerateInterpolator(1.5f));
2365 s.start();
2366 }
Adam Cohen19f37922012-03-21 11:59:11 -07002367 }
2368
2369 private void completeAndClearReorderHintAnimations() {
2370 for (ReorderHintAnimation a: mShakeAnimators.values()) {
Brandon Keely50e6e562012-05-08 16:28:49 -07002371 a.completeAnimationImmediately();
Adam Cohen19f37922012-03-21 11:59:11 -07002372 }
2373 mShakeAnimators.clear();
2374 }
2375
Adam Cohen482ed822012-03-02 14:15:13 -08002376 private void commitTempPlacement() {
2377 for (int i = 0; i < mCountX; i++) {
2378 for (int j = 0; j < mCountY; j++) {
2379 mOccupied[i][j] = mTmpOccupied[i][j];
2380 }
2381 }
Michael Jurkaa52570f2012-03-20 03:18:20 -07002382 int childCount = mShortcutsAndWidgets.getChildCount();
Adam Cohen482ed822012-03-02 14:15:13 -08002383 for (int i = 0; i < childCount; i++) {
Adam Cohenea889a22012-03-27 16:45:39 -07002384 View child = mShortcutsAndWidgets.getChildAt(i);
2385 LayoutParams lp = (LayoutParams) child.getLayoutParams();
2386 ItemInfo info = (ItemInfo) child.getTag();
Adam Cohen2acce882012-03-28 19:03:19 -07002387 // We do a null check here because the item info can be null in the case of the
2388 // AllApps button in the hotseat.
2389 if (info != null) {
Adam Cohen487f7dd2012-06-28 18:12:10 -07002390 if (info.cellX != lp.tmpCellX || info.cellY != lp.tmpCellY ||
2391 info.spanX != lp.cellHSpan || info.spanY != lp.cellVSpan) {
2392 info.requiresDbUpdate = true;
2393 }
Adam Cohen2acce882012-03-28 19:03:19 -07002394 info.cellX = lp.cellX = lp.tmpCellX;
2395 info.cellY = lp.cellY = lp.tmpCellY;
Adam Cohenbebf0422012-04-11 18:06:28 -07002396 info.spanX = lp.cellHSpan;
2397 info.spanY = lp.cellVSpan;
Adam Cohen2acce882012-03-28 19:03:19 -07002398 }
Adam Cohen482ed822012-03-02 14:15:13 -08002399 }
Adam Cohen2acce882012-03-28 19:03:19 -07002400 mLauncher.getWorkspace().updateItemLocationsInDatabase(this);
Adam Cohen482ed822012-03-02 14:15:13 -08002401 }
2402
2403 public void setUseTempCoords(boolean useTempCoords) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07002404 int childCount = mShortcutsAndWidgets.getChildCount();
Adam Cohen482ed822012-03-02 14:15:13 -08002405 for (int i = 0; i < childCount; i++) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07002406 LayoutParams lp = (LayoutParams) mShortcutsAndWidgets.getChildAt(i).getLayoutParams();
Adam Cohen482ed822012-03-02 14:15:13 -08002407 lp.useTmpCoords = useTempCoords;
2408 }
2409 }
2410
Adam Cohen482ed822012-03-02 14:15:13 -08002411 ItemConfiguration findConfigurationNoShuffle(int pixelX, int pixelY, int minSpanX, int minSpanY,
2412 int spanX, int spanY, View dragView, ItemConfiguration solution) {
2413 int[] result = new int[2];
2414 int[] resultSpan = new int[2];
2415 findNearestVacantArea(pixelX, pixelY, minSpanX, minSpanY, spanX, spanY, null, result,
2416 resultSpan);
2417 if (result[0] >= 0 && result[1] >= 0) {
2418 copyCurrentStateToSolution(solution, false);
2419 solution.dragViewX = result[0];
2420 solution.dragViewY = result[1];
2421 solution.dragViewSpanX = resultSpan[0];
2422 solution.dragViewSpanY = resultSpan[1];
2423 solution.isSolution = true;
2424 } else {
2425 solution.isSolution = false;
2426 }
2427 return solution;
2428 }
2429
2430 public void prepareChildForDrag(View child) {
2431 markCellsAsUnoccupiedForView(child);
Adam Cohen482ed822012-03-02 14:15:13 -08002432 }
2433
Adam Cohen19f37922012-03-21 11:59:11 -07002434 /* This seems like it should be obvious and straight-forward, but when the direction vector
2435 needs to match with the notion of the dragView pushing other views, we have to employ
2436 a slightly more subtle notion of the direction vector. The question is what two points is
2437 the vector between? The center of the dragView and its desired destination? Not quite, as
2438 this doesn't necessarily coincide with the interaction of the dragView and items occupying
2439 those cells. Instead we use some heuristics to often lock the vector to up, down, left
2440 or right, which helps make pushing feel right.
2441 */
2442 private void getDirectionVectorForDrop(int dragViewCenterX, int dragViewCenterY, int spanX,
2443 int spanY, View dragView, int[] resultDirection) {
2444 int[] targetDestination = new int[2];
2445
2446 findNearestArea(dragViewCenterX, dragViewCenterY, spanX, spanY, targetDestination);
2447 Rect dragRect = new Rect();
2448 regionToRect(targetDestination[0], targetDestination[1], spanX, spanY, dragRect);
2449 dragRect.offset(dragViewCenterX - dragRect.centerX(), dragViewCenterY - dragRect.centerY());
2450
2451 Rect dropRegionRect = new Rect();
2452 getViewsIntersectingRegion(targetDestination[0], targetDestination[1], spanX, spanY,
2453 dragView, dropRegionRect, mIntersectingViews);
2454
2455 int dropRegionSpanX = dropRegionRect.width();
2456 int dropRegionSpanY = dropRegionRect.height();
2457
2458 regionToRect(dropRegionRect.left, dropRegionRect.top, dropRegionRect.width(),
2459 dropRegionRect.height(), dropRegionRect);
2460
2461 int deltaX = (dropRegionRect.centerX() - dragViewCenterX) / spanX;
2462 int deltaY = (dropRegionRect.centerY() - dragViewCenterY) / spanY;
2463
2464 if (dropRegionSpanX == mCountX || spanX == mCountX) {
2465 deltaX = 0;
2466 }
2467 if (dropRegionSpanY == mCountY || spanY == mCountY) {
2468 deltaY = 0;
2469 }
2470
2471 if (deltaX == 0 && deltaY == 0) {
2472 // No idea what to do, give a random direction.
2473 resultDirection[0] = 1;
2474 resultDirection[1] = 0;
2475 } else {
2476 computeDirectionVector(deltaX, deltaY, resultDirection);
2477 }
2478 }
2479
2480 // For a given cell and span, fetch the set of views intersecting the region.
2481 private void getViewsIntersectingRegion(int cellX, int cellY, int spanX, int spanY,
2482 View dragView, Rect boundingRect, ArrayList<View> intersectingViews) {
2483 if (boundingRect != null) {
2484 boundingRect.set(cellX, cellY, cellX + spanX, cellY + spanY);
2485 }
2486 intersectingViews.clear();
2487 Rect r0 = new Rect(cellX, cellY, cellX + spanX, cellY + spanY);
2488 Rect r1 = new Rect();
2489 final int count = mShortcutsAndWidgets.getChildCount();
2490 for (int i = 0; i < count; i++) {
2491 View child = mShortcutsAndWidgets.getChildAt(i);
2492 if (child == dragView) continue;
2493 LayoutParams lp = (LayoutParams) child.getLayoutParams();
2494 r1.set(lp.cellX, lp.cellY, lp.cellX + lp.cellHSpan, lp.cellY + lp.cellVSpan);
2495 if (Rect.intersects(r0, r1)) {
2496 mIntersectingViews.add(child);
2497 if (boundingRect != null) {
2498 boundingRect.union(r1);
2499 }
2500 }
2501 }
2502 }
2503
2504 boolean isNearestDropLocationOccupied(int pixelX, int pixelY, int spanX, int spanY,
2505 View dragView, int[] result) {
2506 result = findNearestArea(pixelX, pixelY, spanX, spanY, result);
2507 getViewsIntersectingRegion(result[0], result[1], spanX, spanY, dragView, null,
2508 mIntersectingViews);
2509 return !mIntersectingViews.isEmpty();
2510 }
2511
2512 void revertTempState() {
2513 if (!isItemPlacementDirty() || DESTRUCTIVE_REORDER) return;
2514 final int count = mShortcutsAndWidgets.getChildCount();
2515 for (int i = 0; i < count; i++) {
2516 View child = mShortcutsAndWidgets.getChildAt(i);
2517 LayoutParams lp = (LayoutParams) child.getLayoutParams();
2518 if (lp.tmpCellX != lp.cellX || lp.tmpCellY != lp.cellY) {
2519 lp.tmpCellX = lp.cellX;
2520 lp.tmpCellY = lp.cellY;
2521 animateChildToPosition(child, lp.cellX, lp.cellY, REORDER_ANIMATION_DURATION,
2522 0, false, false);
2523 }
2524 }
2525 completeAndClearReorderHintAnimations();
2526 setItemPlacementDirty(false);
2527 }
2528
Adam Cohenbebf0422012-04-11 18:06:28 -07002529 boolean createAreaForResize(int cellX, int cellY, int spanX, int spanY,
2530 View dragView, int[] direction, boolean commit) {
2531 int[] pixelXY = new int[2];
2532 regionToCenterPoint(cellX, cellY, spanX, spanY, pixelXY);
2533
2534 // First we determine if things have moved enough to cause a different layout
2535 ItemConfiguration swapSolution = simpleSwap(pixelXY[0], pixelXY[1], spanX, spanY,
2536 spanX, spanY, direction, dragView, true, new ItemConfiguration());
2537
2538 setUseTempCoords(true);
2539 if (swapSolution != null && swapSolution.isSolution) {
2540 // If we're just testing for a possible location (MODE_ACCEPT_DROP), we don't bother
2541 // committing anything or animating anything as we just want to determine if a solution
2542 // exists
2543 copySolutionToTempState(swapSolution, dragView);
2544 setItemPlacementDirty(true);
2545 animateItemsToSolution(swapSolution, dragView, commit);
2546
2547 if (commit) {
2548 commitTempPlacement();
2549 completeAndClearReorderHintAnimations();
2550 setItemPlacementDirty(false);
2551 } else {
2552 beginOrAdjustHintAnimations(swapSolution, dragView,
2553 REORDER_ANIMATION_DURATION);
2554 }
2555 mShortcutsAndWidgets.requestLayout();
2556 }
2557 return swapSolution.isSolution;
2558 }
2559
Adam Cohen482ed822012-03-02 14:15:13 -08002560 int[] createArea(int pixelX, int pixelY, int minSpanX, int minSpanY, int spanX, int spanY,
2561 View dragView, int[] result, int resultSpan[], int mode) {
Adam Cohen482ed822012-03-02 14:15:13 -08002562 // First we determine if things have moved enough to cause a different layout
Adam Cohen47a876d2012-03-19 13:21:41 -07002563 result = findNearestArea(pixelX, pixelY, spanX, spanY, result);
Adam Cohen482ed822012-03-02 14:15:13 -08002564
2565 if (resultSpan == null) {
2566 resultSpan = new int[2];
2567 }
2568
Adam Cohen19f37922012-03-21 11:59:11 -07002569 // When we are checking drop validity or actually dropping, we don't recompute the
2570 // direction vector, since we want the solution to match the preview, and it's possible
2571 // that the exact position of the item has changed to result in a new reordering outcome.
Adam Cohenb209e632012-03-27 17:09:36 -07002572 if ((mode == MODE_ON_DROP || mode == MODE_ON_DROP_EXTERNAL || mode == MODE_ACCEPT_DROP)
2573 && mPreviousReorderDirection[0] != INVALID_DIRECTION) {
Adam Cohen19f37922012-03-21 11:59:11 -07002574 mDirectionVector[0] = mPreviousReorderDirection[0];
2575 mDirectionVector[1] = mPreviousReorderDirection[1];
2576 // We reset this vector after drop
Adam Cohenb209e632012-03-27 17:09:36 -07002577 if (mode == MODE_ON_DROP || mode == MODE_ON_DROP_EXTERNAL) {
2578 mPreviousReorderDirection[0] = INVALID_DIRECTION;
2579 mPreviousReorderDirection[1] = INVALID_DIRECTION;
Adam Cohen19f37922012-03-21 11:59:11 -07002580 }
2581 } else {
2582 getDirectionVectorForDrop(pixelX, pixelY, spanX, spanY, dragView, mDirectionVector);
2583 mPreviousReorderDirection[0] = mDirectionVector[0];
2584 mPreviousReorderDirection[1] = mDirectionVector[1];
2585 }
2586
Adam Cohen482ed822012-03-02 14:15:13 -08002587 ItemConfiguration swapSolution = simpleSwap(pixelX, pixelY, minSpanX, minSpanY,
2588 spanX, spanY, mDirectionVector, dragView, true, new ItemConfiguration());
2589
2590 // We attempt the approach which doesn't shuffle views at all
2591 ItemConfiguration noShuffleSolution = findConfigurationNoShuffle(pixelX, pixelY, minSpanX,
2592 minSpanY, spanX, spanY, dragView, new ItemConfiguration());
2593
2594 ItemConfiguration finalSolution = null;
2595 if (swapSolution.isSolution && swapSolution.area() >= noShuffleSolution.area()) {
2596 finalSolution = swapSolution;
2597 } else if (noShuffleSolution.isSolution) {
2598 finalSolution = noShuffleSolution;
2599 }
2600
2601 boolean foundSolution = true;
2602 if (!DESTRUCTIVE_REORDER) {
2603 setUseTempCoords(true);
2604 }
2605
2606 if (finalSolution != null) {
2607 result[0] = finalSolution.dragViewX;
2608 result[1] = finalSolution.dragViewY;
2609 resultSpan[0] = finalSolution.dragViewSpanX;
2610 resultSpan[1] = finalSolution.dragViewSpanY;
2611
2612 // If we're just testing for a possible location (MODE_ACCEPT_DROP), we don't bother
2613 // committing anything or animating anything as we just want to determine if a solution
2614 // exists
2615 if (mode == MODE_DRAG_OVER || mode == MODE_ON_DROP || mode == MODE_ON_DROP_EXTERNAL) {
2616 if (!DESTRUCTIVE_REORDER) {
2617 copySolutionToTempState(finalSolution, dragView);
2618 }
2619 setItemPlacementDirty(true);
2620 animateItemsToSolution(finalSolution, dragView, mode == MODE_ON_DROP);
2621
Adam Cohen19f37922012-03-21 11:59:11 -07002622 if (!DESTRUCTIVE_REORDER &&
2623 (mode == MODE_ON_DROP || mode == MODE_ON_DROP_EXTERNAL)) {
Adam Cohen482ed822012-03-02 14:15:13 -08002624 commitTempPlacement();
Adam Cohen19f37922012-03-21 11:59:11 -07002625 completeAndClearReorderHintAnimations();
2626 setItemPlacementDirty(false);
2627 } else {
2628 beginOrAdjustHintAnimations(finalSolution, dragView,
2629 REORDER_ANIMATION_DURATION);
Adam Cohen482ed822012-03-02 14:15:13 -08002630 }
2631 }
2632 } else {
2633 foundSolution = false;
2634 result[0] = result[1] = resultSpan[0] = resultSpan[1] = -1;
2635 }
2636
2637 if ((mode == MODE_ON_DROP || !foundSolution) && !DESTRUCTIVE_REORDER) {
2638 setUseTempCoords(false);
2639 }
Adam Cohen482ed822012-03-02 14:15:13 -08002640
Michael Jurkaa52570f2012-03-20 03:18:20 -07002641 mShortcutsAndWidgets.requestLayout();
Adam Cohen482ed822012-03-02 14:15:13 -08002642 return result;
2643 }
2644
Adam Cohen19f37922012-03-21 11:59:11 -07002645 void setItemPlacementDirty(boolean dirty) {
2646 mItemPlacementDirty = dirty;
Adam Cohen482ed822012-03-02 14:15:13 -08002647 }
Adam Cohen19f37922012-03-21 11:59:11 -07002648 boolean isItemPlacementDirty() {
2649 return mItemPlacementDirty;
Adam Cohen482ed822012-03-02 14:15:13 -08002650 }
2651
2652 private class ItemConfiguration {
Adam Cohen8baab352012-03-20 17:39:21 -07002653 HashMap<View, CellAndSpan> map = new HashMap<View, CellAndSpan>();
Adam Cohenf3900c22012-11-16 18:28:11 -08002654 private HashMap<View, CellAndSpan> savedMap = new HashMap<View, CellAndSpan>();
2655 ArrayList<View> sortedViews = new ArrayList<View>();
Adam Cohen482ed822012-03-02 14:15:13 -08002656 boolean isSolution = false;
2657 int dragViewX, dragViewY, dragViewSpanX, dragViewSpanY;
2658
Adam Cohenf3900c22012-11-16 18:28:11 -08002659 void save() {
2660 // Copy current state into savedMap
2661 for (View v: map.keySet()) {
2662 map.get(v).copy(savedMap.get(v));
2663 }
2664 }
2665
2666 void restore() {
2667 // Restore current state from savedMap
2668 for (View v: savedMap.keySet()) {
2669 savedMap.get(v).copy(map.get(v));
2670 }
2671 }
2672
2673 void add(View v, CellAndSpan cs) {
2674 map.put(v, cs);
2675 savedMap.put(v, new CellAndSpan());
2676 sortedViews.add(v);
2677 }
2678
Adam Cohen482ed822012-03-02 14:15:13 -08002679 int area() {
2680 return dragViewSpanX * dragViewSpanY;
2681 }
Adam Cohen8baab352012-03-20 17:39:21 -07002682 }
2683
2684 private class CellAndSpan {
2685 int x, y;
2686 int spanX, spanY;
2687
Adam Cohenf3900c22012-11-16 18:28:11 -08002688 public CellAndSpan() {
2689 }
2690
2691 public void copy(CellAndSpan copy) {
2692 copy.x = x;
2693 copy.y = y;
2694 copy.spanX = spanX;
2695 copy.spanY = spanY;
2696 }
2697
Adam Cohen8baab352012-03-20 17:39:21 -07002698 public CellAndSpan(int x, int y, int spanX, int spanY) {
2699 this.x = x;
2700 this.y = y;
2701 this.spanX = spanX;
2702 this.spanY = spanY;
Adam Cohen482ed822012-03-02 14:15:13 -08002703 }
Adam Cohenf3900c22012-11-16 18:28:11 -08002704
2705 public String toString() {
2706 return "(" + x + ", " + y + ": " + spanX + ", " + spanY + ")";
2707 }
2708
Adam Cohen482ed822012-03-02 14:15:13 -08002709 }
2710
Adam Cohendf035382011-04-11 17:22:04 -07002711 /**
2712 * Find a vacant area that will fit the given bounds nearest the requested
2713 * cell location. Uses Euclidean distance to score multiple vacant areas.
2714 *
2715 * @param pixelX The X location at which you want to search for a vacant area.
2716 * @param pixelY The Y location at which you want to search for a vacant area.
2717 * @param spanX Horizontal span of the object.
2718 * @param spanY Vertical span of the object.
2719 * @param ignoreView Considers space occupied by this view as unoccupied
2720 * @param result Previously returned value to possibly recycle.
2721 * @return The X, Y cell of a vacant area that can contain this object,
2722 * nearest the requested location.
2723 */
2724 int[] findNearestVacantArea(
2725 int pixelX, int pixelY, int spanX, int spanY, View ignoreView, int[] result) {
2726 return findNearestArea(pixelX, pixelY, spanX, spanY, ignoreView, true, result);
2727 }
2728
2729 /**
Adam Cohend41fbf52012-02-16 23:53:59 -08002730 * Find a vacant area that will fit the given bounds nearest the requested
2731 * cell location. Uses Euclidean distance to score multiple vacant areas.
2732 *
2733 * @param pixelX The X location at which you want to search for a vacant area.
2734 * @param pixelY The Y location at which you want to search for a vacant area.
2735 * @param minSpanX The minimum horizontal span required
2736 * @param minSpanY The minimum vertical span required
2737 * @param spanX Horizontal span of the object.
2738 * @param spanY Vertical span of the object.
2739 * @param ignoreView Considers space occupied by this view as unoccupied
2740 * @param result Previously returned value to possibly recycle.
2741 * @return The X, Y cell of a vacant area that can contain this object,
2742 * nearest the requested location.
2743 */
2744 int[] findNearestVacantArea(int pixelX, int pixelY, int minSpanX, int minSpanY,
2745 int spanX, int spanY, View ignoreView, int[] result, int[] resultSpan) {
Adam Cohen482ed822012-03-02 14:15:13 -08002746 return findNearestArea(pixelX, pixelY, minSpanX, minSpanY, spanX, spanY, ignoreView, true,
2747 result, resultSpan, mOccupied);
Adam Cohend41fbf52012-02-16 23:53:59 -08002748 }
2749
2750 /**
Adam Cohendf035382011-04-11 17:22:04 -07002751 * Find a starting cell position that will fit the given bounds nearest the requested
2752 * cell location. Uses Euclidean distance to score multiple vacant areas.
2753 *
2754 * @param pixelX The X location at which you want to search for a vacant area.
2755 * @param pixelY The Y location at which you want to search for a vacant area.
2756 * @param spanX Horizontal span of the object.
2757 * @param spanY Vertical span of the object.
2758 * @param ignoreView Considers space occupied by this view as unoccupied
2759 * @param result Previously returned value to possibly recycle.
2760 * @return The X, Y cell of a vacant area that can contain this object,
2761 * nearest the requested location.
2762 */
2763 int[] findNearestArea(
2764 int pixelX, int pixelY, int spanX, int spanY, int[] result) {
2765 return findNearestArea(pixelX, pixelY, spanX, spanY, null, false, result);
2766 }
2767
Michael Jurka0280c3b2010-09-17 15:00:07 -07002768 boolean existsEmptyCell() {
2769 return findCellForSpan(null, 1, 1);
2770 }
2771
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002772 /**
Michael Jurka0280c3b2010-09-17 15:00:07 -07002773 * Finds the upper-left coordinate of the first rectangle in the grid that can
2774 * hold a cell of the specified dimensions. If intersectX and intersectY are not -1,
2775 * then this method will only return coordinates for rectangles that contain the cell
2776 * (intersectX, intersectY)
2777 *
2778 * @param cellXY The array that will contain the position of a vacant cell if such a cell
2779 * can be found.
2780 * @param spanX The horizontal span of the cell we want to find.
2781 * @param spanY The vertical span of the cell we want to find.
2782 *
2783 * @return True if a vacant cell of the specified dimension was found, false otherwise.
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07002784 */
Michael Jurka0280c3b2010-09-17 15:00:07 -07002785 boolean findCellForSpan(int[] cellXY, int spanX, int spanY) {
Adam Cohen482ed822012-03-02 14:15:13 -08002786 return findCellForSpanThatIntersectsIgnoring(cellXY, spanX, spanY, -1, -1, null, mOccupied);
Michael Jurka0280c3b2010-09-17 15:00:07 -07002787 }
2788
2789 /**
2790 * Like above, but ignores any cells occupied by the item "ignoreView"
2791 *
2792 * @param cellXY The array that will contain the position of a vacant cell if such a cell
2793 * can be found.
2794 * @param spanX The horizontal span of the cell we want to find.
2795 * @param spanY The vertical span of the cell we want to find.
2796 * @param ignoreView The home screen item we should treat as not occupying any space
2797 * @return
2798 */
2799 boolean findCellForSpanIgnoring(int[] cellXY, int spanX, int spanY, View ignoreView) {
Adam Cohen482ed822012-03-02 14:15:13 -08002800 return findCellForSpanThatIntersectsIgnoring(cellXY, spanX, spanY, -1, -1,
2801 ignoreView, mOccupied);
Michael Jurka0280c3b2010-09-17 15:00:07 -07002802 }
2803
2804 /**
2805 * Like above, but if intersectX and intersectY are not -1, then this method will try to
2806 * return coordinates for rectangles that contain the cell [intersectX, intersectY]
2807 *
2808 * @param spanX The horizontal span of the cell we want to find.
2809 * @param spanY The vertical span of the cell we want to find.
2810 * @param ignoreView The home screen item we should treat as not occupying any space
2811 * @param intersectX The X coordinate of the cell that we should try to overlap
2812 * @param intersectX The Y coordinate of the cell that we should try to overlap
2813 *
2814 * @return True if a vacant cell of the specified dimension was found, false otherwise.
2815 */
2816 boolean findCellForSpanThatIntersects(int[] cellXY, int spanX, int spanY,
2817 int intersectX, int intersectY) {
2818 return findCellForSpanThatIntersectsIgnoring(
Adam Cohen482ed822012-03-02 14:15:13 -08002819 cellXY, spanX, spanY, intersectX, intersectY, null, mOccupied);
Michael Jurka0280c3b2010-09-17 15:00:07 -07002820 }
2821
2822 /**
2823 * The superset of the above two methods
2824 */
2825 boolean findCellForSpanThatIntersectsIgnoring(int[] cellXY, int spanX, int spanY,
Adam Cohen482ed822012-03-02 14:15:13 -08002826 int intersectX, int intersectY, View ignoreView, boolean occupied[][]) {
Michael Jurkac6ee42e2010-09-30 12:04:50 -07002827 // mark space take by ignoreView as available (method checks if ignoreView is null)
Adam Cohen482ed822012-03-02 14:15:13 -08002828 markCellsAsUnoccupiedForView(ignoreView, occupied);
Michael Jurka0280c3b2010-09-17 15:00:07 -07002829
Michael Jurka28750fb2010-09-24 17:43:49 -07002830 boolean foundCell = false;
Michael Jurka0280c3b2010-09-17 15:00:07 -07002831 while (true) {
2832 int startX = 0;
2833 if (intersectX >= 0) {
2834 startX = Math.max(startX, intersectX - (spanX - 1));
2835 }
2836 int endX = mCountX - (spanX - 1);
2837 if (intersectX >= 0) {
2838 endX = Math.min(endX, intersectX + (spanX - 1) + (spanX == 1 ? 1 : 0));
2839 }
2840 int startY = 0;
2841 if (intersectY >= 0) {
2842 startY = Math.max(startY, intersectY - (spanY - 1));
2843 }
2844 int endY = mCountY - (spanY - 1);
2845 if (intersectY >= 0) {
2846 endY = Math.min(endY, intersectY + (spanY - 1) + (spanY == 1 ? 1 : 0));
2847 }
2848
Winson Chungbbc60d82010-11-11 16:34:41 -08002849 for (int y = startY; y < endY && !foundCell; y++) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002850 inner:
Winson Chungbbc60d82010-11-11 16:34:41 -08002851 for (int x = startX; x < endX; x++) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002852 for (int i = 0; i < spanX; i++) {
2853 for (int j = 0; j < spanY; j++) {
Adam Cohen482ed822012-03-02 14:15:13 -08002854 if (occupied[x + i][y + j]) {
Winson Chungbbc60d82010-11-11 16:34:41 -08002855 // small optimization: we can skip to after the column we just found
Michael Jurka0280c3b2010-09-17 15:00:07 -07002856 // an occupied cell
Winson Chungbbc60d82010-11-11 16:34:41 -08002857 x += i;
Michael Jurka0280c3b2010-09-17 15:00:07 -07002858 continue inner;
2859 }
2860 }
2861 }
2862 if (cellXY != null) {
2863 cellXY[0] = x;
2864 cellXY[1] = y;
2865 }
Michael Jurka28750fb2010-09-24 17:43:49 -07002866 foundCell = true;
2867 break;
Michael Jurka0280c3b2010-09-17 15:00:07 -07002868 }
2869 }
2870 if (intersectX == -1 && intersectY == -1) {
2871 break;
2872 } else {
2873 // if we failed to find anything, try again but without any requirements of
2874 // intersecting
2875 intersectX = -1;
2876 intersectY = -1;
2877 continue;
2878 }
2879 }
2880
Michael Jurkac6ee42e2010-09-30 12:04:50 -07002881 // re-mark space taken by ignoreView as occupied
Adam Cohen482ed822012-03-02 14:15:13 -08002882 markCellsAsOccupiedForView(ignoreView, occupied);
Michael Jurka28750fb2010-09-24 17:43:49 -07002883 return foundCell;
Michael Jurka0280c3b2010-09-17 15:00:07 -07002884 }
2885
2886 /**
Winson Chungc07918d2011-07-01 15:35:26 -07002887 * A drag event has begun over this layout.
2888 * It may have begun over this layout (in which case onDragChild is called first),
2889 * or it may have begun on another layout.
2890 */
2891 void onDragEnter() {
Adam Cohenc6cc61d2012-04-04 12:47:08 -07002892 mDragEnforcer.onDragEnter();
Winson Chungc07918d2011-07-01 15:35:26 -07002893 mDragging = true;
2894 }
2895
2896 /**
Michael Jurka0280c3b2010-09-17 15:00:07 -07002897 * Called when drag has left this CellLayout or has been completed (successfully or not)
2898 */
2899 void onDragExit() {
Adam Cohenc6cc61d2012-04-04 12:47:08 -07002900 mDragEnforcer.onDragExit();
Joe Onorato4be866d2010-10-10 11:26:02 -07002901 // This can actually be called when we aren't in a drag, e.g. when adding a new
2902 // item to this layout via the customize drawer.
2903 // Guard against that case.
2904 if (mDragging) {
2905 mDragging = false;
Patrick Dubroyde7658b2010-09-27 11:15:43 -07002906 }
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -07002907
2908 // Invalidate the drag data
Adam Cohend41fbf52012-02-16 23:53:59 -08002909 mDragCell[0] = mDragCell[1] = -1;
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -07002910 mDragOutlineAnims[mDragOutlineCurrent].animateOut();
2911 mDragOutlineCurrent = (mDragOutlineCurrent + 1) % mDragOutlineAnims.length;
Adam Cohen19f37922012-03-21 11:59:11 -07002912 revertTempState();
Michael Jurka33945b22010-12-21 18:19:38 -08002913 setIsDragOverlapping(false);
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07002914 }
2915
2916 /**
Winson Chungaafa03c2010-06-11 17:34:16 -07002917 * Mark a child as having been dropped.
Patrick Dubroyde7658b2010-09-27 11:15:43 -07002918 * At the beginning of the drag operation, the child may have been on another
Patrick Dubroyce34a972010-10-19 10:34:32 -07002919 * screen, but it is re-parented before this method is called.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002920 *
2921 * @param child The child that is being dropped
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002922 */
Adam Cohen716b51e2011-06-30 12:09:54 -07002923 void onDropChild(View child) {
Romain Guyd94533d2009-08-17 10:01:15 -07002924 if (child != null) {
2925 LayoutParams lp = (LayoutParams) child.getLayoutParams();
Romain Guy84f296c2009-11-04 15:00:44 -08002926 lp.dropped = true;
Romain Guyd94533d2009-08-17 10:01:15 -07002927 child.requestLayout();
Romain Guyd94533d2009-08-17 10:01:15 -07002928 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002929 }
2930
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002931 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002932 * Computes a bounding rectangle for a range of cells
Winson Chungaafa03c2010-06-11 17:34:16 -07002933 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002934 * @param cellX X coordinate of upper left corner expressed as a cell position
2935 * @param cellY Y coordinate of upper left corner expressed as a cell position
Winson Chungaafa03c2010-06-11 17:34:16 -07002936 * @param cellHSpan Width in cells
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002937 * @param cellVSpan Height in cells
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07002938 * @param resultRect Rect into which to put the results
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002939 */
Adam Cohend41fbf52012-02-16 23:53:59 -08002940 public void cellToRect(int cellX, int cellY, int cellHSpan, int cellVSpan, Rect resultRect) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002941 final int cellWidth = mCellWidth;
2942 final int cellHeight = mCellHeight;
2943 final int widthGap = mWidthGap;
2944 final int heightGap = mHeightGap;
Winson Chungaafa03c2010-06-11 17:34:16 -07002945
Winson Chung4b825dcd2011-06-19 12:41:22 -07002946 final int hStartPadding = getPaddingLeft();
2947 final int vStartPadding = getPaddingTop();
Winson Chungaafa03c2010-06-11 17:34:16 -07002948
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002949 int width = cellHSpan * cellWidth + ((cellHSpan - 1) * widthGap);
2950 int height = cellVSpan * cellHeight + ((cellVSpan - 1) * heightGap);
2951
2952 int x = hStartPadding + cellX * (cellWidth + widthGap);
2953 int y = vStartPadding + cellY * (cellHeight + heightGap);
Winson Chungaafa03c2010-06-11 17:34:16 -07002954
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07002955 resultRect.set(x, y, x + width, y + height);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002956 }
Winson Chungaafa03c2010-06-11 17:34:16 -07002957
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002958 /**
Winson Chungaafa03c2010-06-11 17:34:16 -07002959 * Computes the required horizontal and vertical cell spans to always
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002960 * fit the given rectangle.
Winson Chungaafa03c2010-06-11 17:34:16 -07002961 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002962 * @param width Width in pixels
2963 * @param height Height in pixels
Patrick Dubroy8f86ddc2010-07-16 13:55:32 -07002964 * @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 -08002965 */
Winson Chung66700732013-08-20 16:56:15 -07002966 public static int[] rectToCell(int width, int height, int[] result) {
Winson Chung5f8afe62013-08-12 16:19:28 -07002967 LauncherAppState app = LauncherAppState.getInstance();
2968 DeviceProfile grid = app.getDynamicGrid().getDeviceProfile();
Winson Chung66700732013-08-20 16:56:15 -07002969 Rect padding = grid.getWorkspacePadding(grid.isLandscape ?
2970 CellLayout.LANDSCAPE : CellLayout.PORTRAIT);
Winson Chung5f8afe62013-08-12 16:19:28 -07002971
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002972 // Always assume we're working with the smallest span to make sure we
2973 // reserve enough space in both orientations.
Winson Chung66700732013-08-20 16:56:15 -07002974 int parentWidth = grid.calculateCellWidth(grid.widthPx
2975 - padding.left - padding.right, (int) grid.numColumns);
2976 int parentHeight = grid.calculateCellHeight(grid.heightPx
2977 - padding.top - padding.bottom, (int) grid.numRows);
2978 int smallerSize = Math.min(parentWidth, parentHeight);
Joe Onorato79e56262009-09-21 15:23:04 -04002979
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002980 // Always round up to next largest cell
Winson Chung54c725c2011-08-03 12:03:40 -07002981 int spanX = (int) Math.ceil(width / (float) smallerSize);
2982 int spanY = (int) Math.ceil(height / (float) smallerSize);
Joe Onorato79e56262009-09-21 15:23:04 -04002983
Patrick Dubroy8f86ddc2010-07-16 13:55:32 -07002984 if (result == null) {
2985 return new int[] { spanX, spanY };
2986 }
2987 result[0] = spanX;
2988 result[1] = spanY;
2989 return result;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002990 }
2991
Michael Jurkaf12c75c2011-01-25 22:41:40 -08002992 public int[] cellSpansToSize(int hSpans, int vSpans) {
2993 int[] size = new int[2];
2994 size[0] = hSpans * mCellWidth + (hSpans - 1) * mWidthGap;
2995 size[1] = vSpans * mCellHeight + (vSpans - 1) * mHeightGap;
2996 return size;
2997 }
2998
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002999 /**
Patrick Dubroy047379a2010-12-19 22:02:04 -08003000 * Calculate the grid spans needed to fit given item
3001 */
3002 public void calculateSpans(ItemInfo info) {
3003 final int minWidth;
3004 final int minHeight;
3005
3006 if (info instanceof LauncherAppWidgetInfo) {
3007 minWidth = ((LauncherAppWidgetInfo) info).minWidth;
3008 minHeight = ((LauncherAppWidgetInfo) info).minHeight;
3009 } else if (info instanceof PendingAddWidgetInfo) {
3010 minWidth = ((PendingAddWidgetInfo) info).minWidth;
3011 minHeight = ((PendingAddWidgetInfo) info).minHeight;
3012 } else {
3013 // It's not a widget, so it must be 1x1
3014 info.spanX = info.spanY = 1;
3015 return;
3016 }
3017 int[] spans = rectToCell(minWidth, minHeight, null);
3018 info.spanX = spans[0];
3019 info.spanY = spans[1];
3020 }
3021
3022 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003023 * Find the first vacant cell, if there is one.
3024 *
3025 * @param vacant Holds the x and y coordinate of the vacant cell
3026 * @param spanX Horizontal cell span.
3027 * @param spanY Vertical cell span.
Winson Chungaafa03c2010-06-11 17:34:16 -07003028 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003029 * @return True if a vacant cell was found
3030 */
3031 public boolean getVacantCell(int[] vacant, int spanX, int spanY) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003032
Michael Jurka0280c3b2010-09-17 15:00:07 -07003033 return findVacantCell(vacant, spanX, spanY, mCountX, mCountY, mOccupied);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003034 }
3035
3036 static boolean findVacantCell(int[] vacant, int spanX, int spanY,
3037 int xCount, int yCount, boolean[][] occupied) {
3038
Adam Cohen2801caf2011-05-13 20:57:39 -07003039 for (int y = 0; y < yCount; y++) {
3040 for (int x = 0; x < xCount; x++) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003041 boolean available = !occupied[x][y];
3042out: for (int i = x; i < x + spanX - 1 && x < xCount; i++) {
3043 for (int j = y; j < y + spanY - 1 && y < yCount; j++) {
3044 available = available && !occupied[i][j];
3045 if (!available) break out;
3046 }
3047 }
3048
3049 if (available) {
3050 vacant[0] = x;
3051 vacant[1] = y;
3052 return true;
3053 }
3054 }
3055 }
3056
3057 return false;
3058 }
3059
Michael Jurka0280c3b2010-09-17 15:00:07 -07003060 private void clearOccupiedCells() {
3061 for (int x = 0; x < mCountX; x++) {
3062 for (int y = 0; y < mCountY; y++) {
3063 mOccupied[x][y] = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003064 }
3065 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07003066 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003067
Adam Cohend41fbf52012-02-16 23:53:59 -08003068 public void onMove(View view, int newCellX, int newCellY, int newSpanX, int newSpanY) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07003069 markCellsAsUnoccupiedForView(view);
Adam Cohen482ed822012-03-02 14:15:13 -08003070 markCellsForView(newCellX, newCellY, newSpanX, newSpanY, mOccupied, true);
Michael Jurka0280c3b2010-09-17 15:00:07 -07003071 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003072
Adam Cohend4844c32011-02-18 19:25:06 -08003073 public void markCellsAsOccupiedForView(View view) {
Adam Cohen482ed822012-03-02 14:15:13 -08003074 markCellsAsOccupiedForView(view, mOccupied);
3075 }
3076 public void markCellsAsOccupiedForView(View view, boolean[][] occupied) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07003077 if (view == null || view.getParent() != mShortcutsAndWidgets) return;
Michael Jurka0280c3b2010-09-17 15:00:07 -07003078 LayoutParams lp = (LayoutParams) view.getLayoutParams();
Adam Cohen482ed822012-03-02 14:15:13 -08003079 markCellsForView(lp.cellX, lp.cellY, lp.cellHSpan, lp.cellVSpan, occupied, true);
Michael Jurka0280c3b2010-09-17 15:00:07 -07003080 }
3081
Adam Cohend4844c32011-02-18 19:25:06 -08003082 public void markCellsAsUnoccupiedForView(View view) {
Adam Cohen482ed822012-03-02 14:15:13 -08003083 markCellsAsUnoccupiedForView(view, mOccupied);
3084 }
3085 public void markCellsAsUnoccupiedForView(View view, boolean occupied[][]) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07003086 if (view == null || view.getParent() != mShortcutsAndWidgets) return;
Michael Jurka0280c3b2010-09-17 15:00:07 -07003087 LayoutParams lp = (LayoutParams) view.getLayoutParams();
Adam Cohen482ed822012-03-02 14:15:13 -08003088 markCellsForView(lp.cellX, lp.cellY, lp.cellHSpan, lp.cellVSpan, occupied, false);
Michael Jurka0280c3b2010-09-17 15:00:07 -07003089 }
3090
Adam Cohen482ed822012-03-02 14:15:13 -08003091 private void markCellsForView(int cellX, int cellY, int spanX, int spanY, boolean[][] occupied,
3092 boolean value) {
3093 if (cellX < 0 || cellY < 0) return;
Michael Jurka0280c3b2010-09-17 15:00:07 -07003094 for (int x = cellX; x < cellX + spanX && x < mCountX; x++) {
3095 for (int y = cellY; y < cellY + spanY && y < mCountY; y++) {
Adam Cohen482ed822012-03-02 14:15:13 -08003096 occupied[x][y] = value;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003097 }
3098 }
3099 }
3100
Adam Cohen2801caf2011-05-13 20:57:39 -07003101 public int getDesiredWidth() {
Michael Jurka8b805b12012-04-18 14:23:14 -07003102 return getPaddingLeft() + getPaddingRight() + (mCountX * mCellWidth) +
Adam Cohen2801caf2011-05-13 20:57:39 -07003103 (Math.max((mCountX - 1), 0) * mWidthGap);
3104 }
3105
3106 public int getDesiredHeight() {
Michael Jurka8b805b12012-04-18 14:23:14 -07003107 return getPaddingTop() + getPaddingBottom() + (mCountY * mCellHeight) +
Adam Cohen2801caf2011-05-13 20:57:39 -07003108 (Math.max((mCountY - 1), 0) * mHeightGap);
3109 }
3110
Michael Jurka66d72172011-04-12 16:29:25 -07003111 public boolean isOccupied(int x, int y) {
3112 if (x < mCountX && y < mCountY) {
3113 return mOccupied[x][y];
3114 } else {
3115 throw new RuntimeException("Position exceeds the bound of this CellLayout");
3116 }
3117 }
3118
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003119 @Override
3120 public ViewGroup.LayoutParams generateLayoutParams(AttributeSet attrs) {
3121 return new CellLayout.LayoutParams(getContext(), attrs);
3122 }
3123
3124 @Override
3125 protected boolean checkLayoutParams(ViewGroup.LayoutParams p) {
3126 return p instanceof CellLayout.LayoutParams;
3127 }
3128
3129 @Override
3130 protected ViewGroup.LayoutParams generateLayoutParams(ViewGroup.LayoutParams p) {
3131 return new CellLayout.LayoutParams(p);
3132 }
3133
Winson Chungaafa03c2010-06-11 17:34:16 -07003134 public static class CellLayoutAnimationController extends LayoutAnimationController {
3135 public CellLayoutAnimationController(Animation animation, float delay) {
3136 super(animation, delay);
3137 }
3138
3139 @Override
3140 protected long getDelayForView(View view) {
3141 return (int) (Math.random() * 150);
3142 }
3143 }
3144
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003145 public static class LayoutParams extends ViewGroup.MarginLayoutParams {
3146 /**
3147 * Horizontal location of the item in the grid.
3148 */
3149 @ViewDebug.ExportedProperty
3150 public int cellX;
3151
3152 /**
3153 * Vertical location of the item in the grid.
3154 */
3155 @ViewDebug.ExportedProperty
3156 public int cellY;
3157
3158 /**
Adam Cohen482ed822012-03-02 14:15:13 -08003159 * Temporary horizontal location of the item in the grid during reorder
3160 */
3161 public int tmpCellX;
3162
3163 /**
3164 * Temporary vertical location of the item in the grid during reorder
3165 */
3166 public int tmpCellY;
3167
3168 /**
3169 * Indicates that the temporary coordinates should be used to layout the items
3170 */
3171 public boolean useTmpCoords;
3172
3173 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003174 * Number of cells spanned horizontally by the item.
3175 */
3176 @ViewDebug.ExportedProperty
3177 public int cellHSpan;
3178
3179 /**
3180 * Number of cells spanned vertically by the item.
3181 */
3182 @ViewDebug.ExportedProperty
3183 public int cellVSpan;
Winson Chungaafa03c2010-06-11 17:34:16 -07003184
Adam Cohen1b607ed2011-03-03 17:26:50 -08003185 /**
3186 * Indicates whether the item will set its x, y, width and height parameters freely,
3187 * or whether these will be computed based on cellX, cellY, cellHSpan and cellVSpan.
3188 */
Adam Cohend4844c32011-02-18 19:25:06 -08003189 public boolean isLockedToGrid = true;
3190
Adam Cohen482ed822012-03-02 14:15:13 -08003191 /**
Adam Cohenec40b2b2013-07-23 15:52:40 -07003192 * Indicates that this item should use the full extents of its parent.
3193 */
3194 public boolean isFullscreen = false;
3195
3196 /**
Adam Cohen482ed822012-03-02 14:15:13 -08003197 * Indicates whether this item can be reordered. Always true except in the case of the
3198 * the AllApps button.
3199 */
3200 public boolean canReorder = true;
3201
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003202 // X coordinate of the view in the layout.
3203 @ViewDebug.ExportedProperty
3204 int x;
3205 // Y coordinate of the view in the layout.
3206 @ViewDebug.ExportedProperty
3207 int y;
3208
Romain Guy84f296c2009-11-04 15:00:44 -08003209 boolean dropped;
Romain Guyfcb9e712009-10-02 16:06:52 -07003210
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003211 public LayoutParams(Context c, AttributeSet attrs) {
3212 super(c, attrs);
3213 cellHSpan = 1;
3214 cellVSpan = 1;
3215 }
3216
3217 public LayoutParams(ViewGroup.LayoutParams source) {
3218 super(source);
3219 cellHSpan = 1;
3220 cellVSpan = 1;
3221 }
Winson Chungaafa03c2010-06-11 17:34:16 -07003222
3223 public LayoutParams(LayoutParams source) {
3224 super(source);
3225 this.cellX = source.cellX;
3226 this.cellY = source.cellY;
3227 this.cellHSpan = source.cellHSpan;
3228 this.cellVSpan = source.cellVSpan;
3229 }
3230
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003231 public LayoutParams(int cellX, int cellY, int cellHSpan, int cellVSpan) {
Romain Guy8f19cdd2010-01-08 15:07:00 -08003232 super(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003233 this.cellX = cellX;
3234 this.cellY = cellY;
3235 this.cellHSpan = cellHSpan;
3236 this.cellVSpan = cellVSpan;
3237 }
3238
Adam Cohen2374abf2013-04-16 14:56:57 -07003239 public void setup(int cellWidth, int cellHeight, int widthGap, int heightGap,
3240 boolean invertHorizontally, int colCount) {
Adam Cohend4844c32011-02-18 19:25:06 -08003241 if (isLockedToGrid) {
3242 final int myCellHSpan = cellHSpan;
3243 final int myCellVSpan = cellVSpan;
Adam Cohen2374abf2013-04-16 14:56:57 -07003244 int myCellX = useTmpCoords ? tmpCellX : cellX;
3245 int myCellY = useTmpCoords ? tmpCellY : cellY;
3246
3247 if (invertHorizontally) {
3248 myCellX = colCount - myCellX - cellHSpan;
3249 }
Adam Cohen1b607ed2011-03-03 17:26:50 -08003250
Adam Cohend4844c32011-02-18 19:25:06 -08003251 width = myCellHSpan * cellWidth + ((myCellHSpan - 1) * widthGap) -
3252 leftMargin - rightMargin;
3253 height = myCellVSpan * cellHeight + ((myCellVSpan - 1) * heightGap) -
3254 topMargin - bottomMargin;
Winson Chungeecf02d2012-03-02 17:14:58 -08003255 x = (int) (myCellX * (cellWidth + widthGap) + leftMargin);
3256 y = (int) (myCellY * (cellHeight + heightGap) + topMargin);
Adam Cohend4844c32011-02-18 19:25:06 -08003257 }
3258 }
Winson Chungaafa03c2010-06-11 17:34:16 -07003259
Winson Chungaafa03c2010-06-11 17:34:16 -07003260 public String toString() {
3261 return "(" + this.cellX + ", " + this.cellY + ")";
3262 }
Adam Cohen7f4eabe2011-04-21 16:19:16 -07003263
3264 public void setWidth(int width) {
3265 this.width = width;
3266 }
3267
3268 public int getWidth() {
3269 return width;
3270 }
3271
3272 public void setHeight(int height) {
3273 this.height = height;
3274 }
3275
3276 public int getHeight() {
3277 return height;
3278 }
3279
3280 public void setX(int x) {
3281 this.x = x;
3282 }
3283
3284 public int getX() {
3285 return x;
3286 }
3287
3288 public void setY(int y) {
3289 this.y = y;
3290 }
3291
3292 public int getY() {
3293 return y;
3294 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003295 }
3296
Michael Jurka0280c3b2010-09-17 15:00:07 -07003297 // This class stores info for two purposes:
3298 // 1. When dragging items (mDragInfo in Workspace), we store the View, its cellX & cellY,
3299 // its spanX, spanY, and the screen it is on
3300 // 2. When long clicking on an empty cell in a CellLayout, we save information about the
3301 // cellX and cellY coordinates and which page was clicked. We then set this as a tag on
3302 // the CellLayout that was long clicked
Michael Jurkae5fb0f22011-04-11 13:27:46 -07003303 static final class CellInfo {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003304 View cell;
Michael Jurkaa63c4522010-08-19 13:52:27 -07003305 int cellX = -1;
3306 int cellY = -1;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003307 int spanX;
3308 int spanY;
Adam Cohendcd297f2013-06-18 13:13:40 -07003309 long screenId;
Winson Chung3d503fb2011-07-13 17:25:49 -07003310 long container;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003311
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003312 @Override
3313 public String toString() {
Winson Chungaafa03c2010-06-11 17:34:16 -07003314 return "Cell[view=" + (cell == null ? "null" : cell.getClass())
3315 + ", x=" + cellX + ", y=" + cellY + "]";
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003316 }
3317 }
Michael Jurkad771c962011-08-09 15:00:48 -07003318
3319 public boolean lastDownOnOccupiedCell() {
3320 return mLastDownOnOccupiedCell;
3321 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003322}