blob: 9c629eef9f971621edac17c4d060c9aefbfa18ed [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 Jurka18014792010-10-14 09:01:34 -0700311 }
312 }
313
314 public float getHoverScale() {
Michael Jurka33945b22010-12-21 18:19:38 -0800315 return mGlowBackgroundScale;
Michael Jurka18014792010-10-14 09:01:34 -0700316 }
317
318 public float getHoverAlpha() {
Michael Jurka33945b22010-12-21 18:19:38 -0800319 return mGlowBackgroundAlpha;
Michael Jurka18014792010-10-14 09:01:34 -0700320 }
321
322 public void setHoverAlpha(float alpha) {
Michael Jurka33945b22010-12-21 18:19:38 -0800323 mGlowBackgroundAlpha = alpha;
Michael Jurka18014792010-10-14 09:01:34 -0700324 invalidate();
325 }
326
327 void animateDrop() {
328 if (LauncherApplication.isScreenXLarge()) {
329 Resources res = getResources();
330 float onDropScale = res.getInteger(R.integer.config_screenOnDropScalePercent) / 100.0f;
331 ObjectAnimator scaleUp = ObjectAnimator.ofFloat(this, "hoverScale", onDropScale);
332 scaleUp.setDuration(res.getInteger(R.integer.config_screenOnDropScaleUpDuration));
333 ObjectAnimator scaleDown = ObjectAnimator.ofFloat(this, "hoverScale", 1.0f);
334 scaleDown.setDuration(res.getInteger(R.integer.config_screenOnDropScaleDownDuration));
335 ObjectAnimator alphaFadeOut = ObjectAnimator.ofFloat(this, "hoverAlpha", 0.0f);
336
337 alphaFadeOut.setStartDelay(res.getInteger(R.integer.config_screenOnDropAlphaFadeDelay));
338 alphaFadeOut.setDuration(res.getInteger(R.integer.config_screenOnDropAlphaFadeDelay));
339
340 AnimatorSet bouncer = new AnimatorSet();
341 bouncer.play(scaleUp).before(scaleDown);
342 bouncer.play(scaleUp).with(alphaFadeOut);
Michael Jurka8edd75c2010-12-17 20:15:06 -0800343 bouncer.addListener(new AnimatorListenerAdapter() {
Michael Jurka3c4c20f2010-10-28 15:36:06 -0700344 @Override
Michael Jurka18014792010-10-14 09:01:34 -0700345 public void onAnimationStart(Animator animation) {
Michael Jurka33945b22010-12-21 18:19:38 -0800346 setIsDragOverlapping(true);
Michael Jurka18014792010-10-14 09:01:34 -0700347 }
Michael Jurka3c4c20f2010-10-28 15:36:06 -0700348 @Override
Michael Jurka8edd75c2010-12-17 20:15:06 -0800349 public void onAnimationEnd(Animator animation) {
Michael Jurka33945b22010-12-21 18:19:38 -0800350 setIsDragOverlapping(false);
Michael Jurka18014792010-10-14 09:01:34 -0700351 setHoverScale(1.0f);
352 setHoverAlpha(1.0f);
353 }
354 });
355 bouncer.start();
356 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800357 }
358
Michael Jurkabea15192010-11-17 12:33:46 -0800359 public void disableCacheUpdates() {
Michael Jurka8c920dd2011-01-20 14:16:56 -0800360 mChildren.disableCacheUpdates();
Michael Jurkabea15192010-11-17 12:33:46 -0800361 }
362
363 public void enableCacheUpdates() {
Michael Jurka8c920dd2011-01-20 14:16:56 -0800364 mChildren.enableCacheUpdates();
Michael Jurkabea15192010-11-17 12:33:46 -0800365 }
366
Jeff Sharkey83f111d2009-04-20 21:03:13 -0700367 @Override
Patrick Dubroy1262e362010-10-06 15:49:50 -0700368 protected void onDraw(Canvas canvas) {
Michael Jurka3e7c7632010-10-02 16:01:03 -0700369 // When we're large, we are either drawn in a "hover" state (ie when dragging an item to
370 // a neighboring page) or with just a normal background (if backgroundAlpha > 0.0f)
371 // When we're small, we are either drawn normally or in the "accepts drops" state (during
372 // a drag). However, we also drag the mini hover background *over* one of those two
373 // backgrounds
Winson Chung26cbf3a2011-01-06 16:25:55 -0800374 if (LauncherApplication.isScreenXLarge() && mBackgroundAlpha > 0.0f) {
Adam Cohenf34bab52010-09-30 14:11:56 -0700375 Drawable bg;
Michael Jurka33945b22010-12-21 18:19:38 -0800376 boolean mini = getScaleX() < 0.5f;
377
378 if (mIsDragOverlapping) {
379 // In the mini case, we draw the active_glow bg *over* the active background
380 bg = mini ? mActiveBackgroundMini : mActiveGlowBackground;
381 } else if (mIsDragOccuring && mAcceptsDrops) {
382 bg = mini ? mActiveBackgroundMini : mActiveBackground;
Winson Chungae222ab2011-01-10 11:45:27 -0800383 } else if (mIsDragOccuring && mIsDefaultDropTarget) {
Michael Jurka33945b22010-12-21 18:19:38 -0800384 bg = mini ? mNormalGlowBackgroundMini : mNormalGlowBackground;
Adam Cohenf34bab52010-09-30 14:11:56 -0700385 } else {
Michael Jurka33945b22010-12-21 18:19:38 -0800386 bg = mini ? mNormalBackgroundMini : mNormalBackground;
Adam Cohenf34bab52010-09-30 14:11:56 -0700387 }
Michael Jurka33945b22010-12-21 18:19:38 -0800388
389 bg.setAlpha((int) (mBackgroundAlpha * mBackgroundAlphaMultiplier * 255));
390 bg.setBounds(mBackgroundRect);
391 bg.draw(canvas);
392
393 if (mini && mIsDragOverlapping) {
Michael Jurka18014792010-10-14 09:01:34 -0700394 boolean modifiedClipRect = false;
Michael Jurka33945b22010-12-21 18:19:38 -0800395 if (mGlowBackgroundScale > 1.0f) {
Michael Jurka18014792010-10-14 09:01:34 -0700396 // If the hover background's scale is greater than 1, we'll be drawing outside
397 // the bounds of this CellLayout. Get around that by temporarily increasing the
398 // size of the clip rect
Michael Jurka33945b22010-12-21 18:19:38 -0800399 float marginFraction = (mGlowBackgroundScale - 1.0f) / 2.0f;
Michael Jurka18014792010-10-14 09:01:34 -0700400 Rect clipRect = canvas.getClipBounds();
401 int marginX = (int) (marginFraction * (clipRect.right - clipRect.left));
402 int marginY = (int) (marginFraction * (clipRect.bottom - clipRect.top));
403 canvas.save(Canvas.CLIP_SAVE_FLAG);
404 canvas.clipRect(-marginX, -marginY,
405 getWidth() + marginX, getHeight() + marginY, Region.Op.REPLACE);
406 modifiedClipRect = true;
407 }
408
Michael Jurka33945b22010-12-21 18:19:38 -0800409 mActiveGlowBackgroundMini.setAlpha(
410 (int) (mBackgroundAlpha * mGlowBackgroundAlpha * 255));
411 mActiveGlowBackgroundMini.setBounds(mGlowBackgroundRect);
412 mActiveGlowBackgroundMini.draw(canvas);
Michael Jurka18014792010-10-14 09:01:34 -0700413 if (modifiedClipRect) {
414 canvas.restore();
415 }
Michael Jurka3e7c7632010-10-02 16:01:03 -0700416 }
Michael Jurkaa63c4522010-08-19 13:52:27 -0700417 }
Romain Guya6abce82009-11-10 02:54:41 -0800418
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700419 if (mCrosshairsVisibility > 0.0f) {
420 final int countX = mCountX;
421 final int countY = mCountY;
422
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700423 final float MAX_ALPHA = 0.4f;
424 final int MAX_VISIBLE_DISTANCE = 600;
425 final float DISTANCE_MULTIPLIER = 0.002f;
426
427 final Drawable d = mCrosshairsDrawable;
428 final int width = d.getIntrinsicWidth();
429 final int height = d.getIntrinsicHeight();
430
431 int x = getLeftPadding() - (mWidthGap / 2) - (width / 2);
432 for (int col = 0; col <= countX; col++) {
433 int y = getTopPadding() - (mHeightGap / 2) - (height / 2);
434 for (int row = 0; row <= countY; row++) {
435 mTmpPointF.set(x - mDragCenter.x, y - mDragCenter.y);
436 float dist = mTmpPointF.length();
437 // Crosshairs further from the drag point are more faint
438 float alpha = Math.min(MAX_ALPHA,
439 DISTANCE_MULTIPLIER * (MAX_VISIBLE_DISTANCE - dist));
440 if (alpha > 0.0f) {
441 d.setBounds(x, y, x + width, y + height);
442 d.setAlpha((int) (alpha * 255 * mCrosshairsVisibility));
443 d.draw(canvas);
444 }
445 y += mCellHeight + mHeightGap;
446 }
447 x += mCellWidth + mWidthGap;
448 }
Joe Onorato4be866d2010-10-10 11:26:02 -0700449 }
Winson Chung150fbab2010-09-29 17:14:26 -0700450
Patrick Dubroy8e58e912010-10-14 13:21:48 -0700451 final Paint paint = mDragOutlinePaint;
Joe Onorato4be866d2010-10-10 11:26:02 -0700452 for (int i = 0; i < mDragOutlines.length; i++) {
Chet Haase472b2812010-10-14 07:02:04 -0700453 final float alpha = mDragOutlineAlphas[i];
Joe Onorato4be866d2010-10-10 11:26:02 -0700454 if (alpha > 0) {
455 final Point p = mDragOutlines[i];
456 final Bitmap b = (Bitmap) mDragOutlineAnims[i].getTag();
Chet Haase472b2812010-10-14 07:02:04 -0700457 paint.setAlpha((int)(alpha + .5f));
Joe Onorato4be866d2010-10-10 11:26:02 -0700458 canvas.drawBitmap(b, p.x, p.y, paint);
Winson Chung150fbab2010-09-29 17:14:26 -0700459 }
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700460 }
461 }
462
463 @Override
Jeff Sharkey83f111d2009-04-20 21:03:13 -0700464 public void cancelLongPress() {
465 super.cancelLongPress();
466
467 // Cancel long press for all children
468 final int count = getChildCount();
469 for (int i = 0; i < count; i++) {
470 final View child = getChildAt(i);
471 child.cancelLongPress();
472 }
473 }
474
Michael Jurkadee05892010-07-27 10:01:56 -0700475 public void setOnInterceptTouchListener(View.OnTouchListener listener) {
476 mInterceptTouchListener = listener;
477 }
478
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800479 int getCountX() {
Adam Cohend22015c2010-07-26 22:02:18 -0700480 return mCountX;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800481 }
482
483 int getCountY() {
Adam Cohend22015c2010-07-26 22:02:18 -0700484 return mCountY;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800485 }
486
Winson Chungaafa03c2010-06-11 17:34:16 -0700487 public boolean addViewToCellLayout(View child, int index, int childId, LayoutParams params) {
Michael Jurkaf3ca3ab2010-10-20 17:08:24 -0700488 return addViewToCellLayout(child, index, childId, params, true);
489 }
490
491 public boolean addViewToCellLayout(
492 View child, int index, int childId, LayoutParams params, boolean markCells) {
Winson Chungaafa03c2010-06-11 17:34:16 -0700493 final LayoutParams lp = params;
494
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800495 // Generate an id for each view, this assumes we have at most 256x256 cells
496 // per workspace screen
Adam Cohend22015c2010-07-26 22:02:18 -0700497 if (lp.cellX >= 0 && lp.cellX <= mCountX - 1 && lp.cellY >= 0 && lp.cellY <= mCountY - 1) {
Winson Chungaafa03c2010-06-11 17:34:16 -0700498 // If the horizontal or vertical span is set to -1, it is taken to
499 // mean that it spans the extent of the CellLayout
Adam Cohend22015c2010-07-26 22:02:18 -0700500 if (lp.cellHSpan < 0) lp.cellHSpan = mCountX;
501 if (lp.cellVSpan < 0) lp.cellVSpan = mCountY;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800502
Winson Chungaafa03c2010-06-11 17:34:16 -0700503 child.setId(childId);
504
Michael Jurka8c920dd2011-01-20 14:16:56 -0800505 mChildren.addView(child, index, lp);
Michael Jurkadee05892010-07-27 10:01:56 -0700506
Michael Jurkaf3ca3ab2010-10-20 17:08:24 -0700507 if (markCells) markCellsAsOccupiedForView(child);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700508
Winson Chungaafa03c2010-06-11 17:34:16 -0700509 return true;
510 }
511 return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800512 }
Michael Jurka3e7c7632010-10-02 16:01:03 -0700513
Michael Jurkabea15192010-11-17 12:33:46 -0800514 public void setAcceptsDrops(boolean acceptsDrops) {
515 if (mAcceptsDrops != acceptsDrops) {
516 mAcceptsDrops = acceptsDrops;
517 invalidate();
518 }
519 }
520
Michael Jurka3e7c7632010-10-02 16:01:03 -0700521 public boolean getAcceptsDrops() {
522 return mAcceptsDrops;
523 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800524
525 @Override
Michael Jurka0280c3b2010-09-17 15:00:07 -0700526 public void removeAllViews() {
527 clearOccupiedCells();
Michael Jurka8c920dd2011-01-20 14:16:56 -0800528 mChildren.removeAllViews();
Michael Jurka0280c3b2010-09-17 15:00:07 -0700529 }
530
531 @Override
532 public void removeAllViewsInLayout() {
533 clearOccupiedCells();
Michael Jurka8c920dd2011-01-20 14:16:56 -0800534 mChildren.removeAllViewsInLayout();
Michael Jurka0280c3b2010-09-17 15:00:07 -0700535 }
536
Michael Jurkaf3ca3ab2010-10-20 17:08:24 -0700537 public void removeViewWithoutMarkingCells(View view) {
Michael Jurka8c920dd2011-01-20 14:16:56 -0800538 mChildren.removeViewWithoutMarkingCells(view);
Michael Jurkaf3ca3ab2010-10-20 17:08:24 -0700539 }
540
Michael Jurka0280c3b2010-09-17 15:00:07 -0700541 @Override
542 public void removeView(View view) {
543 markCellsAsUnoccupiedForView(view);
Michael Jurka8c920dd2011-01-20 14:16:56 -0800544 mChildren.removeView(view);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700545 }
546
547 @Override
548 public void removeViewAt(int index) {
Michael Jurka8c920dd2011-01-20 14:16:56 -0800549 markCellsAsUnoccupiedForView(mChildren.getChildAt(index));
550 mChildren.removeViewAt(index);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700551 }
552
553 @Override
554 public void removeViewInLayout(View view) {
555 markCellsAsUnoccupiedForView(view);
Michael Jurka8c920dd2011-01-20 14:16:56 -0800556 mChildren.removeViewInLayout(view);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700557 }
558
559 @Override
560 public void removeViews(int start, int count) {
561 for (int i = start; i < start + count; i++) {
Michael Jurka8c920dd2011-01-20 14:16:56 -0800562 markCellsAsUnoccupiedForView(mChildren.getChildAt(i));
Michael Jurka0280c3b2010-09-17 15:00:07 -0700563 }
Michael Jurka8c920dd2011-01-20 14:16:56 -0800564 mChildren.removeViews(start, count);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700565 }
566
567 @Override
568 public void removeViewsInLayout(int start, int count) {
569 for (int i = start; i < start + count; i++) {
Michael Jurka8c920dd2011-01-20 14:16:56 -0800570 markCellsAsUnoccupiedForView(mChildren.getChildAt(i));
Michael Jurka0280c3b2010-09-17 15:00:07 -0700571 }
Michael Jurka8c920dd2011-01-20 14:16:56 -0800572 mChildren.removeViewsInLayout(start, count);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700573 }
574
Michael Jurka8c920dd2011-01-20 14:16:56 -0800575 public void drawChildren(Canvas canvas) {
576 mChildren.draw(canvas);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800577 }
578
579 @Override
580 protected void onAttachedToWindow() {
581 super.onAttachedToWindow();
582 mCellInfo.screen = ((ViewGroup) getParent()).indexOfChild(this);
583 }
584
Michael Jurkaaf442092010-06-10 17:01:57 -0700585 public void setTagToCellInfoForPoint(int touchX, int touchY) {
586 final CellInfo cellInfo = mCellInfo;
587 final Rect frame = mRect;
588 final int x = touchX + mScrollX;
589 final int y = touchY + mScrollY;
Michael Jurka8c920dd2011-01-20 14:16:56 -0800590 final int count = mChildren.getChildCount();
Michael Jurkaaf442092010-06-10 17:01:57 -0700591
592 boolean found = false;
593 for (int i = count - 1; i >= 0; i--) {
Michael Jurka8c920dd2011-01-20 14:16:56 -0800594 final View child = mChildren.getChildAt(i);
Michael Jurkaaf442092010-06-10 17:01:57 -0700595
596 if ((child.getVisibility()) == VISIBLE || child.getAnimation() != null) {
597 child.getHitRect(frame);
598 if (frame.contains(x, y)) {
599 final LayoutParams lp = (LayoutParams) child.getLayoutParams();
600 cellInfo.cell = child;
601 cellInfo.cellX = lp.cellX;
602 cellInfo.cellY = lp.cellY;
603 cellInfo.spanX = lp.cellHSpan;
604 cellInfo.spanY = lp.cellVSpan;
605 cellInfo.valid = true;
606 found = true;
Michael Jurkaaf442092010-06-10 17:01:57 -0700607 break;
608 }
609 }
610 }
Winson Chungaafa03c2010-06-11 17:34:16 -0700611
Michael Jurkaaf442092010-06-10 17:01:57 -0700612 if (!found) {
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700613 final int cellXY[] = mTmpCellXY;
Michael Jurkaaf442092010-06-10 17:01:57 -0700614 pointToCellExact(x, y, cellXY);
615
Michael Jurkaaf442092010-06-10 17:01:57 -0700616 cellInfo.cell = null;
617 cellInfo.cellX = cellXY[0];
618 cellInfo.cellY = cellXY[1];
619 cellInfo.spanX = 1;
620 cellInfo.spanY = 1;
Michael Jurka0280c3b2010-09-17 15:00:07 -0700621 cellInfo.valid = cellXY[0] >= 0 && cellXY[1] >= 0 && cellXY[0] < mCountX &&
622 cellXY[1] < mCountY && !mOccupied[cellXY[0]][cellXY[1]];
Michael Jurkaaf442092010-06-10 17:01:57 -0700623 }
624 setTag(cellInfo);
625 }
626
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800627 @Override
628 public boolean onInterceptTouchEvent(MotionEvent ev) {
Michael Jurkadee05892010-07-27 10:01:56 -0700629 if (mInterceptTouchListener != null && mInterceptTouchListener.onTouch(this, ev)) {
630 return true;
631 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800632 final int action = ev.getAction();
633 final CellInfo cellInfo = mCellInfo;
634
635 if (action == MotionEvent.ACTION_DOWN) {
Michael Jurkaaf442092010-06-10 17:01:57 -0700636 setTagToCellInfoForPoint((int) ev.getX(), (int) ev.getY());
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800637 } else if (action == MotionEvent.ACTION_UP) {
638 cellInfo.cell = null;
639 cellInfo.cellX = -1;
640 cellInfo.cellY = -1;
641 cellInfo.spanX = 0;
642 cellInfo.spanY = 0;
643 cellInfo.valid = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800644 setTag(cellInfo);
645 }
646
647 return false;
648 }
649
650 @Override
651 public CellInfo getTag() {
Michael Jurka0280c3b2010-09-17 15:00:07 -0700652 return (CellInfo) super.getTag();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800653 }
654
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700655 /**
Winson Chungaafa03c2010-06-11 17:34:16 -0700656 * Given a point, return the cell that strictly encloses that point
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800657 * @param x X coordinate of the point
658 * @param y Y coordinate of the point
659 * @param result Array of 2 ints to hold the x and y coordinate of the cell
660 */
661 void pointToCellExact(int x, int y, int[] result) {
Winson Chungaafa03c2010-06-11 17:34:16 -0700662 final int hStartPadding = getLeftPadding();
663 final int vStartPadding = getTopPadding();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800664
665 result[0] = (x - hStartPadding) / (mCellWidth + mWidthGap);
666 result[1] = (y - vStartPadding) / (mCellHeight + mHeightGap);
667
Adam Cohend22015c2010-07-26 22:02:18 -0700668 final int xAxis = mCountX;
669 final int yAxis = mCountY;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800670
671 if (result[0] < 0) result[0] = 0;
672 if (result[0] >= xAxis) result[0] = xAxis - 1;
673 if (result[1] < 0) result[1] = 0;
674 if (result[1] >= yAxis) result[1] = yAxis - 1;
675 }
Winson Chungaafa03c2010-06-11 17:34:16 -0700676
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800677 /**
678 * Given a point, return the cell that most closely encloses that point
679 * @param x X coordinate of the point
680 * @param y Y coordinate of the point
681 * @param result Array of 2 ints to hold the x and y coordinate of the cell
682 */
683 void pointToCellRounded(int x, int y, int[] result) {
684 pointToCellExact(x + (mCellWidth / 2), y + (mCellHeight / 2), result);
685 }
686
687 /**
688 * Given a cell coordinate, return the point that represents the upper left corner of that cell
Winson Chungaafa03c2010-06-11 17:34:16 -0700689 *
690 * @param cellX X coordinate of the cell
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800691 * @param cellY Y coordinate of the cell
Winson Chungaafa03c2010-06-11 17:34:16 -0700692 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800693 * @param result Array of 2 ints to hold the x and y coordinate of the point
694 */
695 void cellToPoint(int cellX, int cellY, int[] result) {
Winson Chungaafa03c2010-06-11 17:34:16 -0700696 final int hStartPadding = getLeftPadding();
697 final int vStartPadding = getTopPadding();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800698
699 result[0] = hStartPadding + cellX * (mCellWidth + mWidthGap);
700 result[1] = vStartPadding + cellY * (mCellHeight + mHeightGap);
701 }
702
Romain Guy84f296c2009-11-04 15:00:44 -0800703 int getCellWidth() {
704 return mCellWidth;
705 }
706
707 int getCellHeight() {
708 return mCellHeight;
709 }
710
Romain Guy1a304a12009-11-10 00:02:32 -0800711 int getLeftPadding() {
Winson Chungaafa03c2010-06-11 17:34:16 -0700712 return mLeftPadding;
Romain Guy1a304a12009-11-10 00:02:32 -0800713 }
714
715 int getTopPadding() {
Winson Chungaafa03c2010-06-11 17:34:16 -0700716 return mTopPadding;
Romain Guy1a304a12009-11-10 00:02:32 -0800717 }
718
719 int getRightPadding() {
Winson Chungaafa03c2010-06-11 17:34:16 -0700720 return mRightPadding;
Romain Guy1a304a12009-11-10 00:02:32 -0800721 }
722
723 int getBottomPadding() {
Winson Chungaafa03c2010-06-11 17:34:16 -0700724 return mBottomPadding;
Romain Guy1a304a12009-11-10 00:02:32 -0800725 }
726
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800727 @Override
728 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
729 // TODO: currently ignoring padding
Winson Chungaafa03c2010-06-11 17:34:16 -0700730
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800731 int widthSpecMode = MeasureSpec.getMode(widthMeasureSpec);
Winson Chungaafa03c2010-06-11 17:34:16 -0700732 int widthSpecSize = MeasureSpec.getSize(widthMeasureSpec);
733
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800734 int heightSpecMode = MeasureSpec.getMode(heightMeasureSpec);
735 int heightSpecSize = MeasureSpec.getSize(heightMeasureSpec);
Winson Chungaafa03c2010-06-11 17:34:16 -0700736
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800737 if (widthSpecMode == MeasureSpec.UNSPECIFIED || heightSpecMode == MeasureSpec.UNSPECIFIED) {
738 throw new RuntimeException("CellLayout cannot have UNSPECIFIED dimensions");
739 }
740
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800741 final int cellWidth = mCellWidth;
742 final int cellHeight = mCellHeight;
743
Adam Cohend22015c2010-07-26 22:02:18 -0700744 int numWidthGaps = mCountX - 1;
745 int numHeightGaps = mCountY - 1;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800746
Winson Chungece7f5b2010-10-22 14:54:12 -0700747 if (mWidthGap < 0 || mHeightGap < 0) {
748 int vSpaceLeft = heightSpecSize - mTopPadding - mBottomPadding - (cellHeight * mCountY);
749 mHeightGap = vSpaceLeft / numHeightGaps;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800750
Winson Chungece7f5b2010-10-22 14:54:12 -0700751 int hSpaceLeft = widthSpecSize - mLeftPadding - mRightPadding - (cellWidth * mCountX);
752 mWidthGap = hSpaceLeft / numWidthGaps;
Winson Chungaafa03c2010-06-11 17:34:16 -0700753
Winson Chungece7f5b2010-10-22 14:54:12 -0700754 // center it around the min gaps
755 int minGap = Math.min(mWidthGap, mHeightGap);
756 mWidthGap = mHeightGap = minGap;
757 }
Michael Jurka5f1c5092010-09-03 14:15:02 -0700758
Michael Jurka8c920dd2011-01-20 14:16:56 -0800759 // Initial values correspond to widthSpecMode == MeasureSpec.EXACTLY
760 int newWidth = widthSpecSize;
761 int newHeight = heightSpecSize;
Michael Jurka5f1c5092010-09-03 14:15:02 -0700762 if (widthSpecMode == MeasureSpec.AT_MOST) {
Michael Jurka8c920dd2011-01-20 14:16:56 -0800763 newWidth = mLeftPadding + mRightPadding + (mCountX * cellWidth) +
Winson Chungece7f5b2010-10-22 14:54:12 -0700764 ((mCountX - 1) * mWidthGap);
Michael Jurka8c920dd2011-01-20 14:16:56 -0800765 newHeight = mTopPadding + mBottomPadding + (mCountY * cellHeight) +
Winson Chungece7f5b2010-10-22 14:54:12 -0700766 ((mCountY - 1) * mHeightGap);
Michael Jurka5f1c5092010-09-03 14:15:02 -0700767 setMeasuredDimension(newWidth, newHeight);
Michael Jurka5f1c5092010-09-03 14:15:02 -0700768 }
Michael Jurka8c920dd2011-01-20 14:16:56 -0800769
770 int count = getChildCount();
771 for (int i = 0; i < count; i++) {
772 View child = getChildAt(i);
773 int childWidthMeasureSpec = MeasureSpec.makeMeasureSpec(newWidth, MeasureSpec.EXACTLY);
774 int childheightMeasureSpec = MeasureSpec.makeMeasureSpec(newHeight,
775 MeasureSpec.EXACTLY);
776 child.measure(childWidthMeasureSpec, childheightMeasureSpec);
777 }
778 setMeasuredDimension(newWidth, newHeight);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800779 }
780
781 @Override
Michael Jurka28750fb2010-09-24 17:43:49 -0700782 protected void onLayout(boolean changed, int l, int t, int r, int b) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800783 int count = getChildCount();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800784 for (int i = 0; i < count; i++) {
Michael Jurka8c920dd2011-01-20 14:16:56 -0800785 View child = getChildAt(i);
786 child.layout(0, 0, r - l, b - t);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800787 }
788 }
789
790 @Override
Michael Jurkadee05892010-07-27 10:01:56 -0700791 protected void onSizeChanged(int w, int h, int oldw, int oldh) {
792 super.onSizeChanged(w, h, oldw, oldh);
Michael Jurka18014792010-10-14 09:01:34 -0700793 mBackgroundRect.set(0, 0, w, h);
Michael Jurka33945b22010-12-21 18:19:38 -0800794 updateGlowRect();
Michael Jurkadee05892010-07-27 10:01:56 -0700795 }
796
797 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800798 protected void setChildrenDrawingCacheEnabled(boolean enabled) {
Michael Jurka8c920dd2011-01-20 14:16:56 -0800799 mChildren.setChildrenDrawingCacheEnabled(enabled);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800800 }
801
802 @Override
803 protected void setChildrenDrawnWithCacheEnabled(boolean enabled) {
Michael Jurka8c920dd2011-01-20 14:16:56 -0800804 mChildren.setChildrenDrawnWithCacheEnabled(enabled);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800805 }
806
Michael Jurka5f1c5092010-09-03 14:15:02 -0700807 public float getBackgroundAlpha() {
808 return mBackgroundAlpha;
Michael Jurkadee05892010-07-27 10:01:56 -0700809 }
810
Adam Cohen1b0aaac2010-10-28 11:11:18 -0700811 public void setBackgroundAlphaMultiplier(float multiplier) {
812 mBackgroundAlphaMultiplier = multiplier;
813 }
814
Adam Cohenddb82192010-11-10 16:32:54 -0800815 public float getBackgroundAlphaMultiplier() {
816 return mBackgroundAlphaMultiplier;
817 }
818
Michael Jurka5f1c5092010-09-03 14:15:02 -0700819 public void setBackgroundAlpha(float alpha) {
820 mBackgroundAlpha = alpha;
Michael Jurka0142d492010-08-25 17:46:15 -0700821 invalidate();
Michael Jurkadee05892010-07-27 10:01:56 -0700822 }
823
Michael Jurka5f1c5092010-09-03 14:15:02 -0700824 // Need to return true to let the view system know we know how to handle alpha-- this is
825 // because when our children have an alpha of 0.0f, they are still rendering their "dimmed"
826 // versions
827 @Override
828 protected boolean onSetAlpha(int alpha) {
829 return true;
830 }
831
832 public void setAlpha(float alpha) {
833 setChildrenAlpha(alpha);
834 super.setAlpha(alpha);
835 }
836
Michael Jurkadee05892010-07-27 10:01:56 -0700837 private void setChildrenAlpha(float alpha) {
Michael Jurka0142d492010-08-25 17:46:15 -0700838 final int childCount = getChildCount();
839 for (int i = 0; i < childCount; i++) {
Michael Jurkadee05892010-07-27 10:01:56 -0700840 getChildAt(i).setAlpha(alpha);
841 }
842 }
843
Patrick Dubroy440c3602010-07-13 17:50:32 -0700844 public View getChildAt(int x, int y) {
Michael Jurka8c920dd2011-01-20 14:16:56 -0800845 return mChildren.getChildAt(x, y);
Patrick Dubroy440c3602010-07-13 17:50:32 -0700846 }
847
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700848 /**
849 * Estimate where the top left cell of the dragged item will land if it is dropped.
850 *
851 * @param originX The X value of the top left corner of the item
852 * @param originY The Y value of the top left corner of the item
853 * @param spanX The number of horizontal cells that the item spans
854 * @param spanY The number of vertical cells that the item spans
855 * @param result The estimated drop cell X and Y.
856 */
857 void estimateDropCell(int originX, int originY, int spanX, int spanY, int[] result) {
Adam Cohend22015c2010-07-26 22:02:18 -0700858 final int countX = mCountX;
859 final int countY = mCountY;
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700860
Michael Jurkaa63c4522010-08-19 13:52:27 -0700861 // pointToCellRounded takes the top left of a cell but will pad that with
862 // cellWidth/2 and cellHeight/2 when finding the matching cell
863 pointToCellRounded(originX, originY, result);
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700864
865 // If the item isn't fully on this screen, snap to the edges
866 int rightOverhang = result[0] + spanX - countX;
867 if (rightOverhang > 0) {
868 result[0] -= rightOverhang; // Snap to right
869 }
870 result[0] = Math.max(0, result[0]); // Snap to left
871 int bottomOverhang = result[1] + spanY - countY;
872 if (bottomOverhang > 0) {
873 result[1] -= bottomOverhang; // Snap to bottom
874 }
875 result[1] = Math.max(0, result[1]); // Snap to top
876 }
877
Joe Onorato4be866d2010-10-10 11:26:02 -0700878 void visualizeDropLocation(
879 View v, Bitmap dragOutline, int originX, int originY, int spanX, int spanY) {
880
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -0700881 final int oldDragCellX = mDragCell[0];
882 final int oldDragCellY = mDragCell[1];
Joe Onorato4be866d2010-10-10 11:26:02 -0700883 final int[] nearest = findNearestVacantArea(originX, originY, spanX, spanY, v, mDragCell);
Winson Chunga9abd0e2010-10-27 17:18:37 -0700884 if (v != null) {
885 mDragCenter.set(originX + (v.getWidth() / 2), originY + (v.getHeight() / 2));
886 } else {
887 mDragCenter.set(originX, originY);
888 }
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700889
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -0700890 if (nearest != null && (nearest[0] != oldDragCellX || nearest[1] != oldDragCellY)) {
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700891 // Find the top left corner of the rect the object will occupy
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700892 final int[] topLeft = mTmpPoint;
893 cellToPoint(nearest[0], nearest[1], topLeft);
894
Joe Onorato4be866d2010-10-10 11:26:02 -0700895 int left = topLeft[0];
896 int top = topLeft[1];
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700897
Winson Chunga9abd0e2010-10-27 17:18:37 -0700898 if (v != null) {
899 if (v.getParent() instanceof CellLayout) {
900 LayoutParams lp = (LayoutParams) v.getLayoutParams();
901 left += lp.leftMargin;
902 top += lp.topMargin;
903 }
Winson Chung150fbab2010-09-29 17:14:26 -0700904
Winson Chunga9abd0e2010-10-27 17:18:37 -0700905 // Offsets due to the size difference between the View and the dragOutline
906 left += (v.getWidth() - dragOutline.getWidth()) / 2;
907 top += (v.getHeight() - dragOutline.getHeight()) / 2;
908 }
Winson Chung150fbab2010-09-29 17:14:26 -0700909
Joe Onorato4be866d2010-10-10 11:26:02 -0700910 final int oldIndex = mDragOutlineCurrent;
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -0700911 mDragOutlineAnims[oldIndex].animateOut();
912 mDragOutlineCurrent = (oldIndex + 1) % mDragOutlines.length;
Winson Chung150fbab2010-09-29 17:14:26 -0700913
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -0700914 mDragOutlines[mDragOutlineCurrent].set(left, top);
915 mDragOutlineAnims[mDragOutlineCurrent].setTag(dragOutline);
916 mDragOutlineAnims[mDragOutlineCurrent].animateIn();
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700917 }
Patrick Dubroy49250ad2010-10-08 15:33:52 -0700918
919 // If we are drawing crosshairs, the entire CellLayout needs to be invalidated
920 if (mCrosshairsDrawable != null) {
921 invalidate();
922 }
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700923 }
924
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800925 /**
Jeff Sharkey70864282009-04-07 21:08:40 -0700926 * Find a vacant area that will fit the given bounds nearest the requested
927 * cell location. Uses Euclidean distance to score multiple vacant areas.
Winson Chungaafa03c2010-06-11 17:34:16 -0700928 *
Romain Guy51afc022009-05-04 18:03:43 -0700929 * @param pixelX The X location at which you want to search for a vacant area.
930 * @param pixelY The Y location at which you want to search for a vacant area.
Jeff Sharkey70864282009-04-07 21:08:40 -0700931 * @param spanX Horizontal span of the object.
932 * @param spanY Vertical span of the object.
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700933 * @param result Array in which to place the result, or null (in which case a new array will
934 * be allocated)
Jeff Sharkey70864282009-04-07 21:08:40 -0700935 * @return The X, Y cell of a vacant area that can contain this object,
936 * nearest the requested location.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800937 */
Michael Jurka6a1435d2010-09-27 17:35:12 -0700938 int[] findNearestVacantArea(
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700939 int pixelX, int pixelY, int spanX, int spanY, int[] result) {
940 return findNearestVacantArea(pixelX, pixelY, spanX, spanY, null, result);
Michael Jurka6a1435d2010-09-27 17:35:12 -0700941 }
Winson Chungaafa03c2010-06-11 17:34:16 -0700942
Michael Jurka6a1435d2010-09-27 17:35:12 -0700943 /**
944 * Find a vacant area that will fit the given bounds nearest the requested
945 * cell location. Uses Euclidean distance to score multiple vacant areas.
946 *
947 * @param pixelX The X location at which you want to search for a vacant area.
948 * @param pixelY The Y location at which you want to search for a vacant area.
949 * @param spanX Horizontal span of the object.
950 * @param spanY Vertical span of the object.
Michael Jurka6a1435d2010-09-27 17:35:12 -0700951 * @param ignoreView Considers space occupied by this view as unoccupied
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700952 * @param result Previously returned value to possibly recycle.
Michael Jurka6a1435d2010-09-27 17:35:12 -0700953 * @return The X, Y cell of a vacant area that can contain this object,
954 * nearest the requested location.
955 */
956 int[] findNearestVacantArea(
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700957 int pixelX, int pixelY, int spanX, int spanY, View ignoreView, int[] result) {
Michael Jurkac6ee42e2010-09-30 12:04:50 -0700958 // mark space take by ignoreView as available (method checks if ignoreView is null)
959 markCellsAsUnoccupiedForView(ignoreView);
960
Jeff Sharkey70864282009-04-07 21:08:40 -0700961 // Keep track of best-scoring drop area
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700962 final int[] bestXY = result != null ? result : new int[2];
Jeff Sharkey70864282009-04-07 21:08:40 -0700963 double bestDistance = Double.MAX_VALUE;
Winson Chungaafa03c2010-06-11 17:34:16 -0700964
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700965 final int countX = mCountX;
966 final int countY = mCountY;
967 final boolean[][] occupied = mOccupied;
968
Winson Chungbbc60d82010-11-11 16:34:41 -0800969 for (int y = 0; y < countY - (spanY - 1); y++) {
Michael Jurkac28de512010-08-13 11:27:44 -0700970 inner:
Winson Chungbbc60d82010-11-11 16:34:41 -0800971 for (int x = 0; x < countX - (spanX - 1); x++) {
Michael Jurkac28de512010-08-13 11:27:44 -0700972 for (int i = 0; i < spanX; i++) {
973 for (int j = 0; j < spanY; j++) {
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700974 if (occupied[x + i][y + j]) {
Winson Chungbbc60d82010-11-11 16:34:41 -0800975 // small optimization: we can skip to after the column we just found
Michael Jurkac28de512010-08-13 11:27:44 -0700976 // an occupied cell
Winson Chungbbc60d82010-11-11 16:34:41 -0800977 x += i;
Michael Jurkac28de512010-08-13 11:27:44 -0700978 continue inner;
979 }
980 }
981 }
982 final int[] cellXY = mTmpCellXY;
983 cellToPoint(x, y, cellXY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800984
Michael Jurkac28de512010-08-13 11:27:44 -0700985 double distance = Math.sqrt(Math.pow(cellXY[0] - pixelX, 2)
986 + Math.pow(cellXY[1] - pixelY, 2));
987 if (distance <= bestDistance) {
988 bestDistance = distance;
989 bestXY[0] = x;
990 bestXY[1] = y;
991 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800992 }
993 }
Michael Jurkac6ee42e2010-09-30 12:04:50 -0700994 // re-mark space taken by ignoreView as occupied
995 markCellsAsOccupiedForView(ignoreView);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800996
Winson Chungaafa03c2010-06-11 17:34:16 -0700997 // Return null if no suitable location found
Jeff Sharkey70864282009-04-07 21:08:40 -0700998 if (bestDistance < Double.MAX_VALUE) {
999 return bestXY;
1000 } else {
1001 return null;
1002 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001003 }
Winson Chungaafa03c2010-06-11 17:34:16 -07001004
Michael Jurka0280c3b2010-09-17 15:00:07 -07001005 boolean existsEmptyCell() {
1006 return findCellForSpan(null, 1, 1);
1007 }
1008
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001009 /**
Michael Jurka0280c3b2010-09-17 15:00:07 -07001010 * Finds the upper-left coordinate of the first rectangle in the grid that can
1011 * hold a cell of the specified dimensions. If intersectX and intersectY are not -1,
1012 * then this method will only return coordinates for rectangles that contain the cell
1013 * (intersectX, intersectY)
1014 *
1015 * @param cellXY The array that will contain the position of a vacant cell if such a cell
1016 * can be found.
1017 * @param spanX The horizontal span of the cell we want to find.
1018 * @param spanY The vertical span of the cell we want to find.
1019 *
1020 * @return True if a vacant cell of the specified dimension was found, false otherwise.
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001021 */
Michael Jurka0280c3b2010-09-17 15:00:07 -07001022 boolean findCellForSpan(int[] cellXY, int spanX, int spanY) {
1023 return findCellForSpanThatIntersectsIgnoring(cellXY, spanX, spanY, -1, -1, null);
1024 }
1025
1026 /**
1027 * Like above, but ignores any cells occupied by the item "ignoreView"
1028 *
1029 * @param cellXY The array that will contain the position of a vacant cell if such a cell
1030 * can be found.
1031 * @param spanX The horizontal span of the cell we want to find.
1032 * @param spanY The vertical span of the cell we want to find.
1033 * @param ignoreView The home screen item we should treat as not occupying any space
1034 * @return
1035 */
1036 boolean findCellForSpanIgnoring(int[] cellXY, int spanX, int spanY, View ignoreView) {
1037 return findCellForSpanThatIntersectsIgnoring(cellXY, spanX, spanY, -1, -1, ignoreView);
1038 }
1039
1040 /**
1041 * Like above, but if intersectX and intersectY are not -1, then this method will try to
1042 * return coordinates for rectangles that contain the cell [intersectX, intersectY]
1043 *
1044 * @param spanX The horizontal span of the cell we want to find.
1045 * @param spanY The vertical span of the cell we want to find.
1046 * @param ignoreView The home screen item we should treat as not occupying any space
1047 * @param intersectX The X coordinate of the cell that we should try to overlap
1048 * @param intersectX The Y coordinate of the cell that we should try to overlap
1049 *
1050 * @return True if a vacant cell of the specified dimension was found, false otherwise.
1051 */
1052 boolean findCellForSpanThatIntersects(int[] cellXY, int spanX, int spanY,
1053 int intersectX, int intersectY) {
1054 return findCellForSpanThatIntersectsIgnoring(
1055 cellXY, spanX, spanY, intersectX, intersectY, null);
1056 }
1057
1058 /**
1059 * The superset of the above two methods
1060 */
1061 boolean findCellForSpanThatIntersectsIgnoring(int[] cellXY, int spanX, int spanY,
1062 int intersectX, int intersectY, View ignoreView) {
Michael Jurkac6ee42e2010-09-30 12:04:50 -07001063 // mark space take by ignoreView as available (method checks if ignoreView is null)
1064 markCellsAsUnoccupiedForView(ignoreView);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001065
Michael Jurka28750fb2010-09-24 17:43:49 -07001066 boolean foundCell = false;
Michael Jurka0280c3b2010-09-17 15:00:07 -07001067 while (true) {
1068 int startX = 0;
1069 if (intersectX >= 0) {
1070 startX = Math.max(startX, intersectX - (spanX - 1));
1071 }
1072 int endX = mCountX - (spanX - 1);
1073 if (intersectX >= 0) {
1074 endX = Math.min(endX, intersectX + (spanX - 1) + (spanX == 1 ? 1 : 0));
1075 }
1076 int startY = 0;
1077 if (intersectY >= 0) {
1078 startY = Math.max(startY, intersectY - (spanY - 1));
1079 }
1080 int endY = mCountY - (spanY - 1);
1081 if (intersectY >= 0) {
1082 endY = Math.min(endY, intersectY + (spanY - 1) + (spanY == 1 ? 1 : 0));
1083 }
1084
Winson Chungbbc60d82010-11-11 16:34:41 -08001085 for (int y = startY; y < endY && !foundCell; y++) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001086 inner:
Winson Chungbbc60d82010-11-11 16:34:41 -08001087 for (int x = startX; x < endX; x++) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001088 for (int i = 0; i < spanX; i++) {
1089 for (int j = 0; j < spanY; j++) {
1090 if (mOccupied[x + i][y + j]) {
Winson Chungbbc60d82010-11-11 16:34:41 -08001091 // small optimization: we can skip to after the column we just found
Michael Jurka0280c3b2010-09-17 15:00:07 -07001092 // an occupied cell
Winson Chungbbc60d82010-11-11 16:34:41 -08001093 x += i;
Michael Jurka0280c3b2010-09-17 15:00:07 -07001094 continue inner;
1095 }
1096 }
1097 }
1098 if (cellXY != null) {
1099 cellXY[0] = x;
1100 cellXY[1] = y;
1101 }
Michael Jurka28750fb2010-09-24 17:43:49 -07001102 foundCell = true;
1103 break;
Michael Jurka0280c3b2010-09-17 15:00:07 -07001104 }
1105 }
1106 if (intersectX == -1 && intersectY == -1) {
1107 break;
1108 } else {
1109 // if we failed to find anything, try again but without any requirements of
1110 // intersecting
1111 intersectX = -1;
1112 intersectY = -1;
1113 continue;
1114 }
1115 }
1116
Michael Jurkac6ee42e2010-09-30 12:04:50 -07001117 // re-mark space taken by ignoreView as occupied
1118 markCellsAsOccupiedForView(ignoreView);
Michael Jurka28750fb2010-09-24 17:43:49 -07001119 return foundCell;
Michael Jurka0280c3b2010-09-17 15:00:07 -07001120 }
1121
1122 /**
1123 * Called when drag has left this CellLayout or has been completed (successfully or not)
1124 */
1125 void onDragExit() {
Joe Onorato4be866d2010-10-10 11:26:02 -07001126 // This can actually be called when we aren't in a drag, e.g. when adding a new
1127 // item to this layout via the customize drawer.
1128 // Guard against that case.
1129 if (mDragging) {
1130 mDragging = false;
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001131
Joe Onorato4be866d2010-10-10 11:26:02 -07001132 // Fade out the drag indicators
1133 if (mCrosshairsAnimator != null) {
1134 mCrosshairsAnimator.animateOut();
1135 }
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001136 }
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -07001137
1138 // Invalidate the drag data
1139 mDragCell[0] = -1;
1140 mDragCell[1] = -1;
1141 mDragOutlineAnims[mDragOutlineCurrent].animateOut();
1142 mDragOutlineCurrent = (mDragOutlineCurrent + 1) % mDragOutlineAnims.length;
1143
Michael Jurka33945b22010-12-21 18:19:38 -08001144 setIsDragOverlapping(false);
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001145 }
1146
1147 /**
Winson Chungaafa03c2010-06-11 17:34:16 -07001148 * Mark a child as having been dropped.
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001149 * At the beginning of the drag operation, the child may have been on another
Patrick Dubroyce34a972010-10-19 10:34:32 -07001150 * screen, but it is re-parented before this method is called.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001151 *
1152 * @param child The child that is being dropped
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001153 */
Michael Jurkad3ef3062010-11-23 16:23:58 -08001154 void onDropChild(View child, boolean animate) {
Romain Guyd94533d2009-08-17 10:01:15 -07001155 if (child != null) {
1156 LayoutParams lp = (LayoutParams) child.getLayoutParams();
Romain Guyd94533d2009-08-17 10:01:15 -07001157 lp.isDragging = false;
Romain Guy84f296c2009-11-04 15:00:44 -08001158 lp.dropped = true;
Michael Jurkad3ef3062010-11-23 16:23:58 -08001159 lp.animateDrop = animate;
Patrick Dubroye3887cc2011-01-20 10:43:40 -08001160 child.setVisibility(animate ? View.INVISIBLE : View.VISIBLE);
Romain Guyd94533d2009-08-17 10:01:15 -07001161 child.requestLayout();
Romain Guyd94533d2009-08-17 10:01:15 -07001162 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001163 }
1164
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001165 /**
1166 * Start dragging the specified child
Winson Chungaafa03c2010-06-11 17:34:16 -07001167 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001168 * @param child The child that is being dragged
1169 */
1170 void onDragChild(View child) {
1171 LayoutParams lp = (LayoutParams) child.getLayoutParams();
1172 lp.isDragging = true;
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001173 }
1174
1175 /**
1176 * A drag event has begun over this layout.
1177 * It may have begun over this layout (in which case onDragChild is called first),
1178 * or it may have begun on another layout.
1179 */
Winson Chunga9abd0e2010-10-27 17:18:37 -07001180 void onDragEnter() {
Patrick Dubroyfe6bd872010-10-13 17:32:10 -07001181 if (!mDragging) {
Patrick Dubroyfe6bd872010-10-13 17:32:10 -07001182 // Fade in the drag indicators
1183 if (mCrosshairsAnimator != null) {
1184 mCrosshairsAnimator.animateIn();
1185 }
Joe Onorato4be866d2010-10-10 11:26:02 -07001186 }
1187 mDragging = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001188 }
Winson Chungaafa03c2010-06-11 17:34:16 -07001189
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001190 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001191 * Computes a bounding rectangle for a range of cells
Winson Chungaafa03c2010-06-11 17:34:16 -07001192 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001193 * @param cellX X coordinate of upper left corner expressed as a cell position
1194 * @param cellY Y coordinate of upper left corner expressed as a cell position
Winson Chungaafa03c2010-06-11 17:34:16 -07001195 * @param cellHSpan Width in cells
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001196 * @param cellVSpan Height in cells
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001197 * @param resultRect Rect into which to put the results
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001198 */
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001199 public void cellToRect(int cellX, int cellY, int cellHSpan, int cellVSpan, RectF resultRect) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001200 final int cellWidth = mCellWidth;
1201 final int cellHeight = mCellHeight;
1202 final int widthGap = mWidthGap;
1203 final int heightGap = mHeightGap;
Winson Chungaafa03c2010-06-11 17:34:16 -07001204
1205 final int hStartPadding = getLeftPadding();
1206 final int vStartPadding = getTopPadding();
1207
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001208 int width = cellHSpan * cellWidth + ((cellHSpan - 1) * widthGap);
1209 int height = cellVSpan * cellHeight + ((cellVSpan - 1) * heightGap);
1210
1211 int x = hStartPadding + cellX * (cellWidth + widthGap);
1212 int y = vStartPadding + cellY * (cellHeight + heightGap);
Winson Chungaafa03c2010-06-11 17:34:16 -07001213
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001214 resultRect.set(x, y, x + width, y + height);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001215 }
Winson Chungaafa03c2010-06-11 17:34:16 -07001216
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001217 /**
Winson Chungaafa03c2010-06-11 17:34:16 -07001218 * Computes the required horizontal and vertical cell spans to always
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001219 * fit the given rectangle.
Winson Chungaafa03c2010-06-11 17:34:16 -07001220 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001221 * @param width Width in pixels
1222 * @param height Height in pixels
Patrick Dubroy8f86ddc2010-07-16 13:55:32 -07001223 * @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 -08001224 */
Patrick Dubroy8f86ddc2010-07-16 13:55:32 -07001225 public int[] rectToCell(int width, int height, int[] result) {
Michael Jurka9987a5c2010-10-08 16:58:12 -07001226 return rectToCell(getResources(), width, height, result);
1227 }
1228
1229 public static int[] rectToCell(Resources resources, int width, int height, int[] result) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001230 // Always assume we're working with the smallest span to make sure we
1231 // reserve enough space in both orientations.
Joe Onorato79e56262009-09-21 15:23:04 -04001232 int actualWidth = resources.getDimensionPixelSize(R.dimen.workspace_cell_width);
1233 int actualHeight = resources.getDimensionPixelSize(R.dimen.workspace_cell_height);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001234 int smallerSize = Math.min(actualWidth, actualHeight);
Joe Onorato79e56262009-09-21 15:23:04 -04001235
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001236 // Always round up to next largest cell
1237 int spanX = (width + smallerSize) / smallerSize;
1238 int spanY = (height + smallerSize) / smallerSize;
Joe Onorato79e56262009-09-21 15:23:04 -04001239
Patrick Dubroy8f86ddc2010-07-16 13:55:32 -07001240 if (result == null) {
1241 return new int[] { spanX, spanY };
1242 }
1243 result[0] = spanX;
1244 result[1] = spanY;
1245 return result;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001246 }
1247
1248 /**
Patrick Dubroy047379a2010-12-19 22:02:04 -08001249 * Calculate the grid spans needed to fit given item
1250 */
1251 public void calculateSpans(ItemInfo info) {
1252 final int minWidth;
1253 final int minHeight;
1254
1255 if (info instanceof LauncherAppWidgetInfo) {
1256 minWidth = ((LauncherAppWidgetInfo) info).minWidth;
1257 minHeight = ((LauncherAppWidgetInfo) info).minHeight;
1258 } else if (info instanceof PendingAddWidgetInfo) {
1259 minWidth = ((PendingAddWidgetInfo) info).minWidth;
1260 minHeight = ((PendingAddWidgetInfo) info).minHeight;
1261 } else {
1262 // It's not a widget, so it must be 1x1
1263 info.spanX = info.spanY = 1;
1264 return;
1265 }
1266 int[] spans = rectToCell(minWidth, minHeight, null);
1267 info.spanX = spans[0];
1268 info.spanY = spans[1];
1269 }
1270
1271 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001272 * Find the first vacant cell, if there is one.
1273 *
1274 * @param vacant Holds the x and y coordinate of the vacant cell
1275 * @param spanX Horizontal cell span.
1276 * @param spanY Vertical cell span.
Winson Chungaafa03c2010-06-11 17:34:16 -07001277 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001278 * @return True if a vacant cell was found
1279 */
1280 public boolean getVacantCell(int[] vacant, int spanX, int spanY) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001281
Michael Jurka0280c3b2010-09-17 15:00:07 -07001282 return findVacantCell(vacant, spanX, spanY, mCountX, mCountY, mOccupied);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001283 }
1284
1285 static boolean findVacantCell(int[] vacant, int spanX, int spanY,
1286 int xCount, int yCount, boolean[][] occupied) {
1287
1288 for (int x = 0; x < xCount; x++) {
1289 for (int y = 0; y < yCount; y++) {
1290 boolean available = !occupied[x][y];
1291out: for (int i = x; i < x + spanX - 1 && x < xCount; i++) {
1292 for (int j = y; j < y + spanY - 1 && y < yCount; j++) {
1293 available = available && !occupied[i][j];
1294 if (!available) break out;
1295 }
1296 }
1297
1298 if (available) {
1299 vacant[0] = x;
1300 vacant[1] = y;
1301 return true;
1302 }
1303 }
1304 }
1305
1306 return false;
1307 }
1308
Michael Jurka0280c3b2010-09-17 15:00:07 -07001309 private void clearOccupiedCells() {
1310 for (int x = 0; x < mCountX; x++) {
1311 for (int y = 0; y < mCountY; y++) {
1312 mOccupied[x][y] = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001313 }
1314 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001315 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001316
Michael Jurka0280c3b2010-09-17 15:00:07 -07001317 public void onMove(View view, int newCellX, int newCellY) {
1318 LayoutParams lp = (LayoutParams) view.getLayoutParams();
1319 markCellsAsUnoccupiedForView(view);
1320 markCellsForView(newCellX, newCellY, lp.cellHSpan, lp.cellVSpan, true);
1321 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001322
Michael Jurka0280c3b2010-09-17 15:00:07 -07001323 private void markCellsAsOccupiedForView(View view) {
Michael Jurka8c920dd2011-01-20 14:16:56 -08001324 if (view == null || view.getParent() != mChildren) return;
Michael Jurka0280c3b2010-09-17 15:00:07 -07001325 LayoutParams lp = (LayoutParams) view.getLayoutParams();
1326 markCellsForView(lp.cellX, lp.cellY, lp.cellHSpan, lp.cellVSpan, true);
1327 }
1328
1329 private void markCellsAsUnoccupiedForView(View view) {
Michael Jurka8c920dd2011-01-20 14:16:56 -08001330 if (view == null || view.getParent() != mChildren) return;
Michael Jurka0280c3b2010-09-17 15:00:07 -07001331 LayoutParams lp = (LayoutParams) view.getLayoutParams();
1332 markCellsForView(lp.cellX, lp.cellY, lp.cellHSpan, lp.cellVSpan, false);
1333 }
1334
1335 private void markCellsForView(int cellX, int cellY, int spanX, int spanY, boolean value) {
1336 for (int x = cellX; x < cellX + spanX && x < mCountX; x++) {
1337 for (int y = cellY; y < cellY + spanY && y < mCountY; y++) {
1338 mOccupied[x][y] = value;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001339 }
1340 }
1341 }
1342
1343 @Override
1344 public ViewGroup.LayoutParams generateLayoutParams(AttributeSet attrs) {
1345 return new CellLayout.LayoutParams(getContext(), attrs);
1346 }
1347
1348 @Override
1349 protected boolean checkLayoutParams(ViewGroup.LayoutParams p) {
1350 return p instanceof CellLayout.LayoutParams;
1351 }
1352
1353 @Override
1354 protected ViewGroup.LayoutParams generateLayoutParams(ViewGroup.LayoutParams p) {
1355 return new CellLayout.LayoutParams(p);
1356 }
1357
Winson Chungaafa03c2010-06-11 17:34:16 -07001358 public static class CellLayoutAnimationController extends LayoutAnimationController {
1359 public CellLayoutAnimationController(Animation animation, float delay) {
1360 super(animation, delay);
1361 }
1362
1363 @Override
1364 protected long getDelayForView(View view) {
1365 return (int) (Math.random() * 150);
1366 }
1367 }
1368
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001369 public static class LayoutParams extends ViewGroup.MarginLayoutParams {
1370 /**
1371 * Horizontal location of the item in the grid.
1372 */
1373 @ViewDebug.ExportedProperty
1374 public int cellX;
1375
1376 /**
1377 * Vertical location of the item in the grid.
1378 */
1379 @ViewDebug.ExportedProperty
1380 public int cellY;
1381
1382 /**
1383 * Number of cells spanned horizontally by the item.
1384 */
1385 @ViewDebug.ExportedProperty
1386 public int cellHSpan;
1387
1388 /**
1389 * Number of cells spanned vertically by the item.
1390 */
1391 @ViewDebug.ExportedProperty
1392 public int cellVSpan;
Winson Chungaafa03c2010-06-11 17:34:16 -07001393
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001394 /**
1395 * Is this item currently being dragged
1396 */
1397 public boolean isDragging;
1398
1399 // X coordinate of the view in the layout.
1400 @ViewDebug.ExportedProperty
1401 int x;
1402 // Y coordinate of the view in the layout.
1403 @ViewDebug.ExportedProperty
1404 int y;
1405
Patrick Dubroyce34a972010-10-19 10:34:32 -07001406 /**
1407 * The old X coordinate of this item, relative to its current parent.
1408 * Used to animate the item into its new position.
1409 */
1410 int oldX;
1411
1412 /**
1413 * The old Y coordinate of this item, relative to its current parent.
1414 * Used to animate the item into its new position.
1415 */
1416 int oldY;
1417
Romain Guy84f296c2009-11-04 15:00:44 -08001418 boolean dropped;
Romain Guyfcb9e712009-10-02 16:06:52 -07001419
Michael Jurkad3ef3062010-11-23 16:23:58 -08001420 boolean animateDrop;
1421
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001422 public LayoutParams(Context c, AttributeSet attrs) {
1423 super(c, attrs);
1424 cellHSpan = 1;
1425 cellVSpan = 1;
1426 }
1427
1428 public LayoutParams(ViewGroup.LayoutParams source) {
1429 super(source);
1430 cellHSpan = 1;
1431 cellVSpan = 1;
1432 }
Winson Chungaafa03c2010-06-11 17:34:16 -07001433
1434 public LayoutParams(LayoutParams source) {
1435 super(source);
1436 this.cellX = source.cellX;
1437 this.cellY = source.cellY;
1438 this.cellHSpan = source.cellHSpan;
1439 this.cellVSpan = source.cellVSpan;
1440 }
1441
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001442 public LayoutParams(int cellX, int cellY, int cellHSpan, int cellVSpan) {
Romain Guy8f19cdd2010-01-08 15:07:00 -08001443 super(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001444 this.cellX = cellX;
1445 this.cellY = cellY;
1446 this.cellHSpan = cellHSpan;
1447 this.cellVSpan = cellVSpan;
1448 }
1449
1450 public void setup(int cellWidth, int cellHeight, int widthGap, int heightGap,
1451 int hStartPadding, int vStartPadding) {
Winson Chungaafa03c2010-06-11 17:34:16 -07001452
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001453 final int myCellHSpan = cellHSpan;
1454 final int myCellVSpan = cellVSpan;
1455 final int myCellX = cellX;
1456 final int myCellY = cellY;
Winson Chungaafa03c2010-06-11 17:34:16 -07001457
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001458 width = myCellHSpan * cellWidth + ((myCellHSpan - 1) * widthGap) -
1459 leftMargin - rightMargin;
1460 height = myCellVSpan * cellHeight + ((myCellVSpan - 1) * heightGap) -
1461 topMargin - bottomMargin;
1462
1463 x = hStartPadding + myCellX * (cellWidth + widthGap) + leftMargin;
1464 y = vStartPadding + myCellY * (cellHeight + heightGap) + topMargin;
1465 }
Winson Chungaafa03c2010-06-11 17:34:16 -07001466
1467 public String toString() {
1468 return "(" + this.cellX + ", " + this.cellY + ")";
1469 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001470 }
1471
Michael Jurka0280c3b2010-09-17 15:00:07 -07001472 // This class stores info for two purposes:
1473 // 1. When dragging items (mDragInfo in Workspace), we store the View, its cellX & cellY,
1474 // its spanX, spanY, and the screen it is on
1475 // 2. When long clicking on an empty cell in a CellLayout, we save information about the
1476 // cellX and cellY coordinates and which page was clicked. We then set this as a tag on
1477 // the CellLayout that was long clicked
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001478 static final class CellInfo implements ContextMenu.ContextMenuInfo {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001479 View cell;
Michael Jurkaa63c4522010-08-19 13:52:27 -07001480 int cellX = -1;
1481 int cellY = -1;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001482 int spanX;
1483 int spanY;
1484 int screen;
1485 boolean valid;
1486
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001487 @Override
1488 public String toString() {
Winson Chungaafa03c2010-06-11 17:34:16 -07001489 return "Cell[view=" + (cell == null ? "null" : cell.getClass())
1490 + ", x=" + cellX + ", y=" + cellY + "]";
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001491 }
1492 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001493}