blob: 23d1d61c80edd4001584ea91e03ed225f6363338 [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 // CellLayout guarantees its children are not laid out outside of its bounds,
185 // child clipping is thus unnecessary and disabling it offers better performance
186 // when we rotate the whole layout
187 setClipChildren(false);
188 setClipToPadding(false);
Adam Cohen2acce882012-03-28 19:03:19 -0700189 mLauncher = (Launcher) context;
Michael Jurkaa63c4522010-08-19 13:52:27 -0700190
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800191 TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.CellLayout, defStyle, 0);
192
Adam Cohenf4bd5792012-04-27 11:35:29 -0700193 mCellWidth = a.getDimensionPixelSize(R.styleable.CellLayout_cellWidth, 10);
194 mCellHeight = a.getDimensionPixelSize(R.styleable.CellLayout_cellHeight, 10);
Adam Cohen234c4cd2011-07-17 21:03:04 -0700195 mWidthGap = mOriginalWidthGap = a.getDimensionPixelSize(R.styleable.CellLayout_widthGap, 0);
196 mHeightGap = mOriginalHeightGap = a.getDimensionPixelSize(R.styleable.CellLayout_heightGap, 0);
Winson Chung4b825dcd2011-06-19 12:41:22 -0700197 mMaxGap = a.getDimensionPixelSize(R.styleable.CellLayout_maxGap, 0);
Adam Cohend22015c2010-07-26 22:02:18 -0700198 mCountX = LauncherModel.getCellCountX();
199 mCountY = LauncherModel.getCellCountY();
Michael Jurka0280c3b2010-09-17 15:00:07 -0700200 mOccupied = new boolean[mCountX][mCountY];
Adam Cohen482ed822012-03-02 14:15:13 -0800201 mTmpOccupied = new boolean[mCountX][mCountY];
Adam Cohen5b53f292012-03-29 14:30:35 -0700202 mPreviousReorderDirection[0] = INVALID_DIRECTION;
203 mPreviousReorderDirection[1] = INVALID_DIRECTION;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800204
205 a.recycle();
206
207 setAlwaysDrawnWithCacheEnabled(false);
208
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700209 final Resources res = getResources();
Adam Cohen307fe232012-08-16 17:55:58 -0700210 mHotseatScale = (res.getInteger(R.integer.hotseat_item_scale_percentage) / 100f);
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700211
Winson Chung967289b2011-06-30 18:09:30 -0700212 mNormalBackground = res.getDrawable(R.drawable.homescreen_blue_normal_holo);
Winson Chungdea74b72011-09-13 18:06:43 -0700213 mActiveGlowBackground = res.getDrawable(R.drawable.homescreen_blue_strong_holo);
Michael Jurka33945b22010-12-21 18:19:38 -0800214
Adam Cohenb5ba0972011-09-07 18:02:31 -0700215 mOverScrollLeft = res.getDrawable(R.drawable.overscroll_glow_left);
216 mOverScrollRight = res.getDrawable(R.drawable.overscroll_glow_right);
217 mForegroundPadding =
218 res.getDimensionPixelSize(R.dimen.workspace_overscroll_drawable_padding);
Michael Jurka33945b22010-12-21 18:19:38 -0800219
Adam Cohen19f37922012-03-21 11:59:11 -0700220 mReorderHintAnimationMagnitude = (REORDER_HINT_MAGNITUDE *
221 res.getDimensionPixelSize(R.dimen.app_icon_size));
222
Winson Chungb26f3d62011-06-02 10:49:29 -0700223 mNormalBackground.setFilterBitmap(true);
Winson Chungb26f3d62011-06-02 10:49:29 -0700224 mActiveGlowBackground.setFilterBitmap(true);
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700225
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700226 // Initialize the data structures used for the drag visualization.
Winson Chung150fbab2010-09-29 17:14:26 -0700227
Patrick Dubroyce34a972010-10-19 10:34:32 -0700228 mEaseOutInterpolator = new DecelerateInterpolator(2.5f); // Quint ease out
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700229
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700230
Winson Chungb8c69f32011-10-19 21:36:08 -0700231 mDragCell[0] = mDragCell[1] = -1;
Joe Onorato4be866d2010-10-10 11:26:02 -0700232 for (int i = 0; i < mDragOutlines.length; i++) {
Adam Cohend41fbf52012-02-16 23:53:59 -0800233 mDragOutlines[i] = new Rect(-1, -1, -1, -1);
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700234 }
235
236 // When dragging things around the home screens, we show a green outline of
237 // where the item will land. The outlines gradually fade out, leaving a trail
238 // behind the drag path.
239 // Set up all the animations that are used to implement this fading.
240 final int duration = res.getInteger(R.integer.config_dragOutlineFadeTime);
Chet Haase472b2812010-10-14 07:02:04 -0700241 final float fromAlphaValue = 0;
242 final float toAlphaValue = (float)res.getInteger(R.integer.config_dragOutlineMaxAlpha);
Joe Onorato4be866d2010-10-10 11:26:02 -0700243
Patrick Dubroy8e58e912010-10-14 13:21:48 -0700244 Arrays.fill(mDragOutlineAlphas, fromAlphaValue);
Joe Onorato4be866d2010-10-10 11:26:02 -0700245
246 for (int i = 0; i < mDragOutlineAnims.length; i++) {
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700247 final InterruptibleInOutAnimator anim =
248 new InterruptibleInOutAnimator(duration, fromAlphaValue, toAlphaValue);
Patrick Dubroyce34a972010-10-19 10:34:32 -0700249 anim.getAnimator().setInterpolator(mEaseOutInterpolator);
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700250 final int thisIndex = i;
Chet Haase472b2812010-10-14 07:02:04 -0700251 anim.getAnimator().addUpdateListener(new AnimatorUpdateListener() {
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700252 public void onAnimationUpdate(ValueAnimator animation) {
Joe Onorato4be866d2010-10-10 11:26:02 -0700253 final Bitmap outline = (Bitmap)anim.getTag();
254
255 // If an animation is started and then stopped very quickly, we can still
256 // get spurious updates we've cleared the tag. Guard against this.
257 if (outline == null) {
Michael Jurka3a9fced2012-04-13 14:44:29 -0700258 @SuppressWarnings("all") // suppress dead code warning
259 final boolean debug = false;
260 if (debug) {
Patrick Dubroyfe6bd872010-10-13 17:32:10 -0700261 Object val = animation.getAnimatedValue();
262 Log.d(TAG, "anim " + thisIndex + " update: " + val +
263 ", isStopped " + anim.isStopped());
264 }
Joe Onorato4be866d2010-10-10 11:26:02 -0700265 // Try to prevent it from continuing to run
266 animation.cancel();
267 } else {
Chet Haase472b2812010-10-14 07:02:04 -0700268 mDragOutlineAlphas[thisIndex] = (Float) animation.getAnimatedValue();
Adam Cohend41fbf52012-02-16 23:53:59 -0800269 CellLayout.this.invalidate(mDragOutlines[thisIndex]);
Joe Onorato4be866d2010-10-10 11:26:02 -0700270 }
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700271 }
272 });
Joe Onorato4be866d2010-10-10 11:26:02 -0700273 // The animation holds a reference to the drag outline bitmap as long is it's
274 // running. This way the bitmap can be GCed when the animations are complete.
Chet Haase472b2812010-10-14 07:02:04 -0700275 anim.getAnimator().addListener(new AnimatorListenerAdapter() {
Michael Jurka3c4c20f2010-10-28 15:36:06 -0700276 @Override
Joe Onorato4be866d2010-10-10 11:26:02 -0700277 public void onAnimationEnd(Animator animation) {
Chet Haase472b2812010-10-14 07:02:04 -0700278 if ((Float) ((ValueAnimator) animation).getAnimatedValue() == 0f) {
Joe Onorato4be866d2010-10-10 11:26:02 -0700279 anim.setTag(null);
280 }
281 }
282 });
283 mDragOutlineAnims[i] = anim;
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700284 }
Patrick Dubroyce34a972010-10-19 10:34:32 -0700285
Michael Jurka18014792010-10-14 09:01:34 -0700286 mBackgroundRect = new Rect();
Adam Cohenb5ba0972011-09-07 18:02:31 -0700287 mForegroundRect = new Rect();
Michael Jurkabea15192010-11-17 12:33:46 -0800288
Michael Jurkaa52570f2012-03-20 03:18:20 -0700289 mShortcutsAndWidgets = new ShortcutAndWidgetContainer(context);
290 mShortcutsAndWidgets.setCellDimensions(mCellWidth, mCellHeight, mWidthGap, mHeightGap);
291 addView(mShortcutsAndWidgets);
Michael Jurka18014792010-10-14 09:01:34 -0700292 }
293
Michael Jurkaf6440da2011-04-05 14:50:34 -0700294 static int widthInPortrait(Resources r, int numCells) {
295 // We use this method from Workspace to figure out how many rows/columns Launcher should
296 // have. We ignore the left/right padding on CellLayout because it turns out in our design
297 // the padding extends outside the visible screen size, but it looked fine anyway.
Michael Jurkaf6440da2011-04-05 14:50:34 -0700298 int cellWidth = r.getDimensionPixelSize(R.dimen.workspace_cell_width);
Winson Chung4b825dcd2011-06-19 12:41:22 -0700299 int minGap = Math.min(r.getDimensionPixelSize(R.dimen.workspace_width_gap),
300 r.getDimensionPixelSize(R.dimen.workspace_height_gap));
Michael Jurkaf6440da2011-04-05 14:50:34 -0700301
Winson Chung4b825dcd2011-06-19 12:41:22 -0700302 return minGap * (numCells - 1) + cellWidth * numCells;
Michael Jurkaf6440da2011-04-05 14:50:34 -0700303 }
304
Michael Jurkaf6440da2011-04-05 14:50:34 -0700305 static int heightInLandscape(Resources r, int numCells) {
306 // We use this method from Workspace to figure out how many rows/columns Launcher should
307 // have. We ignore the left/right padding on CellLayout because it turns out in our design
308 // the padding extends outside the visible screen size, but it looked fine anyway.
Michael Jurkaf6440da2011-04-05 14:50:34 -0700309 int cellHeight = r.getDimensionPixelSize(R.dimen.workspace_cell_height);
Winson Chung4b825dcd2011-06-19 12:41:22 -0700310 int minGap = Math.min(r.getDimensionPixelSize(R.dimen.workspace_width_gap),
311 r.getDimensionPixelSize(R.dimen.workspace_height_gap));
Michael Jurkaf6440da2011-04-05 14:50:34 -0700312
Winson Chung4b825dcd2011-06-19 12:41:22 -0700313 return minGap * (numCells - 1) + cellHeight * numCells;
Michael Jurkaf6440da2011-04-05 14:50:34 -0700314 }
315
Adam Cohen2801caf2011-05-13 20:57:39 -0700316 public void enableHardwareLayers() {
Michael Jurkaca993832012-06-29 15:17:04 -0700317 mShortcutsAndWidgets.setLayerType(LAYER_TYPE_HARDWARE, sPaint);
Michael Jurkad51f33a2012-06-28 15:35:26 -0700318 }
319
320 public void disableHardwareLayers() {
Michael Jurkaca993832012-06-29 15:17:04 -0700321 mShortcutsAndWidgets.setLayerType(LAYER_TYPE_NONE, sPaint);
Michael Jurkad51f33a2012-06-28 15:35:26 -0700322 }
323
324 public void buildHardwareLayer() {
325 mShortcutsAndWidgets.buildLayer();
Adam Cohen2801caf2011-05-13 20:57:39 -0700326 }
327
Adam Cohen307fe232012-08-16 17:55:58 -0700328 public float getChildrenScale() {
329 return mIsHotseat ? mHotseatScale : 1.0f;
330 }
331
Adam Cohen2801caf2011-05-13 20:57:39 -0700332 public void setGridSize(int x, int y) {
333 mCountX = x;
334 mCountY = y;
335 mOccupied = new boolean[mCountX][mCountY];
Adam Cohen482ed822012-03-02 14:15:13 -0800336 mTmpOccupied = new boolean[mCountX][mCountY];
Adam Cohen7fbec102012-03-27 12:42:19 -0700337 mTempRectStack.clear();
Adam Cohen76fc0852011-06-17 13:26:23 -0700338 requestLayout();
Adam Cohen2801caf2011-05-13 20:57:39 -0700339 }
340
Patrick Dubroy96864c32011-03-10 17:17:23 -0800341 private void invalidateBubbleTextView(BubbleTextView icon) {
342 final int padding = icon.getPressedOrFocusedBackgroundPadding();
Winson Chung4b825dcd2011-06-19 12:41:22 -0700343 invalidate(icon.getLeft() + getPaddingLeft() - padding,
344 icon.getTop() + getPaddingTop() - padding,
345 icon.getRight() + getPaddingLeft() + padding,
346 icon.getBottom() + getPaddingTop() + padding);
Patrick Dubroy96864c32011-03-10 17:17:23 -0800347 }
348
Adam Cohenb5ba0972011-09-07 18:02:31 -0700349 void setOverScrollAmount(float r, boolean left) {
350 if (left && mOverScrollForegroundDrawable != mOverScrollLeft) {
351 mOverScrollForegroundDrawable = mOverScrollLeft;
352 } else if (!left && mOverScrollForegroundDrawable != mOverScrollRight) {
353 mOverScrollForegroundDrawable = mOverScrollRight;
354 }
355
356 mForegroundAlpha = (int) Math.round((r * 255));
357 mOverScrollForegroundDrawable.setAlpha(mForegroundAlpha);
358 invalidate();
359 }
360
Patrick Dubroy96864c32011-03-10 17:17:23 -0800361 void setPressedOrFocusedIcon(BubbleTextView icon) {
362 // We draw the pressed or focused BubbleTextView's background in CellLayout because it
363 // requires an expanded clip rect (due to the glow's blur radius)
364 BubbleTextView oldIcon = mPressedOrFocusedIcon;
365 mPressedOrFocusedIcon = icon;
366 if (oldIcon != null) {
367 invalidateBubbleTextView(oldIcon);
368 }
369 if (mPressedOrFocusedIcon != null) {
370 invalidateBubbleTextView(mPressedOrFocusedIcon);
371 }
372 }
373
Michael Jurka33945b22010-12-21 18:19:38 -0800374 void setIsDragOverlapping(boolean isDragOverlapping) {
375 if (mIsDragOverlapping != isDragOverlapping) {
376 mIsDragOverlapping = isDragOverlapping;
377 invalidate();
378 }
379 }
380
381 boolean getIsDragOverlapping() {
382 return mIsDragOverlapping;
383 }
384
Adam Cohenebea84d2011-11-09 17:20:41 -0800385 protected void setOverscrollTransformsDirty(boolean dirty) {
386 mScrollingTransformsDirty = dirty;
387 }
388
389 protected void resetOverscrollTransforms() {
390 if (mScrollingTransformsDirty) {
391 setOverscrollTransformsDirty(false);
392 setTranslationX(0);
393 setRotationY(0);
394 // It doesn't matter if we pass true or false here, the important thing is that we
395 // pass 0, which results in the overscroll drawable not being drawn any more.
396 setOverScrollAmount(0, false);
397 setPivotX(getMeasuredWidth() / 2);
398 setPivotY(getMeasuredHeight() / 2);
399 }
400 }
401
Adam Cohen307fe232012-08-16 17:55:58 -0700402 public void scaleRect(Rect r, float scale) {
403 if (scale != 1.0f) {
404 r.left = (int) (r.left * scale + 0.5f);
405 r.top = (int) (r.top * scale + 0.5f);
406 r.right = (int) (r.right * scale + 0.5f);
407 r.bottom = (int) (r.bottom * scale + 0.5f);
408 }
409 }
410
411 Rect temp = new Rect();
412 void scaleRectAboutCenter(Rect in, Rect out, float scale) {
413 int cx = in.centerX();
414 int cy = in.centerY();
415 out.set(in);
416 out.offset(-cx, -cy);
417 scaleRect(out, scale);
418 out.offset(cx, cy);
419 }
420
Jeff Sharkey83f111d2009-04-20 21:03:13 -0700421 @Override
Patrick Dubroy1262e362010-10-06 15:49:50 -0700422 protected void onDraw(Canvas canvas) {
Michael Jurka3e7c7632010-10-02 16:01:03 -0700423 // When we're large, we are either drawn in a "hover" state (ie when dragging an item to
424 // a neighboring page) or with just a normal background (if backgroundAlpha > 0.0f)
425 // When we're small, we are either drawn normally or in the "accepts drops" state (during
426 // a drag). However, we also drag the mini hover background *over* one of those two
427 // backgrounds
Winson Chungb26f3d62011-06-02 10:49:29 -0700428 if (mBackgroundAlpha > 0.0f) {
Adam Cohenf34bab52010-09-30 14:11:56 -0700429 Drawable bg;
Michael Jurka33945b22010-12-21 18:19:38 -0800430
431 if (mIsDragOverlapping) {
432 // In the mini case, we draw the active_glow bg *over* the active background
Michael Jurkabdf78552011-10-31 14:34:25 -0700433 bg = mActiveGlowBackground;
Adam Cohenf34bab52010-09-30 14:11:56 -0700434 } else {
Michael Jurkabdf78552011-10-31 14:34:25 -0700435 bg = mNormalBackground;
Adam Cohenf34bab52010-09-30 14:11:56 -0700436 }
Michael Jurka33945b22010-12-21 18:19:38 -0800437
438 bg.setAlpha((int) (mBackgroundAlpha * mBackgroundAlphaMultiplier * 255));
439 bg.setBounds(mBackgroundRect);
440 bg.draw(canvas);
Michael Jurkaa63c4522010-08-19 13:52:27 -0700441 }
Romain Guya6abce82009-11-10 02:54:41 -0800442
Patrick Dubroy8e58e912010-10-14 13:21:48 -0700443 final Paint paint = mDragOutlinePaint;
Joe Onorato4be866d2010-10-10 11:26:02 -0700444 for (int i = 0; i < mDragOutlines.length; i++) {
Chet Haase472b2812010-10-14 07:02:04 -0700445 final float alpha = mDragOutlineAlphas[i];
Joe Onorato4be866d2010-10-10 11:26:02 -0700446 if (alpha > 0) {
Adam Cohend41fbf52012-02-16 23:53:59 -0800447 final Rect r = mDragOutlines[i];
Adam Cohen307fe232012-08-16 17:55:58 -0700448 scaleRectAboutCenter(r, temp, getChildrenScale());
Joe Onorato4be866d2010-10-10 11:26:02 -0700449 final Bitmap b = (Bitmap) mDragOutlineAnims[i].getTag();
Chet Haase472b2812010-10-14 07:02:04 -0700450 paint.setAlpha((int)(alpha + .5f));
Adam Cohen307fe232012-08-16 17:55:58 -0700451 canvas.drawBitmap(b, null, temp, paint);
Winson Chung150fbab2010-09-29 17:14:26 -0700452 }
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700453 }
Patrick Dubroy96864c32011-03-10 17:17:23 -0800454
455 // We draw the pressed or focused BubbleTextView's background in CellLayout because it
456 // requires an expanded clip rect (due to the glow's blur radius)
457 if (mPressedOrFocusedIcon != null) {
458 final int padding = mPressedOrFocusedIcon.getPressedOrFocusedBackgroundPadding();
459 final Bitmap b = mPressedOrFocusedIcon.getPressedOrFocusedBackground();
460 if (b != null) {
461 canvas.drawBitmap(b,
Winson Chung4b825dcd2011-06-19 12:41:22 -0700462 mPressedOrFocusedIcon.getLeft() + getPaddingLeft() - padding,
463 mPressedOrFocusedIcon.getTop() + getPaddingTop() - padding,
Patrick Dubroy96864c32011-03-10 17:17:23 -0800464 null);
465 }
466 }
Adam Cohen69ce2e52011-07-03 19:25:21 -0700467
Adam Cohen482ed822012-03-02 14:15:13 -0800468 if (DEBUG_VISUALIZE_OCCUPIED) {
469 int[] pt = new int[2];
470 ColorDrawable cd = new ColorDrawable(Color.RED);
Adam Cohene7587d22012-05-24 18:50:02 -0700471 cd.setBounds(0, 0, mCellWidth, mCellHeight);
Adam Cohen482ed822012-03-02 14:15:13 -0800472 for (int i = 0; i < mCountX; i++) {
473 for (int j = 0; j < mCountY; j++) {
474 if (mOccupied[i][j]) {
475 cellToPoint(i, j, pt);
476 canvas.save();
477 canvas.translate(pt[0], pt[1]);
478 cd.draw(canvas);
479 canvas.restore();
480 }
481 }
482 }
483 }
484
Andrew Flynn850d2e72012-04-26 16:51:20 -0700485 int previewOffset = FolderRingAnimator.sPreviewSize;
486
Adam Cohen69ce2e52011-07-03 19:25:21 -0700487 // The folder outer / inner ring image(s)
488 for (int i = 0; i < mFolderOuterRings.size(); i++) {
489 FolderRingAnimator fra = mFolderOuterRings.get(i);
490
491 // Draw outer ring
492 Drawable d = FolderRingAnimator.sSharedOuterRingDrawable;
493 int width = (int) fra.getOuterRingSize();
494 int height = width;
495 cellToPoint(fra.mCellX, fra.mCellY, mTempLocation);
496
497 int centerX = mTempLocation[0] + mCellWidth / 2;
Andrew Flynn850d2e72012-04-26 16:51:20 -0700498 int centerY = mTempLocation[1] + previewOffset / 2;
Adam Cohen69ce2e52011-07-03 19:25:21 -0700499
500 canvas.save();
501 canvas.translate(centerX - width / 2, centerY - height / 2);
502 d.setBounds(0, 0, width, height);
503 d.draw(canvas);
504 canvas.restore();
505
506 // Draw inner ring
507 d = FolderRingAnimator.sSharedInnerRingDrawable;
508 width = (int) fra.getInnerRingSize();
509 height = width;
510 cellToPoint(fra.mCellX, fra.mCellY, mTempLocation);
511
512 centerX = mTempLocation[0] + mCellWidth / 2;
Andrew Flynn850d2e72012-04-26 16:51:20 -0700513 centerY = mTempLocation[1] + previewOffset / 2;
Adam Cohen69ce2e52011-07-03 19:25:21 -0700514 canvas.save();
515 canvas.translate(centerX - width / 2, centerY - width / 2);
516 d.setBounds(0, 0, width, height);
517 d.draw(canvas);
518 canvas.restore();
519 }
Adam Cohenc51934b2011-07-26 21:07:43 -0700520
521 if (mFolderLeaveBehindCell[0] >= 0 && mFolderLeaveBehindCell[1] >= 0) {
522 Drawable d = FolderIcon.sSharedFolderLeaveBehind;
523 int width = d.getIntrinsicWidth();
524 int height = d.getIntrinsicHeight();
525
526 cellToPoint(mFolderLeaveBehindCell[0], mFolderLeaveBehindCell[1], mTempLocation);
527 int centerX = mTempLocation[0] + mCellWidth / 2;
Andrew Flynn850d2e72012-04-26 16:51:20 -0700528 int centerY = mTempLocation[1] + previewOffset / 2;
Adam Cohenc51934b2011-07-26 21:07:43 -0700529
530 canvas.save();
531 canvas.translate(centerX - width / 2, centerY - width / 2);
532 d.setBounds(0, 0, width, height);
533 d.draw(canvas);
534 canvas.restore();
535 }
Adam Cohen69ce2e52011-07-03 19:25:21 -0700536 }
537
Adam Cohenb5ba0972011-09-07 18:02:31 -0700538 @Override
539 protected void dispatchDraw(Canvas canvas) {
540 super.dispatchDraw(canvas);
541 if (mForegroundAlpha > 0) {
542 mOverScrollForegroundDrawable.setBounds(mForegroundRect);
543 Paint p = ((NinePatchDrawable) mOverScrollForegroundDrawable).getPaint();
Romain Guy8a0bff52012-05-06 13:14:33 -0700544 p.setXfermode(sAddBlendMode);
Adam Cohenb5ba0972011-09-07 18:02:31 -0700545 mOverScrollForegroundDrawable.draw(canvas);
546 p.setXfermode(null);
547 }
548 }
549
Adam Cohen69ce2e52011-07-03 19:25:21 -0700550 public void showFolderAccept(FolderRingAnimator fra) {
551 mFolderOuterRings.add(fra);
552 }
553
554 public void hideFolderAccept(FolderRingAnimator fra) {
555 if (mFolderOuterRings.contains(fra)) {
556 mFolderOuterRings.remove(fra);
557 }
558 invalidate();
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700559 }
560
Adam Cohenc51934b2011-07-26 21:07:43 -0700561 public void setFolderLeaveBehindCell(int x, int y) {
562 mFolderLeaveBehindCell[0] = x;
563 mFolderLeaveBehindCell[1] = y;
564 invalidate();
565 }
566
567 public void clearFolderLeaveBehind() {
568 mFolderLeaveBehindCell[0] = -1;
569 mFolderLeaveBehindCell[1] = -1;
570 invalidate();
571 }
572
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700573 @Override
Michael Jurkae6235dd2011-10-04 15:02:05 -0700574 public boolean shouldDelayChildPressedState() {
575 return false;
576 }
577
Adam Cohen1462de32012-07-24 22:34:36 -0700578 public void restoreInstanceState(SparseArray<Parcelable> states) {
579 dispatchRestoreInstanceState(states);
580 }
581
Michael Jurkae6235dd2011-10-04 15:02:05 -0700582 @Override
Jeff Sharkey83f111d2009-04-20 21:03:13 -0700583 public void cancelLongPress() {
584 super.cancelLongPress();
585
586 // Cancel long press for all children
587 final int count = getChildCount();
588 for (int i = 0; i < count; i++) {
589 final View child = getChildAt(i);
590 child.cancelLongPress();
591 }
592 }
593
Michael Jurkadee05892010-07-27 10:01:56 -0700594 public void setOnInterceptTouchListener(View.OnTouchListener listener) {
595 mInterceptTouchListener = listener;
596 }
597
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800598 int getCountX() {
Adam Cohend22015c2010-07-26 22:02:18 -0700599 return mCountX;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800600 }
601
602 int getCountY() {
Adam Cohend22015c2010-07-26 22:02:18 -0700603 return mCountY;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800604 }
605
Andrew Flynn0dca1ec2012-02-29 13:33:22 -0800606 public void setIsHotseat(boolean isHotseat) {
607 mIsHotseat = isHotseat;
608 }
609
Andrew Flynn0dca1ec2012-02-29 13:33:22 -0800610 public boolean addViewToCellLayout(View child, int index, int childId, LayoutParams params,
Andrew Flynn850d2e72012-04-26 16:51:20 -0700611 boolean markCells) {
Winson Chungaafa03c2010-06-11 17:34:16 -0700612 final LayoutParams lp = params;
613
Andrew Flynnde38e422012-05-08 11:22:15 -0700614 // Hotseat icons - remove text
Andrew Flynn0dca1ec2012-02-29 13:33:22 -0800615 if (child instanceof BubbleTextView) {
616 BubbleTextView bubbleChild = (BubbleTextView) child;
617
Andrew Flynnde38e422012-05-08 11:22:15 -0700618 Resources res = getResources();
619 if (mIsHotseat) {
620 bubbleChild.setTextColor(res.getColor(android.R.color.transparent));
621 } else {
622 bubbleChild.setTextColor(res.getColor(R.color.workspace_icon_text_color));
Andrew Flynn0dca1ec2012-02-29 13:33:22 -0800623 }
624 }
625
Adam Cohen307fe232012-08-16 17:55:58 -0700626 child.setScaleX(getChildrenScale());
627 child.setScaleY(getChildrenScale());
628
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800629 // Generate an id for each view, this assumes we have at most 256x256 cells
630 // per workspace screen
Adam Cohend22015c2010-07-26 22:02:18 -0700631 if (lp.cellX >= 0 && lp.cellX <= mCountX - 1 && lp.cellY >= 0 && lp.cellY <= mCountY - 1) {
Winson Chungaafa03c2010-06-11 17:34:16 -0700632 // If the horizontal or vertical span is set to -1, it is taken to
633 // mean that it spans the extent of the CellLayout
Adam Cohend22015c2010-07-26 22:02:18 -0700634 if (lp.cellHSpan < 0) lp.cellHSpan = mCountX;
635 if (lp.cellVSpan < 0) lp.cellVSpan = mCountY;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800636
Winson Chungaafa03c2010-06-11 17:34:16 -0700637 child.setId(childId);
638
Michael Jurkaa52570f2012-03-20 03:18:20 -0700639 mShortcutsAndWidgets.addView(child, index, lp);
Michael Jurkadee05892010-07-27 10:01:56 -0700640
Michael Jurkaf3ca3ab2010-10-20 17:08:24 -0700641 if (markCells) markCellsAsOccupiedForView(child);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700642
Winson Chungaafa03c2010-06-11 17:34:16 -0700643 return true;
644 }
645 return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800646 }
Michael Jurka3e7c7632010-10-02 16:01:03 -0700647
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800648 @Override
Michael Jurka0280c3b2010-09-17 15:00:07 -0700649 public void removeAllViews() {
650 clearOccupiedCells();
Michael Jurkaa52570f2012-03-20 03:18:20 -0700651 mShortcutsAndWidgets.removeAllViews();
Michael Jurka0280c3b2010-09-17 15:00:07 -0700652 }
653
654 @Override
655 public void removeAllViewsInLayout() {
Michael Jurkaa52570f2012-03-20 03:18:20 -0700656 if (mShortcutsAndWidgets.getChildCount() > 0) {
Michael Jurka7cfc2822011-08-02 20:19:24 -0700657 clearOccupiedCells();
Michael Jurkaa52570f2012-03-20 03:18:20 -0700658 mShortcutsAndWidgets.removeAllViewsInLayout();
Michael Jurka7cfc2822011-08-02 20:19:24 -0700659 }
Michael Jurka0280c3b2010-09-17 15:00:07 -0700660 }
661
Michael Jurkaf3ca3ab2010-10-20 17:08:24 -0700662 public void removeViewWithoutMarkingCells(View view) {
Michael Jurkaa52570f2012-03-20 03:18:20 -0700663 mShortcutsAndWidgets.removeView(view);
Michael Jurkaf3ca3ab2010-10-20 17:08:24 -0700664 }
665
Michael Jurka0280c3b2010-09-17 15:00:07 -0700666 @Override
667 public void removeView(View view) {
668 markCellsAsUnoccupiedForView(view);
Michael Jurkaa52570f2012-03-20 03:18:20 -0700669 mShortcutsAndWidgets.removeView(view);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700670 }
671
672 @Override
673 public void removeViewAt(int index) {
Michael Jurkaa52570f2012-03-20 03:18:20 -0700674 markCellsAsUnoccupiedForView(mShortcutsAndWidgets.getChildAt(index));
675 mShortcutsAndWidgets.removeViewAt(index);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700676 }
677
678 @Override
679 public void removeViewInLayout(View view) {
680 markCellsAsUnoccupiedForView(view);
Michael Jurkaa52570f2012-03-20 03:18:20 -0700681 mShortcutsAndWidgets.removeViewInLayout(view);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700682 }
683
684 @Override
685 public void removeViews(int start, int count) {
686 for (int i = start; i < start + count; i++) {
Michael Jurkaa52570f2012-03-20 03:18:20 -0700687 markCellsAsUnoccupiedForView(mShortcutsAndWidgets.getChildAt(i));
Michael Jurka0280c3b2010-09-17 15:00:07 -0700688 }
Michael Jurkaa52570f2012-03-20 03:18:20 -0700689 mShortcutsAndWidgets.removeViews(start, count);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700690 }
691
692 @Override
693 public void removeViewsInLayout(int start, int count) {
694 for (int i = start; i < start + count; i++) {
Michael Jurkaa52570f2012-03-20 03:18:20 -0700695 markCellsAsUnoccupiedForView(mShortcutsAndWidgets.getChildAt(i));
Michael Jurka0280c3b2010-09-17 15:00:07 -0700696 }
Michael Jurkaa52570f2012-03-20 03:18:20 -0700697 mShortcutsAndWidgets.removeViewsInLayout(start, count);
Michael Jurkaabded662011-03-04 12:06:57 -0800698 }
699
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800700 @Override
701 protected void onAttachedToWindow() {
702 super.onAttachedToWindow();
703 mCellInfo.screen = ((ViewGroup) getParent()).indexOfChild(this);
704 }
705
Michael Jurkaaf442092010-06-10 17:01:57 -0700706 public void setTagToCellInfoForPoint(int touchX, int touchY) {
707 final CellInfo cellInfo = mCellInfo;
Winson Chungeecf02d2012-03-02 17:14:58 -0800708 Rect frame = mRect;
Michael Jurka8b805b12012-04-18 14:23:14 -0700709 final int x = touchX + getScrollX();
710 final int y = touchY + getScrollY();
Michael Jurkaa52570f2012-03-20 03:18:20 -0700711 final int count = mShortcutsAndWidgets.getChildCount();
Michael Jurkaaf442092010-06-10 17:01:57 -0700712
713 boolean found = false;
714 for (int i = count - 1; i >= 0; i--) {
Michael Jurkaa52570f2012-03-20 03:18:20 -0700715 final View child = mShortcutsAndWidgets.getChildAt(i);
Adam Cohend4844c32011-02-18 19:25:06 -0800716 final LayoutParams lp = (LayoutParams) child.getLayoutParams();
Michael Jurkaaf442092010-06-10 17:01:57 -0700717
Adam Cohen1b607ed2011-03-03 17:26:50 -0800718 if ((child.getVisibility() == VISIBLE || child.getAnimation() != null) &&
719 lp.isLockedToGrid) {
Michael Jurkaaf442092010-06-10 17:01:57 -0700720 child.getHitRect(frame);
Winson Chung0be025d2011-05-23 17:45:09 -0700721
Winson Chungeecf02d2012-03-02 17:14:58 -0800722 float scale = child.getScaleX();
723 frame = new Rect(child.getLeft(), child.getTop(), child.getRight(),
724 child.getBottom());
Winson Chung0be025d2011-05-23 17:45:09 -0700725 // The child hit rect is relative to the CellLayoutChildren parent, so we need to
726 // offset that by this CellLayout's padding to test an (x,y) point that is relative
727 // to this view.
Michael Jurka8b805b12012-04-18 14:23:14 -0700728 frame.offset(getPaddingLeft(), getPaddingTop());
Winson Chungeecf02d2012-03-02 17:14:58 -0800729 frame.inset((int) (frame.width() * (1f - scale) / 2),
730 (int) (frame.height() * (1f - scale) / 2));
Winson Chung0be025d2011-05-23 17:45:09 -0700731
Michael Jurkaaf442092010-06-10 17:01:57 -0700732 if (frame.contains(x, y)) {
Michael Jurkaaf442092010-06-10 17:01:57 -0700733 cellInfo.cell = child;
734 cellInfo.cellX = lp.cellX;
735 cellInfo.cellY = lp.cellY;
736 cellInfo.spanX = lp.cellHSpan;
737 cellInfo.spanY = lp.cellVSpan;
Michael Jurkaaf442092010-06-10 17:01:57 -0700738 found = true;
Michael Jurkaaf442092010-06-10 17:01:57 -0700739 break;
740 }
741 }
742 }
Winson Chungaafa03c2010-06-11 17:34:16 -0700743
Michael Jurkad771c962011-08-09 15:00:48 -0700744 mLastDownOnOccupiedCell = found;
745
Michael Jurkaaf442092010-06-10 17:01:57 -0700746 if (!found) {
Winson Chung0be025d2011-05-23 17:45:09 -0700747 final int cellXY[] = mTmpXY;
Michael Jurkaaf442092010-06-10 17:01:57 -0700748 pointToCellExact(x, y, cellXY);
749
Michael Jurkaaf442092010-06-10 17:01:57 -0700750 cellInfo.cell = null;
751 cellInfo.cellX = cellXY[0];
752 cellInfo.cellY = cellXY[1];
753 cellInfo.spanX = 1;
754 cellInfo.spanY = 1;
Michael Jurkaaf442092010-06-10 17:01:57 -0700755 }
756 setTag(cellInfo);
757 }
758
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800759 @Override
760 public boolean onInterceptTouchEvent(MotionEvent ev) {
Adam Cohenc1997fd2011-08-15 18:26:39 -0700761 // First we clear the tag to ensure that on every touch down we start with a fresh slate,
762 // even in the case where we return early. Not clearing here was causing bugs whereby on
763 // long-press we'd end up picking up an item from a previous drag operation.
764 final int action = ev.getAction();
765
766 if (action == MotionEvent.ACTION_DOWN) {
767 clearTagCellInfo();
768 }
769
Michael Jurkadee05892010-07-27 10:01:56 -0700770 if (mInterceptTouchListener != null && mInterceptTouchListener.onTouch(this, ev)) {
771 return true;
772 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800773
774 if (action == MotionEvent.ACTION_DOWN) {
Michael Jurkaaf442092010-06-10 17:01:57 -0700775 setTagToCellInfoForPoint((int) ev.getX(), (int) ev.getY());
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800776 }
Winson Chungeecf02d2012-03-02 17:14:58 -0800777
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800778 return false;
779 }
780
Adam Cohenc1997fd2011-08-15 18:26:39 -0700781 private void clearTagCellInfo() {
782 final CellInfo cellInfo = mCellInfo;
783 cellInfo.cell = null;
784 cellInfo.cellX = -1;
785 cellInfo.cellY = -1;
786 cellInfo.spanX = 0;
787 cellInfo.spanY = 0;
788 setTag(cellInfo);
789 }
790
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800791 public CellInfo getTag() {
Michael Jurka0280c3b2010-09-17 15:00:07 -0700792 return (CellInfo) super.getTag();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800793 }
794
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700795 /**
Winson Chungaafa03c2010-06-11 17:34:16 -0700796 * Given a point, return the cell that strictly encloses that point
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800797 * @param x X coordinate of the point
798 * @param y Y coordinate of the point
799 * @param result Array of 2 ints to hold the x and y coordinate of the cell
800 */
801 void pointToCellExact(int x, int y, int[] result) {
Winson Chung4b825dcd2011-06-19 12:41:22 -0700802 final int hStartPadding = getPaddingLeft();
803 final int vStartPadding = getPaddingTop();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800804
805 result[0] = (x - hStartPadding) / (mCellWidth + mWidthGap);
806 result[1] = (y - vStartPadding) / (mCellHeight + mHeightGap);
807
Adam Cohend22015c2010-07-26 22:02:18 -0700808 final int xAxis = mCountX;
809 final int yAxis = mCountY;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800810
811 if (result[0] < 0) result[0] = 0;
812 if (result[0] >= xAxis) result[0] = xAxis - 1;
813 if (result[1] < 0) result[1] = 0;
814 if (result[1] >= yAxis) result[1] = yAxis - 1;
815 }
Winson Chungaafa03c2010-06-11 17:34:16 -0700816
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800817 /**
818 * Given a point, return the cell that most closely encloses that point
819 * @param x X coordinate of the point
820 * @param y Y coordinate of the point
821 * @param result Array of 2 ints to hold the x and y coordinate of the cell
822 */
823 void pointToCellRounded(int x, int y, int[] result) {
824 pointToCellExact(x + (mCellWidth / 2), y + (mCellHeight / 2), result);
825 }
826
827 /**
828 * Given a cell coordinate, return the point that represents the upper left corner of that cell
Winson Chungaafa03c2010-06-11 17:34:16 -0700829 *
830 * @param cellX X coordinate of the cell
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800831 * @param cellY Y coordinate of the cell
Winson Chungaafa03c2010-06-11 17:34:16 -0700832 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800833 * @param result Array of 2 ints to hold the x and y coordinate of the point
834 */
835 void cellToPoint(int cellX, int cellY, int[] result) {
Winson Chung4b825dcd2011-06-19 12:41:22 -0700836 final int hStartPadding = getPaddingLeft();
837 final int vStartPadding = getPaddingTop();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800838
839 result[0] = hStartPadding + cellX * (mCellWidth + mWidthGap);
840 result[1] = vStartPadding + cellY * (mCellHeight + mHeightGap);
841 }
842
Adam Cohene3e27a82011-04-15 12:07:39 -0700843 /**
Adam Cohen482ed822012-03-02 14:15:13 -0800844 * Given a cell coordinate, return the point that represents the center of the cell
Adam Cohene3e27a82011-04-15 12:07:39 -0700845 *
846 * @param cellX X coordinate of the cell
847 * @param cellY Y coordinate of the cell
848 *
849 * @param result Array of 2 ints to hold the x and y coordinate of the point
850 */
851 void cellToCenterPoint(int cellX, int cellY, int[] result) {
Adam Cohen47a876d2012-03-19 13:21:41 -0700852 regionToCenterPoint(cellX, cellY, 1, 1, result);
853 }
854
855 /**
856 * Given a cell coordinate and span return the point that represents the center of the regio
857 *
858 * @param cellX X coordinate of the cell
859 * @param cellY Y coordinate of the cell
860 *
861 * @param result Array of 2 ints to hold the x and y coordinate of the point
862 */
863 void regionToCenterPoint(int cellX, int cellY, int spanX, int spanY, int[] result) {
Winson Chung4b825dcd2011-06-19 12:41:22 -0700864 final int hStartPadding = getPaddingLeft();
865 final int vStartPadding = getPaddingTop();
Adam Cohen47a876d2012-03-19 13:21:41 -0700866 result[0] = hStartPadding + cellX * (mCellWidth + mWidthGap) +
867 (spanX * mCellWidth + (spanX - 1) * mWidthGap) / 2;
868 result[1] = vStartPadding + cellY * (mCellHeight + mHeightGap) +
869 (spanY * mCellHeight + (spanY - 1) * mHeightGap) / 2;
Adam Cohene3e27a82011-04-15 12:07:39 -0700870 }
871
Adam Cohen19f37922012-03-21 11:59:11 -0700872 /**
873 * Given a cell coordinate and span fills out a corresponding pixel rect
874 *
875 * @param cellX X coordinate of the cell
876 * @param cellY Y coordinate of the cell
877 * @param result Rect in which to write the result
878 */
879 void regionToRect(int cellX, int cellY, int spanX, int spanY, Rect result) {
880 final int hStartPadding = getPaddingLeft();
881 final int vStartPadding = getPaddingTop();
882 final int left = hStartPadding + cellX * (mCellWidth + mWidthGap);
883 final int top = vStartPadding + cellY * (mCellHeight + mHeightGap);
884 result.set(left, top, left + (spanX * mCellWidth + (spanX - 1) * mWidthGap),
885 top + (spanY * mCellHeight + (spanY - 1) * mHeightGap));
886 }
887
Adam Cohen482ed822012-03-02 14:15:13 -0800888 public float getDistanceFromCell(float x, float y, int[] cell) {
889 cellToCenterPoint(cell[0], cell[1], mTmpPoint);
890 float distance = (float) Math.sqrt( Math.pow(x - mTmpPoint[0], 2) +
891 Math.pow(y - mTmpPoint[1], 2));
892 return distance;
893 }
894
Romain Guy84f296c2009-11-04 15:00:44 -0800895 int getCellWidth() {
896 return mCellWidth;
897 }
898
899 int getCellHeight() {
900 return mCellHeight;
901 }
902
Adam Cohend4844c32011-02-18 19:25:06 -0800903 int getWidthGap() {
904 return mWidthGap;
905 }
906
907 int getHeightGap() {
908 return mHeightGap;
909 }
910
Adam Cohen7f4eabe2011-04-21 16:19:16 -0700911 Rect getContentRect(Rect r) {
912 if (r == null) {
913 r = new Rect();
914 }
915 int left = getPaddingLeft();
916 int top = getPaddingTop();
Michael Jurka8b805b12012-04-18 14:23:14 -0700917 int right = left + getWidth() - getPaddingLeft() - getPaddingRight();
918 int bottom = top + getHeight() - getPaddingTop() - getPaddingBottom();
Adam Cohen7f4eabe2011-04-21 16:19:16 -0700919 r.set(left, top, right, bottom);
920 return r;
921 }
922
Adam Cohena897f392012-04-27 18:12:05 -0700923 static void getMetrics(Rect metrics, Resources res, int measureWidth, int measureHeight,
924 int countX, int countY, int orientation) {
925 int numWidthGaps = countX - 1;
926 int numHeightGaps = countY - 1;
Adam Cohenf4bd5792012-04-27 11:35:29 -0700927
928 int widthGap;
929 int heightGap;
930 int cellWidth;
931 int cellHeight;
932 int paddingLeft;
933 int paddingRight;
934 int paddingTop;
935 int paddingBottom;
936
Adam Cohena897f392012-04-27 18:12:05 -0700937 int maxGap = res.getDimensionPixelSize(R.dimen.workspace_max_gap);
Adam Cohenf4bd5792012-04-27 11:35:29 -0700938 if (orientation == LANDSCAPE) {
939 cellWidth = res.getDimensionPixelSize(R.dimen.workspace_cell_width_land);
940 cellHeight = res.getDimensionPixelSize(R.dimen.workspace_cell_height_land);
941 widthGap = res.getDimensionPixelSize(R.dimen.workspace_width_gap_land);
942 heightGap = res.getDimensionPixelSize(R.dimen.workspace_height_gap_land);
943 paddingLeft = res.getDimensionPixelSize(R.dimen.cell_layout_left_padding_land);
944 paddingRight = res.getDimensionPixelSize(R.dimen.cell_layout_right_padding_land);
945 paddingTop = res.getDimensionPixelSize(R.dimen.cell_layout_top_padding_land);
946 paddingBottom = res.getDimensionPixelSize(R.dimen.cell_layout_bottom_padding_land);
947 } else {
948 // PORTRAIT
949 cellWidth = res.getDimensionPixelSize(R.dimen.workspace_cell_width_port);
950 cellHeight = res.getDimensionPixelSize(R.dimen.workspace_cell_height_port);
951 widthGap = res.getDimensionPixelSize(R.dimen.workspace_width_gap_port);
952 heightGap = res.getDimensionPixelSize(R.dimen.workspace_height_gap_port);
953 paddingLeft = res.getDimensionPixelSize(R.dimen.cell_layout_left_padding_port);
954 paddingRight = res.getDimensionPixelSize(R.dimen.cell_layout_right_padding_port);
955 paddingTop = res.getDimensionPixelSize(R.dimen.cell_layout_top_padding_port);
956 paddingBottom = res.getDimensionPixelSize(R.dimen.cell_layout_bottom_padding_port);
957 }
958
959 if (widthGap < 0 || heightGap < 0) {
960 int hSpace = measureWidth - paddingLeft - paddingRight;
961 int vSpace = measureHeight - paddingTop - paddingBottom;
Adam Cohena897f392012-04-27 18:12:05 -0700962 int hFreeSpace = hSpace - (countX * cellWidth);
963 int vFreeSpace = vSpace - (countY * cellHeight);
964 widthGap = Math.min(maxGap, numWidthGaps > 0 ? (hFreeSpace / numWidthGaps) : 0);
965 heightGap = Math.min(maxGap, numHeightGaps > 0 ? (vFreeSpace / numHeightGaps) : 0);
Adam Cohenf4bd5792012-04-27 11:35:29 -0700966 }
967 metrics.set(cellWidth, cellHeight, widthGap, heightGap);
968 }
969
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800970 @Override
971 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800972 int widthSpecMode = MeasureSpec.getMode(widthMeasureSpec);
Winson Chungaafa03c2010-06-11 17:34:16 -0700973 int widthSpecSize = MeasureSpec.getSize(widthMeasureSpec);
974
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800975 int heightSpecMode = MeasureSpec.getMode(heightMeasureSpec);
976 int heightSpecSize = MeasureSpec.getSize(heightMeasureSpec);
Winson Chungaafa03c2010-06-11 17:34:16 -0700977
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800978 if (widthSpecMode == MeasureSpec.UNSPECIFIED || heightSpecMode == MeasureSpec.UNSPECIFIED) {
979 throw new RuntimeException("CellLayout cannot have UNSPECIFIED dimensions");
980 }
981
Adam Cohend22015c2010-07-26 22:02:18 -0700982 int numWidthGaps = mCountX - 1;
983 int numHeightGaps = mCountY - 1;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800984
Adam Cohen234c4cd2011-07-17 21:03:04 -0700985 if (mOriginalWidthGap < 0 || mOriginalHeightGap < 0) {
Michael Jurkadd13e3d2012-05-01 12:38:17 -0700986 int hSpace = widthSpecSize - getPaddingLeft() - getPaddingRight();
987 int vSpace = heightSpecSize - getPaddingTop() - getPaddingBottom();
Adam Cohenf4bd5792012-04-27 11:35:29 -0700988 int hFreeSpace = hSpace - (mCountX * mCellWidth);
989 int vFreeSpace = vSpace - (mCountY * mCellHeight);
Winson Chung4b825dcd2011-06-19 12:41:22 -0700990 mWidthGap = Math.min(mMaxGap, numWidthGaps > 0 ? (hFreeSpace / numWidthGaps) : 0);
991 mHeightGap = Math.min(mMaxGap,numHeightGaps > 0 ? (vFreeSpace / numHeightGaps) : 0);
Michael Jurkaa52570f2012-03-20 03:18:20 -0700992 mShortcutsAndWidgets.setCellDimensions(mCellWidth, mCellHeight, mWidthGap, mHeightGap);
Adam Cohen234c4cd2011-07-17 21:03:04 -0700993 } else {
994 mWidthGap = mOriginalWidthGap;
995 mHeightGap = mOriginalHeightGap;
Winson Chungece7f5b2010-10-22 14:54:12 -0700996 }
Michael Jurka5f1c5092010-09-03 14:15:02 -0700997
Michael Jurka8c920dd2011-01-20 14:16:56 -0800998 // Initial values correspond to widthSpecMode == MeasureSpec.EXACTLY
999 int newWidth = widthSpecSize;
1000 int newHeight = heightSpecSize;
Michael Jurka5f1c5092010-09-03 14:15:02 -07001001 if (widthSpecMode == MeasureSpec.AT_MOST) {
Michael Jurka8b805b12012-04-18 14:23:14 -07001002 newWidth = getPaddingLeft() + getPaddingRight() + (mCountX * mCellWidth) +
Winson Chungece7f5b2010-10-22 14:54:12 -07001003 ((mCountX - 1) * mWidthGap);
Michael Jurka8b805b12012-04-18 14:23:14 -07001004 newHeight = getPaddingTop() + getPaddingBottom() + (mCountY * mCellHeight) +
Winson Chungece7f5b2010-10-22 14:54:12 -07001005 ((mCountY - 1) * mHeightGap);
Michael Jurka5f1c5092010-09-03 14:15:02 -07001006 setMeasuredDimension(newWidth, newHeight);
Michael Jurka5f1c5092010-09-03 14:15:02 -07001007 }
Michael Jurka8c920dd2011-01-20 14:16:56 -08001008
1009 int count = getChildCount();
1010 for (int i = 0; i < count; i++) {
1011 View child = getChildAt(i);
Michael Jurka8b805b12012-04-18 14:23:14 -07001012 int childWidthMeasureSpec = MeasureSpec.makeMeasureSpec(newWidth - getPaddingLeft() -
1013 getPaddingRight(), MeasureSpec.EXACTLY);
1014 int childheightMeasureSpec = MeasureSpec.makeMeasureSpec(newHeight - getPaddingTop() -
1015 getPaddingBottom(), MeasureSpec.EXACTLY);
Michael Jurka8c920dd2011-01-20 14:16:56 -08001016 child.measure(childWidthMeasureSpec, childheightMeasureSpec);
1017 }
1018 setMeasuredDimension(newWidth, newHeight);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001019 }
1020
1021 @Override
Michael Jurka28750fb2010-09-24 17:43:49 -07001022 protected void onLayout(boolean changed, int l, int t, int r, int b) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001023 int count = getChildCount();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001024 for (int i = 0; i < count; i++) {
Michael Jurka8c920dd2011-01-20 14:16:56 -08001025 View child = getChildAt(i);
Michael Jurka8b805b12012-04-18 14:23:14 -07001026 child.layout(getPaddingLeft(), getPaddingTop(),
1027 r - l - getPaddingRight(), b - t - getPaddingBottom());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001028 }
1029 }
1030
1031 @Override
Michael Jurkadee05892010-07-27 10:01:56 -07001032 protected void onSizeChanged(int w, int h, int oldw, int oldh) {
1033 super.onSizeChanged(w, h, oldw, oldh);
Michael Jurka18014792010-10-14 09:01:34 -07001034 mBackgroundRect.set(0, 0, w, h);
Adam Cohenb5ba0972011-09-07 18:02:31 -07001035 mForegroundRect.set(mForegroundPadding, mForegroundPadding,
Adam Cohen215b4162012-08-30 13:14:08 -07001036 w - mForegroundPadding, h - mForegroundPadding);
Michael Jurkadee05892010-07-27 10:01:56 -07001037 }
1038
1039 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001040 protected void setChildrenDrawingCacheEnabled(boolean enabled) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07001041 mShortcutsAndWidgets.setChildrenDrawingCacheEnabled(enabled);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001042 }
1043
1044 @Override
1045 protected void setChildrenDrawnWithCacheEnabled(boolean enabled) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07001046 mShortcutsAndWidgets.setChildrenDrawnWithCacheEnabled(enabled);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001047 }
1048
Michael Jurka5f1c5092010-09-03 14:15:02 -07001049 public float getBackgroundAlpha() {
1050 return mBackgroundAlpha;
Michael Jurkadee05892010-07-27 10:01:56 -07001051 }
1052
Adam Cohen1b0aaac2010-10-28 11:11:18 -07001053 public void setBackgroundAlphaMultiplier(float multiplier) {
Michael Jurkaa3d30ad2012-05-08 13:43:43 -07001054 if (mBackgroundAlphaMultiplier != multiplier) {
1055 mBackgroundAlphaMultiplier = multiplier;
1056 invalidate();
1057 }
Adam Cohen1b0aaac2010-10-28 11:11:18 -07001058 }
1059
Adam Cohenddb82192010-11-10 16:32:54 -08001060 public float getBackgroundAlphaMultiplier() {
1061 return mBackgroundAlphaMultiplier;
1062 }
1063
Michael Jurka5f1c5092010-09-03 14:15:02 -07001064 public void setBackgroundAlpha(float alpha) {
Michael Jurkaafaa0502011-12-13 18:22:50 -08001065 if (mBackgroundAlpha != alpha) {
1066 mBackgroundAlpha = alpha;
1067 invalidate();
1068 }
Michael Jurkadee05892010-07-27 10:01:56 -07001069 }
1070
Michael Jurkaa52570f2012-03-20 03:18:20 -07001071 public void setShortcutAndWidgetAlpha(float alpha) {
Michael Jurka0142d492010-08-25 17:46:15 -07001072 final int childCount = getChildCount();
1073 for (int i = 0; i < childCount; i++) {
Michael Jurkadee05892010-07-27 10:01:56 -07001074 getChildAt(i).setAlpha(alpha);
1075 }
1076 }
1077
Michael Jurkaa52570f2012-03-20 03:18:20 -07001078 public ShortcutAndWidgetContainer getShortcutsAndWidgets() {
1079 if (getChildCount() > 0) {
1080 return (ShortcutAndWidgetContainer) getChildAt(0);
1081 }
1082 return null;
1083 }
1084
Patrick Dubroy440c3602010-07-13 17:50:32 -07001085 public View getChildAt(int x, int y) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07001086 return mShortcutsAndWidgets.getChildAt(x, y);
Patrick Dubroy440c3602010-07-13 17:50:32 -07001087 }
1088
Adam Cohen76fc0852011-06-17 13:26:23 -07001089 public boolean animateChildToPosition(final View child, int cellX, int cellY, int duration,
Adam Cohen482ed822012-03-02 14:15:13 -08001090 int delay, boolean permanent, boolean adjustOccupied) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07001091 ShortcutAndWidgetContainer clc = getShortcutsAndWidgets();
Adam Cohen482ed822012-03-02 14:15:13 -08001092 boolean[][] occupied = mOccupied;
1093 if (!permanent) {
1094 occupied = mTmpOccupied;
1095 }
1096
Adam Cohen19f37922012-03-21 11:59:11 -07001097 if (clc.indexOfChild(child) != -1) {
Adam Cohenbfbfd262011-06-13 16:55:12 -07001098 final LayoutParams lp = (LayoutParams) child.getLayoutParams();
1099 final ItemInfo info = (ItemInfo) child.getTag();
1100
1101 // We cancel any existing animations
1102 if (mReorderAnimators.containsKey(lp)) {
1103 mReorderAnimators.get(lp).cancel();
1104 mReorderAnimators.remove(lp);
1105 }
1106
Adam Cohen482ed822012-03-02 14:15:13 -08001107 final int oldX = lp.x;
1108 final int oldY = lp.y;
1109 if (adjustOccupied) {
1110 occupied[lp.cellX][lp.cellY] = false;
1111 occupied[cellX][cellY] = true;
1112 }
Adam Cohenbfbfd262011-06-13 16:55:12 -07001113 lp.isLockedToGrid = true;
Adam Cohen482ed822012-03-02 14:15:13 -08001114 if (permanent) {
1115 lp.cellX = info.cellX = cellX;
1116 lp.cellY = info.cellY = cellY;
1117 } else {
1118 lp.tmpCellX = cellX;
1119 lp.tmpCellY = cellY;
1120 }
Adam Cohenbfbfd262011-06-13 16:55:12 -07001121 clc.setupLp(lp);
1122 lp.isLockedToGrid = false;
Adam Cohen482ed822012-03-02 14:15:13 -08001123 final int newX = lp.x;
1124 final int newY = lp.y;
Adam Cohenbfbfd262011-06-13 16:55:12 -07001125
Adam Cohen76fc0852011-06-17 13:26:23 -07001126 lp.x = oldX;
1127 lp.y = oldY;
Adam Cohen76fc0852011-06-17 13:26:23 -07001128
Adam Cohen482ed822012-03-02 14:15:13 -08001129 // Exit early if we're not actually moving the view
1130 if (oldX == newX && oldY == newY) {
1131 lp.isLockedToGrid = true;
1132 return true;
1133 }
1134
Michael Jurka2ecf9952012-06-18 12:52:28 -07001135 ValueAnimator va = LauncherAnimUtils.ofFloat(0f, 1f);
Adam Cohen482ed822012-03-02 14:15:13 -08001136 va.setDuration(duration);
1137 mReorderAnimators.put(lp, va);
1138
1139 va.addUpdateListener(new AnimatorUpdateListener() {
1140 @Override
Adam Cohenbfbfd262011-06-13 16:55:12 -07001141 public void onAnimationUpdate(ValueAnimator animation) {
Adam Cohen482ed822012-03-02 14:15:13 -08001142 float r = ((Float) animation.getAnimatedValue()).floatValue();
Adam Cohen19f37922012-03-21 11:59:11 -07001143 lp.x = (int) ((1 - r) * oldX + r * newX);
1144 lp.y = (int) ((1 - r) * oldY + r * newY);
Adam Cohen6b8a02d2012-03-22 15:13:40 -07001145 child.requestLayout();
Adam Cohenbfbfd262011-06-13 16:55:12 -07001146 }
1147 });
Adam Cohen482ed822012-03-02 14:15:13 -08001148 va.addListener(new AnimatorListenerAdapter() {
Adam Cohenbfbfd262011-06-13 16:55:12 -07001149 boolean cancelled = false;
1150 public void onAnimationEnd(Animator animation) {
1151 // If the animation was cancelled, it means that another animation
1152 // has interrupted this one, and we don't want to lock the item into
1153 // place just yet.
1154 if (!cancelled) {
1155 lp.isLockedToGrid = true;
Adam Cohen482ed822012-03-02 14:15:13 -08001156 child.requestLayout();
Adam Cohenbfbfd262011-06-13 16:55:12 -07001157 }
1158 if (mReorderAnimators.containsKey(lp)) {
1159 mReorderAnimators.remove(lp);
1160 }
1161 }
1162 public void onAnimationCancel(Animator animation) {
1163 cancelled = true;
1164 }
1165 });
Adam Cohen482ed822012-03-02 14:15:13 -08001166 va.setStartDelay(delay);
1167 va.start();
Adam Cohenbfbfd262011-06-13 16:55:12 -07001168 return true;
1169 }
1170 return false;
1171 }
1172
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001173 /**
1174 * Estimate where the top left cell of the dragged item will land if it is dropped.
1175 *
1176 * @param originX The X value of the top left corner of the item
1177 * @param originY The Y value of the top left corner of the item
1178 * @param spanX The number of horizontal cells that the item spans
1179 * @param spanY The number of vertical cells that the item spans
1180 * @param result The estimated drop cell X and Y.
1181 */
1182 void estimateDropCell(int originX, int originY, int spanX, int spanY, int[] result) {
Adam Cohend22015c2010-07-26 22:02:18 -07001183 final int countX = mCountX;
1184 final int countY = mCountY;
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001185
Michael Jurkaa63c4522010-08-19 13:52:27 -07001186 // pointToCellRounded takes the top left of a cell but will pad that with
1187 // cellWidth/2 and cellHeight/2 when finding the matching cell
1188 pointToCellRounded(originX, originY, result);
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001189
1190 // If the item isn't fully on this screen, snap to the edges
1191 int rightOverhang = result[0] + spanX - countX;
1192 if (rightOverhang > 0) {
1193 result[0] -= rightOverhang; // Snap to right
1194 }
1195 result[0] = Math.max(0, result[0]); // Snap to left
1196 int bottomOverhang = result[1] + spanY - countY;
1197 if (bottomOverhang > 0) {
1198 result[1] -= bottomOverhang; // Snap to bottom
1199 }
1200 result[1] = Math.max(0, result[1]); // Snap to top
1201 }
1202
Adam Cohen482ed822012-03-02 14:15:13 -08001203 void visualizeDropLocation(View v, Bitmap dragOutline, int originX, int originY, int cellX,
1204 int cellY, int spanX, int spanY, boolean resize, Point dragOffset, Rect dragRegion) {
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -07001205 final int oldDragCellX = mDragCell[0];
1206 final int oldDragCellY = mDragCell[1];
Adam Cohen482ed822012-03-02 14:15:13 -08001207
Winson Chungb8c69f32011-10-19 21:36:08 -07001208 if (v != null && dragOffset == null) {
Winson Chunga9abd0e2010-10-27 17:18:37 -07001209 mDragCenter.set(originX + (v.getWidth() / 2), originY + (v.getHeight() / 2));
1210 } else {
1211 mDragCenter.set(originX, originY);
1212 }
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001213
Adam Cohen2801caf2011-05-13 20:57:39 -07001214 if (dragOutline == null && v == null) {
Adam Cohen2801caf2011-05-13 20:57:39 -07001215 return;
1216 }
1217
Adam Cohen482ed822012-03-02 14:15:13 -08001218 if (cellX != oldDragCellX || cellY != oldDragCellY) {
1219 mDragCell[0] = cellX;
1220 mDragCell[1] = cellY;
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001221 // Find the top left corner of the rect the object will occupy
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001222 final int[] topLeft = mTmpPoint;
Adam Cohen482ed822012-03-02 14:15:13 -08001223 cellToPoint(cellX, cellY, topLeft);
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001224
Joe Onorato4be866d2010-10-10 11:26:02 -07001225 int left = topLeft[0];
1226 int top = topLeft[1];
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001227
Winson Chungb8c69f32011-10-19 21:36:08 -07001228 if (v != null && dragOffset == null) {
Adam Cohen99e8b402011-03-25 19:23:43 -07001229 // When drawing the drag outline, it did not account for margin offsets
1230 // added by the view's parent.
1231 MarginLayoutParams lp = (MarginLayoutParams) v.getLayoutParams();
1232 left += lp.leftMargin;
1233 top += lp.topMargin;
Winson Chung150fbab2010-09-29 17:14:26 -07001234
Adam Cohen99e8b402011-03-25 19:23:43 -07001235 // Offsets due to the size difference between the View and the dragOutline.
1236 // There is a size difference to account for the outer blur, which may lie
1237 // outside the bounds of the view.
Winson Chunga9abd0e2010-10-27 17:18:37 -07001238 top += (v.getHeight() - dragOutline.getHeight()) / 2;
Adam Cohenae915ce2011-08-25 13:47:22 -07001239 // We center about the x axis
1240 left += ((mCellWidth * spanX) + ((spanX - 1) * mWidthGap)
1241 - dragOutline.getWidth()) / 2;
Adam Cohen66396872011-04-15 17:50:36 -07001242 } else {
Winson Chungb8c69f32011-10-19 21:36:08 -07001243 if (dragOffset != null && dragRegion != null) {
1244 // Center the drag region *horizontally* in the cell and apply a drag
1245 // outline offset
1246 left += dragOffset.x + ((mCellWidth * spanX) + ((spanX - 1) * mWidthGap)
1247 - dragRegion.width()) / 2;
1248 top += dragOffset.y;
1249 } else {
1250 // Center the drag outline in the cell
1251 left += ((mCellWidth * spanX) + ((spanX - 1) * mWidthGap)
1252 - dragOutline.getWidth()) / 2;
1253 top += ((mCellHeight * spanY) + ((spanY - 1) * mHeightGap)
1254 - dragOutline.getHeight()) / 2;
1255 }
Winson Chunga9abd0e2010-10-27 17:18:37 -07001256 }
Joe Onorato4be866d2010-10-10 11:26:02 -07001257 final int oldIndex = mDragOutlineCurrent;
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -07001258 mDragOutlineAnims[oldIndex].animateOut();
1259 mDragOutlineCurrent = (oldIndex + 1) % mDragOutlines.length;
Adam Cohend41fbf52012-02-16 23:53:59 -08001260 Rect r = mDragOutlines[mDragOutlineCurrent];
1261 r.set(left, top, left + dragOutline.getWidth(), top + dragOutline.getHeight());
1262 if (resize) {
Adam Cohen482ed822012-03-02 14:15:13 -08001263 cellToRect(cellX, cellY, spanX, spanY, r);
Adam Cohend41fbf52012-02-16 23:53:59 -08001264 }
Winson Chung150fbab2010-09-29 17:14:26 -07001265
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -07001266 mDragOutlineAnims[mDragOutlineCurrent].setTag(dragOutline);
1267 mDragOutlineAnims[mDragOutlineCurrent].animateIn();
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001268 }
1269 }
1270
Adam Cohene0310962011-04-18 16:15:31 -07001271 public void clearDragOutlines() {
1272 final int oldIndex = mDragOutlineCurrent;
1273 mDragOutlineAnims[oldIndex].animateOut();
Adam Cohend41fbf52012-02-16 23:53:59 -08001274 mDragCell[0] = mDragCell[1] = -1;
Adam Cohene0310962011-04-18 16:15:31 -07001275 }
1276
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001277 /**
Jeff Sharkey70864282009-04-07 21:08:40 -07001278 * Find a vacant area that will fit the given bounds nearest the requested
1279 * cell location. Uses Euclidean distance to score multiple vacant areas.
Winson Chungaafa03c2010-06-11 17:34:16 -07001280 *
Romain Guy51afc022009-05-04 18:03:43 -07001281 * @param pixelX The X location at which you want to search for a vacant area.
1282 * @param pixelY The Y location at which you want to search for a vacant area.
Jeff Sharkey70864282009-04-07 21:08:40 -07001283 * @param spanX Horizontal span of the object.
1284 * @param spanY Vertical span of the object.
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001285 * @param result Array in which to place the result, or null (in which case a new array will
1286 * be allocated)
Jeff Sharkey70864282009-04-07 21:08:40 -07001287 * @return The X, Y cell of a vacant area that can contain this object,
1288 * nearest the requested location.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001289 */
Adam Cohend41fbf52012-02-16 23:53:59 -08001290 int[] findNearestVacantArea(int pixelX, int pixelY, int spanX, int spanY,
1291 int[] result) {
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001292 return findNearestVacantArea(pixelX, pixelY, spanX, spanY, null, result);
Michael Jurka6a1435d2010-09-27 17:35:12 -07001293 }
Winson Chungaafa03c2010-06-11 17:34:16 -07001294
Michael Jurka6a1435d2010-09-27 17:35:12 -07001295 /**
1296 * Find a vacant area that will fit the given bounds nearest the requested
1297 * cell location. Uses Euclidean distance to score multiple vacant areas.
1298 *
1299 * @param pixelX The X location at which you want to search for a vacant area.
1300 * @param pixelY The Y location at which you want to search for a vacant area.
Adam Cohend41fbf52012-02-16 23:53:59 -08001301 * @param minSpanX The minimum horizontal span required
1302 * @param minSpanY The minimum vertical span required
1303 * @param spanX Horizontal span of the object.
1304 * @param spanY Vertical span of the object.
1305 * @param result Array in which to place the result, or null (in which case a new array will
1306 * be allocated)
1307 * @return The X, Y cell of a vacant area that can contain this object,
1308 * nearest the requested location.
1309 */
1310 int[] findNearestVacantArea(int pixelX, int pixelY, int minSpanX, int minSpanY, int spanX,
1311 int spanY, int[] result, int[] resultSpan) {
1312 return findNearestVacantArea(pixelX, pixelY, minSpanX, minSpanY, spanX, spanY, null,
1313 result, resultSpan);
1314 }
1315
1316 /**
1317 * Find a vacant area that will fit the given bounds nearest the requested
1318 * cell location. Uses Euclidean distance to score multiple vacant areas.
1319 *
1320 * @param pixelX The X location at which you want to search for a vacant area.
1321 * @param pixelY The Y location at which you want to search for a vacant area.
Michael Jurka6a1435d2010-09-27 17:35:12 -07001322 * @param spanX Horizontal span of the object.
1323 * @param spanY Vertical span of the object.
Adam Cohendf035382011-04-11 17:22:04 -07001324 * @param ignoreOccupied If true, the result can be an occupied cell
1325 * @param result Array in which to place the result, or null (in which case a new array will
1326 * be allocated)
Michael Jurka6a1435d2010-09-27 17:35:12 -07001327 * @return The X, Y cell of a vacant area that can contain this object,
1328 * nearest the requested location.
1329 */
Adam Cohendf035382011-04-11 17:22:04 -07001330 int[] findNearestArea(int pixelX, int pixelY, int spanX, int spanY, View ignoreView,
1331 boolean ignoreOccupied, int[] result) {
Adam Cohend41fbf52012-02-16 23:53:59 -08001332 return findNearestArea(pixelX, pixelY, spanX, spanY,
Adam Cohen482ed822012-03-02 14:15:13 -08001333 spanX, spanY, ignoreView, ignoreOccupied, result, null, mOccupied);
Adam Cohend41fbf52012-02-16 23:53:59 -08001334 }
1335
1336 private final Stack<Rect> mTempRectStack = new Stack<Rect>();
1337 private void lazyInitTempRectStack() {
1338 if (mTempRectStack.isEmpty()) {
1339 for (int i = 0; i < mCountX * mCountY; i++) {
1340 mTempRectStack.push(new Rect());
1341 }
1342 }
1343 }
Adam Cohen482ed822012-03-02 14:15:13 -08001344
Adam Cohend41fbf52012-02-16 23:53:59 -08001345 private void recycleTempRects(Stack<Rect> used) {
1346 while (!used.isEmpty()) {
1347 mTempRectStack.push(used.pop());
1348 }
1349 }
1350
1351 /**
1352 * Find a vacant area that will fit the given bounds nearest the requested
1353 * cell location. Uses Euclidean distance to score multiple vacant areas.
1354 *
1355 * @param pixelX The X location at which you want to search for a vacant area.
1356 * @param pixelY The Y location at which you want to search for a vacant area.
1357 * @param minSpanX The minimum horizontal span required
1358 * @param minSpanY The minimum vertical span required
1359 * @param spanX Horizontal span of the object.
1360 * @param spanY Vertical span of the object.
1361 * @param ignoreOccupied If true, the result can be an occupied cell
1362 * @param result Array in which to place the result, or null (in which case a new array will
1363 * be allocated)
1364 * @return The X, Y cell of a vacant area that can contain this object,
1365 * nearest the requested location.
1366 */
1367 int[] findNearestArea(int pixelX, int pixelY, int minSpanX, int minSpanY, int spanX, int spanY,
Adam Cohen482ed822012-03-02 14:15:13 -08001368 View ignoreView, boolean ignoreOccupied, int[] result, int[] resultSpan,
1369 boolean[][] occupied) {
Adam Cohend41fbf52012-02-16 23:53:59 -08001370 lazyInitTempRectStack();
Michael Jurkac6ee42e2010-09-30 12:04:50 -07001371 // mark space take by ignoreView as available (method checks if ignoreView is null)
Adam Cohen482ed822012-03-02 14:15:13 -08001372 markCellsAsUnoccupiedForView(ignoreView, occupied);
Michael Jurkac6ee42e2010-09-30 12:04:50 -07001373
Adam Cohene3e27a82011-04-15 12:07:39 -07001374 // For items with a spanX / spanY > 1, the passed in point (pixelX, pixelY) corresponds
1375 // to the center of the item, but we are searching based on the top-left cell, so
1376 // we translate the point over to correspond to the top-left.
1377 pixelX -= (mCellWidth + mWidthGap) * (spanX - 1) / 2f;
1378 pixelY -= (mCellHeight + mHeightGap) * (spanY - 1) / 2f;
1379
Jeff Sharkey70864282009-04-07 21:08:40 -07001380 // Keep track of best-scoring drop area
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001381 final int[] bestXY = result != null ? result : new int[2];
Jeff Sharkey70864282009-04-07 21:08:40 -07001382 double bestDistance = Double.MAX_VALUE;
Adam Cohend41fbf52012-02-16 23:53:59 -08001383 final Rect bestRect = new Rect(-1, -1, -1, -1);
1384 final Stack<Rect> validRegions = new Stack<Rect>();
Winson Chungaafa03c2010-06-11 17:34:16 -07001385
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001386 final int countX = mCountX;
1387 final int countY = mCountY;
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001388
Adam Cohend41fbf52012-02-16 23:53:59 -08001389 if (minSpanX <= 0 || minSpanY <= 0 || spanX <= 0 || spanY <= 0 ||
1390 spanX < minSpanX || spanY < minSpanY) {
1391 return bestXY;
1392 }
1393
1394 for (int y = 0; y < countY - (minSpanY - 1); y++) {
Michael Jurkac28de512010-08-13 11:27:44 -07001395 inner:
Adam Cohend41fbf52012-02-16 23:53:59 -08001396 for (int x = 0; x < countX - (minSpanX - 1); x++) {
1397 int ySize = -1;
1398 int xSize = -1;
Adam Cohendf035382011-04-11 17:22:04 -07001399 if (ignoreOccupied) {
Adam Cohend41fbf52012-02-16 23:53:59 -08001400 // First, let's see if this thing fits anywhere
1401 for (int i = 0; i < minSpanX; i++) {
1402 for (int j = 0; j < minSpanY; j++) {
Adam Cohendf035382011-04-11 17:22:04 -07001403 if (occupied[x + i][y + j]) {
Adam Cohendf035382011-04-11 17:22:04 -07001404 continue inner;
1405 }
Michael Jurkac28de512010-08-13 11:27:44 -07001406 }
1407 }
Adam Cohend41fbf52012-02-16 23:53:59 -08001408 xSize = minSpanX;
1409 ySize = minSpanY;
1410
1411 // We know that the item will fit at _some_ acceptable size, now let's see
1412 // how big we can make it. We'll alternate between incrementing x and y spans
1413 // until we hit a limit.
1414 boolean incX = true;
1415 boolean hitMaxX = xSize >= spanX;
1416 boolean hitMaxY = ySize >= spanY;
1417 while (!(hitMaxX && hitMaxY)) {
1418 if (incX && !hitMaxX) {
1419 for (int j = 0; j < ySize; j++) {
1420 if (x + xSize > countX -1 || occupied[x + xSize][y + j]) {
1421 // We can't move out horizontally
1422 hitMaxX = true;
1423 }
1424 }
1425 if (!hitMaxX) {
1426 xSize++;
1427 }
1428 } else if (!hitMaxY) {
1429 for (int i = 0; i < xSize; i++) {
1430 if (y + ySize > countY - 1 || occupied[x + i][y + ySize]) {
1431 // We can't move out vertically
1432 hitMaxY = true;
1433 }
1434 }
1435 if (!hitMaxY) {
1436 ySize++;
1437 }
1438 }
1439 hitMaxX |= xSize >= spanX;
1440 hitMaxY |= ySize >= spanY;
1441 incX = !incX;
1442 }
1443 incX = true;
1444 hitMaxX = xSize >= spanX;
1445 hitMaxY = ySize >= spanY;
Michael Jurkac28de512010-08-13 11:27:44 -07001446 }
Winson Chung0be025d2011-05-23 17:45:09 -07001447 final int[] cellXY = mTmpXY;
Adam Cohene3e27a82011-04-15 12:07:39 -07001448 cellToCenterPoint(x, y, cellXY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001449
Adam Cohend41fbf52012-02-16 23:53:59 -08001450 // We verify that the current rect is not a sub-rect of any of our previous
1451 // candidates. In this case, the current rect is disqualified in favour of the
1452 // containing rect.
1453 Rect currentRect = mTempRectStack.pop();
1454 currentRect.set(x, y, x + xSize, y + ySize);
1455 boolean contained = false;
1456 for (Rect r : validRegions) {
1457 if (r.contains(currentRect)) {
1458 contained = true;
1459 break;
1460 }
1461 }
1462 validRegions.push(currentRect);
Michael Jurkac28de512010-08-13 11:27:44 -07001463 double distance = Math.sqrt(Math.pow(cellXY[0] - pixelX, 2)
1464 + Math.pow(cellXY[1] - pixelY, 2));
Adam Cohen482ed822012-03-02 14:15:13 -08001465
Adam Cohend41fbf52012-02-16 23:53:59 -08001466 if ((distance <= bestDistance && !contained) ||
1467 currentRect.contains(bestRect)) {
Michael Jurkac28de512010-08-13 11:27:44 -07001468 bestDistance = distance;
1469 bestXY[0] = x;
1470 bestXY[1] = y;
Adam Cohend41fbf52012-02-16 23:53:59 -08001471 if (resultSpan != null) {
1472 resultSpan[0] = xSize;
1473 resultSpan[1] = ySize;
1474 }
1475 bestRect.set(currentRect);
Michael Jurkac28de512010-08-13 11:27:44 -07001476 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001477 }
1478 }
Michael Jurkac6ee42e2010-09-30 12:04:50 -07001479 // re-mark space taken by ignoreView as occupied
Adam Cohen482ed822012-03-02 14:15:13 -08001480 markCellsAsOccupiedForView(ignoreView, occupied);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001481
Adam Cohenc0dcf592011-06-01 15:30:43 -07001482 // Return -1, -1 if no suitable location found
1483 if (bestDistance == Double.MAX_VALUE) {
1484 bestXY[0] = -1;
1485 bestXY[1] = -1;
Jeff Sharkey70864282009-04-07 21:08:40 -07001486 }
Adam Cohend41fbf52012-02-16 23:53:59 -08001487 recycleTempRects(validRegions);
Adam Cohenc0dcf592011-06-01 15:30:43 -07001488 return bestXY;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001489 }
Winson Chungaafa03c2010-06-11 17:34:16 -07001490
Adam Cohen482ed822012-03-02 14:15:13 -08001491 /**
1492 * Find a vacant area that will fit the given bounds nearest the requested
1493 * cell location, and will also weigh in a suggested direction vector of the
1494 * desired location. This method computers distance based on unit grid distances,
1495 * not pixel distances.
1496 *
Adam Cohen47a876d2012-03-19 13:21:41 -07001497 * @param cellX The X cell nearest to which you want to search for a vacant area.
1498 * @param cellY The Y cell nearest which you want to search for a vacant area.
Adam Cohen482ed822012-03-02 14:15:13 -08001499 * @param spanX Horizontal span of the object.
1500 * @param spanY Vertical span of the object.
Adam Cohen47a876d2012-03-19 13:21:41 -07001501 * @param direction The favored direction in which the views should move from x, y
1502 * @param exactDirectionOnly If this parameter is true, then only solutions where the direction
1503 * matches exactly. Otherwise we find the best matching direction.
1504 * @param occoupied The array which represents which cells in the CellLayout are occupied
1505 * @param blockOccupied The array which represents which cells in the specified block (cellX,
1506 * cellY, spanX, spanY) are occupied. This is used when try to move a group of views.
Adam Cohen482ed822012-03-02 14:15:13 -08001507 * @param result Array in which to place the result, or null (in which case a new array will
1508 * be allocated)
1509 * @return The X, Y cell of a vacant area that can contain this object,
1510 * nearest the requested location.
1511 */
1512 private int[] findNearestArea(int cellX, int cellY, int spanX, int spanY, int[] direction,
Adam Cohen47a876d2012-03-19 13:21:41 -07001513 boolean[][] occupied, boolean blockOccupied[][], int[] result) {
Adam Cohen482ed822012-03-02 14:15:13 -08001514 // Keep track of best-scoring drop area
1515 final int[] bestXY = result != null ? result : new int[2];
1516 float bestDistance = Float.MAX_VALUE;
1517 int bestDirectionScore = Integer.MIN_VALUE;
1518
1519 final int countX = mCountX;
1520 final int countY = mCountY;
1521
1522 for (int y = 0; y < countY - (spanY - 1); y++) {
1523 inner:
1524 for (int x = 0; x < countX - (spanX - 1); x++) {
1525 // First, let's see if this thing fits anywhere
1526 for (int i = 0; i < spanX; i++) {
1527 for (int j = 0; j < spanY; j++) {
Adam Cohen47a876d2012-03-19 13:21:41 -07001528 if (occupied[x + i][y + j] && (blockOccupied == null || blockOccupied[i][j])) {
Adam Cohen482ed822012-03-02 14:15:13 -08001529 continue inner;
1530 }
1531 }
1532 }
1533
1534 float distance = (float)
1535 Math.sqrt((x - cellX) * (x - cellX) + (y - cellY) * (y - cellY));
1536 int[] curDirection = mTmpPoint;
Adam Cohen47a876d2012-03-19 13:21:41 -07001537 computeDirectionVector(x - cellX, y - cellY, curDirection);
1538 // The direction score is just the dot product of the two candidate direction
1539 // and that passed in.
Adam Cohen482ed822012-03-02 14:15:13 -08001540 int curDirectionScore = direction[0] * curDirection[0] +
1541 direction[1] * curDirection[1];
Adam Cohen47a876d2012-03-19 13:21:41 -07001542 boolean exactDirectionOnly = false;
1543 boolean directionMatches = direction[0] == curDirection[0] &&
1544 direction[0] == curDirection[0];
1545 if ((directionMatches || !exactDirectionOnly) &&
1546 Float.compare(distance, bestDistance) < 0 || (Float.compare(distance,
Adam Cohen482ed822012-03-02 14:15:13 -08001547 bestDistance) == 0 && curDirectionScore > bestDirectionScore)) {
1548 bestDistance = distance;
1549 bestDirectionScore = curDirectionScore;
1550 bestXY[0] = x;
1551 bestXY[1] = y;
1552 }
1553 }
1554 }
1555
1556 // Return -1, -1 if no suitable location found
1557 if (bestDistance == Float.MAX_VALUE) {
1558 bestXY[0] = -1;
1559 bestXY[1] = -1;
1560 }
1561 return bestXY;
1562 }
1563
1564 private boolean addViewToTempLocation(View v, Rect rectOccupiedByPotentialDrop,
Adam Cohen8baab352012-03-20 17:39:21 -07001565 int[] direction, ItemConfiguration currentState) {
1566 CellAndSpan c = currentState.map.get(v);
Adam Cohen482ed822012-03-02 14:15:13 -08001567 boolean success = false;
Adam Cohen8baab352012-03-20 17:39:21 -07001568 markCellsForView(c.x, c.y, c.spanX, c.spanY, mTmpOccupied, false);
Adam Cohen482ed822012-03-02 14:15:13 -08001569 markCellsForRect(rectOccupiedByPotentialDrop, mTmpOccupied, true);
1570
Adam Cohen8baab352012-03-20 17:39:21 -07001571 findNearestArea(c.x, c.y, c.spanX, c.spanY, direction, mTmpOccupied, null, mTempLocation);
Adam Cohen482ed822012-03-02 14:15:13 -08001572
1573 if (mTempLocation[0] >= 0 && mTempLocation[1] >= 0) {
Adam Cohen8baab352012-03-20 17:39:21 -07001574 c.x = mTempLocation[0];
1575 c.y = mTempLocation[1];
Adam Cohen482ed822012-03-02 14:15:13 -08001576 success = true;
Adam Cohen482ed822012-03-02 14:15:13 -08001577 }
Adam Cohen8baab352012-03-20 17:39:21 -07001578 markCellsForView(c.x, c.y, c.spanX, c.spanY, mTmpOccupied, true);
Adam Cohen482ed822012-03-02 14:15:13 -08001579 return success;
1580 }
1581
Adam Cohenf3900c22012-11-16 18:28:11 -08001582 /**
1583 * This helper class defines a cluster of views. It helps with defining complex edges
1584 * of the cluster and determining how those edges interact with other views. The edges
1585 * essentially define a fine-grained boundary around the cluster of views -- like a more
1586 * precise version of a bounding box.
1587 */
1588 private class ViewCluster {
1589 final static int LEFT = 0;
1590 final static int TOP = 1;
1591 final static int RIGHT = 2;
1592 final static int BOTTOM = 3;
Adam Cohen47a876d2012-03-19 13:21:41 -07001593
Adam Cohenf3900c22012-11-16 18:28:11 -08001594 ArrayList<View> views;
1595 ItemConfiguration config;
1596 Rect boundingRect = new Rect();
Adam Cohen47a876d2012-03-19 13:21:41 -07001597
Adam Cohenf3900c22012-11-16 18:28:11 -08001598 int[] leftEdge = new int[mCountY];
1599 int[] rightEdge = new int[mCountY];
1600 int[] topEdge = new int[mCountX];
1601 int[] bottomEdge = new int[mCountX];
1602 boolean leftEdgeDirty, rightEdgeDirty, topEdgeDirty, bottomEdgeDirty, boundingRectDirty;
1603
1604 @SuppressWarnings("unchecked")
1605 public ViewCluster(ArrayList<View> views, ItemConfiguration config) {
1606 this.views = (ArrayList<View>) views.clone();
1607 this.config = config;
1608 resetEdges();
Adam Cohen47a876d2012-03-19 13:21:41 -07001609 }
1610
Adam Cohenf3900c22012-11-16 18:28:11 -08001611 void resetEdges() {
1612 for (int i = 0; i < mCountX; i++) {
1613 topEdge[i] = -1;
1614 bottomEdge[i] = -1;
1615 }
1616 for (int i = 0; i < mCountY; i++) {
1617 leftEdge[i] = -1;
1618 rightEdge[i] = -1;
1619 }
1620 leftEdgeDirty = true;
1621 rightEdgeDirty = true;
1622 bottomEdgeDirty = true;
1623 topEdgeDirty = true;
1624 boundingRectDirty = true;
1625 }
Adam Cohen47a876d2012-03-19 13:21:41 -07001626
Adam Cohenf3900c22012-11-16 18:28:11 -08001627 void computeEdge(int which, int[] edge) {
1628 int count = views.size();
1629 for (int i = 0; i < count; i++) {
1630 CellAndSpan cs = config.map.get(views.get(i));
1631 switch (which) {
1632 case LEFT:
1633 int left = cs.x;
1634 for (int j = cs.y; j < cs.y + cs.spanY; j++) {
1635 if (left < edge[j] || edge[j] < 0) {
1636 edge[j] = left;
Adam Cohena56dc102012-07-13 13:41:42 -07001637 }
Adam Cohen47a876d2012-03-19 13:21:41 -07001638 }
Adam Cohenf3900c22012-11-16 18:28:11 -08001639 break;
1640 case RIGHT:
1641 int right = cs.x + cs.spanX;
1642 for (int j = cs.y; j < cs.y + cs.spanY; j++) {
1643 if (right > edge[j]) {
1644 edge[j] = right;
1645 }
1646 }
1647 break;
1648 case TOP:
1649 int top = cs.y;
1650 for (int j = cs.x; j < cs.x + cs.spanX; j++) {
1651 if (top < edge[j] || edge[j] < 0) {
1652 edge[j] = top;
1653 }
1654 }
1655 break;
1656 case BOTTOM:
1657 int bottom = cs.y + cs.spanY;
1658 for (int j = cs.x; j < cs.x + cs.spanX; j++) {
1659 if (bottom > edge[j]) {
1660 edge[j] = bottom;
1661 }
1662 }
1663 break;
Adam Cohen47a876d2012-03-19 13:21:41 -07001664 }
1665 }
1666 }
Adam Cohenf3900c22012-11-16 18:28:11 -08001667
1668 boolean isViewTouchingEdge(View v, int whichEdge) {
1669 CellAndSpan cs = config.map.get(v);
1670
1671 int[] edge = getEdge(whichEdge);
1672
1673 switch (whichEdge) {
1674 case LEFT:
1675 for (int i = cs.y; i < cs.y + cs.spanY; i++) {
1676 if (edge[i] == cs.x + cs.spanX) {
1677 return true;
1678 }
1679 }
1680 break;
1681 case RIGHT:
1682 for (int i = cs.y; i < cs.y + cs.spanY; i++) {
1683 if (edge[i] == cs.x) {
1684 return true;
1685 }
1686 }
1687 break;
1688 case TOP:
1689 for (int i = cs.x; i < cs.x + cs.spanX; i++) {
1690 if (edge[i] == cs.y + cs.spanY) {
1691 return true;
1692 }
1693 }
1694 break;
1695 case BOTTOM:
1696 for (int i = cs.x; i < cs.x + cs.spanX; i++) {
1697 if (edge[i] == cs.y) {
1698 return true;
1699 }
1700 }
1701 break;
1702 }
1703 return false;
1704 }
1705
1706 void shift(int whichEdge, int delta) {
1707 for (View v: views) {
1708 CellAndSpan c = config.map.get(v);
1709 switch (whichEdge) {
1710 case LEFT:
1711 c.x -= delta;
1712 break;
1713 case RIGHT:
1714 c.x += delta;
1715 break;
1716 case TOP:
1717 c.y -= delta;
1718 break;
1719 case BOTTOM:
1720 default:
1721 c.y += delta;
1722 break;
1723 }
1724 }
1725 resetEdges();
1726 }
1727
1728 public void addView(View v) {
1729 views.add(v);
1730 resetEdges();
1731 }
1732
1733 public Rect getBoundingRect() {
1734 if (boundingRectDirty) {
1735 boolean first = true;
1736 for (View v: views) {
1737 CellAndSpan c = config.map.get(v);
1738 if (first) {
1739 boundingRect.set(c.x, c.y, c.x + c.spanX, c.y + c.spanY);
1740 first = false;
1741 } else {
1742 boundingRect.union(c.x, c.y, c.x + c.spanX, c.y + c.spanY);
1743 }
1744 }
1745 }
1746 return boundingRect;
1747 }
1748
1749 public int[] getEdge(int which) {
1750 switch (which) {
1751 case LEFT:
1752 return getLeftEdge();
1753 case RIGHT:
1754 return getRightEdge();
1755 case TOP:
1756 return getTopEdge();
1757 case BOTTOM:
1758 default:
1759 return getBottomEdge();
1760 }
1761 }
1762
1763 public int[] getLeftEdge() {
1764 if (leftEdgeDirty) {
1765 computeEdge(LEFT, leftEdge);
1766 }
1767 return leftEdge;
1768 }
1769
1770 public int[] getRightEdge() {
1771 if (rightEdgeDirty) {
1772 computeEdge(RIGHT, rightEdge);
1773 }
1774 return rightEdge;
1775 }
1776
1777 public int[] getTopEdge() {
1778 if (topEdgeDirty) {
1779 computeEdge(TOP, topEdge);
1780 }
1781 return topEdge;
1782 }
1783
1784 public int[] getBottomEdge() {
1785 if (bottomEdgeDirty) {
1786 computeEdge(BOTTOM, bottomEdge);
1787 }
1788 return bottomEdge;
1789 }
1790
1791 PositionComparator comparator = new PositionComparator();
1792 class PositionComparator implements Comparator<View> {
1793 int whichEdge = 0;
1794 public int compare(View left, View right) {
1795 CellAndSpan l = config.map.get(left);
1796 CellAndSpan r = config.map.get(right);
1797 switch (whichEdge) {
1798 case LEFT:
1799 return (r.x + r.spanX) - (l.x + l.spanX);
1800 case RIGHT:
1801 return l.x - r.x;
1802 case TOP:
1803 return (r.y + r.spanY) - (l.y + l.spanY);
1804 case BOTTOM:
1805 default:
1806 return l.y - r.y;
1807 }
1808 }
1809 }
1810
1811 public void sortConfigurationForEdgePush(int edge) {
1812 comparator.whichEdge = edge;
1813 Collections.sort(config.sortedViews, comparator);
1814 }
Adam Cohen47a876d2012-03-19 13:21:41 -07001815 }
1816
Adam Cohenf3900c22012-11-16 18:28:11 -08001817 private boolean pushViewsToTempLocation(ArrayList<View> views, Rect rectOccupiedByPotentialDrop,
1818 int[] direction, View dragView, ItemConfiguration currentState) {
Adam Cohene0489502012-08-27 15:18:53 -07001819
Adam Cohenf3900c22012-11-16 18:28:11 -08001820 ViewCluster cluster = new ViewCluster(views, currentState);
1821 Rect clusterRect = cluster.getBoundingRect();
1822 int whichEdge;
1823 int pushDistance;
1824 boolean fail = false;
1825
1826 // Determine the edge of the cluster that will be leading the push and how far
1827 // the cluster must be shifted.
1828 if (direction[0] < 0) {
1829 whichEdge = ViewCluster.LEFT;
1830 pushDistance = clusterRect.right - rectOccupiedByPotentialDrop.left;
Adam Cohene0489502012-08-27 15:18:53 -07001831 } else if (direction[0] > 0) {
Adam Cohenf3900c22012-11-16 18:28:11 -08001832 whichEdge = ViewCluster.RIGHT;
1833 pushDistance = rectOccupiedByPotentialDrop.right - clusterRect.left;
1834 } else if (direction[1] < 0) {
1835 whichEdge = ViewCluster.TOP;
1836 pushDistance = clusterRect.bottom - rectOccupiedByPotentialDrop.top;
1837 } else {
1838 whichEdge = ViewCluster.BOTTOM;
1839 pushDistance = rectOccupiedByPotentialDrop.bottom - clusterRect.top;
Adam Cohene0489502012-08-27 15:18:53 -07001840 }
1841
Adam Cohenf3900c22012-11-16 18:28:11 -08001842 // Break early for invalid push distance.
1843 if (pushDistance <= 0) {
1844 return false;
Adam Cohene0489502012-08-27 15:18:53 -07001845 }
Adam Cohenf3900c22012-11-16 18:28:11 -08001846
1847 // Mark the occupied state as false for the group of views we want to move.
1848 for (View v: views) {
1849 CellAndSpan c = currentState.map.get(v);
1850 markCellsForView(c.x, c.y, c.spanX, c.spanY, mTmpOccupied, false);
1851 }
1852
1853 // We save the current configuration -- if we fail to find a solution we will revert
1854 // to the initial state. The process of finding a solution modifies the configuration
1855 // in place, hence the need for revert in the failure case.
1856 currentState.save();
1857
1858 // The pushing algorithm is simplified by considering the views in the order in which
1859 // they would be pushed by the cluster. For example, if the cluster is leading with its
1860 // left edge, we consider sort the views by their right edge, from right to left.
1861 cluster.sortConfigurationForEdgePush(whichEdge);
1862
1863 while (pushDistance > 0 && !fail) {
1864 for (View v: currentState.sortedViews) {
1865 // For each view that isn't in the cluster, we see if the leading edge of the
1866 // cluster is contacting the edge of that view. If so, we add that view to the
1867 // cluster.
1868 if (!cluster.views.contains(v) && v != dragView) {
1869 if (cluster.isViewTouchingEdge(v, whichEdge)) {
1870 LayoutParams lp = (LayoutParams) v.getLayoutParams();
1871 if (!lp.canReorder) {
1872 // The push solution includes the all apps button, this is not viable.
1873 fail = true;
1874 break;
1875 }
1876 cluster.addView(v);
1877 CellAndSpan c = currentState.map.get(v);
1878
1879 // Adding view to cluster, mark it as not occupied.
1880 markCellsForView(c.x, c.y, c.spanX, c.spanY, mTmpOccupied, false);
1881 }
1882 }
1883 }
1884 pushDistance--;
1885
1886 // The cluster has been completed, now we move the whole thing over in the appropriate
1887 // direction.
1888 cluster.shift(whichEdge, 1);
1889 }
1890
1891 boolean foundSolution = false;
1892 clusterRect = cluster.getBoundingRect();
1893
1894 // Due to the nature of the algorithm, the only check required to verify a valid solution
1895 // is to ensure that completed shifted cluster lies completely within the cell layout.
1896 if (!fail && clusterRect.left >= 0 && clusterRect.right <= mCountX && clusterRect.top >= 0 &&
1897 clusterRect.bottom <= mCountY) {
1898 foundSolution = true;
1899 } else {
1900 currentState.restore();
1901 }
1902
1903 // In either case, we set the occupied array as marked for the location of the views
1904 for (View v: cluster.views) {
1905 CellAndSpan c = currentState.map.get(v);
1906 markCellsForView(c.x, c.y, c.spanX, c.spanY, mTmpOccupied, true);
1907 }
1908
1909 return foundSolution;
Adam Cohene0489502012-08-27 15:18:53 -07001910 }
1911
Adam Cohen482ed822012-03-02 14:15:13 -08001912 private boolean addViewsToTempLocation(ArrayList<View> views, Rect rectOccupiedByPotentialDrop,
Adam Cohenf3900c22012-11-16 18:28:11 -08001913 int[] direction, View dragView, ItemConfiguration currentState) {
Adam Cohen482ed822012-03-02 14:15:13 -08001914 if (views.size() == 0) return true;
Adam Cohen482ed822012-03-02 14:15:13 -08001915
Adam Cohen8baab352012-03-20 17:39:21 -07001916 boolean success = false;
Adam Cohen482ed822012-03-02 14:15:13 -08001917 Rect boundingRect = null;
Adam Cohen8baab352012-03-20 17:39:21 -07001918 // We construct a rect which represents the entire group of views passed in
Adam Cohen482ed822012-03-02 14:15:13 -08001919 for (View v: views) {
Adam Cohen8baab352012-03-20 17:39:21 -07001920 CellAndSpan c = currentState.map.get(v);
Adam Cohen482ed822012-03-02 14:15:13 -08001921 if (boundingRect == null) {
Adam Cohen8baab352012-03-20 17:39:21 -07001922 boundingRect = new Rect(c.x, c.y, c.x + c.spanX, c.y + c.spanY);
Adam Cohen482ed822012-03-02 14:15:13 -08001923 } else {
Adam Cohen8baab352012-03-20 17:39:21 -07001924 boundingRect.union(c.x, c.y, c.x + c.spanX, c.y + c.spanY);
Adam Cohen482ed822012-03-02 14:15:13 -08001925 }
1926 }
Adam Cohen8baab352012-03-20 17:39:21 -07001927
Adam Cohen8baab352012-03-20 17:39:21 -07001928 // Mark the occupied state as false for the group of views we want to move.
Adam Cohenf3900c22012-11-16 18:28:11 -08001929 for (View v: views) {
Adam Cohen8baab352012-03-20 17:39:21 -07001930 CellAndSpan c = currentState.map.get(v);
1931 markCellsForView(c.x, c.y, c.spanX, c.spanY, mTmpOccupied, false);
1932 }
1933
Adam Cohen47a876d2012-03-19 13:21:41 -07001934 boolean[][] blockOccupied = new boolean[boundingRect.width()][boundingRect.height()];
1935 int top = boundingRect.top;
1936 int left = boundingRect.left;
Adam Cohen8baab352012-03-20 17:39:21 -07001937 // We mark more precisely which parts of the bounding rect are truly occupied, allowing
Adam Cohena56dc102012-07-13 13:41:42 -07001938 // for interlocking.
Adam Cohenf3900c22012-11-16 18:28:11 -08001939 for (View v: views) {
Adam Cohen8baab352012-03-20 17:39:21 -07001940 CellAndSpan c = currentState.map.get(v);
1941 markCellsForView(c.x - left, c.y - top, c.spanX, c.spanY, blockOccupied, true);
Adam Cohen47a876d2012-03-19 13:21:41 -07001942 }
1943
Adam Cohen482ed822012-03-02 14:15:13 -08001944 markCellsForRect(rectOccupiedByPotentialDrop, mTmpOccupied, true);
1945
Adam Cohenf3900c22012-11-16 18:28:11 -08001946 findNearestArea(boundingRect.left, boundingRect.top, boundingRect.width(),
1947 boundingRect.height(), direction, mTmpOccupied, blockOccupied, mTempLocation);
Adam Cohen482ed822012-03-02 14:15:13 -08001948
Adam Cohen8baab352012-03-20 17:39:21 -07001949 // If we successfuly found a location by pushing the block of views, we commit it
Adam Cohen482ed822012-03-02 14:15:13 -08001950 if (mTempLocation[0] >= 0 && mTempLocation[1] >= 0) {
Adam Cohen8baab352012-03-20 17:39:21 -07001951 int deltaX = mTempLocation[0] - boundingRect.left;
1952 int deltaY = mTempLocation[1] - boundingRect.top;
Adam Cohenf3900c22012-11-16 18:28:11 -08001953 for (View v: views) {
Adam Cohen8baab352012-03-20 17:39:21 -07001954 CellAndSpan c = currentState.map.get(v);
1955 c.x += deltaX;
1956 c.y += deltaY;
Adam Cohen482ed822012-03-02 14:15:13 -08001957 }
1958 success = true;
1959 }
Adam Cohen8baab352012-03-20 17:39:21 -07001960
1961 // In either case, we set the occupied array as marked for the location of the views
Adam Cohenf3900c22012-11-16 18:28:11 -08001962 for (View v: views) {
Adam Cohen8baab352012-03-20 17:39:21 -07001963 CellAndSpan c = currentState.map.get(v);
1964 markCellsForView(c.x, c.y, c.spanX, c.spanY, mTmpOccupied, true);
Adam Cohen482ed822012-03-02 14:15:13 -08001965 }
1966 return success;
1967 }
1968
1969 private void markCellsForRect(Rect r, boolean[][] occupied, boolean value) {
1970 markCellsForView(r.left, r.top, r.width(), r.height(), occupied, value);
1971 }
1972
Adam Cohen4abc5bd2012-05-29 21:06:03 -07001973 // This method tries to find a reordering solution which satisfies the push mechanic by trying
1974 // to push items in each of the cardinal directions, in an order based on the direction vector
1975 // passed.
1976 private boolean attemptPushInDirection(ArrayList<View> intersectingViews, Rect occupied,
1977 int[] direction, View ignoreView, ItemConfiguration solution) {
1978 if ((Math.abs(direction[0]) + Math.abs(direction[1])) > 1) {
1979 // If the direction vector has two non-zero components, we try pushing
1980 // separately in each of the components.
1981 int temp = direction[1];
1982 direction[1] = 0;
Adam Cohenf3900c22012-11-16 18:28:11 -08001983
1984 if (pushViewsToTempLocation(intersectingViews, occupied, direction,
Adam Cohen4abc5bd2012-05-29 21:06:03 -07001985 ignoreView, solution)) {
1986 return true;
1987 }
1988 direction[1] = temp;
1989 temp = direction[0];
1990 direction[0] = 0;
Adam Cohenf3900c22012-11-16 18:28:11 -08001991
1992 if (pushViewsToTempLocation(intersectingViews, occupied, direction,
Adam Cohen4abc5bd2012-05-29 21:06:03 -07001993 ignoreView, solution)) {
1994 return true;
1995 }
1996 // Revert the direction
1997 direction[0] = temp;
1998
1999 // Now we try pushing in each component of the opposite direction
2000 direction[0] *= -1;
2001 direction[1] *= -1;
2002 temp = direction[1];
2003 direction[1] = 0;
Adam Cohenf3900c22012-11-16 18:28:11 -08002004 if (pushViewsToTempLocation(intersectingViews, occupied, direction,
Adam Cohen4abc5bd2012-05-29 21:06:03 -07002005 ignoreView, solution)) {
2006 return true;
2007 }
2008
2009 direction[1] = temp;
2010 temp = direction[0];
2011 direction[0] = 0;
Adam Cohenf3900c22012-11-16 18:28:11 -08002012 if (pushViewsToTempLocation(intersectingViews, occupied, direction,
Adam Cohen4abc5bd2012-05-29 21:06:03 -07002013 ignoreView, solution)) {
2014 return true;
2015 }
2016 // revert the direction
2017 direction[0] = temp;
2018 direction[0] *= -1;
2019 direction[1] *= -1;
2020
2021 } else {
2022 // If the direction vector has a single non-zero component, we push first in the
2023 // direction of the vector
Adam Cohenf3900c22012-11-16 18:28:11 -08002024 if (pushViewsToTempLocation(intersectingViews, occupied, direction,
Adam Cohen4abc5bd2012-05-29 21:06:03 -07002025 ignoreView, solution)) {
2026 return true;
2027 }
Adam Cohen4abc5bd2012-05-29 21:06:03 -07002028 // Then we try the opposite direction
2029 direction[0] *= -1;
2030 direction[1] *= -1;
Adam Cohenf3900c22012-11-16 18:28:11 -08002031 if (pushViewsToTempLocation(intersectingViews, occupied, direction,
Adam Cohen4abc5bd2012-05-29 21:06:03 -07002032 ignoreView, solution)) {
2033 return true;
2034 }
2035 // Switch the direction back
2036 direction[0] *= -1;
2037 direction[1] *= -1;
2038
2039 // If we have failed to find a push solution with the above, then we try
2040 // to find a solution by pushing along the perpendicular axis.
2041
2042 // Swap the components
2043 int temp = direction[1];
2044 direction[1] = direction[0];
2045 direction[0] = temp;
Adam Cohenf3900c22012-11-16 18:28:11 -08002046 if (pushViewsToTempLocation(intersectingViews, occupied, direction,
Adam Cohen4abc5bd2012-05-29 21:06:03 -07002047 ignoreView, solution)) {
2048 return true;
2049 }
2050
2051 // Then we try the opposite direction
2052 direction[0] *= -1;
2053 direction[1] *= -1;
Adam Cohenf3900c22012-11-16 18:28:11 -08002054 if (pushViewsToTempLocation(intersectingViews, occupied, direction,
Adam Cohen4abc5bd2012-05-29 21:06:03 -07002055 ignoreView, solution)) {
2056 return true;
2057 }
2058 // Switch the direction back
2059 direction[0] *= -1;
2060 direction[1] *= -1;
2061
2062 // Swap the components back
2063 temp = direction[1];
2064 direction[1] = direction[0];
2065 direction[0] = temp;
2066 }
2067 return false;
2068 }
2069
Adam Cohen482ed822012-03-02 14:15:13 -08002070 private boolean rearrangementExists(int cellX, int cellY, int spanX, int spanY, int[] direction,
Adam Cohen8baab352012-03-20 17:39:21 -07002071 View ignoreView, ItemConfiguration solution) {
Winson Chunge3e03bc2012-05-01 15:10:11 -07002072 // Return early if get invalid cell positions
2073 if (cellX < 0 || cellY < 0) return false;
Adam Cohen482ed822012-03-02 14:15:13 -08002074
Adam Cohen8baab352012-03-20 17:39:21 -07002075 mIntersectingViews.clear();
Adam Cohen482ed822012-03-02 14:15:13 -08002076 mOccupiedRect.set(cellX, cellY, cellX + spanX, cellY + spanY);
Adam Cohen482ed822012-03-02 14:15:13 -08002077
Adam Cohen8baab352012-03-20 17:39:21 -07002078 // Mark the desired location of the view currently being dragged.
Adam Cohen482ed822012-03-02 14:15:13 -08002079 if (ignoreView != null) {
Adam Cohen8baab352012-03-20 17:39:21 -07002080 CellAndSpan c = solution.map.get(ignoreView);
Adam Cohen19f37922012-03-21 11:59:11 -07002081 if (c != null) {
2082 c.x = cellX;
2083 c.y = cellY;
2084 }
Adam Cohen482ed822012-03-02 14:15:13 -08002085 }
Adam Cohen482ed822012-03-02 14:15:13 -08002086 Rect r0 = new Rect(cellX, cellY, cellX + spanX, cellY + spanY);
2087 Rect r1 = new Rect();
Adam Cohen8baab352012-03-20 17:39:21 -07002088 for (View child: solution.map.keySet()) {
Adam Cohen482ed822012-03-02 14:15:13 -08002089 if (child == ignoreView) continue;
Adam Cohen8baab352012-03-20 17:39:21 -07002090 CellAndSpan c = solution.map.get(child);
Adam Cohen482ed822012-03-02 14:15:13 -08002091 LayoutParams lp = (LayoutParams) child.getLayoutParams();
Adam Cohen8baab352012-03-20 17:39:21 -07002092 r1.set(c.x, c.y, c.x + c.spanX, c.y + c.spanY);
Adam Cohen482ed822012-03-02 14:15:13 -08002093 if (Rect.intersects(r0, r1)) {
2094 if (!lp.canReorder) {
2095 return false;
2096 }
2097 mIntersectingViews.add(child);
2098 }
2099 }
Adam Cohen47a876d2012-03-19 13:21:41 -07002100
Adam Cohen4abc5bd2012-05-29 21:06:03 -07002101 // First we try to find a solution which respects the push mechanic. That is,
2102 // we try to find a solution such that no displaced item travels through another item
2103 // without also displacing that item.
2104 if (attemptPushInDirection(mIntersectingViews, mOccupiedRect, direction, ignoreView,
Adam Cohen19f37922012-03-21 11:59:11 -07002105 solution)) {
Adam Cohen47a876d2012-03-19 13:21:41 -07002106 return true;
2107 }
Adam Cohen47a876d2012-03-19 13:21:41 -07002108
Adam Cohen4abc5bd2012-05-29 21:06:03 -07002109 // Next we try moving the views as a block, but without requiring the push mechanic.
Adam Cohenf3900c22012-11-16 18:28:11 -08002110 if (addViewsToTempLocation(mIntersectingViews, mOccupiedRect, direction, ignoreView,
Adam Cohen19f37922012-03-21 11:59:11 -07002111 solution)) {
Adam Cohen482ed822012-03-02 14:15:13 -08002112 return true;
2113 }
Adam Cohen47a876d2012-03-19 13:21:41 -07002114
Adam Cohen482ed822012-03-02 14:15:13 -08002115 // Ok, they couldn't move as a block, let's move them individually
2116 for (View v : mIntersectingViews) {
Adam Cohen8baab352012-03-20 17:39:21 -07002117 if (!addViewToTempLocation(v, mOccupiedRect, direction, solution)) {
Adam Cohen482ed822012-03-02 14:15:13 -08002118 return false;
2119 }
2120 }
2121 return true;
2122 }
2123
2124 /*
2125 * Returns a pair (x, y), where x,y are in {-1, 0, 1} corresponding to vector between
2126 * the provided point and the provided cell
2127 */
Adam Cohen47a876d2012-03-19 13:21:41 -07002128 private void computeDirectionVector(float deltaX, float deltaY, int[] result) {
Adam Cohen482ed822012-03-02 14:15:13 -08002129 double angle = Math.atan(((float) deltaY) / deltaX);
2130
2131 result[0] = 0;
2132 result[1] = 0;
2133 if (Math.abs(Math.cos(angle)) > 0.5f) {
2134 result[0] = (int) Math.signum(deltaX);
2135 }
2136 if (Math.abs(Math.sin(angle)) > 0.5f) {
2137 result[1] = (int) Math.signum(deltaY);
2138 }
2139 }
2140
Adam Cohen8baab352012-03-20 17:39:21 -07002141 private void copyOccupiedArray(boolean[][] occupied) {
2142 for (int i = 0; i < mCountX; i++) {
2143 for (int j = 0; j < mCountY; j++) {
2144 occupied[i][j] = mOccupied[i][j];
2145 }
2146 }
2147 }
2148
Adam Cohen482ed822012-03-02 14:15:13 -08002149 ItemConfiguration simpleSwap(int pixelX, int pixelY, int minSpanX, int minSpanY, int spanX,
2150 int spanY, int[] direction, View dragView, boolean decX, ItemConfiguration solution) {
Adam Cohen8baab352012-03-20 17:39:21 -07002151 // Copy the current state into the solution. This solution will be manipulated as necessary.
2152 copyCurrentStateToSolution(solution, false);
2153 // Copy the current occupied array into the temporary occupied array. This array will be
2154 // manipulated as necessary to find a solution.
2155 copyOccupiedArray(mTmpOccupied);
Adam Cohen482ed822012-03-02 14:15:13 -08002156
2157 // We find the nearest cell into which we would place the dragged item, assuming there's
2158 // nothing in its way.
2159 int result[] = new int[2];
2160 result = findNearestArea(pixelX, pixelY, spanX, spanY, result);
2161
2162 boolean success = false;
2163 // First we try the exact nearest position of the item being dragged,
2164 // we will then want to try to move this around to other neighbouring positions
Adam Cohen8baab352012-03-20 17:39:21 -07002165 success = rearrangementExists(result[0], result[1], spanX, spanY, direction, dragView,
2166 solution);
Adam Cohen482ed822012-03-02 14:15:13 -08002167
2168 if (!success) {
2169 // We try shrinking the widget down to size in an alternating pattern, shrink 1 in
2170 // x, then 1 in y etc.
2171 if (spanX > minSpanX && (minSpanY == spanY || decX)) {
2172 return simpleSwap(pixelX, pixelY, minSpanX, minSpanY, spanX - 1, spanY, direction,
2173 dragView, false, solution);
2174 } else if (spanY > minSpanY) {
2175 return simpleSwap(pixelX, pixelY, minSpanX, minSpanY, spanX, spanY - 1, direction,
2176 dragView, true, solution);
2177 }
2178 solution.isSolution = false;
2179 } else {
2180 solution.isSolution = true;
2181 solution.dragViewX = result[0];
2182 solution.dragViewY = result[1];
2183 solution.dragViewSpanX = spanX;
2184 solution.dragViewSpanY = spanY;
Adam Cohen482ed822012-03-02 14:15:13 -08002185 }
2186 return solution;
2187 }
2188
2189 private void copyCurrentStateToSolution(ItemConfiguration solution, boolean temp) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07002190 int childCount = mShortcutsAndWidgets.getChildCount();
Adam Cohen482ed822012-03-02 14:15:13 -08002191 for (int i = 0; i < childCount; i++) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07002192 View child = mShortcutsAndWidgets.getChildAt(i);
Adam Cohen482ed822012-03-02 14:15:13 -08002193 LayoutParams lp = (LayoutParams) child.getLayoutParams();
Adam Cohen8baab352012-03-20 17:39:21 -07002194 CellAndSpan c;
Adam Cohen482ed822012-03-02 14:15:13 -08002195 if (temp) {
Adam Cohen8baab352012-03-20 17:39:21 -07002196 c = new CellAndSpan(lp.tmpCellX, lp.tmpCellY, lp.cellHSpan, lp.cellVSpan);
Adam Cohen482ed822012-03-02 14:15:13 -08002197 } else {
Adam Cohen8baab352012-03-20 17:39:21 -07002198 c = new CellAndSpan(lp.cellX, lp.cellY, lp.cellHSpan, lp.cellVSpan);
Adam Cohen482ed822012-03-02 14:15:13 -08002199 }
Adam Cohenf3900c22012-11-16 18:28:11 -08002200 solution.add(child, c);
Adam Cohen482ed822012-03-02 14:15:13 -08002201 }
2202 }
2203
2204 private void copySolutionToTempState(ItemConfiguration solution, View dragView) {
2205 for (int i = 0; i < mCountX; i++) {
2206 for (int j = 0; j < mCountY; j++) {
2207 mTmpOccupied[i][j] = false;
2208 }
2209 }
2210
Michael Jurkaa52570f2012-03-20 03:18:20 -07002211 int childCount = mShortcutsAndWidgets.getChildCount();
Adam Cohen482ed822012-03-02 14:15:13 -08002212 for (int i = 0; i < childCount; i++) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07002213 View child = mShortcutsAndWidgets.getChildAt(i);
Adam Cohen482ed822012-03-02 14:15:13 -08002214 if (child == dragView) continue;
2215 LayoutParams lp = (LayoutParams) child.getLayoutParams();
Adam Cohen8baab352012-03-20 17:39:21 -07002216 CellAndSpan c = solution.map.get(child);
2217 if (c != null) {
2218 lp.tmpCellX = c.x;
2219 lp.tmpCellY = c.y;
2220 lp.cellHSpan = c.spanX;
2221 lp.cellVSpan = c.spanY;
2222 markCellsForView(c.x, c.y, c.spanX, c.spanY, mTmpOccupied, true);
Adam Cohen482ed822012-03-02 14:15:13 -08002223 }
2224 }
2225 markCellsForView(solution.dragViewX, solution.dragViewY, solution.dragViewSpanX,
2226 solution.dragViewSpanY, mTmpOccupied, true);
2227 }
2228
2229 private void animateItemsToSolution(ItemConfiguration solution, View dragView, boolean
2230 commitDragView) {
2231
2232 boolean[][] occupied = DESTRUCTIVE_REORDER ? mOccupied : mTmpOccupied;
2233 for (int i = 0; i < mCountX; i++) {
2234 for (int j = 0; j < mCountY; j++) {
2235 occupied[i][j] = false;
2236 }
2237 }
2238
Michael Jurkaa52570f2012-03-20 03:18:20 -07002239 int childCount = mShortcutsAndWidgets.getChildCount();
Adam Cohen482ed822012-03-02 14:15:13 -08002240 for (int i = 0; i < childCount; i++) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07002241 View child = mShortcutsAndWidgets.getChildAt(i);
Adam Cohen482ed822012-03-02 14:15:13 -08002242 if (child == dragView) continue;
Adam Cohen8baab352012-03-20 17:39:21 -07002243 CellAndSpan c = solution.map.get(child);
2244 if (c != null) {
Adam Cohen19f37922012-03-21 11:59:11 -07002245 animateChildToPosition(child, c.x, c.y, REORDER_ANIMATION_DURATION, 0,
2246 DESTRUCTIVE_REORDER, false);
Adam Cohen8baab352012-03-20 17:39:21 -07002247 markCellsForView(c.x, c.y, c.spanX, c.spanY, occupied, true);
Adam Cohen482ed822012-03-02 14:15:13 -08002248 }
2249 }
2250 if (commitDragView) {
2251 markCellsForView(solution.dragViewX, solution.dragViewY, solution.dragViewSpanX,
2252 solution.dragViewSpanY, occupied, true);
2253 }
2254 }
2255
Adam Cohen19f37922012-03-21 11:59:11 -07002256 // This method starts or changes the reorder hint animations
2257 private void beginOrAdjustHintAnimations(ItemConfiguration solution, View dragView, int delay) {
2258 int childCount = mShortcutsAndWidgets.getChildCount();
Adam Cohen19f37922012-03-21 11:59:11 -07002259 for (int i = 0; i < childCount; i++) {
2260 View child = mShortcutsAndWidgets.getChildAt(i);
2261 if (child == dragView) continue;
2262 CellAndSpan c = solution.map.get(child);
2263 LayoutParams lp = (LayoutParams) child.getLayoutParams();
2264 if (c != null) {
2265 ReorderHintAnimation rha = new ReorderHintAnimation(child, lp.cellX, lp.cellY,
2266 c.x, c.y, c.spanX, c.spanY);
Adam Cohend024f982012-05-23 18:26:45 -07002267 rha.animate();
Adam Cohen19f37922012-03-21 11:59:11 -07002268 }
2269 }
2270 }
2271
2272 // Class which represents the reorder hint animations. These animations show that an item is
2273 // in a temporary state, and hint at where the item will return to.
2274 class ReorderHintAnimation {
2275 View child;
Adam Cohend024f982012-05-23 18:26:45 -07002276 float finalDeltaX;
2277 float finalDeltaY;
2278 float initDeltaX;
2279 float initDeltaY;
2280 float finalScale;
2281 float initScale;
Brandon Keely50e6e562012-05-08 16:28:49 -07002282 private static final int DURATION = 300;
Adam Cohene7587d22012-05-24 18:50:02 -07002283 Animator a;
Adam Cohen19f37922012-03-21 11:59:11 -07002284
2285 public ReorderHintAnimation(View child, int cellX0, int cellY0, int cellX1, int cellY1,
2286 int spanX, int spanY) {
2287 regionToCenterPoint(cellX0, cellY0, spanX, spanY, mTmpPoint);
2288 final int x0 = mTmpPoint[0];
2289 final int y0 = mTmpPoint[1];
2290 regionToCenterPoint(cellX1, cellY1, spanX, spanY, mTmpPoint);
2291 final int x1 = mTmpPoint[0];
2292 final int y1 = mTmpPoint[1];
2293 final int dX = x1 - x0;
2294 final int dY = y1 - y0;
Adam Cohend024f982012-05-23 18:26:45 -07002295 finalDeltaX = 0;
2296 finalDeltaY = 0;
Adam Cohen19f37922012-03-21 11:59:11 -07002297 if (dX == dY && dX == 0) {
2298 } else {
2299 if (dY == 0) {
Adam Cohend024f982012-05-23 18:26:45 -07002300 finalDeltaX = - Math.signum(dX) * mReorderHintAnimationMagnitude;
Adam Cohen19f37922012-03-21 11:59:11 -07002301 } else if (dX == 0) {
Adam Cohend024f982012-05-23 18:26:45 -07002302 finalDeltaY = - Math.signum(dY) * mReorderHintAnimationMagnitude;
Adam Cohen19f37922012-03-21 11:59:11 -07002303 } else {
2304 double angle = Math.atan( (float) (dY) / dX);
Adam Cohend024f982012-05-23 18:26:45 -07002305 finalDeltaX = (int) (- Math.signum(dX) *
Adam Cohenfe41ac62012-05-23 14:00:37 -07002306 Math.abs(Math.cos(angle) * mReorderHintAnimationMagnitude));
Adam Cohend024f982012-05-23 18:26:45 -07002307 finalDeltaY = (int) (- Math.signum(dY) *
Adam Cohenfe41ac62012-05-23 14:00:37 -07002308 Math.abs(Math.sin(angle) * mReorderHintAnimationMagnitude));
Adam Cohen19f37922012-03-21 11:59:11 -07002309 }
2310 }
Adam Cohend024f982012-05-23 18:26:45 -07002311 initDeltaX = child.getTranslationX();
2312 initDeltaY = child.getTranslationY();
Adam Cohen307fe232012-08-16 17:55:58 -07002313 finalScale = getChildrenScale() - 4.0f / child.getWidth();
Adam Cohend024f982012-05-23 18:26:45 -07002314 initScale = child.getScaleX();
Adam Cohen19f37922012-03-21 11:59:11 -07002315 this.child = child;
2316 }
2317
Adam Cohend024f982012-05-23 18:26:45 -07002318 void animate() {
Adam Cohen19f37922012-03-21 11:59:11 -07002319 if (mShakeAnimators.containsKey(child)) {
2320 ReorderHintAnimation oldAnimation = mShakeAnimators.get(child);
Adam Cohend024f982012-05-23 18:26:45 -07002321 oldAnimation.cancel();
Adam Cohen19f37922012-03-21 11:59:11 -07002322 mShakeAnimators.remove(child);
Adam Cohene7587d22012-05-24 18:50:02 -07002323 if (finalDeltaX == 0 && finalDeltaY == 0) {
2324 completeAnimationImmediately();
2325 return;
2326 }
Adam Cohen19f37922012-03-21 11:59:11 -07002327 }
Adam Cohend024f982012-05-23 18:26:45 -07002328 if (finalDeltaX == 0 && finalDeltaY == 0) {
Adam Cohen19f37922012-03-21 11:59:11 -07002329 return;
2330 }
Michael Jurka2ecf9952012-06-18 12:52:28 -07002331 ValueAnimator va = LauncherAnimUtils.ofFloat(0f, 1f);
Adam Cohene7587d22012-05-24 18:50:02 -07002332 a = va;
Adam Cohen19f37922012-03-21 11:59:11 -07002333 va.setRepeatMode(ValueAnimator.REVERSE);
2334 va.setRepeatCount(ValueAnimator.INFINITE);
Adam Cohen7bdfc972012-05-22 16:50:35 -07002335 va.setDuration(DURATION);
Adam Cohend024f982012-05-23 18:26:45 -07002336 va.setStartDelay((int) (Math.random() * 60));
Adam Cohen19f37922012-03-21 11:59:11 -07002337 va.addUpdateListener(new AnimatorUpdateListener() {
2338 @Override
2339 public void onAnimationUpdate(ValueAnimator animation) {
2340 float r = ((Float) animation.getAnimatedValue()).floatValue();
Adam Cohend024f982012-05-23 18:26:45 -07002341 float x = r * finalDeltaX + (1 - r) * initDeltaX;
2342 float y = r * finalDeltaY + (1 - r) * initDeltaY;
Adam Cohen19f37922012-03-21 11:59:11 -07002343 child.setTranslationX(x);
2344 child.setTranslationY(y);
Adam Cohend024f982012-05-23 18:26:45 -07002345 float s = r * finalScale + (1 - r) * initScale;
Brandon Keely50e6e562012-05-08 16:28:49 -07002346 child.setScaleX(s);
2347 child.setScaleY(s);
Adam Cohen19f37922012-03-21 11:59:11 -07002348 }
2349 });
2350 va.addListener(new AnimatorListenerAdapter() {
2351 public void onAnimationRepeat(Animator animation) {
Adam Cohen19f37922012-03-21 11:59:11 -07002352 // We make sure to end only after a full period
Adam Cohend024f982012-05-23 18:26:45 -07002353 initDeltaX = 0;
2354 initDeltaY = 0;
Adam Cohen307fe232012-08-16 17:55:58 -07002355 initScale = getChildrenScale();
Adam Cohen19f37922012-03-21 11:59:11 -07002356 }
2357 });
Adam Cohen19f37922012-03-21 11:59:11 -07002358 mShakeAnimators.put(child, this);
2359 va.start();
2360 }
2361
Adam Cohend024f982012-05-23 18:26:45 -07002362 private void cancel() {
Adam Cohene7587d22012-05-24 18:50:02 -07002363 if (a != null) {
2364 a.cancel();
2365 }
Adam Cohen19f37922012-03-21 11:59:11 -07002366 }
Adam Cohene7587d22012-05-24 18:50:02 -07002367
Brandon Keely50e6e562012-05-08 16:28:49 -07002368 private void completeAnimationImmediately() {
Adam Cohene7587d22012-05-24 18:50:02 -07002369 if (a != null) {
2370 a.cancel();
2371 }
Brandon Keely50e6e562012-05-08 16:28:49 -07002372
Michael Jurka2ecf9952012-06-18 12:52:28 -07002373 AnimatorSet s = LauncherAnimUtils.createAnimatorSet();
Adam Cohene7587d22012-05-24 18:50:02 -07002374 a = s;
Brandon Keely50e6e562012-05-08 16:28:49 -07002375 s.playTogether(
Adam Cohen307fe232012-08-16 17:55:58 -07002376 LauncherAnimUtils.ofFloat(child, "scaleX", getChildrenScale()),
2377 LauncherAnimUtils.ofFloat(child, "scaleY", getChildrenScale()),
Michael Jurka2ecf9952012-06-18 12:52:28 -07002378 LauncherAnimUtils.ofFloat(child, "translationX", 0f),
2379 LauncherAnimUtils.ofFloat(child, "translationY", 0f)
Brandon Keely50e6e562012-05-08 16:28:49 -07002380 );
2381 s.setDuration(REORDER_ANIMATION_DURATION);
2382 s.setInterpolator(new android.view.animation.DecelerateInterpolator(1.5f));
2383 s.start();
2384 }
Adam Cohen19f37922012-03-21 11:59:11 -07002385 }
2386
2387 private void completeAndClearReorderHintAnimations() {
2388 for (ReorderHintAnimation a: mShakeAnimators.values()) {
Brandon Keely50e6e562012-05-08 16:28:49 -07002389 a.completeAnimationImmediately();
Adam Cohen19f37922012-03-21 11:59:11 -07002390 }
2391 mShakeAnimators.clear();
2392 }
2393
Adam Cohen482ed822012-03-02 14:15:13 -08002394 private void commitTempPlacement() {
2395 for (int i = 0; i < mCountX; i++) {
2396 for (int j = 0; j < mCountY; j++) {
2397 mOccupied[i][j] = mTmpOccupied[i][j];
2398 }
2399 }
Michael Jurkaa52570f2012-03-20 03:18:20 -07002400 int childCount = mShortcutsAndWidgets.getChildCount();
Adam Cohen482ed822012-03-02 14:15:13 -08002401 for (int i = 0; i < childCount; i++) {
Adam Cohenea889a22012-03-27 16:45:39 -07002402 View child = mShortcutsAndWidgets.getChildAt(i);
2403 LayoutParams lp = (LayoutParams) child.getLayoutParams();
2404 ItemInfo info = (ItemInfo) child.getTag();
Adam Cohen2acce882012-03-28 19:03:19 -07002405 // We do a null check here because the item info can be null in the case of the
2406 // AllApps button in the hotseat.
2407 if (info != null) {
Adam Cohen487f7dd2012-06-28 18:12:10 -07002408 if (info.cellX != lp.tmpCellX || info.cellY != lp.tmpCellY ||
2409 info.spanX != lp.cellHSpan || info.spanY != lp.cellVSpan) {
2410 info.requiresDbUpdate = true;
2411 }
Adam Cohen2acce882012-03-28 19:03:19 -07002412 info.cellX = lp.cellX = lp.tmpCellX;
2413 info.cellY = lp.cellY = lp.tmpCellY;
Adam Cohenbebf0422012-04-11 18:06:28 -07002414 info.spanX = lp.cellHSpan;
2415 info.spanY = lp.cellVSpan;
Adam Cohen2acce882012-03-28 19:03:19 -07002416 }
Adam Cohen482ed822012-03-02 14:15:13 -08002417 }
Adam Cohen2acce882012-03-28 19:03:19 -07002418 mLauncher.getWorkspace().updateItemLocationsInDatabase(this);
Adam Cohen482ed822012-03-02 14:15:13 -08002419 }
2420
2421 public void setUseTempCoords(boolean useTempCoords) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07002422 int childCount = mShortcutsAndWidgets.getChildCount();
Adam Cohen482ed822012-03-02 14:15:13 -08002423 for (int i = 0; i < childCount; i++) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07002424 LayoutParams lp = (LayoutParams) mShortcutsAndWidgets.getChildAt(i).getLayoutParams();
Adam Cohen482ed822012-03-02 14:15:13 -08002425 lp.useTmpCoords = useTempCoords;
2426 }
2427 }
2428
Adam Cohen482ed822012-03-02 14:15:13 -08002429 ItemConfiguration findConfigurationNoShuffle(int pixelX, int pixelY, int minSpanX, int minSpanY,
2430 int spanX, int spanY, View dragView, ItemConfiguration solution) {
2431 int[] result = new int[2];
2432 int[] resultSpan = new int[2];
2433 findNearestVacantArea(pixelX, pixelY, minSpanX, minSpanY, spanX, spanY, null, result,
2434 resultSpan);
2435 if (result[0] >= 0 && result[1] >= 0) {
2436 copyCurrentStateToSolution(solution, false);
2437 solution.dragViewX = result[0];
2438 solution.dragViewY = result[1];
2439 solution.dragViewSpanX = resultSpan[0];
2440 solution.dragViewSpanY = resultSpan[1];
2441 solution.isSolution = true;
2442 } else {
2443 solution.isSolution = false;
2444 }
2445 return solution;
2446 }
2447
2448 public void prepareChildForDrag(View child) {
2449 markCellsAsUnoccupiedForView(child);
Adam Cohen482ed822012-03-02 14:15:13 -08002450 }
2451
Adam Cohen19f37922012-03-21 11:59:11 -07002452 /* This seems like it should be obvious and straight-forward, but when the direction vector
2453 needs to match with the notion of the dragView pushing other views, we have to employ
2454 a slightly more subtle notion of the direction vector. The question is what two points is
2455 the vector between? The center of the dragView and its desired destination? Not quite, as
2456 this doesn't necessarily coincide with the interaction of the dragView and items occupying
2457 those cells. Instead we use some heuristics to often lock the vector to up, down, left
2458 or right, which helps make pushing feel right.
2459 */
2460 private void getDirectionVectorForDrop(int dragViewCenterX, int dragViewCenterY, int spanX,
2461 int spanY, View dragView, int[] resultDirection) {
2462 int[] targetDestination = new int[2];
2463
2464 findNearestArea(dragViewCenterX, dragViewCenterY, spanX, spanY, targetDestination);
2465 Rect dragRect = new Rect();
2466 regionToRect(targetDestination[0], targetDestination[1], spanX, spanY, dragRect);
2467 dragRect.offset(dragViewCenterX - dragRect.centerX(), dragViewCenterY - dragRect.centerY());
2468
2469 Rect dropRegionRect = new Rect();
2470 getViewsIntersectingRegion(targetDestination[0], targetDestination[1], spanX, spanY,
2471 dragView, dropRegionRect, mIntersectingViews);
2472
2473 int dropRegionSpanX = dropRegionRect.width();
2474 int dropRegionSpanY = dropRegionRect.height();
2475
2476 regionToRect(dropRegionRect.left, dropRegionRect.top, dropRegionRect.width(),
2477 dropRegionRect.height(), dropRegionRect);
2478
2479 int deltaX = (dropRegionRect.centerX() - dragViewCenterX) / spanX;
2480 int deltaY = (dropRegionRect.centerY() - dragViewCenterY) / spanY;
2481
2482 if (dropRegionSpanX == mCountX || spanX == mCountX) {
2483 deltaX = 0;
2484 }
2485 if (dropRegionSpanY == mCountY || spanY == mCountY) {
2486 deltaY = 0;
2487 }
2488
2489 if (deltaX == 0 && deltaY == 0) {
2490 // No idea what to do, give a random direction.
2491 resultDirection[0] = 1;
2492 resultDirection[1] = 0;
2493 } else {
2494 computeDirectionVector(deltaX, deltaY, resultDirection);
2495 }
2496 }
2497
2498 // For a given cell and span, fetch the set of views intersecting the region.
2499 private void getViewsIntersectingRegion(int cellX, int cellY, int spanX, int spanY,
2500 View dragView, Rect boundingRect, ArrayList<View> intersectingViews) {
2501 if (boundingRect != null) {
2502 boundingRect.set(cellX, cellY, cellX + spanX, cellY + spanY);
2503 }
2504 intersectingViews.clear();
2505 Rect r0 = new Rect(cellX, cellY, cellX + spanX, cellY + spanY);
2506 Rect r1 = new Rect();
2507 final int count = mShortcutsAndWidgets.getChildCount();
2508 for (int i = 0; i < count; i++) {
2509 View child = mShortcutsAndWidgets.getChildAt(i);
2510 if (child == dragView) continue;
2511 LayoutParams lp = (LayoutParams) child.getLayoutParams();
2512 r1.set(lp.cellX, lp.cellY, lp.cellX + lp.cellHSpan, lp.cellY + lp.cellVSpan);
2513 if (Rect.intersects(r0, r1)) {
2514 mIntersectingViews.add(child);
2515 if (boundingRect != null) {
2516 boundingRect.union(r1);
2517 }
2518 }
2519 }
2520 }
2521
2522 boolean isNearestDropLocationOccupied(int pixelX, int pixelY, int spanX, int spanY,
2523 View dragView, int[] result) {
2524 result = findNearestArea(pixelX, pixelY, spanX, spanY, result);
2525 getViewsIntersectingRegion(result[0], result[1], spanX, spanY, dragView, null,
2526 mIntersectingViews);
2527 return !mIntersectingViews.isEmpty();
2528 }
2529
2530 void revertTempState() {
2531 if (!isItemPlacementDirty() || DESTRUCTIVE_REORDER) return;
2532 final int count = mShortcutsAndWidgets.getChildCount();
2533 for (int i = 0; i < count; i++) {
2534 View child = mShortcutsAndWidgets.getChildAt(i);
2535 LayoutParams lp = (LayoutParams) child.getLayoutParams();
2536 if (lp.tmpCellX != lp.cellX || lp.tmpCellY != lp.cellY) {
2537 lp.tmpCellX = lp.cellX;
2538 lp.tmpCellY = lp.cellY;
2539 animateChildToPosition(child, lp.cellX, lp.cellY, REORDER_ANIMATION_DURATION,
2540 0, false, false);
2541 }
2542 }
2543 completeAndClearReorderHintAnimations();
2544 setItemPlacementDirty(false);
2545 }
2546
Adam Cohenbebf0422012-04-11 18:06:28 -07002547 boolean createAreaForResize(int cellX, int cellY, int spanX, int spanY,
2548 View dragView, int[] direction, boolean commit) {
2549 int[] pixelXY = new int[2];
2550 regionToCenterPoint(cellX, cellY, spanX, spanY, pixelXY);
2551
2552 // First we determine if things have moved enough to cause a different layout
2553 ItemConfiguration swapSolution = simpleSwap(pixelXY[0], pixelXY[1], spanX, spanY,
2554 spanX, spanY, direction, dragView, true, new ItemConfiguration());
2555
2556 setUseTempCoords(true);
2557 if (swapSolution != null && swapSolution.isSolution) {
2558 // If we're just testing for a possible location (MODE_ACCEPT_DROP), we don't bother
2559 // committing anything or animating anything as we just want to determine if a solution
2560 // exists
2561 copySolutionToTempState(swapSolution, dragView);
2562 setItemPlacementDirty(true);
2563 animateItemsToSolution(swapSolution, dragView, commit);
2564
2565 if (commit) {
2566 commitTempPlacement();
2567 completeAndClearReorderHintAnimations();
2568 setItemPlacementDirty(false);
2569 } else {
2570 beginOrAdjustHintAnimations(swapSolution, dragView,
2571 REORDER_ANIMATION_DURATION);
2572 }
2573 mShortcutsAndWidgets.requestLayout();
2574 }
2575 return swapSolution.isSolution;
2576 }
2577
Adam Cohen482ed822012-03-02 14:15:13 -08002578 int[] createArea(int pixelX, int pixelY, int minSpanX, int minSpanY, int spanX, int spanY,
2579 View dragView, int[] result, int resultSpan[], int mode) {
Adam Cohen482ed822012-03-02 14:15:13 -08002580 // First we determine if things have moved enough to cause a different layout
Adam Cohen47a876d2012-03-19 13:21:41 -07002581 result = findNearestArea(pixelX, pixelY, spanX, spanY, result);
Adam Cohen482ed822012-03-02 14:15:13 -08002582
2583 if (resultSpan == null) {
2584 resultSpan = new int[2];
2585 }
2586
Adam Cohen19f37922012-03-21 11:59:11 -07002587 // When we are checking drop validity or actually dropping, we don't recompute the
2588 // direction vector, since we want the solution to match the preview, and it's possible
2589 // that the exact position of the item has changed to result in a new reordering outcome.
Adam Cohenb209e632012-03-27 17:09:36 -07002590 if ((mode == MODE_ON_DROP || mode == MODE_ON_DROP_EXTERNAL || mode == MODE_ACCEPT_DROP)
2591 && mPreviousReorderDirection[0] != INVALID_DIRECTION) {
Adam Cohen19f37922012-03-21 11:59:11 -07002592 mDirectionVector[0] = mPreviousReorderDirection[0];
2593 mDirectionVector[1] = mPreviousReorderDirection[1];
2594 // We reset this vector after drop
Adam Cohenb209e632012-03-27 17:09:36 -07002595 if (mode == MODE_ON_DROP || mode == MODE_ON_DROP_EXTERNAL) {
2596 mPreviousReorderDirection[0] = INVALID_DIRECTION;
2597 mPreviousReorderDirection[1] = INVALID_DIRECTION;
Adam Cohen19f37922012-03-21 11:59:11 -07002598 }
2599 } else {
2600 getDirectionVectorForDrop(pixelX, pixelY, spanX, spanY, dragView, mDirectionVector);
2601 mPreviousReorderDirection[0] = mDirectionVector[0];
2602 mPreviousReorderDirection[1] = mDirectionVector[1];
2603 }
2604
Adam Cohen482ed822012-03-02 14:15:13 -08002605 ItemConfiguration swapSolution = simpleSwap(pixelX, pixelY, minSpanX, minSpanY,
2606 spanX, spanY, mDirectionVector, dragView, true, new ItemConfiguration());
2607
2608 // We attempt the approach which doesn't shuffle views at all
2609 ItemConfiguration noShuffleSolution = findConfigurationNoShuffle(pixelX, pixelY, minSpanX,
2610 minSpanY, spanX, spanY, dragView, new ItemConfiguration());
2611
2612 ItemConfiguration finalSolution = null;
2613 if (swapSolution.isSolution && swapSolution.area() >= noShuffleSolution.area()) {
2614 finalSolution = swapSolution;
2615 } else if (noShuffleSolution.isSolution) {
2616 finalSolution = noShuffleSolution;
2617 }
2618
2619 boolean foundSolution = true;
2620 if (!DESTRUCTIVE_REORDER) {
2621 setUseTempCoords(true);
2622 }
2623
2624 if (finalSolution != null) {
2625 result[0] = finalSolution.dragViewX;
2626 result[1] = finalSolution.dragViewY;
2627 resultSpan[0] = finalSolution.dragViewSpanX;
2628 resultSpan[1] = finalSolution.dragViewSpanY;
2629
2630 // If we're just testing for a possible location (MODE_ACCEPT_DROP), we don't bother
2631 // committing anything or animating anything as we just want to determine if a solution
2632 // exists
2633 if (mode == MODE_DRAG_OVER || mode == MODE_ON_DROP || mode == MODE_ON_DROP_EXTERNAL) {
2634 if (!DESTRUCTIVE_REORDER) {
2635 copySolutionToTempState(finalSolution, dragView);
2636 }
2637 setItemPlacementDirty(true);
2638 animateItemsToSolution(finalSolution, dragView, mode == MODE_ON_DROP);
2639
Adam Cohen19f37922012-03-21 11:59:11 -07002640 if (!DESTRUCTIVE_REORDER &&
2641 (mode == MODE_ON_DROP || mode == MODE_ON_DROP_EXTERNAL)) {
Adam Cohen482ed822012-03-02 14:15:13 -08002642 commitTempPlacement();
Adam Cohen19f37922012-03-21 11:59:11 -07002643 completeAndClearReorderHintAnimations();
2644 setItemPlacementDirty(false);
2645 } else {
2646 beginOrAdjustHintAnimations(finalSolution, dragView,
2647 REORDER_ANIMATION_DURATION);
Adam Cohen482ed822012-03-02 14:15:13 -08002648 }
2649 }
2650 } else {
2651 foundSolution = false;
2652 result[0] = result[1] = resultSpan[0] = resultSpan[1] = -1;
2653 }
2654
2655 if ((mode == MODE_ON_DROP || !foundSolution) && !DESTRUCTIVE_REORDER) {
2656 setUseTempCoords(false);
2657 }
Adam Cohen482ed822012-03-02 14:15:13 -08002658
Michael Jurkaa52570f2012-03-20 03:18:20 -07002659 mShortcutsAndWidgets.requestLayout();
Adam Cohen482ed822012-03-02 14:15:13 -08002660 return result;
2661 }
2662
Adam Cohen19f37922012-03-21 11:59:11 -07002663 void setItemPlacementDirty(boolean dirty) {
2664 mItemPlacementDirty = dirty;
Adam Cohen482ed822012-03-02 14:15:13 -08002665 }
Adam Cohen19f37922012-03-21 11:59:11 -07002666 boolean isItemPlacementDirty() {
2667 return mItemPlacementDirty;
Adam Cohen482ed822012-03-02 14:15:13 -08002668 }
2669
2670 private class ItemConfiguration {
Adam Cohen8baab352012-03-20 17:39:21 -07002671 HashMap<View, CellAndSpan> map = new HashMap<View, CellAndSpan>();
Adam Cohenf3900c22012-11-16 18:28:11 -08002672 private HashMap<View, CellAndSpan> savedMap = new HashMap<View, CellAndSpan>();
2673 ArrayList<View> sortedViews = new ArrayList<View>();
Adam Cohen482ed822012-03-02 14:15:13 -08002674 boolean isSolution = false;
2675 int dragViewX, dragViewY, dragViewSpanX, dragViewSpanY;
2676
Adam Cohenf3900c22012-11-16 18:28:11 -08002677 void save() {
2678 // Copy current state into savedMap
2679 for (View v: map.keySet()) {
2680 map.get(v).copy(savedMap.get(v));
2681 }
2682 }
2683
2684 void restore() {
2685 // Restore current state from savedMap
2686 for (View v: savedMap.keySet()) {
2687 savedMap.get(v).copy(map.get(v));
2688 }
2689 }
2690
2691 void add(View v, CellAndSpan cs) {
2692 map.put(v, cs);
2693 savedMap.put(v, new CellAndSpan());
2694 sortedViews.add(v);
2695 }
2696
Adam Cohen482ed822012-03-02 14:15:13 -08002697 int area() {
2698 return dragViewSpanX * dragViewSpanY;
2699 }
Adam Cohen8baab352012-03-20 17:39:21 -07002700 }
2701
2702 private class CellAndSpan {
2703 int x, y;
2704 int spanX, spanY;
2705
Adam Cohenf3900c22012-11-16 18:28:11 -08002706 public CellAndSpan() {
2707 }
2708
2709 public void copy(CellAndSpan copy) {
2710 copy.x = x;
2711 copy.y = y;
2712 copy.spanX = spanX;
2713 copy.spanY = spanY;
2714 }
2715
Adam Cohen8baab352012-03-20 17:39:21 -07002716 public CellAndSpan(int x, int y, int spanX, int spanY) {
2717 this.x = x;
2718 this.y = y;
2719 this.spanX = spanX;
2720 this.spanY = spanY;
Adam Cohen482ed822012-03-02 14:15:13 -08002721 }
Adam Cohenf3900c22012-11-16 18:28:11 -08002722
2723 public String toString() {
2724 return "(" + x + ", " + y + ": " + spanX + ", " + spanY + ")";
2725 }
2726
Adam Cohen482ed822012-03-02 14:15:13 -08002727 }
2728
Adam Cohendf035382011-04-11 17:22:04 -07002729 /**
2730 * Find a vacant area that will fit the given bounds nearest the requested
2731 * cell location. Uses Euclidean distance to score multiple vacant areas.
2732 *
2733 * @param pixelX The X location at which you want to search for a vacant area.
2734 * @param pixelY The Y location at which you want to search for a vacant area.
2735 * @param spanX Horizontal span of the object.
2736 * @param spanY Vertical span of the object.
2737 * @param ignoreView Considers space occupied by this view as unoccupied
2738 * @param result Previously returned value to possibly recycle.
2739 * @return The X, Y cell of a vacant area that can contain this object,
2740 * nearest the requested location.
2741 */
2742 int[] findNearestVacantArea(
2743 int pixelX, int pixelY, int spanX, int spanY, View ignoreView, int[] result) {
2744 return findNearestArea(pixelX, pixelY, spanX, spanY, ignoreView, true, result);
2745 }
2746
2747 /**
Adam Cohend41fbf52012-02-16 23:53:59 -08002748 * Find a vacant area that will fit the given bounds nearest the requested
2749 * cell location. Uses Euclidean distance to score multiple vacant areas.
2750 *
2751 * @param pixelX The X location at which you want to search for a vacant area.
2752 * @param pixelY The Y location at which you want to search for a vacant area.
2753 * @param minSpanX The minimum horizontal span required
2754 * @param minSpanY The minimum vertical span required
2755 * @param spanX Horizontal span of the object.
2756 * @param spanY Vertical span of the object.
2757 * @param ignoreView Considers space occupied by this view as unoccupied
2758 * @param result Previously returned value to possibly recycle.
2759 * @return The X, Y cell of a vacant area that can contain this object,
2760 * nearest the requested location.
2761 */
2762 int[] findNearestVacantArea(int pixelX, int pixelY, int minSpanX, int minSpanY,
2763 int spanX, int spanY, View ignoreView, int[] result, int[] resultSpan) {
Adam Cohen482ed822012-03-02 14:15:13 -08002764 return findNearestArea(pixelX, pixelY, minSpanX, minSpanY, spanX, spanY, ignoreView, true,
2765 result, resultSpan, mOccupied);
Adam Cohend41fbf52012-02-16 23:53:59 -08002766 }
2767
2768 /**
Adam Cohendf035382011-04-11 17:22:04 -07002769 * Find a starting cell position that will fit the given bounds nearest the requested
2770 * cell location. Uses Euclidean distance to score multiple vacant areas.
2771 *
2772 * @param pixelX The X location at which you want to search for a vacant area.
2773 * @param pixelY The Y location at which you want to search for a vacant area.
2774 * @param spanX Horizontal span of the object.
2775 * @param spanY Vertical span of the object.
2776 * @param ignoreView Considers space occupied by this view as unoccupied
2777 * @param result Previously returned value to possibly recycle.
2778 * @return The X, Y cell of a vacant area that can contain this object,
2779 * nearest the requested location.
2780 */
2781 int[] findNearestArea(
2782 int pixelX, int pixelY, int spanX, int spanY, int[] result) {
2783 return findNearestArea(pixelX, pixelY, spanX, spanY, null, false, result);
2784 }
2785
Michael Jurka0280c3b2010-09-17 15:00:07 -07002786 boolean existsEmptyCell() {
2787 return findCellForSpan(null, 1, 1);
2788 }
2789
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002790 /**
Michael Jurka0280c3b2010-09-17 15:00:07 -07002791 * Finds the upper-left coordinate of the first rectangle in the grid that can
2792 * hold a cell of the specified dimensions. If intersectX and intersectY are not -1,
2793 * then this method will only return coordinates for rectangles that contain the cell
2794 * (intersectX, intersectY)
2795 *
2796 * @param cellXY The array that will contain the position of a vacant cell if such a cell
2797 * can be found.
2798 * @param spanX The horizontal span of the cell we want to find.
2799 * @param spanY The vertical span of the cell we want to find.
2800 *
2801 * @return True if a vacant cell of the specified dimension was found, false otherwise.
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07002802 */
Michael Jurka0280c3b2010-09-17 15:00:07 -07002803 boolean findCellForSpan(int[] cellXY, int spanX, int spanY) {
Adam Cohen482ed822012-03-02 14:15:13 -08002804 return findCellForSpanThatIntersectsIgnoring(cellXY, spanX, spanY, -1, -1, null, mOccupied);
Michael Jurka0280c3b2010-09-17 15:00:07 -07002805 }
2806
2807 /**
2808 * Like above, but ignores any cells occupied by the item "ignoreView"
2809 *
2810 * @param cellXY The array that will contain the position of a vacant cell if such a cell
2811 * can be found.
2812 * @param spanX The horizontal span of the cell we want to find.
2813 * @param spanY The vertical span of the cell we want to find.
2814 * @param ignoreView The home screen item we should treat as not occupying any space
2815 * @return
2816 */
2817 boolean findCellForSpanIgnoring(int[] cellXY, int spanX, int spanY, View ignoreView) {
Adam Cohen482ed822012-03-02 14:15:13 -08002818 return findCellForSpanThatIntersectsIgnoring(cellXY, spanX, spanY, -1, -1,
2819 ignoreView, mOccupied);
Michael Jurka0280c3b2010-09-17 15:00:07 -07002820 }
2821
2822 /**
2823 * Like above, but if intersectX and intersectY are not -1, then this method will try to
2824 * return coordinates for rectangles that contain the cell [intersectX, intersectY]
2825 *
2826 * @param spanX The horizontal span of the cell we want to find.
2827 * @param spanY The vertical span of the cell we want to find.
2828 * @param ignoreView The home screen item we should treat as not occupying any space
2829 * @param intersectX The X coordinate of the cell that we should try to overlap
2830 * @param intersectX The Y coordinate of the cell that we should try to overlap
2831 *
2832 * @return True if a vacant cell of the specified dimension was found, false otherwise.
2833 */
2834 boolean findCellForSpanThatIntersects(int[] cellXY, int spanX, int spanY,
2835 int intersectX, int intersectY) {
2836 return findCellForSpanThatIntersectsIgnoring(
Adam Cohen482ed822012-03-02 14:15:13 -08002837 cellXY, spanX, spanY, intersectX, intersectY, null, mOccupied);
Michael Jurka0280c3b2010-09-17 15:00:07 -07002838 }
2839
2840 /**
2841 * The superset of the above two methods
2842 */
2843 boolean findCellForSpanThatIntersectsIgnoring(int[] cellXY, int spanX, int spanY,
Adam Cohen482ed822012-03-02 14:15:13 -08002844 int intersectX, int intersectY, View ignoreView, boolean occupied[][]) {
Michael Jurkac6ee42e2010-09-30 12:04:50 -07002845 // mark space take by ignoreView as available (method checks if ignoreView is null)
Adam Cohen482ed822012-03-02 14:15:13 -08002846 markCellsAsUnoccupiedForView(ignoreView, occupied);
Michael Jurka0280c3b2010-09-17 15:00:07 -07002847
Michael Jurka28750fb2010-09-24 17:43:49 -07002848 boolean foundCell = false;
Michael Jurka0280c3b2010-09-17 15:00:07 -07002849 while (true) {
2850 int startX = 0;
2851 if (intersectX >= 0) {
2852 startX = Math.max(startX, intersectX - (spanX - 1));
2853 }
2854 int endX = mCountX - (spanX - 1);
2855 if (intersectX >= 0) {
2856 endX = Math.min(endX, intersectX + (spanX - 1) + (spanX == 1 ? 1 : 0));
2857 }
2858 int startY = 0;
2859 if (intersectY >= 0) {
2860 startY = Math.max(startY, intersectY - (spanY - 1));
2861 }
2862 int endY = mCountY - (spanY - 1);
2863 if (intersectY >= 0) {
2864 endY = Math.min(endY, intersectY + (spanY - 1) + (spanY == 1 ? 1 : 0));
2865 }
2866
Winson Chungbbc60d82010-11-11 16:34:41 -08002867 for (int y = startY; y < endY && !foundCell; y++) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002868 inner:
Winson Chungbbc60d82010-11-11 16:34:41 -08002869 for (int x = startX; x < endX; x++) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002870 for (int i = 0; i < spanX; i++) {
2871 for (int j = 0; j < spanY; j++) {
Adam Cohen482ed822012-03-02 14:15:13 -08002872 if (occupied[x + i][y + j]) {
Winson Chungbbc60d82010-11-11 16:34:41 -08002873 // small optimization: we can skip to after the column we just found
Michael Jurka0280c3b2010-09-17 15:00:07 -07002874 // an occupied cell
Winson Chungbbc60d82010-11-11 16:34:41 -08002875 x += i;
Michael Jurka0280c3b2010-09-17 15:00:07 -07002876 continue inner;
2877 }
2878 }
2879 }
2880 if (cellXY != null) {
2881 cellXY[0] = x;
2882 cellXY[1] = y;
2883 }
Michael Jurka28750fb2010-09-24 17:43:49 -07002884 foundCell = true;
2885 break;
Michael Jurka0280c3b2010-09-17 15:00:07 -07002886 }
2887 }
2888 if (intersectX == -1 && intersectY == -1) {
2889 break;
2890 } else {
2891 // if we failed to find anything, try again but without any requirements of
2892 // intersecting
2893 intersectX = -1;
2894 intersectY = -1;
2895 continue;
2896 }
2897 }
2898
Michael Jurkac6ee42e2010-09-30 12:04:50 -07002899 // re-mark space taken by ignoreView as occupied
Adam Cohen482ed822012-03-02 14:15:13 -08002900 markCellsAsOccupiedForView(ignoreView, occupied);
Michael Jurka28750fb2010-09-24 17:43:49 -07002901 return foundCell;
Michael Jurka0280c3b2010-09-17 15:00:07 -07002902 }
2903
2904 /**
Winson Chungc07918d2011-07-01 15:35:26 -07002905 * A drag event has begun over this layout.
2906 * It may have begun over this layout (in which case onDragChild is called first),
2907 * or it may have begun on another layout.
2908 */
2909 void onDragEnter() {
Adam Cohenc6cc61d2012-04-04 12:47:08 -07002910 mDragEnforcer.onDragEnter();
Winson Chungc07918d2011-07-01 15:35:26 -07002911 mDragging = true;
2912 }
2913
2914 /**
Michael Jurka0280c3b2010-09-17 15:00:07 -07002915 * Called when drag has left this CellLayout or has been completed (successfully or not)
2916 */
2917 void onDragExit() {
Adam Cohenc6cc61d2012-04-04 12:47:08 -07002918 mDragEnforcer.onDragExit();
Joe Onorato4be866d2010-10-10 11:26:02 -07002919 // This can actually be called when we aren't in a drag, e.g. when adding a new
2920 // item to this layout via the customize drawer.
2921 // Guard against that case.
2922 if (mDragging) {
2923 mDragging = false;
Patrick Dubroyde7658b2010-09-27 11:15:43 -07002924 }
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -07002925
2926 // Invalidate the drag data
Adam Cohend41fbf52012-02-16 23:53:59 -08002927 mDragCell[0] = mDragCell[1] = -1;
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -07002928 mDragOutlineAnims[mDragOutlineCurrent].animateOut();
2929 mDragOutlineCurrent = (mDragOutlineCurrent + 1) % mDragOutlineAnims.length;
Adam Cohen19f37922012-03-21 11:59:11 -07002930 revertTempState();
Michael Jurka33945b22010-12-21 18:19:38 -08002931 setIsDragOverlapping(false);
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07002932 }
2933
2934 /**
Winson Chungaafa03c2010-06-11 17:34:16 -07002935 * Mark a child as having been dropped.
Patrick Dubroyde7658b2010-09-27 11:15:43 -07002936 * At the beginning of the drag operation, the child may have been on another
Patrick Dubroyce34a972010-10-19 10:34:32 -07002937 * screen, but it is re-parented before this method is called.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002938 *
2939 * @param child The child that is being dropped
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002940 */
Adam Cohen716b51e2011-06-30 12:09:54 -07002941 void onDropChild(View child) {
Romain Guyd94533d2009-08-17 10:01:15 -07002942 if (child != null) {
2943 LayoutParams lp = (LayoutParams) child.getLayoutParams();
Romain Guy84f296c2009-11-04 15:00:44 -08002944 lp.dropped = true;
Romain Guyd94533d2009-08-17 10:01:15 -07002945 child.requestLayout();
Romain Guyd94533d2009-08-17 10:01:15 -07002946 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002947 }
2948
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002949 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002950 * Computes a bounding rectangle for a range of cells
Winson Chungaafa03c2010-06-11 17:34:16 -07002951 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002952 * @param cellX X coordinate of upper left corner expressed as a cell position
2953 * @param cellY Y coordinate of upper left corner expressed as a cell position
Winson Chungaafa03c2010-06-11 17:34:16 -07002954 * @param cellHSpan Width in cells
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002955 * @param cellVSpan Height in cells
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07002956 * @param resultRect Rect into which to put the results
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002957 */
Adam Cohend41fbf52012-02-16 23:53:59 -08002958 public void cellToRect(int cellX, int cellY, int cellHSpan, int cellVSpan, Rect resultRect) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002959 final int cellWidth = mCellWidth;
2960 final int cellHeight = mCellHeight;
2961 final int widthGap = mWidthGap;
2962 final int heightGap = mHeightGap;
Winson Chungaafa03c2010-06-11 17:34:16 -07002963
Winson Chung4b825dcd2011-06-19 12:41:22 -07002964 final int hStartPadding = getPaddingLeft();
2965 final int vStartPadding = getPaddingTop();
Winson Chungaafa03c2010-06-11 17:34:16 -07002966
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002967 int width = cellHSpan * cellWidth + ((cellHSpan - 1) * widthGap);
2968 int height = cellVSpan * cellHeight + ((cellVSpan - 1) * heightGap);
2969
2970 int x = hStartPadding + cellX * (cellWidth + widthGap);
2971 int y = vStartPadding + cellY * (cellHeight + heightGap);
Winson Chungaafa03c2010-06-11 17:34:16 -07002972
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07002973 resultRect.set(x, y, x + width, y + height);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002974 }
Winson Chungaafa03c2010-06-11 17:34:16 -07002975
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002976 /**
Winson Chungaafa03c2010-06-11 17:34:16 -07002977 * Computes the required horizontal and vertical cell spans to always
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002978 * fit the given rectangle.
Winson Chungaafa03c2010-06-11 17:34:16 -07002979 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002980 * @param width Width in pixels
2981 * @param height Height in pixels
Patrick Dubroy8f86ddc2010-07-16 13:55:32 -07002982 * @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 -08002983 */
Patrick Dubroy8f86ddc2010-07-16 13:55:32 -07002984 public int[] rectToCell(int width, int height, int[] result) {
Michael Jurka9987a5c2010-10-08 16:58:12 -07002985 return rectToCell(getResources(), width, height, result);
2986 }
2987
2988 public static int[] rectToCell(Resources resources, int width, int height, int[] result) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002989 // Always assume we're working with the smallest span to make sure we
2990 // reserve enough space in both orientations.
Joe Onorato79e56262009-09-21 15:23:04 -04002991 int actualWidth = resources.getDimensionPixelSize(R.dimen.workspace_cell_width);
2992 int actualHeight = resources.getDimensionPixelSize(R.dimen.workspace_cell_height);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002993 int smallerSize = Math.min(actualWidth, actualHeight);
Joe Onorato79e56262009-09-21 15:23:04 -04002994
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002995 // Always round up to next largest cell
Winson Chung54c725c2011-08-03 12:03:40 -07002996 int spanX = (int) Math.ceil(width / (float) smallerSize);
2997 int spanY = (int) Math.ceil(height / (float) smallerSize);
Joe Onorato79e56262009-09-21 15:23:04 -04002998
Patrick Dubroy8f86ddc2010-07-16 13:55:32 -07002999 if (result == null) {
3000 return new int[] { spanX, spanY };
3001 }
3002 result[0] = spanX;
3003 result[1] = spanY;
3004 return result;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003005 }
3006
Michael Jurkaf12c75c2011-01-25 22:41:40 -08003007 public int[] cellSpansToSize(int hSpans, int vSpans) {
3008 int[] size = new int[2];
3009 size[0] = hSpans * mCellWidth + (hSpans - 1) * mWidthGap;
3010 size[1] = vSpans * mCellHeight + (vSpans - 1) * mHeightGap;
3011 return size;
3012 }
3013
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003014 /**
Patrick Dubroy047379a2010-12-19 22:02:04 -08003015 * Calculate the grid spans needed to fit given item
3016 */
3017 public void calculateSpans(ItemInfo info) {
3018 final int minWidth;
3019 final int minHeight;
3020
3021 if (info instanceof LauncherAppWidgetInfo) {
3022 minWidth = ((LauncherAppWidgetInfo) info).minWidth;
3023 minHeight = ((LauncherAppWidgetInfo) info).minHeight;
3024 } else if (info instanceof PendingAddWidgetInfo) {
3025 minWidth = ((PendingAddWidgetInfo) info).minWidth;
3026 minHeight = ((PendingAddWidgetInfo) info).minHeight;
3027 } else {
3028 // It's not a widget, so it must be 1x1
3029 info.spanX = info.spanY = 1;
3030 return;
3031 }
3032 int[] spans = rectToCell(minWidth, minHeight, null);
3033 info.spanX = spans[0];
3034 info.spanY = spans[1];
3035 }
3036
3037 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003038 * Find the first vacant cell, if there is one.
3039 *
3040 * @param vacant Holds the x and y coordinate of the vacant cell
3041 * @param spanX Horizontal cell span.
3042 * @param spanY Vertical cell span.
Winson Chungaafa03c2010-06-11 17:34:16 -07003043 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003044 * @return True if a vacant cell was found
3045 */
3046 public boolean getVacantCell(int[] vacant, int spanX, int spanY) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003047
Michael Jurka0280c3b2010-09-17 15:00:07 -07003048 return findVacantCell(vacant, spanX, spanY, mCountX, mCountY, mOccupied);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003049 }
3050
3051 static boolean findVacantCell(int[] vacant, int spanX, int spanY,
3052 int xCount, int yCount, boolean[][] occupied) {
3053
Adam Cohen2801caf2011-05-13 20:57:39 -07003054 for (int y = 0; y < yCount; y++) {
3055 for (int x = 0; x < xCount; x++) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003056 boolean available = !occupied[x][y];
3057out: for (int i = x; i < x + spanX - 1 && x < xCount; i++) {
3058 for (int j = y; j < y + spanY - 1 && y < yCount; j++) {
3059 available = available && !occupied[i][j];
3060 if (!available) break out;
3061 }
3062 }
3063
3064 if (available) {
3065 vacant[0] = x;
3066 vacant[1] = y;
3067 return true;
3068 }
3069 }
3070 }
3071
3072 return false;
3073 }
3074
Michael Jurka0280c3b2010-09-17 15:00:07 -07003075 private void clearOccupiedCells() {
3076 for (int x = 0; x < mCountX; x++) {
3077 for (int y = 0; y < mCountY; y++) {
3078 mOccupied[x][y] = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003079 }
3080 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07003081 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003082
Adam Cohend41fbf52012-02-16 23:53:59 -08003083 public void onMove(View view, int newCellX, int newCellY, int newSpanX, int newSpanY) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07003084 markCellsAsUnoccupiedForView(view);
Adam Cohen482ed822012-03-02 14:15:13 -08003085 markCellsForView(newCellX, newCellY, newSpanX, newSpanY, mOccupied, true);
Michael Jurka0280c3b2010-09-17 15:00:07 -07003086 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003087
Adam Cohend4844c32011-02-18 19:25:06 -08003088 public void markCellsAsOccupiedForView(View view) {
Adam Cohen482ed822012-03-02 14:15:13 -08003089 markCellsAsOccupiedForView(view, mOccupied);
3090 }
3091 public void markCellsAsOccupiedForView(View view, boolean[][] occupied) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07003092 if (view == null || view.getParent() != mShortcutsAndWidgets) return;
Michael Jurka0280c3b2010-09-17 15:00:07 -07003093 LayoutParams lp = (LayoutParams) view.getLayoutParams();
Adam Cohen482ed822012-03-02 14:15:13 -08003094 markCellsForView(lp.cellX, lp.cellY, lp.cellHSpan, lp.cellVSpan, occupied, true);
Michael Jurka0280c3b2010-09-17 15:00:07 -07003095 }
3096
Adam Cohend4844c32011-02-18 19:25:06 -08003097 public void markCellsAsUnoccupiedForView(View view) {
Adam Cohen482ed822012-03-02 14:15:13 -08003098 markCellsAsUnoccupiedForView(view, mOccupied);
3099 }
3100 public void markCellsAsUnoccupiedForView(View view, boolean occupied[][]) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07003101 if (view == null || view.getParent() != mShortcutsAndWidgets) return;
Michael Jurka0280c3b2010-09-17 15:00:07 -07003102 LayoutParams lp = (LayoutParams) view.getLayoutParams();
Adam Cohen482ed822012-03-02 14:15:13 -08003103 markCellsForView(lp.cellX, lp.cellY, lp.cellHSpan, lp.cellVSpan, occupied, false);
Michael Jurka0280c3b2010-09-17 15:00:07 -07003104 }
3105
Adam Cohen482ed822012-03-02 14:15:13 -08003106 private void markCellsForView(int cellX, int cellY, int spanX, int spanY, boolean[][] occupied,
3107 boolean value) {
3108 if (cellX < 0 || cellY < 0) return;
Michael Jurka0280c3b2010-09-17 15:00:07 -07003109 for (int x = cellX; x < cellX + spanX && x < mCountX; x++) {
3110 for (int y = cellY; y < cellY + spanY && y < mCountY; y++) {
Adam Cohen482ed822012-03-02 14:15:13 -08003111 occupied[x][y] = value;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003112 }
3113 }
3114 }
3115
Adam Cohen2801caf2011-05-13 20:57:39 -07003116 public int getDesiredWidth() {
Michael Jurka8b805b12012-04-18 14:23:14 -07003117 return getPaddingLeft() + getPaddingRight() + (mCountX * mCellWidth) +
Adam Cohen2801caf2011-05-13 20:57:39 -07003118 (Math.max((mCountX - 1), 0) * mWidthGap);
3119 }
3120
3121 public int getDesiredHeight() {
Michael Jurka8b805b12012-04-18 14:23:14 -07003122 return getPaddingTop() + getPaddingBottom() + (mCountY * mCellHeight) +
Adam Cohen2801caf2011-05-13 20:57:39 -07003123 (Math.max((mCountY - 1), 0) * mHeightGap);
3124 }
3125
Michael Jurka66d72172011-04-12 16:29:25 -07003126 public boolean isOccupied(int x, int y) {
3127 if (x < mCountX && y < mCountY) {
3128 return mOccupied[x][y];
3129 } else {
3130 throw new RuntimeException("Position exceeds the bound of this CellLayout");
3131 }
3132 }
3133
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003134 @Override
3135 public ViewGroup.LayoutParams generateLayoutParams(AttributeSet attrs) {
3136 return new CellLayout.LayoutParams(getContext(), attrs);
3137 }
3138
3139 @Override
3140 protected boolean checkLayoutParams(ViewGroup.LayoutParams p) {
3141 return p instanceof CellLayout.LayoutParams;
3142 }
3143
3144 @Override
3145 protected ViewGroup.LayoutParams generateLayoutParams(ViewGroup.LayoutParams p) {
3146 return new CellLayout.LayoutParams(p);
3147 }
3148
Winson Chungaafa03c2010-06-11 17:34:16 -07003149 public static class CellLayoutAnimationController extends LayoutAnimationController {
3150 public CellLayoutAnimationController(Animation animation, float delay) {
3151 super(animation, delay);
3152 }
3153
3154 @Override
3155 protected long getDelayForView(View view) {
3156 return (int) (Math.random() * 150);
3157 }
3158 }
3159
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003160 public static class LayoutParams extends ViewGroup.MarginLayoutParams {
3161 /**
3162 * Horizontal location of the item in the grid.
3163 */
3164 @ViewDebug.ExportedProperty
3165 public int cellX;
3166
3167 /**
3168 * Vertical location of the item in the grid.
3169 */
3170 @ViewDebug.ExportedProperty
3171 public int cellY;
3172
3173 /**
Adam Cohen482ed822012-03-02 14:15:13 -08003174 * Temporary horizontal location of the item in the grid during reorder
3175 */
3176 public int tmpCellX;
3177
3178 /**
3179 * Temporary vertical location of the item in the grid during reorder
3180 */
3181 public int tmpCellY;
3182
3183 /**
3184 * Indicates that the temporary coordinates should be used to layout the items
3185 */
3186 public boolean useTmpCoords;
3187
3188 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003189 * Number of cells spanned horizontally by the item.
3190 */
3191 @ViewDebug.ExportedProperty
3192 public int cellHSpan;
3193
3194 /**
3195 * Number of cells spanned vertically by the item.
3196 */
3197 @ViewDebug.ExportedProperty
3198 public int cellVSpan;
Winson Chungaafa03c2010-06-11 17:34:16 -07003199
Adam Cohen1b607ed2011-03-03 17:26:50 -08003200 /**
3201 * Indicates whether the item will set its x, y, width and height parameters freely,
3202 * or whether these will be computed based on cellX, cellY, cellHSpan and cellVSpan.
3203 */
Adam Cohend4844c32011-02-18 19:25:06 -08003204 public boolean isLockedToGrid = true;
3205
Adam Cohen482ed822012-03-02 14:15:13 -08003206 /**
3207 * Indicates whether this item can be reordered. Always true except in the case of the
3208 * the AllApps button.
3209 */
3210 public boolean canReorder = true;
3211
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003212 // X coordinate of the view in the layout.
3213 @ViewDebug.ExportedProperty
3214 int x;
3215 // Y coordinate of the view in the layout.
3216 @ViewDebug.ExportedProperty
3217 int y;
3218
Romain Guy84f296c2009-11-04 15:00:44 -08003219 boolean dropped;
Romain Guyfcb9e712009-10-02 16:06:52 -07003220
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003221 public LayoutParams(Context c, AttributeSet attrs) {
3222 super(c, attrs);
3223 cellHSpan = 1;
3224 cellVSpan = 1;
3225 }
3226
3227 public LayoutParams(ViewGroup.LayoutParams source) {
3228 super(source);
3229 cellHSpan = 1;
3230 cellVSpan = 1;
3231 }
Winson Chungaafa03c2010-06-11 17:34:16 -07003232
3233 public LayoutParams(LayoutParams source) {
3234 super(source);
3235 this.cellX = source.cellX;
3236 this.cellY = source.cellY;
3237 this.cellHSpan = source.cellHSpan;
3238 this.cellVSpan = source.cellVSpan;
3239 }
3240
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003241 public LayoutParams(int cellX, int cellY, int cellHSpan, int cellVSpan) {
Romain Guy8f19cdd2010-01-08 15:07:00 -08003242 super(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003243 this.cellX = cellX;
3244 this.cellY = cellY;
3245 this.cellHSpan = cellHSpan;
3246 this.cellVSpan = cellVSpan;
3247 }
3248
Adam Cohen7f4eabe2011-04-21 16:19:16 -07003249 public void setup(int cellWidth, int cellHeight, int widthGap, int heightGap) {
Adam Cohend4844c32011-02-18 19:25:06 -08003250 if (isLockedToGrid) {
3251 final int myCellHSpan = cellHSpan;
3252 final int myCellVSpan = cellVSpan;
Adam Cohen482ed822012-03-02 14:15:13 -08003253 final int myCellX = useTmpCoords ? tmpCellX : cellX;
3254 final int myCellY = useTmpCoords ? tmpCellY : cellY;
Adam Cohen1b607ed2011-03-03 17:26:50 -08003255
Adam Cohend4844c32011-02-18 19:25:06 -08003256 width = myCellHSpan * cellWidth + ((myCellHSpan - 1) * widthGap) -
3257 leftMargin - rightMargin;
3258 height = myCellVSpan * cellHeight + ((myCellVSpan - 1) * heightGap) -
3259 topMargin - bottomMargin;
Winson Chungeecf02d2012-03-02 17:14:58 -08003260 x = (int) (myCellX * (cellWidth + widthGap) + leftMargin);
3261 y = (int) (myCellY * (cellHeight + heightGap) + topMargin);
Adam Cohend4844c32011-02-18 19:25:06 -08003262 }
3263 }
Winson Chungaafa03c2010-06-11 17:34:16 -07003264
Winson Chungaafa03c2010-06-11 17:34:16 -07003265 public String toString() {
3266 return "(" + this.cellX + ", " + this.cellY + ")";
3267 }
Adam Cohen7f4eabe2011-04-21 16:19:16 -07003268
3269 public void setWidth(int width) {
3270 this.width = width;
3271 }
3272
3273 public int getWidth() {
3274 return width;
3275 }
3276
3277 public void setHeight(int height) {
3278 this.height = height;
3279 }
3280
3281 public int getHeight() {
3282 return height;
3283 }
3284
3285 public void setX(int x) {
3286 this.x = x;
3287 }
3288
3289 public int getX() {
3290 return x;
3291 }
3292
3293 public void setY(int y) {
3294 this.y = y;
3295 }
3296
3297 public int getY() {
3298 return y;
3299 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003300 }
3301
Michael Jurka0280c3b2010-09-17 15:00:07 -07003302 // This class stores info for two purposes:
3303 // 1. When dragging items (mDragInfo in Workspace), we store the View, its cellX & cellY,
3304 // its spanX, spanY, and the screen it is on
3305 // 2. When long clicking on an empty cell in a CellLayout, we save information about the
3306 // cellX and cellY coordinates and which page was clicked. We then set this as a tag on
3307 // the CellLayout that was long clicked
Michael Jurkae5fb0f22011-04-11 13:27:46 -07003308 static final class CellInfo {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003309 View cell;
Michael Jurkaa63c4522010-08-19 13:52:27 -07003310 int cellX = -1;
3311 int cellY = -1;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003312 int spanX;
3313 int spanY;
3314 int screen;
Winson Chung3d503fb2011-07-13 17:25:49 -07003315 long container;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003316
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003317 @Override
3318 public String toString() {
Winson Chungaafa03c2010-06-11 17:34:16 -07003319 return "Cell[view=" + (cell == null ? "null" : cell.getClass())
3320 + ", x=" + cellX + ", y=" + cellY + "]";
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003321 }
3322 }
Michael Jurkad771c962011-08-09 15:00:48 -07003323
3324 public boolean lastDownOnOccupiedCell() {
3325 return mLastDownOnOccupiedCell;
3326 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003327}