blob: 73e0c8fb4f1be0b70810291f7f4c1898e2247883 [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
Michael Jurka99b6a5b2011-01-07 15:37:17 -080056public class CellLayout extends ViewGroup implements Dimmable, VisibilityChangedListener {
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;
Michael Jurka99b6a5b2011-01-07 15:37:17 -0800412 invalidate();
413 }
414
415 public void receiveVisibilityChangedMessage(View v) {
416 invalidateCache();
Michael Jurkabea15192010-11-17 12:33:46 -0800417 }
418
419 public void updateCache() {
420 mCacheCanvas.drawColor(0x00000000, Mode.CLEAR);
421
422 float alpha = getAlpha();
423 setAlpha(1.0f);
424 drawChildren(mCacheCanvas);
425 setAlpha(alpha);
426
427 mIsCacheDirty = false;
428 }
429
430 public void dispatchDraw(Canvas canvas) {
431 final int count = getChildCount();
432
433 if (!mIsCacheDirty) {
434 // Check if one of the children (an icon or widget) is dirty
435 for (int i = 0; i < count; i++) {
436 final View child = getChildAt(i);
437 if (child.isDirty()) {
438 mIsCacheDirty = true;
439 break;
440 }
441 }
442 }
443
Michael Jurka026f5552011-01-07 12:58:57 -0800444 boolean useBitmapCache = mIsCacheEnabled && getScaleX() < mMaxScaleForUsingBitmapCache;
Michael Jurkabea15192010-11-17 12:33:46 -0800445 if (mForceCacheUpdate ||
Michael Jurka026f5552011-01-07 12:58:57 -0800446 (useBitmapCache && !mDisableCacheUpdates)) {
Michael Jurkabea15192010-11-17 12:33:46 -0800447 // Sometimes we force a cache update-- this is used to make sure the cache will look as
448 // up-to-date as possible right when we disable cache updates
449 if (mIsCacheDirty) {
450 updateCache();
451 }
452 mForceCacheUpdate = false;
453 }
454
Michael Jurka026f5552011-01-07 12:58:57 -0800455 if (useBitmapCache) {
Michael Jurkabea15192010-11-17 12:33:46 -0800456 mCachePaint.setAlpha((int)(255*getAlpha()));
457 canvas.drawBitmap(mCache, mCacheRect, mBackgroundRect, mCachePaint);
458 } else {
459 super.dispatchDraw(canvas);
460 }
461 }
462
463 private void prepareCacheBitmap() {
464 if (mCache == null) {
465 mCache = Bitmap.createBitmap((int) (getWidth() * mBitmapCacheScale),
466 (int) (getHeight() * mBitmapCacheScale), Config.ARGB_8888);
467
468 mCachePaint = new Paint();
469 mCachePaint.setFilterBitmap(true);
470 mCacheCanvas.setBitmap(mCache);
471 mCacheCanvas.scale(mBitmapCacheScale, mBitmapCacheScale);
472 }
473 }
474
Jeff Sharkey83f111d2009-04-20 21:03:13 -0700475 @Override
Patrick Dubroy1262e362010-10-06 15:49:50 -0700476 protected void onDraw(Canvas canvas) {
Michael Jurka3e7c7632010-10-02 16:01:03 -0700477 // When we're large, we are either drawn in a "hover" state (ie when dragging an item to
478 // a neighboring page) or with just a normal background (if backgroundAlpha > 0.0f)
479 // When we're small, we are either drawn normally or in the "accepts drops" state (during
480 // a drag). However, we also drag the mini hover background *over* one of those two
481 // backgrounds
Winson Chung26cbf3a2011-01-06 16:25:55 -0800482 if (LauncherApplication.isScreenXLarge() && mBackgroundAlpha > 0.0f) {
Adam Cohenf34bab52010-09-30 14:11:56 -0700483 Drawable bg;
Michael Jurka33945b22010-12-21 18:19:38 -0800484 boolean mini = getScaleX() < 0.5f;
485
486 if (mIsDragOverlapping) {
487 // In the mini case, we draw the active_glow bg *over* the active background
488 bg = mini ? mActiveBackgroundMini : mActiveGlowBackground;
489 } else if (mIsDragOccuring && mAcceptsDrops) {
490 bg = mini ? mActiveBackgroundMini : mActiveBackground;
Winson Chungae222ab2011-01-10 11:45:27 -0800491 } else if (mIsDragOccuring && mIsDefaultDropTarget) {
Michael Jurka33945b22010-12-21 18:19:38 -0800492 bg = mini ? mNormalGlowBackgroundMini : mNormalGlowBackground;
Adam Cohenf34bab52010-09-30 14:11:56 -0700493 } else {
Michael Jurka33945b22010-12-21 18:19:38 -0800494 bg = mini ? mNormalBackgroundMini : mNormalBackground;
Adam Cohenf34bab52010-09-30 14:11:56 -0700495 }
Michael Jurka33945b22010-12-21 18:19:38 -0800496
497 bg.setAlpha((int) (mBackgroundAlpha * mBackgroundAlphaMultiplier * 255));
498 bg.setBounds(mBackgroundRect);
499 bg.draw(canvas);
500
501 if (mini && mIsDragOverlapping) {
Michael Jurka18014792010-10-14 09:01:34 -0700502 boolean modifiedClipRect = false;
Michael Jurka33945b22010-12-21 18:19:38 -0800503 if (mGlowBackgroundScale > 1.0f) {
Michael Jurka18014792010-10-14 09:01:34 -0700504 // If the hover background's scale is greater than 1, we'll be drawing outside
505 // the bounds of this CellLayout. Get around that by temporarily increasing the
506 // size of the clip rect
Michael Jurka33945b22010-12-21 18:19:38 -0800507 float marginFraction = (mGlowBackgroundScale - 1.0f) / 2.0f;
Michael Jurka18014792010-10-14 09:01:34 -0700508 Rect clipRect = canvas.getClipBounds();
509 int marginX = (int) (marginFraction * (clipRect.right - clipRect.left));
510 int marginY = (int) (marginFraction * (clipRect.bottom - clipRect.top));
511 canvas.save(Canvas.CLIP_SAVE_FLAG);
512 canvas.clipRect(-marginX, -marginY,
513 getWidth() + marginX, getHeight() + marginY, Region.Op.REPLACE);
514 modifiedClipRect = true;
515 }
516
Michael Jurka33945b22010-12-21 18:19:38 -0800517 mActiveGlowBackgroundMini.setAlpha(
518 (int) (mBackgroundAlpha * mGlowBackgroundAlpha * 255));
519 mActiveGlowBackgroundMini.setBounds(mGlowBackgroundRect);
520 mActiveGlowBackgroundMini.draw(canvas);
Michael Jurka18014792010-10-14 09:01:34 -0700521 if (modifiedClipRect) {
522 canvas.restore();
523 }
Michael Jurka3e7c7632010-10-02 16:01:03 -0700524 }
Michael Jurkaa63c4522010-08-19 13:52:27 -0700525 }
Romain Guya6abce82009-11-10 02:54:41 -0800526
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700527 if (mCrosshairsVisibility > 0.0f) {
528 final int countX = mCountX;
529 final int countY = mCountY;
530
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700531 final float MAX_ALPHA = 0.4f;
532 final int MAX_VISIBLE_DISTANCE = 600;
533 final float DISTANCE_MULTIPLIER = 0.002f;
534
535 final Drawable d = mCrosshairsDrawable;
536 final int width = d.getIntrinsicWidth();
537 final int height = d.getIntrinsicHeight();
538
539 int x = getLeftPadding() - (mWidthGap / 2) - (width / 2);
540 for (int col = 0; col <= countX; col++) {
541 int y = getTopPadding() - (mHeightGap / 2) - (height / 2);
542 for (int row = 0; row <= countY; row++) {
543 mTmpPointF.set(x - mDragCenter.x, y - mDragCenter.y);
544 float dist = mTmpPointF.length();
545 // Crosshairs further from the drag point are more faint
546 float alpha = Math.min(MAX_ALPHA,
547 DISTANCE_MULTIPLIER * (MAX_VISIBLE_DISTANCE - dist));
548 if (alpha > 0.0f) {
549 d.setBounds(x, y, x + width, y + height);
550 d.setAlpha((int) (alpha * 255 * mCrosshairsVisibility));
551 d.draw(canvas);
552 }
553 y += mCellHeight + mHeightGap;
554 }
555 x += mCellWidth + mWidthGap;
556 }
Joe Onorato4be866d2010-10-10 11:26:02 -0700557 }
Winson Chung150fbab2010-09-29 17:14:26 -0700558
Patrick Dubroy8e58e912010-10-14 13:21:48 -0700559 final Paint paint = mDragOutlinePaint;
Joe Onorato4be866d2010-10-10 11:26:02 -0700560 for (int i = 0; i < mDragOutlines.length; i++) {
Chet Haase472b2812010-10-14 07:02:04 -0700561 final float alpha = mDragOutlineAlphas[i];
Joe Onorato4be866d2010-10-10 11:26:02 -0700562 if (alpha > 0) {
563 final Point p = mDragOutlines[i];
564 final Bitmap b = (Bitmap) mDragOutlineAnims[i].getTag();
Chet Haase472b2812010-10-14 07:02:04 -0700565 paint.setAlpha((int)(alpha + .5f));
Joe Onorato4be866d2010-10-10 11:26:02 -0700566 canvas.drawBitmap(b, p.x, p.y, paint);
Winson Chung150fbab2010-09-29 17:14:26 -0700567 }
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700568 }
569 }
570
Adam Cohenf34bab52010-09-30 14:11:56 -0700571 public void setDimmableProgress(float progress) {
572 for (int i = 0; i < getChildCount(); i++) {
573 Dimmable d = (Dimmable) getChildAt(i);
574 d.setDimmableProgress(progress);
575 }
576 }
577
578 public float getDimmableProgress() {
579 if (getChildCount() > 0) {
580 return ((Dimmable) getChildAt(0)).getDimmableProgress();
581 }
582 return 0.0f;
583 }
584
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700585 @Override
Jeff Sharkey83f111d2009-04-20 21:03:13 -0700586 public void cancelLongPress() {
587 super.cancelLongPress();
588
589 // Cancel long press for all children
590 final int count = getChildCount();
591 for (int i = 0; i < count; i++) {
592 final View child = getChildAt(i);
593 child.cancelLongPress();
594 }
595 }
596
Michael Jurkadee05892010-07-27 10:01:56 -0700597 public void setOnInterceptTouchListener(View.OnTouchListener listener) {
598 mInterceptTouchListener = listener;
599 }
600
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800601 int getCountX() {
Adam Cohend22015c2010-07-26 22:02:18 -0700602 return mCountX;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800603 }
604
605 int getCountY() {
Adam Cohend22015c2010-07-26 22:02:18 -0700606 return mCountY;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800607 }
608
Winson Chungaafa03c2010-06-11 17:34:16 -0700609 public boolean addViewToCellLayout(View child, int index, int childId, LayoutParams params) {
Michael Jurkaf3ca3ab2010-10-20 17:08:24 -0700610 return addViewToCellLayout(child, index, childId, params, true);
611 }
612
613 public boolean addViewToCellLayout(
614 View child, int index, int childId, LayoutParams params, boolean markCells) {
Winson Chungaafa03c2010-06-11 17:34:16 -0700615 final LayoutParams lp = params;
616
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800617 // Generate an id for each view, this assumes we have at most 256x256 cells
618 // per workspace screen
Adam Cohend22015c2010-07-26 22:02:18 -0700619 if (lp.cellX >= 0 && lp.cellX <= mCountX - 1 && lp.cellY >= 0 && lp.cellY <= mCountY - 1) {
Winson Chungaafa03c2010-06-11 17:34:16 -0700620 // If the horizontal or vertical span is set to -1, it is taken to
621 // mean that it spans the extent of the CellLayout
Adam Cohend22015c2010-07-26 22:02:18 -0700622 if (lp.cellHSpan < 0) lp.cellHSpan = mCountX;
623 if (lp.cellVSpan < 0) lp.cellVSpan = mCountY;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800624
Winson Chungaafa03c2010-06-11 17:34:16 -0700625 child.setId(childId);
626
627 addView(child, index, lp);
Michael Jurka24232cc2011-01-10 21:41:55 -0800628 child.setAlpha(getAlpha());
Michael Jurka99b6a5b2011-01-07 15:37:17 -0800629 if (child instanceof VisibilityChangedBroadcaster) {
630 VisibilityChangedBroadcaster v = (VisibilityChangedBroadcaster) child;
631 v.setVisibilityChangedListener(this);
632 }
Michael Jurkadee05892010-07-27 10:01:56 -0700633
Michael Jurkabea15192010-11-17 12:33:46 -0800634 // invalidate the cache to have it reflect the new item
635 invalidateCache();
Michael Jurkaf3ca3ab2010-10-20 17:08:24 -0700636 if (markCells) markCellsAsOccupiedForView(child);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700637
Winson Chungaafa03c2010-06-11 17:34:16 -0700638 return true;
639 }
640 return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800641 }
Michael Jurka3e7c7632010-10-02 16:01:03 -0700642
Michael Jurkabea15192010-11-17 12:33:46 -0800643 public void setAcceptsDrops(boolean acceptsDrops) {
644 if (mAcceptsDrops != acceptsDrops) {
645 mAcceptsDrops = acceptsDrops;
646 invalidate();
647 }
648 }
649
Michael Jurka3e7c7632010-10-02 16:01:03 -0700650 public boolean getAcceptsDrops() {
651 return mAcceptsDrops;
652 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800653
654 @Override
Michael Jurka0280c3b2010-09-17 15:00:07 -0700655 public void removeAllViews() {
Joe Onorato7cef2892011-01-07 10:08:48 -0800656 super.removeAllViews();
Michael Jurka0280c3b2010-09-17 15:00:07 -0700657 clearOccupiedCells();
Michael Jurka99b6a5b2011-01-07 15:37:17 -0800658 invalidateCache();
Michael Jurka0280c3b2010-09-17 15:00:07 -0700659 }
660
661 @Override
662 public void removeAllViewsInLayout() {
Joe Onorato7cef2892011-01-07 10:08:48 -0800663 super.removeAllViewsInLayout();
Michael Jurka0280c3b2010-09-17 15:00:07 -0700664 clearOccupiedCells();
Michael Jurka99b6a5b2011-01-07 15:37:17 -0800665 invalidateCache();
Michael Jurka0280c3b2010-09-17 15:00:07 -0700666 }
667
Michael Jurkaf3ca3ab2010-10-20 17:08:24 -0700668 public void removeViewWithoutMarkingCells(View view) {
669 super.removeView(view);
Michael Jurka99b6a5b2011-01-07 15:37:17 -0800670 invalidateCache();
Michael Jurkaf3ca3ab2010-10-20 17:08:24 -0700671 }
672
Michael Jurka0280c3b2010-09-17 15:00:07 -0700673 @Override
674 public void removeView(View view) {
675 markCellsAsUnoccupiedForView(view);
676 super.removeView(view);
Michael Jurka99b6a5b2011-01-07 15:37:17 -0800677 invalidateCache();
Michael Jurka0280c3b2010-09-17 15:00:07 -0700678 }
679
680 @Override
681 public void removeViewAt(int index) {
682 markCellsAsUnoccupiedForView(getChildAt(index));
683 super.removeViewAt(index);
Michael Jurka99b6a5b2011-01-07 15:37:17 -0800684 invalidateCache();
Michael Jurka0280c3b2010-09-17 15:00:07 -0700685 }
686
687 @Override
688 public void removeViewInLayout(View view) {
689 markCellsAsUnoccupiedForView(view);
690 super.removeViewInLayout(view);
Michael Jurka99b6a5b2011-01-07 15:37:17 -0800691 invalidateCache();
Michael Jurka0280c3b2010-09-17 15:00:07 -0700692 }
693
694 @Override
695 public void removeViews(int start, int count) {
696 for (int i = start; i < start + count; i++) {
697 markCellsAsUnoccupiedForView(getChildAt(i));
698 }
699 super.removeViews(start, count);
Michael Jurka99b6a5b2011-01-07 15:37:17 -0800700 invalidateCache();
Michael Jurka0280c3b2010-09-17 15:00:07 -0700701 }
702
703 @Override
704 public void removeViewsInLayout(int start, int count) {
705 for (int i = start; i < start + count; i++) {
706 markCellsAsUnoccupiedForView(getChildAt(i));
707 }
708 super.removeViewsInLayout(start, count);
Michael Jurka99b6a5b2011-01-07 15:37:17 -0800709 invalidateCache();
Michael Jurka0280c3b2010-09-17 15:00:07 -0700710 }
711
712 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800713 public void requestChildFocus(View child, View focused) {
714 super.requestChildFocus(child, focused);
715 if (child != null) {
716 Rect r = new Rect();
717 child.getDrawingRect(r);
718 requestRectangleOnScreen(r);
719 }
720 }
721
722 @Override
723 protected void onAttachedToWindow() {
724 super.onAttachedToWindow();
725 mCellInfo.screen = ((ViewGroup) getParent()).indexOfChild(this);
726 }
727
Michael Jurkaaf442092010-06-10 17:01:57 -0700728 public void setTagToCellInfoForPoint(int touchX, int touchY) {
729 final CellInfo cellInfo = mCellInfo;
730 final Rect frame = mRect;
731 final int x = touchX + mScrollX;
732 final int y = touchY + mScrollY;
733 final int count = getChildCount();
734
735 boolean found = false;
736 for (int i = count - 1; i >= 0; i--) {
737 final View child = getChildAt(i);
738
739 if ((child.getVisibility()) == VISIBLE || child.getAnimation() != null) {
740 child.getHitRect(frame);
741 if (frame.contains(x, y)) {
742 final LayoutParams lp = (LayoutParams) child.getLayoutParams();
743 cellInfo.cell = child;
744 cellInfo.cellX = lp.cellX;
745 cellInfo.cellY = lp.cellY;
746 cellInfo.spanX = lp.cellHSpan;
747 cellInfo.spanY = lp.cellVSpan;
748 cellInfo.valid = true;
749 found = true;
Michael Jurkaaf442092010-06-10 17:01:57 -0700750 break;
751 }
752 }
753 }
Winson Chungaafa03c2010-06-11 17:34:16 -0700754
Michael Jurkaaf442092010-06-10 17:01:57 -0700755 if (!found) {
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700756 final int cellXY[] = mTmpCellXY;
Michael Jurkaaf442092010-06-10 17:01:57 -0700757 pointToCellExact(x, y, cellXY);
758
Michael Jurkaaf442092010-06-10 17:01:57 -0700759 cellInfo.cell = null;
760 cellInfo.cellX = cellXY[0];
761 cellInfo.cellY = cellXY[1];
762 cellInfo.spanX = 1;
763 cellInfo.spanY = 1;
Michael Jurka0280c3b2010-09-17 15:00:07 -0700764 cellInfo.valid = cellXY[0] >= 0 && cellXY[1] >= 0 && cellXY[0] < mCountX &&
765 cellXY[1] < mCountY && !mOccupied[cellXY[0]][cellXY[1]];
Michael Jurkaaf442092010-06-10 17:01:57 -0700766 }
767 setTag(cellInfo);
768 }
769
Winson Chungaafa03c2010-06-11 17:34:16 -0700770
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800771 @Override
772 public boolean onInterceptTouchEvent(MotionEvent ev) {
Michael Jurkadee05892010-07-27 10:01:56 -0700773 if (mInterceptTouchListener != null && mInterceptTouchListener.onTouch(this, ev)) {
774 return true;
775 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800776 final int action = ev.getAction();
777 final CellInfo cellInfo = mCellInfo;
778
779 if (action == MotionEvent.ACTION_DOWN) {
Michael Jurkaaf442092010-06-10 17:01:57 -0700780 setTagToCellInfoForPoint((int) ev.getX(), (int) ev.getY());
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800781 } else if (action == MotionEvent.ACTION_UP) {
782 cellInfo.cell = null;
783 cellInfo.cellX = -1;
784 cellInfo.cellY = -1;
785 cellInfo.spanX = 0;
786 cellInfo.spanY = 0;
787 cellInfo.valid = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800788 setTag(cellInfo);
789 }
790
791 return false;
792 }
793
794 @Override
795 public CellInfo getTag() {
Michael Jurka0280c3b2010-09-17 15:00:07 -0700796 return (CellInfo) super.getTag();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800797 }
798
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700799 /**
800 * Check if the row 'y' is empty from columns 'left' to 'right', inclusive.
801 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800802 private static boolean isRowEmpty(int y, int left, int right, boolean[][] occupied) {
803 for (int x = left; x <= right; x++) {
804 if (occupied[x][y]) {
805 return false;
806 }
807 }
808 return true;
809 }
810
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800811 /**
Winson Chungaafa03c2010-06-11 17:34:16 -0700812 * Given a point, return the cell that strictly encloses that point
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800813 * @param x X coordinate of the point
814 * @param y Y coordinate of the point
815 * @param result Array of 2 ints to hold the x and y coordinate of the cell
816 */
817 void pointToCellExact(int x, int y, int[] result) {
Winson Chungaafa03c2010-06-11 17:34:16 -0700818 final int hStartPadding = getLeftPadding();
819 final int vStartPadding = getTopPadding();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800820
821 result[0] = (x - hStartPadding) / (mCellWidth + mWidthGap);
822 result[1] = (y - vStartPadding) / (mCellHeight + mHeightGap);
823
Adam Cohend22015c2010-07-26 22:02:18 -0700824 final int xAxis = mCountX;
825 final int yAxis = mCountY;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800826
827 if (result[0] < 0) result[0] = 0;
828 if (result[0] >= xAxis) result[0] = xAxis - 1;
829 if (result[1] < 0) result[1] = 0;
830 if (result[1] >= yAxis) result[1] = yAxis - 1;
831 }
Winson Chungaafa03c2010-06-11 17:34:16 -0700832
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800833 /**
834 * Given a point, return the cell that most closely encloses that point
835 * @param x X coordinate of the point
836 * @param y Y coordinate of the point
837 * @param result Array of 2 ints to hold the x and y coordinate of the cell
838 */
839 void pointToCellRounded(int x, int y, int[] result) {
840 pointToCellExact(x + (mCellWidth / 2), y + (mCellHeight / 2), result);
841 }
842
843 /**
844 * Given a cell coordinate, return the point that represents the upper left corner of that cell
Winson Chungaafa03c2010-06-11 17:34:16 -0700845 *
846 * @param cellX X coordinate of the cell
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800847 * @param cellY Y coordinate of the cell
Winson Chungaafa03c2010-06-11 17:34:16 -0700848 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800849 * @param result Array of 2 ints to hold the x and y coordinate of the point
850 */
851 void cellToPoint(int cellX, int cellY, int[] result) {
Winson Chungaafa03c2010-06-11 17:34:16 -0700852 final int hStartPadding = getLeftPadding();
853 final int vStartPadding = getTopPadding();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800854
855 result[0] = hStartPadding + cellX * (mCellWidth + mWidthGap);
856 result[1] = vStartPadding + cellY * (mCellHeight + mHeightGap);
857 }
858
Romain Guy84f296c2009-11-04 15:00:44 -0800859 int getCellWidth() {
860 return mCellWidth;
861 }
862
863 int getCellHeight() {
864 return mCellHeight;
865 }
866
Romain Guy1a304a12009-11-10 00:02:32 -0800867 int getLeftPadding() {
Winson Chungaafa03c2010-06-11 17:34:16 -0700868 return mLeftPadding;
Romain Guy1a304a12009-11-10 00:02:32 -0800869 }
870
871 int getTopPadding() {
Winson Chungaafa03c2010-06-11 17:34:16 -0700872 return mTopPadding;
Romain Guy1a304a12009-11-10 00:02:32 -0800873 }
874
875 int getRightPadding() {
Winson Chungaafa03c2010-06-11 17:34:16 -0700876 return mRightPadding;
Romain Guy1a304a12009-11-10 00:02:32 -0800877 }
878
879 int getBottomPadding() {
Winson Chungaafa03c2010-06-11 17:34:16 -0700880 return mBottomPadding;
Romain Guy1a304a12009-11-10 00:02:32 -0800881 }
882
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800883 @Override
884 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
885 // TODO: currently ignoring padding
Winson Chungaafa03c2010-06-11 17:34:16 -0700886
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800887 int widthSpecMode = MeasureSpec.getMode(widthMeasureSpec);
Winson Chungaafa03c2010-06-11 17:34:16 -0700888 int widthSpecSize = MeasureSpec.getSize(widthMeasureSpec);
889
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800890 int heightSpecMode = MeasureSpec.getMode(heightMeasureSpec);
891 int heightSpecSize = MeasureSpec.getSize(heightMeasureSpec);
Winson Chungaafa03c2010-06-11 17:34:16 -0700892
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800893 if (widthSpecMode == MeasureSpec.UNSPECIFIED || heightSpecMode == MeasureSpec.UNSPECIFIED) {
894 throw new RuntimeException("CellLayout cannot have UNSPECIFIED dimensions");
895 }
896
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800897 final int cellWidth = mCellWidth;
898 final int cellHeight = mCellHeight;
899
Adam Cohend22015c2010-07-26 22:02:18 -0700900 int numWidthGaps = mCountX - 1;
901 int numHeightGaps = mCountY - 1;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800902
Winson Chungece7f5b2010-10-22 14:54:12 -0700903 if (mWidthGap < 0 || mHeightGap < 0) {
904 int vSpaceLeft = heightSpecSize - mTopPadding - mBottomPadding - (cellHeight * mCountY);
905 mHeightGap = vSpaceLeft / numHeightGaps;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800906
Winson Chungece7f5b2010-10-22 14:54:12 -0700907 int hSpaceLeft = widthSpecSize - mLeftPadding - mRightPadding - (cellWidth * mCountX);
908 mWidthGap = hSpaceLeft / numWidthGaps;
Winson Chungaafa03c2010-06-11 17:34:16 -0700909
Winson Chungece7f5b2010-10-22 14:54:12 -0700910 // center it around the min gaps
911 int minGap = Math.min(mWidthGap, mHeightGap);
912 mWidthGap = mHeightGap = minGap;
913 }
Michael Jurka5f1c5092010-09-03 14:15:02 -0700914
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800915 int count = getChildCount();
916
917 for (int i = 0; i < count; i++) {
918 View child = getChildAt(i);
919 LayoutParams lp = (LayoutParams) child.getLayoutParams();
Winson Chungaafa03c2010-06-11 17:34:16 -0700920 lp.setup(cellWidth, cellHeight, mWidthGap, mHeightGap,
921 mLeftPadding, mTopPadding);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800922
Michael Jurka0280c3b2010-09-17 15:00:07 -0700923 int childWidthMeasureSpec = MeasureSpec.makeMeasureSpec(lp.width, MeasureSpec.EXACTLY);
Winson Chungaafa03c2010-06-11 17:34:16 -0700924 int childheightMeasureSpec = MeasureSpec.makeMeasureSpec(lp.height,
925 MeasureSpec.EXACTLY);
926
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800927 child.measure(childWidthMeasureSpec, childheightMeasureSpec);
928 }
Michael Jurka5f1c5092010-09-03 14:15:02 -0700929 if (widthSpecMode == MeasureSpec.AT_MOST) {
930 int newWidth = mLeftPadding + mRightPadding + (mCountX * cellWidth) +
Winson Chungece7f5b2010-10-22 14:54:12 -0700931 ((mCountX - 1) * mWidthGap);
Michael Jurka5f1c5092010-09-03 14:15:02 -0700932 int newHeight = mTopPadding + mBottomPadding + (mCountY * cellHeight) +
Winson Chungece7f5b2010-10-22 14:54:12 -0700933 ((mCountY - 1) * mHeightGap);
Michael Jurka5f1c5092010-09-03 14:15:02 -0700934 setMeasuredDimension(newWidth, newHeight);
935 } else if (widthSpecMode == MeasureSpec.EXACTLY) {
936 setMeasuredDimension(widthSpecSize, heightSpecSize);
937 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800938 }
939
940 @Override
Michael Jurka28750fb2010-09-24 17:43:49 -0700941 protected void onLayout(boolean changed, int l, int t, int r, int b) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800942 int count = getChildCount();
943
944 for (int i = 0; i < count; i++) {
Patrick Dubroyce34a972010-10-19 10:34:32 -0700945 final View child = getChildAt(i);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800946 if (child.getVisibility() != GONE) {
947
948 CellLayout.LayoutParams lp = (CellLayout.LayoutParams) child.getLayoutParams();
949
950 int childLeft = lp.x;
951 int childTop = lp.y;
952 child.layout(childLeft, childTop, childLeft + lp.width, childTop + lp.height);
Romain Guy84f296c2009-11-04 15:00:44 -0800953
954 if (lp.dropped) {
955 lp.dropped = false;
956
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700957 final int[] cellXY = mTmpCellXY;
Romain Guy06762ab2010-01-25 16:51:08 -0800958 getLocationOnScreen(cellXY);
Jeff Brown1d0867c2010-12-02 18:27:39 -0800959 mWallpaperManager.sendWallpaperCommand(getWindowToken(),
960 WallpaperManager.COMMAND_DROP,
Romain Guy06762ab2010-01-25 16:51:08 -0800961 cellXY[0] + childLeft + lp.width / 2,
962 cellXY[1] + childTop + lp.height / 2, 0, null);
Patrick Dubroyce34a972010-10-19 10:34:32 -0700963
Michael Jurkad3ef3062010-11-23 16:23:58 -0800964 if (lp.animateDrop) {
965 lp.animateDrop = false;
Adam Cohenc42d5e32011-01-07 19:17:51 -0800966
Adam Cohendf9de0b2011-01-10 11:01:49 -0800967 // This call does not result in a requestLayout(), but at one point did.
968 // We need to be cautious about any method calls within the layout pass
969 // to insure we don't leave the view tree in a bad state.
970 ((Workspace) mParent).animateViewIntoPosition(child);
Michael Jurkad3ef3062010-11-23 16:23:58 -0800971 }
Romain Guy84f296c2009-11-04 15:00:44 -0800972 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800973 }
974 }
Michael Jurkabea15192010-11-17 12:33:46 -0800975 prepareCacheBitmap();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800976 }
977
978 @Override
Michael Jurkadee05892010-07-27 10:01:56 -0700979 protected void onSizeChanged(int w, int h, int oldw, int oldh) {
980 super.onSizeChanged(w, h, oldw, oldh);
Michael Jurka18014792010-10-14 09:01:34 -0700981 mBackgroundRect.set(0, 0, w, h);
Michael Jurka33945b22010-12-21 18:19:38 -0800982 updateGlowRect();
Michael Jurkabea15192010-11-17 12:33:46 -0800983 mCacheRect.set(0, 0, (int) (mBitmapCacheScale * w), (int) (mBitmapCacheScale * h));
984 mCache = null;
985 prepareCacheBitmap();
986 invalidateCache();
Michael Jurkadee05892010-07-27 10:01:56 -0700987 }
988
989 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800990 protected void setChildrenDrawingCacheEnabled(boolean enabled) {
991 final int count = getChildCount();
992 for (int i = 0; i < count; i++) {
993 final View view = getChildAt(i);
994 view.setDrawingCacheEnabled(enabled);
995 // Update the drawing caches
Romain Guy3cf4c032010-10-26 14:29:35 -0700996 if (!view.isHardwareAccelerated()) {
997 view.buildDrawingCache(true);
998 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800999 }
1000 }
1001
1002 @Override
1003 protected void setChildrenDrawnWithCacheEnabled(boolean enabled) {
1004 super.setChildrenDrawnWithCacheEnabled(enabled);
1005 }
1006
Michael Jurka5f1c5092010-09-03 14:15:02 -07001007 public float getBackgroundAlpha() {
1008 return mBackgroundAlpha;
Michael Jurkadee05892010-07-27 10:01:56 -07001009 }
1010
Adam Cohen1b0aaac2010-10-28 11:11:18 -07001011 public void setBackgroundAlphaMultiplier(float multiplier) {
1012 mBackgroundAlphaMultiplier = multiplier;
1013 }
1014
Adam Cohenddb82192010-11-10 16:32:54 -08001015 public float getBackgroundAlphaMultiplier() {
1016 return mBackgroundAlphaMultiplier;
1017 }
1018
Michael Jurka5f1c5092010-09-03 14:15:02 -07001019 public void setBackgroundAlpha(float alpha) {
1020 mBackgroundAlpha = alpha;
Michael Jurka0142d492010-08-25 17:46:15 -07001021 invalidate();
Michael Jurkadee05892010-07-27 10:01:56 -07001022 }
1023
Michael Jurka5f1c5092010-09-03 14:15:02 -07001024 // Need to return true to let the view system know we know how to handle alpha-- this is
1025 // because when our children have an alpha of 0.0f, they are still rendering their "dimmed"
1026 // versions
1027 @Override
1028 protected boolean onSetAlpha(int alpha) {
1029 return true;
1030 }
1031
1032 public void setAlpha(float alpha) {
1033 setChildrenAlpha(alpha);
1034 super.setAlpha(alpha);
1035 }
1036
Michael Jurkadee05892010-07-27 10:01:56 -07001037 private void setChildrenAlpha(float alpha) {
Michael Jurka0142d492010-08-25 17:46:15 -07001038 final int childCount = getChildCount();
1039 for (int i = 0; i < childCount; i++) {
Michael Jurkadee05892010-07-27 10:01:56 -07001040 getChildAt(i).setAlpha(alpha);
1041 }
1042 }
1043
Michael Jurka0280c3b2010-09-17 15:00:07 -07001044 private boolean isVacantIgnoring(
1045 int originX, int originY, int spanX, int spanY, View ignoreView) {
1046 if (ignoreView != null) {
1047 markCellsAsUnoccupiedForView(ignoreView);
1048 }
Michael Jurka28750fb2010-09-24 17:43:49 -07001049 boolean isVacant = true;
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001050 for (int i = 0; i < spanY; i++) {
1051 if (!isRowEmpty(originY + i, originX, originX + spanX - 1, mOccupied)) {
Michael Jurka28750fb2010-09-24 17:43:49 -07001052 isVacant = false;
1053 break;
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001054 }
1055 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001056 if (ignoreView != null) {
1057 markCellsAsOccupiedForView(ignoreView);
1058 }
Michael Jurka28750fb2010-09-24 17:43:49 -07001059 return isVacant;
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001060 }
1061
Michael Jurka0280c3b2010-09-17 15:00:07 -07001062 private boolean isVacant(int originX, int originY, int spanX, int spanY) {
1063 return isVacantIgnoring(originX, originY, spanX, spanY, null);
1064 }
1065
Patrick Dubroy440c3602010-07-13 17:50:32 -07001066 public View getChildAt(int x, int y) {
1067 final int count = getChildCount();
1068 for (int i = 0; i < count; i++) {
1069 View child = getChildAt(i);
1070 LayoutParams lp = (LayoutParams) child.getLayoutParams();
1071
1072 if ((lp.cellX <= x) && (x < lp.cellX + lp.cellHSpan) &&
1073 (lp.cellY <= y) && (y < lp.cellY + lp.cellHSpan)) {
1074 return child;
1075 }
1076 }
1077 return null;
1078 }
1079
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001080 /**
1081 * Estimate where the top left cell of the dragged item will land if it is dropped.
1082 *
1083 * @param originX The X value of the top left corner of the item
1084 * @param originY The Y value of the top left corner of the item
1085 * @param spanX The number of horizontal cells that the item spans
1086 * @param spanY The number of vertical cells that the item spans
1087 * @param result The estimated drop cell X and Y.
1088 */
1089 void estimateDropCell(int originX, int originY, int spanX, int spanY, int[] result) {
Adam Cohend22015c2010-07-26 22:02:18 -07001090 final int countX = mCountX;
1091 final int countY = mCountY;
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001092
Michael Jurkaa63c4522010-08-19 13:52:27 -07001093 // pointToCellRounded takes the top left of a cell but will pad that with
1094 // cellWidth/2 and cellHeight/2 when finding the matching cell
1095 pointToCellRounded(originX, originY, result);
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001096
1097 // If the item isn't fully on this screen, snap to the edges
1098 int rightOverhang = result[0] + spanX - countX;
1099 if (rightOverhang > 0) {
1100 result[0] -= rightOverhang; // Snap to right
1101 }
1102 result[0] = Math.max(0, result[0]); // Snap to left
1103 int bottomOverhang = result[1] + spanY - countY;
1104 if (bottomOverhang > 0) {
1105 result[1] -= bottomOverhang; // Snap to bottom
1106 }
1107 result[1] = Math.max(0, result[1]); // Snap to top
1108 }
1109
Joe Onorato4be866d2010-10-10 11:26:02 -07001110 void visualizeDropLocation(
1111 View v, Bitmap dragOutline, int originX, int originY, int spanX, int spanY) {
1112
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -07001113 final int oldDragCellX = mDragCell[0];
1114 final int oldDragCellY = mDragCell[1];
Joe Onorato4be866d2010-10-10 11:26:02 -07001115 final int[] nearest = findNearestVacantArea(originX, originY, spanX, spanY, v, mDragCell);
Winson Chunga9abd0e2010-10-27 17:18:37 -07001116 if (v != null) {
1117 mDragCenter.set(originX + (v.getWidth() / 2), originY + (v.getHeight() / 2));
1118 } else {
1119 mDragCenter.set(originX, originY);
1120 }
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001121
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -07001122 if (nearest != null && (nearest[0] != oldDragCellX || nearest[1] != oldDragCellY)) {
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001123 // Find the top left corner of the rect the object will occupy
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001124 final int[] topLeft = mTmpPoint;
1125 cellToPoint(nearest[0], nearest[1], topLeft);
1126
Joe Onorato4be866d2010-10-10 11:26:02 -07001127 int left = topLeft[0];
1128 int top = topLeft[1];
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001129
Winson Chunga9abd0e2010-10-27 17:18:37 -07001130 if (v != null) {
1131 if (v.getParent() instanceof CellLayout) {
1132 LayoutParams lp = (LayoutParams) v.getLayoutParams();
1133 left += lp.leftMargin;
1134 top += lp.topMargin;
1135 }
Winson Chung150fbab2010-09-29 17:14:26 -07001136
Winson Chunga9abd0e2010-10-27 17:18:37 -07001137 // Offsets due to the size difference between the View and the dragOutline
1138 left += (v.getWidth() - dragOutline.getWidth()) / 2;
1139 top += (v.getHeight() - dragOutline.getHeight()) / 2;
1140 }
Winson Chung150fbab2010-09-29 17:14:26 -07001141
Joe Onorato4be866d2010-10-10 11:26:02 -07001142 final int oldIndex = mDragOutlineCurrent;
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -07001143 mDragOutlineAnims[oldIndex].animateOut();
1144 mDragOutlineCurrent = (oldIndex + 1) % mDragOutlines.length;
Winson Chung150fbab2010-09-29 17:14:26 -07001145
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -07001146 mDragOutlines[mDragOutlineCurrent].set(left, top);
1147 mDragOutlineAnims[mDragOutlineCurrent].setTag(dragOutline);
1148 mDragOutlineAnims[mDragOutlineCurrent].animateIn();
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001149 }
Patrick Dubroy49250ad2010-10-08 15:33:52 -07001150
1151 // If we are drawing crosshairs, the entire CellLayout needs to be invalidated
1152 if (mCrosshairsDrawable != null) {
1153 invalidate();
1154 }
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001155 }
1156
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001157 /**
Jeff Sharkey70864282009-04-07 21:08:40 -07001158 * Find a vacant area that will fit the given bounds nearest the requested
1159 * cell location. Uses Euclidean distance to score multiple vacant areas.
Winson Chungaafa03c2010-06-11 17:34:16 -07001160 *
Romain Guy51afc022009-05-04 18:03:43 -07001161 * @param pixelX The X location at which you want to search for a vacant area.
1162 * @param pixelY The Y location at which you want to search for a vacant area.
Jeff Sharkey70864282009-04-07 21:08:40 -07001163 * @param spanX Horizontal span of the object.
1164 * @param spanY Vertical span of the object.
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001165 * @param result Array in which to place the result, or null (in which case a new array will
1166 * be allocated)
Jeff Sharkey70864282009-04-07 21:08:40 -07001167 * @return The X, Y cell of a vacant area that can contain this object,
1168 * nearest the requested location.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001169 */
Michael Jurka6a1435d2010-09-27 17:35:12 -07001170 int[] findNearestVacantArea(
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001171 int pixelX, int pixelY, int spanX, int spanY, int[] result) {
1172 return findNearestVacantArea(pixelX, pixelY, spanX, spanY, null, result);
Michael Jurka6a1435d2010-09-27 17:35:12 -07001173 }
Winson Chungaafa03c2010-06-11 17:34:16 -07001174
Michael Jurka6a1435d2010-09-27 17:35:12 -07001175 /**
1176 * Find a vacant area that will fit the given bounds nearest the requested
1177 * cell location. Uses Euclidean distance to score multiple vacant areas.
1178 *
1179 * @param pixelX The X location at which you want to search for a vacant area.
1180 * @param pixelY The Y location at which you want to search for a vacant area.
1181 * @param spanX Horizontal span of the object.
1182 * @param spanY Vertical span of the object.
Michael Jurka6a1435d2010-09-27 17:35:12 -07001183 * @param ignoreView Considers space occupied by this view as unoccupied
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001184 * @param result Previously returned value to possibly recycle.
Michael Jurka6a1435d2010-09-27 17:35:12 -07001185 * @return The X, Y cell of a vacant area that can contain this object,
1186 * nearest the requested location.
1187 */
1188 int[] findNearestVacantArea(
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001189 int pixelX, int pixelY, int spanX, int spanY, View ignoreView, int[] result) {
Michael Jurkac6ee42e2010-09-30 12:04:50 -07001190 // mark space take by ignoreView as available (method checks if ignoreView is null)
1191 markCellsAsUnoccupiedForView(ignoreView);
1192
Jeff Sharkey70864282009-04-07 21:08:40 -07001193 // Keep track of best-scoring drop area
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001194 final int[] bestXY = result != null ? result : new int[2];
Jeff Sharkey70864282009-04-07 21:08:40 -07001195 double bestDistance = Double.MAX_VALUE;
Winson Chungaafa03c2010-06-11 17:34:16 -07001196
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001197 final int countX = mCountX;
1198 final int countY = mCountY;
1199 final boolean[][] occupied = mOccupied;
1200
Winson Chungbbc60d82010-11-11 16:34:41 -08001201 for (int y = 0; y < countY - (spanY - 1); y++) {
Michael Jurkac28de512010-08-13 11:27:44 -07001202 inner:
Winson Chungbbc60d82010-11-11 16:34:41 -08001203 for (int x = 0; x < countX - (spanX - 1); x++) {
Michael Jurkac28de512010-08-13 11:27:44 -07001204 for (int i = 0; i < spanX; i++) {
1205 for (int j = 0; j < spanY; j++) {
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001206 if (occupied[x + i][y + j]) {
Winson Chungbbc60d82010-11-11 16:34:41 -08001207 // small optimization: we can skip to after the column we just found
Michael Jurkac28de512010-08-13 11:27:44 -07001208 // an occupied cell
Winson Chungbbc60d82010-11-11 16:34:41 -08001209 x += i;
Michael Jurkac28de512010-08-13 11:27:44 -07001210 continue inner;
1211 }
1212 }
1213 }
1214 final int[] cellXY = mTmpCellXY;
1215 cellToPoint(x, y, cellXY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001216
Michael Jurkac28de512010-08-13 11:27:44 -07001217 double distance = Math.sqrt(Math.pow(cellXY[0] - pixelX, 2)
1218 + Math.pow(cellXY[1] - pixelY, 2));
1219 if (distance <= bestDistance) {
1220 bestDistance = distance;
1221 bestXY[0] = x;
1222 bestXY[1] = y;
1223 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001224 }
1225 }
Michael Jurkac6ee42e2010-09-30 12:04:50 -07001226 // re-mark space taken by ignoreView as occupied
1227 markCellsAsOccupiedForView(ignoreView);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001228
Winson Chungaafa03c2010-06-11 17:34:16 -07001229 // Return null if no suitable location found
Jeff Sharkey70864282009-04-07 21:08:40 -07001230 if (bestDistance < Double.MAX_VALUE) {
1231 return bestXY;
1232 } else {
1233 return null;
1234 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001235 }
Winson Chungaafa03c2010-06-11 17:34:16 -07001236
Michael Jurka0280c3b2010-09-17 15:00:07 -07001237 boolean existsEmptyCell() {
1238 return findCellForSpan(null, 1, 1);
1239 }
1240
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001241 /**
Michael Jurka0280c3b2010-09-17 15:00:07 -07001242 * Finds the upper-left coordinate of the first rectangle in the grid that can
1243 * hold a cell of the specified dimensions. If intersectX and intersectY are not -1,
1244 * then this method will only return coordinates for rectangles that contain the cell
1245 * (intersectX, intersectY)
1246 *
1247 * @param cellXY The array that will contain the position of a vacant cell if such a cell
1248 * can be found.
1249 * @param spanX The horizontal span of the cell we want to find.
1250 * @param spanY The vertical span of the cell we want to find.
1251 *
1252 * @return True if a vacant cell of the specified dimension was found, false otherwise.
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001253 */
Michael Jurka0280c3b2010-09-17 15:00:07 -07001254 boolean findCellForSpan(int[] cellXY, int spanX, int spanY) {
1255 return findCellForSpanThatIntersectsIgnoring(cellXY, spanX, spanY, -1, -1, null);
1256 }
1257
1258 /**
1259 * Like above, but ignores any cells occupied by the item "ignoreView"
1260 *
1261 * @param cellXY The array that will contain the position of a vacant cell if such a cell
1262 * can be found.
1263 * @param spanX The horizontal span of the cell we want to find.
1264 * @param spanY The vertical span of the cell we want to find.
1265 * @param ignoreView The home screen item we should treat as not occupying any space
1266 * @return
1267 */
1268 boolean findCellForSpanIgnoring(int[] cellXY, int spanX, int spanY, View ignoreView) {
1269 return findCellForSpanThatIntersectsIgnoring(cellXY, spanX, spanY, -1, -1, ignoreView);
1270 }
1271
1272 /**
1273 * Like above, but if intersectX and intersectY are not -1, then this method will try to
1274 * return coordinates for rectangles that contain the cell [intersectX, intersectY]
1275 *
1276 * @param spanX The horizontal span of the cell we want to find.
1277 * @param spanY The vertical span of the cell we want to find.
1278 * @param ignoreView The home screen item we should treat as not occupying any space
1279 * @param intersectX The X coordinate of the cell that we should try to overlap
1280 * @param intersectX The Y coordinate of the cell that we should try to overlap
1281 *
1282 * @return True if a vacant cell of the specified dimension was found, false otherwise.
1283 */
1284 boolean findCellForSpanThatIntersects(int[] cellXY, int spanX, int spanY,
1285 int intersectX, int intersectY) {
1286 return findCellForSpanThatIntersectsIgnoring(
1287 cellXY, spanX, spanY, intersectX, intersectY, null);
1288 }
1289
1290 /**
1291 * The superset of the above two methods
1292 */
1293 boolean findCellForSpanThatIntersectsIgnoring(int[] cellXY, int spanX, int spanY,
1294 int intersectX, int intersectY, View ignoreView) {
Michael Jurkac6ee42e2010-09-30 12:04:50 -07001295 // mark space take by ignoreView as available (method checks if ignoreView is null)
1296 markCellsAsUnoccupiedForView(ignoreView);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001297
Michael Jurka28750fb2010-09-24 17:43:49 -07001298 boolean foundCell = false;
Michael Jurka0280c3b2010-09-17 15:00:07 -07001299 while (true) {
1300 int startX = 0;
1301 if (intersectX >= 0) {
1302 startX = Math.max(startX, intersectX - (spanX - 1));
1303 }
1304 int endX = mCountX - (spanX - 1);
1305 if (intersectX >= 0) {
1306 endX = Math.min(endX, intersectX + (spanX - 1) + (spanX == 1 ? 1 : 0));
1307 }
1308 int startY = 0;
1309 if (intersectY >= 0) {
1310 startY = Math.max(startY, intersectY - (spanY - 1));
1311 }
1312 int endY = mCountY - (spanY - 1);
1313 if (intersectY >= 0) {
1314 endY = Math.min(endY, intersectY + (spanY - 1) + (spanY == 1 ? 1 : 0));
1315 }
1316
Winson Chungbbc60d82010-11-11 16:34:41 -08001317 for (int y = startY; y < endY && !foundCell; y++) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001318 inner:
Winson Chungbbc60d82010-11-11 16:34:41 -08001319 for (int x = startX; x < endX; x++) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001320 for (int i = 0; i < spanX; i++) {
1321 for (int j = 0; j < spanY; j++) {
1322 if (mOccupied[x + i][y + j]) {
Winson Chungbbc60d82010-11-11 16:34:41 -08001323 // small optimization: we can skip to after the column we just found
Michael Jurka0280c3b2010-09-17 15:00:07 -07001324 // an occupied cell
Winson Chungbbc60d82010-11-11 16:34:41 -08001325 x += i;
Michael Jurka0280c3b2010-09-17 15:00:07 -07001326 continue inner;
1327 }
1328 }
1329 }
1330 if (cellXY != null) {
1331 cellXY[0] = x;
1332 cellXY[1] = y;
1333 }
Michael Jurka28750fb2010-09-24 17:43:49 -07001334 foundCell = true;
1335 break;
Michael Jurka0280c3b2010-09-17 15:00:07 -07001336 }
1337 }
1338 if (intersectX == -1 && intersectY == -1) {
1339 break;
1340 } else {
1341 // if we failed to find anything, try again but without any requirements of
1342 // intersecting
1343 intersectX = -1;
1344 intersectY = -1;
1345 continue;
1346 }
1347 }
1348
Michael Jurkac6ee42e2010-09-30 12:04:50 -07001349 // re-mark space taken by ignoreView as occupied
1350 markCellsAsOccupiedForView(ignoreView);
Michael Jurka28750fb2010-09-24 17:43:49 -07001351 return foundCell;
Michael Jurka0280c3b2010-09-17 15:00:07 -07001352 }
1353
1354 /**
1355 * Called when drag has left this CellLayout or has been completed (successfully or not)
1356 */
1357 void onDragExit() {
Joe Onorato4be866d2010-10-10 11:26:02 -07001358 // This can actually be called when we aren't in a drag, e.g. when adding a new
1359 // item to this layout via the customize drawer.
1360 // Guard against that case.
1361 if (mDragging) {
1362 mDragging = false;
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001363
Joe Onorato4be866d2010-10-10 11:26:02 -07001364 // Fade out the drag indicators
1365 if (mCrosshairsAnimator != null) {
1366 mCrosshairsAnimator.animateOut();
1367 }
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001368 }
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -07001369
1370 // Invalidate the drag data
1371 mDragCell[0] = -1;
1372 mDragCell[1] = -1;
1373 mDragOutlineAnims[mDragOutlineCurrent].animateOut();
1374 mDragOutlineCurrent = (mDragOutlineCurrent + 1) % mDragOutlineAnims.length;
1375
Michael Jurka33945b22010-12-21 18:19:38 -08001376 setIsDragOverlapping(false);
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001377 }
1378
1379 /**
Winson Chungaafa03c2010-06-11 17:34:16 -07001380 * Mark a child as having been dropped.
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001381 * At the beginning of the drag operation, the child may have been on another
Patrick Dubroyce34a972010-10-19 10:34:32 -07001382 * screen, but it is re-parented before this method is called.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001383 *
1384 * @param child The child that is being dropped
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001385 */
Michael Jurkad3ef3062010-11-23 16:23:58 -08001386 void onDropChild(View child, boolean animate) {
Romain Guyd94533d2009-08-17 10:01:15 -07001387 if (child != null) {
1388 LayoutParams lp = (LayoutParams) child.getLayoutParams();
Romain Guyd94533d2009-08-17 10:01:15 -07001389 lp.isDragging = false;
Romain Guy84f296c2009-11-04 15:00:44 -08001390 lp.dropped = true;
Michael Jurkad3ef3062010-11-23 16:23:58 -08001391 lp.animateDrop = animate;
Adam Cohendf9de0b2011-01-10 11:01:49 -08001392 child.setVisibility(View.INVISIBLE);
Romain Guyd94533d2009-08-17 10:01:15 -07001393 child.requestLayout();
Romain Guyd94533d2009-08-17 10:01:15 -07001394 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001395 }
1396
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001397 /**
1398 * Start dragging the specified child
Winson Chungaafa03c2010-06-11 17:34:16 -07001399 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001400 * @param child The child that is being dragged
1401 */
1402 void onDragChild(View child) {
1403 LayoutParams lp = (LayoutParams) child.getLayoutParams();
1404 lp.isDragging = true;
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001405 }
1406
1407 /**
1408 * A drag event has begun over this layout.
1409 * It may have begun over this layout (in which case onDragChild is called first),
1410 * or it may have begun on another layout.
1411 */
Winson Chunga9abd0e2010-10-27 17:18:37 -07001412 void onDragEnter() {
Patrick Dubroyfe6bd872010-10-13 17:32:10 -07001413 if (!mDragging) {
Patrick Dubroyfe6bd872010-10-13 17:32:10 -07001414 // Fade in the drag indicators
1415 if (mCrosshairsAnimator != null) {
1416 mCrosshairsAnimator.animateIn();
1417 }
Joe Onorato4be866d2010-10-10 11:26:02 -07001418 }
1419 mDragging = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001420 }
Winson Chungaafa03c2010-06-11 17:34:16 -07001421
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001422 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001423 * Computes a bounding rectangle for a range of cells
Winson Chungaafa03c2010-06-11 17:34:16 -07001424 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001425 * @param cellX X coordinate of upper left corner expressed as a cell position
1426 * @param cellY Y coordinate of upper left corner expressed as a cell position
Winson Chungaafa03c2010-06-11 17:34:16 -07001427 * @param cellHSpan Width in cells
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001428 * @param cellVSpan Height in cells
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001429 * @param resultRect Rect into which to put the results
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001430 */
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001431 public void cellToRect(int cellX, int cellY, int cellHSpan, int cellVSpan, RectF resultRect) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001432 final int cellWidth = mCellWidth;
1433 final int cellHeight = mCellHeight;
1434 final int widthGap = mWidthGap;
1435 final int heightGap = mHeightGap;
Winson Chungaafa03c2010-06-11 17:34:16 -07001436
1437 final int hStartPadding = getLeftPadding();
1438 final int vStartPadding = getTopPadding();
1439
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001440 int width = cellHSpan * cellWidth + ((cellHSpan - 1) * widthGap);
1441 int height = cellVSpan * cellHeight + ((cellVSpan - 1) * heightGap);
1442
1443 int x = hStartPadding + cellX * (cellWidth + widthGap);
1444 int y = vStartPadding + cellY * (cellHeight + heightGap);
Winson Chungaafa03c2010-06-11 17:34:16 -07001445
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001446 resultRect.set(x, y, x + width, y + height);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001447 }
Winson Chungaafa03c2010-06-11 17:34:16 -07001448
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001449 /**
Winson Chungaafa03c2010-06-11 17:34:16 -07001450 * Computes the required horizontal and vertical cell spans to always
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001451 * fit the given rectangle.
Winson Chungaafa03c2010-06-11 17:34:16 -07001452 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001453 * @param width Width in pixels
1454 * @param height Height in pixels
Patrick Dubroy8f86ddc2010-07-16 13:55:32 -07001455 * @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 -08001456 */
Patrick Dubroy8f86ddc2010-07-16 13:55:32 -07001457 public int[] rectToCell(int width, int height, int[] result) {
Michael Jurka9987a5c2010-10-08 16:58:12 -07001458 return rectToCell(getResources(), width, height, result);
1459 }
1460
1461 public static int[] rectToCell(Resources resources, int width, int height, int[] result) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001462 // Always assume we're working with the smallest span to make sure we
1463 // reserve enough space in both orientations.
Joe Onorato79e56262009-09-21 15:23:04 -04001464 int actualWidth = resources.getDimensionPixelSize(R.dimen.workspace_cell_width);
1465 int actualHeight = resources.getDimensionPixelSize(R.dimen.workspace_cell_height);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001466 int smallerSize = Math.min(actualWidth, actualHeight);
Joe Onorato79e56262009-09-21 15:23:04 -04001467
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001468 // Always round up to next largest cell
1469 int spanX = (width + smallerSize) / smallerSize;
1470 int spanY = (height + smallerSize) / smallerSize;
Joe Onorato79e56262009-09-21 15:23:04 -04001471
Patrick Dubroy8f86ddc2010-07-16 13:55:32 -07001472 if (result == null) {
1473 return new int[] { spanX, spanY };
1474 }
1475 result[0] = spanX;
1476 result[1] = spanY;
1477 return result;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001478 }
1479
1480 /**
Patrick Dubroy047379a2010-12-19 22:02:04 -08001481 * Calculate the grid spans needed to fit given item
1482 */
1483 public void calculateSpans(ItemInfo info) {
1484 final int minWidth;
1485 final int minHeight;
1486
1487 if (info instanceof LauncherAppWidgetInfo) {
1488 minWidth = ((LauncherAppWidgetInfo) info).minWidth;
1489 minHeight = ((LauncherAppWidgetInfo) info).minHeight;
1490 } else if (info instanceof PendingAddWidgetInfo) {
1491 minWidth = ((PendingAddWidgetInfo) info).minWidth;
1492 minHeight = ((PendingAddWidgetInfo) info).minHeight;
1493 } else {
1494 // It's not a widget, so it must be 1x1
1495 info.spanX = info.spanY = 1;
1496 return;
1497 }
1498 int[] spans = rectToCell(minWidth, minHeight, null);
1499 info.spanX = spans[0];
1500 info.spanY = spans[1];
1501 }
1502
1503 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001504 * Find the first vacant cell, if there is one.
1505 *
1506 * @param vacant Holds the x and y coordinate of the vacant cell
1507 * @param spanX Horizontal cell span.
1508 * @param spanY Vertical cell span.
Winson Chungaafa03c2010-06-11 17:34:16 -07001509 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001510 * @return True if a vacant cell was found
1511 */
1512 public boolean getVacantCell(int[] vacant, int spanX, int spanY) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001513
Michael Jurka0280c3b2010-09-17 15:00:07 -07001514 return findVacantCell(vacant, spanX, spanY, mCountX, mCountY, mOccupied);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001515 }
1516
1517 static boolean findVacantCell(int[] vacant, int spanX, int spanY,
1518 int xCount, int yCount, boolean[][] occupied) {
1519
1520 for (int x = 0; x < xCount; x++) {
1521 for (int y = 0; y < yCount; y++) {
1522 boolean available = !occupied[x][y];
1523out: for (int i = x; i < x + spanX - 1 && x < xCount; i++) {
1524 for (int j = y; j < y + spanY - 1 && y < yCount; j++) {
1525 available = available && !occupied[i][j];
1526 if (!available) break out;
1527 }
1528 }
1529
1530 if (available) {
1531 vacant[0] = x;
1532 vacant[1] = y;
1533 return true;
1534 }
1535 }
1536 }
1537
1538 return false;
1539 }
1540
Michael Jurka0280c3b2010-09-17 15:00:07 -07001541 private void clearOccupiedCells() {
1542 for (int x = 0; x < mCountX; x++) {
1543 for (int y = 0; y < mCountY; y++) {
1544 mOccupied[x][y] = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001545 }
1546 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001547 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001548
Michael Jurka0280c3b2010-09-17 15:00:07 -07001549 public void onMove(View view, int newCellX, int newCellY) {
1550 LayoutParams lp = (LayoutParams) view.getLayoutParams();
1551 markCellsAsUnoccupiedForView(view);
1552 markCellsForView(newCellX, newCellY, lp.cellHSpan, lp.cellVSpan, true);
1553 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001554
Michael Jurka0280c3b2010-09-17 15:00:07 -07001555 private void markCellsAsOccupiedForView(View view) {
Michael Jurkac6ee42e2010-09-30 12:04:50 -07001556 if (view == null || view.getParent() != this) return;
Michael Jurka0280c3b2010-09-17 15:00:07 -07001557 LayoutParams lp = (LayoutParams) view.getLayoutParams();
1558 markCellsForView(lp.cellX, lp.cellY, lp.cellHSpan, lp.cellVSpan, true);
1559 }
1560
1561 private void markCellsAsUnoccupiedForView(View view) {
Michael Jurkac6ee42e2010-09-30 12:04:50 -07001562 if (view == null || view.getParent() != this) return;
Michael Jurka0280c3b2010-09-17 15:00:07 -07001563 LayoutParams lp = (LayoutParams) view.getLayoutParams();
1564 markCellsForView(lp.cellX, lp.cellY, lp.cellHSpan, lp.cellVSpan, false);
1565 }
1566
1567 private void markCellsForView(int cellX, int cellY, int spanX, int spanY, boolean value) {
1568 for (int x = cellX; x < cellX + spanX && x < mCountX; x++) {
1569 for (int y = cellY; y < cellY + spanY && y < mCountY; y++) {
1570 mOccupied[x][y] = value;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001571 }
1572 }
1573 }
1574
1575 @Override
1576 public ViewGroup.LayoutParams generateLayoutParams(AttributeSet attrs) {
1577 return new CellLayout.LayoutParams(getContext(), attrs);
1578 }
1579
1580 @Override
1581 protected boolean checkLayoutParams(ViewGroup.LayoutParams p) {
1582 return p instanceof CellLayout.LayoutParams;
1583 }
1584
1585 @Override
1586 protected ViewGroup.LayoutParams generateLayoutParams(ViewGroup.LayoutParams p) {
1587 return new CellLayout.LayoutParams(p);
1588 }
1589
Winson Chungaafa03c2010-06-11 17:34:16 -07001590 public static class CellLayoutAnimationController extends LayoutAnimationController {
1591 public CellLayoutAnimationController(Animation animation, float delay) {
1592 super(animation, delay);
1593 }
1594
1595 @Override
1596 protected long getDelayForView(View view) {
1597 return (int) (Math.random() * 150);
1598 }
1599 }
1600
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001601 public static class LayoutParams extends ViewGroup.MarginLayoutParams {
1602 /**
1603 * Horizontal location of the item in the grid.
1604 */
1605 @ViewDebug.ExportedProperty
1606 public int cellX;
1607
1608 /**
1609 * Vertical location of the item in the grid.
1610 */
1611 @ViewDebug.ExportedProperty
1612 public int cellY;
1613
1614 /**
1615 * Number of cells spanned horizontally by the item.
1616 */
1617 @ViewDebug.ExportedProperty
1618 public int cellHSpan;
1619
1620 /**
1621 * Number of cells spanned vertically by the item.
1622 */
1623 @ViewDebug.ExportedProperty
1624 public int cellVSpan;
Winson Chungaafa03c2010-06-11 17:34:16 -07001625
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001626 /**
1627 * Is this item currently being dragged
1628 */
1629 public boolean isDragging;
1630
1631 // X coordinate of the view in the layout.
1632 @ViewDebug.ExportedProperty
1633 int x;
1634 // Y coordinate of the view in the layout.
1635 @ViewDebug.ExportedProperty
1636 int y;
1637
Patrick Dubroyce34a972010-10-19 10:34:32 -07001638 /**
1639 * The old X coordinate of this item, relative to its current parent.
1640 * Used to animate the item into its new position.
1641 */
1642 int oldX;
1643
1644 /**
1645 * The old Y coordinate of this item, relative to its current parent.
1646 * Used to animate the item into its new position.
1647 */
1648 int oldY;
1649
Romain Guy84f296c2009-11-04 15:00:44 -08001650 boolean dropped;
Romain Guyfcb9e712009-10-02 16:06:52 -07001651
Michael Jurkad3ef3062010-11-23 16:23:58 -08001652 boolean animateDrop;
1653
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001654 public LayoutParams(Context c, AttributeSet attrs) {
1655 super(c, attrs);
1656 cellHSpan = 1;
1657 cellVSpan = 1;
1658 }
1659
1660 public LayoutParams(ViewGroup.LayoutParams source) {
1661 super(source);
1662 cellHSpan = 1;
1663 cellVSpan = 1;
1664 }
Winson Chungaafa03c2010-06-11 17:34:16 -07001665
1666 public LayoutParams(LayoutParams source) {
1667 super(source);
1668 this.cellX = source.cellX;
1669 this.cellY = source.cellY;
1670 this.cellHSpan = source.cellHSpan;
1671 this.cellVSpan = source.cellVSpan;
1672 }
1673
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001674 public LayoutParams(int cellX, int cellY, int cellHSpan, int cellVSpan) {
Romain Guy8f19cdd2010-01-08 15:07:00 -08001675 super(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001676 this.cellX = cellX;
1677 this.cellY = cellY;
1678 this.cellHSpan = cellHSpan;
1679 this.cellVSpan = cellVSpan;
1680 }
1681
1682 public void setup(int cellWidth, int cellHeight, int widthGap, int heightGap,
1683 int hStartPadding, int vStartPadding) {
Winson Chungaafa03c2010-06-11 17:34:16 -07001684
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001685 final int myCellHSpan = cellHSpan;
1686 final int myCellVSpan = cellVSpan;
1687 final int myCellX = cellX;
1688 final int myCellY = cellY;
Winson Chungaafa03c2010-06-11 17:34:16 -07001689
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001690 width = myCellHSpan * cellWidth + ((myCellHSpan - 1) * widthGap) -
1691 leftMargin - rightMargin;
1692 height = myCellVSpan * cellHeight + ((myCellVSpan - 1) * heightGap) -
1693 topMargin - bottomMargin;
1694
1695 x = hStartPadding + myCellX * (cellWidth + widthGap) + leftMargin;
1696 y = vStartPadding + myCellY * (cellHeight + heightGap) + topMargin;
1697 }
Winson Chungaafa03c2010-06-11 17:34:16 -07001698
1699 public String toString() {
1700 return "(" + this.cellX + ", " + this.cellY + ")";
1701 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001702 }
1703
Michael Jurka0280c3b2010-09-17 15:00:07 -07001704 // This class stores info for two purposes:
1705 // 1. When dragging items (mDragInfo in Workspace), we store the View, its cellX & cellY,
1706 // its spanX, spanY, and the screen it is on
1707 // 2. When long clicking on an empty cell in a CellLayout, we save information about the
1708 // cellX and cellY coordinates and which page was clicked. We then set this as a tag on
1709 // the CellLayout that was long clicked
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001710 static final class CellInfo implements ContextMenu.ContextMenuInfo {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001711 View cell;
Michael Jurkaa63c4522010-08-19 13:52:27 -07001712 int cellX = -1;
1713 int cellY = -1;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001714 int spanX;
1715 int spanY;
1716 int screen;
1717 boolean valid;
1718
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001719 @Override
1720 public String toString() {
Winson Chungaafa03c2010-06-11 17:34:16 -07001721 return "Cell[view=" + (cell == null ? "null" : cell.getClass())
1722 + ", x=" + cellX + ", y=" + cellY + "]";
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001723 }
1724 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001725}
Michael Jurka99b6a5b2011-01-07 15:37:17 -08001726
1727// Custom interfaces used to listen to "visibility changed" events of *children* of Views. Avoided
1728// using "onVisibilityChanged" in the names because there's a method of that name in framework
1729// (which can only can be used to listen to ancestors' "visibility changed" events)
1730interface VisibilityChangedBroadcaster {
1731 public void setVisibilityChangedListener(VisibilityChangedListener listener);
1732}
1733
1734interface VisibilityChangedListener {
1735 public void receiveVisibilityChangedMessage(View v);
1736}