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