blob: 5c1f57052038da16f11338d5c8fc70772f686cdc [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) {
215 // Try to prevent it from continuing to run
216 animation.cancel();
217 } else {
218 mDragOutlineAlphas[thisIndex] = (Integer) animation.getAnimatedValue();
219 final int left = mDragOutlines[thisIndex].x;
220 final int top = mDragOutlines[thisIndex].y;
221 CellLayout.this.invalidate(left, top,
222 left + outline.getWidth(), top + outline.getHeight());
223 }
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700224 }
225 });
Joe Onorato4be866d2010-10-10 11:26:02 -0700226 // The animation holds a reference to the drag outline bitmap as long is it's
227 // running. This way the bitmap can be GCed when the animations are complete.
228 anim.addListener(new AnimatorListenerAdapter() {
229 public void onAnimationEnd(Animator animation) {
230 if ((Integer) anim.getAnimatedValue() == 0) {
231 anim.setTag(null);
232 }
233 }
234 });
235 mDragOutlineAnims[i] = anim;
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700236 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800237 }
238
Michael Jurkaa63c4522010-08-19 13:52:27 -0700239 public void setHover(boolean value) {
240 if (mHover != value) {
Michael Jurka3e7c7632010-10-02 16:01:03 -0700241 mHover = value;
Michael Jurkaa63c4522010-08-19 13:52:27 -0700242 invalidate();
243 }
Michael Jurkaa63c4522010-08-19 13:52:27 -0700244 }
245
Patrick Dubroy1262e362010-10-06 15:49:50 -0700246 public void drawChildren(Canvas canvas) {
247 super.dispatchDraw(canvas);
248 }
249
Jeff Sharkey83f111d2009-04-20 21:03:13 -0700250 @Override
Patrick Dubroy1262e362010-10-06 15:49:50 -0700251 protected void onDraw(Canvas canvas) {
Michael Jurka3e7c7632010-10-02 16:01:03 -0700252 // When we're large, we are either drawn in a "hover" state (ie when dragging an item to
253 // a neighboring page) or with just a normal background (if backgroundAlpha > 0.0f)
254 // When we're small, we are either drawn normally or in the "accepts drops" state (during
255 // a drag). However, we also drag the mini hover background *over* one of those two
256 // backgrounds
Michael Jurka5f1c5092010-09-03 14:15:02 -0700257 if (mBackgroundAlpha > 0.0f) {
Adam Cohenf34bab52010-09-30 14:11:56 -0700258 Drawable bg;
Patrick Dubroy1262e362010-10-06 15:49:50 -0700259 if (getScaleX() < 0.5f) {
Michael Jurka3e7c7632010-10-02 16:01:03 -0700260 bg = mAcceptsDrops ? mBackgroundMiniAcceptsDrops : mBackgroundMini;
Adam Cohenf34bab52010-09-30 14:11:56 -0700261 } else {
Patrick Dubroy1262e362010-10-06 15:49:50 -0700262 bg = mHover ? mBackgroundHover : mBackground;
Adam Cohenf34bab52010-09-30 14:11:56 -0700263 }
Adam Cohen9c4949e2010-10-05 12:27:22 -0700264 if (bg != null) {
265 bg.setAlpha((int) (mBackgroundAlpha * 255));
266 bg.draw(canvas);
267 }
Michael Jurka3e7c7632010-10-02 16:01:03 -0700268 if (mHover && getScaleX() < 0.5f) {
269 mBackgroundMiniHover.setAlpha((int) (mBackgroundAlpha * 255));
270 mBackgroundMiniHover.draw(canvas);
271 }
Michael Jurkaa63c4522010-08-19 13:52:27 -0700272 }
Romain Guya6abce82009-11-10 02:54:41 -0800273
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700274 if (mCrosshairsVisibility > 0.0f) {
275 final int countX = mCountX;
276 final int countY = mCountY;
277
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700278 final float MAX_ALPHA = 0.4f;
279 final int MAX_VISIBLE_DISTANCE = 600;
280 final float DISTANCE_MULTIPLIER = 0.002f;
281
282 final Drawable d = mCrosshairsDrawable;
283 final int width = d.getIntrinsicWidth();
284 final int height = d.getIntrinsicHeight();
285
286 int x = getLeftPadding() - (mWidthGap / 2) - (width / 2);
287 for (int col = 0; col <= countX; col++) {
288 int y = getTopPadding() - (mHeightGap / 2) - (height / 2);
289 for (int row = 0; row <= countY; row++) {
290 mTmpPointF.set(x - mDragCenter.x, y - mDragCenter.y);
291 float dist = mTmpPointF.length();
292 // Crosshairs further from the drag point are more faint
293 float alpha = Math.min(MAX_ALPHA,
294 DISTANCE_MULTIPLIER * (MAX_VISIBLE_DISTANCE - dist));
295 if (alpha > 0.0f) {
296 d.setBounds(x, y, x + width, y + height);
297 d.setAlpha((int) (alpha * 255 * mCrosshairsVisibility));
298 d.draw(canvas);
299 }
300 y += mCellHeight + mHeightGap;
301 }
302 x += mCellWidth + mWidthGap;
303 }
Joe Onorato4be866d2010-10-10 11:26:02 -0700304 }
Winson Chung150fbab2010-09-29 17:14:26 -0700305
Joe Onorato4be866d2010-10-10 11:26:02 -0700306 final Paint paint = new Paint();
307 for (int i = 0; i < mDragOutlines.length; i++) {
308 final int alpha = mDragOutlineAlphas[i];
309 if (alpha > 0) {
310 final Point p = mDragOutlines[i];
311 final Bitmap b = (Bitmap) mDragOutlineAnims[i].getTag();
312 paint.setAlpha(alpha);
313 canvas.drawBitmap(b, p.x, p.y, paint);
Winson Chung150fbab2010-09-29 17:14:26 -0700314 }
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700315 }
316 }
317
Adam Cohenf34bab52010-09-30 14:11:56 -0700318 public void setDimmableProgress(float progress) {
319 for (int i = 0; i < getChildCount(); i++) {
320 Dimmable d = (Dimmable) getChildAt(i);
321 d.setDimmableProgress(progress);
322 }
323 }
324
325 public float getDimmableProgress() {
326 if (getChildCount() > 0) {
327 return ((Dimmable) getChildAt(0)).getDimmableProgress();
328 }
329 return 0.0f;
330 }
331
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700332 @Override
Jeff Sharkey83f111d2009-04-20 21:03:13 -0700333 public void cancelLongPress() {
334 super.cancelLongPress();
335
336 // Cancel long press for all children
337 final int count = getChildCount();
338 for (int i = 0; i < count; i++) {
339 final View child = getChildAt(i);
340 child.cancelLongPress();
341 }
342 }
343
Michael Jurkadee05892010-07-27 10:01:56 -0700344 public void setOnInterceptTouchListener(View.OnTouchListener listener) {
345 mInterceptTouchListener = listener;
346 }
347
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800348 int getCountX() {
Adam Cohend22015c2010-07-26 22:02:18 -0700349 return mCountX;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800350 }
351
352 int getCountY() {
Adam Cohend22015c2010-07-26 22:02:18 -0700353 return mCountY;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800354 }
355
Winson Chungaafa03c2010-06-11 17:34:16 -0700356 public boolean addViewToCellLayout(View child, int index, int childId, LayoutParams params) {
357 final LayoutParams lp = params;
358
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800359 // Generate an id for each view, this assumes we have at most 256x256 cells
360 // per workspace screen
Adam Cohend22015c2010-07-26 22:02:18 -0700361 if (lp.cellX >= 0 && lp.cellX <= mCountX - 1 && lp.cellY >= 0 && lp.cellY <= mCountY - 1) {
Winson Chungaafa03c2010-06-11 17:34:16 -0700362 // If the horizontal or vertical span is set to -1, it is taken to
363 // mean that it spans the extent of the CellLayout
Adam Cohend22015c2010-07-26 22:02:18 -0700364 if (lp.cellHSpan < 0) lp.cellHSpan = mCountX;
365 if (lp.cellVSpan < 0) lp.cellVSpan = mCountY;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800366
Winson Chungaafa03c2010-06-11 17:34:16 -0700367 child.setId(childId);
368
Michael Jurkadee05892010-07-27 10:01:56 -0700369 // We might be in the middle or end of shrinking/fading to a dimmed view
370 // Make sure this view's alpha is set the same as all the rest of the views
Michael Jurka5f1c5092010-09-03 14:15:02 -0700371 child.setAlpha(getAlpha());
Winson Chungaafa03c2010-06-11 17:34:16 -0700372 addView(child, index, lp);
Michael Jurkadee05892010-07-27 10:01:56 -0700373
Michael Jurka0280c3b2010-09-17 15:00:07 -0700374 markCellsAsOccupiedForView(child);
375
Winson Chungaafa03c2010-06-11 17:34:16 -0700376 return true;
377 }
378 return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800379 }
Michael Jurka3e7c7632010-10-02 16:01:03 -0700380 public void setAcceptsDrops(boolean acceptsDrops) {
381 if (mAcceptsDrops != acceptsDrops) {
382 mAcceptsDrops = acceptsDrops;
383 invalidate();
384 }
385 }
386
387 public boolean getAcceptsDrops() {
388 return mAcceptsDrops;
389 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800390
391 @Override
Michael Jurka0280c3b2010-09-17 15:00:07 -0700392 public void removeAllViews() {
393 clearOccupiedCells();
394 }
395
396 @Override
397 public void removeAllViewsInLayout() {
398 clearOccupiedCells();
399 }
400
401 @Override
402 public void removeView(View view) {
403 markCellsAsUnoccupiedForView(view);
404 super.removeView(view);
405 }
406
407 @Override
408 public void removeViewAt(int index) {
409 markCellsAsUnoccupiedForView(getChildAt(index));
410 super.removeViewAt(index);
411 }
412
413 @Override
414 public void removeViewInLayout(View view) {
415 markCellsAsUnoccupiedForView(view);
416 super.removeViewInLayout(view);
417 }
418
419 @Override
420 public void removeViews(int start, int count) {
421 for (int i = start; i < start + count; i++) {
422 markCellsAsUnoccupiedForView(getChildAt(i));
423 }
424 super.removeViews(start, count);
425 }
426
427 @Override
428 public void removeViewsInLayout(int start, int count) {
429 for (int i = start; i < start + count; i++) {
430 markCellsAsUnoccupiedForView(getChildAt(i));
431 }
432 super.removeViewsInLayout(start, count);
433 }
434
435 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800436 public void requestChildFocus(View child, View focused) {
437 super.requestChildFocus(child, focused);
438 if (child != null) {
439 Rect r = new Rect();
440 child.getDrawingRect(r);
441 requestRectangleOnScreen(r);
442 }
443 }
444
445 @Override
446 protected void onAttachedToWindow() {
447 super.onAttachedToWindow();
448 mCellInfo.screen = ((ViewGroup) getParent()).indexOfChild(this);
449 }
450
Michael Jurkaaf442092010-06-10 17:01:57 -0700451 public void setTagToCellInfoForPoint(int touchX, int touchY) {
452 final CellInfo cellInfo = mCellInfo;
453 final Rect frame = mRect;
454 final int x = touchX + mScrollX;
455 final int y = touchY + mScrollY;
456 final int count = getChildCount();
457
458 boolean found = false;
459 for (int i = count - 1; i >= 0; i--) {
460 final View child = getChildAt(i);
461
462 if ((child.getVisibility()) == VISIBLE || child.getAnimation() != null) {
463 child.getHitRect(frame);
464 if (frame.contains(x, y)) {
465 final LayoutParams lp = (LayoutParams) child.getLayoutParams();
466 cellInfo.cell = child;
467 cellInfo.cellX = lp.cellX;
468 cellInfo.cellY = lp.cellY;
469 cellInfo.spanX = lp.cellHSpan;
470 cellInfo.spanY = lp.cellVSpan;
471 cellInfo.valid = true;
472 found = true;
Michael Jurkaaf442092010-06-10 17:01:57 -0700473 break;
474 }
475 }
476 }
Winson Chungaafa03c2010-06-11 17:34:16 -0700477
Michael Jurkaaf442092010-06-10 17:01:57 -0700478 if (!found) {
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700479 final int cellXY[] = mTmpCellXY;
Michael Jurkaaf442092010-06-10 17:01:57 -0700480 pointToCellExact(x, y, cellXY);
481
Michael Jurkaaf442092010-06-10 17:01:57 -0700482 cellInfo.cell = null;
483 cellInfo.cellX = cellXY[0];
484 cellInfo.cellY = cellXY[1];
485 cellInfo.spanX = 1;
486 cellInfo.spanY = 1;
Michael Jurka0280c3b2010-09-17 15:00:07 -0700487 cellInfo.valid = cellXY[0] >= 0 && cellXY[1] >= 0 && cellXY[0] < mCountX &&
488 cellXY[1] < mCountY && !mOccupied[cellXY[0]][cellXY[1]];
Michael Jurkaaf442092010-06-10 17:01:57 -0700489 }
490 setTag(cellInfo);
491 }
492
Winson Chungaafa03c2010-06-11 17:34:16 -0700493
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800494 @Override
495 public boolean onInterceptTouchEvent(MotionEvent ev) {
Michael Jurkadee05892010-07-27 10:01:56 -0700496 if (mInterceptTouchListener != null && mInterceptTouchListener.onTouch(this, ev)) {
497 return true;
498 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800499 final int action = ev.getAction();
500 final CellInfo cellInfo = mCellInfo;
501
502 if (action == MotionEvent.ACTION_DOWN) {
Michael Jurkaaf442092010-06-10 17:01:57 -0700503 setTagToCellInfoForPoint((int) ev.getX(), (int) ev.getY());
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800504 } else if (action == MotionEvent.ACTION_UP) {
505 cellInfo.cell = null;
506 cellInfo.cellX = -1;
507 cellInfo.cellY = -1;
508 cellInfo.spanX = 0;
509 cellInfo.spanY = 0;
510 cellInfo.valid = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800511 setTag(cellInfo);
512 }
513
514 return false;
515 }
516
517 @Override
518 public CellInfo getTag() {
Michael Jurka0280c3b2010-09-17 15:00:07 -0700519 return (CellInfo) super.getTag();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800520 }
521
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700522 /**
523 * Check if the row 'y' is empty from columns 'left' to 'right', inclusive.
524 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800525 private static boolean isRowEmpty(int y, int left, int right, boolean[][] occupied) {
526 for (int x = left; x <= right; x++) {
527 if (occupied[x][y]) {
528 return false;
529 }
530 }
531 return true;
532 }
533
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800534 /**
Winson Chungaafa03c2010-06-11 17:34:16 -0700535 * Given a point, return the cell that strictly encloses that point
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800536 * @param x X coordinate of the point
537 * @param y Y coordinate of the point
538 * @param result Array of 2 ints to hold the x and y coordinate of the cell
539 */
540 void pointToCellExact(int x, int y, int[] result) {
Winson Chungaafa03c2010-06-11 17:34:16 -0700541 final int hStartPadding = getLeftPadding();
542 final int vStartPadding = getTopPadding();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800543
544 result[0] = (x - hStartPadding) / (mCellWidth + mWidthGap);
545 result[1] = (y - vStartPadding) / (mCellHeight + mHeightGap);
546
Adam Cohend22015c2010-07-26 22:02:18 -0700547 final int xAxis = mCountX;
548 final int yAxis = mCountY;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800549
550 if (result[0] < 0) result[0] = 0;
551 if (result[0] >= xAxis) result[0] = xAxis - 1;
552 if (result[1] < 0) result[1] = 0;
553 if (result[1] >= yAxis) result[1] = yAxis - 1;
554 }
Winson Chungaafa03c2010-06-11 17:34:16 -0700555
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800556 /**
557 * Given a point, return the cell that most closely encloses that point
558 * @param x X coordinate of the point
559 * @param y Y coordinate of the point
560 * @param result Array of 2 ints to hold the x and y coordinate of the cell
561 */
562 void pointToCellRounded(int x, int y, int[] result) {
563 pointToCellExact(x + (mCellWidth / 2), y + (mCellHeight / 2), result);
564 }
565
566 /**
567 * Given a cell coordinate, return the point that represents the upper left corner of that cell
Winson Chungaafa03c2010-06-11 17:34:16 -0700568 *
569 * @param cellX X coordinate of the cell
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800570 * @param cellY Y coordinate of the cell
Winson Chungaafa03c2010-06-11 17:34:16 -0700571 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800572 * @param result Array of 2 ints to hold the x and y coordinate of the point
573 */
574 void cellToPoint(int cellX, int cellY, int[] result) {
Winson Chungaafa03c2010-06-11 17:34:16 -0700575 final int hStartPadding = getLeftPadding();
576 final int vStartPadding = getTopPadding();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800577
578 result[0] = hStartPadding + cellX * (mCellWidth + mWidthGap);
579 result[1] = vStartPadding + cellY * (mCellHeight + mHeightGap);
580 }
581
Romain Guy84f296c2009-11-04 15:00:44 -0800582 int getCellWidth() {
583 return mCellWidth;
584 }
585
586 int getCellHeight() {
587 return mCellHeight;
588 }
589
Romain Guy1a304a12009-11-10 00:02:32 -0800590 int getLeftPadding() {
Winson Chungaafa03c2010-06-11 17:34:16 -0700591 return mLeftPadding;
Romain Guy1a304a12009-11-10 00:02:32 -0800592 }
593
594 int getTopPadding() {
Winson Chungaafa03c2010-06-11 17:34:16 -0700595 return mTopPadding;
Romain Guy1a304a12009-11-10 00:02:32 -0800596 }
597
598 int getRightPadding() {
Winson Chungaafa03c2010-06-11 17:34:16 -0700599 return mRightPadding;
Romain Guy1a304a12009-11-10 00:02:32 -0800600 }
601
602 int getBottomPadding() {
Winson Chungaafa03c2010-06-11 17:34:16 -0700603 return mBottomPadding;
Romain Guy1a304a12009-11-10 00:02:32 -0800604 }
605
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800606 @Override
607 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
608 // TODO: currently ignoring padding
Winson Chungaafa03c2010-06-11 17:34:16 -0700609
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800610 int widthSpecMode = MeasureSpec.getMode(widthMeasureSpec);
Winson Chungaafa03c2010-06-11 17:34:16 -0700611 int widthSpecSize = MeasureSpec.getSize(widthMeasureSpec);
612
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800613 int heightSpecMode = MeasureSpec.getMode(heightMeasureSpec);
614 int heightSpecSize = MeasureSpec.getSize(heightMeasureSpec);
Winson Chungaafa03c2010-06-11 17:34:16 -0700615
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800616 if (widthSpecMode == MeasureSpec.UNSPECIFIED || heightSpecMode == MeasureSpec.UNSPECIFIED) {
617 throw new RuntimeException("CellLayout cannot have UNSPECIFIED dimensions");
618 }
619
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800620 final int cellWidth = mCellWidth;
621 final int cellHeight = mCellHeight;
622
Adam Cohend22015c2010-07-26 22:02:18 -0700623 int numWidthGaps = mCountX - 1;
624 int numHeightGaps = mCountY - 1;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800625
Michael Jurka0280c3b2010-09-17 15:00:07 -0700626 int vSpaceLeft = heightSpecSize - mTopPadding - mBottomPadding - (cellHeight * mCountY);
Adam Cohend22015c2010-07-26 22:02:18 -0700627 mHeightGap = vSpaceLeft / numHeightGaps;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800628
Michael Jurka0280c3b2010-09-17 15:00:07 -0700629 int hSpaceLeft = widthSpecSize - mLeftPadding - mRightPadding - (cellWidth * mCountX);
Adam Cohend22015c2010-07-26 22:02:18 -0700630 mWidthGap = hSpaceLeft / numWidthGaps;
Winson Chungaafa03c2010-06-11 17:34:16 -0700631
Michael Jurka5f1c5092010-09-03 14:15:02 -0700632 // center it around the min gaps
633 int minGap = Math.min(mWidthGap, mHeightGap);
634 mWidthGap = mHeightGap = minGap;
635
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800636 int count = getChildCount();
637
638 for (int i = 0; i < count; i++) {
639 View child = getChildAt(i);
640 LayoutParams lp = (LayoutParams) child.getLayoutParams();
Winson Chungaafa03c2010-06-11 17:34:16 -0700641 lp.setup(cellWidth, cellHeight, mWidthGap, mHeightGap,
642 mLeftPadding, mTopPadding);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800643
Michael Jurka0280c3b2010-09-17 15:00:07 -0700644 int childWidthMeasureSpec = MeasureSpec.makeMeasureSpec(lp.width, MeasureSpec.EXACTLY);
Winson Chungaafa03c2010-06-11 17:34:16 -0700645 int childheightMeasureSpec = MeasureSpec.makeMeasureSpec(lp.height,
646 MeasureSpec.EXACTLY);
647
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800648 child.measure(childWidthMeasureSpec, childheightMeasureSpec);
649 }
Michael Jurka5f1c5092010-09-03 14:15:02 -0700650 if (widthSpecMode == MeasureSpec.AT_MOST) {
651 int newWidth = mLeftPadding + mRightPadding + (mCountX * cellWidth) +
652 ((mCountX - 1) * minGap);
653 int newHeight = mTopPadding + mBottomPadding + (mCountY * cellHeight) +
654 ((mCountY - 1) * minGap);
655 setMeasuredDimension(newWidth, newHeight);
656 } else if (widthSpecMode == MeasureSpec.EXACTLY) {
657 setMeasuredDimension(widthSpecSize, heightSpecSize);
658 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800659 }
660
661 @Override
Michael Jurka28750fb2010-09-24 17:43:49 -0700662 protected void onLayout(boolean changed, int l, int t, int r, int b) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800663 int count = getChildCount();
664
665 for (int i = 0; i < count; i++) {
666 View child = getChildAt(i);
667 if (child.getVisibility() != GONE) {
668
669 CellLayout.LayoutParams lp = (CellLayout.LayoutParams) child.getLayoutParams();
670
671 int childLeft = lp.x;
672 int childTop = lp.y;
673 child.layout(childLeft, childTop, childLeft + lp.width, childTop + lp.height);
Romain Guy84f296c2009-11-04 15:00:44 -0800674
675 if (lp.dropped) {
676 lp.dropped = false;
677
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700678 final int[] cellXY = mTmpCellXY;
Romain Guy06762ab2010-01-25 16:51:08 -0800679 getLocationOnScreen(cellXY);
Romain Guy84f296c2009-11-04 15:00:44 -0800680 mWallpaperManager.sendWallpaperCommand(getWindowToken(), "android.home.drop",
Romain Guy06762ab2010-01-25 16:51:08 -0800681 cellXY[0] + childLeft + lp.width / 2,
682 cellXY[1] + childTop + lp.height / 2, 0, null);
Romain Guy84f296c2009-11-04 15:00:44 -0800683 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800684 }
685 }
686 }
687
688 @Override
Michael Jurkadee05892010-07-27 10:01:56 -0700689 protected void onSizeChanged(int w, int h, int oldw, int oldh) {
690 super.onSizeChanged(w, h, oldw, oldh);
Michael Jurka5f1c5092010-09-03 14:15:02 -0700691 if (mBackground != null) {
Patrick Dubroy1262e362010-10-06 15:49:50 -0700692 mBackground.setBounds(0, 0, w, h);
693 }
694 if (mBackgroundHover != null) {
695 mBackgroundHover.setBounds(0, 0, w, h);
Michael Jurkaa63c4522010-08-19 13:52:27 -0700696 }
Adam Cohenf34bab52010-09-30 14:11:56 -0700697 if (mBackgroundMiniHover != null) {
Patrick Dubroy1262e362010-10-06 15:49:50 -0700698 mBackgroundMiniHover.setBounds(0, 0, w, h);
Adam Cohenf34bab52010-09-30 14:11:56 -0700699 }
700 if (mBackgroundMini != null) {
Patrick Dubroy1262e362010-10-06 15:49:50 -0700701 mBackgroundMini.setBounds(0, 0, w, h);
Michael Jurkaa63c4522010-08-19 13:52:27 -0700702 }
Michael Jurka3e7c7632010-10-02 16:01:03 -0700703 if (mBackgroundMiniAcceptsDrops != null) {
704 mBackgroundMiniAcceptsDrops.setBounds(0, 0, w, h);
705 }
Michael Jurkadee05892010-07-27 10:01:56 -0700706 }
707
708 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800709 protected void setChildrenDrawingCacheEnabled(boolean enabled) {
710 final int count = getChildCount();
711 for (int i = 0; i < count; i++) {
712 final View view = getChildAt(i);
713 view.setDrawingCacheEnabled(enabled);
714 // Update the drawing caches
Adam Powellfefa0ce2010-05-03 10:23:50 -0700715 view.buildDrawingCache(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800716 }
717 }
718
719 @Override
720 protected void setChildrenDrawnWithCacheEnabled(boolean enabled) {
721 super.setChildrenDrawnWithCacheEnabled(enabled);
722 }
723
Michael Jurka5f1c5092010-09-03 14:15:02 -0700724 public float getBackgroundAlpha() {
725 return mBackgroundAlpha;
Michael Jurkadee05892010-07-27 10:01:56 -0700726 }
727
Michael Jurka5f1c5092010-09-03 14:15:02 -0700728 public void setBackgroundAlpha(float alpha) {
729 mBackgroundAlpha = alpha;
Michael Jurka0142d492010-08-25 17:46:15 -0700730 invalidate();
Michael Jurkadee05892010-07-27 10:01:56 -0700731 }
732
Michael Jurka5f1c5092010-09-03 14:15:02 -0700733 // Need to return true to let the view system know we know how to handle alpha-- this is
734 // because when our children have an alpha of 0.0f, they are still rendering their "dimmed"
735 // versions
736 @Override
737 protected boolean onSetAlpha(int alpha) {
738 return true;
739 }
740
741 public void setAlpha(float alpha) {
742 setChildrenAlpha(alpha);
743 super.setAlpha(alpha);
744 }
745
Michael Jurkadee05892010-07-27 10:01:56 -0700746 private void setChildrenAlpha(float alpha) {
Michael Jurka0142d492010-08-25 17:46:15 -0700747 final int childCount = getChildCount();
748 for (int i = 0; i < childCount; i++) {
Michael Jurkadee05892010-07-27 10:01:56 -0700749 getChildAt(i).setAlpha(alpha);
750 }
751 }
752
Michael Jurka0280c3b2010-09-17 15:00:07 -0700753 private boolean isVacantIgnoring(
754 int originX, int originY, int spanX, int spanY, View ignoreView) {
755 if (ignoreView != null) {
756 markCellsAsUnoccupiedForView(ignoreView);
757 }
Michael Jurka28750fb2010-09-24 17:43:49 -0700758 boolean isVacant = true;
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700759 for (int i = 0; i < spanY; i++) {
760 if (!isRowEmpty(originY + i, originX, originX + spanX - 1, mOccupied)) {
Michael Jurka28750fb2010-09-24 17:43:49 -0700761 isVacant = false;
762 break;
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700763 }
764 }
Michael Jurka0280c3b2010-09-17 15:00:07 -0700765 if (ignoreView != null) {
766 markCellsAsOccupiedForView(ignoreView);
767 }
Michael Jurka28750fb2010-09-24 17:43:49 -0700768 return isVacant;
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700769 }
770
Michael Jurka0280c3b2010-09-17 15:00:07 -0700771 private boolean isVacant(int originX, int originY, int spanX, int spanY) {
772 return isVacantIgnoring(originX, originY, spanX, spanY, null);
773 }
774
Patrick Dubroy440c3602010-07-13 17:50:32 -0700775 public View getChildAt(int x, int y) {
776 final int count = getChildCount();
777 for (int i = 0; i < count; i++) {
778 View child = getChildAt(i);
779 LayoutParams lp = (LayoutParams) child.getLayoutParams();
780
781 if ((lp.cellX <= x) && (x < lp.cellX + lp.cellHSpan) &&
782 (lp.cellY <= y) && (y < lp.cellY + lp.cellHSpan)) {
783 return child;
784 }
785 }
786 return null;
787 }
788
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700789 /**
Patrick Dubroy8f86ddc2010-07-16 13:55:32 -0700790 * Estimate the size that a child with the given dimensions will take in the layout.
791 */
792 void estimateChildSize(int minWidth, int minHeight, int[] result) {
793 // Assuming it's placed at 0, 0, find where the bottom right cell will land
794 rectToCell(minWidth, minHeight, result);
795
796 // Then figure out the rect it will occupy
797 cellToRect(0, 0, result[0], result[1], mRectF);
798 result[0] = (int)mRectF.width();
799 result[1] = (int)mRectF.height();
800 }
801
802 /**
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700803 * Estimate where the top left cell of the dragged item will land if it is dropped.
804 *
805 * @param originX The X value of the top left corner of the item
806 * @param originY The Y value of the top left corner of the item
807 * @param spanX The number of horizontal cells that the item spans
808 * @param spanY The number of vertical cells that the item spans
809 * @param result The estimated drop cell X and Y.
810 */
811 void estimateDropCell(int originX, int originY, int spanX, int spanY, int[] result) {
Adam Cohend22015c2010-07-26 22:02:18 -0700812 final int countX = mCountX;
813 final int countY = mCountY;
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700814
Michael Jurkaa63c4522010-08-19 13:52:27 -0700815 // pointToCellRounded takes the top left of a cell but will pad that with
816 // cellWidth/2 and cellHeight/2 when finding the matching cell
817 pointToCellRounded(originX, originY, result);
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700818
819 // If the item isn't fully on this screen, snap to the edges
820 int rightOverhang = result[0] + spanX - countX;
821 if (rightOverhang > 0) {
822 result[0] -= rightOverhang; // Snap to right
823 }
824 result[0] = Math.max(0, result[0]); // Snap to left
825 int bottomOverhang = result[1] + spanY - countY;
826 if (bottomOverhang > 0) {
827 result[1] -= bottomOverhang; // Snap to bottom
828 }
829 result[1] = Math.max(0, result[1]); // Snap to top
830 }
831
Joe Onorato4be866d2010-10-10 11:26:02 -0700832 void visualizeDropLocation(
833 View v, Bitmap dragOutline, int originX, int originY, int spanX, int spanY) {
834
835 final int[] nearest = findNearestVacantArea(originX, originY, spanX, spanY, v, mDragCell);
836 mDragCenter.set(originX + (v.getWidth() / 2), originY + (v.getHeight() / 2));
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700837
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700838 if (nearest != null) {
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700839 // Find the top left corner of the rect the object will occupy
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700840 final int[] topLeft = mTmpPoint;
841 cellToPoint(nearest[0], nearest[1], topLeft);
842
Joe Onorato4be866d2010-10-10 11:26:02 -0700843 int left = topLeft[0];
844 int top = topLeft[1];
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700845
Joe Onorato4be866d2010-10-10 11:26:02 -0700846 if (v.getParent() instanceof CellLayout) {
847 LayoutParams lp = (LayoutParams) v.getLayoutParams();
848 left += lp.leftMargin;
849 top += lp.topMargin;
850 }
Winson Chung150fbab2010-09-29 17:14:26 -0700851
Joe Onorato4be866d2010-10-10 11:26:02 -0700852 // Offsets due to the size difference between the View and the dragOutline
853 left += (v.getWidth() - dragOutline.getWidth()) / 2;
854 top += (v.getHeight() - dragOutline.getHeight()) / 2;
Winson Chung150fbab2010-09-29 17:14:26 -0700855
Joe Onorato4be866d2010-10-10 11:26:02 -0700856 final int oldIndex = mDragOutlineCurrent;
857 final Point lastPoint = mDragOutlines[oldIndex];
858 if (lastPoint.x != left || lastPoint.y != top) {
859 mDragOutlineCurrent = (oldIndex + 1) % mDragOutlines.length;
Winson Chung150fbab2010-09-29 17:14:26 -0700860
Joe Onorato4be866d2010-10-10 11:26:02 -0700861 mDragOutlines[mDragOutlineCurrent].set(left, top);
Winson Chung150fbab2010-09-29 17:14:26 -0700862
Joe Onorato4be866d2010-10-10 11:26:02 -0700863 mDragOutlineAnims[oldIndex].animateOut();
864 mDragOutlineAnims[mDragOutlineCurrent].setTag(dragOutline);
865 mDragOutlineAnims[mDragOutlineCurrent].animateIn();
Winson Chung150fbab2010-09-29 17:14:26 -0700866 }
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700867 }
Patrick Dubroy49250ad2010-10-08 15:33:52 -0700868
869 // If we are drawing crosshairs, the entire CellLayout needs to be invalidated
870 if (mCrosshairsDrawable != null) {
871 invalidate();
872 }
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700873 }
874
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800875 /**
Jeff Sharkey70864282009-04-07 21:08:40 -0700876 * Find a vacant area that will fit the given bounds nearest the requested
877 * cell location. Uses Euclidean distance to score multiple vacant areas.
Winson Chungaafa03c2010-06-11 17:34:16 -0700878 *
Romain Guy51afc022009-05-04 18:03:43 -0700879 * @param pixelX The X location at which you want to search for a vacant area.
880 * @param pixelY The Y location at which you want to search for a vacant area.
Jeff Sharkey70864282009-04-07 21:08:40 -0700881 * @param spanX Horizontal span of the object.
882 * @param spanY Vertical span of the object.
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700883 * @param result Array in which to place the result, or null (in which case a new array will
884 * be allocated)
Jeff Sharkey70864282009-04-07 21:08:40 -0700885 * @return The X, Y cell of a vacant area that can contain this object,
886 * nearest the requested location.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800887 */
Michael Jurka6a1435d2010-09-27 17:35:12 -0700888 int[] findNearestVacantArea(
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700889 int pixelX, int pixelY, int spanX, int spanY, int[] result) {
890 return findNearestVacantArea(pixelX, pixelY, spanX, spanY, null, result);
Michael Jurka6a1435d2010-09-27 17:35:12 -0700891 }
Winson Chungaafa03c2010-06-11 17:34:16 -0700892
Michael Jurka6a1435d2010-09-27 17:35:12 -0700893 /**
894 * Find a vacant area that will fit the given bounds nearest the requested
895 * cell location. Uses Euclidean distance to score multiple vacant areas.
896 *
897 * @param pixelX The X location at which you want to search for a vacant area.
898 * @param pixelY The Y location at which you want to search for a vacant area.
899 * @param spanX Horizontal span of the object.
900 * @param spanY Vertical span of the object.
Michael Jurka6a1435d2010-09-27 17:35:12 -0700901 * @param ignoreView Considers space occupied by this view as unoccupied
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700902 * @param result Previously returned value to possibly recycle.
Michael Jurka6a1435d2010-09-27 17:35:12 -0700903 * @return The X, Y cell of a vacant area that can contain this object,
904 * nearest the requested location.
905 */
906 int[] findNearestVacantArea(
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700907 int pixelX, int pixelY, int spanX, int spanY, View ignoreView, int[] result) {
Michael Jurkac6ee42e2010-09-30 12:04:50 -0700908 // mark space take by ignoreView as available (method checks if ignoreView is null)
909 markCellsAsUnoccupiedForView(ignoreView);
910
Jeff Sharkey70864282009-04-07 21:08:40 -0700911 // Keep track of best-scoring drop area
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700912 final int[] bestXY = result != null ? result : new int[2];
Jeff Sharkey70864282009-04-07 21:08:40 -0700913 double bestDistance = Double.MAX_VALUE;
Winson Chungaafa03c2010-06-11 17:34:16 -0700914
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700915 final int countX = mCountX;
916 final int countY = mCountY;
917 final boolean[][] occupied = mOccupied;
918
919 for (int x = 0; x < countX - (spanX - 1); x++) {
Michael Jurkac28de512010-08-13 11:27:44 -0700920 inner:
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700921 for (int y = 0; y < countY - (spanY - 1); y++) {
Michael Jurkac28de512010-08-13 11:27:44 -0700922 for (int i = 0; i < spanX; i++) {
923 for (int j = 0; j < spanY; j++) {
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700924 if (occupied[x + i][y + j]) {
Michael Jurkac28de512010-08-13 11:27:44 -0700925 // small optimization: we can skip to below the row we just found
926 // an occupied cell
927 y += j;
928 continue inner;
929 }
930 }
931 }
932 final int[] cellXY = mTmpCellXY;
933 cellToPoint(x, y, cellXY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800934
Michael Jurkac28de512010-08-13 11:27:44 -0700935 double distance = Math.sqrt(Math.pow(cellXY[0] - pixelX, 2)
936 + Math.pow(cellXY[1] - pixelY, 2));
937 if (distance <= bestDistance) {
938 bestDistance = distance;
939 bestXY[0] = x;
940 bestXY[1] = y;
941 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800942 }
943 }
Michael Jurkac6ee42e2010-09-30 12:04:50 -0700944 // re-mark space taken by ignoreView as occupied
945 markCellsAsOccupiedForView(ignoreView);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800946
Winson Chungaafa03c2010-06-11 17:34:16 -0700947 // Return null if no suitable location found
Jeff Sharkey70864282009-04-07 21:08:40 -0700948 if (bestDistance < Double.MAX_VALUE) {
949 return bestXY;
950 } else {
951 return null;
952 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800953 }
Winson Chungaafa03c2010-06-11 17:34:16 -0700954
Michael Jurka0280c3b2010-09-17 15:00:07 -0700955 boolean existsEmptyCell() {
956 return findCellForSpan(null, 1, 1);
957 }
958
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800959 /**
Michael Jurka0280c3b2010-09-17 15:00:07 -0700960 * Finds the upper-left coordinate of the first rectangle in the grid that can
961 * hold a cell of the specified dimensions. If intersectX and intersectY are not -1,
962 * then this method will only return coordinates for rectangles that contain the cell
963 * (intersectX, intersectY)
964 *
965 * @param cellXY The array that will contain the position of a vacant cell if such a cell
966 * can be found.
967 * @param spanX The horizontal span of the cell we want to find.
968 * @param spanY The vertical span of the cell we want to find.
969 *
970 * @return True if a vacant cell of the specified dimension was found, false otherwise.
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700971 */
Michael Jurka0280c3b2010-09-17 15:00:07 -0700972 boolean findCellForSpan(int[] cellXY, int spanX, int spanY) {
973 return findCellForSpanThatIntersectsIgnoring(cellXY, spanX, spanY, -1, -1, null);
974 }
975
976 /**
977 * Like above, but ignores any cells occupied by the item "ignoreView"
978 *
979 * @param cellXY The array that will contain the position of a vacant cell if such a cell
980 * can be found.
981 * @param spanX The horizontal span of the cell we want to find.
982 * @param spanY The vertical span of the cell we want to find.
983 * @param ignoreView The home screen item we should treat as not occupying any space
984 * @return
985 */
986 boolean findCellForSpanIgnoring(int[] cellXY, int spanX, int spanY, View ignoreView) {
987 return findCellForSpanThatIntersectsIgnoring(cellXY, spanX, spanY, -1, -1, ignoreView);
988 }
989
990 /**
991 * Like above, but if intersectX and intersectY are not -1, then this method will try to
992 * return coordinates for rectangles that contain the cell [intersectX, intersectY]
993 *
994 * @param spanX The horizontal span of the cell we want to find.
995 * @param spanY The vertical span of the cell we want to find.
996 * @param ignoreView The home screen item we should treat as not occupying any space
997 * @param intersectX The X coordinate of the cell that we should try to overlap
998 * @param intersectX The Y coordinate of the cell that we should try to overlap
999 *
1000 * @return True if a vacant cell of the specified dimension was found, false otherwise.
1001 */
1002 boolean findCellForSpanThatIntersects(int[] cellXY, int spanX, int spanY,
1003 int intersectX, int intersectY) {
1004 return findCellForSpanThatIntersectsIgnoring(
1005 cellXY, spanX, spanY, intersectX, intersectY, null);
1006 }
1007
1008 /**
1009 * The superset of the above two methods
1010 */
1011 boolean findCellForSpanThatIntersectsIgnoring(int[] cellXY, int spanX, int spanY,
1012 int intersectX, int intersectY, View ignoreView) {
Michael Jurkac6ee42e2010-09-30 12:04:50 -07001013 // mark space take by ignoreView as available (method checks if ignoreView is null)
1014 markCellsAsUnoccupiedForView(ignoreView);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001015
Michael Jurka28750fb2010-09-24 17:43:49 -07001016 boolean foundCell = false;
Michael Jurka0280c3b2010-09-17 15:00:07 -07001017 while (true) {
1018 int startX = 0;
1019 if (intersectX >= 0) {
1020 startX = Math.max(startX, intersectX - (spanX - 1));
1021 }
1022 int endX = mCountX - (spanX - 1);
1023 if (intersectX >= 0) {
1024 endX = Math.min(endX, intersectX + (spanX - 1) + (spanX == 1 ? 1 : 0));
1025 }
1026 int startY = 0;
1027 if (intersectY >= 0) {
1028 startY = Math.max(startY, intersectY - (spanY - 1));
1029 }
1030 int endY = mCountY - (spanY - 1);
1031 if (intersectY >= 0) {
1032 endY = Math.min(endY, intersectY + (spanY - 1) + (spanY == 1 ? 1 : 0));
1033 }
1034
1035 for (int x = startX; x < endX; x++) {
1036 inner:
1037 for (int y = startY; y < endY; y++) {
1038 for (int i = 0; i < spanX; i++) {
1039 for (int j = 0; j < spanY; j++) {
1040 if (mOccupied[x + i][y + j]) {
1041 // small optimization: we can skip to below the row we just found
1042 // an occupied cell
1043 y += j;
1044 continue inner;
1045 }
1046 }
1047 }
1048 if (cellXY != null) {
1049 cellXY[0] = x;
1050 cellXY[1] = y;
1051 }
Michael Jurka28750fb2010-09-24 17:43:49 -07001052 foundCell = true;
1053 break;
Michael Jurka0280c3b2010-09-17 15:00:07 -07001054 }
1055 }
1056 if (intersectX == -1 && intersectY == -1) {
1057 break;
1058 } else {
1059 // if we failed to find anything, try again but without any requirements of
1060 // intersecting
1061 intersectX = -1;
1062 intersectY = -1;
1063 continue;
1064 }
1065 }
1066
Michael Jurkac6ee42e2010-09-30 12:04:50 -07001067 // re-mark space taken by ignoreView as occupied
1068 markCellsAsOccupiedForView(ignoreView);
Michael Jurka28750fb2010-09-24 17:43:49 -07001069 return foundCell;
Michael Jurka0280c3b2010-09-17 15:00:07 -07001070 }
1071
1072 /**
1073 * Called when drag has left this CellLayout or has been completed (successfully or not)
1074 */
1075 void onDragExit() {
Joe Onorato4be866d2010-10-10 11:26:02 -07001076 // This can actually be called when we aren't in a drag, e.g. when adding a new
1077 // item to this layout via the customize drawer.
1078 // Guard against that case.
1079 if (mDragging) {
1080 mDragging = false;
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001081
Joe Onorato4be866d2010-10-10 11:26:02 -07001082 // Invalidate the drag data
1083 mDragCell[0] = -1;
1084 mDragCell[1] = -1;
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001085
Joe Onorato4be866d2010-10-10 11:26:02 -07001086 setHover(false);
1087
1088 // Fade out the drag indicators
1089 if (mCrosshairsAnimator != null) {
1090 mCrosshairsAnimator.animateOut();
1091 }
1092
1093 final int prev = mDragOutlineCurrent;
1094 mDragOutlineAnims[prev].animateOut();
1095 mDragOutlineCurrent = (prev + 1) % mDragOutlines.length;
1096 mDragOutlines[mDragOutlineCurrent].set(-1, -1);
1097 mDragOutlineAlphas[mDragOutlineCurrent] = 0;
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001098 }
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001099 }
1100
1101 /**
Winson Chungaafa03c2010-06-11 17:34:16 -07001102 * Mark a child as having been dropped.
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001103 * At the beginning of the drag operation, the child may have been on another
1104 * screen, but it is reparented before this method is called.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001105 *
1106 * @param child The child that is being dropped
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001107 */
Winson Chungaafa03c2010-06-11 17:34:16 -07001108 void onDropChild(View child) {
Romain Guyd94533d2009-08-17 10:01:15 -07001109 if (child != null) {
1110 LayoutParams lp = (LayoutParams) child.getLayoutParams();
Romain Guyd94533d2009-08-17 10:01:15 -07001111 lp.isDragging = false;
Romain Guy84f296c2009-11-04 15:00:44 -08001112 lp.dropped = true;
Romain Guyd94533d2009-08-17 10:01:15 -07001113 child.requestLayout();
Romain Guyd94533d2009-08-17 10:01:15 -07001114 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001115 onDragExit();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001116 }
1117
1118 void onDropAborted(View child) {
1119 if (child != null) {
1120 ((LayoutParams) child.getLayoutParams()).isDragging = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001121 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001122 onDragExit();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001123 }
1124
1125 /**
1126 * Start dragging the specified child
Winson Chungaafa03c2010-06-11 17:34:16 -07001127 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001128 * @param child The child that is being dragged
1129 */
1130 void onDragChild(View child) {
1131 LayoutParams lp = (LayoutParams) child.getLayoutParams();
1132 lp.isDragging = true;
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001133 }
1134
1135 /**
1136 * A drag event has begun over this layout.
1137 * It may have begun over this layout (in which case onDragChild is called first),
1138 * or it may have begun on another layout.
1139 */
1140 void onDragEnter(View dragView) {
Joe Onorato4be866d2010-10-10 11:26:02 -07001141 if (mDragging) {
1142// Log.d(TAG, "Received onDragEnter while drag still active");
1143 }
1144 mDragging = true;
1145
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001146 // Fade in the drag indicators
1147 if (mCrosshairsAnimator != null) {
Patrick Dubroy49250ad2010-10-08 15:33:52 -07001148 mCrosshairsAnimator.animateIn();
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001149 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001150 }
Winson Chungaafa03c2010-06-11 17:34:16 -07001151
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001152 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001153 * Computes a bounding rectangle for a range of cells
Winson Chungaafa03c2010-06-11 17:34:16 -07001154 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001155 * @param cellX X coordinate of upper left corner expressed as a cell position
1156 * @param cellY Y coordinate of upper left corner expressed as a cell position
Winson Chungaafa03c2010-06-11 17:34:16 -07001157 * @param cellHSpan Width in cells
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001158 * @param cellVSpan Height in cells
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001159 * @param resultRect Rect into which to put the results
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001160 */
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001161 public void cellToRect(int cellX, int cellY, int cellHSpan, int cellVSpan, RectF resultRect) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001162 final int cellWidth = mCellWidth;
1163 final int cellHeight = mCellHeight;
1164 final int widthGap = mWidthGap;
1165 final int heightGap = mHeightGap;
Winson Chungaafa03c2010-06-11 17:34:16 -07001166
1167 final int hStartPadding = getLeftPadding();
1168 final int vStartPadding = getTopPadding();
1169
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001170 int width = cellHSpan * cellWidth + ((cellHSpan - 1) * widthGap);
1171 int height = cellVSpan * cellHeight + ((cellVSpan - 1) * heightGap);
1172
1173 int x = hStartPadding + cellX * (cellWidth + widthGap);
1174 int y = vStartPadding + cellY * (cellHeight + heightGap);
Winson Chungaafa03c2010-06-11 17:34:16 -07001175
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001176 resultRect.set(x, y, x + width, y + height);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001177 }
Winson Chungaafa03c2010-06-11 17:34:16 -07001178
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001179 /**
Winson Chungaafa03c2010-06-11 17:34:16 -07001180 * Computes the required horizontal and vertical cell spans to always
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001181 * fit the given rectangle.
Winson Chungaafa03c2010-06-11 17:34:16 -07001182 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001183 * @param width Width in pixels
1184 * @param height Height in pixels
Patrick Dubroy8f86ddc2010-07-16 13:55:32 -07001185 * @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 -08001186 */
Patrick Dubroy8f86ddc2010-07-16 13:55:32 -07001187 public int[] rectToCell(int width, int height, int[] result) {
Michael Jurka9987a5c2010-10-08 16:58:12 -07001188 return rectToCell(getResources(), width, height, result);
1189 }
1190
1191 public static int[] rectToCell(Resources resources, int width, int height, int[] result) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001192 // Always assume we're working with the smallest span to make sure we
1193 // reserve enough space in both orientations.
Joe Onorato79e56262009-09-21 15:23:04 -04001194 int actualWidth = resources.getDimensionPixelSize(R.dimen.workspace_cell_width);
1195 int actualHeight = resources.getDimensionPixelSize(R.dimen.workspace_cell_height);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001196 int smallerSize = Math.min(actualWidth, actualHeight);
Joe Onorato79e56262009-09-21 15:23:04 -04001197
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001198 // Always round up to next largest cell
1199 int spanX = (width + smallerSize) / smallerSize;
1200 int spanY = (height + smallerSize) / smallerSize;
Joe Onorato79e56262009-09-21 15:23:04 -04001201
Patrick Dubroy8f86ddc2010-07-16 13:55:32 -07001202 if (result == null) {
1203 return new int[] { spanX, spanY };
1204 }
1205 result[0] = spanX;
1206 result[1] = spanY;
1207 return result;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001208 }
1209
1210 /**
1211 * Find the first vacant cell, if there is one.
1212 *
1213 * @param vacant Holds the x and y coordinate of the vacant cell
1214 * @param spanX Horizontal cell span.
1215 * @param spanY Vertical cell span.
Winson Chungaafa03c2010-06-11 17:34:16 -07001216 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001217 * @return True if a vacant cell was found
1218 */
1219 public boolean getVacantCell(int[] vacant, int spanX, int spanY) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001220
Michael Jurka0280c3b2010-09-17 15:00:07 -07001221 return findVacantCell(vacant, spanX, spanY, mCountX, mCountY, mOccupied);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001222 }
1223
1224 static boolean findVacantCell(int[] vacant, int spanX, int spanY,
1225 int xCount, int yCount, boolean[][] occupied) {
1226
1227 for (int x = 0; x < xCount; x++) {
1228 for (int y = 0; y < yCount; y++) {
1229 boolean available = !occupied[x][y];
1230out: for (int i = x; i < x + spanX - 1 && x < xCount; i++) {
1231 for (int j = y; j < y + spanY - 1 && y < yCount; j++) {
1232 available = available && !occupied[i][j];
1233 if (!available) break out;
1234 }
1235 }
1236
1237 if (available) {
1238 vacant[0] = x;
1239 vacant[1] = y;
1240 return true;
1241 }
1242 }
1243 }
1244
1245 return false;
1246 }
1247
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001248 /**
1249 * Update the array of occupied cells (mOccupied), and return a flattened copy of the array.
1250 */
1251 boolean[] getOccupiedCellsFlattened() {
Adam Cohend22015c2010-07-26 22:02:18 -07001252 final int xCount = mCountX;
1253 final int yCount = mCountY;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001254 final boolean[][] occupied = mOccupied;
1255
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001256 final boolean[] flat = new boolean[xCount * yCount];
1257 for (int y = 0; y < yCount; y++) {
1258 for (int x = 0; x < xCount; x++) {
1259 flat[y * xCount + x] = occupied[x][y];
1260 }
1261 }
1262
1263 return flat;
1264 }
1265
Michael Jurka0280c3b2010-09-17 15:00:07 -07001266 private void clearOccupiedCells() {
1267 for (int x = 0; x < mCountX; x++) {
1268 for (int y = 0; y < mCountY; y++) {
1269 mOccupied[x][y] = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001270 }
1271 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001272 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001273
Michael Jurka0280c3b2010-09-17 15:00:07 -07001274 public void onMove(View view, int newCellX, int newCellY) {
1275 LayoutParams lp = (LayoutParams) view.getLayoutParams();
1276 markCellsAsUnoccupiedForView(view);
1277 markCellsForView(newCellX, newCellY, lp.cellHSpan, lp.cellVSpan, true);
1278 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001279
Michael Jurka0280c3b2010-09-17 15:00:07 -07001280 private void markCellsAsOccupiedForView(View view) {
Michael Jurkac6ee42e2010-09-30 12:04:50 -07001281 if (view == null || view.getParent() != this) return;
Michael Jurka0280c3b2010-09-17 15:00:07 -07001282 LayoutParams lp = (LayoutParams) view.getLayoutParams();
1283 markCellsForView(lp.cellX, lp.cellY, lp.cellHSpan, lp.cellVSpan, true);
1284 }
1285
1286 private void markCellsAsUnoccupiedForView(View view) {
Michael Jurkac6ee42e2010-09-30 12:04:50 -07001287 if (view == null || view.getParent() != this) return;
Michael Jurka0280c3b2010-09-17 15:00:07 -07001288 LayoutParams lp = (LayoutParams) view.getLayoutParams();
1289 markCellsForView(lp.cellX, lp.cellY, lp.cellHSpan, lp.cellVSpan, false);
1290 }
1291
1292 private void markCellsForView(int cellX, int cellY, int spanX, int spanY, boolean value) {
1293 for (int x = cellX; x < cellX + spanX && x < mCountX; x++) {
1294 for (int y = cellY; y < cellY + spanY && y < mCountY; y++) {
1295 mOccupied[x][y] = value;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001296 }
1297 }
1298 }
1299
1300 @Override
1301 public ViewGroup.LayoutParams generateLayoutParams(AttributeSet attrs) {
1302 return new CellLayout.LayoutParams(getContext(), attrs);
1303 }
1304
1305 @Override
1306 protected boolean checkLayoutParams(ViewGroup.LayoutParams p) {
1307 return p instanceof CellLayout.LayoutParams;
1308 }
1309
1310 @Override
1311 protected ViewGroup.LayoutParams generateLayoutParams(ViewGroup.LayoutParams p) {
1312 return new CellLayout.LayoutParams(p);
1313 }
1314
Winson Chungaafa03c2010-06-11 17:34:16 -07001315 public static class CellLayoutAnimationController extends LayoutAnimationController {
1316 public CellLayoutAnimationController(Animation animation, float delay) {
1317 super(animation, delay);
1318 }
1319
1320 @Override
1321 protected long getDelayForView(View view) {
1322 return (int) (Math.random() * 150);
1323 }
1324 }
1325
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001326 public static class LayoutParams extends ViewGroup.MarginLayoutParams {
1327 /**
1328 * Horizontal location of the item in the grid.
1329 */
1330 @ViewDebug.ExportedProperty
1331 public int cellX;
1332
1333 /**
1334 * Vertical location of the item in the grid.
1335 */
1336 @ViewDebug.ExportedProperty
1337 public int cellY;
1338
1339 /**
1340 * Number of cells spanned horizontally by the item.
1341 */
1342 @ViewDebug.ExportedProperty
1343 public int cellHSpan;
1344
1345 /**
1346 * Number of cells spanned vertically by the item.
1347 */
1348 @ViewDebug.ExportedProperty
1349 public int cellVSpan;
Winson Chungaafa03c2010-06-11 17:34:16 -07001350
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001351 /**
1352 * Is this item currently being dragged
1353 */
1354 public boolean isDragging;
1355
1356 // X coordinate of the view in the layout.
1357 @ViewDebug.ExportedProperty
1358 int x;
1359 // Y coordinate of the view in the layout.
1360 @ViewDebug.ExportedProperty
1361 int y;
1362
Romain Guy84f296c2009-11-04 15:00:44 -08001363 boolean dropped;
Romain Guyfcb9e712009-10-02 16:06:52 -07001364
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001365 public LayoutParams(Context c, AttributeSet attrs) {
1366 super(c, attrs);
1367 cellHSpan = 1;
1368 cellVSpan = 1;
1369 }
1370
1371 public LayoutParams(ViewGroup.LayoutParams source) {
1372 super(source);
1373 cellHSpan = 1;
1374 cellVSpan = 1;
1375 }
Winson Chungaafa03c2010-06-11 17:34:16 -07001376
1377 public LayoutParams(LayoutParams source) {
1378 super(source);
1379 this.cellX = source.cellX;
1380 this.cellY = source.cellY;
1381 this.cellHSpan = source.cellHSpan;
1382 this.cellVSpan = source.cellVSpan;
1383 }
1384
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001385 public LayoutParams(int cellX, int cellY, int cellHSpan, int cellVSpan) {
Romain Guy8f19cdd2010-01-08 15:07:00 -08001386 super(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001387 this.cellX = cellX;
1388 this.cellY = cellY;
1389 this.cellHSpan = cellHSpan;
1390 this.cellVSpan = cellVSpan;
1391 }
1392
1393 public void setup(int cellWidth, int cellHeight, int widthGap, int heightGap,
1394 int hStartPadding, int vStartPadding) {
Winson Chungaafa03c2010-06-11 17:34:16 -07001395
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001396 final int myCellHSpan = cellHSpan;
1397 final int myCellVSpan = cellVSpan;
1398 final int myCellX = cellX;
1399 final int myCellY = cellY;
Winson Chungaafa03c2010-06-11 17:34:16 -07001400
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001401 width = myCellHSpan * cellWidth + ((myCellHSpan - 1) * widthGap) -
1402 leftMargin - rightMargin;
1403 height = myCellVSpan * cellHeight + ((myCellVSpan - 1) * heightGap) -
1404 topMargin - bottomMargin;
1405
1406 x = hStartPadding + myCellX * (cellWidth + widthGap) + leftMargin;
1407 y = vStartPadding + myCellY * (cellHeight + heightGap) + topMargin;
1408 }
Winson Chungaafa03c2010-06-11 17:34:16 -07001409
1410 public String toString() {
1411 return "(" + this.cellX + ", " + this.cellY + ")";
1412 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001413 }
1414
Michael Jurka0280c3b2010-09-17 15:00:07 -07001415 // This class stores info for two purposes:
1416 // 1. When dragging items (mDragInfo in Workspace), we store the View, its cellX & cellY,
1417 // its spanX, spanY, and the screen it is on
1418 // 2. When long clicking on an empty cell in a CellLayout, we save information about the
1419 // cellX and cellY coordinates and which page was clicked. We then set this as a tag on
1420 // the CellLayout that was long clicked
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001421 static final class CellInfo implements ContextMenu.ContextMenuInfo {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001422 View cell;
Michael Jurkaa63c4522010-08-19 13:52:27 -07001423 int cellX = -1;
1424 int cellY = -1;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001425 int spanX;
1426 int spanY;
1427 int screen;
1428 boolean valid;
1429
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001430 @Override
1431 public String toString() {
Winson Chungaafa03c2010-06-11 17:34:16 -07001432 return "Cell[view=" + (cell == null ? "null" : cell.getClass())
1433 + ", x=" + cellX + ", y=" + cellY + "]";
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001434 }
1435 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001436}