blob: 26a3ecf3491ebc9d7f9219f83e1ed677b83d1012 [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 Cohenc6cc61d2012-04-04 12:47:08 -0700164 private DropTarget.DragEnforcer mDragEnforcer;
Adam Cohen482ed822012-03-02 14:15:13 -0800165
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800166 public CellLayout(Context context) {
167 this(context, null);
168 }
169
170 public CellLayout(Context context, AttributeSet attrs) {
171 this(context, attrs, 0);
172 }
173
174 public CellLayout(Context context, AttributeSet attrs, int defStyle) {
175 super(context, attrs, defStyle);
Adam Cohenc6cc61d2012-04-04 12:47:08 -0700176 mDragEnforcer = new DropTarget.DragEnforcer(mContext);
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700177
178 // A ViewGroup usually does not draw, but CellLayout needs to draw a rectangle to show
179 // the user where a dragged item will land when dropped.
180 setWillNotDraw(false);
Adam Cohen2acce882012-03-28 19:03:19 -0700181 mLauncher = (Launcher) context;
Michael Jurkaa63c4522010-08-19 13:52:27 -0700182
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800183 TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.CellLayout, defStyle, 0);
184
Winson Chung4b825dcd2011-06-19 12:41:22 -0700185 mOriginalCellWidth =
186 mCellWidth = a.getDimensionPixelSize(R.styleable.CellLayout_cellWidth, 10);
187 mOriginalCellHeight =
188 mCellHeight = a.getDimensionPixelSize(R.styleable.CellLayout_cellHeight, 10);
Adam Cohen234c4cd2011-07-17 21:03:04 -0700189 mWidthGap = mOriginalWidthGap = a.getDimensionPixelSize(R.styleable.CellLayout_widthGap, 0);
190 mHeightGap = mOriginalHeightGap = a.getDimensionPixelSize(R.styleable.CellLayout_heightGap, 0);
Winson Chung4b825dcd2011-06-19 12:41:22 -0700191 mMaxGap = a.getDimensionPixelSize(R.styleable.CellLayout_maxGap, 0);
Adam Cohend22015c2010-07-26 22:02:18 -0700192 mCountX = LauncherModel.getCellCountX();
193 mCountY = LauncherModel.getCellCountY();
Michael Jurka0280c3b2010-09-17 15:00:07 -0700194 mOccupied = new boolean[mCountX][mCountY];
Adam Cohen482ed822012-03-02 14:15:13 -0800195 mTmpOccupied = new boolean[mCountX][mCountY];
Adam Cohen5b53f292012-03-29 14:30:35 -0700196 mPreviousReorderDirection[0] = INVALID_DIRECTION;
197 mPreviousReorderDirection[1] = INVALID_DIRECTION;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800198
199 a.recycle();
200
201 setAlwaysDrawnWithCacheEnabled(false);
202
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700203 final Resources res = getResources();
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700204
Winson Chung967289b2011-06-30 18:09:30 -0700205 mNormalBackground = res.getDrawable(R.drawable.homescreen_blue_normal_holo);
Winson Chungdea74b72011-09-13 18:06:43 -0700206 mActiveGlowBackground = res.getDrawable(R.drawable.homescreen_blue_strong_holo);
Michael Jurka33945b22010-12-21 18:19:38 -0800207
Adam Cohenb5ba0972011-09-07 18:02:31 -0700208 mOverScrollLeft = res.getDrawable(R.drawable.overscroll_glow_left);
209 mOverScrollRight = res.getDrawable(R.drawable.overscroll_glow_right);
210 mForegroundPadding =
211 res.getDimensionPixelSize(R.dimen.workspace_overscroll_drawable_padding);
Michael Jurka33945b22010-12-21 18:19:38 -0800212
Adam Cohen19f37922012-03-21 11:59:11 -0700213 mReorderHintAnimationMagnitude = (REORDER_HINT_MAGNITUDE *
214 res.getDimensionPixelSize(R.dimen.app_icon_size));
215
Winson Chungb26f3d62011-06-02 10:49:29 -0700216 mNormalBackground.setFilterBitmap(true);
Winson Chungb26f3d62011-06-02 10:49:29 -0700217 mActiveGlowBackground.setFilterBitmap(true);
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700218
Winson Chungeecf02d2012-03-02 17:14:58 -0800219 int iconScale = res.getInteger(R.integer.app_icon_scale_percent);
220 if (iconScale >= 0) {
221 mChildScale = iconScale / 100f;
222 }
223 int hotseatIconScale = res.getInteger(R.integer.app_icon_hotseat_scale_percent);
224 if (hotseatIconScale >= 0) {
225 mHotseatChildScale = hotseatIconScale / 100f;
226 }
Andrew Flynn0dca1ec2012-02-29 13:33:22 -0800227
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700228 // Initialize the data structures used for the drag visualization.
Winson Chung150fbab2010-09-29 17:14:26 -0700229
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700230 mCrosshairsDrawable = res.getDrawable(R.drawable.gardening_crosshairs);
Patrick Dubroyce34a972010-10-19 10:34:32 -0700231 mEaseOutInterpolator = new DecelerateInterpolator(2.5f); // Quint ease out
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700232
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700233 // Set up the animation for fading the crosshairs in and out
234 int animDuration = res.getInteger(R.integer.config_crosshairsFadeInTime);
Patrick Dubroy49250ad2010-10-08 15:33:52 -0700235 mCrosshairsAnimator = new InterruptibleInOutAnimator(animDuration, 0.0f, 1.0f);
Chet Haase472b2812010-10-14 07:02:04 -0700236 mCrosshairsAnimator.getAnimator().addUpdateListener(new AnimatorUpdateListener() {
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700237 public void onAnimationUpdate(ValueAnimator animation) {
238 mCrosshairsVisibility = ((Float) animation.getAnimatedValue()).floatValue();
Patrick Dubroy8e58e912010-10-14 13:21:48 -0700239 invalidate();
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700240 }
241 });
Patrick Dubroyce34a972010-10-19 10:34:32 -0700242 mCrosshairsAnimator.getAnimator().setInterpolator(mEaseOutInterpolator);
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700243
Winson Chungb8c69f32011-10-19 21:36:08 -0700244 mDragCell[0] = mDragCell[1] = -1;
Joe Onorato4be866d2010-10-10 11:26:02 -0700245 for (int i = 0; i < mDragOutlines.length; i++) {
Adam Cohend41fbf52012-02-16 23:53:59 -0800246 mDragOutlines[i] = new Rect(-1, -1, -1, -1);
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700247 }
248
249 // When dragging things around the home screens, we show a green outline of
250 // where the item will land. The outlines gradually fade out, leaving a trail
251 // behind the drag path.
252 // Set up all the animations that are used to implement this fading.
253 final int duration = res.getInteger(R.integer.config_dragOutlineFadeTime);
Chet Haase472b2812010-10-14 07:02:04 -0700254 final float fromAlphaValue = 0;
255 final float toAlphaValue = (float)res.getInteger(R.integer.config_dragOutlineMaxAlpha);
Joe Onorato4be866d2010-10-10 11:26:02 -0700256
Patrick Dubroy8e58e912010-10-14 13:21:48 -0700257 Arrays.fill(mDragOutlineAlphas, fromAlphaValue);
Joe Onorato4be866d2010-10-10 11:26:02 -0700258
259 for (int i = 0; i < mDragOutlineAnims.length; i++) {
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700260 final InterruptibleInOutAnimator anim =
261 new InterruptibleInOutAnimator(duration, fromAlphaValue, toAlphaValue);
Patrick Dubroyce34a972010-10-19 10:34:32 -0700262 anim.getAnimator().setInterpolator(mEaseOutInterpolator);
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700263 final int thisIndex = i;
Chet Haase472b2812010-10-14 07:02:04 -0700264 anim.getAnimator().addUpdateListener(new AnimatorUpdateListener() {
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700265 public void onAnimationUpdate(ValueAnimator animation) {
Joe Onorato4be866d2010-10-10 11:26:02 -0700266 final Bitmap outline = (Bitmap)anim.getTag();
267
268 // If an animation is started and then stopped very quickly, we can still
269 // get spurious updates we've cleared the tag. Guard against this.
270 if (outline == null) {
Patrick Dubroyfe6bd872010-10-13 17:32:10 -0700271 if (false) {
272 Object val = animation.getAnimatedValue();
273 Log.d(TAG, "anim " + thisIndex + " update: " + val +
274 ", isStopped " + anim.isStopped());
275 }
Joe Onorato4be866d2010-10-10 11:26:02 -0700276 // Try to prevent it from continuing to run
277 animation.cancel();
278 } else {
Chet Haase472b2812010-10-14 07:02:04 -0700279 mDragOutlineAlphas[thisIndex] = (Float) animation.getAnimatedValue();
Adam Cohend41fbf52012-02-16 23:53:59 -0800280 CellLayout.this.invalidate(mDragOutlines[thisIndex]);
Joe Onorato4be866d2010-10-10 11:26:02 -0700281 }
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700282 }
283 });
Joe Onorato4be866d2010-10-10 11:26:02 -0700284 // The animation holds a reference to the drag outline bitmap as long is it's
285 // running. This way the bitmap can be GCed when the animations are complete.
Chet Haase472b2812010-10-14 07:02:04 -0700286 anim.getAnimator().addListener(new AnimatorListenerAdapter() {
Michael Jurka3c4c20f2010-10-28 15:36:06 -0700287 @Override
Joe Onorato4be866d2010-10-10 11:26:02 -0700288 public void onAnimationEnd(Animator animation) {
Chet Haase472b2812010-10-14 07:02:04 -0700289 if ((Float) ((ValueAnimator) animation).getAnimatedValue() == 0f) {
Joe Onorato4be866d2010-10-10 11:26:02 -0700290 anim.setTag(null);
291 }
292 }
293 });
294 mDragOutlineAnims[i] = anim;
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700295 }
Patrick Dubroyce34a972010-10-19 10:34:32 -0700296
Michael Jurka18014792010-10-14 09:01:34 -0700297 mBackgroundRect = new Rect();
Adam Cohenb5ba0972011-09-07 18:02:31 -0700298 mForegroundRect = new Rect();
Michael Jurkabea15192010-11-17 12:33:46 -0800299
Michael Jurkaa52570f2012-03-20 03:18:20 -0700300 mShortcutsAndWidgets = new ShortcutAndWidgetContainer(context);
301 mShortcutsAndWidgets.setCellDimensions(mCellWidth, mCellHeight, mWidthGap, mHeightGap);
302 addView(mShortcutsAndWidgets);
Michael Jurka18014792010-10-14 09:01:34 -0700303 }
304
Michael Jurkaf6440da2011-04-05 14:50:34 -0700305 static int widthInPortrait(Resources r, int numCells) {
306 // We use this method from Workspace to figure out how many rows/columns Launcher should
307 // have. We ignore the left/right padding on CellLayout because it turns out in our design
308 // the padding extends outside the visible screen size, but it looked fine anyway.
Michael Jurkaf6440da2011-04-05 14:50:34 -0700309 int cellWidth = r.getDimensionPixelSize(R.dimen.workspace_cell_width);
Winson Chung4b825dcd2011-06-19 12:41:22 -0700310 int minGap = Math.min(r.getDimensionPixelSize(R.dimen.workspace_width_gap),
311 r.getDimensionPixelSize(R.dimen.workspace_height_gap));
Michael Jurkaf6440da2011-04-05 14:50:34 -0700312
Winson Chung4b825dcd2011-06-19 12:41:22 -0700313 return minGap * (numCells - 1) + cellWidth * numCells;
Michael Jurkaf6440da2011-04-05 14:50:34 -0700314 }
315
Michael Jurkaf6440da2011-04-05 14:50:34 -0700316 static int heightInLandscape(Resources r, int numCells) {
317 // We use this method from Workspace to figure out how many rows/columns Launcher should
318 // have. We ignore the left/right padding on CellLayout because it turns out in our design
319 // the padding extends outside the visible screen size, but it looked fine anyway.
Michael Jurkaf6440da2011-04-05 14:50:34 -0700320 int cellHeight = r.getDimensionPixelSize(R.dimen.workspace_cell_height);
Winson Chung4b825dcd2011-06-19 12:41:22 -0700321 int minGap = Math.min(r.getDimensionPixelSize(R.dimen.workspace_width_gap),
322 r.getDimensionPixelSize(R.dimen.workspace_height_gap));
Michael Jurkaf6440da2011-04-05 14:50:34 -0700323
Winson Chung4b825dcd2011-06-19 12:41:22 -0700324 return minGap * (numCells - 1) + cellHeight * numCells;
Michael Jurkaf6440da2011-04-05 14:50:34 -0700325 }
326
Adam Cohen2801caf2011-05-13 20:57:39 -0700327 public void enableHardwareLayers() {
Michael Jurkaa52570f2012-03-20 03:18:20 -0700328 mShortcutsAndWidgets.enableHardwareLayers();
Adam Cohen2801caf2011-05-13 20:57:39 -0700329 }
330
331 public void setGridSize(int x, int y) {
332 mCountX = x;
333 mCountY = y;
334 mOccupied = new boolean[mCountX][mCountY];
Adam Cohen482ed822012-03-02 14:15:13 -0800335 mTmpOccupied = new boolean[mCountX][mCountY];
Adam Cohen7fbec102012-03-27 12:42:19 -0700336 mTempRectStack.clear();
Adam Cohen76fc0852011-06-17 13:26:23 -0700337 requestLayout();
Adam Cohen2801caf2011-05-13 20:57:39 -0700338 }
339
Patrick Dubroy96864c32011-03-10 17:17:23 -0800340 private void invalidateBubbleTextView(BubbleTextView icon) {
341 final int padding = icon.getPressedOrFocusedBackgroundPadding();
Winson Chung4b825dcd2011-06-19 12:41:22 -0700342 invalidate(icon.getLeft() + getPaddingLeft() - padding,
343 icon.getTop() + getPaddingTop() - padding,
344 icon.getRight() + getPaddingLeft() + padding,
345 icon.getBottom() + getPaddingTop() + padding);
Patrick Dubroy96864c32011-03-10 17:17:23 -0800346 }
347
Adam Cohenb5ba0972011-09-07 18:02:31 -0700348 void setOverScrollAmount(float r, boolean left) {
349 if (left && mOverScrollForegroundDrawable != mOverScrollLeft) {
350 mOverScrollForegroundDrawable = mOverScrollLeft;
351 } else if (!left && mOverScrollForegroundDrawable != mOverScrollRight) {
352 mOverScrollForegroundDrawable = mOverScrollRight;
353 }
354
355 mForegroundAlpha = (int) Math.round((r * 255));
356 mOverScrollForegroundDrawable.setAlpha(mForegroundAlpha);
357 invalidate();
358 }
359
Patrick Dubroy96864c32011-03-10 17:17:23 -0800360 void setPressedOrFocusedIcon(BubbleTextView icon) {
361 // We draw the pressed or focused BubbleTextView's background in CellLayout because it
362 // requires an expanded clip rect (due to the glow's blur radius)
363 BubbleTextView oldIcon = mPressedOrFocusedIcon;
364 mPressedOrFocusedIcon = icon;
365 if (oldIcon != null) {
366 invalidateBubbleTextView(oldIcon);
367 }
368 if (mPressedOrFocusedIcon != null) {
369 invalidateBubbleTextView(mPressedOrFocusedIcon);
370 }
371 }
372
Michael Jurka33945b22010-12-21 18:19:38 -0800373 void setIsDragOverlapping(boolean isDragOverlapping) {
374 if (mIsDragOverlapping != isDragOverlapping) {
375 mIsDragOverlapping = isDragOverlapping;
376 invalidate();
377 }
378 }
379
380 boolean getIsDragOverlapping() {
381 return mIsDragOverlapping;
382 }
383
Adam Cohenebea84d2011-11-09 17:20:41 -0800384 protected void setOverscrollTransformsDirty(boolean dirty) {
385 mScrollingTransformsDirty = dirty;
386 }
387
388 protected void resetOverscrollTransforms() {
389 if (mScrollingTransformsDirty) {
390 setOverscrollTransformsDirty(false);
391 setTranslationX(0);
392 setRotationY(0);
393 // It doesn't matter if we pass true or false here, the important thing is that we
394 // pass 0, which results in the overscroll drawable not being drawn any more.
395 setOverScrollAmount(0, false);
396 setPivotX(getMeasuredWidth() / 2);
397 setPivotY(getMeasuredHeight() / 2);
398 }
399 }
400
Jeff Sharkey83f111d2009-04-20 21:03:13 -0700401 @Override
Patrick Dubroy1262e362010-10-06 15:49:50 -0700402 protected void onDraw(Canvas canvas) {
Michael Jurka3e7c7632010-10-02 16:01:03 -0700403 // When we're large, we are either drawn in a "hover" state (ie when dragging an item to
404 // a neighboring page) or with just a normal background (if backgroundAlpha > 0.0f)
405 // When we're small, we are either drawn normally or in the "accepts drops" state (during
406 // a drag). However, we also drag the mini hover background *over* one of those two
407 // backgrounds
Winson Chungb26f3d62011-06-02 10:49:29 -0700408 if (mBackgroundAlpha > 0.0f) {
Adam Cohenf34bab52010-09-30 14:11:56 -0700409 Drawable bg;
Michael Jurka33945b22010-12-21 18:19:38 -0800410
411 if (mIsDragOverlapping) {
412 // In the mini case, we draw the active_glow bg *over* the active background
Michael Jurkabdf78552011-10-31 14:34:25 -0700413 bg = mActiveGlowBackground;
Adam Cohenf34bab52010-09-30 14:11:56 -0700414 } else {
Michael Jurkabdf78552011-10-31 14:34:25 -0700415 bg = mNormalBackground;
Adam Cohenf34bab52010-09-30 14:11:56 -0700416 }
Michael Jurka33945b22010-12-21 18:19:38 -0800417
418 bg.setAlpha((int) (mBackgroundAlpha * mBackgroundAlphaMultiplier * 255));
419 bg.setBounds(mBackgroundRect);
420 bg.draw(canvas);
Michael Jurkaa63c4522010-08-19 13:52:27 -0700421 }
Romain Guya6abce82009-11-10 02:54:41 -0800422
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700423 if (mCrosshairsVisibility > 0.0f) {
424 final int countX = mCountX;
425 final int countY = mCountY;
426
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700427 final float MAX_ALPHA = 0.4f;
428 final int MAX_VISIBLE_DISTANCE = 600;
429 final float DISTANCE_MULTIPLIER = 0.002f;
430
431 final Drawable d = mCrosshairsDrawable;
432 final int width = d.getIntrinsicWidth();
433 final int height = d.getIntrinsicHeight();
434
Winson Chung4b825dcd2011-06-19 12:41:22 -0700435 int x = getPaddingLeft() - (mWidthGap / 2) - (width / 2);
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700436 for (int col = 0; col <= countX; col++) {
Winson Chung4b825dcd2011-06-19 12:41:22 -0700437 int y = getPaddingTop() - (mHeightGap / 2) - (height / 2);
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700438 for (int row = 0; row <= countY; row++) {
439 mTmpPointF.set(x - mDragCenter.x, y - mDragCenter.y);
440 float dist = mTmpPointF.length();
441 // Crosshairs further from the drag point are more faint
442 float alpha = Math.min(MAX_ALPHA,
443 DISTANCE_MULTIPLIER * (MAX_VISIBLE_DISTANCE - dist));
444 if (alpha > 0.0f) {
445 d.setBounds(x, y, x + width, y + height);
446 d.setAlpha((int) (alpha * 255 * mCrosshairsVisibility));
447 d.draw(canvas);
448 }
449 y += mCellHeight + mHeightGap;
450 }
451 x += mCellWidth + mWidthGap;
452 }
Joe Onorato4be866d2010-10-10 11:26:02 -0700453 }
Winson Chung150fbab2010-09-29 17:14:26 -0700454
Patrick Dubroy8e58e912010-10-14 13:21:48 -0700455 final Paint paint = mDragOutlinePaint;
Joe Onorato4be866d2010-10-10 11:26:02 -0700456 for (int i = 0; i < mDragOutlines.length; i++) {
Chet Haase472b2812010-10-14 07:02:04 -0700457 final float alpha = mDragOutlineAlphas[i];
Joe Onorato4be866d2010-10-10 11:26:02 -0700458 if (alpha > 0) {
Adam Cohend41fbf52012-02-16 23:53:59 -0800459 final Rect r = mDragOutlines[i];
Joe Onorato4be866d2010-10-10 11:26:02 -0700460 final Bitmap b = (Bitmap) mDragOutlineAnims[i].getTag();
Chet Haase472b2812010-10-14 07:02:04 -0700461 paint.setAlpha((int)(alpha + .5f));
Adam Cohend41fbf52012-02-16 23:53:59 -0800462 canvas.drawBitmap(b, null, r, paint);
Winson Chung150fbab2010-09-29 17:14:26 -0700463 }
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700464 }
Patrick Dubroy96864c32011-03-10 17:17:23 -0800465
466 // We draw the pressed or focused BubbleTextView's background in CellLayout because it
467 // requires an expanded clip rect (due to the glow's blur radius)
468 if (mPressedOrFocusedIcon != null) {
469 final int padding = mPressedOrFocusedIcon.getPressedOrFocusedBackgroundPadding();
470 final Bitmap b = mPressedOrFocusedIcon.getPressedOrFocusedBackground();
471 if (b != null) {
472 canvas.drawBitmap(b,
Winson Chung4b825dcd2011-06-19 12:41:22 -0700473 mPressedOrFocusedIcon.getLeft() + getPaddingLeft() - padding,
474 mPressedOrFocusedIcon.getTop() + getPaddingTop() - padding,
Patrick Dubroy96864c32011-03-10 17:17:23 -0800475 null);
476 }
477 }
Adam Cohen69ce2e52011-07-03 19:25:21 -0700478
Adam Cohen482ed822012-03-02 14:15:13 -0800479 if (DEBUG_VISUALIZE_OCCUPIED) {
480 int[] pt = new int[2];
481 ColorDrawable cd = new ColorDrawable(Color.RED);
482 cd.setBounds(0, 0, 80, 80);
483 for (int i = 0; i < mCountX; i++) {
484 for (int j = 0; j < mCountY; j++) {
485 if (mOccupied[i][j]) {
486 cellToPoint(i, j, pt);
487 canvas.save();
488 canvas.translate(pt[0], pt[1]);
489 cd.draw(canvas);
490 canvas.restore();
491 }
492 }
493 }
494 }
495
Adam Cohen69ce2e52011-07-03 19:25:21 -0700496 // The folder outer / inner ring image(s)
497 for (int i = 0; i < mFolderOuterRings.size(); i++) {
498 FolderRingAnimator fra = mFolderOuterRings.get(i);
499
500 // Draw outer ring
501 Drawable d = FolderRingAnimator.sSharedOuterRingDrawable;
502 int width = (int) fra.getOuterRingSize();
503 int height = width;
504 cellToPoint(fra.mCellX, fra.mCellY, mTempLocation);
505
506 int centerX = mTempLocation[0] + mCellWidth / 2;
507 int centerY = mTempLocation[1] + FolderRingAnimator.sPreviewSize / 2;
508
509 canvas.save();
510 canvas.translate(centerX - width / 2, centerY - height / 2);
511 d.setBounds(0, 0, width, height);
512 d.draw(canvas);
513 canvas.restore();
514
515 // Draw inner ring
516 d = FolderRingAnimator.sSharedInnerRingDrawable;
517 width = (int) fra.getInnerRingSize();
518 height = width;
519 cellToPoint(fra.mCellX, fra.mCellY, mTempLocation);
520
521 centerX = mTempLocation[0] + mCellWidth / 2;
522 centerY = mTempLocation[1] + FolderRingAnimator.sPreviewSize / 2;
523 canvas.save();
524 canvas.translate(centerX - width / 2, centerY - width / 2);
525 d.setBounds(0, 0, width, height);
526 d.draw(canvas);
527 canvas.restore();
528 }
Adam Cohenc51934b2011-07-26 21:07:43 -0700529
530 if (mFolderLeaveBehindCell[0] >= 0 && mFolderLeaveBehindCell[1] >= 0) {
531 Drawable d = FolderIcon.sSharedFolderLeaveBehind;
532 int width = d.getIntrinsicWidth();
533 int height = d.getIntrinsicHeight();
534
535 cellToPoint(mFolderLeaveBehindCell[0], mFolderLeaveBehindCell[1], mTempLocation);
536 int centerX = mTempLocation[0] + mCellWidth / 2;
537 int centerY = mTempLocation[1] + FolderRingAnimator.sPreviewSize / 2;
538
539 canvas.save();
540 canvas.translate(centerX - width / 2, centerY - width / 2);
541 d.setBounds(0, 0, width, height);
542 d.draw(canvas);
543 canvas.restore();
544 }
Adam Cohen69ce2e52011-07-03 19:25:21 -0700545 }
546
Adam Cohenb5ba0972011-09-07 18:02:31 -0700547 @Override
548 protected void dispatchDraw(Canvas canvas) {
549 super.dispatchDraw(canvas);
550 if (mForegroundAlpha > 0) {
551 mOverScrollForegroundDrawable.setBounds(mForegroundRect);
552 Paint p = ((NinePatchDrawable) mOverScrollForegroundDrawable).getPaint();
553 p.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.ADD));
554 mOverScrollForegroundDrawable.draw(canvas);
555 p.setXfermode(null);
556 }
557 }
558
Adam Cohen69ce2e52011-07-03 19:25:21 -0700559 public void showFolderAccept(FolderRingAnimator fra) {
560 mFolderOuterRings.add(fra);
561 }
562
563 public void hideFolderAccept(FolderRingAnimator fra) {
564 if (mFolderOuterRings.contains(fra)) {
565 mFolderOuterRings.remove(fra);
566 }
567 invalidate();
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700568 }
569
Adam Cohenc51934b2011-07-26 21:07:43 -0700570 public void setFolderLeaveBehindCell(int x, int y) {
571 mFolderLeaveBehindCell[0] = x;
572 mFolderLeaveBehindCell[1] = y;
573 invalidate();
574 }
575
576 public void clearFolderLeaveBehind() {
577 mFolderLeaveBehindCell[0] = -1;
578 mFolderLeaveBehindCell[1] = -1;
579 invalidate();
580 }
581
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700582 @Override
Michael Jurkae6235dd2011-10-04 15:02:05 -0700583 public boolean shouldDelayChildPressedState() {
584 return false;
585 }
586
587 @Override
Jeff Sharkey83f111d2009-04-20 21:03:13 -0700588 public void cancelLongPress() {
589 super.cancelLongPress();
590
591 // Cancel long press for all children
592 final int count = getChildCount();
593 for (int i = 0; i < count; i++) {
594 final View child = getChildAt(i);
595 child.cancelLongPress();
596 }
597 }
598
Michael Jurkadee05892010-07-27 10:01:56 -0700599 public void setOnInterceptTouchListener(View.OnTouchListener listener) {
600 mInterceptTouchListener = listener;
601 }
602
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800603 int getCountX() {
Adam Cohend22015c2010-07-26 22:02:18 -0700604 return mCountX;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800605 }
606
607 int getCountY() {
Adam Cohend22015c2010-07-26 22:02:18 -0700608 return mCountY;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800609 }
610
Andrew Flynn0dca1ec2012-02-29 13:33:22 -0800611 public void setIsHotseat(boolean isHotseat) {
612 mIsHotseat = isHotseat;
613 }
614
Winson Chungeecf02d2012-03-02 17:14:58 -0800615 public float getChildrenScale() {
616 return mIsHotseat ? mHotseatChildScale : mChildScale;
617 }
618
Michael Jurkaf3ca3ab2010-10-20 17:08:24 -0700619 public boolean addViewToCellLayout(
620 View child, int index, int childId, LayoutParams params, boolean markCells) {
Andrew Flynn0dca1ec2012-02-29 13:33:22 -0800621 return addViewToCellLayout(child, index, childId, params, markCells, false);
622 }
623
Winson Chungeecf02d2012-03-02 17:14:58 -0800624 private void scaleChild(BubbleTextView bubbleChild, float pivot, float scale) {
Andrew Flynnbc239a12012-03-06 11:39:49 -0800625 // If we haven't measured the child yet, do it now
626 // (this happens if we're being dropped from all-apps
627 if (bubbleChild.getLayoutParams() instanceof LayoutParams &&
628 (bubbleChild.getMeasuredWidth() | bubbleChild.getMeasuredHeight()) == 0) {
Michael Jurkaa52570f2012-03-20 03:18:20 -0700629 getShortcutsAndWidgets().measureChild(bubbleChild);
Andrew Flynnbc239a12012-03-06 11:39:49 -0800630 }
Andrew Flynnbc239a12012-03-06 11:39:49 -0800631
Andrew Flynnbc239a12012-03-06 11:39:49 -0800632 bubbleChild.setScaleX(scale);
633 bubbleChild.setScaleY(scale);
Andrew Flynnbc239a12012-03-06 11:39:49 -0800634 }
635
636 private void resetChild(BubbleTextView bubbleChild) {
637 bubbleChild.setScaleX(1f);
638 bubbleChild.setScaleY(1f);
Andrew Flynnbc239a12012-03-06 11:39:49 -0800639
640 bubbleChild.setTextColor(getResources().getColor(R.color.workspace_icon_text_color));
641 }
642
Andrew Flynn0dca1ec2012-02-29 13:33:22 -0800643 public boolean addViewToCellLayout(View child, int index, int childId, LayoutParams params,
644 boolean markCells, boolean allApps) {
Winson Chungaafa03c2010-06-11 17:34:16 -0700645 final LayoutParams lp = params;
646
Andrew Flynn0dca1ec2012-02-29 13:33:22 -0800647 // Hotseat icons - scale down and remove text
648 // Don't scale the all apps button
649 // scale percent set to -1 means do not scale
650 // Only scale BubbleTextViews
651 if (child instanceof BubbleTextView) {
652 BubbleTextView bubbleChild = (BubbleTextView) child;
653
Andrew Flynnbc239a12012-03-06 11:39:49 -0800654 // Start the child with 100% scale and visible text
655 resetChild(bubbleChild);
Andrew Flynn0dca1ec2012-02-29 13:33:22 -0800656
Winson Chungeecf02d2012-03-02 17:14:58 -0800657 if (mIsHotseat && !allApps && mHotseatChildScale >= 0) {
Andrew Flynnbc239a12012-03-06 11:39:49 -0800658 // Scale/make transparent for a hotseat
Winson Chungeecf02d2012-03-02 17:14:58 -0800659 scaleChild(bubbleChild, 0f, mHotseatChildScale);
Andrew Flynn0dca1ec2012-02-29 13:33:22 -0800660
Andrew Flynnbc239a12012-03-06 11:39:49 -0800661 bubbleChild.setTextColor(getResources().getColor(android.R.color.transparent));
Winson Chungeecf02d2012-03-02 17:14:58 -0800662 } else if (mChildScale >= 0) {
Andrew Flynnbc239a12012-03-06 11:39:49 -0800663 // Else possibly still scale it if we need to for smaller icons
Winson Chungeecf02d2012-03-02 17:14:58 -0800664 scaleChild(bubbleChild, 0f, mChildScale);
Andrew Flynn0dca1ec2012-02-29 13:33:22 -0800665 }
666 }
667
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800668 // Generate an id for each view, this assumes we have at most 256x256 cells
669 // per workspace screen
Adam Cohend22015c2010-07-26 22:02:18 -0700670 if (lp.cellX >= 0 && lp.cellX <= mCountX - 1 && lp.cellY >= 0 && lp.cellY <= mCountY - 1) {
Winson Chungaafa03c2010-06-11 17:34:16 -0700671 // If the horizontal or vertical span is set to -1, it is taken to
672 // mean that it spans the extent of the CellLayout
Adam Cohend22015c2010-07-26 22:02:18 -0700673 if (lp.cellHSpan < 0) lp.cellHSpan = mCountX;
674 if (lp.cellVSpan < 0) lp.cellVSpan = mCountY;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800675
Winson Chungaafa03c2010-06-11 17:34:16 -0700676 child.setId(childId);
677
Michael Jurkaa52570f2012-03-20 03:18:20 -0700678 mShortcutsAndWidgets.addView(child, index, lp);
Michael Jurkadee05892010-07-27 10:01:56 -0700679
Michael Jurkaf3ca3ab2010-10-20 17:08:24 -0700680 if (markCells) markCellsAsOccupiedForView(child);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700681
Winson Chungaafa03c2010-06-11 17:34:16 -0700682 return true;
683 }
684 return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800685 }
Michael Jurka3e7c7632010-10-02 16:01:03 -0700686
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800687 @Override
Michael Jurka0280c3b2010-09-17 15:00:07 -0700688 public void removeAllViews() {
689 clearOccupiedCells();
Michael Jurkaa52570f2012-03-20 03:18:20 -0700690 mShortcutsAndWidgets.removeAllViews();
Michael Jurka0280c3b2010-09-17 15:00:07 -0700691 }
692
693 @Override
694 public void removeAllViewsInLayout() {
Michael Jurkaa52570f2012-03-20 03:18:20 -0700695 if (mShortcutsAndWidgets.getChildCount() > 0) {
Michael Jurka7cfc2822011-08-02 20:19:24 -0700696 clearOccupiedCells();
Michael Jurkaa52570f2012-03-20 03:18:20 -0700697 mShortcutsAndWidgets.removeAllViewsInLayout();
Michael Jurka7cfc2822011-08-02 20:19:24 -0700698 }
Michael Jurka0280c3b2010-09-17 15:00:07 -0700699 }
700
Michael Jurkaf3ca3ab2010-10-20 17:08:24 -0700701 public void removeViewWithoutMarkingCells(View view) {
Michael Jurkaa52570f2012-03-20 03:18:20 -0700702 mShortcutsAndWidgets.removeView(view);
Michael Jurkaf3ca3ab2010-10-20 17:08:24 -0700703 }
704
Michael Jurka0280c3b2010-09-17 15:00:07 -0700705 @Override
706 public void removeView(View view) {
707 markCellsAsUnoccupiedForView(view);
Michael Jurkaa52570f2012-03-20 03:18:20 -0700708 mShortcutsAndWidgets.removeView(view);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700709 }
710
711 @Override
712 public void removeViewAt(int index) {
Michael Jurkaa52570f2012-03-20 03:18:20 -0700713 markCellsAsUnoccupiedForView(mShortcutsAndWidgets.getChildAt(index));
714 mShortcutsAndWidgets.removeViewAt(index);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700715 }
716
717 @Override
718 public void removeViewInLayout(View view) {
719 markCellsAsUnoccupiedForView(view);
Michael Jurkaa52570f2012-03-20 03:18:20 -0700720 mShortcutsAndWidgets.removeViewInLayout(view);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700721 }
722
723 @Override
724 public void removeViews(int start, int count) {
725 for (int i = start; i < start + count; i++) {
Michael Jurkaa52570f2012-03-20 03:18:20 -0700726 markCellsAsUnoccupiedForView(mShortcutsAndWidgets.getChildAt(i));
Michael Jurka0280c3b2010-09-17 15:00:07 -0700727 }
Michael Jurkaa52570f2012-03-20 03:18:20 -0700728 mShortcutsAndWidgets.removeViews(start, count);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700729 }
730
731 @Override
732 public void removeViewsInLayout(int start, int count) {
733 for (int i = start; i < start + count; i++) {
Michael Jurkaa52570f2012-03-20 03:18:20 -0700734 markCellsAsUnoccupiedForView(mShortcutsAndWidgets.getChildAt(i));
Michael Jurka0280c3b2010-09-17 15:00:07 -0700735 }
Michael Jurkaa52570f2012-03-20 03:18:20 -0700736 mShortcutsAndWidgets.removeViewsInLayout(start, count);
Michael Jurkaabded662011-03-04 12:06:57 -0800737 }
738
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800739 @Override
740 protected void onAttachedToWindow() {
741 super.onAttachedToWindow();
742 mCellInfo.screen = ((ViewGroup) getParent()).indexOfChild(this);
743 }
744
Michael Jurkaaf442092010-06-10 17:01:57 -0700745 public void setTagToCellInfoForPoint(int touchX, int touchY) {
746 final CellInfo cellInfo = mCellInfo;
Winson Chungeecf02d2012-03-02 17:14:58 -0800747 Rect frame = mRect;
Michael Jurkaaf442092010-06-10 17:01:57 -0700748 final int x = touchX + mScrollX;
749 final int y = touchY + mScrollY;
Michael Jurkaa52570f2012-03-20 03:18:20 -0700750 final int count = mShortcutsAndWidgets.getChildCount();
Michael Jurkaaf442092010-06-10 17:01:57 -0700751
752 boolean found = false;
753 for (int i = count - 1; i >= 0; i--) {
Michael Jurkaa52570f2012-03-20 03:18:20 -0700754 final View child = mShortcutsAndWidgets.getChildAt(i);
Adam Cohend4844c32011-02-18 19:25:06 -0800755 final LayoutParams lp = (LayoutParams) child.getLayoutParams();
Michael Jurkaaf442092010-06-10 17:01:57 -0700756
Adam Cohen1b607ed2011-03-03 17:26:50 -0800757 if ((child.getVisibility() == VISIBLE || child.getAnimation() != null) &&
758 lp.isLockedToGrid) {
Michael Jurkaaf442092010-06-10 17:01:57 -0700759 child.getHitRect(frame);
Winson Chung0be025d2011-05-23 17:45:09 -0700760
Winson Chungeecf02d2012-03-02 17:14:58 -0800761 float scale = child.getScaleX();
762 frame = new Rect(child.getLeft(), child.getTop(), child.getRight(),
763 child.getBottom());
Winson Chung0be025d2011-05-23 17:45:09 -0700764 // The child hit rect is relative to the CellLayoutChildren parent, so we need to
765 // offset that by this CellLayout's padding to test an (x,y) point that is relative
766 // to this view.
Winson Chung4b825dcd2011-06-19 12:41:22 -0700767 frame.offset(mPaddingLeft, mPaddingTop);
Winson Chungeecf02d2012-03-02 17:14:58 -0800768 frame.inset((int) (frame.width() * (1f - scale) / 2),
769 (int) (frame.height() * (1f - scale) / 2));
Winson Chung0be025d2011-05-23 17:45:09 -0700770
Michael Jurkaaf442092010-06-10 17:01:57 -0700771 if (frame.contains(x, y)) {
Michael Jurkaaf442092010-06-10 17:01:57 -0700772 cellInfo.cell = child;
773 cellInfo.cellX = lp.cellX;
774 cellInfo.cellY = lp.cellY;
775 cellInfo.spanX = lp.cellHSpan;
776 cellInfo.spanY = lp.cellVSpan;
Michael Jurkaaf442092010-06-10 17:01:57 -0700777 found = true;
Michael Jurkaaf442092010-06-10 17:01:57 -0700778 break;
779 }
780 }
781 }
Winson Chungaafa03c2010-06-11 17:34:16 -0700782
Michael Jurkad771c962011-08-09 15:00:48 -0700783 mLastDownOnOccupiedCell = found;
784
Michael Jurkaaf442092010-06-10 17:01:57 -0700785 if (!found) {
Winson Chung0be025d2011-05-23 17:45:09 -0700786 final int cellXY[] = mTmpXY;
Michael Jurkaaf442092010-06-10 17:01:57 -0700787 pointToCellExact(x, y, cellXY);
788
Michael Jurkaaf442092010-06-10 17:01:57 -0700789 cellInfo.cell = null;
790 cellInfo.cellX = cellXY[0];
791 cellInfo.cellY = cellXY[1];
792 cellInfo.spanX = 1;
793 cellInfo.spanY = 1;
Michael Jurkaaf442092010-06-10 17:01:57 -0700794 }
795 setTag(cellInfo);
796 }
797
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800798 @Override
799 public boolean onInterceptTouchEvent(MotionEvent ev) {
Adam Cohenc1997fd2011-08-15 18:26:39 -0700800 // First we clear the tag to ensure that on every touch down we start with a fresh slate,
801 // even in the case where we return early. Not clearing here was causing bugs whereby on
802 // long-press we'd end up picking up an item from a previous drag operation.
803 final int action = ev.getAction();
804
805 if (action == MotionEvent.ACTION_DOWN) {
806 clearTagCellInfo();
807 }
808
Michael Jurkadee05892010-07-27 10:01:56 -0700809 if (mInterceptTouchListener != null && mInterceptTouchListener.onTouch(this, ev)) {
810 return true;
811 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800812
813 if (action == MotionEvent.ACTION_DOWN) {
Michael Jurkaaf442092010-06-10 17:01:57 -0700814 setTagToCellInfoForPoint((int) ev.getX(), (int) ev.getY());
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800815 }
Winson Chungeecf02d2012-03-02 17:14:58 -0800816
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800817 return false;
818 }
819
Adam Cohenc1997fd2011-08-15 18:26:39 -0700820 private void clearTagCellInfo() {
821 final CellInfo cellInfo = mCellInfo;
822 cellInfo.cell = null;
823 cellInfo.cellX = -1;
824 cellInfo.cellY = -1;
825 cellInfo.spanX = 0;
826 cellInfo.spanY = 0;
827 setTag(cellInfo);
828 }
829
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800830 public CellInfo getTag() {
Michael Jurka0280c3b2010-09-17 15:00:07 -0700831 return (CellInfo) super.getTag();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800832 }
833
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700834 /**
Winson Chungaafa03c2010-06-11 17:34:16 -0700835 * Given a point, return the cell that strictly encloses that point
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800836 * @param x X coordinate of the point
837 * @param y Y coordinate of the point
838 * @param result Array of 2 ints to hold the x and y coordinate of the cell
839 */
840 void pointToCellExact(int x, int y, int[] result) {
Winson Chung4b825dcd2011-06-19 12:41:22 -0700841 final int hStartPadding = getPaddingLeft();
842 final int vStartPadding = getPaddingTop();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800843
844 result[0] = (x - hStartPadding) / (mCellWidth + mWidthGap);
845 result[1] = (y - vStartPadding) / (mCellHeight + mHeightGap);
846
Adam Cohend22015c2010-07-26 22:02:18 -0700847 final int xAxis = mCountX;
848 final int yAxis = mCountY;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800849
850 if (result[0] < 0) result[0] = 0;
851 if (result[0] >= xAxis) result[0] = xAxis - 1;
852 if (result[1] < 0) result[1] = 0;
853 if (result[1] >= yAxis) result[1] = yAxis - 1;
854 }
Winson Chungaafa03c2010-06-11 17:34:16 -0700855
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800856 /**
857 * Given a point, return the cell that most closely encloses that point
858 * @param x X coordinate of the point
859 * @param y Y coordinate of the point
860 * @param result Array of 2 ints to hold the x and y coordinate of the cell
861 */
862 void pointToCellRounded(int x, int y, int[] result) {
863 pointToCellExact(x + (mCellWidth / 2), y + (mCellHeight / 2), result);
864 }
865
866 /**
867 * Given a cell coordinate, return the point that represents the upper left corner of that cell
Winson Chungaafa03c2010-06-11 17:34:16 -0700868 *
869 * @param cellX X coordinate of the cell
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800870 * @param cellY Y coordinate of the cell
Winson Chungaafa03c2010-06-11 17:34:16 -0700871 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800872 * @param result Array of 2 ints to hold the x and y coordinate of the point
873 */
874 void cellToPoint(int cellX, int cellY, int[] result) {
Winson Chung4b825dcd2011-06-19 12:41:22 -0700875 final int hStartPadding = getPaddingLeft();
876 final int vStartPadding = getPaddingTop();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800877
878 result[0] = hStartPadding + cellX * (mCellWidth + mWidthGap);
879 result[1] = vStartPadding + cellY * (mCellHeight + mHeightGap);
880 }
881
Adam Cohene3e27a82011-04-15 12:07:39 -0700882 /**
Adam Cohen482ed822012-03-02 14:15:13 -0800883 * Given a cell coordinate, return the point that represents the center of the cell
Adam Cohene3e27a82011-04-15 12:07:39 -0700884 *
885 * @param cellX X coordinate of the cell
886 * @param cellY Y coordinate of the cell
887 *
888 * @param result Array of 2 ints to hold the x and y coordinate of the point
889 */
890 void cellToCenterPoint(int cellX, int cellY, int[] result) {
Adam Cohen47a876d2012-03-19 13:21:41 -0700891 regionToCenterPoint(cellX, cellY, 1, 1, result);
892 }
893
894 /**
895 * Given a cell coordinate and span return the point that represents the center of the regio
896 *
897 * @param cellX X coordinate of the cell
898 * @param cellY Y coordinate of the cell
899 *
900 * @param result Array of 2 ints to hold the x and y coordinate of the point
901 */
902 void regionToCenterPoint(int cellX, int cellY, int spanX, int spanY, int[] result) {
Winson Chung4b825dcd2011-06-19 12:41:22 -0700903 final int hStartPadding = getPaddingLeft();
904 final int vStartPadding = getPaddingTop();
Adam Cohen47a876d2012-03-19 13:21:41 -0700905 result[0] = hStartPadding + cellX * (mCellWidth + mWidthGap) +
906 (spanX * mCellWidth + (spanX - 1) * mWidthGap) / 2;
907 result[1] = vStartPadding + cellY * (mCellHeight + mHeightGap) +
908 (spanY * mCellHeight + (spanY - 1) * mHeightGap) / 2;
Adam Cohene3e27a82011-04-15 12:07:39 -0700909 }
910
Adam Cohen19f37922012-03-21 11:59:11 -0700911 /**
912 * Given a cell coordinate and span fills out a corresponding pixel rect
913 *
914 * @param cellX X coordinate of the cell
915 * @param cellY Y coordinate of the cell
916 * @param result Rect in which to write the result
917 */
918 void regionToRect(int cellX, int cellY, int spanX, int spanY, Rect result) {
919 final int hStartPadding = getPaddingLeft();
920 final int vStartPadding = getPaddingTop();
921 final int left = hStartPadding + cellX * (mCellWidth + mWidthGap);
922 final int top = vStartPadding + cellY * (mCellHeight + mHeightGap);
923 result.set(left, top, left + (spanX * mCellWidth + (spanX - 1) * mWidthGap),
924 top + (spanY * mCellHeight + (spanY - 1) * mHeightGap));
925 }
926
Adam Cohen482ed822012-03-02 14:15:13 -0800927 public float getDistanceFromCell(float x, float y, int[] cell) {
928 cellToCenterPoint(cell[0], cell[1], mTmpPoint);
929 float distance = (float) Math.sqrt( Math.pow(x - mTmpPoint[0], 2) +
930 Math.pow(y - mTmpPoint[1], 2));
931 return distance;
932 }
933
Romain Guy84f296c2009-11-04 15:00:44 -0800934 int getCellWidth() {
935 return mCellWidth;
936 }
937
938 int getCellHeight() {
939 return mCellHeight;
940 }
941
Adam Cohend4844c32011-02-18 19:25:06 -0800942 int getWidthGap() {
943 return mWidthGap;
944 }
945
946 int getHeightGap() {
947 return mHeightGap;
948 }
949
Adam Cohen7f4eabe2011-04-21 16:19:16 -0700950 Rect getContentRect(Rect r) {
951 if (r == null) {
952 r = new Rect();
953 }
954 int left = getPaddingLeft();
955 int top = getPaddingTop();
Winson Chung4b825dcd2011-06-19 12:41:22 -0700956 int right = left + getWidth() - mPaddingLeft - mPaddingRight;
957 int bottom = top + getHeight() - mPaddingTop - mPaddingBottom;
Adam Cohen7f4eabe2011-04-21 16:19:16 -0700958 r.set(left, top, right, bottom);
959 return r;
960 }
961
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800962 @Override
963 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
964 // TODO: currently ignoring padding
Winson Chungaafa03c2010-06-11 17:34:16 -0700965
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800966 int widthSpecMode = MeasureSpec.getMode(widthMeasureSpec);
Winson Chungaafa03c2010-06-11 17:34:16 -0700967 int widthSpecSize = MeasureSpec.getSize(widthMeasureSpec);
968
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800969 int heightSpecMode = MeasureSpec.getMode(heightMeasureSpec);
970 int heightSpecSize = MeasureSpec.getSize(heightMeasureSpec);
Winson Chungaafa03c2010-06-11 17:34:16 -0700971
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800972 if (widthSpecMode == MeasureSpec.UNSPECIFIED || heightSpecMode == MeasureSpec.UNSPECIFIED) {
973 throw new RuntimeException("CellLayout cannot have UNSPECIFIED dimensions");
974 }
975
Adam Cohend22015c2010-07-26 22:02:18 -0700976 int numWidthGaps = mCountX - 1;
977 int numHeightGaps = mCountY - 1;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800978
Adam Cohen234c4cd2011-07-17 21:03:04 -0700979 if (mOriginalWidthGap < 0 || mOriginalHeightGap < 0) {
Winson Chung4b825dcd2011-06-19 12:41:22 -0700980 int hSpace = widthSpecSize - mPaddingLeft - mPaddingRight;
981 int vSpace = heightSpecSize - mPaddingTop - mPaddingBottom;
982 int hFreeSpace = hSpace - (mCountX * mOriginalCellWidth);
983 int vFreeSpace = vSpace - (mCountY * mOriginalCellHeight);
984 mWidthGap = Math.min(mMaxGap, numWidthGaps > 0 ? (hFreeSpace / numWidthGaps) : 0);
985 mHeightGap = Math.min(mMaxGap,numHeightGaps > 0 ? (vFreeSpace / numHeightGaps) : 0);
Michael Jurkaa52570f2012-03-20 03:18:20 -0700986 mShortcutsAndWidgets.setCellDimensions(mCellWidth, mCellHeight, mWidthGap, mHeightGap);
Adam Cohen234c4cd2011-07-17 21:03:04 -0700987 } else {
988 mWidthGap = mOriginalWidthGap;
989 mHeightGap = mOriginalHeightGap;
Winson Chungece7f5b2010-10-22 14:54:12 -0700990 }
Michael Jurka5f1c5092010-09-03 14:15:02 -0700991
Michael Jurka8c920dd2011-01-20 14:16:56 -0800992 // Initial values correspond to widthSpecMode == MeasureSpec.EXACTLY
993 int newWidth = widthSpecSize;
994 int newHeight = heightSpecSize;
Michael Jurka5f1c5092010-09-03 14:15:02 -0700995 if (widthSpecMode == MeasureSpec.AT_MOST) {
Winson Chung4b825dcd2011-06-19 12:41:22 -0700996 newWidth = mPaddingLeft + mPaddingRight + (mCountX * mCellWidth) +
Winson Chungece7f5b2010-10-22 14:54:12 -0700997 ((mCountX - 1) * mWidthGap);
Winson Chung4b825dcd2011-06-19 12:41:22 -0700998 newHeight = mPaddingTop + mPaddingBottom + (mCountY * mCellHeight) +
Winson Chungece7f5b2010-10-22 14:54:12 -0700999 ((mCountY - 1) * mHeightGap);
Michael Jurka5f1c5092010-09-03 14:15:02 -07001000 setMeasuredDimension(newWidth, newHeight);
Michael Jurka5f1c5092010-09-03 14:15:02 -07001001 }
Michael Jurka8c920dd2011-01-20 14:16:56 -08001002
1003 int count = getChildCount();
1004 for (int i = 0; i < count; i++) {
1005 View child = getChildAt(i);
Winson Chung4b825dcd2011-06-19 12:41:22 -07001006 int childWidthMeasureSpec = MeasureSpec.makeMeasureSpec(newWidth - mPaddingLeft -
1007 mPaddingRight, MeasureSpec.EXACTLY);
1008 int childheightMeasureSpec = MeasureSpec.makeMeasureSpec(newHeight - mPaddingTop -
1009 mPaddingBottom, MeasureSpec.EXACTLY);
Michael Jurka8c920dd2011-01-20 14:16:56 -08001010 child.measure(childWidthMeasureSpec, childheightMeasureSpec);
1011 }
1012 setMeasuredDimension(newWidth, newHeight);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001013 }
1014
1015 @Override
Michael Jurka28750fb2010-09-24 17:43:49 -07001016 protected void onLayout(boolean changed, int l, int t, int r, int b) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001017 int count = getChildCount();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001018 for (int i = 0; i < count; i++) {
Michael Jurka8c920dd2011-01-20 14:16:56 -08001019 View child = getChildAt(i);
Winson Chung4b825dcd2011-06-19 12:41:22 -07001020 child.layout(mPaddingLeft, mPaddingTop,
1021 r - l - mPaddingRight, b - t - mPaddingBottom);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001022 }
1023 }
1024
1025 @Override
Michael Jurkadee05892010-07-27 10:01:56 -07001026 protected void onSizeChanged(int w, int h, int oldw, int oldh) {
1027 super.onSizeChanged(w, h, oldw, oldh);
Michael Jurka18014792010-10-14 09:01:34 -07001028 mBackgroundRect.set(0, 0, w, h);
Adam Cohenb5ba0972011-09-07 18:02:31 -07001029 mForegroundRect.set(mForegroundPadding, mForegroundPadding,
1030 w - 2 * mForegroundPadding, h - 2 * mForegroundPadding);
Michael Jurkadee05892010-07-27 10:01:56 -07001031 }
1032
1033 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001034 protected void setChildrenDrawingCacheEnabled(boolean enabled) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07001035 mShortcutsAndWidgets.setChildrenDrawingCacheEnabled(enabled);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001036 }
1037
1038 @Override
1039 protected void setChildrenDrawnWithCacheEnabled(boolean enabled) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07001040 mShortcutsAndWidgets.setChildrenDrawnWithCacheEnabled(enabled);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001041 }
1042
Michael Jurka5f1c5092010-09-03 14:15:02 -07001043 public float getBackgroundAlpha() {
1044 return mBackgroundAlpha;
Michael Jurkadee05892010-07-27 10:01:56 -07001045 }
1046
Adam Cohen1b0aaac2010-10-28 11:11:18 -07001047 public void setBackgroundAlphaMultiplier(float multiplier) {
1048 mBackgroundAlphaMultiplier = multiplier;
1049 }
1050
Adam Cohenddb82192010-11-10 16:32:54 -08001051 public float getBackgroundAlphaMultiplier() {
1052 return mBackgroundAlphaMultiplier;
1053 }
1054
Michael Jurka5f1c5092010-09-03 14:15:02 -07001055 public void setBackgroundAlpha(float alpha) {
Michael Jurkaafaa0502011-12-13 18:22:50 -08001056 if (mBackgroundAlpha != alpha) {
1057 mBackgroundAlpha = alpha;
1058 invalidate();
1059 }
Michael Jurkadee05892010-07-27 10:01:56 -07001060 }
1061
Michael Jurkaa52570f2012-03-20 03:18:20 -07001062 public void setShortcutAndWidgetAlpha(float alpha) {
Michael Jurka0142d492010-08-25 17:46:15 -07001063 final int childCount = getChildCount();
1064 for (int i = 0; i < childCount; i++) {
Michael Jurkadee05892010-07-27 10:01:56 -07001065 getChildAt(i).setAlpha(alpha);
1066 }
1067 }
1068
Michael Jurkaa52570f2012-03-20 03:18:20 -07001069 public ShortcutAndWidgetContainer getShortcutsAndWidgets() {
1070 if (getChildCount() > 0) {
1071 return (ShortcutAndWidgetContainer) getChildAt(0);
1072 }
1073 return null;
1074 }
1075
Patrick Dubroy440c3602010-07-13 17:50:32 -07001076 public View getChildAt(int x, int y) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07001077 return mShortcutsAndWidgets.getChildAt(x, y);
Patrick Dubroy440c3602010-07-13 17:50:32 -07001078 }
1079
Adam Cohen76fc0852011-06-17 13:26:23 -07001080 public boolean animateChildToPosition(final View child, int cellX, int cellY, int duration,
Adam Cohen482ed822012-03-02 14:15:13 -08001081 int delay, boolean permanent, boolean adjustOccupied) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07001082 ShortcutAndWidgetContainer clc = getShortcutsAndWidgets();
Adam Cohen482ed822012-03-02 14:15:13 -08001083 boolean[][] occupied = mOccupied;
1084 if (!permanent) {
1085 occupied = mTmpOccupied;
1086 }
1087
Adam Cohen19f37922012-03-21 11:59:11 -07001088 if (clc.indexOfChild(child) != -1) {
Adam Cohenbfbfd262011-06-13 16:55:12 -07001089 final LayoutParams lp = (LayoutParams) child.getLayoutParams();
1090 final ItemInfo info = (ItemInfo) child.getTag();
1091
1092 // We cancel any existing animations
1093 if (mReorderAnimators.containsKey(lp)) {
1094 mReorderAnimators.get(lp).cancel();
1095 mReorderAnimators.remove(lp);
1096 }
1097
Adam Cohen482ed822012-03-02 14:15:13 -08001098 final int oldX = lp.x;
1099 final int oldY = lp.y;
1100 if (adjustOccupied) {
1101 occupied[lp.cellX][lp.cellY] = false;
1102 occupied[cellX][cellY] = true;
1103 }
Adam Cohenbfbfd262011-06-13 16:55:12 -07001104 lp.isLockedToGrid = true;
Adam Cohen482ed822012-03-02 14:15:13 -08001105 if (permanent) {
1106 lp.cellX = info.cellX = cellX;
1107 lp.cellY = info.cellY = cellY;
1108 } else {
1109 lp.tmpCellX = cellX;
1110 lp.tmpCellY = cellY;
1111 }
Adam Cohenbfbfd262011-06-13 16:55:12 -07001112 clc.setupLp(lp);
1113 lp.isLockedToGrid = false;
Adam Cohen482ed822012-03-02 14:15:13 -08001114 final int newX = lp.x;
1115 final int newY = lp.y;
Adam Cohenbfbfd262011-06-13 16:55:12 -07001116
Adam Cohen76fc0852011-06-17 13:26:23 -07001117 lp.x = oldX;
1118 lp.y = oldY;
Adam Cohen76fc0852011-06-17 13:26:23 -07001119
Adam Cohen482ed822012-03-02 14:15:13 -08001120 // Exit early if we're not actually moving the view
1121 if (oldX == newX && oldY == newY) {
1122 lp.isLockedToGrid = true;
1123 return true;
1124 }
1125
1126 ValueAnimator va = ValueAnimator.ofFloat(0f, 1f);
1127 va.setDuration(duration);
1128 mReorderAnimators.put(lp, va);
1129
1130 va.addUpdateListener(new AnimatorUpdateListener() {
1131 @Override
Adam Cohenbfbfd262011-06-13 16:55:12 -07001132 public void onAnimationUpdate(ValueAnimator animation) {
Adam Cohen482ed822012-03-02 14:15:13 -08001133 float r = ((Float) animation.getAnimatedValue()).floatValue();
Adam Cohen19f37922012-03-21 11:59:11 -07001134 lp.x = (int) ((1 - r) * oldX + r * newX);
1135 lp.y = (int) ((1 - r) * oldY + r * newY);
Adam Cohen6b8a02d2012-03-22 15:13:40 -07001136 child.requestLayout();
Adam Cohenbfbfd262011-06-13 16:55:12 -07001137 }
1138 });
Adam Cohen482ed822012-03-02 14:15:13 -08001139 va.addListener(new AnimatorListenerAdapter() {
Adam Cohenbfbfd262011-06-13 16:55:12 -07001140 boolean cancelled = false;
1141 public void onAnimationEnd(Animator animation) {
1142 // If the animation was cancelled, it means that another animation
1143 // has interrupted this one, and we don't want to lock the item into
1144 // place just yet.
1145 if (!cancelled) {
1146 lp.isLockedToGrid = true;
Adam Cohen482ed822012-03-02 14:15:13 -08001147 child.requestLayout();
Adam Cohenbfbfd262011-06-13 16:55:12 -07001148 }
1149 if (mReorderAnimators.containsKey(lp)) {
1150 mReorderAnimators.remove(lp);
1151 }
1152 }
1153 public void onAnimationCancel(Animator animation) {
1154 cancelled = true;
1155 }
1156 });
Adam Cohen482ed822012-03-02 14:15:13 -08001157 va.setStartDelay(delay);
1158 va.start();
Adam Cohenbfbfd262011-06-13 16:55:12 -07001159 return true;
1160 }
1161 return false;
1162 }
1163
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001164 /**
1165 * Estimate where the top left cell of the dragged item will land if it is dropped.
1166 *
1167 * @param originX The X value of the top left corner of the item
1168 * @param originY The Y value of the top left corner of the item
1169 * @param spanX The number of horizontal cells that the item spans
1170 * @param spanY The number of vertical cells that the item spans
1171 * @param result The estimated drop cell X and Y.
1172 */
1173 void estimateDropCell(int originX, int originY, int spanX, int spanY, int[] result) {
Adam Cohend22015c2010-07-26 22:02:18 -07001174 final int countX = mCountX;
1175 final int countY = mCountY;
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001176
Michael Jurkaa63c4522010-08-19 13:52:27 -07001177 // pointToCellRounded takes the top left of a cell but will pad that with
1178 // cellWidth/2 and cellHeight/2 when finding the matching cell
1179 pointToCellRounded(originX, originY, result);
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001180
1181 // If the item isn't fully on this screen, snap to the edges
1182 int rightOverhang = result[0] + spanX - countX;
1183 if (rightOverhang > 0) {
1184 result[0] -= rightOverhang; // Snap to right
1185 }
1186 result[0] = Math.max(0, result[0]); // Snap to left
1187 int bottomOverhang = result[1] + spanY - countY;
1188 if (bottomOverhang > 0) {
1189 result[1] -= bottomOverhang; // Snap to bottom
1190 }
1191 result[1] = Math.max(0, result[1]); // Snap to top
1192 }
1193
Adam Cohen482ed822012-03-02 14:15:13 -08001194 void visualizeDropLocation(View v, Bitmap dragOutline, int originX, int originY, int cellX,
1195 int cellY, int spanX, int spanY, boolean resize, Point dragOffset, Rect dragRegion) {
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -07001196 final int oldDragCellX = mDragCell[0];
1197 final int oldDragCellY = mDragCell[1];
Adam Cohen482ed822012-03-02 14:15:13 -08001198
Winson Chungb8c69f32011-10-19 21:36:08 -07001199 if (v != null && dragOffset == null) {
Winson Chunga9abd0e2010-10-27 17:18:37 -07001200 mDragCenter.set(originX + (v.getWidth() / 2), originY + (v.getHeight() / 2));
1201 } else {
1202 mDragCenter.set(originX, originY);
1203 }
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001204
Adam Cohen2801caf2011-05-13 20:57:39 -07001205 if (dragOutline == null && v == null) {
1206 if (mCrosshairsDrawable != null) {
1207 invalidate();
1208 }
1209 return;
1210 }
1211
Adam Cohen482ed822012-03-02 14:15:13 -08001212 if (cellX != oldDragCellX || cellY != oldDragCellY) {
1213 mDragCell[0] = cellX;
1214 mDragCell[1] = cellY;
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001215 // Find the top left corner of the rect the object will occupy
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001216 final int[] topLeft = mTmpPoint;
Adam Cohen482ed822012-03-02 14:15:13 -08001217 cellToPoint(cellX, cellY, topLeft);
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001218
Joe Onorato4be866d2010-10-10 11:26:02 -07001219 int left = topLeft[0];
1220 int top = topLeft[1];
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001221
Winson Chungb8c69f32011-10-19 21:36:08 -07001222 if (v != null && dragOffset == null) {
Adam Cohen99e8b402011-03-25 19:23:43 -07001223 // When drawing the drag outline, it did not account for margin offsets
1224 // added by the view's parent.
1225 MarginLayoutParams lp = (MarginLayoutParams) v.getLayoutParams();
1226 left += lp.leftMargin;
1227 top += lp.topMargin;
Winson Chung150fbab2010-09-29 17:14:26 -07001228
Adam Cohen99e8b402011-03-25 19:23:43 -07001229 // Offsets due to the size difference between the View and the dragOutline.
1230 // There is a size difference to account for the outer blur, which may lie
1231 // outside the bounds of the view.
Winson Chunga9abd0e2010-10-27 17:18:37 -07001232 top += (v.getHeight() - dragOutline.getHeight()) / 2;
Adam Cohenae915ce2011-08-25 13:47:22 -07001233 // We center about the x axis
1234 left += ((mCellWidth * spanX) + ((spanX - 1) * mWidthGap)
1235 - dragOutline.getWidth()) / 2;
Adam Cohen66396872011-04-15 17:50:36 -07001236 } else {
Winson Chungb8c69f32011-10-19 21:36:08 -07001237 if (dragOffset != null && dragRegion != null) {
1238 // Center the drag region *horizontally* in the cell and apply a drag
1239 // outline offset
1240 left += dragOffset.x + ((mCellWidth * spanX) + ((spanX - 1) * mWidthGap)
1241 - dragRegion.width()) / 2;
1242 top += dragOffset.y;
1243 } else {
1244 // Center the drag outline in the cell
1245 left += ((mCellWidth * spanX) + ((spanX - 1) * mWidthGap)
1246 - dragOutline.getWidth()) / 2;
1247 top += ((mCellHeight * spanY) + ((spanY - 1) * mHeightGap)
1248 - dragOutline.getHeight()) / 2;
1249 }
Winson Chunga9abd0e2010-10-27 17:18:37 -07001250 }
Joe Onorato4be866d2010-10-10 11:26:02 -07001251 final int oldIndex = mDragOutlineCurrent;
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -07001252 mDragOutlineAnims[oldIndex].animateOut();
1253 mDragOutlineCurrent = (oldIndex + 1) % mDragOutlines.length;
Adam Cohend41fbf52012-02-16 23:53:59 -08001254 Rect r = mDragOutlines[mDragOutlineCurrent];
1255 r.set(left, top, left + dragOutline.getWidth(), top + dragOutline.getHeight());
1256 if (resize) {
Adam Cohen482ed822012-03-02 14:15:13 -08001257 cellToRect(cellX, cellY, spanX, spanY, r);
Adam Cohend41fbf52012-02-16 23:53:59 -08001258 }
Winson Chung150fbab2010-09-29 17:14:26 -07001259
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -07001260 mDragOutlineAnims[mDragOutlineCurrent].setTag(dragOutline);
1261 mDragOutlineAnims[mDragOutlineCurrent].animateIn();
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001262 }
Patrick Dubroy49250ad2010-10-08 15:33:52 -07001263
1264 // If we are drawing crosshairs, the entire CellLayout needs to be invalidated
1265 if (mCrosshairsDrawable != null) {
1266 invalidate();
1267 }
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001268 }
1269
Adam Cohene0310962011-04-18 16:15:31 -07001270 public void clearDragOutlines() {
1271 final int oldIndex = mDragOutlineCurrent;
1272 mDragOutlineAnims[oldIndex].animateOut();
Adam Cohend41fbf52012-02-16 23:53:59 -08001273 mDragCell[0] = mDragCell[1] = -1;
Adam Cohene0310962011-04-18 16:15:31 -07001274 }
1275
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001276 /**
Jeff Sharkey70864282009-04-07 21:08:40 -07001277 * Find a vacant area that will fit the given bounds nearest the requested
1278 * cell location. Uses Euclidean distance to score multiple vacant areas.
Winson Chungaafa03c2010-06-11 17:34:16 -07001279 *
Romain Guy51afc022009-05-04 18:03:43 -07001280 * @param pixelX The X location at which you want to search for a vacant area.
1281 * @param pixelY The Y location at which you want to search for a vacant area.
Jeff Sharkey70864282009-04-07 21:08:40 -07001282 * @param spanX Horizontal span of the object.
1283 * @param spanY Vertical span of the object.
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001284 * @param result Array in which to place the result, or null (in which case a new array will
1285 * be allocated)
Jeff Sharkey70864282009-04-07 21:08:40 -07001286 * @return The X, Y cell of a vacant area that can contain this object,
1287 * nearest the requested location.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001288 */
Adam Cohend41fbf52012-02-16 23:53:59 -08001289 int[] findNearestVacantArea(int pixelX, int pixelY, int spanX, int spanY,
1290 int[] result) {
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001291 return findNearestVacantArea(pixelX, pixelY, spanX, spanY, null, result);
Michael Jurka6a1435d2010-09-27 17:35:12 -07001292 }
Winson Chungaafa03c2010-06-11 17:34:16 -07001293
Michael Jurka6a1435d2010-09-27 17:35:12 -07001294 /**
1295 * Find a vacant area that will fit the given bounds nearest the requested
1296 * cell location. Uses Euclidean distance to score multiple vacant areas.
1297 *
1298 * @param pixelX The X location at which you want to search for a vacant area.
1299 * @param pixelY The Y location at which you want to search for a vacant area.
Adam Cohend41fbf52012-02-16 23:53:59 -08001300 * @param minSpanX The minimum horizontal span required
1301 * @param minSpanY The minimum vertical span required
1302 * @param spanX Horizontal span of the object.
1303 * @param spanY Vertical span of the object.
1304 * @param result Array in which to place the result, or null (in which case a new array will
1305 * be allocated)
1306 * @return The X, Y cell of a vacant area that can contain this object,
1307 * nearest the requested location.
1308 */
1309 int[] findNearestVacantArea(int pixelX, int pixelY, int minSpanX, int minSpanY, int spanX,
1310 int spanY, int[] result, int[] resultSpan) {
1311 return findNearestVacantArea(pixelX, pixelY, minSpanX, minSpanY, spanX, spanY, null,
1312 result, resultSpan);
1313 }
1314
1315 /**
1316 * Find a vacant area that will fit the given bounds nearest the requested
1317 * cell location. Uses Euclidean distance to score multiple vacant areas.
1318 *
1319 * @param pixelX The X location at which you want to search for a vacant area.
1320 * @param pixelY The Y location at which you want to search for a vacant area.
Michael Jurka6a1435d2010-09-27 17:35:12 -07001321 * @param spanX Horizontal span of the object.
1322 * @param spanY Vertical span of the object.
Adam Cohendf035382011-04-11 17:22:04 -07001323 * @param ignoreOccupied If true, the result can be an occupied cell
1324 * @param result Array in which to place the result, or null (in which case a new array will
1325 * be allocated)
Michael Jurka6a1435d2010-09-27 17:35:12 -07001326 * @return The X, Y cell of a vacant area that can contain this object,
1327 * nearest the requested location.
1328 */
Adam Cohendf035382011-04-11 17:22:04 -07001329 int[] findNearestArea(int pixelX, int pixelY, int spanX, int spanY, View ignoreView,
1330 boolean ignoreOccupied, int[] result) {
Adam Cohend41fbf52012-02-16 23:53:59 -08001331 return findNearestArea(pixelX, pixelY, spanX, spanY,
Adam Cohen482ed822012-03-02 14:15:13 -08001332 spanX, spanY, ignoreView, ignoreOccupied, result, null, mOccupied);
Adam Cohend41fbf52012-02-16 23:53:59 -08001333 }
1334
1335 private final Stack<Rect> mTempRectStack = new Stack<Rect>();
1336 private void lazyInitTempRectStack() {
1337 if (mTempRectStack.isEmpty()) {
1338 for (int i = 0; i < mCountX * mCountY; i++) {
1339 mTempRectStack.push(new Rect());
1340 }
1341 }
1342 }
Adam Cohen482ed822012-03-02 14:15:13 -08001343
Adam Cohend41fbf52012-02-16 23:53:59 -08001344 private void recycleTempRects(Stack<Rect> used) {
1345 while (!used.isEmpty()) {
1346 mTempRectStack.push(used.pop());
1347 }
1348 }
1349
1350 /**
1351 * Find a vacant area that will fit the given bounds nearest the requested
1352 * cell location. Uses Euclidean distance to score multiple vacant areas.
1353 *
1354 * @param pixelX The X location at which you want to search for a vacant area.
1355 * @param pixelY The Y location at which you want to search for a vacant area.
1356 * @param minSpanX The minimum horizontal span required
1357 * @param minSpanY The minimum vertical span required
1358 * @param spanX Horizontal span of the object.
1359 * @param spanY Vertical span of the object.
1360 * @param ignoreOccupied If true, the result can be an occupied cell
1361 * @param result Array in which to place the result, or null (in which case a new array will
1362 * be allocated)
1363 * @return The X, Y cell of a vacant area that can contain this object,
1364 * nearest the requested location.
1365 */
1366 int[] findNearestArea(int pixelX, int pixelY, int minSpanX, int minSpanY, int spanX, int spanY,
Adam Cohen482ed822012-03-02 14:15:13 -08001367 View ignoreView, boolean ignoreOccupied, int[] result, int[] resultSpan,
1368 boolean[][] occupied) {
Adam Cohend41fbf52012-02-16 23:53:59 -08001369 lazyInitTempRectStack();
Michael Jurkac6ee42e2010-09-30 12:04:50 -07001370 // mark space take by ignoreView as available (method checks if ignoreView is null)
Adam Cohen482ed822012-03-02 14:15:13 -08001371 markCellsAsUnoccupiedForView(ignoreView, occupied);
Michael Jurkac6ee42e2010-09-30 12:04:50 -07001372
Adam Cohene3e27a82011-04-15 12:07:39 -07001373 // For items with a spanX / spanY > 1, the passed in point (pixelX, pixelY) corresponds
1374 // to the center of the item, but we are searching based on the top-left cell, so
1375 // we translate the point over to correspond to the top-left.
1376 pixelX -= (mCellWidth + mWidthGap) * (spanX - 1) / 2f;
1377 pixelY -= (mCellHeight + mHeightGap) * (spanY - 1) / 2f;
1378
Jeff Sharkey70864282009-04-07 21:08:40 -07001379 // Keep track of best-scoring drop area
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001380 final int[] bestXY = result != null ? result : new int[2];
Jeff Sharkey70864282009-04-07 21:08:40 -07001381 double bestDistance = Double.MAX_VALUE;
Adam Cohend41fbf52012-02-16 23:53:59 -08001382 final Rect bestRect = new Rect(-1, -1, -1, -1);
1383 final Stack<Rect> validRegions = new Stack<Rect>();
Winson Chungaafa03c2010-06-11 17:34:16 -07001384
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001385 final int countX = mCountX;
1386 final int countY = mCountY;
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001387
Adam Cohend41fbf52012-02-16 23:53:59 -08001388 if (minSpanX <= 0 || minSpanY <= 0 || spanX <= 0 || spanY <= 0 ||
1389 spanX < minSpanX || spanY < minSpanY) {
1390 return bestXY;
1391 }
1392
1393 for (int y = 0; y < countY - (minSpanY - 1); y++) {
Michael Jurkac28de512010-08-13 11:27:44 -07001394 inner:
Adam Cohend41fbf52012-02-16 23:53:59 -08001395 for (int x = 0; x < countX - (minSpanX - 1); x++) {
1396 int ySize = -1;
1397 int xSize = -1;
Adam Cohendf035382011-04-11 17:22:04 -07001398 if (ignoreOccupied) {
Adam Cohend41fbf52012-02-16 23:53:59 -08001399 // First, let's see if this thing fits anywhere
1400 for (int i = 0; i < minSpanX; i++) {
1401 for (int j = 0; j < minSpanY; j++) {
Adam Cohendf035382011-04-11 17:22:04 -07001402 if (occupied[x + i][y + j]) {
Adam Cohendf035382011-04-11 17:22:04 -07001403 continue inner;
1404 }
Michael Jurkac28de512010-08-13 11:27:44 -07001405 }
1406 }
Adam Cohend41fbf52012-02-16 23:53:59 -08001407 xSize = minSpanX;
1408 ySize = minSpanY;
1409
1410 // We know that the item will fit at _some_ acceptable size, now let's see
1411 // how big we can make it. We'll alternate between incrementing x and y spans
1412 // until we hit a limit.
1413 boolean incX = true;
1414 boolean hitMaxX = xSize >= spanX;
1415 boolean hitMaxY = ySize >= spanY;
1416 while (!(hitMaxX && hitMaxY)) {
1417 if (incX && !hitMaxX) {
1418 for (int j = 0; j < ySize; j++) {
1419 if (x + xSize > countX -1 || occupied[x + xSize][y + j]) {
1420 // We can't move out horizontally
1421 hitMaxX = true;
1422 }
1423 }
1424 if (!hitMaxX) {
1425 xSize++;
1426 }
1427 } else if (!hitMaxY) {
1428 for (int i = 0; i < xSize; i++) {
1429 if (y + ySize > countY - 1 || occupied[x + i][y + ySize]) {
1430 // We can't move out vertically
1431 hitMaxY = true;
1432 }
1433 }
1434 if (!hitMaxY) {
1435 ySize++;
1436 }
1437 }
1438 hitMaxX |= xSize >= spanX;
1439 hitMaxY |= ySize >= spanY;
1440 incX = !incX;
1441 }
1442 incX = true;
1443 hitMaxX = xSize >= spanX;
1444 hitMaxY = ySize >= spanY;
Michael Jurkac28de512010-08-13 11:27:44 -07001445 }
Winson Chung0be025d2011-05-23 17:45:09 -07001446 final int[] cellXY = mTmpXY;
Adam Cohene3e27a82011-04-15 12:07:39 -07001447 cellToCenterPoint(x, y, cellXY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001448
Adam Cohend41fbf52012-02-16 23:53:59 -08001449 // We verify that the current rect is not a sub-rect of any of our previous
1450 // candidates. In this case, the current rect is disqualified in favour of the
1451 // containing rect.
1452 Rect currentRect = mTempRectStack.pop();
1453 currentRect.set(x, y, x + xSize, y + ySize);
1454 boolean contained = false;
1455 for (Rect r : validRegions) {
1456 if (r.contains(currentRect)) {
1457 contained = true;
1458 break;
1459 }
1460 }
1461 validRegions.push(currentRect);
Michael Jurkac28de512010-08-13 11:27:44 -07001462 double distance = Math.sqrt(Math.pow(cellXY[0] - pixelX, 2)
1463 + Math.pow(cellXY[1] - pixelY, 2));
Adam Cohen482ed822012-03-02 14:15:13 -08001464
Adam Cohend41fbf52012-02-16 23:53:59 -08001465 if ((distance <= bestDistance && !contained) ||
1466 currentRect.contains(bestRect)) {
Michael Jurkac28de512010-08-13 11:27:44 -07001467 bestDistance = distance;
1468 bestXY[0] = x;
1469 bestXY[1] = y;
Adam Cohend41fbf52012-02-16 23:53:59 -08001470 if (resultSpan != null) {
1471 resultSpan[0] = xSize;
1472 resultSpan[1] = ySize;
1473 }
1474 bestRect.set(currentRect);
Michael Jurkac28de512010-08-13 11:27:44 -07001475 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001476 }
1477 }
Michael Jurkac6ee42e2010-09-30 12:04:50 -07001478 // re-mark space taken by ignoreView as occupied
Adam Cohen482ed822012-03-02 14:15:13 -08001479 markCellsAsOccupiedForView(ignoreView, occupied);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001480
Adam Cohenc0dcf592011-06-01 15:30:43 -07001481 // Return -1, -1 if no suitable location found
1482 if (bestDistance == Double.MAX_VALUE) {
1483 bestXY[0] = -1;
1484 bestXY[1] = -1;
Jeff Sharkey70864282009-04-07 21:08:40 -07001485 }
Adam Cohend41fbf52012-02-16 23:53:59 -08001486 recycleTempRects(validRegions);
Adam Cohenc0dcf592011-06-01 15:30:43 -07001487 return bestXY;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001488 }
Winson Chungaafa03c2010-06-11 17:34:16 -07001489
Adam Cohen482ed822012-03-02 14:15:13 -08001490 /**
1491 * Find a vacant area that will fit the given bounds nearest the requested
1492 * cell location, and will also weigh in a suggested direction vector of the
1493 * desired location. This method computers distance based on unit grid distances,
1494 * not pixel distances.
1495 *
Adam Cohen47a876d2012-03-19 13:21:41 -07001496 * @param cellX The X cell nearest to which you want to search for a vacant area.
1497 * @param cellY The Y cell nearest which you want to search for a vacant area.
Adam Cohen482ed822012-03-02 14:15:13 -08001498 * @param spanX Horizontal span of the object.
1499 * @param spanY Vertical span of the object.
Adam Cohen47a876d2012-03-19 13:21:41 -07001500 * @param direction The favored direction in which the views should move from x, y
1501 * @param exactDirectionOnly If this parameter is true, then only solutions where the direction
1502 * matches exactly. Otherwise we find the best matching direction.
1503 * @param occoupied The array which represents which cells in the CellLayout are occupied
1504 * @param blockOccupied The array which represents which cells in the specified block (cellX,
1505 * cellY, spanX, spanY) are occupied. This is used when try to move a group of views.
Adam Cohen482ed822012-03-02 14:15:13 -08001506 * @param result Array in which to place the result, or null (in which case a new array will
1507 * be allocated)
1508 * @return The X, Y cell of a vacant area that can contain this object,
1509 * nearest the requested location.
1510 */
1511 private int[] findNearestArea(int cellX, int cellY, int spanX, int spanY, int[] direction,
Adam Cohen47a876d2012-03-19 13:21:41 -07001512 boolean[][] occupied, boolean blockOccupied[][], int[] result) {
Adam Cohen482ed822012-03-02 14:15:13 -08001513 // Keep track of best-scoring drop area
1514 final int[] bestXY = result != null ? result : new int[2];
1515 float bestDistance = Float.MAX_VALUE;
1516 int bestDirectionScore = Integer.MIN_VALUE;
1517
1518 final int countX = mCountX;
1519 final int countY = mCountY;
1520
1521 for (int y = 0; y < countY - (spanY - 1); y++) {
1522 inner:
1523 for (int x = 0; x < countX - (spanX - 1); x++) {
1524 // First, let's see if this thing fits anywhere
1525 for (int i = 0; i < spanX; i++) {
1526 for (int j = 0; j < spanY; j++) {
Adam Cohen47a876d2012-03-19 13:21:41 -07001527 if (occupied[x + i][y + j] && (blockOccupied == null || blockOccupied[i][j])) {
Adam Cohen482ed822012-03-02 14:15:13 -08001528 continue inner;
1529 }
1530 }
1531 }
1532
1533 float distance = (float)
1534 Math.sqrt((x - cellX) * (x - cellX) + (y - cellY) * (y - cellY));
1535 int[] curDirection = mTmpPoint;
Adam Cohen47a876d2012-03-19 13:21:41 -07001536 computeDirectionVector(x - cellX, y - cellY, curDirection);
1537 // The direction score is just the dot product of the two candidate direction
1538 // and that passed in.
Adam Cohen482ed822012-03-02 14:15:13 -08001539 int curDirectionScore = direction[0] * curDirection[0] +
1540 direction[1] * curDirection[1];
Adam Cohen47a876d2012-03-19 13:21:41 -07001541 boolean exactDirectionOnly = false;
1542 boolean directionMatches = direction[0] == curDirection[0] &&
1543 direction[0] == curDirection[0];
1544 if ((directionMatches || !exactDirectionOnly) &&
1545 Float.compare(distance, bestDistance) < 0 || (Float.compare(distance,
Adam Cohen482ed822012-03-02 14:15:13 -08001546 bestDistance) == 0 && curDirectionScore > bestDirectionScore)) {
1547 bestDistance = distance;
1548 bestDirectionScore = curDirectionScore;
1549 bestXY[0] = x;
1550 bestXY[1] = y;
1551 }
1552 }
1553 }
1554
1555 // Return -1, -1 if no suitable location found
1556 if (bestDistance == Float.MAX_VALUE) {
1557 bestXY[0] = -1;
1558 bestXY[1] = -1;
1559 }
1560 return bestXY;
1561 }
1562
Adam Cohen47a876d2012-03-19 13:21:41 -07001563 private int[] findNearestAreaInDirection(int cellX, int cellY, int spanX, int spanY,
1564 int[] direction,boolean[][] occupied,
1565 boolean blockOccupied[][], int[] result) {
1566 // Keep track of best-scoring drop area
1567 final int[] bestXY = result != null ? result : new int[2];
1568 bestXY[0] = -1;
1569 bestXY[1] = -1;
1570 float bestDistance = Float.MAX_VALUE;
1571
1572 // We use this to march in a single direction
Adam Cohen5b53f292012-03-29 14:30:35 -07001573 if ((direction[0] != 0 && direction[1] != 0) ||
1574 (direction[0] == 0 && direction[1] == 0)) {
Adam Cohen47a876d2012-03-19 13:21:41 -07001575 return bestXY;
1576 }
1577
1578 // This will only incrememnet one of x or y based on the assertion above
1579 int x = cellX + direction[0];
1580 int y = cellY + direction[1];
1581 while (x >= 0 && x + spanX <= mCountX && y >= 0 && y + spanY <= mCountY) {
1582
1583 boolean fail = false;
1584 for (int i = 0; i < spanX; i++) {
1585 for (int j = 0; j < spanY; j++) {
1586 if (occupied[x + i][y + j] && (blockOccupied == null || blockOccupied[i][j])) {
1587 fail = true;
1588 }
1589 }
1590 }
1591 if (!fail) {
1592 float distance = (float)
1593 Math.sqrt((x - cellX) * (x - cellX) + (y - cellY) * (y - cellY));
1594 if (Float.compare(distance, bestDistance) < 0) {
1595 bestDistance = distance;
1596 bestXY[0] = x;
1597 bestXY[1] = y;
1598 }
1599 }
1600 x += direction[0];
1601 y += direction[1];
1602 }
1603 return bestXY;
1604 }
1605
Adam Cohen482ed822012-03-02 14:15:13 -08001606 private boolean addViewToTempLocation(View v, Rect rectOccupiedByPotentialDrop,
Adam Cohen8baab352012-03-20 17:39:21 -07001607 int[] direction, ItemConfiguration currentState) {
1608 CellAndSpan c = currentState.map.get(v);
Adam Cohen482ed822012-03-02 14:15:13 -08001609 boolean success = false;
Adam Cohen8baab352012-03-20 17:39:21 -07001610 markCellsForView(c.x, c.y, c.spanX, c.spanY, mTmpOccupied, false);
Adam Cohen482ed822012-03-02 14:15:13 -08001611 markCellsForRect(rectOccupiedByPotentialDrop, mTmpOccupied, true);
1612
Adam Cohen8baab352012-03-20 17:39:21 -07001613 findNearestArea(c.x, c.y, c.spanX, c.spanY, direction, mTmpOccupied, null, mTempLocation);
Adam Cohen482ed822012-03-02 14:15:13 -08001614
1615 if (mTempLocation[0] >= 0 && mTempLocation[1] >= 0) {
Adam Cohen8baab352012-03-20 17:39:21 -07001616 c.x = mTempLocation[0];
1617 c.y = mTempLocation[1];
Adam Cohen482ed822012-03-02 14:15:13 -08001618 success = true;
1619
1620 }
Adam Cohen8baab352012-03-20 17:39:21 -07001621 markCellsForView(c.x, c.y, c.spanX, c.spanY, mTmpOccupied, true);
Adam Cohen482ed822012-03-02 14:15:13 -08001622 return success;
1623 }
1624
Adam Cohen47a876d2012-03-19 13:21:41 -07001625 // This method looks in the specified direction to see if there is an additional view
1626 // immediately adjecent in that direction
1627 private boolean addViewInDirection(ArrayList<View> views, Rect boundingRect, int[] direction,
Adam Cohen19f37922012-03-21 11:59:11 -07001628 boolean[][] occupied, View dragView, ItemConfiguration currentState) {
Adam Cohen47a876d2012-03-19 13:21:41 -07001629 boolean found = false;
1630
Michael Jurkaa52570f2012-03-20 03:18:20 -07001631 int childCount = mShortcutsAndWidgets.getChildCount();
Adam Cohen47a876d2012-03-19 13:21:41 -07001632 Rect r0 = new Rect(boundingRect);
1633 Rect r1 = new Rect();
1634
1635 int deltaX = 0;
1636 int deltaY = 0;
1637 if (direction[1] < 0) {
1638 r0.set(r0.left, r0.top - 1, r0.right, r0.bottom);
1639 deltaY = -1;
1640 } else if (direction[1] > 0) {
1641 r0.set(r0.left, r0.top, r0.right, r0.bottom + 1);
1642 deltaY = 1;
1643 } else if (direction[0] < 0) {
1644 r0.set(r0.left - 1, r0.top, r0.right, r0.bottom);
1645 deltaX = -1;
1646 } else if (direction[0] > 0) {
1647 r0.set(r0.left, r0.top, r0.right + 1, r0.bottom);
1648 deltaX = 1;
1649 }
1650
1651 for (int i = 0; i < childCount; i++) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07001652 View child = mShortcutsAndWidgets.getChildAt(i);
Adam Cohen19f37922012-03-21 11:59:11 -07001653 if (views.contains(child) || child == dragView) continue;
Adam Cohen8baab352012-03-20 17:39:21 -07001654 CellAndSpan c = currentState.map.get(child);
Adam Cohen47a876d2012-03-19 13:21:41 -07001655
Adam Cohen8baab352012-03-20 17:39:21 -07001656 LayoutParams lp = (LayoutParams) child.getLayoutParams();
1657 r1.set(c.x, c.y, c.x + c.spanX, c.y + c.spanY);
Adam Cohen47a876d2012-03-19 13:21:41 -07001658 if (Rect.intersects(r0, r1)) {
1659 if (!lp.canReorder) {
1660 return false;
1661 }
1662 boolean pushed = false;
Adam Cohen8baab352012-03-20 17:39:21 -07001663 for (int x = c.x; x < c.x + c.spanX; x++) {
1664 for (int y = c.y; y < c.y + c.spanY; y++) {
Adam Cohen47a876d2012-03-19 13:21:41 -07001665 boolean inBounds = x - deltaX >= 0 && x -deltaX < mCountX
1666 && y - deltaY >= 0 && y - deltaY < mCountY;
1667 if (inBounds && occupied[x - deltaX][y - deltaY]) {
1668 pushed = true;
1669 }
1670 }
1671 }
1672 if (pushed) {
1673 views.add(child);
Adam Cohen8baab352012-03-20 17:39:21 -07001674 boundingRect.union(c.x, c.y, c.x + c.spanX, c.y + c.spanY);
Adam Cohen47a876d2012-03-19 13:21:41 -07001675 found = true;
1676 }
1677 }
1678 }
1679 return found;
1680 }
1681
Adam Cohen482ed822012-03-02 14:15:13 -08001682 private boolean addViewsToTempLocation(ArrayList<View> views, Rect rectOccupiedByPotentialDrop,
Adam Cohen19f37922012-03-21 11:59:11 -07001683 int[] direction, boolean push, View dragView, ItemConfiguration currentState) {
Adam Cohen482ed822012-03-02 14:15:13 -08001684 if (views.size() == 0) return true;
Adam Cohen482ed822012-03-02 14:15:13 -08001685
Adam Cohen8baab352012-03-20 17:39:21 -07001686 boolean success = false;
Adam Cohen482ed822012-03-02 14:15:13 -08001687 Rect boundingRect = null;
Adam Cohen8baab352012-03-20 17:39:21 -07001688 // We construct a rect which represents the entire group of views passed in
Adam Cohen482ed822012-03-02 14:15:13 -08001689 for (View v: views) {
Adam Cohen8baab352012-03-20 17:39:21 -07001690 CellAndSpan c = currentState.map.get(v);
Adam Cohen482ed822012-03-02 14:15:13 -08001691 if (boundingRect == null) {
Adam Cohen8baab352012-03-20 17:39:21 -07001692 boundingRect = new Rect(c.x, c.y, c.x + c.spanX, c.y + c.spanY);
Adam Cohen482ed822012-03-02 14:15:13 -08001693 } else {
Adam Cohen8baab352012-03-20 17:39:21 -07001694 boundingRect.union(c.x, c.y, c.x + c.spanX, c.y + c.spanY);
Adam Cohen482ed822012-03-02 14:15:13 -08001695 }
1696 }
Adam Cohen8baab352012-03-20 17:39:21 -07001697
1698 @SuppressWarnings("unchecked")
1699 ArrayList<View> dup = (ArrayList<View>) views.clone();
1700 // We try and expand the group of views in the direction vector passed, based on
1701 // whether they are physically adjacent, ie. based on "push mechanics".
Adam Cohen19f37922012-03-21 11:59:11 -07001702 while (push && addViewInDirection(dup, boundingRect, direction, mTmpOccupied, dragView,
Adam Cohen8baab352012-03-20 17:39:21 -07001703 currentState)) {
1704 }
1705
1706 // Mark the occupied state as false for the group of views we want to move.
1707 for (View v: dup) {
1708 CellAndSpan c = currentState.map.get(v);
1709 markCellsForView(c.x, c.y, c.spanX, c.spanY, mTmpOccupied, false);
1710 }
1711
Adam Cohen47a876d2012-03-19 13:21:41 -07001712 boolean[][] blockOccupied = new boolean[boundingRect.width()][boundingRect.height()];
1713 int top = boundingRect.top;
1714 int left = boundingRect.left;
Adam Cohen8baab352012-03-20 17:39:21 -07001715 // We mark more precisely which parts of the bounding rect are truly occupied, allowing
1716 // for tetris-style interlocking.
1717 for (View v: dup) {
1718 CellAndSpan c = currentState.map.get(v);
1719 markCellsForView(c.x - left, c.y - top, c.spanX, c.spanY, blockOccupied, true);
Adam Cohen47a876d2012-03-19 13:21:41 -07001720 }
1721
Adam Cohen482ed822012-03-02 14:15:13 -08001722 markCellsForRect(rectOccupiedByPotentialDrop, mTmpOccupied, true);
1723
Adam Cohen8baab352012-03-20 17:39:21 -07001724 if (push) {
1725 findNearestAreaInDirection(boundingRect.left, boundingRect.top, boundingRect.width(),
1726 boundingRect.height(), direction, mTmpOccupied, blockOccupied, mTempLocation);
1727 } else {
1728 findNearestArea(boundingRect.left, boundingRect.top, boundingRect.width(),
1729 boundingRect.height(), direction, mTmpOccupied, blockOccupied, mTempLocation);
1730 }
Adam Cohen482ed822012-03-02 14:15:13 -08001731
Adam Cohen8baab352012-03-20 17:39:21 -07001732 // If we successfuly found a location by pushing the block of views, we commit it
Adam Cohen482ed822012-03-02 14:15:13 -08001733 if (mTempLocation[0] >= 0 && mTempLocation[1] >= 0) {
Adam Cohen8baab352012-03-20 17:39:21 -07001734 int deltaX = mTempLocation[0] - boundingRect.left;
1735 int deltaY = mTempLocation[1] - boundingRect.top;
1736 for (View v: dup) {
1737 CellAndSpan c = currentState.map.get(v);
1738 c.x += deltaX;
1739 c.y += deltaY;
Adam Cohen482ed822012-03-02 14:15:13 -08001740 }
1741 success = true;
1742 }
Adam Cohen8baab352012-03-20 17:39:21 -07001743
1744 // In either case, we set the occupied array as marked for the location of the views
1745 for (View v: dup) {
1746 CellAndSpan c = currentState.map.get(v);
1747 markCellsForView(c.x, c.y, c.spanX, c.spanY, mTmpOccupied, true);
Adam Cohen482ed822012-03-02 14:15:13 -08001748 }
1749 return success;
1750 }
1751
1752 private void markCellsForRect(Rect r, boolean[][] occupied, boolean value) {
1753 markCellsForView(r.left, r.top, r.width(), r.height(), occupied, value);
1754 }
1755
1756 private boolean rearrangementExists(int cellX, int cellY, int spanX, int spanY, int[] direction,
Adam Cohen8baab352012-03-20 17:39:21 -07001757 View ignoreView, ItemConfiguration solution) {
Adam Cohen482ed822012-03-02 14:15:13 -08001758
Adam Cohen8baab352012-03-20 17:39:21 -07001759 mIntersectingViews.clear();
Adam Cohen482ed822012-03-02 14:15:13 -08001760 mOccupiedRect.set(cellX, cellY, cellX + spanX, cellY + spanY);
Adam Cohen482ed822012-03-02 14:15:13 -08001761
Adam Cohen8baab352012-03-20 17:39:21 -07001762 // Mark the desired location of the view currently being dragged.
Adam Cohen482ed822012-03-02 14:15:13 -08001763 if (ignoreView != null) {
Adam Cohen8baab352012-03-20 17:39:21 -07001764 CellAndSpan c = solution.map.get(ignoreView);
Adam Cohen19f37922012-03-21 11:59:11 -07001765 if (c != null) {
1766 c.x = cellX;
1767 c.y = cellY;
1768 }
Adam Cohen482ed822012-03-02 14:15:13 -08001769 }
Adam Cohen482ed822012-03-02 14:15:13 -08001770 Rect r0 = new Rect(cellX, cellY, cellX + spanX, cellY + spanY);
1771 Rect r1 = new Rect();
Adam Cohen8baab352012-03-20 17:39:21 -07001772 for (View child: solution.map.keySet()) {
Adam Cohen482ed822012-03-02 14:15:13 -08001773 if (child == ignoreView) continue;
Adam Cohen8baab352012-03-20 17:39:21 -07001774 CellAndSpan c = solution.map.get(child);
Adam Cohen482ed822012-03-02 14:15:13 -08001775 LayoutParams lp = (LayoutParams) child.getLayoutParams();
Adam Cohen8baab352012-03-20 17:39:21 -07001776 r1.set(c.x, c.y, c.x + c.spanX, c.y + c.spanY);
Adam Cohen482ed822012-03-02 14:15:13 -08001777 if (Rect.intersects(r0, r1)) {
1778 if (!lp.canReorder) {
1779 return false;
1780 }
1781 mIntersectingViews.add(child);
1782 }
1783 }
Adam Cohen47a876d2012-03-19 13:21:41 -07001784
Adam Cohen8baab352012-03-20 17:39:21 -07001785 // We try to move the intersecting views as a block using the push mechanic
Adam Cohen19f37922012-03-21 11:59:11 -07001786 if (addViewsToTempLocation(mIntersectingViews, mOccupiedRect, direction, true, ignoreView,
1787 solution)) {
Adam Cohen47a876d2012-03-19 13:21:41 -07001788 return true;
1789 }
1790 // Try the opposite direction
1791 direction[0] *= -1;
1792 direction[1] *= -1;
Adam Cohen19f37922012-03-21 11:59:11 -07001793 if (addViewsToTempLocation(mIntersectingViews, mOccupiedRect, direction, true, ignoreView,
1794 solution)) {
Adam Cohen47a876d2012-03-19 13:21:41 -07001795 return true;
1796 }
1797 // Switch the direction back
1798 direction[0] *= -1;
1799 direction[1] *= -1;
1800
Adam Cohen8baab352012-03-20 17:39:21 -07001801 // Next we try moving the views as a block , but without requiring the push mechanic
Adam Cohen19f37922012-03-21 11:59:11 -07001802 if (addViewsToTempLocation(mIntersectingViews, mOccupiedRect, direction, false, ignoreView,
1803 solution)) {
Adam Cohen482ed822012-03-02 14:15:13 -08001804 return true;
1805 }
Adam Cohen47a876d2012-03-19 13:21:41 -07001806
Adam Cohen482ed822012-03-02 14:15:13 -08001807 // Ok, they couldn't move as a block, let's move them individually
1808 for (View v : mIntersectingViews) {
Adam Cohen8baab352012-03-20 17:39:21 -07001809 if (!addViewToTempLocation(v, mOccupiedRect, direction, solution)) {
Adam Cohen482ed822012-03-02 14:15:13 -08001810 return false;
1811 }
1812 }
1813 return true;
1814 }
1815
1816 /*
1817 * Returns a pair (x, y), where x,y are in {-1, 0, 1} corresponding to vector between
1818 * the provided point and the provided cell
1819 */
Adam Cohen47a876d2012-03-19 13:21:41 -07001820 private void computeDirectionVector(float deltaX, float deltaY, int[] result) {
Adam Cohen482ed822012-03-02 14:15:13 -08001821 double angle = Math.atan(((float) deltaY) / deltaX);
1822
1823 result[0] = 0;
1824 result[1] = 0;
1825 if (Math.abs(Math.cos(angle)) > 0.5f) {
1826 result[0] = (int) Math.signum(deltaX);
1827 }
1828 if (Math.abs(Math.sin(angle)) > 0.5f) {
1829 result[1] = (int) Math.signum(deltaY);
1830 }
1831 }
1832
Adam Cohen8baab352012-03-20 17:39:21 -07001833 private void copyOccupiedArray(boolean[][] occupied) {
1834 for (int i = 0; i < mCountX; i++) {
1835 for (int j = 0; j < mCountY; j++) {
1836 occupied[i][j] = mOccupied[i][j];
1837 }
1838 }
1839 }
1840
Adam Cohen482ed822012-03-02 14:15:13 -08001841 ItemConfiguration simpleSwap(int pixelX, int pixelY, int minSpanX, int minSpanY, int spanX,
1842 int spanY, int[] direction, View dragView, boolean decX, ItemConfiguration solution) {
Adam Cohen8baab352012-03-20 17:39:21 -07001843 // Copy the current state into the solution. This solution will be manipulated as necessary.
1844 copyCurrentStateToSolution(solution, false);
1845 // Copy the current occupied array into the temporary occupied array. This array will be
1846 // manipulated as necessary to find a solution.
1847 copyOccupiedArray(mTmpOccupied);
Adam Cohen482ed822012-03-02 14:15:13 -08001848
1849 // We find the nearest cell into which we would place the dragged item, assuming there's
1850 // nothing in its way.
1851 int result[] = new int[2];
1852 result = findNearestArea(pixelX, pixelY, spanX, spanY, result);
1853
1854 boolean success = false;
1855 // First we try the exact nearest position of the item being dragged,
1856 // we will then want to try to move this around to other neighbouring positions
Adam Cohen8baab352012-03-20 17:39:21 -07001857 success = rearrangementExists(result[0], result[1], spanX, spanY, direction, dragView,
1858 solution);
Adam Cohen482ed822012-03-02 14:15:13 -08001859
1860 if (!success) {
1861 // We try shrinking the widget down to size in an alternating pattern, shrink 1 in
1862 // x, then 1 in y etc.
1863 if (spanX > minSpanX && (minSpanY == spanY || decX)) {
1864 return simpleSwap(pixelX, pixelY, minSpanX, minSpanY, spanX - 1, spanY, direction,
1865 dragView, false, solution);
1866 } else if (spanY > minSpanY) {
1867 return simpleSwap(pixelX, pixelY, minSpanX, minSpanY, spanX, spanY - 1, direction,
1868 dragView, true, solution);
1869 }
1870 solution.isSolution = false;
1871 } else {
1872 solution.isSolution = true;
1873 solution.dragViewX = result[0];
1874 solution.dragViewY = result[1];
1875 solution.dragViewSpanX = spanX;
1876 solution.dragViewSpanY = spanY;
Adam Cohen482ed822012-03-02 14:15:13 -08001877 }
1878 return solution;
1879 }
1880
1881 private void copyCurrentStateToSolution(ItemConfiguration solution, boolean temp) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07001882 int childCount = mShortcutsAndWidgets.getChildCount();
Adam Cohen482ed822012-03-02 14:15:13 -08001883 for (int i = 0; i < childCount; i++) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07001884 View child = mShortcutsAndWidgets.getChildAt(i);
Adam Cohen482ed822012-03-02 14:15:13 -08001885 LayoutParams lp = (LayoutParams) child.getLayoutParams();
Adam Cohen8baab352012-03-20 17:39:21 -07001886 CellAndSpan c;
Adam Cohen482ed822012-03-02 14:15:13 -08001887 if (temp) {
Adam Cohen8baab352012-03-20 17:39:21 -07001888 c = new CellAndSpan(lp.tmpCellX, lp.tmpCellY, lp.cellHSpan, lp.cellVSpan);
Adam Cohen482ed822012-03-02 14:15:13 -08001889 } else {
Adam Cohen8baab352012-03-20 17:39:21 -07001890 c = new CellAndSpan(lp.cellX, lp.cellY, lp.cellHSpan, lp.cellVSpan);
Adam Cohen482ed822012-03-02 14:15:13 -08001891 }
Adam Cohen8baab352012-03-20 17:39:21 -07001892 solution.map.put(child, c);
Adam Cohen482ed822012-03-02 14:15:13 -08001893 }
1894 }
1895
1896 private void copySolutionToTempState(ItemConfiguration solution, View dragView) {
1897 for (int i = 0; i < mCountX; i++) {
1898 for (int j = 0; j < mCountY; j++) {
1899 mTmpOccupied[i][j] = false;
1900 }
1901 }
1902
Michael Jurkaa52570f2012-03-20 03:18:20 -07001903 int childCount = mShortcutsAndWidgets.getChildCount();
Adam Cohen482ed822012-03-02 14:15:13 -08001904 for (int i = 0; i < childCount; i++) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07001905 View child = mShortcutsAndWidgets.getChildAt(i);
Adam Cohen482ed822012-03-02 14:15:13 -08001906 if (child == dragView) continue;
1907 LayoutParams lp = (LayoutParams) child.getLayoutParams();
Adam Cohen8baab352012-03-20 17:39:21 -07001908 CellAndSpan c = solution.map.get(child);
1909 if (c != null) {
1910 lp.tmpCellX = c.x;
1911 lp.tmpCellY = c.y;
1912 lp.cellHSpan = c.spanX;
1913 lp.cellVSpan = c.spanY;
1914 markCellsForView(c.x, c.y, c.spanX, c.spanY, mTmpOccupied, true);
Adam Cohen482ed822012-03-02 14:15:13 -08001915 }
1916 }
1917 markCellsForView(solution.dragViewX, solution.dragViewY, solution.dragViewSpanX,
1918 solution.dragViewSpanY, mTmpOccupied, true);
1919 }
1920
1921 private void animateItemsToSolution(ItemConfiguration solution, View dragView, boolean
1922 commitDragView) {
1923
1924 boolean[][] occupied = DESTRUCTIVE_REORDER ? mOccupied : mTmpOccupied;
1925 for (int i = 0; i < mCountX; i++) {
1926 for (int j = 0; j < mCountY; j++) {
1927 occupied[i][j] = false;
1928 }
1929 }
1930
Michael Jurkaa52570f2012-03-20 03:18:20 -07001931 int childCount = mShortcutsAndWidgets.getChildCount();
Adam Cohen482ed822012-03-02 14:15:13 -08001932 for (int i = 0; i < childCount; i++) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07001933 View child = mShortcutsAndWidgets.getChildAt(i);
Adam Cohen482ed822012-03-02 14:15:13 -08001934 if (child == dragView) continue;
Adam Cohen8baab352012-03-20 17:39:21 -07001935 CellAndSpan c = solution.map.get(child);
1936 if (c != null) {
Adam Cohen19f37922012-03-21 11:59:11 -07001937 animateChildToPosition(child, c.x, c.y, REORDER_ANIMATION_DURATION, 0,
1938 DESTRUCTIVE_REORDER, false);
Adam Cohen8baab352012-03-20 17:39:21 -07001939 markCellsForView(c.x, c.y, c.spanX, c.spanY, occupied, true);
Adam Cohen482ed822012-03-02 14:15:13 -08001940 }
1941 }
1942 if (commitDragView) {
1943 markCellsForView(solution.dragViewX, solution.dragViewY, solution.dragViewSpanX,
1944 solution.dragViewSpanY, occupied, true);
1945 }
1946 }
1947
Adam Cohen19f37922012-03-21 11:59:11 -07001948 // This method starts or changes the reorder hint animations
1949 private void beginOrAdjustHintAnimations(ItemConfiguration solution, View dragView, int delay) {
1950 int childCount = mShortcutsAndWidgets.getChildCount();
1951 int timeForPriorAnimationToComplete = getMaxCompletionTime();
1952 for (int i = 0; i < childCount; i++) {
1953 View child = mShortcutsAndWidgets.getChildAt(i);
1954 if (child == dragView) continue;
1955 CellAndSpan c = solution.map.get(child);
1956 LayoutParams lp = (LayoutParams) child.getLayoutParams();
1957 if (c != null) {
1958 ReorderHintAnimation rha = new ReorderHintAnimation(child, lp.cellX, lp.cellY,
1959 c.x, c.y, c.spanX, c.spanY);
1960 rha.animate(timeForPriorAnimationToComplete);
1961 }
1962 }
1963 }
1964
1965 // Class which represents the reorder hint animations. These animations show that an item is
1966 // in a temporary state, and hint at where the item will return to.
1967 class ReorderHintAnimation {
1968 View child;
1969 float deltaX;
1970 float deltaY;
1971 private static final int DURATION = 140;
1972 private int repeatCount;
1973 private boolean cancelOnCycleComplete = false;
1974 ValueAnimator va;
1975
1976 public ReorderHintAnimation(View child, int cellX0, int cellY0, int cellX1, int cellY1,
1977 int spanX, int spanY) {
1978 regionToCenterPoint(cellX0, cellY0, spanX, spanY, mTmpPoint);
1979 final int x0 = mTmpPoint[0];
1980 final int y0 = mTmpPoint[1];
1981 regionToCenterPoint(cellX1, cellY1, spanX, spanY, mTmpPoint);
1982 final int x1 = mTmpPoint[0];
1983 final int y1 = mTmpPoint[1];
1984 final int dX = x1 - x0;
1985 final int dY = y1 - y0;
1986 deltaX = 0;
1987 deltaY = 0;
1988 if (dX == dY && dX == 0) {
1989 } else {
1990 if (dY == 0) {
1991 deltaX = mReorderHintAnimationMagnitude;
1992 } else if (dX == 0) {
1993 deltaY = mReorderHintAnimationMagnitude;
1994 } else {
1995 double angle = Math.atan( (float) (dY) / dX);
1996 deltaX = (int) (Math.cos(angle) * mReorderHintAnimationMagnitude);
1997 deltaY = (int) (Math.sin(angle) * mReorderHintAnimationMagnitude);
1998 }
1999 }
2000 this.child = child;
2001 }
2002
2003 void animate(int delay) {
2004 if (mShakeAnimators.containsKey(child)) {
2005 ReorderHintAnimation oldAnimation = mShakeAnimators.get(child);
2006 oldAnimation.completeAnimation();
2007 mShakeAnimators.remove(child);
2008 }
2009 if (deltaX == 0 && deltaY == 0) {
2010 return;
2011 }
2012 va = ValueAnimator.ofFloat(0f, 1f);
2013 va.setRepeatMode(ValueAnimator.REVERSE);
2014 va.setRepeatCount(ValueAnimator.INFINITE);
2015 va.setDuration(DURATION);
2016 va.addUpdateListener(new AnimatorUpdateListener() {
2017 @Override
2018 public void onAnimationUpdate(ValueAnimator animation) {
2019 float r = ((Float) animation.getAnimatedValue()).floatValue();
2020 float x = r * deltaX;
2021 float y = r * deltaY;
2022 child.setTranslationX(x);
2023 child.setTranslationY(y);
2024 }
2025 });
2026 va.addListener(new AnimatorListenerAdapter() {
2027 public void onAnimationRepeat(Animator animation) {
2028 repeatCount++;
2029 // We make sure to end only after a full period
2030 if (cancelOnCycleComplete && repeatCount % 2 == 0) {
2031 va.cancel();
2032 }
2033 }
2034 });
2035 va.setStartDelay(Math.max(REORDER_ANIMATION_DURATION, delay));
2036 mShakeAnimators.put(child, this);
2037 va.start();
2038 }
2039
2040
2041 private void completeAnimation() {
2042 cancelOnCycleComplete = true;
2043 }
2044
2045 // Returns the time required to complete the current oscillating animation
2046 private int completionTime() {
2047 if (repeatCount % 2 == 0) {
2048 return (int) (va.getDuration() - va.getCurrentPlayTime() + DURATION);
2049 } else {
2050 return (int) (va.getDuration() - va.getCurrentPlayTime());
2051 }
2052 }
2053 }
2054
2055 private void completeAndClearReorderHintAnimations() {
2056 for (ReorderHintAnimation a: mShakeAnimators.values()) {
2057 a.completeAnimation();
2058 }
2059 mShakeAnimators.clear();
2060 }
2061
2062 private int getMaxCompletionTime() {
2063 int maxTime = 0;
2064 for (ReorderHintAnimation a: mShakeAnimators.values()) {
2065 maxTime = Math.max(maxTime, a.completionTime());
2066 }
2067 return maxTime;
2068 }
2069
Adam Cohen482ed822012-03-02 14:15:13 -08002070 private void commitTempPlacement() {
2071 for (int i = 0; i < mCountX; i++) {
2072 for (int j = 0; j < mCountY; j++) {
2073 mOccupied[i][j] = mTmpOccupied[i][j];
2074 }
2075 }
Michael Jurkaa52570f2012-03-20 03:18:20 -07002076 int childCount = mShortcutsAndWidgets.getChildCount();
Adam Cohen482ed822012-03-02 14:15:13 -08002077 for (int i = 0; i < childCount; i++) {
Adam Cohenea889a22012-03-27 16:45:39 -07002078 View child = mShortcutsAndWidgets.getChildAt(i);
2079 LayoutParams lp = (LayoutParams) child.getLayoutParams();
2080 ItemInfo info = (ItemInfo) child.getTag();
Adam Cohen2acce882012-03-28 19:03:19 -07002081 // We do a null check here because the item info can be null in the case of the
2082 // AllApps button in the hotseat.
2083 if (info != null) {
2084 info.cellX = lp.cellX = lp.tmpCellX;
2085 info.cellY = lp.cellY = lp.tmpCellY;
Adam Cohenbebf0422012-04-11 18:06:28 -07002086 info.spanX = lp.cellHSpan;
2087 info.spanY = lp.cellVSpan;
Adam Cohen2acce882012-03-28 19:03:19 -07002088 }
Adam Cohen482ed822012-03-02 14:15:13 -08002089 }
Adam Cohen2acce882012-03-28 19:03:19 -07002090 mLauncher.getWorkspace().updateItemLocationsInDatabase(this);
Adam Cohen482ed822012-03-02 14:15:13 -08002091 }
2092
2093 public void setUseTempCoords(boolean useTempCoords) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07002094 int childCount = mShortcutsAndWidgets.getChildCount();
Adam Cohen482ed822012-03-02 14:15:13 -08002095 for (int i = 0; i < childCount; i++) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07002096 LayoutParams lp = (LayoutParams) mShortcutsAndWidgets.getChildAt(i).getLayoutParams();
Adam Cohen482ed822012-03-02 14:15:13 -08002097 lp.useTmpCoords = useTempCoords;
2098 }
2099 }
2100
Adam Cohen482ed822012-03-02 14:15:13 -08002101 ItemConfiguration findConfigurationNoShuffle(int pixelX, int pixelY, int minSpanX, int minSpanY,
2102 int spanX, int spanY, View dragView, ItemConfiguration solution) {
2103 int[] result = new int[2];
2104 int[] resultSpan = new int[2];
2105 findNearestVacantArea(pixelX, pixelY, minSpanX, minSpanY, spanX, spanY, null, result,
2106 resultSpan);
2107 if (result[0] >= 0 && result[1] >= 0) {
2108 copyCurrentStateToSolution(solution, false);
2109 solution.dragViewX = result[0];
2110 solution.dragViewY = result[1];
2111 solution.dragViewSpanX = resultSpan[0];
2112 solution.dragViewSpanY = resultSpan[1];
2113 solution.isSolution = true;
2114 } else {
2115 solution.isSolution = false;
2116 }
2117 return solution;
2118 }
2119
2120 public void prepareChildForDrag(View child) {
2121 markCellsAsUnoccupiedForView(child);
Adam Cohen482ed822012-03-02 14:15:13 -08002122 }
2123
Adam Cohen19f37922012-03-21 11:59:11 -07002124 /* This seems like it should be obvious and straight-forward, but when the direction vector
2125 needs to match with the notion of the dragView pushing other views, we have to employ
2126 a slightly more subtle notion of the direction vector. The question is what two points is
2127 the vector between? The center of the dragView and its desired destination? Not quite, as
2128 this doesn't necessarily coincide with the interaction of the dragView and items occupying
2129 those cells. Instead we use some heuristics to often lock the vector to up, down, left
2130 or right, which helps make pushing feel right.
2131 */
2132 private void getDirectionVectorForDrop(int dragViewCenterX, int dragViewCenterY, int spanX,
2133 int spanY, View dragView, int[] resultDirection) {
2134 int[] targetDestination = new int[2];
2135
2136 findNearestArea(dragViewCenterX, dragViewCenterY, spanX, spanY, targetDestination);
2137 Rect dragRect = new Rect();
2138 regionToRect(targetDestination[0], targetDestination[1], spanX, spanY, dragRect);
2139 dragRect.offset(dragViewCenterX - dragRect.centerX(), dragViewCenterY - dragRect.centerY());
2140
2141 Rect dropRegionRect = new Rect();
2142 getViewsIntersectingRegion(targetDestination[0], targetDestination[1], spanX, spanY,
2143 dragView, dropRegionRect, mIntersectingViews);
2144
2145 int dropRegionSpanX = dropRegionRect.width();
2146 int dropRegionSpanY = dropRegionRect.height();
2147
2148 regionToRect(dropRegionRect.left, dropRegionRect.top, dropRegionRect.width(),
2149 dropRegionRect.height(), dropRegionRect);
2150
2151 int deltaX = (dropRegionRect.centerX() - dragViewCenterX) / spanX;
2152 int deltaY = (dropRegionRect.centerY() - dragViewCenterY) / spanY;
2153
2154 if (dropRegionSpanX == mCountX || spanX == mCountX) {
2155 deltaX = 0;
2156 }
2157 if (dropRegionSpanY == mCountY || spanY == mCountY) {
2158 deltaY = 0;
2159 }
2160
2161 if (deltaX == 0 && deltaY == 0) {
2162 // No idea what to do, give a random direction.
2163 resultDirection[0] = 1;
2164 resultDirection[1] = 0;
2165 } else {
2166 computeDirectionVector(deltaX, deltaY, resultDirection);
2167 }
2168 }
2169
2170 // For a given cell and span, fetch the set of views intersecting the region.
2171 private void getViewsIntersectingRegion(int cellX, int cellY, int spanX, int spanY,
2172 View dragView, Rect boundingRect, ArrayList<View> intersectingViews) {
2173 if (boundingRect != null) {
2174 boundingRect.set(cellX, cellY, cellX + spanX, cellY + spanY);
2175 }
2176 intersectingViews.clear();
2177 Rect r0 = new Rect(cellX, cellY, cellX + spanX, cellY + spanY);
2178 Rect r1 = new Rect();
2179 final int count = mShortcutsAndWidgets.getChildCount();
2180 for (int i = 0; i < count; i++) {
2181 View child = mShortcutsAndWidgets.getChildAt(i);
2182 if (child == dragView) continue;
2183 LayoutParams lp = (LayoutParams) child.getLayoutParams();
2184 r1.set(lp.cellX, lp.cellY, lp.cellX + lp.cellHSpan, lp.cellY + lp.cellVSpan);
2185 if (Rect.intersects(r0, r1)) {
2186 mIntersectingViews.add(child);
2187 if (boundingRect != null) {
2188 boundingRect.union(r1);
2189 }
2190 }
2191 }
2192 }
2193
2194 boolean isNearestDropLocationOccupied(int pixelX, int pixelY, int spanX, int spanY,
2195 View dragView, int[] result) {
2196 result = findNearestArea(pixelX, pixelY, spanX, spanY, result);
2197 getViewsIntersectingRegion(result[0], result[1], spanX, spanY, dragView, null,
2198 mIntersectingViews);
2199 return !mIntersectingViews.isEmpty();
2200 }
2201
2202 void revertTempState() {
2203 if (!isItemPlacementDirty() || DESTRUCTIVE_REORDER) return;
2204 final int count = mShortcutsAndWidgets.getChildCount();
2205 for (int i = 0; i < count; i++) {
2206 View child = mShortcutsAndWidgets.getChildAt(i);
2207 LayoutParams lp = (LayoutParams) child.getLayoutParams();
2208 if (lp.tmpCellX != lp.cellX || lp.tmpCellY != lp.cellY) {
2209 lp.tmpCellX = lp.cellX;
2210 lp.tmpCellY = lp.cellY;
2211 animateChildToPosition(child, lp.cellX, lp.cellY, REORDER_ANIMATION_DURATION,
2212 0, false, false);
2213 }
2214 }
2215 completeAndClearReorderHintAnimations();
2216 setItemPlacementDirty(false);
2217 }
2218
Adam Cohenbebf0422012-04-11 18:06:28 -07002219 boolean createAreaForResize(int cellX, int cellY, int spanX, int spanY,
2220 View dragView, int[] direction, boolean commit) {
2221 int[] pixelXY = new int[2];
2222 regionToCenterPoint(cellX, cellY, spanX, spanY, pixelXY);
2223
2224 // First we determine if things have moved enough to cause a different layout
2225 ItemConfiguration swapSolution = simpleSwap(pixelXY[0], pixelXY[1], spanX, spanY,
2226 spanX, spanY, direction, dragView, true, new ItemConfiguration());
2227
2228 setUseTempCoords(true);
2229 if (swapSolution != null && swapSolution.isSolution) {
2230 // If we're just testing for a possible location (MODE_ACCEPT_DROP), we don't bother
2231 // committing anything or animating anything as we just want to determine if a solution
2232 // exists
2233 copySolutionToTempState(swapSolution, dragView);
2234 setItemPlacementDirty(true);
2235 animateItemsToSolution(swapSolution, dragView, commit);
2236
2237 if (commit) {
2238 commitTempPlacement();
2239 completeAndClearReorderHintAnimations();
2240 setItemPlacementDirty(false);
2241 } else {
2242 beginOrAdjustHintAnimations(swapSolution, dragView,
2243 REORDER_ANIMATION_DURATION);
2244 }
2245 mShortcutsAndWidgets.requestLayout();
2246 }
2247 return swapSolution.isSolution;
2248 }
2249
Adam Cohen482ed822012-03-02 14:15:13 -08002250 int[] createArea(int pixelX, int pixelY, int minSpanX, int minSpanY, int spanX, int spanY,
2251 View dragView, int[] result, int resultSpan[], int mode) {
Adam Cohen482ed822012-03-02 14:15:13 -08002252 // First we determine if things have moved enough to cause a different layout
Adam Cohen47a876d2012-03-19 13:21:41 -07002253 result = findNearestArea(pixelX, pixelY, spanX, spanY, result);
Adam Cohen482ed822012-03-02 14:15:13 -08002254
2255 if (resultSpan == null) {
2256 resultSpan = new int[2];
2257 }
2258
Adam Cohen19f37922012-03-21 11:59:11 -07002259 // When we are checking drop validity or actually dropping, we don't recompute the
2260 // direction vector, since we want the solution to match the preview, and it's possible
2261 // that the exact position of the item has changed to result in a new reordering outcome.
Adam Cohenb209e632012-03-27 17:09:36 -07002262 if ((mode == MODE_ON_DROP || mode == MODE_ON_DROP_EXTERNAL || mode == MODE_ACCEPT_DROP)
2263 && mPreviousReorderDirection[0] != INVALID_DIRECTION) {
Adam Cohen19f37922012-03-21 11:59:11 -07002264 mDirectionVector[0] = mPreviousReorderDirection[0];
2265 mDirectionVector[1] = mPreviousReorderDirection[1];
2266 // We reset this vector after drop
Adam Cohenb209e632012-03-27 17:09:36 -07002267 if (mode == MODE_ON_DROP || mode == MODE_ON_DROP_EXTERNAL) {
2268 mPreviousReorderDirection[0] = INVALID_DIRECTION;
2269 mPreviousReorderDirection[1] = INVALID_DIRECTION;
Adam Cohen19f37922012-03-21 11:59:11 -07002270 }
2271 } else {
2272 getDirectionVectorForDrop(pixelX, pixelY, spanX, spanY, dragView, mDirectionVector);
2273 mPreviousReorderDirection[0] = mDirectionVector[0];
2274 mPreviousReorderDirection[1] = mDirectionVector[1];
2275 }
2276
Adam Cohen482ed822012-03-02 14:15:13 -08002277 ItemConfiguration swapSolution = simpleSwap(pixelX, pixelY, minSpanX, minSpanY,
2278 spanX, spanY, mDirectionVector, dragView, true, new ItemConfiguration());
2279
2280 // We attempt the approach which doesn't shuffle views at all
2281 ItemConfiguration noShuffleSolution = findConfigurationNoShuffle(pixelX, pixelY, minSpanX,
2282 minSpanY, spanX, spanY, dragView, new ItemConfiguration());
2283
2284 ItemConfiguration finalSolution = null;
2285 if (swapSolution.isSolution && swapSolution.area() >= noShuffleSolution.area()) {
2286 finalSolution = swapSolution;
2287 } else if (noShuffleSolution.isSolution) {
2288 finalSolution = noShuffleSolution;
2289 }
2290
2291 boolean foundSolution = true;
2292 if (!DESTRUCTIVE_REORDER) {
2293 setUseTempCoords(true);
2294 }
2295
2296 if (finalSolution != null) {
2297 result[0] = finalSolution.dragViewX;
2298 result[1] = finalSolution.dragViewY;
2299 resultSpan[0] = finalSolution.dragViewSpanX;
2300 resultSpan[1] = finalSolution.dragViewSpanY;
2301
2302 // If we're just testing for a possible location (MODE_ACCEPT_DROP), we don't bother
2303 // committing anything or animating anything as we just want to determine if a solution
2304 // exists
2305 if (mode == MODE_DRAG_OVER || mode == MODE_ON_DROP || mode == MODE_ON_DROP_EXTERNAL) {
2306 if (!DESTRUCTIVE_REORDER) {
2307 copySolutionToTempState(finalSolution, dragView);
2308 }
2309 setItemPlacementDirty(true);
2310 animateItemsToSolution(finalSolution, dragView, mode == MODE_ON_DROP);
2311
Adam Cohen19f37922012-03-21 11:59:11 -07002312 if (!DESTRUCTIVE_REORDER &&
2313 (mode == MODE_ON_DROP || mode == MODE_ON_DROP_EXTERNAL)) {
Adam Cohen482ed822012-03-02 14:15:13 -08002314 commitTempPlacement();
Adam Cohen19f37922012-03-21 11:59:11 -07002315 completeAndClearReorderHintAnimations();
2316 setItemPlacementDirty(false);
2317 } else {
2318 beginOrAdjustHintAnimations(finalSolution, dragView,
2319 REORDER_ANIMATION_DURATION);
Adam Cohen482ed822012-03-02 14:15:13 -08002320 }
2321 }
2322 } else {
2323 foundSolution = false;
2324 result[0] = result[1] = resultSpan[0] = resultSpan[1] = -1;
2325 }
2326
2327 if ((mode == MODE_ON_DROP || !foundSolution) && !DESTRUCTIVE_REORDER) {
2328 setUseTempCoords(false);
2329 }
Adam Cohen482ed822012-03-02 14:15:13 -08002330
Michael Jurkaa52570f2012-03-20 03:18:20 -07002331 mShortcutsAndWidgets.requestLayout();
Adam Cohen482ed822012-03-02 14:15:13 -08002332 return result;
2333 }
2334
Adam Cohen19f37922012-03-21 11:59:11 -07002335 void setItemPlacementDirty(boolean dirty) {
2336 mItemPlacementDirty = dirty;
Adam Cohen482ed822012-03-02 14:15:13 -08002337 }
Adam Cohen19f37922012-03-21 11:59:11 -07002338 boolean isItemPlacementDirty() {
2339 return mItemPlacementDirty;
Adam Cohen482ed822012-03-02 14:15:13 -08002340 }
2341
2342 private class ItemConfiguration {
Adam Cohen8baab352012-03-20 17:39:21 -07002343 HashMap<View, CellAndSpan> map = new HashMap<View, CellAndSpan>();
Adam Cohen482ed822012-03-02 14:15:13 -08002344 boolean isSolution = false;
2345 int dragViewX, dragViewY, dragViewSpanX, dragViewSpanY;
2346
2347 int area() {
2348 return dragViewSpanX * dragViewSpanY;
2349 }
Adam Cohen8baab352012-03-20 17:39:21 -07002350 }
2351
2352 private class CellAndSpan {
2353 int x, y;
2354 int spanX, spanY;
2355
2356 public CellAndSpan(int x, int y, int spanX, int spanY) {
2357 this.x = x;
2358 this.y = y;
2359 this.spanX = spanX;
2360 this.spanY = spanY;
Adam Cohen482ed822012-03-02 14:15:13 -08002361 }
2362 }
2363
Adam Cohendf035382011-04-11 17:22:04 -07002364 /**
2365 * Find a vacant area that will fit the given bounds nearest the requested
2366 * cell location. Uses Euclidean distance to score multiple vacant areas.
2367 *
2368 * @param pixelX The X location at which you want to search for a vacant area.
2369 * @param pixelY The Y location at which you want to search for a vacant area.
2370 * @param spanX Horizontal span of the object.
2371 * @param spanY Vertical span of the object.
2372 * @param ignoreView Considers space occupied by this view as unoccupied
2373 * @param result Previously returned value to possibly recycle.
2374 * @return The X, Y cell of a vacant area that can contain this object,
2375 * nearest the requested location.
2376 */
2377 int[] findNearestVacantArea(
2378 int pixelX, int pixelY, int spanX, int spanY, View ignoreView, int[] result) {
2379 return findNearestArea(pixelX, pixelY, spanX, spanY, ignoreView, true, result);
2380 }
2381
2382 /**
Adam Cohend41fbf52012-02-16 23:53:59 -08002383 * Find a vacant area that will fit the given bounds nearest the requested
2384 * cell location. Uses Euclidean distance to score multiple vacant areas.
2385 *
2386 * @param pixelX The X location at which you want to search for a vacant area.
2387 * @param pixelY The Y location at which you want to search for a vacant area.
2388 * @param minSpanX The minimum horizontal span required
2389 * @param minSpanY The minimum vertical span required
2390 * @param spanX Horizontal span of the object.
2391 * @param spanY Vertical span of the object.
2392 * @param ignoreView Considers space occupied by this view as unoccupied
2393 * @param result Previously returned value to possibly recycle.
2394 * @return The X, Y cell of a vacant area that can contain this object,
2395 * nearest the requested location.
2396 */
2397 int[] findNearestVacantArea(int pixelX, int pixelY, int minSpanX, int minSpanY,
2398 int spanX, int spanY, View ignoreView, int[] result, int[] resultSpan) {
Adam Cohen482ed822012-03-02 14:15:13 -08002399 return findNearestArea(pixelX, pixelY, minSpanX, minSpanY, spanX, spanY, ignoreView, true,
2400 result, resultSpan, mOccupied);
Adam Cohend41fbf52012-02-16 23:53:59 -08002401 }
2402
2403 /**
Adam Cohendf035382011-04-11 17:22:04 -07002404 * Find a starting cell position that will fit the given bounds nearest the requested
2405 * cell location. Uses Euclidean distance to score multiple vacant areas.
2406 *
2407 * @param pixelX The X location at which you want to search for a vacant area.
2408 * @param pixelY The Y location at which you want to search for a vacant area.
2409 * @param spanX Horizontal span of the object.
2410 * @param spanY Vertical span of the object.
2411 * @param ignoreView Considers space occupied by this view as unoccupied
2412 * @param result Previously returned value to possibly recycle.
2413 * @return The X, Y cell of a vacant area that can contain this object,
2414 * nearest the requested location.
2415 */
2416 int[] findNearestArea(
2417 int pixelX, int pixelY, int spanX, int spanY, int[] result) {
2418 return findNearestArea(pixelX, pixelY, spanX, spanY, null, false, result);
2419 }
2420
Michael Jurka0280c3b2010-09-17 15:00:07 -07002421 boolean existsEmptyCell() {
2422 return findCellForSpan(null, 1, 1);
2423 }
2424
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002425 /**
Michael Jurka0280c3b2010-09-17 15:00:07 -07002426 * Finds the upper-left coordinate of the first rectangle in the grid that can
2427 * hold a cell of the specified dimensions. If intersectX and intersectY are not -1,
2428 * then this method will only return coordinates for rectangles that contain the cell
2429 * (intersectX, intersectY)
2430 *
2431 * @param cellXY The array that will contain the position of a vacant cell if such a cell
2432 * can be found.
2433 * @param spanX The horizontal span of the cell we want to find.
2434 * @param spanY The vertical span of the cell we want to find.
2435 *
2436 * @return True if a vacant cell of the specified dimension was found, false otherwise.
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07002437 */
Michael Jurka0280c3b2010-09-17 15:00:07 -07002438 boolean findCellForSpan(int[] cellXY, int spanX, int spanY) {
Adam Cohen482ed822012-03-02 14:15:13 -08002439 return findCellForSpanThatIntersectsIgnoring(cellXY, spanX, spanY, -1, -1, null, mOccupied);
Michael Jurka0280c3b2010-09-17 15:00:07 -07002440 }
2441
2442 /**
2443 * Like above, but ignores any cells occupied by the item "ignoreView"
2444 *
2445 * @param cellXY The array that will contain the position of a vacant cell if such a cell
2446 * can be found.
2447 * @param spanX The horizontal span of the cell we want to find.
2448 * @param spanY The vertical span of the cell we want to find.
2449 * @param ignoreView The home screen item we should treat as not occupying any space
2450 * @return
2451 */
2452 boolean findCellForSpanIgnoring(int[] cellXY, int spanX, int spanY, View ignoreView) {
Adam Cohen482ed822012-03-02 14:15:13 -08002453 return findCellForSpanThatIntersectsIgnoring(cellXY, spanX, spanY, -1, -1,
2454 ignoreView, mOccupied);
Michael Jurka0280c3b2010-09-17 15:00:07 -07002455 }
2456
2457 /**
2458 * Like above, but if intersectX and intersectY are not -1, then this method will try to
2459 * return coordinates for rectangles that contain the cell [intersectX, intersectY]
2460 *
2461 * @param spanX The horizontal span of the cell we want to find.
2462 * @param spanY The vertical span of the cell we want to find.
2463 * @param ignoreView The home screen item we should treat as not occupying any space
2464 * @param intersectX The X coordinate of the cell that we should try to overlap
2465 * @param intersectX The Y coordinate of the cell that we should try to overlap
2466 *
2467 * @return True if a vacant cell of the specified dimension was found, false otherwise.
2468 */
2469 boolean findCellForSpanThatIntersects(int[] cellXY, int spanX, int spanY,
2470 int intersectX, int intersectY) {
2471 return findCellForSpanThatIntersectsIgnoring(
Adam Cohen482ed822012-03-02 14:15:13 -08002472 cellXY, spanX, spanY, intersectX, intersectY, null, mOccupied);
Michael Jurka0280c3b2010-09-17 15:00:07 -07002473 }
2474
2475 /**
2476 * The superset of the above two methods
2477 */
2478 boolean findCellForSpanThatIntersectsIgnoring(int[] cellXY, int spanX, int spanY,
Adam Cohen482ed822012-03-02 14:15:13 -08002479 int intersectX, int intersectY, View ignoreView, boolean occupied[][]) {
Michael Jurkac6ee42e2010-09-30 12:04:50 -07002480 // mark space take by ignoreView as available (method checks if ignoreView is null)
Adam Cohen482ed822012-03-02 14:15:13 -08002481 markCellsAsUnoccupiedForView(ignoreView, occupied);
Michael Jurka0280c3b2010-09-17 15:00:07 -07002482
Michael Jurka28750fb2010-09-24 17:43:49 -07002483 boolean foundCell = false;
Michael Jurka0280c3b2010-09-17 15:00:07 -07002484 while (true) {
2485 int startX = 0;
2486 if (intersectX >= 0) {
2487 startX = Math.max(startX, intersectX - (spanX - 1));
2488 }
2489 int endX = mCountX - (spanX - 1);
2490 if (intersectX >= 0) {
2491 endX = Math.min(endX, intersectX + (spanX - 1) + (spanX == 1 ? 1 : 0));
2492 }
2493 int startY = 0;
2494 if (intersectY >= 0) {
2495 startY = Math.max(startY, intersectY - (spanY - 1));
2496 }
2497 int endY = mCountY - (spanY - 1);
2498 if (intersectY >= 0) {
2499 endY = Math.min(endY, intersectY + (spanY - 1) + (spanY == 1 ? 1 : 0));
2500 }
2501
Winson Chungbbc60d82010-11-11 16:34:41 -08002502 for (int y = startY; y < endY && !foundCell; y++) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002503 inner:
Winson Chungbbc60d82010-11-11 16:34:41 -08002504 for (int x = startX; x < endX; x++) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002505 for (int i = 0; i < spanX; i++) {
2506 for (int j = 0; j < spanY; j++) {
Adam Cohen482ed822012-03-02 14:15:13 -08002507 if (occupied[x + i][y + j]) {
Winson Chungbbc60d82010-11-11 16:34:41 -08002508 // small optimization: we can skip to after the column we just found
Michael Jurka0280c3b2010-09-17 15:00:07 -07002509 // an occupied cell
Winson Chungbbc60d82010-11-11 16:34:41 -08002510 x += i;
Michael Jurka0280c3b2010-09-17 15:00:07 -07002511 continue inner;
2512 }
2513 }
2514 }
2515 if (cellXY != null) {
2516 cellXY[0] = x;
2517 cellXY[1] = y;
2518 }
Michael Jurka28750fb2010-09-24 17:43:49 -07002519 foundCell = true;
2520 break;
Michael Jurka0280c3b2010-09-17 15:00:07 -07002521 }
2522 }
2523 if (intersectX == -1 && intersectY == -1) {
2524 break;
2525 } else {
2526 // if we failed to find anything, try again but without any requirements of
2527 // intersecting
2528 intersectX = -1;
2529 intersectY = -1;
2530 continue;
2531 }
2532 }
2533
Michael Jurkac6ee42e2010-09-30 12:04:50 -07002534 // re-mark space taken by ignoreView as occupied
Adam Cohen482ed822012-03-02 14:15:13 -08002535 markCellsAsOccupiedForView(ignoreView, occupied);
Michael Jurka28750fb2010-09-24 17:43:49 -07002536 return foundCell;
Michael Jurka0280c3b2010-09-17 15:00:07 -07002537 }
2538
2539 /**
Winson Chungc07918d2011-07-01 15:35:26 -07002540 * A drag event has begun over this layout.
2541 * It may have begun over this layout (in which case onDragChild is called first),
2542 * or it may have begun on another layout.
2543 */
2544 void onDragEnter() {
Adam Cohenc6cc61d2012-04-04 12:47:08 -07002545 mDragEnforcer.onDragEnter();
Winson Chungc07918d2011-07-01 15:35:26 -07002546 if (!mDragging) {
2547 // Fade in the drag indicators
2548 if (mCrosshairsAnimator != null) {
2549 mCrosshairsAnimator.animateIn();
2550 }
2551 }
2552 mDragging = true;
2553 }
2554
2555 /**
Michael Jurka0280c3b2010-09-17 15:00:07 -07002556 * Called when drag has left this CellLayout or has been completed (successfully or not)
2557 */
2558 void onDragExit() {
Adam Cohenc6cc61d2012-04-04 12:47:08 -07002559 mDragEnforcer.onDragExit();
Joe Onorato4be866d2010-10-10 11:26:02 -07002560 // This can actually be called when we aren't in a drag, e.g. when adding a new
2561 // item to this layout via the customize drawer.
2562 // Guard against that case.
2563 if (mDragging) {
2564 mDragging = false;
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07002565
Joe Onorato4be866d2010-10-10 11:26:02 -07002566 // Fade out the drag indicators
2567 if (mCrosshairsAnimator != null) {
2568 mCrosshairsAnimator.animateOut();
2569 }
Patrick Dubroyde7658b2010-09-27 11:15:43 -07002570 }
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -07002571
2572 // Invalidate the drag data
Adam Cohend41fbf52012-02-16 23:53:59 -08002573 mDragCell[0] = mDragCell[1] = -1;
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -07002574 mDragOutlineAnims[mDragOutlineCurrent].animateOut();
2575 mDragOutlineCurrent = (mDragOutlineCurrent + 1) % mDragOutlineAnims.length;
Adam Cohen19f37922012-03-21 11:59:11 -07002576 revertTempState();
Michael Jurka33945b22010-12-21 18:19:38 -08002577 setIsDragOverlapping(false);
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07002578 }
2579
2580 /**
Winson Chungaafa03c2010-06-11 17:34:16 -07002581 * Mark a child as having been dropped.
Patrick Dubroyde7658b2010-09-27 11:15:43 -07002582 * At the beginning of the drag operation, the child may have been on another
Patrick Dubroyce34a972010-10-19 10:34:32 -07002583 * screen, but it is re-parented before this method is called.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002584 *
2585 * @param child The child that is being dropped
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002586 */
Adam Cohen716b51e2011-06-30 12:09:54 -07002587 void onDropChild(View child) {
Romain Guyd94533d2009-08-17 10:01:15 -07002588 if (child != null) {
2589 LayoutParams lp = (LayoutParams) child.getLayoutParams();
Romain Guy84f296c2009-11-04 15:00:44 -08002590 lp.dropped = true;
Romain Guyd94533d2009-08-17 10:01:15 -07002591 child.requestLayout();
Romain Guyd94533d2009-08-17 10:01:15 -07002592 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002593 }
2594
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002595 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002596 * Computes a bounding rectangle for a range of cells
Winson Chungaafa03c2010-06-11 17:34:16 -07002597 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002598 * @param cellX X coordinate of upper left corner expressed as a cell position
2599 * @param cellY Y coordinate of upper left corner expressed as a cell position
Winson Chungaafa03c2010-06-11 17:34:16 -07002600 * @param cellHSpan Width in cells
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002601 * @param cellVSpan Height in cells
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07002602 * @param resultRect Rect into which to put the results
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002603 */
Adam Cohend41fbf52012-02-16 23:53:59 -08002604 public void cellToRect(int cellX, int cellY, int cellHSpan, int cellVSpan, Rect resultRect) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002605 final int cellWidth = mCellWidth;
2606 final int cellHeight = mCellHeight;
2607 final int widthGap = mWidthGap;
2608 final int heightGap = mHeightGap;
Winson Chungaafa03c2010-06-11 17:34:16 -07002609
Winson Chung4b825dcd2011-06-19 12:41:22 -07002610 final int hStartPadding = getPaddingLeft();
2611 final int vStartPadding = getPaddingTop();
Winson Chungaafa03c2010-06-11 17:34:16 -07002612
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002613 int width = cellHSpan * cellWidth + ((cellHSpan - 1) * widthGap);
2614 int height = cellVSpan * cellHeight + ((cellVSpan - 1) * heightGap);
2615
2616 int x = hStartPadding + cellX * (cellWidth + widthGap);
2617 int y = vStartPadding + cellY * (cellHeight + heightGap);
Winson Chungaafa03c2010-06-11 17:34:16 -07002618
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07002619 resultRect.set(x, y, x + width, y + height);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002620 }
Winson Chungaafa03c2010-06-11 17:34:16 -07002621
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002622 /**
Winson Chungaafa03c2010-06-11 17:34:16 -07002623 * Computes the required horizontal and vertical cell spans to always
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002624 * fit the given rectangle.
Winson Chungaafa03c2010-06-11 17:34:16 -07002625 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002626 * @param width Width in pixels
2627 * @param height Height in pixels
Patrick Dubroy8f86ddc2010-07-16 13:55:32 -07002628 * @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 -08002629 */
Patrick Dubroy8f86ddc2010-07-16 13:55:32 -07002630 public int[] rectToCell(int width, int height, int[] result) {
Michael Jurka9987a5c2010-10-08 16:58:12 -07002631 return rectToCell(getResources(), width, height, result);
2632 }
2633
2634 public static int[] rectToCell(Resources resources, int width, int height, int[] result) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002635 // Always assume we're working with the smallest span to make sure we
2636 // reserve enough space in both orientations.
Joe Onorato79e56262009-09-21 15:23:04 -04002637 int actualWidth = resources.getDimensionPixelSize(R.dimen.workspace_cell_width);
2638 int actualHeight = resources.getDimensionPixelSize(R.dimen.workspace_cell_height);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002639 int smallerSize = Math.min(actualWidth, actualHeight);
Joe Onorato79e56262009-09-21 15:23:04 -04002640
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002641 // Always round up to next largest cell
Winson Chung54c725c2011-08-03 12:03:40 -07002642 int spanX = (int) Math.ceil(width / (float) smallerSize);
2643 int spanY = (int) Math.ceil(height / (float) smallerSize);
Joe Onorato79e56262009-09-21 15:23:04 -04002644
Patrick Dubroy8f86ddc2010-07-16 13:55:32 -07002645 if (result == null) {
2646 return new int[] { spanX, spanY };
2647 }
2648 result[0] = spanX;
2649 result[1] = spanY;
2650 return result;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002651 }
2652
Michael Jurkaf12c75c2011-01-25 22:41:40 -08002653 public int[] cellSpansToSize(int hSpans, int vSpans) {
2654 int[] size = new int[2];
2655 size[0] = hSpans * mCellWidth + (hSpans - 1) * mWidthGap;
2656 size[1] = vSpans * mCellHeight + (vSpans - 1) * mHeightGap;
2657 return size;
2658 }
2659
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002660 /**
Patrick Dubroy047379a2010-12-19 22:02:04 -08002661 * Calculate the grid spans needed to fit given item
2662 */
2663 public void calculateSpans(ItemInfo info) {
2664 final int minWidth;
2665 final int minHeight;
2666
2667 if (info instanceof LauncherAppWidgetInfo) {
2668 minWidth = ((LauncherAppWidgetInfo) info).minWidth;
2669 minHeight = ((LauncherAppWidgetInfo) info).minHeight;
2670 } else if (info instanceof PendingAddWidgetInfo) {
2671 minWidth = ((PendingAddWidgetInfo) info).minWidth;
2672 minHeight = ((PendingAddWidgetInfo) info).minHeight;
2673 } else {
2674 // It's not a widget, so it must be 1x1
2675 info.spanX = info.spanY = 1;
2676 return;
2677 }
2678 int[] spans = rectToCell(minWidth, minHeight, null);
2679 info.spanX = spans[0];
2680 info.spanY = spans[1];
2681 }
2682
2683 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002684 * Find the first vacant cell, if there is one.
2685 *
2686 * @param vacant Holds the x and y coordinate of the vacant cell
2687 * @param spanX Horizontal cell span.
2688 * @param spanY Vertical cell span.
Winson Chungaafa03c2010-06-11 17:34:16 -07002689 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002690 * @return True if a vacant cell was found
2691 */
2692 public boolean getVacantCell(int[] vacant, int spanX, int spanY) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002693
Michael Jurka0280c3b2010-09-17 15:00:07 -07002694 return findVacantCell(vacant, spanX, spanY, mCountX, mCountY, mOccupied);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002695 }
2696
2697 static boolean findVacantCell(int[] vacant, int spanX, int spanY,
2698 int xCount, int yCount, boolean[][] occupied) {
2699
Adam Cohen2801caf2011-05-13 20:57:39 -07002700 for (int y = 0; y < yCount; y++) {
2701 for (int x = 0; x < xCount; x++) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002702 boolean available = !occupied[x][y];
2703out: for (int i = x; i < x + spanX - 1 && x < xCount; i++) {
2704 for (int j = y; j < y + spanY - 1 && y < yCount; j++) {
2705 available = available && !occupied[i][j];
2706 if (!available) break out;
2707 }
2708 }
2709
2710 if (available) {
2711 vacant[0] = x;
2712 vacant[1] = y;
2713 return true;
2714 }
2715 }
2716 }
2717
2718 return false;
2719 }
2720
Michael Jurka0280c3b2010-09-17 15:00:07 -07002721 private void clearOccupiedCells() {
2722 for (int x = 0; x < mCountX; x++) {
2723 for (int y = 0; y < mCountY; y++) {
2724 mOccupied[x][y] = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002725 }
2726 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07002727 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002728
Adam Cohend41fbf52012-02-16 23:53:59 -08002729 public void onMove(View view, int newCellX, int newCellY, int newSpanX, int newSpanY) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002730 markCellsAsUnoccupiedForView(view);
Adam Cohen482ed822012-03-02 14:15:13 -08002731 markCellsForView(newCellX, newCellY, newSpanX, newSpanY, mOccupied, true);
Michael Jurka0280c3b2010-09-17 15:00:07 -07002732 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002733
Adam Cohend4844c32011-02-18 19:25:06 -08002734 public void markCellsAsOccupiedForView(View view) {
Adam Cohen482ed822012-03-02 14:15:13 -08002735 markCellsAsOccupiedForView(view, mOccupied);
2736 }
2737 public void markCellsAsOccupiedForView(View view, boolean[][] occupied) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07002738 if (view == null || view.getParent() != mShortcutsAndWidgets) return;
Michael Jurka0280c3b2010-09-17 15:00:07 -07002739 LayoutParams lp = (LayoutParams) view.getLayoutParams();
Adam Cohen482ed822012-03-02 14:15:13 -08002740 markCellsForView(lp.cellX, lp.cellY, lp.cellHSpan, lp.cellVSpan, occupied, true);
Michael Jurka0280c3b2010-09-17 15:00:07 -07002741 }
2742
Adam Cohend4844c32011-02-18 19:25:06 -08002743 public void markCellsAsUnoccupiedForView(View view) {
Adam Cohen482ed822012-03-02 14:15:13 -08002744 markCellsAsUnoccupiedForView(view, mOccupied);
2745 }
2746 public void markCellsAsUnoccupiedForView(View view, boolean occupied[][]) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07002747 if (view == null || view.getParent() != mShortcutsAndWidgets) return;
Michael Jurka0280c3b2010-09-17 15:00:07 -07002748 LayoutParams lp = (LayoutParams) view.getLayoutParams();
Adam Cohen482ed822012-03-02 14:15:13 -08002749 markCellsForView(lp.cellX, lp.cellY, lp.cellHSpan, lp.cellVSpan, occupied, false);
Michael Jurka0280c3b2010-09-17 15:00:07 -07002750 }
2751
Adam Cohen482ed822012-03-02 14:15:13 -08002752 private void markCellsForView(int cellX, int cellY, int spanX, int spanY, boolean[][] occupied,
2753 boolean value) {
2754 if (cellX < 0 || cellY < 0) return;
Michael Jurka0280c3b2010-09-17 15:00:07 -07002755 for (int x = cellX; x < cellX + spanX && x < mCountX; x++) {
2756 for (int y = cellY; y < cellY + spanY && y < mCountY; y++) {
Adam Cohen482ed822012-03-02 14:15:13 -08002757 occupied[x][y] = value;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002758 }
2759 }
2760 }
2761
Adam Cohen2801caf2011-05-13 20:57:39 -07002762 public int getDesiredWidth() {
Winson Chung4b825dcd2011-06-19 12:41:22 -07002763 return mPaddingLeft + mPaddingRight + (mCountX * mCellWidth) +
Adam Cohen2801caf2011-05-13 20:57:39 -07002764 (Math.max((mCountX - 1), 0) * mWidthGap);
2765 }
2766
2767 public int getDesiredHeight() {
Winson Chung4b825dcd2011-06-19 12:41:22 -07002768 return mPaddingTop + mPaddingBottom + (mCountY * mCellHeight) +
Adam Cohen2801caf2011-05-13 20:57:39 -07002769 (Math.max((mCountY - 1), 0) * mHeightGap);
2770 }
2771
Michael Jurka66d72172011-04-12 16:29:25 -07002772 public boolean isOccupied(int x, int y) {
2773 if (x < mCountX && y < mCountY) {
2774 return mOccupied[x][y];
2775 } else {
2776 throw new RuntimeException("Position exceeds the bound of this CellLayout");
2777 }
2778 }
2779
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002780 @Override
2781 public ViewGroup.LayoutParams generateLayoutParams(AttributeSet attrs) {
2782 return new CellLayout.LayoutParams(getContext(), attrs);
2783 }
2784
2785 @Override
2786 protected boolean checkLayoutParams(ViewGroup.LayoutParams p) {
2787 return p instanceof CellLayout.LayoutParams;
2788 }
2789
2790 @Override
2791 protected ViewGroup.LayoutParams generateLayoutParams(ViewGroup.LayoutParams p) {
2792 return new CellLayout.LayoutParams(p);
2793 }
2794
Winson Chungaafa03c2010-06-11 17:34:16 -07002795 public static class CellLayoutAnimationController extends LayoutAnimationController {
2796 public CellLayoutAnimationController(Animation animation, float delay) {
2797 super(animation, delay);
2798 }
2799
2800 @Override
2801 protected long getDelayForView(View view) {
2802 return (int) (Math.random() * 150);
2803 }
2804 }
2805
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002806 public static class LayoutParams extends ViewGroup.MarginLayoutParams {
2807 /**
2808 * Horizontal location of the item in the grid.
2809 */
2810 @ViewDebug.ExportedProperty
2811 public int cellX;
2812
2813 /**
2814 * Vertical location of the item in the grid.
2815 */
2816 @ViewDebug.ExportedProperty
2817 public int cellY;
2818
2819 /**
Adam Cohen482ed822012-03-02 14:15:13 -08002820 * Temporary horizontal location of the item in the grid during reorder
2821 */
2822 public int tmpCellX;
2823
2824 /**
2825 * Temporary vertical location of the item in the grid during reorder
2826 */
2827 public int tmpCellY;
2828
2829 /**
2830 * Indicates that the temporary coordinates should be used to layout the items
2831 */
2832 public boolean useTmpCoords;
2833
2834 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002835 * Number of cells spanned horizontally by the item.
2836 */
2837 @ViewDebug.ExportedProperty
2838 public int cellHSpan;
2839
2840 /**
2841 * Number of cells spanned vertically by the item.
2842 */
2843 @ViewDebug.ExportedProperty
2844 public int cellVSpan;
Winson Chungaafa03c2010-06-11 17:34:16 -07002845
Adam Cohen1b607ed2011-03-03 17:26:50 -08002846 /**
2847 * Indicates whether the item will set its x, y, width and height parameters freely,
2848 * or whether these will be computed based on cellX, cellY, cellHSpan and cellVSpan.
2849 */
Adam Cohend4844c32011-02-18 19:25:06 -08002850 public boolean isLockedToGrid = true;
2851
Adam Cohen482ed822012-03-02 14:15:13 -08002852 /**
2853 * Indicates whether this item can be reordered. Always true except in the case of the
2854 * the AllApps button.
2855 */
2856 public boolean canReorder = true;
2857
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002858 // X coordinate of the view in the layout.
2859 @ViewDebug.ExportedProperty
2860 int x;
2861 // Y coordinate of the view in the layout.
2862 @ViewDebug.ExportedProperty
2863 int y;
2864
Romain Guy84f296c2009-11-04 15:00:44 -08002865 boolean dropped;
Romain Guyfcb9e712009-10-02 16:06:52 -07002866
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002867 public LayoutParams(Context c, AttributeSet attrs) {
2868 super(c, attrs);
2869 cellHSpan = 1;
2870 cellVSpan = 1;
2871 }
2872
2873 public LayoutParams(ViewGroup.LayoutParams source) {
2874 super(source);
2875 cellHSpan = 1;
2876 cellVSpan = 1;
2877 }
Winson Chungaafa03c2010-06-11 17:34:16 -07002878
2879 public LayoutParams(LayoutParams source) {
2880 super(source);
2881 this.cellX = source.cellX;
2882 this.cellY = source.cellY;
2883 this.cellHSpan = source.cellHSpan;
2884 this.cellVSpan = source.cellVSpan;
2885 }
2886
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002887 public LayoutParams(int cellX, int cellY, int cellHSpan, int cellVSpan) {
Romain Guy8f19cdd2010-01-08 15:07:00 -08002888 super(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002889 this.cellX = cellX;
2890 this.cellY = cellY;
2891 this.cellHSpan = cellHSpan;
2892 this.cellVSpan = cellVSpan;
2893 }
2894
Adam Cohen7f4eabe2011-04-21 16:19:16 -07002895 public void setup(int cellWidth, int cellHeight, int widthGap, int heightGap) {
Adam Cohend4844c32011-02-18 19:25:06 -08002896 if (isLockedToGrid) {
2897 final int myCellHSpan = cellHSpan;
2898 final int myCellVSpan = cellVSpan;
Adam Cohen482ed822012-03-02 14:15:13 -08002899 final int myCellX = useTmpCoords ? tmpCellX : cellX;
2900 final int myCellY = useTmpCoords ? tmpCellY : cellY;
Adam Cohen1b607ed2011-03-03 17:26:50 -08002901
Adam Cohend4844c32011-02-18 19:25:06 -08002902 width = myCellHSpan * cellWidth + ((myCellHSpan - 1) * widthGap) -
2903 leftMargin - rightMargin;
2904 height = myCellVSpan * cellHeight + ((myCellVSpan - 1) * heightGap) -
2905 topMargin - bottomMargin;
Winson Chungeecf02d2012-03-02 17:14:58 -08002906 x = (int) (myCellX * (cellWidth + widthGap) + leftMargin);
2907 y = (int) (myCellY * (cellHeight + heightGap) + topMargin);
Adam Cohend4844c32011-02-18 19:25:06 -08002908 }
2909 }
Winson Chungaafa03c2010-06-11 17:34:16 -07002910
Winson Chungaafa03c2010-06-11 17:34:16 -07002911 public String toString() {
2912 return "(" + this.cellX + ", " + this.cellY + ")";
2913 }
Adam Cohen7f4eabe2011-04-21 16:19:16 -07002914
2915 public void setWidth(int width) {
2916 this.width = width;
2917 }
2918
2919 public int getWidth() {
2920 return width;
2921 }
2922
2923 public void setHeight(int height) {
2924 this.height = height;
2925 }
2926
2927 public int getHeight() {
2928 return height;
2929 }
2930
2931 public void setX(int x) {
2932 this.x = x;
2933 }
2934
2935 public int getX() {
2936 return x;
2937 }
2938
2939 public void setY(int y) {
2940 this.y = y;
2941 }
2942
2943 public int getY() {
2944 return y;
2945 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002946 }
2947
Michael Jurka0280c3b2010-09-17 15:00:07 -07002948 // This class stores info for two purposes:
2949 // 1. When dragging items (mDragInfo in Workspace), we store the View, its cellX & cellY,
2950 // its spanX, spanY, and the screen it is on
2951 // 2. When long clicking on an empty cell in a CellLayout, we save information about the
2952 // cellX and cellY coordinates and which page was clicked. We then set this as a tag on
2953 // the CellLayout that was long clicked
Michael Jurkae5fb0f22011-04-11 13:27:46 -07002954 static final class CellInfo {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002955 View cell;
Michael Jurkaa63c4522010-08-19 13:52:27 -07002956 int cellX = -1;
2957 int cellY = -1;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002958 int spanX;
2959 int spanY;
2960 int screen;
Winson Chung3d503fb2011-07-13 17:25:49 -07002961 long container;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002962
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002963 @Override
2964 public String toString() {
Winson Chungaafa03c2010-06-11 17:34:16 -07002965 return "Cell[view=" + (cell == null ? "null" : cell.getClass())
2966 + ", x=" + cellX + ", y=" + cellY + "]";
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002967 }
2968 }
Michael Jurkad771c962011-08-09 15:00:48 -07002969
2970 public boolean lastDownOnOccupiedCell() {
2971 return mLastDownOnOccupiedCell;
2972 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002973}