blob: d0fe5951c389b74d5a436b18795a70374c25c825 [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
285 static int widthInLandscape(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 either
290 // edge to be rendered (half the crosshair is sticking out on either side)
291 int cellWidth = r.getDimensionPixelSize(R.dimen.workspace_cell_width);
292 int widthGap = r.getDimensionPixelSize(R.dimen.workspace_width_gap_land);
293 int crosshairsSize = r.getDrawable(R.drawable.gardening_crosshairs).getIntrinsicWidth();
294
295 return widthGap * (numCells - 1) + cellWidth * numCells + crosshairsSize;
296 }
297
298 static int heightInPortrait(Resources r, int numCells) {
299 // We use this method from Workspace to figure out how many rows/columns Launcher should
300 // have. We ignore the left/right padding on CellLayout because it turns out in our design
301 // the padding extends outside the visible screen size, but it looked fine anyway.
302 // However, we make sure there's at least enough space for the crosshairs at the bottom
303 // to be rendered (half the crosshair is sticking out); we don't worry about the top
304 // crosshair since it can bleed into the action bar space
305 int cellHeight = r.getDimensionPixelSize(R.dimen.workspace_cell_height);
306 int heightGap = r.getDimensionPixelSize(R.dimen.workspace_height_gap_port);
307 int crosshairsSize = r.getDrawable(R.drawable.gardening_crosshairs).getIntrinsicHeight();
308
309 return heightGap * (numCells - 1) + cellHeight * numCells + (crosshairsSize + 1) / 2;
310 }
311
312 static int heightInLandscape(Resources r, int numCells) {
313 // We use this method from Workspace to figure out how many rows/columns Launcher should
314 // have. We ignore the left/right padding on CellLayout because it turns out in our design
315 // the padding extends outside the visible screen size, but it looked fine anyway.
316 // However, we make sure there's at least enough space for the crosshairs at the bottom
317 // to be rendered (half the crosshair is sticking out); we don't worry about the top
318 // crosshair since it can bleed into the action bar space
319 int cellHeight = r.getDimensionPixelSize(R.dimen.workspace_cell_height);
320 int heightGap = r.getDimensionPixelSize(R.dimen.workspace_height_gap_land);
321 int crosshairsSize = r.getDrawable(R.drawable.gardening_crosshairs).getIntrinsicHeight();
322
323 return heightGap * (numCells - 1) + cellHeight * numCells + (crosshairsSize + 1) / 2;
324 }
325
Patrick Dubroy3499d8c2011-03-10 17:17:23 -0800326 private void invalidateBubbleTextView(BubbleTextView icon) {
327 final int padding = icon.getPressedOrFocusedBackgroundPadding();
328 invalidate(icon.getLeft() - padding,
329 icon.getTop() - padding,
330 icon.getRight() + padding,
331 icon.getBottom() + padding);
332 }
333
334 void setPressedOrFocusedIcon(BubbleTextView icon) {
335 // We draw the pressed or focused BubbleTextView's background in CellLayout because it
336 // requires an expanded clip rect (due to the glow's blur radius)
337 BubbleTextView oldIcon = mPressedOrFocusedIcon;
338 mPressedOrFocusedIcon = icon;
339 if (oldIcon != null) {
340 invalidateBubbleTextView(oldIcon);
341 }
342 if (mPressedOrFocusedIcon != null) {
343 invalidateBubbleTextView(mPressedOrFocusedIcon);
344 }
345 }
346
Winson Chung6e314082011-01-27 16:46:51 -0800347 public CellLayoutChildren getChildrenLayout() {
348 if (getChildCount() > 0) {
349 return (CellLayoutChildren) getChildAt(0);
350 }
351 return null;
352 }
353
Michael Jurka33945b22010-12-21 18:19:38 -0800354 public void setIsDefaultDropTarget(boolean isDefaultDropTarget) {
355 if (mIsDefaultDropTarget != isDefaultDropTarget) {
356 mIsDefaultDropTarget = isDefaultDropTarget;
357 invalidate();
358 }
359 }
360
Michael Jurka33945b22010-12-21 18:19:38 -0800361 void setIsDragOccuring(boolean isDragOccuring) {
362 if (mIsDragOccuring != isDragOccuring) {
363 mIsDragOccuring = isDragOccuring;
364 invalidate();
365 }
366 }
367
368 void setIsDragOverlapping(boolean isDragOverlapping) {
369 if (mIsDragOverlapping != isDragOverlapping) {
370 mIsDragOverlapping = isDragOverlapping;
371 invalidate();
372 }
373 }
374
375 boolean getIsDragOverlapping() {
376 return mIsDragOverlapping;
377 }
378
379 private void updateGlowRect() {
380 float marginFraction = (mGlowBackgroundScale - 1.0f) / 2.0f;
Michael Jurka18014792010-10-14 09:01:34 -0700381 int marginX = (int) (marginFraction * (mBackgroundRect.right - mBackgroundRect.left));
382 int marginY = (int) (marginFraction * (mBackgroundRect.bottom - mBackgroundRect.top));
Michael Jurka33945b22010-12-21 18:19:38 -0800383 mGlowBackgroundRect.set(mBackgroundRect.left - marginX, mBackgroundRect.top - marginY,
Michael Jurka18014792010-10-14 09:01:34 -0700384 mBackgroundRect.right + marginX, mBackgroundRect.bottom + marginY);
385 invalidate();
386 }
387
388 public void setHoverScale(float scaleFactor) {
Michael Jurka33945b22010-12-21 18:19:38 -0800389 if (scaleFactor != mGlowBackgroundScale) {
390 mGlowBackgroundScale = scaleFactor;
391 updateGlowRect();
Michael Jurka8deb1e62011-01-25 16:27:43 -0800392 if (getParent() != null) {
393 ((View) getParent()).invalidate();
394 }
Michael Jurka18014792010-10-14 09:01:34 -0700395 }
396 }
397
398 public float getHoverScale() {
Michael Jurka33945b22010-12-21 18:19:38 -0800399 return mGlowBackgroundScale;
Michael Jurka18014792010-10-14 09:01:34 -0700400 }
401
402 public float getHoverAlpha() {
Michael Jurka33945b22010-12-21 18:19:38 -0800403 return mGlowBackgroundAlpha;
Michael Jurka18014792010-10-14 09:01:34 -0700404 }
405
406 public void setHoverAlpha(float alpha) {
Michael Jurka33945b22010-12-21 18:19:38 -0800407 mGlowBackgroundAlpha = alpha;
Michael Jurka18014792010-10-14 09:01:34 -0700408 invalidate();
409 }
410
411 void animateDrop() {
412 if (LauncherApplication.isScreenXLarge()) {
413 Resources res = getResources();
414 float onDropScale = res.getInteger(R.integer.config_screenOnDropScalePercent) / 100.0f;
415 ObjectAnimator scaleUp = ObjectAnimator.ofFloat(this, "hoverScale", onDropScale);
416 scaleUp.setDuration(res.getInteger(R.integer.config_screenOnDropScaleUpDuration));
417 ObjectAnimator scaleDown = ObjectAnimator.ofFloat(this, "hoverScale", 1.0f);
418 scaleDown.setDuration(res.getInteger(R.integer.config_screenOnDropScaleDownDuration));
419 ObjectAnimator alphaFadeOut = ObjectAnimator.ofFloat(this, "hoverAlpha", 0.0f);
420
421 alphaFadeOut.setStartDelay(res.getInteger(R.integer.config_screenOnDropAlphaFadeDelay));
422 alphaFadeOut.setDuration(res.getInteger(R.integer.config_screenOnDropAlphaFadeDelay));
423
424 AnimatorSet bouncer = new AnimatorSet();
425 bouncer.play(scaleUp).before(scaleDown);
426 bouncer.play(scaleUp).with(alphaFadeOut);
Michael Jurka8edd75c2010-12-17 20:15:06 -0800427 bouncer.addListener(new AnimatorListenerAdapter() {
Michael Jurka3c4c20f2010-10-28 15:36:06 -0700428 @Override
Michael Jurka18014792010-10-14 09:01:34 -0700429 public void onAnimationStart(Animator animation) {
Michael Jurka33945b22010-12-21 18:19:38 -0800430 setIsDragOverlapping(true);
Michael Jurka18014792010-10-14 09:01:34 -0700431 }
Michael Jurka3c4c20f2010-10-28 15:36:06 -0700432 @Override
Michael Jurka8edd75c2010-12-17 20:15:06 -0800433 public void onAnimationEnd(Animator animation) {
Michael Jurka33945b22010-12-21 18:19:38 -0800434 setIsDragOverlapping(false);
Michael Jurka18014792010-10-14 09:01:34 -0700435 setHoverScale(1.0f);
436 setHoverAlpha(1.0f);
437 }
438 });
439 bouncer.start();
440 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800441 }
442
Jeff Sharkey83f111d2009-04-20 21:03:13 -0700443 @Override
Patrick Dubroy1262e362010-10-06 15:49:50 -0700444 protected void onDraw(Canvas canvas) {
Michael Jurka3e7c7632010-10-02 16:01:03 -0700445 // When we're large, we are either drawn in a "hover" state (ie when dragging an item to
446 // a neighboring page) or with just a normal background (if backgroundAlpha > 0.0f)
447 // When we're small, we are either drawn normally or in the "accepts drops" state (during
448 // a drag). However, we also drag the mini hover background *over* one of those two
449 // backgrounds
Winson Chung26cbf3a2011-01-06 16:25:55 -0800450 if (LauncherApplication.isScreenXLarge() && mBackgroundAlpha > 0.0f) {
Adam Cohenf34bab52010-09-30 14:11:56 -0700451 Drawable bg;
Michael Jurka33945b22010-12-21 18:19:38 -0800452 boolean mini = getScaleX() < 0.5f;
453
454 if (mIsDragOverlapping) {
455 // In the mini case, we draw the active_glow bg *over* the active background
456 bg = mini ? mActiveBackgroundMini : mActiveGlowBackground;
457 } else if (mIsDragOccuring && mAcceptsDrops) {
458 bg = mini ? mActiveBackgroundMini : mActiveBackground;
Adam Cohen3af863b2011-01-25 12:16:51 -0800459 } else if (mIsDefaultDropTarget && mini) {
460 bg = mNormalGlowBackgroundMini;
Adam Cohenf34bab52010-09-30 14:11:56 -0700461 } else {
Michael Jurka33945b22010-12-21 18:19:38 -0800462 bg = mini ? mNormalBackgroundMini : mNormalBackground;
Adam Cohenf34bab52010-09-30 14:11:56 -0700463 }
Michael Jurka33945b22010-12-21 18:19:38 -0800464
465 bg.setAlpha((int) (mBackgroundAlpha * mBackgroundAlphaMultiplier * 255));
466 bg.setBounds(mBackgroundRect);
467 bg.draw(canvas);
468
469 if (mini && mIsDragOverlapping) {
Michael Jurka18014792010-10-14 09:01:34 -0700470 boolean modifiedClipRect = false;
Michael Jurka33945b22010-12-21 18:19:38 -0800471 if (mGlowBackgroundScale > 1.0f) {
Michael Jurka18014792010-10-14 09:01:34 -0700472 // If the hover background's scale is greater than 1, we'll be drawing outside
473 // the bounds of this CellLayout. Get around that by temporarily increasing the
474 // size of the clip rect
Michael Jurka33945b22010-12-21 18:19:38 -0800475 float marginFraction = (mGlowBackgroundScale - 1.0f) / 2.0f;
Michael Jurka18014792010-10-14 09:01:34 -0700476 Rect clipRect = canvas.getClipBounds();
477 int marginX = (int) (marginFraction * (clipRect.right - clipRect.left));
478 int marginY = (int) (marginFraction * (clipRect.bottom - clipRect.top));
479 canvas.save(Canvas.CLIP_SAVE_FLAG);
480 canvas.clipRect(-marginX, -marginY,
481 getWidth() + marginX, getHeight() + marginY, Region.Op.REPLACE);
482 modifiedClipRect = true;
483 }
484
Michael Jurka33945b22010-12-21 18:19:38 -0800485 mActiveGlowBackgroundMini.setAlpha(
486 (int) (mBackgroundAlpha * mGlowBackgroundAlpha * 255));
487 mActiveGlowBackgroundMini.setBounds(mGlowBackgroundRect);
488 mActiveGlowBackgroundMini.draw(canvas);
Michael Jurka18014792010-10-14 09:01:34 -0700489 if (modifiedClipRect) {
490 canvas.restore();
491 }
Michael Jurka3e7c7632010-10-02 16:01:03 -0700492 }
Michael Jurkaa63c4522010-08-19 13:52:27 -0700493 }
Romain Guya6abce82009-11-10 02:54:41 -0800494
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700495 if (mCrosshairsVisibility > 0.0f) {
496 final int countX = mCountX;
497 final int countY = mCountY;
498
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700499 final float MAX_ALPHA = 0.4f;
500 final int MAX_VISIBLE_DISTANCE = 600;
501 final float DISTANCE_MULTIPLIER = 0.002f;
502
503 final Drawable d = mCrosshairsDrawable;
504 final int width = d.getIntrinsicWidth();
505 final int height = d.getIntrinsicHeight();
506
507 int x = getLeftPadding() - (mWidthGap / 2) - (width / 2);
508 for (int col = 0; col <= countX; col++) {
509 int y = getTopPadding() - (mHeightGap / 2) - (height / 2);
510 for (int row = 0; row <= countY; row++) {
511 mTmpPointF.set(x - mDragCenter.x, y - mDragCenter.y);
512 float dist = mTmpPointF.length();
513 // Crosshairs further from the drag point are more faint
514 float alpha = Math.min(MAX_ALPHA,
515 DISTANCE_MULTIPLIER * (MAX_VISIBLE_DISTANCE - dist));
516 if (alpha > 0.0f) {
517 d.setBounds(x, y, x + width, y + height);
518 d.setAlpha((int) (alpha * 255 * mCrosshairsVisibility));
519 d.draw(canvas);
520 }
521 y += mCellHeight + mHeightGap;
522 }
523 x += mCellWidth + mWidthGap;
524 }
Joe Onorato4be866d2010-10-10 11:26:02 -0700525 }
Winson Chung150fbab2010-09-29 17:14:26 -0700526
Patrick Dubroy8e58e912010-10-14 13:21:48 -0700527 final Paint paint = mDragOutlinePaint;
Joe Onorato4be866d2010-10-10 11:26:02 -0700528 for (int i = 0; i < mDragOutlines.length; i++) {
Chet Haase472b2812010-10-14 07:02:04 -0700529 final float alpha = mDragOutlineAlphas[i];
Joe Onorato4be866d2010-10-10 11:26:02 -0700530 if (alpha > 0) {
531 final Point p = mDragOutlines[i];
532 final Bitmap b = (Bitmap) mDragOutlineAnims[i].getTag();
Chet Haase472b2812010-10-14 07:02:04 -0700533 paint.setAlpha((int)(alpha + .5f));
Joe Onorato4be866d2010-10-10 11:26:02 -0700534 canvas.drawBitmap(b, p.x, p.y, paint);
Winson Chung150fbab2010-09-29 17:14:26 -0700535 }
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700536 }
Patrick Dubroy3499d8c2011-03-10 17:17:23 -0800537
538 // We draw the pressed or focused BubbleTextView's background in CellLayout because it
539 // requires an expanded clip rect (due to the glow's blur radius)
540 if (mPressedOrFocusedIcon != null) {
541 final int padding = mPressedOrFocusedIcon.getPressedOrFocusedBackgroundPadding();
542 final Bitmap b = mPressedOrFocusedIcon.getPressedOrFocusedBackground();
543 if (b != null) {
544 canvas.drawBitmap(b,
545 mPressedOrFocusedIcon.getLeft() - padding,
546 mPressedOrFocusedIcon.getTop() - padding,
547 null);
548 }
549 }
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700550 }
551
552 @Override
Jeff Sharkey83f111d2009-04-20 21:03:13 -0700553 public void cancelLongPress() {
554 super.cancelLongPress();
555
556 // Cancel long press for all children
557 final int count = getChildCount();
558 for (int i = 0; i < count; i++) {
559 final View child = getChildAt(i);
560 child.cancelLongPress();
561 }
562 }
563
Michael Jurkadee05892010-07-27 10:01:56 -0700564 public void setOnInterceptTouchListener(View.OnTouchListener listener) {
565 mInterceptTouchListener = listener;
566 }
567
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800568 int getCountX() {
Adam Cohend22015c2010-07-26 22:02:18 -0700569 return mCountX;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800570 }
571
572 int getCountY() {
Adam Cohend22015c2010-07-26 22:02:18 -0700573 return mCountY;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800574 }
575
Michael Jurkaf3ca3ab2010-10-20 17:08:24 -0700576 public boolean addViewToCellLayout(
577 View child, int index, int childId, LayoutParams params, boolean markCells) {
Winson Chungaafa03c2010-06-11 17:34:16 -0700578 final LayoutParams lp = params;
579
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800580 // Generate an id for each view, this assumes we have at most 256x256 cells
581 // per workspace screen
Adam Cohend22015c2010-07-26 22:02:18 -0700582 if (lp.cellX >= 0 && lp.cellX <= mCountX - 1 && lp.cellY >= 0 && lp.cellY <= mCountY - 1) {
Winson Chungaafa03c2010-06-11 17:34:16 -0700583 // If the horizontal or vertical span is set to -1, it is taken to
584 // mean that it spans the extent of the CellLayout
Adam Cohend22015c2010-07-26 22:02:18 -0700585 if (lp.cellHSpan < 0) lp.cellHSpan = mCountX;
586 if (lp.cellVSpan < 0) lp.cellVSpan = mCountY;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800587
Winson Chungaafa03c2010-06-11 17:34:16 -0700588 child.setId(childId);
589
Michael Jurka8c920dd2011-01-20 14:16:56 -0800590 mChildren.addView(child, index, lp);
Michael Jurkadee05892010-07-27 10:01:56 -0700591
Michael Jurkaf3ca3ab2010-10-20 17:08:24 -0700592 if (markCells) markCellsAsOccupiedForView(child);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700593
Winson Chungaafa03c2010-06-11 17:34:16 -0700594 return true;
595 }
596 return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800597 }
Michael Jurka3e7c7632010-10-02 16:01:03 -0700598
Michael Jurkabea15192010-11-17 12:33:46 -0800599 public void setAcceptsDrops(boolean acceptsDrops) {
600 if (mAcceptsDrops != acceptsDrops) {
601 mAcceptsDrops = acceptsDrops;
602 invalidate();
603 }
604 }
605
Michael Jurka3e7c7632010-10-02 16:01:03 -0700606 public boolean getAcceptsDrops() {
607 return mAcceptsDrops;
608 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800609
610 @Override
Michael Jurka0280c3b2010-09-17 15:00:07 -0700611 public void removeAllViews() {
612 clearOccupiedCells();
Michael Jurka8c920dd2011-01-20 14:16:56 -0800613 mChildren.removeAllViews();
Michael Jurka0280c3b2010-09-17 15:00:07 -0700614 }
615
616 @Override
617 public void removeAllViewsInLayout() {
618 clearOccupiedCells();
Michael Jurka8c920dd2011-01-20 14:16:56 -0800619 mChildren.removeAllViewsInLayout();
Michael Jurka0280c3b2010-09-17 15:00:07 -0700620 }
621
Michael Jurkaf3ca3ab2010-10-20 17:08:24 -0700622 public void removeViewWithoutMarkingCells(View view) {
Michael Jurkacf6125c2011-01-28 15:20:01 -0800623 mChildren.removeView(view);
Michael Jurkaf3ca3ab2010-10-20 17:08:24 -0700624 }
625
Michael Jurka0280c3b2010-09-17 15:00:07 -0700626 @Override
627 public void removeView(View view) {
628 markCellsAsUnoccupiedForView(view);
Michael Jurka8c920dd2011-01-20 14:16:56 -0800629 mChildren.removeView(view);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700630 }
631
632 @Override
633 public void removeViewAt(int index) {
Michael Jurka8c920dd2011-01-20 14:16:56 -0800634 markCellsAsUnoccupiedForView(mChildren.getChildAt(index));
635 mChildren.removeViewAt(index);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700636 }
637
638 @Override
639 public void removeViewInLayout(View view) {
640 markCellsAsUnoccupiedForView(view);
Michael Jurka8c920dd2011-01-20 14:16:56 -0800641 mChildren.removeViewInLayout(view);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700642 }
643
644 @Override
645 public void removeViews(int start, int count) {
646 for (int i = start; i < start + count; i++) {
Michael Jurka8c920dd2011-01-20 14:16:56 -0800647 markCellsAsUnoccupiedForView(mChildren.getChildAt(i));
Michael Jurka0280c3b2010-09-17 15:00:07 -0700648 }
Michael Jurka8c920dd2011-01-20 14:16:56 -0800649 mChildren.removeViews(start, count);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700650 }
651
652 @Override
653 public void removeViewsInLayout(int start, int count) {
654 for (int i = start; i < start + count; i++) {
Michael Jurka8c920dd2011-01-20 14:16:56 -0800655 markCellsAsUnoccupiedForView(mChildren.getChildAt(i));
Michael Jurka0280c3b2010-09-17 15:00:07 -0700656 }
Michael Jurka8c920dd2011-01-20 14:16:56 -0800657 mChildren.removeViewsInLayout(start, count);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700658 }
659
Michael Jurka8c920dd2011-01-20 14:16:56 -0800660 public void drawChildren(Canvas canvas) {
661 mChildren.draw(canvas);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800662 }
663
Michael Jurkaabded662011-03-04 12:06:57 -0800664 void buildChildrenLayer() {
665 mChildren.buildLayer();
666 }
667
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800668 @Override
669 protected void onAttachedToWindow() {
670 super.onAttachedToWindow();
671 mCellInfo.screen = ((ViewGroup) getParent()).indexOfChild(this);
672 }
673
Michael Jurkaaf442092010-06-10 17:01:57 -0700674 public void setTagToCellInfoForPoint(int touchX, int touchY) {
675 final CellInfo cellInfo = mCellInfo;
676 final Rect frame = mRect;
677 final int x = touchX + mScrollX;
678 final int y = touchY + mScrollY;
Michael Jurka8c920dd2011-01-20 14:16:56 -0800679 final int count = mChildren.getChildCount();
Michael Jurkaaf442092010-06-10 17:01:57 -0700680
681 boolean found = false;
682 for (int i = count - 1; i >= 0; i--) {
Michael Jurka8c920dd2011-01-20 14:16:56 -0800683 final View child = mChildren.getChildAt(i);
Adam Cohend4844c32011-02-18 19:25:06 -0800684 final LayoutParams lp = (LayoutParams) child.getLayoutParams();
Michael Jurkaaf442092010-06-10 17:01:57 -0700685
Adam Cohen1b607ed2011-03-03 17:26:50 -0800686 if ((child.getVisibility() == VISIBLE || child.getAnimation() != null) &&
687 lp.isLockedToGrid) {
Michael Jurkaaf442092010-06-10 17:01:57 -0700688 child.getHitRect(frame);
689 if (frame.contains(x, y)) {
Michael Jurkaaf442092010-06-10 17:01:57 -0700690 cellInfo.cell = child;
691 cellInfo.cellX = lp.cellX;
692 cellInfo.cellY = lp.cellY;
693 cellInfo.spanX = lp.cellHSpan;
694 cellInfo.spanY = lp.cellVSpan;
695 cellInfo.valid = true;
696 found = true;
Michael Jurkaaf442092010-06-10 17:01:57 -0700697 break;
698 }
699 }
700 }
Winson Chungaafa03c2010-06-11 17:34:16 -0700701
Michael Jurkaaf442092010-06-10 17:01:57 -0700702 if (!found) {
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700703 final int cellXY[] = mTmpCellXY;
Michael Jurkaaf442092010-06-10 17:01:57 -0700704 pointToCellExact(x, y, cellXY);
705
Michael Jurkaaf442092010-06-10 17:01:57 -0700706 cellInfo.cell = null;
707 cellInfo.cellX = cellXY[0];
708 cellInfo.cellY = cellXY[1];
709 cellInfo.spanX = 1;
710 cellInfo.spanY = 1;
Michael Jurka0280c3b2010-09-17 15:00:07 -0700711 cellInfo.valid = cellXY[0] >= 0 && cellXY[1] >= 0 && cellXY[0] < mCountX &&
712 cellXY[1] < mCountY && !mOccupied[cellXY[0]][cellXY[1]];
Michael Jurkaaf442092010-06-10 17:01:57 -0700713 }
714 setTag(cellInfo);
715 }
716
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800717 @Override
718 public boolean onInterceptTouchEvent(MotionEvent ev) {
Michael Jurkadee05892010-07-27 10:01:56 -0700719 if (mInterceptTouchListener != null && mInterceptTouchListener.onTouch(this, ev)) {
720 return true;
721 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800722 final int action = ev.getAction();
723 final CellInfo cellInfo = mCellInfo;
724
725 if (action == MotionEvent.ACTION_DOWN) {
Michael Jurkaaf442092010-06-10 17:01:57 -0700726 setTagToCellInfoForPoint((int) ev.getX(), (int) ev.getY());
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800727 } else if (action == MotionEvent.ACTION_UP) {
728 cellInfo.cell = null;
729 cellInfo.cellX = -1;
730 cellInfo.cellY = -1;
731 cellInfo.spanX = 0;
732 cellInfo.spanY = 0;
733 cellInfo.valid = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800734 setTag(cellInfo);
735 }
736
737 return false;
738 }
739
740 @Override
741 public CellInfo getTag() {
Michael Jurka0280c3b2010-09-17 15:00:07 -0700742 return (CellInfo) super.getTag();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800743 }
744
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700745 /**
Winson Chungaafa03c2010-06-11 17:34:16 -0700746 * Given a point, return the cell that strictly encloses that point
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800747 * @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 pointToCellExact(int x, int y, int[] result) {
Winson Chungaafa03c2010-06-11 17:34:16 -0700752 final int hStartPadding = getLeftPadding();
753 final int vStartPadding = getTopPadding();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800754
755 result[0] = (x - hStartPadding) / (mCellWidth + mWidthGap);
756 result[1] = (y - vStartPadding) / (mCellHeight + mHeightGap);
757
Adam Cohend22015c2010-07-26 22:02:18 -0700758 final int xAxis = mCountX;
759 final int yAxis = mCountY;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800760
761 if (result[0] < 0) result[0] = 0;
762 if (result[0] >= xAxis) result[0] = xAxis - 1;
763 if (result[1] < 0) result[1] = 0;
764 if (result[1] >= yAxis) result[1] = yAxis - 1;
765 }
Winson Chungaafa03c2010-06-11 17:34:16 -0700766
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800767 /**
768 * Given a point, return the cell that most closely encloses that point
769 * @param x X coordinate of the point
770 * @param y Y coordinate of the point
771 * @param result Array of 2 ints to hold the x and y coordinate of the cell
772 */
773 void pointToCellRounded(int x, int y, int[] result) {
774 pointToCellExact(x + (mCellWidth / 2), y + (mCellHeight / 2), result);
775 }
776
777 /**
778 * Given a cell coordinate, return the point that represents the upper left corner of that cell
Winson Chungaafa03c2010-06-11 17:34:16 -0700779 *
780 * @param cellX X coordinate of the cell
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800781 * @param cellY Y coordinate of the cell
Winson Chungaafa03c2010-06-11 17:34:16 -0700782 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800783 * @param result Array of 2 ints to hold the x and y coordinate of the point
784 */
785 void cellToPoint(int cellX, int cellY, int[] result) {
Winson Chungaafa03c2010-06-11 17:34:16 -0700786 final int hStartPadding = getLeftPadding();
787 final int vStartPadding = getTopPadding();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800788
789 result[0] = hStartPadding + cellX * (mCellWidth + mWidthGap);
790 result[1] = vStartPadding + cellY * (mCellHeight + mHeightGap);
791 }
792
Romain Guy84f296c2009-11-04 15:00:44 -0800793 int getCellWidth() {
794 return mCellWidth;
795 }
796
797 int getCellHeight() {
798 return mCellHeight;
799 }
800
Adam Cohend4844c32011-02-18 19:25:06 -0800801 int getWidthGap() {
802 return mWidthGap;
803 }
804
805 int getHeightGap() {
806 return mHeightGap;
807 }
808
Romain Guy1a304a12009-11-10 00:02:32 -0800809 int getLeftPadding() {
Winson Chungaafa03c2010-06-11 17:34:16 -0700810 return mLeftPadding;
Romain Guy1a304a12009-11-10 00:02:32 -0800811 }
812
813 int getTopPadding() {
Winson Chungaafa03c2010-06-11 17:34:16 -0700814 return mTopPadding;
Romain Guy1a304a12009-11-10 00:02:32 -0800815 }
816
817 int getRightPadding() {
Winson Chungaafa03c2010-06-11 17:34:16 -0700818 return mRightPadding;
Romain Guy1a304a12009-11-10 00:02:32 -0800819 }
820
821 int getBottomPadding() {
Winson Chungaafa03c2010-06-11 17:34:16 -0700822 return mBottomPadding;
Romain Guy1a304a12009-11-10 00:02:32 -0800823 }
824
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800825 @Override
826 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
827 // TODO: currently ignoring padding
Winson Chungaafa03c2010-06-11 17:34:16 -0700828
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800829 int widthSpecMode = MeasureSpec.getMode(widthMeasureSpec);
Winson Chungaafa03c2010-06-11 17:34:16 -0700830 int widthSpecSize = MeasureSpec.getSize(widthMeasureSpec);
831
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800832 int heightSpecMode = MeasureSpec.getMode(heightMeasureSpec);
833 int heightSpecSize = MeasureSpec.getSize(heightMeasureSpec);
Winson Chungaafa03c2010-06-11 17:34:16 -0700834
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800835 if (widthSpecMode == MeasureSpec.UNSPECIFIED || heightSpecMode == MeasureSpec.UNSPECIFIED) {
836 throw new RuntimeException("CellLayout cannot have UNSPECIFIED dimensions");
837 }
838
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800839 final int cellWidth = mCellWidth;
840 final int cellHeight = mCellHeight;
841
Adam Cohend22015c2010-07-26 22:02:18 -0700842 int numWidthGaps = mCountX - 1;
843 int numHeightGaps = mCountY - 1;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800844
Winson Chungece7f5b2010-10-22 14:54:12 -0700845 if (mWidthGap < 0 || mHeightGap < 0) {
846 int vSpaceLeft = heightSpecSize - mTopPadding - mBottomPadding - (cellHeight * mCountY);
847 mHeightGap = vSpaceLeft / numHeightGaps;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800848
Winson Chungece7f5b2010-10-22 14:54:12 -0700849 int hSpaceLeft = widthSpecSize - mLeftPadding - mRightPadding - (cellWidth * mCountX);
850 mWidthGap = hSpaceLeft / numWidthGaps;
Winson Chungaafa03c2010-06-11 17:34:16 -0700851
Winson Chungece7f5b2010-10-22 14:54:12 -0700852 // center it around the min gaps
853 int minGap = Math.min(mWidthGap, mHeightGap);
854 mWidthGap = mHeightGap = minGap;
855 }
Michael Jurka5f1c5092010-09-03 14:15:02 -0700856
Michael Jurka8c920dd2011-01-20 14:16:56 -0800857 // Initial values correspond to widthSpecMode == MeasureSpec.EXACTLY
858 int newWidth = widthSpecSize;
859 int newHeight = heightSpecSize;
Michael Jurka5f1c5092010-09-03 14:15:02 -0700860 if (widthSpecMode == MeasureSpec.AT_MOST) {
Michael Jurka8c920dd2011-01-20 14:16:56 -0800861 newWidth = mLeftPadding + mRightPadding + (mCountX * cellWidth) +
Winson Chungece7f5b2010-10-22 14:54:12 -0700862 ((mCountX - 1) * mWidthGap);
Michael Jurka8c920dd2011-01-20 14:16:56 -0800863 newHeight = mTopPadding + mBottomPadding + (mCountY * cellHeight) +
Winson Chungece7f5b2010-10-22 14:54:12 -0700864 ((mCountY - 1) * mHeightGap);
Michael Jurka5f1c5092010-09-03 14:15:02 -0700865 setMeasuredDimension(newWidth, newHeight);
Michael Jurka5f1c5092010-09-03 14:15:02 -0700866 }
Michael Jurka8c920dd2011-01-20 14:16:56 -0800867
868 int count = getChildCount();
869 for (int i = 0; i < count; i++) {
870 View child = getChildAt(i);
871 int childWidthMeasureSpec = MeasureSpec.makeMeasureSpec(newWidth, MeasureSpec.EXACTLY);
872 int childheightMeasureSpec = MeasureSpec.makeMeasureSpec(newHeight,
873 MeasureSpec.EXACTLY);
874 child.measure(childWidthMeasureSpec, childheightMeasureSpec);
875 }
876 setMeasuredDimension(newWidth, newHeight);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800877 }
878
879 @Override
Michael Jurka28750fb2010-09-24 17:43:49 -0700880 protected void onLayout(boolean changed, int l, int t, int r, int b) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800881 int count = getChildCount();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800882 for (int i = 0; i < count; i++) {
Michael Jurka8c920dd2011-01-20 14:16:56 -0800883 View child = getChildAt(i);
884 child.layout(0, 0, r - l, b - t);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800885 }
886 }
887
888 @Override
Michael Jurkadee05892010-07-27 10:01:56 -0700889 protected void onSizeChanged(int w, int h, int oldw, int oldh) {
890 super.onSizeChanged(w, h, oldw, oldh);
Michael Jurka18014792010-10-14 09:01:34 -0700891 mBackgroundRect.set(0, 0, w, h);
Michael Jurka33945b22010-12-21 18:19:38 -0800892 updateGlowRect();
Michael Jurkadee05892010-07-27 10:01:56 -0700893 }
894
895 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800896 protected void setChildrenDrawingCacheEnabled(boolean enabled) {
Michael Jurka8c920dd2011-01-20 14:16:56 -0800897 mChildren.setChildrenDrawingCacheEnabled(enabled);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800898 }
899
900 @Override
901 protected void setChildrenDrawnWithCacheEnabled(boolean enabled) {
Michael Jurka8c920dd2011-01-20 14:16:56 -0800902 mChildren.setChildrenDrawnWithCacheEnabled(enabled);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800903 }
904
Michael Jurka5f1c5092010-09-03 14:15:02 -0700905 public float getBackgroundAlpha() {
906 return mBackgroundAlpha;
Michael Jurkadee05892010-07-27 10:01:56 -0700907 }
908
Michael Jurka742574b2011-02-02 23:51:01 -0800909 public void setFastBackgroundAlpha(float alpha) {
910 mBackgroundAlpha = alpha;
911 }
912
Adam Cohen1b0aaac2010-10-28 11:11:18 -0700913 public void setBackgroundAlphaMultiplier(float multiplier) {
914 mBackgroundAlphaMultiplier = multiplier;
915 }
916
Adam Cohenddb82192010-11-10 16:32:54 -0800917 public float getBackgroundAlphaMultiplier() {
918 return mBackgroundAlphaMultiplier;
919 }
920
Michael Jurka5f1c5092010-09-03 14:15:02 -0700921 public void setBackgroundAlpha(float alpha) {
922 mBackgroundAlpha = alpha;
Michael Jurka0142d492010-08-25 17:46:15 -0700923 invalidate();
Michael Jurkadee05892010-07-27 10:01:56 -0700924 }
925
Michael Jurka5f1c5092010-09-03 14:15:02 -0700926 // Need to return true to let the view system know we know how to handle alpha-- this is
927 // because when our children have an alpha of 0.0f, they are still rendering their "dimmed"
928 // versions
929 @Override
930 protected boolean onSetAlpha(int alpha) {
931 return true;
932 }
933
934 public void setAlpha(float alpha) {
935 setChildrenAlpha(alpha);
936 super.setAlpha(alpha);
937 }
938
Michael Jurka742574b2011-02-02 23:51:01 -0800939 public void setFastAlpha(float alpha) {
940 setFastChildrenAlpha(alpha);
941 super.setFastAlpha(alpha);
942 }
943
Michael Jurkadee05892010-07-27 10:01:56 -0700944 private void setChildrenAlpha(float alpha) {
Michael Jurka0142d492010-08-25 17:46:15 -0700945 final int childCount = getChildCount();
946 for (int i = 0; i < childCount; i++) {
Michael Jurkadee05892010-07-27 10:01:56 -0700947 getChildAt(i).setAlpha(alpha);
948 }
949 }
950
Michael Jurka742574b2011-02-02 23:51:01 -0800951 private void setFastChildrenAlpha(float alpha) {
952 final int childCount = getChildCount();
953 for (int i = 0; i < childCount; i++) {
954 getChildAt(i).setFastAlpha(alpha);
955 }
956 }
957
Patrick Dubroy440c3602010-07-13 17:50:32 -0700958 public View getChildAt(int x, int y) {
Michael Jurka8c920dd2011-01-20 14:16:56 -0800959 return mChildren.getChildAt(x, y);
Patrick Dubroy440c3602010-07-13 17:50:32 -0700960 }
961
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700962 /**
963 * Estimate where the top left cell of the dragged item will land if it is dropped.
964 *
965 * @param originX The X value of the top left corner of the item
966 * @param originY The Y value of the top left corner of the item
967 * @param spanX The number of horizontal cells that the item spans
968 * @param spanY The number of vertical cells that the item spans
969 * @param result The estimated drop cell X and Y.
970 */
971 void estimateDropCell(int originX, int originY, int spanX, int spanY, int[] result) {
Adam Cohend22015c2010-07-26 22:02:18 -0700972 final int countX = mCountX;
973 final int countY = mCountY;
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700974
Michael Jurkaa63c4522010-08-19 13:52:27 -0700975 // pointToCellRounded takes the top left of a cell but will pad that with
976 // cellWidth/2 and cellHeight/2 when finding the matching cell
977 pointToCellRounded(originX, originY, result);
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700978
979 // If the item isn't fully on this screen, snap to the edges
980 int rightOverhang = result[0] + spanX - countX;
981 if (rightOverhang > 0) {
982 result[0] -= rightOverhang; // Snap to right
983 }
984 result[0] = Math.max(0, result[0]); // Snap to left
985 int bottomOverhang = result[1] + spanY - countY;
986 if (bottomOverhang > 0) {
987 result[1] -= bottomOverhang; // Snap to bottom
988 }
989 result[1] = Math.max(0, result[1]); // Snap to top
990 }
991
Joe Onorato4be866d2010-10-10 11:26:02 -0700992 void visualizeDropLocation(
993 View v, Bitmap dragOutline, int originX, int originY, int spanX, int spanY) {
994
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -0700995 final int oldDragCellX = mDragCell[0];
996 final int oldDragCellY = mDragCell[1];
Joe Onorato4be866d2010-10-10 11:26:02 -0700997 final int[] nearest = findNearestVacantArea(originX, originY, spanX, spanY, v, mDragCell);
Winson Chunga9abd0e2010-10-27 17:18:37 -0700998 if (v != null) {
999 mDragCenter.set(originX + (v.getWidth() / 2), originY + (v.getHeight() / 2));
1000 } else {
1001 mDragCenter.set(originX, originY);
1002 }
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001003
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -07001004 if (nearest != null && (nearest[0] != oldDragCellX || nearest[1] != oldDragCellY)) {
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001005 // Find the top left corner of the rect the object will occupy
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001006 final int[] topLeft = mTmpPoint;
1007 cellToPoint(nearest[0], nearest[1], topLeft);
1008
Joe Onorato4be866d2010-10-10 11:26:02 -07001009 int left = topLeft[0];
1010 int top = topLeft[1];
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001011
Winson Chunga9abd0e2010-10-27 17:18:37 -07001012 if (v != null) {
1013 if (v.getParent() instanceof CellLayout) {
1014 LayoutParams lp = (LayoutParams) v.getLayoutParams();
1015 left += lp.leftMargin;
1016 top += lp.topMargin;
1017 }
Winson Chung150fbab2010-09-29 17:14:26 -07001018
Winson Chunga9abd0e2010-10-27 17:18:37 -07001019 // Offsets due to the size difference between the View and the dragOutline
1020 left += (v.getWidth() - dragOutline.getWidth()) / 2;
1021 top += (v.getHeight() - dragOutline.getHeight()) / 2;
1022 }
Winson Chung150fbab2010-09-29 17:14:26 -07001023
Joe Onorato4be866d2010-10-10 11:26:02 -07001024 final int oldIndex = mDragOutlineCurrent;
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -07001025 mDragOutlineAnims[oldIndex].animateOut();
1026 mDragOutlineCurrent = (oldIndex + 1) % mDragOutlines.length;
Winson Chung150fbab2010-09-29 17:14:26 -07001027
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -07001028 mDragOutlines[mDragOutlineCurrent].set(left, top);
1029 mDragOutlineAnims[mDragOutlineCurrent].setTag(dragOutline);
1030 mDragOutlineAnims[mDragOutlineCurrent].animateIn();
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001031 }
Patrick Dubroy49250ad2010-10-08 15:33:52 -07001032
1033 // If we are drawing crosshairs, the entire CellLayout needs to be invalidated
1034 if (mCrosshairsDrawable != null) {
1035 invalidate();
1036 }
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001037 }
1038
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001039 /**
Jeff Sharkey70864282009-04-07 21:08:40 -07001040 * Find a vacant area that will fit the given bounds nearest the requested
1041 * cell location. Uses Euclidean distance to score multiple vacant areas.
Winson Chungaafa03c2010-06-11 17:34:16 -07001042 *
Romain Guy51afc022009-05-04 18:03:43 -07001043 * @param pixelX The X location at which you want to search for a vacant area.
1044 * @param pixelY The Y location at which you want to search for a vacant area.
Jeff Sharkey70864282009-04-07 21:08:40 -07001045 * @param spanX Horizontal span of the object.
1046 * @param spanY Vertical span of the object.
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001047 * @param result Array in which to place the result, or null (in which case a new array will
1048 * be allocated)
Jeff Sharkey70864282009-04-07 21:08:40 -07001049 * @return The X, Y cell of a vacant area that can contain this object,
1050 * nearest the requested location.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001051 */
Michael Jurka6a1435d2010-09-27 17:35:12 -07001052 int[] findNearestVacantArea(
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001053 int pixelX, int pixelY, int spanX, int spanY, int[] result) {
1054 return findNearestVacantArea(pixelX, pixelY, spanX, spanY, null, result);
Michael Jurka6a1435d2010-09-27 17:35:12 -07001055 }
Winson Chungaafa03c2010-06-11 17:34:16 -07001056
Michael Jurka6a1435d2010-09-27 17:35:12 -07001057 /**
1058 * Find a vacant area that will fit the given bounds nearest the requested
1059 * cell location. Uses Euclidean distance to score multiple vacant areas.
1060 *
1061 * @param pixelX The X location at which you want to search for a vacant area.
1062 * @param pixelY The Y location at which you want to search for a vacant area.
1063 * @param spanX Horizontal span of the object.
1064 * @param spanY Vertical span of the object.
Michael Jurka6a1435d2010-09-27 17:35:12 -07001065 * @param ignoreView Considers space occupied by this view as unoccupied
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001066 * @param result Previously returned value to possibly recycle.
Michael Jurka6a1435d2010-09-27 17:35:12 -07001067 * @return The X, Y cell of a vacant area that can contain this object,
1068 * nearest the requested location.
1069 */
1070 int[] findNearestVacantArea(
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001071 int pixelX, int pixelY, int spanX, int spanY, View ignoreView, int[] result) {
Michael Jurkac6ee42e2010-09-30 12:04:50 -07001072 // mark space take by ignoreView as available (method checks if ignoreView is null)
1073 markCellsAsUnoccupiedForView(ignoreView);
1074
Jeff Sharkey70864282009-04-07 21:08:40 -07001075 // Keep track of best-scoring drop area
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001076 final int[] bestXY = result != null ? result : new int[2];
Jeff Sharkey70864282009-04-07 21:08:40 -07001077 double bestDistance = Double.MAX_VALUE;
Winson Chungaafa03c2010-06-11 17:34:16 -07001078
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001079 final int countX = mCountX;
1080 final int countY = mCountY;
1081 final boolean[][] occupied = mOccupied;
1082
Winson Chungbbc60d82010-11-11 16:34:41 -08001083 for (int y = 0; y < countY - (spanY - 1); y++) {
Michael Jurkac28de512010-08-13 11:27:44 -07001084 inner:
Winson Chungbbc60d82010-11-11 16:34:41 -08001085 for (int x = 0; x < countX - (spanX - 1); x++) {
Michael Jurkac28de512010-08-13 11:27:44 -07001086 for (int i = 0; i < spanX; i++) {
1087 for (int j = 0; j < spanY; j++) {
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001088 if (occupied[x + i][y + j]) {
Winson Chungbbc60d82010-11-11 16:34:41 -08001089 // small optimization: we can skip to after the column we just found
Michael Jurkac28de512010-08-13 11:27:44 -07001090 // an occupied cell
Winson Chungbbc60d82010-11-11 16:34:41 -08001091 x += i;
Michael Jurkac28de512010-08-13 11:27:44 -07001092 continue inner;
1093 }
1094 }
1095 }
1096 final int[] cellXY = mTmpCellXY;
1097 cellToPoint(x, y, cellXY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001098
Michael Jurkac28de512010-08-13 11:27:44 -07001099 double distance = Math.sqrt(Math.pow(cellXY[0] - pixelX, 2)
1100 + Math.pow(cellXY[1] - pixelY, 2));
1101 if (distance <= bestDistance) {
1102 bestDistance = distance;
1103 bestXY[0] = x;
1104 bestXY[1] = y;
1105 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001106 }
1107 }
Michael Jurkac6ee42e2010-09-30 12:04:50 -07001108 // re-mark space taken by ignoreView as occupied
1109 markCellsAsOccupiedForView(ignoreView);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001110
Winson Chungaafa03c2010-06-11 17:34:16 -07001111 // Return null if no suitable location found
Jeff Sharkey70864282009-04-07 21:08:40 -07001112 if (bestDistance < Double.MAX_VALUE) {
1113 return bestXY;
1114 } else {
1115 return null;
1116 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001117 }
Winson Chungaafa03c2010-06-11 17:34:16 -07001118
Michael Jurka0280c3b2010-09-17 15:00:07 -07001119 boolean existsEmptyCell() {
1120 return findCellForSpan(null, 1, 1);
1121 }
1122
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001123 /**
Michael Jurka0280c3b2010-09-17 15:00:07 -07001124 * Finds the upper-left coordinate of the first rectangle in the grid that can
1125 * hold a cell of the specified dimensions. If intersectX and intersectY are not -1,
1126 * then this method will only return coordinates for rectangles that contain the cell
1127 * (intersectX, intersectY)
1128 *
1129 * @param cellXY The array that will contain the position of a vacant cell if such a cell
1130 * can be found.
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 *
1134 * @return True if a vacant cell of the specified dimension was found, false otherwise.
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001135 */
Michael Jurka0280c3b2010-09-17 15:00:07 -07001136 boolean findCellForSpan(int[] cellXY, int spanX, int spanY) {
1137 return findCellForSpanThatIntersectsIgnoring(cellXY, spanX, spanY, -1, -1, null);
1138 }
1139
1140 /**
1141 * Like above, but ignores any cells occupied by the item "ignoreView"
1142 *
1143 * @param cellXY The array that will contain the position of a vacant cell if such a cell
1144 * can be found.
1145 * @param spanX The horizontal span of the cell we want to find.
1146 * @param spanY The vertical span of the cell we want to find.
1147 * @param ignoreView The home screen item we should treat as not occupying any space
1148 * @return
1149 */
1150 boolean findCellForSpanIgnoring(int[] cellXY, int spanX, int spanY, View ignoreView) {
1151 return findCellForSpanThatIntersectsIgnoring(cellXY, spanX, spanY, -1, -1, ignoreView);
1152 }
1153
1154 /**
1155 * Like above, but if intersectX and intersectY are not -1, then this method will try to
1156 * return coordinates for rectangles that contain the cell [intersectX, intersectY]
1157 *
1158 * @param spanX The horizontal span of the cell we want to find.
1159 * @param spanY The vertical span of the cell we want to find.
1160 * @param ignoreView The home screen item we should treat as not occupying any space
1161 * @param intersectX The X coordinate of the cell that we should try to overlap
1162 * @param intersectX The Y coordinate of the cell that we should try to overlap
1163 *
1164 * @return True if a vacant cell of the specified dimension was found, false otherwise.
1165 */
1166 boolean findCellForSpanThatIntersects(int[] cellXY, int spanX, int spanY,
1167 int intersectX, int intersectY) {
1168 return findCellForSpanThatIntersectsIgnoring(
1169 cellXY, spanX, spanY, intersectX, intersectY, null);
1170 }
1171
1172 /**
1173 * The superset of the above two methods
1174 */
1175 boolean findCellForSpanThatIntersectsIgnoring(int[] cellXY, int spanX, int spanY,
1176 int intersectX, int intersectY, View ignoreView) {
Michael Jurkac6ee42e2010-09-30 12:04:50 -07001177 // mark space take by ignoreView as available (method checks if ignoreView is null)
1178 markCellsAsUnoccupiedForView(ignoreView);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001179
Michael Jurka28750fb2010-09-24 17:43:49 -07001180 boolean foundCell = false;
Michael Jurka0280c3b2010-09-17 15:00:07 -07001181 while (true) {
1182 int startX = 0;
1183 if (intersectX >= 0) {
1184 startX = Math.max(startX, intersectX - (spanX - 1));
1185 }
1186 int endX = mCountX - (spanX - 1);
1187 if (intersectX >= 0) {
1188 endX = Math.min(endX, intersectX + (spanX - 1) + (spanX == 1 ? 1 : 0));
1189 }
1190 int startY = 0;
1191 if (intersectY >= 0) {
1192 startY = Math.max(startY, intersectY - (spanY - 1));
1193 }
1194 int endY = mCountY - (spanY - 1);
1195 if (intersectY >= 0) {
1196 endY = Math.min(endY, intersectY + (spanY - 1) + (spanY == 1 ? 1 : 0));
1197 }
1198
Winson Chungbbc60d82010-11-11 16:34:41 -08001199 for (int y = startY; y < endY && !foundCell; y++) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001200 inner:
Winson Chungbbc60d82010-11-11 16:34:41 -08001201 for (int x = startX; x < endX; x++) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001202 for (int i = 0; i < spanX; i++) {
1203 for (int j = 0; j < spanY; j++) {
1204 if (mOccupied[x + i][y + j]) {
Winson Chungbbc60d82010-11-11 16:34:41 -08001205 // small optimization: we can skip to after the column we just found
Michael Jurka0280c3b2010-09-17 15:00:07 -07001206 // an occupied cell
Winson Chungbbc60d82010-11-11 16:34:41 -08001207 x += i;
Michael Jurka0280c3b2010-09-17 15:00:07 -07001208 continue inner;
1209 }
1210 }
1211 }
1212 if (cellXY != null) {
1213 cellXY[0] = x;
1214 cellXY[1] = y;
1215 }
Michael Jurka28750fb2010-09-24 17:43:49 -07001216 foundCell = true;
1217 break;
Michael Jurka0280c3b2010-09-17 15:00:07 -07001218 }
1219 }
1220 if (intersectX == -1 && intersectY == -1) {
1221 break;
1222 } else {
1223 // if we failed to find anything, try again but without any requirements of
1224 // intersecting
1225 intersectX = -1;
1226 intersectY = -1;
1227 continue;
1228 }
1229 }
1230
Michael Jurkac6ee42e2010-09-30 12:04:50 -07001231 // re-mark space taken by ignoreView as occupied
1232 markCellsAsOccupiedForView(ignoreView);
Michael Jurka28750fb2010-09-24 17:43:49 -07001233 return foundCell;
Michael Jurka0280c3b2010-09-17 15:00:07 -07001234 }
1235
1236 /**
1237 * Called when drag has left this CellLayout or has been completed (successfully or not)
1238 */
1239 void onDragExit() {
Joe Onorato4be866d2010-10-10 11:26:02 -07001240 // This can actually be called when we aren't in a drag, e.g. when adding a new
1241 // item to this layout via the customize drawer.
1242 // Guard against that case.
1243 if (mDragging) {
1244 mDragging = false;
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001245
Joe Onorato4be866d2010-10-10 11:26:02 -07001246 // Fade out the drag indicators
1247 if (mCrosshairsAnimator != null) {
1248 mCrosshairsAnimator.animateOut();
1249 }
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001250 }
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -07001251
1252 // Invalidate the drag data
1253 mDragCell[0] = -1;
1254 mDragCell[1] = -1;
1255 mDragOutlineAnims[mDragOutlineCurrent].animateOut();
1256 mDragOutlineCurrent = (mDragOutlineCurrent + 1) % mDragOutlineAnims.length;
1257
Michael Jurka33945b22010-12-21 18:19:38 -08001258 setIsDragOverlapping(false);
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001259 }
1260
1261 /**
Winson Chungaafa03c2010-06-11 17:34:16 -07001262 * Mark a child as having been dropped.
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001263 * At the beginning of the drag operation, the child may have been on another
Patrick Dubroyce34a972010-10-19 10:34:32 -07001264 * screen, but it is re-parented before this method is called.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001265 *
1266 * @param child The child that is being dropped
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001267 */
Michael Jurkad3ef3062010-11-23 16:23:58 -08001268 void onDropChild(View child, boolean animate) {
Romain Guyd94533d2009-08-17 10:01:15 -07001269 if (child != null) {
1270 LayoutParams lp = (LayoutParams) child.getLayoutParams();
Romain Guyd94533d2009-08-17 10:01:15 -07001271 lp.isDragging = false;
Romain Guy84f296c2009-11-04 15:00:44 -08001272 lp.dropped = true;
Michael Jurkad3ef3062010-11-23 16:23:58 -08001273 lp.animateDrop = animate;
Patrick Dubroye3887cc2011-01-20 10:43:40 -08001274 child.setVisibility(animate ? View.INVISIBLE : View.VISIBLE);
Romain Guyd94533d2009-08-17 10:01:15 -07001275 child.requestLayout();
Romain Guyd94533d2009-08-17 10:01:15 -07001276 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001277 }
1278
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001279 /**
1280 * Start dragging the specified child
Winson Chungaafa03c2010-06-11 17:34:16 -07001281 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001282 * @param child The child that is being dragged
1283 */
1284 void onDragChild(View child) {
1285 LayoutParams lp = (LayoutParams) child.getLayoutParams();
1286 lp.isDragging = true;
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001287 }
1288
1289 /**
1290 * A drag event has begun over this layout.
1291 * It may have begun over this layout (in which case onDragChild is called first),
1292 * or it may have begun on another layout.
1293 */
Winson Chunga9abd0e2010-10-27 17:18:37 -07001294 void onDragEnter() {
Patrick Dubroyfe6bd872010-10-13 17:32:10 -07001295 if (!mDragging) {
Patrick Dubroyfe6bd872010-10-13 17:32:10 -07001296 // Fade in the drag indicators
1297 if (mCrosshairsAnimator != null) {
1298 mCrosshairsAnimator.animateIn();
1299 }
Joe Onorato4be866d2010-10-10 11:26:02 -07001300 }
1301 mDragging = true;
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 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001305 * Computes a bounding rectangle for a range of cells
Winson Chungaafa03c2010-06-11 17:34:16 -07001306 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001307 * @param cellX X coordinate of upper left corner expressed as a cell position
1308 * @param cellY Y coordinate of upper left corner expressed as a cell position
Winson Chungaafa03c2010-06-11 17:34:16 -07001309 * @param cellHSpan Width in cells
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001310 * @param cellVSpan Height in cells
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001311 * @param resultRect Rect into which to put the results
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001312 */
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001313 public void cellToRect(int cellX, int cellY, int cellHSpan, int cellVSpan, RectF resultRect) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001314 final int cellWidth = mCellWidth;
1315 final int cellHeight = mCellHeight;
1316 final int widthGap = mWidthGap;
1317 final int heightGap = mHeightGap;
Winson Chungaafa03c2010-06-11 17:34:16 -07001318
1319 final int hStartPadding = getLeftPadding();
1320 final int vStartPadding = getTopPadding();
1321
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001322 int width = cellHSpan * cellWidth + ((cellHSpan - 1) * widthGap);
1323 int height = cellVSpan * cellHeight + ((cellVSpan - 1) * heightGap);
1324
1325 int x = hStartPadding + cellX * (cellWidth + widthGap);
1326 int y = vStartPadding + cellY * (cellHeight + heightGap);
Winson Chungaafa03c2010-06-11 17:34:16 -07001327
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001328 resultRect.set(x, y, x + width, y + height);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001329 }
Winson Chungaafa03c2010-06-11 17:34:16 -07001330
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001331 /**
Winson Chungaafa03c2010-06-11 17:34:16 -07001332 * Computes the required horizontal and vertical cell spans to always
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001333 * fit the given rectangle.
Winson Chungaafa03c2010-06-11 17:34:16 -07001334 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001335 * @param width Width in pixels
1336 * @param height Height in pixels
Patrick Dubroy8f86ddc2010-07-16 13:55:32 -07001337 * @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 -08001338 */
Patrick Dubroy8f86ddc2010-07-16 13:55:32 -07001339 public int[] rectToCell(int width, int height, int[] result) {
Michael Jurka9987a5c2010-10-08 16:58:12 -07001340 return rectToCell(getResources(), width, height, result);
1341 }
1342
1343 public static int[] rectToCell(Resources resources, int width, int height, int[] result) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001344 // Always assume we're working with the smallest span to make sure we
1345 // reserve enough space in both orientations.
Joe Onorato79e56262009-09-21 15:23:04 -04001346 int actualWidth = resources.getDimensionPixelSize(R.dimen.workspace_cell_width);
1347 int actualHeight = resources.getDimensionPixelSize(R.dimen.workspace_cell_height);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001348 int smallerSize = Math.min(actualWidth, actualHeight);
Joe Onorato79e56262009-09-21 15:23:04 -04001349
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001350 // Always round up to next largest cell
1351 int spanX = (width + smallerSize) / smallerSize;
1352 int spanY = (height + smallerSize) / smallerSize;
Joe Onorato79e56262009-09-21 15:23:04 -04001353
Patrick Dubroy8f86ddc2010-07-16 13:55:32 -07001354 if (result == null) {
1355 return new int[] { spanX, spanY };
1356 }
1357 result[0] = spanX;
1358 result[1] = spanY;
1359 return result;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001360 }
1361
Michael Jurkaf12c75c2011-01-25 22:41:40 -08001362 public int[] cellSpansToSize(int hSpans, int vSpans) {
1363 int[] size = new int[2];
1364 size[0] = hSpans * mCellWidth + (hSpans - 1) * mWidthGap;
1365 size[1] = vSpans * mCellHeight + (vSpans - 1) * mHeightGap;
1366 return size;
1367 }
1368
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001369 /**
Patrick Dubroy047379a2010-12-19 22:02:04 -08001370 * Calculate the grid spans needed to fit given item
1371 */
1372 public void calculateSpans(ItemInfo info) {
1373 final int minWidth;
1374 final int minHeight;
1375
1376 if (info instanceof LauncherAppWidgetInfo) {
1377 minWidth = ((LauncherAppWidgetInfo) info).minWidth;
1378 minHeight = ((LauncherAppWidgetInfo) info).minHeight;
1379 } else if (info instanceof PendingAddWidgetInfo) {
1380 minWidth = ((PendingAddWidgetInfo) info).minWidth;
1381 minHeight = ((PendingAddWidgetInfo) info).minHeight;
1382 } else {
1383 // It's not a widget, so it must be 1x1
1384 info.spanX = info.spanY = 1;
1385 return;
1386 }
1387 int[] spans = rectToCell(minWidth, minHeight, null);
1388 info.spanX = spans[0];
1389 info.spanY = spans[1];
1390 }
1391
1392 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001393 * Find the first vacant cell, if there is one.
1394 *
1395 * @param vacant Holds the x and y coordinate of the vacant cell
1396 * @param spanX Horizontal cell span.
1397 * @param spanY Vertical cell span.
Winson Chungaafa03c2010-06-11 17:34:16 -07001398 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001399 * @return True if a vacant cell was found
1400 */
1401 public boolean getVacantCell(int[] vacant, int spanX, int spanY) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001402
Michael Jurka0280c3b2010-09-17 15:00:07 -07001403 return findVacantCell(vacant, spanX, spanY, mCountX, mCountY, mOccupied);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001404 }
1405
1406 static boolean findVacantCell(int[] vacant, int spanX, int spanY,
1407 int xCount, int yCount, boolean[][] occupied) {
1408
1409 for (int x = 0; x < xCount; x++) {
1410 for (int y = 0; y < yCount; y++) {
1411 boolean available = !occupied[x][y];
1412out: for (int i = x; i < x + spanX - 1 && x < xCount; i++) {
1413 for (int j = y; j < y + spanY - 1 && y < yCount; j++) {
1414 available = available && !occupied[i][j];
1415 if (!available) break out;
1416 }
1417 }
1418
1419 if (available) {
1420 vacant[0] = x;
1421 vacant[1] = y;
1422 return true;
1423 }
1424 }
1425 }
1426
1427 return false;
1428 }
1429
Michael Jurka0280c3b2010-09-17 15:00:07 -07001430 private void clearOccupiedCells() {
1431 for (int x = 0; x < mCountX; x++) {
1432 for (int y = 0; y < mCountY; y++) {
1433 mOccupied[x][y] = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001434 }
1435 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001436 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001437
Adam Cohen1b607ed2011-03-03 17:26:50 -08001438 /**
1439 * Given a view, determines how much that view can be expanded in all directions, in terms of
1440 * whether or not there are other items occupying adjacent cells. Used by the
1441 * AppWidgetResizeFrame to determine how the widget can be resized.
1442 */
Adam Cohend4844c32011-02-18 19:25:06 -08001443 public void getExpandabilityArrayForView(View view, int[] expandability) {
Adam Cohen1b607ed2011-03-03 17:26:50 -08001444 final LayoutParams lp = (LayoutParams) view.getLayoutParams();
Adam Cohend4844c32011-02-18 19:25:06 -08001445 boolean flag;
1446
Adam Cohen1b607ed2011-03-03 17:26:50 -08001447 expandability[AppWidgetResizeFrame.LEFT] = 0;
Adam Cohend4844c32011-02-18 19:25:06 -08001448 for (int x = lp.cellX - 1; x >= 0; x--) {
1449 flag = false;
1450 for (int y = lp.cellY; y < lp.cellY + lp.cellVSpan; y++) {
1451 if (mOccupied[x][y]) flag = true;
1452 }
1453 if (flag) break;
Adam Cohen1b607ed2011-03-03 17:26:50 -08001454 expandability[AppWidgetResizeFrame.LEFT]++;
Adam Cohend4844c32011-02-18 19:25:06 -08001455 }
1456
Adam Cohen1b607ed2011-03-03 17:26:50 -08001457 expandability[AppWidgetResizeFrame.TOP] = 0;
Adam Cohend4844c32011-02-18 19:25:06 -08001458 for (int y = lp.cellY - 1; y >= 0; y--) {
1459 flag = false;
1460 for (int x = lp.cellX; x < lp.cellX + lp.cellHSpan; x++) {
1461 if (mOccupied[x][y]) flag = true;
1462 }
1463 if (flag) break;
Adam Cohen1b607ed2011-03-03 17:26:50 -08001464 expandability[AppWidgetResizeFrame.TOP]++;
1465 }
Adam Cohend4844c32011-02-18 19:25:06 -08001466
Adam Cohen1b607ed2011-03-03 17:26:50 -08001467 expandability[AppWidgetResizeFrame.RIGHT] = 0;
Adam Cohend4844c32011-02-18 19:25:06 -08001468 for (int x = lp.cellX + lp.cellHSpan; x < mCountX; x++) {
1469 flag = false;
1470 for (int y = lp.cellY; y < lp.cellY + lp.cellVSpan; y++) {
1471 if (mOccupied[x][y]) flag = true;
1472 }
1473 if (flag) break;
Adam Cohen1b607ed2011-03-03 17:26:50 -08001474 expandability[AppWidgetResizeFrame.RIGHT]++;
1475 }
Adam Cohend4844c32011-02-18 19:25:06 -08001476
Adam Cohen1b607ed2011-03-03 17:26:50 -08001477 expandability[AppWidgetResizeFrame.BOTTOM] = 0;
Adam Cohend4844c32011-02-18 19:25:06 -08001478 for (int y = lp.cellY + lp.cellVSpan; y < mCountY; y++) {
1479 flag = false;
1480 for (int x = lp.cellX; x < lp.cellX + lp.cellHSpan; x++) {
1481 if (mOccupied[x][y]) flag = true;
1482 }
1483 if (flag) break;
Adam Cohen1b607ed2011-03-03 17:26:50 -08001484 expandability[AppWidgetResizeFrame.BOTTOM]++;
1485 }
Adam Cohend4844c32011-02-18 19:25:06 -08001486 }
1487
Michael Jurka0280c3b2010-09-17 15:00:07 -07001488 public void onMove(View view, int newCellX, int newCellY) {
1489 LayoutParams lp = (LayoutParams) view.getLayoutParams();
1490 markCellsAsUnoccupiedForView(view);
1491 markCellsForView(newCellX, newCellY, lp.cellHSpan, lp.cellVSpan, true);
1492 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001493
Adam Cohend4844c32011-02-18 19:25:06 -08001494 public void markCellsAsOccupiedForView(View view) {
Michael Jurka8c920dd2011-01-20 14:16:56 -08001495 if (view == null || view.getParent() != mChildren) return;
Michael Jurka0280c3b2010-09-17 15:00:07 -07001496 LayoutParams lp = (LayoutParams) view.getLayoutParams();
1497 markCellsForView(lp.cellX, lp.cellY, lp.cellHSpan, lp.cellVSpan, true);
1498 }
1499
Adam Cohend4844c32011-02-18 19:25:06 -08001500 public void markCellsAsUnoccupiedForView(View view) {
Michael Jurka8c920dd2011-01-20 14:16:56 -08001501 if (view == null || view.getParent() != mChildren) return;
Michael Jurka0280c3b2010-09-17 15:00:07 -07001502 LayoutParams lp = (LayoutParams) view.getLayoutParams();
1503 markCellsForView(lp.cellX, lp.cellY, lp.cellHSpan, lp.cellVSpan, false);
1504 }
1505
1506 private void markCellsForView(int cellX, int cellY, int spanX, int spanY, boolean value) {
1507 for (int x = cellX; x < cellX + spanX && x < mCountX; x++) {
1508 for (int y = cellY; y < cellY + spanY && y < mCountY; y++) {
1509 mOccupied[x][y] = value;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001510 }
1511 }
1512 }
1513
1514 @Override
1515 public ViewGroup.LayoutParams generateLayoutParams(AttributeSet attrs) {
1516 return new CellLayout.LayoutParams(getContext(), attrs);
1517 }
1518
1519 @Override
1520 protected boolean checkLayoutParams(ViewGroup.LayoutParams p) {
1521 return p instanceof CellLayout.LayoutParams;
1522 }
1523
1524 @Override
1525 protected ViewGroup.LayoutParams generateLayoutParams(ViewGroup.LayoutParams p) {
1526 return new CellLayout.LayoutParams(p);
1527 }
1528
Winson Chungaafa03c2010-06-11 17:34:16 -07001529 public static class CellLayoutAnimationController extends LayoutAnimationController {
1530 public CellLayoutAnimationController(Animation animation, float delay) {
1531 super(animation, delay);
1532 }
1533
1534 @Override
1535 protected long getDelayForView(View view) {
1536 return (int) (Math.random() * 150);
1537 }
1538 }
1539
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001540 public static class LayoutParams extends ViewGroup.MarginLayoutParams {
1541 /**
1542 * Horizontal location of the item in the grid.
1543 */
1544 @ViewDebug.ExportedProperty
1545 public int cellX;
1546
1547 /**
1548 * Vertical location of the item in the grid.
1549 */
1550 @ViewDebug.ExportedProperty
1551 public int cellY;
1552
1553 /**
1554 * Number of cells spanned horizontally by the item.
1555 */
1556 @ViewDebug.ExportedProperty
1557 public int cellHSpan;
1558
1559 /**
1560 * Number of cells spanned vertically by the item.
1561 */
1562 @ViewDebug.ExportedProperty
1563 public int cellVSpan;
Winson Chungaafa03c2010-06-11 17:34:16 -07001564
Adam Cohen1b607ed2011-03-03 17:26:50 -08001565 /**
1566 * Indicates whether the item will set its x, y, width and height parameters freely,
1567 * or whether these will be computed based on cellX, cellY, cellHSpan and cellVSpan.
1568 */
Adam Cohend4844c32011-02-18 19:25:06 -08001569 public boolean isLockedToGrid = true;
1570
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001571 /**
1572 * Is this item currently being dragged
1573 */
1574 public boolean isDragging;
1575
1576 // X coordinate of the view in the layout.
1577 @ViewDebug.ExportedProperty
1578 int x;
1579 // Y coordinate of the view in the layout.
1580 @ViewDebug.ExportedProperty
1581 int y;
1582
Patrick Dubroyce34a972010-10-19 10:34:32 -07001583 /**
1584 * The old X coordinate of this item, relative to its current parent.
1585 * Used to animate the item into its new position.
1586 */
1587 int oldX;
1588
1589 /**
1590 * The old Y coordinate of this item, relative to its current parent.
1591 * Used to animate the item into its new position.
1592 */
1593 int oldY;
1594
Romain Guy84f296c2009-11-04 15:00:44 -08001595 boolean dropped;
Romain Guyfcb9e712009-10-02 16:06:52 -07001596
Michael Jurkad3ef3062010-11-23 16:23:58 -08001597 boolean animateDrop;
1598
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001599 public LayoutParams(Context c, AttributeSet attrs) {
1600 super(c, attrs);
1601 cellHSpan = 1;
1602 cellVSpan = 1;
1603 }
1604
1605 public LayoutParams(ViewGroup.LayoutParams source) {
1606 super(source);
1607 cellHSpan = 1;
1608 cellVSpan = 1;
1609 }
Winson Chungaafa03c2010-06-11 17:34:16 -07001610
1611 public LayoutParams(LayoutParams source) {
1612 super(source);
1613 this.cellX = source.cellX;
1614 this.cellY = source.cellY;
1615 this.cellHSpan = source.cellHSpan;
1616 this.cellVSpan = source.cellVSpan;
1617 }
1618
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001619 public LayoutParams(int cellX, int cellY, int cellHSpan, int cellVSpan) {
Romain Guy8f19cdd2010-01-08 15:07:00 -08001620 super(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001621 this.cellX = cellX;
1622 this.cellY = cellY;
1623 this.cellHSpan = cellHSpan;
1624 this.cellVSpan = cellVSpan;
1625 }
1626
1627 public void setup(int cellWidth, int cellHeight, int widthGap, int heightGap,
1628 int hStartPadding, int vStartPadding) {
Adam Cohend4844c32011-02-18 19:25:06 -08001629 if (isLockedToGrid) {
1630 final int myCellHSpan = cellHSpan;
1631 final int myCellVSpan = cellVSpan;
1632 final int myCellX = cellX;
1633 final int myCellY = cellY;
Adam Cohen1b607ed2011-03-03 17:26:50 -08001634
Adam Cohend4844c32011-02-18 19:25:06 -08001635 width = myCellHSpan * cellWidth + ((myCellHSpan - 1) * widthGap) -
1636 leftMargin - rightMargin;
1637 height = myCellVSpan * cellHeight + ((myCellVSpan - 1) * heightGap) -
1638 topMargin - bottomMargin;
Adam Cohend4844c32011-02-18 19:25:06 -08001639 x = hStartPadding + myCellX * (cellWidth + widthGap) + leftMargin;
1640 y = vStartPadding + myCellY * (cellHeight + heightGap) + topMargin;
1641 }
1642 }
Winson Chungaafa03c2010-06-11 17:34:16 -07001643
Adam Cohend4844c32011-02-18 19:25:06 -08001644 public void setWidth(int width) {
1645 this.width = width;
1646 }
Winson Chungaafa03c2010-06-11 17:34:16 -07001647
Adam Cohend4844c32011-02-18 19:25:06 -08001648 public int getWidth() {
1649 return width;
1650 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001651
Adam Cohend4844c32011-02-18 19:25:06 -08001652 public void setHeight(int height) {
1653 this.height = height;
1654 }
1655
1656 public int getHeight() {
1657 return height;
1658 }
1659
1660 public void setX(int x) {
1661 this.x = x;
1662 }
1663
1664 public int getX() {
1665 return x;
1666 }
1667
1668 public void setY(int y) {
1669 this.y = y;
1670 }
1671
1672 public int getY() {
1673 return y;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001674 }
Winson Chungaafa03c2010-06-11 17:34:16 -07001675
1676 public String toString() {
1677 return "(" + this.cellX + ", " + this.cellY + ")";
1678 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001679 }
1680
Michael Jurka0280c3b2010-09-17 15:00:07 -07001681 // This class stores info for two purposes:
1682 // 1. When dragging items (mDragInfo in Workspace), we store the View, its cellX & cellY,
1683 // its spanX, spanY, and the screen it is on
1684 // 2. When long clicking on an empty cell in a CellLayout, we save information about the
1685 // cellX and cellY coordinates and which page was clicked. We then set this as a tag on
1686 // the CellLayout that was long clicked
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001687 static final class CellInfo implements ContextMenu.ContextMenuInfo {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001688 View cell;
Michael Jurkaa63c4522010-08-19 13:52:27 -07001689 int cellX = -1;
1690 int cellY = -1;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001691 int spanX;
1692 int spanY;
1693 int screen;
1694 boolean valid;
1695
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001696 @Override
1697 public String toString() {
Winson Chungaafa03c2010-06-11 17:34:16 -07001698 return "Cell[view=" + (cell == null ? "null" : cell.getClass())
1699 + ", x=" + cellX + ", y=" + cellY + "]";
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001700 }
1701 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001702}