blob: 3c6a8aaedabf776e0129a5dbb1a0da3df054367b [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
Joe Onorato4be866d2010-10-10 11:26:02 -070021import android.animation.Animator;
22import android.animation.AnimatorListenerAdapter;
Chet Haase00397b12010-10-07 11:13:10 -070023import android.animation.TimeInterpolator;
Patrick Dubroyde7658b2010-09-27 11:15:43 -070024import android.animation.ValueAnimator;
25import android.animation.ValueAnimator.AnimatorUpdateListener;
Winson Chungaafa03c2010-06-11 17:34:16 -070026import android.app.WallpaperManager;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080027import android.content.Context;
Joe Onorato79e56262009-09-21 15:23:04 -040028import android.content.res.Resources;
Winson Chungaafa03c2010-06-11 17:34:16 -070029import android.content.res.TypedArray;
Joe Onorato4be866d2010-10-10 11:26:02 -070030import android.graphics.Bitmap;
Winson Chungaafa03c2010-06-11 17:34:16 -070031import android.graphics.Canvas;
Joe Onorato4be866d2010-10-10 11:26:02 -070032import android.graphics.Paint;
Patrick Dubroyde7658b2010-09-27 11:15:43 -070033import android.graphics.Point;
34import android.graphics.PointF;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080035import android.graphics.Rect;
36import android.graphics.RectF;
Patrick Dubroy6569f2c2010-07-12 14:25:18 -070037import android.graphics.drawable.Drawable;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080038import android.util.AttributeSet;
Joe Onorato4be866d2010-10-10 11:26:02 -070039import android.util.Log;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080040import android.view.ContextMenu;
41import android.view.MotionEvent;
42import android.view.View;
43import android.view.ViewDebug;
44import android.view.ViewGroup;
Winson Chungaafa03c2010-06-11 17:34:16 -070045import android.view.animation.Animation;
Winson Chung150fbab2010-09-29 17:14:26 -070046import android.view.animation.DecelerateInterpolator;
Winson Chungaafa03c2010-06-11 17:34:16 -070047import android.view.animation.LayoutAnimationController;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080048
Adam Cohenf34bab52010-09-30 14:11:56 -070049public class CellLayout extends ViewGroup implements Dimmable {
Winson Chungaafa03c2010-06-11 17:34:16 -070050 static final String TAG = "CellLayout";
51
The Android Open Source Project31dd5032009-03-03 19:32:27 -080052 private int mCellWidth;
53 private int mCellHeight;
Winson Chungaafa03c2010-06-11 17:34:16 -070054
Winson Chungaafa03c2010-06-11 17:34:16 -070055 private int mLeftPadding;
56 private int mRightPadding;
57 private int mTopPadding;
58 private int mBottomPadding;
59
Adam Cohend22015c2010-07-26 22:02:18 -070060 private int mCountX;
61 private int mCountY;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080062
63 private int mWidthGap;
64 private int mHeightGap;
65
66 private final Rect mRect = new Rect();
Patrick Dubroy8f86ddc2010-07-16 13:55:32 -070067 private final RectF mRectF = new RectF();
The Android Open Source Project31dd5032009-03-03 19:32:27 -080068 private final CellInfo mCellInfo = new CellInfo();
Winson Chungaafa03c2010-06-11 17:34:16 -070069
Patrick Dubroyde7658b2010-09-27 11:15:43 -070070 // These are temporary variables to prevent having to allocate a new object just to
71 // return an (x, y) value from helper functions. Do NOT use them to maintain other state.
Patrick Dubroy6569f2c2010-07-12 14:25:18 -070072 private final int[] mTmpCellXY = new int[2];
Patrick Dubroyde7658b2010-09-27 11:15:43 -070073 private final int[] mTmpPoint = new int[2];
74 private final PointF mTmpPointF = new PointF();
Patrick Dubroy6569f2c2010-07-12 14:25:18 -070075
The Android Open Source Project31dd5032009-03-03 19:32:27 -080076 boolean[][] mOccupied;
77
Michael Jurkadee05892010-07-27 10:01:56 -070078 private OnTouchListener mInterceptTouchListener;
79
Michael Jurka5f1c5092010-09-03 14:15:02 -070080 private float mBackgroundAlpha;
Adam Cohenf34bab52010-09-30 14:11:56 -070081
Michael Jurka5f1c5092010-09-03 14:15:02 -070082 private Drawable mBackground;
Adam Cohenf34bab52010-09-30 14:11:56 -070083 private Drawable mBackgroundMini;
84 private Drawable mBackgroundMiniHover;
Patrick Dubroy1262e362010-10-06 15:49:50 -070085 private Drawable mBackgroundHover;
Michael Jurka3e7c7632010-10-02 16:01:03 -070086 private Drawable mBackgroundMiniAcceptsDrops;
87 private boolean mAcceptsDrops;
Patrick Dubroy1262e362010-10-06 15:49:50 -070088
89 // If we're actively dragging something over this screen, mHover is true
Michael Jurkaa63c4522010-08-19 13:52:27 -070090 private boolean mHover = false;
Michael Jurkadee05892010-07-27 10:01:56 -070091
Patrick Dubroyde7658b2010-09-27 11:15:43 -070092 private final Point mDragCenter = new Point();
Patrick Dubroy6569f2c2010-07-12 14:25:18 -070093
Winson Chung150fbab2010-09-29 17:14:26 -070094 // These arrays are used to implement the drag visualization on x-large screens.
Joe Onorato4be866d2010-10-10 11:26:02 -070095 // They are used as circular arrays, indexed by mDragOutlineCurrent.
96 private Point[] mDragOutlines = new Point[8];
97 private int[] mDragOutlineAlphas = new int[mDragOutlines.length];
98 private InterruptibleInOutAnimator[] mDragOutlineAnims =
99 new InterruptibleInOutAnimator[mDragOutlines.length];
Winson Chung150fbab2010-09-29 17:14:26 -0700100
101 // Used as an index into the above 3 arrays; indicates which is the most current value.
Joe Onorato4be866d2010-10-10 11:26:02 -0700102 private int mDragOutlineCurrent = 0;
Winson Chung150fbab2010-09-29 17:14:26 -0700103
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700104 private Drawable mCrosshairsDrawable = null;
Patrick Dubroy49250ad2010-10-08 15:33:52 -0700105 private InterruptibleInOutAnimator mCrosshairsAnimator = null;
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700106 private float mCrosshairsVisibility = 0.0f;
107
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700108 // When a drag operation is in progress, holds the nearest cell to the touch point
109 private final int[] mDragCell = new int[2];
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800110
Winson Chungaafa03c2010-06-11 17:34:16 -0700111 private final WallpaperManager mWallpaperManager;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800112
Joe Onorato4be866d2010-10-10 11:26:02 -0700113 private boolean mDragging = false;
114
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800115 public CellLayout(Context context) {
116 this(context, null);
117 }
118
119 public CellLayout(Context context, AttributeSet attrs) {
120 this(context, attrs, 0);
121 }
122
123 public CellLayout(Context context, AttributeSet attrs, int defStyle) {
124 super(context, attrs, defStyle);
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700125
126 // A ViewGroup usually does not draw, but CellLayout needs to draw a rectangle to show
127 // the user where a dragged item will land when dropped.
128 setWillNotDraw(false);
Michael Jurkaa63c4522010-08-19 13:52:27 -0700129
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800130 TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.CellLayout, defStyle, 0);
131
132 mCellWidth = a.getDimensionPixelSize(R.styleable.CellLayout_cellWidth, 10);
133 mCellHeight = a.getDimensionPixelSize(R.styleable.CellLayout_cellHeight, 10);
Winson Chungaafa03c2010-06-11 17:34:16 -0700134
Adam Cohend22015c2010-07-26 22:02:18 -0700135 mLeftPadding =
136 a.getDimensionPixelSize(R.styleable.CellLayout_xAxisStartPadding, 10);
137 mRightPadding =
138 a.getDimensionPixelSize(R.styleable.CellLayout_xAxisEndPadding, 10);
139 mTopPadding =
140 a.getDimensionPixelSize(R.styleable.CellLayout_yAxisStartPadding, 10);
141 mBottomPadding =
142 a.getDimensionPixelSize(R.styleable.CellLayout_yAxisEndPadding, 10);
Winson Chungaafa03c2010-06-11 17:34:16 -0700143
Adam Cohend22015c2010-07-26 22:02:18 -0700144 mCountX = LauncherModel.getCellCountX();
145 mCountY = LauncherModel.getCellCountY();
Michael Jurka0280c3b2010-09-17 15:00:07 -0700146 mOccupied = new boolean[mCountX][mCountY];
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800147
148 a.recycle();
149
150 setAlwaysDrawnWithCacheEnabled(false);
151
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700152 mWallpaperManager = WallpaperManager.getInstance(context);
153
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700154 final Resources res = getResources();
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700155
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700156 if (LauncherApplication.isScreenXLarge()) {
Winson Chung150fbab2010-09-29 17:14:26 -0700157 mBackgroundMini = res.getDrawable(R.drawable.mini_home_screen_bg);
Adam Cohenf34bab52010-09-30 14:11:56 -0700158 mBackgroundMini.setFilterBitmap(true);
Winson Chung150fbab2010-09-29 17:14:26 -0700159 mBackground = res.getDrawable(R.drawable.home_screen_bg);
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700160 mBackground.setFilterBitmap(true);
Winson Chung150fbab2010-09-29 17:14:26 -0700161 mBackgroundMiniHover = res.getDrawable(R.drawable.mini_home_screen_bg_hover);
Adam Cohenf34bab52010-09-30 14:11:56 -0700162 mBackgroundMiniHover.setFilterBitmap(true);
Patrick Dubroy1262e362010-10-06 15:49:50 -0700163 mBackgroundHover = res.getDrawable(R.drawable.home_screen_bg_hover);
164 mBackgroundHover.setFilterBitmap(true);
Michael Jurka3e7c7632010-10-02 16:01:03 -0700165 mBackgroundMiniAcceptsDrops = res.getDrawable(
166 R.drawable.mini_home_screen_bg_accepts_drops);
167 mBackgroundMiniAcceptsDrops.setFilterBitmap(true);
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700168 }
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700169
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700170 // Initialize the data structures used for the drag visualization.
Winson Chung150fbab2010-09-29 17:14:26 -0700171
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700172 mCrosshairsDrawable = res.getDrawable(R.drawable.gardening_crosshairs);
Chet Haase00397b12010-10-07 11:13:10 -0700173 TimeInterpolator interp = new DecelerateInterpolator(2.5f); // Quint ease out
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700174
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700175 // Set up the animation for fading the crosshairs in and out
176 int animDuration = res.getInteger(R.integer.config_crosshairsFadeInTime);
Patrick Dubroy49250ad2010-10-08 15:33:52 -0700177 mCrosshairsAnimator = new InterruptibleInOutAnimator(animDuration, 0.0f, 1.0f);
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700178 mCrosshairsAnimator.addUpdateListener(new AnimatorUpdateListener() {
179 public void onAnimationUpdate(ValueAnimator animation) {
180 mCrosshairsVisibility = ((Float) animation.getAnimatedValue()).floatValue();
181 CellLayout.this.invalidate();
182 }
183 });
184 mCrosshairsAnimator.setInterpolator(interp);
185
Joe Onorato4be866d2010-10-10 11:26:02 -0700186 for (int i = 0; i < mDragOutlines.length; i++) {
187 mDragOutlines[i] = new Point(-1, -1);
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700188 }
189
190 // When dragging things around the home screens, we show a green outline of
191 // where the item will land. The outlines gradually fade out, leaving a trail
192 // behind the drag path.
193 // Set up all the animations that are used to implement this fading.
194 final int duration = res.getInteger(R.integer.config_dragOutlineFadeTime);
195 final int fromAlphaValue = 0;
196 final int toAlphaValue = res.getInteger(R.integer.config_dragOutlineMaxAlpha);
Joe Onorato4be866d2010-10-10 11:26:02 -0700197
198 for (int i = 0; i < mDragOutlineAlphas.length; i++) {
199 mDragOutlineAlphas[i] = fromAlphaValue;
200 }
201
202 for (int i = 0; i < mDragOutlineAnims.length; i++) {
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700203 final InterruptibleInOutAnimator anim =
204 new InterruptibleInOutAnimator(duration, fromAlphaValue, toAlphaValue);
205 anim.setInterpolator(interp);
Joe Onorato4be866d2010-10-10 11:26:02 -0700206
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700207 final int thisIndex = i;
208 anim.addUpdateListener(new AnimatorUpdateListener() {
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700209 public void onAnimationUpdate(ValueAnimator animation) {
Joe Onorato4be866d2010-10-10 11:26:02 -0700210 final Bitmap outline = (Bitmap)anim.getTag();
211
212 // If an animation is started and then stopped very quickly, we can still
213 // get spurious updates we've cleared the tag. Guard against this.
214 if (outline == null) {
Patrick Dubroyfe6bd872010-10-13 17:32:10 -0700215 if (false) {
216 Object val = animation.getAnimatedValue();
217 Log.d(TAG, "anim " + thisIndex + " update: " + val +
218 ", isStopped " + anim.isStopped());
219 }
220
Joe Onorato4be866d2010-10-10 11:26:02 -0700221 // Try to prevent it from continuing to run
222 animation.cancel();
223 } else {
224 mDragOutlineAlphas[thisIndex] = (Integer) animation.getAnimatedValue();
225 final int left = mDragOutlines[thisIndex].x;
226 final int top = mDragOutlines[thisIndex].y;
227 CellLayout.this.invalidate(left, top,
228 left + outline.getWidth(), top + outline.getHeight());
229 }
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700230 }
231 });
Joe Onorato4be866d2010-10-10 11:26:02 -0700232 // The animation holds a reference to the drag outline bitmap as long is it's
233 // running. This way the bitmap can be GCed when the animations are complete.
234 anim.addListener(new AnimatorListenerAdapter() {
235 public void onAnimationEnd(Animator animation) {
236 if ((Integer) anim.getAnimatedValue() == 0) {
237 anim.setTag(null);
238 }
239 }
240 });
241 mDragOutlineAnims[i] = anim;
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700242 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800243 }
244
Michael Jurkaa63c4522010-08-19 13:52:27 -0700245 public void setHover(boolean value) {
246 if (mHover != value) {
Michael Jurka3e7c7632010-10-02 16:01:03 -0700247 mHover = value;
Michael Jurkaa63c4522010-08-19 13:52:27 -0700248 invalidate();
249 }
Michael Jurkaa63c4522010-08-19 13:52:27 -0700250 }
251
Patrick Dubroy1262e362010-10-06 15:49:50 -0700252 public void drawChildren(Canvas canvas) {
253 super.dispatchDraw(canvas);
254 }
255
Jeff Sharkey83f111d2009-04-20 21:03:13 -0700256 @Override
Patrick Dubroy1262e362010-10-06 15:49:50 -0700257 protected void onDraw(Canvas canvas) {
Michael Jurka3e7c7632010-10-02 16:01:03 -0700258 // When we're large, we are either drawn in a "hover" state (ie when dragging an item to
259 // a neighboring page) or with just a normal background (if backgroundAlpha > 0.0f)
260 // When we're small, we are either drawn normally or in the "accepts drops" state (during
261 // a drag). However, we also drag the mini hover background *over* one of those two
262 // backgrounds
Michael Jurka5f1c5092010-09-03 14:15:02 -0700263 if (mBackgroundAlpha > 0.0f) {
Adam Cohenf34bab52010-09-30 14:11:56 -0700264 Drawable bg;
Patrick Dubroy1262e362010-10-06 15:49:50 -0700265 if (getScaleX() < 0.5f) {
Michael Jurka3e7c7632010-10-02 16:01:03 -0700266 bg = mAcceptsDrops ? mBackgroundMiniAcceptsDrops : mBackgroundMini;
Adam Cohenf34bab52010-09-30 14:11:56 -0700267 } else {
Patrick Dubroy1262e362010-10-06 15:49:50 -0700268 bg = mHover ? mBackgroundHover : mBackground;
Adam Cohenf34bab52010-09-30 14:11:56 -0700269 }
Adam Cohen9c4949e2010-10-05 12:27:22 -0700270 if (bg != null) {
271 bg.setAlpha((int) (mBackgroundAlpha * 255));
272 bg.draw(canvas);
273 }
Michael Jurka3e7c7632010-10-02 16:01:03 -0700274 if (mHover && getScaleX() < 0.5f) {
275 mBackgroundMiniHover.setAlpha((int) (mBackgroundAlpha * 255));
276 mBackgroundMiniHover.draw(canvas);
277 }
Michael Jurkaa63c4522010-08-19 13:52:27 -0700278 }
Romain Guya6abce82009-11-10 02:54:41 -0800279
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700280 if (mCrosshairsVisibility > 0.0f) {
281 final int countX = mCountX;
282 final int countY = mCountY;
283
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700284 final float MAX_ALPHA = 0.4f;
285 final int MAX_VISIBLE_DISTANCE = 600;
286 final float DISTANCE_MULTIPLIER = 0.002f;
287
288 final Drawable d = mCrosshairsDrawable;
289 final int width = d.getIntrinsicWidth();
290 final int height = d.getIntrinsicHeight();
291
292 int x = getLeftPadding() - (mWidthGap / 2) - (width / 2);
293 for (int col = 0; col <= countX; col++) {
294 int y = getTopPadding() - (mHeightGap / 2) - (height / 2);
295 for (int row = 0; row <= countY; row++) {
296 mTmpPointF.set(x - mDragCenter.x, y - mDragCenter.y);
297 float dist = mTmpPointF.length();
298 // Crosshairs further from the drag point are more faint
299 float alpha = Math.min(MAX_ALPHA,
300 DISTANCE_MULTIPLIER * (MAX_VISIBLE_DISTANCE - dist));
301 if (alpha > 0.0f) {
302 d.setBounds(x, y, x + width, y + height);
303 d.setAlpha((int) (alpha * 255 * mCrosshairsVisibility));
304 d.draw(canvas);
305 }
306 y += mCellHeight + mHeightGap;
307 }
308 x += mCellWidth + mWidthGap;
309 }
Joe Onorato4be866d2010-10-10 11:26:02 -0700310 }
Winson Chung150fbab2010-09-29 17:14:26 -0700311
Joe Onorato4be866d2010-10-10 11:26:02 -0700312 final Paint paint = new Paint();
313 for (int i = 0; i < mDragOutlines.length; i++) {
314 final int alpha = mDragOutlineAlphas[i];
315 if (alpha > 0) {
316 final Point p = mDragOutlines[i];
317 final Bitmap b = (Bitmap) mDragOutlineAnims[i].getTag();
318 paint.setAlpha(alpha);
319 canvas.drawBitmap(b, p.x, p.y, paint);
Winson Chung150fbab2010-09-29 17:14:26 -0700320 }
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700321 }
322 }
323
Adam Cohenf34bab52010-09-30 14:11:56 -0700324 public void setDimmableProgress(float progress) {
325 for (int i = 0; i < getChildCount(); i++) {
326 Dimmable d = (Dimmable) getChildAt(i);
327 d.setDimmableProgress(progress);
328 }
329 }
330
331 public float getDimmableProgress() {
332 if (getChildCount() > 0) {
333 return ((Dimmable) getChildAt(0)).getDimmableProgress();
334 }
335 return 0.0f;
336 }
337
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700338 @Override
Jeff Sharkey83f111d2009-04-20 21:03:13 -0700339 public void cancelLongPress() {
340 super.cancelLongPress();
341
342 // Cancel long press for all children
343 final int count = getChildCount();
344 for (int i = 0; i < count; i++) {
345 final View child = getChildAt(i);
346 child.cancelLongPress();
347 }
348 }
349
Michael Jurkadee05892010-07-27 10:01:56 -0700350 public void setOnInterceptTouchListener(View.OnTouchListener listener) {
351 mInterceptTouchListener = listener;
352 }
353
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800354 int getCountX() {
Adam Cohend22015c2010-07-26 22:02:18 -0700355 return mCountX;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800356 }
357
358 int getCountY() {
Adam Cohend22015c2010-07-26 22:02:18 -0700359 return mCountY;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800360 }
361
Winson Chungaafa03c2010-06-11 17:34:16 -0700362 public boolean addViewToCellLayout(View child, int index, int childId, LayoutParams params) {
363 final LayoutParams lp = params;
364
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800365 // Generate an id for each view, this assumes we have at most 256x256 cells
366 // per workspace screen
Adam Cohend22015c2010-07-26 22:02:18 -0700367 if (lp.cellX >= 0 && lp.cellX <= mCountX - 1 && lp.cellY >= 0 && lp.cellY <= mCountY - 1) {
Winson Chungaafa03c2010-06-11 17:34:16 -0700368 // If the horizontal or vertical span is set to -1, it is taken to
369 // mean that it spans the extent of the CellLayout
Adam Cohend22015c2010-07-26 22:02:18 -0700370 if (lp.cellHSpan < 0) lp.cellHSpan = mCountX;
371 if (lp.cellVSpan < 0) lp.cellVSpan = mCountY;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800372
Winson Chungaafa03c2010-06-11 17:34:16 -0700373 child.setId(childId);
374
Michael Jurkadee05892010-07-27 10:01:56 -0700375 // We might be in the middle or end of shrinking/fading to a dimmed view
376 // Make sure this view's alpha is set the same as all the rest of the views
Michael Jurka5f1c5092010-09-03 14:15:02 -0700377 child.setAlpha(getAlpha());
Winson Chungaafa03c2010-06-11 17:34:16 -0700378 addView(child, index, lp);
Michael Jurkadee05892010-07-27 10:01:56 -0700379
Michael Jurka0280c3b2010-09-17 15:00:07 -0700380 markCellsAsOccupiedForView(child);
381
Winson Chungaafa03c2010-06-11 17:34:16 -0700382 return true;
383 }
384 return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800385 }
Michael Jurka3e7c7632010-10-02 16:01:03 -0700386 public void setAcceptsDrops(boolean acceptsDrops) {
387 if (mAcceptsDrops != acceptsDrops) {
388 mAcceptsDrops = acceptsDrops;
389 invalidate();
390 }
391 }
392
393 public boolean getAcceptsDrops() {
394 return mAcceptsDrops;
395 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800396
397 @Override
Michael Jurka0280c3b2010-09-17 15:00:07 -0700398 public void removeAllViews() {
399 clearOccupiedCells();
400 }
401
402 @Override
403 public void removeAllViewsInLayout() {
404 clearOccupiedCells();
405 }
406
407 @Override
408 public void removeView(View view) {
409 markCellsAsUnoccupiedForView(view);
410 super.removeView(view);
411 }
412
413 @Override
414 public void removeViewAt(int index) {
415 markCellsAsUnoccupiedForView(getChildAt(index));
416 super.removeViewAt(index);
417 }
418
419 @Override
420 public void removeViewInLayout(View view) {
421 markCellsAsUnoccupiedForView(view);
422 super.removeViewInLayout(view);
423 }
424
425 @Override
426 public void removeViews(int start, int count) {
427 for (int i = start; i < start + count; i++) {
428 markCellsAsUnoccupiedForView(getChildAt(i));
429 }
430 super.removeViews(start, count);
431 }
432
433 @Override
434 public void removeViewsInLayout(int start, int count) {
435 for (int i = start; i < start + count; i++) {
436 markCellsAsUnoccupiedForView(getChildAt(i));
437 }
438 super.removeViewsInLayout(start, count);
439 }
440
441 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800442 public void requestChildFocus(View child, View focused) {
443 super.requestChildFocus(child, focused);
444 if (child != null) {
445 Rect r = new Rect();
446 child.getDrawingRect(r);
447 requestRectangleOnScreen(r);
448 }
449 }
450
451 @Override
452 protected void onAttachedToWindow() {
453 super.onAttachedToWindow();
454 mCellInfo.screen = ((ViewGroup) getParent()).indexOfChild(this);
455 }
456
Michael Jurkaaf442092010-06-10 17:01:57 -0700457 public void setTagToCellInfoForPoint(int touchX, int touchY) {
458 final CellInfo cellInfo = mCellInfo;
459 final Rect frame = mRect;
460 final int x = touchX + mScrollX;
461 final int y = touchY + mScrollY;
462 final int count = getChildCount();
463
464 boolean found = false;
465 for (int i = count - 1; i >= 0; i--) {
466 final View child = getChildAt(i);
467
468 if ((child.getVisibility()) == VISIBLE || child.getAnimation() != null) {
469 child.getHitRect(frame);
470 if (frame.contains(x, y)) {
471 final LayoutParams lp = (LayoutParams) child.getLayoutParams();
472 cellInfo.cell = child;
473 cellInfo.cellX = lp.cellX;
474 cellInfo.cellY = lp.cellY;
475 cellInfo.spanX = lp.cellHSpan;
476 cellInfo.spanY = lp.cellVSpan;
477 cellInfo.valid = true;
478 found = true;
Michael Jurkaaf442092010-06-10 17:01:57 -0700479 break;
480 }
481 }
482 }
Winson Chungaafa03c2010-06-11 17:34:16 -0700483
Michael Jurkaaf442092010-06-10 17:01:57 -0700484 if (!found) {
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700485 final int cellXY[] = mTmpCellXY;
Michael Jurkaaf442092010-06-10 17:01:57 -0700486 pointToCellExact(x, y, cellXY);
487
Michael Jurkaaf442092010-06-10 17:01:57 -0700488 cellInfo.cell = null;
489 cellInfo.cellX = cellXY[0];
490 cellInfo.cellY = cellXY[1];
491 cellInfo.spanX = 1;
492 cellInfo.spanY = 1;
Michael Jurka0280c3b2010-09-17 15:00:07 -0700493 cellInfo.valid = cellXY[0] >= 0 && cellXY[1] >= 0 && cellXY[0] < mCountX &&
494 cellXY[1] < mCountY && !mOccupied[cellXY[0]][cellXY[1]];
Michael Jurkaaf442092010-06-10 17:01:57 -0700495 }
496 setTag(cellInfo);
497 }
498
Winson Chungaafa03c2010-06-11 17:34:16 -0700499
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800500 @Override
501 public boolean onInterceptTouchEvent(MotionEvent ev) {
Michael Jurkadee05892010-07-27 10:01:56 -0700502 if (mInterceptTouchListener != null && mInterceptTouchListener.onTouch(this, ev)) {
503 return true;
504 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800505 final int action = ev.getAction();
506 final CellInfo cellInfo = mCellInfo;
507
508 if (action == MotionEvent.ACTION_DOWN) {
Michael Jurkaaf442092010-06-10 17:01:57 -0700509 setTagToCellInfoForPoint((int) ev.getX(), (int) ev.getY());
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800510 } else if (action == MotionEvent.ACTION_UP) {
511 cellInfo.cell = null;
512 cellInfo.cellX = -1;
513 cellInfo.cellY = -1;
514 cellInfo.spanX = 0;
515 cellInfo.spanY = 0;
516 cellInfo.valid = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800517 setTag(cellInfo);
518 }
519
520 return false;
521 }
522
523 @Override
524 public CellInfo getTag() {
Michael Jurka0280c3b2010-09-17 15:00:07 -0700525 return (CellInfo) super.getTag();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800526 }
527
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700528 /**
529 * Check if the row 'y' is empty from columns 'left' to 'right', inclusive.
530 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800531 private static boolean isRowEmpty(int y, int left, int right, boolean[][] occupied) {
532 for (int x = left; x <= right; x++) {
533 if (occupied[x][y]) {
534 return false;
535 }
536 }
537 return true;
538 }
539
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800540 /**
Winson Chungaafa03c2010-06-11 17:34:16 -0700541 * Given a point, return the cell that strictly encloses that point
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800542 * @param x X coordinate of the point
543 * @param y Y coordinate of the point
544 * @param result Array of 2 ints to hold the x and y coordinate of the cell
545 */
546 void pointToCellExact(int x, int y, int[] result) {
Winson Chungaafa03c2010-06-11 17:34:16 -0700547 final int hStartPadding = getLeftPadding();
548 final int vStartPadding = getTopPadding();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800549
550 result[0] = (x - hStartPadding) / (mCellWidth + mWidthGap);
551 result[1] = (y - vStartPadding) / (mCellHeight + mHeightGap);
552
Adam Cohend22015c2010-07-26 22:02:18 -0700553 final int xAxis = mCountX;
554 final int yAxis = mCountY;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800555
556 if (result[0] < 0) result[0] = 0;
557 if (result[0] >= xAxis) result[0] = xAxis - 1;
558 if (result[1] < 0) result[1] = 0;
559 if (result[1] >= yAxis) result[1] = yAxis - 1;
560 }
Winson Chungaafa03c2010-06-11 17:34:16 -0700561
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800562 /**
563 * Given a point, return the cell that most closely encloses that point
564 * @param x X coordinate of the point
565 * @param y Y coordinate of the point
566 * @param result Array of 2 ints to hold the x and y coordinate of the cell
567 */
568 void pointToCellRounded(int x, int y, int[] result) {
569 pointToCellExact(x + (mCellWidth / 2), y + (mCellHeight / 2), result);
570 }
571
572 /**
573 * Given a cell coordinate, return the point that represents the upper left corner of that cell
Winson Chungaafa03c2010-06-11 17:34:16 -0700574 *
575 * @param cellX X coordinate of the cell
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800576 * @param cellY Y coordinate of the cell
Winson Chungaafa03c2010-06-11 17:34:16 -0700577 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800578 * @param result Array of 2 ints to hold the x and y coordinate of the point
579 */
580 void cellToPoint(int cellX, int cellY, int[] result) {
Winson Chungaafa03c2010-06-11 17:34:16 -0700581 final int hStartPadding = getLeftPadding();
582 final int vStartPadding = getTopPadding();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800583
584 result[0] = hStartPadding + cellX * (mCellWidth + mWidthGap);
585 result[1] = vStartPadding + cellY * (mCellHeight + mHeightGap);
586 }
587
Romain Guy84f296c2009-11-04 15:00:44 -0800588 int getCellWidth() {
589 return mCellWidth;
590 }
591
592 int getCellHeight() {
593 return mCellHeight;
594 }
595
Romain Guy1a304a12009-11-10 00:02:32 -0800596 int getLeftPadding() {
Winson Chungaafa03c2010-06-11 17:34:16 -0700597 return mLeftPadding;
Romain Guy1a304a12009-11-10 00:02:32 -0800598 }
599
600 int getTopPadding() {
Winson Chungaafa03c2010-06-11 17:34:16 -0700601 return mTopPadding;
Romain Guy1a304a12009-11-10 00:02:32 -0800602 }
603
604 int getRightPadding() {
Winson Chungaafa03c2010-06-11 17:34:16 -0700605 return mRightPadding;
Romain Guy1a304a12009-11-10 00:02:32 -0800606 }
607
608 int getBottomPadding() {
Winson Chungaafa03c2010-06-11 17:34:16 -0700609 return mBottomPadding;
Romain Guy1a304a12009-11-10 00:02:32 -0800610 }
611
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800612 @Override
613 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
614 // TODO: currently ignoring padding
Winson Chungaafa03c2010-06-11 17:34:16 -0700615
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800616 int widthSpecMode = MeasureSpec.getMode(widthMeasureSpec);
Winson Chungaafa03c2010-06-11 17:34:16 -0700617 int widthSpecSize = MeasureSpec.getSize(widthMeasureSpec);
618
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800619 int heightSpecMode = MeasureSpec.getMode(heightMeasureSpec);
620 int heightSpecSize = MeasureSpec.getSize(heightMeasureSpec);
Winson Chungaafa03c2010-06-11 17:34:16 -0700621
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800622 if (widthSpecMode == MeasureSpec.UNSPECIFIED || heightSpecMode == MeasureSpec.UNSPECIFIED) {
623 throw new RuntimeException("CellLayout cannot have UNSPECIFIED dimensions");
624 }
625
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800626 final int cellWidth = mCellWidth;
627 final int cellHeight = mCellHeight;
628
Adam Cohend22015c2010-07-26 22:02:18 -0700629 int numWidthGaps = mCountX - 1;
630 int numHeightGaps = mCountY - 1;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800631
Michael Jurka0280c3b2010-09-17 15:00:07 -0700632 int vSpaceLeft = heightSpecSize - mTopPadding - mBottomPadding - (cellHeight * mCountY);
Adam Cohend22015c2010-07-26 22:02:18 -0700633 mHeightGap = vSpaceLeft / numHeightGaps;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800634
Michael Jurka0280c3b2010-09-17 15:00:07 -0700635 int hSpaceLeft = widthSpecSize - mLeftPadding - mRightPadding - (cellWidth * mCountX);
Adam Cohend22015c2010-07-26 22:02:18 -0700636 mWidthGap = hSpaceLeft / numWidthGaps;
Winson Chungaafa03c2010-06-11 17:34:16 -0700637
Michael Jurka5f1c5092010-09-03 14:15:02 -0700638 // center it around the min gaps
639 int minGap = Math.min(mWidthGap, mHeightGap);
640 mWidthGap = mHeightGap = minGap;
641
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800642 int count = getChildCount();
643
644 for (int i = 0; i < count; i++) {
645 View child = getChildAt(i);
646 LayoutParams lp = (LayoutParams) child.getLayoutParams();
Winson Chungaafa03c2010-06-11 17:34:16 -0700647 lp.setup(cellWidth, cellHeight, mWidthGap, mHeightGap,
648 mLeftPadding, mTopPadding);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800649
Michael Jurka0280c3b2010-09-17 15:00:07 -0700650 int childWidthMeasureSpec = MeasureSpec.makeMeasureSpec(lp.width, MeasureSpec.EXACTLY);
Winson Chungaafa03c2010-06-11 17:34:16 -0700651 int childheightMeasureSpec = MeasureSpec.makeMeasureSpec(lp.height,
652 MeasureSpec.EXACTLY);
653
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800654 child.measure(childWidthMeasureSpec, childheightMeasureSpec);
655 }
Michael Jurka5f1c5092010-09-03 14:15:02 -0700656 if (widthSpecMode == MeasureSpec.AT_MOST) {
657 int newWidth = mLeftPadding + mRightPadding + (mCountX * cellWidth) +
658 ((mCountX - 1) * minGap);
659 int newHeight = mTopPadding + mBottomPadding + (mCountY * cellHeight) +
660 ((mCountY - 1) * minGap);
661 setMeasuredDimension(newWidth, newHeight);
662 } else if (widthSpecMode == MeasureSpec.EXACTLY) {
663 setMeasuredDimension(widthSpecSize, heightSpecSize);
664 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800665 }
666
667 @Override
Michael Jurka28750fb2010-09-24 17:43:49 -0700668 protected void onLayout(boolean changed, int l, int t, int r, int b) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800669 int count = getChildCount();
670
671 for (int i = 0; i < count; i++) {
672 View child = getChildAt(i);
673 if (child.getVisibility() != GONE) {
674
675 CellLayout.LayoutParams lp = (CellLayout.LayoutParams) child.getLayoutParams();
676
677 int childLeft = lp.x;
678 int childTop = lp.y;
679 child.layout(childLeft, childTop, childLeft + lp.width, childTop + lp.height);
Romain Guy84f296c2009-11-04 15:00:44 -0800680
681 if (lp.dropped) {
682 lp.dropped = false;
683
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700684 final int[] cellXY = mTmpCellXY;
Romain Guy06762ab2010-01-25 16:51:08 -0800685 getLocationOnScreen(cellXY);
Romain Guy84f296c2009-11-04 15:00:44 -0800686 mWallpaperManager.sendWallpaperCommand(getWindowToken(), "android.home.drop",
Romain Guy06762ab2010-01-25 16:51:08 -0800687 cellXY[0] + childLeft + lp.width / 2,
688 cellXY[1] + childTop + lp.height / 2, 0, null);
Romain Guy84f296c2009-11-04 15:00:44 -0800689 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800690 }
691 }
692 }
693
694 @Override
Michael Jurkadee05892010-07-27 10:01:56 -0700695 protected void onSizeChanged(int w, int h, int oldw, int oldh) {
696 super.onSizeChanged(w, h, oldw, oldh);
Michael Jurka5f1c5092010-09-03 14:15:02 -0700697 if (mBackground != null) {
Patrick Dubroy1262e362010-10-06 15:49:50 -0700698 mBackground.setBounds(0, 0, w, h);
699 }
700 if (mBackgroundHover != null) {
701 mBackgroundHover.setBounds(0, 0, w, h);
Michael Jurkaa63c4522010-08-19 13:52:27 -0700702 }
Adam Cohenf34bab52010-09-30 14:11:56 -0700703 if (mBackgroundMiniHover != null) {
Patrick Dubroy1262e362010-10-06 15:49:50 -0700704 mBackgroundMiniHover.setBounds(0, 0, w, h);
Adam Cohenf34bab52010-09-30 14:11:56 -0700705 }
706 if (mBackgroundMini != null) {
Patrick Dubroy1262e362010-10-06 15:49:50 -0700707 mBackgroundMini.setBounds(0, 0, w, h);
Michael Jurkaa63c4522010-08-19 13:52:27 -0700708 }
Michael Jurka3e7c7632010-10-02 16:01:03 -0700709 if (mBackgroundMiniAcceptsDrops != null) {
710 mBackgroundMiniAcceptsDrops.setBounds(0, 0, w, h);
711 }
Michael Jurkadee05892010-07-27 10:01:56 -0700712 }
713
714 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800715 protected void setChildrenDrawingCacheEnabled(boolean enabled) {
716 final int count = getChildCount();
717 for (int i = 0; i < count; i++) {
718 final View view = getChildAt(i);
719 view.setDrawingCacheEnabled(enabled);
720 // Update the drawing caches
Adam Powellfefa0ce2010-05-03 10:23:50 -0700721 view.buildDrawingCache(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800722 }
723 }
724
725 @Override
726 protected void setChildrenDrawnWithCacheEnabled(boolean enabled) {
727 super.setChildrenDrawnWithCacheEnabled(enabled);
728 }
729
Michael Jurka5f1c5092010-09-03 14:15:02 -0700730 public float getBackgroundAlpha() {
731 return mBackgroundAlpha;
Michael Jurkadee05892010-07-27 10:01:56 -0700732 }
733
Michael Jurka5f1c5092010-09-03 14:15:02 -0700734 public void setBackgroundAlpha(float alpha) {
735 mBackgroundAlpha = alpha;
Michael Jurka0142d492010-08-25 17:46:15 -0700736 invalidate();
Michael Jurkadee05892010-07-27 10:01:56 -0700737 }
738
Michael Jurka5f1c5092010-09-03 14:15:02 -0700739 // Need to return true to let the view system know we know how to handle alpha-- this is
740 // because when our children have an alpha of 0.0f, they are still rendering their "dimmed"
741 // versions
742 @Override
743 protected boolean onSetAlpha(int alpha) {
744 return true;
745 }
746
747 public void setAlpha(float alpha) {
748 setChildrenAlpha(alpha);
749 super.setAlpha(alpha);
750 }
751
Michael Jurkadee05892010-07-27 10:01:56 -0700752 private void setChildrenAlpha(float alpha) {
Michael Jurka0142d492010-08-25 17:46:15 -0700753 final int childCount = getChildCount();
754 for (int i = 0; i < childCount; i++) {
Michael Jurkadee05892010-07-27 10:01:56 -0700755 getChildAt(i).setAlpha(alpha);
756 }
757 }
758
Michael Jurka0280c3b2010-09-17 15:00:07 -0700759 private boolean isVacantIgnoring(
760 int originX, int originY, int spanX, int spanY, View ignoreView) {
761 if (ignoreView != null) {
762 markCellsAsUnoccupiedForView(ignoreView);
763 }
Michael Jurka28750fb2010-09-24 17:43:49 -0700764 boolean isVacant = true;
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700765 for (int i = 0; i < spanY; i++) {
766 if (!isRowEmpty(originY + i, originX, originX + spanX - 1, mOccupied)) {
Michael Jurka28750fb2010-09-24 17:43:49 -0700767 isVacant = false;
768 break;
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700769 }
770 }
Michael Jurka0280c3b2010-09-17 15:00:07 -0700771 if (ignoreView != null) {
772 markCellsAsOccupiedForView(ignoreView);
773 }
Michael Jurka28750fb2010-09-24 17:43:49 -0700774 return isVacant;
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700775 }
776
Michael Jurka0280c3b2010-09-17 15:00:07 -0700777 private boolean isVacant(int originX, int originY, int spanX, int spanY) {
778 return isVacantIgnoring(originX, originY, spanX, spanY, null);
779 }
780
Patrick Dubroy440c3602010-07-13 17:50:32 -0700781 public View getChildAt(int x, int y) {
782 final int count = getChildCount();
783 for (int i = 0; i < count; i++) {
784 View child = getChildAt(i);
785 LayoutParams lp = (LayoutParams) child.getLayoutParams();
786
787 if ((lp.cellX <= x) && (x < lp.cellX + lp.cellHSpan) &&
788 (lp.cellY <= y) && (y < lp.cellY + lp.cellHSpan)) {
789 return child;
790 }
791 }
792 return null;
793 }
794
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700795 /**
Patrick Dubroy8f86ddc2010-07-16 13:55:32 -0700796 * Estimate the size that a child with the given dimensions will take in the layout.
797 */
798 void estimateChildSize(int minWidth, int minHeight, int[] result) {
799 // Assuming it's placed at 0, 0, find where the bottom right cell will land
800 rectToCell(minWidth, minHeight, result);
801
802 // Then figure out the rect it will occupy
803 cellToRect(0, 0, result[0], result[1], mRectF);
804 result[0] = (int)mRectF.width();
805 result[1] = (int)mRectF.height();
806 }
807
808 /**
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700809 * Estimate where the top left cell of the dragged item will land if it is dropped.
810 *
811 * @param originX The X value of the top left corner of the item
812 * @param originY The Y value of the top left corner of the item
813 * @param spanX The number of horizontal cells that the item spans
814 * @param spanY The number of vertical cells that the item spans
815 * @param result The estimated drop cell X and Y.
816 */
817 void estimateDropCell(int originX, int originY, int spanX, int spanY, int[] result) {
Adam Cohend22015c2010-07-26 22:02:18 -0700818 final int countX = mCountX;
819 final int countY = mCountY;
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700820
Michael Jurkaa63c4522010-08-19 13:52:27 -0700821 // pointToCellRounded takes the top left of a cell but will pad that with
822 // cellWidth/2 and cellHeight/2 when finding the matching cell
823 pointToCellRounded(originX, originY, result);
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700824
825 // If the item isn't fully on this screen, snap to the edges
826 int rightOverhang = result[0] + spanX - countX;
827 if (rightOverhang > 0) {
828 result[0] -= rightOverhang; // Snap to right
829 }
830 result[0] = Math.max(0, result[0]); // Snap to left
831 int bottomOverhang = result[1] + spanY - countY;
832 if (bottomOverhang > 0) {
833 result[1] -= bottomOverhang; // Snap to bottom
834 }
835 result[1] = Math.max(0, result[1]); // Snap to top
836 }
837
Joe Onorato4be866d2010-10-10 11:26:02 -0700838 void visualizeDropLocation(
839 View v, Bitmap dragOutline, int originX, int originY, int spanX, int spanY) {
840
841 final int[] nearest = findNearestVacantArea(originX, originY, spanX, spanY, v, mDragCell);
842 mDragCenter.set(originX + (v.getWidth() / 2), originY + (v.getHeight() / 2));
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700843
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700844 if (nearest != null) {
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700845 // Find the top left corner of the rect the object will occupy
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700846 final int[] topLeft = mTmpPoint;
847 cellToPoint(nearest[0], nearest[1], topLeft);
848
Joe Onorato4be866d2010-10-10 11:26:02 -0700849 int left = topLeft[0];
850 int top = topLeft[1];
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700851
Joe Onorato4be866d2010-10-10 11:26:02 -0700852 if (v.getParent() instanceof CellLayout) {
853 LayoutParams lp = (LayoutParams) v.getLayoutParams();
854 left += lp.leftMargin;
855 top += lp.topMargin;
856 }
Winson Chung150fbab2010-09-29 17:14:26 -0700857
Joe Onorato4be866d2010-10-10 11:26:02 -0700858 // Offsets due to the size difference between the View and the dragOutline
859 left += (v.getWidth() - dragOutline.getWidth()) / 2;
860 top += (v.getHeight() - dragOutline.getHeight()) / 2;
Winson Chung150fbab2010-09-29 17:14:26 -0700861
Joe Onorato4be866d2010-10-10 11:26:02 -0700862 final int oldIndex = mDragOutlineCurrent;
863 final Point lastPoint = mDragOutlines[oldIndex];
864 if (lastPoint.x != left || lastPoint.y != top) {
865 mDragOutlineCurrent = (oldIndex + 1) % mDragOutlines.length;
Winson Chung150fbab2010-09-29 17:14:26 -0700866
Joe Onorato4be866d2010-10-10 11:26:02 -0700867 mDragOutlines[mDragOutlineCurrent].set(left, top);
Winson Chung150fbab2010-09-29 17:14:26 -0700868
Joe Onorato4be866d2010-10-10 11:26:02 -0700869 mDragOutlineAnims[oldIndex].animateOut();
870 mDragOutlineAnims[mDragOutlineCurrent].setTag(dragOutline);
871 mDragOutlineAnims[mDragOutlineCurrent].animateIn();
Winson Chung150fbab2010-09-29 17:14:26 -0700872 }
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700873 }
Patrick Dubroy49250ad2010-10-08 15:33:52 -0700874
875 // If we are drawing crosshairs, the entire CellLayout needs to be invalidated
876 if (mCrosshairsDrawable != null) {
877 invalidate();
878 }
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700879 }
880
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800881 /**
Jeff Sharkey70864282009-04-07 21:08:40 -0700882 * Find a vacant area that will fit the given bounds nearest the requested
883 * cell location. Uses Euclidean distance to score multiple vacant areas.
Winson Chungaafa03c2010-06-11 17:34:16 -0700884 *
Romain Guy51afc022009-05-04 18:03:43 -0700885 * @param pixelX The X location at which you want to search for a vacant area.
886 * @param pixelY The Y location at which you want to search for a vacant area.
Jeff Sharkey70864282009-04-07 21:08:40 -0700887 * @param spanX Horizontal span of the object.
888 * @param spanY Vertical span of the object.
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700889 * @param result Array in which to place the result, or null (in which case a new array will
890 * be allocated)
Jeff Sharkey70864282009-04-07 21:08:40 -0700891 * @return The X, Y cell of a vacant area that can contain this object,
892 * nearest the requested location.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800893 */
Michael Jurka6a1435d2010-09-27 17:35:12 -0700894 int[] findNearestVacantArea(
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700895 int pixelX, int pixelY, int spanX, int spanY, int[] result) {
896 return findNearestVacantArea(pixelX, pixelY, spanX, spanY, null, result);
Michael Jurka6a1435d2010-09-27 17:35:12 -0700897 }
Winson Chungaafa03c2010-06-11 17:34:16 -0700898
Michael Jurka6a1435d2010-09-27 17:35:12 -0700899 /**
900 * Find a vacant area that will fit the given bounds nearest the requested
901 * cell location. Uses Euclidean distance to score multiple vacant areas.
902 *
903 * @param pixelX The X location at which you want to search for a vacant area.
904 * @param pixelY The Y location at which you want to search for a vacant area.
905 * @param spanX Horizontal span of the object.
906 * @param spanY Vertical span of the object.
Michael Jurka6a1435d2010-09-27 17:35:12 -0700907 * @param ignoreView Considers space occupied by this view as unoccupied
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700908 * @param result Previously returned value to possibly recycle.
Michael Jurka6a1435d2010-09-27 17:35:12 -0700909 * @return The X, Y cell of a vacant area that can contain this object,
910 * nearest the requested location.
911 */
912 int[] findNearestVacantArea(
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700913 int pixelX, int pixelY, int spanX, int spanY, View ignoreView, int[] result) {
Michael Jurkac6ee42e2010-09-30 12:04:50 -0700914 // mark space take by ignoreView as available (method checks if ignoreView is null)
915 markCellsAsUnoccupiedForView(ignoreView);
916
Jeff Sharkey70864282009-04-07 21:08:40 -0700917 // Keep track of best-scoring drop area
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700918 final int[] bestXY = result != null ? result : new int[2];
Jeff Sharkey70864282009-04-07 21:08:40 -0700919 double bestDistance = Double.MAX_VALUE;
Winson Chungaafa03c2010-06-11 17:34:16 -0700920
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700921 final int countX = mCountX;
922 final int countY = mCountY;
923 final boolean[][] occupied = mOccupied;
924
925 for (int x = 0; x < countX - (spanX - 1); x++) {
Michael Jurkac28de512010-08-13 11:27:44 -0700926 inner:
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700927 for (int y = 0; y < countY - (spanY - 1); y++) {
Michael Jurkac28de512010-08-13 11:27:44 -0700928 for (int i = 0; i < spanX; i++) {
929 for (int j = 0; j < spanY; j++) {
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700930 if (occupied[x + i][y + j]) {
Michael Jurkac28de512010-08-13 11:27:44 -0700931 // small optimization: we can skip to below the row we just found
932 // an occupied cell
933 y += j;
934 continue inner;
935 }
936 }
937 }
938 final int[] cellXY = mTmpCellXY;
939 cellToPoint(x, y, cellXY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800940
Michael Jurkac28de512010-08-13 11:27:44 -0700941 double distance = Math.sqrt(Math.pow(cellXY[0] - pixelX, 2)
942 + Math.pow(cellXY[1] - pixelY, 2));
943 if (distance <= bestDistance) {
944 bestDistance = distance;
945 bestXY[0] = x;
946 bestXY[1] = y;
947 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800948 }
949 }
Michael Jurkac6ee42e2010-09-30 12:04:50 -0700950 // re-mark space taken by ignoreView as occupied
951 markCellsAsOccupiedForView(ignoreView);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800952
Winson Chungaafa03c2010-06-11 17:34:16 -0700953 // Return null if no suitable location found
Jeff Sharkey70864282009-04-07 21:08:40 -0700954 if (bestDistance < Double.MAX_VALUE) {
955 return bestXY;
956 } else {
957 return null;
958 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800959 }
Winson Chungaafa03c2010-06-11 17:34:16 -0700960
Michael Jurka0280c3b2010-09-17 15:00:07 -0700961 boolean existsEmptyCell() {
962 return findCellForSpan(null, 1, 1);
963 }
964
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800965 /**
Michael Jurka0280c3b2010-09-17 15:00:07 -0700966 * Finds the upper-left coordinate of the first rectangle in the grid that can
967 * hold a cell of the specified dimensions. If intersectX and intersectY are not -1,
968 * then this method will only return coordinates for rectangles that contain the cell
969 * (intersectX, intersectY)
970 *
971 * @param cellXY The array that will contain the position of a vacant cell if such a cell
972 * can be found.
973 * @param spanX The horizontal span of the cell we want to find.
974 * @param spanY The vertical span of the cell we want to find.
975 *
976 * @return True if a vacant cell of the specified dimension was found, false otherwise.
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700977 */
Michael Jurka0280c3b2010-09-17 15:00:07 -0700978 boolean findCellForSpan(int[] cellXY, int spanX, int spanY) {
979 return findCellForSpanThatIntersectsIgnoring(cellXY, spanX, spanY, -1, -1, null);
980 }
981
982 /**
983 * Like above, but ignores any cells occupied by the item "ignoreView"
984 *
985 * @param cellXY The array that will contain the position of a vacant cell if such a cell
986 * can be found.
987 * @param spanX The horizontal span of the cell we want to find.
988 * @param spanY The vertical span of the cell we want to find.
989 * @param ignoreView The home screen item we should treat as not occupying any space
990 * @return
991 */
992 boolean findCellForSpanIgnoring(int[] cellXY, int spanX, int spanY, View ignoreView) {
993 return findCellForSpanThatIntersectsIgnoring(cellXY, spanX, spanY, -1, -1, ignoreView);
994 }
995
996 /**
997 * Like above, but if intersectX and intersectY are not -1, then this method will try to
998 * return coordinates for rectangles that contain the cell [intersectX, intersectY]
999 *
1000 * @param spanX The horizontal span of the cell we want to find.
1001 * @param spanY The vertical span of the cell we want to find.
1002 * @param ignoreView The home screen item we should treat as not occupying any space
1003 * @param intersectX The X coordinate of the cell that we should try to overlap
1004 * @param intersectX The Y coordinate of the cell that we should try to overlap
1005 *
1006 * @return True if a vacant cell of the specified dimension was found, false otherwise.
1007 */
1008 boolean findCellForSpanThatIntersects(int[] cellXY, int spanX, int spanY,
1009 int intersectX, int intersectY) {
1010 return findCellForSpanThatIntersectsIgnoring(
1011 cellXY, spanX, spanY, intersectX, intersectY, null);
1012 }
1013
1014 /**
1015 * The superset of the above two methods
1016 */
1017 boolean findCellForSpanThatIntersectsIgnoring(int[] cellXY, int spanX, int spanY,
1018 int intersectX, int intersectY, View ignoreView) {
Michael Jurkac6ee42e2010-09-30 12:04:50 -07001019 // mark space take by ignoreView as available (method checks if ignoreView is null)
1020 markCellsAsUnoccupiedForView(ignoreView);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001021
Michael Jurka28750fb2010-09-24 17:43:49 -07001022 boolean foundCell = false;
Michael Jurka0280c3b2010-09-17 15:00:07 -07001023 while (true) {
1024 int startX = 0;
1025 if (intersectX >= 0) {
1026 startX = Math.max(startX, intersectX - (spanX - 1));
1027 }
1028 int endX = mCountX - (spanX - 1);
1029 if (intersectX >= 0) {
1030 endX = Math.min(endX, intersectX + (spanX - 1) + (spanX == 1 ? 1 : 0));
1031 }
1032 int startY = 0;
1033 if (intersectY >= 0) {
1034 startY = Math.max(startY, intersectY - (spanY - 1));
1035 }
1036 int endY = mCountY - (spanY - 1);
1037 if (intersectY >= 0) {
1038 endY = Math.min(endY, intersectY + (spanY - 1) + (spanY == 1 ? 1 : 0));
1039 }
1040
1041 for (int x = startX; x < endX; x++) {
1042 inner:
1043 for (int y = startY; y < endY; y++) {
1044 for (int i = 0; i < spanX; i++) {
1045 for (int j = 0; j < spanY; j++) {
1046 if (mOccupied[x + i][y + j]) {
1047 // small optimization: we can skip to below the row we just found
1048 // an occupied cell
1049 y += j;
1050 continue inner;
1051 }
1052 }
1053 }
1054 if (cellXY != null) {
1055 cellXY[0] = x;
1056 cellXY[1] = y;
1057 }
Michael Jurka28750fb2010-09-24 17:43:49 -07001058 foundCell = true;
1059 break;
Michael Jurka0280c3b2010-09-17 15:00:07 -07001060 }
1061 }
1062 if (intersectX == -1 && intersectY == -1) {
1063 break;
1064 } else {
1065 // if we failed to find anything, try again but without any requirements of
1066 // intersecting
1067 intersectX = -1;
1068 intersectY = -1;
1069 continue;
1070 }
1071 }
1072
Michael Jurkac6ee42e2010-09-30 12:04:50 -07001073 // re-mark space taken by ignoreView as occupied
1074 markCellsAsOccupiedForView(ignoreView);
Michael Jurka28750fb2010-09-24 17:43:49 -07001075 return foundCell;
Michael Jurka0280c3b2010-09-17 15:00:07 -07001076 }
1077
1078 /**
1079 * Called when drag has left this CellLayout or has been completed (successfully or not)
1080 */
1081 void onDragExit() {
Joe Onorato4be866d2010-10-10 11:26:02 -07001082 // This can actually be called when we aren't in a drag, e.g. when adding a new
1083 // item to this layout via the customize drawer.
1084 // Guard against that case.
1085 if (mDragging) {
1086 mDragging = false;
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001087
Joe Onorato4be866d2010-10-10 11:26:02 -07001088 // Invalidate the drag data
1089 mDragCell[0] = -1;
1090 mDragCell[1] = -1;
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001091
Joe Onorato4be866d2010-10-10 11:26:02 -07001092 setHover(false);
1093
1094 // Fade out the drag indicators
1095 if (mCrosshairsAnimator != null) {
1096 mCrosshairsAnimator.animateOut();
1097 }
1098
1099 final int prev = mDragOutlineCurrent;
1100 mDragOutlineAnims[prev].animateOut();
1101 mDragOutlineCurrent = (prev + 1) % mDragOutlines.length;
1102 mDragOutlines[mDragOutlineCurrent].set(-1, -1);
1103 mDragOutlineAlphas[mDragOutlineCurrent] = 0;
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001104 }
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001105 }
1106
1107 /**
Winson Chungaafa03c2010-06-11 17:34:16 -07001108 * Mark a child as having been dropped.
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001109 * At the beginning of the drag operation, the child may have been on another
1110 * screen, but it is reparented before this method is called.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001111 *
1112 * @param child The child that is being dropped
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001113 */
Winson Chungaafa03c2010-06-11 17:34:16 -07001114 void onDropChild(View child) {
Romain Guyd94533d2009-08-17 10:01:15 -07001115 if (child != null) {
1116 LayoutParams lp = (LayoutParams) child.getLayoutParams();
Romain Guyd94533d2009-08-17 10:01:15 -07001117 lp.isDragging = false;
Romain Guy84f296c2009-11-04 15:00:44 -08001118 lp.dropped = true;
Romain Guyd94533d2009-08-17 10:01:15 -07001119 child.requestLayout();
Romain Guyd94533d2009-08-17 10:01:15 -07001120 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001121 onDragExit();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001122 }
1123
1124 void onDropAborted(View child) {
1125 if (child != null) {
1126 ((LayoutParams) child.getLayoutParams()).isDragging = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001127 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001128 onDragExit();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001129 }
1130
1131 /**
1132 * Start dragging the specified child
Winson Chungaafa03c2010-06-11 17:34:16 -07001133 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001134 * @param child The child that is being dragged
1135 */
1136 void onDragChild(View child) {
1137 LayoutParams lp = (LayoutParams) child.getLayoutParams();
1138 lp.isDragging = true;
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001139 }
1140
1141 /**
1142 * A drag event has begun over this layout.
1143 * It may have begun over this layout (in which case onDragChild is called first),
1144 * or it may have begun on another layout.
1145 */
1146 void onDragEnter(View dragView) {
Patrick Dubroyfe6bd872010-10-13 17:32:10 -07001147 if (!mDragging) {
Joe Onorato4be866d2010-10-10 11:26:02 -07001148// Log.d(TAG, "Received onDragEnter while drag still active");
Patrick Dubroyfe6bd872010-10-13 17:32:10 -07001149 // Fade in the drag indicators
1150 if (mCrosshairsAnimator != null) {
1151 mCrosshairsAnimator.animateIn();
1152 }
Joe Onorato4be866d2010-10-10 11:26:02 -07001153 }
1154 mDragging = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001155 }
Winson Chungaafa03c2010-06-11 17:34:16 -07001156
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001157 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001158 * Computes a bounding rectangle for a range of cells
Winson Chungaafa03c2010-06-11 17:34:16 -07001159 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001160 * @param cellX X coordinate of upper left corner expressed as a cell position
1161 * @param cellY Y coordinate of upper left corner expressed as a cell position
Winson Chungaafa03c2010-06-11 17:34:16 -07001162 * @param cellHSpan Width in cells
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001163 * @param cellVSpan Height in cells
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001164 * @param resultRect Rect into which to put the results
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001165 */
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001166 public void cellToRect(int cellX, int cellY, int cellHSpan, int cellVSpan, RectF resultRect) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001167 final int cellWidth = mCellWidth;
1168 final int cellHeight = mCellHeight;
1169 final int widthGap = mWidthGap;
1170 final int heightGap = mHeightGap;
Winson Chungaafa03c2010-06-11 17:34:16 -07001171
1172 final int hStartPadding = getLeftPadding();
1173 final int vStartPadding = getTopPadding();
1174
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001175 int width = cellHSpan * cellWidth + ((cellHSpan - 1) * widthGap);
1176 int height = cellVSpan * cellHeight + ((cellVSpan - 1) * heightGap);
1177
1178 int x = hStartPadding + cellX * (cellWidth + widthGap);
1179 int y = vStartPadding + cellY * (cellHeight + heightGap);
Winson Chungaafa03c2010-06-11 17:34:16 -07001180
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001181 resultRect.set(x, y, x + width, y + height);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001182 }
Winson Chungaafa03c2010-06-11 17:34:16 -07001183
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001184 /**
Winson Chungaafa03c2010-06-11 17:34:16 -07001185 * Computes the required horizontal and vertical cell spans to always
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001186 * fit the given rectangle.
Winson Chungaafa03c2010-06-11 17:34:16 -07001187 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001188 * @param width Width in pixels
1189 * @param height Height in pixels
Patrick Dubroy8f86ddc2010-07-16 13:55:32 -07001190 * @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 -08001191 */
Patrick Dubroy8f86ddc2010-07-16 13:55:32 -07001192 public int[] rectToCell(int width, int height, int[] result) {
Michael Jurka9987a5c2010-10-08 16:58:12 -07001193 return rectToCell(getResources(), width, height, result);
1194 }
1195
1196 public static int[] rectToCell(Resources resources, int width, int height, int[] result) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001197 // Always assume we're working with the smallest span to make sure we
1198 // reserve enough space in both orientations.
Joe Onorato79e56262009-09-21 15:23:04 -04001199 int actualWidth = resources.getDimensionPixelSize(R.dimen.workspace_cell_width);
1200 int actualHeight = resources.getDimensionPixelSize(R.dimen.workspace_cell_height);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001201 int smallerSize = Math.min(actualWidth, actualHeight);
Joe Onorato79e56262009-09-21 15:23:04 -04001202
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001203 // Always round up to next largest cell
1204 int spanX = (width + smallerSize) / smallerSize;
1205 int spanY = (height + smallerSize) / smallerSize;
Joe Onorato79e56262009-09-21 15:23:04 -04001206
Patrick Dubroy8f86ddc2010-07-16 13:55:32 -07001207 if (result == null) {
1208 return new int[] { spanX, spanY };
1209 }
1210 result[0] = spanX;
1211 result[1] = spanY;
1212 return result;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001213 }
1214
1215 /**
1216 * Find the first vacant cell, if there is one.
1217 *
1218 * @param vacant Holds the x and y coordinate of the vacant cell
1219 * @param spanX Horizontal cell span.
1220 * @param spanY Vertical cell span.
Winson Chungaafa03c2010-06-11 17:34:16 -07001221 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001222 * @return True if a vacant cell was found
1223 */
1224 public boolean getVacantCell(int[] vacant, int spanX, int spanY) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001225
Michael Jurka0280c3b2010-09-17 15:00:07 -07001226 return findVacantCell(vacant, spanX, spanY, mCountX, mCountY, mOccupied);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001227 }
1228
1229 static boolean findVacantCell(int[] vacant, int spanX, int spanY,
1230 int xCount, int yCount, boolean[][] occupied) {
1231
1232 for (int x = 0; x < xCount; x++) {
1233 for (int y = 0; y < yCount; y++) {
1234 boolean available = !occupied[x][y];
1235out: for (int i = x; i < x + spanX - 1 && x < xCount; i++) {
1236 for (int j = y; j < y + spanY - 1 && y < yCount; j++) {
1237 available = available && !occupied[i][j];
1238 if (!available) break out;
1239 }
1240 }
1241
1242 if (available) {
1243 vacant[0] = x;
1244 vacant[1] = y;
1245 return true;
1246 }
1247 }
1248 }
1249
1250 return false;
1251 }
1252
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001253 /**
1254 * Update the array of occupied cells (mOccupied), and return a flattened copy of the array.
1255 */
1256 boolean[] getOccupiedCellsFlattened() {
Adam Cohend22015c2010-07-26 22:02:18 -07001257 final int xCount = mCountX;
1258 final int yCount = mCountY;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001259 final boolean[][] occupied = mOccupied;
1260
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001261 final boolean[] flat = new boolean[xCount * yCount];
1262 for (int y = 0; y < yCount; y++) {
1263 for (int x = 0; x < xCount; x++) {
1264 flat[y * xCount + x] = occupied[x][y];
1265 }
1266 }
1267
1268 return flat;
1269 }
1270
Michael Jurka0280c3b2010-09-17 15:00:07 -07001271 private void clearOccupiedCells() {
1272 for (int x = 0; x < mCountX; x++) {
1273 for (int y = 0; y < mCountY; y++) {
1274 mOccupied[x][y] = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001275 }
1276 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001277 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001278
Michael Jurka0280c3b2010-09-17 15:00:07 -07001279 public void onMove(View view, int newCellX, int newCellY) {
1280 LayoutParams lp = (LayoutParams) view.getLayoutParams();
1281 markCellsAsUnoccupiedForView(view);
1282 markCellsForView(newCellX, newCellY, lp.cellHSpan, lp.cellVSpan, true);
1283 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001284
Michael Jurka0280c3b2010-09-17 15:00:07 -07001285 private void markCellsAsOccupiedForView(View view) {
Michael Jurkac6ee42e2010-09-30 12:04:50 -07001286 if (view == null || view.getParent() != this) return;
Michael Jurka0280c3b2010-09-17 15:00:07 -07001287 LayoutParams lp = (LayoutParams) view.getLayoutParams();
1288 markCellsForView(lp.cellX, lp.cellY, lp.cellHSpan, lp.cellVSpan, true);
1289 }
1290
1291 private void markCellsAsUnoccupiedForView(View view) {
Michael Jurkac6ee42e2010-09-30 12:04:50 -07001292 if (view == null || view.getParent() != this) return;
Michael Jurka0280c3b2010-09-17 15:00:07 -07001293 LayoutParams lp = (LayoutParams) view.getLayoutParams();
1294 markCellsForView(lp.cellX, lp.cellY, lp.cellHSpan, lp.cellVSpan, false);
1295 }
1296
1297 private void markCellsForView(int cellX, int cellY, int spanX, int spanY, boolean value) {
1298 for (int x = cellX; x < cellX + spanX && x < mCountX; x++) {
1299 for (int y = cellY; y < cellY + spanY && y < mCountY; y++) {
1300 mOccupied[x][y] = value;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001301 }
1302 }
1303 }
1304
1305 @Override
1306 public ViewGroup.LayoutParams generateLayoutParams(AttributeSet attrs) {
1307 return new CellLayout.LayoutParams(getContext(), attrs);
1308 }
1309
1310 @Override
1311 protected boolean checkLayoutParams(ViewGroup.LayoutParams p) {
1312 return p instanceof CellLayout.LayoutParams;
1313 }
1314
1315 @Override
1316 protected ViewGroup.LayoutParams generateLayoutParams(ViewGroup.LayoutParams p) {
1317 return new CellLayout.LayoutParams(p);
1318 }
1319
Winson Chungaafa03c2010-06-11 17:34:16 -07001320 public static class CellLayoutAnimationController extends LayoutAnimationController {
1321 public CellLayoutAnimationController(Animation animation, float delay) {
1322 super(animation, delay);
1323 }
1324
1325 @Override
1326 protected long getDelayForView(View view) {
1327 return (int) (Math.random() * 150);
1328 }
1329 }
1330
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001331 public static class LayoutParams extends ViewGroup.MarginLayoutParams {
1332 /**
1333 * Horizontal location of the item in the grid.
1334 */
1335 @ViewDebug.ExportedProperty
1336 public int cellX;
1337
1338 /**
1339 * Vertical location of the item in the grid.
1340 */
1341 @ViewDebug.ExportedProperty
1342 public int cellY;
1343
1344 /**
1345 * Number of cells spanned horizontally by the item.
1346 */
1347 @ViewDebug.ExportedProperty
1348 public int cellHSpan;
1349
1350 /**
1351 * Number of cells spanned vertically by the item.
1352 */
1353 @ViewDebug.ExportedProperty
1354 public int cellVSpan;
Winson Chungaafa03c2010-06-11 17:34:16 -07001355
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001356 /**
1357 * Is this item currently being dragged
1358 */
1359 public boolean isDragging;
1360
1361 // X coordinate of the view in the layout.
1362 @ViewDebug.ExportedProperty
1363 int x;
1364 // Y coordinate of the view in the layout.
1365 @ViewDebug.ExportedProperty
1366 int y;
1367
Romain Guy84f296c2009-11-04 15:00:44 -08001368 boolean dropped;
Romain Guyfcb9e712009-10-02 16:06:52 -07001369
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001370 public LayoutParams(Context c, AttributeSet attrs) {
1371 super(c, attrs);
1372 cellHSpan = 1;
1373 cellVSpan = 1;
1374 }
1375
1376 public LayoutParams(ViewGroup.LayoutParams source) {
1377 super(source);
1378 cellHSpan = 1;
1379 cellVSpan = 1;
1380 }
Winson Chungaafa03c2010-06-11 17:34:16 -07001381
1382 public LayoutParams(LayoutParams source) {
1383 super(source);
1384 this.cellX = source.cellX;
1385 this.cellY = source.cellY;
1386 this.cellHSpan = source.cellHSpan;
1387 this.cellVSpan = source.cellVSpan;
1388 }
1389
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001390 public LayoutParams(int cellX, int cellY, int cellHSpan, int cellVSpan) {
Romain Guy8f19cdd2010-01-08 15:07:00 -08001391 super(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001392 this.cellX = cellX;
1393 this.cellY = cellY;
1394 this.cellHSpan = cellHSpan;
1395 this.cellVSpan = cellVSpan;
1396 }
1397
1398 public void setup(int cellWidth, int cellHeight, int widthGap, int heightGap,
1399 int hStartPadding, int vStartPadding) {
Winson Chungaafa03c2010-06-11 17:34:16 -07001400
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001401 final int myCellHSpan = cellHSpan;
1402 final int myCellVSpan = cellVSpan;
1403 final int myCellX = cellX;
1404 final int myCellY = cellY;
Winson Chungaafa03c2010-06-11 17:34:16 -07001405
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001406 width = myCellHSpan * cellWidth + ((myCellHSpan - 1) * widthGap) -
1407 leftMargin - rightMargin;
1408 height = myCellVSpan * cellHeight + ((myCellVSpan - 1) * heightGap) -
1409 topMargin - bottomMargin;
1410
1411 x = hStartPadding + myCellX * (cellWidth + widthGap) + leftMargin;
1412 y = vStartPadding + myCellY * (cellHeight + heightGap) + topMargin;
1413 }
Winson Chungaafa03c2010-06-11 17:34:16 -07001414
1415 public String toString() {
1416 return "(" + this.cellX + ", " + this.cellY + ")";
1417 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001418 }
1419
Michael Jurka0280c3b2010-09-17 15:00:07 -07001420 // This class stores info for two purposes:
1421 // 1. When dragging items (mDragInfo in Workspace), we store the View, its cellX & cellY,
1422 // its spanX, spanY, and the screen it is on
1423 // 2. When long clicking on an empty cell in a CellLayout, we save information about the
1424 // cellX and cellY coordinates and which page was clicked. We then set this as a tag on
1425 // the CellLayout that was long clicked
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001426 static final class CellInfo implements ContextMenu.ContextMenuInfo {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001427 View cell;
Michael Jurkaa63c4522010-08-19 13:52:27 -07001428 int cellX = -1;
1429 int cellY = -1;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001430 int spanX;
1431 int spanY;
1432 int screen;
1433 boolean valid;
1434
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001435 @Override
1436 public String toString() {
Winson Chungaafa03c2010-06-11 17:34:16 -07001437 return "Cell[view=" + (cell == null ? "null" : cell.getClass())
1438 + ", x=" + cellX + ", y=" + cellY + "]";
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001439 }
1440 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001441}