blob: 086f2b83e4e5eced449952025532d3bf52a4572f [file] [log] [blame]
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001/*
2 * Copyright (C) 2008 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Joe Onoratoa5902522009-07-30 13:37:37 -070017package com.android.launcher2;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080018
Michael Jurka3c4c20f2010-10-28 15:36:06 -070019import com.android.launcher.R;
Winson Chungaafa03c2010-06-11 17:34:16 -070020
Joe Onorato4be866d2010-10-10 11:26:02 -070021import android.animation.Animator;
22import android.animation.AnimatorListenerAdapter;
Michael Jurka18014792010-10-14 09:01:34 -070023import android.animation.AnimatorSet;
24import android.animation.ObjectAnimator;
Chet Haase00397b12010-10-07 11:13:10 -070025import android.animation.TimeInterpolator;
Patrick Dubroyde7658b2010-09-27 11:15:43 -070026import android.animation.ValueAnimator;
27import android.animation.ValueAnimator.AnimatorUpdateListener;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080028import android.content.Context;
Joe Onorato79e56262009-09-21 15:23:04 -040029import android.content.res.Resources;
Winson Chungaafa03c2010-06-11 17:34:16 -070030import android.content.res.TypedArray;
Joe Onorato4be866d2010-10-10 11:26:02 -070031import android.graphics.Bitmap;
Winson Chungaafa03c2010-06-11 17:34:16 -070032import android.graphics.Canvas;
Joe Onorato4be866d2010-10-10 11:26:02 -070033import android.graphics.Paint;
Patrick Dubroyde7658b2010-09-27 11:15:43 -070034import android.graphics.Point;
35import android.graphics.PointF;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080036import android.graphics.Rect;
37import android.graphics.RectF;
Michael Jurka18014792010-10-14 09:01:34 -070038import android.graphics.Region;
Patrick Dubroy6569f2c2010-07-12 14:25:18 -070039import android.graphics.drawable.Drawable;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080040import android.util.AttributeSet;
Joe Onorato4be866d2010-10-10 11:26:02 -070041import android.util.Log;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080042import android.view.ContextMenu;
43import android.view.MotionEvent;
44import android.view.View;
45import android.view.ViewDebug;
46import android.view.ViewGroup;
Winson Chungaafa03c2010-06-11 17:34:16 -070047import android.view.animation.Animation;
Winson Chung150fbab2010-09-29 17:14:26 -070048import android.view.animation.DecelerateInterpolator;
Winson Chungaafa03c2010-06-11 17:34:16 -070049import android.view.animation.LayoutAnimationController;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080050
Michael Jurka3c4c20f2010-10-28 15:36:06 -070051import java.util.Arrays;
Patrick Dubroy8e58e912010-10-14 13:21:48 -070052
Michael Jurka8c920dd2011-01-20 14:16:56 -080053public class CellLayout extends ViewGroup {
Winson Chungaafa03c2010-06-11 17:34:16 -070054 static final String TAG = "CellLayout";
55
The Android Open Source Project31dd5032009-03-03 19:32:27 -080056 private int mCellWidth;
57 private int mCellHeight;
Winson Chungaafa03c2010-06-11 17:34:16 -070058
Winson Chungaafa03c2010-06-11 17:34:16 -070059 private int mLeftPadding;
60 private int mRightPadding;
61 private int mTopPadding;
62 private int mBottomPadding;
63
Adam Cohend22015c2010-07-26 22:02:18 -070064 private int mCountX;
65 private int mCountY;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080066
67 private int mWidthGap;
68 private int mHeightGap;
69
70 private final Rect mRect = new Rect();
71 private final CellInfo mCellInfo = new CellInfo();
Winson Chungaafa03c2010-06-11 17:34:16 -070072
Patrick Dubroyde7658b2010-09-27 11:15:43 -070073 // These are temporary variables to prevent having to allocate a new object just to
74 // return an (x, y) value from helper functions. Do NOT use them to maintain other state.
Patrick Dubroy6569f2c2010-07-12 14:25:18 -070075 private final int[] mTmpCellXY = new int[2];
Patrick Dubroyde7658b2010-09-27 11:15:43 -070076 private final int[] mTmpPoint = new int[2];
77 private final PointF mTmpPointF = new PointF();
Patrick Dubroy6569f2c2010-07-12 14:25:18 -070078
The Android Open Source Project31dd5032009-03-03 19:32:27 -080079 boolean[][] mOccupied;
80
Michael Jurkadee05892010-07-27 10:01:56 -070081 private OnTouchListener mInterceptTouchListener;
82
Michael Jurka5f1c5092010-09-03 14:15:02 -070083 private float mBackgroundAlpha;
Adam Cohen1b0aaac2010-10-28 11:11:18 -070084 private float mBackgroundAlphaMultiplier = 1.0f;
Adam Cohenf34bab52010-09-30 14:11:56 -070085
Michael Jurka33945b22010-12-21 18:19:38 -080086 private Drawable mNormalBackground;
Michael Jurka33945b22010-12-21 18:19:38 -080087 private Drawable mActiveBackground;
88 private Drawable mActiveGlowBackground;
89 private Drawable mNormalBackgroundMini;
90 private Drawable mNormalGlowBackgroundMini;
91 private Drawable mActiveBackgroundMini;
92 private Drawable mActiveGlowBackgroundMini;
Michael Jurka18014792010-10-14 09:01:34 -070093 private Rect mBackgroundRect;
Michael Jurka33945b22010-12-21 18:19:38 -080094 private Rect mGlowBackgroundRect;
95 private float mGlowBackgroundScale;
96 private float mGlowBackgroundAlpha;
Patrick Dubroy1262e362010-10-06 15:49:50 -070097
Michael Jurka33945b22010-12-21 18:19:38 -080098 private boolean mAcceptsDrops = false;
99 // If we're actively dragging something over this screen, mIsDragOverlapping is true
100 private boolean mIsDragOverlapping = false;
101 private boolean mIsDragOccuring = false;
102 private boolean mIsDefaultDropTarget = false;
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700103 private final Point mDragCenter = new Point();
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700104
Winson Chung150fbab2010-09-29 17:14:26 -0700105 // These arrays are used to implement the drag visualization on x-large screens.
Joe Onorato4be866d2010-10-10 11:26:02 -0700106 // They are used as circular arrays, indexed by mDragOutlineCurrent.
107 private Point[] mDragOutlines = new Point[8];
Chet Haase472b2812010-10-14 07:02:04 -0700108 private float[] mDragOutlineAlphas = new float[mDragOutlines.length];
Joe Onorato4be866d2010-10-10 11:26:02 -0700109 private InterruptibleInOutAnimator[] mDragOutlineAnims =
110 new InterruptibleInOutAnimator[mDragOutlines.length];
Winson Chung150fbab2010-09-29 17:14:26 -0700111
112 // Used as an index into the above 3 arrays; indicates which is the most current value.
Joe Onorato4be866d2010-10-10 11:26:02 -0700113 private int mDragOutlineCurrent = 0;
Patrick Dubroy8e58e912010-10-14 13:21:48 -0700114 private final Paint mDragOutlinePaint = new Paint();
Winson Chung150fbab2010-09-29 17:14:26 -0700115
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700116 private Drawable mCrosshairsDrawable = null;
Patrick Dubroy49250ad2010-10-08 15:33:52 -0700117 private InterruptibleInOutAnimator mCrosshairsAnimator = null;
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700118 private float mCrosshairsVisibility = 0.0f;
119
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700120 // When a drag operation is in progress, holds the nearest cell to the touch point
121 private final int[] mDragCell = new int[2];
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800122
Joe Onorato4be866d2010-10-10 11:26:02 -0700123 private boolean mDragging = false;
124
Patrick Dubroyce34a972010-10-19 10:34:32 -0700125 private TimeInterpolator mEaseOutInterpolator;
Michael Jurka8c920dd2011-01-20 14:16:56 -0800126 private CellLayoutChildren mChildren;
Patrick Dubroyce34a972010-10-19 10:34:32 -0700127
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800128 public CellLayout(Context context) {
129 this(context, null);
130 }
131
132 public CellLayout(Context context, AttributeSet attrs) {
133 this(context, attrs, 0);
134 }
135
136 public CellLayout(Context context, AttributeSet attrs, int defStyle) {
137 super(context, attrs, defStyle);
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700138
139 // A ViewGroup usually does not draw, but CellLayout needs to draw a rectangle to show
140 // the user where a dragged item will land when dropped.
141 setWillNotDraw(false);
Michael Jurkaa63c4522010-08-19 13:52:27 -0700142
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800143 TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.CellLayout, defStyle, 0);
144
145 mCellWidth = a.getDimensionPixelSize(R.styleable.CellLayout_cellWidth, 10);
146 mCellHeight = a.getDimensionPixelSize(R.styleable.CellLayout_cellHeight, 10);
Winson Chungece7f5b2010-10-22 14:54:12 -0700147 mWidthGap = a.getDimensionPixelSize(R.styleable.CellLayout_widthGap, -1);
148 mHeightGap = a.getDimensionPixelSize(R.styleable.CellLayout_heightGap, -1);
Winson Chungaafa03c2010-06-11 17:34:16 -0700149
Adam Cohend22015c2010-07-26 22:02:18 -0700150 mLeftPadding =
151 a.getDimensionPixelSize(R.styleable.CellLayout_xAxisStartPadding, 10);
152 mRightPadding =
153 a.getDimensionPixelSize(R.styleable.CellLayout_xAxisEndPadding, 10);
154 mTopPadding =
155 a.getDimensionPixelSize(R.styleable.CellLayout_yAxisStartPadding, 10);
156 mBottomPadding =
157 a.getDimensionPixelSize(R.styleable.CellLayout_yAxisEndPadding, 10);
Winson Chungaafa03c2010-06-11 17:34:16 -0700158
Adam Cohend22015c2010-07-26 22:02:18 -0700159 mCountX = LauncherModel.getCellCountX();
160 mCountY = LauncherModel.getCellCountY();
Michael Jurka0280c3b2010-09-17 15:00:07 -0700161 mOccupied = new boolean[mCountX][mCountY];
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800162
163 a.recycle();
164
165 setAlwaysDrawnWithCacheEnabled(false);
166
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700167 final Resources res = getResources();
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700168
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700169 if (LauncherApplication.isScreenXLarge()) {
Michael Jurka33945b22010-12-21 18:19:38 -0800170 mNormalBackground = res.getDrawable(R.drawable.homescreen_large_blue);
Michael Jurka33945b22010-12-21 18:19:38 -0800171 mActiveBackground = res.getDrawable(R.drawable.homescreen_large_green);
172 mActiveGlowBackground = res.getDrawable(R.drawable.homescreen_large_green_strong);
173
174 mNormalBackgroundMini = res.getDrawable(R.drawable.homescreen_small_blue);
175 mNormalGlowBackgroundMini = res.getDrawable(R.drawable.homescreen_small_blue_strong);
176 mActiveBackgroundMini = res.getDrawable(R.drawable.homescreen_small_green);
177 mActiveGlowBackgroundMini = res.getDrawable(R.drawable.homescreen_small_green_strong);
178
179 mNormalBackground.setFilterBitmap(true);
Michael Jurka33945b22010-12-21 18:19:38 -0800180 mActiveBackground.setFilterBitmap(true);
181 mActiveGlowBackground.setFilterBitmap(true);
182 mNormalBackgroundMini.setFilterBitmap(true);
183 mNormalGlowBackgroundMini.setFilterBitmap(true);
184 mActiveBackgroundMini.setFilterBitmap(true);
185 mActiveGlowBackgroundMini.setFilterBitmap(true);
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700186 }
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700187
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700188 // Initialize the data structures used for the drag visualization.
Winson Chung150fbab2010-09-29 17:14:26 -0700189
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700190 mCrosshairsDrawable = res.getDrawable(R.drawable.gardening_crosshairs);
Patrick Dubroyce34a972010-10-19 10:34:32 -0700191 mEaseOutInterpolator = new DecelerateInterpolator(2.5f); // Quint ease out
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700192
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700193 // Set up the animation for fading the crosshairs in and out
194 int animDuration = res.getInteger(R.integer.config_crosshairsFadeInTime);
Patrick Dubroy49250ad2010-10-08 15:33:52 -0700195 mCrosshairsAnimator = new InterruptibleInOutAnimator(animDuration, 0.0f, 1.0f);
Chet Haase472b2812010-10-14 07:02:04 -0700196 mCrosshairsAnimator.getAnimator().addUpdateListener(new AnimatorUpdateListener() {
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700197 public void onAnimationUpdate(ValueAnimator animation) {
198 mCrosshairsVisibility = ((Float) animation.getAnimatedValue()).floatValue();
Patrick Dubroy8e58e912010-10-14 13:21:48 -0700199 invalidate();
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700200 }
201 });
Patrick Dubroyce34a972010-10-19 10:34:32 -0700202 mCrosshairsAnimator.getAnimator().setInterpolator(mEaseOutInterpolator);
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700203
Joe Onorato4be866d2010-10-10 11:26:02 -0700204 for (int i = 0; i < mDragOutlines.length; i++) {
205 mDragOutlines[i] = new Point(-1, -1);
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700206 }
207
208 // When dragging things around the home screens, we show a green outline of
209 // where the item will land. The outlines gradually fade out, leaving a trail
210 // behind the drag path.
211 // Set up all the animations that are used to implement this fading.
212 final int duration = res.getInteger(R.integer.config_dragOutlineFadeTime);
Chet Haase472b2812010-10-14 07:02:04 -0700213 final float fromAlphaValue = 0;
214 final float toAlphaValue = (float)res.getInteger(R.integer.config_dragOutlineMaxAlpha);
Joe Onorato4be866d2010-10-10 11:26:02 -0700215
Patrick Dubroy8e58e912010-10-14 13:21:48 -0700216 Arrays.fill(mDragOutlineAlphas, fromAlphaValue);
Joe Onorato4be866d2010-10-10 11:26:02 -0700217
218 for (int i = 0; i < mDragOutlineAnims.length; i++) {
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700219 final InterruptibleInOutAnimator anim =
220 new InterruptibleInOutAnimator(duration, fromAlphaValue, toAlphaValue);
Patrick Dubroyce34a972010-10-19 10:34:32 -0700221 anim.getAnimator().setInterpolator(mEaseOutInterpolator);
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700222 final int thisIndex = i;
Chet Haase472b2812010-10-14 07:02:04 -0700223 anim.getAnimator().addUpdateListener(new AnimatorUpdateListener() {
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700224 public void onAnimationUpdate(ValueAnimator animation) {
Joe Onorato4be866d2010-10-10 11:26:02 -0700225 final Bitmap outline = (Bitmap)anim.getTag();
226
227 // If an animation is started and then stopped very quickly, we can still
228 // get spurious updates we've cleared the tag. Guard against this.
229 if (outline == null) {
Patrick Dubroyfe6bd872010-10-13 17:32:10 -0700230 if (false) {
231 Object val = animation.getAnimatedValue();
232 Log.d(TAG, "anim " + thisIndex + " update: " + val +
233 ", isStopped " + anim.isStopped());
234 }
Joe Onorato4be866d2010-10-10 11:26:02 -0700235 // Try to prevent it from continuing to run
236 animation.cancel();
237 } else {
Chet Haase472b2812010-10-14 07:02:04 -0700238 mDragOutlineAlphas[thisIndex] = (Float) animation.getAnimatedValue();
Joe Onorato4be866d2010-10-10 11:26:02 -0700239 final int left = mDragOutlines[thisIndex].x;
240 final int top = mDragOutlines[thisIndex].y;
241 CellLayout.this.invalidate(left, top,
242 left + outline.getWidth(), top + outline.getHeight());
243 }
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700244 }
245 });
Joe Onorato4be866d2010-10-10 11:26:02 -0700246 // The animation holds a reference to the drag outline bitmap as long is it's
247 // running. This way the bitmap can be GCed when the animations are complete.
Chet Haase472b2812010-10-14 07:02:04 -0700248 anim.getAnimator().addListener(new AnimatorListenerAdapter() {
Michael Jurka3c4c20f2010-10-28 15:36:06 -0700249 @Override
Joe Onorato4be866d2010-10-10 11:26:02 -0700250 public void onAnimationEnd(Animator animation) {
Chet Haase472b2812010-10-14 07:02:04 -0700251 if ((Float) ((ValueAnimator) animation).getAnimatedValue() == 0f) {
Joe Onorato4be866d2010-10-10 11:26:02 -0700252 anim.setTag(null);
253 }
254 }
255 });
256 mDragOutlineAnims[i] = anim;
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700257 }
Patrick Dubroyce34a972010-10-19 10:34:32 -0700258
Michael Jurka18014792010-10-14 09:01:34 -0700259 mBackgroundRect = new Rect();
Michael Jurka33945b22010-12-21 18:19:38 -0800260 mGlowBackgroundRect = new Rect();
Michael Jurka18014792010-10-14 09:01:34 -0700261 setHoverScale(1.0f);
262 setHoverAlpha(1.0f);
Michael Jurkabea15192010-11-17 12:33:46 -0800263
Michael Jurka8c920dd2011-01-20 14:16:56 -0800264 mChildren = new CellLayoutChildren(context);
265 mChildren.setCellDimensions(
266 mCellWidth, mCellHeight, mLeftPadding, mTopPadding, mWidthGap, mHeightGap);
267 addView(mChildren);
Michael Jurka18014792010-10-14 09:01:34 -0700268 }
269
Michael Jurka33945b22010-12-21 18:19:38 -0800270 public void setIsDefaultDropTarget(boolean isDefaultDropTarget) {
271 if (mIsDefaultDropTarget != isDefaultDropTarget) {
272 mIsDefaultDropTarget = isDefaultDropTarget;
273 invalidate();
274 }
275 }
276
Michael Jurka33945b22010-12-21 18:19:38 -0800277 void setIsDragOccuring(boolean isDragOccuring) {
278 if (mIsDragOccuring != isDragOccuring) {
279 mIsDragOccuring = isDragOccuring;
280 invalidate();
281 }
282 }
283
284 void setIsDragOverlapping(boolean isDragOverlapping) {
285 if (mIsDragOverlapping != isDragOverlapping) {
286 mIsDragOverlapping = isDragOverlapping;
287 invalidate();
288 }
289 }
290
291 boolean getIsDragOverlapping() {
292 return mIsDragOverlapping;
293 }
294
295 private void updateGlowRect() {
296 float marginFraction = (mGlowBackgroundScale - 1.0f) / 2.0f;
Michael Jurka18014792010-10-14 09:01:34 -0700297 int marginX = (int) (marginFraction * (mBackgroundRect.right - mBackgroundRect.left));
298 int marginY = (int) (marginFraction * (mBackgroundRect.bottom - mBackgroundRect.top));
Michael Jurka33945b22010-12-21 18:19:38 -0800299 mGlowBackgroundRect.set(mBackgroundRect.left - marginX, mBackgroundRect.top - marginY,
Michael Jurka18014792010-10-14 09:01:34 -0700300 mBackgroundRect.right + marginX, mBackgroundRect.bottom + marginY);
301 invalidate();
302 }
303
304 public void setHoverScale(float scaleFactor) {
Michael Jurka33945b22010-12-21 18:19:38 -0800305 if (scaleFactor != mGlowBackgroundScale) {
306 mGlowBackgroundScale = scaleFactor;
307 updateGlowRect();
Michael Jurka18014792010-10-14 09:01:34 -0700308 }
309 }
310
311 public float getHoverScale() {
Michael Jurka33945b22010-12-21 18:19:38 -0800312 return mGlowBackgroundScale;
Michael Jurka18014792010-10-14 09:01:34 -0700313 }
314
315 public float getHoverAlpha() {
Michael Jurka33945b22010-12-21 18:19:38 -0800316 return mGlowBackgroundAlpha;
Michael Jurka18014792010-10-14 09:01:34 -0700317 }
318
319 public void setHoverAlpha(float alpha) {
Michael Jurka33945b22010-12-21 18:19:38 -0800320 mGlowBackgroundAlpha = alpha;
Michael Jurka18014792010-10-14 09:01:34 -0700321 invalidate();
322 }
323
324 void animateDrop() {
325 if (LauncherApplication.isScreenXLarge()) {
326 Resources res = getResources();
327 float onDropScale = res.getInteger(R.integer.config_screenOnDropScalePercent) / 100.0f;
328 ObjectAnimator scaleUp = ObjectAnimator.ofFloat(this, "hoverScale", onDropScale);
329 scaleUp.setDuration(res.getInteger(R.integer.config_screenOnDropScaleUpDuration));
330 ObjectAnimator scaleDown = ObjectAnimator.ofFloat(this, "hoverScale", 1.0f);
331 scaleDown.setDuration(res.getInteger(R.integer.config_screenOnDropScaleDownDuration));
332 ObjectAnimator alphaFadeOut = ObjectAnimator.ofFloat(this, "hoverAlpha", 0.0f);
333
334 alphaFadeOut.setStartDelay(res.getInteger(R.integer.config_screenOnDropAlphaFadeDelay));
335 alphaFadeOut.setDuration(res.getInteger(R.integer.config_screenOnDropAlphaFadeDelay));
336
337 AnimatorSet bouncer = new AnimatorSet();
338 bouncer.play(scaleUp).before(scaleDown);
339 bouncer.play(scaleUp).with(alphaFadeOut);
Michael Jurka8edd75c2010-12-17 20:15:06 -0800340 bouncer.addListener(new AnimatorListenerAdapter() {
Michael Jurka3c4c20f2010-10-28 15:36:06 -0700341 @Override
Michael Jurka18014792010-10-14 09:01:34 -0700342 public void onAnimationStart(Animator animation) {
Michael Jurka33945b22010-12-21 18:19:38 -0800343 setIsDragOverlapping(true);
Michael Jurka18014792010-10-14 09:01:34 -0700344 }
Michael Jurka3c4c20f2010-10-28 15:36:06 -0700345 @Override
Michael Jurka8edd75c2010-12-17 20:15:06 -0800346 public void onAnimationEnd(Animator animation) {
Michael Jurka33945b22010-12-21 18:19:38 -0800347 setIsDragOverlapping(false);
Michael Jurka18014792010-10-14 09:01:34 -0700348 setHoverScale(1.0f);
349 setHoverAlpha(1.0f);
350 }
351 });
352 bouncer.start();
353 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800354 }
355
Michael Jurkabea15192010-11-17 12:33:46 -0800356 public void disableCacheUpdates() {
Michael Jurka8c920dd2011-01-20 14:16:56 -0800357 mChildren.disableCacheUpdates();
Michael Jurkabea15192010-11-17 12:33:46 -0800358 }
359
360 public void enableCacheUpdates() {
Michael Jurka8c920dd2011-01-20 14:16:56 -0800361 mChildren.enableCacheUpdates();
Michael Jurkabea15192010-11-17 12:33:46 -0800362 }
363
Jeff Sharkey83f111d2009-04-20 21:03:13 -0700364 @Override
Patrick Dubroy1262e362010-10-06 15:49:50 -0700365 protected void onDraw(Canvas canvas) {
Michael Jurka3e7c7632010-10-02 16:01:03 -0700366 // When we're large, we are either drawn in a "hover" state (ie when dragging an item to
367 // a neighboring page) or with just a normal background (if backgroundAlpha > 0.0f)
368 // When we're small, we are either drawn normally or in the "accepts drops" state (during
369 // a drag). However, we also drag the mini hover background *over* one of those two
370 // backgrounds
Winson Chung26cbf3a2011-01-06 16:25:55 -0800371 if (LauncherApplication.isScreenXLarge() && mBackgroundAlpha > 0.0f) {
Adam Cohenf34bab52010-09-30 14:11:56 -0700372 Drawable bg;
Michael Jurka33945b22010-12-21 18:19:38 -0800373 boolean mini = getScaleX() < 0.5f;
374
375 if (mIsDragOverlapping) {
376 // In the mini case, we draw the active_glow bg *over* the active background
377 bg = mini ? mActiveBackgroundMini : mActiveGlowBackground;
378 } else if (mIsDragOccuring && mAcceptsDrops) {
379 bg = mini ? mActiveBackgroundMini : mActiveBackground;
Adam Cohen3af863b2011-01-25 12:16:51 -0800380 } else if (mIsDefaultDropTarget && mini) {
381 bg = mNormalGlowBackgroundMini;
Adam Cohenf34bab52010-09-30 14:11:56 -0700382 } else {
Michael Jurka33945b22010-12-21 18:19:38 -0800383 bg = mini ? mNormalBackgroundMini : mNormalBackground;
Adam Cohenf34bab52010-09-30 14:11:56 -0700384 }
Michael Jurka33945b22010-12-21 18:19:38 -0800385
386 bg.setAlpha((int) (mBackgroundAlpha * mBackgroundAlphaMultiplier * 255));
387 bg.setBounds(mBackgroundRect);
388 bg.draw(canvas);
389
390 if (mini && mIsDragOverlapping) {
Michael Jurka18014792010-10-14 09:01:34 -0700391 boolean modifiedClipRect = false;
Michael Jurka33945b22010-12-21 18:19:38 -0800392 if (mGlowBackgroundScale > 1.0f) {
Michael Jurka18014792010-10-14 09:01:34 -0700393 // If the hover background's scale is greater than 1, we'll be drawing outside
394 // the bounds of this CellLayout. Get around that by temporarily increasing the
395 // size of the clip rect
Michael Jurka33945b22010-12-21 18:19:38 -0800396 float marginFraction = (mGlowBackgroundScale - 1.0f) / 2.0f;
Michael Jurka18014792010-10-14 09:01:34 -0700397 Rect clipRect = canvas.getClipBounds();
398 int marginX = (int) (marginFraction * (clipRect.right - clipRect.left));
399 int marginY = (int) (marginFraction * (clipRect.bottom - clipRect.top));
400 canvas.save(Canvas.CLIP_SAVE_FLAG);
401 canvas.clipRect(-marginX, -marginY,
402 getWidth() + marginX, getHeight() + marginY, Region.Op.REPLACE);
403 modifiedClipRect = true;
404 }
405
Michael Jurka33945b22010-12-21 18:19:38 -0800406 mActiveGlowBackgroundMini.setAlpha(
407 (int) (mBackgroundAlpha * mGlowBackgroundAlpha * 255));
408 mActiveGlowBackgroundMini.setBounds(mGlowBackgroundRect);
409 mActiveGlowBackgroundMini.draw(canvas);
Michael Jurka18014792010-10-14 09:01:34 -0700410 if (modifiedClipRect) {
411 canvas.restore();
412 }
Michael Jurka3e7c7632010-10-02 16:01:03 -0700413 }
Michael Jurkaa63c4522010-08-19 13:52:27 -0700414 }
Romain Guya6abce82009-11-10 02:54:41 -0800415
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700416 if (mCrosshairsVisibility > 0.0f) {
417 final int countX = mCountX;
418 final int countY = mCountY;
419
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700420 final float MAX_ALPHA = 0.4f;
421 final int MAX_VISIBLE_DISTANCE = 600;
422 final float DISTANCE_MULTIPLIER = 0.002f;
423
424 final Drawable d = mCrosshairsDrawable;
425 final int width = d.getIntrinsicWidth();
426 final int height = d.getIntrinsicHeight();
427
428 int x = getLeftPadding() - (mWidthGap / 2) - (width / 2);
429 for (int col = 0; col <= countX; col++) {
430 int y = getTopPadding() - (mHeightGap / 2) - (height / 2);
431 for (int row = 0; row <= countY; row++) {
432 mTmpPointF.set(x - mDragCenter.x, y - mDragCenter.y);
433 float dist = mTmpPointF.length();
434 // Crosshairs further from the drag point are more faint
435 float alpha = Math.min(MAX_ALPHA,
436 DISTANCE_MULTIPLIER * (MAX_VISIBLE_DISTANCE - dist));
437 if (alpha > 0.0f) {
438 d.setBounds(x, y, x + width, y + height);
439 d.setAlpha((int) (alpha * 255 * mCrosshairsVisibility));
440 d.draw(canvas);
441 }
442 y += mCellHeight + mHeightGap;
443 }
444 x += mCellWidth + mWidthGap;
445 }
Joe Onorato4be866d2010-10-10 11:26:02 -0700446 }
Winson Chung150fbab2010-09-29 17:14:26 -0700447
Patrick Dubroy8e58e912010-10-14 13:21:48 -0700448 final Paint paint = mDragOutlinePaint;
Joe Onorato4be866d2010-10-10 11:26:02 -0700449 for (int i = 0; i < mDragOutlines.length; i++) {
Chet Haase472b2812010-10-14 07:02:04 -0700450 final float alpha = mDragOutlineAlphas[i];
Joe Onorato4be866d2010-10-10 11:26:02 -0700451 if (alpha > 0) {
452 final Point p = mDragOutlines[i];
453 final Bitmap b = (Bitmap) mDragOutlineAnims[i].getTag();
Chet Haase472b2812010-10-14 07:02:04 -0700454 paint.setAlpha((int)(alpha + .5f));
Joe Onorato4be866d2010-10-10 11:26:02 -0700455 canvas.drawBitmap(b, p.x, p.y, paint);
Winson Chung150fbab2010-09-29 17:14:26 -0700456 }
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700457 }
458 }
459
460 @Override
Jeff Sharkey83f111d2009-04-20 21:03:13 -0700461 public void cancelLongPress() {
462 super.cancelLongPress();
463
464 // Cancel long press for all children
465 final int count = getChildCount();
466 for (int i = 0; i < count; i++) {
467 final View child = getChildAt(i);
468 child.cancelLongPress();
469 }
470 }
471
Michael Jurkadee05892010-07-27 10:01:56 -0700472 public void setOnInterceptTouchListener(View.OnTouchListener listener) {
473 mInterceptTouchListener = listener;
474 }
475
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800476 int getCountX() {
Adam Cohend22015c2010-07-26 22:02:18 -0700477 return mCountX;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800478 }
479
480 int getCountY() {
Adam Cohend22015c2010-07-26 22:02:18 -0700481 return mCountY;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800482 }
483
Winson Chungaafa03c2010-06-11 17:34:16 -0700484 public boolean addViewToCellLayout(View child, int index, int childId, LayoutParams params) {
Michael Jurkaf3ca3ab2010-10-20 17:08:24 -0700485 return addViewToCellLayout(child, index, childId, params, true);
486 }
487
488 public boolean addViewToCellLayout(
489 View child, int index, int childId, LayoutParams params, boolean markCells) {
Winson Chungaafa03c2010-06-11 17:34:16 -0700490 final LayoutParams lp = params;
491
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800492 // Generate an id for each view, this assumes we have at most 256x256 cells
493 // per workspace screen
Adam Cohend22015c2010-07-26 22:02:18 -0700494 if (lp.cellX >= 0 && lp.cellX <= mCountX - 1 && lp.cellY >= 0 && lp.cellY <= mCountY - 1) {
Winson Chungaafa03c2010-06-11 17:34:16 -0700495 // If the horizontal or vertical span is set to -1, it is taken to
496 // mean that it spans the extent of the CellLayout
Adam Cohend22015c2010-07-26 22:02:18 -0700497 if (lp.cellHSpan < 0) lp.cellHSpan = mCountX;
498 if (lp.cellVSpan < 0) lp.cellVSpan = mCountY;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800499
Winson Chungaafa03c2010-06-11 17:34:16 -0700500 child.setId(childId);
501
Michael Jurka8c920dd2011-01-20 14:16:56 -0800502 mChildren.addView(child, index, lp);
Michael Jurkadee05892010-07-27 10:01:56 -0700503
Michael Jurkaf3ca3ab2010-10-20 17:08:24 -0700504 if (markCells) markCellsAsOccupiedForView(child);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700505
Winson Chungaafa03c2010-06-11 17:34:16 -0700506 return true;
507 }
508 return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800509 }
Michael Jurka3e7c7632010-10-02 16:01:03 -0700510
Michael Jurkabea15192010-11-17 12:33:46 -0800511 public void setAcceptsDrops(boolean acceptsDrops) {
512 if (mAcceptsDrops != acceptsDrops) {
513 mAcceptsDrops = acceptsDrops;
514 invalidate();
515 }
516 }
517
Michael Jurka3e7c7632010-10-02 16:01:03 -0700518 public boolean getAcceptsDrops() {
519 return mAcceptsDrops;
520 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800521
522 @Override
Michael Jurka0280c3b2010-09-17 15:00:07 -0700523 public void removeAllViews() {
524 clearOccupiedCells();
Michael Jurka8c920dd2011-01-20 14:16:56 -0800525 mChildren.removeAllViews();
Michael Jurka0280c3b2010-09-17 15:00:07 -0700526 }
527
528 @Override
529 public void removeAllViewsInLayout() {
530 clearOccupiedCells();
Michael Jurka8c920dd2011-01-20 14:16:56 -0800531 mChildren.removeAllViewsInLayout();
Michael Jurka0280c3b2010-09-17 15:00:07 -0700532 }
533
Michael Jurkaf3ca3ab2010-10-20 17:08:24 -0700534 public void removeViewWithoutMarkingCells(View view) {
Michael Jurka8c920dd2011-01-20 14:16:56 -0800535 mChildren.removeViewWithoutMarkingCells(view);
Michael Jurkaf3ca3ab2010-10-20 17:08:24 -0700536 }
537
Michael Jurka0280c3b2010-09-17 15:00:07 -0700538 @Override
539 public void removeView(View view) {
540 markCellsAsUnoccupiedForView(view);
Michael Jurka8c920dd2011-01-20 14:16:56 -0800541 mChildren.removeView(view);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700542 }
543
544 @Override
545 public void removeViewAt(int index) {
Michael Jurka8c920dd2011-01-20 14:16:56 -0800546 markCellsAsUnoccupiedForView(mChildren.getChildAt(index));
547 mChildren.removeViewAt(index);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700548 }
549
550 @Override
551 public void removeViewInLayout(View view) {
552 markCellsAsUnoccupiedForView(view);
Michael Jurka8c920dd2011-01-20 14:16:56 -0800553 mChildren.removeViewInLayout(view);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700554 }
555
556 @Override
557 public void removeViews(int start, int count) {
558 for (int i = start; i < start + count; i++) {
Michael Jurka8c920dd2011-01-20 14:16:56 -0800559 markCellsAsUnoccupiedForView(mChildren.getChildAt(i));
Michael Jurka0280c3b2010-09-17 15:00:07 -0700560 }
Michael Jurka8c920dd2011-01-20 14:16:56 -0800561 mChildren.removeViews(start, count);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700562 }
563
564 @Override
565 public void removeViewsInLayout(int start, int count) {
566 for (int i = start; i < start + count; i++) {
Michael Jurka8c920dd2011-01-20 14:16:56 -0800567 markCellsAsUnoccupiedForView(mChildren.getChildAt(i));
Michael Jurka0280c3b2010-09-17 15:00:07 -0700568 }
Michael Jurka8c920dd2011-01-20 14:16:56 -0800569 mChildren.removeViewsInLayout(start, count);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700570 }
571
Michael Jurka8c920dd2011-01-20 14:16:56 -0800572 public void drawChildren(Canvas canvas) {
573 mChildren.draw(canvas);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800574 }
575
576 @Override
577 protected void onAttachedToWindow() {
578 super.onAttachedToWindow();
579 mCellInfo.screen = ((ViewGroup) getParent()).indexOfChild(this);
580 }
581
Michael Jurkaaf442092010-06-10 17:01:57 -0700582 public void setTagToCellInfoForPoint(int touchX, int touchY) {
583 final CellInfo cellInfo = mCellInfo;
584 final Rect frame = mRect;
585 final int x = touchX + mScrollX;
586 final int y = touchY + mScrollY;
Michael Jurka8c920dd2011-01-20 14:16:56 -0800587 final int count = mChildren.getChildCount();
Michael Jurkaaf442092010-06-10 17:01:57 -0700588
589 boolean found = false;
590 for (int i = count - 1; i >= 0; i--) {
Michael Jurka8c920dd2011-01-20 14:16:56 -0800591 final View child = mChildren.getChildAt(i);
Michael Jurkaaf442092010-06-10 17:01:57 -0700592
593 if ((child.getVisibility()) == VISIBLE || child.getAnimation() != null) {
594 child.getHitRect(frame);
595 if (frame.contains(x, y)) {
596 final LayoutParams lp = (LayoutParams) child.getLayoutParams();
597 cellInfo.cell = child;
598 cellInfo.cellX = lp.cellX;
599 cellInfo.cellY = lp.cellY;
600 cellInfo.spanX = lp.cellHSpan;
601 cellInfo.spanY = lp.cellVSpan;
602 cellInfo.valid = true;
603 found = true;
Michael Jurkaaf442092010-06-10 17:01:57 -0700604 break;
605 }
606 }
607 }
Winson Chungaafa03c2010-06-11 17:34:16 -0700608
Michael Jurkaaf442092010-06-10 17:01:57 -0700609 if (!found) {
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700610 final int cellXY[] = mTmpCellXY;
Michael Jurkaaf442092010-06-10 17:01:57 -0700611 pointToCellExact(x, y, cellXY);
612
Michael Jurkaaf442092010-06-10 17:01:57 -0700613 cellInfo.cell = null;
614 cellInfo.cellX = cellXY[0];
615 cellInfo.cellY = cellXY[1];
616 cellInfo.spanX = 1;
617 cellInfo.spanY = 1;
Michael Jurka0280c3b2010-09-17 15:00:07 -0700618 cellInfo.valid = cellXY[0] >= 0 && cellXY[1] >= 0 && cellXY[0] < mCountX &&
619 cellXY[1] < mCountY && !mOccupied[cellXY[0]][cellXY[1]];
Michael Jurkaaf442092010-06-10 17:01:57 -0700620 }
621 setTag(cellInfo);
622 }
623
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800624 @Override
625 public boolean onInterceptTouchEvent(MotionEvent ev) {
Michael Jurkadee05892010-07-27 10:01:56 -0700626 if (mInterceptTouchListener != null && mInterceptTouchListener.onTouch(this, ev)) {
627 return true;
628 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800629 final int action = ev.getAction();
630 final CellInfo cellInfo = mCellInfo;
631
632 if (action == MotionEvent.ACTION_DOWN) {
Michael Jurkaaf442092010-06-10 17:01:57 -0700633 setTagToCellInfoForPoint((int) ev.getX(), (int) ev.getY());
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800634 } else if (action == MotionEvent.ACTION_UP) {
635 cellInfo.cell = null;
636 cellInfo.cellX = -1;
637 cellInfo.cellY = -1;
638 cellInfo.spanX = 0;
639 cellInfo.spanY = 0;
640 cellInfo.valid = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800641 setTag(cellInfo);
642 }
643
644 return false;
645 }
646
647 @Override
648 public CellInfo getTag() {
Michael Jurka0280c3b2010-09-17 15:00:07 -0700649 return (CellInfo) super.getTag();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800650 }
651
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700652 /**
Winson Chungaafa03c2010-06-11 17:34:16 -0700653 * Given a point, return the cell that strictly encloses that point
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800654 * @param x X coordinate of the point
655 * @param y Y coordinate of the point
656 * @param result Array of 2 ints to hold the x and y coordinate of the cell
657 */
658 void pointToCellExact(int x, int y, int[] result) {
Winson Chungaafa03c2010-06-11 17:34:16 -0700659 final int hStartPadding = getLeftPadding();
660 final int vStartPadding = getTopPadding();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800661
662 result[0] = (x - hStartPadding) / (mCellWidth + mWidthGap);
663 result[1] = (y - vStartPadding) / (mCellHeight + mHeightGap);
664
Adam Cohend22015c2010-07-26 22:02:18 -0700665 final int xAxis = mCountX;
666 final int yAxis = mCountY;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800667
668 if (result[0] < 0) result[0] = 0;
669 if (result[0] >= xAxis) result[0] = xAxis - 1;
670 if (result[1] < 0) result[1] = 0;
671 if (result[1] >= yAxis) result[1] = yAxis - 1;
672 }
Winson Chungaafa03c2010-06-11 17:34:16 -0700673
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800674 /**
675 * Given a point, return the cell that most closely encloses that point
676 * @param x X coordinate of the point
677 * @param y Y coordinate of the point
678 * @param result Array of 2 ints to hold the x and y coordinate of the cell
679 */
680 void pointToCellRounded(int x, int y, int[] result) {
681 pointToCellExact(x + (mCellWidth / 2), y + (mCellHeight / 2), result);
682 }
683
684 /**
685 * Given a cell coordinate, return the point that represents the upper left corner of that cell
Winson Chungaafa03c2010-06-11 17:34:16 -0700686 *
687 * @param cellX X coordinate of the cell
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800688 * @param cellY Y coordinate of the cell
Winson Chungaafa03c2010-06-11 17:34:16 -0700689 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800690 * @param result Array of 2 ints to hold the x and y coordinate of the point
691 */
692 void cellToPoint(int cellX, int cellY, int[] result) {
Winson Chungaafa03c2010-06-11 17:34:16 -0700693 final int hStartPadding = getLeftPadding();
694 final int vStartPadding = getTopPadding();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800695
696 result[0] = hStartPadding + cellX * (mCellWidth + mWidthGap);
697 result[1] = vStartPadding + cellY * (mCellHeight + mHeightGap);
698 }
699
Romain Guy84f296c2009-11-04 15:00:44 -0800700 int getCellWidth() {
701 return mCellWidth;
702 }
703
704 int getCellHeight() {
705 return mCellHeight;
706 }
707
Romain Guy1a304a12009-11-10 00:02:32 -0800708 int getLeftPadding() {
Winson Chungaafa03c2010-06-11 17:34:16 -0700709 return mLeftPadding;
Romain Guy1a304a12009-11-10 00:02:32 -0800710 }
711
712 int getTopPadding() {
Winson Chungaafa03c2010-06-11 17:34:16 -0700713 return mTopPadding;
Romain Guy1a304a12009-11-10 00:02:32 -0800714 }
715
716 int getRightPadding() {
Winson Chungaafa03c2010-06-11 17:34:16 -0700717 return mRightPadding;
Romain Guy1a304a12009-11-10 00:02:32 -0800718 }
719
720 int getBottomPadding() {
Winson Chungaafa03c2010-06-11 17:34:16 -0700721 return mBottomPadding;
Romain Guy1a304a12009-11-10 00:02:32 -0800722 }
723
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800724 @Override
725 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
726 // TODO: currently ignoring padding
Winson Chungaafa03c2010-06-11 17:34:16 -0700727
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800728 int widthSpecMode = MeasureSpec.getMode(widthMeasureSpec);
Winson Chungaafa03c2010-06-11 17:34:16 -0700729 int widthSpecSize = MeasureSpec.getSize(widthMeasureSpec);
730
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800731 int heightSpecMode = MeasureSpec.getMode(heightMeasureSpec);
732 int heightSpecSize = MeasureSpec.getSize(heightMeasureSpec);
Winson Chungaafa03c2010-06-11 17:34:16 -0700733
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800734 if (widthSpecMode == MeasureSpec.UNSPECIFIED || heightSpecMode == MeasureSpec.UNSPECIFIED) {
735 throw new RuntimeException("CellLayout cannot have UNSPECIFIED dimensions");
736 }
737
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800738 final int cellWidth = mCellWidth;
739 final int cellHeight = mCellHeight;
740
Adam Cohend22015c2010-07-26 22:02:18 -0700741 int numWidthGaps = mCountX - 1;
742 int numHeightGaps = mCountY - 1;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800743
Winson Chungece7f5b2010-10-22 14:54:12 -0700744 if (mWidthGap < 0 || mHeightGap < 0) {
745 int vSpaceLeft = heightSpecSize - mTopPadding - mBottomPadding - (cellHeight * mCountY);
746 mHeightGap = vSpaceLeft / numHeightGaps;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800747
Winson Chungece7f5b2010-10-22 14:54:12 -0700748 int hSpaceLeft = widthSpecSize - mLeftPadding - mRightPadding - (cellWidth * mCountX);
749 mWidthGap = hSpaceLeft / numWidthGaps;
Winson Chungaafa03c2010-06-11 17:34:16 -0700750
Winson Chungece7f5b2010-10-22 14:54:12 -0700751 // center it around the min gaps
752 int minGap = Math.min(mWidthGap, mHeightGap);
753 mWidthGap = mHeightGap = minGap;
754 }
Michael Jurka5f1c5092010-09-03 14:15:02 -0700755
Michael Jurka8c920dd2011-01-20 14:16:56 -0800756 // Initial values correspond to widthSpecMode == MeasureSpec.EXACTLY
757 int newWidth = widthSpecSize;
758 int newHeight = heightSpecSize;
Michael Jurka5f1c5092010-09-03 14:15:02 -0700759 if (widthSpecMode == MeasureSpec.AT_MOST) {
Michael Jurka8c920dd2011-01-20 14:16:56 -0800760 newWidth = mLeftPadding + mRightPadding + (mCountX * cellWidth) +
Winson Chungece7f5b2010-10-22 14:54:12 -0700761 ((mCountX - 1) * mWidthGap);
Michael Jurka8c920dd2011-01-20 14:16:56 -0800762 newHeight = mTopPadding + mBottomPadding + (mCountY * cellHeight) +
Winson Chungece7f5b2010-10-22 14:54:12 -0700763 ((mCountY - 1) * mHeightGap);
Michael Jurka5f1c5092010-09-03 14:15:02 -0700764 setMeasuredDimension(newWidth, newHeight);
Michael Jurka5f1c5092010-09-03 14:15:02 -0700765 }
Michael Jurka8c920dd2011-01-20 14:16:56 -0800766
767 int count = getChildCount();
768 for (int i = 0; i < count; i++) {
769 View child = getChildAt(i);
770 int childWidthMeasureSpec = MeasureSpec.makeMeasureSpec(newWidth, MeasureSpec.EXACTLY);
771 int childheightMeasureSpec = MeasureSpec.makeMeasureSpec(newHeight,
772 MeasureSpec.EXACTLY);
773 child.measure(childWidthMeasureSpec, childheightMeasureSpec);
774 }
775 setMeasuredDimension(newWidth, newHeight);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800776 }
777
778 @Override
Michael Jurka28750fb2010-09-24 17:43:49 -0700779 protected void onLayout(boolean changed, int l, int t, int r, int b) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800780 int count = getChildCount();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800781 for (int i = 0; i < count; i++) {
Michael Jurka8c920dd2011-01-20 14:16:56 -0800782 View child = getChildAt(i);
783 child.layout(0, 0, r - l, b - t);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800784 }
785 }
786
787 @Override
Michael Jurkadee05892010-07-27 10:01:56 -0700788 protected void onSizeChanged(int w, int h, int oldw, int oldh) {
789 super.onSizeChanged(w, h, oldw, oldh);
Michael Jurka18014792010-10-14 09:01:34 -0700790 mBackgroundRect.set(0, 0, w, h);
Michael Jurka33945b22010-12-21 18:19:38 -0800791 updateGlowRect();
Michael Jurkadee05892010-07-27 10:01:56 -0700792 }
793
794 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800795 protected void setChildrenDrawingCacheEnabled(boolean enabled) {
Michael Jurka8c920dd2011-01-20 14:16:56 -0800796 mChildren.setChildrenDrawingCacheEnabled(enabled);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800797 }
798
799 @Override
800 protected void setChildrenDrawnWithCacheEnabled(boolean enabled) {
Michael Jurka8c920dd2011-01-20 14:16:56 -0800801 mChildren.setChildrenDrawnWithCacheEnabled(enabled);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800802 }
803
Michael Jurka5f1c5092010-09-03 14:15:02 -0700804 public float getBackgroundAlpha() {
805 return mBackgroundAlpha;
Michael Jurkadee05892010-07-27 10:01:56 -0700806 }
807
Adam Cohen1b0aaac2010-10-28 11:11:18 -0700808 public void setBackgroundAlphaMultiplier(float multiplier) {
809 mBackgroundAlphaMultiplier = multiplier;
810 }
811
Adam Cohenddb82192010-11-10 16:32:54 -0800812 public float getBackgroundAlphaMultiplier() {
813 return mBackgroundAlphaMultiplier;
814 }
815
Michael Jurka5f1c5092010-09-03 14:15:02 -0700816 public void setBackgroundAlpha(float alpha) {
817 mBackgroundAlpha = alpha;
Michael Jurka0142d492010-08-25 17:46:15 -0700818 invalidate();
Michael Jurkadee05892010-07-27 10:01:56 -0700819 }
820
Michael Jurka5f1c5092010-09-03 14:15:02 -0700821 // Need to return true to let the view system know we know how to handle alpha-- this is
822 // because when our children have an alpha of 0.0f, they are still rendering their "dimmed"
823 // versions
824 @Override
825 protected boolean onSetAlpha(int alpha) {
826 return true;
827 }
828
829 public void setAlpha(float alpha) {
830 setChildrenAlpha(alpha);
831 super.setAlpha(alpha);
832 }
833
Michael Jurkadee05892010-07-27 10:01:56 -0700834 private void setChildrenAlpha(float alpha) {
Michael Jurka0142d492010-08-25 17:46:15 -0700835 final int childCount = getChildCount();
836 for (int i = 0; i < childCount; i++) {
Michael Jurkadee05892010-07-27 10:01:56 -0700837 getChildAt(i).setAlpha(alpha);
838 }
839 }
840
Patrick Dubroy440c3602010-07-13 17:50:32 -0700841 public View getChildAt(int x, int y) {
Michael Jurka8c920dd2011-01-20 14:16:56 -0800842 return mChildren.getChildAt(x, y);
Patrick Dubroy440c3602010-07-13 17:50:32 -0700843 }
844
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700845 /**
846 * Estimate where the top left cell of the dragged item will land if it is dropped.
847 *
848 * @param originX The X value of the top left corner of the item
849 * @param originY The Y value of the top left corner of the item
850 * @param spanX The number of horizontal cells that the item spans
851 * @param spanY The number of vertical cells that the item spans
852 * @param result The estimated drop cell X and Y.
853 */
854 void estimateDropCell(int originX, int originY, int spanX, int spanY, int[] result) {
Adam Cohend22015c2010-07-26 22:02:18 -0700855 final int countX = mCountX;
856 final int countY = mCountY;
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700857
Michael Jurkaa63c4522010-08-19 13:52:27 -0700858 // pointToCellRounded takes the top left of a cell but will pad that with
859 // cellWidth/2 and cellHeight/2 when finding the matching cell
860 pointToCellRounded(originX, originY, result);
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700861
862 // If the item isn't fully on this screen, snap to the edges
863 int rightOverhang = result[0] + spanX - countX;
864 if (rightOverhang > 0) {
865 result[0] -= rightOverhang; // Snap to right
866 }
867 result[0] = Math.max(0, result[0]); // Snap to left
868 int bottomOverhang = result[1] + spanY - countY;
869 if (bottomOverhang > 0) {
870 result[1] -= bottomOverhang; // Snap to bottom
871 }
872 result[1] = Math.max(0, result[1]); // Snap to top
873 }
874
Joe Onorato4be866d2010-10-10 11:26:02 -0700875 void visualizeDropLocation(
876 View v, Bitmap dragOutline, int originX, int originY, int spanX, int spanY) {
877
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -0700878 final int oldDragCellX = mDragCell[0];
879 final int oldDragCellY = mDragCell[1];
Joe Onorato4be866d2010-10-10 11:26:02 -0700880 final int[] nearest = findNearestVacantArea(originX, originY, spanX, spanY, v, mDragCell);
Winson Chunga9abd0e2010-10-27 17:18:37 -0700881 if (v != null) {
882 mDragCenter.set(originX + (v.getWidth() / 2), originY + (v.getHeight() / 2));
883 } else {
884 mDragCenter.set(originX, originY);
885 }
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700886
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -0700887 if (nearest != null && (nearest[0] != oldDragCellX || nearest[1] != oldDragCellY)) {
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700888 // Find the top left corner of the rect the object will occupy
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700889 final int[] topLeft = mTmpPoint;
890 cellToPoint(nearest[0], nearest[1], topLeft);
891
Joe Onorato4be866d2010-10-10 11:26:02 -0700892 int left = topLeft[0];
893 int top = topLeft[1];
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700894
Winson Chunga9abd0e2010-10-27 17:18:37 -0700895 if (v != null) {
896 if (v.getParent() instanceof CellLayout) {
897 LayoutParams lp = (LayoutParams) v.getLayoutParams();
898 left += lp.leftMargin;
899 top += lp.topMargin;
900 }
Winson Chung150fbab2010-09-29 17:14:26 -0700901
Winson Chunga9abd0e2010-10-27 17:18:37 -0700902 // Offsets due to the size difference between the View and the dragOutline
903 left += (v.getWidth() - dragOutline.getWidth()) / 2;
904 top += (v.getHeight() - dragOutline.getHeight()) / 2;
905 }
Winson Chung150fbab2010-09-29 17:14:26 -0700906
Joe Onorato4be866d2010-10-10 11:26:02 -0700907 final int oldIndex = mDragOutlineCurrent;
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -0700908 mDragOutlineAnims[oldIndex].animateOut();
909 mDragOutlineCurrent = (oldIndex + 1) % mDragOutlines.length;
Winson Chung150fbab2010-09-29 17:14:26 -0700910
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -0700911 mDragOutlines[mDragOutlineCurrent].set(left, top);
912 mDragOutlineAnims[mDragOutlineCurrent].setTag(dragOutline);
913 mDragOutlineAnims[mDragOutlineCurrent].animateIn();
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700914 }
Patrick Dubroy49250ad2010-10-08 15:33:52 -0700915
916 // If we are drawing crosshairs, the entire CellLayout needs to be invalidated
917 if (mCrosshairsDrawable != null) {
918 invalidate();
919 }
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700920 }
921
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800922 /**
Jeff Sharkey70864282009-04-07 21:08:40 -0700923 * Find a vacant area that will fit the given bounds nearest the requested
924 * cell location. Uses Euclidean distance to score multiple vacant areas.
Winson Chungaafa03c2010-06-11 17:34:16 -0700925 *
Romain Guy51afc022009-05-04 18:03:43 -0700926 * @param pixelX The X location at which you want to search for a vacant area.
927 * @param pixelY The Y location at which you want to search for a vacant area.
Jeff Sharkey70864282009-04-07 21:08:40 -0700928 * @param spanX Horizontal span of the object.
929 * @param spanY Vertical span of the object.
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700930 * @param result Array in which to place the result, or null (in which case a new array will
931 * be allocated)
Jeff Sharkey70864282009-04-07 21:08:40 -0700932 * @return The X, Y cell of a vacant area that can contain this object,
933 * nearest the requested location.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800934 */
Michael Jurka6a1435d2010-09-27 17:35:12 -0700935 int[] findNearestVacantArea(
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700936 int pixelX, int pixelY, int spanX, int spanY, int[] result) {
937 return findNearestVacantArea(pixelX, pixelY, spanX, spanY, null, result);
Michael Jurka6a1435d2010-09-27 17:35:12 -0700938 }
Winson Chungaafa03c2010-06-11 17:34:16 -0700939
Michael Jurka6a1435d2010-09-27 17:35:12 -0700940 /**
941 * Find a vacant area that will fit the given bounds nearest the requested
942 * cell location. Uses Euclidean distance to score multiple vacant areas.
943 *
944 * @param pixelX The X location at which you want to search for a vacant area.
945 * @param pixelY The Y location at which you want to search for a vacant area.
946 * @param spanX Horizontal span of the object.
947 * @param spanY Vertical span of the object.
Michael Jurka6a1435d2010-09-27 17:35:12 -0700948 * @param ignoreView Considers space occupied by this view as unoccupied
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700949 * @param result Previously returned value to possibly recycle.
Michael Jurka6a1435d2010-09-27 17:35:12 -0700950 * @return The X, Y cell of a vacant area that can contain this object,
951 * nearest the requested location.
952 */
953 int[] findNearestVacantArea(
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700954 int pixelX, int pixelY, int spanX, int spanY, View ignoreView, int[] result) {
Michael Jurkac6ee42e2010-09-30 12:04:50 -0700955 // mark space take by ignoreView as available (method checks if ignoreView is null)
956 markCellsAsUnoccupiedForView(ignoreView);
957
Jeff Sharkey70864282009-04-07 21:08:40 -0700958 // Keep track of best-scoring drop area
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700959 final int[] bestXY = result != null ? result : new int[2];
Jeff Sharkey70864282009-04-07 21:08:40 -0700960 double bestDistance = Double.MAX_VALUE;
Winson Chungaafa03c2010-06-11 17:34:16 -0700961
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700962 final int countX = mCountX;
963 final int countY = mCountY;
964 final boolean[][] occupied = mOccupied;
965
Winson Chungbbc60d82010-11-11 16:34:41 -0800966 for (int y = 0; y < countY - (spanY - 1); y++) {
Michael Jurkac28de512010-08-13 11:27:44 -0700967 inner:
Winson Chungbbc60d82010-11-11 16:34:41 -0800968 for (int x = 0; x < countX - (spanX - 1); x++) {
Michael Jurkac28de512010-08-13 11:27:44 -0700969 for (int i = 0; i < spanX; i++) {
970 for (int j = 0; j < spanY; j++) {
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700971 if (occupied[x + i][y + j]) {
Winson Chungbbc60d82010-11-11 16:34:41 -0800972 // small optimization: we can skip to after the column we just found
Michael Jurkac28de512010-08-13 11:27:44 -0700973 // an occupied cell
Winson Chungbbc60d82010-11-11 16:34:41 -0800974 x += i;
Michael Jurkac28de512010-08-13 11:27:44 -0700975 continue inner;
976 }
977 }
978 }
979 final int[] cellXY = mTmpCellXY;
980 cellToPoint(x, y, cellXY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800981
Michael Jurkac28de512010-08-13 11:27:44 -0700982 double distance = Math.sqrt(Math.pow(cellXY[0] - pixelX, 2)
983 + Math.pow(cellXY[1] - pixelY, 2));
984 if (distance <= bestDistance) {
985 bestDistance = distance;
986 bestXY[0] = x;
987 bestXY[1] = y;
988 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800989 }
990 }
Michael Jurkac6ee42e2010-09-30 12:04:50 -0700991 // re-mark space taken by ignoreView as occupied
992 markCellsAsOccupiedForView(ignoreView);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800993
Winson Chungaafa03c2010-06-11 17:34:16 -0700994 // Return null if no suitable location found
Jeff Sharkey70864282009-04-07 21:08:40 -0700995 if (bestDistance < Double.MAX_VALUE) {
996 return bestXY;
997 } else {
998 return null;
999 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001000 }
Winson Chungaafa03c2010-06-11 17:34:16 -07001001
Michael Jurka0280c3b2010-09-17 15:00:07 -07001002 boolean existsEmptyCell() {
1003 return findCellForSpan(null, 1, 1);
1004 }
1005
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001006 /**
Michael Jurka0280c3b2010-09-17 15:00:07 -07001007 * Finds the upper-left coordinate of the first rectangle in the grid that can
1008 * hold a cell of the specified dimensions. If intersectX and intersectY are not -1,
1009 * then this method will only return coordinates for rectangles that contain the cell
1010 * (intersectX, intersectY)
1011 *
1012 * @param cellXY The array that will contain the position of a vacant cell if such a cell
1013 * can be found.
1014 * @param spanX The horizontal span of the cell we want to find.
1015 * @param spanY The vertical span of the cell we want to find.
1016 *
1017 * @return True if a vacant cell of the specified dimension was found, false otherwise.
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001018 */
Michael Jurka0280c3b2010-09-17 15:00:07 -07001019 boolean findCellForSpan(int[] cellXY, int spanX, int spanY) {
1020 return findCellForSpanThatIntersectsIgnoring(cellXY, spanX, spanY, -1, -1, null);
1021 }
1022
1023 /**
1024 * Like above, but ignores any cells occupied by the item "ignoreView"
1025 *
1026 * @param cellXY The array that will contain the position of a vacant cell if such a cell
1027 * can be found.
1028 * @param spanX The horizontal span of the cell we want to find.
1029 * @param spanY The vertical span of the cell we want to find.
1030 * @param ignoreView The home screen item we should treat as not occupying any space
1031 * @return
1032 */
1033 boolean findCellForSpanIgnoring(int[] cellXY, int spanX, int spanY, View ignoreView) {
1034 return findCellForSpanThatIntersectsIgnoring(cellXY, spanX, spanY, -1, -1, ignoreView);
1035 }
1036
1037 /**
1038 * Like above, but if intersectX and intersectY are not -1, then this method will try to
1039 * return coordinates for rectangles that contain the cell [intersectX, intersectY]
1040 *
1041 * @param spanX The horizontal span of the cell we want to find.
1042 * @param spanY The vertical span of the cell we want to find.
1043 * @param ignoreView The home screen item we should treat as not occupying any space
1044 * @param intersectX The X coordinate of the cell that we should try to overlap
1045 * @param intersectX The Y coordinate of the cell that we should try to overlap
1046 *
1047 * @return True if a vacant cell of the specified dimension was found, false otherwise.
1048 */
1049 boolean findCellForSpanThatIntersects(int[] cellXY, int spanX, int spanY,
1050 int intersectX, int intersectY) {
1051 return findCellForSpanThatIntersectsIgnoring(
1052 cellXY, spanX, spanY, intersectX, intersectY, null);
1053 }
1054
1055 /**
1056 * The superset of the above two methods
1057 */
1058 boolean findCellForSpanThatIntersectsIgnoring(int[] cellXY, int spanX, int spanY,
1059 int intersectX, int intersectY, View ignoreView) {
Michael Jurkac6ee42e2010-09-30 12:04:50 -07001060 // mark space take by ignoreView as available (method checks if ignoreView is null)
1061 markCellsAsUnoccupiedForView(ignoreView);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001062
Michael Jurka28750fb2010-09-24 17:43:49 -07001063 boolean foundCell = false;
Michael Jurka0280c3b2010-09-17 15:00:07 -07001064 while (true) {
1065 int startX = 0;
1066 if (intersectX >= 0) {
1067 startX = Math.max(startX, intersectX - (spanX - 1));
1068 }
1069 int endX = mCountX - (spanX - 1);
1070 if (intersectX >= 0) {
1071 endX = Math.min(endX, intersectX + (spanX - 1) + (spanX == 1 ? 1 : 0));
1072 }
1073 int startY = 0;
1074 if (intersectY >= 0) {
1075 startY = Math.max(startY, intersectY - (spanY - 1));
1076 }
1077 int endY = mCountY - (spanY - 1);
1078 if (intersectY >= 0) {
1079 endY = Math.min(endY, intersectY + (spanY - 1) + (spanY == 1 ? 1 : 0));
1080 }
1081
Winson Chungbbc60d82010-11-11 16:34:41 -08001082 for (int y = startY; y < endY && !foundCell; y++) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001083 inner:
Winson Chungbbc60d82010-11-11 16:34:41 -08001084 for (int x = startX; x < endX; x++) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001085 for (int i = 0; i < spanX; i++) {
1086 for (int j = 0; j < spanY; j++) {
1087 if (mOccupied[x + i][y + j]) {
Winson Chungbbc60d82010-11-11 16:34:41 -08001088 // small optimization: we can skip to after the column we just found
Michael Jurka0280c3b2010-09-17 15:00:07 -07001089 // an occupied cell
Winson Chungbbc60d82010-11-11 16:34:41 -08001090 x += i;
Michael Jurka0280c3b2010-09-17 15:00:07 -07001091 continue inner;
1092 }
1093 }
1094 }
1095 if (cellXY != null) {
1096 cellXY[0] = x;
1097 cellXY[1] = y;
1098 }
Michael Jurka28750fb2010-09-24 17:43:49 -07001099 foundCell = true;
1100 break;
Michael Jurka0280c3b2010-09-17 15:00:07 -07001101 }
1102 }
1103 if (intersectX == -1 && intersectY == -1) {
1104 break;
1105 } else {
1106 // if we failed to find anything, try again but without any requirements of
1107 // intersecting
1108 intersectX = -1;
1109 intersectY = -1;
1110 continue;
1111 }
1112 }
1113
Michael Jurkac6ee42e2010-09-30 12:04:50 -07001114 // re-mark space taken by ignoreView as occupied
1115 markCellsAsOccupiedForView(ignoreView);
Michael Jurka28750fb2010-09-24 17:43:49 -07001116 return foundCell;
Michael Jurka0280c3b2010-09-17 15:00:07 -07001117 }
1118
1119 /**
1120 * Called when drag has left this CellLayout or has been completed (successfully or not)
1121 */
1122 void onDragExit() {
Joe Onorato4be866d2010-10-10 11:26:02 -07001123 // This can actually be called when we aren't in a drag, e.g. when adding a new
1124 // item to this layout via the customize drawer.
1125 // Guard against that case.
1126 if (mDragging) {
1127 mDragging = false;
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001128
Joe Onorato4be866d2010-10-10 11:26:02 -07001129 // Fade out the drag indicators
1130 if (mCrosshairsAnimator != null) {
1131 mCrosshairsAnimator.animateOut();
1132 }
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001133 }
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -07001134
1135 // Invalidate the drag data
1136 mDragCell[0] = -1;
1137 mDragCell[1] = -1;
1138 mDragOutlineAnims[mDragOutlineCurrent].animateOut();
1139 mDragOutlineCurrent = (mDragOutlineCurrent + 1) % mDragOutlineAnims.length;
1140
Michael Jurka33945b22010-12-21 18:19:38 -08001141 setIsDragOverlapping(false);
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001142 }
1143
1144 /**
Winson Chungaafa03c2010-06-11 17:34:16 -07001145 * Mark a child as having been dropped.
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001146 * At the beginning of the drag operation, the child may have been on another
Patrick Dubroyce34a972010-10-19 10:34:32 -07001147 * screen, but it is re-parented before this method is called.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001148 *
1149 * @param child The child that is being dropped
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001150 */
Michael Jurkad3ef3062010-11-23 16:23:58 -08001151 void onDropChild(View child, boolean animate) {
Romain Guyd94533d2009-08-17 10:01:15 -07001152 if (child != null) {
1153 LayoutParams lp = (LayoutParams) child.getLayoutParams();
Romain Guyd94533d2009-08-17 10:01:15 -07001154 lp.isDragging = false;
Romain Guy84f296c2009-11-04 15:00:44 -08001155 lp.dropped = true;
Michael Jurkad3ef3062010-11-23 16:23:58 -08001156 lp.animateDrop = animate;
Patrick Dubroye3887cc2011-01-20 10:43:40 -08001157 child.setVisibility(animate ? View.INVISIBLE : View.VISIBLE);
Romain Guyd94533d2009-08-17 10:01:15 -07001158 child.requestLayout();
Romain Guyd94533d2009-08-17 10:01:15 -07001159 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001160 }
1161
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001162 /**
1163 * Start dragging the specified child
Winson Chungaafa03c2010-06-11 17:34:16 -07001164 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001165 * @param child The child that is being dragged
1166 */
1167 void onDragChild(View child) {
1168 LayoutParams lp = (LayoutParams) child.getLayoutParams();
1169 lp.isDragging = true;
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001170 }
1171
1172 /**
1173 * A drag event has begun over this layout.
1174 * It may have begun over this layout (in which case onDragChild is called first),
1175 * or it may have begun on another layout.
1176 */
Winson Chunga9abd0e2010-10-27 17:18:37 -07001177 void onDragEnter() {
Patrick Dubroyfe6bd872010-10-13 17:32:10 -07001178 if (!mDragging) {
Patrick Dubroyfe6bd872010-10-13 17:32:10 -07001179 // Fade in the drag indicators
1180 if (mCrosshairsAnimator != null) {
1181 mCrosshairsAnimator.animateIn();
1182 }
Joe Onorato4be866d2010-10-10 11:26:02 -07001183 }
1184 mDragging = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001185 }
Winson Chungaafa03c2010-06-11 17:34:16 -07001186
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001187 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001188 * Computes a bounding rectangle for a range of cells
Winson Chungaafa03c2010-06-11 17:34:16 -07001189 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001190 * @param cellX X coordinate of upper left corner expressed as a cell position
1191 * @param cellY Y coordinate of upper left corner expressed as a cell position
Winson Chungaafa03c2010-06-11 17:34:16 -07001192 * @param cellHSpan Width in cells
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001193 * @param cellVSpan Height in cells
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001194 * @param resultRect Rect into which to put the results
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001195 */
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001196 public void cellToRect(int cellX, int cellY, int cellHSpan, int cellVSpan, RectF resultRect) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001197 final int cellWidth = mCellWidth;
1198 final int cellHeight = mCellHeight;
1199 final int widthGap = mWidthGap;
1200 final int heightGap = mHeightGap;
Winson Chungaafa03c2010-06-11 17:34:16 -07001201
1202 final int hStartPadding = getLeftPadding();
1203 final int vStartPadding = getTopPadding();
1204
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001205 int width = cellHSpan * cellWidth + ((cellHSpan - 1) * widthGap);
1206 int height = cellVSpan * cellHeight + ((cellVSpan - 1) * heightGap);
1207
1208 int x = hStartPadding + cellX * (cellWidth + widthGap);
1209 int y = vStartPadding + cellY * (cellHeight + heightGap);
Winson Chungaafa03c2010-06-11 17:34:16 -07001210
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001211 resultRect.set(x, y, x + width, y + height);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001212 }
Winson Chungaafa03c2010-06-11 17:34:16 -07001213
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001214 /**
Winson Chungaafa03c2010-06-11 17:34:16 -07001215 * Computes the required horizontal and vertical cell spans to always
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001216 * fit the given rectangle.
Winson Chungaafa03c2010-06-11 17:34:16 -07001217 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001218 * @param width Width in pixels
1219 * @param height Height in pixels
Patrick Dubroy8f86ddc2010-07-16 13:55:32 -07001220 * @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 -08001221 */
Patrick Dubroy8f86ddc2010-07-16 13:55:32 -07001222 public int[] rectToCell(int width, int height, int[] result) {
Michael Jurka9987a5c2010-10-08 16:58:12 -07001223 return rectToCell(getResources(), width, height, result);
1224 }
1225
1226 public static int[] rectToCell(Resources resources, int width, int height, int[] result) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001227 // Always assume we're working with the smallest span to make sure we
1228 // reserve enough space in both orientations.
Joe Onorato79e56262009-09-21 15:23:04 -04001229 int actualWidth = resources.getDimensionPixelSize(R.dimen.workspace_cell_width);
1230 int actualHeight = resources.getDimensionPixelSize(R.dimen.workspace_cell_height);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001231 int smallerSize = Math.min(actualWidth, actualHeight);
Joe Onorato79e56262009-09-21 15:23:04 -04001232
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001233 // Always round up to next largest cell
1234 int spanX = (width + smallerSize) / smallerSize;
1235 int spanY = (height + smallerSize) / smallerSize;
Joe Onorato79e56262009-09-21 15:23:04 -04001236
Patrick Dubroy8f86ddc2010-07-16 13:55:32 -07001237 if (result == null) {
1238 return new int[] { spanX, spanY };
1239 }
1240 result[0] = spanX;
1241 result[1] = spanY;
1242 return result;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001243 }
1244
1245 /**
Patrick Dubroy047379a2010-12-19 22:02:04 -08001246 * Calculate the grid spans needed to fit given item
1247 */
1248 public void calculateSpans(ItemInfo info) {
1249 final int minWidth;
1250 final int minHeight;
1251
1252 if (info instanceof LauncherAppWidgetInfo) {
1253 minWidth = ((LauncherAppWidgetInfo) info).minWidth;
1254 minHeight = ((LauncherAppWidgetInfo) info).minHeight;
1255 } else if (info instanceof PendingAddWidgetInfo) {
1256 minWidth = ((PendingAddWidgetInfo) info).minWidth;
1257 minHeight = ((PendingAddWidgetInfo) info).minHeight;
1258 } else {
1259 // It's not a widget, so it must be 1x1
1260 info.spanX = info.spanY = 1;
1261 return;
1262 }
1263 int[] spans = rectToCell(minWidth, minHeight, null);
1264 info.spanX = spans[0];
1265 info.spanY = spans[1];
1266 }
1267
1268 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001269 * Find the first vacant cell, if there is one.
1270 *
1271 * @param vacant Holds the x and y coordinate of the vacant cell
1272 * @param spanX Horizontal cell span.
1273 * @param spanY Vertical cell span.
Winson Chungaafa03c2010-06-11 17:34:16 -07001274 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001275 * @return True if a vacant cell was found
1276 */
1277 public boolean getVacantCell(int[] vacant, int spanX, int spanY) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001278
Michael Jurka0280c3b2010-09-17 15:00:07 -07001279 return findVacantCell(vacant, spanX, spanY, mCountX, mCountY, mOccupied);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001280 }
1281
1282 static boolean findVacantCell(int[] vacant, int spanX, int spanY,
1283 int xCount, int yCount, boolean[][] occupied) {
1284
1285 for (int x = 0; x < xCount; x++) {
1286 for (int y = 0; y < yCount; y++) {
1287 boolean available = !occupied[x][y];
1288out: for (int i = x; i < x + spanX - 1 && x < xCount; i++) {
1289 for (int j = y; j < y + spanY - 1 && y < yCount; j++) {
1290 available = available && !occupied[i][j];
1291 if (!available) break out;
1292 }
1293 }
1294
1295 if (available) {
1296 vacant[0] = x;
1297 vacant[1] = y;
1298 return true;
1299 }
1300 }
1301 }
1302
1303 return false;
1304 }
1305
Michael Jurka0280c3b2010-09-17 15:00:07 -07001306 private void clearOccupiedCells() {
1307 for (int x = 0; x < mCountX; x++) {
1308 for (int y = 0; y < mCountY; y++) {
1309 mOccupied[x][y] = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001310 }
1311 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001312 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001313
Michael Jurka0280c3b2010-09-17 15:00:07 -07001314 public void onMove(View view, int newCellX, int newCellY) {
1315 LayoutParams lp = (LayoutParams) view.getLayoutParams();
1316 markCellsAsUnoccupiedForView(view);
1317 markCellsForView(newCellX, newCellY, lp.cellHSpan, lp.cellVSpan, true);
1318 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001319
Michael Jurka0280c3b2010-09-17 15:00:07 -07001320 private void markCellsAsOccupiedForView(View view) {
Michael Jurka8c920dd2011-01-20 14:16:56 -08001321 if (view == null || view.getParent() != mChildren) return;
Michael Jurka0280c3b2010-09-17 15:00:07 -07001322 LayoutParams lp = (LayoutParams) view.getLayoutParams();
1323 markCellsForView(lp.cellX, lp.cellY, lp.cellHSpan, lp.cellVSpan, true);
1324 }
1325
1326 private void markCellsAsUnoccupiedForView(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, false);
1330 }
1331
1332 private void markCellsForView(int cellX, int cellY, int spanX, int spanY, boolean value) {
1333 for (int x = cellX; x < cellX + spanX && x < mCountX; x++) {
1334 for (int y = cellY; y < cellY + spanY && y < mCountY; y++) {
1335 mOccupied[x][y] = value;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001336 }
1337 }
1338 }
1339
1340 @Override
1341 public ViewGroup.LayoutParams generateLayoutParams(AttributeSet attrs) {
1342 return new CellLayout.LayoutParams(getContext(), attrs);
1343 }
1344
1345 @Override
1346 protected boolean checkLayoutParams(ViewGroup.LayoutParams p) {
1347 return p instanceof CellLayout.LayoutParams;
1348 }
1349
1350 @Override
1351 protected ViewGroup.LayoutParams generateLayoutParams(ViewGroup.LayoutParams p) {
1352 return new CellLayout.LayoutParams(p);
1353 }
1354
Winson Chungaafa03c2010-06-11 17:34:16 -07001355 public static class CellLayoutAnimationController extends LayoutAnimationController {
1356 public CellLayoutAnimationController(Animation animation, float delay) {
1357 super(animation, delay);
1358 }
1359
1360 @Override
1361 protected long getDelayForView(View view) {
1362 return (int) (Math.random() * 150);
1363 }
1364 }
1365
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001366 public static class LayoutParams extends ViewGroup.MarginLayoutParams {
1367 /**
1368 * Horizontal location of the item in the grid.
1369 */
1370 @ViewDebug.ExportedProperty
1371 public int cellX;
1372
1373 /**
1374 * Vertical location of the item in the grid.
1375 */
1376 @ViewDebug.ExportedProperty
1377 public int cellY;
1378
1379 /**
1380 * Number of cells spanned horizontally by the item.
1381 */
1382 @ViewDebug.ExportedProperty
1383 public int cellHSpan;
1384
1385 /**
1386 * Number of cells spanned vertically by the item.
1387 */
1388 @ViewDebug.ExportedProperty
1389 public int cellVSpan;
Winson Chungaafa03c2010-06-11 17:34:16 -07001390
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001391 /**
1392 * Is this item currently being dragged
1393 */
1394 public boolean isDragging;
1395
1396 // X coordinate of the view in the layout.
1397 @ViewDebug.ExportedProperty
1398 int x;
1399 // Y coordinate of the view in the layout.
1400 @ViewDebug.ExportedProperty
1401 int y;
1402
Patrick Dubroyce34a972010-10-19 10:34:32 -07001403 /**
1404 * The old X coordinate of this item, relative to its current parent.
1405 * Used to animate the item into its new position.
1406 */
1407 int oldX;
1408
1409 /**
1410 * The old Y coordinate of this item, relative to its current parent.
1411 * Used to animate the item into its new position.
1412 */
1413 int oldY;
1414
Romain Guy84f296c2009-11-04 15:00:44 -08001415 boolean dropped;
Romain Guyfcb9e712009-10-02 16:06:52 -07001416
Michael Jurkad3ef3062010-11-23 16:23:58 -08001417 boolean animateDrop;
1418
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001419 public LayoutParams(Context c, AttributeSet attrs) {
1420 super(c, attrs);
1421 cellHSpan = 1;
1422 cellVSpan = 1;
1423 }
1424
1425 public LayoutParams(ViewGroup.LayoutParams source) {
1426 super(source);
1427 cellHSpan = 1;
1428 cellVSpan = 1;
1429 }
Winson Chungaafa03c2010-06-11 17:34:16 -07001430
1431 public LayoutParams(LayoutParams source) {
1432 super(source);
1433 this.cellX = source.cellX;
1434 this.cellY = source.cellY;
1435 this.cellHSpan = source.cellHSpan;
1436 this.cellVSpan = source.cellVSpan;
1437 }
1438
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001439 public LayoutParams(int cellX, int cellY, int cellHSpan, int cellVSpan) {
Romain Guy8f19cdd2010-01-08 15:07:00 -08001440 super(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001441 this.cellX = cellX;
1442 this.cellY = cellY;
1443 this.cellHSpan = cellHSpan;
1444 this.cellVSpan = cellVSpan;
1445 }
1446
1447 public void setup(int cellWidth, int cellHeight, int widthGap, int heightGap,
1448 int hStartPadding, int vStartPadding) {
Winson Chungaafa03c2010-06-11 17:34:16 -07001449
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001450 final int myCellHSpan = cellHSpan;
1451 final int myCellVSpan = cellVSpan;
1452 final int myCellX = cellX;
1453 final int myCellY = cellY;
Winson Chungaafa03c2010-06-11 17:34:16 -07001454
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001455 width = myCellHSpan * cellWidth + ((myCellHSpan - 1) * widthGap) -
1456 leftMargin - rightMargin;
1457 height = myCellVSpan * cellHeight + ((myCellVSpan - 1) * heightGap) -
1458 topMargin - bottomMargin;
1459
1460 x = hStartPadding + myCellX * (cellWidth + widthGap) + leftMargin;
1461 y = vStartPadding + myCellY * (cellHeight + heightGap) + topMargin;
1462 }
Winson Chungaafa03c2010-06-11 17:34:16 -07001463
1464 public String toString() {
1465 return "(" + this.cellX + ", " + this.cellY + ")";
1466 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001467 }
1468
Michael Jurka0280c3b2010-09-17 15:00:07 -07001469 // This class stores info for two purposes:
1470 // 1. When dragging items (mDragInfo in Workspace), we store the View, its cellX & cellY,
1471 // its spanX, spanY, and the screen it is on
1472 // 2. When long clicking on an empty cell in a CellLayout, we save information about the
1473 // cellX and cellY coordinates and which page was clicked. We then set this as a tag on
1474 // the CellLayout that was long clicked
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001475 static final class CellInfo implements ContextMenu.ContextMenuInfo {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001476 View cell;
Michael Jurkaa63c4522010-08-19 13:52:27 -07001477 int cellX = -1;
1478 int cellY = -1;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001479 int spanX;
1480 int spanY;
1481 int screen;
1482 boolean valid;
1483
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001484 @Override
1485 public String toString() {
Winson Chungaafa03c2010-06-11 17:34:16 -07001486 return "Cell[view=" + (cell == null ? "null" : cell.getClass())
1487 + ", x=" + cellX + ", y=" + cellY + "]";
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001488 }
1489 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001490}