blob: 2ddebe77fb84f5d414be1f8e3be3c557168de78a [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
Michael Jurka3c4c20f2010-10-28 15:36:06 -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;
Michael Jurka18014792010-10-14 09:01:34 -070023import android.animation.AnimatorSet;
24import android.animation.ObjectAnimator;
Chet Haase00397b12010-10-07 11:13:10 -070025import android.animation.TimeInterpolator;
Patrick Dubroyde7658b2010-09-27 11:15:43 -070026import android.animation.ValueAnimator;
27import android.animation.ValueAnimator.AnimatorUpdateListener;
Winson Chungaafa03c2010-06-11 17:34:16 -070028import android.app.WallpaperManager;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080029import android.content.Context;
Joe Onorato79e56262009-09-21 15:23:04 -040030import android.content.res.Resources;
Winson Chungaafa03c2010-06-11 17:34:16 -070031import android.content.res.TypedArray;
Joe Onorato4be866d2010-10-10 11:26:02 -070032import android.graphics.Bitmap;
Winson Chungaafa03c2010-06-11 17:34:16 -070033import android.graphics.Canvas;
Joe Onorato4be866d2010-10-10 11:26:02 -070034import android.graphics.Paint;
Patrick Dubroyde7658b2010-09-27 11:15:43 -070035import android.graphics.Point;
36import android.graphics.PointF;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080037import android.graphics.Rect;
38import android.graphics.RectF;
Michael Jurka18014792010-10-14 09:01:34 -070039import android.graphics.Region;
Michael Jurkabea15192010-11-17 12:33:46 -080040import android.graphics.Bitmap.Config;
41import android.graphics.PorterDuff.Mode;
Patrick Dubroy6569f2c2010-07-12 14:25:18 -070042import android.graphics.drawable.Drawable;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080043import android.util.AttributeSet;
Joe Onorato4be866d2010-10-10 11:26:02 -070044import android.util.Log;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080045import android.view.ContextMenu;
46import android.view.MotionEvent;
47import android.view.View;
48import android.view.ViewDebug;
49import android.view.ViewGroup;
Winson Chungaafa03c2010-06-11 17:34:16 -070050import android.view.animation.Animation;
Winson Chung150fbab2010-09-29 17:14:26 -070051import android.view.animation.DecelerateInterpolator;
Winson Chungaafa03c2010-06-11 17:34:16 -070052import android.view.animation.LayoutAnimationController;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080053
Michael Jurka3c4c20f2010-10-28 15:36:06 -070054import java.util.Arrays;
Patrick Dubroy8e58e912010-10-14 13:21:48 -070055
Adam Cohenf34bab52010-09-30 14:11:56 -070056public class CellLayout extends ViewGroup implements Dimmable {
Winson Chungaafa03c2010-06-11 17:34:16 -070057 static final String TAG = "CellLayout";
58
The Android Open Source Project31dd5032009-03-03 19:32:27 -080059 private int mCellWidth;
60 private int mCellHeight;
Winson Chungaafa03c2010-06-11 17:34:16 -070061
Winson Chungaafa03c2010-06-11 17:34:16 -070062 private int mLeftPadding;
63 private int mRightPadding;
64 private int mTopPadding;
65 private int mBottomPadding;
66
Adam Cohend22015c2010-07-26 22:02:18 -070067 private int mCountX;
68 private int mCountY;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080069
70 private int mWidthGap;
71 private int mHeightGap;
72
73 private final Rect mRect = new Rect();
74 private final CellInfo mCellInfo = new CellInfo();
Winson Chungaafa03c2010-06-11 17:34:16 -070075
Patrick Dubroyde7658b2010-09-27 11:15:43 -070076 // These are temporary variables to prevent having to allocate a new object just to
77 // return an (x, y) value from helper functions. Do NOT use them to maintain other state.
Patrick Dubroy6569f2c2010-07-12 14:25:18 -070078 private final int[] mTmpCellXY = new int[2];
Patrick Dubroyde7658b2010-09-27 11:15:43 -070079 private final int[] mTmpPoint = new int[2];
80 private final PointF mTmpPointF = new PointF();
Patrick Dubroy6569f2c2010-07-12 14:25:18 -070081
The Android Open Source Project31dd5032009-03-03 19:32:27 -080082 boolean[][] mOccupied;
83
Michael Jurkadee05892010-07-27 10:01:56 -070084 private OnTouchListener mInterceptTouchListener;
85
Michael Jurka5f1c5092010-09-03 14:15:02 -070086 private float mBackgroundAlpha;
Adam Cohen1b0aaac2010-10-28 11:11:18 -070087 private float mBackgroundAlphaMultiplier = 1.0f;
Adam Cohenf34bab52010-09-30 14:11:56 -070088
Michael Jurka33945b22010-12-21 18:19:38 -080089 private Drawable mNormalBackground;
90 private Drawable mNormalGlowBackground;
91 private Drawable mActiveBackground;
92 private Drawable mActiveGlowBackground;
93 private Drawable mNormalBackgroundMini;
94 private Drawable mNormalGlowBackgroundMini;
95 private Drawable mActiveBackgroundMini;
96 private Drawable mActiveGlowBackgroundMini;
Michael Jurka18014792010-10-14 09:01:34 -070097 private Rect mBackgroundRect;
Michael Jurka33945b22010-12-21 18:19:38 -080098 private Rect mGlowBackgroundRect;
99 private float mGlowBackgroundScale;
100 private float mGlowBackgroundAlpha;
Patrick Dubroy1262e362010-10-06 15:49:50 -0700101
Michael Jurkabea15192010-11-17 12:33:46 -0800102 private Bitmap mCache;
103 private Canvas mCacheCanvas;
104 private Rect mCacheRect;
105 private Paint mCachePaint;
106
107 private boolean mIsCacheEnabled = true;
108 private boolean mDisableCacheUpdates = false;
109 private boolean mForceCacheUpdate = false;
110 private boolean mIsCacheDirty = true;
111 private float mBitmapCacheScale;
112 private float mMaxScaleForUsingBitmapCache;
113
Michael Jurka33945b22010-12-21 18:19:38 -0800114 private boolean mAcceptsDrops = false;
115 // If we're actively dragging something over this screen, mIsDragOverlapping is true
116 private boolean mIsDragOverlapping = false;
117 private boolean mIsDragOccuring = false;
118 private boolean mIsDefaultDropTarget = false;
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700119 private final Point mDragCenter = new Point();
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700120
Winson Chung150fbab2010-09-29 17:14:26 -0700121 // These arrays are used to implement the drag visualization on x-large screens.
Joe Onorato4be866d2010-10-10 11:26:02 -0700122 // They are used as circular arrays, indexed by mDragOutlineCurrent.
123 private Point[] mDragOutlines = new Point[8];
Chet Haase472b2812010-10-14 07:02:04 -0700124 private float[] mDragOutlineAlphas = new float[mDragOutlines.length];
Joe Onorato4be866d2010-10-10 11:26:02 -0700125 private InterruptibleInOutAnimator[] mDragOutlineAnims =
126 new InterruptibleInOutAnimator[mDragOutlines.length];
Winson Chung150fbab2010-09-29 17:14:26 -0700127
128 // Used as an index into the above 3 arrays; indicates which is the most current value.
Joe Onorato4be866d2010-10-10 11:26:02 -0700129 private int mDragOutlineCurrent = 0;
Patrick Dubroy8e58e912010-10-14 13:21:48 -0700130 private final Paint mDragOutlinePaint = new Paint();
Winson Chung150fbab2010-09-29 17:14:26 -0700131
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700132 private Drawable mCrosshairsDrawable = null;
Patrick Dubroy49250ad2010-10-08 15:33:52 -0700133 private InterruptibleInOutAnimator mCrosshairsAnimator = null;
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700134 private float mCrosshairsVisibility = 0.0f;
135
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700136 // When a drag operation is in progress, holds the nearest cell to the touch point
137 private final int[] mDragCell = new int[2];
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800138
Winson Chungaafa03c2010-06-11 17:34:16 -0700139 private final WallpaperManager mWallpaperManager;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800140
Joe Onorato4be866d2010-10-10 11:26:02 -0700141 private boolean mDragging = false;
142
Patrick Dubroyce34a972010-10-19 10:34:32 -0700143 private TimeInterpolator mEaseOutInterpolator;
144
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800145 public CellLayout(Context context) {
146 this(context, null);
147 }
148
149 public CellLayout(Context context, AttributeSet attrs) {
150 this(context, attrs, 0);
151 }
152
153 public CellLayout(Context context, AttributeSet attrs, int defStyle) {
154 super(context, attrs, defStyle);
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700155
156 // A ViewGroup usually does not draw, but CellLayout needs to draw a rectangle to show
157 // the user where a dragged item will land when dropped.
158 setWillNotDraw(false);
Michael Jurkaa63c4522010-08-19 13:52:27 -0700159
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800160 TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.CellLayout, defStyle, 0);
161
162 mCellWidth = a.getDimensionPixelSize(R.styleable.CellLayout_cellWidth, 10);
163 mCellHeight = a.getDimensionPixelSize(R.styleable.CellLayout_cellHeight, 10);
Winson Chungece7f5b2010-10-22 14:54:12 -0700164 mWidthGap = a.getDimensionPixelSize(R.styleable.CellLayout_widthGap, -1);
165 mHeightGap = a.getDimensionPixelSize(R.styleable.CellLayout_heightGap, -1);
Winson Chungaafa03c2010-06-11 17:34:16 -0700166
Adam Cohend22015c2010-07-26 22:02:18 -0700167 mLeftPadding =
168 a.getDimensionPixelSize(R.styleable.CellLayout_xAxisStartPadding, 10);
169 mRightPadding =
170 a.getDimensionPixelSize(R.styleable.CellLayout_xAxisEndPadding, 10);
171 mTopPadding =
172 a.getDimensionPixelSize(R.styleable.CellLayout_yAxisStartPadding, 10);
173 mBottomPadding =
174 a.getDimensionPixelSize(R.styleable.CellLayout_yAxisEndPadding, 10);
Winson Chungaafa03c2010-06-11 17:34:16 -0700175
Adam Cohend22015c2010-07-26 22:02:18 -0700176 mCountX = LauncherModel.getCellCountX();
177 mCountY = LauncherModel.getCellCountY();
Michael Jurka0280c3b2010-09-17 15:00:07 -0700178 mOccupied = new boolean[mCountX][mCountY];
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800179
180 a.recycle();
181
182 setAlwaysDrawnWithCacheEnabled(false);
183
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700184 mWallpaperManager = WallpaperManager.getInstance(context);
185
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700186 final Resources res = getResources();
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700187
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700188 if (LauncherApplication.isScreenXLarge()) {
Michael Jurka33945b22010-12-21 18:19:38 -0800189 mNormalBackground = res.getDrawable(R.drawable.homescreen_large_blue);
190 mNormalGlowBackground = res.getDrawable(R.drawable.homescreen_large_blue_strong);
191 mActiveBackground = res.getDrawable(R.drawable.homescreen_large_green);
192 mActiveGlowBackground = res.getDrawable(R.drawable.homescreen_large_green_strong);
193
194 mNormalBackgroundMini = res.getDrawable(R.drawable.homescreen_small_blue);
195 mNormalGlowBackgroundMini = res.getDrawable(R.drawable.homescreen_small_blue_strong);
196 mActiveBackgroundMini = res.getDrawable(R.drawable.homescreen_small_green);
197 mActiveGlowBackgroundMini = res.getDrawable(R.drawable.homescreen_small_green_strong);
198
199 mNormalBackground.setFilterBitmap(true);
200 mNormalGlowBackground.setFilterBitmap(true);
201 mActiveBackground.setFilterBitmap(true);
202 mActiveGlowBackground.setFilterBitmap(true);
203 mNormalBackgroundMini.setFilterBitmap(true);
204 mNormalGlowBackgroundMini.setFilterBitmap(true);
205 mActiveBackgroundMini.setFilterBitmap(true);
206 mActiveGlowBackgroundMini.setFilterBitmap(true);
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700207 }
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700208
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700209 // Initialize the data structures used for the drag visualization.
Winson Chung150fbab2010-09-29 17:14:26 -0700210
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700211 mCrosshairsDrawable = res.getDrawable(R.drawable.gardening_crosshairs);
Patrick Dubroyce34a972010-10-19 10:34:32 -0700212 mEaseOutInterpolator = new DecelerateInterpolator(2.5f); // Quint ease out
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700213
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700214 // Set up the animation for fading the crosshairs in and out
215 int animDuration = res.getInteger(R.integer.config_crosshairsFadeInTime);
Patrick Dubroy49250ad2010-10-08 15:33:52 -0700216 mCrosshairsAnimator = new InterruptibleInOutAnimator(animDuration, 0.0f, 1.0f);
Chet Haase472b2812010-10-14 07:02:04 -0700217 mCrosshairsAnimator.getAnimator().addUpdateListener(new AnimatorUpdateListener() {
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700218 public void onAnimationUpdate(ValueAnimator animation) {
219 mCrosshairsVisibility = ((Float) animation.getAnimatedValue()).floatValue();
Patrick Dubroy8e58e912010-10-14 13:21:48 -0700220 invalidate();
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700221 }
222 });
Patrick Dubroyce34a972010-10-19 10:34:32 -0700223 mCrosshairsAnimator.getAnimator().setInterpolator(mEaseOutInterpolator);
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700224
Joe Onorato4be866d2010-10-10 11:26:02 -0700225 for (int i = 0; i < mDragOutlines.length; i++) {
226 mDragOutlines[i] = new Point(-1, -1);
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700227 }
228
229 // When dragging things around the home screens, we show a green outline of
230 // where the item will land. The outlines gradually fade out, leaving a trail
231 // behind the drag path.
232 // Set up all the animations that are used to implement this fading.
233 final int duration = res.getInteger(R.integer.config_dragOutlineFadeTime);
Chet Haase472b2812010-10-14 07:02:04 -0700234 final float fromAlphaValue = 0;
235 final float toAlphaValue = (float)res.getInteger(R.integer.config_dragOutlineMaxAlpha);
Joe Onorato4be866d2010-10-10 11:26:02 -0700236
Patrick Dubroy8e58e912010-10-14 13:21:48 -0700237 Arrays.fill(mDragOutlineAlphas, fromAlphaValue);
Joe Onorato4be866d2010-10-10 11:26:02 -0700238
239 for (int i = 0; i < mDragOutlineAnims.length; i++) {
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700240 final InterruptibleInOutAnimator anim =
241 new InterruptibleInOutAnimator(duration, fromAlphaValue, toAlphaValue);
Patrick Dubroyce34a972010-10-19 10:34:32 -0700242 anim.getAnimator().setInterpolator(mEaseOutInterpolator);
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700243 final int thisIndex = i;
Chet Haase472b2812010-10-14 07:02:04 -0700244 anim.getAnimator().addUpdateListener(new AnimatorUpdateListener() {
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700245 public void onAnimationUpdate(ValueAnimator animation) {
Joe Onorato4be866d2010-10-10 11:26:02 -0700246 final Bitmap outline = (Bitmap)anim.getTag();
247
248 // If an animation is started and then stopped very quickly, we can still
249 // get spurious updates we've cleared the tag. Guard against this.
250 if (outline == null) {
Patrick Dubroyfe6bd872010-10-13 17:32:10 -0700251 if (false) {
252 Object val = animation.getAnimatedValue();
253 Log.d(TAG, "anim " + thisIndex + " update: " + val +
254 ", isStopped " + anim.isStopped());
255 }
Joe Onorato4be866d2010-10-10 11:26:02 -0700256 // Try to prevent it from continuing to run
257 animation.cancel();
258 } else {
Chet Haase472b2812010-10-14 07:02:04 -0700259 mDragOutlineAlphas[thisIndex] = (Float) animation.getAnimatedValue();
Joe Onorato4be866d2010-10-10 11:26:02 -0700260 final int left = mDragOutlines[thisIndex].x;
261 final int top = mDragOutlines[thisIndex].y;
262 CellLayout.this.invalidate(left, top,
263 left + outline.getWidth(), top + outline.getHeight());
264 }
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700265 }
266 });
Joe Onorato4be866d2010-10-10 11:26:02 -0700267 // The animation holds a reference to the drag outline bitmap as long is it's
268 // running. This way the bitmap can be GCed when the animations are complete.
Chet Haase472b2812010-10-14 07:02:04 -0700269 anim.getAnimator().addListener(new AnimatorListenerAdapter() {
Michael Jurka3c4c20f2010-10-28 15:36:06 -0700270 @Override
Joe Onorato4be866d2010-10-10 11:26:02 -0700271 public void onAnimationEnd(Animator animation) {
Chet Haase472b2812010-10-14 07:02:04 -0700272 if ((Float) ((ValueAnimator) animation).getAnimatedValue() == 0f) {
Joe Onorato4be866d2010-10-10 11:26:02 -0700273 anim.setTag(null);
274 }
275 }
276 });
277 mDragOutlineAnims[i] = anim;
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700278 }
Patrick Dubroyce34a972010-10-19 10:34:32 -0700279
Michael Jurka18014792010-10-14 09:01:34 -0700280 mBackgroundRect = new Rect();
Michael Jurka33945b22010-12-21 18:19:38 -0800281 mGlowBackgroundRect = new Rect();
Michael Jurkabea15192010-11-17 12:33:46 -0800282 mCacheRect = new Rect();
Michael Jurka18014792010-10-14 09:01:34 -0700283 setHoverScale(1.0f);
284 setHoverAlpha(1.0f);
Michael Jurkabea15192010-11-17 12:33:46 -0800285
286 mBitmapCacheScale =
287 res.getInteger(R.integer.config_workspaceScreenBitmapCacheScale) / 100.0f;
288 mMaxScaleForUsingBitmapCache =
289 res.getInteger(R.integer.config_maxScaleForUsingWorkspaceScreenBitmapCache) / 100.0f;
290 mCacheCanvas = new Canvas();
Michael Jurka18014792010-10-14 09:01:34 -0700291 }
292
Michael Jurka33945b22010-12-21 18:19:38 -0800293 public void setIsDefaultDropTarget(boolean isDefaultDropTarget) {
294 if (mIsDefaultDropTarget != isDefaultDropTarget) {
295 mIsDefaultDropTarget = isDefaultDropTarget;
296 invalidate();
297 }
298 }
299
Michael Jurka33945b22010-12-21 18:19:38 -0800300 void setIsDragOccuring(boolean isDragOccuring) {
301 if (mIsDragOccuring != isDragOccuring) {
302 mIsDragOccuring = isDragOccuring;
303 invalidate();
304 }
305 }
306
307 void setIsDragOverlapping(boolean isDragOverlapping) {
308 if (mIsDragOverlapping != isDragOverlapping) {
309 mIsDragOverlapping = isDragOverlapping;
310 invalidate();
311 }
312 }
313
314 boolean getIsDragOverlapping() {
315 return mIsDragOverlapping;
316 }
317
318 private void updateGlowRect() {
319 float marginFraction = (mGlowBackgroundScale - 1.0f) / 2.0f;
Michael Jurka18014792010-10-14 09:01:34 -0700320 int marginX = (int) (marginFraction * (mBackgroundRect.right - mBackgroundRect.left));
321 int marginY = (int) (marginFraction * (mBackgroundRect.bottom - mBackgroundRect.top));
Michael Jurka33945b22010-12-21 18:19:38 -0800322 mGlowBackgroundRect.set(mBackgroundRect.left - marginX, mBackgroundRect.top - marginY,
Michael Jurka18014792010-10-14 09:01:34 -0700323 mBackgroundRect.right + marginX, mBackgroundRect.bottom + marginY);
324 invalidate();
325 }
326
327 public void setHoverScale(float scaleFactor) {
Michael Jurka33945b22010-12-21 18:19:38 -0800328 if (scaleFactor != mGlowBackgroundScale) {
329 mGlowBackgroundScale = scaleFactor;
330 updateGlowRect();
Michael Jurka18014792010-10-14 09:01:34 -0700331 }
332 }
333
334 public float getHoverScale() {
Michael Jurka33945b22010-12-21 18:19:38 -0800335 return mGlowBackgroundScale;
Michael Jurka18014792010-10-14 09:01:34 -0700336 }
337
338 public float getHoverAlpha() {
Michael Jurka33945b22010-12-21 18:19:38 -0800339 return mGlowBackgroundAlpha;
Michael Jurka18014792010-10-14 09:01:34 -0700340 }
341
342 public void setHoverAlpha(float alpha) {
Michael Jurka33945b22010-12-21 18:19:38 -0800343 mGlowBackgroundAlpha = alpha;
Michael Jurka18014792010-10-14 09:01:34 -0700344 invalidate();
345 }
346
347 void animateDrop() {
348 if (LauncherApplication.isScreenXLarge()) {
349 Resources res = getResources();
350 float onDropScale = res.getInteger(R.integer.config_screenOnDropScalePercent) / 100.0f;
351 ObjectAnimator scaleUp = ObjectAnimator.ofFloat(this, "hoverScale", onDropScale);
352 scaleUp.setDuration(res.getInteger(R.integer.config_screenOnDropScaleUpDuration));
353 ObjectAnimator scaleDown = ObjectAnimator.ofFloat(this, "hoverScale", 1.0f);
354 scaleDown.setDuration(res.getInteger(R.integer.config_screenOnDropScaleDownDuration));
355 ObjectAnimator alphaFadeOut = ObjectAnimator.ofFloat(this, "hoverAlpha", 0.0f);
356
357 alphaFadeOut.setStartDelay(res.getInteger(R.integer.config_screenOnDropAlphaFadeDelay));
358 alphaFadeOut.setDuration(res.getInteger(R.integer.config_screenOnDropAlphaFadeDelay));
359
360 AnimatorSet bouncer = new AnimatorSet();
361 bouncer.play(scaleUp).before(scaleDown);
362 bouncer.play(scaleUp).with(alphaFadeOut);
Michael Jurka8edd75c2010-12-17 20:15:06 -0800363 bouncer.addListener(new AnimatorListenerAdapter() {
Michael Jurka3c4c20f2010-10-28 15:36:06 -0700364 @Override
Michael Jurka18014792010-10-14 09:01:34 -0700365 public void onAnimationStart(Animator animation) {
Michael Jurka33945b22010-12-21 18:19:38 -0800366 setIsDragOverlapping(true);
Michael Jurka18014792010-10-14 09:01:34 -0700367 }
Michael Jurka3c4c20f2010-10-28 15:36:06 -0700368 @Override
Michael Jurka8edd75c2010-12-17 20:15:06 -0800369 public void onAnimationEnd(Animator animation) {
Michael Jurka33945b22010-12-21 18:19:38 -0800370 setIsDragOverlapping(false);
Michael Jurka18014792010-10-14 09:01:34 -0700371 setHoverScale(1.0f);
372 setHoverAlpha(1.0f);
373 }
374 });
375 bouncer.start();
376 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800377 }
378
Patrick Dubroy1262e362010-10-06 15:49:50 -0700379 public void drawChildren(Canvas canvas) {
380 super.dispatchDraw(canvas);
381 }
382
Michael Jurkabea15192010-11-17 12:33:46 -0800383 private void invalidateIfNeeded() {
384 if (mIsCacheDirty) {
385 // Force a redraw to update the cache if it's dirty
386 invalidate();
387 }
388 }
389
390 public void enableCache() {
391 mIsCacheEnabled = true;
392 invalidateIfNeeded();
393 }
394
395 public void disableCache() {
396 mIsCacheEnabled = false;
397 }
398
399 public void disableCacheUpdates() {
400 mDisableCacheUpdates = true;
401 // Force just one update before we enter a period of no cache updates
402 mForceCacheUpdate = true;
403 }
404
405 public void enableCacheUpdates() {
406 mDisableCacheUpdates = false;
407 invalidateIfNeeded();
408 }
409
410 private void invalidateCache() {
411 mIsCacheDirty = true;
412 invalidateIfNeeded();
413 }
414
415 public void updateCache() {
416 mCacheCanvas.drawColor(0x00000000, Mode.CLEAR);
417
418 float alpha = getAlpha();
419 setAlpha(1.0f);
420 drawChildren(mCacheCanvas);
421 setAlpha(alpha);
422
423 mIsCacheDirty = false;
424 }
425
426 public void dispatchDraw(Canvas canvas) {
427 final int count = getChildCount();
428
429 if (!mIsCacheDirty) {
430 // Check if one of the children (an icon or widget) is dirty
431 for (int i = 0; i < count; i++) {
432 final View child = getChildAt(i);
433 if (child.isDirty()) {
434 mIsCacheDirty = true;
435 break;
436 }
437 }
438 }
439
Michael Jurka026f5552011-01-07 12:58:57 -0800440 boolean useBitmapCache = mIsCacheEnabled && getScaleX() < mMaxScaleForUsingBitmapCache;
Michael Jurkabea15192010-11-17 12:33:46 -0800441 if (mForceCacheUpdate ||
Michael Jurka026f5552011-01-07 12:58:57 -0800442 (useBitmapCache && !mDisableCacheUpdates)) {
Michael Jurkabea15192010-11-17 12:33:46 -0800443 // Sometimes we force a cache update-- this is used to make sure the cache will look as
444 // up-to-date as possible right when we disable cache updates
445 if (mIsCacheDirty) {
446 updateCache();
447 }
448 mForceCacheUpdate = false;
449 }
450
Michael Jurka026f5552011-01-07 12:58:57 -0800451 if (useBitmapCache) {
Michael Jurkabea15192010-11-17 12:33:46 -0800452 mCachePaint.setAlpha((int)(255*getAlpha()));
453 canvas.drawBitmap(mCache, mCacheRect, mBackgroundRect, mCachePaint);
454 } else {
455 super.dispatchDraw(canvas);
456 }
457 }
458
459 private void prepareCacheBitmap() {
460 if (mCache == null) {
461 mCache = Bitmap.createBitmap((int) (getWidth() * mBitmapCacheScale),
462 (int) (getHeight() * mBitmapCacheScale), Config.ARGB_8888);
463
464 mCachePaint = new Paint();
465 mCachePaint.setFilterBitmap(true);
466 mCacheCanvas.setBitmap(mCache);
467 mCacheCanvas.scale(mBitmapCacheScale, mBitmapCacheScale);
468 }
469 }
470
Jeff Sharkey83f111d2009-04-20 21:03:13 -0700471 @Override
Patrick Dubroy1262e362010-10-06 15:49:50 -0700472 protected void onDraw(Canvas canvas) {
Michael Jurka3e7c7632010-10-02 16:01:03 -0700473 // When we're large, we are either drawn in a "hover" state (ie when dragging an item to
474 // a neighboring page) or with just a normal background (if backgroundAlpha > 0.0f)
475 // When we're small, we are either drawn normally or in the "accepts drops" state (during
476 // a drag). However, we also drag the mini hover background *over* one of those two
477 // backgrounds
Winson Chung26cbf3a2011-01-06 16:25:55 -0800478 if (LauncherApplication.isScreenXLarge() && mBackgroundAlpha > 0.0f) {
Adam Cohenf34bab52010-09-30 14:11:56 -0700479 Drawable bg;
Michael Jurka33945b22010-12-21 18:19:38 -0800480 boolean mini = getScaleX() < 0.5f;
481
482 if (mIsDragOverlapping) {
483 // In the mini case, we draw the active_glow bg *over* the active background
484 bg = mini ? mActiveBackgroundMini : mActiveGlowBackground;
485 } else if (mIsDragOccuring && mAcceptsDrops) {
486 bg = mini ? mActiveBackgroundMini : mActiveBackground;
487 } else if (mIsDefaultDropTarget) {
488 bg = mini ? mNormalGlowBackgroundMini : mNormalGlowBackground;
Adam Cohenf34bab52010-09-30 14:11:56 -0700489 } else {
Michael Jurka33945b22010-12-21 18:19:38 -0800490 bg = mini ? mNormalBackgroundMini : mNormalBackground;
Adam Cohenf34bab52010-09-30 14:11:56 -0700491 }
Michael Jurka33945b22010-12-21 18:19:38 -0800492
493 bg.setAlpha((int) (mBackgroundAlpha * mBackgroundAlphaMultiplier * 255));
494 bg.setBounds(mBackgroundRect);
495 bg.draw(canvas);
496
497 if (mini && mIsDragOverlapping) {
Michael Jurka18014792010-10-14 09:01:34 -0700498 boolean modifiedClipRect = false;
Michael Jurka33945b22010-12-21 18:19:38 -0800499 if (mGlowBackgroundScale > 1.0f) {
Michael Jurka18014792010-10-14 09:01:34 -0700500 // If the hover background's scale is greater than 1, we'll be drawing outside
501 // the bounds of this CellLayout. Get around that by temporarily increasing the
502 // size of the clip rect
Michael Jurka33945b22010-12-21 18:19:38 -0800503 float marginFraction = (mGlowBackgroundScale - 1.0f) / 2.0f;
Michael Jurka18014792010-10-14 09:01:34 -0700504 Rect clipRect = canvas.getClipBounds();
505 int marginX = (int) (marginFraction * (clipRect.right - clipRect.left));
506 int marginY = (int) (marginFraction * (clipRect.bottom - clipRect.top));
507 canvas.save(Canvas.CLIP_SAVE_FLAG);
508 canvas.clipRect(-marginX, -marginY,
509 getWidth() + marginX, getHeight() + marginY, Region.Op.REPLACE);
510 modifiedClipRect = true;
511 }
512
Michael Jurka33945b22010-12-21 18:19:38 -0800513 mActiveGlowBackgroundMini.setAlpha(
514 (int) (mBackgroundAlpha * mGlowBackgroundAlpha * 255));
515 mActiveGlowBackgroundMini.setBounds(mGlowBackgroundRect);
516 mActiveGlowBackgroundMini.draw(canvas);
Michael Jurka18014792010-10-14 09:01:34 -0700517 if (modifiedClipRect) {
518 canvas.restore();
519 }
Michael Jurka3e7c7632010-10-02 16:01:03 -0700520 }
Michael Jurkaa63c4522010-08-19 13:52:27 -0700521 }
Romain Guya6abce82009-11-10 02:54:41 -0800522
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700523 if (mCrosshairsVisibility > 0.0f) {
524 final int countX = mCountX;
525 final int countY = mCountY;
526
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700527 final float MAX_ALPHA = 0.4f;
528 final int MAX_VISIBLE_DISTANCE = 600;
529 final float DISTANCE_MULTIPLIER = 0.002f;
530
531 final Drawable d = mCrosshairsDrawable;
532 final int width = d.getIntrinsicWidth();
533 final int height = d.getIntrinsicHeight();
534
535 int x = getLeftPadding() - (mWidthGap / 2) - (width / 2);
536 for (int col = 0; col <= countX; col++) {
537 int y = getTopPadding() - (mHeightGap / 2) - (height / 2);
538 for (int row = 0; row <= countY; row++) {
539 mTmpPointF.set(x - mDragCenter.x, y - mDragCenter.y);
540 float dist = mTmpPointF.length();
541 // Crosshairs further from the drag point are more faint
542 float alpha = Math.min(MAX_ALPHA,
543 DISTANCE_MULTIPLIER * (MAX_VISIBLE_DISTANCE - dist));
544 if (alpha > 0.0f) {
545 d.setBounds(x, y, x + width, y + height);
546 d.setAlpha((int) (alpha * 255 * mCrosshairsVisibility));
547 d.draw(canvas);
548 }
549 y += mCellHeight + mHeightGap;
550 }
551 x += mCellWidth + mWidthGap;
552 }
Joe Onorato4be866d2010-10-10 11:26:02 -0700553 }
Winson Chung150fbab2010-09-29 17:14:26 -0700554
Patrick Dubroy8e58e912010-10-14 13:21:48 -0700555 final Paint paint = mDragOutlinePaint;
Joe Onorato4be866d2010-10-10 11:26:02 -0700556 for (int i = 0; i < mDragOutlines.length; i++) {
Chet Haase472b2812010-10-14 07:02:04 -0700557 final float alpha = mDragOutlineAlphas[i];
Joe Onorato4be866d2010-10-10 11:26:02 -0700558 if (alpha > 0) {
559 final Point p = mDragOutlines[i];
560 final Bitmap b = (Bitmap) mDragOutlineAnims[i].getTag();
Chet Haase472b2812010-10-14 07:02:04 -0700561 paint.setAlpha((int)(alpha + .5f));
Joe Onorato4be866d2010-10-10 11:26:02 -0700562 canvas.drawBitmap(b, p.x, p.y, paint);
Winson Chung150fbab2010-09-29 17:14:26 -0700563 }
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700564 }
565 }
566
Adam Cohenf34bab52010-09-30 14:11:56 -0700567 public void setDimmableProgress(float progress) {
568 for (int i = 0; i < getChildCount(); i++) {
569 Dimmable d = (Dimmable) getChildAt(i);
570 d.setDimmableProgress(progress);
571 }
572 }
573
574 public float getDimmableProgress() {
575 if (getChildCount() > 0) {
576 return ((Dimmable) getChildAt(0)).getDimmableProgress();
577 }
578 return 0.0f;
579 }
580
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700581 @Override
Jeff Sharkey83f111d2009-04-20 21:03:13 -0700582 public void cancelLongPress() {
583 super.cancelLongPress();
584
585 // Cancel long press for all children
586 final int count = getChildCount();
587 for (int i = 0; i < count; i++) {
588 final View child = getChildAt(i);
589 child.cancelLongPress();
590 }
591 }
592
Michael Jurkadee05892010-07-27 10:01:56 -0700593 public void setOnInterceptTouchListener(View.OnTouchListener listener) {
594 mInterceptTouchListener = listener;
595 }
596
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800597 int getCountX() {
Adam Cohend22015c2010-07-26 22:02:18 -0700598 return mCountX;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800599 }
600
601 int getCountY() {
Adam Cohend22015c2010-07-26 22:02:18 -0700602 return mCountY;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800603 }
604
Winson Chungaafa03c2010-06-11 17:34:16 -0700605 public boolean addViewToCellLayout(View child, int index, int childId, LayoutParams params) {
Michael Jurkaf3ca3ab2010-10-20 17:08:24 -0700606 return addViewToCellLayout(child, index, childId, params, true);
607 }
608
609 public boolean addViewToCellLayout(
610 View child, int index, int childId, LayoutParams params, boolean markCells) {
Winson Chungaafa03c2010-06-11 17:34:16 -0700611 final LayoutParams lp = params;
612
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800613 // Generate an id for each view, this assumes we have at most 256x256 cells
614 // per workspace screen
Adam Cohend22015c2010-07-26 22:02:18 -0700615 if (lp.cellX >= 0 && lp.cellX <= mCountX - 1 && lp.cellY >= 0 && lp.cellY <= mCountY - 1) {
Winson Chungaafa03c2010-06-11 17:34:16 -0700616 // If the horizontal or vertical span is set to -1, it is taken to
617 // mean that it spans the extent of the CellLayout
Adam Cohend22015c2010-07-26 22:02:18 -0700618 if (lp.cellHSpan < 0) lp.cellHSpan = mCountX;
619 if (lp.cellVSpan < 0) lp.cellVSpan = mCountY;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800620
Winson Chungaafa03c2010-06-11 17:34:16 -0700621 child.setId(childId);
622
Michael Jurkadee05892010-07-27 10:01:56 -0700623 // We might be in the middle or end of shrinking/fading to a dimmed view
624 // Make sure this view's alpha is set the same as all the rest of the views
Michael Jurka5f1c5092010-09-03 14:15:02 -0700625 child.setAlpha(getAlpha());
Winson Chungaafa03c2010-06-11 17:34:16 -0700626 addView(child, index, lp);
Michael Jurkadee05892010-07-27 10:01:56 -0700627
Michael Jurkabea15192010-11-17 12:33:46 -0800628 // invalidate the cache to have it reflect the new item
629 invalidateCache();
Michael Jurkaf3ca3ab2010-10-20 17:08:24 -0700630 if (markCells) markCellsAsOccupiedForView(child);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700631
Winson Chungaafa03c2010-06-11 17:34:16 -0700632 return true;
633 }
634 return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800635 }
Michael Jurka3e7c7632010-10-02 16:01:03 -0700636
Michael Jurkabea15192010-11-17 12:33:46 -0800637 public void setAcceptsDrops(boolean acceptsDrops) {
638 if (mAcceptsDrops != acceptsDrops) {
639 mAcceptsDrops = acceptsDrops;
640 invalidate();
641 }
642 }
643
Michael Jurka3e7c7632010-10-02 16:01:03 -0700644 public boolean getAcceptsDrops() {
645 return mAcceptsDrops;
646 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800647
648 @Override
Michael Jurka0280c3b2010-09-17 15:00:07 -0700649 public void removeAllViews() {
Joe Onorato7cef2892011-01-07 10:08:48 -0800650 super.removeAllViews();
Michael Jurka0280c3b2010-09-17 15:00:07 -0700651 clearOccupiedCells();
652 }
653
654 @Override
655 public void removeAllViewsInLayout() {
Joe Onorato7cef2892011-01-07 10:08:48 -0800656 super.removeAllViewsInLayout();
Michael Jurka0280c3b2010-09-17 15:00:07 -0700657 clearOccupiedCells();
658 }
659
Michael Jurkaf3ca3ab2010-10-20 17:08:24 -0700660 public void removeViewWithoutMarkingCells(View view) {
661 super.removeView(view);
662 }
663
Michael Jurka0280c3b2010-09-17 15:00:07 -0700664 @Override
665 public void removeView(View view) {
666 markCellsAsUnoccupiedForView(view);
667 super.removeView(view);
668 }
669
670 @Override
671 public void removeViewAt(int index) {
672 markCellsAsUnoccupiedForView(getChildAt(index));
673 super.removeViewAt(index);
674 }
675
676 @Override
677 public void removeViewInLayout(View view) {
678 markCellsAsUnoccupiedForView(view);
679 super.removeViewInLayout(view);
680 }
681
682 @Override
683 public void removeViews(int start, int count) {
684 for (int i = start; i < start + count; i++) {
685 markCellsAsUnoccupiedForView(getChildAt(i));
686 }
687 super.removeViews(start, count);
688 }
689
690 @Override
691 public void removeViewsInLayout(int start, int count) {
692 for (int i = start; i < start + count; i++) {
693 markCellsAsUnoccupiedForView(getChildAt(i));
694 }
695 super.removeViewsInLayout(start, count);
696 }
697
698 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800699 public void requestChildFocus(View child, View focused) {
700 super.requestChildFocus(child, focused);
701 if (child != null) {
702 Rect r = new Rect();
703 child.getDrawingRect(r);
704 requestRectangleOnScreen(r);
705 }
706 }
707
708 @Override
709 protected void onAttachedToWindow() {
710 super.onAttachedToWindow();
711 mCellInfo.screen = ((ViewGroup) getParent()).indexOfChild(this);
712 }
713
Michael Jurkaaf442092010-06-10 17:01:57 -0700714 public void setTagToCellInfoForPoint(int touchX, int touchY) {
715 final CellInfo cellInfo = mCellInfo;
716 final Rect frame = mRect;
717 final int x = touchX + mScrollX;
718 final int y = touchY + mScrollY;
719 final int count = getChildCount();
720
721 boolean found = false;
722 for (int i = count - 1; i >= 0; i--) {
723 final View child = getChildAt(i);
724
725 if ((child.getVisibility()) == VISIBLE || child.getAnimation() != null) {
726 child.getHitRect(frame);
727 if (frame.contains(x, y)) {
728 final LayoutParams lp = (LayoutParams) child.getLayoutParams();
729 cellInfo.cell = child;
730 cellInfo.cellX = lp.cellX;
731 cellInfo.cellY = lp.cellY;
732 cellInfo.spanX = lp.cellHSpan;
733 cellInfo.spanY = lp.cellVSpan;
734 cellInfo.valid = true;
735 found = true;
Michael Jurkaaf442092010-06-10 17:01:57 -0700736 break;
737 }
738 }
739 }
Winson Chungaafa03c2010-06-11 17:34:16 -0700740
Michael Jurkaaf442092010-06-10 17:01:57 -0700741 if (!found) {
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700742 final int cellXY[] = mTmpCellXY;
Michael Jurkaaf442092010-06-10 17:01:57 -0700743 pointToCellExact(x, y, cellXY);
744
Michael Jurkaaf442092010-06-10 17:01:57 -0700745 cellInfo.cell = null;
746 cellInfo.cellX = cellXY[0];
747 cellInfo.cellY = cellXY[1];
748 cellInfo.spanX = 1;
749 cellInfo.spanY = 1;
Michael Jurka0280c3b2010-09-17 15:00:07 -0700750 cellInfo.valid = cellXY[0] >= 0 && cellXY[1] >= 0 && cellXY[0] < mCountX &&
751 cellXY[1] < mCountY && !mOccupied[cellXY[0]][cellXY[1]];
Michael Jurkaaf442092010-06-10 17:01:57 -0700752 }
753 setTag(cellInfo);
754 }
755
Winson Chungaafa03c2010-06-11 17:34:16 -0700756
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800757 @Override
758 public boolean onInterceptTouchEvent(MotionEvent ev) {
Michael Jurkadee05892010-07-27 10:01:56 -0700759 if (mInterceptTouchListener != null && mInterceptTouchListener.onTouch(this, ev)) {
760 return true;
761 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800762 final int action = ev.getAction();
763 final CellInfo cellInfo = mCellInfo;
764
765 if (action == MotionEvent.ACTION_DOWN) {
Michael Jurkaaf442092010-06-10 17:01:57 -0700766 setTagToCellInfoForPoint((int) ev.getX(), (int) ev.getY());
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800767 } else if (action == MotionEvent.ACTION_UP) {
768 cellInfo.cell = null;
769 cellInfo.cellX = -1;
770 cellInfo.cellY = -1;
771 cellInfo.spanX = 0;
772 cellInfo.spanY = 0;
773 cellInfo.valid = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800774 setTag(cellInfo);
775 }
776
777 return false;
778 }
779
780 @Override
781 public CellInfo getTag() {
Michael Jurka0280c3b2010-09-17 15:00:07 -0700782 return (CellInfo) super.getTag();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800783 }
784
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700785 /**
786 * Check if the row 'y' is empty from columns 'left' to 'right', inclusive.
787 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800788 private static boolean isRowEmpty(int y, int left, int right, boolean[][] occupied) {
789 for (int x = left; x <= right; x++) {
790 if (occupied[x][y]) {
791 return false;
792 }
793 }
794 return true;
795 }
796
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800797 /**
Winson Chungaafa03c2010-06-11 17:34:16 -0700798 * Given a point, return the cell that strictly encloses that point
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800799 * @param x X coordinate of the point
800 * @param y Y coordinate of the point
801 * @param result Array of 2 ints to hold the x and y coordinate of the cell
802 */
803 void pointToCellExact(int x, int y, int[] result) {
Winson Chungaafa03c2010-06-11 17:34:16 -0700804 final int hStartPadding = getLeftPadding();
805 final int vStartPadding = getTopPadding();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800806
807 result[0] = (x - hStartPadding) / (mCellWidth + mWidthGap);
808 result[1] = (y - vStartPadding) / (mCellHeight + mHeightGap);
809
Adam Cohend22015c2010-07-26 22:02:18 -0700810 final int xAxis = mCountX;
811 final int yAxis = mCountY;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800812
813 if (result[0] < 0) result[0] = 0;
814 if (result[0] >= xAxis) result[0] = xAxis - 1;
815 if (result[1] < 0) result[1] = 0;
816 if (result[1] >= yAxis) result[1] = yAxis - 1;
817 }
Winson Chungaafa03c2010-06-11 17:34:16 -0700818
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800819 /**
820 * Given a point, return the cell that most closely encloses that point
821 * @param x X coordinate of the point
822 * @param y Y coordinate of the point
823 * @param result Array of 2 ints to hold the x and y coordinate of the cell
824 */
825 void pointToCellRounded(int x, int y, int[] result) {
826 pointToCellExact(x + (mCellWidth / 2), y + (mCellHeight / 2), result);
827 }
828
829 /**
830 * Given a cell coordinate, return the point that represents the upper left corner of that cell
Winson Chungaafa03c2010-06-11 17:34:16 -0700831 *
832 * @param cellX X coordinate of the cell
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800833 * @param cellY Y coordinate of the cell
Winson Chungaafa03c2010-06-11 17:34:16 -0700834 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800835 * @param result Array of 2 ints to hold the x and y coordinate of the point
836 */
837 void cellToPoint(int cellX, int cellY, int[] result) {
Winson Chungaafa03c2010-06-11 17:34:16 -0700838 final int hStartPadding = getLeftPadding();
839 final int vStartPadding = getTopPadding();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800840
841 result[0] = hStartPadding + cellX * (mCellWidth + mWidthGap);
842 result[1] = vStartPadding + cellY * (mCellHeight + mHeightGap);
843 }
844
Romain Guy84f296c2009-11-04 15:00:44 -0800845 int getCellWidth() {
846 return mCellWidth;
847 }
848
849 int getCellHeight() {
850 return mCellHeight;
851 }
852
Romain Guy1a304a12009-11-10 00:02:32 -0800853 int getLeftPadding() {
Winson Chungaafa03c2010-06-11 17:34:16 -0700854 return mLeftPadding;
Romain Guy1a304a12009-11-10 00:02:32 -0800855 }
856
857 int getTopPadding() {
Winson Chungaafa03c2010-06-11 17:34:16 -0700858 return mTopPadding;
Romain Guy1a304a12009-11-10 00:02:32 -0800859 }
860
861 int getRightPadding() {
Winson Chungaafa03c2010-06-11 17:34:16 -0700862 return mRightPadding;
Romain Guy1a304a12009-11-10 00:02:32 -0800863 }
864
865 int getBottomPadding() {
Winson Chungaafa03c2010-06-11 17:34:16 -0700866 return mBottomPadding;
Romain Guy1a304a12009-11-10 00:02:32 -0800867 }
868
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800869 @Override
870 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
871 // TODO: currently ignoring padding
Winson Chungaafa03c2010-06-11 17:34:16 -0700872
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800873 int widthSpecMode = MeasureSpec.getMode(widthMeasureSpec);
Winson Chungaafa03c2010-06-11 17:34:16 -0700874 int widthSpecSize = MeasureSpec.getSize(widthMeasureSpec);
875
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800876 int heightSpecMode = MeasureSpec.getMode(heightMeasureSpec);
877 int heightSpecSize = MeasureSpec.getSize(heightMeasureSpec);
Winson Chungaafa03c2010-06-11 17:34:16 -0700878
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800879 if (widthSpecMode == MeasureSpec.UNSPECIFIED || heightSpecMode == MeasureSpec.UNSPECIFIED) {
880 throw new RuntimeException("CellLayout cannot have UNSPECIFIED dimensions");
881 }
882
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800883 final int cellWidth = mCellWidth;
884 final int cellHeight = mCellHeight;
885
Adam Cohend22015c2010-07-26 22:02:18 -0700886 int numWidthGaps = mCountX - 1;
887 int numHeightGaps = mCountY - 1;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800888
Winson Chungece7f5b2010-10-22 14:54:12 -0700889 if (mWidthGap < 0 || mHeightGap < 0) {
890 int vSpaceLeft = heightSpecSize - mTopPadding - mBottomPadding - (cellHeight * mCountY);
891 mHeightGap = vSpaceLeft / numHeightGaps;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800892
Winson Chungece7f5b2010-10-22 14:54:12 -0700893 int hSpaceLeft = widthSpecSize - mLeftPadding - mRightPadding - (cellWidth * mCountX);
894 mWidthGap = hSpaceLeft / numWidthGaps;
Winson Chungaafa03c2010-06-11 17:34:16 -0700895
Winson Chungece7f5b2010-10-22 14:54:12 -0700896 // center it around the min gaps
897 int minGap = Math.min(mWidthGap, mHeightGap);
898 mWidthGap = mHeightGap = minGap;
899 }
Michael Jurka5f1c5092010-09-03 14:15:02 -0700900
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800901 int count = getChildCount();
902
903 for (int i = 0; i < count; i++) {
904 View child = getChildAt(i);
905 LayoutParams lp = (LayoutParams) child.getLayoutParams();
Winson Chungaafa03c2010-06-11 17:34:16 -0700906 lp.setup(cellWidth, cellHeight, mWidthGap, mHeightGap,
907 mLeftPadding, mTopPadding);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800908
Michael Jurka0280c3b2010-09-17 15:00:07 -0700909 int childWidthMeasureSpec = MeasureSpec.makeMeasureSpec(lp.width, MeasureSpec.EXACTLY);
Winson Chungaafa03c2010-06-11 17:34:16 -0700910 int childheightMeasureSpec = MeasureSpec.makeMeasureSpec(lp.height,
911 MeasureSpec.EXACTLY);
912
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800913 child.measure(childWidthMeasureSpec, childheightMeasureSpec);
914 }
Michael Jurka5f1c5092010-09-03 14:15:02 -0700915 if (widthSpecMode == MeasureSpec.AT_MOST) {
916 int newWidth = mLeftPadding + mRightPadding + (mCountX * cellWidth) +
Winson Chungece7f5b2010-10-22 14:54:12 -0700917 ((mCountX - 1) * mWidthGap);
Michael Jurka5f1c5092010-09-03 14:15:02 -0700918 int newHeight = mTopPadding + mBottomPadding + (mCountY * cellHeight) +
Winson Chungece7f5b2010-10-22 14:54:12 -0700919 ((mCountY - 1) * mHeightGap);
Michael Jurka5f1c5092010-09-03 14:15:02 -0700920 setMeasuredDimension(newWidth, newHeight);
921 } else if (widthSpecMode == MeasureSpec.EXACTLY) {
922 setMeasuredDimension(widthSpecSize, heightSpecSize);
923 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800924 }
925
926 @Override
Michael Jurka28750fb2010-09-24 17:43:49 -0700927 protected void onLayout(boolean changed, int l, int t, int r, int b) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800928 int count = getChildCount();
929
930 for (int i = 0; i < count; i++) {
Patrick Dubroyce34a972010-10-19 10:34:32 -0700931 final View child = getChildAt(i);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800932 if (child.getVisibility() != GONE) {
933
934 CellLayout.LayoutParams lp = (CellLayout.LayoutParams) child.getLayoutParams();
935
936 int childLeft = lp.x;
937 int childTop = lp.y;
938 child.layout(childLeft, childTop, childLeft + lp.width, childTop + lp.height);
Romain Guy84f296c2009-11-04 15:00:44 -0800939
940 if (lp.dropped) {
941 lp.dropped = false;
942
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700943 final int[] cellXY = mTmpCellXY;
Romain Guy06762ab2010-01-25 16:51:08 -0800944 getLocationOnScreen(cellXY);
Jeff Brown1d0867c2010-12-02 18:27:39 -0800945 mWallpaperManager.sendWallpaperCommand(getWindowToken(),
946 WallpaperManager.COMMAND_DROP,
Romain Guy06762ab2010-01-25 16:51:08 -0800947 cellXY[0] + childLeft + lp.width / 2,
948 cellXY[1] + childTop + lp.height / 2, 0, null);
Patrick Dubroyce34a972010-10-19 10:34:32 -0700949
Michael Jurkad3ef3062010-11-23 16:23:58 -0800950 if (lp.animateDrop) {
951 lp.animateDrop = false;
Adam Cohenc42d5e32011-01-07 19:17:51 -0800952
953 // This call needs to be posted since it results in a requestLayout()
954 // which shouldn't be called from within a layout pass (can cause a bad
955 // state of the tree).
956 post(new Runnable() {
957 public void run() {
958 ((Workspace) mParent).animateViewIntoPosition(child);
959 }
960 });
Michael Jurkad3ef3062010-11-23 16:23:58 -0800961 }
Romain Guy84f296c2009-11-04 15:00:44 -0800962 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800963 }
964 }
Michael Jurkabea15192010-11-17 12:33:46 -0800965 prepareCacheBitmap();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800966 }
967
968 @Override
Michael Jurkadee05892010-07-27 10:01:56 -0700969 protected void onSizeChanged(int w, int h, int oldw, int oldh) {
970 super.onSizeChanged(w, h, oldw, oldh);
Michael Jurka18014792010-10-14 09:01:34 -0700971 mBackgroundRect.set(0, 0, w, h);
Michael Jurka33945b22010-12-21 18:19:38 -0800972 updateGlowRect();
Michael Jurkabea15192010-11-17 12:33:46 -0800973 mCacheRect.set(0, 0, (int) (mBitmapCacheScale * w), (int) (mBitmapCacheScale * h));
974 mCache = null;
975 prepareCacheBitmap();
976 invalidateCache();
Michael Jurkadee05892010-07-27 10:01:56 -0700977 }
978
979 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800980 protected void setChildrenDrawingCacheEnabled(boolean enabled) {
981 final int count = getChildCount();
982 for (int i = 0; i < count; i++) {
983 final View view = getChildAt(i);
984 view.setDrawingCacheEnabled(enabled);
985 // Update the drawing caches
Romain Guy3cf4c032010-10-26 14:29:35 -0700986 if (!view.isHardwareAccelerated()) {
987 view.buildDrawingCache(true);
988 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800989 }
990 }
991
992 @Override
993 protected void setChildrenDrawnWithCacheEnabled(boolean enabled) {
994 super.setChildrenDrawnWithCacheEnabled(enabled);
995 }
996
Michael Jurka5f1c5092010-09-03 14:15:02 -0700997 public float getBackgroundAlpha() {
998 return mBackgroundAlpha;
Michael Jurkadee05892010-07-27 10:01:56 -0700999 }
1000
Adam Cohen1b0aaac2010-10-28 11:11:18 -07001001 public void setBackgroundAlphaMultiplier(float multiplier) {
1002 mBackgroundAlphaMultiplier = multiplier;
1003 }
1004
Adam Cohenddb82192010-11-10 16:32:54 -08001005 public float getBackgroundAlphaMultiplier() {
1006 return mBackgroundAlphaMultiplier;
1007 }
1008
Michael Jurka5f1c5092010-09-03 14:15:02 -07001009 public void setBackgroundAlpha(float alpha) {
1010 mBackgroundAlpha = alpha;
Michael Jurka0142d492010-08-25 17:46:15 -07001011 invalidate();
Michael Jurkadee05892010-07-27 10:01:56 -07001012 }
1013
Michael Jurka5f1c5092010-09-03 14:15:02 -07001014 // Need to return true to let the view system know we know how to handle alpha-- this is
1015 // because when our children have an alpha of 0.0f, they are still rendering their "dimmed"
1016 // versions
1017 @Override
1018 protected boolean onSetAlpha(int alpha) {
1019 return true;
1020 }
1021
1022 public void setAlpha(float alpha) {
1023 setChildrenAlpha(alpha);
1024 super.setAlpha(alpha);
1025 }
1026
Michael Jurkadee05892010-07-27 10:01:56 -07001027 private void setChildrenAlpha(float alpha) {
Michael Jurka0142d492010-08-25 17:46:15 -07001028 final int childCount = getChildCount();
1029 for (int i = 0; i < childCount; i++) {
Michael Jurkadee05892010-07-27 10:01:56 -07001030 getChildAt(i).setAlpha(alpha);
1031 }
1032 }
1033
Michael Jurka0280c3b2010-09-17 15:00:07 -07001034 private boolean isVacantIgnoring(
1035 int originX, int originY, int spanX, int spanY, View ignoreView) {
1036 if (ignoreView != null) {
1037 markCellsAsUnoccupiedForView(ignoreView);
1038 }
Michael Jurka28750fb2010-09-24 17:43:49 -07001039 boolean isVacant = true;
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001040 for (int i = 0; i < spanY; i++) {
1041 if (!isRowEmpty(originY + i, originX, originX + spanX - 1, mOccupied)) {
Michael Jurka28750fb2010-09-24 17:43:49 -07001042 isVacant = false;
1043 break;
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001044 }
1045 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001046 if (ignoreView != null) {
1047 markCellsAsOccupiedForView(ignoreView);
1048 }
Michael Jurka28750fb2010-09-24 17:43:49 -07001049 return isVacant;
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001050 }
1051
Michael Jurka0280c3b2010-09-17 15:00:07 -07001052 private boolean isVacant(int originX, int originY, int spanX, int spanY) {
1053 return isVacantIgnoring(originX, originY, spanX, spanY, null);
1054 }
1055
Patrick Dubroy440c3602010-07-13 17:50:32 -07001056 public View getChildAt(int x, int y) {
1057 final int count = getChildCount();
1058 for (int i = 0; i < count; i++) {
1059 View child = getChildAt(i);
1060 LayoutParams lp = (LayoutParams) child.getLayoutParams();
1061
1062 if ((lp.cellX <= x) && (x < lp.cellX + lp.cellHSpan) &&
1063 (lp.cellY <= y) && (y < lp.cellY + lp.cellHSpan)) {
1064 return child;
1065 }
1066 }
1067 return null;
1068 }
1069
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001070 /**
1071 * Estimate where the top left cell of the dragged item will land if it is dropped.
1072 *
1073 * @param originX The X value of the top left corner of the item
1074 * @param originY The Y value of the top left corner of the item
1075 * @param spanX The number of horizontal cells that the item spans
1076 * @param spanY The number of vertical cells that the item spans
1077 * @param result The estimated drop cell X and Y.
1078 */
1079 void estimateDropCell(int originX, int originY, int spanX, int spanY, int[] result) {
Adam Cohend22015c2010-07-26 22:02:18 -07001080 final int countX = mCountX;
1081 final int countY = mCountY;
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001082
Michael Jurkaa63c4522010-08-19 13:52:27 -07001083 // pointToCellRounded takes the top left of a cell but will pad that with
1084 // cellWidth/2 and cellHeight/2 when finding the matching cell
1085 pointToCellRounded(originX, originY, result);
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001086
1087 // If the item isn't fully on this screen, snap to the edges
1088 int rightOverhang = result[0] + spanX - countX;
1089 if (rightOverhang > 0) {
1090 result[0] -= rightOverhang; // Snap to right
1091 }
1092 result[0] = Math.max(0, result[0]); // Snap to left
1093 int bottomOverhang = result[1] + spanY - countY;
1094 if (bottomOverhang > 0) {
1095 result[1] -= bottomOverhang; // Snap to bottom
1096 }
1097 result[1] = Math.max(0, result[1]); // Snap to top
1098 }
1099
Joe Onorato4be866d2010-10-10 11:26:02 -07001100 void visualizeDropLocation(
1101 View v, Bitmap dragOutline, int originX, int originY, int spanX, int spanY) {
1102
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -07001103 final int oldDragCellX = mDragCell[0];
1104 final int oldDragCellY = mDragCell[1];
Joe Onorato4be866d2010-10-10 11:26:02 -07001105 final int[] nearest = findNearestVacantArea(originX, originY, spanX, spanY, v, mDragCell);
Winson Chunga9abd0e2010-10-27 17:18:37 -07001106 if (v != null) {
1107 mDragCenter.set(originX + (v.getWidth() / 2), originY + (v.getHeight() / 2));
1108 } else {
1109 mDragCenter.set(originX, originY);
1110 }
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001111
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -07001112 if (nearest != null && (nearest[0] != oldDragCellX || nearest[1] != oldDragCellY)) {
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001113 // Find the top left corner of the rect the object will occupy
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001114 final int[] topLeft = mTmpPoint;
1115 cellToPoint(nearest[0], nearest[1], topLeft);
1116
Joe Onorato4be866d2010-10-10 11:26:02 -07001117 int left = topLeft[0];
1118 int top = topLeft[1];
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001119
Winson Chunga9abd0e2010-10-27 17:18:37 -07001120 if (v != null) {
1121 if (v.getParent() instanceof CellLayout) {
1122 LayoutParams lp = (LayoutParams) v.getLayoutParams();
1123 left += lp.leftMargin;
1124 top += lp.topMargin;
1125 }
Winson Chung150fbab2010-09-29 17:14:26 -07001126
Winson Chunga9abd0e2010-10-27 17:18:37 -07001127 // Offsets due to the size difference between the View and the dragOutline
1128 left += (v.getWidth() - dragOutline.getWidth()) / 2;
1129 top += (v.getHeight() - dragOutline.getHeight()) / 2;
1130 }
Winson Chung150fbab2010-09-29 17:14:26 -07001131
Joe Onorato4be866d2010-10-10 11:26:02 -07001132 final int oldIndex = mDragOutlineCurrent;
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -07001133 mDragOutlineAnims[oldIndex].animateOut();
1134 mDragOutlineCurrent = (oldIndex + 1) % mDragOutlines.length;
Winson Chung150fbab2010-09-29 17:14:26 -07001135
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -07001136 mDragOutlines[mDragOutlineCurrent].set(left, top);
1137 mDragOutlineAnims[mDragOutlineCurrent].setTag(dragOutline);
1138 mDragOutlineAnims[mDragOutlineCurrent].animateIn();
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001139 }
Patrick Dubroy49250ad2010-10-08 15:33:52 -07001140
1141 // If we are drawing crosshairs, the entire CellLayout needs to be invalidated
1142 if (mCrosshairsDrawable != null) {
1143 invalidate();
1144 }
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001145 }
1146
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001147 /**
Jeff Sharkey70864282009-04-07 21:08:40 -07001148 * Find a vacant area that will fit the given bounds nearest the requested
1149 * cell location. Uses Euclidean distance to score multiple vacant areas.
Winson Chungaafa03c2010-06-11 17:34:16 -07001150 *
Romain Guy51afc022009-05-04 18:03:43 -07001151 * @param pixelX The X location at which you want to search for a vacant area.
1152 * @param pixelY The Y location at which you want to search for a vacant area.
Jeff Sharkey70864282009-04-07 21:08:40 -07001153 * @param spanX Horizontal span of the object.
1154 * @param spanY Vertical span of the object.
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001155 * @param result Array in which to place the result, or null (in which case a new array will
1156 * be allocated)
Jeff Sharkey70864282009-04-07 21:08:40 -07001157 * @return The X, Y cell of a vacant area that can contain this object,
1158 * nearest the requested location.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001159 */
Michael Jurka6a1435d2010-09-27 17:35:12 -07001160 int[] findNearestVacantArea(
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001161 int pixelX, int pixelY, int spanX, int spanY, int[] result) {
1162 return findNearestVacantArea(pixelX, pixelY, spanX, spanY, null, result);
Michael Jurka6a1435d2010-09-27 17:35:12 -07001163 }
Winson Chungaafa03c2010-06-11 17:34:16 -07001164
Michael Jurka6a1435d2010-09-27 17:35:12 -07001165 /**
1166 * Find a vacant area that will fit the given bounds nearest the requested
1167 * cell location. Uses Euclidean distance to score multiple vacant areas.
1168 *
1169 * @param pixelX The X location at which you want to search for a vacant area.
1170 * @param pixelY The Y location at which you want to search for a vacant area.
1171 * @param spanX Horizontal span of the object.
1172 * @param spanY Vertical span of the object.
Michael Jurka6a1435d2010-09-27 17:35:12 -07001173 * @param ignoreView Considers space occupied by this view as unoccupied
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001174 * @param result Previously returned value to possibly recycle.
Michael Jurka6a1435d2010-09-27 17:35:12 -07001175 * @return The X, Y cell of a vacant area that can contain this object,
1176 * nearest the requested location.
1177 */
1178 int[] findNearestVacantArea(
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001179 int pixelX, int pixelY, int spanX, int spanY, View ignoreView, int[] result) {
Michael Jurkac6ee42e2010-09-30 12:04:50 -07001180 // mark space take by ignoreView as available (method checks if ignoreView is null)
1181 markCellsAsUnoccupiedForView(ignoreView);
1182
Jeff Sharkey70864282009-04-07 21:08:40 -07001183 // Keep track of best-scoring drop area
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001184 final int[] bestXY = result != null ? result : new int[2];
Jeff Sharkey70864282009-04-07 21:08:40 -07001185 double bestDistance = Double.MAX_VALUE;
Winson Chungaafa03c2010-06-11 17:34:16 -07001186
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001187 final int countX = mCountX;
1188 final int countY = mCountY;
1189 final boolean[][] occupied = mOccupied;
1190
Winson Chungbbc60d82010-11-11 16:34:41 -08001191 for (int y = 0; y < countY - (spanY - 1); y++) {
Michael Jurkac28de512010-08-13 11:27:44 -07001192 inner:
Winson Chungbbc60d82010-11-11 16:34:41 -08001193 for (int x = 0; x < countX - (spanX - 1); x++) {
Michael Jurkac28de512010-08-13 11:27:44 -07001194 for (int i = 0; i < spanX; i++) {
1195 for (int j = 0; j < spanY; j++) {
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001196 if (occupied[x + i][y + j]) {
Winson Chungbbc60d82010-11-11 16:34:41 -08001197 // small optimization: we can skip to after the column we just found
Michael Jurkac28de512010-08-13 11:27:44 -07001198 // an occupied cell
Winson Chungbbc60d82010-11-11 16:34:41 -08001199 x += i;
Michael Jurkac28de512010-08-13 11:27:44 -07001200 continue inner;
1201 }
1202 }
1203 }
1204 final int[] cellXY = mTmpCellXY;
1205 cellToPoint(x, y, cellXY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001206
Michael Jurkac28de512010-08-13 11:27:44 -07001207 double distance = Math.sqrt(Math.pow(cellXY[0] - pixelX, 2)
1208 + Math.pow(cellXY[1] - pixelY, 2));
1209 if (distance <= bestDistance) {
1210 bestDistance = distance;
1211 bestXY[0] = x;
1212 bestXY[1] = y;
1213 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001214 }
1215 }
Michael Jurkac6ee42e2010-09-30 12:04:50 -07001216 // re-mark space taken by ignoreView as occupied
1217 markCellsAsOccupiedForView(ignoreView);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001218
Winson Chungaafa03c2010-06-11 17:34:16 -07001219 // Return null if no suitable location found
Jeff Sharkey70864282009-04-07 21:08:40 -07001220 if (bestDistance < Double.MAX_VALUE) {
1221 return bestXY;
1222 } else {
1223 return null;
1224 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001225 }
Winson Chungaafa03c2010-06-11 17:34:16 -07001226
Michael Jurka0280c3b2010-09-17 15:00:07 -07001227 boolean existsEmptyCell() {
1228 return findCellForSpan(null, 1, 1);
1229 }
1230
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001231 /**
Michael Jurka0280c3b2010-09-17 15:00:07 -07001232 * Finds the upper-left coordinate of the first rectangle in the grid that can
1233 * hold a cell of the specified dimensions. If intersectX and intersectY are not -1,
1234 * then this method will only return coordinates for rectangles that contain the cell
1235 * (intersectX, intersectY)
1236 *
1237 * @param cellXY The array that will contain the position of a vacant cell if such a cell
1238 * can be found.
1239 * @param spanX The horizontal span of the cell we want to find.
1240 * @param spanY The vertical span of the cell we want to find.
1241 *
1242 * @return True if a vacant cell of the specified dimension was found, false otherwise.
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001243 */
Michael Jurka0280c3b2010-09-17 15:00:07 -07001244 boolean findCellForSpan(int[] cellXY, int spanX, int spanY) {
1245 return findCellForSpanThatIntersectsIgnoring(cellXY, spanX, spanY, -1, -1, null);
1246 }
1247
1248 /**
1249 * Like above, but ignores any cells occupied by the item "ignoreView"
1250 *
1251 * @param cellXY The array that will contain the position of a vacant cell if such a cell
1252 * can be found.
1253 * @param spanX The horizontal span of the cell we want to find.
1254 * @param spanY The vertical span of the cell we want to find.
1255 * @param ignoreView The home screen item we should treat as not occupying any space
1256 * @return
1257 */
1258 boolean findCellForSpanIgnoring(int[] cellXY, int spanX, int spanY, View ignoreView) {
1259 return findCellForSpanThatIntersectsIgnoring(cellXY, spanX, spanY, -1, -1, ignoreView);
1260 }
1261
1262 /**
1263 * Like above, but if intersectX and intersectY are not -1, then this method will try to
1264 * return coordinates for rectangles that contain the cell [intersectX, intersectY]
1265 *
1266 * @param spanX The horizontal span of the cell we want to find.
1267 * @param spanY The vertical span of the cell we want to find.
1268 * @param ignoreView The home screen item we should treat as not occupying any space
1269 * @param intersectX The X coordinate of the cell that we should try to overlap
1270 * @param intersectX The Y coordinate of the cell that we should try to overlap
1271 *
1272 * @return True if a vacant cell of the specified dimension was found, false otherwise.
1273 */
1274 boolean findCellForSpanThatIntersects(int[] cellXY, int spanX, int spanY,
1275 int intersectX, int intersectY) {
1276 return findCellForSpanThatIntersectsIgnoring(
1277 cellXY, spanX, spanY, intersectX, intersectY, null);
1278 }
1279
1280 /**
1281 * The superset of the above two methods
1282 */
1283 boolean findCellForSpanThatIntersectsIgnoring(int[] cellXY, int spanX, int spanY,
1284 int intersectX, int intersectY, View ignoreView) {
Michael Jurkac6ee42e2010-09-30 12:04:50 -07001285 // mark space take by ignoreView as available (method checks if ignoreView is null)
1286 markCellsAsUnoccupiedForView(ignoreView);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001287
Michael Jurka28750fb2010-09-24 17:43:49 -07001288 boolean foundCell = false;
Michael Jurka0280c3b2010-09-17 15:00:07 -07001289 while (true) {
1290 int startX = 0;
1291 if (intersectX >= 0) {
1292 startX = Math.max(startX, intersectX - (spanX - 1));
1293 }
1294 int endX = mCountX - (spanX - 1);
1295 if (intersectX >= 0) {
1296 endX = Math.min(endX, intersectX + (spanX - 1) + (spanX == 1 ? 1 : 0));
1297 }
1298 int startY = 0;
1299 if (intersectY >= 0) {
1300 startY = Math.max(startY, intersectY - (spanY - 1));
1301 }
1302 int endY = mCountY - (spanY - 1);
1303 if (intersectY >= 0) {
1304 endY = Math.min(endY, intersectY + (spanY - 1) + (spanY == 1 ? 1 : 0));
1305 }
1306
Winson Chungbbc60d82010-11-11 16:34:41 -08001307 for (int y = startY; y < endY && !foundCell; y++) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001308 inner:
Winson Chungbbc60d82010-11-11 16:34:41 -08001309 for (int x = startX; x < endX; x++) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001310 for (int i = 0; i < spanX; i++) {
1311 for (int j = 0; j < spanY; j++) {
1312 if (mOccupied[x + i][y + j]) {
Winson Chungbbc60d82010-11-11 16:34:41 -08001313 // small optimization: we can skip to after the column we just found
Michael Jurka0280c3b2010-09-17 15:00:07 -07001314 // an occupied cell
Winson Chungbbc60d82010-11-11 16:34:41 -08001315 x += i;
Michael Jurka0280c3b2010-09-17 15:00:07 -07001316 continue inner;
1317 }
1318 }
1319 }
1320 if (cellXY != null) {
1321 cellXY[0] = x;
1322 cellXY[1] = y;
1323 }
Michael Jurka28750fb2010-09-24 17:43:49 -07001324 foundCell = true;
1325 break;
Michael Jurka0280c3b2010-09-17 15:00:07 -07001326 }
1327 }
1328 if (intersectX == -1 && intersectY == -1) {
1329 break;
1330 } else {
1331 // if we failed to find anything, try again but without any requirements of
1332 // intersecting
1333 intersectX = -1;
1334 intersectY = -1;
1335 continue;
1336 }
1337 }
1338
Michael Jurkac6ee42e2010-09-30 12:04:50 -07001339 // re-mark space taken by ignoreView as occupied
1340 markCellsAsOccupiedForView(ignoreView);
Michael Jurka28750fb2010-09-24 17:43:49 -07001341 return foundCell;
Michael Jurka0280c3b2010-09-17 15:00:07 -07001342 }
1343
1344 /**
1345 * Called when drag has left this CellLayout or has been completed (successfully or not)
1346 */
1347 void onDragExit() {
Joe Onorato4be866d2010-10-10 11:26:02 -07001348 // This can actually be called when we aren't in a drag, e.g. when adding a new
1349 // item to this layout via the customize drawer.
1350 // Guard against that case.
1351 if (mDragging) {
1352 mDragging = false;
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001353
Joe Onorato4be866d2010-10-10 11:26:02 -07001354 // Fade out the drag indicators
1355 if (mCrosshairsAnimator != null) {
1356 mCrosshairsAnimator.animateOut();
1357 }
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001358 }
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -07001359
1360 // Invalidate the drag data
1361 mDragCell[0] = -1;
1362 mDragCell[1] = -1;
1363 mDragOutlineAnims[mDragOutlineCurrent].animateOut();
1364 mDragOutlineCurrent = (mDragOutlineCurrent + 1) % mDragOutlineAnims.length;
1365
Michael Jurka33945b22010-12-21 18:19:38 -08001366 setIsDragOverlapping(false);
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001367 }
1368
1369 /**
Winson Chungaafa03c2010-06-11 17:34:16 -07001370 * Mark a child as having been dropped.
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001371 * At the beginning of the drag operation, the child may have been on another
Patrick Dubroyce34a972010-10-19 10:34:32 -07001372 * screen, but it is re-parented before this method is called.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001373 *
1374 * @param child The child that is being dropped
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001375 */
Michael Jurkad3ef3062010-11-23 16:23:58 -08001376 void onDropChild(View child, boolean animate) {
Romain Guyd94533d2009-08-17 10:01:15 -07001377 if (child != null) {
1378 LayoutParams lp = (LayoutParams) child.getLayoutParams();
Romain Guyd94533d2009-08-17 10:01:15 -07001379 lp.isDragging = false;
Romain Guy84f296c2009-11-04 15:00:44 -08001380 lp.dropped = true;
Michael Jurkad3ef3062010-11-23 16:23:58 -08001381 lp.animateDrop = animate;
Patrick Dubroyce34a972010-10-19 10:34:32 -07001382 child.setVisibility(View.VISIBLE);
Romain Guyd94533d2009-08-17 10:01:15 -07001383 child.requestLayout();
Romain Guyd94533d2009-08-17 10:01:15 -07001384 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001385 }
1386
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001387 /**
1388 * Start dragging the specified child
Winson Chungaafa03c2010-06-11 17:34:16 -07001389 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001390 * @param child The child that is being dragged
1391 */
1392 void onDragChild(View child) {
1393 LayoutParams lp = (LayoutParams) child.getLayoutParams();
1394 lp.isDragging = true;
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001395 }
1396
1397 /**
1398 * A drag event has begun over this layout.
1399 * It may have begun over this layout (in which case onDragChild is called first),
1400 * or it may have begun on another layout.
1401 */
Winson Chunga9abd0e2010-10-27 17:18:37 -07001402 void onDragEnter() {
Patrick Dubroyfe6bd872010-10-13 17:32:10 -07001403 if (!mDragging) {
Patrick Dubroyfe6bd872010-10-13 17:32:10 -07001404 // Fade in the drag indicators
1405 if (mCrosshairsAnimator != null) {
1406 mCrosshairsAnimator.animateIn();
1407 }
Joe Onorato4be866d2010-10-10 11:26:02 -07001408 }
1409 mDragging = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001410 }
Winson Chungaafa03c2010-06-11 17:34:16 -07001411
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001412 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001413 * Computes a bounding rectangle for a range of cells
Winson Chungaafa03c2010-06-11 17:34:16 -07001414 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001415 * @param cellX X coordinate of upper left corner expressed as a cell position
1416 * @param cellY Y coordinate of upper left corner expressed as a cell position
Winson Chungaafa03c2010-06-11 17:34:16 -07001417 * @param cellHSpan Width in cells
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001418 * @param cellVSpan Height in cells
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001419 * @param resultRect Rect into which to put the results
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001420 */
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001421 public void cellToRect(int cellX, int cellY, int cellHSpan, int cellVSpan, RectF resultRect) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001422 final int cellWidth = mCellWidth;
1423 final int cellHeight = mCellHeight;
1424 final int widthGap = mWidthGap;
1425 final int heightGap = mHeightGap;
Winson Chungaafa03c2010-06-11 17:34:16 -07001426
1427 final int hStartPadding = getLeftPadding();
1428 final int vStartPadding = getTopPadding();
1429
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001430 int width = cellHSpan * cellWidth + ((cellHSpan - 1) * widthGap);
1431 int height = cellVSpan * cellHeight + ((cellVSpan - 1) * heightGap);
1432
1433 int x = hStartPadding + cellX * (cellWidth + widthGap);
1434 int y = vStartPadding + cellY * (cellHeight + heightGap);
Winson Chungaafa03c2010-06-11 17:34:16 -07001435
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001436 resultRect.set(x, y, x + width, y + height);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001437 }
Winson Chungaafa03c2010-06-11 17:34:16 -07001438
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001439 /**
Winson Chungaafa03c2010-06-11 17:34:16 -07001440 * Computes the required horizontal and vertical cell spans to always
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001441 * fit the given rectangle.
Winson Chungaafa03c2010-06-11 17:34:16 -07001442 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001443 * @param width Width in pixels
1444 * @param height Height in pixels
Patrick Dubroy8f86ddc2010-07-16 13:55:32 -07001445 * @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 -08001446 */
Patrick Dubroy8f86ddc2010-07-16 13:55:32 -07001447 public int[] rectToCell(int width, int height, int[] result) {
Michael Jurka9987a5c2010-10-08 16:58:12 -07001448 return rectToCell(getResources(), width, height, result);
1449 }
1450
1451 public static int[] rectToCell(Resources resources, int width, int height, int[] result) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001452 // Always assume we're working with the smallest span to make sure we
1453 // reserve enough space in both orientations.
Joe Onorato79e56262009-09-21 15:23:04 -04001454 int actualWidth = resources.getDimensionPixelSize(R.dimen.workspace_cell_width);
1455 int actualHeight = resources.getDimensionPixelSize(R.dimen.workspace_cell_height);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001456 int smallerSize = Math.min(actualWidth, actualHeight);
Joe Onorato79e56262009-09-21 15:23:04 -04001457
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001458 // Always round up to next largest cell
1459 int spanX = (width + smallerSize) / smallerSize;
1460 int spanY = (height + smallerSize) / smallerSize;
Joe Onorato79e56262009-09-21 15:23:04 -04001461
Patrick Dubroy8f86ddc2010-07-16 13:55:32 -07001462 if (result == null) {
1463 return new int[] { spanX, spanY };
1464 }
1465 result[0] = spanX;
1466 result[1] = spanY;
1467 return result;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001468 }
1469
1470 /**
Patrick Dubroy047379a2010-12-19 22:02:04 -08001471 * Calculate the grid spans needed to fit given item
1472 */
1473 public void calculateSpans(ItemInfo info) {
1474 final int minWidth;
1475 final int minHeight;
1476
1477 if (info instanceof LauncherAppWidgetInfo) {
1478 minWidth = ((LauncherAppWidgetInfo) info).minWidth;
1479 minHeight = ((LauncherAppWidgetInfo) info).minHeight;
1480 } else if (info instanceof PendingAddWidgetInfo) {
1481 minWidth = ((PendingAddWidgetInfo) info).minWidth;
1482 minHeight = ((PendingAddWidgetInfo) info).minHeight;
1483 } else {
1484 // It's not a widget, so it must be 1x1
1485 info.spanX = info.spanY = 1;
1486 return;
1487 }
1488 int[] spans = rectToCell(minWidth, minHeight, null);
1489 info.spanX = spans[0];
1490 info.spanY = spans[1];
1491 }
1492
1493 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001494 * Find the first vacant cell, if there is one.
1495 *
1496 * @param vacant Holds the x and y coordinate of the vacant cell
1497 * @param spanX Horizontal cell span.
1498 * @param spanY Vertical cell span.
Winson Chungaafa03c2010-06-11 17:34:16 -07001499 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001500 * @return True if a vacant cell was found
1501 */
1502 public boolean getVacantCell(int[] vacant, int spanX, int spanY) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001503
Michael Jurka0280c3b2010-09-17 15:00:07 -07001504 return findVacantCell(vacant, spanX, spanY, mCountX, mCountY, mOccupied);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001505 }
1506
1507 static boolean findVacantCell(int[] vacant, int spanX, int spanY,
1508 int xCount, int yCount, boolean[][] occupied) {
1509
1510 for (int x = 0; x < xCount; x++) {
1511 for (int y = 0; y < yCount; y++) {
1512 boolean available = !occupied[x][y];
1513out: for (int i = x; i < x + spanX - 1 && x < xCount; i++) {
1514 for (int j = y; j < y + spanY - 1 && y < yCount; j++) {
1515 available = available && !occupied[i][j];
1516 if (!available) break out;
1517 }
1518 }
1519
1520 if (available) {
1521 vacant[0] = x;
1522 vacant[1] = y;
1523 return true;
1524 }
1525 }
1526 }
1527
1528 return false;
1529 }
1530
Michael Jurka0280c3b2010-09-17 15:00:07 -07001531 private void clearOccupiedCells() {
1532 for (int x = 0; x < mCountX; x++) {
1533 for (int y = 0; y < mCountY; y++) {
1534 mOccupied[x][y] = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001535 }
1536 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001537 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001538
Michael Jurka0280c3b2010-09-17 15:00:07 -07001539 public void onMove(View view, int newCellX, int newCellY) {
1540 LayoutParams lp = (LayoutParams) view.getLayoutParams();
1541 markCellsAsUnoccupiedForView(view);
1542 markCellsForView(newCellX, newCellY, lp.cellHSpan, lp.cellVSpan, true);
1543 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001544
Michael Jurka0280c3b2010-09-17 15:00:07 -07001545 private void markCellsAsOccupiedForView(View view) {
Michael Jurkac6ee42e2010-09-30 12:04:50 -07001546 if (view == null || view.getParent() != this) return;
Michael Jurka0280c3b2010-09-17 15:00:07 -07001547 LayoutParams lp = (LayoutParams) view.getLayoutParams();
1548 markCellsForView(lp.cellX, lp.cellY, lp.cellHSpan, lp.cellVSpan, true);
1549 }
1550
1551 private void markCellsAsUnoccupiedForView(View view) {
Michael Jurkac6ee42e2010-09-30 12:04:50 -07001552 if (view == null || view.getParent() != this) return;
Michael Jurka0280c3b2010-09-17 15:00:07 -07001553 LayoutParams lp = (LayoutParams) view.getLayoutParams();
1554 markCellsForView(lp.cellX, lp.cellY, lp.cellHSpan, lp.cellVSpan, false);
1555 }
1556
1557 private void markCellsForView(int cellX, int cellY, int spanX, int spanY, boolean value) {
1558 for (int x = cellX; x < cellX + spanX && x < mCountX; x++) {
1559 for (int y = cellY; y < cellY + spanY && y < mCountY; y++) {
1560 mOccupied[x][y] = value;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001561 }
1562 }
1563 }
1564
1565 @Override
1566 public ViewGroup.LayoutParams generateLayoutParams(AttributeSet attrs) {
1567 return new CellLayout.LayoutParams(getContext(), attrs);
1568 }
1569
1570 @Override
1571 protected boolean checkLayoutParams(ViewGroup.LayoutParams p) {
1572 return p instanceof CellLayout.LayoutParams;
1573 }
1574
1575 @Override
1576 protected ViewGroup.LayoutParams generateLayoutParams(ViewGroup.LayoutParams p) {
1577 return new CellLayout.LayoutParams(p);
1578 }
1579
Winson Chungaafa03c2010-06-11 17:34:16 -07001580 public static class CellLayoutAnimationController extends LayoutAnimationController {
1581 public CellLayoutAnimationController(Animation animation, float delay) {
1582 super(animation, delay);
1583 }
1584
1585 @Override
1586 protected long getDelayForView(View view) {
1587 return (int) (Math.random() * 150);
1588 }
1589 }
1590
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001591 public static class LayoutParams extends ViewGroup.MarginLayoutParams {
1592 /**
1593 * Horizontal location of the item in the grid.
1594 */
1595 @ViewDebug.ExportedProperty
1596 public int cellX;
1597
1598 /**
1599 * Vertical location of the item in the grid.
1600 */
1601 @ViewDebug.ExportedProperty
1602 public int cellY;
1603
1604 /**
1605 * Number of cells spanned horizontally by the item.
1606 */
1607 @ViewDebug.ExportedProperty
1608 public int cellHSpan;
1609
1610 /**
1611 * Number of cells spanned vertically by the item.
1612 */
1613 @ViewDebug.ExportedProperty
1614 public int cellVSpan;
Winson Chungaafa03c2010-06-11 17:34:16 -07001615
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001616 /**
1617 * Is this item currently being dragged
1618 */
1619 public boolean isDragging;
1620
1621 // X coordinate of the view in the layout.
1622 @ViewDebug.ExportedProperty
1623 int x;
1624 // Y coordinate of the view in the layout.
1625 @ViewDebug.ExportedProperty
1626 int y;
1627
Patrick Dubroyce34a972010-10-19 10:34:32 -07001628 /**
1629 * The old X coordinate of this item, relative to its current parent.
1630 * Used to animate the item into its new position.
1631 */
1632 int oldX;
1633
1634 /**
1635 * The old Y coordinate of this item, relative to its current parent.
1636 * Used to animate the item into its new position.
1637 */
1638 int oldY;
1639
Romain Guy84f296c2009-11-04 15:00:44 -08001640 boolean dropped;
Romain Guyfcb9e712009-10-02 16:06:52 -07001641
Michael Jurkad3ef3062010-11-23 16:23:58 -08001642 boolean animateDrop;
1643
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001644 public LayoutParams(Context c, AttributeSet attrs) {
1645 super(c, attrs);
1646 cellHSpan = 1;
1647 cellVSpan = 1;
1648 }
1649
1650 public LayoutParams(ViewGroup.LayoutParams source) {
1651 super(source);
1652 cellHSpan = 1;
1653 cellVSpan = 1;
1654 }
Winson Chungaafa03c2010-06-11 17:34:16 -07001655
1656 public LayoutParams(LayoutParams source) {
1657 super(source);
1658 this.cellX = source.cellX;
1659 this.cellY = source.cellY;
1660 this.cellHSpan = source.cellHSpan;
1661 this.cellVSpan = source.cellVSpan;
1662 }
1663
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001664 public LayoutParams(int cellX, int cellY, int cellHSpan, int cellVSpan) {
Romain Guy8f19cdd2010-01-08 15:07:00 -08001665 super(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001666 this.cellX = cellX;
1667 this.cellY = cellY;
1668 this.cellHSpan = cellHSpan;
1669 this.cellVSpan = cellVSpan;
1670 }
1671
1672 public void setup(int cellWidth, int cellHeight, int widthGap, int heightGap,
1673 int hStartPadding, int vStartPadding) {
Winson Chungaafa03c2010-06-11 17:34:16 -07001674
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001675 final int myCellHSpan = cellHSpan;
1676 final int myCellVSpan = cellVSpan;
1677 final int myCellX = cellX;
1678 final int myCellY = cellY;
Winson Chungaafa03c2010-06-11 17:34:16 -07001679
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001680 width = myCellHSpan * cellWidth + ((myCellHSpan - 1) * widthGap) -
1681 leftMargin - rightMargin;
1682 height = myCellVSpan * cellHeight + ((myCellVSpan - 1) * heightGap) -
1683 topMargin - bottomMargin;
1684
1685 x = hStartPadding + myCellX * (cellWidth + widthGap) + leftMargin;
1686 y = vStartPadding + myCellY * (cellHeight + heightGap) + topMargin;
1687 }
Winson Chungaafa03c2010-06-11 17:34:16 -07001688
1689 public String toString() {
1690 return "(" + this.cellX + ", " + this.cellY + ")";
1691 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001692 }
1693
Michael Jurka0280c3b2010-09-17 15:00:07 -07001694 // This class stores info for two purposes:
1695 // 1. When dragging items (mDragInfo in Workspace), we store the View, its cellX & cellY,
1696 // its spanX, spanY, and the screen it is on
1697 // 2. When long clicking on an empty cell in a CellLayout, we save information about the
1698 // cellX and cellY coordinates and which page was clicked. We then set this as a tag on
1699 // the CellLayout that was long clicked
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001700 static final class CellInfo implements ContextMenu.ContextMenuInfo {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001701 View cell;
Michael Jurkaa63c4522010-08-19 13:52:27 -07001702 int cellX = -1;
1703 int cellY = -1;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001704 int spanX;
1705 int spanY;
1706 int screen;
1707 boolean valid;
1708
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001709 @Override
1710 public String toString() {
Winson Chungaafa03c2010-06-11 17:34:16 -07001711 return "Cell[view=" + (cell == null ? "null" : cell.getClass())
1712 + ", x=" + cellX + ", y=" + cellY + "]";
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001713 }
1714 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001715}