blob: 98aad14ce8da5346b909cc85f9c133ee58a650a4 [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) {
Michael Jurkaf3ca3ab2010-10-20 17:08:24 -0700459 return addViewToCellLayout(child, index, childId, params, true);
460 }
461
462 public boolean addViewToCellLayout(
463 View child, int index, int childId, LayoutParams params, boolean markCells) {
Winson Chungaafa03c2010-06-11 17:34:16 -0700464 final LayoutParams lp = params;
465
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800466 // Generate an id for each view, this assumes we have at most 256x256 cells
467 // per workspace screen
Adam Cohend22015c2010-07-26 22:02:18 -0700468 if (lp.cellX >= 0 && lp.cellX <= mCountX - 1 && lp.cellY >= 0 && lp.cellY <= mCountY - 1) {
Winson Chungaafa03c2010-06-11 17:34:16 -0700469 // If the horizontal or vertical span is set to -1, it is taken to
470 // mean that it spans the extent of the CellLayout
Adam Cohend22015c2010-07-26 22:02:18 -0700471 if (lp.cellHSpan < 0) lp.cellHSpan = mCountX;
472 if (lp.cellVSpan < 0) lp.cellVSpan = mCountY;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800473
Winson Chungaafa03c2010-06-11 17:34:16 -0700474 child.setId(childId);
475
Michael Jurkadee05892010-07-27 10:01:56 -0700476 // We might be in the middle or end of shrinking/fading to a dimmed view
477 // Make sure this view's alpha is set the same as all the rest of the views
Michael Jurka5f1c5092010-09-03 14:15:02 -0700478 child.setAlpha(getAlpha());
Winson Chungaafa03c2010-06-11 17:34:16 -0700479 addView(child, index, lp);
Michael Jurkadee05892010-07-27 10:01:56 -0700480
Michael Jurkaf3ca3ab2010-10-20 17:08:24 -0700481 if (markCells) markCellsAsOccupiedForView(child);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700482
Winson Chungaafa03c2010-06-11 17:34:16 -0700483 return true;
484 }
485 return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800486 }
Michael Jurka3e7c7632010-10-02 16:01:03 -0700487 public void setAcceptsDrops(boolean acceptsDrops) {
488 if (mAcceptsDrops != acceptsDrops) {
489 mAcceptsDrops = acceptsDrops;
490 invalidate();
491 }
492 }
493
494 public boolean getAcceptsDrops() {
495 return mAcceptsDrops;
496 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800497
498 @Override
Michael Jurka0280c3b2010-09-17 15:00:07 -0700499 public void removeAllViews() {
500 clearOccupiedCells();
501 }
502
503 @Override
504 public void removeAllViewsInLayout() {
505 clearOccupiedCells();
506 }
507
Michael Jurkaf3ca3ab2010-10-20 17:08:24 -0700508 public void removeViewWithoutMarkingCells(View view) {
509 super.removeView(view);
510 }
511
Michael Jurka0280c3b2010-09-17 15:00:07 -0700512 @Override
513 public void removeView(View view) {
514 markCellsAsUnoccupiedForView(view);
515 super.removeView(view);
516 }
517
518 @Override
519 public void removeViewAt(int index) {
520 markCellsAsUnoccupiedForView(getChildAt(index));
521 super.removeViewAt(index);
522 }
523
524 @Override
525 public void removeViewInLayout(View view) {
526 markCellsAsUnoccupiedForView(view);
527 super.removeViewInLayout(view);
528 }
529
530 @Override
531 public void removeViews(int start, int count) {
532 for (int i = start; i < start + count; i++) {
533 markCellsAsUnoccupiedForView(getChildAt(i));
534 }
535 super.removeViews(start, count);
536 }
537
538 @Override
539 public void removeViewsInLayout(int start, int count) {
540 for (int i = start; i < start + count; i++) {
541 markCellsAsUnoccupiedForView(getChildAt(i));
542 }
543 super.removeViewsInLayout(start, count);
544 }
545
546 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800547 public void requestChildFocus(View child, View focused) {
548 super.requestChildFocus(child, focused);
549 if (child != null) {
550 Rect r = new Rect();
551 child.getDrawingRect(r);
552 requestRectangleOnScreen(r);
553 }
554 }
555
556 @Override
557 protected void onAttachedToWindow() {
558 super.onAttachedToWindow();
559 mCellInfo.screen = ((ViewGroup) getParent()).indexOfChild(this);
560 }
561
Michael Jurkaaf442092010-06-10 17:01:57 -0700562 public void setTagToCellInfoForPoint(int touchX, int touchY) {
563 final CellInfo cellInfo = mCellInfo;
564 final Rect frame = mRect;
565 final int x = touchX + mScrollX;
566 final int y = touchY + mScrollY;
567 final int count = getChildCount();
568
569 boolean found = false;
570 for (int i = count - 1; i >= 0; i--) {
571 final View child = getChildAt(i);
572
573 if ((child.getVisibility()) == VISIBLE || child.getAnimation() != null) {
574 child.getHitRect(frame);
575 if (frame.contains(x, y)) {
576 final LayoutParams lp = (LayoutParams) child.getLayoutParams();
577 cellInfo.cell = child;
578 cellInfo.cellX = lp.cellX;
579 cellInfo.cellY = lp.cellY;
580 cellInfo.spanX = lp.cellHSpan;
581 cellInfo.spanY = lp.cellVSpan;
582 cellInfo.valid = true;
583 found = true;
Michael Jurkaaf442092010-06-10 17:01:57 -0700584 break;
585 }
586 }
587 }
Winson Chungaafa03c2010-06-11 17:34:16 -0700588
Michael Jurkaaf442092010-06-10 17:01:57 -0700589 if (!found) {
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700590 final int cellXY[] = mTmpCellXY;
Michael Jurkaaf442092010-06-10 17:01:57 -0700591 pointToCellExact(x, y, cellXY);
592
Michael Jurkaaf442092010-06-10 17:01:57 -0700593 cellInfo.cell = null;
594 cellInfo.cellX = cellXY[0];
595 cellInfo.cellY = cellXY[1];
596 cellInfo.spanX = 1;
597 cellInfo.spanY = 1;
Michael Jurka0280c3b2010-09-17 15:00:07 -0700598 cellInfo.valid = cellXY[0] >= 0 && cellXY[1] >= 0 && cellXY[0] < mCountX &&
599 cellXY[1] < mCountY && !mOccupied[cellXY[0]][cellXY[1]];
Michael Jurkaaf442092010-06-10 17:01:57 -0700600 }
601 setTag(cellInfo);
602 }
603
Winson Chungaafa03c2010-06-11 17:34:16 -0700604
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800605 @Override
606 public boolean onInterceptTouchEvent(MotionEvent ev) {
Michael Jurkadee05892010-07-27 10:01:56 -0700607 if (mInterceptTouchListener != null && mInterceptTouchListener.onTouch(this, ev)) {
608 return true;
609 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800610 final int action = ev.getAction();
611 final CellInfo cellInfo = mCellInfo;
612
613 if (action == MotionEvent.ACTION_DOWN) {
Michael Jurkaaf442092010-06-10 17:01:57 -0700614 setTagToCellInfoForPoint((int) ev.getX(), (int) ev.getY());
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800615 } else if (action == MotionEvent.ACTION_UP) {
616 cellInfo.cell = null;
617 cellInfo.cellX = -1;
618 cellInfo.cellY = -1;
619 cellInfo.spanX = 0;
620 cellInfo.spanY = 0;
621 cellInfo.valid = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800622 setTag(cellInfo);
623 }
624
625 return false;
626 }
627
628 @Override
629 public CellInfo getTag() {
Michael Jurka0280c3b2010-09-17 15:00:07 -0700630 return (CellInfo) super.getTag();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800631 }
632
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700633 /**
634 * Check if the row 'y' is empty from columns 'left' to 'right', inclusive.
635 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800636 private static boolean isRowEmpty(int y, int left, int right, boolean[][] occupied) {
637 for (int x = left; x <= right; x++) {
638 if (occupied[x][y]) {
639 return false;
640 }
641 }
642 return true;
643 }
644
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800645 /**
Winson Chungaafa03c2010-06-11 17:34:16 -0700646 * Given a point, return the cell that strictly encloses that point
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800647 * @param x X coordinate of the point
648 * @param y Y coordinate of the point
649 * @param result Array of 2 ints to hold the x and y coordinate of the cell
650 */
651 void pointToCellExact(int x, int y, int[] result) {
Winson Chungaafa03c2010-06-11 17:34:16 -0700652 final int hStartPadding = getLeftPadding();
653 final int vStartPadding = getTopPadding();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800654
655 result[0] = (x - hStartPadding) / (mCellWidth + mWidthGap);
656 result[1] = (y - vStartPadding) / (mCellHeight + mHeightGap);
657
Adam Cohend22015c2010-07-26 22:02:18 -0700658 final int xAxis = mCountX;
659 final int yAxis = mCountY;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800660
661 if (result[0] < 0) result[0] = 0;
662 if (result[0] >= xAxis) result[0] = xAxis - 1;
663 if (result[1] < 0) result[1] = 0;
664 if (result[1] >= yAxis) result[1] = yAxis - 1;
665 }
Winson Chungaafa03c2010-06-11 17:34:16 -0700666
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800667 /**
668 * Given a point, return the cell that most closely encloses that point
669 * @param x X coordinate of the point
670 * @param y Y coordinate of the point
671 * @param result Array of 2 ints to hold the x and y coordinate of the cell
672 */
673 void pointToCellRounded(int x, int y, int[] result) {
674 pointToCellExact(x + (mCellWidth / 2), y + (mCellHeight / 2), result);
675 }
676
677 /**
678 * Given a cell coordinate, return the point that represents the upper left corner of that cell
Winson Chungaafa03c2010-06-11 17:34:16 -0700679 *
680 * @param cellX X coordinate of the cell
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800681 * @param cellY Y coordinate of the cell
Winson Chungaafa03c2010-06-11 17:34:16 -0700682 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800683 * @param result Array of 2 ints to hold the x and y coordinate of the point
684 */
685 void cellToPoint(int cellX, int cellY, int[] result) {
Winson Chungaafa03c2010-06-11 17:34:16 -0700686 final int hStartPadding = getLeftPadding();
687 final int vStartPadding = getTopPadding();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800688
689 result[0] = hStartPadding + cellX * (mCellWidth + mWidthGap);
690 result[1] = vStartPadding + cellY * (mCellHeight + mHeightGap);
691 }
692
Romain Guy84f296c2009-11-04 15:00:44 -0800693 int getCellWidth() {
694 return mCellWidth;
695 }
696
697 int getCellHeight() {
698 return mCellHeight;
699 }
700
Romain Guy1a304a12009-11-10 00:02:32 -0800701 int getLeftPadding() {
Winson Chungaafa03c2010-06-11 17:34:16 -0700702 return mLeftPadding;
Romain Guy1a304a12009-11-10 00:02:32 -0800703 }
704
705 int getTopPadding() {
Winson Chungaafa03c2010-06-11 17:34:16 -0700706 return mTopPadding;
Romain Guy1a304a12009-11-10 00:02:32 -0800707 }
708
709 int getRightPadding() {
Winson Chungaafa03c2010-06-11 17:34:16 -0700710 return mRightPadding;
Romain Guy1a304a12009-11-10 00:02:32 -0800711 }
712
713 int getBottomPadding() {
Winson Chungaafa03c2010-06-11 17:34:16 -0700714 return mBottomPadding;
Romain Guy1a304a12009-11-10 00:02:32 -0800715 }
716
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800717 @Override
718 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
719 // TODO: currently ignoring padding
Winson Chungaafa03c2010-06-11 17:34:16 -0700720
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800721 int widthSpecMode = MeasureSpec.getMode(widthMeasureSpec);
Winson Chungaafa03c2010-06-11 17:34:16 -0700722 int widthSpecSize = MeasureSpec.getSize(widthMeasureSpec);
723
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800724 int heightSpecMode = MeasureSpec.getMode(heightMeasureSpec);
725 int heightSpecSize = MeasureSpec.getSize(heightMeasureSpec);
Winson Chungaafa03c2010-06-11 17:34:16 -0700726
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800727 if (widthSpecMode == MeasureSpec.UNSPECIFIED || heightSpecMode == MeasureSpec.UNSPECIFIED) {
728 throw new RuntimeException("CellLayout cannot have UNSPECIFIED dimensions");
729 }
730
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800731 final int cellWidth = mCellWidth;
732 final int cellHeight = mCellHeight;
733
Adam Cohend22015c2010-07-26 22:02:18 -0700734 int numWidthGaps = mCountX - 1;
735 int numHeightGaps = mCountY - 1;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800736
Michael Jurka0280c3b2010-09-17 15:00:07 -0700737 int vSpaceLeft = heightSpecSize - mTopPadding - mBottomPadding - (cellHeight * mCountY);
Adam Cohend22015c2010-07-26 22:02:18 -0700738 mHeightGap = vSpaceLeft / numHeightGaps;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800739
Michael Jurka0280c3b2010-09-17 15:00:07 -0700740 int hSpaceLeft = widthSpecSize - mLeftPadding - mRightPadding - (cellWidth * mCountX);
Adam Cohend22015c2010-07-26 22:02:18 -0700741 mWidthGap = hSpaceLeft / numWidthGaps;
Winson Chungaafa03c2010-06-11 17:34:16 -0700742
Michael Jurka5f1c5092010-09-03 14:15:02 -0700743 // center it around the min gaps
744 int minGap = Math.min(mWidthGap, mHeightGap);
745 mWidthGap = mHeightGap = minGap;
746
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800747 int count = getChildCount();
748
749 for (int i = 0; i < count; i++) {
750 View child = getChildAt(i);
751 LayoutParams lp = (LayoutParams) child.getLayoutParams();
Winson Chungaafa03c2010-06-11 17:34:16 -0700752 lp.setup(cellWidth, cellHeight, mWidthGap, mHeightGap,
753 mLeftPadding, mTopPadding);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800754
Michael Jurka0280c3b2010-09-17 15:00:07 -0700755 int childWidthMeasureSpec = MeasureSpec.makeMeasureSpec(lp.width, MeasureSpec.EXACTLY);
Winson Chungaafa03c2010-06-11 17:34:16 -0700756 int childheightMeasureSpec = MeasureSpec.makeMeasureSpec(lp.height,
757 MeasureSpec.EXACTLY);
758
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800759 child.measure(childWidthMeasureSpec, childheightMeasureSpec);
760 }
Michael Jurka5f1c5092010-09-03 14:15:02 -0700761 if (widthSpecMode == MeasureSpec.AT_MOST) {
762 int newWidth = mLeftPadding + mRightPadding + (mCountX * cellWidth) +
763 ((mCountX - 1) * minGap);
764 int newHeight = mTopPadding + mBottomPadding + (mCountY * cellHeight) +
765 ((mCountY - 1) * minGap);
766 setMeasuredDimension(newWidth, newHeight);
767 } else if (widthSpecMode == MeasureSpec.EXACTLY) {
768 setMeasuredDimension(widthSpecSize, heightSpecSize);
769 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800770 }
771
Patrick Dubroyce34a972010-10-19 10:34:32 -0700772 /**
773 * Animate a child of this CellLayout into its current layout position.
774 * The position to animate from is given by the oldX and oldY values in its LayoutParams.
775 */
776 private void animateChildIntoPosition(final View child) {
777 final Resources res = getResources();
778 final ObjectAnimator anim = mDropAnim;
779 final CellLayout.LayoutParams lp = (CellLayout.LayoutParams) child.getLayoutParams();
780 final float startX = lp.oldX - lp.x;
781 final float startY = lp.oldY - lp.y;
782
783 // Calculate the duration of the animation based on the object's distance
784 final float dist = (float) Math.sqrt(startX*startX + startY*startY);
785 final float maxDist = (float) res.getInteger(R.integer.config_dropAnimMaxDist);
786 final int duration = (int) (res.getInteger(R.integer.config_dropAnimMaxDuration)
787 * mEaseOutInterpolator.getInterpolation(dist / maxDist));
788
789 anim.cancel(); // Make sure it's not already running
790 anim.setDuration(duration);
791 anim.setTarget(child);
792 anim.setPropertyName("translationX");
793 anim.setFloatValues(startX, 0);
794
795 anim.removeAllUpdateListeners();
796 anim.addUpdateListener(new AnimatorUpdateListener() {
797 public void onAnimationUpdate(ValueAnimator animation) {
798 // Set the value of translationY based on the current x value
799 final float translationX = (Float) anim.getAnimatedValue();
800 child.setTranslationY((startY / startX) * translationX);
801 }
802 });
803 anim.start();
804 }
805
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800806 @Override
Michael Jurka28750fb2010-09-24 17:43:49 -0700807 protected void onLayout(boolean changed, int l, int t, int r, int b) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800808 int count = getChildCount();
809
810 for (int i = 0; i < count; i++) {
Patrick Dubroyce34a972010-10-19 10:34:32 -0700811 final View child = getChildAt(i);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800812 if (child.getVisibility() != GONE) {
813
814 CellLayout.LayoutParams lp = (CellLayout.LayoutParams) child.getLayoutParams();
815
816 int childLeft = lp.x;
817 int childTop = lp.y;
818 child.layout(childLeft, childTop, childLeft + lp.width, childTop + lp.height);
Romain Guy84f296c2009-11-04 15:00:44 -0800819
820 if (lp.dropped) {
821 lp.dropped = false;
822
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700823 final int[] cellXY = mTmpCellXY;
Romain Guy06762ab2010-01-25 16:51:08 -0800824 getLocationOnScreen(cellXY);
Romain Guy84f296c2009-11-04 15:00:44 -0800825 mWallpaperManager.sendWallpaperCommand(getWindowToken(), "android.home.drop",
Romain Guy06762ab2010-01-25 16:51:08 -0800826 cellXY[0] + childLeft + lp.width / 2,
827 cellXY[1] + childTop + lp.height / 2, 0, null);
Patrick Dubroyce34a972010-10-19 10:34:32 -0700828
829 animateChildIntoPosition(child);
Romain Guy84f296c2009-11-04 15:00:44 -0800830 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800831 }
832 }
833 }
834
835 @Override
Michael Jurkadee05892010-07-27 10:01:56 -0700836 protected void onSizeChanged(int w, int h, int oldw, int oldh) {
837 super.onSizeChanged(w, h, oldw, oldh);
Michael Jurka18014792010-10-14 09:01:34 -0700838 mBackgroundRect.set(0, 0, w, h);
839 updateHoverRect();
Michael Jurkadee05892010-07-27 10:01:56 -0700840 }
841
842 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800843 protected void setChildrenDrawingCacheEnabled(boolean enabled) {
844 final int count = getChildCount();
845 for (int i = 0; i < count; i++) {
846 final View view = getChildAt(i);
847 view.setDrawingCacheEnabled(enabled);
848 // Update the drawing caches
Adam Powellfefa0ce2010-05-03 10:23:50 -0700849 view.buildDrawingCache(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800850 }
851 }
852
853 @Override
854 protected void setChildrenDrawnWithCacheEnabled(boolean enabled) {
855 super.setChildrenDrawnWithCacheEnabled(enabled);
856 }
857
Michael Jurka5f1c5092010-09-03 14:15:02 -0700858 public float getBackgroundAlpha() {
859 return mBackgroundAlpha;
Michael Jurkadee05892010-07-27 10:01:56 -0700860 }
861
Michael Jurka5f1c5092010-09-03 14:15:02 -0700862 public void setBackgroundAlpha(float alpha) {
863 mBackgroundAlpha = alpha;
Michael Jurka0142d492010-08-25 17:46:15 -0700864 invalidate();
Michael Jurkadee05892010-07-27 10:01:56 -0700865 }
866
Michael Jurka5f1c5092010-09-03 14:15:02 -0700867 // Need to return true to let the view system know we know how to handle alpha-- this is
868 // because when our children have an alpha of 0.0f, they are still rendering their "dimmed"
869 // versions
870 @Override
871 protected boolean onSetAlpha(int alpha) {
872 return true;
873 }
874
875 public void setAlpha(float alpha) {
876 setChildrenAlpha(alpha);
877 super.setAlpha(alpha);
878 }
879
Michael Jurkadee05892010-07-27 10:01:56 -0700880 private void setChildrenAlpha(float alpha) {
Michael Jurka0142d492010-08-25 17:46:15 -0700881 final int childCount = getChildCount();
882 for (int i = 0; i < childCount; i++) {
Michael Jurkadee05892010-07-27 10:01:56 -0700883 getChildAt(i).setAlpha(alpha);
884 }
885 }
886
Michael Jurka0280c3b2010-09-17 15:00:07 -0700887 private boolean isVacantIgnoring(
888 int originX, int originY, int spanX, int spanY, View ignoreView) {
889 if (ignoreView != null) {
890 markCellsAsUnoccupiedForView(ignoreView);
891 }
Michael Jurka28750fb2010-09-24 17:43:49 -0700892 boolean isVacant = true;
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700893 for (int i = 0; i < spanY; i++) {
894 if (!isRowEmpty(originY + i, originX, originX + spanX - 1, mOccupied)) {
Michael Jurka28750fb2010-09-24 17:43:49 -0700895 isVacant = false;
896 break;
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700897 }
898 }
Michael Jurka0280c3b2010-09-17 15:00:07 -0700899 if (ignoreView != null) {
900 markCellsAsOccupiedForView(ignoreView);
901 }
Michael Jurka28750fb2010-09-24 17:43:49 -0700902 return isVacant;
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700903 }
904
Michael Jurka0280c3b2010-09-17 15:00:07 -0700905 private boolean isVacant(int originX, int originY, int spanX, int spanY) {
906 return isVacantIgnoring(originX, originY, spanX, spanY, null);
907 }
908
Patrick Dubroy440c3602010-07-13 17:50:32 -0700909 public View getChildAt(int x, int y) {
910 final int count = getChildCount();
911 for (int i = 0; i < count; i++) {
912 View child = getChildAt(i);
913 LayoutParams lp = (LayoutParams) child.getLayoutParams();
914
915 if ((lp.cellX <= x) && (x < lp.cellX + lp.cellHSpan) &&
916 (lp.cellY <= y) && (y < lp.cellY + lp.cellHSpan)) {
917 return child;
918 }
919 }
920 return null;
921 }
922
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700923 /**
Patrick Dubroy8f86ddc2010-07-16 13:55:32 -0700924 * Estimate the size that a child with the given dimensions will take in the layout.
925 */
926 void estimateChildSize(int minWidth, int minHeight, int[] result) {
927 // Assuming it's placed at 0, 0, find where the bottom right cell will land
928 rectToCell(minWidth, minHeight, result);
929
930 // Then figure out the rect it will occupy
931 cellToRect(0, 0, result[0], result[1], mRectF);
932 result[0] = (int)mRectF.width();
933 result[1] = (int)mRectF.height();
934 }
935
936 /**
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700937 * Estimate where the top left cell of the dragged item will land if it is dropped.
938 *
939 * @param originX The X value of the top left corner of the item
940 * @param originY The Y value of the top left corner of the item
941 * @param spanX The number of horizontal cells that the item spans
942 * @param spanY The number of vertical cells that the item spans
943 * @param result The estimated drop cell X and Y.
944 */
945 void estimateDropCell(int originX, int originY, int spanX, int spanY, int[] result) {
Adam Cohend22015c2010-07-26 22:02:18 -0700946 final int countX = mCountX;
947 final int countY = mCountY;
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700948
Michael Jurkaa63c4522010-08-19 13:52:27 -0700949 // pointToCellRounded takes the top left of a cell but will pad that with
950 // cellWidth/2 and cellHeight/2 when finding the matching cell
951 pointToCellRounded(originX, originY, result);
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700952
953 // If the item isn't fully on this screen, snap to the edges
954 int rightOverhang = result[0] + spanX - countX;
955 if (rightOverhang > 0) {
956 result[0] -= rightOverhang; // Snap to right
957 }
958 result[0] = Math.max(0, result[0]); // Snap to left
959 int bottomOverhang = result[1] + spanY - countY;
960 if (bottomOverhang > 0) {
961 result[1] -= bottomOverhang; // Snap to bottom
962 }
963 result[1] = Math.max(0, result[1]); // Snap to top
964 }
965
Joe Onorato4be866d2010-10-10 11:26:02 -0700966 void visualizeDropLocation(
967 View v, Bitmap dragOutline, int originX, int originY, int spanX, int spanY) {
968
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -0700969 final int oldDragCellX = mDragCell[0];
970 final int oldDragCellY = mDragCell[1];
Joe Onorato4be866d2010-10-10 11:26:02 -0700971 final int[] nearest = findNearestVacantArea(originX, originY, spanX, spanY, v, mDragCell);
972 mDragCenter.set(originX + (v.getWidth() / 2), originY + (v.getHeight() / 2));
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700973
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -0700974 if (nearest != null && (nearest[0] != oldDragCellX || nearest[1] != oldDragCellY)) {
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700975 // Find the top left corner of the rect the object will occupy
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700976 final int[] topLeft = mTmpPoint;
977 cellToPoint(nearest[0], nearest[1], topLeft);
978
Joe Onorato4be866d2010-10-10 11:26:02 -0700979 int left = topLeft[0];
980 int top = topLeft[1];
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700981
Joe Onorato4be866d2010-10-10 11:26:02 -0700982 if (v.getParent() instanceof CellLayout) {
983 LayoutParams lp = (LayoutParams) v.getLayoutParams();
984 left += lp.leftMargin;
985 top += lp.topMargin;
986 }
Winson Chung150fbab2010-09-29 17:14:26 -0700987
Joe Onorato4be866d2010-10-10 11:26:02 -0700988 // Offsets due to the size difference between the View and the dragOutline
989 left += (v.getWidth() - dragOutline.getWidth()) / 2;
990 top += (v.getHeight() - dragOutline.getHeight()) / 2;
Winson Chung150fbab2010-09-29 17:14:26 -0700991
Joe Onorato4be866d2010-10-10 11:26:02 -0700992 final int oldIndex = mDragOutlineCurrent;
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -0700993 mDragOutlineAnims[oldIndex].animateOut();
994 mDragOutlineCurrent = (oldIndex + 1) % mDragOutlines.length;
Winson Chung150fbab2010-09-29 17:14:26 -0700995
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -0700996 mDragOutlines[mDragOutlineCurrent].set(left, top);
997 mDragOutlineAnims[mDragOutlineCurrent].setTag(dragOutline);
998 mDragOutlineAnims[mDragOutlineCurrent].animateIn();
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700999 }
Patrick Dubroy49250ad2010-10-08 15:33:52 -07001000
1001 // If we are drawing crosshairs, the entire CellLayout needs to be invalidated
1002 if (mCrosshairsDrawable != null) {
1003 invalidate();
1004 }
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001005 }
1006
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001007 /**
Jeff Sharkey70864282009-04-07 21:08:40 -07001008 * Find a vacant area that will fit the given bounds nearest the requested
1009 * cell location. Uses Euclidean distance to score multiple vacant areas.
Winson Chungaafa03c2010-06-11 17:34:16 -07001010 *
Romain Guy51afc022009-05-04 18:03:43 -07001011 * @param pixelX The X location at which you want to search for a vacant area.
1012 * @param pixelY The Y location at which you want to search for a vacant area.
Jeff Sharkey70864282009-04-07 21:08:40 -07001013 * @param spanX Horizontal span of the object.
1014 * @param spanY Vertical span of the object.
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001015 * @param result Array in which to place the result, or null (in which case a new array will
1016 * be allocated)
Jeff Sharkey70864282009-04-07 21:08:40 -07001017 * @return The X, Y cell of a vacant area that can contain this object,
1018 * nearest the requested location.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001019 */
Michael Jurka6a1435d2010-09-27 17:35:12 -07001020 int[] findNearestVacantArea(
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001021 int pixelX, int pixelY, int spanX, int spanY, int[] result) {
1022 return findNearestVacantArea(pixelX, pixelY, spanX, spanY, null, result);
Michael Jurka6a1435d2010-09-27 17:35:12 -07001023 }
Winson Chungaafa03c2010-06-11 17:34:16 -07001024
Michael Jurka6a1435d2010-09-27 17:35:12 -07001025 /**
1026 * Find a vacant area that will fit the given bounds nearest the requested
1027 * cell location. Uses Euclidean distance to score multiple vacant areas.
1028 *
1029 * @param pixelX The X location at which you want to search for a vacant area.
1030 * @param pixelY The Y location at which you want to search for a vacant area.
1031 * @param spanX Horizontal span of the object.
1032 * @param spanY Vertical span of the object.
Michael Jurka6a1435d2010-09-27 17:35:12 -07001033 * @param ignoreView Considers space occupied by this view as unoccupied
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001034 * @param result Previously returned value to possibly recycle.
Michael Jurka6a1435d2010-09-27 17:35:12 -07001035 * @return The X, Y cell of a vacant area that can contain this object,
1036 * nearest the requested location.
1037 */
1038 int[] findNearestVacantArea(
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001039 int pixelX, int pixelY, int spanX, int spanY, View ignoreView, int[] result) {
Michael Jurkac6ee42e2010-09-30 12:04:50 -07001040 // mark space take by ignoreView as available (method checks if ignoreView is null)
1041 markCellsAsUnoccupiedForView(ignoreView);
1042
Jeff Sharkey70864282009-04-07 21:08:40 -07001043 // Keep track of best-scoring drop area
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001044 final int[] bestXY = result != null ? result : new int[2];
Jeff Sharkey70864282009-04-07 21:08:40 -07001045 double bestDistance = Double.MAX_VALUE;
Winson Chungaafa03c2010-06-11 17:34:16 -07001046
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001047 final int countX = mCountX;
1048 final int countY = mCountY;
1049 final boolean[][] occupied = mOccupied;
1050
1051 for (int x = 0; x < countX - (spanX - 1); x++) {
Michael Jurkac28de512010-08-13 11:27:44 -07001052 inner:
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001053 for (int y = 0; y < countY - (spanY - 1); y++) {
Michael Jurkac28de512010-08-13 11:27:44 -07001054 for (int i = 0; i < spanX; i++) {
1055 for (int j = 0; j < spanY; j++) {
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001056 if (occupied[x + i][y + j]) {
Michael Jurkac28de512010-08-13 11:27:44 -07001057 // small optimization: we can skip to below the row we just found
1058 // an occupied cell
1059 y += j;
1060 continue inner;
1061 }
1062 }
1063 }
1064 final int[] cellXY = mTmpCellXY;
1065 cellToPoint(x, y, cellXY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001066
Michael Jurkac28de512010-08-13 11:27:44 -07001067 double distance = Math.sqrt(Math.pow(cellXY[0] - pixelX, 2)
1068 + Math.pow(cellXY[1] - pixelY, 2));
1069 if (distance <= bestDistance) {
1070 bestDistance = distance;
1071 bestXY[0] = x;
1072 bestXY[1] = y;
1073 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001074 }
1075 }
Michael Jurkac6ee42e2010-09-30 12:04:50 -07001076 // re-mark space taken by ignoreView as occupied
1077 markCellsAsOccupiedForView(ignoreView);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001078
Winson Chungaafa03c2010-06-11 17:34:16 -07001079 // Return null if no suitable location found
Jeff Sharkey70864282009-04-07 21:08:40 -07001080 if (bestDistance < Double.MAX_VALUE) {
1081 return bestXY;
1082 } else {
1083 return null;
1084 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001085 }
Winson Chungaafa03c2010-06-11 17:34:16 -07001086
Michael Jurka0280c3b2010-09-17 15:00:07 -07001087 boolean existsEmptyCell() {
1088 return findCellForSpan(null, 1, 1);
1089 }
1090
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001091 /**
Michael Jurka0280c3b2010-09-17 15:00:07 -07001092 * Finds the upper-left coordinate of the first rectangle in the grid that can
1093 * hold a cell of the specified dimensions. If intersectX and intersectY are not -1,
1094 * then this method will only return coordinates for rectangles that contain the cell
1095 * (intersectX, intersectY)
1096 *
1097 * @param cellXY The array that will contain the position of a vacant cell if such a cell
1098 * can be found.
1099 * @param spanX The horizontal span of the cell we want to find.
1100 * @param spanY The vertical span of the cell we want to find.
1101 *
1102 * @return True if a vacant cell of the specified dimension was found, false otherwise.
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001103 */
Michael Jurka0280c3b2010-09-17 15:00:07 -07001104 boolean findCellForSpan(int[] cellXY, int spanX, int spanY) {
1105 return findCellForSpanThatIntersectsIgnoring(cellXY, spanX, spanY, -1, -1, null);
1106 }
1107
1108 /**
1109 * Like above, but ignores any cells occupied by the item "ignoreView"
1110 *
1111 * @param cellXY The array that will contain the position of a vacant cell if such a cell
1112 * can be found.
1113 * @param spanX The horizontal span of the cell we want to find.
1114 * @param spanY The vertical span of the cell we want to find.
1115 * @param ignoreView The home screen item we should treat as not occupying any space
1116 * @return
1117 */
1118 boolean findCellForSpanIgnoring(int[] cellXY, int spanX, int spanY, View ignoreView) {
1119 return findCellForSpanThatIntersectsIgnoring(cellXY, spanX, spanY, -1, -1, ignoreView);
1120 }
1121
1122 /**
1123 * Like above, but if intersectX and intersectY are not -1, then this method will try to
1124 * return coordinates for rectangles that contain the cell [intersectX, intersectY]
1125 *
1126 * @param spanX The horizontal span of the cell we want to find.
1127 * @param spanY The vertical span of the cell we want to find.
1128 * @param ignoreView The home screen item we should treat as not occupying any space
1129 * @param intersectX The X coordinate of the cell that we should try to overlap
1130 * @param intersectX The Y coordinate of the cell that we should try to overlap
1131 *
1132 * @return True if a vacant cell of the specified dimension was found, false otherwise.
1133 */
1134 boolean findCellForSpanThatIntersects(int[] cellXY, int spanX, int spanY,
1135 int intersectX, int intersectY) {
1136 return findCellForSpanThatIntersectsIgnoring(
1137 cellXY, spanX, spanY, intersectX, intersectY, null);
1138 }
1139
1140 /**
1141 * The superset of the above two methods
1142 */
1143 boolean findCellForSpanThatIntersectsIgnoring(int[] cellXY, int spanX, int spanY,
1144 int intersectX, int intersectY, View ignoreView) {
Michael Jurkac6ee42e2010-09-30 12:04:50 -07001145 // mark space take by ignoreView as available (method checks if ignoreView is null)
1146 markCellsAsUnoccupiedForView(ignoreView);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001147
Michael Jurka28750fb2010-09-24 17:43:49 -07001148 boolean foundCell = false;
Michael Jurka0280c3b2010-09-17 15:00:07 -07001149 while (true) {
1150 int startX = 0;
1151 if (intersectX >= 0) {
1152 startX = Math.max(startX, intersectX - (spanX - 1));
1153 }
1154 int endX = mCountX - (spanX - 1);
1155 if (intersectX >= 0) {
1156 endX = Math.min(endX, intersectX + (spanX - 1) + (spanX == 1 ? 1 : 0));
1157 }
1158 int startY = 0;
1159 if (intersectY >= 0) {
1160 startY = Math.max(startY, intersectY - (spanY - 1));
1161 }
1162 int endY = mCountY - (spanY - 1);
1163 if (intersectY >= 0) {
1164 endY = Math.min(endY, intersectY + (spanY - 1) + (spanY == 1 ? 1 : 0));
1165 }
1166
1167 for (int x = startX; x < endX; x++) {
1168 inner:
1169 for (int y = startY; y < endY; y++) {
1170 for (int i = 0; i < spanX; i++) {
1171 for (int j = 0; j < spanY; j++) {
1172 if (mOccupied[x + i][y + j]) {
1173 // small optimization: we can skip to below the row we just found
1174 // an occupied cell
1175 y += j;
1176 continue inner;
1177 }
1178 }
1179 }
1180 if (cellXY != null) {
1181 cellXY[0] = x;
1182 cellXY[1] = y;
1183 }
Michael Jurka28750fb2010-09-24 17:43:49 -07001184 foundCell = true;
1185 break;
Michael Jurka0280c3b2010-09-17 15:00:07 -07001186 }
1187 }
1188 if (intersectX == -1 && intersectY == -1) {
1189 break;
1190 } else {
1191 // if we failed to find anything, try again but without any requirements of
1192 // intersecting
1193 intersectX = -1;
1194 intersectY = -1;
1195 continue;
1196 }
1197 }
1198
Michael Jurkac6ee42e2010-09-30 12:04:50 -07001199 // re-mark space taken by ignoreView as occupied
1200 markCellsAsOccupiedForView(ignoreView);
Michael Jurka28750fb2010-09-24 17:43:49 -07001201 return foundCell;
Michael Jurka0280c3b2010-09-17 15:00:07 -07001202 }
1203
1204 /**
1205 * Called when drag has left this CellLayout or has been completed (successfully or not)
1206 */
1207 void onDragExit() {
Joe Onorato4be866d2010-10-10 11:26:02 -07001208 // This can actually be called when we aren't in a drag, e.g. when adding a new
1209 // item to this layout via the customize drawer.
1210 // Guard against that case.
1211 if (mDragging) {
1212 mDragging = false;
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001213
Joe Onorato4be866d2010-10-10 11:26:02 -07001214 // Fade out the drag indicators
1215 if (mCrosshairsAnimator != null) {
1216 mCrosshairsAnimator.animateOut();
1217 }
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001218 }
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -07001219
1220 // Invalidate the drag data
1221 mDragCell[0] = -1;
1222 mDragCell[1] = -1;
1223 mDragOutlineAnims[mDragOutlineCurrent].animateOut();
1224 mDragOutlineCurrent = (mDragOutlineCurrent + 1) % mDragOutlineAnims.length;
1225
1226 setHover(false);
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001227 }
1228
1229 /**
Winson Chungaafa03c2010-06-11 17:34:16 -07001230 * Mark a child as having been dropped.
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001231 * At the beginning of the drag operation, the child may have been on another
Patrick Dubroyce34a972010-10-19 10:34:32 -07001232 * screen, but it is re-parented before this method is called.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001233 *
1234 * @param child The child that is being dropped
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001235 */
Winson Chungaafa03c2010-06-11 17:34:16 -07001236 void onDropChild(View child) {
Romain Guyd94533d2009-08-17 10:01:15 -07001237 if (child != null) {
1238 LayoutParams lp = (LayoutParams) child.getLayoutParams();
Romain Guyd94533d2009-08-17 10:01:15 -07001239 lp.isDragging = false;
Romain Guy84f296c2009-11-04 15:00:44 -08001240 lp.dropped = true;
Patrick Dubroyce34a972010-10-19 10:34:32 -07001241 child.setVisibility(View.VISIBLE);
Romain Guyd94533d2009-08-17 10:01:15 -07001242 child.requestLayout();
Romain Guyd94533d2009-08-17 10:01:15 -07001243 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001244 }
1245
1246 void onDropAborted(View child) {
1247 if (child != null) {
Patrick Dubroyce34a972010-10-19 10:34:32 -07001248 LayoutParams lp = (LayoutParams) child.getLayoutParams();
1249 lp.isDragging = false;
1250 child.setVisibility(View.VISIBLE);
1251 animateChildIntoPosition(child);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001252 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001253 }
1254
1255 /**
1256 * Start dragging the specified child
Winson Chungaafa03c2010-06-11 17:34:16 -07001257 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001258 * @param child The child that is being dragged
1259 */
1260 void onDragChild(View child) {
1261 LayoutParams lp = (LayoutParams) child.getLayoutParams();
1262 lp.isDragging = true;
Patrick Dubroyce34a972010-10-19 10:34:32 -07001263 child.setVisibility(View.GONE);
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001264 }
1265
1266 /**
1267 * A drag event has begun over this layout.
1268 * It may have begun over this layout (in which case onDragChild is called first),
1269 * or it may have begun on another layout.
1270 */
1271 void onDragEnter(View dragView) {
Patrick Dubroyfe6bd872010-10-13 17:32:10 -07001272 if (!mDragging) {
Patrick Dubroyfe6bd872010-10-13 17:32:10 -07001273 // Fade in the drag indicators
1274 if (mCrosshairsAnimator != null) {
1275 mCrosshairsAnimator.animateIn();
1276 }
Joe Onorato4be866d2010-10-10 11:26:02 -07001277 }
1278 mDragging = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001279 }
Winson Chungaafa03c2010-06-11 17:34:16 -07001280
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001281 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001282 * Computes a bounding rectangle for a range of cells
Winson Chungaafa03c2010-06-11 17:34:16 -07001283 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001284 * @param cellX X coordinate of upper left corner expressed as a cell position
1285 * @param cellY Y coordinate of upper left corner expressed as a cell position
Winson Chungaafa03c2010-06-11 17:34:16 -07001286 * @param cellHSpan Width in cells
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001287 * @param cellVSpan Height in cells
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001288 * @param resultRect Rect into which to put the results
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001289 */
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001290 public void cellToRect(int cellX, int cellY, int cellHSpan, int cellVSpan, RectF resultRect) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001291 final int cellWidth = mCellWidth;
1292 final int cellHeight = mCellHeight;
1293 final int widthGap = mWidthGap;
1294 final int heightGap = mHeightGap;
Winson Chungaafa03c2010-06-11 17:34:16 -07001295
1296 final int hStartPadding = getLeftPadding();
1297 final int vStartPadding = getTopPadding();
1298
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001299 int width = cellHSpan * cellWidth + ((cellHSpan - 1) * widthGap);
1300 int height = cellVSpan * cellHeight + ((cellVSpan - 1) * heightGap);
1301
1302 int x = hStartPadding + cellX * (cellWidth + widthGap);
1303 int y = vStartPadding + cellY * (cellHeight + heightGap);
Winson Chungaafa03c2010-06-11 17:34:16 -07001304
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001305 resultRect.set(x, y, x + width, y + height);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001306 }
Winson Chungaafa03c2010-06-11 17:34:16 -07001307
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001308 /**
Winson Chungaafa03c2010-06-11 17:34:16 -07001309 * Computes the required horizontal and vertical cell spans to always
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001310 * fit the given rectangle.
Winson Chungaafa03c2010-06-11 17:34:16 -07001311 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001312 * @param width Width in pixels
1313 * @param height Height in pixels
Patrick Dubroy8f86ddc2010-07-16 13:55:32 -07001314 * @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 -08001315 */
Patrick Dubroy8f86ddc2010-07-16 13:55:32 -07001316 public int[] rectToCell(int width, int height, int[] result) {
Michael Jurka9987a5c2010-10-08 16:58:12 -07001317 return rectToCell(getResources(), width, height, result);
1318 }
1319
1320 public static int[] rectToCell(Resources resources, int width, int height, int[] result) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001321 // Always assume we're working with the smallest span to make sure we
1322 // reserve enough space in both orientations.
Joe Onorato79e56262009-09-21 15:23:04 -04001323 int actualWidth = resources.getDimensionPixelSize(R.dimen.workspace_cell_width);
1324 int actualHeight = resources.getDimensionPixelSize(R.dimen.workspace_cell_height);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001325 int smallerSize = Math.min(actualWidth, actualHeight);
Joe Onorato79e56262009-09-21 15:23:04 -04001326
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001327 // Always round up to next largest cell
1328 int spanX = (width + smallerSize) / smallerSize;
1329 int spanY = (height + smallerSize) / smallerSize;
Joe Onorato79e56262009-09-21 15:23:04 -04001330
Patrick Dubroy8f86ddc2010-07-16 13:55:32 -07001331 if (result == null) {
1332 return new int[] { spanX, spanY };
1333 }
1334 result[0] = spanX;
1335 result[1] = spanY;
1336 return result;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001337 }
1338
1339 /**
1340 * Find the first vacant cell, if there is one.
1341 *
1342 * @param vacant Holds the x and y coordinate of the vacant cell
1343 * @param spanX Horizontal cell span.
1344 * @param spanY Vertical cell span.
Winson Chungaafa03c2010-06-11 17:34:16 -07001345 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001346 * @return True if a vacant cell was found
1347 */
1348 public boolean getVacantCell(int[] vacant, int spanX, int spanY) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001349
Michael Jurka0280c3b2010-09-17 15:00:07 -07001350 return findVacantCell(vacant, spanX, spanY, mCountX, mCountY, mOccupied);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001351 }
1352
1353 static boolean findVacantCell(int[] vacant, int spanX, int spanY,
1354 int xCount, int yCount, boolean[][] occupied) {
1355
1356 for (int x = 0; x < xCount; x++) {
1357 for (int y = 0; y < yCount; y++) {
1358 boolean available = !occupied[x][y];
1359out: for (int i = x; i < x + spanX - 1 && x < xCount; i++) {
1360 for (int j = y; j < y + spanY - 1 && y < yCount; j++) {
1361 available = available && !occupied[i][j];
1362 if (!available) break out;
1363 }
1364 }
1365
1366 if (available) {
1367 vacant[0] = x;
1368 vacant[1] = y;
1369 return true;
1370 }
1371 }
1372 }
1373
1374 return false;
1375 }
1376
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001377 /**
1378 * Update the array of occupied cells (mOccupied), and return a flattened copy of the array.
1379 */
1380 boolean[] getOccupiedCellsFlattened() {
Adam Cohend22015c2010-07-26 22:02:18 -07001381 final int xCount = mCountX;
1382 final int yCount = mCountY;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001383 final boolean[][] occupied = mOccupied;
1384
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001385 final boolean[] flat = new boolean[xCount * yCount];
1386 for (int y = 0; y < yCount; y++) {
1387 for (int x = 0; x < xCount; x++) {
1388 flat[y * xCount + x] = occupied[x][y];
1389 }
1390 }
1391
1392 return flat;
1393 }
1394
Michael Jurka0280c3b2010-09-17 15:00:07 -07001395 private void clearOccupiedCells() {
1396 for (int x = 0; x < mCountX; x++) {
1397 for (int y = 0; y < mCountY; y++) {
1398 mOccupied[x][y] = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001399 }
1400 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001401 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001402
Michael Jurka0280c3b2010-09-17 15:00:07 -07001403 public void onMove(View view, int newCellX, int newCellY) {
1404 LayoutParams lp = (LayoutParams) view.getLayoutParams();
1405 markCellsAsUnoccupiedForView(view);
1406 markCellsForView(newCellX, newCellY, lp.cellHSpan, lp.cellVSpan, true);
1407 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001408
Michael Jurka0280c3b2010-09-17 15:00:07 -07001409 private void markCellsAsOccupiedForView(View view) {
Michael Jurkac6ee42e2010-09-30 12:04:50 -07001410 if (view == null || view.getParent() != this) return;
Michael Jurka0280c3b2010-09-17 15:00:07 -07001411 LayoutParams lp = (LayoutParams) view.getLayoutParams();
1412 markCellsForView(lp.cellX, lp.cellY, lp.cellHSpan, lp.cellVSpan, true);
1413 }
1414
1415 private void markCellsAsUnoccupiedForView(View view) {
Michael Jurkac6ee42e2010-09-30 12:04:50 -07001416 if (view == null || view.getParent() != this) return;
Michael Jurka0280c3b2010-09-17 15:00:07 -07001417 LayoutParams lp = (LayoutParams) view.getLayoutParams();
1418 markCellsForView(lp.cellX, lp.cellY, lp.cellHSpan, lp.cellVSpan, false);
1419 }
1420
1421 private void markCellsForView(int cellX, int cellY, int spanX, int spanY, boolean value) {
1422 for (int x = cellX; x < cellX + spanX && x < mCountX; x++) {
1423 for (int y = cellY; y < cellY + spanY && y < mCountY; y++) {
1424 mOccupied[x][y] = value;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001425 }
1426 }
1427 }
1428
1429 @Override
1430 public ViewGroup.LayoutParams generateLayoutParams(AttributeSet attrs) {
1431 return new CellLayout.LayoutParams(getContext(), attrs);
1432 }
1433
1434 @Override
1435 protected boolean checkLayoutParams(ViewGroup.LayoutParams p) {
1436 return p instanceof CellLayout.LayoutParams;
1437 }
1438
1439 @Override
1440 protected ViewGroup.LayoutParams generateLayoutParams(ViewGroup.LayoutParams p) {
1441 return new CellLayout.LayoutParams(p);
1442 }
1443
Winson Chungaafa03c2010-06-11 17:34:16 -07001444 public static class CellLayoutAnimationController extends LayoutAnimationController {
1445 public CellLayoutAnimationController(Animation animation, float delay) {
1446 super(animation, delay);
1447 }
1448
1449 @Override
1450 protected long getDelayForView(View view) {
1451 return (int) (Math.random() * 150);
1452 }
1453 }
1454
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001455 public static class LayoutParams extends ViewGroup.MarginLayoutParams {
1456 /**
1457 * Horizontal location of the item in the grid.
1458 */
1459 @ViewDebug.ExportedProperty
1460 public int cellX;
1461
1462 /**
1463 * Vertical location of the item in the grid.
1464 */
1465 @ViewDebug.ExportedProperty
1466 public int cellY;
1467
1468 /**
1469 * Number of cells spanned horizontally by the item.
1470 */
1471 @ViewDebug.ExportedProperty
1472 public int cellHSpan;
1473
1474 /**
1475 * Number of cells spanned vertically by the item.
1476 */
1477 @ViewDebug.ExportedProperty
1478 public int cellVSpan;
Winson Chungaafa03c2010-06-11 17:34:16 -07001479
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001480 /**
1481 * Is this item currently being dragged
1482 */
1483 public boolean isDragging;
1484
1485 // X coordinate of the view in the layout.
1486 @ViewDebug.ExportedProperty
1487 int x;
1488 // Y coordinate of the view in the layout.
1489 @ViewDebug.ExportedProperty
1490 int y;
1491
Patrick Dubroyce34a972010-10-19 10:34:32 -07001492 /**
1493 * The old X coordinate of this item, relative to its current parent.
1494 * Used to animate the item into its new position.
1495 */
1496 int oldX;
1497
1498 /**
1499 * The old Y coordinate of this item, relative to its current parent.
1500 * Used to animate the item into its new position.
1501 */
1502 int oldY;
1503
Romain Guy84f296c2009-11-04 15:00:44 -08001504 boolean dropped;
Romain Guyfcb9e712009-10-02 16:06:52 -07001505
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001506 public LayoutParams(Context c, AttributeSet attrs) {
1507 super(c, attrs);
1508 cellHSpan = 1;
1509 cellVSpan = 1;
1510 }
1511
1512 public LayoutParams(ViewGroup.LayoutParams source) {
1513 super(source);
1514 cellHSpan = 1;
1515 cellVSpan = 1;
1516 }
Winson Chungaafa03c2010-06-11 17:34:16 -07001517
1518 public LayoutParams(LayoutParams source) {
1519 super(source);
1520 this.cellX = source.cellX;
1521 this.cellY = source.cellY;
1522 this.cellHSpan = source.cellHSpan;
1523 this.cellVSpan = source.cellVSpan;
1524 }
1525
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001526 public LayoutParams(int cellX, int cellY, int cellHSpan, int cellVSpan) {
Romain Guy8f19cdd2010-01-08 15:07:00 -08001527 super(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001528 this.cellX = cellX;
1529 this.cellY = cellY;
1530 this.cellHSpan = cellHSpan;
1531 this.cellVSpan = cellVSpan;
1532 }
1533
1534 public void setup(int cellWidth, int cellHeight, int widthGap, int heightGap,
1535 int hStartPadding, int vStartPadding) {
Winson Chungaafa03c2010-06-11 17:34:16 -07001536
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001537 final int myCellHSpan = cellHSpan;
1538 final int myCellVSpan = cellVSpan;
1539 final int myCellX = cellX;
1540 final int myCellY = cellY;
Winson Chungaafa03c2010-06-11 17:34:16 -07001541
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001542 width = myCellHSpan * cellWidth + ((myCellHSpan - 1) * widthGap) -
1543 leftMargin - rightMargin;
1544 height = myCellVSpan * cellHeight + ((myCellVSpan - 1) * heightGap) -
1545 topMargin - bottomMargin;
1546
1547 x = hStartPadding + myCellX * (cellWidth + widthGap) + leftMargin;
1548 y = vStartPadding + myCellY * (cellHeight + heightGap) + topMargin;
1549 }
Winson Chungaafa03c2010-06-11 17:34:16 -07001550
1551 public String toString() {
1552 return "(" + this.cellX + ", " + this.cellY + ")";
1553 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001554 }
1555
Michael Jurka0280c3b2010-09-17 15:00:07 -07001556 // This class stores info for two purposes:
1557 // 1. When dragging items (mDragInfo in Workspace), we store the View, its cellX & cellY,
1558 // its spanX, spanY, and the screen it is on
1559 // 2. When long clicking on an empty cell in a CellLayout, we save information about the
1560 // cellX and cellY coordinates and which page was clicked. We then set this as a tag on
1561 // the CellLayout that was long clicked
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001562 static final class CellInfo implements ContextMenu.ContextMenuInfo {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001563 View cell;
Michael Jurkaa63c4522010-08-19 13:52:27 -07001564 int cellX = -1;
1565 int cellY = -1;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001566 int spanX;
1567 int spanY;
1568 int screen;
1569 boolean valid;
1570
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001571 @Override
1572 public String toString() {
Winson Chungaafa03c2010-06-11 17:34:16 -07001573 return "Cell[view=" + (cell == null ? "null" : cell.getClass())
1574 + ", x=" + cellX + ", y=" + cellY + "]";
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001575 }
1576 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001577}