blob: d94bacfbf6b2cef732d3c38883b71a267d6bb5a3 [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 Dubroyce34a972010-10-19 10:34:32 -0700125 private ObjectAnimator mDropAnim;
126 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 Chungaafa03c2010-06-11 17:34:16 -0700147
Adam Cohend22015c2010-07-26 22:02:18 -0700148 mLeftPadding =
149 a.getDimensionPixelSize(R.styleable.CellLayout_xAxisStartPadding, 10);
150 mRightPadding =
151 a.getDimensionPixelSize(R.styleable.CellLayout_xAxisEndPadding, 10);
152 mTopPadding =
153 a.getDimensionPixelSize(R.styleable.CellLayout_yAxisStartPadding, 10);
154 mBottomPadding =
155 a.getDimensionPixelSize(R.styleable.CellLayout_yAxisEndPadding, 10);
Winson Chungaafa03c2010-06-11 17:34:16 -0700156
Adam Cohend22015c2010-07-26 22:02:18 -0700157 mCountX = LauncherModel.getCellCountX();
158 mCountY = LauncherModel.getCellCountY();
Michael Jurka0280c3b2010-09-17 15:00:07 -0700159 mOccupied = new boolean[mCountX][mCountY];
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800160
161 a.recycle();
162
163 setAlwaysDrawnWithCacheEnabled(false);
164
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700165 mWallpaperManager = WallpaperManager.getInstance(context);
166
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700167 final Resources res = getResources();
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700168
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700169 if (LauncherApplication.isScreenXLarge()) {
Winson Chung150fbab2010-09-29 17:14:26 -0700170 mBackgroundMini = res.getDrawable(R.drawable.mini_home_screen_bg);
Adam Cohenf34bab52010-09-30 14:11:56 -0700171 mBackgroundMini.setFilterBitmap(true);
Winson Chung150fbab2010-09-29 17:14:26 -0700172 mBackground = res.getDrawable(R.drawable.home_screen_bg);
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700173 mBackground.setFilterBitmap(true);
Winson Chung150fbab2010-09-29 17:14:26 -0700174 mBackgroundMiniHover = res.getDrawable(R.drawable.mini_home_screen_bg_hover);
Adam Cohenf34bab52010-09-30 14:11:56 -0700175 mBackgroundMiniHover.setFilterBitmap(true);
Patrick Dubroy1262e362010-10-06 15:49:50 -0700176 mBackgroundHover = res.getDrawable(R.drawable.home_screen_bg_hover);
177 mBackgroundHover.setFilterBitmap(true);
Michael Jurka3e7c7632010-10-02 16:01:03 -0700178 mBackgroundMiniAcceptsDrops = res.getDrawable(
179 R.drawable.mini_home_screen_bg_accepts_drops);
180 mBackgroundMiniAcceptsDrops.setFilterBitmap(true);
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700181 }
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700182
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700183 // Initialize the data structures used for the drag visualization.
Winson Chung150fbab2010-09-29 17:14:26 -0700184
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700185 mCrosshairsDrawable = res.getDrawable(R.drawable.gardening_crosshairs);
Patrick Dubroyce34a972010-10-19 10:34:32 -0700186 mEaseOutInterpolator = new DecelerateInterpolator(2.5f); // Quint ease out
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700187
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700188 // Set up the animation for fading the crosshairs in and out
189 int animDuration = res.getInteger(R.integer.config_crosshairsFadeInTime);
Patrick Dubroy49250ad2010-10-08 15:33:52 -0700190 mCrosshairsAnimator = new InterruptibleInOutAnimator(animDuration, 0.0f, 1.0f);
Chet Haase472b2812010-10-14 07:02:04 -0700191 mCrosshairsAnimator.getAnimator().addUpdateListener(new AnimatorUpdateListener() {
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700192 public void onAnimationUpdate(ValueAnimator animation) {
193 mCrosshairsVisibility = ((Float) animation.getAnimatedValue()).floatValue();
Patrick Dubroy8e58e912010-10-14 13:21:48 -0700194 invalidate();
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700195 }
196 });
Patrick Dubroyce34a972010-10-19 10:34:32 -0700197 mCrosshairsAnimator.getAnimator().setInterpolator(mEaseOutInterpolator);
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700198
Joe Onorato4be866d2010-10-10 11:26:02 -0700199 for (int i = 0; i < mDragOutlines.length; i++) {
200 mDragOutlines[i] = new Point(-1, -1);
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700201 }
202
203 // When dragging things around the home screens, we show a green outline of
204 // where the item will land. The outlines gradually fade out, leaving a trail
205 // behind the drag path.
206 // Set up all the animations that are used to implement this fading.
207 final int duration = res.getInteger(R.integer.config_dragOutlineFadeTime);
Chet Haase472b2812010-10-14 07:02:04 -0700208 final float fromAlphaValue = 0;
209 final float toAlphaValue = (float)res.getInteger(R.integer.config_dragOutlineMaxAlpha);
Joe Onorato4be866d2010-10-10 11:26:02 -0700210
Patrick Dubroy8e58e912010-10-14 13:21:48 -0700211 Arrays.fill(mDragOutlineAlphas, fromAlphaValue);
Joe Onorato4be866d2010-10-10 11:26:02 -0700212
213 for (int i = 0; i < mDragOutlineAnims.length; i++) {
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700214 final InterruptibleInOutAnimator anim =
215 new InterruptibleInOutAnimator(duration, fromAlphaValue, toAlphaValue);
Patrick Dubroyce34a972010-10-19 10:34:32 -0700216 anim.getAnimator().setInterpolator(mEaseOutInterpolator);
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700217 final int thisIndex = i;
Chet Haase472b2812010-10-14 07:02:04 -0700218 anim.getAnimator().addUpdateListener(new AnimatorUpdateListener() {
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700219 public void onAnimationUpdate(ValueAnimator animation) {
Joe Onorato4be866d2010-10-10 11:26:02 -0700220 final Bitmap outline = (Bitmap)anim.getTag();
221
222 // If an animation is started and then stopped very quickly, we can still
223 // get spurious updates we've cleared the tag. Guard against this.
224 if (outline == null) {
Patrick Dubroyfe6bd872010-10-13 17:32:10 -0700225 if (false) {
226 Object val = animation.getAnimatedValue();
227 Log.d(TAG, "anim " + thisIndex + " update: " + val +
228 ", isStopped " + anim.isStopped());
229 }
Joe Onorato4be866d2010-10-10 11:26:02 -0700230 // Try to prevent it from continuing to run
231 animation.cancel();
232 } else {
Chet Haase472b2812010-10-14 07:02:04 -0700233 mDragOutlineAlphas[thisIndex] = (Float) animation.getAnimatedValue();
Joe Onorato4be866d2010-10-10 11:26:02 -0700234 final int left = mDragOutlines[thisIndex].x;
235 final int top = mDragOutlines[thisIndex].y;
236 CellLayout.this.invalidate(left, top,
237 left + outline.getWidth(), top + outline.getHeight());
238 }
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700239 }
240 });
Joe Onorato4be866d2010-10-10 11:26:02 -0700241 // The animation holds a reference to the drag outline bitmap as long is it's
242 // running. This way the bitmap can be GCed when the animations are complete.
Chet Haase472b2812010-10-14 07:02:04 -0700243 anim.getAnimator().addListener(new AnimatorListenerAdapter() {
Joe Onorato4be866d2010-10-10 11:26:02 -0700244 public void onAnimationEnd(Animator animation) {
Chet Haase472b2812010-10-14 07:02:04 -0700245 if ((Float) ((ValueAnimator) animation).getAnimatedValue() == 0f) {
Joe Onorato4be866d2010-10-10 11:26:02 -0700246 anim.setTag(null);
247 }
248 }
249 });
250 mDragOutlineAnims[i] = anim;
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700251 }
Patrick Dubroyce34a972010-10-19 10:34:32 -0700252
253 mDropAnim = new ObjectAnimator();
254 mDropAnim.setInterpolator(mEaseOutInterpolator);
255
Michael Jurka18014792010-10-14 09:01:34 -0700256 mBackgroundRect = new Rect();
257 mHoverRect = new Rect();
258 setHoverScale(1.0f);
259 setHoverAlpha(1.0f);
260 }
261
262 private void updateHoverRect() {
263 float marginFraction = (mHoverScale - 1.0f) / 2.0f;
264 int marginX = (int) (marginFraction * (mBackgroundRect.right - mBackgroundRect.left));
265 int marginY = (int) (marginFraction * (mBackgroundRect.bottom - mBackgroundRect.top));
266 mHoverRect.set(mBackgroundRect.left - marginX, mBackgroundRect.top - marginY,
267 mBackgroundRect.right + marginX, mBackgroundRect.bottom + marginY);
268 invalidate();
269 }
270
271 public void setHoverScale(float scaleFactor) {
272 if (scaleFactor != mHoverScale) {
273 mHoverScale = scaleFactor;
274 updateHoverRect();
275 }
276 }
277
278 public float getHoverScale() {
279 return mHoverScale;
280 }
281
282 public float getHoverAlpha() {
283 return mHoverAlpha;
284 }
285
286 public void setHoverAlpha(float alpha) {
287 mHoverAlpha = alpha;
288 invalidate();
289 }
290
291 void animateDrop() {
292 if (LauncherApplication.isScreenXLarge()) {
293 Resources res = getResources();
294 float onDropScale = res.getInteger(R.integer.config_screenOnDropScalePercent) / 100.0f;
295 ObjectAnimator scaleUp = ObjectAnimator.ofFloat(this, "hoverScale", onDropScale);
296 scaleUp.setDuration(res.getInteger(R.integer.config_screenOnDropScaleUpDuration));
297 ObjectAnimator scaleDown = ObjectAnimator.ofFloat(this, "hoverScale", 1.0f);
298 scaleDown.setDuration(res.getInteger(R.integer.config_screenOnDropScaleDownDuration));
299 ObjectAnimator alphaFadeOut = ObjectAnimator.ofFloat(this, "hoverAlpha", 0.0f);
300
301 alphaFadeOut.setStartDelay(res.getInteger(R.integer.config_screenOnDropAlphaFadeDelay));
302 alphaFadeOut.setDuration(res.getInteger(R.integer.config_screenOnDropAlphaFadeDelay));
303
304 AnimatorSet bouncer = new AnimatorSet();
305 bouncer.play(scaleUp).before(scaleDown);
306 bouncer.play(scaleUp).with(alphaFadeOut);
307 bouncer.addListener(new AnimatorListenerAdapter() {
308 public void onAnimationStart(Animator animation) {
309 setHover(true);
310 }
311 public void onAnimationEnd(Animator animation) {
312 setHover(false);
313 setHoverScale(1.0f);
314 setHoverAlpha(1.0f);
315 }
316 });
317 bouncer.start();
318 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800319 }
320
Michael Jurkaa63c4522010-08-19 13:52:27 -0700321 public void setHover(boolean value) {
322 if (mHover != value) {
Michael Jurka3e7c7632010-10-02 16:01:03 -0700323 mHover = value;
Michael Jurkaa63c4522010-08-19 13:52:27 -0700324 invalidate();
325 }
Michael Jurkaa63c4522010-08-19 13:52:27 -0700326 }
327
Patrick Dubroy1262e362010-10-06 15:49:50 -0700328 public void drawChildren(Canvas canvas) {
329 super.dispatchDraw(canvas);
330 }
331
Jeff Sharkey83f111d2009-04-20 21:03:13 -0700332 @Override
Patrick Dubroy1262e362010-10-06 15:49:50 -0700333 protected void onDraw(Canvas canvas) {
Michael Jurka3e7c7632010-10-02 16:01:03 -0700334 // When we're large, we are either drawn in a "hover" state (ie when dragging an item to
335 // a neighboring page) or with just a normal background (if backgroundAlpha > 0.0f)
336 // When we're small, we are either drawn normally or in the "accepts drops" state (during
337 // a drag). However, we also drag the mini hover background *over* one of those two
338 // backgrounds
Michael Jurka5f1c5092010-09-03 14:15:02 -0700339 if (mBackgroundAlpha > 0.0f) {
Adam Cohenf34bab52010-09-30 14:11:56 -0700340 Drawable bg;
Patrick Dubroy1262e362010-10-06 15:49:50 -0700341 if (getScaleX() < 0.5f) {
Michael Jurka3e7c7632010-10-02 16:01:03 -0700342 bg = mAcceptsDrops ? mBackgroundMiniAcceptsDrops : mBackgroundMini;
Adam Cohenf34bab52010-09-30 14:11:56 -0700343 } else {
Patrick Dubroy1262e362010-10-06 15:49:50 -0700344 bg = mHover ? mBackgroundHover : mBackground;
Adam Cohenf34bab52010-09-30 14:11:56 -0700345 }
Adam Cohen9c4949e2010-10-05 12:27:22 -0700346 if (bg != null) {
347 bg.setAlpha((int) (mBackgroundAlpha * 255));
Michael Jurka18014792010-10-14 09:01:34 -0700348 bg.setBounds(mBackgroundRect);
Adam Cohen9c4949e2010-10-05 12:27:22 -0700349 bg.draw(canvas);
350 }
Michael Jurka3e7c7632010-10-02 16:01:03 -0700351 if (mHover && getScaleX() < 0.5f) {
Michael Jurka18014792010-10-14 09:01:34 -0700352 boolean modifiedClipRect = false;
353 if (mHoverScale > 1.0f) {
354 // If the hover background's scale is greater than 1, we'll be drawing outside
355 // the bounds of this CellLayout. Get around that by temporarily increasing the
356 // size of the clip rect
357 float marginFraction = (mHoverScale - 1.0f) / 2.0f;
358 Rect clipRect = canvas.getClipBounds();
359 int marginX = (int) (marginFraction * (clipRect.right - clipRect.left));
360 int marginY = (int) (marginFraction * (clipRect.bottom - clipRect.top));
361 canvas.save(Canvas.CLIP_SAVE_FLAG);
362 canvas.clipRect(-marginX, -marginY,
363 getWidth() + marginX, getHeight() + marginY, Region.Op.REPLACE);
364 modifiedClipRect = true;
365 }
366
367 mBackgroundMiniHover.setAlpha((int) (mBackgroundAlpha * mHoverAlpha * 255));
368 mBackgroundMiniHover.setBounds(mHoverRect);
Michael Jurka3e7c7632010-10-02 16:01:03 -0700369 mBackgroundMiniHover.draw(canvas);
Michael Jurka18014792010-10-14 09:01:34 -0700370 if (modifiedClipRect) {
371 canvas.restore();
372 }
Michael Jurka3e7c7632010-10-02 16:01:03 -0700373 }
Michael Jurkaa63c4522010-08-19 13:52:27 -0700374 }
Romain Guya6abce82009-11-10 02:54:41 -0800375
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700376 if (mCrosshairsVisibility > 0.0f) {
377 final int countX = mCountX;
378 final int countY = mCountY;
379
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700380 final float MAX_ALPHA = 0.4f;
381 final int MAX_VISIBLE_DISTANCE = 600;
382 final float DISTANCE_MULTIPLIER = 0.002f;
383
384 final Drawable d = mCrosshairsDrawable;
385 final int width = d.getIntrinsicWidth();
386 final int height = d.getIntrinsicHeight();
387
388 int x = getLeftPadding() - (mWidthGap / 2) - (width / 2);
389 for (int col = 0; col <= countX; col++) {
390 int y = getTopPadding() - (mHeightGap / 2) - (height / 2);
391 for (int row = 0; row <= countY; row++) {
392 mTmpPointF.set(x - mDragCenter.x, y - mDragCenter.y);
393 float dist = mTmpPointF.length();
394 // Crosshairs further from the drag point are more faint
395 float alpha = Math.min(MAX_ALPHA,
396 DISTANCE_MULTIPLIER * (MAX_VISIBLE_DISTANCE - dist));
397 if (alpha > 0.0f) {
398 d.setBounds(x, y, x + width, y + height);
399 d.setAlpha((int) (alpha * 255 * mCrosshairsVisibility));
400 d.draw(canvas);
401 }
402 y += mCellHeight + mHeightGap;
403 }
404 x += mCellWidth + mWidthGap;
405 }
Joe Onorato4be866d2010-10-10 11:26:02 -0700406 }
Winson Chung150fbab2010-09-29 17:14:26 -0700407
Patrick Dubroy8e58e912010-10-14 13:21:48 -0700408 final Paint paint = mDragOutlinePaint;
Joe Onorato4be866d2010-10-10 11:26:02 -0700409 for (int i = 0; i < mDragOutlines.length; i++) {
Chet Haase472b2812010-10-14 07:02:04 -0700410 final float alpha = mDragOutlineAlphas[i];
Joe Onorato4be866d2010-10-10 11:26:02 -0700411 if (alpha > 0) {
412 final Point p = mDragOutlines[i];
413 final Bitmap b = (Bitmap) mDragOutlineAnims[i].getTag();
Chet Haase472b2812010-10-14 07:02:04 -0700414 paint.setAlpha((int)(alpha + .5f));
Joe Onorato4be866d2010-10-10 11:26:02 -0700415 canvas.drawBitmap(b, p.x, p.y, paint);
Winson Chung150fbab2010-09-29 17:14:26 -0700416 }
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700417 }
418 }
419
Adam Cohenf34bab52010-09-30 14:11:56 -0700420 public void setDimmableProgress(float progress) {
421 for (int i = 0; i < getChildCount(); i++) {
422 Dimmable d = (Dimmable) getChildAt(i);
423 d.setDimmableProgress(progress);
424 }
425 }
426
427 public float getDimmableProgress() {
428 if (getChildCount() > 0) {
429 return ((Dimmable) getChildAt(0)).getDimmableProgress();
430 }
431 return 0.0f;
432 }
433
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700434 @Override
Jeff Sharkey83f111d2009-04-20 21:03:13 -0700435 public void cancelLongPress() {
436 super.cancelLongPress();
437
438 // Cancel long press for all children
439 final int count = getChildCount();
440 for (int i = 0; i < count; i++) {
441 final View child = getChildAt(i);
442 child.cancelLongPress();
443 }
444 }
445
Michael Jurkadee05892010-07-27 10:01:56 -0700446 public void setOnInterceptTouchListener(View.OnTouchListener listener) {
447 mInterceptTouchListener = listener;
448 }
449
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800450 int getCountX() {
Adam Cohend22015c2010-07-26 22:02:18 -0700451 return mCountX;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800452 }
453
454 int getCountY() {
Adam Cohend22015c2010-07-26 22:02:18 -0700455 return mCountY;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800456 }
457
Winson Chungaafa03c2010-06-11 17:34:16 -0700458 public boolean addViewToCellLayout(View child, int index, int childId, LayoutParams params) {
459 final LayoutParams lp = params;
460
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800461 // Generate an id for each view, this assumes we have at most 256x256 cells
462 // per workspace screen
Adam Cohend22015c2010-07-26 22:02:18 -0700463 if (lp.cellX >= 0 && lp.cellX <= mCountX - 1 && lp.cellY >= 0 && lp.cellY <= mCountY - 1) {
Winson Chungaafa03c2010-06-11 17:34:16 -0700464 // If the horizontal or vertical span is set to -1, it is taken to
465 // mean that it spans the extent of the CellLayout
Adam Cohend22015c2010-07-26 22:02:18 -0700466 if (lp.cellHSpan < 0) lp.cellHSpan = mCountX;
467 if (lp.cellVSpan < 0) lp.cellVSpan = mCountY;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800468
Winson Chungaafa03c2010-06-11 17:34:16 -0700469 child.setId(childId);
470
Michael Jurkadee05892010-07-27 10:01:56 -0700471 // We might be in the middle or end of shrinking/fading to a dimmed view
472 // Make sure this view's alpha is set the same as all the rest of the views
Michael Jurka5f1c5092010-09-03 14:15:02 -0700473 child.setAlpha(getAlpha());
Winson Chungaafa03c2010-06-11 17:34:16 -0700474 addView(child, index, lp);
Michael Jurkadee05892010-07-27 10:01:56 -0700475
Michael Jurka0280c3b2010-09-17 15:00:07 -0700476 markCellsAsOccupiedForView(child);
477
Winson Chungaafa03c2010-06-11 17:34:16 -0700478 return true;
479 }
480 return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800481 }
Michael Jurka3e7c7632010-10-02 16:01:03 -0700482 public void setAcceptsDrops(boolean acceptsDrops) {
483 if (mAcceptsDrops != acceptsDrops) {
484 mAcceptsDrops = acceptsDrops;
485 invalidate();
486 }
487 }
488
489 public boolean getAcceptsDrops() {
490 return mAcceptsDrops;
491 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800492
493 @Override
Michael Jurka0280c3b2010-09-17 15:00:07 -0700494 public void removeAllViews() {
495 clearOccupiedCells();
496 }
497
498 @Override
499 public void removeAllViewsInLayout() {
500 clearOccupiedCells();
501 }
502
503 @Override
504 public void removeView(View view) {
505 markCellsAsUnoccupiedForView(view);
506 super.removeView(view);
507 }
508
509 @Override
510 public void removeViewAt(int index) {
511 markCellsAsUnoccupiedForView(getChildAt(index));
512 super.removeViewAt(index);
513 }
514
515 @Override
516 public void removeViewInLayout(View view) {
517 markCellsAsUnoccupiedForView(view);
518 super.removeViewInLayout(view);
519 }
520
521 @Override
522 public void removeViews(int start, int count) {
523 for (int i = start; i < start + count; i++) {
524 markCellsAsUnoccupiedForView(getChildAt(i));
525 }
526 super.removeViews(start, count);
527 }
528
529 @Override
530 public void removeViewsInLayout(int start, int count) {
531 for (int i = start; i < start + count; i++) {
532 markCellsAsUnoccupiedForView(getChildAt(i));
533 }
534 super.removeViewsInLayout(start, count);
535 }
536
537 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800538 public void requestChildFocus(View child, View focused) {
539 super.requestChildFocus(child, focused);
540 if (child != null) {
541 Rect r = new Rect();
542 child.getDrawingRect(r);
543 requestRectangleOnScreen(r);
544 }
545 }
546
547 @Override
548 protected void onAttachedToWindow() {
549 super.onAttachedToWindow();
550 mCellInfo.screen = ((ViewGroup) getParent()).indexOfChild(this);
551 }
552
Michael Jurkaaf442092010-06-10 17:01:57 -0700553 public void setTagToCellInfoForPoint(int touchX, int touchY) {
554 final CellInfo cellInfo = mCellInfo;
555 final Rect frame = mRect;
556 final int x = touchX + mScrollX;
557 final int y = touchY + mScrollY;
558 final int count = getChildCount();
559
560 boolean found = false;
561 for (int i = count - 1; i >= 0; i--) {
562 final View child = getChildAt(i);
563
564 if ((child.getVisibility()) == VISIBLE || child.getAnimation() != null) {
565 child.getHitRect(frame);
566 if (frame.contains(x, y)) {
567 final LayoutParams lp = (LayoutParams) child.getLayoutParams();
568 cellInfo.cell = child;
569 cellInfo.cellX = lp.cellX;
570 cellInfo.cellY = lp.cellY;
571 cellInfo.spanX = lp.cellHSpan;
572 cellInfo.spanY = lp.cellVSpan;
573 cellInfo.valid = true;
574 found = true;
Michael Jurkaaf442092010-06-10 17:01:57 -0700575 break;
576 }
577 }
578 }
Winson Chungaafa03c2010-06-11 17:34:16 -0700579
Michael Jurkaaf442092010-06-10 17:01:57 -0700580 if (!found) {
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700581 final int cellXY[] = mTmpCellXY;
Michael Jurkaaf442092010-06-10 17:01:57 -0700582 pointToCellExact(x, y, cellXY);
583
Michael Jurkaaf442092010-06-10 17:01:57 -0700584 cellInfo.cell = null;
585 cellInfo.cellX = cellXY[0];
586 cellInfo.cellY = cellXY[1];
587 cellInfo.spanX = 1;
588 cellInfo.spanY = 1;
Michael Jurka0280c3b2010-09-17 15:00:07 -0700589 cellInfo.valid = cellXY[0] >= 0 && cellXY[1] >= 0 && cellXY[0] < mCountX &&
590 cellXY[1] < mCountY && !mOccupied[cellXY[0]][cellXY[1]];
Michael Jurkaaf442092010-06-10 17:01:57 -0700591 }
592 setTag(cellInfo);
593 }
594
Winson Chungaafa03c2010-06-11 17:34:16 -0700595
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800596 @Override
597 public boolean onInterceptTouchEvent(MotionEvent ev) {
Michael Jurkadee05892010-07-27 10:01:56 -0700598 if (mInterceptTouchListener != null && mInterceptTouchListener.onTouch(this, ev)) {
599 return true;
600 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800601 final int action = ev.getAction();
602 final CellInfo cellInfo = mCellInfo;
603
604 if (action == MotionEvent.ACTION_DOWN) {
Michael Jurkaaf442092010-06-10 17:01:57 -0700605 setTagToCellInfoForPoint((int) ev.getX(), (int) ev.getY());
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800606 } else if (action == MotionEvent.ACTION_UP) {
607 cellInfo.cell = null;
608 cellInfo.cellX = -1;
609 cellInfo.cellY = -1;
610 cellInfo.spanX = 0;
611 cellInfo.spanY = 0;
612 cellInfo.valid = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800613 setTag(cellInfo);
614 }
615
616 return false;
617 }
618
619 @Override
620 public CellInfo getTag() {
Michael Jurka0280c3b2010-09-17 15:00:07 -0700621 return (CellInfo) super.getTag();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800622 }
623
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700624 /**
625 * Check if the row 'y' is empty from columns 'left' to 'right', inclusive.
626 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800627 private static boolean isRowEmpty(int y, int left, int right, boolean[][] occupied) {
628 for (int x = left; x <= right; x++) {
629 if (occupied[x][y]) {
630 return false;
631 }
632 }
633 return true;
634 }
635
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800636 /**
Winson Chungaafa03c2010-06-11 17:34:16 -0700637 * Given a point, return the cell that strictly encloses that point
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800638 * @param x X coordinate of the point
639 * @param y Y coordinate of the point
640 * @param result Array of 2 ints to hold the x and y coordinate of the cell
641 */
642 void pointToCellExact(int x, int y, int[] result) {
Winson Chungaafa03c2010-06-11 17:34:16 -0700643 final int hStartPadding = getLeftPadding();
644 final int vStartPadding = getTopPadding();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800645
646 result[0] = (x - hStartPadding) / (mCellWidth + mWidthGap);
647 result[1] = (y - vStartPadding) / (mCellHeight + mHeightGap);
648
Adam Cohend22015c2010-07-26 22:02:18 -0700649 final int xAxis = mCountX;
650 final int yAxis = mCountY;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800651
652 if (result[0] < 0) result[0] = 0;
653 if (result[0] >= xAxis) result[0] = xAxis - 1;
654 if (result[1] < 0) result[1] = 0;
655 if (result[1] >= yAxis) result[1] = yAxis - 1;
656 }
Winson Chungaafa03c2010-06-11 17:34:16 -0700657
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800658 /**
659 * Given a point, return the cell that most closely encloses that point
660 * @param x X coordinate of the point
661 * @param y Y coordinate of the point
662 * @param result Array of 2 ints to hold the x and y coordinate of the cell
663 */
664 void pointToCellRounded(int x, int y, int[] result) {
665 pointToCellExact(x + (mCellWidth / 2), y + (mCellHeight / 2), result);
666 }
667
668 /**
669 * Given a cell coordinate, return the point that represents the upper left corner of that cell
Winson Chungaafa03c2010-06-11 17:34:16 -0700670 *
671 * @param cellX X coordinate of the cell
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800672 * @param cellY Y coordinate of the cell
Winson Chungaafa03c2010-06-11 17:34:16 -0700673 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800674 * @param result Array of 2 ints to hold the x and y coordinate of the point
675 */
676 void cellToPoint(int cellX, int cellY, int[] result) {
Winson Chungaafa03c2010-06-11 17:34:16 -0700677 final int hStartPadding = getLeftPadding();
678 final int vStartPadding = getTopPadding();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800679
680 result[0] = hStartPadding + cellX * (mCellWidth + mWidthGap);
681 result[1] = vStartPadding + cellY * (mCellHeight + mHeightGap);
682 }
683
Romain Guy84f296c2009-11-04 15:00:44 -0800684 int getCellWidth() {
685 return mCellWidth;
686 }
687
688 int getCellHeight() {
689 return mCellHeight;
690 }
691
Romain Guy1a304a12009-11-10 00:02:32 -0800692 int getLeftPadding() {
Winson Chungaafa03c2010-06-11 17:34:16 -0700693 return mLeftPadding;
Romain Guy1a304a12009-11-10 00:02:32 -0800694 }
695
696 int getTopPadding() {
Winson Chungaafa03c2010-06-11 17:34:16 -0700697 return mTopPadding;
Romain Guy1a304a12009-11-10 00:02:32 -0800698 }
699
700 int getRightPadding() {
Winson Chungaafa03c2010-06-11 17:34:16 -0700701 return mRightPadding;
Romain Guy1a304a12009-11-10 00:02:32 -0800702 }
703
704 int getBottomPadding() {
Winson Chungaafa03c2010-06-11 17:34:16 -0700705 return mBottomPadding;
Romain Guy1a304a12009-11-10 00:02:32 -0800706 }
707
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800708 @Override
709 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
710 // TODO: currently ignoring padding
Winson Chungaafa03c2010-06-11 17:34:16 -0700711
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800712 int widthSpecMode = MeasureSpec.getMode(widthMeasureSpec);
Winson Chungaafa03c2010-06-11 17:34:16 -0700713 int widthSpecSize = MeasureSpec.getSize(widthMeasureSpec);
714
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800715 int heightSpecMode = MeasureSpec.getMode(heightMeasureSpec);
716 int heightSpecSize = MeasureSpec.getSize(heightMeasureSpec);
Winson Chungaafa03c2010-06-11 17:34:16 -0700717
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800718 if (widthSpecMode == MeasureSpec.UNSPECIFIED || heightSpecMode == MeasureSpec.UNSPECIFIED) {
719 throw new RuntimeException("CellLayout cannot have UNSPECIFIED dimensions");
720 }
721
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800722 final int cellWidth = mCellWidth;
723 final int cellHeight = mCellHeight;
724
Adam Cohend22015c2010-07-26 22:02:18 -0700725 int numWidthGaps = mCountX - 1;
726 int numHeightGaps = mCountY - 1;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800727
Michael Jurka0280c3b2010-09-17 15:00:07 -0700728 int vSpaceLeft = heightSpecSize - mTopPadding - mBottomPadding - (cellHeight * mCountY);
Adam Cohend22015c2010-07-26 22:02:18 -0700729 mHeightGap = vSpaceLeft / numHeightGaps;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800730
Michael Jurka0280c3b2010-09-17 15:00:07 -0700731 int hSpaceLeft = widthSpecSize - mLeftPadding - mRightPadding - (cellWidth * mCountX);
Adam Cohend22015c2010-07-26 22:02:18 -0700732 mWidthGap = hSpaceLeft / numWidthGaps;
Winson Chungaafa03c2010-06-11 17:34:16 -0700733
Michael Jurka5f1c5092010-09-03 14:15:02 -0700734 // center it around the min gaps
735 int minGap = Math.min(mWidthGap, mHeightGap);
736 mWidthGap = mHeightGap = minGap;
737
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800738 int count = getChildCount();
739
740 for (int i = 0; i < count; i++) {
741 View child = getChildAt(i);
742 LayoutParams lp = (LayoutParams) child.getLayoutParams();
Winson Chungaafa03c2010-06-11 17:34:16 -0700743 lp.setup(cellWidth, cellHeight, mWidthGap, mHeightGap,
744 mLeftPadding, mTopPadding);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800745
Michael Jurka0280c3b2010-09-17 15:00:07 -0700746 int childWidthMeasureSpec = MeasureSpec.makeMeasureSpec(lp.width, MeasureSpec.EXACTLY);
Winson Chungaafa03c2010-06-11 17:34:16 -0700747 int childheightMeasureSpec = MeasureSpec.makeMeasureSpec(lp.height,
748 MeasureSpec.EXACTLY);
749
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800750 child.measure(childWidthMeasureSpec, childheightMeasureSpec);
751 }
Michael Jurka5f1c5092010-09-03 14:15:02 -0700752 if (widthSpecMode == MeasureSpec.AT_MOST) {
753 int newWidth = mLeftPadding + mRightPadding + (mCountX * cellWidth) +
754 ((mCountX - 1) * minGap);
755 int newHeight = mTopPadding + mBottomPadding + (mCountY * cellHeight) +
756 ((mCountY - 1) * minGap);
757 setMeasuredDimension(newWidth, newHeight);
758 } else if (widthSpecMode == MeasureSpec.EXACTLY) {
759 setMeasuredDimension(widthSpecSize, heightSpecSize);
760 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800761 }
762
Patrick Dubroyce34a972010-10-19 10:34:32 -0700763 /**
764 * Animate a child of this CellLayout into its current layout position.
765 * The position to animate from is given by the oldX and oldY values in its LayoutParams.
766 */
767 private void animateChildIntoPosition(final View child) {
768 final Resources res = getResources();
769 final ObjectAnimator anim = mDropAnim;
770 final CellLayout.LayoutParams lp = (CellLayout.LayoutParams) child.getLayoutParams();
771 final float startX = lp.oldX - lp.x;
772 final float startY = lp.oldY - lp.y;
773
774 // Calculate the duration of the animation based on the object's distance
775 final float dist = (float) Math.sqrt(startX*startX + startY*startY);
776 final float maxDist = (float) res.getInteger(R.integer.config_dropAnimMaxDist);
777 final int duration = (int) (res.getInteger(R.integer.config_dropAnimMaxDuration)
778 * mEaseOutInterpolator.getInterpolation(dist / maxDist));
779
780 anim.cancel(); // Make sure it's not already running
781 anim.setDuration(duration);
782 anim.setTarget(child);
783 anim.setPropertyName("translationX");
784 anim.setFloatValues(startX, 0);
785
786 anim.removeAllUpdateListeners();
787 anim.addUpdateListener(new AnimatorUpdateListener() {
788 public void onAnimationUpdate(ValueAnimator animation) {
789 // Set the value of translationY based on the current x value
790 final float translationX = (Float) anim.getAnimatedValue();
791 child.setTranslationY((startY / startX) * translationX);
792 }
793 });
794 anim.start();
795 }
796
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800797 @Override
Michael Jurka28750fb2010-09-24 17:43:49 -0700798 protected void onLayout(boolean changed, int l, int t, int r, int b) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800799 int count = getChildCount();
800
801 for (int i = 0; i < count; i++) {
Patrick Dubroyce34a972010-10-19 10:34:32 -0700802 final View child = getChildAt(i);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800803 if (child.getVisibility() != GONE) {
804
805 CellLayout.LayoutParams lp = (CellLayout.LayoutParams) child.getLayoutParams();
806
807 int childLeft = lp.x;
808 int childTop = lp.y;
809 child.layout(childLeft, childTop, childLeft + lp.width, childTop + lp.height);
Romain Guy84f296c2009-11-04 15:00:44 -0800810
811 if (lp.dropped) {
812 lp.dropped = false;
813
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700814 final int[] cellXY = mTmpCellXY;
Romain Guy06762ab2010-01-25 16:51:08 -0800815 getLocationOnScreen(cellXY);
Romain Guy84f296c2009-11-04 15:00:44 -0800816 mWallpaperManager.sendWallpaperCommand(getWindowToken(), "android.home.drop",
Romain Guy06762ab2010-01-25 16:51:08 -0800817 cellXY[0] + childLeft + lp.width / 2,
818 cellXY[1] + childTop + lp.height / 2, 0, null);
Patrick Dubroyce34a972010-10-19 10:34:32 -0700819
820 animateChildIntoPosition(child);
Romain Guy84f296c2009-11-04 15:00:44 -0800821 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800822 }
823 }
824 }
825
826 @Override
Michael Jurkadee05892010-07-27 10:01:56 -0700827 protected void onSizeChanged(int w, int h, int oldw, int oldh) {
828 super.onSizeChanged(w, h, oldw, oldh);
Michael Jurka18014792010-10-14 09:01:34 -0700829 mBackgroundRect.set(0, 0, w, h);
830 updateHoverRect();
Michael Jurkadee05892010-07-27 10:01:56 -0700831 }
832
833 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800834 protected void setChildrenDrawingCacheEnabled(boolean enabled) {
835 final int count = getChildCount();
836 for (int i = 0; i < count; i++) {
837 final View view = getChildAt(i);
838 view.setDrawingCacheEnabled(enabled);
839 // Update the drawing caches
Adam Powellfefa0ce2010-05-03 10:23:50 -0700840 view.buildDrawingCache(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800841 }
842 }
843
844 @Override
845 protected void setChildrenDrawnWithCacheEnabled(boolean enabled) {
846 super.setChildrenDrawnWithCacheEnabled(enabled);
847 }
848
Michael Jurka5f1c5092010-09-03 14:15:02 -0700849 public float getBackgroundAlpha() {
850 return mBackgroundAlpha;
Michael Jurkadee05892010-07-27 10:01:56 -0700851 }
852
Michael Jurka5f1c5092010-09-03 14:15:02 -0700853 public void setBackgroundAlpha(float alpha) {
854 mBackgroundAlpha = alpha;
Michael Jurka0142d492010-08-25 17:46:15 -0700855 invalidate();
Michael Jurkadee05892010-07-27 10:01:56 -0700856 }
857
Michael Jurka5f1c5092010-09-03 14:15:02 -0700858 // Need to return true to let the view system know we know how to handle alpha-- this is
859 // because when our children have an alpha of 0.0f, they are still rendering their "dimmed"
860 // versions
861 @Override
862 protected boolean onSetAlpha(int alpha) {
863 return true;
864 }
865
866 public void setAlpha(float alpha) {
867 setChildrenAlpha(alpha);
868 super.setAlpha(alpha);
869 }
870
Michael Jurkadee05892010-07-27 10:01:56 -0700871 private void setChildrenAlpha(float alpha) {
Michael Jurka0142d492010-08-25 17:46:15 -0700872 final int childCount = getChildCount();
873 for (int i = 0; i < childCount; i++) {
Michael Jurkadee05892010-07-27 10:01:56 -0700874 getChildAt(i).setAlpha(alpha);
875 }
876 }
877
Michael Jurka0280c3b2010-09-17 15:00:07 -0700878 private boolean isVacantIgnoring(
879 int originX, int originY, int spanX, int spanY, View ignoreView) {
880 if (ignoreView != null) {
881 markCellsAsUnoccupiedForView(ignoreView);
882 }
Michael Jurka28750fb2010-09-24 17:43:49 -0700883 boolean isVacant = true;
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700884 for (int i = 0; i < spanY; i++) {
885 if (!isRowEmpty(originY + i, originX, originX + spanX - 1, mOccupied)) {
Michael Jurka28750fb2010-09-24 17:43:49 -0700886 isVacant = false;
887 break;
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700888 }
889 }
Michael Jurka0280c3b2010-09-17 15:00:07 -0700890 if (ignoreView != null) {
891 markCellsAsOccupiedForView(ignoreView);
892 }
Michael Jurka28750fb2010-09-24 17:43:49 -0700893 return isVacant;
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700894 }
895
Michael Jurka0280c3b2010-09-17 15:00:07 -0700896 private boolean isVacant(int originX, int originY, int spanX, int spanY) {
897 return isVacantIgnoring(originX, originY, spanX, spanY, null);
898 }
899
Patrick Dubroy440c3602010-07-13 17:50:32 -0700900 public View getChildAt(int x, int y) {
901 final int count = getChildCount();
902 for (int i = 0; i < count; i++) {
903 View child = getChildAt(i);
904 LayoutParams lp = (LayoutParams) child.getLayoutParams();
905
906 if ((lp.cellX <= x) && (x < lp.cellX + lp.cellHSpan) &&
907 (lp.cellY <= y) && (y < lp.cellY + lp.cellHSpan)) {
908 return child;
909 }
910 }
911 return null;
912 }
913
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700914 /**
Patrick Dubroy8f86ddc2010-07-16 13:55:32 -0700915 * Estimate the size that a child with the given dimensions will take in the layout.
916 */
917 void estimateChildSize(int minWidth, int minHeight, int[] result) {
918 // Assuming it's placed at 0, 0, find where the bottom right cell will land
919 rectToCell(minWidth, minHeight, result);
920
921 // Then figure out the rect it will occupy
922 cellToRect(0, 0, result[0], result[1], mRectF);
923 result[0] = (int)mRectF.width();
924 result[1] = (int)mRectF.height();
925 }
926
927 /**
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700928 * Estimate where the top left cell of the dragged item will land if it is dropped.
929 *
930 * @param originX The X value of the top left corner of the item
931 * @param originY The Y value of the top left corner of the item
932 * @param spanX The number of horizontal cells that the item spans
933 * @param spanY The number of vertical cells that the item spans
934 * @param result The estimated drop cell X and Y.
935 */
936 void estimateDropCell(int originX, int originY, int spanX, int spanY, int[] result) {
Adam Cohend22015c2010-07-26 22:02:18 -0700937 final int countX = mCountX;
938 final int countY = mCountY;
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700939
Michael Jurkaa63c4522010-08-19 13:52:27 -0700940 // pointToCellRounded takes the top left of a cell but will pad that with
941 // cellWidth/2 and cellHeight/2 when finding the matching cell
942 pointToCellRounded(originX, originY, result);
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700943
944 // If the item isn't fully on this screen, snap to the edges
945 int rightOverhang = result[0] + spanX - countX;
946 if (rightOverhang > 0) {
947 result[0] -= rightOverhang; // Snap to right
948 }
949 result[0] = Math.max(0, result[0]); // Snap to left
950 int bottomOverhang = result[1] + spanY - countY;
951 if (bottomOverhang > 0) {
952 result[1] -= bottomOverhang; // Snap to bottom
953 }
954 result[1] = Math.max(0, result[1]); // Snap to top
955 }
956
Joe Onorato4be866d2010-10-10 11:26:02 -0700957 void visualizeDropLocation(
958 View v, Bitmap dragOutline, int originX, int originY, int spanX, int spanY) {
959
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -0700960 final int oldDragCellX = mDragCell[0];
961 final int oldDragCellY = mDragCell[1];
Joe Onorato4be866d2010-10-10 11:26:02 -0700962 final int[] nearest = findNearestVacantArea(originX, originY, spanX, spanY, v, mDragCell);
963 mDragCenter.set(originX + (v.getWidth() / 2), originY + (v.getHeight() / 2));
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700964
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -0700965 if (nearest != null && (nearest[0] != oldDragCellX || nearest[1] != oldDragCellY)) {
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700966 // Find the top left corner of the rect the object will occupy
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700967 final int[] topLeft = mTmpPoint;
968 cellToPoint(nearest[0], nearest[1], topLeft);
969
Joe Onorato4be866d2010-10-10 11:26:02 -0700970 int left = topLeft[0];
971 int top = topLeft[1];
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700972
Joe Onorato4be866d2010-10-10 11:26:02 -0700973 if (v.getParent() instanceof CellLayout) {
974 LayoutParams lp = (LayoutParams) v.getLayoutParams();
975 left += lp.leftMargin;
976 top += lp.topMargin;
977 }
Winson Chung150fbab2010-09-29 17:14:26 -0700978
Joe Onorato4be866d2010-10-10 11:26:02 -0700979 // Offsets due to the size difference between the View and the dragOutline
980 left += (v.getWidth() - dragOutline.getWidth()) / 2;
981 top += (v.getHeight() - dragOutline.getHeight()) / 2;
Winson Chung150fbab2010-09-29 17:14:26 -0700982
Joe Onorato4be866d2010-10-10 11:26:02 -0700983 final int oldIndex = mDragOutlineCurrent;
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -0700984 mDragOutlineAnims[oldIndex].animateOut();
985 mDragOutlineCurrent = (oldIndex + 1) % mDragOutlines.length;
Winson Chung150fbab2010-09-29 17:14:26 -0700986
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -0700987 mDragOutlines[mDragOutlineCurrent].set(left, top);
988 mDragOutlineAnims[mDragOutlineCurrent].setTag(dragOutline);
989 mDragOutlineAnims[mDragOutlineCurrent].animateIn();
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700990 }
Patrick Dubroy49250ad2010-10-08 15:33:52 -0700991
992 // If we are drawing crosshairs, the entire CellLayout needs to be invalidated
993 if (mCrosshairsDrawable != null) {
994 invalidate();
995 }
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700996 }
997
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800998 /**
Jeff Sharkey70864282009-04-07 21:08:40 -0700999 * Find a vacant area that will fit the given bounds nearest the requested
1000 * cell location. Uses Euclidean distance to score multiple vacant areas.
Winson Chungaafa03c2010-06-11 17:34:16 -07001001 *
Romain Guy51afc022009-05-04 18:03:43 -07001002 * @param pixelX The X location at which you want to search for a vacant area.
1003 * @param pixelY The Y location at which you want to search for a vacant area.
Jeff Sharkey70864282009-04-07 21:08:40 -07001004 * @param spanX Horizontal span of the object.
1005 * @param spanY Vertical span of the object.
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001006 * @param result Array in which to place the result, or null (in which case a new array will
1007 * be allocated)
Jeff Sharkey70864282009-04-07 21:08:40 -07001008 * @return The X, Y cell of a vacant area that can contain this object,
1009 * nearest the requested location.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001010 */
Michael Jurka6a1435d2010-09-27 17:35:12 -07001011 int[] findNearestVacantArea(
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001012 int pixelX, int pixelY, int spanX, int spanY, int[] result) {
1013 return findNearestVacantArea(pixelX, pixelY, spanX, spanY, null, result);
Michael Jurka6a1435d2010-09-27 17:35:12 -07001014 }
Winson Chungaafa03c2010-06-11 17:34:16 -07001015
Michael Jurka6a1435d2010-09-27 17:35:12 -07001016 /**
1017 * Find a vacant area that will fit the given bounds nearest the requested
1018 * cell location. Uses Euclidean distance to score multiple vacant areas.
1019 *
1020 * @param pixelX The X location at which you want to search for a vacant area.
1021 * @param pixelY The Y location at which you want to search for a vacant area.
1022 * @param spanX Horizontal span of the object.
1023 * @param spanY Vertical span of the object.
Michael Jurka6a1435d2010-09-27 17:35:12 -07001024 * @param ignoreView Considers space occupied by this view as unoccupied
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001025 * @param result Previously returned value to possibly recycle.
Michael Jurka6a1435d2010-09-27 17:35:12 -07001026 * @return The X, Y cell of a vacant area that can contain this object,
1027 * nearest the requested location.
1028 */
1029 int[] findNearestVacantArea(
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001030 int pixelX, int pixelY, int spanX, int spanY, View ignoreView, int[] result) {
Michael Jurkac6ee42e2010-09-30 12:04:50 -07001031 // mark space take by ignoreView as available (method checks if ignoreView is null)
1032 markCellsAsUnoccupiedForView(ignoreView);
1033
Jeff Sharkey70864282009-04-07 21:08:40 -07001034 // Keep track of best-scoring drop area
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001035 final int[] bestXY = result != null ? result : new int[2];
Jeff Sharkey70864282009-04-07 21:08:40 -07001036 double bestDistance = Double.MAX_VALUE;
Winson Chungaafa03c2010-06-11 17:34:16 -07001037
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001038 final int countX = mCountX;
1039 final int countY = mCountY;
1040 final boolean[][] occupied = mOccupied;
1041
1042 for (int x = 0; x < countX - (spanX - 1); x++) {
Michael Jurkac28de512010-08-13 11:27:44 -07001043 inner:
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001044 for (int y = 0; y < countY - (spanY - 1); y++) {
Michael Jurkac28de512010-08-13 11:27:44 -07001045 for (int i = 0; i < spanX; i++) {
1046 for (int j = 0; j < spanY; j++) {
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001047 if (occupied[x + i][y + j]) {
Michael Jurkac28de512010-08-13 11:27:44 -07001048 // small optimization: we can skip to below the row we just found
1049 // an occupied cell
1050 y += j;
1051 continue inner;
1052 }
1053 }
1054 }
1055 final int[] cellXY = mTmpCellXY;
1056 cellToPoint(x, y, cellXY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001057
Michael Jurkac28de512010-08-13 11:27:44 -07001058 double distance = Math.sqrt(Math.pow(cellXY[0] - pixelX, 2)
1059 + Math.pow(cellXY[1] - pixelY, 2));
1060 if (distance <= bestDistance) {
1061 bestDistance = distance;
1062 bestXY[0] = x;
1063 bestXY[1] = y;
1064 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001065 }
1066 }
Michael Jurkac6ee42e2010-09-30 12:04:50 -07001067 // re-mark space taken by ignoreView as occupied
1068 markCellsAsOccupiedForView(ignoreView);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001069
Winson Chungaafa03c2010-06-11 17:34:16 -07001070 // Return null if no suitable location found
Jeff Sharkey70864282009-04-07 21:08:40 -07001071 if (bestDistance < Double.MAX_VALUE) {
1072 return bestXY;
1073 } else {
1074 return null;
1075 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001076 }
Winson Chungaafa03c2010-06-11 17:34:16 -07001077
Michael Jurka0280c3b2010-09-17 15:00:07 -07001078 boolean existsEmptyCell() {
1079 return findCellForSpan(null, 1, 1);
1080 }
1081
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001082 /**
Michael Jurka0280c3b2010-09-17 15:00:07 -07001083 * Finds the upper-left coordinate of the first rectangle in the grid that can
1084 * hold a cell of the specified dimensions. If intersectX and intersectY are not -1,
1085 * then this method will only return coordinates for rectangles that contain the cell
1086 * (intersectX, intersectY)
1087 *
1088 * @param cellXY The array that will contain the position of a vacant cell if such a cell
1089 * can be found.
1090 * @param spanX The horizontal span of the cell we want to find.
1091 * @param spanY The vertical span of the cell we want to find.
1092 *
1093 * @return True if a vacant cell of the specified dimension was found, false otherwise.
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001094 */
Michael Jurka0280c3b2010-09-17 15:00:07 -07001095 boolean findCellForSpan(int[] cellXY, int spanX, int spanY) {
1096 return findCellForSpanThatIntersectsIgnoring(cellXY, spanX, spanY, -1, -1, null);
1097 }
1098
1099 /**
1100 * Like above, but ignores any cells occupied by the item "ignoreView"
1101 *
1102 * @param cellXY The array that will contain the position of a vacant cell if such a cell
1103 * can be found.
1104 * @param spanX The horizontal span of the cell we want to find.
1105 * @param spanY The vertical span of the cell we want to find.
1106 * @param ignoreView The home screen item we should treat as not occupying any space
1107 * @return
1108 */
1109 boolean findCellForSpanIgnoring(int[] cellXY, int spanX, int spanY, View ignoreView) {
1110 return findCellForSpanThatIntersectsIgnoring(cellXY, spanX, spanY, -1, -1, ignoreView);
1111 }
1112
1113 /**
1114 * Like above, but if intersectX and intersectY are not -1, then this method will try to
1115 * return coordinates for rectangles that contain the cell [intersectX, intersectY]
1116 *
1117 * @param spanX The horizontal span of the cell we want to find.
1118 * @param spanY The vertical span of the cell we want to find.
1119 * @param ignoreView The home screen item we should treat as not occupying any space
1120 * @param intersectX The X coordinate of the cell that we should try to overlap
1121 * @param intersectX The Y coordinate of the cell that we should try to overlap
1122 *
1123 * @return True if a vacant cell of the specified dimension was found, false otherwise.
1124 */
1125 boolean findCellForSpanThatIntersects(int[] cellXY, int spanX, int spanY,
1126 int intersectX, int intersectY) {
1127 return findCellForSpanThatIntersectsIgnoring(
1128 cellXY, spanX, spanY, intersectX, intersectY, null);
1129 }
1130
1131 /**
1132 * The superset of the above two methods
1133 */
1134 boolean findCellForSpanThatIntersectsIgnoring(int[] cellXY, int spanX, int spanY,
1135 int intersectX, int intersectY, View ignoreView) {
Michael Jurkac6ee42e2010-09-30 12:04:50 -07001136 // mark space take by ignoreView as available (method checks if ignoreView is null)
1137 markCellsAsUnoccupiedForView(ignoreView);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001138
Michael Jurka28750fb2010-09-24 17:43:49 -07001139 boolean foundCell = false;
Michael Jurka0280c3b2010-09-17 15:00:07 -07001140 while (true) {
1141 int startX = 0;
1142 if (intersectX >= 0) {
1143 startX = Math.max(startX, intersectX - (spanX - 1));
1144 }
1145 int endX = mCountX - (spanX - 1);
1146 if (intersectX >= 0) {
1147 endX = Math.min(endX, intersectX + (spanX - 1) + (spanX == 1 ? 1 : 0));
1148 }
1149 int startY = 0;
1150 if (intersectY >= 0) {
1151 startY = Math.max(startY, intersectY - (spanY - 1));
1152 }
1153 int endY = mCountY - (spanY - 1);
1154 if (intersectY >= 0) {
1155 endY = Math.min(endY, intersectY + (spanY - 1) + (spanY == 1 ? 1 : 0));
1156 }
1157
1158 for (int x = startX; x < endX; x++) {
1159 inner:
1160 for (int y = startY; y < endY; y++) {
1161 for (int i = 0; i < spanX; i++) {
1162 for (int j = 0; j < spanY; j++) {
1163 if (mOccupied[x + i][y + j]) {
1164 // small optimization: we can skip to below the row we just found
1165 // an occupied cell
1166 y += j;
1167 continue inner;
1168 }
1169 }
1170 }
1171 if (cellXY != null) {
1172 cellXY[0] = x;
1173 cellXY[1] = y;
1174 }
Michael Jurka28750fb2010-09-24 17:43:49 -07001175 foundCell = true;
1176 break;
Michael Jurka0280c3b2010-09-17 15:00:07 -07001177 }
1178 }
1179 if (intersectX == -1 && intersectY == -1) {
1180 break;
1181 } else {
1182 // if we failed to find anything, try again but without any requirements of
1183 // intersecting
1184 intersectX = -1;
1185 intersectY = -1;
1186 continue;
1187 }
1188 }
1189
Michael Jurkac6ee42e2010-09-30 12:04:50 -07001190 // re-mark space taken by ignoreView as occupied
1191 markCellsAsOccupiedForView(ignoreView);
Michael Jurka28750fb2010-09-24 17:43:49 -07001192 return foundCell;
Michael Jurka0280c3b2010-09-17 15:00:07 -07001193 }
1194
1195 /**
1196 * Called when drag has left this CellLayout or has been completed (successfully or not)
1197 */
1198 void onDragExit() {
Joe Onorato4be866d2010-10-10 11:26:02 -07001199 // This can actually be called when we aren't in a drag, e.g. when adding a new
1200 // item to this layout via the customize drawer.
1201 // Guard against that case.
1202 if (mDragging) {
1203 mDragging = false;
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001204
Joe Onorato4be866d2010-10-10 11:26:02 -07001205 // Fade out the drag indicators
1206 if (mCrosshairsAnimator != null) {
1207 mCrosshairsAnimator.animateOut();
1208 }
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001209 }
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -07001210
1211 // Invalidate the drag data
1212 mDragCell[0] = -1;
1213 mDragCell[1] = -1;
1214 mDragOutlineAnims[mDragOutlineCurrent].animateOut();
1215 mDragOutlineCurrent = (mDragOutlineCurrent + 1) % mDragOutlineAnims.length;
1216
1217 setHover(false);
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001218 }
1219
1220 /**
Winson Chungaafa03c2010-06-11 17:34:16 -07001221 * Mark a child as having been dropped.
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001222 * At the beginning of the drag operation, the child may have been on another
Patrick Dubroyce34a972010-10-19 10:34:32 -07001223 * screen, but it is re-parented before this method is called.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001224 *
1225 * @param child The child that is being dropped
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001226 */
Winson Chungaafa03c2010-06-11 17:34:16 -07001227 void onDropChild(View child) {
Romain Guyd94533d2009-08-17 10:01:15 -07001228 if (child != null) {
1229 LayoutParams lp = (LayoutParams) child.getLayoutParams();
Romain Guyd94533d2009-08-17 10:01:15 -07001230 lp.isDragging = false;
Romain Guy84f296c2009-11-04 15:00:44 -08001231 lp.dropped = true;
Patrick Dubroyce34a972010-10-19 10:34:32 -07001232 child.setVisibility(View.VISIBLE);
Romain Guyd94533d2009-08-17 10:01:15 -07001233 child.requestLayout();
Romain Guyd94533d2009-08-17 10:01:15 -07001234 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001235 }
1236
1237 void onDropAborted(View child) {
1238 if (child != null) {
Patrick Dubroyce34a972010-10-19 10:34:32 -07001239 LayoutParams lp = (LayoutParams) child.getLayoutParams();
1240 lp.isDragging = false;
1241 child.setVisibility(View.VISIBLE);
1242 animateChildIntoPosition(child);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001243 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001244 }
1245
1246 /**
1247 * Start dragging the specified child
Winson Chungaafa03c2010-06-11 17:34:16 -07001248 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001249 * @param child The child that is being dragged
1250 */
1251 void onDragChild(View child) {
1252 LayoutParams lp = (LayoutParams) child.getLayoutParams();
1253 lp.isDragging = true;
Patrick Dubroyce34a972010-10-19 10:34:32 -07001254 child.setVisibility(View.GONE);
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001255 }
1256
1257 /**
1258 * A drag event has begun over this layout.
1259 * It may have begun over this layout (in which case onDragChild is called first),
1260 * or it may have begun on another layout.
1261 */
1262 void onDragEnter(View dragView) {
Patrick Dubroyfe6bd872010-10-13 17:32:10 -07001263 if (!mDragging) {
Patrick Dubroyfe6bd872010-10-13 17:32:10 -07001264 // Fade in the drag indicators
1265 if (mCrosshairsAnimator != null) {
1266 mCrosshairsAnimator.animateIn();
1267 }
Joe Onorato4be866d2010-10-10 11:26:02 -07001268 }
1269 mDragging = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001270 }
Winson Chungaafa03c2010-06-11 17:34:16 -07001271
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001272 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001273 * Computes a bounding rectangle for a range of cells
Winson Chungaafa03c2010-06-11 17:34:16 -07001274 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001275 * @param cellX X coordinate of upper left corner expressed as a cell position
1276 * @param cellY Y coordinate of upper left corner expressed as a cell position
Winson Chungaafa03c2010-06-11 17:34:16 -07001277 * @param cellHSpan Width in cells
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001278 * @param cellVSpan Height in cells
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001279 * @param resultRect Rect into which to put the results
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001280 */
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001281 public void cellToRect(int cellX, int cellY, int cellHSpan, int cellVSpan, RectF resultRect) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001282 final int cellWidth = mCellWidth;
1283 final int cellHeight = mCellHeight;
1284 final int widthGap = mWidthGap;
1285 final int heightGap = mHeightGap;
Winson Chungaafa03c2010-06-11 17:34:16 -07001286
1287 final int hStartPadding = getLeftPadding();
1288 final int vStartPadding = getTopPadding();
1289
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001290 int width = cellHSpan * cellWidth + ((cellHSpan - 1) * widthGap);
1291 int height = cellVSpan * cellHeight + ((cellVSpan - 1) * heightGap);
1292
1293 int x = hStartPadding + cellX * (cellWidth + widthGap);
1294 int y = vStartPadding + cellY * (cellHeight + heightGap);
Winson Chungaafa03c2010-06-11 17:34:16 -07001295
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001296 resultRect.set(x, y, x + width, y + height);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001297 }
Winson Chungaafa03c2010-06-11 17:34:16 -07001298
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001299 /**
Winson Chungaafa03c2010-06-11 17:34:16 -07001300 * Computes the required horizontal and vertical cell spans to always
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001301 * fit the given rectangle.
Winson Chungaafa03c2010-06-11 17:34:16 -07001302 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001303 * @param width Width in pixels
1304 * @param height Height in pixels
Patrick Dubroy8f86ddc2010-07-16 13:55:32 -07001305 * @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 -08001306 */
Patrick Dubroy8f86ddc2010-07-16 13:55:32 -07001307 public int[] rectToCell(int width, int height, int[] result) {
Michael Jurka9987a5c2010-10-08 16:58:12 -07001308 return rectToCell(getResources(), width, height, result);
1309 }
1310
1311 public static int[] rectToCell(Resources resources, int width, int height, int[] result) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001312 // Always assume we're working with the smallest span to make sure we
1313 // reserve enough space in both orientations.
Joe Onorato79e56262009-09-21 15:23:04 -04001314 int actualWidth = resources.getDimensionPixelSize(R.dimen.workspace_cell_width);
1315 int actualHeight = resources.getDimensionPixelSize(R.dimen.workspace_cell_height);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001316 int smallerSize = Math.min(actualWidth, actualHeight);
Joe Onorato79e56262009-09-21 15:23:04 -04001317
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001318 // Always round up to next largest cell
1319 int spanX = (width + smallerSize) / smallerSize;
1320 int spanY = (height + smallerSize) / smallerSize;
Joe Onorato79e56262009-09-21 15:23:04 -04001321
Patrick Dubroy8f86ddc2010-07-16 13:55:32 -07001322 if (result == null) {
1323 return new int[] { spanX, spanY };
1324 }
1325 result[0] = spanX;
1326 result[1] = spanY;
1327 return result;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001328 }
1329
1330 /**
1331 * Find the first vacant cell, if there is one.
1332 *
1333 * @param vacant Holds the x and y coordinate of the vacant cell
1334 * @param spanX Horizontal cell span.
1335 * @param spanY Vertical cell span.
Winson Chungaafa03c2010-06-11 17:34:16 -07001336 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001337 * @return True if a vacant cell was found
1338 */
1339 public boolean getVacantCell(int[] vacant, int spanX, int spanY) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001340
Michael Jurka0280c3b2010-09-17 15:00:07 -07001341 return findVacantCell(vacant, spanX, spanY, mCountX, mCountY, mOccupied);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001342 }
1343
1344 static boolean findVacantCell(int[] vacant, int spanX, int spanY,
1345 int xCount, int yCount, boolean[][] occupied) {
1346
1347 for (int x = 0; x < xCount; x++) {
1348 for (int y = 0; y < yCount; y++) {
1349 boolean available = !occupied[x][y];
1350out: for (int i = x; i < x + spanX - 1 && x < xCount; i++) {
1351 for (int j = y; j < y + spanY - 1 && y < yCount; j++) {
1352 available = available && !occupied[i][j];
1353 if (!available) break out;
1354 }
1355 }
1356
1357 if (available) {
1358 vacant[0] = x;
1359 vacant[1] = y;
1360 return true;
1361 }
1362 }
1363 }
1364
1365 return false;
1366 }
1367
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001368 /**
1369 * Update the array of occupied cells (mOccupied), and return a flattened copy of the array.
1370 */
1371 boolean[] getOccupiedCellsFlattened() {
Adam Cohend22015c2010-07-26 22:02:18 -07001372 final int xCount = mCountX;
1373 final int yCount = mCountY;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001374 final boolean[][] occupied = mOccupied;
1375
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001376 final boolean[] flat = new boolean[xCount * yCount];
1377 for (int y = 0; y < yCount; y++) {
1378 for (int x = 0; x < xCount; x++) {
1379 flat[y * xCount + x] = occupied[x][y];
1380 }
1381 }
1382
1383 return flat;
1384 }
1385
Michael Jurka0280c3b2010-09-17 15:00:07 -07001386 private void clearOccupiedCells() {
1387 for (int x = 0; x < mCountX; x++) {
1388 for (int y = 0; y < mCountY; y++) {
1389 mOccupied[x][y] = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001390 }
1391 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001392 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001393
Michael Jurka0280c3b2010-09-17 15:00:07 -07001394 public void onMove(View view, int newCellX, int newCellY) {
1395 LayoutParams lp = (LayoutParams) view.getLayoutParams();
1396 markCellsAsUnoccupiedForView(view);
1397 markCellsForView(newCellX, newCellY, lp.cellHSpan, lp.cellVSpan, true);
1398 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001399
Michael Jurka0280c3b2010-09-17 15:00:07 -07001400 private void markCellsAsOccupiedForView(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, true);
1404 }
1405
1406 private void markCellsAsUnoccupiedForView(View view) {
Michael Jurkac6ee42e2010-09-30 12:04:50 -07001407 if (view == null || view.getParent() != this) return;
Michael Jurka0280c3b2010-09-17 15:00:07 -07001408 LayoutParams lp = (LayoutParams) view.getLayoutParams();
1409 markCellsForView(lp.cellX, lp.cellY, lp.cellHSpan, lp.cellVSpan, false);
1410 }
1411
1412 private void markCellsForView(int cellX, int cellY, int spanX, int spanY, boolean value) {
1413 for (int x = cellX; x < cellX + spanX && x < mCountX; x++) {
1414 for (int y = cellY; y < cellY + spanY && y < mCountY; y++) {
1415 mOccupied[x][y] = value;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001416 }
1417 }
1418 }
1419
1420 @Override
1421 public ViewGroup.LayoutParams generateLayoutParams(AttributeSet attrs) {
1422 return new CellLayout.LayoutParams(getContext(), attrs);
1423 }
1424
1425 @Override
1426 protected boolean checkLayoutParams(ViewGroup.LayoutParams p) {
1427 return p instanceof CellLayout.LayoutParams;
1428 }
1429
1430 @Override
1431 protected ViewGroup.LayoutParams generateLayoutParams(ViewGroup.LayoutParams p) {
1432 return new CellLayout.LayoutParams(p);
1433 }
1434
Winson Chungaafa03c2010-06-11 17:34:16 -07001435 public static class CellLayoutAnimationController extends LayoutAnimationController {
1436 public CellLayoutAnimationController(Animation animation, float delay) {
1437 super(animation, delay);
1438 }
1439
1440 @Override
1441 protected long getDelayForView(View view) {
1442 return (int) (Math.random() * 150);
1443 }
1444 }
1445
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001446 public static class LayoutParams extends ViewGroup.MarginLayoutParams {
1447 /**
1448 * Horizontal location of the item in the grid.
1449 */
1450 @ViewDebug.ExportedProperty
1451 public int cellX;
1452
1453 /**
1454 * Vertical location of the item in the grid.
1455 */
1456 @ViewDebug.ExportedProperty
1457 public int cellY;
1458
1459 /**
1460 * Number of cells spanned horizontally by the item.
1461 */
1462 @ViewDebug.ExportedProperty
1463 public int cellHSpan;
1464
1465 /**
1466 * Number of cells spanned vertically by the item.
1467 */
1468 @ViewDebug.ExportedProperty
1469 public int cellVSpan;
Winson Chungaafa03c2010-06-11 17:34:16 -07001470
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001471 /**
1472 * Is this item currently being dragged
1473 */
1474 public boolean isDragging;
1475
1476 // X coordinate of the view in the layout.
1477 @ViewDebug.ExportedProperty
1478 int x;
1479 // Y coordinate of the view in the layout.
1480 @ViewDebug.ExportedProperty
1481 int y;
1482
Patrick Dubroyce34a972010-10-19 10:34:32 -07001483 /**
1484 * The old X coordinate of this item, relative to its current parent.
1485 * Used to animate the item into its new position.
1486 */
1487 int oldX;
1488
1489 /**
1490 * The old Y coordinate of this item, relative to its current parent.
1491 * Used to animate the item into its new position.
1492 */
1493 int oldY;
1494
Romain Guy84f296c2009-11-04 15:00:44 -08001495 boolean dropped;
Romain Guyfcb9e712009-10-02 16:06:52 -07001496
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001497 public LayoutParams(Context c, AttributeSet attrs) {
1498 super(c, attrs);
1499 cellHSpan = 1;
1500 cellVSpan = 1;
1501 }
1502
1503 public LayoutParams(ViewGroup.LayoutParams source) {
1504 super(source);
1505 cellHSpan = 1;
1506 cellVSpan = 1;
1507 }
Winson Chungaafa03c2010-06-11 17:34:16 -07001508
1509 public LayoutParams(LayoutParams source) {
1510 super(source);
1511 this.cellX = source.cellX;
1512 this.cellY = source.cellY;
1513 this.cellHSpan = source.cellHSpan;
1514 this.cellVSpan = source.cellVSpan;
1515 }
1516
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001517 public LayoutParams(int cellX, int cellY, int cellHSpan, int cellVSpan) {
Romain Guy8f19cdd2010-01-08 15:07:00 -08001518 super(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001519 this.cellX = cellX;
1520 this.cellY = cellY;
1521 this.cellHSpan = cellHSpan;
1522 this.cellVSpan = cellVSpan;
1523 }
1524
1525 public void setup(int cellWidth, int cellHeight, int widthGap, int heightGap,
1526 int hStartPadding, int vStartPadding) {
Winson Chungaafa03c2010-06-11 17:34:16 -07001527
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001528 final int myCellHSpan = cellHSpan;
1529 final int myCellVSpan = cellVSpan;
1530 final int myCellX = cellX;
1531 final int myCellY = cellY;
Winson Chungaafa03c2010-06-11 17:34:16 -07001532
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001533 width = myCellHSpan * cellWidth + ((myCellHSpan - 1) * widthGap) -
1534 leftMargin - rightMargin;
1535 height = myCellVSpan * cellHeight + ((myCellVSpan - 1) * heightGap) -
1536 topMargin - bottomMargin;
1537
1538 x = hStartPadding + myCellX * (cellWidth + widthGap) + leftMargin;
1539 y = vStartPadding + myCellY * (cellHeight + heightGap) + topMargin;
1540 }
Winson Chungaafa03c2010-06-11 17:34:16 -07001541
1542 public String toString() {
1543 return "(" + this.cellX + ", " + this.cellY + ")";
1544 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001545 }
1546
Michael Jurka0280c3b2010-09-17 15:00:07 -07001547 // This class stores info for two purposes:
1548 // 1. When dragging items (mDragInfo in Workspace), we store the View, its cellX & cellY,
1549 // its spanX, spanY, and the screen it is on
1550 // 2. When long clicking on an empty cell in a CellLayout, we save information about the
1551 // cellX and cellY coordinates and which page was clicked. We then set this as a tag on
1552 // the CellLayout that was long clicked
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001553 static final class CellInfo implements ContextMenu.ContextMenuInfo {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001554 View cell;
Michael Jurkaa63c4522010-08-19 13:52:27 -07001555 int cellX = -1;
1556 int cellY = -1;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001557 int spanX;
1558 int spanY;
1559 int screen;
1560 boolean valid;
1561
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001562 @Override
1563 public String toString() {
Winson Chungaafa03c2010-06-11 17:34:16 -07001564 return "Cell[view=" + (cell == null ? "null" : cell.getClass())
1565 + ", x=" + cellX + ", y=" + cellY + "]";
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001566 }
1567 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001568}