blob: 3c82290f82bb0c0984b00ea77a9a0608b12d72fb [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 Cohenf34bab52010-09-30 14:11:56 -070088
Michael Jurka5f1c5092010-09-03 14:15:02 -070089 private Drawable mBackground;
Adam Cohenf34bab52010-09-30 14:11:56 -070090 private Drawable mBackgroundMini;
91 private Drawable mBackgroundMiniHover;
Patrick Dubroy1262e362010-10-06 15:49:50 -070092 private Drawable mBackgroundHover;
Michael Jurka3e7c7632010-10-02 16:01:03 -070093 private Drawable mBackgroundMiniAcceptsDrops;
Michael Jurka18014792010-10-14 09:01:34 -070094 private Rect mBackgroundRect;
95 private Rect mHoverRect;
96 private float mHoverScale;
97 private float mHoverAlpha;
Michael Jurka3e7c7632010-10-02 16:01:03 -070098 private boolean mAcceptsDrops;
Patrick Dubroy1262e362010-10-06 15:49:50 -070099
100 // If we're actively dragging something over this screen, mHover is true
Michael Jurkaa63c4522010-08-19 13:52:27 -0700101 private boolean mHover = false;
Michael Jurkadee05892010-07-27 10:01:56 -0700102
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700103 private final Point mDragCenter = new Point();
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700104
Winson Chung150fbab2010-09-29 17:14:26 -0700105 // These arrays are used to implement the drag visualization on x-large screens.
Joe Onorato4be866d2010-10-10 11:26:02 -0700106 // They are used as circular arrays, indexed by mDragOutlineCurrent.
107 private Point[] mDragOutlines = new Point[8];
Chet Haase472b2812010-10-14 07:02:04 -0700108 private float[] mDragOutlineAlphas = new float[mDragOutlines.length];
Joe Onorato4be866d2010-10-10 11:26:02 -0700109 private InterruptibleInOutAnimator[] mDragOutlineAnims =
110 new InterruptibleInOutAnimator[mDragOutlines.length];
Winson Chung150fbab2010-09-29 17:14:26 -0700111
112 // Used as an index into the above 3 arrays; indicates which is the most current value.
Joe Onorato4be866d2010-10-10 11:26:02 -0700113 private int mDragOutlineCurrent = 0;
Patrick Dubroy8e58e912010-10-14 13:21:48 -0700114 private final Paint mDragOutlinePaint = new Paint();
Winson Chung150fbab2010-09-29 17:14:26 -0700115
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700116 private Drawable mCrosshairsDrawable = null;
Patrick Dubroy49250ad2010-10-08 15:33:52 -0700117 private InterruptibleInOutAnimator mCrosshairsAnimator = null;
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700118 private float mCrosshairsVisibility = 0.0f;
119
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700120 // When a drag operation is in progress, holds the nearest cell to the touch point
121 private final int[] mDragCell = new int[2];
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800122
Winson Chungaafa03c2010-06-11 17:34:16 -0700123 private final WallpaperManager mWallpaperManager;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800124
Joe Onorato4be866d2010-10-10 11:26:02 -0700125 private boolean mDragging = false;
126
Patrick Dubroya9442152010-10-25 11:41:41 -0700127 private ValueAnimator mDropAnim;
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
Patrick Dubroya9442152010-10-25 11:41:41 -0700257 mDropAnim = ValueAnimator.ofFloat(1.0f, 0.0f);
Patrick Dubroyce34a972010-10-19 10:34:32 -0700258 mDropAnim.setInterpolator(mEaseOutInterpolator);
259
Michael Jurka18014792010-10-14 09:01:34 -0700260 mBackgroundRect = new Rect();
261 mHoverRect = new Rect();
262 setHoverScale(1.0f);
263 setHoverAlpha(1.0f);
264 }
265
266 private void updateHoverRect() {
267 float marginFraction = (mHoverScale - 1.0f) / 2.0f;
268 int marginX = (int) (marginFraction * (mBackgroundRect.right - mBackgroundRect.left));
269 int marginY = (int) (marginFraction * (mBackgroundRect.bottom - mBackgroundRect.top));
270 mHoverRect.set(mBackgroundRect.left - marginX, mBackgroundRect.top - marginY,
271 mBackgroundRect.right + marginX, mBackgroundRect.bottom + marginY);
272 invalidate();
273 }
274
275 public void setHoverScale(float scaleFactor) {
276 if (scaleFactor != mHoverScale) {
277 mHoverScale = scaleFactor;
278 updateHoverRect();
279 }
280 }
281
282 public float getHoverScale() {
283 return mHoverScale;
284 }
285
286 public float getHoverAlpha() {
287 return mHoverAlpha;
288 }
289
290 public void setHoverAlpha(float alpha) {
291 mHoverAlpha = alpha;
292 invalidate();
293 }
294
295 void animateDrop() {
296 if (LauncherApplication.isScreenXLarge()) {
297 Resources res = getResources();
298 float onDropScale = res.getInteger(R.integer.config_screenOnDropScalePercent) / 100.0f;
299 ObjectAnimator scaleUp = ObjectAnimator.ofFloat(this, "hoverScale", onDropScale);
300 scaleUp.setDuration(res.getInteger(R.integer.config_screenOnDropScaleUpDuration));
301 ObjectAnimator scaleDown = ObjectAnimator.ofFloat(this, "hoverScale", 1.0f);
302 scaleDown.setDuration(res.getInteger(R.integer.config_screenOnDropScaleDownDuration));
303 ObjectAnimator alphaFadeOut = ObjectAnimator.ofFloat(this, "hoverAlpha", 0.0f);
304
305 alphaFadeOut.setStartDelay(res.getInteger(R.integer.config_screenOnDropAlphaFadeDelay));
306 alphaFadeOut.setDuration(res.getInteger(R.integer.config_screenOnDropAlphaFadeDelay));
307
308 AnimatorSet bouncer = new AnimatorSet();
309 bouncer.play(scaleUp).before(scaleDown);
310 bouncer.play(scaleUp).with(alphaFadeOut);
311 bouncer.addListener(new AnimatorListenerAdapter() {
312 public void onAnimationStart(Animator animation) {
313 setHover(true);
314 }
315 public void onAnimationEnd(Animator animation) {
316 setHover(false);
317 setHoverScale(1.0f);
318 setHoverAlpha(1.0f);
319 }
320 });
321 bouncer.start();
322 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800323 }
324
Michael Jurkaa63c4522010-08-19 13:52:27 -0700325 public void setHover(boolean value) {
326 if (mHover != value) {
Michael Jurka3e7c7632010-10-02 16:01:03 -0700327 mHover = value;
Michael Jurkaa63c4522010-08-19 13:52:27 -0700328 invalidate();
329 }
Michael Jurkaa63c4522010-08-19 13:52:27 -0700330 }
331
Patrick Dubroy1262e362010-10-06 15:49:50 -0700332 public void drawChildren(Canvas canvas) {
333 super.dispatchDraw(canvas);
334 }
335
Jeff Sharkey83f111d2009-04-20 21:03:13 -0700336 @Override
Patrick Dubroy1262e362010-10-06 15:49:50 -0700337 protected void onDraw(Canvas canvas) {
Michael Jurka3e7c7632010-10-02 16:01:03 -0700338 // When we're large, we are either drawn in a "hover" state (ie when dragging an item to
339 // a neighboring page) or with just a normal background (if backgroundAlpha > 0.0f)
340 // When we're small, we are either drawn normally or in the "accepts drops" state (during
341 // a drag). However, we also drag the mini hover background *over* one of those two
342 // backgrounds
Michael Jurka5f1c5092010-09-03 14:15:02 -0700343 if (mBackgroundAlpha > 0.0f) {
Adam Cohenf34bab52010-09-30 14:11:56 -0700344 Drawable bg;
Patrick Dubroy1262e362010-10-06 15:49:50 -0700345 if (getScaleX() < 0.5f) {
Michael Jurka3e7c7632010-10-02 16:01:03 -0700346 bg = mAcceptsDrops ? mBackgroundMiniAcceptsDrops : mBackgroundMini;
Adam Cohenf34bab52010-09-30 14:11:56 -0700347 } else {
Patrick Dubroy1262e362010-10-06 15:49:50 -0700348 bg = mHover ? mBackgroundHover : mBackground;
Adam Cohenf34bab52010-09-30 14:11:56 -0700349 }
Adam Cohen9c4949e2010-10-05 12:27:22 -0700350 if (bg != null) {
351 bg.setAlpha((int) (mBackgroundAlpha * 255));
Michael Jurka18014792010-10-14 09:01:34 -0700352 bg.setBounds(mBackgroundRect);
Adam Cohen9c4949e2010-10-05 12:27:22 -0700353 bg.draw(canvas);
354 }
Michael Jurka3e7c7632010-10-02 16:01:03 -0700355 if (mHover && getScaleX() < 0.5f) {
Michael Jurka18014792010-10-14 09:01:34 -0700356 boolean modifiedClipRect = false;
357 if (mHoverScale > 1.0f) {
358 // If the hover background's scale is greater than 1, we'll be drawing outside
359 // the bounds of this CellLayout. Get around that by temporarily increasing the
360 // size of the clip rect
361 float marginFraction = (mHoverScale - 1.0f) / 2.0f;
362 Rect clipRect = canvas.getClipBounds();
363 int marginX = (int) (marginFraction * (clipRect.right - clipRect.left));
364 int marginY = (int) (marginFraction * (clipRect.bottom - clipRect.top));
365 canvas.save(Canvas.CLIP_SAVE_FLAG);
366 canvas.clipRect(-marginX, -marginY,
367 getWidth() + marginX, getHeight() + marginY, Region.Op.REPLACE);
368 modifiedClipRect = true;
369 }
370
371 mBackgroundMiniHover.setAlpha((int) (mBackgroundAlpha * mHoverAlpha * 255));
372 mBackgroundMiniHover.setBounds(mHoverRect);
Michael Jurka3e7c7632010-10-02 16:01:03 -0700373 mBackgroundMiniHover.draw(canvas);
Michael Jurka18014792010-10-14 09:01:34 -0700374 if (modifiedClipRect) {
375 canvas.restore();
376 }
Michael Jurka3e7c7632010-10-02 16:01:03 -0700377 }
Michael Jurkaa63c4522010-08-19 13:52:27 -0700378 }
Romain Guya6abce82009-11-10 02:54:41 -0800379
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700380 if (mCrosshairsVisibility > 0.0f) {
381 final int countX = mCountX;
382 final int countY = mCountY;
383
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700384 final float MAX_ALPHA = 0.4f;
385 final int MAX_VISIBLE_DISTANCE = 600;
386 final float DISTANCE_MULTIPLIER = 0.002f;
387
388 final Drawable d = mCrosshairsDrawable;
389 final int width = d.getIntrinsicWidth();
390 final int height = d.getIntrinsicHeight();
391
392 int x = getLeftPadding() - (mWidthGap / 2) - (width / 2);
393 for (int col = 0; col <= countX; col++) {
394 int y = getTopPadding() - (mHeightGap / 2) - (height / 2);
395 for (int row = 0; row <= countY; row++) {
396 mTmpPointF.set(x - mDragCenter.x, y - mDragCenter.y);
397 float dist = mTmpPointF.length();
398 // Crosshairs further from the drag point are more faint
399 float alpha = Math.min(MAX_ALPHA,
400 DISTANCE_MULTIPLIER * (MAX_VISIBLE_DISTANCE - dist));
401 if (alpha > 0.0f) {
402 d.setBounds(x, y, x + width, y + height);
403 d.setAlpha((int) (alpha * 255 * mCrosshairsVisibility));
404 d.draw(canvas);
405 }
406 y += mCellHeight + mHeightGap;
407 }
408 x += mCellWidth + mWidthGap;
409 }
Joe Onorato4be866d2010-10-10 11:26:02 -0700410 }
Winson Chung150fbab2010-09-29 17:14:26 -0700411
Patrick Dubroy8e58e912010-10-14 13:21:48 -0700412 final Paint paint = mDragOutlinePaint;
Joe Onorato4be866d2010-10-10 11:26:02 -0700413 for (int i = 0; i < mDragOutlines.length; i++) {
Chet Haase472b2812010-10-14 07:02:04 -0700414 final float alpha = mDragOutlineAlphas[i];
Joe Onorato4be866d2010-10-10 11:26:02 -0700415 if (alpha > 0) {
416 final Point p = mDragOutlines[i];
417 final Bitmap b = (Bitmap) mDragOutlineAnims[i].getTag();
Chet Haase472b2812010-10-14 07:02:04 -0700418 paint.setAlpha((int)(alpha + .5f));
Joe Onorato4be866d2010-10-10 11:26:02 -0700419 canvas.drawBitmap(b, p.x, p.y, paint);
Winson Chung150fbab2010-09-29 17:14:26 -0700420 }
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700421 }
422 }
423
Adam Cohenf34bab52010-09-30 14:11:56 -0700424 public void setDimmableProgress(float progress) {
425 for (int i = 0; i < getChildCount(); i++) {
426 Dimmable d = (Dimmable) getChildAt(i);
427 d.setDimmableProgress(progress);
428 }
429 }
430
431 public float getDimmableProgress() {
432 if (getChildCount() > 0) {
433 return ((Dimmable) getChildAt(0)).getDimmableProgress();
434 }
435 return 0.0f;
436 }
437
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700438 @Override
Jeff Sharkey83f111d2009-04-20 21:03:13 -0700439 public void cancelLongPress() {
440 super.cancelLongPress();
441
442 // Cancel long press for all children
443 final int count = getChildCount();
444 for (int i = 0; i < count; i++) {
445 final View child = getChildAt(i);
446 child.cancelLongPress();
447 }
448 }
449
Michael Jurkadee05892010-07-27 10:01:56 -0700450 public void setOnInterceptTouchListener(View.OnTouchListener listener) {
451 mInterceptTouchListener = listener;
452 }
453
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800454 int getCountX() {
Adam Cohend22015c2010-07-26 22:02:18 -0700455 return mCountX;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800456 }
457
458 int getCountY() {
Adam Cohend22015c2010-07-26 22:02:18 -0700459 return mCountY;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800460 }
461
Winson Chungaafa03c2010-06-11 17:34:16 -0700462 public boolean addViewToCellLayout(View child, int index, int childId, LayoutParams params) {
Michael Jurkaf3ca3ab2010-10-20 17:08:24 -0700463 return addViewToCellLayout(child, index, childId, params, true);
464 }
465
466 public boolean addViewToCellLayout(
467 View child, int index, int childId, LayoutParams params, boolean markCells) {
Winson Chungaafa03c2010-06-11 17:34:16 -0700468 final LayoutParams lp = params;
469
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800470 // Generate an id for each view, this assumes we have at most 256x256 cells
471 // per workspace screen
Adam Cohend22015c2010-07-26 22:02:18 -0700472 if (lp.cellX >= 0 && lp.cellX <= mCountX - 1 && lp.cellY >= 0 && lp.cellY <= mCountY - 1) {
Winson Chungaafa03c2010-06-11 17:34:16 -0700473 // If the horizontal or vertical span is set to -1, it is taken to
474 // mean that it spans the extent of the CellLayout
Adam Cohend22015c2010-07-26 22:02:18 -0700475 if (lp.cellHSpan < 0) lp.cellHSpan = mCountX;
476 if (lp.cellVSpan < 0) lp.cellVSpan = mCountY;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800477
Winson Chungaafa03c2010-06-11 17:34:16 -0700478 child.setId(childId);
479
Michael Jurkadee05892010-07-27 10:01:56 -0700480 // We might be in the middle or end of shrinking/fading to a dimmed view
481 // Make sure this view's alpha is set the same as all the rest of the views
Michael Jurka5f1c5092010-09-03 14:15:02 -0700482 child.setAlpha(getAlpha());
Winson Chungaafa03c2010-06-11 17:34:16 -0700483 addView(child, index, lp);
Michael Jurkadee05892010-07-27 10:01:56 -0700484
Michael Jurkaf3ca3ab2010-10-20 17:08:24 -0700485 if (markCells) markCellsAsOccupiedForView(child);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700486
Winson Chungaafa03c2010-06-11 17:34:16 -0700487 return true;
488 }
489 return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800490 }
Michael Jurka3e7c7632010-10-02 16:01:03 -0700491 public void setAcceptsDrops(boolean acceptsDrops) {
492 if (mAcceptsDrops != acceptsDrops) {
493 mAcceptsDrops = acceptsDrops;
494 invalidate();
495 }
496 }
497
498 public boolean getAcceptsDrops() {
499 return mAcceptsDrops;
500 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800501
502 @Override
Michael Jurka0280c3b2010-09-17 15:00:07 -0700503 public void removeAllViews() {
504 clearOccupiedCells();
505 }
506
507 @Override
508 public void removeAllViewsInLayout() {
509 clearOccupiedCells();
510 }
511
Michael Jurkaf3ca3ab2010-10-20 17:08:24 -0700512 public void removeViewWithoutMarkingCells(View view) {
513 super.removeView(view);
514 }
515
Michael Jurka0280c3b2010-09-17 15:00:07 -0700516 @Override
517 public void removeView(View view) {
518 markCellsAsUnoccupiedForView(view);
519 super.removeView(view);
520 }
521
522 @Override
523 public void removeViewAt(int index) {
524 markCellsAsUnoccupiedForView(getChildAt(index));
525 super.removeViewAt(index);
526 }
527
528 @Override
529 public void removeViewInLayout(View view) {
530 markCellsAsUnoccupiedForView(view);
531 super.removeViewInLayout(view);
532 }
533
534 @Override
535 public void removeViews(int start, int count) {
536 for (int i = start; i < start + count; i++) {
537 markCellsAsUnoccupiedForView(getChildAt(i));
538 }
539 super.removeViews(start, count);
540 }
541
542 @Override
543 public void removeViewsInLayout(int start, int count) {
544 for (int i = start; i < start + count; i++) {
545 markCellsAsUnoccupiedForView(getChildAt(i));
546 }
547 super.removeViewsInLayout(start, count);
548 }
549
550 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800551 public void requestChildFocus(View child, View focused) {
552 super.requestChildFocus(child, focused);
553 if (child != null) {
554 Rect r = new Rect();
555 child.getDrawingRect(r);
556 requestRectangleOnScreen(r);
557 }
558 }
559
560 @Override
561 protected void onAttachedToWindow() {
562 super.onAttachedToWindow();
563 mCellInfo.screen = ((ViewGroup) getParent()).indexOfChild(this);
564 }
565
Michael Jurkaaf442092010-06-10 17:01:57 -0700566 public void setTagToCellInfoForPoint(int touchX, int touchY) {
567 final CellInfo cellInfo = mCellInfo;
568 final Rect frame = mRect;
569 final int x = touchX + mScrollX;
570 final int y = touchY + mScrollY;
571 final int count = getChildCount();
572
573 boolean found = false;
574 for (int i = count - 1; i >= 0; i--) {
575 final View child = getChildAt(i);
576
577 if ((child.getVisibility()) == VISIBLE || child.getAnimation() != null) {
578 child.getHitRect(frame);
579 if (frame.contains(x, y)) {
580 final LayoutParams lp = (LayoutParams) child.getLayoutParams();
581 cellInfo.cell = child;
582 cellInfo.cellX = lp.cellX;
583 cellInfo.cellY = lp.cellY;
584 cellInfo.spanX = lp.cellHSpan;
585 cellInfo.spanY = lp.cellVSpan;
586 cellInfo.valid = true;
587 found = true;
Michael Jurkaaf442092010-06-10 17:01:57 -0700588 break;
589 }
590 }
591 }
Winson Chungaafa03c2010-06-11 17:34:16 -0700592
Michael Jurkaaf442092010-06-10 17:01:57 -0700593 if (!found) {
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700594 final int cellXY[] = mTmpCellXY;
Michael Jurkaaf442092010-06-10 17:01:57 -0700595 pointToCellExact(x, y, cellXY);
596
Michael Jurkaaf442092010-06-10 17:01:57 -0700597 cellInfo.cell = null;
598 cellInfo.cellX = cellXY[0];
599 cellInfo.cellY = cellXY[1];
600 cellInfo.spanX = 1;
601 cellInfo.spanY = 1;
Michael Jurka0280c3b2010-09-17 15:00:07 -0700602 cellInfo.valid = cellXY[0] >= 0 && cellXY[1] >= 0 && cellXY[0] < mCountX &&
603 cellXY[1] < mCountY && !mOccupied[cellXY[0]][cellXY[1]];
Michael Jurkaaf442092010-06-10 17:01:57 -0700604 }
605 setTag(cellInfo);
606 }
607
Winson Chungaafa03c2010-06-11 17:34:16 -0700608
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800609 @Override
610 public boolean onInterceptTouchEvent(MotionEvent ev) {
Michael Jurkadee05892010-07-27 10:01:56 -0700611 if (mInterceptTouchListener != null && mInterceptTouchListener.onTouch(this, ev)) {
612 return true;
613 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800614 final int action = ev.getAction();
615 final CellInfo cellInfo = mCellInfo;
616
617 if (action == MotionEvent.ACTION_DOWN) {
Michael Jurkaaf442092010-06-10 17:01:57 -0700618 setTagToCellInfoForPoint((int) ev.getX(), (int) ev.getY());
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800619 } else if (action == MotionEvent.ACTION_UP) {
620 cellInfo.cell = null;
621 cellInfo.cellX = -1;
622 cellInfo.cellY = -1;
623 cellInfo.spanX = 0;
624 cellInfo.spanY = 0;
625 cellInfo.valid = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800626 setTag(cellInfo);
627 }
628
629 return false;
630 }
631
632 @Override
633 public CellInfo getTag() {
Michael Jurka0280c3b2010-09-17 15:00:07 -0700634 return (CellInfo) super.getTag();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800635 }
636
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700637 /**
638 * Check if the row 'y' is empty from columns 'left' to 'right', inclusive.
639 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800640 private static boolean isRowEmpty(int y, int left, int right, boolean[][] occupied) {
641 for (int x = left; x <= right; x++) {
642 if (occupied[x][y]) {
643 return false;
644 }
645 }
646 return true;
647 }
648
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800649 /**
Winson Chungaafa03c2010-06-11 17:34:16 -0700650 * Given a point, return the cell that strictly encloses that point
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800651 * @param x X coordinate of the point
652 * @param y Y coordinate of the point
653 * @param result Array of 2 ints to hold the x and y coordinate of the cell
654 */
655 void pointToCellExact(int x, int y, int[] result) {
Winson Chungaafa03c2010-06-11 17:34:16 -0700656 final int hStartPadding = getLeftPadding();
657 final int vStartPadding = getTopPadding();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800658
659 result[0] = (x - hStartPadding) / (mCellWidth + mWidthGap);
660 result[1] = (y - vStartPadding) / (mCellHeight + mHeightGap);
661
Adam Cohend22015c2010-07-26 22:02:18 -0700662 final int xAxis = mCountX;
663 final int yAxis = mCountY;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800664
665 if (result[0] < 0) result[0] = 0;
666 if (result[0] >= xAxis) result[0] = xAxis - 1;
667 if (result[1] < 0) result[1] = 0;
668 if (result[1] >= yAxis) result[1] = yAxis - 1;
669 }
Winson Chungaafa03c2010-06-11 17:34:16 -0700670
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800671 /**
672 * Given a point, return the cell that most closely encloses that point
673 * @param x X coordinate of the point
674 * @param y Y coordinate of the point
675 * @param result Array of 2 ints to hold the x and y coordinate of the cell
676 */
677 void pointToCellRounded(int x, int y, int[] result) {
678 pointToCellExact(x + (mCellWidth / 2), y + (mCellHeight / 2), result);
679 }
680
681 /**
682 * Given a cell coordinate, return the point that represents the upper left corner of that cell
Winson Chungaafa03c2010-06-11 17:34:16 -0700683 *
684 * @param cellX X coordinate of the cell
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800685 * @param cellY Y coordinate of the cell
Winson Chungaafa03c2010-06-11 17:34:16 -0700686 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800687 * @param result Array of 2 ints to hold the x and y coordinate of the point
688 */
689 void cellToPoint(int cellX, int cellY, int[] result) {
Winson Chungaafa03c2010-06-11 17:34:16 -0700690 final int hStartPadding = getLeftPadding();
691 final int vStartPadding = getTopPadding();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800692
693 result[0] = hStartPadding + cellX * (mCellWidth + mWidthGap);
694 result[1] = vStartPadding + cellY * (mCellHeight + mHeightGap);
695 }
696
Romain Guy84f296c2009-11-04 15:00:44 -0800697 int getCellWidth() {
698 return mCellWidth;
699 }
700
701 int getCellHeight() {
702 return mCellHeight;
703 }
704
Romain Guy1a304a12009-11-10 00:02:32 -0800705 int getLeftPadding() {
Winson Chungaafa03c2010-06-11 17:34:16 -0700706 return mLeftPadding;
Romain Guy1a304a12009-11-10 00:02:32 -0800707 }
708
709 int getTopPadding() {
Winson Chungaafa03c2010-06-11 17:34:16 -0700710 return mTopPadding;
Romain Guy1a304a12009-11-10 00:02:32 -0800711 }
712
713 int getRightPadding() {
Winson Chungaafa03c2010-06-11 17:34:16 -0700714 return mRightPadding;
Romain Guy1a304a12009-11-10 00:02:32 -0800715 }
716
717 int getBottomPadding() {
Winson Chungaafa03c2010-06-11 17:34:16 -0700718 return mBottomPadding;
Romain Guy1a304a12009-11-10 00:02:32 -0800719 }
720
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800721 @Override
722 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
723 // TODO: currently ignoring padding
Winson Chungaafa03c2010-06-11 17:34:16 -0700724
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800725 int widthSpecMode = MeasureSpec.getMode(widthMeasureSpec);
Winson Chungaafa03c2010-06-11 17:34:16 -0700726 int widthSpecSize = MeasureSpec.getSize(widthMeasureSpec);
727
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800728 int heightSpecMode = MeasureSpec.getMode(heightMeasureSpec);
729 int heightSpecSize = MeasureSpec.getSize(heightMeasureSpec);
Winson Chungaafa03c2010-06-11 17:34:16 -0700730
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800731 if (widthSpecMode == MeasureSpec.UNSPECIFIED || heightSpecMode == MeasureSpec.UNSPECIFIED) {
732 throw new RuntimeException("CellLayout cannot have UNSPECIFIED dimensions");
733 }
734
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800735 final int cellWidth = mCellWidth;
736 final int cellHeight = mCellHeight;
737
Adam Cohend22015c2010-07-26 22:02:18 -0700738 int numWidthGaps = mCountX - 1;
739 int numHeightGaps = mCountY - 1;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800740
Winson Chungece7f5b2010-10-22 14:54:12 -0700741 if (mWidthGap < 0 || mHeightGap < 0) {
742 int vSpaceLeft = heightSpecSize - mTopPadding - mBottomPadding - (cellHeight * mCountY);
743 mHeightGap = vSpaceLeft / numHeightGaps;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800744
Winson Chungece7f5b2010-10-22 14:54:12 -0700745 int hSpaceLeft = widthSpecSize - mLeftPadding - mRightPadding - (cellWidth * mCountX);
746 mWidthGap = hSpaceLeft / numWidthGaps;
Winson Chungaafa03c2010-06-11 17:34:16 -0700747
Winson Chungece7f5b2010-10-22 14:54:12 -0700748 // center it around the min gaps
749 int minGap = Math.min(mWidthGap, mHeightGap);
750 mWidthGap = mHeightGap = minGap;
751 }
Michael Jurka5f1c5092010-09-03 14:15:02 -0700752
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800753 int count = getChildCount();
754
755 for (int i = 0; i < count; i++) {
756 View child = getChildAt(i);
757 LayoutParams lp = (LayoutParams) child.getLayoutParams();
Winson Chungaafa03c2010-06-11 17:34:16 -0700758 lp.setup(cellWidth, cellHeight, mWidthGap, mHeightGap,
759 mLeftPadding, mTopPadding);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800760
Michael Jurka0280c3b2010-09-17 15:00:07 -0700761 int childWidthMeasureSpec = MeasureSpec.makeMeasureSpec(lp.width, MeasureSpec.EXACTLY);
Winson Chungaafa03c2010-06-11 17:34:16 -0700762 int childheightMeasureSpec = MeasureSpec.makeMeasureSpec(lp.height,
763 MeasureSpec.EXACTLY);
764
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800765 child.measure(childWidthMeasureSpec, childheightMeasureSpec);
766 }
Michael Jurka5f1c5092010-09-03 14:15:02 -0700767 if (widthSpecMode == MeasureSpec.AT_MOST) {
768 int newWidth = mLeftPadding + mRightPadding + (mCountX * cellWidth) +
Winson Chungece7f5b2010-10-22 14:54:12 -0700769 ((mCountX - 1) * mWidthGap);
Michael Jurka5f1c5092010-09-03 14:15:02 -0700770 int newHeight = mTopPadding + mBottomPadding + (mCountY * cellHeight) +
Winson Chungece7f5b2010-10-22 14:54:12 -0700771 ((mCountY - 1) * mHeightGap);
Michael Jurka5f1c5092010-09-03 14:15:02 -0700772 setMeasuredDimension(newWidth, newHeight);
773 } else if (widthSpecMode == MeasureSpec.EXACTLY) {
774 setMeasuredDimension(widthSpecSize, heightSpecSize);
775 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800776 }
777
Patrick Dubroyce34a972010-10-19 10:34:32 -0700778 /**
779 * Animate a child of this CellLayout into its current layout position.
780 * The position to animate from is given by the oldX and oldY values in its LayoutParams.
781 */
782 private void animateChildIntoPosition(final View child) {
783 final Resources res = getResources();
Patrick Dubroya9442152010-10-25 11:41:41 -0700784 final ValueAnimator anim = mDropAnim;
Patrick Dubroyce34a972010-10-19 10:34:32 -0700785 final CellLayout.LayoutParams lp = (CellLayout.LayoutParams) child.getLayoutParams();
786 final float startX = lp.oldX - lp.x;
787 final float startY = lp.oldY - lp.y;
788
789 // Calculate the duration of the animation based on the object's distance
790 final float dist = (float) Math.sqrt(startX*startX + startY*startY);
791 final float maxDist = (float) res.getInteger(R.integer.config_dropAnimMaxDist);
792 final int duration = (int) (res.getInteger(R.integer.config_dropAnimMaxDuration)
793 * mEaseOutInterpolator.getInterpolation(dist / maxDist));
794
Patrick Dubroya9442152010-10-25 11:41:41 -0700795 anim.end(); // Make sure it's not already running
Patrick Dubroyce34a972010-10-19 10:34:32 -0700796 anim.setDuration(duration);
Patrick Dubroya9442152010-10-25 11:41:41 -0700797 anim.setFloatValues(1.0f, 0.0f);
Patrick Dubroyce34a972010-10-19 10:34:32 -0700798 anim.removeAllUpdateListeners();
799 anim.addUpdateListener(new AnimatorUpdateListener() {
800 public void onAnimationUpdate(ValueAnimator animation) {
Patrick Dubroya9442152010-10-25 11:41:41 -0700801 final float value = (Float) anim.getAnimatedValue();
802 child.setTranslationX(startX * value);
803 child.setTranslationY(startY * value);
Patrick Dubroyce34a972010-10-19 10:34:32 -0700804 }
805 });
806 anim.start();
807 }
808
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800809 @Override
Michael Jurka28750fb2010-09-24 17:43:49 -0700810 protected void onLayout(boolean changed, int l, int t, int r, int b) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800811 int count = getChildCount();
812
813 for (int i = 0; i < count; i++) {
Patrick Dubroyce34a972010-10-19 10:34:32 -0700814 final View child = getChildAt(i);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800815 if (child.getVisibility() != GONE) {
816
817 CellLayout.LayoutParams lp = (CellLayout.LayoutParams) child.getLayoutParams();
818
819 int childLeft = lp.x;
820 int childTop = lp.y;
821 child.layout(childLeft, childTop, childLeft + lp.width, childTop + lp.height);
Romain Guy84f296c2009-11-04 15:00:44 -0800822
823 if (lp.dropped) {
824 lp.dropped = false;
825
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700826 final int[] cellXY = mTmpCellXY;
Romain Guy06762ab2010-01-25 16:51:08 -0800827 getLocationOnScreen(cellXY);
Romain Guy84f296c2009-11-04 15:00:44 -0800828 mWallpaperManager.sendWallpaperCommand(getWindowToken(), "android.home.drop",
Romain Guy06762ab2010-01-25 16:51:08 -0800829 cellXY[0] + childLeft + lp.width / 2,
830 cellXY[1] + childTop + lp.height / 2, 0, null);
Patrick Dubroyce34a972010-10-19 10:34:32 -0700831
832 animateChildIntoPosition(child);
Romain Guy84f296c2009-11-04 15:00:44 -0800833 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800834 }
835 }
836 }
837
838 @Override
Michael Jurkadee05892010-07-27 10:01:56 -0700839 protected void onSizeChanged(int w, int h, int oldw, int oldh) {
840 super.onSizeChanged(w, h, oldw, oldh);
Michael Jurka18014792010-10-14 09:01:34 -0700841 mBackgroundRect.set(0, 0, w, h);
842 updateHoverRect();
Michael Jurkadee05892010-07-27 10:01:56 -0700843 }
844
845 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800846 protected void setChildrenDrawingCacheEnabled(boolean enabled) {
847 final int count = getChildCount();
848 for (int i = 0; i < count; i++) {
849 final View view = getChildAt(i);
850 view.setDrawingCacheEnabled(enabled);
851 // Update the drawing caches
Romain Guy3cf4c032010-10-26 14:29:35 -0700852 if (!view.isHardwareAccelerated()) {
853 view.buildDrawingCache(true);
854 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800855 }
856 }
857
858 @Override
859 protected void setChildrenDrawnWithCacheEnabled(boolean enabled) {
860 super.setChildrenDrawnWithCacheEnabled(enabled);
861 }
862
Michael Jurka5f1c5092010-09-03 14:15:02 -0700863 public float getBackgroundAlpha() {
864 return mBackgroundAlpha;
Michael Jurkadee05892010-07-27 10:01:56 -0700865 }
866
Michael Jurka5f1c5092010-09-03 14:15:02 -0700867 public void setBackgroundAlpha(float alpha) {
868 mBackgroundAlpha = alpha;
Michael Jurka0142d492010-08-25 17:46:15 -0700869 invalidate();
Michael Jurkadee05892010-07-27 10:01:56 -0700870 }
871
Michael Jurka5f1c5092010-09-03 14:15:02 -0700872 // Need to return true to let the view system know we know how to handle alpha-- this is
873 // because when our children have an alpha of 0.0f, they are still rendering their "dimmed"
874 // versions
875 @Override
876 protected boolean onSetAlpha(int alpha) {
877 return true;
878 }
879
880 public void setAlpha(float alpha) {
881 setChildrenAlpha(alpha);
882 super.setAlpha(alpha);
883 }
884
Michael Jurkadee05892010-07-27 10:01:56 -0700885 private void setChildrenAlpha(float alpha) {
Michael Jurka0142d492010-08-25 17:46:15 -0700886 final int childCount = getChildCount();
887 for (int i = 0; i < childCount; i++) {
Michael Jurkadee05892010-07-27 10:01:56 -0700888 getChildAt(i).setAlpha(alpha);
889 }
890 }
891
Michael Jurka0280c3b2010-09-17 15:00:07 -0700892 private boolean isVacantIgnoring(
893 int originX, int originY, int spanX, int spanY, View ignoreView) {
894 if (ignoreView != null) {
895 markCellsAsUnoccupiedForView(ignoreView);
896 }
Michael Jurka28750fb2010-09-24 17:43:49 -0700897 boolean isVacant = true;
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700898 for (int i = 0; i < spanY; i++) {
899 if (!isRowEmpty(originY + i, originX, originX + spanX - 1, mOccupied)) {
Michael Jurka28750fb2010-09-24 17:43:49 -0700900 isVacant = false;
901 break;
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700902 }
903 }
Michael Jurka0280c3b2010-09-17 15:00:07 -0700904 if (ignoreView != null) {
905 markCellsAsOccupiedForView(ignoreView);
906 }
Michael Jurka28750fb2010-09-24 17:43:49 -0700907 return isVacant;
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700908 }
909
Michael Jurka0280c3b2010-09-17 15:00:07 -0700910 private boolean isVacant(int originX, int originY, int spanX, int spanY) {
911 return isVacantIgnoring(originX, originY, spanX, spanY, null);
912 }
913
Patrick Dubroy440c3602010-07-13 17:50:32 -0700914 public View getChildAt(int x, int y) {
915 final int count = getChildCount();
916 for (int i = 0; i < count; i++) {
917 View child = getChildAt(i);
918 LayoutParams lp = (LayoutParams) child.getLayoutParams();
919
920 if ((lp.cellX <= x) && (x < lp.cellX + lp.cellHSpan) &&
921 (lp.cellY <= y) && (y < lp.cellY + lp.cellHSpan)) {
922 return child;
923 }
924 }
925 return null;
926 }
927
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700928 /**
Patrick Dubroy8f86ddc2010-07-16 13:55:32 -0700929 * Estimate the size that a child with the given dimensions will take in the layout.
930 */
931 void estimateChildSize(int minWidth, int minHeight, int[] result) {
932 // Assuming it's placed at 0, 0, find where the bottom right cell will land
933 rectToCell(minWidth, minHeight, result);
934
935 // Then figure out the rect it will occupy
936 cellToRect(0, 0, result[0], result[1], mRectF);
937 result[0] = (int)mRectF.width();
938 result[1] = (int)mRectF.height();
939 }
940
941 /**
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700942 * Estimate where the top left cell of the dragged item will land if it is dropped.
943 *
944 * @param originX The X value of the top left corner of the item
945 * @param originY The Y value of the top left corner of the item
946 * @param spanX The number of horizontal cells that the item spans
947 * @param spanY The number of vertical cells that the item spans
948 * @param result The estimated drop cell X and Y.
949 */
950 void estimateDropCell(int originX, int originY, int spanX, int spanY, int[] result) {
Adam Cohend22015c2010-07-26 22:02:18 -0700951 final int countX = mCountX;
952 final int countY = mCountY;
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700953
Michael Jurkaa63c4522010-08-19 13:52:27 -0700954 // pointToCellRounded takes the top left of a cell but will pad that with
955 // cellWidth/2 and cellHeight/2 when finding the matching cell
956 pointToCellRounded(originX, originY, result);
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700957
958 // If the item isn't fully on this screen, snap to the edges
959 int rightOverhang = result[0] + spanX - countX;
960 if (rightOverhang > 0) {
961 result[0] -= rightOverhang; // Snap to right
962 }
963 result[0] = Math.max(0, result[0]); // Snap to left
964 int bottomOverhang = result[1] + spanY - countY;
965 if (bottomOverhang > 0) {
966 result[1] -= bottomOverhang; // Snap to bottom
967 }
968 result[1] = Math.max(0, result[1]); // Snap to top
969 }
970
Joe Onorato4be866d2010-10-10 11:26:02 -0700971 void visualizeDropLocation(
972 View v, Bitmap dragOutline, int originX, int originY, int spanX, int spanY) {
973
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -0700974 final int oldDragCellX = mDragCell[0];
975 final int oldDragCellY = mDragCell[1];
Joe Onorato4be866d2010-10-10 11:26:02 -0700976 final int[] nearest = findNearestVacantArea(originX, originY, spanX, spanY, v, mDragCell);
Winson Chunga9abd0e2010-10-27 17:18:37 -0700977 if (v != null) {
978 mDragCenter.set(originX + (v.getWidth() / 2), originY + (v.getHeight() / 2));
979 } else {
980 mDragCenter.set(originX, originY);
981 }
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700982
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -0700983 if (nearest != null && (nearest[0] != oldDragCellX || nearest[1] != oldDragCellY)) {
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700984 // Find the top left corner of the rect the object will occupy
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700985 final int[] topLeft = mTmpPoint;
986 cellToPoint(nearest[0], nearest[1], topLeft);
987
Joe Onorato4be866d2010-10-10 11:26:02 -0700988 int left = topLeft[0];
989 int top = topLeft[1];
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700990
Winson Chunga9abd0e2010-10-27 17:18:37 -0700991 if (v != null) {
992 if (v.getParent() instanceof CellLayout) {
993 LayoutParams lp = (LayoutParams) v.getLayoutParams();
994 left += lp.leftMargin;
995 top += lp.topMargin;
996 }
Winson Chung150fbab2010-09-29 17:14:26 -0700997
Winson Chunga9abd0e2010-10-27 17:18:37 -0700998 // Offsets due to the size difference between the View and the dragOutline
999 left += (v.getWidth() - dragOutline.getWidth()) / 2;
1000 top += (v.getHeight() - dragOutline.getHeight()) / 2;
1001 }
Winson Chung150fbab2010-09-29 17:14:26 -07001002
Joe Onorato4be866d2010-10-10 11:26:02 -07001003 final int oldIndex = mDragOutlineCurrent;
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -07001004 mDragOutlineAnims[oldIndex].animateOut();
1005 mDragOutlineCurrent = (oldIndex + 1) % mDragOutlines.length;
Winson Chung150fbab2010-09-29 17:14:26 -07001006
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -07001007 mDragOutlines[mDragOutlineCurrent].set(left, top);
1008 mDragOutlineAnims[mDragOutlineCurrent].setTag(dragOutline);
1009 mDragOutlineAnims[mDragOutlineCurrent].animateIn();
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001010 }
Patrick Dubroy49250ad2010-10-08 15:33:52 -07001011
1012 // If we are drawing crosshairs, the entire CellLayout needs to be invalidated
1013 if (mCrosshairsDrawable != null) {
1014 invalidate();
1015 }
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001016 }
1017
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001018 /**
Jeff Sharkey70864282009-04-07 21:08:40 -07001019 * Find a vacant area that will fit the given bounds nearest the requested
1020 * cell location. Uses Euclidean distance to score multiple vacant areas.
Winson Chungaafa03c2010-06-11 17:34:16 -07001021 *
Romain Guy51afc022009-05-04 18:03:43 -07001022 * @param pixelX The X location at which you want to search for a vacant area.
1023 * @param pixelY The Y location at which you want to search for a vacant area.
Jeff Sharkey70864282009-04-07 21:08:40 -07001024 * @param spanX Horizontal span of the object.
1025 * @param spanY Vertical span of the object.
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001026 * @param result Array in which to place the result, or null (in which case a new array will
1027 * be allocated)
Jeff Sharkey70864282009-04-07 21:08:40 -07001028 * @return The X, Y cell of a vacant area that can contain this object,
1029 * nearest the requested location.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001030 */
Michael Jurka6a1435d2010-09-27 17:35:12 -07001031 int[] findNearestVacantArea(
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001032 int pixelX, int pixelY, int spanX, int spanY, int[] result) {
1033 return findNearestVacantArea(pixelX, pixelY, spanX, spanY, null, result);
Michael Jurka6a1435d2010-09-27 17:35:12 -07001034 }
Winson Chungaafa03c2010-06-11 17:34:16 -07001035
Michael Jurka6a1435d2010-09-27 17:35:12 -07001036 /**
1037 * Find a vacant area that will fit the given bounds nearest the requested
1038 * cell location. Uses Euclidean distance to score multiple vacant areas.
1039 *
1040 * @param pixelX The X location at which you want to search for a vacant area.
1041 * @param pixelY The Y location at which you want to search for a vacant area.
1042 * @param spanX Horizontal span of the object.
1043 * @param spanY Vertical span of the object.
Michael Jurka6a1435d2010-09-27 17:35:12 -07001044 * @param ignoreView Considers space occupied by this view as unoccupied
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001045 * @param result Previously returned value to possibly recycle.
Michael Jurka6a1435d2010-09-27 17:35:12 -07001046 * @return The X, Y cell of a vacant area that can contain this object,
1047 * nearest the requested location.
1048 */
1049 int[] findNearestVacantArea(
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001050 int pixelX, int pixelY, int spanX, int spanY, View ignoreView, int[] result) {
Michael Jurkac6ee42e2010-09-30 12:04:50 -07001051 // mark space take by ignoreView as available (method checks if ignoreView is null)
1052 markCellsAsUnoccupiedForView(ignoreView);
1053
Jeff Sharkey70864282009-04-07 21:08:40 -07001054 // Keep track of best-scoring drop area
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001055 final int[] bestXY = result != null ? result : new int[2];
Jeff Sharkey70864282009-04-07 21:08:40 -07001056 double bestDistance = Double.MAX_VALUE;
Winson Chungaafa03c2010-06-11 17:34:16 -07001057
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001058 final int countX = mCountX;
1059 final int countY = mCountY;
1060 final boolean[][] occupied = mOccupied;
1061
1062 for (int x = 0; x < countX - (spanX - 1); x++) {
Michael Jurkac28de512010-08-13 11:27:44 -07001063 inner:
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001064 for (int y = 0; y < countY - (spanY - 1); y++) {
Michael Jurkac28de512010-08-13 11:27:44 -07001065 for (int i = 0; i < spanX; i++) {
1066 for (int j = 0; j < spanY; j++) {
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001067 if (occupied[x + i][y + j]) {
Michael Jurkac28de512010-08-13 11:27:44 -07001068 // small optimization: we can skip to below the row we just found
1069 // an occupied cell
1070 y += j;
1071 continue inner;
1072 }
1073 }
1074 }
1075 final int[] cellXY = mTmpCellXY;
1076 cellToPoint(x, y, cellXY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001077
Michael Jurkac28de512010-08-13 11:27:44 -07001078 double distance = Math.sqrt(Math.pow(cellXY[0] - pixelX, 2)
1079 + Math.pow(cellXY[1] - pixelY, 2));
1080 if (distance <= bestDistance) {
1081 bestDistance = distance;
1082 bestXY[0] = x;
1083 bestXY[1] = y;
1084 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001085 }
1086 }
Michael Jurkac6ee42e2010-09-30 12:04:50 -07001087 // re-mark space taken by ignoreView as occupied
1088 markCellsAsOccupiedForView(ignoreView);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001089
Winson Chungaafa03c2010-06-11 17:34:16 -07001090 // Return null if no suitable location found
Jeff Sharkey70864282009-04-07 21:08:40 -07001091 if (bestDistance < Double.MAX_VALUE) {
1092 return bestXY;
1093 } else {
1094 return null;
1095 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001096 }
Winson Chungaafa03c2010-06-11 17:34:16 -07001097
Michael Jurka0280c3b2010-09-17 15:00:07 -07001098 boolean existsEmptyCell() {
1099 return findCellForSpan(null, 1, 1);
1100 }
1101
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001102 /**
Michael Jurka0280c3b2010-09-17 15:00:07 -07001103 * Finds the upper-left coordinate of the first rectangle in the grid that can
1104 * hold a cell of the specified dimensions. If intersectX and intersectY are not -1,
1105 * then this method will only return coordinates for rectangles that contain the cell
1106 * (intersectX, intersectY)
1107 *
1108 * @param cellXY The array that will contain the position of a vacant cell if such a cell
1109 * can be found.
1110 * @param spanX The horizontal span of the cell we want to find.
1111 * @param spanY The vertical span of the cell we want to find.
1112 *
1113 * @return True if a vacant cell of the specified dimension was found, false otherwise.
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001114 */
Michael Jurka0280c3b2010-09-17 15:00:07 -07001115 boolean findCellForSpan(int[] cellXY, int spanX, int spanY) {
1116 return findCellForSpanThatIntersectsIgnoring(cellXY, spanX, spanY, -1, -1, null);
1117 }
1118
1119 /**
1120 * Like above, but ignores any cells occupied by the item "ignoreView"
1121 *
1122 * @param cellXY The array that will contain the position of a vacant cell if such a cell
1123 * can be found.
1124 * @param spanX The horizontal span of the cell we want to find.
1125 * @param spanY The vertical span of the cell we want to find.
1126 * @param ignoreView The home screen item we should treat as not occupying any space
1127 * @return
1128 */
1129 boolean findCellForSpanIgnoring(int[] cellXY, int spanX, int spanY, View ignoreView) {
1130 return findCellForSpanThatIntersectsIgnoring(cellXY, spanX, spanY, -1, -1, ignoreView);
1131 }
1132
1133 /**
1134 * Like above, but if intersectX and intersectY are not -1, then this method will try to
1135 * return coordinates for rectangles that contain the cell [intersectX, intersectY]
1136 *
1137 * @param spanX The horizontal span of the cell we want to find.
1138 * @param spanY The vertical span of the cell we want to find.
1139 * @param ignoreView The home screen item we should treat as not occupying any space
1140 * @param intersectX The X coordinate of the cell that we should try to overlap
1141 * @param intersectX The Y coordinate of the cell that we should try to overlap
1142 *
1143 * @return True if a vacant cell of the specified dimension was found, false otherwise.
1144 */
1145 boolean findCellForSpanThatIntersects(int[] cellXY, int spanX, int spanY,
1146 int intersectX, int intersectY) {
1147 return findCellForSpanThatIntersectsIgnoring(
1148 cellXY, spanX, spanY, intersectX, intersectY, null);
1149 }
1150
1151 /**
1152 * The superset of the above two methods
1153 */
1154 boolean findCellForSpanThatIntersectsIgnoring(int[] cellXY, int spanX, int spanY,
1155 int intersectX, int intersectY, View ignoreView) {
Michael Jurkac6ee42e2010-09-30 12:04:50 -07001156 // mark space take by ignoreView as available (method checks if ignoreView is null)
1157 markCellsAsUnoccupiedForView(ignoreView);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001158
Michael Jurka28750fb2010-09-24 17:43:49 -07001159 boolean foundCell = false;
Michael Jurka0280c3b2010-09-17 15:00:07 -07001160 while (true) {
1161 int startX = 0;
1162 if (intersectX >= 0) {
1163 startX = Math.max(startX, intersectX - (spanX - 1));
1164 }
1165 int endX = mCountX - (spanX - 1);
1166 if (intersectX >= 0) {
1167 endX = Math.min(endX, intersectX + (spanX - 1) + (spanX == 1 ? 1 : 0));
1168 }
1169 int startY = 0;
1170 if (intersectY >= 0) {
1171 startY = Math.max(startY, intersectY - (spanY - 1));
1172 }
1173 int endY = mCountY - (spanY - 1);
1174 if (intersectY >= 0) {
1175 endY = Math.min(endY, intersectY + (spanY - 1) + (spanY == 1 ? 1 : 0));
1176 }
1177
1178 for (int x = startX; x < endX; x++) {
1179 inner:
1180 for (int y = startY; y < endY; y++) {
1181 for (int i = 0; i < spanX; i++) {
1182 for (int j = 0; j < spanY; j++) {
1183 if (mOccupied[x + i][y + j]) {
1184 // small optimization: we can skip to below the row we just found
1185 // an occupied cell
1186 y += j;
1187 continue inner;
1188 }
1189 }
1190 }
1191 if (cellXY != null) {
1192 cellXY[0] = x;
1193 cellXY[1] = y;
1194 }
Michael Jurka28750fb2010-09-24 17:43:49 -07001195 foundCell = true;
1196 break;
Michael Jurka0280c3b2010-09-17 15:00:07 -07001197 }
1198 }
1199 if (intersectX == -1 && intersectY == -1) {
1200 break;
1201 } else {
1202 // if we failed to find anything, try again but without any requirements of
1203 // intersecting
1204 intersectX = -1;
1205 intersectY = -1;
1206 continue;
1207 }
1208 }
1209
Michael Jurkac6ee42e2010-09-30 12:04:50 -07001210 // re-mark space taken by ignoreView as occupied
1211 markCellsAsOccupiedForView(ignoreView);
Michael Jurka28750fb2010-09-24 17:43:49 -07001212 return foundCell;
Michael Jurka0280c3b2010-09-17 15:00:07 -07001213 }
1214
1215 /**
1216 * Called when drag has left this CellLayout or has been completed (successfully or not)
1217 */
1218 void onDragExit() {
Joe Onorato4be866d2010-10-10 11:26:02 -07001219 // This can actually be called when we aren't in a drag, e.g. when adding a new
1220 // item to this layout via the customize drawer.
1221 // Guard against that case.
1222 if (mDragging) {
1223 mDragging = false;
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001224
Joe Onorato4be866d2010-10-10 11:26:02 -07001225 // Fade out the drag indicators
1226 if (mCrosshairsAnimator != null) {
1227 mCrosshairsAnimator.animateOut();
1228 }
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001229 }
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -07001230
1231 // Invalidate the drag data
1232 mDragCell[0] = -1;
1233 mDragCell[1] = -1;
1234 mDragOutlineAnims[mDragOutlineCurrent].animateOut();
1235 mDragOutlineCurrent = (mDragOutlineCurrent + 1) % mDragOutlineAnims.length;
1236
1237 setHover(false);
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001238 }
1239
1240 /**
Winson Chungaafa03c2010-06-11 17:34:16 -07001241 * Mark a child as having been dropped.
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001242 * At the beginning of the drag operation, the child may have been on another
Patrick Dubroyce34a972010-10-19 10:34:32 -07001243 * screen, but it is re-parented before this method is called.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001244 *
1245 * @param child The child that is being dropped
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001246 */
Winson Chungaafa03c2010-06-11 17:34:16 -07001247 void onDropChild(View child) {
Romain Guyd94533d2009-08-17 10:01:15 -07001248 if (child != null) {
1249 LayoutParams lp = (LayoutParams) child.getLayoutParams();
Romain Guyd94533d2009-08-17 10:01:15 -07001250 lp.isDragging = false;
Romain Guy84f296c2009-11-04 15:00:44 -08001251 lp.dropped = true;
Patrick Dubroyce34a972010-10-19 10:34:32 -07001252 child.setVisibility(View.VISIBLE);
Romain Guyd94533d2009-08-17 10:01:15 -07001253 child.requestLayout();
Romain Guyd94533d2009-08-17 10:01:15 -07001254 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001255 }
1256
1257 void onDropAborted(View child) {
1258 if (child != null) {
Patrick Dubroyce34a972010-10-19 10:34:32 -07001259 LayoutParams lp = (LayoutParams) child.getLayoutParams();
1260 lp.isDragging = false;
1261 child.setVisibility(View.VISIBLE);
1262 animateChildIntoPosition(child);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001263 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001264 }
1265
1266 /**
1267 * Start dragging the specified child
Winson Chungaafa03c2010-06-11 17:34:16 -07001268 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001269 * @param child The child that is being dragged
1270 */
1271 void onDragChild(View child) {
1272 LayoutParams lp = (LayoutParams) child.getLayoutParams();
1273 lp.isDragging = true;
Patrick Dubroyce34a972010-10-19 10:34:32 -07001274 child.setVisibility(View.GONE);
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001275 }
1276
1277 /**
1278 * A drag event has begun over this layout.
1279 * It may have begun over this layout (in which case onDragChild is called first),
1280 * or it may have begun on another layout.
1281 */
Winson Chunga9abd0e2010-10-27 17:18:37 -07001282 void onDragEnter() {
Patrick Dubroyfe6bd872010-10-13 17:32:10 -07001283 if (!mDragging) {
Patrick Dubroyfe6bd872010-10-13 17:32:10 -07001284 // Fade in the drag indicators
1285 if (mCrosshairsAnimator != null) {
1286 mCrosshairsAnimator.animateIn();
1287 }
Joe Onorato4be866d2010-10-10 11:26:02 -07001288 }
1289 mDragging = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001290 }
Winson Chungaafa03c2010-06-11 17:34:16 -07001291
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001292 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001293 * Computes a bounding rectangle for a range of cells
Winson Chungaafa03c2010-06-11 17:34:16 -07001294 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001295 * @param cellX X coordinate of upper left corner expressed as a cell position
1296 * @param cellY Y coordinate of upper left corner expressed as a cell position
Winson Chungaafa03c2010-06-11 17:34:16 -07001297 * @param cellHSpan Width in cells
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001298 * @param cellVSpan Height in cells
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001299 * @param resultRect Rect into which to put the results
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001300 */
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001301 public void cellToRect(int cellX, int cellY, int cellHSpan, int cellVSpan, RectF resultRect) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001302 final int cellWidth = mCellWidth;
1303 final int cellHeight = mCellHeight;
1304 final int widthGap = mWidthGap;
1305 final int heightGap = mHeightGap;
Winson Chungaafa03c2010-06-11 17:34:16 -07001306
1307 final int hStartPadding = getLeftPadding();
1308 final int vStartPadding = getTopPadding();
1309
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001310 int width = cellHSpan * cellWidth + ((cellHSpan - 1) * widthGap);
1311 int height = cellVSpan * cellHeight + ((cellVSpan - 1) * heightGap);
1312
1313 int x = hStartPadding + cellX * (cellWidth + widthGap);
1314 int y = vStartPadding + cellY * (cellHeight + heightGap);
Winson Chungaafa03c2010-06-11 17:34:16 -07001315
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001316 resultRect.set(x, y, x + width, y + height);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001317 }
Winson Chungaafa03c2010-06-11 17:34:16 -07001318
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001319 /**
Winson Chungaafa03c2010-06-11 17:34:16 -07001320 * Computes the required horizontal and vertical cell spans to always
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001321 * fit the given rectangle.
Winson Chungaafa03c2010-06-11 17:34:16 -07001322 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001323 * @param width Width in pixels
1324 * @param height Height in pixels
Patrick Dubroy8f86ddc2010-07-16 13:55:32 -07001325 * @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 -08001326 */
Patrick Dubroy8f86ddc2010-07-16 13:55:32 -07001327 public int[] rectToCell(int width, int height, int[] result) {
Michael Jurka9987a5c2010-10-08 16:58:12 -07001328 return rectToCell(getResources(), width, height, result);
1329 }
1330
1331 public static int[] rectToCell(Resources resources, int width, int height, int[] result) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001332 // Always assume we're working with the smallest span to make sure we
1333 // reserve enough space in both orientations.
Joe Onorato79e56262009-09-21 15:23:04 -04001334 int actualWidth = resources.getDimensionPixelSize(R.dimen.workspace_cell_width);
1335 int actualHeight = resources.getDimensionPixelSize(R.dimen.workspace_cell_height);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001336 int smallerSize = Math.min(actualWidth, actualHeight);
Joe Onorato79e56262009-09-21 15:23:04 -04001337
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001338 // Always round up to next largest cell
1339 int spanX = (width + smallerSize) / smallerSize;
1340 int spanY = (height + smallerSize) / smallerSize;
Joe Onorato79e56262009-09-21 15:23:04 -04001341
Patrick Dubroy8f86ddc2010-07-16 13:55:32 -07001342 if (result == null) {
1343 return new int[] { spanX, spanY };
1344 }
1345 result[0] = spanX;
1346 result[1] = spanY;
1347 return result;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001348 }
1349
1350 /**
1351 * Find the first vacant cell, if there is one.
1352 *
1353 * @param vacant Holds the x and y coordinate of the vacant cell
1354 * @param spanX Horizontal cell span.
1355 * @param spanY Vertical cell span.
Winson Chungaafa03c2010-06-11 17:34:16 -07001356 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001357 * @return True if a vacant cell was found
1358 */
1359 public boolean getVacantCell(int[] vacant, int spanX, int spanY) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001360
Michael Jurka0280c3b2010-09-17 15:00:07 -07001361 return findVacantCell(vacant, spanX, spanY, mCountX, mCountY, mOccupied);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001362 }
1363
1364 static boolean findVacantCell(int[] vacant, int spanX, int spanY,
1365 int xCount, int yCount, boolean[][] occupied) {
1366
1367 for (int x = 0; x < xCount; x++) {
1368 for (int y = 0; y < yCount; y++) {
1369 boolean available = !occupied[x][y];
1370out: for (int i = x; i < x + spanX - 1 && x < xCount; i++) {
1371 for (int j = y; j < y + spanY - 1 && y < yCount; j++) {
1372 available = available && !occupied[i][j];
1373 if (!available) break out;
1374 }
1375 }
1376
1377 if (available) {
1378 vacant[0] = x;
1379 vacant[1] = y;
1380 return true;
1381 }
1382 }
1383 }
1384
1385 return false;
1386 }
1387
Michael Jurka0280c3b2010-09-17 15:00:07 -07001388 private void clearOccupiedCells() {
1389 for (int x = 0; x < mCountX; x++) {
1390 for (int y = 0; y < mCountY; y++) {
1391 mOccupied[x][y] = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001392 }
1393 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001394 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001395
Michael Jurka0280c3b2010-09-17 15:00:07 -07001396 public void onMove(View view, int newCellX, int newCellY) {
1397 LayoutParams lp = (LayoutParams) view.getLayoutParams();
1398 markCellsAsUnoccupiedForView(view);
1399 markCellsForView(newCellX, newCellY, lp.cellHSpan, lp.cellVSpan, true);
1400 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001401
Michael Jurka0280c3b2010-09-17 15:00:07 -07001402 private void markCellsAsOccupiedForView(View view) {
Michael Jurkac6ee42e2010-09-30 12:04:50 -07001403 if (view == null || view.getParent() != this) return;
Michael Jurka0280c3b2010-09-17 15:00:07 -07001404 LayoutParams lp = (LayoutParams) view.getLayoutParams();
1405 markCellsForView(lp.cellX, lp.cellY, lp.cellHSpan, lp.cellVSpan, true);
1406 }
1407
1408 private void markCellsAsUnoccupiedForView(View view) {
Michael Jurkac6ee42e2010-09-30 12:04:50 -07001409 if (view == null || view.getParent() != this) return;
Michael Jurka0280c3b2010-09-17 15:00:07 -07001410 LayoutParams lp = (LayoutParams) view.getLayoutParams();
1411 markCellsForView(lp.cellX, lp.cellY, lp.cellHSpan, lp.cellVSpan, false);
1412 }
1413
1414 private void markCellsForView(int cellX, int cellY, int spanX, int spanY, boolean value) {
1415 for (int x = cellX; x < cellX + spanX && x < mCountX; x++) {
1416 for (int y = cellY; y < cellY + spanY && y < mCountY; y++) {
1417 mOccupied[x][y] = value;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001418 }
1419 }
1420 }
1421
1422 @Override
1423 public ViewGroup.LayoutParams generateLayoutParams(AttributeSet attrs) {
1424 return new CellLayout.LayoutParams(getContext(), attrs);
1425 }
1426
1427 @Override
1428 protected boolean checkLayoutParams(ViewGroup.LayoutParams p) {
1429 return p instanceof CellLayout.LayoutParams;
1430 }
1431
1432 @Override
1433 protected ViewGroup.LayoutParams generateLayoutParams(ViewGroup.LayoutParams p) {
1434 return new CellLayout.LayoutParams(p);
1435 }
1436
Winson Chungaafa03c2010-06-11 17:34:16 -07001437 public static class CellLayoutAnimationController extends LayoutAnimationController {
1438 public CellLayoutAnimationController(Animation animation, float delay) {
1439 super(animation, delay);
1440 }
1441
1442 @Override
1443 protected long getDelayForView(View view) {
1444 return (int) (Math.random() * 150);
1445 }
1446 }
1447
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001448 public static class LayoutParams extends ViewGroup.MarginLayoutParams {
1449 /**
1450 * Horizontal location of the item in the grid.
1451 */
1452 @ViewDebug.ExportedProperty
1453 public int cellX;
1454
1455 /**
1456 * Vertical location of the item in the grid.
1457 */
1458 @ViewDebug.ExportedProperty
1459 public int cellY;
1460
1461 /**
1462 * Number of cells spanned horizontally by the item.
1463 */
1464 @ViewDebug.ExportedProperty
1465 public int cellHSpan;
1466
1467 /**
1468 * Number of cells spanned vertically by the item.
1469 */
1470 @ViewDebug.ExportedProperty
1471 public int cellVSpan;
Winson Chungaafa03c2010-06-11 17:34:16 -07001472
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001473 /**
1474 * Is this item currently being dragged
1475 */
1476 public boolean isDragging;
1477
1478 // X coordinate of the view in the layout.
1479 @ViewDebug.ExportedProperty
1480 int x;
1481 // Y coordinate of the view in the layout.
1482 @ViewDebug.ExportedProperty
1483 int y;
1484
Patrick Dubroyce34a972010-10-19 10:34:32 -07001485 /**
1486 * The old X coordinate of this item, relative to its current parent.
1487 * Used to animate the item into its new position.
1488 */
1489 int oldX;
1490
1491 /**
1492 * The old Y coordinate of this item, relative to its current parent.
1493 * Used to animate the item into its new position.
1494 */
1495 int oldY;
1496
Romain Guy84f296c2009-11-04 15:00:44 -08001497 boolean dropped;
Romain Guyfcb9e712009-10-02 16:06:52 -07001498
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001499 public LayoutParams(Context c, AttributeSet attrs) {
1500 super(c, attrs);
1501 cellHSpan = 1;
1502 cellVSpan = 1;
1503 }
1504
1505 public LayoutParams(ViewGroup.LayoutParams source) {
1506 super(source);
1507 cellHSpan = 1;
1508 cellVSpan = 1;
1509 }
Winson Chungaafa03c2010-06-11 17:34:16 -07001510
1511 public LayoutParams(LayoutParams source) {
1512 super(source);
1513 this.cellX = source.cellX;
1514 this.cellY = source.cellY;
1515 this.cellHSpan = source.cellHSpan;
1516 this.cellVSpan = source.cellVSpan;
1517 }
1518
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001519 public LayoutParams(int cellX, int cellY, int cellHSpan, int cellVSpan) {
Romain Guy8f19cdd2010-01-08 15:07:00 -08001520 super(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001521 this.cellX = cellX;
1522 this.cellY = cellY;
1523 this.cellHSpan = cellHSpan;
1524 this.cellVSpan = cellVSpan;
1525 }
1526
1527 public void setup(int cellWidth, int cellHeight, int widthGap, int heightGap,
1528 int hStartPadding, int vStartPadding) {
Winson Chungaafa03c2010-06-11 17:34:16 -07001529
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001530 final int myCellHSpan = cellHSpan;
1531 final int myCellVSpan = cellVSpan;
1532 final int myCellX = cellX;
1533 final int myCellY = cellY;
Winson Chungaafa03c2010-06-11 17:34:16 -07001534
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001535 width = myCellHSpan * cellWidth + ((myCellHSpan - 1) * widthGap) -
1536 leftMargin - rightMargin;
1537 height = myCellVSpan * cellHeight + ((myCellVSpan - 1) * heightGap) -
1538 topMargin - bottomMargin;
1539
1540 x = hStartPadding + myCellX * (cellWidth + widthGap) + leftMargin;
1541 y = vStartPadding + myCellY * (cellHeight + heightGap) + topMargin;
1542 }
Winson Chungaafa03c2010-06-11 17:34:16 -07001543
1544 public String toString() {
1545 return "(" + this.cellX + ", " + this.cellY + ")";
1546 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001547 }
1548
Michael Jurka0280c3b2010-09-17 15:00:07 -07001549 // This class stores info for two purposes:
1550 // 1. When dragging items (mDragInfo in Workspace), we store the View, its cellX & cellY,
1551 // its spanX, spanY, and the screen it is on
1552 // 2. When long clicking on an empty cell in a CellLayout, we save information about the
1553 // cellX and cellY coordinates and which page was clicked. We then set this as a tag on
1554 // the CellLayout that was long clicked
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001555 static final class CellInfo implements ContextMenu.ContextMenuInfo {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001556 View cell;
Michael Jurkaa63c4522010-08-19 13:52:27 -07001557 int cellX = -1;
1558 int cellY = -1;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001559 int spanX;
1560 int spanY;
1561 int screen;
1562 boolean valid;
1563
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001564 @Override
1565 public String toString() {
Winson Chungaafa03c2010-06-11 17:34:16 -07001566 return "Cell[view=" + (cell == null ? "null" : cell.getClass())
1567 + ", x=" + cellX + ", y=" + cellY + "]";
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001568 }
1569 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001570}