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