blob: 08560e930802f91c74301387646582e8e83b9eb7 [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 Cohenf34bab52010-09-30 14:11:56 -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 Cohenf34bab52010-09-30 14:11:56 -070078
Michael Jurka5f1c5092010-09-03 14:15:02 -070079 private Drawable mBackground;
Adam Cohenf34bab52010-09-30 14:11:56 -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
Adam Cohenf34bab52010-09-30 14:11:56 -0700142 mBackgroundMini = getResources().getDrawable(R.drawable.mini_home_screen_bg);
143 mBackgroundMini.setFilterBitmap(true);
144 mBackground = getResources().getDrawable(R.drawable.home_screen_bg);
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700145 mBackground.setFilterBitmap(true);
Adam Cohenf34bab52010-09-30 14:11:56 -0700146 mBackgroundMiniHover = getResources().getDrawable(R.drawable.mini_home_screen_bg_hover);
147 mBackgroundMiniHover.setFilterBitmap(true);
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700148
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 Cohenf34bab52010-09-30 14:11:56 -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 Cohenf34bab52010-09-30 14:11:56 -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 Cohenf34bab52010-09-30 14:11:56 -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 Jurkac6ee42e2010-09-30 12:04:50 -0700797 // mark space take by ignoreView as available (method checks if ignoreView is 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 Jurkac6ee42e2010-09-30 12:04:50 -0700833 // re-mark space taken by ignoreView as occupied
834 markCellsAsOccupiedForView(ignoreView);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800835
Winson Chungaafa03c2010-06-11 17:34:16 -0700836 // Return null if no suitable location found
Jeff Sharkey70864282009-04-07 21:08:40 -0700837 if (bestDistance < Double.MAX_VALUE) {
838 return bestXY;
839 } else {
840 return null;
841 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800842 }
Winson Chungaafa03c2010-06-11 17:34:16 -0700843
Michael Jurka0280c3b2010-09-17 15:00:07 -0700844 boolean existsEmptyCell() {
845 return findCellForSpan(null, 1, 1);
846 }
847
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800848 /**
Michael Jurka0280c3b2010-09-17 15:00:07 -0700849 * Finds the upper-left coordinate of the first rectangle in the grid that can
850 * hold a cell of the specified dimensions. If intersectX and intersectY are not -1,
851 * then this method will only return coordinates for rectangles that contain the cell
852 * (intersectX, intersectY)
853 *
854 * @param cellXY The array that will contain the position of a vacant cell if such a cell
855 * can be found.
856 * @param spanX The horizontal span of the cell we want to find.
857 * @param spanY The vertical span of the cell we want to find.
858 *
859 * @return True if a vacant cell of the specified dimension was found, false otherwise.
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700860 */
Michael Jurka0280c3b2010-09-17 15:00:07 -0700861 boolean findCellForSpan(int[] cellXY, int spanX, int spanY) {
862 return findCellForSpanThatIntersectsIgnoring(cellXY, spanX, spanY, -1, -1, null);
863 }
864
865 /**
866 * Like above, but ignores any cells occupied by the item "ignoreView"
867 *
868 * @param cellXY The array that will contain the position of a vacant cell if such a cell
869 * can be found.
870 * @param spanX The horizontal span of the cell we want to find.
871 * @param spanY The vertical span of the cell we want to find.
872 * @param ignoreView The home screen item we should treat as not occupying any space
873 * @return
874 */
875 boolean findCellForSpanIgnoring(int[] cellXY, int spanX, int spanY, View ignoreView) {
876 return findCellForSpanThatIntersectsIgnoring(cellXY, spanX, spanY, -1, -1, ignoreView);
877 }
878
879 /**
880 * Like above, but if intersectX and intersectY are not -1, then this method will try to
881 * return coordinates for rectangles that contain the cell [intersectX, intersectY]
882 *
883 * @param spanX The horizontal span of the cell we want to find.
884 * @param spanY The vertical span of the cell we want to find.
885 * @param ignoreView The home screen item we should treat as not occupying any space
886 * @param intersectX The X coordinate of the cell that we should try to overlap
887 * @param intersectX The Y coordinate of the cell that we should try to overlap
888 *
889 * @return True if a vacant cell of the specified dimension was found, false otherwise.
890 */
891 boolean findCellForSpanThatIntersects(int[] cellXY, int spanX, int spanY,
892 int intersectX, int intersectY) {
893 return findCellForSpanThatIntersectsIgnoring(
894 cellXY, spanX, spanY, intersectX, intersectY, null);
895 }
896
897 /**
898 * The superset of the above two methods
899 */
900 boolean findCellForSpanThatIntersectsIgnoring(int[] cellXY, int spanX, int spanY,
901 int intersectX, int intersectY, View ignoreView) {
Michael Jurkac6ee42e2010-09-30 12:04:50 -0700902 // mark space take by ignoreView as available (method checks if ignoreView is null)
903 markCellsAsUnoccupiedForView(ignoreView);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700904
Michael Jurka28750fb2010-09-24 17:43:49 -0700905 boolean foundCell = false;
Michael Jurka0280c3b2010-09-17 15:00:07 -0700906 while (true) {
907 int startX = 0;
908 if (intersectX >= 0) {
909 startX = Math.max(startX, intersectX - (spanX - 1));
910 }
911 int endX = mCountX - (spanX - 1);
912 if (intersectX >= 0) {
913 endX = Math.min(endX, intersectX + (spanX - 1) + (spanX == 1 ? 1 : 0));
914 }
915 int startY = 0;
916 if (intersectY >= 0) {
917 startY = Math.max(startY, intersectY - (spanY - 1));
918 }
919 int endY = mCountY - (spanY - 1);
920 if (intersectY >= 0) {
921 endY = Math.min(endY, intersectY + (spanY - 1) + (spanY == 1 ? 1 : 0));
922 }
923
924 for (int x = startX; x < endX; x++) {
925 inner:
926 for (int y = startY; y < endY; y++) {
927 for (int i = 0; i < spanX; i++) {
928 for (int j = 0; j < spanY; j++) {
929 if (mOccupied[x + i][y + j]) {
930 // small optimization: we can skip to below the row we just found
931 // an occupied cell
932 y += j;
933 continue inner;
934 }
935 }
936 }
937 if (cellXY != null) {
938 cellXY[0] = x;
939 cellXY[1] = y;
940 }
Michael Jurka28750fb2010-09-24 17:43:49 -0700941 foundCell = true;
942 break;
Michael Jurka0280c3b2010-09-17 15:00:07 -0700943 }
944 }
945 if (intersectX == -1 && intersectY == -1) {
946 break;
947 } else {
948 // if we failed to find anything, try again but without any requirements of
949 // intersecting
950 intersectX = -1;
951 intersectY = -1;
952 continue;
953 }
954 }
955
Michael Jurkac6ee42e2010-09-30 12:04:50 -0700956 // re-mark space taken by ignoreView as occupied
957 markCellsAsOccupiedForView(ignoreView);
Michael Jurka28750fb2010-09-24 17:43:49 -0700958 return foundCell;
Michael Jurka0280c3b2010-09-17 15:00:07 -0700959 }
960
961 /**
962 * Called when drag has left this CellLayout or has been completed (successfully or not)
963 */
964 void onDragExit() {
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700965 // Invalidate the drag data
966 mDragCell[0] = -1;
967 mDragCell[1] = -1;
968
Michael Jurkaa63c4522010-08-19 13:52:27 -0700969 setHover(false);
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700970 invalidate();
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700971
972 // Fade out the drag indicators
973 if (mCrosshairsAnimator != null) {
974 animateCrosshairsTo(0.0f);
975 }
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700976 }
977
978 /**
Winson Chungaafa03c2010-06-11 17:34:16 -0700979 * Mark a child as having been dropped.
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700980 * At the beginning of the drag operation, the child may have been on another
981 * screen, but it is reparented before this method is called.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800982 *
983 * @param child The child that is being dropped
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800984 */
Winson Chungaafa03c2010-06-11 17:34:16 -0700985 void onDropChild(View child) {
Romain Guyd94533d2009-08-17 10:01:15 -0700986 if (child != null) {
987 LayoutParams lp = (LayoutParams) child.getLayoutParams();
Romain Guyd94533d2009-08-17 10:01:15 -0700988 lp.isDragging = false;
Romain Guy84f296c2009-11-04 15:00:44 -0800989 lp.dropped = true;
Romain Guyd94533d2009-08-17 10:01:15 -0700990 child.requestLayout();
Romain Guyd94533d2009-08-17 10:01:15 -0700991 }
Michael Jurka0280c3b2010-09-17 15:00:07 -0700992 onDragExit();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800993 }
994
995 void onDropAborted(View child) {
996 if (child != null) {
997 ((LayoutParams) child.getLayoutParams()).isDragging = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800998 }
Michael Jurka0280c3b2010-09-17 15:00:07 -0700999 onDragExit();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001000 }
1001
1002 /**
1003 * Start dragging the specified child
Winson Chungaafa03c2010-06-11 17:34:16 -07001004 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001005 * @param child The child that is being dragged
1006 */
1007 void onDragChild(View child) {
1008 LayoutParams lp = (LayoutParams) child.getLayoutParams();
1009 lp.isDragging = true;
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001010 }
1011
1012 /**
1013 * A drag event has begun over this layout.
1014 * It may have begun over this layout (in which case onDragChild is called first),
1015 * or it may have begun on another layout.
1016 */
1017 void onDragEnter(View dragView) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001018 mDragRect.setEmpty();
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001019 // Fade in the drag indicators
1020 if (mCrosshairsAnimator != null) {
1021 animateCrosshairsTo(1.0f);
1022 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001023 }
Winson Chungaafa03c2010-06-11 17:34:16 -07001024
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001025 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001026 * Computes a bounding rectangle for a range of cells
Winson Chungaafa03c2010-06-11 17:34:16 -07001027 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001028 * @param cellX X coordinate of upper left corner expressed as a cell position
1029 * @param cellY Y coordinate of upper left corner expressed as a cell position
Winson Chungaafa03c2010-06-11 17:34:16 -07001030 * @param cellHSpan Width in cells
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001031 * @param cellVSpan Height in cells
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001032 * @param resultRect Rect into which to put the results
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001033 */
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001034 public void cellToRect(int cellX, int cellY, int cellHSpan, int cellVSpan, RectF resultRect) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001035 final int cellWidth = mCellWidth;
1036 final int cellHeight = mCellHeight;
1037 final int widthGap = mWidthGap;
1038 final int heightGap = mHeightGap;
Winson Chungaafa03c2010-06-11 17:34:16 -07001039
1040 final int hStartPadding = getLeftPadding();
1041 final int vStartPadding = getTopPadding();
1042
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001043 int width = cellHSpan * cellWidth + ((cellHSpan - 1) * widthGap);
1044 int height = cellVSpan * cellHeight + ((cellVSpan - 1) * heightGap);
1045
1046 int x = hStartPadding + cellX * (cellWidth + widthGap);
1047 int y = vStartPadding + cellY * (cellHeight + heightGap);
Winson Chungaafa03c2010-06-11 17:34:16 -07001048
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001049 resultRect.set(x, y, x + width, y + height);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001050 }
Winson Chungaafa03c2010-06-11 17:34:16 -07001051
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001052 /**
Winson Chungaafa03c2010-06-11 17:34:16 -07001053 * Computes the required horizontal and vertical cell spans to always
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001054 * fit the given rectangle.
Winson Chungaafa03c2010-06-11 17:34:16 -07001055 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001056 * @param width Width in pixels
1057 * @param height Height in pixels
Patrick Dubroy8f86ddc2010-07-16 13:55:32 -07001058 * @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 -08001059 */
Patrick Dubroy8f86ddc2010-07-16 13:55:32 -07001060 public int[] rectToCell(int width, int height, int[] result) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001061 // Always assume we're working with the smallest span to make sure we
1062 // reserve enough space in both orientations.
Joe Onorato79e56262009-09-21 15:23:04 -04001063 final Resources resources = getResources();
1064 int actualWidth = resources.getDimensionPixelSize(R.dimen.workspace_cell_width);
1065 int actualHeight = resources.getDimensionPixelSize(R.dimen.workspace_cell_height);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001066 int smallerSize = Math.min(actualWidth, actualHeight);
Joe Onorato79e56262009-09-21 15:23:04 -04001067
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001068 // Always round up to next largest cell
1069 int spanX = (width + smallerSize) / smallerSize;
1070 int spanY = (height + smallerSize) / smallerSize;
Joe Onorato79e56262009-09-21 15:23:04 -04001071
Patrick Dubroy8f86ddc2010-07-16 13:55:32 -07001072 if (result == null) {
1073 return new int[] { spanX, spanY };
1074 }
1075 result[0] = spanX;
1076 result[1] = spanY;
1077 return result;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001078 }
1079
1080 /**
1081 * Find the first vacant cell, if there is one.
1082 *
1083 * @param vacant Holds the x and y coordinate of the vacant cell
1084 * @param spanX Horizontal cell span.
1085 * @param spanY Vertical cell span.
Winson Chungaafa03c2010-06-11 17:34:16 -07001086 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001087 * @return True if a vacant cell was found
1088 */
1089 public boolean getVacantCell(int[] vacant, int spanX, int spanY) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001090
Michael Jurka0280c3b2010-09-17 15:00:07 -07001091 return findVacantCell(vacant, spanX, spanY, mCountX, mCountY, mOccupied);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001092 }
1093
1094 static boolean findVacantCell(int[] vacant, int spanX, int spanY,
1095 int xCount, int yCount, boolean[][] occupied) {
1096
1097 for (int x = 0; x < xCount; x++) {
1098 for (int y = 0; y < yCount; y++) {
1099 boolean available = !occupied[x][y];
1100out: for (int i = x; i < x + spanX - 1 && x < xCount; i++) {
1101 for (int j = y; j < y + spanY - 1 && y < yCount; j++) {
1102 available = available && !occupied[i][j];
1103 if (!available) break out;
1104 }
1105 }
1106
1107 if (available) {
1108 vacant[0] = x;
1109 vacant[1] = y;
1110 return true;
1111 }
1112 }
1113 }
1114
1115 return false;
1116 }
1117
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001118 /**
1119 * Update the array of occupied cells (mOccupied), and return a flattened copy of the array.
1120 */
1121 boolean[] getOccupiedCellsFlattened() {
Adam Cohend22015c2010-07-26 22:02:18 -07001122 final int xCount = mCountX;
1123 final int yCount = mCountY;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001124 final boolean[][] occupied = mOccupied;
1125
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001126 final boolean[] flat = new boolean[xCount * yCount];
1127 for (int y = 0; y < yCount; y++) {
1128 for (int x = 0; x < xCount; x++) {
1129 flat[y * xCount + x] = occupied[x][y];
1130 }
1131 }
1132
1133 return flat;
1134 }
1135
Michael Jurka0280c3b2010-09-17 15:00:07 -07001136 private void clearOccupiedCells() {
1137 for (int x = 0; x < mCountX; x++) {
1138 for (int y = 0; y < mCountY; y++) {
1139 mOccupied[x][y] = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001140 }
1141 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001142 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001143
Michael Jurka0280c3b2010-09-17 15:00:07 -07001144 public void onMove(View view, int newCellX, int newCellY) {
1145 LayoutParams lp = (LayoutParams) view.getLayoutParams();
1146 markCellsAsUnoccupiedForView(view);
1147 markCellsForView(newCellX, newCellY, lp.cellHSpan, lp.cellVSpan, true);
1148 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001149
Michael Jurka0280c3b2010-09-17 15:00:07 -07001150 private void markCellsAsOccupiedForView(View view) {
Michael Jurkac6ee42e2010-09-30 12:04:50 -07001151 if (view == null || view.getParent() != this) return;
Michael Jurka0280c3b2010-09-17 15:00:07 -07001152 LayoutParams lp = (LayoutParams) view.getLayoutParams();
1153 markCellsForView(lp.cellX, lp.cellY, lp.cellHSpan, lp.cellVSpan, true);
1154 }
1155
1156 private void markCellsAsUnoccupiedForView(View view) {
Michael Jurkac6ee42e2010-09-30 12:04:50 -07001157 if (view == null || view.getParent() != this) return;
Michael Jurka0280c3b2010-09-17 15:00:07 -07001158 LayoutParams lp = (LayoutParams) view.getLayoutParams();
1159 markCellsForView(lp.cellX, lp.cellY, lp.cellHSpan, lp.cellVSpan, false);
1160 }
1161
1162 private void markCellsForView(int cellX, int cellY, int spanX, int spanY, boolean value) {
1163 for (int x = cellX; x < cellX + spanX && x < mCountX; x++) {
1164 for (int y = cellY; y < cellY + spanY && y < mCountY; y++) {
1165 mOccupied[x][y] = value;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001166 }
1167 }
1168 }
1169
1170 @Override
1171 public ViewGroup.LayoutParams generateLayoutParams(AttributeSet attrs) {
1172 return new CellLayout.LayoutParams(getContext(), attrs);
1173 }
1174
1175 @Override
1176 protected boolean checkLayoutParams(ViewGroup.LayoutParams p) {
1177 return p instanceof CellLayout.LayoutParams;
1178 }
1179
1180 @Override
1181 protected ViewGroup.LayoutParams generateLayoutParams(ViewGroup.LayoutParams p) {
1182 return new CellLayout.LayoutParams(p);
1183 }
1184
Winson Chungaafa03c2010-06-11 17:34:16 -07001185 public static class CellLayoutAnimationController extends LayoutAnimationController {
1186 public CellLayoutAnimationController(Animation animation, float delay) {
1187 super(animation, delay);
1188 }
1189
1190 @Override
1191 protected long getDelayForView(View view) {
1192 return (int) (Math.random() * 150);
1193 }
1194 }
1195
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001196 public static class LayoutParams extends ViewGroup.MarginLayoutParams {
1197 /**
1198 * Horizontal location of the item in the grid.
1199 */
1200 @ViewDebug.ExportedProperty
1201 public int cellX;
1202
1203 /**
1204 * Vertical location of the item in the grid.
1205 */
1206 @ViewDebug.ExportedProperty
1207 public int cellY;
1208
1209 /**
1210 * Number of cells spanned horizontally by the item.
1211 */
1212 @ViewDebug.ExportedProperty
1213 public int cellHSpan;
1214
1215 /**
1216 * Number of cells spanned vertically by the item.
1217 */
1218 @ViewDebug.ExportedProperty
1219 public int cellVSpan;
Winson Chungaafa03c2010-06-11 17:34:16 -07001220
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001221 /**
1222 * Is this item currently being dragged
1223 */
1224 public boolean isDragging;
1225
1226 // X coordinate of the view in the layout.
1227 @ViewDebug.ExportedProperty
1228 int x;
1229 // Y coordinate of the view in the layout.
1230 @ViewDebug.ExportedProperty
1231 int y;
1232
Romain Guy84f296c2009-11-04 15:00:44 -08001233 boolean dropped;
Romain Guyfcb9e712009-10-02 16:06:52 -07001234
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001235 public LayoutParams(Context c, AttributeSet attrs) {
1236 super(c, attrs);
1237 cellHSpan = 1;
1238 cellVSpan = 1;
1239 }
1240
1241 public LayoutParams(ViewGroup.LayoutParams source) {
1242 super(source);
1243 cellHSpan = 1;
1244 cellVSpan = 1;
1245 }
Winson Chungaafa03c2010-06-11 17:34:16 -07001246
1247 public LayoutParams(LayoutParams source) {
1248 super(source);
1249 this.cellX = source.cellX;
1250 this.cellY = source.cellY;
1251 this.cellHSpan = source.cellHSpan;
1252 this.cellVSpan = source.cellVSpan;
1253 }
1254
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001255 public LayoutParams(int cellX, int cellY, int cellHSpan, int cellVSpan) {
Romain Guy8f19cdd2010-01-08 15:07:00 -08001256 super(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001257 this.cellX = cellX;
1258 this.cellY = cellY;
1259 this.cellHSpan = cellHSpan;
1260 this.cellVSpan = cellVSpan;
1261 }
1262
1263 public void setup(int cellWidth, int cellHeight, int widthGap, int heightGap,
1264 int hStartPadding, int vStartPadding) {
Winson Chungaafa03c2010-06-11 17:34:16 -07001265
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001266 final int myCellHSpan = cellHSpan;
1267 final int myCellVSpan = cellVSpan;
1268 final int myCellX = cellX;
1269 final int myCellY = cellY;
Winson Chungaafa03c2010-06-11 17:34:16 -07001270
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001271 width = myCellHSpan * cellWidth + ((myCellHSpan - 1) * widthGap) -
1272 leftMargin - rightMargin;
1273 height = myCellVSpan * cellHeight + ((myCellVSpan - 1) * heightGap) -
1274 topMargin - bottomMargin;
1275
1276 x = hStartPadding + myCellX * (cellWidth + widthGap) + leftMargin;
1277 y = vStartPadding + myCellY * (cellHeight + heightGap) + topMargin;
1278 }
Winson Chungaafa03c2010-06-11 17:34:16 -07001279
1280 public String toString() {
1281 return "(" + this.cellX + ", " + this.cellY + ")";
1282 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001283 }
1284
Michael Jurka0280c3b2010-09-17 15:00:07 -07001285 // This class stores info for two purposes:
1286 // 1. When dragging items (mDragInfo in Workspace), we store the View, its cellX & cellY,
1287 // its spanX, spanY, and the screen it is on
1288 // 2. When long clicking on an empty cell in a CellLayout, we save information about the
1289 // cellX and cellY coordinates and which page was clicked. We then set this as a tag on
1290 // the CellLayout that was long clicked
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001291 static final class CellInfo implements ContextMenu.ContextMenuInfo {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001292 View cell;
Michael Jurkaa63c4522010-08-19 13:52:27 -07001293 int cellX = -1;
1294 int cellY = -1;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001295 int spanX;
1296 int spanY;
1297 int screen;
1298 boolean valid;
1299
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001300 @Override
1301 public String toString() {
Winson Chungaafa03c2010-06-11 17:34:16 -07001302 return "Cell[view=" + (cell == null ? "null" : cell.getClass())
1303 + ", x=" + cellX + ", y=" + cellY + "]";
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001304 }
1305 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001306}