blob: 0d774c9665d64bba215a38c313bf6b98b4561702 [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;
87 private Drawable mNormalGlowBackground;
88 private Drawable mActiveBackground;
89 private Drawable mActiveGlowBackground;
90 private Drawable mNormalBackgroundMini;
91 private Drawable mNormalGlowBackgroundMini;
92 private Drawable mActiveBackgroundMini;
93 private Drawable mActiveGlowBackgroundMini;
Michael Jurka18014792010-10-14 09:01:34 -070094 private Rect mBackgroundRect;
Michael Jurka33945b22010-12-21 18:19:38 -080095 private Rect mGlowBackgroundRect;
96 private float mGlowBackgroundScale;
97 private float mGlowBackgroundAlpha;
Patrick Dubroy1262e362010-10-06 15:49:50 -070098
Michael Jurka33945b22010-12-21 18:19:38 -080099 private boolean mAcceptsDrops = false;
100 // If we're actively dragging something over this screen, mIsDragOverlapping is true
101 private boolean mIsDragOverlapping = false;
102 private boolean mIsDragOccuring = false;
103 private boolean mIsDefaultDropTarget = false;
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
Joe Onorato4be866d2010-10-10 11:26:02 -0700124 private boolean mDragging = false;
125
Patrick Dubroyce34a972010-10-19 10:34:32 -0700126 private TimeInterpolator mEaseOutInterpolator;
Michael Jurka8c920dd2011-01-20 14:16:56 -0800127 private CellLayoutChildren mChildren;
Patrick Dubroyce34a972010-10-19 10:34:32 -0700128
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800129 public CellLayout(Context context) {
130 this(context, null);
131 }
132
133 public CellLayout(Context context, AttributeSet attrs) {
134 this(context, attrs, 0);
135 }
136
137 public CellLayout(Context context, AttributeSet attrs, int defStyle) {
138 super(context, attrs, defStyle);
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700139
140 // A ViewGroup usually does not draw, but CellLayout needs to draw a rectangle to show
141 // the user where a dragged item will land when dropped.
142 setWillNotDraw(false);
Michael Jurkaa63c4522010-08-19 13:52:27 -0700143
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800144 TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.CellLayout, defStyle, 0);
145
146 mCellWidth = a.getDimensionPixelSize(R.styleable.CellLayout_cellWidth, 10);
147 mCellHeight = a.getDimensionPixelSize(R.styleable.CellLayout_cellHeight, 10);
Winson Chungece7f5b2010-10-22 14:54:12 -0700148 mWidthGap = a.getDimensionPixelSize(R.styleable.CellLayout_widthGap, -1);
149 mHeightGap = a.getDimensionPixelSize(R.styleable.CellLayout_heightGap, -1);
Winson Chungaafa03c2010-06-11 17:34:16 -0700150
Adam Cohend22015c2010-07-26 22:02:18 -0700151 mLeftPadding =
152 a.getDimensionPixelSize(R.styleable.CellLayout_xAxisStartPadding, 10);
153 mRightPadding =
154 a.getDimensionPixelSize(R.styleable.CellLayout_xAxisEndPadding, 10);
155 mTopPadding =
156 a.getDimensionPixelSize(R.styleable.CellLayout_yAxisStartPadding, 10);
157 mBottomPadding =
158 a.getDimensionPixelSize(R.styleable.CellLayout_yAxisEndPadding, 10);
Winson Chungaafa03c2010-06-11 17:34:16 -0700159
Adam Cohend22015c2010-07-26 22:02:18 -0700160 mCountX = LauncherModel.getCellCountX();
161 mCountY = LauncherModel.getCellCountY();
Michael Jurka0280c3b2010-09-17 15:00:07 -0700162 mOccupied = new boolean[mCountX][mCountY];
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800163
164 a.recycle();
165
166 setAlwaysDrawnWithCacheEnabled(false);
167
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700168 final Resources res = getResources();
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700169
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700170 if (LauncherApplication.isScreenXLarge()) {
Michael Jurka33945b22010-12-21 18:19:38 -0800171 mNormalBackground = res.getDrawable(R.drawable.homescreen_large_blue);
172 mNormalGlowBackground = res.getDrawable(R.drawable.homescreen_large_blue_strong);
173 mActiveBackground = res.getDrawable(R.drawable.homescreen_large_green);
174 mActiveGlowBackground = res.getDrawable(R.drawable.homescreen_large_green_strong);
175
176 mNormalBackgroundMini = res.getDrawable(R.drawable.homescreen_small_blue);
177 mNormalGlowBackgroundMini = res.getDrawable(R.drawable.homescreen_small_blue_strong);
178 mActiveBackgroundMini = res.getDrawable(R.drawable.homescreen_small_green);
179 mActiveGlowBackgroundMini = res.getDrawable(R.drawable.homescreen_small_green_strong);
180
181 mNormalBackground.setFilterBitmap(true);
182 mNormalGlowBackground.setFilterBitmap(true);
183 mActiveBackground.setFilterBitmap(true);
184 mActiveGlowBackground.setFilterBitmap(true);
185 mNormalBackgroundMini.setFilterBitmap(true);
186 mNormalGlowBackgroundMini.setFilterBitmap(true);
187 mActiveBackgroundMini.setFilterBitmap(true);
188 mActiveGlowBackgroundMini.setFilterBitmap(true);
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700189 }
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700190
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700191 // Initialize the data structures used for the drag visualization.
Winson Chung150fbab2010-09-29 17:14:26 -0700192
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700193 mCrosshairsDrawable = res.getDrawable(R.drawable.gardening_crosshairs);
Patrick Dubroyce34a972010-10-19 10:34:32 -0700194 mEaseOutInterpolator = new DecelerateInterpolator(2.5f); // Quint ease out
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700195
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700196 // Set up the animation for fading the crosshairs in and out
197 int animDuration = res.getInteger(R.integer.config_crosshairsFadeInTime);
Patrick Dubroy49250ad2010-10-08 15:33:52 -0700198 mCrosshairsAnimator = new InterruptibleInOutAnimator(animDuration, 0.0f, 1.0f);
Chet Haase472b2812010-10-14 07:02:04 -0700199 mCrosshairsAnimator.getAnimator().addUpdateListener(new AnimatorUpdateListener() {
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700200 public void onAnimationUpdate(ValueAnimator animation) {
201 mCrosshairsVisibility = ((Float) animation.getAnimatedValue()).floatValue();
Patrick Dubroy8e58e912010-10-14 13:21:48 -0700202 invalidate();
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700203 }
204 });
Patrick Dubroyce34a972010-10-19 10:34:32 -0700205 mCrosshairsAnimator.getAnimator().setInterpolator(mEaseOutInterpolator);
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700206
Joe Onorato4be866d2010-10-10 11:26:02 -0700207 for (int i = 0; i < mDragOutlines.length; i++) {
208 mDragOutlines[i] = new Point(-1, -1);
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700209 }
210
211 // When dragging things around the home screens, we show a green outline of
212 // where the item will land. The outlines gradually fade out, leaving a trail
213 // behind the drag path.
214 // Set up all the animations that are used to implement this fading.
215 final int duration = res.getInteger(R.integer.config_dragOutlineFadeTime);
Chet Haase472b2812010-10-14 07:02:04 -0700216 final float fromAlphaValue = 0;
217 final float toAlphaValue = (float)res.getInteger(R.integer.config_dragOutlineMaxAlpha);
Joe Onorato4be866d2010-10-10 11:26:02 -0700218
Patrick Dubroy8e58e912010-10-14 13:21:48 -0700219 Arrays.fill(mDragOutlineAlphas, fromAlphaValue);
Joe Onorato4be866d2010-10-10 11:26:02 -0700220
221 for (int i = 0; i < mDragOutlineAnims.length; i++) {
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700222 final InterruptibleInOutAnimator anim =
223 new InterruptibleInOutAnimator(duration, fromAlphaValue, toAlphaValue);
Patrick Dubroyce34a972010-10-19 10:34:32 -0700224 anim.getAnimator().setInterpolator(mEaseOutInterpolator);
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700225 final int thisIndex = i;
Chet Haase472b2812010-10-14 07:02:04 -0700226 anim.getAnimator().addUpdateListener(new AnimatorUpdateListener() {
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700227 public void onAnimationUpdate(ValueAnimator animation) {
Joe Onorato4be866d2010-10-10 11:26:02 -0700228 final Bitmap outline = (Bitmap)anim.getTag();
229
230 // If an animation is started and then stopped very quickly, we can still
231 // get spurious updates we've cleared the tag. Guard against this.
232 if (outline == null) {
Patrick Dubroyfe6bd872010-10-13 17:32:10 -0700233 if (false) {
234 Object val = animation.getAnimatedValue();
235 Log.d(TAG, "anim " + thisIndex + " update: " + val +
236 ", isStopped " + anim.isStopped());
237 }
Joe Onorato4be866d2010-10-10 11:26:02 -0700238 // Try to prevent it from continuing to run
239 animation.cancel();
240 } else {
Chet Haase472b2812010-10-14 07:02:04 -0700241 mDragOutlineAlphas[thisIndex] = (Float) animation.getAnimatedValue();
Joe Onorato4be866d2010-10-10 11:26:02 -0700242 final int left = mDragOutlines[thisIndex].x;
243 final int top = mDragOutlines[thisIndex].y;
244 CellLayout.this.invalidate(left, top,
245 left + outline.getWidth(), top + outline.getHeight());
246 }
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700247 }
248 });
Joe Onorato4be866d2010-10-10 11:26:02 -0700249 // The animation holds a reference to the drag outline bitmap as long is it's
250 // running. This way the bitmap can be GCed when the animations are complete.
Chet Haase472b2812010-10-14 07:02:04 -0700251 anim.getAnimator().addListener(new AnimatorListenerAdapter() {
Michael Jurka3c4c20f2010-10-28 15:36:06 -0700252 @Override
Joe Onorato4be866d2010-10-10 11:26:02 -0700253 public void onAnimationEnd(Animator animation) {
Chet Haase472b2812010-10-14 07:02:04 -0700254 if ((Float) ((ValueAnimator) animation).getAnimatedValue() == 0f) {
Joe Onorato4be866d2010-10-10 11:26:02 -0700255 anim.setTag(null);
256 }
257 }
258 });
259 mDragOutlineAnims[i] = anim;
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700260 }
Patrick Dubroyce34a972010-10-19 10:34:32 -0700261
Michael Jurka18014792010-10-14 09:01:34 -0700262 mBackgroundRect = new Rect();
Michael Jurka33945b22010-12-21 18:19:38 -0800263 mGlowBackgroundRect = new Rect();
Michael Jurka18014792010-10-14 09:01:34 -0700264 setHoverScale(1.0f);
265 setHoverAlpha(1.0f);
Michael Jurkabea15192010-11-17 12:33:46 -0800266
Michael Jurka8c920dd2011-01-20 14:16:56 -0800267 mChildren = new CellLayoutChildren(context);
268 mChildren.setCellDimensions(
269 mCellWidth, mCellHeight, mLeftPadding, mTopPadding, mWidthGap, mHeightGap);
270 addView(mChildren);
Michael Jurka18014792010-10-14 09:01:34 -0700271 }
272
Michael Jurka33945b22010-12-21 18:19:38 -0800273 public void setIsDefaultDropTarget(boolean isDefaultDropTarget) {
274 if (mIsDefaultDropTarget != isDefaultDropTarget) {
275 mIsDefaultDropTarget = isDefaultDropTarget;
276 invalidate();
277 }
278 }
279
Michael Jurka33945b22010-12-21 18:19:38 -0800280 void setIsDragOccuring(boolean isDragOccuring) {
281 if (mIsDragOccuring != isDragOccuring) {
282 mIsDragOccuring = isDragOccuring;
283 invalidate();
284 }
285 }
286
287 void setIsDragOverlapping(boolean isDragOverlapping) {
288 if (mIsDragOverlapping != isDragOverlapping) {
289 mIsDragOverlapping = isDragOverlapping;
290 invalidate();
291 }
292 }
293
294 boolean getIsDragOverlapping() {
295 return mIsDragOverlapping;
296 }
297
298 private void updateGlowRect() {
299 float marginFraction = (mGlowBackgroundScale - 1.0f) / 2.0f;
Michael Jurka18014792010-10-14 09:01:34 -0700300 int marginX = (int) (marginFraction * (mBackgroundRect.right - mBackgroundRect.left));
301 int marginY = (int) (marginFraction * (mBackgroundRect.bottom - mBackgroundRect.top));
Michael Jurka33945b22010-12-21 18:19:38 -0800302 mGlowBackgroundRect.set(mBackgroundRect.left - marginX, mBackgroundRect.top - marginY,
Michael Jurka18014792010-10-14 09:01:34 -0700303 mBackgroundRect.right + marginX, mBackgroundRect.bottom + marginY);
304 invalidate();
305 }
306
307 public void setHoverScale(float scaleFactor) {
Michael Jurka33945b22010-12-21 18:19:38 -0800308 if (scaleFactor != mGlowBackgroundScale) {
309 mGlowBackgroundScale = scaleFactor;
310 updateGlowRect();
Michael Jurka8deb1e62011-01-25 16:27:43 -0800311 if (getParent() != null) {
312 ((View) getParent()).invalidate();
313 }
Michael Jurka18014792010-10-14 09:01:34 -0700314 }
315 }
316
317 public float getHoverScale() {
Michael Jurka33945b22010-12-21 18:19:38 -0800318 return mGlowBackgroundScale;
Michael Jurka18014792010-10-14 09:01:34 -0700319 }
320
321 public float getHoverAlpha() {
Michael Jurka33945b22010-12-21 18:19:38 -0800322 return mGlowBackgroundAlpha;
Michael Jurka18014792010-10-14 09:01:34 -0700323 }
324
325 public void setHoverAlpha(float alpha) {
Michael Jurka33945b22010-12-21 18:19:38 -0800326 mGlowBackgroundAlpha = alpha;
Michael Jurka18014792010-10-14 09:01:34 -0700327 invalidate();
328 }
329
330 void animateDrop() {
331 if (LauncherApplication.isScreenXLarge()) {
332 Resources res = getResources();
333 float onDropScale = res.getInteger(R.integer.config_screenOnDropScalePercent) / 100.0f;
334 ObjectAnimator scaleUp = ObjectAnimator.ofFloat(this, "hoverScale", onDropScale);
335 scaleUp.setDuration(res.getInteger(R.integer.config_screenOnDropScaleUpDuration));
336 ObjectAnimator scaleDown = ObjectAnimator.ofFloat(this, "hoverScale", 1.0f);
337 scaleDown.setDuration(res.getInteger(R.integer.config_screenOnDropScaleDownDuration));
338 ObjectAnimator alphaFadeOut = ObjectAnimator.ofFloat(this, "hoverAlpha", 0.0f);
339
340 alphaFadeOut.setStartDelay(res.getInteger(R.integer.config_screenOnDropAlphaFadeDelay));
341 alphaFadeOut.setDuration(res.getInteger(R.integer.config_screenOnDropAlphaFadeDelay));
342
343 AnimatorSet bouncer = new AnimatorSet();
344 bouncer.play(scaleUp).before(scaleDown);
345 bouncer.play(scaleUp).with(alphaFadeOut);
Michael Jurka8edd75c2010-12-17 20:15:06 -0800346 bouncer.addListener(new AnimatorListenerAdapter() {
Michael Jurka3c4c20f2010-10-28 15:36:06 -0700347 @Override
Michael Jurka18014792010-10-14 09:01:34 -0700348 public void onAnimationStart(Animator animation) {
Michael Jurka33945b22010-12-21 18:19:38 -0800349 setIsDragOverlapping(true);
Michael Jurka18014792010-10-14 09:01:34 -0700350 }
Michael Jurka3c4c20f2010-10-28 15:36:06 -0700351 @Override
Michael Jurka8edd75c2010-12-17 20:15:06 -0800352 public void onAnimationEnd(Animator animation) {
Michael Jurka33945b22010-12-21 18:19:38 -0800353 setIsDragOverlapping(false);
Michael Jurka18014792010-10-14 09:01:34 -0700354 setHoverScale(1.0f);
355 setHoverAlpha(1.0f);
356 }
357 });
358 bouncer.start();
359 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800360 }
361
Michael Jurkabea15192010-11-17 12:33:46 -0800362 public void disableCacheUpdates() {
Michael Jurka8c920dd2011-01-20 14:16:56 -0800363 mChildren.disableCacheUpdates();
Michael Jurkabea15192010-11-17 12:33:46 -0800364 }
365
366 public void enableCacheUpdates() {
Michael Jurka8c920dd2011-01-20 14:16:56 -0800367 mChildren.enableCacheUpdates();
Michael Jurkabea15192010-11-17 12:33:46 -0800368 }
369
Jeff Sharkey83f111d2009-04-20 21:03:13 -0700370 @Override
Patrick Dubroy1262e362010-10-06 15:49:50 -0700371 protected void onDraw(Canvas canvas) {
Michael Jurka3e7c7632010-10-02 16:01:03 -0700372 // When we're large, we are either drawn in a "hover" state (ie when dragging an item to
373 // a neighboring page) or with just a normal background (if backgroundAlpha > 0.0f)
374 // When we're small, we are either drawn normally or in the "accepts drops" state (during
375 // a drag). However, we also drag the mini hover background *over* one of those two
376 // backgrounds
Winson Chung26cbf3a2011-01-06 16:25:55 -0800377 if (LauncherApplication.isScreenXLarge() && mBackgroundAlpha > 0.0f) {
Adam Cohenf34bab52010-09-30 14:11:56 -0700378 Drawable bg;
Michael Jurka33945b22010-12-21 18:19:38 -0800379 boolean mini = getScaleX() < 0.5f;
380
381 if (mIsDragOverlapping) {
382 // In the mini case, we draw the active_glow bg *over* the active background
383 bg = mini ? mActiveBackgroundMini : mActiveGlowBackground;
384 } else if (mIsDragOccuring && mAcceptsDrops) {
385 bg = mini ? mActiveBackgroundMini : mActiveBackground;
Winson Chungae222ab2011-01-10 11:45:27 -0800386 } else if (mIsDragOccuring && mIsDefaultDropTarget) {
Michael Jurka33945b22010-12-21 18:19:38 -0800387 bg = mini ? mNormalGlowBackgroundMini : mNormalGlowBackground;
Adam Cohenf34bab52010-09-30 14:11:56 -0700388 } else {
Michael Jurka33945b22010-12-21 18:19:38 -0800389 bg = mini ? mNormalBackgroundMini : mNormalBackground;
Adam Cohenf34bab52010-09-30 14:11:56 -0700390 }
Michael Jurka33945b22010-12-21 18:19:38 -0800391
392 bg.setAlpha((int) (mBackgroundAlpha * mBackgroundAlphaMultiplier * 255));
393 bg.setBounds(mBackgroundRect);
394 bg.draw(canvas);
395
396 if (mini && mIsDragOverlapping) {
Michael Jurka18014792010-10-14 09:01:34 -0700397 boolean modifiedClipRect = false;
Michael Jurka33945b22010-12-21 18:19:38 -0800398 if (mGlowBackgroundScale > 1.0f) {
Michael Jurka18014792010-10-14 09:01:34 -0700399 // If the hover background's scale is greater than 1, we'll be drawing outside
400 // the bounds of this CellLayout. Get around that by temporarily increasing the
401 // size of the clip rect
Michael Jurka33945b22010-12-21 18:19:38 -0800402 float marginFraction = (mGlowBackgroundScale - 1.0f) / 2.0f;
Michael Jurka18014792010-10-14 09:01:34 -0700403 Rect clipRect = canvas.getClipBounds();
404 int marginX = (int) (marginFraction * (clipRect.right - clipRect.left));
405 int marginY = (int) (marginFraction * (clipRect.bottom - clipRect.top));
406 canvas.save(Canvas.CLIP_SAVE_FLAG);
407 canvas.clipRect(-marginX, -marginY,
408 getWidth() + marginX, getHeight() + marginY, Region.Op.REPLACE);
409 modifiedClipRect = true;
410 }
411
Michael Jurka33945b22010-12-21 18:19:38 -0800412 mActiveGlowBackgroundMini.setAlpha(
413 (int) (mBackgroundAlpha * mGlowBackgroundAlpha * 255));
414 mActiveGlowBackgroundMini.setBounds(mGlowBackgroundRect);
415 mActiveGlowBackgroundMini.draw(canvas);
Michael Jurka18014792010-10-14 09:01:34 -0700416 if (modifiedClipRect) {
417 canvas.restore();
418 }
Michael Jurka3e7c7632010-10-02 16:01:03 -0700419 }
Michael Jurkaa63c4522010-08-19 13:52:27 -0700420 }
Romain Guya6abce82009-11-10 02:54:41 -0800421
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700422 if (mCrosshairsVisibility > 0.0f) {
423 final int countX = mCountX;
424 final int countY = mCountY;
425
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700426 final float MAX_ALPHA = 0.4f;
427 final int MAX_VISIBLE_DISTANCE = 600;
428 final float DISTANCE_MULTIPLIER = 0.002f;
429
430 final Drawable d = mCrosshairsDrawable;
431 final int width = d.getIntrinsicWidth();
432 final int height = d.getIntrinsicHeight();
433
434 int x = getLeftPadding() - (mWidthGap / 2) - (width / 2);
435 for (int col = 0; col <= countX; col++) {
436 int y = getTopPadding() - (mHeightGap / 2) - (height / 2);
437 for (int row = 0; row <= countY; row++) {
438 mTmpPointF.set(x - mDragCenter.x, y - mDragCenter.y);
439 float dist = mTmpPointF.length();
440 // Crosshairs further from the drag point are more faint
441 float alpha = Math.min(MAX_ALPHA,
442 DISTANCE_MULTIPLIER * (MAX_VISIBLE_DISTANCE - dist));
443 if (alpha > 0.0f) {
444 d.setBounds(x, y, x + width, y + height);
445 d.setAlpha((int) (alpha * 255 * mCrosshairsVisibility));
446 d.draw(canvas);
447 }
448 y += mCellHeight + mHeightGap;
449 }
450 x += mCellWidth + mWidthGap;
451 }
Joe Onorato4be866d2010-10-10 11:26:02 -0700452 }
Winson Chung150fbab2010-09-29 17:14:26 -0700453
Patrick Dubroy8e58e912010-10-14 13:21:48 -0700454 final Paint paint = mDragOutlinePaint;
Joe Onorato4be866d2010-10-10 11:26:02 -0700455 for (int i = 0; i < mDragOutlines.length; i++) {
Chet Haase472b2812010-10-14 07:02:04 -0700456 final float alpha = mDragOutlineAlphas[i];
Joe Onorato4be866d2010-10-10 11:26:02 -0700457 if (alpha > 0) {
458 final Point p = mDragOutlines[i];
459 final Bitmap b = (Bitmap) mDragOutlineAnims[i].getTag();
Chet Haase472b2812010-10-14 07:02:04 -0700460 paint.setAlpha((int)(alpha + .5f));
Joe Onorato4be866d2010-10-10 11:26:02 -0700461 canvas.drawBitmap(b, p.x, p.y, paint);
Winson Chung150fbab2010-09-29 17:14:26 -0700462 }
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700463 }
464 }
465
466 @Override
Jeff Sharkey83f111d2009-04-20 21:03:13 -0700467 public void cancelLongPress() {
468 super.cancelLongPress();
469
470 // Cancel long press for all children
471 final int count = getChildCount();
472 for (int i = 0; i < count; i++) {
473 final View child = getChildAt(i);
474 child.cancelLongPress();
475 }
476 }
477
Michael Jurkadee05892010-07-27 10:01:56 -0700478 public void setOnInterceptTouchListener(View.OnTouchListener listener) {
479 mInterceptTouchListener = listener;
480 }
481
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800482 int getCountX() {
Adam Cohend22015c2010-07-26 22:02:18 -0700483 return mCountX;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800484 }
485
486 int getCountY() {
Adam Cohend22015c2010-07-26 22:02:18 -0700487 return mCountY;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800488 }
489
Winson Chungaafa03c2010-06-11 17:34:16 -0700490 public boolean addViewToCellLayout(View child, int index, int childId, LayoutParams params) {
Michael Jurkaf3ca3ab2010-10-20 17:08:24 -0700491 return addViewToCellLayout(child, index, childId, params, true);
492 }
493
494 public boolean addViewToCellLayout(
495 View child, int index, int childId, LayoutParams params, boolean markCells) {
Winson Chungaafa03c2010-06-11 17:34:16 -0700496 final LayoutParams lp = params;
497
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800498 // Generate an id for each view, this assumes we have at most 256x256 cells
499 // per workspace screen
Adam Cohend22015c2010-07-26 22:02:18 -0700500 if (lp.cellX >= 0 && lp.cellX <= mCountX - 1 && lp.cellY >= 0 && lp.cellY <= mCountY - 1) {
Winson Chungaafa03c2010-06-11 17:34:16 -0700501 // If the horizontal or vertical span is set to -1, it is taken to
502 // mean that it spans the extent of the CellLayout
Adam Cohend22015c2010-07-26 22:02:18 -0700503 if (lp.cellHSpan < 0) lp.cellHSpan = mCountX;
504 if (lp.cellVSpan < 0) lp.cellVSpan = mCountY;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800505
Winson Chungaafa03c2010-06-11 17:34:16 -0700506 child.setId(childId);
507
Michael Jurka8c920dd2011-01-20 14:16:56 -0800508 mChildren.addView(child, index, lp);
Michael Jurkadee05892010-07-27 10:01:56 -0700509
Michael Jurkaf3ca3ab2010-10-20 17:08:24 -0700510 if (markCells) markCellsAsOccupiedForView(child);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700511
Winson Chungaafa03c2010-06-11 17:34:16 -0700512 return true;
513 }
514 return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800515 }
Michael Jurka3e7c7632010-10-02 16:01:03 -0700516
Michael Jurkabea15192010-11-17 12:33:46 -0800517 public void setAcceptsDrops(boolean acceptsDrops) {
518 if (mAcceptsDrops != acceptsDrops) {
519 mAcceptsDrops = acceptsDrops;
520 invalidate();
521 }
522 }
523
Michael Jurka3e7c7632010-10-02 16:01:03 -0700524 public boolean getAcceptsDrops() {
525 return mAcceptsDrops;
526 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800527
528 @Override
Michael Jurka0280c3b2010-09-17 15:00:07 -0700529 public void removeAllViews() {
530 clearOccupiedCells();
Michael Jurka8c920dd2011-01-20 14:16:56 -0800531 mChildren.removeAllViews();
Michael Jurka0280c3b2010-09-17 15:00:07 -0700532 }
533
534 @Override
535 public void removeAllViewsInLayout() {
536 clearOccupiedCells();
Michael Jurka8c920dd2011-01-20 14:16:56 -0800537 mChildren.removeAllViewsInLayout();
Michael Jurka0280c3b2010-09-17 15:00:07 -0700538 }
539
Michael Jurkaf3ca3ab2010-10-20 17:08:24 -0700540 public void removeViewWithoutMarkingCells(View view) {
Michael Jurka8c920dd2011-01-20 14:16:56 -0800541 mChildren.removeViewWithoutMarkingCells(view);
Michael Jurkaf3ca3ab2010-10-20 17:08:24 -0700542 }
543
Michael Jurka0280c3b2010-09-17 15:00:07 -0700544 @Override
545 public void removeView(View view) {
546 markCellsAsUnoccupiedForView(view);
Michael Jurka8c920dd2011-01-20 14:16:56 -0800547 mChildren.removeView(view);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700548 }
549
550 @Override
551 public void removeViewAt(int index) {
Michael Jurka8c920dd2011-01-20 14:16:56 -0800552 markCellsAsUnoccupiedForView(mChildren.getChildAt(index));
553 mChildren.removeViewAt(index);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700554 }
555
556 @Override
557 public void removeViewInLayout(View view) {
558 markCellsAsUnoccupiedForView(view);
Michael Jurka8c920dd2011-01-20 14:16:56 -0800559 mChildren.removeViewInLayout(view);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700560 }
561
562 @Override
563 public void removeViews(int start, int count) {
564 for (int i = start; i < start + count; i++) {
Michael Jurka8c920dd2011-01-20 14:16:56 -0800565 markCellsAsUnoccupiedForView(mChildren.getChildAt(i));
Michael Jurka0280c3b2010-09-17 15:00:07 -0700566 }
Michael Jurka8c920dd2011-01-20 14:16:56 -0800567 mChildren.removeViews(start, count);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700568 }
569
570 @Override
571 public void removeViewsInLayout(int start, int count) {
572 for (int i = start; i < start + count; i++) {
Michael Jurka8c920dd2011-01-20 14:16:56 -0800573 markCellsAsUnoccupiedForView(mChildren.getChildAt(i));
Michael Jurka0280c3b2010-09-17 15:00:07 -0700574 }
Michael Jurka8c920dd2011-01-20 14:16:56 -0800575 mChildren.removeViewsInLayout(start, count);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700576 }
577
Michael Jurka8c920dd2011-01-20 14:16:56 -0800578 public void drawChildren(Canvas canvas) {
579 mChildren.draw(canvas);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800580 }
581
582 @Override
583 protected void onAttachedToWindow() {
584 super.onAttachedToWindow();
585 mCellInfo.screen = ((ViewGroup) getParent()).indexOfChild(this);
586 }
587
Michael Jurkaaf442092010-06-10 17:01:57 -0700588 public void setTagToCellInfoForPoint(int touchX, int touchY) {
589 final CellInfo cellInfo = mCellInfo;
590 final Rect frame = mRect;
591 final int x = touchX + mScrollX;
592 final int y = touchY + mScrollY;
Michael Jurka8c920dd2011-01-20 14:16:56 -0800593 final int count = mChildren.getChildCount();
Michael Jurkaaf442092010-06-10 17:01:57 -0700594
595 boolean found = false;
596 for (int i = count - 1; i >= 0; i--) {
Michael Jurka8c920dd2011-01-20 14:16:56 -0800597 final View child = mChildren.getChildAt(i);
Michael Jurkaaf442092010-06-10 17:01:57 -0700598
599 if ((child.getVisibility()) == VISIBLE || child.getAnimation() != null) {
600 child.getHitRect(frame);
601 if (frame.contains(x, y)) {
602 final LayoutParams lp = (LayoutParams) child.getLayoutParams();
603 cellInfo.cell = child;
604 cellInfo.cellX = lp.cellX;
605 cellInfo.cellY = lp.cellY;
606 cellInfo.spanX = lp.cellHSpan;
607 cellInfo.spanY = lp.cellVSpan;
608 cellInfo.valid = true;
609 found = true;
Michael Jurkaaf442092010-06-10 17:01:57 -0700610 break;
611 }
612 }
613 }
Winson Chungaafa03c2010-06-11 17:34:16 -0700614
Michael Jurkaaf442092010-06-10 17:01:57 -0700615 if (!found) {
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700616 final int cellXY[] = mTmpCellXY;
Michael Jurkaaf442092010-06-10 17:01:57 -0700617 pointToCellExact(x, y, cellXY);
618
Michael Jurkaaf442092010-06-10 17:01:57 -0700619 cellInfo.cell = null;
620 cellInfo.cellX = cellXY[0];
621 cellInfo.cellY = cellXY[1];
622 cellInfo.spanX = 1;
623 cellInfo.spanY = 1;
Michael Jurka0280c3b2010-09-17 15:00:07 -0700624 cellInfo.valid = cellXY[0] >= 0 && cellXY[1] >= 0 && cellXY[0] < mCountX &&
625 cellXY[1] < mCountY && !mOccupied[cellXY[0]][cellXY[1]];
Michael Jurkaaf442092010-06-10 17:01:57 -0700626 }
627 setTag(cellInfo);
628 }
629
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800630 @Override
631 public boolean onInterceptTouchEvent(MotionEvent ev) {
Michael Jurkadee05892010-07-27 10:01:56 -0700632 if (mInterceptTouchListener != null && mInterceptTouchListener.onTouch(this, ev)) {
633 return true;
634 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800635 final int action = ev.getAction();
636 final CellInfo cellInfo = mCellInfo;
637
638 if (action == MotionEvent.ACTION_DOWN) {
Michael Jurkaaf442092010-06-10 17:01:57 -0700639 setTagToCellInfoForPoint((int) ev.getX(), (int) ev.getY());
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800640 } else if (action == MotionEvent.ACTION_UP) {
641 cellInfo.cell = null;
642 cellInfo.cellX = -1;
643 cellInfo.cellY = -1;
644 cellInfo.spanX = 0;
645 cellInfo.spanY = 0;
646 cellInfo.valid = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800647 setTag(cellInfo);
648 }
649
650 return false;
651 }
652
653 @Override
654 public CellInfo getTag() {
Michael Jurka0280c3b2010-09-17 15:00:07 -0700655 return (CellInfo) super.getTag();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800656 }
657
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700658 /**
Winson Chungaafa03c2010-06-11 17:34:16 -0700659 * Given a point, return the cell that strictly encloses that point
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800660 * @param x X coordinate of the point
661 * @param y Y coordinate of the point
662 * @param result Array of 2 ints to hold the x and y coordinate of the cell
663 */
664 void pointToCellExact(int x, int y, int[] result) {
Winson Chungaafa03c2010-06-11 17:34:16 -0700665 final int hStartPadding = getLeftPadding();
666 final int vStartPadding = getTopPadding();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800667
668 result[0] = (x - hStartPadding) / (mCellWidth + mWidthGap);
669 result[1] = (y - vStartPadding) / (mCellHeight + mHeightGap);
670
Adam Cohend22015c2010-07-26 22:02:18 -0700671 final int xAxis = mCountX;
672 final int yAxis = mCountY;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800673
674 if (result[0] < 0) result[0] = 0;
675 if (result[0] >= xAxis) result[0] = xAxis - 1;
676 if (result[1] < 0) result[1] = 0;
677 if (result[1] >= yAxis) result[1] = yAxis - 1;
678 }
Winson Chungaafa03c2010-06-11 17:34:16 -0700679
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800680 /**
681 * Given a point, return the cell that most closely encloses that point
682 * @param x X coordinate of the point
683 * @param y Y coordinate of the point
684 * @param result Array of 2 ints to hold the x and y coordinate of the cell
685 */
686 void pointToCellRounded(int x, int y, int[] result) {
687 pointToCellExact(x + (mCellWidth / 2), y + (mCellHeight / 2), result);
688 }
689
690 /**
691 * Given a cell coordinate, return the point that represents the upper left corner of that cell
Winson Chungaafa03c2010-06-11 17:34:16 -0700692 *
693 * @param cellX X coordinate of the cell
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800694 * @param cellY Y coordinate of the cell
Winson Chungaafa03c2010-06-11 17:34:16 -0700695 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800696 * @param result Array of 2 ints to hold the x and y coordinate of the point
697 */
698 void cellToPoint(int cellX, int cellY, int[] result) {
Winson Chungaafa03c2010-06-11 17:34:16 -0700699 final int hStartPadding = getLeftPadding();
700 final int vStartPadding = getTopPadding();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800701
702 result[0] = hStartPadding + cellX * (mCellWidth + mWidthGap);
703 result[1] = vStartPadding + cellY * (mCellHeight + mHeightGap);
704 }
705
Romain Guy84f296c2009-11-04 15:00:44 -0800706 int getCellWidth() {
707 return mCellWidth;
708 }
709
710 int getCellHeight() {
711 return mCellHeight;
712 }
713
Romain Guy1a304a12009-11-10 00:02:32 -0800714 int getLeftPadding() {
Winson Chungaafa03c2010-06-11 17:34:16 -0700715 return mLeftPadding;
Romain Guy1a304a12009-11-10 00:02:32 -0800716 }
717
718 int getTopPadding() {
Winson Chungaafa03c2010-06-11 17:34:16 -0700719 return mTopPadding;
Romain Guy1a304a12009-11-10 00:02:32 -0800720 }
721
722 int getRightPadding() {
Winson Chungaafa03c2010-06-11 17:34:16 -0700723 return mRightPadding;
Romain Guy1a304a12009-11-10 00:02:32 -0800724 }
725
726 int getBottomPadding() {
Winson Chungaafa03c2010-06-11 17:34:16 -0700727 return mBottomPadding;
Romain Guy1a304a12009-11-10 00:02:32 -0800728 }
729
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800730 @Override
731 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
732 // TODO: currently ignoring padding
Winson Chungaafa03c2010-06-11 17:34:16 -0700733
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800734 int widthSpecMode = MeasureSpec.getMode(widthMeasureSpec);
Winson Chungaafa03c2010-06-11 17:34:16 -0700735 int widthSpecSize = MeasureSpec.getSize(widthMeasureSpec);
736
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800737 int heightSpecMode = MeasureSpec.getMode(heightMeasureSpec);
738 int heightSpecSize = MeasureSpec.getSize(heightMeasureSpec);
Winson Chungaafa03c2010-06-11 17:34:16 -0700739
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800740 if (widthSpecMode == MeasureSpec.UNSPECIFIED || heightSpecMode == MeasureSpec.UNSPECIFIED) {
741 throw new RuntimeException("CellLayout cannot have UNSPECIFIED dimensions");
742 }
743
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800744 final int cellWidth = mCellWidth;
745 final int cellHeight = mCellHeight;
746
Adam Cohend22015c2010-07-26 22:02:18 -0700747 int numWidthGaps = mCountX - 1;
748 int numHeightGaps = mCountY - 1;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800749
Winson Chungece7f5b2010-10-22 14:54:12 -0700750 if (mWidthGap < 0 || mHeightGap < 0) {
751 int vSpaceLeft = heightSpecSize - mTopPadding - mBottomPadding - (cellHeight * mCountY);
752 mHeightGap = vSpaceLeft / numHeightGaps;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800753
Winson Chungece7f5b2010-10-22 14:54:12 -0700754 int hSpaceLeft = widthSpecSize - mLeftPadding - mRightPadding - (cellWidth * mCountX);
755 mWidthGap = hSpaceLeft / numWidthGaps;
Winson Chungaafa03c2010-06-11 17:34:16 -0700756
Winson Chungece7f5b2010-10-22 14:54:12 -0700757 // center it around the min gaps
758 int minGap = Math.min(mWidthGap, mHeightGap);
759 mWidthGap = mHeightGap = minGap;
760 }
Michael Jurka5f1c5092010-09-03 14:15:02 -0700761
Michael Jurka8c920dd2011-01-20 14:16:56 -0800762 // Initial values correspond to widthSpecMode == MeasureSpec.EXACTLY
763 int newWidth = widthSpecSize;
764 int newHeight = heightSpecSize;
Michael Jurka5f1c5092010-09-03 14:15:02 -0700765 if (widthSpecMode == MeasureSpec.AT_MOST) {
Michael Jurka8c920dd2011-01-20 14:16:56 -0800766 newWidth = mLeftPadding + mRightPadding + (mCountX * cellWidth) +
Winson Chungece7f5b2010-10-22 14:54:12 -0700767 ((mCountX - 1) * mWidthGap);
Michael Jurka8c920dd2011-01-20 14:16:56 -0800768 newHeight = mTopPadding + mBottomPadding + (mCountY * cellHeight) +
Winson Chungece7f5b2010-10-22 14:54:12 -0700769 ((mCountY - 1) * mHeightGap);
Michael Jurka5f1c5092010-09-03 14:15:02 -0700770 setMeasuredDimension(newWidth, newHeight);
Michael Jurka5f1c5092010-09-03 14:15:02 -0700771 }
Michael Jurka8c920dd2011-01-20 14:16:56 -0800772
773 int count = getChildCount();
774 for (int i = 0; i < count; i++) {
775 View child = getChildAt(i);
776 int childWidthMeasureSpec = MeasureSpec.makeMeasureSpec(newWidth, MeasureSpec.EXACTLY);
777 int childheightMeasureSpec = MeasureSpec.makeMeasureSpec(newHeight,
778 MeasureSpec.EXACTLY);
779 child.measure(childWidthMeasureSpec, childheightMeasureSpec);
780 }
781 setMeasuredDimension(newWidth, newHeight);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800782 }
783
784 @Override
Michael Jurka28750fb2010-09-24 17:43:49 -0700785 protected void onLayout(boolean changed, int l, int t, int r, int b) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800786 int count = getChildCount();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800787 for (int i = 0; i < count; i++) {
Michael Jurka8c920dd2011-01-20 14:16:56 -0800788 View child = getChildAt(i);
789 child.layout(0, 0, r - l, b - t);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800790 }
791 }
792
793 @Override
Michael Jurkadee05892010-07-27 10:01:56 -0700794 protected void onSizeChanged(int w, int h, int oldw, int oldh) {
795 super.onSizeChanged(w, h, oldw, oldh);
Michael Jurka18014792010-10-14 09:01:34 -0700796 mBackgroundRect.set(0, 0, w, h);
Michael Jurka33945b22010-12-21 18:19:38 -0800797 updateGlowRect();
Michael Jurkadee05892010-07-27 10:01:56 -0700798 }
799
800 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800801 protected void setChildrenDrawingCacheEnabled(boolean enabled) {
Michael Jurka8c920dd2011-01-20 14:16:56 -0800802 mChildren.setChildrenDrawingCacheEnabled(enabled);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800803 }
804
805 @Override
806 protected void setChildrenDrawnWithCacheEnabled(boolean enabled) {
Michael Jurka8c920dd2011-01-20 14:16:56 -0800807 mChildren.setChildrenDrawnWithCacheEnabled(enabled);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800808 }
809
Michael Jurka5f1c5092010-09-03 14:15:02 -0700810 public float getBackgroundAlpha() {
811 return mBackgroundAlpha;
Michael Jurkadee05892010-07-27 10:01:56 -0700812 }
813
Adam Cohen1b0aaac2010-10-28 11:11:18 -0700814 public void setBackgroundAlphaMultiplier(float multiplier) {
815 mBackgroundAlphaMultiplier = multiplier;
816 }
817
Adam Cohenddb82192010-11-10 16:32:54 -0800818 public float getBackgroundAlphaMultiplier() {
819 return mBackgroundAlphaMultiplier;
820 }
821
Michael Jurka5f1c5092010-09-03 14:15:02 -0700822 public void setBackgroundAlpha(float alpha) {
823 mBackgroundAlpha = alpha;
Michael Jurka0142d492010-08-25 17:46:15 -0700824 invalidate();
Michael Jurkadee05892010-07-27 10:01:56 -0700825 }
826
Michael Jurka5f1c5092010-09-03 14:15:02 -0700827 // Need to return true to let the view system know we know how to handle alpha-- this is
828 // because when our children have an alpha of 0.0f, they are still rendering their "dimmed"
829 // versions
830 @Override
831 protected boolean onSetAlpha(int alpha) {
832 return true;
833 }
834
835 public void setAlpha(float alpha) {
836 setChildrenAlpha(alpha);
837 super.setAlpha(alpha);
838 }
839
Michael Jurkadee05892010-07-27 10:01:56 -0700840 private void setChildrenAlpha(float alpha) {
Michael Jurka0142d492010-08-25 17:46:15 -0700841 final int childCount = getChildCount();
842 for (int i = 0; i < childCount; i++) {
Michael Jurkadee05892010-07-27 10:01:56 -0700843 getChildAt(i).setAlpha(alpha);
844 }
845 }
846
Patrick Dubroy440c3602010-07-13 17:50:32 -0700847 public View getChildAt(int x, int y) {
Michael Jurka8c920dd2011-01-20 14:16:56 -0800848 return mChildren.getChildAt(x, y);
Patrick Dubroy440c3602010-07-13 17:50:32 -0700849 }
850
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700851 /**
852 * Estimate where the top left cell of the dragged item will land if it is dropped.
853 *
854 * @param originX The X value of the top left corner of the item
855 * @param originY The Y value of the top left corner of the item
856 * @param spanX The number of horizontal cells that the item spans
857 * @param spanY The number of vertical cells that the item spans
858 * @param result The estimated drop cell X and Y.
859 */
860 void estimateDropCell(int originX, int originY, int spanX, int spanY, int[] result) {
Adam Cohend22015c2010-07-26 22:02:18 -0700861 final int countX = mCountX;
862 final int countY = mCountY;
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700863
Michael Jurkaa63c4522010-08-19 13:52:27 -0700864 // pointToCellRounded takes the top left of a cell but will pad that with
865 // cellWidth/2 and cellHeight/2 when finding the matching cell
866 pointToCellRounded(originX, originY, result);
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700867
868 // If the item isn't fully on this screen, snap to the edges
869 int rightOverhang = result[0] + spanX - countX;
870 if (rightOverhang > 0) {
871 result[0] -= rightOverhang; // Snap to right
872 }
873 result[0] = Math.max(0, result[0]); // Snap to left
874 int bottomOverhang = result[1] + spanY - countY;
875 if (bottomOverhang > 0) {
876 result[1] -= bottomOverhang; // Snap to bottom
877 }
878 result[1] = Math.max(0, result[1]); // Snap to top
879 }
880
Joe Onorato4be866d2010-10-10 11:26:02 -0700881 void visualizeDropLocation(
882 View v, Bitmap dragOutline, int originX, int originY, int spanX, int spanY) {
883
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -0700884 final int oldDragCellX = mDragCell[0];
885 final int oldDragCellY = mDragCell[1];
Joe Onorato4be866d2010-10-10 11:26:02 -0700886 final int[] nearest = findNearestVacantArea(originX, originY, spanX, spanY, v, mDragCell);
Winson Chunga9abd0e2010-10-27 17:18:37 -0700887 if (v != null) {
888 mDragCenter.set(originX + (v.getWidth() / 2), originY + (v.getHeight() / 2));
889 } else {
890 mDragCenter.set(originX, originY);
891 }
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700892
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -0700893 if (nearest != null && (nearest[0] != oldDragCellX || nearest[1] != oldDragCellY)) {
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700894 // Find the top left corner of the rect the object will occupy
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700895 final int[] topLeft = mTmpPoint;
896 cellToPoint(nearest[0], nearest[1], topLeft);
897
Joe Onorato4be866d2010-10-10 11:26:02 -0700898 int left = topLeft[0];
899 int top = topLeft[1];
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700900
Winson Chunga9abd0e2010-10-27 17:18:37 -0700901 if (v != null) {
902 if (v.getParent() instanceof CellLayout) {
903 LayoutParams lp = (LayoutParams) v.getLayoutParams();
904 left += lp.leftMargin;
905 top += lp.topMargin;
906 }
Winson Chung150fbab2010-09-29 17:14:26 -0700907
Winson Chunga9abd0e2010-10-27 17:18:37 -0700908 // Offsets due to the size difference between the View and the dragOutline
909 left += (v.getWidth() - dragOutline.getWidth()) / 2;
910 top += (v.getHeight() - dragOutline.getHeight()) / 2;
911 }
Winson Chung150fbab2010-09-29 17:14:26 -0700912
Joe Onorato4be866d2010-10-10 11:26:02 -0700913 final int oldIndex = mDragOutlineCurrent;
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -0700914 mDragOutlineAnims[oldIndex].animateOut();
915 mDragOutlineCurrent = (oldIndex + 1) % mDragOutlines.length;
Winson Chung150fbab2010-09-29 17:14:26 -0700916
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -0700917 mDragOutlines[mDragOutlineCurrent].set(left, top);
918 mDragOutlineAnims[mDragOutlineCurrent].setTag(dragOutline);
919 mDragOutlineAnims[mDragOutlineCurrent].animateIn();
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700920 }
Patrick Dubroy49250ad2010-10-08 15:33:52 -0700921
922 // If we are drawing crosshairs, the entire CellLayout needs to be invalidated
923 if (mCrosshairsDrawable != null) {
924 invalidate();
925 }
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700926 }
927
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800928 /**
Jeff Sharkey70864282009-04-07 21:08:40 -0700929 * Find a vacant area that will fit the given bounds nearest the requested
930 * cell location. Uses Euclidean distance to score multiple vacant areas.
Winson Chungaafa03c2010-06-11 17:34:16 -0700931 *
Romain Guy51afc022009-05-04 18:03:43 -0700932 * @param pixelX The X location at which you want to search for a vacant area.
933 * @param pixelY The Y location at which you want to search for a vacant area.
Jeff Sharkey70864282009-04-07 21:08:40 -0700934 * @param spanX Horizontal span of the object.
935 * @param spanY Vertical span of the object.
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700936 * @param result Array in which to place the result, or null (in which case a new array will
937 * be allocated)
Jeff Sharkey70864282009-04-07 21:08:40 -0700938 * @return The X, Y cell of a vacant area that can contain this object,
939 * nearest the requested location.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800940 */
Michael Jurka6a1435d2010-09-27 17:35:12 -0700941 int[] findNearestVacantArea(
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700942 int pixelX, int pixelY, int spanX, int spanY, int[] result) {
943 return findNearestVacantArea(pixelX, pixelY, spanX, spanY, null, result);
Michael Jurka6a1435d2010-09-27 17:35:12 -0700944 }
Winson Chungaafa03c2010-06-11 17:34:16 -0700945
Michael Jurka6a1435d2010-09-27 17:35:12 -0700946 /**
947 * Find a vacant area that will fit the given bounds nearest the requested
948 * cell location. Uses Euclidean distance to score multiple vacant areas.
949 *
950 * @param pixelX The X location at which you want to search for a vacant area.
951 * @param pixelY The Y location at which you want to search for a vacant area.
952 * @param spanX Horizontal span of the object.
953 * @param spanY Vertical span of the object.
Michael Jurka6a1435d2010-09-27 17:35:12 -0700954 * @param ignoreView Considers space occupied by this view as unoccupied
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700955 * @param result Previously returned value to possibly recycle.
Michael Jurka6a1435d2010-09-27 17:35:12 -0700956 * @return The X, Y cell of a vacant area that can contain this object,
957 * nearest the requested location.
958 */
959 int[] findNearestVacantArea(
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700960 int pixelX, int pixelY, int spanX, int spanY, View ignoreView, int[] result) {
Michael Jurkac6ee42e2010-09-30 12:04:50 -0700961 // mark space take by ignoreView as available (method checks if ignoreView is null)
962 markCellsAsUnoccupiedForView(ignoreView);
963
Jeff Sharkey70864282009-04-07 21:08:40 -0700964 // Keep track of best-scoring drop area
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700965 final int[] bestXY = result != null ? result : new int[2];
Jeff Sharkey70864282009-04-07 21:08:40 -0700966 double bestDistance = Double.MAX_VALUE;
Winson Chungaafa03c2010-06-11 17:34:16 -0700967
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700968 final int countX = mCountX;
969 final int countY = mCountY;
970 final boolean[][] occupied = mOccupied;
971
Winson Chungbbc60d82010-11-11 16:34:41 -0800972 for (int y = 0; y < countY - (spanY - 1); y++) {
Michael Jurkac28de512010-08-13 11:27:44 -0700973 inner:
Winson Chungbbc60d82010-11-11 16:34:41 -0800974 for (int x = 0; x < countX - (spanX - 1); x++) {
Michael Jurkac28de512010-08-13 11:27:44 -0700975 for (int i = 0; i < spanX; i++) {
976 for (int j = 0; j < spanY; j++) {
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700977 if (occupied[x + i][y + j]) {
Winson Chungbbc60d82010-11-11 16:34:41 -0800978 // small optimization: we can skip to after the column we just found
Michael Jurkac28de512010-08-13 11:27:44 -0700979 // an occupied cell
Winson Chungbbc60d82010-11-11 16:34:41 -0800980 x += i;
Michael Jurkac28de512010-08-13 11:27:44 -0700981 continue inner;
982 }
983 }
984 }
985 final int[] cellXY = mTmpCellXY;
986 cellToPoint(x, y, cellXY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800987
Michael Jurkac28de512010-08-13 11:27:44 -0700988 double distance = Math.sqrt(Math.pow(cellXY[0] - pixelX, 2)
989 + Math.pow(cellXY[1] - pixelY, 2));
990 if (distance <= bestDistance) {
991 bestDistance = distance;
992 bestXY[0] = x;
993 bestXY[1] = y;
994 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800995 }
996 }
Michael Jurkac6ee42e2010-09-30 12:04:50 -0700997 // re-mark space taken by ignoreView as occupied
998 markCellsAsOccupiedForView(ignoreView);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800999
Winson Chungaafa03c2010-06-11 17:34:16 -07001000 // Return null if no suitable location found
Jeff Sharkey70864282009-04-07 21:08:40 -07001001 if (bestDistance < Double.MAX_VALUE) {
1002 return bestXY;
1003 } else {
1004 return null;
1005 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001006 }
Winson Chungaafa03c2010-06-11 17:34:16 -07001007
Michael Jurka0280c3b2010-09-17 15:00:07 -07001008 boolean existsEmptyCell() {
1009 return findCellForSpan(null, 1, 1);
1010 }
1011
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001012 /**
Michael Jurka0280c3b2010-09-17 15:00:07 -07001013 * Finds the upper-left coordinate of the first rectangle in the grid that can
1014 * hold a cell of the specified dimensions. If intersectX and intersectY are not -1,
1015 * then this method will only return coordinates for rectangles that contain the cell
1016 * (intersectX, intersectY)
1017 *
1018 * @param cellXY The array that will contain the position of a vacant cell if such a cell
1019 * can be found.
1020 * @param spanX The horizontal span of the cell we want to find.
1021 * @param spanY The vertical span of the cell we want to find.
1022 *
1023 * @return True if a vacant cell of the specified dimension was found, false otherwise.
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001024 */
Michael Jurka0280c3b2010-09-17 15:00:07 -07001025 boolean findCellForSpan(int[] cellXY, int spanX, int spanY) {
1026 return findCellForSpanThatIntersectsIgnoring(cellXY, spanX, spanY, -1, -1, null);
1027 }
1028
1029 /**
1030 * Like above, but ignores any cells occupied by the item "ignoreView"
1031 *
1032 * @param cellXY The array that will contain the position of a vacant cell if such a cell
1033 * can be found.
1034 * @param spanX The horizontal span of the cell we want to find.
1035 * @param spanY The vertical span of the cell we want to find.
1036 * @param ignoreView The home screen item we should treat as not occupying any space
1037 * @return
1038 */
1039 boolean findCellForSpanIgnoring(int[] cellXY, int spanX, int spanY, View ignoreView) {
1040 return findCellForSpanThatIntersectsIgnoring(cellXY, spanX, spanY, -1, -1, ignoreView);
1041 }
1042
1043 /**
1044 * Like above, but if intersectX and intersectY are not -1, then this method will try to
1045 * return coordinates for rectangles that contain the cell [intersectX, intersectY]
1046 *
1047 * @param spanX The horizontal span of the cell we want to find.
1048 * @param spanY The vertical span of the cell we want to find.
1049 * @param ignoreView The home screen item we should treat as not occupying any space
1050 * @param intersectX The X coordinate of the cell that we should try to overlap
1051 * @param intersectX The Y coordinate of the cell that we should try to overlap
1052 *
1053 * @return True if a vacant cell of the specified dimension was found, false otherwise.
1054 */
1055 boolean findCellForSpanThatIntersects(int[] cellXY, int spanX, int spanY,
1056 int intersectX, int intersectY) {
1057 return findCellForSpanThatIntersectsIgnoring(
1058 cellXY, spanX, spanY, intersectX, intersectY, null);
1059 }
1060
1061 /**
1062 * The superset of the above two methods
1063 */
1064 boolean findCellForSpanThatIntersectsIgnoring(int[] cellXY, int spanX, int spanY,
1065 int intersectX, int intersectY, View ignoreView) {
Michael Jurkac6ee42e2010-09-30 12:04:50 -07001066 // mark space take by ignoreView as available (method checks if ignoreView is null)
1067 markCellsAsUnoccupiedForView(ignoreView);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001068
Michael Jurka28750fb2010-09-24 17:43:49 -07001069 boolean foundCell = false;
Michael Jurka0280c3b2010-09-17 15:00:07 -07001070 while (true) {
1071 int startX = 0;
1072 if (intersectX >= 0) {
1073 startX = Math.max(startX, intersectX - (spanX - 1));
1074 }
1075 int endX = mCountX - (spanX - 1);
1076 if (intersectX >= 0) {
1077 endX = Math.min(endX, intersectX + (spanX - 1) + (spanX == 1 ? 1 : 0));
1078 }
1079 int startY = 0;
1080 if (intersectY >= 0) {
1081 startY = Math.max(startY, intersectY - (spanY - 1));
1082 }
1083 int endY = mCountY - (spanY - 1);
1084 if (intersectY >= 0) {
1085 endY = Math.min(endY, intersectY + (spanY - 1) + (spanY == 1 ? 1 : 0));
1086 }
1087
Winson Chungbbc60d82010-11-11 16:34:41 -08001088 for (int y = startY; y < endY && !foundCell; y++) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001089 inner:
Winson Chungbbc60d82010-11-11 16:34:41 -08001090 for (int x = startX; x < endX; x++) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001091 for (int i = 0; i < spanX; i++) {
1092 for (int j = 0; j < spanY; j++) {
1093 if (mOccupied[x + i][y + j]) {
Winson Chungbbc60d82010-11-11 16:34:41 -08001094 // small optimization: we can skip to after the column we just found
Michael Jurka0280c3b2010-09-17 15:00:07 -07001095 // an occupied cell
Winson Chungbbc60d82010-11-11 16:34:41 -08001096 x += i;
Michael Jurka0280c3b2010-09-17 15:00:07 -07001097 continue inner;
1098 }
1099 }
1100 }
1101 if (cellXY != null) {
1102 cellXY[0] = x;
1103 cellXY[1] = y;
1104 }
Michael Jurka28750fb2010-09-24 17:43:49 -07001105 foundCell = true;
1106 break;
Michael Jurka0280c3b2010-09-17 15:00:07 -07001107 }
1108 }
1109 if (intersectX == -1 && intersectY == -1) {
1110 break;
1111 } else {
1112 // if we failed to find anything, try again but without any requirements of
1113 // intersecting
1114 intersectX = -1;
1115 intersectY = -1;
1116 continue;
1117 }
1118 }
1119
Michael Jurkac6ee42e2010-09-30 12:04:50 -07001120 // re-mark space taken by ignoreView as occupied
1121 markCellsAsOccupiedForView(ignoreView);
Michael Jurka28750fb2010-09-24 17:43:49 -07001122 return foundCell;
Michael Jurka0280c3b2010-09-17 15:00:07 -07001123 }
1124
1125 /**
1126 * Called when drag has left this CellLayout or has been completed (successfully or not)
1127 */
1128 void onDragExit() {
Joe Onorato4be866d2010-10-10 11:26:02 -07001129 // This can actually be called when we aren't in a drag, e.g. when adding a new
1130 // item to this layout via the customize drawer.
1131 // Guard against that case.
1132 if (mDragging) {
1133 mDragging = false;
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001134
Joe Onorato4be866d2010-10-10 11:26:02 -07001135 // Fade out the drag indicators
1136 if (mCrosshairsAnimator != null) {
1137 mCrosshairsAnimator.animateOut();
1138 }
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001139 }
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -07001140
1141 // Invalidate the drag data
1142 mDragCell[0] = -1;
1143 mDragCell[1] = -1;
1144 mDragOutlineAnims[mDragOutlineCurrent].animateOut();
1145 mDragOutlineCurrent = (mDragOutlineCurrent + 1) % mDragOutlineAnims.length;
1146
Michael Jurka33945b22010-12-21 18:19:38 -08001147 setIsDragOverlapping(false);
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001148 }
1149
1150 /**
Winson Chungaafa03c2010-06-11 17:34:16 -07001151 * Mark a child as having been dropped.
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001152 * At the beginning of the drag operation, the child may have been on another
Patrick Dubroyce34a972010-10-19 10:34:32 -07001153 * screen, but it is re-parented before this method is called.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001154 *
1155 * @param child The child that is being dropped
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001156 */
Michael Jurkad3ef3062010-11-23 16:23:58 -08001157 void onDropChild(View child, boolean animate) {
Romain Guyd94533d2009-08-17 10:01:15 -07001158 if (child != null) {
1159 LayoutParams lp = (LayoutParams) child.getLayoutParams();
Romain Guyd94533d2009-08-17 10:01:15 -07001160 lp.isDragging = false;
Romain Guy84f296c2009-11-04 15:00:44 -08001161 lp.dropped = true;
Michael Jurkad3ef3062010-11-23 16:23:58 -08001162 lp.animateDrop = animate;
Patrick Dubroye3887cc2011-01-20 10:43:40 -08001163 child.setVisibility(animate ? View.INVISIBLE : View.VISIBLE);
Romain Guyd94533d2009-08-17 10:01:15 -07001164 child.requestLayout();
Romain Guyd94533d2009-08-17 10:01:15 -07001165 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001166 }
1167
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001168 /**
1169 * Start dragging the specified child
Winson Chungaafa03c2010-06-11 17:34:16 -07001170 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001171 * @param child The child that is being dragged
1172 */
1173 void onDragChild(View child) {
1174 LayoutParams lp = (LayoutParams) child.getLayoutParams();
1175 lp.isDragging = true;
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001176 }
1177
1178 /**
1179 * A drag event has begun over this layout.
1180 * It may have begun over this layout (in which case onDragChild is called first),
1181 * or it may have begun on another layout.
1182 */
Winson Chunga9abd0e2010-10-27 17:18:37 -07001183 void onDragEnter() {
Patrick Dubroyfe6bd872010-10-13 17:32:10 -07001184 if (!mDragging) {
Patrick Dubroyfe6bd872010-10-13 17:32:10 -07001185 // Fade in the drag indicators
1186 if (mCrosshairsAnimator != null) {
1187 mCrosshairsAnimator.animateIn();
1188 }
Joe Onorato4be866d2010-10-10 11:26:02 -07001189 }
1190 mDragging = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001191 }
Winson Chungaafa03c2010-06-11 17:34:16 -07001192
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001193 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001194 * Computes a bounding rectangle for a range of cells
Winson Chungaafa03c2010-06-11 17:34:16 -07001195 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001196 * @param cellX X coordinate of upper left corner expressed as a cell position
1197 * @param cellY Y coordinate of upper left corner expressed as a cell position
Winson Chungaafa03c2010-06-11 17:34:16 -07001198 * @param cellHSpan Width in cells
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001199 * @param cellVSpan Height in cells
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001200 * @param resultRect Rect into which to put the results
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001201 */
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001202 public void cellToRect(int cellX, int cellY, int cellHSpan, int cellVSpan, RectF resultRect) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001203 final int cellWidth = mCellWidth;
1204 final int cellHeight = mCellHeight;
1205 final int widthGap = mWidthGap;
1206 final int heightGap = mHeightGap;
Winson Chungaafa03c2010-06-11 17:34:16 -07001207
1208 final int hStartPadding = getLeftPadding();
1209 final int vStartPadding = getTopPadding();
1210
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001211 int width = cellHSpan * cellWidth + ((cellHSpan - 1) * widthGap);
1212 int height = cellVSpan * cellHeight + ((cellVSpan - 1) * heightGap);
1213
1214 int x = hStartPadding + cellX * (cellWidth + widthGap);
1215 int y = vStartPadding + cellY * (cellHeight + heightGap);
Winson Chungaafa03c2010-06-11 17:34:16 -07001216
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001217 resultRect.set(x, y, x + width, y + height);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001218 }
Winson Chungaafa03c2010-06-11 17:34:16 -07001219
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001220 /**
Winson Chungaafa03c2010-06-11 17:34:16 -07001221 * Computes the required horizontal and vertical cell spans to always
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001222 * fit the given rectangle.
Winson Chungaafa03c2010-06-11 17:34:16 -07001223 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001224 * @param width Width in pixels
1225 * @param height Height in pixels
Patrick Dubroy8f86ddc2010-07-16 13:55:32 -07001226 * @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 -08001227 */
Patrick Dubroy8f86ddc2010-07-16 13:55:32 -07001228 public int[] rectToCell(int width, int height, int[] result) {
Michael Jurka9987a5c2010-10-08 16:58:12 -07001229 return rectToCell(getResources(), width, height, result);
1230 }
1231
1232 public static int[] rectToCell(Resources resources, int width, int height, int[] result) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001233 // Always assume we're working with the smallest span to make sure we
1234 // reserve enough space in both orientations.
Joe Onorato79e56262009-09-21 15:23:04 -04001235 int actualWidth = resources.getDimensionPixelSize(R.dimen.workspace_cell_width);
1236 int actualHeight = resources.getDimensionPixelSize(R.dimen.workspace_cell_height);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001237 int smallerSize = Math.min(actualWidth, actualHeight);
Joe Onorato79e56262009-09-21 15:23:04 -04001238
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001239 // Always round up to next largest cell
1240 int spanX = (width + smallerSize) / smallerSize;
1241 int spanY = (height + smallerSize) / smallerSize;
Joe Onorato79e56262009-09-21 15:23:04 -04001242
Patrick Dubroy8f86ddc2010-07-16 13:55:32 -07001243 if (result == null) {
1244 return new int[] { spanX, spanY };
1245 }
1246 result[0] = spanX;
1247 result[1] = spanY;
1248 return result;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001249 }
1250
1251 /**
Patrick Dubroy047379a2010-12-19 22:02:04 -08001252 * Calculate the grid spans needed to fit given item
1253 */
1254 public void calculateSpans(ItemInfo info) {
1255 final int minWidth;
1256 final int minHeight;
1257
1258 if (info instanceof LauncherAppWidgetInfo) {
1259 minWidth = ((LauncherAppWidgetInfo) info).minWidth;
1260 minHeight = ((LauncherAppWidgetInfo) info).minHeight;
1261 } else if (info instanceof PendingAddWidgetInfo) {
1262 minWidth = ((PendingAddWidgetInfo) info).minWidth;
1263 minHeight = ((PendingAddWidgetInfo) info).minHeight;
1264 } else {
1265 // It's not a widget, so it must be 1x1
1266 info.spanX = info.spanY = 1;
1267 return;
1268 }
1269 int[] spans = rectToCell(minWidth, minHeight, null);
1270 info.spanX = spans[0];
1271 info.spanY = spans[1];
1272 }
1273
1274 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001275 * Find the first vacant cell, if there is one.
1276 *
1277 * @param vacant Holds the x and y coordinate of the vacant cell
1278 * @param spanX Horizontal cell span.
1279 * @param spanY Vertical cell span.
Winson Chungaafa03c2010-06-11 17:34:16 -07001280 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001281 * @return True if a vacant cell was found
1282 */
1283 public boolean getVacantCell(int[] vacant, int spanX, int spanY) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001284
Michael Jurka0280c3b2010-09-17 15:00:07 -07001285 return findVacantCell(vacant, spanX, spanY, mCountX, mCountY, mOccupied);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001286 }
1287
1288 static boolean findVacantCell(int[] vacant, int spanX, int spanY,
1289 int xCount, int yCount, boolean[][] occupied) {
1290
1291 for (int x = 0; x < xCount; x++) {
1292 for (int y = 0; y < yCount; y++) {
1293 boolean available = !occupied[x][y];
1294out: for (int i = x; i < x + spanX - 1 && x < xCount; i++) {
1295 for (int j = y; j < y + spanY - 1 && y < yCount; j++) {
1296 available = available && !occupied[i][j];
1297 if (!available) break out;
1298 }
1299 }
1300
1301 if (available) {
1302 vacant[0] = x;
1303 vacant[1] = y;
1304 return true;
1305 }
1306 }
1307 }
1308
1309 return false;
1310 }
1311
Michael Jurka0280c3b2010-09-17 15:00:07 -07001312 private void clearOccupiedCells() {
1313 for (int x = 0; x < mCountX; x++) {
1314 for (int y = 0; y < mCountY; y++) {
1315 mOccupied[x][y] = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001316 }
1317 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001318 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001319
Michael Jurka0280c3b2010-09-17 15:00:07 -07001320 public void onMove(View view, int newCellX, int newCellY) {
1321 LayoutParams lp = (LayoutParams) view.getLayoutParams();
1322 markCellsAsUnoccupiedForView(view);
1323 markCellsForView(newCellX, newCellY, lp.cellHSpan, lp.cellVSpan, true);
1324 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001325
Michael Jurka0280c3b2010-09-17 15:00:07 -07001326 private void markCellsAsOccupiedForView(View view) {
Michael Jurka8c920dd2011-01-20 14:16:56 -08001327 if (view == null || view.getParent() != mChildren) return;
Michael Jurka0280c3b2010-09-17 15:00:07 -07001328 LayoutParams lp = (LayoutParams) view.getLayoutParams();
1329 markCellsForView(lp.cellX, lp.cellY, lp.cellHSpan, lp.cellVSpan, true);
1330 }
1331
1332 private void markCellsAsUnoccupiedForView(View view) {
Michael Jurka8c920dd2011-01-20 14:16:56 -08001333 if (view == null || view.getParent() != mChildren) return;
Michael Jurka0280c3b2010-09-17 15:00:07 -07001334 LayoutParams lp = (LayoutParams) view.getLayoutParams();
1335 markCellsForView(lp.cellX, lp.cellY, lp.cellHSpan, lp.cellVSpan, false);
1336 }
1337
1338 private void markCellsForView(int cellX, int cellY, int spanX, int spanY, boolean value) {
1339 for (int x = cellX; x < cellX + spanX && x < mCountX; x++) {
1340 for (int y = cellY; y < cellY + spanY && y < mCountY; y++) {
1341 mOccupied[x][y] = value;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001342 }
1343 }
1344 }
1345
1346 @Override
1347 public ViewGroup.LayoutParams generateLayoutParams(AttributeSet attrs) {
1348 return new CellLayout.LayoutParams(getContext(), attrs);
1349 }
1350
1351 @Override
1352 protected boolean checkLayoutParams(ViewGroup.LayoutParams p) {
1353 return p instanceof CellLayout.LayoutParams;
1354 }
1355
1356 @Override
1357 protected ViewGroup.LayoutParams generateLayoutParams(ViewGroup.LayoutParams p) {
1358 return new CellLayout.LayoutParams(p);
1359 }
1360
Winson Chungaafa03c2010-06-11 17:34:16 -07001361 public static class CellLayoutAnimationController extends LayoutAnimationController {
1362 public CellLayoutAnimationController(Animation animation, float delay) {
1363 super(animation, delay);
1364 }
1365
1366 @Override
1367 protected long getDelayForView(View view) {
1368 return (int) (Math.random() * 150);
1369 }
1370 }
1371
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001372 public static class LayoutParams extends ViewGroup.MarginLayoutParams {
1373 /**
1374 * Horizontal location of the item in the grid.
1375 */
1376 @ViewDebug.ExportedProperty
1377 public int cellX;
1378
1379 /**
1380 * Vertical location of the item in the grid.
1381 */
1382 @ViewDebug.ExportedProperty
1383 public int cellY;
1384
1385 /**
1386 * Number of cells spanned horizontally by the item.
1387 */
1388 @ViewDebug.ExportedProperty
1389 public int cellHSpan;
1390
1391 /**
1392 * Number of cells spanned vertically by the item.
1393 */
1394 @ViewDebug.ExportedProperty
1395 public int cellVSpan;
Winson Chungaafa03c2010-06-11 17:34:16 -07001396
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001397 /**
1398 * Is this item currently being dragged
1399 */
1400 public boolean isDragging;
1401
1402 // X coordinate of the view in the layout.
1403 @ViewDebug.ExportedProperty
1404 int x;
1405 // Y coordinate of the view in the layout.
1406 @ViewDebug.ExportedProperty
1407 int y;
1408
Patrick Dubroyce34a972010-10-19 10:34:32 -07001409 /**
1410 * The old X coordinate of this item, relative to its current parent.
1411 * Used to animate the item into its new position.
1412 */
1413 int oldX;
1414
1415 /**
1416 * The old Y coordinate of this item, relative to its current parent.
1417 * Used to animate the item into its new position.
1418 */
1419 int oldY;
1420
Romain Guy84f296c2009-11-04 15:00:44 -08001421 boolean dropped;
Romain Guyfcb9e712009-10-02 16:06:52 -07001422
Michael Jurkad3ef3062010-11-23 16:23:58 -08001423 boolean animateDrop;
1424
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001425 public LayoutParams(Context c, AttributeSet attrs) {
1426 super(c, attrs);
1427 cellHSpan = 1;
1428 cellVSpan = 1;
1429 }
1430
1431 public LayoutParams(ViewGroup.LayoutParams source) {
1432 super(source);
1433 cellHSpan = 1;
1434 cellVSpan = 1;
1435 }
Winson Chungaafa03c2010-06-11 17:34:16 -07001436
1437 public LayoutParams(LayoutParams source) {
1438 super(source);
1439 this.cellX = source.cellX;
1440 this.cellY = source.cellY;
1441 this.cellHSpan = source.cellHSpan;
1442 this.cellVSpan = source.cellVSpan;
1443 }
1444
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001445 public LayoutParams(int cellX, int cellY, int cellHSpan, int cellVSpan) {
Romain Guy8f19cdd2010-01-08 15:07:00 -08001446 super(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001447 this.cellX = cellX;
1448 this.cellY = cellY;
1449 this.cellHSpan = cellHSpan;
1450 this.cellVSpan = cellVSpan;
1451 }
1452
1453 public void setup(int cellWidth, int cellHeight, int widthGap, int heightGap,
1454 int hStartPadding, int vStartPadding) {
Winson Chungaafa03c2010-06-11 17:34:16 -07001455
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001456 final int myCellHSpan = cellHSpan;
1457 final int myCellVSpan = cellVSpan;
1458 final int myCellX = cellX;
1459 final int myCellY = cellY;
Winson Chungaafa03c2010-06-11 17:34:16 -07001460
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001461 width = myCellHSpan * cellWidth + ((myCellHSpan - 1) * widthGap) -
1462 leftMargin - rightMargin;
1463 height = myCellVSpan * cellHeight + ((myCellVSpan - 1) * heightGap) -
1464 topMargin - bottomMargin;
1465
1466 x = hStartPadding + myCellX * (cellWidth + widthGap) + leftMargin;
1467 y = vStartPadding + myCellY * (cellHeight + heightGap) + topMargin;
1468 }
Winson Chungaafa03c2010-06-11 17:34:16 -07001469
1470 public String toString() {
1471 return "(" + this.cellX + ", " + this.cellY + ")";
1472 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001473 }
1474
Michael Jurka0280c3b2010-09-17 15:00:07 -07001475 // This class stores info for two purposes:
1476 // 1. When dragging items (mDragInfo in Workspace), we store the View, its cellX & cellY,
1477 // its spanX, spanY, and the screen it is on
1478 // 2. When long clicking on an empty cell in a CellLayout, we save information about the
1479 // cellX and cellY coordinates and which page was clicked. We then set this as a tag on
1480 // the CellLayout that was long clicked
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001481 static final class CellInfo implements ContextMenu.ContextMenuInfo {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001482 View cell;
Michael Jurkaa63c4522010-08-19 13:52:27 -07001483 int cellX = -1;
1484 int cellY = -1;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001485 int spanX;
1486 int spanY;
1487 int screen;
1488 boolean valid;
1489
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001490 @Override
1491 public String toString() {
Winson Chungaafa03c2010-06-11 17:34:16 -07001492 return "Cell[view=" + (cell == null ? "null" : cell.getClass())
1493 + ", x=" + cellX + ", y=" + cellY + "]";
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001494 }
1495 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001496}