blob: b7491bae1e1eff6f2c79642368dc3ccca6d5a213 [file] [log] [blame]
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001/*
2 * Copyright (C) 2008 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Joe Onoratoa5902522009-07-30 13:37:37 -070017package com.android.launcher2;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080018
Winson Chunga9abd0e2010-10-27 17:18:37 -070019import java.util.Arrays;
Winson Chungaafa03c2010-06-11 17:34:16 -070020
Joe Onorato4be866d2010-10-10 11:26:02 -070021import android.animation.Animator;
22import android.animation.AnimatorListenerAdapter;
Michael Jurka18014792010-10-14 09:01:34 -070023import android.animation.AnimatorSet;
24import android.animation.ObjectAnimator;
Chet Haase00397b12010-10-07 11:13:10 -070025import android.animation.TimeInterpolator;
Patrick Dubroyde7658b2010-09-27 11:15:43 -070026import android.animation.ValueAnimator;
27import android.animation.ValueAnimator.AnimatorUpdateListener;
Winson Chungaafa03c2010-06-11 17:34:16 -070028import android.app.WallpaperManager;
Winson Chunga9abd0e2010-10-27 17:18:37 -070029import android.content.ClipDescription;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080030import android.content.Context;
Joe Onorato79e56262009-09-21 15:23:04 -040031import android.content.res.Resources;
Winson Chungaafa03c2010-06-11 17:34:16 -070032import android.content.res.TypedArray;
Joe Onorato4be866d2010-10-10 11:26:02 -070033import android.graphics.Bitmap;
Winson Chungaafa03c2010-06-11 17:34:16 -070034import android.graphics.Canvas;
Joe Onorato4be866d2010-10-10 11:26:02 -070035import android.graphics.Paint;
Patrick Dubroyde7658b2010-09-27 11:15:43 -070036import android.graphics.Point;
37import android.graphics.PointF;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080038import android.graphics.Rect;
39import android.graphics.RectF;
Michael Jurka18014792010-10-14 09:01:34 -070040import android.graphics.Region;
Patrick Dubroy6569f2c2010-07-12 14:25:18 -070041import android.graphics.drawable.Drawable;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080042import android.util.AttributeSet;
Joe Onorato4be866d2010-10-10 11:26:02 -070043import android.util.Log;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080044import android.view.ContextMenu;
Winson Chunga9abd0e2010-10-27 17:18:37 -070045import android.view.DragEvent;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080046import android.view.MotionEvent;
47import android.view.View;
48import android.view.ViewDebug;
49import android.view.ViewGroup;
Winson Chungaafa03c2010-06-11 17:34:16 -070050import android.view.animation.Animation;
Winson Chung150fbab2010-09-29 17:14:26 -070051import android.view.animation.DecelerateInterpolator;
Winson Chungaafa03c2010-06-11 17:34:16 -070052import android.view.animation.LayoutAnimationController;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080053
Winson Chunga9abd0e2010-10-27 17:18:37 -070054import com.android.launcher.R;
Patrick Dubroy8e58e912010-10-14 13:21:48 -070055
Adam Cohenf34bab52010-09-30 14:11:56 -070056public class CellLayout extends ViewGroup implements Dimmable {
Winson Chungaafa03c2010-06-11 17:34:16 -070057 static final String TAG = "CellLayout";
58
The Android Open Source Project31dd5032009-03-03 19:32:27 -080059 private int mCellWidth;
60 private int mCellHeight;
Winson Chungaafa03c2010-06-11 17:34:16 -070061
Winson Chungaafa03c2010-06-11 17:34:16 -070062 private int mLeftPadding;
63 private int mRightPadding;
64 private int mTopPadding;
65 private int mBottomPadding;
66
Adam Cohend22015c2010-07-26 22:02:18 -070067 private int mCountX;
68 private int mCountY;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080069
70 private int mWidthGap;
71 private int mHeightGap;
72
73 private final Rect mRect = new Rect();
Patrick Dubroy8f86ddc2010-07-16 13:55:32 -070074 private final RectF mRectF = new RectF();
The Android Open Source Project31dd5032009-03-03 19:32:27 -080075 private final CellInfo mCellInfo = new CellInfo();
Winson Chungaafa03c2010-06-11 17:34:16 -070076
Patrick Dubroyde7658b2010-09-27 11:15:43 -070077 // These are temporary variables to prevent having to allocate a new object just to
78 // return an (x, y) value from helper functions. Do NOT use them to maintain other state.
Patrick Dubroy6569f2c2010-07-12 14:25:18 -070079 private final int[] mTmpCellXY = new int[2];
Patrick Dubroyde7658b2010-09-27 11:15:43 -070080 private final int[] mTmpPoint = new int[2];
81 private final PointF mTmpPointF = new PointF();
Patrick Dubroy6569f2c2010-07-12 14:25:18 -070082
The Android Open Source Project31dd5032009-03-03 19:32:27 -080083 boolean[][] mOccupied;
84
Michael Jurkadee05892010-07-27 10:01:56 -070085 private OnTouchListener mInterceptTouchListener;
86
Michael Jurka5f1c5092010-09-03 14:15:02 -070087 private float mBackgroundAlpha;
Adam Cohen1b0aaac2010-10-28 11:11:18 -070088 private float mBackgroundAlphaMultiplier = 1.0f;
Adam Cohenf34bab52010-09-30 14:11:56 -070089
Michael Jurka5f1c5092010-09-03 14:15:02 -070090 private Drawable mBackground;
Adam Cohenf34bab52010-09-30 14:11:56 -070091 private Drawable mBackgroundMini;
92 private Drawable mBackgroundMiniHover;
Patrick Dubroy1262e362010-10-06 15:49:50 -070093 private Drawable mBackgroundHover;
Michael Jurka3e7c7632010-10-02 16:01:03 -070094 private Drawable mBackgroundMiniAcceptsDrops;
Michael Jurka18014792010-10-14 09:01:34 -070095 private Rect mBackgroundRect;
96 private Rect mHoverRect;
97 private float mHoverScale;
98 private float mHoverAlpha;
Michael Jurka3e7c7632010-10-02 16:01:03 -070099 private boolean mAcceptsDrops;
Patrick Dubroy1262e362010-10-06 15:49:50 -0700100
101 // If we're actively dragging something over this screen, mHover is true
Michael Jurkaa63c4522010-08-19 13:52:27 -0700102 private boolean mHover = false;
Michael Jurkadee05892010-07-27 10:01:56 -0700103
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700104 private final Point mDragCenter = new Point();
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700105
Winson Chung150fbab2010-09-29 17:14:26 -0700106 // These arrays are used to implement the drag visualization on x-large screens.
Joe Onorato4be866d2010-10-10 11:26:02 -0700107 // They are used as circular arrays, indexed by mDragOutlineCurrent.
108 private Point[] mDragOutlines = new Point[8];
Chet Haase472b2812010-10-14 07:02:04 -0700109 private float[] mDragOutlineAlphas = new float[mDragOutlines.length];
Joe Onorato4be866d2010-10-10 11:26:02 -0700110 private InterruptibleInOutAnimator[] mDragOutlineAnims =
111 new InterruptibleInOutAnimator[mDragOutlines.length];
Winson Chung150fbab2010-09-29 17:14:26 -0700112
113 // Used as an index into the above 3 arrays; indicates which is the most current value.
Joe Onorato4be866d2010-10-10 11:26:02 -0700114 private int mDragOutlineCurrent = 0;
Patrick Dubroy8e58e912010-10-14 13:21:48 -0700115 private final Paint mDragOutlinePaint = new Paint();
Winson Chung150fbab2010-09-29 17:14:26 -0700116
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700117 private Drawable mCrosshairsDrawable = null;
Patrick Dubroy49250ad2010-10-08 15:33:52 -0700118 private InterruptibleInOutAnimator mCrosshairsAnimator = null;
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700119 private float mCrosshairsVisibility = 0.0f;
120
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700121 // When a drag operation is in progress, holds the nearest cell to the touch point
122 private final int[] mDragCell = new int[2];
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800123
Winson Chungaafa03c2010-06-11 17:34:16 -0700124 private final WallpaperManager mWallpaperManager;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800125
Joe Onorato4be866d2010-10-10 11:26:02 -0700126 private boolean mDragging = false;
127
Patrick Dubroya9442152010-10-25 11:41:41 -0700128 private ValueAnimator mDropAnim;
Patrick Dubroyce34a972010-10-19 10:34:32 -0700129 private TimeInterpolator mEaseOutInterpolator;
130
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800131 public CellLayout(Context context) {
132 this(context, null);
133 }
134
135 public CellLayout(Context context, AttributeSet attrs) {
136 this(context, attrs, 0);
137 }
138
139 public CellLayout(Context context, AttributeSet attrs, int defStyle) {
140 super(context, attrs, defStyle);
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700141
142 // A ViewGroup usually does not draw, but CellLayout needs to draw a rectangle to show
143 // the user where a dragged item will land when dropped.
144 setWillNotDraw(false);
Michael Jurkaa63c4522010-08-19 13:52:27 -0700145
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800146 TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.CellLayout, defStyle, 0);
147
148 mCellWidth = a.getDimensionPixelSize(R.styleable.CellLayout_cellWidth, 10);
149 mCellHeight = a.getDimensionPixelSize(R.styleable.CellLayout_cellHeight, 10);
Winson Chungece7f5b2010-10-22 14:54:12 -0700150 mWidthGap = a.getDimensionPixelSize(R.styleable.CellLayout_widthGap, -1);
151 mHeightGap = a.getDimensionPixelSize(R.styleable.CellLayout_heightGap, -1);
Winson Chungaafa03c2010-06-11 17:34:16 -0700152
Adam Cohend22015c2010-07-26 22:02:18 -0700153 mLeftPadding =
154 a.getDimensionPixelSize(R.styleable.CellLayout_xAxisStartPadding, 10);
155 mRightPadding =
156 a.getDimensionPixelSize(R.styleable.CellLayout_xAxisEndPadding, 10);
157 mTopPadding =
158 a.getDimensionPixelSize(R.styleable.CellLayout_yAxisStartPadding, 10);
159 mBottomPadding =
160 a.getDimensionPixelSize(R.styleable.CellLayout_yAxisEndPadding, 10);
Winson Chungaafa03c2010-06-11 17:34:16 -0700161
Adam Cohend22015c2010-07-26 22:02:18 -0700162 mCountX = LauncherModel.getCellCountX();
163 mCountY = LauncherModel.getCellCountY();
Michael Jurka0280c3b2010-09-17 15:00:07 -0700164 mOccupied = new boolean[mCountX][mCountY];
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800165
166 a.recycle();
167
168 setAlwaysDrawnWithCacheEnabled(false);
169
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700170 mWallpaperManager = WallpaperManager.getInstance(context);
171
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700172 final Resources res = getResources();
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700173
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700174 if (LauncherApplication.isScreenXLarge()) {
Winson Chung150fbab2010-09-29 17:14:26 -0700175 mBackgroundMini = res.getDrawable(R.drawable.mini_home_screen_bg);
Adam Cohenf34bab52010-09-30 14:11:56 -0700176 mBackgroundMini.setFilterBitmap(true);
Winson Chung150fbab2010-09-29 17:14:26 -0700177 mBackground = res.getDrawable(R.drawable.home_screen_bg);
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700178 mBackground.setFilterBitmap(true);
Winson Chung150fbab2010-09-29 17:14:26 -0700179 mBackgroundMiniHover = res.getDrawable(R.drawable.mini_home_screen_bg_hover);
Adam Cohenf34bab52010-09-30 14:11:56 -0700180 mBackgroundMiniHover.setFilterBitmap(true);
Patrick Dubroy1262e362010-10-06 15:49:50 -0700181 mBackgroundHover = res.getDrawable(R.drawable.home_screen_bg_hover);
182 mBackgroundHover.setFilterBitmap(true);
Michael Jurka3e7c7632010-10-02 16:01:03 -0700183 mBackgroundMiniAcceptsDrops = res.getDrawable(
184 R.drawable.mini_home_screen_bg_accepts_drops);
185 mBackgroundMiniAcceptsDrops.setFilterBitmap(true);
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700186 }
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700187
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700188 // Initialize the data structures used for the drag visualization.
Winson Chung150fbab2010-09-29 17:14:26 -0700189
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700190 mCrosshairsDrawable = res.getDrawable(R.drawable.gardening_crosshairs);
Patrick Dubroyce34a972010-10-19 10:34:32 -0700191 mEaseOutInterpolator = new DecelerateInterpolator(2.5f); // Quint ease out
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700192
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700193 // Set up the animation for fading the crosshairs in and out
194 int animDuration = res.getInteger(R.integer.config_crosshairsFadeInTime);
Patrick Dubroy49250ad2010-10-08 15:33:52 -0700195 mCrosshairsAnimator = new InterruptibleInOutAnimator(animDuration, 0.0f, 1.0f);
Chet Haase472b2812010-10-14 07:02:04 -0700196 mCrosshairsAnimator.getAnimator().addUpdateListener(new AnimatorUpdateListener() {
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700197 public void onAnimationUpdate(ValueAnimator animation) {
198 mCrosshairsVisibility = ((Float) animation.getAnimatedValue()).floatValue();
Patrick Dubroy8e58e912010-10-14 13:21:48 -0700199 invalidate();
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700200 }
201 });
Patrick Dubroyce34a972010-10-19 10:34:32 -0700202 mCrosshairsAnimator.getAnimator().setInterpolator(mEaseOutInterpolator);
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700203
Joe Onorato4be866d2010-10-10 11:26:02 -0700204 for (int i = 0; i < mDragOutlines.length; i++) {
205 mDragOutlines[i] = new Point(-1, -1);
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700206 }
207
208 // When dragging things around the home screens, we show a green outline of
209 // where the item will land. The outlines gradually fade out, leaving a trail
210 // behind the drag path.
211 // Set up all the animations that are used to implement this fading.
212 final int duration = res.getInteger(R.integer.config_dragOutlineFadeTime);
Chet Haase472b2812010-10-14 07:02:04 -0700213 final float fromAlphaValue = 0;
214 final float toAlphaValue = (float)res.getInteger(R.integer.config_dragOutlineMaxAlpha);
Joe Onorato4be866d2010-10-10 11:26:02 -0700215
Patrick Dubroy8e58e912010-10-14 13:21:48 -0700216 Arrays.fill(mDragOutlineAlphas, fromAlphaValue);
Joe Onorato4be866d2010-10-10 11:26:02 -0700217
218 for (int i = 0; i < mDragOutlineAnims.length; i++) {
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700219 final InterruptibleInOutAnimator anim =
220 new InterruptibleInOutAnimator(duration, fromAlphaValue, toAlphaValue);
Patrick Dubroyce34a972010-10-19 10:34:32 -0700221 anim.getAnimator().setInterpolator(mEaseOutInterpolator);
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700222 final int thisIndex = i;
Chet Haase472b2812010-10-14 07:02:04 -0700223 anim.getAnimator().addUpdateListener(new AnimatorUpdateListener() {
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700224 public void onAnimationUpdate(ValueAnimator animation) {
Joe Onorato4be866d2010-10-10 11:26:02 -0700225 final Bitmap outline = (Bitmap)anim.getTag();
226
227 // If an animation is started and then stopped very quickly, we can still
228 // get spurious updates we've cleared the tag. Guard against this.
229 if (outline == null) {
Patrick Dubroyfe6bd872010-10-13 17:32:10 -0700230 if (false) {
231 Object val = animation.getAnimatedValue();
232 Log.d(TAG, "anim " + thisIndex + " update: " + val +
233 ", isStopped " + anim.isStopped());
234 }
Joe Onorato4be866d2010-10-10 11:26:02 -0700235 // Try to prevent it from continuing to run
236 animation.cancel();
237 } else {
Chet Haase472b2812010-10-14 07:02:04 -0700238 mDragOutlineAlphas[thisIndex] = (Float) animation.getAnimatedValue();
Joe Onorato4be866d2010-10-10 11:26:02 -0700239 final int left = mDragOutlines[thisIndex].x;
240 final int top = mDragOutlines[thisIndex].y;
241 CellLayout.this.invalidate(left, top,
242 left + outline.getWidth(), top + outline.getHeight());
243 }
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700244 }
245 });
Joe Onorato4be866d2010-10-10 11:26:02 -0700246 // The animation holds a reference to the drag outline bitmap as long is it's
247 // running. This way the bitmap can be GCed when the animations are complete.
Chet Haase472b2812010-10-14 07:02:04 -0700248 anim.getAnimator().addListener(new AnimatorListenerAdapter() {
Joe Onorato4be866d2010-10-10 11:26:02 -0700249 public void onAnimationEnd(Animator animation) {
Chet Haase472b2812010-10-14 07:02:04 -0700250 if ((Float) ((ValueAnimator) animation).getAnimatedValue() == 0f) {
Joe Onorato4be866d2010-10-10 11:26:02 -0700251 anim.setTag(null);
252 }
253 }
254 });
255 mDragOutlineAnims[i] = anim;
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700256 }
Patrick Dubroyce34a972010-10-19 10:34:32 -0700257
Patrick Dubroya9442152010-10-25 11:41:41 -0700258 mDropAnim = ValueAnimator.ofFloat(1.0f, 0.0f);
Patrick Dubroyce34a972010-10-19 10:34:32 -0700259 mDropAnim.setInterpolator(mEaseOutInterpolator);
260
Michael Jurka18014792010-10-14 09:01:34 -0700261 mBackgroundRect = new Rect();
262 mHoverRect = new Rect();
263 setHoverScale(1.0f);
264 setHoverAlpha(1.0f);
265 }
266
267 private void updateHoverRect() {
268 float marginFraction = (mHoverScale - 1.0f) / 2.0f;
269 int marginX = (int) (marginFraction * (mBackgroundRect.right - mBackgroundRect.left));
270 int marginY = (int) (marginFraction * (mBackgroundRect.bottom - mBackgroundRect.top));
271 mHoverRect.set(mBackgroundRect.left - marginX, mBackgroundRect.top - marginY,
272 mBackgroundRect.right + marginX, mBackgroundRect.bottom + marginY);
273 invalidate();
274 }
275
276 public void setHoverScale(float scaleFactor) {
277 if (scaleFactor != mHoverScale) {
278 mHoverScale = scaleFactor;
279 updateHoverRect();
280 }
281 }
282
283 public float getHoverScale() {
284 return mHoverScale;
285 }
286
287 public float getHoverAlpha() {
288 return mHoverAlpha;
289 }
290
291 public void setHoverAlpha(float alpha) {
292 mHoverAlpha = alpha;
293 invalidate();
294 }
295
296 void animateDrop() {
297 if (LauncherApplication.isScreenXLarge()) {
298 Resources res = getResources();
299 float onDropScale = res.getInteger(R.integer.config_screenOnDropScalePercent) / 100.0f;
300 ObjectAnimator scaleUp = ObjectAnimator.ofFloat(this, "hoverScale", onDropScale);
301 scaleUp.setDuration(res.getInteger(R.integer.config_screenOnDropScaleUpDuration));
302 ObjectAnimator scaleDown = ObjectAnimator.ofFloat(this, "hoverScale", 1.0f);
303 scaleDown.setDuration(res.getInteger(R.integer.config_screenOnDropScaleDownDuration));
304 ObjectAnimator alphaFadeOut = ObjectAnimator.ofFloat(this, "hoverAlpha", 0.0f);
305
306 alphaFadeOut.setStartDelay(res.getInteger(R.integer.config_screenOnDropAlphaFadeDelay));
307 alphaFadeOut.setDuration(res.getInteger(R.integer.config_screenOnDropAlphaFadeDelay));
308
309 AnimatorSet bouncer = new AnimatorSet();
310 bouncer.play(scaleUp).before(scaleDown);
311 bouncer.play(scaleUp).with(alphaFadeOut);
312 bouncer.addListener(new AnimatorListenerAdapter() {
313 public void onAnimationStart(Animator animation) {
314 setHover(true);
315 }
316 public void onAnimationEnd(Animator animation) {
317 setHover(false);
318 setHoverScale(1.0f);
319 setHoverAlpha(1.0f);
320 }
321 });
322 bouncer.start();
323 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800324 }
325
Michael Jurkaa63c4522010-08-19 13:52:27 -0700326 public void setHover(boolean value) {
327 if (mHover != value) {
Michael Jurka3e7c7632010-10-02 16:01:03 -0700328 mHover = value;
Michael Jurkaa63c4522010-08-19 13:52:27 -0700329 invalidate();
330 }
Michael Jurkaa63c4522010-08-19 13:52:27 -0700331 }
332
Patrick Dubroy1262e362010-10-06 15:49:50 -0700333 public void drawChildren(Canvas canvas) {
334 super.dispatchDraw(canvas);
335 }
336
Jeff Sharkey83f111d2009-04-20 21:03:13 -0700337 @Override
Patrick Dubroy1262e362010-10-06 15:49:50 -0700338 protected void onDraw(Canvas canvas) {
Michael Jurka3e7c7632010-10-02 16:01:03 -0700339 // When we're large, we are either drawn in a "hover" state (ie when dragging an item to
340 // a neighboring page) or with just a normal background (if backgroundAlpha > 0.0f)
341 // When we're small, we are either drawn normally or in the "accepts drops" state (during
342 // a drag). However, we also drag the mini hover background *over* one of those two
343 // backgrounds
Michael Jurka5f1c5092010-09-03 14:15:02 -0700344 if (mBackgroundAlpha > 0.0f) {
Adam Cohenf34bab52010-09-30 14:11:56 -0700345 Drawable bg;
Patrick Dubroy1262e362010-10-06 15:49:50 -0700346 if (getScaleX() < 0.5f) {
Michael Jurka3e7c7632010-10-02 16:01:03 -0700347 bg = mAcceptsDrops ? mBackgroundMiniAcceptsDrops : mBackgroundMini;
Adam Cohenf34bab52010-09-30 14:11:56 -0700348 } else {
Patrick Dubroy1262e362010-10-06 15:49:50 -0700349 bg = mHover ? mBackgroundHover : mBackground;
Adam Cohenf34bab52010-09-30 14:11:56 -0700350 }
Adam Cohen9c4949e2010-10-05 12:27:22 -0700351 if (bg != null) {
Adam Cohen1b0aaac2010-10-28 11:11:18 -0700352 bg.setAlpha((int) (mBackgroundAlpha * mBackgroundAlphaMultiplier * 255));
Michael Jurka18014792010-10-14 09:01:34 -0700353 bg.setBounds(mBackgroundRect);
Adam Cohen9c4949e2010-10-05 12:27:22 -0700354 bg.draw(canvas);
355 }
Michael Jurka3e7c7632010-10-02 16:01:03 -0700356 if (mHover && getScaleX() < 0.5f) {
Michael Jurka18014792010-10-14 09:01:34 -0700357 boolean modifiedClipRect = false;
358 if (mHoverScale > 1.0f) {
359 // If the hover background's scale is greater than 1, we'll be drawing outside
360 // the bounds of this CellLayout. Get around that by temporarily increasing the
361 // size of the clip rect
362 float marginFraction = (mHoverScale - 1.0f) / 2.0f;
363 Rect clipRect = canvas.getClipBounds();
364 int marginX = (int) (marginFraction * (clipRect.right - clipRect.left));
365 int marginY = (int) (marginFraction * (clipRect.bottom - clipRect.top));
366 canvas.save(Canvas.CLIP_SAVE_FLAG);
367 canvas.clipRect(-marginX, -marginY,
368 getWidth() + marginX, getHeight() + marginY, Region.Op.REPLACE);
369 modifiedClipRect = true;
370 }
371
372 mBackgroundMiniHover.setAlpha((int) (mBackgroundAlpha * mHoverAlpha * 255));
373 mBackgroundMiniHover.setBounds(mHoverRect);
Michael Jurka3e7c7632010-10-02 16:01:03 -0700374 mBackgroundMiniHover.draw(canvas);
Michael Jurka18014792010-10-14 09:01:34 -0700375 if (modifiedClipRect) {
376 canvas.restore();
377 }
Michael Jurka3e7c7632010-10-02 16:01:03 -0700378 }
Michael Jurkaa63c4522010-08-19 13:52:27 -0700379 }
Romain Guya6abce82009-11-10 02:54:41 -0800380
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700381 if (mCrosshairsVisibility > 0.0f) {
382 final int countX = mCountX;
383 final int countY = mCountY;
384
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700385 final float MAX_ALPHA = 0.4f;
386 final int MAX_VISIBLE_DISTANCE = 600;
387 final float DISTANCE_MULTIPLIER = 0.002f;
388
389 final Drawable d = mCrosshairsDrawable;
390 final int width = d.getIntrinsicWidth();
391 final int height = d.getIntrinsicHeight();
392
393 int x = getLeftPadding() - (mWidthGap / 2) - (width / 2);
394 for (int col = 0; col <= countX; col++) {
395 int y = getTopPadding() - (mHeightGap / 2) - (height / 2);
396 for (int row = 0; row <= countY; row++) {
397 mTmpPointF.set(x - mDragCenter.x, y - mDragCenter.y);
398 float dist = mTmpPointF.length();
399 // Crosshairs further from the drag point are more faint
400 float alpha = Math.min(MAX_ALPHA,
401 DISTANCE_MULTIPLIER * (MAX_VISIBLE_DISTANCE - dist));
402 if (alpha > 0.0f) {
403 d.setBounds(x, y, x + width, y + height);
404 d.setAlpha((int) (alpha * 255 * mCrosshairsVisibility));
405 d.draw(canvas);
406 }
407 y += mCellHeight + mHeightGap;
408 }
409 x += mCellWidth + mWidthGap;
410 }
Joe Onorato4be866d2010-10-10 11:26:02 -0700411 }
Winson Chung150fbab2010-09-29 17:14:26 -0700412
Patrick Dubroy8e58e912010-10-14 13:21:48 -0700413 final Paint paint = mDragOutlinePaint;
Joe Onorato4be866d2010-10-10 11:26:02 -0700414 for (int i = 0; i < mDragOutlines.length; i++) {
Chet Haase472b2812010-10-14 07:02:04 -0700415 final float alpha = mDragOutlineAlphas[i];
Joe Onorato4be866d2010-10-10 11:26:02 -0700416 if (alpha > 0) {
417 final Point p = mDragOutlines[i];
418 final Bitmap b = (Bitmap) mDragOutlineAnims[i].getTag();
Chet Haase472b2812010-10-14 07:02:04 -0700419 paint.setAlpha((int)(alpha + .5f));
Joe Onorato4be866d2010-10-10 11:26:02 -0700420 canvas.drawBitmap(b, p.x, p.y, paint);
Winson Chung150fbab2010-09-29 17:14:26 -0700421 }
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700422 }
423 }
424
Adam Cohenf34bab52010-09-30 14:11:56 -0700425 public void setDimmableProgress(float progress) {
426 for (int i = 0; i < getChildCount(); i++) {
427 Dimmable d = (Dimmable) getChildAt(i);
428 d.setDimmableProgress(progress);
429 }
430 }
431
432 public float getDimmableProgress() {
433 if (getChildCount() > 0) {
434 return ((Dimmable) getChildAt(0)).getDimmableProgress();
435 }
436 return 0.0f;
437 }
438
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700439 @Override
Jeff Sharkey83f111d2009-04-20 21:03:13 -0700440 public void cancelLongPress() {
441 super.cancelLongPress();
442
443 // Cancel long press for all children
444 final int count = getChildCount();
445 for (int i = 0; i < count; i++) {
446 final View child = getChildAt(i);
447 child.cancelLongPress();
448 }
449 }
450
Michael Jurkadee05892010-07-27 10:01:56 -0700451 public void setOnInterceptTouchListener(View.OnTouchListener listener) {
452 mInterceptTouchListener = listener;
453 }
454
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800455 int getCountX() {
Adam Cohend22015c2010-07-26 22:02:18 -0700456 return mCountX;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800457 }
458
459 int getCountY() {
Adam Cohend22015c2010-07-26 22:02:18 -0700460 return mCountY;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800461 }
462
Winson Chungaafa03c2010-06-11 17:34:16 -0700463 public boolean addViewToCellLayout(View child, int index, int childId, LayoutParams params) {
Michael Jurkaf3ca3ab2010-10-20 17:08:24 -0700464 return addViewToCellLayout(child, index, childId, params, true);
465 }
466
467 public boolean addViewToCellLayout(
468 View child, int index, int childId, LayoutParams params, boolean markCells) {
Winson Chungaafa03c2010-06-11 17:34:16 -0700469 final LayoutParams lp = params;
470
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800471 // Generate an id for each view, this assumes we have at most 256x256 cells
472 // per workspace screen
Adam Cohend22015c2010-07-26 22:02:18 -0700473 if (lp.cellX >= 0 && lp.cellX <= mCountX - 1 && lp.cellY >= 0 && lp.cellY <= mCountY - 1) {
Winson Chungaafa03c2010-06-11 17:34:16 -0700474 // If the horizontal or vertical span is set to -1, it is taken to
475 // mean that it spans the extent of the CellLayout
Adam Cohend22015c2010-07-26 22:02:18 -0700476 if (lp.cellHSpan < 0) lp.cellHSpan = mCountX;
477 if (lp.cellVSpan < 0) lp.cellVSpan = mCountY;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800478
Winson Chungaafa03c2010-06-11 17:34:16 -0700479 child.setId(childId);
480
Michael Jurkadee05892010-07-27 10:01:56 -0700481 // We might be in the middle or end of shrinking/fading to a dimmed view
482 // Make sure this view's alpha is set the same as all the rest of the views
Michael Jurka5f1c5092010-09-03 14:15:02 -0700483 child.setAlpha(getAlpha());
Winson Chungaafa03c2010-06-11 17:34:16 -0700484 addView(child, index, lp);
Michael Jurkadee05892010-07-27 10:01:56 -0700485
Michael Jurkaf3ca3ab2010-10-20 17:08:24 -0700486 if (markCells) markCellsAsOccupiedForView(child);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700487
Winson Chungaafa03c2010-06-11 17:34:16 -0700488 return true;
489 }
490 return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800491 }
Michael Jurka3e7c7632010-10-02 16:01:03 -0700492 public void setAcceptsDrops(boolean acceptsDrops) {
493 if (mAcceptsDrops != acceptsDrops) {
494 mAcceptsDrops = acceptsDrops;
495 invalidate();
496 }
497 }
498
499 public boolean getAcceptsDrops() {
500 return mAcceptsDrops;
501 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800502
503 @Override
Michael Jurka0280c3b2010-09-17 15:00:07 -0700504 public void removeAllViews() {
505 clearOccupiedCells();
506 }
507
508 @Override
509 public void removeAllViewsInLayout() {
510 clearOccupiedCells();
511 }
512
Michael Jurkaf3ca3ab2010-10-20 17:08:24 -0700513 public void removeViewWithoutMarkingCells(View view) {
514 super.removeView(view);
515 }
516
Michael Jurka0280c3b2010-09-17 15:00:07 -0700517 @Override
518 public void removeView(View view) {
519 markCellsAsUnoccupiedForView(view);
520 super.removeView(view);
521 }
522
523 @Override
524 public void removeViewAt(int index) {
525 markCellsAsUnoccupiedForView(getChildAt(index));
526 super.removeViewAt(index);
527 }
528
529 @Override
530 public void removeViewInLayout(View view) {
531 markCellsAsUnoccupiedForView(view);
532 super.removeViewInLayout(view);
533 }
534
535 @Override
536 public void removeViews(int start, int count) {
537 for (int i = start; i < start + count; i++) {
538 markCellsAsUnoccupiedForView(getChildAt(i));
539 }
540 super.removeViews(start, count);
541 }
542
543 @Override
544 public void removeViewsInLayout(int start, int count) {
545 for (int i = start; i < start + count; i++) {
546 markCellsAsUnoccupiedForView(getChildAt(i));
547 }
548 super.removeViewsInLayout(start, count);
549 }
550
551 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800552 public void requestChildFocus(View child, View focused) {
553 super.requestChildFocus(child, focused);
554 if (child != null) {
555 Rect r = new Rect();
556 child.getDrawingRect(r);
557 requestRectangleOnScreen(r);
558 }
559 }
560
561 @Override
562 protected void onAttachedToWindow() {
563 super.onAttachedToWindow();
564 mCellInfo.screen = ((ViewGroup) getParent()).indexOfChild(this);
565 }
566
Michael Jurkaaf442092010-06-10 17:01:57 -0700567 public void setTagToCellInfoForPoint(int touchX, int touchY) {
568 final CellInfo cellInfo = mCellInfo;
569 final Rect frame = mRect;
570 final int x = touchX + mScrollX;
571 final int y = touchY + mScrollY;
572 final int count = getChildCount();
573
574 boolean found = false;
575 for (int i = count - 1; i >= 0; i--) {
576 final View child = getChildAt(i);
577
578 if ((child.getVisibility()) == VISIBLE || child.getAnimation() != null) {
579 child.getHitRect(frame);
580 if (frame.contains(x, y)) {
581 final LayoutParams lp = (LayoutParams) child.getLayoutParams();
582 cellInfo.cell = child;
583 cellInfo.cellX = lp.cellX;
584 cellInfo.cellY = lp.cellY;
585 cellInfo.spanX = lp.cellHSpan;
586 cellInfo.spanY = lp.cellVSpan;
587 cellInfo.valid = true;
588 found = true;
Michael Jurkaaf442092010-06-10 17:01:57 -0700589 break;
590 }
591 }
592 }
Winson Chungaafa03c2010-06-11 17:34:16 -0700593
Michael Jurkaaf442092010-06-10 17:01:57 -0700594 if (!found) {
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700595 final int cellXY[] = mTmpCellXY;
Michael Jurkaaf442092010-06-10 17:01:57 -0700596 pointToCellExact(x, y, cellXY);
597
Michael Jurkaaf442092010-06-10 17:01:57 -0700598 cellInfo.cell = null;
599 cellInfo.cellX = cellXY[0];
600 cellInfo.cellY = cellXY[1];
601 cellInfo.spanX = 1;
602 cellInfo.spanY = 1;
Michael Jurka0280c3b2010-09-17 15:00:07 -0700603 cellInfo.valid = cellXY[0] >= 0 && cellXY[1] >= 0 && cellXY[0] < mCountX &&
604 cellXY[1] < mCountY && !mOccupied[cellXY[0]][cellXY[1]];
Michael Jurkaaf442092010-06-10 17:01:57 -0700605 }
606 setTag(cellInfo);
607 }
608
Winson Chungaafa03c2010-06-11 17:34:16 -0700609
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800610 @Override
611 public boolean onInterceptTouchEvent(MotionEvent ev) {
Michael Jurkadee05892010-07-27 10:01:56 -0700612 if (mInterceptTouchListener != null && mInterceptTouchListener.onTouch(this, ev)) {
613 return true;
614 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800615 final int action = ev.getAction();
616 final CellInfo cellInfo = mCellInfo;
617
618 if (action == MotionEvent.ACTION_DOWN) {
Michael Jurkaaf442092010-06-10 17:01:57 -0700619 setTagToCellInfoForPoint((int) ev.getX(), (int) ev.getY());
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800620 } else if (action == MotionEvent.ACTION_UP) {
621 cellInfo.cell = null;
622 cellInfo.cellX = -1;
623 cellInfo.cellY = -1;
624 cellInfo.spanX = 0;
625 cellInfo.spanY = 0;
626 cellInfo.valid = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800627 setTag(cellInfo);
628 }
629
630 return false;
631 }
632
633 @Override
634 public CellInfo getTag() {
Michael Jurka0280c3b2010-09-17 15:00:07 -0700635 return (CellInfo) super.getTag();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800636 }
637
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700638 /**
639 * Check if the row 'y' is empty from columns 'left' to 'right', inclusive.
640 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800641 private static boolean isRowEmpty(int y, int left, int right, boolean[][] occupied) {
642 for (int x = left; x <= right; x++) {
643 if (occupied[x][y]) {
644 return false;
645 }
646 }
647 return true;
648 }
649
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800650 /**
Winson Chungaafa03c2010-06-11 17:34:16 -0700651 * Given a point, return the cell that strictly encloses that point
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800652 * @param x X coordinate of the point
653 * @param y Y coordinate of the point
654 * @param result Array of 2 ints to hold the x and y coordinate of the cell
655 */
656 void pointToCellExact(int x, int y, int[] result) {
Winson Chungaafa03c2010-06-11 17:34:16 -0700657 final int hStartPadding = getLeftPadding();
658 final int vStartPadding = getTopPadding();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800659
660 result[0] = (x - hStartPadding) / (mCellWidth + mWidthGap);
661 result[1] = (y - vStartPadding) / (mCellHeight + mHeightGap);
662
Adam Cohend22015c2010-07-26 22:02:18 -0700663 final int xAxis = mCountX;
664 final int yAxis = mCountY;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800665
666 if (result[0] < 0) result[0] = 0;
667 if (result[0] >= xAxis) result[0] = xAxis - 1;
668 if (result[1] < 0) result[1] = 0;
669 if (result[1] >= yAxis) result[1] = yAxis - 1;
670 }
Winson Chungaafa03c2010-06-11 17:34:16 -0700671
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800672 /**
673 * Given a point, return the cell that most closely encloses that point
674 * @param x X coordinate of the point
675 * @param y Y coordinate of the point
676 * @param result Array of 2 ints to hold the x and y coordinate of the cell
677 */
678 void pointToCellRounded(int x, int y, int[] result) {
679 pointToCellExact(x + (mCellWidth / 2), y + (mCellHeight / 2), result);
680 }
681
682 /**
683 * Given a cell coordinate, return the point that represents the upper left corner of that cell
Winson Chungaafa03c2010-06-11 17:34:16 -0700684 *
685 * @param cellX X coordinate of the cell
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800686 * @param cellY Y coordinate of the cell
Winson Chungaafa03c2010-06-11 17:34:16 -0700687 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800688 * @param result Array of 2 ints to hold the x and y coordinate of the point
689 */
690 void cellToPoint(int cellX, int cellY, int[] result) {
Winson Chungaafa03c2010-06-11 17:34:16 -0700691 final int hStartPadding = getLeftPadding();
692 final int vStartPadding = getTopPadding();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800693
694 result[0] = hStartPadding + cellX * (mCellWidth + mWidthGap);
695 result[1] = vStartPadding + cellY * (mCellHeight + mHeightGap);
696 }
697
Romain Guy84f296c2009-11-04 15:00:44 -0800698 int getCellWidth() {
699 return mCellWidth;
700 }
701
702 int getCellHeight() {
703 return mCellHeight;
704 }
705
Romain Guy1a304a12009-11-10 00:02:32 -0800706 int getLeftPadding() {
Winson Chungaafa03c2010-06-11 17:34:16 -0700707 return mLeftPadding;
Romain Guy1a304a12009-11-10 00:02:32 -0800708 }
709
710 int getTopPadding() {
Winson Chungaafa03c2010-06-11 17:34:16 -0700711 return mTopPadding;
Romain Guy1a304a12009-11-10 00:02:32 -0800712 }
713
714 int getRightPadding() {
Winson Chungaafa03c2010-06-11 17:34:16 -0700715 return mRightPadding;
Romain Guy1a304a12009-11-10 00:02:32 -0800716 }
717
718 int getBottomPadding() {
Winson Chungaafa03c2010-06-11 17:34:16 -0700719 return mBottomPadding;
Romain Guy1a304a12009-11-10 00:02:32 -0800720 }
721
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800722 @Override
723 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
724 // TODO: currently ignoring padding
Winson Chungaafa03c2010-06-11 17:34:16 -0700725
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800726 int widthSpecMode = MeasureSpec.getMode(widthMeasureSpec);
Winson Chungaafa03c2010-06-11 17:34:16 -0700727 int widthSpecSize = MeasureSpec.getSize(widthMeasureSpec);
728
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800729 int heightSpecMode = MeasureSpec.getMode(heightMeasureSpec);
730 int heightSpecSize = MeasureSpec.getSize(heightMeasureSpec);
Winson Chungaafa03c2010-06-11 17:34:16 -0700731
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800732 if (widthSpecMode == MeasureSpec.UNSPECIFIED || heightSpecMode == MeasureSpec.UNSPECIFIED) {
733 throw new RuntimeException("CellLayout cannot have UNSPECIFIED dimensions");
734 }
735
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800736 final int cellWidth = mCellWidth;
737 final int cellHeight = mCellHeight;
738
Adam Cohend22015c2010-07-26 22:02:18 -0700739 int numWidthGaps = mCountX - 1;
740 int numHeightGaps = mCountY - 1;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800741
Winson Chungece7f5b2010-10-22 14:54:12 -0700742 if (mWidthGap < 0 || mHeightGap < 0) {
743 int vSpaceLeft = heightSpecSize - mTopPadding - mBottomPadding - (cellHeight * mCountY);
744 mHeightGap = vSpaceLeft / numHeightGaps;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800745
Winson Chungece7f5b2010-10-22 14:54:12 -0700746 int hSpaceLeft = widthSpecSize - mLeftPadding - mRightPadding - (cellWidth * mCountX);
747 mWidthGap = hSpaceLeft / numWidthGaps;
Winson Chungaafa03c2010-06-11 17:34:16 -0700748
Winson Chungece7f5b2010-10-22 14:54:12 -0700749 // center it around the min gaps
750 int minGap = Math.min(mWidthGap, mHeightGap);
751 mWidthGap = mHeightGap = minGap;
752 }
Michael Jurka5f1c5092010-09-03 14:15:02 -0700753
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800754 int count = getChildCount();
755
756 for (int i = 0; i < count; i++) {
757 View child = getChildAt(i);
758 LayoutParams lp = (LayoutParams) child.getLayoutParams();
Winson Chungaafa03c2010-06-11 17:34:16 -0700759 lp.setup(cellWidth, cellHeight, mWidthGap, mHeightGap,
760 mLeftPadding, mTopPadding);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800761
Michael Jurka0280c3b2010-09-17 15:00:07 -0700762 int childWidthMeasureSpec = MeasureSpec.makeMeasureSpec(lp.width, MeasureSpec.EXACTLY);
Winson Chungaafa03c2010-06-11 17:34:16 -0700763 int childheightMeasureSpec = MeasureSpec.makeMeasureSpec(lp.height,
764 MeasureSpec.EXACTLY);
765
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800766 child.measure(childWidthMeasureSpec, childheightMeasureSpec);
767 }
Michael Jurka5f1c5092010-09-03 14:15:02 -0700768 if (widthSpecMode == MeasureSpec.AT_MOST) {
769 int newWidth = mLeftPadding + mRightPadding + (mCountX * cellWidth) +
Winson Chungece7f5b2010-10-22 14:54:12 -0700770 ((mCountX - 1) * mWidthGap);
Michael Jurka5f1c5092010-09-03 14:15:02 -0700771 int newHeight = mTopPadding + mBottomPadding + (mCountY * cellHeight) +
Winson Chungece7f5b2010-10-22 14:54:12 -0700772 ((mCountY - 1) * mHeightGap);
Michael Jurka5f1c5092010-09-03 14:15:02 -0700773 setMeasuredDimension(newWidth, newHeight);
774 } else if (widthSpecMode == MeasureSpec.EXACTLY) {
775 setMeasuredDimension(widthSpecSize, heightSpecSize);
776 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800777 }
778
Patrick Dubroyce34a972010-10-19 10:34:32 -0700779 /**
780 * Animate a child of this CellLayout into its current layout position.
781 * The position to animate from is given by the oldX and oldY values in its LayoutParams.
782 */
783 private void animateChildIntoPosition(final View child) {
784 final Resources res = getResources();
Patrick Dubroya9442152010-10-25 11:41:41 -0700785 final ValueAnimator anim = mDropAnim;
Patrick Dubroyce34a972010-10-19 10:34:32 -0700786 final CellLayout.LayoutParams lp = (CellLayout.LayoutParams) child.getLayoutParams();
787 final float startX = lp.oldX - lp.x;
788 final float startY = lp.oldY - lp.y;
789
790 // Calculate the duration of the animation based on the object's distance
791 final float dist = (float) Math.sqrt(startX*startX + startY*startY);
792 final float maxDist = (float) res.getInteger(R.integer.config_dropAnimMaxDist);
793 final int duration = (int) (res.getInteger(R.integer.config_dropAnimMaxDuration)
794 * mEaseOutInterpolator.getInterpolation(dist / maxDist));
795
Patrick Dubroya9442152010-10-25 11:41:41 -0700796 anim.end(); // Make sure it's not already running
Patrick Dubroyce34a972010-10-19 10:34:32 -0700797 anim.setDuration(duration);
Patrick Dubroya9442152010-10-25 11:41:41 -0700798 anim.setFloatValues(1.0f, 0.0f);
Patrick Dubroyce34a972010-10-19 10:34:32 -0700799 anim.removeAllUpdateListeners();
800 anim.addUpdateListener(new AnimatorUpdateListener() {
801 public void onAnimationUpdate(ValueAnimator animation) {
Patrick Dubroya9442152010-10-25 11:41:41 -0700802 final float value = (Float) anim.getAnimatedValue();
803 child.setTranslationX(startX * value);
804 child.setTranslationY(startY * value);
Patrick Dubroyce34a972010-10-19 10:34:32 -0700805 }
806 });
807 anim.start();
808 }
809
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800810 @Override
Michael Jurka28750fb2010-09-24 17:43:49 -0700811 protected void onLayout(boolean changed, int l, int t, int r, int b) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800812 int count = getChildCount();
813
814 for (int i = 0; i < count; i++) {
Patrick Dubroyce34a972010-10-19 10:34:32 -0700815 final View child = getChildAt(i);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800816 if (child.getVisibility() != GONE) {
817
818 CellLayout.LayoutParams lp = (CellLayout.LayoutParams) child.getLayoutParams();
819
820 int childLeft = lp.x;
821 int childTop = lp.y;
822 child.layout(childLeft, childTop, childLeft + lp.width, childTop + lp.height);
Romain Guy84f296c2009-11-04 15:00:44 -0800823
824 if (lp.dropped) {
825 lp.dropped = false;
826
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700827 final int[] cellXY = mTmpCellXY;
Romain Guy06762ab2010-01-25 16:51:08 -0800828 getLocationOnScreen(cellXY);
Romain Guy84f296c2009-11-04 15:00:44 -0800829 mWallpaperManager.sendWallpaperCommand(getWindowToken(), "android.home.drop",
Romain Guy06762ab2010-01-25 16:51:08 -0800830 cellXY[0] + childLeft + lp.width / 2,
831 cellXY[1] + childTop + lp.height / 2, 0, null);
Patrick Dubroyce34a972010-10-19 10:34:32 -0700832
833 animateChildIntoPosition(child);
Romain Guy84f296c2009-11-04 15:00:44 -0800834 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800835 }
836 }
837 }
838
839 @Override
Michael Jurkadee05892010-07-27 10:01:56 -0700840 protected void onSizeChanged(int w, int h, int oldw, int oldh) {
841 super.onSizeChanged(w, h, oldw, oldh);
Michael Jurka18014792010-10-14 09:01:34 -0700842 mBackgroundRect.set(0, 0, w, h);
843 updateHoverRect();
Michael Jurkadee05892010-07-27 10:01:56 -0700844 }
845
846 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800847 protected void setChildrenDrawingCacheEnabled(boolean enabled) {
848 final int count = getChildCount();
849 for (int i = 0; i < count; i++) {
850 final View view = getChildAt(i);
851 view.setDrawingCacheEnabled(enabled);
852 // Update the drawing caches
Romain Guy3cf4c032010-10-26 14:29:35 -0700853 if (!view.isHardwareAccelerated()) {
854 view.buildDrawingCache(true);
855 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800856 }
857 }
858
859 @Override
860 protected void setChildrenDrawnWithCacheEnabled(boolean enabled) {
861 super.setChildrenDrawnWithCacheEnabled(enabled);
862 }
863
Michael Jurka5f1c5092010-09-03 14:15:02 -0700864 public float getBackgroundAlpha() {
865 return mBackgroundAlpha;
Michael Jurkadee05892010-07-27 10:01:56 -0700866 }
867
Adam Cohen1b0aaac2010-10-28 11:11:18 -0700868 public void setBackgroundAlphaMultiplier(float multiplier) {
869 mBackgroundAlphaMultiplier = multiplier;
870 }
871
Michael Jurka5f1c5092010-09-03 14:15:02 -0700872 public void setBackgroundAlpha(float alpha) {
873 mBackgroundAlpha = alpha;
Michael Jurka0142d492010-08-25 17:46:15 -0700874 invalidate();
Michael Jurkadee05892010-07-27 10:01:56 -0700875 }
876
Michael Jurka5f1c5092010-09-03 14:15:02 -0700877 // Need to return true to let the view system know we know how to handle alpha-- this is
878 // because when our children have an alpha of 0.0f, they are still rendering their "dimmed"
879 // versions
880 @Override
881 protected boolean onSetAlpha(int alpha) {
882 return true;
883 }
884
885 public void setAlpha(float alpha) {
886 setChildrenAlpha(alpha);
887 super.setAlpha(alpha);
888 }
889
Michael Jurkadee05892010-07-27 10:01:56 -0700890 private void setChildrenAlpha(float alpha) {
Michael Jurka0142d492010-08-25 17:46:15 -0700891 final int childCount = getChildCount();
892 for (int i = 0; i < childCount; i++) {
Michael Jurkadee05892010-07-27 10:01:56 -0700893 getChildAt(i).setAlpha(alpha);
894 }
895 }
896
Michael Jurka0280c3b2010-09-17 15:00:07 -0700897 private boolean isVacantIgnoring(
898 int originX, int originY, int spanX, int spanY, View ignoreView) {
899 if (ignoreView != null) {
900 markCellsAsUnoccupiedForView(ignoreView);
901 }
Michael Jurka28750fb2010-09-24 17:43:49 -0700902 boolean isVacant = true;
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700903 for (int i = 0; i < spanY; i++) {
904 if (!isRowEmpty(originY + i, originX, originX + spanX - 1, mOccupied)) {
Michael Jurka28750fb2010-09-24 17:43:49 -0700905 isVacant = false;
906 break;
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700907 }
908 }
Michael Jurka0280c3b2010-09-17 15:00:07 -0700909 if (ignoreView != null) {
910 markCellsAsOccupiedForView(ignoreView);
911 }
Michael Jurka28750fb2010-09-24 17:43:49 -0700912 return isVacant;
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700913 }
914
Michael Jurka0280c3b2010-09-17 15:00:07 -0700915 private boolean isVacant(int originX, int originY, int spanX, int spanY) {
916 return isVacantIgnoring(originX, originY, spanX, spanY, null);
917 }
918
Patrick Dubroy440c3602010-07-13 17:50:32 -0700919 public View getChildAt(int x, int y) {
920 final int count = getChildCount();
921 for (int i = 0; i < count; i++) {
922 View child = getChildAt(i);
923 LayoutParams lp = (LayoutParams) child.getLayoutParams();
924
925 if ((lp.cellX <= x) && (x < lp.cellX + lp.cellHSpan) &&
926 (lp.cellY <= y) && (y < lp.cellY + lp.cellHSpan)) {
927 return child;
928 }
929 }
930 return null;
931 }
932
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700933 /**
Patrick Dubroy8f86ddc2010-07-16 13:55:32 -0700934 * Estimate the size that a child with the given dimensions will take in the layout.
935 */
936 void estimateChildSize(int minWidth, int minHeight, int[] result) {
937 // Assuming it's placed at 0, 0, find where the bottom right cell will land
938 rectToCell(minWidth, minHeight, result);
939
940 // Then figure out the rect it will occupy
941 cellToRect(0, 0, result[0], result[1], mRectF);
942 result[0] = (int)mRectF.width();
943 result[1] = (int)mRectF.height();
944 }
945
946 /**
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700947 * Estimate where the top left cell of the dragged item will land if it is dropped.
948 *
949 * @param originX The X value of the top left corner of the item
950 * @param originY The Y value of the top left corner of the item
951 * @param spanX The number of horizontal cells that the item spans
952 * @param spanY The number of vertical cells that the item spans
953 * @param result The estimated drop cell X and Y.
954 */
955 void estimateDropCell(int originX, int originY, int spanX, int spanY, int[] result) {
Adam Cohend22015c2010-07-26 22:02:18 -0700956 final int countX = mCountX;
957 final int countY = mCountY;
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700958
Michael Jurkaa63c4522010-08-19 13:52:27 -0700959 // pointToCellRounded takes the top left of a cell but will pad that with
960 // cellWidth/2 and cellHeight/2 when finding the matching cell
961 pointToCellRounded(originX, originY, result);
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700962
963 // If the item isn't fully on this screen, snap to the edges
964 int rightOverhang = result[0] + spanX - countX;
965 if (rightOverhang > 0) {
966 result[0] -= rightOverhang; // Snap to right
967 }
968 result[0] = Math.max(0, result[0]); // Snap to left
969 int bottomOverhang = result[1] + spanY - countY;
970 if (bottomOverhang > 0) {
971 result[1] -= bottomOverhang; // Snap to bottom
972 }
973 result[1] = Math.max(0, result[1]); // Snap to top
974 }
975
Joe Onorato4be866d2010-10-10 11:26:02 -0700976 void visualizeDropLocation(
977 View v, Bitmap dragOutline, int originX, int originY, int spanX, int spanY) {
978
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -0700979 final int oldDragCellX = mDragCell[0];
980 final int oldDragCellY = mDragCell[1];
Joe Onorato4be866d2010-10-10 11:26:02 -0700981 final int[] nearest = findNearestVacantArea(originX, originY, spanX, spanY, v, mDragCell);
Winson Chunga9abd0e2010-10-27 17:18:37 -0700982 if (v != null) {
983 mDragCenter.set(originX + (v.getWidth() / 2), originY + (v.getHeight() / 2));
984 } else {
985 mDragCenter.set(originX, originY);
986 }
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700987
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -0700988 if (nearest != null && (nearest[0] != oldDragCellX || nearest[1] != oldDragCellY)) {
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700989 // Find the top left corner of the rect the object will occupy
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700990 final int[] topLeft = mTmpPoint;
991 cellToPoint(nearest[0], nearest[1], topLeft);
992
Joe Onorato4be866d2010-10-10 11:26:02 -0700993 int left = topLeft[0];
994 int top = topLeft[1];
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700995
Winson Chunga9abd0e2010-10-27 17:18:37 -0700996 if (v != null) {
997 if (v.getParent() instanceof CellLayout) {
998 LayoutParams lp = (LayoutParams) v.getLayoutParams();
999 left += lp.leftMargin;
1000 top += lp.topMargin;
1001 }
Winson Chung150fbab2010-09-29 17:14:26 -07001002
Winson Chunga9abd0e2010-10-27 17:18:37 -07001003 // Offsets due to the size difference between the View and the dragOutline
1004 left += (v.getWidth() - dragOutline.getWidth()) / 2;
1005 top += (v.getHeight() - dragOutline.getHeight()) / 2;
1006 }
Winson Chung150fbab2010-09-29 17:14:26 -07001007
Joe Onorato4be866d2010-10-10 11:26:02 -07001008 final int oldIndex = mDragOutlineCurrent;
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -07001009 mDragOutlineAnims[oldIndex].animateOut();
1010 mDragOutlineCurrent = (oldIndex + 1) % mDragOutlines.length;
Winson Chung150fbab2010-09-29 17:14:26 -07001011
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -07001012 mDragOutlines[mDragOutlineCurrent].set(left, top);
1013 mDragOutlineAnims[mDragOutlineCurrent].setTag(dragOutline);
1014 mDragOutlineAnims[mDragOutlineCurrent].animateIn();
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001015 }
Patrick Dubroy49250ad2010-10-08 15:33:52 -07001016
1017 // If we are drawing crosshairs, the entire CellLayout needs to be invalidated
1018 if (mCrosshairsDrawable != null) {
1019 invalidate();
1020 }
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001021 }
1022
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001023 /**
Jeff Sharkey70864282009-04-07 21:08:40 -07001024 * Find a vacant area that will fit the given bounds nearest the requested
1025 * cell location. Uses Euclidean distance to score multiple vacant areas.
Winson Chungaafa03c2010-06-11 17:34:16 -07001026 *
Romain Guy51afc022009-05-04 18:03:43 -07001027 * @param pixelX The X location at which you want to search for a vacant area.
1028 * @param pixelY The Y location at which you want to search for a vacant area.
Jeff Sharkey70864282009-04-07 21:08:40 -07001029 * @param spanX Horizontal span of the object.
1030 * @param spanY Vertical span of the object.
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001031 * @param result Array in which to place the result, or null (in which case a new array will
1032 * be allocated)
Jeff Sharkey70864282009-04-07 21:08:40 -07001033 * @return The X, Y cell of a vacant area that can contain this object,
1034 * nearest the requested location.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001035 */
Michael Jurka6a1435d2010-09-27 17:35:12 -07001036 int[] findNearestVacantArea(
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001037 int pixelX, int pixelY, int spanX, int spanY, int[] result) {
1038 return findNearestVacantArea(pixelX, pixelY, spanX, spanY, null, result);
Michael Jurka6a1435d2010-09-27 17:35:12 -07001039 }
Winson Chungaafa03c2010-06-11 17:34:16 -07001040
Michael Jurka6a1435d2010-09-27 17:35:12 -07001041 /**
1042 * Find a vacant area that will fit the given bounds nearest the requested
1043 * cell location. Uses Euclidean distance to score multiple vacant areas.
1044 *
1045 * @param pixelX The X location at which you want to search for a vacant area.
1046 * @param pixelY The Y location at which you want to search for a vacant area.
1047 * @param spanX Horizontal span of the object.
1048 * @param spanY Vertical span of the object.
Michael Jurka6a1435d2010-09-27 17:35:12 -07001049 * @param ignoreView Considers space occupied by this view as unoccupied
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001050 * @param result Previously returned value to possibly recycle.
Michael Jurka6a1435d2010-09-27 17:35:12 -07001051 * @return The X, Y cell of a vacant area that can contain this object,
1052 * nearest the requested location.
1053 */
1054 int[] findNearestVacantArea(
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001055 int pixelX, int pixelY, int spanX, int spanY, View ignoreView, int[] result) {
Michael Jurkac6ee42e2010-09-30 12:04:50 -07001056 // mark space take by ignoreView as available (method checks if ignoreView is null)
1057 markCellsAsUnoccupiedForView(ignoreView);
1058
Jeff Sharkey70864282009-04-07 21:08:40 -07001059 // Keep track of best-scoring drop area
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001060 final int[] bestXY = result != null ? result : new int[2];
Jeff Sharkey70864282009-04-07 21:08:40 -07001061 double bestDistance = Double.MAX_VALUE;
Winson Chungaafa03c2010-06-11 17:34:16 -07001062
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001063 final int countX = mCountX;
1064 final int countY = mCountY;
1065 final boolean[][] occupied = mOccupied;
1066
1067 for (int x = 0; x < countX - (spanX - 1); x++) {
Michael Jurkac28de512010-08-13 11:27:44 -07001068 inner:
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001069 for (int y = 0; y < countY - (spanY - 1); y++) {
Michael Jurkac28de512010-08-13 11:27:44 -07001070 for (int i = 0; i < spanX; i++) {
1071 for (int j = 0; j < spanY; j++) {
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001072 if (occupied[x + i][y + j]) {
Michael Jurkac28de512010-08-13 11:27:44 -07001073 // small optimization: we can skip to below the row we just found
1074 // an occupied cell
1075 y += j;
1076 continue inner;
1077 }
1078 }
1079 }
1080 final int[] cellXY = mTmpCellXY;
1081 cellToPoint(x, y, cellXY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001082
Michael Jurkac28de512010-08-13 11:27:44 -07001083 double distance = Math.sqrt(Math.pow(cellXY[0] - pixelX, 2)
1084 + Math.pow(cellXY[1] - pixelY, 2));
1085 if (distance <= bestDistance) {
1086 bestDistance = distance;
1087 bestXY[0] = x;
1088 bestXY[1] = y;
1089 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001090 }
1091 }
Michael Jurkac6ee42e2010-09-30 12:04:50 -07001092 // re-mark space taken by ignoreView as occupied
1093 markCellsAsOccupiedForView(ignoreView);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001094
Winson Chungaafa03c2010-06-11 17:34:16 -07001095 // Return null if no suitable location found
Jeff Sharkey70864282009-04-07 21:08:40 -07001096 if (bestDistance < Double.MAX_VALUE) {
1097 return bestXY;
1098 } else {
1099 return null;
1100 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001101 }
Winson Chungaafa03c2010-06-11 17:34:16 -07001102
Michael Jurka0280c3b2010-09-17 15:00:07 -07001103 boolean existsEmptyCell() {
1104 return findCellForSpan(null, 1, 1);
1105 }
1106
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001107 /**
Michael Jurka0280c3b2010-09-17 15:00:07 -07001108 * Finds the upper-left coordinate of the first rectangle in the grid that can
1109 * hold a cell of the specified dimensions. If intersectX and intersectY are not -1,
1110 * then this method will only return coordinates for rectangles that contain the cell
1111 * (intersectX, intersectY)
1112 *
1113 * @param cellXY The array that will contain the position of a vacant cell if such a cell
1114 * can be found.
1115 * @param spanX The horizontal span of the cell we want to find.
1116 * @param spanY The vertical span of the cell we want to find.
1117 *
1118 * @return True if a vacant cell of the specified dimension was found, false otherwise.
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001119 */
Michael Jurka0280c3b2010-09-17 15:00:07 -07001120 boolean findCellForSpan(int[] cellXY, int spanX, int spanY) {
1121 return findCellForSpanThatIntersectsIgnoring(cellXY, spanX, spanY, -1, -1, null);
1122 }
1123
1124 /**
1125 * Like above, but ignores any cells occupied by the item "ignoreView"
1126 *
1127 * @param cellXY The array that will contain the position of a vacant cell if such a cell
1128 * can be found.
1129 * @param spanX The horizontal span of the cell we want to find.
1130 * @param spanY The vertical span of the cell we want to find.
1131 * @param ignoreView The home screen item we should treat as not occupying any space
1132 * @return
1133 */
1134 boolean findCellForSpanIgnoring(int[] cellXY, int spanX, int spanY, View ignoreView) {
1135 return findCellForSpanThatIntersectsIgnoring(cellXY, spanX, spanY, -1, -1, ignoreView);
1136 }
1137
1138 /**
1139 * Like above, but if intersectX and intersectY are not -1, then this method will try to
1140 * return coordinates for rectangles that contain the cell [intersectX, intersectY]
1141 *
1142 * @param spanX The horizontal span of the cell we want to find.
1143 * @param spanY The vertical span of the cell we want to find.
1144 * @param ignoreView The home screen item we should treat as not occupying any space
1145 * @param intersectX The X coordinate of the cell that we should try to overlap
1146 * @param intersectX The Y coordinate of the cell that we should try to overlap
1147 *
1148 * @return True if a vacant cell of the specified dimension was found, false otherwise.
1149 */
1150 boolean findCellForSpanThatIntersects(int[] cellXY, int spanX, int spanY,
1151 int intersectX, int intersectY) {
1152 return findCellForSpanThatIntersectsIgnoring(
1153 cellXY, spanX, spanY, intersectX, intersectY, null);
1154 }
1155
1156 /**
1157 * The superset of the above two methods
1158 */
1159 boolean findCellForSpanThatIntersectsIgnoring(int[] cellXY, int spanX, int spanY,
1160 int intersectX, int intersectY, View ignoreView) {
Michael Jurkac6ee42e2010-09-30 12:04:50 -07001161 // mark space take by ignoreView as available (method checks if ignoreView is null)
1162 markCellsAsUnoccupiedForView(ignoreView);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001163
Michael Jurka28750fb2010-09-24 17:43:49 -07001164 boolean foundCell = false;
Michael Jurka0280c3b2010-09-17 15:00:07 -07001165 while (true) {
1166 int startX = 0;
1167 if (intersectX >= 0) {
1168 startX = Math.max(startX, intersectX - (spanX - 1));
1169 }
1170 int endX = mCountX - (spanX - 1);
1171 if (intersectX >= 0) {
1172 endX = Math.min(endX, intersectX + (spanX - 1) + (spanX == 1 ? 1 : 0));
1173 }
1174 int startY = 0;
1175 if (intersectY >= 0) {
1176 startY = Math.max(startY, intersectY - (spanY - 1));
1177 }
1178 int endY = mCountY - (spanY - 1);
1179 if (intersectY >= 0) {
1180 endY = Math.min(endY, intersectY + (spanY - 1) + (spanY == 1 ? 1 : 0));
1181 }
1182
1183 for (int x = startX; x < endX; x++) {
1184 inner:
1185 for (int y = startY; y < endY; y++) {
1186 for (int i = 0; i < spanX; i++) {
1187 for (int j = 0; j < spanY; j++) {
1188 if (mOccupied[x + i][y + j]) {
1189 // small optimization: we can skip to below the row we just found
1190 // an occupied cell
1191 y += j;
1192 continue inner;
1193 }
1194 }
1195 }
1196 if (cellXY != null) {
1197 cellXY[0] = x;
1198 cellXY[1] = y;
1199 }
Michael Jurka28750fb2010-09-24 17:43:49 -07001200 foundCell = true;
1201 break;
Michael Jurka0280c3b2010-09-17 15:00:07 -07001202 }
1203 }
1204 if (intersectX == -1 && intersectY == -1) {
1205 break;
1206 } else {
1207 // if we failed to find anything, try again but without any requirements of
1208 // intersecting
1209 intersectX = -1;
1210 intersectY = -1;
1211 continue;
1212 }
1213 }
1214
Michael Jurkac6ee42e2010-09-30 12:04:50 -07001215 // re-mark space taken by ignoreView as occupied
1216 markCellsAsOccupiedForView(ignoreView);
Michael Jurka28750fb2010-09-24 17:43:49 -07001217 return foundCell;
Michael Jurka0280c3b2010-09-17 15:00:07 -07001218 }
1219
1220 /**
1221 * Called when drag has left this CellLayout or has been completed (successfully or not)
1222 */
1223 void onDragExit() {
Joe Onorato4be866d2010-10-10 11:26:02 -07001224 // This can actually be called when we aren't in a drag, e.g. when adding a new
1225 // item to this layout via the customize drawer.
1226 // Guard against that case.
1227 if (mDragging) {
1228 mDragging = false;
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001229
Joe Onorato4be866d2010-10-10 11:26:02 -07001230 // Fade out the drag indicators
1231 if (mCrosshairsAnimator != null) {
1232 mCrosshairsAnimator.animateOut();
1233 }
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001234 }
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -07001235
1236 // Invalidate the drag data
1237 mDragCell[0] = -1;
1238 mDragCell[1] = -1;
1239 mDragOutlineAnims[mDragOutlineCurrent].animateOut();
1240 mDragOutlineCurrent = (mDragOutlineCurrent + 1) % mDragOutlineAnims.length;
1241
1242 setHover(false);
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001243 }
1244
1245 /**
Winson Chungaafa03c2010-06-11 17:34:16 -07001246 * Mark a child as having been dropped.
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001247 * At the beginning of the drag operation, the child may have been on another
Patrick Dubroyce34a972010-10-19 10:34:32 -07001248 * screen, but it is re-parented before this method is called.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001249 *
1250 * @param child The child that is being dropped
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001251 */
Winson Chungaafa03c2010-06-11 17:34:16 -07001252 void onDropChild(View child) {
Romain Guyd94533d2009-08-17 10:01:15 -07001253 if (child != null) {
1254 LayoutParams lp = (LayoutParams) child.getLayoutParams();
Romain Guyd94533d2009-08-17 10:01:15 -07001255 lp.isDragging = false;
Romain Guy84f296c2009-11-04 15:00:44 -08001256 lp.dropped = true;
Patrick Dubroyce34a972010-10-19 10:34:32 -07001257 child.setVisibility(View.VISIBLE);
Romain Guyd94533d2009-08-17 10:01:15 -07001258 child.requestLayout();
Romain Guyd94533d2009-08-17 10:01:15 -07001259 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001260 }
1261
1262 void onDropAborted(View child) {
1263 if (child != null) {
Patrick Dubroyce34a972010-10-19 10:34:32 -07001264 LayoutParams lp = (LayoutParams) child.getLayoutParams();
1265 lp.isDragging = false;
1266 child.setVisibility(View.VISIBLE);
1267 animateChildIntoPosition(child);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001268 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001269 }
1270
1271 /**
1272 * Start dragging the specified child
Winson Chungaafa03c2010-06-11 17:34:16 -07001273 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001274 * @param child The child that is being dragged
1275 */
1276 void onDragChild(View child) {
1277 LayoutParams lp = (LayoutParams) child.getLayoutParams();
1278 lp.isDragging = true;
Patrick Dubroyce34a972010-10-19 10:34:32 -07001279 child.setVisibility(View.GONE);
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001280 }
1281
1282 /**
1283 * A drag event has begun over this layout.
1284 * It may have begun over this layout (in which case onDragChild is called first),
1285 * or it may have begun on another layout.
1286 */
Winson Chunga9abd0e2010-10-27 17:18:37 -07001287 void onDragEnter() {
Patrick Dubroyfe6bd872010-10-13 17:32:10 -07001288 if (!mDragging) {
Patrick Dubroyfe6bd872010-10-13 17:32:10 -07001289 // Fade in the drag indicators
1290 if (mCrosshairsAnimator != null) {
1291 mCrosshairsAnimator.animateIn();
1292 }
Joe Onorato4be866d2010-10-10 11:26:02 -07001293 }
1294 mDragging = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001295 }
Winson Chungaafa03c2010-06-11 17:34:16 -07001296
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001297 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001298 * Computes a bounding rectangle for a range of cells
Winson Chungaafa03c2010-06-11 17:34:16 -07001299 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001300 * @param cellX X coordinate of upper left corner expressed as a cell position
1301 * @param cellY Y coordinate of upper left corner expressed as a cell position
Winson Chungaafa03c2010-06-11 17:34:16 -07001302 * @param cellHSpan Width in cells
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001303 * @param cellVSpan Height in cells
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001304 * @param resultRect Rect into which to put the results
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001305 */
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001306 public void cellToRect(int cellX, int cellY, int cellHSpan, int cellVSpan, RectF resultRect) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001307 final int cellWidth = mCellWidth;
1308 final int cellHeight = mCellHeight;
1309 final int widthGap = mWidthGap;
1310 final int heightGap = mHeightGap;
Winson Chungaafa03c2010-06-11 17:34:16 -07001311
1312 final int hStartPadding = getLeftPadding();
1313 final int vStartPadding = getTopPadding();
1314
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001315 int width = cellHSpan * cellWidth + ((cellHSpan - 1) * widthGap);
1316 int height = cellVSpan * cellHeight + ((cellVSpan - 1) * heightGap);
1317
1318 int x = hStartPadding + cellX * (cellWidth + widthGap);
1319 int y = vStartPadding + cellY * (cellHeight + heightGap);
Winson Chungaafa03c2010-06-11 17:34:16 -07001320
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001321 resultRect.set(x, y, x + width, y + height);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001322 }
Winson Chungaafa03c2010-06-11 17:34:16 -07001323
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001324 /**
Winson Chungaafa03c2010-06-11 17:34:16 -07001325 * Computes the required horizontal and vertical cell spans to always
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001326 * fit the given rectangle.
Winson Chungaafa03c2010-06-11 17:34:16 -07001327 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001328 * @param width Width in pixels
1329 * @param height Height in pixels
Patrick Dubroy8f86ddc2010-07-16 13:55:32 -07001330 * @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 -08001331 */
Patrick Dubroy8f86ddc2010-07-16 13:55:32 -07001332 public int[] rectToCell(int width, int height, int[] result) {
Michael Jurka9987a5c2010-10-08 16:58:12 -07001333 return rectToCell(getResources(), width, height, result);
1334 }
1335
1336 public static int[] rectToCell(Resources resources, int width, int height, int[] result) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001337 // Always assume we're working with the smallest span to make sure we
1338 // reserve enough space in both orientations.
Joe Onorato79e56262009-09-21 15:23:04 -04001339 int actualWidth = resources.getDimensionPixelSize(R.dimen.workspace_cell_width);
1340 int actualHeight = resources.getDimensionPixelSize(R.dimen.workspace_cell_height);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001341 int smallerSize = Math.min(actualWidth, actualHeight);
Joe Onorato79e56262009-09-21 15:23:04 -04001342
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001343 // Always round up to next largest cell
1344 int spanX = (width + smallerSize) / smallerSize;
1345 int spanY = (height + smallerSize) / smallerSize;
Joe Onorato79e56262009-09-21 15:23:04 -04001346
Patrick Dubroy8f86ddc2010-07-16 13:55:32 -07001347 if (result == null) {
1348 return new int[] { spanX, spanY };
1349 }
1350 result[0] = spanX;
1351 result[1] = spanY;
1352 return result;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001353 }
1354
1355 /**
1356 * Find the first vacant cell, if there is one.
1357 *
1358 * @param vacant Holds the x and y coordinate of the vacant cell
1359 * @param spanX Horizontal cell span.
1360 * @param spanY Vertical cell span.
Winson Chungaafa03c2010-06-11 17:34:16 -07001361 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001362 * @return True if a vacant cell was found
1363 */
1364 public boolean getVacantCell(int[] vacant, int spanX, int spanY) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001365
Michael Jurka0280c3b2010-09-17 15:00:07 -07001366 return findVacantCell(vacant, spanX, spanY, mCountX, mCountY, mOccupied);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001367 }
1368
1369 static boolean findVacantCell(int[] vacant, int spanX, int spanY,
1370 int xCount, int yCount, boolean[][] occupied) {
1371
1372 for (int x = 0; x < xCount; x++) {
1373 for (int y = 0; y < yCount; y++) {
1374 boolean available = !occupied[x][y];
1375out: for (int i = x; i < x + spanX - 1 && x < xCount; i++) {
1376 for (int j = y; j < y + spanY - 1 && y < yCount; j++) {
1377 available = available && !occupied[i][j];
1378 if (!available) break out;
1379 }
1380 }
1381
1382 if (available) {
1383 vacant[0] = x;
1384 vacant[1] = y;
1385 return true;
1386 }
1387 }
1388 }
1389
1390 return false;
1391 }
1392
Michael Jurka0280c3b2010-09-17 15:00:07 -07001393 private void clearOccupiedCells() {
1394 for (int x = 0; x < mCountX; x++) {
1395 for (int y = 0; y < mCountY; y++) {
1396 mOccupied[x][y] = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001397 }
1398 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001399 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001400
Michael Jurka0280c3b2010-09-17 15:00:07 -07001401 public void onMove(View view, int newCellX, int newCellY) {
1402 LayoutParams lp = (LayoutParams) view.getLayoutParams();
1403 markCellsAsUnoccupiedForView(view);
1404 markCellsForView(newCellX, newCellY, lp.cellHSpan, lp.cellVSpan, true);
1405 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001406
Michael Jurka0280c3b2010-09-17 15:00:07 -07001407 private void markCellsAsOccupiedForView(View view) {
Michael Jurkac6ee42e2010-09-30 12:04:50 -07001408 if (view == null || view.getParent() != this) return;
Michael Jurka0280c3b2010-09-17 15:00:07 -07001409 LayoutParams lp = (LayoutParams) view.getLayoutParams();
1410 markCellsForView(lp.cellX, lp.cellY, lp.cellHSpan, lp.cellVSpan, true);
1411 }
1412
1413 private void markCellsAsUnoccupiedForView(View view) {
Michael Jurkac6ee42e2010-09-30 12:04:50 -07001414 if (view == null || view.getParent() != this) return;
Michael Jurka0280c3b2010-09-17 15:00:07 -07001415 LayoutParams lp = (LayoutParams) view.getLayoutParams();
1416 markCellsForView(lp.cellX, lp.cellY, lp.cellHSpan, lp.cellVSpan, false);
1417 }
1418
1419 private void markCellsForView(int cellX, int cellY, int spanX, int spanY, boolean value) {
1420 for (int x = cellX; x < cellX + spanX && x < mCountX; x++) {
1421 for (int y = cellY; y < cellY + spanY && y < mCountY; y++) {
1422 mOccupied[x][y] = value;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001423 }
1424 }
1425 }
1426
1427 @Override
1428 public ViewGroup.LayoutParams generateLayoutParams(AttributeSet attrs) {
1429 return new CellLayout.LayoutParams(getContext(), attrs);
1430 }
1431
1432 @Override
1433 protected boolean checkLayoutParams(ViewGroup.LayoutParams p) {
1434 return p instanceof CellLayout.LayoutParams;
1435 }
1436
1437 @Override
1438 protected ViewGroup.LayoutParams generateLayoutParams(ViewGroup.LayoutParams p) {
1439 return new CellLayout.LayoutParams(p);
1440 }
1441
Winson Chungaafa03c2010-06-11 17:34:16 -07001442 public static class CellLayoutAnimationController extends LayoutAnimationController {
1443 public CellLayoutAnimationController(Animation animation, float delay) {
1444 super(animation, delay);
1445 }
1446
1447 @Override
1448 protected long getDelayForView(View view) {
1449 return (int) (Math.random() * 150);
1450 }
1451 }
1452
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001453 public static class LayoutParams extends ViewGroup.MarginLayoutParams {
1454 /**
1455 * Horizontal location of the item in the grid.
1456 */
1457 @ViewDebug.ExportedProperty
1458 public int cellX;
1459
1460 /**
1461 * Vertical location of the item in the grid.
1462 */
1463 @ViewDebug.ExportedProperty
1464 public int cellY;
1465
1466 /**
1467 * Number of cells spanned horizontally by the item.
1468 */
1469 @ViewDebug.ExportedProperty
1470 public int cellHSpan;
1471
1472 /**
1473 * Number of cells spanned vertically by the item.
1474 */
1475 @ViewDebug.ExportedProperty
1476 public int cellVSpan;
Winson Chungaafa03c2010-06-11 17:34:16 -07001477
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001478 /**
1479 * Is this item currently being dragged
1480 */
1481 public boolean isDragging;
1482
1483 // X coordinate of the view in the layout.
1484 @ViewDebug.ExportedProperty
1485 int x;
1486 // Y coordinate of the view in the layout.
1487 @ViewDebug.ExportedProperty
1488 int y;
1489
Patrick Dubroyce34a972010-10-19 10:34:32 -07001490 /**
1491 * The old X coordinate of this item, relative to its current parent.
1492 * Used to animate the item into its new position.
1493 */
1494 int oldX;
1495
1496 /**
1497 * The old Y coordinate of this item, relative to its current parent.
1498 * Used to animate the item into its new position.
1499 */
1500 int oldY;
1501
Romain Guy84f296c2009-11-04 15:00:44 -08001502 boolean dropped;
Romain Guyfcb9e712009-10-02 16:06:52 -07001503
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001504 public LayoutParams(Context c, AttributeSet attrs) {
1505 super(c, attrs);
1506 cellHSpan = 1;
1507 cellVSpan = 1;
1508 }
1509
1510 public LayoutParams(ViewGroup.LayoutParams source) {
1511 super(source);
1512 cellHSpan = 1;
1513 cellVSpan = 1;
1514 }
Winson Chungaafa03c2010-06-11 17:34:16 -07001515
1516 public LayoutParams(LayoutParams source) {
1517 super(source);
1518 this.cellX = source.cellX;
1519 this.cellY = source.cellY;
1520 this.cellHSpan = source.cellHSpan;
1521 this.cellVSpan = source.cellVSpan;
1522 }
1523
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001524 public LayoutParams(int cellX, int cellY, int cellHSpan, int cellVSpan) {
Romain Guy8f19cdd2010-01-08 15:07:00 -08001525 super(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001526 this.cellX = cellX;
1527 this.cellY = cellY;
1528 this.cellHSpan = cellHSpan;
1529 this.cellVSpan = cellVSpan;
1530 }
1531
1532 public void setup(int cellWidth, int cellHeight, int widthGap, int heightGap,
1533 int hStartPadding, int vStartPadding) {
Winson Chungaafa03c2010-06-11 17:34:16 -07001534
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001535 final int myCellHSpan = cellHSpan;
1536 final int myCellVSpan = cellVSpan;
1537 final int myCellX = cellX;
1538 final int myCellY = cellY;
Winson Chungaafa03c2010-06-11 17:34:16 -07001539
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001540 width = myCellHSpan * cellWidth + ((myCellHSpan - 1) * widthGap) -
1541 leftMargin - rightMargin;
1542 height = myCellVSpan * cellHeight + ((myCellVSpan - 1) * heightGap) -
1543 topMargin - bottomMargin;
1544
1545 x = hStartPadding + myCellX * (cellWidth + widthGap) + leftMargin;
1546 y = vStartPadding + myCellY * (cellHeight + heightGap) + topMargin;
1547 }
Winson Chungaafa03c2010-06-11 17:34:16 -07001548
1549 public String toString() {
1550 return "(" + this.cellX + ", " + this.cellY + ")";
1551 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001552 }
1553
Michael Jurka0280c3b2010-09-17 15:00:07 -07001554 // This class stores info for two purposes:
1555 // 1. When dragging items (mDragInfo in Workspace), we store the View, its cellX & cellY,
1556 // its spanX, spanY, and the screen it is on
1557 // 2. When long clicking on an empty cell in a CellLayout, we save information about the
1558 // cellX and cellY coordinates and which page was clicked. We then set this as a tag on
1559 // the CellLayout that was long clicked
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001560 static final class CellInfo implements ContextMenu.ContextMenuInfo {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001561 View cell;
Michael Jurkaa63c4522010-08-19 13:52:27 -07001562 int cellX = -1;
1563 int cellY = -1;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001564 int spanX;
1565 int spanY;
1566 int screen;
1567 boolean valid;
1568
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001569 @Override
1570 public String toString() {
Winson Chungaafa03c2010-06-11 17:34:16 -07001571 return "Cell[view=" + (cell == null ? "null" : cell.getClass())
1572 + ", x=" + cellX + ", y=" + cellY + "]";
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001573 }
1574 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001575}