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