The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1 | /* |
| 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 Onorato | a590252 | 2009-07-30 13:37:37 -0700 | [diff] [blame] | 17 | package com.android.launcher2; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 18 | |
| 19 | import android.content.Context; |
| 20 | import android.content.res.TypedArray; |
Joe Onorato | 79e5626 | 2009-09-21 15:23:04 -0400 | [diff] [blame] | 21 | import android.content.res.Resources; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 22 | import android.graphics.Rect; |
| 23 | import android.graphics.RectF; |
| 24 | import android.util.AttributeSet; |
| 25 | import android.view.ContextMenu; |
| 26 | import android.view.MotionEvent; |
| 27 | import android.view.View; |
| 28 | import android.view.ViewDebug; |
| 29 | import android.view.ViewGroup; |
Romain Guy | 84f296c | 2009-11-04 15:00:44 -0800 | [diff] [blame^] | 30 | import android.app.WallpaperManager; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 31 | |
| 32 | import java.util.ArrayList; |
| 33 | |
| 34 | public class CellLayout extends ViewGroup { |
| 35 | private boolean mPortrait; |
| 36 | |
| 37 | private int mCellWidth; |
| 38 | private int mCellHeight; |
| 39 | |
| 40 | private int mLongAxisStartPadding; |
| 41 | private int mLongAxisEndPadding; |
| 42 | |
| 43 | private int mShortAxisStartPadding; |
| 44 | private int mShortAxisEndPadding; |
| 45 | |
| 46 | private int mShortAxisCells; |
| 47 | private int mLongAxisCells; |
| 48 | |
| 49 | private int mWidthGap; |
| 50 | private int mHeightGap; |
| 51 | |
| 52 | private final Rect mRect = new Rect(); |
| 53 | private final CellInfo mCellInfo = new CellInfo(); |
| 54 | |
| 55 | int[] mCellXY = new int[2]; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 56 | boolean[][] mOccupied; |
| 57 | |
| 58 | private RectF mDragRect = new RectF(); |
| 59 | |
| 60 | private boolean mDirtyTag; |
Mike Cleron | f8bbd34 | 2009-10-23 16:15:16 -0700 | [diff] [blame] | 61 | private boolean mLastDownOnOccupiedCell = false; |
Romain Guy | 84f296c | 2009-11-04 15:00:44 -0800 | [diff] [blame^] | 62 | |
| 63 | private final WallpaperManager mWallpaperManager; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 64 | |
| 65 | public CellLayout(Context context) { |
| 66 | this(context, null); |
| 67 | } |
| 68 | |
| 69 | public CellLayout(Context context, AttributeSet attrs) { |
| 70 | this(context, attrs, 0); |
| 71 | } |
| 72 | |
| 73 | public CellLayout(Context context, AttributeSet attrs, int defStyle) { |
| 74 | super(context, attrs, defStyle); |
| 75 | TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.CellLayout, defStyle, 0); |
| 76 | |
| 77 | mCellWidth = a.getDimensionPixelSize(R.styleable.CellLayout_cellWidth, 10); |
| 78 | mCellHeight = a.getDimensionPixelSize(R.styleable.CellLayout_cellHeight, 10); |
| 79 | |
| 80 | mLongAxisStartPadding = |
| 81 | a.getDimensionPixelSize(R.styleable.CellLayout_longAxisStartPadding, 10); |
| 82 | mLongAxisEndPadding = |
| 83 | a.getDimensionPixelSize(R.styleable.CellLayout_longAxisEndPadding, 10); |
| 84 | mShortAxisStartPadding = |
| 85 | a.getDimensionPixelSize(R.styleable.CellLayout_shortAxisStartPadding, 10); |
| 86 | mShortAxisEndPadding = |
| 87 | a.getDimensionPixelSize(R.styleable.CellLayout_shortAxisEndPadding, 10); |
| 88 | |
| 89 | mShortAxisCells = a.getInt(R.styleable.CellLayout_shortAxisCells, 4); |
| 90 | mLongAxisCells = a.getInt(R.styleable.CellLayout_longAxisCells, 4); |
| 91 | |
| 92 | a.recycle(); |
| 93 | |
| 94 | setAlwaysDrawnWithCacheEnabled(false); |
| 95 | |
| 96 | if (mOccupied == null) { |
| 97 | if (mPortrait) { |
| 98 | mOccupied = new boolean[mShortAxisCells][mLongAxisCells]; |
| 99 | } else { |
| 100 | mOccupied = new boolean[mLongAxisCells][mShortAxisCells]; |
| 101 | } |
| 102 | } |
Romain Guy | 84f296c | 2009-11-04 15:00:44 -0800 | [diff] [blame^] | 103 | |
| 104 | mWallpaperManager = WallpaperManager.getInstance(getContext()); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 105 | } |
| 106 | |
Jeff Sharkey | 83f111d | 2009-04-20 21:03:13 -0700 | [diff] [blame] | 107 | @Override |
| 108 | public void cancelLongPress() { |
| 109 | super.cancelLongPress(); |
| 110 | |
| 111 | // Cancel long press for all children |
| 112 | final int count = getChildCount(); |
| 113 | for (int i = 0; i < count; i++) { |
| 114 | final View child = getChildAt(i); |
| 115 | child.cancelLongPress(); |
| 116 | } |
| 117 | } |
| 118 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 119 | int getCountX() { |
| 120 | return mPortrait ? mShortAxisCells : mLongAxisCells; |
| 121 | } |
| 122 | |
| 123 | int getCountY() { |
| 124 | return mPortrait ? mLongAxisCells : mShortAxisCells; |
| 125 | } |
| 126 | |
| 127 | @Override |
| 128 | public void addView(View child, int index, ViewGroup.LayoutParams params) { |
| 129 | // Generate an id for each view, this assumes we have at most 256x256 cells |
| 130 | // per workspace screen |
| 131 | final LayoutParams cellParams = (LayoutParams) params; |
Romain Guy | fcb9e71 | 2009-10-02 16:06:52 -0700 | [diff] [blame] | 132 | cellParams.regenerateId = true; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 133 | |
| 134 | super.addView(child, index, params); |
| 135 | } |
| 136 | |
| 137 | @Override |
| 138 | public void requestChildFocus(View child, View focused) { |
| 139 | super.requestChildFocus(child, focused); |
| 140 | if (child != null) { |
| 141 | Rect r = new Rect(); |
| 142 | child.getDrawingRect(r); |
| 143 | requestRectangleOnScreen(r); |
| 144 | } |
| 145 | } |
| 146 | |
| 147 | @Override |
| 148 | protected void onAttachedToWindow() { |
| 149 | super.onAttachedToWindow(); |
| 150 | mCellInfo.screen = ((ViewGroup) getParent()).indexOfChild(this); |
| 151 | } |
| 152 | |
| 153 | @Override |
| 154 | public boolean onInterceptTouchEvent(MotionEvent ev) { |
| 155 | final int action = ev.getAction(); |
| 156 | final CellInfo cellInfo = mCellInfo; |
| 157 | |
| 158 | if (action == MotionEvent.ACTION_DOWN) { |
| 159 | final Rect frame = mRect; |
| 160 | final int x = (int) ev.getX() + mScrollX; |
| 161 | final int y = (int) ev.getY() + mScrollY; |
| 162 | final int count = getChildCount(); |
| 163 | |
| 164 | boolean found = false; |
| 165 | for (int i = count - 1; i >= 0; i--) { |
| 166 | final View child = getChildAt(i); |
| 167 | |
| 168 | if ((child.getVisibility()) == VISIBLE || child.getAnimation() != null) { |
| 169 | child.getHitRect(frame); |
| 170 | if (frame.contains(x, y)) { |
| 171 | final LayoutParams lp = (LayoutParams) child.getLayoutParams(); |
| 172 | cellInfo.cell = child; |
| 173 | cellInfo.cellX = lp.cellX; |
| 174 | cellInfo.cellY = lp.cellY; |
| 175 | cellInfo.spanX = lp.cellHSpan; |
| 176 | cellInfo.spanY = lp.cellVSpan; |
| 177 | cellInfo.valid = true; |
| 178 | found = true; |
| 179 | mDirtyTag = false; |
| 180 | break; |
| 181 | } |
| 182 | } |
| 183 | } |
Mike Cleron | f8bbd34 | 2009-10-23 16:15:16 -0700 | [diff] [blame] | 184 | |
| 185 | mLastDownOnOccupiedCell = found; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 186 | |
| 187 | if (!found) { |
| 188 | int cellXY[] = mCellXY; |
| 189 | pointToCellExact(x, y, cellXY); |
| 190 | |
| 191 | final boolean portrait = mPortrait; |
| 192 | final int xCount = portrait ? mShortAxisCells : mLongAxisCells; |
| 193 | final int yCount = portrait ? mLongAxisCells : mShortAxisCells; |
| 194 | |
| 195 | final boolean[][] occupied = mOccupied; |
Jeff Sharkey | 7086428 | 2009-04-07 21:08:40 -0700 | [diff] [blame] | 196 | findOccupiedCells(xCount, yCount, occupied, null); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 197 | |
| 198 | cellInfo.cell = null; |
| 199 | cellInfo.cellX = cellXY[0]; |
| 200 | cellInfo.cellY = cellXY[1]; |
| 201 | cellInfo.spanX = 1; |
| 202 | cellInfo.spanY = 1; |
| 203 | cellInfo.valid = cellXY[0] >= 0 && cellXY[1] >= 0 && cellXY[0] < xCount && |
| 204 | cellXY[1] < yCount && !occupied[cellXY[0]][cellXY[1]]; |
| 205 | |
| 206 | // Instead of finding the interesting vacant cells here, wait until a |
| 207 | // caller invokes getTag() to retrieve the result. Finding the vacant |
| 208 | // cells is a bit expensive and can generate many new objects, it's |
| 209 | // therefore better to defer it until we know we actually need it. |
| 210 | |
| 211 | mDirtyTag = true; |
| 212 | } |
| 213 | setTag(cellInfo); |
| 214 | } else if (action == MotionEvent.ACTION_UP) { |
| 215 | cellInfo.cell = null; |
| 216 | cellInfo.cellX = -1; |
| 217 | cellInfo.cellY = -1; |
| 218 | cellInfo.spanX = 0; |
| 219 | cellInfo.spanY = 0; |
| 220 | cellInfo.valid = false; |
| 221 | mDirtyTag = false; |
| 222 | setTag(cellInfo); |
| 223 | } |
| 224 | |
| 225 | return false; |
| 226 | } |
| 227 | |
| 228 | @Override |
| 229 | public CellInfo getTag() { |
| 230 | final CellInfo info = (CellInfo) super.getTag(); |
| 231 | if (mDirtyTag && info.valid) { |
| 232 | final boolean portrait = mPortrait; |
| 233 | final int xCount = portrait ? mShortAxisCells : mLongAxisCells; |
| 234 | final int yCount = portrait ? mLongAxisCells : mShortAxisCells; |
| 235 | |
| 236 | final boolean[][] occupied = mOccupied; |
Jeff Sharkey | 7086428 | 2009-04-07 21:08:40 -0700 | [diff] [blame] | 237 | findOccupiedCells(xCount, yCount, occupied, null); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 238 | |
| 239 | findIntersectingVacantCells(info, info.cellX, info.cellY, xCount, yCount, occupied); |
| 240 | |
| 241 | mDirtyTag = false; |
| 242 | } |
| 243 | return info; |
| 244 | } |
| 245 | |
| 246 | private static void findIntersectingVacantCells(CellInfo cellInfo, int x, int y, |
| 247 | int xCount, int yCount, boolean[][] occupied) { |
| 248 | |
| 249 | cellInfo.maxVacantSpanX = Integer.MIN_VALUE; |
| 250 | cellInfo.maxVacantSpanXSpanY = Integer.MIN_VALUE; |
| 251 | cellInfo.maxVacantSpanY = Integer.MIN_VALUE; |
| 252 | cellInfo.maxVacantSpanYSpanX = Integer.MIN_VALUE; |
| 253 | cellInfo.clearVacantCells(); |
| 254 | |
| 255 | if (occupied[x][y]) { |
| 256 | return; |
| 257 | } |
| 258 | |
| 259 | cellInfo.current.set(x, y, x, y); |
| 260 | |
| 261 | findVacantCell(cellInfo.current, xCount, yCount, occupied, cellInfo); |
| 262 | } |
| 263 | |
| 264 | private static void findVacantCell(Rect current, int xCount, int yCount, boolean[][] occupied, |
| 265 | CellInfo cellInfo) { |
| 266 | |
| 267 | addVacantCell(current, cellInfo); |
| 268 | |
| 269 | if (current.left > 0) { |
| 270 | if (isColumnEmpty(current.left - 1, current.top, current.bottom, occupied)) { |
| 271 | current.left--; |
| 272 | findVacantCell(current, xCount, yCount, occupied, cellInfo); |
| 273 | current.left++; |
| 274 | } |
| 275 | } |
| 276 | |
| 277 | if (current.right < xCount - 1) { |
| 278 | if (isColumnEmpty(current.right + 1, current.top, current.bottom, occupied)) { |
| 279 | current.right++; |
| 280 | findVacantCell(current, xCount, yCount, occupied, cellInfo); |
| 281 | current.right--; |
| 282 | } |
| 283 | } |
| 284 | |
| 285 | if (current.top > 0) { |
| 286 | if (isRowEmpty(current.top - 1, current.left, current.right, occupied)) { |
| 287 | current.top--; |
| 288 | findVacantCell(current, xCount, yCount, occupied, cellInfo); |
| 289 | current.top++; |
| 290 | } |
| 291 | } |
| 292 | |
| 293 | if (current.bottom < yCount - 1) { |
| 294 | if (isRowEmpty(current.bottom + 1, current.left, current.right, occupied)) { |
| 295 | current.bottom++; |
| 296 | findVacantCell(current, xCount, yCount, occupied, cellInfo); |
| 297 | current.bottom--; |
| 298 | } |
| 299 | } |
| 300 | } |
| 301 | |
| 302 | private static void addVacantCell(Rect current, CellInfo cellInfo) { |
| 303 | CellInfo.VacantCell cell = CellInfo.VacantCell.acquire(); |
| 304 | cell.cellX = current.left; |
| 305 | cell.cellY = current.top; |
| 306 | cell.spanX = current.right - current.left + 1; |
| 307 | cell.spanY = current.bottom - current.top + 1; |
| 308 | if (cell.spanX > cellInfo.maxVacantSpanX) { |
| 309 | cellInfo.maxVacantSpanX = cell.spanX; |
| 310 | cellInfo.maxVacantSpanXSpanY = cell.spanY; |
| 311 | } |
| 312 | if (cell.spanY > cellInfo.maxVacantSpanY) { |
| 313 | cellInfo.maxVacantSpanY = cell.spanY; |
| 314 | cellInfo.maxVacantSpanYSpanX = cell.spanX; |
| 315 | } |
| 316 | cellInfo.vacantCells.add(cell); |
| 317 | } |
| 318 | |
| 319 | private static boolean isColumnEmpty(int x, int top, int bottom, boolean[][] occupied) { |
| 320 | for (int y = top; y <= bottom; y++) { |
| 321 | if (occupied[x][y]) { |
| 322 | return false; |
| 323 | } |
| 324 | } |
| 325 | return true; |
| 326 | } |
| 327 | |
| 328 | private static boolean isRowEmpty(int y, int left, int right, boolean[][] occupied) { |
| 329 | for (int x = left; x <= right; x++) { |
| 330 | if (occupied[x][y]) { |
| 331 | return false; |
| 332 | } |
| 333 | } |
| 334 | return true; |
| 335 | } |
| 336 | |
Jeff Sharkey | 7086428 | 2009-04-07 21:08:40 -0700 | [diff] [blame] | 337 | CellInfo findAllVacantCells(boolean[] occupiedCells, View ignoreView) { |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 338 | final boolean portrait = mPortrait; |
| 339 | final int xCount = portrait ? mShortAxisCells : mLongAxisCells; |
| 340 | final int yCount = portrait ? mLongAxisCells : mShortAxisCells; |
| 341 | |
| 342 | boolean[][] occupied = mOccupied; |
| 343 | |
| 344 | if (occupiedCells != null) { |
| 345 | for (int y = 0; y < yCount; y++) { |
| 346 | for (int x = 0; x < xCount; x++) { |
| 347 | occupied[x][y] = occupiedCells[y * xCount + x]; |
| 348 | } |
| 349 | } |
| 350 | } else { |
Jeff Sharkey | 7086428 | 2009-04-07 21:08:40 -0700 | [diff] [blame] | 351 | findOccupiedCells(xCount, yCount, occupied, ignoreView); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 352 | } |
| 353 | |
| 354 | CellInfo cellInfo = new CellInfo(); |
| 355 | |
| 356 | cellInfo.cellX = -1; |
| 357 | cellInfo.cellY = -1; |
| 358 | cellInfo.spanY = 0; |
| 359 | cellInfo.spanX = 0; |
| 360 | cellInfo.maxVacantSpanX = Integer.MIN_VALUE; |
| 361 | cellInfo.maxVacantSpanXSpanY = Integer.MIN_VALUE; |
| 362 | cellInfo.maxVacantSpanY = Integer.MIN_VALUE; |
| 363 | cellInfo.maxVacantSpanYSpanX = Integer.MIN_VALUE; |
| 364 | cellInfo.screen = mCellInfo.screen; |
| 365 | |
| 366 | Rect current = cellInfo.current; |
| 367 | |
| 368 | for (int x = 0; x < xCount; x++) { |
| 369 | for (int y = 0; y < yCount; y++) { |
| 370 | if (!occupied[x][y]) { |
| 371 | current.set(x, y, x, y); |
| 372 | findVacantCell(current, xCount, yCount, occupied, cellInfo); |
| 373 | occupied[x][y] = true; |
| 374 | } |
| 375 | } |
| 376 | } |
| 377 | |
| 378 | cellInfo.valid = cellInfo.vacantCells.size() > 0; |
| 379 | |
| 380 | // Assume the caller will perform their own cell searching, otherwise we |
| 381 | // risk causing an unnecessary rebuild after findCellForSpan() |
| 382 | |
| 383 | return cellInfo; |
| 384 | } |
| 385 | |
| 386 | /** |
| 387 | * Given a point, return the cell that strictly encloses that point |
| 388 | * @param x X coordinate of the point |
| 389 | * @param y Y coordinate of the point |
| 390 | * @param result Array of 2 ints to hold the x and y coordinate of the cell |
| 391 | */ |
| 392 | void pointToCellExact(int x, int y, int[] result) { |
| 393 | final boolean portrait = mPortrait; |
| 394 | |
| 395 | final int hStartPadding = portrait ? mShortAxisStartPadding : mLongAxisStartPadding; |
| 396 | final int vStartPadding = portrait ? mLongAxisStartPadding : mShortAxisStartPadding; |
| 397 | |
| 398 | result[0] = (x - hStartPadding) / (mCellWidth + mWidthGap); |
| 399 | result[1] = (y - vStartPadding) / (mCellHeight + mHeightGap); |
| 400 | |
| 401 | final int xAxis = portrait ? mShortAxisCells : mLongAxisCells; |
| 402 | final int yAxis = portrait ? mLongAxisCells : mShortAxisCells; |
| 403 | |
| 404 | if (result[0] < 0) result[0] = 0; |
| 405 | if (result[0] >= xAxis) result[0] = xAxis - 1; |
| 406 | if (result[1] < 0) result[1] = 0; |
| 407 | if (result[1] >= yAxis) result[1] = yAxis - 1; |
| 408 | } |
| 409 | |
| 410 | /** |
| 411 | * Given a point, return the cell that most closely encloses that point |
| 412 | * @param x X coordinate of the point |
| 413 | * @param y Y coordinate of the point |
| 414 | * @param result Array of 2 ints to hold the x and y coordinate of the cell |
| 415 | */ |
| 416 | void pointToCellRounded(int x, int y, int[] result) { |
| 417 | pointToCellExact(x + (mCellWidth / 2), y + (mCellHeight / 2), result); |
| 418 | } |
| 419 | |
| 420 | /** |
| 421 | * Given a cell coordinate, return the point that represents the upper left corner of that cell |
| 422 | * |
| 423 | * @param cellX X coordinate of the cell |
| 424 | * @param cellY Y coordinate of the cell |
| 425 | * |
| 426 | * @param result Array of 2 ints to hold the x and y coordinate of the point |
| 427 | */ |
| 428 | void cellToPoint(int cellX, int cellY, int[] result) { |
| 429 | final boolean portrait = mPortrait; |
| 430 | |
| 431 | final int hStartPadding = portrait ? mShortAxisStartPadding : mLongAxisStartPadding; |
| 432 | final int vStartPadding = portrait ? mLongAxisStartPadding : mShortAxisStartPadding; |
| 433 | |
| 434 | |
| 435 | result[0] = hStartPadding + cellX * (mCellWidth + mWidthGap); |
| 436 | result[1] = vStartPadding + cellY * (mCellHeight + mHeightGap); |
| 437 | } |
| 438 | |
Romain Guy | 84f296c | 2009-11-04 15:00:44 -0800 | [diff] [blame^] | 439 | int getCellWidth() { |
| 440 | return mCellWidth; |
| 441 | } |
| 442 | |
| 443 | int getCellHeight() { |
| 444 | return mCellHeight; |
| 445 | } |
| 446 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 447 | @Override |
| 448 | protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { |
| 449 | // TODO: currently ignoring padding |
| 450 | |
| 451 | int widthSpecMode = MeasureSpec.getMode(widthMeasureSpec); |
| 452 | int widthSpecSize = MeasureSpec.getSize(widthMeasureSpec); |
| 453 | |
| 454 | int heightSpecMode = MeasureSpec.getMode(heightMeasureSpec); |
| 455 | int heightSpecSize = MeasureSpec.getSize(heightMeasureSpec); |
| 456 | |
| 457 | if (widthSpecMode == MeasureSpec.UNSPECIFIED || heightSpecMode == MeasureSpec.UNSPECIFIED) { |
| 458 | throw new RuntimeException("CellLayout cannot have UNSPECIFIED dimensions"); |
| 459 | } |
| 460 | |
| 461 | final int shortAxisCells = mShortAxisCells; |
| 462 | final int longAxisCells = mLongAxisCells; |
| 463 | final int longAxisStartPadding = mLongAxisStartPadding; |
| 464 | final int longAxisEndPadding = mLongAxisEndPadding; |
| 465 | final int shortAxisStartPadding = mShortAxisStartPadding; |
| 466 | final int shortAxisEndPadding = mShortAxisEndPadding; |
| 467 | final int cellWidth = mCellWidth; |
| 468 | final int cellHeight = mCellHeight; |
| 469 | |
| 470 | mPortrait = heightSpecSize > widthSpecSize; |
| 471 | |
| 472 | int numShortGaps = shortAxisCells - 1; |
| 473 | int numLongGaps = longAxisCells - 1; |
| 474 | |
| 475 | if (mPortrait) { |
| 476 | int vSpaceLeft = heightSpecSize - longAxisStartPadding - longAxisEndPadding |
| 477 | - (cellHeight * longAxisCells); |
| 478 | mHeightGap = vSpaceLeft / numLongGaps; |
| 479 | |
| 480 | int hSpaceLeft = widthSpecSize - shortAxisStartPadding - shortAxisEndPadding |
| 481 | - (cellWidth * shortAxisCells); |
| 482 | if (numShortGaps > 0) { |
| 483 | mWidthGap = hSpaceLeft / numShortGaps; |
| 484 | } else { |
| 485 | mWidthGap = 0; |
| 486 | } |
| 487 | } else { |
| 488 | int hSpaceLeft = widthSpecSize - longAxisStartPadding - longAxisEndPadding |
| 489 | - (cellWidth * longAxisCells); |
| 490 | mWidthGap = hSpaceLeft / numLongGaps; |
| 491 | |
| 492 | int vSpaceLeft = heightSpecSize - shortAxisStartPadding - shortAxisEndPadding |
| 493 | - (cellHeight * shortAxisCells); |
| 494 | if (numShortGaps > 0) { |
| 495 | mHeightGap = vSpaceLeft / numShortGaps; |
| 496 | } else { |
| 497 | mHeightGap = 0; |
| 498 | } |
| 499 | } |
| 500 | |
| 501 | int count = getChildCount(); |
| 502 | |
| 503 | for (int i = 0; i < count; i++) { |
| 504 | View child = getChildAt(i); |
| 505 | LayoutParams lp = (LayoutParams) child.getLayoutParams(); |
| 506 | |
| 507 | if (mPortrait) { |
| 508 | lp.setup(cellWidth, cellHeight, mWidthGap, mHeightGap, shortAxisStartPadding, |
| 509 | longAxisStartPadding); |
| 510 | } else { |
| 511 | lp.setup(cellWidth, cellHeight, mWidthGap, mHeightGap, longAxisStartPadding, |
| 512 | shortAxisStartPadding); |
| 513 | } |
Romain Guy | fcb9e71 | 2009-10-02 16:06:52 -0700 | [diff] [blame] | 514 | |
| 515 | if (lp.regenerateId) { |
| 516 | child.setId(((getId() & 0xFF) << 16) | (lp.cellX & 0xFF) << 8 | (lp.cellY & 0xFF)); |
| 517 | lp.regenerateId = false; |
| 518 | } |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 519 | |
| 520 | int childWidthMeasureSpec = MeasureSpec.makeMeasureSpec(lp.width, MeasureSpec.EXACTLY); |
| 521 | int childheightMeasureSpec = |
| 522 | MeasureSpec.makeMeasureSpec(lp.height, MeasureSpec.EXACTLY); |
| 523 | child.measure(childWidthMeasureSpec, childheightMeasureSpec); |
| 524 | } |
| 525 | |
| 526 | setMeasuredDimension(widthSpecSize, heightSpecSize); |
| 527 | } |
| 528 | |
| 529 | @Override |
| 530 | protected void onLayout(boolean changed, int l, int t, int r, int b) { |
| 531 | int count = getChildCount(); |
| 532 | |
| 533 | for (int i = 0; i < count; i++) { |
| 534 | View child = getChildAt(i); |
| 535 | if (child.getVisibility() != GONE) { |
| 536 | |
| 537 | CellLayout.LayoutParams lp = (CellLayout.LayoutParams) child.getLayoutParams(); |
| 538 | |
| 539 | int childLeft = lp.x; |
| 540 | int childTop = lp.y; |
| 541 | child.layout(childLeft, childTop, childLeft + lp.width, childTop + lp.height); |
Romain Guy | 84f296c | 2009-11-04 15:00:44 -0800 | [diff] [blame^] | 542 | |
| 543 | if (lp.dropped) { |
| 544 | lp.dropped = false; |
| 545 | |
| 546 | mWallpaperManager.sendWallpaperCommand(getWindowToken(), "android.home.drop", |
| 547 | childLeft + lp.width / 2, childTop + lp.height / 2, 0, null); |
| 548 | } |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 549 | } |
| 550 | } |
| 551 | } |
| 552 | |
| 553 | @Override |
| 554 | protected void setChildrenDrawingCacheEnabled(boolean enabled) { |
| 555 | final int count = getChildCount(); |
| 556 | for (int i = 0; i < count; i++) { |
| 557 | final View view = getChildAt(i); |
| 558 | view.setDrawingCacheEnabled(enabled); |
| 559 | // Update the drawing caches |
Romain Guy | 806b0f3 | 2009-06-26 16:57:39 -0700 | [diff] [blame] | 560 | view.buildDrawingCache(true); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 561 | } |
| 562 | } |
| 563 | |
| 564 | @Override |
| 565 | protected void setChildrenDrawnWithCacheEnabled(boolean enabled) { |
| 566 | super.setChildrenDrawnWithCacheEnabled(enabled); |
| 567 | } |
| 568 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 569 | /** |
Jeff Sharkey | 7086428 | 2009-04-07 21:08:40 -0700 | [diff] [blame] | 570 | * Find a vacant area that will fit the given bounds nearest the requested |
| 571 | * cell location. Uses Euclidean distance to score multiple vacant areas. |
| 572 | * |
Romain Guy | 51afc02 | 2009-05-04 18:03:43 -0700 | [diff] [blame] | 573 | * @param pixelX The X location at which you want to search for a vacant area. |
| 574 | * @param pixelY The Y location at which you want to search for a vacant area. |
Jeff Sharkey | 7086428 | 2009-04-07 21:08:40 -0700 | [diff] [blame] | 575 | * @param spanX Horizontal span of the object. |
| 576 | * @param spanY Vertical span of the object. |
| 577 | * @param vacantCells Pre-computed set of vacant cells to search. |
| 578 | * @param recycle Previously returned value to possibly recycle. |
| 579 | * @return The X, Y cell of a vacant area that can contain this object, |
| 580 | * nearest the requested location. |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 581 | */ |
Jeff Sharkey | 7086428 | 2009-04-07 21:08:40 -0700 | [diff] [blame] | 582 | int[] findNearestVacantArea(int pixelX, int pixelY, int spanX, int spanY, |
| 583 | CellInfo vacantCells, int[] recycle) { |
| 584 | |
| 585 | // Keep track of best-scoring drop area |
| 586 | final int[] bestXY = recycle != null ? recycle : new int[2]; |
| 587 | final int[] cellXY = mCellXY; |
| 588 | double bestDistance = Double.MAX_VALUE; |
| 589 | |
| 590 | // Bail early if vacant cells aren't valid |
| 591 | if (!vacantCells.valid) { |
| 592 | return null; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 593 | } |
| 594 | |
Jeff Sharkey | 7086428 | 2009-04-07 21:08:40 -0700 | [diff] [blame] | 595 | // Look across all vacant cells for best fit |
| 596 | final int size = vacantCells.vacantCells.size(); |
| 597 | for (int i = 0; i < size; i++) { |
| 598 | final CellInfo.VacantCell cell = vacantCells.vacantCells.get(i); |
| 599 | |
| 600 | // Reject if vacant cell isn't our exact size |
| 601 | if (cell.spanX != spanX || cell.spanY != spanY) { |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 602 | continue; |
| 603 | } |
Jeff Sharkey | 7086428 | 2009-04-07 21:08:40 -0700 | [diff] [blame] | 604 | |
| 605 | // Score is center distance from requested pixel |
| 606 | cellToPoint(cell.cellX, cell.cellY, cellXY); |
| 607 | |
| 608 | double distance = Math.sqrt(Math.pow(cellXY[0] - pixelX, 2) + |
| 609 | Math.pow(cellXY[1] - pixelY, 2)); |
| 610 | if (distance <= bestDistance) { |
| 611 | bestDistance = distance; |
| 612 | bestXY[0] = cell.cellX; |
| 613 | bestXY[1] = cell.cellY; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 614 | } |
| 615 | } |
| 616 | |
Jeff Sharkey | 7086428 | 2009-04-07 21:08:40 -0700 | [diff] [blame] | 617 | // Return null if no suitable location found |
| 618 | if (bestDistance < Double.MAX_VALUE) { |
| 619 | return bestXY; |
| 620 | } else { |
| 621 | return null; |
| 622 | } |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 623 | } |
Jeff Sharkey | 7086428 | 2009-04-07 21:08:40 -0700 | [diff] [blame] | 624 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 625 | /** |
| 626 | * Drop a child at the specified position |
| 627 | * |
| 628 | * @param child The child that is being dropped |
Jeff Sharkey | 7086428 | 2009-04-07 21:08:40 -0700 | [diff] [blame] | 629 | * @param targetXY Destination area to move to |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 630 | */ |
Jeff Sharkey | 7086428 | 2009-04-07 21:08:40 -0700 | [diff] [blame] | 631 | void onDropChild(View child, int[] targetXY) { |
Romain Guy | d94533d | 2009-08-17 10:01:15 -0700 | [diff] [blame] | 632 | if (child != null) { |
| 633 | LayoutParams lp = (LayoutParams) child.getLayoutParams(); |
| 634 | lp.cellX = targetXY[0]; |
| 635 | lp.cellY = targetXY[1]; |
| 636 | lp.isDragging = false; |
Romain Guy | 84f296c | 2009-11-04 15:00:44 -0800 | [diff] [blame^] | 637 | lp.dropped = true; |
Romain Guy | d94533d | 2009-08-17 10:01:15 -0700 | [diff] [blame] | 638 | mDragRect.setEmpty(); |
| 639 | child.requestLayout(); |
| 640 | invalidate(); |
| 641 | } |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 642 | } |
| 643 | |
| 644 | void onDropAborted(View child) { |
| 645 | if (child != null) { |
| 646 | ((LayoutParams) child.getLayoutParams()).isDragging = false; |
| 647 | invalidate(); |
| 648 | } |
| 649 | mDragRect.setEmpty(); |
| 650 | } |
| 651 | |
| 652 | /** |
| 653 | * Start dragging the specified child |
| 654 | * |
| 655 | * @param child The child that is being dragged |
| 656 | */ |
| 657 | void onDragChild(View child) { |
| 658 | LayoutParams lp = (LayoutParams) child.getLayoutParams(); |
| 659 | lp.isDragging = true; |
| 660 | mDragRect.setEmpty(); |
| 661 | } |
| 662 | |
| 663 | /** |
| 664 | * Drag a child over the specified position |
| 665 | * |
| 666 | * @param child The child that is being dropped |
| 667 | * @param cellX The child's new x cell location |
| 668 | * @param cellY The child's new y cell location |
| 669 | */ |
| 670 | void onDragOverChild(View child, int cellX, int cellY) { |
| 671 | int[] cellXY = mCellXY; |
| 672 | pointToCellRounded(cellX, cellY, cellXY); |
| 673 | LayoutParams lp = (LayoutParams) child.getLayoutParams(); |
| 674 | cellToRect(cellXY[0], cellXY[1], lp.cellHSpan, lp.cellVSpan, mDragRect); |
| 675 | invalidate(); |
| 676 | } |
| 677 | |
| 678 | /** |
| 679 | * Computes a bounding rectangle for a range of cells |
| 680 | * |
| 681 | * @param cellX X coordinate of upper left corner expressed as a cell position |
| 682 | * @param cellY Y coordinate of upper left corner expressed as a cell position |
| 683 | * @param cellHSpan Width in cells |
| 684 | * @param cellVSpan Height in cells |
| 685 | * @param dragRect Rectnagle into which to put the results |
| 686 | */ |
| 687 | public void cellToRect(int cellX, int cellY, int cellHSpan, int cellVSpan, RectF dragRect) { |
| 688 | final boolean portrait = mPortrait; |
| 689 | final int cellWidth = mCellWidth; |
| 690 | final int cellHeight = mCellHeight; |
| 691 | final int widthGap = mWidthGap; |
| 692 | final int heightGap = mHeightGap; |
| 693 | |
| 694 | final int hStartPadding = portrait ? mShortAxisStartPadding : mLongAxisStartPadding; |
| 695 | final int vStartPadding = portrait ? mLongAxisStartPadding : mShortAxisStartPadding; |
| 696 | |
| 697 | int width = cellHSpan * cellWidth + ((cellHSpan - 1) * widthGap); |
| 698 | int height = cellVSpan * cellHeight + ((cellVSpan - 1) * heightGap); |
| 699 | |
| 700 | int x = hStartPadding + cellX * (cellWidth + widthGap); |
| 701 | int y = vStartPadding + cellY * (cellHeight + heightGap); |
| 702 | |
| 703 | dragRect.set(x, y, x + width, y + height); |
| 704 | } |
| 705 | |
| 706 | /** |
| 707 | * Computes the required horizontal and vertical cell spans to always |
| 708 | * fit the given rectangle. |
| 709 | * |
| 710 | * @param width Width in pixels |
| 711 | * @param height Height in pixels |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 712 | */ |
| 713 | public int[] rectToCell(int width, int height) { |
| 714 | // Always assume we're working with the smallest span to make sure we |
| 715 | // reserve enough space in both orientations. |
Joe Onorato | 79e5626 | 2009-09-21 15:23:04 -0400 | [diff] [blame] | 716 | final Resources resources = getResources(); |
| 717 | int actualWidth = resources.getDimensionPixelSize(R.dimen.workspace_cell_width); |
| 718 | int actualHeight = resources.getDimensionPixelSize(R.dimen.workspace_cell_height); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 719 | int smallerSize = Math.min(actualWidth, actualHeight); |
Joe Onorato | 79e5626 | 2009-09-21 15:23:04 -0400 | [diff] [blame] | 720 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 721 | // Always round up to next largest cell |
| 722 | int spanX = (width + smallerSize) / smallerSize; |
| 723 | int spanY = (height + smallerSize) / smallerSize; |
Joe Onorato | 79e5626 | 2009-09-21 15:23:04 -0400 | [diff] [blame] | 724 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 725 | return new int[] { spanX, spanY }; |
| 726 | } |
| 727 | |
| 728 | /** |
| 729 | * Find the first vacant cell, if there is one. |
| 730 | * |
| 731 | * @param vacant Holds the x and y coordinate of the vacant cell |
| 732 | * @param spanX Horizontal cell span. |
| 733 | * @param spanY Vertical cell span. |
| 734 | * |
| 735 | * @return True if a vacant cell was found |
| 736 | */ |
| 737 | public boolean getVacantCell(int[] vacant, int spanX, int spanY) { |
| 738 | final boolean portrait = mPortrait; |
| 739 | final int xCount = portrait ? mShortAxisCells : mLongAxisCells; |
| 740 | final int yCount = portrait ? mLongAxisCells : mShortAxisCells; |
| 741 | final boolean[][] occupied = mOccupied; |
| 742 | |
Jeff Sharkey | 7086428 | 2009-04-07 21:08:40 -0700 | [diff] [blame] | 743 | findOccupiedCells(xCount, yCount, occupied, null); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 744 | |
| 745 | return findVacantCell(vacant, spanX, spanY, xCount, yCount, occupied); |
| 746 | } |
| 747 | |
| 748 | static boolean findVacantCell(int[] vacant, int spanX, int spanY, |
| 749 | int xCount, int yCount, boolean[][] occupied) { |
| 750 | |
| 751 | for (int x = 0; x < xCount; x++) { |
| 752 | for (int y = 0; y < yCount; y++) { |
| 753 | boolean available = !occupied[x][y]; |
| 754 | out: for (int i = x; i < x + spanX - 1 && x < xCount; i++) { |
| 755 | for (int j = y; j < y + spanY - 1 && y < yCount; j++) { |
| 756 | available = available && !occupied[i][j]; |
| 757 | if (!available) break out; |
| 758 | } |
| 759 | } |
| 760 | |
| 761 | if (available) { |
| 762 | vacant[0] = x; |
| 763 | vacant[1] = y; |
| 764 | return true; |
| 765 | } |
| 766 | } |
| 767 | } |
| 768 | |
| 769 | return false; |
| 770 | } |
| 771 | |
| 772 | boolean[] getOccupiedCells() { |
| 773 | final boolean portrait = mPortrait; |
| 774 | final int xCount = portrait ? mShortAxisCells : mLongAxisCells; |
| 775 | final int yCount = portrait ? mLongAxisCells : mShortAxisCells; |
| 776 | final boolean[][] occupied = mOccupied; |
| 777 | |
Jeff Sharkey | 7086428 | 2009-04-07 21:08:40 -0700 | [diff] [blame] | 778 | findOccupiedCells(xCount, yCount, occupied, null); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 779 | |
| 780 | final boolean[] flat = new boolean[xCount * yCount]; |
| 781 | for (int y = 0; y < yCount; y++) { |
| 782 | for (int x = 0; x < xCount; x++) { |
| 783 | flat[y * xCount + x] = occupied[x][y]; |
| 784 | } |
| 785 | } |
| 786 | |
| 787 | return flat; |
| 788 | } |
| 789 | |
Jeff Sharkey | 7086428 | 2009-04-07 21:08:40 -0700 | [diff] [blame] | 790 | private void findOccupiedCells(int xCount, int yCount, boolean[][] occupied, View ignoreView) { |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 791 | for (int x = 0; x < xCount; x++) { |
| 792 | for (int y = 0; y < yCount; y++) { |
| 793 | occupied[x][y] = false; |
| 794 | } |
| 795 | } |
| 796 | |
| 797 | int count = getChildCount(); |
| 798 | for (int i = 0; i < count; i++) { |
| 799 | View child = getChildAt(i); |
Jeff Sharkey | 7086428 | 2009-04-07 21:08:40 -0700 | [diff] [blame] | 800 | if (child instanceof Folder || child.equals(ignoreView)) { |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 801 | continue; |
| 802 | } |
| 803 | LayoutParams lp = (LayoutParams) child.getLayoutParams(); |
| 804 | |
| 805 | for (int x = lp.cellX; x < lp.cellX + lp.cellHSpan && x < xCount; x++) { |
| 806 | for (int y = lp.cellY; y < lp.cellY + lp.cellVSpan && y < yCount; y++) { |
| 807 | occupied[x][y] = true; |
| 808 | } |
| 809 | } |
| 810 | } |
| 811 | } |
| 812 | |
| 813 | @Override |
| 814 | public ViewGroup.LayoutParams generateLayoutParams(AttributeSet attrs) { |
| 815 | return new CellLayout.LayoutParams(getContext(), attrs); |
| 816 | } |
| 817 | |
| 818 | @Override |
| 819 | protected boolean checkLayoutParams(ViewGroup.LayoutParams p) { |
| 820 | return p instanceof CellLayout.LayoutParams; |
| 821 | } |
| 822 | |
| 823 | @Override |
| 824 | protected ViewGroup.LayoutParams generateLayoutParams(ViewGroup.LayoutParams p) { |
| 825 | return new CellLayout.LayoutParams(p); |
| 826 | } |
| 827 | |
| 828 | public static class LayoutParams extends ViewGroup.MarginLayoutParams { |
| 829 | /** |
| 830 | * Horizontal location of the item in the grid. |
| 831 | */ |
| 832 | @ViewDebug.ExportedProperty |
| 833 | public int cellX; |
| 834 | |
| 835 | /** |
| 836 | * Vertical location of the item in the grid. |
| 837 | */ |
| 838 | @ViewDebug.ExportedProperty |
| 839 | public int cellY; |
| 840 | |
| 841 | /** |
| 842 | * Number of cells spanned horizontally by the item. |
| 843 | */ |
| 844 | @ViewDebug.ExportedProperty |
| 845 | public int cellHSpan; |
| 846 | |
| 847 | /** |
| 848 | * Number of cells spanned vertically by the item. |
| 849 | */ |
| 850 | @ViewDebug.ExportedProperty |
| 851 | public int cellVSpan; |
| 852 | |
| 853 | /** |
| 854 | * Is this item currently being dragged |
| 855 | */ |
| 856 | public boolean isDragging; |
| 857 | |
| 858 | // X coordinate of the view in the layout. |
| 859 | @ViewDebug.ExportedProperty |
| 860 | int x; |
| 861 | // Y coordinate of the view in the layout. |
| 862 | @ViewDebug.ExportedProperty |
| 863 | int y; |
| 864 | |
Romain Guy | fcb9e71 | 2009-10-02 16:06:52 -0700 | [diff] [blame] | 865 | boolean regenerateId; |
Romain Guy | 84f296c | 2009-11-04 15:00:44 -0800 | [diff] [blame^] | 866 | |
| 867 | boolean dropped; |
Romain Guy | fcb9e71 | 2009-10-02 16:06:52 -0700 | [diff] [blame] | 868 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 869 | public LayoutParams(Context c, AttributeSet attrs) { |
| 870 | super(c, attrs); |
| 871 | cellHSpan = 1; |
| 872 | cellVSpan = 1; |
| 873 | } |
| 874 | |
| 875 | public LayoutParams(ViewGroup.LayoutParams source) { |
| 876 | super(source); |
| 877 | cellHSpan = 1; |
| 878 | cellVSpan = 1; |
| 879 | } |
| 880 | |
| 881 | public LayoutParams(int cellX, int cellY, int cellHSpan, int cellVSpan) { |
| 882 | super(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT); |
| 883 | this.cellX = cellX; |
| 884 | this.cellY = cellY; |
| 885 | this.cellHSpan = cellHSpan; |
| 886 | this.cellVSpan = cellVSpan; |
| 887 | } |
| 888 | |
| 889 | public void setup(int cellWidth, int cellHeight, int widthGap, int heightGap, |
| 890 | int hStartPadding, int vStartPadding) { |
| 891 | |
| 892 | final int myCellHSpan = cellHSpan; |
| 893 | final int myCellVSpan = cellVSpan; |
| 894 | final int myCellX = cellX; |
| 895 | final int myCellY = cellY; |
| 896 | |
| 897 | width = myCellHSpan * cellWidth + ((myCellHSpan - 1) * widthGap) - |
| 898 | leftMargin - rightMargin; |
| 899 | height = myCellVSpan * cellHeight + ((myCellVSpan - 1) * heightGap) - |
| 900 | topMargin - bottomMargin; |
| 901 | |
| 902 | x = hStartPadding + myCellX * (cellWidth + widthGap) + leftMargin; |
| 903 | y = vStartPadding + myCellY * (cellHeight + heightGap) + topMargin; |
| 904 | } |
| 905 | } |
| 906 | |
| 907 | static final class CellInfo implements ContextMenu.ContextMenuInfo { |
| 908 | /** |
| 909 | * See View.AttachInfo.InvalidateInfo for futher explanations about |
| 910 | * the recycling mechanism. In this case, we recycle the vacant cells |
| 911 | * instances because up to several hundreds can be instanciated when |
| 912 | * the user long presses an empty cell. |
| 913 | */ |
| 914 | static final class VacantCell { |
| 915 | int cellX; |
| 916 | int cellY; |
| 917 | int spanX; |
| 918 | int spanY; |
| 919 | |
| 920 | // We can create up to 523 vacant cells on a 4x4 grid, 100 seems |
| 921 | // like a reasonable compromise given the size of a VacantCell and |
| 922 | // the fact that the user is not likely to touch an empty 4x4 grid |
| 923 | // very often |
| 924 | private static final int POOL_LIMIT = 100; |
| 925 | private static final Object sLock = new Object(); |
| 926 | |
| 927 | private static int sAcquiredCount = 0; |
| 928 | private static VacantCell sRoot; |
| 929 | |
| 930 | private VacantCell next; |
| 931 | |
| 932 | static VacantCell acquire() { |
| 933 | synchronized (sLock) { |
| 934 | if (sRoot == null) { |
| 935 | return new VacantCell(); |
| 936 | } |
| 937 | |
| 938 | VacantCell info = sRoot; |
| 939 | sRoot = info.next; |
| 940 | sAcquiredCount--; |
| 941 | |
| 942 | return info; |
| 943 | } |
| 944 | } |
| 945 | |
| 946 | void release() { |
| 947 | synchronized (sLock) { |
| 948 | if (sAcquiredCount < POOL_LIMIT) { |
| 949 | sAcquiredCount++; |
| 950 | next = sRoot; |
| 951 | sRoot = this; |
| 952 | } |
| 953 | } |
| 954 | } |
| 955 | |
| 956 | @Override |
| 957 | public String toString() { |
| 958 | return "VacantCell[x=" + cellX + ", y=" + cellY + ", spanX=" + spanX + |
| 959 | ", spanY=" + spanY + "]"; |
| 960 | } |
| 961 | } |
| 962 | |
| 963 | View cell; |
| 964 | int cellX; |
| 965 | int cellY; |
| 966 | int spanX; |
| 967 | int spanY; |
| 968 | int screen; |
| 969 | boolean valid; |
| 970 | |
| 971 | final ArrayList<VacantCell> vacantCells = new ArrayList<VacantCell>(VacantCell.POOL_LIMIT); |
| 972 | int maxVacantSpanX; |
| 973 | int maxVacantSpanXSpanY; |
| 974 | int maxVacantSpanY; |
| 975 | int maxVacantSpanYSpanX; |
| 976 | final Rect current = new Rect(); |
| 977 | |
Romain Guy | 4c58c48 | 2009-05-12 17:35:41 -0700 | [diff] [blame] | 978 | void clearVacantCells() { |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 979 | final ArrayList<VacantCell> list = vacantCells; |
| 980 | final int count = list.size(); |
| 981 | |
| 982 | for (int i = 0; i < count; i++) list.get(i).release(); |
| 983 | |
| 984 | list.clear(); |
| 985 | } |
| 986 | |
| 987 | void findVacantCellsFromOccupied(boolean[] occupied, int xCount, int yCount) { |
| 988 | if (cellX < 0 || cellY < 0) { |
| 989 | maxVacantSpanX = maxVacantSpanXSpanY = Integer.MIN_VALUE; |
| 990 | maxVacantSpanY = maxVacantSpanYSpanX = Integer.MIN_VALUE; |
| 991 | clearVacantCells(); |
| 992 | return; |
| 993 | } |
| 994 | |
| 995 | final boolean[][] unflattened = new boolean[xCount][yCount]; |
| 996 | for (int y = 0; y < yCount; y++) { |
| 997 | for (int x = 0; x < xCount; x++) { |
| 998 | unflattened[x][y] = occupied[y * xCount + x]; |
| 999 | } |
| 1000 | } |
| 1001 | CellLayout.findIntersectingVacantCells(this, cellX, cellY, xCount, yCount, unflattened); |
| 1002 | } |
| 1003 | |
| 1004 | /** |
| 1005 | * This method can be called only once! Calling #findVacantCellsFromOccupied will |
| 1006 | * restore the ability to call this method. |
| 1007 | * |
| 1008 | * Finds the upper-left coordinate of the first rectangle in the grid that can |
| 1009 | * hold a cell of the specified dimensions. |
| 1010 | * |
| 1011 | * @param cellXY The array that will contain the position of a vacant cell if such a cell |
| 1012 | * can be found. |
| 1013 | * @param spanX The horizontal span of the cell we want to find. |
| 1014 | * @param spanY The vertical span of the cell we want to find. |
| 1015 | * |
| 1016 | * @return True if a vacant cell of the specified dimension was found, false otherwise. |
| 1017 | */ |
| 1018 | boolean findCellForSpan(int[] cellXY, int spanX, int spanY) { |
Romain Guy | 4c58c48 | 2009-05-12 17:35:41 -0700 | [diff] [blame] | 1019 | return findCellForSpan(cellXY, spanX, spanY, true); |
| 1020 | } |
| 1021 | |
| 1022 | boolean findCellForSpan(int[] cellXY, int spanX, int spanY, boolean clear) { |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1023 | final ArrayList<VacantCell> list = vacantCells; |
| 1024 | final int count = list.size(); |
| 1025 | |
| 1026 | boolean found = false; |
| 1027 | |
| 1028 | if (this.spanX >= spanX && this.spanY >= spanY) { |
| 1029 | cellXY[0] = cellX; |
| 1030 | cellXY[1] = cellY; |
| 1031 | found = true; |
| 1032 | } |
| 1033 | |
| 1034 | // Look for an exact match first |
| 1035 | for (int i = 0; i < count; i++) { |
| 1036 | VacantCell cell = list.get(i); |
| 1037 | if (cell.spanX == spanX && cell.spanY == spanY) { |
| 1038 | cellXY[0] = cell.cellX; |
| 1039 | cellXY[1] = cell.cellY; |
| 1040 | found = true; |
| 1041 | break; |
| 1042 | } |
| 1043 | } |
| 1044 | |
| 1045 | // Look for the first cell large enough |
| 1046 | for (int i = 0; i < count; i++) { |
| 1047 | VacantCell cell = list.get(i); |
| 1048 | if (cell.spanX >= spanX && cell.spanY >= spanY) { |
| 1049 | cellXY[0] = cell.cellX; |
| 1050 | cellXY[1] = cell.cellY; |
| 1051 | found = true; |
| 1052 | break; |
| 1053 | } |
| 1054 | } |
| 1055 | |
Romain Guy | 4c58c48 | 2009-05-12 17:35:41 -0700 | [diff] [blame] | 1056 | if (clear) clearVacantCells(); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1057 | |
| 1058 | return found; |
| 1059 | } |
| 1060 | |
| 1061 | @Override |
| 1062 | public String toString() { |
| 1063 | return "Cell[view=" + (cell == null ? "null" : cell.getClass()) + ", x=" + cellX + |
| 1064 | ", y=" + cellY + "]"; |
| 1065 | } |
| 1066 | } |
Mike Cleron | f8bbd34 | 2009-10-23 16:15:16 -0700 | [diff] [blame] | 1067 | |
| 1068 | public boolean lastDownOnOccupiedCell() { |
| 1069 | return mLastDownOnOccupiedCell; |
| 1070 | } |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1071 | } |
| 1072 | |
| 1073 | |