blob: 95c976ba834482b95eaf8e96f1966ecc7311ac18 [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
Winson Chunga9abd0e2010-10-27 17:18:37 -070019import java.util.Arrays;
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;
Winson Chunga9abd0e2010-10-27 17:18:37 -070029import android.content.ClipDescription;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080030import android.content.Context;
Joe Onorato79e56262009-09-21 15:23:04 -040031import android.content.res.Resources;
Winson Chungaafa03c2010-06-11 17:34:16 -070032import android.content.res.TypedArray;
Joe Onorato4be866d2010-10-10 11:26:02 -070033import android.graphics.Bitmap;
Winson Chungaafa03c2010-06-11 17:34:16 -070034import android.graphics.Canvas;
Joe Onorato4be866d2010-10-10 11:26:02 -070035import android.graphics.Paint;
Patrick Dubroyde7658b2010-09-27 11:15:43 -070036import android.graphics.Point;
37import android.graphics.PointF;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080038import android.graphics.Rect;
39import android.graphics.RectF;
Michael Jurka18014792010-10-14 09:01:34 -070040import android.graphics.Region;
Patrick Dubroy6569f2c2010-07-12 14:25:18 -070041import android.graphics.drawable.Drawable;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080042import android.util.AttributeSet;
Joe Onorato4be866d2010-10-10 11:26:02 -070043import android.util.Log;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080044import android.view.ContextMenu;
Winson Chunga9abd0e2010-10-27 17:18:37 -070045import android.view.DragEvent;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080046import 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
Winson Chunga9abd0e2010-10-27 17:18:37 -070054import com.android.launcher.R;
Patrick Dubroy8e58e912010-10-14 13:21:48 -070055
Adam Cohenf34bab52010-09-30 14:11:56 -070056public class CellLayout extends ViewGroup implements Dimmable {
Winson Chungaafa03c2010-06-11 17:34:16 -070057 static final String TAG = "CellLayout";
58
The Android Open Source Project31dd5032009-03-03 19:32:27 -080059 private int mCellWidth;
60 private int mCellHeight;
Winson Chungaafa03c2010-06-11 17:34:16 -070061
Winson Chungaafa03c2010-06-11 17:34:16 -070062 private int mLeftPadding;
63 private int mRightPadding;
64 private int mTopPadding;
65 private int mBottomPadding;
66
Adam Cohend22015c2010-07-26 22:02:18 -070067 private int mCountX;
68 private int mCountY;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080069
70 private int mWidthGap;
71 private int mHeightGap;
72
73 private final Rect mRect = new Rect();
Patrick Dubroy8f86ddc2010-07-16 13:55:32 -070074 private final RectF mRectF = new RectF();
The Android Open Source Project31dd5032009-03-03 19:32:27 -080075 private final CellInfo mCellInfo = new CellInfo();
Winson Chungaafa03c2010-06-11 17:34:16 -070076
Patrick Dubroyde7658b2010-09-27 11:15:43 -070077 // These are temporary variables to prevent having to allocate a new object just to
78 // return an (x, y) value from helper functions. Do NOT use them to maintain other state.
Patrick Dubroy6569f2c2010-07-12 14:25:18 -070079 private final int[] mTmpCellXY = new int[2];
Patrick Dubroyde7658b2010-09-27 11:15:43 -070080 private final int[] mTmpPoint = new int[2];
81 private final PointF mTmpPointF = new PointF();
Patrick Dubroy6569f2c2010-07-12 14:25:18 -070082
The Android Open Source Project31dd5032009-03-03 19:32:27 -080083 boolean[][] mOccupied;
84
Michael Jurkadee05892010-07-27 10:01:56 -070085 private OnTouchListener mInterceptTouchListener;
86
Michael Jurka5f1c5092010-09-03 14:15:02 -070087 private float mBackgroundAlpha;
Adam Cohen1b0aaac2010-10-28 11:11:18 -070088 private float mBackgroundAlphaMultiplier = 1.0f;
Adam Cohenf34bab52010-09-30 14:11:56 -070089
Michael Jurka5f1c5092010-09-03 14:15:02 -070090 private Drawable mBackground;
Adam Cohenf34bab52010-09-30 14:11:56 -070091 private Drawable mBackgroundMini;
92 private Drawable mBackgroundMiniHover;
Patrick Dubroy1262e362010-10-06 15:49:50 -070093 private Drawable mBackgroundHover;
Michael Jurka3e7c7632010-10-02 16:01:03 -070094 private Drawable mBackgroundMiniAcceptsDrops;
Michael Jurka18014792010-10-14 09:01:34 -070095 private Rect mBackgroundRect;
96 private Rect mHoverRect;
97 private float mHoverScale;
98 private float mHoverAlpha;
Michael Jurka3e7c7632010-10-02 16:01:03 -070099 private boolean mAcceptsDrops;
Patrick Dubroy1262e362010-10-06 15:49:50 -0700100
101 // If we're actively dragging something over this screen, mHover is true
Michael Jurkaa63c4522010-08-19 13:52:27 -0700102 private boolean mHover = false;
Michael Jurkadee05892010-07-27 10:01:56 -0700103
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700104 private final Point mDragCenter = new Point();
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700105
Winson Chung150fbab2010-09-29 17:14:26 -0700106 // These arrays are used to implement the drag visualization on x-large screens.
Joe Onorato4be866d2010-10-10 11:26:02 -0700107 // They are used as circular arrays, indexed by mDragOutlineCurrent.
108 private Point[] mDragOutlines = new Point[8];
Chet Haase472b2812010-10-14 07:02:04 -0700109 private float[] mDragOutlineAlphas = new float[mDragOutlines.length];
Joe Onorato4be866d2010-10-10 11:26:02 -0700110 private InterruptibleInOutAnimator[] mDragOutlineAnims =
111 new InterruptibleInOutAnimator[mDragOutlines.length];
Winson Chung150fbab2010-09-29 17:14:26 -0700112
113 // Used as an index into the above 3 arrays; indicates which is the most current value.
Joe Onorato4be866d2010-10-10 11:26:02 -0700114 private int mDragOutlineCurrent = 0;
Patrick Dubroy8e58e912010-10-14 13:21:48 -0700115 private final Paint mDragOutlinePaint = new Paint();
Winson Chung150fbab2010-09-29 17:14:26 -0700116
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700117 private Drawable mCrosshairsDrawable = null;
Patrick Dubroy49250ad2010-10-08 15:33:52 -0700118 private InterruptibleInOutAnimator mCrosshairsAnimator = null;
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700119 private float mCrosshairsVisibility = 0.0f;
120
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700121 // When a drag operation is in progress, holds the nearest cell to the touch point
122 private final int[] mDragCell = new int[2];
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800123
Winson Chungaafa03c2010-06-11 17:34:16 -0700124 private final WallpaperManager mWallpaperManager;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800125
Joe Onorato4be866d2010-10-10 11:26:02 -0700126 private boolean mDragging = false;
127
Patrick Dubroyce34a972010-10-19 10:34:32 -0700128 private TimeInterpolator mEaseOutInterpolator;
129
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800130 public CellLayout(Context context) {
131 this(context, null);
132 }
133
134 public CellLayout(Context context, AttributeSet attrs) {
135 this(context, attrs, 0);
136 }
137
138 public CellLayout(Context context, AttributeSet attrs, int defStyle) {
139 super(context, attrs, defStyle);
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700140
141 // A ViewGroup usually does not draw, but CellLayout needs to draw a rectangle to show
142 // the user where a dragged item will land when dropped.
143 setWillNotDraw(false);
Michael Jurkaa63c4522010-08-19 13:52:27 -0700144
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800145 TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.CellLayout, defStyle, 0);
146
147 mCellWidth = a.getDimensionPixelSize(R.styleable.CellLayout_cellWidth, 10);
148 mCellHeight = a.getDimensionPixelSize(R.styleable.CellLayout_cellHeight, 10);
Winson Chungece7f5b2010-10-22 14:54:12 -0700149 mWidthGap = a.getDimensionPixelSize(R.styleable.CellLayout_widthGap, -1);
150 mHeightGap = a.getDimensionPixelSize(R.styleable.CellLayout_heightGap, -1);
Winson Chungaafa03c2010-06-11 17:34:16 -0700151
Adam Cohend22015c2010-07-26 22:02:18 -0700152 mLeftPadding =
153 a.getDimensionPixelSize(R.styleable.CellLayout_xAxisStartPadding, 10);
154 mRightPadding =
155 a.getDimensionPixelSize(R.styleable.CellLayout_xAxisEndPadding, 10);
156 mTopPadding =
157 a.getDimensionPixelSize(R.styleable.CellLayout_yAxisStartPadding, 10);
158 mBottomPadding =
159 a.getDimensionPixelSize(R.styleable.CellLayout_yAxisEndPadding, 10);
Winson Chungaafa03c2010-06-11 17:34:16 -0700160
Adam Cohend22015c2010-07-26 22:02:18 -0700161 mCountX = LauncherModel.getCellCountX();
162 mCountY = LauncherModel.getCellCountY();
Michael Jurka0280c3b2010-09-17 15:00:07 -0700163 mOccupied = new boolean[mCountX][mCountY];
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800164
165 a.recycle();
166
167 setAlwaysDrawnWithCacheEnabled(false);
168
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700169 mWallpaperManager = WallpaperManager.getInstance(context);
170
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700171 final Resources res = getResources();
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700172
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700173 if (LauncherApplication.isScreenXLarge()) {
Winson Chung150fbab2010-09-29 17:14:26 -0700174 mBackgroundMini = res.getDrawable(R.drawable.mini_home_screen_bg);
Adam Cohenf34bab52010-09-30 14:11:56 -0700175 mBackgroundMini.setFilterBitmap(true);
Winson Chung150fbab2010-09-29 17:14:26 -0700176 mBackground = res.getDrawable(R.drawable.home_screen_bg);
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700177 mBackground.setFilterBitmap(true);
Winson Chung150fbab2010-09-29 17:14:26 -0700178 mBackgroundMiniHover = res.getDrawable(R.drawable.mini_home_screen_bg_hover);
Adam Cohenf34bab52010-09-30 14:11:56 -0700179 mBackgroundMiniHover.setFilterBitmap(true);
Patrick Dubroy1262e362010-10-06 15:49:50 -0700180 mBackgroundHover = res.getDrawable(R.drawable.home_screen_bg_hover);
181 mBackgroundHover.setFilterBitmap(true);
Michael Jurka3e7c7632010-10-02 16:01:03 -0700182 mBackgroundMiniAcceptsDrops = res.getDrawable(
183 R.drawable.mini_home_screen_bg_accepts_drops);
184 mBackgroundMiniAcceptsDrops.setFilterBitmap(true);
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700185 }
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700186
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700187 // Initialize the data structures used for the drag visualization.
Winson Chung150fbab2010-09-29 17:14:26 -0700188
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700189 mCrosshairsDrawable = res.getDrawable(R.drawable.gardening_crosshairs);
Patrick Dubroyce34a972010-10-19 10:34:32 -0700190 mEaseOutInterpolator = new DecelerateInterpolator(2.5f); // Quint ease out
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700191
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700192 // Set up the animation for fading the crosshairs in and out
193 int animDuration = res.getInteger(R.integer.config_crosshairsFadeInTime);
Patrick Dubroy49250ad2010-10-08 15:33:52 -0700194 mCrosshairsAnimator = new InterruptibleInOutAnimator(animDuration, 0.0f, 1.0f);
Chet Haase472b2812010-10-14 07:02:04 -0700195 mCrosshairsAnimator.getAnimator().addUpdateListener(new AnimatorUpdateListener() {
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700196 public void onAnimationUpdate(ValueAnimator animation) {
197 mCrosshairsVisibility = ((Float) animation.getAnimatedValue()).floatValue();
Patrick Dubroy8e58e912010-10-14 13:21:48 -0700198 invalidate();
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700199 }
200 });
Patrick Dubroyce34a972010-10-19 10:34:32 -0700201 mCrosshairsAnimator.getAnimator().setInterpolator(mEaseOutInterpolator);
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700202
Joe Onorato4be866d2010-10-10 11:26:02 -0700203 for (int i = 0; i < mDragOutlines.length; i++) {
204 mDragOutlines[i] = new Point(-1, -1);
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700205 }
206
207 // When dragging things around the home screens, we show a green outline of
208 // where the item will land. The outlines gradually fade out, leaving a trail
209 // behind the drag path.
210 // Set up all the animations that are used to implement this fading.
211 final int duration = res.getInteger(R.integer.config_dragOutlineFadeTime);
Chet Haase472b2812010-10-14 07:02:04 -0700212 final float fromAlphaValue = 0;
213 final float toAlphaValue = (float)res.getInteger(R.integer.config_dragOutlineMaxAlpha);
Joe Onorato4be866d2010-10-10 11:26:02 -0700214
Patrick Dubroy8e58e912010-10-14 13:21:48 -0700215 Arrays.fill(mDragOutlineAlphas, fromAlphaValue);
Joe Onorato4be866d2010-10-10 11:26:02 -0700216
217 for (int i = 0; i < mDragOutlineAnims.length; i++) {
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700218 final InterruptibleInOutAnimator anim =
219 new InterruptibleInOutAnimator(duration, fromAlphaValue, toAlphaValue);
Patrick Dubroyce34a972010-10-19 10:34:32 -0700220 anim.getAnimator().setInterpolator(mEaseOutInterpolator);
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700221 final int thisIndex = i;
Chet Haase472b2812010-10-14 07:02:04 -0700222 anim.getAnimator().addUpdateListener(new AnimatorUpdateListener() {
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700223 public void onAnimationUpdate(ValueAnimator animation) {
Joe Onorato4be866d2010-10-10 11:26:02 -0700224 final Bitmap outline = (Bitmap)anim.getTag();
225
226 // If an animation is started and then stopped very quickly, we can still
227 // get spurious updates we've cleared the tag. Guard against this.
228 if (outline == null) {
Patrick Dubroyfe6bd872010-10-13 17:32:10 -0700229 if (false) {
230 Object val = animation.getAnimatedValue();
231 Log.d(TAG, "anim " + thisIndex + " update: " + val +
232 ", isStopped " + anim.isStopped());
233 }
Joe Onorato4be866d2010-10-10 11:26:02 -0700234 // Try to prevent it from continuing to run
235 animation.cancel();
236 } else {
Chet Haase472b2812010-10-14 07:02:04 -0700237 mDragOutlineAlphas[thisIndex] = (Float) animation.getAnimatedValue();
Joe Onorato4be866d2010-10-10 11:26:02 -0700238 final int left = mDragOutlines[thisIndex].x;
239 final int top = mDragOutlines[thisIndex].y;
240 CellLayout.this.invalidate(left, top,
241 left + outline.getWidth(), top + outline.getHeight());
242 }
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700243 }
244 });
Joe Onorato4be866d2010-10-10 11:26:02 -0700245 // The animation holds a reference to the drag outline bitmap as long is it's
246 // running. This way the bitmap can be GCed when the animations are complete.
Chet Haase472b2812010-10-14 07:02:04 -0700247 anim.getAnimator().addListener(new AnimatorListenerAdapter() {
Joe Onorato4be866d2010-10-10 11:26:02 -0700248 public void onAnimationEnd(Animator animation) {
Chet Haase472b2812010-10-14 07:02:04 -0700249 if ((Float) ((ValueAnimator) animation).getAnimatedValue() == 0f) {
Joe Onorato4be866d2010-10-10 11:26:02 -0700250 anim.setTag(null);
251 }
252 }
253 });
254 mDragOutlineAnims[i] = anim;
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700255 }
Patrick Dubroyce34a972010-10-19 10:34:32 -0700256
Michael Jurka18014792010-10-14 09:01:34 -0700257 mBackgroundRect = new Rect();
258 mHoverRect = new Rect();
259 setHoverScale(1.0f);
260 setHoverAlpha(1.0f);
261 }
262
263 private void updateHoverRect() {
264 float marginFraction = (mHoverScale - 1.0f) / 2.0f;
265 int marginX = (int) (marginFraction * (mBackgroundRect.right - mBackgroundRect.left));
266 int marginY = (int) (marginFraction * (mBackgroundRect.bottom - mBackgroundRect.top));
267 mHoverRect.set(mBackgroundRect.left - marginX, mBackgroundRect.top - marginY,
268 mBackgroundRect.right + marginX, mBackgroundRect.bottom + marginY);
269 invalidate();
270 }
271
272 public void setHoverScale(float scaleFactor) {
273 if (scaleFactor != mHoverScale) {
274 mHoverScale = scaleFactor;
275 updateHoverRect();
276 }
277 }
278
279 public float getHoverScale() {
280 return mHoverScale;
281 }
282
283 public float getHoverAlpha() {
284 return mHoverAlpha;
285 }
286
287 public void setHoverAlpha(float alpha) {
288 mHoverAlpha = alpha;
289 invalidate();
290 }
291
292 void animateDrop() {
293 if (LauncherApplication.isScreenXLarge()) {
294 Resources res = getResources();
295 float onDropScale = res.getInteger(R.integer.config_screenOnDropScalePercent) / 100.0f;
296 ObjectAnimator scaleUp = ObjectAnimator.ofFloat(this, "hoverScale", onDropScale);
297 scaleUp.setDuration(res.getInteger(R.integer.config_screenOnDropScaleUpDuration));
298 ObjectAnimator scaleDown = ObjectAnimator.ofFloat(this, "hoverScale", 1.0f);
299 scaleDown.setDuration(res.getInteger(R.integer.config_screenOnDropScaleDownDuration));
300 ObjectAnimator alphaFadeOut = ObjectAnimator.ofFloat(this, "hoverAlpha", 0.0f);
301
302 alphaFadeOut.setStartDelay(res.getInteger(R.integer.config_screenOnDropAlphaFadeDelay));
303 alphaFadeOut.setDuration(res.getInteger(R.integer.config_screenOnDropAlphaFadeDelay));
304
305 AnimatorSet bouncer = new AnimatorSet();
306 bouncer.play(scaleUp).before(scaleDown);
307 bouncer.play(scaleUp).with(alphaFadeOut);
308 bouncer.addListener(new AnimatorListenerAdapter() {
309 public void onAnimationStart(Animator animation) {
310 setHover(true);
311 }
312 public void onAnimationEnd(Animator animation) {
313 setHover(false);
314 setHoverScale(1.0f);
315 setHoverAlpha(1.0f);
316 }
317 });
318 bouncer.start();
319 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800320 }
321
Michael Jurkaa63c4522010-08-19 13:52:27 -0700322 public void setHover(boolean value) {
323 if (mHover != value) {
Michael Jurka3e7c7632010-10-02 16:01:03 -0700324 mHover = value;
Michael Jurkaa63c4522010-08-19 13:52:27 -0700325 invalidate();
326 }
Michael Jurkaa63c4522010-08-19 13:52:27 -0700327 }
328
Patrick Dubroy1262e362010-10-06 15:49:50 -0700329 public void drawChildren(Canvas canvas) {
330 super.dispatchDraw(canvas);
331 }
332
Jeff Sharkey83f111d2009-04-20 21:03:13 -0700333 @Override
Patrick Dubroy1262e362010-10-06 15:49:50 -0700334 protected void onDraw(Canvas canvas) {
Michael Jurka3e7c7632010-10-02 16:01:03 -0700335 // When we're large, we are either drawn in a "hover" state (ie when dragging an item to
336 // a neighboring page) or with just a normal background (if backgroundAlpha > 0.0f)
337 // When we're small, we are either drawn normally or in the "accepts drops" state (during
338 // a drag). However, we also drag the mini hover background *over* one of those two
339 // backgrounds
Michael Jurka5f1c5092010-09-03 14:15:02 -0700340 if (mBackgroundAlpha > 0.0f) {
Adam Cohenf34bab52010-09-30 14:11:56 -0700341 Drawable bg;
Patrick Dubroy1262e362010-10-06 15:49:50 -0700342 if (getScaleX() < 0.5f) {
Michael Jurka3e7c7632010-10-02 16:01:03 -0700343 bg = mAcceptsDrops ? mBackgroundMiniAcceptsDrops : mBackgroundMini;
Adam Cohenf34bab52010-09-30 14:11:56 -0700344 } else {
Patrick Dubroy1262e362010-10-06 15:49:50 -0700345 bg = mHover ? mBackgroundHover : mBackground;
Adam Cohenf34bab52010-09-30 14:11:56 -0700346 }
Adam Cohen9c4949e2010-10-05 12:27:22 -0700347 if (bg != null) {
Adam Cohen1b0aaac2010-10-28 11:11:18 -0700348 bg.setAlpha((int) (mBackgroundAlpha * mBackgroundAlphaMultiplier * 255));
Michael Jurka18014792010-10-14 09:01:34 -0700349 bg.setBounds(mBackgroundRect);
Adam Cohen9c4949e2010-10-05 12:27:22 -0700350 bg.draw(canvas);
351 }
Michael Jurka3e7c7632010-10-02 16:01:03 -0700352 if (mHover && getScaleX() < 0.5f) {
Michael Jurka18014792010-10-14 09:01:34 -0700353 boolean modifiedClipRect = false;
354 if (mHoverScale > 1.0f) {
355 // If the hover background's scale is greater than 1, we'll be drawing outside
356 // the bounds of this CellLayout. Get around that by temporarily increasing the
357 // size of the clip rect
358 float marginFraction = (mHoverScale - 1.0f) / 2.0f;
359 Rect clipRect = canvas.getClipBounds();
360 int marginX = (int) (marginFraction * (clipRect.right - clipRect.left));
361 int marginY = (int) (marginFraction * (clipRect.bottom - clipRect.top));
362 canvas.save(Canvas.CLIP_SAVE_FLAG);
363 canvas.clipRect(-marginX, -marginY,
364 getWidth() + marginX, getHeight() + marginY, Region.Op.REPLACE);
365 modifiedClipRect = true;
366 }
367
368 mBackgroundMiniHover.setAlpha((int) (mBackgroundAlpha * mHoverAlpha * 255));
369 mBackgroundMiniHover.setBounds(mHoverRect);
Michael Jurka3e7c7632010-10-02 16:01:03 -0700370 mBackgroundMiniHover.draw(canvas);
Michael Jurka18014792010-10-14 09:01:34 -0700371 if (modifiedClipRect) {
372 canvas.restore();
373 }
Michael Jurka3e7c7632010-10-02 16:01:03 -0700374 }
Michael Jurkaa63c4522010-08-19 13:52:27 -0700375 }
Romain Guya6abce82009-11-10 02:54:41 -0800376
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700377 if (mCrosshairsVisibility > 0.0f) {
378 final int countX = mCountX;
379 final int countY = mCountY;
380
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700381 final float MAX_ALPHA = 0.4f;
382 final int MAX_VISIBLE_DISTANCE = 600;
383 final float DISTANCE_MULTIPLIER = 0.002f;
384
385 final Drawable d = mCrosshairsDrawable;
386 final int width = d.getIntrinsicWidth();
387 final int height = d.getIntrinsicHeight();
388
389 int x = getLeftPadding() - (mWidthGap / 2) - (width / 2);
390 for (int col = 0; col <= countX; col++) {
391 int y = getTopPadding() - (mHeightGap / 2) - (height / 2);
392 for (int row = 0; row <= countY; row++) {
393 mTmpPointF.set(x - mDragCenter.x, y - mDragCenter.y);
394 float dist = mTmpPointF.length();
395 // Crosshairs further from the drag point are more faint
396 float alpha = Math.min(MAX_ALPHA,
397 DISTANCE_MULTIPLIER * (MAX_VISIBLE_DISTANCE - dist));
398 if (alpha > 0.0f) {
399 d.setBounds(x, y, x + width, y + height);
400 d.setAlpha((int) (alpha * 255 * mCrosshairsVisibility));
401 d.draw(canvas);
402 }
403 y += mCellHeight + mHeightGap;
404 }
405 x += mCellWidth + mWidthGap;
406 }
Joe Onorato4be866d2010-10-10 11:26:02 -0700407 }
Winson Chung150fbab2010-09-29 17:14:26 -0700408
Patrick Dubroy8e58e912010-10-14 13:21:48 -0700409 final Paint paint = mDragOutlinePaint;
Joe Onorato4be866d2010-10-10 11:26:02 -0700410 for (int i = 0; i < mDragOutlines.length; i++) {
Chet Haase472b2812010-10-14 07:02:04 -0700411 final float alpha = mDragOutlineAlphas[i];
Joe Onorato4be866d2010-10-10 11:26:02 -0700412 if (alpha > 0) {
413 final Point p = mDragOutlines[i];
414 final Bitmap b = (Bitmap) mDragOutlineAnims[i].getTag();
Chet Haase472b2812010-10-14 07:02:04 -0700415 paint.setAlpha((int)(alpha + .5f));
Joe Onorato4be866d2010-10-10 11:26:02 -0700416 canvas.drawBitmap(b, p.x, p.y, paint);
Winson Chung150fbab2010-09-29 17:14:26 -0700417 }
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700418 }
419 }
420
Adam Cohenf34bab52010-09-30 14:11:56 -0700421 public void setDimmableProgress(float progress) {
422 for (int i = 0; i < getChildCount(); i++) {
423 Dimmable d = (Dimmable) getChildAt(i);
424 d.setDimmableProgress(progress);
425 }
426 }
427
428 public float getDimmableProgress() {
429 if (getChildCount() > 0) {
430 return ((Dimmable) getChildAt(0)).getDimmableProgress();
431 }
432 return 0.0f;
433 }
434
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700435 @Override
Jeff Sharkey83f111d2009-04-20 21:03:13 -0700436 public void cancelLongPress() {
437 super.cancelLongPress();
438
439 // Cancel long press for all children
440 final int count = getChildCount();
441 for (int i = 0; i < count; i++) {
442 final View child = getChildAt(i);
443 child.cancelLongPress();
444 }
445 }
446
Michael Jurkadee05892010-07-27 10:01:56 -0700447 public void setOnInterceptTouchListener(View.OnTouchListener listener) {
448 mInterceptTouchListener = listener;
449 }
450
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800451 int getCountX() {
Adam Cohend22015c2010-07-26 22:02:18 -0700452 return mCountX;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800453 }
454
455 int getCountY() {
Adam Cohend22015c2010-07-26 22:02:18 -0700456 return mCountY;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800457 }
458
Winson Chungaafa03c2010-06-11 17:34:16 -0700459 public boolean addViewToCellLayout(View child, int index, int childId, LayoutParams params) {
Michael Jurkaf3ca3ab2010-10-20 17:08:24 -0700460 return addViewToCellLayout(child, index, childId, params, true);
461 }
462
463 public boolean addViewToCellLayout(
464 View child, int index, int childId, LayoutParams params, boolean markCells) {
Winson Chungaafa03c2010-06-11 17:34:16 -0700465 final LayoutParams lp = params;
466
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800467 // Generate an id for each view, this assumes we have at most 256x256 cells
468 // per workspace screen
Adam Cohend22015c2010-07-26 22:02:18 -0700469 if (lp.cellX >= 0 && lp.cellX <= mCountX - 1 && lp.cellY >= 0 && lp.cellY <= mCountY - 1) {
Winson Chungaafa03c2010-06-11 17:34:16 -0700470 // If the horizontal or vertical span is set to -1, it is taken to
471 // mean that it spans the extent of the CellLayout
Adam Cohend22015c2010-07-26 22:02:18 -0700472 if (lp.cellHSpan < 0) lp.cellHSpan = mCountX;
473 if (lp.cellVSpan < 0) lp.cellVSpan = mCountY;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800474
Winson Chungaafa03c2010-06-11 17:34:16 -0700475 child.setId(childId);
476
Michael Jurkadee05892010-07-27 10:01:56 -0700477 // We might be in the middle or end of shrinking/fading to a dimmed view
478 // Make sure this view's alpha is set the same as all the rest of the views
Michael Jurka5f1c5092010-09-03 14:15:02 -0700479 child.setAlpha(getAlpha());
Winson Chungaafa03c2010-06-11 17:34:16 -0700480 addView(child, index, lp);
Michael Jurkadee05892010-07-27 10:01:56 -0700481
Michael Jurkaf3ca3ab2010-10-20 17:08:24 -0700482 if (markCells) markCellsAsOccupiedForView(child);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700483
Winson Chungaafa03c2010-06-11 17:34:16 -0700484 return true;
485 }
486 return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800487 }
Michael Jurka3e7c7632010-10-02 16:01:03 -0700488 public void setAcceptsDrops(boolean acceptsDrops) {
489 if (mAcceptsDrops != acceptsDrops) {
490 mAcceptsDrops = acceptsDrops;
491 invalidate();
492 }
493 }
494
495 public boolean getAcceptsDrops() {
496 return mAcceptsDrops;
497 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800498
499 @Override
Michael Jurka0280c3b2010-09-17 15:00:07 -0700500 public void removeAllViews() {
501 clearOccupiedCells();
502 }
503
504 @Override
505 public void removeAllViewsInLayout() {
506 clearOccupiedCells();
507 }
508
Michael Jurkaf3ca3ab2010-10-20 17:08:24 -0700509 public void removeViewWithoutMarkingCells(View view) {
510 super.removeView(view);
511 }
512
Michael Jurka0280c3b2010-09-17 15:00:07 -0700513 @Override
514 public void removeView(View view) {
515 markCellsAsUnoccupiedForView(view);
516 super.removeView(view);
517 }
518
519 @Override
520 public void removeViewAt(int index) {
521 markCellsAsUnoccupiedForView(getChildAt(index));
522 super.removeViewAt(index);
523 }
524
525 @Override
526 public void removeViewInLayout(View view) {
527 markCellsAsUnoccupiedForView(view);
528 super.removeViewInLayout(view);
529 }
530
531 @Override
532 public void removeViews(int start, int count) {
533 for (int i = start; i < start + count; i++) {
534 markCellsAsUnoccupiedForView(getChildAt(i));
535 }
536 super.removeViews(start, count);
537 }
538
539 @Override
540 public void removeViewsInLayout(int start, int count) {
541 for (int i = start; i < start + count; i++) {
542 markCellsAsUnoccupiedForView(getChildAt(i));
543 }
544 super.removeViewsInLayout(start, count);
545 }
546
547 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800548 public void requestChildFocus(View child, View focused) {
549 super.requestChildFocus(child, focused);
550 if (child != null) {
551 Rect r = new Rect();
552 child.getDrawingRect(r);
553 requestRectangleOnScreen(r);
554 }
555 }
556
557 @Override
558 protected void onAttachedToWindow() {
559 super.onAttachedToWindow();
560 mCellInfo.screen = ((ViewGroup) getParent()).indexOfChild(this);
561 }
562
Michael Jurkaaf442092010-06-10 17:01:57 -0700563 public void setTagToCellInfoForPoint(int touchX, int touchY) {
564 final CellInfo cellInfo = mCellInfo;
565 final Rect frame = mRect;
566 final int x = touchX + mScrollX;
567 final int y = touchY + mScrollY;
568 final int count = getChildCount();
569
570 boolean found = false;
571 for (int i = count - 1; i >= 0; i--) {
572 final View child = getChildAt(i);
573
574 if ((child.getVisibility()) == VISIBLE || child.getAnimation() != null) {
575 child.getHitRect(frame);
576 if (frame.contains(x, y)) {
577 final LayoutParams lp = (LayoutParams) child.getLayoutParams();
578 cellInfo.cell = child;
579 cellInfo.cellX = lp.cellX;
580 cellInfo.cellY = lp.cellY;
581 cellInfo.spanX = lp.cellHSpan;
582 cellInfo.spanY = lp.cellVSpan;
583 cellInfo.valid = true;
584 found = true;
Michael Jurkaaf442092010-06-10 17:01:57 -0700585 break;
586 }
587 }
588 }
Winson Chungaafa03c2010-06-11 17:34:16 -0700589
Michael Jurkaaf442092010-06-10 17:01:57 -0700590 if (!found) {
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700591 final int cellXY[] = mTmpCellXY;
Michael Jurkaaf442092010-06-10 17:01:57 -0700592 pointToCellExact(x, y, cellXY);
593
Michael Jurkaaf442092010-06-10 17:01:57 -0700594 cellInfo.cell = null;
595 cellInfo.cellX = cellXY[0];
596 cellInfo.cellY = cellXY[1];
597 cellInfo.spanX = 1;
598 cellInfo.spanY = 1;
Michael Jurka0280c3b2010-09-17 15:00:07 -0700599 cellInfo.valid = cellXY[0] >= 0 && cellXY[1] >= 0 && cellXY[0] < mCountX &&
600 cellXY[1] < mCountY && !mOccupied[cellXY[0]][cellXY[1]];
Michael Jurkaaf442092010-06-10 17:01:57 -0700601 }
602 setTag(cellInfo);
603 }
604
Winson Chungaafa03c2010-06-11 17:34:16 -0700605
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800606 @Override
607 public boolean onInterceptTouchEvent(MotionEvent ev) {
Michael Jurkadee05892010-07-27 10:01:56 -0700608 if (mInterceptTouchListener != null && mInterceptTouchListener.onTouch(this, ev)) {
609 return true;
610 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800611 final int action = ev.getAction();
612 final CellInfo cellInfo = mCellInfo;
613
614 if (action == MotionEvent.ACTION_DOWN) {
Michael Jurkaaf442092010-06-10 17:01:57 -0700615 setTagToCellInfoForPoint((int) ev.getX(), (int) ev.getY());
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800616 } else if (action == MotionEvent.ACTION_UP) {
617 cellInfo.cell = null;
618 cellInfo.cellX = -1;
619 cellInfo.cellY = -1;
620 cellInfo.spanX = 0;
621 cellInfo.spanY = 0;
622 cellInfo.valid = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800623 setTag(cellInfo);
624 }
625
626 return false;
627 }
628
629 @Override
630 public CellInfo getTag() {
Michael Jurka0280c3b2010-09-17 15:00:07 -0700631 return (CellInfo) super.getTag();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800632 }
633
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700634 /**
635 * Check if the row 'y' is empty from columns 'left' to 'right', inclusive.
636 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800637 private static boolean isRowEmpty(int y, int left, int right, boolean[][] occupied) {
638 for (int x = left; x <= right; x++) {
639 if (occupied[x][y]) {
640 return false;
641 }
642 }
643 return true;
644 }
645
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800646 /**
Winson Chungaafa03c2010-06-11 17:34:16 -0700647 * Given a point, return the cell that strictly encloses that point
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800648 * @param x X coordinate of the point
649 * @param y Y coordinate of the point
650 * @param result Array of 2 ints to hold the x and y coordinate of the cell
651 */
652 void pointToCellExact(int x, int y, int[] result) {
Winson Chungaafa03c2010-06-11 17:34:16 -0700653 final int hStartPadding = getLeftPadding();
654 final int vStartPadding = getTopPadding();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800655
656 result[0] = (x - hStartPadding) / (mCellWidth + mWidthGap);
657 result[1] = (y - vStartPadding) / (mCellHeight + mHeightGap);
658
Adam Cohend22015c2010-07-26 22:02:18 -0700659 final int xAxis = mCountX;
660 final int yAxis = mCountY;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800661
662 if (result[0] < 0) result[0] = 0;
663 if (result[0] >= xAxis) result[0] = xAxis - 1;
664 if (result[1] < 0) result[1] = 0;
665 if (result[1] >= yAxis) result[1] = yAxis - 1;
666 }
Winson Chungaafa03c2010-06-11 17:34:16 -0700667
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800668 /**
669 * Given a point, return the cell that most closely encloses that point
670 * @param x X coordinate of the point
671 * @param y Y coordinate of the point
672 * @param result Array of 2 ints to hold the x and y coordinate of the cell
673 */
674 void pointToCellRounded(int x, int y, int[] result) {
675 pointToCellExact(x + (mCellWidth / 2), y + (mCellHeight / 2), result);
676 }
677
678 /**
679 * Given a cell coordinate, return the point that represents the upper left corner of that cell
Winson Chungaafa03c2010-06-11 17:34:16 -0700680 *
681 * @param cellX X coordinate of the cell
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800682 * @param cellY Y coordinate of the cell
Winson Chungaafa03c2010-06-11 17:34:16 -0700683 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800684 * @param result Array of 2 ints to hold the x and y coordinate of the point
685 */
686 void cellToPoint(int cellX, int cellY, int[] result) {
Winson Chungaafa03c2010-06-11 17:34:16 -0700687 final int hStartPadding = getLeftPadding();
688 final int vStartPadding = getTopPadding();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800689
690 result[0] = hStartPadding + cellX * (mCellWidth + mWidthGap);
691 result[1] = vStartPadding + cellY * (mCellHeight + mHeightGap);
692 }
693
Romain Guy84f296c2009-11-04 15:00:44 -0800694 int getCellWidth() {
695 return mCellWidth;
696 }
697
698 int getCellHeight() {
699 return mCellHeight;
700 }
701
Romain Guy1a304a12009-11-10 00:02:32 -0800702 int getLeftPadding() {
Winson Chungaafa03c2010-06-11 17:34:16 -0700703 return mLeftPadding;
Romain Guy1a304a12009-11-10 00:02:32 -0800704 }
705
706 int getTopPadding() {
Winson Chungaafa03c2010-06-11 17:34:16 -0700707 return mTopPadding;
Romain Guy1a304a12009-11-10 00:02:32 -0800708 }
709
710 int getRightPadding() {
Winson Chungaafa03c2010-06-11 17:34:16 -0700711 return mRightPadding;
Romain Guy1a304a12009-11-10 00:02:32 -0800712 }
713
714 int getBottomPadding() {
Winson Chungaafa03c2010-06-11 17:34:16 -0700715 return mBottomPadding;
Romain Guy1a304a12009-11-10 00:02:32 -0800716 }
717
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800718 @Override
719 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
720 // TODO: currently ignoring padding
Winson Chungaafa03c2010-06-11 17:34:16 -0700721
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800722 int widthSpecMode = MeasureSpec.getMode(widthMeasureSpec);
Winson Chungaafa03c2010-06-11 17:34:16 -0700723 int widthSpecSize = MeasureSpec.getSize(widthMeasureSpec);
724
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800725 int heightSpecMode = MeasureSpec.getMode(heightMeasureSpec);
726 int heightSpecSize = MeasureSpec.getSize(heightMeasureSpec);
Winson Chungaafa03c2010-06-11 17:34:16 -0700727
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800728 if (widthSpecMode == MeasureSpec.UNSPECIFIED || heightSpecMode == MeasureSpec.UNSPECIFIED) {
729 throw new RuntimeException("CellLayout cannot have UNSPECIFIED dimensions");
730 }
731
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800732 final int cellWidth = mCellWidth;
733 final int cellHeight = mCellHeight;
734
Adam Cohend22015c2010-07-26 22:02:18 -0700735 int numWidthGaps = mCountX - 1;
736 int numHeightGaps = mCountY - 1;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800737
Winson Chungece7f5b2010-10-22 14:54:12 -0700738 if (mWidthGap < 0 || mHeightGap < 0) {
739 int vSpaceLeft = heightSpecSize - mTopPadding - mBottomPadding - (cellHeight * mCountY);
740 mHeightGap = vSpaceLeft / numHeightGaps;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800741
Winson Chungece7f5b2010-10-22 14:54:12 -0700742 int hSpaceLeft = widthSpecSize - mLeftPadding - mRightPadding - (cellWidth * mCountX);
743 mWidthGap = hSpaceLeft / numWidthGaps;
Winson Chungaafa03c2010-06-11 17:34:16 -0700744
Winson Chungece7f5b2010-10-22 14:54:12 -0700745 // center it around the min gaps
746 int minGap = Math.min(mWidthGap, mHeightGap);
747 mWidthGap = mHeightGap = minGap;
748 }
Michael Jurka5f1c5092010-09-03 14:15:02 -0700749
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800750 int count = getChildCount();
751
752 for (int i = 0; i < count; i++) {
753 View child = getChildAt(i);
754 LayoutParams lp = (LayoutParams) child.getLayoutParams();
Winson Chungaafa03c2010-06-11 17:34:16 -0700755 lp.setup(cellWidth, cellHeight, mWidthGap, mHeightGap,
756 mLeftPadding, mTopPadding);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800757
Michael Jurka0280c3b2010-09-17 15:00:07 -0700758 int childWidthMeasureSpec = MeasureSpec.makeMeasureSpec(lp.width, MeasureSpec.EXACTLY);
Winson Chungaafa03c2010-06-11 17:34:16 -0700759 int childheightMeasureSpec = MeasureSpec.makeMeasureSpec(lp.height,
760 MeasureSpec.EXACTLY);
761
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800762 child.measure(childWidthMeasureSpec, childheightMeasureSpec);
763 }
Michael Jurka5f1c5092010-09-03 14:15:02 -0700764 if (widthSpecMode == MeasureSpec.AT_MOST) {
765 int newWidth = mLeftPadding + mRightPadding + (mCountX * cellWidth) +
Winson Chungece7f5b2010-10-22 14:54:12 -0700766 ((mCountX - 1) * mWidthGap);
Michael Jurka5f1c5092010-09-03 14:15:02 -0700767 int newHeight = mTopPadding + mBottomPadding + (mCountY * cellHeight) +
Winson Chungece7f5b2010-10-22 14:54:12 -0700768 ((mCountY - 1) * mHeightGap);
Michael Jurka5f1c5092010-09-03 14:15:02 -0700769 setMeasuredDimension(newWidth, newHeight);
770 } else if (widthSpecMode == MeasureSpec.EXACTLY) {
771 setMeasuredDimension(widthSpecSize, heightSpecSize);
772 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800773 }
774
775 @Override
Michael Jurka28750fb2010-09-24 17:43:49 -0700776 protected void onLayout(boolean changed, int l, int t, int r, int b) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800777 int count = getChildCount();
778
779 for (int i = 0; i < count; i++) {
Patrick Dubroyce34a972010-10-19 10:34:32 -0700780 final View child = getChildAt(i);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800781 if (child.getVisibility() != GONE) {
782
783 CellLayout.LayoutParams lp = (CellLayout.LayoutParams) child.getLayoutParams();
784
785 int childLeft = lp.x;
786 int childTop = lp.y;
787 child.layout(childLeft, childTop, childLeft + lp.width, childTop + lp.height);
Romain Guy84f296c2009-11-04 15:00:44 -0800788
789 if (lp.dropped) {
790 lp.dropped = false;
791
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700792 final int[] cellXY = mTmpCellXY;
Romain Guy06762ab2010-01-25 16:51:08 -0800793 getLocationOnScreen(cellXY);
Romain Guy84f296c2009-11-04 15:00:44 -0800794 mWallpaperManager.sendWallpaperCommand(getWindowToken(), "android.home.drop",
Romain Guy06762ab2010-01-25 16:51:08 -0800795 cellXY[0] + childLeft + lp.width / 2,
796 cellXY[1] + childTop + lp.height / 2, 0, null);
Patrick Dubroyce34a972010-10-19 10:34:32 -0700797
Patrick Dubroycd68ff52010-10-28 17:57:05 -0700798 ((Workspace) mParent).animateViewIntoPosition(child);
Romain Guy84f296c2009-11-04 15:00:44 -0800799 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800800 }
801 }
802 }
803
804 @Override
Michael Jurkadee05892010-07-27 10:01:56 -0700805 protected void onSizeChanged(int w, int h, int oldw, int oldh) {
806 super.onSizeChanged(w, h, oldw, oldh);
Michael Jurka18014792010-10-14 09:01:34 -0700807 mBackgroundRect.set(0, 0, w, h);
808 updateHoverRect();
Michael Jurkadee05892010-07-27 10:01:56 -0700809 }
810
811 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800812 protected void setChildrenDrawingCacheEnabled(boolean enabled) {
813 final int count = getChildCount();
814 for (int i = 0; i < count; i++) {
815 final View view = getChildAt(i);
816 view.setDrawingCacheEnabled(enabled);
817 // Update the drawing caches
Romain Guy3cf4c032010-10-26 14:29:35 -0700818 if (!view.isHardwareAccelerated()) {
819 view.buildDrawingCache(true);
820 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800821 }
822 }
823
824 @Override
825 protected void setChildrenDrawnWithCacheEnabled(boolean enabled) {
826 super.setChildrenDrawnWithCacheEnabled(enabled);
827 }
828
Michael Jurka5f1c5092010-09-03 14:15:02 -0700829 public float getBackgroundAlpha() {
830 return mBackgroundAlpha;
Michael Jurkadee05892010-07-27 10:01:56 -0700831 }
832
Adam Cohen1b0aaac2010-10-28 11:11:18 -0700833 public void setBackgroundAlphaMultiplier(float multiplier) {
834 mBackgroundAlphaMultiplier = multiplier;
835 }
836
Michael Jurka5f1c5092010-09-03 14:15:02 -0700837 public void setBackgroundAlpha(float alpha) {
838 mBackgroundAlpha = alpha;
Michael Jurka0142d492010-08-25 17:46:15 -0700839 invalidate();
Michael Jurkadee05892010-07-27 10:01:56 -0700840 }
841
Michael Jurka5f1c5092010-09-03 14:15:02 -0700842 // Need to return true to let the view system know we know how to handle alpha-- this is
843 // because when our children have an alpha of 0.0f, they are still rendering their "dimmed"
844 // versions
845 @Override
846 protected boolean onSetAlpha(int alpha) {
847 return true;
848 }
849
850 public void setAlpha(float alpha) {
851 setChildrenAlpha(alpha);
852 super.setAlpha(alpha);
853 }
854
Michael Jurkadee05892010-07-27 10:01:56 -0700855 private void setChildrenAlpha(float alpha) {
Michael Jurka0142d492010-08-25 17:46:15 -0700856 final int childCount = getChildCount();
857 for (int i = 0; i < childCount; i++) {
Michael Jurkadee05892010-07-27 10:01:56 -0700858 getChildAt(i).setAlpha(alpha);
859 }
860 }
861
Michael Jurka0280c3b2010-09-17 15:00:07 -0700862 private boolean isVacantIgnoring(
863 int originX, int originY, int spanX, int spanY, View ignoreView) {
864 if (ignoreView != null) {
865 markCellsAsUnoccupiedForView(ignoreView);
866 }
Michael Jurka28750fb2010-09-24 17:43:49 -0700867 boolean isVacant = true;
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700868 for (int i = 0; i < spanY; i++) {
869 if (!isRowEmpty(originY + i, originX, originX + spanX - 1, mOccupied)) {
Michael Jurka28750fb2010-09-24 17:43:49 -0700870 isVacant = false;
871 break;
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700872 }
873 }
Michael Jurka0280c3b2010-09-17 15:00:07 -0700874 if (ignoreView != null) {
875 markCellsAsOccupiedForView(ignoreView);
876 }
Michael Jurka28750fb2010-09-24 17:43:49 -0700877 return isVacant;
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700878 }
879
Michael Jurka0280c3b2010-09-17 15:00:07 -0700880 private boolean isVacant(int originX, int originY, int spanX, int spanY) {
881 return isVacantIgnoring(originX, originY, spanX, spanY, null);
882 }
883
Patrick Dubroy440c3602010-07-13 17:50:32 -0700884 public View getChildAt(int x, int y) {
885 final int count = getChildCount();
886 for (int i = 0; i < count; i++) {
887 View child = getChildAt(i);
888 LayoutParams lp = (LayoutParams) child.getLayoutParams();
889
890 if ((lp.cellX <= x) && (x < lp.cellX + lp.cellHSpan) &&
891 (lp.cellY <= y) && (y < lp.cellY + lp.cellHSpan)) {
892 return child;
893 }
894 }
895 return null;
896 }
897
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700898 /**
Patrick Dubroy8f86ddc2010-07-16 13:55:32 -0700899 * Estimate the size that a child with the given dimensions will take in the layout.
900 */
901 void estimateChildSize(int minWidth, int minHeight, int[] result) {
902 // Assuming it's placed at 0, 0, find where the bottom right cell will land
903 rectToCell(minWidth, minHeight, result);
904
905 // Then figure out the rect it will occupy
906 cellToRect(0, 0, result[0], result[1], mRectF);
907 result[0] = (int)mRectF.width();
908 result[1] = (int)mRectF.height();
909 }
910
911 /**
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700912 * Estimate where the top left cell of the dragged item will land if it is dropped.
913 *
914 * @param originX The X value of the top left corner of the item
915 * @param originY The Y value of the top left corner of the item
916 * @param spanX The number of horizontal cells that the item spans
917 * @param spanY The number of vertical cells that the item spans
918 * @param result The estimated drop cell X and Y.
919 */
920 void estimateDropCell(int originX, int originY, int spanX, int spanY, int[] result) {
Adam Cohend22015c2010-07-26 22:02:18 -0700921 final int countX = mCountX;
922 final int countY = mCountY;
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700923
Michael Jurkaa63c4522010-08-19 13:52:27 -0700924 // pointToCellRounded takes the top left of a cell but will pad that with
925 // cellWidth/2 and cellHeight/2 when finding the matching cell
926 pointToCellRounded(originX, originY, result);
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700927
928 // If the item isn't fully on this screen, snap to the edges
929 int rightOverhang = result[0] + spanX - countX;
930 if (rightOverhang > 0) {
931 result[0] -= rightOverhang; // Snap to right
932 }
933 result[0] = Math.max(0, result[0]); // Snap to left
934 int bottomOverhang = result[1] + spanY - countY;
935 if (bottomOverhang > 0) {
936 result[1] -= bottomOverhang; // Snap to bottom
937 }
938 result[1] = Math.max(0, result[1]); // Snap to top
939 }
940
Joe Onorato4be866d2010-10-10 11:26:02 -0700941 void visualizeDropLocation(
942 View v, Bitmap dragOutline, int originX, int originY, int spanX, int spanY) {
943
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -0700944 final int oldDragCellX = mDragCell[0];
945 final int oldDragCellY = mDragCell[1];
Joe Onorato4be866d2010-10-10 11:26:02 -0700946 final int[] nearest = findNearestVacantArea(originX, originY, spanX, spanY, v, mDragCell);
Winson Chunga9abd0e2010-10-27 17:18:37 -0700947 if (v != null) {
948 mDragCenter.set(originX + (v.getWidth() / 2), originY + (v.getHeight() / 2));
949 } else {
950 mDragCenter.set(originX, originY);
951 }
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700952
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -0700953 if (nearest != null && (nearest[0] != oldDragCellX || nearest[1] != oldDragCellY)) {
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700954 // Find the top left corner of the rect the object will occupy
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700955 final int[] topLeft = mTmpPoint;
956 cellToPoint(nearest[0], nearest[1], topLeft);
957
Joe Onorato4be866d2010-10-10 11:26:02 -0700958 int left = topLeft[0];
959 int top = topLeft[1];
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700960
Winson Chunga9abd0e2010-10-27 17:18:37 -0700961 if (v != null) {
962 if (v.getParent() instanceof CellLayout) {
963 LayoutParams lp = (LayoutParams) v.getLayoutParams();
964 left += lp.leftMargin;
965 top += lp.topMargin;
966 }
Winson Chung150fbab2010-09-29 17:14:26 -0700967
Winson Chunga9abd0e2010-10-27 17:18:37 -0700968 // Offsets due to the size difference between the View and the dragOutline
969 left += (v.getWidth() - dragOutline.getWidth()) / 2;
970 top += (v.getHeight() - dragOutline.getHeight()) / 2;
971 }
Winson Chung150fbab2010-09-29 17:14:26 -0700972
Joe Onorato4be866d2010-10-10 11:26:02 -0700973 final int oldIndex = mDragOutlineCurrent;
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -0700974 mDragOutlineAnims[oldIndex].animateOut();
975 mDragOutlineCurrent = (oldIndex + 1) % mDragOutlines.length;
Winson Chung150fbab2010-09-29 17:14:26 -0700976
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -0700977 mDragOutlines[mDragOutlineCurrent].set(left, top);
978 mDragOutlineAnims[mDragOutlineCurrent].setTag(dragOutline);
979 mDragOutlineAnims[mDragOutlineCurrent].animateIn();
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700980 }
Patrick Dubroy49250ad2010-10-08 15:33:52 -0700981
982 // If we are drawing crosshairs, the entire CellLayout needs to be invalidated
983 if (mCrosshairsDrawable != null) {
984 invalidate();
985 }
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700986 }
987
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800988 /**
Jeff Sharkey70864282009-04-07 21:08:40 -0700989 * Find a vacant area that will fit the given bounds nearest the requested
990 * cell location. Uses Euclidean distance to score multiple vacant areas.
Winson Chungaafa03c2010-06-11 17:34:16 -0700991 *
Romain Guy51afc022009-05-04 18:03:43 -0700992 * @param pixelX The X location at which you want to search for a vacant area.
993 * @param pixelY The Y location at which you want to search for a vacant area.
Jeff Sharkey70864282009-04-07 21:08:40 -0700994 * @param spanX Horizontal span of the object.
995 * @param spanY Vertical span of the object.
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700996 * @param result Array in which to place the result, or null (in which case a new array will
997 * be allocated)
Jeff Sharkey70864282009-04-07 21:08:40 -0700998 * @return The X, Y cell of a vacant area that can contain this object,
999 * nearest the requested location.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001000 */
Michael Jurka6a1435d2010-09-27 17:35:12 -07001001 int[] findNearestVacantArea(
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001002 int pixelX, int pixelY, int spanX, int spanY, int[] result) {
1003 return findNearestVacantArea(pixelX, pixelY, spanX, spanY, null, result);
Michael Jurka6a1435d2010-09-27 17:35:12 -07001004 }
Winson Chungaafa03c2010-06-11 17:34:16 -07001005
Michael Jurka6a1435d2010-09-27 17:35:12 -07001006 /**
1007 * Find a vacant area that will fit the given bounds nearest the requested
1008 * cell location. Uses Euclidean distance to score multiple vacant areas.
1009 *
1010 * @param pixelX The X location at which you want to search for a vacant area.
1011 * @param pixelY The Y location at which you want to search for a vacant area.
1012 * @param spanX Horizontal span of the object.
1013 * @param spanY Vertical span of the object.
Michael Jurka6a1435d2010-09-27 17:35:12 -07001014 * @param ignoreView Considers space occupied by this view as unoccupied
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001015 * @param result Previously returned value to possibly recycle.
Michael Jurka6a1435d2010-09-27 17:35:12 -07001016 * @return The X, Y cell of a vacant area that can contain this object,
1017 * nearest the requested location.
1018 */
1019 int[] findNearestVacantArea(
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001020 int pixelX, int pixelY, int spanX, int spanY, View ignoreView, int[] result) {
Michael Jurkac6ee42e2010-09-30 12:04:50 -07001021 // mark space take by ignoreView as available (method checks if ignoreView is null)
1022 markCellsAsUnoccupiedForView(ignoreView);
1023
Jeff Sharkey70864282009-04-07 21:08:40 -07001024 // Keep track of best-scoring drop area
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001025 final int[] bestXY = result != null ? result : new int[2];
Jeff Sharkey70864282009-04-07 21:08:40 -07001026 double bestDistance = Double.MAX_VALUE;
Winson Chungaafa03c2010-06-11 17:34:16 -07001027
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001028 final int countX = mCountX;
1029 final int countY = mCountY;
1030 final boolean[][] occupied = mOccupied;
1031
1032 for (int x = 0; x < countX - (spanX - 1); x++) {
Michael Jurkac28de512010-08-13 11:27:44 -07001033 inner:
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001034 for (int y = 0; y < countY - (spanY - 1); y++) {
Michael Jurkac28de512010-08-13 11:27:44 -07001035 for (int i = 0; i < spanX; i++) {
1036 for (int j = 0; j < spanY; j++) {
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001037 if (occupied[x + i][y + j]) {
Michael Jurkac28de512010-08-13 11:27:44 -07001038 // small optimization: we can skip to below the row we just found
1039 // an occupied cell
1040 y += j;
1041 continue inner;
1042 }
1043 }
1044 }
1045 final int[] cellXY = mTmpCellXY;
1046 cellToPoint(x, y, cellXY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001047
Michael Jurkac28de512010-08-13 11:27:44 -07001048 double distance = Math.sqrt(Math.pow(cellXY[0] - pixelX, 2)
1049 + Math.pow(cellXY[1] - pixelY, 2));
1050 if (distance <= bestDistance) {
1051 bestDistance = distance;
1052 bestXY[0] = x;
1053 bestXY[1] = y;
1054 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001055 }
1056 }
Michael Jurkac6ee42e2010-09-30 12:04:50 -07001057 // re-mark space taken by ignoreView as occupied
1058 markCellsAsOccupiedForView(ignoreView);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001059
Winson Chungaafa03c2010-06-11 17:34:16 -07001060 // Return null if no suitable location found
Jeff Sharkey70864282009-04-07 21:08:40 -07001061 if (bestDistance < Double.MAX_VALUE) {
1062 return bestXY;
1063 } else {
1064 return null;
1065 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001066 }
Winson Chungaafa03c2010-06-11 17:34:16 -07001067
Michael Jurka0280c3b2010-09-17 15:00:07 -07001068 boolean existsEmptyCell() {
1069 return findCellForSpan(null, 1, 1);
1070 }
1071
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001072 /**
Michael Jurka0280c3b2010-09-17 15:00:07 -07001073 * Finds the upper-left coordinate of the first rectangle in the grid that can
1074 * hold a cell of the specified dimensions. If intersectX and intersectY are not -1,
1075 * then this method will only return coordinates for rectangles that contain the cell
1076 * (intersectX, intersectY)
1077 *
1078 * @param cellXY The array that will contain the position of a vacant cell if such a cell
1079 * can be found.
1080 * @param spanX The horizontal span of the cell we want to find.
1081 * @param spanY The vertical span of the cell we want to find.
1082 *
1083 * @return True if a vacant cell of the specified dimension was found, false otherwise.
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001084 */
Michael Jurka0280c3b2010-09-17 15:00:07 -07001085 boolean findCellForSpan(int[] cellXY, int spanX, int spanY) {
1086 return findCellForSpanThatIntersectsIgnoring(cellXY, spanX, spanY, -1, -1, null);
1087 }
1088
1089 /**
1090 * Like above, but ignores any cells occupied by the item "ignoreView"
1091 *
1092 * @param cellXY The array that will contain the position of a vacant cell if such a cell
1093 * can be found.
1094 * @param spanX The horizontal span of the cell we want to find.
1095 * @param spanY The vertical span of the cell we want to find.
1096 * @param ignoreView The home screen item we should treat as not occupying any space
1097 * @return
1098 */
1099 boolean findCellForSpanIgnoring(int[] cellXY, int spanX, int spanY, View ignoreView) {
1100 return findCellForSpanThatIntersectsIgnoring(cellXY, spanX, spanY, -1, -1, ignoreView);
1101 }
1102
1103 /**
1104 * Like above, but if intersectX and intersectY are not -1, then this method will try to
1105 * return coordinates for rectangles that contain the cell [intersectX, intersectY]
1106 *
1107 * @param spanX The horizontal span of the cell we want to find.
1108 * @param spanY The vertical span of the cell we want to find.
1109 * @param ignoreView The home screen item we should treat as not occupying any space
1110 * @param intersectX The X coordinate of the cell that we should try to overlap
1111 * @param intersectX The Y coordinate of the cell that we should try to overlap
1112 *
1113 * @return True if a vacant cell of the specified dimension was found, false otherwise.
1114 */
1115 boolean findCellForSpanThatIntersects(int[] cellXY, int spanX, int spanY,
1116 int intersectX, int intersectY) {
1117 return findCellForSpanThatIntersectsIgnoring(
1118 cellXY, spanX, spanY, intersectX, intersectY, null);
1119 }
1120
1121 /**
1122 * The superset of the above two methods
1123 */
1124 boolean findCellForSpanThatIntersectsIgnoring(int[] cellXY, int spanX, int spanY,
1125 int intersectX, int intersectY, View ignoreView) {
Michael Jurkac6ee42e2010-09-30 12:04:50 -07001126 // mark space take by ignoreView as available (method checks if ignoreView is null)
1127 markCellsAsUnoccupiedForView(ignoreView);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001128
Michael Jurka28750fb2010-09-24 17:43:49 -07001129 boolean foundCell = false;
Michael Jurka0280c3b2010-09-17 15:00:07 -07001130 while (true) {
1131 int startX = 0;
1132 if (intersectX >= 0) {
1133 startX = Math.max(startX, intersectX - (spanX - 1));
1134 }
1135 int endX = mCountX - (spanX - 1);
1136 if (intersectX >= 0) {
1137 endX = Math.min(endX, intersectX + (spanX - 1) + (spanX == 1 ? 1 : 0));
1138 }
1139 int startY = 0;
1140 if (intersectY >= 0) {
1141 startY = Math.max(startY, intersectY - (spanY - 1));
1142 }
1143 int endY = mCountY - (spanY - 1);
1144 if (intersectY >= 0) {
1145 endY = Math.min(endY, intersectY + (spanY - 1) + (spanY == 1 ? 1 : 0));
1146 }
1147
1148 for (int x = startX; x < endX; x++) {
1149 inner:
1150 for (int y = startY; y < endY; y++) {
1151 for (int i = 0; i < spanX; i++) {
1152 for (int j = 0; j < spanY; j++) {
1153 if (mOccupied[x + i][y + j]) {
1154 // small optimization: we can skip to below the row we just found
1155 // an occupied cell
1156 y += j;
1157 continue inner;
1158 }
1159 }
1160 }
1161 if (cellXY != null) {
1162 cellXY[0] = x;
1163 cellXY[1] = y;
1164 }
Michael Jurka28750fb2010-09-24 17:43:49 -07001165 foundCell = true;
1166 break;
Michael Jurka0280c3b2010-09-17 15:00:07 -07001167 }
1168 }
1169 if (intersectX == -1 && intersectY == -1) {
1170 break;
1171 } else {
1172 // if we failed to find anything, try again but without any requirements of
1173 // intersecting
1174 intersectX = -1;
1175 intersectY = -1;
1176 continue;
1177 }
1178 }
1179
Michael Jurkac6ee42e2010-09-30 12:04:50 -07001180 // re-mark space taken by ignoreView as occupied
1181 markCellsAsOccupiedForView(ignoreView);
Michael Jurka28750fb2010-09-24 17:43:49 -07001182 return foundCell;
Michael Jurka0280c3b2010-09-17 15:00:07 -07001183 }
1184
1185 /**
1186 * Called when drag has left this CellLayout or has been completed (successfully or not)
1187 */
1188 void onDragExit() {
Joe Onorato4be866d2010-10-10 11:26:02 -07001189 // This can actually be called when we aren't in a drag, e.g. when adding a new
1190 // item to this layout via the customize drawer.
1191 // Guard against that case.
1192 if (mDragging) {
1193 mDragging = false;
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001194
Joe Onorato4be866d2010-10-10 11:26:02 -07001195 // Fade out the drag indicators
1196 if (mCrosshairsAnimator != null) {
1197 mCrosshairsAnimator.animateOut();
1198 }
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001199 }
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -07001200
1201 // Invalidate the drag data
1202 mDragCell[0] = -1;
1203 mDragCell[1] = -1;
1204 mDragOutlineAnims[mDragOutlineCurrent].animateOut();
1205 mDragOutlineCurrent = (mDragOutlineCurrent + 1) % mDragOutlineAnims.length;
1206
1207 setHover(false);
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001208 }
1209
1210 /**
Winson Chungaafa03c2010-06-11 17:34:16 -07001211 * Mark a child as having been dropped.
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001212 * At the beginning of the drag operation, the child may have been on another
Patrick Dubroyce34a972010-10-19 10:34:32 -07001213 * screen, but it is re-parented before this method is called.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001214 *
1215 * @param child The child that is being dropped
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001216 */
Winson Chungaafa03c2010-06-11 17:34:16 -07001217 void onDropChild(View child) {
Romain Guyd94533d2009-08-17 10:01:15 -07001218 if (child != null) {
1219 LayoutParams lp = (LayoutParams) child.getLayoutParams();
Romain Guyd94533d2009-08-17 10:01:15 -07001220 lp.isDragging = false;
Romain Guy84f296c2009-11-04 15:00:44 -08001221 lp.dropped = true;
Patrick Dubroyce34a972010-10-19 10:34:32 -07001222 child.setVisibility(View.VISIBLE);
Romain Guyd94533d2009-08-17 10:01:15 -07001223 child.requestLayout();
Romain Guyd94533d2009-08-17 10:01:15 -07001224 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001225 }
1226
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001227 /**
1228 * Start dragging the specified child
Winson Chungaafa03c2010-06-11 17:34:16 -07001229 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001230 * @param child The child that is being dragged
1231 */
1232 void onDragChild(View child) {
1233 LayoutParams lp = (LayoutParams) child.getLayoutParams();
1234 lp.isDragging = true;
Patrick Dubroyce34a972010-10-19 10:34:32 -07001235 child.setVisibility(View.GONE);
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001236 }
1237
1238 /**
1239 * A drag event has begun over this layout.
1240 * It may have begun over this layout (in which case onDragChild is called first),
1241 * or it may have begun on another layout.
1242 */
Winson Chunga9abd0e2010-10-27 17:18:37 -07001243 void onDragEnter() {
Patrick Dubroyfe6bd872010-10-13 17:32:10 -07001244 if (!mDragging) {
Patrick Dubroyfe6bd872010-10-13 17:32:10 -07001245 // Fade in the drag indicators
1246 if (mCrosshairsAnimator != null) {
1247 mCrosshairsAnimator.animateIn();
1248 }
Joe Onorato4be866d2010-10-10 11:26:02 -07001249 }
1250 mDragging = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001251 }
Winson Chungaafa03c2010-06-11 17:34:16 -07001252
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001253 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001254 * Computes a bounding rectangle for a range of cells
Winson Chungaafa03c2010-06-11 17:34:16 -07001255 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001256 * @param cellX X coordinate of upper left corner expressed as a cell position
1257 * @param cellY Y coordinate of upper left corner expressed as a cell position
Winson Chungaafa03c2010-06-11 17:34:16 -07001258 * @param cellHSpan Width in cells
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001259 * @param cellVSpan Height in cells
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001260 * @param resultRect Rect into which to put the results
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001261 */
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001262 public void cellToRect(int cellX, int cellY, int cellHSpan, int cellVSpan, RectF resultRect) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001263 final int cellWidth = mCellWidth;
1264 final int cellHeight = mCellHeight;
1265 final int widthGap = mWidthGap;
1266 final int heightGap = mHeightGap;
Winson Chungaafa03c2010-06-11 17:34:16 -07001267
1268 final int hStartPadding = getLeftPadding();
1269 final int vStartPadding = getTopPadding();
1270
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001271 int width = cellHSpan * cellWidth + ((cellHSpan - 1) * widthGap);
1272 int height = cellVSpan * cellHeight + ((cellVSpan - 1) * heightGap);
1273
1274 int x = hStartPadding + cellX * (cellWidth + widthGap);
1275 int y = vStartPadding + cellY * (cellHeight + heightGap);
Winson Chungaafa03c2010-06-11 17:34:16 -07001276
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001277 resultRect.set(x, y, x + width, y + height);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001278 }
Winson Chungaafa03c2010-06-11 17:34:16 -07001279
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001280 /**
Winson Chungaafa03c2010-06-11 17:34:16 -07001281 * Computes the required horizontal and vertical cell spans to always
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001282 * fit the given rectangle.
Winson Chungaafa03c2010-06-11 17:34:16 -07001283 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001284 * @param width Width in pixels
1285 * @param height Height in pixels
Patrick Dubroy8f86ddc2010-07-16 13:55:32 -07001286 * @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 -08001287 */
Patrick Dubroy8f86ddc2010-07-16 13:55:32 -07001288 public int[] rectToCell(int width, int height, int[] result) {
Michael Jurka9987a5c2010-10-08 16:58:12 -07001289 return rectToCell(getResources(), width, height, result);
1290 }
1291
1292 public static int[] rectToCell(Resources resources, int width, int height, int[] result) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001293 // Always assume we're working with the smallest span to make sure we
1294 // reserve enough space in both orientations.
Joe Onorato79e56262009-09-21 15:23:04 -04001295 int actualWidth = resources.getDimensionPixelSize(R.dimen.workspace_cell_width);
1296 int actualHeight = resources.getDimensionPixelSize(R.dimen.workspace_cell_height);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001297 int smallerSize = Math.min(actualWidth, actualHeight);
Joe Onorato79e56262009-09-21 15:23:04 -04001298
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001299 // Always round up to next largest cell
1300 int spanX = (width + smallerSize) / smallerSize;
1301 int spanY = (height + smallerSize) / smallerSize;
Joe Onorato79e56262009-09-21 15:23:04 -04001302
Patrick Dubroy8f86ddc2010-07-16 13:55:32 -07001303 if (result == null) {
1304 return new int[] { spanX, spanY };
1305 }
1306 result[0] = spanX;
1307 result[1] = spanY;
1308 return result;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001309 }
1310
1311 /**
1312 * Find the first vacant cell, if there is one.
1313 *
1314 * @param vacant Holds the x and y coordinate of the vacant cell
1315 * @param spanX Horizontal cell span.
1316 * @param spanY Vertical cell span.
Winson Chungaafa03c2010-06-11 17:34:16 -07001317 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001318 * @return True if a vacant cell was found
1319 */
1320 public boolean getVacantCell(int[] vacant, int spanX, int spanY) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001321
Michael Jurka0280c3b2010-09-17 15:00:07 -07001322 return findVacantCell(vacant, spanX, spanY, mCountX, mCountY, mOccupied);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001323 }
1324
1325 static boolean findVacantCell(int[] vacant, int spanX, int spanY,
1326 int xCount, int yCount, boolean[][] occupied) {
1327
1328 for (int x = 0; x < xCount; x++) {
1329 for (int y = 0; y < yCount; y++) {
1330 boolean available = !occupied[x][y];
1331out: for (int i = x; i < x + spanX - 1 && x < xCount; i++) {
1332 for (int j = y; j < y + spanY - 1 && y < yCount; j++) {
1333 available = available && !occupied[i][j];
1334 if (!available) break out;
1335 }
1336 }
1337
1338 if (available) {
1339 vacant[0] = x;
1340 vacant[1] = y;
1341 return true;
1342 }
1343 }
1344 }
1345
1346 return false;
1347 }
1348
Michael Jurka0280c3b2010-09-17 15:00:07 -07001349 private void clearOccupiedCells() {
1350 for (int x = 0; x < mCountX; x++) {
1351 for (int y = 0; y < mCountY; y++) {
1352 mOccupied[x][y] = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001353 }
1354 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001355 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001356
Michael Jurka0280c3b2010-09-17 15:00:07 -07001357 public void onMove(View view, int newCellX, int newCellY) {
1358 LayoutParams lp = (LayoutParams) view.getLayoutParams();
1359 markCellsAsUnoccupiedForView(view);
1360 markCellsForView(newCellX, newCellY, lp.cellHSpan, lp.cellVSpan, true);
1361 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001362
Michael Jurka0280c3b2010-09-17 15:00:07 -07001363 private void markCellsAsOccupiedForView(View view) {
Michael Jurkac6ee42e2010-09-30 12:04:50 -07001364 if (view == null || view.getParent() != this) return;
Michael Jurka0280c3b2010-09-17 15:00:07 -07001365 LayoutParams lp = (LayoutParams) view.getLayoutParams();
1366 markCellsForView(lp.cellX, lp.cellY, lp.cellHSpan, lp.cellVSpan, true);
1367 }
1368
1369 private void markCellsAsUnoccupiedForView(View view) {
Michael Jurkac6ee42e2010-09-30 12:04:50 -07001370 if (view == null || view.getParent() != this) return;
Michael Jurka0280c3b2010-09-17 15:00:07 -07001371 LayoutParams lp = (LayoutParams) view.getLayoutParams();
1372 markCellsForView(lp.cellX, lp.cellY, lp.cellHSpan, lp.cellVSpan, false);
1373 }
1374
1375 private void markCellsForView(int cellX, int cellY, int spanX, int spanY, boolean value) {
1376 for (int x = cellX; x < cellX + spanX && x < mCountX; x++) {
1377 for (int y = cellY; y < cellY + spanY && y < mCountY; y++) {
1378 mOccupied[x][y] = value;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001379 }
1380 }
1381 }
1382
1383 @Override
1384 public ViewGroup.LayoutParams generateLayoutParams(AttributeSet attrs) {
1385 return new CellLayout.LayoutParams(getContext(), attrs);
1386 }
1387
1388 @Override
1389 protected boolean checkLayoutParams(ViewGroup.LayoutParams p) {
1390 return p instanceof CellLayout.LayoutParams;
1391 }
1392
1393 @Override
1394 protected ViewGroup.LayoutParams generateLayoutParams(ViewGroup.LayoutParams p) {
1395 return new CellLayout.LayoutParams(p);
1396 }
1397
Winson Chungaafa03c2010-06-11 17:34:16 -07001398 public static class CellLayoutAnimationController extends LayoutAnimationController {
1399 public CellLayoutAnimationController(Animation animation, float delay) {
1400 super(animation, delay);
1401 }
1402
1403 @Override
1404 protected long getDelayForView(View view) {
1405 return (int) (Math.random() * 150);
1406 }
1407 }
1408
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001409 public static class LayoutParams extends ViewGroup.MarginLayoutParams {
1410 /**
1411 * Horizontal location of the item in the grid.
1412 */
1413 @ViewDebug.ExportedProperty
1414 public int cellX;
1415
1416 /**
1417 * Vertical location of the item in the grid.
1418 */
1419 @ViewDebug.ExportedProperty
1420 public int cellY;
1421
1422 /**
1423 * Number of cells spanned horizontally by the item.
1424 */
1425 @ViewDebug.ExportedProperty
1426 public int cellHSpan;
1427
1428 /**
1429 * Number of cells spanned vertically by the item.
1430 */
1431 @ViewDebug.ExportedProperty
1432 public int cellVSpan;
Winson Chungaafa03c2010-06-11 17:34:16 -07001433
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001434 /**
1435 * Is this item currently being dragged
1436 */
1437 public boolean isDragging;
1438
1439 // X coordinate of the view in the layout.
1440 @ViewDebug.ExportedProperty
1441 int x;
1442 // Y coordinate of the view in the layout.
1443 @ViewDebug.ExportedProperty
1444 int y;
1445
Patrick Dubroyce34a972010-10-19 10:34:32 -07001446 /**
1447 * The old X coordinate of this item, relative to its current parent.
1448 * Used to animate the item into its new position.
1449 */
1450 int oldX;
1451
1452 /**
1453 * The old Y coordinate of this item, relative to its current parent.
1454 * Used to animate the item into its new position.
1455 */
1456 int oldY;
1457
Romain Guy84f296c2009-11-04 15:00:44 -08001458 boolean dropped;
Romain Guyfcb9e712009-10-02 16:06:52 -07001459
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001460 public LayoutParams(Context c, AttributeSet attrs) {
1461 super(c, attrs);
1462 cellHSpan = 1;
1463 cellVSpan = 1;
1464 }
1465
1466 public LayoutParams(ViewGroup.LayoutParams source) {
1467 super(source);
1468 cellHSpan = 1;
1469 cellVSpan = 1;
1470 }
Winson Chungaafa03c2010-06-11 17:34:16 -07001471
1472 public LayoutParams(LayoutParams source) {
1473 super(source);
1474 this.cellX = source.cellX;
1475 this.cellY = source.cellY;
1476 this.cellHSpan = source.cellHSpan;
1477 this.cellVSpan = source.cellVSpan;
1478 }
1479
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001480 public LayoutParams(int cellX, int cellY, int cellHSpan, int cellVSpan) {
Romain Guy8f19cdd2010-01-08 15:07:00 -08001481 super(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001482 this.cellX = cellX;
1483 this.cellY = cellY;
1484 this.cellHSpan = cellHSpan;
1485 this.cellVSpan = cellVSpan;
1486 }
1487
1488 public void setup(int cellWidth, int cellHeight, int widthGap, int heightGap,
1489 int hStartPadding, int vStartPadding) {
Winson Chungaafa03c2010-06-11 17:34:16 -07001490
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001491 final int myCellHSpan = cellHSpan;
1492 final int myCellVSpan = cellVSpan;
1493 final int myCellX = cellX;
1494 final int myCellY = cellY;
Winson Chungaafa03c2010-06-11 17:34:16 -07001495
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001496 width = myCellHSpan * cellWidth + ((myCellHSpan - 1) * widthGap) -
1497 leftMargin - rightMargin;
1498 height = myCellVSpan * cellHeight + ((myCellVSpan - 1) * heightGap) -
1499 topMargin - bottomMargin;
1500
1501 x = hStartPadding + myCellX * (cellWidth + widthGap) + leftMargin;
1502 y = vStartPadding + myCellY * (cellHeight + heightGap) + topMargin;
1503 }
Winson Chungaafa03c2010-06-11 17:34:16 -07001504
1505 public String toString() {
1506 return "(" + this.cellX + ", " + this.cellY + ")";
1507 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001508 }
1509
Michael Jurka0280c3b2010-09-17 15:00:07 -07001510 // This class stores info for two purposes:
1511 // 1. When dragging items (mDragInfo in Workspace), we store the View, its cellX & cellY,
1512 // its spanX, spanY, and the screen it is on
1513 // 2. When long clicking on an empty cell in a CellLayout, we save information about the
1514 // cellX and cellY coordinates and which page was clicked. We then set this as a tag on
1515 // the CellLayout that was long clicked
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001516 static final class CellInfo implements ContextMenu.ContextMenuInfo {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001517 View cell;
Michael Jurkaa63c4522010-08-19 13:52:27 -07001518 int cellX = -1;
1519 int cellY = -1;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001520 int spanX;
1521 int spanY;
1522 int screen;
1523 boolean valid;
1524
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001525 @Override
1526 public String toString() {
Winson Chungaafa03c2010-06-11 17:34:16 -07001527 return "Cell[view=" + (cell == null ? "null" : cell.getClass())
1528 + ", x=" + cellX + ", y=" + cellY + "]";
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001529 }
1530 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001531}