blob: a55990b1efd03dd03e377f3cbea168d3b479a8eb [file] [log] [blame]
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001/*
2 * Copyright (C) 2008 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Joe Onoratoa5902522009-07-30 13:37:37 -070017package com.android.launcher2;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080018
Patrick Dubroy6569f2c2010-07-12 14:25:18 -070019import com.android.launcher.R;
Winson Chungaafa03c2010-06-11 17:34:16 -070020
Patrick Dubroyde7658b2010-09-27 11:15:43 -070021import android.animation.ValueAnimator;
22import android.animation.ValueAnimator.AnimatorUpdateListener;
Winson Chungaafa03c2010-06-11 17:34:16 -070023import android.app.WallpaperManager;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080024import android.content.Context;
Joe Onorato79e56262009-09-21 15:23:04 -040025import android.content.res.Resources;
Winson Chungaafa03c2010-06-11 17:34:16 -070026import android.content.res.TypedArray;
27import android.graphics.Canvas;
Patrick Dubroyde7658b2010-09-27 11:15:43 -070028import android.graphics.Point;
29import android.graphics.PointF;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080030import android.graphics.Rect;
31import android.graphics.RectF;
Patrick Dubroy6569f2c2010-07-12 14:25:18 -070032import android.graphics.drawable.Drawable;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080033import android.util.AttributeSet;
Patrick Dubroyde7658b2010-09-27 11:15:43 -070034import android.util.Log;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080035import android.view.ContextMenu;
36import android.view.MotionEvent;
37import android.view.View;
38import android.view.ViewDebug;
39import android.view.ViewGroup;
Winson Chungaafa03c2010-06-11 17:34:16 -070040import android.view.animation.Animation;
41import android.view.animation.LayoutAnimationController;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080042
Patrick Dubroy6569f2c2010-07-12 14:25:18 -070043import java.util.Arrays;
Romain Guyedcce092010-03-04 13:03:17 -080044
Adam Cohen0fb7db52010-09-29 13:02:43 -070045public class CellLayout extends ViewGroup {
Winson Chungaafa03c2010-06-11 17:34:16 -070046 static final String TAG = "CellLayout";
47
The Android Open Source Project31dd5032009-03-03 19:32:27 -080048 private int mCellWidth;
49 private int mCellHeight;
Winson Chungaafa03c2010-06-11 17:34:16 -070050
Winson Chungaafa03c2010-06-11 17:34:16 -070051 private int mLeftPadding;
52 private int mRightPadding;
53 private int mTopPadding;
54 private int mBottomPadding;
55
Adam Cohend22015c2010-07-26 22:02:18 -070056 private int mCountX;
57 private int mCountY;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080058
59 private int mWidthGap;
60 private int mHeightGap;
61
62 private final Rect mRect = new Rect();
Patrick Dubroy8f86ddc2010-07-16 13:55:32 -070063 private final RectF mRectF = new RectF();
The Android Open Source Project31dd5032009-03-03 19:32:27 -080064 private final CellInfo mCellInfo = new CellInfo();
Winson Chungaafa03c2010-06-11 17:34:16 -070065
Patrick Dubroyde7658b2010-09-27 11:15:43 -070066 // These are temporary variables to prevent having to allocate a new object just to
67 // return an (x, y) value from helper functions. Do NOT use them to maintain other state.
Patrick Dubroy6569f2c2010-07-12 14:25:18 -070068 private final int[] mTmpCellXY = new int[2];
Patrick Dubroyde7658b2010-09-27 11:15:43 -070069 private final int[] mTmpPoint = new int[2];
70 private final PointF mTmpPointF = new PointF();
Patrick Dubroy6569f2c2010-07-12 14:25:18 -070071
The Android Open Source Project31dd5032009-03-03 19:32:27 -080072 boolean[][] mOccupied;
73
Michael Jurkadee05892010-07-27 10:01:56 -070074 private OnTouchListener mInterceptTouchListener;
75
Michael Jurka5f1c5092010-09-03 14:15:02 -070076 private float mBackgroundAlpha;
77 private final Rect mBackgroundLayoutRect = new Rect();
78 private Drawable mBackground;
Adam Cohen0fb7db52010-09-29 13:02:43 -070079 private Drawable mBackgroundHover;
Michael Jurkaa63c4522010-08-19 13:52:27 -070080 // If we're actively dragging something over this screen and it's small,
81 // mHover is true
82 private boolean mHover = false;
Michael Jurkadee05892010-07-27 10:01:56 -070083
Patrick Dubroy6569f2c2010-07-12 14:25:18 -070084 private final RectF mDragRect = new RectF();
Patrick Dubroyde7658b2010-09-27 11:15:43 -070085 private final Point mDragCenter = new Point();
Patrick Dubroy6569f2c2010-07-12 14:25:18 -070086
Patrick Dubroy6569f2c2010-07-12 14:25:18 -070087 private Drawable mDragRectDrawable;
88
Patrick Dubroyde7658b2010-09-27 11:15:43 -070089 private Drawable mCrosshairsDrawable = null;
90 private ValueAnimator mCrosshairsAnimator = null;
91 private float mCrosshairsVisibility = 0.0f;
92
Patrick Dubroy6569f2c2010-07-12 14:25:18 -070093 // When a drag operation is in progress, holds the nearest cell to the touch point
94 private final int[] mDragCell = new int[2];
The Android Open Source Project31dd5032009-03-03 19:32:27 -080095
Winson Chungaafa03c2010-06-11 17:34:16 -070096 private final WallpaperManager mWallpaperManager;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080097
98 public CellLayout(Context context) {
99 this(context, null);
100 }
101
102 public CellLayout(Context context, AttributeSet attrs) {
103 this(context, attrs, 0);
104 }
105
106 public CellLayout(Context context, AttributeSet attrs, int defStyle) {
107 super(context, attrs, defStyle);
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700108
109 // A ViewGroup usually does not draw, but CellLayout needs to draw a rectangle to show
110 // the user where a dragged item will land when dropped.
111 setWillNotDraw(false);
Michael Jurkaa63c4522010-08-19 13:52:27 -0700112
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800113 TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.CellLayout, defStyle, 0);
114
115 mCellWidth = a.getDimensionPixelSize(R.styleable.CellLayout_cellWidth, 10);
116 mCellHeight = a.getDimensionPixelSize(R.styleable.CellLayout_cellHeight, 10);
Winson Chungaafa03c2010-06-11 17:34:16 -0700117
Adam Cohend22015c2010-07-26 22:02:18 -0700118 mLeftPadding =
119 a.getDimensionPixelSize(R.styleable.CellLayout_xAxisStartPadding, 10);
120 mRightPadding =
121 a.getDimensionPixelSize(R.styleable.CellLayout_xAxisEndPadding, 10);
122 mTopPadding =
123 a.getDimensionPixelSize(R.styleable.CellLayout_yAxisStartPadding, 10);
124 mBottomPadding =
125 a.getDimensionPixelSize(R.styleable.CellLayout_yAxisEndPadding, 10);
Winson Chungaafa03c2010-06-11 17:34:16 -0700126
Adam Cohend22015c2010-07-26 22:02:18 -0700127 mCountX = LauncherModel.getCellCountX();
128 mCountY = LauncherModel.getCellCountY();
Michael Jurka0280c3b2010-09-17 15:00:07 -0700129 mOccupied = new boolean[mCountX][mCountY];
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800130
131 a.recycle();
132
133 setAlwaysDrawnWithCacheEnabled(false);
134
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700135 mWallpaperManager = WallpaperManager.getInstance(context);
136
137 if (LauncherApplication.isScreenXLarge()) {
138 final Resources res = getResources();
139
Adam Cohen3db18d22010-09-29 13:47:41 -0700140 mBackground = res.getDrawable(R.drawable.mini_home_screen_bg);
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700141 mBackground.setFilterBitmap(true);
Adam Cohen0fb7db52010-09-29 13:02:43 -0700142 mBackgroundHover = res.getDrawable(R.drawable.mini_home_screen_bg_hover);
143 mBackgroundHover.setFilterBitmap(true);
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700144
145 mDragRectDrawable = res.getDrawable(R.drawable.rounded_rect_green);
146 mCrosshairsDrawable = res.getDrawable(R.drawable.gardening_crosshairs);
147
148 // Set up the animation for fading the crosshairs in and out
149 int animDuration = res.getInteger(R.integer.config_crosshairsFadeInTime);
150 mCrosshairsAnimator = new ValueAnimator<Float>(animDuration);
151 mCrosshairsAnimator.addUpdateListener(new AnimatorUpdateListener() {
152 public void onAnimationUpdate(ValueAnimator animation) {
153 mCrosshairsVisibility = ((Float) animation.getAnimatedValue()).floatValue();
154 CellLayout.this.invalidate();
155 }
156 });
157 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800158 }
159
Michael Jurkaa63c4522010-08-19 13:52:27 -0700160 public void setHover(boolean value) {
161 if (mHover != value) {
162 invalidate();
163 }
164 mHover = value;
165 }
166
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700167 private void animateCrosshairsTo(float value) {
168 final ValueAnimator anim = mCrosshairsAnimator;
169 long fullDuration = getResources().getInteger(R.integer.config_crosshairsFadeInTime);
170 anim.setDuration(fullDuration - anim.getCurrentPlayTime());
171 anim.setValues(mCrosshairsVisibility, value);
172 anim.cancel();
173 anim.start();
174 }
175
Jeff Sharkey83f111d2009-04-20 21:03:13 -0700176 @Override
Romain Guya6abce82009-11-10 02:54:41 -0800177 public void dispatchDraw(Canvas canvas) {
Michael Jurka5f1c5092010-09-03 14:15:02 -0700178 if (mBackgroundAlpha > 0.0f) {
Adam Cohen0fb7db52010-09-29 13:02:43 -0700179 final Drawable bg = mHover ? mBackgroundHover : mBackground;
Michael Jurka5f1c5092010-09-03 14:15:02 -0700180 bg.setAlpha((int) (mBackgroundAlpha * 255));
Michael Jurkaa63c4522010-08-19 13:52:27 -0700181 bg.draw(canvas);
182 }
Romain Guya6abce82009-11-10 02:54:41 -0800183 super.dispatchDraw(canvas);
184 }
185
186 @Override
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700187 protected void onDraw(Canvas canvas) {
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700188 if (mCrosshairsVisibility > 0.0f) {
189 final int countX = mCountX;
190 final int countY = mCountY;
191
192 if (!mDragRect.isEmpty()) {
193 mDragRectDrawable.setBounds(
194 (int)mDragRect.left,
195 (int)mDragRect.top,
196 (int)mDragRect.right,
197 (int)mDragRect.bottom);
198 mDragRectDrawable.setAlpha((int) (mCrosshairsVisibility * 255));
199 mDragRectDrawable.draw(canvas);
200 }
201
202 final float MAX_ALPHA = 0.4f;
203 final int MAX_VISIBLE_DISTANCE = 600;
204 final float DISTANCE_MULTIPLIER = 0.002f;
205
206 final Drawable d = mCrosshairsDrawable;
207 final int width = d.getIntrinsicWidth();
208 final int height = d.getIntrinsicHeight();
209
210 int x = getLeftPadding() - (mWidthGap / 2) - (width / 2);
211 for (int col = 0; col <= countX; col++) {
212 int y = getTopPadding() - (mHeightGap / 2) - (height / 2);
213 for (int row = 0; row <= countY; row++) {
214 mTmpPointF.set(x - mDragCenter.x, y - mDragCenter.y);
215 float dist = mTmpPointF.length();
216 // Crosshairs further from the drag point are more faint
217 float alpha = Math.min(MAX_ALPHA,
218 DISTANCE_MULTIPLIER * (MAX_VISIBLE_DISTANCE - dist));
219 if (alpha > 0.0f) {
220 d.setBounds(x, y, x + width, y + height);
221 d.setAlpha((int) (alpha * 255 * mCrosshairsVisibility));
222 d.draw(canvas);
223 }
224 y += mCellHeight + mHeightGap;
225 }
226 x += mCellWidth + mWidthGap;
227 }
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700228 }
229 }
230
231 @Override
Jeff Sharkey83f111d2009-04-20 21:03:13 -0700232 public void cancelLongPress() {
233 super.cancelLongPress();
234
235 // Cancel long press for all children
236 final int count = getChildCount();
237 for (int i = 0; i < count; i++) {
238 final View child = getChildAt(i);
239 child.cancelLongPress();
240 }
241 }
242
Michael Jurkadee05892010-07-27 10:01:56 -0700243 public void setOnInterceptTouchListener(View.OnTouchListener listener) {
244 mInterceptTouchListener = listener;
245 }
246
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800247 int getCountX() {
Adam Cohend22015c2010-07-26 22:02:18 -0700248 return mCountX;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800249 }
250
251 int getCountY() {
Adam Cohend22015c2010-07-26 22:02:18 -0700252 return mCountY;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800253 }
254
Winson Chungaafa03c2010-06-11 17:34:16 -0700255 public boolean addViewToCellLayout(View child, int index, int childId, LayoutParams params) {
256 final LayoutParams lp = params;
257
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800258 // Generate an id for each view, this assumes we have at most 256x256 cells
259 // per workspace screen
Adam Cohend22015c2010-07-26 22:02:18 -0700260 if (lp.cellX >= 0 && lp.cellX <= mCountX - 1 && lp.cellY >= 0 && lp.cellY <= mCountY - 1) {
Winson Chungaafa03c2010-06-11 17:34:16 -0700261 // If the horizontal or vertical span is set to -1, it is taken to
262 // mean that it spans the extent of the CellLayout
Adam Cohend22015c2010-07-26 22:02:18 -0700263 if (lp.cellHSpan < 0) lp.cellHSpan = mCountX;
264 if (lp.cellVSpan < 0) lp.cellVSpan = mCountY;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800265
Winson Chungaafa03c2010-06-11 17:34:16 -0700266 child.setId(childId);
267
Michael Jurkadee05892010-07-27 10:01:56 -0700268 // We might be in the middle or end of shrinking/fading to a dimmed view
269 // Make sure this view's alpha is set the same as all the rest of the views
Michael Jurka5f1c5092010-09-03 14:15:02 -0700270 child.setAlpha(getAlpha());
Winson Chungaafa03c2010-06-11 17:34:16 -0700271 addView(child, index, lp);
Michael Jurkadee05892010-07-27 10:01:56 -0700272
Michael Jurka0280c3b2010-09-17 15:00:07 -0700273 markCellsAsOccupiedForView(child);
274
Winson Chungaafa03c2010-06-11 17:34:16 -0700275 return true;
276 }
277 return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800278 }
279
280 @Override
Michael Jurka0280c3b2010-09-17 15:00:07 -0700281 public void removeAllViews() {
282 clearOccupiedCells();
283 }
284
285 @Override
286 public void removeAllViewsInLayout() {
287 clearOccupiedCells();
288 }
289
290 @Override
291 public void removeView(View view) {
292 markCellsAsUnoccupiedForView(view);
293 super.removeView(view);
294 }
295
296 @Override
297 public void removeViewAt(int index) {
298 markCellsAsUnoccupiedForView(getChildAt(index));
299 super.removeViewAt(index);
300 }
301
302 @Override
303 public void removeViewInLayout(View view) {
304 markCellsAsUnoccupiedForView(view);
305 super.removeViewInLayout(view);
306 }
307
308 @Override
309 public void removeViews(int start, int count) {
310 for (int i = start; i < start + count; i++) {
311 markCellsAsUnoccupiedForView(getChildAt(i));
312 }
313 super.removeViews(start, count);
314 }
315
316 @Override
317 public void removeViewsInLayout(int start, int count) {
318 for (int i = start; i < start + count; i++) {
319 markCellsAsUnoccupiedForView(getChildAt(i));
320 }
321 super.removeViewsInLayout(start, count);
322 }
323
324 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800325 public void requestChildFocus(View child, View focused) {
326 super.requestChildFocus(child, focused);
327 if (child != null) {
328 Rect r = new Rect();
329 child.getDrawingRect(r);
330 requestRectangleOnScreen(r);
331 }
332 }
333
334 @Override
335 protected void onAttachedToWindow() {
336 super.onAttachedToWindow();
337 mCellInfo.screen = ((ViewGroup) getParent()).indexOfChild(this);
338 }
339
Michael Jurkaaf442092010-06-10 17:01:57 -0700340 public void setTagToCellInfoForPoint(int touchX, int touchY) {
341 final CellInfo cellInfo = mCellInfo;
342 final Rect frame = mRect;
343 final int x = touchX + mScrollX;
344 final int y = touchY + mScrollY;
345 final int count = getChildCount();
346
347 boolean found = false;
348 for (int i = count - 1; i >= 0; i--) {
349 final View child = getChildAt(i);
350
351 if ((child.getVisibility()) == VISIBLE || child.getAnimation() != null) {
352 child.getHitRect(frame);
353 if (frame.contains(x, y)) {
354 final LayoutParams lp = (LayoutParams) child.getLayoutParams();
355 cellInfo.cell = child;
356 cellInfo.cellX = lp.cellX;
357 cellInfo.cellY = lp.cellY;
358 cellInfo.spanX = lp.cellHSpan;
359 cellInfo.spanY = lp.cellVSpan;
360 cellInfo.valid = true;
361 found = true;
Michael Jurkaaf442092010-06-10 17:01:57 -0700362 break;
363 }
364 }
365 }
Winson Chungaafa03c2010-06-11 17:34:16 -0700366
Michael Jurkaaf442092010-06-10 17:01:57 -0700367 if (!found) {
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700368 final int cellXY[] = mTmpCellXY;
Michael Jurkaaf442092010-06-10 17:01:57 -0700369 pointToCellExact(x, y, cellXY);
370
Michael Jurkaaf442092010-06-10 17:01:57 -0700371 cellInfo.cell = null;
372 cellInfo.cellX = cellXY[0];
373 cellInfo.cellY = cellXY[1];
374 cellInfo.spanX = 1;
375 cellInfo.spanY = 1;
Michael Jurka0280c3b2010-09-17 15:00:07 -0700376 cellInfo.valid = cellXY[0] >= 0 && cellXY[1] >= 0 && cellXY[0] < mCountX &&
377 cellXY[1] < mCountY && !mOccupied[cellXY[0]][cellXY[1]];
Michael Jurkaaf442092010-06-10 17:01:57 -0700378 }
379 setTag(cellInfo);
380 }
381
Winson Chungaafa03c2010-06-11 17:34:16 -0700382
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800383 @Override
384 public boolean onInterceptTouchEvent(MotionEvent ev) {
Michael Jurkadee05892010-07-27 10:01:56 -0700385 if (mInterceptTouchListener != null && mInterceptTouchListener.onTouch(this, ev)) {
386 return true;
387 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800388 final int action = ev.getAction();
389 final CellInfo cellInfo = mCellInfo;
390
391 if (action == MotionEvent.ACTION_DOWN) {
Michael Jurkaaf442092010-06-10 17:01:57 -0700392 setTagToCellInfoForPoint((int) ev.getX(), (int) ev.getY());
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800393 } else if (action == MotionEvent.ACTION_UP) {
394 cellInfo.cell = null;
395 cellInfo.cellX = -1;
396 cellInfo.cellY = -1;
397 cellInfo.spanX = 0;
398 cellInfo.spanY = 0;
399 cellInfo.valid = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800400 setTag(cellInfo);
401 }
402
403 return false;
404 }
405
406 @Override
407 public CellInfo getTag() {
Michael Jurka0280c3b2010-09-17 15:00:07 -0700408 return (CellInfo) super.getTag();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800409 }
410
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700411 /**
412 * Check if the row 'y' is empty from columns 'left' to 'right', inclusive.
413 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800414 private static boolean isRowEmpty(int y, int left, int right, boolean[][] occupied) {
415 for (int x = left; x <= right; x++) {
416 if (occupied[x][y]) {
417 return false;
418 }
419 }
420 return true;
421 }
422
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800423 /**
Winson Chungaafa03c2010-06-11 17:34:16 -0700424 * Given a point, return the cell that strictly encloses that point
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800425 * @param x X coordinate of the point
426 * @param y Y coordinate of the point
427 * @param result Array of 2 ints to hold the x and y coordinate of the cell
428 */
429 void pointToCellExact(int x, int y, int[] result) {
Winson Chungaafa03c2010-06-11 17:34:16 -0700430 final int hStartPadding = getLeftPadding();
431 final int vStartPadding = getTopPadding();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800432
433 result[0] = (x - hStartPadding) / (mCellWidth + mWidthGap);
434 result[1] = (y - vStartPadding) / (mCellHeight + mHeightGap);
435
Adam Cohend22015c2010-07-26 22:02:18 -0700436 final int xAxis = mCountX;
437 final int yAxis = mCountY;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800438
439 if (result[0] < 0) result[0] = 0;
440 if (result[0] >= xAxis) result[0] = xAxis - 1;
441 if (result[1] < 0) result[1] = 0;
442 if (result[1] >= yAxis) result[1] = yAxis - 1;
443 }
Winson Chungaafa03c2010-06-11 17:34:16 -0700444
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800445 /**
446 * Given a point, return the cell that most closely encloses that point
447 * @param x X coordinate of the point
448 * @param y Y coordinate of the point
449 * @param result Array of 2 ints to hold the x and y coordinate of the cell
450 */
451 void pointToCellRounded(int x, int y, int[] result) {
452 pointToCellExact(x + (mCellWidth / 2), y + (mCellHeight / 2), result);
453 }
454
455 /**
456 * Given a cell coordinate, return the point that represents the upper left corner of that cell
Winson Chungaafa03c2010-06-11 17:34:16 -0700457 *
458 * @param cellX X coordinate of the cell
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800459 * @param cellY Y coordinate of the cell
Winson Chungaafa03c2010-06-11 17:34:16 -0700460 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800461 * @param result Array of 2 ints to hold the x and y coordinate of the point
462 */
463 void cellToPoint(int cellX, int cellY, int[] result) {
Winson Chungaafa03c2010-06-11 17:34:16 -0700464 final int hStartPadding = getLeftPadding();
465 final int vStartPadding = getTopPadding();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800466
467 result[0] = hStartPadding + cellX * (mCellWidth + mWidthGap);
468 result[1] = vStartPadding + cellY * (mCellHeight + mHeightGap);
469 }
470
Romain Guy84f296c2009-11-04 15:00:44 -0800471 int getCellWidth() {
472 return mCellWidth;
473 }
474
475 int getCellHeight() {
476 return mCellHeight;
477 }
478
Romain Guy1a304a12009-11-10 00:02:32 -0800479 int getLeftPadding() {
Winson Chungaafa03c2010-06-11 17:34:16 -0700480 return mLeftPadding;
Romain Guy1a304a12009-11-10 00:02:32 -0800481 }
482
483 int getTopPadding() {
Winson Chungaafa03c2010-06-11 17:34:16 -0700484 return mTopPadding;
Romain Guy1a304a12009-11-10 00:02:32 -0800485 }
486
487 int getRightPadding() {
Winson Chungaafa03c2010-06-11 17:34:16 -0700488 return mRightPadding;
Romain Guy1a304a12009-11-10 00:02:32 -0800489 }
490
491 int getBottomPadding() {
Winson Chungaafa03c2010-06-11 17:34:16 -0700492 return mBottomPadding;
Romain Guy1a304a12009-11-10 00:02:32 -0800493 }
494
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800495 @Override
496 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
497 // TODO: currently ignoring padding
Winson Chungaafa03c2010-06-11 17:34:16 -0700498
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800499 int widthSpecMode = MeasureSpec.getMode(widthMeasureSpec);
Winson Chungaafa03c2010-06-11 17:34:16 -0700500 int widthSpecSize = MeasureSpec.getSize(widthMeasureSpec);
501
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800502 int heightSpecMode = MeasureSpec.getMode(heightMeasureSpec);
503 int heightSpecSize = MeasureSpec.getSize(heightMeasureSpec);
Winson Chungaafa03c2010-06-11 17:34:16 -0700504
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800505 if (widthSpecMode == MeasureSpec.UNSPECIFIED || heightSpecMode == MeasureSpec.UNSPECIFIED) {
506 throw new RuntimeException("CellLayout cannot have UNSPECIFIED dimensions");
507 }
508
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800509 final int cellWidth = mCellWidth;
510 final int cellHeight = mCellHeight;
511
Adam Cohend22015c2010-07-26 22:02:18 -0700512 int numWidthGaps = mCountX - 1;
513 int numHeightGaps = mCountY - 1;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800514
Michael Jurka0280c3b2010-09-17 15:00:07 -0700515 int vSpaceLeft = heightSpecSize - mTopPadding - mBottomPadding - (cellHeight * mCountY);
Adam Cohend22015c2010-07-26 22:02:18 -0700516 mHeightGap = vSpaceLeft / numHeightGaps;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800517
Michael Jurka0280c3b2010-09-17 15:00:07 -0700518 int hSpaceLeft = widthSpecSize - mLeftPadding - mRightPadding - (cellWidth * mCountX);
Adam Cohend22015c2010-07-26 22:02:18 -0700519 mWidthGap = hSpaceLeft / numWidthGaps;
Winson Chungaafa03c2010-06-11 17:34:16 -0700520
Michael Jurka5f1c5092010-09-03 14:15:02 -0700521 // center it around the min gaps
522 int minGap = Math.min(mWidthGap, mHeightGap);
523 mWidthGap = mHeightGap = minGap;
524
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800525 int count = getChildCount();
526
527 for (int i = 0; i < count; i++) {
528 View child = getChildAt(i);
529 LayoutParams lp = (LayoutParams) child.getLayoutParams();
Winson Chungaafa03c2010-06-11 17:34:16 -0700530 lp.setup(cellWidth, cellHeight, mWidthGap, mHeightGap,
531 mLeftPadding, mTopPadding);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800532
Michael Jurka0280c3b2010-09-17 15:00:07 -0700533 int childWidthMeasureSpec = MeasureSpec.makeMeasureSpec(lp.width, MeasureSpec.EXACTLY);
Winson Chungaafa03c2010-06-11 17:34:16 -0700534 int childheightMeasureSpec = MeasureSpec.makeMeasureSpec(lp.height,
535 MeasureSpec.EXACTLY);
536
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800537 child.measure(childWidthMeasureSpec, childheightMeasureSpec);
538 }
Michael Jurka5f1c5092010-09-03 14:15:02 -0700539 if (widthSpecMode == MeasureSpec.AT_MOST) {
540 int newWidth = mLeftPadding + mRightPadding + (mCountX * cellWidth) +
541 ((mCountX - 1) * minGap);
542 int newHeight = mTopPadding + mBottomPadding + (mCountY * cellHeight) +
543 ((mCountY - 1) * minGap);
544 setMeasuredDimension(newWidth, newHeight);
545 } else if (widthSpecMode == MeasureSpec.EXACTLY) {
546 setMeasuredDimension(widthSpecSize, heightSpecSize);
547 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800548 }
549
550 @Override
Michael Jurka28750fb2010-09-24 17:43:49 -0700551 protected void onLayout(boolean changed, int l, int t, int r, int b) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800552 int count = getChildCount();
553
554 for (int i = 0; i < count; i++) {
555 View child = getChildAt(i);
556 if (child.getVisibility() != GONE) {
557
558 CellLayout.LayoutParams lp = (CellLayout.LayoutParams) child.getLayoutParams();
559
560 int childLeft = lp.x;
561 int childTop = lp.y;
562 child.layout(childLeft, childTop, childLeft + lp.width, childTop + lp.height);
Romain Guy84f296c2009-11-04 15:00:44 -0800563
564 if (lp.dropped) {
565 lp.dropped = false;
566
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700567 final int[] cellXY = mTmpCellXY;
Romain Guy06762ab2010-01-25 16:51:08 -0800568 getLocationOnScreen(cellXY);
Romain Guy84f296c2009-11-04 15:00:44 -0800569 mWallpaperManager.sendWallpaperCommand(getWindowToken(), "android.home.drop",
Romain Guy06762ab2010-01-25 16:51:08 -0800570 cellXY[0] + childLeft + lp.width / 2,
571 cellXY[1] + childTop + lp.height / 2, 0, null);
Romain Guy84f296c2009-11-04 15:00:44 -0800572 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800573 }
574 }
575 }
576
577 @Override
Michael Jurkadee05892010-07-27 10:01:56 -0700578 protected void onSizeChanged(int w, int h, int oldw, int oldh) {
579 super.onSizeChanged(w, h, oldw, oldh);
Michael Jurka5f1c5092010-09-03 14:15:02 -0700580 mBackgroundLayoutRect.set(0, 0, w, h);
581 if (mBackground != null) {
582 mBackground.setBounds(mBackgroundLayoutRect);
Michael Jurkaa63c4522010-08-19 13:52:27 -0700583 }
Adam Cohen0fb7db52010-09-29 13:02:43 -0700584 if (mBackgroundHover != null) {
585 mBackgroundHover.setBounds(mBackgroundLayoutRect);
Michael Jurkaa63c4522010-08-19 13:52:27 -0700586 }
Michael Jurkadee05892010-07-27 10:01:56 -0700587 }
588
589 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800590 protected void setChildrenDrawingCacheEnabled(boolean enabled) {
591 final int count = getChildCount();
592 for (int i = 0; i < count; i++) {
593 final View view = getChildAt(i);
594 view.setDrawingCacheEnabled(enabled);
595 // Update the drawing caches
Adam Powellfefa0ce2010-05-03 10:23:50 -0700596 view.buildDrawingCache(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800597 }
598 }
599
600 @Override
601 protected void setChildrenDrawnWithCacheEnabled(boolean enabled) {
602 super.setChildrenDrawnWithCacheEnabled(enabled);
603 }
604
Michael Jurka5f1c5092010-09-03 14:15:02 -0700605 public float getBackgroundAlpha() {
606 return mBackgroundAlpha;
Michael Jurkadee05892010-07-27 10:01:56 -0700607 }
608
Michael Jurka5f1c5092010-09-03 14:15:02 -0700609 public void setBackgroundAlpha(float alpha) {
610 mBackgroundAlpha = alpha;
Michael Jurka0142d492010-08-25 17:46:15 -0700611 invalidate();
Michael Jurkadee05892010-07-27 10:01:56 -0700612 }
613
Michael Jurka5f1c5092010-09-03 14:15:02 -0700614 // Need to return true to let the view system know we know how to handle alpha-- this is
615 // because when our children have an alpha of 0.0f, they are still rendering their "dimmed"
616 // versions
617 @Override
618 protected boolean onSetAlpha(int alpha) {
619 return true;
620 }
621
622 public void setAlpha(float alpha) {
623 setChildrenAlpha(alpha);
624 super.setAlpha(alpha);
625 }
626
Michael Jurkadee05892010-07-27 10:01:56 -0700627 private void setChildrenAlpha(float alpha) {
Michael Jurka0142d492010-08-25 17:46:15 -0700628 final int childCount = getChildCount();
629 for (int i = 0; i < childCount; i++) {
Michael Jurkadee05892010-07-27 10:01:56 -0700630 getChildAt(i).setAlpha(alpha);
631 }
632 }
633
Michael Jurka0280c3b2010-09-17 15:00:07 -0700634 private boolean isVacantIgnoring(
635 int originX, int originY, int spanX, int spanY, View ignoreView) {
636 if (ignoreView != null) {
637 markCellsAsUnoccupiedForView(ignoreView);
638 }
Michael Jurka28750fb2010-09-24 17:43:49 -0700639 boolean isVacant = true;
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700640 for (int i = 0; i < spanY; i++) {
641 if (!isRowEmpty(originY + i, originX, originX + spanX - 1, mOccupied)) {
Michael Jurka28750fb2010-09-24 17:43:49 -0700642 isVacant = false;
643 break;
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700644 }
645 }
Michael Jurka0280c3b2010-09-17 15:00:07 -0700646 if (ignoreView != null) {
647 markCellsAsOccupiedForView(ignoreView);
648 }
Michael Jurka28750fb2010-09-24 17:43:49 -0700649 return isVacant;
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700650 }
651
Michael Jurka0280c3b2010-09-17 15:00:07 -0700652 private boolean isVacant(int originX, int originY, int spanX, int spanY) {
653 return isVacantIgnoring(originX, originY, spanX, spanY, null);
654 }
655
Patrick Dubroy440c3602010-07-13 17:50:32 -0700656 public View getChildAt(int x, int y) {
657 final int count = getChildCount();
658 for (int i = 0; i < count; i++) {
659 View child = getChildAt(i);
660 LayoutParams lp = (LayoutParams) child.getLayoutParams();
661
662 if ((lp.cellX <= x) && (x < lp.cellX + lp.cellHSpan) &&
663 (lp.cellY <= y) && (y < lp.cellY + lp.cellHSpan)) {
664 return child;
665 }
666 }
667 return null;
668 }
669
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700670 /**
Patrick Dubroy8f86ddc2010-07-16 13:55:32 -0700671 * Estimate the size that a child with the given dimensions will take in the layout.
672 */
673 void estimateChildSize(int minWidth, int minHeight, int[] result) {
674 // Assuming it's placed at 0, 0, find where the bottom right cell will land
675 rectToCell(minWidth, minHeight, result);
676
677 // Then figure out the rect it will occupy
678 cellToRect(0, 0, result[0], result[1], mRectF);
679 result[0] = (int)mRectF.width();
680 result[1] = (int)mRectF.height();
681 }
682
683 /**
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700684 * Estimate where the top left cell of the dragged item will land if it is dropped.
685 *
686 * @param originX The X value of the top left corner of the item
687 * @param originY The Y value of the top left corner of the item
688 * @param spanX The number of horizontal cells that the item spans
689 * @param spanY The number of vertical cells that the item spans
690 * @param result The estimated drop cell X and Y.
691 */
692 void estimateDropCell(int originX, int originY, int spanX, int spanY, int[] result) {
Adam Cohend22015c2010-07-26 22:02:18 -0700693 final int countX = mCountX;
694 final int countY = mCountY;
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700695
Michael Jurkaa63c4522010-08-19 13:52:27 -0700696 // pointToCellRounded takes the top left of a cell but will pad that with
697 // cellWidth/2 and cellHeight/2 when finding the matching cell
698 pointToCellRounded(originX, originY, result);
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700699
700 // If the item isn't fully on this screen, snap to the edges
701 int rightOverhang = result[0] + spanX - countX;
702 if (rightOverhang > 0) {
703 result[0] -= rightOverhang; // Snap to right
704 }
705 result[0] = Math.max(0, result[0]); // Snap to left
706 int bottomOverhang = result[1] + spanY - countY;
707 if (bottomOverhang > 0) {
708 result[1] -= bottomOverhang; // Snap to bottom
709 }
710 result[1] = Math.max(0, result[1]); // Snap to top
711 }
712
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700713 void visualizeDropLocation(View view, int originX, int originY, int spanX, int spanY) {
714 final int[] nearest = findNearestVacantArea(originX, originY, spanX, spanY, view, mDragCell);
715 mDragCenter.set(originX + (view.getWidth() / 2), originY + (view.getHeight() / 2));
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700716
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700717 if (nearest != null) {
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700718 // Find the top left corner of the rect the object will occupy
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700719 final int[] topLeft = mTmpPoint;
720 cellToPoint(nearest[0], nearest[1], topLeft);
721
722 // Need to copy these, because the next call to cellToPoint will overwrite them
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700723 final int left = topLeft[0];
724 final int top = topLeft[1];
725
726 // Now find the bottom right
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700727 final int[] bottomRight = mTmpPoint;
728 cellToPoint(nearest[0] + spanX - 1, nearest[1] + spanY - 1, bottomRight);
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700729 bottomRight[0] += mCellWidth;
730 bottomRight[1] += mCellHeight;
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700731 mDragRect.set(left, top, bottomRight[0], bottomRight[1]);
732 invalidate();
733 }
734 }
735
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800736 /**
Jeff Sharkey70864282009-04-07 21:08:40 -0700737 * Find a vacant area that will fit the given bounds nearest the requested
738 * cell location. Uses Euclidean distance to score multiple vacant areas.
Winson Chungaafa03c2010-06-11 17:34:16 -0700739 *
Romain Guy51afc022009-05-04 18:03:43 -0700740 * @param pixelX The X location at which you want to search for a vacant area.
741 * @param pixelY The Y location at which you want to search for a vacant area.
Jeff Sharkey70864282009-04-07 21:08:40 -0700742 * @param spanX Horizontal span of the object.
743 * @param spanY Vertical span of the object.
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700744 * @param result Array in which to place the result, or null (in which case a new array will
745 * be allocated)
Jeff Sharkey70864282009-04-07 21:08:40 -0700746 * @return The X, Y cell of a vacant area that can contain this object,
747 * nearest the requested location.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800748 */
Michael Jurka6a1435d2010-09-27 17:35:12 -0700749 int[] findNearestVacantArea(
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700750 int pixelX, int pixelY, int spanX, int spanY, int[] result) {
751 return findNearestVacantArea(pixelX, pixelY, spanX, spanY, null, result);
Michael Jurka6a1435d2010-09-27 17:35:12 -0700752 }
Winson Chungaafa03c2010-06-11 17:34:16 -0700753
Michael Jurka6a1435d2010-09-27 17:35:12 -0700754 /**
755 * Find a vacant area that will fit the given bounds nearest the requested
756 * cell location. Uses Euclidean distance to score multiple vacant areas.
757 *
758 * @param pixelX The X location at which you want to search for a vacant area.
759 * @param pixelY The Y location at which you want to search for a vacant area.
760 * @param spanX Horizontal span of the object.
761 * @param spanY Vertical span of the object.
Michael Jurka6a1435d2010-09-27 17:35:12 -0700762 * @param ignoreView Considers space occupied by this view as unoccupied
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700763 * @param result Previously returned value to possibly recycle.
Michael Jurka6a1435d2010-09-27 17:35:12 -0700764 * @return The X, Y cell of a vacant area that can contain this object,
765 * nearest the requested location.
766 */
767 int[] findNearestVacantArea(
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700768 int pixelX, int pixelY, int spanX, int spanY, View ignoreView, int[] result) {
Michael Jurkac6ee42e2010-09-30 12:04:50 -0700769 // mark space take by ignoreView as available (method checks if ignoreView is null)
770 markCellsAsUnoccupiedForView(ignoreView);
771
Jeff Sharkey70864282009-04-07 21:08:40 -0700772 // Keep track of best-scoring drop area
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700773 final int[] bestXY = result != null ? result : new int[2];
Jeff Sharkey70864282009-04-07 21:08:40 -0700774 double bestDistance = Double.MAX_VALUE;
Winson Chungaafa03c2010-06-11 17:34:16 -0700775
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700776 final int countX = mCountX;
777 final int countY = mCountY;
778 final boolean[][] occupied = mOccupied;
779
780 for (int x = 0; x < countX - (spanX - 1); x++) {
Michael Jurkac28de512010-08-13 11:27:44 -0700781 inner:
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700782 for (int y = 0; y < countY - (spanY - 1); y++) {
Michael Jurkac28de512010-08-13 11:27:44 -0700783 for (int i = 0; i < spanX; i++) {
784 for (int j = 0; j < spanY; j++) {
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700785 if (occupied[x + i][y + j]) {
Michael Jurkac28de512010-08-13 11:27:44 -0700786 // small optimization: we can skip to below the row we just found
787 // an occupied cell
788 y += j;
789 continue inner;
790 }
791 }
792 }
793 final int[] cellXY = mTmpCellXY;
794 cellToPoint(x, y, cellXY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800795
Michael Jurkac28de512010-08-13 11:27:44 -0700796 double distance = Math.sqrt(Math.pow(cellXY[0] - pixelX, 2)
797 + Math.pow(cellXY[1] - pixelY, 2));
798 if (distance <= bestDistance) {
799 bestDistance = distance;
800 bestXY[0] = x;
801 bestXY[1] = y;
802 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800803 }
804 }
Michael Jurkac6ee42e2010-09-30 12:04:50 -0700805 // re-mark space taken by ignoreView as occupied
806 markCellsAsOccupiedForView(ignoreView);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800807
Winson Chungaafa03c2010-06-11 17:34:16 -0700808 // Return null if no suitable location found
Jeff Sharkey70864282009-04-07 21:08:40 -0700809 if (bestDistance < Double.MAX_VALUE) {
810 return bestXY;
811 } else {
812 return null;
813 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800814 }
Winson Chungaafa03c2010-06-11 17:34:16 -0700815
Michael Jurka0280c3b2010-09-17 15:00:07 -0700816 boolean existsEmptyCell() {
817 return findCellForSpan(null, 1, 1);
818 }
819
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800820 /**
Michael Jurka0280c3b2010-09-17 15:00:07 -0700821 * Finds the upper-left coordinate of the first rectangle in the grid that can
822 * hold a cell of the specified dimensions. If intersectX and intersectY are not -1,
823 * then this method will only return coordinates for rectangles that contain the cell
824 * (intersectX, intersectY)
825 *
826 * @param cellXY The array that will contain the position of a vacant cell if such a cell
827 * can be found.
828 * @param spanX The horizontal span of the cell we want to find.
829 * @param spanY The vertical span of the cell we want to find.
830 *
831 * @return True if a vacant cell of the specified dimension was found, false otherwise.
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700832 */
Michael Jurka0280c3b2010-09-17 15:00:07 -0700833 boolean findCellForSpan(int[] cellXY, int spanX, int spanY) {
834 return findCellForSpanThatIntersectsIgnoring(cellXY, spanX, spanY, -1, -1, null);
835 }
836
837 /**
838 * Like above, but ignores any cells occupied by the item "ignoreView"
839 *
840 * @param cellXY The array that will contain the position of a vacant cell if such a cell
841 * can be found.
842 * @param spanX The horizontal span of the cell we want to find.
843 * @param spanY The vertical span of the cell we want to find.
844 * @param ignoreView The home screen item we should treat as not occupying any space
845 * @return
846 */
847 boolean findCellForSpanIgnoring(int[] cellXY, int spanX, int spanY, View ignoreView) {
848 return findCellForSpanThatIntersectsIgnoring(cellXY, spanX, spanY, -1, -1, ignoreView);
849 }
850
851 /**
852 * Like above, but if intersectX and intersectY are not -1, then this method will try to
853 * return coordinates for rectangles that contain the cell [intersectX, intersectY]
854 *
855 * @param spanX The horizontal span of the cell we want to find.
856 * @param spanY The vertical span of the cell we want to find.
857 * @param ignoreView The home screen item we should treat as not occupying any space
858 * @param intersectX The X coordinate of the cell that we should try to overlap
859 * @param intersectX The Y coordinate of the cell that we should try to overlap
860 *
861 * @return True if a vacant cell of the specified dimension was found, false otherwise.
862 */
863 boolean findCellForSpanThatIntersects(int[] cellXY, int spanX, int spanY,
864 int intersectX, int intersectY) {
865 return findCellForSpanThatIntersectsIgnoring(
866 cellXY, spanX, spanY, intersectX, intersectY, null);
867 }
868
869 /**
870 * The superset of the above two methods
871 */
872 boolean findCellForSpanThatIntersectsIgnoring(int[] cellXY, int spanX, int spanY,
873 int intersectX, int intersectY, View ignoreView) {
Michael Jurkac6ee42e2010-09-30 12:04:50 -0700874 // mark space take by ignoreView as available (method checks if ignoreView is null)
875 markCellsAsUnoccupiedForView(ignoreView);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700876
Michael Jurka28750fb2010-09-24 17:43:49 -0700877 boolean foundCell = false;
Michael Jurka0280c3b2010-09-17 15:00:07 -0700878 while (true) {
879 int startX = 0;
880 if (intersectX >= 0) {
881 startX = Math.max(startX, intersectX - (spanX - 1));
882 }
883 int endX = mCountX - (spanX - 1);
884 if (intersectX >= 0) {
885 endX = Math.min(endX, intersectX + (spanX - 1) + (spanX == 1 ? 1 : 0));
886 }
887 int startY = 0;
888 if (intersectY >= 0) {
889 startY = Math.max(startY, intersectY - (spanY - 1));
890 }
891 int endY = mCountY - (spanY - 1);
892 if (intersectY >= 0) {
893 endY = Math.min(endY, intersectY + (spanY - 1) + (spanY == 1 ? 1 : 0));
894 }
895
896 for (int x = startX; x < endX; x++) {
897 inner:
898 for (int y = startY; y < endY; y++) {
899 for (int i = 0; i < spanX; i++) {
900 for (int j = 0; j < spanY; j++) {
901 if (mOccupied[x + i][y + j]) {
902 // small optimization: we can skip to below the row we just found
903 // an occupied cell
904 y += j;
905 continue inner;
906 }
907 }
908 }
909 if (cellXY != null) {
910 cellXY[0] = x;
911 cellXY[1] = y;
912 }
Michael Jurka28750fb2010-09-24 17:43:49 -0700913 foundCell = true;
914 break;
Michael Jurka0280c3b2010-09-17 15:00:07 -0700915 }
916 }
917 if (intersectX == -1 && intersectY == -1) {
918 break;
919 } else {
920 // if we failed to find anything, try again but without any requirements of
921 // intersecting
922 intersectX = -1;
923 intersectY = -1;
924 continue;
925 }
926 }
927
Michael Jurkac6ee42e2010-09-30 12:04:50 -0700928 // re-mark space taken by ignoreView as occupied
929 markCellsAsOccupiedForView(ignoreView);
Michael Jurka28750fb2010-09-24 17:43:49 -0700930 return foundCell;
Michael Jurka0280c3b2010-09-17 15:00:07 -0700931 }
932
933 /**
934 * Called when drag has left this CellLayout or has been completed (successfully or not)
935 */
936 void onDragExit() {
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700937 // Invalidate the drag data
938 mDragCell[0] = -1;
939 mDragCell[1] = -1;
940
Michael Jurkaa63c4522010-08-19 13:52:27 -0700941 setHover(false);
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700942 invalidate();
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700943
944 // Fade out the drag indicators
945 if (mCrosshairsAnimator != null) {
946 animateCrosshairsTo(0.0f);
947 }
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700948 }
949
950 /**
Winson Chungaafa03c2010-06-11 17:34:16 -0700951 * Mark a child as having been dropped.
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700952 * At the beginning of the drag operation, the child may have been on another
953 * screen, but it is reparented before this method is called.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800954 *
955 * @param child The child that is being dropped
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800956 */
Winson Chungaafa03c2010-06-11 17:34:16 -0700957 void onDropChild(View child) {
Romain Guyd94533d2009-08-17 10:01:15 -0700958 if (child != null) {
959 LayoutParams lp = (LayoutParams) child.getLayoutParams();
Romain Guyd94533d2009-08-17 10:01:15 -0700960 lp.isDragging = false;
Romain Guy84f296c2009-11-04 15:00:44 -0800961 lp.dropped = true;
Romain Guyd94533d2009-08-17 10:01:15 -0700962 child.requestLayout();
Romain Guyd94533d2009-08-17 10:01:15 -0700963 }
Michael Jurka0280c3b2010-09-17 15:00:07 -0700964 onDragExit();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800965 }
966
967 void onDropAborted(View child) {
968 if (child != null) {
969 ((LayoutParams) child.getLayoutParams()).isDragging = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800970 }
Michael Jurka0280c3b2010-09-17 15:00:07 -0700971 onDragExit();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800972 }
973
974 /**
975 * Start dragging the specified child
Winson Chungaafa03c2010-06-11 17:34:16 -0700976 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800977 * @param child The child that is being dragged
978 */
979 void onDragChild(View child) {
980 LayoutParams lp = (LayoutParams) child.getLayoutParams();
981 lp.isDragging = true;
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700982 }
983
984 /**
985 * A drag event has begun over this layout.
986 * It may have begun over this layout (in which case onDragChild is called first),
987 * or it may have begun on another layout.
988 */
989 void onDragEnter(View dragView) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800990 mDragRect.setEmpty();
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700991 // Fade in the drag indicators
992 if (mCrosshairsAnimator != null) {
993 animateCrosshairsTo(1.0f);
994 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800995 }
Winson Chungaafa03c2010-06-11 17:34:16 -0700996
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800997 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800998 * Computes a bounding rectangle for a range of cells
Winson Chungaafa03c2010-06-11 17:34:16 -0700999 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001000 * @param cellX X coordinate of upper left corner expressed as a cell position
1001 * @param cellY Y coordinate of upper left corner expressed as a cell position
Winson Chungaafa03c2010-06-11 17:34:16 -07001002 * @param cellHSpan Width in cells
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001003 * @param cellVSpan Height in cells
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001004 * @param resultRect Rect into which to put the results
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001005 */
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001006 public void cellToRect(int cellX, int cellY, int cellHSpan, int cellVSpan, RectF resultRect) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001007 final int cellWidth = mCellWidth;
1008 final int cellHeight = mCellHeight;
1009 final int widthGap = mWidthGap;
1010 final int heightGap = mHeightGap;
Winson Chungaafa03c2010-06-11 17:34:16 -07001011
1012 final int hStartPadding = getLeftPadding();
1013 final int vStartPadding = getTopPadding();
1014
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001015 int width = cellHSpan * cellWidth + ((cellHSpan - 1) * widthGap);
1016 int height = cellVSpan * cellHeight + ((cellVSpan - 1) * heightGap);
1017
1018 int x = hStartPadding + cellX * (cellWidth + widthGap);
1019 int y = vStartPadding + cellY * (cellHeight + heightGap);
Winson Chungaafa03c2010-06-11 17:34:16 -07001020
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001021 resultRect.set(x, y, x + width, y + height);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001022 }
Winson Chungaafa03c2010-06-11 17:34:16 -07001023
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001024 /**
Winson Chungaafa03c2010-06-11 17:34:16 -07001025 * Computes the required horizontal and vertical cell spans to always
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001026 * fit the given rectangle.
Winson Chungaafa03c2010-06-11 17:34:16 -07001027 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001028 * @param width Width in pixels
1029 * @param height Height in pixels
Patrick Dubroy8f86ddc2010-07-16 13:55:32 -07001030 * @param result An array of length 2 in which to store the result (may be null).
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001031 */
Patrick Dubroy8f86ddc2010-07-16 13:55:32 -07001032 public int[] rectToCell(int width, int height, int[] result) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001033 // Always assume we're working with the smallest span to make sure we
1034 // reserve enough space in both orientations.
Joe Onorato79e56262009-09-21 15:23:04 -04001035 final Resources resources = getResources();
1036 int actualWidth = resources.getDimensionPixelSize(R.dimen.workspace_cell_width);
1037 int actualHeight = resources.getDimensionPixelSize(R.dimen.workspace_cell_height);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001038 int smallerSize = Math.min(actualWidth, actualHeight);
Joe Onorato79e56262009-09-21 15:23:04 -04001039
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001040 // Always round up to next largest cell
1041 int spanX = (width + smallerSize) / smallerSize;
1042 int spanY = (height + smallerSize) / smallerSize;
Joe Onorato79e56262009-09-21 15:23:04 -04001043
Patrick Dubroy8f86ddc2010-07-16 13:55:32 -07001044 if (result == null) {
1045 return new int[] { spanX, spanY };
1046 }
1047 result[0] = spanX;
1048 result[1] = spanY;
1049 return result;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001050 }
1051
1052 /**
1053 * Find the first vacant cell, if there is one.
1054 *
1055 * @param vacant Holds the x and y coordinate of the vacant cell
1056 * @param spanX Horizontal cell span.
1057 * @param spanY Vertical cell span.
Winson Chungaafa03c2010-06-11 17:34:16 -07001058 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001059 * @return True if a vacant cell was found
1060 */
1061 public boolean getVacantCell(int[] vacant, int spanX, int spanY) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001062
Michael Jurka0280c3b2010-09-17 15:00:07 -07001063 return findVacantCell(vacant, spanX, spanY, mCountX, mCountY, mOccupied);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001064 }
1065
1066 static boolean findVacantCell(int[] vacant, int spanX, int spanY,
1067 int xCount, int yCount, boolean[][] occupied) {
1068
1069 for (int x = 0; x < xCount; x++) {
1070 for (int y = 0; y < yCount; y++) {
1071 boolean available = !occupied[x][y];
1072out: for (int i = x; i < x + spanX - 1 && x < xCount; i++) {
1073 for (int j = y; j < y + spanY - 1 && y < yCount; j++) {
1074 available = available && !occupied[i][j];
1075 if (!available) break out;
1076 }
1077 }
1078
1079 if (available) {
1080 vacant[0] = x;
1081 vacant[1] = y;
1082 return true;
1083 }
1084 }
1085 }
1086
1087 return false;
1088 }
1089
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001090 /**
1091 * Update the array of occupied cells (mOccupied), and return a flattened copy of the array.
1092 */
1093 boolean[] getOccupiedCellsFlattened() {
Adam Cohend22015c2010-07-26 22:02:18 -07001094 final int xCount = mCountX;
1095 final int yCount = mCountY;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001096 final boolean[][] occupied = mOccupied;
1097
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001098 final boolean[] flat = new boolean[xCount * yCount];
1099 for (int y = 0; y < yCount; y++) {
1100 for (int x = 0; x < xCount; x++) {
1101 flat[y * xCount + x] = occupied[x][y];
1102 }
1103 }
1104
1105 return flat;
1106 }
1107
Michael Jurka0280c3b2010-09-17 15:00:07 -07001108 private void clearOccupiedCells() {
1109 for (int x = 0; x < mCountX; x++) {
1110 for (int y = 0; y < mCountY; y++) {
1111 mOccupied[x][y] = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001112 }
1113 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001114 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001115
Michael Jurka0280c3b2010-09-17 15:00:07 -07001116 public void onMove(View view, int newCellX, int newCellY) {
1117 LayoutParams lp = (LayoutParams) view.getLayoutParams();
1118 markCellsAsUnoccupiedForView(view);
1119 markCellsForView(newCellX, newCellY, lp.cellHSpan, lp.cellVSpan, true);
1120 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001121
Michael Jurka0280c3b2010-09-17 15:00:07 -07001122 private void markCellsAsOccupiedForView(View view) {
Michael Jurkac6ee42e2010-09-30 12:04:50 -07001123 if (view == null || view.getParent() != this) return;
Michael Jurka0280c3b2010-09-17 15:00:07 -07001124 LayoutParams lp = (LayoutParams) view.getLayoutParams();
1125 markCellsForView(lp.cellX, lp.cellY, lp.cellHSpan, lp.cellVSpan, true);
1126 }
1127
1128 private void markCellsAsUnoccupiedForView(View view) {
Michael Jurkac6ee42e2010-09-30 12:04:50 -07001129 if (view == null || view.getParent() != this) return;
Michael Jurka0280c3b2010-09-17 15:00:07 -07001130 LayoutParams lp = (LayoutParams) view.getLayoutParams();
1131 markCellsForView(lp.cellX, lp.cellY, lp.cellHSpan, lp.cellVSpan, false);
1132 }
1133
1134 private void markCellsForView(int cellX, int cellY, int spanX, int spanY, boolean value) {
1135 for (int x = cellX; x < cellX + spanX && x < mCountX; x++) {
1136 for (int y = cellY; y < cellY + spanY && y < mCountY; y++) {
1137 mOccupied[x][y] = value;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001138 }
1139 }
1140 }
1141
1142 @Override
1143 public ViewGroup.LayoutParams generateLayoutParams(AttributeSet attrs) {
1144 return new CellLayout.LayoutParams(getContext(), attrs);
1145 }
1146
1147 @Override
1148 protected boolean checkLayoutParams(ViewGroup.LayoutParams p) {
1149 return p instanceof CellLayout.LayoutParams;
1150 }
1151
1152 @Override
1153 protected ViewGroup.LayoutParams generateLayoutParams(ViewGroup.LayoutParams p) {
1154 return new CellLayout.LayoutParams(p);
1155 }
1156
Winson Chungaafa03c2010-06-11 17:34:16 -07001157 public static class CellLayoutAnimationController extends LayoutAnimationController {
1158 public CellLayoutAnimationController(Animation animation, float delay) {
1159 super(animation, delay);
1160 }
1161
1162 @Override
1163 protected long getDelayForView(View view) {
1164 return (int) (Math.random() * 150);
1165 }
1166 }
1167
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001168 public static class LayoutParams extends ViewGroup.MarginLayoutParams {
1169 /**
1170 * Horizontal location of the item in the grid.
1171 */
1172 @ViewDebug.ExportedProperty
1173 public int cellX;
1174
1175 /**
1176 * Vertical location of the item in the grid.
1177 */
1178 @ViewDebug.ExportedProperty
1179 public int cellY;
1180
1181 /**
1182 * Number of cells spanned horizontally by the item.
1183 */
1184 @ViewDebug.ExportedProperty
1185 public int cellHSpan;
1186
1187 /**
1188 * Number of cells spanned vertically by the item.
1189 */
1190 @ViewDebug.ExportedProperty
1191 public int cellVSpan;
Winson Chungaafa03c2010-06-11 17:34:16 -07001192
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001193 /**
1194 * Is this item currently being dragged
1195 */
1196 public boolean isDragging;
1197
1198 // X coordinate of the view in the layout.
1199 @ViewDebug.ExportedProperty
1200 int x;
1201 // Y coordinate of the view in the layout.
1202 @ViewDebug.ExportedProperty
1203 int y;
1204
Romain Guy84f296c2009-11-04 15:00:44 -08001205 boolean dropped;
Romain Guyfcb9e712009-10-02 16:06:52 -07001206
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001207 public LayoutParams(Context c, AttributeSet attrs) {
1208 super(c, attrs);
1209 cellHSpan = 1;
1210 cellVSpan = 1;
1211 }
1212
1213 public LayoutParams(ViewGroup.LayoutParams source) {
1214 super(source);
1215 cellHSpan = 1;
1216 cellVSpan = 1;
1217 }
Winson Chungaafa03c2010-06-11 17:34:16 -07001218
1219 public LayoutParams(LayoutParams source) {
1220 super(source);
1221 this.cellX = source.cellX;
1222 this.cellY = source.cellY;
1223 this.cellHSpan = source.cellHSpan;
1224 this.cellVSpan = source.cellVSpan;
1225 }
1226
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001227 public LayoutParams(int cellX, int cellY, int cellHSpan, int cellVSpan) {
Romain Guy8f19cdd2010-01-08 15:07:00 -08001228 super(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001229 this.cellX = cellX;
1230 this.cellY = cellY;
1231 this.cellHSpan = cellHSpan;
1232 this.cellVSpan = cellVSpan;
1233 }
1234
1235 public void setup(int cellWidth, int cellHeight, int widthGap, int heightGap,
1236 int hStartPadding, int vStartPadding) {
Winson Chungaafa03c2010-06-11 17:34:16 -07001237
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001238 final int myCellHSpan = cellHSpan;
1239 final int myCellVSpan = cellVSpan;
1240 final int myCellX = cellX;
1241 final int myCellY = cellY;
Winson Chungaafa03c2010-06-11 17:34:16 -07001242
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001243 width = myCellHSpan * cellWidth + ((myCellHSpan - 1) * widthGap) -
1244 leftMargin - rightMargin;
1245 height = myCellVSpan * cellHeight + ((myCellVSpan - 1) * heightGap) -
1246 topMargin - bottomMargin;
1247
1248 x = hStartPadding + myCellX * (cellWidth + widthGap) + leftMargin;
1249 y = vStartPadding + myCellY * (cellHeight + heightGap) + topMargin;
1250 }
Winson Chungaafa03c2010-06-11 17:34:16 -07001251
1252 public String toString() {
1253 return "(" + this.cellX + ", " + this.cellY + ")";
1254 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001255 }
1256
Michael Jurka0280c3b2010-09-17 15:00:07 -07001257 // This class stores info for two purposes:
1258 // 1. When dragging items (mDragInfo in Workspace), we store the View, its cellX & cellY,
1259 // its spanX, spanY, and the screen it is on
1260 // 2. When long clicking on an empty cell in a CellLayout, we save information about the
1261 // cellX and cellY coordinates and which page was clicked. We then set this as a tag on
1262 // the CellLayout that was long clicked
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001263 static final class CellInfo implements ContextMenu.ContextMenuInfo {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001264 View cell;
Michael Jurkaa63c4522010-08-19 13:52:27 -07001265 int cellX = -1;
1266 int cellY = -1;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001267 int spanX;
1268 int spanY;
1269 int screen;
1270 boolean valid;
1271
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001272 @Override
1273 public String toString() {
Winson Chungaafa03c2010-06-11 17:34:16 -07001274 return "Cell[view=" + (cell == null ? "null" : cell.getClass())
1275 + ", x=" + cellX + ", y=" + cellY + "]";
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001276 }
1277 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001278}