blob: 448b766041b54d866c9c95e2ae7ed4c2078138fd [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
Michael Jurka3c4c20f2010-10-28 15:36:06 -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;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080028import android.content.Context;
Joe Onorato79e56262009-09-21 15:23:04 -040029import android.content.res.Resources;
Winson Chungaafa03c2010-06-11 17:34:16 -070030import android.content.res.TypedArray;
Joe Onorato4be866d2010-10-10 11:26:02 -070031import android.graphics.Bitmap;
Winson Chungaafa03c2010-06-11 17:34:16 -070032import android.graphics.Canvas;
Joe Onorato4be866d2010-10-10 11:26:02 -070033import android.graphics.Paint;
Patrick Dubroyde7658b2010-09-27 11:15:43 -070034import android.graphics.Point;
35import android.graphics.PointF;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080036import android.graphics.Rect;
37import android.graphics.RectF;
Michael Jurka18014792010-10-14 09:01:34 -070038import android.graphics.Region;
Patrick Dubroy6569f2c2010-07-12 14:25:18 -070039import android.graphics.drawable.Drawable;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080040import android.util.AttributeSet;
Joe Onorato4be866d2010-10-10 11:26:02 -070041import android.util.Log;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080042import android.view.ContextMenu;
43import android.view.MotionEvent;
44import android.view.View;
45import android.view.ViewDebug;
46import android.view.ViewGroup;
Winson Chungaafa03c2010-06-11 17:34:16 -070047import android.view.animation.Animation;
Winson Chung150fbab2010-09-29 17:14:26 -070048import android.view.animation.DecelerateInterpolator;
Winson Chungaafa03c2010-06-11 17:34:16 -070049import android.view.animation.LayoutAnimationController;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080050
Michael Jurka3c4c20f2010-10-28 15:36:06 -070051import java.util.Arrays;
Patrick Dubroy8e58e912010-10-14 13:21:48 -070052
Michael Jurka8c920dd2011-01-20 14:16:56 -080053public class CellLayout extends ViewGroup {
Winson Chungaafa03c2010-06-11 17:34:16 -070054 static final String TAG = "CellLayout";
55
The Android Open Source Project31dd5032009-03-03 19:32:27 -080056 private int mCellWidth;
57 private int mCellHeight;
Winson Chungaafa03c2010-06-11 17:34:16 -070058
Winson Chungaafa03c2010-06-11 17:34:16 -070059 private int mLeftPadding;
60 private int mRightPadding;
61 private int mTopPadding;
62 private int mBottomPadding;
63
Adam Cohend22015c2010-07-26 22:02:18 -070064 private int mCountX;
65 private int mCountY;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080066
67 private int mWidthGap;
68 private int mHeightGap;
69
70 private final Rect mRect = new Rect();
71 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 Cohen1b0aaac2010-10-28 11:11:18 -070084 private float mBackgroundAlphaMultiplier = 1.0f;
Adam Cohenf34bab52010-09-30 14:11:56 -070085
Michael Jurka33945b22010-12-21 18:19:38 -080086 private Drawable mNormalBackground;
Michael Jurka33945b22010-12-21 18:19:38 -080087 private Drawable mActiveBackground;
88 private Drawable mActiveGlowBackground;
89 private Drawable mNormalBackgroundMini;
90 private Drawable mNormalGlowBackgroundMini;
91 private Drawable mActiveBackgroundMini;
92 private Drawable mActiveGlowBackgroundMini;
Michael Jurka18014792010-10-14 09:01:34 -070093 private Rect mBackgroundRect;
Michael Jurka33945b22010-12-21 18:19:38 -080094 private Rect mGlowBackgroundRect;
95 private float mGlowBackgroundScale;
96 private float mGlowBackgroundAlpha;
Patrick Dubroy1262e362010-10-06 15:49:50 -070097
Michael Jurka33945b22010-12-21 18:19:38 -080098 private boolean mAcceptsDrops = false;
99 // If we're actively dragging something over this screen, mIsDragOverlapping is true
100 private boolean mIsDragOverlapping = false;
101 private boolean mIsDragOccuring = false;
102 private boolean mIsDefaultDropTarget = false;
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700103 private final Point mDragCenter = new Point();
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700104
Winson Chung150fbab2010-09-29 17:14:26 -0700105 // These arrays are used to implement the drag visualization on x-large screens.
Joe Onorato4be866d2010-10-10 11:26:02 -0700106 // They are used as circular arrays, indexed by mDragOutlineCurrent.
107 private Point[] mDragOutlines = new Point[8];
Chet Haase472b2812010-10-14 07:02:04 -0700108 private float[] mDragOutlineAlphas = new float[mDragOutlines.length];
Joe Onorato4be866d2010-10-10 11:26:02 -0700109 private InterruptibleInOutAnimator[] mDragOutlineAnims =
110 new InterruptibleInOutAnimator[mDragOutlines.length];
Winson Chung150fbab2010-09-29 17:14:26 -0700111
112 // Used as an index into the above 3 arrays; indicates which is the most current value.
Joe Onorato4be866d2010-10-10 11:26:02 -0700113 private int mDragOutlineCurrent = 0;
Patrick Dubroy8e58e912010-10-14 13:21:48 -0700114 private final Paint mDragOutlinePaint = new Paint();
Winson Chung150fbab2010-09-29 17:14:26 -0700115
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700116 private Drawable mCrosshairsDrawable = null;
Patrick Dubroy49250ad2010-10-08 15:33:52 -0700117 private InterruptibleInOutAnimator mCrosshairsAnimator = null;
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700118 private float mCrosshairsVisibility = 0.0f;
119
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700120 // When a drag operation is in progress, holds the nearest cell to the touch point
121 private final int[] mDragCell = new int[2];
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800122
Joe Onorato4be866d2010-10-10 11:26:02 -0700123 private boolean mDragging = false;
124
Patrick Dubroyce34a972010-10-19 10:34:32 -0700125 private TimeInterpolator mEaseOutInterpolator;
Michael Jurka8c920dd2011-01-20 14:16:56 -0800126 private CellLayoutChildren mChildren;
Patrick Dubroyce34a972010-10-19 10:34:32 -0700127
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800128 public CellLayout(Context context) {
129 this(context, null);
130 }
131
132 public CellLayout(Context context, AttributeSet attrs) {
133 this(context, attrs, 0);
134 }
135
136 public CellLayout(Context context, AttributeSet attrs, int defStyle) {
137 super(context, attrs, defStyle);
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700138
139 // A ViewGroup usually does not draw, but CellLayout needs to draw a rectangle to show
140 // the user where a dragged item will land when dropped.
141 setWillNotDraw(false);
Michael Jurkaa63c4522010-08-19 13:52:27 -0700142
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800143 TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.CellLayout, defStyle, 0);
144
145 mCellWidth = a.getDimensionPixelSize(R.styleable.CellLayout_cellWidth, 10);
146 mCellHeight = a.getDimensionPixelSize(R.styleable.CellLayout_cellHeight, 10);
Winson Chungece7f5b2010-10-22 14:54:12 -0700147 mWidthGap = a.getDimensionPixelSize(R.styleable.CellLayout_widthGap, -1);
148 mHeightGap = a.getDimensionPixelSize(R.styleable.CellLayout_heightGap, -1);
Winson Chungaafa03c2010-06-11 17:34:16 -0700149
Adam Cohend22015c2010-07-26 22:02:18 -0700150 mLeftPadding =
151 a.getDimensionPixelSize(R.styleable.CellLayout_xAxisStartPadding, 10);
152 mRightPadding =
153 a.getDimensionPixelSize(R.styleable.CellLayout_xAxisEndPadding, 10);
154 mTopPadding =
155 a.getDimensionPixelSize(R.styleable.CellLayout_yAxisStartPadding, 10);
156 mBottomPadding =
157 a.getDimensionPixelSize(R.styleable.CellLayout_yAxisEndPadding, 10);
Winson Chungaafa03c2010-06-11 17:34:16 -0700158
Adam Cohend22015c2010-07-26 22:02:18 -0700159 mCountX = LauncherModel.getCellCountX();
160 mCountY = LauncherModel.getCellCountY();
Michael Jurka0280c3b2010-09-17 15:00:07 -0700161 mOccupied = new boolean[mCountX][mCountY];
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800162
163 a.recycle();
164
165 setAlwaysDrawnWithCacheEnabled(false);
166
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700167 final Resources res = getResources();
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700168
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700169 if (LauncherApplication.isScreenXLarge()) {
Michael Jurka33945b22010-12-21 18:19:38 -0800170 mNormalBackground = res.getDrawable(R.drawable.homescreen_large_blue);
Michael Jurka33945b22010-12-21 18:19:38 -0800171 mActiveBackground = res.getDrawable(R.drawable.homescreen_large_green);
172 mActiveGlowBackground = res.getDrawable(R.drawable.homescreen_large_green_strong);
173
174 mNormalBackgroundMini = res.getDrawable(R.drawable.homescreen_small_blue);
175 mNormalGlowBackgroundMini = res.getDrawable(R.drawable.homescreen_small_blue_strong);
176 mActiveBackgroundMini = res.getDrawable(R.drawable.homescreen_small_green);
177 mActiveGlowBackgroundMini = res.getDrawable(R.drawable.homescreen_small_green_strong);
178
179 mNormalBackground.setFilterBitmap(true);
Michael Jurka33945b22010-12-21 18:19:38 -0800180 mActiveBackground.setFilterBitmap(true);
181 mActiveGlowBackground.setFilterBitmap(true);
182 mNormalBackgroundMini.setFilterBitmap(true);
183 mNormalGlowBackgroundMini.setFilterBitmap(true);
184 mActiveBackgroundMini.setFilterBitmap(true);
185 mActiveGlowBackgroundMini.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() {
Michael Jurka3c4c20f2010-10-28 15:36:06 -0700249 @Override
Joe Onorato4be866d2010-10-10 11:26:02 -0700250 public void onAnimationEnd(Animator animation) {
Chet Haase472b2812010-10-14 07:02:04 -0700251 if ((Float) ((ValueAnimator) animation).getAnimatedValue() == 0f) {
Joe Onorato4be866d2010-10-10 11:26:02 -0700252 anim.setTag(null);
253 }
254 }
255 });
256 mDragOutlineAnims[i] = anim;
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700257 }
Patrick Dubroyce34a972010-10-19 10:34:32 -0700258
Michael Jurka18014792010-10-14 09:01:34 -0700259 mBackgroundRect = new Rect();
Michael Jurka33945b22010-12-21 18:19:38 -0800260 mGlowBackgroundRect = new Rect();
Michael Jurka18014792010-10-14 09:01:34 -0700261 setHoverScale(1.0f);
262 setHoverAlpha(1.0f);
Michael Jurkabea15192010-11-17 12:33:46 -0800263
Michael Jurka8c920dd2011-01-20 14:16:56 -0800264 mChildren = new CellLayoutChildren(context);
265 mChildren.setCellDimensions(
266 mCellWidth, mCellHeight, mLeftPadding, mTopPadding, mWidthGap, mHeightGap);
267 addView(mChildren);
Michael Jurka18014792010-10-14 09:01:34 -0700268 }
269
Winson Chung6e314082011-01-27 16:46:51 -0800270 public CellLayoutChildren getChildrenLayout() {
271 if (getChildCount() > 0) {
272 return (CellLayoutChildren) getChildAt(0);
273 }
274 return null;
275 }
276
Michael Jurka33945b22010-12-21 18:19:38 -0800277 public void setIsDefaultDropTarget(boolean isDefaultDropTarget) {
278 if (mIsDefaultDropTarget != isDefaultDropTarget) {
279 mIsDefaultDropTarget = isDefaultDropTarget;
280 invalidate();
281 }
282 }
283
Michael Jurka33945b22010-12-21 18:19:38 -0800284 void setIsDragOccuring(boolean isDragOccuring) {
285 if (mIsDragOccuring != isDragOccuring) {
286 mIsDragOccuring = isDragOccuring;
287 invalidate();
288 }
289 }
290
291 void setIsDragOverlapping(boolean isDragOverlapping) {
292 if (mIsDragOverlapping != isDragOverlapping) {
293 mIsDragOverlapping = isDragOverlapping;
294 invalidate();
295 }
296 }
297
298 boolean getIsDragOverlapping() {
299 return mIsDragOverlapping;
300 }
301
302 private void updateGlowRect() {
303 float marginFraction = (mGlowBackgroundScale - 1.0f) / 2.0f;
Michael Jurka18014792010-10-14 09:01:34 -0700304 int marginX = (int) (marginFraction * (mBackgroundRect.right - mBackgroundRect.left));
305 int marginY = (int) (marginFraction * (mBackgroundRect.bottom - mBackgroundRect.top));
Michael Jurka33945b22010-12-21 18:19:38 -0800306 mGlowBackgroundRect.set(mBackgroundRect.left - marginX, mBackgroundRect.top - marginY,
Michael Jurka18014792010-10-14 09:01:34 -0700307 mBackgroundRect.right + marginX, mBackgroundRect.bottom + marginY);
308 invalidate();
309 }
310
311 public void setHoverScale(float scaleFactor) {
Michael Jurka33945b22010-12-21 18:19:38 -0800312 if (scaleFactor != mGlowBackgroundScale) {
313 mGlowBackgroundScale = scaleFactor;
314 updateGlowRect();
Michael Jurka8deb1e62011-01-25 16:27:43 -0800315 if (getParent() != null) {
316 ((View) getParent()).invalidate();
317 }
Michael Jurka18014792010-10-14 09:01:34 -0700318 }
319 }
320
321 public float getHoverScale() {
Michael Jurka33945b22010-12-21 18:19:38 -0800322 return mGlowBackgroundScale;
Michael Jurka18014792010-10-14 09:01:34 -0700323 }
324
325 public float getHoverAlpha() {
Michael Jurka33945b22010-12-21 18:19:38 -0800326 return mGlowBackgroundAlpha;
Michael Jurka18014792010-10-14 09:01:34 -0700327 }
328
329 public void setHoverAlpha(float alpha) {
Michael Jurka33945b22010-12-21 18:19:38 -0800330 mGlowBackgroundAlpha = alpha;
Michael Jurka18014792010-10-14 09:01:34 -0700331 invalidate();
332 }
333
334 void animateDrop() {
335 if (LauncherApplication.isScreenXLarge()) {
336 Resources res = getResources();
337 float onDropScale = res.getInteger(R.integer.config_screenOnDropScalePercent) / 100.0f;
338 ObjectAnimator scaleUp = ObjectAnimator.ofFloat(this, "hoverScale", onDropScale);
339 scaleUp.setDuration(res.getInteger(R.integer.config_screenOnDropScaleUpDuration));
340 ObjectAnimator scaleDown = ObjectAnimator.ofFloat(this, "hoverScale", 1.0f);
341 scaleDown.setDuration(res.getInteger(R.integer.config_screenOnDropScaleDownDuration));
342 ObjectAnimator alphaFadeOut = ObjectAnimator.ofFloat(this, "hoverAlpha", 0.0f);
343
344 alphaFadeOut.setStartDelay(res.getInteger(R.integer.config_screenOnDropAlphaFadeDelay));
345 alphaFadeOut.setDuration(res.getInteger(R.integer.config_screenOnDropAlphaFadeDelay));
346
347 AnimatorSet bouncer = new AnimatorSet();
348 bouncer.play(scaleUp).before(scaleDown);
349 bouncer.play(scaleUp).with(alphaFadeOut);
Michael Jurka8edd75c2010-12-17 20:15:06 -0800350 bouncer.addListener(new AnimatorListenerAdapter() {
Michael Jurka3c4c20f2010-10-28 15:36:06 -0700351 @Override
Michael Jurka18014792010-10-14 09:01:34 -0700352 public void onAnimationStart(Animator animation) {
Michael Jurka33945b22010-12-21 18:19:38 -0800353 setIsDragOverlapping(true);
Michael Jurka18014792010-10-14 09:01:34 -0700354 }
Michael Jurka3c4c20f2010-10-28 15:36:06 -0700355 @Override
Michael Jurka8edd75c2010-12-17 20:15:06 -0800356 public void onAnimationEnd(Animator animation) {
Michael Jurka33945b22010-12-21 18:19:38 -0800357 setIsDragOverlapping(false);
Michael Jurka18014792010-10-14 09:01:34 -0700358 setHoverScale(1.0f);
359 setHoverAlpha(1.0f);
360 }
361 });
362 bouncer.start();
363 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800364 }
365
Michael Jurkabea15192010-11-17 12:33:46 -0800366 public void disableCacheUpdates() {
Michael Jurka8c920dd2011-01-20 14:16:56 -0800367 mChildren.disableCacheUpdates();
Michael Jurkabea15192010-11-17 12:33:46 -0800368 }
369
370 public void enableCacheUpdates() {
Michael Jurka8c920dd2011-01-20 14:16:56 -0800371 mChildren.enableCacheUpdates();
Michael Jurkabea15192010-11-17 12:33:46 -0800372 }
373
Jeff Sharkey83f111d2009-04-20 21:03:13 -0700374 @Override
Patrick Dubroy1262e362010-10-06 15:49:50 -0700375 protected void onDraw(Canvas canvas) {
Michael Jurka3e7c7632010-10-02 16:01:03 -0700376 // When we're large, we are either drawn in a "hover" state (ie when dragging an item to
377 // a neighboring page) or with just a normal background (if backgroundAlpha > 0.0f)
378 // When we're small, we are either drawn normally or in the "accepts drops" state (during
379 // a drag). However, we also drag the mini hover background *over* one of those two
380 // backgrounds
Winson Chung26cbf3a2011-01-06 16:25:55 -0800381 if (LauncherApplication.isScreenXLarge() && mBackgroundAlpha > 0.0f) {
Adam Cohenf34bab52010-09-30 14:11:56 -0700382 Drawable bg;
Michael Jurka33945b22010-12-21 18:19:38 -0800383 boolean mini = getScaleX() < 0.5f;
384
385 if (mIsDragOverlapping) {
386 // In the mini case, we draw the active_glow bg *over* the active background
387 bg = mini ? mActiveBackgroundMini : mActiveGlowBackground;
388 } else if (mIsDragOccuring && mAcceptsDrops) {
389 bg = mini ? mActiveBackgroundMini : mActiveBackground;
Adam Cohen3af863b2011-01-25 12:16:51 -0800390 } else if (mIsDefaultDropTarget && mini) {
391 bg = mNormalGlowBackgroundMini;
Adam Cohenf34bab52010-09-30 14:11:56 -0700392 } else {
Michael Jurka33945b22010-12-21 18:19:38 -0800393 bg = mini ? mNormalBackgroundMini : mNormalBackground;
Adam Cohenf34bab52010-09-30 14:11:56 -0700394 }
Michael Jurka33945b22010-12-21 18:19:38 -0800395
396 bg.setAlpha((int) (mBackgroundAlpha * mBackgroundAlphaMultiplier * 255));
397 bg.setBounds(mBackgroundRect);
398 bg.draw(canvas);
399
400 if (mini && mIsDragOverlapping) {
Michael Jurka18014792010-10-14 09:01:34 -0700401 boolean modifiedClipRect = false;
Michael Jurka33945b22010-12-21 18:19:38 -0800402 if (mGlowBackgroundScale > 1.0f) {
Michael Jurka18014792010-10-14 09:01:34 -0700403 // If the hover background's scale is greater than 1, we'll be drawing outside
404 // the bounds of this CellLayout. Get around that by temporarily increasing the
405 // size of the clip rect
Michael Jurka33945b22010-12-21 18:19:38 -0800406 float marginFraction = (mGlowBackgroundScale - 1.0f) / 2.0f;
Michael Jurka18014792010-10-14 09:01:34 -0700407 Rect clipRect = canvas.getClipBounds();
408 int marginX = (int) (marginFraction * (clipRect.right - clipRect.left));
409 int marginY = (int) (marginFraction * (clipRect.bottom - clipRect.top));
410 canvas.save(Canvas.CLIP_SAVE_FLAG);
411 canvas.clipRect(-marginX, -marginY,
412 getWidth() + marginX, getHeight() + marginY, Region.Op.REPLACE);
413 modifiedClipRect = true;
414 }
415
Michael Jurka33945b22010-12-21 18:19:38 -0800416 mActiveGlowBackgroundMini.setAlpha(
417 (int) (mBackgroundAlpha * mGlowBackgroundAlpha * 255));
418 mActiveGlowBackgroundMini.setBounds(mGlowBackgroundRect);
419 mActiveGlowBackgroundMini.draw(canvas);
Michael Jurka18014792010-10-14 09:01:34 -0700420 if (modifiedClipRect) {
421 canvas.restore();
422 }
Michael Jurka3e7c7632010-10-02 16:01:03 -0700423 }
Michael Jurkaa63c4522010-08-19 13:52:27 -0700424 }
Romain Guya6abce82009-11-10 02:54:41 -0800425
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700426 if (mCrosshairsVisibility > 0.0f) {
427 final int countX = mCountX;
428 final int countY = mCountY;
429
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700430 final float MAX_ALPHA = 0.4f;
431 final int MAX_VISIBLE_DISTANCE = 600;
432 final float DISTANCE_MULTIPLIER = 0.002f;
433
434 final Drawable d = mCrosshairsDrawable;
435 final int width = d.getIntrinsicWidth();
436 final int height = d.getIntrinsicHeight();
437
438 int x = getLeftPadding() - (mWidthGap / 2) - (width / 2);
439 for (int col = 0; col <= countX; col++) {
440 int y = getTopPadding() - (mHeightGap / 2) - (height / 2);
441 for (int row = 0; row <= countY; row++) {
442 mTmpPointF.set(x - mDragCenter.x, y - mDragCenter.y);
443 float dist = mTmpPointF.length();
444 // Crosshairs further from the drag point are more faint
445 float alpha = Math.min(MAX_ALPHA,
446 DISTANCE_MULTIPLIER * (MAX_VISIBLE_DISTANCE - dist));
447 if (alpha > 0.0f) {
448 d.setBounds(x, y, x + width, y + height);
449 d.setAlpha((int) (alpha * 255 * mCrosshairsVisibility));
450 d.draw(canvas);
451 }
452 y += mCellHeight + mHeightGap;
453 }
454 x += mCellWidth + mWidthGap;
455 }
Joe Onorato4be866d2010-10-10 11:26:02 -0700456 }
Winson Chung150fbab2010-09-29 17:14:26 -0700457
Patrick Dubroy8e58e912010-10-14 13:21:48 -0700458 final Paint paint = mDragOutlinePaint;
Joe Onorato4be866d2010-10-10 11:26:02 -0700459 for (int i = 0; i < mDragOutlines.length; i++) {
Chet Haase472b2812010-10-14 07:02:04 -0700460 final float alpha = mDragOutlineAlphas[i];
Joe Onorato4be866d2010-10-10 11:26:02 -0700461 if (alpha > 0) {
462 final Point p = mDragOutlines[i];
463 final Bitmap b = (Bitmap) mDragOutlineAnims[i].getTag();
Chet Haase472b2812010-10-14 07:02:04 -0700464 paint.setAlpha((int)(alpha + .5f));
Joe Onorato4be866d2010-10-10 11:26:02 -0700465 canvas.drawBitmap(b, p.x, p.y, paint);
Winson Chung150fbab2010-09-29 17:14:26 -0700466 }
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700467 }
468 }
469
470 @Override
Jeff Sharkey83f111d2009-04-20 21:03:13 -0700471 public void cancelLongPress() {
472 super.cancelLongPress();
473
474 // Cancel long press for all children
475 final int count = getChildCount();
476 for (int i = 0; i < count; i++) {
477 final View child = getChildAt(i);
478 child.cancelLongPress();
479 }
480 }
481
Michael Jurkadee05892010-07-27 10:01:56 -0700482 public void setOnInterceptTouchListener(View.OnTouchListener listener) {
483 mInterceptTouchListener = listener;
484 }
485
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800486 int getCountX() {
Adam Cohend22015c2010-07-26 22:02:18 -0700487 return mCountX;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800488 }
489
490 int getCountY() {
Adam Cohend22015c2010-07-26 22:02:18 -0700491 return mCountY;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800492 }
493
Winson Chungaafa03c2010-06-11 17:34:16 -0700494 public boolean addViewToCellLayout(View child, int index, int childId, LayoutParams params) {
Michael Jurkaf3ca3ab2010-10-20 17:08:24 -0700495 return addViewToCellLayout(child, index, childId, params, true);
496 }
497
498 public boolean addViewToCellLayout(
499 View child, int index, int childId, LayoutParams params, boolean markCells) {
Winson Chungaafa03c2010-06-11 17:34:16 -0700500 final LayoutParams lp = params;
501
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800502 // Generate an id for each view, this assumes we have at most 256x256 cells
503 // per workspace screen
Adam Cohend22015c2010-07-26 22:02:18 -0700504 if (lp.cellX >= 0 && lp.cellX <= mCountX - 1 && lp.cellY >= 0 && lp.cellY <= mCountY - 1) {
Winson Chungaafa03c2010-06-11 17:34:16 -0700505 // If the horizontal or vertical span is set to -1, it is taken to
506 // mean that it spans the extent of the CellLayout
Adam Cohend22015c2010-07-26 22:02:18 -0700507 if (lp.cellHSpan < 0) lp.cellHSpan = mCountX;
508 if (lp.cellVSpan < 0) lp.cellVSpan = mCountY;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800509
Winson Chungaafa03c2010-06-11 17:34:16 -0700510 child.setId(childId);
511
Michael Jurka8c920dd2011-01-20 14:16:56 -0800512 mChildren.addView(child, index, lp);
Michael Jurkadee05892010-07-27 10:01:56 -0700513
Michael Jurkaf3ca3ab2010-10-20 17:08:24 -0700514 if (markCells) markCellsAsOccupiedForView(child);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700515
Winson Chungaafa03c2010-06-11 17:34:16 -0700516 return true;
517 }
518 return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800519 }
Michael Jurka3e7c7632010-10-02 16:01:03 -0700520
Michael Jurkabea15192010-11-17 12:33:46 -0800521 public void setAcceptsDrops(boolean acceptsDrops) {
522 if (mAcceptsDrops != acceptsDrops) {
523 mAcceptsDrops = acceptsDrops;
524 invalidate();
525 }
526 }
527
Michael Jurka3e7c7632010-10-02 16:01:03 -0700528 public boolean getAcceptsDrops() {
529 return mAcceptsDrops;
530 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800531
532 @Override
Michael Jurka0280c3b2010-09-17 15:00:07 -0700533 public void removeAllViews() {
534 clearOccupiedCells();
Michael Jurka8c920dd2011-01-20 14:16:56 -0800535 mChildren.removeAllViews();
Michael Jurka0280c3b2010-09-17 15:00:07 -0700536 }
537
538 @Override
539 public void removeAllViewsInLayout() {
540 clearOccupiedCells();
Michael Jurka8c920dd2011-01-20 14:16:56 -0800541 mChildren.removeAllViewsInLayout();
Michael Jurka0280c3b2010-09-17 15:00:07 -0700542 }
543
Michael Jurkaf3ca3ab2010-10-20 17:08:24 -0700544 public void removeViewWithoutMarkingCells(View view) {
Michael Jurka8c920dd2011-01-20 14:16:56 -0800545 mChildren.removeViewWithoutMarkingCells(view);
Michael Jurkaf3ca3ab2010-10-20 17:08:24 -0700546 }
547
Michael Jurka0280c3b2010-09-17 15:00:07 -0700548 @Override
549 public void removeView(View view) {
550 markCellsAsUnoccupiedForView(view);
Michael Jurka8c920dd2011-01-20 14:16:56 -0800551 mChildren.removeView(view);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700552 }
553
554 @Override
555 public void removeViewAt(int index) {
Michael Jurka8c920dd2011-01-20 14:16:56 -0800556 markCellsAsUnoccupiedForView(mChildren.getChildAt(index));
557 mChildren.removeViewAt(index);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700558 }
559
560 @Override
561 public void removeViewInLayout(View view) {
562 markCellsAsUnoccupiedForView(view);
Michael Jurka8c920dd2011-01-20 14:16:56 -0800563 mChildren.removeViewInLayout(view);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700564 }
565
566 @Override
567 public void removeViews(int start, int count) {
568 for (int i = start; i < start + count; i++) {
Michael Jurka8c920dd2011-01-20 14:16:56 -0800569 markCellsAsUnoccupiedForView(mChildren.getChildAt(i));
Michael Jurka0280c3b2010-09-17 15:00:07 -0700570 }
Michael Jurka8c920dd2011-01-20 14:16:56 -0800571 mChildren.removeViews(start, count);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700572 }
573
574 @Override
575 public void removeViewsInLayout(int start, int count) {
576 for (int i = start; i < start + count; i++) {
Michael Jurka8c920dd2011-01-20 14:16:56 -0800577 markCellsAsUnoccupiedForView(mChildren.getChildAt(i));
Michael Jurka0280c3b2010-09-17 15:00:07 -0700578 }
Michael Jurka8c920dd2011-01-20 14:16:56 -0800579 mChildren.removeViewsInLayout(start, count);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700580 }
581
Michael Jurka8c920dd2011-01-20 14:16:56 -0800582 public void drawChildren(Canvas canvas) {
583 mChildren.draw(canvas);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800584 }
585
586 @Override
587 protected void onAttachedToWindow() {
588 super.onAttachedToWindow();
589 mCellInfo.screen = ((ViewGroup) getParent()).indexOfChild(this);
590 }
591
Michael Jurkaaf442092010-06-10 17:01:57 -0700592 public void setTagToCellInfoForPoint(int touchX, int touchY) {
593 final CellInfo cellInfo = mCellInfo;
594 final Rect frame = mRect;
595 final int x = touchX + mScrollX;
596 final int y = touchY + mScrollY;
Michael Jurka8c920dd2011-01-20 14:16:56 -0800597 final int count = mChildren.getChildCount();
Michael Jurkaaf442092010-06-10 17:01:57 -0700598
599 boolean found = false;
600 for (int i = count - 1; i >= 0; i--) {
Michael Jurka8c920dd2011-01-20 14:16:56 -0800601 final View child = mChildren.getChildAt(i);
Michael Jurkaaf442092010-06-10 17:01:57 -0700602
603 if ((child.getVisibility()) == VISIBLE || child.getAnimation() != null) {
604 child.getHitRect(frame);
605 if (frame.contains(x, y)) {
606 final LayoutParams lp = (LayoutParams) child.getLayoutParams();
607 cellInfo.cell = child;
608 cellInfo.cellX = lp.cellX;
609 cellInfo.cellY = lp.cellY;
610 cellInfo.spanX = lp.cellHSpan;
611 cellInfo.spanY = lp.cellVSpan;
612 cellInfo.valid = true;
613 found = true;
Michael Jurkaaf442092010-06-10 17:01:57 -0700614 break;
615 }
616 }
617 }
Winson Chungaafa03c2010-06-11 17:34:16 -0700618
Michael Jurkaaf442092010-06-10 17:01:57 -0700619 if (!found) {
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700620 final int cellXY[] = mTmpCellXY;
Michael Jurkaaf442092010-06-10 17:01:57 -0700621 pointToCellExact(x, y, cellXY);
622
Michael Jurkaaf442092010-06-10 17:01:57 -0700623 cellInfo.cell = null;
624 cellInfo.cellX = cellXY[0];
625 cellInfo.cellY = cellXY[1];
626 cellInfo.spanX = 1;
627 cellInfo.spanY = 1;
Michael Jurka0280c3b2010-09-17 15:00:07 -0700628 cellInfo.valid = cellXY[0] >= 0 && cellXY[1] >= 0 && cellXY[0] < mCountX &&
629 cellXY[1] < mCountY && !mOccupied[cellXY[0]][cellXY[1]];
Michael Jurkaaf442092010-06-10 17:01:57 -0700630 }
631 setTag(cellInfo);
632 }
633
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800634 @Override
635 public boolean onInterceptTouchEvent(MotionEvent ev) {
Michael Jurkadee05892010-07-27 10:01:56 -0700636 if (mInterceptTouchListener != null && mInterceptTouchListener.onTouch(this, ev)) {
637 return true;
638 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800639 final int action = ev.getAction();
640 final CellInfo cellInfo = mCellInfo;
641
642 if (action == MotionEvent.ACTION_DOWN) {
Michael Jurkaaf442092010-06-10 17:01:57 -0700643 setTagToCellInfoForPoint((int) ev.getX(), (int) ev.getY());
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800644 } else if (action == MotionEvent.ACTION_UP) {
645 cellInfo.cell = null;
646 cellInfo.cellX = -1;
647 cellInfo.cellY = -1;
648 cellInfo.spanX = 0;
649 cellInfo.spanY = 0;
650 cellInfo.valid = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800651 setTag(cellInfo);
652 }
653
654 return false;
655 }
656
657 @Override
658 public CellInfo getTag() {
Michael Jurka0280c3b2010-09-17 15:00:07 -0700659 return (CellInfo) super.getTag();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800660 }
661
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700662 /**
Winson Chungaafa03c2010-06-11 17:34:16 -0700663 * Given a point, return the cell that strictly encloses that point
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800664 * @param x X coordinate of the point
665 * @param y Y coordinate of the point
666 * @param result Array of 2 ints to hold the x and y coordinate of the cell
667 */
668 void pointToCellExact(int x, int y, int[] result) {
Winson Chungaafa03c2010-06-11 17:34:16 -0700669 final int hStartPadding = getLeftPadding();
670 final int vStartPadding = getTopPadding();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800671
672 result[0] = (x - hStartPadding) / (mCellWidth + mWidthGap);
673 result[1] = (y - vStartPadding) / (mCellHeight + mHeightGap);
674
Adam Cohend22015c2010-07-26 22:02:18 -0700675 final int xAxis = mCountX;
676 final int yAxis = mCountY;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800677
678 if (result[0] < 0) result[0] = 0;
679 if (result[0] >= xAxis) result[0] = xAxis - 1;
680 if (result[1] < 0) result[1] = 0;
681 if (result[1] >= yAxis) result[1] = yAxis - 1;
682 }
Winson Chungaafa03c2010-06-11 17:34:16 -0700683
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800684 /**
685 * Given a point, return the cell that most closely encloses that point
686 * @param x X coordinate of the point
687 * @param y Y coordinate of the point
688 * @param result Array of 2 ints to hold the x and y coordinate of the cell
689 */
690 void pointToCellRounded(int x, int y, int[] result) {
691 pointToCellExact(x + (mCellWidth / 2), y + (mCellHeight / 2), result);
692 }
693
694 /**
695 * Given a cell coordinate, return the point that represents the upper left corner of that cell
Winson Chungaafa03c2010-06-11 17:34:16 -0700696 *
697 * @param cellX X coordinate of the cell
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800698 * @param cellY Y coordinate of the cell
Winson Chungaafa03c2010-06-11 17:34:16 -0700699 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800700 * @param result Array of 2 ints to hold the x and y coordinate of the point
701 */
702 void cellToPoint(int cellX, int cellY, int[] result) {
Winson Chungaafa03c2010-06-11 17:34:16 -0700703 final int hStartPadding = getLeftPadding();
704 final int vStartPadding = getTopPadding();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800705
706 result[0] = hStartPadding + cellX * (mCellWidth + mWidthGap);
707 result[1] = vStartPadding + cellY * (mCellHeight + mHeightGap);
708 }
709
Romain Guy84f296c2009-11-04 15:00:44 -0800710 int getCellWidth() {
711 return mCellWidth;
712 }
713
714 int getCellHeight() {
715 return mCellHeight;
716 }
717
Romain Guy1a304a12009-11-10 00:02:32 -0800718 int getLeftPadding() {
Winson Chungaafa03c2010-06-11 17:34:16 -0700719 return mLeftPadding;
Romain Guy1a304a12009-11-10 00:02:32 -0800720 }
721
722 int getTopPadding() {
Winson Chungaafa03c2010-06-11 17:34:16 -0700723 return mTopPadding;
Romain Guy1a304a12009-11-10 00:02:32 -0800724 }
725
726 int getRightPadding() {
Winson Chungaafa03c2010-06-11 17:34:16 -0700727 return mRightPadding;
Romain Guy1a304a12009-11-10 00:02:32 -0800728 }
729
730 int getBottomPadding() {
Winson Chungaafa03c2010-06-11 17:34:16 -0700731 return mBottomPadding;
Romain Guy1a304a12009-11-10 00:02:32 -0800732 }
733
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800734 @Override
735 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
736 // TODO: currently ignoring padding
Winson Chungaafa03c2010-06-11 17:34:16 -0700737
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800738 int widthSpecMode = MeasureSpec.getMode(widthMeasureSpec);
Winson Chungaafa03c2010-06-11 17:34:16 -0700739 int widthSpecSize = MeasureSpec.getSize(widthMeasureSpec);
740
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800741 int heightSpecMode = MeasureSpec.getMode(heightMeasureSpec);
742 int heightSpecSize = MeasureSpec.getSize(heightMeasureSpec);
Winson Chungaafa03c2010-06-11 17:34:16 -0700743
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800744 if (widthSpecMode == MeasureSpec.UNSPECIFIED || heightSpecMode == MeasureSpec.UNSPECIFIED) {
745 throw new RuntimeException("CellLayout cannot have UNSPECIFIED dimensions");
746 }
747
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800748 final int cellWidth = mCellWidth;
749 final int cellHeight = mCellHeight;
750
Adam Cohend22015c2010-07-26 22:02:18 -0700751 int numWidthGaps = mCountX - 1;
752 int numHeightGaps = mCountY - 1;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800753
Winson Chungece7f5b2010-10-22 14:54:12 -0700754 if (mWidthGap < 0 || mHeightGap < 0) {
755 int vSpaceLeft = heightSpecSize - mTopPadding - mBottomPadding - (cellHeight * mCountY);
756 mHeightGap = vSpaceLeft / numHeightGaps;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800757
Winson Chungece7f5b2010-10-22 14:54:12 -0700758 int hSpaceLeft = widthSpecSize - mLeftPadding - mRightPadding - (cellWidth * mCountX);
759 mWidthGap = hSpaceLeft / numWidthGaps;
Winson Chungaafa03c2010-06-11 17:34:16 -0700760
Winson Chungece7f5b2010-10-22 14:54:12 -0700761 // center it around the min gaps
762 int minGap = Math.min(mWidthGap, mHeightGap);
763 mWidthGap = mHeightGap = minGap;
764 }
Michael Jurka5f1c5092010-09-03 14:15:02 -0700765
Michael Jurka8c920dd2011-01-20 14:16:56 -0800766 // Initial values correspond to widthSpecMode == MeasureSpec.EXACTLY
767 int newWidth = widthSpecSize;
768 int newHeight = heightSpecSize;
Michael Jurka5f1c5092010-09-03 14:15:02 -0700769 if (widthSpecMode == MeasureSpec.AT_MOST) {
Michael Jurka8c920dd2011-01-20 14:16:56 -0800770 newWidth = mLeftPadding + mRightPadding + (mCountX * cellWidth) +
Winson Chungece7f5b2010-10-22 14:54:12 -0700771 ((mCountX - 1) * mWidthGap);
Michael Jurka8c920dd2011-01-20 14:16:56 -0800772 newHeight = mTopPadding + mBottomPadding + (mCountY * cellHeight) +
Winson Chungece7f5b2010-10-22 14:54:12 -0700773 ((mCountY - 1) * mHeightGap);
Michael Jurka5f1c5092010-09-03 14:15:02 -0700774 setMeasuredDimension(newWidth, newHeight);
Michael Jurka5f1c5092010-09-03 14:15:02 -0700775 }
Michael Jurka8c920dd2011-01-20 14:16:56 -0800776
777 int count = getChildCount();
778 for (int i = 0; i < count; i++) {
779 View child = getChildAt(i);
780 int childWidthMeasureSpec = MeasureSpec.makeMeasureSpec(newWidth, MeasureSpec.EXACTLY);
781 int childheightMeasureSpec = MeasureSpec.makeMeasureSpec(newHeight,
782 MeasureSpec.EXACTLY);
783 child.measure(childWidthMeasureSpec, childheightMeasureSpec);
784 }
785 setMeasuredDimension(newWidth, newHeight);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800786 }
787
788 @Override
Michael Jurka28750fb2010-09-24 17:43:49 -0700789 protected void onLayout(boolean changed, int l, int t, int r, int b) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800790 int count = getChildCount();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800791 for (int i = 0; i < count; i++) {
Michael Jurka8c920dd2011-01-20 14:16:56 -0800792 View child = getChildAt(i);
793 child.layout(0, 0, r - l, b - t);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800794 }
795 }
796
797 @Override
Michael Jurkadee05892010-07-27 10:01:56 -0700798 protected void onSizeChanged(int w, int h, int oldw, int oldh) {
799 super.onSizeChanged(w, h, oldw, oldh);
Michael Jurka18014792010-10-14 09:01:34 -0700800 mBackgroundRect.set(0, 0, w, h);
Michael Jurka33945b22010-12-21 18:19:38 -0800801 updateGlowRect();
Michael Jurkadee05892010-07-27 10:01:56 -0700802 }
803
804 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800805 protected void setChildrenDrawingCacheEnabled(boolean enabled) {
Michael Jurka8c920dd2011-01-20 14:16:56 -0800806 mChildren.setChildrenDrawingCacheEnabled(enabled);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800807 }
808
809 @Override
810 protected void setChildrenDrawnWithCacheEnabled(boolean enabled) {
Michael Jurka8c920dd2011-01-20 14:16:56 -0800811 mChildren.setChildrenDrawnWithCacheEnabled(enabled);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800812 }
813
Michael Jurka5f1c5092010-09-03 14:15:02 -0700814 public float getBackgroundAlpha() {
815 return mBackgroundAlpha;
Michael Jurkadee05892010-07-27 10:01:56 -0700816 }
817
Adam Cohen1b0aaac2010-10-28 11:11:18 -0700818 public void setBackgroundAlphaMultiplier(float multiplier) {
819 mBackgroundAlphaMultiplier = multiplier;
820 }
821
Adam Cohenddb82192010-11-10 16:32:54 -0800822 public float getBackgroundAlphaMultiplier() {
823 return mBackgroundAlphaMultiplier;
824 }
825
Michael Jurka5f1c5092010-09-03 14:15:02 -0700826 public void setBackgroundAlpha(float alpha) {
827 mBackgroundAlpha = alpha;
Michael Jurka0142d492010-08-25 17:46:15 -0700828 invalidate();
Michael Jurkadee05892010-07-27 10:01:56 -0700829 }
830
Michael Jurka5f1c5092010-09-03 14:15:02 -0700831 // Need to return true to let the view system know we know how to handle alpha-- this is
832 // because when our children have an alpha of 0.0f, they are still rendering their "dimmed"
833 // versions
834 @Override
835 protected boolean onSetAlpha(int alpha) {
836 return true;
837 }
838
839 public void setAlpha(float alpha) {
840 setChildrenAlpha(alpha);
841 super.setAlpha(alpha);
842 }
843
Michael Jurkadee05892010-07-27 10:01:56 -0700844 private void setChildrenAlpha(float alpha) {
Michael Jurka0142d492010-08-25 17:46:15 -0700845 final int childCount = getChildCount();
846 for (int i = 0; i < childCount; i++) {
Michael Jurkadee05892010-07-27 10:01:56 -0700847 getChildAt(i).setAlpha(alpha);
848 }
849 }
850
Patrick Dubroy440c3602010-07-13 17:50:32 -0700851 public View getChildAt(int x, int y) {
Michael Jurka8c920dd2011-01-20 14:16:56 -0800852 return mChildren.getChildAt(x, y);
Patrick Dubroy440c3602010-07-13 17:50:32 -0700853 }
854
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700855 /**
856 * Estimate where the top left cell of the dragged item will land if it is dropped.
857 *
858 * @param originX The X value of the top left corner of the item
859 * @param originY The Y value of the top left corner of the item
860 * @param spanX The number of horizontal cells that the item spans
861 * @param spanY The number of vertical cells that the item spans
862 * @param result The estimated drop cell X and Y.
863 */
864 void estimateDropCell(int originX, int originY, int spanX, int spanY, int[] result) {
Adam Cohend22015c2010-07-26 22:02:18 -0700865 final int countX = mCountX;
866 final int countY = mCountY;
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700867
Michael Jurkaa63c4522010-08-19 13:52:27 -0700868 // pointToCellRounded takes the top left of a cell but will pad that with
869 // cellWidth/2 and cellHeight/2 when finding the matching cell
870 pointToCellRounded(originX, originY, result);
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700871
872 // If the item isn't fully on this screen, snap to the edges
873 int rightOverhang = result[0] + spanX - countX;
874 if (rightOverhang > 0) {
875 result[0] -= rightOverhang; // Snap to right
876 }
877 result[0] = Math.max(0, result[0]); // Snap to left
878 int bottomOverhang = result[1] + spanY - countY;
879 if (bottomOverhang > 0) {
880 result[1] -= bottomOverhang; // Snap to bottom
881 }
882 result[1] = Math.max(0, result[1]); // Snap to top
883 }
884
Joe Onorato4be866d2010-10-10 11:26:02 -0700885 void visualizeDropLocation(
886 View v, Bitmap dragOutline, int originX, int originY, int spanX, int spanY) {
887
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -0700888 final int oldDragCellX = mDragCell[0];
889 final int oldDragCellY = mDragCell[1];
Joe Onorato4be866d2010-10-10 11:26:02 -0700890 final int[] nearest = findNearestVacantArea(originX, originY, spanX, spanY, v, mDragCell);
Winson Chunga9abd0e2010-10-27 17:18:37 -0700891 if (v != null) {
892 mDragCenter.set(originX + (v.getWidth() / 2), originY + (v.getHeight() / 2));
893 } else {
894 mDragCenter.set(originX, originY);
895 }
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700896
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -0700897 if (nearest != null && (nearest[0] != oldDragCellX || nearest[1] != oldDragCellY)) {
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700898 // Find the top left corner of the rect the object will occupy
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700899 final int[] topLeft = mTmpPoint;
900 cellToPoint(nearest[0], nearest[1], topLeft);
901
Joe Onorato4be866d2010-10-10 11:26:02 -0700902 int left = topLeft[0];
903 int top = topLeft[1];
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700904
Winson Chunga9abd0e2010-10-27 17:18:37 -0700905 if (v != null) {
906 if (v.getParent() instanceof CellLayout) {
907 LayoutParams lp = (LayoutParams) v.getLayoutParams();
908 left += lp.leftMargin;
909 top += lp.topMargin;
910 }
Winson Chung150fbab2010-09-29 17:14:26 -0700911
Winson Chunga9abd0e2010-10-27 17:18:37 -0700912 // Offsets due to the size difference between the View and the dragOutline
913 left += (v.getWidth() - dragOutline.getWidth()) / 2;
914 top += (v.getHeight() - dragOutline.getHeight()) / 2;
915 }
Winson Chung150fbab2010-09-29 17:14:26 -0700916
Joe Onorato4be866d2010-10-10 11:26:02 -0700917 final int oldIndex = mDragOutlineCurrent;
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -0700918 mDragOutlineAnims[oldIndex].animateOut();
919 mDragOutlineCurrent = (oldIndex + 1) % mDragOutlines.length;
Winson Chung150fbab2010-09-29 17:14:26 -0700920
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -0700921 mDragOutlines[mDragOutlineCurrent].set(left, top);
922 mDragOutlineAnims[mDragOutlineCurrent].setTag(dragOutline);
923 mDragOutlineAnims[mDragOutlineCurrent].animateIn();
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700924 }
Patrick Dubroy49250ad2010-10-08 15:33:52 -0700925
926 // If we are drawing crosshairs, the entire CellLayout needs to be invalidated
927 if (mCrosshairsDrawable != null) {
928 invalidate();
929 }
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700930 }
931
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800932 /**
Jeff Sharkey70864282009-04-07 21:08:40 -0700933 * Find a vacant area that will fit the given bounds nearest the requested
934 * cell location. Uses Euclidean distance to score multiple vacant areas.
Winson Chungaafa03c2010-06-11 17:34:16 -0700935 *
Romain Guy51afc022009-05-04 18:03:43 -0700936 * @param pixelX The X location at which you want to search for a vacant area.
937 * @param pixelY The Y location at which you want to search for a vacant area.
Jeff Sharkey70864282009-04-07 21:08:40 -0700938 * @param spanX Horizontal span of the object.
939 * @param spanY Vertical span of the object.
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700940 * @param result Array in which to place the result, or null (in which case a new array will
941 * be allocated)
Jeff Sharkey70864282009-04-07 21:08:40 -0700942 * @return The X, Y cell of a vacant area that can contain this object,
943 * nearest the requested location.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800944 */
Michael Jurka6a1435d2010-09-27 17:35:12 -0700945 int[] findNearestVacantArea(
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700946 int pixelX, int pixelY, int spanX, int spanY, int[] result) {
947 return findNearestVacantArea(pixelX, pixelY, spanX, spanY, null, result);
Michael Jurka6a1435d2010-09-27 17:35:12 -0700948 }
Winson Chungaafa03c2010-06-11 17:34:16 -0700949
Michael Jurka6a1435d2010-09-27 17:35:12 -0700950 /**
951 * Find a vacant area that will fit the given bounds nearest the requested
952 * cell location. Uses Euclidean distance to score multiple vacant areas.
953 *
954 * @param pixelX The X location at which you want to search for a vacant area.
955 * @param pixelY The Y location at which you want to search for a vacant area.
956 * @param spanX Horizontal span of the object.
957 * @param spanY Vertical span of the object.
Michael Jurka6a1435d2010-09-27 17:35:12 -0700958 * @param ignoreView Considers space occupied by this view as unoccupied
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700959 * @param result Previously returned value to possibly recycle.
Michael Jurka6a1435d2010-09-27 17:35:12 -0700960 * @return The X, Y cell of a vacant area that can contain this object,
961 * nearest the requested location.
962 */
963 int[] findNearestVacantArea(
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700964 int pixelX, int pixelY, int spanX, int spanY, View ignoreView, int[] result) {
Michael Jurkac6ee42e2010-09-30 12:04:50 -0700965 // mark space take by ignoreView as available (method checks if ignoreView is null)
966 markCellsAsUnoccupiedForView(ignoreView);
967
Jeff Sharkey70864282009-04-07 21:08:40 -0700968 // Keep track of best-scoring drop area
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700969 final int[] bestXY = result != null ? result : new int[2];
Jeff Sharkey70864282009-04-07 21:08:40 -0700970 double bestDistance = Double.MAX_VALUE;
Winson Chungaafa03c2010-06-11 17:34:16 -0700971
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700972 final int countX = mCountX;
973 final int countY = mCountY;
974 final boolean[][] occupied = mOccupied;
975
Winson Chungbbc60d82010-11-11 16:34:41 -0800976 for (int y = 0; y < countY - (spanY - 1); y++) {
Michael Jurkac28de512010-08-13 11:27:44 -0700977 inner:
Winson Chungbbc60d82010-11-11 16:34:41 -0800978 for (int x = 0; x < countX - (spanX - 1); x++) {
Michael Jurkac28de512010-08-13 11:27:44 -0700979 for (int i = 0; i < spanX; i++) {
980 for (int j = 0; j < spanY; j++) {
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700981 if (occupied[x + i][y + j]) {
Winson Chungbbc60d82010-11-11 16:34:41 -0800982 // small optimization: we can skip to after the column we just found
Michael Jurkac28de512010-08-13 11:27:44 -0700983 // an occupied cell
Winson Chungbbc60d82010-11-11 16:34:41 -0800984 x += i;
Michael Jurkac28de512010-08-13 11:27:44 -0700985 continue inner;
986 }
987 }
988 }
989 final int[] cellXY = mTmpCellXY;
990 cellToPoint(x, y, cellXY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800991
Michael Jurkac28de512010-08-13 11:27:44 -0700992 double distance = Math.sqrt(Math.pow(cellXY[0] - pixelX, 2)
993 + Math.pow(cellXY[1] - pixelY, 2));
994 if (distance <= bestDistance) {
995 bestDistance = distance;
996 bestXY[0] = x;
997 bestXY[1] = y;
998 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800999 }
1000 }
Michael Jurkac6ee42e2010-09-30 12:04:50 -07001001 // re-mark space taken by ignoreView as occupied
1002 markCellsAsOccupiedForView(ignoreView);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001003
Winson Chungaafa03c2010-06-11 17:34:16 -07001004 // Return null if no suitable location found
Jeff Sharkey70864282009-04-07 21:08:40 -07001005 if (bestDistance < Double.MAX_VALUE) {
1006 return bestXY;
1007 } else {
1008 return null;
1009 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001010 }
Winson Chungaafa03c2010-06-11 17:34:16 -07001011
Michael Jurka0280c3b2010-09-17 15:00:07 -07001012 boolean existsEmptyCell() {
1013 return findCellForSpan(null, 1, 1);
1014 }
1015
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001016 /**
Michael Jurka0280c3b2010-09-17 15:00:07 -07001017 * Finds the upper-left coordinate of the first rectangle in the grid that can
1018 * hold a cell of the specified dimensions. If intersectX and intersectY are not -1,
1019 * then this method will only return coordinates for rectangles that contain the cell
1020 * (intersectX, intersectY)
1021 *
1022 * @param cellXY The array that will contain the position of a vacant cell if such a cell
1023 * can be found.
1024 * @param spanX The horizontal span of the cell we want to find.
1025 * @param spanY The vertical span of the cell we want to find.
1026 *
1027 * @return True if a vacant cell of the specified dimension was found, false otherwise.
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001028 */
Michael Jurka0280c3b2010-09-17 15:00:07 -07001029 boolean findCellForSpan(int[] cellXY, int spanX, int spanY) {
1030 return findCellForSpanThatIntersectsIgnoring(cellXY, spanX, spanY, -1, -1, null);
1031 }
1032
1033 /**
1034 * Like above, but ignores any cells occupied by the item "ignoreView"
1035 *
1036 * @param cellXY The array that will contain the position of a vacant cell if such a cell
1037 * can be found.
1038 * @param spanX The horizontal span of the cell we want to find.
1039 * @param spanY The vertical span of the cell we want to find.
1040 * @param ignoreView The home screen item we should treat as not occupying any space
1041 * @return
1042 */
1043 boolean findCellForSpanIgnoring(int[] cellXY, int spanX, int spanY, View ignoreView) {
1044 return findCellForSpanThatIntersectsIgnoring(cellXY, spanX, spanY, -1, -1, ignoreView);
1045 }
1046
1047 /**
1048 * Like above, but if intersectX and intersectY are not -1, then this method will try to
1049 * return coordinates for rectangles that contain the cell [intersectX, intersectY]
1050 *
1051 * @param spanX The horizontal span of the cell we want to find.
1052 * @param spanY The vertical span of the cell we want to find.
1053 * @param ignoreView The home screen item we should treat as not occupying any space
1054 * @param intersectX The X coordinate of the cell that we should try to overlap
1055 * @param intersectX The Y coordinate of the cell that we should try to overlap
1056 *
1057 * @return True if a vacant cell of the specified dimension was found, false otherwise.
1058 */
1059 boolean findCellForSpanThatIntersects(int[] cellXY, int spanX, int spanY,
1060 int intersectX, int intersectY) {
1061 return findCellForSpanThatIntersectsIgnoring(
1062 cellXY, spanX, spanY, intersectX, intersectY, null);
1063 }
1064
1065 /**
1066 * The superset of the above two methods
1067 */
1068 boolean findCellForSpanThatIntersectsIgnoring(int[] cellXY, int spanX, int spanY,
1069 int intersectX, int intersectY, View ignoreView) {
Michael Jurkac6ee42e2010-09-30 12:04:50 -07001070 // mark space take by ignoreView as available (method checks if ignoreView is null)
1071 markCellsAsUnoccupiedForView(ignoreView);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001072
Michael Jurka28750fb2010-09-24 17:43:49 -07001073 boolean foundCell = false;
Michael Jurka0280c3b2010-09-17 15:00:07 -07001074 while (true) {
1075 int startX = 0;
1076 if (intersectX >= 0) {
1077 startX = Math.max(startX, intersectX - (spanX - 1));
1078 }
1079 int endX = mCountX - (spanX - 1);
1080 if (intersectX >= 0) {
1081 endX = Math.min(endX, intersectX + (spanX - 1) + (spanX == 1 ? 1 : 0));
1082 }
1083 int startY = 0;
1084 if (intersectY >= 0) {
1085 startY = Math.max(startY, intersectY - (spanY - 1));
1086 }
1087 int endY = mCountY - (spanY - 1);
1088 if (intersectY >= 0) {
1089 endY = Math.min(endY, intersectY + (spanY - 1) + (spanY == 1 ? 1 : 0));
1090 }
1091
Winson Chungbbc60d82010-11-11 16:34:41 -08001092 for (int y = startY; y < endY && !foundCell; y++) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001093 inner:
Winson Chungbbc60d82010-11-11 16:34:41 -08001094 for (int x = startX; x < endX; x++) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001095 for (int i = 0; i < spanX; i++) {
1096 for (int j = 0; j < spanY; j++) {
1097 if (mOccupied[x + i][y + j]) {
Winson Chungbbc60d82010-11-11 16:34:41 -08001098 // small optimization: we can skip to after the column we just found
Michael Jurka0280c3b2010-09-17 15:00:07 -07001099 // an occupied cell
Winson Chungbbc60d82010-11-11 16:34:41 -08001100 x += i;
Michael Jurka0280c3b2010-09-17 15:00:07 -07001101 continue inner;
1102 }
1103 }
1104 }
1105 if (cellXY != null) {
1106 cellXY[0] = x;
1107 cellXY[1] = y;
1108 }
Michael Jurka28750fb2010-09-24 17:43:49 -07001109 foundCell = true;
1110 break;
Michael Jurka0280c3b2010-09-17 15:00:07 -07001111 }
1112 }
1113 if (intersectX == -1 && intersectY == -1) {
1114 break;
1115 } else {
1116 // if we failed to find anything, try again but without any requirements of
1117 // intersecting
1118 intersectX = -1;
1119 intersectY = -1;
1120 continue;
1121 }
1122 }
1123
Michael Jurkac6ee42e2010-09-30 12:04:50 -07001124 // re-mark space taken by ignoreView as occupied
1125 markCellsAsOccupiedForView(ignoreView);
Michael Jurka28750fb2010-09-24 17:43:49 -07001126 return foundCell;
Michael Jurka0280c3b2010-09-17 15:00:07 -07001127 }
1128
1129 /**
1130 * Called when drag has left this CellLayout or has been completed (successfully or not)
1131 */
1132 void onDragExit() {
Joe Onorato4be866d2010-10-10 11:26:02 -07001133 // This can actually be called when we aren't in a drag, e.g. when adding a new
1134 // item to this layout via the customize drawer.
1135 // Guard against that case.
1136 if (mDragging) {
1137 mDragging = false;
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001138
Joe Onorato4be866d2010-10-10 11:26:02 -07001139 // Fade out the drag indicators
1140 if (mCrosshairsAnimator != null) {
1141 mCrosshairsAnimator.animateOut();
1142 }
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001143 }
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -07001144
1145 // Invalidate the drag data
1146 mDragCell[0] = -1;
1147 mDragCell[1] = -1;
1148 mDragOutlineAnims[mDragOutlineCurrent].animateOut();
1149 mDragOutlineCurrent = (mDragOutlineCurrent + 1) % mDragOutlineAnims.length;
1150
Michael Jurka33945b22010-12-21 18:19:38 -08001151 setIsDragOverlapping(false);
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001152 }
1153
1154 /**
Winson Chungaafa03c2010-06-11 17:34:16 -07001155 * Mark a child as having been dropped.
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001156 * At the beginning of the drag operation, the child may have been on another
Patrick Dubroyce34a972010-10-19 10:34:32 -07001157 * screen, but it is re-parented before this method is called.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001158 *
1159 * @param child The child that is being dropped
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001160 */
Michael Jurkad3ef3062010-11-23 16:23:58 -08001161 void onDropChild(View child, boolean animate) {
Romain Guyd94533d2009-08-17 10:01:15 -07001162 if (child != null) {
1163 LayoutParams lp = (LayoutParams) child.getLayoutParams();
Romain Guyd94533d2009-08-17 10:01:15 -07001164 lp.isDragging = false;
Romain Guy84f296c2009-11-04 15:00:44 -08001165 lp.dropped = true;
Michael Jurkad3ef3062010-11-23 16:23:58 -08001166 lp.animateDrop = animate;
Patrick Dubroye3887cc2011-01-20 10:43:40 -08001167 child.setVisibility(animate ? View.INVISIBLE : View.VISIBLE);
Romain Guyd94533d2009-08-17 10:01:15 -07001168 child.requestLayout();
Romain Guyd94533d2009-08-17 10:01:15 -07001169 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001170 }
1171
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001172 /**
1173 * Start dragging the specified child
Winson Chungaafa03c2010-06-11 17:34:16 -07001174 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001175 * @param child The child that is being dragged
1176 */
1177 void onDragChild(View child) {
1178 LayoutParams lp = (LayoutParams) child.getLayoutParams();
1179 lp.isDragging = true;
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001180 }
1181
1182 /**
1183 * A drag event has begun over this layout.
1184 * It may have begun over this layout (in which case onDragChild is called first),
1185 * or it may have begun on another layout.
1186 */
Winson Chunga9abd0e2010-10-27 17:18:37 -07001187 void onDragEnter() {
Patrick Dubroyfe6bd872010-10-13 17:32:10 -07001188 if (!mDragging) {
Patrick Dubroyfe6bd872010-10-13 17:32:10 -07001189 // Fade in the drag indicators
1190 if (mCrosshairsAnimator != null) {
1191 mCrosshairsAnimator.animateIn();
1192 }
Joe Onorato4be866d2010-10-10 11:26:02 -07001193 }
1194 mDragging = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001195 }
Winson Chungaafa03c2010-06-11 17:34:16 -07001196
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001197 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001198 * Computes a bounding rectangle for a range of cells
Winson Chungaafa03c2010-06-11 17:34:16 -07001199 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001200 * @param cellX X coordinate of upper left corner expressed as a cell position
1201 * @param cellY Y coordinate of upper left corner expressed as a cell position
Winson Chungaafa03c2010-06-11 17:34:16 -07001202 * @param cellHSpan Width in cells
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001203 * @param cellVSpan Height in cells
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001204 * @param resultRect Rect into which to put the results
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001205 */
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001206 public void cellToRect(int cellX, int cellY, int cellHSpan, int cellVSpan, RectF resultRect) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001207 final int cellWidth = mCellWidth;
1208 final int cellHeight = mCellHeight;
1209 final int widthGap = mWidthGap;
1210 final int heightGap = mHeightGap;
Winson Chungaafa03c2010-06-11 17:34:16 -07001211
1212 final int hStartPadding = getLeftPadding();
1213 final int vStartPadding = getTopPadding();
1214
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001215 int width = cellHSpan * cellWidth + ((cellHSpan - 1) * widthGap);
1216 int height = cellVSpan * cellHeight + ((cellVSpan - 1) * heightGap);
1217
1218 int x = hStartPadding + cellX * (cellWidth + widthGap);
1219 int y = vStartPadding + cellY * (cellHeight + heightGap);
Winson Chungaafa03c2010-06-11 17:34:16 -07001220
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001221 resultRect.set(x, y, x + width, y + height);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001222 }
Winson Chungaafa03c2010-06-11 17:34:16 -07001223
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001224 /**
Winson Chungaafa03c2010-06-11 17:34:16 -07001225 * Computes the required horizontal and vertical cell spans to always
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001226 * fit the given rectangle.
Winson Chungaafa03c2010-06-11 17:34:16 -07001227 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001228 * @param width Width in pixels
1229 * @param height Height in pixels
Patrick Dubroy8f86ddc2010-07-16 13:55:32 -07001230 * @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 -08001231 */
Patrick Dubroy8f86ddc2010-07-16 13:55:32 -07001232 public int[] rectToCell(int width, int height, int[] result) {
Michael Jurka9987a5c2010-10-08 16:58:12 -07001233 return rectToCell(getResources(), width, height, result);
1234 }
1235
1236 public static int[] rectToCell(Resources resources, int width, int height, int[] result) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001237 // Always assume we're working with the smallest span to make sure we
1238 // reserve enough space in both orientations.
Joe Onorato79e56262009-09-21 15:23:04 -04001239 int actualWidth = resources.getDimensionPixelSize(R.dimen.workspace_cell_width);
1240 int actualHeight = resources.getDimensionPixelSize(R.dimen.workspace_cell_height);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001241 int smallerSize = Math.min(actualWidth, actualHeight);
Joe Onorato79e56262009-09-21 15:23:04 -04001242
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001243 // Always round up to next largest cell
1244 int spanX = (width + smallerSize) / smallerSize;
1245 int spanY = (height + smallerSize) / smallerSize;
Joe Onorato79e56262009-09-21 15:23:04 -04001246
Patrick Dubroy8f86ddc2010-07-16 13:55:32 -07001247 if (result == null) {
1248 return new int[] { spanX, spanY };
1249 }
1250 result[0] = spanX;
1251 result[1] = spanY;
1252 return result;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001253 }
1254
Michael Jurkaf12c75c2011-01-25 22:41:40 -08001255 public int[] cellSpansToSize(int hSpans, int vSpans) {
1256 int[] size = new int[2];
1257 size[0] = hSpans * mCellWidth + (hSpans - 1) * mWidthGap;
1258 size[1] = vSpans * mCellHeight + (vSpans - 1) * mHeightGap;
1259 return size;
1260 }
1261
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001262 /**
Patrick Dubroy047379a2010-12-19 22:02:04 -08001263 * Calculate the grid spans needed to fit given item
1264 */
1265 public void calculateSpans(ItemInfo info) {
1266 final int minWidth;
1267 final int minHeight;
1268
1269 if (info instanceof LauncherAppWidgetInfo) {
1270 minWidth = ((LauncherAppWidgetInfo) info).minWidth;
1271 minHeight = ((LauncherAppWidgetInfo) info).minHeight;
1272 } else if (info instanceof PendingAddWidgetInfo) {
1273 minWidth = ((PendingAddWidgetInfo) info).minWidth;
1274 minHeight = ((PendingAddWidgetInfo) info).minHeight;
1275 } else {
1276 // It's not a widget, so it must be 1x1
1277 info.spanX = info.spanY = 1;
1278 return;
1279 }
1280 int[] spans = rectToCell(minWidth, minHeight, null);
1281 info.spanX = spans[0];
1282 info.spanY = spans[1];
1283 }
1284
1285 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001286 * Find the first vacant cell, if there is one.
1287 *
1288 * @param vacant Holds the x and y coordinate of the vacant cell
1289 * @param spanX Horizontal cell span.
1290 * @param spanY Vertical cell span.
Winson Chungaafa03c2010-06-11 17:34:16 -07001291 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001292 * @return True if a vacant cell was found
1293 */
1294 public boolean getVacantCell(int[] vacant, int spanX, int spanY) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001295
Michael Jurka0280c3b2010-09-17 15:00:07 -07001296 return findVacantCell(vacant, spanX, spanY, mCountX, mCountY, mOccupied);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001297 }
1298
1299 static boolean findVacantCell(int[] vacant, int spanX, int spanY,
1300 int xCount, int yCount, boolean[][] occupied) {
1301
1302 for (int x = 0; x < xCount; x++) {
1303 for (int y = 0; y < yCount; y++) {
1304 boolean available = !occupied[x][y];
1305out: for (int i = x; i < x + spanX - 1 && x < xCount; i++) {
1306 for (int j = y; j < y + spanY - 1 && y < yCount; j++) {
1307 available = available && !occupied[i][j];
1308 if (!available) break out;
1309 }
1310 }
1311
1312 if (available) {
1313 vacant[0] = x;
1314 vacant[1] = y;
1315 return true;
1316 }
1317 }
1318 }
1319
1320 return false;
1321 }
1322
Michael Jurka0280c3b2010-09-17 15:00:07 -07001323 private void clearOccupiedCells() {
1324 for (int x = 0; x < mCountX; x++) {
1325 for (int y = 0; y < mCountY; y++) {
1326 mOccupied[x][y] = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001327 }
1328 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001329 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001330
Michael Jurka0280c3b2010-09-17 15:00:07 -07001331 public void onMove(View view, int newCellX, int newCellY) {
1332 LayoutParams lp = (LayoutParams) view.getLayoutParams();
1333 markCellsAsUnoccupiedForView(view);
1334 markCellsForView(newCellX, newCellY, lp.cellHSpan, lp.cellVSpan, true);
1335 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001336
Michael Jurka0280c3b2010-09-17 15:00:07 -07001337 private void markCellsAsOccupiedForView(View view) {
Michael Jurka8c920dd2011-01-20 14:16:56 -08001338 if (view == null || view.getParent() != mChildren) return;
Michael Jurka0280c3b2010-09-17 15:00:07 -07001339 LayoutParams lp = (LayoutParams) view.getLayoutParams();
1340 markCellsForView(lp.cellX, lp.cellY, lp.cellHSpan, lp.cellVSpan, true);
1341 }
1342
1343 private void markCellsAsUnoccupiedForView(View view) {
Michael Jurka8c920dd2011-01-20 14:16:56 -08001344 if (view == null || view.getParent() != mChildren) return;
Michael Jurka0280c3b2010-09-17 15:00:07 -07001345 LayoutParams lp = (LayoutParams) view.getLayoutParams();
1346 markCellsForView(lp.cellX, lp.cellY, lp.cellHSpan, lp.cellVSpan, false);
1347 }
1348
1349 private void markCellsForView(int cellX, int cellY, int spanX, int spanY, boolean value) {
1350 for (int x = cellX; x < cellX + spanX && x < mCountX; x++) {
1351 for (int y = cellY; y < cellY + spanY && y < mCountY; y++) {
1352 mOccupied[x][y] = value;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001353 }
1354 }
1355 }
1356
1357 @Override
1358 public ViewGroup.LayoutParams generateLayoutParams(AttributeSet attrs) {
1359 return new CellLayout.LayoutParams(getContext(), attrs);
1360 }
1361
1362 @Override
1363 protected boolean checkLayoutParams(ViewGroup.LayoutParams p) {
1364 return p instanceof CellLayout.LayoutParams;
1365 }
1366
1367 @Override
1368 protected ViewGroup.LayoutParams generateLayoutParams(ViewGroup.LayoutParams p) {
1369 return new CellLayout.LayoutParams(p);
1370 }
1371
Winson Chungaafa03c2010-06-11 17:34:16 -07001372 public static class CellLayoutAnimationController extends LayoutAnimationController {
1373 public CellLayoutAnimationController(Animation animation, float delay) {
1374 super(animation, delay);
1375 }
1376
1377 @Override
1378 protected long getDelayForView(View view) {
1379 return (int) (Math.random() * 150);
1380 }
1381 }
1382
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001383 public static class LayoutParams extends ViewGroup.MarginLayoutParams {
1384 /**
1385 * Horizontal location of the item in the grid.
1386 */
1387 @ViewDebug.ExportedProperty
1388 public int cellX;
1389
1390 /**
1391 * Vertical location of the item in the grid.
1392 */
1393 @ViewDebug.ExportedProperty
1394 public int cellY;
1395
1396 /**
1397 * Number of cells spanned horizontally by the item.
1398 */
1399 @ViewDebug.ExportedProperty
1400 public int cellHSpan;
1401
1402 /**
1403 * Number of cells spanned vertically by the item.
1404 */
1405 @ViewDebug.ExportedProperty
1406 public int cellVSpan;
Winson Chungaafa03c2010-06-11 17:34:16 -07001407
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001408 /**
1409 * Is this item currently being dragged
1410 */
1411 public boolean isDragging;
1412
1413 // X coordinate of the view in the layout.
1414 @ViewDebug.ExportedProperty
1415 int x;
1416 // Y coordinate of the view in the layout.
1417 @ViewDebug.ExportedProperty
1418 int y;
1419
Patrick Dubroyce34a972010-10-19 10:34:32 -07001420 /**
1421 * The old X coordinate of this item, relative to its current parent.
1422 * Used to animate the item into its new position.
1423 */
1424 int oldX;
1425
1426 /**
1427 * The old Y coordinate of this item, relative to its current parent.
1428 * Used to animate the item into its new position.
1429 */
1430 int oldY;
1431
Romain Guy84f296c2009-11-04 15:00:44 -08001432 boolean dropped;
Romain Guyfcb9e712009-10-02 16:06:52 -07001433
Michael Jurkad3ef3062010-11-23 16:23:58 -08001434 boolean animateDrop;
1435
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001436 public LayoutParams(Context c, AttributeSet attrs) {
1437 super(c, attrs);
1438 cellHSpan = 1;
1439 cellVSpan = 1;
1440 }
1441
1442 public LayoutParams(ViewGroup.LayoutParams source) {
1443 super(source);
1444 cellHSpan = 1;
1445 cellVSpan = 1;
1446 }
Winson Chungaafa03c2010-06-11 17:34:16 -07001447
1448 public LayoutParams(LayoutParams source) {
1449 super(source);
1450 this.cellX = source.cellX;
1451 this.cellY = source.cellY;
1452 this.cellHSpan = source.cellHSpan;
1453 this.cellVSpan = source.cellVSpan;
1454 }
1455
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001456 public LayoutParams(int cellX, int cellY, int cellHSpan, int cellVSpan) {
Romain Guy8f19cdd2010-01-08 15:07:00 -08001457 super(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001458 this.cellX = cellX;
1459 this.cellY = cellY;
1460 this.cellHSpan = cellHSpan;
1461 this.cellVSpan = cellVSpan;
1462 }
1463
1464 public void setup(int cellWidth, int cellHeight, int widthGap, int heightGap,
1465 int hStartPadding, int vStartPadding) {
Winson Chungaafa03c2010-06-11 17:34:16 -07001466
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001467 final int myCellHSpan = cellHSpan;
1468 final int myCellVSpan = cellVSpan;
1469 final int myCellX = cellX;
1470 final int myCellY = cellY;
Winson Chungaafa03c2010-06-11 17:34:16 -07001471
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001472 width = myCellHSpan * cellWidth + ((myCellHSpan - 1) * widthGap) -
1473 leftMargin - rightMargin;
1474 height = myCellVSpan * cellHeight + ((myCellVSpan - 1) * heightGap) -
1475 topMargin - bottomMargin;
1476
1477 x = hStartPadding + myCellX * (cellWidth + widthGap) + leftMargin;
1478 y = vStartPadding + myCellY * (cellHeight + heightGap) + topMargin;
1479 }
Winson Chungaafa03c2010-06-11 17:34:16 -07001480
1481 public String toString() {
1482 return "(" + this.cellX + ", " + this.cellY + ")";
1483 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001484 }
1485
Michael Jurka0280c3b2010-09-17 15:00:07 -07001486 // This class stores info for two purposes:
1487 // 1. When dragging items (mDragInfo in Workspace), we store the View, its cellX & cellY,
1488 // its spanX, spanY, and the screen it is on
1489 // 2. When long clicking on an empty cell in a CellLayout, we save information about the
1490 // cellX and cellY coordinates and which page was clicked. We then set this as a tag on
1491 // the CellLayout that was long clicked
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001492 static final class CellInfo implements ContextMenu.ContextMenuInfo {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001493 View cell;
Michael Jurkaa63c4522010-08-19 13:52:27 -07001494 int cellX = -1;
1495 int cellY = -1;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001496 int spanX;
1497 int spanY;
1498 int screen;
1499 boolean valid;
1500
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001501 @Override
1502 public String toString() {
Winson Chungaafa03c2010-06-11 17:34:16 -07001503 return "Cell[view=" + (cell == null ? "null" : cell.getClass())
1504 + ", x=" + cellX + ", y=" + cellY + "]";
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001505 }
1506 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001507}