blob: c7c850b65e692aaf20c1e347d6021f1e341987c7 [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 }
219
Joe Onorato4be866d2010-10-10 11:26:02 -0700220 // Try to prevent it from continuing to run
221 animation.cancel();
222 } else {
Chet Haase472b2812010-10-14 07:02:04 -0700223 mDragOutlineAlphas[thisIndex] = (Float) animation.getAnimatedValue();
Joe Onorato4be866d2010-10-10 11:26:02 -0700224 final int left = mDragOutlines[thisIndex].x;
225 final int top = mDragOutlines[thisIndex].y;
226 CellLayout.this.invalidate(left, top,
227 left + outline.getWidth(), top + outline.getHeight());
228 }
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700229 }
230 });
Joe Onorato4be866d2010-10-10 11:26:02 -0700231 // The animation holds a reference to the drag outline bitmap as long is it's
232 // running. This way the bitmap can be GCed when the animations are complete.
Chet Haase472b2812010-10-14 07:02:04 -0700233 anim.getAnimator().addListener(new AnimatorListenerAdapter() {
Joe Onorato4be866d2010-10-10 11:26:02 -0700234 public void onAnimationEnd(Animator animation) {
Chet Haase472b2812010-10-14 07:02:04 -0700235 if ((Float) ((ValueAnimator) animation).getAnimatedValue() == 0f) {
Joe Onorato4be866d2010-10-10 11:26:02 -0700236 anim.setTag(null);
237 }
238 }
239 });
240 mDragOutlineAnims[i] = anim;
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700241 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800242 }
243
Michael Jurkaa63c4522010-08-19 13:52:27 -0700244 public void setHover(boolean value) {
245 if (mHover != value) {
Michael Jurka3e7c7632010-10-02 16:01:03 -0700246 mHover = value;
Michael Jurkaa63c4522010-08-19 13:52:27 -0700247 invalidate();
248 }
Michael Jurkaa63c4522010-08-19 13:52:27 -0700249 }
250
Patrick Dubroy1262e362010-10-06 15:49:50 -0700251 public void drawChildren(Canvas canvas) {
252 super.dispatchDraw(canvas);
253 }
254
Jeff Sharkey83f111d2009-04-20 21:03:13 -0700255 @Override
Patrick Dubroy1262e362010-10-06 15:49:50 -0700256 protected void onDraw(Canvas canvas) {
Michael Jurka3e7c7632010-10-02 16:01:03 -0700257 // When we're large, we are either drawn in a "hover" state (ie when dragging an item to
258 // a neighboring page) or with just a normal background (if backgroundAlpha > 0.0f)
259 // When we're small, we are either drawn normally or in the "accepts drops" state (during
260 // a drag). However, we also drag the mini hover background *over* one of those two
261 // backgrounds
Michael Jurka5f1c5092010-09-03 14:15:02 -0700262 if (mBackgroundAlpha > 0.0f) {
Adam Cohenf34bab52010-09-30 14:11:56 -0700263 Drawable bg;
Patrick Dubroy1262e362010-10-06 15:49:50 -0700264 if (getScaleX() < 0.5f) {
Michael Jurka3e7c7632010-10-02 16:01:03 -0700265 bg = mAcceptsDrops ? mBackgroundMiniAcceptsDrops : mBackgroundMini;
Adam Cohenf34bab52010-09-30 14:11:56 -0700266 } else {
Patrick Dubroy1262e362010-10-06 15:49:50 -0700267 bg = mHover ? mBackgroundHover : mBackground;
Adam Cohenf34bab52010-09-30 14:11:56 -0700268 }
Adam Cohen9c4949e2010-10-05 12:27:22 -0700269 if (bg != null) {
270 bg.setAlpha((int) (mBackgroundAlpha * 255));
271 bg.draw(canvas);
272 }
Michael Jurka3e7c7632010-10-02 16:01:03 -0700273 if (mHover && getScaleX() < 0.5f) {
274 mBackgroundMiniHover.setAlpha((int) (mBackgroundAlpha * 255));
275 mBackgroundMiniHover.draw(canvas);
276 }
Michael Jurkaa63c4522010-08-19 13:52:27 -0700277 }
Romain Guya6abce82009-11-10 02:54:41 -0800278
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700279 if (mCrosshairsVisibility > 0.0f) {
280 final int countX = mCountX;
281 final int countY = mCountY;
282
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700283 final float MAX_ALPHA = 0.4f;
284 final int MAX_VISIBLE_DISTANCE = 600;
285 final float DISTANCE_MULTIPLIER = 0.002f;
286
287 final Drawable d = mCrosshairsDrawable;
288 final int width = d.getIntrinsicWidth();
289 final int height = d.getIntrinsicHeight();
290
291 int x = getLeftPadding() - (mWidthGap / 2) - (width / 2);
292 for (int col = 0; col <= countX; col++) {
293 int y = getTopPadding() - (mHeightGap / 2) - (height / 2);
294 for (int row = 0; row <= countY; row++) {
295 mTmpPointF.set(x - mDragCenter.x, y - mDragCenter.y);
296 float dist = mTmpPointF.length();
297 // Crosshairs further from the drag point are more faint
298 float alpha = Math.min(MAX_ALPHA,
299 DISTANCE_MULTIPLIER * (MAX_VISIBLE_DISTANCE - dist));
300 if (alpha > 0.0f) {
301 d.setBounds(x, y, x + width, y + height);
302 d.setAlpha((int) (alpha * 255 * mCrosshairsVisibility));
303 d.draw(canvas);
304 }
305 y += mCellHeight + mHeightGap;
306 }
307 x += mCellWidth + mWidthGap;
308 }
Joe Onorato4be866d2010-10-10 11:26:02 -0700309 }
Winson Chung150fbab2010-09-29 17:14:26 -0700310
Joe Onorato4be866d2010-10-10 11:26:02 -0700311 final Paint paint = new Paint();
312 for (int i = 0; i < mDragOutlines.length; i++) {
Chet Haase472b2812010-10-14 07:02:04 -0700313 final float alpha = mDragOutlineAlphas[i];
Joe Onorato4be866d2010-10-10 11:26:02 -0700314 if (alpha > 0) {
315 final Point p = mDragOutlines[i];
316 final Bitmap b = (Bitmap) mDragOutlineAnims[i].getTag();
Chet Haase472b2812010-10-14 07:02:04 -0700317 paint.setAlpha((int)(alpha + .5f));
Joe Onorato4be866d2010-10-10 11:26:02 -0700318 canvas.drawBitmap(b, p.x, p.y, paint);
Winson Chung150fbab2010-09-29 17:14:26 -0700319 }
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700320 }
321 }
322
Adam Cohenf34bab52010-09-30 14:11:56 -0700323 public void setDimmableProgress(float progress) {
324 for (int i = 0; i < getChildCount(); i++) {
325 Dimmable d = (Dimmable) getChildAt(i);
326 d.setDimmableProgress(progress);
327 }
328 }
329
330 public float getDimmableProgress() {
331 if (getChildCount() > 0) {
332 return ((Dimmable) getChildAt(0)).getDimmableProgress();
333 }
334 return 0.0f;
335 }
336
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700337 @Override
Jeff Sharkey83f111d2009-04-20 21:03:13 -0700338 public void cancelLongPress() {
339 super.cancelLongPress();
340
341 // Cancel long press for all children
342 final int count = getChildCount();
343 for (int i = 0; i < count; i++) {
344 final View child = getChildAt(i);
345 child.cancelLongPress();
346 }
347 }
348
Michael Jurkadee05892010-07-27 10:01:56 -0700349 public void setOnInterceptTouchListener(View.OnTouchListener listener) {
350 mInterceptTouchListener = listener;
351 }
352
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800353 int getCountX() {
Adam Cohend22015c2010-07-26 22:02:18 -0700354 return mCountX;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800355 }
356
357 int getCountY() {
Adam Cohend22015c2010-07-26 22:02:18 -0700358 return mCountY;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800359 }
360
Winson Chungaafa03c2010-06-11 17:34:16 -0700361 public boolean addViewToCellLayout(View child, int index, int childId, LayoutParams params) {
362 final LayoutParams lp = params;
363
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800364 // Generate an id for each view, this assumes we have at most 256x256 cells
365 // per workspace screen
Adam Cohend22015c2010-07-26 22:02:18 -0700366 if (lp.cellX >= 0 && lp.cellX <= mCountX - 1 && lp.cellY >= 0 && lp.cellY <= mCountY - 1) {
Winson Chungaafa03c2010-06-11 17:34:16 -0700367 // If the horizontal or vertical span is set to -1, it is taken to
368 // mean that it spans the extent of the CellLayout
Adam Cohend22015c2010-07-26 22:02:18 -0700369 if (lp.cellHSpan < 0) lp.cellHSpan = mCountX;
370 if (lp.cellVSpan < 0) lp.cellVSpan = mCountY;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800371
Winson Chungaafa03c2010-06-11 17:34:16 -0700372 child.setId(childId);
373
Michael Jurkadee05892010-07-27 10:01:56 -0700374 // We might be in the middle or end of shrinking/fading to a dimmed view
375 // Make sure this view's alpha is set the same as all the rest of the views
Michael Jurka5f1c5092010-09-03 14:15:02 -0700376 child.setAlpha(getAlpha());
Winson Chungaafa03c2010-06-11 17:34:16 -0700377 addView(child, index, lp);
Michael Jurkadee05892010-07-27 10:01:56 -0700378
Michael Jurka0280c3b2010-09-17 15:00:07 -0700379 markCellsAsOccupiedForView(child);
380
Winson Chungaafa03c2010-06-11 17:34:16 -0700381 return true;
382 }
383 return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800384 }
Michael Jurka3e7c7632010-10-02 16:01:03 -0700385 public void setAcceptsDrops(boolean acceptsDrops) {
386 if (mAcceptsDrops != acceptsDrops) {
387 mAcceptsDrops = acceptsDrops;
388 invalidate();
389 }
390 }
391
392 public boolean getAcceptsDrops() {
393 return mAcceptsDrops;
394 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800395
396 @Override
Michael Jurka0280c3b2010-09-17 15:00:07 -0700397 public void removeAllViews() {
398 clearOccupiedCells();
399 }
400
401 @Override
402 public void removeAllViewsInLayout() {
403 clearOccupiedCells();
404 }
405
406 @Override
407 public void removeView(View view) {
408 markCellsAsUnoccupiedForView(view);
409 super.removeView(view);
410 }
411
412 @Override
413 public void removeViewAt(int index) {
414 markCellsAsUnoccupiedForView(getChildAt(index));
415 super.removeViewAt(index);
416 }
417
418 @Override
419 public void removeViewInLayout(View view) {
420 markCellsAsUnoccupiedForView(view);
421 super.removeViewInLayout(view);
422 }
423
424 @Override
425 public void removeViews(int start, int count) {
426 for (int i = start; i < start + count; i++) {
427 markCellsAsUnoccupiedForView(getChildAt(i));
428 }
429 super.removeViews(start, count);
430 }
431
432 @Override
433 public void removeViewsInLayout(int start, int count) {
434 for (int i = start; i < start + count; i++) {
435 markCellsAsUnoccupiedForView(getChildAt(i));
436 }
437 super.removeViewsInLayout(start, count);
438 }
439
440 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800441 public void requestChildFocus(View child, View focused) {
442 super.requestChildFocus(child, focused);
443 if (child != null) {
444 Rect r = new Rect();
445 child.getDrawingRect(r);
446 requestRectangleOnScreen(r);
447 }
448 }
449
450 @Override
451 protected void onAttachedToWindow() {
452 super.onAttachedToWindow();
453 mCellInfo.screen = ((ViewGroup) getParent()).indexOfChild(this);
454 }
455
Michael Jurkaaf442092010-06-10 17:01:57 -0700456 public void setTagToCellInfoForPoint(int touchX, int touchY) {
457 final CellInfo cellInfo = mCellInfo;
458 final Rect frame = mRect;
459 final int x = touchX + mScrollX;
460 final int y = touchY + mScrollY;
461 final int count = getChildCount();
462
463 boolean found = false;
464 for (int i = count - 1; i >= 0; i--) {
465 final View child = getChildAt(i);
466
467 if ((child.getVisibility()) == VISIBLE || child.getAnimation() != null) {
468 child.getHitRect(frame);
469 if (frame.contains(x, y)) {
470 final LayoutParams lp = (LayoutParams) child.getLayoutParams();
471 cellInfo.cell = child;
472 cellInfo.cellX = lp.cellX;
473 cellInfo.cellY = lp.cellY;
474 cellInfo.spanX = lp.cellHSpan;
475 cellInfo.spanY = lp.cellVSpan;
476 cellInfo.valid = true;
477 found = true;
Michael Jurkaaf442092010-06-10 17:01:57 -0700478 break;
479 }
480 }
481 }
Winson Chungaafa03c2010-06-11 17:34:16 -0700482
Michael Jurkaaf442092010-06-10 17:01:57 -0700483 if (!found) {
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700484 final int cellXY[] = mTmpCellXY;
Michael Jurkaaf442092010-06-10 17:01:57 -0700485 pointToCellExact(x, y, cellXY);
486
Michael Jurkaaf442092010-06-10 17:01:57 -0700487 cellInfo.cell = null;
488 cellInfo.cellX = cellXY[0];
489 cellInfo.cellY = cellXY[1];
490 cellInfo.spanX = 1;
491 cellInfo.spanY = 1;
Michael Jurka0280c3b2010-09-17 15:00:07 -0700492 cellInfo.valid = cellXY[0] >= 0 && cellXY[1] >= 0 && cellXY[0] < mCountX &&
493 cellXY[1] < mCountY && !mOccupied[cellXY[0]][cellXY[1]];
Michael Jurkaaf442092010-06-10 17:01:57 -0700494 }
495 setTag(cellInfo);
496 }
497
Winson Chungaafa03c2010-06-11 17:34:16 -0700498
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800499 @Override
500 public boolean onInterceptTouchEvent(MotionEvent ev) {
Michael Jurkadee05892010-07-27 10:01:56 -0700501 if (mInterceptTouchListener != null && mInterceptTouchListener.onTouch(this, ev)) {
502 return true;
503 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800504 final int action = ev.getAction();
505 final CellInfo cellInfo = mCellInfo;
506
507 if (action == MotionEvent.ACTION_DOWN) {
Michael Jurkaaf442092010-06-10 17:01:57 -0700508 setTagToCellInfoForPoint((int) ev.getX(), (int) ev.getY());
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800509 } else if (action == MotionEvent.ACTION_UP) {
510 cellInfo.cell = null;
511 cellInfo.cellX = -1;
512 cellInfo.cellY = -1;
513 cellInfo.spanX = 0;
514 cellInfo.spanY = 0;
515 cellInfo.valid = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800516 setTag(cellInfo);
517 }
518
519 return false;
520 }
521
522 @Override
523 public CellInfo getTag() {
Michael Jurka0280c3b2010-09-17 15:00:07 -0700524 return (CellInfo) super.getTag();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800525 }
526
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700527 /**
528 * Check if the row 'y' is empty from columns 'left' to 'right', inclusive.
529 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800530 private static boolean isRowEmpty(int y, int left, int right, boolean[][] occupied) {
531 for (int x = left; x <= right; x++) {
532 if (occupied[x][y]) {
533 return false;
534 }
535 }
536 return true;
537 }
538
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800539 /**
Winson Chungaafa03c2010-06-11 17:34:16 -0700540 * Given a point, return the cell that strictly encloses that point
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800541 * @param x X coordinate of the point
542 * @param y Y coordinate of the point
543 * @param result Array of 2 ints to hold the x and y coordinate of the cell
544 */
545 void pointToCellExact(int x, int y, int[] result) {
Winson Chungaafa03c2010-06-11 17:34:16 -0700546 final int hStartPadding = getLeftPadding();
547 final int vStartPadding = getTopPadding();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800548
549 result[0] = (x - hStartPadding) / (mCellWidth + mWidthGap);
550 result[1] = (y - vStartPadding) / (mCellHeight + mHeightGap);
551
Adam Cohend22015c2010-07-26 22:02:18 -0700552 final int xAxis = mCountX;
553 final int yAxis = mCountY;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800554
555 if (result[0] < 0) result[0] = 0;
556 if (result[0] >= xAxis) result[0] = xAxis - 1;
557 if (result[1] < 0) result[1] = 0;
558 if (result[1] >= yAxis) result[1] = yAxis - 1;
559 }
Winson Chungaafa03c2010-06-11 17:34:16 -0700560
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800561 /**
562 * Given a point, return the cell that most closely encloses that point
563 * @param x X coordinate of the point
564 * @param y Y coordinate of the point
565 * @param result Array of 2 ints to hold the x and y coordinate of the cell
566 */
567 void pointToCellRounded(int x, int y, int[] result) {
568 pointToCellExact(x + (mCellWidth / 2), y + (mCellHeight / 2), result);
569 }
570
571 /**
572 * Given a cell coordinate, return the point that represents the upper left corner of that cell
Winson Chungaafa03c2010-06-11 17:34:16 -0700573 *
574 * @param cellX X coordinate of the cell
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800575 * @param cellY Y coordinate of the cell
Winson Chungaafa03c2010-06-11 17:34:16 -0700576 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800577 * @param result Array of 2 ints to hold the x and y coordinate of the point
578 */
579 void cellToPoint(int cellX, int cellY, int[] result) {
Winson Chungaafa03c2010-06-11 17:34:16 -0700580 final int hStartPadding = getLeftPadding();
581 final int vStartPadding = getTopPadding();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800582
583 result[0] = hStartPadding + cellX * (mCellWidth + mWidthGap);
584 result[1] = vStartPadding + cellY * (mCellHeight + mHeightGap);
585 }
586
Romain Guy84f296c2009-11-04 15:00:44 -0800587 int getCellWidth() {
588 return mCellWidth;
589 }
590
591 int getCellHeight() {
592 return mCellHeight;
593 }
594
Romain Guy1a304a12009-11-10 00:02:32 -0800595 int getLeftPadding() {
Winson Chungaafa03c2010-06-11 17:34:16 -0700596 return mLeftPadding;
Romain Guy1a304a12009-11-10 00:02:32 -0800597 }
598
599 int getTopPadding() {
Winson Chungaafa03c2010-06-11 17:34:16 -0700600 return mTopPadding;
Romain Guy1a304a12009-11-10 00:02:32 -0800601 }
602
603 int getRightPadding() {
Winson Chungaafa03c2010-06-11 17:34:16 -0700604 return mRightPadding;
Romain Guy1a304a12009-11-10 00:02:32 -0800605 }
606
607 int getBottomPadding() {
Winson Chungaafa03c2010-06-11 17:34:16 -0700608 return mBottomPadding;
Romain Guy1a304a12009-11-10 00:02:32 -0800609 }
610
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800611 @Override
612 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
613 // TODO: currently ignoring padding
Winson Chungaafa03c2010-06-11 17:34:16 -0700614
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800615 int widthSpecMode = MeasureSpec.getMode(widthMeasureSpec);
Winson Chungaafa03c2010-06-11 17:34:16 -0700616 int widthSpecSize = MeasureSpec.getSize(widthMeasureSpec);
617
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800618 int heightSpecMode = MeasureSpec.getMode(heightMeasureSpec);
619 int heightSpecSize = MeasureSpec.getSize(heightMeasureSpec);
Winson Chungaafa03c2010-06-11 17:34:16 -0700620
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800621 if (widthSpecMode == MeasureSpec.UNSPECIFIED || heightSpecMode == MeasureSpec.UNSPECIFIED) {
622 throw new RuntimeException("CellLayout cannot have UNSPECIFIED dimensions");
623 }
624
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800625 final int cellWidth = mCellWidth;
626 final int cellHeight = mCellHeight;
627
Adam Cohend22015c2010-07-26 22:02:18 -0700628 int numWidthGaps = mCountX - 1;
629 int numHeightGaps = mCountY - 1;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800630
Michael Jurka0280c3b2010-09-17 15:00:07 -0700631 int vSpaceLeft = heightSpecSize - mTopPadding - mBottomPadding - (cellHeight * mCountY);
Adam Cohend22015c2010-07-26 22:02:18 -0700632 mHeightGap = vSpaceLeft / numHeightGaps;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800633
Michael Jurka0280c3b2010-09-17 15:00:07 -0700634 int hSpaceLeft = widthSpecSize - mLeftPadding - mRightPadding - (cellWidth * mCountX);
Adam Cohend22015c2010-07-26 22:02:18 -0700635 mWidthGap = hSpaceLeft / numWidthGaps;
Winson Chungaafa03c2010-06-11 17:34:16 -0700636
Michael Jurka5f1c5092010-09-03 14:15:02 -0700637 // center it around the min gaps
638 int minGap = Math.min(mWidthGap, mHeightGap);
639 mWidthGap = mHeightGap = minGap;
640
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800641 int count = getChildCount();
642
643 for (int i = 0; i < count; i++) {
644 View child = getChildAt(i);
645 LayoutParams lp = (LayoutParams) child.getLayoutParams();
Winson Chungaafa03c2010-06-11 17:34:16 -0700646 lp.setup(cellWidth, cellHeight, mWidthGap, mHeightGap,
647 mLeftPadding, mTopPadding);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800648
Michael Jurka0280c3b2010-09-17 15:00:07 -0700649 int childWidthMeasureSpec = MeasureSpec.makeMeasureSpec(lp.width, MeasureSpec.EXACTLY);
Winson Chungaafa03c2010-06-11 17:34:16 -0700650 int childheightMeasureSpec = MeasureSpec.makeMeasureSpec(lp.height,
651 MeasureSpec.EXACTLY);
652
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800653 child.measure(childWidthMeasureSpec, childheightMeasureSpec);
654 }
Michael Jurka5f1c5092010-09-03 14:15:02 -0700655 if (widthSpecMode == MeasureSpec.AT_MOST) {
656 int newWidth = mLeftPadding + mRightPadding + (mCountX * cellWidth) +
657 ((mCountX - 1) * minGap);
658 int newHeight = mTopPadding + mBottomPadding + (mCountY * cellHeight) +
659 ((mCountY - 1) * minGap);
660 setMeasuredDimension(newWidth, newHeight);
661 } else if (widthSpecMode == MeasureSpec.EXACTLY) {
662 setMeasuredDimension(widthSpecSize, heightSpecSize);
663 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800664 }
665
666 @Override
Michael Jurka28750fb2010-09-24 17:43:49 -0700667 protected void onLayout(boolean changed, int l, int t, int r, int b) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800668 int count = getChildCount();
669
670 for (int i = 0; i < count; i++) {
671 View child = getChildAt(i);
672 if (child.getVisibility() != GONE) {
673
674 CellLayout.LayoutParams lp = (CellLayout.LayoutParams) child.getLayoutParams();
675
676 int childLeft = lp.x;
677 int childTop = lp.y;
678 child.layout(childLeft, childTop, childLeft + lp.width, childTop + lp.height);
Romain Guy84f296c2009-11-04 15:00:44 -0800679
680 if (lp.dropped) {
681 lp.dropped = false;
682
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700683 final int[] cellXY = mTmpCellXY;
Romain Guy06762ab2010-01-25 16:51:08 -0800684 getLocationOnScreen(cellXY);
Romain Guy84f296c2009-11-04 15:00:44 -0800685 mWallpaperManager.sendWallpaperCommand(getWindowToken(), "android.home.drop",
Romain Guy06762ab2010-01-25 16:51:08 -0800686 cellXY[0] + childLeft + lp.width / 2,
687 cellXY[1] + childTop + lp.height / 2, 0, null);
Romain Guy84f296c2009-11-04 15:00:44 -0800688 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800689 }
690 }
691 }
692
693 @Override
Michael Jurkadee05892010-07-27 10:01:56 -0700694 protected void onSizeChanged(int w, int h, int oldw, int oldh) {
695 super.onSizeChanged(w, h, oldw, oldh);
Michael Jurka5f1c5092010-09-03 14:15:02 -0700696 if (mBackground != null) {
Patrick Dubroy1262e362010-10-06 15:49:50 -0700697 mBackground.setBounds(0, 0, w, h);
698 }
699 if (mBackgroundHover != null) {
700 mBackgroundHover.setBounds(0, 0, w, h);
Michael Jurkaa63c4522010-08-19 13:52:27 -0700701 }
Adam Cohenf34bab52010-09-30 14:11:56 -0700702 if (mBackgroundMiniHover != null) {
Patrick Dubroy1262e362010-10-06 15:49:50 -0700703 mBackgroundMiniHover.setBounds(0, 0, w, h);
Adam Cohenf34bab52010-09-30 14:11:56 -0700704 }
705 if (mBackgroundMini != null) {
Patrick Dubroy1262e362010-10-06 15:49:50 -0700706 mBackgroundMini.setBounds(0, 0, w, h);
Michael Jurkaa63c4522010-08-19 13:52:27 -0700707 }
Michael Jurka3e7c7632010-10-02 16:01:03 -0700708 if (mBackgroundMiniAcceptsDrops != null) {
709 mBackgroundMiniAcceptsDrops.setBounds(0, 0, w, h);
710 }
Michael Jurkadee05892010-07-27 10:01:56 -0700711 }
712
713 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800714 protected void setChildrenDrawingCacheEnabled(boolean enabled) {
715 final int count = getChildCount();
716 for (int i = 0; i < count; i++) {
717 final View view = getChildAt(i);
718 view.setDrawingCacheEnabled(enabled);
719 // Update the drawing caches
Adam Powellfefa0ce2010-05-03 10:23:50 -0700720 view.buildDrawingCache(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800721 }
722 }
723
724 @Override
725 protected void setChildrenDrawnWithCacheEnabled(boolean enabled) {
726 super.setChildrenDrawnWithCacheEnabled(enabled);
727 }
728
Michael Jurka5f1c5092010-09-03 14:15:02 -0700729 public float getBackgroundAlpha() {
730 return mBackgroundAlpha;
Michael Jurkadee05892010-07-27 10:01:56 -0700731 }
732
Michael Jurka5f1c5092010-09-03 14:15:02 -0700733 public void setBackgroundAlpha(float alpha) {
734 mBackgroundAlpha = alpha;
Michael Jurka0142d492010-08-25 17:46:15 -0700735 invalidate();
Michael Jurkadee05892010-07-27 10:01:56 -0700736 }
737
Michael Jurka5f1c5092010-09-03 14:15:02 -0700738 // Need to return true to let the view system know we know how to handle alpha-- this is
739 // because when our children have an alpha of 0.0f, they are still rendering their "dimmed"
740 // versions
741 @Override
742 protected boolean onSetAlpha(int alpha) {
743 return true;
744 }
745
746 public void setAlpha(float alpha) {
747 setChildrenAlpha(alpha);
748 super.setAlpha(alpha);
749 }
750
Michael Jurkadee05892010-07-27 10:01:56 -0700751 private void setChildrenAlpha(float alpha) {
Michael Jurka0142d492010-08-25 17:46:15 -0700752 final int childCount = getChildCount();
753 for (int i = 0; i < childCount; i++) {
Michael Jurkadee05892010-07-27 10:01:56 -0700754 getChildAt(i).setAlpha(alpha);
755 }
756 }
757
Michael Jurka0280c3b2010-09-17 15:00:07 -0700758 private boolean isVacantIgnoring(
759 int originX, int originY, int spanX, int spanY, View ignoreView) {
760 if (ignoreView != null) {
761 markCellsAsUnoccupiedForView(ignoreView);
762 }
Michael Jurka28750fb2010-09-24 17:43:49 -0700763 boolean isVacant = true;
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700764 for (int i = 0; i < spanY; i++) {
765 if (!isRowEmpty(originY + i, originX, originX + spanX - 1, mOccupied)) {
Michael Jurka28750fb2010-09-24 17:43:49 -0700766 isVacant = false;
767 break;
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700768 }
769 }
Michael Jurka0280c3b2010-09-17 15:00:07 -0700770 if (ignoreView != null) {
771 markCellsAsOccupiedForView(ignoreView);
772 }
Michael Jurka28750fb2010-09-24 17:43:49 -0700773 return isVacant;
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700774 }
775
Michael Jurka0280c3b2010-09-17 15:00:07 -0700776 private boolean isVacant(int originX, int originY, int spanX, int spanY) {
777 return isVacantIgnoring(originX, originY, spanX, spanY, null);
778 }
779
Patrick Dubroy440c3602010-07-13 17:50:32 -0700780 public View getChildAt(int x, int y) {
781 final int count = getChildCount();
782 for (int i = 0; i < count; i++) {
783 View child = getChildAt(i);
784 LayoutParams lp = (LayoutParams) child.getLayoutParams();
785
786 if ((lp.cellX <= x) && (x < lp.cellX + lp.cellHSpan) &&
787 (lp.cellY <= y) && (y < lp.cellY + lp.cellHSpan)) {
788 return child;
789 }
790 }
791 return null;
792 }
793
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700794 /**
Patrick Dubroy8f86ddc2010-07-16 13:55:32 -0700795 * Estimate the size that a child with the given dimensions will take in the layout.
796 */
797 void estimateChildSize(int minWidth, int minHeight, int[] result) {
798 // Assuming it's placed at 0, 0, find where the bottom right cell will land
799 rectToCell(minWidth, minHeight, result);
800
801 // Then figure out the rect it will occupy
802 cellToRect(0, 0, result[0], result[1], mRectF);
803 result[0] = (int)mRectF.width();
804 result[1] = (int)mRectF.height();
805 }
806
807 /**
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700808 * Estimate where the top left cell of the dragged item will land if it is dropped.
809 *
810 * @param originX The X value of the top left corner of the item
811 * @param originY The Y value of the top left corner of the item
812 * @param spanX The number of horizontal cells that the item spans
813 * @param spanY The number of vertical cells that the item spans
814 * @param result The estimated drop cell X and Y.
815 */
816 void estimateDropCell(int originX, int originY, int spanX, int spanY, int[] result) {
Adam Cohend22015c2010-07-26 22:02:18 -0700817 final int countX = mCountX;
818 final int countY = mCountY;
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700819
Michael Jurkaa63c4522010-08-19 13:52:27 -0700820 // pointToCellRounded takes the top left of a cell but will pad that with
821 // cellWidth/2 and cellHeight/2 when finding the matching cell
822 pointToCellRounded(originX, originY, result);
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700823
824 // If the item isn't fully on this screen, snap to the edges
825 int rightOverhang = result[0] + spanX - countX;
826 if (rightOverhang > 0) {
827 result[0] -= rightOverhang; // Snap to right
828 }
829 result[0] = Math.max(0, result[0]); // Snap to left
830 int bottomOverhang = result[1] + spanY - countY;
831 if (bottomOverhang > 0) {
832 result[1] -= bottomOverhang; // Snap to bottom
833 }
834 result[1] = Math.max(0, result[1]); // Snap to top
835 }
836
Joe Onorato4be866d2010-10-10 11:26:02 -0700837 void visualizeDropLocation(
838 View v, Bitmap dragOutline, int originX, int originY, int spanX, int spanY) {
839
840 final int[] nearest = findNearestVacantArea(originX, originY, spanX, spanY, v, mDragCell);
841 mDragCenter.set(originX + (v.getWidth() / 2), originY + (v.getHeight() / 2));
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700842
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700843 if (nearest != null) {
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700844 // Find the top left corner of the rect the object will occupy
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700845 final int[] topLeft = mTmpPoint;
846 cellToPoint(nearest[0], nearest[1], topLeft);
847
Joe Onorato4be866d2010-10-10 11:26:02 -0700848 int left = topLeft[0];
849 int top = topLeft[1];
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700850
Joe Onorato4be866d2010-10-10 11:26:02 -0700851 if (v.getParent() instanceof CellLayout) {
852 LayoutParams lp = (LayoutParams) v.getLayoutParams();
853 left += lp.leftMargin;
854 top += lp.topMargin;
855 }
Winson Chung150fbab2010-09-29 17:14:26 -0700856
Joe Onorato4be866d2010-10-10 11:26:02 -0700857 // Offsets due to the size difference between the View and the dragOutline
858 left += (v.getWidth() - dragOutline.getWidth()) / 2;
859 top += (v.getHeight() - dragOutline.getHeight()) / 2;
Winson Chung150fbab2010-09-29 17:14:26 -0700860
Joe Onorato4be866d2010-10-10 11:26:02 -0700861 final int oldIndex = mDragOutlineCurrent;
862 final Point lastPoint = mDragOutlines[oldIndex];
863 if (lastPoint.x != left || lastPoint.y != top) {
864 mDragOutlineCurrent = (oldIndex + 1) % mDragOutlines.length;
Winson Chung150fbab2010-09-29 17:14:26 -0700865
Joe Onorato4be866d2010-10-10 11:26:02 -0700866 mDragOutlines[mDragOutlineCurrent].set(left, top);
Winson Chung150fbab2010-09-29 17:14:26 -0700867
Joe Onorato4be866d2010-10-10 11:26:02 -0700868 mDragOutlineAnims[oldIndex].animateOut();
869 mDragOutlineAnims[mDragOutlineCurrent].setTag(dragOutline);
870 mDragOutlineAnims[mDragOutlineCurrent].animateIn();
Winson Chung150fbab2010-09-29 17:14:26 -0700871 }
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700872 }
Patrick Dubroy49250ad2010-10-08 15:33:52 -0700873
874 // If we are drawing crosshairs, the entire CellLayout needs to be invalidated
875 if (mCrosshairsDrawable != null) {
876 invalidate();
877 }
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700878 }
879
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800880 /**
Jeff Sharkey70864282009-04-07 21:08:40 -0700881 * Find a vacant area that will fit the given bounds nearest the requested
882 * cell location. Uses Euclidean distance to score multiple vacant areas.
Winson Chungaafa03c2010-06-11 17:34:16 -0700883 *
Romain Guy51afc022009-05-04 18:03:43 -0700884 * @param pixelX The X location at which you want to search for a vacant area.
885 * @param pixelY The Y location at which you want to search for a vacant area.
Jeff Sharkey70864282009-04-07 21:08:40 -0700886 * @param spanX Horizontal span of the object.
887 * @param spanY Vertical span of the object.
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700888 * @param result Array in which to place the result, or null (in which case a new array will
889 * be allocated)
Jeff Sharkey70864282009-04-07 21:08:40 -0700890 * @return The X, Y cell of a vacant area that can contain this object,
891 * nearest the requested location.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800892 */
Michael Jurka6a1435d2010-09-27 17:35:12 -0700893 int[] findNearestVacantArea(
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700894 int pixelX, int pixelY, int spanX, int spanY, int[] result) {
895 return findNearestVacantArea(pixelX, pixelY, spanX, spanY, null, result);
Michael Jurka6a1435d2010-09-27 17:35:12 -0700896 }
Winson Chungaafa03c2010-06-11 17:34:16 -0700897
Michael Jurka6a1435d2010-09-27 17:35:12 -0700898 /**
899 * Find a vacant area that will fit the given bounds nearest the requested
900 * cell location. Uses Euclidean distance to score multiple vacant areas.
901 *
902 * @param pixelX The X location at which you want to search for a vacant area.
903 * @param pixelY The Y location at which you want to search for a vacant area.
904 * @param spanX Horizontal span of the object.
905 * @param spanY Vertical span of the object.
Michael Jurka6a1435d2010-09-27 17:35:12 -0700906 * @param ignoreView Considers space occupied by this view as unoccupied
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700907 * @param result Previously returned value to possibly recycle.
Michael Jurka6a1435d2010-09-27 17:35:12 -0700908 * @return The X, Y cell of a vacant area that can contain this object,
909 * nearest the requested location.
910 */
911 int[] findNearestVacantArea(
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700912 int pixelX, int pixelY, int spanX, int spanY, View ignoreView, int[] result) {
Michael Jurkac6ee42e2010-09-30 12:04:50 -0700913 // mark space take by ignoreView as available (method checks if ignoreView is null)
914 markCellsAsUnoccupiedForView(ignoreView);
915
Jeff Sharkey70864282009-04-07 21:08:40 -0700916 // Keep track of best-scoring drop area
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700917 final int[] bestXY = result != null ? result : new int[2];
Jeff Sharkey70864282009-04-07 21:08:40 -0700918 double bestDistance = Double.MAX_VALUE;
Winson Chungaafa03c2010-06-11 17:34:16 -0700919
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700920 final int countX = mCountX;
921 final int countY = mCountY;
922 final boolean[][] occupied = mOccupied;
923
924 for (int x = 0; x < countX - (spanX - 1); x++) {
Michael Jurkac28de512010-08-13 11:27:44 -0700925 inner:
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700926 for (int y = 0; y < countY - (spanY - 1); y++) {
Michael Jurkac28de512010-08-13 11:27:44 -0700927 for (int i = 0; i < spanX; i++) {
928 for (int j = 0; j < spanY; j++) {
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700929 if (occupied[x + i][y + j]) {
Michael Jurkac28de512010-08-13 11:27:44 -0700930 // small optimization: we can skip to below the row we just found
931 // an occupied cell
932 y += j;
933 continue inner;
934 }
935 }
936 }
937 final int[] cellXY = mTmpCellXY;
938 cellToPoint(x, y, cellXY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800939
Michael Jurkac28de512010-08-13 11:27:44 -0700940 double distance = Math.sqrt(Math.pow(cellXY[0] - pixelX, 2)
941 + Math.pow(cellXY[1] - pixelY, 2));
942 if (distance <= bestDistance) {
943 bestDistance = distance;
944 bestXY[0] = x;
945 bestXY[1] = y;
946 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800947 }
948 }
Michael Jurkac6ee42e2010-09-30 12:04:50 -0700949 // re-mark space taken by ignoreView as occupied
950 markCellsAsOccupiedForView(ignoreView);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800951
Winson Chungaafa03c2010-06-11 17:34:16 -0700952 // Return null if no suitable location found
Jeff Sharkey70864282009-04-07 21:08:40 -0700953 if (bestDistance < Double.MAX_VALUE) {
954 return bestXY;
955 } else {
956 return null;
957 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800958 }
Winson Chungaafa03c2010-06-11 17:34:16 -0700959
Michael Jurka0280c3b2010-09-17 15:00:07 -0700960 boolean existsEmptyCell() {
961 return findCellForSpan(null, 1, 1);
962 }
963
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800964 /**
Michael Jurka0280c3b2010-09-17 15:00:07 -0700965 * Finds the upper-left coordinate of the first rectangle in the grid that can
966 * hold a cell of the specified dimensions. If intersectX and intersectY are not -1,
967 * then this method will only return coordinates for rectangles that contain the cell
968 * (intersectX, intersectY)
969 *
970 * @param cellXY The array that will contain the position of a vacant cell if such a cell
971 * can be found.
972 * @param spanX The horizontal span of the cell we want to find.
973 * @param spanY The vertical span of the cell we want to find.
974 *
975 * @return True if a vacant cell of the specified dimension was found, false otherwise.
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700976 */
Michael Jurka0280c3b2010-09-17 15:00:07 -0700977 boolean findCellForSpan(int[] cellXY, int spanX, int spanY) {
978 return findCellForSpanThatIntersectsIgnoring(cellXY, spanX, spanY, -1, -1, null);
979 }
980
981 /**
982 * Like above, but ignores any cells occupied by the item "ignoreView"
983 *
984 * @param cellXY The array that will contain the position of a vacant cell if such a cell
985 * can be found.
986 * @param spanX The horizontal span of the cell we want to find.
987 * @param spanY The vertical span of the cell we want to find.
988 * @param ignoreView The home screen item we should treat as not occupying any space
989 * @return
990 */
991 boolean findCellForSpanIgnoring(int[] cellXY, int spanX, int spanY, View ignoreView) {
992 return findCellForSpanThatIntersectsIgnoring(cellXY, spanX, spanY, -1, -1, ignoreView);
993 }
994
995 /**
996 * Like above, but if intersectX and intersectY are not -1, then this method will try to
997 * return coordinates for rectangles that contain the cell [intersectX, intersectY]
998 *
999 * @param spanX The horizontal span of the cell we want to find.
1000 * @param spanY The vertical span of the cell we want to find.
1001 * @param ignoreView The home screen item we should treat as not occupying any space
1002 * @param intersectX The X coordinate of the cell that we should try to overlap
1003 * @param intersectX The Y coordinate of the cell that we should try to overlap
1004 *
1005 * @return True if a vacant cell of the specified dimension was found, false otherwise.
1006 */
1007 boolean findCellForSpanThatIntersects(int[] cellXY, int spanX, int spanY,
1008 int intersectX, int intersectY) {
1009 return findCellForSpanThatIntersectsIgnoring(
1010 cellXY, spanX, spanY, intersectX, intersectY, null);
1011 }
1012
1013 /**
1014 * The superset of the above two methods
1015 */
1016 boolean findCellForSpanThatIntersectsIgnoring(int[] cellXY, int spanX, int spanY,
1017 int intersectX, int intersectY, View ignoreView) {
Michael Jurkac6ee42e2010-09-30 12:04:50 -07001018 // mark space take by ignoreView as available (method checks if ignoreView is null)
1019 markCellsAsUnoccupiedForView(ignoreView);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001020
Michael Jurka28750fb2010-09-24 17:43:49 -07001021 boolean foundCell = false;
Michael Jurka0280c3b2010-09-17 15:00:07 -07001022 while (true) {
1023 int startX = 0;
1024 if (intersectX >= 0) {
1025 startX = Math.max(startX, intersectX - (spanX - 1));
1026 }
1027 int endX = mCountX - (spanX - 1);
1028 if (intersectX >= 0) {
1029 endX = Math.min(endX, intersectX + (spanX - 1) + (spanX == 1 ? 1 : 0));
1030 }
1031 int startY = 0;
1032 if (intersectY >= 0) {
1033 startY = Math.max(startY, intersectY - (spanY - 1));
1034 }
1035 int endY = mCountY - (spanY - 1);
1036 if (intersectY >= 0) {
1037 endY = Math.min(endY, intersectY + (spanY - 1) + (spanY == 1 ? 1 : 0));
1038 }
1039
1040 for (int x = startX; x < endX; x++) {
1041 inner:
1042 for (int y = startY; y < endY; y++) {
1043 for (int i = 0; i < spanX; i++) {
1044 for (int j = 0; j < spanY; j++) {
1045 if (mOccupied[x + i][y + j]) {
1046 // small optimization: we can skip to below the row we just found
1047 // an occupied cell
1048 y += j;
1049 continue inner;
1050 }
1051 }
1052 }
1053 if (cellXY != null) {
1054 cellXY[0] = x;
1055 cellXY[1] = y;
1056 }
Michael Jurka28750fb2010-09-24 17:43:49 -07001057 foundCell = true;
1058 break;
Michael Jurka0280c3b2010-09-17 15:00:07 -07001059 }
1060 }
1061 if (intersectX == -1 && intersectY == -1) {
1062 break;
1063 } else {
1064 // if we failed to find anything, try again but without any requirements of
1065 // intersecting
1066 intersectX = -1;
1067 intersectY = -1;
1068 continue;
1069 }
1070 }
1071
Michael Jurkac6ee42e2010-09-30 12:04:50 -07001072 // re-mark space taken by ignoreView as occupied
1073 markCellsAsOccupiedForView(ignoreView);
Michael Jurka28750fb2010-09-24 17:43:49 -07001074 return foundCell;
Michael Jurka0280c3b2010-09-17 15:00:07 -07001075 }
1076
1077 /**
1078 * Called when drag has left this CellLayout or has been completed (successfully or not)
1079 */
1080 void onDragExit() {
Joe Onorato4be866d2010-10-10 11:26:02 -07001081 // This can actually be called when we aren't in a drag, e.g. when adding a new
1082 // item to this layout via the customize drawer.
1083 // Guard against that case.
1084 if (mDragging) {
1085 mDragging = false;
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001086
Joe Onorato4be866d2010-10-10 11:26:02 -07001087 // Invalidate the drag data
1088 mDragCell[0] = -1;
1089 mDragCell[1] = -1;
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001090
Joe Onorato4be866d2010-10-10 11:26:02 -07001091 setHover(false);
1092
1093 // Fade out the drag indicators
1094 if (mCrosshairsAnimator != null) {
1095 mCrosshairsAnimator.animateOut();
1096 }
1097
1098 final int prev = mDragOutlineCurrent;
1099 mDragOutlineAnims[prev].animateOut();
1100 mDragOutlineCurrent = (prev + 1) % mDragOutlines.length;
1101 mDragOutlines[mDragOutlineCurrent].set(-1, -1);
1102 mDragOutlineAlphas[mDragOutlineCurrent] = 0;
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001103 }
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001104 }
1105
1106 /**
Winson Chungaafa03c2010-06-11 17:34:16 -07001107 * Mark a child as having been dropped.
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001108 * At the beginning of the drag operation, the child may have been on another
1109 * screen, but it is reparented before this method is called.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001110 *
1111 * @param child The child that is being dropped
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001112 */
Winson Chungaafa03c2010-06-11 17:34:16 -07001113 void onDropChild(View child) {
Romain Guyd94533d2009-08-17 10:01:15 -07001114 if (child != null) {
1115 LayoutParams lp = (LayoutParams) child.getLayoutParams();
Romain Guyd94533d2009-08-17 10:01:15 -07001116 lp.isDragging = false;
Romain Guy84f296c2009-11-04 15:00:44 -08001117 lp.dropped = true;
Romain Guyd94533d2009-08-17 10:01:15 -07001118 child.requestLayout();
Romain Guyd94533d2009-08-17 10:01:15 -07001119 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001120 onDragExit();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001121 }
1122
1123 void onDropAborted(View child) {
1124 if (child != null) {
1125 ((LayoutParams) child.getLayoutParams()).isDragging = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001126 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001127 onDragExit();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001128 }
1129
1130 /**
1131 * Start dragging the specified child
Winson Chungaafa03c2010-06-11 17:34:16 -07001132 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001133 * @param child The child that is being dragged
1134 */
1135 void onDragChild(View child) {
1136 LayoutParams lp = (LayoutParams) child.getLayoutParams();
1137 lp.isDragging = true;
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001138 }
1139
1140 /**
1141 * A drag event has begun over this layout.
1142 * It may have begun over this layout (in which case onDragChild is called first),
1143 * or it may have begun on another layout.
1144 */
1145 void onDragEnter(View dragView) {
Patrick Dubroyfe6bd872010-10-13 17:32:10 -07001146 if (!mDragging) {
Joe Onorato4be866d2010-10-10 11:26:02 -07001147// Log.d(TAG, "Received onDragEnter while drag still active");
Patrick Dubroyfe6bd872010-10-13 17:32:10 -07001148 // Fade in the drag indicators
1149 if (mCrosshairsAnimator != null) {
1150 mCrosshairsAnimator.animateIn();
1151 }
Joe Onorato4be866d2010-10-10 11:26:02 -07001152 }
1153 mDragging = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001154 }
Winson Chungaafa03c2010-06-11 17:34:16 -07001155
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001156 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001157 * Computes a bounding rectangle for a range of cells
Winson Chungaafa03c2010-06-11 17:34:16 -07001158 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001159 * @param cellX X coordinate of upper left corner expressed as a cell position
1160 * @param cellY Y coordinate of upper left corner expressed as a cell position
Winson Chungaafa03c2010-06-11 17:34:16 -07001161 * @param cellHSpan Width in cells
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001162 * @param cellVSpan Height in cells
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001163 * @param resultRect Rect into which to put the results
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001164 */
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001165 public void cellToRect(int cellX, int cellY, int cellHSpan, int cellVSpan, RectF resultRect) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001166 final int cellWidth = mCellWidth;
1167 final int cellHeight = mCellHeight;
1168 final int widthGap = mWidthGap;
1169 final int heightGap = mHeightGap;
Winson Chungaafa03c2010-06-11 17:34:16 -07001170
1171 final int hStartPadding = getLeftPadding();
1172 final int vStartPadding = getTopPadding();
1173
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001174 int width = cellHSpan * cellWidth + ((cellHSpan - 1) * widthGap);
1175 int height = cellVSpan * cellHeight + ((cellVSpan - 1) * heightGap);
1176
1177 int x = hStartPadding + cellX * (cellWidth + widthGap);
1178 int y = vStartPadding + cellY * (cellHeight + heightGap);
Winson Chungaafa03c2010-06-11 17:34:16 -07001179
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001180 resultRect.set(x, y, x + width, y + height);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001181 }
Winson Chungaafa03c2010-06-11 17:34:16 -07001182
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001183 /**
Winson Chungaafa03c2010-06-11 17:34:16 -07001184 * Computes the required horizontal and vertical cell spans to always
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001185 * fit the given rectangle.
Winson Chungaafa03c2010-06-11 17:34:16 -07001186 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001187 * @param width Width in pixels
1188 * @param height Height in pixels
Patrick Dubroy8f86ddc2010-07-16 13:55:32 -07001189 * @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 -08001190 */
Patrick Dubroy8f86ddc2010-07-16 13:55:32 -07001191 public int[] rectToCell(int width, int height, int[] result) {
Michael Jurka9987a5c2010-10-08 16:58:12 -07001192 return rectToCell(getResources(), width, height, result);
1193 }
1194
1195 public static int[] rectToCell(Resources resources, int width, int height, int[] result) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001196 // Always assume we're working with the smallest span to make sure we
1197 // reserve enough space in both orientations.
Joe Onorato79e56262009-09-21 15:23:04 -04001198 int actualWidth = resources.getDimensionPixelSize(R.dimen.workspace_cell_width);
1199 int actualHeight = resources.getDimensionPixelSize(R.dimen.workspace_cell_height);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001200 int smallerSize = Math.min(actualWidth, actualHeight);
Joe Onorato79e56262009-09-21 15:23:04 -04001201
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001202 // Always round up to next largest cell
1203 int spanX = (width + smallerSize) / smallerSize;
1204 int spanY = (height + smallerSize) / smallerSize;
Joe Onorato79e56262009-09-21 15:23:04 -04001205
Patrick Dubroy8f86ddc2010-07-16 13:55:32 -07001206 if (result == null) {
1207 return new int[] { spanX, spanY };
1208 }
1209 result[0] = spanX;
1210 result[1] = spanY;
1211 return result;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001212 }
1213
1214 /**
1215 * Find the first vacant cell, if there is one.
1216 *
1217 * @param vacant Holds the x and y coordinate of the vacant cell
1218 * @param spanX Horizontal cell span.
1219 * @param spanY Vertical cell span.
Winson Chungaafa03c2010-06-11 17:34:16 -07001220 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001221 * @return True if a vacant cell was found
1222 */
1223 public boolean getVacantCell(int[] vacant, int spanX, int spanY) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001224
Michael Jurka0280c3b2010-09-17 15:00:07 -07001225 return findVacantCell(vacant, spanX, spanY, mCountX, mCountY, mOccupied);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001226 }
1227
1228 static boolean findVacantCell(int[] vacant, int spanX, int spanY,
1229 int xCount, int yCount, boolean[][] occupied) {
1230
1231 for (int x = 0; x < xCount; x++) {
1232 for (int y = 0; y < yCount; y++) {
1233 boolean available = !occupied[x][y];
1234out: for (int i = x; i < x + spanX - 1 && x < xCount; i++) {
1235 for (int j = y; j < y + spanY - 1 && y < yCount; j++) {
1236 available = available && !occupied[i][j];
1237 if (!available) break out;
1238 }
1239 }
1240
1241 if (available) {
1242 vacant[0] = x;
1243 vacant[1] = y;
1244 return true;
1245 }
1246 }
1247 }
1248
1249 return false;
1250 }
1251
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001252 /**
1253 * Update the array of occupied cells (mOccupied), and return a flattened copy of the array.
1254 */
1255 boolean[] getOccupiedCellsFlattened() {
Adam Cohend22015c2010-07-26 22:02:18 -07001256 final int xCount = mCountX;
1257 final int yCount = mCountY;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001258 final boolean[][] occupied = mOccupied;
1259
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001260 final boolean[] flat = new boolean[xCount * yCount];
1261 for (int y = 0; y < yCount; y++) {
1262 for (int x = 0; x < xCount; x++) {
1263 flat[y * xCount + x] = occupied[x][y];
1264 }
1265 }
1266
1267 return flat;
1268 }
1269
Michael Jurka0280c3b2010-09-17 15:00:07 -07001270 private void clearOccupiedCells() {
1271 for (int x = 0; x < mCountX; x++) {
1272 for (int y = 0; y < mCountY; y++) {
1273 mOccupied[x][y] = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001274 }
1275 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001276 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001277
Michael Jurka0280c3b2010-09-17 15:00:07 -07001278 public void onMove(View view, int newCellX, int newCellY) {
1279 LayoutParams lp = (LayoutParams) view.getLayoutParams();
1280 markCellsAsUnoccupiedForView(view);
1281 markCellsForView(newCellX, newCellY, lp.cellHSpan, lp.cellVSpan, true);
1282 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001283
Michael Jurka0280c3b2010-09-17 15:00:07 -07001284 private void markCellsAsOccupiedForView(View view) {
Michael Jurkac6ee42e2010-09-30 12:04:50 -07001285 if (view == null || view.getParent() != this) return;
Michael Jurka0280c3b2010-09-17 15:00:07 -07001286 LayoutParams lp = (LayoutParams) view.getLayoutParams();
1287 markCellsForView(lp.cellX, lp.cellY, lp.cellHSpan, lp.cellVSpan, true);
1288 }
1289
1290 private void markCellsAsUnoccupiedForView(View view) {
Michael Jurkac6ee42e2010-09-30 12:04:50 -07001291 if (view == null || view.getParent() != this) return;
Michael Jurka0280c3b2010-09-17 15:00:07 -07001292 LayoutParams lp = (LayoutParams) view.getLayoutParams();
1293 markCellsForView(lp.cellX, lp.cellY, lp.cellHSpan, lp.cellVSpan, false);
1294 }
1295
1296 private void markCellsForView(int cellX, int cellY, int spanX, int spanY, boolean value) {
1297 for (int x = cellX; x < cellX + spanX && x < mCountX; x++) {
1298 for (int y = cellY; y < cellY + spanY && y < mCountY; y++) {
1299 mOccupied[x][y] = value;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001300 }
1301 }
1302 }
1303
1304 @Override
1305 public ViewGroup.LayoutParams generateLayoutParams(AttributeSet attrs) {
1306 return new CellLayout.LayoutParams(getContext(), attrs);
1307 }
1308
1309 @Override
1310 protected boolean checkLayoutParams(ViewGroup.LayoutParams p) {
1311 return p instanceof CellLayout.LayoutParams;
1312 }
1313
1314 @Override
1315 protected ViewGroup.LayoutParams generateLayoutParams(ViewGroup.LayoutParams p) {
1316 return new CellLayout.LayoutParams(p);
1317 }
1318
Winson Chungaafa03c2010-06-11 17:34:16 -07001319 public static class CellLayoutAnimationController extends LayoutAnimationController {
1320 public CellLayoutAnimationController(Animation animation, float delay) {
1321 super(animation, delay);
1322 }
1323
1324 @Override
1325 protected long getDelayForView(View view) {
1326 return (int) (Math.random() * 150);
1327 }
1328 }
1329
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001330 public static class LayoutParams extends ViewGroup.MarginLayoutParams {
1331 /**
1332 * Horizontal location of the item in the grid.
1333 */
1334 @ViewDebug.ExportedProperty
1335 public int cellX;
1336
1337 /**
1338 * Vertical location of the item in the grid.
1339 */
1340 @ViewDebug.ExportedProperty
1341 public int cellY;
1342
1343 /**
1344 * Number of cells spanned horizontally by the item.
1345 */
1346 @ViewDebug.ExportedProperty
1347 public int cellHSpan;
1348
1349 /**
1350 * Number of cells spanned vertically by the item.
1351 */
1352 @ViewDebug.ExportedProperty
1353 public int cellVSpan;
Winson Chungaafa03c2010-06-11 17:34:16 -07001354
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001355 /**
1356 * Is this item currently being dragged
1357 */
1358 public boolean isDragging;
1359
1360 // X coordinate of the view in the layout.
1361 @ViewDebug.ExportedProperty
1362 int x;
1363 // Y coordinate of the view in the layout.
1364 @ViewDebug.ExportedProperty
1365 int y;
1366
Romain Guy84f296c2009-11-04 15:00:44 -08001367 boolean dropped;
Romain Guyfcb9e712009-10-02 16:06:52 -07001368
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001369 public LayoutParams(Context c, AttributeSet attrs) {
1370 super(c, attrs);
1371 cellHSpan = 1;
1372 cellVSpan = 1;
1373 }
1374
1375 public LayoutParams(ViewGroup.LayoutParams source) {
1376 super(source);
1377 cellHSpan = 1;
1378 cellVSpan = 1;
1379 }
Winson Chungaafa03c2010-06-11 17:34:16 -07001380
1381 public LayoutParams(LayoutParams source) {
1382 super(source);
1383 this.cellX = source.cellX;
1384 this.cellY = source.cellY;
1385 this.cellHSpan = source.cellHSpan;
1386 this.cellVSpan = source.cellVSpan;
1387 }
1388
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001389 public LayoutParams(int cellX, int cellY, int cellHSpan, int cellVSpan) {
Romain Guy8f19cdd2010-01-08 15:07:00 -08001390 super(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001391 this.cellX = cellX;
1392 this.cellY = cellY;
1393 this.cellHSpan = cellHSpan;
1394 this.cellVSpan = cellVSpan;
1395 }
1396
1397 public void setup(int cellWidth, int cellHeight, int widthGap, int heightGap,
1398 int hStartPadding, int vStartPadding) {
Winson Chungaafa03c2010-06-11 17:34:16 -07001399
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001400 final int myCellHSpan = cellHSpan;
1401 final int myCellVSpan = cellVSpan;
1402 final int myCellX = cellX;
1403 final int myCellY = cellY;
Winson Chungaafa03c2010-06-11 17:34:16 -07001404
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001405 width = myCellHSpan * cellWidth + ((myCellHSpan - 1) * widthGap) -
1406 leftMargin - rightMargin;
1407 height = myCellVSpan * cellHeight + ((myCellVSpan - 1) * heightGap) -
1408 topMargin - bottomMargin;
1409
1410 x = hStartPadding + myCellX * (cellWidth + widthGap) + leftMargin;
1411 y = vStartPadding + myCellY * (cellHeight + heightGap) + topMargin;
1412 }
Winson Chungaafa03c2010-06-11 17:34:16 -07001413
1414 public String toString() {
1415 return "(" + this.cellX + ", " + this.cellY + ")";
1416 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001417 }
1418
Michael Jurka0280c3b2010-09-17 15:00:07 -07001419 // This class stores info for two purposes:
1420 // 1. When dragging items (mDragInfo in Workspace), we store the View, its cellX & cellY,
1421 // its spanX, spanY, and the screen it is on
1422 // 2. When long clicking on an empty cell in a CellLayout, we save information about the
1423 // cellX and cellY coordinates and which page was clicked. We then set this as a tag on
1424 // the CellLayout that was long clicked
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001425 static final class CellInfo implements ContextMenu.ContextMenuInfo {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001426 View cell;
Michael Jurkaa63c4522010-08-19 13:52:27 -07001427 int cellX = -1;
1428 int cellY = -1;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001429 int spanX;
1430 int spanY;
1431 int screen;
1432 boolean valid;
1433
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001434 @Override
1435 public String toString() {
Winson Chungaafa03c2010-06-11 17:34:16 -07001436 return "Cell[view=" + (cell == null ? "null" : cell.getClass())
1437 + ", x=" + cellX + ", y=" + cellY + "]";
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001438 }
1439 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001440}