blob: 943d61b5dcdfb8d2955bcb4f14a5bfce9191709c [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 Cohen9415d872010-09-13 14:49:43 -070045public class CellLayout extends ViewGroup implements Dimmable {
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();
Adam Cohen9415d872010-09-13 14:49:43 -070078
Michael Jurka5f1c5092010-09-03 14:15:02 -070079 private Drawable mBackground;
Adam Cohen9415d872010-09-13 14:49:43 -070080 private Drawable mBackgroundMini;
81 private Drawable mBackgroundMiniHover;
Michael Jurkaa63c4522010-08-19 13:52:27 -070082 // If we're actively dragging something over this screen and it's small,
83 // mHover is true
84 private boolean mHover = false;
Michael Jurkadee05892010-07-27 10:01:56 -070085
Patrick Dubroy6569f2c2010-07-12 14:25:18 -070086 private final RectF mDragRect = new RectF();
Patrick Dubroyde7658b2010-09-27 11:15:43 -070087 private final Point mDragCenter = new Point();
Patrick Dubroy6569f2c2010-07-12 14:25:18 -070088
Patrick Dubroy6569f2c2010-07-12 14:25:18 -070089 private Drawable mDragRectDrawable;
90
Patrick Dubroyde7658b2010-09-27 11:15:43 -070091 private Drawable mCrosshairsDrawable = null;
92 private ValueAnimator mCrosshairsAnimator = null;
93 private float mCrosshairsVisibility = 0.0f;
94
Patrick Dubroy6569f2c2010-07-12 14:25:18 -070095 // When a drag operation is in progress, holds the nearest cell to the touch point
96 private final int[] mDragCell = new int[2];
The Android Open Source Project31dd5032009-03-03 19:32:27 -080097
Winson Chungaafa03c2010-06-11 17:34:16 -070098 private final WallpaperManager mWallpaperManager;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080099
100 public CellLayout(Context context) {
101 this(context, null);
102 }
103
104 public CellLayout(Context context, AttributeSet attrs) {
105 this(context, attrs, 0);
106 }
107
108 public CellLayout(Context context, AttributeSet attrs, int defStyle) {
109 super(context, attrs, defStyle);
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700110
111 // A ViewGroup usually does not draw, but CellLayout needs to draw a rectangle to show
112 // the user where a dragged item will land when dropped.
113 setWillNotDraw(false);
Michael Jurkaa63c4522010-08-19 13:52:27 -0700114
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800115 TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.CellLayout, defStyle, 0);
116
117 mCellWidth = a.getDimensionPixelSize(R.styleable.CellLayout_cellWidth, 10);
118 mCellHeight = a.getDimensionPixelSize(R.styleable.CellLayout_cellHeight, 10);
Winson Chungaafa03c2010-06-11 17:34:16 -0700119
Adam Cohend22015c2010-07-26 22:02:18 -0700120 mLeftPadding =
121 a.getDimensionPixelSize(R.styleable.CellLayout_xAxisStartPadding, 10);
122 mRightPadding =
123 a.getDimensionPixelSize(R.styleable.CellLayout_xAxisEndPadding, 10);
124 mTopPadding =
125 a.getDimensionPixelSize(R.styleable.CellLayout_yAxisStartPadding, 10);
126 mBottomPadding =
127 a.getDimensionPixelSize(R.styleable.CellLayout_yAxisEndPadding, 10);
Winson Chungaafa03c2010-06-11 17:34:16 -0700128
Adam Cohend22015c2010-07-26 22:02:18 -0700129 mCountX = LauncherModel.getCellCountX();
130 mCountY = LauncherModel.getCellCountY();
Michael Jurka0280c3b2010-09-17 15:00:07 -0700131 mOccupied = new boolean[mCountX][mCountY];
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800132
133 a.recycle();
134
135 setAlwaysDrawnWithCacheEnabled(false);
136
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700137 mWallpaperManager = WallpaperManager.getInstance(context);
138
139 if (LauncherApplication.isScreenXLarge()) {
140 final Resources res = getResources();
141
142 mBackgroundMini = res.getDrawable(R.drawable.mini_home_screen_bg);
143 mBackgroundMini.setFilterBitmap(true);
144 mBackground = res.getDrawable(R.drawable.home_screen_bg);
145 mBackground.setFilterBitmap(true);
146 mBackgroundMiniHover = res.getDrawable(R.drawable.mini_home_screen_bg_hover);
147 mBackgroundMiniHover.setFilterBitmap(true);
148
149 mDragRectDrawable = res.getDrawable(R.drawable.rounded_rect_green);
150 mCrosshairsDrawable = res.getDrawable(R.drawable.gardening_crosshairs);
151
152 // Set up the animation for fading the crosshairs in and out
153 int animDuration = res.getInteger(R.integer.config_crosshairsFadeInTime);
154 mCrosshairsAnimator = new ValueAnimator<Float>(animDuration);
155 mCrosshairsAnimator.addUpdateListener(new AnimatorUpdateListener() {
156 public void onAnimationUpdate(ValueAnimator animation) {
157 mCrosshairsVisibility = ((Float) animation.getAnimatedValue()).floatValue();
158 CellLayout.this.invalidate();
159 }
160 });
161 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800162 }
163
Michael Jurkaa63c4522010-08-19 13:52:27 -0700164 public void setHover(boolean value) {
165 if (mHover != value) {
166 invalidate();
167 }
168 mHover = value;
169 }
170
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700171 private void animateCrosshairsTo(float value) {
172 final ValueAnimator anim = mCrosshairsAnimator;
173 long fullDuration = getResources().getInteger(R.integer.config_crosshairsFadeInTime);
174 anim.setDuration(fullDuration - anim.getCurrentPlayTime());
175 anim.setValues(mCrosshairsVisibility, value);
176 anim.cancel();
177 anim.start();
178 }
179
Jeff Sharkey83f111d2009-04-20 21:03:13 -0700180 @Override
Romain Guya6abce82009-11-10 02:54:41 -0800181 public void dispatchDraw(Canvas canvas) {
Michael Jurka5f1c5092010-09-03 14:15:02 -0700182 if (mBackgroundAlpha > 0.0f) {
Adam Cohen9415d872010-09-13 14:49:43 -0700183 Drawable bg;
184 if (mHover && getScaleX() < 0.5f) {
185 bg = mBackgroundMiniHover;
186 } else if (getScaleX() < 0.5f) {
187 bg = mBackgroundMini;
188 } else {
189 bg = mBackground;
190 }
Michael Jurka5f1c5092010-09-03 14:15:02 -0700191 bg.setAlpha((int) (mBackgroundAlpha * 255));
Michael Jurkaa63c4522010-08-19 13:52:27 -0700192 bg.draw(canvas);
193 }
Romain Guya6abce82009-11-10 02:54:41 -0800194 super.dispatchDraw(canvas);
195 }
196
197 @Override
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700198 protected void onDraw(Canvas canvas) {
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700199 if (mCrosshairsVisibility > 0.0f) {
200 final int countX = mCountX;
201 final int countY = mCountY;
202
203 if (!mDragRect.isEmpty()) {
204 mDragRectDrawable.setBounds(
205 (int)mDragRect.left,
206 (int)mDragRect.top,
207 (int)mDragRect.right,
208 (int)mDragRect.bottom);
209 mDragRectDrawable.setAlpha((int) (mCrosshairsVisibility * 255));
210 mDragRectDrawable.draw(canvas);
211 }
212
213 final float MAX_ALPHA = 0.4f;
214 final int MAX_VISIBLE_DISTANCE = 600;
215 final float DISTANCE_MULTIPLIER = 0.002f;
216
217 final Drawable d = mCrosshairsDrawable;
218 final int width = d.getIntrinsicWidth();
219 final int height = d.getIntrinsicHeight();
220
221 int x = getLeftPadding() - (mWidthGap / 2) - (width / 2);
222 for (int col = 0; col <= countX; col++) {
223 int y = getTopPadding() - (mHeightGap / 2) - (height / 2);
224 for (int row = 0; row <= countY; row++) {
225 mTmpPointF.set(x - mDragCenter.x, y - mDragCenter.y);
226 float dist = mTmpPointF.length();
227 // Crosshairs further from the drag point are more faint
228 float alpha = Math.min(MAX_ALPHA,
229 DISTANCE_MULTIPLIER * (MAX_VISIBLE_DISTANCE - dist));
230 if (alpha > 0.0f) {
231 d.setBounds(x, y, x + width, y + height);
232 d.setAlpha((int) (alpha * 255 * mCrosshairsVisibility));
233 d.draw(canvas);
234 }
235 y += mCellHeight + mHeightGap;
236 }
237 x += mCellWidth + mWidthGap;
238 }
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700239 }
240 }
241
Adam Cohen9415d872010-09-13 14:49:43 -0700242 public void setDimmableProgress(float progress) {
243 for (int i = 0; i < getChildCount(); i++) {
244 Dimmable d = (Dimmable) getChildAt(i);
245 d.setDimmableProgress(progress);
246 }
247 }
248
249 public float getDimmableProgress() {
250 if (getChildCount() > 0) {
251 return ((Dimmable) getChildAt(0)).getDimmableProgress();
252 }
253 return 0.0f;
254 }
255
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700256 @Override
Jeff Sharkey83f111d2009-04-20 21:03:13 -0700257 public void cancelLongPress() {
258 super.cancelLongPress();
259
260 // Cancel long press for all children
261 final int count = getChildCount();
262 for (int i = 0; i < count; i++) {
263 final View child = getChildAt(i);
264 child.cancelLongPress();
265 }
266 }
267
Michael Jurkadee05892010-07-27 10:01:56 -0700268 public void setOnInterceptTouchListener(View.OnTouchListener listener) {
269 mInterceptTouchListener = listener;
270 }
271
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800272 int getCountX() {
Adam Cohend22015c2010-07-26 22:02:18 -0700273 return mCountX;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800274 }
275
276 int getCountY() {
Adam Cohend22015c2010-07-26 22:02:18 -0700277 return mCountY;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800278 }
279
Winson Chungaafa03c2010-06-11 17:34:16 -0700280 public boolean addViewToCellLayout(View child, int index, int childId, LayoutParams params) {
281 final LayoutParams lp = params;
282
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800283 // Generate an id for each view, this assumes we have at most 256x256 cells
284 // per workspace screen
Adam Cohend22015c2010-07-26 22:02:18 -0700285 if (lp.cellX >= 0 && lp.cellX <= mCountX - 1 && lp.cellY >= 0 && lp.cellY <= mCountY - 1) {
Winson Chungaafa03c2010-06-11 17:34:16 -0700286 // If the horizontal or vertical span is set to -1, it is taken to
287 // mean that it spans the extent of the CellLayout
Adam Cohend22015c2010-07-26 22:02:18 -0700288 if (lp.cellHSpan < 0) lp.cellHSpan = mCountX;
289 if (lp.cellVSpan < 0) lp.cellVSpan = mCountY;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800290
Winson Chungaafa03c2010-06-11 17:34:16 -0700291 child.setId(childId);
292
Michael Jurkadee05892010-07-27 10:01:56 -0700293 // We might be in the middle or end of shrinking/fading to a dimmed view
294 // Make sure this view's alpha is set the same as all the rest of the views
Michael Jurka5f1c5092010-09-03 14:15:02 -0700295 child.setAlpha(getAlpha());
Winson Chungaafa03c2010-06-11 17:34:16 -0700296 addView(child, index, lp);
Michael Jurkadee05892010-07-27 10:01:56 -0700297
Michael Jurka0280c3b2010-09-17 15:00:07 -0700298 markCellsAsOccupiedForView(child);
299
Winson Chungaafa03c2010-06-11 17:34:16 -0700300 return true;
301 }
302 return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800303 }
304
305 @Override
Michael Jurka0280c3b2010-09-17 15:00:07 -0700306 public void removeAllViews() {
307 clearOccupiedCells();
308 }
309
310 @Override
311 public void removeAllViewsInLayout() {
312 clearOccupiedCells();
313 }
314
315 @Override
316 public void removeView(View view) {
317 markCellsAsUnoccupiedForView(view);
318 super.removeView(view);
319 }
320
321 @Override
322 public void removeViewAt(int index) {
323 markCellsAsUnoccupiedForView(getChildAt(index));
324 super.removeViewAt(index);
325 }
326
327 @Override
328 public void removeViewInLayout(View view) {
329 markCellsAsUnoccupiedForView(view);
330 super.removeViewInLayout(view);
331 }
332
333 @Override
334 public void removeViews(int start, int count) {
335 for (int i = start; i < start + count; i++) {
336 markCellsAsUnoccupiedForView(getChildAt(i));
337 }
338 super.removeViews(start, count);
339 }
340
341 @Override
342 public void removeViewsInLayout(int start, int count) {
343 for (int i = start; i < start + count; i++) {
344 markCellsAsUnoccupiedForView(getChildAt(i));
345 }
346 super.removeViewsInLayout(start, count);
347 }
348
349 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800350 public void requestChildFocus(View child, View focused) {
351 super.requestChildFocus(child, focused);
352 if (child != null) {
353 Rect r = new Rect();
354 child.getDrawingRect(r);
355 requestRectangleOnScreen(r);
356 }
357 }
358
359 @Override
360 protected void onAttachedToWindow() {
361 super.onAttachedToWindow();
362 mCellInfo.screen = ((ViewGroup) getParent()).indexOfChild(this);
363 }
364
Michael Jurkaaf442092010-06-10 17:01:57 -0700365 public void setTagToCellInfoForPoint(int touchX, int touchY) {
366 final CellInfo cellInfo = mCellInfo;
367 final Rect frame = mRect;
368 final int x = touchX + mScrollX;
369 final int y = touchY + mScrollY;
370 final int count = getChildCount();
371
372 boolean found = false;
373 for (int i = count - 1; i >= 0; i--) {
374 final View child = getChildAt(i);
375
376 if ((child.getVisibility()) == VISIBLE || child.getAnimation() != null) {
377 child.getHitRect(frame);
378 if (frame.contains(x, y)) {
379 final LayoutParams lp = (LayoutParams) child.getLayoutParams();
380 cellInfo.cell = child;
381 cellInfo.cellX = lp.cellX;
382 cellInfo.cellY = lp.cellY;
383 cellInfo.spanX = lp.cellHSpan;
384 cellInfo.spanY = lp.cellVSpan;
385 cellInfo.valid = true;
386 found = true;
Michael Jurkaaf442092010-06-10 17:01:57 -0700387 break;
388 }
389 }
390 }
Winson Chungaafa03c2010-06-11 17:34:16 -0700391
Michael Jurkaaf442092010-06-10 17:01:57 -0700392 if (!found) {
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700393 final int cellXY[] = mTmpCellXY;
Michael Jurkaaf442092010-06-10 17:01:57 -0700394 pointToCellExact(x, y, cellXY);
395
Michael Jurkaaf442092010-06-10 17:01:57 -0700396 cellInfo.cell = null;
397 cellInfo.cellX = cellXY[0];
398 cellInfo.cellY = cellXY[1];
399 cellInfo.spanX = 1;
400 cellInfo.spanY = 1;
Michael Jurka0280c3b2010-09-17 15:00:07 -0700401 cellInfo.valid = cellXY[0] >= 0 && cellXY[1] >= 0 && cellXY[0] < mCountX &&
402 cellXY[1] < mCountY && !mOccupied[cellXY[0]][cellXY[1]];
Michael Jurkaaf442092010-06-10 17:01:57 -0700403 }
404 setTag(cellInfo);
405 }
406
Winson Chungaafa03c2010-06-11 17:34:16 -0700407
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800408 @Override
409 public boolean onInterceptTouchEvent(MotionEvent ev) {
Michael Jurkadee05892010-07-27 10:01:56 -0700410 if (mInterceptTouchListener != null && mInterceptTouchListener.onTouch(this, ev)) {
411 return true;
412 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800413 final int action = ev.getAction();
414 final CellInfo cellInfo = mCellInfo;
415
416 if (action == MotionEvent.ACTION_DOWN) {
Michael Jurkaaf442092010-06-10 17:01:57 -0700417 setTagToCellInfoForPoint((int) ev.getX(), (int) ev.getY());
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800418 } else if (action == MotionEvent.ACTION_UP) {
419 cellInfo.cell = null;
420 cellInfo.cellX = -1;
421 cellInfo.cellY = -1;
422 cellInfo.spanX = 0;
423 cellInfo.spanY = 0;
424 cellInfo.valid = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800425 setTag(cellInfo);
426 }
427
428 return false;
429 }
430
431 @Override
432 public CellInfo getTag() {
Michael Jurka0280c3b2010-09-17 15:00:07 -0700433 return (CellInfo) super.getTag();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800434 }
435
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700436 /**
437 * Check if the row 'y' is empty from columns 'left' to 'right', inclusive.
438 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800439 private static boolean isRowEmpty(int y, int left, int right, boolean[][] occupied) {
440 for (int x = left; x <= right; x++) {
441 if (occupied[x][y]) {
442 return false;
443 }
444 }
445 return true;
446 }
447
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800448 /**
Winson Chungaafa03c2010-06-11 17:34:16 -0700449 * Given a point, return the cell that strictly encloses that point
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800450 * @param x X coordinate of the point
451 * @param y Y coordinate of the point
452 * @param result Array of 2 ints to hold the x and y coordinate of the cell
453 */
454 void pointToCellExact(int x, int y, int[] result) {
Winson Chungaafa03c2010-06-11 17:34:16 -0700455 final int hStartPadding = getLeftPadding();
456 final int vStartPadding = getTopPadding();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800457
458 result[0] = (x - hStartPadding) / (mCellWidth + mWidthGap);
459 result[1] = (y - vStartPadding) / (mCellHeight + mHeightGap);
460
Adam Cohend22015c2010-07-26 22:02:18 -0700461 final int xAxis = mCountX;
462 final int yAxis = mCountY;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800463
464 if (result[0] < 0) result[0] = 0;
465 if (result[0] >= xAxis) result[0] = xAxis - 1;
466 if (result[1] < 0) result[1] = 0;
467 if (result[1] >= yAxis) result[1] = yAxis - 1;
468 }
Winson Chungaafa03c2010-06-11 17:34:16 -0700469
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800470 /**
471 * Given a point, return the cell that most closely encloses that point
472 * @param x X coordinate of the point
473 * @param y Y coordinate of the point
474 * @param result Array of 2 ints to hold the x and y coordinate of the cell
475 */
476 void pointToCellRounded(int x, int y, int[] result) {
477 pointToCellExact(x + (mCellWidth / 2), y + (mCellHeight / 2), result);
478 }
479
480 /**
481 * Given a cell coordinate, return the point that represents the upper left corner of that cell
Winson Chungaafa03c2010-06-11 17:34:16 -0700482 *
483 * @param cellX X coordinate of the cell
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800484 * @param cellY Y coordinate of the cell
Winson Chungaafa03c2010-06-11 17:34:16 -0700485 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800486 * @param result Array of 2 ints to hold the x and y coordinate of the point
487 */
488 void cellToPoint(int cellX, int cellY, int[] result) {
Winson Chungaafa03c2010-06-11 17:34:16 -0700489 final int hStartPadding = getLeftPadding();
490 final int vStartPadding = getTopPadding();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800491
492 result[0] = hStartPadding + cellX * (mCellWidth + mWidthGap);
493 result[1] = vStartPadding + cellY * (mCellHeight + mHeightGap);
494 }
495
Romain Guy84f296c2009-11-04 15:00:44 -0800496 int getCellWidth() {
497 return mCellWidth;
498 }
499
500 int getCellHeight() {
501 return mCellHeight;
502 }
503
Romain Guy1a304a12009-11-10 00:02:32 -0800504 int getLeftPadding() {
Winson Chungaafa03c2010-06-11 17:34:16 -0700505 return mLeftPadding;
Romain Guy1a304a12009-11-10 00:02:32 -0800506 }
507
508 int getTopPadding() {
Winson Chungaafa03c2010-06-11 17:34:16 -0700509 return mTopPadding;
Romain Guy1a304a12009-11-10 00:02:32 -0800510 }
511
512 int getRightPadding() {
Winson Chungaafa03c2010-06-11 17:34:16 -0700513 return mRightPadding;
Romain Guy1a304a12009-11-10 00:02:32 -0800514 }
515
516 int getBottomPadding() {
Winson Chungaafa03c2010-06-11 17:34:16 -0700517 return mBottomPadding;
Romain Guy1a304a12009-11-10 00:02:32 -0800518 }
519
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800520 @Override
521 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
522 // TODO: currently ignoring padding
Winson Chungaafa03c2010-06-11 17:34:16 -0700523
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800524 int widthSpecMode = MeasureSpec.getMode(widthMeasureSpec);
Winson Chungaafa03c2010-06-11 17:34:16 -0700525 int widthSpecSize = MeasureSpec.getSize(widthMeasureSpec);
526
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800527 int heightSpecMode = MeasureSpec.getMode(heightMeasureSpec);
528 int heightSpecSize = MeasureSpec.getSize(heightMeasureSpec);
Winson Chungaafa03c2010-06-11 17:34:16 -0700529
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800530 if (widthSpecMode == MeasureSpec.UNSPECIFIED || heightSpecMode == MeasureSpec.UNSPECIFIED) {
531 throw new RuntimeException("CellLayout cannot have UNSPECIFIED dimensions");
532 }
533
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800534 final int cellWidth = mCellWidth;
535 final int cellHeight = mCellHeight;
536
Adam Cohend22015c2010-07-26 22:02:18 -0700537 int numWidthGaps = mCountX - 1;
538 int numHeightGaps = mCountY - 1;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800539
Michael Jurka0280c3b2010-09-17 15:00:07 -0700540 int vSpaceLeft = heightSpecSize - mTopPadding - mBottomPadding - (cellHeight * mCountY);
Adam Cohend22015c2010-07-26 22:02:18 -0700541 mHeightGap = vSpaceLeft / numHeightGaps;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800542
Michael Jurka0280c3b2010-09-17 15:00:07 -0700543 int hSpaceLeft = widthSpecSize - mLeftPadding - mRightPadding - (cellWidth * mCountX);
Adam Cohend22015c2010-07-26 22:02:18 -0700544 mWidthGap = hSpaceLeft / numWidthGaps;
Winson Chungaafa03c2010-06-11 17:34:16 -0700545
Michael Jurka5f1c5092010-09-03 14:15:02 -0700546 // center it around the min gaps
547 int minGap = Math.min(mWidthGap, mHeightGap);
548 mWidthGap = mHeightGap = minGap;
549
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800550 int count = getChildCount();
551
552 for (int i = 0; i < count; i++) {
553 View child = getChildAt(i);
554 LayoutParams lp = (LayoutParams) child.getLayoutParams();
Winson Chungaafa03c2010-06-11 17:34:16 -0700555 lp.setup(cellWidth, cellHeight, mWidthGap, mHeightGap,
556 mLeftPadding, mTopPadding);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800557
Michael Jurka0280c3b2010-09-17 15:00:07 -0700558 int childWidthMeasureSpec = MeasureSpec.makeMeasureSpec(lp.width, MeasureSpec.EXACTLY);
Winson Chungaafa03c2010-06-11 17:34:16 -0700559 int childheightMeasureSpec = MeasureSpec.makeMeasureSpec(lp.height,
560 MeasureSpec.EXACTLY);
561
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800562 child.measure(childWidthMeasureSpec, childheightMeasureSpec);
563 }
Michael Jurka5f1c5092010-09-03 14:15:02 -0700564 if (widthSpecMode == MeasureSpec.AT_MOST) {
565 int newWidth = mLeftPadding + mRightPadding + (mCountX * cellWidth) +
566 ((mCountX - 1) * minGap);
567 int newHeight = mTopPadding + mBottomPadding + (mCountY * cellHeight) +
568 ((mCountY - 1) * minGap);
569 setMeasuredDimension(newWidth, newHeight);
570 } else if (widthSpecMode == MeasureSpec.EXACTLY) {
571 setMeasuredDimension(widthSpecSize, heightSpecSize);
572 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800573 }
574
575 @Override
Michael Jurka28750fb2010-09-24 17:43:49 -0700576 protected void onLayout(boolean changed, int l, int t, int r, int b) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800577 int count = getChildCount();
578
579 for (int i = 0; i < count; i++) {
580 View child = getChildAt(i);
581 if (child.getVisibility() != GONE) {
582
583 CellLayout.LayoutParams lp = (CellLayout.LayoutParams) child.getLayoutParams();
584
585 int childLeft = lp.x;
586 int childTop = lp.y;
587 child.layout(childLeft, childTop, childLeft + lp.width, childTop + lp.height);
Romain Guy84f296c2009-11-04 15:00:44 -0800588
589 if (lp.dropped) {
590 lp.dropped = false;
591
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700592 final int[] cellXY = mTmpCellXY;
Romain Guy06762ab2010-01-25 16:51:08 -0800593 getLocationOnScreen(cellXY);
Romain Guy84f296c2009-11-04 15:00:44 -0800594 mWallpaperManager.sendWallpaperCommand(getWindowToken(), "android.home.drop",
Romain Guy06762ab2010-01-25 16:51:08 -0800595 cellXY[0] + childLeft + lp.width / 2,
596 cellXY[1] + childTop + lp.height / 2, 0, null);
Romain Guy84f296c2009-11-04 15:00:44 -0800597 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800598 }
599 }
600 }
601
602 @Override
Michael Jurkadee05892010-07-27 10:01:56 -0700603 protected void onSizeChanged(int w, int h, int oldw, int oldh) {
604 super.onSizeChanged(w, h, oldw, oldh);
Michael Jurka5f1c5092010-09-03 14:15:02 -0700605 mBackgroundLayoutRect.set(0, 0, w, h);
606 if (mBackground != null) {
607 mBackground.setBounds(mBackgroundLayoutRect);
Michael Jurkaa63c4522010-08-19 13:52:27 -0700608 }
Adam Cohen9415d872010-09-13 14:49:43 -0700609 if (mBackgroundMiniHover != null) {
610 mBackgroundMiniHover.setBounds(mBackgroundLayoutRect);
611 }
612 if (mBackgroundMini != null) {
613 mBackgroundMini.setBounds(mBackgroundLayoutRect);
Michael Jurkaa63c4522010-08-19 13:52:27 -0700614 }
Michael Jurkadee05892010-07-27 10:01:56 -0700615 }
616
617 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800618 protected void setChildrenDrawingCacheEnabled(boolean enabled) {
619 final int count = getChildCount();
620 for (int i = 0; i < count; i++) {
621 final View view = getChildAt(i);
622 view.setDrawingCacheEnabled(enabled);
623 // Update the drawing caches
Adam Powellfefa0ce2010-05-03 10:23:50 -0700624 view.buildDrawingCache(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800625 }
626 }
627
628 @Override
629 protected void setChildrenDrawnWithCacheEnabled(boolean enabled) {
630 super.setChildrenDrawnWithCacheEnabled(enabled);
631 }
632
Michael Jurka5f1c5092010-09-03 14:15:02 -0700633 public float getBackgroundAlpha() {
634 return mBackgroundAlpha;
Michael Jurkadee05892010-07-27 10:01:56 -0700635 }
636
Michael Jurka5f1c5092010-09-03 14:15:02 -0700637 public void setBackgroundAlpha(float alpha) {
638 mBackgroundAlpha = alpha;
Michael Jurka0142d492010-08-25 17:46:15 -0700639 invalidate();
Michael Jurkadee05892010-07-27 10:01:56 -0700640 }
641
Michael Jurka5f1c5092010-09-03 14:15:02 -0700642 // Need to return true to let the view system know we know how to handle alpha-- this is
643 // because when our children have an alpha of 0.0f, they are still rendering their "dimmed"
644 // versions
645 @Override
646 protected boolean onSetAlpha(int alpha) {
647 return true;
648 }
649
650 public void setAlpha(float alpha) {
651 setChildrenAlpha(alpha);
652 super.setAlpha(alpha);
653 }
654
Michael Jurkadee05892010-07-27 10:01:56 -0700655 private void setChildrenAlpha(float alpha) {
Michael Jurka0142d492010-08-25 17:46:15 -0700656 final int childCount = getChildCount();
657 for (int i = 0; i < childCount; i++) {
Michael Jurkadee05892010-07-27 10:01:56 -0700658 getChildAt(i).setAlpha(alpha);
659 }
660 }
661
Michael Jurka0280c3b2010-09-17 15:00:07 -0700662 private boolean isVacantIgnoring(
663 int originX, int originY, int spanX, int spanY, View ignoreView) {
664 if (ignoreView != null) {
665 markCellsAsUnoccupiedForView(ignoreView);
666 }
Michael Jurka28750fb2010-09-24 17:43:49 -0700667 boolean isVacant = true;
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700668 for (int i = 0; i < spanY; i++) {
669 if (!isRowEmpty(originY + i, originX, originX + spanX - 1, mOccupied)) {
Michael Jurka28750fb2010-09-24 17:43:49 -0700670 isVacant = false;
671 break;
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700672 }
673 }
Michael Jurka0280c3b2010-09-17 15:00:07 -0700674 if (ignoreView != null) {
675 markCellsAsOccupiedForView(ignoreView);
676 }
Michael Jurka28750fb2010-09-24 17:43:49 -0700677 return isVacant;
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700678 }
679
Michael Jurka0280c3b2010-09-17 15:00:07 -0700680 private boolean isVacant(int originX, int originY, int spanX, int spanY) {
681 return isVacantIgnoring(originX, originY, spanX, spanY, null);
682 }
683
Patrick Dubroy440c3602010-07-13 17:50:32 -0700684 public View getChildAt(int x, int y) {
685 final int count = getChildCount();
686 for (int i = 0; i < count; i++) {
687 View child = getChildAt(i);
688 LayoutParams lp = (LayoutParams) child.getLayoutParams();
689
690 if ((lp.cellX <= x) && (x < lp.cellX + lp.cellHSpan) &&
691 (lp.cellY <= y) && (y < lp.cellY + lp.cellHSpan)) {
692 return child;
693 }
694 }
695 return null;
696 }
697
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700698 /**
Patrick Dubroy8f86ddc2010-07-16 13:55:32 -0700699 * Estimate the size that a child with the given dimensions will take in the layout.
700 */
701 void estimateChildSize(int minWidth, int minHeight, int[] result) {
702 // Assuming it's placed at 0, 0, find where the bottom right cell will land
703 rectToCell(minWidth, minHeight, result);
704
705 // Then figure out the rect it will occupy
706 cellToRect(0, 0, result[0], result[1], mRectF);
707 result[0] = (int)mRectF.width();
708 result[1] = (int)mRectF.height();
709 }
710
711 /**
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700712 * Estimate where the top left cell of the dragged item will land if it is dropped.
713 *
714 * @param originX The X value of the top left corner of the item
715 * @param originY The Y value of the top left corner of the item
716 * @param spanX The number of horizontal cells that the item spans
717 * @param spanY The number of vertical cells that the item spans
718 * @param result The estimated drop cell X and Y.
719 */
720 void estimateDropCell(int originX, int originY, int spanX, int spanY, int[] result) {
Adam Cohend22015c2010-07-26 22:02:18 -0700721 final int countX = mCountX;
722 final int countY = mCountY;
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700723
Michael Jurkaa63c4522010-08-19 13:52:27 -0700724 // pointToCellRounded takes the top left of a cell but will pad that with
725 // cellWidth/2 and cellHeight/2 when finding the matching cell
726 pointToCellRounded(originX, originY, result);
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700727
728 // If the item isn't fully on this screen, snap to the edges
729 int rightOverhang = result[0] + spanX - countX;
730 if (rightOverhang > 0) {
731 result[0] -= rightOverhang; // Snap to right
732 }
733 result[0] = Math.max(0, result[0]); // Snap to left
734 int bottomOverhang = result[1] + spanY - countY;
735 if (bottomOverhang > 0) {
736 result[1] -= bottomOverhang; // Snap to bottom
737 }
738 result[1] = Math.max(0, result[1]); // Snap to top
739 }
740
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700741 void visualizeDropLocation(View view, int originX, int originY, int spanX, int spanY) {
742 final int[] nearest = findNearestVacantArea(originX, originY, spanX, spanY, view, mDragCell);
743 mDragCenter.set(originX + (view.getWidth() / 2), originY + (view.getHeight() / 2));
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700744
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700745 if (nearest != null) {
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700746 // Find the top left corner of the rect the object will occupy
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700747 final int[] topLeft = mTmpPoint;
748 cellToPoint(nearest[0], nearest[1], topLeft);
749
750 // Need to copy these, because the next call to cellToPoint will overwrite them
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700751 final int left = topLeft[0];
752 final int top = topLeft[1];
753
754 // Now find the bottom right
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700755 final int[] bottomRight = mTmpPoint;
756 cellToPoint(nearest[0] + spanX - 1, nearest[1] + spanY - 1, bottomRight);
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700757 bottomRight[0] += mCellWidth;
758 bottomRight[1] += mCellHeight;
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700759 mDragRect.set(left, top, bottomRight[0], bottomRight[1]);
760 invalidate();
761 }
762 }
763
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800764 /**
Jeff Sharkey70864282009-04-07 21:08:40 -0700765 * Find a vacant area that will fit the given bounds nearest the requested
766 * cell location. Uses Euclidean distance to score multiple vacant areas.
Winson Chungaafa03c2010-06-11 17:34:16 -0700767 *
Romain Guy51afc022009-05-04 18:03:43 -0700768 * @param pixelX The X location at which you want to search for a vacant area.
769 * @param pixelY The Y location at which you want to search for a vacant area.
Jeff Sharkey70864282009-04-07 21:08:40 -0700770 * @param spanX Horizontal span of the object.
771 * @param spanY Vertical span of the object.
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700772 * @param result Array in which to place the result, or null (in which case a new array will
773 * be allocated)
Jeff Sharkey70864282009-04-07 21:08:40 -0700774 * @return The X, Y cell of a vacant area that can contain this object,
775 * nearest the requested location.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800776 */
Michael Jurka6a1435d2010-09-27 17:35:12 -0700777 int[] findNearestVacantArea(
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700778 int pixelX, int pixelY, int spanX, int spanY, int[] result) {
779 return findNearestVacantArea(pixelX, pixelY, spanX, spanY, null, result);
Michael Jurka6a1435d2010-09-27 17:35:12 -0700780 }
Winson Chungaafa03c2010-06-11 17:34:16 -0700781
Michael Jurka6a1435d2010-09-27 17:35:12 -0700782 /**
783 * Find a vacant area that will fit the given bounds nearest the requested
784 * cell location. Uses Euclidean distance to score multiple vacant areas.
785 *
786 * @param pixelX The X location at which you want to search for a vacant area.
787 * @param pixelY The Y location at which you want to search for a vacant area.
788 * @param spanX Horizontal span of the object.
789 * @param spanY Vertical span of the object.
Michael Jurka6a1435d2010-09-27 17:35:12 -0700790 * @param ignoreView Considers space occupied by this view as unoccupied
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700791 * @param result Previously returned value to possibly recycle.
Michael Jurka6a1435d2010-09-27 17:35:12 -0700792 * @return The X, Y cell of a vacant area that can contain this object,
793 * nearest the requested location.
794 */
795 int[] findNearestVacantArea(
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700796 int pixelX, int pixelY, int spanX, int spanY, View ignoreView, int[] result) {
Michael Jurka6a1435d2010-09-27 17:35:12 -0700797 if (ignoreView != null) {
798 markCellsAsUnoccupiedForView(ignoreView);
799 }
Jeff Sharkey70864282009-04-07 21:08:40 -0700800 // Keep track of best-scoring drop area
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700801 final int[] bestXY = result != null ? result : new int[2];
Jeff Sharkey70864282009-04-07 21:08:40 -0700802 double bestDistance = Double.MAX_VALUE;
Winson Chungaafa03c2010-06-11 17:34:16 -0700803
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700804 final int countX = mCountX;
805 final int countY = mCountY;
806 final boolean[][] occupied = mOccupied;
807
808 for (int x = 0; x < countX - (spanX - 1); x++) {
Michael Jurkac28de512010-08-13 11:27:44 -0700809 inner:
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700810 for (int y = 0; y < countY - (spanY - 1); y++) {
Michael Jurkac28de512010-08-13 11:27:44 -0700811 for (int i = 0; i < spanX; i++) {
812 for (int j = 0; j < spanY; j++) {
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700813 if (occupied[x + i][y + j]) {
Michael Jurkac28de512010-08-13 11:27:44 -0700814 // small optimization: we can skip to below the row we just found
815 // an occupied cell
816 y += j;
817 continue inner;
818 }
819 }
820 }
821 final int[] cellXY = mTmpCellXY;
822 cellToPoint(x, y, cellXY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800823
Michael Jurkac28de512010-08-13 11:27:44 -0700824 double distance = Math.sqrt(Math.pow(cellXY[0] - pixelX, 2)
825 + Math.pow(cellXY[1] - pixelY, 2));
826 if (distance <= bestDistance) {
827 bestDistance = distance;
828 bestXY[0] = x;
829 bestXY[1] = y;
830 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800831 }
832 }
Michael Jurka6a1435d2010-09-27 17:35:12 -0700833 if (ignoreView != null) {
834 markCellsAsOccupiedForView(ignoreView);
835 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800836
Winson Chungaafa03c2010-06-11 17:34:16 -0700837 // Return null if no suitable location found
Jeff Sharkey70864282009-04-07 21:08:40 -0700838 if (bestDistance < Double.MAX_VALUE) {
839 return bestXY;
840 } else {
841 return null;
842 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800843 }
Winson Chungaafa03c2010-06-11 17:34:16 -0700844
Michael Jurka0280c3b2010-09-17 15:00:07 -0700845 boolean existsEmptyCell() {
846 return findCellForSpan(null, 1, 1);
847 }
848
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800849 /**
Michael Jurka0280c3b2010-09-17 15:00:07 -0700850 * Finds the upper-left coordinate of the first rectangle in the grid that can
851 * hold a cell of the specified dimensions. If intersectX and intersectY are not -1,
852 * then this method will only return coordinates for rectangles that contain the cell
853 * (intersectX, intersectY)
854 *
855 * @param cellXY The array that will contain the position of a vacant cell if such a cell
856 * can be found.
857 * @param spanX The horizontal span of the cell we want to find.
858 * @param spanY The vertical span of the cell we want to find.
859 *
860 * @return True if a vacant cell of the specified dimension was found, false otherwise.
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700861 */
Michael Jurka0280c3b2010-09-17 15:00:07 -0700862 boolean findCellForSpan(int[] cellXY, int spanX, int spanY) {
863 return findCellForSpanThatIntersectsIgnoring(cellXY, spanX, spanY, -1, -1, null);
864 }
865
866 /**
867 * Like above, but ignores any cells occupied by the item "ignoreView"
868 *
869 * @param cellXY The array that will contain the position of a vacant cell if such a cell
870 * can be found.
871 * @param spanX The horizontal span of the cell we want to find.
872 * @param spanY The vertical span of the cell we want to find.
873 * @param ignoreView The home screen item we should treat as not occupying any space
874 * @return
875 */
876 boolean findCellForSpanIgnoring(int[] cellXY, int spanX, int spanY, View ignoreView) {
877 return findCellForSpanThatIntersectsIgnoring(cellXY, spanX, spanY, -1, -1, ignoreView);
878 }
879
880 /**
881 * Like above, but if intersectX and intersectY are not -1, then this method will try to
882 * return coordinates for rectangles that contain the cell [intersectX, intersectY]
883 *
884 * @param spanX The horizontal span of the cell we want to find.
885 * @param spanY The vertical span of the cell we want to find.
886 * @param ignoreView The home screen item we should treat as not occupying any space
887 * @param intersectX The X coordinate of the cell that we should try to overlap
888 * @param intersectX The Y coordinate of the cell that we should try to overlap
889 *
890 * @return True if a vacant cell of the specified dimension was found, false otherwise.
891 */
892 boolean findCellForSpanThatIntersects(int[] cellXY, int spanX, int spanY,
893 int intersectX, int intersectY) {
894 return findCellForSpanThatIntersectsIgnoring(
895 cellXY, spanX, spanY, intersectX, intersectY, null);
896 }
897
898 /**
899 * The superset of the above two methods
900 */
901 boolean findCellForSpanThatIntersectsIgnoring(int[] cellXY, int spanX, int spanY,
902 int intersectX, int intersectY, View ignoreView) {
903 if (ignoreView != null) {
904 markCellsAsUnoccupiedForView(ignoreView);
905 }
906
Michael Jurka28750fb2010-09-24 17:43:49 -0700907 boolean foundCell = false;
Michael Jurka0280c3b2010-09-17 15:00:07 -0700908 while (true) {
909 int startX = 0;
910 if (intersectX >= 0) {
911 startX = Math.max(startX, intersectX - (spanX - 1));
912 }
913 int endX = mCountX - (spanX - 1);
914 if (intersectX >= 0) {
915 endX = Math.min(endX, intersectX + (spanX - 1) + (spanX == 1 ? 1 : 0));
916 }
917 int startY = 0;
918 if (intersectY >= 0) {
919 startY = Math.max(startY, intersectY - (spanY - 1));
920 }
921 int endY = mCountY - (spanY - 1);
922 if (intersectY >= 0) {
923 endY = Math.min(endY, intersectY + (spanY - 1) + (spanY == 1 ? 1 : 0));
924 }
925
926 for (int x = startX; x < endX; x++) {
927 inner:
928 for (int y = startY; y < endY; y++) {
929 for (int i = 0; i < spanX; i++) {
930 for (int j = 0; j < spanY; j++) {
931 if (mOccupied[x + i][y + j]) {
932 // small optimization: we can skip to below the row we just found
933 // an occupied cell
934 y += j;
935 continue inner;
936 }
937 }
938 }
939 if (cellXY != null) {
940 cellXY[0] = x;
941 cellXY[1] = y;
942 }
Michael Jurka28750fb2010-09-24 17:43:49 -0700943 foundCell = true;
944 break;
Michael Jurka0280c3b2010-09-17 15:00:07 -0700945 }
946 }
947 if (intersectX == -1 && intersectY == -1) {
948 break;
949 } else {
950 // if we failed to find anything, try again but without any requirements of
951 // intersecting
952 intersectX = -1;
953 intersectY = -1;
954 continue;
955 }
956 }
957
958 if (ignoreView != null) {
959 markCellsAsOccupiedForView(ignoreView);
960 }
Michael Jurka28750fb2010-09-24 17:43:49 -0700961 return foundCell;
Michael Jurka0280c3b2010-09-17 15:00:07 -0700962 }
963
964 /**
965 * Called when drag has left this CellLayout or has been completed (successfully or not)
966 */
967 void onDragExit() {
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700968 // Invalidate the drag data
969 mDragCell[0] = -1;
970 mDragCell[1] = -1;
971
Michael Jurkaa63c4522010-08-19 13:52:27 -0700972 setHover(false);
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700973 invalidate();
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700974
975 // Fade out the drag indicators
976 if (mCrosshairsAnimator != null) {
977 animateCrosshairsTo(0.0f);
978 }
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700979 }
980
981 /**
Winson Chungaafa03c2010-06-11 17:34:16 -0700982 * Mark a child as having been dropped.
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700983 * At the beginning of the drag operation, the child may have been on another
984 * screen, but it is reparented before this method is called.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800985 *
986 * @param child The child that is being dropped
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800987 */
Winson Chungaafa03c2010-06-11 17:34:16 -0700988 void onDropChild(View child) {
Romain Guyd94533d2009-08-17 10:01:15 -0700989 if (child != null) {
990 LayoutParams lp = (LayoutParams) child.getLayoutParams();
Romain Guyd94533d2009-08-17 10:01:15 -0700991 lp.isDragging = false;
Romain Guy84f296c2009-11-04 15:00:44 -0800992 lp.dropped = true;
Romain Guyd94533d2009-08-17 10:01:15 -0700993 child.requestLayout();
Romain Guyd94533d2009-08-17 10:01:15 -0700994 }
Michael Jurka0280c3b2010-09-17 15:00:07 -0700995 onDragExit();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800996 }
997
998 void onDropAborted(View child) {
999 if (child != null) {
1000 ((LayoutParams) child.getLayoutParams()).isDragging = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001001 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001002 onDragExit();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001003 }
1004
1005 /**
1006 * Start dragging the specified child
Winson Chungaafa03c2010-06-11 17:34:16 -07001007 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001008 * @param child The child that is being dragged
1009 */
1010 void onDragChild(View child) {
1011 LayoutParams lp = (LayoutParams) child.getLayoutParams();
1012 lp.isDragging = true;
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001013 }
1014
1015 /**
1016 * A drag event has begun over this layout.
1017 * It may have begun over this layout (in which case onDragChild is called first),
1018 * or it may have begun on another layout.
1019 */
1020 void onDragEnter(View dragView) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001021 mDragRect.setEmpty();
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001022 // Fade in the drag indicators
1023 if (mCrosshairsAnimator != null) {
1024 animateCrosshairsTo(1.0f);
1025 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001026 }
Winson Chungaafa03c2010-06-11 17:34:16 -07001027
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001028 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001029 * Computes a bounding rectangle for a range of cells
Winson Chungaafa03c2010-06-11 17:34:16 -07001030 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001031 * @param cellX X coordinate of upper left corner expressed as a cell position
1032 * @param cellY Y coordinate of upper left corner expressed as a cell position
Winson Chungaafa03c2010-06-11 17:34:16 -07001033 * @param cellHSpan Width in cells
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001034 * @param cellVSpan Height in cells
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001035 * @param resultRect Rect into which to put the results
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001036 */
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001037 public void cellToRect(int cellX, int cellY, int cellHSpan, int cellVSpan, RectF resultRect) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001038 final int cellWidth = mCellWidth;
1039 final int cellHeight = mCellHeight;
1040 final int widthGap = mWidthGap;
1041 final int heightGap = mHeightGap;
Winson Chungaafa03c2010-06-11 17:34:16 -07001042
1043 final int hStartPadding = getLeftPadding();
1044 final int vStartPadding = getTopPadding();
1045
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001046 int width = cellHSpan * cellWidth + ((cellHSpan - 1) * widthGap);
1047 int height = cellVSpan * cellHeight + ((cellVSpan - 1) * heightGap);
1048
1049 int x = hStartPadding + cellX * (cellWidth + widthGap);
1050 int y = vStartPadding + cellY * (cellHeight + heightGap);
Winson Chungaafa03c2010-06-11 17:34:16 -07001051
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001052 resultRect.set(x, y, x + width, y + height);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001053 }
Winson Chungaafa03c2010-06-11 17:34:16 -07001054
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001055 /**
Winson Chungaafa03c2010-06-11 17:34:16 -07001056 * Computes the required horizontal and vertical cell spans to always
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001057 * fit the given rectangle.
Winson Chungaafa03c2010-06-11 17:34:16 -07001058 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001059 * @param width Width in pixels
1060 * @param height Height in pixels
Patrick Dubroy8f86ddc2010-07-16 13:55:32 -07001061 * @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 -08001062 */
Patrick Dubroy8f86ddc2010-07-16 13:55:32 -07001063 public int[] rectToCell(int width, int height, int[] result) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001064 // Always assume we're working with the smallest span to make sure we
1065 // reserve enough space in both orientations.
Joe Onorato79e56262009-09-21 15:23:04 -04001066 final Resources resources = getResources();
1067 int actualWidth = resources.getDimensionPixelSize(R.dimen.workspace_cell_width);
1068 int actualHeight = resources.getDimensionPixelSize(R.dimen.workspace_cell_height);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001069 int smallerSize = Math.min(actualWidth, actualHeight);
Joe Onorato79e56262009-09-21 15:23:04 -04001070
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001071 // Always round up to next largest cell
1072 int spanX = (width + smallerSize) / smallerSize;
1073 int spanY = (height + smallerSize) / smallerSize;
Joe Onorato79e56262009-09-21 15:23:04 -04001074
Patrick Dubroy8f86ddc2010-07-16 13:55:32 -07001075 if (result == null) {
1076 return new int[] { spanX, spanY };
1077 }
1078 result[0] = spanX;
1079 result[1] = spanY;
1080 return result;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001081 }
1082
1083 /**
1084 * Find the first vacant cell, if there is one.
1085 *
1086 * @param vacant Holds the x and y coordinate of the vacant cell
1087 * @param spanX Horizontal cell span.
1088 * @param spanY Vertical cell span.
Winson Chungaafa03c2010-06-11 17:34:16 -07001089 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001090 * @return True if a vacant cell was found
1091 */
1092 public boolean getVacantCell(int[] vacant, int spanX, int spanY) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001093
Michael Jurka0280c3b2010-09-17 15:00:07 -07001094 return findVacantCell(vacant, spanX, spanY, mCountX, mCountY, mOccupied);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001095 }
1096
1097 static boolean findVacantCell(int[] vacant, int spanX, int spanY,
1098 int xCount, int yCount, boolean[][] occupied) {
1099
1100 for (int x = 0; x < xCount; x++) {
1101 for (int y = 0; y < yCount; y++) {
1102 boolean available = !occupied[x][y];
1103out: for (int i = x; i < x + spanX - 1 && x < xCount; i++) {
1104 for (int j = y; j < y + spanY - 1 && y < yCount; j++) {
1105 available = available && !occupied[i][j];
1106 if (!available) break out;
1107 }
1108 }
1109
1110 if (available) {
1111 vacant[0] = x;
1112 vacant[1] = y;
1113 return true;
1114 }
1115 }
1116 }
1117
1118 return false;
1119 }
1120
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001121 /**
1122 * Update the array of occupied cells (mOccupied), and return a flattened copy of the array.
1123 */
1124 boolean[] getOccupiedCellsFlattened() {
Adam Cohend22015c2010-07-26 22:02:18 -07001125 final int xCount = mCountX;
1126 final int yCount = mCountY;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001127 final boolean[][] occupied = mOccupied;
1128
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001129 final boolean[] flat = new boolean[xCount * yCount];
1130 for (int y = 0; y < yCount; y++) {
1131 for (int x = 0; x < xCount; x++) {
1132 flat[y * xCount + x] = occupied[x][y];
1133 }
1134 }
1135
1136 return flat;
1137 }
1138
Michael Jurka0280c3b2010-09-17 15:00:07 -07001139 private void clearOccupiedCells() {
1140 for (int x = 0; x < mCountX; x++) {
1141 for (int y = 0; y < mCountY; y++) {
1142 mOccupied[x][y] = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001143 }
1144 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001145 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001146
Michael Jurka0280c3b2010-09-17 15:00:07 -07001147 public void onMove(View view, int newCellX, int newCellY) {
1148 LayoutParams lp = (LayoutParams) view.getLayoutParams();
1149 markCellsAsUnoccupiedForView(view);
1150 markCellsForView(newCellX, newCellY, lp.cellHSpan, lp.cellVSpan, true);
1151 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001152
Michael Jurka0280c3b2010-09-17 15:00:07 -07001153 private void markCellsAsOccupiedForView(View view) {
1154 LayoutParams lp = (LayoutParams) view.getLayoutParams();
1155 markCellsForView(lp.cellX, lp.cellY, lp.cellHSpan, lp.cellVSpan, true);
1156 }
1157
1158 private void markCellsAsUnoccupiedForView(View view) {
1159 LayoutParams lp = (LayoutParams) view.getLayoutParams();
1160 markCellsForView(lp.cellX, lp.cellY, lp.cellHSpan, lp.cellVSpan, false);
1161 }
1162
1163 private void markCellsForView(int cellX, int cellY, int spanX, int spanY, boolean value) {
1164 for (int x = cellX; x < cellX + spanX && x < mCountX; x++) {
1165 for (int y = cellY; y < cellY + spanY && y < mCountY; y++) {
1166 mOccupied[x][y] = value;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001167 }
1168 }
1169 }
1170
1171 @Override
1172 public ViewGroup.LayoutParams generateLayoutParams(AttributeSet attrs) {
1173 return new CellLayout.LayoutParams(getContext(), attrs);
1174 }
1175
1176 @Override
1177 protected boolean checkLayoutParams(ViewGroup.LayoutParams p) {
1178 return p instanceof CellLayout.LayoutParams;
1179 }
1180
1181 @Override
1182 protected ViewGroup.LayoutParams generateLayoutParams(ViewGroup.LayoutParams p) {
1183 return new CellLayout.LayoutParams(p);
1184 }
1185
Winson Chungaafa03c2010-06-11 17:34:16 -07001186 public static class CellLayoutAnimationController extends LayoutAnimationController {
1187 public CellLayoutAnimationController(Animation animation, float delay) {
1188 super(animation, delay);
1189 }
1190
1191 @Override
1192 protected long getDelayForView(View view) {
1193 return (int) (Math.random() * 150);
1194 }
1195 }
1196
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001197 public static class LayoutParams extends ViewGroup.MarginLayoutParams {
1198 /**
1199 * Horizontal location of the item in the grid.
1200 */
1201 @ViewDebug.ExportedProperty
1202 public int cellX;
1203
1204 /**
1205 * Vertical location of the item in the grid.
1206 */
1207 @ViewDebug.ExportedProperty
1208 public int cellY;
1209
1210 /**
1211 * Number of cells spanned horizontally by the item.
1212 */
1213 @ViewDebug.ExportedProperty
1214 public int cellHSpan;
1215
1216 /**
1217 * Number of cells spanned vertically by the item.
1218 */
1219 @ViewDebug.ExportedProperty
1220 public int cellVSpan;
Winson Chungaafa03c2010-06-11 17:34:16 -07001221
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001222 /**
1223 * Is this item currently being dragged
1224 */
1225 public boolean isDragging;
1226
1227 // X coordinate of the view in the layout.
1228 @ViewDebug.ExportedProperty
1229 int x;
1230 // Y coordinate of the view in the layout.
1231 @ViewDebug.ExportedProperty
1232 int y;
1233
Romain Guy84f296c2009-11-04 15:00:44 -08001234 boolean dropped;
Romain Guyfcb9e712009-10-02 16:06:52 -07001235
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001236 public LayoutParams(Context c, AttributeSet attrs) {
1237 super(c, attrs);
1238 cellHSpan = 1;
1239 cellVSpan = 1;
1240 }
1241
1242 public LayoutParams(ViewGroup.LayoutParams source) {
1243 super(source);
1244 cellHSpan = 1;
1245 cellVSpan = 1;
1246 }
Winson Chungaafa03c2010-06-11 17:34:16 -07001247
1248 public LayoutParams(LayoutParams source) {
1249 super(source);
1250 this.cellX = source.cellX;
1251 this.cellY = source.cellY;
1252 this.cellHSpan = source.cellHSpan;
1253 this.cellVSpan = source.cellVSpan;
1254 }
1255
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001256 public LayoutParams(int cellX, int cellY, int cellHSpan, int cellVSpan) {
Romain Guy8f19cdd2010-01-08 15:07:00 -08001257 super(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001258 this.cellX = cellX;
1259 this.cellY = cellY;
1260 this.cellHSpan = cellHSpan;
1261 this.cellVSpan = cellVSpan;
1262 }
1263
1264 public void setup(int cellWidth, int cellHeight, int widthGap, int heightGap,
1265 int hStartPadding, int vStartPadding) {
Winson Chungaafa03c2010-06-11 17:34:16 -07001266
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001267 final int myCellHSpan = cellHSpan;
1268 final int myCellVSpan = cellVSpan;
1269 final int myCellX = cellX;
1270 final int myCellY = cellY;
Winson Chungaafa03c2010-06-11 17:34:16 -07001271
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001272 width = myCellHSpan * cellWidth + ((myCellHSpan - 1) * widthGap) -
1273 leftMargin - rightMargin;
1274 height = myCellVSpan * cellHeight + ((myCellVSpan - 1) * heightGap) -
1275 topMargin - bottomMargin;
1276
1277 x = hStartPadding + myCellX * (cellWidth + widthGap) + leftMargin;
1278 y = vStartPadding + myCellY * (cellHeight + heightGap) + topMargin;
1279 }
Winson Chungaafa03c2010-06-11 17:34:16 -07001280
1281 public String toString() {
1282 return "(" + this.cellX + ", " + this.cellY + ")";
1283 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001284 }
1285
Michael Jurka0280c3b2010-09-17 15:00:07 -07001286 // This class stores info for two purposes:
1287 // 1. When dragging items (mDragInfo in Workspace), we store the View, its cellX & cellY,
1288 // its spanX, spanY, and the screen it is on
1289 // 2. When long clicking on an empty cell in a CellLayout, we save information about the
1290 // cellX and cellY coordinates and which page was clicked. We then set this as a tag on
1291 // the CellLayout that was long clicked
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001292 static final class CellInfo implements ContextMenu.ContextMenuInfo {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001293 View cell;
Michael Jurkaa63c4522010-08-19 13:52:27 -07001294 int cellX = -1;
1295 int cellY = -1;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001296 int spanX;
1297 int spanY;
1298 int screen;
1299 boolean valid;
1300
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001301 @Override
1302 public String toString() {
Winson Chungaafa03c2010-06-11 17:34:16 -07001303 return "Cell[view=" + (cell == null ? "null" : cell.getClass())
1304 + ", x=" + cellX + ", y=" + cellY + "]";
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001305 }
1306 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001307}