blob: 7818da4353f31bb813d054368e7978e1cca1b262 [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
Joe Onoratoa5902522009-07-30 13:37:37 -070017package com.android.launcher2;
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
Adam Cohen66396872011-04-15 17:50:36 -070051import com.android.launcher.R;
Adam Cohen69ce2e52011-07-03 19:25:21 -070052import com.android.launcher2.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
Michael Jurka33945b22010-12-21 18:19:38 -0800109 // If we're actively dragging something over this screen, mIsDragOverlapping is true
110 private boolean mIsDragOverlapping = false;
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700111 private final Point mDragCenter = new Point();
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700112
Winson Chung150fbab2010-09-29 17:14:26 -0700113 // These arrays are used to implement the drag visualization on x-large screens.
Joe Onorato4be866d2010-10-10 11:26:02 -0700114 // They are used as circular arrays, indexed by mDragOutlineCurrent.
Adam Cohend41fbf52012-02-16 23:53:59 -0800115 private Rect[] mDragOutlines = new Rect[4];
Chet Haase472b2812010-10-14 07:02:04 -0700116 private float[] mDragOutlineAlphas = new float[mDragOutlines.length];
Joe Onorato4be866d2010-10-10 11:26:02 -0700117 private InterruptibleInOutAnimator[] mDragOutlineAnims =
118 new InterruptibleInOutAnimator[mDragOutlines.length];
Winson Chung150fbab2010-09-29 17:14:26 -0700119
120 // Used as an index into the above 3 arrays; indicates which is the most current value.
Joe Onorato4be866d2010-10-10 11:26:02 -0700121 private int mDragOutlineCurrent = 0;
Patrick Dubroy8e58e912010-10-14 13:21:48 -0700122 private final Paint mDragOutlinePaint = new Paint();
Winson Chung150fbab2010-09-29 17:14:26 -0700123
Patrick Dubroy96864c32011-03-10 17:17:23 -0800124 private BubbleTextView mPressedOrFocusedIcon;
125
Adam Cohen482ed822012-03-02 14:15:13 -0800126 private HashMap<CellLayout.LayoutParams, Animator> mReorderAnimators = new
127 HashMap<CellLayout.LayoutParams, Animator>();
Adam Cohen19f37922012-03-21 11:59:11 -0700128 private HashMap<View, ReorderHintAnimation>
129 mShakeAnimators = new HashMap<View, ReorderHintAnimation>();
130
131 private boolean mItemPlacementDirty = false;
Adam Cohenbfbfd262011-06-13 16:55:12 -0700132
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700133 // When a drag operation is in progress, holds the nearest cell to the touch point
134 private final int[] mDragCell = new int[2];
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800135
Joe Onorato4be866d2010-10-10 11:26:02 -0700136 private boolean mDragging = false;
137
Patrick Dubroyce34a972010-10-19 10:34:32 -0700138 private TimeInterpolator mEaseOutInterpolator;
Michael Jurkaa52570f2012-03-20 03:18:20 -0700139 private ShortcutAndWidgetContainer mShortcutsAndWidgets;
Patrick Dubroyce34a972010-10-19 10:34:32 -0700140
Andrew Flynn0dca1ec2012-02-29 13:33:22 -0800141 private boolean mIsHotseat = false;
Adam Cohen307fe232012-08-16 17:55:58 -0700142 private float mHotseatScale = 1f;
Andrew Flynn0dca1ec2012-02-29 13:33:22 -0800143
Adam Cohen482ed822012-03-02 14:15:13 -0800144 public static final int MODE_DRAG_OVER = 0;
145 public static final int MODE_ON_DROP = 1;
146 public static final int MODE_ON_DROP_EXTERNAL = 2;
147 public static final int MODE_ACCEPT_DROP = 3;
Adam Cohen19f37922012-03-21 11:59:11 -0700148 private static final boolean DESTRUCTIVE_REORDER = false;
Adam Cohen482ed822012-03-02 14:15:13 -0800149 private static final boolean DEBUG_VISUALIZE_OCCUPIED = false;
150
Adam Cohena897f392012-04-27 18:12:05 -0700151 static final int LANDSCAPE = 0;
152 static final int PORTRAIT = 1;
153
Adam Cohen7bdfc972012-05-22 16:50:35 -0700154 private static final float REORDER_HINT_MAGNITUDE = 0.12f;
Adam Cohen19f37922012-03-21 11:59:11 -0700155 private static final int REORDER_ANIMATION_DURATION = 150;
156 private float mReorderHintAnimationMagnitude;
157
Adam Cohen482ed822012-03-02 14:15:13 -0800158 private ArrayList<View> mIntersectingViews = new ArrayList<View>();
159 private Rect mOccupiedRect = new Rect();
160 private int[] mDirectionVector = new int[2];
Adam Cohen19f37922012-03-21 11:59:11 -0700161 int[] mPreviousReorderDirection = new int[2];
Adam Cohenb209e632012-03-27 17:09:36 -0700162 private static final int INVALID_DIRECTION = -100;
Adam Cohenc6cc61d2012-04-04 12:47:08 -0700163 private DropTarget.DragEnforcer mDragEnforcer;
Adam Cohen482ed822012-03-02 14:15:13 -0800164
Romain Guy8a0bff52012-05-06 13:14:33 -0700165 private final static PorterDuffXfermode sAddBlendMode =
166 new PorterDuffXfermode(PorterDuff.Mode.ADD);
Michael Jurkaca993832012-06-29 15:17:04 -0700167 private final static Paint sPaint = new Paint();
Romain Guy8a0bff52012-05-06 13:14:33 -0700168
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800169 public CellLayout(Context context) {
170 this(context, null);
171 }
172
173 public CellLayout(Context context, AttributeSet attrs) {
174 this(context, attrs, 0);
175 }
176
177 public CellLayout(Context context, AttributeSet attrs, int defStyle) {
178 super(context, attrs, defStyle);
Michael Jurka8b805b12012-04-18 14:23:14 -0700179 mDragEnforcer = new DropTarget.DragEnforcer(context);
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700180
181 // A ViewGroup usually does not draw, but CellLayout needs to draw a rectangle to show
182 // the user where a dragged item will land when dropped.
183 setWillNotDraw(false);
Adam Cohen2acce882012-03-28 19:03:19 -0700184 mLauncher = (Launcher) context;
Michael Jurkaa63c4522010-08-19 13:52:27 -0700185
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800186 TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.CellLayout, defStyle, 0);
187
Adam Cohenf4bd5792012-04-27 11:35:29 -0700188 mCellWidth = a.getDimensionPixelSize(R.styleable.CellLayout_cellWidth, 10);
189 mCellHeight = a.getDimensionPixelSize(R.styleable.CellLayout_cellHeight, 10);
Adam Cohen234c4cd2011-07-17 21:03:04 -0700190 mWidthGap = mOriginalWidthGap = a.getDimensionPixelSize(R.styleable.CellLayout_widthGap, 0);
191 mHeightGap = mOriginalHeightGap = a.getDimensionPixelSize(R.styleable.CellLayout_heightGap, 0);
Winson Chung4b825dcd2011-06-19 12:41:22 -0700192 mMaxGap = a.getDimensionPixelSize(R.styleable.CellLayout_maxGap, 0);
Adam Cohend22015c2010-07-26 22:02:18 -0700193 mCountX = LauncherModel.getCellCountX();
194 mCountY = LauncherModel.getCellCountY();
Michael Jurka0280c3b2010-09-17 15:00:07 -0700195 mOccupied = new boolean[mCountX][mCountY];
Adam Cohen482ed822012-03-02 14:15:13 -0800196 mTmpOccupied = new boolean[mCountX][mCountY];
Adam Cohen5b53f292012-03-29 14:30:35 -0700197 mPreviousReorderDirection[0] = INVALID_DIRECTION;
198 mPreviousReorderDirection[1] = INVALID_DIRECTION;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800199
200 a.recycle();
201
202 setAlwaysDrawnWithCacheEnabled(false);
203
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700204 final Resources res = getResources();
Adam Cohen307fe232012-08-16 17:55:58 -0700205 mHotseatScale = (res.getInteger(R.integer.hotseat_item_scale_percentage) / 100f);
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700206
Winson Chung967289b2011-06-30 18:09:30 -0700207 mNormalBackground = res.getDrawable(R.drawable.homescreen_blue_normal_holo);
Winson Chungdea74b72011-09-13 18:06:43 -0700208 mActiveGlowBackground = res.getDrawable(R.drawable.homescreen_blue_strong_holo);
Michael Jurka33945b22010-12-21 18:19:38 -0800209
Adam Cohenb5ba0972011-09-07 18:02:31 -0700210 mOverScrollLeft = res.getDrawable(R.drawable.overscroll_glow_left);
211 mOverScrollRight = res.getDrawable(R.drawable.overscroll_glow_right);
212 mForegroundPadding =
213 res.getDimensionPixelSize(R.dimen.workspace_overscroll_drawable_padding);
Michael Jurka33945b22010-12-21 18:19:38 -0800214
Adam Cohen19f37922012-03-21 11:59:11 -0700215 mReorderHintAnimationMagnitude = (REORDER_HINT_MAGNITUDE *
216 res.getDimensionPixelSize(R.dimen.app_icon_size));
217
Winson Chungb26f3d62011-06-02 10:49:29 -0700218 mNormalBackground.setFilterBitmap(true);
Winson Chungb26f3d62011-06-02 10:49:29 -0700219 mActiveGlowBackground.setFilterBitmap(true);
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700220
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700221 // Initialize the data structures used for the drag visualization.
Winson Chung150fbab2010-09-29 17:14:26 -0700222
Patrick Dubroyce34a972010-10-19 10:34:32 -0700223 mEaseOutInterpolator = new DecelerateInterpolator(2.5f); // Quint ease out
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700224
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700225
Winson Chungb8c69f32011-10-19 21:36:08 -0700226 mDragCell[0] = mDragCell[1] = -1;
Joe Onorato4be866d2010-10-10 11:26:02 -0700227 for (int i = 0; i < mDragOutlines.length; i++) {
Adam Cohend41fbf52012-02-16 23:53:59 -0800228 mDragOutlines[i] = new Rect(-1, -1, -1, -1);
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700229 }
230
231 // When dragging things around the home screens, we show a green outline of
232 // where the item will land. The outlines gradually fade out, leaving a trail
233 // behind the drag path.
234 // Set up all the animations that are used to implement this fading.
235 final int duration = res.getInteger(R.integer.config_dragOutlineFadeTime);
Chet Haase472b2812010-10-14 07:02:04 -0700236 final float fromAlphaValue = 0;
237 final float toAlphaValue = (float)res.getInteger(R.integer.config_dragOutlineMaxAlpha);
Joe Onorato4be866d2010-10-10 11:26:02 -0700238
Patrick Dubroy8e58e912010-10-14 13:21:48 -0700239 Arrays.fill(mDragOutlineAlphas, fromAlphaValue);
Joe Onorato4be866d2010-10-10 11:26:02 -0700240
241 for (int i = 0; i < mDragOutlineAnims.length; i++) {
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700242 final InterruptibleInOutAnimator anim =
243 new InterruptibleInOutAnimator(duration, fromAlphaValue, toAlphaValue);
Patrick Dubroyce34a972010-10-19 10:34:32 -0700244 anim.getAnimator().setInterpolator(mEaseOutInterpolator);
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700245 final int thisIndex = i;
Chet Haase472b2812010-10-14 07:02:04 -0700246 anim.getAnimator().addUpdateListener(new AnimatorUpdateListener() {
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700247 public void onAnimationUpdate(ValueAnimator animation) {
Joe Onorato4be866d2010-10-10 11:26:02 -0700248 final Bitmap outline = (Bitmap)anim.getTag();
249
250 // If an animation is started and then stopped very quickly, we can still
251 // get spurious updates we've cleared the tag. Guard against this.
252 if (outline == null) {
Michael Jurka3a9fced2012-04-13 14:44:29 -0700253 @SuppressWarnings("all") // suppress dead code warning
254 final boolean debug = false;
255 if (debug) {
Patrick Dubroyfe6bd872010-10-13 17:32:10 -0700256 Object val = animation.getAnimatedValue();
257 Log.d(TAG, "anim " + thisIndex + " update: " + val +
258 ", isStopped " + anim.isStopped());
259 }
Joe Onorato4be866d2010-10-10 11:26:02 -0700260 // Try to prevent it from continuing to run
261 animation.cancel();
262 } else {
Chet Haase472b2812010-10-14 07:02:04 -0700263 mDragOutlineAlphas[thisIndex] = (Float) animation.getAnimatedValue();
Adam Cohend41fbf52012-02-16 23:53:59 -0800264 CellLayout.this.invalidate(mDragOutlines[thisIndex]);
Joe Onorato4be866d2010-10-10 11:26:02 -0700265 }
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700266 }
267 });
Joe Onorato4be866d2010-10-10 11:26:02 -0700268 // The animation holds a reference to the drag outline bitmap as long is it's
269 // running. This way the bitmap can be GCed when the animations are complete.
Chet Haase472b2812010-10-14 07:02:04 -0700270 anim.getAnimator().addListener(new AnimatorListenerAdapter() {
Michael Jurka3c4c20f2010-10-28 15:36:06 -0700271 @Override
Joe Onorato4be866d2010-10-10 11:26:02 -0700272 public void onAnimationEnd(Animator animation) {
Chet Haase472b2812010-10-14 07:02:04 -0700273 if ((Float) ((ValueAnimator) animation).getAnimatedValue() == 0f) {
Joe Onorato4be866d2010-10-10 11:26:02 -0700274 anim.setTag(null);
275 }
276 }
277 });
278 mDragOutlineAnims[i] = anim;
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700279 }
Patrick Dubroyce34a972010-10-19 10:34:32 -0700280
Michael Jurka18014792010-10-14 09:01:34 -0700281 mBackgroundRect = new Rect();
Adam Cohenb5ba0972011-09-07 18:02:31 -0700282 mForegroundRect = new Rect();
Michael Jurkabea15192010-11-17 12:33:46 -0800283
Michael Jurkaa52570f2012-03-20 03:18:20 -0700284 mShortcutsAndWidgets = new ShortcutAndWidgetContainer(context);
285 mShortcutsAndWidgets.setCellDimensions(mCellWidth, mCellHeight, mWidthGap, mHeightGap);
286 addView(mShortcutsAndWidgets);
Michael Jurka18014792010-10-14 09:01:34 -0700287 }
288
Michael Jurkaf6440da2011-04-05 14:50:34 -0700289 static int widthInPortrait(Resources r, int numCells) {
290 // We use this method from Workspace to figure out how many rows/columns Launcher should
291 // have. We ignore the left/right padding on CellLayout because it turns out in our design
292 // the padding extends outside the visible screen size, but it looked fine anyway.
Michael Jurkaf6440da2011-04-05 14:50:34 -0700293 int cellWidth = r.getDimensionPixelSize(R.dimen.workspace_cell_width);
Winson Chung4b825dcd2011-06-19 12:41:22 -0700294 int minGap = Math.min(r.getDimensionPixelSize(R.dimen.workspace_width_gap),
295 r.getDimensionPixelSize(R.dimen.workspace_height_gap));
Michael Jurkaf6440da2011-04-05 14:50:34 -0700296
Winson Chung4b825dcd2011-06-19 12:41:22 -0700297 return minGap * (numCells - 1) + cellWidth * numCells;
Michael Jurkaf6440da2011-04-05 14:50:34 -0700298 }
299
Michael Jurkaf6440da2011-04-05 14:50:34 -0700300 static int heightInLandscape(Resources r, int numCells) {
301 // We use this method from Workspace to figure out how many rows/columns Launcher should
302 // have. We ignore the left/right padding on CellLayout because it turns out in our design
303 // the padding extends outside the visible screen size, but it looked fine anyway.
Michael Jurkaf6440da2011-04-05 14:50:34 -0700304 int cellHeight = r.getDimensionPixelSize(R.dimen.workspace_cell_height);
Winson Chung4b825dcd2011-06-19 12:41:22 -0700305 int minGap = Math.min(r.getDimensionPixelSize(R.dimen.workspace_width_gap),
306 r.getDimensionPixelSize(R.dimen.workspace_height_gap));
Michael Jurkaf6440da2011-04-05 14:50:34 -0700307
Winson Chung4b825dcd2011-06-19 12:41:22 -0700308 return minGap * (numCells - 1) + cellHeight * numCells;
Michael Jurkaf6440da2011-04-05 14:50:34 -0700309 }
310
Adam Cohen2801caf2011-05-13 20:57:39 -0700311 public void enableHardwareLayers() {
Michael Jurkaca993832012-06-29 15:17:04 -0700312 mShortcutsAndWidgets.setLayerType(LAYER_TYPE_HARDWARE, sPaint);
Michael Jurkad51f33a2012-06-28 15:35:26 -0700313 }
314
315 public void disableHardwareLayers() {
Michael Jurkaca993832012-06-29 15:17:04 -0700316 mShortcutsAndWidgets.setLayerType(LAYER_TYPE_NONE, sPaint);
Michael Jurkad51f33a2012-06-28 15:35:26 -0700317 }
318
319 public void buildHardwareLayer() {
320 mShortcutsAndWidgets.buildLayer();
Adam Cohen2801caf2011-05-13 20:57:39 -0700321 }
322
Adam Cohen307fe232012-08-16 17:55:58 -0700323 public float getChildrenScale() {
324 return mIsHotseat ? mHotseatScale : 1.0f;
325 }
326
Adam Cohen2801caf2011-05-13 20:57:39 -0700327 public void setGridSize(int x, int y) {
328 mCountX = x;
329 mCountY = y;
330 mOccupied = new boolean[mCountX][mCountY];
Adam Cohen482ed822012-03-02 14:15:13 -0800331 mTmpOccupied = new boolean[mCountX][mCountY];
Adam Cohen7fbec102012-03-27 12:42:19 -0700332 mTempRectStack.clear();
Adam Cohen76fc0852011-06-17 13:26:23 -0700333 requestLayout();
Adam Cohen2801caf2011-05-13 20:57:39 -0700334 }
335
Patrick Dubroy96864c32011-03-10 17:17:23 -0800336 private void invalidateBubbleTextView(BubbleTextView icon) {
337 final int padding = icon.getPressedOrFocusedBackgroundPadding();
Winson Chung4b825dcd2011-06-19 12:41:22 -0700338 invalidate(icon.getLeft() + getPaddingLeft() - padding,
339 icon.getTop() + getPaddingTop() - padding,
340 icon.getRight() + getPaddingLeft() + padding,
341 icon.getBottom() + getPaddingTop() + padding);
Patrick Dubroy96864c32011-03-10 17:17:23 -0800342 }
343
Adam Cohenb5ba0972011-09-07 18:02:31 -0700344 void setOverScrollAmount(float r, boolean left) {
345 if (left && mOverScrollForegroundDrawable != mOverScrollLeft) {
346 mOverScrollForegroundDrawable = mOverScrollLeft;
347 } else if (!left && mOverScrollForegroundDrawable != mOverScrollRight) {
348 mOverScrollForegroundDrawable = mOverScrollRight;
349 }
350
351 mForegroundAlpha = (int) Math.round((r * 255));
352 mOverScrollForegroundDrawable.setAlpha(mForegroundAlpha);
353 invalidate();
354 }
355
Patrick Dubroy96864c32011-03-10 17:17:23 -0800356 void setPressedOrFocusedIcon(BubbleTextView icon) {
357 // We draw the pressed or focused BubbleTextView's background in CellLayout because it
358 // requires an expanded clip rect (due to the glow's blur radius)
359 BubbleTextView oldIcon = mPressedOrFocusedIcon;
360 mPressedOrFocusedIcon = icon;
361 if (oldIcon != null) {
362 invalidateBubbleTextView(oldIcon);
363 }
364 if (mPressedOrFocusedIcon != null) {
365 invalidateBubbleTextView(mPressedOrFocusedIcon);
366 }
367 }
368
Michael Jurka33945b22010-12-21 18:19:38 -0800369 void setIsDragOverlapping(boolean isDragOverlapping) {
370 if (mIsDragOverlapping != isDragOverlapping) {
371 mIsDragOverlapping = isDragOverlapping;
372 invalidate();
373 }
374 }
375
376 boolean getIsDragOverlapping() {
377 return mIsDragOverlapping;
378 }
379
Adam Cohenebea84d2011-11-09 17:20:41 -0800380 protected void setOverscrollTransformsDirty(boolean dirty) {
381 mScrollingTransformsDirty = dirty;
382 }
383
384 protected void resetOverscrollTransforms() {
385 if (mScrollingTransformsDirty) {
386 setOverscrollTransformsDirty(false);
387 setTranslationX(0);
388 setRotationY(0);
389 // It doesn't matter if we pass true or false here, the important thing is that we
390 // pass 0, which results in the overscroll drawable not being drawn any more.
391 setOverScrollAmount(0, false);
392 setPivotX(getMeasuredWidth() / 2);
393 setPivotY(getMeasuredHeight() / 2);
394 }
395 }
396
Adam Cohen307fe232012-08-16 17:55:58 -0700397 public void scaleRect(Rect r, float scale) {
398 if (scale != 1.0f) {
399 r.left = (int) (r.left * scale + 0.5f);
400 r.top = (int) (r.top * scale + 0.5f);
401 r.right = (int) (r.right * scale + 0.5f);
402 r.bottom = (int) (r.bottom * scale + 0.5f);
403 }
404 }
405
406 Rect temp = new Rect();
407 void scaleRectAboutCenter(Rect in, Rect out, float scale) {
408 int cx = in.centerX();
409 int cy = in.centerY();
410 out.set(in);
411 out.offset(-cx, -cy);
412 scaleRect(out, scale);
413 out.offset(cx, cy);
414 }
415
Jeff Sharkey83f111d2009-04-20 21:03:13 -0700416 @Override
Patrick Dubroy1262e362010-10-06 15:49:50 -0700417 protected void onDraw(Canvas canvas) {
Michael Jurka3e7c7632010-10-02 16:01:03 -0700418 // When we're large, we are either drawn in a "hover" state (ie when dragging an item to
419 // a neighboring page) or with just a normal background (if backgroundAlpha > 0.0f)
420 // When we're small, we are either drawn normally or in the "accepts drops" state (during
421 // a drag). However, we also drag the mini hover background *over* one of those two
422 // backgrounds
Winson Chungb26f3d62011-06-02 10:49:29 -0700423 if (mBackgroundAlpha > 0.0f) {
Adam Cohenf34bab52010-09-30 14:11:56 -0700424 Drawable bg;
Michael Jurka33945b22010-12-21 18:19:38 -0800425
426 if (mIsDragOverlapping) {
427 // In the mini case, we draw the active_glow bg *over* the active background
Michael Jurkabdf78552011-10-31 14:34:25 -0700428 bg = mActiveGlowBackground;
Adam Cohenf34bab52010-09-30 14:11:56 -0700429 } else {
Michael Jurkabdf78552011-10-31 14:34:25 -0700430 bg = mNormalBackground;
Adam Cohenf34bab52010-09-30 14:11:56 -0700431 }
Michael Jurka33945b22010-12-21 18:19:38 -0800432
433 bg.setAlpha((int) (mBackgroundAlpha * mBackgroundAlphaMultiplier * 255));
434 bg.setBounds(mBackgroundRect);
435 bg.draw(canvas);
Michael Jurkaa63c4522010-08-19 13:52:27 -0700436 }
Romain Guya6abce82009-11-10 02:54:41 -0800437
Patrick Dubroy8e58e912010-10-14 13:21:48 -0700438 final Paint paint = mDragOutlinePaint;
Joe Onorato4be866d2010-10-10 11:26:02 -0700439 for (int i = 0; i < mDragOutlines.length; i++) {
Chet Haase472b2812010-10-14 07:02:04 -0700440 final float alpha = mDragOutlineAlphas[i];
Joe Onorato4be866d2010-10-10 11:26:02 -0700441 if (alpha > 0) {
Adam Cohend41fbf52012-02-16 23:53:59 -0800442 final Rect r = mDragOutlines[i];
Adam Cohen307fe232012-08-16 17:55:58 -0700443 scaleRectAboutCenter(r, temp, getChildrenScale());
Joe Onorato4be866d2010-10-10 11:26:02 -0700444 final Bitmap b = (Bitmap) mDragOutlineAnims[i].getTag();
Chet Haase472b2812010-10-14 07:02:04 -0700445 paint.setAlpha((int)(alpha + .5f));
Adam Cohen307fe232012-08-16 17:55:58 -0700446 canvas.drawBitmap(b, null, temp, paint);
Winson Chung150fbab2010-09-29 17:14:26 -0700447 }
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700448 }
Patrick Dubroy96864c32011-03-10 17:17:23 -0800449
450 // We draw the pressed or focused BubbleTextView's background in CellLayout because it
451 // requires an expanded clip rect (due to the glow's blur radius)
452 if (mPressedOrFocusedIcon != null) {
453 final int padding = mPressedOrFocusedIcon.getPressedOrFocusedBackgroundPadding();
454 final Bitmap b = mPressedOrFocusedIcon.getPressedOrFocusedBackground();
455 if (b != null) {
456 canvas.drawBitmap(b,
Winson Chung4b825dcd2011-06-19 12:41:22 -0700457 mPressedOrFocusedIcon.getLeft() + getPaddingLeft() - padding,
458 mPressedOrFocusedIcon.getTop() + getPaddingTop() - padding,
Patrick Dubroy96864c32011-03-10 17:17:23 -0800459 null);
460 }
461 }
Adam Cohen69ce2e52011-07-03 19:25:21 -0700462
Adam Cohen482ed822012-03-02 14:15:13 -0800463 if (DEBUG_VISUALIZE_OCCUPIED) {
464 int[] pt = new int[2];
465 ColorDrawable cd = new ColorDrawable(Color.RED);
Adam Cohene7587d22012-05-24 18:50:02 -0700466 cd.setBounds(0, 0, mCellWidth, mCellHeight);
Adam Cohen482ed822012-03-02 14:15:13 -0800467 for (int i = 0; i < mCountX; i++) {
468 for (int j = 0; j < mCountY; j++) {
469 if (mOccupied[i][j]) {
470 cellToPoint(i, j, pt);
471 canvas.save();
472 canvas.translate(pt[0], pt[1]);
473 cd.draw(canvas);
474 canvas.restore();
475 }
476 }
477 }
478 }
479
Andrew Flynn850d2e72012-04-26 16:51:20 -0700480 int previewOffset = FolderRingAnimator.sPreviewSize;
481
Adam Cohen69ce2e52011-07-03 19:25:21 -0700482 // The folder outer / inner ring image(s)
483 for (int i = 0; i < mFolderOuterRings.size(); i++) {
484 FolderRingAnimator fra = mFolderOuterRings.get(i);
485
486 // Draw outer ring
487 Drawable d = FolderRingAnimator.sSharedOuterRingDrawable;
488 int width = (int) fra.getOuterRingSize();
489 int height = width;
490 cellToPoint(fra.mCellX, fra.mCellY, mTempLocation);
491
492 int centerX = mTempLocation[0] + mCellWidth / 2;
Andrew Flynn850d2e72012-04-26 16:51:20 -0700493 int centerY = mTempLocation[1] + previewOffset / 2;
Adam Cohen69ce2e52011-07-03 19:25:21 -0700494
495 canvas.save();
496 canvas.translate(centerX - width / 2, centerY - height / 2);
497 d.setBounds(0, 0, width, height);
498 d.draw(canvas);
499 canvas.restore();
500
501 // Draw inner ring
502 d = FolderRingAnimator.sSharedInnerRingDrawable;
503 width = (int) fra.getInnerRingSize();
504 height = width;
505 cellToPoint(fra.mCellX, fra.mCellY, mTempLocation);
506
507 centerX = mTempLocation[0] + mCellWidth / 2;
Andrew Flynn850d2e72012-04-26 16:51:20 -0700508 centerY = mTempLocation[1] + previewOffset / 2;
Adam Cohen69ce2e52011-07-03 19:25:21 -0700509 canvas.save();
510 canvas.translate(centerX - width / 2, centerY - width / 2);
511 d.setBounds(0, 0, width, height);
512 d.draw(canvas);
513 canvas.restore();
514 }
Adam Cohenc51934b2011-07-26 21:07:43 -0700515
516 if (mFolderLeaveBehindCell[0] >= 0 && mFolderLeaveBehindCell[1] >= 0) {
517 Drawable d = FolderIcon.sSharedFolderLeaveBehind;
518 int width = d.getIntrinsicWidth();
519 int height = d.getIntrinsicHeight();
520
521 cellToPoint(mFolderLeaveBehindCell[0], mFolderLeaveBehindCell[1], mTempLocation);
522 int centerX = mTempLocation[0] + mCellWidth / 2;
Andrew Flynn850d2e72012-04-26 16:51:20 -0700523 int centerY = mTempLocation[1] + previewOffset / 2;
Adam Cohenc51934b2011-07-26 21:07:43 -0700524
525 canvas.save();
526 canvas.translate(centerX - width / 2, centerY - width / 2);
527 d.setBounds(0, 0, width, height);
528 d.draw(canvas);
529 canvas.restore();
530 }
Adam Cohen69ce2e52011-07-03 19:25:21 -0700531 }
532
Adam Cohenb5ba0972011-09-07 18:02:31 -0700533 @Override
534 protected void dispatchDraw(Canvas canvas) {
535 super.dispatchDraw(canvas);
536 if (mForegroundAlpha > 0) {
537 mOverScrollForegroundDrawable.setBounds(mForegroundRect);
538 Paint p = ((NinePatchDrawable) mOverScrollForegroundDrawable).getPaint();
Romain Guy8a0bff52012-05-06 13:14:33 -0700539 p.setXfermode(sAddBlendMode);
Adam Cohenb5ba0972011-09-07 18:02:31 -0700540 mOverScrollForegroundDrawable.draw(canvas);
541 p.setXfermode(null);
542 }
543 }
544
Adam Cohen69ce2e52011-07-03 19:25:21 -0700545 public void showFolderAccept(FolderRingAnimator fra) {
546 mFolderOuterRings.add(fra);
547 }
548
549 public void hideFolderAccept(FolderRingAnimator fra) {
550 if (mFolderOuterRings.contains(fra)) {
551 mFolderOuterRings.remove(fra);
552 }
553 invalidate();
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700554 }
555
Adam Cohenc51934b2011-07-26 21:07:43 -0700556 public void setFolderLeaveBehindCell(int x, int y) {
557 mFolderLeaveBehindCell[0] = x;
558 mFolderLeaveBehindCell[1] = y;
559 invalidate();
560 }
561
562 public void clearFolderLeaveBehind() {
563 mFolderLeaveBehindCell[0] = -1;
564 mFolderLeaveBehindCell[1] = -1;
565 invalidate();
566 }
567
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700568 @Override
Michael Jurkae6235dd2011-10-04 15:02:05 -0700569 public boolean shouldDelayChildPressedState() {
570 return false;
571 }
572
Adam Cohen1462de32012-07-24 22:34:36 -0700573 public void restoreInstanceState(SparseArray<Parcelable> states) {
574 dispatchRestoreInstanceState(states);
575 }
576
Michael Jurkae6235dd2011-10-04 15:02:05 -0700577 @Override
Jeff Sharkey83f111d2009-04-20 21:03:13 -0700578 public void cancelLongPress() {
579 super.cancelLongPress();
580
581 // Cancel long press for all children
582 final int count = getChildCount();
583 for (int i = 0; i < count; i++) {
584 final View child = getChildAt(i);
585 child.cancelLongPress();
586 }
587 }
588
Michael Jurkadee05892010-07-27 10:01:56 -0700589 public void setOnInterceptTouchListener(View.OnTouchListener listener) {
590 mInterceptTouchListener = listener;
591 }
592
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800593 int getCountX() {
Adam Cohend22015c2010-07-26 22:02:18 -0700594 return mCountX;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800595 }
596
597 int getCountY() {
Adam Cohend22015c2010-07-26 22:02:18 -0700598 return mCountY;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800599 }
600
Andrew Flynn0dca1ec2012-02-29 13:33:22 -0800601 public void setIsHotseat(boolean isHotseat) {
602 mIsHotseat = isHotseat;
603 }
604
Andrew Flynn0dca1ec2012-02-29 13:33:22 -0800605 public boolean addViewToCellLayout(View child, int index, int childId, LayoutParams params,
Andrew Flynn850d2e72012-04-26 16:51:20 -0700606 boolean markCells) {
Winson Chungaafa03c2010-06-11 17:34:16 -0700607 final LayoutParams lp = params;
608
Andrew Flynnde38e422012-05-08 11:22:15 -0700609 // Hotseat icons - remove text
Andrew Flynn0dca1ec2012-02-29 13:33:22 -0800610 if (child instanceof BubbleTextView) {
611 BubbleTextView bubbleChild = (BubbleTextView) child;
612
Andrew Flynnde38e422012-05-08 11:22:15 -0700613 Resources res = getResources();
614 if (mIsHotseat) {
615 bubbleChild.setTextColor(res.getColor(android.R.color.transparent));
616 } else {
617 bubbleChild.setTextColor(res.getColor(R.color.workspace_icon_text_color));
Andrew Flynn0dca1ec2012-02-29 13:33:22 -0800618 }
619 }
620
Adam Cohen307fe232012-08-16 17:55:58 -0700621 child.setScaleX(getChildrenScale());
622 child.setScaleY(getChildrenScale());
623
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800624 // Generate an id for each view, this assumes we have at most 256x256 cells
625 // per workspace screen
Adam Cohend22015c2010-07-26 22:02:18 -0700626 if (lp.cellX >= 0 && lp.cellX <= mCountX - 1 && lp.cellY >= 0 && lp.cellY <= mCountY - 1) {
Winson Chungaafa03c2010-06-11 17:34:16 -0700627 // If the horizontal or vertical span is set to -1, it is taken to
628 // mean that it spans the extent of the CellLayout
Adam Cohend22015c2010-07-26 22:02:18 -0700629 if (lp.cellHSpan < 0) lp.cellHSpan = mCountX;
630 if (lp.cellVSpan < 0) lp.cellVSpan = mCountY;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800631
Winson Chungaafa03c2010-06-11 17:34:16 -0700632 child.setId(childId);
633
Michael Jurkaa52570f2012-03-20 03:18:20 -0700634 mShortcutsAndWidgets.addView(child, index, lp);
Michael Jurkadee05892010-07-27 10:01:56 -0700635
Michael Jurkaf3ca3ab2010-10-20 17:08:24 -0700636 if (markCells) markCellsAsOccupiedForView(child);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700637
Winson Chungaafa03c2010-06-11 17:34:16 -0700638 return true;
639 }
640 return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800641 }
Michael Jurka3e7c7632010-10-02 16:01:03 -0700642
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800643 @Override
Michael Jurka0280c3b2010-09-17 15:00:07 -0700644 public void removeAllViews() {
645 clearOccupiedCells();
Michael Jurkaa52570f2012-03-20 03:18:20 -0700646 mShortcutsAndWidgets.removeAllViews();
Michael Jurka0280c3b2010-09-17 15:00:07 -0700647 }
648
649 @Override
650 public void removeAllViewsInLayout() {
Michael Jurkaa52570f2012-03-20 03:18:20 -0700651 if (mShortcutsAndWidgets.getChildCount() > 0) {
Michael Jurka7cfc2822011-08-02 20:19:24 -0700652 clearOccupiedCells();
Michael Jurkaa52570f2012-03-20 03:18:20 -0700653 mShortcutsAndWidgets.removeAllViewsInLayout();
Michael Jurka7cfc2822011-08-02 20:19:24 -0700654 }
Michael Jurka0280c3b2010-09-17 15:00:07 -0700655 }
656
Michael Jurkaf3ca3ab2010-10-20 17:08:24 -0700657 public void removeViewWithoutMarkingCells(View view) {
Michael Jurkaa52570f2012-03-20 03:18:20 -0700658 mShortcutsAndWidgets.removeView(view);
Michael Jurkaf3ca3ab2010-10-20 17:08:24 -0700659 }
660
Michael Jurka0280c3b2010-09-17 15:00:07 -0700661 @Override
662 public void removeView(View view) {
663 markCellsAsUnoccupiedForView(view);
Michael Jurkaa52570f2012-03-20 03:18:20 -0700664 mShortcutsAndWidgets.removeView(view);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700665 }
666
667 @Override
668 public void removeViewAt(int index) {
Michael Jurkaa52570f2012-03-20 03:18:20 -0700669 markCellsAsUnoccupiedForView(mShortcutsAndWidgets.getChildAt(index));
670 mShortcutsAndWidgets.removeViewAt(index);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700671 }
672
673 @Override
674 public void removeViewInLayout(View view) {
675 markCellsAsUnoccupiedForView(view);
Michael Jurkaa52570f2012-03-20 03:18:20 -0700676 mShortcutsAndWidgets.removeViewInLayout(view);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700677 }
678
679 @Override
680 public void removeViews(int start, int count) {
681 for (int i = start; i < start + count; i++) {
Michael Jurkaa52570f2012-03-20 03:18:20 -0700682 markCellsAsUnoccupiedForView(mShortcutsAndWidgets.getChildAt(i));
Michael Jurka0280c3b2010-09-17 15:00:07 -0700683 }
Michael Jurkaa52570f2012-03-20 03:18:20 -0700684 mShortcutsAndWidgets.removeViews(start, count);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700685 }
686
687 @Override
688 public void removeViewsInLayout(int start, int count) {
689 for (int i = start; i < start + count; i++) {
Michael Jurkaa52570f2012-03-20 03:18:20 -0700690 markCellsAsUnoccupiedForView(mShortcutsAndWidgets.getChildAt(i));
Michael Jurka0280c3b2010-09-17 15:00:07 -0700691 }
Michael Jurkaa52570f2012-03-20 03:18:20 -0700692 mShortcutsAndWidgets.removeViewsInLayout(start, count);
Michael Jurkaabded662011-03-04 12:06:57 -0800693 }
694
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800695 @Override
696 protected void onAttachedToWindow() {
697 super.onAttachedToWindow();
698 mCellInfo.screen = ((ViewGroup) getParent()).indexOfChild(this);
699 }
700
Michael Jurkaaf442092010-06-10 17:01:57 -0700701 public void setTagToCellInfoForPoint(int touchX, int touchY) {
702 final CellInfo cellInfo = mCellInfo;
Winson Chungeecf02d2012-03-02 17:14:58 -0800703 Rect frame = mRect;
Michael Jurka8b805b12012-04-18 14:23:14 -0700704 final int x = touchX + getScrollX();
705 final int y = touchY + getScrollY();
Michael Jurkaa52570f2012-03-20 03:18:20 -0700706 final int count = mShortcutsAndWidgets.getChildCount();
Michael Jurkaaf442092010-06-10 17:01:57 -0700707
708 boolean found = false;
709 for (int i = count - 1; i >= 0; i--) {
Michael Jurkaa52570f2012-03-20 03:18:20 -0700710 final View child = mShortcutsAndWidgets.getChildAt(i);
Adam Cohend4844c32011-02-18 19:25:06 -0800711 final LayoutParams lp = (LayoutParams) child.getLayoutParams();
Michael Jurkaaf442092010-06-10 17:01:57 -0700712
Adam Cohen1b607ed2011-03-03 17:26:50 -0800713 if ((child.getVisibility() == VISIBLE || child.getAnimation() != null) &&
714 lp.isLockedToGrid) {
Michael Jurkaaf442092010-06-10 17:01:57 -0700715 child.getHitRect(frame);
Winson Chung0be025d2011-05-23 17:45:09 -0700716
Winson Chungeecf02d2012-03-02 17:14:58 -0800717 float scale = child.getScaleX();
718 frame = new Rect(child.getLeft(), child.getTop(), child.getRight(),
719 child.getBottom());
Winson Chung0be025d2011-05-23 17:45:09 -0700720 // The child hit rect is relative to the CellLayoutChildren parent, so we need to
721 // offset that by this CellLayout's padding to test an (x,y) point that is relative
722 // to this view.
Michael Jurka8b805b12012-04-18 14:23:14 -0700723 frame.offset(getPaddingLeft(), getPaddingTop());
Winson Chungeecf02d2012-03-02 17:14:58 -0800724 frame.inset((int) (frame.width() * (1f - scale) / 2),
725 (int) (frame.height() * (1f - scale) / 2));
Winson Chung0be025d2011-05-23 17:45:09 -0700726
Michael Jurkaaf442092010-06-10 17:01:57 -0700727 if (frame.contains(x, y)) {
Michael Jurkaaf442092010-06-10 17:01:57 -0700728 cellInfo.cell = child;
729 cellInfo.cellX = lp.cellX;
730 cellInfo.cellY = lp.cellY;
731 cellInfo.spanX = lp.cellHSpan;
732 cellInfo.spanY = lp.cellVSpan;
Michael Jurkaaf442092010-06-10 17:01:57 -0700733 found = true;
Michael Jurkaaf442092010-06-10 17:01:57 -0700734 break;
735 }
736 }
737 }
Winson Chungaafa03c2010-06-11 17:34:16 -0700738
Michael Jurkad771c962011-08-09 15:00:48 -0700739 mLastDownOnOccupiedCell = found;
740
Michael Jurkaaf442092010-06-10 17:01:57 -0700741 if (!found) {
Winson Chung0be025d2011-05-23 17:45:09 -0700742 final int cellXY[] = mTmpXY;
Michael Jurkaaf442092010-06-10 17:01:57 -0700743 pointToCellExact(x, y, cellXY);
744
Michael Jurkaaf442092010-06-10 17:01:57 -0700745 cellInfo.cell = null;
746 cellInfo.cellX = cellXY[0];
747 cellInfo.cellY = cellXY[1];
748 cellInfo.spanX = 1;
749 cellInfo.spanY = 1;
Michael Jurkaaf442092010-06-10 17:01:57 -0700750 }
751 setTag(cellInfo);
752 }
753
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800754 @Override
755 public boolean onInterceptTouchEvent(MotionEvent ev) {
Adam Cohenc1997fd2011-08-15 18:26:39 -0700756 // First we clear the tag to ensure that on every touch down we start with a fresh slate,
757 // even in the case where we return early. Not clearing here was causing bugs whereby on
758 // long-press we'd end up picking up an item from a previous drag operation.
759 final int action = ev.getAction();
760
761 if (action == MotionEvent.ACTION_DOWN) {
762 clearTagCellInfo();
763 }
764
Michael Jurkadee05892010-07-27 10:01:56 -0700765 if (mInterceptTouchListener != null && mInterceptTouchListener.onTouch(this, ev)) {
766 return true;
767 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800768
769 if (action == MotionEvent.ACTION_DOWN) {
Michael Jurkaaf442092010-06-10 17:01:57 -0700770 setTagToCellInfoForPoint((int) ev.getX(), (int) ev.getY());
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800771 }
Winson Chungeecf02d2012-03-02 17:14:58 -0800772
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800773 return false;
774 }
775
Adam Cohenc1997fd2011-08-15 18:26:39 -0700776 private void clearTagCellInfo() {
777 final CellInfo cellInfo = mCellInfo;
778 cellInfo.cell = null;
779 cellInfo.cellX = -1;
780 cellInfo.cellY = -1;
781 cellInfo.spanX = 0;
782 cellInfo.spanY = 0;
783 setTag(cellInfo);
784 }
785
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800786 public CellInfo getTag() {
Michael Jurka0280c3b2010-09-17 15:00:07 -0700787 return (CellInfo) super.getTag();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800788 }
789
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700790 /**
Winson Chungaafa03c2010-06-11 17:34:16 -0700791 * Given a point, return the cell that strictly encloses that point
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800792 * @param x X coordinate of the point
793 * @param y Y coordinate of the point
794 * @param result Array of 2 ints to hold the x and y coordinate of the cell
795 */
796 void pointToCellExact(int x, int y, int[] result) {
Winson Chung4b825dcd2011-06-19 12:41:22 -0700797 final int hStartPadding = getPaddingLeft();
798 final int vStartPadding = getPaddingTop();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800799
800 result[0] = (x - hStartPadding) / (mCellWidth + mWidthGap);
801 result[1] = (y - vStartPadding) / (mCellHeight + mHeightGap);
802
Adam Cohend22015c2010-07-26 22:02:18 -0700803 final int xAxis = mCountX;
804 final int yAxis = mCountY;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800805
806 if (result[0] < 0) result[0] = 0;
807 if (result[0] >= xAxis) result[0] = xAxis - 1;
808 if (result[1] < 0) result[1] = 0;
809 if (result[1] >= yAxis) result[1] = yAxis - 1;
810 }
Winson Chungaafa03c2010-06-11 17:34:16 -0700811
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800812 /**
813 * Given a point, return the cell that most closely encloses that point
814 * @param x X coordinate of the point
815 * @param y Y coordinate of the point
816 * @param result Array of 2 ints to hold the x and y coordinate of the cell
817 */
818 void pointToCellRounded(int x, int y, int[] result) {
819 pointToCellExact(x + (mCellWidth / 2), y + (mCellHeight / 2), result);
820 }
821
822 /**
823 * Given a cell coordinate, return the point that represents the upper left corner of that cell
Winson Chungaafa03c2010-06-11 17:34:16 -0700824 *
825 * @param cellX X coordinate of the cell
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800826 * @param cellY Y coordinate of the cell
Winson Chungaafa03c2010-06-11 17:34:16 -0700827 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800828 * @param result Array of 2 ints to hold the x and y coordinate of the point
829 */
830 void cellToPoint(int cellX, int cellY, int[] result) {
Winson Chung4b825dcd2011-06-19 12:41:22 -0700831 final int hStartPadding = getPaddingLeft();
832 final int vStartPadding = getPaddingTop();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800833
834 result[0] = hStartPadding + cellX * (mCellWidth + mWidthGap);
835 result[1] = vStartPadding + cellY * (mCellHeight + mHeightGap);
836 }
837
Adam Cohene3e27a82011-04-15 12:07:39 -0700838 /**
Adam Cohen482ed822012-03-02 14:15:13 -0800839 * Given a cell coordinate, return the point that represents the center of the cell
Adam Cohene3e27a82011-04-15 12:07:39 -0700840 *
841 * @param cellX X coordinate of the cell
842 * @param cellY Y coordinate of the cell
843 *
844 * @param result Array of 2 ints to hold the x and y coordinate of the point
845 */
846 void cellToCenterPoint(int cellX, int cellY, int[] result) {
Adam Cohen47a876d2012-03-19 13:21:41 -0700847 regionToCenterPoint(cellX, cellY, 1, 1, result);
848 }
849
850 /**
851 * Given a cell coordinate and span return the point that represents the center of the regio
852 *
853 * @param cellX X coordinate of the cell
854 * @param cellY Y coordinate of the cell
855 *
856 * @param result Array of 2 ints to hold the x and y coordinate of the point
857 */
858 void regionToCenterPoint(int cellX, int cellY, int spanX, int spanY, int[] result) {
Winson Chung4b825dcd2011-06-19 12:41:22 -0700859 final int hStartPadding = getPaddingLeft();
860 final int vStartPadding = getPaddingTop();
Adam Cohen47a876d2012-03-19 13:21:41 -0700861 result[0] = hStartPadding + cellX * (mCellWidth + mWidthGap) +
862 (spanX * mCellWidth + (spanX - 1) * mWidthGap) / 2;
863 result[1] = vStartPadding + cellY * (mCellHeight + mHeightGap) +
864 (spanY * mCellHeight + (spanY - 1) * mHeightGap) / 2;
Adam Cohene3e27a82011-04-15 12:07:39 -0700865 }
866
Adam Cohen19f37922012-03-21 11:59:11 -0700867 /**
868 * Given a cell coordinate and span fills out a corresponding pixel rect
869 *
870 * @param cellX X coordinate of the cell
871 * @param cellY Y coordinate of the cell
872 * @param result Rect in which to write the result
873 */
874 void regionToRect(int cellX, int cellY, int spanX, int spanY, Rect result) {
875 final int hStartPadding = getPaddingLeft();
876 final int vStartPadding = getPaddingTop();
877 final int left = hStartPadding + cellX * (mCellWidth + mWidthGap);
878 final int top = vStartPadding + cellY * (mCellHeight + mHeightGap);
879 result.set(left, top, left + (spanX * mCellWidth + (spanX - 1) * mWidthGap),
880 top + (spanY * mCellHeight + (spanY - 1) * mHeightGap));
881 }
882
Adam Cohen482ed822012-03-02 14:15:13 -0800883 public float getDistanceFromCell(float x, float y, int[] cell) {
884 cellToCenterPoint(cell[0], cell[1], mTmpPoint);
885 float distance = (float) Math.sqrt( Math.pow(x - mTmpPoint[0], 2) +
886 Math.pow(y - mTmpPoint[1], 2));
887 return distance;
888 }
889
Romain Guy84f296c2009-11-04 15:00:44 -0800890 int getCellWidth() {
891 return mCellWidth;
892 }
893
894 int getCellHeight() {
895 return mCellHeight;
896 }
897
Adam Cohend4844c32011-02-18 19:25:06 -0800898 int getWidthGap() {
899 return mWidthGap;
900 }
901
902 int getHeightGap() {
903 return mHeightGap;
904 }
905
Adam Cohen7f4eabe2011-04-21 16:19:16 -0700906 Rect getContentRect(Rect r) {
907 if (r == null) {
908 r = new Rect();
909 }
910 int left = getPaddingLeft();
911 int top = getPaddingTop();
Michael Jurka8b805b12012-04-18 14:23:14 -0700912 int right = left + getWidth() - getPaddingLeft() - getPaddingRight();
913 int bottom = top + getHeight() - getPaddingTop() - getPaddingBottom();
Adam Cohen7f4eabe2011-04-21 16:19:16 -0700914 r.set(left, top, right, bottom);
915 return r;
916 }
917
Adam Cohena897f392012-04-27 18:12:05 -0700918 static void getMetrics(Rect metrics, Resources res, int measureWidth, int measureHeight,
919 int countX, int countY, int orientation) {
920 int numWidthGaps = countX - 1;
921 int numHeightGaps = countY - 1;
Adam Cohenf4bd5792012-04-27 11:35:29 -0700922
923 int widthGap;
924 int heightGap;
925 int cellWidth;
926 int cellHeight;
927 int paddingLeft;
928 int paddingRight;
929 int paddingTop;
930 int paddingBottom;
931
Adam Cohena897f392012-04-27 18:12:05 -0700932 int maxGap = res.getDimensionPixelSize(R.dimen.workspace_max_gap);
Adam Cohenf4bd5792012-04-27 11:35:29 -0700933 if (orientation == LANDSCAPE) {
934 cellWidth = res.getDimensionPixelSize(R.dimen.workspace_cell_width_land);
935 cellHeight = res.getDimensionPixelSize(R.dimen.workspace_cell_height_land);
936 widthGap = res.getDimensionPixelSize(R.dimen.workspace_width_gap_land);
937 heightGap = res.getDimensionPixelSize(R.dimen.workspace_height_gap_land);
938 paddingLeft = res.getDimensionPixelSize(R.dimen.cell_layout_left_padding_land);
939 paddingRight = res.getDimensionPixelSize(R.dimen.cell_layout_right_padding_land);
940 paddingTop = res.getDimensionPixelSize(R.dimen.cell_layout_top_padding_land);
941 paddingBottom = res.getDimensionPixelSize(R.dimen.cell_layout_bottom_padding_land);
942 } else {
943 // PORTRAIT
944 cellWidth = res.getDimensionPixelSize(R.dimen.workspace_cell_width_port);
945 cellHeight = res.getDimensionPixelSize(R.dimen.workspace_cell_height_port);
946 widthGap = res.getDimensionPixelSize(R.dimen.workspace_width_gap_port);
947 heightGap = res.getDimensionPixelSize(R.dimen.workspace_height_gap_port);
948 paddingLeft = res.getDimensionPixelSize(R.dimen.cell_layout_left_padding_port);
949 paddingRight = res.getDimensionPixelSize(R.dimen.cell_layout_right_padding_port);
950 paddingTop = res.getDimensionPixelSize(R.dimen.cell_layout_top_padding_port);
951 paddingBottom = res.getDimensionPixelSize(R.dimen.cell_layout_bottom_padding_port);
952 }
953
954 if (widthGap < 0 || heightGap < 0) {
955 int hSpace = measureWidth - paddingLeft - paddingRight;
956 int vSpace = measureHeight - paddingTop - paddingBottom;
Adam Cohena897f392012-04-27 18:12:05 -0700957 int hFreeSpace = hSpace - (countX * cellWidth);
958 int vFreeSpace = vSpace - (countY * cellHeight);
959 widthGap = Math.min(maxGap, numWidthGaps > 0 ? (hFreeSpace / numWidthGaps) : 0);
960 heightGap = Math.min(maxGap, numHeightGaps > 0 ? (vFreeSpace / numHeightGaps) : 0);
Adam Cohenf4bd5792012-04-27 11:35:29 -0700961 }
962 metrics.set(cellWidth, cellHeight, widthGap, heightGap);
963 }
964
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800965 @Override
966 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800967 int widthSpecMode = MeasureSpec.getMode(widthMeasureSpec);
Winson Chungaafa03c2010-06-11 17:34:16 -0700968 int widthSpecSize = MeasureSpec.getSize(widthMeasureSpec);
969
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800970 int heightSpecMode = MeasureSpec.getMode(heightMeasureSpec);
971 int heightSpecSize = MeasureSpec.getSize(heightMeasureSpec);
Winson Chungaafa03c2010-06-11 17:34:16 -0700972
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800973 if (widthSpecMode == MeasureSpec.UNSPECIFIED || heightSpecMode == MeasureSpec.UNSPECIFIED) {
974 throw new RuntimeException("CellLayout cannot have UNSPECIFIED dimensions");
975 }
976
Adam Cohend22015c2010-07-26 22:02:18 -0700977 int numWidthGaps = mCountX - 1;
978 int numHeightGaps = mCountY - 1;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800979
Adam Cohen234c4cd2011-07-17 21:03:04 -0700980 if (mOriginalWidthGap < 0 || mOriginalHeightGap < 0) {
Michael Jurkadd13e3d2012-05-01 12:38:17 -0700981 int hSpace = widthSpecSize - getPaddingLeft() - getPaddingRight();
982 int vSpace = heightSpecSize - getPaddingTop() - getPaddingBottom();
Adam Cohenf4bd5792012-04-27 11:35:29 -0700983 int hFreeSpace = hSpace - (mCountX * mCellWidth);
984 int vFreeSpace = vSpace - (mCountY * mCellHeight);
Winson Chung4b825dcd2011-06-19 12:41:22 -0700985 mWidthGap = Math.min(mMaxGap, numWidthGaps > 0 ? (hFreeSpace / numWidthGaps) : 0);
986 mHeightGap = Math.min(mMaxGap,numHeightGaps > 0 ? (vFreeSpace / numHeightGaps) : 0);
Michael Jurkaa52570f2012-03-20 03:18:20 -0700987 mShortcutsAndWidgets.setCellDimensions(mCellWidth, mCellHeight, mWidthGap, mHeightGap);
Adam Cohen234c4cd2011-07-17 21:03:04 -0700988 } else {
989 mWidthGap = mOriginalWidthGap;
990 mHeightGap = mOriginalHeightGap;
Winson Chungece7f5b2010-10-22 14:54:12 -0700991 }
Michael Jurka5f1c5092010-09-03 14:15:02 -0700992
Michael Jurka8c920dd2011-01-20 14:16:56 -0800993 // Initial values correspond to widthSpecMode == MeasureSpec.EXACTLY
994 int newWidth = widthSpecSize;
995 int newHeight = heightSpecSize;
Michael Jurka5f1c5092010-09-03 14:15:02 -0700996 if (widthSpecMode == MeasureSpec.AT_MOST) {
Michael Jurka8b805b12012-04-18 14:23:14 -0700997 newWidth = getPaddingLeft() + getPaddingRight() + (mCountX * mCellWidth) +
Winson Chungece7f5b2010-10-22 14:54:12 -0700998 ((mCountX - 1) * mWidthGap);
Michael Jurka8b805b12012-04-18 14:23:14 -0700999 newHeight = getPaddingTop() + getPaddingBottom() + (mCountY * mCellHeight) +
Winson Chungece7f5b2010-10-22 14:54:12 -07001000 ((mCountY - 1) * mHeightGap);
Michael Jurka5f1c5092010-09-03 14:15:02 -07001001 setMeasuredDimension(newWidth, newHeight);
Michael Jurka5f1c5092010-09-03 14:15:02 -07001002 }
Michael Jurka8c920dd2011-01-20 14:16:56 -08001003
1004 int count = getChildCount();
1005 for (int i = 0; i < count; i++) {
1006 View child = getChildAt(i);
Michael Jurka8b805b12012-04-18 14:23:14 -07001007 int childWidthMeasureSpec = MeasureSpec.makeMeasureSpec(newWidth - getPaddingLeft() -
1008 getPaddingRight(), MeasureSpec.EXACTLY);
1009 int childheightMeasureSpec = MeasureSpec.makeMeasureSpec(newHeight - getPaddingTop() -
1010 getPaddingBottom(), MeasureSpec.EXACTLY);
Michael Jurka8c920dd2011-01-20 14:16:56 -08001011 child.measure(childWidthMeasureSpec, childheightMeasureSpec);
1012 }
1013 setMeasuredDimension(newWidth, newHeight);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001014 }
1015
1016 @Override
Michael Jurka28750fb2010-09-24 17:43:49 -07001017 protected void onLayout(boolean changed, int l, int t, int r, int b) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001018 int count = getChildCount();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001019 for (int i = 0; i < count; i++) {
Michael Jurka8c920dd2011-01-20 14:16:56 -08001020 View child = getChildAt(i);
Michael Jurka8b805b12012-04-18 14:23:14 -07001021 child.layout(getPaddingLeft(), getPaddingTop(),
1022 r - l - getPaddingRight(), b - t - getPaddingBottom());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001023 }
1024 }
1025
1026 @Override
Michael Jurkadee05892010-07-27 10:01:56 -07001027 protected void onSizeChanged(int w, int h, int oldw, int oldh) {
1028 super.onSizeChanged(w, h, oldw, oldh);
Michael Jurka18014792010-10-14 09:01:34 -07001029 mBackgroundRect.set(0, 0, w, h);
Adam Cohenb5ba0972011-09-07 18:02:31 -07001030 mForegroundRect.set(mForegroundPadding, mForegroundPadding,
Adam Cohen215b4162012-08-30 13:14:08 -07001031 w - mForegroundPadding, h - mForegroundPadding);
Michael Jurkadee05892010-07-27 10:01:56 -07001032 }
1033
1034 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001035 protected void setChildrenDrawingCacheEnabled(boolean enabled) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07001036 mShortcutsAndWidgets.setChildrenDrawingCacheEnabled(enabled);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001037 }
1038
1039 @Override
1040 protected void setChildrenDrawnWithCacheEnabled(boolean enabled) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07001041 mShortcutsAndWidgets.setChildrenDrawnWithCacheEnabled(enabled);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001042 }
1043
Michael Jurka5f1c5092010-09-03 14:15:02 -07001044 public float getBackgroundAlpha() {
1045 return mBackgroundAlpha;
Michael Jurkadee05892010-07-27 10:01:56 -07001046 }
1047
Adam Cohen1b0aaac2010-10-28 11:11:18 -07001048 public void setBackgroundAlphaMultiplier(float multiplier) {
Michael Jurkaa3d30ad2012-05-08 13:43:43 -07001049 if (mBackgroundAlphaMultiplier != multiplier) {
1050 mBackgroundAlphaMultiplier = multiplier;
1051 invalidate();
1052 }
Adam Cohen1b0aaac2010-10-28 11:11:18 -07001053 }
1054
Adam Cohenddb82192010-11-10 16:32:54 -08001055 public float getBackgroundAlphaMultiplier() {
1056 return mBackgroundAlphaMultiplier;
1057 }
1058
Michael Jurka5f1c5092010-09-03 14:15:02 -07001059 public void setBackgroundAlpha(float alpha) {
Michael Jurkaafaa0502011-12-13 18:22:50 -08001060 if (mBackgroundAlpha != alpha) {
1061 mBackgroundAlpha = alpha;
1062 invalidate();
1063 }
Michael Jurkadee05892010-07-27 10:01:56 -07001064 }
1065
Michael Jurkaa52570f2012-03-20 03:18:20 -07001066 public void setShortcutAndWidgetAlpha(float alpha) {
Michael Jurka0142d492010-08-25 17:46:15 -07001067 final int childCount = getChildCount();
1068 for (int i = 0; i < childCount; i++) {
Michael Jurkadee05892010-07-27 10:01:56 -07001069 getChildAt(i).setAlpha(alpha);
1070 }
1071 }
1072
Michael Jurkaa52570f2012-03-20 03:18:20 -07001073 public ShortcutAndWidgetContainer getShortcutsAndWidgets() {
1074 if (getChildCount() > 0) {
1075 return (ShortcutAndWidgetContainer) getChildAt(0);
1076 }
1077 return null;
1078 }
1079
Patrick Dubroy440c3602010-07-13 17:50:32 -07001080 public View getChildAt(int x, int y) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07001081 return mShortcutsAndWidgets.getChildAt(x, y);
Patrick Dubroy440c3602010-07-13 17:50:32 -07001082 }
1083
Adam Cohen76fc0852011-06-17 13:26:23 -07001084 public boolean animateChildToPosition(final View child, int cellX, int cellY, int duration,
Adam Cohen482ed822012-03-02 14:15:13 -08001085 int delay, boolean permanent, boolean adjustOccupied) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07001086 ShortcutAndWidgetContainer clc = getShortcutsAndWidgets();
Adam Cohen482ed822012-03-02 14:15:13 -08001087 boolean[][] occupied = mOccupied;
1088 if (!permanent) {
1089 occupied = mTmpOccupied;
1090 }
1091
Adam Cohen19f37922012-03-21 11:59:11 -07001092 if (clc.indexOfChild(child) != -1) {
Adam Cohenbfbfd262011-06-13 16:55:12 -07001093 final LayoutParams lp = (LayoutParams) child.getLayoutParams();
1094 final ItemInfo info = (ItemInfo) child.getTag();
1095
1096 // We cancel any existing animations
1097 if (mReorderAnimators.containsKey(lp)) {
1098 mReorderAnimators.get(lp).cancel();
1099 mReorderAnimators.remove(lp);
1100 }
1101
Adam Cohen482ed822012-03-02 14:15:13 -08001102 final int oldX = lp.x;
1103 final int oldY = lp.y;
1104 if (adjustOccupied) {
1105 occupied[lp.cellX][lp.cellY] = false;
1106 occupied[cellX][cellY] = true;
1107 }
Adam Cohenbfbfd262011-06-13 16:55:12 -07001108 lp.isLockedToGrid = true;
Adam Cohen482ed822012-03-02 14:15:13 -08001109 if (permanent) {
1110 lp.cellX = info.cellX = cellX;
1111 lp.cellY = info.cellY = cellY;
1112 } else {
1113 lp.tmpCellX = cellX;
1114 lp.tmpCellY = cellY;
1115 }
Adam Cohenbfbfd262011-06-13 16:55:12 -07001116 clc.setupLp(lp);
1117 lp.isLockedToGrid = false;
Adam Cohen482ed822012-03-02 14:15:13 -08001118 final int newX = lp.x;
1119 final int newY = lp.y;
Adam Cohenbfbfd262011-06-13 16:55:12 -07001120
Adam Cohen76fc0852011-06-17 13:26:23 -07001121 lp.x = oldX;
1122 lp.y = oldY;
Adam Cohen76fc0852011-06-17 13:26:23 -07001123
Adam Cohen482ed822012-03-02 14:15:13 -08001124 // Exit early if we're not actually moving the view
1125 if (oldX == newX && oldY == newY) {
1126 lp.isLockedToGrid = true;
1127 return true;
1128 }
1129
Michael Jurka2ecf9952012-06-18 12:52:28 -07001130 ValueAnimator va = LauncherAnimUtils.ofFloat(0f, 1f);
Adam Cohen482ed822012-03-02 14:15:13 -08001131 va.setDuration(duration);
1132 mReorderAnimators.put(lp, va);
1133
1134 va.addUpdateListener(new AnimatorUpdateListener() {
1135 @Override
Adam Cohenbfbfd262011-06-13 16:55:12 -07001136 public void onAnimationUpdate(ValueAnimator animation) {
Adam Cohen482ed822012-03-02 14:15:13 -08001137 float r = ((Float) animation.getAnimatedValue()).floatValue();
Adam Cohen19f37922012-03-21 11:59:11 -07001138 lp.x = (int) ((1 - r) * oldX + r * newX);
1139 lp.y = (int) ((1 - r) * oldY + r * newY);
Adam Cohen6b8a02d2012-03-22 15:13:40 -07001140 child.requestLayout();
Adam Cohenbfbfd262011-06-13 16:55:12 -07001141 }
1142 });
Adam Cohen482ed822012-03-02 14:15:13 -08001143 va.addListener(new AnimatorListenerAdapter() {
Adam Cohenbfbfd262011-06-13 16:55:12 -07001144 boolean cancelled = false;
1145 public void onAnimationEnd(Animator animation) {
1146 // If the animation was cancelled, it means that another animation
1147 // has interrupted this one, and we don't want to lock the item into
1148 // place just yet.
1149 if (!cancelled) {
1150 lp.isLockedToGrid = true;
Adam Cohen482ed822012-03-02 14:15:13 -08001151 child.requestLayout();
Adam Cohenbfbfd262011-06-13 16:55:12 -07001152 }
1153 if (mReorderAnimators.containsKey(lp)) {
1154 mReorderAnimators.remove(lp);
1155 }
1156 }
1157 public void onAnimationCancel(Animator animation) {
1158 cancelled = true;
1159 }
1160 });
Adam Cohen482ed822012-03-02 14:15:13 -08001161 va.setStartDelay(delay);
1162 va.start();
Adam Cohenbfbfd262011-06-13 16:55:12 -07001163 return true;
1164 }
1165 return false;
1166 }
1167
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001168 /**
1169 * Estimate where the top left cell of the dragged item will land if it is dropped.
1170 *
1171 * @param originX The X value of the top left corner of the item
1172 * @param originY The Y value of the top left corner of the item
1173 * @param spanX The number of horizontal cells that the item spans
1174 * @param spanY The number of vertical cells that the item spans
1175 * @param result The estimated drop cell X and Y.
1176 */
1177 void estimateDropCell(int originX, int originY, int spanX, int spanY, int[] result) {
Adam Cohend22015c2010-07-26 22:02:18 -07001178 final int countX = mCountX;
1179 final int countY = mCountY;
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001180
Michael Jurkaa63c4522010-08-19 13:52:27 -07001181 // pointToCellRounded takes the top left of a cell but will pad that with
1182 // cellWidth/2 and cellHeight/2 when finding the matching cell
1183 pointToCellRounded(originX, originY, result);
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001184
1185 // If the item isn't fully on this screen, snap to the edges
1186 int rightOverhang = result[0] + spanX - countX;
1187 if (rightOverhang > 0) {
1188 result[0] -= rightOverhang; // Snap to right
1189 }
1190 result[0] = Math.max(0, result[0]); // Snap to left
1191 int bottomOverhang = result[1] + spanY - countY;
1192 if (bottomOverhang > 0) {
1193 result[1] -= bottomOverhang; // Snap to bottom
1194 }
1195 result[1] = Math.max(0, result[1]); // Snap to top
1196 }
1197
Adam Cohen482ed822012-03-02 14:15:13 -08001198 void visualizeDropLocation(View v, Bitmap dragOutline, int originX, int originY, int cellX,
1199 int cellY, int spanX, int spanY, boolean resize, Point dragOffset, Rect dragRegion) {
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -07001200 final int oldDragCellX = mDragCell[0];
1201 final int oldDragCellY = mDragCell[1];
Adam Cohen482ed822012-03-02 14:15:13 -08001202
Winson Chungb8c69f32011-10-19 21:36:08 -07001203 if (v != null && dragOffset == null) {
Winson Chunga9abd0e2010-10-27 17:18:37 -07001204 mDragCenter.set(originX + (v.getWidth() / 2), originY + (v.getHeight() / 2));
1205 } else {
1206 mDragCenter.set(originX, originY);
1207 }
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001208
Adam Cohen2801caf2011-05-13 20:57:39 -07001209 if (dragOutline == null && v == null) {
Adam Cohen2801caf2011-05-13 20:57:39 -07001210 return;
1211 }
1212
Adam Cohen482ed822012-03-02 14:15:13 -08001213 if (cellX != oldDragCellX || cellY != oldDragCellY) {
1214 mDragCell[0] = cellX;
1215 mDragCell[1] = cellY;
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001216 // Find the top left corner of the rect the object will occupy
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001217 final int[] topLeft = mTmpPoint;
Adam Cohen482ed822012-03-02 14:15:13 -08001218 cellToPoint(cellX, cellY, topLeft);
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001219
Joe Onorato4be866d2010-10-10 11:26:02 -07001220 int left = topLeft[0];
1221 int top = topLeft[1];
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001222
Winson Chungb8c69f32011-10-19 21:36:08 -07001223 if (v != null && dragOffset == null) {
Adam Cohen99e8b402011-03-25 19:23:43 -07001224 // When drawing the drag outline, it did not account for margin offsets
1225 // added by the view's parent.
1226 MarginLayoutParams lp = (MarginLayoutParams) v.getLayoutParams();
1227 left += lp.leftMargin;
1228 top += lp.topMargin;
Winson Chung150fbab2010-09-29 17:14:26 -07001229
Adam Cohen99e8b402011-03-25 19:23:43 -07001230 // Offsets due to the size difference between the View and the dragOutline.
1231 // There is a size difference to account for the outer blur, which may lie
1232 // outside the bounds of the view.
Winson Chunga9abd0e2010-10-27 17:18:37 -07001233 top += (v.getHeight() - dragOutline.getHeight()) / 2;
Adam Cohenae915ce2011-08-25 13:47:22 -07001234 // We center about the x axis
1235 left += ((mCellWidth * spanX) + ((spanX - 1) * mWidthGap)
1236 - dragOutline.getWidth()) / 2;
Adam Cohen66396872011-04-15 17:50:36 -07001237 } else {
Winson Chungb8c69f32011-10-19 21:36:08 -07001238 if (dragOffset != null && dragRegion != null) {
1239 // Center the drag region *horizontally* in the cell and apply a drag
1240 // outline offset
1241 left += dragOffset.x + ((mCellWidth * spanX) + ((spanX - 1) * mWidthGap)
1242 - dragRegion.width()) / 2;
1243 top += dragOffset.y;
1244 } else {
1245 // Center the drag outline in the cell
1246 left += ((mCellWidth * spanX) + ((spanX - 1) * mWidthGap)
1247 - dragOutline.getWidth()) / 2;
1248 top += ((mCellHeight * spanY) + ((spanY - 1) * mHeightGap)
1249 - dragOutline.getHeight()) / 2;
1250 }
Winson Chunga9abd0e2010-10-27 17:18:37 -07001251 }
Joe Onorato4be866d2010-10-10 11:26:02 -07001252 final int oldIndex = mDragOutlineCurrent;
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -07001253 mDragOutlineAnims[oldIndex].animateOut();
1254 mDragOutlineCurrent = (oldIndex + 1) % mDragOutlines.length;
Adam Cohend41fbf52012-02-16 23:53:59 -08001255 Rect r = mDragOutlines[mDragOutlineCurrent];
1256 r.set(left, top, left + dragOutline.getWidth(), top + dragOutline.getHeight());
1257 if (resize) {
Adam Cohen482ed822012-03-02 14:15:13 -08001258 cellToRect(cellX, cellY, spanX, spanY, r);
Adam Cohend41fbf52012-02-16 23:53:59 -08001259 }
Winson Chung150fbab2010-09-29 17:14:26 -07001260
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -07001261 mDragOutlineAnims[mDragOutlineCurrent].setTag(dragOutline);
1262 mDragOutlineAnims[mDragOutlineCurrent].animateIn();
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001263 }
1264 }
1265
Adam Cohene0310962011-04-18 16:15:31 -07001266 public void clearDragOutlines() {
1267 final int oldIndex = mDragOutlineCurrent;
1268 mDragOutlineAnims[oldIndex].animateOut();
Adam Cohend41fbf52012-02-16 23:53:59 -08001269 mDragCell[0] = mDragCell[1] = -1;
Adam Cohene0310962011-04-18 16:15:31 -07001270 }
1271
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001272 /**
Jeff Sharkey70864282009-04-07 21:08:40 -07001273 * Find a vacant area that will fit the given bounds nearest the requested
1274 * cell location. Uses Euclidean distance to score multiple vacant areas.
Winson Chungaafa03c2010-06-11 17:34:16 -07001275 *
Romain Guy51afc022009-05-04 18:03:43 -07001276 * @param pixelX The X location at which you want to search for a vacant area.
1277 * @param pixelY The Y location at which you want to search for a vacant area.
Jeff Sharkey70864282009-04-07 21:08:40 -07001278 * @param spanX Horizontal span of the object.
1279 * @param spanY Vertical span of the object.
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001280 * @param result Array in which to place the result, or null (in which case a new array will
1281 * be allocated)
Jeff Sharkey70864282009-04-07 21:08:40 -07001282 * @return The X, Y cell of a vacant area that can contain this object,
1283 * nearest the requested location.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001284 */
Adam Cohend41fbf52012-02-16 23:53:59 -08001285 int[] findNearestVacantArea(int pixelX, int pixelY, int spanX, int spanY,
1286 int[] result) {
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001287 return findNearestVacantArea(pixelX, pixelY, spanX, spanY, null, result);
Michael Jurka6a1435d2010-09-27 17:35:12 -07001288 }
Winson Chungaafa03c2010-06-11 17:34:16 -07001289
Michael Jurka6a1435d2010-09-27 17:35:12 -07001290 /**
1291 * Find a vacant area that will fit the given bounds nearest the requested
1292 * cell location. Uses Euclidean distance to score multiple vacant areas.
1293 *
1294 * @param pixelX The X location at which you want to search for a vacant area.
1295 * @param pixelY The Y location at which you want to search for a vacant area.
Adam Cohend41fbf52012-02-16 23:53:59 -08001296 * @param minSpanX The minimum horizontal span required
1297 * @param minSpanY The minimum vertical span required
1298 * @param spanX Horizontal span of the object.
1299 * @param spanY Vertical span of the object.
1300 * @param result Array in which to place the result, or null (in which case a new array will
1301 * be allocated)
1302 * @return The X, Y cell of a vacant area that can contain this object,
1303 * nearest the requested location.
1304 */
1305 int[] findNearestVacantArea(int pixelX, int pixelY, int minSpanX, int minSpanY, int spanX,
1306 int spanY, int[] result, int[] resultSpan) {
1307 return findNearestVacantArea(pixelX, pixelY, minSpanX, minSpanY, spanX, spanY, null,
1308 result, resultSpan);
1309 }
1310
1311 /**
1312 * Find a vacant area that will fit the given bounds nearest the requested
1313 * cell location. Uses Euclidean distance to score multiple vacant areas.
1314 *
1315 * @param pixelX The X location at which you want to search for a vacant area.
1316 * @param pixelY The Y location at which you want to search for a vacant area.
Michael Jurka6a1435d2010-09-27 17:35:12 -07001317 * @param spanX Horizontal span of the object.
1318 * @param spanY Vertical span of the object.
Adam Cohendf035382011-04-11 17:22:04 -07001319 * @param ignoreOccupied If true, the result can be an occupied cell
1320 * @param result Array in which to place the result, or null (in which case a new array will
1321 * be allocated)
Michael Jurka6a1435d2010-09-27 17:35:12 -07001322 * @return The X, Y cell of a vacant area that can contain this object,
1323 * nearest the requested location.
1324 */
Adam Cohendf035382011-04-11 17:22:04 -07001325 int[] findNearestArea(int pixelX, int pixelY, int spanX, int spanY, View ignoreView,
1326 boolean ignoreOccupied, int[] result) {
Adam Cohend41fbf52012-02-16 23:53:59 -08001327 return findNearestArea(pixelX, pixelY, spanX, spanY,
Adam Cohen482ed822012-03-02 14:15:13 -08001328 spanX, spanY, ignoreView, ignoreOccupied, result, null, mOccupied);
Adam Cohend41fbf52012-02-16 23:53:59 -08001329 }
1330
1331 private final Stack<Rect> mTempRectStack = new Stack<Rect>();
1332 private void lazyInitTempRectStack() {
1333 if (mTempRectStack.isEmpty()) {
1334 for (int i = 0; i < mCountX * mCountY; i++) {
1335 mTempRectStack.push(new Rect());
1336 }
1337 }
1338 }
Adam Cohen482ed822012-03-02 14:15:13 -08001339
Adam Cohend41fbf52012-02-16 23:53:59 -08001340 private void recycleTempRects(Stack<Rect> used) {
1341 while (!used.isEmpty()) {
1342 mTempRectStack.push(used.pop());
1343 }
1344 }
1345
1346 /**
1347 * Find a vacant area that will fit the given bounds nearest the requested
1348 * cell location. Uses Euclidean distance to score multiple vacant areas.
1349 *
1350 * @param pixelX The X location at which you want to search for a vacant area.
1351 * @param pixelY The Y location at which you want to search for a vacant area.
1352 * @param minSpanX The minimum horizontal span required
1353 * @param minSpanY The minimum vertical span required
1354 * @param spanX Horizontal span of the object.
1355 * @param spanY Vertical span of the object.
1356 * @param ignoreOccupied If true, the result can be an occupied cell
1357 * @param result Array in which to place the result, or null (in which case a new array will
1358 * be allocated)
1359 * @return The X, Y cell of a vacant area that can contain this object,
1360 * nearest the requested location.
1361 */
1362 int[] findNearestArea(int pixelX, int pixelY, int minSpanX, int minSpanY, int spanX, int spanY,
Adam Cohen482ed822012-03-02 14:15:13 -08001363 View ignoreView, boolean ignoreOccupied, int[] result, int[] resultSpan,
1364 boolean[][] occupied) {
Adam Cohend41fbf52012-02-16 23:53:59 -08001365 lazyInitTempRectStack();
Michael Jurkac6ee42e2010-09-30 12:04:50 -07001366 // mark space take by ignoreView as available (method checks if ignoreView is null)
Adam Cohen482ed822012-03-02 14:15:13 -08001367 markCellsAsUnoccupiedForView(ignoreView, occupied);
Michael Jurkac6ee42e2010-09-30 12:04:50 -07001368
Adam Cohene3e27a82011-04-15 12:07:39 -07001369 // For items with a spanX / spanY > 1, the passed in point (pixelX, pixelY) corresponds
1370 // to the center of the item, but we are searching based on the top-left cell, so
1371 // we translate the point over to correspond to the top-left.
1372 pixelX -= (mCellWidth + mWidthGap) * (spanX - 1) / 2f;
1373 pixelY -= (mCellHeight + mHeightGap) * (spanY - 1) / 2f;
1374
Jeff Sharkey70864282009-04-07 21:08:40 -07001375 // Keep track of best-scoring drop area
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001376 final int[] bestXY = result != null ? result : new int[2];
Jeff Sharkey70864282009-04-07 21:08:40 -07001377 double bestDistance = Double.MAX_VALUE;
Adam Cohend41fbf52012-02-16 23:53:59 -08001378 final Rect bestRect = new Rect(-1, -1, -1, -1);
1379 final Stack<Rect> validRegions = new Stack<Rect>();
Winson Chungaafa03c2010-06-11 17:34:16 -07001380
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001381 final int countX = mCountX;
1382 final int countY = mCountY;
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001383
Adam Cohend41fbf52012-02-16 23:53:59 -08001384 if (minSpanX <= 0 || minSpanY <= 0 || spanX <= 0 || spanY <= 0 ||
1385 spanX < minSpanX || spanY < minSpanY) {
1386 return bestXY;
1387 }
1388
1389 for (int y = 0; y < countY - (minSpanY - 1); y++) {
Michael Jurkac28de512010-08-13 11:27:44 -07001390 inner:
Adam Cohend41fbf52012-02-16 23:53:59 -08001391 for (int x = 0; x < countX - (minSpanX - 1); x++) {
1392 int ySize = -1;
1393 int xSize = -1;
Adam Cohendf035382011-04-11 17:22:04 -07001394 if (ignoreOccupied) {
Adam Cohend41fbf52012-02-16 23:53:59 -08001395 // First, let's see if this thing fits anywhere
1396 for (int i = 0; i < minSpanX; i++) {
1397 for (int j = 0; j < minSpanY; j++) {
Adam Cohendf035382011-04-11 17:22:04 -07001398 if (occupied[x + i][y + j]) {
Adam Cohendf035382011-04-11 17:22:04 -07001399 continue inner;
1400 }
Michael Jurkac28de512010-08-13 11:27:44 -07001401 }
1402 }
Adam Cohend41fbf52012-02-16 23:53:59 -08001403 xSize = minSpanX;
1404 ySize = minSpanY;
1405
1406 // We know that the item will fit at _some_ acceptable size, now let's see
1407 // how big we can make it. We'll alternate between incrementing x and y spans
1408 // until we hit a limit.
1409 boolean incX = true;
1410 boolean hitMaxX = xSize >= spanX;
1411 boolean hitMaxY = ySize >= spanY;
1412 while (!(hitMaxX && hitMaxY)) {
1413 if (incX && !hitMaxX) {
1414 for (int j = 0; j < ySize; j++) {
1415 if (x + xSize > countX -1 || occupied[x + xSize][y + j]) {
1416 // We can't move out horizontally
1417 hitMaxX = true;
1418 }
1419 }
1420 if (!hitMaxX) {
1421 xSize++;
1422 }
1423 } else if (!hitMaxY) {
1424 for (int i = 0; i < xSize; i++) {
1425 if (y + ySize > countY - 1 || occupied[x + i][y + ySize]) {
1426 // We can't move out vertically
1427 hitMaxY = true;
1428 }
1429 }
1430 if (!hitMaxY) {
1431 ySize++;
1432 }
1433 }
1434 hitMaxX |= xSize >= spanX;
1435 hitMaxY |= ySize >= spanY;
1436 incX = !incX;
1437 }
1438 incX = true;
1439 hitMaxX = xSize >= spanX;
1440 hitMaxY = ySize >= spanY;
Michael Jurkac28de512010-08-13 11:27:44 -07001441 }
Winson Chung0be025d2011-05-23 17:45:09 -07001442 final int[] cellXY = mTmpXY;
Adam Cohene3e27a82011-04-15 12:07:39 -07001443 cellToCenterPoint(x, y, cellXY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001444
Adam Cohend41fbf52012-02-16 23:53:59 -08001445 // We verify that the current rect is not a sub-rect of any of our previous
1446 // candidates. In this case, the current rect is disqualified in favour of the
1447 // containing rect.
1448 Rect currentRect = mTempRectStack.pop();
1449 currentRect.set(x, y, x + xSize, y + ySize);
1450 boolean contained = false;
1451 for (Rect r : validRegions) {
1452 if (r.contains(currentRect)) {
1453 contained = true;
1454 break;
1455 }
1456 }
1457 validRegions.push(currentRect);
Michael Jurkac28de512010-08-13 11:27:44 -07001458 double distance = Math.sqrt(Math.pow(cellXY[0] - pixelX, 2)
1459 + Math.pow(cellXY[1] - pixelY, 2));
Adam Cohen482ed822012-03-02 14:15:13 -08001460
Adam Cohend41fbf52012-02-16 23:53:59 -08001461 if ((distance <= bestDistance && !contained) ||
1462 currentRect.contains(bestRect)) {
Michael Jurkac28de512010-08-13 11:27:44 -07001463 bestDistance = distance;
1464 bestXY[0] = x;
1465 bestXY[1] = y;
Adam Cohend41fbf52012-02-16 23:53:59 -08001466 if (resultSpan != null) {
1467 resultSpan[0] = xSize;
1468 resultSpan[1] = ySize;
1469 }
1470 bestRect.set(currentRect);
Michael Jurkac28de512010-08-13 11:27:44 -07001471 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001472 }
1473 }
Michael Jurkac6ee42e2010-09-30 12:04:50 -07001474 // re-mark space taken by ignoreView as occupied
Adam Cohen482ed822012-03-02 14:15:13 -08001475 markCellsAsOccupiedForView(ignoreView, occupied);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001476
Adam Cohenc0dcf592011-06-01 15:30:43 -07001477 // Return -1, -1 if no suitable location found
1478 if (bestDistance == Double.MAX_VALUE) {
1479 bestXY[0] = -1;
1480 bestXY[1] = -1;
Jeff Sharkey70864282009-04-07 21:08:40 -07001481 }
Adam Cohend41fbf52012-02-16 23:53:59 -08001482 recycleTempRects(validRegions);
Adam Cohenc0dcf592011-06-01 15:30:43 -07001483 return bestXY;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001484 }
Winson Chungaafa03c2010-06-11 17:34:16 -07001485
Adam Cohen482ed822012-03-02 14:15:13 -08001486 /**
1487 * Find a vacant area that will fit the given bounds nearest the requested
1488 * cell location, and will also weigh in a suggested direction vector of the
1489 * desired location. This method computers distance based on unit grid distances,
1490 * not pixel distances.
1491 *
Adam Cohen47a876d2012-03-19 13:21:41 -07001492 * @param cellX The X cell nearest to which you want to search for a vacant area.
1493 * @param cellY The Y cell nearest which you want to search for a vacant area.
Adam Cohen482ed822012-03-02 14:15:13 -08001494 * @param spanX Horizontal span of the object.
1495 * @param spanY Vertical span of the object.
Adam Cohen47a876d2012-03-19 13:21:41 -07001496 * @param direction The favored direction in which the views should move from x, y
1497 * @param exactDirectionOnly If this parameter is true, then only solutions where the direction
1498 * matches exactly. Otherwise we find the best matching direction.
1499 * @param occoupied The array which represents which cells in the CellLayout are occupied
1500 * @param blockOccupied The array which represents which cells in the specified block (cellX,
1501 * cellY, spanX, spanY) are occupied. This is used when try to move a group of views.
Adam Cohen482ed822012-03-02 14:15:13 -08001502 * @param result Array in which to place the result, or null (in which case a new array will
1503 * be allocated)
1504 * @return The X, Y cell of a vacant area that can contain this object,
1505 * nearest the requested location.
1506 */
1507 private int[] findNearestArea(int cellX, int cellY, int spanX, int spanY, int[] direction,
Adam Cohen47a876d2012-03-19 13:21:41 -07001508 boolean[][] occupied, boolean blockOccupied[][], int[] result) {
Adam Cohen482ed822012-03-02 14:15:13 -08001509 // Keep track of best-scoring drop area
1510 final int[] bestXY = result != null ? result : new int[2];
1511 float bestDistance = Float.MAX_VALUE;
1512 int bestDirectionScore = Integer.MIN_VALUE;
1513
1514 final int countX = mCountX;
1515 final int countY = mCountY;
1516
1517 for (int y = 0; y < countY - (spanY - 1); y++) {
1518 inner:
1519 for (int x = 0; x < countX - (spanX - 1); x++) {
1520 // First, let's see if this thing fits anywhere
1521 for (int i = 0; i < spanX; i++) {
1522 for (int j = 0; j < spanY; j++) {
Adam Cohen47a876d2012-03-19 13:21:41 -07001523 if (occupied[x + i][y + j] && (blockOccupied == null || blockOccupied[i][j])) {
Adam Cohen482ed822012-03-02 14:15:13 -08001524 continue inner;
1525 }
1526 }
1527 }
1528
1529 float distance = (float)
1530 Math.sqrt((x - cellX) * (x - cellX) + (y - cellY) * (y - cellY));
1531 int[] curDirection = mTmpPoint;
Adam Cohen47a876d2012-03-19 13:21:41 -07001532 computeDirectionVector(x - cellX, y - cellY, curDirection);
1533 // The direction score is just the dot product of the two candidate direction
1534 // and that passed in.
Adam Cohen482ed822012-03-02 14:15:13 -08001535 int curDirectionScore = direction[0] * curDirection[0] +
1536 direction[1] * curDirection[1];
Adam Cohen47a876d2012-03-19 13:21:41 -07001537 boolean exactDirectionOnly = false;
1538 boolean directionMatches = direction[0] == curDirection[0] &&
1539 direction[0] == curDirection[0];
1540 if ((directionMatches || !exactDirectionOnly) &&
1541 Float.compare(distance, bestDistance) < 0 || (Float.compare(distance,
Adam Cohen482ed822012-03-02 14:15:13 -08001542 bestDistance) == 0 && curDirectionScore > bestDirectionScore)) {
1543 bestDistance = distance;
1544 bestDirectionScore = curDirectionScore;
1545 bestXY[0] = x;
1546 bestXY[1] = y;
1547 }
1548 }
1549 }
1550
1551 // Return -1, -1 if no suitable location found
1552 if (bestDistance == Float.MAX_VALUE) {
1553 bestXY[0] = -1;
1554 bestXY[1] = -1;
1555 }
1556 return bestXY;
1557 }
1558
1559 private boolean addViewToTempLocation(View v, Rect rectOccupiedByPotentialDrop,
Adam Cohen8baab352012-03-20 17:39:21 -07001560 int[] direction, ItemConfiguration currentState) {
1561 CellAndSpan c = currentState.map.get(v);
Adam Cohen482ed822012-03-02 14:15:13 -08001562 boolean success = false;
Adam Cohen8baab352012-03-20 17:39:21 -07001563 markCellsForView(c.x, c.y, c.spanX, c.spanY, mTmpOccupied, false);
Adam Cohen482ed822012-03-02 14:15:13 -08001564 markCellsForRect(rectOccupiedByPotentialDrop, mTmpOccupied, true);
1565
Adam Cohen8baab352012-03-20 17:39:21 -07001566 findNearestArea(c.x, c.y, c.spanX, c.spanY, direction, mTmpOccupied, null, mTempLocation);
Adam Cohen482ed822012-03-02 14:15:13 -08001567
1568 if (mTempLocation[0] >= 0 && mTempLocation[1] >= 0) {
Adam Cohen8baab352012-03-20 17:39:21 -07001569 c.x = mTempLocation[0];
1570 c.y = mTempLocation[1];
Adam Cohen482ed822012-03-02 14:15:13 -08001571 success = true;
Adam Cohen482ed822012-03-02 14:15:13 -08001572 }
Adam Cohen8baab352012-03-20 17:39:21 -07001573 markCellsForView(c.x, c.y, c.spanX, c.spanY, mTmpOccupied, true);
Adam Cohen482ed822012-03-02 14:15:13 -08001574 return success;
1575 }
1576
Adam Cohenf3900c22012-11-16 18:28:11 -08001577 /**
1578 * This helper class defines a cluster of views. It helps with defining complex edges
1579 * of the cluster and determining how those edges interact with other views. The edges
1580 * essentially define a fine-grained boundary around the cluster of views -- like a more
1581 * precise version of a bounding box.
1582 */
1583 private class ViewCluster {
1584 final static int LEFT = 0;
1585 final static int TOP = 1;
1586 final static int RIGHT = 2;
1587 final static int BOTTOM = 3;
Adam Cohen47a876d2012-03-19 13:21:41 -07001588
Adam Cohenf3900c22012-11-16 18:28:11 -08001589 ArrayList<View> views;
1590 ItemConfiguration config;
1591 Rect boundingRect = new Rect();
Adam Cohen47a876d2012-03-19 13:21:41 -07001592
Adam Cohenf3900c22012-11-16 18:28:11 -08001593 int[] leftEdge = new int[mCountY];
1594 int[] rightEdge = new int[mCountY];
1595 int[] topEdge = new int[mCountX];
1596 int[] bottomEdge = new int[mCountX];
1597 boolean leftEdgeDirty, rightEdgeDirty, topEdgeDirty, bottomEdgeDirty, boundingRectDirty;
1598
1599 @SuppressWarnings("unchecked")
1600 public ViewCluster(ArrayList<View> views, ItemConfiguration config) {
1601 this.views = (ArrayList<View>) views.clone();
1602 this.config = config;
1603 resetEdges();
Adam Cohen47a876d2012-03-19 13:21:41 -07001604 }
1605
Adam Cohenf3900c22012-11-16 18:28:11 -08001606 void resetEdges() {
1607 for (int i = 0; i < mCountX; i++) {
1608 topEdge[i] = -1;
1609 bottomEdge[i] = -1;
1610 }
1611 for (int i = 0; i < mCountY; i++) {
1612 leftEdge[i] = -1;
1613 rightEdge[i] = -1;
1614 }
1615 leftEdgeDirty = true;
1616 rightEdgeDirty = true;
1617 bottomEdgeDirty = true;
1618 topEdgeDirty = true;
1619 boundingRectDirty = true;
1620 }
Adam Cohen47a876d2012-03-19 13:21:41 -07001621
Adam Cohenf3900c22012-11-16 18:28:11 -08001622 void computeEdge(int which, int[] edge) {
1623 int count = views.size();
1624 for (int i = 0; i < count; i++) {
1625 CellAndSpan cs = config.map.get(views.get(i));
1626 switch (which) {
1627 case LEFT:
1628 int left = cs.x;
1629 for (int j = cs.y; j < cs.y + cs.spanY; j++) {
1630 if (left < edge[j] || edge[j] < 0) {
1631 edge[j] = left;
Adam Cohena56dc102012-07-13 13:41:42 -07001632 }
Adam Cohen47a876d2012-03-19 13:21:41 -07001633 }
Adam Cohenf3900c22012-11-16 18:28:11 -08001634 break;
1635 case RIGHT:
1636 int right = cs.x + cs.spanX;
1637 for (int j = cs.y; j < cs.y + cs.spanY; j++) {
1638 if (right > edge[j]) {
1639 edge[j] = right;
1640 }
1641 }
1642 break;
1643 case TOP:
1644 int top = cs.y;
1645 for (int j = cs.x; j < cs.x + cs.spanX; j++) {
1646 if (top < edge[j] || edge[j] < 0) {
1647 edge[j] = top;
1648 }
1649 }
1650 break;
1651 case BOTTOM:
1652 int bottom = cs.y + cs.spanY;
1653 for (int j = cs.x; j < cs.x + cs.spanX; j++) {
1654 if (bottom > edge[j]) {
1655 edge[j] = bottom;
1656 }
1657 }
1658 break;
Adam Cohen47a876d2012-03-19 13:21:41 -07001659 }
1660 }
1661 }
Adam Cohenf3900c22012-11-16 18:28:11 -08001662
1663 boolean isViewTouchingEdge(View v, int whichEdge) {
1664 CellAndSpan cs = config.map.get(v);
1665
1666 int[] edge = getEdge(whichEdge);
1667
1668 switch (whichEdge) {
1669 case LEFT:
1670 for (int i = cs.y; i < cs.y + cs.spanY; i++) {
1671 if (edge[i] == cs.x + cs.spanX) {
1672 return true;
1673 }
1674 }
1675 break;
1676 case RIGHT:
1677 for (int i = cs.y; i < cs.y + cs.spanY; i++) {
1678 if (edge[i] == cs.x) {
1679 return true;
1680 }
1681 }
1682 break;
1683 case TOP:
1684 for (int i = cs.x; i < cs.x + cs.spanX; i++) {
1685 if (edge[i] == cs.y + cs.spanY) {
1686 return true;
1687 }
1688 }
1689 break;
1690 case BOTTOM:
1691 for (int i = cs.x; i < cs.x + cs.spanX; i++) {
1692 if (edge[i] == cs.y) {
1693 return true;
1694 }
1695 }
1696 break;
1697 }
1698 return false;
1699 }
1700
1701 void shift(int whichEdge, int delta) {
1702 for (View v: views) {
1703 CellAndSpan c = config.map.get(v);
1704 switch (whichEdge) {
1705 case LEFT:
1706 c.x -= delta;
1707 break;
1708 case RIGHT:
1709 c.x += delta;
1710 break;
1711 case TOP:
1712 c.y -= delta;
1713 break;
1714 case BOTTOM:
1715 default:
1716 c.y += delta;
1717 break;
1718 }
1719 }
1720 resetEdges();
1721 }
1722
1723 public void addView(View v) {
1724 views.add(v);
1725 resetEdges();
1726 }
1727
1728 public Rect getBoundingRect() {
1729 if (boundingRectDirty) {
1730 boolean first = true;
1731 for (View v: views) {
1732 CellAndSpan c = config.map.get(v);
1733 if (first) {
1734 boundingRect.set(c.x, c.y, c.x + c.spanX, c.y + c.spanY);
1735 first = false;
1736 } else {
1737 boundingRect.union(c.x, c.y, c.x + c.spanX, c.y + c.spanY);
1738 }
1739 }
1740 }
1741 return boundingRect;
1742 }
1743
1744 public int[] getEdge(int which) {
1745 switch (which) {
1746 case LEFT:
1747 return getLeftEdge();
1748 case RIGHT:
1749 return getRightEdge();
1750 case TOP:
1751 return getTopEdge();
1752 case BOTTOM:
1753 default:
1754 return getBottomEdge();
1755 }
1756 }
1757
1758 public int[] getLeftEdge() {
1759 if (leftEdgeDirty) {
1760 computeEdge(LEFT, leftEdge);
1761 }
1762 return leftEdge;
1763 }
1764
1765 public int[] getRightEdge() {
1766 if (rightEdgeDirty) {
1767 computeEdge(RIGHT, rightEdge);
1768 }
1769 return rightEdge;
1770 }
1771
1772 public int[] getTopEdge() {
1773 if (topEdgeDirty) {
1774 computeEdge(TOP, topEdge);
1775 }
1776 return topEdge;
1777 }
1778
1779 public int[] getBottomEdge() {
1780 if (bottomEdgeDirty) {
1781 computeEdge(BOTTOM, bottomEdge);
1782 }
1783 return bottomEdge;
1784 }
1785
1786 PositionComparator comparator = new PositionComparator();
1787 class PositionComparator implements Comparator<View> {
1788 int whichEdge = 0;
1789 public int compare(View left, View right) {
1790 CellAndSpan l = config.map.get(left);
1791 CellAndSpan r = config.map.get(right);
1792 switch (whichEdge) {
1793 case LEFT:
1794 return (r.x + r.spanX) - (l.x + l.spanX);
1795 case RIGHT:
1796 return l.x - r.x;
1797 case TOP:
1798 return (r.y + r.spanY) - (l.y + l.spanY);
1799 case BOTTOM:
1800 default:
1801 return l.y - r.y;
1802 }
1803 }
1804 }
1805
1806 public void sortConfigurationForEdgePush(int edge) {
1807 comparator.whichEdge = edge;
1808 Collections.sort(config.sortedViews, comparator);
1809 }
Adam Cohen47a876d2012-03-19 13:21:41 -07001810 }
1811
Adam Cohenf3900c22012-11-16 18:28:11 -08001812 private boolean pushViewsToTempLocation(ArrayList<View> views, Rect rectOccupiedByPotentialDrop,
1813 int[] direction, View dragView, ItemConfiguration currentState) {
Adam Cohene0489502012-08-27 15:18:53 -07001814
Adam Cohenf3900c22012-11-16 18:28:11 -08001815 ViewCluster cluster = new ViewCluster(views, currentState);
1816 Rect clusterRect = cluster.getBoundingRect();
1817 int whichEdge;
1818 int pushDistance;
1819 boolean fail = false;
1820
1821 // Determine the edge of the cluster that will be leading the push and how far
1822 // the cluster must be shifted.
1823 if (direction[0] < 0) {
1824 whichEdge = ViewCluster.LEFT;
1825 pushDistance = clusterRect.right - rectOccupiedByPotentialDrop.left;
Adam Cohene0489502012-08-27 15:18:53 -07001826 } else if (direction[0] > 0) {
Adam Cohenf3900c22012-11-16 18:28:11 -08001827 whichEdge = ViewCluster.RIGHT;
1828 pushDistance = rectOccupiedByPotentialDrop.right - clusterRect.left;
1829 } else if (direction[1] < 0) {
1830 whichEdge = ViewCluster.TOP;
1831 pushDistance = clusterRect.bottom - rectOccupiedByPotentialDrop.top;
1832 } else {
1833 whichEdge = ViewCluster.BOTTOM;
1834 pushDistance = rectOccupiedByPotentialDrop.bottom - clusterRect.top;
Adam Cohene0489502012-08-27 15:18:53 -07001835 }
1836
Adam Cohenf3900c22012-11-16 18:28:11 -08001837 // Break early for invalid push distance.
1838 if (pushDistance <= 0) {
1839 return false;
Adam Cohene0489502012-08-27 15:18:53 -07001840 }
Adam Cohenf3900c22012-11-16 18:28:11 -08001841
1842 // Mark the occupied state as false for the group of views we want to move.
1843 for (View v: views) {
1844 CellAndSpan c = currentState.map.get(v);
1845 markCellsForView(c.x, c.y, c.spanX, c.spanY, mTmpOccupied, false);
1846 }
1847
1848 // We save the current configuration -- if we fail to find a solution we will revert
1849 // to the initial state. The process of finding a solution modifies the configuration
1850 // in place, hence the need for revert in the failure case.
1851 currentState.save();
1852
1853 // The pushing algorithm is simplified by considering the views in the order in which
1854 // they would be pushed by the cluster. For example, if the cluster is leading with its
1855 // left edge, we consider sort the views by their right edge, from right to left.
1856 cluster.sortConfigurationForEdgePush(whichEdge);
1857
1858 while (pushDistance > 0 && !fail) {
1859 for (View v: currentState.sortedViews) {
1860 // For each view that isn't in the cluster, we see if the leading edge of the
1861 // cluster is contacting the edge of that view. If so, we add that view to the
1862 // cluster.
1863 if (!cluster.views.contains(v) && v != dragView) {
1864 if (cluster.isViewTouchingEdge(v, whichEdge)) {
1865 LayoutParams lp = (LayoutParams) v.getLayoutParams();
1866 if (!lp.canReorder) {
1867 // The push solution includes the all apps button, this is not viable.
1868 fail = true;
1869 break;
1870 }
1871 cluster.addView(v);
1872 CellAndSpan c = currentState.map.get(v);
1873
1874 // Adding view to cluster, mark it as not occupied.
1875 markCellsForView(c.x, c.y, c.spanX, c.spanY, mTmpOccupied, false);
1876 }
1877 }
1878 }
1879 pushDistance--;
1880
1881 // The cluster has been completed, now we move the whole thing over in the appropriate
1882 // direction.
1883 cluster.shift(whichEdge, 1);
1884 }
1885
1886 boolean foundSolution = false;
1887 clusterRect = cluster.getBoundingRect();
1888
1889 // Due to the nature of the algorithm, the only check required to verify a valid solution
1890 // is to ensure that completed shifted cluster lies completely within the cell layout.
1891 if (!fail && clusterRect.left >= 0 && clusterRect.right <= mCountX && clusterRect.top >= 0 &&
1892 clusterRect.bottom <= mCountY) {
1893 foundSolution = true;
1894 } else {
1895 currentState.restore();
1896 }
1897
1898 // In either case, we set the occupied array as marked for the location of the views
1899 for (View v: cluster.views) {
1900 CellAndSpan c = currentState.map.get(v);
1901 markCellsForView(c.x, c.y, c.spanX, c.spanY, mTmpOccupied, true);
1902 }
1903
1904 return foundSolution;
Adam Cohene0489502012-08-27 15:18:53 -07001905 }
1906
Adam Cohen482ed822012-03-02 14:15:13 -08001907 private boolean addViewsToTempLocation(ArrayList<View> views, Rect rectOccupiedByPotentialDrop,
Adam Cohenf3900c22012-11-16 18:28:11 -08001908 int[] direction, View dragView, ItemConfiguration currentState) {
Adam Cohen482ed822012-03-02 14:15:13 -08001909 if (views.size() == 0) return true;
Adam Cohen482ed822012-03-02 14:15:13 -08001910
Adam Cohen8baab352012-03-20 17:39:21 -07001911 boolean success = false;
Adam Cohen482ed822012-03-02 14:15:13 -08001912 Rect boundingRect = null;
Adam Cohen8baab352012-03-20 17:39:21 -07001913 // We construct a rect which represents the entire group of views passed in
Adam Cohen482ed822012-03-02 14:15:13 -08001914 for (View v: views) {
Adam Cohen8baab352012-03-20 17:39:21 -07001915 CellAndSpan c = currentState.map.get(v);
Adam Cohen482ed822012-03-02 14:15:13 -08001916 if (boundingRect == null) {
Adam Cohen8baab352012-03-20 17:39:21 -07001917 boundingRect = new Rect(c.x, c.y, c.x + c.spanX, c.y + c.spanY);
Adam Cohen482ed822012-03-02 14:15:13 -08001918 } else {
Adam Cohen8baab352012-03-20 17:39:21 -07001919 boundingRect.union(c.x, c.y, c.x + c.spanX, c.y + c.spanY);
Adam Cohen482ed822012-03-02 14:15:13 -08001920 }
1921 }
Adam Cohen8baab352012-03-20 17:39:21 -07001922
Adam Cohen8baab352012-03-20 17:39:21 -07001923 // Mark the occupied state as false for the group of views we want to move.
Adam Cohenf3900c22012-11-16 18:28:11 -08001924 for (View v: views) {
Adam Cohen8baab352012-03-20 17:39:21 -07001925 CellAndSpan c = currentState.map.get(v);
1926 markCellsForView(c.x, c.y, c.spanX, c.spanY, mTmpOccupied, false);
1927 }
1928
Adam Cohen47a876d2012-03-19 13:21:41 -07001929 boolean[][] blockOccupied = new boolean[boundingRect.width()][boundingRect.height()];
1930 int top = boundingRect.top;
1931 int left = boundingRect.left;
Adam Cohen8baab352012-03-20 17:39:21 -07001932 // We mark more precisely which parts of the bounding rect are truly occupied, allowing
Adam Cohena56dc102012-07-13 13:41:42 -07001933 // for interlocking.
Adam Cohenf3900c22012-11-16 18:28:11 -08001934 for (View v: views) {
Adam Cohen8baab352012-03-20 17:39:21 -07001935 CellAndSpan c = currentState.map.get(v);
1936 markCellsForView(c.x - left, c.y - top, c.spanX, c.spanY, blockOccupied, true);
Adam Cohen47a876d2012-03-19 13:21:41 -07001937 }
1938
Adam Cohen482ed822012-03-02 14:15:13 -08001939 markCellsForRect(rectOccupiedByPotentialDrop, mTmpOccupied, true);
1940
Adam Cohenf3900c22012-11-16 18:28:11 -08001941 findNearestArea(boundingRect.left, boundingRect.top, boundingRect.width(),
1942 boundingRect.height(), direction, mTmpOccupied, blockOccupied, mTempLocation);
Adam Cohen482ed822012-03-02 14:15:13 -08001943
Adam Cohen8baab352012-03-20 17:39:21 -07001944 // If we successfuly found a location by pushing the block of views, we commit it
Adam Cohen482ed822012-03-02 14:15:13 -08001945 if (mTempLocation[0] >= 0 && mTempLocation[1] >= 0) {
Adam Cohen8baab352012-03-20 17:39:21 -07001946 int deltaX = mTempLocation[0] - boundingRect.left;
1947 int deltaY = mTempLocation[1] - boundingRect.top;
Adam Cohenf3900c22012-11-16 18:28:11 -08001948 for (View v: views) {
Adam Cohen8baab352012-03-20 17:39:21 -07001949 CellAndSpan c = currentState.map.get(v);
1950 c.x += deltaX;
1951 c.y += deltaY;
Adam Cohen482ed822012-03-02 14:15:13 -08001952 }
1953 success = true;
1954 }
Adam Cohen8baab352012-03-20 17:39:21 -07001955
1956 // In either case, we set the occupied array as marked for the location of the views
Adam Cohenf3900c22012-11-16 18:28:11 -08001957 for (View v: views) {
Adam Cohen8baab352012-03-20 17:39:21 -07001958 CellAndSpan c = currentState.map.get(v);
1959 markCellsForView(c.x, c.y, c.spanX, c.spanY, mTmpOccupied, true);
Adam Cohen482ed822012-03-02 14:15:13 -08001960 }
1961 return success;
1962 }
1963
1964 private void markCellsForRect(Rect r, boolean[][] occupied, boolean value) {
1965 markCellsForView(r.left, r.top, r.width(), r.height(), occupied, value);
1966 }
1967
Adam Cohen4abc5bd2012-05-29 21:06:03 -07001968 // This method tries to find a reordering solution which satisfies the push mechanic by trying
1969 // to push items in each of the cardinal directions, in an order based on the direction vector
1970 // passed.
1971 private boolean attemptPushInDirection(ArrayList<View> intersectingViews, Rect occupied,
1972 int[] direction, View ignoreView, ItemConfiguration solution) {
1973 if ((Math.abs(direction[0]) + Math.abs(direction[1])) > 1) {
1974 // If the direction vector has two non-zero components, we try pushing
1975 // separately in each of the components.
1976 int temp = direction[1];
1977 direction[1] = 0;
Adam Cohenf3900c22012-11-16 18:28:11 -08001978
1979 if (pushViewsToTempLocation(intersectingViews, occupied, direction,
Adam Cohen4abc5bd2012-05-29 21:06:03 -07001980 ignoreView, solution)) {
1981 return true;
1982 }
1983 direction[1] = temp;
1984 temp = direction[0];
1985 direction[0] = 0;
Adam Cohenf3900c22012-11-16 18:28:11 -08001986
1987 if (pushViewsToTempLocation(intersectingViews, occupied, direction,
Adam Cohen4abc5bd2012-05-29 21:06:03 -07001988 ignoreView, solution)) {
1989 return true;
1990 }
1991 // Revert the direction
1992 direction[0] = temp;
1993
1994 // Now we try pushing in each component of the opposite direction
1995 direction[0] *= -1;
1996 direction[1] *= -1;
1997 temp = direction[1];
1998 direction[1] = 0;
Adam Cohenf3900c22012-11-16 18:28:11 -08001999 if (pushViewsToTempLocation(intersectingViews, occupied, direction,
Adam Cohen4abc5bd2012-05-29 21:06:03 -07002000 ignoreView, solution)) {
2001 return true;
2002 }
2003
2004 direction[1] = temp;
2005 temp = direction[0];
2006 direction[0] = 0;
Adam Cohenf3900c22012-11-16 18:28:11 -08002007 if (pushViewsToTempLocation(intersectingViews, occupied, direction,
Adam Cohen4abc5bd2012-05-29 21:06:03 -07002008 ignoreView, solution)) {
2009 return true;
2010 }
2011 // revert the direction
2012 direction[0] = temp;
2013 direction[0] *= -1;
2014 direction[1] *= -1;
2015
2016 } else {
2017 // If the direction vector has a single non-zero component, we push first in the
2018 // direction of the vector
Adam Cohenf3900c22012-11-16 18:28:11 -08002019 if (pushViewsToTempLocation(intersectingViews, occupied, direction,
Adam Cohen4abc5bd2012-05-29 21:06:03 -07002020 ignoreView, solution)) {
2021 return true;
2022 }
Adam Cohen4abc5bd2012-05-29 21:06:03 -07002023 // Then we try the opposite direction
2024 direction[0] *= -1;
2025 direction[1] *= -1;
Adam Cohenf3900c22012-11-16 18:28:11 -08002026 if (pushViewsToTempLocation(intersectingViews, occupied, direction,
Adam Cohen4abc5bd2012-05-29 21:06:03 -07002027 ignoreView, solution)) {
2028 return true;
2029 }
2030 // Switch the direction back
2031 direction[0] *= -1;
2032 direction[1] *= -1;
2033
2034 // If we have failed to find a push solution with the above, then we try
2035 // to find a solution by pushing along the perpendicular axis.
2036
2037 // Swap the components
2038 int temp = direction[1];
2039 direction[1] = direction[0];
2040 direction[0] = temp;
Adam Cohenf3900c22012-11-16 18:28:11 -08002041 if (pushViewsToTempLocation(intersectingViews, occupied, direction,
Adam Cohen4abc5bd2012-05-29 21:06:03 -07002042 ignoreView, solution)) {
2043 return true;
2044 }
2045
2046 // Then we try the opposite direction
2047 direction[0] *= -1;
2048 direction[1] *= -1;
Adam Cohenf3900c22012-11-16 18:28:11 -08002049 if (pushViewsToTempLocation(intersectingViews, occupied, direction,
Adam Cohen4abc5bd2012-05-29 21:06:03 -07002050 ignoreView, solution)) {
2051 return true;
2052 }
2053 // Switch the direction back
2054 direction[0] *= -1;
2055 direction[1] *= -1;
2056
2057 // Swap the components back
2058 temp = direction[1];
2059 direction[1] = direction[0];
2060 direction[0] = temp;
2061 }
2062 return false;
2063 }
2064
Adam Cohen482ed822012-03-02 14:15:13 -08002065 private boolean rearrangementExists(int cellX, int cellY, int spanX, int spanY, int[] direction,
Adam Cohen8baab352012-03-20 17:39:21 -07002066 View ignoreView, ItemConfiguration solution) {
Winson Chunge3e03bc2012-05-01 15:10:11 -07002067 // Return early if get invalid cell positions
2068 if (cellX < 0 || cellY < 0) return false;
Adam Cohen482ed822012-03-02 14:15:13 -08002069
Adam Cohen8baab352012-03-20 17:39:21 -07002070 mIntersectingViews.clear();
Adam Cohen482ed822012-03-02 14:15:13 -08002071 mOccupiedRect.set(cellX, cellY, cellX + spanX, cellY + spanY);
Adam Cohen482ed822012-03-02 14:15:13 -08002072
Adam Cohen8baab352012-03-20 17:39:21 -07002073 // Mark the desired location of the view currently being dragged.
Adam Cohen482ed822012-03-02 14:15:13 -08002074 if (ignoreView != null) {
Adam Cohen8baab352012-03-20 17:39:21 -07002075 CellAndSpan c = solution.map.get(ignoreView);
Adam Cohen19f37922012-03-21 11:59:11 -07002076 if (c != null) {
2077 c.x = cellX;
2078 c.y = cellY;
2079 }
Adam Cohen482ed822012-03-02 14:15:13 -08002080 }
Adam Cohen482ed822012-03-02 14:15:13 -08002081 Rect r0 = new Rect(cellX, cellY, cellX + spanX, cellY + spanY);
2082 Rect r1 = new Rect();
Adam Cohen8baab352012-03-20 17:39:21 -07002083 for (View child: solution.map.keySet()) {
Adam Cohen482ed822012-03-02 14:15:13 -08002084 if (child == ignoreView) continue;
Adam Cohen8baab352012-03-20 17:39:21 -07002085 CellAndSpan c = solution.map.get(child);
Adam Cohen482ed822012-03-02 14:15:13 -08002086 LayoutParams lp = (LayoutParams) child.getLayoutParams();
Adam Cohen8baab352012-03-20 17:39:21 -07002087 r1.set(c.x, c.y, c.x + c.spanX, c.y + c.spanY);
Adam Cohen482ed822012-03-02 14:15:13 -08002088 if (Rect.intersects(r0, r1)) {
2089 if (!lp.canReorder) {
2090 return false;
2091 }
2092 mIntersectingViews.add(child);
2093 }
2094 }
Adam Cohen47a876d2012-03-19 13:21:41 -07002095
Adam Cohen4abc5bd2012-05-29 21:06:03 -07002096 // First we try to find a solution which respects the push mechanic. That is,
2097 // we try to find a solution such that no displaced item travels through another item
2098 // without also displacing that item.
2099 if (attemptPushInDirection(mIntersectingViews, mOccupiedRect, direction, ignoreView,
Adam Cohen19f37922012-03-21 11:59:11 -07002100 solution)) {
Adam Cohen47a876d2012-03-19 13:21:41 -07002101 return true;
2102 }
Adam Cohen47a876d2012-03-19 13:21:41 -07002103
Adam Cohen4abc5bd2012-05-29 21:06:03 -07002104 // Next we try moving the views as a block, but without requiring the push mechanic.
Adam Cohenf3900c22012-11-16 18:28:11 -08002105 if (addViewsToTempLocation(mIntersectingViews, mOccupiedRect, direction, ignoreView,
Adam Cohen19f37922012-03-21 11:59:11 -07002106 solution)) {
Adam Cohen482ed822012-03-02 14:15:13 -08002107 return true;
2108 }
Adam Cohen47a876d2012-03-19 13:21:41 -07002109
Adam Cohen482ed822012-03-02 14:15:13 -08002110 // Ok, they couldn't move as a block, let's move them individually
2111 for (View v : mIntersectingViews) {
Adam Cohen8baab352012-03-20 17:39:21 -07002112 if (!addViewToTempLocation(v, mOccupiedRect, direction, solution)) {
Adam Cohen482ed822012-03-02 14:15:13 -08002113 return false;
2114 }
2115 }
2116 return true;
2117 }
2118
2119 /*
2120 * Returns a pair (x, y), where x,y are in {-1, 0, 1} corresponding to vector between
2121 * the provided point and the provided cell
2122 */
Adam Cohen47a876d2012-03-19 13:21:41 -07002123 private void computeDirectionVector(float deltaX, float deltaY, int[] result) {
Adam Cohen482ed822012-03-02 14:15:13 -08002124 double angle = Math.atan(((float) deltaY) / deltaX);
2125
2126 result[0] = 0;
2127 result[1] = 0;
2128 if (Math.abs(Math.cos(angle)) > 0.5f) {
2129 result[0] = (int) Math.signum(deltaX);
2130 }
2131 if (Math.abs(Math.sin(angle)) > 0.5f) {
2132 result[1] = (int) Math.signum(deltaY);
2133 }
2134 }
2135
Adam Cohen8baab352012-03-20 17:39:21 -07002136 private void copyOccupiedArray(boolean[][] occupied) {
2137 for (int i = 0; i < mCountX; i++) {
2138 for (int j = 0; j < mCountY; j++) {
2139 occupied[i][j] = mOccupied[i][j];
2140 }
2141 }
2142 }
2143
Adam Cohen482ed822012-03-02 14:15:13 -08002144 ItemConfiguration simpleSwap(int pixelX, int pixelY, int minSpanX, int minSpanY, int spanX,
2145 int spanY, int[] direction, View dragView, boolean decX, ItemConfiguration solution) {
Adam Cohen8baab352012-03-20 17:39:21 -07002146 // Copy the current state into the solution. This solution will be manipulated as necessary.
2147 copyCurrentStateToSolution(solution, false);
2148 // Copy the current occupied array into the temporary occupied array. This array will be
2149 // manipulated as necessary to find a solution.
2150 copyOccupiedArray(mTmpOccupied);
Adam Cohen482ed822012-03-02 14:15:13 -08002151
2152 // We find the nearest cell into which we would place the dragged item, assuming there's
2153 // nothing in its way.
2154 int result[] = new int[2];
2155 result = findNearestArea(pixelX, pixelY, spanX, spanY, result);
2156
2157 boolean success = false;
2158 // First we try the exact nearest position of the item being dragged,
2159 // we will then want to try to move this around to other neighbouring positions
Adam Cohen8baab352012-03-20 17:39:21 -07002160 success = rearrangementExists(result[0], result[1], spanX, spanY, direction, dragView,
2161 solution);
Adam Cohen482ed822012-03-02 14:15:13 -08002162
2163 if (!success) {
2164 // We try shrinking the widget down to size in an alternating pattern, shrink 1 in
2165 // x, then 1 in y etc.
2166 if (spanX > minSpanX && (minSpanY == spanY || decX)) {
2167 return simpleSwap(pixelX, pixelY, minSpanX, minSpanY, spanX - 1, spanY, direction,
2168 dragView, false, solution);
2169 } else if (spanY > minSpanY) {
2170 return simpleSwap(pixelX, pixelY, minSpanX, minSpanY, spanX, spanY - 1, direction,
2171 dragView, true, solution);
2172 }
2173 solution.isSolution = false;
2174 } else {
2175 solution.isSolution = true;
2176 solution.dragViewX = result[0];
2177 solution.dragViewY = result[1];
2178 solution.dragViewSpanX = spanX;
2179 solution.dragViewSpanY = spanY;
Adam Cohen482ed822012-03-02 14:15:13 -08002180 }
2181 return solution;
2182 }
2183
2184 private void copyCurrentStateToSolution(ItemConfiguration solution, boolean temp) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07002185 int childCount = mShortcutsAndWidgets.getChildCount();
Adam Cohen482ed822012-03-02 14:15:13 -08002186 for (int i = 0; i < childCount; i++) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07002187 View child = mShortcutsAndWidgets.getChildAt(i);
Adam Cohen482ed822012-03-02 14:15:13 -08002188 LayoutParams lp = (LayoutParams) child.getLayoutParams();
Adam Cohen8baab352012-03-20 17:39:21 -07002189 CellAndSpan c;
Adam Cohen482ed822012-03-02 14:15:13 -08002190 if (temp) {
Adam Cohen8baab352012-03-20 17:39:21 -07002191 c = new CellAndSpan(lp.tmpCellX, lp.tmpCellY, lp.cellHSpan, lp.cellVSpan);
Adam Cohen482ed822012-03-02 14:15:13 -08002192 } else {
Adam Cohen8baab352012-03-20 17:39:21 -07002193 c = new CellAndSpan(lp.cellX, lp.cellY, lp.cellHSpan, lp.cellVSpan);
Adam Cohen482ed822012-03-02 14:15:13 -08002194 }
Adam Cohenf3900c22012-11-16 18:28:11 -08002195 solution.add(child, c);
Adam Cohen482ed822012-03-02 14:15:13 -08002196 }
2197 }
2198
2199 private void copySolutionToTempState(ItemConfiguration solution, View dragView) {
2200 for (int i = 0; i < mCountX; i++) {
2201 for (int j = 0; j < mCountY; j++) {
2202 mTmpOccupied[i][j] = false;
2203 }
2204 }
2205
Michael Jurkaa52570f2012-03-20 03:18:20 -07002206 int childCount = mShortcutsAndWidgets.getChildCount();
Adam Cohen482ed822012-03-02 14:15:13 -08002207 for (int i = 0; i < childCount; i++) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07002208 View child = mShortcutsAndWidgets.getChildAt(i);
Adam Cohen482ed822012-03-02 14:15:13 -08002209 if (child == dragView) continue;
2210 LayoutParams lp = (LayoutParams) child.getLayoutParams();
Adam Cohen8baab352012-03-20 17:39:21 -07002211 CellAndSpan c = solution.map.get(child);
2212 if (c != null) {
2213 lp.tmpCellX = c.x;
2214 lp.tmpCellY = c.y;
2215 lp.cellHSpan = c.spanX;
2216 lp.cellVSpan = c.spanY;
2217 markCellsForView(c.x, c.y, c.spanX, c.spanY, mTmpOccupied, true);
Adam Cohen482ed822012-03-02 14:15:13 -08002218 }
2219 }
2220 markCellsForView(solution.dragViewX, solution.dragViewY, solution.dragViewSpanX,
2221 solution.dragViewSpanY, mTmpOccupied, true);
2222 }
2223
2224 private void animateItemsToSolution(ItemConfiguration solution, View dragView, boolean
2225 commitDragView) {
2226
2227 boolean[][] occupied = DESTRUCTIVE_REORDER ? mOccupied : mTmpOccupied;
2228 for (int i = 0; i < mCountX; i++) {
2229 for (int j = 0; j < mCountY; j++) {
2230 occupied[i][j] = false;
2231 }
2232 }
2233
Michael Jurkaa52570f2012-03-20 03:18:20 -07002234 int childCount = mShortcutsAndWidgets.getChildCount();
Adam Cohen482ed822012-03-02 14:15:13 -08002235 for (int i = 0; i < childCount; i++) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07002236 View child = mShortcutsAndWidgets.getChildAt(i);
Adam Cohen482ed822012-03-02 14:15:13 -08002237 if (child == dragView) continue;
Adam Cohen8baab352012-03-20 17:39:21 -07002238 CellAndSpan c = solution.map.get(child);
2239 if (c != null) {
Adam Cohen19f37922012-03-21 11:59:11 -07002240 animateChildToPosition(child, c.x, c.y, REORDER_ANIMATION_DURATION, 0,
2241 DESTRUCTIVE_REORDER, false);
Adam Cohen8baab352012-03-20 17:39:21 -07002242 markCellsForView(c.x, c.y, c.spanX, c.spanY, occupied, true);
Adam Cohen482ed822012-03-02 14:15:13 -08002243 }
2244 }
2245 if (commitDragView) {
2246 markCellsForView(solution.dragViewX, solution.dragViewY, solution.dragViewSpanX,
2247 solution.dragViewSpanY, occupied, true);
2248 }
2249 }
2250
Adam Cohen19f37922012-03-21 11:59:11 -07002251 // This method starts or changes the reorder hint animations
2252 private void beginOrAdjustHintAnimations(ItemConfiguration solution, View dragView, int delay) {
2253 int childCount = mShortcutsAndWidgets.getChildCount();
Adam Cohen19f37922012-03-21 11:59:11 -07002254 for (int i = 0; i < childCount; i++) {
2255 View child = mShortcutsAndWidgets.getChildAt(i);
2256 if (child == dragView) continue;
2257 CellAndSpan c = solution.map.get(child);
2258 LayoutParams lp = (LayoutParams) child.getLayoutParams();
2259 if (c != null) {
2260 ReorderHintAnimation rha = new ReorderHintAnimation(child, lp.cellX, lp.cellY,
2261 c.x, c.y, c.spanX, c.spanY);
Adam Cohend024f982012-05-23 18:26:45 -07002262 rha.animate();
Adam Cohen19f37922012-03-21 11:59:11 -07002263 }
2264 }
2265 }
2266
2267 // Class which represents the reorder hint animations. These animations show that an item is
2268 // in a temporary state, and hint at where the item will return to.
2269 class ReorderHintAnimation {
2270 View child;
Adam Cohend024f982012-05-23 18:26:45 -07002271 float finalDeltaX;
2272 float finalDeltaY;
2273 float initDeltaX;
2274 float initDeltaY;
2275 float finalScale;
2276 float initScale;
Brandon Keely50e6e562012-05-08 16:28:49 -07002277 private static final int DURATION = 300;
Adam Cohene7587d22012-05-24 18:50:02 -07002278 Animator a;
Adam Cohen19f37922012-03-21 11:59:11 -07002279
2280 public ReorderHintAnimation(View child, int cellX0, int cellY0, int cellX1, int cellY1,
2281 int spanX, int spanY) {
2282 regionToCenterPoint(cellX0, cellY0, spanX, spanY, mTmpPoint);
2283 final int x0 = mTmpPoint[0];
2284 final int y0 = mTmpPoint[1];
2285 regionToCenterPoint(cellX1, cellY1, spanX, spanY, mTmpPoint);
2286 final int x1 = mTmpPoint[0];
2287 final int y1 = mTmpPoint[1];
2288 final int dX = x1 - x0;
2289 final int dY = y1 - y0;
Adam Cohend024f982012-05-23 18:26:45 -07002290 finalDeltaX = 0;
2291 finalDeltaY = 0;
Adam Cohen19f37922012-03-21 11:59:11 -07002292 if (dX == dY && dX == 0) {
2293 } else {
2294 if (dY == 0) {
Adam Cohend024f982012-05-23 18:26:45 -07002295 finalDeltaX = - Math.signum(dX) * mReorderHintAnimationMagnitude;
Adam Cohen19f37922012-03-21 11:59:11 -07002296 } else if (dX == 0) {
Adam Cohend024f982012-05-23 18:26:45 -07002297 finalDeltaY = - Math.signum(dY) * mReorderHintAnimationMagnitude;
Adam Cohen19f37922012-03-21 11:59:11 -07002298 } else {
2299 double angle = Math.atan( (float) (dY) / dX);
Adam Cohend024f982012-05-23 18:26:45 -07002300 finalDeltaX = (int) (- Math.signum(dX) *
Adam Cohenfe41ac62012-05-23 14:00:37 -07002301 Math.abs(Math.cos(angle) * mReorderHintAnimationMagnitude));
Adam Cohend024f982012-05-23 18:26:45 -07002302 finalDeltaY = (int) (- Math.signum(dY) *
Adam Cohenfe41ac62012-05-23 14:00:37 -07002303 Math.abs(Math.sin(angle) * mReorderHintAnimationMagnitude));
Adam Cohen19f37922012-03-21 11:59:11 -07002304 }
2305 }
Adam Cohend024f982012-05-23 18:26:45 -07002306 initDeltaX = child.getTranslationX();
2307 initDeltaY = child.getTranslationY();
Adam Cohen307fe232012-08-16 17:55:58 -07002308 finalScale = getChildrenScale() - 4.0f / child.getWidth();
Adam Cohend024f982012-05-23 18:26:45 -07002309 initScale = child.getScaleX();
Adam Cohen19f37922012-03-21 11:59:11 -07002310 this.child = child;
2311 }
2312
Adam Cohend024f982012-05-23 18:26:45 -07002313 void animate() {
Adam Cohen19f37922012-03-21 11:59:11 -07002314 if (mShakeAnimators.containsKey(child)) {
2315 ReorderHintAnimation oldAnimation = mShakeAnimators.get(child);
Adam Cohend024f982012-05-23 18:26:45 -07002316 oldAnimation.cancel();
Adam Cohen19f37922012-03-21 11:59:11 -07002317 mShakeAnimators.remove(child);
Adam Cohene7587d22012-05-24 18:50:02 -07002318 if (finalDeltaX == 0 && finalDeltaY == 0) {
2319 completeAnimationImmediately();
2320 return;
2321 }
Adam Cohen19f37922012-03-21 11:59:11 -07002322 }
Adam Cohend024f982012-05-23 18:26:45 -07002323 if (finalDeltaX == 0 && finalDeltaY == 0) {
Adam Cohen19f37922012-03-21 11:59:11 -07002324 return;
2325 }
Michael Jurka2ecf9952012-06-18 12:52:28 -07002326 ValueAnimator va = LauncherAnimUtils.ofFloat(0f, 1f);
Adam Cohene7587d22012-05-24 18:50:02 -07002327 a = va;
Adam Cohen19f37922012-03-21 11:59:11 -07002328 va.setRepeatMode(ValueAnimator.REVERSE);
2329 va.setRepeatCount(ValueAnimator.INFINITE);
Adam Cohen7bdfc972012-05-22 16:50:35 -07002330 va.setDuration(DURATION);
Adam Cohend024f982012-05-23 18:26:45 -07002331 va.setStartDelay((int) (Math.random() * 60));
Adam Cohen19f37922012-03-21 11:59:11 -07002332 va.addUpdateListener(new AnimatorUpdateListener() {
2333 @Override
2334 public void onAnimationUpdate(ValueAnimator animation) {
2335 float r = ((Float) animation.getAnimatedValue()).floatValue();
Adam Cohend024f982012-05-23 18:26:45 -07002336 float x = r * finalDeltaX + (1 - r) * initDeltaX;
2337 float y = r * finalDeltaY + (1 - r) * initDeltaY;
Adam Cohen19f37922012-03-21 11:59:11 -07002338 child.setTranslationX(x);
2339 child.setTranslationY(y);
Adam Cohend024f982012-05-23 18:26:45 -07002340 float s = r * finalScale + (1 - r) * initScale;
Brandon Keely50e6e562012-05-08 16:28:49 -07002341 child.setScaleX(s);
2342 child.setScaleY(s);
Adam Cohen19f37922012-03-21 11:59:11 -07002343 }
2344 });
2345 va.addListener(new AnimatorListenerAdapter() {
2346 public void onAnimationRepeat(Animator animation) {
Adam Cohen19f37922012-03-21 11:59:11 -07002347 // We make sure to end only after a full period
Adam Cohend024f982012-05-23 18:26:45 -07002348 initDeltaX = 0;
2349 initDeltaY = 0;
Adam Cohen307fe232012-08-16 17:55:58 -07002350 initScale = getChildrenScale();
Adam Cohen19f37922012-03-21 11:59:11 -07002351 }
2352 });
Adam Cohen19f37922012-03-21 11:59:11 -07002353 mShakeAnimators.put(child, this);
2354 va.start();
2355 }
2356
Adam Cohend024f982012-05-23 18:26:45 -07002357 private void cancel() {
Adam Cohene7587d22012-05-24 18:50:02 -07002358 if (a != null) {
2359 a.cancel();
2360 }
Adam Cohen19f37922012-03-21 11:59:11 -07002361 }
Adam Cohene7587d22012-05-24 18:50:02 -07002362
Brandon Keely50e6e562012-05-08 16:28:49 -07002363 private void completeAnimationImmediately() {
Adam Cohene7587d22012-05-24 18:50:02 -07002364 if (a != null) {
2365 a.cancel();
2366 }
Brandon Keely50e6e562012-05-08 16:28:49 -07002367
Michael Jurka2ecf9952012-06-18 12:52:28 -07002368 AnimatorSet s = LauncherAnimUtils.createAnimatorSet();
Adam Cohene7587d22012-05-24 18:50:02 -07002369 a = s;
Brandon Keely50e6e562012-05-08 16:28:49 -07002370 s.playTogether(
Adam Cohen307fe232012-08-16 17:55:58 -07002371 LauncherAnimUtils.ofFloat(child, "scaleX", getChildrenScale()),
2372 LauncherAnimUtils.ofFloat(child, "scaleY", getChildrenScale()),
Michael Jurka2ecf9952012-06-18 12:52:28 -07002373 LauncherAnimUtils.ofFloat(child, "translationX", 0f),
2374 LauncherAnimUtils.ofFloat(child, "translationY", 0f)
Brandon Keely50e6e562012-05-08 16:28:49 -07002375 );
2376 s.setDuration(REORDER_ANIMATION_DURATION);
2377 s.setInterpolator(new android.view.animation.DecelerateInterpolator(1.5f));
2378 s.start();
2379 }
Adam Cohen19f37922012-03-21 11:59:11 -07002380 }
2381
2382 private void completeAndClearReorderHintAnimations() {
2383 for (ReorderHintAnimation a: mShakeAnimators.values()) {
Brandon Keely50e6e562012-05-08 16:28:49 -07002384 a.completeAnimationImmediately();
Adam Cohen19f37922012-03-21 11:59:11 -07002385 }
2386 mShakeAnimators.clear();
2387 }
2388
Adam Cohen482ed822012-03-02 14:15:13 -08002389 private void commitTempPlacement() {
2390 for (int i = 0; i < mCountX; i++) {
2391 for (int j = 0; j < mCountY; j++) {
2392 mOccupied[i][j] = mTmpOccupied[i][j];
2393 }
2394 }
Michael Jurkaa52570f2012-03-20 03:18:20 -07002395 int childCount = mShortcutsAndWidgets.getChildCount();
Adam Cohen482ed822012-03-02 14:15:13 -08002396 for (int i = 0; i < childCount; i++) {
Adam Cohenea889a22012-03-27 16:45:39 -07002397 View child = mShortcutsAndWidgets.getChildAt(i);
2398 LayoutParams lp = (LayoutParams) child.getLayoutParams();
2399 ItemInfo info = (ItemInfo) child.getTag();
Adam Cohen2acce882012-03-28 19:03:19 -07002400 // We do a null check here because the item info can be null in the case of the
2401 // AllApps button in the hotseat.
2402 if (info != null) {
Adam Cohen487f7dd2012-06-28 18:12:10 -07002403 if (info.cellX != lp.tmpCellX || info.cellY != lp.tmpCellY ||
2404 info.spanX != lp.cellHSpan || info.spanY != lp.cellVSpan) {
2405 info.requiresDbUpdate = true;
2406 }
Adam Cohen2acce882012-03-28 19:03:19 -07002407 info.cellX = lp.cellX = lp.tmpCellX;
2408 info.cellY = lp.cellY = lp.tmpCellY;
Adam Cohenbebf0422012-04-11 18:06:28 -07002409 info.spanX = lp.cellHSpan;
2410 info.spanY = lp.cellVSpan;
Adam Cohen2acce882012-03-28 19:03:19 -07002411 }
Adam Cohen482ed822012-03-02 14:15:13 -08002412 }
Adam Cohen2acce882012-03-28 19:03:19 -07002413 mLauncher.getWorkspace().updateItemLocationsInDatabase(this);
Adam Cohen482ed822012-03-02 14:15:13 -08002414 }
2415
2416 public void setUseTempCoords(boolean useTempCoords) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07002417 int childCount = mShortcutsAndWidgets.getChildCount();
Adam Cohen482ed822012-03-02 14:15:13 -08002418 for (int i = 0; i < childCount; i++) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07002419 LayoutParams lp = (LayoutParams) mShortcutsAndWidgets.getChildAt(i).getLayoutParams();
Adam Cohen482ed822012-03-02 14:15:13 -08002420 lp.useTmpCoords = useTempCoords;
2421 }
2422 }
2423
Adam Cohen482ed822012-03-02 14:15:13 -08002424 ItemConfiguration findConfigurationNoShuffle(int pixelX, int pixelY, int minSpanX, int minSpanY,
2425 int spanX, int spanY, View dragView, ItemConfiguration solution) {
2426 int[] result = new int[2];
2427 int[] resultSpan = new int[2];
2428 findNearestVacantArea(pixelX, pixelY, minSpanX, minSpanY, spanX, spanY, null, result,
2429 resultSpan);
2430 if (result[0] >= 0 && result[1] >= 0) {
2431 copyCurrentStateToSolution(solution, false);
2432 solution.dragViewX = result[0];
2433 solution.dragViewY = result[1];
2434 solution.dragViewSpanX = resultSpan[0];
2435 solution.dragViewSpanY = resultSpan[1];
2436 solution.isSolution = true;
2437 } else {
2438 solution.isSolution = false;
2439 }
2440 return solution;
2441 }
2442
2443 public void prepareChildForDrag(View child) {
2444 markCellsAsUnoccupiedForView(child);
Adam Cohen482ed822012-03-02 14:15:13 -08002445 }
2446
Adam Cohen19f37922012-03-21 11:59:11 -07002447 /* This seems like it should be obvious and straight-forward, but when the direction vector
2448 needs to match with the notion of the dragView pushing other views, we have to employ
2449 a slightly more subtle notion of the direction vector. The question is what two points is
2450 the vector between? The center of the dragView and its desired destination? Not quite, as
2451 this doesn't necessarily coincide with the interaction of the dragView and items occupying
2452 those cells. Instead we use some heuristics to often lock the vector to up, down, left
2453 or right, which helps make pushing feel right.
2454 */
2455 private void getDirectionVectorForDrop(int dragViewCenterX, int dragViewCenterY, int spanX,
2456 int spanY, View dragView, int[] resultDirection) {
2457 int[] targetDestination = new int[2];
2458
2459 findNearestArea(dragViewCenterX, dragViewCenterY, spanX, spanY, targetDestination);
2460 Rect dragRect = new Rect();
2461 regionToRect(targetDestination[0], targetDestination[1], spanX, spanY, dragRect);
2462 dragRect.offset(dragViewCenterX - dragRect.centerX(), dragViewCenterY - dragRect.centerY());
2463
2464 Rect dropRegionRect = new Rect();
2465 getViewsIntersectingRegion(targetDestination[0], targetDestination[1], spanX, spanY,
2466 dragView, dropRegionRect, mIntersectingViews);
2467
2468 int dropRegionSpanX = dropRegionRect.width();
2469 int dropRegionSpanY = dropRegionRect.height();
2470
2471 regionToRect(dropRegionRect.left, dropRegionRect.top, dropRegionRect.width(),
2472 dropRegionRect.height(), dropRegionRect);
2473
2474 int deltaX = (dropRegionRect.centerX() - dragViewCenterX) / spanX;
2475 int deltaY = (dropRegionRect.centerY() - dragViewCenterY) / spanY;
2476
2477 if (dropRegionSpanX == mCountX || spanX == mCountX) {
2478 deltaX = 0;
2479 }
2480 if (dropRegionSpanY == mCountY || spanY == mCountY) {
2481 deltaY = 0;
2482 }
2483
2484 if (deltaX == 0 && deltaY == 0) {
2485 // No idea what to do, give a random direction.
2486 resultDirection[0] = 1;
2487 resultDirection[1] = 0;
2488 } else {
2489 computeDirectionVector(deltaX, deltaY, resultDirection);
2490 }
2491 }
2492
2493 // For a given cell and span, fetch the set of views intersecting the region.
2494 private void getViewsIntersectingRegion(int cellX, int cellY, int spanX, int spanY,
2495 View dragView, Rect boundingRect, ArrayList<View> intersectingViews) {
2496 if (boundingRect != null) {
2497 boundingRect.set(cellX, cellY, cellX + spanX, cellY + spanY);
2498 }
2499 intersectingViews.clear();
2500 Rect r0 = new Rect(cellX, cellY, cellX + spanX, cellY + spanY);
2501 Rect r1 = new Rect();
2502 final int count = mShortcutsAndWidgets.getChildCount();
2503 for (int i = 0; i < count; i++) {
2504 View child = mShortcutsAndWidgets.getChildAt(i);
2505 if (child == dragView) continue;
2506 LayoutParams lp = (LayoutParams) child.getLayoutParams();
2507 r1.set(lp.cellX, lp.cellY, lp.cellX + lp.cellHSpan, lp.cellY + lp.cellVSpan);
2508 if (Rect.intersects(r0, r1)) {
2509 mIntersectingViews.add(child);
2510 if (boundingRect != null) {
2511 boundingRect.union(r1);
2512 }
2513 }
2514 }
2515 }
2516
2517 boolean isNearestDropLocationOccupied(int pixelX, int pixelY, int spanX, int spanY,
2518 View dragView, int[] result) {
2519 result = findNearestArea(pixelX, pixelY, spanX, spanY, result);
2520 getViewsIntersectingRegion(result[0], result[1], spanX, spanY, dragView, null,
2521 mIntersectingViews);
2522 return !mIntersectingViews.isEmpty();
2523 }
2524
2525 void revertTempState() {
2526 if (!isItemPlacementDirty() || DESTRUCTIVE_REORDER) return;
2527 final int count = mShortcutsAndWidgets.getChildCount();
2528 for (int i = 0; i < count; i++) {
2529 View child = mShortcutsAndWidgets.getChildAt(i);
2530 LayoutParams lp = (LayoutParams) child.getLayoutParams();
2531 if (lp.tmpCellX != lp.cellX || lp.tmpCellY != lp.cellY) {
2532 lp.tmpCellX = lp.cellX;
2533 lp.tmpCellY = lp.cellY;
2534 animateChildToPosition(child, lp.cellX, lp.cellY, REORDER_ANIMATION_DURATION,
2535 0, false, false);
2536 }
2537 }
2538 completeAndClearReorderHintAnimations();
2539 setItemPlacementDirty(false);
2540 }
2541
Adam Cohenbebf0422012-04-11 18:06:28 -07002542 boolean createAreaForResize(int cellX, int cellY, int spanX, int spanY,
2543 View dragView, int[] direction, boolean commit) {
2544 int[] pixelXY = new int[2];
2545 regionToCenterPoint(cellX, cellY, spanX, spanY, pixelXY);
2546
2547 // First we determine if things have moved enough to cause a different layout
2548 ItemConfiguration swapSolution = simpleSwap(pixelXY[0], pixelXY[1], spanX, spanY,
2549 spanX, spanY, direction, dragView, true, new ItemConfiguration());
2550
2551 setUseTempCoords(true);
2552 if (swapSolution != null && swapSolution.isSolution) {
2553 // If we're just testing for a possible location (MODE_ACCEPT_DROP), we don't bother
2554 // committing anything or animating anything as we just want to determine if a solution
2555 // exists
2556 copySolutionToTempState(swapSolution, dragView);
2557 setItemPlacementDirty(true);
2558 animateItemsToSolution(swapSolution, dragView, commit);
2559
2560 if (commit) {
2561 commitTempPlacement();
2562 completeAndClearReorderHintAnimations();
2563 setItemPlacementDirty(false);
2564 } else {
2565 beginOrAdjustHintAnimations(swapSolution, dragView,
2566 REORDER_ANIMATION_DURATION);
2567 }
2568 mShortcutsAndWidgets.requestLayout();
2569 }
2570 return swapSolution.isSolution;
2571 }
2572
Adam Cohen482ed822012-03-02 14:15:13 -08002573 int[] createArea(int pixelX, int pixelY, int minSpanX, int minSpanY, int spanX, int spanY,
2574 View dragView, int[] result, int resultSpan[], int mode) {
Adam Cohen482ed822012-03-02 14:15:13 -08002575 // First we determine if things have moved enough to cause a different layout
Adam Cohen47a876d2012-03-19 13:21:41 -07002576 result = findNearestArea(pixelX, pixelY, spanX, spanY, result);
Adam Cohen482ed822012-03-02 14:15:13 -08002577
2578 if (resultSpan == null) {
2579 resultSpan = new int[2];
2580 }
2581
Adam Cohen19f37922012-03-21 11:59:11 -07002582 // When we are checking drop validity or actually dropping, we don't recompute the
2583 // direction vector, since we want the solution to match the preview, and it's possible
2584 // that the exact position of the item has changed to result in a new reordering outcome.
Adam Cohenb209e632012-03-27 17:09:36 -07002585 if ((mode == MODE_ON_DROP || mode == MODE_ON_DROP_EXTERNAL || mode == MODE_ACCEPT_DROP)
2586 && mPreviousReorderDirection[0] != INVALID_DIRECTION) {
Adam Cohen19f37922012-03-21 11:59:11 -07002587 mDirectionVector[0] = mPreviousReorderDirection[0];
2588 mDirectionVector[1] = mPreviousReorderDirection[1];
2589 // We reset this vector after drop
Adam Cohenb209e632012-03-27 17:09:36 -07002590 if (mode == MODE_ON_DROP || mode == MODE_ON_DROP_EXTERNAL) {
2591 mPreviousReorderDirection[0] = INVALID_DIRECTION;
2592 mPreviousReorderDirection[1] = INVALID_DIRECTION;
Adam Cohen19f37922012-03-21 11:59:11 -07002593 }
2594 } else {
2595 getDirectionVectorForDrop(pixelX, pixelY, spanX, spanY, dragView, mDirectionVector);
2596 mPreviousReorderDirection[0] = mDirectionVector[0];
2597 mPreviousReorderDirection[1] = mDirectionVector[1];
2598 }
2599
Adam Cohen482ed822012-03-02 14:15:13 -08002600 ItemConfiguration swapSolution = simpleSwap(pixelX, pixelY, minSpanX, minSpanY,
2601 spanX, spanY, mDirectionVector, dragView, true, new ItemConfiguration());
2602
2603 // We attempt the approach which doesn't shuffle views at all
2604 ItemConfiguration noShuffleSolution = findConfigurationNoShuffle(pixelX, pixelY, minSpanX,
2605 minSpanY, spanX, spanY, dragView, new ItemConfiguration());
2606
2607 ItemConfiguration finalSolution = null;
2608 if (swapSolution.isSolution && swapSolution.area() >= noShuffleSolution.area()) {
2609 finalSolution = swapSolution;
2610 } else if (noShuffleSolution.isSolution) {
2611 finalSolution = noShuffleSolution;
2612 }
2613
2614 boolean foundSolution = true;
2615 if (!DESTRUCTIVE_REORDER) {
2616 setUseTempCoords(true);
2617 }
2618
2619 if (finalSolution != null) {
2620 result[0] = finalSolution.dragViewX;
2621 result[1] = finalSolution.dragViewY;
2622 resultSpan[0] = finalSolution.dragViewSpanX;
2623 resultSpan[1] = finalSolution.dragViewSpanY;
2624
2625 // If we're just testing for a possible location (MODE_ACCEPT_DROP), we don't bother
2626 // committing anything or animating anything as we just want to determine if a solution
2627 // exists
2628 if (mode == MODE_DRAG_OVER || mode == MODE_ON_DROP || mode == MODE_ON_DROP_EXTERNAL) {
2629 if (!DESTRUCTIVE_REORDER) {
2630 copySolutionToTempState(finalSolution, dragView);
2631 }
2632 setItemPlacementDirty(true);
2633 animateItemsToSolution(finalSolution, dragView, mode == MODE_ON_DROP);
2634
Adam Cohen19f37922012-03-21 11:59:11 -07002635 if (!DESTRUCTIVE_REORDER &&
2636 (mode == MODE_ON_DROP || mode == MODE_ON_DROP_EXTERNAL)) {
Adam Cohen482ed822012-03-02 14:15:13 -08002637 commitTempPlacement();
Adam Cohen19f37922012-03-21 11:59:11 -07002638 completeAndClearReorderHintAnimations();
2639 setItemPlacementDirty(false);
2640 } else {
2641 beginOrAdjustHintAnimations(finalSolution, dragView,
2642 REORDER_ANIMATION_DURATION);
Adam Cohen482ed822012-03-02 14:15:13 -08002643 }
2644 }
2645 } else {
2646 foundSolution = false;
2647 result[0] = result[1] = resultSpan[0] = resultSpan[1] = -1;
2648 }
2649
2650 if ((mode == MODE_ON_DROP || !foundSolution) && !DESTRUCTIVE_REORDER) {
2651 setUseTempCoords(false);
2652 }
Adam Cohen482ed822012-03-02 14:15:13 -08002653
Michael Jurkaa52570f2012-03-20 03:18:20 -07002654 mShortcutsAndWidgets.requestLayout();
Adam Cohen482ed822012-03-02 14:15:13 -08002655 return result;
2656 }
2657
Adam Cohen19f37922012-03-21 11:59:11 -07002658 void setItemPlacementDirty(boolean dirty) {
2659 mItemPlacementDirty = dirty;
Adam Cohen482ed822012-03-02 14:15:13 -08002660 }
Adam Cohen19f37922012-03-21 11:59:11 -07002661 boolean isItemPlacementDirty() {
2662 return mItemPlacementDirty;
Adam Cohen482ed822012-03-02 14:15:13 -08002663 }
2664
2665 private class ItemConfiguration {
Adam Cohen8baab352012-03-20 17:39:21 -07002666 HashMap<View, CellAndSpan> map = new HashMap<View, CellAndSpan>();
Adam Cohenf3900c22012-11-16 18:28:11 -08002667 private HashMap<View, CellAndSpan> savedMap = new HashMap<View, CellAndSpan>();
2668 ArrayList<View> sortedViews = new ArrayList<View>();
Adam Cohen482ed822012-03-02 14:15:13 -08002669 boolean isSolution = false;
2670 int dragViewX, dragViewY, dragViewSpanX, dragViewSpanY;
2671
Adam Cohenf3900c22012-11-16 18:28:11 -08002672 void save() {
2673 // Copy current state into savedMap
2674 for (View v: map.keySet()) {
2675 map.get(v).copy(savedMap.get(v));
2676 }
2677 }
2678
2679 void restore() {
2680 // Restore current state from savedMap
2681 for (View v: savedMap.keySet()) {
2682 savedMap.get(v).copy(map.get(v));
2683 }
2684 }
2685
2686 void add(View v, CellAndSpan cs) {
2687 map.put(v, cs);
2688 savedMap.put(v, new CellAndSpan());
2689 sortedViews.add(v);
2690 }
2691
Adam Cohen482ed822012-03-02 14:15:13 -08002692 int area() {
2693 return dragViewSpanX * dragViewSpanY;
2694 }
Adam Cohen8baab352012-03-20 17:39:21 -07002695 }
2696
2697 private class CellAndSpan {
2698 int x, y;
2699 int spanX, spanY;
2700
Adam Cohenf3900c22012-11-16 18:28:11 -08002701 public CellAndSpan() {
2702 }
2703
2704 public void copy(CellAndSpan copy) {
2705 copy.x = x;
2706 copy.y = y;
2707 copy.spanX = spanX;
2708 copy.spanY = spanY;
2709 }
2710
Adam Cohen8baab352012-03-20 17:39:21 -07002711 public CellAndSpan(int x, int y, int spanX, int spanY) {
2712 this.x = x;
2713 this.y = y;
2714 this.spanX = spanX;
2715 this.spanY = spanY;
Adam Cohen482ed822012-03-02 14:15:13 -08002716 }
Adam Cohenf3900c22012-11-16 18:28:11 -08002717
2718 public String toString() {
2719 return "(" + x + ", " + y + ": " + spanX + ", " + spanY + ")";
2720 }
2721
Adam Cohen482ed822012-03-02 14:15:13 -08002722 }
2723
Adam Cohendf035382011-04-11 17:22:04 -07002724 /**
2725 * Find a vacant area that will fit the given bounds nearest the requested
2726 * cell location. Uses Euclidean distance to score multiple vacant areas.
2727 *
2728 * @param pixelX The X location at which you want to search for a vacant area.
2729 * @param pixelY The Y location at which you want to search for a vacant area.
2730 * @param spanX Horizontal span of the object.
2731 * @param spanY Vertical span of the object.
2732 * @param ignoreView Considers space occupied by this view as unoccupied
2733 * @param result Previously returned value to possibly recycle.
2734 * @return The X, Y cell of a vacant area that can contain this object,
2735 * nearest the requested location.
2736 */
2737 int[] findNearestVacantArea(
2738 int pixelX, int pixelY, int spanX, int spanY, View ignoreView, int[] result) {
2739 return findNearestArea(pixelX, pixelY, spanX, spanY, ignoreView, true, result);
2740 }
2741
2742 /**
Adam Cohend41fbf52012-02-16 23:53:59 -08002743 * Find a vacant area that will fit the given bounds nearest the requested
2744 * cell location. Uses Euclidean distance to score multiple vacant areas.
2745 *
2746 * @param pixelX The X location at which you want to search for a vacant area.
2747 * @param pixelY The Y location at which you want to search for a vacant area.
2748 * @param minSpanX The minimum horizontal span required
2749 * @param minSpanY The minimum vertical span required
2750 * @param spanX Horizontal span of the object.
2751 * @param spanY Vertical span of the object.
2752 * @param ignoreView Considers space occupied by this view as unoccupied
2753 * @param result Previously returned value to possibly recycle.
2754 * @return The X, Y cell of a vacant area that can contain this object,
2755 * nearest the requested location.
2756 */
2757 int[] findNearestVacantArea(int pixelX, int pixelY, int minSpanX, int minSpanY,
2758 int spanX, int spanY, View ignoreView, int[] result, int[] resultSpan) {
Adam Cohen482ed822012-03-02 14:15:13 -08002759 return findNearestArea(pixelX, pixelY, minSpanX, minSpanY, spanX, spanY, ignoreView, true,
2760 result, resultSpan, mOccupied);
Adam Cohend41fbf52012-02-16 23:53:59 -08002761 }
2762
2763 /**
Adam Cohendf035382011-04-11 17:22:04 -07002764 * Find a starting cell position that will fit the given bounds nearest the requested
2765 * cell location. Uses Euclidean distance to score multiple vacant areas.
2766 *
2767 * @param pixelX The X location at which you want to search for a vacant area.
2768 * @param pixelY The Y location at which you want to search for a vacant area.
2769 * @param spanX Horizontal span of the object.
2770 * @param spanY Vertical span of the object.
2771 * @param ignoreView Considers space occupied by this view as unoccupied
2772 * @param result Previously returned value to possibly recycle.
2773 * @return The X, Y cell of a vacant area that can contain this object,
2774 * nearest the requested location.
2775 */
2776 int[] findNearestArea(
2777 int pixelX, int pixelY, int spanX, int spanY, int[] result) {
2778 return findNearestArea(pixelX, pixelY, spanX, spanY, null, false, result);
2779 }
2780
Michael Jurka0280c3b2010-09-17 15:00:07 -07002781 boolean existsEmptyCell() {
2782 return findCellForSpan(null, 1, 1);
2783 }
2784
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002785 /**
Michael Jurka0280c3b2010-09-17 15:00:07 -07002786 * Finds the upper-left coordinate of the first rectangle in the grid that can
2787 * hold a cell of the specified dimensions. If intersectX and intersectY are not -1,
2788 * then this method will only return coordinates for rectangles that contain the cell
2789 * (intersectX, intersectY)
2790 *
2791 * @param cellXY The array that will contain the position of a vacant cell if such a cell
2792 * can be found.
2793 * @param spanX The horizontal span of the cell we want to find.
2794 * @param spanY The vertical span of the cell we want to find.
2795 *
2796 * @return True if a vacant cell of the specified dimension was found, false otherwise.
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07002797 */
Michael Jurka0280c3b2010-09-17 15:00:07 -07002798 boolean findCellForSpan(int[] cellXY, int spanX, int spanY) {
Adam Cohen482ed822012-03-02 14:15:13 -08002799 return findCellForSpanThatIntersectsIgnoring(cellXY, spanX, spanY, -1, -1, null, mOccupied);
Michael Jurka0280c3b2010-09-17 15:00:07 -07002800 }
2801
2802 /**
2803 * Like above, but ignores any cells occupied by the item "ignoreView"
2804 *
2805 * @param cellXY The array that will contain the position of a vacant cell if such a cell
2806 * can be found.
2807 * @param spanX The horizontal span of the cell we want to find.
2808 * @param spanY The vertical span of the cell we want to find.
2809 * @param ignoreView The home screen item we should treat as not occupying any space
2810 * @return
2811 */
2812 boolean findCellForSpanIgnoring(int[] cellXY, int spanX, int spanY, View ignoreView) {
Adam Cohen482ed822012-03-02 14:15:13 -08002813 return findCellForSpanThatIntersectsIgnoring(cellXY, spanX, spanY, -1, -1,
2814 ignoreView, mOccupied);
Michael Jurka0280c3b2010-09-17 15:00:07 -07002815 }
2816
2817 /**
2818 * Like above, but if intersectX and intersectY are not -1, then this method will try to
2819 * return coordinates for rectangles that contain the cell [intersectX, intersectY]
2820 *
2821 * @param spanX The horizontal span of the cell we want to find.
2822 * @param spanY The vertical span of the cell we want to find.
2823 * @param ignoreView The home screen item we should treat as not occupying any space
2824 * @param intersectX The X coordinate of the cell that we should try to overlap
2825 * @param intersectX The Y coordinate of the cell that we should try to overlap
2826 *
2827 * @return True if a vacant cell of the specified dimension was found, false otherwise.
2828 */
2829 boolean findCellForSpanThatIntersects(int[] cellXY, int spanX, int spanY,
2830 int intersectX, int intersectY) {
2831 return findCellForSpanThatIntersectsIgnoring(
Adam Cohen482ed822012-03-02 14:15:13 -08002832 cellXY, spanX, spanY, intersectX, intersectY, null, mOccupied);
Michael Jurka0280c3b2010-09-17 15:00:07 -07002833 }
2834
2835 /**
2836 * The superset of the above two methods
2837 */
2838 boolean findCellForSpanThatIntersectsIgnoring(int[] cellXY, int spanX, int spanY,
Adam Cohen482ed822012-03-02 14:15:13 -08002839 int intersectX, int intersectY, View ignoreView, boolean occupied[][]) {
Michael Jurkac6ee42e2010-09-30 12:04:50 -07002840 // mark space take by ignoreView as available (method checks if ignoreView is null)
Adam Cohen482ed822012-03-02 14:15:13 -08002841 markCellsAsUnoccupiedForView(ignoreView, occupied);
Michael Jurka0280c3b2010-09-17 15:00:07 -07002842
Michael Jurka28750fb2010-09-24 17:43:49 -07002843 boolean foundCell = false;
Michael Jurka0280c3b2010-09-17 15:00:07 -07002844 while (true) {
2845 int startX = 0;
2846 if (intersectX >= 0) {
2847 startX = Math.max(startX, intersectX - (spanX - 1));
2848 }
2849 int endX = mCountX - (spanX - 1);
2850 if (intersectX >= 0) {
2851 endX = Math.min(endX, intersectX + (spanX - 1) + (spanX == 1 ? 1 : 0));
2852 }
2853 int startY = 0;
2854 if (intersectY >= 0) {
2855 startY = Math.max(startY, intersectY - (spanY - 1));
2856 }
2857 int endY = mCountY - (spanY - 1);
2858 if (intersectY >= 0) {
2859 endY = Math.min(endY, intersectY + (spanY - 1) + (spanY == 1 ? 1 : 0));
2860 }
2861
Winson Chungbbc60d82010-11-11 16:34:41 -08002862 for (int y = startY; y < endY && !foundCell; y++) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002863 inner:
Winson Chungbbc60d82010-11-11 16:34:41 -08002864 for (int x = startX; x < endX; x++) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002865 for (int i = 0; i < spanX; i++) {
2866 for (int j = 0; j < spanY; j++) {
Adam Cohen482ed822012-03-02 14:15:13 -08002867 if (occupied[x + i][y + j]) {
Winson Chungbbc60d82010-11-11 16:34:41 -08002868 // small optimization: we can skip to after the column we just found
Michael Jurka0280c3b2010-09-17 15:00:07 -07002869 // an occupied cell
Winson Chungbbc60d82010-11-11 16:34:41 -08002870 x += i;
Michael Jurka0280c3b2010-09-17 15:00:07 -07002871 continue inner;
2872 }
2873 }
2874 }
2875 if (cellXY != null) {
2876 cellXY[0] = x;
2877 cellXY[1] = y;
2878 }
Michael Jurka28750fb2010-09-24 17:43:49 -07002879 foundCell = true;
2880 break;
Michael Jurka0280c3b2010-09-17 15:00:07 -07002881 }
2882 }
2883 if (intersectX == -1 && intersectY == -1) {
2884 break;
2885 } else {
2886 // if we failed to find anything, try again but without any requirements of
2887 // intersecting
2888 intersectX = -1;
2889 intersectY = -1;
2890 continue;
2891 }
2892 }
2893
Michael Jurkac6ee42e2010-09-30 12:04:50 -07002894 // re-mark space taken by ignoreView as occupied
Adam Cohen482ed822012-03-02 14:15:13 -08002895 markCellsAsOccupiedForView(ignoreView, occupied);
Michael Jurka28750fb2010-09-24 17:43:49 -07002896 return foundCell;
Michael Jurka0280c3b2010-09-17 15:00:07 -07002897 }
2898
2899 /**
Winson Chungc07918d2011-07-01 15:35:26 -07002900 * A drag event has begun over this layout.
2901 * It may have begun over this layout (in which case onDragChild is called first),
2902 * or it may have begun on another layout.
2903 */
2904 void onDragEnter() {
Adam Cohenc6cc61d2012-04-04 12:47:08 -07002905 mDragEnforcer.onDragEnter();
Winson Chungc07918d2011-07-01 15:35:26 -07002906 mDragging = true;
2907 }
2908
2909 /**
Michael Jurka0280c3b2010-09-17 15:00:07 -07002910 * Called when drag has left this CellLayout or has been completed (successfully or not)
2911 */
2912 void onDragExit() {
Adam Cohenc6cc61d2012-04-04 12:47:08 -07002913 mDragEnforcer.onDragExit();
Joe Onorato4be866d2010-10-10 11:26:02 -07002914 // This can actually be called when we aren't in a drag, e.g. when adding a new
2915 // item to this layout via the customize drawer.
2916 // Guard against that case.
2917 if (mDragging) {
2918 mDragging = false;
Patrick Dubroyde7658b2010-09-27 11:15:43 -07002919 }
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -07002920
2921 // Invalidate the drag data
Adam Cohend41fbf52012-02-16 23:53:59 -08002922 mDragCell[0] = mDragCell[1] = -1;
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -07002923 mDragOutlineAnims[mDragOutlineCurrent].animateOut();
2924 mDragOutlineCurrent = (mDragOutlineCurrent + 1) % mDragOutlineAnims.length;
Adam Cohen19f37922012-03-21 11:59:11 -07002925 revertTempState();
Michael Jurka33945b22010-12-21 18:19:38 -08002926 setIsDragOverlapping(false);
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07002927 }
2928
2929 /**
Winson Chungaafa03c2010-06-11 17:34:16 -07002930 * Mark a child as having been dropped.
Patrick Dubroyde7658b2010-09-27 11:15:43 -07002931 * At the beginning of the drag operation, the child may have been on another
Patrick Dubroyce34a972010-10-19 10:34:32 -07002932 * screen, but it is re-parented before this method is called.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002933 *
2934 * @param child The child that is being dropped
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002935 */
Adam Cohen716b51e2011-06-30 12:09:54 -07002936 void onDropChild(View child) {
Romain Guyd94533d2009-08-17 10:01:15 -07002937 if (child != null) {
2938 LayoutParams lp = (LayoutParams) child.getLayoutParams();
Romain Guy84f296c2009-11-04 15:00:44 -08002939 lp.dropped = true;
Romain Guyd94533d2009-08-17 10:01:15 -07002940 child.requestLayout();
Romain Guyd94533d2009-08-17 10:01:15 -07002941 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002942 }
2943
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002944 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002945 * Computes a bounding rectangle for a range of cells
Winson Chungaafa03c2010-06-11 17:34:16 -07002946 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002947 * @param cellX X coordinate of upper left corner expressed as a cell position
2948 * @param cellY Y coordinate of upper left corner expressed as a cell position
Winson Chungaafa03c2010-06-11 17:34:16 -07002949 * @param cellHSpan Width in cells
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002950 * @param cellVSpan Height in cells
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07002951 * @param resultRect Rect into which to put the results
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002952 */
Adam Cohend41fbf52012-02-16 23:53:59 -08002953 public void cellToRect(int cellX, int cellY, int cellHSpan, int cellVSpan, Rect resultRect) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002954 final int cellWidth = mCellWidth;
2955 final int cellHeight = mCellHeight;
2956 final int widthGap = mWidthGap;
2957 final int heightGap = mHeightGap;
Winson Chungaafa03c2010-06-11 17:34:16 -07002958
Winson Chung4b825dcd2011-06-19 12:41:22 -07002959 final int hStartPadding = getPaddingLeft();
2960 final int vStartPadding = getPaddingTop();
Winson Chungaafa03c2010-06-11 17:34:16 -07002961
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002962 int width = cellHSpan * cellWidth + ((cellHSpan - 1) * widthGap);
2963 int height = cellVSpan * cellHeight + ((cellVSpan - 1) * heightGap);
2964
2965 int x = hStartPadding + cellX * (cellWidth + widthGap);
2966 int y = vStartPadding + cellY * (cellHeight + heightGap);
Winson Chungaafa03c2010-06-11 17:34:16 -07002967
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07002968 resultRect.set(x, y, x + width, y + height);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002969 }
Winson Chungaafa03c2010-06-11 17:34:16 -07002970
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002971 /**
Winson Chungaafa03c2010-06-11 17:34:16 -07002972 * Computes the required horizontal and vertical cell spans to always
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002973 * fit the given rectangle.
Winson Chungaafa03c2010-06-11 17:34:16 -07002974 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002975 * @param width Width in pixels
2976 * @param height Height in pixels
Patrick Dubroy8f86ddc2010-07-16 13:55:32 -07002977 * @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 -08002978 */
Patrick Dubroy8f86ddc2010-07-16 13:55:32 -07002979 public int[] rectToCell(int width, int height, int[] result) {
Michael Jurka9987a5c2010-10-08 16:58:12 -07002980 return rectToCell(getResources(), width, height, result);
2981 }
2982
2983 public static int[] rectToCell(Resources resources, int width, int height, int[] result) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002984 // Always assume we're working with the smallest span to make sure we
2985 // reserve enough space in both orientations.
Joe Onorato79e56262009-09-21 15:23:04 -04002986 int actualWidth = resources.getDimensionPixelSize(R.dimen.workspace_cell_width);
2987 int actualHeight = resources.getDimensionPixelSize(R.dimen.workspace_cell_height);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002988 int smallerSize = Math.min(actualWidth, actualHeight);
Joe Onorato79e56262009-09-21 15:23:04 -04002989
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002990 // Always round up to next largest cell
Winson Chung54c725c2011-08-03 12:03:40 -07002991 int spanX = (int) Math.ceil(width / (float) smallerSize);
2992 int spanY = (int) Math.ceil(height / (float) smallerSize);
Joe Onorato79e56262009-09-21 15:23:04 -04002993
Patrick Dubroy8f86ddc2010-07-16 13:55:32 -07002994 if (result == null) {
2995 return new int[] { spanX, spanY };
2996 }
2997 result[0] = spanX;
2998 result[1] = spanY;
2999 return result;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003000 }
3001
Michael Jurkaf12c75c2011-01-25 22:41:40 -08003002 public int[] cellSpansToSize(int hSpans, int vSpans) {
3003 int[] size = new int[2];
3004 size[0] = hSpans * mCellWidth + (hSpans - 1) * mWidthGap;
3005 size[1] = vSpans * mCellHeight + (vSpans - 1) * mHeightGap;
3006 return size;
3007 }
3008
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003009 /**
Patrick Dubroy047379a2010-12-19 22:02:04 -08003010 * Calculate the grid spans needed to fit given item
3011 */
3012 public void calculateSpans(ItemInfo info) {
3013 final int minWidth;
3014 final int minHeight;
3015
3016 if (info instanceof LauncherAppWidgetInfo) {
3017 minWidth = ((LauncherAppWidgetInfo) info).minWidth;
3018 minHeight = ((LauncherAppWidgetInfo) info).minHeight;
3019 } else if (info instanceof PendingAddWidgetInfo) {
3020 minWidth = ((PendingAddWidgetInfo) info).minWidth;
3021 minHeight = ((PendingAddWidgetInfo) info).minHeight;
3022 } else {
3023 // It's not a widget, so it must be 1x1
3024 info.spanX = info.spanY = 1;
3025 return;
3026 }
3027 int[] spans = rectToCell(minWidth, minHeight, null);
3028 info.spanX = spans[0];
3029 info.spanY = spans[1];
3030 }
3031
3032 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003033 * Find the first vacant cell, if there is one.
3034 *
3035 * @param vacant Holds the x and y coordinate of the vacant cell
3036 * @param spanX Horizontal cell span.
3037 * @param spanY Vertical cell span.
Winson Chungaafa03c2010-06-11 17:34:16 -07003038 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003039 * @return True if a vacant cell was found
3040 */
3041 public boolean getVacantCell(int[] vacant, int spanX, int spanY) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003042
Michael Jurka0280c3b2010-09-17 15:00:07 -07003043 return findVacantCell(vacant, spanX, spanY, mCountX, mCountY, mOccupied);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003044 }
3045
3046 static boolean findVacantCell(int[] vacant, int spanX, int spanY,
3047 int xCount, int yCount, boolean[][] occupied) {
3048
Adam Cohen2801caf2011-05-13 20:57:39 -07003049 for (int y = 0; y < yCount; y++) {
3050 for (int x = 0; x < xCount; x++) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003051 boolean available = !occupied[x][y];
3052out: for (int i = x; i < x + spanX - 1 && x < xCount; i++) {
3053 for (int j = y; j < y + spanY - 1 && y < yCount; j++) {
3054 available = available && !occupied[i][j];
3055 if (!available) break out;
3056 }
3057 }
3058
3059 if (available) {
3060 vacant[0] = x;
3061 vacant[1] = y;
3062 return true;
3063 }
3064 }
3065 }
3066
3067 return false;
3068 }
3069
Michael Jurka0280c3b2010-09-17 15:00:07 -07003070 private void clearOccupiedCells() {
3071 for (int x = 0; x < mCountX; x++) {
3072 for (int y = 0; y < mCountY; y++) {
3073 mOccupied[x][y] = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003074 }
3075 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07003076 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003077
Adam Cohend41fbf52012-02-16 23:53:59 -08003078 public void onMove(View view, int newCellX, int newCellY, int newSpanX, int newSpanY) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07003079 markCellsAsUnoccupiedForView(view);
Adam Cohen482ed822012-03-02 14:15:13 -08003080 markCellsForView(newCellX, newCellY, newSpanX, newSpanY, mOccupied, true);
Michael Jurka0280c3b2010-09-17 15:00:07 -07003081 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003082
Adam Cohend4844c32011-02-18 19:25:06 -08003083 public void markCellsAsOccupiedForView(View view) {
Adam Cohen482ed822012-03-02 14:15:13 -08003084 markCellsAsOccupiedForView(view, mOccupied);
3085 }
3086 public void markCellsAsOccupiedForView(View view, boolean[][] occupied) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07003087 if (view == null || view.getParent() != mShortcutsAndWidgets) return;
Michael Jurka0280c3b2010-09-17 15:00:07 -07003088 LayoutParams lp = (LayoutParams) view.getLayoutParams();
Adam Cohen482ed822012-03-02 14:15:13 -08003089 markCellsForView(lp.cellX, lp.cellY, lp.cellHSpan, lp.cellVSpan, occupied, true);
Michael Jurka0280c3b2010-09-17 15:00:07 -07003090 }
3091
Adam Cohend4844c32011-02-18 19:25:06 -08003092 public void markCellsAsUnoccupiedForView(View view) {
Adam Cohen482ed822012-03-02 14:15:13 -08003093 markCellsAsUnoccupiedForView(view, mOccupied);
3094 }
3095 public void markCellsAsUnoccupiedForView(View view, boolean occupied[][]) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07003096 if (view == null || view.getParent() != mShortcutsAndWidgets) return;
Michael Jurka0280c3b2010-09-17 15:00:07 -07003097 LayoutParams lp = (LayoutParams) view.getLayoutParams();
Adam Cohen482ed822012-03-02 14:15:13 -08003098 markCellsForView(lp.cellX, lp.cellY, lp.cellHSpan, lp.cellVSpan, occupied, false);
Michael Jurka0280c3b2010-09-17 15:00:07 -07003099 }
3100
Adam Cohen482ed822012-03-02 14:15:13 -08003101 private void markCellsForView(int cellX, int cellY, int spanX, int spanY, boolean[][] occupied,
3102 boolean value) {
3103 if (cellX < 0 || cellY < 0) return;
Michael Jurka0280c3b2010-09-17 15:00:07 -07003104 for (int x = cellX; x < cellX + spanX && x < mCountX; x++) {
3105 for (int y = cellY; y < cellY + spanY && y < mCountY; y++) {
Adam Cohen482ed822012-03-02 14:15:13 -08003106 occupied[x][y] = value;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003107 }
3108 }
3109 }
3110
Adam Cohen2801caf2011-05-13 20:57:39 -07003111 public int getDesiredWidth() {
Michael Jurka8b805b12012-04-18 14:23:14 -07003112 return getPaddingLeft() + getPaddingRight() + (mCountX * mCellWidth) +
Adam Cohen2801caf2011-05-13 20:57:39 -07003113 (Math.max((mCountX - 1), 0) * mWidthGap);
3114 }
3115
3116 public int getDesiredHeight() {
Michael Jurka8b805b12012-04-18 14:23:14 -07003117 return getPaddingTop() + getPaddingBottom() + (mCountY * mCellHeight) +
Adam Cohen2801caf2011-05-13 20:57:39 -07003118 (Math.max((mCountY - 1), 0) * mHeightGap);
3119 }
3120
Michael Jurka66d72172011-04-12 16:29:25 -07003121 public boolean isOccupied(int x, int y) {
3122 if (x < mCountX && y < mCountY) {
3123 return mOccupied[x][y];
3124 } else {
3125 throw new RuntimeException("Position exceeds the bound of this CellLayout");
3126 }
3127 }
3128
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003129 @Override
3130 public ViewGroup.LayoutParams generateLayoutParams(AttributeSet attrs) {
3131 return new CellLayout.LayoutParams(getContext(), attrs);
3132 }
3133
3134 @Override
3135 protected boolean checkLayoutParams(ViewGroup.LayoutParams p) {
3136 return p instanceof CellLayout.LayoutParams;
3137 }
3138
3139 @Override
3140 protected ViewGroup.LayoutParams generateLayoutParams(ViewGroup.LayoutParams p) {
3141 return new CellLayout.LayoutParams(p);
3142 }
3143
Winson Chungaafa03c2010-06-11 17:34:16 -07003144 public static class CellLayoutAnimationController extends LayoutAnimationController {
3145 public CellLayoutAnimationController(Animation animation, float delay) {
3146 super(animation, delay);
3147 }
3148
3149 @Override
3150 protected long getDelayForView(View view) {
3151 return (int) (Math.random() * 150);
3152 }
3153 }
3154
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003155 public static class LayoutParams extends ViewGroup.MarginLayoutParams {
3156 /**
3157 * Horizontal location of the item in the grid.
3158 */
3159 @ViewDebug.ExportedProperty
3160 public int cellX;
3161
3162 /**
3163 * Vertical location of the item in the grid.
3164 */
3165 @ViewDebug.ExportedProperty
3166 public int cellY;
3167
3168 /**
Adam Cohen482ed822012-03-02 14:15:13 -08003169 * Temporary horizontal location of the item in the grid during reorder
3170 */
3171 public int tmpCellX;
3172
3173 /**
3174 * Temporary vertical location of the item in the grid during reorder
3175 */
3176 public int tmpCellY;
3177
3178 /**
3179 * Indicates that the temporary coordinates should be used to layout the items
3180 */
3181 public boolean useTmpCoords;
3182
3183 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003184 * Number of cells spanned horizontally by the item.
3185 */
3186 @ViewDebug.ExportedProperty
3187 public int cellHSpan;
3188
3189 /**
3190 * Number of cells spanned vertically by the item.
3191 */
3192 @ViewDebug.ExportedProperty
3193 public int cellVSpan;
Winson Chungaafa03c2010-06-11 17:34:16 -07003194
Adam Cohen1b607ed2011-03-03 17:26:50 -08003195 /**
3196 * Indicates whether the item will set its x, y, width and height parameters freely,
3197 * or whether these will be computed based on cellX, cellY, cellHSpan and cellVSpan.
3198 */
Adam Cohend4844c32011-02-18 19:25:06 -08003199 public boolean isLockedToGrid = true;
3200
Adam Cohen482ed822012-03-02 14:15:13 -08003201 /**
3202 * Indicates whether this item can be reordered. Always true except in the case of the
3203 * the AllApps button.
3204 */
3205 public boolean canReorder = true;
3206
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003207 // X coordinate of the view in the layout.
3208 @ViewDebug.ExportedProperty
3209 int x;
3210 // Y coordinate of the view in the layout.
3211 @ViewDebug.ExportedProperty
3212 int y;
3213
Romain Guy84f296c2009-11-04 15:00:44 -08003214 boolean dropped;
Romain Guyfcb9e712009-10-02 16:06:52 -07003215
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003216 public LayoutParams(Context c, AttributeSet attrs) {
3217 super(c, attrs);
3218 cellHSpan = 1;
3219 cellVSpan = 1;
3220 }
3221
3222 public LayoutParams(ViewGroup.LayoutParams source) {
3223 super(source);
3224 cellHSpan = 1;
3225 cellVSpan = 1;
3226 }
Winson Chungaafa03c2010-06-11 17:34:16 -07003227
3228 public LayoutParams(LayoutParams source) {
3229 super(source);
3230 this.cellX = source.cellX;
3231 this.cellY = source.cellY;
3232 this.cellHSpan = source.cellHSpan;
3233 this.cellVSpan = source.cellVSpan;
3234 }
3235
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003236 public LayoutParams(int cellX, int cellY, int cellHSpan, int cellVSpan) {
Romain Guy8f19cdd2010-01-08 15:07:00 -08003237 super(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003238 this.cellX = cellX;
3239 this.cellY = cellY;
3240 this.cellHSpan = cellHSpan;
3241 this.cellVSpan = cellVSpan;
3242 }
3243
Adam Cohen7f4eabe2011-04-21 16:19:16 -07003244 public void setup(int cellWidth, int cellHeight, int widthGap, int heightGap) {
Adam Cohend4844c32011-02-18 19:25:06 -08003245 if (isLockedToGrid) {
3246 final int myCellHSpan = cellHSpan;
3247 final int myCellVSpan = cellVSpan;
Adam Cohen482ed822012-03-02 14:15:13 -08003248 final int myCellX = useTmpCoords ? tmpCellX : cellX;
3249 final int myCellY = useTmpCoords ? tmpCellY : cellY;
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;
3309 int screen;
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}