blob: c9be887d3ab2de48b92a3ff028dffd32afc741b4 [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
Adam Cohenf34bab52010-09-30 14:11:56 -070052public class CellLayout extends ViewGroup implements Dimmable {
Winson Chungaafa03c2010-06-11 17:34:16 -070053 static final String TAG = "CellLayout";
54
The Android Open Source Project31dd5032009-03-03 19:32:27 -080055 private int mCellWidth;
56 private int mCellHeight;
Winson Chungaafa03c2010-06-11 17:34:16 -070057
Winson Chungaafa03c2010-06-11 17:34:16 -070058 private int mLeftPadding;
59 private int mRightPadding;
60 private int mTopPadding;
61 private int mBottomPadding;
62
Adam Cohend22015c2010-07-26 22:02:18 -070063 private int mCountX;
64 private int mCountY;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080065
66 private int mWidthGap;
67 private int mHeightGap;
68
69 private final Rect mRect = new Rect();
Patrick Dubroy8f86ddc2010-07-16 13:55:32 -070070 private final RectF mRectF = new RectF();
The Android Open Source Project31dd5032009-03-03 19:32:27 -080071 private final CellInfo mCellInfo = new CellInfo();
Winson Chungaafa03c2010-06-11 17:34:16 -070072
Patrick Dubroyde7658b2010-09-27 11:15:43 -070073 // These are temporary variables to prevent having to allocate a new object just to
74 // return an (x, y) value from helper functions. Do NOT use them to maintain other state.
Patrick Dubroy6569f2c2010-07-12 14:25:18 -070075 private final int[] mTmpCellXY = new int[2];
Patrick Dubroyde7658b2010-09-27 11:15:43 -070076 private final int[] mTmpPoint = new int[2];
77 private final PointF mTmpPointF = new PointF();
Patrick Dubroy6569f2c2010-07-12 14:25:18 -070078
The Android Open Source Project31dd5032009-03-03 19:32:27 -080079 boolean[][] mOccupied;
80
Michael Jurkadee05892010-07-27 10:01:56 -070081 private OnTouchListener mInterceptTouchListener;
82
Michael Jurka5f1c5092010-09-03 14:15:02 -070083 private float mBackgroundAlpha;
Adam Cohenf34bab52010-09-30 14:11:56 -070084
Michael Jurka5f1c5092010-09-03 14:15:02 -070085 private Drawable mBackground;
Adam Cohenf34bab52010-09-30 14:11:56 -070086 private Drawable mBackgroundMini;
87 private Drawable mBackgroundMiniHover;
Patrick Dubroy1262e362010-10-06 15:49:50 -070088 private Drawable mBackgroundHover;
Michael Jurka3e7c7632010-10-02 16:01:03 -070089 private Drawable mBackgroundMiniAcceptsDrops;
Michael Jurka18014792010-10-14 09:01:34 -070090 private Rect mBackgroundRect;
91 private Rect mHoverRect;
92 private float mHoverScale;
93 private float mHoverAlpha;
Michael Jurka3e7c7632010-10-02 16:01:03 -070094 private boolean mAcceptsDrops;
Patrick Dubroy1262e362010-10-06 15:49:50 -070095
96 // If we're actively dragging something over this screen, mHover is true
Michael Jurkaa63c4522010-08-19 13:52:27 -070097 private boolean mHover = false;
Michael Jurkadee05892010-07-27 10:01:56 -070098
Patrick Dubroyde7658b2010-09-27 11:15:43 -070099 private final Point mDragCenter = new Point();
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700100
Winson Chung150fbab2010-09-29 17:14:26 -0700101 // These arrays are used to implement the drag visualization on x-large screens.
Joe Onorato4be866d2010-10-10 11:26:02 -0700102 // They are used as circular arrays, indexed by mDragOutlineCurrent.
103 private Point[] mDragOutlines = new Point[8];
Chet Haase472b2812010-10-14 07:02:04 -0700104 private float[] mDragOutlineAlphas = new float[mDragOutlines.length];
Joe Onorato4be866d2010-10-10 11:26:02 -0700105 private InterruptibleInOutAnimator[] mDragOutlineAnims =
106 new InterruptibleInOutAnimator[mDragOutlines.length];
Winson Chung150fbab2010-09-29 17:14:26 -0700107
108 // Used as an index into the above 3 arrays; indicates which is the most current value.
Joe Onorato4be866d2010-10-10 11:26:02 -0700109 private int mDragOutlineCurrent = 0;
Winson Chung150fbab2010-09-29 17:14:26 -0700110
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700111 private Drawable mCrosshairsDrawable = null;
Patrick Dubroy49250ad2010-10-08 15:33:52 -0700112 private InterruptibleInOutAnimator mCrosshairsAnimator = null;
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700113 private float mCrosshairsVisibility = 0.0f;
114
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700115 // When a drag operation is in progress, holds the nearest cell to the touch point
116 private final int[] mDragCell = new int[2];
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800117
Winson Chungaafa03c2010-06-11 17:34:16 -0700118 private final WallpaperManager mWallpaperManager;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800119
Joe Onorato4be866d2010-10-10 11:26:02 -0700120 private boolean mDragging = false;
121
Patrick Dubroyce34a972010-10-19 10:34:32 -0700122 private ObjectAnimator mDropAnim;
123 private TimeInterpolator mEaseOutInterpolator;
124
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800125 public CellLayout(Context context) {
126 this(context, null);
127 }
128
129 public CellLayout(Context context, AttributeSet attrs) {
130 this(context, attrs, 0);
131 }
132
133 public CellLayout(Context context, AttributeSet attrs, int defStyle) {
134 super(context, attrs, defStyle);
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700135
136 // A ViewGroup usually does not draw, but CellLayout needs to draw a rectangle to show
137 // the user where a dragged item will land when dropped.
138 setWillNotDraw(false);
Michael Jurkaa63c4522010-08-19 13:52:27 -0700139
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800140 TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.CellLayout, defStyle, 0);
141
142 mCellWidth = a.getDimensionPixelSize(R.styleable.CellLayout_cellWidth, 10);
143 mCellHeight = a.getDimensionPixelSize(R.styleable.CellLayout_cellHeight, 10);
Winson Chungaafa03c2010-06-11 17:34:16 -0700144
Adam Cohend22015c2010-07-26 22:02:18 -0700145 mLeftPadding =
146 a.getDimensionPixelSize(R.styleable.CellLayout_xAxisStartPadding, 10);
147 mRightPadding =
148 a.getDimensionPixelSize(R.styleable.CellLayout_xAxisEndPadding, 10);
149 mTopPadding =
150 a.getDimensionPixelSize(R.styleable.CellLayout_yAxisStartPadding, 10);
151 mBottomPadding =
152 a.getDimensionPixelSize(R.styleable.CellLayout_yAxisEndPadding, 10);
Winson Chungaafa03c2010-06-11 17:34:16 -0700153
Adam Cohend22015c2010-07-26 22:02:18 -0700154 mCountX = LauncherModel.getCellCountX();
155 mCountY = LauncherModel.getCellCountY();
Michael Jurka0280c3b2010-09-17 15:00:07 -0700156 mOccupied = new boolean[mCountX][mCountY];
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800157
158 a.recycle();
159
160 setAlwaysDrawnWithCacheEnabled(false);
161
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700162 mWallpaperManager = WallpaperManager.getInstance(context);
163
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700164 final Resources res = getResources();
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700165
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700166 if (LauncherApplication.isScreenXLarge()) {
Winson Chung150fbab2010-09-29 17:14:26 -0700167 mBackgroundMini = res.getDrawable(R.drawable.mini_home_screen_bg);
Adam Cohenf34bab52010-09-30 14:11:56 -0700168 mBackgroundMini.setFilterBitmap(true);
Winson Chung150fbab2010-09-29 17:14:26 -0700169 mBackground = res.getDrawable(R.drawable.home_screen_bg);
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700170 mBackground.setFilterBitmap(true);
Winson Chung150fbab2010-09-29 17:14:26 -0700171 mBackgroundMiniHover = res.getDrawable(R.drawable.mini_home_screen_bg_hover);
Adam Cohenf34bab52010-09-30 14:11:56 -0700172 mBackgroundMiniHover.setFilterBitmap(true);
Patrick Dubroy1262e362010-10-06 15:49:50 -0700173 mBackgroundHover = res.getDrawable(R.drawable.home_screen_bg_hover);
174 mBackgroundHover.setFilterBitmap(true);
Michael Jurka3e7c7632010-10-02 16:01:03 -0700175 mBackgroundMiniAcceptsDrops = res.getDrawable(
176 R.drawable.mini_home_screen_bg_accepts_drops);
177 mBackgroundMiniAcceptsDrops.setFilterBitmap(true);
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700178 }
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700179
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700180 // Initialize the data structures used for the drag visualization.
Winson Chung150fbab2010-09-29 17:14:26 -0700181
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700182 mCrosshairsDrawable = res.getDrawable(R.drawable.gardening_crosshairs);
Patrick Dubroyce34a972010-10-19 10:34:32 -0700183 mEaseOutInterpolator = new DecelerateInterpolator(2.5f); // Quint ease out
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700184
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700185 // Set up the animation for fading the crosshairs in and out
186 int animDuration = res.getInteger(R.integer.config_crosshairsFadeInTime);
Patrick Dubroy49250ad2010-10-08 15:33:52 -0700187 mCrosshairsAnimator = new InterruptibleInOutAnimator(animDuration, 0.0f, 1.0f);
Chet Haase472b2812010-10-14 07:02:04 -0700188 mCrosshairsAnimator.getAnimator().addUpdateListener(new AnimatorUpdateListener() {
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700189 public void onAnimationUpdate(ValueAnimator animation) {
190 mCrosshairsVisibility = ((Float) animation.getAnimatedValue()).floatValue();
191 CellLayout.this.invalidate();
192 }
193 });
Patrick Dubroyce34a972010-10-19 10:34:32 -0700194 mCrosshairsAnimator.getAnimator().setInterpolator(mEaseOutInterpolator);
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700195
Joe Onorato4be866d2010-10-10 11:26:02 -0700196 for (int i = 0; i < mDragOutlines.length; i++) {
197 mDragOutlines[i] = new Point(-1, -1);
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700198 }
199
200 // When dragging things around the home screens, we show a green outline of
201 // where the item will land. The outlines gradually fade out, leaving a trail
202 // behind the drag path.
203 // Set up all the animations that are used to implement this fading.
204 final int duration = res.getInteger(R.integer.config_dragOutlineFadeTime);
Chet Haase472b2812010-10-14 07:02:04 -0700205 final float fromAlphaValue = 0;
206 final float toAlphaValue = (float)res.getInteger(R.integer.config_dragOutlineMaxAlpha);
Joe Onorato4be866d2010-10-10 11:26:02 -0700207
208 for (int i = 0; i < mDragOutlineAlphas.length; i++) {
209 mDragOutlineAlphas[i] = fromAlphaValue;
210 }
211
212 for (int i = 0; i < mDragOutlineAnims.length; i++) {
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700213 final InterruptibleInOutAnimator anim =
214 new InterruptibleInOutAnimator(duration, fromAlphaValue, toAlphaValue);
Patrick Dubroyce34a972010-10-19 10:34:32 -0700215 anim.getAnimator().setInterpolator(mEaseOutInterpolator);
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700216 final int thisIndex = i;
Chet Haase472b2812010-10-14 07:02:04 -0700217 anim.getAnimator().addUpdateListener(new AnimatorUpdateListener() {
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700218 public void onAnimationUpdate(ValueAnimator animation) {
Joe Onorato4be866d2010-10-10 11:26:02 -0700219 final Bitmap outline = (Bitmap)anim.getTag();
220
221 // If an animation is started and then stopped very quickly, we can still
222 // get spurious updates we've cleared the tag. Guard against this.
223 if (outline == null) {
Patrick Dubroyfe6bd872010-10-13 17:32:10 -0700224 if (false) {
225 Object val = animation.getAnimatedValue();
226 Log.d(TAG, "anim " + thisIndex + " update: " + val +
227 ", isStopped " + anim.isStopped());
228 }
Joe Onorato4be866d2010-10-10 11:26:02 -0700229 // Try to prevent it from continuing to run
230 animation.cancel();
231 } else {
Chet Haase472b2812010-10-14 07:02:04 -0700232 mDragOutlineAlphas[thisIndex] = (Float) animation.getAnimatedValue();
Joe Onorato4be866d2010-10-10 11:26:02 -0700233 final int left = mDragOutlines[thisIndex].x;
234 final int top = mDragOutlines[thisIndex].y;
235 CellLayout.this.invalidate(left, top,
236 left + outline.getWidth(), top + outline.getHeight());
237 }
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700238 }
239 });
Joe Onorato4be866d2010-10-10 11:26:02 -0700240 // The animation holds a reference to the drag outline bitmap as long is it's
241 // running. This way the bitmap can be GCed when the animations are complete.
Chet Haase472b2812010-10-14 07:02:04 -0700242 anim.getAnimator().addListener(new AnimatorListenerAdapter() {
Joe Onorato4be866d2010-10-10 11:26:02 -0700243 public void onAnimationEnd(Animator animation) {
Chet Haase472b2812010-10-14 07:02:04 -0700244 if ((Float) ((ValueAnimator) animation).getAnimatedValue() == 0f) {
Joe Onorato4be866d2010-10-10 11:26:02 -0700245 anim.setTag(null);
246 }
247 }
248 });
249 mDragOutlineAnims[i] = anim;
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700250 }
Patrick Dubroyce34a972010-10-19 10:34:32 -0700251
252 mDropAnim = new ObjectAnimator();
253 mDropAnim.setInterpolator(mEaseOutInterpolator);
254
Michael Jurka18014792010-10-14 09:01:34 -0700255 mBackgroundRect = new Rect();
256 mHoverRect = new Rect();
257 setHoverScale(1.0f);
258 setHoverAlpha(1.0f);
259 }
260
261 private void updateHoverRect() {
262 float marginFraction = (mHoverScale - 1.0f) / 2.0f;
263 int marginX = (int) (marginFraction * (mBackgroundRect.right - mBackgroundRect.left));
264 int marginY = (int) (marginFraction * (mBackgroundRect.bottom - mBackgroundRect.top));
265 mHoverRect.set(mBackgroundRect.left - marginX, mBackgroundRect.top - marginY,
266 mBackgroundRect.right + marginX, mBackgroundRect.bottom + marginY);
267 invalidate();
268 }
269
270 public void setHoverScale(float scaleFactor) {
271 if (scaleFactor != mHoverScale) {
272 mHoverScale = scaleFactor;
273 updateHoverRect();
274 }
275 }
276
277 public float getHoverScale() {
278 return mHoverScale;
279 }
280
281 public float getHoverAlpha() {
282 return mHoverAlpha;
283 }
284
285 public void setHoverAlpha(float alpha) {
286 mHoverAlpha = alpha;
287 invalidate();
288 }
289
290 void animateDrop() {
291 if (LauncherApplication.isScreenXLarge()) {
292 Resources res = getResources();
293 float onDropScale = res.getInteger(R.integer.config_screenOnDropScalePercent) / 100.0f;
294 ObjectAnimator scaleUp = ObjectAnimator.ofFloat(this, "hoverScale", onDropScale);
295 scaleUp.setDuration(res.getInteger(R.integer.config_screenOnDropScaleUpDuration));
296 ObjectAnimator scaleDown = ObjectAnimator.ofFloat(this, "hoverScale", 1.0f);
297 scaleDown.setDuration(res.getInteger(R.integer.config_screenOnDropScaleDownDuration));
298 ObjectAnimator alphaFadeOut = ObjectAnimator.ofFloat(this, "hoverAlpha", 0.0f);
299
300 alphaFadeOut.setStartDelay(res.getInteger(R.integer.config_screenOnDropAlphaFadeDelay));
301 alphaFadeOut.setDuration(res.getInteger(R.integer.config_screenOnDropAlphaFadeDelay));
302
303 AnimatorSet bouncer = new AnimatorSet();
304 bouncer.play(scaleUp).before(scaleDown);
305 bouncer.play(scaleUp).with(alphaFadeOut);
306 bouncer.addListener(new AnimatorListenerAdapter() {
307 public void onAnimationStart(Animator animation) {
308 setHover(true);
309 }
310 public void onAnimationEnd(Animator animation) {
311 setHover(false);
312 setHoverScale(1.0f);
313 setHoverAlpha(1.0f);
314 }
315 });
316 bouncer.start();
317 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800318 }
319
Michael Jurkaa63c4522010-08-19 13:52:27 -0700320 public void setHover(boolean value) {
321 if (mHover != value) {
Michael Jurka3e7c7632010-10-02 16:01:03 -0700322 mHover = value;
Michael Jurkaa63c4522010-08-19 13:52:27 -0700323 invalidate();
324 }
Michael Jurkaa63c4522010-08-19 13:52:27 -0700325 }
326
Patrick Dubroy1262e362010-10-06 15:49:50 -0700327 public void drawChildren(Canvas canvas) {
328 super.dispatchDraw(canvas);
329 }
330
Jeff Sharkey83f111d2009-04-20 21:03:13 -0700331 @Override
Patrick Dubroy1262e362010-10-06 15:49:50 -0700332 protected void onDraw(Canvas canvas) {
Michael Jurka3e7c7632010-10-02 16:01:03 -0700333 // When we're large, we are either drawn in a "hover" state (ie when dragging an item to
334 // a neighboring page) or with just a normal background (if backgroundAlpha > 0.0f)
335 // When we're small, we are either drawn normally or in the "accepts drops" state (during
336 // a drag). However, we also drag the mini hover background *over* one of those two
337 // backgrounds
Michael Jurka5f1c5092010-09-03 14:15:02 -0700338 if (mBackgroundAlpha > 0.0f) {
Adam Cohenf34bab52010-09-30 14:11:56 -0700339 Drawable bg;
Patrick Dubroy1262e362010-10-06 15:49:50 -0700340 if (getScaleX() < 0.5f) {
Michael Jurka3e7c7632010-10-02 16:01:03 -0700341 bg = mAcceptsDrops ? mBackgroundMiniAcceptsDrops : mBackgroundMini;
Adam Cohenf34bab52010-09-30 14:11:56 -0700342 } else {
Patrick Dubroy1262e362010-10-06 15:49:50 -0700343 bg = mHover ? mBackgroundHover : mBackground;
Adam Cohenf34bab52010-09-30 14:11:56 -0700344 }
Adam Cohen9c4949e2010-10-05 12:27:22 -0700345 if (bg != null) {
346 bg.setAlpha((int) (mBackgroundAlpha * 255));
Michael Jurka18014792010-10-14 09:01:34 -0700347 bg.setBounds(mBackgroundRect);
Adam Cohen9c4949e2010-10-05 12:27:22 -0700348 bg.draw(canvas);
349 }
Michael Jurka3e7c7632010-10-02 16:01:03 -0700350 if (mHover && getScaleX() < 0.5f) {
Michael Jurka18014792010-10-14 09:01:34 -0700351 boolean modifiedClipRect = false;
352 if (mHoverScale > 1.0f) {
353 // If the hover background's scale is greater than 1, we'll be drawing outside
354 // the bounds of this CellLayout. Get around that by temporarily increasing the
355 // size of the clip rect
356 float marginFraction = (mHoverScale - 1.0f) / 2.0f;
357 Rect clipRect = canvas.getClipBounds();
358 int marginX = (int) (marginFraction * (clipRect.right - clipRect.left));
359 int marginY = (int) (marginFraction * (clipRect.bottom - clipRect.top));
360 canvas.save(Canvas.CLIP_SAVE_FLAG);
361 canvas.clipRect(-marginX, -marginY,
362 getWidth() + marginX, getHeight() + marginY, Region.Op.REPLACE);
363 modifiedClipRect = true;
364 }
365
366 mBackgroundMiniHover.setAlpha((int) (mBackgroundAlpha * mHoverAlpha * 255));
367 mBackgroundMiniHover.setBounds(mHoverRect);
Michael Jurka3e7c7632010-10-02 16:01:03 -0700368 mBackgroundMiniHover.draw(canvas);
Michael Jurka18014792010-10-14 09:01:34 -0700369 if (modifiedClipRect) {
370 canvas.restore();
371 }
Michael Jurka3e7c7632010-10-02 16:01:03 -0700372 }
Michael Jurkaa63c4522010-08-19 13:52:27 -0700373 }
Romain Guya6abce82009-11-10 02:54:41 -0800374
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700375 if (mCrosshairsVisibility > 0.0f) {
376 final int countX = mCountX;
377 final int countY = mCountY;
378
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700379 final float MAX_ALPHA = 0.4f;
380 final int MAX_VISIBLE_DISTANCE = 600;
381 final float DISTANCE_MULTIPLIER = 0.002f;
382
383 final Drawable d = mCrosshairsDrawable;
384 final int width = d.getIntrinsicWidth();
385 final int height = d.getIntrinsicHeight();
386
387 int x = getLeftPadding() - (mWidthGap / 2) - (width / 2);
388 for (int col = 0; col <= countX; col++) {
389 int y = getTopPadding() - (mHeightGap / 2) - (height / 2);
390 for (int row = 0; row <= countY; row++) {
391 mTmpPointF.set(x - mDragCenter.x, y - mDragCenter.y);
392 float dist = mTmpPointF.length();
393 // Crosshairs further from the drag point are more faint
394 float alpha = Math.min(MAX_ALPHA,
395 DISTANCE_MULTIPLIER * (MAX_VISIBLE_DISTANCE - dist));
396 if (alpha > 0.0f) {
397 d.setBounds(x, y, x + width, y + height);
398 d.setAlpha((int) (alpha * 255 * mCrosshairsVisibility));
399 d.draw(canvas);
400 }
401 y += mCellHeight + mHeightGap;
402 }
403 x += mCellWidth + mWidthGap;
404 }
Joe Onorato4be866d2010-10-10 11:26:02 -0700405 }
Winson Chung150fbab2010-09-29 17:14:26 -0700406
Joe Onorato4be866d2010-10-10 11:26:02 -0700407 final Paint paint = new Paint();
408 for (int i = 0; i < mDragOutlines.length; i++) {
Chet Haase472b2812010-10-14 07:02:04 -0700409 final float alpha = mDragOutlineAlphas[i];
Joe Onorato4be866d2010-10-10 11:26:02 -0700410 if (alpha > 0) {
411 final Point p = mDragOutlines[i];
412 final Bitmap b = (Bitmap) mDragOutlineAnims[i].getTag();
Chet Haase472b2812010-10-14 07:02:04 -0700413 paint.setAlpha((int)(alpha + .5f));
Joe Onorato4be866d2010-10-10 11:26:02 -0700414 canvas.drawBitmap(b, p.x, p.y, paint);
Winson Chung150fbab2010-09-29 17:14:26 -0700415 }
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700416 }
417 }
418
Adam Cohenf34bab52010-09-30 14:11:56 -0700419 public void setDimmableProgress(float progress) {
420 for (int i = 0; i < getChildCount(); i++) {
421 Dimmable d = (Dimmable) getChildAt(i);
422 d.setDimmableProgress(progress);
423 }
424 }
425
426 public float getDimmableProgress() {
427 if (getChildCount() > 0) {
428 return ((Dimmable) getChildAt(0)).getDimmableProgress();
429 }
430 return 0.0f;
431 }
432
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700433 @Override
Jeff Sharkey83f111d2009-04-20 21:03:13 -0700434 public void cancelLongPress() {
435 super.cancelLongPress();
436
437 // Cancel long press for all children
438 final int count = getChildCount();
439 for (int i = 0; i < count; i++) {
440 final View child = getChildAt(i);
441 child.cancelLongPress();
442 }
443 }
444
Michael Jurkadee05892010-07-27 10:01:56 -0700445 public void setOnInterceptTouchListener(View.OnTouchListener listener) {
446 mInterceptTouchListener = listener;
447 }
448
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800449 int getCountX() {
Adam Cohend22015c2010-07-26 22:02:18 -0700450 return mCountX;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800451 }
452
453 int getCountY() {
Adam Cohend22015c2010-07-26 22:02:18 -0700454 return mCountY;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800455 }
456
Winson Chungaafa03c2010-06-11 17:34:16 -0700457 public boolean addViewToCellLayout(View child, int index, int childId, LayoutParams params) {
458 final LayoutParams lp = params;
459
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800460 // Generate an id for each view, this assumes we have at most 256x256 cells
461 // per workspace screen
Adam Cohend22015c2010-07-26 22:02:18 -0700462 if (lp.cellX >= 0 && lp.cellX <= mCountX - 1 && lp.cellY >= 0 && lp.cellY <= mCountY - 1) {
Winson Chungaafa03c2010-06-11 17:34:16 -0700463 // If the horizontal or vertical span is set to -1, it is taken to
464 // mean that it spans the extent of the CellLayout
Adam Cohend22015c2010-07-26 22:02:18 -0700465 if (lp.cellHSpan < 0) lp.cellHSpan = mCountX;
466 if (lp.cellVSpan < 0) lp.cellVSpan = mCountY;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800467
Winson Chungaafa03c2010-06-11 17:34:16 -0700468 child.setId(childId);
469
Michael Jurkadee05892010-07-27 10:01:56 -0700470 // We might be in the middle or end of shrinking/fading to a dimmed view
471 // Make sure this view's alpha is set the same as all the rest of the views
Michael Jurka5f1c5092010-09-03 14:15:02 -0700472 child.setAlpha(getAlpha());
Winson Chungaafa03c2010-06-11 17:34:16 -0700473 addView(child, index, lp);
Michael Jurkadee05892010-07-27 10:01:56 -0700474
Michael Jurka0280c3b2010-09-17 15:00:07 -0700475 markCellsAsOccupiedForView(child);
476
Winson Chungaafa03c2010-06-11 17:34:16 -0700477 return true;
478 }
479 return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800480 }
Michael Jurka3e7c7632010-10-02 16:01:03 -0700481 public void setAcceptsDrops(boolean acceptsDrops) {
482 if (mAcceptsDrops != acceptsDrops) {
483 mAcceptsDrops = acceptsDrops;
484 invalidate();
485 }
486 }
487
488 public boolean getAcceptsDrops() {
489 return mAcceptsDrops;
490 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800491
492 @Override
Michael Jurka0280c3b2010-09-17 15:00:07 -0700493 public void removeAllViews() {
494 clearOccupiedCells();
495 }
496
497 @Override
498 public void removeAllViewsInLayout() {
499 clearOccupiedCells();
500 }
501
502 @Override
503 public void removeView(View view) {
504 markCellsAsUnoccupiedForView(view);
505 super.removeView(view);
506 }
507
508 @Override
509 public void removeViewAt(int index) {
510 markCellsAsUnoccupiedForView(getChildAt(index));
511 super.removeViewAt(index);
512 }
513
514 @Override
515 public void removeViewInLayout(View view) {
516 markCellsAsUnoccupiedForView(view);
517 super.removeViewInLayout(view);
518 }
519
520 @Override
521 public void removeViews(int start, int count) {
522 for (int i = start; i < start + count; i++) {
523 markCellsAsUnoccupiedForView(getChildAt(i));
524 }
525 super.removeViews(start, count);
526 }
527
528 @Override
529 public void removeViewsInLayout(int start, int count) {
530 for (int i = start; i < start + count; i++) {
531 markCellsAsUnoccupiedForView(getChildAt(i));
532 }
533 super.removeViewsInLayout(start, count);
534 }
535
536 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800537 public void requestChildFocus(View child, View focused) {
538 super.requestChildFocus(child, focused);
539 if (child != null) {
540 Rect r = new Rect();
541 child.getDrawingRect(r);
542 requestRectangleOnScreen(r);
543 }
544 }
545
546 @Override
547 protected void onAttachedToWindow() {
548 super.onAttachedToWindow();
549 mCellInfo.screen = ((ViewGroup) getParent()).indexOfChild(this);
550 }
551
Michael Jurkaaf442092010-06-10 17:01:57 -0700552 public void setTagToCellInfoForPoint(int touchX, int touchY) {
553 final CellInfo cellInfo = mCellInfo;
554 final Rect frame = mRect;
555 final int x = touchX + mScrollX;
556 final int y = touchY + mScrollY;
557 final int count = getChildCount();
558
559 boolean found = false;
560 for (int i = count - 1; i >= 0; i--) {
561 final View child = getChildAt(i);
562
563 if ((child.getVisibility()) == VISIBLE || child.getAnimation() != null) {
564 child.getHitRect(frame);
565 if (frame.contains(x, y)) {
566 final LayoutParams lp = (LayoutParams) child.getLayoutParams();
567 cellInfo.cell = child;
568 cellInfo.cellX = lp.cellX;
569 cellInfo.cellY = lp.cellY;
570 cellInfo.spanX = lp.cellHSpan;
571 cellInfo.spanY = lp.cellVSpan;
572 cellInfo.valid = true;
573 found = true;
Michael Jurkaaf442092010-06-10 17:01:57 -0700574 break;
575 }
576 }
577 }
Winson Chungaafa03c2010-06-11 17:34:16 -0700578
Michael Jurkaaf442092010-06-10 17:01:57 -0700579 if (!found) {
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700580 final int cellXY[] = mTmpCellXY;
Michael Jurkaaf442092010-06-10 17:01:57 -0700581 pointToCellExact(x, y, cellXY);
582
Michael Jurkaaf442092010-06-10 17:01:57 -0700583 cellInfo.cell = null;
584 cellInfo.cellX = cellXY[0];
585 cellInfo.cellY = cellXY[1];
586 cellInfo.spanX = 1;
587 cellInfo.spanY = 1;
Michael Jurka0280c3b2010-09-17 15:00:07 -0700588 cellInfo.valid = cellXY[0] >= 0 && cellXY[1] >= 0 && cellXY[0] < mCountX &&
589 cellXY[1] < mCountY && !mOccupied[cellXY[0]][cellXY[1]];
Michael Jurkaaf442092010-06-10 17:01:57 -0700590 }
591 setTag(cellInfo);
592 }
593
Winson Chungaafa03c2010-06-11 17:34:16 -0700594
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800595 @Override
596 public boolean onInterceptTouchEvent(MotionEvent ev) {
Michael Jurkadee05892010-07-27 10:01:56 -0700597 if (mInterceptTouchListener != null && mInterceptTouchListener.onTouch(this, ev)) {
598 return true;
599 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800600 final int action = ev.getAction();
601 final CellInfo cellInfo = mCellInfo;
602
603 if (action == MotionEvent.ACTION_DOWN) {
Michael Jurkaaf442092010-06-10 17:01:57 -0700604 setTagToCellInfoForPoint((int) ev.getX(), (int) ev.getY());
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800605 } else if (action == MotionEvent.ACTION_UP) {
606 cellInfo.cell = null;
607 cellInfo.cellX = -1;
608 cellInfo.cellY = -1;
609 cellInfo.spanX = 0;
610 cellInfo.spanY = 0;
611 cellInfo.valid = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800612 setTag(cellInfo);
613 }
614
615 return false;
616 }
617
618 @Override
619 public CellInfo getTag() {
Michael Jurka0280c3b2010-09-17 15:00:07 -0700620 return (CellInfo) super.getTag();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800621 }
622
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700623 /**
624 * Check if the row 'y' is empty from columns 'left' to 'right', inclusive.
625 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800626 private static boolean isRowEmpty(int y, int left, int right, boolean[][] occupied) {
627 for (int x = left; x <= right; x++) {
628 if (occupied[x][y]) {
629 return false;
630 }
631 }
632 return true;
633 }
634
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800635 /**
Winson Chungaafa03c2010-06-11 17:34:16 -0700636 * Given a point, return the cell that strictly encloses that point
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800637 * @param x X coordinate of the point
638 * @param y Y coordinate of the point
639 * @param result Array of 2 ints to hold the x and y coordinate of the cell
640 */
641 void pointToCellExact(int x, int y, int[] result) {
Winson Chungaafa03c2010-06-11 17:34:16 -0700642 final int hStartPadding = getLeftPadding();
643 final int vStartPadding = getTopPadding();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800644
645 result[0] = (x - hStartPadding) / (mCellWidth + mWidthGap);
646 result[1] = (y - vStartPadding) / (mCellHeight + mHeightGap);
647
Adam Cohend22015c2010-07-26 22:02:18 -0700648 final int xAxis = mCountX;
649 final int yAxis = mCountY;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800650
651 if (result[0] < 0) result[0] = 0;
652 if (result[0] >= xAxis) result[0] = xAxis - 1;
653 if (result[1] < 0) result[1] = 0;
654 if (result[1] >= yAxis) result[1] = yAxis - 1;
655 }
Winson Chungaafa03c2010-06-11 17:34:16 -0700656
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800657 /**
658 * Given a point, return the cell that most closely encloses that point
659 * @param x X coordinate of the point
660 * @param y Y coordinate of the point
661 * @param result Array of 2 ints to hold the x and y coordinate of the cell
662 */
663 void pointToCellRounded(int x, int y, int[] result) {
664 pointToCellExact(x + (mCellWidth / 2), y + (mCellHeight / 2), result);
665 }
666
667 /**
668 * Given a cell coordinate, return the point that represents the upper left corner of that cell
Winson Chungaafa03c2010-06-11 17:34:16 -0700669 *
670 * @param cellX X coordinate of the cell
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800671 * @param cellY Y coordinate of the cell
Winson Chungaafa03c2010-06-11 17:34:16 -0700672 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800673 * @param result Array of 2 ints to hold the x and y coordinate of the point
674 */
675 void cellToPoint(int cellX, int cellY, int[] result) {
Winson Chungaafa03c2010-06-11 17:34:16 -0700676 final int hStartPadding = getLeftPadding();
677 final int vStartPadding = getTopPadding();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800678
679 result[0] = hStartPadding + cellX * (mCellWidth + mWidthGap);
680 result[1] = vStartPadding + cellY * (mCellHeight + mHeightGap);
681 }
682
Romain Guy84f296c2009-11-04 15:00:44 -0800683 int getCellWidth() {
684 return mCellWidth;
685 }
686
687 int getCellHeight() {
688 return mCellHeight;
689 }
690
Romain Guy1a304a12009-11-10 00:02:32 -0800691 int getLeftPadding() {
Winson Chungaafa03c2010-06-11 17:34:16 -0700692 return mLeftPadding;
Romain Guy1a304a12009-11-10 00:02:32 -0800693 }
694
695 int getTopPadding() {
Winson Chungaafa03c2010-06-11 17:34:16 -0700696 return mTopPadding;
Romain Guy1a304a12009-11-10 00:02:32 -0800697 }
698
699 int getRightPadding() {
Winson Chungaafa03c2010-06-11 17:34:16 -0700700 return mRightPadding;
Romain Guy1a304a12009-11-10 00:02:32 -0800701 }
702
703 int getBottomPadding() {
Winson Chungaafa03c2010-06-11 17:34:16 -0700704 return mBottomPadding;
Romain Guy1a304a12009-11-10 00:02:32 -0800705 }
706
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800707 @Override
708 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
709 // TODO: currently ignoring padding
Winson Chungaafa03c2010-06-11 17:34:16 -0700710
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800711 int widthSpecMode = MeasureSpec.getMode(widthMeasureSpec);
Winson Chungaafa03c2010-06-11 17:34:16 -0700712 int widthSpecSize = MeasureSpec.getSize(widthMeasureSpec);
713
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800714 int heightSpecMode = MeasureSpec.getMode(heightMeasureSpec);
715 int heightSpecSize = MeasureSpec.getSize(heightMeasureSpec);
Winson Chungaafa03c2010-06-11 17:34:16 -0700716
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800717 if (widthSpecMode == MeasureSpec.UNSPECIFIED || heightSpecMode == MeasureSpec.UNSPECIFIED) {
718 throw new RuntimeException("CellLayout cannot have UNSPECIFIED dimensions");
719 }
720
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800721 final int cellWidth = mCellWidth;
722 final int cellHeight = mCellHeight;
723
Adam Cohend22015c2010-07-26 22:02:18 -0700724 int numWidthGaps = mCountX - 1;
725 int numHeightGaps = mCountY - 1;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800726
Michael Jurka0280c3b2010-09-17 15:00:07 -0700727 int vSpaceLeft = heightSpecSize - mTopPadding - mBottomPadding - (cellHeight * mCountY);
Adam Cohend22015c2010-07-26 22:02:18 -0700728 mHeightGap = vSpaceLeft / numHeightGaps;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800729
Michael Jurka0280c3b2010-09-17 15:00:07 -0700730 int hSpaceLeft = widthSpecSize - mLeftPadding - mRightPadding - (cellWidth * mCountX);
Adam Cohend22015c2010-07-26 22:02:18 -0700731 mWidthGap = hSpaceLeft / numWidthGaps;
Winson Chungaafa03c2010-06-11 17:34:16 -0700732
Michael Jurka5f1c5092010-09-03 14:15:02 -0700733 // center it around the min gaps
734 int minGap = Math.min(mWidthGap, mHeightGap);
735 mWidthGap = mHeightGap = minGap;
736
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800737 int count = getChildCount();
738
739 for (int i = 0; i < count; i++) {
740 View child = getChildAt(i);
741 LayoutParams lp = (LayoutParams) child.getLayoutParams();
Winson Chungaafa03c2010-06-11 17:34:16 -0700742 lp.setup(cellWidth, cellHeight, mWidthGap, mHeightGap,
743 mLeftPadding, mTopPadding);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800744
Michael Jurka0280c3b2010-09-17 15:00:07 -0700745 int childWidthMeasureSpec = MeasureSpec.makeMeasureSpec(lp.width, MeasureSpec.EXACTLY);
Winson Chungaafa03c2010-06-11 17:34:16 -0700746 int childheightMeasureSpec = MeasureSpec.makeMeasureSpec(lp.height,
747 MeasureSpec.EXACTLY);
748
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800749 child.measure(childWidthMeasureSpec, childheightMeasureSpec);
750 }
Michael Jurka5f1c5092010-09-03 14:15:02 -0700751 if (widthSpecMode == MeasureSpec.AT_MOST) {
752 int newWidth = mLeftPadding + mRightPadding + (mCountX * cellWidth) +
753 ((mCountX - 1) * minGap);
754 int newHeight = mTopPadding + mBottomPadding + (mCountY * cellHeight) +
755 ((mCountY - 1) * minGap);
756 setMeasuredDimension(newWidth, newHeight);
757 } else if (widthSpecMode == MeasureSpec.EXACTLY) {
758 setMeasuredDimension(widthSpecSize, heightSpecSize);
759 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800760 }
761
Patrick Dubroyce34a972010-10-19 10:34:32 -0700762 /**
763 * Animate a child of this CellLayout into its current layout position.
764 * The position to animate from is given by the oldX and oldY values in its LayoutParams.
765 */
766 private void animateChildIntoPosition(final View child) {
767 final Resources res = getResources();
768 final ObjectAnimator anim = mDropAnim;
769 final CellLayout.LayoutParams lp = (CellLayout.LayoutParams) child.getLayoutParams();
770 final float startX = lp.oldX - lp.x;
771 final float startY = lp.oldY - lp.y;
772
773 // Calculate the duration of the animation based on the object's distance
774 final float dist = (float) Math.sqrt(startX*startX + startY*startY);
775 final float maxDist = (float) res.getInteger(R.integer.config_dropAnimMaxDist);
776 final int duration = (int) (res.getInteger(R.integer.config_dropAnimMaxDuration)
777 * mEaseOutInterpolator.getInterpolation(dist / maxDist));
778
779 anim.cancel(); // Make sure it's not already running
780 anim.setDuration(duration);
781 anim.setTarget(child);
782 anim.setPropertyName("translationX");
783 anim.setFloatValues(startX, 0);
784
785 anim.removeAllUpdateListeners();
786 anim.addUpdateListener(new AnimatorUpdateListener() {
787 public void onAnimationUpdate(ValueAnimator animation) {
788 // Set the value of translationY based on the current x value
789 final float translationX = (Float) anim.getAnimatedValue();
790 child.setTranslationY((startY / startX) * translationX);
791 }
792 });
793 anim.start();
794 }
795
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800796 @Override
Michael Jurka28750fb2010-09-24 17:43:49 -0700797 protected void onLayout(boolean changed, int l, int t, int r, int b) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800798 int count = getChildCount();
799
800 for (int i = 0; i < count; i++) {
Patrick Dubroyce34a972010-10-19 10:34:32 -0700801 final View child = getChildAt(i);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800802 if (child.getVisibility() != GONE) {
803
804 CellLayout.LayoutParams lp = (CellLayout.LayoutParams) child.getLayoutParams();
805
806 int childLeft = lp.x;
807 int childTop = lp.y;
808 child.layout(childLeft, childTop, childLeft + lp.width, childTop + lp.height);
Romain Guy84f296c2009-11-04 15:00:44 -0800809
810 if (lp.dropped) {
811 lp.dropped = false;
812
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700813 final int[] cellXY = mTmpCellXY;
Romain Guy06762ab2010-01-25 16:51:08 -0800814 getLocationOnScreen(cellXY);
Romain Guy84f296c2009-11-04 15:00:44 -0800815 mWallpaperManager.sendWallpaperCommand(getWindowToken(), "android.home.drop",
Romain Guy06762ab2010-01-25 16:51:08 -0800816 cellXY[0] + childLeft + lp.width / 2,
817 cellXY[1] + childTop + lp.height / 2, 0, null);
Patrick Dubroyce34a972010-10-19 10:34:32 -0700818
819 animateChildIntoPosition(child);
Romain Guy84f296c2009-11-04 15:00:44 -0800820 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800821 }
822 }
823 }
824
825 @Override
Michael Jurkadee05892010-07-27 10:01:56 -0700826 protected void onSizeChanged(int w, int h, int oldw, int oldh) {
827 super.onSizeChanged(w, h, oldw, oldh);
Michael Jurka18014792010-10-14 09:01:34 -0700828 mBackgroundRect.set(0, 0, w, h);
829 updateHoverRect();
Michael Jurkadee05892010-07-27 10:01:56 -0700830 }
831
832 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800833 protected void setChildrenDrawingCacheEnabled(boolean enabled) {
834 final int count = getChildCount();
835 for (int i = 0; i < count; i++) {
836 final View view = getChildAt(i);
837 view.setDrawingCacheEnabled(enabled);
838 // Update the drawing caches
Adam Powellfefa0ce2010-05-03 10:23:50 -0700839 view.buildDrawingCache(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800840 }
841 }
842
843 @Override
844 protected void setChildrenDrawnWithCacheEnabled(boolean enabled) {
845 super.setChildrenDrawnWithCacheEnabled(enabled);
846 }
847
Michael Jurka5f1c5092010-09-03 14:15:02 -0700848 public float getBackgroundAlpha() {
849 return mBackgroundAlpha;
Michael Jurkadee05892010-07-27 10:01:56 -0700850 }
851
Michael Jurka5f1c5092010-09-03 14:15:02 -0700852 public void setBackgroundAlpha(float alpha) {
853 mBackgroundAlpha = alpha;
Michael Jurka0142d492010-08-25 17:46:15 -0700854 invalidate();
Michael Jurkadee05892010-07-27 10:01:56 -0700855 }
856
Michael Jurka5f1c5092010-09-03 14:15:02 -0700857 // Need to return true to let the view system know we know how to handle alpha-- this is
858 // because when our children have an alpha of 0.0f, they are still rendering their "dimmed"
859 // versions
860 @Override
861 protected boolean onSetAlpha(int alpha) {
862 return true;
863 }
864
865 public void setAlpha(float alpha) {
866 setChildrenAlpha(alpha);
867 super.setAlpha(alpha);
868 }
869
Michael Jurkadee05892010-07-27 10:01:56 -0700870 private void setChildrenAlpha(float alpha) {
Michael Jurka0142d492010-08-25 17:46:15 -0700871 final int childCount = getChildCount();
872 for (int i = 0; i < childCount; i++) {
Michael Jurkadee05892010-07-27 10:01:56 -0700873 getChildAt(i).setAlpha(alpha);
874 }
875 }
876
Michael Jurka0280c3b2010-09-17 15:00:07 -0700877 private boolean isVacantIgnoring(
878 int originX, int originY, int spanX, int spanY, View ignoreView) {
879 if (ignoreView != null) {
880 markCellsAsUnoccupiedForView(ignoreView);
881 }
Michael Jurka28750fb2010-09-24 17:43:49 -0700882 boolean isVacant = true;
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700883 for (int i = 0; i < spanY; i++) {
884 if (!isRowEmpty(originY + i, originX, originX + spanX - 1, mOccupied)) {
Michael Jurka28750fb2010-09-24 17:43:49 -0700885 isVacant = false;
886 break;
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700887 }
888 }
Michael Jurka0280c3b2010-09-17 15:00:07 -0700889 if (ignoreView != null) {
890 markCellsAsOccupiedForView(ignoreView);
891 }
Michael Jurka28750fb2010-09-24 17:43:49 -0700892 return isVacant;
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700893 }
894
Michael Jurka0280c3b2010-09-17 15:00:07 -0700895 private boolean isVacant(int originX, int originY, int spanX, int spanY) {
896 return isVacantIgnoring(originX, originY, spanX, spanY, null);
897 }
898
Patrick Dubroy440c3602010-07-13 17:50:32 -0700899 public View getChildAt(int x, int y) {
900 final int count = getChildCount();
901 for (int i = 0; i < count; i++) {
902 View child = getChildAt(i);
903 LayoutParams lp = (LayoutParams) child.getLayoutParams();
904
905 if ((lp.cellX <= x) && (x < lp.cellX + lp.cellHSpan) &&
906 (lp.cellY <= y) && (y < lp.cellY + lp.cellHSpan)) {
907 return child;
908 }
909 }
910 return null;
911 }
912
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700913 /**
Patrick Dubroy8f86ddc2010-07-16 13:55:32 -0700914 * Estimate the size that a child with the given dimensions will take in the layout.
915 */
916 void estimateChildSize(int minWidth, int minHeight, int[] result) {
917 // Assuming it's placed at 0, 0, find where the bottom right cell will land
918 rectToCell(minWidth, minHeight, result);
919
920 // Then figure out the rect it will occupy
921 cellToRect(0, 0, result[0], result[1], mRectF);
922 result[0] = (int)mRectF.width();
923 result[1] = (int)mRectF.height();
924 }
925
926 /**
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700927 * Estimate where the top left cell of the dragged item will land if it is dropped.
928 *
929 * @param originX The X value of the top left corner of the item
930 * @param originY The Y value of the top left corner of the item
931 * @param spanX The number of horizontal cells that the item spans
932 * @param spanY The number of vertical cells that the item spans
933 * @param result The estimated drop cell X and Y.
934 */
935 void estimateDropCell(int originX, int originY, int spanX, int spanY, int[] result) {
Adam Cohend22015c2010-07-26 22:02:18 -0700936 final int countX = mCountX;
937 final int countY = mCountY;
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700938
Michael Jurkaa63c4522010-08-19 13:52:27 -0700939 // pointToCellRounded takes the top left of a cell but will pad that with
940 // cellWidth/2 and cellHeight/2 when finding the matching cell
941 pointToCellRounded(originX, originY, result);
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700942
943 // If the item isn't fully on this screen, snap to the edges
944 int rightOverhang = result[0] + spanX - countX;
945 if (rightOverhang > 0) {
946 result[0] -= rightOverhang; // Snap to right
947 }
948 result[0] = Math.max(0, result[0]); // Snap to left
949 int bottomOverhang = result[1] + spanY - countY;
950 if (bottomOverhang > 0) {
951 result[1] -= bottomOverhang; // Snap to bottom
952 }
953 result[1] = Math.max(0, result[1]); // Snap to top
954 }
955
Joe Onorato4be866d2010-10-10 11:26:02 -0700956 void visualizeDropLocation(
957 View v, Bitmap dragOutline, int originX, int originY, int spanX, int spanY) {
958
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -0700959 final int oldDragCellX = mDragCell[0];
960 final int oldDragCellY = mDragCell[1];
Joe Onorato4be866d2010-10-10 11:26:02 -0700961 final int[] nearest = findNearestVacantArea(originX, originY, spanX, spanY, v, mDragCell);
962 mDragCenter.set(originX + (v.getWidth() / 2), originY + (v.getHeight() / 2));
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700963
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -0700964 if (nearest != null && (nearest[0] != oldDragCellX || nearest[1] != oldDragCellY)) {
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700965 // Find the top left corner of the rect the object will occupy
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700966 final int[] topLeft = mTmpPoint;
967 cellToPoint(nearest[0], nearest[1], topLeft);
968
Joe Onorato4be866d2010-10-10 11:26:02 -0700969 int left = topLeft[0];
970 int top = topLeft[1];
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700971
Joe Onorato4be866d2010-10-10 11:26:02 -0700972 if (v.getParent() instanceof CellLayout) {
973 LayoutParams lp = (LayoutParams) v.getLayoutParams();
974 left += lp.leftMargin;
975 top += lp.topMargin;
976 }
Winson Chung150fbab2010-09-29 17:14:26 -0700977
Joe Onorato4be866d2010-10-10 11:26:02 -0700978 // Offsets due to the size difference between the View and the dragOutline
979 left += (v.getWidth() - dragOutline.getWidth()) / 2;
980 top += (v.getHeight() - dragOutline.getHeight()) / 2;
Winson Chung150fbab2010-09-29 17:14:26 -0700981
Joe Onorato4be866d2010-10-10 11:26:02 -0700982 final int oldIndex = mDragOutlineCurrent;
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -0700983 mDragOutlineAnims[oldIndex].animateOut();
984 mDragOutlineCurrent = (oldIndex + 1) % mDragOutlines.length;
Winson Chung150fbab2010-09-29 17:14:26 -0700985
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -0700986 mDragOutlines[mDragOutlineCurrent].set(left, top);
987 mDragOutlineAnims[mDragOutlineCurrent].setTag(dragOutline);
988 mDragOutlineAnims[mDragOutlineCurrent].animateIn();
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700989 }
Patrick Dubroy49250ad2010-10-08 15:33:52 -0700990
991 // If we are drawing crosshairs, the entire CellLayout needs to be invalidated
992 if (mCrosshairsDrawable != null) {
993 invalidate();
994 }
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700995 }
996
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800997 /**
Jeff Sharkey70864282009-04-07 21:08:40 -0700998 * Find a vacant area that will fit the given bounds nearest the requested
999 * cell location. Uses Euclidean distance to score multiple vacant areas.
Winson Chungaafa03c2010-06-11 17:34:16 -07001000 *
Romain Guy51afc022009-05-04 18:03:43 -07001001 * @param pixelX The X location at which you want to search for a vacant area.
1002 * @param pixelY The Y location at which you want to search for a vacant area.
Jeff Sharkey70864282009-04-07 21:08:40 -07001003 * @param spanX Horizontal span of the object.
1004 * @param spanY Vertical span of the object.
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001005 * @param result Array in which to place the result, or null (in which case a new array will
1006 * be allocated)
Jeff Sharkey70864282009-04-07 21:08:40 -07001007 * @return The X, Y cell of a vacant area that can contain this object,
1008 * nearest the requested location.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001009 */
Michael Jurka6a1435d2010-09-27 17:35:12 -07001010 int[] findNearestVacantArea(
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001011 int pixelX, int pixelY, int spanX, int spanY, int[] result) {
1012 return findNearestVacantArea(pixelX, pixelY, spanX, spanY, null, result);
Michael Jurka6a1435d2010-09-27 17:35:12 -07001013 }
Winson Chungaafa03c2010-06-11 17:34:16 -07001014
Michael Jurka6a1435d2010-09-27 17:35:12 -07001015 /**
1016 * Find a vacant area that will fit the given bounds nearest the requested
1017 * cell location. Uses Euclidean distance to score multiple vacant areas.
1018 *
1019 * @param pixelX The X location at which you want to search for a vacant area.
1020 * @param pixelY The Y location at which you want to search for a vacant area.
1021 * @param spanX Horizontal span of the object.
1022 * @param spanY Vertical span of the object.
Michael Jurka6a1435d2010-09-27 17:35:12 -07001023 * @param ignoreView Considers space occupied by this view as unoccupied
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001024 * @param result Previously returned value to possibly recycle.
Michael Jurka6a1435d2010-09-27 17:35:12 -07001025 * @return The X, Y cell of a vacant area that can contain this object,
1026 * nearest the requested location.
1027 */
1028 int[] findNearestVacantArea(
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001029 int pixelX, int pixelY, int spanX, int spanY, View ignoreView, int[] result) {
Michael Jurkac6ee42e2010-09-30 12:04:50 -07001030 // mark space take by ignoreView as available (method checks if ignoreView is null)
1031 markCellsAsUnoccupiedForView(ignoreView);
1032
Jeff Sharkey70864282009-04-07 21:08:40 -07001033 // Keep track of best-scoring drop area
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001034 final int[] bestXY = result != null ? result : new int[2];
Jeff Sharkey70864282009-04-07 21:08:40 -07001035 double bestDistance = Double.MAX_VALUE;
Winson Chungaafa03c2010-06-11 17:34:16 -07001036
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001037 final int countX = mCountX;
1038 final int countY = mCountY;
1039 final boolean[][] occupied = mOccupied;
1040
1041 for (int x = 0; x < countX - (spanX - 1); x++) {
Michael Jurkac28de512010-08-13 11:27:44 -07001042 inner:
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001043 for (int y = 0; y < countY - (spanY - 1); y++) {
Michael Jurkac28de512010-08-13 11:27:44 -07001044 for (int i = 0; i < spanX; i++) {
1045 for (int j = 0; j < spanY; j++) {
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001046 if (occupied[x + i][y + j]) {
Michael Jurkac28de512010-08-13 11:27:44 -07001047 // small optimization: we can skip to below the row we just found
1048 // an occupied cell
1049 y += j;
1050 continue inner;
1051 }
1052 }
1053 }
1054 final int[] cellXY = mTmpCellXY;
1055 cellToPoint(x, y, cellXY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001056
Michael Jurkac28de512010-08-13 11:27:44 -07001057 double distance = Math.sqrt(Math.pow(cellXY[0] - pixelX, 2)
1058 + Math.pow(cellXY[1] - pixelY, 2));
1059 if (distance <= bestDistance) {
1060 bestDistance = distance;
1061 bestXY[0] = x;
1062 bestXY[1] = y;
1063 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001064 }
1065 }
Michael Jurkac6ee42e2010-09-30 12:04:50 -07001066 // re-mark space taken by ignoreView as occupied
1067 markCellsAsOccupiedForView(ignoreView);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001068
Winson Chungaafa03c2010-06-11 17:34:16 -07001069 // Return null if no suitable location found
Jeff Sharkey70864282009-04-07 21:08:40 -07001070 if (bestDistance < Double.MAX_VALUE) {
1071 return bestXY;
1072 } else {
1073 return null;
1074 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001075 }
Winson Chungaafa03c2010-06-11 17:34:16 -07001076
Michael Jurka0280c3b2010-09-17 15:00:07 -07001077 boolean existsEmptyCell() {
1078 return findCellForSpan(null, 1, 1);
1079 }
1080
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001081 /**
Michael Jurka0280c3b2010-09-17 15:00:07 -07001082 * Finds the upper-left coordinate of the first rectangle in the grid that can
1083 * hold a cell of the specified dimensions. If intersectX and intersectY are not -1,
1084 * then this method will only return coordinates for rectangles that contain the cell
1085 * (intersectX, intersectY)
1086 *
1087 * @param cellXY The array that will contain the position of a vacant cell if such a cell
1088 * can be found.
1089 * @param spanX The horizontal span of the cell we want to find.
1090 * @param spanY The vertical span of the cell we want to find.
1091 *
1092 * @return True if a vacant cell of the specified dimension was found, false otherwise.
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001093 */
Michael Jurka0280c3b2010-09-17 15:00:07 -07001094 boolean findCellForSpan(int[] cellXY, int spanX, int spanY) {
1095 return findCellForSpanThatIntersectsIgnoring(cellXY, spanX, spanY, -1, -1, null);
1096 }
1097
1098 /**
1099 * Like above, but ignores any cells occupied by the item "ignoreView"
1100 *
1101 * @param cellXY The array that will contain the position of a vacant cell if such a cell
1102 * can be found.
1103 * @param spanX The horizontal span of the cell we want to find.
1104 * @param spanY The vertical span of the cell we want to find.
1105 * @param ignoreView The home screen item we should treat as not occupying any space
1106 * @return
1107 */
1108 boolean findCellForSpanIgnoring(int[] cellXY, int spanX, int spanY, View ignoreView) {
1109 return findCellForSpanThatIntersectsIgnoring(cellXY, spanX, spanY, -1, -1, ignoreView);
1110 }
1111
1112 /**
1113 * Like above, but if intersectX and intersectY are not -1, then this method will try to
1114 * return coordinates for rectangles that contain the cell [intersectX, intersectY]
1115 *
1116 * @param spanX The horizontal span of the cell we want to find.
1117 * @param spanY The vertical span of the cell we want to find.
1118 * @param ignoreView The home screen item we should treat as not occupying any space
1119 * @param intersectX The X coordinate of the cell that we should try to overlap
1120 * @param intersectX The Y coordinate of the cell that we should try to overlap
1121 *
1122 * @return True if a vacant cell of the specified dimension was found, false otherwise.
1123 */
1124 boolean findCellForSpanThatIntersects(int[] cellXY, int spanX, int spanY,
1125 int intersectX, int intersectY) {
1126 return findCellForSpanThatIntersectsIgnoring(
1127 cellXY, spanX, spanY, intersectX, intersectY, null);
1128 }
1129
1130 /**
1131 * The superset of the above two methods
1132 */
1133 boolean findCellForSpanThatIntersectsIgnoring(int[] cellXY, int spanX, int spanY,
1134 int intersectX, int intersectY, View ignoreView) {
Michael Jurkac6ee42e2010-09-30 12:04:50 -07001135 // mark space take by ignoreView as available (method checks if ignoreView is null)
1136 markCellsAsUnoccupiedForView(ignoreView);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001137
Michael Jurka28750fb2010-09-24 17:43:49 -07001138 boolean foundCell = false;
Michael Jurka0280c3b2010-09-17 15:00:07 -07001139 while (true) {
1140 int startX = 0;
1141 if (intersectX >= 0) {
1142 startX = Math.max(startX, intersectX - (spanX - 1));
1143 }
1144 int endX = mCountX - (spanX - 1);
1145 if (intersectX >= 0) {
1146 endX = Math.min(endX, intersectX + (spanX - 1) + (spanX == 1 ? 1 : 0));
1147 }
1148 int startY = 0;
1149 if (intersectY >= 0) {
1150 startY = Math.max(startY, intersectY - (spanY - 1));
1151 }
1152 int endY = mCountY - (spanY - 1);
1153 if (intersectY >= 0) {
1154 endY = Math.min(endY, intersectY + (spanY - 1) + (spanY == 1 ? 1 : 0));
1155 }
1156
1157 for (int x = startX; x < endX; x++) {
1158 inner:
1159 for (int y = startY; y < endY; y++) {
1160 for (int i = 0; i < spanX; i++) {
1161 for (int j = 0; j < spanY; j++) {
1162 if (mOccupied[x + i][y + j]) {
1163 // small optimization: we can skip to below the row we just found
1164 // an occupied cell
1165 y += j;
1166 continue inner;
1167 }
1168 }
1169 }
1170 if (cellXY != null) {
1171 cellXY[0] = x;
1172 cellXY[1] = y;
1173 }
Michael Jurka28750fb2010-09-24 17:43:49 -07001174 foundCell = true;
1175 break;
Michael Jurka0280c3b2010-09-17 15:00:07 -07001176 }
1177 }
1178 if (intersectX == -1 && intersectY == -1) {
1179 break;
1180 } else {
1181 // if we failed to find anything, try again but without any requirements of
1182 // intersecting
1183 intersectX = -1;
1184 intersectY = -1;
1185 continue;
1186 }
1187 }
1188
Michael Jurkac6ee42e2010-09-30 12:04:50 -07001189 // re-mark space taken by ignoreView as occupied
1190 markCellsAsOccupiedForView(ignoreView);
Michael Jurka28750fb2010-09-24 17:43:49 -07001191 return foundCell;
Michael Jurka0280c3b2010-09-17 15:00:07 -07001192 }
1193
1194 /**
1195 * Called when drag has left this CellLayout or has been completed (successfully or not)
1196 */
1197 void onDragExit() {
Joe Onorato4be866d2010-10-10 11:26:02 -07001198 // This can actually be called when we aren't in a drag, e.g. when adding a new
1199 // item to this layout via the customize drawer.
1200 // Guard against that case.
1201 if (mDragging) {
1202 mDragging = false;
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001203
Joe Onorato4be866d2010-10-10 11:26:02 -07001204 // Fade out the drag indicators
1205 if (mCrosshairsAnimator != null) {
1206 mCrosshairsAnimator.animateOut();
1207 }
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001208 }
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -07001209
1210 // Invalidate the drag data
1211 mDragCell[0] = -1;
1212 mDragCell[1] = -1;
1213 mDragOutlineAnims[mDragOutlineCurrent].animateOut();
1214 mDragOutlineCurrent = (mDragOutlineCurrent + 1) % mDragOutlineAnims.length;
1215
1216 setHover(false);
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001217 }
1218
1219 /**
Winson Chungaafa03c2010-06-11 17:34:16 -07001220 * Mark a child as having been dropped.
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001221 * At the beginning of the drag operation, the child may have been on another
Patrick Dubroyce34a972010-10-19 10:34:32 -07001222 * screen, but it is re-parented before this method is called.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001223 *
1224 * @param child The child that is being dropped
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001225 */
Winson Chungaafa03c2010-06-11 17:34:16 -07001226 void onDropChild(View child) {
Romain Guyd94533d2009-08-17 10:01:15 -07001227 if (child != null) {
1228 LayoutParams lp = (LayoutParams) child.getLayoutParams();
Romain Guyd94533d2009-08-17 10:01:15 -07001229 lp.isDragging = false;
Romain Guy84f296c2009-11-04 15:00:44 -08001230 lp.dropped = true;
Patrick Dubroyce34a972010-10-19 10:34:32 -07001231 child.setVisibility(View.VISIBLE);
Romain Guyd94533d2009-08-17 10:01:15 -07001232 child.requestLayout();
Romain Guyd94533d2009-08-17 10:01:15 -07001233 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001234 }
1235
1236 void onDropAborted(View child) {
1237 if (child != null) {
Patrick Dubroyce34a972010-10-19 10:34:32 -07001238 LayoutParams lp = (LayoutParams) child.getLayoutParams();
1239 lp.isDragging = false;
1240 child.setVisibility(View.VISIBLE);
1241 animateChildIntoPosition(child);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001242 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001243 }
1244
1245 /**
1246 * Start dragging the specified child
Winson Chungaafa03c2010-06-11 17:34:16 -07001247 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001248 * @param child The child that is being dragged
1249 */
1250 void onDragChild(View child) {
1251 LayoutParams lp = (LayoutParams) child.getLayoutParams();
1252 lp.isDragging = true;
Patrick Dubroyce34a972010-10-19 10:34:32 -07001253 child.setVisibility(View.GONE);
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001254 }
1255
1256 /**
1257 * A drag event has begun over this layout.
1258 * It may have begun over this layout (in which case onDragChild is called first),
1259 * or it may have begun on another layout.
1260 */
1261 void onDragEnter(View dragView) {
Patrick Dubroyfe6bd872010-10-13 17:32:10 -07001262 if (!mDragging) {
Patrick Dubroyfe6bd872010-10-13 17:32:10 -07001263 // Fade in the drag indicators
1264 if (mCrosshairsAnimator != null) {
1265 mCrosshairsAnimator.animateIn();
1266 }
Joe Onorato4be866d2010-10-10 11:26:02 -07001267 }
1268 mDragging = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001269 }
Winson Chungaafa03c2010-06-11 17:34:16 -07001270
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001271 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001272 * Computes a bounding rectangle for a range of cells
Winson Chungaafa03c2010-06-11 17:34:16 -07001273 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001274 * @param cellX X coordinate of upper left corner expressed as a cell position
1275 * @param cellY Y coordinate of upper left corner expressed as a cell position
Winson Chungaafa03c2010-06-11 17:34:16 -07001276 * @param cellHSpan Width in cells
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001277 * @param cellVSpan Height in cells
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001278 * @param resultRect Rect into which to put the results
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001279 */
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001280 public void cellToRect(int cellX, int cellY, int cellHSpan, int cellVSpan, RectF resultRect) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001281 final int cellWidth = mCellWidth;
1282 final int cellHeight = mCellHeight;
1283 final int widthGap = mWidthGap;
1284 final int heightGap = mHeightGap;
Winson Chungaafa03c2010-06-11 17:34:16 -07001285
1286 final int hStartPadding = getLeftPadding();
1287 final int vStartPadding = getTopPadding();
1288
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001289 int width = cellHSpan * cellWidth + ((cellHSpan - 1) * widthGap);
1290 int height = cellVSpan * cellHeight + ((cellVSpan - 1) * heightGap);
1291
1292 int x = hStartPadding + cellX * (cellWidth + widthGap);
1293 int y = vStartPadding + cellY * (cellHeight + heightGap);
Winson Chungaafa03c2010-06-11 17:34:16 -07001294
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001295 resultRect.set(x, y, x + width, y + height);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001296 }
Winson Chungaafa03c2010-06-11 17:34:16 -07001297
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001298 /**
Winson Chungaafa03c2010-06-11 17:34:16 -07001299 * Computes the required horizontal and vertical cell spans to always
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001300 * fit the given rectangle.
Winson Chungaafa03c2010-06-11 17:34:16 -07001301 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001302 * @param width Width in pixels
1303 * @param height Height in pixels
Patrick Dubroy8f86ddc2010-07-16 13:55:32 -07001304 * @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 -08001305 */
Patrick Dubroy8f86ddc2010-07-16 13:55:32 -07001306 public int[] rectToCell(int width, int height, int[] result) {
Michael Jurka9987a5c2010-10-08 16:58:12 -07001307 return rectToCell(getResources(), width, height, result);
1308 }
1309
1310 public static int[] rectToCell(Resources resources, int width, int height, int[] result) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001311 // Always assume we're working with the smallest span to make sure we
1312 // reserve enough space in both orientations.
Joe Onorato79e56262009-09-21 15:23:04 -04001313 int actualWidth = resources.getDimensionPixelSize(R.dimen.workspace_cell_width);
1314 int actualHeight = resources.getDimensionPixelSize(R.dimen.workspace_cell_height);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001315 int smallerSize = Math.min(actualWidth, actualHeight);
Joe Onorato79e56262009-09-21 15:23:04 -04001316
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001317 // Always round up to next largest cell
1318 int spanX = (width + smallerSize) / smallerSize;
1319 int spanY = (height + smallerSize) / smallerSize;
Joe Onorato79e56262009-09-21 15:23:04 -04001320
Patrick Dubroy8f86ddc2010-07-16 13:55:32 -07001321 if (result == null) {
1322 return new int[] { spanX, spanY };
1323 }
1324 result[0] = spanX;
1325 result[1] = spanY;
1326 return result;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001327 }
1328
1329 /**
1330 * Find the first vacant cell, if there is one.
1331 *
1332 * @param vacant Holds the x and y coordinate of the vacant cell
1333 * @param spanX Horizontal cell span.
1334 * @param spanY Vertical cell span.
Winson Chungaafa03c2010-06-11 17:34:16 -07001335 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001336 * @return True if a vacant cell was found
1337 */
1338 public boolean getVacantCell(int[] vacant, int spanX, int spanY) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001339
Michael Jurka0280c3b2010-09-17 15:00:07 -07001340 return findVacantCell(vacant, spanX, spanY, mCountX, mCountY, mOccupied);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001341 }
1342
1343 static boolean findVacantCell(int[] vacant, int spanX, int spanY,
1344 int xCount, int yCount, boolean[][] occupied) {
1345
1346 for (int x = 0; x < xCount; x++) {
1347 for (int y = 0; y < yCount; y++) {
1348 boolean available = !occupied[x][y];
1349out: for (int i = x; i < x + spanX - 1 && x < xCount; i++) {
1350 for (int j = y; j < y + spanY - 1 && y < yCount; j++) {
1351 available = available && !occupied[i][j];
1352 if (!available) break out;
1353 }
1354 }
1355
1356 if (available) {
1357 vacant[0] = x;
1358 vacant[1] = y;
1359 return true;
1360 }
1361 }
1362 }
1363
1364 return false;
1365 }
1366
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001367 /**
1368 * Update the array of occupied cells (mOccupied), and return a flattened copy of the array.
1369 */
1370 boolean[] getOccupiedCellsFlattened() {
Adam Cohend22015c2010-07-26 22:02:18 -07001371 final int xCount = mCountX;
1372 final int yCount = mCountY;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001373 final boolean[][] occupied = mOccupied;
1374
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001375 final boolean[] flat = new boolean[xCount * yCount];
1376 for (int y = 0; y < yCount; y++) {
1377 for (int x = 0; x < xCount; x++) {
1378 flat[y * xCount + x] = occupied[x][y];
1379 }
1380 }
1381
1382 return flat;
1383 }
1384
Michael Jurka0280c3b2010-09-17 15:00:07 -07001385 private void clearOccupiedCells() {
1386 for (int x = 0; x < mCountX; x++) {
1387 for (int y = 0; y < mCountY; y++) {
1388 mOccupied[x][y] = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001389 }
1390 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001391 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001392
Michael Jurka0280c3b2010-09-17 15:00:07 -07001393 public void onMove(View view, int newCellX, int newCellY) {
1394 LayoutParams lp = (LayoutParams) view.getLayoutParams();
1395 markCellsAsUnoccupiedForView(view);
1396 markCellsForView(newCellX, newCellY, lp.cellHSpan, lp.cellVSpan, true);
1397 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001398
Michael Jurka0280c3b2010-09-17 15:00:07 -07001399 private void markCellsAsOccupiedForView(View view) {
Michael Jurkac6ee42e2010-09-30 12:04:50 -07001400 if (view == null || view.getParent() != this) return;
Michael Jurka0280c3b2010-09-17 15:00:07 -07001401 LayoutParams lp = (LayoutParams) view.getLayoutParams();
1402 markCellsForView(lp.cellX, lp.cellY, lp.cellHSpan, lp.cellVSpan, true);
1403 }
1404
1405 private void markCellsAsUnoccupiedForView(View view) {
Michael Jurkac6ee42e2010-09-30 12:04:50 -07001406 if (view == null || view.getParent() != this) return;
Michael Jurka0280c3b2010-09-17 15:00:07 -07001407 LayoutParams lp = (LayoutParams) view.getLayoutParams();
1408 markCellsForView(lp.cellX, lp.cellY, lp.cellHSpan, lp.cellVSpan, false);
1409 }
1410
1411 private void markCellsForView(int cellX, int cellY, int spanX, int spanY, boolean value) {
1412 for (int x = cellX; x < cellX + spanX && x < mCountX; x++) {
1413 for (int y = cellY; y < cellY + spanY && y < mCountY; y++) {
1414 mOccupied[x][y] = value;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001415 }
1416 }
1417 }
1418
1419 @Override
1420 public ViewGroup.LayoutParams generateLayoutParams(AttributeSet attrs) {
1421 return new CellLayout.LayoutParams(getContext(), attrs);
1422 }
1423
1424 @Override
1425 protected boolean checkLayoutParams(ViewGroup.LayoutParams p) {
1426 return p instanceof CellLayout.LayoutParams;
1427 }
1428
1429 @Override
1430 protected ViewGroup.LayoutParams generateLayoutParams(ViewGroup.LayoutParams p) {
1431 return new CellLayout.LayoutParams(p);
1432 }
1433
Winson Chungaafa03c2010-06-11 17:34:16 -07001434 public static class CellLayoutAnimationController extends LayoutAnimationController {
1435 public CellLayoutAnimationController(Animation animation, float delay) {
1436 super(animation, delay);
1437 }
1438
1439 @Override
1440 protected long getDelayForView(View view) {
1441 return (int) (Math.random() * 150);
1442 }
1443 }
1444
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001445 public static class LayoutParams extends ViewGroup.MarginLayoutParams {
1446 /**
1447 * Horizontal location of the item in the grid.
1448 */
1449 @ViewDebug.ExportedProperty
1450 public int cellX;
1451
1452 /**
1453 * Vertical location of the item in the grid.
1454 */
1455 @ViewDebug.ExportedProperty
1456 public int cellY;
1457
1458 /**
1459 * Number of cells spanned horizontally by the item.
1460 */
1461 @ViewDebug.ExportedProperty
1462 public int cellHSpan;
1463
1464 /**
1465 * Number of cells spanned vertically by the item.
1466 */
1467 @ViewDebug.ExportedProperty
1468 public int cellVSpan;
Winson Chungaafa03c2010-06-11 17:34:16 -07001469
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001470 /**
1471 * Is this item currently being dragged
1472 */
1473 public boolean isDragging;
1474
1475 // X coordinate of the view in the layout.
1476 @ViewDebug.ExportedProperty
1477 int x;
1478 // Y coordinate of the view in the layout.
1479 @ViewDebug.ExportedProperty
1480 int y;
1481
Patrick Dubroyce34a972010-10-19 10:34:32 -07001482 /**
1483 * The old X coordinate of this item, relative to its current parent.
1484 * Used to animate the item into its new position.
1485 */
1486 int oldX;
1487
1488 /**
1489 * The old Y coordinate of this item, relative to its current parent.
1490 * Used to animate the item into its new position.
1491 */
1492 int oldY;
1493
Romain Guy84f296c2009-11-04 15:00:44 -08001494 boolean dropped;
Romain Guyfcb9e712009-10-02 16:06:52 -07001495
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001496 public LayoutParams(Context c, AttributeSet attrs) {
1497 super(c, attrs);
1498 cellHSpan = 1;
1499 cellVSpan = 1;
1500 }
1501
1502 public LayoutParams(ViewGroup.LayoutParams source) {
1503 super(source);
1504 cellHSpan = 1;
1505 cellVSpan = 1;
1506 }
Winson Chungaafa03c2010-06-11 17:34:16 -07001507
1508 public LayoutParams(LayoutParams source) {
1509 super(source);
1510 this.cellX = source.cellX;
1511 this.cellY = source.cellY;
1512 this.cellHSpan = source.cellHSpan;
1513 this.cellVSpan = source.cellVSpan;
1514 }
1515
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001516 public LayoutParams(int cellX, int cellY, int cellHSpan, int cellVSpan) {
Romain Guy8f19cdd2010-01-08 15:07:00 -08001517 super(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001518 this.cellX = cellX;
1519 this.cellY = cellY;
1520 this.cellHSpan = cellHSpan;
1521 this.cellVSpan = cellVSpan;
1522 }
1523
1524 public void setup(int cellWidth, int cellHeight, int widthGap, int heightGap,
1525 int hStartPadding, int vStartPadding) {
Winson Chungaafa03c2010-06-11 17:34:16 -07001526
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001527 final int myCellHSpan = cellHSpan;
1528 final int myCellVSpan = cellVSpan;
1529 final int myCellX = cellX;
1530 final int myCellY = cellY;
Winson Chungaafa03c2010-06-11 17:34:16 -07001531
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001532 width = myCellHSpan * cellWidth + ((myCellHSpan - 1) * widthGap) -
1533 leftMargin - rightMargin;
1534 height = myCellVSpan * cellHeight + ((myCellVSpan - 1) * heightGap) -
1535 topMargin - bottomMargin;
1536
1537 x = hStartPadding + myCellX * (cellWidth + widthGap) + leftMargin;
1538 y = vStartPadding + myCellY * (cellHeight + heightGap) + topMargin;
1539 }
Winson Chungaafa03c2010-06-11 17:34:16 -07001540
1541 public String toString() {
1542 return "(" + this.cellX + ", " + this.cellY + ")";
1543 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001544 }
1545
Michael Jurka0280c3b2010-09-17 15:00:07 -07001546 // This class stores info for two purposes:
1547 // 1. When dragging items (mDragInfo in Workspace), we store the View, its cellX & cellY,
1548 // its spanX, spanY, and the screen it is on
1549 // 2. When long clicking on an empty cell in a CellLayout, we save information about the
1550 // cellX and cellY coordinates and which page was clicked. We then set this as a tag on
1551 // the CellLayout that was long clicked
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001552 static final class CellInfo implements ContextMenu.ContextMenuInfo {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001553 View cell;
Michael Jurkaa63c4522010-08-19 13:52:27 -07001554 int cellX = -1;
1555 int cellY = -1;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001556 int spanX;
1557 int spanY;
1558 int screen;
1559 boolean valid;
1560
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001561 @Override
1562 public String toString() {
Winson Chungaafa03c2010-06-11 17:34:16 -07001563 return "Cell[view=" + (cell == null ? "null" : cell.getClass())
1564 + ", x=" + cellX + ", y=" + cellY + "]";
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001565 }
1566 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001567}