blob: 62e32d254972e4832e4ebf1dc5beea5724c73f85 [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
Patrick Dubroy6569f2c2010-07-12 14:25:18 -070019import com.android.launcher.R;
Winson Chungaafa03c2010-06-11 17:34:16 -070020
21import android.app.WallpaperManager;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080022import android.content.Context;
Joe Onorato79e56262009-09-21 15:23:04 -040023import android.content.res.Resources;
Winson Chungaafa03c2010-06-11 17:34:16 -070024import android.content.res.TypedArray;
25import android.graphics.Canvas;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080026import android.graphics.Rect;
27import android.graphics.RectF;
Patrick Dubroy6569f2c2010-07-12 14:25:18 -070028import android.graphics.drawable.Drawable;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080029import android.util.AttributeSet;
30import android.view.ContextMenu;
31import android.view.MotionEvent;
32import android.view.View;
33import android.view.ViewDebug;
34import android.view.ViewGroup;
Winson Chungaafa03c2010-06-11 17:34:16 -070035import android.view.animation.Animation;
36import android.view.animation.LayoutAnimationController;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080037
Patrick Dubroy6569f2c2010-07-12 14:25:18 -070038import java.util.Arrays;
Romain Guyedcce092010-03-04 13:03:17 -080039
The Android Open Source Project31dd5032009-03-03 19:32:27 -080040public class CellLayout extends ViewGroup {
Winson Chungaafa03c2010-06-11 17:34:16 -070041 static final String TAG = "CellLayout";
42
The Android Open Source Project31dd5032009-03-03 19:32:27 -080043 private int mCellWidth;
44 private int mCellHeight;
Winson Chungaafa03c2010-06-11 17:34:16 -070045
Winson Chungaafa03c2010-06-11 17:34:16 -070046 private int mLeftPadding;
47 private int mRightPadding;
48 private int mTopPadding;
49 private int mBottomPadding;
50
Adam Cohend22015c2010-07-26 22:02:18 -070051 private int mCountX;
52 private int mCountY;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080053
54 private int mWidthGap;
55 private int mHeightGap;
56
57 private final Rect mRect = new Rect();
Patrick Dubroy8f86ddc2010-07-16 13:55:32 -070058 private final RectF mRectF = new RectF();
The Android Open Source Project31dd5032009-03-03 19:32:27 -080059 private final CellInfo mCellInfo = new CellInfo();
Winson Chungaafa03c2010-06-11 17:34:16 -070060
Patrick Dubroy6569f2c2010-07-12 14:25:18 -070061 // This is a temporary variable to prevent having to allocate a new object just to
62 // return an (x, y) value from helper functions. Do NOT use it to maintain other state.
63 private final int[] mTmpCellXY = new int[2];
64
The Android Open Source Project31dd5032009-03-03 19:32:27 -080065 boolean[][] mOccupied;
66
Michael Jurkadee05892010-07-27 10:01:56 -070067 private OnTouchListener mInterceptTouchListener;
68
Michael Jurka5f1c5092010-09-03 14:15:02 -070069 private float mBackgroundAlpha;
70 private final Rect mBackgroundLayoutRect = new Rect();
71 private Drawable mBackground;
72 private Drawable mBackgroundHover;
Michael Jurkaa63c4522010-08-19 13:52:27 -070073 // If we're actively dragging something over this screen and it's small,
74 // mHover is true
75 private boolean mHover = false;
Michael Jurkadee05892010-07-27 10:01:56 -070076
Patrick Dubroy6569f2c2010-07-12 14:25:18 -070077 private final RectF mDragRect = new RectF();
78
79 // When dragging, used to indicate a vacant drop location
80 private Drawable mVacantDrawable;
81
82 // When dragging, used to indicate an occupied drop location
83 private Drawable mOccupiedDrawable;
84
85 // Updated to point to mVacantDrawable or mOccupiedDrawable, as appropriate
86 private Drawable mDragRectDrawable;
87
88 // When a drag operation is in progress, holds the nearest cell to the touch point
89 private final int[] mDragCell = new int[2];
The Android Open Source Project31dd5032009-03-03 19:32:27 -080090
Winson Chungaafa03c2010-06-11 17:34:16 -070091 private final WallpaperManager mWallpaperManager;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080092
93 public CellLayout(Context context) {
94 this(context, null);
95 }
96
97 public CellLayout(Context context, AttributeSet attrs) {
98 this(context, attrs, 0);
99 }
100
101 public CellLayout(Context context, AttributeSet attrs, int defStyle) {
102 super(context, attrs, defStyle);
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700103
104 // A ViewGroup usually does not draw, but CellLayout needs to draw a rectangle to show
105 // the user where a dragged item will land when dropped.
106 setWillNotDraw(false);
107 mVacantDrawable = getResources().getDrawable(R.drawable.rounded_rect_green);
108 mOccupiedDrawable = getResources().getDrawable(R.drawable.rounded_rect_red);
109
Michael Jurkaa63c4522010-08-19 13:52:27 -0700110 if (LauncherApplication.isScreenXLarge()) {
Michael Jurka5f1c5092010-09-03 14:15:02 -0700111 mBackground = getResources().getDrawable(R.drawable.mini_home_screen_bg);
112 mBackground.setFilterBitmap(true);
113 mBackgroundHover = getResources().getDrawable(R.drawable.mini_home_screen_bg_hover);
114 mBackgroundHover.setFilterBitmap(true);
Michael Jurkaa63c4522010-08-19 13:52:27 -0700115 }
116
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800117 TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.CellLayout, defStyle, 0);
118
119 mCellWidth = a.getDimensionPixelSize(R.styleable.CellLayout_cellWidth, 10);
120 mCellHeight = a.getDimensionPixelSize(R.styleable.CellLayout_cellHeight, 10);
Winson Chungaafa03c2010-06-11 17:34:16 -0700121
Adam Cohend22015c2010-07-26 22:02:18 -0700122 mLeftPadding =
123 a.getDimensionPixelSize(R.styleable.CellLayout_xAxisStartPadding, 10);
124 mRightPadding =
125 a.getDimensionPixelSize(R.styleable.CellLayout_xAxisEndPadding, 10);
126 mTopPadding =
127 a.getDimensionPixelSize(R.styleable.CellLayout_yAxisStartPadding, 10);
128 mBottomPadding =
129 a.getDimensionPixelSize(R.styleable.CellLayout_yAxisEndPadding, 10);
Winson Chungaafa03c2010-06-11 17:34:16 -0700130
Adam Cohend22015c2010-07-26 22:02:18 -0700131 mCountX = LauncherModel.getCellCountX();
132 mCountY = LauncherModel.getCellCountY();
Michael Jurka0280c3b2010-09-17 15:00:07 -0700133 mOccupied = new boolean[mCountX][mCountY];
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800134
135 a.recycle();
136
137 setAlwaysDrawnWithCacheEnabled(false);
138
Romain Guy84f296c2009-11-04 15:00:44 -0800139 mWallpaperManager = WallpaperManager.getInstance(getContext());
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800140 }
141
Michael Jurkaa63c4522010-08-19 13:52:27 -0700142 public void setHover(boolean value) {
143 if (mHover != value) {
144 invalidate();
145 }
146 mHover = value;
147 }
148
Jeff Sharkey83f111d2009-04-20 21:03:13 -0700149 @Override
Romain Guya6abce82009-11-10 02:54:41 -0800150 public void dispatchDraw(Canvas canvas) {
Michael Jurka5f1c5092010-09-03 14:15:02 -0700151 if (mBackgroundAlpha > 0.0f) {
152 final Drawable bg = mHover ? mBackgroundHover : mBackground;
153 bg.setAlpha((int) (mBackgroundAlpha * 255));
Michael Jurkaa63c4522010-08-19 13:52:27 -0700154 bg.draw(canvas);
155 }
Romain Guya6abce82009-11-10 02:54:41 -0800156 super.dispatchDraw(canvas);
157 }
158
159 @Override
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700160 protected void onDraw(Canvas canvas) {
161 if (!mDragRect.isEmpty()) {
162 mDragRectDrawable.setBounds(
163 (int)mDragRect.left,
164 (int)mDragRect.top,
165 (int)mDragRect.right,
166 (int)mDragRect.bottom);
167 mDragRectDrawable.draw(canvas);
168 }
Michael Jurka5f1c5092010-09-03 14:15:02 -0700169 super.onDraw(canvas);
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700170 }
171
172 @Override
Jeff Sharkey83f111d2009-04-20 21:03:13 -0700173 public void cancelLongPress() {
174 super.cancelLongPress();
175
176 // Cancel long press for all children
177 final int count = getChildCount();
178 for (int i = 0; i < count; i++) {
179 final View child = getChildAt(i);
180 child.cancelLongPress();
181 }
182 }
183
Michael Jurkadee05892010-07-27 10:01:56 -0700184 public void setOnInterceptTouchListener(View.OnTouchListener listener) {
185 mInterceptTouchListener = listener;
186 }
187
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800188 int getCountX() {
Adam Cohend22015c2010-07-26 22:02:18 -0700189 return mCountX;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800190 }
191
192 int getCountY() {
Adam Cohend22015c2010-07-26 22:02:18 -0700193 return mCountY;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800194 }
195
Winson Chungaafa03c2010-06-11 17:34:16 -0700196 // Takes canonical layout parameters
197 public boolean addViewToCellLayout(View child, int index, int childId, LayoutParams params) {
198 final LayoutParams lp = params;
199
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800200 // Generate an id for each view, this assumes we have at most 256x256 cells
201 // per workspace screen
Adam Cohend22015c2010-07-26 22:02:18 -0700202 if (lp.cellX >= 0 && lp.cellX <= mCountX - 1 && lp.cellY >= 0 && lp.cellY <= mCountY - 1) {
Winson Chungaafa03c2010-06-11 17:34:16 -0700203 // If the horizontal or vertical span is set to -1, it is taken to
204 // mean that it spans the extent of the CellLayout
Adam Cohend22015c2010-07-26 22:02:18 -0700205 if (lp.cellHSpan < 0) lp.cellHSpan = mCountX;
206 if (lp.cellVSpan < 0) lp.cellVSpan = mCountY;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800207
Winson Chungaafa03c2010-06-11 17:34:16 -0700208 child.setId(childId);
209
Michael Jurkadee05892010-07-27 10:01:56 -0700210 // We might be in the middle or end of shrinking/fading to a dimmed view
211 // Make sure this view's alpha is set the same as all the rest of the views
Michael Jurka5f1c5092010-09-03 14:15:02 -0700212 child.setAlpha(getAlpha());
Winson Chungaafa03c2010-06-11 17:34:16 -0700213 addView(child, index, lp);
Michael Jurkadee05892010-07-27 10:01:56 -0700214
Michael Jurka0280c3b2010-09-17 15:00:07 -0700215 markCellsAsOccupiedForView(child);
216
Winson Chungaafa03c2010-06-11 17:34:16 -0700217 return true;
218 }
219 return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800220 }
221
222 @Override
Michael Jurka0280c3b2010-09-17 15:00:07 -0700223 public void removeAllViews() {
224 clearOccupiedCells();
225 }
226
227 @Override
228 public void removeAllViewsInLayout() {
229 clearOccupiedCells();
230 }
231
232 @Override
233 public void removeView(View view) {
234 markCellsAsUnoccupiedForView(view);
235 super.removeView(view);
236 }
237
238 @Override
239 public void removeViewAt(int index) {
240 markCellsAsUnoccupiedForView(getChildAt(index));
241 super.removeViewAt(index);
242 }
243
244 @Override
245 public void removeViewInLayout(View view) {
246 markCellsAsUnoccupiedForView(view);
247 super.removeViewInLayout(view);
248 }
249
250 @Override
251 public void removeViews(int start, int count) {
252 for (int i = start; i < start + count; i++) {
253 markCellsAsUnoccupiedForView(getChildAt(i));
254 }
255 super.removeViews(start, count);
256 }
257
258 @Override
259 public void removeViewsInLayout(int start, int count) {
260 for (int i = start; i < start + count; i++) {
261 markCellsAsUnoccupiedForView(getChildAt(i));
262 }
263 super.removeViewsInLayout(start, count);
264 }
265
266 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800267 public void requestChildFocus(View child, View focused) {
268 super.requestChildFocus(child, focused);
269 if (child != null) {
270 Rect r = new Rect();
271 child.getDrawingRect(r);
272 requestRectangleOnScreen(r);
273 }
274 }
275
276 @Override
277 protected void onAttachedToWindow() {
278 super.onAttachedToWindow();
279 mCellInfo.screen = ((ViewGroup) getParent()).indexOfChild(this);
280 }
281
Michael Jurkaaf442092010-06-10 17:01:57 -0700282 public void setTagToCellInfoForPoint(int touchX, int touchY) {
283 final CellInfo cellInfo = mCellInfo;
284 final Rect frame = mRect;
285 final int x = touchX + mScrollX;
286 final int y = touchY + mScrollY;
287 final int count = getChildCount();
288
289 boolean found = false;
290 for (int i = count - 1; i >= 0; i--) {
291 final View child = getChildAt(i);
292
293 if ((child.getVisibility()) == VISIBLE || child.getAnimation() != null) {
294 child.getHitRect(frame);
295 if (frame.contains(x, y)) {
296 final LayoutParams lp = (LayoutParams) child.getLayoutParams();
297 cellInfo.cell = child;
298 cellInfo.cellX = lp.cellX;
299 cellInfo.cellY = lp.cellY;
300 cellInfo.spanX = lp.cellHSpan;
301 cellInfo.spanY = lp.cellVSpan;
302 cellInfo.valid = true;
303 found = true;
Michael Jurkaaf442092010-06-10 17:01:57 -0700304 break;
305 }
306 }
307 }
Winson Chungaafa03c2010-06-11 17:34:16 -0700308
Michael Jurkaaf442092010-06-10 17:01:57 -0700309 if (!found) {
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700310 final int cellXY[] = mTmpCellXY;
Michael Jurkaaf442092010-06-10 17:01:57 -0700311 pointToCellExact(x, y, cellXY);
312
Michael Jurkaaf442092010-06-10 17:01:57 -0700313 cellInfo.cell = null;
314 cellInfo.cellX = cellXY[0];
315 cellInfo.cellY = cellXY[1];
316 cellInfo.spanX = 1;
317 cellInfo.spanY = 1;
Michael Jurka0280c3b2010-09-17 15:00:07 -0700318 cellInfo.valid = cellXY[0] >= 0 && cellXY[1] >= 0 && cellXY[0] < mCountX &&
319 cellXY[1] < mCountY && !mOccupied[cellXY[0]][cellXY[1]];
Michael Jurkaaf442092010-06-10 17:01:57 -0700320 }
321 setTag(cellInfo);
322 }
323
Winson Chungaafa03c2010-06-11 17:34:16 -0700324
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800325 @Override
326 public boolean onInterceptTouchEvent(MotionEvent ev) {
Michael Jurkadee05892010-07-27 10:01:56 -0700327 if (mInterceptTouchListener != null && mInterceptTouchListener.onTouch(this, ev)) {
328 return true;
329 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800330 final int action = ev.getAction();
331 final CellInfo cellInfo = mCellInfo;
332
333 if (action == MotionEvent.ACTION_DOWN) {
Michael Jurkaaf442092010-06-10 17:01:57 -0700334 setTagToCellInfoForPoint((int) ev.getX(), (int) ev.getY());
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800335 } else if (action == MotionEvent.ACTION_UP) {
336 cellInfo.cell = null;
337 cellInfo.cellX = -1;
338 cellInfo.cellY = -1;
339 cellInfo.spanX = 0;
340 cellInfo.spanY = 0;
341 cellInfo.valid = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800342 setTag(cellInfo);
343 }
344
345 return false;
346 }
347
348 @Override
349 public CellInfo getTag() {
Michael Jurka0280c3b2010-09-17 15:00:07 -0700350 return (CellInfo) super.getTag();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800351 }
352
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700353 /**
354 * Check if the row 'y' is empty from columns 'left' to 'right', inclusive.
355 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800356 private static boolean isRowEmpty(int y, int left, int right, boolean[][] occupied) {
357 for (int x = left; x <= right; x++) {
358 if (occupied[x][y]) {
359 return false;
360 }
361 }
362 return true;
363 }
364
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800365 /**
Winson Chungaafa03c2010-06-11 17:34:16 -0700366 * Given a point, return the cell that strictly encloses that point
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800367 * @param x X coordinate of the point
368 * @param y Y coordinate of the point
369 * @param result Array of 2 ints to hold the x and y coordinate of the cell
370 */
371 void pointToCellExact(int x, int y, int[] result) {
Winson Chungaafa03c2010-06-11 17:34:16 -0700372 final int hStartPadding = getLeftPadding();
373 final int vStartPadding = getTopPadding();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800374
375 result[0] = (x - hStartPadding) / (mCellWidth + mWidthGap);
376 result[1] = (y - vStartPadding) / (mCellHeight + mHeightGap);
377
Adam Cohend22015c2010-07-26 22:02:18 -0700378 final int xAxis = mCountX;
379 final int yAxis = mCountY;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800380
381 if (result[0] < 0) result[0] = 0;
382 if (result[0] >= xAxis) result[0] = xAxis - 1;
383 if (result[1] < 0) result[1] = 0;
384 if (result[1] >= yAxis) result[1] = yAxis - 1;
385 }
Winson Chungaafa03c2010-06-11 17:34:16 -0700386
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800387 /**
388 * Given a point, return the cell that most closely encloses that point
389 * @param x X coordinate of the point
390 * @param y Y coordinate of the point
391 * @param result Array of 2 ints to hold the x and y coordinate of the cell
392 */
393 void pointToCellRounded(int x, int y, int[] result) {
394 pointToCellExact(x + (mCellWidth / 2), y + (mCellHeight / 2), result);
395 }
396
397 /**
398 * Given a cell coordinate, return the point that represents the upper left corner of that cell
Winson Chungaafa03c2010-06-11 17:34:16 -0700399 *
400 * @param cellX X coordinate of the cell
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800401 * @param cellY Y coordinate of the cell
Winson Chungaafa03c2010-06-11 17:34:16 -0700402 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800403 * @param result Array of 2 ints to hold the x and y coordinate of the point
404 */
405 void cellToPoint(int cellX, int cellY, int[] result) {
Winson Chungaafa03c2010-06-11 17:34:16 -0700406 final int hStartPadding = getLeftPadding();
407 final int vStartPadding = getTopPadding();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800408
409 result[0] = hStartPadding + cellX * (mCellWidth + mWidthGap);
410 result[1] = vStartPadding + cellY * (mCellHeight + mHeightGap);
411 }
412
Romain Guy84f296c2009-11-04 15:00:44 -0800413 int getCellWidth() {
414 return mCellWidth;
415 }
416
417 int getCellHeight() {
418 return mCellHeight;
419 }
420
Romain Guy1a304a12009-11-10 00:02:32 -0800421 int getLeftPadding() {
Winson Chungaafa03c2010-06-11 17:34:16 -0700422 return mLeftPadding;
Romain Guy1a304a12009-11-10 00:02:32 -0800423 }
424
425 int getTopPadding() {
Winson Chungaafa03c2010-06-11 17:34:16 -0700426 return mTopPadding;
Romain Guy1a304a12009-11-10 00:02:32 -0800427 }
428
429 int getRightPadding() {
Winson Chungaafa03c2010-06-11 17:34:16 -0700430 return mRightPadding;
Romain Guy1a304a12009-11-10 00:02:32 -0800431 }
432
433 int getBottomPadding() {
Winson Chungaafa03c2010-06-11 17:34:16 -0700434 return mBottomPadding;
Romain Guy1a304a12009-11-10 00:02:32 -0800435 }
436
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800437 @Override
438 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
439 // TODO: currently ignoring padding
Winson Chungaafa03c2010-06-11 17:34:16 -0700440
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800441 int widthSpecMode = MeasureSpec.getMode(widthMeasureSpec);
Winson Chungaafa03c2010-06-11 17:34:16 -0700442 int widthSpecSize = MeasureSpec.getSize(widthMeasureSpec);
443
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800444 int heightSpecMode = MeasureSpec.getMode(heightMeasureSpec);
445 int heightSpecSize = MeasureSpec.getSize(heightMeasureSpec);
Winson Chungaafa03c2010-06-11 17:34:16 -0700446
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800447 if (widthSpecMode == MeasureSpec.UNSPECIFIED || heightSpecMode == MeasureSpec.UNSPECIFIED) {
448 throw new RuntimeException("CellLayout cannot have UNSPECIFIED dimensions");
449 }
450
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800451 final int cellWidth = mCellWidth;
452 final int cellHeight = mCellHeight;
453
Adam Cohend22015c2010-07-26 22:02:18 -0700454 int numWidthGaps = mCountX - 1;
455 int numHeightGaps = mCountY - 1;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800456
Michael Jurka0280c3b2010-09-17 15:00:07 -0700457 int vSpaceLeft = heightSpecSize - mTopPadding - mBottomPadding - (cellHeight * mCountY);
Adam Cohend22015c2010-07-26 22:02:18 -0700458 mHeightGap = vSpaceLeft / numHeightGaps;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800459
Michael Jurka0280c3b2010-09-17 15:00:07 -0700460 int hSpaceLeft = widthSpecSize - mLeftPadding - mRightPadding - (cellWidth * mCountX);
Adam Cohend22015c2010-07-26 22:02:18 -0700461 mWidthGap = hSpaceLeft / numWidthGaps;
Winson Chungaafa03c2010-06-11 17:34:16 -0700462
Michael Jurka5f1c5092010-09-03 14:15:02 -0700463 // center it around the min gaps
464 int minGap = Math.min(mWidthGap, mHeightGap);
465 mWidthGap = mHeightGap = minGap;
466
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800467 int count = getChildCount();
468
469 for (int i = 0; i < count; i++) {
470 View child = getChildAt(i);
471 LayoutParams lp = (LayoutParams) child.getLayoutParams();
Winson Chungaafa03c2010-06-11 17:34:16 -0700472 lp.setup(cellWidth, cellHeight, mWidthGap, mHeightGap,
473 mLeftPadding, mTopPadding);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800474
Michael Jurka0280c3b2010-09-17 15:00:07 -0700475 int childWidthMeasureSpec = MeasureSpec.makeMeasureSpec(lp.width, MeasureSpec.EXACTLY);
Winson Chungaafa03c2010-06-11 17:34:16 -0700476 int childheightMeasureSpec = MeasureSpec.makeMeasureSpec(lp.height,
477 MeasureSpec.EXACTLY);
478
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800479 child.measure(childWidthMeasureSpec, childheightMeasureSpec);
480 }
Michael Jurka5f1c5092010-09-03 14:15:02 -0700481 if (widthSpecMode == MeasureSpec.AT_MOST) {
482 int newWidth = mLeftPadding + mRightPadding + (mCountX * cellWidth) +
483 ((mCountX - 1) * minGap);
484 int newHeight = mTopPadding + mBottomPadding + (mCountY * cellHeight) +
485 ((mCountY - 1) * minGap);
486 setMeasuredDimension(newWidth, newHeight);
487 } else if (widthSpecMode == MeasureSpec.EXACTLY) {
488 setMeasuredDimension(widthSpecSize, heightSpecSize);
489 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800490 }
491
492 @Override
Michael Jurka28750fb2010-09-24 17:43:49 -0700493 protected void onLayout(boolean changed, int l, int t, int r, int b) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800494 int count = getChildCount();
495
496 for (int i = 0; i < count; i++) {
497 View child = getChildAt(i);
498 if (child.getVisibility() != GONE) {
499
500 CellLayout.LayoutParams lp = (CellLayout.LayoutParams) child.getLayoutParams();
501
502 int childLeft = lp.x;
503 int childTop = lp.y;
504 child.layout(childLeft, childTop, childLeft + lp.width, childTop + lp.height);
Romain Guy84f296c2009-11-04 15:00:44 -0800505
506 if (lp.dropped) {
507 lp.dropped = false;
508
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700509 final int[] cellXY = mTmpCellXY;
Romain Guy06762ab2010-01-25 16:51:08 -0800510 getLocationOnScreen(cellXY);
Romain Guy84f296c2009-11-04 15:00:44 -0800511 mWallpaperManager.sendWallpaperCommand(getWindowToken(), "android.home.drop",
Romain Guy06762ab2010-01-25 16:51:08 -0800512 cellXY[0] + childLeft + lp.width / 2,
513 cellXY[1] + childTop + lp.height / 2, 0, null);
Romain Guy84f296c2009-11-04 15:00:44 -0800514 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800515 }
516 }
517 }
518
519 @Override
Michael Jurkadee05892010-07-27 10:01:56 -0700520 protected void onSizeChanged(int w, int h, int oldw, int oldh) {
521 super.onSizeChanged(w, h, oldw, oldh);
Michael Jurka5f1c5092010-09-03 14:15:02 -0700522 mBackgroundLayoutRect.set(0, 0, w, h);
523 if (mBackground != null) {
524 mBackground.setBounds(mBackgroundLayoutRect);
Michael Jurkaa63c4522010-08-19 13:52:27 -0700525 }
Michael Jurka5f1c5092010-09-03 14:15:02 -0700526 if (mBackgroundHover != null) {
527 mBackgroundHover.setBounds(mBackgroundLayoutRect);
Michael Jurkaa63c4522010-08-19 13:52:27 -0700528 }
Michael Jurkadee05892010-07-27 10:01:56 -0700529 }
530
531 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800532 protected void setChildrenDrawingCacheEnabled(boolean enabled) {
533 final int count = getChildCount();
534 for (int i = 0; i < count; i++) {
535 final View view = getChildAt(i);
536 view.setDrawingCacheEnabled(enabled);
537 // Update the drawing caches
Adam Powellfefa0ce2010-05-03 10:23:50 -0700538 view.buildDrawingCache(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800539 }
540 }
541
542 @Override
543 protected void setChildrenDrawnWithCacheEnabled(boolean enabled) {
544 super.setChildrenDrawnWithCacheEnabled(enabled);
545 }
546
Michael Jurka5f1c5092010-09-03 14:15:02 -0700547 public float getBackgroundAlpha() {
548 return mBackgroundAlpha;
Michael Jurkadee05892010-07-27 10:01:56 -0700549 }
550
Michael Jurka5f1c5092010-09-03 14:15:02 -0700551 public void setBackgroundAlpha(float alpha) {
552 mBackgroundAlpha = alpha;
Michael Jurka0142d492010-08-25 17:46:15 -0700553 invalidate();
Michael Jurkadee05892010-07-27 10:01:56 -0700554 }
555
Michael Jurka5f1c5092010-09-03 14:15:02 -0700556 // Need to return true to let the view system know we know how to handle alpha-- this is
557 // because when our children have an alpha of 0.0f, they are still rendering their "dimmed"
558 // versions
559 @Override
560 protected boolean onSetAlpha(int alpha) {
561 return true;
562 }
563
564 public void setAlpha(float alpha) {
565 setChildrenAlpha(alpha);
566 super.setAlpha(alpha);
567 }
568
Michael Jurkadee05892010-07-27 10:01:56 -0700569 private void setChildrenAlpha(float alpha) {
Michael Jurka0142d492010-08-25 17:46:15 -0700570 final int childCount = getChildCount();
571 for (int i = 0; i < childCount; i++) {
Michael Jurkadee05892010-07-27 10:01:56 -0700572 getChildAt(i).setAlpha(alpha);
573 }
574 }
575
Michael Jurka0280c3b2010-09-17 15:00:07 -0700576 private boolean isVacantIgnoring(
577 int originX, int originY, int spanX, int spanY, View ignoreView) {
578 if (ignoreView != null) {
579 markCellsAsUnoccupiedForView(ignoreView);
580 }
Michael Jurka28750fb2010-09-24 17:43:49 -0700581 boolean isVacant = true;
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700582 for (int i = 0; i < spanY; i++) {
583 if (!isRowEmpty(originY + i, originX, originX + spanX - 1, mOccupied)) {
Michael Jurka28750fb2010-09-24 17:43:49 -0700584 isVacant = false;
585 break;
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700586 }
587 }
Michael Jurka0280c3b2010-09-17 15:00:07 -0700588 if (ignoreView != null) {
589 markCellsAsOccupiedForView(ignoreView);
590 }
Michael Jurka28750fb2010-09-24 17:43:49 -0700591 return isVacant;
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700592 }
593
Michael Jurka0280c3b2010-09-17 15:00:07 -0700594 private boolean isVacant(int originX, int originY, int spanX, int spanY) {
595 return isVacantIgnoring(originX, originY, spanX, spanY, null);
596 }
597
Patrick Dubroy440c3602010-07-13 17:50:32 -0700598 public View getChildAt(int x, int y) {
599 final int count = getChildCount();
600 for (int i = 0; i < count; i++) {
601 View child = getChildAt(i);
602 LayoutParams lp = (LayoutParams) child.getLayoutParams();
603
604 if ((lp.cellX <= x) && (x < lp.cellX + lp.cellHSpan) &&
605 (lp.cellY <= y) && (y < lp.cellY + lp.cellHSpan)) {
606 return child;
607 }
608 }
609 return null;
610 }
611
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700612 /**
Patrick Dubroy8f86ddc2010-07-16 13:55:32 -0700613 * Estimate the size that a child with the given dimensions will take in the layout.
614 */
615 void estimateChildSize(int minWidth, int minHeight, int[] result) {
616 // Assuming it's placed at 0, 0, find where the bottom right cell will land
617 rectToCell(minWidth, minHeight, result);
618
619 // Then figure out the rect it will occupy
620 cellToRect(0, 0, result[0], result[1], mRectF);
621 result[0] = (int)mRectF.width();
622 result[1] = (int)mRectF.height();
623 }
624
625 /**
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700626 * Estimate where the top left cell of the dragged item will land if it is dropped.
627 *
628 * @param originX The X value of the top left corner of the item
629 * @param originY The Y value of the top left corner of the item
630 * @param spanX The number of horizontal cells that the item spans
631 * @param spanY The number of vertical cells that the item spans
632 * @param result The estimated drop cell X and Y.
633 */
634 void estimateDropCell(int originX, int originY, int spanX, int spanY, int[] result) {
Adam Cohend22015c2010-07-26 22:02:18 -0700635 final int countX = mCountX;
636 final int countY = mCountY;
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700637
Michael Jurkaa63c4522010-08-19 13:52:27 -0700638 // pointToCellRounded takes the top left of a cell but will pad that with
639 // cellWidth/2 and cellHeight/2 when finding the matching cell
640 pointToCellRounded(originX, originY, result);
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700641
642 // If the item isn't fully on this screen, snap to the edges
643 int rightOverhang = result[0] + spanX - countX;
644 if (rightOverhang > 0) {
645 result[0] -= rightOverhang; // Snap to right
646 }
647 result[0] = Math.max(0, result[0]); // Snap to left
648 int bottomOverhang = result[1] + spanY - countY;
649 if (bottomOverhang > 0) {
650 result[1] -= bottomOverhang; // Snap to bottom
651 }
652 result[1] = Math.max(0, result[1]); // Snap to top
653 }
654
Michael Jurka0280c3b2010-09-17 15:00:07 -0700655 void visualizeDropLocation(
656 View view, int originX, int originY, int spanX, int spanY, View draggedItem) {
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700657 final int[] originCell = mDragCell;
658 final int[] cellXY = mTmpCellXY;
659 estimateDropCell(originX, originY, spanX, spanY, cellXY);
660
661 // Only recalculate the bounding rect when necessary
662 if (!Arrays.equals(cellXY, originCell)) {
663 originCell[0] = cellXY[0];
664 originCell[1] = cellXY[1];
665
666 // Find the top left corner of the rect the object will occupy
667 final int[] topLeft = mTmpCellXY;
668 cellToPoint(originCell[0], originCell[1], topLeft);
669 final int left = topLeft[0];
670 final int top = topLeft[1];
671
672 // Now find the bottom right
673 final int[] bottomRight = mTmpCellXY;
674 cellToPoint(originCell[0] + spanX - 1, originCell[1] + spanY - 1, bottomRight);
675 bottomRight[0] += mCellWidth;
676 bottomRight[1] += mCellHeight;
677
Michael Jurka0280c3b2010-09-17 15:00:07 -0700678 boolean vacant =
679 isVacantIgnoring(originCell[0], originCell[1], spanX, spanY, draggedItem);
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700680 mDragRectDrawable = vacant ? mVacantDrawable : mOccupiedDrawable;
681
682 // mDragRect will be rendered in onDraw()
683 mDragRect.set(left, top, bottomRight[0], bottomRight[1]);
684 invalidate();
685 }
686 }
687
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800688 /**
Jeff Sharkey70864282009-04-07 21:08:40 -0700689 * Find a vacant area that will fit the given bounds nearest the requested
690 * cell location. Uses Euclidean distance to score multiple vacant areas.
Winson Chungaafa03c2010-06-11 17:34:16 -0700691 *
Romain Guy51afc022009-05-04 18:03:43 -0700692 * @param pixelX The X location at which you want to search for a vacant area.
693 * @param pixelY The Y location at which you want to search for a vacant area.
Jeff Sharkey70864282009-04-07 21:08:40 -0700694 * @param spanX Horizontal span of the object.
695 * @param spanY Vertical span of the object.
696 * @param vacantCells Pre-computed set of vacant cells to search.
697 * @param recycle Previously returned value to possibly recycle.
698 * @return The X, Y cell of a vacant area that can contain this object,
699 * nearest the requested location.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800700 */
Michael Jurka0280c3b2010-09-17 15:00:07 -0700701 int[] findNearestVacantArea(int pixelX, int pixelY, int spanX, int spanY, int[] recycle) {
Winson Chungaafa03c2010-06-11 17:34:16 -0700702
Jeff Sharkey70864282009-04-07 21:08:40 -0700703 // Keep track of best-scoring drop area
704 final int[] bestXY = recycle != null ? recycle : new int[2];
Jeff Sharkey70864282009-04-07 21:08:40 -0700705 double bestDistance = Double.MAX_VALUE;
Winson Chungaafa03c2010-06-11 17:34:16 -0700706
Michael Jurkac28de512010-08-13 11:27:44 -0700707 for (int x = 0; x < mCountX - (spanX - 1); x++) {
708 inner:
709 for (int y = 0; y < mCountY - (spanY - 1); y++) {
710 for (int i = 0; i < spanX; i++) {
711 for (int j = 0; j < spanY; j++) {
712 if (mOccupied[x + i][y + j]) {
713 // small optimization: we can skip to below the row we just found
714 // an occupied cell
715 y += j;
716 continue inner;
717 }
718 }
719 }
720 final int[] cellXY = mTmpCellXY;
721 cellToPoint(x, y, cellXY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800722
Michael Jurkac28de512010-08-13 11:27:44 -0700723 double distance = Math.sqrt(Math.pow(cellXY[0] - pixelX, 2)
724 + Math.pow(cellXY[1] - pixelY, 2));
725 if (distance <= bestDistance) {
726 bestDistance = distance;
727 bestXY[0] = x;
728 bestXY[1] = y;
729 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800730 }
731 }
732
Winson Chungaafa03c2010-06-11 17:34:16 -0700733 // Return null if no suitable location found
Jeff Sharkey70864282009-04-07 21:08:40 -0700734 if (bestDistance < Double.MAX_VALUE) {
735 return bestXY;
736 } else {
737 return null;
738 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800739 }
Winson Chungaafa03c2010-06-11 17:34:16 -0700740
Michael Jurka0280c3b2010-09-17 15:00:07 -0700741 boolean existsEmptyCell() {
742 return findCellForSpan(null, 1, 1);
743 }
744
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800745 /**
Michael Jurka0280c3b2010-09-17 15:00:07 -0700746 * Finds the upper-left coordinate of the first rectangle in the grid that can
747 * hold a cell of the specified dimensions. If intersectX and intersectY are not -1,
748 * then this method will only return coordinates for rectangles that contain the cell
749 * (intersectX, intersectY)
750 *
751 * @param cellXY The array that will contain the position of a vacant cell if such a cell
752 * can be found.
753 * @param spanX The horizontal span of the cell we want to find.
754 * @param spanY The vertical span of the cell we want to find.
755 *
756 * @return True if a vacant cell of the specified dimension was found, false otherwise.
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700757 */
Michael Jurka0280c3b2010-09-17 15:00:07 -0700758 boolean findCellForSpan(int[] cellXY, int spanX, int spanY) {
759 return findCellForSpanThatIntersectsIgnoring(cellXY, spanX, spanY, -1, -1, null);
760 }
761
762 /**
763 * Like above, but ignores any cells occupied by the item "ignoreView"
764 *
765 * @param cellXY The array that will contain the position of a vacant cell if such a cell
766 * can be found.
767 * @param spanX The horizontal span of the cell we want to find.
768 * @param spanY The vertical span of the cell we want to find.
769 * @param ignoreView The home screen item we should treat as not occupying any space
770 * @return
771 */
772 boolean findCellForSpanIgnoring(int[] cellXY, int spanX, int spanY, View ignoreView) {
773 return findCellForSpanThatIntersectsIgnoring(cellXY, spanX, spanY, -1, -1, ignoreView);
774 }
775
776 /**
777 * Like above, but if intersectX and intersectY are not -1, then this method will try to
778 * return coordinates for rectangles that contain the cell [intersectX, intersectY]
779 *
780 * @param spanX The horizontal span of the cell we want to find.
781 * @param spanY The vertical span of the cell we want to find.
782 * @param ignoreView The home screen item we should treat as not occupying any space
783 * @param intersectX The X coordinate of the cell that we should try to overlap
784 * @param intersectX The Y coordinate of the cell that we should try to overlap
785 *
786 * @return True if a vacant cell of the specified dimension was found, false otherwise.
787 */
788 boolean findCellForSpanThatIntersects(int[] cellXY, int spanX, int spanY,
789 int intersectX, int intersectY) {
790 return findCellForSpanThatIntersectsIgnoring(
791 cellXY, spanX, spanY, intersectX, intersectY, null);
792 }
793
794 /**
795 * The superset of the above two methods
796 */
797 boolean findCellForSpanThatIntersectsIgnoring(int[] cellXY, int spanX, int spanY,
798 int intersectX, int intersectY, View ignoreView) {
799 if (ignoreView != null) {
800 markCellsAsUnoccupiedForView(ignoreView);
801 }
802
Michael Jurka28750fb2010-09-24 17:43:49 -0700803 boolean foundCell = false;
Michael Jurka0280c3b2010-09-17 15:00:07 -0700804 while (true) {
805 int startX = 0;
806 if (intersectX >= 0) {
807 startX = Math.max(startX, intersectX - (spanX - 1));
808 }
809 int endX = mCountX - (spanX - 1);
810 if (intersectX >= 0) {
811 endX = Math.min(endX, intersectX + (spanX - 1) + (spanX == 1 ? 1 : 0));
812 }
813 int startY = 0;
814 if (intersectY >= 0) {
815 startY = Math.max(startY, intersectY - (spanY - 1));
816 }
817 int endY = mCountY - (spanY - 1);
818 if (intersectY >= 0) {
819 endY = Math.min(endY, intersectY + (spanY - 1) + (spanY == 1 ? 1 : 0));
820 }
821
822 for (int x = startX; x < endX; x++) {
823 inner:
824 for (int y = startY; y < endY; y++) {
825 for (int i = 0; i < spanX; i++) {
826 for (int j = 0; j < spanY; j++) {
827 if (mOccupied[x + i][y + j]) {
828 // small optimization: we can skip to below the row we just found
829 // an occupied cell
830 y += j;
831 continue inner;
832 }
833 }
834 }
835 if (cellXY != null) {
836 cellXY[0] = x;
837 cellXY[1] = y;
838 }
Michael Jurka28750fb2010-09-24 17:43:49 -0700839 foundCell = true;
840 break;
Michael Jurka0280c3b2010-09-17 15:00:07 -0700841 }
842 }
843 if (intersectX == -1 && intersectY == -1) {
844 break;
845 } else {
846 // if we failed to find anything, try again but without any requirements of
847 // intersecting
848 intersectX = -1;
849 intersectY = -1;
850 continue;
851 }
852 }
853
854 if (ignoreView != null) {
855 markCellsAsOccupiedForView(ignoreView);
856 }
Michael Jurka28750fb2010-09-24 17:43:49 -0700857 return foundCell;
Michael Jurka0280c3b2010-09-17 15:00:07 -0700858 }
859
860 /**
861 * Called when drag has left this CellLayout or has been completed (successfully or not)
862 */
863 void onDragExit() {
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700864 // Invalidate the drag data
865 mDragCell[0] = -1;
866 mDragCell[1] = -1;
867
Michael Jurkaa63c4522010-08-19 13:52:27 -0700868 setHover(false);
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700869 mDragRect.setEmpty();
870 invalidate();
871 }
872
873 /**
Winson Chungaafa03c2010-06-11 17:34:16 -0700874 * Mark a child as having been dropped.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800875 *
876 * @param child The child that is being dropped
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800877 */
Winson Chungaafa03c2010-06-11 17:34:16 -0700878 void onDropChild(View child) {
Romain Guyd94533d2009-08-17 10:01:15 -0700879 if (child != null) {
880 LayoutParams lp = (LayoutParams) child.getLayoutParams();
Romain Guyd94533d2009-08-17 10:01:15 -0700881 lp.isDragging = false;
Romain Guy84f296c2009-11-04 15:00:44 -0800882 lp.dropped = true;
Romain Guyd94533d2009-08-17 10:01:15 -0700883 mDragRect.setEmpty();
884 child.requestLayout();
Romain Guyd94533d2009-08-17 10:01:15 -0700885 }
Michael Jurka0280c3b2010-09-17 15:00:07 -0700886 onDragExit();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800887 }
888
889 void onDropAborted(View child) {
890 if (child != null) {
891 ((LayoutParams) child.getLayoutParams()).isDragging = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800892 }
Michael Jurka0280c3b2010-09-17 15:00:07 -0700893 onDragExit();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800894 }
895
896 /**
897 * Start dragging the specified child
Winson Chungaafa03c2010-06-11 17:34:16 -0700898 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800899 * @param child The child that is being dragged
900 */
901 void onDragChild(View child) {
902 LayoutParams lp = (LayoutParams) child.getLayoutParams();
903 lp.isDragging = true;
904 mDragRect.setEmpty();
905 }
Winson Chungaafa03c2010-06-11 17:34:16 -0700906
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800907 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800908 * Computes a bounding rectangle for a range of cells
Winson Chungaafa03c2010-06-11 17:34:16 -0700909 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800910 * @param cellX X coordinate of upper left corner expressed as a cell position
911 * @param cellY Y coordinate of upper left corner expressed as a cell position
Winson Chungaafa03c2010-06-11 17:34:16 -0700912 * @param cellHSpan Width in cells
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800913 * @param cellVSpan Height in cells
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700914 * @param resultRect Rect into which to put the results
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800915 */
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700916 public void cellToRect(int cellX, int cellY, int cellHSpan, int cellVSpan, RectF resultRect) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800917 final int cellWidth = mCellWidth;
918 final int cellHeight = mCellHeight;
919 final int widthGap = mWidthGap;
920 final int heightGap = mHeightGap;
Winson Chungaafa03c2010-06-11 17:34:16 -0700921
922 final int hStartPadding = getLeftPadding();
923 final int vStartPadding = getTopPadding();
924
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800925 int width = cellHSpan * cellWidth + ((cellHSpan - 1) * widthGap);
926 int height = cellVSpan * cellHeight + ((cellVSpan - 1) * heightGap);
927
928 int x = hStartPadding + cellX * (cellWidth + widthGap);
929 int y = vStartPadding + cellY * (cellHeight + heightGap);
Winson Chungaafa03c2010-06-11 17:34:16 -0700930
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700931 resultRect.set(x, y, x + width, y + height);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800932 }
Winson Chungaafa03c2010-06-11 17:34:16 -0700933
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800934 /**
Winson Chungaafa03c2010-06-11 17:34:16 -0700935 * Computes the required horizontal and vertical cell spans to always
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800936 * fit the given rectangle.
Winson Chungaafa03c2010-06-11 17:34:16 -0700937 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800938 * @param width Width in pixels
939 * @param height Height in pixels
Patrick Dubroy8f86ddc2010-07-16 13:55:32 -0700940 * @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 -0800941 */
Patrick Dubroy8f86ddc2010-07-16 13:55:32 -0700942 public int[] rectToCell(int width, int height, int[] result) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800943 // Always assume we're working with the smallest span to make sure we
944 // reserve enough space in both orientations.
Joe Onorato79e56262009-09-21 15:23:04 -0400945 final Resources resources = getResources();
946 int actualWidth = resources.getDimensionPixelSize(R.dimen.workspace_cell_width);
947 int actualHeight = resources.getDimensionPixelSize(R.dimen.workspace_cell_height);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800948 int smallerSize = Math.min(actualWidth, actualHeight);
Joe Onorato79e56262009-09-21 15:23:04 -0400949
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800950 // Always round up to next largest cell
951 int spanX = (width + smallerSize) / smallerSize;
952 int spanY = (height + smallerSize) / smallerSize;
Joe Onorato79e56262009-09-21 15:23:04 -0400953
Patrick Dubroy8f86ddc2010-07-16 13:55:32 -0700954 if (result == null) {
955 return new int[] { spanX, spanY };
956 }
957 result[0] = spanX;
958 result[1] = spanY;
959 return result;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800960 }
961
962 /**
963 * Find the first vacant cell, if there is one.
964 *
965 * @param vacant Holds the x and y coordinate of the vacant cell
966 * @param spanX Horizontal cell span.
967 * @param spanY Vertical cell span.
Winson Chungaafa03c2010-06-11 17:34:16 -0700968 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800969 * @return True if a vacant cell was found
970 */
971 public boolean getVacantCell(int[] vacant, int spanX, int spanY) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800972
Michael Jurka0280c3b2010-09-17 15:00:07 -0700973 return findVacantCell(vacant, spanX, spanY, mCountX, mCountY, mOccupied);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800974 }
975
976 static boolean findVacantCell(int[] vacant, int spanX, int spanY,
977 int xCount, int yCount, boolean[][] occupied) {
978
979 for (int x = 0; x < xCount; x++) {
980 for (int y = 0; y < yCount; y++) {
981 boolean available = !occupied[x][y];
982out: for (int i = x; i < x + spanX - 1 && x < xCount; i++) {
983 for (int j = y; j < y + spanY - 1 && y < yCount; j++) {
984 available = available && !occupied[i][j];
985 if (!available) break out;
986 }
987 }
988
989 if (available) {
990 vacant[0] = x;
991 vacant[1] = y;
992 return true;
993 }
994 }
995 }
996
997 return false;
998 }
999
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001000 /**
1001 * Update the array of occupied cells (mOccupied), and return a flattened copy of the array.
1002 */
1003 boolean[] getOccupiedCellsFlattened() {
Adam Cohend22015c2010-07-26 22:02:18 -07001004 final int xCount = mCountX;
1005 final int yCount = mCountY;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001006 final boolean[][] occupied = mOccupied;
1007
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001008 final boolean[] flat = new boolean[xCount * yCount];
1009 for (int y = 0; y < yCount; y++) {
1010 for (int x = 0; x < xCount; x++) {
1011 flat[y * xCount + x] = occupied[x][y];
1012 }
1013 }
1014
1015 return flat;
1016 }
1017
Michael Jurka0280c3b2010-09-17 15:00:07 -07001018 private void clearOccupiedCells() {
1019 for (int x = 0; x < mCountX; x++) {
1020 for (int y = 0; y < mCountY; y++) {
1021 mOccupied[x][y] = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001022 }
1023 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001024 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001025
Michael Jurka0280c3b2010-09-17 15:00:07 -07001026 public void onMove(View view, int newCellX, int newCellY) {
1027 LayoutParams lp = (LayoutParams) view.getLayoutParams();
1028 markCellsAsUnoccupiedForView(view);
1029 markCellsForView(newCellX, newCellY, lp.cellHSpan, lp.cellVSpan, true);
1030 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001031
Michael Jurka0280c3b2010-09-17 15:00:07 -07001032 private void markCellsAsOccupiedForView(View view) {
1033 LayoutParams lp = (LayoutParams) view.getLayoutParams();
1034 markCellsForView(lp.cellX, lp.cellY, lp.cellHSpan, lp.cellVSpan, true);
1035 }
1036
1037 private void markCellsAsUnoccupiedForView(View view) {
1038 LayoutParams lp = (LayoutParams) view.getLayoutParams();
1039 markCellsForView(lp.cellX, lp.cellY, lp.cellHSpan, lp.cellVSpan, false);
1040 }
1041
1042 private void markCellsForView(int cellX, int cellY, int spanX, int spanY, boolean value) {
1043 for (int x = cellX; x < cellX + spanX && x < mCountX; x++) {
1044 for (int y = cellY; y < cellY + spanY && y < mCountY; y++) {
1045 mOccupied[x][y] = value;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001046 }
1047 }
1048 }
1049
1050 @Override
1051 public ViewGroup.LayoutParams generateLayoutParams(AttributeSet attrs) {
1052 return new CellLayout.LayoutParams(getContext(), attrs);
1053 }
1054
1055 @Override
1056 protected boolean checkLayoutParams(ViewGroup.LayoutParams p) {
1057 return p instanceof CellLayout.LayoutParams;
1058 }
1059
1060 @Override
1061 protected ViewGroup.LayoutParams generateLayoutParams(ViewGroup.LayoutParams p) {
1062 return new CellLayout.LayoutParams(p);
1063 }
1064
Winson Chungaafa03c2010-06-11 17:34:16 -07001065 public static class CellLayoutAnimationController extends LayoutAnimationController {
1066 public CellLayoutAnimationController(Animation animation, float delay) {
1067 super(animation, delay);
1068 }
1069
1070 @Override
1071 protected long getDelayForView(View view) {
1072 return (int) (Math.random() * 150);
1073 }
1074 }
1075
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001076 public static class LayoutParams extends ViewGroup.MarginLayoutParams {
1077 /**
1078 * Horizontal location of the item in the grid.
1079 */
1080 @ViewDebug.ExportedProperty
1081 public int cellX;
1082
1083 /**
1084 * Vertical location of the item in the grid.
1085 */
1086 @ViewDebug.ExportedProperty
1087 public int cellY;
1088
1089 /**
1090 * Number of cells spanned horizontally by the item.
1091 */
1092 @ViewDebug.ExportedProperty
1093 public int cellHSpan;
1094
1095 /**
1096 * Number of cells spanned vertically by the item.
1097 */
1098 @ViewDebug.ExportedProperty
1099 public int cellVSpan;
Winson Chungaafa03c2010-06-11 17:34:16 -07001100
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001101 /**
1102 * Is this item currently being dragged
1103 */
1104 public boolean isDragging;
1105
1106 // X coordinate of the view in the layout.
1107 @ViewDebug.ExportedProperty
1108 int x;
1109 // Y coordinate of the view in the layout.
1110 @ViewDebug.ExportedProperty
1111 int y;
1112
Romain Guy84f296c2009-11-04 15:00:44 -08001113 boolean dropped;
Romain Guyfcb9e712009-10-02 16:06:52 -07001114
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001115 public LayoutParams(Context c, AttributeSet attrs) {
1116 super(c, attrs);
1117 cellHSpan = 1;
1118 cellVSpan = 1;
1119 }
1120
1121 public LayoutParams(ViewGroup.LayoutParams source) {
1122 super(source);
1123 cellHSpan = 1;
1124 cellVSpan = 1;
1125 }
Winson Chungaafa03c2010-06-11 17:34:16 -07001126
1127 public LayoutParams(LayoutParams source) {
1128 super(source);
1129 this.cellX = source.cellX;
1130 this.cellY = source.cellY;
1131 this.cellHSpan = source.cellHSpan;
1132 this.cellVSpan = source.cellVSpan;
1133 }
1134
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001135 public LayoutParams(int cellX, int cellY, int cellHSpan, int cellVSpan) {
Romain Guy8f19cdd2010-01-08 15:07:00 -08001136 super(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001137 this.cellX = cellX;
1138 this.cellY = cellY;
1139 this.cellHSpan = cellHSpan;
1140 this.cellVSpan = cellVSpan;
1141 }
1142
1143 public void setup(int cellWidth, int cellHeight, int widthGap, int heightGap,
1144 int hStartPadding, int vStartPadding) {
Winson Chungaafa03c2010-06-11 17:34:16 -07001145
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001146 final int myCellHSpan = cellHSpan;
1147 final int myCellVSpan = cellVSpan;
1148 final int myCellX = cellX;
1149 final int myCellY = cellY;
Winson Chungaafa03c2010-06-11 17:34:16 -07001150
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001151 width = myCellHSpan * cellWidth + ((myCellHSpan - 1) * widthGap) -
1152 leftMargin - rightMargin;
1153 height = myCellVSpan * cellHeight + ((myCellVSpan - 1) * heightGap) -
1154 topMargin - bottomMargin;
1155
1156 x = hStartPadding + myCellX * (cellWidth + widthGap) + leftMargin;
1157 y = vStartPadding + myCellY * (cellHeight + heightGap) + topMargin;
1158 }
Winson Chungaafa03c2010-06-11 17:34:16 -07001159
1160 public String toString() {
1161 return "(" + this.cellX + ", " + this.cellY + ")";
1162 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001163 }
1164
Michael Jurka0280c3b2010-09-17 15:00:07 -07001165 // This class stores info for two purposes:
1166 // 1. When dragging items (mDragInfo in Workspace), we store the View, its cellX & cellY,
1167 // its spanX, spanY, and the screen it is on
1168 // 2. When long clicking on an empty cell in a CellLayout, we save information about the
1169 // cellX and cellY coordinates and which page was clicked. We then set this as a tag on
1170 // the CellLayout that was long clicked
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001171 static final class CellInfo implements ContextMenu.ContextMenuInfo {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001172 View cell;
Michael Jurkaa63c4522010-08-19 13:52:27 -07001173 int cellX = -1;
1174 int cellY = -1;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001175 int spanX;
1176 int spanY;
1177 int screen;
1178 boolean valid;
1179
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001180 @Override
1181 public String toString() {
Winson Chungaafa03c2010-06-11 17:34:16 -07001182 return "Cell[view=" + (cell == null ? "null" : cell.getClass())
1183 + ", x=" + cellX + ", y=" + cellY + "]";
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001184 }
1185 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001186}