blob: 70fe8f98755a25d77fd7df4f71da934e6a673aad [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;
Patrick Dubroy6569f2c2010-07-12 14:25:18 -070040import android.graphics.drawable.Drawable;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080041import android.util.AttributeSet;
Joe Onorato4be866d2010-10-10 11:26:02 -070042import android.util.Log;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080043import android.view.ContextMenu;
44import android.view.MotionEvent;
45import android.view.View;
46import android.view.ViewDebug;
47import android.view.ViewGroup;
Winson Chungaafa03c2010-06-11 17:34:16 -070048import android.view.animation.Animation;
Winson Chung150fbab2010-09-29 17:14:26 -070049import android.view.animation.DecelerateInterpolator;
Winson Chungaafa03c2010-06-11 17:34:16 -070050import android.view.animation.LayoutAnimationController;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080051
Michael Jurka3c4c20f2010-10-28 15:36:06 -070052import java.util.Arrays;
Patrick Dubroy8e58e912010-10-14 13:21:48 -070053
Adam Cohenf34bab52010-09-30 14:11:56 -070054public class CellLayout extends ViewGroup implements Dimmable {
Winson Chungaafa03c2010-06-11 17:34:16 -070055 static final String TAG = "CellLayout";
56
The Android Open Source Project31dd5032009-03-03 19:32:27 -080057 private int mCellWidth;
58 private int mCellHeight;
Winson Chungaafa03c2010-06-11 17:34:16 -070059
Winson Chungaafa03c2010-06-11 17:34:16 -070060 private int mLeftPadding;
61 private int mRightPadding;
62 private int mTopPadding;
63 private int mBottomPadding;
64
Adam Cohend22015c2010-07-26 22:02:18 -070065 private int mCountX;
66 private int mCountY;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080067
68 private int mWidthGap;
69 private int mHeightGap;
70
71 private final Rect mRect = new Rect();
72 private final CellInfo mCellInfo = new CellInfo();
Winson Chungaafa03c2010-06-11 17:34:16 -070073
Patrick Dubroyde7658b2010-09-27 11:15:43 -070074 // These are temporary variables to prevent having to allocate a new object just to
75 // return an (x, y) value from helper functions. Do NOT use them to maintain other state.
Patrick Dubroy6569f2c2010-07-12 14:25:18 -070076 private final int[] mTmpCellXY = new int[2];
Patrick Dubroyde7658b2010-09-27 11:15:43 -070077 private final int[] mTmpPoint = new int[2];
78 private final PointF mTmpPointF = new PointF();
Patrick Dubroy6569f2c2010-07-12 14:25:18 -070079
The Android Open Source Project31dd5032009-03-03 19:32:27 -080080 boolean[][] mOccupied;
81
Michael Jurkadee05892010-07-27 10:01:56 -070082 private OnTouchListener mInterceptTouchListener;
83
Michael Jurka5f1c5092010-09-03 14:15:02 -070084 private float mBackgroundAlpha;
Adam Cohen1b0aaac2010-10-28 11:11:18 -070085 private float mBackgroundAlphaMultiplier = 1.0f;
Adam Cohenf34bab52010-09-30 14:11:56 -070086
Michael Jurka33945b22010-12-21 18:19:38 -080087 private Drawable mNormalBackground;
88 private Drawable mNormalGlowBackground;
89 private Drawable mActiveBackground;
90 private Drawable mActiveGlowBackground;
91 private Drawable mNormalBackgroundMini;
92 private Drawable mNormalGlowBackgroundMini;
93 private Drawable mActiveBackgroundMini;
94 private Drawable mActiveGlowBackgroundMini;
Michael Jurka18014792010-10-14 09:01:34 -070095 private Rect mBackgroundRect;
Michael Jurka33945b22010-12-21 18:19:38 -080096 private Rect mGlowBackgroundRect;
97 private float mGlowBackgroundScale;
98 private float mGlowBackgroundAlpha;
Patrick Dubroy1262e362010-10-06 15:49:50 -070099
Michael Jurka33945b22010-12-21 18:19:38 -0800100 private boolean mAcceptsDrops = false;
101 // If we're actively dragging something over this screen, mIsDragOverlapping is true
102 private boolean mIsDragOverlapping = false;
103 private boolean mIsDragOccuring = false;
104 private boolean mIsDefaultDropTarget = false;
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700105 private final Point mDragCenter = new Point();
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700106
Winson Chung150fbab2010-09-29 17:14:26 -0700107 // These arrays are used to implement the drag visualization on x-large screens.
Joe Onorato4be866d2010-10-10 11:26:02 -0700108 // They are used as circular arrays, indexed by mDragOutlineCurrent.
109 private Point[] mDragOutlines = new Point[8];
Chet Haase472b2812010-10-14 07:02:04 -0700110 private float[] mDragOutlineAlphas = new float[mDragOutlines.length];
Joe Onorato4be866d2010-10-10 11:26:02 -0700111 private InterruptibleInOutAnimator[] mDragOutlineAnims =
112 new InterruptibleInOutAnimator[mDragOutlines.length];
Winson Chung150fbab2010-09-29 17:14:26 -0700113
114 // Used as an index into the above 3 arrays; indicates which is the most current value.
Joe Onorato4be866d2010-10-10 11:26:02 -0700115 private int mDragOutlineCurrent = 0;
Patrick Dubroy8e58e912010-10-14 13:21:48 -0700116 private final Paint mDragOutlinePaint = new Paint();
Winson Chung150fbab2010-09-29 17:14:26 -0700117
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700118 private Drawable mCrosshairsDrawable = null;
Patrick Dubroy49250ad2010-10-08 15:33:52 -0700119 private InterruptibleInOutAnimator mCrosshairsAnimator = null;
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700120 private float mCrosshairsVisibility = 0.0f;
121
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700122 // When a drag operation is in progress, holds the nearest cell to the touch point
123 private final int[] mDragCell = new int[2];
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800124
Winson Chungaafa03c2010-06-11 17:34:16 -0700125 private final WallpaperManager mWallpaperManager;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800126
Joe Onorato4be866d2010-10-10 11:26:02 -0700127 private boolean mDragging = false;
128
Patrick Dubroyce34a972010-10-19 10:34:32 -0700129 private TimeInterpolator mEaseOutInterpolator;
130
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800131 public CellLayout(Context context) {
132 this(context, null);
133 }
134
135 public CellLayout(Context context, AttributeSet attrs) {
136 this(context, attrs, 0);
137 }
138
139 public CellLayout(Context context, AttributeSet attrs, int defStyle) {
140 super(context, attrs, defStyle);
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700141
142 // A ViewGroup usually does not draw, but CellLayout needs to draw a rectangle to show
143 // the user where a dragged item will land when dropped.
144 setWillNotDraw(false);
Michael Jurkaa63c4522010-08-19 13:52:27 -0700145
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800146 TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.CellLayout, defStyle, 0);
147
148 mCellWidth = a.getDimensionPixelSize(R.styleable.CellLayout_cellWidth, 10);
149 mCellHeight = a.getDimensionPixelSize(R.styleable.CellLayout_cellHeight, 10);
Winson Chungece7f5b2010-10-22 14:54:12 -0700150 mWidthGap = a.getDimensionPixelSize(R.styleable.CellLayout_widthGap, -1);
151 mHeightGap = a.getDimensionPixelSize(R.styleable.CellLayout_heightGap, -1);
Winson Chungaafa03c2010-06-11 17:34:16 -0700152
Adam Cohend22015c2010-07-26 22:02:18 -0700153 mLeftPadding =
154 a.getDimensionPixelSize(R.styleable.CellLayout_xAxisStartPadding, 10);
155 mRightPadding =
156 a.getDimensionPixelSize(R.styleable.CellLayout_xAxisEndPadding, 10);
157 mTopPadding =
158 a.getDimensionPixelSize(R.styleable.CellLayout_yAxisStartPadding, 10);
159 mBottomPadding =
160 a.getDimensionPixelSize(R.styleable.CellLayout_yAxisEndPadding, 10);
Winson Chungaafa03c2010-06-11 17:34:16 -0700161
Adam Cohend22015c2010-07-26 22:02:18 -0700162 mCountX = LauncherModel.getCellCountX();
163 mCountY = LauncherModel.getCellCountY();
Michael Jurka0280c3b2010-09-17 15:00:07 -0700164 mOccupied = new boolean[mCountX][mCountY];
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800165
166 a.recycle();
167
168 setAlwaysDrawnWithCacheEnabled(false);
169
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700170 mWallpaperManager = WallpaperManager.getInstance(context);
171
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700172 final Resources res = getResources();
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700173
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700174 if (LauncherApplication.isScreenXLarge()) {
Michael Jurka33945b22010-12-21 18:19:38 -0800175 mNormalBackground = res.getDrawable(R.drawable.homescreen_large_blue);
176 mNormalGlowBackground = res.getDrawable(R.drawable.homescreen_large_blue_strong);
177 mActiveBackground = res.getDrawable(R.drawable.homescreen_large_green);
178 mActiveGlowBackground = res.getDrawable(R.drawable.homescreen_large_green_strong);
179
180 mNormalBackgroundMini = res.getDrawable(R.drawable.homescreen_small_blue);
181 mNormalGlowBackgroundMini = res.getDrawable(R.drawable.homescreen_small_blue_strong);
182 mActiveBackgroundMini = res.getDrawable(R.drawable.homescreen_small_green);
183 mActiveGlowBackgroundMini = res.getDrawable(R.drawable.homescreen_small_green_strong);
184
185 mNormalBackground.setFilterBitmap(true);
186 mNormalGlowBackground.setFilterBitmap(true);
187 mActiveBackground.setFilterBitmap(true);
188 mActiveGlowBackground.setFilterBitmap(true);
189 mNormalBackgroundMini.setFilterBitmap(true);
190 mNormalGlowBackgroundMini.setFilterBitmap(true);
191 mActiveBackgroundMini.setFilterBitmap(true);
192 mActiveGlowBackgroundMini.setFilterBitmap(true);
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700193 }
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700194
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700195 // Initialize the data structures used for the drag visualization.
Winson Chung150fbab2010-09-29 17:14:26 -0700196
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700197 mCrosshairsDrawable = res.getDrawable(R.drawable.gardening_crosshairs);
Patrick Dubroyce34a972010-10-19 10:34:32 -0700198 mEaseOutInterpolator = new DecelerateInterpolator(2.5f); // Quint ease out
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700199
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700200 // Set up the animation for fading the crosshairs in and out
201 int animDuration = res.getInteger(R.integer.config_crosshairsFadeInTime);
Patrick Dubroy49250ad2010-10-08 15:33:52 -0700202 mCrosshairsAnimator = new InterruptibleInOutAnimator(animDuration, 0.0f, 1.0f);
Chet Haase472b2812010-10-14 07:02:04 -0700203 mCrosshairsAnimator.getAnimator().addUpdateListener(new AnimatorUpdateListener() {
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700204 public void onAnimationUpdate(ValueAnimator animation) {
205 mCrosshairsVisibility = ((Float) animation.getAnimatedValue()).floatValue();
Patrick Dubroy8e58e912010-10-14 13:21:48 -0700206 invalidate();
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700207 }
208 });
Patrick Dubroyce34a972010-10-19 10:34:32 -0700209 mCrosshairsAnimator.getAnimator().setInterpolator(mEaseOutInterpolator);
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700210
Joe Onorato4be866d2010-10-10 11:26:02 -0700211 for (int i = 0; i < mDragOutlines.length; i++) {
212 mDragOutlines[i] = new Point(-1, -1);
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700213 }
214
215 // When dragging things around the home screens, we show a green outline of
216 // where the item will land. The outlines gradually fade out, leaving a trail
217 // behind the drag path.
218 // Set up all the animations that are used to implement this fading.
219 final int duration = res.getInteger(R.integer.config_dragOutlineFadeTime);
Chet Haase472b2812010-10-14 07:02:04 -0700220 final float fromAlphaValue = 0;
221 final float toAlphaValue = (float)res.getInteger(R.integer.config_dragOutlineMaxAlpha);
Joe Onorato4be866d2010-10-10 11:26:02 -0700222
Patrick Dubroy8e58e912010-10-14 13:21:48 -0700223 Arrays.fill(mDragOutlineAlphas, fromAlphaValue);
Joe Onorato4be866d2010-10-10 11:26:02 -0700224
225 for (int i = 0; i < mDragOutlineAnims.length; i++) {
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700226 final InterruptibleInOutAnimator anim =
227 new InterruptibleInOutAnimator(duration, fromAlphaValue, toAlphaValue);
Patrick Dubroyce34a972010-10-19 10:34:32 -0700228 anim.getAnimator().setInterpolator(mEaseOutInterpolator);
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700229 final int thisIndex = i;
Chet Haase472b2812010-10-14 07:02:04 -0700230 anim.getAnimator().addUpdateListener(new AnimatorUpdateListener() {
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700231 public void onAnimationUpdate(ValueAnimator animation) {
Joe Onorato4be866d2010-10-10 11:26:02 -0700232 final Bitmap outline = (Bitmap)anim.getTag();
233
234 // If an animation is started and then stopped very quickly, we can still
235 // get spurious updates we've cleared the tag. Guard against this.
236 if (outline == null) {
Patrick Dubroyfe6bd872010-10-13 17:32:10 -0700237 if (false) {
238 Object val = animation.getAnimatedValue();
239 Log.d(TAG, "anim " + thisIndex + " update: " + val +
240 ", isStopped " + anim.isStopped());
241 }
Joe Onorato4be866d2010-10-10 11:26:02 -0700242 // Try to prevent it from continuing to run
243 animation.cancel();
244 } else {
Chet Haase472b2812010-10-14 07:02:04 -0700245 mDragOutlineAlphas[thisIndex] = (Float) animation.getAnimatedValue();
Joe Onorato4be866d2010-10-10 11:26:02 -0700246 final int left = mDragOutlines[thisIndex].x;
247 final int top = mDragOutlines[thisIndex].y;
248 CellLayout.this.invalidate(left, top,
249 left + outline.getWidth(), top + outline.getHeight());
250 }
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700251 }
252 });
Joe Onorato4be866d2010-10-10 11:26:02 -0700253 // The animation holds a reference to the drag outline bitmap as long is it's
254 // running. This way the bitmap can be GCed when the animations are complete.
Chet Haase472b2812010-10-14 07:02:04 -0700255 anim.getAnimator().addListener(new AnimatorListenerAdapter() {
Michael Jurka3c4c20f2010-10-28 15:36:06 -0700256 @Override
Joe Onorato4be866d2010-10-10 11:26:02 -0700257 public void onAnimationEnd(Animator animation) {
Chet Haase472b2812010-10-14 07:02:04 -0700258 if ((Float) ((ValueAnimator) animation).getAnimatedValue() == 0f) {
Joe Onorato4be866d2010-10-10 11:26:02 -0700259 anim.setTag(null);
260 }
261 }
262 });
263 mDragOutlineAnims[i] = anim;
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700264 }
Patrick Dubroyce34a972010-10-19 10:34:32 -0700265
Michael Jurka18014792010-10-14 09:01:34 -0700266 mBackgroundRect = new Rect();
Michael Jurka33945b22010-12-21 18:19:38 -0800267 mGlowBackgroundRect = new Rect();
Michael Jurka18014792010-10-14 09:01:34 -0700268 setHoverScale(1.0f);
269 setHoverAlpha(1.0f);
270 }
271
Michael Jurka33945b22010-12-21 18:19:38 -0800272 public void setIsDefaultDropTarget(boolean isDefaultDropTarget) {
273 if (mIsDefaultDropTarget != isDefaultDropTarget) {
274 mIsDefaultDropTarget = isDefaultDropTarget;
275 invalidate();
276 }
277 }
278
279 public void setAcceptsDrops(boolean acceptsDrops) {
280 if (mAcceptsDrops != acceptsDrops) {
281 mAcceptsDrops = acceptsDrops;
282 invalidate();
283 }
284 }
285
286 void setIsDragOccuring(boolean isDragOccuring) {
287 if (mIsDragOccuring != isDragOccuring) {
288 mIsDragOccuring = isDragOccuring;
289 invalidate();
290 }
291 }
292
293 void setIsDragOverlapping(boolean isDragOverlapping) {
294 if (mIsDragOverlapping != isDragOverlapping) {
295 mIsDragOverlapping = isDragOverlapping;
296 invalidate();
297 }
298 }
299
300 boolean getIsDragOverlapping() {
301 return mIsDragOverlapping;
302 }
303
304 private void updateGlowRect() {
305 float marginFraction = (mGlowBackgroundScale - 1.0f) / 2.0f;
Michael Jurka18014792010-10-14 09:01:34 -0700306 int marginX = (int) (marginFraction * (mBackgroundRect.right - mBackgroundRect.left));
307 int marginY = (int) (marginFraction * (mBackgroundRect.bottom - mBackgroundRect.top));
Michael Jurka33945b22010-12-21 18:19:38 -0800308 mGlowBackgroundRect.set(mBackgroundRect.left - marginX, mBackgroundRect.top - marginY,
Michael Jurka18014792010-10-14 09:01:34 -0700309 mBackgroundRect.right + marginX, mBackgroundRect.bottom + marginY);
310 invalidate();
311 }
312
313 public void setHoverScale(float scaleFactor) {
Michael Jurka33945b22010-12-21 18:19:38 -0800314 if (scaleFactor != mGlowBackgroundScale) {
315 mGlowBackgroundScale = scaleFactor;
316 updateGlowRect();
Michael Jurka18014792010-10-14 09:01:34 -0700317 }
318 }
319
320 public float getHoverScale() {
Michael Jurka33945b22010-12-21 18:19:38 -0800321 return mGlowBackgroundScale;
Michael Jurka18014792010-10-14 09:01:34 -0700322 }
323
324 public float getHoverAlpha() {
Michael Jurka33945b22010-12-21 18:19:38 -0800325 return mGlowBackgroundAlpha;
Michael Jurka18014792010-10-14 09:01:34 -0700326 }
327
328 public void setHoverAlpha(float alpha) {
Michael Jurka33945b22010-12-21 18:19:38 -0800329 mGlowBackgroundAlpha = alpha;
Michael Jurka18014792010-10-14 09:01:34 -0700330 invalidate();
331 }
332
333 void animateDrop() {
334 if (LauncherApplication.isScreenXLarge()) {
335 Resources res = getResources();
336 float onDropScale = res.getInteger(R.integer.config_screenOnDropScalePercent) / 100.0f;
337 ObjectAnimator scaleUp = ObjectAnimator.ofFloat(this, "hoverScale", onDropScale);
338 scaleUp.setDuration(res.getInteger(R.integer.config_screenOnDropScaleUpDuration));
339 ObjectAnimator scaleDown = ObjectAnimator.ofFloat(this, "hoverScale", 1.0f);
340 scaleDown.setDuration(res.getInteger(R.integer.config_screenOnDropScaleDownDuration));
341 ObjectAnimator alphaFadeOut = ObjectAnimator.ofFloat(this, "hoverAlpha", 0.0f);
342
343 alphaFadeOut.setStartDelay(res.getInteger(R.integer.config_screenOnDropAlphaFadeDelay));
344 alphaFadeOut.setDuration(res.getInteger(R.integer.config_screenOnDropAlphaFadeDelay));
345
346 AnimatorSet bouncer = new AnimatorSet();
347 bouncer.play(scaleUp).before(scaleDown);
348 bouncer.play(scaleUp).with(alphaFadeOut);
Michael Jurka8edd75c2010-12-17 20:15:06 -0800349 bouncer.addListener(new AnimatorListenerAdapter() {
Michael Jurka3c4c20f2010-10-28 15:36:06 -0700350 @Override
Michael Jurka18014792010-10-14 09:01:34 -0700351 public void onAnimationStart(Animator animation) {
Michael Jurka33945b22010-12-21 18:19:38 -0800352 setIsDragOverlapping(true);
Michael Jurka18014792010-10-14 09:01:34 -0700353 }
Michael Jurka3c4c20f2010-10-28 15:36:06 -0700354 @Override
Michael Jurka8edd75c2010-12-17 20:15:06 -0800355 public void onAnimationEnd(Animator animation) {
Michael Jurka33945b22010-12-21 18:19:38 -0800356 setIsDragOverlapping(false);
Michael Jurka18014792010-10-14 09:01:34 -0700357 setHoverScale(1.0f);
358 setHoverAlpha(1.0f);
359 }
360 });
361 bouncer.start();
362 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800363 }
364
Patrick Dubroy1262e362010-10-06 15:49:50 -0700365 public void drawChildren(Canvas canvas) {
366 super.dispatchDraw(canvas);
367 }
368
Jeff Sharkey83f111d2009-04-20 21:03:13 -0700369 @Override
Patrick Dubroy1262e362010-10-06 15:49:50 -0700370 protected void onDraw(Canvas canvas) {
Michael Jurka3e7c7632010-10-02 16:01:03 -0700371 // When we're large, we are either drawn in a "hover" state (ie when dragging an item to
372 // a neighboring page) or with just a normal background (if backgroundAlpha > 0.0f)
373 // When we're small, we are either drawn normally or in the "accepts drops" state (during
374 // a drag). However, we also drag the mini hover background *over* one of those two
375 // backgrounds
Michael Jurka5f1c5092010-09-03 14:15:02 -0700376 if (mBackgroundAlpha > 0.0f) {
Adam Cohenf34bab52010-09-30 14:11:56 -0700377 Drawable bg;
Michael Jurka33945b22010-12-21 18:19:38 -0800378 boolean mini = getScaleX() < 0.5f;
379
380 if (mIsDragOverlapping) {
381 // In the mini case, we draw the active_glow bg *over* the active background
382 bg = mini ? mActiveBackgroundMini : mActiveGlowBackground;
383 } else if (mIsDragOccuring && mAcceptsDrops) {
384 bg = mini ? mActiveBackgroundMini : mActiveBackground;
385 } else if (mIsDefaultDropTarget) {
386 bg = mini ? mNormalGlowBackgroundMini : mNormalGlowBackground;
Adam Cohenf34bab52010-09-30 14:11:56 -0700387 } else {
Michael Jurka33945b22010-12-21 18:19:38 -0800388 bg = mini ? mNormalBackgroundMini : mNormalBackground;
Adam Cohenf34bab52010-09-30 14:11:56 -0700389 }
Michael Jurka33945b22010-12-21 18:19:38 -0800390
391 bg.setAlpha((int) (mBackgroundAlpha * mBackgroundAlphaMultiplier * 255));
392 bg.setBounds(mBackgroundRect);
393 bg.draw(canvas);
394
395 if (mini && mIsDragOverlapping) {
Michael Jurka18014792010-10-14 09:01:34 -0700396 boolean modifiedClipRect = false;
Michael Jurka33945b22010-12-21 18:19:38 -0800397 if (mGlowBackgroundScale > 1.0f) {
Michael Jurka18014792010-10-14 09:01:34 -0700398 // If the hover background's scale is greater than 1, we'll be drawing outside
399 // the bounds of this CellLayout. Get around that by temporarily increasing the
400 // size of the clip rect
Michael Jurka33945b22010-12-21 18:19:38 -0800401 float marginFraction = (mGlowBackgroundScale - 1.0f) / 2.0f;
Michael Jurka18014792010-10-14 09:01:34 -0700402 Rect clipRect = canvas.getClipBounds();
403 int marginX = (int) (marginFraction * (clipRect.right - clipRect.left));
404 int marginY = (int) (marginFraction * (clipRect.bottom - clipRect.top));
405 canvas.save(Canvas.CLIP_SAVE_FLAG);
406 canvas.clipRect(-marginX, -marginY,
407 getWidth() + marginX, getHeight() + marginY, Region.Op.REPLACE);
408 modifiedClipRect = true;
409 }
410
Michael Jurka33945b22010-12-21 18:19:38 -0800411 mActiveGlowBackgroundMini.setAlpha(
412 (int) (mBackgroundAlpha * mGlowBackgroundAlpha * 255));
413 mActiveGlowBackgroundMini.setBounds(mGlowBackgroundRect);
414 mActiveGlowBackgroundMini.draw(canvas);
Michael Jurka18014792010-10-14 09:01:34 -0700415 if (modifiedClipRect) {
416 canvas.restore();
417 }
Michael Jurka3e7c7632010-10-02 16:01:03 -0700418 }
Michael Jurkaa63c4522010-08-19 13:52:27 -0700419 }
Romain Guya6abce82009-11-10 02:54:41 -0800420
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700421 if (mCrosshairsVisibility > 0.0f) {
422 final int countX = mCountX;
423 final int countY = mCountY;
424
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700425 final float MAX_ALPHA = 0.4f;
426 final int MAX_VISIBLE_DISTANCE = 600;
427 final float DISTANCE_MULTIPLIER = 0.002f;
428
429 final Drawable d = mCrosshairsDrawable;
430 final int width = d.getIntrinsicWidth();
431 final int height = d.getIntrinsicHeight();
432
433 int x = getLeftPadding() - (mWidthGap / 2) - (width / 2);
434 for (int col = 0; col <= countX; col++) {
435 int y = getTopPadding() - (mHeightGap / 2) - (height / 2);
436 for (int row = 0; row <= countY; row++) {
437 mTmpPointF.set(x - mDragCenter.x, y - mDragCenter.y);
438 float dist = mTmpPointF.length();
439 // Crosshairs further from the drag point are more faint
440 float alpha = Math.min(MAX_ALPHA,
441 DISTANCE_MULTIPLIER * (MAX_VISIBLE_DISTANCE - dist));
442 if (alpha > 0.0f) {
443 d.setBounds(x, y, x + width, y + height);
444 d.setAlpha((int) (alpha * 255 * mCrosshairsVisibility));
445 d.draw(canvas);
446 }
447 y += mCellHeight + mHeightGap;
448 }
449 x += mCellWidth + mWidthGap;
450 }
Joe Onorato4be866d2010-10-10 11:26:02 -0700451 }
Winson Chung150fbab2010-09-29 17:14:26 -0700452
Patrick Dubroy8e58e912010-10-14 13:21:48 -0700453 final Paint paint = mDragOutlinePaint;
Joe Onorato4be866d2010-10-10 11:26:02 -0700454 for (int i = 0; i < mDragOutlines.length; i++) {
Chet Haase472b2812010-10-14 07:02:04 -0700455 final float alpha = mDragOutlineAlphas[i];
Joe Onorato4be866d2010-10-10 11:26:02 -0700456 if (alpha > 0) {
457 final Point p = mDragOutlines[i];
458 final Bitmap b = (Bitmap) mDragOutlineAnims[i].getTag();
Chet Haase472b2812010-10-14 07:02:04 -0700459 paint.setAlpha((int)(alpha + .5f));
Joe Onorato4be866d2010-10-10 11:26:02 -0700460 canvas.drawBitmap(b, p.x, p.y, paint);
Winson Chung150fbab2010-09-29 17:14:26 -0700461 }
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700462 }
463 }
464
Adam Cohenf34bab52010-09-30 14:11:56 -0700465 public void setDimmableProgress(float progress) {
466 for (int i = 0; i < getChildCount(); i++) {
467 Dimmable d = (Dimmable) getChildAt(i);
468 d.setDimmableProgress(progress);
469 }
470 }
471
472 public float getDimmableProgress() {
473 if (getChildCount() > 0) {
474 return ((Dimmable) getChildAt(0)).getDimmableProgress();
475 }
476 return 0.0f;
477 }
478
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700479 @Override
Jeff Sharkey83f111d2009-04-20 21:03:13 -0700480 public void cancelLongPress() {
481 super.cancelLongPress();
482
483 // Cancel long press for all children
484 final int count = getChildCount();
485 for (int i = 0; i < count; i++) {
486 final View child = getChildAt(i);
487 child.cancelLongPress();
488 }
489 }
490
Michael Jurkadee05892010-07-27 10:01:56 -0700491 public void setOnInterceptTouchListener(View.OnTouchListener listener) {
492 mInterceptTouchListener = listener;
493 }
494
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800495 int getCountX() {
Adam Cohend22015c2010-07-26 22:02:18 -0700496 return mCountX;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800497 }
498
499 int getCountY() {
Adam Cohend22015c2010-07-26 22:02:18 -0700500 return mCountY;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800501 }
502
Winson Chungaafa03c2010-06-11 17:34:16 -0700503 public boolean addViewToCellLayout(View child, int index, int childId, LayoutParams params) {
Michael Jurkaf3ca3ab2010-10-20 17:08:24 -0700504 return addViewToCellLayout(child, index, childId, params, true);
505 }
506
507 public boolean addViewToCellLayout(
508 View child, int index, int childId, LayoutParams params, boolean markCells) {
Winson Chungaafa03c2010-06-11 17:34:16 -0700509 final LayoutParams lp = params;
510
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800511 // Generate an id for each view, this assumes we have at most 256x256 cells
512 // per workspace screen
Adam Cohend22015c2010-07-26 22:02:18 -0700513 if (lp.cellX >= 0 && lp.cellX <= mCountX - 1 && lp.cellY >= 0 && lp.cellY <= mCountY - 1) {
Winson Chungaafa03c2010-06-11 17:34:16 -0700514 // If the horizontal or vertical span is set to -1, it is taken to
515 // mean that it spans the extent of the CellLayout
Adam Cohend22015c2010-07-26 22:02:18 -0700516 if (lp.cellHSpan < 0) lp.cellHSpan = mCountX;
517 if (lp.cellVSpan < 0) lp.cellVSpan = mCountY;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800518
Winson Chungaafa03c2010-06-11 17:34:16 -0700519 child.setId(childId);
520
Michael Jurkadee05892010-07-27 10:01:56 -0700521 // We might be in the middle or end of shrinking/fading to a dimmed view
522 // Make sure this view's alpha is set the same as all the rest of the views
Michael Jurka5f1c5092010-09-03 14:15:02 -0700523 child.setAlpha(getAlpha());
Winson Chungaafa03c2010-06-11 17:34:16 -0700524 addView(child, index, lp);
Michael Jurkadee05892010-07-27 10:01:56 -0700525
Michael Jurkaf3ca3ab2010-10-20 17:08:24 -0700526 if (markCells) markCellsAsOccupiedForView(child);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700527
Winson Chungaafa03c2010-06-11 17:34:16 -0700528 return true;
529 }
530 return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800531 }
Michael Jurka3e7c7632010-10-02 16:01:03 -0700532
533 public boolean getAcceptsDrops() {
534 return mAcceptsDrops;
535 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800536
537 @Override
Michael Jurka0280c3b2010-09-17 15:00:07 -0700538 public void removeAllViews() {
539 clearOccupiedCells();
540 }
541
542 @Override
543 public void removeAllViewsInLayout() {
544 clearOccupiedCells();
545 }
546
Michael Jurkaf3ca3ab2010-10-20 17:08:24 -0700547 public void removeViewWithoutMarkingCells(View view) {
548 super.removeView(view);
549 }
550
Michael Jurka0280c3b2010-09-17 15:00:07 -0700551 @Override
552 public void removeView(View view) {
553 markCellsAsUnoccupiedForView(view);
554 super.removeView(view);
555 }
556
557 @Override
558 public void removeViewAt(int index) {
559 markCellsAsUnoccupiedForView(getChildAt(index));
560 super.removeViewAt(index);
561 }
562
563 @Override
564 public void removeViewInLayout(View view) {
565 markCellsAsUnoccupiedForView(view);
566 super.removeViewInLayout(view);
567 }
568
569 @Override
570 public void removeViews(int start, int count) {
571 for (int i = start; i < start + count; i++) {
572 markCellsAsUnoccupiedForView(getChildAt(i));
573 }
574 super.removeViews(start, count);
575 }
576
577 @Override
578 public void removeViewsInLayout(int start, int count) {
579 for (int i = start; i < start + count; i++) {
580 markCellsAsUnoccupiedForView(getChildAt(i));
581 }
582 super.removeViewsInLayout(start, count);
583 }
584
585 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800586 public void requestChildFocus(View child, View focused) {
587 super.requestChildFocus(child, focused);
588 if (child != null) {
589 Rect r = new Rect();
590 child.getDrawingRect(r);
591 requestRectangleOnScreen(r);
592 }
593 }
594
595 @Override
596 protected void onAttachedToWindow() {
597 super.onAttachedToWindow();
598 mCellInfo.screen = ((ViewGroup) getParent()).indexOfChild(this);
599 }
600
Michael Jurkaaf442092010-06-10 17:01:57 -0700601 public void setTagToCellInfoForPoint(int touchX, int touchY) {
602 final CellInfo cellInfo = mCellInfo;
603 final Rect frame = mRect;
604 final int x = touchX + mScrollX;
605 final int y = touchY + mScrollY;
606 final int count = getChildCount();
607
608 boolean found = false;
609 for (int i = count - 1; i >= 0; i--) {
610 final View child = getChildAt(i);
611
612 if ((child.getVisibility()) == VISIBLE || child.getAnimation() != null) {
613 child.getHitRect(frame);
614 if (frame.contains(x, y)) {
615 final LayoutParams lp = (LayoutParams) child.getLayoutParams();
616 cellInfo.cell = child;
617 cellInfo.cellX = lp.cellX;
618 cellInfo.cellY = lp.cellY;
619 cellInfo.spanX = lp.cellHSpan;
620 cellInfo.spanY = lp.cellVSpan;
621 cellInfo.valid = true;
622 found = true;
Michael Jurkaaf442092010-06-10 17:01:57 -0700623 break;
624 }
625 }
626 }
Winson Chungaafa03c2010-06-11 17:34:16 -0700627
Michael Jurkaaf442092010-06-10 17:01:57 -0700628 if (!found) {
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700629 final int cellXY[] = mTmpCellXY;
Michael Jurkaaf442092010-06-10 17:01:57 -0700630 pointToCellExact(x, y, cellXY);
631
Michael Jurkaaf442092010-06-10 17:01:57 -0700632 cellInfo.cell = null;
633 cellInfo.cellX = cellXY[0];
634 cellInfo.cellY = cellXY[1];
635 cellInfo.spanX = 1;
636 cellInfo.spanY = 1;
Michael Jurka0280c3b2010-09-17 15:00:07 -0700637 cellInfo.valid = cellXY[0] >= 0 && cellXY[1] >= 0 && cellXY[0] < mCountX &&
638 cellXY[1] < mCountY && !mOccupied[cellXY[0]][cellXY[1]];
Michael Jurkaaf442092010-06-10 17:01:57 -0700639 }
640 setTag(cellInfo);
641 }
642
Winson Chungaafa03c2010-06-11 17:34:16 -0700643
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800644 @Override
645 public boolean onInterceptTouchEvent(MotionEvent ev) {
Michael Jurkadee05892010-07-27 10:01:56 -0700646 if (mInterceptTouchListener != null && mInterceptTouchListener.onTouch(this, ev)) {
647 return true;
648 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800649 final int action = ev.getAction();
650 final CellInfo cellInfo = mCellInfo;
651
652 if (action == MotionEvent.ACTION_DOWN) {
Michael Jurkaaf442092010-06-10 17:01:57 -0700653 setTagToCellInfoForPoint((int) ev.getX(), (int) ev.getY());
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800654 } else if (action == MotionEvent.ACTION_UP) {
655 cellInfo.cell = null;
656 cellInfo.cellX = -1;
657 cellInfo.cellY = -1;
658 cellInfo.spanX = 0;
659 cellInfo.spanY = 0;
660 cellInfo.valid = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800661 setTag(cellInfo);
662 }
663
664 return false;
665 }
666
667 @Override
668 public CellInfo getTag() {
Michael Jurka0280c3b2010-09-17 15:00:07 -0700669 return (CellInfo) super.getTag();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800670 }
671
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700672 /**
673 * Check if the row 'y' is empty from columns 'left' to 'right', inclusive.
674 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800675 private static boolean isRowEmpty(int y, int left, int right, boolean[][] occupied) {
676 for (int x = left; x <= right; x++) {
677 if (occupied[x][y]) {
678 return false;
679 }
680 }
681 return true;
682 }
683
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800684 /**
Winson Chungaafa03c2010-06-11 17:34:16 -0700685 * Given a point, return the cell that strictly encloses that point
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800686 * @param x X coordinate of the point
687 * @param y Y coordinate of the point
688 * @param result Array of 2 ints to hold the x and y coordinate of the cell
689 */
690 void pointToCellExact(int x, int y, int[] result) {
Winson Chungaafa03c2010-06-11 17:34:16 -0700691 final int hStartPadding = getLeftPadding();
692 final int vStartPadding = getTopPadding();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800693
694 result[0] = (x - hStartPadding) / (mCellWidth + mWidthGap);
695 result[1] = (y - vStartPadding) / (mCellHeight + mHeightGap);
696
Adam Cohend22015c2010-07-26 22:02:18 -0700697 final int xAxis = mCountX;
698 final int yAxis = mCountY;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800699
700 if (result[0] < 0) result[0] = 0;
701 if (result[0] >= xAxis) result[0] = xAxis - 1;
702 if (result[1] < 0) result[1] = 0;
703 if (result[1] >= yAxis) result[1] = yAxis - 1;
704 }
Winson Chungaafa03c2010-06-11 17:34:16 -0700705
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800706 /**
707 * Given a point, return the cell that most closely encloses that point
708 * @param x X coordinate of the point
709 * @param y Y coordinate of the point
710 * @param result Array of 2 ints to hold the x and y coordinate of the cell
711 */
712 void pointToCellRounded(int x, int y, int[] result) {
713 pointToCellExact(x + (mCellWidth / 2), y + (mCellHeight / 2), result);
714 }
715
716 /**
717 * Given a cell coordinate, return the point that represents the upper left corner of that cell
Winson Chungaafa03c2010-06-11 17:34:16 -0700718 *
719 * @param cellX X coordinate of the cell
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800720 * @param cellY Y coordinate of the cell
Winson Chungaafa03c2010-06-11 17:34:16 -0700721 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800722 * @param result Array of 2 ints to hold the x and y coordinate of the point
723 */
724 void cellToPoint(int cellX, int cellY, int[] result) {
Winson Chungaafa03c2010-06-11 17:34:16 -0700725 final int hStartPadding = getLeftPadding();
726 final int vStartPadding = getTopPadding();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800727
728 result[0] = hStartPadding + cellX * (mCellWidth + mWidthGap);
729 result[1] = vStartPadding + cellY * (mCellHeight + mHeightGap);
730 }
731
Romain Guy84f296c2009-11-04 15:00:44 -0800732 int getCellWidth() {
733 return mCellWidth;
734 }
735
736 int getCellHeight() {
737 return mCellHeight;
738 }
739
Romain Guy1a304a12009-11-10 00:02:32 -0800740 int getLeftPadding() {
Winson Chungaafa03c2010-06-11 17:34:16 -0700741 return mLeftPadding;
Romain Guy1a304a12009-11-10 00:02:32 -0800742 }
743
744 int getTopPadding() {
Winson Chungaafa03c2010-06-11 17:34:16 -0700745 return mTopPadding;
Romain Guy1a304a12009-11-10 00:02:32 -0800746 }
747
748 int getRightPadding() {
Winson Chungaafa03c2010-06-11 17:34:16 -0700749 return mRightPadding;
Romain Guy1a304a12009-11-10 00:02:32 -0800750 }
751
752 int getBottomPadding() {
Winson Chungaafa03c2010-06-11 17:34:16 -0700753 return mBottomPadding;
Romain Guy1a304a12009-11-10 00:02:32 -0800754 }
755
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800756 @Override
757 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
758 // TODO: currently ignoring padding
Winson Chungaafa03c2010-06-11 17:34:16 -0700759
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800760 int widthSpecMode = MeasureSpec.getMode(widthMeasureSpec);
Winson Chungaafa03c2010-06-11 17:34:16 -0700761 int widthSpecSize = MeasureSpec.getSize(widthMeasureSpec);
762
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800763 int heightSpecMode = MeasureSpec.getMode(heightMeasureSpec);
764 int heightSpecSize = MeasureSpec.getSize(heightMeasureSpec);
Winson Chungaafa03c2010-06-11 17:34:16 -0700765
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800766 if (widthSpecMode == MeasureSpec.UNSPECIFIED || heightSpecMode == MeasureSpec.UNSPECIFIED) {
767 throw new RuntimeException("CellLayout cannot have UNSPECIFIED dimensions");
768 }
769
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800770 final int cellWidth = mCellWidth;
771 final int cellHeight = mCellHeight;
772
Adam Cohend22015c2010-07-26 22:02:18 -0700773 int numWidthGaps = mCountX - 1;
774 int numHeightGaps = mCountY - 1;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800775
Winson Chungece7f5b2010-10-22 14:54:12 -0700776 if (mWidthGap < 0 || mHeightGap < 0) {
777 int vSpaceLeft = heightSpecSize - mTopPadding - mBottomPadding - (cellHeight * mCountY);
778 mHeightGap = vSpaceLeft / numHeightGaps;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800779
Winson Chungece7f5b2010-10-22 14:54:12 -0700780 int hSpaceLeft = widthSpecSize - mLeftPadding - mRightPadding - (cellWidth * mCountX);
781 mWidthGap = hSpaceLeft / numWidthGaps;
Winson Chungaafa03c2010-06-11 17:34:16 -0700782
Winson Chungece7f5b2010-10-22 14:54:12 -0700783 // center it around the min gaps
784 int minGap = Math.min(mWidthGap, mHeightGap);
785 mWidthGap = mHeightGap = minGap;
786 }
Michael Jurka5f1c5092010-09-03 14:15:02 -0700787
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800788 int count = getChildCount();
789
790 for (int i = 0; i < count; i++) {
791 View child = getChildAt(i);
792 LayoutParams lp = (LayoutParams) child.getLayoutParams();
Winson Chungaafa03c2010-06-11 17:34:16 -0700793 lp.setup(cellWidth, cellHeight, mWidthGap, mHeightGap,
794 mLeftPadding, mTopPadding);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800795
Michael Jurka0280c3b2010-09-17 15:00:07 -0700796 int childWidthMeasureSpec = MeasureSpec.makeMeasureSpec(lp.width, MeasureSpec.EXACTLY);
Winson Chungaafa03c2010-06-11 17:34:16 -0700797 int childheightMeasureSpec = MeasureSpec.makeMeasureSpec(lp.height,
798 MeasureSpec.EXACTLY);
799
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800800 child.measure(childWidthMeasureSpec, childheightMeasureSpec);
801 }
Michael Jurka5f1c5092010-09-03 14:15:02 -0700802 if (widthSpecMode == MeasureSpec.AT_MOST) {
803 int newWidth = mLeftPadding + mRightPadding + (mCountX * cellWidth) +
Winson Chungece7f5b2010-10-22 14:54:12 -0700804 ((mCountX - 1) * mWidthGap);
Michael Jurka5f1c5092010-09-03 14:15:02 -0700805 int newHeight = mTopPadding + mBottomPadding + (mCountY * cellHeight) +
Winson Chungece7f5b2010-10-22 14:54:12 -0700806 ((mCountY - 1) * mHeightGap);
Michael Jurka5f1c5092010-09-03 14:15:02 -0700807 setMeasuredDimension(newWidth, newHeight);
808 } else if (widthSpecMode == MeasureSpec.EXACTLY) {
809 setMeasuredDimension(widthSpecSize, heightSpecSize);
810 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800811 }
812
813 @Override
Michael Jurka28750fb2010-09-24 17:43:49 -0700814 protected void onLayout(boolean changed, int l, int t, int r, int b) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800815 int count = getChildCount();
816
817 for (int i = 0; i < count; i++) {
Patrick Dubroyce34a972010-10-19 10:34:32 -0700818 final View child = getChildAt(i);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800819 if (child.getVisibility() != GONE) {
820
821 CellLayout.LayoutParams lp = (CellLayout.LayoutParams) child.getLayoutParams();
822
823 int childLeft = lp.x;
824 int childTop = lp.y;
825 child.layout(childLeft, childTop, childLeft + lp.width, childTop + lp.height);
Romain Guy84f296c2009-11-04 15:00:44 -0800826
827 if (lp.dropped) {
828 lp.dropped = false;
829
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700830 final int[] cellXY = mTmpCellXY;
Romain Guy06762ab2010-01-25 16:51:08 -0800831 getLocationOnScreen(cellXY);
Jeff Brown1d0867c2010-12-02 18:27:39 -0800832 mWallpaperManager.sendWallpaperCommand(getWindowToken(),
833 WallpaperManager.COMMAND_DROP,
Romain Guy06762ab2010-01-25 16:51:08 -0800834 cellXY[0] + childLeft + lp.width / 2,
835 cellXY[1] + childTop + lp.height / 2, 0, null);
Patrick Dubroyce34a972010-10-19 10:34:32 -0700836
Michael Jurkad3ef3062010-11-23 16:23:58 -0800837 if (lp.animateDrop) {
838 lp.animateDrop = false;
839 ((Workspace) mParent).animateViewIntoPosition(child);
840 }
Romain Guy84f296c2009-11-04 15:00:44 -0800841 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800842 }
843 }
844 }
845
846 @Override
Michael Jurkadee05892010-07-27 10:01:56 -0700847 protected void onSizeChanged(int w, int h, int oldw, int oldh) {
848 super.onSizeChanged(w, h, oldw, oldh);
Michael Jurka18014792010-10-14 09:01:34 -0700849 mBackgroundRect.set(0, 0, w, h);
Michael Jurka33945b22010-12-21 18:19:38 -0800850 updateGlowRect();
Michael Jurkadee05892010-07-27 10:01:56 -0700851 }
852
853 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800854 protected void setChildrenDrawingCacheEnabled(boolean enabled) {
855 final int count = getChildCount();
856 for (int i = 0; i < count; i++) {
857 final View view = getChildAt(i);
858 view.setDrawingCacheEnabled(enabled);
859 // Update the drawing caches
Romain Guy3cf4c032010-10-26 14:29:35 -0700860 if (!view.isHardwareAccelerated()) {
861 view.buildDrawingCache(true);
862 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800863 }
864 }
865
866 @Override
867 protected void setChildrenDrawnWithCacheEnabled(boolean enabled) {
868 super.setChildrenDrawnWithCacheEnabled(enabled);
869 }
870
Michael Jurka5f1c5092010-09-03 14:15:02 -0700871 public float getBackgroundAlpha() {
872 return mBackgroundAlpha;
Michael Jurkadee05892010-07-27 10:01:56 -0700873 }
874
Adam Cohen1b0aaac2010-10-28 11:11:18 -0700875 public void setBackgroundAlphaMultiplier(float multiplier) {
876 mBackgroundAlphaMultiplier = multiplier;
877 }
878
Adam Cohenddb82192010-11-10 16:32:54 -0800879 public float getBackgroundAlphaMultiplier() {
880 return mBackgroundAlphaMultiplier;
881 }
882
Michael Jurka5f1c5092010-09-03 14:15:02 -0700883 public void setBackgroundAlpha(float alpha) {
884 mBackgroundAlpha = alpha;
Michael Jurka0142d492010-08-25 17:46:15 -0700885 invalidate();
Michael Jurkadee05892010-07-27 10:01:56 -0700886 }
887
Michael Jurka5f1c5092010-09-03 14:15:02 -0700888 // Need to return true to let the view system know we know how to handle alpha-- this is
889 // because when our children have an alpha of 0.0f, they are still rendering their "dimmed"
890 // versions
891 @Override
892 protected boolean onSetAlpha(int alpha) {
893 return true;
894 }
895
896 public void setAlpha(float alpha) {
897 setChildrenAlpha(alpha);
898 super.setAlpha(alpha);
899 }
900
Michael Jurkadee05892010-07-27 10:01:56 -0700901 private void setChildrenAlpha(float alpha) {
Michael Jurka0142d492010-08-25 17:46:15 -0700902 final int childCount = getChildCount();
903 for (int i = 0; i < childCount; i++) {
Michael Jurkadee05892010-07-27 10:01:56 -0700904 getChildAt(i).setAlpha(alpha);
905 }
906 }
907
Michael Jurka0280c3b2010-09-17 15:00:07 -0700908 private boolean isVacantIgnoring(
909 int originX, int originY, int spanX, int spanY, View ignoreView) {
910 if (ignoreView != null) {
911 markCellsAsUnoccupiedForView(ignoreView);
912 }
Michael Jurka28750fb2010-09-24 17:43:49 -0700913 boolean isVacant = true;
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700914 for (int i = 0; i < spanY; i++) {
915 if (!isRowEmpty(originY + i, originX, originX + spanX - 1, mOccupied)) {
Michael Jurka28750fb2010-09-24 17:43:49 -0700916 isVacant = false;
917 break;
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700918 }
919 }
Michael Jurka0280c3b2010-09-17 15:00:07 -0700920 if (ignoreView != null) {
921 markCellsAsOccupiedForView(ignoreView);
922 }
Michael Jurka28750fb2010-09-24 17:43:49 -0700923 return isVacant;
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700924 }
925
Michael Jurka0280c3b2010-09-17 15:00:07 -0700926 private boolean isVacant(int originX, int originY, int spanX, int spanY) {
927 return isVacantIgnoring(originX, originY, spanX, spanY, null);
928 }
929
Patrick Dubroy440c3602010-07-13 17:50:32 -0700930 public View getChildAt(int x, int y) {
931 final int count = getChildCount();
932 for (int i = 0; i < count; i++) {
933 View child = getChildAt(i);
934 LayoutParams lp = (LayoutParams) child.getLayoutParams();
935
936 if ((lp.cellX <= x) && (x < lp.cellX + lp.cellHSpan) &&
937 (lp.cellY <= y) && (y < lp.cellY + lp.cellHSpan)) {
938 return child;
939 }
940 }
941 return null;
942 }
943
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700944 /**
945 * Estimate where the top left cell of the dragged item will land if it is dropped.
946 *
947 * @param originX The X value of the top left corner of the item
948 * @param originY The Y value of the top left corner of the item
949 * @param spanX The number of horizontal cells that the item spans
950 * @param spanY The number of vertical cells that the item spans
951 * @param result The estimated drop cell X and Y.
952 */
953 void estimateDropCell(int originX, int originY, int spanX, int spanY, int[] result) {
Adam Cohend22015c2010-07-26 22:02:18 -0700954 final int countX = mCountX;
955 final int countY = mCountY;
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700956
Michael Jurkaa63c4522010-08-19 13:52:27 -0700957 // pointToCellRounded takes the top left of a cell but will pad that with
958 // cellWidth/2 and cellHeight/2 when finding the matching cell
959 pointToCellRounded(originX, originY, result);
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700960
961 // If the item isn't fully on this screen, snap to the edges
962 int rightOverhang = result[0] + spanX - countX;
963 if (rightOverhang > 0) {
964 result[0] -= rightOverhang; // Snap to right
965 }
966 result[0] = Math.max(0, result[0]); // Snap to left
967 int bottomOverhang = result[1] + spanY - countY;
968 if (bottomOverhang > 0) {
969 result[1] -= bottomOverhang; // Snap to bottom
970 }
971 result[1] = Math.max(0, result[1]); // Snap to top
972 }
973
Joe Onorato4be866d2010-10-10 11:26:02 -0700974 void visualizeDropLocation(
975 View v, Bitmap dragOutline, int originX, int originY, int spanX, int spanY) {
976
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -0700977 final int oldDragCellX = mDragCell[0];
978 final int oldDragCellY = mDragCell[1];
Joe Onorato4be866d2010-10-10 11:26:02 -0700979 final int[] nearest = findNearestVacantArea(originX, originY, spanX, spanY, v, mDragCell);
Winson Chunga9abd0e2010-10-27 17:18:37 -0700980 if (v != null) {
981 mDragCenter.set(originX + (v.getWidth() / 2), originY + (v.getHeight() / 2));
982 } else {
983 mDragCenter.set(originX, originY);
984 }
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700985
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -0700986 if (nearest != null && (nearest[0] != oldDragCellX || nearest[1] != oldDragCellY)) {
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700987 // Find the top left corner of the rect the object will occupy
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700988 final int[] topLeft = mTmpPoint;
989 cellToPoint(nearest[0], nearest[1], topLeft);
990
Joe Onorato4be866d2010-10-10 11:26:02 -0700991 int left = topLeft[0];
992 int top = topLeft[1];
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700993
Winson Chunga9abd0e2010-10-27 17:18:37 -0700994 if (v != null) {
995 if (v.getParent() instanceof CellLayout) {
996 LayoutParams lp = (LayoutParams) v.getLayoutParams();
997 left += lp.leftMargin;
998 top += lp.topMargin;
999 }
Winson Chung150fbab2010-09-29 17:14:26 -07001000
Winson Chunga9abd0e2010-10-27 17:18:37 -07001001 // Offsets due to the size difference between the View and the dragOutline
1002 left += (v.getWidth() - dragOutline.getWidth()) / 2;
1003 top += (v.getHeight() - dragOutline.getHeight()) / 2;
1004 }
Winson Chung150fbab2010-09-29 17:14:26 -07001005
Joe Onorato4be866d2010-10-10 11:26:02 -07001006 final int oldIndex = mDragOutlineCurrent;
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -07001007 mDragOutlineAnims[oldIndex].animateOut();
1008 mDragOutlineCurrent = (oldIndex + 1) % mDragOutlines.length;
Winson Chung150fbab2010-09-29 17:14:26 -07001009
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -07001010 mDragOutlines[mDragOutlineCurrent].set(left, top);
1011 mDragOutlineAnims[mDragOutlineCurrent].setTag(dragOutline);
1012 mDragOutlineAnims[mDragOutlineCurrent].animateIn();
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001013 }
Patrick Dubroy49250ad2010-10-08 15:33:52 -07001014
1015 // If we are drawing crosshairs, the entire CellLayout needs to be invalidated
1016 if (mCrosshairsDrawable != null) {
1017 invalidate();
1018 }
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001019 }
1020
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001021 /**
Jeff Sharkey70864282009-04-07 21:08:40 -07001022 * Find a vacant area that will fit the given bounds nearest the requested
1023 * cell location. Uses Euclidean distance to score multiple vacant areas.
Winson Chungaafa03c2010-06-11 17:34:16 -07001024 *
Romain Guy51afc022009-05-04 18:03:43 -07001025 * @param pixelX The X location at which you want to search for a vacant area.
1026 * @param pixelY The Y location at which you want to search for a vacant area.
Jeff Sharkey70864282009-04-07 21:08:40 -07001027 * @param spanX Horizontal span of the object.
1028 * @param spanY Vertical span of the object.
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001029 * @param result Array in which to place the result, or null (in which case a new array will
1030 * be allocated)
Jeff Sharkey70864282009-04-07 21:08:40 -07001031 * @return The X, Y cell of a vacant area that can contain this object,
1032 * nearest the requested location.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001033 */
Michael Jurka6a1435d2010-09-27 17:35:12 -07001034 int[] findNearestVacantArea(
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001035 int pixelX, int pixelY, int spanX, int spanY, int[] result) {
1036 return findNearestVacantArea(pixelX, pixelY, spanX, spanY, null, result);
Michael Jurka6a1435d2010-09-27 17:35:12 -07001037 }
Winson Chungaafa03c2010-06-11 17:34:16 -07001038
Michael Jurka6a1435d2010-09-27 17:35:12 -07001039 /**
1040 * Find a vacant area that will fit the given bounds nearest the requested
1041 * cell location. Uses Euclidean distance to score multiple vacant areas.
1042 *
1043 * @param pixelX The X location at which you want to search for a vacant area.
1044 * @param pixelY The Y location at which you want to search for a vacant area.
1045 * @param spanX Horizontal span of the object.
1046 * @param spanY Vertical span of the object.
Michael Jurka6a1435d2010-09-27 17:35:12 -07001047 * @param ignoreView Considers space occupied by this view as unoccupied
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001048 * @param result Previously returned value to possibly recycle.
Michael Jurka6a1435d2010-09-27 17:35:12 -07001049 * @return The X, Y cell of a vacant area that can contain this object,
1050 * nearest the requested location.
1051 */
1052 int[] findNearestVacantArea(
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001053 int pixelX, int pixelY, int spanX, int spanY, View ignoreView, int[] result) {
Michael Jurkac6ee42e2010-09-30 12:04:50 -07001054 // mark space take by ignoreView as available (method checks if ignoreView is null)
1055 markCellsAsUnoccupiedForView(ignoreView);
1056
Jeff Sharkey70864282009-04-07 21:08:40 -07001057 // Keep track of best-scoring drop area
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001058 final int[] bestXY = result != null ? result : new int[2];
Jeff Sharkey70864282009-04-07 21:08:40 -07001059 double bestDistance = Double.MAX_VALUE;
Winson Chungaafa03c2010-06-11 17:34:16 -07001060
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001061 final int countX = mCountX;
1062 final int countY = mCountY;
1063 final boolean[][] occupied = mOccupied;
1064
Winson Chungbbc60d82010-11-11 16:34:41 -08001065 for (int y = 0; y < countY - (spanY - 1); y++) {
Michael Jurkac28de512010-08-13 11:27:44 -07001066 inner:
Winson Chungbbc60d82010-11-11 16:34:41 -08001067 for (int x = 0; x < countX - (spanX - 1); x++) {
Michael Jurkac28de512010-08-13 11:27:44 -07001068 for (int i = 0; i < spanX; i++) {
1069 for (int j = 0; j < spanY; j++) {
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001070 if (occupied[x + i][y + j]) {
Winson Chungbbc60d82010-11-11 16:34:41 -08001071 // small optimization: we can skip to after the column we just found
Michael Jurkac28de512010-08-13 11:27:44 -07001072 // an occupied cell
Winson Chungbbc60d82010-11-11 16:34:41 -08001073 x += i;
Michael Jurkac28de512010-08-13 11:27:44 -07001074 continue inner;
1075 }
1076 }
1077 }
1078 final int[] cellXY = mTmpCellXY;
1079 cellToPoint(x, y, cellXY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001080
Michael Jurkac28de512010-08-13 11:27:44 -07001081 double distance = Math.sqrt(Math.pow(cellXY[0] - pixelX, 2)
1082 + Math.pow(cellXY[1] - pixelY, 2));
1083 if (distance <= bestDistance) {
1084 bestDistance = distance;
1085 bestXY[0] = x;
1086 bestXY[1] = y;
1087 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001088 }
1089 }
Michael Jurkac6ee42e2010-09-30 12:04:50 -07001090 // re-mark space taken by ignoreView as occupied
1091 markCellsAsOccupiedForView(ignoreView);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001092
Winson Chungaafa03c2010-06-11 17:34:16 -07001093 // Return null if no suitable location found
Jeff Sharkey70864282009-04-07 21:08:40 -07001094 if (bestDistance < Double.MAX_VALUE) {
1095 return bestXY;
1096 } else {
1097 return null;
1098 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001099 }
Winson Chungaafa03c2010-06-11 17:34:16 -07001100
Michael Jurka0280c3b2010-09-17 15:00:07 -07001101 boolean existsEmptyCell() {
1102 return findCellForSpan(null, 1, 1);
1103 }
1104
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001105 /**
Michael Jurka0280c3b2010-09-17 15:00:07 -07001106 * Finds the upper-left coordinate of the first rectangle in the grid that can
1107 * hold a cell of the specified dimensions. If intersectX and intersectY are not -1,
1108 * then this method will only return coordinates for rectangles that contain the cell
1109 * (intersectX, intersectY)
1110 *
1111 * @param cellXY The array that will contain the position of a vacant cell if such a cell
1112 * can be found.
1113 * @param spanX The horizontal span of the cell we want to find.
1114 * @param spanY The vertical span of the cell we want to find.
1115 *
1116 * @return True if a vacant cell of the specified dimension was found, false otherwise.
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001117 */
Michael Jurka0280c3b2010-09-17 15:00:07 -07001118 boolean findCellForSpan(int[] cellXY, int spanX, int spanY) {
1119 return findCellForSpanThatIntersectsIgnoring(cellXY, spanX, spanY, -1, -1, null);
1120 }
1121
1122 /**
1123 * Like above, but ignores any cells occupied by the item "ignoreView"
1124 *
1125 * @param cellXY The array that will contain the position of a vacant cell if such a cell
1126 * can be found.
1127 * @param spanX The horizontal span of the cell we want to find.
1128 * @param spanY The vertical span of the cell we want to find.
1129 * @param ignoreView The home screen item we should treat as not occupying any space
1130 * @return
1131 */
1132 boolean findCellForSpanIgnoring(int[] cellXY, int spanX, int spanY, View ignoreView) {
1133 return findCellForSpanThatIntersectsIgnoring(cellXY, spanX, spanY, -1, -1, ignoreView);
1134 }
1135
1136 /**
1137 * Like above, but if intersectX and intersectY are not -1, then this method will try to
1138 * return coordinates for rectangles that contain the cell [intersectX, intersectY]
1139 *
1140 * @param spanX The horizontal span of the cell we want to find.
1141 * @param spanY The vertical span of the cell we want to find.
1142 * @param ignoreView The home screen item we should treat as not occupying any space
1143 * @param intersectX The X coordinate of the cell that we should try to overlap
1144 * @param intersectX The Y coordinate of the cell that we should try to overlap
1145 *
1146 * @return True if a vacant cell of the specified dimension was found, false otherwise.
1147 */
1148 boolean findCellForSpanThatIntersects(int[] cellXY, int spanX, int spanY,
1149 int intersectX, int intersectY) {
1150 return findCellForSpanThatIntersectsIgnoring(
1151 cellXY, spanX, spanY, intersectX, intersectY, null);
1152 }
1153
1154 /**
1155 * The superset of the above two methods
1156 */
1157 boolean findCellForSpanThatIntersectsIgnoring(int[] cellXY, int spanX, int spanY,
1158 int intersectX, int intersectY, View ignoreView) {
Michael Jurkac6ee42e2010-09-30 12:04:50 -07001159 // mark space take by ignoreView as available (method checks if ignoreView is null)
1160 markCellsAsUnoccupiedForView(ignoreView);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001161
Michael Jurka28750fb2010-09-24 17:43:49 -07001162 boolean foundCell = false;
Michael Jurka0280c3b2010-09-17 15:00:07 -07001163 while (true) {
1164 int startX = 0;
1165 if (intersectX >= 0) {
1166 startX = Math.max(startX, intersectX - (spanX - 1));
1167 }
1168 int endX = mCountX - (spanX - 1);
1169 if (intersectX >= 0) {
1170 endX = Math.min(endX, intersectX + (spanX - 1) + (spanX == 1 ? 1 : 0));
1171 }
1172 int startY = 0;
1173 if (intersectY >= 0) {
1174 startY = Math.max(startY, intersectY - (spanY - 1));
1175 }
1176 int endY = mCountY - (spanY - 1);
1177 if (intersectY >= 0) {
1178 endY = Math.min(endY, intersectY + (spanY - 1) + (spanY == 1 ? 1 : 0));
1179 }
1180
Winson Chungbbc60d82010-11-11 16:34:41 -08001181 for (int y = startY; y < endY && !foundCell; y++) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001182 inner:
Winson Chungbbc60d82010-11-11 16:34:41 -08001183 for (int x = startX; x < endX; x++) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001184 for (int i = 0; i < spanX; i++) {
1185 for (int j = 0; j < spanY; j++) {
1186 if (mOccupied[x + i][y + j]) {
Winson Chungbbc60d82010-11-11 16:34:41 -08001187 // small optimization: we can skip to after the column we just found
Michael Jurka0280c3b2010-09-17 15:00:07 -07001188 // an occupied cell
Winson Chungbbc60d82010-11-11 16:34:41 -08001189 x += i;
Michael Jurka0280c3b2010-09-17 15:00:07 -07001190 continue inner;
1191 }
1192 }
1193 }
1194 if (cellXY != null) {
1195 cellXY[0] = x;
1196 cellXY[1] = y;
1197 }
Michael Jurka28750fb2010-09-24 17:43:49 -07001198 foundCell = true;
1199 break;
Michael Jurka0280c3b2010-09-17 15:00:07 -07001200 }
1201 }
1202 if (intersectX == -1 && intersectY == -1) {
1203 break;
1204 } else {
1205 // if we failed to find anything, try again but without any requirements of
1206 // intersecting
1207 intersectX = -1;
1208 intersectY = -1;
1209 continue;
1210 }
1211 }
1212
Michael Jurkac6ee42e2010-09-30 12:04:50 -07001213 // re-mark space taken by ignoreView as occupied
1214 markCellsAsOccupiedForView(ignoreView);
Michael Jurka28750fb2010-09-24 17:43:49 -07001215 return foundCell;
Michael Jurka0280c3b2010-09-17 15:00:07 -07001216 }
1217
1218 /**
1219 * Called when drag has left this CellLayout or has been completed (successfully or not)
1220 */
1221 void onDragExit() {
Joe Onorato4be866d2010-10-10 11:26:02 -07001222 // This can actually be called when we aren't in a drag, e.g. when adding a new
1223 // item to this layout via the customize drawer.
1224 // Guard against that case.
1225 if (mDragging) {
1226 mDragging = false;
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001227
Joe Onorato4be866d2010-10-10 11:26:02 -07001228 // Fade out the drag indicators
1229 if (mCrosshairsAnimator != null) {
1230 mCrosshairsAnimator.animateOut();
1231 }
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001232 }
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -07001233
1234 // Invalidate the drag data
1235 mDragCell[0] = -1;
1236 mDragCell[1] = -1;
1237 mDragOutlineAnims[mDragOutlineCurrent].animateOut();
1238 mDragOutlineCurrent = (mDragOutlineCurrent + 1) % mDragOutlineAnims.length;
1239
Michael Jurka33945b22010-12-21 18:19:38 -08001240 setIsDragOverlapping(false);
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001241 }
1242
1243 /**
Winson Chungaafa03c2010-06-11 17:34:16 -07001244 * Mark a child as having been dropped.
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001245 * At the beginning of the drag operation, the child may have been on another
Patrick Dubroyce34a972010-10-19 10:34:32 -07001246 * screen, but it is re-parented before this method is called.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001247 *
1248 * @param child The child that is being dropped
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001249 */
Michael Jurkad3ef3062010-11-23 16:23:58 -08001250 void onDropChild(View child, boolean animate) {
Romain Guyd94533d2009-08-17 10:01:15 -07001251 if (child != null) {
1252 LayoutParams lp = (LayoutParams) child.getLayoutParams();
Romain Guyd94533d2009-08-17 10:01:15 -07001253 lp.isDragging = false;
Romain Guy84f296c2009-11-04 15:00:44 -08001254 lp.dropped = true;
Michael Jurkad3ef3062010-11-23 16:23:58 -08001255 lp.animateDrop = animate;
Patrick Dubroyce34a972010-10-19 10:34:32 -07001256 child.setVisibility(View.VISIBLE);
Romain Guyd94533d2009-08-17 10:01:15 -07001257 child.requestLayout();
Romain Guyd94533d2009-08-17 10:01:15 -07001258 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001259 }
1260
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001261 /**
1262 * Start dragging the specified child
Winson Chungaafa03c2010-06-11 17:34:16 -07001263 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001264 * @param child The child that is being dragged
1265 */
1266 void onDragChild(View child) {
1267 LayoutParams lp = (LayoutParams) child.getLayoutParams();
1268 lp.isDragging = true;
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001269 }
1270
1271 /**
1272 * A drag event has begun over this layout.
1273 * It may have begun over this layout (in which case onDragChild is called first),
1274 * or it may have begun on another layout.
1275 */
Winson Chunga9abd0e2010-10-27 17:18:37 -07001276 void onDragEnter() {
Patrick Dubroyfe6bd872010-10-13 17:32:10 -07001277 if (!mDragging) {
Patrick Dubroyfe6bd872010-10-13 17:32:10 -07001278 // Fade in the drag indicators
1279 if (mCrosshairsAnimator != null) {
1280 mCrosshairsAnimator.animateIn();
1281 }
Joe Onorato4be866d2010-10-10 11:26:02 -07001282 }
1283 mDragging = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001284 }
Winson Chungaafa03c2010-06-11 17:34:16 -07001285
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001286 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001287 * Computes a bounding rectangle for a range of cells
Winson Chungaafa03c2010-06-11 17:34:16 -07001288 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001289 * @param cellX X coordinate of upper left corner expressed as a cell position
1290 * @param cellY Y coordinate of upper left corner expressed as a cell position
Winson Chungaafa03c2010-06-11 17:34:16 -07001291 * @param cellHSpan Width in cells
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001292 * @param cellVSpan Height in cells
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001293 * @param resultRect Rect into which to put the results
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001294 */
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001295 public void cellToRect(int cellX, int cellY, int cellHSpan, int cellVSpan, RectF resultRect) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001296 final int cellWidth = mCellWidth;
1297 final int cellHeight = mCellHeight;
1298 final int widthGap = mWidthGap;
1299 final int heightGap = mHeightGap;
Winson Chungaafa03c2010-06-11 17:34:16 -07001300
1301 final int hStartPadding = getLeftPadding();
1302 final int vStartPadding = getTopPadding();
1303
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001304 int width = cellHSpan * cellWidth + ((cellHSpan - 1) * widthGap);
1305 int height = cellVSpan * cellHeight + ((cellVSpan - 1) * heightGap);
1306
1307 int x = hStartPadding + cellX * (cellWidth + widthGap);
1308 int y = vStartPadding + cellY * (cellHeight + heightGap);
Winson Chungaafa03c2010-06-11 17:34:16 -07001309
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001310 resultRect.set(x, y, x + width, y + height);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001311 }
Winson Chungaafa03c2010-06-11 17:34:16 -07001312
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001313 /**
Winson Chungaafa03c2010-06-11 17:34:16 -07001314 * Computes the required horizontal and vertical cell spans to always
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001315 * fit the given rectangle.
Winson Chungaafa03c2010-06-11 17:34:16 -07001316 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001317 * @param width Width in pixels
1318 * @param height Height in pixels
Patrick Dubroy8f86ddc2010-07-16 13:55:32 -07001319 * @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 -08001320 */
Patrick Dubroy8f86ddc2010-07-16 13:55:32 -07001321 public int[] rectToCell(int width, int height, int[] result) {
Michael Jurka9987a5c2010-10-08 16:58:12 -07001322 return rectToCell(getResources(), width, height, result);
1323 }
1324
1325 public static int[] rectToCell(Resources resources, int width, int height, int[] result) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001326 // Always assume we're working with the smallest span to make sure we
1327 // reserve enough space in both orientations.
Joe Onorato79e56262009-09-21 15:23:04 -04001328 int actualWidth = resources.getDimensionPixelSize(R.dimen.workspace_cell_width);
1329 int actualHeight = resources.getDimensionPixelSize(R.dimen.workspace_cell_height);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001330 int smallerSize = Math.min(actualWidth, actualHeight);
Joe Onorato79e56262009-09-21 15:23:04 -04001331
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001332 // Always round up to next largest cell
1333 int spanX = (width + smallerSize) / smallerSize;
1334 int spanY = (height + smallerSize) / smallerSize;
Joe Onorato79e56262009-09-21 15:23:04 -04001335
Patrick Dubroy8f86ddc2010-07-16 13:55:32 -07001336 if (result == null) {
1337 return new int[] { spanX, spanY };
1338 }
1339 result[0] = spanX;
1340 result[1] = spanY;
1341 return result;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001342 }
1343
1344 /**
Patrick Dubroy047379a2010-12-19 22:02:04 -08001345 * Calculate the grid spans needed to fit given item
1346 */
1347 public void calculateSpans(ItemInfo info) {
1348 final int minWidth;
1349 final int minHeight;
1350
1351 if (info instanceof LauncherAppWidgetInfo) {
1352 minWidth = ((LauncherAppWidgetInfo) info).minWidth;
1353 minHeight = ((LauncherAppWidgetInfo) info).minHeight;
1354 } else if (info instanceof PendingAddWidgetInfo) {
1355 minWidth = ((PendingAddWidgetInfo) info).minWidth;
1356 minHeight = ((PendingAddWidgetInfo) info).minHeight;
1357 } else {
1358 // It's not a widget, so it must be 1x1
1359 info.spanX = info.spanY = 1;
1360 return;
1361 }
1362 int[] spans = rectToCell(minWidth, minHeight, null);
1363 info.spanX = spans[0];
1364 info.spanY = spans[1];
1365 }
1366
1367 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001368 * Find the first vacant cell, if there is one.
1369 *
1370 * @param vacant Holds the x and y coordinate of the vacant cell
1371 * @param spanX Horizontal cell span.
1372 * @param spanY Vertical cell span.
Winson Chungaafa03c2010-06-11 17:34:16 -07001373 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001374 * @return True if a vacant cell was found
1375 */
1376 public boolean getVacantCell(int[] vacant, int spanX, int spanY) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001377
Michael Jurka0280c3b2010-09-17 15:00:07 -07001378 return findVacantCell(vacant, spanX, spanY, mCountX, mCountY, mOccupied);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001379 }
1380
1381 static boolean findVacantCell(int[] vacant, int spanX, int spanY,
1382 int xCount, int yCount, boolean[][] occupied) {
1383
1384 for (int x = 0; x < xCount; x++) {
1385 for (int y = 0; y < yCount; y++) {
1386 boolean available = !occupied[x][y];
1387out: for (int i = x; i < x + spanX - 1 && x < xCount; i++) {
1388 for (int j = y; j < y + spanY - 1 && y < yCount; j++) {
1389 available = available && !occupied[i][j];
1390 if (!available) break out;
1391 }
1392 }
1393
1394 if (available) {
1395 vacant[0] = x;
1396 vacant[1] = y;
1397 return true;
1398 }
1399 }
1400 }
1401
1402 return false;
1403 }
1404
Michael Jurka0280c3b2010-09-17 15:00:07 -07001405 private void clearOccupiedCells() {
1406 for (int x = 0; x < mCountX; x++) {
1407 for (int y = 0; y < mCountY; y++) {
1408 mOccupied[x][y] = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001409 }
1410 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001411 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001412
Michael Jurka0280c3b2010-09-17 15:00:07 -07001413 public void onMove(View view, int newCellX, int newCellY) {
1414 LayoutParams lp = (LayoutParams) view.getLayoutParams();
1415 markCellsAsUnoccupiedForView(view);
1416 markCellsForView(newCellX, newCellY, lp.cellHSpan, lp.cellVSpan, true);
1417 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001418
Michael Jurka0280c3b2010-09-17 15:00:07 -07001419 private void markCellsAsOccupiedForView(View view) {
Michael Jurkac6ee42e2010-09-30 12:04:50 -07001420 if (view == null || view.getParent() != this) return;
Michael Jurka0280c3b2010-09-17 15:00:07 -07001421 LayoutParams lp = (LayoutParams) view.getLayoutParams();
1422 markCellsForView(lp.cellX, lp.cellY, lp.cellHSpan, lp.cellVSpan, true);
1423 }
1424
1425 private void markCellsAsUnoccupiedForView(View view) {
Michael Jurkac6ee42e2010-09-30 12:04:50 -07001426 if (view == null || view.getParent() != this) return;
Michael Jurka0280c3b2010-09-17 15:00:07 -07001427 LayoutParams lp = (LayoutParams) view.getLayoutParams();
1428 markCellsForView(lp.cellX, lp.cellY, lp.cellHSpan, lp.cellVSpan, false);
1429 }
1430
1431 private void markCellsForView(int cellX, int cellY, int spanX, int spanY, boolean value) {
1432 for (int x = cellX; x < cellX + spanX && x < mCountX; x++) {
1433 for (int y = cellY; y < cellY + spanY && y < mCountY; y++) {
1434 mOccupied[x][y] = value;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001435 }
1436 }
1437 }
1438
1439 @Override
1440 public ViewGroup.LayoutParams generateLayoutParams(AttributeSet attrs) {
1441 return new CellLayout.LayoutParams(getContext(), attrs);
1442 }
1443
1444 @Override
1445 protected boolean checkLayoutParams(ViewGroup.LayoutParams p) {
1446 return p instanceof CellLayout.LayoutParams;
1447 }
1448
1449 @Override
1450 protected ViewGroup.LayoutParams generateLayoutParams(ViewGroup.LayoutParams p) {
1451 return new CellLayout.LayoutParams(p);
1452 }
1453
Winson Chungaafa03c2010-06-11 17:34:16 -07001454 public static class CellLayoutAnimationController extends LayoutAnimationController {
1455 public CellLayoutAnimationController(Animation animation, float delay) {
1456 super(animation, delay);
1457 }
1458
1459 @Override
1460 protected long getDelayForView(View view) {
1461 return (int) (Math.random() * 150);
1462 }
1463 }
1464
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001465 public static class LayoutParams extends ViewGroup.MarginLayoutParams {
1466 /**
1467 * Horizontal location of the item in the grid.
1468 */
1469 @ViewDebug.ExportedProperty
1470 public int cellX;
1471
1472 /**
1473 * Vertical location of the item in the grid.
1474 */
1475 @ViewDebug.ExportedProperty
1476 public int cellY;
1477
1478 /**
1479 * Number of cells spanned horizontally by the item.
1480 */
1481 @ViewDebug.ExportedProperty
1482 public int cellHSpan;
1483
1484 /**
1485 * Number of cells spanned vertically by the item.
1486 */
1487 @ViewDebug.ExportedProperty
1488 public int cellVSpan;
Winson Chungaafa03c2010-06-11 17:34:16 -07001489
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001490 /**
1491 * Is this item currently being dragged
1492 */
1493 public boolean isDragging;
1494
1495 // X coordinate of the view in the layout.
1496 @ViewDebug.ExportedProperty
1497 int x;
1498 // Y coordinate of the view in the layout.
1499 @ViewDebug.ExportedProperty
1500 int y;
1501
Patrick Dubroyce34a972010-10-19 10:34:32 -07001502 /**
1503 * The old X coordinate of this item, relative to its current parent.
1504 * Used to animate the item into its new position.
1505 */
1506 int oldX;
1507
1508 /**
1509 * The old Y coordinate of this item, relative to its current parent.
1510 * Used to animate the item into its new position.
1511 */
1512 int oldY;
1513
Romain Guy84f296c2009-11-04 15:00:44 -08001514 boolean dropped;
Romain Guyfcb9e712009-10-02 16:06:52 -07001515
Michael Jurkad3ef3062010-11-23 16:23:58 -08001516 boolean animateDrop;
1517
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001518 public LayoutParams(Context c, AttributeSet attrs) {
1519 super(c, attrs);
1520 cellHSpan = 1;
1521 cellVSpan = 1;
1522 }
1523
1524 public LayoutParams(ViewGroup.LayoutParams source) {
1525 super(source);
1526 cellHSpan = 1;
1527 cellVSpan = 1;
1528 }
Winson Chungaafa03c2010-06-11 17:34:16 -07001529
1530 public LayoutParams(LayoutParams source) {
1531 super(source);
1532 this.cellX = source.cellX;
1533 this.cellY = source.cellY;
1534 this.cellHSpan = source.cellHSpan;
1535 this.cellVSpan = source.cellVSpan;
1536 }
1537
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001538 public LayoutParams(int cellX, int cellY, int cellHSpan, int cellVSpan) {
Romain Guy8f19cdd2010-01-08 15:07:00 -08001539 super(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001540 this.cellX = cellX;
1541 this.cellY = cellY;
1542 this.cellHSpan = cellHSpan;
1543 this.cellVSpan = cellVSpan;
1544 }
1545
1546 public void setup(int cellWidth, int cellHeight, int widthGap, int heightGap,
1547 int hStartPadding, int vStartPadding) {
Winson Chungaafa03c2010-06-11 17:34:16 -07001548
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001549 final int myCellHSpan = cellHSpan;
1550 final int myCellVSpan = cellVSpan;
1551 final int myCellX = cellX;
1552 final int myCellY = cellY;
Winson Chungaafa03c2010-06-11 17:34:16 -07001553
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001554 width = myCellHSpan * cellWidth + ((myCellHSpan - 1) * widthGap) -
1555 leftMargin - rightMargin;
1556 height = myCellVSpan * cellHeight + ((myCellVSpan - 1) * heightGap) -
1557 topMargin - bottomMargin;
1558
1559 x = hStartPadding + myCellX * (cellWidth + widthGap) + leftMargin;
1560 y = vStartPadding + myCellY * (cellHeight + heightGap) + topMargin;
1561 }
Winson Chungaafa03c2010-06-11 17:34:16 -07001562
1563 public String toString() {
1564 return "(" + this.cellX + ", " + this.cellY + ")";
1565 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001566 }
1567
Michael Jurka0280c3b2010-09-17 15:00:07 -07001568 // This class stores info for two purposes:
1569 // 1. When dragging items (mDragInfo in Workspace), we store the View, its cellX & cellY,
1570 // its spanX, spanY, and the screen it is on
1571 // 2. When long clicking on an empty cell in a CellLayout, we save information about the
1572 // cellX and cellY coordinates and which page was clicked. We then set this as a tag on
1573 // the CellLayout that was long clicked
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001574 static final class CellInfo implements ContextMenu.ContextMenuInfo {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001575 View cell;
Michael Jurkaa63c4522010-08-19 13:52:27 -07001576 int cellX = -1;
1577 int cellY = -1;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001578 int spanX;
1579 int spanY;
1580 int screen;
1581 boolean valid;
1582
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001583 @Override
1584 public String toString() {
Winson Chungaafa03c2010-06-11 17:34:16 -07001585 return "Cell[view=" + (cell == null ? "null" : cell.getClass())
1586 + ", x=" + cellX + ", y=" + cellY + "]";
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001587 }
1588 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001589}