blob: 018a9663af7b753b5c565447f1a55db3aa5001c7 [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];
Chet Haase472b2812010-10-14 07:02:04 -070097 private float[] mDragOutlineAlphas = new float[mDragOutlines.length];
Joe Onorato4be866d2010-10-10 11:26:02 -070098 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);
Chet Haase472b2812010-10-14 07:02:04 -0700178 mCrosshairsAnimator.getAnimator().addUpdateListener(new AnimatorUpdateListener() {
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700179 public void onAnimationUpdate(ValueAnimator animation) {
180 mCrosshairsVisibility = ((Float) animation.getAnimatedValue()).floatValue();
181 CellLayout.this.invalidate();
182 }
183 });
Chet Haase472b2812010-10-14 07:02:04 -0700184 mCrosshairsAnimator.getAnimator().setInterpolator(interp);
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700185
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);
Chet Haase472b2812010-10-14 07:02:04 -0700195 final float fromAlphaValue = 0;
196 final float toAlphaValue = (float)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);
Chet Haase472b2812010-10-14 07:02:04 -0700205 anim.getAnimator().setInterpolator(interp);
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700206 final int thisIndex = i;
Chet Haase472b2812010-10-14 07:02:04 -0700207 anim.getAnimator().addUpdateListener(new AnimatorUpdateListener() {
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700208 public void onAnimationUpdate(ValueAnimator animation) {
Joe Onorato4be866d2010-10-10 11:26:02 -0700209 final Bitmap outline = (Bitmap)anim.getTag();
210
211 // If an animation is started and then stopped very quickly, we can still
212 // get spurious updates we've cleared the tag. Guard against this.
213 if (outline == null) {
Patrick Dubroyfe6bd872010-10-13 17:32:10 -0700214 if (false) {
215 Object val = animation.getAnimatedValue();
216 Log.d(TAG, "anim " + thisIndex + " update: " + val +
217 ", isStopped " + anim.isStopped());
218 }
Joe Onorato4be866d2010-10-10 11:26:02 -0700219 // Try to prevent it from continuing to run
220 animation.cancel();
221 } else {
Chet Haase472b2812010-10-14 07:02:04 -0700222 mDragOutlineAlphas[thisIndex] = (Float) animation.getAnimatedValue();
Joe Onorato4be866d2010-10-10 11:26:02 -0700223 final int left = mDragOutlines[thisIndex].x;
224 final int top = mDragOutlines[thisIndex].y;
225 CellLayout.this.invalidate(left, top,
226 left + outline.getWidth(), top + outline.getHeight());
227 }
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700228 }
229 });
Joe Onorato4be866d2010-10-10 11:26:02 -0700230 // The animation holds a reference to the drag outline bitmap as long is it's
231 // running. This way the bitmap can be GCed when the animations are complete.
Chet Haase472b2812010-10-14 07:02:04 -0700232 anim.getAnimator().addListener(new AnimatorListenerAdapter() {
Joe Onorato4be866d2010-10-10 11:26:02 -0700233 public void onAnimationEnd(Animator animation) {
Chet Haase472b2812010-10-14 07:02:04 -0700234 if ((Float) ((ValueAnimator) animation).getAnimatedValue() == 0f) {
Joe Onorato4be866d2010-10-10 11:26:02 -0700235 anim.setTag(null);
236 }
237 }
238 });
239 mDragOutlineAnims[i] = anim;
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700240 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800241 }
242
Michael Jurkaa63c4522010-08-19 13:52:27 -0700243 public void setHover(boolean value) {
244 if (mHover != value) {
Michael Jurka3e7c7632010-10-02 16:01:03 -0700245 mHover = value;
Michael Jurkaa63c4522010-08-19 13:52:27 -0700246 invalidate();
247 }
Michael Jurkaa63c4522010-08-19 13:52:27 -0700248 }
249
Patrick Dubroy1262e362010-10-06 15:49:50 -0700250 public void drawChildren(Canvas canvas) {
251 super.dispatchDraw(canvas);
252 }
253
Jeff Sharkey83f111d2009-04-20 21:03:13 -0700254 @Override
Patrick Dubroy1262e362010-10-06 15:49:50 -0700255 protected void onDraw(Canvas canvas) {
Michael Jurka3e7c7632010-10-02 16:01:03 -0700256 // When we're large, we are either drawn in a "hover" state (ie when dragging an item to
257 // a neighboring page) or with just a normal background (if backgroundAlpha > 0.0f)
258 // When we're small, we are either drawn normally or in the "accepts drops" state (during
259 // a drag). However, we also drag the mini hover background *over* one of those two
260 // backgrounds
Michael Jurka5f1c5092010-09-03 14:15:02 -0700261 if (mBackgroundAlpha > 0.0f) {
Adam Cohenf34bab52010-09-30 14:11:56 -0700262 Drawable bg;
Patrick Dubroy1262e362010-10-06 15:49:50 -0700263 if (getScaleX() < 0.5f) {
Michael Jurka3e7c7632010-10-02 16:01:03 -0700264 bg = mAcceptsDrops ? mBackgroundMiniAcceptsDrops : mBackgroundMini;
Adam Cohenf34bab52010-09-30 14:11:56 -0700265 } else {
Patrick Dubroy1262e362010-10-06 15:49:50 -0700266 bg = mHover ? mBackgroundHover : mBackground;
Adam Cohenf34bab52010-09-30 14:11:56 -0700267 }
Adam Cohen9c4949e2010-10-05 12:27:22 -0700268 if (bg != null) {
269 bg.setAlpha((int) (mBackgroundAlpha * 255));
270 bg.draw(canvas);
271 }
Michael Jurka3e7c7632010-10-02 16:01:03 -0700272 if (mHover && getScaleX() < 0.5f) {
273 mBackgroundMiniHover.setAlpha((int) (mBackgroundAlpha * 255));
274 mBackgroundMiniHover.draw(canvas);
275 }
Michael Jurkaa63c4522010-08-19 13:52:27 -0700276 }
Romain Guya6abce82009-11-10 02:54:41 -0800277
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700278 if (mCrosshairsVisibility > 0.0f) {
279 final int countX = mCountX;
280 final int countY = mCountY;
281
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700282 final float MAX_ALPHA = 0.4f;
283 final int MAX_VISIBLE_DISTANCE = 600;
284 final float DISTANCE_MULTIPLIER = 0.002f;
285
286 final Drawable d = mCrosshairsDrawable;
287 final int width = d.getIntrinsicWidth();
288 final int height = d.getIntrinsicHeight();
289
290 int x = getLeftPadding() - (mWidthGap / 2) - (width / 2);
291 for (int col = 0; col <= countX; col++) {
292 int y = getTopPadding() - (mHeightGap / 2) - (height / 2);
293 for (int row = 0; row <= countY; row++) {
294 mTmpPointF.set(x - mDragCenter.x, y - mDragCenter.y);
295 float dist = mTmpPointF.length();
296 // Crosshairs further from the drag point are more faint
297 float alpha = Math.min(MAX_ALPHA,
298 DISTANCE_MULTIPLIER * (MAX_VISIBLE_DISTANCE - dist));
299 if (alpha > 0.0f) {
300 d.setBounds(x, y, x + width, y + height);
301 d.setAlpha((int) (alpha * 255 * mCrosshairsVisibility));
302 d.draw(canvas);
303 }
304 y += mCellHeight + mHeightGap;
305 }
306 x += mCellWidth + mWidthGap;
307 }
Joe Onorato4be866d2010-10-10 11:26:02 -0700308 }
Winson Chung150fbab2010-09-29 17:14:26 -0700309
Joe Onorato4be866d2010-10-10 11:26:02 -0700310 final Paint paint = new Paint();
311 for (int i = 0; i < mDragOutlines.length; i++) {
Chet Haase472b2812010-10-14 07:02:04 -0700312 final float alpha = mDragOutlineAlphas[i];
Joe Onorato4be866d2010-10-10 11:26:02 -0700313 if (alpha > 0) {
314 final Point p = mDragOutlines[i];
315 final Bitmap b = (Bitmap) mDragOutlineAnims[i].getTag();
Chet Haase472b2812010-10-14 07:02:04 -0700316 paint.setAlpha((int)(alpha + .5f));
Joe Onorato4be866d2010-10-10 11:26:02 -0700317 canvas.drawBitmap(b, p.x, p.y, paint);
Winson Chung150fbab2010-09-29 17:14:26 -0700318 }
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700319 }
320 }
321
Adam Cohenf34bab52010-09-30 14:11:56 -0700322 public void setDimmableProgress(float progress) {
323 for (int i = 0; i < getChildCount(); i++) {
324 Dimmable d = (Dimmable) getChildAt(i);
325 d.setDimmableProgress(progress);
326 }
327 }
328
329 public float getDimmableProgress() {
330 if (getChildCount() > 0) {
331 return ((Dimmable) getChildAt(0)).getDimmableProgress();
332 }
333 return 0.0f;
334 }
335
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700336 @Override
Jeff Sharkey83f111d2009-04-20 21:03:13 -0700337 public void cancelLongPress() {
338 super.cancelLongPress();
339
340 // Cancel long press for all children
341 final int count = getChildCount();
342 for (int i = 0; i < count; i++) {
343 final View child = getChildAt(i);
344 child.cancelLongPress();
345 }
346 }
347
Michael Jurkadee05892010-07-27 10:01:56 -0700348 public void setOnInterceptTouchListener(View.OnTouchListener listener) {
349 mInterceptTouchListener = listener;
350 }
351
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800352 int getCountX() {
Adam Cohend22015c2010-07-26 22:02:18 -0700353 return mCountX;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800354 }
355
356 int getCountY() {
Adam Cohend22015c2010-07-26 22:02:18 -0700357 return mCountY;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800358 }
359
Winson Chungaafa03c2010-06-11 17:34:16 -0700360 public boolean addViewToCellLayout(View child, int index, int childId, LayoutParams params) {
361 final LayoutParams lp = params;
362
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800363 // Generate an id for each view, this assumes we have at most 256x256 cells
364 // per workspace screen
Adam Cohend22015c2010-07-26 22:02:18 -0700365 if (lp.cellX >= 0 && lp.cellX <= mCountX - 1 && lp.cellY >= 0 && lp.cellY <= mCountY - 1) {
Winson Chungaafa03c2010-06-11 17:34:16 -0700366 // If the horizontal or vertical span is set to -1, it is taken to
367 // mean that it spans the extent of the CellLayout
Adam Cohend22015c2010-07-26 22:02:18 -0700368 if (lp.cellHSpan < 0) lp.cellHSpan = mCountX;
369 if (lp.cellVSpan < 0) lp.cellVSpan = mCountY;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800370
Winson Chungaafa03c2010-06-11 17:34:16 -0700371 child.setId(childId);
372
Michael Jurkadee05892010-07-27 10:01:56 -0700373 // We might be in the middle or end of shrinking/fading to a dimmed view
374 // Make sure this view's alpha is set the same as all the rest of the views
Michael Jurka5f1c5092010-09-03 14:15:02 -0700375 child.setAlpha(getAlpha());
Winson Chungaafa03c2010-06-11 17:34:16 -0700376 addView(child, index, lp);
Michael Jurkadee05892010-07-27 10:01:56 -0700377
Michael Jurka0280c3b2010-09-17 15:00:07 -0700378 markCellsAsOccupiedForView(child);
379
Winson Chungaafa03c2010-06-11 17:34:16 -0700380 return true;
381 }
382 return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800383 }
Michael Jurka3e7c7632010-10-02 16:01:03 -0700384 public void setAcceptsDrops(boolean acceptsDrops) {
385 if (mAcceptsDrops != acceptsDrops) {
386 mAcceptsDrops = acceptsDrops;
387 invalidate();
388 }
389 }
390
391 public boolean getAcceptsDrops() {
392 return mAcceptsDrops;
393 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800394
395 @Override
Michael Jurka0280c3b2010-09-17 15:00:07 -0700396 public void removeAllViews() {
397 clearOccupiedCells();
398 }
399
400 @Override
401 public void removeAllViewsInLayout() {
402 clearOccupiedCells();
403 }
404
405 @Override
406 public void removeView(View view) {
407 markCellsAsUnoccupiedForView(view);
408 super.removeView(view);
409 }
410
411 @Override
412 public void removeViewAt(int index) {
413 markCellsAsUnoccupiedForView(getChildAt(index));
414 super.removeViewAt(index);
415 }
416
417 @Override
418 public void removeViewInLayout(View view) {
419 markCellsAsUnoccupiedForView(view);
420 super.removeViewInLayout(view);
421 }
422
423 @Override
424 public void removeViews(int start, int count) {
425 for (int i = start; i < start + count; i++) {
426 markCellsAsUnoccupiedForView(getChildAt(i));
427 }
428 super.removeViews(start, count);
429 }
430
431 @Override
432 public void removeViewsInLayout(int start, int count) {
433 for (int i = start; i < start + count; i++) {
434 markCellsAsUnoccupiedForView(getChildAt(i));
435 }
436 super.removeViewsInLayout(start, count);
437 }
438
439 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800440 public void requestChildFocus(View child, View focused) {
441 super.requestChildFocus(child, focused);
442 if (child != null) {
443 Rect r = new Rect();
444 child.getDrawingRect(r);
445 requestRectangleOnScreen(r);
446 }
447 }
448
449 @Override
450 protected void onAttachedToWindow() {
451 super.onAttachedToWindow();
452 mCellInfo.screen = ((ViewGroup) getParent()).indexOfChild(this);
453 }
454
Michael Jurkaaf442092010-06-10 17:01:57 -0700455 public void setTagToCellInfoForPoint(int touchX, int touchY) {
456 final CellInfo cellInfo = mCellInfo;
457 final Rect frame = mRect;
458 final int x = touchX + mScrollX;
459 final int y = touchY + mScrollY;
460 final int count = getChildCount();
461
462 boolean found = false;
463 for (int i = count - 1; i >= 0; i--) {
464 final View child = getChildAt(i);
465
466 if ((child.getVisibility()) == VISIBLE || child.getAnimation() != null) {
467 child.getHitRect(frame);
468 if (frame.contains(x, y)) {
469 final LayoutParams lp = (LayoutParams) child.getLayoutParams();
470 cellInfo.cell = child;
471 cellInfo.cellX = lp.cellX;
472 cellInfo.cellY = lp.cellY;
473 cellInfo.spanX = lp.cellHSpan;
474 cellInfo.spanY = lp.cellVSpan;
475 cellInfo.valid = true;
476 found = true;
Michael Jurkaaf442092010-06-10 17:01:57 -0700477 break;
478 }
479 }
480 }
Winson Chungaafa03c2010-06-11 17:34:16 -0700481
Michael Jurkaaf442092010-06-10 17:01:57 -0700482 if (!found) {
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700483 final int cellXY[] = mTmpCellXY;
Michael Jurkaaf442092010-06-10 17:01:57 -0700484 pointToCellExact(x, y, cellXY);
485
Michael Jurkaaf442092010-06-10 17:01:57 -0700486 cellInfo.cell = null;
487 cellInfo.cellX = cellXY[0];
488 cellInfo.cellY = cellXY[1];
489 cellInfo.spanX = 1;
490 cellInfo.spanY = 1;
Michael Jurka0280c3b2010-09-17 15:00:07 -0700491 cellInfo.valid = cellXY[0] >= 0 && cellXY[1] >= 0 && cellXY[0] < mCountX &&
492 cellXY[1] < mCountY && !mOccupied[cellXY[0]][cellXY[1]];
Michael Jurkaaf442092010-06-10 17:01:57 -0700493 }
494 setTag(cellInfo);
495 }
496
Winson Chungaafa03c2010-06-11 17:34:16 -0700497
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800498 @Override
499 public boolean onInterceptTouchEvent(MotionEvent ev) {
Michael Jurkadee05892010-07-27 10:01:56 -0700500 if (mInterceptTouchListener != null && mInterceptTouchListener.onTouch(this, ev)) {
501 return true;
502 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800503 final int action = ev.getAction();
504 final CellInfo cellInfo = mCellInfo;
505
506 if (action == MotionEvent.ACTION_DOWN) {
Michael Jurkaaf442092010-06-10 17:01:57 -0700507 setTagToCellInfoForPoint((int) ev.getX(), (int) ev.getY());
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800508 } else if (action == MotionEvent.ACTION_UP) {
509 cellInfo.cell = null;
510 cellInfo.cellX = -1;
511 cellInfo.cellY = -1;
512 cellInfo.spanX = 0;
513 cellInfo.spanY = 0;
514 cellInfo.valid = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800515 setTag(cellInfo);
516 }
517
518 return false;
519 }
520
521 @Override
522 public CellInfo getTag() {
Michael Jurka0280c3b2010-09-17 15:00:07 -0700523 return (CellInfo) super.getTag();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800524 }
525
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700526 /**
527 * Check if the row 'y' is empty from columns 'left' to 'right', inclusive.
528 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800529 private static boolean isRowEmpty(int y, int left, int right, boolean[][] occupied) {
530 for (int x = left; x <= right; x++) {
531 if (occupied[x][y]) {
532 return false;
533 }
534 }
535 return true;
536 }
537
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800538 /**
Winson Chungaafa03c2010-06-11 17:34:16 -0700539 * Given a point, return the cell that strictly encloses that point
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800540 * @param x X coordinate of the point
541 * @param y Y coordinate of the point
542 * @param result Array of 2 ints to hold the x and y coordinate of the cell
543 */
544 void pointToCellExact(int x, int y, int[] result) {
Winson Chungaafa03c2010-06-11 17:34:16 -0700545 final int hStartPadding = getLeftPadding();
546 final int vStartPadding = getTopPadding();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800547
548 result[0] = (x - hStartPadding) / (mCellWidth + mWidthGap);
549 result[1] = (y - vStartPadding) / (mCellHeight + mHeightGap);
550
Adam Cohend22015c2010-07-26 22:02:18 -0700551 final int xAxis = mCountX;
552 final int yAxis = mCountY;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800553
554 if (result[0] < 0) result[0] = 0;
555 if (result[0] >= xAxis) result[0] = xAxis - 1;
556 if (result[1] < 0) result[1] = 0;
557 if (result[1] >= yAxis) result[1] = yAxis - 1;
558 }
Winson Chungaafa03c2010-06-11 17:34:16 -0700559
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800560 /**
561 * Given a point, return the cell that most closely encloses that point
562 * @param x X coordinate of the point
563 * @param y Y coordinate of the point
564 * @param result Array of 2 ints to hold the x and y coordinate of the cell
565 */
566 void pointToCellRounded(int x, int y, int[] result) {
567 pointToCellExact(x + (mCellWidth / 2), y + (mCellHeight / 2), result);
568 }
569
570 /**
571 * Given a cell coordinate, return the point that represents the upper left corner of that cell
Winson Chungaafa03c2010-06-11 17:34:16 -0700572 *
573 * @param cellX X coordinate of the cell
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800574 * @param cellY Y coordinate of the cell
Winson Chungaafa03c2010-06-11 17:34:16 -0700575 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800576 * @param result Array of 2 ints to hold the x and y coordinate of the point
577 */
578 void cellToPoint(int cellX, int cellY, int[] result) {
Winson Chungaafa03c2010-06-11 17:34:16 -0700579 final int hStartPadding = getLeftPadding();
580 final int vStartPadding = getTopPadding();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800581
582 result[0] = hStartPadding + cellX * (mCellWidth + mWidthGap);
583 result[1] = vStartPadding + cellY * (mCellHeight + mHeightGap);
584 }
585
Romain Guy84f296c2009-11-04 15:00:44 -0800586 int getCellWidth() {
587 return mCellWidth;
588 }
589
590 int getCellHeight() {
591 return mCellHeight;
592 }
593
Romain Guy1a304a12009-11-10 00:02:32 -0800594 int getLeftPadding() {
Winson Chungaafa03c2010-06-11 17:34:16 -0700595 return mLeftPadding;
Romain Guy1a304a12009-11-10 00:02:32 -0800596 }
597
598 int getTopPadding() {
Winson Chungaafa03c2010-06-11 17:34:16 -0700599 return mTopPadding;
Romain Guy1a304a12009-11-10 00:02:32 -0800600 }
601
602 int getRightPadding() {
Winson Chungaafa03c2010-06-11 17:34:16 -0700603 return mRightPadding;
Romain Guy1a304a12009-11-10 00:02:32 -0800604 }
605
606 int getBottomPadding() {
Winson Chungaafa03c2010-06-11 17:34:16 -0700607 return mBottomPadding;
Romain Guy1a304a12009-11-10 00:02:32 -0800608 }
609
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800610 @Override
611 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
612 // TODO: currently ignoring padding
Winson Chungaafa03c2010-06-11 17:34:16 -0700613
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800614 int widthSpecMode = MeasureSpec.getMode(widthMeasureSpec);
Winson Chungaafa03c2010-06-11 17:34:16 -0700615 int widthSpecSize = MeasureSpec.getSize(widthMeasureSpec);
616
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800617 int heightSpecMode = MeasureSpec.getMode(heightMeasureSpec);
618 int heightSpecSize = MeasureSpec.getSize(heightMeasureSpec);
Winson Chungaafa03c2010-06-11 17:34:16 -0700619
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800620 if (widthSpecMode == MeasureSpec.UNSPECIFIED || heightSpecMode == MeasureSpec.UNSPECIFIED) {
621 throw new RuntimeException("CellLayout cannot have UNSPECIFIED dimensions");
622 }
623
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800624 final int cellWidth = mCellWidth;
625 final int cellHeight = mCellHeight;
626
Adam Cohend22015c2010-07-26 22:02:18 -0700627 int numWidthGaps = mCountX - 1;
628 int numHeightGaps = mCountY - 1;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800629
Michael Jurka0280c3b2010-09-17 15:00:07 -0700630 int vSpaceLeft = heightSpecSize - mTopPadding - mBottomPadding - (cellHeight * mCountY);
Adam Cohend22015c2010-07-26 22:02:18 -0700631 mHeightGap = vSpaceLeft / numHeightGaps;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800632
Michael Jurka0280c3b2010-09-17 15:00:07 -0700633 int hSpaceLeft = widthSpecSize - mLeftPadding - mRightPadding - (cellWidth * mCountX);
Adam Cohend22015c2010-07-26 22:02:18 -0700634 mWidthGap = hSpaceLeft / numWidthGaps;
Winson Chungaafa03c2010-06-11 17:34:16 -0700635
Michael Jurka5f1c5092010-09-03 14:15:02 -0700636 // center it around the min gaps
637 int minGap = Math.min(mWidthGap, mHeightGap);
638 mWidthGap = mHeightGap = minGap;
639
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800640 int count = getChildCount();
641
642 for (int i = 0; i < count; i++) {
643 View child = getChildAt(i);
644 LayoutParams lp = (LayoutParams) child.getLayoutParams();
Winson Chungaafa03c2010-06-11 17:34:16 -0700645 lp.setup(cellWidth, cellHeight, mWidthGap, mHeightGap,
646 mLeftPadding, mTopPadding);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800647
Michael Jurka0280c3b2010-09-17 15:00:07 -0700648 int childWidthMeasureSpec = MeasureSpec.makeMeasureSpec(lp.width, MeasureSpec.EXACTLY);
Winson Chungaafa03c2010-06-11 17:34:16 -0700649 int childheightMeasureSpec = MeasureSpec.makeMeasureSpec(lp.height,
650 MeasureSpec.EXACTLY);
651
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800652 child.measure(childWidthMeasureSpec, childheightMeasureSpec);
653 }
Michael Jurka5f1c5092010-09-03 14:15:02 -0700654 if (widthSpecMode == MeasureSpec.AT_MOST) {
655 int newWidth = mLeftPadding + mRightPadding + (mCountX * cellWidth) +
656 ((mCountX - 1) * minGap);
657 int newHeight = mTopPadding + mBottomPadding + (mCountY * cellHeight) +
658 ((mCountY - 1) * minGap);
659 setMeasuredDimension(newWidth, newHeight);
660 } else if (widthSpecMode == MeasureSpec.EXACTLY) {
661 setMeasuredDimension(widthSpecSize, heightSpecSize);
662 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800663 }
664
665 @Override
Michael Jurka28750fb2010-09-24 17:43:49 -0700666 protected void onLayout(boolean changed, int l, int t, int r, int b) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800667 int count = getChildCount();
668
669 for (int i = 0; i < count; i++) {
670 View child = getChildAt(i);
671 if (child.getVisibility() != GONE) {
672
673 CellLayout.LayoutParams lp = (CellLayout.LayoutParams) child.getLayoutParams();
674
675 int childLeft = lp.x;
676 int childTop = lp.y;
677 child.layout(childLeft, childTop, childLeft + lp.width, childTop + lp.height);
Romain Guy84f296c2009-11-04 15:00:44 -0800678
679 if (lp.dropped) {
680 lp.dropped = false;
681
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700682 final int[] cellXY = mTmpCellXY;
Romain Guy06762ab2010-01-25 16:51:08 -0800683 getLocationOnScreen(cellXY);
Romain Guy84f296c2009-11-04 15:00:44 -0800684 mWallpaperManager.sendWallpaperCommand(getWindowToken(), "android.home.drop",
Romain Guy06762ab2010-01-25 16:51:08 -0800685 cellXY[0] + childLeft + lp.width / 2,
686 cellXY[1] + childTop + lp.height / 2, 0, null);
Romain Guy84f296c2009-11-04 15:00:44 -0800687 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800688 }
689 }
690 }
691
692 @Override
Michael Jurkadee05892010-07-27 10:01:56 -0700693 protected void onSizeChanged(int w, int h, int oldw, int oldh) {
694 super.onSizeChanged(w, h, oldw, oldh);
Michael Jurka5f1c5092010-09-03 14:15:02 -0700695 if (mBackground != null) {
Patrick Dubroy1262e362010-10-06 15:49:50 -0700696 mBackground.setBounds(0, 0, w, h);
697 }
698 if (mBackgroundHover != null) {
699 mBackgroundHover.setBounds(0, 0, w, h);
Michael Jurkaa63c4522010-08-19 13:52:27 -0700700 }
Adam Cohenf34bab52010-09-30 14:11:56 -0700701 if (mBackgroundMiniHover != null) {
Patrick Dubroy1262e362010-10-06 15:49:50 -0700702 mBackgroundMiniHover.setBounds(0, 0, w, h);
Adam Cohenf34bab52010-09-30 14:11:56 -0700703 }
704 if (mBackgroundMini != null) {
Patrick Dubroy1262e362010-10-06 15:49:50 -0700705 mBackgroundMini.setBounds(0, 0, w, h);
Michael Jurkaa63c4522010-08-19 13:52:27 -0700706 }
Michael Jurka3e7c7632010-10-02 16:01:03 -0700707 if (mBackgroundMiniAcceptsDrops != null) {
708 mBackgroundMiniAcceptsDrops.setBounds(0, 0, w, h);
709 }
Michael Jurkadee05892010-07-27 10:01:56 -0700710 }
711
712 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800713 protected void setChildrenDrawingCacheEnabled(boolean enabled) {
714 final int count = getChildCount();
715 for (int i = 0; i < count; i++) {
716 final View view = getChildAt(i);
717 view.setDrawingCacheEnabled(enabled);
718 // Update the drawing caches
Adam Powellfefa0ce2010-05-03 10:23:50 -0700719 view.buildDrawingCache(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800720 }
721 }
722
723 @Override
724 protected void setChildrenDrawnWithCacheEnabled(boolean enabled) {
725 super.setChildrenDrawnWithCacheEnabled(enabled);
726 }
727
Michael Jurka5f1c5092010-09-03 14:15:02 -0700728 public float getBackgroundAlpha() {
729 return mBackgroundAlpha;
Michael Jurkadee05892010-07-27 10:01:56 -0700730 }
731
Michael Jurka5f1c5092010-09-03 14:15:02 -0700732 public void setBackgroundAlpha(float alpha) {
733 mBackgroundAlpha = alpha;
Michael Jurka0142d492010-08-25 17:46:15 -0700734 invalidate();
Michael Jurkadee05892010-07-27 10:01:56 -0700735 }
736
Michael Jurka5f1c5092010-09-03 14:15:02 -0700737 // Need to return true to let the view system know we know how to handle alpha-- this is
738 // because when our children have an alpha of 0.0f, they are still rendering their "dimmed"
739 // versions
740 @Override
741 protected boolean onSetAlpha(int alpha) {
742 return true;
743 }
744
745 public void setAlpha(float alpha) {
746 setChildrenAlpha(alpha);
747 super.setAlpha(alpha);
748 }
749
Michael Jurkadee05892010-07-27 10:01:56 -0700750 private void setChildrenAlpha(float alpha) {
Michael Jurka0142d492010-08-25 17:46:15 -0700751 final int childCount = getChildCount();
752 for (int i = 0; i < childCount; i++) {
Michael Jurkadee05892010-07-27 10:01:56 -0700753 getChildAt(i).setAlpha(alpha);
754 }
755 }
756
Michael Jurka0280c3b2010-09-17 15:00:07 -0700757 private boolean isVacantIgnoring(
758 int originX, int originY, int spanX, int spanY, View ignoreView) {
759 if (ignoreView != null) {
760 markCellsAsUnoccupiedForView(ignoreView);
761 }
Michael Jurka28750fb2010-09-24 17:43:49 -0700762 boolean isVacant = true;
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700763 for (int i = 0; i < spanY; i++) {
764 if (!isRowEmpty(originY + i, originX, originX + spanX - 1, mOccupied)) {
Michael Jurka28750fb2010-09-24 17:43:49 -0700765 isVacant = false;
766 break;
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700767 }
768 }
Michael Jurka0280c3b2010-09-17 15:00:07 -0700769 if (ignoreView != null) {
770 markCellsAsOccupiedForView(ignoreView);
771 }
Michael Jurka28750fb2010-09-24 17:43:49 -0700772 return isVacant;
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700773 }
774
Michael Jurka0280c3b2010-09-17 15:00:07 -0700775 private boolean isVacant(int originX, int originY, int spanX, int spanY) {
776 return isVacantIgnoring(originX, originY, spanX, spanY, null);
777 }
778
Patrick Dubroy440c3602010-07-13 17:50:32 -0700779 public View getChildAt(int x, int y) {
780 final int count = getChildCount();
781 for (int i = 0; i < count; i++) {
782 View child = getChildAt(i);
783 LayoutParams lp = (LayoutParams) child.getLayoutParams();
784
785 if ((lp.cellX <= x) && (x < lp.cellX + lp.cellHSpan) &&
786 (lp.cellY <= y) && (y < lp.cellY + lp.cellHSpan)) {
787 return child;
788 }
789 }
790 return null;
791 }
792
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700793 /**
Patrick Dubroy8f86ddc2010-07-16 13:55:32 -0700794 * Estimate the size that a child with the given dimensions will take in the layout.
795 */
796 void estimateChildSize(int minWidth, int minHeight, int[] result) {
797 // Assuming it's placed at 0, 0, find where the bottom right cell will land
798 rectToCell(minWidth, minHeight, result);
799
800 // Then figure out the rect it will occupy
801 cellToRect(0, 0, result[0], result[1], mRectF);
802 result[0] = (int)mRectF.width();
803 result[1] = (int)mRectF.height();
804 }
805
806 /**
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700807 * Estimate where the top left cell of the dragged item will land if it is dropped.
808 *
809 * @param originX The X value of the top left corner of the item
810 * @param originY The Y value of the top left corner of the item
811 * @param spanX The number of horizontal cells that the item spans
812 * @param spanY The number of vertical cells that the item spans
813 * @param result The estimated drop cell X and Y.
814 */
815 void estimateDropCell(int originX, int originY, int spanX, int spanY, int[] result) {
Adam Cohend22015c2010-07-26 22:02:18 -0700816 final int countX = mCountX;
817 final int countY = mCountY;
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700818
Michael Jurkaa63c4522010-08-19 13:52:27 -0700819 // pointToCellRounded takes the top left of a cell but will pad that with
820 // cellWidth/2 and cellHeight/2 when finding the matching cell
821 pointToCellRounded(originX, originY, result);
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700822
823 // If the item isn't fully on this screen, snap to the edges
824 int rightOverhang = result[0] + spanX - countX;
825 if (rightOverhang > 0) {
826 result[0] -= rightOverhang; // Snap to right
827 }
828 result[0] = Math.max(0, result[0]); // Snap to left
829 int bottomOverhang = result[1] + spanY - countY;
830 if (bottomOverhang > 0) {
831 result[1] -= bottomOverhang; // Snap to bottom
832 }
833 result[1] = Math.max(0, result[1]); // Snap to top
834 }
835
Joe Onorato4be866d2010-10-10 11:26:02 -0700836 void visualizeDropLocation(
837 View v, Bitmap dragOutline, int originX, int originY, int spanX, int spanY) {
838
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -0700839 final int oldDragCellX = mDragCell[0];
840 final int oldDragCellY = mDragCell[1];
Joe Onorato4be866d2010-10-10 11:26:02 -0700841 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 Dubroy08ae2ec2010-10-14 23:54:22 -0700844 if (nearest != null && (nearest[0] != oldDragCellX || nearest[1] != oldDragCellY)) {
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;
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -0700863 mDragOutlineAnims[oldIndex].animateOut();
864 mDragOutlineCurrent = (oldIndex + 1) % mDragOutlines.length;
Winson Chung150fbab2010-09-29 17:14:26 -0700865
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -0700866 mDragOutlines[mDragOutlineCurrent].set(left, top);
867 mDragOutlineAnims[mDragOutlineCurrent].setTag(dragOutline);
868 mDragOutlineAnims[mDragOutlineCurrent].animateIn();
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700869 }
Patrick Dubroy49250ad2010-10-08 15:33:52 -0700870
871 // If we are drawing crosshairs, the entire CellLayout needs to be invalidated
872 if (mCrosshairsDrawable != null) {
873 invalidate();
874 }
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700875 }
876
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800877 /**
Jeff Sharkey70864282009-04-07 21:08:40 -0700878 * Find a vacant area that will fit the given bounds nearest the requested
879 * cell location. Uses Euclidean distance to score multiple vacant areas.
Winson Chungaafa03c2010-06-11 17:34:16 -0700880 *
Romain Guy51afc022009-05-04 18:03:43 -0700881 * @param pixelX The X location at which you want to search for a vacant area.
882 * @param pixelY The Y location at which you want to search for a vacant area.
Jeff Sharkey70864282009-04-07 21:08:40 -0700883 * @param spanX Horizontal span of the object.
884 * @param spanY Vertical span of the object.
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700885 * @param result Array in which to place the result, or null (in which case a new array will
886 * be allocated)
Jeff Sharkey70864282009-04-07 21:08:40 -0700887 * @return The X, Y cell of a vacant area that can contain this object,
888 * nearest the requested location.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800889 */
Michael Jurka6a1435d2010-09-27 17:35:12 -0700890 int[] findNearestVacantArea(
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700891 int pixelX, int pixelY, int spanX, int spanY, int[] result) {
892 return findNearestVacantArea(pixelX, pixelY, spanX, spanY, null, result);
Michael Jurka6a1435d2010-09-27 17:35:12 -0700893 }
Winson Chungaafa03c2010-06-11 17:34:16 -0700894
Michael Jurka6a1435d2010-09-27 17:35:12 -0700895 /**
896 * Find a vacant area that will fit the given bounds nearest the requested
897 * cell location. Uses Euclidean distance to score multiple vacant areas.
898 *
899 * @param pixelX The X location at which you want to search for a vacant area.
900 * @param pixelY The Y location at which you want to search for a vacant area.
901 * @param spanX Horizontal span of the object.
902 * @param spanY Vertical span of the object.
Michael Jurka6a1435d2010-09-27 17:35:12 -0700903 * @param ignoreView Considers space occupied by this view as unoccupied
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700904 * @param result Previously returned value to possibly recycle.
Michael Jurka6a1435d2010-09-27 17:35:12 -0700905 * @return The X, Y cell of a vacant area that can contain this object,
906 * nearest the requested location.
907 */
908 int[] findNearestVacantArea(
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700909 int pixelX, int pixelY, int spanX, int spanY, View ignoreView, int[] result) {
Michael Jurkac6ee42e2010-09-30 12:04:50 -0700910 // mark space take by ignoreView as available (method checks if ignoreView is null)
911 markCellsAsUnoccupiedForView(ignoreView);
912
Jeff Sharkey70864282009-04-07 21:08:40 -0700913 // Keep track of best-scoring drop area
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700914 final int[] bestXY = result != null ? result : new int[2];
Jeff Sharkey70864282009-04-07 21:08:40 -0700915 double bestDistance = Double.MAX_VALUE;
Winson Chungaafa03c2010-06-11 17:34:16 -0700916
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700917 final int countX = mCountX;
918 final int countY = mCountY;
919 final boolean[][] occupied = mOccupied;
920
921 for (int x = 0; x < countX - (spanX - 1); x++) {
Michael Jurkac28de512010-08-13 11:27:44 -0700922 inner:
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700923 for (int y = 0; y < countY - (spanY - 1); y++) {
Michael Jurkac28de512010-08-13 11:27:44 -0700924 for (int i = 0; i < spanX; i++) {
925 for (int j = 0; j < spanY; j++) {
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700926 if (occupied[x + i][y + j]) {
Michael Jurkac28de512010-08-13 11:27:44 -0700927 // small optimization: we can skip to below the row we just found
928 // an occupied cell
929 y += j;
930 continue inner;
931 }
932 }
933 }
934 final int[] cellXY = mTmpCellXY;
935 cellToPoint(x, y, cellXY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800936
Michael Jurkac28de512010-08-13 11:27:44 -0700937 double distance = Math.sqrt(Math.pow(cellXY[0] - pixelX, 2)
938 + Math.pow(cellXY[1] - pixelY, 2));
939 if (distance <= bestDistance) {
940 bestDistance = distance;
941 bestXY[0] = x;
942 bestXY[1] = y;
943 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800944 }
945 }
Michael Jurkac6ee42e2010-09-30 12:04:50 -0700946 // re-mark space taken by ignoreView as occupied
947 markCellsAsOccupiedForView(ignoreView);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800948
Winson Chungaafa03c2010-06-11 17:34:16 -0700949 // Return null if no suitable location found
Jeff Sharkey70864282009-04-07 21:08:40 -0700950 if (bestDistance < Double.MAX_VALUE) {
951 return bestXY;
952 } else {
953 return null;
954 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800955 }
Winson Chungaafa03c2010-06-11 17:34:16 -0700956
Michael Jurka0280c3b2010-09-17 15:00:07 -0700957 boolean existsEmptyCell() {
958 return findCellForSpan(null, 1, 1);
959 }
960
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800961 /**
Michael Jurka0280c3b2010-09-17 15:00:07 -0700962 * Finds the upper-left coordinate of the first rectangle in the grid that can
963 * hold a cell of the specified dimensions. If intersectX and intersectY are not -1,
964 * then this method will only return coordinates for rectangles that contain the cell
965 * (intersectX, intersectY)
966 *
967 * @param cellXY The array that will contain the position of a vacant cell if such a cell
968 * can be found.
969 * @param spanX The horizontal span of the cell we want to find.
970 * @param spanY The vertical span of the cell we want to find.
971 *
972 * @return True if a vacant cell of the specified dimension was found, false otherwise.
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700973 */
Michael Jurka0280c3b2010-09-17 15:00:07 -0700974 boolean findCellForSpan(int[] cellXY, int spanX, int spanY) {
975 return findCellForSpanThatIntersectsIgnoring(cellXY, spanX, spanY, -1, -1, null);
976 }
977
978 /**
979 * Like above, but ignores any cells occupied by the item "ignoreView"
980 *
981 * @param cellXY The array that will contain the position of a vacant cell if such a cell
982 * can be found.
983 * @param spanX The horizontal span of the cell we want to find.
984 * @param spanY The vertical span of the cell we want to find.
985 * @param ignoreView The home screen item we should treat as not occupying any space
986 * @return
987 */
988 boolean findCellForSpanIgnoring(int[] cellXY, int spanX, int spanY, View ignoreView) {
989 return findCellForSpanThatIntersectsIgnoring(cellXY, spanX, spanY, -1, -1, ignoreView);
990 }
991
992 /**
993 * Like above, but if intersectX and intersectY are not -1, then this method will try to
994 * return coordinates for rectangles that contain the cell [intersectX, intersectY]
995 *
996 * @param spanX The horizontal span of the cell we want to find.
997 * @param spanY The vertical span of the cell we want to find.
998 * @param ignoreView The home screen item we should treat as not occupying any space
999 * @param intersectX The X coordinate of the cell that we should try to overlap
1000 * @param intersectX The Y coordinate of the cell that we should try to overlap
1001 *
1002 * @return True if a vacant cell of the specified dimension was found, false otherwise.
1003 */
1004 boolean findCellForSpanThatIntersects(int[] cellXY, int spanX, int spanY,
1005 int intersectX, int intersectY) {
1006 return findCellForSpanThatIntersectsIgnoring(
1007 cellXY, spanX, spanY, intersectX, intersectY, null);
1008 }
1009
1010 /**
1011 * The superset of the above two methods
1012 */
1013 boolean findCellForSpanThatIntersectsIgnoring(int[] cellXY, int spanX, int spanY,
1014 int intersectX, int intersectY, View ignoreView) {
Michael Jurkac6ee42e2010-09-30 12:04:50 -07001015 // mark space take by ignoreView as available (method checks if ignoreView is null)
1016 markCellsAsUnoccupiedForView(ignoreView);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001017
Michael Jurka28750fb2010-09-24 17:43:49 -07001018 boolean foundCell = false;
Michael Jurka0280c3b2010-09-17 15:00:07 -07001019 while (true) {
1020 int startX = 0;
1021 if (intersectX >= 0) {
1022 startX = Math.max(startX, intersectX - (spanX - 1));
1023 }
1024 int endX = mCountX - (spanX - 1);
1025 if (intersectX >= 0) {
1026 endX = Math.min(endX, intersectX + (spanX - 1) + (spanX == 1 ? 1 : 0));
1027 }
1028 int startY = 0;
1029 if (intersectY >= 0) {
1030 startY = Math.max(startY, intersectY - (spanY - 1));
1031 }
1032 int endY = mCountY - (spanY - 1);
1033 if (intersectY >= 0) {
1034 endY = Math.min(endY, intersectY + (spanY - 1) + (spanY == 1 ? 1 : 0));
1035 }
1036
1037 for (int x = startX; x < endX; x++) {
1038 inner:
1039 for (int y = startY; y < endY; y++) {
1040 for (int i = 0; i < spanX; i++) {
1041 for (int j = 0; j < spanY; j++) {
1042 if (mOccupied[x + i][y + j]) {
1043 // small optimization: we can skip to below the row we just found
1044 // an occupied cell
1045 y += j;
1046 continue inner;
1047 }
1048 }
1049 }
1050 if (cellXY != null) {
1051 cellXY[0] = x;
1052 cellXY[1] = y;
1053 }
Michael Jurka28750fb2010-09-24 17:43:49 -07001054 foundCell = true;
1055 break;
Michael Jurka0280c3b2010-09-17 15:00:07 -07001056 }
1057 }
1058 if (intersectX == -1 && intersectY == -1) {
1059 break;
1060 } else {
1061 // if we failed to find anything, try again but without any requirements of
1062 // intersecting
1063 intersectX = -1;
1064 intersectY = -1;
1065 continue;
1066 }
1067 }
1068
Michael Jurkac6ee42e2010-09-30 12:04:50 -07001069 // re-mark space taken by ignoreView as occupied
1070 markCellsAsOccupiedForView(ignoreView);
Michael Jurka28750fb2010-09-24 17:43:49 -07001071 return foundCell;
Michael Jurka0280c3b2010-09-17 15:00:07 -07001072 }
1073
1074 /**
1075 * Called when drag has left this CellLayout or has been completed (successfully or not)
1076 */
1077 void onDragExit() {
Joe Onorato4be866d2010-10-10 11:26:02 -07001078 // This can actually be called when we aren't in a drag, e.g. when adding a new
1079 // item to this layout via the customize drawer.
1080 // Guard against that case.
1081 if (mDragging) {
1082 mDragging = false;
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001083
Joe Onorato4be866d2010-10-10 11:26:02 -07001084 // Fade out the drag indicators
1085 if (mCrosshairsAnimator != null) {
1086 mCrosshairsAnimator.animateOut();
1087 }
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001088 }
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -07001089
1090 // Invalidate the drag data
1091 mDragCell[0] = -1;
1092 mDragCell[1] = -1;
1093 mDragOutlineAnims[mDragOutlineCurrent].animateOut();
1094 mDragOutlineCurrent = (mDragOutlineCurrent + 1) % mDragOutlineAnims.length;
1095
1096 setHover(false);
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001097 }
1098
1099 /**
Winson Chungaafa03c2010-06-11 17:34:16 -07001100 * Mark a child as having been dropped.
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001101 * At the beginning of the drag operation, the child may have been on another
1102 * screen, but it is reparented before this method is called.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001103 *
1104 * @param child The child that is being dropped
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001105 */
Winson Chungaafa03c2010-06-11 17:34:16 -07001106 void onDropChild(View child) {
Romain Guyd94533d2009-08-17 10:01:15 -07001107 if (child != null) {
1108 LayoutParams lp = (LayoutParams) child.getLayoutParams();
Romain Guyd94533d2009-08-17 10:01:15 -07001109 lp.isDragging = false;
Romain Guy84f296c2009-11-04 15:00:44 -08001110 lp.dropped = true;
Romain Guyd94533d2009-08-17 10:01:15 -07001111 child.requestLayout();
Romain Guyd94533d2009-08-17 10:01:15 -07001112 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001113 }
1114
1115 void onDropAborted(View child) {
1116 if (child != null) {
1117 ((LayoutParams) child.getLayoutParams()).isDragging = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001118 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001119 }
1120
1121 /**
1122 * Start dragging the specified child
Winson Chungaafa03c2010-06-11 17:34:16 -07001123 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001124 * @param child The child that is being dragged
1125 */
1126 void onDragChild(View child) {
1127 LayoutParams lp = (LayoutParams) child.getLayoutParams();
1128 lp.isDragging = true;
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001129 }
1130
1131 /**
1132 * A drag event has begun over this layout.
1133 * It may have begun over this layout (in which case onDragChild is called first),
1134 * or it may have begun on another layout.
1135 */
1136 void onDragEnter(View dragView) {
Patrick Dubroyfe6bd872010-10-13 17:32:10 -07001137 if (!mDragging) {
Patrick Dubroyfe6bd872010-10-13 17:32:10 -07001138 // Fade in the drag indicators
1139 if (mCrosshairsAnimator != null) {
1140 mCrosshairsAnimator.animateIn();
1141 }
Joe Onorato4be866d2010-10-10 11:26:02 -07001142 }
1143 mDragging = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001144 }
Winson Chungaafa03c2010-06-11 17:34:16 -07001145
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001146 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001147 * Computes a bounding rectangle for a range of cells
Winson Chungaafa03c2010-06-11 17:34:16 -07001148 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001149 * @param cellX X coordinate of upper left corner expressed as a cell position
1150 * @param cellY Y coordinate of upper left corner expressed as a cell position
Winson Chungaafa03c2010-06-11 17:34:16 -07001151 * @param cellHSpan Width in cells
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001152 * @param cellVSpan Height in cells
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001153 * @param resultRect Rect into which to put the results
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001154 */
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001155 public void cellToRect(int cellX, int cellY, int cellHSpan, int cellVSpan, RectF resultRect) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001156 final int cellWidth = mCellWidth;
1157 final int cellHeight = mCellHeight;
1158 final int widthGap = mWidthGap;
1159 final int heightGap = mHeightGap;
Winson Chungaafa03c2010-06-11 17:34:16 -07001160
1161 final int hStartPadding = getLeftPadding();
1162 final int vStartPadding = getTopPadding();
1163
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001164 int width = cellHSpan * cellWidth + ((cellHSpan - 1) * widthGap);
1165 int height = cellVSpan * cellHeight + ((cellVSpan - 1) * heightGap);
1166
1167 int x = hStartPadding + cellX * (cellWidth + widthGap);
1168 int y = vStartPadding + cellY * (cellHeight + heightGap);
Winson Chungaafa03c2010-06-11 17:34:16 -07001169
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001170 resultRect.set(x, y, x + width, y + height);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001171 }
Winson Chungaafa03c2010-06-11 17:34:16 -07001172
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001173 /**
Winson Chungaafa03c2010-06-11 17:34:16 -07001174 * Computes the required horizontal and vertical cell spans to always
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001175 * fit the given rectangle.
Winson Chungaafa03c2010-06-11 17:34:16 -07001176 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001177 * @param width Width in pixels
1178 * @param height Height in pixels
Patrick Dubroy8f86ddc2010-07-16 13:55:32 -07001179 * @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 -08001180 */
Patrick Dubroy8f86ddc2010-07-16 13:55:32 -07001181 public int[] rectToCell(int width, int height, int[] result) {
Michael Jurka9987a5c2010-10-08 16:58:12 -07001182 return rectToCell(getResources(), width, height, result);
1183 }
1184
1185 public static int[] rectToCell(Resources resources, int width, int height, int[] result) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001186 // Always assume we're working with the smallest span to make sure we
1187 // reserve enough space in both orientations.
Joe Onorato79e56262009-09-21 15:23:04 -04001188 int actualWidth = resources.getDimensionPixelSize(R.dimen.workspace_cell_width);
1189 int actualHeight = resources.getDimensionPixelSize(R.dimen.workspace_cell_height);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001190 int smallerSize = Math.min(actualWidth, actualHeight);
Joe Onorato79e56262009-09-21 15:23:04 -04001191
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001192 // Always round up to next largest cell
1193 int spanX = (width + smallerSize) / smallerSize;
1194 int spanY = (height + smallerSize) / smallerSize;
Joe Onorato79e56262009-09-21 15:23:04 -04001195
Patrick Dubroy8f86ddc2010-07-16 13:55:32 -07001196 if (result == null) {
1197 return new int[] { spanX, spanY };
1198 }
1199 result[0] = spanX;
1200 result[1] = spanY;
1201 return result;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001202 }
1203
1204 /**
1205 * Find the first vacant cell, if there is one.
1206 *
1207 * @param vacant Holds the x and y coordinate of the vacant cell
1208 * @param spanX Horizontal cell span.
1209 * @param spanY Vertical cell span.
Winson Chungaafa03c2010-06-11 17:34:16 -07001210 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001211 * @return True if a vacant cell was found
1212 */
1213 public boolean getVacantCell(int[] vacant, int spanX, int spanY) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001214
Michael Jurka0280c3b2010-09-17 15:00:07 -07001215 return findVacantCell(vacant, spanX, spanY, mCountX, mCountY, mOccupied);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001216 }
1217
1218 static boolean findVacantCell(int[] vacant, int spanX, int spanY,
1219 int xCount, int yCount, boolean[][] occupied) {
1220
1221 for (int x = 0; x < xCount; x++) {
1222 for (int y = 0; y < yCount; y++) {
1223 boolean available = !occupied[x][y];
1224out: for (int i = x; i < x + spanX - 1 && x < xCount; i++) {
1225 for (int j = y; j < y + spanY - 1 && y < yCount; j++) {
1226 available = available && !occupied[i][j];
1227 if (!available) break out;
1228 }
1229 }
1230
1231 if (available) {
1232 vacant[0] = x;
1233 vacant[1] = y;
1234 return true;
1235 }
1236 }
1237 }
1238
1239 return false;
1240 }
1241
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001242 /**
1243 * Update the array of occupied cells (mOccupied), and return a flattened copy of the array.
1244 */
1245 boolean[] getOccupiedCellsFlattened() {
Adam Cohend22015c2010-07-26 22:02:18 -07001246 final int xCount = mCountX;
1247 final int yCount = mCountY;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001248 final boolean[][] occupied = mOccupied;
1249
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001250 final boolean[] flat = new boolean[xCount * yCount];
1251 for (int y = 0; y < yCount; y++) {
1252 for (int x = 0; x < xCount; x++) {
1253 flat[y * xCount + x] = occupied[x][y];
1254 }
1255 }
1256
1257 return flat;
1258 }
1259
Michael Jurka0280c3b2010-09-17 15:00:07 -07001260 private void clearOccupiedCells() {
1261 for (int x = 0; x < mCountX; x++) {
1262 for (int y = 0; y < mCountY; y++) {
1263 mOccupied[x][y] = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001264 }
1265 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001266 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001267
Michael Jurka0280c3b2010-09-17 15:00:07 -07001268 public void onMove(View view, int newCellX, int newCellY) {
1269 LayoutParams lp = (LayoutParams) view.getLayoutParams();
1270 markCellsAsUnoccupiedForView(view);
1271 markCellsForView(newCellX, newCellY, lp.cellHSpan, lp.cellVSpan, true);
1272 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001273
Michael Jurka0280c3b2010-09-17 15:00:07 -07001274 private void markCellsAsOccupiedForView(View view) {
Michael Jurkac6ee42e2010-09-30 12:04:50 -07001275 if (view == null || view.getParent() != this) return;
Michael Jurka0280c3b2010-09-17 15:00:07 -07001276 LayoutParams lp = (LayoutParams) view.getLayoutParams();
1277 markCellsForView(lp.cellX, lp.cellY, lp.cellHSpan, lp.cellVSpan, true);
1278 }
1279
1280 private void markCellsAsUnoccupiedForView(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, false);
1284 }
1285
1286 private void markCellsForView(int cellX, int cellY, int spanX, int spanY, boolean value) {
1287 for (int x = cellX; x < cellX + spanX && x < mCountX; x++) {
1288 for (int y = cellY; y < cellY + spanY && y < mCountY; y++) {
1289 mOccupied[x][y] = value;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001290 }
1291 }
1292 }
1293
1294 @Override
1295 public ViewGroup.LayoutParams generateLayoutParams(AttributeSet attrs) {
1296 return new CellLayout.LayoutParams(getContext(), attrs);
1297 }
1298
1299 @Override
1300 protected boolean checkLayoutParams(ViewGroup.LayoutParams p) {
1301 return p instanceof CellLayout.LayoutParams;
1302 }
1303
1304 @Override
1305 protected ViewGroup.LayoutParams generateLayoutParams(ViewGroup.LayoutParams p) {
1306 return new CellLayout.LayoutParams(p);
1307 }
1308
Winson Chungaafa03c2010-06-11 17:34:16 -07001309 public static class CellLayoutAnimationController extends LayoutAnimationController {
1310 public CellLayoutAnimationController(Animation animation, float delay) {
1311 super(animation, delay);
1312 }
1313
1314 @Override
1315 protected long getDelayForView(View view) {
1316 return (int) (Math.random() * 150);
1317 }
1318 }
1319
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001320 public static class LayoutParams extends ViewGroup.MarginLayoutParams {
1321 /**
1322 * Horizontal location of the item in the grid.
1323 */
1324 @ViewDebug.ExportedProperty
1325 public int cellX;
1326
1327 /**
1328 * Vertical location of the item in the grid.
1329 */
1330 @ViewDebug.ExportedProperty
1331 public int cellY;
1332
1333 /**
1334 * Number of cells spanned horizontally by the item.
1335 */
1336 @ViewDebug.ExportedProperty
1337 public int cellHSpan;
1338
1339 /**
1340 * Number of cells spanned vertically by the item.
1341 */
1342 @ViewDebug.ExportedProperty
1343 public int cellVSpan;
Winson Chungaafa03c2010-06-11 17:34:16 -07001344
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001345 /**
1346 * Is this item currently being dragged
1347 */
1348 public boolean isDragging;
1349
1350 // X coordinate of the view in the layout.
1351 @ViewDebug.ExportedProperty
1352 int x;
1353 // Y coordinate of the view in the layout.
1354 @ViewDebug.ExportedProperty
1355 int y;
1356
Romain Guy84f296c2009-11-04 15:00:44 -08001357 boolean dropped;
Romain Guyfcb9e712009-10-02 16:06:52 -07001358
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001359 public LayoutParams(Context c, AttributeSet attrs) {
1360 super(c, attrs);
1361 cellHSpan = 1;
1362 cellVSpan = 1;
1363 }
1364
1365 public LayoutParams(ViewGroup.LayoutParams source) {
1366 super(source);
1367 cellHSpan = 1;
1368 cellVSpan = 1;
1369 }
Winson Chungaafa03c2010-06-11 17:34:16 -07001370
1371 public LayoutParams(LayoutParams source) {
1372 super(source);
1373 this.cellX = source.cellX;
1374 this.cellY = source.cellY;
1375 this.cellHSpan = source.cellHSpan;
1376 this.cellVSpan = source.cellVSpan;
1377 }
1378
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001379 public LayoutParams(int cellX, int cellY, int cellHSpan, int cellVSpan) {
Romain Guy8f19cdd2010-01-08 15:07:00 -08001380 super(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001381 this.cellX = cellX;
1382 this.cellY = cellY;
1383 this.cellHSpan = cellHSpan;
1384 this.cellVSpan = cellVSpan;
1385 }
1386
1387 public void setup(int cellWidth, int cellHeight, int widthGap, int heightGap,
1388 int hStartPadding, int vStartPadding) {
Winson Chungaafa03c2010-06-11 17:34:16 -07001389
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001390 final int myCellHSpan = cellHSpan;
1391 final int myCellVSpan = cellVSpan;
1392 final int myCellX = cellX;
1393 final int myCellY = cellY;
Winson Chungaafa03c2010-06-11 17:34:16 -07001394
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001395 width = myCellHSpan * cellWidth + ((myCellHSpan - 1) * widthGap) -
1396 leftMargin - rightMargin;
1397 height = myCellVSpan * cellHeight + ((myCellVSpan - 1) * heightGap) -
1398 topMargin - bottomMargin;
1399
1400 x = hStartPadding + myCellX * (cellWidth + widthGap) + leftMargin;
1401 y = vStartPadding + myCellY * (cellHeight + heightGap) + topMargin;
1402 }
Winson Chungaafa03c2010-06-11 17:34:16 -07001403
1404 public String toString() {
1405 return "(" + this.cellX + ", " + this.cellY + ")";
1406 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001407 }
1408
Michael Jurka0280c3b2010-09-17 15:00:07 -07001409 // This class stores info for two purposes:
1410 // 1. When dragging items (mDragInfo in Workspace), we store the View, its cellX & cellY,
1411 // its spanX, spanY, and the screen it is on
1412 // 2. When long clicking on an empty cell in a CellLayout, we save information about the
1413 // cellX and cellY coordinates and which page was clicked. We then set this as a tag on
1414 // the CellLayout that was long clicked
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001415 static final class CellInfo implements ContextMenu.ContextMenuInfo {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001416 View cell;
Michael Jurkaa63c4522010-08-19 13:52:27 -07001417 int cellX = -1;
1418 int cellY = -1;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001419 int spanX;
1420 int spanY;
1421 int screen;
1422 boolean valid;
1423
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001424 @Override
1425 public String toString() {
Winson Chungaafa03c2010-06-11 17:34:16 -07001426 return "Cell[view=" + (cell == null ? "null" : cell.getClass())
1427 + ", x=" + cellX + ", y=" + cellY + "]";
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001428 }
1429 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001430}