blob: ce914e17aef307a4da1a913520794cb35979af28 [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
Winson Chung4b825dcd2011-06-19 12:41:22 -070060 private int mOriginalCellWidth;
61 private int mOriginalCellHeight;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080062 private int mCellWidth;
63 private int mCellHeight;
Winson Chungaafa03c2010-06-11 17:34:16 -070064
Adam Cohend22015c2010-07-26 22:02:18 -070065 private int mCountX;
66 private int mCountY;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080067
Adam Cohen234c4cd2011-07-17 21:03:04 -070068 private int mOriginalWidthGap;
69 private int mOriginalHeightGap;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080070 private int mWidthGap;
71 private int mHeightGap;
Winson Chung4b825dcd2011-06-19 12:41:22 -070072 private int mMaxGap;
Adam Cohenebea84d2011-11-09 17:20:41 -080073 private boolean mScrollingTransformsDirty = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080074
75 private final Rect mRect = new Rect();
76 private final CellInfo mCellInfo = new CellInfo();
Winson Chungaafa03c2010-06-11 17:34:16 -070077
Patrick Dubroyde7658b2010-09-27 11:15:43 -070078 // These are temporary variables to prevent having to allocate a new object just to
79 // return an (x, y) value from helper functions. Do NOT use them to maintain other state.
Winson Chung0be025d2011-05-23 17:45:09 -070080 private final int[] mTmpXY = new int[2];
Patrick Dubroyde7658b2010-09-27 11:15:43 -070081 private final int[] mTmpPoint = new int[2];
82 private final PointF mTmpPointF = new PointF();
Adam Cohen69ce2e52011-07-03 19:25:21 -070083 int[] mTempLocation = new int[2];
Patrick Dubroy6569f2c2010-07-12 14:25:18 -070084
The Android Open Source Project31dd5032009-03-03 19:32:27 -080085 boolean[][] mOccupied;
Adam Cohen482ed822012-03-02 14:15:13 -080086 boolean[][] mTmpOccupied;
Michael Jurkad771c962011-08-09 15:00:48 -070087 private boolean mLastDownOnOccupiedCell = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080088
Michael Jurkadee05892010-07-27 10:01:56 -070089 private OnTouchListener mInterceptTouchListener;
90
Adam Cohen69ce2e52011-07-03 19:25:21 -070091 private ArrayList<FolderRingAnimator> mFolderOuterRings = new ArrayList<FolderRingAnimator>();
Adam Cohenc51934b2011-07-26 21:07:43 -070092 private int[] mFolderLeaveBehindCell = {-1, -1};
Adam Cohen69ce2e52011-07-03 19:25:21 -070093
Adam Cohenb5ba0972011-09-07 18:02:31 -070094 private int mForegroundAlpha = 0;
Michael Jurka5f1c5092010-09-03 14:15:02 -070095 private float mBackgroundAlpha;
Adam Cohen1b0aaac2010-10-28 11:11:18 -070096 private float mBackgroundAlphaMultiplier = 1.0f;
Adam Cohenf34bab52010-09-30 14:11:56 -070097
Michael Jurka33945b22010-12-21 18:19:38 -080098 private Drawable mNormalBackground;
Michael Jurka33945b22010-12-21 18:19:38 -080099 private Drawable mActiveGlowBackground;
Adam Cohenb5ba0972011-09-07 18:02:31 -0700100 private Drawable mOverScrollForegroundDrawable;
101 private Drawable mOverScrollLeft;
102 private Drawable mOverScrollRight;
Michael Jurka18014792010-10-14 09:01:34 -0700103 private Rect mBackgroundRect;
Adam Cohenb5ba0972011-09-07 18:02:31 -0700104 private Rect mForegroundRect;
Adam Cohenb5ba0972011-09-07 18:02:31 -0700105 private int mForegroundPadding;
Patrick Dubroy1262e362010-10-06 15:49:50 -0700106
Michael Jurka33945b22010-12-21 18:19:38 -0800107 // If we're actively dragging something over this screen, mIsDragOverlapping is true
108 private boolean mIsDragOverlapping = false;
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700109 private final Point mDragCenter = new Point();
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700110
Winson Chung150fbab2010-09-29 17:14:26 -0700111 // These arrays are used to implement the drag visualization on x-large screens.
Joe Onorato4be866d2010-10-10 11:26:02 -0700112 // They are used as circular arrays, indexed by mDragOutlineCurrent.
Adam Cohend41fbf52012-02-16 23:53:59 -0800113 private Rect[] mDragOutlines = new Rect[4];
Chet Haase472b2812010-10-14 07:02:04 -0700114 private float[] mDragOutlineAlphas = new float[mDragOutlines.length];
Joe Onorato4be866d2010-10-10 11:26:02 -0700115 private InterruptibleInOutAnimator[] mDragOutlineAnims =
116 new InterruptibleInOutAnimator[mDragOutlines.length];
Winson Chung150fbab2010-09-29 17:14:26 -0700117
118 // Used as an index into the above 3 arrays; indicates which is the most current value.
Joe Onorato4be866d2010-10-10 11:26:02 -0700119 private int mDragOutlineCurrent = 0;
Patrick Dubroy8e58e912010-10-14 13:21:48 -0700120 private final Paint mDragOutlinePaint = new Paint();
Winson Chung150fbab2010-09-29 17:14:26 -0700121
Patrick Dubroy96864c32011-03-10 17:17:23 -0800122 private BubbleTextView mPressedOrFocusedIcon;
123
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700124 private Drawable mCrosshairsDrawable = null;
Patrick Dubroy49250ad2010-10-08 15:33:52 -0700125 private InterruptibleInOutAnimator mCrosshairsAnimator = null;
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700126 private float mCrosshairsVisibility = 0.0f;
127
Adam Cohen482ed822012-03-02 14:15:13 -0800128 private HashMap<CellLayout.LayoutParams, Animator> mReorderAnimators = new
129 HashMap<CellLayout.LayoutParams, Animator>();
Adam Cohen19f37922012-03-21 11:59:11 -0700130 private HashMap<View, ReorderHintAnimation>
131 mShakeAnimators = new HashMap<View, ReorderHintAnimation>();
132
133 private boolean mItemPlacementDirty = false;
Adam Cohenbfbfd262011-06-13 16:55:12 -0700134
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700135 // When a drag operation is in progress, holds the nearest cell to the touch point
136 private final int[] mDragCell = new int[2];
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800137
Joe Onorato4be866d2010-10-10 11:26:02 -0700138 private boolean mDragging = false;
139
Patrick Dubroyce34a972010-10-19 10:34:32 -0700140 private TimeInterpolator mEaseOutInterpolator;
Michael Jurkaa52570f2012-03-20 03:18:20 -0700141 private ShortcutAndWidgetContainer mShortcutsAndWidgets;
Patrick Dubroyce34a972010-10-19 10:34:32 -0700142
Andrew Flynn0dca1ec2012-02-29 13:33:22 -0800143 private boolean mIsHotseat = false;
Winson Chungeecf02d2012-03-02 17:14:58 -0800144 private float mChildScale = 1f;
145 private float mHotseatChildScale = 1f;
Andrew Flynn0dca1ec2012-02-29 13:33:22 -0800146
Adam Cohen482ed822012-03-02 14:15:13 -0800147 public static final int MODE_DRAG_OVER = 0;
148 public static final int MODE_ON_DROP = 1;
149 public static final int MODE_ON_DROP_EXTERNAL = 2;
150 public static final int MODE_ACCEPT_DROP = 3;
Adam Cohen19f37922012-03-21 11:59:11 -0700151 private static final boolean DESTRUCTIVE_REORDER = false;
Adam Cohen482ed822012-03-02 14:15:13 -0800152 private static final boolean DEBUG_VISUALIZE_OCCUPIED = false;
153
Adam Cohen19f37922012-03-21 11:59:11 -0700154 private static final float REORDER_HINT_MAGNITUDE = 0.27f;
155 private static final int REORDER_ANIMATION_DURATION = 150;
156 private float mReorderHintAnimationMagnitude;
157
Adam Cohen482ed822012-03-02 14:15:13 -0800158 private ArrayList<View> mIntersectingViews = new ArrayList<View>();
159 private Rect mOccupiedRect = new Rect();
160 private int[] mDirectionVector = new int[2];
Adam Cohen19f37922012-03-21 11:59:11 -0700161 int[] mPreviousReorderDirection = new int[2];
Adam Cohen482ed822012-03-02 14:15:13 -0800162
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800163 public CellLayout(Context context) {
164 this(context, null);
165 }
166
167 public CellLayout(Context context, AttributeSet attrs) {
168 this(context, attrs, 0);
169 }
170
171 public CellLayout(Context context, AttributeSet attrs, int defStyle) {
172 super(context, attrs, defStyle);
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700173
174 // A ViewGroup usually does not draw, but CellLayout needs to draw a rectangle to show
175 // the user where a dragged item will land when dropped.
176 setWillNotDraw(false);
Michael Jurkaa63c4522010-08-19 13:52:27 -0700177
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800178 TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.CellLayout, defStyle, 0);
179
Winson Chung4b825dcd2011-06-19 12:41:22 -0700180 mOriginalCellWidth =
181 mCellWidth = a.getDimensionPixelSize(R.styleable.CellLayout_cellWidth, 10);
182 mOriginalCellHeight =
183 mCellHeight = a.getDimensionPixelSize(R.styleable.CellLayout_cellHeight, 10);
Adam Cohen234c4cd2011-07-17 21:03:04 -0700184 mWidthGap = mOriginalWidthGap = a.getDimensionPixelSize(R.styleable.CellLayout_widthGap, 0);
185 mHeightGap = mOriginalHeightGap = a.getDimensionPixelSize(R.styleable.CellLayout_heightGap, 0);
Winson Chung4b825dcd2011-06-19 12:41:22 -0700186 mMaxGap = a.getDimensionPixelSize(R.styleable.CellLayout_maxGap, 0);
Adam Cohend22015c2010-07-26 22:02:18 -0700187 mCountX = LauncherModel.getCellCountX();
188 mCountY = LauncherModel.getCellCountY();
Michael Jurka0280c3b2010-09-17 15:00:07 -0700189 mOccupied = new boolean[mCountX][mCountY];
Adam Cohen482ed822012-03-02 14:15:13 -0800190 mTmpOccupied = new boolean[mCountX][mCountY];
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800191
192 a.recycle();
193
194 setAlwaysDrawnWithCacheEnabled(false);
195
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700196 final Resources res = getResources();
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700197
Winson Chung967289b2011-06-30 18:09:30 -0700198 mNormalBackground = res.getDrawable(R.drawable.homescreen_blue_normal_holo);
Winson Chungdea74b72011-09-13 18:06:43 -0700199 mActiveGlowBackground = res.getDrawable(R.drawable.homescreen_blue_strong_holo);
Michael Jurka33945b22010-12-21 18:19:38 -0800200
Adam Cohenb5ba0972011-09-07 18:02:31 -0700201 mOverScrollLeft = res.getDrawable(R.drawable.overscroll_glow_left);
202 mOverScrollRight = res.getDrawable(R.drawable.overscroll_glow_right);
203 mForegroundPadding =
204 res.getDimensionPixelSize(R.dimen.workspace_overscroll_drawable_padding);
Michael Jurka33945b22010-12-21 18:19:38 -0800205
Adam Cohen19f37922012-03-21 11:59:11 -0700206 mReorderHintAnimationMagnitude = (REORDER_HINT_MAGNITUDE *
207 res.getDimensionPixelSize(R.dimen.app_icon_size));
208
Winson Chungb26f3d62011-06-02 10:49:29 -0700209 mNormalBackground.setFilterBitmap(true);
Winson Chungb26f3d62011-06-02 10:49:29 -0700210 mActiveGlowBackground.setFilterBitmap(true);
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700211
Winson Chungeecf02d2012-03-02 17:14:58 -0800212 int iconScale = res.getInteger(R.integer.app_icon_scale_percent);
213 if (iconScale >= 0) {
214 mChildScale = iconScale / 100f;
215 }
216 int hotseatIconScale = res.getInteger(R.integer.app_icon_hotseat_scale_percent);
217 if (hotseatIconScale >= 0) {
218 mHotseatChildScale = hotseatIconScale / 100f;
219 }
Andrew Flynn0dca1ec2012-02-29 13:33:22 -0800220
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700221 // Initialize the data structures used for the drag visualization.
Winson Chung150fbab2010-09-29 17:14:26 -0700222
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700223 mCrosshairsDrawable = res.getDrawable(R.drawable.gardening_crosshairs);
Patrick Dubroyce34a972010-10-19 10:34:32 -0700224 mEaseOutInterpolator = new DecelerateInterpolator(2.5f); // Quint ease out
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700225
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700226 // Set up the animation for fading the crosshairs in and out
227 int animDuration = res.getInteger(R.integer.config_crosshairsFadeInTime);
Patrick Dubroy49250ad2010-10-08 15:33:52 -0700228 mCrosshairsAnimator = new InterruptibleInOutAnimator(animDuration, 0.0f, 1.0f);
Chet Haase472b2812010-10-14 07:02:04 -0700229 mCrosshairsAnimator.getAnimator().addUpdateListener(new AnimatorUpdateListener() {
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700230 public void onAnimationUpdate(ValueAnimator animation) {
231 mCrosshairsVisibility = ((Float) animation.getAnimatedValue()).floatValue();
Patrick Dubroy8e58e912010-10-14 13:21:48 -0700232 invalidate();
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700233 }
234 });
Patrick Dubroyce34a972010-10-19 10:34:32 -0700235 mCrosshairsAnimator.getAnimator().setInterpolator(mEaseOutInterpolator);
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700236
Winson Chungb8c69f32011-10-19 21:36:08 -0700237 mDragCell[0] = mDragCell[1] = -1;
Joe Onorato4be866d2010-10-10 11:26:02 -0700238 for (int i = 0; i < mDragOutlines.length; i++) {
Adam Cohend41fbf52012-02-16 23:53:59 -0800239 mDragOutlines[i] = new Rect(-1, -1, -1, -1);
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700240 }
241
242 // When dragging things around the home screens, we show a green outline of
243 // where the item will land. The outlines gradually fade out, leaving a trail
244 // behind the drag path.
245 // Set up all the animations that are used to implement this fading.
246 final int duration = res.getInteger(R.integer.config_dragOutlineFadeTime);
Chet Haase472b2812010-10-14 07:02:04 -0700247 final float fromAlphaValue = 0;
248 final float toAlphaValue = (float)res.getInteger(R.integer.config_dragOutlineMaxAlpha);
Joe Onorato4be866d2010-10-10 11:26:02 -0700249
Patrick Dubroy8e58e912010-10-14 13:21:48 -0700250 Arrays.fill(mDragOutlineAlphas, fromAlphaValue);
Joe Onorato4be866d2010-10-10 11:26:02 -0700251
252 for (int i = 0; i < mDragOutlineAnims.length; i++) {
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700253 final InterruptibleInOutAnimator anim =
254 new InterruptibleInOutAnimator(duration, fromAlphaValue, toAlphaValue);
Patrick Dubroyce34a972010-10-19 10:34:32 -0700255 anim.getAnimator().setInterpolator(mEaseOutInterpolator);
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700256 final int thisIndex = i;
Chet Haase472b2812010-10-14 07:02:04 -0700257 anim.getAnimator().addUpdateListener(new AnimatorUpdateListener() {
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700258 public void onAnimationUpdate(ValueAnimator animation) {
Joe Onorato4be866d2010-10-10 11:26:02 -0700259 final Bitmap outline = (Bitmap)anim.getTag();
260
261 // If an animation is started and then stopped very quickly, we can still
262 // get spurious updates we've cleared the tag. Guard against this.
263 if (outline == null) {
Patrick Dubroyfe6bd872010-10-13 17:32:10 -0700264 if (false) {
265 Object val = animation.getAnimatedValue();
266 Log.d(TAG, "anim " + thisIndex + " update: " + val +
267 ", isStopped " + anim.isStopped());
268 }
Joe Onorato4be866d2010-10-10 11:26:02 -0700269 // Try to prevent it from continuing to run
270 animation.cancel();
271 } else {
Chet Haase472b2812010-10-14 07:02:04 -0700272 mDragOutlineAlphas[thisIndex] = (Float) animation.getAnimatedValue();
Adam Cohend41fbf52012-02-16 23:53:59 -0800273 CellLayout.this.invalidate(mDragOutlines[thisIndex]);
Joe Onorato4be866d2010-10-10 11:26:02 -0700274 }
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700275 }
276 });
Joe Onorato4be866d2010-10-10 11:26:02 -0700277 // The animation holds a reference to the drag outline bitmap as long is it's
278 // running. This way the bitmap can be GCed when the animations are complete.
Chet Haase472b2812010-10-14 07:02:04 -0700279 anim.getAnimator().addListener(new AnimatorListenerAdapter() {
Michael Jurka3c4c20f2010-10-28 15:36:06 -0700280 @Override
Joe Onorato4be866d2010-10-10 11:26:02 -0700281 public void onAnimationEnd(Animator animation) {
Chet Haase472b2812010-10-14 07:02:04 -0700282 if ((Float) ((ValueAnimator) animation).getAnimatedValue() == 0f) {
Joe Onorato4be866d2010-10-10 11:26:02 -0700283 anim.setTag(null);
284 }
285 }
286 });
287 mDragOutlineAnims[i] = anim;
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700288 }
Patrick Dubroyce34a972010-10-19 10:34:32 -0700289
Michael Jurka18014792010-10-14 09:01:34 -0700290 mBackgroundRect = new Rect();
Adam Cohenb5ba0972011-09-07 18:02:31 -0700291 mForegroundRect = new Rect();
Michael Jurkabea15192010-11-17 12:33:46 -0800292
Michael Jurkaa52570f2012-03-20 03:18:20 -0700293 mShortcutsAndWidgets = new ShortcutAndWidgetContainer(context);
294 mShortcutsAndWidgets.setCellDimensions(mCellWidth, mCellHeight, mWidthGap, mHeightGap);
295 addView(mShortcutsAndWidgets);
Michael Jurka18014792010-10-14 09:01:34 -0700296 }
297
Michael Jurkaf6440da2011-04-05 14:50:34 -0700298 static int widthInPortrait(Resources r, int numCells) {
299 // We use this method from Workspace to figure out how many rows/columns Launcher should
300 // have. We ignore the left/right padding on CellLayout because it turns out in our design
301 // the padding extends outside the visible screen size, but it looked fine anyway.
Michael Jurkaf6440da2011-04-05 14:50:34 -0700302 int cellWidth = r.getDimensionPixelSize(R.dimen.workspace_cell_width);
Winson Chung4b825dcd2011-06-19 12:41:22 -0700303 int minGap = Math.min(r.getDimensionPixelSize(R.dimen.workspace_width_gap),
304 r.getDimensionPixelSize(R.dimen.workspace_height_gap));
Michael Jurkaf6440da2011-04-05 14:50:34 -0700305
Winson Chung4b825dcd2011-06-19 12:41:22 -0700306 return minGap * (numCells - 1) + cellWidth * numCells;
Michael Jurkaf6440da2011-04-05 14:50:34 -0700307 }
308
Michael Jurkaf6440da2011-04-05 14:50:34 -0700309 static int heightInLandscape(Resources r, int numCells) {
310 // We use this method from Workspace to figure out how many rows/columns Launcher should
311 // have. We ignore the left/right padding on CellLayout because it turns out in our design
312 // the padding extends outside the visible screen size, but it looked fine anyway.
Michael Jurkaf6440da2011-04-05 14:50:34 -0700313 int cellHeight = r.getDimensionPixelSize(R.dimen.workspace_cell_height);
Winson Chung4b825dcd2011-06-19 12:41:22 -0700314 int minGap = Math.min(r.getDimensionPixelSize(R.dimen.workspace_width_gap),
315 r.getDimensionPixelSize(R.dimen.workspace_height_gap));
Michael Jurkaf6440da2011-04-05 14:50:34 -0700316
Winson Chung4b825dcd2011-06-19 12:41:22 -0700317 return minGap * (numCells - 1) + cellHeight * numCells;
Michael Jurkaf6440da2011-04-05 14:50:34 -0700318 }
319
Adam Cohen2801caf2011-05-13 20:57:39 -0700320 public void enableHardwareLayers() {
Michael Jurkaa52570f2012-03-20 03:18:20 -0700321 mShortcutsAndWidgets.enableHardwareLayers();
Adam Cohen2801caf2011-05-13 20:57:39 -0700322 }
323
324 public void setGridSize(int x, int y) {
325 mCountX = x;
326 mCountY = y;
327 mOccupied = new boolean[mCountX][mCountY];
Adam Cohen482ed822012-03-02 14:15:13 -0800328 mTmpOccupied = new boolean[mCountX][mCountY];
Adam Cohen7fbec102012-03-27 12:42:19 -0700329 mTempRectStack.clear();
Adam Cohen76fc0852011-06-17 13:26:23 -0700330 requestLayout();
Adam Cohen2801caf2011-05-13 20:57:39 -0700331 }
332
Patrick Dubroy96864c32011-03-10 17:17:23 -0800333 private void invalidateBubbleTextView(BubbleTextView icon) {
334 final int padding = icon.getPressedOrFocusedBackgroundPadding();
Winson Chung4b825dcd2011-06-19 12:41:22 -0700335 invalidate(icon.getLeft() + getPaddingLeft() - padding,
336 icon.getTop() + getPaddingTop() - padding,
337 icon.getRight() + getPaddingLeft() + padding,
338 icon.getBottom() + getPaddingTop() + padding);
Patrick Dubroy96864c32011-03-10 17:17:23 -0800339 }
340
Adam Cohenb5ba0972011-09-07 18:02:31 -0700341 void setOverScrollAmount(float r, boolean left) {
342 if (left && mOverScrollForegroundDrawable != mOverScrollLeft) {
343 mOverScrollForegroundDrawable = mOverScrollLeft;
344 } else if (!left && mOverScrollForegroundDrawable != mOverScrollRight) {
345 mOverScrollForegroundDrawable = mOverScrollRight;
346 }
347
348 mForegroundAlpha = (int) Math.round((r * 255));
349 mOverScrollForegroundDrawable.setAlpha(mForegroundAlpha);
350 invalidate();
351 }
352
Patrick Dubroy96864c32011-03-10 17:17:23 -0800353 void setPressedOrFocusedIcon(BubbleTextView icon) {
354 // We draw the pressed or focused BubbleTextView's background in CellLayout because it
355 // requires an expanded clip rect (due to the glow's blur radius)
356 BubbleTextView oldIcon = mPressedOrFocusedIcon;
357 mPressedOrFocusedIcon = icon;
358 if (oldIcon != null) {
359 invalidateBubbleTextView(oldIcon);
360 }
361 if (mPressedOrFocusedIcon != null) {
362 invalidateBubbleTextView(mPressedOrFocusedIcon);
363 }
364 }
365
Michael Jurka33945b22010-12-21 18:19:38 -0800366 void setIsDragOverlapping(boolean isDragOverlapping) {
367 if (mIsDragOverlapping != isDragOverlapping) {
368 mIsDragOverlapping = isDragOverlapping;
369 invalidate();
370 }
371 }
372
373 boolean getIsDragOverlapping() {
374 return mIsDragOverlapping;
375 }
376
Adam Cohenebea84d2011-11-09 17:20:41 -0800377 protected void setOverscrollTransformsDirty(boolean dirty) {
378 mScrollingTransformsDirty = dirty;
379 }
380
381 protected void resetOverscrollTransforms() {
382 if (mScrollingTransformsDirty) {
383 setOverscrollTransformsDirty(false);
384 setTranslationX(0);
385 setRotationY(0);
386 // It doesn't matter if we pass true or false here, the important thing is that we
387 // pass 0, which results in the overscroll drawable not being drawn any more.
388 setOverScrollAmount(0, false);
389 setPivotX(getMeasuredWidth() / 2);
390 setPivotY(getMeasuredHeight() / 2);
391 }
392 }
393
Jeff Sharkey83f111d2009-04-20 21:03:13 -0700394 @Override
Patrick Dubroy1262e362010-10-06 15:49:50 -0700395 protected void onDraw(Canvas canvas) {
Michael Jurka3e7c7632010-10-02 16:01:03 -0700396 // When we're large, we are either drawn in a "hover" state (ie when dragging an item to
397 // a neighboring page) or with just a normal background (if backgroundAlpha > 0.0f)
398 // When we're small, we are either drawn normally or in the "accepts drops" state (during
399 // a drag). However, we also drag the mini hover background *over* one of those two
400 // backgrounds
Winson Chungb26f3d62011-06-02 10:49:29 -0700401 if (mBackgroundAlpha > 0.0f) {
Adam Cohenf34bab52010-09-30 14:11:56 -0700402 Drawable bg;
Michael Jurka33945b22010-12-21 18:19:38 -0800403
404 if (mIsDragOverlapping) {
405 // In the mini case, we draw the active_glow bg *over* the active background
Michael Jurkabdf78552011-10-31 14:34:25 -0700406 bg = mActiveGlowBackground;
Adam Cohenf34bab52010-09-30 14:11:56 -0700407 } else {
Michael Jurkabdf78552011-10-31 14:34:25 -0700408 bg = mNormalBackground;
Adam Cohenf34bab52010-09-30 14:11:56 -0700409 }
Michael Jurka33945b22010-12-21 18:19:38 -0800410
411 bg.setAlpha((int) (mBackgroundAlpha * mBackgroundAlphaMultiplier * 255));
412 bg.setBounds(mBackgroundRect);
413 bg.draw(canvas);
Michael Jurkaa63c4522010-08-19 13:52:27 -0700414 }
Romain Guya6abce82009-11-10 02:54:41 -0800415
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700416 if (mCrosshairsVisibility > 0.0f) {
417 final int countX = mCountX;
418 final int countY = mCountY;
419
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700420 final float MAX_ALPHA = 0.4f;
421 final int MAX_VISIBLE_DISTANCE = 600;
422 final float DISTANCE_MULTIPLIER = 0.002f;
423
424 final Drawable d = mCrosshairsDrawable;
425 final int width = d.getIntrinsicWidth();
426 final int height = d.getIntrinsicHeight();
427
Winson Chung4b825dcd2011-06-19 12:41:22 -0700428 int x = getPaddingLeft() - (mWidthGap / 2) - (width / 2);
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700429 for (int col = 0; col <= countX; col++) {
Winson Chung4b825dcd2011-06-19 12:41:22 -0700430 int y = getPaddingTop() - (mHeightGap / 2) - (height / 2);
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700431 for (int row = 0; row <= countY; row++) {
432 mTmpPointF.set(x - mDragCenter.x, y - mDragCenter.y);
433 float dist = mTmpPointF.length();
434 // Crosshairs further from the drag point are more faint
435 float alpha = Math.min(MAX_ALPHA,
436 DISTANCE_MULTIPLIER * (MAX_VISIBLE_DISTANCE - dist));
437 if (alpha > 0.0f) {
438 d.setBounds(x, y, x + width, y + height);
439 d.setAlpha((int) (alpha * 255 * mCrosshairsVisibility));
440 d.draw(canvas);
441 }
442 y += mCellHeight + mHeightGap;
443 }
444 x += mCellWidth + mWidthGap;
445 }
Joe Onorato4be866d2010-10-10 11:26:02 -0700446 }
Winson Chung150fbab2010-09-29 17:14:26 -0700447
Patrick Dubroy8e58e912010-10-14 13:21:48 -0700448 final Paint paint = mDragOutlinePaint;
Joe Onorato4be866d2010-10-10 11:26:02 -0700449 for (int i = 0; i < mDragOutlines.length; i++) {
Chet Haase472b2812010-10-14 07:02:04 -0700450 final float alpha = mDragOutlineAlphas[i];
Joe Onorato4be866d2010-10-10 11:26:02 -0700451 if (alpha > 0) {
Adam Cohend41fbf52012-02-16 23:53:59 -0800452 final Rect r = mDragOutlines[i];
Joe Onorato4be866d2010-10-10 11:26:02 -0700453 final Bitmap b = (Bitmap) mDragOutlineAnims[i].getTag();
Chet Haase472b2812010-10-14 07:02:04 -0700454 paint.setAlpha((int)(alpha + .5f));
Adam Cohend41fbf52012-02-16 23:53:59 -0800455 canvas.drawBitmap(b, null, r, paint);
Winson Chung150fbab2010-09-29 17:14:26 -0700456 }
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700457 }
Patrick Dubroy96864c32011-03-10 17:17:23 -0800458
459 // We draw the pressed or focused BubbleTextView's background in CellLayout because it
460 // requires an expanded clip rect (due to the glow's blur radius)
461 if (mPressedOrFocusedIcon != null) {
462 final int padding = mPressedOrFocusedIcon.getPressedOrFocusedBackgroundPadding();
463 final Bitmap b = mPressedOrFocusedIcon.getPressedOrFocusedBackground();
464 if (b != null) {
465 canvas.drawBitmap(b,
Winson Chung4b825dcd2011-06-19 12:41:22 -0700466 mPressedOrFocusedIcon.getLeft() + getPaddingLeft() - padding,
467 mPressedOrFocusedIcon.getTop() + getPaddingTop() - padding,
Patrick Dubroy96864c32011-03-10 17:17:23 -0800468 null);
469 }
470 }
Adam Cohen69ce2e52011-07-03 19:25:21 -0700471
Adam Cohen482ed822012-03-02 14:15:13 -0800472 if (DEBUG_VISUALIZE_OCCUPIED) {
473 int[] pt = new int[2];
474 ColorDrawable cd = new ColorDrawable(Color.RED);
475 cd.setBounds(0, 0, 80, 80);
476 for (int i = 0; i < mCountX; i++) {
477 for (int j = 0; j < mCountY; j++) {
478 if (mOccupied[i][j]) {
479 cellToPoint(i, j, pt);
480 canvas.save();
481 canvas.translate(pt[0], pt[1]);
482 cd.draw(canvas);
483 canvas.restore();
484 }
485 }
486 }
487 }
488
Adam Cohen69ce2e52011-07-03 19:25:21 -0700489 // The folder outer / inner ring image(s)
490 for (int i = 0; i < mFolderOuterRings.size(); i++) {
491 FolderRingAnimator fra = mFolderOuterRings.get(i);
492
493 // Draw outer ring
494 Drawable d = FolderRingAnimator.sSharedOuterRingDrawable;
495 int width = (int) fra.getOuterRingSize();
496 int height = width;
497 cellToPoint(fra.mCellX, fra.mCellY, mTempLocation);
498
499 int centerX = mTempLocation[0] + mCellWidth / 2;
500 int centerY = mTempLocation[1] + FolderRingAnimator.sPreviewSize / 2;
501
502 canvas.save();
503 canvas.translate(centerX - width / 2, centerY - height / 2);
504 d.setBounds(0, 0, width, height);
505 d.draw(canvas);
506 canvas.restore();
507
508 // Draw inner ring
509 d = FolderRingAnimator.sSharedInnerRingDrawable;
510 width = (int) fra.getInnerRingSize();
511 height = width;
512 cellToPoint(fra.mCellX, fra.mCellY, mTempLocation);
513
514 centerX = mTempLocation[0] + mCellWidth / 2;
515 centerY = mTempLocation[1] + FolderRingAnimator.sPreviewSize / 2;
516 canvas.save();
517 canvas.translate(centerX - width / 2, centerY - width / 2);
518 d.setBounds(0, 0, width, height);
519 d.draw(canvas);
520 canvas.restore();
521 }
Adam Cohenc51934b2011-07-26 21:07:43 -0700522
523 if (mFolderLeaveBehindCell[0] >= 0 && mFolderLeaveBehindCell[1] >= 0) {
524 Drawable d = FolderIcon.sSharedFolderLeaveBehind;
525 int width = d.getIntrinsicWidth();
526 int height = d.getIntrinsicHeight();
527
528 cellToPoint(mFolderLeaveBehindCell[0], mFolderLeaveBehindCell[1], mTempLocation);
529 int centerX = mTempLocation[0] + mCellWidth / 2;
530 int centerY = mTempLocation[1] + FolderRingAnimator.sPreviewSize / 2;
531
532 canvas.save();
533 canvas.translate(centerX - width / 2, centerY - width / 2);
534 d.setBounds(0, 0, width, height);
535 d.draw(canvas);
536 canvas.restore();
537 }
Adam Cohen69ce2e52011-07-03 19:25:21 -0700538 }
539
Adam Cohenb5ba0972011-09-07 18:02:31 -0700540 @Override
541 protected void dispatchDraw(Canvas canvas) {
542 super.dispatchDraw(canvas);
543 if (mForegroundAlpha > 0) {
544 mOverScrollForegroundDrawable.setBounds(mForegroundRect);
545 Paint p = ((NinePatchDrawable) mOverScrollForegroundDrawable).getPaint();
546 p.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.ADD));
547 mOverScrollForegroundDrawable.draw(canvas);
548 p.setXfermode(null);
549 }
550 }
551
Adam Cohen69ce2e52011-07-03 19:25:21 -0700552 public void showFolderAccept(FolderRingAnimator fra) {
553 mFolderOuterRings.add(fra);
554 }
555
556 public void hideFolderAccept(FolderRingAnimator fra) {
557 if (mFolderOuterRings.contains(fra)) {
558 mFolderOuterRings.remove(fra);
559 }
560 invalidate();
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700561 }
562
Adam Cohenc51934b2011-07-26 21:07:43 -0700563 public void setFolderLeaveBehindCell(int x, int y) {
564 mFolderLeaveBehindCell[0] = x;
565 mFolderLeaveBehindCell[1] = y;
566 invalidate();
567 }
568
569 public void clearFolderLeaveBehind() {
570 mFolderLeaveBehindCell[0] = -1;
571 mFolderLeaveBehindCell[1] = -1;
572 invalidate();
573 }
574
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700575 @Override
Michael Jurkae6235dd2011-10-04 15:02:05 -0700576 public boolean shouldDelayChildPressedState() {
577 return false;
578 }
579
580 @Override
Jeff Sharkey83f111d2009-04-20 21:03:13 -0700581 public void cancelLongPress() {
582 super.cancelLongPress();
583
584 // Cancel long press for all children
585 final int count = getChildCount();
586 for (int i = 0; i < count; i++) {
587 final View child = getChildAt(i);
588 child.cancelLongPress();
589 }
590 }
591
Michael Jurkadee05892010-07-27 10:01:56 -0700592 public void setOnInterceptTouchListener(View.OnTouchListener listener) {
593 mInterceptTouchListener = listener;
594 }
595
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800596 int getCountX() {
Adam Cohend22015c2010-07-26 22:02:18 -0700597 return mCountX;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800598 }
599
600 int getCountY() {
Adam Cohend22015c2010-07-26 22:02:18 -0700601 return mCountY;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800602 }
603
Andrew Flynn0dca1ec2012-02-29 13:33:22 -0800604 public void setIsHotseat(boolean isHotseat) {
605 mIsHotseat = isHotseat;
606 }
607
Winson Chungeecf02d2012-03-02 17:14:58 -0800608 public float getChildrenScale() {
609 return mIsHotseat ? mHotseatChildScale : mChildScale;
610 }
611
Michael Jurkaf3ca3ab2010-10-20 17:08:24 -0700612 public boolean addViewToCellLayout(
613 View child, int index, int childId, LayoutParams params, boolean markCells) {
Andrew Flynn0dca1ec2012-02-29 13:33:22 -0800614 return addViewToCellLayout(child, index, childId, params, markCells, false);
615 }
616
Winson Chungeecf02d2012-03-02 17:14:58 -0800617 private void scaleChild(BubbleTextView bubbleChild, float pivot, float scale) {
Andrew Flynnbc239a12012-03-06 11:39:49 -0800618 // If we haven't measured the child yet, do it now
619 // (this happens if we're being dropped from all-apps
620 if (bubbleChild.getLayoutParams() instanceof LayoutParams &&
621 (bubbleChild.getMeasuredWidth() | bubbleChild.getMeasuredHeight()) == 0) {
Michael Jurkaa52570f2012-03-20 03:18:20 -0700622 getShortcutsAndWidgets().measureChild(bubbleChild);
Andrew Flynnbc239a12012-03-06 11:39:49 -0800623 }
Andrew Flynnbc239a12012-03-06 11:39:49 -0800624
Andrew Flynnbc239a12012-03-06 11:39:49 -0800625 bubbleChild.setScaleX(scale);
626 bubbleChild.setScaleY(scale);
Andrew Flynnbc239a12012-03-06 11:39:49 -0800627 }
628
629 private void resetChild(BubbleTextView bubbleChild) {
630 bubbleChild.setScaleX(1f);
631 bubbleChild.setScaleY(1f);
Andrew Flynnbc239a12012-03-06 11:39:49 -0800632
633 bubbleChild.setTextColor(getResources().getColor(R.color.workspace_icon_text_color));
634 }
635
Andrew Flynn0dca1ec2012-02-29 13:33:22 -0800636 public boolean addViewToCellLayout(View child, int index, int childId, LayoutParams params,
637 boolean markCells, boolean allApps) {
Winson Chungaafa03c2010-06-11 17:34:16 -0700638 final LayoutParams lp = params;
639
Andrew Flynn0dca1ec2012-02-29 13:33:22 -0800640 // Hotseat icons - scale down and remove text
641 // Don't scale the all apps button
642 // scale percent set to -1 means do not scale
643 // Only scale BubbleTextViews
644 if (child instanceof BubbleTextView) {
645 BubbleTextView bubbleChild = (BubbleTextView) child;
646
Andrew Flynnbc239a12012-03-06 11:39:49 -0800647 // Start the child with 100% scale and visible text
648 resetChild(bubbleChild);
Andrew Flynn0dca1ec2012-02-29 13:33:22 -0800649
Winson Chungeecf02d2012-03-02 17:14:58 -0800650 if (mIsHotseat && !allApps && mHotseatChildScale >= 0) {
Andrew Flynnbc239a12012-03-06 11:39:49 -0800651 // Scale/make transparent for a hotseat
Winson Chungeecf02d2012-03-02 17:14:58 -0800652 scaleChild(bubbleChild, 0f, mHotseatChildScale);
Andrew Flynn0dca1ec2012-02-29 13:33:22 -0800653
Andrew Flynnbc239a12012-03-06 11:39:49 -0800654 bubbleChild.setTextColor(getResources().getColor(android.R.color.transparent));
Winson Chungeecf02d2012-03-02 17:14:58 -0800655 } else if (mChildScale >= 0) {
Andrew Flynnbc239a12012-03-06 11:39:49 -0800656 // Else possibly still scale it if we need to for smaller icons
Winson Chungeecf02d2012-03-02 17:14:58 -0800657 scaleChild(bubbleChild, 0f, mChildScale);
Andrew Flynn0dca1ec2012-02-29 13:33:22 -0800658 }
659 }
660
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800661 // Generate an id for each view, this assumes we have at most 256x256 cells
662 // per workspace screen
Adam Cohend22015c2010-07-26 22:02:18 -0700663 if (lp.cellX >= 0 && lp.cellX <= mCountX - 1 && lp.cellY >= 0 && lp.cellY <= mCountY - 1) {
Winson Chungaafa03c2010-06-11 17:34:16 -0700664 // If the horizontal or vertical span is set to -1, it is taken to
665 // mean that it spans the extent of the CellLayout
Adam Cohend22015c2010-07-26 22:02:18 -0700666 if (lp.cellHSpan < 0) lp.cellHSpan = mCountX;
667 if (lp.cellVSpan < 0) lp.cellVSpan = mCountY;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800668
Winson Chungaafa03c2010-06-11 17:34:16 -0700669 child.setId(childId);
670
Michael Jurkaa52570f2012-03-20 03:18:20 -0700671 mShortcutsAndWidgets.addView(child, index, lp);
Michael Jurkadee05892010-07-27 10:01:56 -0700672
Michael Jurkaf3ca3ab2010-10-20 17:08:24 -0700673 if (markCells) markCellsAsOccupiedForView(child);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700674
Winson Chungaafa03c2010-06-11 17:34:16 -0700675 return true;
676 }
677 return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800678 }
Michael Jurka3e7c7632010-10-02 16:01:03 -0700679
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800680 @Override
Michael Jurka0280c3b2010-09-17 15:00:07 -0700681 public void removeAllViews() {
682 clearOccupiedCells();
Michael Jurkaa52570f2012-03-20 03:18:20 -0700683 mShortcutsAndWidgets.removeAllViews();
Michael Jurka0280c3b2010-09-17 15:00:07 -0700684 }
685
686 @Override
687 public void removeAllViewsInLayout() {
Michael Jurkaa52570f2012-03-20 03:18:20 -0700688 if (mShortcutsAndWidgets.getChildCount() > 0) {
Michael Jurka7cfc2822011-08-02 20:19:24 -0700689 clearOccupiedCells();
Michael Jurkaa52570f2012-03-20 03:18:20 -0700690 mShortcutsAndWidgets.removeAllViewsInLayout();
Michael Jurka7cfc2822011-08-02 20:19:24 -0700691 }
Michael Jurka0280c3b2010-09-17 15:00:07 -0700692 }
693
Michael Jurkaf3ca3ab2010-10-20 17:08:24 -0700694 public void removeViewWithoutMarkingCells(View view) {
Michael Jurkaa52570f2012-03-20 03:18:20 -0700695 mShortcutsAndWidgets.removeView(view);
Michael Jurkaf3ca3ab2010-10-20 17:08:24 -0700696 }
697
Michael Jurka0280c3b2010-09-17 15:00:07 -0700698 @Override
699 public void removeView(View view) {
700 markCellsAsUnoccupiedForView(view);
Michael Jurkaa52570f2012-03-20 03:18:20 -0700701 mShortcutsAndWidgets.removeView(view);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700702 }
703
704 @Override
705 public void removeViewAt(int index) {
Michael Jurkaa52570f2012-03-20 03:18:20 -0700706 markCellsAsUnoccupiedForView(mShortcutsAndWidgets.getChildAt(index));
707 mShortcutsAndWidgets.removeViewAt(index);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700708 }
709
710 @Override
711 public void removeViewInLayout(View view) {
712 markCellsAsUnoccupiedForView(view);
Michael Jurkaa52570f2012-03-20 03:18:20 -0700713 mShortcutsAndWidgets.removeViewInLayout(view);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700714 }
715
716 @Override
717 public void removeViews(int start, int count) {
718 for (int i = start; i < start + count; i++) {
Michael Jurkaa52570f2012-03-20 03:18:20 -0700719 markCellsAsUnoccupiedForView(mShortcutsAndWidgets.getChildAt(i));
Michael Jurka0280c3b2010-09-17 15:00:07 -0700720 }
Michael Jurkaa52570f2012-03-20 03:18:20 -0700721 mShortcutsAndWidgets.removeViews(start, count);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700722 }
723
724 @Override
725 public void removeViewsInLayout(int start, int count) {
726 for (int i = start; i < start + count; i++) {
Michael Jurkaa52570f2012-03-20 03:18:20 -0700727 markCellsAsUnoccupiedForView(mShortcutsAndWidgets.getChildAt(i));
Michael Jurka0280c3b2010-09-17 15:00:07 -0700728 }
Michael Jurkaa52570f2012-03-20 03:18:20 -0700729 mShortcutsAndWidgets.removeViewsInLayout(start, count);
Michael Jurkaabded662011-03-04 12:06:57 -0800730 }
731
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800732 @Override
733 protected void onAttachedToWindow() {
734 super.onAttachedToWindow();
735 mCellInfo.screen = ((ViewGroup) getParent()).indexOfChild(this);
736 }
737
Michael Jurkaaf442092010-06-10 17:01:57 -0700738 public void setTagToCellInfoForPoint(int touchX, int touchY) {
739 final CellInfo cellInfo = mCellInfo;
Winson Chungeecf02d2012-03-02 17:14:58 -0800740 Rect frame = mRect;
Michael Jurkaaf442092010-06-10 17:01:57 -0700741 final int x = touchX + mScrollX;
742 final int y = touchY + mScrollY;
Michael Jurkaa52570f2012-03-20 03:18:20 -0700743 final int count = mShortcutsAndWidgets.getChildCount();
Michael Jurkaaf442092010-06-10 17:01:57 -0700744
745 boolean found = false;
746 for (int i = count - 1; i >= 0; i--) {
Michael Jurkaa52570f2012-03-20 03:18:20 -0700747 final View child = mShortcutsAndWidgets.getChildAt(i);
Adam Cohend4844c32011-02-18 19:25:06 -0800748 final LayoutParams lp = (LayoutParams) child.getLayoutParams();
Michael Jurkaaf442092010-06-10 17:01:57 -0700749
Adam Cohen1b607ed2011-03-03 17:26:50 -0800750 if ((child.getVisibility() == VISIBLE || child.getAnimation() != null) &&
751 lp.isLockedToGrid) {
Michael Jurkaaf442092010-06-10 17:01:57 -0700752 child.getHitRect(frame);
Winson Chung0be025d2011-05-23 17:45:09 -0700753
Winson Chungeecf02d2012-03-02 17:14:58 -0800754 float scale = child.getScaleX();
755 frame = new Rect(child.getLeft(), child.getTop(), child.getRight(),
756 child.getBottom());
Winson Chung0be025d2011-05-23 17:45:09 -0700757 // The child hit rect is relative to the CellLayoutChildren parent, so we need to
758 // offset that by this CellLayout's padding to test an (x,y) point that is relative
759 // to this view.
Winson Chung4b825dcd2011-06-19 12:41:22 -0700760 frame.offset(mPaddingLeft, mPaddingTop);
Winson Chungeecf02d2012-03-02 17:14:58 -0800761 frame.inset((int) (frame.width() * (1f - scale) / 2),
762 (int) (frame.height() * (1f - scale) / 2));
Winson Chung0be025d2011-05-23 17:45:09 -0700763
Michael Jurkaaf442092010-06-10 17:01:57 -0700764 if (frame.contains(x, y)) {
Michael Jurkaaf442092010-06-10 17:01:57 -0700765 cellInfo.cell = child;
766 cellInfo.cellX = lp.cellX;
767 cellInfo.cellY = lp.cellY;
768 cellInfo.spanX = lp.cellHSpan;
769 cellInfo.spanY = lp.cellVSpan;
Michael Jurkaaf442092010-06-10 17:01:57 -0700770 found = true;
Michael Jurkaaf442092010-06-10 17:01:57 -0700771 break;
772 }
773 }
774 }
Winson Chungaafa03c2010-06-11 17:34:16 -0700775
Michael Jurkad771c962011-08-09 15:00:48 -0700776 mLastDownOnOccupiedCell = found;
777
Michael Jurkaaf442092010-06-10 17:01:57 -0700778 if (!found) {
Winson Chung0be025d2011-05-23 17:45:09 -0700779 final int cellXY[] = mTmpXY;
Michael Jurkaaf442092010-06-10 17:01:57 -0700780 pointToCellExact(x, y, cellXY);
781
Michael Jurkaaf442092010-06-10 17:01:57 -0700782 cellInfo.cell = null;
783 cellInfo.cellX = cellXY[0];
784 cellInfo.cellY = cellXY[1];
785 cellInfo.spanX = 1;
786 cellInfo.spanY = 1;
Michael Jurkaaf442092010-06-10 17:01:57 -0700787 }
788 setTag(cellInfo);
789 }
790
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800791 @Override
792 public boolean onInterceptTouchEvent(MotionEvent ev) {
Adam Cohenc1997fd2011-08-15 18:26:39 -0700793 // First we clear the tag to ensure that on every touch down we start with a fresh slate,
794 // even in the case where we return early. Not clearing here was causing bugs whereby on
795 // long-press we'd end up picking up an item from a previous drag operation.
796 final int action = ev.getAction();
797
798 if (action == MotionEvent.ACTION_DOWN) {
799 clearTagCellInfo();
800 }
801
Michael Jurkadee05892010-07-27 10:01:56 -0700802 if (mInterceptTouchListener != null && mInterceptTouchListener.onTouch(this, ev)) {
803 return true;
804 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800805
806 if (action == MotionEvent.ACTION_DOWN) {
Michael Jurkaaf442092010-06-10 17:01:57 -0700807 setTagToCellInfoForPoint((int) ev.getX(), (int) ev.getY());
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800808 }
Winson Chungeecf02d2012-03-02 17:14:58 -0800809
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800810 return false;
811 }
812
Adam Cohenc1997fd2011-08-15 18:26:39 -0700813 private void clearTagCellInfo() {
814 final CellInfo cellInfo = mCellInfo;
815 cellInfo.cell = null;
816 cellInfo.cellX = -1;
817 cellInfo.cellY = -1;
818 cellInfo.spanX = 0;
819 cellInfo.spanY = 0;
820 setTag(cellInfo);
821 }
822
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800823 public CellInfo getTag() {
Michael Jurka0280c3b2010-09-17 15:00:07 -0700824 return (CellInfo) super.getTag();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800825 }
826
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700827 /**
Winson Chungaafa03c2010-06-11 17:34:16 -0700828 * Given a point, return the cell that strictly encloses that point
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800829 * @param x X coordinate of the point
830 * @param y Y coordinate of the point
831 * @param result Array of 2 ints to hold the x and y coordinate of the cell
832 */
833 void pointToCellExact(int x, int y, int[] result) {
Winson Chung4b825dcd2011-06-19 12:41:22 -0700834 final int hStartPadding = getPaddingLeft();
835 final int vStartPadding = getPaddingTop();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800836
837 result[0] = (x - hStartPadding) / (mCellWidth + mWidthGap);
838 result[1] = (y - vStartPadding) / (mCellHeight + mHeightGap);
839
Adam Cohend22015c2010-07-26 22:02:18 -0700840 final int xAxis = mCountX;
841 final int yAxis = mCountY;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800842
843 if (result[0] < 0) result[0] = 0;
844 if (result[0] >= xAxis) result[0] = xAxis - 1;
845 if (result[1] < 0) result[1] = 0;
846 if (result[1] >= yAxis) result[1] = yAxis - 1;
847 }
Winson Chungaafa03c2010-06-11 17:34:16 -0700848
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800849 /**
850 * Given a point, return the cell that most closely encloses that point
851 * @param x X coordinate of the point
852 * @param y Y coordinate of the point
853 * @param result Array of 2 ints to hold the x and y coordinate of the cell
854 */
855 void pointToCellRounded(int x, int y, int[] result) {
856 pointToCellExact(x + (mCellWidth / 2), y + (mCellHeight / 2), result);
857 }
858
859 /**
860 * Given a cell coordinate, return the point that represents the upper left corner of that cell
Winson Chungaafa03c2010-06-11 17:34:16 -0700861 *
862 * @param cellX X coordinate of the cell
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800863 * @param cellY Y coordinate of the cell
Winson Chungaafa03c2010-06-11 17:34:16 -0700864 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800865 * @param result Array of 2 ints to hold the x and y coordinate of the point
866 */
867 void cellToPoint(int cellX, int cellY, int[] result) {
Winson Chung4b825dcd2011-06-19 12:41:22 -0700868 final int hStartPadding = getPaddingLeft();
869 final int vStartPadding = getPaddingTop();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800870
871 result[0] = hStartPadding + cellX * (mCellWidth + mWidthGap);
872 result[1] = vStartPadding + cellY * (mCellHeight + mHeightGap);
873 }
874
Adam Cohene3e27a82011-04-15 12:07:39 -0700875 /**
Adam Cohen482ed822012-03-02 14:15:13 -0800876 * Given a cell coordinate, return the point that represents the center of the cell
Adam Cohene3e27a82011-04-15 12:07:39 -0700877 *
878 * @param cellX X coordinate of the cell
879 * @param cellY Y coordinate of the cell
880 *
881 * @param result Array of 2 ints to hold the x and y coordinate of the point
882 */
883 void cellToCenterPoint(int cellX, int cellY, int[] result) {
Adam Cohen47a876d2012-03-19 13:21:41 -0700884 regionToCenterPoint(cellX, cellY, 1, 1, result);
885 }
886
887 /**
888 * Given a cell coordinate and span return the point that represents the center of the regio
889 *
890 * @param cellX X coordinate of the cell
891 * @param cellY Y coordinate of the cell
892 *
893 * @param result Array of 2 ints to hold the x and y coordinate of the point
894 */
895 void regionToCenterPoint(int cellX, int cellY, int spanX, int spanY, int[] result) {
Winson Chung4b825dcd2011-06-19 12:41:22 -0700896 final int hStartPadding = getPaddingLeft();
897 final int vStartPadding = getPaddingTop();
Adam Cohen47a876d2012-03-19 13:21:41 -0700898 result[0] = hStartPadding + cellX * (mCellWidth + mWidthGap) +
899 (spanX * mCellWidth + (spanX - 1) * mWidthGap) / 2;
900 result[1] = vStartPadding + cellY * (mCellHeight + mHeightGap) +
901 (spanY * mCellHeight + (spanY - 1) * mHeightGap) / 2;
Adam Cohene3e27a82011-04-15 12:07:39 -0700902 }
903
Adam Cohen19f37922012-03-21 11:59:11 -0700904 /**
905 * Given a cell coordinate and span fills out a corresponding pixel rect
906 *
907 * @param cellX X coordinate of the cell
908 * @param cellY Y coordinate of the cell
909 * @param result Rect in which to write the result
910 */
911 void regionToRect(int cellX, int cellY, int spanX, int spanY, Rect result) {
912 final int hStartPadding = getPaddingLeft();
913 final int vStartPadding = getPaddingTop();
914 final int left = hStartPadding + cellX * (mCellWidth + mWidthGap);
915 final int top = vStartPadding + cellY * (mCellHeight + mHeightGap);
916 result.set(left, top, left + (spanX * mCellWidth + (spanX - 1) * mWidthGap),
917 top + (spanY * mCellHeight + (spanY - 1) * mHeightGap));
918 }
919
Adam Cohen482ed822012-03-02 14:15:13 -0800920 public float getDistanceFromCell(float x, float y, int[] cell) {
921 cellToCenterPoint(cell[0], cell[1], mTmpPoint);
922 float distance = (float) Math.sqrt( Math.pow(x - mTmpPoint[0], 2) +
923 Math.pow(y - mTmpPoint[1], 2));
924 return distance;
925 }
926
Romain Guy84f296c2009-11-04 15:00:44 -0800927 int getCellWidth() {
928 return mCellWidth;
929 }
930
931 int getCellHeight() {
932 return mCellHeight;
933 }
934
Adam Cohend4844c32011-02-18 19:25:06 -0800935 int getWidthGap() {
936 return mWidthGap;
937 }
938
939 int getHeightGap() {
940 return mHeightGap;
941 }
942
Adam Cohen7f4eabe2011-04-21 16:19:16 -0700943 Rect getContentRect(Rect r) {
944 if (r == null) {
945 r = new Rect();
946 }
947 int left = getPaddingLeft();
948 int top = getPaddingTop();
Winson Chung4b825dcd2011-06-19 12:41:22 -0700949 int right = left + getWidth() - mPaddingLeft - mPaddingRight;
950 int bottom = top + getHeight() - mPaddingTop - mPaddingBottom;
Adam Cohen7f4eabe2011-04-21 16:19:16 -0700951 r.set(left, top, right, bottom);
952 return r;
953 }
954
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800955 @Override
956 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
957 // TODO: currently ignoring padding
Winson Chungaafa03c2010-06-11 17:34:16 -0700958
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800959 int widthSpecMode = MeasureSpec.getMode(widthMeasureSpec);
Winson Chungaafa03c2010-06-11 17:34:16 -0700960 int widthSpecSize = MeasureSpec.getSize(widthMeasureSpec);
961
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800962 int heightSpecMode = MeasureSpec.getMode(heightMeasureSpec);
963 int heightSpecSize = MeasureSpec.getSize(heightMeasureSpec);
Winson Chungaafa03c2010-06-11 17:34:16 -0700964
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800965 if (widthSpecMode == MeasureSpec.UNSPECIFIED || heightSpecMode == MeasureSpec.UNSPECIFIED) {
966 throw new RuntimeException("CellLayout cannot have UNSPECIFIED dimensions");
967 }
968
Adam Cohend22015c2010-07-26 22:02:18 -0700969 int numWidthGaps = mCountX - 1;
970 int numHeightGaps = mCountY - 1;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800971
Adam Cohen234c4cd2011-07-17 21:03:04 -0700972 if (mOriginalWidthGap < 0 || mOriginalHeightGap < 0) {
Winson Chung4b825dcd2011-06-19 12:41:22 -0700973 int hSpace = widthSpecSize - mPaddingLeft - mPaddingRight;
974 int vSpace = heightSpecSize - mPaddingTop - mPaddingBottom;
975 int hFreeSpace = hSpace - (mCountX * mOriginalCellWidth);
976 int vFreeSpace = vSpace - (mCountY * mOriginalCellHeight);
977 mWidthGap = Math.min(mMaxGap, numWidthGaps > 0 ? (hFreeSpace / numWidthGaps) : 0);
978 mHeightGap = Math.min(mMaxGap,numHeightGaps > 0 ? (vFreeSpace / numHeightGaps) : 0);
Michael Jurkaa52570f2012-03-20 03:18:20 -0700979 mShortcutsAndWidgets.setCellDimensions(mCellWidth, mCellHeight, mWidthGap, mHeightGap);
Adam Cohen234c4cd2011-07-17 21:03:04 -0700980 } else {
981 mWidthGap = mOriginalWidthGap;
982 mHeightGap = mOriginalHeightGap;
Winson Chungece7f5b2010-10-22 14:54:12 -0700983 }
Michael Jurka5f1c5092010-09-03 14:15:02 -0700984
Michael Jurka8c920dd2011-01-20 14:16:56 -0800985 // Initial values correspond to widthSpecMode == MeasureSpec.EXACTLY
986 int newWidth = widthSpecSize;
987 int newHeight = heightSpecSize;
Michael Jurka5f1c5092010-09-03 14:15:02 -0700988 if (widthSpecMode == MeasureSpec.AT_MOST) {
Winson Chung4b825dcd2011-06-19 12:41:22 -0700989 newWidth = mPaddingLeft + mPaddingRight + (mCountX * mCellWidth) +
Winson Chungece7f5b2010-10-22 14:54:12 -0700990 ((mCountX - 1) * mWidthGap);
Winson Chung4b825dcd2011-06-19 12:41:22 -0700991 newHeight = mPaddingTop + mPaddingBottom + (mCountY * mCellHeight) +
Winson Chungece7f5b2010-10-22 14:54:12 -0700992 ((mCountY - 1) * mHeightGap);
Michael Jurka5f1c5092010-09-03 14:15:02 -0700993 setMeasuredDimension(newWidth, newHeight);
Michael Jurka5f1c5092010-09-03 14:15:02 -0700994 }
Michael Jurka8c920dd2011-01-20 14:16:56 -0800995
996 int count = getChildCount();
997 for (int i = 0; i < count; i++) {
998 View child = getChildAt(i);
Winson Chung4b825dcd2011-06-19 12:41:22 -0700999 int childWidthMeasureSpec = MeasureSpec.makeMeasureSpec(newWidth - mPaddingLeft -
1000 mPaddingRight, MeasureSpec.EXACTLY);
1001 int childheightMeasureSpec = MeasureSpec.makeMeasureSpec(newHeight - mPaddingTop -
1002 mPaddingBottom, MeasureSpec.EXACTLY);
Michael Jurka8c920dd2011-01-20 14:16:56 -08001003 child.measure(childWidthMeasureSpec, childheightMeasureSpec);
1004 }
1005 setMeasuredDimension(newWidth, newHeight);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001006 }
1007
1008 @Override
Michael Jurka28750fb2010-09-24 17:43:49 -07001009 protected void onLayout(boolean changed, int l, int t, int r, int b) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001010 int count = getChildCount();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001011 for (int i = 0; i < count; i++) {
Michael Jurka8c920dd2011-01-20 14:16:56 -08001012 View child = getChildAt(i);
Winson Chung4b825dcd2011-06-19 12:41:22 -07001013 child.layout(mPaddingLeft, mPaddingTop,
1014 r - l - mPaddingRight, b - t - mPaddingBottom);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001015 }
1016 }
1017
1018 @Override
Michael Jurkadee05892010-07-27 10:01:56 -07001019 protected void onSizeChanged(int w, int h, int oldw, int oldh) {
1020 super.onSizeChanged(w, h, oldw, oldh);
Michael Jurka18014792010-10-14 09:01:34 -07001021 mBackgroundRect.set(0, 0, w, h);
Adam Cohenb5ba0972011-09-07 18:02:31 -07001022 mForegroundRect.set(mForegroundPadding, mForegroundPadding,
1023 w - 2 * mForegroundPadding, h - 2 * mForegroundPadding);
Michael Jurkadee05892010-07-27 10:01:56 -07001024 }
1025
1026 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001027 protected void setChildrenDrawingCacheEnabled(boolean enabled) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07001028 mShortcutsAndWidgets.setChildrenDrawingCacheEnabled(enabled);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001029 }
1030
1031 @Override
1032 protected void setChildrenDrawnWithCacheEnabled(boolean enabled) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07001033 mShortcutsAndWidgets.setChildrenDrawnWithCacheEnabled(enabled);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001034 }
1035
Michael Jurka5f1c5092010-09-03 14:15:02 -07001036 public float getBackgroundAlpha() {
1037 return mBackgroundAlpha;
Michael Jurkadee05892010-07-27 10:01:56 -07001038 }
1039
Adam Cohen1b0aaac2010-10-28 11:11:18 -07001040 public void setBackgroundAlphaMultiplier(float multiplier) {
1041 mBackgroundAlphaMultiplier = multiplier;
1042 }
1043
Adam Cohenddb82192010-11-10 16:32:54 -08001044 public float getBackgroundAlphaMultiplier() {
1045 return mBackgroundAlphaMultiplier;
1046 }
1047
Michael Jurka5f1c5092010-09-03 14:15:02 -07001048 public void setBackgroundAlpha(float alpha) {
Michael Jurkaafaa0502011-12-13 18:22:50 -08001049 if (mBackgroundAlpha != alpha) {
1050 mBackgroundAlpha = alpha;
1051 invalidate();
1052 }
Michael Jurkadee05892010-07-27 10:01:56 -07001053 }
1054
Michael Jurkaa52570f2012-03-20 03:18:20 -07001055 public void setShortcutAndWidgetAlpha(float alpha) {
Michael Jurka0142d492010-08-25 17:46:15 -07001056 final int childCount = getChildCount();
1057 for (int i = 0; i < childCount; i++) {
Michael Jurkadee05892010-07-27 10:01:56 -07001058 getChildAt(i).setAlpha(alpha);
1059 }
1060 }
1061
Michael Jurkaa52570f2012-03-20 03:18:20 -07001062 public ShortcutAndWidgetContainer getShortcutsAndWidgets() {
1063 if (getChildCount() > 0) {
1064 return (ShortcutAndWidgetContainer) getChildAt(0);
1065 }
1066 return null;
1067 }
1068
Patrick Dubroy440c3602010-07-13 17:50:32 -07001069 public View getChildAt(int x, int y) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07001070 return mShortcutsAndWidgets.getChildAt(x, y);
Patrick Dubroy440c3602010-07-13 17:50:32 -07001071 }
1072
Adam Cohen76fc0852011-06-17 13:26:23 -07001073 public boolean animateChildToPosition(final View child, int cellX, int cellY, int duration,
Adam Cohen482ed822012-03-02 14:15:13 -08001074 int delay, boolean permanent, boolean adjustOccupied) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07001075 ShortcutAndWidgetContainer clc = getShortcutsAndWidgets();
Adam Cohen482ed822012-03-02 14:15:13 -08001076 boolean[][] occupied = mOccupied;
1077 if (!permanent) {
1078 occupied = mTmpOccupied;
1079 }
1080
Adam Cohen19f37922012-03-21 11:59:11 -07001081 if (clc.indexOfChild(child) != -1) {
Adam Cohenbfbfd262011-06-13 16:55:12 -07001082 final LayoutParams lp = (LayoutParams) child.getLayoutParams();
1083 final ItemInfo info = (ItemInfo) child.getTag();
1084
1085 // We cancel any existing animations
1086 if (mReorderAnimators.containsKey(lp)) {
1087 mReorderAnimators.get(lp).cancel();
1088 mReorderAnimators.remove(lp);
1089 }
1090
Adam Cohen482ed822012-03-02 14:15:13 -08001091 final int oldX = lp.x;
1092 final int oldY = lp.y;
1093 if (adjustOccupied) {
1094 occupied[lp.cellX][lp.cellY] = false;
1095 occupied[cellX][cellY] = true;
1096 }
Adam Cohenbfbfd262011-06-13 16:55:12 -07001097 lp.isLockedToGrid = true;
Adam Cohen482ed822012-03-02 14:15:13 -08001098 if (permanent) {
1099 lp.cellX = info.cellX = cellX;
1100 lp.cellY = info.cellY = cellY;
1101 } else {
1102 lp.tmpCellX = cellX;
1103 lp.tmpCellY = cellY;
1104 }
Adam Cohenbfbfd262011-06-13 16:55:12 -07001105 clc.setupLp(lp);
1106 lp.isLockedToGrid = false;
Adam Cohen482ed822012-03-02 14:15:13 -08001107 final int newX = lp.x;
1108 final int newY = lp.y;
Adam Cohenbfbfd262011-06-13 16:55:12 -07001109
Adam Cohen76fc0852011-06-17 13:26:23 -07001110 lp.x = oldX;
1111 lp.y = oldY;
Adam Cohen76fc0852011-06-17 13:26:23 -07001112
Adam Cohen482ed822012-03-02 14:15:13 -08001113 // Exit early if we're not actually moving the view
1114 if (oldX == newX && oldY == newY) {
1115 lp.isLockedToGrid = true;
1116 return true;
1117 }
1118
1119 ValueAnimator va = ValueAnimator.ofFloat(0f, 1f);
1120 va.setDuration(duration);
1121 mReorderAnimators.put(lp, va);
1122
1123 va.addUpdateListener(new AnimatorUpdateListener() {
1124 @Override
Adam Cohenbfbfd262011-06-13 16:55:12 -07001125 public void onAnimationUpdate(ValueAnimator animation) {
Adam Cohen482ed822012-03-02 14:15:13 -08001126 float r = ((Float) animation.getAnimatedValue()).floatValue();
Adam Cohen19f37922012-03-21 11:59:11 -07001127 lp.x = (int) ((1 - r) * oldX + r * newX);
1128 lp.y = (int) ((1 - r) * oldY + r * newY);
Adam Cohen6b8a02d2012-03-22 15:13:40 -07001129 child.requestLayout();
Adam Cohenbfbfd262011-06-13 16:55:12 -07001130 }
1131 });
Adam Cohen482ed822012-03-02 14:15:13 -08001132 va.addListener(new AnimatorListenerAdapter() {
Adam Cohenbfbfd262011-06-13 16:55:12 -07001133 boolean cancelled = false;
1134 public void onAnimationEnd(Animator animation) {
1135 // If the animation was cancelled, it means that another animation
1136 // has interrupted this one, and we don't want to lock the item into
1137 // place just yet.
1138 if (!cancelled) {
1139 lp.isLockedToGrid = true;
Adam Cohen482ed822012-03-02 14:15:13 -08001140 child.requestLayout();
Adam Cohenbfbfd262011-06-13 16:55:12 -07001141 }
1142 if (mReorderAnimators.containsKey(lp)) {
1143 mReorderAnimators.remove(lp);
1144 }
1145 }
1146 public void onAnimationCancel(Animator animation) {
1147 cancelled = true;
1148 }
1149 });
Adam Cohen482ed822012-03-02 14:15:13 -08001150 va.setStartDelay(delay);
1151 va.start();
Adam Cohenbfbfd262011-06-13 16:55:12 -07001152 return true;
1153 }
1154 return false;
1155 }
1156
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001157 /**
1158 * Estimate where the top left cell of the dragged item will land if it is dropped.
1159 *
1160 * @param originX The X value of the top left corner of the item
1161 * @param originY The Y value of the top left corner of the item
1162 * @param spanX The number of horizontal cells that the item spans
1163 * @param spanY The number of vertical cells that the item spans
1164 * @param result The estimated drop cell X and Y.
1165 */
1166 void estimateDropCell(int originX, int originY, int spanX, int spanY, int[] result) {
Adam Cohend22015c2010-07-26 22:02:18 -07001167 final int countX = mCountX;
1168 final int countY = mCountY;
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001169
Michael Jurkaa63c4522010-08-19 13:52:27 -07001170 // pointToCellRounded takes the top left of a cell but will pad that with
1171 // cellWidth/2 and cellHeight/2 when finding the matching cell
1172 pointToCellRounded(originX, originY, result);
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001173
1174 // If the item isn't fully on this screen, snap to the edges
1175 int rightOverhang = result[0] + spanX - countX;
1176 if (rightOverhang > 0) {
1177 result[0] -= rightOverhang; // Snap to right
1178 }
1179 result[0] = Math.max(0, result[0]); // Snap to left
1180 int bottomOverhang = result[1] + spanY - countY;
1181 if (bottomOverhang > 0) {
1182 result[1] -= bottomOverhang; // Snap to bottom
1183 }
1184 result[1] = Math.max(0, result[1]); // Snap to top
1185 }
1186
Adam Cohen482ed822012-03-02 14:15:13 -08001187 void visualizeDropLocation(View v, Bitmap dragOutline, int originX, int originY, int cellX,
1188 int cellY, int spanX, int spanY, boolean resize, Point dragOffset, Rect dragRegion) {
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -07001189 final int oldDragCellX = mDragCell[0];
1190 final int oldDragCellY = mDragCell[1];
Adam Cohen482ed822012-03-02 14:15:13 -08001191
Winson Chungb8c69f32011-10-19 21:36:08 -07001192 if (v != null && dragOffset == null) {
Winson Chunga9abd0e2010-10-27 17:18:37 -07001193 mDragCenter.set(originX + (v.getWidth() / 2), originY + (v.getHeight() / 2));
1194 } else {
1195 mDragCenter.set(originX, originY);
1196 }
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001197
Adam Cohen2801caf2011-05-13 20:57:39 -07001198 if (dragOutline == null && v == null) {
1199 if (mCrosshairsDrawable != null) {
1200 invalidate();
1201 }
1202 return;
1203 }
1204
Adam Cohen482ed822012-03-02 14:15:13 -08001205 if (cellX != oldDragCellX || cellY != oldDragCellY) {
1206 mDragCell[0] = cellX;
1207 mDragCell[1] = cellY;
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001208 // Find the top left corner of the rect the object will occupy
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001209 final int[] topLeft = mTmpPoint;
Adam Cohen482ed822012-03-02 14:15:13 -08001210 cellToPoint(cellX, cellY, topLeft);
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001211
Joe Onorato4be866d2010-10-10 11:26:02 -07001212 int left = topLeft[0];
1213 int top = topLeft[1];
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001214
Winson Chungb8c69f32011-10-19 21:36:08 -07001215 if (v != null && dragOffset == null) {
Adam Cohen99e8b402011-03-25 19:23:43 -07001216 // When drawing the drag outline, it did not account for margin offsets
1217 // added by the view's parent.
1218 MarginLayoutParams lp = (MarginLayoutParams) v.getLayoutParams();
1219 left += lp.leftMargin;
1220 top += lp.topMargin;
Winson Chung150fbab2010-09-29 17:14:26 -07001221
Adam Cohen99e8b402011-03-25 19:23:43 -07001222 // Offsets due to the size difference between the View and the dragOutline.
1223 // There is a size difference to account for the outer blur, which may lie
1224 // outside the bounds of the view.
Winson Chunga9abd0e2010-10-27 17:18:37 -07001225 top += (v.getHeight() - dragOutline.getHeight()) / 2;
Adam Cohenae915ce2011-08-25 13:47:22 -07001226 // We center about the x axis
1227 left += ((mCellWidth * spanX) + ((spanX - 1) * mWidthGap)
1228 - dragOutline.getWidth()) / 2;
Adam Cohen66396872011-04-15 17:50:36 -07001229 } else {
Winson Chungb8c69f32011-10-19 21:36:08 -07001230 if (dragOffset != null && dragRegion != null) {
1231 // Center the drag region *horizontally* in the cell and apply a drag
1232 // outline offset
1233 left += dragOffset.x + ((mCellWidth * spanX) + ((spanX - 1) * mWidthGap)
1234 - dragRegion.width()) / 2;
1235 top += dragOffset.y;
1236 } else {
1237 // Center the drag outline in the cell
1238 left += ((mCellWidth * spanX) + ((spanX - 1) * mWidthGap)
1239 - dragOutline.getWidth()) / 2;
1240 top += ((mCellHeight * spanY) + ((spanY - 1) * mHeightGap)
1241 - dragOutline.getHeight()) / 2;
1242 }
Winson Chunga9abd0e2010-10-27 17:18:37 -07001243 }
Joe Onorato4be866d2010-10-10 11:26:02 -07001244 final int oldIndex = mDragOutlineCurrent;
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -07001245 mDragOutlineAnims[oldIndex].animateOut();
1246 mDragOutlineCurrent = (oldIndex + 1) % mDragOutlines.length;
Adam Cohend41fbf52012-02-16 23:53:59 -08001247 Rect r = mDragOutlines[mDragOutlineCurrent];
1248 r.set(left, top, left + dragOutline.getWidth(), top + dragOutline.getHeight());
1249 if (resize) {
Adam Cohen482ed822012-03-02 14:15:13 -08001250 cellToRect(cellX, cellY, spanX, spanY, r);
Adam Cohend41fbf52012-02-16 23:53:59 -08001251 }
Winson Chung150fbab2010-09-29 17:14:26 -07001252
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -07001253 mDragOutlineAnims[mDragOutlineCurrent].setTag(dragOutline);
1254 mDragOutlineAnims[mDragOutlineCurrent].animateIn();
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001255 }
Patrick Dubroy49250ad2010-10-08 15:33:52 -07001256
1257 // If we are drawing crosshairs, the entire CellLayout needs to be invalidated
1258 if (mCrosshairsDrawable != null) {
1259 invalidate();
1260 }
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001261 }
1262
Adam Cohene0310962011-04-18 16:15:31 -07001263 public void clearDragOutlines() {
1264 final int oldIndex = mDragOutlineCurrent;
1265 mDragOutlineAnims[oldIndex].animateOut();
Adam Cohend41fbf52012-02-16 23:53:59 -08001266 mDragCell[0] = mDragCell[1] = -1;
Adam Cohene0310962011-04-18 16:15:31 -07001267 }
1268
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001269 /**
Jeff Sharkey70864282009-04-07 21:08:40 -07001270 * Find a vacant area that will fit the given bounds nearest the requested
1271 * cell location. Uses Euclidean distance to score multiple vacant areas.
Winson Chungaafa03c2010-06-11 17:34:16 -07001272 *
Romain Guy51afc022009-05-04 18:03:43 -07001273 * @param pixelX The X location at which you want to search for a vacant area.
1274 * @param pixelY The Y location at which you want to search for a vacant area.
Jeff Sharkey70864282009-04-07 21:08:40 -07001275 * @param spanX Horizontal span of the object.
1276 * @param spanY Vertical span of the object.
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001277 * @param result Array in which to place the result, or null (in which case a new array will
1278 * be allocated)
Jeff Sharkey70864282009-04-07 21:08:40 -07001279 * @return The X, Y cell of a vacant area that can contain this object,
1280 * nearest the requested location.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001281 */
Adam Cohend41fbf52012-02-16 23:53:59 -08001282 int[] findNearestVacantArea(int pixelX, int pixelY, int spanX, int spanY,
1283 int[] result) {
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001284 return findNearestVacantArea(pixelX, pixelY, spanX, spanY, null, result);
Michael Jurka6a1435d2010-09-27 17:35:12 -07001285 }
Winson Chungaafa03c2010-06-11 17:34:16 -07001286
Michael Jurka6a1435d2010-09-27 17:35:12 -07001287 /**
1288 * Find a vacant area that will fit the given bounds nearest the requested
1289 * cell location. Uses Euclidean distance to score multiple vacant areas.
1290 *
1291 * @param pixelX The X location at which you want to search for a vacant area.
1292 * @param pixelY The Y location at which you want to search for a vacant area.
Adam Cohend41fbf52012-02-16 23:53:59 -08001293 * @param minSpanX The minimum horizontal span required
1294 * @param minSpanY The minimum vertical span required
1295 * @param spanX Horizontal span of the object.
1296 * @param spanY Vertical span of the object.
1297 * @param result Array in which to place the result, or null (in which case a new array will
1298 * be allocated)
1299 * @return The X, Y cell of a vacant area that can contain this object,
1300 * nearest the requested location.
1301 */
1302 int[] findNearestVacantArea(int pixelX, int pixelY, int minSpanX, int minSpanY, int spanX,
1303 int spanY, int[] result, int[] resultSpan) {
1304 return findNearestVacantArea(pixelX, pixelY, minSpanX, minSpanY, spanX, spanY, null,
1305 result, resultSpan);
1306 }
1307
1308 /**
1309 * Find a vacant area that will fit the given bounds nearest the requested
1310 * cell location. Uses Euclidean distance to score multiple vacant areas.
1311 *
1312 * @param pixelX The X location at which you want to search for a vacant area.
1313 * @param pixelY The Y location at which you want to search for a vacant area.
Michael Jurka6a1435d2010-09-27 17:35:12 -07001314 * @param spanX Horizontal span of the object.
1315 * @param spanY Vertical span of the object.
Adam Cohendf035382011-04-11 17:22:04 -07001316 * @param ignoreOccupied If true, the result can be an occupied cell
1317 * @param result Array in which to place the result, or null (in which case a new array will
1318 * be allocated)
Michael Jurka6a1435d2010-09-27 17:35:12 -07001319 * @return The X, Y cell of a vacant area that can contain this object,
1320 * nearest the requested location.
1321 */
Adam Cohendf035382011-04-11 17:22:04 -07001322 int[] findNearestArea(int pixelX, int pixelY, int spanX, int spanY, View ignoreView,
1323 boolean ignoreOccupied, int[] result) {
Adam Cohend41fbf52012-02-16 23:53:59 -08001324 return findNearestArea(pixelX, pixelY, spanX, spanY,
Adam Cohen482ed822012-03-02 14:15:13 -08001325 spanX, spanY, ignoreView, ignoreOccupied, result, null, mOccupied);
Adam Cohend41fbf52012-02-16 23:53:59 -08001326 }
1327
1328 private final Stack<Rect> mTempRectStack = new Stack<Rect>();
1329 private void lazyInitTempRectStack() {
1330 if (mTempRectStack.isEmpty()) {
1331 for (int i = 0; i < mCountX * mCountY; i++) {
1332 mTempRectStack.push(new Rect());
1333 }
1334 }
1335 }
Adam Cohen482ed822012-03-02 14:15:13 -08001336
Adam Cohend41fbf52012-02-16 23:53:59 -08001337 private void recycleTempRects(Stack<Rect> used) {
1338 while (!used.isEmpty()) {
1339 mTempRectStack.push(used.pop());
1340 }
1341 }
1342
1343 /**
1344 * Find a vacant area that will fit the given bounds nearest the requested
1345 * cell location. Uses Euclidean distance to score multiple vacant areas.
1346 *
1347 * @param pixelX The X location at which you want to search for a vacant area.
1348 * @param pixelY The Y location at which you want to search for a vacant area.
1349 * @param minSpanX The minimum horizontal span required
1350 * @param minSpanY The minimum vertical span required
1351 * @param spanX Horizontal span of the object.
1352 * @param spanY Vertical span of the object.
1353 * @param ignoreOccupied If true, the result can be an occupied cell
1354 * @param result Array in which to place the result, or null (in which case a new array will
1355 * be allocated)
1356 * @return The X, Y cell of a vacant area that can contain this object,
1357 * nearest the requested location.
1358 */
1359 int[] findNearestArea(int pixelX, int pixelY, int minSpanX, int minSpanY, int spanX, int spanY,
Adam Cohen482ed822012-03-02 14:15:13 -08001360 View ignoreView, boolean ignoreOccupied, int[] result, int[] resultSpan,
1361 boolean[][] occupied) {
Adam Cohend41fbf52012-02-16 23:53:59 -08001362 lazyInitTempRectStack();
Michael Jurkac6ee42e2010-09-30 12:04:50 -07001363 // mark space take by ignoreView as available (method checks if ignoreView is null)
Adam Cohen482ed822012-03-02 14:15:13 -08001364 markCellsAsUnoccupiedForView(ignoreView, occupied);
Michael Jurkac6ee42e2010-09-30 12:04:50 -07001365
Adam Cohene3e27a82011-04-15 12:07:39 -07001366 // For items with a spanX / spanY > 1, the passed in point (pixelX, pixelY) corresponds
1367 // to the center of the item, but we are searching based on the top-left cell, so
1368 // we translate the point over to correspond to the top-left.
1369 pixelX -= (mCellWidth + mWidthGap) * (spanX - 1) / 2f;
1370 pixelY -= (mCellHeight + mHeightGap) * (spanY - 1) / 2f;
1371
Jeff Sharkey70864282009-04-07 21:08:40 -07001372 // Keep track of best-scoring drop area
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001373 final int[] bestXY = result != null ? result : new int[2];
Jeff Sharkey70864282009-04-07 21:08:40 -07001374 double bestDistance = Double.MAX_VALUE;
Adam Cohend41fbf52012-02-16 23:53:59 -08001375 final Rect bestRect = new Rect(-1, -1, -1, -1);
1376 final Stack<Rect> validRegions = new Stack<Rect>();
Winson Chungaafa03c2010-06-11 17:34:16 -07001377
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001378 final int countX = mCountX;
1379 final int countY = mCountY;
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001380
Adam Cohend41fbf52012-02-16 23:53:59 -08001381 if (minSpanX <= 0 || minSpanY <= 0 || spanX <= 0 || spanY <= 0 ||
1382 spanX < minSpanX || spanY < minSpanY) {
1383 return bestXY;
1384 }
1385
1386 for (int y = 0; y < countY - (minSpanY - 1); y++) {
Michael Jurkac28de512010-08-13 11:27:44 -07001387 inner:
Adam Cohend41fbf52012-02-16 23:53:59 -08001388 for (int x = 0; x < countX - (minSpanX - 1); x++) {
1389 int ySize = -1;
1390 int xSize = -1;
Adam Cohendf035382011-04-11 17:22:04 -07001391 if (ignoreOccupied) {
Adam Cohend41fbf52012-02-16 23:53:59 -08001392 // First, let's see if this thing fits anywhere
1393 for (int i = 0; i < minSpanX; i++) {
1394 for (int j = 0; j < minSpanY; j++) {
Adam Cohendf035382011-04-11 17:22:04 -07001395 if (occupied[x + i][y + j]) {
Adam Cohendf035382011-04-11 17:22:04 -07001396 continue inner;
1397 }
Michael Jurkac28de512010-08-13 11:27:44 -07001398 }
1399 }
Adam Cohend41fbf52012-02-16 23:53:59 -08001400 xSize = minSpanX;
1401 ySize = minSpanY;
1402
1403 // We know that the item will fit at _some_ acceptable size, now let's see
1404 // how big we can make it. We'll alternate between incrementing x and y spans
1405 // until we hit a limit.
1406 boolean incX = true;
1407 boolean hitMaxX = xSize >= spanX;
1408 boolean hitMaxY = ySize >= spanY;
1409 while (!(hitMaxX && hitMaxY)) {
1410 if (incX && !hitMaxX) {
1411 for (int j = 0; j < ySize; j++) {
1412 if (x + xSize > countX -1 || occupied[x + xSize][y + j]) {
1413 // We can't move out horizontally
1414 hitMaxX = true;
1415 }
1416 }
1417 if (!hitMaxX) {
1418 xSize++;
1419 }
1420 } else if (!hitMaxY) {
1421 for (int i = 0; i < xSize; i++) {
1422 if (y + ySize > countY - 1 || occupied[x + i][y + ySize]) {
1423 // We can't move out vertically
1424 hitMaxY = true;
1425 }
1426 }
1427 if (!hitMaxY) {
1428 ySize++;
1429 }
1430 }
1431 hitMaxX |= xSize >= spanX;
1432 hitMaxY |= ySize >= spanY;
1433 incX = !incX;
1434 }
1435 incX = true;
1436 hitMaxX = xSize >= spanX;
1437 hitMaxY = ySize >= spanY;
Michael Jurkac28de512010-08-13 11:27:44 -07001438 }
Winson Chung0be025d2011-05-23 17:45:09 -07001439 final int[] cellXY = mTmpXY;
Adam Cohene3e27a82011-04-15 12:07:39 -07001440 cellToCenterPoint(x, y, cellXY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001441
Adam Cohend41fbf52012-02-16 23:53:59 -08001442 // We verify that the current rect is not a sub-rect of any of our previous
1443 // candidates. In this case, the current rect is disqualified in favour of the
1444 // containing rect.
1445 Rect currentRect = mTempRectStack.pop();
1446 currentRect.set(x, y, x + xSize, y + ySize);
1447 boolean contained = false;
1448 for (Rect r : validRegions) {
1449 if (r.contains(currentRect)) {
1450 contained = true;
1451 break;
1452 }
1453 }
1454 validRegions.push(currentRect);
Michael Jurkac28de512010-08-13 11:27:44 -07001455 double distance = Math.sqrt(Math.pow(cellXY[0] - pixelX, 2)
1456 + Math.pow(cellXY[1] - pixelY, 2));
Adam Cohen482ed822012-03-02 14:15:13 -08001457
Adam Cohend41fbf52012-02-16 23:53:59 -08001458 if ((distance <= bestDistance && !contained) ||
1459 currentRect.contains(bestRect)) {
Michael Jurkac28de512010-08-13 11:27:44 -07001460 bestDistance = distance;
1461 bestXY[0] = x;
1462 bestXY[1] = y;
Adam Cohend41fbf52012-02-16 23:53:59 -08001463 if (resultSpan != null) {
1464 resultSpan[0] = xSize;
1465 resultSpan[1] = ySize;
1466 }
1467 bestRect.set(currentRect);
Michael Jurkac28de512010-08-13 11:27:44 -07001468 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001469 }
1470 }
Michael Jurkac6ee42e2010-09-30 12:04:50 -07001471 // re-mark space taken by ignoreView as occupied
Adam Cohen482ed822012-03-02 14:15:13 -08001472 markCellsAsOccupiedForView(ignoreView, occupied);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001473
Adam Cohenc0dcf592011-06-01 15:30:43 -07001474 // Return -1, -1 if no suitable location found
1475 if (bestDistance == Double.MAX_VALUE) {
1476 bestXY[0] = -1;
1477 bestXY[1] = -1;
Jeff Sharkey70864282009-04-07 21:08:40 -07001478 }
Adam Cohend41fbf52012-02-16 23:53:59 -08001479 recycleTempRects(validRegions);
Adam Cohenc0dcf592011-06-01 15:30:43 -07001480 return bestXY;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001481 }
Winson Chungaafa03c2010-06-11 17:34:16 -07001482
Adam Cohen482ed822012-03-02 14:15:13 -08001483 /**
1484 * Find a vacant area that will fit the given bounds nearest the requested
1485 * cell location, and will also weigh in a suggested direction vector of the
1486 * desired location. This method computers distance based on unit grid distances,
1487 * not pixel distances.
1488 *
Adam Cohen47a876d2012-03-19 13:21:41 -07001489 * @param cellX The X cell nearest to which you want to search for a vacant area.
1490 * @param cellY The Y cell nearest which you want to search for a vacant area.
Adam Cohen482ed822012-03-02 14:15:13 -08001491 * @param spanX Horizontal span of the object.
1492 * @param spanY Vertical span of the object.
Adam Cohen47a876d2012-03-19 13:21:41 -07001493 * @param direction The favored direction in which the views should move from x, y
1494 * @param exactDirectionOnly If this parameter is true, then only solutions where the direction
1495 * matches exactly. Otherwise we find the best matching direction.
1496 * @param occoupied The array which represents which cells in the CellLayout are occupied
1497 * @param blockOccupied The array which represents which cells in the specified block (cellX,
1498 * cellY, spanX, spanY) are occupied. This is used when try to move a group of views.
Adam Cohen482ed822012-03-02 14:15:13 -08001499 * @param result Array in which to place the result, or null (in which case a new array will
1500 * be allocated)
1501 * @return The X, Y cell of a vacant area that can contain this object,
1502 * nearest the requested location.
1503 */
1504 private int[] findNearestArea(int cellX, int cellY, int spanX, int spanY, int[] direction,
Adam Cohen47a876d2012-03-19 13:21:41 -07001505 boolean[][] occupied, boolean blockOccupied[][], int[] result) {
Adam Cohen482ed822012-03-02 14:15:13 -08001506 // Keep track of best-scoring drop area
1507 final int[] bestXY = result != null ? result : new int[2];
1508 float bestDistance = Float.MAX_VALUE;
1509 int bestDirectionScore = Integer.MIN_VALUE;
1510
1511 final int countX = mCountX;
1512 final int countY = mCountY;
1513
1514 for (int y = 0; y < countY - (spanY - 1); y++) {
1515 inner:
1516 for (int x = 0; x < countX - (spanX - 1); x++) {
1517 // First, let's see if this thing fits anywhere
1518 for (int i = 0; i < spanX; i++) {
1519 for (int j = 0; j < spanY; j++) {
Adam Cohen47a876d2012-03-19 13:21:41 -07001520 if (occupied[x + i][y + j] && (blockOccupied == null || blockOccupied[i][j])) {
Adam Cohen482ed822012-03-02 14:15:13 -08001521 continue inner;
1522 }
1523 }
1524 }
1525
1526 float distance = (float)
1527 Math.sqrt((x - cellX) * (x - cellX) + (y - cellY) * (y - cellY));
1528 int[] curDirection = mTmpPoint;
Adam Cohen47a876d2012-03-19 13:21:41 -07001529 computeDirectionVector(x - cellX, y - cellY, curDirection);
1530 // The direction score is just the dot product of the two candidate direction
1531 // and that passed in.
Adam Cohen482ed822012-03-02 14:15:13 -08001532 int curDirectionScore = direction[0] * curDirection[0] +
1533 direction[1] * curDirection[1];
Adam Cohen47a876d2012-03-19 13:21:41 -07001534 boolean exactDirectionOnly = false;
1535 boolean directionMatches = direction[0] == curDirection[0] &&
1536 direction[0] == curDirection[0];
1537 if ((directionMatches || !exactDirectionOnly) &&
1538 Float.compare(distance, bestDistance) < 0 || (Float.compare(distance,
Adam Cohen482ed822012-03-02 14:15:13 -08001539 bestDistance) == 0 && curDirectionScore > bestDirectionScore)) {
1540 bestDistance = distance;
1541 bestDirectionScore = curDirectionScore;
1542 bestXY[0] = x;
1543 bestXY[1] = y;
1544 }
1545 }
1546 }
1547
1548 // Return -1, -1 if no suitable location found
1549 if (bestDistance == Float.MAX_VALUE) {
1550 bestXY[0] = -1;
1551 bestXY[1] = -1;
1552 }
1553 return bestXY;
1554 }
1555
Adam Cohen47a876d2012-03-19 13:21:41 -07001556 private int[] findNearestAreaInDirection(int cellX, int cellY, int spanX, int spanY,
1557 int[] direction,boolean[][] occupied,
1558 boolean blockOccupied[][], int[] result) {
1559 // Keep track of best-scoring drop area
1560 final int[] bestXY = result != null ? result : new int[2];
1561 bestXY[0] = -1;
1562 bestXY[1] = -1;
1563 float bestDistance = Float.MAX_VALUE;
1564
1565 // We use this to march in a single direction
1566 if (direction[0] != 0 && direction[1] != 0) {
1567 return bestXY;
1568 }
1569
1570 // This will only incrememnet one of x or y based on the assertion above
1571 int x = cellX + direction[0];
1572 int y = cellY + direction[1];
1573 while (x >= 0 && x + spanX <= mCountX && y >= 0 && y + spanY <= mCountY) {
1574
1575 boolean fail = false;
1576 for (int i = 0; i < spanX; i++) {
1577 for (int j = 0; j < spanY; j++) {
1578 if (occupied[x + i][y + j] && (blockOccupied == null || blockOccupied[i][j])) {
1579 fail = true;
1580 }
1581 }
1582 }
1583 if (!fail) {
1584 float distance = (float)
1585 Math.sqrt((x - cellX) * (x - cellX) + (y - cellY) * (y - cellY));
1586 if (Float.compare(distance, bestDistance) < 0) {
1587 bestDistance = distance;
1588 bestXY[0] = x;
1589 bestXY[1] = y;
1590 }
1591 }
1592 x += direction[0];
1593 y += direction[1];
1594 }
1595 return bestXY;
1596 }
1597
Adam Cohen482ed822012-03-02 14:15:13 -08001598 private boolean addViewToTempLocation(View v, Rect rectOccupiedByPotentialDrop,
Adam Cohen8baab352012-03-20 17:39:21 -07001599 int[] direction, ItemConfiguration currentState) {
1600 CellAndSpan c = currentState.map.get(v);
Adam Cohen482ed822012-03-02 14:15:13 -08001601 boolean success = false;
Adam Cohen8baab352012-03-20 17:39:21 -07001602 markCellsForView(c.x, c.y, c.spanX, c.spanY, mTmpOccupied, false);
Adam Cohen482ed822012-03-02 14:15:13 -08001603 markCellsForRect(rectOccupiedByPotentialDrop, mTmpOccupied, true);
1604
Adam Cohen8baab352012-03-20 17:39:21 -07001605 findNearestArea(c.x, c.y, c.spanX, c.spanY, direction, mTmpOccupied, null, mTempLocation);
Adam Cohen482ed822012-03-02 14:15:13 -08001606
1607 if (mTempLocation[0] >= 0 && mTempLocation[1] >= 0) {
Adam Cohen8baab352012-03-20 17:39:21 -07001608 c.x = mTempLocation[0];
1609 c.y = mTempLocation[1];
Adam Cohen482ed822012-03-02 14:15:13 -08001610 success = true;
1611
1612 }
Adam Cohen8baab352012-03-20 17:39:21 -07001613 markCellsForView(c.x, c.y, c.spanX, c.spanY, mTmpOccupied, true);
Adam Cohen482ed822012-03-02 14:15:13 -08001614 return success;
1615 }
1616
Adam Cohen47a876d2012-03-19 13:21:41 -07001617 // This method looks in the specified direction to see if there is an additional view
1618 // immediately adjecent in that direction
1619 private boolean addViewInDirection(ArrayList<View> views, Rect boundingRect, int[] direction,
Adam Cohen19f37922012-03-21 11:59:11 -07001620 boolean[][] occupied, View dragView, ItemConfiguration currentState) {
Adam Cohen47a876d2012-03-19 13:21:41 -07001621 boolean found = false;
1622
Michael Jurkaa52570f2012-03-20 03:18:20 -07001623 int childCount = mShortcutsAndWidgets.getChildCount();
Adam Cohen47a876d2012-03-19 13:21:41 -07001624 Rect r0 = new Rect(boundingRect);
1625 Rect r1 = new Rect();
1626
1627 int deltaX = 0;
1628 int deltaY = 0;
1629 if (direction[1] < 0) {
1630 r0.set(r0.left, r0.top - 1, r0.right, r0.bottom);
1631 deltaY = -1;
1632 } else if (direction[1] > 0) {
1633 r0.set(r0.left, r0.top, r0.right, r0.bottom + 1);
1634 deltaY = 1;
1635 } else if (direction[0] < 0) {
1636 r0.set(r0.left - 1, r0.top, r0.right, r0.bottom);
1637 deltaX = -1;
1638 } else if (direction[0] > 0) {
1639 r0.set(r0.left, r0.top, r0.right + 1, r0.bottom);
1640 deltaX = 1;
1641 }
1642
1643 for (int i = 0; i < childCount; i++) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07001644 View child = mShortcutsAndWidgets.getChildAt(i);
Adam Cohen19f37922012-03-21 11:59:11 -07001645 if (views.contains(child) || child == dragView) continue;
Adam Cohen8baab352012-03-20 17:39:21 -07001646 CellAndSpan c = currentState.map.get(child);
Adam Cohen47a876d2012-03-19 13:21:41 -07001647
Adam Cohen8baab352012-03-20 17:39:21 -07001648 LayoutParams lp = (LayoutParams) child.getLayoutParams();
1649 r1.set(c.x, c.y, c.x + c.spanX, c.y + c.spanY);
Adam Cohen47a876d2012-03-19 13:21:41 -07001650 if (Rect.intersects(r0, r1)) {
1651 if (!lp.canReorder) {
1652 return false;
1653 }
1654 boolean pushed = false;
Adam Cohen8baab352012-03-20 17:39:21 -07001655 for (int x = c.x; x < c.x + c.spanX; x++) {
1656 for (int y = c.y; y < c.y + c.spanY; y++) {
Adam Cohen47a876d2012-03-19 13:21:41 -07001657 boolean inBounds = x - deltaX >= 0 && x -deltaX < mCountX
1658 && y - deltaY >= 0 && y - deltaY < mCountY;
1659 if (inBounds && occupied[x - deltaX][y - deltaY]) {
1660 pushed = true;
1661 }
1662 }
1663 }
1664 if (pushed) {
1665 views.add(child);
Adam Cohen8baab352012-03-20 17:39:21 -07001666 boundingRect.union(c.x, c.y, c.x + c.spanX, c.y + c.spanY);
Adam Cohen47a876d2012-03-19 13:21:41 -07001667 found = true;
1668 }
1669 }
1670 }
1671 return found;
1672 }
1673
Adam Cohen482ed822012-03-02 14:15:13 -08001674 private boolean addViewsToTempLocation(ArrayList<View> views, Rect rectOccupiedByPotentialDrop,
Adam Cohen19f37922012-03-21 11:59:11 -07001675 int[] direction, boolean push, View dragView, ItemConfiguration currentState) {
Adam Cohen482ed822012-03-02 14:15:13 -08001676 if (views.size() == 0) return true;
Adam Cohen482ed822012-03-02 14:15:13 -08001677
Adam Cohen8baab352012-03-20 17:39:21 -07001678 boolean success = false;
Adam Cohen482ed822012-03-02 14:15:13 -08001679 Rect boundingRect = null;
Adam Cohen8baab352012-03-20 17:39:21 -07001680 // We construct a rect which represents the entire group of views passed in
Adam Cohen482ed822012-03-02 14:15:13 -08001681 for (View v: views) {
Adam Cohen8baab352012-03-20 17:39:21 -07001682 CellAndSpan c = currentState.map.get(v);
Adam Cohen482ed822012-03-02 14:15:13 -08001683 if (boundingRect == null) {
Adam Cohen8baab352012-03-20 17:39:21 -07001684 boundingRect = new Rect(c.x, c.y, c.x + c.spanX, c.y + c.spanY);
Adam Cohen482ed822012-03-02 14:15:13 -08001685 } else {
Adam Cohen8baab352012-03-20 17:39:21 -07001686 boundingRect.union(c.x, c.y, c.x + c.spanX, c.y + c.spanY);
Adam Cohen482ed822012-03-02 14:15:13 -08001687 }
1688 }
Adam Cohen8baab352012-03-20 17:39:21 -07001689
1690 @SuppressWarnings("unchecked")
1691 ArrayList<View> dup = (ArrayList<View>) views.clone();
1692 // We try and expand the group of views in the direction vector passed, based on
1693 // whether they are physically adjacent, ie. based on "push mechanics".
Adam Cohen19f37922012-03-21 11:59:11 -07001694 while (push && addViewInDirection(dup, boundingRect, direction, mTmpOccupied, dragView,
Adam Cohen8baab352012-03-20 17:39:21 -07001695 currentState)) {
1696 }
1697
1698 // Mark the occupied state as false for the group of views we want to move.
1699 for (View v: dup) {
1700 CellAndSpan c = currentState.map.get(v);
1701 markCellsForView(c.x, c.y, c.spanX, c.spanY, mTmpOccupied, false);
1702 }
1703
Adam Cohen47a876d2012-03-19 13:21:41 -07001704 boolean[][] blockOccupied = new boolean[boundingRect.width()][boundingRect.height()];
1705 int top = boundingRect.top;
1706 int left = boundingRect.left;
Adam Cohen8baab352012-03-20 17:39:21 -07001707 // We mark more precisely which parts of the bounding rect are truly occupied, allowing
1708 // for tetris-style interlocking.
1709 for (View v: dup) {
1710 CellAndSpan c = currentState.map.get(v);
1711 markCellsForView(c.x - left, c.y - top, c.spanX, c.spanY, blockOccupied, true);
Adam Cohen47a876d2012-03-19 13:21:41 -07001712 }
1713
Adam Cohen482ed822012-03-02 14:15:13 -08001714 markCellsForRect(rectOccupiedByPotentialDrop, mTmpOccupied, true);
1715
Adam Cohen8baab352012-03-20 17:39:21 -07001716 if (push) {
1717 findNearestAreaInDirection(boundingRect.left, boundingRect.top, boundingRect.width(),
1718 boundingRect.height(), direction, mTmpOccupied, blockOccupied, mTempLocation);
1719 } else {
1720 findNearestArea(boundingRect.left, boundingRect.top, boundingRect.width(),
1721 boundingRect.height(), direction, mTmpOccupied, blockOccupied, mTempLocation);
1722 }
Adam Cohen482ed822012-03-02 14:15:13 -08001723
Adam Cohen8baab352012-03-20 17:39:21 -07001724 // If we successfuly found a location by pushing the block of views, we commit it
Adam Cohen482ed822012-03-02 14:15:13 -08001725 if (mTempLocation[0] >= 0 && mTempLocation[1] >= 0) {
Adam Cohen8baab352012-03-20 17:39:21 -07001726 int deltaX = mTempLocation[0] - boundingRect.left;
1727 int deltaY = mTempLocation[1] - boundingRect.top;
1728 for (View v: dup) {
1729 CellAndSpan c = currentState.map.get(v);
1730 c.x += deltaX;
1731 c.y += deltaY;
Adam Cohen482ed822012-03-02 14:15:13 -08001732 }
1733 success = true;
1734 }
Adam Cohen8baab352012-03-20 17:39:21 -07001735
1736 // In either case, we set the occupied array as marked for the location of the views
1737 for (View v: dup) {
1738 CellAndSpan c = currentState.map.get(v);
1739 markCellsForView(c.x, c.y, c.spanX, c.spanY, mTmpOccupied, true);
Adam Cohen482ed822012-03-02 14:15:13 -08001740 }
1741 return success;
1742 }
1743
1744 private void markCellsForRect(Rect r, boolean[][] occupied, boolean value) {
1745 markCellsForView(r.left, r.top, r.width(), r.height(), occupied, value);
1746 }
1747
1748 private boolean rearrangementExists(int cellX, int cellY, int spanX, int spanY, int[] direction,
Adam Cohen8baab352012-03-20 17:39:21 -07001749 View ignoreView, ItemConfiguration solution) {
Adam Cohen482ed822012-03-02 14:15:13 -08001750
Adam Cohen8baab352012-03-20 17:39:21 -07001751 mIntersectingViews.clear();
Adam Cohen482ed822012-03-02 14:15:13 -08001752 mOccupiedRect.set(cellX, cellY, cellX + spanX, cellY + spanY);
Adam Cohen482ed822012-03-02 14:15:13 -08001753
Adam Cohen8baab352012-03-20 17:39:21 -07001754 // Mark the desired location of the view currently being dragged.
Adam Cohen482ed822012-03-02 14:15:13 -08001755 if (ignoreView != null) {
Adam Cohen8baab352012-03-20 17:39:21 -07001756 CellAndSpan c = solution.map.get(ignoreView);
Adam Cohen19f37922012-03-21 11:59:11 -07001757 if (c != null) {
1758 c.x = cellX;
1759 c.y = cellY;
1760 }
Adam Cohen482ed822012-03-02 14:15:13 -08001761 }
Adam Cohen482ed822012-03-02 14:15:13 -08001762 Rect r0 = new Rect(cellX, cellY, cellX + spanX, cellY + spanY);
1763 Rect r1 = new Rect();
Adam Cohen8baab352012-03-20 17:39:21 -07001764 for (View child: solution.map.keySet()) {
Adam Cohen482ed822012-03-02 14:15:13 -08001765 if (child == ignoreView) continue;
Adam Cohen8baab352012-03-20 17:39:21 -07001766 CellAndSpan c = solution.map.get(child);
Adam Cohen482ed822012-03-02 14:15:13 -08001767 LayoutParams lp = (LayoutParams) child.getLayoutParams();
Adam Cohen8baab352012-03-20 17:39:21 -07001768 r1.set(c.x, c.y, c.x + c.spanX, c.y + c.spanY);
Adam Cohen482ed822012-03-02 14:15:13 -08001769 if (Rect.intersects(r0, r1)) {
1770 if (!lp.canReorder) {
1771 return false;
1772 }
1773 mIntersectingViews.add(child);
1774 }
1775 }
Adam Cohen47a876d2012-03-19 13:21:41 -07001776
Adam Cohen8baab352012-03-20 17:39:21 -07001777 // We try to move the intersecting views as a block using the push mechanic
Adam Cohen19f37922012-03-21 11:59:11 -07001778 if (addViewsToTempLocation(mIntersectingViews, mOccupiedRect, direction, true, ignoreView,
1779 solution)) {
Adam Cohen47a876d2012-03-19 13:21:41 -07001780 return true;
1781 }
1782 // Try the opposite direction
1783 direction[0] *= -1;
1784 direction[1] *= -1;
Adam Cohen19f37922012-03-21 11:59:11 -07001785 if (addViewsToTempLocation(mIntersectingViews, mOccupiedRect, direction, true, ignoreView,
1786 solution)) {
Adam Cohen47a876d2012-03-19 13:21:41 -07001787 return true;
1788 }
1789 // Switch the direction back
1790 direction[0] *= -1;
1791 direction[1] *= -1;
1792
Adam Cohen8baab352012-03-20 17:39:21 -07001793 // Next we try moving the views as a block , but without requiring the push mechanic
Adam Cohen19f37922012-03-21 11:59:11 -07001794 if (addViewsToTempLocation(mIntersectingViews, mOccupiedRect, direction, false, ignoreView,
1795 solution)) {
Adam Cohen482ed822012-03-02 14:15:13 -08001796 return true;
1797 }
Adam Cohen47a876d2012-03-19 13:21:41 -07001798
Adam Cohen482ed822012-03-02 14:15:13 -08001799 // Ok, they couldn't move as a block, let's move them individually
1800 for (View v : mIntersectingViews) {
Adam Cohen8baab352012-03-20 17:39:21 -07001801 if (!addViewToTempLocation(v, mOccupiedRect, direction, solution)) {
Adam Cohen482ed822012-03-02 14:15:13 -08001802 return false;
1803 }
1804 }
1805 return true;
1806 }
1807
1808 /*
1809 * Returns a pair (x, y), where x,y are in {-1, 0, 1} corresponding to vector between
1810 * the provided point and the provided cell
1811 */
Adam Cohen47a876d2012-03-19 13:21:41 -07001812 private void computeDirectionVector(float deltaX, float deltaY, int[] result) {
Adam Cohen482ed822012-03-02 14:15:13 -08001813 double angle = Math.atan(((float) deltaY) / deltaX);
1814
1815 result[0] = 0;
1816 result[1] = 0;
1817 if (Math.abs(Math.cos(angle)) > 0.5f) {
1818 result[0] = (int) Math.signum(deltaX);
1819 }
1820 if (Math.abs(Math.sin(angle)) > 0.5f) {
1821 result[1] = (int) Math.signum(deltaY);
1822 }
1823 }
1824
Adam Cohen8baab352012-03-20 17:39:21 -07001825 private void copyOccupiedArray(boolean[][] occupied) {
1826 for (int i = 0; i < mCountX; i++) {
1827 for (int j = 0; j < mCountY; j++) {
1828 occupied[i][j] = mOccupied[i][j];
1829 }
1830 }
1831 }
1832
Adam Cohen482ed822012-03-02 14:15:13 -08001833 ItemConfiguration simpleSwap(int pixelX, int pixelY, int minSpanX, int minSpanY, int spanX,
1834 int spanY, int[] direction, View dragView, boolean decX, ItemConfiguration solution) {
Adam Cohen8baab352012-03-20 17:39:21 -07001835 // Copy the current state into the solution. This solution will be manipulated as necessary.
1836 copyCurrentStateToSolution(solution, false);
1837 // Copy the current occupied array into the temporary occupied array. This array will be
1838 // manipulated as necessary to find a solution.
1839 copyOccupiedArray(mTmpOccupied);
Adam Cohen482ed822012-03-02 14:15:13 -08001840
1841 // We find the nearest cell into which we would place the dragged item, assuming there's
1842 // nothing in its way.
1843 int result[] = new int[2];
1844 result = findNearestArea(pixelX, pixelY, spanX, spanY, result);
1845
1846 boolean success = false;
1847 // First we try the exact nearest position of the item being dragged,
1848 // we will then want to try to move this around to other neighbouring positions
Adam Cohen8baab352012-03-20 17:39:21 -07001849 success = rearrangementExists(result[0], result[1], spanX, spanY, direction, dragView,
1850 solution);
Adam Cohen482ed822012-03-02 14:15:13 -08001851
1852 if (!success) {
1853 // We try shrinking the widget down to size in an alternating pattern, shrink 1 in
1854 // x, then 1 in y etc.
1855 if (spanX > minSpanX && (minSpanY == spanY || decX)) {
1856 return simpleSwap(pixelX, pixelY, minSpanX, minSpanY, spanX - 1, spanY, direction,
1857 dragView, false, solution);
1858 } else if (spanY > minSpanY) {
1859 return simpleSwap(pixelX, pixelY, minSpanX, minSpanY, spanX, spanY - 1, direction,
1860 dragView, true, solution);
1861 }
1862 solution.isSolution = false;
1863 } else {
1864 solution.isSolution = true;
1865 solution.dragViewX = result[0];
1866 solution.dragViewY = result[1];
1867 solution.dragViewSpanX = spanX;
1868 solution.dragViewSpanY = spanY;
Adam Cohen482ed822012-03-02 14:15:13 -08001869 }
1870 return solution;
1871 }
1872
1873 private void copyCurrentStateToSolution(ItemConfiguration solution, boolean temp) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07001874 int childCount = mShortcutsAndWidgets.getChildCount();
Adam Cohen482ed822012-03-02 14:15:13 -08001875 for (int i = 0; i < childCount; i++) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07001876 View child = mShortcutsAndWidgets.getChildAt(i);
Adam Cohen482ed822012-03-02 14:15:13 -08001877 LayoutParams lp = (LayoutParams) child.getLayoutParams();
Adam Cohen8baab352012-03-20 17:39:21 -07001878 CellAndSpan c;
Adam Cohen482ed822012-03-02 14:15:13 -08001879 if (temp) {
Adam Cohen8baab352012-03-20 17:39:21 -07001880 c = new CellAndSpan(lp.tmpCellX, lp.tmpCellY, lp.cellHSpan, lp.cellVSpan);
Adam Cohen482ed822012-03-02 14:15:13 -08001881 } else {
Adam Cohen8baab352012-03-20 17:39:21 -07001882 c = new CellAndSpan(lp.cellX, lp.cellY, lp.cellHSpan, lp.cellVSpan);
Adam Cohen482ed822012-03-02 14:15:13 -08001883 }
Adam Cohen8baab352012-03-20 17:39:21 -07001884 solution.map.put(child, c);
Adam Cohen482ed822012-03-02 14:15:13 -08001885 }
1886 }
1887
1888 private void copySolutionToTempState(ItemConfiguration solution, View dragView) {
1889 for (int i = 0; i < mCountX; i++) {
1890 for (int j = 0; j < mCountY; j++) {
1891 mTmpOccupied[i][j] = false;
1892 }
1893 }
1894
Michael Jurkaa52570f2012-03-20 03:18:20 -07001895 int childCount = mShortcutsAndWidgets.getChildCount();
Adam Cohen482ed822012-03-02 14:15:13 -08001896 for (int i = 0; i < childCount; i++) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07001897 View child = mShortcutsAndWidgets.getChildAt(i);
Adam Cohen482ed822012-03-02 14:15:13 -08001898 if (child == dragView) continue;
1899 LayoutParams lp = (LayoutParams) child.getLayoutParams();
Adam Cohen8baab352012-03-20 17:39:21 -07001900 CellAndSpan c = solution.map.get(child);
1901 if (c != null) {
1902 lp.tmpCellX = c.x;
1903 lp.tmpCellY = c.y;
1904 lp.cellHSpan = c.spanX;
1905 lp.cellVSpan = c.spanY;
1906 markCellsForView(c.x, c.y, c.spanX, c.spanY, mTmpOccupied, true);
Adam Cohen482ed822012-03-02 14:15:13 -08001907 }
1908 }
1909 markCellsForView(solution.dragViewX, solution.dragViewY, solution.dragViewSpanX,
1910 solution.dragViewSpanY, mTmpOccupied, true);
1911 }
1912
1913 private void animateItemsToSolution(ItemConfiguration solution, View dragView, boolean
1914 commitDragView) {
1915
1916 boolean[][] occupied = DESTRUCTIVE_REORDER ? mOccupied : mTmpOccupied;
1917 for (int i = 0; i < mCountX; i++) {
1918 for (int j = 0; j < mCountY; j++) {
1919 occupied[i][j] = false;
1920 }
1921 }
1922
Michael Jurkaa52570f2012-03-20 03:18:20 -07001923 int childCount = mShortcutsAndWidgets.getChildCount();
Adam Cohen482ed822012-03-02 14:15:13 -08001924 for (int i = 0; i < childCount; i++) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07001925 View child = mShortcutsAndWidgets.getChildAt(i);
Adam Cohen482ed822012-03-02 14:15:13 -08001926 if (child == dragView) continue;
Adam Cohen8baab352012-03-20 17:39:21 -07001927 CellAndSpan c = solution.map.get(child);
1928 if (c != null) {
Adam Cohen19f37922012-03-21 11:59:11 -07001929 animateChildToPosition(child, c.x, c.y, REORDER_ANIMATION_DURATION, 0,
1930 DESTRUCTIVE_REORDER, false);
Adam Cohen8baab352012-03-20 17:39:21 -07001931 markCellsForView(c.x, c.y, c.spanX, c.spanY, occupied, true);
Adam Cohen482ed822012-03-02 14:15:13 -08001932 }
1933 }
1934 if (commitDragView) {
1935 markCellsForView(solution.dragViewX, solution.dragViewY, solution.dragViewSpanX,
1936 solution.dragViewSpanY, occupied, true);
1937 }
1938 }
1939
Adam Cohen19f37922012-03-21 11:59:11 -07001940 // This method starts or changes the reorder hint animations
1941 private void beginOrAdjustHintAnimations(ItemConfiguration solution, View dragView, int delay) {
1942 int childCount = mShortcutsAndWidgets.getChildCount();
1943 int timeForPriorAnimationToComplete = getMaxCompletionTime();
1944 for (int i = 0; i < childCount; i++) {
1945 View child = mShortcutsAndWidgets.getChildAt(i);
1946 if (child == dragView) continue;
1947 CellAndSpan c = solution.map.get(child);
1948 LayoutParams lp = (LayoutParams) child.getLayoutParams();
1949 if (c != null) {
1950 ReorderHintAnimation rha = new ReorderHintAnimation(child, lp.cellX, lp.cellY,
1951 c.x, c.y, c.spanX, c.spanY);
1952 rha.animate(timeForPriorAnimationToComplete);
1953 }
1954 }
1955 }
1956
1957 // Class which represents the reorder hint animations. These animations show that an item is
1958 // in a temporary state, and hint at where the item will return to.
1959 class ReorderHintAnimation {
1960 View child;
1961 float deltaX;
1962 float deltaY;
1963 private static final int DURATION = 140;
1964 private int repeatCount;
1965 private boolean cancelOnCycleComplete = false;
1966 ValueAnimator va;
1967
1968 public ReorderHintAnimation(View child, int cellX0, int cellY0, int cellX1, int cellY1,
1969 int spanX, int spanY) {
1970 regionToCenterPoint(cellX0, cellY0, spanX, spanY, mTmpPoint);
1971 final int x0 = mTmpPoint[0];
1972 final int y0 = mTmpPoint[1];
1973 regionToCenterPoint(cellX1, cellY1, spanX, spanY, mTmpPoint);
1974 final int x1 = mTmpPoint[0];
1975 final int y1 = mTmpPoint[1];
1976 final int dX = x1 - x0;
1977 final int dY = y1 - y0;
1978 deltaX = 0;
1979 deltaY = 0;
1980 if (dX == dY && dX == 0) {
1981 } else {
1982 if (dY == 0) {
1983 deltaX = mReorderHintAnimationMagnitude;
1984 } else if (dX == 0) {
1985 deltaY = mReorderHintAnimationMagnitude;
1986 } else {
1987 double angle = Math.atan( (float) (dY) / dX);
1988 deltaX = (int) (Math.cos(angle) * mReorderHintAnimationMagnitude);
1989 deltaY = (int) (Math.sin(angle) * mReorderHintAnimationMagnitude);
1990 }
1991 }
1992 this.child = child;
1993 }
1994
1995 void animate(int delay) {
1996 if (mShakeAnimators.containsKey(child)) {
1997 ReorderHintAnimation oldAnimation = mShakeAnimators.get(child);
1998 oldAnimation.completeAnimation();
1999 mShakeAnimators.remove(child);
2000 }
2001 if (deltaX == 0 && deltaY == 0) {
2002 return;
2003 }
2004 va = ValueAnimator.ofFloat(0f, 1f);
2005 va.setRepeatMode(ValueAnimator.REVERSE);
2006 va.setRepeatCount(ValueAnimator.INFINITE);
2007 va.setDuration(DURATION);
2008 va.addUpdateListener(new AnimatorUpdateListener() {
2009 @Override
2010 public void onAnimationUpdate(ValueAnimator animation) {
2011 float r = ((Float) animation.getAnimatedValue()).floatValue();
2012 float x = r * deltaX;
2013 float y = r * deltaY;
2014 child.setTranslationX(x);
2015 child.setTranslationY(y);
2016 }
2017 });
2018 va.addListener(new AnimatorListenerAdapter() {
2019 public void onAnimationRepeat(Animator animation) {
2020 repeatCount++;
2021 // We make sure to end only after a full period
2022 if (cancelOnCycleComplete && repeatCount % 2 == 0) {
2023 va.cancel();
2024 }
2025 }
2026 });
2027 va.setStartDelay(Math.max(REORDER_ANIMATION_DURATION, delay));
2028 mShakeAnimators.put(child, this);
2029 va.start();
2030 }
2031
2032
2033 private void completeAnimation() {
2034 cancelOnCycleComplete = true;
2035 }
2036
2037 // Returns the time required to complete the current oscillating animation
2038 private int completionTime() {
2039 if (repeatCount % 2 == 0) {
2040 return (int) (va.getDuration() - va.getCurrentPlayTime() + DURATION);
2041 } else {
2042 return (int) (va.getDuration() - va.getCurrentPlayTime());
2043 }
2044 }
2045 }
2046
2047 private void completeAndClearReorderHintAnimations() {
2048 for (ReorderHintAnimation a: mShakeAnimators.values()) {
2049 a.completeAnimation();
2050 }
2051 mShakeAnimators.clear();
2052 }
2053
2054 private int getMaxCompletionTime() {
2055 int maxTime = 0;
2056 for (ReorderHintAnimation a: mShakeAnimators.values()) {
2057 maxTime = Math.max(maxTime, a.completionTime());
2058 }
2059 return maxTime;
2060 }
2061
Adam Cohen482ed822012-03-02 14:15:13 -08002062 private void commitTempPlacement() {
2063 for (int i = 0; i < mCountX; i++) {
2064 for (int j = 0; j < mCountY; j++) {
2065 mOccupied[i][j] = mTmpOccupied[i][j];
2066 }
2067 }
Michael Jurkaa52570f2012-03-20 03:18:20 -07002068 int childCount = mShortcutsAndWidgets.getChildCount();
Adam Cohen482ed822012-03-02 14:15:13 -08002069 for (int i = 0; i < childCount; i++) {
Adam Cohenea889a22012-03-27 16:45:39 -07002070 View child = mShortcutsAndWidgets.getChildAt(i);
2071 LayoutParams lp = (LayoutParams) child.getLayoutParams();
2072 ItemInfo info = (ItemInfo) child.getTag();
2073 info.cellX = lp.cellX = lp.tmpCellX;
2074 info.cellY = lp.cellY = lp.tmpCellY;
Adam Cohen482ed822012-03-02 14:15:13 -08002075 }
Adam Cohenea889a22012-03-27 16:45:39 -07002076 Workspace workspace = (Workspace) getParent();
2077 workspace.updateItemLocationsInDatabase(this);
Adam Cohen482ed822012-03-02 14:15:13 -08002078 }
2079
2080 public void setUseTempCoords(boolean useTempCoords) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07002081 int childCount = mShortcutsAndWidgets.getChildCount();
Adam Cohen482ed822012-03-02 14:15:13 -08002082 for (int i = 0; i < childCount; i++) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07002083 LayoutParams lp = (LayoutParams) mShortcutsAndWidgets.getChildAt(i).getLayoutParams();
Adam Cohen482ed822012-03-02 14:15:13 -08002084 lp.useTmpCoords = useTempCoords;
2085 }
2086 }
2087
Adam Cohen482ed822012-03-02 14:15:13 -08002088 ItemConfiguration findConfigurationNoShuffle(int pixelX, int pixelY, int minSpanX, int minSpanY,
2089 int spanX, int spanY, View dragView, ItemConfiguration solution) {
2090 int[] result = new int[2];
2091 int[] resultSpan = new int[2];
2092 findNearestVacantArea(pixelX, pixelY, minSpanX, minSpanY, spanX, spanY, null, result,
2093 resultSpan);
2094 if (result[0] >= 0 && result[1] >= 0) {
2095 copyCurrentStateToSolution(solution, false);
2096 solution.dragViewX = result[0];
2097 solution.dragViewY = result[1];
2098 solution.dragViewSpanX = resultSpan[0];
2099 solution.dragViewSpanY = resultSpan[1];
2100 solution.isSolution = true;
2101 } else {
2102 solution.isSolution = false;
2103 }
2104 return solution;
2105 }
2106
2107 public void prepareChildForDrag(View child) {
2108 markCellsAsUnoccupiedForView(child);
Adam Cohen482ed822012-03-02 14:15:13 -08002109 }
2110
Adam Cohen19f37922012-03-21 11:59:11 -07002111 /* This seems like it should be obvious and straight-forward, but when the direction vector
2112 needs to match with the notion of the dragView pushing other views, we have to employ
2113 a slightly more subtle notion of the direction vector. The question is what two points is
2114 the vector between? The center of the dragView and its desired destination? Not quite, as
2115 this doesn't necessarily coincide with the interaction of the dragView and items occupying
2116 those cells. Instead we use some heuristics to often lock the vector to up, down, left
2117 or right, which helps make pushing feel right.
2118 */
2119 private void getDirectionVectorForDrop(int dragViewCenterX, int dragViewCenterY, int spanX,
2120 int spanY, View dragView, int[] resultDirection) {
2121 int[] targetDestination = new int[2];
2122
2123 findNearestArea(dragViewCenterX, dragViewCenterY, spanX, spanY, targetDestination);
2124 Rect dragRect = new Rect();
2125 regionToRect(targetDestination[0], targetDestination[1], spanX, spanY, dragRect);
2126 dragRect.offset(dragViewCenterX - dragRect.centerX(), dragViewCenterY - dragRect.centerY());
2127
2128 Rect dropRegionRect = new Rect();
2129 getViewsIntersectingRegion(targetDestination[0], targetDestination[1], spanX, spanY,
2130 dragView, dropRegionRect, mIntersectingViews);
2131
2132 int dropRegionSpanX = dropRegionRect.width();
2133 int dropRegionSpanY = dropRegionRect.height();
2134
2135 regionToRect(dropRegionRect.left, dropRegionRect.top, dropRegionRect.width(),
2136 dropRegionRect.height(), dropRegionRect);
2137
2138 int deltaX = (dropRegionRect.centerX() - dragViewCenterX) / spanX;
2139 int deltaY = (dropRegionRect.centerY() - dragViewCenterY) / spanY;
2140
2141 if (dropRegionSpanX == mCountX || spanX == mCountX) {
2142 deltaX = 0;
2143 }
2144 if (dropRegionSpanY == mCountY || spanY == mCountY) {
2145 deltaY = 0;
2146 }
2147
2148 if (deltaX == 0 && deltaY == 0) {
2149 // No idea what to do, give a random direction.
2150 resultDirection[0] = 1;
2151 resultDirection[1] = 0;
2152 } else {
2153 computeDirectionVector(deltaX, deltaY, resultDirection);
2154 }
2155 }
2156
2157 // For a given cell and span, fetch the set of views intersecting the region.
2158 private void getViewsIntersectingRegion(int cellX, int cellY, int spanX, int spanY,
2159 View dragView, Rect boundingRect, ArrayList<View> intersectingViews) {
2160 if (boundingRect != null) {
2161 boundingRect.set(cellX, cellY, cellX + spanX, cellY + spanY);
2162 }
2163 intersectingViews.clear();
2164 Rect r0 = new Rect(cellX, cellY, cellX + spanX, cellY + spanY);
2165 Rect r1 = new Rect();
2166 final int count = mShortcutsAndWidgets.getChildCount();
2167 for (int i = 0; i < count; i++) {
2168 View child = mShortcutsAndWidgets.getChildAt(i);
2169 if (child == dragView) continue;
2170 LayoutParams lp = (LayoutParams) child.getLayoutParams();
2171 r1.set(lp.cellX, lp.cellY, lp.cellX + lp.cellHSpan, lp.cellY + lp.cellVSpan);
2172 if (Rect.intersects(r0, r1)) {
2173 mIntersectingViews.add(child);
2174 if (boundingRect != null) {
2175 boundingRect.union(r1);
2176 }
2177 }
2178 }
2179 }
2180
2181 boolean isNearestDropLocationOccupied(int pixelX, int pixelY, int spanX, int spanY,
2182 View dragView, int[] result) {
2183 result = findNearestArea(pixelX, pixelY, spanX, spanY, result);
2184 getViewsIntersectingRegion(result[0], result[1], spanX, spanY, dragView, null,
2185 mIntersectingViews);
2186 return !mIntersectingViews.isEmpty();
2187 }
2188
2189 void revertTempState() {
2190 if (!isItemPlacementDirty() || DESTRUCTIVE_REORDER) return;
2191 final int count = mShortcutsAndWidgets.getChildCount();
2192 for (int i = 0; i < count; i++) {
2193 View child = mShortcutsAndWidgets.getChildAt(i);
2194 LayoutParams lp = (LayoutParams) child.getLayoutParams();
2195 if (lp.tmpCellX != lp.cellX || lp.tmpCellY != lp.cellY) {
2196 lp.tmpCellX = lp.cellX;
2197 lp.tmpCellY = lp.cellY;
2198 animateChildToPosition(child, lp.cellX, lp.cellY, REORDER_ANIMATION_DURATION,
2199 0, false, false);
2200 }
2201 }
2202 completeAndClearReorderHintAnimations();
2203 setItemPlacementDirty(false);
2204 }
2205
Adam Cohen482ed822012-03-02 14:15:13 -08002206 int[] createArea(int pixelX, int pixelY, int minSpanX, int minSpanY, int spanX, int spanY,
2207 View dragView, int[] result, int resultSpan[], int mode) {
Adam Cohen482ed822012-03-02 14:15:13 -08002208 // First we determine if things have moved enough to cause a different layout
Adam Cohen47a876d2012-03-19 13:21:41 -07002209 result = findNearestArea(pixelX, pixelY, spanX, spanY, result);
Adam Cohen482ed822012-03-02 14:15:13 -08002210
2211 if (resultSpan == null) {
2212 resultSpan = new int[2];
2213 }
2214
Adam Cohen19f37922012-03-21 11:59:11 -07002215 // When we are checking drop validity or actually dropping, we don't recompute the
2216 // direction vector, since we want the solution to match the preview, and it's possible
2217 // that the exact position of the item has changed to result in a new reordering outcome.
2218 if ((mode == MODE_ON_DROP || mode == MODE_ACCEPT_DROP)
2219 && mPreviousReorderDirection[0] != -1) {
2220 mDirectionVector[0] = mPreviousReorderDirection[0];
2221 mDirectionVector[1] = mPreviousReorderDirection[1];
2222 // We reset this vector after drop
2223 if (mode == MODE_ON_DROP) {
2224 mPreviousReorderDirection[0] = -1;
2225 mPreviousReorderDirection[1] = -1;
2226 }
2227 } else {
2228 getDirectionVectorForDrop(pixelX, pixelY, spanX, spanY, dragView, mDirectionVector);
2229 mPreviousReorderDirection[0] = mDirectionVector[0];
2230 mPreviousReorderDirection[1] = mDirectionVector[1];
2231 }
2232
Adam Cohen482ed822012-03-02 14:15:13 -08002233 ItemConfiguration swapSolution = simpleSwap(pixelX, pixelY, minSpanX, minSpanY,
2234 spanX, spanY, mDirectionVector, dragView, true, new ItemConfiguration());
2235
2236 // We attempt the approach which doesn't shuffle views at all
2237 ItemConfiguration noShuffleSolution = findConfigurationNoShuffle(pixelX, pixelY, minSpanX,
2238 minSpanY, spanX, spanY, dragView, new ItemConfiguration());
2239
2240 ItemConfiguration finalSolution = null;
2241 if (swapSolution.isSolution && swapSolution.area() >= noShuffleSolution.area()) {
2242 finalSolution = swapSolution;
2243 } else if (noShuffleSolution.isSolution) {
2244 finalSolution = noShuffleSolution;
2245 }
2246
2247 boolean foundSolution = true;
2248 if (!DESTRUCTIVE_REORDER) {
2249 setUseTempCoords(true);
2250 }
2251
2252 if (finalSolution != null) {
2253 result[0] = finalSolution.dragViewX;
2254 result[1] = finalSolution.dragViewY;
2255 resultSpan[0] = finalSolution.dragViewSpanX;
2256 resultSpan[1] = finalSolution.dragViewSpanY;
2257
2258 // If we're just testing for a possible location (MODE_ACCEPT_DROP), we don't bother
2259 // committing anything or animating anything as we just want to determine if a solution
2260 // exists
2261 if (mode == MODE_DRAG_OVER || mode == MODE_ON_DROP || mode == MODE_ON_DROP_EXTERNAL) {
2262 if (!DESTRUCTIVE_REORDER) {
2263 copySolutionToTempState(finalSolution, dragView);
2264 }
2265 setItemPlacementDirty(true);
2266 animateItemsToSolution(finalSolution, dragView, mode == MODE_ON_DROP);
2267
Adam Cohen19f37922012-03-21 11:59:11 -07002268 if (!DESTRUCTIVE_REORDER &&
2269 (mode == MODE_ON_DROP || mode == MODE_ON_DROP_EXTERNAL)) {
Adam Cohen482ed822012-03-02 14:15:13 -08002270 commitTempPlacement();
Adam Cohen19f37922012-03-21 11:59:11 -07002271 completeAndClearReorderHintAnimations();
2272 setItemPlacementDirty(false);
2273 } else {
2274 beginOrAdjustHintAnimations(finalSolution, dragView,
2275 REORDER_ANIMATION_DURATION);
Adam Cohen482ed822012-03-02 14:15:13 -08002276 }
2277 }
2278 } else {
2279 foundSolution = false;
2280 result[0] = result[1] = resultSpan[0] = resultSpan[1] = -1;
2281 }
2282
2283 if ((mode == MODE_ON_DROP || !foundSolution) && !DESTRUCTIVE_REORDER) {
2284 setUseTempCoords(false);
2285 }
Adam Cohen482ed822012-03-02 14:15:13 -08002286
Michael Jurkaa52570f2012-03-20 03:18:20 -07002287 mShortcutsAndWidgets.requestLayout();
Adam Cohen482ed822012-03-02 14:15:13 -08002288 return result;
2289 }
2290
Adam Cohen19f37922012-03-21 11:59:11 -07002291 void setItemPlacementDirty(boolean dirty) {
2292 mItemPlacementDirty = dirty;
Adam Cohen482ed822012-03-02 14:15:13 -08002293 }
Adam Cohen19f37922012-03-21 11:59:11 -07002294 boolean isItemPlacementDirty() {
2295 return mItemPlacementDirty;
Adam Cohen482ed822012-03-02 14:15:13 -08002296 }
2297
2298 private class ItemConfiguration {
Adam Cohen8baab352012-03-20 17:39:21 -07002299 HashMap<View, CellAndSpan> map = new HashMap<View, CellAndSpan>();
Adam Cohen482ed822012-03-02 14:15:13 -08002300 boolean isSolution = false;
2301 int dragViewX, dragViewY, dragViewSpanX, dragViewSpanY;
2302
2303 int area() {
2304 return dragViewSpanX * dragViewSpanY;
2305 }
Adam Cohen8baab352012-03-20 17:39:21 -07002306 }
2307
2308 private class CellAndSpan {
2309 int x, y;
2310 int spanX, spanY;
2311
2312 public CellAndSpan(int x, int y, int spanX, int spanY) {
2313 this.x = x;
2314 this.y = y;
2315 this.spanX = spanX;
2316 this.spanY = spanY;
Adam Cohen482ed822012-03-02 14:15:13 -08002317 }
2318 }
2319
Adam Cohendf035382011-04-11 17:22:04 -07002320 /**
2321 * Find a vacant area that will fit the given bounds nearest the requested
2322 * cell location. Uses Euclidean distance to score multiple vacant areas.
2323 *
2324 * @param pixelX The X location at which you want to search for a vacant area.
2325 * @param pixelY The Y location at which you want to search for a vacant area.
2326 * @param spanX Horizontal span of the object.
2327 * @param spanY Vertical span of the object.
2328 * @param ignoreView Considers space occupied by this view as unoccupied
2329 * @param result Previously returned value to possibly recycle.
2330 * @return The X, Y cell of a vacant area that can contain this object,
2331 * nearest the requested location.
2332 */
2333 int[] findNearestVacantArea(
2334 int pixelX, int pixelY, int spanX, int spanY, View ignoreView, int[] result) {
2335 return findNearestArea(pixelX, pixelY, spanX, spanY, ignoreView, true, result);
2336 }
2337
2338 /**
Adam Cohend41fbf52012-02-16 23:53:59 -08002339 * Find a vacant area that will fit the given bounds nearest the requested
2340 * cell location. Uses Euclidean distance to score multiple vacant areas.
2341 *
2342 * @param pixelX The X location at which you want to search for a vacant area.
2343 * @param pixelY The Y location at which you want to search for a vacant area.
2344 * @param minSpanX The minimum horizontal span required
2345 * @param minSpanY The minimum vertical span required
2346 * @param spanX Horizontal span of the object.
2347 * @param spanY Vertical span of the object.
2348 * @param ignoreView Considers space occupied by this view as unoccupied
2349 * @param result Previously returned value to possibly recycle.
2350 * @return The X, Y cell of a vacant area that can contain this object,
2351 * nearest the requested location.
2352 */
2353 int[] findNearestVacantArea(int pixelX, int pixelY, int minSpanX, int minSpanY,
2354 int spanX, int spanY, View ignoreView, int[] result, int[] resultSpan) {
Adam Cohen482ed822012-03-02 14:15:13 -08002355 return findNearestArea(pixelX, pixelY, minSpanX, minSpanY, spanX, spanY, ignoreView, true,
2356 result, resultSpan, mOccupied);
Adam Cohend41fbf52012-02-16 23:53:59 -08002357 }
2358
2359 /**
Adam Cohendf035382011-04-11 17:22:04 -07002360 * Find a starting cell position that will fit the given bounds nearest the requested
2361 * cell location. Uses Euclidean distance to score multiple vacant areas.
2362 *
2363 * @param pixelX The X location at which you want to search for a vacant area.
2364 * @param pixelY The Y location at which you want to search for a vacant area.
2365 * @param spanX Horizontal span of the object.
2366 * @param spanY Vertical span of the object.
2367 * @param ignoreView Considers space occupied by this view as unoccupied
2368 * @param result Previously returned value to possibly recycle.
2369 * @return The X, Y cell of a vacant area that can contain this object,
2370 * nearest the requested location.
2371 */
2372 int[] findNearestArea(
2373 int pixelX, int pixelY, int spanX, int spanY, int[] result) {
2374 return findNearestArea(pixelX, pixelY, spanX, spanY, null, false, result);
2375 }
2376
Michael Jurka0280c3b2010-09-17 15:00:07 -07002377 boolean existsEmptyCell() {
2378 return findCellForSpan(null, 1, 1);
2379 }
2380
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002381 /**
Michael Jurka0280c3b2010-09-17 15:00:07 -07002382 * Finds the upper-left coordinate of the first rectangle in the grid that can
2383 * hold a cell of the specified dimensions. If intersectX and intersectY are not -1,
2384 * then this method will only return coordinates for rectangles that contain the cell
2385 * (intersectX, intersectY)
2386 *
2387 * @param cellXY The array that will contain the position of a vacant cell if such a cell
2388 * can be found.
2389 * @param spanX The horizontal span of the cell we want to find.
2390 * @param spanY The vertical span of the cell we want to find.
2391 *
2392 * @return True if a vacant cell of the specified dimension was found, false otherwise.
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07002393 */
Michael Jurka0280c3b2010-09-17 15:00:07 -07002394 boolean findCellForSpan(int[] cellXY, int spanX, int spanY) {
Adam Cohen482ed822012-03-02 14:15:13 -08002395 return findCellForSpanThatIntersectsIgnoring(cellXY, spanX, spanY, -1, -1, null, mOccupied);
Michael Jurka0280c3b2010-09-17 15:00:07 -07002396 }
2397
2398 /**
2399 * Like above, but ignores any cells occupied by the item "ignoreView"
2400 *
2401 * @param cellXY The array that will contain the position of a vacant cell if such a cell
2402 * can be found.
2403 * @param spanX The horizontal span of the cell we want to find.
2404 * @param spanY The vertical span of the cell we want to find.
2405 * @param ignoreView The home screen item we should treat as not occupying any space
2406 * @return
2407 */
2408 boolean findCellForSpanIgnoring(int[] cellXY, int spanX, int spanY, View ignoreView) {
Adam Cohen482ed822012-03-02 14:15:13 -08002409 return findCellForSpanThatIntersectsIgnoring(cellXY, spanX, spanY, -1, -1,
2410 ignoreView, mOccupied);
Michael Jurka0280c3b2010-09-17 15:00:07 -07002411 }
2412
2413 /**
2414 * Like above, but if intersectX and intersectY are not -1, then this method will try to
2415 * return coordinates for rectangles that contain the cell [intersectX, intersectY]
2416 *
2417 * @param spanX The horizontal span of the cell we want to find.
2418 * @param spanY The vertical span of the cell we want to find.
2419 * @param ignoreView The home screen item we should treat as not occupying any space
2420 * @param intersectX The X coordinate of the cell that we should try to overlap
2421 * @param intersectX The Y coordinate of the cell that we should try to overlap
2422 *
2423 * @return True if a vacant cell of the specified dimension was found, false otherwise.
2424 */
2425 boolean findCellForSpanThatIntersects(int[] cellXY, int spanX, int spanY,
2426 int intersectX, int intersectY) {
2427 return findCellForSpanThatIntersectsIgnoring(
Adam Cohen482ed822012-03-02 14:15:13 -08002428 cellXY, spanX, spanY, intersectX, intersectY, null, mOccupied);
Michael Jurka0280c3b2010-09-17 15:00:07 -07002429 }
2430
2431 /**
2432 * The superset of the above two methods
2433 */
2434 boolean findCellForSpanThatIntersectsIgnoring(int[] cellXY, int spanX, int spanY,
Adam Cohen482ed822012-03-02 14:15:13 -08002435 int intersectX, int intersectY, View ignoreView, boolean occupied[][]) {
Michael Jurkac6ee42e2010-09-30 12:04:50 -07002436 // mark space take by ignoreView as available (method checks if ignoreView is null)
Adam Cohen482ed822012-03-02 14:15:13 -08002437 markCellsAsUnoccupiedForView(ignoreView, occupied);
Michael Jurka0280c3b2010-09-17 15:00:07 -07002438
Michael Jurka28750fb2010-09-24 17:43:49 -07002439 boolean foundCell = false;
Michael Jurka0280c3b2010-09-17 15:00:07 -07002440 while (true) {
2441 int startX = 0;
2442 if (intersectX >= 0) {
2443 startX = Math.max(startX, intersectX - (spanX - 1));
2444 }
2445 int endX = mCountX - (spanX - 1);
2446 if (intersectX >= 0) {
2447 endX = Math.min(endX, intersectX + (spanX - 1) + (spanX == 1 ? 1 : 0));
2448 }
2449 int startY = 0;
2450 if (intersectY >= 0) {
2451 startY = Math.max(startY, intersectY - (spanY - 1));
2452 }
2453 int endY = mCountY - (spanY - 1);
2454 if (intersectY >= 0) {
2455 endY = Math.min(endY, intersectY + (spanY - 1) + (spanY == 1 ? 1 : 0));
2456 }
2457
Winson Chungbbc60d82010-11-11 16:34:41 -08002458 for (int y = startY; y < endY && !foundCell; y++) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002459 inner:
Winson Chungbbc60d82010-11-11 16:34:41 -08002460 for (int x = startX; x < endX; x++) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002461 for (int i = 0; i < spanX; i++) {
2462 for (int j = 0; j < spanY; j++) {
Adam Cohen482ed822012-03-02 14:15:13 -08002463 if (occupied[x + i][y + j]) {
Winson Chungbbc60d82010-11-11 16:34:41 -08002464 // small optimization: we can skip to after the column we just found
Michael Jurka0280c3b2010-09-17 15:00:07 -07002465 // an occupied cell
Winson Chungbbc60d82010-11-11 16:34:41 -08002466 x += i;
Michael Jurka0280c3b2010-09-17 15:00:07 -07002467 continue inner;
2468 }
2469 }
2470 }
2471 if (cellXY != null) {
2472 cellXY[0] = x;
2473 cellXY[1] = y;
2474 }
Michael Jurka28750fb2010-09-24 17:43:49 -07002475 foundCell = true;
2476 break;
Michael Jurka0280c3b2010-09-17 15:00:07 -07002477 }
2478 }
2479 if (intersectX == -1 && intersectY == -1) {
2480 break;
2481 } else {
2482 // if we failed to find anything, try again but without any requirements of
2483 // intersecting
2484 intersectX = -1;
2485 intersectY = -1;
2486 continue;
2487 }
2488 }
2489
Michael Jurkac6ee42e2010-09-30 12:04:50 -07002490 // re-mark space taken by ignoreView as occupied
Adam Cohen482ed822012-03-02 14:15:13 -08002491 markCellsAsOccupiedForView(ignoreView, occupied);
Michael Jurka28750fb2010-09-24 17:43:49 -07002492 return foundCell;
Michael Jurka0280c3b2010-09-17 15:00:07 -07002493 }
2494
2495 /**
Winson Chungc07918d2011-07-01 15:35:26 -07002496 * A drag event has begun over this layout.
2497 * It may have begun over this layout (in which case onDragChild is called first),
2498 * or it may have begun on another layout.
2499 */
2500 void onDragEnter() {
2501 if (!mDragging) {
2502 // Fade in the drag indicators
2503 if (mCrosshairsAnimator != null) {
2504 mCrosshairsAnimator.animateIn();
2505 }
2506 }
2507 mDragging = true;
2508 }
2509
2510 /**
Michael Jurka0280c3b2010-09-17 15:00:07 -07002511 * Called when drag has left this CellLayout or has been completed (successfully or not)
2512 */
2513 void onDragExit() {
Joe Onorato4be866d2010-10-10 11:26:02 -07002514 // This can actually be called when we aren't in a drag, e.g. when adding a new
2515 // item to this layout via the customize drawer.
2516 // Guard against that case.
2517 if (mDragging) {
2518 mDragging = false;
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07002519
Joe Onorato4be866d2010-10-10 11:26:02 -07002520 // Fade out the drag indicators
2521 if (mCrosshairsAnimator != null) {
2522 mCrosshairsAnimator.animateOut();
2523 }
Patrick Dubroyde7658b2010-09-27 11:15:43 -07002524 }
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -07002525
2526 // Invalidate the drag data
Adam Cohend41fbf52012-02-16 23:53:59 -08002527 mDragCell[0] = mDragCell[1] = -1;
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -07002528 mDragOutlineAnims[mDragOutlineCurrent].animateOut();
2529 mDragOutlineCurrent = (mDragOutlineCurrent + 1) % mDragOutlineAnims.length;
Adam Cohen19f37922012-03-21 11:59:11 -07002530 revertTempState();
Michael Jurka33945b22010-12-21 18:19:38 -08002531 setIsDragOverlapping(false);
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07002532 }
2533
2534 /**
Winson Chungaafa03c2010-06-11 17:34:16 -07002535 * Mark a child as having been dropped.
Patrick Dubroyde7658b2010-09-27 11:15:43 -07002536 * At the beginning of the drag operation, the child may have been on another
Patrick Dubroyce34a972010-10-19 10:34:32 -07002537 * screen, but it is re-parented before this method is called.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002538 *
2539 * @param child The child that is being dropped
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002540 */
Adam Cohen716b51e2011-06-30 12:09:54 -07002541 void onDropChild(View child) {
Romain Guyd94533d2009-08-17 10:01:15 -07002542 if (child != null) {
2543 LayoutParams lp = (LayoutParams) child.getLayoutParams();
Romain Guy84f296c2009-11-04 15:00:44 -08002544 lp.dropped = true;
Romain Guyd94533d2009-08-17 10:01:15 -07002545 child.requestLayout();
Romain Guyd94533d2009-08-17 10:01:15 -07002546 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002547 }
2548
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002549 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002550 * Computes a bounding rectangle for a range of cells
Winson Chungaafa03c2010-06-11 17:34:16 -07002551 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002552 * @param cellX X coordinate of upper left corner expressed as a cell position
2553 * @param cellY Y coordinate of upper left corner expressed as a cell position
Winson Chungaafa03c2010-06-11 17:34:16 -07002554 * @param cellHSpan Width in cells
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002555 * @param cellVSpan Height in cells
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07002556 * @param resultRect Rect into which to put the results
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002557 */
Adam Cohend41fbf52012-02-16 23:53:59 -08002558 public void cellToRect(int cellX, int cellY, int cellHSpan, int cellVSpan, Rect resultRect) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002559 final int cellWidth = mCellWidth;
2560 final int cellHeight = mCellHeight;
2561 final int widthGap = mWidthGap;
2562 final int heightGap = mHeightGap;
Winson Chungaafa03c2010-06-11 17:34:16 -07002563
Winson Chung4b825dcd2011-06-19 12:41:22 -07002564 final int hStartPadding = getPaddingLeft();
2565 final int vStartPadding = getPaddingTop();
Winson Chungaafa03c2010-06-11 17:34:16 -07002566
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002567 int width = cellHSpan * cellWidth + ((cellHSpan - 1) * widthGap);
2568 int height = cellVSpan * cellHeight + ((cellVSpan - 1) * heightGap);
2569
2570 int x = hStartPadding + cellX * (cellWidth + widthGap);
2571 int y = vStartPadding + cellY * (cellHeight + heightGap);
Winson Chungaafa03c2010-06-11 17:34:16 -07002572
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07002573 resultRect.set(x, y, x + width, y + height);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002574 }
Winson Chungaafa03c2010-06-11 17:34:16 -07002575
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002576 /**
Winson Chungaafa03c2010-06-11 17:34:16 -07002577 * Computes the required horizontal and vertical cell spans to always
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002578 * fit the given rectangle.
Winson Chungaafa03c2010-06-11 17:34:16 -07002579 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002580 * @param width Width in pixels
2581 * @param height Height in pixels
Patrick Dubroy8f86ddc2010-07-16 13:55:32 -07002582 * @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 -08002583 */
Patrick Dubroy8f86ddc2010-07-16 13:55:32 -07002584 public int[] rectToCell(int width, int height, int[] result) {
Michael Jurka9987a5c2010-10-08 16:58:12 -07002585 return rectToCell(getResources(), width, height, result);
2586 }
2587
2588 public static int[] rectToCell(Resources resources, int width, int height, int[] result) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002589 // Always assume we're working with the smallest span to make sure we
2590 // reserve enough space in both orientations.
Joe Onorato79e56262009-09-21 15:23:04 -04002591 int actualWidth = resources.getDimensionPixelSize(R.dimen.workspace_cell_width);
2592 int actualHeight = resources.getDimensionPixelSize(R.dimen.workspace_cell_height);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002593 int smallerSize = Math.min(actualWidth, actualHeight);
Joe Onorato79e56262009-09-21 15:23:04 -04002594
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002595 // Always round up to next largest cell
Winson Chung54c725c2011-08-03 12:03:40 -07002596 int spanX = (int) Math.ceil(width / (float) smallerSize);
2597 int spanY = (int) Math.ceil(height / (float) smallerSize);
Joe Onorato79e56262009-09-21 15:23:04 -04002598
Patrick Dubroy8f86ddc2010-07-16 13:55:32 -07002599 if (result == null) {
2600 return new int[] { spanX, spanY };
2601 }
2602 result[0] = spanX;
2603 result[1] = spanY;
2604 return result;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002605 }
2606
Michael Jurkaf12c75c2011-01-25 22:41:40 -08002607 public int[] cellSpansToSize(int hSpans, int vSpans) {
2608 int[] size = new int[2];
2609 size[0] = hSpans * mCellWidth + (hSpans - 1) * mWidthGap;
2610 size[1] = vSpans * mCellHeight + (vSpans - 1) * mHeightGap;
2611 return size;
2612 }
2613
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002614 /**
Patrick Dubroy047379a2010-12-19 22:02:04 -08002615 * Calculate the grid spans needed to fit given item
2616 */
2617 public void calculateSpans(ItemInfo info) {
2618 final int minWidth;
2619 final int minHeight;
2620
2621 if (info instanceof LauncherAppWidgetInfo) {
2622 minWidth = ((LauncherAppWidgetInfo) info).minWidth;
2623 minHeight = ((LauncherAppWidgetInfo) info).minHeight;
2624 } else if (info instanceof PendingAddWidgetInfo) {
2625 minWidth = ((PendingAddWidgetInfo) info).minWidth;
2626 minHeight = ((PendingAddWidgetInfo) info).minHeight;
2627 } else {
2628 // It's not a widget, so it must be 1x1
2629 info.spanX = info.spanY = 1;
2630 return;
2631 }
2632 int[] spans = rectToCell(minWidth, minHeight, null);
2633 info.spanX = spans[0];
2634 info.spanY = spans[1];
2635 }
2636
2637 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002638 * Find the first vacant cell, if there is one.
2639 *
2640 * @param vacant Holds the x and y coordinate of the vacant cell
2641 * @param spanX Horizontal cell span.
2642 * @param spanY Vertical cell span.
Winson Chungaafa03c2010-06-11 17:34:16 -07002643 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002644 * @return True if a vacant cell was found
2645 */
2646 public boolean getVacantCell(int[] vacant, int spanX, int spanY) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002647
Michael Jurka0280c3b2010-09-17 15:00:07 -07002648 return findVacantCell(vacant, spanX, spanY, mCountX, mCountY, mOccupied);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002649 }
2650
2651 static boolean findVacantCell(int[] vacant, int spanX, int spanY,
2652 int xCount, int yCount, boolean[][] occupied) {
2653
Adam Cohen2801caf2011-05-13 20:57:39 -07002654 for (int y = 0; y < yCount; y++) {
2655 for (int x = 0; x < xCount; x++) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002656 boolean available = !occupied[x][y];
2657out: for (int i = x; i < x + spanX - 1 && x < xCount; i++) {
2658 for (int j = y; j < y + spanY - 1 && y < yCount; j++) {
2659 available = available && !occupied[i][j];
2660 if (!available) break out;
2661 }
2662 }
2663
2664 if (available) {
2665 vacant[0] = x;
2666 vacant[1] = y;
2667 return true;
2668 }
2669 }
2670 }
2671
2672 return false;
2673 }
2674
Michael Jurka0280c3b2010-09-17 15:00:07 -07002675 private void clearOccupiedCells() {
2676 for (int x = 0; x < mCountX; x++) {
2677 for (int y = 0; y < mCountY; y++) {
2678 mOccupied[x][y] = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002679 }
2680 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07002681 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002682
Adam Cohen1b607ed2011-03-03 17:26:50 -08002683 /**
2684 * Given a view, determines how much that view can be expanded in all directions, in terms of
2685 * whether or not there are other items occupying adjacent cells. Used by the
2686 * AppWidgetResizeFrame to determine how the widget can be resized.
2687 */
Adam Cohend4844c32011-02-18 19:25:06 -08002688 public void getExpandabilityArrayForView(View view, int[] expandability) {
Adam Cohen1b607ed2011-03-03 17:26:50 -08002689 final LayoutParams lp = (LayoutParams) view.getLayoutParams();
Adam Cohend4844c32011-02-18 19:25:06 -08002690 boolean flag;
2691
Adam Cohen1b607ed2011-03-03 17:26:50 -08002692 expandability[AppWidgetResizeFrame.LEFT] = 0;
Adam Cohend4844c32011-02-18 19:25:06 -08002693 for (int x = lp.cellX - 1; x >= 0; x--) {
2694 flag = false;
2695 for (int y = lp.cellY; y < lp.cellY + lp.cellVSpan; y++) {
2696 if (mOccupied[x][y]) flag = true;
2697 }
2698 if (flag) break;
Adam Cohen1b607ed2011-03-03 17:26:50 -08002699 expandability[AppWidgetResizeFrame.LEFT]++;
Adam Cohend4844c32011-02-18 19:25:06 -08002700 }
2701
Adam Cohen1b607ed2011-03-03 17:26:50 -08002702 expandability[AppWidgetResizeFrame.TOP] = 0;
Adam Cohend4844c32011-02-18 19:25:06 -08002703 for (int y = lp.cellY - 1; y >= 0; y--) {
2704 flag = false;
2705 for (int x = lp.cellX; x < lp.cellX + lp.cellHSpan; x++) {
2706 if (mOccupied[x][y]) flag = true;
2707 }
2708 if (flag) break;
Adam Cohen1b607ed2011-03-03 17:26:50 -08002709 expandability[AppWidgetResizeFrame.TOP]++;
2710 }
Adam Cohend4844c32011-02-18 19:25:06 -08002711
Adam Cohen1b607ed2011-03-03 17:26:50 -08002712 expandability[AppWidgetResizeFrame.RIGHT] = 0;
Adam Cohend4844c32011-02-18 19:25:06 -08002713 for (int x = lp.cellX + lp.cellHSpan; x < mCountX; x++) {
2714 flag = false;
2715 for (int y = lp.cellY; y < lp.cellY + lp.cellVSpan; y++) {
2716 if (mOccupied[x][y]) flag = true;
2717 }
2718 if (flag) break;
Adam Cohen1b607ed2011-03-03 17:26:50 -08002719 expandability[AppWidgetResizeFrame.RIGHT]++;
2720 }
Adam Cohend4844c32011-02-18 19:25:06 -08002721
Adam Cohen1b607ed2011-03-03 17:26:50 -08002722 expandability[AppWidgetResizeFrame.BOTTOM] = 0;
Adam Cohend4844c32011-02-18 19:25:06 -08002723 for (int y = lp.cellY + lp.cellVSpan; y < mCountY; y++) {
2724 flag = false;
2725 for (int x = lp.cellX; x < lp.cellX + lp.cellHSpan; x++) {
2726 if (mOccupied[x][y]) flag = true;
2727 }
2728 if (flag) break;
Adam Cohen1b607ed2011-03-03 17:26:50 -08002729 expandability[AppWidgetResizeFrame.BOTTOM]++;
2730 }
Adam Cohend4844c32011-02-18 19:25:06 -08002731 }
2732
Adam Cohend41fbf52012-02-16 23:53:59 -08002733 public void onMove(View view, int newCellX, int newCellY, int newSpanX, int newSpanY) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002734 markCellsAsUnoccupiedForView(view);
Adam Cohen482ed822012-03-02 14:15:13 -08002735 markCellsForView(newCellX, newCellY, newSpanX, newSpanY, mOccupied, true);
Michael Jurka0280c3b2010-09-17 15:00:07 -07002736 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002737
Adam Cohend4844c32011-02-18 19:25:06 -08002738 public void markCellsAsOccupiedForView(View view) {
Adam Cohen482ed822012-03-02 14:15:13 -08002739 markCellsAsOccupiedForView(view, mOccupied);
2740 }
2741 public void markCellsAsOccupiedForView(View view, boolean[][] occupied) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07002742 if (view == null || view.getParent() != mShortcutsAndWidgets) return;
Michael Jurka0280c3b2010-09-17 15:00:07 -07002743 LayoutParams lp = (LayoutParams) view.getLayoutParams();
Adam Cohen482ed822012-03-02 14:15:13 -08002744 markCellsForView(lp.cellX, lp.cellY, lp.cellHSpan, lp.cellVSpan, occupied, true);
Michael Jurka0280c3b2010-09-17 15:00:07 -07002745 }
2746
Adam Cohend4844c32011-02-18 19:25:06 -08002747 public void markCellsAsUnoccupiedForView(View view) {
Adam Cohen482ed822012-03-02 14:15:13 -08002748 markCellsAsUnoccupiedForView(view, mOccupied);
2749 }
2750 public void markCellsAsUnoccupiedForView(View view, boolean occupied[][]) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07002751 if (view == null || view.getParent() != mShortcutsAndWidgets) return;
Michael Jurka0280c3b2010-09-17 15:00:07 -07002752 LayoutParams lp = (LayoutParams) view.getLayoutParams();
Adam Cohen482ed822012-03-02 14:15:13 -08002753 markCellsForView(lp.cellX, lp.cellY, lp.cellHSpan, lp.cellVSpan, occupied, false);
Michael Jurka0280c3b2010-09-17 15:00:07 -07002754 }
2755
Adam Cohen482ed822012-03-02 14:15:13 -08002756 private void markCellsForView(int cellX, int cellY, int spanX, int spanY, boolean[][] occupied,
2757 boolean value) {
2758 if (cellX < 0 || cellY < 0) return;
Michael Jurka0280c3b2010-09-17 15:00:07 -07002759 for (int x = cellX; x < cellX + spanX && x < mCountX; x++) {
2760 for (int y = cellY; y < cellY + spanY && y < mCountY; y++) {
Adam Cohen482ed822012-03-02 14:15:13 -08002761 occupied[x][y] = value;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002762 }
2763 }
2764 }
2765
Adam Cohen2801caf2011-05-13 20:57:39 -07002766 public int getDesiredWidth() {
Winson Chung4b825dcd2011-06-19 12:41:22 -07002767 return mPaddingLeft + mPaddingRight + (mCountX * mCellWidth) +
Adam Cohen2801caf2011-05-13 20:57:39 -07002768 (Math.max((mCountX - 1), 0) * mWidthGap);
2769 }
2770
2771 public int getDesiredHeight() {
Winson Chung4b825dcd2011-06-19 12:41:22 -07002772 return mPaddingTop + mPaddingBottom + (mCountY * mCellHeight) +
Adam Cohen2801caf2011-05-13 20:57:39 -07002773 (Math.max((mCountY - 1), 0) * mHeightGap);
2774 }
2775
Michael Jurka66d72172011-04-12 16:29:25 -07002776 public boolean isOccupied(int x, int y) {
2777 if (x < mCountX && y < mCountY) {
2778 return mOccupied[x][y];
2779 } else {
2780 throw new RuntimeException("Position exceeds the bound of this CellLayout");
2781 }
2782 }
2783
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002784 @Override
2785 public ViewGroup.LayoutParams generateLayoutParams(AttributeSet attrs) {
2786 return new CellLayout.LayoutParams(getContext(), attrs);
2787 }
2788
2789 @Override
2790 protected boolean checkLayoutParams(ViewGroup.LayoutParams p) {
2791 return p instanceof CellLayout.LayoutParams;
2792 }
2793
2794 @Override
2795 protected ViewGroup.LayoutParams generateLayoutParams(ViewGroup.LayoutParams p) {
2796 return new CellLayout.LayoutParams(p);
2797 }
2798
Winson Chungaafa03c2010-06-11 17:34:16 -07002799 public static class CellLayoutAnimationController extends LayoutAnimationController {
2800 public CellLayoutAnimationController(Animation animation, float delay) {
2801 super(animation, delay);
2802 }
2803
2804 @Override
2805 protected long getDelayForView(View view) {
2806 return (int) (Math.random() * 150);
2807 }
2808 }
2809
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002810 public static class LayoutParams extends ViewGroup.MarginLayoutParams {
2811 /**
2812 * Horizontal location of the item in the grid.
2813 */
2814 @ViewDebug.ExportedProperty
2815 public int cellX;
2816
2817 /**
2818 * Vertical location of the item in the grid.
2819 */
2820 @ViewDebug.ExportedProperty
2821 public int cellY;
2822
2823 /**
Adam Cohen482ed822012-03-02 14:15:13 -08002824 * Temporary horizontal location of the item in the grid during reorder
2825 */
2826 public int tmpCellX;
2827
2828 /**
2829 * Temporary vertical location of the item in the grid during reorder
2830 */
2831 public int tmpCellY;
2832
2833 /**
2834 * Indicates that the temporary coordinates should be used to layout the items
2835 */
2836 public boolean useTmpCoords;
2837
2838 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002839 * Number of cells spanned horizontally by the item.
2840 */
2841 @ViewDebug.ExportedProperty
2842 public int cellHSpan;
2843
2844 /**
2845 * Number of cells spanned vertically by the item.
2846 */
2847 @ViewDebug.ExportedProperty
2848 public int cellVSpan;
Winson Chungaafa03c2010-06-11 17:34:16 -07002849
Adam Cohen1b607ed2011-03-03 17:26:50 -08002850 /**
2851 * Indicates whether the item will set its x, y, width and height parameters freely,
2852 * or whether these will be computed based on cellX, cellY, cellHSpan and cellVSpan.
2853 */
Adam Cohend4844c32011-02-18 19:25:06 -08002854 public boolean isLockedToGrid = true;
2855
Adam Cohen482ed822012-03-02 14:15:13 -08002856 /**
2857 * Indicates whether this item can be reordered. Always true except in the case of the
2858 * the AllApps button.
2859 */
2860 public boolean canReorder = true;
2861
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002862 // X coordinate of the view in the layout.
2863 @ViewDebug.ExportedProperty
2864 int x;
2865 // Y coordinate of the view in the layout.
2866 @ViewDebug.ExportedProperty
2867 int y;
2868
Romain Guy84f296c2009-11-04 15:00:44 -08002869 boolean dropped;
Romain Guyfcb9e712009-10-02 16:06:52 -07002870
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002871 public LayoutParams(Context c, AttributeSet attrs) {
2872 super(c, attrs);
2873 cellHSpan = 1;
2874 cellVSpan = 1;
2875 }
2876
2877 public LayoutParams(ViewGroup.LayoutParams source) {
2878 super(source);
2879 cellHSpan = 1;
2880 cellVSpan = 1;
2881 }
Winson Chungaafa03c2010-06-11 17:34:16 -07002882
2883 public LayoutParams(LayoutParams source) {
2884 super(source);
2885 this.cellX = source.cellX;
2886 this.cellY = source.cellY;
2887 this.cellHSpan = source.cellHSpan;
2888 this.cellVSpan = source.cellVSpan;
2889 }
2890
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002891 public LayoutParams(int cellX, int cellY, int cellHSpan, int cellVSpan) {
Romain Guy8f19cdd2010-01-08 15:07:00 -08002892 super(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002893 this.cellX = cellX;
2894 this.cellY = cellY;
2895 this.cellHSpan = cellHSpan;
2896 this.cellVSpan = cellVSpan;
2897 }
2898
Adam Cohen7f4eabe2011-04-21 16:19:16 -07002899 public void setup(int cellWidth, int cellHeight, int widthGap, int heightGap) {
Adam Cohend4844c32011-02-18 19:25:06 -08002900 if (isLockedToGrid) {
2901 final int myCellHSpan = cellHSpan;
2902 final int myCellVSpan = cellVSpan;
Adam Cohen482ed822012-03-02 14:15:13 -08002903 final int myCellX = useTmpCoords ? tmpCellX : cellX;
2904 final int myCellY = useTmpCoords ? tmpCellY : cellY;
Adam Cohen1b607ed2011-03-03 17:26:50 -08002905
Adam Cohend4844c32011-02-18 19:25:06 -08002906 width = myCellHSpan * cellWidth + ((myCellHSpan - 1) * widthGap) -
2907 leftMargin - rightMargin;
2908 height = myCellVSpan * cellHeight + ((myCellVSpan - 1) * heightGap) -
2909 topMargin - bottomMargin;
Winson Chungeecf02d2012-03-02 17:14:58 -08002910 x = (int) (myCellX * (cellWidth + widthGap) + leftMargin);
2911 y = (int) (myCellY * (cellHeight + heightGap) + topMargin);
Adam Cohend4844c32011-02-18 19:25:06 -08002912 }
2913 }
Winson Chungaafa03c2010-06-11 17:34:16 -07002914
Winson Chungaafa03c2010-06-11 17:34:16 -07002915 public String toString() {
2916 return "(" + this.cellX + ", " + this.cellY + ")";
2917 }
Adam Cohen7f4eabe2011-04-21 16:19:16 -07002918
2919 public void setWidth(int width) {
2920 this.width = width;
2921 }
2922
2923 public int getWidth() {
2924 return width;
2925 }
2926
2927 public void setHeight(int height) {
2928 this.height = height;
2929 }
2930
2931 public int getHeight() {
2932 return height;
2933 }
2934
2935 public void setX(int x) {
2936 this.x = x;
2937 }
2938
2939 public int getX() {
2940 return x;
2941 }
2942
2943 public void setY(int y) {
2944 this.y = y;
2945 }
2946
2947 public int getY() {
2948 return y;
2949 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002950 }
2951
Michael Jurka0280c3b2010-09-17 15:00:07 -07002952 // This class stores info for two purposes:
2953 // 1. When dragging items (mDragInfo in Workspace), we store the View, its cellX & cellY,
2954 // its spanX, spanY, and the screen it is on
2955 // 2. When long clicking on an empty cell in a CellLayout, we save information about the
2956 // cellX and cellY coordinates and which page was clicked. We then set this as a tag on
2957 // the CellLayout that was long clicked
Michael Jurkae5fb0f22011-04-11 13:27:46 -07002958 static final class CellInfo {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002959 View cell;
Michael Jurkaa63c4522010-08-19 13:52:27 -07002960 int cellX = -1;
2961 int cellY = -1;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002962 int spanX;
2963 int spanY;
2964 int screen;
Winson Chung3d503fb2011-07-13 17:25:49 -07002965 long container;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002966
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002967 @Override
2968 public String toString() {
Winson Chungaafa03c2010-06-11 17:34:16 -07002969 return "Cell[view=" + (cell == null ? "null" : cell.getClass())
2970 + ", x=" + cellX + ", y=" + cellY + "]";
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002971 }
2972 }
Michael Jurkad771c962011-08-09 15:00:48 -07002973
2974 public boolean lastDownOnOccupiedCell() {
2975 return mLastDownOnOccupiedCell;
2976 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002977}