blob: 199c41a59e97a375da3581475db46ecd8cbe2c60 [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;
20import android.animation.AnimatorListenerAdapter;
Chet Haase00397b12010-10-07 11:13:10 -070021import android.animation.TimeInterpolator;
Patrick Dubroyde7658b2010-09-27 11:15:43 -070022import android.animation.ValueAnimator;
23import android.animation.ValueAnimator.AnimatorUpdateListener;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080024import android.content.Context;
Joe Onorato79e56262009-09-21 15:23:04 -040025import android.content.res.Resources;
Winson Chungaafa03c2010-06-11 17:34:16 -070026import android.content.res.TypedArray;
Joe Onorato4be866d2010-10-10 11:26:02 -070027import android.graphics.Bitmap;
Winson Chungaafa03c2010-06-11 17:34:16 -070028import android.graphics.Canvas;
Andrew Flynn0dca1ec2012-02-29 13:33:22 -080029import android.graphics.Color;
Joe Onorato4be866d2010-10-10 11:26:02 -070030import android.graphics.Paint;
Patrick Dubroyde7658b2010-09-27 11:15:43 -070031import android.graphics.Point;
32import android.graphics.PointF;
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;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080039import android.util.AttributeSet;
Joe Onorato4be866d2010-10-10 11:26:02 -070040import android.util.Log;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080041import android.view.MotionEvent;
42import android.view.View;
43import android.view.ViewDebug;
44import android.view.ViewGroup;
Winson Chungaafa03c2010-06-11 17:34:16 -070045import android.view.animation.Animation;
Winson Chung150fbab2010-09-29 17:14:26 -070046import android.view.animation.DecelerateInterpolator;
Winson Chungaafa03c2010-06-11 17:34:16 -070047import android.view.animation.LayoutAnimationController;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080048
Adam Cohen66396872011-04-15 17:50:36 -070049import com.android.launcher.R;
Adam Cohen69ce2e52011-07-03 19:25:21 -070050import com.android.launcher2.FolderIcon.FolderRingAnimator;
Patrick Dubroy8e58e912010-10-14 13:21:48 -070051
Adam Cohen69ce2e52011-07-03 19:25:21 -070052import java.util.ArrayList;
Adam Cohenc0dcf592011-06-01 15:30:43 -070053import java.util.Arrays;
Adam Cohenbfbfd262011-06-13 16:55:12 -070054import java.util.HashMap;
Adam Cohend41fbf52012-02-16 23:53:59 -080055import java.util.Stack;
Adam Cohenc0dcf592011-06-01 15:30:43 -070056
Michael Jurkabdb5c532011-02-01 15:05:06 -080057public class CellLayout extends ViewGroup {
Winson Chungaafa03c2010-06-11 17:34:16 -070058 static final String TAG = "CellLayout";
59
Adam Cohen2acce882012-03-28 19:03:19 -070060 private Launcher mLauncher;
Winson Chung4b825dcd2011-06-19 12:41:22 -070061 private int mOriginalCellWidth;
62 private int mOriginalCellHeight;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080063 private int mCellWidth;
64 private int mCellHeight;
Winson Chungaafa03c2010-06-11 17:34:16 -070065
Adam Cohend22015c2010-07-26 22:02:18 -070066 private int mCountX;
67 private int mCountY;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080068
Adam Cohen234c4cd2011-07-17 21:03:04 -070069 private int mOriginalWidthGap;
70 private int mOriginalHeightGap;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080071 private int mWidthGap;
72 private int mHeightGap;
Winson Chung4b825dcd2011-06-19 12:41:22 -070073 private int mMaxGap;
Adam Cohenebea84d2011-11-09 17:20:41 -080074 private boolean mScrollingTransformsDirty = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080075
76 private final Rect mRect = new Rect();
77 private final CellInfo mCellInfo = new CellInfo();
Winson Chungaafa03c2010-06-11 17:34:16 -070078
Patrick Dubroyde7658b2010-09-27 11:15:43 -070079 // These are temporary variables to prevent having to allocate a new object just to
80 // return an (x, y) value from helper functions. Do NOT use them to maintain other state.
Winson Chung0be025d2011-05-23 17:45:09 -070081 private final int[] mTmpXY = new int[2];
Patrick Dubroyde7658b2010-09-27 11:15:43 -070082 private final int[] mTmpPoint = new int[2];
83 private final PointF mTmpPointF = new PointF();
Adam Cohen69ce2e52011-07-03 19:25:21 -070084 int[] mTempLocation = new int[2];
Patrick Dubroy6569f2c2010-07-12 14:25:18 -070085
The Android Open Source Project31dd5032009-03-03 19:32:27 -080086 boolean[][] mOccupied;
Adam Cohen482ed822012-03-02 14:15:13 -080087 boolean[][] mTmpOccupied;
Michael Jurkad771c962011-08-09 15:00:48 -070088 private boolean mLastDownOnOccupiedCell = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080089
Michael Jurkadee05892010-07-27 10:01:56 -070090 private OnTouchListener mInterceptTouchListener;
91
Adam Cohen69ce2e52011-07-03 19:25:21 -070092 private ArrayList<FolderRingAnimator> mFolderOuterRings = new ArrayList<FolderRingAnimator>();
Adam Cohenc51934b2011-07-26 21:07:43 -070093 private int[] mFolderLeaveBehindCell = {-1, -1};
Adam Cohen69ce2e52011-07-03 19:25:21 -070094
Adam Cohenb5ba0972011-09-07 18:02:31 -070095 private int mForegroundAlpha = 0;
Michael Jurka5f1c5092010-09-03 14:15:02 -070096 private float mBackgroundAlpha;
Adam Cohen1b0aaac2010-10-28 11:11:18 -070097 private float mBackgroundAlphaMultiplier = 1.0f;
Adam Cohenf34bab52010-09-30 14:11:56 -070098
Michael Jurka33945b22010-12-21 18:19:38 -080099 private Drawable mNormalBackground;
Michael Jurka33945b22010-12-21 18:19:38 -0800100 private Drawable mActiveGlowBackground;
Adam Cohenb5ba0972011-09-07 18:02:31 -0700101 private Drawable mOverScrollForegroundDrawable;
102 private Drawable mOverScrollLeft;
103 private Drawable mOverScrollRight;
Michael Jurka18014792010-10-14 09:01:34 -0700104 private Rect mBackgroundRect;
Adam Cohenb5ba0972011-09-07 18:02:31 -0700105 private Rect mForegroundRect;
Adam Cohenb5ba0972011-09-07 18:02:31 -0700106 private int mForegroundPadding;
Patrick Dubroy1262e362010-10-06 15:49:50 -0700107
Michael Jurka33945b22010-12-21 18:19:38 -0800108 // If we're actively dragging something over this screen, mIsDragOverlapping is true
109 private boolean mIsDragOverlapping = false;
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700110 private final Point mDragCenter = new Point();
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700111
Winson Chung150fbab2010-09-29 17:14:26 -0700112 // These arrays are used to implement the drag visualization on x-large screens.
Joe Onorato4be866d2010-10-10 11:26:02 -0700113 // They are used as circular arrays, indexed by mDragOutlineCurrent.
Adam Cohend41fbf52012-02-16 23:53:59 -0800114 private Rect[] mDragOutlines = new Rect[4];
Chet Haase472b2812010-10-14 07:02:04 -0700115 private float[] mDragOutlineAlphas = new float[mDragOutlines.length];
Joe Onorato4be866d2010-10-10 11:26:02 -0700116 private InterruptibleInOutAnimator[] mDragOutlineAnims =
117 new InterruptibleInOutAnimator[mDragOutlines.length];
Winson Chung150fbab2010-09-29 17:14:26 -0700118
119 // Used as an index into the above 3 arrays; indicates which is the most current value.
Joe Onorato4be866d2010-10-10 11:26:02 -0700120 private int mDragOutlineCurrent = 0;
Patrick Dubroy8e58e912010-10-14 13:21:48 -0700121 private final Paint mDragOutlinePaint = new Paint();
Winson Chung150fbab2010-09-29 17:14:26 -0700122
Patrick Dubroy96864c32011-03-10 17:17:23 -0800123 private BubbleTextView mPressedOrFocusedIcon;
124
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700125 private Drawable mCrosshairsDrawable = null;
Patrick Dubroy49250ad2010-10-08 15:33:52 -0700126 private InterruptibleInOutAnimator mCrosshairsAnimator = null;
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700127 private float mCrosshairsVisibility = 0.0f;
128
Adam Cohen482ed822012-03-02 14:15:13 -0800129 private HashMap<CellLayout.LayoutParams, Animator> mReorderAnimators = new
130 HashMap<CellLayout.LayoutParams, Animator>();
Adam Cohen19f37922012-03-21 11:59:11 -0700131 private HashMap<View, ReorderHintAnimation>
132 mShakeAnimators = new HashMap<View, ReorderHintAnimation>();
133
134 private boolean mItemPlacementDirty = false;
Adam Cohenbfbfd262011-06-13 16:55:12 -0700135
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700136 // When a drag operation is in progress, holds the nearest cell to the touch point
137 private final int[] mDragCell = new int[2];
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800138
Joe Onorato4be866d2010-10-10 11:26:02 -0700139 private boolean mDragging = false;
140
Patrick Dubroyce34a972010-10-19 10:34:32 -0700141 private TimeInterpolator mEaseOutInterpolator;
Michael Jurkaa52570f2012-03-20 03:18:20 -0700142 private ShortcutAndWidgetContainer mShortcutsAndWidgets;
Patrick Dubroyce34a972010-10-19 10:34:32 -0700143
Andrew Flynn0dca1ec2012-02-29 13:33:22 -0800144 private boolean mIsHotseat = false;
Winson Chungeecf02d2012-03-02 17:14:58 -0800145 private float mChildScale = 1f;
146 private float mHotseatChildScale = 1f;
Andrew Flynn0dca1ec2012-02-29 13:33:22 -0800147
Adam Cohen482ed822012-03-02 14:15:13 -0800148 public static final int MODE_DRAG_OVER = 0;
149 public static final int MODE_ON_DROP = 1;
150 public static final int MODE_ON_DROP_EXTERNAL = 2;
151 public static final int MODE_ACCEPT_DROP = 3;
Adam Cohen19f37922012-03-21 11:59:11 -0700152 private static final boolean DESTRUCTIVE_REORDER = false;
Adam Cohen482ed822012-03-02 14:15:13 -0800153 private static final boolean DEBUG_VISUALIZE_OCCUPIED = false;
154
Adam Cohen19f37922012-03-21 11:59:11 -0700155 private static final float REORDER_HINT_MAGNITUDE = 0.27f;
156 private static final int REORDER_ANIMATION_DURATION = 150;
157 private float mReorderHintAnimationMagnitude;
158
Adam Cohen482ed822012-03-02 14:15:13 -0800159 private ArrayList<View> mIntersectingViews = new ArrayList<View>();
160 private Rect mOccupiedRect = new Rect();
161 private int[] mDirectionVector = new int[2];
Adam Cohen19f37922012-03-21 11:59:11 -0700162 int[] mPreviousReorderDirection = new int[2];
Adam Cohenb209e632012-03-27 17:09:36 -0700163 private static final int INVALID_DIRECTION = -100;
Adam Cohen482ed822012-03-02 14:15:13 -0800164
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800165 public CellLayout(Context context) {
166 this(context, null);
167 }
168
169 public CellLayout(Context context, AttributeSet attrs) {
170 this(context, attrs, 0);
171 }
172
173 public CellLayout(Context context, AttributeSet attrs, int defStyle) {
174 super(context, attrs, defStyle);
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700175
176 // A ViewGroup usually does not draw, but CellLayout needs to draw a rectangle to show
177 // the user where a dragged item will land when dropped.
178 setWillNotDraw(false);
Adam Cohen2acce882012-03-28 19:03:19 -0700179 mLauncher = (Launcher) context;
Michael Jurkaa63c4522010-08-19 13:52:27 -0700180
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800181 TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.CellLayout, defStyle, 0);
182
Winson Chung4b825dcd2011-06-19 12:41:22 -0700183 mOriginalCellWidth =
184 mCellWidth = a.getDimensionPixelSize(R.styleable.CellLayout_cellWidth, 10);
185 mOriginalCellHeight =
186 mCellHeight = a.getDimensionPixelSize(R.styleable.CellLayout_cellHeight, 10);
Adam Cohen234c4cd2011-07-17 21:03:04 -0700187 mWidthGap = mOriginalWidthGap = a.getDimensionPixelSize(R.styleable.CellLayout_widthGap, 0);
188 mHeightGap = mOriginalHeightGap = a.getDimensionPixelSize(R.styleable.CellLayout_heightGap, 0);
Winson Chung4b825dcd2011-06-19 12:41:22 -0700189 mMaxGap = a.getDimensionPixelSize(R.styleable.CellLayout_maxGap, 0);
Adam Cohend22015c2010-07-26 22:02:18 -0700190 mCountX = LauncherModel.getCellCountX();
191 mCountY = LauncherModel.getCellCountY();
Michael Jurka0280c3b2010-09-17 15:00:07 -0700192 mOccupied = new boolean[mCountX][mCountY];
Adam Cohen482ed822012-03-02 14:15:13 -0800193 mTmpOccupied = new boolean[mCountX][mCountY];
Adam Cohen5b53f292012-03-29 14:30:35 -0700194 mPreviousReorderDirection[0] = INVALID_DIRECTION;
195 mPreviousReorderDirection[1] = INVALID_DIRECTION;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800196
197 a.recycle();
198
199 setAlwaysDrawnWithCacheEnabled(false);
200
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700201 final Resources res = getResources();
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700202
Winson Chung967289b2011-06-30 18:09:30 -0700203 mNormalBackground = res.getDrawable(R.drawable.homescreen_blue_normal_holo);
Winson Chungdea74b72011-09-13 18:06:43 -0700204 mActiveGlowBackground = res.getDrawable(R.drawable.homescreen_blue_strong_holo);
Michael Jurka33945b22010-12-21 18:19:38 -0800205
Adam Cohenb5ba0972011-09-07 18:02:31 -0700206 mOverScrollLeft = res.getDrawable(R.drawable.overscroll_glow_left);
207 mOverScrollRight = res.getDrawable(R.drawable.overscroll_glow_right);
208 mForegroundPadding =
209 res.getDimensionPixelSize(R.dimen.workspace_overscroll_drawable_padding);
Michael Jurka33945b22010-12-21 18:19:38 -0800210
Adam Cohen19f37922012-03-21 11:59:11 -0700211 mReorderHintAnimationMagnitude = (REORDER_HINT_MAGNITUDE *
212 res.getDimensionPixelSize(R.dimen.app_icon_size));
213
Winson Chungb26f3d62011-06-02 10:49:29 -0700214 mNormalBackground.setFilterBitmap(true);
Winson Chungb26f3d62011-06-02 10:49:29 -0700215 mActiveGlowBackground.setFilterBitmap(true);
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700216
Winson Chungeecf02d2012-03-02 17:14:58 -0800217 int iconScale = res.getInteger(R.integer.app_icon_scale_percent);
218 if (iconScale >= 0) {
219 mChildScale = iconScale / 100f;
220 }
221 int hotseatIconScale = res.getInteger(R.integer.app_icon_hotseat_scale_percent);
222 if (hotseatIconScale >= 0) {
223 mHotseatChildScale = hotseatIconScale / 100f;
224 }
Andrew Flynn0dca1ec2012-02-29 13:33:22 -0800225
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 Dubroy046e7eb2010-10-06 12:14:43 -0700228 mCrosshairsDrawable = res.getDrawable(R.drawable.gardening_crosshairs);
Patrick Dubroyce34a972010-10-19 10:34:32 -0700229 mEaseOutInterpolator = new DecelerateInterpolator(2.5f); // Quint ease out
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700230
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700231 // Set up the animation for fading the crosshairs in and out
232 int animDuration = res.getInteger(R.integer.config_crosshairsFadeInTime);
Patrick Dubroy49250ad2010-10-08 15:33:52 -0700233 mCrosshairsAnimator = new InterruptibleInOutAnimator(animDuration, 0.0f, 1.0f);
Chet Haase472b2812010-10-14 07:02:04 -0700234 mCrosshairsAnimator.getAnimator().addUpdateListener(new AnimatorUpdateListener() {
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700235 public void onAnimationUpdate(ValueAnimator animation) {
236 mCrosshairsVisibility = ((Float) animation.getAnimatedValue()).floatValue();
Patrick Dubroy8e58e912010-10-14 13:21:48 -0700237 invalidate();
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700238 }
239 });
Patrick Dubroyce34a972010-10-19 10:34:32 -0700240 mCrosshairsAnimator.getAnimator().setInterpolator(mEaseOutInterpolator);
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700241
Winson Chungb8c69f32011-10-19 21:36:08 -0700242 mDragCell[0] = mDragCell[1] = -1;
Joe Onorato4be866d2010-10-10 11:26:02 -0700243 for (int i = 0; i < mDragOutlines.length; i++) {
Adam Cohend41fbf52012-02-16 23:53:59 -0800244 mDragOutlines[i] = new Rect(-1, -1, -1, -1);
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700245 }
246
247 // When dragging things around the home screens, we show a green outline of
248 // where the item will land. The outlines gradually fade out, leaving a trail
249 // behind the drag path.
250 // Set up all the animations that are used to implement this fading.
251 final int duration = res.getInteger(R.integer.config_dragOutlineFadeTime);
Chet Haase472b2812010-10-14 07:02:04 -0700252 final float fromAlphaValue = 0;
253 final float toAlphaValue = (float)res.getInteger(R.integer.config_dragOutlineMaxAlpha);
Joe Onorato4be866d2010-10-10 11:26:02 -0700254
Patrick Dubroy8e58e912010-10-14 13:21:48 -0700255 Arrays.fill(mDragOutlineAlphas, fromAlphaValue);
Joe Onorato4be866d2010-10-10 11:26:02 -0700256
257 for (int i = 0; i < mDragOutlineAnims.length; i++) {
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700258 final InterruptibleInOutAnimator anim =
259 new InterruptibleInOutAnimator(duration, fromAlphaValue, toAlphaValue);
Patrick Dubroyce34a972010-10-19 10:34:32 -0700260 anim.getAnimator().setInterpolator(mEaseOutInterpolator);
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700261 final int thisIndex = i;
Chet Haase472b2812010-10-14 07:02:04 -0700262 anim.getAnimator().addUpdateListener(new AnimatorUpdateListener() {
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700263 public void onAnimationUpdate(ValueAnimator animation) {
Joe Onorato4be866d2010-10-10 11:26:02 -0700264 final Bitmap outline = (Bitmap)anim.getTag();
265
266 // If an animation is started and then stopped very quickly, we can still
267 // get spurious updates we've cleared the tag. Guard against this.
268 if (outline == null) {
Patrick Dubroyfe6bd872010-10-13 17:32:10 -0700269 if (false) {
270 Object val = animation.getAnimatedValue();
271 Log.d(TAG, "anim " + thisIndex + " update: " + val +
272 ", isStopped " + anim.isStopped());
273 }
Joe Onorato4be866d2010-10-10 11:26:02 -0700274 // Try to prevent it from continuing to run
275 animation.cancel();
276 } else {
Chet Haase472b2812010-10-14 07:02:04 -0700277 mDragOutlineAlphas[thisIndex] = (Float) animation.getAnimatedValue();
Adam Cohend41fbf52012-02-16 23:53:59 -0800278 CellLayout.this.invalidate(mDragOutlines[thisIndex]);
Joe Onorato4be866d2010-10-10 11:26:02 -0700279 }
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700280 }
281 });
Joe Onorato4be866d2010-10-10 11:26:02 -0700282 // The animation holds a reference to the drag outline bitmap as long is it's
283 // running. This way the bitmap can be GCed when the animations are complete.
Chet Haase472b2812010-10-14 07:02:04 -0700284 anim.getAnimator().addListener(new AnimatorListenerAdapter() {
Michael Jurka3c4c20f2010-10-28 15:36:06 -0700285 @Override
Joe Onorato4be866d2010-10-10 11:26:02 -0700286 public void onAnimationEnd(Animator animation) {
Chet Haase472b2812010-10-14 07:02:04 -0700287 if ((Float) ((ValueAnimator) animation).getAnimatedValue() == 0f) {
Joe Onorato4be866d2010-10-10 11:26:02 -0700288 anim.setTag(null);
289 }
290 }
291 });
292 mDragOutlineAnims[i] = anim;
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700293 }
Patrick Dubroyce34a972010-10-19 10:34:32 -0700294
Michael Jurka18014792010-10-14 09:01:34 -0700295 mBackgroundRect = new Rect();
Adam Cohenb5ba0972011-09-07 18:02:31 -0700296 mForegroundRect = new Rect();
Michael Jurkabea15192010-11-17 12:33:46 -0800297
Michael Jurkaa52570f2012-03-20 03:18:20 -0700298 mShortcutsAndWidgets = new ShortcutAndWidgetContainer(context);
299 mShortcutsAndWidgets.setCellDimensions(mCellWidth, mCellHeight, mWidthGap, mHeightGap);
300 addView(mShortcutsAndWidgets);
Michael Jurka18014792010-10-14 09:01:34 -0700301 }
302
Michael Jurkaf6440da2011-04-05 14:50:34 -0700303 static int widthInPortrait(Resources r, int numCells) {
304 // We use this method from Workspace to figure out how many rows/columns Launcher should
305 // have. We ignore the left/right padding on CellLayout because it turns out in our design
306 // the padding extends outside the visible screen size, but it looked fine anyway.
Michael Jurkaf6440da2011-04-05 14:50:34 -0700307 int cellWidth = r.getDimensionPixelSize(R.dimen.workspace_cell_width);
Winson Chung4b825dcd2011-06-19 12:41:22 -0700308 int minGap = Math.min(r.getDimensionPixelSize(R.dimen.workspace_width_gap),
309 r.getDimensionPixelSize(R.dimen.workspace_height_gap));
Michael Jurkaf6440da2011-04-05 14:50:34 -0700310
Winson Chung4b825dcd2011-06-19 12:41:22 -0700311 return minGap * (numCells - 1) + cellWidth * numCells;
Michael Jurkaf6440da2011-04-05 14:50:34 -0700312 }
313
Michael Jurkaf6440da2011-04-05 14:50:34 -0700314 static int heightInLandscape(Resources r, int numCells) {
315 // We use this method from Workspace to figure out how many rows/columns Launcher should
316 // have. We ignore the left/right padding on CellLayout because it turns out in our design
317 // the padding extends outside the visible screen size, but it looked fine anyway.
Michael Jurkaf6440da2011-04-05 14:50:34 -0700318 int cellHeight = r.getDimensionPixelSize(R.dimen.workspace_cell_height);
Winson Chung4b825dcd2011-06-19 12:41:22 -0700319 int minGap = Math.min(r.getDimensionPixelSize(R.dimen.workspace_width_gap),
320 r.getDimensionPixelSize(R.dimen.workspace_height_gap));
Michael Jurkaf6440da2011-04-05 14:50:34 -0700321
Winson Chung4b825dcd2011-06-19 12:41:22 -0700322 return minGap * (numCells - 1) + cellHeight * numCells;
Michael Jurkaf6440da2011-04-05 14:50:34 -0700323 }
324
Adam Cohen2801caf2011-05-13 20:57:39 -0700325 public void enableHardwareLayers() {
Michael Jurkaa52570f2012-03-20 03:18:20 -0700326 mShortcutsAndWidgets.enableHardwareLayers();
Adam Cohen2801caf2011-05-13 20:57:39 -0700327 }
328
329 public void setGridSize(int x, int y) {
330 mCountX = x;
331 mCountY = y;
332 mOccupied = new boolean[mCountX][mCountY];
Adam Cohen482ed822012-03-02 14:15:13 -0800333 mTmpOccupied = new boolean[mCountX][mCountY];
Adam Cohen7fbec102012-03-27 12:42:19 -0700334 mTempRectStack.clear();
Adam Cohen76fc0852011-06-17 13:26:23 -0700335 requestLayout();
Adam Cohen2801caf2011-05-13 20:57:39 -0700336 }
337
Patrick Dubroy96864c32011-03-10 17:17:23 -0800338 private void invalidateBubbleTextView(BubbleTextView icon) {
339 final int padding = icon.getPressedOrFocusedBackgroundPadding();
Winson Chung4b825dcd2011-06-19 12:41:22 -0700340 invalidate(icon.getLeft() + getPaddingLeft() - padding,
341 icon.getTop() + getPaddingTop() - padding,
342 icon.getRight() + getPaddingLeft() + padding,
343 icon.getBottom() + getPaddingTop() + padding);
Patrick Dubroy96864c32011-03-10 17:17:23 -0800344 }
345
Adam Cohenb5ba0972011-09-07 18:02:31 -0700346 void setOverScrollAmount(float r, boolean left) {
347 if (left && mOverScrollForegroundDrawable != mOverScrollLeft) {
348 mOverScrollForegroundDrawable = mOverScrollLeft;
349 } else if (!left && mOverScrollForegroundDrawable != mOverScrollRight) {
350 mOverScrollForegroundDrawable = mOverScrollRight;
351 }
352
353 mForegroundAlpha = (int) Math.round((r * 255));
354 mOverScrollForegroundDrawable.setAlpha(mForegroundAlpha);
355 invalidate();
356 }
357
Patrick Dubroy96864c32011-03-10 17:17:23 -0800358 void setPressedOrFocusedIcon(BubbleTextView icon) {
359 // We draw the pressed or focused BubbleTextView's background in CellLayout because it
360 // requires an expanded clip rect (due to the glow's blur radius)
361 BubbleTextView oldIcon = mPressedOrFocusedIcon;
362 mPressedOrFocusedIcon = icon;
363 if (oldIcon != null) {
364 invalidateBubbleTextView(oldIcon);
365 }
366 if (mPressedOrFocusedIcon != null) {
367 invalidateBubbleTextView(mPressedOrFocusedIcon);
368 }
369 }
370
Michael Jurka33945b22010-12-21 18:19:38 -0800371 void setIsDragOverlapping(boolean isDragOverlapping) {
372 if (mIsDragOverlapping != isDragOverlapping) {
373 mIsDragOverlapping = isDragOverlapping;
374 invalidate();
375 }
376 }
377
378 boolean getIsDragOverlapping() {
379 return mIsDragOverlapping;
380 }
381
Adam Cohenebea84d2011-11-09 17:20:41 -0800382 protected void setOverscrollTransformsDirty(boolean dirty) {
383 mScrollingTransformsDirty = dirty;
384 }
385
386 protected void resetOverscrollTransforms() {
387 if (mScrollingTransformsDirty) {
388 setOverscrollTransformsDirty(false);
389 setTranslationX(0);
390 setRotationY(0);
391 // It doesn't matter if we pass true or false here, the important thing is that we
392 // pass 0, which results in the overscroll drawable not being drawn any more.
393 setOverScrollAmount(0, false);
394 setPivotX(getMeasuredWidth() / 2);
395 setPivotY(getMeasuredHeight() / 2);
396 }
397 }
398
Jeff Sharkey83f111d2009-04-20 21:03:13 -0700399 @Override
Patrick Dubroy1262e362010-10-06 15:49:50 -0700400 protected void onDraw(Canvas canvas) {
Michael Jurka3e7c7632010-10-02 16:01:03 -0700401 // When we're large, we are either drawn in a "hover" state (ie when dragging an item to
402 // a neighboring page) or with just a normal background (if backgroundAlpha > 0.0f)
403 // When we're small, we are either drawn normally or in the "accepts drops" state (during
404 // a drag). However, we also drag the mini hover background *over* one of those two
405 // backgrounds
Winson Chungb26f3d62011-06-02 10:49:29 -0700406 if (mBackgroundAlpha > 0.0f) {
Adam Cohenf34bab52010-09-30 14:11:56 -0700407 Drawable bg;
Michael Jurka33945b22010-12-21 18:19:38 -0800408
409 if (mIsDragOverlapping) {
410 // In the mini case, we draw the active_glow bg *over* the active background
Michael Jurkabdf78552011-10-31 14:34:25 -0700411 bg = mActiveGlowBackground;
Adam Cohenf34bab52010-09-30 14:11:56 -0700412 } else {
Michael Jurkabdf78552011-10-31 14:34:25 -0700413 bg = mNormalBackground;
Adam Cohenf34bab52010-09-30 14:11:56 -0700414 }
Michael Jurka33945b22010-12-21 18:19:38 -0800415
416 bg.setAlpha((int) (mBackgroundAlpha * mBackgroundAlphaMultiplier * 255));
417 bg.setBounds(mBackgroundRect);
418 bg.draw(canvas);
Michael Jurkaa63c4522010-08-19 13:52:27 -0700419 }
Romain Guya6abce82009-11-10 02:54:41 -0800420
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700421 if (mCrosshairsVisibility > 0.0f) {
422 final int countX = mCountX;
423 final int countY = mCountY;
424
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700425 final float MAX_ALPHA = 0.4f;
426 final int MAX_VISIBLE_DISTANCE = 600;
427 final float DISTANCE_MULTIPLIER = 0.002f;
428
429 final Drawable d = mCrosshairsDrawable;
430 final int width = d.getIntrinsicWidth();
431 final int height = d.getIntrinsicHeight();
432
Winson Chung4b825dcd2011-06-19 12:41:22 -0700433 int x = getPaddingLeft() - (mWidthGap / 2) - (width / 2);
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700434 for (int col = 0; col <= countX; col++) {
Winson Chung4b825dcd2011-06-19 12:41:22 -0700435 int y = getPaddingTop() - (mHeightGap / 2) - (height / 2);
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700436 for (int row = 0; row <= countY; row++) {
437 mTmpPointF.set(x - mDragCenter.x, y - mDragCenter.y);
438 float dist = mTmpPointF.length();
439 // Crosshairs further from the drag point are more faint
440 float alpha = Math.min(MAX_ALPHA,
441 DISTANCE_MULTIPLIER * (MAX_VISIBLE_DISTANCE - dist));
442 if (alpha > 0.0f) {
443 d.setBounds(x, y, x + width, y + height);
444 d.setAlpha((int) (alpha * 255 * mCrosshairsVisibility));
445 d.draw(canvas);
446 }
447 y += mCellHeight + mHeightGap;
448 }
449 x += mCellWidth + mWidthGap;
450 }
Joe Onorato4be866d2010-10-10 11:26:02 -0700451 }
Winson Chung150fbab2010-09-29 17:14:26 -0700452
Patrick Dubroy8e58e912010-10-14 13:21:48 -0700453 final Paint paint = mDragOutlinePaint;
Joe Onorato4be866d2010-10-10 11:26:02 -0700454 for (int i = 0; i < mDragOutlines.length; i++) {
Chet Haase472b2812010-10-14 07:02:04 -0700455 final float alpha = mDragOutlineAlphas[i];
Joe Onorato4be866d2010-10-10 11:26:02 -0700456 if (alpha > 0) {
Adam Cohend41fbf52012-02-16 23:53:59 -0800457 final Rect r = mDragOutlines[i];
Joe Onorato4be866d2010-10-10 11:26:02 -0700458 final Bitmap b = (Bitmap) mDragOutlineAnims[i].getTag();
Chet Haase472b2812010-10-14 07:02:04 -0700459 paint.setAlpha((int)(alpha + .5f));
Adam Cohend41fbf52012-02-16 23:53:59 -0800460 canvas.drawBitmap(b, null, r, paint);
Winson Chung150fbab2010-09-29 17:14:26 -0700461 }
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700462 }
Patrick Dubroy96864c32011-03-10 17:17:23 -0800463
464 // We draw the pressed or focused BubbleTextView's background in CellLayout because it
465 // requires an expanded clip rect (due to the glow's blur radius)
466 if (mPressedOrFocusedIcon != null) {
467 final int padding = mPressedOrFocusedIcon.getPressedOrFocusedBackgroundPadding();
468 final Bitmap b = mPressedOrFocusedIcon.getPressedOrFocusedBackground();
469 if (b != null) {
470 canvas.drawBitmap(b,
Winson Chung4b825dcd2011-06-19 12:41:22 -0700471 mPressedOrFocusedIcon.getLeft() + getPaddingLeft() - padding,
472 mPressedOrFocusedIcon.getTop() + getPaddingTop() - padding,
Patrick Dubroy96864c32011-03-10 17:17:23 -0800473 null);
474 }
475 }
Adam Cohen69ce2e52011-07-03 19:25:21 -0700476
Adam Cohen482ed822012-03-02 14:15:13 -0800477 if (DEBUG_VISUALIZE_OCCUPIED) {
478 int[] pt = new int[2];
479 ColorDrawable cd = new ColorDrawable(Color.RED);
480 cd.setBounds(0, 0, 80, 80);
481 for (int i = 0; i < mCountX; i++) {
482 for (int j = 0; j < mCountY; j++) {
483 if (mOccupied[i][j]) {
484 cellToPoint(i, j, pt);
485 canvas.save();
486 canvas.translate(pt[0], pt[1]);
487 cd.draw(canvas);
488 canvas.restore();
489 }
490 }
491 }
492 }
493
Adam Cohen69ce2e52011-07-03 19:25:21 -0700494 // The folder outer / inner ring image(s)
495 for (int i = 0; i < mFolderOuterRings.size(); i++) {
496 FolderRingAnimator fra = mFolderOuterRings.get(i);
497
498 // Draw outer ring
499 Drawable d = FolderRingAnimator.sSharedOuterRingDrawable;
500 int width = (int) fra.getOuterRingSize();
501 int height = width;
502 cellToPoint(fra.mCellX, fra.mCellY, mTempLocation);
503
504 int centerX = mTempLocation[0] + mCellWidth / 2;
505 int centerY = mTempLocation[1] + FolderRingAnimator.sPreviewSize / 2;
506
507 canvas.save();
508 canvas.translate(centerX - width / 2, centerY - height / 2);
509 d.setBounds(0, 0, width, height);
510 d.draw(canvas);
511 canvas.restore();
512
513 // Draw inner ring
514 d = FolderRingAnimator.sSharedInnerRingDrawable;
515 width = (int) fra.getInnerRingSize();
516 height = width;
517 cellToPoint(fra.mCellX, fra.mCellY, mTempLocation);
518
519 centerX = mTempLocation[0] + mCellWidth / 2;
520 centerY = mTempLocation[1] + FolderRingAnimator.sPreviewSize / 2;
521 canvas.save();
522 canvas.translate(centerX - width / 2, centerY - width / 2);
523 d.setBounds(0, 0, width, height);
524 d.draw(canvas);
525 canvas.restore();
526 }
Adam Cohenc51934b2011-07-26 21:07:43 -0700527
528 if (mFolderLeaveBehindCell[0] >= 0 && mFolderLeaveBehindCell[1] >= 0) {
529 Drawable d = FolderIcon.sSharedFolderLeaveBehind;
530 int width = d.getIntrinsicWidth();
531 int height = d.getIntrinsicHeight();
532
533 cellToPoint(mFolderLeaveBehindCell[0], mFolderLeaveBehindCell[1], mTempLocation);
534 int centerX = mTempLocation[0] + mCellWidth / 2;
535 int centerY = mTempLocation[1] + FolderRingAnimator.sPreviewSize / 2;
536
537 canvas.save();
538 canvas.translate(centerX - width / 2, centerY - width / 2);
539 d.setBounds(0, 0, width, height);
540 d.draw(canvas);
541 canvas.restore();
542 }
Adam Cohen69ce2e52011-07-03 19:25:21 -0700543 }
544
Adam Cohenb5ba0972011-09-07 18:02:31 -0700545 @Override
546 protected void dispatchDraw(Canvas canvas) {
547 super.dispatchDraw(canvas);
548 if (mForegroundAlpha > 0) {
549 mOverScrollForegroundDrawable.setBounds(mForegroundRect);
550 Paint p = ((NinePatchDrawable) mOverScrollForegroundDrawable).getPaint();
551 p.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.ADD));
552 mOverScrollForegroundDrawable.draw(canvas);
553 p.setXfermode(null);
554 }
555 }
556
Adam Cohen69ce2e52011-07-03 19:25:21 -0700557 public void showFolderAccept(FolderRingAnimator fra) {
558 mFolderOuterRings.add(fra);
559 }
560
561 public void hideFolderAccept(FolderRingAnimator fra) {
562 if (mFolderOuterRings.contains(fra)) {
563 mFolderOuterRings.remove(fra);
564 }
565 invalidate();
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700566 }
567
Adam Cohenc51934b2011-07-26 21:07:43 -0700568 public void setFolderLeaveBehindCell(int x, int y) {
569 mFolderLeaveBehindCell[0] = x;
570 mFolderLeaveBehindCell[1] = y;
571 invalidate();
572 }
573
574 public void clearFolderLeaveBehind() {
575 mFolderLeaveBehindCell[0] = -1;
576 mFolderLeaveBehindCell[1] = -1;
577 invalidate();
578 }
579
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700580 @Override
Michael Jurkae6235dd2011-10-04 15:02:05 -0700581 public boolean shouldDelayChildPressedState() {
582 return false;
583 }
584
585 @Override
Jeff Sharkey83f111d2009-04-20 21:03:13 -0700586 public void cancelLongPress() {
587 super.cancelLongPress();
588
589 // Cancel long press for all children
590 final int count = getChildCount();
591 for (int i = 0; i < count; i++) {
592 final View child = getChildAt(i);
593 child.cancelLongPress();
594 }
595 }
596
Michael Jurkadee05892010-07-27 10:01:56 -0700597 public void setOnInterceptTouchListener(View.OnTouchListener listener) {
598 mInterceptTouchListener = listener;
599 }
600
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800601 int getCountX() {
Adam Cohend22015c2010-07-26 22:02:18 -0700602 return mCountX;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800603 }
604
605 int getCountY() {
Adam Cohend22015c2010-07-26 22:02:18 -0700606 return mCountY;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800607 }
608
Andrew Flynn0dca1ec2012-02-29 13:33:22 -0800609 public void setIsHotseat(boolean isHotseat) {
610 mIsHotseat = isHotseat;
611 }
612
Winson Chungeecf02d2012-03-02 17:14:58 -0800613 public float getChildrenScale() {
614 return mIsHotseat ? mHotseatChildScale : mChildScale;
615 }
616
Michael Jurkaf3ca3ab2010-10-20 17:08:24 -0700617 public boolean addViewToCellLayout(
618 View child, int index, int childId, LayoutParams params, boolean markCells) {
Andrew Flynn0dca1ec2012-02-29 13:33:22 -0800619 return addViewToCellLayout(child, index, childId, params, markCells, false);
620 }
621
Winson Chungeecf02d2012-03-02 17:14:58 -0800622 private void scaleChild(BubbleTextView bubbleChild, float pivot, float scale) {
Andrew Flynnbc239a12012-03-06 11:39:49 -0800623 // If we haven't measured the child yet, do it now
624 // (this happens if we're being dropped from all-apps
625 if (bubbleChild.getLayoutParams() instanceof LayoutParams &&
626 (bubbleChild.getMeasuredWidth() | bubbleChild.getMeasuredHeight()) == 0) {
Michael Jurkaa52570f2012-03-20 03:18:20 -0700627 getShortcutsAndWidgets().measureChild(bubbleChild);
Andrew Flynnbc239a12012-03-06 11:39:49 -0800628 }
Andrew Flynnbc239a12012-03-06 11:39:49 -0800629
Andrew Flynnbc239a12012-03-06 11:39:49 -0800630 bubbleChild.setScaleX(scale);
631 bubbleChild.setScaleY(scale);
Andrew Flynnbc239a12012-03-06 11:39:49 -0800632 }
633
634 private void resetChild(BubbleTextView bubbleChild) {
635 bubbleChild.setScaleX(1f);
636 bubbleChild.setScaleY(1f);
Andrew Flynnbc239a12012-03-06 11:39:49 -0800637
638 bubbleChild.setTextColor(getResources().getColor(R.color.workspace_icon_text_color));
639 }
640
Andrew Flynn0dca1ec2012-02-29 13:33:22 -0800641 public boolean addViewToCellLayout(View child, int index, int childId, LayoutParams params,
642 boolean markCells, boolean allApps) {
Winson Chungaafa03c2010-06-11 17:34:16 -0700643 final LayoutParams lp = params;
644
Andrew Flynn0dca1ec2012-02-29 13:33:22 -0800645 // Hotseat icons - scale down and remove text
646 // Don't scale the all apps button
647 // scale percent set to -1 means do not scale
648 // Only scale BubbleTextViews
649 if (child instanceof BubbleTextView) {
650 BubbleTextView bubbleChild = (BubbleTextView) child;
651
Andrew Flynnbc239a12012-03-06 11:39:49 -0800652 // Start the child with 100% scale and visible text
653 resetChild(bubbleChild);
Andrew Flynn0dca1ec2012-02-29 13:33:22 -0800654
Winson Chungeecf02d2012-03-02 17:14:58 -0800655 if (mIsHotseat && !allApps && mHotseatChildScale >= 0) {
Andrew Flynnbc239a12012-03-06 11:39:49 -0800656 // Scale/make transparent for a hotseat
Winson Chungeecf02d2012-03-02 17:14:58 -0800657 scaleChild(bubbleChild, 0f, mHotseatChildScale);
Andrew Flynn0dca1ec2012-02-29 13:33:22 -0800658
Andrew Flynnbc239a12012-03-06 11:39:49 -0800659 bubbleChild.setTextColor(getResources().getColor(android.R.color.transparent));
Winson Chungeecf02d2012-03-02 17:14:58 -0800660 } else if (mChildScale >= 0) {
Andrew Flynnbc239a12012-03-06 11:39:49 -0800661 // Else possibly still scale it if we need to for smaller icons
Winson Chungeecf02d2012-03-02 17:14:58 -0800662 scaleChild(bubbleChild, 0f, mChildScale);
Andrew Flynn0dca1ec2012-02-29 13:33:22 -0800663 }
664 }
665
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800666 // Generate an id for each view, this assumes we have at most 256x256 cells
667 // per workspace screen
Adam Cohend22015c2010-07-26 22:02:18 -0700668 if (lp.cellX >= 0 && lp.cellX <= mCountX - 1 && lp.cellY >= 0 && lp.cellY <= mCountY - 1) {
Winson Chungaafa03c2010-06-11 17:34:16 -0700669 // If the horizontal or vertical span is set to -1, it is taken to
670 // mean that it spans the extent of the CellLayout
Adam Cohend22015c2010-07-26 22:02:18 -0700671 if (lp.cellHSpan < 0) lp.cellHSpan = mCountX;
672 if (lp.cellVSpan < 0) lp.cellVSpan = mCountY;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800673
Winson Chungaafa03c2010-06-11 17:34:16 -0700674 child.setId(childId);
675
Michael Jurkaa52570f2012-03-20 03:18:20 -0700676 mShortcutsAndWidgets.addView(child, index, lp);
Michael Jurkadee05892010-07-27 10:01:56 -0700677
Michael Jurkaf3ca3ab2010-10-20 17:08:24 -0700678 if (markCells) markCellsAsOccupiedForView(child);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700679
Winson Chungaafa03c2010-06-11 17:34:16 -0700680 return true;
681 }
682 return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800683 }
Michael Jurka3e7c7632010-10-02 16:01:03 -0700684
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800685 @Override
Michael Jurka0280c3b2010-09-17 15:00:07 -0700686 public void removeAllViews() {
687 clearOccupiedCells();
Michael Jurkaa52570f2012-03-20 03:18:20 -0700688 mShortcutsAndWidgets.removeAllViews();
Michael Jurka0280c3b2010-09-17 15:00:07 -0700689 }
690
691 @Override
692 public void removeAllViewsInLayout() {
Michael Jurkaa52570f2012-03-20 03:18:20 -0700693 if (mShortcutsAndWidgets.getChildCount() > 0) {
Michael Jurka7cfc2822011-08-02 20:19:24 -0700694 clearOccupiedCells();
Michael Jurkaa52570f2012-03-20 03:18:20 -0700695 mShortcutsAndWidgets.removeAllViewsInLayout();
Michael Jurka7cfc2822011-08-02 20:19:24 -0700696 }
Michael Jurka0280c3b2010-09-17 15:00:07 -0700697 }
698
Michael Jurkaf3ca3ab2010-10-20 17:08:24 -0700699 public void removeViewWithoutMarkingCells(View view) {
Michael Jurkaa52570f2012-03-20 03:18:20 -0700700 mShortcutsAndWidgets.removeView(view);
Michael Jurkaf3ca3ab2010-10-20 17:08:24 -0700701 }
702
Michael Jurka0280c3b2010-09-17 15:00:07 -0700703 @Override
704 public void removeView(View view) {
705 markCellsAsUnoccupiedForView(view);
Michael Jurkaa52570f2012-03-20 03:18:20 -0700706 mShortcutsAndWidgets.removeView(view);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700707 }
708
709 @Override
710 public void removeViewAt(int index) {
Michael Jurkaa52570f2012-03-20 03:18:20 -0700711 markCellsAsUnoccupiedForView(mShortcutsAndWidgets.getChildAt(index));
712 mShortcutsAndWidgets.removeViewAt(index);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700713 }
714
715 @Override
716 public void removeViewInLayout(View view) {
717 markCellsAsUnoccupiedForView(view);
Michael Jurkaa52570f2012-03-20 03:18:20 -0700718 mShortcutsAndWidgets.removeViewInLayout(view);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700719 }
720
721 @Override
722 public void removeViews(int start, int count) {
723 for (int i = start; i < start + count; i++) {
Michael Jurkaa52570f2012-03-20 03:18:20 -0700724 markCellsAsUnoccupiedForView(mShortcutsAndWidgets.getChildAt(i));
Michael Jurka0280c3b2010-09-17 15:00:07 -0700725 }
Michael Jurkaa52570f2012-03-20 03:18:20 -0700726 mShortcutsAndWidgets.removeViews(start, count);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700727 }
728
729 @Override
730 public void removeViewsInLayout(int start, int count) {
731 for (int i = start; i < start + count; i++) {
Michael Jurkaa52570f2012-03-20 03:18:20 -0700732 markCellsAsUnoccupiedForView(mShortcutsAndWidgets.getChildAt(i));
Michael Jurka0280c3b2010-09-17 15:00:07 -0700733 }
Michael Jurkaa52570f2012-03-20 03:18:20 -0700734 mShortcutsAndWidgets.removeViewsInLayout(start, count);
Michael Jurkaabded662011-03-04 12:06:57 -0800735 }
736
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800737 @Override
738 protected void onAttachedToWindow() {
739 super.onAttachedToWindow();
740 mCellInfo.screen = ((ViewGroup) getParent()).indexOfChild(this);
741 }
742
Michael Jurkaaf442092010-06-10 17:01:57 -0700743 public void setTagToCellInfoForPoint(int touchX, int touchY) {
744 final CellInfo cellInfo = mCellInfo;
Winson Chungeecf02d2012-03-02 17:14:58 -0800745 Rect frame = mRect;
Michael Jurkaaf442092010-06-10 17:01:57 -0700746 final int x = touchX + mScrollX;
747 final int y = touchY + mScrollY;
Michael Jurkaa52570f2012-03-20 03:18:20 -0700748 final int count = mShortcutsAndWidgets.getChildCount();
Michael Jurkaaf442092010-06-10 17:01:57 -0700749
750 boolean found = false;
751 for (int i = count - 1; i >= 0; i--) {
Michael Jurkaa52570f2012-03-20 03:18:20 -0700752 final View child = mShortcutsAndWidgets.getChildAt(i);
Adam Cohend4844c32011-02-18 19:25:06 -0800753 final LayoutParams lp = (LayoutParams) child.getLayoutParams();
Michael Jurkaaf442092010-06-10 17:01:57 -0700754
Adam Cohen1b607ed2011-03-03 17:26:50 -0800755 if ((child.getVisibility() == VISIBLE || child.getAnimation() != null) &&
756 lp.isLockedToGrid) {
Michael Jurkaaf442092010-06-10 17:01:57 -0700757 child.getHitRect(frame);
Winson Chung0be025d2011-05-23 17:45:09 -0700758
Winson Chungeecf02d2012-03-02 17:14:58 -0800759 float scale = child.getScaleX();
760 frame = new Rect(child.getLeft(), child.getTop(), child.getRight(),
761 child.getBottom());
Winson Chung0be025d2011-05-23 17:45:09 -0700762 // The child hit rect is relative to the CellLayoutChildren parent, so we need to
763 // offset that by this CellLayout's padding to test an (x,y) point that is relative
764 // to this view.
Winson Chung4b825dcd2011-06-19 12:41:22 -0700765 frame.offset(mPaddingLeft, mPaddingTop);
Winson Chungeecf02d2012-03-02 17:14:58 -0800766 frame.inset((int) (frame.width() * (1f - scale) / 2),
767 (int) (frame.height() * (1f - scale) / 2));
Winson Chung0be025d2011-05-23 17:45:09 -0700768
Michael Jurkaaf442092010-06-10 17:01:57 -0700769 if (frame.contains(x, y)) {
Michael Jurkaaf442092010-06-10 17:01:57 -0700770 cellInfo.cell = child;
771 cellInfo.cellX = lp.cellX;
772 cellInfo.cellY = lp.cellY;
773 cellInfo.spanX = lp.cellHSpan;
774 cellInfo.spanY = lp.cellVSpan;
Michael Jurkaaf442092010-06-10 17:01:57 -0700775 found = true;
Michael Jurkaaf442092010-06-10 17:01:57 -0700776 break;
777 }
778 }
779 }
Winson Chungaafa03c2010-06-11 17:34:16 -0700780
Michael Jurkad771c962011-08-09 15:00:48 -0700781 mLastDownOnOccupiedCell = found;
782
Michael Jurkaaf442092010-06-10 17:01:57 -0700783 if (!found) {
Winson Chung0be025d2011-05-23 17:45:09 -0700784 final int cellXY[] = mTmpXY;
Michael Jurkaaf442092010-06-10 17:01:57 -0700785 pointToCellExact(x, y, cellXY);
786
Michael Jurkaaf442092010-06-10 17:01:57 -0700787 cellInfo.cell = null;
788 cellInfo.cellX = cellXY[0];
789 cellInfo.cellY = cellXY[1];
790 cellInfo.spanX = 1;
791 cellInfo.spanY = 1;
Michael Jurkaaf442092010-06-10 17:01:57 -0700792 }
793 setTag(cellInfo);
794 }
795
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800796 @Override
797 public boolean onInterceptTouchEvent(MotionEvent ev) {
Adam Cohenc1997fd2011-08-15 18:26:39 -0700798 // First we clear the tag to ensure that on every touch down we start with a fresh slate,
799 // even in the case where we return early. Not clearing here was causing bugs whereby on
800 // long-press we'd end up picking up an item from a previous drag operation.
801 final int action = ev.getAction();
802
803 if (action == MotionEvent.ACTION_DOWN) {
804 clearTagCellInfo();
805 }
806
Michael Jurkadee05892010-07-27 10:01:56 -0700807 if (mInterceptTouchListener != null && mInterceptTouchListener.onTouch(this, ev)) {
808 return true;
809 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800810
811 if (action == MotionEvent.ACTION_DOWN) {
Michael Jurkaaf442092010-06-10 17:01:57 -0700812 setTagToCellInfoForPoint((int) ev.getX(), (int) ev.getY());
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800813 }
Winson Chungeecf02d2012-03-02 17:14:58 -0800814
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800815 return false;
816 }
817
Adam Cohenc1997fd2011-08-15 18:26:39 -0700818 private void clearTagCellInfo() {
819 final CellInfo cellInfo = mCellInfo;
820 cellInfo.cell = null;
821 cellInfo.cellX = -1;
822 cellInfo.cellY = -1;
823 cellInfo.spanX = 0;
824 cellInfo.spanY = 0;
825 setTag(cellInfo);
826 }
827
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800828 public CellInfo getTag() {
Michael Jurka0280c3b2010-09-17 15:00:07 -0700829 return (CellInfo) super.getTag();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800830 }
831
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700832 /**
Winson Chungaafa03c2010-06-11 17:34:16 -0700833 * Given a point, return the cell that strictly encloses that point
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800834 * @param x X coordinate of the point
835 * @param y Y coordinate of the point
836 * @param result Array of 2 ints to hold the x and y coordinate of the cell
837 */
838 void pointToCellExact(int x, int y, int[] result) {
Winson Chung4b825dcd2011-06-19 12:41:22 -0700839 final int hStartPadding = getPaddingLeft();
840 final int vStartPadding = getPaddingTop();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800841
842 result[0] = (x - hStartPadding) / (mCellWidth + mWidthGap);
843 result[1] = (y - vStartPadding) / (mCellHeight + mHeightGap);
844
Adam Cohend22015c2010-07-26 22:02:18 -0700845 final int xAxis = mCountX;
846 final int yAxis = mCountY;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800847
848 if (result[0] < 0) result[0] = 0;
849 if (result[0] >= xAxis) result[0] = xAxis - 1;
850 if (result[1] < 0) result[1] = 0;
851 if (result[1] >= yAxis) result[1] = yAxis - 1;
852 }
Winson Chungaafa03c2010-06-11 17:34:16 -0700853
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800854 /**
855 * Given a point, return the cell that most closely encloses that point
856 * @param x X coordinate of the point
857 * @param y Y coordinate of the point
858 * @param result Array of 2 ints to hold the x and y coordinate of the cell
859 */
860 void pointToCellRounded(int x, int y, int[] result) {
861 pointToCellExact(x + (mCellWidth / 2), y + (mCellHeight / 2), result);
862 }
863
864 /**
865 * Given a cell coordinate, return the point that represents the upper left corner of that cell
Winson Chungaafa03c2010-06-11 17:34:16 -0700866 *
867 * @param cellX X coordinate of the cell
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800868 * @param cellY Y coordinate of the cell
Winson Chungaafa03c2010-06-11 17:34:16 -0700869 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800870 * @param result Array of 2 ints to hold the x and y coordinate of the point
871 */
872 void cellToPoint(int cellX, int cellY, int[] result) {
Winson Chung4b825dcd2011-06-19 12:41:22 -0700873 final int hStartPadding = getPaddingLeft();
874 final int vStartPadding = getPaddingTop();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800875
876 result[0] = hStartPadding + cellX * (mCellWidth + mWidthGap);
877 result[1] = vStartPadding + cellY * (mCellHeight + mHeightGap);
878 }
879
Adam Cohene3e27a82011-04-15 12:07:39 -0700880 /**
Adam Cohen482ed822012-03-02 14:15:13 -0800881 * Given a cell coordinate, return the point that represents the center of the cell
Adam Cohene3e27a82011-04-15 12:07:39 -0700882 *
883 * @param cellX X coordinate of the cell
884 * @param cellY Y coordinate of the cell
885 *
886 * @param result Array of 2 ints to hold the x and y coordinate of the point
887 */
888 void cellToCenterPoint(int cellX, int cellY, int[] result) {
Adam Cohen47a876d2012-03-19 13:21:41 -0700889 regionToCenterPoint(cellX, cellY, 1, 1, result);
890 }
891
892 /**
893 * Given a cell coordinate and span return the point that represents the center of the regio
894 *
895 * @param cellX X coordinate of the cell
896 * @param cellY Y coordinate of the cell
897 *
898 * @param result Array of 2 ints to hold the x and y coordinate of the point
899 */
900 void regionToCenterPoint(int cellX, int cellY, int spanX, int spanY, int[] result) {
Winson Chung4b825dcd2011-06-19 12:41:22 -0700901 final int hStartPadding = getPaddingLeft();
902 final int vStartPadding = getPaddingTop();
Adam Cohen47a876d2012-03-19 13:21:41 -0700903 result[0] = hStartPadding + cellX * (mCellWidth + mWidthGap) +
904 (spanX * mCellWidth + (spanX - 1) * mWidthGap) / 2;
905 result[1] = vStartPadding + cellY * (mCellHeight + mHeightGap) +
906 (spanY * mCellHeight + (spanY - 1) * mHeightGap) / 2;
Adam Cohene3e27a82011-04-15 12:07:39 -0700907 }
908
Adam Cohen19f37922012-03-21 11:59:11 -0700909 /**
910 * Given a cell coordinate and span fills out a corresponding pixel rect
911 *
912 * @param cellX X coordinate of the cell
913 * @param cellY Y coordinate of the cell
914 * @param result Rect in which to write the result
915 */
916 void regionToRect(int cellX, int cellY, int spanX, int spanY, Rect result) {
917 final int hStartPadding = getPaddingLeft();
918 final int vStartPadding = getPaddingTop();
919 final int left = hStartPadding + cellX * (mCellWidth + mWidthGap);
920 final int top = vStartPadding + cellY * (mCellHeight + mHeightGap);
921 result.set(left, top, left + (spanX * mCellWidth + (spanX - 1) * mWidthGap),
922 top + (spanY * mCellHeight + (spanY - 1) * mHeightGap));
923 }
924
Adam Cohen482ed822012-03-02 14:15:13 -0800925 public float getDistanceFromCell(float x, float y, int[] cell) {
926 cellToCenterPoint(cell[0], cell[1], mTmpPoint);
927 float distance = (float) Math.sqrt( Math.pow(x - mTmpPoint[0], 2) +
928 Math.pow(y - mTmpPoint[1], 2));
929 return distance;
930 }
931
Romain Guy84f296c2009-11-04 15:00:44 -0800932 int getCellWidth() {
933 return mCellWidth;
934 }
935
936 int getCellHeight() {
937 return mCellHeight;
938 }
939
Adam Cohend4844c32011-02-18 19:25:06 -0800940 int getWidthGap() {
941 return mWidthGap;
942 }
943
944 int getHeightGap() {
945 return mHeightGap;
946 }
947
Adam Cohen7f4eabe2011-04-21 16:19:16 -0700948 Rect getContentRect(Rect r) {
949 if (r == null) {
950 r = new Rect();
951 }
952 int left = getPaddingLeft();
953 int top = getPaddingTop();
Winson Chung4b825dcd2011-06-19 12:41:22 -0700954 int right = left + getWidth() - mPaddingLeft - mPaddingRight;
955 int bottom = top + getHeight() - mPaddingTop - mPaddingBottom;
Adam Cohen7f4eabe2011-04-21 16:19:16 -0700956 r.set(left, top, right, bottom);
957 return r;
958 }
959
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800960 @Override
961 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
962 // TODO: currently ignoring padding
Winson Chungaafa03c2010-06-11 17:34:16 -0700963
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800964 int widthSpecMode = MeasureSpec.getMode(widthMeasureSpec);
Winson Chungaafa03c2010-06-11 17:34:16 -0700965 int widthSpecSize = MeasureSpec.getSize(widthMeasureSpec);
966
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800967 int heightSpecMode = MeasureSpec.getMode(heightMeasureSpec);
968 int heightSpecSize = MeasureSpec.getSize(heightMeasureSpec);
Winson Chungaafa03c2010-06-11 17:34:16 -0700969
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800970 if (widthSpecMode == MeasureSpec.UNSPECIFIED || heightSpecMode == MeasureSpec.UNSPECIFIED) {
971 throw new RuntimeException("CellLayout cannot have UNSPECIFIED dimensions");
972 }
973
Adam Cohend22015c2010-07-26 22:02:18 -0700974 int numWidthGaps = mCountX - 1;
975 int numHeightGaps = mCountY - 1;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800976
Adam Cohen234c4cd2011-07-17 21:03:04 -0700977 if (mOriginalWidthGap < 0 || mOriginalHeightGap < 0) {
Winson Chung4b825dcd2011-06-19 12:41:22 -0700978 int hSpace = widthSpecSize - mPaddingLeft - mPaddingRight;
979 int vSpace = heightSpecSize - mPaddingTop - mPaddingBottom;
980 int hFreeSpace = hSpace - (mCountX * mOriginalCellWidth);
981 int vFreeSpace = vSpace - (mCountY * mOriginalCellHeight);
982 mWidthGap = Math.min(mMaxGap, numWidthGaps > 0 ? (hFreeSpace / numWidthGaps) : 0);
983 mHeightGap = Math.min(mMaxGap,numHeightGaps > 0 ? (vFreeSpace / numHeightGaps) : 0);
Michael Jurkaa52570f2012-03-20 03:18:20 -0700984 mShortcutsAndWidgets.setCellDimensions(mCellWidth, mCellHeight, mWidthGap, mHeightGap);
Adam Cohen234c4cd2011-07-17 21:03:04 -0700985 } else {
986 mWidthGap = mOriginalWidthGap;
987 mHeightGap = mOriginalHeightGap;
Winson Chungece7f5b2010-10-22 14:54:12 -0700988 }
Michael Jurka5f1c5092010-09-03 14:15:02 -0700989
Michael Jurka8c920dd2011-01-20 14:16:56 -0800990 // Initial values correspond to widthSpecMode == MeasureSpec.EXACTLY
991 int newWidth = widthSpecSize;
992 int newHeight = heightSpecSize;
Michael Jurka5f1c5092010-09-03 14:15:02 -0700993 if (widthSpecMode == MeasureSpec.AT_MOST) {
Winson Chung4b825dcd2011-06-19 12:41:22 -0700994 newWidth = mPaddingLeft + mPaddingRight + (mCountX * mCellWidth) +
Winson Chungece7f5b2010-10-22 14:54:12 -0700995 ((mCountX - 1) * mWidthGap);
Winson Chung4b825dcd2011-06-19 12:41:22 -0700996 newHeight = mPaddingTop + mPaddingBottom + (mCountY * mCellHeight) +
Winson Chungece7f5b2010-10-22 14:54:12 -0700997 ((mCountY - 1) * mHeightGap);
Michael Jurka5f1c5092010-09-03 14:15:02 -0700998 setMeasuredDimension(newWidth, newHeight);
Michael Jurka5f1c5092010-09-03 14:15:02 -0700999 }
Michael Jurka8c920dd2011-01-20 14:16:56 -08001000
1001 int count = getChildCount();
1002 for (int i = 0; i < count; i++) {
1003 View child = getChildAt(i);
Winson Chung4b825dcd2011-06-19 12:41:22 -07001004 int childWidthMeasureSpec = MeasureSpec.makeMeasureSpec(newWidth - mPaddingLeft -
1005 mPaddingRight, MeasureSpec.EXACTLY);
1006 int childheightMeasureSpec = MeasureSpec.makeMeasureSpec(newHeight - mPaddingTop -
1007 mPaddingBottom, MeasureSpec.EXACTLY);
Michael Jurka8c920dd2011-01-20 14:16:56 -08001008 child.measure(childWidthMeasureSpec, childheightMeasureSpec);
1009 }
1010 setMeasuredDimension(newWidth, newHeight);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001011 }
1012
1013 @Override
Michael Jurka28750fb2010-09-24 17:43:49 -07001014 protected void onLayout(boolean changed, int l, int t, int r, int b) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001015 int count = getChildCount();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001016 for (int i = 0; i < count; i++) {
Michael Jurka8c920dd2011-01-20 14:16:56 -08001017 View child = getChildAt(i);
Winson Chung4b825dcd2011-06-19 12:41:22 -07001018 child.layout(mPaddingLeft, mPaddingTop,
1019 r - l - mPaddingRight, b - t - mPaddingBottom);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001020 }
1021 }
1022
1023 @Override
Michael Jurkadee05892010-07-27 10:01:56 -07001024 protected void onSizeChanged(int w, int h, int oldw, int oldh) {
1025 super.onSizeChanged(w, h, oldw, oldh);
Michael Jurka18014792010-10-14 09:01:34 -07001026 mBackgroundRect.set(0, 0, w, h);
Adam Cohenb5ba0972011-09-07 18:02:31 -07001027 mForegroundRect.set(mForegroundPadding, mForegroundPadding,
1028 w - 2 * mForegroundPadding, h - 2 * mForegroundPadding);
Michael Jurkadee05892010-07-27 10:01:56 -07001029 }
1030
1031 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001032 protected void setChildrenDrawingCacheEnabled(boolean enabled) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07001033 mShortcutsAndWidgets.setChildrenDrawingCacheEnabled(enabled);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001034 }
1035
1036 @Override
1037 protected void setChildrenDrawnWithCacheEnabled(boolean enabled) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07001038 mShortcutsAndWidgets.setChildrenDrawnWithCacheEnabled(enabled);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001039 }
1040
Michael Jurka5f1c5092010-09-03 14:15:02 -07001041 public float getBackgroundAlpha() {
1042 return mBackgroundAlpha;
Michael Jurkadee05892010-07-27 10:01:56 -07001043 }
1044
Adam Cohen1b0aaac2010-10-28 11:11:18 -07001045 public void setBackgroundAlphaMultiplier(float multiplier) {
1046 mBackgroundAlphaMultiplier = multiplier;
1047 }
1048
Adam Cohenddb82192010-11-10 16:32:54 -08001049 public float getBackgroundAlphaMultiplier() {
1050 return mBackgroundAlphaMultiplier;
1051 }
1052
Michael Jurka5f1c5092010-09-03 14:15:02 -07001053 public void setBackgroundAlpha(float alpha) {
Michael Jurkaafaa0502011-12-13 18:22:50 -08001054 if (mBackgroundAlpha != alpha) {
1055 mBackgroundAlpha = alpha;
1056 invalidate();
1057 }
Michael Jurkadee05892010-07-27 10:01:56 -07001058 }
1059
Michael Jurkaa52570f2012-03-20 03:18:20 -07001060 public void setShortcutAndWidgetAlpha(float alpha) {
Michael Jurka0142d492010-08-25 17:46:15 -07001061 final int childCount = getChildCount();
1062 for (int i = 0; i < childCount; i++) {
Michael Jurkadee05892010-07-27 10:01:56 -07001063 getChildAt(i).setAlpha(alpha);
1064 }
1065 }
1066
Michael Jurkaa52570f2012-03-20 03:18:20 -07001067 public ShortcutAndWidgetContainer getShortcutsAndWidgets() {
1068 if (getChildCount() > 0) {
1069 return (ShortcutAndWidgetContainer) getChildAt(0);
1070 }
1071 return null;
1072 }
1073
Patrick Dubroy440c3602010-07-13 17:50:32 -07001074 public View getChildAt(int x, int y) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07001075 return mShortcutsAndWidgets.getChildAt(x, y);
Patrick Dubroy440c3602010-07-13 17:50:32 -07001076 }
1077
Adam Cohen76fc0852011-06-17 13:26:23 -07001078 public boolean animateChildToPosition(final View child, int cellX, int cellY, int duration,
Adam Cohen482ed822012-03-02 14:15:13 -08001079 int delay, boolean permanent, boolean adjustOccupied) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07001080 ShortcutAndWidgetContainer clc = getShortcutsAndWidgets();
Adam Cohen482ed822012-03-02 14:15:13 -08001081 boolean[][] occupied = mOccupied;
1082 if (!permanent) {
1083 occupied = mTmpOccupied;
1084 }
1085
Adam Cohen19f37922012-03-21 11:59:11 -07001086 if (clc.indexOfChild(child) != -1) {
Adam Cohenbfbfd262011-06-13 16:55:12 -07001087 final LayoutParams lp = (LayoutParams) child.getLayoutParams();
1088 final ItemInfo info = (ItemInfo) child.getTag();
1089
1090 // We cancel any existing animations
1091 if (mReorderAnimators.containsKey(lp)) {
1092 mReorderAnimators.get(lp).cancel();
1093 mReorderAnimators.remove(lp);
1094 }
1095
Adam Cohen482ed822012-03-02 14:15:13 -08001096 final int oldX = lp.x;
1097 final int oldY = lp.y;
1098 if (adjustOccupied) {
1099 occupied[lp.cellX][lp.cellY] = false;
1100 occupied[cellX][cellY] = true;
1101 }
Adam Cohenbfbfd262011-06-13 16:55:12 -07001102 lp.isLockedToGrid = true;
Adam Cohen482ed822012-03-02 14:15:13 -08001103 if (permanent) {
1104 lp.cellX = info.cellX = cellX;
1105 lp.cellY = info.cellY = cellY;
1106 } else {
1107 lp.tmpCellX = cellX;
1108 lp.tmpCellY = cellY;
1109 }
Adam Cohenbfbfd262011-06-13 16:55:12 -07001110 clc.setupLp(lp);
1111 lp.isLockedToGrid = false;
Adam Cohen482ed822012-03-02 14:15:13 -08001112 final int newX = lp.x;
1113 final int newY = lp.y;
Adam Cohenbfbfd262011-06-13 16:55:12 -07001114
Adam Cohen76fc0852011-06-17 13:26:23 -07001115 lp.x = oldX;
1116 lp.y = oldY;
Adam Cohen76fc0852011-06-17 13:26:23 -07001117
Adam Cohen482ed822012-03-02 14:15:13 -08001118 // Exit early if we're not actually moving the view
1119 if (oldX == newX && oldY == newY) {
1120 lp.isLockedToGrid = true;
1121 return true;
1122 }
1123
1124 ValueAnimator va = ValueAnimator.ofFloat(0f, 1f);
1125 va.setDuration(duration);
1126 mReorderAnimators.put(lp, va);
1127
1128 va.addUpdateListener(new AnimatorUpdateListener() {
1129 @Override
Adam Cohenbfbfd262011-06-13 16:55:12 -07001130 public void onAnimationUpdate(ValueAnimator animation) {
Adam Cohen482ed822012-03-02 14:15:13 -08001131 float r = ((Float) animation.getAnimatedValue()).floatValue();
Adam Cohen19f37922012-03-21 11:59:11 -07001132 lp.x = (int) ((1 - r) * oldX + r * newX);
1133 lp.y = (int) ((1 - r) * oldY + r * newY);
Adam Cohen6b8a02d2012-03-22 15:13:40 -07001134 child.requestLayout();
Adam Cohenbfbfd262011-06-13 16:55:12 -07001135 }
1136 });
Adam Cohen482ed822012-03-02 14:15:13 -08001137 va.addListener(new AnimatorListenerAdapter() {
Adam Cohenbfbfd262011-06-13 16:55:12 -07001138 boolean cancelled = false;
1139 public void onAnimationEnd(Animator animation) {
1140 // If the animation was cancelled, it means that another animation
1141 // has interrupted this one, and we don't want to lock the item into
1142 // place just yet.
1143 if (!cancelled) {
1144 lp.isLockedToGrid = true;
Adam Cohen482ed822012-03-02 14:15:13 -08001145 child.requestLayout();
Adam Cohenbfbfd262011-06-13 16:55:12 -07001146 }
1147 if (mReorderAnimators.containsKey(lp)) {
1148 mReorderAnimators.remove(lp);
1149 }
1150 }
1151 public void onAnimationCancel(Animator animation) {
1152 cancelled = true;
1153 }
1154 });
Adam Cohen482ed822012-03-02 14:15:13 -08001155 va.setStartDelay(delay);
1156 va.start();
Adam Cohenbfbfd262011-06-13 16:55:12 -07001157 return true;
1158 }
1159 return false;
1160 }
1161
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001162 /**
1163 * Estimate where the top left cell of the dragged item will land if it is dropped.
1164 *
1165 * @param originX The X value of the top left corner of the item
1166 * @param originY The Y value of the top left corner of the item
1167 * @param spanX The number of horizontal cells that the item spans
1168 * @param spanY The number of vertical cells that the item spans
1169 * @param result The estimated drop cell X and Y.
1170 */
1171 void estimateDropCell(int originX, int originY, int spanX, int spanY, int[] result) {
Adam Cohend22015c2010-07-26 22:02:18 -07001172 final int countX = mCountX;
1173 final int countY = mCountY;
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001174
Michael Jurkaa63c4522010-08-19 13:52:27 -07001175 // pointToCellRounded takes the top left of a cell but will pad that with
1176 // cellWidth/2 and cellHeight/2 when finding the matching cell
1177 pointToCellRounded(originX, originY, result);
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001178
1179 // If the item isn't fully on this screen, snap to the edges
1180 int rightOverhang = result[0] + spanX - countX;
1181 if (rightOverhang > 0) {
1182 result[0] -= rightOverhang; // Snap to right
1183 }
1184 result[0] = Math.max(0, result[0]); // Snap to left
1185 int bottomOverhang = result[1] + spanY - countY;
1186 if (bottomOverhang > 0) {
1187 result[1] -= bottomOverhang; // Snap to bottom
1188 }
1189 result[1] = Math.max(0, result[1]); // Snap to top
1190 }
1191
Adam Cohen482ed822012-03-02 14:15:13 -08001192 void visualizeDropLocation(View v, Bitmap dragOutline, int originX, int originY, int cellX,
1193 int cellY, int spanX, int spanY, boolean resize, Point dragOffset, Rect dragRegion) {
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -07001194 final int oldDragCellX = mDragCell[0];
1195 final int oldDragCellY = mDragCell[1];
Adam Cohen482ed822012-03-02 14:15:13 -08001196
Winson Chungb8c69f32011-10-19 21:36:08 -07001197 if (v != null && dragOffset == null) {
Winson Chunga9abd0e2010-10-27 17:18:37 -07001198 mDragCenter.set(originX + (v.getWidth() / 2), originY + (v.getHeight() / 2));
1199 } else {
1200 mDragCenter.set(originX, originY);
1201 }
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001202
Adam Cohen2801caf2011-05-13 20:57:39 -07001203 if (dragOutline == null && v == null) {
1204 if (mCrosshairsDrawable != null) {
1205 invalidate();
1206 }
1207 return;
1208 }
1209
Adam Cohen482ed822012-03-02 14:15:13 -08001210 if (cellX != oldDragCellX || cellY != oldDragCellY) {
1211 mDragCell[0] = cellX;
1212 mDragCell[1] = cellY;
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001213 // Find the top left corner of the rect the object will occupy
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001214 final int[] topLeft = mTmpPoint;
Adam Cohen482ed822012-03-02 14:15:13 -08001215 cellToPoint(cellX, cellY, topLeft);
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001216
Joe Onorato4be866d2010-10-10 11:26:02 -07001217 int left = topLeft[0];
1218 int top = topLeft[1];
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001219
Winson Chungb8c69f32011-10-19 21:36:08 -07001220 if (v != null && dragOffset == null) {
Adam Cohen99e8b402011-03-25 19:23:43 -07001221 // When drawing the drag outline, it did not account for margin offsets
1222 // added by the view's parent.
1223 MarginLayoutParams lp = (MarginLayoutParams) v.getLayoutParams();
1224 left += lp.leftMargin;
1225 top += lp.topMargin;
Winson Chung150fbab2010-09-29 17:14:26 -07001226
Adam Cohen99e8b402011-03-25 19:23:43 -07001227 // Offsets due to the size difference between the View and the dragOutline.
1228 // There is a size difference to account for the outer blur, which may lie
1229 // outside the bounds of the view.
Winson Chunga9abd0e2010-10-27 17:18:37 -07001230 top += (v.getHeight() - dragOutline.getHeight()) / 2;
Adam Cohenae915ce2011-08-25 13:47:22 -07001231 // We center about the x axis
1232 left += ((mCellWidth * spanX) + ((spanX - 1) * mWidthGap)
1233 - dragOutline.getWidth()) / 2;
Adam Cohen66396872011-04-15 17:50:36 -07001234 } else {
Winson Chungb8c69f32011-10-19 21:36:08 -07001235 if (dragOffset != null && dragRegion != null) {
1236 // Center the drag region *horizontally* in the cell and apply a drag
1237 // outline offset
1238 left += dragOffset.x + ((mCellWidth * spanX) + ((spanX - 1) * mWidthGap)
1239 - dragRegion.width()) / 2;
1240 top += dragOffset.y;
1241 } else {
1242 // Center the drag outline in the cell
1243 left += ((mCellWidth * spanX) + ((spanX - 1) * mWidthGap)
1244 - dragOutline.getWidth()) / 2;
1245 top += ((mCellHeight * spanY) + ((spanY - 1) * mHeightGap)
1246 - dragOutline.getHeight()) / 2;
1247 }
Winson Chunga9abd0e2010-10-27 17:18:37 -07001248 }
Joe Onorato4be866d2010-10-10 11:26:02 -07001249 final int oldIndex = mDragOutlineCurrent;
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -07001250 mDragOutlineAnims[oldIndex].animateOut();
1251 mDragOutlineCurrent = (oldIndex + 1) % mDragOutlines.length;
Adam Cohend41fbf52012-02-16 23:53:59 -08001252 Rect r = mDragOutlines[mDragOutlineCurrent];
1253 r.set(left, top, left + dragOutline.getWidth(), top + dragOutline.getHeight());
1254 if (resize) {
Adam Cohen482ed822012-03-02 14:15:13 -08001255 cellToRect(cellX, cellY, spanX, spanY, r);
Adam Cohend41fbf52012-02-16 23:53:59 -08001256 }
Winson Chung150fbab2010-09-29 17:14:26 -07001257
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -07001258 mDragOutlineAnims[mDragOutlineCurrent].setTag(dragOutline);
1259 mDragOutlineAnims[mDragOutlineCurrent].animateIn();
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001260 }
Patrick Dubroy49250ad2010-10-08 15:33:52 -07001261
1262 // If we are drawing crosshairs, the entire CellLayout needs to be invalidated
1263 if (mCrosshairsDrawable != null) {
1264 invalidate();
1265 }
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001266 }
1267
Adam Cohene0310962011-04-18 16:15:31 -07001268 public void clearDragOutlines() {
1269 final int oldIndex = mDragOutlineCurrent;
1270 mDragOutlineAnims[oldIndex].animateOut();
Adam Cohend41fbf52012-02-16 23:53:59 -08001271 mDragCell[0] = mDragCell[1] = -1;
Adam Cohene0310962011-04-18 16:15:31 -07001272 }
1273
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001274 /**
Jeff Sharkey70864282009-04-07 21:08:40 -07001275 * Find a vacant area that will fit the given bounds nearest the requested
1276 * cell location. Uses Euclidean distance to score multiple vacant areas.
Winson Chungaafa03c2010-06-11 17:34:16 -07001277 *
Romain Guy51afc022009-05-04 18:03:43 -07001278 * @param pixelX The X location at which you want to search for a vacant area.
1279 * @param pixelY The Y location at which you want to search for a vacant area.
Jeff Sharkey70864282009-04-07 21:08:40 -07001280 * @param spanX Horizontal span of the object.
1281 * @param spanY Vertical span of the object.
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001282 * @param result Array in which to place the result, or null (in which case a new array will
1283 * be allocated)
Jeff Sharkey70864282009-04-07 21:08:40 -07001284 * @return The X, Y cell of a vacant area that can contain this object,
1285 * nearest the requested location.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001286 */
Adam Cohend41fbf52012-02-16 23:53:59 -08001287 int[] findNearestVacantArea(int pixelX, int pixelY, int spanX, int spanY,
1288 int[] result) {
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001289 return findNearestVacantArea(pixelX, pixelY, spanX, spanY, null, result);
Michael Jurka6a1435d2010-09-27 17:35:12 -07001290 }
Winson Chungaafa03c2010-06-11 17:34:16 -07001291
Michael Jurka6a1435d2010-09-27 17:35:12 -07001292 /**
1293 * Find a vacant area that will fit the given bounds nearest the requested
1294 * cell location. Uses Euclidean distance to score multiple vacant areas.
1295 *
1296 * @param pixelX The X location at which you want to search for a vacant area.
1297 * @param pixelY The Y location at which you want to search for a vacant area.
Adam Cohend41fbf52012-02-16 23:53:59 -08001298 * @param minSpanX The minimum horizontal span required
1299 * @param minSpanY The minimum vertical span required
1300 * @param spanX Horizontal span of the object.
1301 * @param spanY Vertical span of the object.
1302 * @param result Array in which to place the result, or null (in which case a new array will
1303 * be allocated)
1304 * @return The X, Y cell of a vacant area that can contain this object,
1305 * nearest the requested location.
1306 */
1307 int[] findNearestVacantArea(int pixelX, int pixelY, int minSpanX, int minSpanY, int spanX,
1308 int spanY, int[] result, int[] resultSpan) {
1309 return findNearestVacantArea(pixelX, pixelY, minSpanX, minSpanY, spanX, spanY, null,
1310 result, resultSpan);
1311 }
1312
1313 /**
1314 * Find a vacant area that will fit the given bounds nearest the requested
1315 * cell location. Uses Euclidean distance to score multiple vacant areas.
1316 *
1317 * @param pixelX The X location at which you want to search for a vacant area.
1318 * @param pixelY The Y location at which you want to search for a vacant area.
Michael Jurka6a1435d2010-09-27 17:35:12 -07001319 * @param spanX Horizontal span of the object.
1320 * @param spanY Vertical span of the object.
Adam Cohendf035382011-04-11 17:22:04 -07001321 * @param ignoreOccupied If true, the result can be an occupied cell
1322 * @param result Array in which to place the result, or null (in which case a new array will
1323 * be allocated)
Michael Jurka6a1435d2010-09-27 17:35:12 -07001324 * @return The X, Y cell of a vacant area that can contain this object,
1325 * nearest the requested location.
1326 */
Adam Cohendf035382011-04-11 17:22:04 -07001327 int[] findNearestArea(int pixelX, int pixelY, int spanX, int spanY, View ignoreView,
1328 boolean ignoreOccupied, int[] result) {
Adam Cohend41fbf52012-02-16 23:53:59 -08001329 return findNearestArea(pixelX, pixelY, spanX, spanY,
Adam Cohen482ed822012-03-02 14:15:13 -08001330 spanX, spanY, ignoreView, ignoreOccupied, result, null, mOccupied);
Adam Cohend41fbf52012-02-16 23:53:59 -08001331 }
1332
1333 private final Stack<Rect> mTempRectStack = new Stack<Rect>();
1334 private void lazyInitTempRectStack() {
1335 if (mTempRectStack.isEmpty()) {
1336 for (int i = 0; i < mCountX * mCountY; i++) {
1337 mTempRectStack.push(new Rect());
1338 }
1339 }
1340 }
Adam Cohen482ed822012-03-02 14:15:13 -08001341
Adam Cohend41fbf52012-02-16 23:53:59 -08001342 private void recycleTempRects(Stack<Rect> used) {
1343 while (!used.isEmpty()) {
1344 mTempRectStack.push(used.pop());
1345 }
1346 }
1347
1348 /**
1349 * Find a vacant area that will fit the given bounds nearest the requested
1350 * cell location. Uses Euclidean distance to score multiple vacant areas.
1351 *
1352 * @param pixelX The X location at which you want to search for a vacant area.
1353 * @param pixelY The Y location at which you want to search for a vacant area.
1354 * @param minSpanX The minimum horizontal span required
1355 * @param minSpanY The minimum vertical span required
1356 * @param spanX Horizontal span of the object.
1357 * @param spanY Vertical span of the object.
1358 * @param ignoreOccupied If true, the result can be an occupied cell
1359 * @param result Array in which to place the result, or null (in which case a new array will
1360 * be allocated)
1361 * @return The X, Y cell of a vacant area that can contain this object,
1362 * nearest the requested location.
1363 */
1364 int[] findNearestArea(int pixelX, int pixelY, int minSpanX, int minSpanY, int spanX, int spanY,
Adam Cohen482ed822012-03-02 14:15:13 -08001365 View ignoreView, boolean ignoreOccupied, int[] result, int[] resultSpan,
1366 boolean[][] occupied) {
Adam Cohend41fbf52012-02-16 23:53:59 -08001367 lazyInitTempRectStack();
Michael Jurkac6ee42e2010-09-30 12:04:50 -07001368 // mark space take by ignoreView as available (method checks if ignoreView is null)
Adam Cohen482ed822012-03-02 14:15:13 -08001369 markCellsAsUnoccupiedForView(ignoreView, occupied);
Michael Jurkac6ee42e2010-09-30 12:04:50 -07001370
Adam Cohene3e27a82011-04-15 12:07:39 -07001371 // For items with a spanX / spanY > 1, the passed in point (pixelX, pixelY) corresponds
1372 // to the center of the item, but we are searching based on the top-left cell, so
1373 // we translate the point over to correspond to the top-left.
1374 pixelX -= (mCellWidth + mWidthGap) * (spanX - 1) / 2f;
1375 pixelY -= (mCellHeight + mHeightGap) * (spanY - 1) / 2f;
1376
Jeff Sharkey70864282009-04-07 21:08:40 -07001377 // Keep track of best-scoring drop area
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001378 final int[] bestXY = result != null ? result : new int[2];
Jeff Sharkey70864282009-04-07 21:08:40 -07001379 double bestDistance = Double.MAX_VALUE;
Adam Cohend41fbf52012-02-16 23:53:59 -08001380 final Rect bestRect = new Rect(-1, -1, -1, -1);
1381 final Stack<Rect> validRegions = new Stack<Rect>();
Winson Chungaafa03c2010-06-11 17:34:16 -07001382
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001383 final int countX = mCountX;
1384 final int countY = mCountY;
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001385
Adam Cohend41fbf52012-02-16 23:53:59 -08001386 if (minSpanX <= 0 || minSpanY <= 0 || spanX <= 0 || spanY <= 0 ||
1387 spanX < minSpanX || spanY < minSpanY) {
1388 return bestXY;
1389 }
1390
1391 for (int y = 0; y < countY - (minSpanY - 1); y++) {
Michael Jurkac28de512010-08-13 11:27:44 -07001392 inner:
Adam Cohend41fbf52012-02-16 23:53:59 -08001393 for (int x = 0; x < countX - (minSpanX - 1); x++) {
1394 int ySize = -1;
1395 int xSize = -1;
Adam Cohendf035382011-04-11 17:22:04 -07001396 if (ignoreOccupied) {
Adam Cohend41fbf52012-02-16 23:53:59 -08001397 // First, let's see if this thing fits anywhere
1398 for (int i = 0; i < minSpanX; i++) {
1399 for (int j = 0; j < minSpanY; j++) {
Adam Cohendf035382011-04-11 17:22:04 -07001400 if (occupied[x + i][y + j]) {
Adam Cohendf035382011-04-11 17:22:04 -07001401 continue inner;
1402 }
Michael Jurkac28de512010-08-13 11:27:44 -07001403 }
1404 }
Adam Cohend41fbf52012-02-16 23:53:59 -08001405 xSize = minSpanX;
1406 ySize = minSpanY;
1407
1408 // We know that the item will fit at _some_ acceptable size, now let's see
1409 // how big we can make it. We'll alternate between incrementing x and y spans
1410 // until we hit a limit.
1411 boolean incX = true;
1412 boolean hitMaxX = xSize >= spanX;
1413 boolean hitMaxY = ySize >= spanY;
1414 while (!(hitMaxX && hitMaxY)) {
1415 if (incX && !hitMaxX) {
1416 for (int j = 0; j < ySize; j++) {
1417 if (x + xSize > countX -1 || occupied[x + xSize][y + j]) {
1418 // We can't move out horizontally
1419 hitMaxX = true;
1420 }
1421 }
1422 if (!hitMaxX) {
1423 xSize++;
1424 }
1425 } else if (!hitMaxY) {
1426 for (int i = 0; i < xSize; i++) {
1427 if (y + ySize > countY - 1 || occupied[x + i][y + ySize]) {
1428 // We can't move out vertically
1429 hitMaxY = true;
1430 }
1431 }
1432 if (!hitMaxY) {
1433 ySize++;
1434 }
1435 }
1436 hitMaxX |= xSize >= spanX;
1437 hitMaxY |= ySize >= spanY;
1438 incX = !incX;
1439 }
1440 incX = true;
1441 hitMaxX = xSize >= spanX;
1442 hitMaxY = ySize >= spanY;
Michael Jurkac28de512010-08-13 11:27:44 -07001443 }
Winson Chung0be025d2011-05-23 17:45:09 -07001444 final int[] cellXY = mTmpXY;
Adam Cohene3e27a82011-04-15 12:07:39 -07001445 cellToCenterPoint(x, y, cellXY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001446
Adam Cohend41fbf52012-02-16 23:53:59 -08001447 // We verify that the current rect is not a sub-rect of any of our previous
1448 // candidates. In this case, the current rect is disqualified in favour of the
1449 // containing rect.
1450 Rect currentRect = mTempRectStack.pop();
1451 currentRect.set(x, y, x + xSize, y + ySize);
1452 boolean contained = false;
1453 for (Rect r : validRegions) {
1454 if (r.contains(currentRect)) {
1455 contained = true;
1456 break;
1457 }
1458 }
1459 validRegions.push(currentRect);
Michael Jurkac28de512010-08-13 11:27:44 -07001460 double distance = Math.sqrt(Math.pow(cellXY[0] - pixelX, 2)
1461 + Math.pow(cellXY[1] - pixelY, 2));
Adam Cohen482ed822012-03-02 14:15:13 -08001462
Adam Cohend41fbf52012-02-16 23:53:59 -08001463 if ((distance <= bestDistance && !contained) ||
1464 currentRect.contains(bestRect)) {
Michael Jurkac28de512010-08-13 11:27:44 -07001465 bestDistance = distance;
1466 bestXY[0] = x;
1467 bestXY[1] = y;
Adam Cohend41fbf52012-02-16 23:53:59 -08001468 if (resultSpan != null) {
1469 resultSpan[0] = xSize;
1470 resultSpan[1] = ySize;
1471 }
1472 bestRect.set(currentRect);
Michael Jurkac28de512010-08-13 11:27:44 -07001473 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001474 }
1475 }
Michael Jurkac6ee42e2010-09-30 12:04:50 -07001476 // re-mark space taken by ignoreView as occupied
Adam Cohen482ed822012-03-02 14:15:13 -08001477 markCellsAsOccupiedForView(ignoreView, occupied);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001478
Adam Cohenc0dcf592011-06-01 15:30:43 -07001479 // Return -1, -1 if no suitable location found
1480 if (bestDistance == Double.MAX_VALUE) {
1481 bestXY[0] = -1;
1482 bestXY[1] = -1;
Jeff Sharkey70864282009-04-07 21:08:40 -07001483 }
Adam Cohend41fbf52012-02-16 23:53:59 -08001484 recycleTempRects(validRegions);
Adam Cohenc0dcf592011-06-01 15:30:43 -07001485 return bestXY;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001486 }
Winson Chungaafa03c2010-06-11 17:34:16 -07001487
Adam Cohen482ed822012-03-02 14:15:13 -08001488 /**
1489 * Find a vacant area that will fit the given bounds nearest the requested
1490 * cell location, and will also weigh in a suggested direction vector of the
1491 * desired location. This method computers distance based on unit grid distances,
1492 * not pixel distances.
1493 *
Adam Cohen47a876d2012-03-19 13:21:41 -07001494 * @param cellX The X cell nearest to which you want to search for a vacant area.
1495 * @param cellY The Y cell nearest which you want to search for a vacant area.
Adam Cohen482ed822012-03-02 14:15:13 -08001496 * @param spanX Horizontal span of the object.
1497 * @param spanY Vertical span of the object.
Adam Cohen47a876d2012-03-19 13:21:41 -07001498 * @param direction The favored direction in which the views should move from x, y
1499 * @param exactDirectionOnly If this parameter is true, then only solutions where the direction
1500 * matches exactly. Otherwise we find the best matching direction.
1501 * @param occoupied The array which represents which cells in the CellLayout are occupied
1502 * @param blockOccupied The array which represents which cells in the specified block (cellX,
1503 * cellY, spanX, spanY) are occupied. This is used when try to move a group of views.
Adam Cohen482ed822012-03-02 14:15:13 -08001504 * @param result Array in which to place the result, or null (in which case a new array will
1505 * be allocated)
1506 * @return The X, Y cell of a vacant area that can contain this object,
1507 * nearest the requested location.
1508 */
1509 private int[] findNearestArea(int cellX, int cellY, int spanX, int spanY, int[] direction,
Adam Cohen47a876d2012-03-19 13:21:41 -07001510 boolean[][] occupied, boolean blockOccupied[][], int[] result) {
Adam Cohen482ed822012-03-02 14:15:13 -08001511 // Keep track of best-scoring drop area
1512 final int[] bestXY = result != null ? result : new int[2];
1513 float bestDistance = Float.MAX_VALUE;
1514 int bestDirectionScore = Integer.MIN_VALUE;
1515
1516 final int countX = mCountX;
1517 final int countY = mCountY;
1518
1519 for (int y = 0; y < countY - (spanY - 1); y++) {
1520 inner:
1521 for (int x = 0; x < countX - (spanX - 1); x++) {
1522 // First, let's see if this thing fits anywhere
1523 for (int i = 0; i < spanX; i++) {
1524 for (int j = 0; j < spanY; j++) {
Adam Cohen47a876d2012-03-19 13:21:41 -07001525 if (occupied[x + i][y + j] && (blockOccupied == null || blockOccupied[i][j])) {
Adam Cohen482ed822012-03-02 14:15:13 -08001526 continue inner;
1527 }
1528 }
1529 }
1530
1531 float distance = (float)
1532 Math.sqrt((x - cellX) * (x - cellX) + (y - cellY) * (y - cellY));
1533 int[] curDirection = mTmpPoint;
Adam Cohen47a876d2012-03-19 13:21:41 -07001534 computeDirectionVector(x - cellX, y - cellY, curDirection);
1535 // The direction score is just the dot product of the two candidate direction
1536 // and that passed in.
Adam Cohen482ed822012-03-02 14:15:13 -08001537 int curDirectionScore = direction[0] * curDirection[0] +
1538 direction[1] * curDirection[1];
Adam Cohen47a876d2012-03-19 13:21:41 -07001539 boolean exactDirectionOnly = false;
1540 boolean directionMatches = direction[0] == curDirection[0] &&
1541 direction[0] == curDirection[0];
1542 if ((directionMatches || !exactDirectionOnly) &&
1543 Float.compare(distance, bestDistance) < 0 || (Float.compare(distance,
Adam Cohen482ed822012-03-02 14:15:13 -08001544 bestDistance) == 0 && curDirectionScore > bestDirectionScore)) {
1545 bestDistance = distance;
1546 bestDirectionScore = curDirectionScore;
1547 bestXY[0] = x;
1548 bestXY[1] = y;
1549 }
1550 }
1551 }
1552
1553 // Return -1, -1 if no suitable location found
1554 if (bestDistance == Float.MAX_VALUE) {
1555 bestXY[0] = -1;
1556 bestXY[1] = -1;
1557 }
1558 return bestXY;
1559 }
1560
Adam Cohen47a876d2012-03-19 13:21:41 -07001561 private int[] findNearestAreaInDirection(int cellX, int cellY, int spanX, int spanY,
1562 int[] direction,boolean[][] occupied,
1563 boolean blockOccupied[][], int[] result) {
1564 // Keep track of best-scoring drop area
1565 final int[] bestXY = result != null ? result : new int[2];
1566 bestXY[0] = -1;
1567 bestXY[1] = -1;
1568 float bestDistance = Float.MAX_VALUE;
1569
1570 // We use this to march in a single direction
Adam Cohen5b53f292012-03-29 14:30:35 -07001571 if ((direction[0] != 0 && direction[1] != 0) ||
1572 (direction[0] == 0 && direction[1] == 0)) {
Adam Cohen47a876d2012-03-19 13:21:41 -07001573 return bestXY;
1574 }
1575
1576 // This will only incrememnet one of x or y based on the assertion above
1577 int x = cellX + direction[0];
1578 int y = cellY + direction[1];
1579 while (x >= 0 && x + spanX <= mCountX && y >= 0 && y + spanY <= mCountY) {
1580
1581 boolean fail = false;
1582 for (int i = 0; i < spanX; i++) {
1583 for (int j = 0; j < spanY; j++) {
1584 if (occupied[x + i][y + j] && (blockOccupied == null || blockOccupied[i][j])) {
1585 fail = true;
1586 }
1587 }
1588 }
1589 if (!fail) {
1590 float distance = (float)
1591 Math.sqrt((x - cellX) * (x - cellX) + (y - cellY) * (y - cellY));
1592 if (Float.compare(distance, bestDistance) < 0) {
1593 bestDistance = distance;
1594 bestXY[0] = x;
1595 bestXY[1] = y;
1596 }
1597 }
1598 x += direction[0];
1599 y += direction[1];
1600 }
1601 return bestXY;
1602 }
1603
Adam Cohen482ed822012-03-02 14:15:13 -08001604 private boolean addViewToTempLocation(View v, Rect rectOccupiedByPotentialDrop,
Adam Cohen8baab352012-03-20 17:39:21 -07001605 int[] direction, ItemConfiguration currentState) {
1606 CellAndSpan c = currentState.map.get(v);
Adam Cohen482ed822012-03-02 14:15:13 -08001607 boolean success = false;
Adam Cohen8baab352012-03-20 17:39:21 -07001608 markCellsForView(c.x, c.y, c.spanX, c.spanY, mTmpOccupied, false);
Adam Cohen482ed822012-03-02 14:15:13 -08001609 markCellsForRect(rectOccupiedByPotentialDrop, mTmpOccupied, true);
1610
Adam Cohen8baab352012-03-20 17:39:21 -07001611 findNearestArea(c.x, c.y, c.spanX, c.spanY, direction, mTmpOccupied, null, mTempLocation);
Adam Cohen482ed822012-03-02 14:15:13 -08001612
1613 if (mTempLocation[0] >= 0 && mTempLocation[1] >= 0) {
Adam Cohen8baab352012-03-20 17:39:21 -07001614 c.x = mTempLocation[0];
1615 c.y = mTempLocation[1];
Adam Cohen482ed822012-03-02 14:15:13 -08001616 success = true;
1617
1618 }
Adam Cohen8baab352012-03-20 17:39:21 -07001619 markCellsForView(c.x, c.y, c.spanX, c.spanY, mTmpOccupied, true);
Adam Cohen482ed822012-03-02 14:15:13 -08001620 return success;
1621 }
1622
Adam Cohen47a876d2012-03-19 13:21:41 -07001623 // This method looks in the specified direction to see if there is an additional view
1624 // immediately adjecent in that direction
1625 private boolean addViewInDirection(ArrayList<View> views, Rect boundingRect, int[] direction,
Adam Cohen19f37922012-03-21 11:59:11 -07001626 boolean[][] occupied, View dragView, ItemConfiguration currentState) {
Adam Cohen47a876d2012-03-19 13:21:41 -07001627 boolean found = false;
1628
Michael Jurkaa52570f2012-03-20 03:18:20 -07001629 int childCount = mShortcutsAndWidgets.getChildCount();
Adam Cohen47a876d2012-03-19 13:21:41 -07001630 Rect r0 = new Rect(boundingRect);
1631 Rect r1 = new Rect();
1632
1633 int deltaX = 0;
1634 int deltaY = 0;
1635 if (direction[1] < 0) {
1636 r0.set(r0.left, r0.top - 1, r0.right, r0.bottom);
1637 deltaY = -1;
1638 } else if (direction[1] > 0) {
1639 r0.set(r0.left, r0.top, r0.right, r0.bottom + 1);
1640 deltaY = 1;
1641 } else if (direction[0] < 0) {
1642 r0.set(r0.left - 1, r0.top, r0.right, r0.bottom);
1643 deltaX = -1;
1644 } else if (direction[0] > 0) {
1645 r0.set(r0.left, r0.top, r0.right + 1, r0.bottom);
1646 deltaX = 1;
1647 }
1648
1649 for (int i = 0; i < childCount; i++) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07001650 View child = mShortcutsAndWidgets.getChildAt(i);
Adam Cohen19f37922012-03-21 11:59:11 -07001651 if (views.contains(child) || child == dragView) continue;
Adam Cohen8baab352012-03-20 17:39:21 -07001652 CellAndSpan c = currentState.map.get(child);
Adam Cohen47a876d2012-03-19 13:21:41 -07001653
Adam Cohen8baab352012-03-20 17:39:21 -07001654 LayoutParams lp = (LayoutParams) child.getLayoutParams();
1655 r1.set(c.x, c.y, c.x + c.spanX, c.y + c.spanY);
Adam Cohen47a876d2012-03-19 13:21:41 -07001656 if (Rect.intersects(r0, r1)) {
1657 if (!lp.canReorder) {
1658 return false;
1659 }
1660 boolean pushed = false;
Adam Cohen8baab352012-03-20 17:39:21 -07001661 for (int x = c.x; x < c.x + c.spanX; x++) {
1662 for (int y = c.y; y < c.y + c.spanY; y++) {
Adam Cohen47a876d2012-03-19 13:21:41 -07001663 boolean inBounds = x - deltaX >= 0 && x -deltaX < mCountX
1664 && y - deltaY >= 0 && y - deltaY < mCountY;
1665 if (inBounds && occupied[x - deltaX][y - deltaY]) {
1666 pushed = true;
1667 }
1668 }
1669 }
1670 if (pushed) {
1671 views.add(child);
Adam Cohen8baab352012-03-20 17:39:21 -07001672 boundingRect.union(c.x, c.y, c.x + c.spanX, c.y + c.spanY);
Adam Cohen47a876d2012-03-19 13:21:41 -07001673 found = true;
1674 }
1675 }
1676 }
1677 return found;
1678 }
1679
Adam Cohen482ed822012-03-02 14:15:13 -08001680 private boolean addViewsToTempLocation(ArrayList<View> views, Rect rectOccupiedByPotentialDrop,
Adam Cohen19f37922012-03-21 11:59:11 -07001681 int[] direction, boolean push, View dragView, ItemConfiguration currentState) {
Adam Cohen482ed822012-03-02 14:15:13 -08001682 if (views.size() == 0) return true;
Adam Cohen482ed822012-03-02 14:15:13 -08001683
Adam Cohen8baab352012-03-20 17:39:21 -07001684 boolean success = false;
Adam Cohen482ed822012-03-02 14:15:13 -08001685 Rect boundingRect = null;
Adam Cohen8baab352012-03-20 17:39:21 -07001686 // We construct a rect which represents the entire group of views passed in
Adam Cohen482ed822012-03-02 14:15:13 -08001687 for (View v: views) {
Adam Cohen8baab352012-03-20 17:39:21 -07001688 CellAndSpan c = currentState.map.get(v);
Adam Cohen482ed822012-03-02 14:15:13 -08001689 if (boundingRect == null) {
Adam Cohen8baab352012-03-20 17:39:21 -07001690 boundingRect = new Rect(c.x, c.y, c.x + c.spanX, c.y + c.spanY);
Adam Cohen482ed822012-03-02 14:15:13 -08001691 } else {
Adam Cohen8baab352012-03-20 17:39:21 -07001692 boundingRect.union(c.x, c.y, c.x + c.spanX, c.y + c.spanY);
Adam Cohen482ed822012-03-02 14:15:13 -08001693 }
1694 }
Adam Cohen8baab352012-03-20 17:39:21 -07001695
1696 @SuppressWarnings("unchecked")
1697 ArrayList<View> dup = (ArrayList<View>) views.clone();
1698 // We try and expand the group of views in the direction vector passed, based on
1699 // whether they are physically adjacent, ie. based on "push mechanics".
Adam Cohen19f37922012-03-21 11:59:11 -07001700 while (push && addViewInDirection(dup, boundingRect, direction, mTmpOccupied, dragView,
Adam Cohen8baab352012-03-20 17:39:21 -07001701 currentState)) {
1702 }
1703
1704 // Mark the occupied state as false for the group of views we want to move.
1705 for (View v: dup) {
1706 CellAndSpan c = currentState.map.get(v);
1707 markCellsForView(c.x, c.y, c.spanX, c.spanY, mTmpOccupied, false);
1708 }
1709
Adam Cohen47a876d2012-03-19 13:21:41 -07001710 boolean[][] blockOccupied = new boolean[boundingRect.width()][boundingRect.height()];
1711 int top = boundingRect.top;
1712 int left = boundingRect.left;
Adam Cohen8baab352012-03-20 17:39:21 -07001713 // We mark more precisely which parts of the bounding rect are truly occupied, allowing
1714 // for tetris-style interlocking.
1715 for (View v: dup) {
1716 CellAndSpan c = currentState.map.get(v);
1717 markCellsForView(c.x - left, c.y - top, c.spanX, c.spanY, blockOccupied, true);
Adam Cohen47a876d2012-03-19 13:21:41 -07001718 }
1719
Adam Cohen482ed822012-03-02 14:15:13 -08001720 markCellsForRect(rectOccupiedByPotentialDrop, mTmpOccupied, true);
1721
Adam Cohen8baab352012-03-20 17:39:21 -07001722 if (push) {
1723 findNearestAreaInDirection(boundingRect.left, boundingRect.top, boundingRect.width(),
1724 boundingRect.height(), direction, mTmpOccupied, blockOccupied, mTempLocation);
1725 } else {
1726 findNearestArea(boundingRect.left, boundingRect.top, boundingRect.width(),
1727 boundingRect.height(), direction, mTmpOccupied, blockOccupied, mTempLocation);
1728 }
Adam Cohen482ed822012-03-02 14:15:13 -08001729
Adam Cohen8baab352012-03-20 17:39:21 -07001730 // If we successfuly found a location by pushing the block of views, we commit it
Adam Cohen482ed822012-03-02 14:15:13 -08001731 if (mTempLocation[0] >= 0 && mTempLocation[1] >= 0) {
Adam Cohen8baab352012-03-20 17:39:21 -07001732 int deltaX = mTempLocation[0] - boundingRect.left;
1733 int deltaY = mTempLocation[1] - boundingRect.top;
1734 for (View v: dup) {
1735 CellAndSpan c = currentState.map.get(v);
1736 c.x += deltaX;
1737 c.y += deltaY;
Adam Cohen482ed822012-03-02 14:15:13 -08001738 }
1739 success = true;
1740 }
Adam Cohen8baab352012-03-20 17:39:21 -07001741
1742 // In either case, we set the occupied array as marked for the location of the views
1743 for (View v: dup) {
1744 CellAndSpan c = currentState.map.get(v);
1745 markCellsForView(c.x, c.y, c.spanX, c.spanY, mTmpOccupied, true);
Adam Cohen482ed822012-03-02 14:15:13 -08001746 }
1747 return success;
1748 }
1749
1750 private void markCellsForRect(Rect r, boolean[][] occupied, boolean value) {
1751 markCellsForView(r.left, r.top, r.width(), r.height(), occupied, value);
1752 }
1753
1754 private boolean rearrangementExists(int cellX, int cellY, int spanX, int spanY, int[] direction,
Adam Cohen8baab352012-03-20 17:39:21 -07001755 View ignoreView, ItemConfiguration solution) {
Adam Cohen482ed822012-03-02 14:15:13 -08001756
Adam Cohen8baab352012-03-20 17:39:21 -07001757 mIntersectingViews.clear();
Adam Cohen482ed822012-03-02 14:15:13 -08001758 mOccupiedRect.set(cellX, cellY, cellX + spanX, cellY + spanY);
Adam Cohen482ed822012-03-02 14:15:13 -08001759
Adam Cohen8baab352012-03-20 17:39:21 -07001760 // Mark the desired location of the view currently being dragged.
Adam Cohen482ed822012-03-02 14:15:13 -08001761 if (ignoreView != null) {
Adam Cohen8baab352012-03-20 17:39:21 -07001762 CellAndSpan c = solution.map.get(ignoreView);
Adam Cohen19f37922012-03-21 11:59:11 -07001763 if (c != null) {
1764 c.x = cellX;
1765 c.y = cellY;
1766 }
Adam Cohen482ed822012-03-02 14:15:13 -08001767 }
Adam Cohen482ed822012-03-02 14:15:13 -08001768 Rect r0 = new Rect(cellX, cellY, cellX + spanX, cellY + spanY);
1769 Rect r1 = new Rect();
Adam Cohen8baab352012-03-20 17:39:21 -07001770 for (View child: solution.map.keySet()) {
Adam Cohen482ed822012-03-02 14:15:13 -08001771 if (child == ignoreView) continue;
Adam Cohen8baab352012-03-20 17:39:21 -07001772 CellAndSpan c = solution.map.get(child);
Adam Cohen482ed822012-03-02 14:15:13 -08001773 LayoutParams lp = (LayoutParams) child.getLayoutParams();
Adam Cohen8baab352012-03-20 17:39:21 -07001774 r1.set(c.x, c.y, c.x + c.spanX, c.y + c.spanY);
Adam Cohen482ed822012-03-02 14:15:13 -08001775 if (Rect.intersects(r0, r1)) {
1776 if (!lp.canReorder) {
1777 return false;
1778 }
1779 mIntersectingViews.add(child);
1780 }
1781 }
Adam Cohen47a876d2012-03-19 13:21:41 -07001782
Adam Cohen8baab352012-03-20 17:39:21 -07001783 // We try to move the intersecting views as a block using the push mechanic
Adam Cohen19f37922012-03-21 11:59:11 -07001784 if (addViewsToTempLocation(mIntersectingViews, mOccupiedRect, direction, true, ignoreView,
1785 solution)) {
Adam Cohen47a876d2012-03-19 13:21:41 -07001786 return true;
1787 }
1788 // Try the opposite direction
1789 direction[0] *= -1;
1790 direction[1] *= -1;
Adam Cohen19f37922012-03-21 11:59:11 -07001791 if (addViewsToTempLocation(mIntersectingViews, mOccupiedRect, direction, true, ignoreView,
1792 solution)) {
Adam Cohen47a876d2012-03-19 13:21:41 -07001793 return true;
1794 }
1795 // Switch the direction back
1796 direction[0] *= -1;
1797 direction[1] *= -1;
1798
Adam Cohen8baab352012-03-20 17:39:21 -07001799 // Next we try moving the views as a block , but without requiring the push mechanic
Adam Cohen19f37922012-03-21 11:59:11 -07001800 if (addViewsToTempLocation(mIntersectingViews, mOccupiedRect, direction, false, ignoreView,
1801 solution)) {
Adam Cohen482ed822012-03-02 14:15:13 -08001802 return true;
1803 }
Adam Cohen47a876d2012-03-19 13:21:41 -07001804
Adam Cohen482ed822012-03-02 14:15:13 -08001805 // Ok, they couldn't move as a block, let's move them individually
1806 for (View v : mIntersectingViews) {
Adam Cohen8baab352012-03-20 17:39:21 -07001807 if (!addViewToTempLocation(v, mOccupiedRect, direction, solution)) {
Adam Cohen482ed822012-03-02 14:15:13 -08001808 return false;
1809 }
1810 }
1811 return true;
1812 }
1813
1814 /*
1815 * Returns a pair (x, y), where x,y are in {-1, 0, 1} corresponding to vector between
1816 * the provided point and the provided cell
1817 */
Adam Cohen47a876d2012-03-19 13:21:41 -07001818 private void computeDirectionVector(float deltaX, float deltaY, int[] result) {
Adam Cohen482ed822012-03-02 14:15:13 -08001819 double angle = Math.atan(((float) deltaY) / deltaX);
1820
1821 result[0] = 0;
1822 result[1] = 0;
1823 if (Math.abs(Math.cos(angle)) > 0.5f) {
1824 result[0] = (int) Math.signum(deltaX);
1825 }
1826 if (Math.abs(Math.sin(angle)) > 0.5f) {
1827 result[1] = (int) Math.signum(deltaY);
1828 }
1829 }
1830
Adam Cohen8baab352012-03-20 17:39:21 -07001831 private void copyOccupiedArray(boolean[][] occupied) {
1832 for (int i = 0; i < mCountX; i++) {
1833 for (int j = 0; j < mCountY; j++) {
1834 occupied[i][j] = mOccupied[i][j];
1835 }
1836 }
1837 }
1838
Adam Cohen482ed822012-03-02 14:15:13 -08001839 ItemConfiguration simpleSwap(int pixelX, int pixelY, int minSpanX, int minSpanY, int spanX,
1840 int spanY, int[] direction, View dragView, boolean decX, ItemConfiguration solution) {
Adam Cohen8baab352012-03-20 17:39:21 -07001841 // Copy the current state into the solution. This solution will be manipulated as necessary.
1842 copyCurrentStateToSolution(solution, false);
1843 // Copy the current occupied array into the temporary occupied array. This array will be
1844 // manipulated as necessary to find a solution.
1845 copyOccupiedArray(mTmpOccupied);
Adam Cohen482ed822012-03-02 14:15:13 -08001846
1847 // We find the nearest cell into which we would place the dragged item, assuming there's
1848 // nothing in its way.
1849 int result[] = new int[2];
1850 result = findNearestArea(pixelX, pixelY, spanX, spanY, result);
1851
1852 boolean success = false;
1853 // First we try the exact nearest position of the item being dragged,
1854 // we will then want to try to move this around to other neighbouring positions
Adam Cohen8baab352012-03-20 17:39:21 -07001855 success = rearrangementExists(result[0], result[1], spanX, spanY, direction, dragView,
1856 solution);
Adam Cohen482ed822012-03-02 14:15:13 -08001857
1858 if (!success) {
1859 // We try shrinking the widget down to size in an alternating pattern, shrink 1 in
1860 // x, then 1 in y etc.
1861 if (spanX > minSpanX && (minSpanY == spanY || decX)) {
1862 return simpleSwap(pixelX, pixelY, minSpanX, minSpanY, spanX - 1, spanY, direction,
1863 dragView, false, solution);
1864 } else if (spanY > minSpanY) {
1865 return simpleSwap(pixelX, pixelY, minSpanX, minSpanY, spanX, spanY - 1, direction,
1866 dragView, true, solution);
1867 }
1868 solution.isSolution = false;
1869 } else {
1870 solution.isSolution = true;
1871 solution.dragViewX = result[0];
1872 solution.dragViewY = result[1];
1873 solution.dragViewSpanX = spanX;
1874 solution.dragViewSpanY = spanY;
Adam Cohen482ed822012-03-02 14:15:13 -08001875 }
1876 return solution;
1877 }
1878
1879 private void copyCurrentStateToSolution(ItemConfiguration solution, boolean temp) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07001880 int childCount = mShortcutsAndWidgets.getChildCount();
Adam Cohen482ed822012-03-02 14:15:13 -08001881 for (int i = 0; i < childCount; i++) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07001882 View child = mShortcutsAndWidgets.getChildAt(i);
Adam Cohen482ed822012-03-02 14:15:13 -08001883 LayoutParams lp = (LayoutParams) child.getLayoutParams();
Adam Cohen8baab352012-03-20 17:39:21 -07001884 CellAndSpan c;
Adam Cohen482ed822012-03-02 14:15:13 -08001885 if (temp) {
Adam Cohen8baab352012-03-20 17:39:21 -07001886 c = new CellAndSpan(lp.tmpCellX, lp.tmpCellY, lp.cellHSpan, lp.cellVSpan);
Adam Cohen482ed822012-03-02 14:15:13 -08001887 } else {
Adam Cohen8baab352012-03-20 17:39:21 -07001888 c = new CellAndSpan(lp.cellX, lp.cellY, lp.cellHSpan, lp.cellVSpan);
Adam Cohen482ed822012-03-02 14:15:13 -08001889 }
Adam Cohen8baab352012-03-20 17:39:21 -07001890 solution.map.put(child, c);
Adam Cohen482ed822012-03-02 14:15:13 -08001891 }
1892 }
1893
1894 private void copySolutionToTempState(ItemConfiguration solution, View dragView) {
1895 for (int i = 0; i < mCountX; i++) {
1896 for (int j = 0; j < mCountY; j++) {
1897 mTmpOccupied[i][j] = false;
1898 }
1899 }
1900
Michael Jurkaa52570f2012-03-20 03:18:20 -07001901 int childCount = mShortcutsAndWidgets.getChildCount();
Adam Cohen482ed822012-03-02 14:15:13 -08001902 for (int i = 0; i < childCount; i++) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07001903 View child = mShortcutsAndWidgets.getChildAt(i);
Adam Cohen482ed822012-03-02 14:15:13 -08001904 if (child == dragView) continue;
1905 LayoutParams lp = (LayoutParams) child.getLayoutParams();
Adam Cohen8baab352012-03-20 17:39:21 -07001906 CellAndSpan c = solution.map.get(child);
1907 if (c != null) {
1908 lp.tmpCellX = c.x;
1909 lp.tmpCellY = c.y;
1910 lp.cellHSpan = c.spanX;
1911 lp.cellVSpan = c.spanY;
1912 markCellsForView(c.x, c.y, c.spanX, c.spanY, mTmpOccupied, true);
Adam Cohen482ed822012-03-02 14:15:13 -08001913 }
1914 }
1915 markCellsForView(solution.dragViewX, solution.dragViewY, solution.dragViewSpanX,
1916 solution.dragViewSpanY, mTmpOccupied, true);
1917 }
1918
1919 private void animateItemsToSolution(ItemConfiguration solution, View dragView, boolean
1920 commitDragView) {
1921
1922 boolean[][] occupied = DESTRUCTIVE_REORDER ? mOccupied : mTmpOccupied;
1923 for (int i = 0; i < mCountX; i++) {
1924 for (int j = 0; j < mCountY; j++) {
1925 occupied[i][j] = false;
1926 }
1927 }
1928
Michael Jurkaa52570f2012-03-20 03:18:20 -07001929 int childCount = mShortcutsAndWidgets.getChildCount();
Adam Cohen482ed822012-03-02 14:15:13 -08001930 for (int i = 0; i < childCount; i++) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07001931 View child = mShortcutsAndWidgets.getChildAt(i);
Adam Cohen482ed822012-03-02 14:15:13 -08001932 if (child == dragView) continue;
Adam Cohen8baab352012-03-20 17:39:21 -07001933 CellAndSpan c = solution.map.get(child);
1934 if (c != null) {
Adam Cohen19f37922012-03-21 11:59:11 -07001935 animateChildToPosition(child, c.x, c.y, REORDER_ANIMATION_DURATION, 0,
1936 DESTRUCTIVE_REORDER, false);
Adam Cohen8baab352012-03-20 17:39:21 -07001937 markCellsForView(c.x, c.y, c.spanX, c.spanY, occupied, true);
Adam Cohen482ed822012-03-02 14:15:13 -08001938 }
1939 }
1940 if (commitDragView) {
1941 markCellsForView(solution.dragViewX, solution.dragViewY, solution.dragViewSpanX,
1942 solution.dragViewSpanY, occupied, true);
1943 }
1944 }
1945
Adam Cohen19f37922012-03-21 11:59:11 -07001946 // This method starts or changes the reorder hint animations
1947 private void beginOrAdjustHintAnimations(ItemConfiguration solution, View dragView, int delay) {
1948 int childCount = mShortcutsAndWidgets.getChildCount();
1949 int timeForPriorAnimationToComplete = getMaxCompletionTime();
1950 for (int i = 0; i < childCount; i++) {
1951 View child = mShortcutsAndWidgets.getChildAt(i);
1952 if (child == dragView) continue;
1953 CellAndSpan c = solution.map.get(child);
1954 LayoutParams lp = (LayoutParams) child.getLayoutParams();
1955 if (c != null) {
1956 ReorderHintAnimation rha = new ReorderHintAnimation(child, lp.cellX, lp.cellY,
1957 c.x, c.y, c.spanX, c.spanY);
1958 rha.animate(timeForPriorAnimationToComplete);
1959 }
1960 }
1961 }
1962
1963 // Class which represents the reorder hint animations. These animations show that an item is
1964 // in a temporary state, and hint at where the item will return to.
1965 class ReorderHintAnimation {
1966 View child;
1967 float deltaX;
1968 float deltaY;
1969 private static final int DURATION = 140;
1970 private int repeatCount;
1971 private boolean cancelOnCycleComplete = false;
1972 ValueAnimator va;
1973
1974 public ReorderHintAnimation(View child, int cellX0, int cellY0, int cellX1, int cellY1,
1975 int spanX, int spanY) {
1976 regionToCenterPoint(cellX0, cellY0, spanX, spanY, mTmpPoint);
1977 final int x0 = mTmpPoint[0];
1978 final int y0 = mTmpPoint[1];
1979 regionToCenterPoint(cellX1, cellY1, spanX, spanY, mTmpPoint);
1980 final int x1 = mTmpPoint[0];
1981 final int y1 = mTmpPoint[1];
1982 final int dX = x1 - x0;
1983 final int dY = y1 - y0;
1984 deltaX = 0;
1985 deltaY = 0;
1986 if (dX == dY && dX == 0) {
1987 } else {
1988 if (dY == 0) {
1989 deltaX = mReorderHintAnimationMagnitude;
1990 } else if (dX == 0) {
1991 deltaY = mReorderHintAnimationMagnitude;
1992 } else {
1993 double angle = Math.atan( (float) (dY) / dX);
1994 deltaX = (int) (Math.cos(angle) * mReorderHintAnimationMagnitude);
1995 deltaY = (int) (Math.sin(angle) * mReorderHintAnimationMagnitude);
1996 }
1997 }
1998 this.child = child;
1999 }
2000
2001 void animate(int delay) {
2002 if (mShakeAnimators.containsKey(child)) {
2003 ReorderHintAnimation oldAnimation = mShakeAnimators.get(child);
2004 oldAnimation.completeAnimation();
2005 mShakeAnimators.remove(child);
2006 }
2007 if (deltaX == 0 && deltaY == 0) {
2008 return;
2009 }
2010 va = ValueAnimator.ofFloat(0f, 1f);
2011 va.setRepeatMode(ValueAnimator.REVERSE);
2012 va.setRepeatCount(ValueAnimator.INFINITE);
2013 va.setDuration(DURATION);
2014 va.addUpdateListener(new AnimatorUpdateListener() {
2015 @Override
2016 public void onAnimationUpdate(ValueAnimator animation) {
2017 float r = ((Float) animation.getAnimatedValue()).floatValue();
2018 float x = r * deltaX;
2019 float y = r * deltaY;
2020 child.setTranslationX(x);
2021 child.setTranslationY(y);
2022 }
2023 });
2024 va.addListener(new AnimatorListenerAdapter() {
2025 public void onAnimationRepeat(Animator animation) {
2026 repeatCount++;
2027 // We make sure to end only after a full period
2028 if (cancelOnCycleComplete && repeatCount % 2 == 0) {
2029 va.cancel();
2030 }
2031 }
2032 });
2033 va.setStartDelay(Math.max(REORDER_ANIMATION_DURATION, delay));
2034 mShakeAnimators.put(child, this);
2035 va.start();
2036 }
2037
2038
2039 private void completeAnimation() {
2040 cancelOnCycleComplete = true;
2041 }
2042
2043 // Returns the time required to complete the current oscillating animation
2044 private int completionTime() {
2045 if (repeatCount % 2 == 0) {
2046 return (int) (va.getDuration() - va.getCurrentPlayTime() + DURATION);
2047 } else {
2048 return (int) (va.getDuration() - va.getCurrentPlayTime());
2049 }
2050 }
2051 }
2052
2053 private void completeAndClearReorderHintAnimations() {
2054 for (ReorderHintAnimation a: mShakeAnimators.values()) {
2055 a.completeAnimation();
2056 }
2057 mShakeAnimators.clear();
2058 }
2059
2060 private int getMaxCompletionTime() {
2061 int maxTime = 0;
2062 for (ReorderHintAnimation a: mShakeAnimators.values()) {
2063 maxTime = Math.max(maxTime, a.completionTime());
2064 }
2065 return maxTime;
2066 }
2067
Adam Cohen482ed822012-03-02 14:15:13 -08002068 private void commitTempPlacement() {
2069 for (int i = 0; i < mCountX; i++) {
2070 for (int j = 0; j < mCountY; j++) {
2071 mOccupied[i][j] = mTmpOccupied[i][j];
2072 }
2073 }
Michael Jurkaa52570f2012-03-20 03:18:20 -07002074 int childCount = mShortcutsAndWidgets.getChildCount();
Adam Cohen482ed822012-03-02 14:15:13 -08002075 for (int i = 0; i < childCount; i++) {
Adam Cohenea889a22012-03-27 16:45:39 -07002076 View child = mShortcutsAndWidgets.getChildAt(i);
2077 LayoutParams lp = (LayoutParams) child.getLayoutParams();
2078 ItemInfo info = (ItemInfo) child.getTag();
Adam Cohen2acce882012-03-28 19:03:19 -07002079 // We do a null check here because the item info can be null in the case of the
2080 // AllApps button in the hotseat.
2081 if (info != null) {
2082 info.cellX = lp.cellX = lp.tmpCellX;
2083 info.cellY = lp.cellY = lp.tmpCellY;
2084 }
Adam Cohen482ed822012-03-02 14:15:13 -08002085 }
Adam Cohen2acce882012-03-28 19:03:19 -07002086 mLauncher.getWorkspace().updateItemLocationsInDatabase(this);
Adam Cohen482ed822012-03-02 14:15:13 -08002087 }
2088
2089 public void setUseTempCoords(boolean useTempCoords) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07002090 int childCount = mShortcutsAndWidgets.getChildCount();
Adam Cohen482ed822012-03-02 14:15:13 -08002091 for (int i = 0; i < childCount; i++) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07002092 LayoutParams lp = (LayoutParams) mShortcutsAndWidgets.getChildAt(i).getLayoutParams();
Adam Cohen482ed822012-03-02 14:15:13 -08002093 lp.useTmpCoords = useTempCoords;
2094 }
2095 }
2096
Adam Cohen482ed822012-03-02 14:15:13 -08002097 ItemConfiguration findConfigurationNoShuffle(int pixelX, int pixelY, int minSpanX, int minSpanY,
2098 int spanX, int spanY, View dragView, ItemConfiguration solution) {
2099 int[] result = new int[2];
2100 int[] resultSpan = new int[2];
2101 findNearestVacantArea(pixelX, pixelY, minSpanX, minSpanY, spanX, spanY, null, result,
2102 resultSpan);
2103 if (result[0] >= 0 && result[1] >= 0) {
2104 copyCurrentStateToSolution(solution, false);
2105 solution.dragViewX = result[0];
2106 solution.dragViewY = result[1];
2107 solution.dragViewSpanX = resultSpan[0];
2108 solution.dragViewSpanY = resultSpan[1];
2109 solution.isSolution = true;
2110 } else {
2111 solution.isSolution = false;
2112 }
2113 return solution;
2114 }
2115
2116 public void prepareChildForDrag(View child) {
2117 markCellsAsUnoccupiedForView(child);
Adam Cohen482ed822012-03-02 14:15:13 -08002118 }
2119
Adam Cohen19f37922012-03-21 11:59:11 -07002120 /* This seems like it should be obvious and straight-forward, but when the direction vector
2121 needs to match with the notion of the dragView pushing other views, we have to employ
2122 a slightly more subtle notion of the direction vector. The question is what two points is
2123 the vector between? The center of the dragView and its desired destination? Not quite, as
2124 this doesn't necessarily coincide with the interaction of the dragView and items occupying
2125 those cells. Instead we use some heuristics to often lock the vector to up, down, left
2126 or right, which helps make pushing feel right.
2127 */
2128 private void getDirectionVectorForDrop(int dragViewCenterX, int dragViewCenterY, int spanX,
2129 int spanY, View dragView, int[] resultDirection) {
2130 int[] targetDestination = new int[2];
2131
2132 findNearestArea(dragViewCenterX, dragViewCenterY, spanX, spanY, targetDestination);
2133 Rect dragRect = new Rect();
2134 regionToRect(targetDestination[0], targetDestination[1], spanX, spanY, dragRect);
2135 dragRect.offset(dragViewCenterX - dragRect.centerX(), dragViewCenterY - dragRect.centerY());
2136
2137 Rect dropRegionRect = new Rect();
2138 getViewsIntersectingRegion(targetDestination[0], targetDestination[1], spanX, spanY,
2139 dragView, dropRegionRect, mIntersectingViews);
2140
2141 int dropRegionSpanX = dropRegionRect.width();
2142 int dropRegionSpanY = dropRegionRect.height();
2143
2144 regionToRect(dropRegionRect.left, dropRegionRect.top, dropRegionRect.width(),
2145 dropRegionRect.height(), dropRegionRect);
2146
2147 int deltaX = (dropRegionRect.centerX() - dragViewCenterX) / spanX;
2148 int deltaY = (dropRegionRect.centerY() - dragViewCenterY) / spanY;
2149
2150 if (dropRegionSpanX == mCountX || spanX == mCountX) {
2151 deltaX = 0;
2152 }
2153 if (dropRegionSpanY == mCountY || spanY == mCountY) {
2154 deltaY = 0;
2155 }
2156
2157 if (deltaX == 0 && deltaY == 0) {
2158 // No idea what to do, give a random direction.
2159 resultDirection[0] = 1;
2160 resultDirection[1] = 0;
2161 } else {
2162 computeDirectionVector(deltaX, deltaY, resultDirection);
2163 }
2164 }
2165
2166 // For a given cell and span, fetch the set of views intersecting the region.
2167 private void getViewsIntersectingRegion(int cellX, int cellY, int spanX, int spanY,
2168 View dragView, Rect boundingRect, ArrayList<View> intersectingViews) {
2169 if (boundingRect != null) {
2170 boundingRect.set(cellX, cellY, cellX + spanX, cellY + spanY);
2171 }
2172 intersectingViews.clear();
2173 Rect r0 = new Rect(cellX, cellY, cellX + spanX, cellY + spanY);
2174 Rect r1 = new Rect();
2175 final int count = mShortcutsAndWidgets.getChildCount();
2176 for (int i = 0; i < count; i++) {
2177 View child = mShortcutsAndWidgets.getChildAt(i);
2178 if (child == dragView) continue;
2179 LayoutParams lp = (LayoutParams) child.getLayoutParams();
2180 r1.set(lp.cellX, lp.cellY, lp.cellX + lp.cellHSpan, lp.cellY + lp.cellVSpan);
2181 if (Rect.intersects(r0, r1)) {
2182 mIntersectingViews.add(child);
2183 if (boundingRect != null) {
2184 boundingRect.union(r1);
2185 }
2186 }
2187 }
2188 }
2189
2190 boolean isNearestDropLocationOccupied(int pixelX, int pixelY, int spanX, int spanY,
2191 View dragView, int[] result) {
2192 result = findNearestArea(pixelX, pixelY, spanX, spanY, result);
2193 getViewsIntersectingRegion(result[0], result[1], spanX, spanY, dragView, null,
2194 mIntersectingViews);
2195 return !mIntersectingViews.isEmpty();
2196 }
2197
2198 void revertTempState() {
2199 if (!isItemPlacementDirty() || DESTRUCTIVE_REORDER) return;
2200 final int count = mShortcutsAndWidgets.getChildCount();
2201 for (int i = 0; i < count; i++) {
2202 View child = mShortcutsAndWidgets.getChildAt(i);
2203 LayoutParams lp = (LayoutParams) child.getLayoutParams();
2204 if (lp.tmpCellX != lp.cellX || lp.tmpCellY != lp.cellY) {
2205 lp.tmpCellX = lp.cellX;
2206 lp.tmpCellY = lp.cellY;
2207 animateChildToPosition(child, lp.cellX, lp.cellY, REORDER_ANIMATION_DURATION,
2208 0, false, false);
2209 }
2210 }
2211 completeAndClearReorderHintAnimations();
2212 setItemPlacementDirty(false);
2213 }
2214
Adam Cohen482ed822012-03-02 14:15:13 -08002215 int[] createArea(int pixelX, int pixelY, int minSpanX, int minSpanY, int spanX, int spanY,
2216 View dragView, int[] result, int resultSpan[], int mode) {
Adam Cohen482ed822012-03-02 14:15:13 -08002217 // First we determine if things have moved enough to cause a different layout
Adam Cohen47a876d2012-03-19 13:21:41 -07002218 result = findNearestArea(pixelX, pixelY, spanX, spanY, result);
Adam Cohen482ed822012-03-02 14:15:13 -08002219
2220 if (resultSpan == null) {
2221 resultSpan = new int[2];
2222 }
2223
Adam Cohen19f37922012-03-21 11:59:11 -07002224 // When we are checking drop validity or actually dropping, we don't recompute the
2225 // direction vector, since we want the solution to match the preview, and it's possible
2226 // that the exact position of the item has changed to result in a new reordering outcome.
Adam Cohenb209e632012-03-27 17:09:36 -07002227 if ((mode == MODE_ON_DROP || mode == MODE_ON_DROP_EXTERNAL || mode == MODE_ACCEPT_DROP)
2228 && mPreviousReorderDirection[0] != INVALID_DIRECTION) {
Adam Cohen19f37922012-03-21 11:59:11 -07002229 mDirectionVector[0] = mPreviousReorderDirection[0];
2230 mDirectionVector[1] = mPreviousReorderDirection[1];
2231 // We reset this vector after drop
Adam Cohenb209e632012-03-27 17:09:36 -07002232 if (mode == MODE_ON_DROP || mode == MODE_ON_DROP_EXTERNAL) {
2233 mPreviousReorderDirection[0] = INVALID_DIRECTION;
2234 mPreviousReorderDirection[1] = INVALID_DIRECTION;
Adam Cohen19f37922012-03-21 11:59:11 -07002235 }
Adam Cohenb209e632012-03-27 17:09:36 -07002236
Adam Cohen19f37922012-03-21 11:59:11 -07002237 } else {
2238 getDirectionVectorForDrop(pixelX, pixelY, spanX, spanY, dragView, mDirectionVector);
2239 mPreviousReorderDirection[0] = mDirectionVector[0];
2240 mPreviousReorderDirection[1] = mDirectionVector[1];
2241 }
2242
Adam Cohen482ed822012-03-02 14:15:13 -08002243 ItemConfiguration swapSolution = simpleSwap(pixelX, pixelY, minSpanX, minSpanY,
2244 spanX, spanY, mDirectionVector, dragView, true, new ItemConfiguration());
2245
2246 // We attempt the approach which doesn't shuffle views at all
2247 ItemConfiguration noShuffleSolution = findConfigurationNoShuffle(pixelX, pixelY, minSpanX,
2248 minSpanY, spanX, spanY, dragView, new ItemConfiguration());
2249
2250 ItemConfiguration finalSolution = null;
2251 if (swapSolution.isSolution && swapSolution.area() >= noShuffleSolution.area()) {
2252 finalSolution = swapSolution;
2253 } else if (noShuffleSolution.isSolution) {
2254 finalSolution = noShuffleSolution;
2255 }
2256
2257 boolean foundSolution = true;
2258 if (!DESTRUCTIVE_REORDER) {
2259 setUseTempCoords(true);
2260 }
2261
2262 if (finalSolution != null) {
2263 result[0] = finalSolution.dragViewX;
2264 result[1] = finalSolution.dragViewY;
2265 resultSpan[0] = finalSolution.dragViewSpanX;
2266 resultSpan[1] = finalSolution.dragViewSpanY;
2267
2268 // If we're just testing for a possible location (MODE_ACCEPT_DROP), we don't bother
2269 // committing anything or animating anything as we just want to determine if a solution
2270 // exists
2271 if (mode == MODE_DRAG_OVER || mode == MODE_ON_DROP || mode == MODE_ON_DROP_EXTERNAL) {
2272 if (!DESTRUCTIVE_REORDER) {
2273 copySolutionToTempState(finalSolution, dragView);
2274 }
2275 setItemPlacementDirty(true);
2276 animateItemsToSolution(finalSolution, dragView, mode == MODE_ON_DROP);
2277
Adam Cohen19f37922012-03-21 11:59:11 -07002278 if (!DESTRUCTIVE_REORDER &&
2279 (mode == MODE_ON_DROP || mode == MODE_ON_DROP_EXTERNAL)) {
Adam Cohen482ed822012-03-02 14:15:13 -08002280 commitTempPlacement();
Adam Cohen19f37922012-03-21 11:59:11 -07002281 completeAndClearReorderHintAnimations();
2282 setItemPlacementDirty(false);
2283 } else {
2284 beginOrAdjustHintAnimations(finalSolution, dragView,
2285 REORDER_ANIMATION_DURATION);
Adam Cohen482ed822012-03-02 14:15:13 -08002286 }
2287 }
2288 } else {
2289 foundSolution = false;
2290 result[0] = result[1] = resultSpan[0] = resultSpan[1] = -1;
2291 }
2292
2293 if ((mode == MODE_ON_DROP || !foundSolution) && !DESTRUCTIVE_REORDER) {
2294 setUseTempCoords(false);
2295 }
Adam Cohen482ed822012-03-02 14:15:13 -08002296
Michael Jurkaa52570f2012-03-20 03:18:20 -07002297 mShortcutsAndWidgets.requestLayout();
Adam Cohen482ed822012-03-02 14:15:13 -08002298 return result;
2299 }
2300
Adam Cohen19f37922012-03-21 11:59:11 -07002301 void setItemPlacementDirty(boolean dirty) {
2302 mItemPlacementDirty = dirty;
Adam Cohen482ed822012-03-02 14:15:13 -08002303 }
Adam Cohen19f37922012-03-21 11:59:11 -07002304 boolean isItemPlacementDirty() {
2305 return mItemPlacementDirty;
Adam Cohen482ed822012-03-02 14:15:13 -08002306 }
2307
2308 private class ItemConfiguration {
Adam Cohen8baab352012-03-20 17:39:21 -07002309 HashMap<View, CellAndSpan> map = new HashMap<View, CellAndSpan>();
Adam Cohen482ed822012-03-02 14:15:13 -08002310 boolean isSolution = false;
2311 int dragViewX, dragViewY, dragViewSpanX, dragViewSpanY;
2312
2313 int area() {
2314 return dragViewSpanX * dragViewSpanY;
2315 }
Adam Cohen8baab352012-03-20 17:39:21 -07002316 }
2317
2318 private class CellAndSpan {
2319 int x, y;
2320 int spanX, spanY;
2321
2322 public CellAndSpan(int x, int y, int spanX, int spanY) {
2323 this.x = x;
2324 this.y = y;
2325 this.spanX = spanX;
2326 this.spanY = spanY;
Adam Cohen482ed822012-03-02 14:15:13 -08002327 }
2328 }
2329
Adam Cohendf035382011-04-11 17:22:04 -07002330 /**
2331 * Find a vacant area that will fit the given bounds nearest the requested
2332 * cell location. Uses Euclidean distance to score multiple vacant areas.
2333 *
2334 * @param pixelX The X location at which you want to search for a vacant area.
2335 * @param pixelY The Y location at which you want to search for a vacant area.
2336 * @param spanX Horizontal span of the object.
2337 * @param spanY Vertical span of the object.
2338 * @param ignoreView Considers space occupied by this view as unoccupied
2339 * @param result Previously returned value to possibly recycle.
2340 * @return The X, Y cell of a vacant area that can contain this object,
2341 * nearest the requested location.
2342 */
2343 int[] findNearestVacantArea(
2344 int pixelX, int pixelY, int spanX, int spanY, View ignoreView, int[] result) {
2345 return findNearestArea(pixelX, pixelY, spanX, spanY, ignoreView, true, result);
2346 }
2347
2348 /**
Adam Cohend41fbf52012-02-16 23:53:59 -08002349 * Find a vacant area that will fit the given bounds nearest the requested
2350 * cell location. Uses Euclidean distance to score multiple vacant areas.
2351 *
2352 * @param pixelX The X location at which you want to search for a vacant area.
2353 * @param pixelY The Y location at which you want to search for a vacant area.
2354 * @param minSpanX The minimum horizontal span required
2355 * @param minSpanY The minimum vertical span required
2356 * @param spanX Horizontal span of the object.
2357 * @param spanY Vertical span of the object.
2358 * @param ignoreView Considers space occupied by this view as unoccupied
2359 * @param result Previously returned value to possibly recycle.
2360 * @return The X, Y cell of a vacant area that can contain this object,
2361 * nearest the requested location.
2362 */
2363 int[] findNearestVacantArea(int pixelX, int pixelY, int minSpanX, int minSpanY,
2364 int spanX, int spanY, View ignoreView, int[] result, int[] resultSpan) {
Adam Cohen482ed822012-03-02 14:15:13 -08002365 return findNearestArea(pixelX, pixelY, minSpanX, minSpanY, spanX, spanY, ignoreView, true,
2366 result, resultSpan, mOccupied);
Adam Cohend41fbf52012-02-16 23:53:59 -08002367 }
2368
2369 /**
Adam Cohendf035382011-04-11 17:22:04 -07002370 * Find a starting cell position that will fit the given bounds nearest the requested
2371 * cell location. Uses Euclidean distance to score multiple vacant areas.
2372 *
2373 * @param pixelX The X location at which you want to search for a vacant area.
2374 * @param pixelY The Y location at which you want to search for a vacant area.
2375 * @param spanX Horizontal span of the object.
2376 * @param spanY Vertical span of the object.
2377 * @param ignoreView Considers space occupied by this view as unoccupied
2378 * @param result Previously returned value to possibly recycle.
2379 * @return The X, Y cell of a vacant area that can contain this object,
2380 * nearest the requested location.
2381 */
2382 int[] findNearestArea(
2383 int pixelX, int pixelY, int spanX, int spanY, int[] result) {
2384 return findNearestArea(pixelX, pixelY, spanX, spanY, null, false, result);
2385 }
2386
Michael Jurka0280c3b2010-09-17 15:00:07 -07002387 boolean existsEmptyCell() {
2388 return findCellForSpan(null, 1, 1);
2389 }
2390
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002391 /**
Michael Jurka0280c3b2010-09-17 15:00:07 -07002392 * Finds the upper-left coordinate of the first rectangle in the grid that can
2393 * hold a cell of the specified dimensions. If intersectX and intersectY are not -1,
2394 * then this method will only return coordinates for rectangles that contain the cell
2395 * (intersectX, intersectY)
2396 *
2397 * @param cellXY The array that will contain the position of a vacant cell if such a cell
2398 * can be found.
2399 * @param spanX The horizontal span of the cell we want to find.
2400 * @param spanY The vertical span of the cell we want to find.
2401 *
2402 * @return True if a vacant cell of the specified dimension was found, false otherwise.
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07002403 */
Michael Jurka0280c3b2010-09-17 15:00:07 -07002404 boolean findCellForSpan(int[] cellXY, int spanX, int spanY) {
Adam Cohen482ed822012-03-02 14:15:13 -08002405 return findCellForSpanThatIntersectsIgnoring(cellXY, spanX, spanY, -1, -1, null, mOccupied);
Michael Jurka0280c3b2010-09-17 15:00:07 -07002406 }
2407
2408 /**
2409 * Like above, but ignores any cells occupied by the item "ignoreView"
2410 *
2411 * @param cellXY The array that will contain the position of a vacant cell if such a cell
2412 * can be found.
2413 * @param spanX The horizontal span of the cell we want to find.
2414 * @param spanY The vertical span of the cell we want to find.
2415 * @param ignoreView The home screen item we should treat as not occupying any space
2416 * @return
2417 */
2418 boolean findCellForSpanIgnoring(int[] cellXY, int spanX, int spanY, View ignoreView) {
Adam Cohen482ed822012-03-02 14:15:13 -08002419 return findCellForSpanThatIntersectsIgnoring(cellXY, spanX, spanY, -1, -1,
2420 ignoreView, mOccupied);
Michael Jurka0280c3b2010-09-17 15:00:07 -07002421 }
2422
2423 /**
2424 * Like above, but if intersectX and intersectY are not -1, then this method will try to
2425 * return coordinates for rectangles that contain the cell [intersectX, intersectY]
2426 *
2427 * @param spanX The horizontal span of the cell we want to find.
2428 * @param spanY The vertical span of the cell we want to find.
2429 * @param ignoreView The home screen item we should treat as not occupying any space
2430 * @param intersectX The X coordinate of the cell that we should try to overlap
2431 * @param intersectX The Y coordinate of the cell that we should try to overlap
2432 *
2433 * @return True if a vacant cell of the specified dimension was found, false otherwise.
2434 */
2435 boolean findCellForSpanThatIntersects(int[] cellXY, int spanX, int spanY,
2436 int intersectX, int intersectY) {
2437 return findCellForSpanThatIntersectsIgnoring(
Adam Cohen482ed822012-03-02 14:15:13 -08002438 cellXY, spanX, spanY, intersectX, intersectY, null, mOccupied);
Michael Jurka0280c3b2010-09-17 15:00:07 -07002439 }
2440
2441 /**
2442 * The superset of the above two methods
2443 */
2444 boolean findCellForSpanThatIntersectsIgnoring(int[] cellXY, int spanX, int spanY,
Adam Cohen482ed822012-03-02 14:15:13 -08002445 int intersectX, int intersectY, View ignoreView, boolean occupied[][]) {
Michael Jurkac6ee42e2010-09-30 12:04:50 -07002446 // mark space take by ignoreView as available (method checks if ignoreView is null)
Adam Cohen482ed822012-03-02 14:15:13 -08002447 markCellsAsUnoccupiedForView(ignoreView, occupied);
Michael Jurka0280c3b2010-09-17 15:00:07 -07002448
Michael Jurka28750fb2010-09-24 17:43:49 -07002449 boolean foundCell = false;
Michael Jurka0280c3b2010-09-17 15:00:07 -07002450 while (true) {
2451 int startX = 0;
2452 if (intersectX >= 0) {
2453 startX = Math.max(startX, intersectX - (spanX - 1));
2454 }
2455 int endX = mCountX - (spanX - 1);
2456 if (intersectX >= 0) {
2457 endX = Math.min(endX, intersectX + (spanX - 1) + (spanX == 1 ? 1 : 0));
2458 }
2459 int startY = 0;
2460 if (intersectY >= 0) {
2461 startY = Math.max(startY, intersectY - (spanY - 1));
2462 }
2463 int endY = mCountY - (spanY - 1);
2464 if (intersectY >= 0) {
2465 endY = Math.min(endY, intersectY + (spanY - 1) + (spanY == 1 ? 1 : 0));
2466 }
2467
Winson Chungbbc60d82010-11-11 16:34:41 -08002468 for (int y = startY; y < endY && !foundCell; y++) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002469 inner:
Winson Chungbbc60d82010-11-11 16:34:41 -08002470 for (int x = startX; x < endX; x++) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002471 for (int i = 0; i < spanX; i++) {
2472 for (int j = 0; j < spanY; j++) {
Adam Cohen482ed822012-03-02 14:15:13 -08002473 if (occupied[x + i][y + j]) {
Winson Chungbbc60d82010-11-11 16:34:41 -08002474 // small optimization: we can skip to after the column we just found
Michael Jurka0280c3b2010-09-17 15:00:07 -07002475 // an occupied cell
Winson Chungbbc60d82010-11-11 16:34:41 -08002476 x += i;
Michael Jurka0280c3b2010-09-17 15:00:07 -07002477 continue inner;
2478 }
2479 }
2480 }
2481 if (cellXY != null) {
2482 cellXY[0] = x;
2483 cellXY[1] = y;
2484 }
Michael Jurka28750fb2010-09-24 17:43:49 -07002485 foundCell = true;
2486 break;
Michael Jurka0280c3b2010-09-17 15:00:07 -07002487 }
2488 }
2489 if (intersectX == -1 && intersectY == -1) {
2490 break;
2491 } else {
2492 // if we failed to find anything, try again but without any requirements of
2493 // intersecting
2494 intersectX = -1;
2495 intersectY = -1;
2496 continue;
2497 }
2498 }
2499
Michael Jurkac6ee42e2010-09-30 12:04:50 -07002500 // re-mark space taken by ignoreView as occupied
Adam Cohen482ed822012-03-02 14:15:13 -08002501 markCellsAsOccupiedForView(ignoreView, occupied);
Michael Jurka28750fb2010-09-24 17:43:49 -07002502 return foundCell;
Michael Jurka0280c3b2010-09-17 15:00:07 -07002503 }
2504
2505 /**
Winson Chungc07918d2011-07-01 15:35:26 -07002506 * A drag event has begun over this layout.
2507 * It may have begun over this layout (in which case onDragChild is called first),
2508 * or it may have begun on another layout.
2509 */
2510 void onDragEnter() {
2511 if (!mDragging) {
2512 // Fade in the drag indicators
2513 if (mCrosshairsAnimator != null) {
2514 mCrosshairsAnimator.animateIn();
2515 }
2516 }
2517 mDragging = true;
2518 }
2519
2520 /**
Michael Jurka0280c3b2010-09-17 15:00:07 -07002521 * Called when drag has left this CellLayout or has been completed (successfully or not)
2522 */
2523 void onDragExit() {
Joe Onorato4be866d2010-10-10 11:26:02 -07002524 // This can actually be called when we aren't in a drag, e.g. when adding a new
2525 // item to this layout via the customize drawer.
2526 // Guard against that case.
2527 if (mDragging) {
2528 mDragging = false;
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07002529
Joe Onorato4be866d2010-10-10 11:26:02 -07002530 // Fade out the drag indicators
2531 if (mCrosshairsAnimator != null) {
2532 mCrosshairsAnimator.animateOut();
2533 }
Patrick Dubroyde7658b2010-09-27 11:15:43 -07002534 }
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -07002535
2536 // Invalidate the drag data
Adam Cohend41fbf52012-02-16 23:53:59 -08002537 mDragCell[0] = mDragCell[1] = -1;
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -07002538 mDragOutlineAnims[mDragOutlineCurrent].animateOut();
2539 mDragOutlineCurrent = (mDragOutlineCurrent + 1) % mDragOutlineAnims.length;
Adam Cohen19f37922012-03-21 11:59:11 -07002540 revertTempState();
Michael Jurka33945b22010-12-21 18:19:38 -08002541 setIsDragOverlapping(false);
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07002542 }
2543
2544 /**
Winson Chungaafa03c2010-06-11 17:34:16 -07002545 * Mark a child as having been dropped.
Patrick Dubroyde7658b2010-09-27 11:15:43 -07002546 * At the beginning of the drag operation, the child may have been on another
Patrick Dubroyce34a972010-10-19 10:34:32 -07002547 * screen, but it is re-parented before this method is called.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002548 *
2549 * @param child The child that is being dropped
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002550 */
Adam Cohen716b51e2011-06-30 12:09:54 -07002551 void onDropChild(View child) {
Romain Guyd94533d2009-08-17 10:01:15 -07002552 if (child != null) {
2553 LayoutParams lp = (LayoutParams) child.getLayoutParams();
Romain Guy84f296c2009-11-04 15:00:44 -08002554 lp.dropped = true;
Romain Guyd94533d2009-08-17 10:01:15 -07002555 child.requestLayout();
Romain Guyd94533d2009-08-17 10:01:15 -07002556 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002557 }
2558
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002559 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002560 * Computes a bounding rectangle for a range of cells
Winson Chungaafa03c2010-06-11 17:34:16 -07002561 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002562 * @param cellX X coordinate of upper left corner expressed as a cell position
2563 * @param cellY Y coordinate of upper left corner expressed as a cell position
Winson Chungaafa03c2010-06-11 17:34:16 -07002564 * @param cellHSpan Width in cells
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002565 * @param cellVSpan Height in cells
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07002566 * @param resultRect Rect into which to put the results
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002567 */
Adam Cohend41fbf52012-02-16 23:53:59 -08002568 public void cellToRect(int cellX, int cellY, int cellHSpan, int cellVSpan, Rect resultRect) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002569 final int cellWidth = mCellWidth;
2570 final int cellHeight = mCellHeight;
2571 final int widthGap = mWidthGap;
2572 final int heightGap = mHeightGap;
Winson Chungaafa03c2010-06-11 17:34:16 -07002573
Winson Chung4b825dcd2011-06-19 12:41:22 -07002574 final int hStartPadding = getPaddingLeft();
2575 final int vStartPadding = getPaddingTop();
Winson Chungaafa03c2010-06-11 17:34:16 -07002576
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002577 int width = cellHSpan * cellWidth + ((cellHSpan - 1) * widthGap);
2578 int height = cellVSpan * cellHeight + ((cellVSpan - 1) * heightGap);
2579
2580 int x = hStartPadding + cellX * (cellWidth + widthGap);
2581 int y = vStartPadding + cellY * (cellHeight + heightGap);
Winson Chungaafa03c2010-06-11 17:34:16 -07002582
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07002583 resultRect.set(x, y, x + width, y + height);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002584 }
Winson Chungaafa03c2010-06-11 17:34:16 -07002585
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002586 /**
Winson Chungaafa03c2010-06-11 17:34:16 -07002587 * Computes the required horizontal and vertical cell spans to always
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002588 * fit the given rectangle.
Winson Chungaafa03c2010-06-11 17:34:16 -07002589 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002590 * @param width Width in pixels
2591 * @param height Height in pixels
Patrick Dubroy8f86ddc2010-07-16 13:55:32 -07002592 * @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 -08002593 */
Patrick Dubroy8f86ddc2010-07-16 13:55:32 -07002594 public int[] rectToCell(int width, int height, int[] result) {
Michael Jurka9987a5c2010-10-08 16:58:12 -07002595 return rectToCell(getResources(), width, height, result);
2596 }
2597
2598 public static int[] rectToCell(Resources resources, int width, int height, int[] result) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002599 // Always assume we're working with the smallest span to make sure we
2600 // reserve enough space in both orientations.
Joe Onorato79e56262009-09-21 15:23:04 -04002601 int actualWidth = resources.getDimensionPixelSize(R.dimen.workspace_cell_width);
2602 int actualHeight = resources.getDimensionPixelSize(R.dimen.workspace_cell_height);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002603 int smallerSize = Math.min(actualWidth, actualHeight);
Joe Onorato79e56262009-09-21 15:23:04 -04002604
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002605 // Always round up to next largest cell
Winson Chung54c725c2011-08-03 12:03:40 -07002606 int spanX = (int) Math.ceil(width / (float) smallerSize);
2607 int spanY = (int) Math.ceil(height / (float) smallerSize);
Joe Onorato79e56262009-09-21 15:23:04 -04002608
Patrick Dubroy8f86ddc2010-07-16 13:55:32 -07002609 if (result == null) {
2610 return new int[] { spanX, spanY };
2611 }
2612 result[0] = spanX;
2613 result[1] = spanY;
2614 return result;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002615 }
2616
Michael Jurkaf12c75c2011-01-25 22:41:40 -08002617 public int[] cellSpansToSize(int hSpans, int vSpans) {
2618 int[] size = new int[2];
2619 size[0] = hSpans * mCellWidth + (hSpans - 1) * mWidthGap;
2620 size[1] = vSpans * mCellHeight + (vSpans - 1) * mHeightGap;
2621 return size;
2622 }
2623
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002624 /**
Patrick Dubroy047379a2010-12-19 22:02:04 -08002625 * Calculate the grid spans needed to fit given item
2626 */
2627 public void calculateSpans(ItemInfo info) {
2628 final int minWidth;
2629 final int minHeight;
2630
2631 if (info instanceof LauncherAppWidgetInfo) {
2632 minWidth = ((LauncherAppWidgetInfo) info).minWidth;
2633 minHeight = ((LauncherAppWidgetInfo) info).minHeight;
2634 } else if (info instanceof PendingAddWidgetInfo) {
2635 minWidth = ((PendingAddWidgetInfo) info).minWidth;
2636 minHeight = ((PendingAddWidgetInfo) info).minHeight;
2637 } else {
2638 // It's not a widget, so it must be 1x1
2639 info.spanX = info.spanY = 1;
2640 return;
2641 }
2642 int[] spans = rectToCell(minWidth, minHeight, null);
2643 info.spanX = spans[0];
2644 info.spanY = spans[1];
2645 }
2646
2647 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002648 * Find the first vacant cell, if there is one.
2649 *
2650 * @param vacant Holds the x and y coordinate of the vacant cell
2651 * @param spanX Horizontal cell span.
2652 * @param spanY Vertical cell span.
Winson Chungaafa03c2010-06-11 17:34:16 -07002653 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002654 * @return True if a vacant cell was found
2655 */
2656 public boolean getVacantCell(int[] vacant, int spanX, int spanY) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002657
Michael Jurka0280c3b2010-09-17 15:00:07 -07002658 return findVacantCell(vacant, spanX, spanY, mCountX, mCountY, mOccupied);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002659 }
2660
2661 static boolean findVacantCell(int[] vacant, int spanX, int spanY,
2662 int xCount, int yCount, boolean[][] occupied) {
2663
Adam Cohen2801caf2011-05-13 20:57:39 -07002664 for (int y = 0; y < yCount; y++) {
2665 for (int x = 0; x < xCount; x++) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002666 boolean available = !occupied[x][y];
2667out: for (int i = x; i < x + spanX - 1 && x < xCount; i++) {
2668 for (int j = y; j < y + spanY - 1 && y < yCount; j++) {
2669 available = available && !occupied[i][j];
2670 if (!available) break out;
2671 }
2672 }
2673
2674 if (available) {
2675 vacant[0] = x;
2676 vacant[1] = y;
2677 return true;
2678 }
2679 }
2680 }
2681
2682 return false;
2683 }
2684
Michael Jurka0280c3b2010-09-17 15:00:07 -07002685 private void clearOccupiedCells() {
2686 for (int x = 0; x < mCountX; x++) {
2687 for (int y = 0; y < mCountY; y++) {
2688 mOccupied[x][y] = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002689 }
2690 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07002691 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002692
Adam Cohen1b607ed2011-03-03 17:26:50 -08002693 /**
2694 * Given a view, determines how much that view can be expanded in all directions, in terms of
2695 * whether or not there are other items occupying adjacent cells. Used by the
2696 * AppWidgetResizeFrame to determine how the widget can be resized.
2697 */
Adam Cohend4844c32011-02-18 19:25:06 -08002698 public void getExpandabilityArrayForView(View view, int[] expandability) {
Adam Cohen1b607ed2011-03-03 17:26:50 -08002699 final LayoutParams lp = (LayoutParams) view.getLayoutParams();
Adam Cohend4844c32011-02-18 19:25:06 -08002700 boolean flag;
2701
Adam Cohen1b607ed2011-03-03 17:26:50 -08002702 expandability[AppWidgetResizeFrame.LEFT] = 0;
Adam Cohend4844c32011-02-18 19:25:06 -08002703 for (int x = lp.cellX - 1; x >= 0; x--) {
2704 flag = false;
2705 for (int y = lp.cellY; y < lp.cellY + lp.cellVSpan; y++) {
2706 if (mOccupied[x][y]) flag = true;
2707 }
2708 if (flag) break;
Adam Cohen1b607ed2011-03-03 17:26:50 -08002709 expandability[AppWidgetResizeFrame.LEFT]++;
Adam Cohend4844c32011-02-18 19:25:06 -08002710 }
2711
Adam Cohen1b607ed2011-03-03 17:26:50 -08002712 expandability[AppWidgetResizeFrame.TOP] = 0;
Adam Cohend4844c32011-02-18 19:25:06 -08002713 for (int y = lp.cellY - 1; y >= 0; y--) {
2714 flag = false;
2715 for (int x = lp.cellX; x < lp.cellX + lp.cellHSpan; x++) {
2716 if (mOccupied[x][y]) flag = true;
2717 }
2718 if (flag) break;
Adam Cohen1b607ed2011-03-03 17:26:50 -08002719 expandability[AppWidgetResizeFrame.TOP]++;
2720 }
Adam Cohend4844c32011-02-18 19:25:06 -08002721
Adam Cohen1b607ed2011-03-03 17:26:50 -08002722 expandability[AppWidgetResizeFrame.RIGHT] = 0;
Adam Cohend4844c32011-02-18 19:25:06 -08002723 for (int x = lp.cellX + lp.cellHSpan; x < mCountX; x++) {
2724 flag = false;
2725 for (int y = lp.cellY; y < lp.cellY + lp.cellVSpan; y++) {
2726 if (mOccupied[x][y]) flag = true;
2727 }
2728 if (flag) break;
Adam Cohen1b607ed2011-03-03 17:26:50 -08002729 expandability[AppWidgetResizeFrame.RIGHT]++;
2730 }
Adam Cohend4844c32011-02-18 19:25:06 -08002731
Adam Cohen1b607ed2011-03-03 17:26:50 -08002732 expandability[AppWidgetResizeFrame.BOTTOM] = 0;
Adam Cohend4844c32011-02-18 19:25:06 -08002733 for (int y = lp.cellY + lp.cellVSpan; y < mCountY; y++) {
2734 flag = false;
2735 for (int x = lp.cellX; x < lp.cellX + lp.cellHSpan; x++) {
2736 if (mOccupied[x][y]) flag = true;
2737 }
2738 if (flag) break;
Adam Cohen1b607ed2011-03-03 17:26:50 -08002739 expandability[AppWidgetResizeFrame.BOTTOM]++;
2740 }
Adam Cohend4844c32011-02-18 19:25:06 -08002741 }
2742
Adam Cohend41fbf52012-02-16 23:53:59 -08002743 public void onMove(View view, int newCellX, int newCellY, int newSpanX, int newSpanY) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002744 markCellsAsUnoccupiedForView(view);
Adam Cohen482ed822012-03-02 14:15:13 -08002745 markCellsForView(newCellX, newCellY, newSpanX, newSpanY, mOccupied, true);
Michael Jurka0280c3b2010-09-17 15:00:07 -07002746 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002747
Adam Cohend4844c32011-02-18 19:25:06 -08002748 public void markCellsAsOccupiedForView(View view) {
Adam Cohen482ed822012-03-02 14:15:13 -08002749 markCellsAsOccupiedForView(view, mOccupied);
2750 }
2751 public void markCellsAsOccupiedForView(View view, boolean[][] occupied) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07002752 if (view == null || view.getParent() != mShortcutsAndWidgets) return;
Michael Jurka0280c3b2010-09-17 15:00:07 -07002753 LayoutParams lp = (LayoutParams) view.getLayoutParams();
Adam Cohen482ed822012-03-02 14:15:13 -08002754 markCellsForView(lp.cellX, lp.cellY, lp.cellHSpan, lp.cellVSpan, occupied, true);
Michael Jurka0280c3b2010-09-17 15:00:07 -07002755 }
2756
Adam Cohend4844c32011-02-18 19:25:06 -08002757 public void markCellsAsUnoccupiedForView(View view) {
Adam Cohen482ed822012-03-02 14:15:13 -08002758 markCellsAsUnoccupiedForView(view, mOccupied);
2759 }
2760 public void markCellsAsUnoccupiedForView(View view, boolean occupied[][]) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07002761 if (view == null || view.getParent() != mShortcutsAndWidgets) return;
Michael Jurka0280c3b2010-09-17 15:00:07 -07002762 LayoutParams lp = (LayoutParams) view.getLayoutParams();
Adam Cohen482ed822012-03-02 14:15:13 -08002763 markCellsForView(lp.cellX, lp.cellY, lp.cellHSpan, lp.cellVSpan, occupied, false);
Michael Jurka0280c3b2010-09-17 15:00:07 -07002764 }
2765
Adam Cohen482ed822012-03-02 14:15:13 -08002766 private void markCellsForView(int cellX, int cellY, int spanX, int spanY, boolean[][] occupied,
2767 boolean value) {
2768 if (cellX < 0 || cellY < 0) return;
Michael Jurka0280c3b2010-09-17 15:00:07 -07002769 for (int x = cellX; x < cellX + spanX && x < mCountX; x++) {
2770 for (int y = cellY; y < cellY + spanY && y < mCountY; y++) {
Adam Cohen482ed822012-03-02 14:15:13 -08002771 occupied[x][y] = value;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002772 }
2773 }
2774 }
2775
Adam Cohen2801caf2011-05-13 20:57:39 -07002776 public int getDesiredWidth() {
Winson Chung4b825dcd2011-06-19 12:41:22 -07002777 return mPaddingLeft + mPaddingRight + (mCountX * mCellWidth) +
Adam Cohen2801caf2011-05-13 20:57:39 -07002778 (Math.max((mCountX - 1), 0) * mWidthGap);
2779 }
2780
2781 public int getDesiredHeight() {
Winson Chung4b825dcd2011-06-19 12:41:22 -07002782 return mPaddingTop + mPaddingBottom + (mCountY * mCellHeight) +
Adam Cohen2801caf2011-05-13 20:57:39 -07002783 (Math.max((mCountY - 1), 0) * mHeightGap);
2784 }
2785
Michael Jurka66d72172011-04-12 16:29:25 -07002786 public boolean isOccupied(int x, int y) {
2787 if (x < mCountX && y < mCountY) {
2788 return mOccupied[x][y];
2789 } else {
2790 throw new RuntimeException("Position exceeds the bound of this CellLayout");
2791 }
2792 }
2793
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002794 @Override
2795 public ViewGroup.LayoutParams generateLayoutParams(AttributeSet attrs) {
2796 return new CellLayout.LayoutParams(getContext(), attrs);
2797 }
2798
2799 @Override
2800 protected boolean checkLayoutParams(ViewGroup.LayoutParams p) {
2801 return p instanceof CellLayout.LayoutParams;
2802 }
2803
2804 @Override
2805 protected ViewGroup.LayoutParams generateLayoutParams(ViewGroup.LayoutParams p) {
2806 return new CellLayout.LayoutParams(p);
2807 }
2808
Winson Chungaafa03c2010-06-11 17:34:16 -07002809 public static class CellLayoutAnimationController extends LayoutAnimationController {
2810 public CellLayoutAnimationController(Animation animation, float delay) {
2811 super(animation, delay);
2812 }
2813
2814 @Override
2815 protected long getDelayForView(View view) {
2816 return (int) (Math.random() * 150);
2817 }
2818 }
2819
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002820 public static class LayoutParams extends ViewGroup.MarginLayoutParams {
2821 /**
2822 * Horizontal location of the item in the grid.
2823 */
2824 @ViewDebug.ExportedProperty
2825 public int cellX;
2826
2827 /**
2828 * Vertical location of the item in the grid.
2829 */
2830 @ViewDebug.ExportedProperty
2831 public int cellY;
2832
2833 /**
Adam Cohen482ed822012-03-02 14:15:13 -08002834 * Temporary horizontal location of the item in the grid during reorder
2835 */
2836 public int tmpCellX;
2837
2838 /**
2839 * Temporary vertical location of the item in the grid during reorder
2840 */
2841 public int tmpCellY;
2842
2843 /**
2844 * Indicates that the temporary coordinates should be used to layout the items
2845 */
2846 public boolean useTmpCoords;
2847
2848 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002849 * Number of cells spanned horizontally by the item.
2850 */
2851 @ViewDebug.ExportedProperty
2852 public int cellHSpan;
2853
2854 /**
2855 * Number of cells spanned vertically by the item.
2856 */
2857 @ViewDebug.ExportedProperty
2858 public int cellVSpan;
Winson Chungaafa03c2010-06-11 17:34:16 -07002859
Adam Cohen1b607ed2011-03-03 17:26:50 -08002860 /**
2861 * Indicates whether the item will set its x, y, width and height parameters freely,
2862 * or whether these will be computed based on cellX, cellY, cellHSpan and cellVSpan.
2863 */
Adam Cohend4844c32011-02-18 19:25:06 -08002864 public boolean isLockedToGrid = true;
2865
Adam Cohen482ed822012-03-02 14:15:13 -08002866 /**
2867 * Indicates whether this item can be reordered. Always true except in the case of the
2868 * the AllApps button.
2869 */
2870 public boolean canReorder = true;
2871
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002872 // X coordinate of the view in the layout.
2873 @ViewDebug.ExportedProperty
2874 int x;
2875 // Y coordinate of the view in the layout.
2876 @ViewDebug.ExportedProperty
2877 int y;
2878
Romain Guy84f296c2009-11-04 15:00:44 -08002879 boolean dropped;
Romain Guyfcb9e712009-10-02 16:06:52 -07002880
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002881 public LayoutParams(Context c, AttributeSet attrs) {
2882 super(c, attrs);
2883 cellHSpan = 1;
2884 cellVSpan = 1;
2885 }
2886
2887 public LayoutParams(ViewGroup.LayoutParams source) {
2888 super(source);
2889 cellHSpan = 1;
2890 cellVSpan = 1;
2891 }
Winson Chungaafa03c2010-06-11 17:34:16 -07002892
2893 public LayoutParams(LayoutParams source) {
2894 super(source);
2895 this.cellX = source.cellX;
2896 this.cellY = source.cellY;
2897 this.cellHSpan = source.cellHSpan;
2898 this.cellVSpan = source.cellVSpan;
2899 }
2900
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002901 public LayoutParams(int cellX, int cellY, int cellHSpan, int cellVSpan) {
Romain Guy8f19cdd2010-01-08 15:07:00 -08002902 super(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002903 this.cellX = cellX;
2904 this.cellY = cellY;
2905 this.cellHSpan = cellHSpan;
2906 this.cellVSpan = cellVSpan;
2907 }
2908
Adam Cohen7f4eabe2011-04-21 16:19:16 -07002909 public void setup(int cellWidth, int cellHeight, int widthGap, int heightGap) {
Adam Cohend4844c32011-02-18 19:25:06 -08002910 if (isLockedToGrid) {
2911 final int myCellHSpan = cellHSpan;
2912 final int myCellVSpan = cellVSpan;
Adam Cohen482ed822012-03-02 14:15:13 -08002913 final int myCellX = useTmpCoords ? tmpCellX : cellX;
2914 final int myCellY = useTmpCoords ? tmpCellY : cellY;
Adam Cohen1b607ed2011-03-03 17:26:50 -08002915
Adam Cohend4844c32011-02-18 19:25:06 -08002916 width = myCellHSpan * cellWidth + ((myCellHSpan - 1) * widthGap) -
2917 leftMargin - rightMargin;
2918 height = myCellVSpan * cellHeight + ((myCellVSpan - 1) * heightGap) -
2919 topMargin - bottomMargin;
Winson Chungeecf02d2012-03-02 17:14:58 -08002920 x = (int) (myCellX * (cellWidth + widthGap) + leftMargin);
2921 y = (int) (myCellY * (cellHeight + heightGap) + topMargin);
Adam Cohend4844c32011-02-18 19:25:06 -08002922 }
2923 }
Winson Chungaafa03c2010-06-11 17:34:16 -07002924
Winson Chungaafa03c2010-06-11 17:34:16 -07002925 public String toString() {
2926 return "(" + this.cellX + ", " + this.cellY + ")";
2927 }
Adam Cohen7f4eabe2011-04-21 16:19:16 -07002928
2929 public void setWidth(int width) {
2930 this.width = width;
2931 }
2932
2933 public int getWidth() {
2934 return width;
2935 }
2936
2937 public void setHeight(int height) {
2938 this.height = height;
2939 }
2940
2941 public int getHeight() {
2942 return height;
2943 }
2944
2945 public void setX(int x) {
2946 this.x = x;
2947 }
2948
2949 public int getX() {
2950 return x;
2951 }
2952
2953 public void setY(int y) {
2954 this.y = y;
2955 }
2956
2957 public int getY() {
2958 return y;
2959 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002960 }
2961
Michael Jurka0280c3b2010-09-17 15:00:07 -07002962 // This class stores info for two purposes:
2963 // 1. When dragging items (mDragInfo in Workspace), we store the View, its cellX & cellY,
2964 // its spanX, spanY, and the screen it is on
2965 // 2. When long clicking on an empty cell in a CellLayout, we save information about the
2966 // cellX and cellY coordinates and which page was clicked. We then set this as a tag on
2967 // the CellLayout that was long clicked
Michael Jurkae5fb0f22011-04-11 13:27:46 -07002968 static final class CellInfo {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002969 View cell;
Michael Jurkaa63c4522010-08-19 13:52:27 -07002970 int cellX = -1;
2971 int cellY = -1;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002972 int spanX;
2973 int spanY;
2974 int screen;
Winson Chung3d503fb2011-07-13 17:25:49 -07002975 long container;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002976
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002977 @Override
2978 public String toString() {
Winson Chungaafa03c2010-06-11 17:34:16 -07002979 return "Cell[view=" + (cell == null ? "null" : cell.getClass())
2980 + ", x=" + cellX + ", y=" + cellY + "]";
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002981 }
2982 }
Michael Jurkad771c962011-08-09 15:00:48 -07002983
2984 public boolean lastDownOnOccupiedCell() {
2985 return mLastDownOnOccupiedCell;
2986 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002987}