blob: 0d026b8aa1950ae618f474c2c397f1c6c2629323 [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 Jurkabdb5c532011-02-01 15:05:06 -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 Dubroy3499d8c2011-03-10 17:17:23 -0800116 private BubbleTextView mPressedOrFocusedIcon;
117
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700118 private Drawable mCrosshairsDrawable = null;
Patrick Dubroy49250ad2010-10-08 15:33:52 -0700119 private InterruptibleInOutAnimator mCrosshairsAnimator = null;
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700120 private float mCrosshairsVisibility = 0.0f;
121
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700122 // When a drag operation is in progress, holds the nearest cell to the touch point
123 private final int[] mDragCell = new int[2];
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800124
Joe Onorato4be866d2010-10-10 11:26:02 -0700125 private boolean mDragging = false;
126
Patrick Dubroyce34a972010-10-19 10:34:32 -0700127 private TimeInterpolator mEaseOutInterpolator;
Michael Jurka8c920dd2011-01-20 14:16:56 -0800128 private CellLayoutChildren mChildren;
Patrick Dubroyce34a972010-10-19 10:34:32 -0700129
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800130 public CellLayout(Context context) {
131 this(context, null);
132 }
133
134 public CellLayout(Context context, AttributeSet attrs) {
135 this(context, attrs, 0);
136 }
137
138 public CellLayout(Context context, AttributeSet attrs, int defStyle) {
139 super(context, attrs, defStyle);
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700140
141 // A ViewGroup usually does not draw, but CellLayout needs to draw a rectangle to show
142 // the user where a dragged item will land when dropped.
143 setWillNotDraw(false);
Michael Jurkaa63c4522010-08-19 13:52:27 -0700144
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800145 TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.CellLayout, defStyle, 0);
146
147 mCellWidth = a.getDimensionPixelSize(R.styleable.CellLayout_cellWidth, 10);
148 mCellHeight = a.getDimensionPixelSize(R.styleable.CellLayout_cellHeight, 10);
Winson Chungece7f5b2010-10-22 14:54:12 -0700149 mWidthGap = a.getDimensionPixelSize(R.styleable.CellLayout_widthGap, -1);
150 mHeightGap = a.getDimensionPixelSize(R.styleable.CellLayout_heightGap, -1);
Winson Chungaafa03c2010-06-11 17:34:16 -0700151
Adam Cohend22015c2010-07-26 22:02:18 -0700152 mLeftPadding =
153 a.getDimensionPixelSize(R.styleable.CellLayout_xAxisStartPadding, 10);
154 mRightPadding =
155 a.getDimensionPixelSize(R.styleable.CellLayout_xAxisEndPadding, 10);
156 mTopPadding =
157 a.getDimensionPixelSize(R.styleable.CellLayout_yAxisStartPadding, 10);
158 mBottomPadding =
159 a.getDimensionPixelSize(R.styleable.CellLayout_yAxisEndPadding, 10);
Winson Chungaafa03c2010-06-11 17:34:16 -0700160
Adam Cohend22015c2010-07-26 22:02:18 -0700161 mCountX = LauncherModel.getCellCountX();
162 mCountY = LauncherModel.getCellCountY();
Michael Jurka0280c3b2010-09-17 15:00:07 -0700163 mOccupied = new boolean[mCountX][mCountY];
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800164
165 a.recycle();
166
167 setAlwaysDrawnWithCacheEnabled(false);
168
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700169 final Resources res = getResources();
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700170
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700171 if (LauncherApplication.isScreenXLarge()) {
Michael Jurka33945b22010-12-21 18:19:38 -0800172 mNormalBackground = res.getDrawable(R.drawable.homescreen_large_blue);
Michael Jurka33945b22010-12-21 18:19:38 -0800173 mActiveBackground = res.getDrawable(R.drawable.homescreen_large_green);
174 mActiveGlowBackground = res.getDrawable(R.drawable.homescreen_large_green_strong);
175
176 mNormalBackgroundMini = res.getDrawable(R.drawable.homescreen_small_blue);
177 mNormalGlowBackgroundMini = res.getDrawable(R.drawable.homescreen_small_blue_strong);
178 mActiveBackgroundMini = res.getDrawable(R.drawable.homescreen_small_green);
179 mActiveGlowBackgroundMini = res.getDrawable(R.drawable.homescreen_small_green_strong);
180
181 mNormalBackground.setFilterBitmap(true);
Michael Jurka33945b22010-12-21 18:19:38 -0800182 mActiveBackground.setFilterBitmap(true);
183 mActiveGlowBackground.setFilterBitmap(true);
184 mNormalBackgroundMini.setFilterBitmap(true);
185 mNormalGlowBackgroundMini.setFilterBitmap(true);
186 mActiveBackgroundMini.setFilterBitmap(true);
187 mActiveGlowBackgroundMini.setFilterBitmap(true);
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700188 }
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700189
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700190 // Initialize the data structures used for the drag visualization.
Winson Chung150fbab2010-09-29 17:14:26 -0700191
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700192 mCrosshairsDrawable = res.getDrawable(R.drawable.gardening_crosshairs);
Patrick Dubroyce34a972010-10-19 10:34:32 -0700193 mEaseOutInterpolator = new DecelerateInterpolator(2.5f); // Quint ease out
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700194
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700195 // Set up the animation for fading the crosshairs in and out
196 int animDuration = res.getInteger(R.integer.config_crosshairsFadeInTime);
Patrick Dubroy49250ad2010-10-08 15:33:52 -0700197 mCrosshairsAnimator = new InterruptibleInOutAnimator(animDuration, 0.0f, 1.0f);
Chet Haase472b2812010-10-14 07:02:04 -0700198 mCrosshairsAnimator.getAnimator().addUpdateListener(new AnimatorUpdateListener() {
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700199 public void onAnimationUpdate(ValueAnimator animation) {
200 mCrosshairsVisibility = ((Float) animation.getAnimatedValue()).floatValue();
Patrick Dubroy8e58e912010-10-14 13:21:48 -0700201 invalidate();
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700202 }
203 });
Patrick Dubroyce34a972010-10-19 10:34:32 -0700204 mCrosshairsAnimator.getAnimator().setInterpolator(mEaseOutInterpolator);
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700205
Joe Onorato4be866d2010-10-10 11:26:02 -0700206 for (int i = 0; i < mDragOutlines.length; i++) {
207 mDragOutlines[i] = new Point(-1, -1);
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700208 }
209
210 // When dragging things around the home screens, we show a green outline of
211 // where the item will land. The outlines gradually fade out, leaving a trail
212 // behind the drag path.
213 // Set up all the animations that are used to implement this fading.
214 final int duration = res.getInteger(R.integer.config_dragOutlineFadeTime);
Chet Haase472b2812010-10-14 07:02:04 -0700215 final float fromAlphaValue = 0;
216 final float toAlphaValue = (float)res.getInteger(R.integer.config_dragOutlineMaxAlpha);
Joe Onorato4be866d2010-10-10 11:26:02 -0700217
Patrick Dubroy8e58e912010-10-14 13:21:48 -0700218 Arrays.fill(mDragOutlineAlphas, fromAlphaValue);
Joe Onorato4be866d2010-10-10 11:26:02 -0700219
220 for (int i = 0; i < mDragOutlineAnims.length; i++) {
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700221 final InterruptibleInOutAnimator anim =
222 new InterruptibleInOutAnimator(duration, fromAlphaValue, toAlphaValue);
Patrick Dubroyce34a972010-10-19 10:34:32 -0700223 anim.getAnimator().setInterpolator(mEaseOutInterpolator);
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700224 final int thisIndex = i;
Chet Haase472b2812010-10-14 07:02:04 -0700225 anim.getAnimator().addUpdateListener(new AnimatorUpdateListener() {
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700226 public void onAnimationUpdate(ValueAnimator animation) {
Joe Onorato4be866d2010-10-10 11:26:02 -0700227 final Bitmap outline = (Bitmap)anim.getTag();
228
229 // If an animation is started and then stopped very quickly, we can still
230 // get spurious updates we've cleared the tag. Guard against this.
231 if (outline == null) {
Patrick Dubroyfe6bd872010-10-13 17:32:10 -0700232 if (false) {
233 Object val = animation.getAnimatedValue();
234 Log.d(TAG, "anim " + thisIndex + " update: " + val +
235 ", isStopped " + anim.isStopped());
236 }
Joe Onorato4be866d2010-10-10 11:26:02 -0700237 // Try to prevent it from continuing to run
238 animation.cancel();
239 } else {
Chet Haase472b2812010-10-14 07:02:04 -0700240 mDragOutlineAlphas[thisIndex] = (Float) animation.getAnimatedValue();
Joe Onorato4be866d2010-10-10 11:26:02 -0700241 final int left = mDragOutlines[thisIndex].x;
242 final int top = mDragOutlines[thisIndex].y;
243 CellLayout.this.invalidate(left, top,
244 left + outline.getWidth(), top + outline.getHeight());
245 }
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700246 }
247 });
Joe Onorato4be866d2010-10-10 11:26:02 -0700248 // The animation holds a reference to the drag outline bitmap as long is it's
249 // running. This way the bitmap can be GCed when the animations are complete.
Chet Haase472b2812010-10-14 07:02:04 -0700250 anim.getAnimator().addListener(new AnimatorListenerAdapter() {
Michael Jurka3c4c20f2010-10-28 15:36:06 -0700251 @Override
Joe Onorato4be866d2010-10-10 11:26:02 -0700252 public void onAnimationEnd(Animator animation) {
Chet Haase472b2812010-10-14 07:02:04 -0700253 if ((Float) ((ValueAnimator) animation).getAnimatedValue() == 0f) {
Joe Onorato4be866d2010-10-10 11:26:02 -0700254 anim.setTag(null);
255 }
256 }
257 });
258 mDragOutlineAnims[i] = anim;
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700259 }
Patrick Dubroyce34a972010-10-19 10:34:32 -0700260
Michael Jurka18014792010-10-14 09:01:34 -0700261 mBackgroundRect = new Rect();
Michael Jurka33945b22010-12-21 18:19:38 -0800262 mGlowBackgroundRect = new Rect();
Michael Jurka18014792010-10-14 09:01:34 -0700263 setHoverScale(1.0f);
264 setHoverAlpha(1.0f);
Michael Jurkabea15192010-11-17 12:33:46 -0800265
Michael Jurka8c920dd2011-01-20 14:16:56 -0800266 mChildren = new CellLayoutChildren(context);
267 mChildren.setCellDimensions(
268 mCellWidth, mCellHeight, mLeftPadding, mTopPadding, mWidthGap, mHeightGap);
269 addView(mChildren);
Michael Jurka18014792010-10-14 09:01:34 -0700270 }
271
Michael Jurka38d1b0b2011-04-05 14:50:34 -0700272 static int widthInPortrait(Resources r, int numCells) {
273 // We use this method from Workspace to figure out how many rows/columns Launcher should
274 // have. We ignore the left/right padding on CellLayout because it turns out in our design
275 // the padding extends outside the visible screen size, but it looked fine anyway.
276 // However, we make sure there's at least enough space for the crosshairs at either
277 // edge to be rendered (half the crosshair is sticking out on either side)
278 int cellWidth = r.getDimensionPixelSize(R.dimen.workspace_cell_width);
279 int widthGap = r.getDimensionPixelSize(R.dimen.workspace_width_gap_port);
280 int crosshairsSize = r.getDrawable(R.drawable.gardening_crosshairs).getIntrinsicWidth();
281
282 return widthGap * (numCells - 1) + cellWidth * numCells + crosshairsSize;
283 }
284
Michael Jurka38d1b0b2011-04-05 14:50:34 -0700285 static int heightInLandscape(Resources r, int numCells) {
286 // We use this method from Workspace to figure out how many rows/columns Launcher should
287 // have. We ignore the left/right padding on CellLayout because it turns out in our design
288 // the padding extends outside the visible screen size, but it looked fine anyway.
289 // However, we make sure there's at least enough space for the crosshairs at the bottom
290 // to be rendered (half the crosshair is sticking out); we don't worry about the top
291 // crosshair since it can bleed into the action bar space
292 int cellHeight = r.getDimensionPixelSize(R.dimen.workspace_cell_height);
293 int heightGap = r.getDimensionPixelSize(R.dimen.workspace_height_gap_land);
294 int crosshairsSize = r.getDrawable(R.drawable.gardening_crosshairs).getIntrinsicHeight();
295
296 return heightGap * (numCells - 1) + cellHeight * numCells + (crosshairsSize + 1) / 2;
297 }
298
Patrick Dubroy3499d8c2011-03-10 17:17:23 -0800299 private void invalidateBubbleTextView(BubbleTextView icon) {
300 final int padding = icon.getPressedOrFocusedBackgroundPadding();
301 invalidate(icon.getLeft() - padding,
302 icon.getTop() - padding,
303 icon.getRight() + padding,
304 icon.getBottom() + padding);
305 }
306
307 void setPressedOrFocusedIcon(BubbleTextView icon) {
308 // We draw the pressed or focused BubbleTextView's background in CellLayout because it
309 // requires an expanded clip rect (due to the glow's blur radius)
310 BubbleTextView oldIcon = mPressedOrFocusedIcon;
311 mPressedOrFocusedIcon = icon;
312 if (oldIcon != null) {
313 invalidateBubbleTextView(oldIcon);
314 }
315 if (mPressedOrFocusedIcon != null) {
316 invalidateBubbleTextView(mPressedOrFocusedIcon);
317 }
318 }
319
Winson Chung6e314082011-01-27 16:46:51 -0800320 public CellLayoutChildren getChildrenLayout() {
321 if (getChildCount() > 0) {
322 return (CellLayoutChildren) getChildAt(0);
323 }
324 return null;
325 }
326
Michael Jurka33945b22010-12-21 18:19:38 -0800327 public void setIsDefaultDropTarget(boolean isDefaultDropTarget) {
328 if (mIsDefaultDropTarget != isDefaultDropTarget) {
329 mIsDefaultDropTarget = isDefaultDropTarget;
330 invalidate();
331 }
332 }
333
Michael Jurka33945b22010-12-21 18:19:38 -0800334 void setIsDragOccuring(boolean isDragOccuring) {
335 if (mIsDragOccuring != isDragOccuring) {
336 mIsDragOccuring = isDragOccuring;
337 invalidate();
338 }
339 }
340
341 void setIsDragOverlapping(boolean isDragOverlapping) {
342 if (mIsDragOverlapping != isDragOverlapping) {
343 mIsDragOverlapping = isDragOverlapping;
344 invalidate();
345 }
346 }
347
348 boolean getIsDragOverlapping() {
349 return mIsDragOverlapping;
350 }
351
352 private void updateGlowRect() {
353 float marginFraction = (mGlowBackgroundScale - 1.0f) / 2.0f;
Michael Jurka18014792010-10-14 09:01:34 -0700354 int marginX = (int) (marginFraction * (mBackgroundRect.right - mBackgroundRect.left));
355 int marginY = (int) (marginFraction * (mBackgroundRect.bottom - mBackgroundRect.top));
Michael Jurka33945b22010-12-21 18:19:38 -0800356 mGlowBackgroundRect.set(mBackgroundRect.left - marginX, mBackgroundRect.top - marginY,
Michael Jurka18014792010-10-14 09:01:34 -0700357 mBackgroundRect.right + marginX, mBackgroundRect.bottom + marginY);
358 invalidate();
359 }
360
361 public void setHoverScale(float scaleFactor) {
Michael Jurka33945b22010-12-21 18:19:38 -0800362 if (scaleFactor != mGlowBackgroundScale) {
363 mGlowBackgroundScale = scaleFactor;
364 updateGlowRect();
Michael Jurka8deb1e62011-01-25 16:27:43 -0800365 if (getParent() != null) {
366 ((View) getParent()).invalidate();
367 }
Michael Jurka18014792010-10-14 09:01:34 -0700368 }
369 }
370
371 public float getHoverScale() {
Michael Jurka33945b22010-12-21 18:19:38 -0800372 return mGlowBackgroundScale;
Michael Jurka18014792010-10-14 09:01:34 -0700373 }
374
375 public float getHoverAlpha() {
Michael Jurka33945b22010-12-21 18:19:38 -0800376 return mGlowBackgroundAlpha;
Michael Jurka18014792010-10-14 09:01:34 -0700377 }
378
379 public void setHoverAlpha(float alpha) {
Michael Jurka33945b22010-12-21 18:19:38 -0800380 mGlowBackgroundAlpha = alpha;
Michael Jurka18014792010-10-14 09:01:34 -0700381 invalidate();
382 }
383
384 void animateDrop() {
385 if (LauncherApplication.isScreenXLarge()) {
386 Resources res = getResources();
387 float onDropScale = res.getInteger(R.integer.config_screenOnDropScalePercent) / 100.0f;
388 ObjectAnimator scaleUp = ObjectAnimator.ofFloat(this, "hoverScale", onDropScale);
389 scaleUp.setDuration(res.getInteger(R.integer.config_screenOnDropScaleUpDuration));
390 ObjectAnimator scaleDown = ObjectAnimator.ofFloat(this, "hoverScale", 1.0f);
391 scaleDown.setDuration(res.getInteger(R.integer.config_screenOnDropScaleDownDuration));
392 ObjectAnimator alphaFadeOut = ObjectAnimator.ofFloat(this, "hoverAlpha", 0.0f);
393
394 alphaFadeOut.setStartDelay(res.getInteger(R.integer.config_screenOnDropAlphaFadeDelay));
395 alphaFadeOut.setDuration(res.getInteger(R.integer.config_screenOnDropAlphaFadeDelay));
396
397 AnimatorSet bouncer = new AnimatorSet();
398 bouncer.play(scaleUp).before(scaleDown);
399 bouncer.play(scaleUp).with(alphaFadeOut);
Michael Jurka8edd75c2010-12-17 20:15:06 -0800400 bouncer.addListener(new AnimatorListenerAdapter() {
Michael Jurka3c4c20f2010-10-28 15:36:06 -0700401 @Override
Michael Jurka18014792010-10-14 09:01:34 -0700402 public void onAnimationStart(Animator animation) {
Michael Jurka33945b22010-12-21 18:19:38 -0800403 setIsDragOverlapping(true);
Michael Jurka18014792010-10-14 09:01:34 -0700404 }
Michael Jurka3c4c20f2010-10-28 15:36:06 -0700405 @Override
Michael Jurka8edd75c2010-12-17 20:15:06 -0800406 public void onAnimationEnd(Animator animation) {
Michael Jurka33945b22010-12-21 18:19:38 -0800407 setIsDragOverlapping(false);
Michael Jurka18014792010-10-14 09:01:34 -0700408 setHoverScale(1.0f);
409 setHoverAlpha(1.0f);
410 }
411 });
412 bouncer.start();
413 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800414 }
415
Jeff Sharkey83f111d2009-04-20 21:03:13 -0700416 @Override
Patrick Dubroy1262e362010-10-06 15:49:50 -0700417 protected void onDraw(Canvas canvas) {
Michael Jurka3e7c7632010-10-02 16:01:03 -0700418 // When we're large, we are either drawn in a "hover" state (ie when dragging an item to
419 // a neighboring page) or with just a normal background (if backgroundAlpha > 0.0f)
420 // When we're small, we are either drawn normally or in the "accepts drops" state (during
421 // a drag). However, we also drag the mini hover background *over* one of those two
422 // backgrounds
Winson Chung26cbf3a2011-01-06 16:25:55 -0800423 if (LauncherApplication.isScreenXLarge() && mBackgroundAlpha > 0.0f) {
Adam Cohenf34bab52010-09-30 14:11:56 -0700424 Drawable bg;
Michael Jurka33945b22010-12-21 18:19:38 -0800425 boolean mini = getScaleX() < 0.5f;
426
427 if (mIsDragOverlapping) {
428 // In the mini case, we draw the active_glow bg *over* the active background
429 bg = mini ? mActiveBackgroundMini : mActiveGlowBackground;
430 } else if (mIsDragOccuring && mAcceptsDrops) {
431 bg = mini ? mActiveBackgroundMini : mActiveBackground;
Adam Cohen3af863b2011-01-25 12:16:51 -0800432 } else if (mIsDefaultDropTarget && mini) {
433 bg = mNormalGlowBackgroundMini;
Adam Cohenf34bab52010-09-30 14:11:56 -0700434 } else {
Michael Jurka33945b22010-12-21 18:19:38 -0800435 bg = mini ? mNormalBackgroundMini : mNormalBackground;
Adam Cohenf34bab52010-09-30 14:11:56 -0700436 }
Michael Jurka33945b22010-12-21 18:19:38 -0800437
438 bg.setAlpha((int) (mBackgroundAlpha * mBackgroundAlphaMultiplier * 255));
439 bg.setBounds(mBackgroundRect);
440 bg.draw(canvas);
441
442 if (mini && mIsDragOverlapping) {
Michael Jurka18014792010-10-14 09:01:34 -0700443 boolean modifiedClipRect = false;
Michael Jurka33945b22010-12-21 18:19:38 -0800444 if (mGlowBackgroundScale > 1.0f) {
Michael Jurka18014792010-10-14 09:01:34 -0700445 // If the hover background's scale is greater than 1, we'll be drawing outside
446 // the bounds of this CellLayout. Get around that by temporarily increasing the
447 // size of the clip rect
Michael Jurka33945b22010-12-21 18:19:38 -0800448 float marginFraction = (mGlowBackgroundScale - 1.0f) / 2.0f;
Michael Jurka18014792010-10-14 09:01:34 -0700449 Rect clipRect = canvas.getClipBounds();
450 int marginX = (int) (marginFraction * (clipRect.right - clipRect.left));
451 int marginY = (int) (marginFraction * (clipRect.bottom - clipRect.top));
452 canvas.save(Canvas.CLIP_SAVE_FLAG);
453 canvas.clipRect(-marginX, -marginY,
454 getWidth() + marginX, getHeight() + marginY, Region.Op.REPLACE);
455 modifiedClipRect = true;
456 }
457
Michael Jurka33945b22010-12-21 18:19:38 -0800458 mActiveGlowBackgroundMini.setAlpha(
459 (int) (mBackgroundAlpha * mGlowBackgroundAlpha * 255));
460 mActiveGlowBackgroundMini.setBounds(mGlowBackgroundRect);
461 mActiveGlowBackgroundMini.draw(canvas);
Michael Jurka18014792010-10-14 09:01:34 -0700462 if (modifiedClipRect) {
463 canvas.restore();
464 }
Michael Jurka3e7c7632010-10-02 16:01:03 -0700465 }
Michael Jurkaa63c4522010-08-19 13:52:27 -0700466 }
Romain Guya6abce82009-11-10 02:54:41 -0800467
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700468 if (mCrosshairsVisibility > 0.0f) {
469 final int countX = mCountX;
470 final int countY = mCountY;
471
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700472 final float MAX_ALPHA = 0.4f;
473 final int MAX_VISIBLE_DISTANCE = 600;
474 final float DISTANCE_MULTIPLIER = 0.002f;
475
476 final Drawable d = mCrosshairsDrawable;
477 final int width = d.getIntrinsicWidth();
478 final int height = d.getIntrinsicHeight();
479
480 int x = getLeftPadding() - (mWidthGap / 2) - (width / 2);
481 for (int col = 0; col <= countX; col++) {
482 int y = getTopPadding() - (mHeightGap / 2) - (height / 2);
483 for (int row = 0; row <= countY; row++) {
484 mTmpPointF.set(x - mDragCenter.x, y - mDragCenter.y);
485 float dist = mTmpPointF.length();
486 // Crosshairs further from the drag point are more faint
487 float alpha = Math.min(MAX_ALPHA,
488 DISTANCE_MULTIPLIER * (MAX_VISIBLE_DISTANCE - dist));
489 if (alpha > 0.0f) {
490 d.setBounds(x, y, x + width, y + height);
491 d.setAlpha((int) (alpha * 255 * mCrosshairsVisibility));
492 d.draw(canvas);
493 }
494 y += mCellHeight + mHeightGap;
495 }
496 x += mCellWidth + mWidthGap;
497 }
Joe Onorato4be866d2010-10-10 11:26:02 -0700498 }
Winson Chung150fbab2010-09-29 17:14:26 -0700499
Patrick Dubroy8e58e912010-10-14 13:21:48 -0700500 final Paint paint = mDragOutlinePaint;
Joe Onorato4be866d2010-10-10 11:26:02 -0700501 for (int i = 0; i < mDragOutlines.length; i++) {
Chet Haase472b2812010-10-14 07:02:04 -0700502 final float alpha = mDragOutlineAlphas[i];
Joe Onorato4be866d2010-10-10 11:26:02 -0700503 if (alpha > 0) {
504 final Point p = mDragOutlines[i];
505 final Bitmap b = (Bitmap) mDragOutlineAnims[i].getTag();
Chet Haase472b2812010-10-14 07:02:04 -0700506 paint.setAlpha((int)(alpha + .5f));
Joe Onorato4be866d2010-10-10 11:26:02 -0700507 canvas.drawBitmap(b, p.x, p.y, paint);
Winson Chung150fbab2010-09-29 17:14:26 -0700508 }
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700509 }
Patrick Dubroy3499d8c2011-03-10 17:17:23 -0800510
511 // We draw the pressed or focused BubbleTextView's background in CellLayout because it
512 // requires an expanded clip rect (due to the glow's blur radius)
513 if (mPressedOrFocusedIcon != null) {
514 final int padding = mPressedOrFocusedIcon.getPressedOrFocusedBackgroundPadding();
515 final Bitmap b = mPressedOrFocusedIcon.getPressedOrFocusedBackground();
516 if (b != null) {
517 canvas.drawBitmap(b,
518 mPressedOrFocusedIcon.getLeft() - padding,
519 mPressedOrFocusedIcon.getTop() - padding,
520 null);
521 }
522 }
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700523 }
524
525 @Override
Jeff Sharkey83f111d2009-04-20 21:03:13 -0700526 public void cancelLongPress() {
527 super.cancelLongPress();
528
529 // Cancel long press for all children
530 final int count = getChildCount();
531 for (int i = 0; i < count; i++) {
532 final View child = getChildAt(i);
533 child.cancelLongPress();
534 }
535 }
536
Michael Jurkadee05892010-07-27 10:01:56 -0700537 public void setOnInterceptTouchListener(View.OnTouchListener listener) {
538 mInterceptTouchListener = listener;
539 }
540
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800541 int getCountX() {
Adam Cohend22015c2010-07-26 22:02:18 -0700542 return mCountX;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800543 }
544
545 int getCountY() {
Adam Cohend22015c2010-07-26 22:02:18 -0700546 return mCountY;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800547 }
548
Michael Jurkaf3ca3ab2010-10-20 17:08:24 -0700549 public boolean addViewToCellLayout(
550 View child, int index, int childId, LayoutParams params, boolean markCells) {
Winson Chungaafa03c2010-06-11 17:34:16 -0700551 final LayoutParams lp = params;
552
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800553 // Generate an id for each view, this assumes we have at most 256x256 cells
554 // per workspace screen
Adam Cohend22015c2010-07-26 22:02:18 -0700555 if (lp.cellX >= 0 && lp.cellX <= mCountX - 1 && lp.cellY >= 0 && lp.cellY <= mCountY - 1) {
Winson Chungaafa03c2010-06-11 17:34:16 -0700556 // If the horizontal or vertical span is set to -1, it is taken to
557 // mean that it spans the extent of the CellLayout
Adam Cohend22015c2010-07-26 22:02:18 -0700558 if (lp.cellHSpan < 0) lp.cellHSpan = mCountX;
559 if (lp.cellVSpan < 0) lp.cellVSpan = mCountY;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800560
Winson Chungaafa03c2010-06-11 17:34:16 -0700561 child.setId(childId);
562
Michael Jurka8c920dd2011-01-20 14:16:56 -0800563 mChildren.addView(child, index, lp);
Michael Jurkadee05892010-07-27 10:01:56 -0700564
Michael Jurkaf3ca3ab2010-10-20 17:08:24 -0700565 if (markCells) markCellsAsOccupiedForView(child);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700566
Winson Chungaafa03c2010-06-11 17:34:16 -0700567 return true;
568 }
569 return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800570 }
Michael Jurka3e7c7632010-10-02 16:01:03 -0700571
Michael Jurkabea15192010-11-17 12:33:46 -0800572 public void setAcceptsDrops(boolean acceptsDrops) {
573 if (mAcceptsDrops != acceptsDrops) {
574 mAcceptsDrops = acceptsDrops;
575 invalidate();
576 }
577 }
578
Michael Jurka3e7c7632010-10-02 16:01:03 -0700579 public boolean getAcceptsDrops() {
580 return mAcceptsDrops;
581 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800582
583 @Override
Michael Jurka0280c3b2010-09-17 15:00:07 -0700584 public void removeAllViews() {
585 clearOccupiedCells();
Michael Jurka8c920dd2011-01-20 14:16:56 -0800586 mChildren.removeAllViews();
Michael Jurka0280c3b2010-09-17 15:00:07 -0700587 }
588
589 @Override
590 public void removeAllViewsInLayout() {
591 clearOccupiedCells();
Michael Jurka8c920dd2011-01-20 14:16:56 -0800592 mChildren.removeAllViewsInLayout();
Michael Jurka0280c3b2010-09-17 15:00:07 -0700593 }
594
Michael Jurkaf3ca3ab2010-10-20 17:08:24 -0700595 public void removeViewWithoutMarkingCells(View view) {
Michael Jurkacf6125c2011-01-28 15:20:01 -0800596 mChildren.removeView(view);
Michael Jurkaf3ca3ab2010-10-20 17:08:24 -0700597 }
598
Michael Jurka0280c3b2010-09-17 15:00:07 -0700599 @Override
600 public void removeView(View view) {
601 markCellsAsUnoccupiedForView(view);
Michael Jurka8c920dd2011-01-20 14:16:56 -0800602 mChildren.removeView(view);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700603 }
604
605 @Override
606 public void removeViewAt(int index) {
Michael Jurka8c920dd2011-01-20 14:16:56 -0800607 markCellsAsUnoccupiedForView(mChildren.getChildAt(index));
608 mChildren.removeViewAt(index);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700609 }
610
611 @Override
612 public void removeViewInLayout(View view) {
613 markCellsAsUnoccupiedForView(view);
Michael Jurka8c920dd2011-01-20 14:16:56 -0800614 mChildren.removeViewInLayout(view);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700615 }
616
617 @Override
618 public void removeViews(int start, int count) {
619 for (int i = start; i < start + count; i++) {
Michael Jurka8c920dd2011-01-20 14:16:56 -0800620 markCellsAsUnoccupiedForView(mChildren.getChildAt(i));
Michael Jurka0280c3b2010-09-17 15:00:07 -0700621 }
Michael Jurka8c920dd2011-01-20 14:16:56 -0800622 mChildren.removeViews(start, count);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700623 }
624
625 @Override
626 public void removeViewsInLayout(int start, int count) {
627 for (int i = start; i < start + count; i++) {
Michael Jurka8c920dd2011-01-20 14:16:56 -0800628 markCellsAsUnoccupiedForView(mChildren.getChildAt(i));
Michael Jurka0280c3b2010-09-17 15:00:07 -0700629 }
Michael Jurka8c920dd2011-01-20 14:16:56 -0800630 mChildren.removeViewsInLayout(start, count);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700631 }
632
Michael Jurka8c920dd2011-01-20 14:16:56 -0800633 public void drawChildren(Canvas canvas) {
634 mChildren.draw(canvas);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800635 }
636
Michael Jurkaabded662011-03-04 12:06:57 -0800637 void buildChildrenLayer() {
638 mChildren.buildLayer();
639 }
640
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800641 @Override
642 protected void onAttachedToWindow() {
643 super.onAttachedToWindow();
644 mCellInfo.screen = ((ViewGroup) getParent()).indexOfChild(this);
645 }
646
Michael Jurkaaf442092010-06-10 17:01:57 -0700647 public void setTagToCellInfoForPoint(int touchX, int touchY) {
648 final CellInfo cellInfo = mCellInfo;
649 final Rect frame = mRect;
650 final int x = touchX + mScrollX;
651 final int y = touchY + mScrollY;
Michael Jurka8c920dd2011-01-20 14:16:56 -0800652 final int count = mChildren.getChildCount();
Michael Jurkaaf442092010-06-10 17:01:57 -0700653
654 boolean found = false;
655 for (int i = count - 1; i >= 0; i--) {
Michael Jurka8c920dd2011-01-20 14:16:56 -0800656 final View child = mChildren.getChildAt(i);
Adam Cohend4844c32011-02-18 19:25:06 -0800657 final LayoutParams lp = (LayoutParams) child.getLayoutParams();
Michael Jurkaaf442092010-06-10 17:01:57 -0700658
Adam Cohen1b607ed2011-03-03 17:26:50 -0800659 if ((child.getVisibility() == VISIBLE || child.getAnimation() != null) &&
660 lp.isLockedToGrid) {
Michael Jurkaaf442092010-06-10 17:01:57 -0700661 child.getHitRect(frame);
662 if (frame.contains(x, y)) {
Michael Jurkaaf442092010-06-10 17:01:57 -0700663 cellInfo.cell = child;
664 cellInfo.cellX = lp.cellX;
665 cellInfo.cellY = lp.cellY;
666 cellInfo.spanX = lp.cellHSpan;
667 cellInfo.spanY = lp.cellVSpan;
668 cellInfo.valid = true;
669 found = true;
Michael Jurkaaf442092010-06-10 17:01:57 -0700670 break;
671 }
672 }
673 }
Winson Chungaafa03c2010-06-11 17:34:16 -0700674
Michael Jurkaaf442092010-06-10 17:01:57 -0700675 if (!found) {
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700676 final int cellXY[] = mTmpCellXY;
Michael Jurkaaf442092010-06-10 17:01:57 -0700677 pointToCellExact(x, y, cellXY);
678
Michael Jurkaaf442092010-06-10 17:01:57 -0700679 cellInfo.cell = null;
680 cellInfo.cellX = cellXY[0];
681 cellInfo.cellY = cellXY[1];
682 cellInfo.spanX = 1;
683 cellInfo.spanY = 1;
Michael Jurka0280c3b2010-09-17 15:00:07 -0700684 cellInfo.valid = cellXY[0] >= 0 && cellXY[1] >= 0 && cellXY[0] < mCountX &&
685 cellXY[1] < mCountY && !mOccupied[cellXY[0]][cellXY[1]];
Michael Jurkaaf442092010-06-10 17:01:57 -0700686 }
687 setTag(cellInfo);
688 }
689
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800690 @Override
691 public boolean onInterceptTouchEvent(MotionEvent ev) {
Michael Jurkadee05892010-07-27 10:01:56 -0700692 if (mInterceptTouchListener != null && mInterceptTouchListener.onTouch(this, ev)) {
693 return true;
694 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800695 final int action = ev.getAction();
696 final CellInfo cellInfo = mCellInfo;
697
698 if (action == MotionEvent.ACTION_DOWN) {
Michael Jurkaaf442092010-06-10 17:01:57 -0700699 setTagToCellInfoForPoint((int) ev.getX(), (int) ev.getY());
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800700 } else if (action == MotionEvent.ACTION_UP) {
701 cellInfo.cell = null;
702 cellInfo.cellX = -1;
703 cellInfo.cellY = -1;
704 cellInfo.spanX = 0;
705 cellInfo.spanY = 0;
706 cellInfo.valid = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800707 setTag(cellInfo);
708 }
709
710 return false;
711 }
712
713 @Override
714 public CellInfo getTag() {
Michael Jurka0280c3b2010-09-17 15:00:07 -0700715 return (CellInfo) super.getTag();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800716 }
717
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700718 /**
Winson Chungaafa03c2010-06-11 17:34:16 -0700719 * Given a point, return the cell that strictly encloses that point
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800720 * @param x X coordinate of the point
721 * @param y Y coordinate of the point
722 * @param result Array of 2 ints to hold the x and y coordinate of the cell
723 */
724 void pointToCellExact(int x, int y, int[] result) {
Winson Chungaafa03c2010-06-11 17:34:16 -0700725 final int hStartPadding = getLeftPadding();
726 final int vStartPadding = getTopPadding();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800727
728 result[0] = (x - hStartPadding) / (mCellWidth + mWidthGap);
729 result[1] = (y - vStartPadding) / (mCellHeight + mHeightGap);
730
Adam Cohend22015c2010-07-26 22:02:18 -0700731 final int xAxis = mCountX;
732 final int yAxis = mCountY;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800733
734 if (result[0] < 0) result[0] = 0;
735 if (result[0] >= xAxis) result[0] = xAxis - 1;
736 if (result[1] < 0) result[1] = 0;
737 if (result[1] >= yAxis) result[1] = yAxis - 1;
738 }
Winson Chungaafa03c2010-06-11 17:34:16 -0700739
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800740 /**
741 * Given a point, return the cell that most closely encloses that point
742 * @param x X coordinate of the point
743 * @param y Y coordinate of the point
744 * @param result Array of 2 ints to hold the x and y coordinate of the cell
745 */
746 void pointToCellRounded(int x, int y, int[] result) {
747 pointToCellExact(x + (mCellWidth / 2), y + (mCellHeight / 2), result);
748 }
749
750 /**
751 * Given a cell coordinate, return the point that represents the upper left corner of that cell
Winson Chungaafa03c2010-06-11 17:34:16 -0700752 *
753 * @param cellX X coordinate of the cell
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800754 * @param cellY Y coordinate of the cell
Winson Chungaafa03c2010-06-11 17:34:16 -0700755 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800756 * @param result Array of 2 ints to hold the x and y coordinate of the point
757 */
758 void cellToPoint(int cellX, int cellY, int[] result) {
Winson Chungaafa03c2010-06-11 17:34:16 -0700759 final int hStartPadding = getLeftPadding();
760 final int vStartPadding = getTopPadding();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800761
762 result[0] = hStartPadding + cellX * (mCellWidth + mWidthGap);
763 result[1] = vStartPadding + cellY * (mCellHeight + mHeightGap);
764 }
765
Romain Guy84f296c2009-11-04 15:00:44 -0800766 int getCellWidth() {
767 return mCellWidth;
768 }
769
770 int getCellHeight() {
771 return mCellHeight;
772 }
773
Adam Cohend4844c32011-02-18 19:25:06 -0800774 int getWidthGap() {
775 return mWidthGap;
776 }
777
778 int getHeightGap() {
779 return mHeightGap;
780 }
781
Romain Guy1a304a12009-11-10 00:02:32 -0800782 int getLeftPadding() {
Winson Chungaafa03c2010-06-11 17:34:16 -0700783 return mLeftPadding;
Romain Guy1a304a12009-11-10 00:02:32 -0800784 }
785
786 int getTopPadding() {
Winson Chungaafa03c2010-06-11 17:34:16 -0700787 return mTopPadding;
Romain Guy1a304a12009-11-10 00:02:32 -0800788 }
789
790 int getRightPadding() {
Winson Chungaafa03c2010-06-11 17:34:16 -0700791 return mRightPadding;
Romain Guy1a304a12009-11-10 00:02:32 -0800792 }
793
794 int getBottomPadding() {
Winson Chungaafa03c2010-06-11 17:34:16 -0700795 return mBottomPadding;
Romain Guy1a304a12009-11-10 00:02:32 -0800796 }
797
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800798 @Override
799 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
800 // TODO: currently ignoring padding
Winson Chungaafa03c2010-06-11 17:34:16 -0700801
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800802 int widthSpecMode = MeasureSpec.getMode(widthMeasureSpec);
Winson Chungaafa03c2010-06-11 17:34:16 -0700803 int widthSpecSize = MeasureSpec.getSize(widthMeasureSpec);
804
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800805 int heightSpecMode = MeasureSpec.getMode(heightMeasureSpec);
806 int heightSpecSize = MeasureSpec.getSize(heightMeasureSpec);
Winson Chungaafa03c2010-06-11 17:34:16 -0700807
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800808 if (widthSpecMode == MeasureSpec.UNSPECIFIED || heightSpecMode == MeasureSpec.UNSPECIFIED) {
809 throw new RuntimeException("CellLayout cannot have UNSPECIFIED dimensions");
810 }
811
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800812 final int cellWidth = mCellWidth;
813 final int cellHeight = mCellHeight;
814
Adam Cohend22015c2010-07-26 22:02:18 -0700815 int numWidthGaps = mCountX - 1;
816 int numHeightGaps = mCountY - 1;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800817
Winson Chungece7f5b2010-10-22 14:54:12 -0700818 if (mWidthGap < 0 || mHeightGap < 0) {
819 int vSpaceLeft = heightSpecSize - mTopPadding - mBottomPadding - (cellHeight * mCountY);
820 mHeightGap = vSpaceLeft / numHeightGaps;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800821
Winson Chungece7f5b2010-10-22 14:54:12 -0700822 int hSpaceLeft = widthSpecSize - mLeftPadding - mRightPadding - (cellWidth * mCountX);
823 mWidthGap = hSpaceLeft / numWidthGaps;
Winson Chungaafa03c2010-06-11 17:34:16 -0700824
Winson Chungece7f5b2010-10-22 14:54:12 -0700825 // center it around the min gaps
826 int minGap = Math.min(mWidthGap, mHeightGap);
827 mWidthGap = mHeightGap = minGap;
828 }
Michael Jurka5f1c5092010-09-03 14:15:02 -0700829
Michael Jurka8c920dd2011-01-20 14:16:56 -0800830 // Initial values correspond to widthSpecMode == MeasureSpec.EXACTLY
831 int newWidth = widthSpecSize;
832 int newHeight = heightSpecSize;
Michael Jurka5f1c5092010-09-03 14:15:02 -0700833 if (widthSpecMode == MeasureSpec.AT_MOST) {
Michael Jurka8c920dd2011-01-20 14:16:56 -0800834 newWidth = mLeftPadding + mRightPadding + (mCountX * cellWidth) +
Winson Chungece7f5b2010-10-22 14:54:12 -0700835 ((mCountX - 1) * mWidthGap);
Michael Jurka8c920dd2011-01-20 14:16:56 -0800836 newHeight = mTopPadding + mBottomPadding + (mCountY * cellHeight) +
Winson Chungece7f5b2010-10-22 14:54:12 -0700837 ((mCountY - 1) * mHeightGap);
Michael Jurka5f1c5092010-09-03 14:15:02 -0700838 setMeasuredDimension(newWidth, newHeight);
Michael Jurka5f1c5092010-09-03 14:15:02 -0700839 }
Michael Jurka8c920dd2011-01-20 14:16:56 -0800840
841 int count = getChildCount();
842 for (int i = 0; i < count; i++) {
843 View child = getChildAt(i);
844 int childWidthMeasureSpec = MeasureSpec.makeMeasureSpec(newWidth, MeasureSpec.EXACTLY);
845 int childheightMeasureSpec = MeasureSpec.makeMeasureSpec(newHeight,
846 MeasureSpec.EXACTLY);
847 child.measure(childWidthMeasureSpec, childheightMeasureSpec);
848 }
849 setMeasuredDimension(newWidth, newHeight);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800850 }
851
852 @Override
Michael Jurka28750fb2010-09-24 17:43:49 -0700853 protected void onLayout(boolean changed, int l, int t, int r, int b) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800854 int count = getChildCount();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800855 for (int i = 0; i < count; i++) {
Michael Jurka8c920dd2011-01-20 14:16:56 -0800856 View child = getChildAt(i);
857 child.layout(0, 0, r - l, b - t);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800858 }
859 }
860
861 @Override
Michael Jurkadee05892010-07-27 10:01:56 -0700862 protected void onSizeChanged(int w, int h, int oldw, int oldh) {
863 super.onSizeChanged(w, h, oldw, oldh);
Michael Jurka18014792010-10-14 09:01:34 -0700864 mBackgroundRect.set(0, 0, w, h);
Michael Jurka33945b22010-12-21 18:19:38 -0800865 updateGlowRect();
Michael Jurkadee05892010-07-27 10:01:56 -0700866 }
867
868 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800869 protected void setChildrenDrawingCacheEnabled(boolean enabled) {
Michael Jurka8c920dd2011-01-20 14:16:56 -0800870 mChildren.setChildrenDrawingCacheEnabled(enabled);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800871 }
872
873 @Override
874 protected void setChildrenDrawnWithCacheEnabled(boolean enabled) {
Michael Jurka8c920dd2011-01-20 14:16:56 -0800875 mChildren.setChildrenDrawnWithCacheEnabled(enabled);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800876 }
877
Michael Jurka5f1c5092010-09-03 14:15:02 -0700878 public float getBackgroundAlpha() {
879 return mBackgroundAlpha;
Michael Jurkadee05892010-07-27 10:01:56 -0700880 }
881
Michael Jurka742574b2011-02-02 23:51:01 -0800882 public void setFastBackgroundAlpha(float alpha) {
883 mBackgroundAlpha = alpha;
884 }
885
Adam Cohen1b0aaac2010-10-28 11:11:18 -0700886 public void setBackgroundAlphaMultiplier(float multiplier) {
887 mBackgroundAlphaMultiplier = multiplier;
888 }
889
Adam Cohenddb82192010-11-10 16:32:54 -0800890 public float getBackgroundAlphaMultiplier() {
891 return mBackgroundAlphaMultiplier;
892 }
893
Michael Jurka5f1c5092010-09-03 14:15:02 -0700894 public void setBackgroundAlpha(float alpha) {
895 mBackgroundAlpha = alpha;
Michael Jurka0142d492010-08-25 17:46:15 -0700896 invalidate();
Michael Jurkadee05892010-07-27 10:01:56 -0700897 }
898
Michael Jurka5f1c5092010-09-03 14:15:02 -0700899 // Need to return true to let the view system know we know how to handle alpha-- this is
900 // because when our children have an alpha of 0.0f, they are still rendering their "dimmed"
901 // versions
902 @Override
903 protected boolean onSetAlpha(int alpha) {
904 return true;
905 }
906
907 public void setAlpha(float alpha) {
908 setChildrenAlpha(alpha);
909 super.setAlpha(alpha);
910 }
911
Michael Jurka742574b2011-02-02 23:51:01 -0800912 public void setFastAlpha(float alpha) {
913 setFastChildrenAlpha(alpha);
914 super.setFastAlpha(alpha);
915 }
916
Michael Jurkadee05892010-07-27 10:01:56 -0700917 private void setChildrenAlpha(float alpha) {
Michael Jurka0142d492010-08-25 17:46:15 -0700918 final int childCount = getChildCount();
919 for (int i = 0; i < childCount; i++) {
Michael Jurkadee05892010-07-27 10:01:56 -0700920 getChildAt(i).setAlpha(alpha);
921 }
922 }
923
Michael Jurka742574b2011-02-02 23:51:01 -0800924 private void setFastChildrenAlpha(float alpha) {
925 final int childCount = getChildCount();
926 for (int i = 0; i < childCount; i++) {
927 getChildAt(i).setFastAlpha(alpha);
928 }
929 }
930
Patrick Dubroy440c3602010-07-13 17:50:32 -0700931 public View getChildAt(int x, int y) {
Michael Jurka8c920dd2011-01-20 14:16:56 -0800932 return mChildren.getChildAt(x, y);
Patrick Dubroy440c3602010-07-13 17:50:32 -0700933 }
934
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700935 /**
936 * Estimate where the top left cell of the dragged item will land if it is dropped.
937 *
938 * @param originX The X value of the top left corner of the item
939 * @param originY The Y value of the top left corner of the item
940 * @param spanX The number of horizontal cells that the item spans
941 * @param spanY The number of vertical cells that the item spans
942 * @param result The estimated drop cell X and Y.
943 */
944 void estimateDropCell(int originX, int originY, int spanX, int spanY, int[] result) {
Adam Cohend22015c2010-07-26 22:02:18 -0700945 final int countX = mCountX;
946 final int countY = mCountY;
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700947
Michael Jurkaa63c4522010-08-19 13:52:27 -0700948 // pointToCellRounded takes the top left of a cell but will pad that with
949 // cellWidth/2 and cellHeight/2 when finding the matching cell
950 pointToCellRounded(originX, originY, result);
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700951
952 // If the item isn't fully on this screen, snap to the edges
953 int rightOverhang = result[0] + spanX - countX;
954 if (rightOverhang > 0) {
955 result[0] -= rightOverhang; // Snap to right
956 }
957 result[0] = Math.max(0, result[0]); // Snap to left
958 int bottomOverhang = result[1] + spanY - countY;
959 if (bottomOverhang > 0) {
960 result[1] -= bottomOverhang; // Snap to bottom
961 }
962 result[1] = Math.max(0, result[1]); // Snap to top
963 }
964
Joe Onorato4be866d2010-10-10 11:26:02 -0700965 void visualizeDropLocation(
966 View v, Bitmap dragOutline, int originX, int originY, int spanX, int spanY) {
967
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -0700968 final int oldDragCellX = mDragCell[0];
969 final int oldDragCellY = mDragCell[1];
Joe Onorato4be866d2010-10-10 11:26:02 -0700970 final int[] nearest = findNearestVacantArea(originX, originY, spanX, spanY, v, mDragCell);
Winson Chunga9abd0e2010-10-27 17:18:37 -0700971 if (v != null) {
972 mDragCenter.set(originX + (v.getWidth() / 2), originY + (v.getHeight() / 2));
973 } else {
974 mDragCenter.set(originX, originY);
975 }
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700976
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -0700977 if (nearest != null && (nearest[0] != oldDragCellX || nearest[1] != oldDragCellY)) {
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700978 // Find the top left corner of the rect the object will occupy
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700979 final int[] topLeft = mTmpPoint;
980 cellToPoint(nearest[0], nearest[1], topLeft);
981
Joe Onorato4be866d2010-10-10 11:26:02 -0700982 int left = topLeft[0];
983 int top = topLeft[1];
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700984
Winson Chunga9abd0e2010-10-27 17:18:37 -0700985 if (v != null) {
986 if (v.getParent() instanceof CellLayout) {
987 LayoutParams lp = (LayoutParams) v.getLayoutParams();
988 left += lp.leftMargin;
989 top += lp.topMargin;
990 }
Winson Chung150fbab2010-09-29 17:14:26 -0700991
Winson Chunga9abd0e2010-10-27 17:18:37 -0700992 // Offsets due to the size difference between the View and the dragOutline
993 left += (v.getWidth() - dragOutline.getWidth()) / 2;
994 top += (v.getHeight() - dragOutline.getHeight()) / 2;
995 }
Winson Chung150fbab2010-09-29 17:14:26 -0700996
Joe Onorato4be866d2010-10-10 11:26:02 -0700997 final int oldIndex = mDragOutlineCurrent;
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -0700998 mDragOutlineAnims[oldIndex].animateOut();
999 mDragOutlineCurrent = (oldIndex + 1) % mDragOutlines.length;
Winson Chung150fbab2010-09-29 17:14:26 -07001000
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -07001001 mDragOutlines[mDragOutlineCurrent].set(left, top);
1002 mDragOutlineAnims[mDragOutlineCurrent].setTag(dragOutline);
1003 mDragOutlineAnims[mDragOutlineCurrent].animateIn();
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001004 }
Patrick Dubroy49250ad2010-10-08 15:33:52 -07001005
1006 // If we are drawing crosshairs, the entire CellLayout needs to be invalidated
1007 if (mCrosshairsDrawable != null) {
1008 invalidate();
1009 }
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001010 }
1011
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001012 /**
Jeff Sharkey70864282009-04-07 21:08:40 -07001013 * Find a vacant area that will fit the given bounds nearest the requested
1014 * cell location. Uses Euclidean distance to score multiple vacant areas.
Winson Chungaafa03c2010-06-11 17:34:16 -07001015 *
Romain Guy51afc022009-05-04 18:03:43 -07001016 * @param pixelX The X location at which you want to search for a vacant area.
1017 * @param pixelY The Y location at which you want to search for a vacant area.
Jeff Sharkey70864282009-04-07 21:08:40 -07001018 * @param spanX Horizontal span of the object.
1019 * @param spanY Vertical span of the object.
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001020 * @param result Array in which to place the result, or null (in which case a new array will
1021 * be allocated)
Jeff Sharkey70864282009-04-07 21:08:40 -07001022 * @return The X, Y cell of a vacant area that can contain this object,
1023 * nearest the requested location.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001024 */
Michael Jurka6a1435d2010-09-27 17:35:12 -07001025 int[] findNearestVacantArea(
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001026 int pixelX, int pixelY, int spanX, int spanY, int[] result) {
1027 return findNearestVacantArea(pixelX, pixelY, spanX, spanY, null, result);
Michael Jurka6a1435d2010-09-27 17:35:12 -07001028 }
Winson Chungaafa03c2010-06-11 17:34:16 -07001029
Michael Jurka6a1435d2010-09-27 17:35:12 -07001030 /**
1031 * Find a vacant area that will fit the given bounds nearest the requested
1032 * cell location. Uses Euclidean distance to score multiple vacant areas.
1033 *
1034 * @param pixelX The X location at which you want to search for a vacant area.
1035 * @param pixelY The Y location at which you want to search for a vacant area.
1036 * @param spanX Horizontal span of the object.
1037 * @param spanY Vertical span of the object.
Michael Jurka6a1435d2010-09-27 17:35:12 -07001038 * @param ignoreView Considers space occupied by this view as unoccupied
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001039 * @param result Previously returned value to possibly recycle.
Michael Jurka6a1435d2010-09-27 17:35:12 -07001040 * @return The X, Y cell of a vacant area that can contain this object,
1041 * nearest the requested location.
1042 */
1043 int[] findNearestVacantArea(
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001044 int pixelX, int pixelY, int spanX, int spanY, View ignoreView, int[] result) {
Michael Jurkac6ee42e2010-09-30 12:04:50 -07001045 // mark space take by ignoreView as available (method checks if ignoreView is null)
1046 markCellsAsUnoccupiedForView(ignoreView);
1047
Jeff Sharkey70864282009-04-07 21:08:40 -07001048 // Keep track of best-scoring drop area
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001049 final int[] bestXY = result != null ? result : new int[2];
Jeff Sharkey70864282009-04-07 21:08:40 -07001050 double bestDistance = Double.MAX_VALUE;
Winson Chungaafa03c2010-06-11 17:34:16 -07001051
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001052 final int countX = mCountX;
1053 final int countY = mCountY;
1054 final boolean[][] occupied = mOccupied;
1055
Winson Chungbbc60d82010-11-11 16:34:41 -08001056 for (int y = 0; y < countY - (spanY - 1); y++) {
Michael Jurkac28de512010-08-13 11:27:44 -07001057 inner:
Winson Chungbbc60d82010-11-11 16:34:41 -08001058 for (int x = 0; x < countX - (spanX - 1); x++) {
Michael Jurkac28de512010-08-13 11:27:44 -07001059 for (int i = 0; i < spanX; i++) {
1060 for (int j = 0; j < spanY; j++) {
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001061 if (occupied[x + i][y + j]) {
Winson Chungbbc60d82010-11-11 16:34:41 -08001062 // small optimization: we can skip to after the column we just found
Michael Jurkac28de512010-08-13 11:27:44 -07001063 // an occupied cell
Winson Chungbbc60d82010-11-11 16:34:41 -08001064 x += i;
Michael Jurkac28de512010-08-13 11:27:44 -07001065 continue inner;
1066 }
1067 }
1068 }
1069 final int[] cellXY = mTmpCellXY;
1070 cellToPoint(x, y, cellXY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001071
Michael Jurkac28de512010-08-13 11:27:44 -07001072 double distance = Math.sqrt(Math.pow(cellXY[0] - pixelX, 2)
1073 + Math.pow(cellXY[1] - pixelY, 2));
1074 if (distance <= bestDistance) {
1075 bestDistance = distance;
1076 bestXY[0] = x;
1077 bestXY[1] = y;
1078 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001079 }
1080 }
Michael Jurkac6ee42e2010-09-30 12:04:50 -07001081 // re-mark space taken by ignoreView as occupied
1082 markCellsAsOccupiedForView(ignoreView);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001083
Winson Chungaafa03c2010-06-11 17:34:16 -07001084 // Return null if no suitable location found
Jeff Sharkey70864282009-04-07 21:08:40 -07001085 if (bestDistance < Double.MAX_VALUE) {
1086 return bestXY;
1087 } else {
1088 return null;
1089 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001090 }
Winson Chungaafa03c2010-06-11 17:34:16 -07001091
Michael Jurka0280c3b2010-09-17 15:00:07 -07001092 boolean existsEmptyCell() {
1093 return findCellForSpan(null, 1, 1);
1094 }
1095
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001096 /**
Michael Jurka0280c3b2010-09-17 15:00:07 -07001097 * Finds the upper-left coordinate of the first rectangle in the grid that can
1098 * hold a cell of the specified dimensions. If intersectX and intersectY are not -1,
1099 * then this method will only return coordinates for rectangles that contain the cell
1100 * (intersectX, intersectY)
1101 *
1102 * @param cellXY The array that will contain the position of a vacant cell if such a cell
1103 * can be found.
1104 * @param spanX The horizontal span of the cell we want to find.
1105 * @param spanY The vertical span of the cell we want to find.
1106 *
1107 * @return True if a vacant cell of the specified dimension was found, false otherwise.
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001108 */
Michael Jurka0280c3b2010-09-17 15:00:07 -07001109 boolean findCellForSpan(int[] cellXY, int spanX, int spanY) {
1110 return findCellForSpanThatIntersectsIgnoring(cellXY, spanX, spanY, -1, -1, null);
1111 }
1112
1113 /**
1114 * Like above, but ignores any cells occupied by the item "ignoreView"
1115 *
1116 * @param cellXY The array that will contain the position of a vacant cell if such a cell
1117 * can be found.
1118 * @param spanX The horizontal span of the cell we want to find.
1119 * @param spanY The vertical span of the cell we want to find.
1120 * @param ignoreView The home screen item we should treat as not occupying any space
1121 * @return
1122 */
1123 boolean findCellForSpanIgnoring(int[] cellXY, int spanX, int spanY, View ignoreView) {
1124 return findCellForSpanThatIntersectsIgnoring(cellXY, spanX, spanY, -1, -1, ignoreView);
1125 }
1126
1127 /**
1128 * Like above, but if intersectX and intersectY are not -1, then this method will try to
1129 * return coordinates for rectangles that contain the cell [intersectX, intersectY]
1130 *
1131 * @param spanX The horizontal span of the cell we want to find.
1132 * @param spanY The vertical span of the cell we want to find.
1133 * @param ignoreView The home screen item we should treat as not occupying any space
1134 * @param intersectX The X coordinate of the cell that we should try to overlap
1135 * @param intersectX The Y coordinate of the cell that we should try to overlap
1136 *
1137 * @return True if a vacant cell of the specified dimension was found, false otherwise.
1138 */
1139 boolean findCellForSpanThatIntersects(int[] cellXY, int spanX, int spanY,
1140 int intersectX, int intersectY) {
1141 return findCellForSpanThatIntersectsIgnoring(
1142 cellXY, spanX, spanY, intersectX, intersectY, null);
1143 }
1144
1145 /**
1146 * The superset of the above two methods
1147 */
1148 boolean findCellForSpanThatIntersectsIgnoring(int[] cellXY, int spanX, int spanY,
1149 int intersectX, int intersectY, View ignoreView) {
Michael Jurkac6ee42e2010-09-30 12:04:50 -07001150 // mark space take by ignoreView as available (method checks if ignoreView is null)
1151 markCellsAsUnoccupiedForView(ignoreView);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001152
Michael Jurka28750fb2010-09-24 17:43:49 -07001153 boolean foundCell = false;
Michael Jurka0280c3b2010-09-17 15:00:07 -07001154 while (true) {
1155 int startX = 0;
1156 if (intersectX >= 0) {
1157 startX = Math.max(startX, intersectX - (spanX - 1));
1158 }
1159 int endX = mCountX - (spanX - 1);
1160 if (intersectX >= 0) {
1161 endX = Math.min(endX, intersectX + (spanX - 1) + (spanX == 1 ? 1 : 0));
1162 }
1163 int startY = 0;
1164 if (intersectY >= 0) {
1165 startY = Math.max(startY, intersectY - (spanY - 1));
1166 }
1167 int endY = mCountY - (spanY - 1);
1168 if (intersectY >= 0) {
1169 endY = Math.min(endY, intersectY + (spanY - 1) + (spanY == 1 ? 1 : 0));
1170 }
1171
Winson Chungbbc60d82010-11-11 16:34:41 -08001172 for (int y = startY; y < endY && !foundCell; y++) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001173 inner:
Winson Chungbbc60d82010-11-11 16:34:41 -08001174 for (int x = startX; x < endX; x++) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001175 for (int i = 0; i < spanX; i++) {
1176 for (int j = 0; j < spanY; j++) {
1177 if (mOccupied[x + i][y + j]) {
Winson Chungbbc60d82010-11-11 16:34:41 -08001178 // small optimization: we can skip to after the column we just found
Michael Jurka0280c3b2010-09-17 15:00:07 -07001179 // an occupied cell
Winson Chungbbc60d82010-11-11 16:34:41 -08001180 x += i;
Michael Jurka0280c3b2010-09-17 15:00:07 -07001181 continue inner;
1182 }
1183 }
1184 }
1185 if (cellXY != null) {
1186 cellXY[0] = x;
1187 cellXY[1] = y;
1188 }
Michael Jurka28750fb2010-09-24 17:43:49 -07001189 foundCell = true;
1190 break;
Michael Jurka0280c3b2010-09-17 15:00:07 -07001191 }
1192 }
1193 if (intersectX == -1 && intersectY == -1) {
1194 break;
1195 } else {
1196 // if we failed to find anything, try again but without any requirements of
1197 // intersecting
1198 intersectX = -1;
1199 intersectY = -1;
1200 continue;
1201 }
1202 }
1203
Michael Jurkac6ee42e2010-09-30 12:04:50 -07001204 // re-mark space taken by ignoreView as occupied
1205 markCellsAsOccupiedForView(ignoreView);
Michael Jurka28750fb2010-09-24 17:43:49 -07001206 return foundCell;
Michael Jurka0280c3b2010-09-17 15:00:07 -07001207 }
1208
1209 /**
1210 * Called when drag has left this CellLayout or has been completed (successfully or not)
1211 */
1212 void onDragExit() {
Joe Onorato4be866d2010-10-10 11:26:02 -07001213 // This can actually be called when we aren't in a drag, e.g. when adding a new
1214 // item to this layout via the customize drawer.
1215 // Guard against that case.
1216 if (mDragging) {
1217 mDragging = false;
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001218
Joe Onorato4be866d2010-10-10 11:26:02 -07001219 // Fade out the drag indicators
1220 if (mCrosshairsAnimator != null) {
1221 mCrosshairsAnimator.animateOut();
1222 }
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001223 }
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -07001224
1225 // Invalidate the drag data
1226 mDragCell[0] = -1;
1227 mDragCell[1] = -1;
1228 mDragOutlineAnims[mDragOutlineCurrent].animateOut();
1229 mDragOutlineCurrent = (mDragOutlineCurrent + 1) % mDragOutlineAnims.length;
1230
Michael Jurka33945b22010-12-21 18:19:38 -08001231 setIsDragOverlapping(false);
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001232 }
1233
1234 /**
Winson Chungaafa03c2010-06-11 17:34:16 -07001235 * Mark a child as having been dropped.
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001236 * At the beginning of the drag operation, the child may have been on another
Patrick Dubroyce34a972010-10-19 10:34:32 -07001237 * screen, but it is re-parented before this method is called.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001238 *
1239 * @param child The child that is being dropped
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001240 */
Michael Jurkad3ef3062010-11-23 16:23:58 -08001241 void onDropChild(View child, boolean animate) {
Romain Guyd94533d2009-08-17 10:01:15 -07001242 if (child != null) {
1243 LayoutParams lp = (LayoutParams) child.getLayoutParams();
Romain Guyd94533d2009-08-17 10:01:15 -07001244 lp.isDragging = false;
Romain Guy84f296c2009-11-04 15:00:44 -08001245 lp.dropped = true;
Michael Jurkad3ef3062010-11-23 16:23:58 -08001246 lp.animateDrop = animate;
Patrick Dubroye3887cc2011-01-20 10:43:40 -08001247 child.setVisibility(animate ? View.INVISIBLE : View.VISIBLE);
Romain Guyd94533d2009-08-17 10:01:15 -07001248 child.requestLayout();
Romain Guyd94533d2009-08-17 10:01:15 -07001249 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001250 }
1251
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001252 /**
1253 * Start dragging the specified child
Winson Chungaafa03c2010-06-11 17:34:16 -07001254 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001255 * @param child The child that is being dragged
1256 */
1257 void onDragChild(View child) {
1258 LayoutParams lp = (LayoutParams) child.getLayoutParams();
1259 lp.isDragging = true;
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001260 }
1261
1262 /**
1263 * A drag event has begun over this layout.
1264 * It may have begun over this layout (in which case onDragChild is called first),
1265 * or it may have begun on another layout.
1266 */
Winson Chunga9abd0e2010-10-27 17:18:37 -07001267 void onDragEnter() {
Patrick Dubroyfe6bd872010-10-13 17:32:10 -07001268 if (!mDragging) {
Patrick Dubroyfe6bd872010-10-13 17:32:10 -07001269 // Fade in the drag indicators
1270 if (mCrosshairsAnimator != null) {
1271 mCrosshairsAnimator.animateIn();
1272 }
Joe Onorato4be866d2010-10-10 11:26:02 -07001273 }
1274 mDragging = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001275 }
Winson Chungaafa03c2010-06-11 17:34:16 -07001276
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001277 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001278 * Computes a bounding rectangle for a range of cells
Winson Chungaafa03c2010-06-11 17:34:16 -07001279 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001280 * @param cellX X coordinate of upper left corner expressed as a cell position
1281 * @param cellY Y coordinate of upper left corner expressed as a cell position
Winson Chungaafa03c2010-06-11 17:34:16 -07001282 * @param cellHSpan Width in cells
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001283 * @param cellVSpan Height in cells
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001284 * @param resultRect Rect into which to put the results
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001285 */
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001286 public void cellToRect(int cellX, int cellY, int cellHSpan, int cellVSpan, RectF resultRect) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001287 final int cellWidth = mCellWidth;
1288 final int cellHeight = mCellHeight;
1289 final int widthGap = mWidthGap;
1290 final int heightGap = mHeightGap;
Winson Chungaafa03c2010-06-11 17:34:16 -07001291
1292 final int hStartPadding = getLeftPadding();
1293 final int vStartPadding = getTopPadding();
1294
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001295 int width = cellHSpan * cellWidth + ((cellHSpan - 1) * widthGap);
1296 int height = cellVSpan * cellHeight + ((cellVSpan - 1) * heightGap);
1297
1298 int x = hStartPadding + cellX * (cellWidth + widthGap);
1299 int y = vStartPadding + cellY * (cellHeight + heightGap);
Winson Chungaafa03c2010-06-11 17:34:16 -07001300
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001301 resultRect.set(x, y, x + width, y + height);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001302 }
Winson Chungaafa03c2010-06-11 17:34:16 -07001303
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001304 /**
Winson Chungaafa03c2010-06-11 17:34:16 -07001305 * Computes the required horizontal and vertical cell spans to always
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001306 * fit the given rectangle.
Winson Chungaafa03c2010-06-11 17:34:16 -07001307 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001308 * @param width Width in pixels
1309 * @param height Height in pixels
Patrick Dubroy8f86ddc2010-07-16 13:55:32 -07001310 * @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 -08001311 */
Patrick Dubroy8f86ddc2010-07-16 13:55:32 -07001312 public int[] rectToCell(int width, int height, int[] result) {
Michael Jurka9987a5c2010-10-08 16:58:12 -07001313 return rectToCell(getResources(), width, height, result);
1314 }
1315
1316 public static int[] rectToCell(Resources resources, int width, int height, int[] result) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001317 // Always assume we're working with the smallest span to make sure we
1318 // reserve enough space in both orientations.
Joe Onorato79e56262009-09-21 15:23:04 -04001319 int actualWidth = resources.getDimensionPixelSize(R.dimen.workspace_cell_width);
1320 int actualHeight = resources.getDimensionPixelSize(R.dimen.workspace_cell_height);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001321 int smallerSize = Math.min(actualWidth, actualHeight);
Joe Onorato79e56262009-09-21 15:23:04 -04001322
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001323 // Always round up to next largest cell
1324 int spanX = (width + smallerSize) / smallerSize;
1325 int spanY = (height + smallerSize) / smallerSize;
Joe Onorato79e56262009-09-21 15:23:04 -04001326
Patrick Dubroy8f86ddc2010-07-16 13:55:32 -07001327 if (result == null) {
1328 return new int[] { spanX, spanY };
1329 }
1330 result[0] = spanX;
1331 result[1] = spanY;
1332 return result;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001333 }
1334
Michael Jurkaf12c75c2011-01-25 22:41:40 -08001335 public int[] cellSpansToSize(int hSpans, int vSpans) {
1336 int[] size = new int[2];
1337 size[0] = hSpans * mCellWidth + (hSpans - 1) * mWidthGap;
1338 size[1] = vSpans * mCellHeight + (vSpans - 1) * mHeightGap;
1339 return size;
1340 }
1341
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001342 /**
Patrick Dubroy047379a2010-12-19 22:02:04 -08001343 * Calculate the grid spans needed to fit given item
1344 */
1345 public void calculateSpans(ItemInfo info) {
1346 final int minWidth;
1347 final int minHeight;
1348
1349 if (info instanceof LauncherAppWidgetInfo) {
1350 minWidth = ((LauncherAppWidgetInfo) info).minWidth;
1351 minHeight = ((LauncherAppWidgetInfo) info).minHeight;
1352 } else if (info instanceof PendingAddWidgetInfo) {
1353 minWidth = ((PendingAddWidgetInfo) info).minWidth;
1354 minHeight = ((PendingAddWidgetInfo) info).minHeight;
1355 } else {
1356 // It's not a widget, so it must be 1x1
1357 info.spanX = info.spanY = 1;
1358 return;
1359 }
1360 int[] spans = rectToCell(minWidth, minHeight, null);
1361 info.spanX = spans[0];
1362 info.spanY = spans[1];
1363 }
1364
1365 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001366 * Find the first vacant cell, if there is one.
1367 *
1368 * @param vacant Holds the x and y coordinate of the vacant cell
1369 * @param spanX Horizontal cell span.
1370 * @param spanY Vertical cell span.
Winson Chungaafa03c2010-06-11 17:34:16 -07001371 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001372 * @return True if a vacant cell was found
1373 */
1374 public boolean getVacantCell(int[] vacant, int spanX, int spanY) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001375
Michael Jurka0280c3b2010-09-17 15:00:07 -07001376 return findVacantCell(vacant, spanX, spanY, mCountX, mCountY, mOccupied);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001377 }
1378
1379 static boolean findVacantCell(int[] vacant, int spanX, int spanY,
1380 int xCount, int yCount, boolean[][] occupied) {
1381
1382 for (int x = 0; x < xCount; x++) {
1383 for (int y = 0; y < yCount; y++) {
1384 boolean available = !occupied[x][y];
1385out: for (int i = x; i < x + spanX - 1 && x < xCount; i++) {
1386 for (int j = y; j < y + spanY - 1 && y < yCount; j++) {
1387 available = available && !occupied[i][j];
1388 if (!available) break out;
1389 }
1390 }
1391
1392 if (available) {
1393 vacant[0] = x;
1394 vacant[1] = y;
1395 return true;
1396 }
1397 }
1398 }
1399
1400 return false;
1401 }
1402
Michael Jurka0280c3b2010-09-17 15:00:07 -07001403 private void clearOccupiedCells() {
1404 for (int x = 0; x < mCountX; x++) {
1405 for (int y = 0; y < mCountY; y++) {
1406 mOccupied[x][y] = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001407 }
1408 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001409 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001410
Adam Cohen1b607ed2011-03-03 17:26:50 -08001411 /**
1412 * Given a view, determines how much that view can be expanded in all directions, in terms of
1413 * whether or not there are other items occupying adjacent cells. Used by the
1414 * AppWidgetResizeFrame to determine how the widget can be resized.
1415 */
Adam Cohend4844c32011-02-18 19:25:06 -08001416 public void getExpandabilityArrayForView(View view, int[] expandability) {
Adam Cohen1b607ed2011-03-03 17:26:50 -08001417 final LayoutParams lp = (LayoutParams) view.getLayoutParams();
Adam Cohend4844c32011-02-18 19:25:06 -08001418 boolean flag;
1419
Adam Cohen1b607ed2011-03-03 17:26:50 -08001420 expandability[AppWidgetResizeFrame.LEFT] = 0;
Adam Cohend4844c32011-02-18 19:25:06 -08001421 for (int x = lp.cellX - 1; x >= 0; x--) {
1422 flag = false;
1423 for (int y = lp.cellY; y < lp.cellY + lp.cellVSpan; y++) {
1424 if (mOccupied[x][y]) flag = true;
1425 }
1426 if (flag) break;
Adam Cohen1b607ed2011-03-03 17:26:50 -08001427 expandability[AppWidgetResizeFrame.LEFT]++;
Adam Cohend4844c32011-02-18 19:25:06 -08001428 }
1429
Adam Cohen1b607ed2011-03-03 17:26:50 -08001430 expandability[AppWidgetResizeFrame.TOP] = 0;
Adam Cohend4844c32011-02-18 19:25:06 -08001431 for (int y = lp.cellY - 1; y >= 0; y--) {
1432 flag = false;
1433 for (int x = lp.cellX; x < lp.cellX + lp.cellHSpan; x++) {
1434 if (mOccupied[x][y]) flag = true;
1435 }
1436 if (flag) break;
Adam Cohen1b607ed2011-03-03 17:26:50 -08001437 expandability[AppWidgetResizeFrame.TOP]++;
1438 }
Adam Cohend4844c32011-02-18 19:25:06 -08001439
Adam Cohen1b607ed2011-03-03 17:26:50 -08001440 expandability[AppWidgetResizeFrame.RIGHT] = 0;
Adam Cohend4844c32011-02-18 19:25:06 -08001441 for (int x = lp.cellX + lp.cellHSpan; x < mCountX; x++) {
1442 flag = false;
1443 for (int y = lp.cellY; y < lp.cellY + lp.cellVSpan; y++) {
1444 if (mOccupied[x][y]) flag = true;
1445 }
1446 if (flag) break;
Adam Cohen1b607ed2011-03-03 17:26:50 -08001447 expandability[AppWidgetResizeFrame.RIGHT]++;
1448 }
Adam Cohend4844c32011-02-18 19:25:06 -08001449
Adam Cohen1b607ed2011-03-03 17:26:50 -08001450 expandability[AppWidgetResizeFrame.BOTTOM] = 0;
Adam Cohend4844c32011-02-18 19:25:06 -08001451 for (int y = lp.cellY + lp.cellVSpan; y < mCountY; y++) {
1452 flag = false;
1453 for (int x = lp.cellX; x < lp.cellX + lp.cellHSpan; x++) {
1454 if (mOccupied[x][y]) flag = true;
1455 }
1456 if (flag) break;
Adam Cohen1b607ed2011-03-03 17:26:50 -08001457 expandability[AppWidgetResizeFrame.BOTTOM]++;
1458 }
Adam Cohend4844c32011-02-18 19:25:06 -08001459 }
1460
Michael Jurka0280c3b2010-09-17 15:00:07 -07001461 public void onMove(View view, int newCellX, int newCellY) {
1462 LayoutParams lp = (LayoutParams) view.getLayoutParams();
1463 markCellsAsUnoccupiedForView(view);
1464 markCellsForView(newCellX, newCellY, lp.cellHSpan, lp.cellVSpan, true);
1465 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001466
Adam Cohend4844c32011-02-18 19:25:06 -08001467 public void markCellsAsOccupiedForView(View view) {
Michael Jurka8c920dd2011-01-20 14:16:56 -08001468 if (view == null || view.getParent() != mChildren) return;
Michael Jurka0280c3b2010-09-17 15:00:07 -07001469 LayoutParams lp = (LayoutParams) view.getLayoutParams();
1470 markCellsForView(lp.cellX, lp.cellY, lp.cellHSpan, lp.cellVSpan, true);
1471 }
1472
Adam Cohend4844c32011-02-18 19:25:06 -08001473 public void markCellsAsUnoccupiedForView(View view) {
Michael Jurka8c920dd2011-01-20 14:16:56 -08001474 if (view == null || view.getParent() != mChildren) return;
Michael Jurka0280c3b2010-09-17 15:00:07 -07001475 LayoutParams lp = (LayoutParams) view.getLayoutParams();
1476 markCellsForView(lp.cellX, lp.cellY, lp.cellHSpan, lp.cellVSpan, false);
1477 }
1478
1479 private void markCellsForView(int cellX, int cellY, int spanX, int spanY, boolean value) {
1480 for (int x = cellX; x < cellX + spanX && x < mCountX; x++) {
1481 for (int y = cellY; y < cellY + spanY && y < mCountY; y++) {
1482 mOccupied[x][y] = value;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001483 }
1484 }
1485 }
1486
1487 @Override
1488 public ViewGroup.LayoutParams generateLayoutParams(AttributeSet attrs) {
1489 return new CellLayout.LayoutParams(getContext(), attrs);
1490 }
1491
1492 @Override
1493 protected boolean checkLayoutParams(ViewGroup.LayoutParams p) {
1494 return p instanceof CellLayout.LayoutParams;
1495 }
1496
1497 @Override
1498 protected ViewGroup.LayoutParams generateLayoutParams(ViewGroup.LayoutParams p) {
1499 return new CellLayout.LayoutParams(p);
1500 }
1501
Winson Chungaafa03c2010-06-11 17:34:16 -07001502 public static class CellLayoutAnimationController extends LayoutAnimationController {
1503 public CellLayoutAnimationController(Animation animation, float delay) {
1504 super(animation, delay);
1505 }
1506
1507 @Override
1508 protected long getDelayForView(View view) {
1509 return (int) (Math.random() * 150);
1510 }
1511 }
1512
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001513 public static class LayoutParams extends ViewGroup.MarginLayoutParams {
1514 /**
1515 * Horizontal location of the item in the grid.
1516 */
1517 @ViewDebug.ExportedProperty
1518 public int cellX;
1519
1520 /**
1521 * Vertical location of the item in the grid.
1522 */
1523 @ViewDebug.ExportedProperty
1524 public int cellY;
1525
1526 /**
1527 * Number of cells spanned horizontally by the item.
1528 */
1529 @ViewDebug.ExportedProperty
1530 public int cellHSpan;
1531
1532 /**
1533 * Number of cells spanned vertically by the item.
1534 */
1535 @ViewDebug.ExportedProperty
1536 public int cellVSpan;
Winson Chungaafa03c2010-06-11 17:34:16 -07001537
Adam Cohen1b607ed2011-03-03 17:26:50 -08001538 /**
1539 * Indicates whether the item will set its x, y, width and height parameters freely,
1540 * or whether these will be computed based on cellX, cellY, cellHSpan and cellVSpan.
1541 */
Adam Cohend4844c32011-02-18 19:25:06 -08001542 public boolean isLockedToGrid = true;
1543
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001544 /**
1545 * Is this item currently being dragged
1546 */
1547 public boolean isDragging;
1548
1549 // X coordinate of the view in the layout.
1550 @ViewDebug.ExportedProperty
1551 int x;
1552 // Y coordinate of the view in the layout.
1553 @ViewDebug.ExportedProperty
1554 int y;
1555
Patrick Dubroyce34a972010-10-19 10:34:32 -07001556 /**
1557 * The old X coordinate of this item, relative to its current parent.
1558 * Used to animate the item into its new position.
1559 */
1560 int oldX;
1561
1562 /**
1563 * The old Y coordinate of this item, relative to its current parent.
1564 * Used to animate the item into its new position.
1565 */
1566 int oldY;
1567
Romain Guy84f296c2009-11-04 15:00:44 -08001568 boolean dropped;
Romain Guyfcb9e712009-10-02 16:06:52 -07001569
Michael Jurkad3ef3062010-11-23 16:23:58 -08001570 boolean animateDrop;
1571
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001572 public LayoutParams(Context c, AttributeSet attrs) {
1573 super(c, attrs);
1574 cellHSpan = 1;
1575 cellVSpan = 1;
1576 }
1577
1578 public LayoutParams(ViewGroup.LayoutParams source) {
1579 super(source);
1580 cellHSpan = 1;
1581 cellVSpan = 1;
1582 }
Winson Chungaafa03c2010-06-11 17:34:16 -07001583
1584 public LayoutParams(LayoutParams source) {
1585 super(source);
1586 this.cellX = source.cellX;
1587 this.cellY = source.cellY;
1588 this.cellHSpan = source.cellHSpan;
1589 this.cellVSpan = source.cellVSpan;
1590 }
1591
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001592 public LayoutParams(int cellX, int cellY, int cellHSpan, int cellVSpan) {
Romain Guy8f19cdd2010-01-08 15:07:00 -08001593 super(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001594 this.cellX = cellX;
1595 this.cellY = cellY;
1596 this.cellHSpan = cellHSpan;
1597 this.cellVSpan = cellVSpan;
1598 }
1599
1600 public void setup(int cellWidth, int cellHeight, int widthGap, int heightGap,
1601 int hStartPadding, int vStartPadding) {
Adam Cohend4844c32011-02-18 19:25:06 -08001602 if (isLockedToGrid) {
1603 final int myCellHSpan = cellHSpan;
1604 final int myCellVSpan = cellVSpan;
1605 final int myCellX = cellX;
1606 final int myCellY = cellY;
Adam Cohen1b607ed2011-03-03 17:26:50 -08001607
Adam Cohend4844c32011-02-18 19:25:06 -08001608 width = myCellHSpan * cellWidth + ((myCellHSpan - 1) * widthGap) -
1609 leftMargin - rightMargin;
1610 height = myCellVSpan * cellHeight + ((myCellVSpan - 1) * heightGap) -
1611 topMargin - bottomMargin;
Adam Cohend4844c32011-02-18 19:25:06 -08001612 x = hStartPadding + myCellX * (cellWidth + widthGap) + leftMargin;
1613 y = vStartPadding + myCellY * (cellHeight + heightGap) + topMargin;
1614 }
1615 }
Winson Chungaafa03c2010-06-11 17:34:16 -07001616
Adam Cohend4844c32011-02-18 19:25:06 -08001617 public void setWidth(int width) {
1618 this.width = width;
1619 }
Winson Chungaafa03c2010-06-11 17:34:16 -07001620
Adam Cohend4844c32011-02-18 19:25:06 -08001621 public int getWidth() {
1622 return width;
1623 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001624
Adam Cohend4844c32011-02-18 19:25:06 -08001625 public void setHeight(int height) {
1626 this.height = height;
1627 }
1628
1629 public int getHeight() {
1630 return height;
1631 }
1632
1633 public void setX(int x) {
1634 this.x = x;
1635 }
1636
1637 public int getX() {
1638 return x;
1639 }
1640
1641 public void setY(int y) {
1642 this.y = y;
1643 }
1644
1645 public int getY() {
1646 return y;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001647 }
Winson Chungaafa03c2010-06-11 17:34:16 -07001648
1649 public String toString() {
1650 return "(" + this.cellX + ", " + this.cellY + ")";
1651 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001652 }
1653
Michael Jurka0280c3b2010-09-17 15:00:07 -07001654 // This class stores info for two purposes:
1655 // 1. When dragging items (mDragInfo in Workspace), we store the View, its cellX & cellY,
1656 // its spanX, spanY, and the screen it is on
1657 // 2. When long clicking on an empty cell in a CellLayout, we save information about the
1658 // cellX and cellY coordinates and which page was clicked. We then set this as a tag on
1659 // the CellLayout that was long clicked
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001660 static final class CellInfo implements ContextMenu.ContextMenuInfo {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001661 View cell;
Michael Jurkaa63c4522010-08-19 13:52:27 -07001662 int cellX = -1;
1663 int cellY = -1;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001664 int spanX;
1665 int spanY;
1666 int screen;
1667 boolean valid;
1668
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001669 @Override
1670 public String toString() {
Winson Chungaafa03c2010-06-11 17:34:16 -07001671 return "Cell[view=" + (cell == null ? "null" : cell.getClass())
1672 + ", x=" + cellX + ", y=" + cellY + "]";
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001673 }
1674 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001675}