blob: 4a82799de97f0fbb8ab86d32b6571f80bf656496 [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
Michael Jurka3c4c20f2010-10-28 15:36:06 -070019import com.android.launcher.R;
Winson Chungaafa03c2010-06-11 17:34:16 -070020
Joe Onorato4be866d2010-10-10 11:26:02 -070021import android.animation.Animator;
22import android.animation.AnimatorListenerAdapter;
Michael Jurka18014792010-10-14 09:01:34 -070023import android.animation.AnimatorSet;
24import android.animation.ObjectAnimator;
Chet Haase00397b12010-10-07 11:13:10 -070025import android.animation.TimeInterpolator;
Patrick Dubroyde7658b2010-09-27 11:15:43 -070026import android.animation.ValueAnimator;
27import android.animation.ValueAnimator.AnimatorUpdateListener;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080028import android.content.Context;
Joe Onorato79e56262009-09-21 15:23:04 -040029import android.content.res.Resources;
Winson Chungaafa03c2010-06-11 17:34:16 -070030import android.content.res.TypedArray;
Joe Onorato4be866d2010-10-10 11:26:02 -070031import android.graphics.Bitmap;
Winson Chungaafa03c2010-06-11 17:34:16 -070032import android.graphics.Canvas;
Joe Onorato4be866d2010-10-10 11:26:02 -070033import android.graphics.Paint;
Patrick Dubroyde7658b2010-09-27 11:15:43 -070034import android.graphics.Point;
35import android.graphics.PointF;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080036import android.graphics.Rect;
37import android.graphics.RectF;
Michael Jurka18014792010-10-14 09:01:34 -070038import android.graphics.Region;
Patrick Dubroy6569f2c2010-07-12 14:25:18 -070039import android.graphics.drawable.Drawable;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080040import android.util.AttributeSet;
Joe Onorato4be866d2010-10-10 11:26:02 -070041import android.util.Log;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080042import android.view.ContextMenu;
43import android.view.MotionEvent;
44import android.view.View;
45import android.view.ViewDebug;
46import android.view.ViewGroup;
Winson Chungaafa03c2010-06-11 17:34:16 -070047import android.view.animation.Animation;
Winson Chung150fbab2010-09-29 17:14:26 -070048import android.view.animation.DecelerateInterpolator;
Winson Chungaafa03c2010-06-11 17:34:16 -070049import android.view.animation.LayoutAnimationController;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080050
Michael Jurka3c4c20f2010-10-28 15:36:06 -070051import java.util.Arrays;
Patrick Dubroy8e58e912010-10-14 13:21:48 -070052
Michael Jurkabdb5c532011-02-01 15:05:06 -080053public class CellLayout extends ViewGroup {
Winson Chungaafa03c2010-06-11 17:34:16 -070054 static final String TAG = "CellLayout";
55
The Android Open Source Project31dd5032009-03-03 19:32:27 -080056 private int mCellWidth;
57 private int mCellHeight;
Winson Chungaafa03c2010-06-11 17:34:16 -070058
Winson Chungaafa03c2010-06-11 17:34:16 -070059 private int mLeftPadding;
60 private int mRightPadding;
61 private int mTopPadding;
62 private int mBottomPadding;
63
Adam Cohend22015c2010-07-26 22:02:18 -070064 private int mCountX;
65 private int mCountY;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080066
67 private int mWidthGap;
68 private int mHeightGap;
69
70 private final Rect mRect = new Rect();
71 private final CellInfo mCellInfo = new CellInfo();
Winson Chungaafa03c2010-06-11 17:34:16 -070072
Patrick Dubroyde7658b2010-09-27 11:15:43 -070073 // These are temporary variables to prevent having to allocate a new object just to
74 // return an (x, y) value from helper functions. Do NOT use them to maintain other state.
Patrick Dubroy6569f2c2010-07-12 14:25:18 -070075 private final int[] mTmpCellXY = new int[2];
Patrick Dubroyde7658b2010-09-27 11:15:43 -070076 private final int[] mTmpPoint = new int[2];
77 private final PointF mTmpPointF = new PointF();
Patrick Dubroy6569f2c2010-07-12 14:25:18 -070078
The Android Open Source Project31dd5032009-03-03 19:32:27 -080079 boolean[][] mOccupied;
80
Michael Jurkadee05892010-07-27 10:01:56 -070081 private OnTouchListener mInterceptTouchListener;
82
Michael Jurka5f1c5092010-09-03 14:15:02 -070083 private float mBackgroundAlpha;
Adam Cohen1b0aaac2010-10-28 11:11:18 -070084 private float mBackgroundAlphaMultiplier = 1.0f;
Adam Cohenf34bab52010-09-30 14:11:56 -070085
Michael Jurka33945b22010-12-21 18:19:38 -080086 private Drawable mNormalBackground;
Michael Jurka33945b22010-12-21 18:19:38 -080087 private Drawable mActiveBackground;
88 private Drawable mActiveGlowBackground;
89 private Drawable mNormalBackgroundMini;
90 private Drawable mNormalGlowBackgroundMini;
91 private Drawable mActiveBackgroundMini;
92 private Drawable mActiveGlowBackgroundMini;
Michael Jurka18014792010-10-14 09:01:34 -070093 private Rect mBackgroundRect;
Michael Jurka33945b22010-12-21 18:19:38 -080094 private Rect mGlowBackgroundRect;
95 private float mGlowBackgroundScale;
96 private float mGlowBackgroundAlpha;
Patrick Dubroy1262e362010-10-06 15:49:50 -070097
Michael Jurka33945b22010-12-21 18:19:38 -080098 private boolean mAcceptsDrops = false;
99 // If we're actively dragging something over this screen, mIsDragOverlapping is true
100 private boolean mIsDragOverlapping = false;
101 private boolean mIsDragOccuring = false;
102 private boolean mIsDefaultDropTarget = false;
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700103 private final Point mDragCenter = new Point();
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700104
Winson Chung150fbab2010-09-29 17:14:26 -0700105 // These arrays are used to implement the drag visualization on x-large screens.
Joe Onorato4be866d2010-10-10 11:26:02 -0700106 // They are used as circular arrays, indexed by mDragOutlineCurrent.
107 private Point[] mDragOutlines = new Point[8];
Chet Haase472b2812010-10-14 07:02:04 -0700108 private float[] mDragOutlineAlphas = new float[mDragOutlines.length];
Joe Onorato4be866d2010-10-10 11:26:02 -0700109 private InterruptibleInOutAnimator[] mDragOutlineAnims =
110 new InterruptibleInOutAnimator[mDragOutlines.length];
Winson Chung150fbab2010-09-29 17:14:26 -0700111
112 // Used as an index into the above 3 arrays; indicates which is the most current value.
Joe Onorato4be866d2010-10-10 11:26:02 -0700113 private int mDragOutlineCurrent = 0;
Patrick Dubroy8e58e912010-10-14 13:21:48 -0700114 private final Paint mDragOutlinePaint = new Paint();
Winson Chung150fbab2010-09-29 17:14:26 -0700115
Patrick Dubroy3499d8c2011-03-10 17:17:23 -0800116 private BubbleTextView mPressedOrFocusedIcon;
117
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700118 private Drawable mCrosshairsDrawable = null;
Patrick Dubroy49250ad2010-10-08 15:33:52 -0700119 private InterruptibleInOutAnimator mCrosshairsAnimator = null;
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700120 private float mCrosshairsVisibility = 0.0f;
121
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700122 // When a drag operation is in progress, holds the nearest cell to the touch point
123 private final int[] mDragCell = new int[2];
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800124
Joe Onorato4be866d2010-10-10 11:26:02 -0700125 private boolean mDragging = false;
126
Patrick Dubroyce34a972010-10-19 10:34:32 -0700127 private TimeInterpolator mEaseOutInterpolator;
Michael Jurka8c920dd2011-01-20 14:16:56 -0800128 private CellLayoutChildren mChildren;
Patrick Dubroyce34a972010-10-19 10:34:32 -0700129
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800130 public CellLayout(Context context) {
131 this(context, null);
132 }
133
134 public CellLayout(Context context, AttributeSet attrs) {
135 this(context, attrs, 0);
136 }
137
138 public CellLayout(Context context, AttributeSet attrs, int defStyle) {
139 super(context, attrs, defStyle);
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700140
141 // A ViewGroup usually does not draw, but CellLayout needs to draw a rectangle to show
142 // the user where a dragged item will land when dropped.
143 setWillNotDraw(false);
Michael Jurkaa63c4522010-08-19 13:52:27 -0700144
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800145 TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.CellLayout, defStyle, 0);
146
147 mCellWidth = a.getDimensionPixelSize(R.styleable.CellLayout_cellWidth, 10);
148 mCellHeight = a.getDimensionPixelSize(R.styleable.CellLayout_cellHeight, 10);
Winson Chungece7f5b2010-10-22 14:54:12 -0700149 mWidthGap = a.getDimensionPixelSize(R.styleable.CellLayout_widthGap, -1);
150 mHeightGap = a.getDimensionPixelSize(R.styleable.CellLayout_heightGap, -1);
Winson Chungaafa03c2010-06-11 17:34:16 -0700151
Adam Cohend22015c2010-07-26 22:02:18 -0700152 mLeftPadding =
153 a.getDimensionPixelSize(R.styleable.CellLayout_xAxisStartPadding, 10);
154 mRightPadding =
155 a.getDimensionPixelSize(R.styleable.CellLayout_xAxisEndPadding, 10);
156 mTopPadding =
157 a.getDimensionPixelSize(R.styleable.CellLayout_yAxisStartPadding, 10);
158 mBottomPadding =
159 a.getDimensionPixelSize(R.styleable.CellLayout_yAxisEndPadding, 10);
Winson Chungaafa03c2010-06-11 17:34:16 -0700160
Adam Cohend22015c2010-07-26 22:02:18 -0700161 mCountX = LauncherModel.getCellCountX();
162 mCountY = LauncherModel.getCellCountY();
Michael Jurka0280c3b2010-09-17 15:00:07 -0700163 mOccupied = new boolean[mCountX][mCountY];
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800164
165 a.recycle();
166
167 setAlwaysDrawnWithCacheEnabled(false);
168
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700169 final Resources res = getResources();
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700170
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700171 if (LauncherApplication.isScreenXLarge()) {
Michael Jurka33945b22010-12-21 18:19:38 -0800172 mNormalBackground = res.getDrawable(R.drawable.homescreen_large_blue);
Michael Jurka33945b22010-12-21 18:19:38 -0800173 mActiveBackground = res.getDrawable(R.drawable.homescreen_large_green);
174 mActiveGlowBackground = res.getDrawable(R.drawable.homescreen_large_green_strong);
175
176 mNormalBackgroundMini = res.getDrawable(R.drawable.homescreen_small_blue);
177 mNormalGlowBackgroundMini = res.getDrawable(R.drawable.homescreen_small_blue_strong);
178 mActiveBackgroundMini = res.getDrawable(R.drawable.homescreen_small_green);
179 mActiveGlowBackgroundMini = res.getDrawable(R.drawable.homescreen_small_green_strong);
180
181 mNormalBackground.setFilterBitmap(true);
Michael Jurka33945b22010-12-21 18:19:38 -0800182 mActiveBackground.setFilterBitmap(true);
183 mActiveGlowBackground.setFilterBitmap(true);
184 mNormalBackgroundMini.setFilterBitmap(true);
185 mNormalGlowBackgroundMini.setFilterBitmap(true);
186 mActiveBackgroundMini.setFilterBitmap(true);
187 mActiveGlowBackgroundMini.setFilterBitmap(true);
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700188 }
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700189
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700190 // Initialize the data structures used for the drag visualization.
Winson Chung150fbab2010-09-29 17:14:26 -0700191
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700192 mCrosshairsDrawable = res.getDrawable(R.drawable.gardening_crosshairs);
Patrick Dubroyce34a972010-10-19 10:34:32 -0700193 mEaseOutInterpolator = new DecelerateInterpolator(2.5f); // Quint ease out
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700194
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700195 // Set up the animation for fading the crosshairs in and out
196 int animDuration = res.getInteger(R.integer.config_crosshairsFadeInTime);
Patrick Dubroy49250ad2010-10-08 15:33:52 -0700197 mCrosshairsAnimator = new InterruptibleInOutAnimator(animDuration, 0.0f, 1.0f);
Chet Haase472b2812010-10-14 07:02:04 -0700198 mCrosshairsAnimator.getAnimator().addUpdateListener(new AnimatorUpdateListener() {
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700199 public void onAnimationUpdate(ValueAnimator animation) {
200 mCrosshairsVisibility = ((Float) animation.getAnimatedValue()).floatValue();
Patrick Dubroy8e58e912010-10-14 13:21:48 -0700201 invalidate();
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700202 }
203 });
Patrick Dubroyce34a972010-10-19 10:34:32 -0700204 mCrosshairsAnimator.getAnimator().setInterpolator(mEaseOutInterpolator);
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700205
Joe Onorato4be866d2010-10-10 11:26:02 -0700206 for (int i = 0; i < mDragOutlines.length; i++) {
207 mDragOutlines[i] = new Point(-1, -1);
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700208 }
209
210 // When dragging things around the home screens, we show a green outline of
211 // where the item will land. The outlines gradually fade out, leaving a trail
212 // behind the drag path.
213 // Set up all the animations that are used to implement this fading.
214 final int duration = res.getInteger(R.integer.config_dragOutlineFadeTime);
Chet Haase472b2812010-10-14 07:02:04 -0700215 final float fromAlphaValue = 0;
216 final float toAlphaValue = (float)res.getInteger(R.integer.config_dragOutlineMaxAlpha);
Joe Onorato4be866d2010-10-10 11:26:02 -0700217
Patrick Dubroy8e58e912010-10-14 13:21:48 -0700218 Arrays.fill(mDragOutlineAlphas, fromAlphaValue);
Joe Onorato4be866d2010-10-10 11:26:02 -0700219
220 for (int i = 0; i < mDragOutlineAnims.length; i++) {
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700221 final InterruptibleInOutAnimator anim =
222 new InterruptibleInOutAnimator(duration, fromAlphaValue, toAlphaValue);
Patrick Dubroyce34a972010-10-19 10:34:32 -0700223 anim.getAnimator().setInterpolator(mEaseOutInterpolator);
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700224 final int thisIndex = i;
Chet Haase472b2812010-10-14 07:02:04 -0700225 anim.getAnimator().addUpdateListener(new AnimatorUpdateListener() {
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700226 public void onAnimationUpdate(ValueAnimator animation) {
Joe Onorato4be866d2010-10-10 11:26:02 -0700227 final Bitmap outline = (Bitmap)anim.getTag();
228
229 // If an animation is started and then stopped very quickly, we can still
230 // get spurious updates we've cleared the tag. Guard against this.
231 if (outline == null) {
Patrick Dubroyfe6bd872010-10-13 17:32:10 -0700232 if (false) {
233 Object val = animation.getAnimatedValue();
234 Log.d(TAG, "anim " + thisIndex + " update: " + val +
235 ", isStopped " + anim.isStopped());
236 }
Joe Onorato4be866d2010-10-10 11:26:02 -0700237 // Try to prevent it from continuing to run
238 animation.cancel();
239 } else {
Chet Haase472b2812010-10-14 07:02:04 -0700240 mDragOutlineAlphas[thisIndex] = (Float) animation.getAnimatedValue();
Joe Onorato4be866d2010-10-10 11:26:02 -0700241 final int left = mDragOutlines[thisIndex].x;
242 final int top = mDragOutlines[thisIndex].y;
243 CellLayout.this.invalidate(left, top,
244 left + outline.getWidth(), top + outline.getHeight());
245 }
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700246 }
247 });
Joe Onorato4be866d2010-10-10 11:26:02 -0700248 // The animation holds a reference to the drag outline bitmap as long is it's
249 // running. This way the bitmap can be GCed when the animations are complete.
Chet Haase472b2812010-10-14 07:02:04 -0700250 anim.getAnimator().addListener(new AnimatorListenerAdapter() {
Michael Jurka3c4c20f2010-10-28 15:36:06 -0700251 @Override
Joe Onorato4be866d2010-10-10 11:26:02 -0700252 public void onAnimationEnd(Animator animation) {
Chet Haase472b2812010-10-14 07:02:04 -0700253 if ((Float) ((ValueAnimator) animation).getAnimatedValue() == 0f) {
Joe Onorato4be866d2010-10-10 11:26:02 -0700254 anim.setTag(null);
255 }
256 }
257 });
258 mDragOutlineAnims[i] = anim;
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700259 }
Patrick Dubroyce34a972010-10-19 10:34:32 -0700260
Michael Jurka18014792010-10-14 09:01:34 -0700261 mBackgroundRect = new Rect();
Michael Jurka33945b22010-12-21 18:19:38 -0800262 mGlowBackgroundRect = new Rect();
Michael Jurka18014792010-10-14 09:01:34 -0700263 setHoverScale(1.0f);
264 setHoverAlpha(1.0f);
Michael Jurkabea15192010-11-17 12:33:46 -0800265
Michael Jurka8c920dd2011-01-20 14:16:56 -0800266 mChildren = new CellLayoutChildren(context);
267 mChildren.setCellDimensions(
268 mCellWidth, mCellHeight, mLeftPadding, mTopPadding, mWidthGap, mHeightGap);
269 addView(mChildren);
Michael Jurka18014792010-10-14 09:01:34 -0700270 }
271
Michael Jurka38d1b0b2011-04-05 14:50:34 -0700272 static int widthInPortrait(Resources r, int numCells) {
273 // We use this method from Workspace to figure out how many rows/columns Launcher should
274 // have. We ignore the left/right padding on CellLayout because it turns out in our design
275 // the padding extends outside the visible screen size, but it looked fine anyway.
Michael Jurka38d1b0b2011-04-05 14:50:34 -0700276 int cellWidth = r.getDimensionPixelSize(R.dimen.workspace_cell_width);
277 int widthGap = r.getDimensionPixelSize(R.dimen.workspace_width_gap_port);
Michael Jurka38d1b0b2011-04-05 14:50:34 -0700278
Michael Jurka32271c32011-06-01 17:03:13 -0700279 return widthGap * (numCells - 1) + cellWidth * numCells;
Michael Jurka38d1b0b2011-04-05 14:50:34 -0700280 }
281
Michael Jurka38d1b0b2011-04-05 14:50:34 -0700282 static int heightInLandscape(Resources r, int numCells) {
283 // We use this method from Workspace to figure out how many rows/columns Launcher should
284 // have. We ignore the left/right padding on CellLayout because it turns out in our design
285 // the padding extends outside the visible screen size, but it looked fine anyway.
Michael Jurka38d1b0b2011-04-05 14:50:34 -0700286 int cellHeight = r.getDimensionPixelSize(R.dimen.workspace_cell_height);
287 int heightGap = r.getDimensionPixelSize(R.dimen.workspace_height_gap_land);
Michael Jurka38d1b0b2011-04-05 14:50:34 -0700288
Michael Jurka32271c32011-06-01 17:03:13 -0700289 return heightGap * (numCells - 1) + cellHeight * numCells;
Michael Jurka38d1b0b2011-04-05 14:50:34 -0700290 }
291
Patrick Dubroy3499d8c2011-03-10 17:17:23 -0800292 private void invalidateBubbleTextView(BubbleTextView icon) {
293 final int padding = icon.getPressedOrFocusedBackgroundPadding();
294 invalidate(icon.getLeft() - padding,
295 icon.getTop() - padding,
296 icon.getRight() + padding,
297 icon.getBottom() + padding);
298 }
299
300 void setPressedOrFocusedIcon(BubbleTextView icon) {
301 // We draw the pressed or focused BubbleTextView's background in CellLayout because it
302 // requires an expanded clip rect (due to the glow's blur radius)
303 BubbleTextView oldIcon = mPressedOrFocusedIcon;
304 mPressedOrFocusedIcon = icon;
305 if (oldIcon != null) {
306 invalidateBubbleTextView(oldIcon);
307 }
308 if (mPressedOrFocusedIcon != null) {
309 invalidateBubbleTextView(mPressedOrFocusedIcon);
310 }
311 }
312
Winson Chung6e314082011-01-27 16:46:51 -0800313 public CellLayoutChildren getChildrenLayout() {
314 if (getChildCount() > 0) {
315 return (CellLayoutChildren) getChildAt(0);
316 }
317 return null;
318 }
319
Michael Jurka33945b22010-12-21 18:19:38 -0800320 public void setIsDefaultDropTarget(boolean isDefaultDropTarget) {
321 if (mIsDefaultDropTarget != isDefaultDropTarget) {
322 mIsDefaultDropTarget = isDefaultDropTarget;
323 invalidate();
324 }
325 }
326
Michael Jurka33945b22010-12-21 18:19:38 -0800327 void setIsDragOccuring(boolean isDragOccuring) {
328 if (mIsDragOccuring != isDragOccuring) {
329 mIsDragOccuring = isDragOccuring;
330 invalidate();
331 }
332 }
333
334 void setIsDragOverlapping(boolean isDragOverlapping) {
335 if (mIsDragOverlapping != isDragOverlapping) {
336 mIsDragOverlapping = isDragOverlapping;
337 invalidate();
338 }
339 }
340
341 boolean getIsDragOverlapping() {
342 return mIsDragOverlapping;
343 }
344
345 private void updateGlowRect() {
346 float marginFraction = (mGlowBackgroundScale - 1.0f) / 2.0f;
Michael Jurka18014792010-10-14 09:01:34 -0700347 int marginX = (int) (marginFraction * (mBackgroundRect.right - mBackgroundRect.left));
348 int marginY = (int) (marginFraction * (mBackgroundRect.bottom - mBackgroundRect.top));
Michael Jurka33945b22010-12-21 18:19:38 -0800349 mGlowBackgroundRect.set(mBackgroundRect.left - marginX, mBackgroundRect.top - marginY,
Michael Jurka18014792010-10-14 09:01:34 -0700350 mBackgroundRect.right + marginX, mBackgroundRect.bottom + marginY);
351 invalidate();
352 }
353
354 public void setHoverScale(float scaleFactor) {
Michael Jurka33945b22010-12-21 18:19:38 -0800355 if (scaleFactor != mGlowBackgroundScale) {
356 mGlowBackgroundScale = scaleFactor;
357 updateGlowRect();
Michael Jurka8deb1e62011-01-25 16:27:43 -0800358 if (getParent() != null) {
359 ((View) getParent()).invalidate();
360 }
Michael Jurka18014792010-10-14 09:01:34 -0700361 }
362 }
363
364 public float getHoverScale() {
Michael Jurka33945b22010-12-21 18:19:38 -0800365 return mGlowBackgroundScale;
Michael Jurka18014792010-10-14 09:01:34 -0700366 }
367
368 public float getHoverAlpha() {
Michael Jurka33945b22010-12-21 18:19:38 -0800369 return mGlowBackgroundAlpha;
Michael Jurka18014792010-10-14 09:01:34 -0700370 }
371
372 public void setHoverAlpha(float alpha) {
Michael Jurka33945b22010-12-21 18:19:38 -0800373 mGlowBackgroundAlpha = alpha;
Michael Jurka18014792010-10-14 09:01:34 -0700374 invalidate();
375 }
376
377 void animateDrop() {
378 if (LauncherApplication.isScreenXLarge()) {
379 Resources res = getResources();
380 float onDropScale = res.getInteger(R.integer.config_screenOnDropScalePercent) / 100.0f;
381 ObjectAnimator scaleUp = ObjectAnimator.ofFloat(this, "hoverScale", onDropScale);
382 scaleUp.setDuration(res.getInteger(R.integer.config_screenOnDropScaleUpDuration));
383 ObjectAnimator scaleDown = ObjectAnimator.ofFloat(this, "hoverScale", 1.0f);
384 scaleDown.setDuration(res.getInteger(R.integer.config_screenOnDropScaleDownDuration));
385 ObjectAnimator alphaFadeOut = ObjectAnimator.ofFloat(this, "hoverAlpha", 0.0f);
386
387 alphaFadeOut.setStartDelay(res.getInteger(R.integer.config_screenOnDropAlphaFadeDelay));
388 alphaFadeOut.setDuration(res.getInteger(R.integer.config_screenOnDropAlphaFadeDelay));
389
390 AnimatorSet bouncer = new AnimatorSet();
391 bouncer.play(scaleUp).before(scaleDown);
392 bouncer.play(scaleUp).with(alphaFadeOut);
Michael Jurka8edd75c2010-12-17 20:15:06 -0800393 bouncer.addListener(new AnimatorListenerAdapter() {
Michael Jurka3c4c20f2010-10-28 15:36:06 -0700394 @Override
Michael Jurka18014792010-10-14 09:01:34 -0700395 public void onAnimationStart(Animator animation) {
Michael Jurka33945b22010-12-21 18:19:38 -0800396 setIsDragOverlapping(true);
Michael Jurka18014792010-10-14 09:01:34 -0700397 }
Michael Jurka3c4c20f2010-10-28 15:36:06 -0700398 @Override
Michael Jurka8edd75c2010-12-17 20:15:06 -0800399 public void onAnimationEnd(Animator animation) {
Michael Jurka33945b22010-12-21 18:19:38 -0800400 setIsDragOverlapping(false);
Michael Jurka18014792010-10-14 09:01:34 -0700401 setHoverScale(1.0f);
402 setHoverAlpha(1.0f);
403 }
404 });
405 bouncer.start();
406 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800407 }
408
Jeff Sharkey83f111d2009-04-20 21:03:13 -0700409 @Override
Patrick Dubroy1262e362010-10-06 15:49:50 -0700410 protected void onDraw(Canvas canvas) {
Michael Jurka3e7c7632010-10-02 16:01:03 -0700411 // When we're large, we are either drawn in a "hover" state (ie when dragging an item to
412 // a neighboring page) or with just a normal background (if backgroundAlpha > 0.0f)
413 // When we're small, we are either drawn normally or in the "accepts drops" state (during
414 // a drag). However, we also drag the mini hover background *over* one of those two
415 // backgrounds
Winson Chung26cbf3a2011-01-06 16:25:55 -0800416 if (LauncherApplication.isScreenXLarge() && mBackgroundAlpha > 0.0f) {
Adam Cohenf34bab52010-09-30 14:11:56 -0700417 Drawable bg;
Michael Jurka33945b22010-12-21 18:19:38 -0800418 boolean mini = getScaleX() < 0.5f;
419
420 if (mIsDragOverlapping) {
421 // In the mini case, we draw the active_glow bg *over* the active background
422 bg = mini ? mActiveBackgroundMini : mActiveGlowBackground;
423 } else if (mIsDragOccuring && mAcceptsDrops) {
424 bg = mini ? mActiveBackgroundMini : mActiveBackground;
Adam Cohen3af863b2011-01-25 12:16:51 -0800425 } else if (mIsDefaultDropTarget && mini) {
426 bg = mNormalGlowBackgroundMini;
Adam Cohenf34bab52010-09-30 14:11:56 -0700427 } else {
Michael Jurka33945b22010-12-21 18:19:38 -0800428 bg = mini ? mNormalBackgroundMini : mNormalBackground;
Adam Cohenf34bab52010-09-30 14:11:56 -0700429 }
Michael Jurka33945b22010-12-21 18:19:38 -0800430
431 bg.setAlpha((int) (mBackgroundAlpha * mBackgroundAlphaMultiplier * 255));
432 bg.setBounds(mBackgroundRect);
433 bg.draw(canvas);
434
435 if (mini && mIsDragOverlapping) {
Michael Jurka18014792010-10-14 09:01:34 -0700436 boolean modifiedClipRect = false;
Michael Jurka33945b22010-12-21 18:19:38 -0800437 if (mGlowBackgroundScale > 1.0f) {
Michael Jurka18014792010-10-14 09:01:34 -0700438 // If the hover background's scale is greater than 1, we'll be drawing outside
439 // the bounds of this CellLayout. Get around that by temporarily increasing the
440 // size of the clip rect
Michael Jurka33945b22010-12-21 18:19:38 -0800441 float marginFraction = (mGlowBackgroundScale - 1.0f) / 2.0f;
Michael Jurka18014792010-10-14 09:01:34 -0700442 Rect clipRect = canvas.getClipBounds();
443 int marginX = (int) (marginFraction * (clipRect.right - clipRect.left));
444 int marginY = (int) (marginFraction * (clipRect.bottom - clipRect.top));
445 canvas.save(Canvas.CLIP_SAVE_FLAG);
446 canvas.clipRect(-marginX, -marginY,
447 getWidth() + marginX, getHeight() + marginY, Region.Op.REPLACE);
448 modifiedClipRect = true;
449 }
450
Michael Jurka33945b22010-12-21 18:19:38 -0800451 mActiveGlowBackgroundMini.setAlpha(
452 (int) (mBackgroundAlpha * mGlowBackgroundAlpha * 255));
453 mActiveGlowBackgroundMini.setBounds(mGlowBackgroundRect);
454 mActiveGlowBackgroundMini.draw(canvas);
Michael Jurka18014792010-10-14 09:01:34 -0700455 if (modifiedClipRect) {
456 canvas.restore();
457 }
Michael Jurka3e7c7632010-10-02 16:01:03 -0700458 }
Michael Jurkaa63c4522010-08-19 13:52:27 -0700459 }
Romain Guya6abce82009-11-10 02:54:41 -0800460
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700461 if (mCrosshairsVisibility > 0.0f) {
462 final int countX = mCountX;
463 final int countY = mCountY;
464
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700465 final float MAX_ALPHA = 0.4f;
466 final int MAX_VISIBLE_DISTANCE = 600;
467 final float DISTANCE_MULTIPLIER = 0.002f;
468
469 final Drawable d = mCrosshairsDrawable;
470 final int width = d.getIntrinsicWidth();
471 final int height = d.getIntrinsicHeight();
472
473 int x = getLeftPadding() - (mWidthGap / 2) - (width / 2);
474 for (int col = 0; col <= countX; col++) {
475 int y = getTopPadding() - (mHeightGap / 2) - (height / 2);
476 for (int row = 0; row <= countY; row++) {
477 mTmpPointF.set(x - mDragCenter.x, y - mDragCenter.y);
478 float dist = mTmpPointF.length();
479 // Crosshairs further from the drag point are more faint
480 float alpha = Math.min(MAX_ALPHA,
481 DISTANCE_MULTIPLIER * (MAX_VISIBLE_DISTANCE - dist));
482 if (alpha > 0.0f) {
483 d.setBounds(x, y, x + width, y + height);
484 d.setAlpha((int) (alpha * 255 * mCrosshairsVisibility));
485 d.draw(canvas);
486 }
487 y += mCellHeight + mHeightGap;
488 }
489 x += mCellWidth + mWidthGap;
490 }
Joe Onorato4be866d2010-10-10 11:26:02 -0700491 }
Winson Chung150fbab2010-09-29 17:14:26 -0700492
Patrick Dubroy8e58e912010-10-14 13:21:48 -0700493 final Paint paint = mDragOutlinePaint;
Joe Onorato4be866d2010-10-10 11:26:02 -0700494 for (int i = 0; i < mDragOutlines.length; i++) {
Chet Haase472b2812010-10-14 07:02:04 -0700495 final float alpha = mDragOutlineAlphas[i];
Joe Onorato4be866d2010-10-10 11:26:02 -0700496 if (alpha > 0) {
497 final Point p = mDragOutlines[i];
498 final Bitmap b = (Bitmap) mDragOutlineAnims[i].getTag();
Chet Haase472b2812010-10-14 07:02:04 -0700499 paint.setAlpha((int)(alpha + .5f));
Joe Onorato4be866d2010-10-10 11:26:02 -0700500 canvas.drawBitmap(b, p.x, p.y, paint);
Winson Chung150fbab2010-09-29 17:14:26 -0700501 }
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700502 }
Patrick Dubroy3499d8c2011-03-10 17:17:23 -0800503
504 // We draw the pressed or focused BubbleTextView's background in CellLayout because it
505 // requires an expanded clip rect (due to the glow's blur radius)
506 if (mPressedOrFocusedIcon != null) {
507 final int padding = mPressedOrFocusedIcon.getPressedOrFocusedBackgroundPadding();
508 final Bitmap b = mPressedOrFocusedIcon.getPressedOrFocusedBackground();
509 if (b != null) {
510 canvas.drawBitmap(b,
511 mPressedOrFocusedIcon.getLeft() - padding,
512 mPressedOrFocusedIcon.getTop() - padding,
513 null);
514 }
515 }
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700516 }
517
518 @Override
Jeff Sharkey83f111d2009-04-20 21:03:13 -0700519 public void cancelLongPress() {
520 super.cancelLongPress();
521
522 // Cancel long press for all children
523 final int count = getChildCount();
524 for (int i = 0; i < count; i++) {
525 final View child = getChildAt(i);
526 child.cancelLongPress();
527 }
528 }
529
Michael Jurkadee05892010-07-27 10:01:56 -0700530 public void setOnInterceptTouchListener(View.OnTouchListener listener) {
531 mInterceptTouchListener = listener;
532 }
533
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800534 int getCountX() {
Adam Cohend22015c2010-07-26 22:02:18 -0700535 return mCountX;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800536 }
537
538 int getCountY() {
Adam Cohend22015c2010-07-26 22:02:18 -0700539 return mCountY;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800540 }
541
Michael Jurkaf3ca3ab2010-10-20 17:08:24 -0700542 public boolean addViewToCellLayout(
543 View child, int index, int childId, LayoutParams params, boolean markCells) {
Winson Chungaafa03c2010-06-11 17:34:16 -0700544 final LayoutParams lp = params;
545
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800546 // Generate an id for each view, this assumes we have at most 256x256 cells
547 // per workspace screen
Adam Cohend22015c2010-07-26 22:02:18 -0700548 if (lp.cellX >= 0 && lp.cellX <= mCountX - 1 && lp.cellY >= 0 && lp.cellY <= mCountY - 1) {
Winson Chungaafa03c2010-06-11 17:34:16 -0700549 // If the horizontal or vertical span is set to -1, it is taken to
550 // mean that it spans the extent of the CellLayout
Adam Cohend22015c2010-07-26 22:02:18 -0700551 if (lp.cellHSpan < 0) lp.cellHSpan = mCountX;
552 if (lp.cellVSpan < 0) lp.cellVSpan = mCountY;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800553
Winson Chungaafa03c2010-06-11 17:34:16 -0700554 child.setId(childId);
555
Michael Jurka8c920dd2011-01-20 14:16:56 -0800556 mChildren.addView(child, index, lp);
Michael Jurkadee05892010-07-27 10:01:56 -0700557
Michael Jurkaf3ca3ab2010-10-20 17:08:24 -0700558 if (markCells) markCellsAsOccupiedForView(child);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700559
Winson Chungaafa03c2010-06-11 17:34:16 -0700560 return true;
561 }
562 return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800563 }
Michael Jurka3e7c7632010-10-02 16:01:03 -0700564
Michael Jurkabea15192010-11-17 12:33:46 -0800565 public void setAcceptsDrops(boolean acceptsDrops) {
566 if (mAcceptsDrops != acceptsDrops) {
567 mAcceptsDrops = acceptsDrops;
568 invalidate();
569 }
570 }
571
Michael Jurka3e7c7632010-10-02 16:01:03 -0700572 public boolean getAcceptsDrops() {
573 return mAcceptsDrops;
574 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800575
576 @Override
Michael Jurka0280c3b2010-09-17 15:00:07 -0700577 public void removeAllViews() {
578 clearOccupiedCells();
Michael Jurka8c920dd2011-01-20 14:16:56 -0800579 mChildren.removeAllViews();
Michael Jurka0280c3b2010-09-17 15:00:07 -0700580 }
581
582 @Override
583 public void removeAllViewsInLayout() {
584 clearOccupiedCells();
Michael Jurka8c920dd2011-01-20 14:16:56 -0800585 mChildren.removeAllViewsInLayout();
Michael Jurka0280c3b2010-09-17 15:00:07 -0700586 }
587
Michael Jurkaf3ca3ab2010-10-20 17:08:24 -0700588 public void removeViewWithoutMarkingCells(View view) {
Michael Jurkacf6125c2011-01-28 15:20:01 -0800589 mChildren.removeView(view);
Michael Jurkaf3ca3ab2010-10-20 17:08:24 -0700590 }
591
Michael Jurka0280c3b2010-09-17 15:00:07 -0700592 @Override
593 public void removeView(View view) {
594 markCellsAsUnoccupiedForView(view);
Michael Jurka8c920dd2011-01-20 14:16:56 -0800595 mChildren.removeView(view);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700596 }
597
598 @Override
599 public void removeViewAt(int index) {
Michael Jurka8c920dd2011-01-20 14:16:56 -0800600 markCellsAsUnoccupiedForView(mChildren.getChildAt(index));
601 mChildren.removeViewAt(index);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700602 }
603
604 @Override
605 public void removeViewInLayout(View view) {
606 markCellsAsUnoccupiedForView(view);
Michael Jurka8c920dd2011-01-20 14:16:56 -0800607 mChildren.removeViewInLayout(view);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700608 }
609
610 @Override
611 public void removeViews(int start, int count) {
612 for (int i = start; i < start + count; i++) {
Michael Jurka8c920dd2011-01-20 14:16:56 -0800613 markCellsAsUnoccupiedForView(mChildren.getChildAt(i));
Michael Jurka0280c3b2010-09-17 15:00:07 -0700614 }
Michael Jurka8c920dd2011-01-20 14:16:56 -0800615 mChildren.removeViews(start, count);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700616 }
617
618 @Override
619 public void removeViewsInLayout(int start, int count) {
620 for (int i = start; i < start + count; i++) {
Michael Jurka8c920dd2011-01-20 14:16:56 -0800621 markCellsAsUnoccupiedForView(mChildren.getChildAt(i));
Michael Jurka0280c3b2010-09-17 15:00:07 -0700622 }
Michael Jurka8c920dd2011-01-20 14:16:56 -0800623 mChildren.removeViewsInLayout(start, count);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700624 }
625
Michael Jurka8c920dd2011-01-20 14:16:56 -0800626 public void drawChildren(Canvas canvas) {
627 mChildren.draw(canvas);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800628 }
629
Michael Jurkaabded662011-03-04 12:06:57 -0800630 void buildChildrenLayer() {
631 mChildren.buildLayer();
632 }
633
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800634 @Override
635 protected void onAttachedToWindow() {
636 super.onAttachedToWindow();
637 mCellInfo.screen = ((ViewGroup) getParent()).indexOfChild(this);
638 }
639
Michael Jurkaaf442092010-06-10 17:01:57 -0700640 public void setTagToCellInfoForPoint(int touchX, int touchY) {
641 final CellInfo cellInfo = mCellInfo;
642 final Rect frame = mRect;
643 final int x = touchX + mScrollX;
644 final int y = touchY + mScrollY;
Michael Jurka8c920dd2011-01-20 14:16:56 -0800645 final int count = mChildren.getChildCount();
Michael Jurkaaf442092010-06-10 17:01:57 -0700646
647 boolean found = false;
648 for (int i = count - 1; i >= 0; i--) {
Michael Jurka8c920dd2011-01-20 14:16:56 -0800649 final View child = mChildren.getChildAt(i);
Adam Cohend4844c32011-02-18 19:25:06 -0800650 final LayoutParams lp = (LayoutParams) child.getLayoutParams();
Michael Jurkaaf442092010-06-10 17:01:57 -0700651
Adam Cohen1b607ed2011-03-03 17:26:50 -0800652 if ((child.getVisibility() == VISIBLE || child.getAnimation() != null) &&
653 lp.isLockedToGrid) {
Michael Jurkaaf442092010-06-10 17:01:57 -0700654 child.getHitRect(frame);
655 if (frame.contains(x, y)) {
Michael Jurkaaf442092010-06-10 17:01:57 -0700656 cellInfo.cell = child;
657 cellInfo.cellX = lp.cellX;
658 cellInfo.cellY = lp.cellY;
659 cellInfo.spanX = lp.cellHSpan;
660 cellInfo.spanY = lp.cellVSpan;
661 cellInfo.valid = true;
662 found = true;
Michael Jurkaaf442092010-06-10 17:01:57 -0700663 break;
664 }
665 }
666 }
Winson Chungaafa03c2010-06-11 17:34:16 -0700667
Michael Jurkaaf442092010-06-10 17:01:57 -0700668 if (!found) {
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700669 final int cellXY[] = mTmpCellXY;
Michael Jurkaaf442092010-06-10 17:01:57 -0700670 pointToCellExact(x, y, cellXY);
671
Michael Jurkaaf442092010-06-10 17:01:57 -0700672 cellInfo.cell = null;
673 cellInfo.cellX = cellXY[0];
674 cellInfo.cellY = cellXY[1];
675 cellInfo.spanX = 1;
676 cellInfo.spanY = 1;
Michael Jurka0280c3b2010-09-17 15:00:07 -0700677 cellInfo.valid = cellXY[0] >= 0 && cellXY[1] >= 0 && cellXY[0] < mCountX &&
678 cellXY[1] < mCountY && !mOccupied[cellXY[0]][cellXY[1]];
Michael Jurkaaf442092010-06-10 17:01:57 -0700679 }
680 setTag(cellInfo);
681 }
682
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800683 @Override
684 public boolean onInterceptTouchEvent(MotionEvent ev) {
Michael Jurkadee05892010-07-27 10:01:56 -0700685 if (mInterceptTouchListener != null && mInterceptTouchListener.onTouch(this, ev)) {
686 return true;
687 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800688 final int action = ev.getAction();
689 final CellInfo cellInfo = mCellInfo;
690
691 if (action == MotionEvent.ACTION_DOWN) {
Michael Jurkaaf442092010-06-10 17:01:57 -0700692 setTagToCellInfoForPoint((int) ev.getX(), (int) ev.getY());
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800693 } else if (action == MotionEvent.ACTION_UP) {
694 cellInfo.cell = null;
695 cellInfo.cellX = -1;
696 cellInfo.cellY = -1;
697 cellInfo.spanX = 0;
698 cellInfo.spanY = 0;
699 cellInfo.valid = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800700 setTag(cellInfo);
701 }
702
703 return false;
704 }
705
706 @Override
707 public CellInfo getTag() {
Michael Jurka0280c3b2010-09-17 15:00:07 -0700708 return (CellInfo) super.getTag();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800709 }
710
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700711 /**
Winson Chungaafa03c2010-06-11 17:34:16 -0700712 * Given a point, return the cell that strictly encloses that point
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800713 * @param x X coordinate of the point
714 * @param y Y coordinate of the point
715 * @param result Array of 2 ints to hold the x and y coordinate of the cell
716 */
717 void pointToCellExact(int x, int y, int[] result) {
Winson Chungaafa03c2010-06-11 17:34:16 -0700718 final int hStartPadding = getLeftPadding();
719 final int vStartPadding = getTopPadding();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800720
721 result[0] = (x - hStartPadding) / (mCellWidth + mWidthGap);
722 result[1] = (y - vStartPadding) / (mCellHeight + mHeightGap);
723
Adam Cohend22015c2010-07-26 22:02:18 -0700724 final int xAxis = mCountX;
725 final int yAxis = mCountY;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800726
727 if (result[0] < 0) result[0] = 0;
728 if (result[0] >= xAxis) result[0] = xAxis - 1;
729 if (result[1] < 0) result[1] = 0;
730 if (result[1] >= yAxis) result[1] = yAxis - 1;
731 }
Winson Chungaafa03c2010-06-11 17:34:16 -0700732
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800733 /**
734 * Given a point, return the cell that most closely encloses that point
735 * @param x X coordinate of the point
736 * @param y Y coordinate of the point
737 * @param result Array of 2 ints to hold the x and y coordinate of the cell
738 */
739 void pointToCellRounded(int x, int y, int[] result) {
740 pointToCellExact(x + (mCellWidth / 2), y + (mCellHeight / 2), result);
741 }
742
743 /**
744 * Given a cell coordinate, return the point that represents the upper left corner of that cell
Winson Chungaafa03c2010-06-11 17:34:16 -0700745 *
746 * @param cellX X coordinate of the cell
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800747 * @param cellY Y coordinate of the cell
Winson Chungaafa03c2010-06-11 17:34:16 -0700748 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800749 * @param result Array of 2 ints to hold the x and y coordinate of the point
750 */
751 void cellToPoint(int cellX, int cellY, int[] result) {
Winson Chungaafa03c2010-06-11 17:34:16 -0700752 final int hStartPadding = getLeftPadding();
753 final int vStartPadding = getTopPadding();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800754
755 result[0] = hStartPadding + cellX * (mCellWidth + mWidthGap);
756 result[1] = vStartPadding + cellY * (mCellHeight + mHeightGap);
757 }
758
Romain Guy84f296c2009-11-04 15:00:44 -0800759 int getCellWidth() {
760 return mCellWidth;
761 }
762
763 int getCellHeight() {
764 return mCellHeight;
765 }
766
Adam Cohend4844c32011-02-18 19:25:06 -0800767 int getWidthGap() {
768 return mWidthGap;
769 }
770
771 int getHeightGap() {
772 return mHeightGap;
773 }
774
Romain Guy1a304a12009-11-10 00:02:32 -0800775 int getLeftPadding() {
Winson Chungaafa03c2010-06-11 17:34:16 -0700776 return mLeftPadding;
Romain Guy1a304a12009-11-10 00:02:32 -0800777 }
778
779 int getTopPadding() {
Winson Chungaafa03c2010-06-11 17:34:16 -0700780 return mTopPadding;
Romain Guy1a304a12009-11-10 00:02:32 -0800781 }
782
783 int getRightPadding() {
Winson Chungaafa03c2010-06-11 17:34:16 -0700784 return mRightPadding;
Romain Guy1a304a12009-11-10 00:02:32 -0800785 }
786
787 int getBottomPadding() {
Winson Chungaafa03c2010-06-11 17:34:16 -0700788 return mBottomPadding;
Romain Guy1a304a12009-11-10 00:02:32 -0800789 }
790
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800791 @Override
792 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
793 // TODO: currently ignoring padding
Winson Chungaafa03c2010-06-11 17:34:16 -0700794
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800795 int widthSpecMode = MeasureSpec.getMode(widthMeasureSpec);
Winson Chungaafa03c2010-06-11 17:34:16 -0700796 int widthSpecSize = MeasureSpec.getSize(widthMeasureSpec);
797
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800798 int heightSpecMode = MeasureSpec.getMode(heightMeasureSpec);
799 int heightSpecSize = MeasureSpec.getSize(heightMeasureSpec);
Winson Chungaafa03c2010-06-11 17:34:16 -0700800
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800801 if (widthSpecMode == MeasureSpec.UNSPECIFIED || heightSpecMode == MeasureSpec.UNSPECIFIED) {
802 throw new RuntimeException("CellLayout cannot have UNSPECIFIED dimensions");
803 }
804
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800805 final int cellWidth = mCellWidth;
806 final int cellHeight = mCellHeight;
807
Adam Cohend22015c2010-07-26 22:02:18 -0700808 int numWidthGaps = mCountX - 1;
809 int numHeightGaps = mCountY - 1;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800810
Winson Chungece7f5b2010-10-22 14:54:12 -0700811 if (mWidthGap < 0 || mHeightGap < 0) {
812 int vSpaceLeft = heightSpecSize - mTopPadding - mBottomPadding - (cellHeight * mCountY);
813 mHeightGap = vSpaceLeft / numHeightGaps;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800814
Winson Chungece7f5b2010-10-22 14:54:12 -0700815 int hSpaceLeft = widthSpecSize - mLeftPadding - mRightPadding - (cellWidth * mCountX);
816 mWidthGap = hSpaceLeft / numWidthGaps;
Winson Chungaafa03c2010-06-11 17:34:16 -0700817
Winson Chungece7f5b2010-10-22 14:54:12 -0700818 // center it around the min gaps
819 int minGap = Math.min(mWidthGap, mHeightGap);
820 mWidthGap = mHeightGap = minGap;
821 }
Michael Jurka5f1c5092010-09-03 14:15:02 -0700822
Michael Jurka8c920dd2011-01-20 14:16:56 -0800823 // Initial values correspond to widthSpecMode == MeasureSpec.EXACTLY
824 int newWidth = widthSpecSize;
825 int newHeight = heightSpecSize;
Michael Jurka5f1c5092010-09-03 14:15:02 -0700826 if (widthSpecMode == MeasureSpec.AT_MOST) {
Michael Jurka8c920dd2011-01-20 14:16:56 -0800827 newWidth = mLeftPadding + mRightPadding + (mCountX * cellWidth) +
Winson Chungece7f5b2010-10-22 14:54:12 -0700828 ((mCountX - 1) * mWidthGap);
Michael Jurka8c920dd2011-01-20 14:16:56 -0800829 newHeight = mTopPadding + mBottomPadding + (mCountY * cellHeight) +
Winson Chungece7f5b2010-10-22 14:54:12 -0700830 ((mCountY - 1) * mHeightGap);
Michael Jurka5f1c5092010-09-03 14:15:02 -0700831 setMeasuredDimension(newWidth, newHeight);
Michael Jurka5f1c5092010-09-03 14:15:02 -0700832 }
Michael Jurka8c920dd2011-01-20 14:16:56 -0800833
834 int count = getChildCount();
835 for (int i = 0; i < count; i++) {
836 View child = getChildAt(i);
837 int childWidthMeasureSpec = MeasureSpec.makeMeasureSpec(newWidth, MeasureSpec.EXACTLY);
838 int childheightMeasureSpec = MeasureSpec.makeMeasureSpec(newHeight,
839 MeasureSpec.EXACTLY);
840 child.measure(childWidthMeasureSpec, childheightMeasureSpec);
841 }
842 setMeasuredDimension(newWidth, newHeight);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800843 }
844
845 @Override
Michael Jurka28750fb2010-09-24 17:43:49 -0700846 protected void onLayout(boolean changed, int l, int t, int r, int b) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800847 int count = getChildCount();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800848 for (int i = 0; i < count; i++) {
Michael Jurka8c920dd2011-01-20 14:16:56 -0800849 View child = getChildAt(i);
850 child.layout(0, 0, r - l, b - t);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800851 }
852 }
853
854 @Override
Michael Jurkadee05892010-07-27 10:01:56 -0700855 protected void onSizeChanged(int w, int h, int oldw, int oldh) {
856 super.onSizeChanged(w, h, oldw, oldh);
Michael Jurka18014792010-10-14 09:01:34 -0700857 mBackgroundRect.set(0, 0, w, h);
Michael Jurka33945b22010-12-21 18:19:38 -0800858 updateGlowRect();
Michael Jurkadee05892010-07-27 10:01:56 -0700859 }
860
861 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800862 protected void setChildrenDrawingCacheEnabled(boolean enabled) {
Michael Jurka8c920dd2011-01-20 14:16:56 -0800863 mChildren.setChildrenDrawingCacheEnabled(enabled);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800864 }
865
866 @Override
867 protected void setChildrenDrawnWithCacheEnabled(boolean enabled) {
Michael Jurka8c920dd2011-01-20 14:16:56 -0800868 mChildren.setChildrenDrawnWithCacheEnabled(enabled);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800869 }
870
Michael Jurka5f1c5092010-09-03 14:15:02 -0700871 public float getBackgroundAlpha() {
872 return mBackgroundAlpha;
Michael Jurkadee05892010-07-27 10:01:56 -0700873 }
874
Michael Jurka742574b2011-02-02 23:51:01 -0800875 public void setFastBackgroundAlpha(float alpha) {
876 mBackgroundAlpha = alpha;
877 }
878
Adam Cohen1b0aaac2010-10-28 11:11:18 -0700879 public void setBackgroundAlphaMultiplier(float multiplier) {
880 mBackgroundAlphaMultiplier = multiplier;
881 }
882
Adam Cohenddb82192010-11-10 16:32:54 -0800883 public float getBackgroundAlphaMultiplier() {
884 return mBackgroundAlphaMultiplier;
885 }
886
Michael Jurka5f1c5092010-09-03 14:15:02 -0700887 public void setBackgroundAlpha(float alpha) {
888 mBackgroundAlpha = alpha;
Michael Jurka0142d492010-08-25 17:46:15 -0700889 invalidate();
Michael Jurkadee05892010-07-27 10:01:56 -0700890 }
891
Michael Jurka5f1c5092010-09-03 14:15:02 -0700892 // Need to return true to let the view system know we know how to handle alpha-- this is
893 // because when our children have an alpha of 0.0f, they are still rendering their "dimmed"
894 // versions
895 @Override
896 protected boolean onSetAlpha(int alpha) {
897 return true;
898 }
899
900 public void setAlpha(float alpha) {
901 setChildrenAlpha(alpha);
902 super.setAlpha(alpha);
903 }
904
Michael Jurka742574b2011-02-02 23:51:01 -0800905 public void setFastAlpha(float alpha) {
906 setFastChildrenAlpha(alpha);
907 super.setFastAlpha(alpha);
908 }
909
Michael Jurkadee05892010-07-27 10:01:56 -0700910 private void setChildrenAlpha(float alpha) {
Michael Jurka0142d492010-08-25 17:46:15 -0700911 final int childCount = getChildCount();
912 for (int i = 0; i < childCount; i++) {
Michael Jurkadee05892010-07-27 10:01:56 -0700913 getChildAt(i).setAlpha(alpha);
914 }
915 }
916
Michael Jurka742574b2011-02-02 23:51:01 -0800917 private void setFastChildrenAlpha(float alpha) {
918 final int childCount = getChildCount();
919 for (int i = 0; i < childCount; i++) {
920 getChildAt(i).setFastAlpha(alpha);
921 }
922 }
923
Patrick Dubroy440c3602010-07-13 17:50:32 -0700924 public View getChildAt(int x, int y) {
Michael Jurka8c920dd2011-01-20 14:16:56 -0800925 return mChildren.getChildAt(x, y);
Patrick Dubroy440c3602010-07-13 17:50:32 -0700926 }
927
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700928 /**
929 * Estimate where the top left cell of the dragged item will land if it is dropped.
930 *
931 * @param originX The X value of the top left corner of the item
932 * @param originY The Y value of the top left corner of the item
933 * @param spanX The number of horizontal cells that the item spans
934 * @param spanY The number of vertical cells that the item spans
935 * @param result The estimated drop cell X and Y.
936 */
937 void estimateDropCell(int originX, int originY, int spanX, int spanY, int[] result) {
Adam Cohend22015c2010-07-26 22:02:18 -0700938 final int countX = mCountX;
939 final int countY = mCountY;
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700940
Michael Jurkaa63c4522010-08-19 13:52:27 -0700941 // pointToCellRounded takes the top left of a cell but will pad that with
942 // cellWidth/2 and cellHeight/2 when finding the matching cell
943 pointToCellRounded(originX, originY, result);
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700944
945 // If the item isn't fully on this screen, snap to the edges
946 int rightOverhang = result[0] + spanX - countX;
947 if (rightOverhang > 0) {
948 result[0] -= rightOverhang; // Snap to right
949 }
950 result[0] = Math.max(0, result[0]); // Snap to left
951 int bottomOverhang = result[1] + spanY - countY;
952 if (bottomOverhang > 0) {
953 result[1] -= bottomOverhang; // Snap to bottom
954 }
955 result[1] = Math.max(0, result[1]); // Snap to top
956 }
957
Joe Onorato4be866d2010-10-10 11:26:02 -0700958 void visualizeDropLocation(
959 View v, Bitmap dragOutline, int originX, int originY, int spanX, int spanY) {
960
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -0700961 final int oldDragCellX = mDragCell[0];
962 final int oldDragCellY = mDragCell[1];
Joe Onorato4be866d2010-10-10 11:26:02 -0700963 final int[] nearest = findNearestVacantArea(originX, originY, spanX, spanY, v, mDragCell);
Winson Chunga9abd0e2010-10-27 17:18:37 -0700964 if (v != null) {
965 mDragCenter.set(originX + (v.getWidth() / 2), originY + (v.getHeight() / 2));
966 } else {
967 mDragCenter.set(originX, originY);
968 }
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700969
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -0700970 if (nearest != null && (nearest[0] != oldDragCellX || nearest[1] != oldDragCellY)) {
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700971 // Find the top left corner of the rect the object will occupy
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700972 final int[] topLeft = mTmpPoint;
973 cellToPoint(nearest[0], nearest[1], topLeft);
974
Joe Onorato4be866d2010-10-10 11:26:02 -0700975 int left = topLeft[0];
976 int top = topLeft[1];
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700977
Winson Chunga9abd0e2010-10-27 17:18:37 -0700978 if (v != null) {
979 if (v.getParent() instanceof CellLayout) {
980 LayoutParams lp = (LayoutParams) v.getLayoutParams();
981 left += lp.leftMargin;
982 top += lp.topMargin;
983 }
Winson Chung150fbab2010-09-29 17:14:26 -0700984
Winson Chunga9abd0e2010-10-27 17:18:37 -0700985 // Offsets due to the size difference between the View and the dragOutline
986 left += (v.getWidth() - dragOutline.getWidth()) / 2;
987 top += (v.getHeight() - dragOutline.getHeight()) / 2;
988 }
Winson Chung150fbab2010-09-29 17:14:26 -0700989
Joe Onorato4be866d2010-10-10 11:26:02 -0700990 final int oldIndex = mDragOutlineCurrent;
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -0700991 mDragOutlineAnims[oldIndex].animateOut();
992 mDragOutlineCurrent = (oldIndex + 1) % mDragOutlines.length;
Winson Chung150fbab2010-09-29 17:14:26 -0700993
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -0700994 mDragOutlines[mDragOutlineCurrent].set(left, top);
995 mDragOutlineAnims[mDragOutlineCurrent].setTag(dragOutline);
996 mDragOutlineAnims[mDragOutlineCurrent].animateIn();
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700997 }
Patrick Dubroy49250ad2010-10-08 15:33:52 -0700998
999 // If we are drawing crosshairs, the entire CellLayout needs to be invalidated
1000 if (mCrosshairsDrawable != null) {
1001 invalidate();
1002 }
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001003 }
1004
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001005 /**
Jeff Sharkey70864282009-04-07 21:08:40 -07001006 * Find a vacant area that will fit the given bounds nearest the requested
1007 * cell location. Uses Euclidean distance to score multiple vacant areas.
Winson Chungaafa03c2010-06-11 17:34:16 -07001008 *
Romain Guy51afc022009-05-04 18:03:43 -07001009 * @param pixelX The X location at which you want to search for a vacant area.
1010 * @param pixelY The Y location at which you want to search for a vacant area.
Jeff Sharkey70864282009-04-07 21:08:40 -07001011 * @param spanX Horizontal span of the object.
1012 * @param spanY Vertical span of the object.
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001013 * @param result Array in which to place the result, or null (in which case a new array will
1014 * be allocated)
Jeff Sharkey70864282009-04-07 21:08:40 -07001015 * @return The X, Y cell of a vacant area that can contain this object,
1016 * nearest the requested location.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001017 */
Michael Jurka6a1435d2010-09-27 17:35:12 -07001018 int[] findNearestVacantArea(
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001019 int pixelX, int pixelY, int spanX, int spanY, int[] result) {
1020 return findNearestVacantArea(pixelX, pixelY, spanX, spanY, null, result);
Michael Jurka6a1435d2010-09-27 17:35:12 -07001021 }
Winson Chungaafa03c2010-06-11 17:34:16 -07001022
Michael Jurka6a1435d2010-09-27 17:35:12 -07001023 /**
1024 * Find a vacant area that will fit the given bounds nearest the requested
1025 * cell location. Uses Euclidean distance to score multiple vacant areas.
1026 *
1027 * @param pixelX The X location at which you want to search for a vacant area.
1028 * @param pixelY The Y location at which you want to search for a vacant area.
1029 * @param spanX Horizontal span of the object.
1030 * @param spanY Vertical span of the object.
Michael Jurka6a1435d2010-09-27 17:35:12 -07001031 * @param ignoreView Considers space occupied by this view as unoccupied
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001032 * @param result Previously returned value to possibly recycle.
Michael Jurka6a1435d2010-09-27 17:35:12 -07001033 * @return The X, Y cell of a vacant area that can contain this object,
1034 * nearest the requested location.
1035 */
1036 int[] findNearestVacantArea(
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001037 int pixelX, int pixelY, int spanX, int spanY, View ignoreView, int[] result) {
Michael Jurkac6ee42e2010-09-30 12:04:50 -07001038 // mark space take by ignoreView as available (method checks if ignoreView is null)
1039 markCellsAsUnoccupiedForView(ignoreView);
1040
Jeff Sharkey70864282009-04-07 21:08:40 -07001041 // Keep track of best-scoring drop area
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001042 final int[] bestXY = result != null ? result : new int[2];
Jeff Sharkey70864282009-04-07 21:08:40 -07001043 double bestDistance = Double.MAX_VALUE;
Winson Chungaafa03c2010-06-11 17:34:16 -07001044
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001045 final int countX = mCountX;
1046 final int countY = mCountY;
1047 final boolean[][] occupied = mOccupied;
1048
Winson Chungbbc60d82010-11-11 16:34:41 -08001049 for (int y = 0; y < countY - (spanY - 1); y++) {
Michael Jurkac28de512010-08-13 11:27:44 -07001050 inner:
Winson Chungbbc60d82010-11-11 16:34:41 -08001051 for (int x = 0; x < countX - (spanX - 1); x++) {
Michael Jurkac28de512010-08-13 11:27:44 -07001052 for (int i = 0; i < spanX; i++) {
1053 for (int j = 0; j < spanY; j++) {
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001054 if (occupied[x + i][y + j]) {
Winson Chungbbc60d82010-11-11 16:34:41 -08001055 // small optimization: we can skip to after the column we just found
Michael Jurkac28de512010-08-13 11:27:44 -07001056 // an occupied cell
Winson Chungbbc60d82010-11-11 16:34:41 -08001057 x += i;
Michael Jurkac28de512010-08-13 11:27:44 -07001058 continue inner;
1059 }
1060 }
1061 }
1062 final int[] cellXY = mTmpCellXY;
1063 cellToPoint(x, y, cellXY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001064
Michael Jurkac28de512010-08-13 11:27:44 -07001065 double distance = Math.sqrt(Math.pow(cellXY[0] - pixelX, 2)
1066 + Math.pow(cellXY[1] - pixelY, 2));
1067 if (distance <= bestDistance) {
1068 bestDistance = distance;
1069 bestXY[0] = x;
1070 bestXY[1] = y;
1071 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001072 }
1073 }
Michael Jurkac6ee42e2010-09-30 12:04:50 -07001074 // re-mark space taken by ignoreView as occupied
1075 markCellsAsOccupiedForView(ignoreView);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001076
Winson Chungaafa03c2010-06-11 17:34:16 -07001077 // Return null if no suitable location found
Jeff Sharkey70864282009-04-07 21:08:40 -07001078 if (bestDistance < Double.MAX_VALUE) {
1079 return bestXY;
1080 } else {
1081 return null;
1082 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001083 }
Winson Chungaafa03c2010-06-11 17:34:16 -07001084
Michael Jurka0280c3b2010-09-17 15:00:07 -07001085 boolean existsEmptyCell() {
1086 return findCellForSpan(null, 1, 1);
1087 }
1088
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001089 /**
Michael Jurka0280c3b2010-09-17 15:00:07 -07001090 * Finds the upper-left coordinate of the first rectangle in the grid that can
1091 * hold a cell of the specified dimensions. If intersectX and intersectY are not -1,
1092 * then this method will only return coordinates for rectangles that contain the cell
1093 * (intersectX, intersectY)
1094 *
1095 * @param cellXY The array that will contain the position of a vacant cell if such a cell
1096 * can be found.
1097 * @param spanX The horizontal span of the cell we want to find.
1098 * @param spanY The vertical span of the cell we want to find.
1099 *
1100 * @return True if a vacant cell of the specified dimension was found, false otherwise.
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001101 */
Michael Jurka0280c3b2010-09-17 15:00:07 -07001102 boolean findCellForSpan(int[] cellXY, int spanX, int spanY) {
1103 return findCellForSpanThatIntersectsIgnoring(cellXY, spanX, spanY, -1, -1, null);
1104 }
1105
1106 /**
1107 * Like above, but ignores any cells occupied by the item "ignoreView"
1108 *
1109 * @param cellXY The array that will contain the position of a vacant cell if such a cell
1110 * can be found.
1111 * @param spanX The horizontal span of the cell we want to find.
1112 * @param spanY The vertical span of the cell we want to find.
1113 * @param ignoreView The home screen item we should treat as not occupying any space
1114 * @return
1115 */
1116 boolean findCellForSpanIgnoring(int[] cellXY, int spanX, int spanY, View ignoreView) {
1117 return findCellForSpanThatIntersectsIgnoring(cellXY, spanX, spanY, -1, -1, ignoreView);
1118 }
1119
1120 /**
1121 * Like above, but if intersectX and intersectY are not -1, then this method will try to
1122 * return coordinates for rectangles that contain the cell [intersectX, intersectY]
1123 *
1124 * @param spanX The horizontal span of the cell we want to find.
1125 * @param spanY The vertical span of the cell we want to find.
1126 * @param ignoreView The home screen item we should treat as not occupying any space
1127 * @param intersectX The X coordinate of the cell that we should try to overlap
1128 * @param intersectX The Y coordinate of the cell that we should try to overlap
1129 *
1130 * @return True if a vacant cell of the specified dimension was found, false otherwise.
1131 */
1132 boolean findCellForSpanThatIntersects(int[] cellXY, int spanX, int spanY,
1133 int intersectX, int intersectY) {
1134 return findCellForSpanThatIntersectsIgnoring(
1135 cellXY, spanX, spanY, intersectX, intersectY, null);
1136 }
1137
1138 /**
1139 * The superset of the above two methods
1140 */
1141 boolean findCellForSpanThatIntersectsIgnoring(int[] cellXY, int spanX, int spanY,
1142 int intersectX, int intersectY, View ignoreView) {
Michael Jurkac6ee42e2010-09-30 12:04:50 -07001143 // mark space take by ignoreView as available (method checks if ignoreView is null)
1144 markCellsAsUnoccupiedForView(ignoreView);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001145
Michael Jurka28750fb2010-09-24 17:43:49 -07001146 boolean foundCell = false;
Michael Jurka0280c3b2010-09-17 15:00:07 -07001147 while (true) {
1148 int startX = 0;
1149 if (intersectX >= 0) {
1150 startX = Math.max(startX, intersectX - (spanX - 1));
1151 }
1152 int endX = mCountX - (spanX - 1);
1153 if (intersectX >= 0) {
1154 endX = Math.min(endX, intersectX + (spanX - 1) + (spanX == 1 ? 1 : 0));
1155 }
1156 int startY = 0;
1157 if (intersectY >= 0) {
1158 startY = Math.max(startY, intersectY - (spanY - 1));
1159 }
1160 int endY = mCountY - (spanY - 1);
1161 if (intersectY >= 0) {
1162 endY = Math.min(endY, intersectY + (spanY - 1) + (spanY == 1 ? 1 : 0));
1163 }
1164
Winson Chungbbc60d82010-11-11 16:34:41 -08001165 for (int y = startY; y < endY && !foundCell; y++) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001166 inner:
Winson Chungbbc60d82010-11-11 16:34:41 -08001167 for (int x = startX; x < endX; x++) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001168 for (int i = 0; i < spanX; i++) {
1169 for (int j = 0; j < spanY; j++) {
1170 if (mOccupied[x + i][y + j]) {
Winson Chungbbc60d82010-11-11 16:34:41 -08001171 // small optimization: we can skip to after the column we just found
Michael Jurka0280c3b2010-09-17 15:00:07 -07001172 // an occupied cell
Winson Chungbbc60d82010-11-11 16:34:41 -08001173 x += i;
Michael Jurka0280c3b2010-09-17 15:00:07 -07001174 continue inner;
1175 }
1176 }
1177 }
1178 if (cellXY != null) {
1179 cellXY[0] = x;
1180 cellXY[1] = y;
1181 }
Michael Jurka28750fb2010-09-24 17:43:49 -07001182 foundCell = true;
1183 break;
Michael Jurka0280c3b2010-09-17 15:00:07 -07001184 }
1185 }
1186 if (intersectX == -1 && intersectY == -1) {
1187 break;
1188 } else {
1189 // if we failed to find anything, try again but without any requirements of
1190 // intersecting
1191 intersectX = -1;
1192 intersectY = -1;
1193 continue;
1194 }
1195 }
1196
Michael Jurkac6ee42e2010-09-30 12:04:50 -07001197 // re-mark space taken by ignoreView as occupied
1198 markCellsAsOccupiedForView(ignoreView);
Michael Jurka28750fb2010-09-24 17:43:49 -07001199 return foundCell;
Michael Jurka0280c3b2010-09-17 15:00:07 -07001200 }
1201
1202 /**
1203 * Called when drag has left this CellLayout or has been completed (successfully or not)
1204 */
1205 void onDragExit() {
Joe Onorato4be866d2010-10-10 11:26:02 -07001206 // This can actually be called when we aren't in a drag, e.g. when adding a new
1207 // item to this layout via the customize drawer.
1208 // Guard against that case.
1209 if (mDragging) {
1210 mDragging = false;
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001211
Joe Onorato4be866d2010-10-10 11:26:02 -07001212 // Fade out the drag indicators
1213 if (mCrosshairsAnimator != null) {
1214 mCrosshairsAnimator.animateOut();
1215 }
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001216 }
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -07001217
1218 // Invalidate the drag data
1219 mDragCell[0] = -1;
1220 mDragCell[1] = -1;
1221 mDragOutlineAnims[mDragOutlineCurrent].animateOut();
1222 mDragOutlineCurrent = (mDragOutlineCurrent + 1) % mDragOutlineAnims.length;
1223
Michael Jurka33945b22010-12-21 18:19:38 -08001224 setIsDragOverlapping(false);
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001225 }
1226
1227 /**
Winson Chungaafa03c2010-06-11 17:34:16 -07001228 * Mark a child as having been dropped.
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001229 * At the beginning of the drag operation, the child may have been on another
Patrick Dubroyce34a972010-10-19 10:34:32 -07001230 * screen, but it is re-parented before this method is called.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001231 *
1232 * @param child The child that is being dropped
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001233 */
Michael Jurkad3ef3062010-11-23 16:23:58 -08001234 void onDropChild(View child, boolean animate) {
Romain Guyd94533d2009-08-17 10:01:15 -07001235 if (child != null) {
1236 LayoutParams lp = (LayoutParams) child.getLayoutParams();
Romain Guyd94533d2009-08-17 10:01:15 -07001237 lp.isDragging = false;
Romain Guy84f296c2009-11-04 15:00:44 -08001238 lp.dropped = true;
Michael Jurkad3ef3062010-11-23 16:23:58 -08001239 lp.animateDrop = animate;
Patrick Dubroye3887cc2011-01-20 10:43:40 -08001240 child.setVisibility(animate ? View.INVISIBLE : View.VISIBLE);
Romain Guyd94533d2009-08-17 10:01:15 -07001241 child.requestLayout();
Romain Guyd94533d2009-08-17 10:01:15 -07001242 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001243 }
1244
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001245 /**
1246 * Start dragging the specified child
Winson Chungaafa03c2010-06-11 17:34:16 -07001247 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001248 * @param child The child that is being dragged
1249 */
1250 void onDragChild(View child) {
1251 LayoutParams lp = (LayoutParams) child.getLayoutParams();
1252 lp.isDragging = true;
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001253 }
1254
1255 /**
1256 * A drag event has begun over this layout.
1257 * It may have begun over this layout (in which case onDragChild is called first),
1258 * or it may have begun on another layout.
1259 */
Winson Chunga9abd0e2010-10-27 17:18:37 -07001260 void onDragEnter() {
Patrick Dubroyfe6bd872010-10-13 17:32:10 -07001261 if (!mDragging) {
Patrick Dubroyfe6bd872010-10-13 17:32:10 -07001262 // Fade in the drag indicators
1263 if (mCrosshairsAnimator != null) {
1264 mCrosshairsAnimator.animateIn();
1265 }
Joe Onorato4be866d2010-10-10 11:26:02 -07001266 }
1267 mDragging = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001268 }
Winson Chungaafa03c2010-06-11 17:34:16 -07001269
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001270 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001271 * Computes a bounding rectangle for a range of cells
Winson Chungaafa03c2010-06-11 17:34:16 -07001272 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001273 * @param cellX X coordinate of upper left corner expressed as a cell position
1274 * @param cellY Y coordinate of upper left corner expressed as a cell position
Winson Chungaafa03c2010-06-11 17:34:16 -07001275 * @param cellHSpan Width in cells
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001276 * @param cellVSpan Height in cells
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001277 * @param resultRect Rect into which to put the results
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001278 */
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001279 public void cellToRect(int cellX, int cellY, int cellHSpan, int cellVSpan, RectF resultRect) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001280 final int cellWidth = mCellWidth;
1281 final int cellHeight = mCellHeight;
1282 final int widthGap = mWidthGap;
1283 final int heightGap = mHeightGap;
Winson Chungaafa03c2010-06-11 17:34:16 -07001284
1285 final int hStartPadding = getLeftPadding();
1286 final int vStartPadding = getTopPadding();
1287
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001288 int width = cellHSpan * cellWidth + ((cellHSpan - 1) * widthGap);
1289 int height = cellVSpan * cellHeight + ((cellVSpan - 1) * heightGap);
1290
1291 int x = hStartPadding + cellX * (cellWidth + widthGap);
1292 int y = vStartPadding + cellY * (cellHeight + heightGap);
Winson Chungaafa03c2010-06-11 17:34:16 -07001293
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001294 resultRect.set(x, y, x + width, y + height);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001295 }
Winson Chungaafa03c2010-06-11 17:34:16 -07001296
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001297 /**
Winson Chungaafa03c2010-06-11 17:34:16 -07001298 * Computes the required horizontal and vertical cell spans to always
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001299 * fit the given rectangle.
Winson Chungaafa03c2010-06-11 17:34:16 -07001300 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001301 * @param width Width in pixels
1302 * @param height Height in pixels
Patrick Dubroy8f86ddc2010-07-16 13:55:32 -07001303 * @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 -08001304 */
Patrick Dubroy8f86ddc2010-07-16 13:55:32 -07001305 public int[] rectToCell(int width, int height, int[] result) {
Michael Jurka9987a5c2010-10-08 16:58:12 -07001306 return rectToCell(getResources(), width, height, result);
1307 }
1308
1309 public static int[] rectToCell(Resources resources, int width, int height, int[] result) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001310 // Always assume we're working with the smallest span to make sure we
1311 // reserve enough space in both orientations.
Joe Onorato79e56262009-09-21 15:23:04 -04001312 int actualWidth = resources.getDimensionPixelSize(R.dimen.workspace_cell_width);
1313 int actualHeight = resources.getDimensionPixelSize(R.dimen.workspace_cell_height);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001314 int smallerSize = Math.min(actualWidth, actualHeight);
Joe Onorato79e56262009-09-21 15:23:04 -04001315
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001316 // Always round up to next largest cell
1317 int spanX = (width + smallerSize) / smallerSize;
1318 int spanY = (height + smallerSize) / smallerSize;
Joe Onorato79e56262009-09-21 15:23:04 -04001319
Patrick Dubroy8f86ddc2010-07-16 13:55:32 -07001320 if (result == null) {
1321 return new int[] { spanX, spanY };
1322 }
1323 result[0] = spanX;
1324 result[1] = spanY;
1325 return result;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001326 }
1327
Michael Jurkaf12c75c2011-01-25 22:41:40 -08001328 public int[] cellSpansToSize(int hSpans, int vSpans) {
1329 int[] size = new int[2];
1330 size[0] = hSpans * mCellWidth + (hSpans - 1) * mWidthGap;
1331 size[1] = vSpans * mCellHeight + (vSpans - 1) * mHeightGap;
1332 return size;
1333 }
1334
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001335 /**
Patrick Dubroy047379a2010-12-19 22:02:04 -08001336 * Calculate the grid spans needed to fit given item
1337 */
1338 public void calculateSpans(ItemInfo info) {
1339 final int minWidth;
1340 final int minHeight;
1341
1342 if (info instanceof LauncherAppWidgetInfo) {
1343 minWidth = ((LauncherAppWidgetInfo) info).minWidth;
1344 minHeight = ((LauncherAppWidgetInfo) info).minHeight;
1345 } else if (info instanceof PendingAddWidgetInfo) {
1346 minWidth = ((PendingAddWidgetInfo) info).minWidth;
1347 minHeight = ((PendingAddWidgetInfo) info).minHeight;
1348 } else {
1349 // It's not a widget, so it must be 1x1
1350 info.spanX = info.spanY = 1;
1351 return;
1352 }
1353 int[] spans = rectToCell(minWidth, minHeight, null);
1354 info.spanX = spans[0];
1355 info.spanY = spans[1];
1356 }
1357
1358 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001359 * Find the first vacant cell, if there is one.
1360 *
1361 * @param vacant Holds the x and y coordinate of the vacant cell
1362 * @param spanX Horizontal cell span.
1363 * @param spanY Vertical cell span.
Winson Chungaafa03c2010-06-11 17:34:16 -07001364 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001365 * @return True if a vacant cell was found
1366 */
1367 public boolean getVacantCell(int[] vacant, int spanX, int spanY) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001368
Michael Jurka0280c3b2010-09-17 15:00:07 -07001369 return findVacantCell(vacant, spanX, spanY, mCountX, mCountY, mOccupied);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001370 }
1371
1372 static boolean findVacantCell(int[] vacant, int spanX, int spanY,
1373 int xCount, int yCount, boolean[][] occupied) {
1374
1375 for (int x = 0; x < xCount; x++) {
1376 for (int y = 0; y < yCount; y++) {
1377 boolean available = !occupied[x][y];
1378out: for (int i = x; i < x + spanX - 1 && x < xCount; i++) {
1379 for (int j = y; j < y + spanY - 1 && y < yCount; j++) {
1380 available = available && !occupied[i][j];
1381 if (!available) break out;
1382 }
1383 }
1384
1385 if (available) {
1386 vacant[0] = x;
1387 vacant[1] = y;
1388 return true;
1389 }
1390 }
1391 }
1392
1393 return false;
1394 }
1395
Michael Jurka0280c3b2010-09-17 15:00:07 -07001396 private void clearOccupiedCells() {
1397 for (int x = 0; x < mCountX; x++) {
1398 for (int y = 0; y < mCountY; y++) {
1399 mOccupied[x][y] = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001400 }
1401 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001402 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001403
Adam Cohen1b607ed2011-03-03 17:26:50 -08001404 /**
1405 * Given a view, determines how much that view can be expanded in all directions, in terms of
1406 * whether or not there are other items occupying adjacent cells. Used by the
1407 * AppWidgetResizeFrame to determine how the widget can be resized.
1408 */
Adam Cohend4844c32011-02-18 19:25:06 -08001409 public void getExpandabilityArrayForView(View view, int[] expandability) {
Adam Cohen1b607ed2011-03-03 17:26:50 -08001410 final LayoutParams lp = (LayoutParams) view.getLayoutParams();
Adam Cohend4844c32011-02-18 19:25:06 -08001411 boolean flag;
1412
Adam Cohen1b607ed2011-03-03 17:26:50 -08001413 expandability[AppWidgetResizeFrame.LEFT] = 0;
Adam Cohend4844c32011-02-18 19:25:06 -08001414 for (int x = lp.cellX - 1; x >= 0; x--) {
1415 flag = false;
1416 for (int y = lp.cellY; y < lp.cellY + lp.cellVSpan; y++) {
1417 if (mOccupied[x][y]) flag = true;
1418 }
1419 if (flag) break;
Adam Cohen1b607ed2011-03-03 17:26:50 -08001420 expandability[AppWidgetResizeFrame.LEFT]++;
Adam Cohend4844c32011-02-18 19:25:06 -08001421 }
1422
Adam Cohen1b607ed2011-03-03 17:26:50 -08001423 expandability[AppWidgetResizeFrame.TOP] = 0;
Adam Cohend4844c32011-02-18 19:25:06 -08001424 for (int y = lp.cellY - 1; y >= 0; y--) {
1425 flag = false;
1426 for (int x = lp.cellX; x < lp.cellX + lp.cellHSpan; x++) {
1427 if (mOccupied[x][y]) flag = true;
1428 }
1429 if (flag) break;
Adam Cohen1b607ed2011-03-03 17:26:50 -08001430 expandability[AppWidgetResizeFrame.TOP]++;
1431 }
Adam Cohend4844c32011-02-18 19:25:06 -08001432
Adam Cohen1b607ed2011-03-03 17:26:50 -08001433 expandability[AppWidgetResizeFrame.RIGHT] = 0;
Adam Cohend4844c32011-02-18 19:25:06 -08001434 for (int x = lp.cellX + lp.cellHSpan; x < mCountX; x++) {
1435 flag = false;
1436 for (int y = lp.cellY; y < lp.cellY + lp.cellVSpan; y++) {
1437 if (mOccupied[x][y]) flag = true;
1438 }
1439 if (flag) break;
Adam Cohen1b607ed2011-03-03 17:26:50 -08001440 expandability[AppWidgetResizeFrame.RIGHT]++;
1441 }
Adam Cohend4844c32011-02-18 19:25:06 -08001442
Adam Cohen1b607ed2011-03-03 17:26:50 -08001443 expandability[AppWidgetResizeFrame.BOTTOM] = 0;
Adam Cohend4844c32011-02-18 19:25:06 -08001444 for (int y = lp.cellY + lp.cellVSpan; y < mCountY; y++) {
1445 flag = false;
1446 for (int x = lp.cellX; x < lp.cellX + lp.cellHSpan; x++) {
1447 if (mOccupied[x][y]) flag = true;
1448 }
1449 if (flag) break;
Adam Cohen1b607ed2011-03-03 17:26:50 -08001450 expandability[AppWidgetResizeFrame.BOTTOM]++;
1451 }
Adam Cohend4844c32011-02-18 19:25:06 -08001452 }
1453
Michael Jurka0280c3b2010-09-17 15:00:07 -07001454 public void onMove(View view, int newCellX, int newCellY) {
1455 LayoutParams lp = (LayoutParams) view.getLayoutParams();
1456 markCellsAsUnoccupiedForView(view);
1457 markCellsForView(newCellX, newCellY, lp.cellHSpan, lp.cellVSpan, true);
1458 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001459
Adam Cohend4844c32011-02-18 19:25:06 -08001460 public void markCellsAsOccupiedForView(View view) {
Michael Jurka8c920dd2011-01-20 14:16:56 -08001461 if (view == null || view.getParent() != mChildren) return;
Michael Jurka0280c3b2010-09-17 15:00:07 -07001462 LayoutParams lp = (LayoutParams) view.getLayoutParams();
1463 markCellsForView(lp.cellX, lp.cellY, lp.cellHSpan, lp.cellVSpan, true);
1464 }
1465
Adam Cohend4844c32011-02-18 19:25:06 -08001466 public void markCellsAsUnoccupiedForView(View view) {
Michael Jurka8c920dd2011-01-20 14:16:56 -08001467 if (view == null || view.getParent() != mChildren) return;
Michael Jurka0280c3b2010-09-17 15:00:07 -07001468 LayoutParams lp = (LayoutParams) view.getLayoutParams();
1469 markCellsForView(lp.cellX, lp.cellY, lp.cellHSpan, lp.cellVSpan, false);
1470 }
1471
1472 private void markCellsForView(int cellX, int cellY, int spanX, int spanY, boolean value) {
1473 for (int x = cellX; x < cellX + spanX && x < mCountX; x++) {
1474 for (int y = cellY; y < cellY + spanY && y < mCountY; y++) {
1475 mOccupied[x][y] = value;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001476 }
1477 }
1478 }
1479
1480 @Override
1481 public ViewGroup.LayoutParams generateLayoutParams(AttributeSet attrs) {
1482 return new CellLayout.LayoutParams(getContext(), attrs);
1483 }
1484
1485 @Override
1486 protected boolean checkLayoutParams(ViewGroup.LayoutParams p) {
1487 return p instanceof CellLayout.LayoutParams;
1488 }
1489
1490 @Override
1491 protected ViewGroup.LayoutParams generateLayoutParams(ViewGroup.LayoutParams p) {
1492 return new CellLayout.LayoutParams(p);
1493 }
1494
Winson Chungaafa03c2010-06-11 17:34:16 -07001495 public static class CellLayoutAnimationController extends LayoutAnimationController {
1496 public CellLayoutAnimationController(Animation animation, float delay) {
1497 super(animation, delay);
1498 }
1499
1500 @Override
1501 protected long getDelayForView(View view) {
1502 return (int) (Math.random() * 150);
1503 }
1504 }
1505
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001506 public static class LayoutParams extends ViewGroup.MarginLayoutParams {
1507 /**
1508 * Horizontal location of the item in the grid.
1509 */
1510 @ViewDebug.ExportedProperty
1511 public int cellX;
1512
1513 /**
1514 * Vertical location of the item in the grid.
1515 */
1516 @ViewDebug.ExportedProperty
1517 public int cellY;
1518
1519 /**
1520 * Number of cells spanned horizontally by the item.
1521 */
1522 @ViewDebug.ExportedProperty
1523 public int cellHSpan;
1524
1525 /**
1526 * Number of cells spanned vertically by the item.
1527 */
1528 @ViewDebug.ExportedProperty
1529 public int cellVSpan;
Winson Chungaafa03c2010-06-11 17:34:16 -07001530
Adam Cohen1b607ed2011-03-03 17:26:50 -08001531 /**
1532 * Indicates whether the item will set its x, y, width and height parameters freely,
1533 * or whether these will be computed based on cellX, cellY, cellHSpan and cellVSpan.
1534 */
Adam Cohend4844c32011-02-18 19:25:06 -08001535 public boolean isLockedToGrid = true;
1536
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001537 /**
1538 * Is this item currently being dragged
1539 */
1540 public boolean isDragging;
1541
1542 // X coordinate of the view in the layout.
1543 @ViewDebug.ExportedProperty
1544 int x;
1545 // Y coordinate of the view in the layout.
1546 @ViewDebug.ExportedProperty
1547 int y;
1548
Patrick Dubroyce34a972010-10-19 10:34:32 -07001549 /**
1550 * The old X coordinate of this item, relative to its current parent.
1551 * Used to animate the item into its new position.
1552 */
1553 int oldX;
1554
1555 /**
1556 * The old Y coordinate of this item, relative to its current parent.
1557 * Used to animate the item into its new position.
1558 */
1559 int oldY;
1560
Romain Guy84f296c2009-11-04 15:00:44 -08001561 boolean dropped;
Romain Guyfcb9e712009-10-02 16:06:52 -07001562
Michael Jurkad3ef3062010-11-23 16:23:58 -08001563 boolean animateDrop;
1564
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001565 public LayoutParams(Context c, AttributeSet attrs) {
1566 super(c, attrs);
1567 cellHSpan = 1;
1568 cellVSpan = 1;
1569 }
1570
1571 public LayoutParams(ViewGroup.LayoutParams source) {
1572 super(source);
1573 cellHSpan = 1;
1574 cellVSpan = 1;
1575 }
Winson Chungaafa03c2010-06-11 17:34:16 -07001576
1577 public LayoutParams(LayoutParams source) {
1578 super(source);
1579 this.cellX = source.cellX;
1580 this.cellY = source.cellY;
1581 this.cellHSpan = source.cellHSpan;
1582 this.cellVSpan = source.cellVSpan;
1583 }
1584
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001585 public LayoutParams(int cellX, int cellY, int cellHSpan, int cellVSpan) {
Romain Guy8f19cdd2010-01-08 15:07:00 -08001586 super(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001587 this.cellX = cellX;
1588 this.cellY = cellY;
1589 this.cellHSpan = cellHSpan;
1590 this.cellVSpan = cellVSpan;
1591 }
1592
1593 public void setup(int cellWidth, int cellHeight, int widthGap, int heightGap,
1594 int hStartPadding, int vStartPadding) {
Adam Cohend4844c32011-02-18 19:25:06 -08001595 if (isLockedToGrid) {
1596 final int myCellHSpan = cellHSpan;
1597 final int myCellVSpan = cellVSpan;
1598 final int myCellX = cellX;
1599 final int myCellY = cellY;
Adam Cohen1b607ed2011-03-03 17:26:50 -08001600
Adam Cohend4844c32011-02-18 19:25:06 -08001601 width = myCellHSpan * cellWidth + ((myCellHSpan - 1) * widthGap) -
1602 leftMargin - rightMargin;
1603 height = myCellVSpan * cellHeight + ((myCellVSpan - 1) * heightGap) -
1604 topMargin - bottomMargin;
Adam Cohend4844c32011-02-18 19:25:06 -08001605 x = hStartPadding + myCellX * (cellWidth + widthGap) + leftMargin;
1606 y = vStartPadding + myCellY * (cellHeight + heightGap) + topMargin;
1607 }
1608 }
Winson Chungaafa03c2010-06-11 17:34:16 -07001609
Adam Cohend4844c32011-02-18 19:25:06 -08001610 public void setWidth(int width) {
1611 this.width = width;
1612 }
Winson Chungaafa03c2010-06-11 17:34:16 -07001613
Adam Cohend4844c32011-02-18 19:25:06 -08001614 public int getWidth() {
1615 return width;
1616 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001617
Adam Cohend4844c32011-02-18 19:25:06 -08001618 public void setHeight(int height) {
1619 this.height = height;
1620 }
1621
1622 public int getHeight() {
1623 return height;
1624 }
1625
1626 public void setX(int x) {
1627 this.x = x;
1628 }
1629
1630 public int getX() {
1631 return x;
1632 }
1633
1634 public void setY(int y) {
1635 this.y = y;
1636 }
1637
1638 public int getY() {
1639 return y;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001640 }
Winson Chungaafa03c2010-06-11 17:34:16 -07001641
1642 public String toString() {
1643 return "(" + this.cellX + ", " + this.cellY + ")";
1644 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001645 }
1646
Michael Jurka0280c3b2010-09-17 15:00:07 -07001647 // This class stores info for two purposes:
1648 // 1. When dragging items (mDragInfo in Workspace), we store the View, its cellX & cellY,
1649 // its spanX, spanY, and the screen it is on
1650 // 2. When long clicking on an empty cell in a CellLayout, we save information about the
1651 // cellX and cellY coordinates and which page was clicked. We then set this as a tag on
1652 // the CellLayout that was long clicked
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001653 static final class CellInfo implements ContextMenu.ContextMenuInfo {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001654 View cell;
Michael Jurkaa63c4522010-08-19 13:52:27 -07001655 int cellX = -1;
1656 int cellY = -1;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001657 int spanX;
1658 int spanY;
1659 int screen;
1660 boolean valid;
1661
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001662 @Override
1663 public String toString() {
Winson Chungaafa03c2010-06-11 17:34:16 -07001664 return "Cell[view=" + (cell == null ? "null" : cell.getClass())
1665 + ", x=" + cellX + ", y=" + cellY + "]";
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001666 }
1667 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001668}