blob: 98fcdea0745b3d24af230fba556a3b246ac61e1e [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);
Romain Guyce3cbd12013-02-25 15:00:36 -0800184 setClipToPadding(false);
Adam Cohen2acce882012-03-28 19:03:19 -0700185 mLauncher = (Launcher) context;
Michael Jurkaa63c4522010-08-19 13:52:27 -0700186
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800187 TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.CellLayout, defStyle, 0);
188
Adam Cohenf4bd5792012-04-27 11:35:29 -0700189 mCellWidth = a.getDimensionPixelSize(R.styleable.CellLayout_cellWidth, 10);
190 mCellHeight = a.getDimensionPixelSize(R.styleable.CellLayout_cellHeight, 10);
Adam Cohen234c4cd2011-07-17 21:03:04 -0700191 mWidthGap = mOriginalWidthGap = a.getDimensionPixelSize(R.styleable.CellLayout_widthGap, 0);
192 mHeightGap = mOriginalHeightGap = a.getDimensionPixelSize(R.styleable.CellLayout_heightGap, 0);
Winson Chung4b825dcd2011-06-19 12:41:22 -0700193 mMaxGap = a.getDimensionPixelSize(R.styleable.CellLayout_maxGap, 0);
Adam Cohend22015c2010-07-26 22:02:18 -0700194 mCountX = LauncherModel.getCellCountX();
195 mCountY = LauncherModel.getCellCountY();
Michael Jurka0280c3b2010-09-17 15:00:07 -0700196 mOccupied = new boolean[mCountX][mCountY];
Adam Cohen482ed822012-03-02 14:15:13 -0800197 mTmpOccupied = new boolean[mCountX][mCountY];
Adam Cohen5b53f292012-03-29 14:30:35 -0700198 mPreviousReorderDirection[0] = INVALID_DIRECTION;
199 mPreviousReorderDirection[1] = INVALID_DIRECTION;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800200
201 a.recycle();
202
203 setAlwaysDrawnWithCacheEnabled(false);
204
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700205 final Resources res = getResources();
Adam Cohen307fe232012-08-16 17:55:58 -0700206 mHotseatScale = (res.getInteger(R.integer.hotseat_item_scale_percentage) / 100f);
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700207
Winson Chung967289b2011-06-30 18:09:30 -0700208 mNormalBackground = res.getDrawable(R.drawable.homescreen_blue_normal_holo);
Winson Chungdea74b72011-09-13 18:06:43 -0700209 mActiveGlowBackground = res.getDrawable(R.drawable.homescreen_blue_strong_holo);
Michael Jurka33945b22010-12-21 18:19:38 -0800210
Adam Cohenb5ba0972011-09-07 18:02:31 -0700211 mOverScrollLeft = res.getDrawable(R.drawable.overscroll_glow_left);
212 mOverScrollRight = res.getDrawable(R.drawable.overscroll_glow_right);
213 mForegroundPadding =
214 res.getDimensionPixelSize(R.dimen.workspace_overscroll_drawable_padding);
Michael Jurka33945b22010-12-21 18:19:38 -0800215
Adam Cohen19f37922012-03-21 11:59:11 -0700216 mReorderHintAnimationMagnitude = (REORDER_HINT_MAGNITUDE *
217 res.getDimensionPixelSize(R.dimen.app_icon_size));
218
Winson Chungb26f3d62011-06-02 10:49:29 -0700219 mNormalBackground.setFilterBitmap(true);
Winson Chungb26f3d62011-06-02 10:49:29 -0700220 mActiveGlowBackground.setFilterBitmap(true);
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700221
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700222 // Initialize the data structures used for the drag visualization.
Winson Chung150fbab2010-09-29 17:14:26 -0700223
Patrick Dubroyce34a972010-10-19 10:34:32 -0700224 mEaseOutInterpolator = new DecelerateInterpolator(2.5f); // Quint ease out
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700225
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700226
Winson Chungb8c69f32011-10-19 21:36:08 -0700227 mDragCell[0] = mDragCell[1] = -1;
Joe Onorato4be866d2010-10-10 11:26:02 -0700228 for (int i = 0; i < mDragOutlines.length; i++) {
Adam Cohend41fbf52012-02-16 23:53:59 -0800229 mDragOutlines[i] = new Rect(-1, -1, -1, -1);
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700230 }
231
232 // When dragging things around the home screens, we show a green outline of
233 // where the item will land. The outlines gradually fade out, leaving a trail
234 // behind the drag path.
235 // Set up all the animations that are used to implement this fading.
236 final int duration = res.getInteger(R.integer.config_dragOutlineFadeTime);
Chet Haase472b2812010-10-14 07:02:04 -0700237 final float fromAlphaValue = 0;
238 final float toAlphaValue = (float)res.getInteger(R.integer.config_dragOutlineMaxAlpha);
Joe Onorato4be866d2010-10-10 11:26:02 -0700239
Patrick Dubroy8e58e912010-10-14 13:21:48 -0700240 Arrays.fill(mDragOutlineAlphas, fromAlphaValue);
Joe Onorato4be866d2010-10-10 11:26:02 -0700241
242 for (int i = 0; i < mDragOutlineAnims.length; i++) {
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700243 final InterruptibleInOutAnimator anim =
244 new InterruptibleInOutAnimator(duration, fromAlphaValue, toAlphaValue);
Patrick Dubroyce34a972010-10-19 10:34:32 -0700245 anim.getAnimator().setInterpolator(mEaseOutInterpolator);
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700246 final int thisIndex = i;
Chet Haase472b2812010-10-14 07:02:04 -0700247 anim.getAnimator().addUpdateListener(new AnimatorUpdateListener() {
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700248 public void onAnimationUpdate(ValueAnimator animation) {
Joe Onorato4be866d2010-10-10 11:26:02 -0700249 final Bitmap outline = (Bitmap)anim.getTag();
250
251 // If an animation is started and then stopped very quickly, we can still
252 // get spurious updates we've cleared the tag. Guard against this.
253 if (outline == null) {
Michael Jurka3a9fced2012-04-13 14:44:29 -0700254 @SuppressWarnings("all") // suppress dead code warning
255 final boolean debug = false;
256 if (debug) {
Patrick Dubroyfe6bd872010-10-13 17:32:10 -0700257 Object val = animation.getAnimatedValue();
258 Log.d(TAG, "anim " + thisIndex + " update: " + val +
259 ", isStopped " + anim.isStopped());
260 }
Joe Onorato4be866d2010-10-10 11:26:02 -0700261 // Try to prevent it from continuing to run
262 animation.cancel();
263 } else {
Chet Haase472b2812010-10-14 07:02:04 -0700264 mDragOutlineAlphas[thisIndex] = (Float) animation.getAnimatedValue();
Adam Cohend41fbf52012-02-16 23:53:59 -0800265 CellLayout.this.invalidate(mDragOutlines[thisIndex]);
Joe Onorato4be866d2010-10-10 11:26:02 -0700266 }
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700267 }
268 });
Joe Onorato4be866d2010-10-10 11:26:02 -0700269 // The animation holds a reference to the drag outline bitmap as long is it's
270 // running. This way the bitmap can be GCed when the animations are complete.
Chet Haase472b2812010-10-14 07:02:04 -0700271 anim.getAnimator().addListener(new AnimatorListenerAdapter() {
Michael Jurka3c4c20f2010-10-28 15:36:06 -0700272 @Override
Joe Onorato4be866d2010-10-10 11:26:02 -0700273 public void onAnimationEnd(Animator animation) {
Chet Haase472b2812010-10-14 07:02:04 -0700274 if ((Float) ((ValueAnimator) animation).getAnimatedValue() == 0f) {
Joe Onorato4be866d2010-10-10 11:26:02 -0700275 anim.setTag(null);
276 }
277 }
278 });
279 mDragOutlineAnims[i] = anim;
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700280 }
Patrick Dubroyce34a972010-10-19 10:34:32 -0700281
Michael Jurka18014792010-10-14 09:01:34 -0700282 mBackgroundRect = new Rect();
Adam Cohenb5ba0972011-09-07 18:02:31 -0700283 mForegroundRect = new Rect();
Michael Jurkabea15192010-11-17 12:33:46 -0800284
Michael Jurkaa52570f2012-03-20 03:18:20 -0700285 mShortcutsAndWidgets = new ShortcutAndWidgetContainer(context);
286 mShortcutsAndWidgets.setCellDimensions(mCellWidth, mCellHeight, mWidthGap, mHeightGap);
287 addView(mShortcutsAndWidgets);
Michael Jurka18014792010-10-14 09:01:34 -0700288 }
289
Michael Jurkaf6440da2011-04-05 14:50:34 -0700290 static int widthInPortrait(Resources r, int numCells) {
291 // We use this method from Workspace to figure out how many rows/columns Launcher should
292 // have. We ignore the left/right padding on CellLayout because it turns out in our design
293 // the padding extends outside the visible screen size, but it looked fine anyway.
Michael Jurkaf6440da2011-04-05 14:50:34 -0700294 int cellWidth = r.getDimensionPixelSize(R.dimen.workspace_cell_width);
Winson Chung4b825dcd2011-06-19 12:41:22 -0700295 int minGap = Math.min(r.getDimensionPixelSize(R.dimen.workspace_width_gap),
296 r.getDimensionPixelSize(R.dimen.workspace_height_gap));
Michael Jurkaf6440da2011-04-05 14:50:34 -0700297
Winson Chung4b825dcd2011-06-19 12:41:22 -0700298 return minGap * (numCells - 1) + cellWidth * numCells;
Michael Jurkaf6440da2011-04-05 14:50:34 -0700299 }
300
Michael Jurkaf6440da2011-04-05 14:50:34 -0700301 static int heightInLandscape(Resources r, int numCells) {
302 // We use this method from Workspace to figure out how many rows/columns Launcher should
303 // have. We ignore the left/right padding on CellLayout because it turns out in our design
304 // the padding extends outside the visible screen size, but it looked fine anyway.
Michael Jurkaf6440da2011-04-05 14:50:34 -0700305 int cellHeight = r.getDimensionPixelSize(R.dimen.workspace_cell_height);
Winson Chung4b825dcd2011-06-19 12:41:22 -0700306 int minGap = Math.min(r.getDimensionPixelSize(R.dimen.workspace_width_gap),
307 r.getDimensionPixelSize(R.dimen.workspace_height_gap));
Michael Jurkaf6440da2011-04-05 14:50:34 -0700308
Winson Chung4b825dcd2011-06-19 12:41:22 -0700309 return minGap * (numCells - 1) + cellHeight * numCells;
Michael Jurkaf6440da2011-04-05 14:50:34 -0700310 }
311
Adam Cohen2801caf2011-05-13 20:57:39 -0700312 public void enableHardwareLayers() {
Michael Jurkaca993832012-06-29 15:17:04 -0700313 mShortcutsAndWidgets.setLayerType(LAYER_TYPE_HARDWARE, sPaint);
Michael Jurkad51f33a2012-06-28 15:35:26 -0700314 }
315
316 public void disableHardwareLayers() {
Michael Jurkaca993832012-06-29 15:17:04 -0700317 mShortcutsAndWidgets.setLayerType(LAYER_TYPE_NONE, sPaint);
Michael Jurkad51f33a2012-06-28 15:35:26 -0700318 }
319
320 public void buildHardwareLayer() {
321 mShortcutsAndWidgets.buildLayer();
Adam Cohen2801caf2011-05-13 20:57:39 -0700322 }
323
Adam Cohen307fe232012-08-16 17:55:58 -0700324 public float getChildrenScale() {
325 return mIsHotseat ? mHotseatScale : 1.0f;
326 }
327
Adam Cohen2801caf2011-05-13 20:57:39 -0700328 public void setGridSize(int x, int y) {
329 mCountX = x;
330 mCountY = y;
331 mOccupied = new boolean[mCountX][mCountY];
Adam Cohen482ed822012-03-02 14:15:13 -0800332 mTmpOccupied = new boolean[mCountX][mCountY];
Adam Cohen7fbec102012-03-27 12:42:19 -0700333 mTempRectStack.clear();
Adam Cohen76fc0852011-06-17 13:26:23 -0700334 requestLayout();
Adam Cohen2801caf2011-05-13 20:57:39 -0700335 }
336
Patrick Dubroy96864c32011-03-10 17:17:23 -0800337 private void invalidateBubbleTextView(BubbleTextView icon) {
338 final int padding = icon.getPressedOrFocusedBackgroundPadding();
Winson Chung4b825dcd2011-06-19 12:41:22 -0700339 invalidate(icon.getLeft() + getPaddingLeft() - padding,
340 icon.getTop() + getPaddingTop() - padding,
341 icon.getRight() + getPaddingLeft() + padding,
342 icon.getBottom() + getPaddingTop() + padding);
Patrick Dubroy96864c32011-03-10 17:17:23 -0800343 }
344
Adam Cohenb5ba0972011-09-07 18:02:31 -0700345 void setOverScrollAmount(float r, boolean left) {
346 if (left && mOverScrollForegroundDrawable != mOverScrollLeft) {
347 mOverScrollForegroundDrawable = mOverScrollLeft;
348 } else if (!left && mOverScrollForegroundDrawable != mOverScrollRight) {
349 mOverScrollForegroundDrawable = mOverScrollRight;
350 }
351
352 mForegroundAlpha = (int) Math.round((r * 255));
353 mOverScrollForegroundDrawable.setAlpha(mForegroundAlpha);
354 invalidate();
355 }
356
Patrick Dubroy96864c32011-03-10 17:17:23 -0800357 void setPressedOrFocusedIcon(BubbleTextView icon) {
358 // We draw the pressed or focused BubbleTextView's background in CellLayout because it
359 // requires an expanded clip rect (due to the glow's blur radius)
360 BubbleTextView oldIcon = mPressedOrFocusedIcon;
361 mPressedOrFocusedIcon = icon;
362 if (oldIcon != null) {
363 invalidateBubbleTextView(oldIcon);
364 }
365 if (mPressedOrFocusedIcon != null) {
366 invalidateBubbleTextView(mPressedOrFocusedIcon);
367 }
368 }
369
Michael Jurka33945b22010-12-21 18:19:38 -0800370 void setIsDragOverlapping(boolean isDragOverlapping) {
371 if (mIsDragOverlapping != isDragOverlapping) {
372 mIsDragOverlapping = isDragOverlapping;
373 invalidate();
374 }
375 }
376
377 boolean getIsDragOverlapping() {
378 return mIsDragOverlapping;
379 }
380
Adam Cohenebea84d2011-11-09 17:20:41 -0800381 protected void setOverscrollTransformsDirty(boolean dirty) {
382 mScrollingTransformsDirty = dirty;
383 }
384
385 protected void resetOverscrollTransforms() {
386 if (mScrollingTransformsDirty) {
387 setOverscrollTransformsDirty(false);
388 setTranslationX(0);
389 setRotationY(0);
390 // It doesn't matter if we pass true or false here, the important thing is that we
391 // pass 0, which results in the overscroll drawable not being drawn any more.
392 setOverScrollAmount(0, false);
393 setPivotX(getMeasuredWidth() / 2);
394 setPivotY(getMeasuredHeight() / 2);
395 }
396 }
397
Adam Cohen307fe232012-08-16 17:55:58 -0700398 public void scaleRect(Rect r, float scale) {
399 if (scale != 1.0f) {
400 r.left = (int) (r.left * scale + 0.5f);
401 r.top = (int) (r.top * scale + 0.5f);
402 r.right = (int) (r.right * scale + 0.5f);
403 r.bottom = (int) (r.bottom * scale + 0.5f);
404 }
405 }
406
407 Rect temp = new Rect();
408 void scaleRectAboutCenter(Rect in, Rect out, float scale) {
409 int cx = in.centerX();
410 int cy = in.centerY();
411 out.set(in);
412 out.offset(-cx, -cy);
413 scaleRect(out, scale);
414 out.offset(cx, cy);
415 }
416
Jeff Sharkey83f111d2009-04-20 21:03:13 -0700417 @Override
Patrick Dubroy1262e362010-10-06 15:49:50 -0700418 protected void onDraw(Canvas canvas) {
Michael Jurka3e7c7632010-10-02 16:01:03 -0700419 // When we're large, we are either drawn in a "hover" state (ie when dragging an item to
420 // a neighboring page) or with just a normal background (if backgroundAlpha > 0.0f)
421 // When we're small, we are either drawn normally or in the "accepts drops" state (during
422 // a drag). However, we also drag the mini hover background *over* one of those two
423 // backgrounds
Winson Chungb26f3d62011-06-02 10:49:29 -0700424 if (mBackgroundAlpha > 0.0f) {
Adam Cohenf34bab52010-09-30 14:11:56 -0700425 Drawable bg;
Michael Jurka33945b22010-12-21 18:19:38 -0800426
427 if (mIsDragOverlapping) {
428 // In the mini case, we draw the active_glow bg *over* the active background
Michael Jurkabdf78552011-10-31 14:34:25 -0700429 bg = mActiveGlowBackground;
Adam Cohenf34bab52010-09-30 14:11:56 -0700430 } else {
Michael Jurkabdf78552011-10-31 14:34:25 -0700431 bg = mNormalBackground;
Adam Cohenf34bab52010-09-30 14:11:56 -0700432 }
Michael Jurka33945b22010-12-21 18:19:38 -0800433
434 bg.setAlpha((int) (mBackgroundAlpha * mBackgroundAlphaMultiplier * 255));
435 bg.setBounds(mBackgroundRect);
436 bg.draw(canvas);
Michael Jurkaa63c4522010-08-19 13:52:27 -0700437 }
Romain Guya6abce82009-11-10 02:54:41 -0800438
Patrick Dubroy8e58e912010-10-14 13:21:48 -0700439 final Paint paint = mDragOutlinePaint;
Joe Onorato4be866d2010-10-10 11:26:02 -0700440 for (int i = 0; i < mDragOutlines.length; i++) {
Chet Haase472b2812010-10-14 07:02:04 -0700441 final float alpha = mDragOutlineAlphas[i];
Joe Onorato4be866d2010-10-10 11:26:02 -0700442 if (alpha > 0) {
Adam Cohend41fbf52012-02-16 23:53:59 -0800443 final Rect r = mDragOutlines[i];
Adam Cohen307fe232012-08-16 17:55:58 -0700444 scaleRectAboutCenter(r, temp, getChildrenScale());
Joe Onorato4be866d2010-10-10 11:26:02 -0700445 final Bitmap b = (Bitmap) mDragOutlineAnims[i].getTag();
Chet Haase472b2812010-10-14 07:02:04 -0700446 paint.setAlpha((int)(alpha + .5f));
Adam Cohen307fe232012-08-16 17:55:58 -0700447 canvas.drawBitmap(b, null, temp, paint);
Winson Chung150fbab2010-09-29 17:14:26 -0700448 }
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700449 }
Patrick Dubroy96864c32011-03-10 17:17:23 -0800450
451 // We draw the pressed or focused BubbleTextView's background in CellLayout because it
452 // requires an expanded clip rect (due to the glow's blur radius)
453 if (mPressedOrFocusedIcon != null) {
454 final int padding = mPressedOrFocusedIcon.getPressedOrFocusedBackgroundPadding();
455 final Bitmap b = mPressedOrFocusedIcon.getPressedOrFocusedBackground();
456 if (b != null) {
457 canvas.drawBitmap(b,
Winson Chung4b825dcd2011-06-19 12:41:22 -0700458 mPressedOrFocusedIcon.getLeft() + getPaddingLeft() - padding,
459 mPressedOrFocusedIcon.getTop() + getPaddingTop() - padding,
Patrick Dubroy96864c32011-03-10 17:17:23 -0800460 null);
461 }
462 }
Adam Cohen69ce2e52011-07-03 19:25:21 -0700463
Adam Cohen482ed822012-03-02 14:15:13 -0800464 if (DEBUG_VISUALIZE_OCCUPIED) {
465 int[] pt = new int[2];
466 ColorDrawable cd = new ColorDrawable(Color.RED);
Adam Cohene7587d22012-05-24 18:50:02 -0700467 cd.setBounds(0, 0, mCellWidth, mCellHeight);
Adam Cohen482ed822012-03-02 14:15:13 -0800468 for (int i = 0; i < mCountX; i++) {
469 for (int j = 0; j < mCountY; j++) {
470 if (mOccupied[i][j]) {
471 cellToPoint(i, j, pt);
472 canvas.save();
473 canvas.translate(pt[0], pt[1]);
474 cd.draw(canvas);
475 canvas.restore();
476 }
477 }
478 }
479 }
480
Andrew Flynn850d2e72012-04-26 16:51:20 -0700481 int previewOffset = FolderRingAnimator.sPreviewSize;
482
Adam Cohen69ce2e52011-07-03 19:25:21 -0700483 // The folder outer / inner ring image(s)
484 for (int i = 0; i < mFolderOuterRings.size(); i++) {
485 FolderRingAnimator fra = mFolderOuterRings.get(i);
486
487 // Draw outer ring
488 Drawable d = FolderRingAnimator.sSharedOuterRingDrawable;
489 int width = (int) fra.getOuterRingSize();
490 int height = width;
491 cellToPoint(fra.mCellX, fra.mCellY, mTempLocation);
492
493 int centerX = mTempLocation[0] + mCellWidth / 2;
Andrew Flynn850d2e72012-04-26 16:51:20 -0700494 int centerY = mTempLocation[1] + previewOffset / 2;
Adam Cohen69ce2e52011-07-03 19:25:21 -0700495
496 canvas.save();
497 canvas.translate(centerX - width / 2, centerY - height / 2);
498 d.setBounds(0, 0, width, height);
499 d.draw(canvas);
500 canvas.restore();
501
502 // Draw inner ring
503 d = FolderRingAnimator.sSharedInnerRingDrawable;
504 width = (int) fra.getInnerRingSize();
505 height = width;
506 cellToPoint(fra.mCellX, fra.mCellY, mTempLocation);
507
508 centerX = mTempLocation[0] + mCellWidth / 2;
Andrew Flynn850d2e72012-04-26 16:51:20 -0700509 centerY = mTempLocation[1] + previewOffset / 2;
Adam Cohen69ce2e52011-07-03 19:25:21 -0700510 canvas.save();
511 canvas.translate(centerX - width / 2, centerY - width / 2);
512 d.setBounds(0, 0, width, height);
513 d.draw(canvas);
514 canvas.restore();
515 }
Adam Cohenc51934b2011-07-26 21:07:43 -0700516
517 if (mFolderLeaveBehindCell[0] >= 0 && mFolderLeaveBehindCell[1] >= 0) {
518 Drawable d = FolderIcon.sSharedFolderLeaveBehind;
519 int width = d.getIntrinsicWidth();
520 int height = d.getIntrinsicHeight();
521
522 cellToPoint(mFolderLeaveBehindCell[0], mFolderLeaveBehindCell[1], mTempLocation);
523 int centerX = mTempLocation[0] + mCellWidth / 2;
Andrew Flynn850d2e72012-04-26 16:51:20 -0700524 int centerY = mTempLocation[1] + previewOffset / 2;
Adam Cohenc51934b2011-07-26 21:07:43 -0700525
526 canvas.save();
527 canvas.translate(centerX - width / 2, centerY - width / 2);
528 d.setBounds(0, 0, width, height);
529 d.draw(canvas);
530 canvas.restore();
531 }
Adam Cohen69ce2e52011-07-03 19:25:21 -0700532 }
533
Adam Cohenb5ba0972011-09-07 18:02:31 -0700534 @Override
535 protected void dispatchDraw(Canvas canvas) {
536 super.dispatchDraw(canvas);
537 if (mForegroundAlpha > 0) {
538 mOverScrollForegroundDrawable.setBounds(mForegroundRect);
539 Paint p = ((NinePatchDrawable) mOverScrollForegroundDrawable).getPaint();
Romain Guy8a0bff52012-05-06 13:14:33 -0700540 p.setXfermode(sAddBlendMode);
Adam Cohenb5ba0972011-09-07 18:02:31 -0700541 mOverScrollForegroundDrawable.draw(canvas);
542 p.setXfermode(null);
543 }
544 }
545
Adam Cohen69ce2e52011-07-03 19:25:21 -0700546 public void showFolderAccept(FolderRingAnimator fra) {
547 mFolderOuterRings.add(fra);
548 }
549
550 public void hideFolderAccept(FolderRingAnimator fra) {
551 if (mFolderOuterRings.contains(fra)) {
552 mFolderOuterRings.remove(fra);
553 }
554 invalidate();
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700555 }
556
Adam Cohenc51934b2011-07-26 21:07:43 -0700557 public void setFolderLeaveBehindCell(int x, int y) {
558 mFolderLeaveBehindCell[0] = x;
559 mFolderLeaveBehindCell[1] = y;
560 invalidate();
561 }
562
563 public void clearFolderLeaveBehind() {
564 mFolderLeaveBehindCell[0] = -1;
565 mFolderLeaveBehindCell[1] = -1;
566 invalidate();
567 }
568
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700569 @Override
Michael Jurkae6235dd2011-10-04 15:02:05 -0700570 public boolean shouldDelayChildPressedState() {
571 return false;
572 }
573
Adam Cohen1462de32012-07-24 22:34:36 -0700574 public void restoreInstanceState(SparseArray<Parcelable> states) {
575 dispatchRestoreInstanceState(states);
576 }
577
Michael Jurkae6235dd2011-10-04 15:02:05 -0700578 @Override
Jeff Sharkey83f111d2009-04-20 21:03:13 -0700579 public void cancelLongPress() {
580 super.cancelLongPress();
581
582 // Cancel long press for all children
583 final int count = getChildCount();
584 for (int i = 0; i < count; i++) {
585 final View child = getChildAt(i);
586 child.cancelLongPress();
587 }
588 }
589
Michael Jurkadee05892010-07-27 10:01:56 -0700590 public void setOnInterceptTouchListener(View.OnTouchListener listener) {
591 mInterceptTouchListener = listener;
592 }
593
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800594 int getCountX() {
Adam Cohend22015c2010-07-26 22:02:18 -0700595 return mCountX;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800596 }
597
598 int getCountY() {
Adam Cohend22015c2010-07-26 22:02:18 -0700599 return mCountY;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800600 }
601
Andrew Flynn0dca1ec2012-02-29 13:33:22 -0800602 public void setIsHotseat(boolean isHotseat) {
603 mIsHotseat = isHotseat;
604 }
605
Andrew Flynn0dca1ec2012-02-29 13:33:22 -0800606 public boolean addViewToCellLayout(View child, int index, int childId, LayoutParams params,
Andrew Flynn850d2e72012-04-26 16:51:20 -0700607 boolean markCells) {
Winson Chungaafa03c2010-06-11 17:34:16 -0700608 final LayoutParams lp = params;
609
Andrew Flynnde38e422012-05-08 11:22:15 -0700610 // Hotseat icons - remove text
Andrew Flynn0dca1ec2012-02-29 13:33:22 -0800611 if (child instanceof BubbleTextView) {
612 BubbleTextView bubbleChild = (BubbleTextView) child;
613
Andrew Flynnde38e422012-05-08 11:22:15 -0700614 Resources res = getResources();
615 if (mIsHotseat) {
616 bubbleChild.setTextColor(res.getColor(android.R.color.transparent));
617 } else {
618 bubbleChild.setTextColor(res.getColor(R.color.workspace_icon_text_color));
Andrew Flynn0dca1ec2012-02-29 13:33:22 -0800619 }
620 }
621
Adam Cohen307fe232012-08-16 17:55:58 -0700622 child.setScaleX(getChildrenScale());
623 child.setScaleY(getChildrenScale());
624
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800625 // Generate an id for each view, this assumes we have at most 256x256 cells
626 // per workspace screen
Adam Cohend22015c2010-07-26 22:02:18 -0700627 if (lp.cellX >= 0 && lp.cellX <= mCountX - 1 && lp.cellY >= 0 && lp.cellY <= mCountY - 1) {
Winson Chungaafa03c2010-06-11 17:34:16 -0700628 // If the horizontal or vertical span is set to -1, it is taken to
629 // mean that it spans the extent of the CellLayout
Adam Cohend22015c2010-07-26 22:02:18 -0700630 if (lp.cellHSpan < 0) lp.cellHSpan = mCountX;
631 if (lp.cellVSpan < 0) lp.cellVSpan = mCountY;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800632
Winson Chungaafa03c2010-06-11 17:34:16 -0700633 child.setId(childId);
634
Michael Jurkaa52570f2012-03-20 03:18:20 -0700635 mShortcutsAndWidgets.addView(child, index, lp);
Michael Jurkadee05892010-07-27 10:01:56 -0700636
Michael Jurkaf3ca3ab2010-10-20 17:08:24 -0700637 if (markCells) markCellsAsOccupiedForView(child);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700638
Winson Chungaafa03c2010-06-11 17:34:16 -0700639 return true;
640 }
641 return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800642 }
Michael Jurka3e7c7632010-10-02 16:01:03 -0700643
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800644 @Override
Michael Jurka0280c3b2010-09-17 15:00:07 -0700645 public void removeAllViews() {
646 clearOccupiedCells();
Michael Jurkaa52570f2012-03-20 03:18:20 -0700647 mShortcutsAndWidgets.removeAllViews();
Michael Jurka0280c3b2010-09-17 15:00:07 -0700648 }
649
650 @Override
651 public void removeAllViewsInLayout() {
Michael Jurkaa52570f2012-03-20 03:18:20 -0700652 if (mShortcutsAndWidgets.getChildCount() > 0) {
Michael Jurka7cfc2822011-08-02 20:19:24 -0700653 clearOccupiedCells();
Michael Jurkaa52570f2012-03-20 03:18:20 -0700654 mShortcutsAndWidgets.removeAllViewsInLayout();
Michael Jurka7cfc2822011-08-02 20:19:24 -0700655 }
Michael Jurka0280c3b2010-09-17 15:00:07 -0700656 }
657
Michael Jurkaf3ca3ab2010-10-20 17:08:24 -0700658 public void removeViewWithoutMarkingCells(View view) {
Michael Jurkaa52570f2012-03-20 03:18:20 -0700659 mShortcutsAndWidgets.removeView(view);
Michael Jurkaf3ca3ab2010-10-20 17:08:24 -0700660 }
661
Michael Jurka0280c3b2010-09-17 15:00:07 -0700662 @Override
663 public void removeView(View view) {
664 markCellsAsUnoccupiedForView(view);
Michael Jurkaa52570f2012-03-20 03:18:20 -0700665 mShortcutsAndWidgets.removeView(view);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700666 }
667
668 @Override
669 public void removeViewAt(int index) {
Michael Jurkaa52570f2012-03-20 03:18:20 -0700670 markCellsAsUnoccupiedForView(mShortcutsAndWidgets.getChildAt(index));
671 mShortcutsAndWidgets.removeViewAt(index);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700672 }
673
674 @Override
675 public void removeViewInLayout(View view) {
676 markCellsAsUnoccupiedForView(view);
Michael Jurkaa52570f2012-03-20 03:18:20 -0700677 mShortcutsAndWidgets.removeViewInLayout(view);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700678 }
679
680 @Override
681 public void removeViews(int start, int count) {
682 for (int i = start; i < start + count; i++) {
Michael Jurkaa52570f2012-03-20 03:18:20 -0700683 markCellsAsUnoccupiedForView(mShortcutsAndWidgets.getChildAt(i));
Michael Jurka0280c3b2010-09-17 15:00:07 -0700684 }
Michael Jurkaa52570f2012-03-20 03:18:20 -0700685 mShortcutsAndWidgets.removeViews(start, count);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700686 }
687
688 @Override
689 public void removeViewsInLayout(int start, int count) {
690 for (int i = start; i < start + count; i++) {
Michael Jurkaa52570f2012-03-20 03:18:20 -0700691 markCellsAsUnoccupiedForView(mShortcutsAndWidgets.getChildAt(i));
Michael Jurka0280c3b2010-09-17 15:00:07 -0700692 }
Michael Jurkaa52570f2012-03-20 03:18:20 -0700693 mShortcutsAndWidgets.removeViewsInLayout(start, count);
Michael Jurkaabded662011-03-04 12:06:57 -0800694 }
695
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800696 @Override
697 protected void onAttachedToWindow() {
698 super.onAttachedToWindow();
699 mCellInfo.screen = ((ViewGroup) getParent()).indexOfChild(this);
700 }
701
Michael Jurkaaf442092010-06-10 17:01:57 -0700702 public void setTagToCellInfoForPoint(int touchX, int touchY) {
703 final CellInfo cellInfo = mCellInfo;
Winson Chungeecf02d2012-03-02 17:14:58 -0800704 Rect frame = mRect;
Michael Jurka8b805b12012-04-18 14:23:14 -0700705 final int x = touchX + getScrollX();
706 final int y = touchY + getScrollY();
Michael Jurkaa52570f2012-03-20 03:18:20 -0700707 final int count = mShortcutsAndWidgets.getChildCount();
Michael Jurkaaf442092010-06-10 17:01:57 -0700708
709 boolean found = false;
710 for (int i = count - 1; i >= 0; i--) {
Michael Jurkaa52570f2012-03-20 03:18:20 -0700711 final View child = mShortcutsAndWidgets.getChildAt(i);
Adam Cohend4844c32011-02-18 19:25:06 -0800712 final LayoutParams lp = (LayoutParams) child.getLayoutParams();
Michael Jurkaaf442092010-06-10 17:01:57 -0700713
Adam Cohen1b607ed2011-03-03 17:26:50 -0800714 if ((child.getVisibility() == VISIBLE || child.getAnimation() != null) &&
715 lp.isLockedToGrid) {
Michael Jurkaaf442092010-06-10 17:01:57 -0700716 child.getHitRect(frame);
Winson Chung0be025d2011-05-23 17:45:09 -0700717
Winson Chungeecf02d2012-03-02 17:14:58 -0800718 float scale = child.getScaleX();
719 frame = new Rect(child.getLeft(), child.getTop(), child.getRight(),
720 child.getBottom());
Winson Chung0be025d2011-05-23 17:45:09 -0700721 // The child hit rect is relative to the CellLayoutChildren parent, so we need to
722 // offset that by this CellLayout's padding to test an (x,y) point that is relative
723 // to this view.
Michael Jurka8b805b12012-04-18 14:23:14 -0700724 frame.offset(getPaddingLeft(), getPaddingTop());
Winson Chungeecf02d2012-03-02 17:14:58 -0800725 frame.inset((int) (frame.width() * (1f - scale) / 2),
726 (int) (frame.height() * (1f - scale) / 2));
Winson Chung0be025d2011-05-23 17:45:09 -0700727
Michael Jurkaaf442092010-06-10 17:01:57 -0700728 if (frame.contains(x, y)) {
Michael Jurkaaf442092010-06-10 17:01:57 -0700729 cellInfo.cell = child;
730 cellInfo.cellX = lp.cellX;
731 cellInfo.cellY = lp.cellY;
732 cellInfo.spanX = lp.cellHSpan;
733 cellInfo.spanY = lp.cellVSpan;
Michael Jurkaaf442092010-06-10 17:01:57 -0700734 found = true;
Michael Jurkaaf442092010-06-10 17:01:57 -0700735 break;
736 }
737 }
738 }
Winson Chungaafa03c2010-06-11 17:34:16 -0700739
Michael Jurkad771c962011-08-09 15:00:48 -0700740 mLastDownOnOccupiedCell = found;
741
Michael Jurkaaf442092010-06-10 17:01:57 -0700742 if (!found) {
Winson Chung0be025d2011-05-23 17:45:09 -0700743 final int cellXY[] = mTmpXY;
Michael Jurkaaf442092010-06-10 17:01:57 -0700744 pointToCellExact(x, y, cellXY);
745
Michael Jurkaaf442092010-06-10 17:01:57 -0700746 cellInfo.cell = null;
747 cellInfo.cellX = cellXY[0];
748 cellInfo.cellY = cellXY[1];
749 cellInfo.spanX = 1;
750 cellInfo.spanY = 1;
Michael Jurkaaf442092010-06-10 17:01:57 -0700751 }
752 setTag(cellInfo);
753 }
754
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800755 @Override
756 public boolean onInterceptTouchEvent(MotionEvent ev) {
Adam Cohenc1997fd2011-08-15 18:26:39 -0700757 // First we clear the tag to ensure that on every touch down we start with a fresh slate,
758 // even in the case where we return early. Not clearing here was causing bugs whereby on
759 // long-press we'd end up picking up an item from a previous drag operation.
760 final int action = ev.getAction();
761
762 if (action == MotionEvent.ACTION_DOWN) {
763 clearTagCellInfo();
764 }
765
Michael Jurkadee05892010-07-27 10:01:56 -0700766 if (mInterceptTouchListener != null && mInterceptTouchListener.onTouch(this, ev)) {
767 return true;
768 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800769
770 if (action == MotionEvent.ACTION_DOWN) {
Michael Jurkaaf442092010-06-10 17:01:57 -0700771 setTagToCellInfoForPoint((int) ev.getX(), (int) ev.getY());
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800772 }
Winson Chungeecf02d2012-03-02 17:14:58 -0800773
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800774 return false;
775 }
776
Adam Cohenc1997fd2011-08-15 18:26:39 -0700777 private void clearTagCellInfo() {
778 final CellInfo cellInfo = mCellInfo;
779 cellInfo.cell = null;
780 cellInfo.cellX = -1;
781 cellInfo.cellY = -1;
782 cellInfo.spanX = 0;
783 cellInfo.spanY = 0;
784 setTag(cellInfo);
785 }
786
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800787 public CellInfo getTag() {
Michael Jurka0280c3b2010-09-17 15:00:07 -0700788 return (CellInfo) super.getTag();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800789 }
790
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700791 /**
Winson Chungaafa03c2010-06-11 17:34:16 -0700792 * Given a point, return the cell that strictly encloses that point
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800793 * @param x X coordinate of the point
794 * @param y Y coordinate of the point
795 * @param result Array of 2 ints to hold the x and y coordinate of the cell
796 */
797 void pointToCellExact(int x, int y, int[] result) {
Winson Chung4b825dcd2011-06-19 12:41:22 -0700798 final int hStartPadding = getPaddingLeft();
799 final int vStartPadding = getPaddingTop();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800800
801 result[0] = (x - hStartPadding) / (mCellWidth + mWidthGap);
802 result[1] = (y - vStartPadding) / (mCellHeight + mHeightGap);
803
Adam Cohend22015c2010-07-26 22:02:18 -0700804 final int xAxis = mCountX;
805 final int yAxis = mCountY;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800806
807 if (result[0] < 0) result[0] = 0;
808 if (result[0] >= xAxis) result[0] = xAxis - 1;
809 if (result[1] < 0) result[1] = 0;
810 if (result[1] >= yAxis) result[1] = yAxis - 1;
811 }
Winson Chungaafa03c2010-06-11 17:34:16 -0700812
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800813 /**
814 * Given a point, return the cell that most closely encloses that point
815 * @param x X coordinate of the point
816 * @param y Y coordinate of the point
817 * @param result Array of 2 ints to hold the x and y coordinate of the cell
818 */
819 void pointToCellRounded(int x, int y, int[] result) {
820 pointToCellExact(x + (mCellWidth / 2), y + (mCellHeight / 2), result);
821 }
822
823 /**
824 * Given a cell coordinate, return the point that represents the upper left corner of that cell
Winson Chungaafa03c2010-06-11 17:34:16 -0700825 *
826 * @param cellX X coordinate of the cell
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800827 * @param cellY Y coordinate of the cell
Winson Chungaafa03c2010-06-11 17:34:16 -0700828 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800829 * @param result Array of 2 ints to hold the x and y coordinate of the point
830 */
831 void cellToPoint(int cellX, int cellY, int[] result) {
Winson Chung4b825dcd2011-06-19 12:41:22 -0700832 final int hStartPadding = getPaddingLeft();
833 final int vStartPadding = getPaddingTop();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800834
835 result[0] = hStartPadding + cellX * (mCellWidth + mWidthGap);
836 result[1] = vStartPadding + cellY * (mCellHeight + mHeightGap);
837 }
838
Adam Cohene3e27a82011-04-15 12:07:39 -0700839 /**
Adam Cohen482ed822012-03-02 14:15:13 -0800840 * Given a cell coordinate, return the point that represents the center of the cell
Adam Cohene3e27a82011-04-15 12:07:39 -0700841 *
842 * @param cellX X coordinate of the cell
843 * @param cellY Y coordinate of the cell
844 *
845 * @param result Array of 2 ints to hold the x and y coordinate of the point
846 */
847 void cellToCenterPoint(int cellX, int cellY, int[] result) {
Adam Cohen47a876d2012-03-19 13:21:41 -0700848 regionToCenterPoint(cellX, cellY, 1, 1, result);
849 }
850
851 /**
852 * Given a cell coordinate and span return the point that represents the center of the regio
853 *
854 * @param cellX X coordinate of the cell
855 * @param cellY Y coordinate of the cell
856 *
857 * @param result Array of 2 ints to hold the x and y coordinate of the point
858 */
859 void regionToCenterPoint(int cellX, int cellY, int spanX, int spanY, int[] result) {
Winson Chung4b825dcd2011-06-19 12:41:22 -0700860 final int hStartPadding = getPaddingLeft();
861 final int vStartPadding = getPaddingTop();
Adam Cohen47a876d2012-03-19 13:21:41 -0700862 result[0] = hStartPadding + cellX * (mCellWidth + mWidthGap) +
863 (spanX * mCellWidth + (spanX - 1) * mWidthGap) / 2;
864 result[1] = vStartPadding + cellY * (mCellHeight + mHeightGap) +
865 (spanY * mCellHeight + (spanY - 1) * mHeightGap) / 2;
Adam Cohene3e27a82011-04-15 12:07:39 -0700866 }
867
Adam Cohen19f37922012-03-21 11:59:11 -0700868 /**
869 * Given a cell coordinate and span fills out a corresponding pixel rect
870 *
871 * @param cellX X coordinate of the cell
872 * @param cellY Y coordinate of the cell
873 * @param result Rect in which to write the result
874 */
875 void regionToRect(int cellX, int cellY, int spanX, int spanY, Rect result) {
876 final int hStartPadding = getPaddingLeft();
877 final int vStartPadding = getPaddingTop();
878 final int left = hStartPadding + cellX * (mCellWidth + mWidthGap);
879 final int top = vStartPadding + cellY * (mCellHeight + mHeightGap);
880 result.set(left, top, left + (spanX * mCellWidth + (spanX - 1) * mWidthGap),
881 top + (spanY * mCellHeight + (spanY - 1) * mHeightGap));
882 }
883
Adam Cohen482ed822012-03-02 14:15:13 -0800884 public float getDistanceFromCell(float x, float y, int[] cell) {
885 cellToCenterPoint(cell[0], cell[1], mTmpPoint);
886 float distance = (float) Math.sqrt( Math.pow(x - mTmpPoint[0], 2) +
887 Math.pow(y - mTmpPoint[1], 2));
888 return distance;
889 }
890
Romain Guy84f296c2009-11-04 15:00:44 -0800891 int getCellWidth() {
892 return mCellWidth;
893 }
894
895 int getCellHeight() {
896 return mCellHeight;
897 }
898
Adam Cohend4844c32011-02-18 19:25:06 -0800899 int getWidthGap() {
900 return mWidthGap;
901 }
902
903 int getHeightGap() {
904 return mHeightGap;
905 }
906
Adam Cohen7f4eabe2011-04-21 16:19:16 -0700907 Rect getContentRect(Rect r) {
908 if (r == null) {
909 r = new Rect();
910 }
911 int left = getPaddingLeft();
912 int top = getPaddingTop();
Michael Jurka8b805b12012-04-18 14:23:14 -0700913 int right = left + getWidth() - getPaddingLeft() - getPaddingRight();
914 int bottom = top + getHeight() - getPaddingTop() - getPaddingBottom();
Adam Cohen7f4eabe2011-04-21 16:19:16 -0700915 r.set(left, top, right, bottom);
916 return r;
917 }
918
Adam Cohena897f392012-04-27 18:12:05 -0700919 static void getMetrics(Rect metrics, Resources res, int measureWidth, int measureHeight,
920 int countX, int countY, int orientation) {
921 int numWidthGaps = countX - 1;
922 int numHeightGaps = countY - 1;
Adam Cohenf4bd5792012-04-27 11:35:29 -0700923
924 int widthGap;
925 int heightGap;
926 int cellWidth;
927 int cellHeight;
928 int paddingLeft;
929 int paddingRight;
930 int paddingTop;
931 int paddingBottom;
932
Adam Cohena897f392012-04-27 18:12:05 -0700933 int maxGap = res.getDimensionPixelSize(R.dimen.workspace_max_gap);
Adam Cohenf4bd5792012-04-27 11:35:29 -0700934 if (orientation == LANDSCAPE) {
935 cellWidth = res.getDimensionPixelSize(R.dimen.workspace_cell_width_land);
936 cellHeight = res.getDimensionPixelSize(R.dimen.workspace_cell_height_land);
937 widthGap = res.getDimensionPixelSize(R.dimen.workspace_width_gap_land);
938 heightGap = res.getDimensionPixelSize(R.dimen.workspace_height_gap_land);
939 paddingLeft = res.getDimensionPixelSize(R.dimen.cell_layout_left_padding_land);
940 paddingRight = res.getDimensionPixelSize(R.dimen.cell_layout_right_padding_land);
941 paddingTop = res.getDimensionPixelSize(R.dimen.cell_layout_top_padding_land);
942 paddingBottom = res.getDimensionPixelSize(R.dimen.cell_layout_bottom_padding_land);
943 } else {
944 // PORTRAIT
945 cellWidth = res.getDimensionPixelSize(R.dimen.workspace_cell_width_port);
946 cellHeight = res.getDimensionPixelSize(R.dimen.workspace_cell_height_port);
947 widthGap = res.getDimensionPixelSize(R.dimen.workspace_width_gap_port);
948 heightGap = res.getDimensionPixelSize(R.dimen.workspace_height_gap_port);
949 paddingLeft = res.getDimensionPixelSize(R.dimen.cell_layout_left_padding_port);
950 paddingRight = res.getDimensionPixelSize(R.dimen.cell_layout_right_padding_port);
951 paddingTop = res.getDimensionPixelSize(R.dimen.cell_layout_top_padding_port);
952 paddingBottom = res.getDimensionPixelSize(R.dimen.cell_layout_bottom_padding_port);
953 }
954
955 if (widthGap < 0 || heightGap < 0) {
956 int hSpace = measureWidth - paddingLeft - paddingRight;
957 int vSpace = measureHeight - paddingTop - paddingBottom;
Adam Cohena897f392012-04-27 18:12:05 -0700958 int hFreeSpace = hSpace - (countX * cellWidth);
959 int vFreeSpace = vSpace - (countY * cellHeight);
960 widthGap = Math.min(maxGap, numWidthGaps > 0 ? (hFreeSpace / numWidthGaps) : 0);
961 heightGap = Math.min(maxGap, numHeightGaps > 0 ? (vFreeSpace / numHeightGaps) : 0);
Adam Cohenf4bd5792012-04-27 11:35:29 -0700962 }
963 metrics.set(cellWidth, cellHeight, widthGap, heightGap);
964 }
965
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800966 @Override
967 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800968 int widthSpecMode = MeasureSpec.getMode(widthMeasureSpec);
Winson Chungaafa03c2010-06-11 17:34:16 -0700969 int widthSpecSize = MeasureSpec.getSize(widthMeasureSpec);
970
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800971 int heightSpecMode = MeasureSpec.getMode(heightMeasureSpec);
972 int heightSpecSize = MeasureSpec.getSize(heightMeasureSpec);
Winson Chungaafa03c2010-06-11 17:34:16 -0700973
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800974 if (widthSpecMode == MeasureSpec.UNSPECIFIED || heightSpecMode == MeasureSpec.UNSPECIFIED) {
975 throw new RuntimeException("CellLayout cannot have UNSPECIFIED dimensions");
976 }
977
Adam Cohend22015c2010-07-26 22:02:18 -0700978 int numWidthGaps = mCountX - 1;
979 int numHeightGaps = mCountY - 1;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800980
Adam Cohen234c4cd2011-07-17 21:03:04 -0700981 if (mOriginalWidthGap < 0 || mOriginalHeightGap < 0) {
Michael Jurkadd13e3d2012-05-01 12:38:17 -0700982 int hSpace = widthSpecSize - getPaddingLeft() - getPaddingRight();
983 int vSpace = heightSpecSize - getPaddingTop() - getPaddingBottom();
Adam Cohenf4bd5792012-04-27 11:35:29 -0700984 int hFreeSpace = hSpace - (mCountX * mCellWidth);
985 int vFreeSpace = vSpace - (mCountY * mCellHeight);
Winson Chung4b825dcd2011-06-19 12:41:22 -0700986 mWidthGap = Math.min(mMaxGap, numWidthGaps > 0 ? (hFreeSpace / numWidthGaps) : 0);
987 mHeightGap = Math.min(mMaxGap,numHeightGaps > 0 ? (vFreeSpace / numHeightGaps) : 0);
Michael Jurkaa52570f2012-03-20 03:18:20 -0700988 mShortcutsAndWidgets.setCellDimensions(mCellWidth, mCellHeight, mWidthGap, mHeightGap);
Adam Cohen234c4cd2011-07-17 21:03:04 -0700989 } else {
990 mWidthGap = mOriginalWidthGap;
991 mHeightGap = mOriginalHeightGap;
Winson Chungece7f5b2010-10-22 14:54:12 -0700992 }
Michael Jurka5f1c5092010-09-03 14:15:02 -0700993
Michael Jurka8c920dd2011-01-20 14:16:56 -0800994 // Initial values correspond to widthSpecMode == MeasureSpec.EXACTLY
995 int newWidth = widthSpecSize;
996 int newHeight = heightSpecSize;
Michael Jurka5f1c5092010-09-03 14:15:02 -0700997 if (widthSpecMode == MeasureSpec.AT_MOST) {
Michael Jurka8b805b12012-04-18 14:23:14 -0700998 newWidth = getPaddingLeft() + getPaddingRight() + (mCountX * mCellWidth) +
Winson Chungece7f5b2010-10-22 14:54:12 -0700999 ((mCountX - 1) * mWidthGap);
Michael Jurka8b805b12012-04-18 14:23:14 -07001000 newHeight = getPaddingTop() + getPaddingBottom() + (mCountY * mCellHeight) +
Winson Chungece7f5b2010-10-22 14:54:12 -07001001 ((mCountY - 1) * mHeightGap);
Michael Jurka5f1c5092010-09-03 14:15:02 -07001002 setMeasuredDimension(newWidth, newHeight);
Michael Jurka5f1c5092010-09-03 14:15:02 -07001003 }
Michael Jurka8c920dd2011-01-20 14:16:56 -08001004
1005 int count = getChildCount();
1006 for (int i = 0; i < count; i++) {
1007 View child = getChildAt(i);
Michael Jurka8b805b12012-04-18 14:23:14 -07001008 int childWidthMeasureSpec = MeasureSpec.makeMeasureSpec(newWidth - getPaddingLeft() -
1009 getPaddingRight(), MeasureSpec.EXACTLY);
1010 int childheightMeasureSpec = MeasureSpec.makeMeasureSpec(newHeight - getPaddingTop() -
1011 getPaddingBottom(), MeasureSpec.EXACTLY);
Michael Jurka8c920dd2011-01-20 14:16:56 -08001012 child.measure(childWidthMeasureSpec, childheightMeasureSpec);
1013 }
1014 setMeasuredDimension(newWidth, newHeight);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001015 }
1016
1017 @Override
Michael Jurka28750fb2010-09-24 17:43:49 -07001018 protected void onLayout(boolean changed, int l, int t, int r, int b) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001019 int count = getChildCount();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001020 for (int i = 0; i < count; i++) {
Michael Jurka8c920dd2011-01-20 14:16:56 -08001021 View child = getChildAt(i);
Michael Jurka8b805b12012-04-18 14:23:14 -07001022 child.layout(getPaddingLeft(), getPaddingTop(),
1023 r - l - getPaddingRight(), b - t - getPaddingBottom());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001024 }
1025 }
1026
1027 @Override
Michael Jurkadee05892010-07-27 10:01:56 -07001028 protected void onSizeChanged(int w, int h, int oldw, int oldh) {
1029 super.onSizeChanged(w, h, oldw, oldh);
Michael Jurka18014792010-10-14 09:01:34 -07001030 mBackgroundRect.set(0, 0, w, h);
Adam Cohenb5ba0972011-09-07 18:02:31 -07001031 mForegroundRect.set(mForegroundPadding, mForegroundPadding,
Adam Cohen215b4162012-08-30 13:14:08 -07001032 w - mForegroundPadding, h - mForegroundPadding);
Michael Jurkadee05892010-07-27 10:01:56 -07001033 }
1034
1035 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001036 protected void setChildrenDrawingCacheEnabled(boolean enabled) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07001037 mShortcutsAndWidgets.setChildrenDrawingCacheEnabled(enabled);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001038 }
1039
1040 @Override
1041 protected void setChildrenDrawnWithCacheEnabled(boolean enabled) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07001042 mShortcutsAndWidgets.setChildrenDrawnWithCacheEnabled(enabled);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001043 }
1044
Michael Jurka5f1c5092010-09-03 14:15:02 -07001045 public float getBackgroundAlpha() {
1046 return mBackgroundAlpha;
Michael Jurkadee05892010-07-27 10:01:56 -07001047 }
1048
Adam Cohen1b0aaac2010-10-28 11:11:18 -07001049 public void setBackgroundAlphaMultiplier(float multiplier) {
Michael Jurkaa3d30ad2012-05-08 13:43:43 -07001050 if (mBackgroundAlphaMultiplier != multiplier) {
1051 mBackgroundAlphaMultiplier = multiplier;
1052 invalidate();
1053 }
Adam Cohen1b0aaac2010-10-28 11:11:18 -07001054 }
1055
Adam Cohenddb82192010-11-10 16:32:54 -08001056 public float getBackgroundAlphaMultiplier() {
1057 return mBackgroundAlphaMultiplier;
1058 }
1059
Michael Jurka5f1c5092010-09-03 14:15:02 -07001060 public void setBackgroundAlpha(float alpha) {
Michael Jurkaafaa0502011-12-13 18:22:50 -08001061 if (mBackgroundAlpha != alpha) {
1062 mBackgroundAlpha = alpha;
1063 invalidate();
1064 }
Michael Jurkadee05892010-07-27 10:01:56 -07001065 }
1066
Michael Jurkaa52570f2012-03-20 03:18:20 -07001067 public void setShortcutAndWidgetAlpha(float alpha) {
Michael Jurka0142d492010-08-25 17:46:15 -07001068 final int childCount = getChildCount();
1069 for (int i = 0; i < childCount; i++) {
Michael Jurkadee05892010-07-27 10:01:56 -07001070 getChildAt(i).setAlpha(alpha);
1071 }
1072 }
1073
Michael Jurkaa52570f2012-03-20 03:18:20 -07001074 public ShortcutAndWidgetContainer getShortcutsAndWidgets() {
1075 if (getChildCount() > 0) {
1076 return (ShortcutAndWidgetContainer) getChildAt(0);
1077 }
1078 return null;
1079 }
1080
Patrick Dubroy440c3602010-07-13 17:50:32 -07001081 public View getChildAt(int x, int y) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07001082 return mShortcutsAndWidgets.getChildAt(x, y);
Patrick Dubroy440c3602010-07-13 17:50:32 -07001083 }
1084
Adam Cohen76fc0852011-06-17 13:26:23 -07001085 public boolean animateChildToPosition(final View child, int cellX, int cellY, int duration,
Adam Cohen482ed822012-03-02 14:15:13 -08001086 int delay, boolean permanent, boolean adjustOccupied) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07001087 ShortcutAndWidgetContainer clc = getShortcutsAndWidgets();
Adam Cohen482ed822012-03-02 14:15:13 -08001088 boolean[][] occupied = mOccupied;
1089 if (!permanent) {
1090 occupied = mTmpOccupied;
1091 }
1092
Adam Cohen19f37922012-03-21 11:59:11 -07001093 if (clc.indexOfChild(child) != -1) {
Adam Cohenbfbfd262011-06-13 16:55:12 -07001094 final LayoutParams lp = (LayoutParams) child.getLayoutParams();
1095 final ItemInfo info = (ItemInfo) child.getTag();
1096
1097 // We cancel any existing animations
1098 if (mReorderAnimators.containsKey(lp)) {
1099 mReorderAnimators.get(lp).cancel();
1100 mReorderAnimators.remove(lp);
1101 }
1102
Adam Cohen482ed822012-03-02 14:15:13 -08001103 final int oldX = lp.x;
1104 final int oldY = lp.y;
1105 if (adjustOccupied) {
1106 occupied[lp.cellX][lp.cellY] = false;
1107 occupied[cellX][cellY] = true;
1108 }
Adam Cohenbfbfd262011-06-13 16:55:12 -07001109 lp.isLockedToGrid = true;
Adam Cohen482ed822012-03-02 14:15:13 -08001110 if (permanent) {
1111 lp.cellX = info.cellX = cellX;
1112 lp.cellY = info.cellY = cellY;
1113 } else {
1114 lp.tmpCellX = cellX;
1115 lp.tmpCellY = cellY;
1116 }
Adam Cohenbfbfd262011-06-13 16:55:12 -07001117 clc.setupLp(lp);
1118 lp.isLockedToGrid = false;
Adam Cohen482ed822012-03-02 14:15:13 -08001119 final int newX = lp.x;
1120 final int newY = lp.y;
Adam Cohenbfbfd262011-06-13 16:55:12 -07001121
Adam Cohen76fc0852011-06-17 13:26:23 -07001122 lp.x = oldX;
1123 lp.y = oldY;
Adam Cohen76fc0852011-06-17 13:26:23 -07001124
Adam Cohen482ed822012-03-02 14:15:13 -08001125 // Exit early if we're not actually moving the view
1126 if (oldX == newX && oldY == newY) {
1127 lp.isLockedToGrid = true;
1128 return true;
1129 }
1130
Michael Jurka2ecf9952012-06-18 12:52:28 -07001131 ValueAnimator va = LauncherAnimUtils.ofFloat(0f, 1f);
Adam Cohen482ed822012-03-02 14:15:13 -08001132 va.setDuration(duration);
1133 mReorderAnimators.put(lp, va);
1134
1135 va.addUpdateListener(new AnimatorUpdateListener() {
1136 @Override
Adam Cohenbfbfd262011-06-13 16:55:12 -07001137 public void onAnimationUpdate(ValueAnimator animation) {
Adam Cohen482ed822012-03-02 14:15:13 -08001138 float r = ((Float) animation.getAnimatedValue()).floatValue();
Adam Cohen19f37922012-03-21 11:59:11 -07001139 lp.x = (int) ((1 - r) * oldX + r * newX);
1140 lp.y = (int) ((1 - r) * oldY + r * newY);
Adam Cohen6b8a02d2012-03-22 15:13:40 -07001141 child.requestLayout();
Adam Cohenbfbfd262011-06-13 16:55:12 -07001142 }
1143 });
Adam Cohen482ed822012-03-02 14:15:13 -08001144 va.addListener(new AnimatorListenerAdapter() {
Adam Cohenbfbfd262011-06-13 16:55:12 -07001145 boolean cancelled = false;
1146 public void onAnimationEnd(Animator animation) {
1147 // If the animation was cancelled, it means that another animation
1148 // has interrupted this one, and we don't want to lock the item into
1149 // place just yet.
1150 if (!cancelled) {
1151 lp.isLockedToGrid = true;
Adam Cohen482ed822012-03-02 14:15:13 -08001152 child.requestLayout();
Adam Cohenbfbfd262011-06-13 16:55:12 -07001153 }
1154 if (mReorderAnimators.containsKey(lp)) {
1155 mReorderAnimators.remove(lp);
1156 }
1157 }
1158 public void onAnimationCancel(Animator animation) {
1159 cancelled = true;
1160 }
1161 });
Adam Cohen482ed822012-03-02 14:15:13 -08001162 va.setStartDelay(delay);
1163 va.start();
Adam Cohenbfbfd262011-06-13 16:55:12 -07001164 return true;
1165 }
1166 return false;
1167 }
1168
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001169 /**
1170 * Estimate where the top left cell of the dragged item will land if it is dropped.
1171 *
1172 * @param originX The X value of the top left corner of the item
1173 * @param originY The Y value of the top left corner of the item
1174 * @param spanX The number of horizontal cells that the item spans
1175 * @param spanY The number of vertical cells that the item spans
1176 * @param result The estimated drop cell X and Y.
1177 */
1178 void estimateDropCell(int originX, int originY, int spanX, int spanY, int[] result) {
Adam Cohend22015c2010-07-26 22:02:18 -07001179 final int countX = mCountX;
1180 final int countY = mCountY;
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001181
Michael Jurkaa63c4522010-08-19 13:52:27 -07001182 // pointToCellRounded takes the top left of a cell but will pad that with
1183 // cellWidth/2 and cellHeight/2 when finding the matching cell
1184 pointToCellRounded(originX, originY, result);
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001185
1186 // If the item isn't fully on this screen, snap to the edges
1187 int rightOverhang = result[0] + spanX - countX;
1188 if (rightOverhang > 0) {
1189 result[0] -= rightOverhang; // Snap to right
1190 }
1191 result[0] = Math.max(0, result[0]); // Snap to left
1192 int bottomOverhang = result[1] + spanY - countY;
1193 if (bottomOverhang > 0) {
1194 result[1] -= bottomOverhang; // Snap to bottom
1195 }
1196 result[1] = Math.max(0, result[1]); // Snap to top
1197 }
1198
Adam Cohen482ed822012-03-02 14:15:13 -08001199 void visualizeDropLocation(View v, Bitmap dragOutline, int originX, int originY, int cellX,
1200 int cellY, int spanX, int spanY, boolean resize, Point dragOffset, Rect dragRegion) {
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -07001201 final int oldDragCellX = mDragCell[0];
1202 final int oldDragCellY = mDragCell[1];
Adam Cohen482ed822012-03-02 14:15:13 -08001203
Winson Chungb8c69f32011-10-19 21:36:08 -07001204 if (v != null && dragOffset == null) {
Winson Chunga9abd0e2010-10-27 17:18:37 -07001205 mDragCenter.set(originX + (v.getWidth() / 2), originY + (v.getHeight() / 2));
1206 } else {
1207 mDragCenter.set(originX, originY);
1208 }
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001209
Adam Cohen2801caf2011-05-13 20:57:39 -07001210 if (dragOutline == null && v == null) {
Adam Cohen2801caf2011-05-13 20:57:39 -07001211 return;
1212 }
1213
Adam Cohen482ed822012-03-02 14:15:13 -08001214 if (cellX != oldDragCellX || cellY != oldDragCellY) {
1215 mDragCell[0] = cellX;
1216 mDragCell[1] = cellY;
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001217 // Find the top left corner of the rect the object will occupy
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001218 final int[] topLeft = mTmpPoint;
Adam Cohen482ed822012-03-02 14:15:13 -08001219 cellToPoint(cellX, cellY, topLeft);
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001220
Joe Onorato4be866d2010-10-10 11:26:02 -07001221 int left = topLeft[0];
1222 int top = topLeft[1];
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001223
Winson Chungb8c69f32011-10-19 21:36:08 -07001224 if (v != null && dragOffset == null) {
Adam Cohen99e8b402011-03-25 19:23:43 -07001225 // When drawing the drag outline, it did not account for margin offsets
1226 // added by the view's parent.
1227 MarginLayoutParams lp = (MarginLayoutParams) v.getLayoutParams();
1228 left += lp.leftMargin;
1229 top += lp.topMargin;
Winson Chung150fbab2010-09-29 17:14:26 -07001230
Adam Cohen99e8b402011-03-25 19:23:43 -07001231 // Offsets due to the size difference between the View and the dragOutline.
1232 // There is a size difference to account for the outer blur, which may lie
1233 // outside the bounds of the view.
Winson Chunga9abd0e2010-10-27 17:18:37 -07001234 top += (v.getHeight() - dragOutline.getHeight()) / 2;
Adam Cohenae915ce2011-08-25 13:47:22 -07001235 // We center about the x axis
1236 left += ((mCellWidth * spanX) + ((spanX - 1) * mWidthGap)
1237 - dragOutline.getWidth()) / 2;
Adam Cohen66396872011-04-15 17:50:36 -07001238 } else {
Winson Chungb8c69f32011-10-19 21:36:08 -07001239 if (dragOffset != null && dragRegion != null) {
1240 // Center the drag region *horizontally* in the cell and apply a drag
1241 // outline offset
1242 left += dragOffset.x + ((mCellWidth * spanX) + ((spanX - 1) * mWidthGap)
1243 - dragRegion.width()) / 2;
1244 top += dragOffset.y;
1245 } else {
1246 // Center the drag outline in the cell
1247 left += ((mCellWidth * spanX) + ((spanX - 1) * mWidthGap)
1248 - dragOutline.getWidth()) / 2;
1249 top += ((mCellHeight * spanY) + ((spanY - 1) * mHeightGap)
1250 - dragOutline.getHeight()) / 2;
1251 }
Winson Chunga9abd0e2010-10-27 17:18:37 -07001252 }
Joe Onorato4be866d2010-10-10 11:26:02 -07001253 final int oldIndex = mDragOutlineCurrent;
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -07001254 mDragOutlineAnims[oldIndex].animateOut();
1255 mDragOutlineCurrent = (oldIndex + 1) % mDragOutlines.length;
Adam Cohend41fbf52012-02-16 23:53:59 -08001256 Rect r = mDragOutlines[mDragOutlineCurrent];
1257 r.set(left, top, left + dragOutline.getWidth(), top + dragOutline.getHeight());
1258 if (resize) {
Adam Cohen482ed822012-03-02 14:15:13 -08001259 cellToRect(cellX, cellY, spanX, spanY, r);
Adam Cohend41fbf52012-02-16 23:53:59 -08001260 }
Winson Chung150fbab2010-09-29 17:14:26 -07001261
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -07001262 mDragOutlineAnims[mDragOutlineCurrent].setTag(dragOutline);
1263 mDragOutlineAnims[mDragOutlineCurrent].animateIn();
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001264 }
1265 }
1266
Adam Cohene0310962011-04-18 16:15:31 -07001267 public void clearDragOutlines() {
1268 final int oldIndex = mDragOutlineCurrent;
1269 mDragOutlineAnims[oldIndex].animateOut();
Adam Cohend41fbf52012-02-16 23:53:59 -08001270 mDragCell[0] = mDragCell[1] = -1;
Adam Cohene0310962011-04-18 16:15:31 -07001271 }
1272
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001273 /**
Jeff Sharkey70864282009-04-07 21:08:40 -07001274 * Find a vacant area that will fit the given bounds nearest the requested
1275 * cell location. Uses Euclidean distance to score multiple vacant areas.
Winson Chungaafa03c2010-06-11 17:34:16 -07001276 *
Romain Guy51afc022009-05-04 18:03:43 -07001277 * @param pixelX The X location at which you want to search for a vacant area.
1278 * @param pixelY The Y location at which you want to search for a vacant area.
Jeff Sharkey70864282009-04-07 21:08:40 -07001279 * @param spanX Horizontal span of the object.
1280 * @param spanY Vertical span of the object.
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001281 * @param result Array in which to place the result, or null (in which case a new array will
1282 * be allocated)
Jeff Sharkey70864282009-04-07 21:08:40 -07001283 * @return The X, Y cell of a vacant area that can contain this object,
1284 * nearest the requested location.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001285 */
Adam Cohend41fbf52012-02-16 23:53:59 -08001286 int[] findNearestVacantArea(int pixelX, int pixelY, int spanX, int spanY,
1287 int[] result) {
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001288 return findNearestVacantArea(pixelX, pixelY, spanX, spanY, null, result);
Michael Jurka6a1435d2010-09-27 17:35:12 -07001289 }
Winson Chungaafa03c2010-06-11 17:34:16 -07001290
Michael Jurka6a1435d2010-09-27 17:35:12 -07001291 /**
1292 * Find a vacant area that will fit the given bounds nearest the requested
1293 * cell location. Uses Euclidean distance to score multiple vacant areas.
1294 *
1295 * @param pixelX The X location at which you want to search for a vacant area.
1296 * @param pixelY The Y location at which you want to search for a vacant area.
Adam Cohend41fbf52012-02-16 23:53:59 -08001297 * @param minSpanX The minimum horizontal span required
1298 * @param minSpanY The minimum vertical span required
1299 * @param spanX Horizontal span of the object.
1300 * @param spanY Vertical span of the object.
1301 * @param result Array in which to place the result, or null (in which case a new array will
1302 * be allocated)
1303 * @return The X, Y cell of a vacant area that can contain this object,
1304 * nearest the requested location.
1305 */
1306 int[] findNearestVacantArea(int pixelX, int pixelY, int minSpanX, int minSpanY, int spanX,
1307 int spanY, int[] result, int[] resultSpan) {
1308 return findNearestVacantArea(pixelX, pixelY, minSpanX, minSpanY, spanX, spanY, null,
1309 result, resultSpan);
1310 }
1311
1312 /**
1313 * Find a vacant area that will fit the given bounds nearest the requested
1314 * cell location. Uses Euclidean distance to score multiple vacant areas.
1315 *
1316 * @param pixelX The X location at which you want to search for a vacant area.
1317 * @param pixelY The Y location at which you want to search for a vacant area.
Michael Jurka6a1435d2010-09-27 17:35:12 -07001318 * @param spanX Horizontal span of the object.
1319 * @param spanY Vertical span of the object.
Adam Cohendf035382011-04-11 17:22:04 -07001320 * @param ignoreOccupied If true, the result can be an occupied cell
1321 * @param result Array in which to place the result, or null (in which case a new array will
1322 * be allocated)
Michael Jurka6a1435d2010-09-27 17:35:12 -07001323 * @return The X, Y cell of a vacant area that can contain this object,
1324 * nearest the requested location.
1325 */
Adam Cohendf035382011-04-11 17:22:04 -07001326 int[] findNearestArea(int pixelX, int pixelY, int spanX, int spanY, View ignoreView,
1327 boolean ignoreOccupied, int[] result) {
Adam Cohend41fbf52012-02-16 23:53:59 -08001328 return findNearestArea(pixelX, pixelY, spanX, spanY,
Adam Cohen482ed822012-03-02 14:15:13 -08001329 spanX, spanY, ignoreView, ignoreOccupied, result, null, mOccupied);
Adam Cohend41fbf52012-02-16 23:53:59 -08001330 }
1331
1332 private final Stack<Rect> mTempRectStack = new Stack<Rect>();
1333 private void lazyInitTempRectStack() {
1334 if (mTempRectStack.isEmpty()) {
1335 for (int i = 0; i < mCountX * mCountY; i++) {
1336 mTempRectStack.push(new Rect());
1337 }
1338 }
1339 }
Adam Cohen482ed822012-03-02 14:15:13 -08001340
Adam Cohend41fbf52012-02-16 23:53:59 -08001341 private void recycleTempRects(Stack<Rect> used) {
1342 while (!used.isEmpty()) {
1343 mTempRectStack.push(used.pop());
1344 }
1345 }
1346
1347 /**
1348 * Find a vacant area that will fit the given bounds nearest the requested
1349 * cell location. Uses Euclidean distance to score multiple vacant areas.
1350 *
1351 * @param pixelX The X location at which you want to search for a vacant area.
1352 * @param pixelY The Y location at which you want to search for a vacant area.
1353 * @param minSpanX The minimum horizontal span required
1354 * @param minSpanY The minimum vertical span required
1355 * @param spanX Horizontal span of the object.
1356 * @param spanY Vertical span of the object.
1357 * @param ignoreOccupied If true, the result can be an occupied cell
1358 * @param result Array in which to place the result, or null (in which case a new array will
1359 * be allocated)
1360 * @return The X, Y cell of a vacant area that can contain this object,
1361 * nearest the requested location.
1362 */
1363 int[] findNearestArea(int pixelX, int pixelY, int minSpanX, int minSpanY, int spanX, int spanY,
Adam Cohen482ed822012-03-02 14:15:13 -08001364 View ignoreView, boolean ignoreOccupied, int[] result, int[] resultSpan,
1365 boolean[][] occupied) {
Adam Cohend41fbf52012-02-16 23:53:59 -08001366 lazyInitTempRectStack();
Michael Jurkac6ee42e2010-09-30 12:04:50 -07001367 // mark space take by ignoreView as available (method checks if ignoreView is null)
Adam Cohen482ed822012-03-02 14:15:13 -08001368 markCellsAsUnoccupiedForView(ignoreView, occupied);
Michael Jurkac6ee42e2010-09-30 12:04:50 -07001369
Adam Cohene3e27a82011-04-15 12:07:39 -07001370 // For items with a spanX / spanY > 1, the passed in point (pixelX, pixelY) corresponds
1371 // to the center of the item, but we are searching based on the top-left cell, so
1372 // we translate the point over to correspond to the top-left.
1373 pixelX -= (mCellWidth + mWidthGap) * (spanX - 1) / 2f;
1374 pixelY -= (mCellHeight + mHeightGap) * (spanY - 1) / 2f;
1375
Jeff Sharkey70864282009-04-07 21:08:40 -07001376 // Keep track of best-scoring drop area
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001377 final int[] bestXY = result != null ? result : new int[2];
Jeff Sharkey70864282009-04-07 21:08:40 -07001378 double bestDistance = Double.MAX_VALUE;
Adam Cohend41fbf52012-02-16 23:53:59 -08001379 final Rect bestRect = new Rect(-1, -1, -1, -1);
1380 final Stack<Rect> validRegions = new Stack<Rect>();
Winson Chungaafa03c2010-06-11 17:34:16 -07001381
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001382 final int countX = mCountX;
1383 final int countY = mCountY;
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001384
Adam Cohend41fbf52012-02-16 23:53:59 -08001385 if (minSpanX <= 0 || minSpanY <= 0 || spanX <= 0 || spanY <= 0 ||
1386 spanX < minSpanX || spanY < minSpanY) {
1387 return bestXY;
1388 }
1389
1390 for (int y = 0; y < countY - (minSpanY - 1); y++) {
Michael Jurkac28de512010-08-13 11:27:44 -07001391 inner:
Adam Cohend41fbf52012-02-16 23:53:59 -08001392 for (int x = 0; x < countX - (minSpanX - 1); x++) {
1393 int ySize = -1;
1394 int xSize = -1;
Adam Cohendf035382011-04-11 17:22:04 -07001395 if (ignoreOccupied) {
Adam Cohend41fbf52012-02-16 23:53:59 -08001396 // First, let's see if this thing fits anywhere
1397 for (int i = 0; i < minSpanX; i++) {
1398 for (int j = 0; j < minSpanY; j++) {
Adam Cohendf035382011-04-11 17:22:04 -07001399 if (occupied[x + i][y + j]) {
Adam Cohendf035382011-04-11 17:22:04 -07001400 continue inner;
1401 }
Michael Jurkac28de512010-08-13 11:27:44 -07001402 }
1403 }
Adam Cohend41fbf52012-02-16 23:53:59 -08001404 xSize = minSpanX;
1405 ySize = minSpanY;
1406
1407 // We know that the item will fit at _some_ acceptable size, now let's see
1408 // how big we can make it. We'll alternate between incrementing x and y spans
1409 // until we hit a limit.
1410 boolean incX = true;
1411 boolean hitMaxX = xSize >= spanX;
1412 boolean hitMaxY = ySize >= spanY;
1413 while (!(hitMaxX && hitMaxY)) {
1414 if (incX && !hitMaxX) {
1415 for (int j = 0; j < ySize; j++) {
1416 if (x + xSize > countX -1 || occupied[x + xSize][y + j]) {
1417 // We can't move out horizontally
1418 hitMaxX = true;
1419 }
1420 }
1421 if (!hitMaxX) {
1422 xSize++;
1423 }
1424 } else if (!hitMaxY) {
1425 for (int i = 0; i < xSize; i++) {
1426 if (y + ySize > countY - 1 || occupied[x + i][y + ySize]) {
1427 // We can't move out vertically
1428 hitMaxY = true;
1429 }
1430 }
1431 if (!hitMaxY) {
1432 ySize++;
1433 }
1434 }
1435 hitMaxX |= xSize >= spanX;
1436 hitMaxY |= ySize >= spanY;
1437 incX = !incX;
1438 }
1439 incX = true;
1440 hitMaxX = xSize >= spanX;
1441 hitMaxY = ySize >= spanY;
Michael Jurkac28de512010-08-13 11:27:44 -07001442 }
Winson Chung0be025d2011-05-23 17:45:09 -07001443 final int[] cellXY = mTmpXY;
Adam Cohene3e27a82011-04-15 12:07:39 -07001444 cellToCenterPoint(x, y, cellXY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001445
Adam Cohend41fbf52012-02-16 23:53:59 -08001446 // We verify that the current rect is not a sub-rect of any of our previous
1447 // candidates. In this case, the current rect is disqualified in favour of the
1448 // containing rect.
1449 Rect currentRect = mTempRectStack.pop();
1450 currentRect.set(x, y, x + xSize, y + ySize);
1451 boolean contained = false;
1452 for (Rect r : validRegions) {
1453 if (r.contains(currentRect)) {
1454 contained = true;
1455 break;
1456 }
1457 }
1458 validRegions.push(currentRect);
Michael Jurkac28de512010-08-13 11:27:44 -07001459 double distance = Math.sqrt(Math.pow(cellXY[0] - pixelX, 2)
1460 + Math.pow(cellXY[1] - pixelY, 2));
Adam Cohen482ed822012-03-02 14:15:13 -08001461
Adam Cohend41fbf52012-02-16 23:53:59 -08001462 if ((distance <= bestDistance && !contained) ||
1463 currentRect.contains(bestRect)) {
Michael Jurkac28de512010-08-13 11:27:44 -07001464 bestDistance = distance;
1465 bestXY[0] = x;
1466 bestXY[1] = y;
Adam Cohend41fbf52012-02-16 23:53:59 -08001467 if (resultSpan != null) {
1468 resultSpan[0] = xSize;
1469 resultSpan[1] = ySize;
1470 }
1471 bestRect.set(currentRect);
Michael Jurkac28de512010-08-13 11:27:44 -07001472 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001473 }
1474 }
Michael Jurkac6ee42e2010-09-30 12:04:50 -07001475 // re-mark space taken by ignoreView as occupied
Adam Cohen482ed822012-03-02 14:15:13 -08001476 markCellsAsOccupiedForView(ignoreView, occupied);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001477
Adam Cohenc0dcf592011-06-01 15:30:43 -07001478 // Return -1, -1 if no suitable location found
1479 if (bestDistance == Double.MAX_VALUE) {
1480 bestXY[0] = -1;
1481 bestXY[1] = -1;
Jeff Sharkey70864282009-04-07 21:08:40 -07001482 }
Adam Cohend41fbf52012-02-16 23:53:59 -08001483 recycleTempRects(validRegions);
Adam Cohenc0dcf592011-06-01 15:30:43 -07001484 return bestXY;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001485 }
Winson Chungaafa03c2010-06-11 17:34:16 -07001486
Adam Cohen482ed822012-03-02 14:15:13 -08001487 /**
1488 * Find a vacant area that will fit the given bounds nearest the requested
1489 * cell location, and will also weigh in a suggested direction vector of the
1490 * desired location. This method computers distance based on unit grid distances,
1491 * not pixel distances.
1492 *
Adam Cohen47a876d2012-03-19 13:21:41 -07001493 * @param cellX The X cell nearest to which you want to search for a vacant area.
1494 * @param cellY The Y cell nearest which you want to search for a vacant area.
Adam Cohen482ed822012-03-02 14:15:13 -08001495 * @param spanX Horizontal span of the object.
1496 * @param spanY Vertical span of the object.
Adam Cohen47a876d2012-03-19 13:21:41 -07001497 * @param direction The favored direction in which the views should move from x, y
1498 * @param exactDirectionOnly If this parameter is true, then only solutions where the direction
1499 * matches exactly. Otherwise we find the best matching direction.
1500 * @param occoupied The array which represents which cells in the CellLayout are occupied
1501 * @param blockOccupied The array which represents which cells in the specified block (cellX,
1502 * cellY, spanX, spanY) are occupied. This is used when try to move a group of views.
Adam Cohen482ed822012-03-02 14:15:13 -08001503 * @param result Array in which to place the result, or null (in which case a new array will
1504 * be allocated)
1505 * @return The X, Y cell of a vacant area that can contain this object,
1506 * nearest the requested location.
1507 */
1508 private int[] findNearestArea(int cellX, int cellY, int spanX, int spanY, int[] direction,
Adam Cohen47a876d2012-03-19 13:21:41 -07001509 boolean[][] occupied, boolean blockOccupied[][], int[] result) {
Adam Cohen482ed822012-03-02 14:15:13 -08001510 // Keep track of best-scoring drop area
1511 final int[] bestXY = result != null ? result : new int[2];
1512 float bestDistance = Float.MAX_VALUE;
1513 int bestDirectionScore = Integer.MIN_VALUE;
1514
1515 final int countX = mCountX;
1516 final int countY = mCountY;
1517
1518 for (int y = 0; y < countY - (spanY - 1); y++) {
1519 inner:
1520 for (int x = 0; x < countX - (spanX - 1); x++) {
1521 // First, let's see if this thing fits anywhere
1522 for (int i = 0; i < spanX; i++) {
1523 for (int j = 0; j < spanY; j++) {
Adam Cohen47a876d2012-03-19 13:21:41 -07001524 if (occupied[x + i][y + j] && (blockOccupied == null || blockOccupied[i][j])) {
Adam Cohen482ed822012-03-02 14:15:13 -08001525 continue inner;
1526 }
1527 }
1528 }
1529
1530 float distance = (float)
1531 Math.sqrt((x - cellX) * (x - cellX) + (y - cellY) * (y - cellY));
1532 int[] curDirection = mTmpPoint;
Adam Cohen47a876d2012-03-19 13:21:41 -07001533 computeDirectionVector(x - cellX, y - cellY, curDirection);
1534 // The direction score is just the dot product of the two candidate direction
1535 // and that passed in.
Adam Cohen482ed822012-03-02 14:15:13 -08001536 int curDirectionScore = direction[0] * curDirection[0] +
1537 direction[1] * curDirection[1];
Adam Cohen47a876d2012-03-19 13:21:41 -07001538 boolean exactDirectionOnly = false;
1539 boolean directionMatches = direction[0] == curDirection[0] &&
1540 direction[0] == curDirection[0];
1541 if ((directionMatches || !exactDirectionOnly) &&
1542 Float.compare(distance, bestDistance) < 0 || (Float.compare(distance,
Adam Cohen482ed822012-03-02 14:15:13 -08001543 bestDistance) == 0 && curDirectionScore > bestDirectionScore)) {
1544 bestDistance = distance;
1545 bestDirectionScore = curDirectionScore;
1546 bestXY[0] = x;
1547 bestXY[1] = y;
1548 }
1549 }
1550 }
1551
1552 // Return -1, -1 if no suitable location found
1553 if (bestDistance == Float.MAX_VALUE) {
1554 bestXY[0] = -1;
1555 bestXY[1] = -1;
1556 }
1557 return bestXY;
1558 }
1559
1560 private boolean addViewToTempLocation(View v, Rect rectOccupiedByPotentialDrop,
Adam Cohen8baab352012-03-20 17:39:21 -07001561 int[] direction, ItemConfiguration currentState) {
1562 CellAndSpan c = currentState.map.get(v);
Adam Cohen482ed822012-03-02 14:15:13 -08001563 boolean success = false;
Adam Cohen8baab352012-03-20 17:39:21 -07001564 markCellsForView(c.x, c.y, c.spanX, c.spanY, mTmpOccupied, false);
Adam Cohen482ed822012-03-02 14:15:13 -08001565 markCellsForRect(rectOccupiedByPotentialDrop, mTmpOccupied, true);
1566
Adam Cohen8baab352012-03-20 17:39:21 -07001567 findNearestArea(c.x, c.y, c.spanX, c.spanY, direction, mTmpOccupied, null, mTempLocation);
Adam Cohen482ed822012-03-02 14:15:13 -08001568
1569 if (mTempLocation[0] >= 0 && mTempLocation[1] >= 0) {
Adam Cohen8baab352012-03-20 17:39:21 -07001570 c.x = mTempLocation[0];
1571 c.y = mTempLocation[1];
Adam Cohen482ed822012-03-02 14:15:13 -08001572 success = true;
Adam Cohen482ed822012-03-02 14:15:13 -08001573 }
Adam Cohen8baab352012-03-20 17:39:21 -07001574 markCellsForView(c.x, c.y, c.spanX, c.spanY, mTmpOccupied, true);
Adam Cohen482ed822012-03-02 14:15:13 -08001575 return success;
1576 }
1577
Adam Cohenf3900c22012-11-16 18:28:11 -08001578 /**
1579 * This helper class defines a cluster of views. It helps with defining complex edges
1580 * of the cluster and determining how those edges interact with other views. The edges
1581 * essentially define a fine-grained boundary around the cluster of views -- like a more
1582 * precise version of a bounding box.
1583 */
1584 private class ViewCluster {
1585 final static int LEFT = 0;
1586 final static int TOP = 1;
1587 final static int RIGHT = 2;
1588 final static int BOTTOM = 3;
Adam Cohen47a876d2012-03-19 13:21:41 -07001589
Adam Cohenf3900c22012-11-16 18:28:11 -08001590 ArrayList<View> views;
1591 ItemConfiguration config;
1592 Rect boundingRect = new Rect();
Adam Cohen47a876d2012-03-19 13:21:41 -07001593
Adam Cohenf3900c22012-11-16 18:28:11 -08001594 int[] leftEdge = new int[mCountY];
1595 int[] rightEdge = new int[mCountY];
1596 int[] topEdge = new int[mCountX];
1597 int[] bottomEdge = new int[mCountX];
1598 boolean leftEdgeDirty, rightEdgeDirty, topEdgeDirty, bottomEdgeDirty, boundingRectDirty;
1599
1600 @SuppressWarnings("unchecked")
1601 public ViewCluster(ArrayList<View> views, ItemConfiguration config) {
1602 this.views = (ArrayList<View>) views.clone();
1603 this.config = config;
1604 resetEdges();
Adam Cohen47a876d2012-03-19 13:21:41 -07001605 }
1606
Adam Cohenf3900c22012-11-16 18:28:11 -08001607 void resetEdges() {
1608 for (int i = 0; i < mCountX; i++) {
1609 topEdge[i] = -1;
1610 bottomEdge[i] = -1;
1611 }
1612 for (int i = 0; i < mCountY; i++) {
1613 leftEdge[i] = -1;
1614 rightEdge[i] = -1;
1615 }
1616 leftEdgeDirty = true;
1617 rightEdgeDirty = true;
1618 bottomEdgeDirty = true;
1619 topEdgeDirty = true;
1620 boundingRectDirty = true;
1621 }
Adam Cohen47a876d2012-03-19 13:21:41 -07001622
Adam Cohenf3900c22012-11-16 18:28:11 -08001623 void computeEdge(int which, int[] edge) {
1624 int count = views.size();
1625 for (int i = 0; i < count; i++) {
1626 CellAndSpan cs = config.map.get(views.get(i));
1627 switch (which) {
1628 case LEFT:
1629 int left = cs.x;
1630 for (int j = cs.y; j < cs.y + cs.spanY; j++) {
1631 if (left < edge[j] || edge[j] < 0) {
1632 edge[j] = left;
Adam Cohena56dc102012-07-13 13:41:42 -07001633 }
Adam Cohen47a876d2012-03-19 13:21:41 -07001634 }
Adam Cohenf3900c22012-11-16 18:28:11 -08001635 break;
1636 case RIGHT:
1637 int right = cs.x + cs.spanX;
1638 for (int j = cs.y; j < cs.y + cs.spanY; j++) {
1639 if (right > edge[j]) {
1640 edge[j] = right;
1641 }
1642 }
1643 break;
1644 case TOP:
1645 int top = cs.y;
1646 for (int j = cs.x; j < cs.x + cs.spanX; j++) {
1647 if (top < edge[j] || edge[j] < 0) {
1648 edge[j] = top;
1649 }
1650 }
1651 break;
1652 case BOTTOM:
1653 int bottom = cs.y + cs.spanY;
1654 for (int j = cs.x; j < cs.x + cs.spanX; j++) {
1655 if (bottom > edge[j]) {
1656 edge[j] = bottom;
1657 }
1658 }
1659 break;
Adam Cohen47a876d2012-03-19 13:21:41 -07001660 }
1661 }
1662 }
Adam Cohenf3900c22012-11-16 18:28:11 -08001663
1664 boolean isViewTouchingEdge(View v, int whichEdge) {
1665 CellAndSpan cs = config.map.get(v);
1666
1667 int[] edge = getEdge(whichEdge);
1668
1669 switch (whichEdge) {
1670 case LEFT:
1671 for (int i = cs.y; i < cs.y + cs.spanY; i++) {
1672 if (edge[i] == cs.x + cs.spanX) {
1673 return true;
1674 }
1675 }
1676 break;
1677 case RIGHT:
1678 for (int i = cs.y; i < cs.y + cs.spanY; i++) {
1679 if (edge[i] == cs.x) {
1680 return true;
1681 }
1682 }
1683 break;
1684 case TOP:
1685 for (int i = cs.x; i < cs.x + cs.spanX; i++) {
1686 if (edge[i] == cs.y + cs.spanY) {
1687 return true;
1688 }
1689 }
1690 break;
1691 case BOTTOM:
1692 for (int i = cs.x; i < cs.x + cs.spanX; i++) {
1693 if (edge[i] == cs.y) {
1694 return true;
1695 }
1696 }
1697 break;
1698 }
1699 return false;
1700 }
1701
1702 void shift(int whichEdge, int delta) {
1703 for (View v: views) {
1704 CellAndSpan c = config.map.get(v);
1705 switch (whichEdge) {
1706 case LEFT:
1707 c.x -= delta;
1708 break;
1709 case RIGHT:
1710 c.x += delta;
1711 break;
1712 case TOP:
1713 c.y -= delta;
1714 break;
1715 case BOTTOM:
1716 default:
1717 c.y += delta;
1718 break;
1719 }
1720 }
1721 resetEdges();
1722 }
1723
1724 public void addView(View v) {
1725 views.add(v);
1726 resetEdges();
1727 }
1728
1729 public Rect getBoundingRect() {
1730 if (boundingRectDirty) {
1731 boolean first = true;
1732 for (View v: views) {
1733 CellAndSpan c = config.map.get(v);
1734 if (first) {
1735 boundingRect.set(c.x, c.y, c.x + c.spanX, c.y + c.spanY);
1736 first = false;
1737 } else {
1738 boundingRect.union(c.x, c.y, c.x + c.spanX, c.y + c.spanY);
1739 }
1740 }
1741 }
1742 return boundingRect;
1743 }
1744
1745 public int[] getEdge(int which) {
1746 switch (which) {
1747 case LEFT:
1748 return getLeftEdge();
1749 case RIGHT:
1750 return getRightEdge();
1751 case TOP:
1752 return getTopEdge();
1753 case BOTTOM:
1754 default:
1755 return getBottomEdge();
1756 }
1757 }
1758
1759 public int[] getLeftEdge() {
1760 if (leftEdgeDirty) {
1761 computeEdge(LEFT, leftEdge);
1762 }
1763 return leftEdge;
1764 }
1765
1766 public int[] getRightEdge() {
1767 if (rightEdgeDirty) {
1768 computeEdge(RIGHT, rightEdge);
1769 }
1770 return rightEdge;
1771 }
1772
1773 public int[] getTopEdge() {
1774 if (topEdgeDirty) {
1775 computeEdge(TOP, topEdge);
1776 }
1777 return topEdge;
1778 }
1779
1780 public int[] getBottomEdge() {
1781 if (bottomEdgeDirty) {
1782 computeEdge(BOTTOM, bottomEdge);
1783 }
1784 return bottomEdge;
1785 }
1786
1787 PositionComparator comparator = new PositionComparator();
1788 class PositionComparator implements Comparator<View> {
1789 int whichEdge = 0;
1790 public int compare(View left, View right) {
1791 CellAndSpan l = config.map.get(left);
1792 CellAndSpan r = config.map.get(right);
1793 switch (whichEdge) {
1794 case LEFT:
1795 return (r.x + r.spanX) - (l.x + l.spanX);
1796 case RIGHT:
1797 return l.x - r.x;
1798 case TOP:
1799 return (r.y + r.spanY) - (l.y + l.spanY);
1800 case BOTTOM:
1801 default:
1802 return l.y - r.y;
1803 }
1804 }
1805 }
1806
1807 public void sortConfigurationForEdgePush(int edge) {
1808 comparator.whichEdge = edge;
1809 Collections.sort(config.sortedViews, comparator);
1810 }
Adam Cohen47a876d2012-03-19 13:21:41 -07001811 }
1812
Adam Cohenf3900c22012-11-16 18:28:11 -08001813 private boolean pushViewsToTempLocation(ArrayList<View> views, Rect rectOccupiedByPotentialDrop,
1814 int[] direction, View dragView, ItemConfiguration currentState) {
Adam Cohene0489502012-08-27 15:18:53 -07001815
Adam Cohenf3900c22012-11-16 18:28:11 -08001816 ViewCluster cluster = new ViewCluster(views, currentState);
1817 Rect clusterRect = cluster.getBoundingRect();
1818 int whichEdge;
1819 int pushDistance;
1820 boolean fail = false;
1821
1822 // Determine the edge of the cluster that will be leading the push and how far
1823 // the cluster must be shifted.
1824 if (direction[0] < 0) {
1825 whichEdge = ViewCluster.LEFT;
1826 pushDistance = clusterRect.right - rectOccupiedByPotentialDrop.left;
Adam Cohene0489502012-08-27 15:18:53 -07001827 } else if (direction[0] > 0) {
Adam Cohenf3900c22012-11-16 18:28:11 -08001828 whichEdge = ViewCluster.RIGHT;
1829 pushDistance = rectOccupiedByPotentialDrop.right - clusterRect.left;
1830 } else if (direction[1] < 0) {
1831 whichEdge = ViewCluster.TOP;
1832 pushDistance = clusterRect.bottom - rectOccupiedByPotentialDrop.top;
1833 } else {
1834 whichEdge = ViewCluster.BOTTOM;
1835 pushDistance = rectOccupiedByPotentialDrop.bottom - clusterRect.top;
Adam Cohene0489502012-08-27 15:18:53 -07001836 }
1837
Adam Cohenf3900c22012-11-16 18:28:11 -08001838 // Break early for invalid push distance.
1839 if (pushDistance <= 0) {
1840 return false;
Adam Cohene0489502012-08-27 15:18:53 -07001841 }
Adam Cohenf3900c22012-11-16 18:28:11 -08001842
1843 // Mark the occupied state as false for the group of views we want to move.
1844 for (View v: views) {
1845 CellAndSpan c = currentState.map.get(v);
1846 markCellsForView(c.x, c.y, c.spanX, c.spanY, mTmpOccupied, false);
1847 }
1848
1849 // We save the current configuration -- if we fail to find a solution we will revert
1850 // to the initial state. The process of finding a solution modifies the configuration
1851 // in place, hence the need for revert in the failure case.
1852 currentState.save();
1853
1854 // The pushing algorithm is simplified by considering the views in the order in which
1855 // they would be pushed by the cluster. For example, if the cluster is leading with its
1856 // left edge, we consider sort the views by their right edge, from right to left.
1857 cluster.sortConfigurationForEdgePush(whichEdge);
1858
1859 while (pushDistance > 0 && !fail) {
1860 for (View v: currentState.sortedViews) {
1861 // For each view that isn't in the cluster, we see if the leading edge of the
1862 // cluster is contacting the edge of that view. If so, we add that view to the
1863 // cluster.
1864 if (!cluster.views.contains(v) && v != dragView) {
1865 if (cluster.isViewTouchingEdge(v, whichEdge)) {
1866 LayoutParams lp = (LayoutParams) v.getLayoutParams();
1867 if (!lp.canReorder) {
1868 // The push solution includes the all apps button, this is not viable.
1869 fail = true;
1870 break;
1871 }
1872 cluster.addView(v);
1873 CellAndSpan c = currentState.map.get(v);
1874
1875 // Adding view to cluster, mark it as not occupied.
1876 markCellsForView(c.x, c.y, c.spanX, c.spanY, mTmpOccupied, false);
1877 }
1878 }
1879 }
1880 pushDistance--;
1881
1882 // The cluster has been completed, now we move the whole thing over in the appropriate
1883 // direction.
1884 cluster.shift(whichEdge, 1);
1885 }
1886
1887 boolean foundSolution = false;
1888 clusterRect = cluster.getBoundingRect();
1889
1890 // Due to the nature of the algorithm, the only check required to verify a valid solution
1891 // is to ensure that completed shifted cluster lies completely within the cell layout.
1892 if (!fail && clusterRect.left >= 0 && clusterRect.right <= mCountX && clusterRect.top >= 0 &&
1893 clusterRect.bottom <= mCountY) {
1894 foundSolution = true;
1895 } else {
1896 currentState.restore();
1897 }
1898
1899 // In either case, we set the occupied array as marked for the location of the views
1900 for (View v: cluster.views) {
1901 CellAndSpan c = currentState.map.get(v);
1902 markCellsForView(c.x, c.y, c.spanX, c.spanY, mTmpOccupied, true);
1903 }
1904
1905 return foundSolution;
Adam Cohene0489502012-08-27 15:18:53 -07001906 }
1907
Adam Cohen482ed822012-03-02 14:15:13 -08001908 private boolean addViewsToTempLocation(ArrayList<View> views, Rect rectOccupiedByPotentialDrop,
Adam Cohenf3900c22012-11-16 18:28:11 -08001909 int[] direction, View dragView, ItemConfiguration currentState) {
Adam Cohen482ed822012-03-02 14:15:13 -08001910 if (views.size() == 0) return true;
Adam Cohen482ed822012-03-02 14:15:13 -08001911
Adam Cohen8baab352012-03-20 17:39:21 -07001912 boolean success = false;
Adam Cohen482ed822012-03-02 14:15:13 -08001913 Rect boundingRect = null;
Adam Cohen8baab352012-03-20 17:39:21 -07001914 // We construct a rect which represents the entire group of views passed in
Adam Cohen482ed822012-03-02 14:15:13 -08001915 for (View v: views) {
Adam Cohen8baab352012-03-20 17:39:21 -07001916 CellAndSpan c = currentState.map.get(v);
Adam Cohen482ed822012-03-02 14:15:13 -08001917 if (boundingRect == null) {
Adam Cohen8baab352012-03-20 17:39:21 -07001918 boundingRect = new Rect(c.x, c.y, c.x + c.spanX, c.y + c.spanY);
Adam Cohen482ed822012-03-02 14:15:13 -08001919 } else {
Adam Cohen8baab352012-03-20 17:39:21 -07001920 boundingRect.union(c.x, c.y, c.x + c.spanX, c.y + c.spanY);
Adam Cohen482ed822012-03-02 14:15:13 -08001921 }
1922 }
Adam Cohen8baab352012-03-20 17:39:21 -07001923
Adam Cohen8baab352012-03-20 17:39:21 -07001924 // Mark the occupied state as false for the group of views we want to move.
Adam Cohenf3900c22012-11-16 18:28:11 -08001925 for (View v: views) {
Adam Cohen8baab352012-03-20 17:39:21 -07001926 CellAndSpan c = currentState.map.get(v);
1927 markCellsForView(c.x, c.y, c.spanX, c.spanY, mTmpOccupied, false);
1928 }
1929
Adam Cohen47a876d2012-03-19 13:21:41 -07001930 boolean[][] blockOccupied = new boolean[boundingRect.width()][boundingRect.height()];
1931 int top = boundingRect.top;
1932 int left = boundingRect.left;
Adam Cohen8baab352012-03-20 17:39:21 -07001933 // We mark more precisely which parts of the bounding rect are truly occupied, allowing
Adam Cohena56dc102012-07-13 13:41:42 -07001934 // for interlocking.
Adam Cohenf3900c22012-11-16 18:28:11 -08001935 for (View v: views) {
Adam Cohen8baab352012-03-20 17:39:21 -07001936 CellAndSpan c = currentState.map.get(v);
1937 markCellsForView(c.x - left, c.y - top, c.spanX, c.spanY, blockOccupied, true);
Adam Cohen47a876d2012-03-19 13:21:41 -07001938 }
1939
Adam Cohen482ed822012-03-02 14:15:13 -08001940 markCellsForRect(rectOccupiedByPotentialDrop, mTmpOccupied, true);
1941
Adam Cohenf3900c22012-11-16 18:28:11 -08001942 findNearestArea(boundingRect.left, boundingRect.top, boundingRect.width(),
1943 boundingRect.height(), direction, mTmpOccupied, blockOccupied, mTempLocation);
Adam Cohen482ed822012-03-02 14:15:13 -08001944
Adam Cohen8baab352012-03-20 17:39:21 -07001945 // If we successfuly found a location by pushing the block of views, we commit it
Adam Cohen482ed822012-03-02 14:15:13 -08001946 if (mTempLocation[0] >= 0 && mTempLocation[1] >= 0) {
Adam Cohen8baab352012-03-20 17:39:21 -07001947 int deltaX = mTempLocation[0] - boundingRect.left;
1948 int deltaY = mTempLocation[1] - boundingRect.top;
Adam Cohenf3900c22012-11-16 18:28:11 -08001949 for (View v: views) {
Adam Cohen8baab352012-03-20 17:39:21 -07001950 CellAndSpan c = currentState.map.get(v);
1951 c.x += deltaX;
1952 c.y += deltaY;
Adam Cohen482ed822012-03-02 14:15:13 -08001953 }
1954 success = true;
1955 }
Adam Cohen8baab352012-03-20 17:39:21 -07001956
1957 // In either case, we set the occupied array as marked for the location of the views
Adam Cohenf3900c22012-11-16 18:28:11 -08001958 for (View v: views) {
Adam Cohen8baab352012-03-20 17:39:21 -07001959 CellAndSpan c = currentState.map.get(v);
1960 markCellsForView(c.x, c.y, c.spanX, c.spanY, mTmpOccupied, true);
Adam Cohen482ed822012-03-02 14:15:13 -08001961 }
1962 return success;
1963 }
1964
1965 private void markCellsForRect(Rect r, boolean[][] occupied, boolean value) {
1966 markCellsForView(r.left, r.top, r.width(), r.height(), occupied, value);
1967 }
1968
Adam Cohen4abc5bd2012-05-29 21:06:03 -07001969 // This method tries to find a reordering solution which satisfies the push mechanic by trying
1970 // to push items in each of the cardinal directions, in an order based on the direction vector
1971 // passed.
1972 private boolean attemptPushInDirection(ArrayList<View> intersectingViews, Rect occupied,
1973 int[] direction, View ignoreView, ItemConfiguration solution) {
1974 if ((Math.abs(direction[0]) + Math.abs(direction[1])) > 1) {
1975 // If the direction vector has two non-zero components, we try pushing
1976 // separately in each of the components.
1977 int temp = direction[1];
1978 direction[1] = 0;
Adam Cohenf3900c22012-11-16 18:28:11 -08001979
1980 if (pushViewsToTempLocation(intersectingViews, occupied, direction,
Adam Cohen4abc5bd2012-05-29 21:06:03 -07001981 ignoreView, solution)) {
1982 return true;
1983 }
1984 direction[1] = temp;
1985 temp = direction[0];
1986 direction[0] = 0;
Adam Cohenf3900c22012-11-16 18:28:11 -08001987
1988 if (pushViewsToTempLocation(intersectingViews, occupied, direction,
Adam Cohen4abc5bd2012-05-29 21:06:03 -07001989 ignoreView, solution)) {
1990 return true;
1991 }
1992 // Revert the direction
1993 direction[0] = temp;
1994
1995 // Now we try pushing in each component of the opposite direction
1996 direction[0] *= -1;
1997 direction[1] *= -1;
1998 temp = direction[1];
1999 direction[1] = 0;
Adam Cohenf3900c22012-11-16 18:28:11 -08002000 if (pushViewsToTempLocation(intersectingViews, occupied, direction,
Adam Cohen4abc5bd2012-05-29 21:06:03 -07002001 ignoreView, solution)) {
2002 return true;
2003 }
2004
2005 direction[1] = temp;
2006 temp = direction[0];
2007 direction[0] = 0;
Adam Cohenf3900c22012-11-16 18:28:11 -08002008 if (pushViewsToTempLocation(intersectingViews, occupied, direction,
Adam Cohen4abc5bd2012-05-29 21:06:03 -07002009 ignoreView, solution)) {
2010 return true;
2011 }
2012 // revert the direction
2013 direction[0] = temp;
2014 direction[0] *= -1;
2015 direction[1] *= -1;
2016
2017 } else {
2018 // If the direction vector has a single non-zero component, we push first in the
2019 // direction of the vector
Adam Cohenf3900c22012-11-16 18:28:11 -08002020 if (pushViewsToTempLocation(intersectingViews, occupied, direction,
Adam Cohen4abc5bd2012-05-29 21:06:03 -07002021 ignoreView, solution)) {
2022 return true;
2023 }
Adam Cohen4abc5bd2012-05-29 21:06:03 -07002024 // Then we try the opposite direction
2025 direction[0] *= -1;
2026 direction[1] *= -1;
Adam Cohenf3900c22012-11-16 18:28:11 -08002027 if (pushViewsToTempLocation(intersectingViews, occupied, direction,
Adam Cohen4abc5bd2012-05-29 21:06:03 -07002028 ignoreView, solution)) {
2029 return true;
2030 }
2031 // Switch the direction back
2032 direction[0] *= -1;
2033 direction[1] *= -1;
2034
2035 // If we have failed to find a push solution with the above, then we try
2036 // to find a solution by pushing along the perpendicular axis.
2037
2038 // Swap the components
2039 int temp = direction[1];
2040 direction[1] = direction[0];
2041 direction[0] = temp;
Adam Cohenf3900c22012-11-16 18:28:11 -08002042 if (pushViewsToTempLocation(intersectingViews, occupied, direction,
Adam Cohen4abc5bd2012-05-29 21:06:03 -07002043 ignoreView, solution)) {
2044 return true;
2045 }
2046
2047 // Then we try the opposite direction
2048 direction[0] *= -1;
2049 direction[1] *= -1;
Adam Cohenf3900c22012-11-16 18:28:11 -08002050 if (pushViewsToTempLocation(intersectingViews, occupied, direction,
Adam Cohen4abc5bd2012-05-29 21:06:03 -07002051 ignoreView, solution)) {
2052 return true;
2053 }
2054 // Switch the direction back
2055 direction[0] *= -1;
2056 direction[1] *= -1;
2057
2058 // Swap the components back
2059 temp = direction[1];
2060 direction[1] = direction[0];
2061 direction[0] = temp;
2062 }
2063 return false;
2064 }
2065
Adam Cohen482ed822012-03-02 14:15:13 -08002066 private boolean rearrangementExists(int cellX, int cellY, int spanX, int spanY, int[] direction,
Adam Cohen8baab352012-03-20 17:39:21 -07002067 View ignoreView, ItemConfiguration solution) {
Winson Chunge3e03bc2012-05-01 15:10:11 -07002068 // Return early if get invalid cell positions
2069 if (cellX < 0 || cellY < 0) return false;
Adam Cohen482ed822012-03-02 14:15:13 -08002070
Adam Cohen8baab352012-03-20 17:39:21 -07002071 mIntersectingViews.clear();
Adam Cohen482ed822012-03-02 14:15:13 -08002072 mOccupiedRect.set(cellX, cellY, cellX + spanX, cellY + spanY);
Adam Cohen482ed822012-03-02 14:15:13 -08002073
Adam Cohen8baab352012-03-20 17:39:21 -07002074 // Mark the desired location of the view currently being dragged.
Adam Cohen482ed822012-03-02 14:15:13 -08002075 if (ignoreView != null) {
Adam Cohen8baab352012-03-20 17:39:21 -07002076 CellAndSpan c = solution.map.get(ignoreView);
Adam Cohen19f37922012-03-21 11:59:11 -07002077 if (c != null) {
2078 c.x = cellX;
2079 c.y = cellY;
2080 }
Adam Cohen482ed822012-03-02 14:15:13 -08002081 }
Adam Cohen482ed822012-03-02 14:15:13 -08002082 Rect r0 = new Rect(cellX, cellY, cellX + spanX, cellY + spanY);
2083 Rect r1 = new Rect();
Adam Cohen8baab352012-03-20 17:39:21 -07002084 for (View child: solution.map.keySet()) {
Adam Cohen482ed822012-03-02 14:15:13 -08002085 if (child == ignoreView) continue;
Adam Cohen8baab352012-03-20 17:39:21 -07002086 CellAndSpan c = solution.map.get(child);
Adam Cohen482ed822012-03-02 14:15:13 -08002087 LayoutParams lp = (LayoutParams) child.getLayoutParams();
Adam Cohen8baab352012-03-20 17:39:21 -07002088 r1.set(c.x, c.y, c.x + c.spanX, c.y + c.spanY);
Adam Cohen482ed822012-03-02 14:15:13 -08002089 if (Rect.intersects(r0, r1)) {
2090 if (!lp.canReorder) {
2091 return false;
2092 }
2093 mIntersectingViews.add(child);
2094 }
2095 }
Adam Cohen47a876d2012-03-19 13:21:41 -07002096
Adam Cohen4abc5bd2012-05-29 21:06:03 -07002097 // First we try to find a solution which respects the push mechanic. That is,
2098 // we try to find a solution such that no displaced item travels through another item
2099 // without also displacing that item.
2100 if (attemptPushInDirection(mIntersectingViews, mOccupiedRect, direction, ignoreView,
Adam Cohen19f37922012-03-21 11:59:11 -07002101 solution)) {
Adam Cohen47a876d2012-03-19 13:21:41 -07002102 return true;
2103 }
Adam Cohen47a876d2012-03-19 13:21:41 -07002104
Adam Cohen4abc5bd2012-05-29 21:06:03 -07002105 // Next we try moving the views as a block, but without requiring the push mechanic.
Adam Cohenf3900c22012-11-16 18:28:11 -08002106 if (addViewsToTempLocation(mIntersectingViews, mOccupiedRect, direction, ignoreView,
Adam Cohen19f37922012-03-21 11:59:11 -07002107 solution)) {
Adam Cohen482ed822012-03-02 14:15:13 -08002108 return true;
2109 }
Adam Cohen47a876d2012-03-19 13:21:41 -07002110
Adam Cohen482ed822012-03-02 14:15:13 -08002111 // Ok, they couldn't move as a block, let's move them individually
2112 for (View v : mIntersectingViews) {
Adam Cohen8baab352012-03-20 17:39:21 -07002113 if (!addViewToTempLocation(v, mOccupiedRect, direction, solution)) {
Adam Cohen482ed822012-03-02 14:15:13 -08002114 return false;
2115 }
2116 }
2117 return true;
2118 }
2119
2120 /*
2121 * Returns a pair (x, y), where x,y are in {-1, 0, 1} corresponding to vector between
2122 * the provided point and the provided cell
2123 */
Adam Cohen47a876d2012-03-19 13:21:41 -07002124 private void computeDirectionVector(float deltaX, float deltaY, int[] result) {
Adam Cohen482ed822012-03-02 14:15:13 -08002125 double angle = Math.atan(((float) deltaY) / deltaX);
2126
2127 result[0] = 0;
2128 result[1] = 0;
2129 if (Math.abs(Math.cos(angle)) > 0.5f) {
2130 result[0] = (int) Math.signum(deltaX);
2131 }
2132 if (Math.abs(Math.sin(angle)) > 0.5f) {
2133 result[1] = (int) Math.signum(deltaY);
2134 }
2135 }
2136
Adam Cohen8baab352012-03-20 17:39:21 -07002137 private void copyOccupiedArray(boolean[][] occupied) {
2138 for (int i = 0; i < mCountX; i++) {
2139 for (int j = 0; j < mCountY; j++) {
2140 occupied[i][j] = mOccupied[i][j];
2141 }
2142 }
2143 }
2144
Adam Cohen482ed822012-03-02 14:15:13 -08002145 ItemConfiguration simpleSwap(int pixelX, int pixelY, int minSpanX, int minSpanY, int spanX,
2146 int spanY, int[] direction, View dragView, boolean decX, ItemConfiguration solution) {
Adam Cohen8baab352012-03-20 17:39:21 -07002147 // Copy the current state into the solution. This solution will be manipulated as necessary.
2148 copyCurrentStateToSolution(solution, false);
2149 // Copy the current occupied array into the temporary occupied array. This array will be
2150 // manipulated as necessary to find a solution.
2151 copyOccupiedArray(mTmpOccupied);
Adam Cohen482ed822012-03-02 14:15:13 -08002152
2153 // We find the nearest cell into which we would place the dragged item, assuming there's
2154 // nothing in its way.
2155 int result[] = new int[2];
2156 result = findNearestArea(pixelX, pixelY, spanX, spanY, result);
2157
2158 boolean success = false;
2159 // First we try the exact nearest position of the item being dragged,
2160 // we will then want to try to move this around to other neighbouring positions
Adam Cohen8baab352012-03-20 17:39:21 -07002161 success = rearrangementExists(result[0], result[1], spanX, spanY, direction, dragView,
2162 solution);
Adam Cohen482ed822012-03-02 14:15:13 -08002163
2164 if (!success) {
2165 // We try shrinking the widget down to size in an alternating pattern, shrink 1 in
2166 // x, then 1 in y etc.
2167 if (spanX > minSpanX && (minSpanY == spanY || decX)) {
2168 return simpleSwap(pixelX, pixelY, minSpanX, minSpanY, spanX - 1, spanY, direction,
2169 dragView, false, solution);
2170 } else if (spanY > minSpanY) {
2171 return simpleSwap(pixelX, pixelY, minSpanX, minSpanY, spanX, spanY - 1, direction,
2172 dragView, true, solution);
2173 }
2174 solution.isSolution = false;
2175 } else {
2176 solution.isSolution = true;
2177 solution.dragViewX = result[0];
2178 solution.dragViewY = result[1];
2179 solution.dragViewSpanX = spanX;
2180 solution.dragViewSpanY = spanY;
Adam Cohen482ed822012-03-02 14:15:13 -08002181 }
2182 return solution;
2183 }
2184
2185 private void copyCurrentStateToSolution(ItemConfiguration solution, boolean temp) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07002186 int childCount = mShortcutsAndWidgets.getChildCount();
Adam Cohen482ed822012-03-02 14:15:13 -08002187 for (int i = 0; i < childCount; i++) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07002188 View child = mShortcutsAndWidgets.getChildAt(i);
Adam Cohen482ed822012-03-02 14:15:13 -08002189 LayoutParams lp = (LayoutParams) child.getLayoutParams();
Adam Cohen8baab352012-03-20 17:39:21 -07002190 CellAndSpan c;
Adam Cohen482ed822012-03-02 14:15:13 -08002191 if (temp) {
Adam Cohen8baab352012-03-20 17:39:21 -07002192 c = new CellAndSpan(lp.tmpCellX, lp.tmpCellY, lp.cellHSpan, lp.cellVSpan);
Adam Cohen482ed822012-03-02 14:15:13 -08002193 } else {
Adam Cohen8baab352012-03-20 17:39:21 -07002194 c = new CellAndSpan(lp.cellX, lp.cellY, lp.cellHSpan, lp.cellVSpan);
Adam Cohen482ed822012-03-02 14:15:13 -08002195 }
Adam Cohenf3900c22012-11-16 18:28:11 -08002196 solution.add(child, c);
Adam Cohen482ed822012-03-02 14:15:13 -08002197 }
2198 }
2199
2200 private void copySolutionToTempState(ItemConfiguration solution, View dragView) {
2201 for (int i = 0; i < mCountX; i++) {
2202 for (int j = 0; j < mCountY; j++) {
2203 mTmpOccupied[i][j] = false;
2204 }
2205 }
2206
Michael Jurkaa52570f2012-03-20 03:18:20 -07002207 int childCount = mShortcutsAndWidgets.getChildCount();
Adam Cohen482ed822012-03-02 14:15:13 -08002208 for (int i = 0; i < childCount; i++) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07002209 View child = mShortcutsAndWidgets.getChildAt(i);
Adam Cohen482ed822012-03-02 14:15:13 -08002210 if (child == dragView) continue;
2211 LayoutParams lp = (LayoutParams) child.getLayoutParams();
Adam Cohen8baab352012-03-20 17:39:21 -07002212 CellAndSpan c = solution.map.get(child);
2213 if (c != null) {
2214 lp.tmpCellX = c.x;
2215 lp.tmpCellY = c.y;
2216 lp.cellHSpan = c.spanX;
2217 lp.cellVSpan = c.spanY;
2218 markCellsForView(c.x, c.y, c.spanX, c.spanY, mTmpOccupied, true);
Adam Cohen482ed822012-03-02 14:15:13 -08002219 }
2220 }
2221 markCellsForView(solution.dragViewX, solution.dragViewY, solution.dragViewSpanX,
2222 solution.dragViewSpanY, mTmpOccupied, true);
2223 }
2224
2225 private void animateItemsToSolution(ItemConfiguration solution, View dragView, boolean
2226 commitDragView) {
2227
2228 boolean[][] occupied = DESTRUCTIVE_REORDER ? mOccupied : mTmpOccupied;
2229 for (int i = 0; i < mCountX; i++) {
2230 for (int j = 0; j < mCountY; j++) {
2231 occupied[i][j] = false;
2232 }
2233 }
2234
Michael Jurkaa52570f2012-03-20 03:18:20 -07002235 int childCount = mShortcutsAndWidgets.getChildCount();
Adam Cohen482ed822012-03-02 14:15:13 -08002236 for (int i = 0; i < childCount; i++) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07002237 View child = mShortcutsAndWidgets.getChildAt(i);
Adam Cohen482ed822012-03-02 14:15:13 -08002238 if (child == dragView) continue;
Adam Cohen8baab352012-03-20 17:39:21 -07002239 CellAndSpan c = solution.map.get(child);
2240 if (c != null) {
Adam Cohen19f37922012-03-21 11:59:11 -07002241 animateChildToPosition(child, c.x, c.y, REORDER_ANIMATION_DURATION, 0,
2242 DESTRUCTIVE_REORDER, false);
Adam Cohen8baab352012-03-20 17:39:21 -07002243 markCellsForView(c.x, c.y, c.spanX, c.spanY, occupied, true);
Adam Cohen482ed822012-03-02 14:15:13 -08002244 }
2245 }
2246 if (commitDragView) {
2247 markCellsForView(solution.dragViewX, solution.dragViewY, solution.dragViewSpanX,
2248 solution.dragViewSpanY, occupied, true);
2249 }
2250 }
2251
Adam Cohen19f37922012-03-21 11:59:11 -07002252 // This method starts or changes the reorder hint animations
2253 private void beginOrAdjustHintAnimations(ItemConfiguration solution, View dragView, int delay) {
2254 int childCount = mShortcutsAndWidgets.getChildCount();
Adam Cohen19f37922012-03-21 11:59:11 -07002255 for (int i = 0; i < childCount; i++) {
2256 View child = mShortcutsAndWidgets.getChildAt(i);
2257 if (child == dragView) continue;
2258 CellAndSpan c = solution.map.get(child);
2259 LayoutParams lp = (LayoutParams) child.getLayoutParams();
2260 if (c != null) {
2261 ReorderHintAnimation rha = new ReorderHintAnimation(child, lp.cellX, lp.cellY,
2262 c.x, c.y, c.spanX, c.spanY);
Adam Cohend024f982012-05-23 18:26:45 -07002263 rha.animate();
Adam Cohen19f37922012-03-21 11:59:11 -07002264 }
2265 }
2266 }
2267
2268 // Class which represents the reorder hint animations. These animations show that an item is
2269 // in a temporary state, and hint at where the item will return to.
2270 class ReorderHintAnimation {
2271 View child;
Adam Cohend024f982012-05-23 18:26:45 -07002272 float finalDeltaX;
2273 float finalDeltaY;
2274 float initDeltaX;
2275 float initDeltaY;
2276 float finalScale;
2277 float initScale;
Brandon Keely50e6e562012-05-08 16:28:49 -07002278 private static final int DURATION = 300;
Adam Cohene7587d22012-05-24 18:50:02 -07002279 Animator a;
Adam Cohen19f37922012-03-21 11:59:11 -07002280
2281 public ReorderHintAnimation(View child, int cellX0, int cellY0, int cellX1, int cellY1,
2282 int spanX, int spanY) {
2283 regionToCenterPoint(cellX0, cellY0, spanX, spanY, mTmpPoint);
2284 final int x0 = mTmpPoint[0];
2285 final int y0 = mTmpPoint[1];
2286 regionToCenterPoint(cellX1, cellY1, spanX, spanY, mTmpPoint);
2287 final int x1 = mTmpPoint[0];
2288 final int y1 = mTmpPoint[1];
2289 final int dX = x1 - x0;
2290 final int dY = y1 - y0;
Adam Cohend024f982012-05-23 18:26:45 -07002291 finalDeltaX = 0;
2292 finalDeltaY = 0;
Adam Cohen19f37922012-03-21 11:59:11 -07002293 if (dX == dY && dX == 0) {
2294 } else {
2295 if (dY == 0) {
Adam Cohend024f982012-05-23 18:26:45 -07002296 finalDeltaX = - Math.signum(dX) * mReorderHintAnimationMagnitude;
Adam Cohen19f37922012-03-21 11:59:11 -07002297 } else if (dX == 0) {
Adam Cohend024f982012-05-23 18:26:45 -07002298 finalDeltaY = - Math.signum(dY) * mReorderHintAnimationMagnitude;
Adam Cohen19f37922012-03-21 11:59:11 -07002299 } else {
2300 double angle = Math.atan( (float) (dY) / dX);
Adam Cohend024f982012-05-23 18:26:45 -07002301 finalDeltaX = (int) (- Math.signum(dX) *
Adam Cohenfe41ac62012-05-23 14:00:37 -07002302 Math.abs(Math.cos(angle) * mReorderHintAnimationMagnitude));
Adam Cohend024f982012-05-23 18:26:45 -07002303 finalDeltaY = (int) (- Math.signum(dY) *
Adam Cohenfe41ac62012-05-23 14:00:37 -07002304 Math.abs(Math.sin(angle) * mReorderHintAnimationMagnitude));
Adam Cohen19f37922012-03-21 11:59:11 -07002305 }
2306 }
Adam Cohend024f982012-05-23 18:26:45 -07002307 initDeltaX = child.getTranslationX();
2308 initDeltaY = child.getTranslationY();
Adam Cohen307fe232012-08-16 17:55:58 -07002309 finalScale = getChildrenScale() - 4.0f / child.getWidth();
Adam Cohend024f982012-05-23 18:26:45 -07002310 initScale = child.getScaleX();
Adam Cohen19f37922012-03-21 11:59:11 -07002311 this.child = child;
2312 }
2313
Adam Cohend024f982012-05-23 18:26:45 -07002314 void animate() {
Adam Cohen19f37922012-03-21 11:59:11 -07002315 if (mShakeAnimators.containsKey(child)) {
2316 ReorderHintAnimation oldAnimation = mShakeAnimators.get(child);
Adam Cohend024f982012-05-23 18:26:45 -07002317 oldAnimation.cancel();
Adam Cohen19f37922012-03-21 11:59:11 -07002318 mShakeAnimators.remove(child);
Adam Cohene7587d22012-05-24 18:50:02 -07002319 if (finalDeltaX == 0 && finalDeltaY == 0) {
2320 completeAnimationImmediately();
2321 return;
2322 }
Adam Cohen19f37922012-03-21 11:59:11 -07002323 }
Adam Cohend024f982012-05-23 18:26:45 -07002324 if (finalDeltaX == 0 && finalDeltaY == 0) {
Adam Cohen19f37922012-03-21 11:59:11 -07002325 return;
2326 }
Michael Jurka2ecf9952012-06-18 12:52:28 -07002327 ValueAnimator va = LauncherAnimUtils.ofFloat(0f, 1f);
Adam Cohene7587d22012-05-24 18:50:02 -07002328 a = va;
Adam Cohen19f37922012-03-21 11:59:11 -07002329 va.setRepeatMode(ValueAnimator.REVERSE);
2330 va.setRepeatCount(ValueAnimator.INFINITE);
Adam Cohen7bdfc972012-05-22 16:50:35 -07002331 va.setDuration(DURATION);
Adam Cohend024f982012-05-23 18:26:45 -07002332 va.setStartDelay((int) (Math.random() * 60));
Adam Cohen19f37922012-03-21 11:59:11 -07002333 va.addUpdateListener(new AnimatorUpdateListener() {
2334 @Override
2335 public void onAnimationUpdate(ValueAnimator animation) {
2336 float r = ((Float) animation.getAnimatedValue()).floatValue();
Adam Cohend024f982012-05-23 18:26:45 -07002337 float x = r * finalDeltaX + (1 - r) * initDeltaX;
2338 float y = r * finalDeltaY + (1 - r) * initDeltaY;
Adam Cohen19f37922012-03-21 11:59:11 -07002339 child.setTranslationX(x);
2340 child.setTranslationY(y);
Adam Cohend024f982012-05-23 18:26:45 -07002341 float s = r * finalScale + (1 - r) * initScale;
Brandon Keely50e6e562012-05-08 16:28:49 -07002342 child.setScaleX(s);
2343 child.setScaleY(s);
Adam Cohen19f37922012-03-21 11:59:11 -07002344 }
2345 });
2346 va.addListener(new AnimatorListenerAdapter() {
2347 public void onAnimationRepeat(Animator animation) {
Adam Cohen19f37922012-03-21 11:59:11 -07002348 // We make sure to end only after a full period
Adam Cohend024f982012-05-23 18:26:45 -07002349 initDeltaX = 0;
2350 initDeltaY = 0;
Adam Cohen307fe232012-08-16 17:55:58 -07002351 initScale = getChildrenScale();
Adam Cohen19f37922012-03-21 11:59:11 -07002352 }
2353 });
Adam Cohen19f37922012-03-21 11:59:11 -07002354 mShakeAnimators.put(child, this);
2355 va.start();
2356 }
2357
Adam Cohend024f982012-05-23 18:26:45 -07002358 private void cancel() {
Adam Cohene7587d22012-05-24 18:50:02 -07002359 if (a != null) {
2360 a.cancel();
2361 }
Adam Cohen19f37922012-03-21 11:59:11 -07002362 }
Adam Cohene7587d22012-05-24 18:50:02 -07002363
Brandon Keely50e6e562012-05-08 16:28:49 -07002364 private void completeAnimationImmediately() {
Adam Cohene7587d22012-05-24 18:50:02 -07002365 if (a != null) {
2366 a.cancel();
2367 }
Brandon Keely50e6e562012-05-08 16:28:49 -07002368
Michael Jurka2ecf9952012-06-18 12:52:28 -07002369 AnimatorSet s = LauncherAnimUtils.createAnimatorSet();
Adam Cohene7587d22012-05-24 18:50:02 -07002370 a = s;
Brandon Keely50e6e562012-05-08 16:28:49 -07002371 s.playTogether(
Adam Cohen307fe232012-08-16 17:55:58 -07002372 LauncherAnimUtils.ofFloat(child, "scaleX", getChildrenScale()),
2373 LauncherAnimUtils.ofFloat(child, "scaleY", getChildrenScale()),
Michael Jurka2ecf9952012-06-18 12:52:28 -07002374 LauncherAnimUtils.ofFloat(child, "translationX", 0f),
2375 LauncherAnimUtils.ofFloat(child, "translationY", 0f)
Brandon Keely50e6e562012-05-08 16:28:49 -07002376 );
2377 s.setDuration(REORDER_ANIMATION_DURATION);
2378 s.setInterpolator(new android.view.animation.DecelerateInterpolator(1.5f));
2379 s.start();
2380 }
Adam Cohen19f37922012-03-21 11:59:11 -07002381 }
2382
2383 private void completeAndClearReorderHintAnimations() {
2384 for (ReorderHintAnimation a: mShakeAnimators.values()) {
Brandon Keely50e6e562012-05-08 16:28:49 -07002385 a.completeAnimationImmediately();
Adam Cohen19f37922012-03-21 11:59:11 -07002386 }
2387 mShakeAnimators.clear();
2388 }
2389
Adam Cohen482ed822012-03-02 14:15:13 -08002390 private void commitTempPlacement() {
2391 for (int i = 0; i < mCountX; i++) {
2392 for (int j = 0; j < mCountY; j++) {
2393 mOccupied[i][j] = mTmpOccupied[i][j];
2394 }
2395 }
Michael Jurkaa52570f2012-03-20 03:18:20 -07002396 int childCount = mShortcutsAndWidgets.getChildCount();
Adam Cohen482ed822012-03-02 14:15:13 -08002397 for (int i = 0; i < childCount; i++) {
Adam Cohenea889a22012-03-27 16:45:39 -07002398 View child = mShortcutsAndWidgets.getChildAt(i);
2399 LayoutParams lp = (LayoutParams) child.getLayoutParams();
2400 ItemInfo info = (ItemInfo) child.getTag();
Adam Cohen2acce882012-03-28 19:03:19 -07002401 // We do a null check here because the item info can be null in the case of the
2402 // AllApps button in the hotseat.
2403 if (info != null) {
Adam Cohen487f7dd2012-06-28 18:12:10 -07002404 if (info.cellX != lp.tmpCellX || info.cellY != lp.tmpCellY ||
2405 info.spanX != lp.cellHSpan || info.spanY != lp.cellVSpan) {
2406 info.requiresDbUpdate = true;
2407 }
Adam Cohen2acce882012-03-28 19:03:19 -07002408 info.cellX = lp.cellX = lp.tmpCellX;
2409 info.cellY = lp.cellY = lp.tmpCellY;
Adam Cohenbebf0422012-04-11 18:06:28 -07002410 info.spanX = lp.cellHSpan;
2411 info.spanY = lp.cellVSpan;
Adam Cohen2acce882012-03-28 19:03:19 -07002412 }
Adam Cohen482ed822012-03-02 14:15:13 -08002413 }
Adam Cohen2acce882012-03-28 19:03:19 -07002414 mLauncher.getWorkspace().updateItemLocationsInDatabase(this);
Adam Cohen482ed822012-03-02 14:15:13 -08002415 }
2416
2417 public void setUseTempCoords(boolean useTempCoords) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07002418 int childCount = mShortcutsAndWidgets.getChildCount();
Adam Cohen482ed822012-03-02 14:15:13 -08002419 for (int i = 0; i < childCount; i++) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07002420 LayoutParams lp = (LayoutParams) mShortcutsAndWidgets.getChildAt(i).getLayoutParams();
Adam Cohen482ed822012-03-02 14:15:13 -08002421 lp.useTmpCoords = useTempCoords;
2422 }
2423 }
2424
Adam Cohen482ed822012-03-02 14:15:13 -08002425 ItemConfiguration findConfigurationNoShuffle(int pixelX, int pixelY, int minSpanX, int minSpanY,
2426 int spanX, int spanY, View dragView, ItemConfiguration solution) {
2427 int[] result = new int[2];
2428 int[] resultSpan = new int[2];
2429 findNearestVacantArea(pixelX, pixelY, minSpanX, minSpanY, spanX, spanY, null, result,
2430 resultSpan);
2431 if (result[0] >= 0 && result[1] >= 0) {
2432 copyCurrentStateToSolution(solution, false);
2433 solution.dragViewX = result[0];
2434 solution.dragViewY = result[1];
2435 solution.dragViewSpanX = resultSpan[0];
2436 solution.dragViewSpanY = resultSpan[1];
2437 solution.isSolution = true;
2438 } else {
2439 solution.isSolution = false;
2440 }
2441 return solution;
2442 }
2443
2444 public void prepareChildForDrag(View child) {
2445 markCellsAsUnoccupiedForView(child);
Adam Cohen482ed822012-03-02 14:15:13 -08002446 }
2447
Adam Cohen19f37922012-03-21 11:59:11 -07002448 /* This seems like it should be obvious and straight-forward, but when the direction vector
2449 needs to match with the notion of the dragView pushing other views, we have to employ
2450 a slightly more subtle notion of the direction vector. The question is what two points is
2451 the vector between? The center of the dragView and its desired destination? Not quite, as
2452 this doesn't necessarily coincide with the interaction of the dragView and items occupying
2453 those cells. Instead we use some heuristics to often lock the vector to up, down, left
2454 or right, which helps make pushing feel right.
2455 */
2456 private void getDirectionVectorForDrop(int dragViewCenterX, int dragViewCenterY, int spanX,
2457 int spanY, View dragView, int[] resultDirection) {
2458 int[] targetDestination = new int[2];
2459
2460 findNearestArea(dragViewCenterX, dragViewCenterY, spanX, spanY, targetDestination);
2461 Rect dragRect = new Rect();
2462 regionToRect(targetDestination[0], targetDestination[1], spanX, spanY, dragRect);
2463 dragRect.offset(dragViewCenterX - dragRect.centerX(), dragViewCenterY - dragRect.centerY());
2464
2465 Rect dropRegionRect = new Rect();
2466 getViewsIntersectingRegion(targetDestination[0], targetDestination[1], spanX, spanY,
2467 dragView, dropRegionRect, mIntersectingViews);
2468
2469 int dropRegionSpanX = dropRegionRect.width();
2470 int dropRegionSpanY = dropRegionRect.height();
2471
2472 regionToRect(dropRegionRect.left, dropRegionRect.top, dropRegionRect.width(),
2473 dropRegionRect.height(), dropRegionRect);
2474
2475 int deltaX = (dropRegionRect.centerX() - dragViewCenterX) / spanX;
2476 int deltaY = (dropRegionRect.centerY() - dragViewCenterY) / spanY;
2477
2478 if (dropRegionSpanX == mCountX || spanX == mCountX) {
2479 deltaX = 0;
2480 }
2481 if (dropRegionSpanY == mCountY || spanY == mCountY) {
2482 deltaY = 0;
2483 }
2484
2485 if (deltaX == 0 && deltaY == 0) {
2486 // No idea what to do, give a random direction.
2487 resultDirection[0] = 1;
2488 resultDirection[1] = 0;
2489 } else {
2490 computeDirectionVector(deltaX, deltaY, resultDirection);
2491 }
2492 }
2493
2494 // For a given cell and span, fetch the set of views intersecting the region.
2495 private void getViewsIntersectingRegion(int cellX, int cellY, int spanX, int spanY,
2496 View dragView, Rect boundingRect, ArrayList<View> intersectingViews) {
2497 if (boundingRect != null) {
2498 boundingRect.set(cellX, cellY, cellX + spanX, cellY + spanY);
2499 }
2500 intersectingViews.clear();
2501 Rect r0 = new Rect(cellX, cellY, cellX + spanX, cellY + spanY);
2502 Rect r1 = new Rect();
2503 final int count = mShortcutsAndWidgets.getChildCount();
2504 for (int i = 0; i < count; i++) {
2505 View child = mShortcutsAndWidgets.getChildAt(i);
2506 if (child == dragView) continue;
2507 LayoutParams lp = (LayoutParams) child.getLayoutParams();
2508 r1.set(lp.cellX, lp.cellY, lp.cellX + lp.cellHSpan, lp.cellY + lp.cellVSpan);
2509 if (Rect.intersects(r0, r1)) {
2510 mIntersectingViews.add(child);
2511 if (boundingRect != null) {
2512 boundingRect.union(r1);
2513 }
2514 }
2515 }
2516 }
2517
2518 boolean isNearestDropLocationOccupied(int pixelX, int pixelY, int spanX, int spanY,
2519 View dragView, int[] result) {
2520 result = findNearestArea(pixelX, pixelY, spanX, spanY, result);
2521 getViewsIntersectingRegion(result[0], result[1], spanX, spanY, dragView, null,
2522 mIntersectingViews);
2523 return !mIntersectingViews.isEmpty();
2524 }
2525
2526 void revertTempState() {
2527 if (!isItemPlacementDirty() || DESTRUCTIVE_REORDER) return;
2528 final int count = mShortcutsAndWidgets.getChildCount();
2529 for (int i = 0; i < count; i++) {
2530 View child = mShortcutsAndWidgets.getChildAt(i);
2531 LayoutParams lp = (LayoutParams) child.getLayoutParams();
2532 if (lp.tmpCellX != lp.cellX || lp.tmpCellY != lp.cellY) {
2533 lp.tmpCellX = lp.cellX;
2534 lp.tmpCellY = lp.cellY;
2535 animateChildToPosition(child, lp.cellX, lp.cellY, REORDER_ANIMATION_DURATION,
2536 0, false, false);
2537 }
2538 }
2539 completeAndClearReorderHintAnimations();
2540 setItemPlacementDirty(false);
2541 }
2542
Adam Cohenbebf0422012-04-11 18:06:28 -07002543 boolean createAreaForResize(int cellX, int cellY, int spanX, int spanY,
2544 View dragView, int[] direction, boolean commit) {
2545 int[] pixelXY = new int[2];
2546 regionToCenterPoint(cellX, cellY, spanX, spanY, pixelXY);
2547
2548 // First we determine if things have moved enough to cause a different layout
2549 ItemConfiguration swapSolution = simpleSwap(pixelXY[0], pixelXY[1], spanX, spanY,
2550 spanX, spanY, direction, dragView, true, new ItemConfiguration());
2551
2552 setUseTempCoords(true);
2553 if (swapSolution != null && swapSolution.isSolution) {
2554 // If we're just testing for a possible location (MODE_ACCEPT_DROP), we don't bother
2555 // committing anything or animating anything as we just want to determine if a solution
2556 // exists
2557 copySolutionToTempState(swapSolution, dragView);
2558 setItemPlacementDirty(true);
2559 animateItemsToSolution(swapSolution, dragView, commit);
2560
2561 if (commit) {
2562 commitTempPlacement();
2563 completeAndClearReorderHintAnimations();
2564 setItemPlacementDirty(false);
2565 } else {
2566 beginOrAdjustHintAnimations(swapSolution, dragView,
2567 REORDER_ANIMATION_DURATION);
2568 }
2569 mShortcutsAndWidgets.requestLayout();
2570 }
2571 return swapSolution.isSolution;
2572 }
2573
Adam Cohen482ed822012-03-02 14:15:13 -08002574 int[] createArea(int pixelX, int pixelY, int minSpanX, int minSpanY, int spanX, int spanY,
2575 View dragView, int[] result, int resultSpan[], int mode) {
Adam Cohen482ed822012-03-02 14:15:13 -08002576 // First we determine if things have moved enough to cause a different layout
Adam Cohen47a876d2012-03-19 13:21:41 -07002577 result = findNearestArea(pixelX, pixelY, spanX, spanY, result);
Adam Cohen482ed822012-03-02 14:15:13 -08002578
2579 if (resultSpan == null) {
2580 resultSpan = new int[2];
2581 }
2582
Adam Cohen19f37922012-03-21 11:59:11 -07002583 // When we are checking drop validity or actually dropping, we don't recompute the
2584 // direction vector, since we want the solution to match the preview, and it's possible
2585 // that the exact position of the item has changed to result in a new reordering outcome.
Adam Cohenb209e632012-03-27 17:09:36 -07002586 if ((mode == MODE_ON_DROP || mode == MODE_ON_DROP_EXTERNAL || mode == MODE_ACCEPT_DROP)
2587 && mPreviousReorderDirection[0] != INVALID_DIRECTION) {
Adam Cohen19f37922012-03-21 11:59:11 -07002588 mDirectionVector[0] = mPreviousReorderDirection[0];
2589 mDirectionVector[1] = mPreviousReorderDirection[1];
2590 // We reset this vector after drop
Adam Cohenb209e632012-03-27 17:09:36 -07002591 if (mode == MODE_ON_DROP || mode == MODE_ON_DROP_EXTERNAL) {
2592 mPreviousReorderDirection[0] = INVALID_DIRECTION;
2593 mPreviousReorderDirection[1] = INVALID_DIRECTION;
Adam Cohen19f37922012-03-21 11:59:11 -07002594 }
2595 } else {
2596 getDirectionVectorForDrop(pixelX, pixelY, spanX, spanY, dragView, mDirectionVector);
2597 mPreviousReorderDirection[0] = mDirectionVector[0];
2598 mPreviousReorderDirection[1] = mDirectionVector[1];
2599 }
2600
Adam Cohen482ed822012-03-02 14:15:13 -08002601 ItemConfiguration swapSolution = simpleSwap(pixelX, pixelY, minSpanX, minSpanY,
2602 spanX, spanY, mDirectionVector, dragView, true, new ItemConfiguration());
2603
2604 // We attempt the approach which doesn't shuffle views at all
2605 ItemConfiguration noShuffleSolution = findConfigurationNoShuffle(pixelX, pixelY, minSpanX,
2606 minSpanY, spanX, spanY, dragView, new ItemConfiguration());
2607
2608 ItemConfiguration finalSolution = null;
2609 if (swapSolution.isSolution && swapSolution.area() >= noShuffleSolution.area()) {
2610 finalSolution = swapSolution;
2611 } else if (noShuffleSolution.isSolution) {
2612 finalSolution = noShuffleSolution;
2613 }
2614
2615 boolean foundSolution = true;
2616 if (!DESTRUCTIVE_REORDER) {
2617 setUseTempCoords(true);
2618 }
2619
2620 if (finalSolution != null) {
2621 result[0] = finalSolution.dragViewX;
2622 result[1] = finalSolution.dragViewY;
2623 resultSpan[0] = finalSolution.dragViewSpanX;
2624 resultSpan[1] = finalSolution.dragViewSpanY;
2625
2626 // If we're just testing for a possible location (MODE_ACCEPT_DROP), we don't bother
2627 // committing anything or animating anything as we just want to determine if a solution
2628 // exists
2629 if (mode == MODE_DRAG_OVER || mode == MODE_ON_DROP || mode == MODE_ON_DROP_EXTERNAL) {
2630 if (!DESTRUCTIVE_REORDER) {
2631 copySolutionToTempState(finalSolution, dragView);
2632 }
2633 setItemPlacementDirty(true);
2634 animateItemsToSolution(finalSolution, dragView, mode == MODE_ON_DROP);
2635
Adam Cohen19f37922012-03-21 11:59:11 -07002636 if (!DESTRUCTIVE_REORDER &&
2637 (mode == MODE_ON_DROP || mode == MODE_ON_DROP_EXTERNAL)) {
Adam Cohen482ed822012-03-02 14:15:13 -08002638 commitTempPlacement();
Adam Cohen19f37922012-03-21 11:59:11 -07002639 completeAndClearReorderHintAnimations();
2640 setItemPlacementDirty(false);
2641 } else {
2642 beginOrAdjustHintAnimations(finalSolution, dragView,
2643 REORDER_ANIMATION_DURATION);
Adam Cohen482ed822012-03-02 14:15:13 -08002644 }
2645 }
2646 } else {
2647 foundSolution = false;
2648 result[0] = result[1] = resultSpan[0] = resultSpan[1] = -1;
2649 }
2650
2651 if ((mode == MODE_ON_DROP || !foundSolution) && !DESTRUCTIVE_REORDER) {
2652 setUseTempCoords(false);
2653 }
Adam Cohen482ed822012-03-02 14:15:13 -08002654
Michael Jurkaa52570f2012-03-20 03:18:20 -07002655 mShortcutsAndWidgets.requestLayout();
Adam Cohen482ed822012-03-02 14:15:13 -08002656 return result;
2657 }
2658
Adam Cohen19f37922012-03-21 11:59:11 -07002659 void setItemPlacementDirty(boolean dirty) {
2660 mItemPlacementDirty = dirty;
Adam Cohen482ed822012-03-02 14:15:13 -08002661 }
Adam Cohen19f37922012-03-21 11:59:11 -07002662 boolean isItemPlacementDirty() {
2663 return mItemPlacementDirty;
Adam Cohen482ed822012-03-02 14:15:13 -08002664 }
2665
2666 private class ItemConfiguration {
Adam Cohen8baab352012-03-20 17:39:21 -07002667 HashMap<View, CellAndSpan> map = new HashMap<View, CellAndSpan>();
Adam Cohenf3900c22012-11-16 18:28:11 -08002668 private HashMap<View, CellAndSpan> savedMap = new HashMap<View, CellAndSpan>();
2669 ArrayList<View> sortedViews = new ArrayList<View>();
Adam Cohen482ed822012-03-02 14:15:13 -08002670 boolean isSolution = false;
2671 int dragViewX, dragViewY, dragViewSpanX, dragViewSpanY;
2672
Adam Cohenf3900c22012-11-16 18:28:11 -08002673 void save() {
2674 // Copy current state into savedMap
2675 for (View v: map.keySet()) {
2676 map.get(v).copy(savedMap.get(v));
2677 }
2678 }
2679
2680 void restore() {
2681 // Restore current state from savedMap
2682 for (View v: savedMap.keySet()) {
2683 savedMap.get(v).copy(map.get(v));
2684 }
2685 }
2686
2687 void add(View v, CellAndSpan cs) {
2688 map.put(v, cs);
2689 savedMap.put(v, new CellAndSpan());
2690 sortedViews.add(v);
2691 }
2692
Adam Cohen482ed822012-03-02 14:15:13 -08002693 int area() {
2694 return dragViewSpanX * dragViewSpanY;
2695 }
Adam Cohen8baab352012-03-20 17:39:21 -07002696 }
2697
2698 private class CellAndSpan {
2699 int x, y;
2700 int spanX, spanY;
2701
Adam Cohenf3900c22012-11-16 18:28:11 -08002702 public CellAndSpan() {
2703 }
2704
2705 public void copy(CellAndSpan copy) {
2706 copy.x = x;
2707 copy.y = y;
2708 copy.spanX = spanX;
2709 copy.spanY = spanY;
2710 }
2711
Adam Cohen8baab352012-03-20 17:39:21 -07002712 public CellAndSpan(int x, int y, int spanX, int spanY) {
2713 this.x = x;
2714 this.y = y;
2715 this.spanX = spanX;
2716 this.spanY = spanY;
Adam Cohen482ed822012-03-02 14:15:13 -08002717 }
Adam Cohenf3900c22012-11-16 18:28:11 -08002718
2719 public String toString() {
2720 return "(" + x + ", " + y + ": " + spanX + ", " + spanY + ")";
2721 }
2722
Adam Cohen482ed822012-03-02 14:15:13 -08002723 }
2724
Adam Cohendf035382011-04-11 17:22:04 -07002725 /**
2726 * Find a vacant area that will fit the given bounds nearest the requested
2727 * cell location. Uses Euclidean distance to score multiple vacant areas.
2728 *
2729 * @param pixelX The X location at which you want to search for a vacant area.
2730 * @param pixelY The Y location at which you want to search for a vacant area.
2731 * @param spanX Horizontal span of the object.
2732 * @param spanY Vertical span of the object.
2733 * @param ignoreView Considers space occupied by this view as unoccupied
2734 * @param result Previously returned value to possibly recycle.
2735 * @return The X, Y cell of a vacant area that can contain this object,
2736 * nearest the requested location.
2737 */
2738 int[] findNearestVacantArea(
2739 int pixelX, int pixelY, int spanX, int spanY, View ignoreView, int[] result) {
2740 return findNearestArea(pixelX, pixelY, spanX, spanY, ignoreView, true, result);
2741 }
2742
2743 /**
Adam Cohend41fbf52012-02-16 23:53:59 -08002744 * Find a vacant area that will fit the given bounds nearest the requested
2745 * cell location. Uses Euclidean distance to score multiple vacant areas.
2746 *
2747 * @param pixelX The X location at which you want to search for a vacant area.
2748 * @param pixelY The Y location at which you want to search for a vacant area.
2749 * @param minSpanX The minimum horizontal span required
2750 * @param minSpanY The minimum vertical span required
2751 * @param spanX Horizontal span of the object.
2752 * @param spanY Vertical span of the object.
2753 * @param ignoreView Considers space occupied by this view as unoccupied
2754 * @param result Previously returned value to possibly recycle.
2755 * @return The X, Y cell of a vacant area that can contain this object,
2756 * nearest the requested location.
2757 */
2758 int[] findNearestVacantArea(int pixelX, int pixelY, int minSpanX, int minSpanY,
2759 int spanX, int spanY, View ignoreView, int[] result, int[] resultSpan) {
Adam Cohen482ed822012-03-02 14:15:13 -08002760 return findNearestArea(pixelX, pixelY, minSpanX, minSpanY, spanX, spanY, ignoreView, true,
2761 result, resultSpan, mOccupied);
Adam Cohend41fbf52012-02-16 23:53:59 -08002762 }
2763
2764 /**
Adam Cohendf035382011-04-11 17:22:04 -07002765 * Find a starting cell position that will fit the given bounds nearest the requested
2766 * cell location. Uses Euclidean distance to score multiple vacant areas.
2767 *
2768 * @param pixelX The X location at which you want to search for a vacant area.
2769 * @param pixelY The Y location at which you want to search for a vacant area.
2770 * @param spanX Horizontal span of the object.
2771 * @param spanY Vertical span of the object.
2772 * @param ignoreView Considers space occupied by this view as unoccupied
2773 * @param result Previously returned value to possibly recycle.
2774 * @return The X, Y cell of a vacant area that can contain this object,
2775 * nearest the requested location.
2776 */
2777 int[] findNearestArea(
2778 int pixelX, int pixelY, int spanX, int spanY, int[] result) {
2779 return findNearestArea(pixelX, pixelY, spanX, spanY, null, false, result);
2780 }
2781
Michael Jurka0280c3b2010-09-17 15:00:07 -07002782 boolean existsEmptyCell() {
2783 return findCellForSpan(null, 1, 1);
2784 }
2785
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002786 /**
Michael Jurka0280c3b2010-09-17 15:00:07 -07002787 * Finds the upper-left coordinate of the first rectangle in the grid that can
2788 * hold a cell of the specified dimensions. If intersectX and intersectY are not -1,
2789 * then this method will only return coordinates for rectangles that contain the cell
2790 * (intersectX, intersectY)
2791 *
2792 * @param cellXY The array that will contain the position of a vacant cell if such a cell
2793 * can be found.
2794 * @param spanX The horizontal span of the cell we want to find.
2795 * @param spanY The vertical span of the cell we want to find.
2796 *
2797 * @return True if a vacant cell of the specified dimension was found, false otherwise.
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07002798 */
Michael Jurka0280c3b2010-09-17 15:00:07 -07002799 boolean findCellForSpan(int[] cellXY, int spanX, int spanY) {
Adam Cohen482ed822012-03-02 14:15:13 -08002800 return findCellForSpanThatIntersectsIgnoring(cellXY, spanX, spanY, -1, -1, null, mOccupied);
Michael Jurka0280c3b2010-09-17 15:00:07 -07002801 }
2802
2803 /**
2804 * Like above, but ignores any cells occupied by the item "ignoreView"
2805 *
2806 * @param cellXY The array that will contain the position of a vacant cell if such a cell
2807 * can be found.
2808 * @param spanX The horizontal span of the cell we want to find.
2809 * @param spanY The vertical span of the cell we want to find.
2810 * @param ignoreView The home screen item we should treat as not occupying any space
2811 * @return
2812 */
2813 boolean findCellForSpanIgnoring(int[] cellXY, int spanX, int spanY, View ignoreView) {
Adam Cohen482ed822012-03-02 14:15:13 -08002814 return findCellForSpanThatIntersectsIgnoring(cellXY, spanX, spanY, -1, -1,
2815 ignoreView, mOccupied);
Michael Jurka0280c3b2010-09-17 15:00:07 -07002816 }
2817
2818 /**
2819 * Like above, but if intersectX and intersectY are not -1, then this method will try to
2820 * return coordinates for rectangles that contain the cell [intersectX, intersectY]
2821 *
2822 * @param spanX The horizontal span of the cell we want to find.
2823 * @param spanY The vertical span of the cell we want to find.
2824 * @param ignoreView The home screen item we should treat as not occupying any space
2825 * @param intersectX The X coordinate of the cell that we should try to overlap
2826 * @param intersectX The Y coordinate of the cell that we should try to overlap
2827 *
2828 * @return True if a vacant cell of the specified dimension was found, false otherwise.
2829 */
2830 boolean findCellForSpanThatIntersects(int[] cellXY, int spanX, int spanY,
2831 int intersectX, int intersectY) {
2832 return findCellForSpanThatIntersectsIgnoring(
Adam Cohen482ed822012-03-02 14:15:13 -08002833 cellXY, spanX, spanY, intersectX, intersectY, null, mOccupied);
Michael Jurka0280c3b2010-09-17 15:00:07 -07002834 }
2835
2836 /**
2837 * The superset of the above two methods
2838 */
2839 boolean findCellForSpanThatIntersectsIgnoring(int[] cellXY, int spanX, int spanY,
Adam Cohen482ed822012-03-02 14:15:13 -08002840 int intersectX, int intersectY, View ignoreView, boolean occupied[][]) {
Michael Jurkac6ee42e2010-09-30 12:04:50 -07002841 // mark space take by ignoreView as available (method checks if ignoreView is null)
Adam Cohen482ed822012-03-02 14:15:13 -08002842 markCellsAsUnoccupiedForView(ignoreView, occupied);
Michael Jurka0280c3b2010-09-17 15:00:07 -07002843
Michael Jurka28750fb2010-09-24 17:43:49 -07002844 boolean foundCell = false;
Michael Jurka0280c3b2010-09-17 15:00:07 -07002845 while (true) {
2846 int startX = 0;
2847 if (intersectX >= 0) {
2848 startX = Math.max(startX, intersectX - (spanX - 1));
2849 }
2850 int endX = mCountX - (spanX - 1);
2851 if (intersectX >= 0) {
2852 endX = Math.min(endX, intersectX + (spanX - 1) + (spanX == 1 ? 1 : 0));
2853 }
2854 int startY = 0;
2855 if (intersectY >= 0) {
2856 startY = Math.max(startY, intersectY - (spanY - 1));
2857 }
2858 int endY = mCountY - (spanY - 1);
2859 if (intersectY >= 0) {
2860 endY = Math.min(endY, intersectY + (spanY - 1) + (spanY == 1 ? 1 : 0));
2861 }
2862
Winson Chungbbc60d82010-11-11 16:34:41 -08002863 for (int y = startY; y < endY && !foundCell; y++) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002864 inner:
Winson Chungbbc60d82010-11-11 16:34:41 -08002865 for (int x = startX; x < endX; x++) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002866 for (int i = 0; i < spanX; i++) {
2867 for (int j = 0; j < spanY; j++) {
Adam Cohen482ed822012-03-02 14:15:13 -08002868 if (occupied[x + i][y + j]) {
Winson Chungbbc60d82010-11-11 16:34:41 -08002869 // small optimization: we can skip to after the column we just found
Michael Jurka0280c3b2010-09-17 15:00:07 -07002870 // an occupied cell
Winson Chungbbc60d82010-11-11 16:34:41 -08002871 x += i;
Michael Jurka0280c3b2010-09-17 15:00:07 -07002872 continue inner;
2873 }
2874 }
2875 }
2876 if (cellXY != null) {
2877 cellXY[0] = x;
2878 cellXY[1] = y;
2879 }
Michael Jurka28750fb2010-09-24 17:43:49 -07002880 foundCell = true;
2881 break;
Michael Jurka0280c3b2010-09-17 15:00:07 -07002882 }
2883 }
2884 if (intersectX == -1 && intersectY == -1) {
2885 break;
2886 } else {
2887 // if we failed to find anything, try again but without any requirements of
2888 // intersecting
2889 intersectX = -1;
2890 intersectY = -1;
2891 continue;
2892 }
2893 }
2894
Michael Jurkac6ee42e2010-09-30 12:04:50 -07002895 // re-mark space taken by ignoreView as occupied
Adam Cohen482ed822012-03-02 14:15:13 -08002896 markCellsAsOccupiedForView(ignoreView, occupied);
Michael Jurka28750fb2010-09-24 17:43:49 -07002897 return foundCell;
Michael Jurka0280c3b2010-09-17 15:00:07 -07002898 }
2899
2900 /**
Winson Chungc07918d2011-07-01 15:35:26 -07002901 * A drag event has begun over this layout.
2902 * It may have begun over this layout (in which case onDragChild is called first),
2903 * or it may have begun on another layout.
2904 */
2905 void onDragEnter() {
Adam Cohenc6cc61d2012-04-04 12:47:08 -07002906 mDragEnforcer.onDragEnter();
Winson Chungc07918d2011-07-01 15:35:26 -07002907 mDragging = true;
2908 }
2909
2910 /**
Michael Jurka0280c3b2010-09-17 15:00:07 -07002911 * Called when drag has left this CellLayout or has been completed (successfully or not)
2912 */
2913 void onDragExit() {
Adam Cohenc6cc61d2012-04-04 12:47:08 -07002914 mDragEnforcer.onDragExit();
Joe Onorato4be866d2010-10-10 11:26:02 -07002915 // This can actually be called when we aren't in a drag, e.g. when adding a new
2916 // item to this layout via the customize drawer.
2917 // Guard against that case.
2918 if (mDragging) {
2919 mDragging = false;
Patrick Dubroyde7658b2010-09-27 11:15:43 -07002920 }
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -07002921
2922 // Invalidate the drag data
Adam Cohend41fbf52012-02-16 23:53:59 -08002923 mDragCell[0] = mDragCell[1] = -1;
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -07002924 mDragOutlineAnims[mDragOutlineCurrent].animateOut();
2925 mDragOutlineCurrent = (mDragOutlineCurrent + 1) % mDragOutlineAnims.length;
Adam Cohen19f37922012-03-21 11:59:11 -07002926 revertTempState();
Michael Jurka33945b22010-12-21 18:19:38 -08002927 setIsDragOverlapping(false);
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07002928 }
2929
2930 /**
Winson Chungaafa03c2010-06-11 17:34:16 -07002931 * Mark a child as having been dropped.
Patrick Dubroyde7658b2010-09-27 11:15:43 -07002932 * At the beginning of the drag operation, the child may have been on another
Patrick Dubroyce34a972010-10-19 10:34:32 -07002933 * screen, but it is re-parented before this method is called.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002934 *
2935 * @param child The child that is being dropped
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002936 */
Adam Cohen716b51e2011-06-30 12:09:54 -07002937 void onDropChild(View child) {
Romain Guyd94533d2009-08-17 10:01:15 -07002938 if (child != null) {
2939 LayoutParams lp = (LayoutParams) child.getLayoutParams();
Romain Guy84f296c2009-11-04 15:00:44 -08002940 lp.dropped = true;
Romain Guyd94533d2009-08-17 10:01:15 -07002941 child.requestLayout();
Romain Guyd94533d2009-08-17 10:01:15 -07002942 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002943 }
2944
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002945 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002946 * Computes a bounding rectangle for a range of cells
Winson Chungaafa03c2010-06-11 17:34:16 -07002947 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002948 * @param cellX X coordinate of upper left corner expressed as a cell position
2949 * @param cellY Y coordinate of upper left corner expressed as a cell position
Winson Chungaafa03c2010-06-11 17:34:16 -07002950 * @param cellHSpan Width in cells
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002951 * @param cellVSpan Height in cells
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07002952 * @param resultRect Rect into which to put the results
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002953 */
Adam Cohend41fbf52012-02-16 23:53:59 -08002954 public void cellToRect(int cellX, int cellY, int cellHSpan, int cellVSpan, Rect resultRect) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002955 final int cellWidth = mCellWidth;
2956 final int cellHeight = mCellHeight;
2957 final int widthGap = mWidthGap;
2958 final int heightGap = mHeightGap;
Winson Chungaafa03c2010-06-11 17:34:16 -07002959
Winson Chung4b825dcd2011-06-19 12:41:22 -07002960 final int hStartPadding = getPaddingLeft();
2961 final int vStartPadding = getPaddingTop();
Winson Chungaafa03c2010-06-11 17:34:16 -07002962
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002963 int width = cellHSpan * cellWidth + ((cellHSpan - 1) * widthGap);
2964 int height = cellVSpan * cellHeight + ((cellVSpan - 1) * heightGap);
2965
2966 int x = hStartPadding + cellX * (cellWidth + widthGap);
2967 int y = vStartPadding + cellY * (cellHeight + heightGap);
Winson Chungaafa03c2010-06-11 17:34:16 -07002968
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07002969 resultRect.set(x, y, x + width, y + height);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002970 }
Winson Chungaafa03c2010-06-11 17:34:16 -07002971
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002972 /**
Winson Chungaafa03c2010-06-11 17:34:16 -07002973 * Computes the required horizontal and vertical cell spans to always
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002974 * fit the given rectangle.
Winson Chungaafa03c2010-06-11 17:34:16 -07002975 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002976 * @param width Width in pixels
2977 * @param height Height in pixels
Patrick Dubroy8f86ddc2010-07-16 13:55:32 -07002978 * @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 -08002979 */
Patrick Dubroy8f86ddc2010-07-16 13:55:32 -07002980 public int[] rectToCell(int width, int height, int[] result) {
Michael Jurka9987a5c2010-10-08 16:58:12 -07002981 return rectToCell(getResources(), width, height, result);
2982 }
2983
2984 public static int[] rectToCell(Resources resources, int width, int height, int[] result) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002985 // Always assume we're working with the smallest span to make sure we
2986 // reserve enough space in both orientations.
Joe Onorato79e56262009-09-21 15:23:04 -04002987 int actualWidth = resources.getDimensionPixelSize(R.dimen.workspace_cell_width);
2988 int actualHeight = resources.getDimensionPixelSize(R.dimen.workspace_cell_height);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002989 int smallerSize = Math.min(actualWidth, actualHeight);
Joe Onorato79e56262009-09-21 15:23:04 -04002990
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002991 // Always round up to next largest cell
Winson Chung54c725c2011-08-03 12:03:40 -07002992 int spanX = (int) Math.ceil(width / (float) smallerSize);
2993 int spanY = (int) Math.ceil(height / (float) smallerSize);
Joe Onorato79e56262009-09-21 15:23:04 -04002994
Patrick Dubroy8f86ddc2010-07-16 13:55:32 -07002995 if (result == null) {
2996 return new int[] { spanX, spanY };
2997 }
2998 result[0] = spanX;
2999 result[1] = spanY;
3000 return result;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003001 }
3002
Michael Jurkaf12c75c2011-01-25 22:41:40 -08003003 public int[] cellSpansToSize(int hSpans, int vSpans) {
3004 int[] size = new int[2];
3005 size[0] = hSpans * mCellWidth + (hSpans - 1) * mWidthGap;
3006 size[1] = vSpans * mCellHeight + (vSpans - 1) * mHeightGap;
3007 return size;
3008 }
3009
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003010 /**
Patrick Dubroy047379a2010-12-19 22:02:04 -08003011 * Calculate the grid spans needed to fit given item
3012 */
3013 public void calculateSpans(ItemInfo info) {
3014 final int minWidth;
3015 final int minHeight;
3016
3017 if (info instanceof LauncherAppWidgetInfo) {
3018 minWidth = ((LauncherAppWidgetInfo) info).minWidth;
3019 minHeight = ((LauncherAppWidgetInfo) info).minHeight;
3020 } else if (info instanceof PendingAddWidgetInfo) {
3021 minWidth = ((PendingAddWidgetInfo) info).minWidth;
3022 minHeight = ((PendingAddWidgetInfo) info).minHeight;
3023 } else {
3024 // It's not a widget, so it must be 1x1
3025 info.spanX = info.spanY = 1;
3026 return;
3027 }
3028 int[] spans = rectToCell(minWidth, minHeight, null);
3029 info.spanX = spans[0];
3030 info.spanY = spans[1];
3031 }
3032
3033 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003034 * Find the first vacant cell, if there is one.
3035 *
3036 * @param vacant Holds the x and y coordinate of the vacant cell
3037 * @param spanX Horizontal cell span.
3038 * @param spanY Vertical cell span.
Winson Chungaafa03c2010-06-11 17:34:16 -07003039 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003040 * @return True if a vacant cell was found
3041 */
3042 public boolean getVacantCell(int[] vacant, int spanX, int spanY) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003043
Michael Jurka0280c3b2010-09-17 15:00:07 -07003044 return findVacantCell(vacant, spanX, spanY, mCountX, mCountY, mOccupied);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003045 }
3046
3047 static boolean findVacantCell(int[] vacant, int spanX, int spanY,
3048 int xCount, int yCount, boolean[][] occupied) {
3049
Adam Cohen2801caf2011-05-13 20:57:39 -07003050 for (int y = 0; y < yCount; y++) {
3051 for (int x = 0; x < xCount; x++) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003052 boolean available = !occupied[x][y];
3053out: for (int i = x; i < x + spanX - 1 && x < xCount; i++) {
3054 for (int j = y; j < y + spanY - 1 && y < yCount; j++) {
3055 available = available && !occupied[i][j];
3056 if (!available) break out;
3057 }
3058 }
3059
3060 if (available) {
3061 vacant[0] = x;
3062 vacant[1] = y;
3063 return true;
3064 }
3065 }
3066 }
3067
3068 return false;
3069 }
3070
Michael Jurka0280c3b2010-09-17 15:00:07 -07003071 private void clearOccupiedCells() {
3072 for (int x = 0; x < mCountX; x++) {
3073 for (int y = 0; y < mCountY; y++) {
3074 mOccupied[x][y] = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003075 }
3076 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07003077 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003078
Adam Cohend41fbf52012-02-16 23:53:59 -08003079 public void onMove(View view, int newCellX, int newCellY, int newSpanX, int newSpanY) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07003080 markCellsAsUnoccupiedForView(view);
Adam Cohen482ed822012-03-02 14:15:13 -08003081 markCellsForView(newCellX, newCellY, newSpanX, newSpanY, mOccupied, true);
Michael Jurka0280c3b2010-09-17 15:00:07 -07003082 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003083
Adam Cohend4844c32011-02-18 19:25:06 -08003084 public void markCellsAsOccupiedForView(View view) {
Adam Cohen482ed822012-03-02 14:15:13 -08003085 markCellsAsOccupiedForView(view, mOccupied);
3086 }
3087 public void markCellsAsOccupiedForView(View view, boolean[][] occupied) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07003088 if (view == null || view.getParent() != mShortcutsAndWidgets) return;
Michael Jurka0280c3b2010-09-17 15:00:07 -07003089 LayoutParams lp = (LayoutParams) view.getLayoutParams();
Adam Cohen482ed822012-03-02 14:15:13 -08003090 markCellsForView(lp.cellX, lp.cellY, lp.cellHSpan, lp.cellVSpan, occupied, true);
Michael Jurka0280c3b2010-09-17 15:00:07 -07003091 }
3092
Adam Cohend4844c32011-02-18 19:25:06 -08003093 public void markCellsAsUnoccupiedForView(View view) {
Adam Cohen482ed822012-03-02 14:15:13 -08003094 markCellsAsUnoccupiedForView(view, mOccupied);
3095 }
3096 public void markCellsAsUnoccupiedForView(View view, boolean occupied[][]) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07003097 if (view == null || view.getParent() != mShortcutsAndWidgets) return;
Michael Jurka0280c3b2010-09-17 15:00:07 -07003098 LayoutParams lp = (LayoutParams) view.getLayoutParams();
Adam Cohen482ed822012-03-02 14:15:13 -08003099 markCellsForView(lp.cellX, lp.cellY, lp.cellHSpan, lp.cellVSpan, occupied, false);
Michael Jurka0280c3b2010-09-17 15:00:07 -07003100 }
3101
Adam Cohen482ed822012-03-02 14:15:13 -08003102 private void markCellsForView(int cellX, int cellY, int spanX, int spanY, boolean[][] occupied,
3103 boolean value) {
3104 if (cellX < 0 || cellY < 0) return;
Michael Jurka0280c3b2010-09-17 15:00:07 -07003105 for (int x = cellX; x < cellX + spanX && x < mCountX; x++) {
3106 for (int y = cellY; y < cellY + spanY && y < mCountY; y++) {
Adam Cohen482ed822012-03-02 14:15:13 -08003107 occupied[x][y] = value;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003108 }
3109 }
3110 }
3111
Adam Cohen2801caf2011-05-13 20:57:39 -07003112 public int getDesiredWidth() {
Michael Jurka8b805b12012-04-18 14:23:14 -07003113 return getPaddingLeft() + getPaddingRight() + (mCountX * mCellWidth) +
Adam Cohen2801caf2011-05-13 20:57:39 -07003114 (Math.max((mCountX - 1), 0) * mWidthGap);
3115 }
3116
3117 public int getDesiredHeight() {
Michael Jurka8b805b12012-04-18 14:23:14 -07003118 return getPaddingTop() + getPaddingBottom() + (mCountY * mCellHeight) +
Adam Cohen2801caf2011-05-13 20:57:39 -07003119 (Math.max((mCountY - 1), 0) * mHeightGap);
3120 }
3121
Michael Jurka66d72172011-04-12 16:29:25 -07003122 public boolean isOccupied(int x, int y) {
3123 if (x < mCountX && y < mCountY) {
3124 return mOccupied[x][y];
3125 } else {
3126 throw new RuntimeException("Position exceeds the bound of this CellLayout");
3127 }
3128 }
3129
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003130 @Override
3131 public ViewGroup.LayoutParams generateLayoutParams(AttributeSet attrs) {
3132 return new CellLayout.LayoutParams(getContext(), attrs);
3133 }
3134
3135 @Override
3136 protected boolean checkLayoutParams(ViewGroup.LayoutParams p) {
3137 return p instanceof CellLayout.LayoutParams;
3138 }
3139
3140 @Override
3141 protected ViewGroup.LayoutParams generateLayoutParams(ViewGroup.LayoutParams p) {
3142 return new CellLayout.LayoutParams(p);
3143 }
3144
Winson Chungaafa03c2010-06-11 17:34:16 -07003145 public static class CellLayoutAnimationController extends LayoutAnimationController {
3146 public CellLayoutAnimationController(Animation animation, float delay) {
3147 super(animation, delay);
3148 }
3149
3150 @Override
3151 protected long getDelayForView(View view) {
3152 return (int) (Math.random() * 150);
3153 }
3154 }
3155
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003156 public static class LayoutParams extends ViewGroup.MarginLayoutParams {
3157 /**
3158 * Horizontal location of the item in the grid.
3159 */
3160 @ViewDebug.ExportedProperty
3161 public int cellX;
3162
3163 /**
3164 * Vertical location of the item in the grid.
3165 */
3166 @ViewDebug.ExportedProperty
3167 public int cellY;
3168
3169 /**
Adam Cohen482ed822012-03-02 14:15:13 -08003170 * Temporary horizontal location of the item in the grid during reorder
3171 */
3172 public int tmpCellX;
3173
3174 /**
3175 * Temporary vertical location of the item in the grid during reorder
3176 */
3177 public int tmpCellY;
3178
3179 /**
3180 * Indicates that the temporary coordinates should be used to layout the items
3181 */
3182 public boolean useTmpCoords;
3183
3184 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003185 * Number of cells spanned horizontally by the item.
3186 */
3187 @ViewDebug.ExportedProperty
3188 public int cellHSpan;
3189
3190 /**
3191 * Number of cells spanned vertically by the item.
3192 */
3193 @ViewDebug.ExportedProperty
3194 public int cellVSpan;
Winson Chungaafa03c2010-06-11 17:34:16 -07003195
Adam Cohen1b607ed2011-03-03 17:26:50 -08003196 /**
3197 * Indicates whether the item will set its x, y, width and height parameters freely,
3198 * or whether these will be computed based on cellX, cellY, cellHSpan and cellVSpan.
3199 */
Adam Cohend4844c32011-02-18 19:25:06 -08003200 public boolean isLockedToGrid = true;
3201
Adam Cohen482ed822012-03-02 14:15:13 -08003202 /**
3203 * Indicates whether this item can be reordered. Always true except in the case of the
3204 * the AllApps button.
3205 */
3206 public boolean canReorder = true;
3207
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003208 // X coordinate of the view in the layout.
3209 @ViewDebug.ExportedProperty
3210 int x;
3211 // Y coordinate of the view in the layout.
3212 @ViewDebug.ExportedProperty
3213 int y;
3214
Romain Guy84f296c2009-11-04 15:00:44 -08003215 boolean dropped;
Romain Guyfcb9e712009-10-02 16:06:52 -07003216
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003217 public LayoutParams(Context c, AttributeSet attrs) {
3218 super(c, attrs);
3219 cellHSpan = 1;
3220 cellVSpan = 1;
3221 }
3222
3223 public LayoutParams(ViewGroup.LayoutParams source) {
3224 super(source);
3225 cellHSpan = 1;
3226 cellVSpan = 1;
3227 }
Winson Chungaafa03c2010-06-11 17:34:16 -07003228
3229 public LayoutParams(LayoutParams source) {
3230 super(source);
3231 this.cellX = source.cellX;
3232 this.cellY = source.cellY;
3233 this.cellHSpan = source.cellHSpan;
3234 this.cellVSpan = source.cellVSpan;
3235 }
3236
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003237 public LayoutParams(int cellX, int cellY, int cellHSpan, int cellVSpan) {
Romain Guy8f19cdd2010-01-08 15:07:00 -08003238 super(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003239 this.cellX = cellX;
3240 this.cellY = cellY;
3241 this.cellHSpan = cellHSpan;
3242 this.cellVSpan = cellVSpan;
3243 }
3244
Adam Cohen7f4eabe2011-04-21 16:19:16 -07003245 public void setup(int cellWidth, int cellHeight, int widthGap, int heightGap) {
Adam Cohend4844c32011-02-18 19:25:06 -08003246 if (isLockedToGrid) {
3247 final int myCellHSpan = cellHSpan;
3248 final int myCellVSpan = cellVSpan;
Adam Cohen482ed822012-03-02 14:15:13 -08003249 final int myCellX = useTmpCoords ? tmpCellX : cellX;
3250 final int myCellY = useTmpCoords ? tmpCellY : cellY;
Adam Cohen1b607ed2011-03-03 17:26:50 -08003251
Adam Cohend4844c32011-02-18 19:25:06 -08003252 width = myCellHSpan * cellWidth + ((myCellHSpan - 1) * widthGap) -
3253 leftMargin - rightMargin;
3254 height = myCellVSpan * cellHeight + ((myCellVSpan - 1) * heightGap) -
3255 topMargin - bottomMargin;
Winson Chungeecf02d2012-03-02 17:14:58 -08003256 x = (int) (myCellX * (cellWidth + widthGap) + leftMargin);
3257 y = (int) (myCellY * (cellHeight + heightGap) + topMargin);
Adam Cohend4844c32011-02-18 19:25:06 -08003258 }
3259 }
Winson Chungaafa03c2010-06-11 17:34:16 -07003260
Winson Chungaafa03c2010-06-11 17:34:16 -07003261 public String toString() {
3262 return "(" + this.cellX + ", " + this.cellY + ")";
3263 }
Adam Cohen7f4eabe2011-04-21 16:19:16 -07003264
3265 public void setWidth(int width) {
3266 this.width = width;
3267 }
3268
3269 public int getWidth() {
3270 return width;
3271 }
3272
3273 public void setHeight(int height) {
3274 this.height = height;
3275 }
3276
3277 public int getHeight() {
3278 return height;
3279 }
3280
3281 public void setX(int x) {
3282 this.x = x;
3283 }
3284
3285 public int getX() {
3286 return x;
3287 }
3288
3289 public void setY(int y) {
3290 this.y = y;
3291 }
3292
3293 public int getY() {
3294 return y;
3295 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003296 }
3297
Michael Jurka0280c3b2010-09-17 15:00:07 -07003298 // This class stores info for two purposes:
3299 // 1. When dragging items (mDragInfo in Workspace), we store the View, its cellX & cellY,
3300 // its spanX, spanY, and the screen it is on
3301 // 2. When long clicking on an empty cell in a CellLayout, we save information about the
3302 // cellX and cellY coordinates and which page was clicked. We then set this as a tag on
3303 // the CellLayout that was long clicked
Michael Jurkae5fb0f22011-04-11 13:27:46 -07003304 static final class CellInfo {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003305 View cell;
Michael Jurkaa63c4522010-08-19 13:52:27 -07003306 int cellX = -1;
3307 int cellY = -1;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003308 int spanX;
3309 int spanY;
3310 int screen;
Winson Chung3d503fb2011-07-13 17:25:49 -07003311 long container;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003312
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003313 @Override
3314 public String toString() {
Winson Chungaafa03c2010-06-11 17:34:16 -07003315 return "Cell[view=" + (cell == null ? "null" : cell.getClass())
3316 + ", x=" + cellX + ", y=" + cellY + "]";
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003317 }
3318 }
Michael Jurkad771c962011-08-09 15:00:48 -07003319
3320 public boolean lastDownOnOccupiedCell() {
3321 return mLastDownOnOccupiedCell;
3322 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003323}