blob: 63da108d06675b93ebaadad4107313fcf25780e9 [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
Patrick Dubroy6569f2c2010-07-12 14:25:18 -070019import com.android.launcher.R;
Winson Chungaafa03c2010-06-11 17:34:16 -070020
Joe Onorato4be866d2010-10-10 11:26:02 -070021import android.animation.Animator;
22import android.animation.AnimatorListenerAdapter;
Michael Jurka18014792010-10-14 09:01:34 -070023import android.animation.AnimatorSet;
24import android.animation.ObjectAnimator;
Chet Haase00397b12010-10-07 11:13:10 -070025import android.animation.TimeInterpolator;
Patrick Dubroyde7658b2010-09-27 11:15:43 -070026import android.animation.ValueAnimator;
27import android.animation.ValueAnimator.AnimatorUpdateListener;
Winson Chungaafa03c2010-06-11 17:34:16 -070028import android.app.WallpaperManager;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080029import android.content.Context;
Joe Onorato79e56262009-09-21 15:23:04 -040030import android.content.res.Resources;
Winson Chungaafa03c2010-06-11 17:34:16 -070031import android.content.res.TypedArray;
Joe Onorato4be866d2010-10-10 11:26:02 -070032import android.graphics.Bitmap;
Winson Chungaafa03c2010-06-11 17:34:16 -070033import android.graphics.Canvas;
Joe Onorato4be866d2010-10-10 11:26:02 -070034import android.graphics.Paint;
Patrick Dubroyde7658b2010-09-27 11:15:43 -070035import android.graphics.Point;
36import android.graphics.PointF;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080037import android.graphics.Rect;
38import android.graphics.RectF;
Michael Jurka18014792010-10-14 09:01:34 -070039import android.graphics.Region;
Patrick Dubroy6569f2c2010-07-12 14:25:18 -070040import android.graphics.drawable.Drawable;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080041import android.util.AttributeSet;
Joe Onorato4be866d2010-10-10 11:26:02 -070042import android.util.Log;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080043import android.view.ContextMenu;
44import android.view.MotionEvent;
45import android.view.View;
46import android.view.ViewDebug;
47import android.view.ViewGroup;
Winson Chungaafa03c2010-06-11 17:34:16 -070048import android.view.animation.Animation;
Winson Chung150fbab2010-09-29 17:14:26 -070049import android.view.animation.DecelerateInterpolator;
Winson Chungaafa03c2010-06-11 17:34:16 -070050import android.view.animation.LayoutAnimationController;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080051
Patrick Dubroy8e58e912010-10-14 13:21:48 -070052import java.util.Arrays;
53
Adam Cohenf34bab52010-09-30 14:11:56 -070054public class CellLayout extends ViewGroup implements Dimmable {
Winson Chungaafa03c2010-06-11 17:34:16 -070055 static final String TAG = "CellLayout";
56
The Android Open Source Project31dd5032009-03-03 19:32:27 -080057 private int mCellWidth;
58 private int mCellHeight;
Winson Chungaafa03c2010-06-11 17:34:16 -070059
Winson Chungaafa03c2010-06-11 17:34:16 -070060 private int mLeftPadding;
61 private int mRightPadding;
62 private int mTopPadding;
63 private int mBottomPadding;
64
Adam Cohend22015c2010-07-26 22:02:18 -070065 private int mCountX;
66 private int mCountY;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080067
68 private int mWidthGap;
69 private int mHeightGap;
70
71 private final Rect mRect = new Rect();
Patrick Dubroy8f86ddc2010-07-16 13:55:32 -070072 private final RectF mRectF = new RectF();
The Android Open Source Project31dd5032009-03-03 19:32:27 -080073 private final CellInfo mCellInfo = new CellInfo();
Winson Chungaafa03c2010-06-11 17:34:16 -070074
Patrick Dubroyde7658b2010-09-27 11:15:43 -070075 // These are temporary variables to prevent having to allocate a new object just to
76 // return an (x, y) value from helper functions. Do NOT use them to maintain other state.
Patrick Dubroy6569f2c2010-07-12 14:25:18 -070077 private final int[] mTmpCellXY = new int[2];
Patrick Dubroyde7658b2010-09-27 11:15:43 -070078 private final int[] mTmpPoint = new int[2];
79 private final PointF mTmpPointF = new PointF();
Patrick Dubroy6569f2c2010-07-12 14:25:18 -070080
The Android Open Source Project31dd5032009-03-03 19:32:27 -080081 boolean[][] mOccupied;
82
Michael Jurkadee05892010-07-27 10:01:56 -070083 private OnTouchListener mInterceptTouchListener;
84
Michael Jurka5f1c5092010-09-03 14:15:02 -070085 private float mBackgroundAlpha;
Adam Cohenf34bab52010-09-30 14:11:56 -070086
Michael Jurka5f1c5092010-09-03 14:15:02 -070087 private Drawable mBackground;
Adam Cohenf34bab52010-09-30 14:11:56 -070088 private Drawable mBackgroundMini;
89 private Drawable mBackgroundMiniHover;
Patrick Dubroy1262e362010-10-06 15:49:50 -070090 private Drawable mBackgroundHover;
Michael Jurka3e7c7632010-10-02 16:01:03 -070091 private Drawable mBackgroundMiniAcceptsDrops;
Michael Jurka18014792010-10-14 09:01:34 -070092 private Rect mBackgroundRect;
93 private Rect mHoverRect;
94 private float mHoverScale;
95 private float mHoverAlpha;
Michael Jurka3e7c7632010-10-02 16:01:03 -070096 private boolean mAcceptsDrops;
Patrick Dubroy1262e362010-10-06 15:49:50 -070097
98 // If we're actively dragging something over this screen, mHover is true
Michael Jurkaa63c4522010-08-19 13:52:27 -070099 private boolean mHover = false;
Michael Jurkadee05892010-07-27 10:01:56 -0700100
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700101 private final Point mDragCenter = new Point();
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700102
Winson Chung150fbab2010-09-29 17:14:26 -0700103 // These arrays are used to implement the drag visualization on x-large screens.
Joe Onorato4be866d2010-10-10 11:26:02 -0700104 // They are used as circular arrays, indexed by mDragOutlineCurrent.
105 private Point[] mDragOutlines = new Point[8];
Chet Haase472b2812010-10-14 07:02:04 -0700106 private float[] mDragOutlineAlphas = new float[mDragOutlines.length];
Joe Onorato4be866d2010-10-10 11:26:02 -0700107 private InterruptibleInOutAnimator[] mDragOutlineAnims =
108 new InterruptibleInOutAnimator[mDragOutlines.length];
Winson Chung150fbab2010-09-29 17:14:26 -0700109
110 // Used as an index into the above 3 arrays; indicates which is the most current value.
Joe Onorato4be866d2010-10-10 11:26:02 -0700111 private int mDragOutlineCurrent = 0;
Patrick Dubroy8e58e912010-10-14 13:21:48 -0700112 private final Paint mDragOutlinePaint = new Paint();
Winson Chung150fbab2010-09-29 17:14:26 -0700113
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700114 private Drawable mCrosshairsDrawable = null;
Patrick Dubroy49250ad2010-10-08 15:33:52 -0700115 private InterruptibleInOutAnimator mCrosshairsAnimator = null;
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700116 private float mCrosshairsVisibility = 0.0f;
117
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700118 // When a drag operation is in progress, holds the nearest cell to the touch point
119 private final int[] mDragCell = new int[2];
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800120
Winson Chungaafa03c2010-06-11 17:34:16 -0700121 private final WallpaperManager mWallpaperManager;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800122
Joe Onorato4be866d2010-10-10 11:26:02 -0700123 private boolean mDragging = false;
124
Patrick Dubroya9442152010-10-25 11:41:41 -0700125 private ValueAnimator mDropAnim;
Patrick Dubroyce34a972010-10-19 10:34:32 -0700126 private TimeInterpolator mEaseOutInterpolator;
127
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800128 public CellLayout(Context context) {
129 this(context, null);
130 }
131
132 public CellLayout(Context context, AttributeSet attrs) {
133 this(context, attrs, 0);
134 }
135
136 public CellLayout(Context context, AttributeSet attrs, int defStyle) {
137 super(context, attrs, defStyle);
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700138
139 // A ViewGroup usually does not draw, but CellLayout needs to draw a rectangle to show
140 // the user where a dragged item will land when dropped.
141 setWillNotDraw(false);
Michael Jurkaa63c4522010-08-19 13:52:27 -0700142
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800143 TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.CellLayout, defStyle, 0);
144
145 mCellWidth = a.getDimensionPixelSize(R.styleable.CellLayout_cellWidth, 10);
146 mCellHeight = a.getDimensionPixelSize(R.styleable.CellLayout_cellHeight, 10);
Winson Chungece7f5b2010-10-22 14:54:12 -0700147 mWidthGap = a.getDimensionPixelSize(R.styleable.CellLayout_widthGap, -1);
148 mHeightGap = a.getDimensionPixelSize(R.styleable.CellLayout_heightGap, -1);
Winson Chungaafa03c2010-06-11 17:34:16 -0700149
Adam Cohend22015c2010-07-26 22:02:18 -0700150 mLeftPadding =
151 a.getDimensionPixelSize(R.styleable.CellLayout_xAxisStartPadding, 10);
152 mRightPadding =
153 a.getDimensionPixelSize(R.styleable.CellLayout_xAxisEndPadding, 10);
154 mTopPadding =
155 a.getDimensionPixelSize(R.styleable.CellLayout_yAxisStartPadding, 10);
156 mBottomPadding =
157 a.getDimensionPixelSize(R.styleable.CellLayout_yAxisEndPadding, 10);
Winson Chungaafa03c2010-06-11 17:34:16 -0700158
Adam Cohend22015c2010-07-26 22:02:18 -0700159 mCountX = LauncherModel.getCellCountX();
160 mCountY = LauncherModel.getCellCountY();
Michael Jurka0280c3b2010-09-17 15:00:07 -0700161 mOccupied = new boolean[mCountX][mCountY];
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800162
163 a.recycle();
164
165 setAlwaysDrawnWithCacheEnabled(false);
166
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700167 mWallpaperManager = WallpaperManager.getInstance(context);
168
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700169 final Resources res = getResources();
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700170
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700171 if (LauncherApplication.isScreenXLarge()) {
Winson Chung150fbab2010-09-29 17:14:26 -0700172 mBackgroundMini = res.getDrawable(R.drawable.mini_home_screen_bg);
Adam Cohenf34bab52010-09-30 14:11:56 -0700173 mBackgroundMini.setFilterBitmap(true);
Winson Chung150fbab2010-09-29 17:14:26 -0700174 mBackground = res.getDrawable(R.drawable.home_screen_bg);
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700175 mBackground.setFilterBitmap(true);
Winson Chung150fbab2010-09-29 17:14:26 -0700176 mBackgroundMiniHover = res.getDrawable(R.drawable.mini_home_screen_bg_hover);
Adam Cohenf34bab52010-09-30 14:11:56 -0700177 mBackgroundMiniHover.setFilterBitmap(true);
Patrick Dubroy1262e362010-10-06 15:49:50 -0700178 mBackgroundHover = res.getDrawable(R.drawable.home_screen_bg_hover);
179 mBackgroundHover.setFilterBitmap(true);
Michael Jurka3e7c7632010-10-02 16:01:03 -0700180 mBackgroundMiniAcceptsDrops = res.getDrawable(
181 R.drawable.mini_home_screen_bg_accepts_drops);
182 mBackgroundMiniAcceptsDrops.setFilterBitmap(true);
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700183 }
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700184
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700185 // Initialize the data structures used for the drag visualization.
Winson Chung150fbab2010-09-29 17:14:26 -0700186
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700187 mCrosshairsDrawable = res.getDrawable(R.drawable.gardening_crosshairs);
Patrick Dubroyce34a972010-10-19 10:34:32 -0700188 mEaseOutInterpolator = new DecelerateInterpolator(2.5f); // Quint ease out
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700189
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700190 // Set up the animation for fading the crosshairs in and out
191 int animDuration = res.getInteger(R.integer.config_crosshairsFadeInTime);
Patrick Dubroy49250ad2010-10-08 15:33:52 -0700192 mCrosshairsAnimator = new InterruptibleInOutAnimator(animDuration, 0.0f, 1.0f);
Chet Haase472b2812010-10-14 07:02:04 -0700193 mCrosshairsAnimator.getAnimator().addUpdateListener(new AnimatorUpdateListener() {
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700194 public void onAnimationUpdate(ValueAnimator animation) {
195 mCrosshairsVisibility = ((Float) animation.getAnimatedValue()).floatValue();
Patrick Dubroy8e58e912010-10-14 13:21:48 -0700196 invalidate();
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700197 }
198 });
Patrick Dubroyce34a972010-10-19 10:34:32 -0700199 mCrosshairsAnimator.getAnimator().setInterpolator(mEaseOutInterpolator);
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700200
Joe Onorato4be866d2010-10-10 11:26:02 -0700201 for (int i = 0; i < mDragOutlines.length; i++) {
202 mDragOutlines[i] = new Point(-1, -1);
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700203 }
204
205 // When dragging things around the home screens, we show a green outline of
206 // where the item will land. The outlines gradually fade out, leaving a trail
207 // behind the drag path.
208 // Set up all the animations that are used to implement this fading.
209 final int duration = res.getInteger(R.integer.config_dragOutlineFadeTime);
Chet Haase472b2812010-10-14 07:02:04 -0700210 final float fromAlphaValue = 0;
211 final float toAlphaValue = (float)res.getInteger(R.integer.config_dragOutlineMaxAlpha);
Joe Onorato4be866d2010-10-10 11:26:02 -0700212
Patrick Dubroy8e58e912010-10-14 13:21:48 -0700213 Arrays.fill(mDragOutlineAlphas, fromAlphaValue);
Joe Onorato4be866d2010-10-10 11:26:02 -0700214
215 for (int i = 0; i < mDragOutlineAnims.length; i++) {
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700216 final InterruptibleInOutAnimator anim =
217 new InterruptibleInOutAnimator(duration, fromAlphaValue, toAlphaValue);
Patrick Dubroyce34a972010-10-19 10:34:32 -0700218 anim.getAnimator().setInterpolator(mEaseOutInterpolator);
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700219 final int thisIndex = i;
Chet Haase472b2812010-10-14 07:02:04 -0700220 anim.getAnimator().addUpdateListener(new AnimatorUpdateListener() {
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700221 public void onAnimationUpdate(ValueAnimator animation) {
Joe Onorato4be866d2010-10-10 11:26:02 -0700222 final Bitmap outline = (Bitmap)anim.getTag();
223
224 // If an animation is started and then stopped very quickly, we can still
225 // get spurious updates we've cleared the tag. Guard against this.
226 if (outline == null) {
Patrick Dubroyfe6bd872010-10-13 17:32:10 -0700227 if (false) {
228 Object val = animation.getAnimatedValue();
229 Log.d(TAG, "anim " + thisIndex + " update: " + val +
230 ", isStopped " + anim.isStopped());
231 }
Joe Onorato4be866d2010-10-10 11:26:02 -0700232 // Try to prevent it from continuing to run
233 animation.cancel();
234 } else {
Chet Haase472b2812010-10-14 07:02:04 -0700235 mDragOutlineAlphas[thisIndex] = (Float) animation.getAnimatedValue();
Joe Onorato4be866d2010-10-10 11:26:02 -0700236 final int left = mDragOutlines[thisIndex].x;
237 final int top = mDragOutlines[thisIndex].y;
238 CellLayout.this.invalidate(left, top,
239 left + outline.getWidth(), top + outline.getHeight());
240 }
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700241 }
242 });
Joe Onorato4be866d2010-10-10 11:26:02 -0700243 // The animation holds a reference to the drag outline bitmap as long is it's
244 // running. This way the bitmap can be GCed when the animations are complete.
Chet Haase472b2812010-10-14 07:02:04 -0700245 anim.getAnimator().addListener(new AnimatorListenerAdapter() {
Joe Onorato4be866d2010-10-10 11:26:02 -0700246 public void onAnimationEnd(Animator animation) {
Chet Haase472b2812010-10-14 07:02:04 -0700247 if ((Float) ((ValueAnimator) animation).getAnimatedValue() == 0f) {
Joe Onorato4be866d2010-10-10 11:26:02 -0700248 anim.setTag(null);
249 }
250 }
251 });
252 mDragOutlineAnims[i] = anim;
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700253 }
Patrick Dubroyce34a972010-10-19 10:34:32 -0700254
Patrick Dubroya9442152010-10-25 11:41:41 -0700255 mDropAnim = ValueAnimator.ofFloat(1.0f, 0.0f);
Patrick Dubroyce34a972010-10-19 10:34:32 -0700256 mDropAnim.setInterpolator(mEaseOutInterpolator);
257
Michael Jurka18014792010-10-14 09:01:34 -0700258 mBackgroundRect = new Rect();
259 mHoverRect = new Rect();
260 setHoverScale(1.0f);
261 setHoverAlpha(1.0f);
262 }
263
264 private void updateHoverRect() {
265 float marginFraction = (mHoverScale - 1.0f) / 2.0f;
266 int marginX = (int) (marginFraction * (mBackgroundRect.right - mBackgroundRect.left));
267 int marginY = (int) (marginFraction * (mBackgroundRect.bottom - mBackgroundRect.top));
268 mHoverRect.set(mBackgroundRect.left - marginX, mBackgroundRect.top - marginY,
269 mBackgroundRect.right + marginX, mBackgroundRect.bottom + marginY);
270 invalidate();
271 }
272
273 public void setHoverScale(float scaleFactor) {
274 if (scaleFactor != mHoverScale) {
275 mHoverScale = scaleFactor;
276 updateHoverRect();
277 }
278 }
279
280 public float getHoverScale() {
281 return mHoverScale;
282 }
283
284 public float getHoverAlpha() {
285 return mHoverAlpha;
286 }
287
288 public void setHoverAlpha(float alpha) {
289 mHoverAlpha = alpha;
290 invalidate();
291 }
292
293 void animateDrop() {
294 if (LauncherApplication.isScreenXLarge()) {
295 Resources res = getResources();
296 float onDropScale = res.getInteger(R.integer.config_screenOnDropScalePercent) / 100.0f;
297 ObjectAnimator scaleUp = ObjectAnimator.ofFloat(this, "hoverScale", onDropScale);
298 scaleUp.setDuration(res.getInteger(R.integer.config_screenOnDropScaleUpDuration));
299 ObjectAnimator scaleDown = ObjectAnimator.ofFloat(this, "hoverScale", 1.0f);
300 scaleDown.setDuration(res.getInteger(R.integer.config_screenOnDropScaleDownDuration));
301 ObjectAnimator alphaFadeOut = ObjectAnimator.ofFloat(this, "hoverAlpha", 0.0f);
302
303 alphaFadeOut.setStartDelay(res.getInteger(R.integer.config_screenOnDropAlphaFadeDelay));
304 alphaFadeOut.setDuration(res.getInteger(R.integer.config_screenOnDropAlphaFadeDelay));
305
306 AnimatorSet bouncer = new AnimatorSet();
307 bouncer.play(scaleUp).before(scaleDown);
308 bouncer.play(scaleUp).with(alphaFadeOut);
309 bouncer.addListener(new AnimatorListenerAdapter() {
310 public void onAnimationStart(Animator animation) {
311 setHover(true);
312 }
313 public void onAnimationEnd(Animator animation) {
314 setHover(false);
315 setHoverScale(1.0f);
316 setHoverAlpha(1.0f);
317 }
318 });
319 bouncer.start();
320 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800321 }
322
Michael Jurkaa63c4522010-08-19 13:52:27 -0700323 public void setHover(boolean value) {
324 if (mHover != value) {
Michael Jurka3e7c7632010-10-02 16:01:03 -0700325 mHover = value;
Michael Jurkaa63c4522010-08-19 13:52:27 -0700326 invalidate();
327 }
Michael Jurkaa63c4522010-08-19 13:52:27 -0700328 }
329
Patrick Dubroy1262e362010-10-06 15:49:50 -0700330 public void drawChildren(Canvas canvas) {
331 super.dispatchDraw(canvas);
332 }
333
Jeff Sharkey83f111d2009-04-20 21:03:13 -0700334 @Override
Patrick Dubroy1262e362010-10-06 15:49:50 -0700335 protected void onDraw(Canvas canvas) {
Michael Jurka3e7c7632010-10-02 16:01:03 -0700336 // When we're large, we are either drawn in a "hover" state (ie when dragging an item to
337 // a neighboring page) or with just a normal background (if backgroundAlpha > 0.0f)
338 // When we're small, we are either drawn normally or in the "accepts drops" state (during
339 // a drag). However, we also drag the mini hover background *over* one of those two
340 // backgrounds
Michael Jurka5f1c5092010-09-03 14:15:02 -0700341 if (mBackgroundAlpha > 0.0f) {
Adam Cohenf34bab52010-09-30 14:11:56 -0700342 Drawable bg;
Patrick Dubroy1262e362010-10-06 15:49:50 -0700343 if (getScaleX() < 0.5f) {
Michael Jurka3e7c7632010-10-02 16:01:03 -0700344 bg = mAcceptsDrops ? mBackgroundMiniAcceptsDrops : mBackgroundMini;
Adam Cohenf34bab52010-09-30 14:11:56 -0700345 } else {
Patrick Dubroy1262e362010-10-06 15:49:50 -0700346 bg = mHover ? mBackgroundHover : mBackground;
Adam Cohenf34bab52010-09-30 14:11:56 -0700347 }
Adam Cohen9c4949e2010-10-05 12:27:22 -0700348 if (bg != null) {
349 bg.setAlpha((int) (mBackgroundAlpha * 255));
Michael Jurka18014792010-10-14 09:01:34 -0700350 bg.setBounds(mBackgroundRect);
Adam Cohen9c4949e2010-10-05 12:27:22 -0700351 bg.draw(canvas);
352 }
Michael Jurka3e7c7632010-10-02 16:01:03 -0700353 if (mHover && getScaleX() < 0.5f) {
Michael Jurka18014792010-10-14 09:01:34 -0700354 boolean modifiedClipRect = false;
355 if (mHoverScale > 1.0f) {
356 // If the hover background's scale is greater than 1, we'll be drawing outside
357 // the bounds of this CellLayout. Get around that by temporarily increasing the
358 // size of the clip rect
359 float marginFraction = (mHoverScale - 1.0f) / 2.0f;
360 Rect clipRect = canvas.getClipBounds();
361 int marginX = (int) (marginFraction * (clipRect.right - clipRect.left));
362 int marginY = (int) (marginFraction * (clipRect.bottom - clipRect.top));
363 canvas.save(Canvas.CLIP_SAVE_FLAG);
364 canvas.clipRect(-marginX, -marginY,
365 getWidth() + marginX, getHeight() + marginY, Region.Op.REPLACE);
366 modifiedClipRect = true;
367 }
368
369 mBackgroundMiniHover.setAlpha((int) (mBackgroundAlpha * mHoverAlpha * 255));
370 mBackgroundMiniHover.setBounds(mHoverRect);
Michael Jurka3e7c7632010-10-02 16:01:03 -0700371 mBackgroundMiniHover.draw(canvas);
Michael Jurka18014792010-10-14 09:01:34 -0700372 if (modifiedClipRect) {
373 canvas.restore();
374 }
Michael Jurka3e7c7632010-10-02 16:01:03 -0700375 }
Michael Jurkaa63c4522010-08-19 13:52:27 -0700376 }
Romain Guya6abce82009-11-10 02:54:41 -0800377
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700378 if (mCrosshairsVisibility > 0.0f) {
379 final int countX = mCountX;
380 final int countY = mCountY;
381
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700382 final float MAX_ALPHA = 0.4f;
383 final int MAX_VISIBLE_DISTANCE = 600;
384 final float DISTANCE_MULTIPLIER = 0.002f;
385
386 final Drawable d = mCrosshairsDrawable;
387 final int width = d.getIntrinsicWidth();
388 final int height = d.getIntrinsicHeight();
389
390 int x = getLeftPadding() - (mWidthGap / 2) - (width / 2);
391 for (int col = 0; col <= countX; col++) {
392 int y = getTopPadding() - (mHeightGap / 2) - (height / 2);
393 for (int row = 0; row <= countY; row++) {
394 mTmpPointF.set(x - mDragCenter.x, y - mDragCenter.y);
395 float dist = mTmpPointF.length();
396 // Crosshairs further from the drag point are more faint
397 float alpha = Math.min(MAX_ALPHA,
398 DISTANCE_MULTIPLIER * (MAX_VISIBLE_DISTANCE - dist));
399 if (alpha > 0.0f) {
400 d.setBounds(x, y, x + width, y + height);
401 d.setAlpha((int) (alpha * 255 * mCrosshairsVisibility));
402 d.draw(canvas);
403 }
404 y += mCellHeight + mHeightGap;
405 }
406 x += mCellWidth + mWidthGap;
407 }
Joe Onorato4be866d2010-10-10 11:26:02 -0700408 }
Winson Chung150fbab2010-09-29 17:14:26 -0700409
Patrick Dubroy8e58e912010-10-14 13:21:48 -0700410 final Paint paint = mDragOutlinePaint;
Joe Onorato4be866d2010-10-10 11:26:02 -0700411 for (int i = 0; i < mDragOutlines.length; i++) {
Chet Haase472b2812010-10-14 07:02:04 -0700412 final float alpha = mDragOutlineAlphas[i];
Joe Onorato4be866d2010-10-10 11:26:02 -0700413 if (alpha > 0) {
414 final Point p = mDragOutlines[i];
415 final Bitmap b = (Bitmap) mDragOutlineAnims[i].getTag();
Chet Haase472b2812010-10-14 07:02:04 -0700416 paint.setAlpha((int)(alpha + .5f));
Joe Onorato4be866d2010-10-10 11:26:02 -0700417 canvas.drawBitmap(b, p.x, p.y, paint);
Winson Chung150fbab2010-09-29 17:14:26 -0700418 }
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700419 }
420 }
421
Adam Cohenf34bab52010-09-30 14:11:56 -0700422 public void setDimmableProgress(float progress) {
423 for (int i = 0; i < getChildCount(); i++) {
424 Dimmable d = (Dimmable) getChildAt(i);
425 d.setDimmableProgress(progress);
426 }
427 }
428
429 public float getDimmableProgress() {
430 if (getChildCount() > 0) {
431 return ((Dimmable) getChildAt(0)).getDimmableProgress();
432 }
433 return 0.0f;
434 }
435
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700436 @Override
Jeff Sharkey83f111d2009-04-20 21:03:13 -0700437 public void cancelLongPress() {
438 super.cancelLongPress();
439
440 // Cancel long press for all children
441 final int count = getChildCount();
442 for (int i = 0; i < count; i++) {
443 final View child = getChildAt(i);
444 child.cancelLongPress();
445 }
446 }
447
Michael Jurkadee05892010-07-27 10:01:56 -0700448 public void setOnInterceptTouchListener(View.OnTouchListener listener) {
449 mInterceptTouchListener = listener;
450 }
451
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800452 int getCountX() {
Adam Cohend22015c2010-07-26 22:02:18 -0700453 return mCountX;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800454 }
455
456 int getCountY() {
Adam Cohend22015c2010-07-26 22:02:18 -0700457 return mCountY;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800458 }
459
Winson Chungaafa03c2010-06-11 17:34:16 -0700460 public boolean addViewToCellLayout(View child, int index, int childId, LayoutParams params) {
Michael Jurkaf3ca3ab2010-10-20 17:08:24 -0700461 return addViewToCellLayout(child, index, childId, params, true);
462 }
463
464 public boolean addViewToCellLayout(
465 View child, int index, int childId, LayoutParams params, boolean markCells) {
Winson Chungaafa03c2010-06-11 17:34:16 -0700466 final LayoutParams lp = params;
467
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800468 // Generate an id for each view, this assumes we have at most 256x256 cells
469 // per workspace screen
Adam Cohend22015c2010-07-26 22:02:18 -0700470 if (lp.cellX >= 0 && lp.cellX <= mCountX - 1 && lp.cellY >= 0 && lp.cellY <= mCountY - 1) {
Winson Chungaafa03c2010-06-11 17:34:16 -0700471 // If the horizontal or vertical span is set to -1, it is taken to
472 // mean that it spans the extent of the CellLayout
Adam Cohend22015c2010-07-26 22:02:18 -0700473 if (lp.cellHSpan < 0) lp.cellHSpan = mCountX;
474 if (lp.cellVSpan < 0) lp.cellVSpan = mCountY;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800475
Winson Chungaafa03c2010-06-11 17:34:16 -0700476 child.setId(childId);
477
Michael Jurkadee05892010-07-27 10:01:56 -0700478 // We might be in the middle or end of shrinking/fading to a dimmed view
479 // Make sure this view's alpha is set the same as all the rest of the views
Michael Jurka5f1c5092010-09-03 14:15:02 -0700480 child.setAlpha(getAlpha());
Winson Chungaafa03c2010-06-11 17:34:16 -0700481 addView(child, index, lp);
Michael Jurkadee05892010-07-27 10:01:56 -0700482
Michael Jurkaf3ca3ab2010-10-20 17:08:24 -0700483 if (markCells) markCellsAsOccupiedForView(child);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700484
Winson Chungaafa03c2010-06-11 17:34:16 -0700485 return true;
486 }
487 return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800488 }
Michael Jurka3e7c7632010-10-02 16:01:03 -0700489 public void setAcceptsDrops(boolean acceptsDrops) {
490 if (mAcceptsDrops != acceptsDrops) {
491 mAcceptsDrops = acceptsDrops;
492 invalidate();
493 }
494 }
495
496 public boolean getAcceptsDrops() {
497 return mAcceptsDrops;
498 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800499
500 @Override
Michael Jurka0280c3b2010-09-17 15:00:07 -0700501 public void removeAllViews() {
502 clearOccupiedCells();
503 }
504
505 @Override
506 public void removeAllViewsInLayout() {
507 clearOccupiedCells();
508 }
509
Michael Jurkaf3ca3ab2010-10-20 17:08:24 -0700510 public void removeViewWithoutMarkingCells(View view) {
511 super.removeView(view);
512 }
513
Michael Jurka0280c3b2010-09-17 15:00:07 -0700514 @Override
515 public void removeView(View view) {
516 markCellsAsUnoccupiedForView(view);
517 super.removeView(view);
518 }
519
520 @Override
521 public void removeViewAt(int index) {
522 markCellsAsUnoccupiedForView(getChildAt(index));
523 super.removeViewAt(index);
524 }
525
526 @Override
527 public void removeViewInLayout(View view) {
528 markCellsAsUnoccupiedForView(view);
529 super.removeViewInLayout(view);
530 }
531
532 @Override
533 public void removeViews(int start, int count) {
534 for (int i = start; i < start + count; i++) {
535 markCellsAsUnoccupiedForView(getChildAt(i));
536 }
537 super.removeViews(start, count);
538 }
539
540 @Override
541 public void removeViewsInLayout(int start, int count) {
542 for (int i = start; i < start + count; i++) {
543 markCellsAsUnoccupiedForView(getChildAt(i));
544 }
545 super.removeViewsInLayout(start, count);
546 }
547
548 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800549 public void requestChildFocus(View child, View focused) {
550 super.requestChildFocus(child, focused);
551 if (child != null) {
552 Rect r = new Rect();
553 child.getDrawingRect(r);
554 requestRectangleOnScreen(r);
555 }
556 }
557
558 @Override
559 protected void onAttachedToWindow() {
560 super.onAttachedToWindow();
561 mCellInfo.screen = ((ViewGroup) getParent()).indexOfChild(this);
562 }
563
Michael Jurkaaf442092010-06-10 17:01:57 -0700564 public void setTagToCellInfoForPoint(int touchX, int touchY) {
565 final CellInfo cellInfo = mCellInfo;
566 final Rect frame = mRect;
567 final int x = touchX + mScrollX;
568 final int y = touchY + mScrollY;
569 final int count = getChildCount();
570
571 boolean found = false;
572 for (int i = count - 1; i >= 0; i--) {
573 final View child = getChildAt(i);
574
575 if ((child.getVisibility()) == VISIBLE || child.getAnimation() != null) {
576 child.getHitRect(frame);
577 if (frame.contains(x, y)) {
578 final LayoutParams lp = (LayoutParams) child.getLayoutParams();
579 cellInfo.cell = child;
580 cellInfo.cellX = lp.cellX;
581 cellInfo.cellY = lp.cellY;
582 cellInfo.spanX = lp.cellHSpan;
583 cellInfo.spanY = lp.cellVSpan;
584 cellInfo.valid = true;
585 found = true;
Michael Jurkaaf442092010-06-10 17:01:57 -0700586 break;
587 }
588 }
589 }
Winson Chungaafa03c2010-06-11 17:34:16 -0700590
Michael Jurkaaf442092010-06-10 17:01:57 -0700591 if (!found) {
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700592 final int cellXY[] = mTmpCellXY;
Michael Jurkaaf442092010-06-10 17:01:57 -0700593 pointToCellExact(x, y, cellXY);
594
Michael Jurkaaf442092010-06-10 17:01:57 -0700595 cellInfo.cell = null;
596 cellInfo.cellX = cellXY[0];
597 cellInfo.cellY = cellXY[1];
598 cellInfo.spanX = 1;
599 cellInfo.spanY = 1;
Michael Jurka0280c3b2010-09-17 15:00:07 -0700600 cellInfo.valid = cellXY[0] >= 0 && cellXY[1] >= 0 && cellXY[0] < mCountX &&
601 cellXY[1] < mCountY && !mOccupied[cellXY[0]][cellXY[1]];
Michael Jurkaaf442092010-06-10 17:01:57 -0700602 }
603 setTag(cellInfo);
604 }
605
Winson Chungaafa03c2010-06-11 17:34:16 -0700606
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800607 @Override
608 public boolean onInterceptTouchEvent(MotionEvent ev) {
Michael Jurkadee05892010-07-27 10:01:56 -0700609 if (mInterceptTouchListener != null && mInterceptTouchListener.onTouch(this, ev)) {
610 return true;
611 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800612 final int action = ev.getAction();
613 final CellInfo cellInfo = mCellInfo;
614
615 if (action == MotionEvent.ACTION_DOWN) {
Michael Jurkaaf442092010-06-10 17:01:57 -0700616 setTagToCellInfoForPoint((int) ev.getX(), (int) ev.getY());
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800617 } else if (action == MotionEvent.ACTION_UP) {
618 cellInfo.cell = null;
619 cellInfo.cellX = -1;
620 cellInfo.cellY = -1;
621 cellInfo.spanX = 0;
622 cellInfo.spanY = 0;
623 cellInfo.valid = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800624 setTag(cellInfo);
625 }
626
627 return false;
628 }
629
630 @Override
631 public CellInfo getTag() {
Michael Jurka0280c3b2010-09-17 15:00:07 -0700632 return (CellInfo) super.getTag();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800633 }
634
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700635 /**
636 * Check if the row 'y' is empty from columns 'left' to 'right', inclusive.
637 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800638 private static boolean isRowEmpty(int y, int left, int right, boolean[][] occupied) {
639 for (int x = left; x <= right; x++) {
640 if (occupied[x][y]) {
641 return false;
642 }
643 }
644 return true;
645 }
646
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800647 /**
Winson Chungaafa03c2010-06-11 17:34:16 -0700648 * Given a point, return the cell that strictly encloses that point
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800649 * @param x X coordinate of the point
650 * @param y Y coordinate of the point
651 * @param result Array of 2 ints to hold the x and y coordinate of the cell
652 */
653 void pointToCellExact(int x, int y, int[] result) {
Winson Chungaafa03c2010-06-11 17:34:16 -0700654 final int hStartPadding = getLeftPadding();
655 final int vStartPadding = getTopPadding();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800656
657 result[0] = (x - hStartPadding) / (mCellWidth + mWidthGap);
658 result[1] = (y - vStartPadding) / (mCellHeight + mHeightGap);
659
Adam Cohend22015c2010-07-26 22:02:18 -0700660 final int xAxis = mCountX;
661 final int yAxis = mCountY;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800662
663 if (result[0] < 0) result[0] = 0;
664 if (result[0] >= xAxis) result[0] = xAxis - 1;
665 if (result[1] < 0) result[1] = 0;
666 if (result[1] >= yAxis) result[1] = yAxis - 1;
667 }
Winson Chungaafa03c2010-06-11 17:34:16 -0700668
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800669 /**
670 * Given a point, return the cell that most closely encloses that point
671 * @param x X coordinate of the point
672 * @param y Y coordinate of the point
673 * @param result Array of 2 ints to hold the x and y coordinate of the cell
674 */
675 void pointToCellRounded(int x, int y, int[] result) {
676 pointToCellExact(x + (mCellWidth / 2), y + (mCellHeight / 2), result);
677 }
678
679 /**
680 * Given a cell coordinate, return the point that represents the upper left corner of that cell
Winson Chungaafa03c2010-06-11 17:34:16 -0700681 *
682 * @param cellX X coordinate of the cell
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800683 * @param cellY Y coordinate of the cell
Winson Chungaafa03c2010-06-11 17:34:16 -0700684 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800685 * @param result Array of 2 ints to hold the x and y coordinate of the point
686 */
687 void cellToPoint(int cellX, int cellY, int[] result) {
Winson Chungaafa03c2010-06-11 17:34:16 -0700688 final int hStartPadding = getLeftPadding();
689 final int vStartPadding = getTopPadding();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800690
691 result[0] = hStartPadding + cellX * (mCellWidth + mWidthGap);
692 result[1] = vStartPadding + cellY * (mCellHeight + mHeightGap);
693 }
694
Romain Guy84f296c2009-11-04 15:00:44 -0800695 int getCellWidth() {
696 return mCellWidth;
697 }
698
699 int getCellHeight() {
700 return mCellHeight;
701 }
702
Romain Guy1a304a12009-11-10 00:02:32 -0800703 int getLeftPadding() {
Winson Chungaafa03c2010-06-11 17:34:16 -0700704 return mLeftPadding;
Romain Guy1a304a12009-11-10 00:02:32 -0800705 }
706
707 int getTopPadding() {
Winson Chungaafa03c2010-06-11 17:34:16 -0700708 return mTopPadding;
Romain Guy1a304a12009-11-10 00:02:32 -0800709 }
710
711 int getRightPadding() {
Winson Chungaafa03c2010-06-11 17:34:16 -0700712 return mRightPadding;
Romain Guy1a304a12009-11-10 00:02:32 -0800713 }
714
715 int getBottomPadding() {
Winson Chungaafa03c2010-06-11 17:34:16 -0700716 return mBottomPadding;
Romain Guy1a304a12009-11-10 00:02:32 -0800717 }
718
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800719 @Override
720 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
721 // TODO: currently ignoring padding
Winson Chungaafa03c2010-06-11 17:34:16 -0700722
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800723 int widthSpecMode = MeasureSpec.getMode(widthMeasureSpec);
Winson Chungaafa03c2010-06-11 17:34:16 -0700724 int widthSpecSize = MeasureSpec.getSize(widthMeasureSpec);
725
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800726 int heightSpecMode = MeasureSpec.getMode(heightMeasureSpec);
727 int heightSpecSize = MeasureSpec.getSize(heightMeasureSpec);
Winson Chungaafa03c2010-06-11 17:34:16 -0700728
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800729 if (widthSpecMode == MeasureSpec.UNSPECIFIED || heightSpecMode == MeasureSpec.UNSPECIFIED) {
730 throw new RuntimeException("CellLayout cannot have UNSPECIFIED dimensions");
731 }
732
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800733 final int cellWidth = mCellWidth;
734 final int cellHeight = mCellHeight;
735
Adam Cohend22015c2010-07-26 22:02:18 -0700736 int numWidthGaps = mCountX - 1;
737 int numHeightGaps = mCountY - 1;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800738
Winson Chungece7f5b2010-10-22 14:54:12 -0700739 if (mWidthGap < 0 || mHeightGap < 0) {
740 int vSpaceLeft = heightSpecSize - mTopPadding - mBottomPadding - (cellHeight * mCountY);
741 mHeightGap = vSpaceLeft / numHeightGaps;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800742
Winson Chungece7f5b2010-10-22 14:54:12 -0700743 int hSpaceLeft = widthSpecSize - mLeftPadding - mRightPadding - (cellWidth * mCountX);
744 mWidthGap = hSpaceLeft / numWidthGaps;
Winson Chungaafa03c2010-06-11 17:34:16 -0700745
Winson Chungece7f5b2010-10-22 14:54:12 -0700746 // center it around the min gaps
747 int minGap = Math.min(mWidthGap, mHeightGap);
748 mWidthGap = mHeightGap = minGap;
749 }
Michael Jurka5f1c5092010-09-03 14:15:02 -0700750
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800751 int count = getChildCount();
752
753 for (int i = 0; i < count; i++) {
754 View child = getChildAt(i);
755 LayoutParams lp = (LayoutParams) child.getLayoutParams();
Winson Chungaafa03c2010-06-11 17:34:16 -0700756 lp.setup(cellWidth, cellHeight, mWidthGap, mHeightGap,
757 mLeftPadding, mTopPadding);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800758
Michael Jurka0280c3b2010-09-17 15:00:07 -0700759 int childWidthMeasureSpec = MeasureSpec.makeMeasureSpec(lp.width, MeasureSpec.EXACTLY);
Winson Chungaafa03c2010-06-11 17:34:16 -0700760 int childheightMeasureSpec = MeasureSpec.makeMeasureSpec(lp.height,
761 MeasureSpec.EXACTLY);
762
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800763 child.measure(childWidthMeasureSpec, childheightMeasureSpec);
764 }
Michael Jurka5f1c5092010-09-03 14:15:02 -0700765 if (widthSpecMode == MeasureSpec.AT_MOST) {
766 int newWidth = mLeftPadding + mRightPadding + (mCountX * cellWidth) +
Winson Chungece7f5b2010-10-22 14:54:12 -0700767 ((mCountX - 1) * mWidthGap);
Michael Jurka5f1c5092010-09-03 14:15:02 -0700768 int newHeight = mTopPadding + mBottomPadding + (mCountY * cellHeight) +
Winson Chungece7f5b2010-10-22 14:54:12 -0700769 ((mCountY - 1) * mHeightGap);
Michael Jurka5f1c5092010-09-03 14:15:02 -0700770 setMeasuredDimension(newWidth, newHeight);
771 } else if (widthSpecMode == MeasureSpec.EXACTLY) {
772 setMeasuredDimension(widthSpecSize, heightSpecSize);
773 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800774 }
775
Patrick Dubroyce34a972010-10-19 10:34:32 -0700776 /**
777 * Animate a child of this CellLayout into its current layout position.
778 * The position to animate from is given by the oldX and oldY values in its LayoutParams.
779 */
780 private void animateChildIntoPosition(final View child) {
781 final Resources res = getResources();
Patrick Dubroya9442152010-10-25 11:41:41 -0700782 final ValueAnimator anim = mDropAnim;
Patrick Dubroyce34a972010-10-19 10:34:32 -0700783 final CellLayout.LayoutParams lp = (CellLayout.LayoutParams) child.getLayoutParams();
784 final float startX = lp.oldX - lp.x;
785 final float startY = lp.oldY - lp.y;
786
787 // Calculate the duration of the animation based on the object's distance
788 final float dist = (float) Math.sqrt(startX*startX + startY*startY);
789 final float maxDist = (float) res.getInteger(R.integer.config_dropAnimMaxDist);
790 final int duration = (int) (res.getInteger(R.integer.config_dropAnimMaxDuration)
791 * mEaseOutInterpolator.getInterpolation(dist / maxDist));
792
Patrick Dubroya9442152010-10-25 11:41:41 -0700793 anim.end(); // Make sure it's not already running
Patrick Dubroyce34a972010-10-19 10:34:32 -0700794 anim.setDuration(duration);
Patrick Dubroya9442152010-10-25 11:41:41 -0700795 anim.setFloatValues(1.0f, 0.0f);
Patrick Dubroyce34a972010-10-19 10:34:32 -0700796 anim.removeAllUpdateListeners();
797 anim.addUpdateListener(new AnimatorUpdateListener() {
798 public void onAnimationUpdate(ValueAnimator animation) {
Patrick Dubroya9442152010-10-25 11:41:41 -0700799 final float value = (Float) anim.getAnimatedValue();
800 child.setTranslationX(startX * value);
801 child.setTranslationY(startY * value);
Patrick Dubroyce34a972010-10-19 10:34:32 -0700802 }
803 });
804 anim.start();
805 }
806
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800807 @Override
Michael Jurka28750fb2010-09-24 17:43:49 -0700808 protected void onLayout(boolean changed, int l, int t, int r, int b) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800809 int count = getChildCount();
810
811 for (int i = 0; i < count; i++) {
Patrick Dubroyce34a972010-10-19 10:34:32 -0700812 final View child = getChildAt(i);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800813 if (child.getVisibility() != GONE) {
814
815 CellLayout.LayoutParams lp = (CellLayout.LayoutParams) child.getLayoutParams();
816
817 int childLeft = lp.x;
818 int childTop = lp.y;
819 child.layout(childLeft, childTop, childLeft + lp.width, childTop + lp.height);
Romain Guy84f296c2009-11-04 15:00:44 -0800820
821 if (lp.dropped) {
822 lp.dropped = false;
823
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700824 final int[] cellXY = mTmpCellXY;
Romain Guy06762ab2010-01-25 16:51:08 -0800825 getLocationOnScreen(cellXY);
Romain Guy84f296c2009-11-04 15:00:44 -0800826 mWallpaperManager.sendWallpaperCommand(getWindowToken(), "android.home.drop",
Romain Guy06762ab2010-01-25 16:51:08 -0800827 cellXY[0] + childLeft + lp.width / 2,
828 cellXY[1] + childTop + lp.height / 2, 0, null);
Patrick Dubroyce34a972010-10-19 10:34:32 -0700829
830 animateChildIntoPosition(child);
Romain Guy84f296c2009-11-04 15:00:44 -0800831 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800832 }
833 }
834 }
835
836 @Override
Michael Jurkadee05892010-07-27 10:01:56 -0700837 protected void onSizeChanged(int w, int h, int oldw, int oldh) {
838 super.onSizeChanged(w, h, oldw, oldh);
Michael Jurka18014792010-10-14 09:01:34 -0700839 mBackgroundRect.set(0, 0, w, h);
840 updateHoverRect();
Michael Jurkadee05892010-07-27 10:01:56 -0700841 }
842
843 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800844 protected void setChildrenDrawingCacheEnabled(boolean enabled) {
845 final int count = getChildCount();
846 for (int i = 0; i < count; i++) {
847 final View view = getChildAt(i);
848 view.setDrawingCacheEnabled(enabled);
849 // Update the drawing caches
Romain Guy3cf4c032010-10-26 14:29:35 -0700850 if (!view.isHardwareAccelerated()) {
851 view.buildDrawingCache(true);
852 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800853 }
854 }
855
856 @Override
857 protected void setChildrenDrawnWithCacheEnabled(boolean enabled) {
858 super.setChildrenDrawnWithCacheEnabled(enabled);
859 }
860
Michael Jurka5f1c5092010-09-03 14:15:02 -0700861 public float getBackgroundAlpha() {
862 return mBackgroundAlpha;
Michael Jurkadee05892010-07-27 10:01:56 -0700863 }
864
Michael Jurka5f1c5092010-09-03 14:15:02 -0700865 public void setBackgroundAlpha(float alpha) {
866 mBackgroundAlpha = alpha;
Michael Jurka0142d492010-08-25 17:46:15 -0700867 invalidate();
Michael Jurkadee05892010-07-27 10:01:56 -0700868 }
869
Michael Jurka5f1c5092010-09-03 14:15:02 -0700870 // Need to return true to let the view system know we know how to handle alpha-- this is
871 // because when our children have an alpha of 0.0f, they are still rendering their "dimmed"
872 // versions
873 @Override
874 protected boolean onSetAlpha(int alpha) {
875 return true;
876 }
877
878 public void setAlpha(float alpha) {
879 setChildrenAlpha(alpha);
880 super.setAlpha(alpha);
881 }
882
Michael Jurkadee05892010-07-27 10:01:56 -0700883 private void setChildrenAlpha(float alpha) {
Michael Jurka0142d492010-08-25 17:46:15 -0700884 final int childCount = getChildCount();
885 for (int i = 0; i < childCount; i++) {
Michael Jurkadee05892010-07-27 10:01:56 -0700886 getChildAt(i).setAlpha(alpha);
887 }
888 }
889
Michael Jurka0280c3b2010-09-17 15:00:07 -0700890 private boolean isVacantIgnoring(
891 int originX, int originY, int spanX, int spanY, View ignoreView) {
892 if (ignoreView != null) {
893 markCellsAsUnoccupiedForView(ignoreView);
894 }
Michael Jurka28750fb2010-09-24 17:43:49 -0700895 boolean isVacant = true;
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700896 for (int i = 0; i < spanY; i++) {
897 if (!isRowEmpty(originY + i, originX, originX + spanX - 1, mOccupied)) {
Michael Jurka28750fb2010-09-24 17:43:49 -0700898 isVacant = false;
899 break;
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700900 }
901 }
Michael Jurka0280c3b2010-09-17 15:00:07 -0700902 if (ignoreView != null) {
903 markCellsAsOccupiedForView(ignoreView);
904 }
Michael Jurka28750fb2010-09-24 17:43:49 -0700905 return isVacant;
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700906 }
907
Michael Jurka0280c3b2010-09-17 15:00:07 -0700908 private boolean isVacant(int originX, int originY, int spanX, int spanY) {
909 return isVacantIgnoring(originX, originY, spanX, spanY, null);
910 }
911
Patrick Dubroy440c3602010-07-13 17:50:32 -0700912 public View getChildAt(int x, int y) {
913 final int count = getChildCount();
914 for (int i = 0; i < count; i++) {
915 View child = getChildAt(i);
916 LayoutParams lp = (LayoutParams) child.getLayoutParams();
917
918 if ((lp.cellX <= x) && (x < lp.cellX + lp.cellHSpan) &&
919 (lp.cellY <= y) && (y < lp.cellY + lp.cellHSpan)) {
920 return child;
921 }
922 }
923 return null;
924 }
925
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700926 /**
Patrick Dubroy8f86ddc2010-07-16 13:55:32 -0700927 * Estimate the size that a child with the given dimensions will take in the layout.
928 */
929 void estimateChildSize(int minWidth, int minHeight, int[] result) {
930 // Assuming it's placed at 0, 0, find where the bottom right cell will land
931 rectToCell(minWidth, minHeight, result);
932
933 // Then figure out the rect it will occupy
934 cellToRect(0, 0, result[0], result[1], mRectF);
935 result[0] = (int)mRectF.width();
936 result[1] = (int)mRectF.height();
937 }
938
939 /**
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700940 * Estimate where the top left cell of the dragged item will land if it is dropped.
941 *
942 * @param originX The X value of the top left corner of the item
943 * @param originY The Y value of the top left corner of the item
944 * @param spanX The number of horizontal cells that the item spans
945 * @param spanY The number of vertical cells that the item spans
946 * @param result The estimated drop cell X and Y.
947 */
948 void estimateDropCell(int originX, int originY, int spanX, int spanY, int[] result) {
Adam Cohend22015c2010-07-26 22:02:18 -0700949 final int countX = mCountX;
950 final int countY = mCountY;
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700951
Michael Jurkaa63c4522010-08-19 13:52:27 -0700952 // pointToCellRounded takes the top left of a cell but will pad that with
953 // cellWidth/2 and cellHeight/2 when finding the matching cell
954 pointToCellRounded(originX, originY, result);
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700955
956 // If the item isn't fully on this screen, snap to the edges
957 int rightOverhang = result[0] + spanX - countX;
958 if (rightOverhang > 0) {
959 result[0] -= rightOverhang; // Snap to right
960 }
961 result[0] = Math.max(0, result[0]); // Snap to left
962 int bottomOverhang = result[1] + spanY - countY;
963 if (bottomOverhang > 0) {
964 result[1] -= bottomOverhang; // Snap to bottom
965 }
966 result[1] = Math.max(0, result[1]); // Snap to top
967 }
968
Joe Onorato4be866d2010-10-10 11:26:02 -0700969 void visualizeDropLocation(
970 View v, Bitmap dragOutline, int originX, int originY, int spanX, int spanY) {
971
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -0700972 final int oldDragCellX = mDragCell[0];
973 final int oldDragCellY = mDragCell[1];
Joe Onorato4be866d2010-10-10 11:26:02 -0700974 final int[] nearest = findNearestVacantArea(originX, originY, spanX, spanY, v, mDragCell);
975 mDragCenter.set(originX + (v.getWidth() / 2), originY + (v.getHeight() / 2));
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700976
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -0700977 if (nearest != null && (nearest[0] != oldDragCellX || nearest[1] != oldDragCellY)) {
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700978 // Find the top left corner of the rect the object will occupy
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700979 final int[] topLeft = mTmpPoint;
980 cellToPoint(nearest[0], nearest[1], topLeft);
981
Joe Onorato4be866d2010-10-10 11:26:02 -0700982 int left = topLeft[0];
983 int top = topLeft[1];
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700984
Joe Onorato4be866d2010-10-10 11:26:02 -0700985 if (v.getParent() instanceof CellLayout) {
986 LayoutParams lp = (LayoutParams) v.getLayoutParams();
987 left += lp.leftMargin;
988 top += lp.topMargin;
989 }
Winson Chung150fbab2010-09-29 17:14:26 -0700990
Joe Onorato4be866d2010-10-10 11:26:02 -0700991 // Offsets due to the size difference between the View and the dragOutline
992 left += (v.getWidth() - dragOutline.getWidth()) / 2;
993 top += (v.getHeight() - dragOutline.getHeight()) / 2;
Winson Chung150fbab2010-09-29 17:14:26 -0700994
Joe Onorato4be866d2010-10-10 11:26:02 -0700995 final int oldIndex = mDragOutlineCurrent;
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -0700996 mDragOutlineAnims[oldIndex].animateOut();
997 mDragOutlineCurrent = (oldIndex + 1) % mDragOutlines.length;
Winson Chung150fbab2010-09-29 17:14:26 -0700998
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -0700999 mDragOutlines[mDragOutlineCurrent].set(left, top);
1000 mDragOutlineAnims[mDragOutlineCurrent].setTag(dragOutline);
1001 mDragOutlineAnims[mDragOutlineCurrent].animateIn();
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001002 }
Patrick Dubroy49250ad2010-10-08 15:33:52 -07001003
1004 // If we are drawing crosshairs, the entire CellLayout needs to be invalidated
1005 if (mCrosshairsDrawable != null) {
1006 invalidate();
1007 }
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001008 }
1009
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001010 /**
Jeff Sharkey70864282009-04-07 21:08:40 -07001011 * Find a vacant area that will fit the given bounds nearest the requested
1012 * cell location. Uses Euclidean distance to score multiple vacant areas.
Winson Chungaafa03c2010-06-11 17:34:16 -07001013 *
Romain Guy51afc022009-05-04 18:03:43 -07001014 * @param pixelX The X location at which you want to search for a vacant area.
1015 * @param pixelY The Y location at which you want to search for a vacant area.
Jeff Sharkey70864282009-04-07 21:08:40 -07001016 * @param spanX Horizontal span of the object.
1017 * @param spanY Vertical span of the object.
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001018 * @param result Array in which to place the result, or null (in which case a new array will
1019 * be allocated)
Jeff Sharkey70864282009-04-07 21:08:40 -07001020 * @return The X, Y cell of a vacant area that can contain this object,
1021 * nearest the requested location.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001022 */
Michael Jurka6a1435d2010-09-27 17:35:12 -07001023 int[] findNearestVacantArea(
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001024 int pixelX, int pixelY, int spanX, int spanY, int[] result) {
1025 return findNearestVacantArea(pixelX, pixelY, spanX, spanY, null, result);
Michael Jurka6a1435d2010-09-27 17:35:12 -07001026 }
Winson Chungaafa03c2010-06-11 17:34:16 -07001027
Michael Jurka6a1435d2010-09-27 17:35:12 -07001028 /**
1029 * Find a vacant area that will fit the given bounds nearest the requested
1030 * cell location. Uses Euclidean distance to score multiple vacant areas.
1031 *
1032 * @param pixelX The X location at which you want to search for a vacant area.
1033 * @param pixelY The Y location at which you want to search for a vacant area.
1034 * @param spanX Horizontal span of the object.
1035 * @param spanY Vertical span of the object.
Michael Jurka6a1435d2010-09-27 17:35:12 -07001036 * @param ignoreView Considers space occupied by this view as unoccupied
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001037 * @param result Previously returned value to possibly recycle.
Michael Jurka6a1435d2010-09-27 17:35:12 -07001038 * @return The X, Y cell of a vacant area that can contain this object,
1039 * nearest the requested location.
1040 */
1041 int[] findNearestVacantArea(
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001042 int pixelX, int pixelY, int spanX, int spanY, View ignoreView, int[] result) {
Michael Jurkac6ee42e2010-09-30 12:04:50 -07001043 // mark space take by ignoreView as available (method checks if ignoreView is null)
1044 markCellsAsUnoccupiedForView(ignoreView);
1045
Jeff Sharkey70864282009-04-07 21:08:40 -07001046 // Keep track of best-scoring drop area
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001047 final int[] bestXY = result != null ? result : new int[2];
Jeff Sharkey70864282009-04-07 21:08:40 -07001048 double bestDistance = Double.MAX_VALUE;
Winson Chungaafa03c2010-06-11 17:34:16 -07001049
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001050 final int countX = mCountX;
1051 final int countY = mCountY;
1052 final boolean[][] occupied = mOccupied;
1053
1054 for (int x = 0; x < countX - (spanX - 1); x++) {
Michael Jurkac28de512010-08-13 11:27:44 -07001055 inner:
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001056 for (int y = 0; y < countY - (spanY - 1); y++) {
Michael Jurkac28de512010-08-13 11:27:44 -07001057 for (int i = 0; i < spanX; i++) {
1058 for (int j = 0; j < spanY; j++) {
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001059 if (occupied[x + i][y + j]) {
Michael Jurkac28de512010-08-13 11:27:44 -07001060 // small optimization: we can skip to below the row we just found
1061 // an occupied cell
1062 y += j;
1063 continue inner;
1064 }
1065 }
1066 }
1067 final int[] cellXY = mTmpCellXY;
1068 cellToPoint(x, y, cellXY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001069
Michael Jurkac28de512010-08-13 11:27:44 -07001070 double distance = Math.sqrt(Math.pow(cellXY[0] - pixelX, 2)
1071 + Math.pow(cellXY[1] - pixelY, 2));
1072 if (distance <= bestDistance) {
1073 bestDistance = distance;
1074 bestXY[0] = x;
1075 bestXY[1] = y;
1076 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001077 }
1078 }
Michael Jurkac6ee42e2010-09-30 12:04:50 -07001079 // re-mark space taken by ignoreView as occupied
1080 markCellsAsOccupiedForView(ignoreView);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001081
Winson Chungaafa03c2010-06-11 17:34:16 -07001082 // Return null if no suitable location found
Jeff Sharkey70864282009-04-07 21:08:40 -07001083 if (bestDistance < Double.MAX_VALUE) {
1084 return bestXY;
1085 } else {
1086 return null;
1087 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001088 }
Winson Chungaafa03c2010-06-11 17:34:16 -07001089
Michael Jurka0280c3b2010-09-17 15:00:07 -07001090 boolean existsEmptyCell() {
1091 return findCellForSpan(null, 1, 1);
1092 }
1093
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001094 /**
Michael Jurka0280c3b2010-09-17 15:00:07 -07001095 * Finds the upper-left coordinate of the first rectangle in the grid that can
1096 * hold a cell of the specified dimensions. If intersectX and intersectY are not -1,
1097 * then this method will only return coordinates for rectangles that contain the cell
1098 * (intersectX, intersectY)
1099 *
1100 * @param cellXY The array that will contain the position of a vacant cell if such a cell
1101 * can be found.
1102 * @param spanX The horizontal span of the cell we want to find.
1103 * @param spanY The vertical span of the cell we want to find.
1104 *
1105 * @return True if a vacant cell of the specified dimension was found, false otherwise.
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001106 */
Michael Jurka0280c3b2010-09-17 15:00:07 -07001107 boolean findCellForSpan(int[] cellXY, int spanX, int spanY) {
1108 return findCellForSpanThatIntersectsIgnoring(cellXY, spanX, spanY, -1, -1, null);
1109 }
1110
1111 /**
1112 * Like above, but ignores any cells occupied by the item "ignoreView"
1113 *
1114 * @param cellXY The array that will contain the position of a vacant cell if such a cell
1115 * can be found.
1116 * @param spanX The horizontal span of the cell we want to find.
1117 * @param spanY The vertical span of the cell we want to find.
1118 * @param ignoreView The home screen item we should treat as not occupying any space
1119 * @return
1120 */
1121 boolean findCellForSpanIgnoring(int[] cellXY, int spanX, int spanY, View ignoreView) {
1122 return findCellForSpanThatIntersectsIgnoring(cellXY, spanX, spanY, -1, -1, ignoreView);
1123 }
1124
1125 /**
1126 * Like above, but if intersectX and intersectY are not -1, then this method will try to
1127 * return coordinates for rectangles that contain the cell [intersectX, intersectY]
1128 *
1129 * @param spanX The horizontal span of the cell we want to find.
1130 * @param spanY The vertical span of the cell we want to find.
1131 * @param ignoreView The home screen item we should treat as not occupying any space
1132 * @param intersectX The X coordinate of the cell that we should try to overlap
1133 * @param intersectX The Y coordinate of the cell that we should try to overlap
1134 *
1135 * @return True if a vacant cell of the specified dimension was found, false otherwise.
1136 */
1137 boolean findCellForSpanThatIntersects(int[] cellXY, int spanX, int spanY,
1138 int intersectX, int intersectY) {
1139 return findCellForSpanThatIntersectsIgnoring(
1140 cellXY, spanX, spanY, intersectX, intersectY, null);
1141 }
1142
1143 /**
1144 * The superset of the above two methods
1145 */
1146 boolean findCellForSpanThatIntersectsIgnoring(int[] cellXY, int spanX, int spanY,
1147 int intersectX, int intersectY, View ignoreView) {
Michael Jurkac6ee42e2010-09-30 12:04:50 -07001148 // mark space take by ignoreView as available (method checks if ignoreView is null)
1149 markCellsAsUnoccupiedForView(ignoreView);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001150
Michael Jurka28750fb2010-09-24 17:43:49 -07001151 boolean foundCell = false;
Michael Jurka0280c3b2010-09-17 15:00:07 -07001152 while (true) {
1153 int startX = 0;
1154 if (intersectX >= 0) {
1155 startX = Math.max(startX, intersectX - (spanX - 1));
1156 }
1157 int endX = mCountX - (spanX - 1);
1158 if (intersectX >= 0) {
1159 endX = Math.min(endX, intersectX + (spanX - 1) + (spanX == 1 ? 1 : 0));
1160 }
1161 int startY = 0;
1162 if (intersectY >= 0) {
1163 startY = Math.max(startY, intersectY - (spanY - 1));
1164 }
1165 int endY = mCountY - (spanY - 1);
1166 if (intersectY >= 0) {
1167 endY = Math.min(endY, intersectY + (spanY - 1) + (spanY == 1 ? 1 : 0));
1168 }
1169
1170 for (int x = startX; x < endX; x++) {
1171 inner:
1172 for (int y = startY; y < endY; y++) {
1173 for (int i = 0; i < spanX; i++) {
1174 for (int j = 0; j < spanY; j++) {
1175 if (mOccupied[x + i][y + j]) {
1176 // small optimization: we can skip to below the row we just found
1177 // an occupied cell
1178 y += j;
1179 continue inner;
1180 }
1181 }
1182 }
1183 if (cellXY != null) {
1184 cellXY[0] = x;
1185 cellXY[1] = y;
1186 }
Michael Jurka28750fb2010-09-24 17:43:49 -07001187 foundCell = true;
1188 break;
Michael Jurka0280c3b2010-09-17 15:00:07 -07001189 }
1190 }
1191 if (intersectX == -1 && intersectY == -1) {
1192 break;
1193 } else {
1194 // if we failed to find anything, try again but without any requirements of
1195 // intersecting
1196 intersectX = -1;
1197 intersectY = -1;
1198 continue;
1199 }
1200 }
1201
Michael Jurkac6ee42e2010-09-30 12:04:50 -07001202 // re-mark space taken by ignoreView as occupied
1203 markCellsAsOccupiedForView(ignoreView);
Michael Jurka28750fb2010-09-24 17:43:49 -07001204 return foundCell;
Michael Jurka0280c3b2010-09-17 15:00:07 -07001205 }
1206
1207 /**
1208 * Called when drag has left this CellLayout or has been completed (successfully or not)
1209 */
1210 void onDragExit() {
Joe Onorato4be866d2010-10-10 11:26:02 -07001211 // This can actually be called when we aren't in a drag, e.g. when adding a new
1212 // item to this layout via the customize drawer.
1213 // Guard against that case.
1214 if (mDragging) {
1215 mDragging = false;
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001216
Joe Onorato4be866d2010-10-10 11:26:02 -07001217 // Fade out the drag indicators
1218 if (mCrosshairsAnimator != null) {
1219 mCrosshairsAnimator.animateOut();
1220 }
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001221 }
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -07001222
1223 // Invalidate the drag data
1224 mDragCell[0] = -1;
1225 mDragCell[1] = -1;
1226 mDragOutlineAnims[mDragOutlineCurrent].animateOut();
1227 mDragOutlineCurrent = (mDragOutlineCurrent + 1) % mDragOutlineAnims.length;
1228
1229 setHover(false);
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001230 }
1231
1232 /**
Winson Chungaafa03c2010-06-11 17:34:16 -07001233 * Mark a child as having been dropped.
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001234 * At the beginning of the drag operation, the child may have been on another
Patrick Dubroyce34a972010-10-19 10:34:32 -07001235 * screen, but it is re-parented before this method is called.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001236 *
1237 * @param child The child that is being dropped
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001238 */
Winson Chungaafa03c2010-06-11 17:34:16 -07001239 void onDropChild(View child) {
Romain Guyd94533d2009-08-17 10:01:15 -07001240 if (child != null) {
1241 LayoutParams lp = (LayoutParams) child.getLayoutParams();
Romain Guyd94533d2009-08-17 10:01:15 -07001242 lp.isDragging = false;
Romain Guy84f296c2009-11-04 15:00:44 -08001243 lp.dropped = true;
Patrick Dubroyce34a972010-10-19 10:34:32 -07001244 child.setVisibility(View.VISIBLE);
Romain Guyd94533d2009-08-17 10:01:15 -07001245 child.requestLayout();
Romain Guyd94533d2009-08-17 10:01:15 -07001246 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001247 }
1248
1249 void onDropAborted(View child) {
1250 if (child != null) {
Patrick Dubroyce34a972010-10-19 10:34:32 -07001251 LayoutParams lp = (LayoutParams) child.getLayoutParams();
1252 lp.isDragging = false;
1253 child.setVisibility(View.VISIBLE);
1254 animateChildIntoPosition(child);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001255 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001256 }
1257
1258 /**
1259 * Start dragging the specified child
Winson Chungaafa03c2010-06-11 17:34:16 -07001260 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001261 * @param child The child that is being dragged
1262 */
1263 void onDragChild(View child) {
1264 LayoutParams lp = (LayoutParams) child.getLayoutParams();
1265 lp.isDragging = true;
Patrick Dubroyce34a972010-10-19 10:34:32 -07001266 child.setVisibility(View.GONE);
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001267 }
1268
1269 /**
1270 * A drag event has begun over this layout.
1271 * It may have begun over this layout (in which case onDragChild is called first),
1272 * or it may have begun on another layout.
1273 */
1274 void onDragEnter(View dragView) {
Patrick Dubroyfe6bd872010-10-13 17:32:10 -07001275 if (!mDragging) {
Patrick Dubroyfe6bd872010-10-13 17:32:10 -07001276 // Fade in the drag indicators
1277 if (mCrosshairsAnimator != null) {
1278 mCrosshairsAnimator.animateIn();
1279 }
Joe Onorato4be866d2010-10-10 11:26:02 -07001280 }
1281 mDragging = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001282 }
Winson Chungaafa03c2010-06-11 17:34:16 -07001283
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001284 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001285 * Computes a bounding rectangle for a range of cells
Winson Chungaafa03c2010-06-11 17:34:16 -07001286 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001287 * @param cellX X coordinate of upper left corner expressed as a cell position
1288 * @param cellY Y coordinate of upper left corner expressed as a cell position
Winson Chungaafa03c2010-06-11 17:34:16 -07001289 * @param cellHSpan Width in cells
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001290 * @param cellVSpan Height in cells
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001291 * @param resultRect Rect into which to put the results
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001292 */
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001293 public void cellToRect(int cellX, int cellY, int cellHSpan, int cellVSpan, RectF resultRect) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001294 final int cellWidth = mCellWidth;
1295 final int cellHeight = mCellHeight;
1296 final int widthGap = mWidthGap;
1297 final int heightGap = mHeightGap;
Winson Chungaafa03c2010-06-11 17:34:16 -07001298
1299 final int hStartPadding = getLeftPadding();
1300 final int vStartPadding = getTopPadding();
1301
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001302 int width = cellHSpan * cellWidth + ((cellHSpan - 1) * widthGap);
1303 int height = cellVSpan * cellHeight + ((cellVSpan - 1) * heightGap);
1304
1305 int x = hStartPadding + cellX * (cellWidth + widthGap);
1306 int y = vStartPadding + cellY * (cellHeight + heightGap);
Winson Chungaafa03c2010-06-11 17:34:16 -07001307
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001308 resultRect.set(x, y, x + width, y + height);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001309 }
Winson Chungaafa03c2010-06-11 17:34:16 -07001310
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001311 /**
Winson Chungaafa03c2010-06-11 17:34:16 -07001312 * Computes the required horizontal and vertical cell spans to always
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001313 * fit the given rectangle.
Winson Chungaafa03c2010-06-11 17:34:16 -07001314 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001315 * @param width Width in pixels
1316 * @param height Height in pixels
Patrick Dubroy8f86ddc2010-07-16 13:55:32 -07001317 * @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 -08001318 */
Patrick Dubroy8f86ddc2010-07-16 13:55:32 -07001319 public int[] rectToCell(int width, int height, int[] result) {
Michael Jurka9987a5c2010-10-08 16:58:12 -07001320 return rectToCell(getResources(), width, height, result);
1321 }
1322
1323 public static int[] rectToCell(Resources resources, int width, int height, int[] result) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001324 // Always assume we're working with the smallest span to make sure we
1325 // reserve enough space in both orientations.
Joe Onorato79e56262009-09-21 15:23:04 -04001326 int actualWidth = resources.getDimensionPixelSize(R.dimen.workspace_cell_width);
1327 int actualHeight = resources.getDimensionPixelSize(R.dimen.workspace_cell_height);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001328 int smallerSize = Math.min(actualWidth, actualHeight);
Joe Onorato79e56262009-09-21 15:23:04 -04001329
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001330 // Always round up to next largest cell
1331 int spanX = (width + smallerSize) / smallerSize;
1332 int spanY = (height + smallerSize) / smallerSize;
Joe Onorato79e56262009-09-21 15:23:04 -04001333
Patrick Dubroy8f86ddc2010-07-16 13:55:32 -07001334 if (result == null) {
1335 return new int[] { spanX, spanY };
1336 }
1337 result[0] = spanX;
1338 result[1] = spanY;
1339 return result;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001340 }
1341
1342 /**
1343 * Find the first vacant cell, if there is one.
1344 *
1345 * @param vacant Holds the x and y coordinate of the vacant cell
1346 * @param spanX Horizontal cell span.
1347 * @param spanY Vertical cell span.
Winson Chungaafa03c2010-06-11 17:34:16 -07001348 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001349 * @return True if a vacant cell was found
1350 */
1351 public boolean getVacantCell(int[] vacant, int spanX, int spanY) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001352
Michael Jurka0280c3b2010-09-17 15:00:07 -07001353 return findVacantCell(vacant, spanX, spanY, mCountX, mCountY, mOccupied);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001354 }
1355
1356 static boolean findVacantCell(int[] vacant, int spanX, int spanY,
1357 int xCount, int yCount, boolean[][] occupied) {
1358
1359 for (int x = 0; x < xCount; x++) {
1360 for (int y = 0; y < yCount; y++) {
1361 boolean available = !occupied[x][y];
1362out: for (int i = x; i < x + spanX - 1 && x < xCount; i++) {
1363 for (int j = y; j < y + spanY - 1 && y < yCount; j++) {
1364 available = available && !occupied[i][j];
1365 if (!available) break out;
1366 }
1367 }
1368
1369 if (available) {
1370 vacant[0] = x;
1371 vacant[1] = y;
1372 return true;
1373 }
1374 }
1375 }
1376
1377 return false;
1378 }
1379
Michael Jurka0280c3b2010-09-17 15:00:07 -07001380 private void clearOccupiedCells() {
1381 for (int x = 0; x < mCountX; x++) {
1382 for (int y = 0; y < mCountY; y++) {
1383 mOccupied[x][y] = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001384 }
1385 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001386 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001387
Michael Jurka0280c3b2010-09-17 15:00:07 -07001388 public void onMove(View view, int newCellX, int newCellY) {
1389 LayoutParams lp = (LayoutParams) view.getLayoutParams();
1390 markCellsAsUnoccupiedForView(view);
1391 markCellsForView(newCellX, newCellY, lp.cellHSpan, lp.cellVSpan, true);
1392 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001393
Michael Jurka0280c3b2010-09-17 15:00:07 -07001394 private void markCellsAsOccupiedForView(View view) {
Michael Jurkac6ee42e2010-09-30 12:04:50 -07001395 if (view == null || view.getParent() != this) return;
Michael Jurka0280c3b2010-09-17 15:00:07 -07001396 LayoutParams lp = (LayoutParams) view.getLayoutParams();
1397 markCellsForView(lp.cellX, lp.cellY, lp.cellHSpan, lp.cellVSpan, true);
1398 }
1399
1400 private void markCellsAsUnoccupiedForView(View view) {
Michael Jurkac6ee42e2010-09-30 12:04:50 -07001401 if (view == null || view.getParent() != this) return;
Michael Jurka0280c3b2010-09-17 15:00:07 -07001402 LayoutParams lp = (LayoutParams) view.getLayoutParams();
1403 markCellsForView(lp.cellX, lp.cellY, lp.cellHSpan, lp.cellVSpan, false);
1404 }
1405
1406 private void markCellsForView(int cellX, int cellY, int spanX, int spanY, boolean value) {
1407 for (int x = cellX; x < cellX + spanX && x < mCountX; x++) {
1408 for (int y = cellY; y < cellY + spanY && y < mCountY; y++) {
1409 mOccupied[x][y] = value;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001410 }
1411 }
1412 }
1413
1414 @Override
1415 public ViewGroup.LayoutParams generateLayoutParams(AttributeSet attrs) {
1416 return new CellLayout.LayoutParams(getContext(), attrs);
1417 }
1418
1419 @Override
1420 protected boolean checkLayoutParams(ViewGroup.LayoutParams p) {
1421 return p instanceof CellLayout.LayoutParams;
1422 }
1423
1424 @Override
1425 protected ViewGroup.LayoutParams generateLayoutParams(ViewGroup.LayoutParams p) {
1426 return new CellLayout.LayoutParams(p);
1427 }
1428
Winson Chungaafa03c2010-06-11 17:34:16 -07001429 public static class CellLayoutAnimationController extends LayoutAnimationController {
1430 public CellLayoutAnimationController(Animation animation, float delay) {
1431 super(animation, delay);
1432 }
1433
1434 @Override
1435 protected long getDelayForView(View view) {
1436 return (int) (Math.random() * 150);
1437 }
1438 }
1439
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001440 public static class LayoutParams extends ViewGroup.MarginLayoutParams {
1441 /**
1442 * Horizontal location of the item in the grid.
1443 */
1444 @ViewDebug.ExportedProperty
1445 public int cellX;
1446
1447 /**
1448 * Vertical location of the item in the grid.
1449 */
1450 @ViewDebug.ExportedProperty
1451 public int cellY;
1452
1453 /**
1454 * Number of cells spanned horizontally by the item.
1455 */
1456 @ViewDebug.ExportedProperty
1457 public int cellHSpan;
1458
1459 /**
1460 * Number of cells spanned vertically by the item.
1461 */
1462 @ViewDebug.ExportedProperty
1463 public int cellVSpan;
Winson Chungaafa03c2010-06-11 17:34:16 -07001464
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001465 /**
1466 * Is this item currently being dragged
1467 */
1468 public boolean isDragging;
1469
1470 // X coordinate of the view in the layout.
1471 @ViewDebug.ExportedProperty
1472 int x;
1473 // Y coordinate of the view in the layout.
1474 @ViewDebug.ExportedProperty
1475 int y;
1476
Patrick Dubroyce34a972010-10-19 10:34:32 -07001477 /**
1478 * The old X coordinate of this item, relative to its current parent.
1479 * Used to animate the item into its new position.
1480 */
1481 int oldX;
1482
1483 /**
1484 * The old Y coordinate of this item, relative to its current parent.
1485 * Used to animate the item into its new position.
1486 */
1487 int oldY;
1488
Romain Guy84f296c2009-11-04 15:00:44 -08001489 boolean dropped;
Romain Guyfcb9e712009-10-02 16:06:52 -07001490
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001491 public LayoutParams(Context c, AttributeSet attrs) {
1492 super(c, attrs);
1493 cellHSpan = 1;
1494 cellVSpan = 1;
1495 }
1496
1497 public LayoutParams(ViewGroup.LayoutParams source) {
1498 super(source);
1499 cellHSpan = 1;
1500 cellVSpan = 1;
1501 }
Winson Chungaafa03c2010-06-11 17:34:16 -07001502
1503 public LayoutParams(LayoutParams source) {
1504 super(source);
1505 this.cellX = source.cellX;
1506 this.cellY = source.cellY;
1507 this.cellHSpan = source.cellHSpan;
1508 this.cellVSpan = source.cellVSpan;
1509 }
1510
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001511 public LayoutParams(int cellX, int cellY, int cellHSpan, int cellVSpan) {
Romain Guy8f19cdd2010-01-08 15:07:00 -08001512 super(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001513 this.cellX = cellX;
1514 this.cellY = cellY;
1515 this.cellHSpan = cellHSpan;
1516 this.cellVSpan = cellVSpan;
1517 }
1518
1519 public void setup(int cellWidth, int cellHeight, int widthGap, int heightGap,
1520 int hStartPadding, int vStartPadding) {
Winson Chungaafa03c2010-06-11 17:34:16 -07001521
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001522 final int myCellHSpan = cellHSpan;
1523 final int myCellVSpan = cellVSpan;
1524 final int myCellX = cellX;
1525 final int myCellY = cellY;
Winson Chungaafa03c2010-06-11 17:34:16 -07001526
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001527 width = myCellHSpan * cellWidth + ((myCellHSpan - 1) * widthGap) -
1528 leftMargin - rightMargin;
1529 height = myCellVSpan * cellHeight + ((myCellVSpan - 1) * heightGap) -
1530 topMargin - bottomMargin;
1531
1532 x = hStartPadding + myCellX * (cellWidth + widthGap) + leftMargin;
1533 y = vStartPadding + myCellY * (cellHeight + heightGap) + topMargin;
1534 }
Winson Chungaafa03c2010-06-11 17:34:16 -07001535
1536 public String toString() {
1537 return "(" + this.cellX + ", " + this.cellY + ")";
1538 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001539 }
1540
Michael Jurka0280c3b2010-09-17 15:00:07 -07001541 // This class stores info for two purposes:
1542 // 1. When dragging items (mDragInfo in Workspace), we store the View, its cellX & cellY,
1543 // its spanX, spanY, and the screen it is on
1544 // 2. When long clicking on an empty cell in a CellLayout, we save information about the
1545 // cellX and cellY coordinates and which page was clicked. We then set this as a tag on
1546 // the CellLayout that was long clicked
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001547 static final class CellInfo implements ContextMenu.ContextMenuInfo {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001548 View cell;
Michael Jurkaa63c4522010-08-19 13:52:27 -07001549 int cellX = -1;
1550 int cellY = -1;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001551 int spanX;
1552 int spanY;
1553 int screen;
1554 boolean valid;
1555
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001556 @Override
1557 public String toString() {
Winson Chungaafa03c2010-06-11 17:34:16 -07001558 return "Cell[view=" + (cell == null ? "null" : cell.getClass())
1559 + ", x=" + cellX + ", y=" + cellY + "]";
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001560 }
1561 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001562}