blob: e5aa7a96a2d3e9cd1c1b86b3172ba8d8df9b30b6 [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
Joe Onorato4be866d2010-10-10 11:26:02 -070019import android.animation.Animator;
20import android.animation.AnimatorListenerAdapter;
Michael Jurka18014792010-10-14 09:01:34 -070021import android.animation.AnimatorSet;
22import android.animation.ObjectAnimator;
Chet Haase00397b12010-10-07 11:13:10 -070023import android.animation.TimeInterpolator;
Patrick Dubroyde7658b2010-09-27 11:15:43 -070024import android.animation.ValueAnimator;
25import android.animation.ValueAnimator.AnimatorUpdateListener;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080026import android.content.Context;
Joe Onorato79e56262009-09-21 15:23:04 -040027import android.content.res.Resources;
Winson Chungaafa03c2010-06-11 17:34:16 -070028import android.content.res.TypedArray;
Joe Onorato4be866d2010-10-10 11:26:02 -070029import android.graphics.Bitmap;
Winson Chungaafa03c2010-06-11 17:34:16 -070030import android.graphics.Canvas;
Joe Onorato4be866d2010-10-10 11:26:02 -070031import android.graphics.Paint;
Patrick Dubroyde7658b2010-09-27 11:15:43 -070032import android.graphics.Point;
33import android.graphics.PointF;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080034import android.graphics.Rect;
35import android.graphics.RectF;
Michael Jurka18014792010-10-14 09:01:34 -070036import android.graphics.Region;
Patrick Dubroy6569f2c2010-07-12 14:25:18 -070037import android.graphics.drawable.Drawable;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080038import android.util.AttributeSet;
Joe Onorato4be866d2010-10-10 11:26:02 -070039import android.util.Log;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080040import android.view.MotionEvent;
41import android.view.View;
42import android.view.ViewDebug;
43import android.view.ViewGroup;
Winson Chungaafa03c2010-06-11 17:34:16 -070044import android.view.animation.Animation;
Winson Chung150fbab2010-09-29 17:14:26 -070045import android.view.animation.DecelerateInterpolator;
Winson Chungaafa03c2010-06-11 17:34:16 -070046import android.view.animation.LayoutAnimationController;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080047
Adam Cohen66396872011-04-15 17:50:36 -070048import com.android.launcher.R;
Patrick Dubroy8e58e912010-10-14 13:21:48 -070049
Adam Cohenc0dcf592011-06-01 15:30:43 -070050import java.util.Arrays;
51
Michael Jurkabdb5c532011-02-01 15:05:06 -080052public class CellLayout extends ViewGroup {
Winson Chungaafa03c2010-06-11 17:34:16 -070053 static final String TAG = "CellLayout";
54
The Android Open Source Project31dd5032009-03-03 19:32:27 -080055 private int mCellWidth;
56 private int mCellHeight;
Winson Chungaafa03c2010-06-11 17:34:16 -070057
Winson Chungaafa03c2010-06-11 17:34:16 -070058 private int mLeftPadding;
59 private int mRightPadding;
60 private int mTopPadding;
61 private int mBottomPadding;
62
Adam Cohend22015c2010-07-26 22:02:18 -070063 private int mCountX;
64 private int mCountY;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080065
66 private int mWidthGap;
67 private int mHeightGap;
68
69 private final Rect mRect = new Rect();
70 private final CellInfo mCellInfo = new CellInfo();
Winson Chungaafa03c2010-06-11 17:34:16 -070071
Patrick Dubroyde7658b2010-09-27 11:15:43 -070072 // These are temporary variables to prevent having to allocate a new object just to
73 // return an (x, y) value from helper functions. Do NOT use them to maintain other state.
Winson Chung0be025d2011-05-23 17:45:09 -070074 private final int[] mTmpXY = new int[2];
Patrick Dubroyde7658b2010-09-27 11:15:43 -070075 private final int[] mTmpPoint = new int[2];
76 private final PointF mTmpPointF = new PointF();
Patrick Dubroy6569f2c2010-07-12 14:25:18 -070077
The Android Open Source Project31dd5032009-03-03 19:32:27 -080078 boolean[][] mOccupied;
79
Michael Jurkadee05892010-07-27 10:01:56 -070080 private OnTouchListener mInterceptTouchListener;
81
Michael Jurka5f1c5092010-09-03 14:15:02 -070082 private float mBackgroundAlpha;
Adam Cohen1b0aaac2010-10-28 11:11:18 -070083 private float mBackgroundAlphaMultiplier = 1.0f;
Adam Cohenf34bab52010-09-30 14:11:56 -070084
Michael Jurka33945b22010-12-21 18:19:38 -080085 private Drawable mNormalBackground;
Michael Jurka33945b22010-12-21 18:19:38 -080086 private Drawable mActiveBackground;
87 private Drawable mActiveGlowBackground;
88 private Drawable mNormalBackgroundMini;
89 private Drawable mNormalGlowBackgroundMini;
90 private Drawable mActiveBackgroundMini;
91 private Drawable mActiveGlowBackgroundMini;
Michael Jurka18014792010-10-14 09:01:34 -070092 private Rect mBackgroundRect;
Michael Jurka33945b22010-12-21 18:19:38 -080093 private Rect mGlowBackgroundRect;
94 private float mGlowBackgroundScale;
95 private float mGlowBackgroundAlpha;
Patrick Dubroy1262e362010-10-06 15:49:50 -070096
Adam Cohendf035382011-04-11 17:22:04 -070097 private boolean mAcceptsDrops = true;
Michael Jurka33945b22010-12-21 18:19:38 -080098 // If we're actively dragging something over this screen, mIsDragOverlapping is true
99 private boolean mIsDragOverlapping = false;
100 private boolean mIsDragOccuring = false;
101 private boolean mIsDefaultDropTarget = false;
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700102 private final Point mDragCenter = new Point();
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700103
Winson Chung150fbab2010-09-29 17:14:26 -0700104 // These arrays are used to implement the drag visualization on x-large screens.
Joe Onorato4be866d2010-10-10 11:26:02 -0700105 // They are used as circular arrays, indexed by mDragOutlineCurrent.
Winson Chung63257c12011-05-05 17:06:13 -0700106 private Point[] mDragOutlines = new Point[4];
Chet Haase472b2812010-10-14 07:02:04 -0700107 private float[] mDragOutlineAlphas = new float[mDragOutlines.length];
Joe Onorato4be866d2010-10-10 11:26:02 -0700108 private InterruptibleInOutAnimator[] mDragOutlineAnims =
109 new InterruptibleInOutAnimator[mDragOutlines.length];
Winson Chung150fbab2010-09-29 17:14:26 -0700110
111 // Used as an index into the above 3 arrays; indicates which is the most current value.
Joe Onorato4be866d2010-10-10 11:26:02 -0700112 private int mDragOutlineCurrent = 0;
Patrick Dubroy8e58e912010-10-14 13:21:48 -0700113 private final Paint mDragOutlinePaint = new Paint();
Winson Chung150fbab2010-09-29 17:14:26 -0700114
Patrick Dubroy96864c32011-03-10 17:17:23 -0800115 private BubbleTextView mPressedOrFocusedIcon;
116
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
Winson Chungb26f3d62011-06-02 10:49:29 -0700170 mNormalBackground = res.getDrawable(R.drawable.homescreen_large_blue);
171 mActiveBackground = res.getDrawable(R.drawable.homescreen_large_green);
172 mActiveGlowBackground = res.getDrawable(R.drawable.homescreen_large_green_strong);
Michael Jurka33945b22010-12-21 18:19:38 -0800173
Winson Chungb26f3d62011-06-02 10:49:29 -0700174 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);
Michael Jurka33945b22010-12-21 18:19:38 -0800178
Winson Chungb26f3d62011-06-02 10:49:29 -0700179 mNormalBackground.setFilterBitmap(true);
180 mActiveBackground.setFilterBitmap(true);
181 mActiveGlowBackground.setFilterBitmap(true);
182 mNormalBackgroundMini.setFilterBitmap(true);
183 mNormalGlowBackgroundMini.setFilterBitmap(true);
184 mActiveBackgroundMini.setFilterBitmap(true);
185 mActiveGlowBackgroundMini.setFilterBitmap(true);
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700186
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700187 // Initialize the data structures used for the drag visualization.
Winson Chung150fbab2010-09-29 17:14:26 -0700188
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700189 mCrosshairsDrawable = res.getDrawable(R.drawable.gardening_crosshairs);
Patrick Dubroyce34a972010-10-19 10:34:32 -0700190 mEaseOutInterpolator = new DecelerateInterpolator(2.5f); // Quint ease out
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700191
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700192 // Set up the animation for fading the crosshairs in and out
193 int animDuration = res.getInteger(R.integer.config_crosshairsFadeInTime);
Patrick Dubroy49250ad2010-10-08 15:33:52 -0700194 mCrosshairsAnimator = new InterruptibleInOutAnimator(animDuration, 0.0f, 1.0f);
Chet Haase472b2812010-10-14 07:02:04 -0700195 mCrosshairsAnimator.getAnimator().addUpdateListener(new AnimatorUpdateListener() {
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700196 public void onAnimationUpdate(ValueAnimator animation) {
197 mCrosshairsVisibility = ((Float) animation.getAnimatedValue()).floatValue();
Patrick Dubroy8e58e912010-10-14 13:21:48 -0700198 invalidate();
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700199 }
200 });
Patrick Dubroyce34a972010-10-19 10:34:32 -0700201 mCrosshairsAnimator.getAnimator().setInterpolator(mEaseOutInterpolator);
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700202
Joe Onorato4be866d2010-10-10 11:26:02 -0700203 for (int i = 0; i < mDragOutlines.length; i++) {
204 mDragOutlines[i] = new Point(-1, -1);
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700205 }
206
207 // When dragging things around the home screens, we show a green outline of
208 // where the item will land. The outlines gradually fade out, leaving a trail
209 // behind the drag path.
210 // Set up all the animations that are used to implement this fading.
211 final int duration = res.getInteger(R.integer.config_dragOutlineFadeTime);
Chet Haase472b2812010-10-14 07:02:04 -0700212 final float fromAlphaValue = 0;
213 final float toAlphaValue = (float)res.getInteger(R.integer.config_dragOutlineMaxAlpha);
Joe Onorato4be866d2010-10-10 11:26:02 -0700214
Patrick Dubroy8e58e912010-10-14 13:21:48 -0700215 Arrays.fill(mDragOutlineAlphas, fromAlphaValue);
Joe Onorato4be866d2010-10-10 11:26:02 -0700216
217 for (int i = 0; i < mDragOutlineAnims.length; i++) {
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700218 final InterruptibleInOutAnimator anim =
219 new InterruptibleInOutAnimator(duration, fromAlphaValue, toAlphaValue);
Patrick Dubroyce34a972010-10-19 10:34:32 -0700220 anim.getAnimator().setInterpolator(mEaseOutInterpolator);
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700221 final int thisIndex = i;
Chet Haase472b2812010-10-14 07:02:04 -0700222 anim.getAnimator().addUpdateListener(new AnimatorUpdateListener() {
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700223 public void onAnimationUpdate(ValueAnimator animation) {
Joe Onorato4be866d2010-10-10 11:26:02 -0700224 final Bitmap outline = (Bitmap)anim.getTag();
225
226 // If an animation is started and then stopped very quickly, we can still
227 // get spurious updates we've cleared the tag. Guard against this.
228 if (outline == null) {
Patrick Dubroyfe6bd872010-10-13 17:32:10 -0700229 if (false) {
230 Object val = animation.getAnimatedValue();
231 Log.d(TAG, "anim " + thisIndex + " update: " + val +
232 ", isStopped " + anim.isStopped());
233 }
Joe Onorato4be866d2010-10-10 11:26:02 -0700234 // Try to prevent it from continuing to run
235 animation.cancel();
236 } else {
Chet Haase472b2812010-10-14 07:02:04 -0700237 mDragOutlineAlphas[thisIndex] = (Float) animation.getAnimatedValue();
Joe Onorato4be866d2010-10-10 11:26:02 -0700238 final int left = mDragOutlines[thisIndex].x;
239 final int top = mDragOutlines[thisIndex].y;
240 CellLayout.this.invalidate(left, top,
241 left + outline.getWidth(), top + outline.getHeight());
242 }
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700243 }
244 });
Joe Onorato4be866d2010-10-10 11:26:02 -0700245 // The animation holds a reference to the drag outline bitmap as long is it's
246 // running. This way the bitmap can be GCed when the animations are complete.
Chet Haase472b2812010-10-14 07:02:04 -0700247 anim.getAnimator().addListener(new AnimatorListenerAdapter() {
Michael Jurka3c4c20f2010-10-28 15:36:06 -0700248 @Override
Joe Onorato4be866d2010-10-10 11:26:02 -0700249 public void onAnimationEnd(Animator animation) {
Chet Haase472b2812010-10-14 07:02:04 -0700250 if ((Float) ((ValueAnimator) animation).getAnimatedValue() == 0f) {
Joe Onorato4be866d2010-10-10 11:26:02 -0700251 anim.setTag(null);
252 }
253 }
254 });
255 mDragOutlineAnims[i] = anim;
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700256 }
Patrick Dubroyce34a972010-10-19 10:34:32 -0700257
Michael Jurka18014792010-10-14 09:01:34 -0700258 mBackgroundRect = new Rect();
Michael Jurka33945b22010-12-21 18:19:38 -0800259 mGlowBackgroundRect = new Rect();
Michael Jurka18014792010-10-14 09:01:34 -0700260 setHoverScale(1.0f);
261 setHoverAlpha(1.0f);
Michael Jurkabea15192010-11-17 12:33:46 -0800262
Michael Jurka8c920dd2011-01-20 14:16:56 -0800263 mChildren = new CellLayoutChildren(context);
Adam Cohen7f4eabe2011-04-21 16:19:16 -0700264 mChildren.setCellDimensions(mCellWidth, mCellHeight, mWidthGap, mHeightGap);
Michael Jurka8c920dd2011-01-20 14:16:56 -0800265 addView(mChildren);
Michael Jurka18014792010-10-14 09:01:34 -0700266 }
267
Michael Jurkaf6440da2011-04-05 14:50:34 -0700268 static int widthInPortrait(Resources r, int numCells) {
269 // We use this method from Workspace to figure out how many rows/columns Launcher should
270 // have. We ignore the left/right padding on CellLayout because it turns out in our design
271 // the padding extends outside the visible screen size, but it looked fine anyway.
Michael Jurkaf6440da2011-04-05 14:50:34 -0700272 int cellWidth = r.getDimensionPixelSize(R.dimen.workspace_cell_width);
273 int widthGap = r.getDimensionPixelSize(R.dimen.workspace_width_gap_port);
Michael Jurkaf6440da2011-04-05 14:50:34 -0700274
Michael Jurka32271c32011-06-01 17:03:13 -0700275 return widthGap * (numCells - 1) + cellWidth * numCells;
Michael Jurkaf6440da2011-04-05 14:50:34 -0700276 }
277
Michael Jurkaf6440da2011-04-05 14:50:34 -0700278 static int heightInLandscape(Resources r, int numCells) {
279 // We use this method from Workspace to figure out how many rows/columns Launcher should
280 // have. We ignore the left/right padding on CellLayout because it turns out in our design
281 // the padding extends outside the visible screen size, but it looked fine anyway.
Michael Jurkaf6440da2011-04-05 14:50:34 -0700282 int cellHeight = r.getDimensionPixelSize(R.dimen.workspace_cell_height);
283 int heightGap = r.getDimensionPixelSize(R.dimen.workspace_height_gap_land);
Michael Jurkaf6440da2011-04-05 14:50:34 -0700284
Michael Jurka32271c32011-06-01 17:03:13 -0700285 return heightGap * (numCells - 1) + cellHeight * numCells;
Michael Jurkaf6440da2011-04-05 14:50:34 -0700286 }
287
Adam Cohen2801caf2011-05-13 20:57:39 -0700288 public void enableHardwareLayers() {
289 mChildren.enableHardwareLayers();
290 }
291
292 public void setGridSize(int x, int y) {
293 mCountX = x;
294 mCountY = y;
295 mOccupied = new boolean[mCountX][mCountY];
296 }
297
Patrick Dubroy96864c32011-03-10 17:17:23 -0800298 private void invalidateBubbleTextView(BubbleTextView icon) {
299 final int padding = icon.getPressedOrFocusedBackgroundPadding();
Adam Cohen7f4eabe2011-04-21 16:19:16 -0700300 invalidate(icon.getLeft() + getLeftPadding() - padding,
301 icon.getTop() + getTopPadding() - padding,
302 icon.getRight() + getLeftPadding() + padding,
303 icon.getBottom() + getTopPadding() + padding);
Patrick Dubroy96864c32011-03-10 17:17:23 -0800304 }
305
306 void setPressedOrFocusedIcon(BubbleTextView icon) {
307 // We draw the pressed or focused BubbleTextView's background in CellLayout because it
308 // requires an expanded clip rect (due to the glow's blur radius)
309 BubbleTextView oldIcon = mPressedOrFocusedIcon;
310 mPressedOrFocusedIcon = icon;
311 if (oldIcon != null) {
312 invalidateBubbleTextView(oldIcon);
313 }
314 if (mPressedOrFocusedIcon != null) {
315 invalidateBubbleTextView(mPressedOrFocusedIcon);
316 }
317 }
318
Winson Chung6e314082011-01-27 16:46:51 -0800319 public CellLayoutChildren getChildrenLayout() {
320 if (getChildCount() > 0) {
321 return (CellLayoutChildren) getChildAt(0);
322 }
323 return null;
324 }
325
Michael Jurka33945b22010-12-21 18:19:38 -0800326 public void setIsDefaultDropTarget(boolean isDefaultDropTarget) {
327 if (mIsDefaultDropTarget != isDefaultDropTarget) {
328 mIsDefaultDropTarget = isDefaultDropTarget;
329 invalidate();
330 }
331 }
332
Michael Jurka33945b22010-12-21 18:19:38 -0800333 void setIsDragOccuring(boolean isDragOccuring) {
334 if (mIsDragOccuring != isDragOccuring) {
335 mIsDragOccuring = isDragOccuring;
336 invalidate();
337 }
338 }
339
340 void setIsDragOverlapping(boolean isDragOverlapping) {
341 if (mIsDragOverlapping != isDragOverlapping) {
342 mIsDragOverlapping = isDragOverlapping;
343 invalidate();
344 }
345 }
346
347 boolean getIsDragOverlapping() {
348 return mIsDragOverlapping;
349 }
350
351 private void updateGlowRect() {
352 float marginFraction = (mGlowBackgroundScale - 1.0f) / 2.0f;
Michael Jurka18014792010-10-14 09:01:34 -0700353 int marginX = (int) (marginFraction * (mBackgroundRect.right - mBackgroundRect.left));
354 int marginY = (int) (marginFraction * (mBackgroundRect.bottom - mBackgroundRect.top));
Michael Jurka33945b22010-12-21 18:19:38 -0800355 mGlowBackgroundRect.set(mBackgroundRect.left - marginX, mBackgroundRect.top - marginY,
Michael Jurka18014792010-10-14 09:01:34 -0700356 mBackgroundRect.right + marginX, mBackgroundRect.bottom + marginY);
357 invalidate();
358 }
359
360 public void setHoverScale(float scaleFactor) {
Michael Jurka33945b22010-12-21 18:19:38 -0800361 if (scaleFactor != mGlowBackgroundScale) {
362 mGlowBackgroundScale = scaleFactor;
363 updateGlowRect();
Michael Jurka8deb1e62011-01-25 16:27:43 -0800364 if (getParent() != null) {
365 ((View) getParent()).invalidate();
366 }
Michael Jurka18014792010-10-14 09:01:34 -0700367 }
368 }
369
370 public float getHoverScale() {
Michael Jurka33945b22010-12-21 18:19:38 -0800371 return mGlowBackgroundScale;
Michael Jurka18014792010-10-14 09:01:34 -0700372 }
373
374 public float getHoverAlpha() {
Michael Jurka33945b22010-12-21 18:19:38 -0800375 return mGlowBackgroundAlpha;
Michael Jurka18014792010-10-14 09:01:34 -0700376 }
377
378 public void setHoverAlpha(float alpha) {
Michael Jurka33945b22010-12-21 18:19:38 -0800379 mGlowBackgroundAlpha = alpha;
Michael Jurka18014792010-10-14 09:01:34 -0700380 invalidate();
381 }
382
383 void animateDrop() {
Winson Chungb26f3d62011-06-02 10:49:29 -0700384 Resources res = getResources();
385 float onDropScale = res.getInteger(R.integer.config_screenOnDropScalePercent) / 100.0f;
386 ObjectAnimator scaleUp = ObjectAnimator.ofFloat(this, "hoverScale", onDropScale);
387 scaleUp.setDuration(res.getInteger(R.integer.config_screenOnDropScaleUpDuration));
388 ObjectAnimator scaleDown = ObjectAnimator.ofFloat(this, "hoverScale", 1.0f);
389 scaleDown.setDuration(res.getInteger(R.integer.config_screenOnDropScaleDownDuration));
390 ObjectAnimator alphaFadeOut = ObjectAnimator.ofFloat(this, "hoverAlpha", 0.0f);
Michael Jurka18014792010-10-14 09:01:34 -0700391
Winson Chungb26f3d62011-06-02 10:49:29 -0700392 alphaFadeOut.setStartDelay(res.getInteger(R.integer.config_screenOnDropAlphaFadeDelay));
393 alphaFadeOut.setDuration(res.getInteger(R.integer.config_screenOnDropAlphaFadeDuration));
Michael Jurka18014792010-10-14 09:01:34 -0700394
Winson Chungb26f3d62011-06-02 10:49:29 -0700395 AnimatorSet bouncer = new AnimatorSet();
396 bouncer.play(scaleUp).before(scaleDown);
397 bouncer.play(scaleUp).with(alphaFadeOut);
398 bouncer.addListener(new AnimatorListenerAdapter() {
399 @Override
400 public void onAnimationStart(Animator animation) {
401 setIsDragOverlapping(true);
402 }
403 @Override
404 public void onAnimationEnd(Animator animation) {
405 setIsDragOverlapping(false);
406 setHoverScale(1.0f);
407 setHoverAlpha(1.0f);
408 }
409 });
410 bouncer.start();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800411 }
412
Jeff Sharkey83f111d2009-04-20 21:03:13 -0700413 @Override
Patrick Dubroy1262e362010-10-06 15:49:50 -0700414 protected void onDraw(Canvas canvas) {
Michael Jurka3e7c7632010-10-02 16:01:03 -0700415 // When we're large, we are either drawn in a "hover" state (ie when dragging an item to
416 // a neighboring page) or with just a normal background (if backgroundAlpha > 0.0f)
417 // When we're small, we are either drawn normally or in the "accepts drops" state (during
418 // a drag). However, we also drag the mini hover background *over* one of those two
419 // backgrounds
Winson Chungb26f3d62011-06-02 10:49:29 -0700420 if (mBackgroundAlpha > 0.0f) {
Adam Cohenf34bab52010-09-30 14:11:56 -0700421 Drawable bg;
Michael Jurka33945b22010-12-21 18:19:38 -0800422 boolean mini = getScaleX() < 0.5f;
423
424 if (mIsDragOverlapping) {
425 // In the mini case, we draw the active_glow bg *over* the active background
426 bg = mini ? mActiveBackgroundMini : mActiveGlowBackground;
427 } else if (mIsDragOccuring && mAcceptsDrops) {
428 bg = mini ? mActiveBackgroundMini : mActiveBackground;
Adam Cohen3af863b2011-01-25 12:16:51 -0800429 } else if (mIsDefaultDropTarget && mini) {
430 bg = mNormalGlowBackgroundMini;
Adam Cohenf34bab52010-09-30 14:11:56 -0700431 } else {
Michael Jurka33945b22010-12-21 18:19:38 -0800432 bg = mini ? mNormalBackgroundMini : mNormalBackground;
Adam Cohenf34bab52010-09-30 14:11:56 -0700433 }
Michael Jurka33945b22010-12-21 18:19:38 -0800434
435 bg.setAlpha((int) (mBackgroundAlpha * mBackgroundAlphaMultiplier * 255));
436 bg.setBounds(mBackgroundRect);
437 bg.draw(canvas);
438
439 if (mini && mIsDragOverlapping) {
Michael Jurka18014792010-10-14 09:01:34 -0700440 boolean modifiedClipRect = false;
Michael Jurka33945b22010-12-21 18:19:38 -0800441 if (mGlowBackgroundScale > 1.0f) {
Michael Jurka18014792010-10-14 09:01:34 -0700442 // If the hover background's scale is greater than 1, we'll be drawing outside
443 // the bounds of this CellLayout. Get around that by temporarily increasing the
444 // size of the clip rect
Michael Jurka33945b22010-12-21 18:19:38 -0800445 float marginFraction = (mGlowBackgroundScale - 1.0f) / 2.0f;
Michael Jurka18014792010-10-14 09:01:34 -0700446 Rect clipRect = canvas.getClipBounds();
447 int marginX = (int) (marginFraction * (clipRect.right - clipRect.left));
448 int marginY = (int) (marginFraction * (clipRect.bottom - clipRect.top));
449 canvas.save(Canvas.CLIP_SAVE_FLAG);
450 canvas.clipRect(-marginX, -marginY,
451 getWidth() + marginX, getHeight() + marginY, Region.Op.REPLACE);
452 modifiedClipRect = true;
453 }
454
Michael Jurka33945b22010-12-21 18:19:38 -0800455 mActiveGlowBackgroundMini.setAlpha(
456 (int) (mBackgroundAlpha * mGlowBackgroundAlpha * 255));
457 mActiveGlowBackgroundMini.setBounds(mGlowBackgroundRect);
458 mActiveGlowBackgroundMini.draw(canvas);
Michael Jurka18014792010-10-14 09:01:34 -0700459 if (modifiedClipRect) {
460 canvas.restore();
461 }
Michael Jurka3e7c7632010-10-02 16:01:03 -0700462 }
Michael Jurkaa63c4522010-08-19 13:52:27 -0700463 }
Romain Guya6abce82009-11-10 02:54:41 -0800464
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700465 if (mCrosshairsVisibility > 0.0f) {
466 final int countX = mCountX;
467 final int countY = mCountY;
468
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700469 final float MAX_ALPHA = 0.4f;
470 final int MAX_VISIBLE_DISTANCE = 600;
471 final float DISTANCE_MULTIPLIER = 0.002f;
472
473 final Drawable d = mCrosshairsDrawable;
474 final int width = d.getIntrinsicWidth();
475 final int height = d.getIntrinsicHeight();
476
477 int x = getLeftPadding() - (mWidthGap / 2) - (width / 2);
478 for (int col = 0; col <= countX; col++) {
479 int y = getTopPadding() - (mHeightGap / 2) - (height / 2);
480 for (int row = 0; row <= countY; row++) {
481 mTmpPointF.set(x - mDragCenter.x, y - mDragCenter.y);
482 float dist = mTmpPointF.length();
483 // Crosshairs further from the drag point are more faint
484 float alpha = Math.min(MAX_ALPHA,
485 DISTANCE_MULTIPLIER * (MAX_VISIBLE_DISTANCE - dist));
486 if (alpha > 0.0f) {
487 d.setBounds(x, y, x + width, y + height);
488 d.setAlpha((int) (alpha * 255 * mCrosshairsVisibility));
489 d.draw(canvas);
490 }
491 y += mCellHeight + mHeightGap;
492 }
493 x += mCellWidth + mWidthGap;
494 }
Joe Onorato4be866d2010-10-10 11:26:02 -0700495 }
Winson Chung150fbab2010-09-29 17:14:26 -0700496
Patrick Dubroy8e58e912010-10-14 13:21:48 -0700497 final Paint paint = mDragOutlinePaint;
Joe Onorato4be866d2010-10-10 11:26:02 -0700498 for (int i = 0; i < mDragOutlines.length; i++) {
Chet Haase472b2812010-10-14 07:02:04 -0700499 final float alpha = mDragOutlineAlphas[i];
Joe Onorato4be866d2010-10-10 11:26:02 -0700500 if (alpha > 0) {
501 final Point p = mDragOutlines[i];
502 final Bitmap b = (Bitmap) mDragOutlineAnims[i].getTag();
Chet Haase472b2812010-10-14 07:02:04 -0700503 paint.setAlpha((int)(alpha + .5f));
Joe Onorato4be866d2010-10-10 11:26:02 -0700504 canvas.drawBitmap(b, p.x, p.y, paint);
Winson Chung150fbab2010-09-29 17:14:26 -0700505 }
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700506 }
Patrick Dubroy96864c32011-03-10 17:17:23 -0800507
508 // We draw the pressed or focused BubbleTextView's background in CellLayout because it
509 // requires an expanded clip rect (due to the glow's blur radius)
510 if (mPressedOrFocusedIcon != null) {
511 final int padding = mPressedOrFocusedIcon.getPressedOrFocusedBackgroundPadding();
512 final Bitmap b = mPressedOrFocusedIcon.getPressedOrFocusedBackground();
513 if (b != null) {
514 canvas.drawBitmap(b,
Adam Cohen7f4eabe2011-04-21 16:19:16 -0700515 mPressedOrFocusedIcon.getLeft() + getLeftPadding() - padding,
516 mPressedOrFocusedIcon.getTop() + getTopPadding() - padding,
Patrick Dubroy96864c32011-03-10 17:17:23 -0800517 null);
518 }
519 }
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700520 }
521
522 @Override
Jeff Sharkey83f111d2009-04-20 21:03:13 -0700523 public void cancelLongPress() {
524 super.cancelLongPress();
525
526 // Cancel long press for all children
527 final int count = getChildCount();
528 for (int i = 0; i < count; i++) {
529 final View child = getChildAt(i);
530 child.cancelLongPress();
531 }
532 }
533
Michael Jurkadee05892010-07-27 10:01:56 -0700534 public void setOnInterceptTouchListener(View.OnTouchListener listener) {
535 mInterceptTouchListener = listener;
536 }
537
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800538 int getCountX() {
Adam Cohend22015c2010-07-26 22:02:18 -0700539 return mCountX;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800540 }
541
542 int getCountY() {
Adam Cohend22015c2010-07-26 22:02:18 -0700543 return mCountY;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800544 }
545
Michael Jurkaf3ca3ab2010-10-20 17:08:24 -0700546 public boolean addViewToCellLayout(
547 View child, int index, int childId, LayoutParams params, boolean markCells) {
Winson Chungaafa03c2010-06-11 17:34:16 -0700548 final LayoutParams lp = params;
549
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800550 // Generate an id for each view, this assumes we have at most 256x256 cells
551 // per workspace screen
Adam Cohend22015c2010-07-26 22:02:18 -0700552 if (lp.cellX >= 0 && lp.cellX <= mCountX - 1 && lp.cellY >= 0 && lp.cellY <= mCountY - 1) {
Winson Chungaafa03c2010-06-11 17:34:16 -0700553 // If the horizontal or vertical span is set to -1, it is taken to
554 // mean that it spans the extent of the CellLayout
Adam Cohend22015c2010-07-26 22:02:18 -0700555 if (lp.cellHSpan < 0) lp.cellHSpan = mCountX;
556 if (lp.cellVSpan < 0) lp.cellVSpan = mCountY;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800557
Winson Chungaafa03c2010-06-11 17:34:16 -0700558 child.setId(childId);
559
Michael Jurka8c920dd2011-01-20 14:16:56 -0800560 mChildren.addView(child, index, lp);
Michael Jurkadee05892010-07-27 10:01:56 -0700561
Michael Jurkaf3ca3ab2010-10-20 17:08:24 -0700562 if (markCells) markCellsAsOccupiedForView(child);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700563
Winson Chungaafa03c2010-06-11 17:34:16 -0700564 return true;
565 }
566 return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800567 }
Michael Jurka3e7c7632010-10-02 16:01:03 -0700568
Michael Jurkabea15192010-11-17 12:33:46 -0800569 public void setAcceptsDrops(boolean acceptsDrops) {
570 if (mAcceptsDrops != acceptsDrops) {
571 mAcceptsDrops = acceptsDrops;
572 invalidate();
573 }
574 }
575
Michael Jurka3e7c7632010-10-02 16:01:03 -0700576 public boolean getAcceptsDrops() {
577 return mAcceptsDrops;
578 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800579
580 @Override
Michael Jurka0280c3b2010-09-17 15:00:07 -0700581 public void removeAllViews() {
582 clearOccupiedCells();
Michael Jurka8c920dd2011-01-20 14:16:56 -0800583 mChildren.removeAllViews();
Michael Jurka0280c3b2010-09-17 15:00:07 -0700584 }
585
586 @Override
587 public void removeAllViewsInLayout() {
588 clearOccupiedCells();
Michael Jurka8c920dd2011-01-20 14:16:56 -0800589 mChildren.removeAllViewsInLayout();
Michael Jurka0280c3b2010-09-17 15:00:07 -0700590 }
591
Michael Jurkaf3ca3ab2010-10-20 17:08:24 -0700592 public void removeViewWithoutMarkingCells(View view) {
Michael Jurkacf6125c2011-01-28 15:20:01 -0800593 mChildren.removeView(view);
Michael Jurkaf3ca3ab2010-10-20 17:08:24 -0700594 }
595
Michael Jurka0280c3b2010-09-17 15:00:07 -0700596 @Override
597 public void removeView(View view) {
598 markCellsAsUnoccupiedForView(view);
Michael Jurka8c920dd2011-01-20 14:16:56 -0800599 mChildren.removeView(view);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700600 }
601
602 @Override
603 public void removeViewAt(int index) {
Michael Jurka8c920dd2011-01-20 14:16:56 -0800604 markCellsAsUnoccupiedForView(mChildren.getChildAt(index));
605 mChildren.removeViewAt(index);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700606 }
607
608 @Override
609 public void removeViewInLayout(View view) {
610 markCellsAsUnoccupiedForView(view);
Michael Jurka8c920dd2011-01-20 14:16:56 -0800611 mChildren.removeViewInLayout(view);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700612 }
613
614 @Override
615 public void removeViews(int start, int count) {
616 for (int i = start; i < start + count; i++) {
Michael Jurka8c920dd2011-01-20 14:16:56 -0800617 markCellsAsUnoccupiedForView(mChildren.getChildAt(i));
Michael Jurka0280c3b2010-09-17 15:00:07 -0700618 }
Michael Jurka8c920dd2011-01-20 14:16:56 -0800619 mChildren.removeViews(start, count);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700620 }
621
622 @Override
623 public void removeViewsInLayout(int start, int count) {
624 for (int i = start; i < start + count; i++) {
Michael Jurka8c920dd2011-01-20 14:16:56 -0800625 markCellsAsUnoccupiedForView(mChildren.getChildAt(i));
Michael Jurka0280c3b2010-09-17 15:00:07 -0700626 }
Michael Jurka8c920dd2011-01-20 14:16:56 -0800627 mChildren.removeViewsInLayout(start, count);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700628 }
629
Michael Jurka8c920dd2011-01-20 14:16:56 -0800630 public void drawChildren(Canvas canvas) {
631 mChildren.draw(canvas);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800632 }
633
Michael Jurkaabded662011-03-04 12:06:57 -0800634 void buildChildrenLayer() {
635 mChildren.buildLayer();
636 }
637
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800638 @Override
639 protected void onAttachedToWindow() {
640 super.onAttachedToWindow();
641 mCellInfo.screen = ((ViewGroup) getParent()).indexOfChild(this);
642 }
643
Michael Jurkaaf442092010-06-10 17:01:57 -0700644 public void setTagToCellInfoForPoint(int touchX, int touchY) {
645 final CellInfo cellInfo = mCellInfo;
646 final Rect frame = mRect;
647 final int x = touchX + mScrollX;
648 final int y = touchY + mScrollY;
Michael Jurka8c920dd2011-01-20 14:16:56 -0800649 final int count = mChildren.getChildCount();
Michael Jurkaaf442092010-06-10 17:01:57 -0700650
651 boolean found = false;
652 for (int i = count - 1; i >= 0; i--) {
Michael Jurka8c920dd2011-01-20 14:16:56 -0800653 final View child = mChildren.getChildAt(i);
Adam Cohend4844c32011-02-18 19:25:06 -0800654 final LayoutParams lp = (LayoutParams) child.getLayoutParams();
Michael Jurkaaf442092010-06-10 17:01:57 -0700655
Adam Cohen1b607ed2011-03-03 17:26:50 -0800656 if ((child.getVisibility() == VISIBLE || child.getAnimation() != null) &&
657 lp.isLockedToGrid) {
Michael Jurkaaf442092010-06-10 17:01:57 -0700658 child.getHitRect(frame);
Winson Chung0be025d2011-05-23 17:45:09 -0700659
660 // The child hit rect is relative to the CellLayoutChildren parent, so we need to
661 // offset that by this CellLayout's padding to test an (x,y) point that is relative
662 // to this view.
663 final int tmpXY[] = mTmpXY;
664 child.getLocationOnScreen(tmpXY);
665 frame.offset(mLeftPadding, mTopPadding);
666
Michael Jurkaaf442092010-06-10 17:01:57 -0700667 if (frame.contains(x, y)) {
Michael Jurkaaf442092010-06-10 17:01:57 -0700668 cellInfo.cell = child;
669 cellInfo.cellX = lp.cellX;
670 cellInfo.cellY = lp.cellY;
671 cellInfo.spanX = lp.cellHSpan;
672 cellInfo.spanY = lp.cellVSpan;
673 cellInfo.valid = true;
674 found = true;
Michael Jurkaaf442092010-06-10 17:01:57 -0700675 break;
676 }
677 }
678 }
Winson Chungaafa03c2010-06-11 17:34:16 -0700679
Michael Jurkaaf442092010-06-10 17:01:57 -0700680 if (!found) {
Winson Chung0be025d2011-05-23 17:45:09 -0700681 final int cellXY[] = mTmpXY;
Michael Jurkaaf442092010-06-10 17:01:57 -0700682 pointToCellExact(x, y, cellXY);
683
Michael Jurkaaf442092010-06-10 17:01:57 -0700684 cellInfo.cell = null;
685 cellInfo.cellX = cellXY[0];
686 cellInfo.cellY = cellXY[1];
687 cellInfo.spanX = 1;
688 cellInfo.spanY = 1;
Michael Jurka0280c3b2010-09-17 15:00:07 -0700689 cellInfo.valid = cellXY[0] >= 0 && cellXY[1] >= 0 && cellXY[0] < mCountX &&
690 cellXY[1] < mCountY && !mOccupied[cellXY[0]][cellXY[1]];
Michael Jurkaaf442092010-06-10 17:01:57 -0700691 }
692 setTag(cellInfo);
693 }
694
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800695 @Override
696 public boolean onInterceptTouchEvent(MotionEvent ev) {
Michael Jurkadee05892010-07-27 10:01:56 -0700697 if (mInterceptTouchListener != null && mInterceptTouchListener.onTouch(this, ev)) {
698 return true;
699 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800700 final int action = ev.getAction();
701 final CellInfo cellInfo = mCellInfo;
702
703 if (action == MotionEvent.ACTION_DOWN) {
Michael Jurkaaf442092010-06-10 17:01:57 -0700704 setTagToCellInfoForPoint((int) ev.getX(), (int) ev.getY());
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800705 } else if (action == MotionEvent.ACTION_UP) {
706 cellInfo.cell = null;
707 cellInfo.cellX = -1;
708 cellInfo.cellY = -1;
709 cellInfo.spanX = 0;
710 cellInfo.spanY = 0;
711 cellInfo.valid = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800712 setTag(cellInfo);
713 }
714
715 return false;
716 }
717
718 @Override
719 public CellInfo getTag() {
Michael Jurka0280c3b2010-09-17 15:00:07 -0700720 return (CellInfo) super.getTag();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800721 }
722
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700723 /**
Winson Chungaafa03c2010-06-11 17:34:16 -0700724 * Given a point, return the cell that strictly encloses that point
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800725 * @param x X coordinate of the point
726 * @param y Y coordinate of the point
727 * @param result Array of 2 ints to hold the x and y coordinate of the cell
728 */
729 void pointToCellExact(int x, int y, int[] result) {
Winson Chungaafa03c2010-06-11 17:34:16 -0700730 final int hStartPadding = getLeftPadding();
731 final int vStartPadding = getTopPadding();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800732
733 result[0] = (x - hStartPadding) / (mCellWidth + mWidthGap);
734 result[1] = (y - vStartPadding) / (mCellHeight + mHeightGap);
735
Adam Cohend22015c2010-07-26 22:02:18 -0700736 final int xAxis = mCountX;
737 final int yAxis = mCountY;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800738
739 if (result[0] < 0) result[0] = 0;
740 if (result[0] >= xAxis) result[0] = xAxis - 1;
741 if (result[1] < 0) result[1] = 0;
742 if (result[1] >= yAxis) result[1] = yAxis - 1;
743 }
Winson Chungaafa03c2010-06-11 17:34:16 -0700744
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800745 /**
746 * Given a point, return the cell that most closely encloses that point
747 * @param x X coordinate of the point
748 * @param y Y coordinate of the point
749 * @param result Array of 2 ints to hold the x and y coordinate of the cell
750 */
751 void pointToCellRounded(int x, int y, int[] result) {
752 pointToCellExact(x + (mCellWidth / 2), y + (mCellHeight / 2), result);
753 }
754
755 /**
756 * Given a cell coordinate, return the point that represents the upper left corner of that cell
Winson Chungaafa03c2010-06-11 17:34:16 -0700757 *
758 * @param cellX X coordinate of the cell
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800759 * @param cellY Y coordinate of the cell
Winson Chungaafa03c2010-06-11 17:34:16 -0700760 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800761 * @param result Array of 2 ints to hold the x and y coordinate of the point
762 */
763 void cellToPoint(int cellX, int cellY, int[] result) {
Winson Chungaafa03c2010-06-11 17:34:16 -0700764 final int hStartPadding = getLeftPadding();
765 final int vStartPadding = getTopPadding();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800766
767 result[0] = hStartPadding + cellX * (mCellWidth + mWidthGap);
768 result[1] = vStartPadding + cellY * (mCellHeight + mHeightGap);
769 }
770
Adam Cohene3e27a82011-04-15 12:07:39 -0700771 /**
772 * Given a cell coordinate, return the point that represents the upper left corner of that cell
773 *
774 * @param cellX X coordinate of the cell
775 * @param cellY Y coordinate of the cell
776 *
777 * @param result Array of 2 ints to hold the x and y coordinate of the point
778 */
779 void cellToCenterPoint(int cellX, int cellY, int[] result) {
780 final int hStartPadding = getLeftPadding();
781 final int vStartPadding = getTopPadding();
782
783 result[0] = hStartPadding + cellX * (mCellWidth + mWidthGap) + mCellWidth / 2;
784 result[1] = vStartPadding + cellY * (mCellHeight + mHeightGap) + mCellHeight / 2;
785 }
786
Romain Guy84f296c2009-11-04 15:00:44 -0800787 int getCellWidth() {
788 return mCellWidth;
789 }
790
791 int getCellHeight() {
792 return mCellHeight;
793 }
794
Adam Cohend4844c32011-02-18 19:25:06 -0800795 int getWidthGap() {
796 return mWidthGap;
797 }
798
799 int getHeightGap() {
800 return mHeightGap;
801 }
802
Romain Guy1a304a12009-11-10 00:02:32 -0800803 int getLeftPadding() {
Winson Chungaafa03c2010-06-11 17:34:16 -0700804 return mLeftPadding;
Romain Guy1a304a12009-11-10 00:02:32 -0800805 }
806
807 int getTopPadding() {
Winson Chungaafa03c2010-06-11 17:34:16 -0700808 return mTopPadding;
Romain Guy1a304a12009-11-10 00:02:32 -0800809 }
810
811 int getRightPadding() {
Winson Chungaafa03c2010-06-11 17:34:16 -0700812 return mRightPadding;
Romain Guy1a304a12009-11-10 00:02:32 -0800813 }
814
815 int getBottomPadding() {
Winson Chungaafa03c2010-06-11 17:34:16 -0700816 return mBottomPadding;
Romain Guy1a304a12009-11-10 00:02:32 -0800817 }
818
Adam Cohen7f4eabe2011-04-21 16:19:16 -0700819 Rect getContentRect(Rect r) {
820 if (r == null) {
821 r = new Rect();
822 }
823 int left = getPaddingLeft();
824 int top = getPaddingTop();
825 int right = left + getWidth() - mLeftPadding - mRightPadding;
826 int bottom = top + getHeight() - mTopPadding - mBottomPadding;
827 r.set(left, top, right, bottom);
828 return r;
829 }
830
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800831 @Override
832 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
833 // TODO: currently ignoring padding
Winson Chungaafa03c2010-06-11 17:34:16 -0700834
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800835 int widthSpecMode = MeasureSpec.getMode(widthMeasureSpec);
Winson Chungaafa03c2010-06-11 17:34:16 -0700836 int widthSpecSize = MeasureSpec.getSize(widthMeasureSpec);
837
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800838 int heightSpecMode = MeasureSpec.getMode(heightMeasureSpec);
839 int heightSpecSize = MeasureSpec.getSize(heightMeasureSpec);
Winson Chungaafa03c2010-06-11 17:34:16 -0700840
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800841 if (widthSpecMode == MeasureSpec.UNSPECIFIED || heightSpecMode == MeasureSpec.UNSPECIFIED) {
842 throw new RuntimeException("CellLayout cannot have UNSPECIFIED dimensions");
843 }
844
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800845 final int cellWidth = mCellWidth;
846 final int cellHeight = mCellHeight;
847
Adam Cohend22015c2010-07-26 22:02:18 -0700848 int numWidthGaps = mCountX - 1;
849 int numHeightGaps = mCountY - 1;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800850
Winson Chungece7f5b2010-10-22 14:54:12 -0700851 if (mWidthGap < 0 || mHeightGap < 0) {
852 int vSpaceLeft = heightSpecSize - mTopPadding - mBottomPadding - (cellHeight * mCountY);
Adam Cohen2801caf2011-05-13 20:57:39 -0700853 mHeightGap = numHeightGaps > 0 ? vSpaceLeft / numHeightGaps : 0;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800854
Winson Chungece7f5b2010-10-22 14:54:12 -0700855 int hSpaceLeft = widthSpecSize - mLeftPadding - mRightPadding - (cellWidth * mCountX);
Adam Cohen2801caf2011-05-13 20:57:39 -0700856 mWidthGap = numWidthGaps > 0 ? hSpaceLeft / numWidthGaps : 0;
Winson Chungaafa03c2010-06-11 17:34:16 -0700857
Winson Chungece7f5b2010-10-22 14:54:12 -0700858 // center it around the min gaps
859 int minGap = Math.min(mWidthGap, mHeightGap);
860 mWidthGap = mHeightGap = minGap;
861 }
Michael Jurka5f1c5092010-09-03 14:15:02 -0700862
Michael Jurka8c920dd2011-01-20 14:16:56 -0800863 // Initial values correspond to widthSpecMode == MeasureSpec.EXACTLY
864 int newWidth = widthSpecSize;
865 int newHeight = heightSpecSize;
Michael Jurka5f1c5092010-09-03 14:15:02 -0700866 if (widthSpecMode == MeasureSpec.AT_MOST) {
Michael Jurka8c920dd2011-01-20 14:16:56 -0800867 newWidth = mLeftPadding + mRightPadding + (mCountX * cellWidth) +
Winson Chungece7f5b2010-10-22 14:54:12 -0700868 ((mCountX - 1) * mWidthGap);
Michael Jurka8c920dd2011-01-20 14:16:56 -0800869 newHeight = mTopPadding + mBottomPadding + (mCountY * cellHeight) +
Winson Chungece7f5b2010-10-22 14:54:12 -0700870 ((mCountY - 1) * mHeightGap);
Michael Jurka5f1c5092010-09-03 14:15:02 -0700871 setMeasuredDimension(newWidth, newHeight);
Michael Jurka5f1c5092010-09-03 14:15:02 -0700872 }
Michael Jurka8c920dd2011-01-20 14:16:56 -0800873
874 int count = getChildCount();
875 for (int i = 0; i < count; i++) {
876 View child = getChildAt(i);
Adam Cohen2801caf2011-05-13 20:57:39 -0700877 int childWidthMeasureSpec = MeasureSpec.makeMeasureSpec(newWidth - mLeftPadding -
878 mRightPadding, MeasureSpec.EXACTLY);
879 int childheightMeasureSpec = MeasureSpec.makeMeasureSpec(newHeight - mTopPadding -
880 mBottomPadding, MeasureSpec.EXACTLY);
Michael Jurka8c920dd2011-01-20 14:16:56 -0800881 child.measure(childWidthMeasureSpec, childheightMeasureSpec);
882 }
883 setMeasuredDimension(newWidth, newHeight);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800884 }
885
886 @Override
Michael Jurka28750fb2010-09-24 17:43:49 -0700887 protected void onLayout(boolean changed, int l, int t, int r, int b) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800888 int count = getChildCount();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800889 for (int i = 0; i < count; i++) {
Michael Jurka8c920dd2011-01-20 14:16:56 -0800890 View child = getChildAt(i);
Adam Cohenbb60e2e2011-04-26 16:48:26 -0700891 child.layout(mLeftPadding, mTopPadding, r - l - mRightPadding , b - t - mBottomPadding);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800892 }
893 }
894
895 @Override
Michael Jurkadee05892010-07-27 10:01:56 -0700896 protected void onSizeChanged(int w, int h, int oldw, int oldh) {
897 super.onSizeChanged(w, h, oldw, oldh);
Michael Jurka18014792010-10-14 09:01:34 -0700898 mBackgroundRect.set(0, 0, w, h);
Michael Jurka33945b22010-12-21 18:19:38 -0800899 updateGlowRect();
Michael Jurkadee05892010-07-27 10:01:56 -0700900 }
901
902 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800903 protected void setChildrenDrawingCacheEnabled(boolean enabled) {
Michael Jurka8c920dd2011-01-20 14:16:56 -0800904 mChildren.setChildrenDrawingCacheEnabled(enabled);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800905 }
906
907 @Override
908 protected void setChildrenDrawnWithCacheEnabled(boolean enabled) {
Michael Jurka8c920dd2011-01-20 14:16:56 -0800909 mChildren.setChildrenDrawnWithCacheEnabled(enabled);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800910 }
911
Michael Jurka5f1c5092010-09-03 14:15:02 -0700912 public float getBackgroundAlpha() {
913 return mBackgroundAlpha;
Michael Jurkadee05892010-07-27 10:01:56 -0700914 }
915
Michael Jurka742574b2011-02-02 23:51:01 -0800916 public void setFastBackgroundAlpha(float alpha) {
917 mBackgroundAlpha = alpha;
918 }
919
Adam Cohen1b0aaac2010-10-28 11:11:18 -0700920 public void setBackgroundAlphaMultiplier(float multiplier) {
921 mBackgroundAlphaMultiplier = multiplier;
922 }
923
Adam Cohenddb82192010-11-10 16:32:54 -0800924 public float getBackgroundAlphaMultiplier() {
925 return mBackgroundAlphaMultiplier;
926 }
927
Michael Jurka5f1c5092010-09-03 14:15:02 -0700928 public void setBackgroundAlpha(float alpha) {
929 mBackgroundAlpha = alpha;
Michael Jurka0142d492010-08-25 17:46:15 -0700930 invalidate();
Michael Jurkadee05892010-07-27 10:01:56 -0700931 }
932
Michael Jurka5f1c5092010-09-03 14:15:02 -0700933 // Need to return true to let the view system know we know how to handle alpha-- this is
934 // because when our children have an alpha of 0.0f, they are still rendering their "dimmed"
935 // versions
936 @Override
937 protected boolean onSetAlpha(int alpha) {
938 return true;
939 }
940
941 public void setAlpha(float alpha) {
942 setChildrenAlpha(alpha);
943 super.setAlpha(alpha);
944 }
945
Michael Jurka742574b2011-02-02 23:51:01 -0800946 public void setFastAlpha(float alpha) {
947 setFastChildrenAlpha(alpha);
948 super.setFastAlpha(alpha);
949 }
950
Michael Jurkadee05892010-07-27 10:01:56 -0700951 private void setChildrenAlpha(float alpha) {
Michael Jurka0142d492010-08-25 17:46:15 -0700952 final int childCount = getChildCount();
953 for (int i = 0; i < childCount; i++) {
Michael Jurkadee05892010-07-27 10:01:56 -0700954 getChildAt(i).setAlpha(alpha);
955 }
956 }
957
Michael Jurka742574b2011-02-02 23:51:01 -0800958 private void setFastChildrenAlpha(float alpha) {
959 final int childCount = getChildCount();
960 for (int i = 0; i < childCount; i++) {
961 getChildAt(i).setFastAlpha(alpha);
962 }
963 }
964
Patrick Dubroy440c3602010-07-13 17:50:32 -0700965 public View getChildAt(int x, int y) {
Michael Jurka8c920dd2011-01-20 14:16:56 -0800966 return mChildren.getChildAt(x, y);
Patrick Dubroy440c3602010-07-13 17:50:32 -0700967 }
968
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700969 /**
970 * Estimate where the top left cell of the dragged item will land if it is dropped.
971 *
972 * @param originX The X value of the top left corner of the item
973 * @param originY The Y value of the top left corner of the item
974 * @param spanX The number of horizontal cells that the item spans
975 * @param spanY The number of vertical cells that the item spans
976 * @param result The estimated drop cell X and Y.
977 */
978 void estimateDropCell(int originX, int originY, int spanX, int spanY, int[] result) {
Adam Cohend22015c2010-07-26 22:02:18 -0700979 final int countX = mCountX;
980 final int countY = mCountY;
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700981
Michael Jurkaa63c4522010-08-19 13:52:27 -0700982 // pointToCellRounded takes the top left of a cell but will pad that with
983 // cellWidth/2 and cellHeight/2 when finding the matching cell
984 pointToCellRounded(originX, originY, result);
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700985
986 // If the item isn't fully on this screen, snap to the edges
987 int rightOverhang = result[0] + spanX - countX;
988 if (rightOverhang > 0) {
989 result[0] -= rightOverhang; // Snap to right
990 }
991 result[0] = Math.max(0, result[0]); // Snap to left
992 int bottomOverhang = result[1] + spanY - countY;
993 if (bottomOverhang > 0) {
994 result[1] -= bottomOverhang; // Snap to bottom
995 }
996 result[1] = Math.max(0, result[1]); // Snap to top
997 }
998
Joe Onorato4be866d2010-10-10 11:26:02 -0700999 void visualizeDropLocation(
1000 View v, Bitmap dragOutline, int originX, int originY, int spanX, int spanY) {
1001
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -07001002 final int oldDragCellX = mDragCell[0];
1003 final int oldDragCellY = mDragCell[1];
Joe Onorato4be866d2010-10-10 11:26:02 -07001004 final int[] nearest = findNearestVacantArea(originX, originY, spanX, spanY, v, mDragCell);
Winson Chunga9abd0e2010-10-27 17:18:37 -07001005 if (v != null) {
1006 mDragCenter.set(originX + (v.getWidth() / 2), originY + (v.getHeight() / 2));
1007 } else {
1008 mDragCenter.set(originX, originY);
1009 }
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001010
Adam Cohen2801caf2011-05-13 20:57:39 -07001011 if (dragOutline == null && v == null) {
1012 if (mCrosshairsDrawable != null) {
1013 invalidate();
1014 }
1015 return;
1016 }
1017
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -07001018 if (nearest != null && (nearest[0] != oldDragCellX || nearest[1] != oldDragCellY)) {
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001019 // Find the top left corner of the rect the object will occupy
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001020 final int[] topLeft = mTmpPoint;
1021 cellToPoint(nearest[0], nearest[1], topLeft);
1022
Joe Onorato4be866d2010-10-10 11:26:02 -07001023 int left = topLeft[0];
1024 int top = topLeft[1];
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001025
Winson Chunga9abd0e2010-10-27 17:18:37 -07001026 if (v != null) {
Adam Cohen99e8b402011-03-25 19:23:43 -07001027 // When drawing the drag outline, it did not account for margin offsets
1028 // added by the view's parent.
1029 MarginLayoutParams lp = (MarginLayoutParams) v.getLayoutParams();
1030 left += lp.leftMargin;
1031 top += lp.topMargin;
Winson Chung150fbab2010-09-29 17:14:26 -07001032
Adam Cohen99e8b402011-03-25 19:23:43 -07001033 // Offsets due to the size difference between the View and the dragOutline.
1034 // There is a size difference to account for the outer blur, which may lie
1035 // outside the bounds of the view.
Winson Chunga9abd0e2010-10-27 17:18:37 -07001036 left += (v.getWidth() - dragOutline.getWidth()) / 2;
1037 top += (v.getHeight() - dragOutline.getHeight()) / 2;
Adam Cohen66396872011-04-15 17:50:36 -07001038 } else {
1039 // Center the drag outline in the cell
Winson Chung4b576be2011-04-27 17:40:20 -07001040 left += ((mCellWidth * spanX) + ((spanX - 1) * mWidthGap)
1041 - dragOutline.getWidth()) / 2;
1042 top += ((mCellHeight * spanY) + ((spanY - 1) * mHeightGap)
1043 - dragOutline.getHeight()) / 2;
Winson Chunga9abd0e2010-10-27 17:18:37 -07001044 }
Winson Chung150fbab2010-09-29 17:14:26 -07001045
Joe Onorato4be866d2010-10-10 11:26:02 -07001046 final int oldIndex = mDragOutlineCurrent;
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -07001047 mDragOutlineAnims[oldIndex].animateOut();
1048 mDragOutlineCurrent = (oldIndex + 1) % mDragOutlines.length;
Winson Chung150fbab2010-09-29 17:14:26 -07001049
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -07001050 mDragOutlines[mDragOutlineCurrent].set(left, top);
1051 mDragOutlineAnims[mDragOutlineCurrent].setTag(dragOutline);
1052 mDragOutlineAnims[mDragOutlineCurrent].animateIn();
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001053 }
Patrick Dubroy49250ad2010-10-08 15:33:52 -07001054
1055 // If we are drawing crosshairs, the entire CellLayout needs to be invalidated
1056 if (mCrosshairsDrawable != null) {
1057 invalidate();
1058 }
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001059 }
1060
Adam Cohene0310962011-04-18 16:15:31 -07001061 public void clearDragOutlines() {
1062 final int oldIndex = mDragOutlineCurrent;
1063 mDragOutlineAnims[oldIndex].animateOut();
1064 mDragCell[0] = -1;
1065 mDragCell[1] = -1;
1066 }
1067
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001068 /**
Jeff Sharkey70864282009-04-07 21:08:40 -07001069 * Find a vacant area that will fit the given bounds nearest the requested
1070 * cell location. Uses Euclidean distance to score multiple vacant areas.
Winson Chungaafa03c2010-06-11 17:34:16 -07001071 *
Romain Guy51afc022009-05-04 18:03:43 -07001072 * @param pixelX The X location at which you want to search for a vacant area.
1073 * @param pixelY The Y location at which you want to search for a vacant area.
Jeff Sharkey70864282009-04-07 21:08:40 -07001074 * @param spanX Horizontal span of the object.
1075 * @param spanY Vertical span of the object.
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001076 * @param result Array in which to place the result, or null (in which case a new array will
1077 * be allocated)
Jeff Sharkey70864282009-04-07 21:08:40 -07001078 * @return The X, Y cell of a vacant area that can contain this object,
1079 * nearest the requested location.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001080 */
Michael Jurka6a1435d2010-09-27 17:35:12 -07001081 int[] findNearestVacantArea(
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001082 int pixelX, int pixelY, int spanX, int spanY, int[] result) {
1083 return findNearestVacantArea(pixelX, pixelY, spanX, spanY, null, result);
Michael Jurka6a1435d2010-09-27 17:35:12 -07001084 }
Winson Chungaafa03c2010-06-11 17:34:16 -07001085
Michael Jurka6a1435d2010-09-27 17:35:12 -07001086 /**
1087 * Find a vacant area that will fit the given bounds nearest the requested
1088 * cell location. Uses Euclidean distance to score multiple vacant areas.
1089 *
1090 * @param pixelX The X location at which you want to search for a vacant area.
1091 * @param pixelY The Y location at which you want to search for a vacant area.
1092 * @param spanX Horizontal span of the object.
1093 * @param spanY Vertical span of the object.
Adam Cohendf035382011-04-11 17:22:04 -07001094 * @param ignoreOccupied If true, the result can be an occupied cell
1095 * @param result Array in which to place the result, or null (in which case a new array will
1096 * be allocated)
Michael Jurka6a1435d2010-09-27 17:35:12 -07001097 * @return The X, Y cell of a vacant area that can contain this object,
1098 * nearest the requested location.
1099 */
Adam Cohendf035382011-04-11 17:22:04 -07001100 int[] findNearestArea(int pixelX, int pixelY, int spanX, int spanY, View ignoreView,
1101 boolean ignoreOccupied, int[] result) {
Michael Jurkac6ee42e2010-09-30 12:04:50 -07001102 // mark space take by ignoreView as available (method checks if ignoreView is null)
1103 markCellsAsUnoccupiedForView(ignoreView);
1104
Adam Cohene3e27a82011-04-15 12:07:39 -07001105 // For items with a spanX / spanY > 1, the passed in point (pixelX, pixelY) corresponds
1106 // to the center of the item, but we are searching based on the top-left cell, so
1107 // we translate the point over to correspond to the top-left.
1108 pixelX -= (mCellWidth + mWidthGap) * (spanX - 1) / 2f;
1109 pixelY -= (mCellHeight + mHeightGap) * (spanY - 1) / 2f;
1110
Jeff Sharkey70864282009-04-07 21:08:40 -07001111 // Keep track of best-scoring drop area
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001112 final int[] bestXY = result != null ? result : new int[2];
Jeff Sharkey70864282009-04-07 21:08:40 -07001113 double bestDistance = Double.MAX_VALUE;
Winson Chungaafa03c2010-06-11 17:34:16 -07001114
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001115 final int countX = mCountX;
1116 final int countY = mCountY;
1117 final boolean[][] occupied = mOccupied;
1118
Winson Chungbbc60d82010-11-11 16:34:41 -08001119 for (int y = 0; y < countY - (spanY - 1); y++) {
Michael Jurkac28de512010-08-13 11:27:44 -07001120 inner:
Winson Chungbbc60d82010-11-11 16:34:41 -08001121 for (int x = 0; x < countX - (spanX - 1); x++) {
Adam Cohendf035382011-04-11 17:22:04 -07001122 if (ignoreOccupied) {
1123 for (int i = 0; i < spanX; i++) {
1124 for (int j = 0; j < spanY; j++) {
1125 if (occupied[x + i][y + j]) {
1126 // small optimization: we can skip to after the column we
1127 // just found an occupied cell
1128 x += i;
1129 continue inner;
1130 }
Michael Jurkac28de512010-08-13 11:27:44 -07001131 }
1132 }
1133 }
Winson Chung0be025d2011-05-23 17:45:09 -07001134 final int[] cellXY = mTmpXY;
Adam Cohene3e27a82011-04-15 12:07:39 -07001135 cellToCenterPoint(x, y, cellXY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001136
Michael Jurkac28de512010-08-13 11:27:44 -07001137 double distance = Math.sqrt(Math.pow(cellXY[0] - pixelX, 2)
1138 + Math.pow(cellXY[1] - pixelY, 2));
1139 if (distance <= bestDistance) {
1140 bestDistance = distance;
1141 bestXY[0] = x;
1142 bestXY[1] = y;
1143 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001144 }
1145 }
Michael Jurkac6ee42e2010-09-30 12:04:50 -07001146 // re-mark space taken by ignoreView as occupied
1147 markCellsAsOccupiedForView(ignoreView);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001148
Adam Cohenc0dcf592011-06-01 15:30:43 -07001149 // Return -1, -1 if no suitable location found
1150 if (bestDistance == Double.MAX_VALUE) {
1151 bestXY[0] = -1;
1152 bestXY[1] = -1;
Jeff Sharkey70864282009-04-07 21:08:40 -07001153 }
Adam Cohenc0dcf592011-06-01 15:30:43 -07001154 return bestXY;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001155 }
Winson Chungaafa03c2010-06-11 17:34:16 -07001156
Adam Cohendf035382011-04-11 17:22:04 -07001157 /**
1158 * Find a vacant area that will fit the given bounds nearest the requested
1159 * cell location. Uses Euclidean distance to score multiple vacant areas.
1160 *
1161 * @param pixelX The X location at which you want to search for a vacant area.
1162 * @param pixelY The Y location at which you want to search for a vacant area.
1163 * @param spanX Horizontal span of the object.
1164 * @param spanY Vertical span of the object.
1165 * @param ignoreView Considers space occupied by this view as unoccupied
1166 * @param result Previously returned value to possibly recycle.
1167 * @return The X, Y cell of a vacant area that can contain this object,
1168 * nearest the requested location.
1169 */
1170 int[] findNearestVacantArea(
1171 int pixelX, int pixelY, int spanX, int spanY, View ignoreView, int[] result) {
1172 return findNearestArea(pixelX, pixelY, spanX, spanY, ignoreView, true, result);
1173 }
1174
1175 /**
1176 * Find a starting cell position that will fit the given bounds nearest the requested
1177 * cell location. Uses Euclidean distance to score multiple vacant areas.
1178 *
1179 * @param pixelX The X location at which you want to search for a vacant area.
1180 * @param pixelY The Y location at which you want to search for a vacant area.
1181 * @param spanX Horizontal span of the object.
1182 * @param spanY Vertical span of the object.
1183 * @param ignoreView Considers space occupied by this view as unoccupied
1184 * @param result Previously returned value to possibly recycle.
1185 * @return The X, Y cell of a vacant area that can contain this object,
1186 * nearest the requested location.
1187 */
1188 int[] findNearestArea(
1189 int pixelX, int pixelY, int spanX, int spanY, int[] result) {
1190 return findNearestArea(pixelX, pixelY, spanX, spanY, null, false, result);
1191 }
1192
Michael Jurka0280c3b2010-09-17 15:00:07 -07001193 boolean existsEmptyCell() {
1194 return findCellForSpan(null, 1, 1);
1195 }
1196
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001197 /**
Michael Jurka0280c3b2010-09-17 15:00:07 -07001198 * Finds the upper-left coordinate of the first rectangle in the grid that can
1199 * hold a cell of the specified dimensions. If intersectX and intersectY are not -1,
1200 * then this method will only return coordinates for rectangles that contain the cell
1201 * (intersectX, intersectY)
1202 *
1203 * @param cellXY The array that will contain the position of a vacant cell if such a cell
1204 * can be found.
1205 * @param spanX The horizontal span of the cell we want to find.
1206 * @param spanY The vertical span of the cell we want to find.
1207 *
1208 * @return True if a vacant cell of the specified dimension was found, false otherwise.
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001209 */
Michael Jurka0280c3b2010-09-17 15:00:07 -07001210 boolean findCellForSpan(int[] cellXY, int spanX, int spanY) {
1211 return findCellForSpanThatIntersectsIgnoring(cellXY, spanX, spanY, -1, -1, null);
1212 }
1213
1214 /**
1215 * Like above, but ignores any cells occupied by the item "ignoreView"
1216 *
1217 * @param cellXY The array that will contain the position of a vacant cell if such a cell
1218 * can be found.
1219 * @param spanX The horizontal span of the cell we want to find.
1220 * @param spanY The vertical span of the cell we want to find.
1221 * @param ignoreView The home screen item we should treat as not occupying any space
1222 * @return
1223 */
1224 boolean findCellForSpanIgnoring(int[] cellXY, int spanX, int spanY, View ignoreView) {
1225 return findCellForSpanThatIntersectsIgnoring(cellXY, spanX, spanY, -1, -1, ignoreView);
1226 }
1227
1228 /**
1229 * Like above, but if intersectX and intersectY are not -1, then this method will try to
1230 * return coordinates for rectangles that contain the cell [intersectX, intersectY]
1231 *
1232 * @param spanX The horizontal span of the cell we want to find.
1233 * @param spanY The vertical span of the cell we want to find.
1234 * @param ignoreView The home screen item we should treat as not occupying any space
1235 * @param intersectX The X coordinate of the cell that we should try to overlap
1236 * @param intersectX The Y coordinate of the cell that we should try to overlap
1237 *
1238 * @return True if a vacant cell of the specified dimension was found, false otherwise.
1239 */
1240 boolean findCellForSpanThatIntersects(int[] cellXY, int spanX, int spanY,
1241 int intersectX, int intersectY) {
1242 return findCellForSpanThatIntersectsIgnoring(
1243 cellXY, spanX, spanY, intersectX, intersectY, null);
1244 }
1245
1246 /**
1247 * The superset of the above two methods
1248 */
1249 boolean findCellForSpanThatIntersectsIgnoring(int[] cellXY, int spanX, int spanY,
1250 int intersectX, int intersectY, View ignoreView) {
Michael Jurkac6ee42e2010-09-30 12:04:50 -07001251 // mark space take by ignoreView as available (method checks if ignoreView is null)
1252 markCellsAsUnoccupiedForView(ignoreView);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001253
Michael Jurka28750fb2010-09-24 17:43:49 -07001254 boolean foundCell = false;
Michael Jurka0280c3b2010-09-17 15:00:07 -07001255 while (true) {
1256 int startX = 0;
1257 if (intersectX >= 0) {
1258 startX = Math.max(startX, intersectX - (spanX - 1));
1259 }
1260 int endX = mCountX - (spanX - 1);
1261 if (intersectX >= 0) {
1262 endX = Math.min(endX, intersectX + (spanX - 1) + (spanX == 1 ? 1 : 0));
1263 }
1264 int startY = 0;
1265 if (intersectY >= 0) {
1266 startY = Math.max(startY, intersectY - (spanY - 1));
1267 }
1268 int endY = mCountY - (spanY - 1);
1269 if (intersectY >= 0) {
1270 endY = Math.min(endY, intersectY + (spanY - 1) + (spanY == 1 ? 1 : 0));
1271 }
1272
Winson Chungbbc60d82010-11-11 16:34:41 -08001273 for (int y = startY; y < endY && !foundCell; y++) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001274 inner:
Winson Chungbbc60d82010-11-11 16:34:41 -08001275 for (int x = startX; x < endX; x++) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001276 for (int i = 0; i < spanX; i++) {
1277 for (int j = 0; j < spanY; j++) {
1278 if (mOccupied[x + i][y + j]) {
Winson Chungbbc60d82010-11-11 16:34:41 -08001279 // small optimization: we can skip to after the column we just found
Michael Jurka0280c3b2010-09-17 15:00:07 -07001280 // an occupied cell
Winson Chungbbc60d82010-11-11 16:34:41 -08001281 x += i;
Michael Jurka0280c3b2010-09-17 15:00:07 -07001282 continue inner;
1283 }
1284 }
1285 }
1286 if (cellXY != null) {
1287 cellXY[0] = x;
1288 cellXY[1] = y;
1289 }
Michael Jurka28750fb2010-09-24 17:43:49 -07001290 foundCell = true;
1291 break;
Michael Jurka0280c3b2010-09-17 15:00:07 -07001292 }
1293 }
1294 if (intersectX == -1 && intersectY == -1) {
1295 break;
1296 } else {
1297 // if we failed to find anything, try again but without any requirements of
1298 // intersecting
1299 intersectX = -1;
1300 intersectY = -1;
1301 continue;
1302 }
1303 }
1304
Michael Jurkac6ee42e2010-09-30 12:04:50 -07001305 // re-mark space taken by ignoreView as occupied
1306 markCellsAsOccupiedForView(ignoreView);
Michael Jurka28750fb2010-09-24 17:43:49 -07001307 return foundCell;
Michael Jurka0280c3b2010-09-17 15:00:07 -07001308 }
1309
1310 /**
1311 * Called when drag has left this CellLayout or has been completed (successfully or not)
1312 */
1313 void onDragExit() {
Joe Onorato4be866d2010-10-10 11:26:02 -07001314 // This can actually be called when we aren't in a drag, e.g. when adding a new
1315 // item to this layout via the customize drawer.
1316 // Guard against that case.
1317 if (mDragging) {
1318 mDragging = false;
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001319
Joe Onorato4be866d2010-10-10 11:26:02 -07001320 // Fade out the drag indicators
1321 if (mCrosshairsAnimator != null) {
1322 mCrosshairsAnimator.animateOut();
1323 }
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001324 }
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -07001325
1326 // Invalidate the drag data
1327 mDragCell[0] = -1;
1328 mDragCell[1] = -1;
1329 mDragOutlineAnims[mDragOutlineCurrent].animateOut();
1330 mDragOutlineCurrent = (mDragOutlineCurrent + 1) % mDragOutlineAnims.length;
1331
Michael Jurka33945b22010-12-21 18:19:38 -08001332 setIsDragOverlapping(false);
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001333 }
1334
1335 /**
Winson Chungaafa03c2010-06-11 17:34:16 -07001336 * Mark a child as having been dropped.
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001337 * At the beginning of the drag operation, the child may have been on another
Patrick Dubroyce34a972010-10-19 10:34:32 -07001338 * screen, but it is re-parented before this method is called.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001339 *
1340 * @param child The child that is being dropped
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001341 */
Michael Jurkad3ef3062010-11-23 16:23:58 -08001342 void onDropChild(View child, boolean animate) {
Romain Guyd94533d2009-08-17 10:01:15 -07001343 if (child != null) {
1344 LayoutParams lp = (LayoutParams) child.getLayoutParams();
Romain Guyd94533d2009-08-17 10:01:15 -07001345 lp.isDragging = false;
Romain Guy84f296c2009-11-04 15:00:44 -08001346 lp.dropped = true;
Michael Jurkad3ef3062010-11-23 16:23:58 -08001347 lp.animateDrop = animate;
Patrick Dubroye3887cc2011-01-20 10:43:40 -08001348 child.setVisibility(animate ? View.INVISIBLE : View.VISIBLE);
Romain Guyd94533d2009-08-17 10:01:15 -07001349 child.requestLayout();
Romain Guyd94533d2009-08-17 10:01:15 -07001350 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001351 }
1352
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001353 /**
1354 * Start dragging the specified child
Winson Chungaafa03c2010-06-11 17:34:16 -07001355 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001356 * @param child The child that is being dragged
1357 */
1358 void onDragChild(View child) {
1359 LayoutParams lp = (LayoutParams) child.getLayoutParams();
1360 lp.isDragging = true;
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001361 }
1362
1363 /**
1364 * A drag event has begun over this layout.
1365 * It may have begun over this layout (in which case onDragChild is called first),
1366 * or it may have begun on another layout.
1367 */
Winson Chunga9abd0e2010-10-27 17:18:37 -07001368 void onDragEnter() {
Patrick Dubroyfe6bd872010-10-13 17:32:10 -07001369 if (!mDragging) {
Patrick Dubroyfe6bd872010-10-13 17:32:10 -07001370 // Fade in the drag indicators
1371 if (mCrosshairsAnimator != null) {
1372 mCrosshairsAnimator.animateIn();
1373 }
Joe Onorato4be866d2010-10-10 11:26:02 -07001374 }
1375 mDragging = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001376 }
Winson Chungaafa03c2010-06-11 17:34:16 -07001377
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001378 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001379 * Computes a bounding rectangle for a range of cells
Winson Chungaafa03c2010-06-11 17:34:16 -07001380 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001381 * @param cellX X coordinate of upper left corner expressed as a cell position
1382 * @param cellY Y coordinate of upper left corner expressed as a cell position
Winson Chungaafa03c2010-06-11 17:34:16 -07001383 * @param cellHSpan Width in cells
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001384 * @param cellVSpan Height in cells
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001385 * @param resultRect Rect into which to put the results
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001386 */
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001387 public void cellToRect(int cellX, int cellY, int cellHSpan, int cellVSpan, RectF resultRect) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001388 final int cellWidth = mCellWidth;
1389 final int cellHeight = mCellHeight;
1390 final int widthGap = mWidthGap;
1391 final int heightGap = mHeightGap;
Winson Chungaafa03c2010-06-11 17:34:16 -07001392
1393 final int hStartPadding = getLeftPadding();
1394 final int vStartPadding = getTopPadding();
1395
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001396 int width = cellHSpan * cellWidth + ((cellHSpan - 1) * widthGap);
1397 int height = cellVSpan * cellHeight + ((cellVSpan - 1) * heightGap);
1398
1399 int x = hStartPadding + cellX * (cellWidth + widthGap);
1400 int y = vStartPadding + cellY * (cellHeight + heightGap);
Winson Chungaafa03c2010-06-11 17:34:16 -07001401
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001402 resultRect.set(x, y, x + width, y + height);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001403 }
Winson Chungaafa03c2010-06-11 17:34:16 -07001404
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001405 /**
Winson Chungaafa03c2010-06-11 17:34:16 -07001406 * Computes the required horizontal and vertical cell spans to always
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001407 * fit the given rectangle.
Winson Chungaafa03c2010-06-11 17:34:16 -07001408 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001409 * @param width Width in pixels
1410 * @param height Height in pixels
Patrick Dubroy8f86ddc2010-07-16 13:55:32 -07001411 * @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 -08001412 */
Patrick Dubroy8f86ddc2010-07-16 13:55:32 -07001413 public int[] rectToCell(int width, int height, int[] result) {
Michael Jurka9987a5c2010-10-08 16:58:12 -07001414 return rectToCell(getResources(), width, height, result);
1415 }
1416
1417 public static int[] rectToCell(Resources resources, int width, int height, int[] result) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001418 // Always assume we're working with the smallest span to make sure we
1419 // reserve enough space in both orientations.
Joe Onorato79e56262009-09-21 15:23:04 -04001420 int actualWidth = resources.getDimensionPixelSize(R.dimen.workspace_cell_width);
1421 int actualHeight = resources.getDimensionPixelSize(R.dimen.workspace_cell_height);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001422 int smallerSize = Math.min(actualWidth, actualHeight);
Joe Onorato79e56262009-09-21 15:23:04 -04001423
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001424 // Always round up to next largest cell
1425 int spanX = (width + smallerSize) / smallerSize;
1426 int spanY = (height + smallerSize) / smallerSize;
Joe Onorato79e56262009-09-21 15:23:04 -04001427
Patrick Dubroy8f86ddc2010-07-16 13:55:32 -07001428 if (result == null) {
1429 return new int[] { spanX, spanY };
1430 }
1431 result[0] = spanX;
1432 result[1] = spanY;
1433 return result;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001434 }
1435
Michael Jurkaf12c75c2011-01-25 22:41:40 -08001436 public int[] cellSpansToSize(int hSpans, int vSpans) {
1437 int[] size = new int[2];
1438 size[0] = hSpans * mCellWidth + (hSpans - 1) * mWidthGap;
1439 size[1] = vSpans * mCellHeight + (vSpans - 1) * mHeightGap;
1440 return size;
1441 }
1442
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001443 /**
Patrick Dubroy047379a2010-12-19 22:02:04 -08001444 * Calculate the grid spans needed to fit given item
1445 */
1446 public void calculateSpans(ItemInfo info) {
1447 final int minWidth;
1448 final int minHeight;
1449
1450 if (info instanceof LauncherAppWidgetInfo) {
1451 minWidth = ((LauncherAppWidgetInfo) info).minWidth;
1452 minHeight = ((LauncherAppWidgetInfo) info).minHeight;
1453 } else if (info instanceof PendingAddWidgetInfo) {
1454 minWidth = ((PendingAddWidgetInfo) info).minWidth;
1455 minHeight = ((PendingAddWidgetInfo) info).minHeight;
1456 } else {
1457 // It's not a widget, so it must be 1x1
1458 info.spanX = info.spanY = 1;
1459 return;
1460 }
1461 int[] spans = rectToCell(minWidth, minHeight, null);
1462 info.spanX = spans[0];
1463 info.spanY = spans[1];
1464 }
1465
1466 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001467 * Find the first vacant cell, if there is one.
1468 *
1469 * @param vacant Holds the x and y coordinate of the vacant cell
1470 * @param spanX Horizontal cell span.
1471 * @param spanY Vertical cell span.
Winson Chungaafa03c2010-06-11 17:34:16 -07001472 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001473 * @return True if a vacant cell was found
1474 */
1475 public boolean getVacantCell(int[] vacant, int spanX, int spanY) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001476
Michael Jurka0280c3b2010-09-17 15:00:07 -07001477 return findVacantCell(vacant, spanX, spanY, mCountX, mCountY, mOccupied);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001478 }
1479
1480 static boolean findVacantCell(int[] vacant, int spanX, int spanY,
1481 int xCount, int yCount, boolean[][] occupied) {
1482
Adam Cohen2801caf2011-05-13 20:57:39 -07001483 for (int y = 0; y < yCount; y++) {
1484 for (int x = 0; x < xCount; x++) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001485 boolean available = !occupied[x][y];
1486out: for (int i = x; i < x + spanX - 1 && x < xCount; i++) {
1487 for (int j = y; j < y + spanY - 1 && y < yCount; j++) {
1488 available = available && !occupied[i][j];
1489 if (!available) break out;
1490 }
1491 }
1492
1493 if (available) {
1494 vacant[0] = x;
1495 vacant[1] = y;
1496 return true;
1497 }
1498 }
1499 }
1500
1501 return false;
1502 }
1503
Michael Jurka0280c3b2010-09-17 15:00:07 -07001504 private void clearOccupiedCells() {
1505 for (int x = 0; x < mCountX; x++) {
1506 for (int y = 0; y < mCountY; y++) {
1507 mOccupied[x][y] = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001508 }
1509 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001510 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001511
Adam Cohen1b607ed2011-03-03 17:26:50 -08001512 /**
1513 * Given a view, determines how much that view can be expanded in all directions, in terms of
1514 * whether or not there are other items occupying adjacent cells. Used by the
1515 * AppWidgetResizeFrame to determine how the widget can be resized.
1516 */
Adam Cohend4844c32011-02-18 19:25:06 -08001517 public void getExpandabilityArrayForView(View view, int[] expandability) {
Adam Cohen1b607ed2011-03-03 17:26:50 -08001518 final LayoutParams lp = (LayoutParams) view.getLayoutParams();
Adam Cohend4844c32011-02-18 19:25:06 -08001519 boolean flag;
1520
Adam Cohen1b607ed2011-03-03 17:26:50 -08001521 expandability[AppWidgetResizeFrame.LEFT] = 0;
Adam Cohend4844c32011-02-18 19:25:06 -08001522 for (int x = lp.cellX - 1; x >= 0; x--) {
1523 flag = false;
1524 for (int y = lp.cellY; y < lp.cellY + lp.cellVSpan; y++) {
1525 if (mOccupied[x][y]) flag = true;
1526 }
1527 if (flag) break;
Adam Cohen1b607ed2011-03-03 17:26:50 -08001528 expandability[AppWidgetResizeFrame.LEFT]++;
Adam Cohend4844c32011-02-18 19:25:06 -08001529 }
1530
Adam Cohen1b607ed2011-03-03 17:26:50 -08001531 expandability[AppWidgetResizeFrame.TOP] = 0;
Adam Cohend4844c32011-02-18 19:25:06 -08001532 for (int y = lp.cellY - 1; y >= 0; y--) {
1533 flag = false;
1534 for (int x = lp.cellX; x < lp.cellX + lp.cellHSpan; x++) {
1535 if (mOccupied[x][y]) flag = true;
1536 }
1537 if (flag) break;
Adam Cohen1b607ed2011-03-03 17:26:50 -08001538 expandability[AppWidgetResizeFrame.TOP]++;
1539 }
Adam Cohend4844c32011-02-18 19:25:06 -08001540
Adam Cohen1b607ed2011-03-03 17:26:50 -08001541 expandability[AppWidgetResizeFrame.RIGHT] = 0;
Adam Cohend4844c32011-02-18 19:25:06 -08001542 for (int x = lp.cellX + lp.cellHSpan; x < mCountX; x++) {
1543 flag = false;
1544 for (int y = lp.cellY; y < lp.cellY + lp.cellVSpan; y++) {
1545 if (mOccupied[x][y]) flag = true;
1546 }
1547 if (flag) break;
Adam Cohen1b607ed2011-03-03 17:26:50 -08001548 expandability[AppWidgetResizeFrame.RIGHT]++;
1549 }
Adam Cohend4844c32011-02-18 19:25:06 -08001550
Adam Cohen1b607ed2011-03-03 17:26:50 -08001551 expandability[AppWidgetResizeFrame.BOTTOM] = 0;
Adam Cohend4844c32011-02-18 19:25:06 -08001552 for (int y = lp.cellY + lp.cellVSpan; y < mCountY; y++) {
1553 flag = false;
1554 for (int x = lp.cellX; x < lp.cellX + lp.cellHSpan; x++) {
1555 if (mOccupied[x][y]) flag = true;
1556 }
1557 if (flag) break;
Adam Cohen1b607ed2011-03-03 17:26:50 -08001558 expandability[AppWidgetResizeFrame.BOTTOM]++;
1559 }
Adam Cohend4844c32011-02-18 19:25:06 -08001560 }
1561
Michael Jurka0280c3b2010-09-17 15:00:07 -07001562 public void onMove(View view, int newCellX, int newCellY) {
1563 LayoutParams lp = (LayoutParams) view.getLayoutParams();
1564 markCellsAsUnoccupiedForView(view);
1565 markCellsForView(newCellX, newCellY, lp.cellHSpan, lp.cellVSpan, true);
1566 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001567
Adam Cohend4844c32011-02-18 19:25:06 -08001568 public void markCellsAsOccupiedForView(View view) {
Michael Jurka8c920dd2011-01-20 14:16:56 -08001569 if (view == null || view.getParent() != mChildren) return;
Michael Jurka0280c3b2010-09-17 15:00:07 -07001570 LayoutParams lp = (LayoutParams) view.getLayoutParams();
1571 markCellsForView(lp.cellX, lp.cellY, lp.cellHSpan, lp.cellVSpan, true);
1572 }
1573
Adam Cohend4844c32011-02-18 19:25:06 -08001574 public void markCellsAsUnoccupiedForView(View view) {
Michael Jurka8c920dd2011-01-20 14:16:56 -08001575 if (view == null || view.getParent() != mChildren) return;
Michael Jurka0280c3b2010-09-17 15:00:07 -07001576 LayoutParams lp = (LayoutParams) view.getLayoutParams();
1577 markCellsForView(lp.cellX, lp.cellY, lp.cellHSpan, lp.cellVSpan, false);
1578 }
1579
1580 private void markCellsForView(int cellX, int cellY, int spanX, int spanY, boolean value) {
1581 for (int x = cellX; x < cellX + spanX && x < mCountX; x++) {
1582 for (int y = cellY; y < cellY + spanY && y < mCountY; y++) {
1583 mOccupied[x][y] = value;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001584 }
1585 }
1586 }
1587
Adam Cohen2801caf2011-05-13 20:57:39 -07001588 public int getDesiredWidth() {
1589 return mLeftPadding + mRightPadding + (mCountX * mCellWidth) +
1590 (Math.max((mCountX - 1), 0) * mWidthGap);
1591 }
1592
1593 public int getDesiredHeight() {
1594 return mTopPadding + mBottomPadding + (mCountY * mCellHeight) +
1595 (Math.max((mCountY - 1), 0) * mHeightGap);
1596 }
1597
Michael Jurka66d72172011-04-12 16:29:25 -07001598 public boolean isOccupied(int x, int y) {
1599 if (x < mCountX && y < mCountY) {
1600 return mOccupied[x][y];
1601 } else {
1602 throw new RuntimeException("Position exceeds the bound of this CellLayout");
1603 }
1604 }
1605
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001606 @Override
1607 public ViewGroup.LayoutParams generateLayoutParams(AttributeSet attrs) {
1608 return new CellLayout.LayoutParams(getContext(), attrs);
1609 }
1610
1611 @Override
1612 protected boolean checkLayoutParams(ViewGroup.LayoutParams p) {
1613 return p instanceof CellLayout.LayoutParams;
1614 }
1615
1616 @Override
1617 protected ViewGroup.LayoutParams generateLayoutParams(ViewGroup.LayoutParams p) {
1618 return new CellLayout.LayoutParams(p);
1619 }
1620
Winson Chungaafa03c2010-06-11 17:34:16 -07001621 public static class CellLayoutAnimationController extends LayoutAnimationController {
1622 public CellLayoutAnimationController(Animation animation, float delay) {
1623 super(animation, delay);
1624 }
1625
1626 @Override
1627 protected long getDelayForView(View view) {
1628 return (int) (Math.random() * 150);
1629 }
1630 }
1631
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001632 public static class LayoutParams extends ViewGroup.MarginLayoutParams {
1633 /**
1634 * Horizontal location of the item in the grid.
1635 */
1636 @ViewDebug.ExportedProperty
1637 public int cellX;
1638
1639 /**
1640 * Vertical location of the item in the grid.
1641 */
1642 @ViewDebug.ExportedProperty
1643 public int cellY;
1644
1645 /**
1646 * Number of cells spanned horizontally by the item.
1647 */
1648 @ViewDebug.ExportedProperty
1649 public int cellHSpan;
1650
1651 /**
1652 * Number of cells spanned vertically by the item.
1653 */
1654 @ViewDebug.ExportedProperty
1655 public int cellVSpan;
Winson Chungaafa03c2010-06-11 17:34:16 -07001656
Adam Cohen1b607ed2011-03-03 17:26:50 -08001657 /**
1658 * Indicates whether the item will set its x, y, width and height parameters freely,
1659 * or whether these will be computed based on cellX, cellY, cellHSpan and cellVSpan.
1660 */
Adam Cohend4844c32011-02-18 19:25:06 -08001661 public boolean isLockedToGrid = true;
1662
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001663 /**
1664 * Is this item currently being dragged
1665 */
1666 public boolean isDragging;
1667
1668 // X coordinate of the view in the layout.
1669 @ViewDebug.ExportedProperty
1670 int x;
1671 // Y coordinate of the view in the layout.
1672 @ViewDebug.ExportedProperty
1673 int y;
1674
Patrick Dubroyce34a972010-10-19 10:34:32 -07001675 /**
1676 * The old X coordinate of this item, relative to its current parent.
1677 * Used to animate the item into its new position.
1678 */
1679 int oldX;
1680
1681 /**
1682 * The old Y coordinate of this item, relative to its current parent.
1683 * Used to animate the item into its new position.
1684 */
1685 int oldY;
1686
Romain Guy84f296c2009-11-04 15:00:44 -08001687 boolean dropped;
Romain Guyfcb9e712009-10-02 16:06:52 -07001688
Michael Jurkad3ef3062010-11-23 16:23:58 -08001689 boolean animateDrop;
1690
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001691 public LayoutParams(Context c, AttributeSet attrs) {
1692 super(c, attrs);
1693 cellHSpan = 1;
1694 cellVSpan = 1;
1695 }
1696
1697 public LayoutParams(ViewGroup.LayoutParams source) {
1698 super(source);
1699 cellHSpan = 1;
1700 cellVSpan = 1;
1701 }
Winson Chungaafa03c2010-06-11 17:34:16 -07001702
1703 public LayoutParams(LayoutParams source) {
1704 super(source);
1705 this.cellX = source.cellX;
1706 this.cellY = source.cellY;
1707 this.cellHSpan = source.cellHSpan;
1708 this.cellVSpan = source.cellVSpan;
1709 }
1710
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001711 public LayoutParams(int cellX, int cellY, int cellHSpan, int cellVSpan) {
Romain Guy8f19cdd2010-01-08 15:07:00 -08001712 super(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001713 this.cellX = cellX;
1714 this.cellY = cellY;
1715 this.cellHSpan = cellHSpan;
1716 this.cellVSpan = cellVSpan;
1717 }
1718
Adam Cohen7f4eabe2011-04-21 16:19:16 -07001719 public void setup(int cellWidth, int cellHeight, int widthGap, int heightGap) {
Adam Cohend4844c32011-02-18 19:25:06 -08001720 if (isLockedToGrid) {
1721 final int myCellHSpan = cellHSpan;
1722 final int myCellVSpan = cellVSpan;
1723 final int myCellX = cellX;
1724 final int myCellY = cellY;
Adam Cohen1b607ed2011-03-03 17:26:50 -08001725
Adam Cohend4844c32011-02-18 19:25:06 -08001726 width = myCellHSpan * cellWidth + ((myCellHSpan - 1) * widthGap) -
1727 leftMargin - rightMargin;
1728 height = myCellVSpan * cellHeight + ((myCellVSpan - 1) * heightGap) -
1729 topMargin - bottomMargin;
Adam Cohen7f4eabe2011-04-21 16:19:16 -07001730 x = myCellX * (cellWidth + widthGap) + leftMargin;
1731 y = myCellY * (cellHeight + heightGap) + topMargin;
Adam Cohend4844c32011-02-18 19:25:06 -08001732 }
1733 }
Winson Chungaafa03c2010-06-11 17:34:16 -07001734
Winson Chungaafa03c2010-06-11 17:34:16 -07001735 public String toString() {
1736 return "(" + this.cellX + ", " + this.cellY + ")";
1737 }
Adam Cohen7f4eabe2011-04-21 16:19:16 -07001738
1739 public void setWidth(int width) {
1740 this.width = width;
1741 }
1742
1743 public int getWidth() {
1744 return width;
1745 }
1746
1747 public void setHeight(int height) {
1748 this.height = height;
1749 }
1750
1751 public int getHeight() {
1752 return height;
1753 }
1754
1755 public void setX(int x) {
1756 this.x = x;
1757 }
1758
1759 public int getX() {
1760 return x;
1761 }
1762
1763 public void setY(int y) {
1764 this.y = y;
1765 }
1766
1767 public int getY() {
1768 return y;
1769 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001770 }
1771
Michael Jurka0280c3b2010-09-17 15:00:07 -07001772 // This class stores info for two purposes:
1773 // 1. When dragging items (mDragInfo in Workspace), we store the View, its cellX & cellY,
1774 // its spanX, spanY, and the screen it is on
1775 // 2. When long clicking on an empty cell in a CellLayout, we save information about the
1776 // cellX and cellY coordinates and which page was clicked. We then set this as a tag on
1777 // the CellLayout that was long clicked
Michael Jurkae5fb0f22011-04-11 13:27:46 -07001778 static final class CellInfo {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001779 View cell;
Michael Jurkaa63c4522010-08-19 13:52:27 -07001780 int cellX = -1;
1781 int cellY = -1;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001782 int spanX;
1783 int spanY;
1784 int screen;
1785 boolean valid;
1786
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001787 @Override
1788 public String toString() {
Winson Chungaafa03c2010-06-11 17:34:16 -07001789 return "Cell[view=" + (cell == null ? "null" : cell.getClass())
1790 + ", x=" + cellX + ", y=" + cellY + "]";
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001791 }
1792 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001793}