blob: 4d1c29961ba9c81d76c7a5a1e70e9675a81a650c [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 boolean mPortrait;
44
45 private int mCellWidth;
46 private int mCellHeight;
Winson Chungaafa03c2010-06-11 17:34:16 -070047
Winson Chungaafa03c2010-06-11 17:34:16 -070048 private int mLeftPadding;
49 private int mRightPadding;
50 private int mTopPadding;
51 private int mBottomPadding;
52
Adam Cohend22015c2010-07-26 22:02:18 -070053 private int mCountX;
54 private int mCountY;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080055
56 private int mWidthGap;
57 private int mHeightGap;
58
59 private final Rect mRect = new Rect();
Patrick Dubroy8f86ddc2010-07-16 13:55:32 -070060 private final RectF mRectF = new RectF();
The Android Open Source Project31dd5032009-03-03 19:32:27 -080061 private final CellInfo mCellInfo = new CellInfo();
Winson Chungaafa03c2010-06-11 17:34:16 -070062
Patrick Dubroy6569f2c2010-07-12 14:25:18 -070063 // This is a temporary variable to prevent having to allocate a new object just to
64 // return an (x, y) value from helper functions. Do NOT use it to maintain other state.
65 private final int[] mTmpCellXY = new int[2];
66
The Android Open Source Project31dd5032009-03-03 19:32:27 -080067 boolean[][] mOccupied;
68
Michael Jurkadee05892010-07-27 10:01:56 -070069 private OnTouchListener mInterceptTouchListener;
70
Michael Jurka5f1c5092010-09-03 14:15:02 -070071 private float mBackgroundAlpha;
72 private final Rect mBackgroundLayoutRect = new Rect();
73 private Drawable mBackground;
74 private Drawable mBackgroundHover;
Michael Jurkaa63c4522010-08-19 13:52:27 -070075 // If we're actively dragging something over this screen and it's small,
76 // mHover is true
77 private boolean mHover = false;
Michael Jurkadee05892010-07-27 10:01:56 -070078
Patrick Dubroy6569f2c2010-07-12 14:25:18 -070079 private final RectF mDragRect = new RectF();
80
81 // When dragging, used to indicate a vacant drop location
82 private Drawable mVacantDrawable;
83
84 // When dragging, used to indicate an occupied drop location
85 private Drawable mOccupiedDrawable;
86
87 // Updated to point to mVacantDrawable or mOccupiedDrawable, as appropriate
88 private Drawable mDragRectDrawable;
89
90 // When a drag operation is in progress, holds the nearest cell to the touch point
91 private final int[] mDragCell = new int[2];
The Android Open Source Project31dd5032009-03-03 19:32:27 -080092
93 private boolean mDirtyTag;
Mike Cleronf8bbd342009-10-23 16:15:16 -070094 private boolean mLastDownOnOccupiedCell = false;
Winson Chungaafa03c2010-06-11 17:34:16 -070095
96 private final WallpaperManager mWallpaperManager;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080097
98 public CellLayout(Context context) {
99 this(context, null);
100 }
101
102 public CellLayout(Context context, AttributeSet attrs) {
103 this(context, attrs, 0);
104 }
105
106 public CellLayout(Context context, AttributeSet attrs, int defStyle) {
107 super(context, attrs, defStyle);
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700108
109 // A ViewGroup usually does not draw, but CellLayout needs to draw a rectangle to show
110 // the user where a dragged item will land when dropped.
111 setWillNotDraw(false);
112 mVacantDrawable = getResources().getDrawable(R.drawable.rounded_rect_green);
113 mOccupiedDrawable = getResources().getDrawable(R.drawable.rounded_rect_red);
114
Michael Jurkaa63c4522010-08-19 13:52:27 -0700115 if (LauncherApplication.isScreenXLarge()) {
Michael Jurka5f1c5092010-09-03 14:15:02 -0700116 mBackground = getResources().getDrawable(R.drawable.mini_home_screen_bg);
117 mBackground.setFilterBitmap(true);
118 mBackgroundHover = getResources().getDrawable(R.drawable.mini_home_screen_bg_hover);
119 mBackgroundHover.setFilterBitmap(true);
Michael Jurkaa63c4522010-08-19 13:52:27 -0700120 }
121
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800122 TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.CellLayout, defStyle, 0);
123
124 mCellWidth = a.getDimensionPixelSize(R.styleable.CellLayout_cellWidth, 10);
125 mCellHeight = a.getDimensionPixelSize(R.styleable.CellLayout_cellHeight, 10);
Winson Chungaafa03c2010-06-11 17:34:16 -0700126
Adam Cohend22015c2010-07-26 22:02:18 -0700127 mLeftPadding =
128 a.getDimensionPixelSize(R.styleable.CellLayout_xAxisStartPadding, 10);
129 mRightPadding =
130 a.getDimensionPixelSize(R.styleable.CellLayout_xAxisEndPadding, 10);
131 mTopPadding =
132 a.getDimensionPixelSize(R.styleable.CellLayout_yAxisStartPadding, 10);
133 mBottomPadding =
134 a.getDimensionPixelSize(R.styleable.CellLayout_yAxisEndPadding, 10);
Winson Chungaafa03c2010-06-11 17:34:16 -0700135
Adam Cohend22015c2010-07-26 22:02:18 -0700136 mCountX = LauncherModel.getCellCountX();
137 mCountY = LauncherModel.getCellCountY();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800138
139 a.recycle();
140
141 setAlwaysDrawnWithCacheEnabled(false);
142
Romain Guy84f296c2009-11-04 15:00:44 -0800143 mWallpaperManager = WallpaperManager.getInstance(getContext());
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800144 }
145
Michael Jurkaa63c4522010-08-19 13:52:27 -0700146 public void setHover(boolean value) {
147 if (mHover != value) {
148 invalidate();
149 }
150 mHover = value;
151 }
152
Jeff Sharkey83f111d2009-04-20 21:03:13 -0700153 @Override
Romain Guya6abce82009-11-10 02:54:41 -0800154 public void dispatchDraw(Canvas canvas) {
Michael Jurka5f1c5092010-09-03 14:15:02 -0700155 if (mBackgroundAlpha > 0.0f) {
156 final Drawable bg = mHover ? mBackgroundHover : mBackground;
157 bg.setAlpha((int) (mBackgroundAlpha * 255));
Michael Jurkaa63c4522010-08-19 13:52:27 -0700158 bg.draw(canvas);
159 }
Romain Guya6abce82009-11-10 02:54:41 -0800160 super.dispatchDraw(canvas);
161 }
162
163 @Override
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700164 protected void onDraw(Canvas canvas) {
165 if (!mDragRect.isEmpty()) {
166 mDragRectDrawable.setBounds(
167 (int)mDragRect.left,
168 (int)mDragRect.top,
169 (int)mDragRect.right,
170 (int)mDragRect.bottom);
171 mDragRectDrawable.draw(canvas);
172 }
Michael Jurka5f1c5092010-09-03 14:15:02 -0700173 super.onDraw(canvas);
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700174 }
175
176 @Override
Jeff Sharkey83f111d2009-04-20 21:03:13 -0700177 public void cancelLongPress() {
178 super.cancelLongPress();
179
180 // Cancel long press for all children
181 final int count = getChildCount();
182 for (int i = 0; i < count; i++) {
183 final View child = getChildAt(i);
184 child.cancelLongPress();
185 }
186 }
187
Michael Jurkadee05892010-07-27 10:01:56 -0700188 public void setOnInterceptTouchListener(View.OnTouchListener listener) {
189 mInterceptTouchListener = listener;
190 }
191
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800192 int getCountX() {
Adam Cohend22015c2010-07-26 22:02:18 -0700193 return mCountX;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800194 }
195
196 int getCountY() {
Adam Cohend22015c2010-07-26 22:02:18 -0700197 return mCountY;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800198 }
199
Winson Chungaafa03c2010-06-11 17:34:16 -0700200 // Takes canonical layout parameters
201 public boolean addViewToCellLayout(View child, int index, int childId, LayoutParams params) {
202 final LayoutParams lp = params;
203
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800204 // Generate an id for each view, this assumes we have at most 256x256 cells
205 // per workspace screen
Adam Cohend22015c2010-07-26 22:02:18 -0700206 if (lp.cellX >= 0 && lp.cellX <= mCountX - 1 && lp.cellY >= 0 && lp.cellY <= mCountY - 1) {
Winson Chungaafa03c2010-06-11 17:34:16 -0700207 // If the horizontal or vertical span is set to -1, it is taken to
208 // mean that it spans the extent of the CellLayout
Adam Cohend22015c2010-07-26 22:02:18 -0700209 if (lp.cellHSpan < 0) lp.cellHSpan = mCountX;
210 if (lp.cellVSpan < 0) lp.cellVSpan = mCountY;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800211
Winson Chungaafa03c2010-06-11 17:34:16 -0700212 child.setId(childId);
213
Michael Jurkadee05892010-07-27 10:01:56 -0700214 // We might be in the middle or end of shrinking/fading to a dimmed view
215 // Make sure this view's alpha is set the same as all the rest of the views
Michael Jurka5f1c5092010-09-03 14:15:02 -0700216 child.setAlpha(getAlpha());
Michael Jurkadee05892010-07-27 10:01:56 -0700217
Winson Chungaafa03c2010-06-11 17:34:16 -0700218 addView(child, index, lp);
Michael Jurkadee05892010-07-27 10:01:56 -0700219
Winson Chungaafa03c2010-06-11 17:34:16 -0700220 return true;
221 }
222 return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800223 }
224
225 @Override
226 public void requestChildFocus(View child, View focused) {
227 super.requestChildFocus(child, focused);
228 if (child != null) {
229 Rect r = new Rect();
230 child.getDrawingRect(r);
231 requestRectangleOnScreen(r);
232 }
233 }
234
235 @Override
236 protected void onAttachedToWindow() {
237 super.onAttachedToWindow();
238 mCellInfo.screen = ((ViewGroup) getParent()).indexOfChild(this);
239 }
240
Michael Jurkaaf442092010-06-10 17:01:57 -0700241 public void setTagToCellInfoForPoint(int touchX, int touchY) {
242 final CellInfo cellInfo = mCellInfo;
243 final Rect frame = mRect;
244 final int x = touchX + mScrollX;
245 final int y = touchY + mScrollY;
246 final int count = getChildCount();
247
248 boolean found = false;
249 for (int i = count - 1; i >= 0; i--) {
250 final View child = getChildAt(i);
251
252 if ((child.getVisibility()) == VISIBLE || child.getAnimation() != null) {
253 child.getHitRect(frame);
254 if (frame.contains(x, y)) {
255 final LayoutParams lp = (LayoutParams) child.getLayoutParams();
256 cellInfo.cell = child;
257 cellInfo.cellX = lp.cellX;
258 cellInfo.cellY = lp.cellY;
259 cellInfo.spanX = lp.cellHSpan;
260 cellInfo.spanY = lp.cellVSpan;
Michael Jurkac28de512010-08-13 11:27:44 -0700261 cellInfo.intersectX = lp.cellX;
262 cellInfo.intersectY = lp.cellY;
Michael Jurkaaf442092010-06-10 17:01:57 -0700263 cellInfo.valid = true;
264 found = true;
265 mDirtyTag = false;
266 break;
267 }
268 }
269 }
Winson Chungaafa03c2010-06-11 17:34:16 -0700270
Michael Jurkaaf442092010-06-10 17:01:57 -0700271 mLastDownOnOccupiedCell = found;
272
273 if (!found) {
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700274 final int cellXY[] = mTmpCellXY;
Michael Jurkaaf442092010-06-10 17:01:57 -0700275 pointToCellExact(x, y, cellXY);
276
277 final boolean portrait = mPortrait;
Adam Cohend22015c2010-07-26 22:02:18 -0700278 final int xCount = mCountX;
279 final int yCount = mCountY;
Michael Jurkaaf442092010-06-10 17:01:57 -0700280
281 final boolean[][] occupied = mOccupied;
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700282 findOccupiedCells(xCount, yCount, occupied, null, true);
Michael Jurkaaf442092010-06-10 17:01:57 -0700283
284 cellInfo.cell = null;
285 cellInfo.cellX = cellXY[0];
286 cellInfo.cellY = cellXY[1];
287 cellInfo.spanX = 1;
288 cellInfo.spanY = 1;
Michael Jurkac28de512010-08-13 11:27:44 -0700289 cellInfo.intersectX = cellXY[0];
290 cellInfo.intersectY = cellXY[1];
Michael Jurkaaf442092010-06-10 17:01:57 -0700291 cellInfo.valid = cellXY[0] >= 0 && cellXY[1] >= 0 && cellXY[0] < xCount &&
292 cellXY[1] < yCount && !occupied[cellXY[0]][cellXY[1]];
293
294 // Instead of finding the interesting vacant cells here, wait until a
295 // caller invokes getTag() to retrieve the result. Finding the vacant
296 // cells is a bit expensive and can generate many new objects, it's
297 // therefore better to defer it until we know we actually need it.
298
299 mDirtyTag = true;
300 }
301 setTag(cellInfo);
302 }
303
Winson Chungaafa03c2010-06-11 17:34:16 -0700304
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800305 @Override
306 public boolean onInterceptTouchEvent(MotionEvent ev) {
Michael Jurkadee05892010-07-27 10:01:56 -0700307 if (mInterceptTouchListener != null && mInterceptTouchListener.onTouch(this, ev)) {
308 return true;
309 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800310 final int action = ev.getAction();
311 final CellInfo cellInfo = mCellInfo;
312
313 if (action == MotionEvent.ACTION_DOWN) {
Michael Jurkaaf442092010-06-10 17:01:57 -0700314 setTagToCellInfoForPoint((int) ev.getX(), (int) ev.getY());
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800315 } else if (action == MotionEvent.ACTION_UP) {
316 cellInfo.cell = null;
317 cellInfo.cellX = -1;
318 cellInfo.cellY = -1;
319 cellInfo.spanX = 0;
320 cellInfo.spanY = 0;
321 cellInfo.valid = false;
322 mDirtyTag = false;
323 setTag(cellInfo);
324 }
325
326 return false;
327 }
328
329 @Override
330 public CellInfo getTag() {
331 final CellInfo info = (CellInfo) super.getTag();
332 if (mDirtyTag && info.valid) {
Adam Cohend22015c2010-07-26 22:02:18 -0700333 final int xCount = mCountX;
334 final int yCount = mCountY;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800335
336 final boolean[][] occupied = mOccupied;
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700337 findOccupiedCells(xCount, yCount, occupied, null, true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800338
Michael Jurkac28de512010-08-13 11:27:44 -0700339 info.updateOccupiedCells(occupied, mCountX, mCountY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800340
341 mDirtyTag = false;
342 }
343 return info;
344 }
345
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700346 /**
347 * Check if the column 'x' is empty from rows 'top' to 'bottom', inclusive.
348 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800349 private static boolean isColumnEmpty(int x, int top, int bottom, boolean[][] occupied) {
350 for (int y = top; y <= bottom; y++) {
351 if (occupied[x][y]) {
352 return false;
353 }
354 }
355 return true;
356 }
357
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700358 /**
359 * Check if the row 'y' is empty from columns 'left' to 'right', inclusive.
360 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800361 private static boolean isRowEmpty(int y, int left, int right, boolean[][] occupied) {
362 for (int x = left; x <= right; x++) {
363 if (occupied[x][y]) {
364 return false;
365 }
366 }
367 return true;
368 }
369
Michael Jurkac28de512010-08-13 11:27:44 -0700370 CellInfo updateOccupiedCells(boolean[] occupiedCells, View ignoreView) {
Adam Cohend22015c2010-07-26 22:02:18 -0700371 final int xCount = mCountX;
372 final int yCount = mCountY;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800373
374 boolean[][] occupied = mOccupied;
375
376 if (occupiedCells != null) {
377 for (int y = 0; y < yCount; y++) {
378 for (int x = 0; x < xCount; x++) {
379 occupied[x][y] = occupiedCells[y * xCount + x];
380 }
381 }
382 } else {
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700383 findOccupiedCells(xCount, yCount, occupied, ignoreView, true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800384 }
385
386 CellInfo cellInfo = new CellInfo();
387
388 cellInfo.cellX = -1;
389 cellInfo.cellY = -1;
Michael Jurkac28de512010-08-13 11:27:44 -0700390 cellInfo.intersectX = -1;
391 cellInfo.intersectY = -1;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800392 cellInfo.spanY = 0;
393 cellInfo.spanX = 0;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800394 cellInfo.screen = mCellInfo.screen;
395
Michael Jurkac28de512010-08-13 11:27:44 -0700396 cellInfo.updateOccupiedCells(occupied, mCountX, mCountY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800397
Michael Jurkac28de512010-08-13 11:27:44 -0700398 cellInfo.valid = cellInfo.existsEmptyCell();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800399
400 // Assume the caller will perform their own cell searching, otherwise we
401 // risk causing an unnecessary rebuild after findCellForSpan()
Winson Chungaafa03c2010-06-11 17:34:16 -0700402
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800403 return cellInfo;
404 }
405
406 /**
Winson Chungaafa03c2010-06-11 17:34:16 -0700407 * Given a point, return the cell that strictly encloses that point
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800408 * @param x X coordinate of the point
409 * @param y Y coordinate of the point
410 * @param result Array of 2 ints to hold the x and y coordinate of the cell
411 */
412 void pointToCellExact(int x, int y, int[] result) {
Winson Chungaafa03c2010-06-11 17:34:16 -0700413 final int hStartPadding = getLeftPadding();
414 final int vStartPadding = getTopPadding();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800415
416 result[0] = (x - hStartPadding) / (mCellWidth + mWidthGap);
417 result[1] = (y - vStartPadding) / (mCellHeight + mHeightGap);
418
Adam Cohend22015c2010-07-26 22:02:18 -0700419 final int xAxis = mCountX;
420 final int yAxis = mCountY;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800421
422 if (result[0] < 0) result[0] = 0;
423 if (result[0] >= xAxis) result[0] = xAxis - 1;
424 if (result[1] < 0) result[1] = 0;
425 if (result[1] >= yAxis) result[1] = yAxis - 1;
426 }
Winson Chungaafa03c2010-06-11 17:34:16 -0700427
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800428 /**
429 * Given a point, return the cell that most closely encloses that point
430 * @param x X coordinate of the point
431 * @param y Y coordinate of the point
432 * @param result Array of 2 ints to hold the x and y coordinate of the cell
433 */
434 void pointToCellRounded(int x, int y, int[] result) {
435 pointToCellExact(x + (mCellWidth / 2), y + (mCellHeight / 2), result);
436 }
437
438 /**
439 * Given a cell coordinate, return the point that represents the upper left corner of that cell
Winson Chungaafa03c2010-06-11 17:34:16 -0700440 *
441 * @param cellX X coordinate of the cell
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800442 * @param cellY Y coordinate of the cell
Winson Chungaafa03c2010-06-11 17:34:16 -0700443 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800444 * @param result Array of 2 ints to hold the x and y coordinate of the point
445 */
446 void cellToPoint(int cellX, int cellY, int[] result) {
Winson Chungaafa03c2010-06-11 17:34:16 -0700447 final int hStartPadding = getLeftPadding();
448 final int vStartPadding = getTopPadding();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800449
450 result[0] = hStartPadding + cellX * (mCellWidth + mWidthGap);
451 result[1] = vStartPadding + cellY * (mCellHeight + mHeightGap);
452 }
453
Romain Guy84f296c2009-11-04 15:00:44 -0800454 int getCellWidth() {
455 return mCellWidth;
456 }
457
458 int getCellHeight() {
459 return mCellHeight;
460 }
461
Romain Guy1a304a12009-11-10 00:02:32 -0800462 int getLeftPadding() {
Winson Chungaafa03c2010-06-11 17:34:16 -0700463 return mLeftPadding;
Romain Guy1a304a12009-11-10 00:02:32 -0800464 }
465
466 int getTopPadding() {
Winson Chungaafa03c2010-06-11 17:34:16 -0700467 return mTopPadding;
Romain Guy1a304a12009-11-10 00:02:32 -0800468 }
469
470 int getRightPadding() {
Winson Chungaafa03c2010-06-11 17:34:16 -0700471 return mRightPadding;
Romain Guy1a304a12009-11-10 00:02:32 -0800472 }
473
474 int getBottomPadding() {
Winson Chungaafa03c2010-06-11 17:34:16 -0700475 return mBottomPadding;
Romain Guy1a304a12009-11-10 00:02:32 -0800476 }
477
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800478 @Override
479 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
480 // TODO: currently ignoring padding
Winson Chungaafa03c2010-06-11 17:34:16 -0700481
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800482 int widthSpecMode = MeasureSpec.getMode(widthMeasureSpec);
Winson Chungaafa03c2010-06-11 17:34:16 -0700483 int widthSpecSize = MeasureSpec.getSize(widthMeasureSpec);
484
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800485 int heightSpecMode = MeasureSpec.getMode(heightMeasureSpec);
486 int heightSpecSize = MeasureSpec.getSize(heightMeasureSpec);
Winson Chungaafa03c2010-06-11 17:34:16 -0700487
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800488 if (widthSpecMode == MeasureSpec.UNSPECIFIED || heightSpecMode == MeasureSpec.UNSPECIFIED) {
489 throw new RuntimeException("CellLayout cannot have UNSPECIFIED dimensions");
490 }
491
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800492 final int cellWidth = mCellWidth;
493 final int cellHeight = mCellHeight;
494
Adam Cohend22015c2010-07-26 22:02:18 -0700495 if (mOccupied == null) {
496 mOccupied = new boolean[mCountX][mCountY];
Winson Chungaafa03c2010-06-11 17:34:16 -0700497 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800498
Adam Cohend22015c2010-07-26 22:02:18 -0700499 int numWidthGaps = mCountX - 1;
500 int numHeightGaps = mCountY - 1;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800501
Adam Cohend22015c2010-07-26 22:02:18 -0700502 int vSpaceLeft = heightSpecSize - mTopPadding
503 - mBottomPadding - (cellHeight * mCountY);
504 mHeightGap = vSpaceLeft / numHeightGaps;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800505
Adam Cohend22015c2010-07-26 22:02:18 -0700506 int hSpaceLeft = widthSpecSize - mLeftPadding
507 - mRightPadding - (cellWidth * mCountX);
508 mWidthGap = hSpaceLeft / numWidthGaps;
Winson Chungaafa03c2010-06-11 17:34:16 -0700509
Michael Jurka5f1c5092010-09-03 14:15:02 -0700510 // center it around the min gaps
511 int minGap = Math.min(mWidthGap, mHeightGap);
512 mWidthGap = mHeightGap = minGap;
513
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800514 int count = getChildCount();
515
516 for (int i = 0; i < count; i++) {
517 View child = getChildAt(i);
518 LayoutParams lp = (LayoutParams) child.getLayoutParams();
Winson Chungaafa03c2010-06-11 17:34:16 -0700519 lp.setup(cellWidth, cellHeight, mWidthGap, mHeightGap,
520 mLeftPadding, mTopPadding);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800521
Winson Chungaafa03c2010-06-11 17:34:16 -0700522 int childWidthMeasureSpec = MeasureSpec.makeMeasureSpec(lp.width,
523 MeasureSpec.EXACTLY);
524 int childheightMeasureSpec = MeasureSpec.makeMeasureSpec(lp.height,
525 MeasureSpec.EXACTLY);
526
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800527 child.measure(childWidthMeasureSpec, childheightMeasureSpec);
528 }
Michael Jurka5f1c5092010-09-03 14:15:02 -0700529 if (widthSpecMode == MeasureSpec.AT_MOST) {
530 int newWidth = mLeftPadding + mRightPadding + (mCountX * cellWidth) +
531 ((mCountX - 1) * minGap);
532 int newHeight = mTopPadding + mBottomPadding + (mCountY * cellHeight) +
533 ((mCountY - 1) * minGap);
534 setMeasuredDimension(newWidth, newHeight);
535 } else if (widthSpecMode == MeasureSpec.EXACTLY) {
536 setMeasuredDimension(widthSpecSize, heightSpecSize);
537 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800538 }
539
540 @Override
541 protected void onLayout(boolean changed, int l, int t, int r, int b) {
542 int count = getChildCount();
543
544 for (int i = 0; i < count; i++) {
545 View child = getChildAt(i);
546 if (child.getVisibility() != GONE) {
547
548 CellLayout.LayoutParams lp = (CellLayout.LayoutParams) child.getLayoutParams();
549
550 int childLeft = lp.x;
551 int childTop = lp.y;
552 child.layout(childLeft, childTop, childLeft + lp.width, childTop + lp.height);
Romain Guy84f296c2009-11-04 15:00:44 -0800553
554 if (lp.dropped) {
555 lp.dropped = false;
556
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700557 final int[] cellXY = mTmpCellXY;
Romain Guy06762ab2010-01-25 16:51:08 -0800558 getLocationOnScreen(cellXY);
Romain Guy84f296c2009-11-04 15:00:44 -0800559 mWallpaperManager.sendWallpaperCommand(getWindowToken(), "android.home.drop",
Romain Guy06762ab2010-01-25 16:51:08 -0800560 cellXY[0] + childLeft + lp.width / 2,
561 cellXY[1] + childTop + lp.height / 2, 0, null);
Romain Guy84f296c2009-11-04 15:00:44 -0800562 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800563 }
564 }
565 }
566
567 @Override
Michael Jurkadee05892010-07-27 10:01:56 -0700568 protected void onSizeChanged(int w, int h, int oldw, int oldh) {
569 super.onSizeChanged(w, h, oldw, oldh);
Michael Jurka5f1c5092010-09-03 14:15:02 -0700570 mBackgroundLayoutRect.set(0, 0, w, h);
571 if (mBackground != null) {
572 mBackground.setBounds(mBackgroundLayoutRect);
Michael Jurkaa63c4522010-08-19 13:52:27 -0700573 }
Michael Jurka5f1c5092010-09-03 14:15:02 -0700574 if (mBackgroundHover != null) {
575 mBackgroundHover.setBounds(mBackgroundLayoutRect);
Michael Jurkaa63c4522010-08-19 13:52:27 -0700576 }
Michael Jurkadee05892010-07-27 10:01:56 -0700577 }
578
579 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800580 protected void setChildrenDrawingCacheEnabled(boolean enabled) {
581 final int count = getChildCount();
582 for (int i = 0; i < count; i++) {
583 final View view = getChildAt(i);
584 view.setDrawingCacheEnabled(enabled);
585 // Update the drawing caches
Adam Powellfefa0ce2010-05-03 10:23:50 -0700586 view.buildDrawingCache(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800587 }
588 }
589
590 @Override
591 protected void setChildrenDrawnWithCacheEnabled(boolean enabled) {
592 super.setChildrenDrawnWithCacheEnabled(enabled);
593 }
594
Michael Jurka5f1c5092010-09-03 14:15:02 -0700595 public float getBackgroundAlpha() {
596 return mBackgroundAlpha;
Michael Jurkadee05892010-07-27 10:01:56 -0700597 }
598
Michael Jurka5f1c5092010-09-03 14:15:02 -0700599 public void setBackgroundAlpha(float alpha) {
600 mBackgroundAlpha = alpha;
Michael Jurka0142d492010-08-25 17:46:15 -0700601 invalidate();
Michael Jurkadee05892010-07-27 10:01:56 -0700602 }
603
Michael Jurka5f1c5092010-09-03 14:15:02 -0700604 // Need to return true to let the view system know we know how to handle alpha-- this is
605 // because when our children have an alpha of 0.0f, they are still rendering their "dimmed"
606 // versions
607 @Override
608 protected boolean onSetAlpha(int alpha) {
609 return true;
610 }
611
612 public void setAlpha(float alpha) {
613 setChildrenAlpha(alpha);
614 super.setAlpha(alpha);
615 }
616
Michael Jurkadee05892010-07-27 10:01:56 -0700617 private void setChildrenAlpha(float alpha) {
Michael Jurka0142d492010-08-25 17:46:15 -0700618 final int childCount = getChildCount();
619 for (int i = 0; i < childCount; i++) {
Michael Jurkadee05892010-07-27 10:01:56 -0700620 getChildAt(i).setAlpha(alpha);
621 }
622 }
623
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700624 private boolean isVacant(int originX, int originY, int spanX, int spanY) {
625 for (int i = 0; i < spanY; i++) {
626 if (!isRowEmpty(originY + i, originX, originX + spanX - 1, mOccupied)) {
627 return false;
628 }
629 }
630 return true;
631 }
632
Patrick Dubroy440c3602010-07-13 17:50:32 -0700633 public View getChildAt(int x, int y) {
634 final int count = getChildCount();
635 for (int i = 0; i < count; i++) {
636 View child = getChildAt(i);
637 LayoutParams lp = (LayoutParams) child.getLayoutParams();
638
639 if ((lp.cellX <= x) && (x < lp.cellX + lp.cellHSpan) &&
640 (lp.cellY <= y) && (y < lp.cellY + lp.cellHSpan)) {
641 return child;
642 }
643 }
644 return null;
645 }
646
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700647 /**
Patrick Dubroy8f86ddc2010-07-16 13:55:32 -0700648 * Estimate the size that a child with the given dimensions will take in the layout.
649 */
650 void estimateChildSize(int minWidth, int minHeight, int[] result) {
651 // Assuming it's placed at 0, 0, find where the bottom right cell will land
652 rectToCell(minWidth, minHeight, result);
653
654 // Then figure out the rect it will occupy
655 cellToRect(0, 0, result[0], result[1], mRectF);
656 result[0] = (int)mRectF.width();
657 result[1] = (int)mRectF.height();
658 }
659
660 /**
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700661 * Estimate where the top left cell of the dragged item will land if it is dropped.
662 *
663 * @param originX The X value of the top left corner of the item
664 * @param originY The Y value of the top left corner of the item
665 * @param spanX The number of horizontal cells that the item spans
666 * @param spanY The number of vertical cells that the item spans
667 * @param result The estimated drop cell X and Y.
668 */
669 void estimateDropCell(int originX, int originY, int spanX, int spanY, int[] result) {
Adam Cohend22015c2010-07-26 22:02:18 -0700670 final int countX = mCountX;
671 final int countY = mCountY;
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700672
Michael Jurkaa63c4522010-08-19 13:52:27 -0700673 // pointToCellRounded takes the top left of a cell but will pad that with
674 // cellWidth/2 and cellHeight/2 when finding the matching cell
675 pointToCellRounded(originX, originY, result);
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700676
677 // If the item isn't fully on this screen, snap to the edges
678 int rightOverhang = result[0] + spanX - countX;
679 if (rightOverhang > 0) {
680 result[0] -= rightOverhang; // Snap to right
681 }
682 result[0] = Math.max(0, result[0]); // Snap to left
683 int bottomOverhang = result[1] + spanY - countY;
684 if (bottomOverhang > 0) {
685 result[1] -= bottomOverhang; // Snap to bottom
686 }
687 result[1] = Math.max(0, result[1]); // Snap to top
688 }
689
690 void visualizeDropLocation(View view, int originX, int originY, int spanX, int spanY) {
691 final int[] originCell = mDragCell;
692 final int[] cellXY = mTmpCellXY;
693 estimateDropCell(originX, originY, spanX, spanY, cellXY);
694
695 // Only recalculate the bounding rect when necessary
696 if (!Arrays.equals(cellXY, originCell)) {
697 originCell[0] = cellXY[0];
698 originCell[1] = cellXY[1];
699
700 // Find the top left corner of the rect the object will occupy
701 final int[] topLeft = mTmpCellXY;
702 cellToPoint(originCell[0], originCell[1], topLeft);
703 final int left = topLeft[0];
704 final int top = topLeft[1];
705
706 // Now find the bottom right
707 final int[] bottomRight = mTmpCellXY;
708 cellToPoint(originCell[0] + spanX - 1, originCell[1] + spanY - 1, bottomRight);
709 bottomRight[0] += mCellWidth;
710 bottomRight[1] += mCellHeight;
711
Adam Cohend22015c2010-07-26 22:02:18 -0700712 final int countX = mCountX;
713 final int countY = mCountY;
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700714 // TODO: It's not necessary to do this every time, but it's not especially expensive
715 findOccupiedCells(countX, countY, mOccupied, view, false);
716
717 boolean vacant = isVacant(originCell[0], originCell[1], spanX, spanY);
718 mDragRectDrawable = vacant ? mVacantDrawable : mOccupiedDrawable;
719
720 // mDragRect will be rendered in onDraw()
721 mDragRect.set(left, top, bottomRight[0], bottomRight[1]);
722 invalidate();
723 }
724 }
725
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800726 /**
Jeff Sharkey70864282009-04-07 21:08:40 -0700727 * Find a vacant area that will fit the given bounds nearest the requested
728 * cell location. Uses Euclidean distance to score multiple vacant areas.
Winson Chungaafa03c2010-06-11 17:34:16 -0700729 *
Romain Guy51afc022009-05-04 18:03:43 -0700730 * @param pixelX The X location at which you want to search for a vacant area.
731 * @param pixelY The Y location at which you want to search for a vacant area.
Jeff Sharkey70864282009-04-07 21:08:40 -0700732 * @param spanX Horizontal span of the object.
733 * @param spanY Vertical span of the object.
734 * @param vacantCells Pre-computed set of vacant cells to search.
735 * @param recycle Previously returned value to possibly recycle.
736 * @return The X, Y cell of a vacant area that can contain this object,
737 * nearest the requested location.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800738 */
Jeff Sharkey70864282009-04-07 21:08:40 -0700739 int[] findNearestVacantArea(int pixelX, int pixelY, int spanX, int spanY,
740 CellInfo vacantCells, int[] recycle) {
Winson Chungaafa03c2010-06-11 17:34:16 -0700741
Jeff Sharkey70864282009-04-07 21:08:40 -0700742 // Keep track of best-scoring drop area
743 final int[] bestXY = recycle != null ? recycle : new int[2];
Jeff Sharkey70864282009-04-07 21:08:40 -0700744 double bestDistance = Double.MAX_VALUE;
Winson Chungaafa03c2010-06-11 17:34:16 -0700745
Michael Jurkac28de512010-08-13 11:27:44 -0700746 for (int x = 0; x < mCountX - (spanX - 1); x++) {
747 inner:
748 for (int y = 0; y < mCountY - (spanY - 1); y++) {
749 for (int i = 0; i < spanX; i++) {
750 for (int j = 0; j < spanY; j++) {
751 if (mOccupied[x + i][y + j]) {
752 // small optimization: we can skip to below the row we just found
753 // an occupied cell
754 y += j;
755 continue inner;
756 }
757 }
758 }
759 final int[] cellXY = mTmpCellXY;
760 cellToPoint(x, y, cellXY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800761
Michael Jurkac28de512010-08-13 11:27:44 -0700762 double distance = Math.sqrt(Math.pow(cellXY[0] - pixelX, 2)
763 + Math.pow(cellXY[1] - pixelY, 2));
764 if (distance <= bestDistance) {
765 bestDistance = distance;
766 bestXY[0] = x;
767 bestXY[1] = y;
768 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800769 }
770 }
771
Winson Chungaafa03c2010-06-11 17:34:16 -0700772 // Return null if no suitable location found
Jeff Sharkey70864282009-04-07 21:08:40 -0700773 if (bestDistance < Double.MAX_VALUE) {
774 return bestXY;
775 } else {
776 return null;
777 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800778 }
Winson Chungaafa03c2010-06-11 17:34:16 -0700779
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800780 /**
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700781 * Called when a drag and drop operation has finished (successfully or not).
782 */
783 void onDragComplete() {
784 // Invalidate the drag data
785 mDragCell[0] = -1;
786 mDragCell[1] = -1;
787
Michael Jurkaa63c4522010-08-19 13:52:27 -0700788 setHover(false);
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700789 mDragRect.setEmpty();
790 invalidate();
791 }
792
793 /**
Winson Chungaafa03c2010-06-11 17:34:16 -0700794 * Mark a child as having been dropped.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800795 *
796 * @param child The child that is being dropped
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800797 */
Winson Chungaafa03c2010-06-11 17:34:16 -0700798 void onDropChild(View child) {
Romain Guyd94533d2009-08-17 10:01:15 -0700799 if (child != null) {
800 LayoutParams lp = (LayoutParams) child.getLayoutParams();
Romain Guyd94533d2009-08-17 10:01:15 -0700801 lp.isDragging = false;
Romain Guy84f296c2009-11-04 15:00:44 -0800802 lp.dropped = true;
Romain Guyd94533d2009-08-17 10:01:15 -0700803 mDragRect.setEmpty();
804 child.requestLayout();
Romain Guyd94533d2009-08-17 10:01:15 -0700805 }
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700806 onDragComplete();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800807 }
808
809 void onDropAborted(View child) {
810 if (child != null) {
811 ((LayoutParams) child.getLayoutParams()).isDragging = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800812 }
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700813 onDragComplete();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800814 }
815
816 /**
817 * Start dragging the specified child
Winson Chungaafa03c2010-06-11 17:34:16 -0700818 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800819 * @param child The child that is being dragged
820 */
821 void onDragChild(View child) {
822 LayoutParams lp = (LayoutParams) child.getLayoutParams();
823 lp.isDragging = true;
824 mDragRect.setEmpty();
825 }
Winson Chungaafa03c2010-06-11 17:34:16 -0700826
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800827 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800828 * Computes a bounding rectangle for a range of cells
Winson Chungaafa03c2010-06-11 17:34:16 -0700829 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800830 * @param cellX X coordinate of upper left corner expressed as a cell position
831 * @param cellY Y coordinate of upper left corner expressed as a cell position
Winson Chungaafa03c2010-06-11 17:34:16 -0700832 * @param cellHSpan Width in cells
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800833 * @param cellVSpan Height in cells
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700834 * @param resultRect Rect into which to put the results
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800835 */
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700836 public void cellToRect(int cellX, int cellY, int cellHSpan, int cellVSpan, RectF resultRect) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800837 final int cellWidth = mCellWidth;
838 final int cellHeight = mCellHeight;
839 final int widthGap = mWidthGap;
840 final int heightGap = mHeightGap;
Winson Chungaafa03c2010-06-11 17:34:16 -0700841
842 final int hStartPadding = getLeftPadding();
843 final int vStartPadding = getTopPadding();
844
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800845 int width = cellHSpan * cellWidth + ((cellHSpan - 1) * widthGap);
846 int height = cellVSpan * cellHeight + ((cellVSpan - 1) * heightGap);
847
848 int x = hStartPadding + cellX * (cellWidth + widthGap);
849 int y = vStartPadding + cellY * (cellHeight + heightGap);
Winson Chungaafa03c2010-06-11 17:34:16 -0700850
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700851 resultRect.set(x, y, x + width, y + height);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800852 }
Winson Chungaafa03c2010-06-11 17:34:16 -0700853
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800854 /**
Winson Chungaafa03c2010-06-11 17:34:16 -0700855 * Computes the required horizontal and vertical cell spans to always
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800856 * fit the given rectangle.
Winson Chungaafa03c2010-06-11 17:34:16 -0700857 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800858 * @param width Width in pixels
859 * @param height Height in pixels
Patrick Dubroy8f86ddc2010-07-16 13:55:32 -0700860 * @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 -0800861 */
Patrick Dubroy8f86ddc2010-07-16 13:55:32 -0700862 public int[] rectToCell(int width, int height, int[] result) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800863 // Always assume we're working with the smallest span to make sure we
864 // reserve enough space in both orientations.
Joe Onorato79e56262009-09-21 15:23:04 -0400865 final Resources resources = getResources();
866 int actualWidth = resources.getDimensionPixelSize(R.dimen.workspace_cell_width);
867 int actualHeight = resources.getDimensionPixelSize(R.dimen.workspace_cell_height);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800868 int smallerSize = Math.min(actualWidth, actualHeight);
Joe Onorato79e56262009-09-21 15:23:04 -0400869
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800870 // Always round up to next largest cell
871 int spanX = (width + smallerSize) / smallerSize;
872 int spanY = (height + smallerSize) / smallerSize;
Joe Onorato79e56262009-09-21 15:23:04 -0400873
Patrick Dubroy8f86ddc2010-07-16 13:55:32 -0700874 if (result == null) {
875 return new int[] { spanX, spanY };
876 }
877 result[0] = spanX;
878 result[1] = spanY;
879 return result;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800880 }
881
882 /**
883 * Find the first vacant cell, if there is one.
884 *
885 * @param vacant Holds the x and y coordinate of the vacant cell
886 * @param spanX Horizontal cell span.
887 * @param spanY Vertical cell span.
Winson Chungaafa03c2010-06-11 17:34:16 -0700888 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800889 * @return True if a vacant cell was found
890 */
891 public boolean getVacantCell(int[] vacant, int spanX, int spanY) {
Adam Cohend22015c2010-07-26 22:02:18 -0700892 final int xCount = mCountX;
893 final int yCount = mCountY;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800894 final boolean[][] occupied = mOccupied;
895
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700896 findOccupiedCells(xCount, yCount, occupied, null, true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800897
898 return findVacantCell(vacant, spanX, spanY, xCount, yCount, occupied);
899 }
900
901 static boolean findVacantCell(int[] vacant, int spanX, int spanY,
902 int xCount, int yCount, boolean[][] occupied) {
903
904 for (int x = 0; x < xCount; x++) {
905 for (int y = 0; y < yCount; y++) {
906 boolean available = !occupied[x][y];
907out: for (int i = x; i < x + spanX - 1 && x < xCount; i++) {
908 for (int j = y; j < y + spanY - 1 && y < yCount; j++) {
909 available = available && !occupied[i][j];
910 if (!available) break out;
911 }
912 }
913
914 if (available) {
915 vacant[0] = x;
916 vacant[1] = y;
917 return true;
918 }
919 }
920 }
921
922 return false;
923 }
924
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700925 /**
926 * Update the array of occupied cells (mOccupied), and return a flattened copy of the array.
927 */
928 boolean[] getOccupiedCellsFlattened() {
Adam Cohend22015c2010-07-26 22:02:18 -0700929 final int xCount = mCountX;
930 final int yCount = mCountY;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800931 final boolean[][] occupied = mOccupied;
932
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700933 findOccupiedCells(xCount, yCount, occupied, null, true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800934
935 final boolean[] flat = new boolean[xCount * yCount];
936 for (int y = 0; y < yCount; y++) {
937 for (int x = 0; x < xCount; x++) {
938 flat[y * xCount + x] = occupied[x][y];
939 }
940 }
941
942 return flat;
943 }
944
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700945 /**
946 * Update the array of occupied cells.
947 * @param ignoreView If non-null, the space occupied by this View is treated as vacant
948 * @param ignoreFolders If true, a cell occupied by a Folder is treated as vacant
949 */
950 private void findOccupiedCells(
951 int xCount, int yCount, boolean[][] occupied, View ignoreView, boolean ignoreFolders) {
952
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800953 for (int x = 0; x < xCount; x++) {
954 for (int y = 0; y < yCount; y++) {
955 occupied[x][y] = false;
956 }
957 }
958
959 int count = getChildCount();
960 for (int i = 0; i < count; i++) {
961 View child = getChildAt(i);
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700962 if ((ignoreFolders && child instanceof Folder) || child.equals(ignoreView)) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800963 continue;
964 }
965 LayoutParams lp = (LayoutParams) child.getLayoutParams();
966
967 for (int x = lp.cellX; x < lp.cellX + lp.cellHSpan && x < xCount; x++) {
968 for (int y = lp.cellY; y < lp.cellY + lp.cellVSpan && y < yCount; y++) {
969 occupied[x][y] = true;
970 }
971 }
972 }
973 }
974
975 @Override
976 public ViewGroup.LayoutParams generateLayoutParams(AttributeSet attrs) {
977 return new CellLayout.LayoutParams(getContext(), attrs);
978 }
979
980 @Override
981 protected boolean checkLayoutParams(ViewGroup.LayoutParams p) {
982 return p instanceof CellLayout.LayoutParams;
983 }
984
985 @Override
986 protected ViewGroup.LayoutParams generateLayoutParams(ViewGroup.LayoutParams p) {
987 return new CellLayout.LayoutParams(p);
988 }
989
Winson Chungaafa03c2010-06-11 17:34:16 -0700990 public static class CellLayoutAnimationController extends LayoutAnimationController {
991 public CellLayoutAnimationController(Animation animation, float delay) {
992 super(animation, delay);
993 }
994
995 @Override
996 protected long getDelayForView(View view) {
997 return (int) (Math.random() * 150);
998 }
999 }
1000
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001001 public static class LayoutParams extends ViewGroup.MarginLayoutParams {
1002 /**
1003 * Horizontal location of the item in the grid.
1004 */
1005 @ViewDebug.ExportedProperty
1006 public int cellX;
1007
1008 /**
1009 * Vertical location of the item in the grid.
1010 */
1011 @ViewDebug.ExportedProperty
1012 public int cellY;
1013
1014 /**
1015 * Number of cells spanned horizontally by the item.
1016 */
1017 @ViewDebug.ExportedProperty
1018 public int cellHSpan;
1019
1020 /**
1021 * Number of cells spanned vertically by the item.
1022 */
1023 @ViewDebug.ExportedProperty
1024 public int cellVSpan;
Winson Chungaafa03c2010-06-11 17:34:16 -07001025
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001026 /**
1027 * Is this item currently being dragged
1028 */
1029 public boolean isDragging;
1030
1031 // X coordinate of the view in the layout.
1032 @ViewDebug.ExportedProperty
1033 int x;
1034 // Y coordinate of the view in the layout.
1035 @ViewDebug.ExportedProperty
1036 int y;
1037
Romain Guy84f296c2009-11-04 15:00:44 -08001038 boolean dropped;
Romain Guyfcb9e712009-10-02 16:06:52 -07001039
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001040 public LayoutParams(Context c, AttributeSet attrs) {
1041 super(c, attrs);
1042 cellHSpan = 1;
1043 cellVSpan = 1;
1044 }
1045
1046 public LayoutParams(ViewGroup.LayoutParams source) {
1047 super(source);
1048 cellHSpan = 1;
1049 cellVSpan = 1;
1050 }
Winson Chungaafa03c2010-06-11 17:34:16 -07001051
1052 public LayoutParams(LayoutParams source) {
1053 super(source);
1054 this.cellX = source.cellX;
1055 this.cellY = source.cellY;
1056 this.cellHSpan = source.cellHSpan;
1057 this.cellVSpan = source.cellVSpan;
1058 }
1059
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001060 public LayoutParams(int cellX, int cellY, int cellHSpan, int cellVSpan) {
Romain Guy8f19cdd2010-01-08 15:07:00 -08001061 super(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001062 this.cellX = cellX;
1063 this.cellY = cellY;
1064 this.cellHSpan = cellHSpan;
1065 this.cellVSpan = cellVSpan;
1066 }
1067
1068 public void setup(int cellWidth, int cellHeight, int widthGap, int heightGap,
1069 int hStartPadding, int vStartPadding) {
Winson Chungaafa03c2010-06-11 17:34:16 -07001070
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001071 final int myCellHSpan = cellHSpan;
1072 final int myCellVSpan = cellVSpan;
1073 final int myCellX = cellX;
1074 final int myCellY = cellY;
Winson Chungaafa03c2010-06-11 17:34:16 -07001075
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001076 width = myCellHSpan * cellWidth + ((myCellHSpan - 1) * widthGap) -
1077 leftMargin - rightMargin;
1078 height = myCellVSpan * cellHeight + ((myCellVSpan - 1) * heightGap) -
1079 topMargin - bottomMargin;
1080
1081 x = hStartPadding + myCellX * (cellWidth + widthGap) + leftMargin;
1082 y = vStartPadding + myCellY * (cellHeight + heightGap) + topMargin;
1083 }
Winson Chungaafa03c2010-06-11 17:34:16 -07001084
1085 public String toString() {
1086 return "(" + this.cellX + ", " + this.cellY + ")";
1087 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001088 }
1089
1090 static final class CellInfo implements ContextMenu.ContextMenuInfo {
Michael Jurkac28de512010-08-13 11:27:44 -07001091 private boolean[][] mOccupied;
1092 private int mCountX;
1093 private int mCountY;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001094 View cell;
Michael Jurkaa63c4522010-08-19 13:52:27 -07001095 int cellX = -1;
1096 int cellY = -1;
Michael Jurkac28de512010-08-13 11:27:44 -07001097 // intersectX and intersectY constrain the results of findCellForSpan; any empty space
1098 // it results must include this point (unless intersectX and intersectY are -1)
Michael Jurkaa63c4522010-08-19 13:52:27 -07001099 int intersectX = -1;
1100 int intersectY = -1;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001101 int spanX;
1102 int spanY;
1103 int screen;
1104 boolean valid;
1105
Michael Jurkac28de512010-08-13 11:27:44 -07001106 void updateOccupiedCells(boolean[][] occupied, int xCount, int yCount) {
1107 mOccupied = occupied.clone();
1108 mCountX = xCount;
1109 mCountY = yCount;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001110 }
1111
Michael Jurkac28de512010-08-13 11:27:44 -07001112 void updateOccupiedCells(boolean[] occupied, int xCount, int yCount) {
1113 if (mOccupied == null || mCountX != xCount || mCountY != yCount) {
1114 mOccupied = new boolean[xCount][yCount];
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001115 }
Michael Jurkac28de512010-08-13 11:27:44 -07001116 mCountX = xCount;
1117 mCountY = yCount;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001118 for (int y = 0; y < yCount; y++) {
1119 for (int x = 0; x < xCount; x++) {
Michael Jurkac28de512010-08-13 11:27:44 -07001120 mOccupied[x][y] = occupied[y * xCount + x];
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001121 }
1122 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001123 }
1124
Michael Jurkac28de512010-08-13 11:27:44 -07001125 boolean existsEmptyCell() {
1126 return findCellForSpan(null, 1, 1);
1127 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001128 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001129 * Finds the upper-left coordinate of the first rectangle in the grid that can
Michael Jurkac28de512010-08-13 11:27:44 -07001130 * hold a cell of the specified dimensions. If intersectX and intersectY are not -1,
1131 * then this method will only return coordinates for rectangles that contain the cell
1132 * (intersectX, intersectY)
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001133 *
1134 * @param cellXY The array that will contain the position of a vacant cell if such a cell
1135 * can be found.
1136 * @param spanX The horizontal span of the cell we want to find.
1137 * @param spanY The vertical span of the cell we want to find.
1138 *
1139 * @return True if a vacant cell of the specified dimension was found, false otherwise.
1140 */
1141 boolean findCellForSpan(int[] cellXY, int spanX, int spanY) {
Michael Jurka0e260592010-06-30 17:07:39 -07001142 // return the span represented by the CellInfo only there is no view there
1143 // (this.cell == null) and there is enough space
Michael Jurkac28de512010-08-13 11:27:44 -07001144
Michael Jurka0e260592010-06-30 17:07:39 -07001145 if (this.cell == null && this.spanX >= spanX && this.spanY >= spanY) {
Michael Jurkac28de512010-08-13 11:27:44 -07001146 if (cellXY != null) {
1147 cellXY[0] = cellX;
1148 cellXY[1] = cellY;
1149 }
1150 return true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001151 }
1152
Michael Jurkac28de512010-08-13 11:27:44 -07001153 int startX = 0;
1154 if (intersectX >= 0) {
1155 startX = Math.max(startX, intersectX - (spanX - 1));
1156 }
1157 int endX = mCountX - (spanX - 1);
1158 if (intersectX >= 0) {
1159 endX = Math.min(endX, intersectX + (spanX - 1));
1160 }
1161 int startY = 0;
1162 if (intersectY >= 0) {
1163 startY = Math.max(startY, intersectY - (spanY - 1));
1164 }
1165 int endY = mCountY - (spanY - 1);
1166 if (intersectY >= 0) {
1167 endY = Math.min(endY, intersectY + (spanY - 1));
1168 }
1169
Michael Jurkaa63c4522010-08-19 13:52:27 -07001170 for (int x = startX; x < endX; x++) {
Michael Jurkac28de512010-08-13 11:27:44 -07001171 inner:
1172 for (int y = startY; y < endY; y++) {
1173 for (int i = 0; i < spanX; i++) {
1174 for (int j = 0; j < spanY; j++) {
1175 if (mOccupied[x + i][y + j]) {
1176 // small optimization: we can skip to below the row we just found
1177 // an occupied cell
1178 y += j;
1179 continue inner;
1180 }
1181 }
1182 }
1183 if (cellXY != null) {
1184 cellXY[0] = x;
1185 cellXY[1] = y;
1186 }
1187 return true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001188 }
1189 }
Michael Jurkac28de512010-08-13 11:27:44 -07001190 return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001191 }
1192
1193 @Override
1194 public String toString() {
Winson Chungaafa03c2010-06-11 17:34:16 -07001195 return "Cell[view=" + (cell == null ? "null" : cell.getClass())
1196 + ", x=" + cellX + ", y=" + cellY + "]";
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001197 }
1198 }
Mike Cleronf8bbd342009-10-23 16:15:16 -07001199
1200 public boolean lastDownOnOccupiedCell() {
1201 return mLastDownOnOccupiedCell;
1202 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001203}