blob: fdef18da4a5d551f72441a5f986120d03b9f4982 [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
Adam Cohen66396872011-04-15 17:50:36 -070019import java.util.Arrays;
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.MotionEvent;
43import android.view.View;
44import android.view.ViewDebug;
45import android.view.ViewGroup;
Winson Chungaafa03c2010-06-11 17:34:16 -070046import android.view.animation.Animation;
Winson Chung150fbab2010-09-29 17:14:26 -070047import android.view.animation.DecelerateInterpolator;
Winson Chungaafa03c2010-06-11 17:34:16 -070048import android.view.animation.LayoutAnimationController;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080049
Adam Cohen66396872011-04-15 17:50:36 -070050import com.android.launcher.R;
Patrick Dubroy8e58e912010-10-14 13:21:48 -070051
Michael Jurkabdb5c532011-02-01 15:05:06 -080052public class CellLayout extends ViewGroup {
Winson Chungaafa03c2010-06-11 17:34:16 -070053 static final String TAG = "CellLayout";
54
The Android Open Source Project31dd5032009-03-03 19:32:27 -080055 private int mCellWidth;
56 private int mCellHeight;
Winson Chungaafa03c2010-06-11 17:34:16 -070057
Winson Chungaafa03c2010-06-11 17:34:16 -070058 private int mLeftPadding;
59 private int mRightPadding;
60 private int mTopPadding;
61 private int mBottomPadding;
62
Adam Cohend22015c2010-07-26 22:02:18 -070063 private int mCountX;
64 private int mCountY;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080065
66 private int mWidthGap;
67 private int mHeightGap;
68
69 private final Rect mRect = new Rect();
70 private final CellInfo mCellInfo = new CellInfo();
Winson Chungaafa03c2010-06-11 17:34:16 -070071
Patrick Dubroyde7658b2010-09-27 11:15:43 -070072 // These are temporary variables to prevent having to allocate a new object just to
73 // return an (x, y) value from helper functions. Do NOT use them to maintain other state.
Patrick Dubroy6569f2c2010-07-12 14:25:18 -070074 private final int[] mTmpCellXY = new int[2];
Patrick Dubroyde7658b2010-09-27 11:15:43 -070075 private final int[] mTmpPoint = new int[2];
76 private final PointF mTmpPointF = new PointF();
Patrick Dubroy6569f2c2010-07-12 14:25:18 -070077
The Android Open Source Project31dd5032009-03-03 19:32:27 -080078 boolean[][] mOccupied;
79
Michael Jurkadee05892010-07-27 10:01:56 -070080 private OnTouchListener mInterceptTouchListener;
81
Michael Jurka5f1c5092010-09-03 14:15:02 -070082 private float mBackgroundAlpha;
Adam Cohen1b0aaac2010-10-28 11:11:18 -070083 private float mBackgroundAlphaMultiplier = 1.0f;
Adam Cohenf34bab52010-09-30 14:11:56 -070084
Michael Jurka33945b22010-12-21 18:19:38 -080085 private Drawable mNormalBackground;
Michael Jurka33945b22010-12-21 18:19:38 -080086 private Drawable mActiveBackground;
87 private Drawable mActiveGlowBackground;
88 private Drawable mNormalBackgroundMini;
89 private Drawable mNormalGlowBackgroundMini;
90 private Drawable mActiveBackgroundMini;
91 private Drawable mActiveGlowBackgroundMini;
Michael Jurka18014792010-10-14 09:01:34 -070092 private Rect mBackgroundRect;
Michael Jurka33945b22010-12-21 18:19:38 -080093 private Rect mGlowBackgroundRect;
94 private float mGlowBackgroundScale;
95 private float mGlowBackgroundAlpha;
Patrick Dubroy1262e362010-10-06 15:49:50 -070096
Adam Cohendf035382011-04-11 17:22:04 -070097 private boolean mAcceptsDrops = true;
Michael Jurka33945b22010-12-21 18:19:38 -080098 // If we're actively dragging something over this screen, mIsDragOverlapping is true
99 private boolean mIsDragOverlapping = false;
100 private boolean mIsDragOccuring = false;
101 private boolean mIsDefaultDropTarget = false;
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700102 private final Point mDragCenter = new Point();
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700103
Winson Chung150fbab2010-09-29 17:14:26 -0700104 // These arrays are used to implement the drag visualization on x-large screens.
Joe Onorato4be866d2010-10-10 11:26:02 -0700105 // They are used as circular arrays, indexed by mDragOutlineCurrent.
Winson Chung63257c12011-05-05 17:06:13 -0700106 private Point[] mDragOutlines = new Point[4];
Chet Haase472b2812010-10-14 07:02:04 -0700107 private float[] mDragOutlineAlphas = new float[mDragOutlines.length];
Joe Onorato4be866d2010-10-10 11:26:02 -0700108 private InterruptibleInOutAnimator[] mDragOutlineAnims =
109 new InterruptibleInOutAnimator[mDragOutlines.length];
Winson Chung150fbab2010-09-29 17:14:26 -0700110
111 // Used as an index into the above 3 arrays; indicates which is the most current value.
Joe Onorato4be866d2010-10-10 11:26:02 -0700112 private int mDragOutlineCurrent = 0;
Patrick Dubroy8e58e912010-10-14 13:21:48 -0700113 private final Paint mDragOutlinePaint = new Paint();
Winson Chung150fbab2010-09-29 17:14:26 -0700114
Patrick Dubroy96864c32011-03-10 17:17:23 -0800115 private BubbleTextView mPressedOrFocusedIcon;
116
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700117 private Drawable mCrosshairsDrawable = null;
Patrick Dubroy49250ad2010-10-08 15:33:52 -0700118 private InterruptibleInOutAnimator mCrosshairsAnimator = null;
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700119 private float mCrosshairsVisibility = 0.0f;
120
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700121 // When a drag operation is in progress, holds the nearest cell to the touch point
122 private final int[] mDragCell = new int[2];
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800123
Joe Onorato4be866d2010-10-10 11:26:02 -0700124 private boolean mDragging = false;
125
Patrick Dubroyce34a972010-10-19 10:34:32 -0700126 private TimeInterpolator mEaseOutInterpolator;
Michael Jurka8c920dd2011-01-20 14:16:56 -0800127 private CellLayoutChildren mChildren;
Patrick Dubroyce34a972010-10-19 10:34:32 -0700128
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800129 public CellLayout(Context context) {
130 this(context, null);
131 }
132
133 public CellLayout(Context context, AttributeSet attrs) {
134 this(context, attrs, 0);
135 }
136
137 public CellLayout(Context context, AttributeSet attrs, int defStyle) {
138 super(context, attrs, defStyle);
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700139
140 // A ViewGroup usually does not draw, but CellLayout needs to draw a rectangle to show
141 // the user where a dragged item will land when dropped.
142 setWillNotDraw(false);
Michael Jurkaa63c4522010-08-19 13:52:27 -0700143
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800144 TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.CellLayout, defStyle, 0);
145
146 mCellWidth = a.getDimensionPixelSize(R.styleable.CellLayout_cellWidth, 10);
147 mCellHeight = a.getDimensionPixelSize(R.styleable.CellLayout_cellHeight, 10);
Winson Chungece7f5b2010-10-22 14:54:12 -0700148 mWidthGap = a.getDimensionPixelSize(R.styleable.CellLayout_widthGap, -1);
149 mHeightGap = a.getDimensionPixelSize(R.styleable.CellLayout_heightGap, -1);
Winson Chungaafa03c2010-06-11 17:34:16 -0700150
Adam Cohend22015c2010-07-26 22:02:18 -0700151 mLeftPadding =
152 a.getDimensionPixelSize(R.styleable.CellLayout_xAxisStartPadding, 10);
153 mRightPadding =
154 a.getDimensionPixelSize(R.styleable.CellLayout_xAxisEndPadding, 10);
155 mTopPadding =
156 a.getDimensionPixelSize(R.styleable.CellLayout_yAxisStartPadding, 10);
157 mBottomPadding =
158 a.getDimensionPixelSize(R.styleable.CellLayout_yAxisEndPadding, 10);
Winson Chungaafa03c2010-06-11 17:34:16 -0700159
Adam Cohend22015c2010-07-26 22:02:18 -0700160 mCountX = LauncherModel.getCellCountX();
161 mCountY = LauncherModel.getCellCountY();
Michael Jurka0280c3b2010-09-17 15:00:07 -0700162 mOccupied = new boolean[mCountX][mCountY];
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800163
164 a.recycle();
165
166 setAlwaysDrawnWithCacheEnabled(false);
167
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700168 final Resources res = getResources();
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700169
Michael Jurkaa2eb1702011-05-12 14:57:05 -0700170 if (LauncherApplication.isScreenLarge()) {
Michael Jurka33945b22010-12-21 18:19:38 -0800171 mNormalBackground = res.getDrawable(R.drawable.homescreen_large_blue);
Michael Jurka33945b22010-12-21 18:19:38 -0800172 mActiveBackground = res.getDrawable(R.drawable.homescreen_large_green);
173 mActiveGlowBackground = res.getDrawable(R.drawable.homescreen_large_green_strong);
174
175 mNormalBackgroundMini = res.getDrawable(R.drawable.homescreen_small_blue);
176 mNormalGlowBackgroundMini = res.getDrawable(R.drawable.homescreen_small_blue_strong);
177 mActiveBackgroundMini = res.getDrawable(R.drawable.homescreen_small_green);
178 mActiveGlowBackgroundMini = res.getDrawable(R.drawable.homescreen_small_green_strong);
179
180 mNormalBackground.setFilterBitmap(true);
Michael Jurka33945b22010-12-21 18:19:38 -0800181 mActiveBackground.setFilterBitmap(true);
182 mActiveGlowBackground.setFilterBitmap(true);
183 mNormalBackgroundMini.setFilterBitmap(true);
184 mNormalGlowBackgroundMini.setFilterBitmap(true);
185 mActiveBackgroundMini.setFilterBitmap(true);
186 mActiveGlowBackgroundMini.setFilterBitmap(true);
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700187 }
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700188
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700189 // Initialize the data structures used for the drag visualization.
Winson Chung150fbab2010-09-29 17:14:26 -0700190
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700191 mCrosshairsDrawable = res.getDrawable(R.drawable.gardening_crosshairs);
Patrick Dubroyce34a972010-10-19 10:34:32 -0700192 mEaseOutInterpolator = new DecelerateInterpolator(2.5f); // Quint ease out
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700193
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700194 // Set up the animation for fading the crosshairs in and out
195 int animDuration = res.getInteger(R.integer.config_crosshairsFadeInTime);
Patrick Dubroy49250ad2010-10-08 15:33:52 -0700196 mCrosshairsAnimator = new InterruptibleInOutAnimator(animDuration, 0.0f, 1.0f);
Chet Haase472b2812010-10-14 07:02:04 -0700197 mCrosshairsAnimator.getAnimator().addUpdateListener(new AnimatorUpdateListener() {
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700198 public void onAnimationUpdate(ValueAnimator animation) {
199 mCrosshairsVisibility = ((Float) animation.getAnimatedValue()).floatValue();
Patrick Dubroy8e58e912010-10-14 13:21:48 -0700200 invalidate();
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700201 }
202 });
Patrick Dubroyce34a972010-10-19 10:34:32 -0700203 mCrosshairsAnimator.getAnimator().setInterpolator(mEaseOutInterpolator);
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700204
Joe Onorato4be866d2010-10-10 11:26:02 -0700205 for (int i = 0; i < mDragOutlines.length; i++) {
206 mDragOutlines[i] = new Point(-1, -1);
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700207 }
208
209 // When dragging things around the home screens, we show a green outline of
210 // where the item will land. The outlines gradually fade out, leaving a trail
211 // behind the drag path.
212 // Set up all the animations that are used to implement this fading.
213 final int duration = res.getInteger(R.integer.config_dragOutlineFadeTime);
Chet Haase472b2812010-10-14 07:02:04 -0700214 final float fromAlphaValue = 0;
215 final float toAlphaValue = (float)res.getInteger(R.integer.config_dragOutlineMaxAlpha);
Joe Onorato4be866d2010-10-10 11:26:02 -0700216
Patrick Dubroy8e58e912010-10-14 13:21:48 -0700217 Arrays.fill(mDragOutlineAlphas, fromAlphaValue);
Joe Onorato4be866d2010-10-10 11:26:02 -0700218
219 for (int i = 0; i < mDragOutlineAnims.length; i++) {
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700220 final InterruptibleInOutAnimator anim =
221 new InterruptibleInOutAnimator(duration, fromAlphaValue, toAlphaValue);
Patrick Dubroyce34a972010-10-19 10:34:32 -0700222 anim.getAnimator().setInterpolator(mEaseOutInterpolator);
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700223 final int thisIndex = i;
Chet Haase472b2812010-10-14 07:02:04 -0700224 anim.getAnimator().addUpdateListener(new AnimatorUpdateListener() {
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700225 public void onAnimationUpdate(ValueAnimator animation) {
Joe Onorato4be866d2010-10-10 11:26:02 -0700226 final Bitmap outline = (Bitmap)anim.getTag();
227
228 // If an animation is started and then stopped very quickly, we can still
229 // get spurious updates we've cleared the tag. Guard against this.
230 if (outline == null) {
Patrick Dubroyfe6bd872010-10-13 17:32:10 -0700231 if (false) {
232 Object val = animation.getAnimatedValue();
233 Log.d(TAG, "anim " + thisIndex + " update: " + val +
234 ", isStopped " + anim.isStopped());
235 }
Joe Onorato4be866d2010-10-10 11:26:02 -0700236 // Try to prevent it from continuing to run
237 animation.cancel();
238 } else {
Chet Haase472b2812010-10-14 07:02:04 -0700239 mDragOutlineAlphas[thisIndex] = (Float) animation.getAnimatedValue();
Joe Onorato4be866d2010-10-10 11:26:02 -0700240 final int left = mDragOutlines[thisIndex].x;
241 final int top = mDragOutlines[thisIndex].y;
242 CellLayout.this.invalidate(left, top,
243 left + outline.getWidth(), top + outline.getHeight());
244 }
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700245 }
246 });
Joe Onorato4be866d2010-10-10 11:26:02 -0700247 // The animation holds a reference to the drag outline bitmap as long is it's
248 // running. This way the bitmap can be GCed when the animations are complete.
Chet Haase472b2812010-10-14 07:02:04 -0700249 anim.getAnimator().addListener(new AnimatorListenerAdapter() {
Michael Jurka3c4c20f2010-10-28 15:36:06 -0700250 @Override
Joe Onorato4be866d2010-10-10 11:26:02 -0700251 public void onAnimationEnd(Animator animation) {
Chet Haase472b2812010-10-14 07:02:04 -0700252 if ((Float) ((ValueAnimator) animation).getAnimatedValue() == 0f) {
Joe Onorato4be866d2010-10-10 11:26:02 -0700253 anim.setTag(null);
254 }
255 }
256 });
257 mDragOutlineAnims[i] = anim;
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700258 }
Patrick Dubroyce34a972010-10-19 10:34:32 -0700259
Michael Jurka18014792010-10-14 09:01:34 -0700260 mBackgroundRect = new Rect();
Michael Jurka33945b22010-12-21 18:19:38 -0800261 mGlowBackgroundRect = new Rect();
Michael Jurka18014792010-10-14 09:01:34 -0700262 setHoverScale(1.0f);
263 setHoverAlpha(1.0f);
Michael Jurkabea15192010-11-17 12:33:46 -0800264
Michael Jurka8c920dd2011-01-20 14:16:56 -0800265 mChildren = new CellLayoutChildren(context);
Adam Cohen7f4eabe2011-04-21 16:19:16 -0700266 mChildren.setCellDimensions(mCellWidth, mCellHeight, mWidthGap, mHeightGap);
Michael Jurka8c920dd2011-01-20 14:16:56 -0800267 addView(mChildren);
Michael Jurka18014792010-10-14 09:01:34 -0700268 }
269
Michael Jurkaf6440da2011-04-05 14:50:34 -0700270 static int widthInPortrait(Resources r, int numCells) {
271 // We use this method from Workspace to figure out how many rows/columns Launcher should
272 // have. We ignore the left/right padding on CellLayout because it turns out in our design
273 // the padding extends outside the visible screen size, but it looked fine anyway.
274 // However, we make sure there's at least enough space for the crosshairs at either
275 // edge to be rendered (half the crosshair is sticking out on either side)
276 int cellWidth = r.getDimensionPixelSize(R.dimen.workspace_cell_width);
277 int widthGap = r.getDimensionPixelSize(R.dimen.workspace_width_gap_port);
278 int crosshairsSize = r.getDrawable(R.drawable.gardening_crosshairs).getIntrinsicWidth();
279
280 return widthGap * (numCells - 1) + cellWidth * numCells + crosshairsSize;
281 }
282
283 static int widthInLandscape(Resources r, int numCells) {
284 // We use this method from Workspace to figure out how many rows/columns Launcher should
285 // have. We ignore the left/right padding on CellLayout because it turns out in our design
286 // the padding extends outside the visible screen size, but it looked fine anyway.
287 // However, we make sure there's at least enough space for the crosshairs at either
288 // edge to be rendered (half the crosshair is sticking out on either side)
289 int cellWidth = r.getDimensionPixelSize(R.dimen.workspace_cell_width);
290 int widthGap = r.getDimensionPixelSize(R.dimen.workspace_width_gap_land);
291 int crosshairsSize = r.getDrawable(R.drawable.gardening_crosshairs).getIntrinsicWidth();
292
293 return widthGap * (numCells - 1) + cellWidth * numCells + crosshairsSize;
294 }
295
296 static int heightInPortrait(Resources r, int numCells) {
297 // We use this method from Workspace to figure out how many rows/columns Launcher should
298 // have. We ignore the left/right padding on CellLayout because it turns out in our design
299 // the padding extends outside the visible screen size, but it looked fine anyway.
300 // However, we make sure there's at least enough space for the crosshairs at the bottom
301 // to be rendered (half the crosshair is sticking out); we don't worry about the top
302 // crosshair since it can bleed into the action bar space
303 int cellHeight = r.getDimensionPixelSize(R.dimen.workspace_cell_height);
304 int heightGap = r.getDimensionPixelSize(R.dimen.workspace_height_gap_port);
305 int crosshairsSize = r.getDrawable(R.drawable.gardening_crosshairs).getIntrinsicHeight();
306
307 return heightGap * (numCells - 1) + cellHeight * numCells + (crosshairsSize + 1) / 2;
308 }
309
310 static int heightInLandscape(Resources r, int numCells) {
311 // We use this method from Workspace to figure out how many rows/columns Launcher should
312 // have. We ignore the left/right padding on CellLayout because it turns out in our design
313 // the padding extends outside the visible screen size, but it looked fine anyway.
314 // However, we make sure there's at least enough space for the crosshairs at the bottom
315 // to be rendered (half the crosshair is sticking out); we don't worry about the top
316 // crosshair since it can bleed into the action bar space
317 int cellHeight = r.getDimensionPixelSize(R.dimen.workspace_cell_height);
318 int heightGap = r.getDimensionPixelSize(R.dimen.workspace_height_gap_land);
319 int crosshairsSize = r.getDrawable(R.drawable.gardening_crosshairs).getIntrinsicHeight();
320
321 return heightGap * (numCells - 1) + cellHeight * numCells + (crosshairsSize + 1) / 2;
322 }
323
Patrick Dubroy96864c32011-03-10 17:17:23 -0800324 private void invalidateBubbleTextView(BubbleTextView icon) {
325 final int padding = icon.getPressedOrFocusedBackgroundPadding();
Adam Cohen7f4eabe2011-04-21 16:19:16 -0700326 invalidate(icon.getLeft() + getLeftPadding() - padding,
327 icon.getTop() + getTopPadding() - padding,
328 icon.getRight() + getLeftPadding() + padding,
329 icon.getBottom() + getTopPadding() + padding);
Patrick Dubroy96864c32011-03-10 17:17:23 -0800330 }
331
332 void setPressedOrFocusedIcon(BubbleTextView icon) {
333 // We draw the pressed or focused BubbleTextView's background in CellLayout because it
334 // requires an expanded clip rect (due to the glow's blur radius)
335 BubbleTextView oldIcon = mPressedOrFocusedIcon;
336 mPressedOrFocusedIcon = icon;
337 if (oldIcon != null) {
338 invalidateBubbleTextView(oldIcon);
339 }
340 if (mPressedOrFocusedIcon != null) {
341 invalidateBubbleTextView(mPressedOrFocusedIcon);
342 }
343 }
344
Winson Chung6e314082011-01-27 16:46:51 -0800345 public CellLayoutChildren getChildrenLayout() {
346 if (getChildCount() > 0) {
347 return (CellLayoutChildren) getChildAt(0);
348 }
349 return null;
350 }
351
Michael Jurka33945b22010-12-21 18:19:38 -0800352 public void setIsDefaultDropTarget(boolean isDefaultDropTarget) {
353 if (mIsDefaultDropTarget != isDefaultDropTarget) {
354 mIsDefaultDropTarget = isDefaultDropTarget;
355 invalidate();
356 }
357 }
358
Michael Jurka33945b22010-12-21 18:19:38 -0800359 void setIsDragOccuring(boolean isDragOccuring) {
360 if (mIsDragOccuring != isDragOccuring) {
361 mIsDragOccuring = isDragOccuring;
362 invalidate();
363 }
364 }
365
366 void setIsDragOverlapping(boolean isDragOverlapping) {
367 if (mIsDragOverlapping != isDragOverlapping) {
368 mIsDragOverlapping = isDragOverlapping;
369 invalidate();
370 }
371 }
372
373 boolean getIsDragOverlapping() {
374 return mIsDragOverlapping;
375 }
376
377 private void updateGlowRect() {
378 float marginFraction = (mGlowBackgroundScale - 1.0f) / 2.0f;
Michael Jurka18014792010-10-14 09:01:34 -0700379 int marginX = (int) (marginFraction * (mBackgroundRect.right - mBackgroundRect.left));
380 int marginY = (int) (marginFraction * (mBackgroundRect.bottom - mBackgroundRect.top));
Michael Jurka33945b22010-12-21 18:19:38 -0800381 mGlowBackgroundRect.set(mBackgroundRect.left - marginX, mBackgroundRect.top - marginY,
Michael Jurka18014792010-10-14 09:01:34 -0700382 mBackgroundRect.right + marginX, mBackgroundRect.bottom + marginY);
383 invalidate();
384 }
385
386 public void setHoverScale(float scaleFactor) {
Michael Jurka33945b22010-12-21 18:19:38 -0800387 if (scaleFactor != mGlowBackgroundScale) {
388 mGlowBackgroundScale = scaleFactor;
389 updateGlowRect();
Michael Jurka8deb1e62011-01-25 16:27:43 -0800390 if (getParent() != null) {
391 ((View) getParent()).invalidate();
392 }
Michael Jurka18014792010-10-14 09:01:34 -0700393 }
394 }
395
396 public float getHoverScale() {
Michael Jurka33945b22010-12-21 18:19:38 -0800397 return mGlowBackgroundScale;
Michael Jurka18014792010-10-14 09:01:34 -0700398 }
399
400 public float getHoverAlpha() {
Michael Jurka33945b22010-12-21 18:19:38 -0800401 return mGlowBackgroundAlpha;
Michael Jurka18014792010-10-14 09:01:34 -0700402 }
403
404 public void setHoverAlpha(float alpha) {
Michael Jurka33945b22010-12-21 18:19:38 -0800405 mGlowBackgroundAlpha = alpha;
Michael Jurka18014792010-10-14 09:01:34 -0700406 invalidate();
407 }
408
409 void animateDrop() {
Michael Jurkaa2eb1702011-05-12 14:57:05 -0700410 if (LauncherApplication.isScreenLarge()) {
Michael Jurka18014792010-10-14 09:01:34 -0700411 Resources res = getResources();
412 float onDropScale = res.getInteger(R.integer.config_screenOnDropScalePercent) / 100.0f;
413 ObjectAnimator scaleUp = ObjectAnimator.ofFloat(this, "hoverScale", onDropScale);
414 scaleUp.setDuration(res.getInteger(R.integer.config_screenOnDropScaleUpDuration));
415 ObjectAnimator scaleDown = ObjectAnimator.ofFloat(this, "hoverScale", 1.0f);
416 scaleDown.setDuration(res.getInteger(R.integer.config_screenOnDropScaleDownDuration));
417 ObjectAnimator alphaFadeOut = ObjectAnimator.ofFloat(this, "hoverAlpha", 0.0f);
418
419 alphaFadeOut.setStartDelay(res.getInteger(R.integer.config_screenOnDropAlphaFadeDelay));
420 alphaFadeOut.setDuration(res.getInteger(R.integer.config_screenOnDropAlphaFadeDelay));
421
422 AnimatorSet bouncer = new AnimatorSet();
423 bouncer.play(scaleUp).before(scaleDown);
424 bouncer.play(scaleUp).with(alphaFadeOut);
Michael Jurka8edd75c2010-12-17 20:15:06 -0800425 bouncer.addListener(new AnimatorListenerAdapter() {
Michael Jurka3c4c20f2010-10-28 15:36:06 -0700426 @Override
Michael Jurka18014792010-10-14 09:01:34 -0700427 public void onAnimationStart(Animator animation) {
Michael Jurka33945b22010-12-21 18:19:38 -0800428 setIsDragOverlapping(true);
Michael Jurka18014792010-10-14 09:01:34 -0700429 }
Michael Jurka3c4c20f2010-10-28 15:36:06 -0700430 @Override
Michael Jurka8edd75c2010-12-17 20:15:06 -0800431 public void onAnimationEnd(Animator animation) {
Michael Jurka33945b22010-12-21 18:19:38 -0800432 setIsDragOverlapping(false);
Michael Jurka18014792010-10-14 09:01:34 -0700433 setHoverScale(1.0f);
434 setHoverAlpha(1.0f);
435 }
436 });
437 bouncer.start();
438 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800439 }
440
Jeff Sharkey83f111d2009-04-20 21:03:13 -0700441 @Override
Patrick Dubroy1262e362010-10-06 15:49:50 -0700442 protected void onDraw(Canvas canvas) {
Michael Jurka3e7c7632010-10-02 16:01:03 -0700443 // When we're large, we are either drawn in a "hover" state (ie when dragging an item to
444 // a neighboring page) or with just a normal background (if backgroundAlpha > 0.0f)
445 // When we're small, we are either drawn normally or in the "accepts drops" state (during
446 // a drag). However, we also drag the mini hover background *over* one of those two
447 // backgrounds
Michael Jurkaa2eb1702011-05-12 14:57:05 -0700448 if (LauncherApplication.isScreenLarge() && mBackgroundAlpha > 0.0f) {
Adam Cohenf34bab52010-09-30 14:11:56 -0700449 Drawable bg;
Michael Jurka33945b22010-12-21 18:19:38 -0800450 boolean mini = getScaleX() < 0.5f;
451
452 if (mIsDragOverlapping) {
453 // In the mini case, we draw the active_glow bg *over* the active background
454 bg = mini ? mActiveBackgroundMini : mActiveGlowBackground;
455 } else if (mIsDragOccuring && mAcceptsDrops) {
456 bg = mini ? mActiveBackgroundMini : mActiveBackground;
Adam Cohen3af863b2011-01-25 12:16:51 -0800457 } else if (mIsDefaultDropTarget && mini) {
458 bg = mNormalGlowBackgroundMini;
Adam Cohenf34bab52010-09-30 14:11:56 -0700459 } else {
Michael Jurka33945b22010-12-21 18:19:38 -0800460 bg = mini ? mNormalBackgroundMini : mNormalBackground;
Adam Cohenf34bab52010-09-30 14:11:56 -0700461 }
Michael Jurka33945b22010-12-21 18:19:38 -0800462
463 bg.setAlpha((int) (mBackgroundAlpha * mBackgroundAlphaMultiplier * 255));
464 bg.setBounds(mBackgroundRect);
465 bg.draw(canvas);
466
467 if (mini && mIsDragOverlapping) {
Michael Jurka18014792010-10-14 09:01:34 -0700468 boolean modifiedClipRect = false;
Michael Jurka33945b22010-12-21 18:19:38 -0800469 if (mGlowBackgroundScale > 1.0f) {
Michael Jurka18014792010-10-14 09:01:34 -0700470 // If the hover background's scale is greater than 1, we'll be drawing outside
471 // the bounds of this CellLayout. Get around that by temporarily increasing the
472 // size of the clip rect
Michael Jurka33945b22010-12-21 18:19:38 -0800473 float marginFraction = (mGlowBackgroundScale - 1.0f) / 2.0f;
Michael Jurka18014792010-10-14 09:01:34 -0700474 Rect clipRect = canvas.getClipBounds();
475 int marginX = (int) (marginFraction * (clipRect.right - clipRect.left));
476 int marginY = (int) (marginFraction * (clipRect.bottom - clipRect.top));
477 canvas.save(Canvas.CLIP_SAVE_FLAG);
478 canvas.clipRect(-marginX, -marginY,
479 getWidth() + marginX, getHeight() + marginY, Region.Op.REPLACE);
480 modifiedClipRect = true;
481 }
482
Michael Jurka33945b22010-12-21 18:19:38 -0800483 mActiveGlowBackgroundMini.setAlpha(
484 (int) (mBackgroundAlpha * mGlowBackgroundAlpha * 255));
485 mActiveGlowBackgroundMini.setBounds(mGlowBackgroundRect);
486 mActiveGlowBackgroundMini.draw(canvas);
Michael Jurka18014792010-10-14 09:01:34 -0700487 if (modifiedClipRect) {
488 canvas.restore();
489 }
Michael Jurka3e7c7632010-10-02 16:01:03 -0700490 }
Michael Jurkaa63c4522010-08-19 13:52:27 -0700491 }
Romain Guya6abce82009-11-10 02:54:41 -0800492
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700493 if (mCrosshairsVisibility > 0.0f) {
494 final int countX = mCountX;
495 final int countY = mCountY;
496
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700497 final float MAX_ALPHA = 0.4f;
498 final int MAX_VISIBLE_DISTANCE = 600;
499 final float DISTANCE_MULTIPLIER = 0.002f;
500
501 final Drawable d = mCrosshairsDrawable;
502 final int width = d.getIntrinsicWidth();
503 final int height = d.getIntrinsicHeight();
504
505 int x = getLeftPadding() - (mWidthGap / 2) - (width / 2);
506 for (int col = 0; col <= countX; col++) {
507 int y = getTopPadding() - (mHeightGap / 2) - (height / 2);
508 for (int row = 0; row <= countY; row++) {
509 mTmpPointF.set(x - mDragCenter.x, y - mDragCenter.y);
510 float dist = mTmpPointF.length();
511 // Crosshairs further from the drag point are more faint
512 float alpha = Math.min(MAX_ALPHA,
513 DISTANCE_MULTIPLIER * (MAX_VISIBLE_DISTANCE - dist));
514 if (alpha > 0.0f) {
515 d.setBounds(x, y, x + width, y + height);
516 d.setAlpha((int) (alpha * 255 * mCrosshairsVisibility));
517 d.draw(canvas);
518 }
519 y += mCellHeight + mHeightGap;
520 }
521 x += mCellWidth + mWidthGap;
522 }
Joe Onorato4be866d2010-10-10 11:26:02 -0700523 }
Winson Chung150fbab2010-09-29 17:14:26 -0700524
Patrick Dubroy8e58e912010-10-14 13:21:48 -0700525 final Paint paint = mDragOutlinePaint;
Joe Onorato4be866d2010-10-10 11:26:02 -0700526 for (int i = 0; i < mDragOutlines.length; i++) {
Chet Haase472b2812010-10-14 07:02:04 -0700527 final float alpha = mDragOutlineAlphas[i];
Joe Onorato4be866d2010-10-10 11:26:02 -0700528 if (alpha > 0) {
529 final Point p = mDragOutlines[i];
530 final Bitmap b = (Bitmap) mDragOutlineAnims[i].getTag();
Chet Haase472b2812010-10-14 07:02:04 -0700531 paint.setAlpha((int)(alpha + .5f));
Joe Onorato4be866d2010-10-10 11:26:02 -0700532 canvas.drawBitmap(b, p.x, p.y, paint);
Winson Chung150fbab2010-09-29 17:14:26 -0700533 }
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700534 }
Patrick Dubroy96864c32011-03-10 17:17:23 -0800535
536 // We draw the pressed or focused BubbleTextView's background in CellLayout because it
537 // requires an expanded clip rect (due to the glow's blur radius)
538 if (mPressedOrFocusedIcon != null) {
539 final int padding = mPressedOrFocusedIcon.getPressedOrFocusedBackgroundPadding();
540 final Bitmap b = mPressedOrFocusedIcon.getPressedOrFocusedBackground();
541 if (b != null) {
542 canvas.drawBitmap(b,
Adam Cohen7f4eabe2011-04-21 16:19:16 -0700543 mPressedOrFocusedIcon.getLeft() + getLeftPadding() - padding,
544 mPressedOrFocusedIcon.getTop() + getTopPadding() - padding,
Patrick Dubroy96864c32011-03-10 17:17:23 -0800545 null);
546 }
547 }
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700548 }
549
550 @Override
Jeff Sharkey83f111d2009-04-20 21:03:13 -0700551 public void cancelLongPress() {
552 super.cancelLongPress();
553
554 // Cancel long press for all children
555 final int count = getChildCount();
556 for (int i = 0; i < count; i++) {
557 final View child = getChildAt(i);
558 child.cancelLongPress();
559 }
560 }
561
Michael Jurkadee05892010-07-27 10:01:56 -0700562 public void setOnInterceptTouchListener(View.OnTouchListener listener) {
563 mInterceptTouchListener = listener;
564 }
565
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800566 int getCountX() {
Adam Cohend22015c2010-07-26 22:02:18 -0700567 return mCountX;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800568 }
569
570 int getCountY() {
Adam Cohend22015c2010-07-26 22:02:18 -0700571 return mCountY;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800572 }
573
Michael Jurkaf3ca3ab2010-10-20 17:08:24 -0700574 public boolean addViewToCellLayout(
575 View child, int index, int childId, LayoutParams params, boolean markCells) {
Winson Chungaafa03c2010-06-11 17:34:16 -0700576 final LayoutParams lp = params;
577
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800578 // Generate an id for each view, this assumes we have at most 256x256 cells
579 // per workspace screen
Adam Cohend22015c2010-07-26 22:02:18 -0700580 if (lp.cellX >= 0 && lp.cellX <= mCountX - 1 && lp.cellY >= 0 && lp.cellY <= mCountY - 1) {
Winson Chungaafa03c2010-06-11 17:34:16 -0700581 // If the horizontal or vertical span is set to -1, it is taken to
582 // mean that it spans the extent of the CellLayout
Adam Cohend22015c2010-07-26 22:02:18 -0700583 if (lp.cellHSpan < 0) lp.cellHSpan = mCountX;
584 if (lp.cellVSpan < 0) lp.cellVSpan = mCountY;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800585
Winson Chungaafa03c2010-06-11 17:34:16 -0700586 child.setId(childId);
587
Michael Jurka8c920dd2011-01-20 14:16:56 -0800588 mChildren.addView(child, index, lp);
Michael Jurkadee05892010-07-27 10:01:56 -0700589
Michael Jurkaf3ca3ab2010-10-20 17:08:24 -0700590 if (markCells) markCellsAsOccupiedForView(child);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700591
Winson Chungaafa03c2010-06-11 17:34:16 -0700592 return true;
593 }
594 return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800595 }
Michael Jurka3e7c7632010-10-02 16:01:03 -0700596
Michael Jurkabea15192010-11-17 12:33:46 -0800597 public void setAcceptsDrops(boolean acceptsDrops) {
598 if (mAcceptsDrops != acceptsDrops) {
599 mAcceptsDrops = acceptsDrops;
600 invalidate();
601 }
602 }
603
Michael Jurka3e7c7632010-10-02 16:01:03 -0700604 public boolean getAcceptsDrops() {
605 return mAcceptsDrops;
606 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800607
608 @Override
Michael Jurka0280c3b2010-09-17 15:00:07 -0700609 public void removeAllViews() {
610 clearOccupiedCells();
Michael Jurka8c920dd2011-01-20 14:16:56 -0800611 mChildren.removeAllViews();
Michael Jurka0280c3b2010-09-17 15:00:07 -0700612 }
613
614 @Override
615 public void removeAllViewsInLayout() {
616 clearOccupiedCells();
Michael Jurka8c920dd2011-01-20 14:16:56 -0800617 mChildren.removeAllViewsInLayout();
Michael Jurka0280c3b2010-09-17 15:00:07 -0700618 }
619
Michael Jurkaf3ca3ab2010-10-20 17:08:24 -0700620 public void removeViewWithoutMarkingCells(View view) {
Michael Jurkacf6125c2011-01-28 15:20:01 -0800621 mChildren.removeView(view);
Michael Jurkaf3ca3ab2010-10-20 17:08:24 -0700622 }
623
Michael Jurka0280c3b2010-09-17 15:00:07 -0700624 @Override
625 public void removeView(View view) {
626 markCellsAsUnoccupiedForView(view);
Michael Jurka8c920dd2011-01-20 14:16:56 -0800627 mChildren.removeView(view);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700628 }
629
630 @Override
631 public void removeViewAt(int index) {
Michael Jurka8c920dd2011-01-20 14:16:56 -0800632 markCellsAsUnoccupiedForView(mChildren.getChildAt(index));
633 mChildren.removeViewAt(index);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700634 }
635
636 @Override
637 public void removeViewInLayout(View view) {
638 markCellsAsUnoccupiedForView(view);
Michael Jurka8c920dd2011-01-20 14:16:56 -0800639 mChildren.removeViewInLayout(view);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700640 }
641
642 @Override
643 public void removeViews(int start, int count) {
644 for (int i = start; i < start + count; i++) {
Michael Jurka8c920dd2011-01-20 14:16:56 -0800645 markCellsAsUnoccupiedForView(mChildren.getChildAt(i));
Michael Jurka0280c3b2010-09-17 15:00:07 -0700646 }
Michael Jurka8c920dd2011-01-20 14:16:56 -0800647 mChildren.removeViews(start, count);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700648 }
649
650 @Override
651 public void removeViewsInLayout(int start, int count) {
652 for (int i = start; i < start + count; i++) {
Michael Jurka8c920dd2011-01-20 14:16:56 -0800653 markCellsAsUnoccupiedForView(mChildren.getChildAt(i));
Michael Jurka0280c3b2010-09-17 15:00:07 -0700654 }
Michael Jurka8c920dd2011-01-20 14:16:56 -0800655 mChildren.removeViewsInLayout(start, count);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700656 }
657
Michael Jurka8c920dd2011-01-20 14:16:56 -0800658 public void drawChildren(Canvas canvas) {
659 mChildren.draw(canvas);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800660 }
661
Michael Jurkaabded662011-03-04 12:06:57 -0800662 void buildChildrenLayer() {
663 mChildren.buildLayer();
664 }
665
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800666 @Override
667 protected void onAttachedToWindow() {
668 super.onAttachedToWindow();
669 mCellInfo.screen = ((ViewGroup) getParent()).indexOfChild(this);
670 }
671
Michael Jurkaaf442092010-06-10 17:01:57 -0700672 public void setTagToCellInfoForPoint(int touchX, int touchY) {
673 final CellInfo cellInfo = mCellInfo;
674 final Rect frame = mRect;
675 final int x = touchX + mScrollX;
676 final int y = touchY + mScrollY;
Michael Jurka8c920dd2011-01-20 14:16:56 -0800677 final int count = mChildren.getChildCount();
Michael Jurkaaf442092010-06-10 17:01:57 -0700678
679 boolean found = false;
680 for (int i = count - 1; i >= 0; i--) {
Michael Jurka8c920dd2011-01-20 14:16:56 -0800681 final View child = mChildren.getChildAt(i);
Adam Cohend4844c32011-02-18 19:25:06 -0800682 final LayoutParams lp = (LayoutParams) child.getLayoutParams();
Michael Jurkaaf442092010-06-10 17:01:57 -0700683
Adam Cohen1b607ed2011-03-03 17:26:50 -0800684 if ((child.getVisibility() == VISIBLE || child.getAnimation() != null) &&
685 lp.isLockedToGrid) {
Michael Jurkaaf442092010-06-10 17:01:57 -0700686 child.getHitRect(frame);
687 if (frame.contains(x, y)) {
Michael Jurkaaf442092010-06-10 17:01:57 -0700688 cellInfo.cell = child;
689 cellInfo.cellX = lp.cellX;
690 cellInfo.cellY = lp.cellY;
691 cellInfo.spanX = lp.cellHSpan;
692 cellInfo.spanY = lp.cellVSpan;
693 cellInfo.valid = true;
694 found = true;
Michael Jurkaaf442092010-06-10 17:01:57 -0700695 break;
696 }
697 }
698 }
Winson Chungaafa03c2010-06-11 17:34:16 -0700699
Michael Jurkaaf442092010-06-10 17:01:57 -0700700 if (!found) {
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700701 final int cellXY[] = mTmpCellXY;
Michael Jurkaaf442092010-06-10 17:01:57 -0700702 pointToCellExact(x, y, cellXY);
703
Michael Jurkaaf442092010-06-10 17:01:57 -0700704 cellInfo.cell = null;
705 cellInfo.cellX = cellXY[0];
706 cellInfo.cellY = cellXY[1];
707 cellInfo.spanX = 1;
708 cellInfo.spanY = 1;
Michael Jurka0280c3b2010-09-17 15:00:07 -0700709 cellInfo.valid = cellXY[0] >= 0 && cellXY[1] >= 0 && cellXY[0] < mCountX &&
710 cellXY[1] < mCountY && !mOccupied[cellXY[0]][cellXY[1]];
Michael Jurkaaf442092010-06-10 17:01:57 -0700711 }
712 setTag(cellInfo);
713 }
714
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800715 @Override
716 public boolean onInterceptTouchEvent(MotionEvent ev) {
Michael Jurkadee05892010-07-27 10:01:56 -0700717 if (mInterceptTouchListener != null && mInterceptTouchListener.onTouch(this, ev)) {
718 return true;
719 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800720 final int action = ev.getAction();
721 final CellInfo cellInfo = mCellInfo;
722
723 if (action == MotionEvent.ACTION_DOWN) {
Michael Jurkaaf442092010-06-10 17:01:57 -0700724 setTagToCellInfoForPoint((int) ev.getX(), (int) ev.getY());
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800725 } else if (action == MotionEvent.ACTION_UP) {
726 cellInfo.cell = null;
727 cellInfo.cellX = -1;
728 cellInfo.cellY = -1;
729 cellInfo.spanX = 0;
730 cellInfo.spanY = 0;
731 cellInfo.valid = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800732 setTag(cellInfo);
733 }
734
735 return false;
736 }
737
738 @Override
739 public CellInfo getTag() {
Michael Jurka0280c3b2010-09-17 15:00:07 -0700740 return (CellInfo) super.getTag();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800741 }
742
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700743 /**
Winson Chungaafa03c2010-06-11 17:34:16 -0700744 * Given a point, return the cell that strictly encloses that point
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800745 * @param x X coordinate of the point
746 * @param y Y coordinate of the point
747 * @param result Array of 2 ints to hold the x and y coordinate of the cell
748 */
749 void pointToCellExact(int x, int y, int[] result) {
Winson Chungaafa03c2010-06-11 17:34:16 -0700750 final int hStartPadding = getLeftPadding();
751 final int vStartPadding = getTopPadding();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800752
753 result[0] = (x - hStartPadding) / (mCellWidth + mWidthGap);
754 result[1] = (y - vStartPadding) / (mCellHeight + mHeightGap);
755
Adam Cohend22015c2010-07-26 22:02:18 -0700756 final int xAxis = mCountX;
757 final int yAxis = mCountY;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800758
759 if (result[0] < 0) result[0] = 0;
760 if (result[0] >= xAxis) result[0] = xAxis - 1;
761 if (result[1] < 0) result[1] = 0;
762 if (result[1] >= yAxis) result[1] = yAxis - 1;
763 }
Winson Chungaafa03c2010-06-11 17:34:16 -0700764
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800765 /**
766 * Given a point, return the cell that most closely encloses that point
767 * @param x X coordinate of the point
768 * @param y Y coordinate of the point
769 * @param result Array of 2 ints to hold the x and y coordinate of the cell
770 */
771 void pointToCellRounded(int x, int y, int[] result) {
772 pointToCellExact(x + (mCellWidth / 2), y + (mCellHeight / 2), result);
773 }
774
775 /**
776 * Given a cell coordinate, return the point that represents the upper left corner of that cell
Winson Chungaafa03c2010-06-11 17:34:16 -0700777 *
778 * @param cellX X coordinate of the cell
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800779 * @param cellY Y coordinate of the cell
Winson Chungaafa03c2010-06-11 17:34:16 -0700780 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800781 * @param result Array of 2 ints to hold the x and y coordinate of the point
782 */
783 void cellToPoint(int cellX, int cellY, int[] result) {
Winson Chungaafa03c2010-06-11 17:34:16 -0700784 final int hStartPadding = getLeftPadding();
785 final int vStartPadding = getTopPadding();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800786
787 result[0] = hStartPadding + cellX * (mCellWidth + mWidthGap);
788 result[1] = vStartPadding + cellY * (mCellHeight + mHeightGap);
789 }
790
Adam Cohene3e27a82011-04-15 12:07:39 -0700791 /**
792 * Given a cell coordinate, return the point that represents the upper left corner of that cell
793 *
794 * @param cellX X coordinate of the cell
795 * @param cellY Y coordinate of the cell
796 *
797 * @param result Array of 2 ints to hold the x and y coordinate of the point
798 */
799 void cellToCenterPoint(int cellX, int cellY, int[] result) {
800 final int hStartPadding = getLeftPadding();
801 final int vStartPadding = getTopPadding();
802
803 result[0] = hStartPadding + cellX * (mCellWidth + mWidthGap) + mCellWidth / 2;
804 result[1] = vStartPadding + cellY * (mCellHeight + mHeightGap) + mCellHeight / 2;
805 }
806
Romain Guy84f296c2009-11-04 15:00:44 -0800807 int getCellWidth() {
808 return mCellWidth;
809 }
810
811 int getCellHeight() {
812 return mCellHeight;
813 }
814
Adam Cohend4844c32011-02-18 19:25:06 -0800815 int getWidthGap() {
816 return mWidthGap;
817 }
818
819 int getHeightGap() {
820 return mHeightGap;
821 }
822
Romain Guy1a304a12009-11-10 00:02:32 -0800823 int getLeftPadding() {
Winson Chungaafa03c2010-06-11 17:34:16 -0700824 return mLeftPadding;
Romain Guy1a304a12009-11-10 00:02:32 -0800825 }
826
827 int getTopPadding() {
Winson Chungaafa03c2010-06-11 17:34:16 -0700828 return mTopPadding;
Romain Guy1a304a12009-11-10 00:02:32 -0800829 }
830
831 int getRightPadding() {
Winson Chungaafa03c2010-06-11 17:34:16 -0700832 return mRightPadding;
Romain Guy1a304a12009-11-10 00:02:32 -0800833 }
834
835 int getBottomPadding() {
Winson Chungaafa03c2010-06-11 17:34:16 -0700836 return mBottomPadding;
Romain Guy1a304a12009-11-10 00:02:32 -0800837 }
838
Adam Cohen7f4eabe2011-04-21 16:19:16 -0700839 Rect getContentRect(Rect r) {
840 if (r == null) {
841 r = new Rect();
842 }
843 int left = getPaddingLeft();
844 int top = getPaddingTop();
845 int right = left + getWidth() - mLeftPadding - mRightPadding;
846 int bottom = top + getHeight() - mTopPadding - mBottomPadding;
847 r.set(left, top, right, bottom);
848 return r;
849 }
850
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800851 @Override
852 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
853 // TODO: currently ignoring padding
Winson Chungaafa03c2010-06-11 17:34:16 -0700854
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800855 int widthSpecMode = MeasureSpec.getMode(widthMeasureSpec);
Winson Chungaafa03c2010-06-11 17:34:16 -0700856 int widthSpecSize = MeasureSpec.getSize(widthMeasureSpec);
857
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800858 int heightSpecMode = MeasureSpec.getMode(heightMeasureSpec);
859 int heightSpecSize = MeasureSpec.getSize(heightMeasureSpec);
Winson Chungaafa03c2010-06-11 17:34:16 -0700860
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800861 if (widthSpecMode == MeasureSpec.UNSPECIFIED || heightSpecMode == MeasureSpec.UNSPECIFIED) {
862 throw new RuntimeException("CellLayout cannot have UNSPECIFIED dimensions");
863 }
864
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800865 final int cellWidth = mCellWidth;
866 final int cellHeight = mCellHeight;
867
Adam Cohend22015c2010-07-26 22:02:18 -0700868 int numWidthGaps = mCountX - 1;
869 int numHeightGaps = mCountY - 1;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800870
Winson Chungece7f5b2010-10-22 14:54:12 -0700871 if (mWidthGap < 0 || mHeightGap < 0) {
872 int vSpaceLeft = heightSpecSize - mTopPadding - mBottomPadding - (cellHeight * mCountY);
873 mHeightGap = vSpaceLeft / numHeightGaps;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800874
Winson Chungece7f5b2010-10-22 14:54:12 -0700875 int hSpaceLeft = widthSpecSize - mLeftPadding - mRightPadding - (cellWidth * mCountX);
876 mWidthGap = hSpaceLeft / numWidthGaps;
Winson Chungaafa03c2010-06-11 17:34:16 -0700877
Winson Chungece7f5b2010-10-22 14:54:12 -0700878 // center it around the min gaps
879 int minGap = Math.min(mWidthGap, mHeightGap);
880 mWidthGap = mHeightGap = minGap;
881 }
Michael Jurka5f1c5092010-09-03 14:15:02 -0700882
Michael Jurka8c920dd2011-01-20 14:16:56 -0800883 // Initial values correspond to widthSpecMode == MeasureSpec.EXACTLY
884 int newWidth = widthSpecSize;
885 int newHeight = heightSpecSize;
Michael Jurka5f1c5092010-09-03 14:15:02 -0700886 if (widthSpecMode == MeasureSpec.AT_MOST) {
Michael Jurka8c920dd2011-01-20 14:16:56 -0800887 newWidth = mLeftPadding + mRightPadding + (mCountX * cellWidth) +
Winson Chungece7f5b2010-10-22 14:54:12 -0700888 ((mCountX - 1) * mWidthGap);
Michael Jurka8c920dd2011-01-20 14:16:56 -0800889 newHeight = mTopPadding + mBottomPadding + (mCountY * cellHeight) +
Winson Chungece7f5b2010-10-22 14:54:12 -0700890 ((mCountY - 1) * mHeightGap);
Michael Jurka5f1c5092010-09-03 14:15:02 -0700891 setMeasuredDimension(newWidth, newHeight);
Michael Jurka5f1c5092010-09-03 14:15:02 -0700892 }
Michael Jurka8c920dd2011-01-20 14:16:56 -0800893
894 int count = getChildCount();
895 for (int i = 0; i < count; i++) {
896 View child = getChildAt(i);
897 int childWidthMeasureSpec = MeasureSpec.makeMeasureSpec(newWidth, MeasureSpec.EXACTLY);
898 int childheightMeasureSpec = MeasureSpec.makeMeasureSpec(newHeight,
899 MeasureSpec.EXACTLY);
900 child.measure(childWidthMeasureSpec, childheightMeasureSpec);
901 }
902 setMeasuredDimension(newWidth, newHeight);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800903 }
904
905 @Override
Michael Jurka28750fb2010-09-24 17:43:49 -0700906 protected void onLayout(boolean changed, int l, int t, int r, int b) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800907 int count = getChildCount();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800908 for (int i = 0; i < count; i++) {
Michael Jurka8c920dd2011-01-20 14:16:56 -0800909 View child = getChildAt(i);
Adam Cohenbb60e2e2011-04-26 16:48:26 -0700910 child.layout(mLeftPadding, mTopPadding, r - l - mRightPadding , b - t - mBottomPadding);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800911 }
912 }
913
914 @Override
Michael Jurkadee05892010-07-27 10:01:56 -0700915 protected void onSizeChanged(int w, int h, int oldw, int oldh) {
916 super.onSizeChanged(w, h, oldw, oldh);
Michael Jurka18014792010-10-14 09:01:34 -0700917 mBackgroundRect.set(0, 0, w, h);
Michael Jurka33945b22010-12-21 18:19:38 -0800918 updateGlowRect();
Michael Jurkadee05892010-07-27 10:01:56 -0700919 }
920
921 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800922 protected void setChildrenDrawingCacheEnabled(boolean enabled) {
Michael Jurka8c920dd2011-01-20 14:16:56 -0800923 mChildren.setChildrenDrawingCacheEnabled(enabled);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800924 }
925
926 @Override
927 protected void setChildrenDrawnWithCacheEnabled(boolean enabled) {
Michael Jurka8c920dd2011-01-20 14:16:56 -0800928 mChildren.setChildrenDrawnWithCacheEnabled(enabled);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800929 }
930
Michael Jurka5f1c5092010-09-03 14:15:02 -0700931 public float getBackgroundAlpha() {
932 return mBackgroundAlpha;
Michael Jurkadee05892010-07-27 10:01:56 -0700933 }
934
Michael Jurka742574b2011-02-02 23:51:01 -0800935 public void setFastBackgroundAlpha(float alpha) {
936 mBackgroundAlpha = alpha;
937 }
938
Adam Cohen1b0aaac2010-10-28 11:11:18 -0700939 public void setBackgroundAlphaMultiplier(float multiplier) {
940 mBackgroundAlphaMultiplier = multiplier;
941 }
942
Adam Cohenddb82192010-11-10 16:32:54 -0800943 public float getBackgroundAlphaMultiplier() {
944 return mBackgroundAlphaMultiplier;
945 }
946
Michael Jurka5f1c5092010-09-03 14:15:02 -0700947 public void setBackgroundAlpha(float alpha) {
948 mBackgroundAlpha = alpha;
Michael Jurka0142d492010-08-25 17:46:15 -0700949 invalidate();
Michael Jurkadee05892010-07-27 10:01:56 -0700950 }
951
Michael Jurka5f1c5092010-09-03 14:15:02 -0700952 // Need to return true to let the view system know we know how to handle alpha-- this is
953 // because when our children have an alpha of 0.0f, they are still rendering their "dimmed"
954 // versions
955 @Override
956 protected boolean onSetAlpha(int alpha) {
957 return true;
958 }
959
960 public void setAlpha(float alpha) {
961 setChildrenAlpha(alpha);
962 super.setAlpha(alpha);
963 }
964
Michael Jurka742574b2011-02-02 23:51:01 -0800965 public void setFastAlpha(float alpha) {
966 setFastChildrenAlpha(alpha);
967 super.setFastAlpha(alpha);
968 }
969
Michael Jurkadee05892010-07-27 10:01:56 -0700970 private void setChildrenAlpha(float alpha) {
Michael Jurka0142d492010-08-25 17:46:15 -0700971 final int childCount = getChildCount();
972 for (int i = 0; i < childCount; i++) {
Michael Jurkadee05892010-07-27 10:01:56 -0700973 getChildAt(i).setAlpha(alpha);
974 }
975 }
976
Michael Jurka742574b2011-02-02 23:51:01 -0800977 private void setFastChildrenAlpha(float alpha) {
978 final int childCount = getChildCount();
979 for (int i = 0; i < childCount; i++) {
980 getChildAt(i).setFastAlpha(alpha);
981 }
982 }
983
Patrick Dubroy440c3602010-07-13 17:50:32 -0700984 public View getChildAt(int x, int y) {
Michael Jurka8c920dd2011-01-20 14:16:56 -0800985 return mChildren.getChildAt(x, y);
Patrick Dubroy440c3602010-07-13 17:50:32 -0700986 }
987
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700988 /**
989 * Estimate where the top left cell of the dragged item will land if it is dropped.
990 *
991 * @param originX The X value of the top left corner of the item
992 * @param originY The Y value of the top left corner of the item
993 * @param spanX The number of horizontal cells that the item spans
994 * @param spanY The number of vertical cells that the item spans
995 * @param result The estimated drop cell X and Y.
996 */
997 void estimateDropCell(int originX, int originY, int spanX, int spanY, int[] result) {
Adam Cohend22015c2010-07-26 22:02:18 -0700998 final int countX = mCountX;
999 final int countY = mCountY;
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001000
Michael Jurkaa63c4522010-08-19 13:52:27 -07001001 // pointToCellRounded takes the top left of a cell but will pad that with
1002 // cellWidth/2 and cellHeight/2 when finding the matching cell
1003 pointToCellRounded(originX, originY, result);
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001004
1005 // If the item isn't fully on this screen, snap to the edges
1006 int rightOverhang = result[0] + spanX - countX;
1007 if (rightOverhang > 0) {
1008 result[0] -= rightOverhang; // Snap to right
1009 }
1010 result[0] = Math.max(0, result[0]); // Snap to left
1011 int bottomOverhang = result[1] + spanY - countY;
1012 if (bottomOverhang > 0) {
1013 result[1] -= bottomOverhang; // Snap to bottom
1014 }
1015 result[1] = Math.max(0, result[1]); // Snap to top
1016 }
1017
Joe Onorato4be866d2010-10-10 11:26:02 -07001018 void visualizeDropLocation(
1019 View v, Bitmap dragOutline, int originX, int originY, int spanX, int spanY) {
1020
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -07001021 final int oldDragCellX = mDragCell[0];
1022 final int oldDragCellY = mDragCell[1];
Joe Onorato4be866d2010-10-10 11:26:02 -07001023 final int[] nearest = findNearestVacantArea(originX, originY, spanX, spanY, v, mDragCell);
Winson Chunga9abd0e2010-10-27 17:18:37 -07001024 if (v != null) {
1025 mDragCenter.set(originX + (v.getWidth() / 2), originY + (v.getHeight() / 2));
1026 } else {
1027 mDragCenter.set(originX, originY);
1028 }
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001029
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -07001030 if (nearest != null && (nearest[0] != oldDragCellX || nearest[1] != oldDragCellY)) {
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001031 // Find the top left corner of the rect the object will occupy
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001032 final int[] topLeft = mTmpPoint;
1033 cellToPoint(nearest[0], nearest[1], topLeft);
1034
Joe Onorato4be866d2010-10-10 11:26:02 -07001035 int left = topLeft[0];
1036 int top = topLeft[1];
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001037
Winson Chunga9abd0e2010-10-27 17:18:37 -07001038 if (v != null) {
Adam Cohen99e8b402011-03-25 19:23:43 -07001039 // When drawing the drag outline, it did not account for margin offsets
1040 // added by the view's parent.
1041 MarginLayoutParams lp = (MarginLayoutParams) v.getLayoutParams();
1042 left += lp.leftMargin;
1043 top += lp.topMargin;
Winson Chung150fbab2010-09-29 17:14:26 -07001044
Adam Cohen99e8b402011-03-25 19:23:43 -07001045 // Offsets due to the size difference between the View and the dragOutline.
1046 // There is a size difference to account for the outer blur, which may lie
1047 // outside the bounds of the view.
Winson Chunga9abd0e2010-10-27 17:18:37 -07001048 left += (v.getWidth() - dragOutline.getWidth()) / 2;
1049 top += (v.getHeight() - dragOutline.getHeight()) / 2;
Adam Cohen66396872011-04-15 17:50:36 -07001050 } else {
1051 // Center the drag outline in the cell
Winson Chung4b576be2011-04-27 17:40:20 -07001052 left += ((mCellWidth * spanX) + ((spanX - 1) * mWidthGap)
1053 - dragOutline.getWidth()) / 2;
1054 top += ((mCellHeight * spanY) + ((spanY - 1) * mHeightGap)
1055 - dragOutline.getHeight()) / 2;
Winson Chunga9abd0e2010-10-27 17:18:37 -07001056 }
Winson Chung150fbab2010-09-29 17:14:26 -07001057
Joe Onorato4be866d2010-10-10 11:26:02 -07001058 final int oldIndex = mDragOutlineCurrent;
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -07001059 mDragOutlineAnims[oldIndex].animateOut();
1060 mDragOutlineCurrent = (oldIndex + 1) % mDragOutlines.length;
Winson Chung150fbab2010-09-29 17:14:26 -07001061
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -07001062 mDragOutlines[mDragOutlineCurrent].set(left, top);
1063 mDragOutlineAnims[mDragOutlineCurrent].setTag(dragOutline);
1064 mDragOutlineAnims[mDragOutlineCurrent].animateIn();
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001065 }
Patrick Dubroy49250ad2010-10-08 15:33:52 -07001066
1067 // If we are drawing crosshairs, the entire CellLayout needs to be invalidated
1068 if (mCrosshairsDrawable != null) {
1069 invalidate();
1070 }
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001071 }
1072
Adam Cohene0310962011-04-18 16:15:31 -07001073 public void clearDragOutlines() {
1074 final int oldIndex = mDragOutlineCurrent;
1075 mDragOutlineAnims[oldIndex].animateOut();
1076 mDragCell[0] = -1;
1077 mDragCell[1] = -1;
1078 }
1079
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001080 /**
Jeff Sharkey70864282009-04-07 21:08:40 -07001081 * Find a vacant area that will fit the given bounds nearest the requested
1082 * cell location. Uses Euclidean distance to score multiple vacant areas.
Winson Chungaafa03c2010-06-11 17:34:16 -07001083 *
Romain Guy51afc022009-05-04 18:03:43 -07001084 * @param pixelX The X location at which you want to search for a vacant area.
1085 * @param pixelY The Y location at which you want to search for a vacant area.
Jeff Sharkey70864282009-04-07 21:08:40 -07001086 * @param spanX Horizontal span of the object.
1087 * @param spanY Vertical span of the object.
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001088 * @param result Array in which to place the result, or null (in which case a new array will
1089 * be allocated)
Jeff Sharkey70864282009-04-07 21:08:40 -07001090 * @return The X, Y cell of a vacant area that can contain this object,
1091 * nearest the requested location.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001092 */
Michael Jurka6a1435d2010-09-27 17:35:12 -07001093 int[] findNearestVacantArea(
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001094 int pixelX, int pixelY, int spanX, int spanY, int[] result) {
1095 return findNearestVacantArea(pixelX, pixelY, spanX, spanY, null, result);
Michael Jurka6a1435d2010-09-27 17:35:12 -07001096 }
Winson Chungaafa03c2010-06-11 17:34:16 -07001097
Michael Jurka6a1435d2010-09-27 17:35:12 -07001098 /**
1099 * Find a vacant area that will fit the given bounds nearest the requested
1100 * cell location. Uses Euclidean distance to score multiple vacant areas.
1101 *
1102 * @param pixelX The X location at which you want to search for a vacant area.
1103 * @param pixelY The Y location at which you want to search for a vacant area.
1104 * @param spanX Horizontal span of the object.
1105 * @param spanY Vertical span of the object.
Adam Cohendf035382011-04-11 17:22:04 -07001106 * @param ignoreOccupied If true, the result can be an occupied cell
1107 * @param result Array in which to place the result, or null (in which case a new array will
1108 * be allocated)
Michael Jurka6a1435d2010-09-27 17:35:12 -07001109 * @return The X, Y cell of a vacant area that can contain this object,
1110 * nearest the requested location.
1111 */
Adam Cohendf035382011-04-11 17:22:04 -07001112 int[] findNearestArea(int pixelX, int pixelY, int spanX, int spanY, View ignoreView,
1113 boolean ignoreOccupied, int[] result) {
Michael Jurkac6ee42e2010-09-30 12:04:50 -07001114 // mark space take by ignoreView as available (method checks if ignoreView is null)
1115 markCellsAsUnoccupiedForView(ignoreView);
1116
Adam Cohene3e27a82011-04-15 12:07:39 -07001117 // For items with a spanX / spanY > 1, the passed in point (pixelX, pixelY) corresponds
1118 // to the center of the item, but we are searching based on the top-left cell, so
1119 // we translate the point over to correspond to the top-left.
1120 pixelX -= (mCellWidth + mWidthGap) * (spanX - 1) / 2f;
1121 pixelY -= (mCellHeight + mHeightGap) * (spanY - 1) / 2f;
1122
Jeff Sharkey70864282009-04-07 21:08:40 -07001123 // Keep track of best-scoring drop area
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001124 final int[] bestXY = result != null ? result : new int[2];
Jeff Sharkey70864282009-04-07 21:08:40 -07001125 double bestDistance = Double.MAX_VALUE;
Winson Chungaafa03c2010-06-11 17:34:16 -07001126
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001127 final int countX = mCountX;
1128 final int countY = mCountY;
1129 final boolean[][] occupied = mOccupied;
1130
Winson Chungbbc60d82010-11-11 16:34:41 -08001131 for (int y = 0; y < countY - (spanY - 1); y++) {
Michael Jurkac28de512010-08-13 11:27:44 -07001132 inner:
Winson Chungbbc60d82010-11-11 16:34:41 -08001133 for (int x = 0; x < countX - (spanX - 1); x++) {
Adam Cohendf035382011-04-11 17:22:04 -07001134 if (ignoreOccupied) {
1135 for (int i = 0; i < spanX; i++) {
1136 for (int j = 0; j < spanY; j++) {
1137 if (occupied[x + i][y + j]) {
1138 // small optimization: we can skip to after the column we
1139 // just found an occupied cell
1140 x += i;
1141 continue inner;
1142 }
Michael Jurkac28de512010-08-13 11:27:44 -07001143 }
1144 }
1145 }
1146 final int[] cellXY = mTmpCellXY;
Adam Cohene3e27a82011-04-15 12:07:39 -07001147 cellToCenterPoint(x, y, cellXY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001148
Michael Jurkac28de512010-08-13 11:27:44 -07001149 double distance = Math.sqrt(Math.pow(cellXY[0] - pixelX, 2)
1150 + Math.pow(cellXY[1] - pixelY, 2));
1151 if (distance <= bestDistance) {
1152 bestDistance = distance;
1153 bestXY[0] = x;
1154 bestXY[1] = y;
1155 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001156 }
1157 }
Michael Jurkac6ee42e2010-09-30 12:04:50 -07001158 // re-mark space taken by ignoreView as occupied
1159 markCellsAsOccupiedForView(ignoreView);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001160
Winson Chungaafa03c2010-06-11 17:34:16 -07001161 // Return null if no suitable location found
Jeff Sharkey70864282009-04-07 21:08:40 -07001162 if (bestDistance < Double.MAX_VALUE) {
1163 return bestXY;
1164 } else {
1165 return null;
1166 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001167 }
Winson Chungaafa03c2010-06-11 17:34:16 -07001168
Adam Cohendf035382011-04-11 17:22:04 -07001169 /**
1170 * Find a vacant area that will fit the given bounds nearest the requested
1171 * cell location. Uses Euclidean distance to score multiple vacant areas.
1172 *
1173 * @param pixelX The X location at which you want to search for a vacant area.
1174 * @param pixelY The Y location at which you want to search for a vacant area.
1175 * @param spanX Horizontal span of the object.
1176 * @param spanY Vertical span of the object.
1177 * @param ignoreView Considers space occupied by this view as unoccupied
1178 * @param result Previously returned value to possibly recycle.
1179 * @return The X, Y cell of a vacant area that can contain this object,
1180 * nearest the requested location.
1181 */
1182 int[] findNearestVacantArea(
1183 int pixelX, int pixelY, int spanX, int spanY, View ignoreView, int[] result) {
1184 return findNearestArea(pixelX, pixelY, spanX, spanY, ignoreView, true, result);
1185 }
1186
1187 /**
1188 * Find a starting cell position that will fit the given bounds nearest the requested
1189 * cell location. Uses Euclidean distance to score multiple vacant areas.
1190 *
1191 * @param pixelX The X location at which you want to search for a vacant area.
1192 * @param pixelY The Y location at which you want to search for a vacant area.
1193 * @param spanX Horizontal span of the object.
1194 * @param spanY Vertical span of the object.
1195 * @param ignoreView Considers space occupied by this view as unoccupied
1196 * @param result Previously returned value to possibly recycle.
1197 * @return The X, Y cell of a vacant area that can contain this object,
1198 * nearest the requested location.
1199 */
1200 int[] findNearestArea(
1201 int pixelX, int pixelY, int spanX, int spanY, int[] result) {
1202 return findNearestArea(pixelX, pixelY, spanX, spanY, null, false, result);
1203 }
1204
Michael Jurka0280c3b2010-09-17 15:00:07 -07001205 boolean existsEmptyCell() {
1206 return findCellForSpan(null, 1, 1);
1207 }
1208
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001209 /**
Michael Jurka0280c3b2010-09-17 15:00:07 -07001210 * Finds the upper-left coordinate of the first rectangle in the grid that can
1211 * hold a cell of the specified dimensions. If intersectX and intersectY are not -1,
1212 * then this method will only return coordinates for rectangles that contain the cell
1213 * (intersectX, intersectY)
1214 *
1215 * @param cellXY The array that will contain the position of a vacant cell if such a cell
1216 * can be found.
1217 * @param spanX The horizontal span of the cell we want to find.
1218 * @param spanY The vertical span of the cell we want to find.
1219 *
1220 * @return True if a vacant cell of the specified dimension was found, false otherwise.
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001221 */
Michael Jurka0280c3b2010-09-17 15:00:07 -07001222 boolean findCellForSpan(int[] cellXY, int spanX, int spanY) {
1223 return findCellForSpanThatIntersectsIgnoring(cellXY, spanX, spanY, -1, -1, null);
1224 }
1225
1226 /**
1227 * Like above, but ignores any cells occupied by the item "ignoreView"
1228 *
1229 * @param cellXY The array that will contain the position of a vacant cell if such a cell
1230 * can be found.
1231 * @param spanX The horizontal span of the cell we want to find.
1232 * @param spanY The vertical span of the cell we want to find.
1233 * @param ignoreView The home screen item we should treat as not occupying any space
1234 * @return
1235 */
1236 boolean findCellForSpanIgnoring(int[] cellXY, int spanX, int spanY, View ignoreView) {
1237 return findCellForSpanThatIntersectsIgnoring(cellXY, spanX, spanY, -1, -1, ignoreView);
1238 }
1239
1240 /**
1241 * Like above, but if intersectX and intersectY are not -1, then this method will try to
1242 * return coordinates for rectangles that contain the cell [intersectX, intersectY]
1243 *
1244 * @param spanX The horizontal span of the cell we want to find.
1245 * @param spanY The vertical span of the cell we want to find.
1246 * @param ignoreView The home screen item we should treat as not occupying any space
1247 * @param intersectX The X coordinate of the cell that we should try to overlap
1248 * @param intersectX The Y coordinate of the cell that we should try to overlap
1249 *
1250 * @return True if a vacant cell of the specified dimension was found, false otherwise.
1251 */
1252 boolean findCellForSpanThatIntersects(int[] cellXY, int spanX, int spanY,
1253 int intersectX, int intersectY) {
1254 return findCellForSpanThatIntersectsIgnoring(
1255 cellXY, spanX, spanY, intersectX, intersectY, null);
1256 }
1257
1258 /**
1259 * The superset of the above two methods
1260 */
1261 boolean findCellForSpanThatIntersectsIgnoring(int[] cellXY, int spanX, int spanY,
1262 int intersectX, int intersectY, View ignoreView) {
Michael Jurkac6ee42e2010-09-30 12:04:50 -07001263 // mark space take by ignoreView as available (method checks if ignoreView is null)
1264 markCellsAsUnoccupiedForView(ignoreView);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001265
Michael Jurka28750fb2010-09-24 17:43:49 -07001266 boolean foundCell = false;
Michael Jurka0280c3b2010-09-17 15:00:07 -07001267 while (true) {
1268 int startX = 0;
1269 if (intersectX >= 0) {
1270 startX = Math.max(startX, intersectX - (spanX - 1));
1271 }
1272 int endX = mCountX - (spanX - 1);
1273 if (intersectX >= 0) {
1274 endX = Math.min(endX, intersectX + (spanX - 1) + (spanX == 1 ? 1 : 0));
1275 }
1276 int startY = 0;
1277 if (intersectY >= 0) {
1278 startY = Math.max(startY, intersectY - (spanY - 1));
1279 }
1280 int endY = mCountY - (spanY - 1);
1281 if (intersectY >= 0) {
1282 endY = Math.min(endY, intersectY + (spanY - 1) + (spanY == 1 ? 1 : 0));
1283 }
1284
Winson Chungbbc60d82010-11-11 16:34:41 -08001285 for (int y = startY; y < endY && !foundCell; y++) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001286 inner:
Winson Chungbbc60d82010-11-11 16:34:41 -08001287 for (int x = startX; x < endX; x++) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001288 for (int i = 0; i < spanX; i++) {
1289 for (int j = 0; j < spanY; j++) {
1290 if (mOccupied[x + i][y + j]) {
Winson Chungbbc60d82010-11-11 16:34:41 -08001291 // small optimization: we can skip to after the column we just found
Michael Jurka0280c3b2010-09-17 15:00:07 -07001292 // an occupied cell
Winson Chungbbc60d82010-11-11 16:34:41 -08001293 x += i;
Michael Jurka0280c3b2010-09-17 15:00:07 -07001294 continue inner;
1295 }
1296 }
1297 }
1298 if (cellXY != null) {
1299 cellXY[0] = x;
1300 cellXY[1] = y;
1301 }
Michael Jurka28750fb2010-09-24 17:43:49 -07001302 foundCell = true;
1303 break;
Michael Jurka0280c3b2010-09-17 15:00:07 -07001304 }
1305 }
1306 if (intersectX == -1 && intersectY == -1) {
1307 break;
1308 } else {
1309 // if we failed to find anything, try again but without any requirements of
1310 // intersecting
1311 intersectX = -1;
1312 intersectY = -1;
1313 continue;
1314 }
1315 }
1316
Michael Jurkac6ee42e2010-09-30 12:04:50 -07001317 // re-mark space taken by ignoreView as occupied
1318 markCellsAsOccupiedForView(ignoreView);
Michael Jurka28750fb2010-09-24 17:43:49 -07001319 return foundCell;
Michael Jurka0280c3b2010-09-17 15:00:07 -07001320 }
1321
1322 /**
1323 * Called when drag has left this CellLayout or has been completed (successfully or not)
1324 */
1325 void onDragExit() {
Joe Onorato4be866d2010-10-10 11:26:02 -07001326 // This can actually be called when we aren't in a drag, e.g. when adding a new
1327 // item to this layout via the customize drawer.
1328 // Guard against that case.
1329 if (mDragging) {
1330 mDragging = false;
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001331
Joe Onorato4be866d2010-10-10 11:26:02 -07001332 // Fade out the drag indicators
1333 if (mCrosshairsAnimator != null) {
1334 mCrosshairsAnimator.animateOut();
1335 }
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001336 }
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -07001337
1338 // Invalidate the drag data
1339 mDragCell[0] = -1;
1340 mDragCell[1] = -1;
1341 mDragOutlineAnims[mDragOutlineCurrent].animateOut();
1342 mDragOutlineCurrent = (mDragOutlineCurrent + 1) % mDragOutlineAnims.length;
1343
Michael Jurka33945b22010-12-21 18:19:38 -08001344 setIsDragOverlapping(false);
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001345 }
1346
1347 /**
Winson Chungaafa03c2010-06-11 17:34:16 -07001348 * Mark a child as having been dropped.
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001349 * At the beginning of the drag operation, the child may have been on another
Patrick Dubroyce34a972010-10-19 10:34:32 -07001350 * screen, but it is re-parented before this method is called.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001351 *
1352 * @param child The child that is being dropped
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001353 */
Michael Jurkad3ef3062010-11-23 16:23:58 -08001354 void onDropChild(View child, boolean animate) {
Romain Guyd94533d2009-08-17 10:01:15 -07001355 if (child != null) {
1356 LayoutParams lp = (LayoutParams) child.getLayoutParams();
Romain Guyd94533d2009-08-17 10:01:15 -07001357 lp.isDragging = false;
Romain Guy84f296c2009-11-04 15:00:44 -08001358 lp.dropped = true;
Michael Jurkad3ef3062010-11-23 16:23:58 -08001359 lp.animateDrop = animate;
Patrick Dubroye3887cc2011-01-20 10:43:40 -08001360 child.setVisibility(animate ? View.INVISIBLE : View.VISIBLE);
Romain Guyd94533d2009-08-17 10:01:15 -07001361 child.requestLayout();
Romain Guyd94533d2009-08-17 10:01:15 -07001362 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001363 }
1364
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001365 /**
1366 * Start dragging the specified child
Winson Chungaafa03c2010-06-11 17:34:16 -07001367 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001368 * @param child The child that is being dragged
1369 */
1370 void onDragChild(View child) {
1371 LayoutParams lp = (LayoutParams) child.getLayoutParams();
1372 lp.isDragging = true;
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001373 }
1374
1375 /**
1376 * A drag event has begun over this layout.
1377 * It may have begun over this layout (in which case onDragChild is called first),
1378 * or it may have begun on another layout.
1379 */
Winson Chunga9abd0e2010-10-27 17:18:37 -07001380 void onDragEnter() {
Patrick Dubroyfe6bd872010-10-13 17:32:10 -07001381 if (!mDragging) {
Patrick Dubroyfe6bd872010-10-13 17:32:10 -07001382 // Fade in the drag indicators
1383 if (mCrosshairsAnimator != null) {
1384 mCrosshairsAnimator.animateIn();
1385 }
Joe Onorato4be866d2010-10-10 11:26:02 -07001386 }
1387 mDragging = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001388 }
Winson Chungaafa03c2010-06-11 17:34:16 -07001389
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001390 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001391 * Computes a bounding rectangle for a range of cells
Winson Chungaafa03c2010-06-11 17:34:16 -07001392 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001393 * @param cellX X coordinate of upper left corner expressed as a cell position
1394 * @param cellY Y coordinate of upper left corner expressed as a cell position
Winson Chungaafa03c2010-06-11 17:34:16 -07001395 * @param cellHSpan Width in cells
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001396 * @param cellVSpan Height in cells
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001397 * @param resultRect Rect into which to put the results
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001398 */
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001399 public void cellToRect(int cellX, int cellY, int cellHSpan, int cellVSpan, RectF resultRect) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001400 final int cellWidth = mCellWidth;
1401 final int cellHeight = mCellHeight;
1402 final int widthGap = mWidthGap;
1403 final int heightGap = mHeightGap;
Winson Chungaafa03c2010-06-11 17:34:16 -07001404
1405 final int hStartPadding = getLeftPadding();
1406 final int vStartPadding = getTopPadding();
1407
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001408 int width = cellHSpan * cellWidth + ((cellHSpan - 1) * widthGap);
1409 int height = cellVSpan * cellHeight + ((cellVSpan - 1) * heightGap);
1410
1411 int x = hStartPadding + cellX * (cellWidth + widthGap);
1412 int y = vStartPadding + cellY * (cellHeight + heightGap);
Winson Chungaafa03c2010-06-11 17:34:16 -07001413
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001414 resultRect.set(x, y, x + width, y + height);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001415 }
Winson Chungaafa03c2010-06-11 17:34:16 -07001416
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001417 /**
Winson Chungaafa03c2010-06-11 17:34:16 -07001418 * Computes the required horizontal and vertical cell spans to always
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001419 * fit the given rectangle.
Winson Chungaafa03c2010-06-11 17:34:16 -07001420 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001421 * @param width Width in pixels
1422 * @param height Height in pixels
Patrick Dubroy8f86ddc2010-07-16 13:55:32 -07001423 * @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 -08001424 */
Patrick Dubroy8f86ddc2010-07-16 13:55:32 -07001425 public int[] rectToCell(int width, int height, int[] result) {
Michael Jurka9987a5c2010-10-08 16:58:12 -07001426 return rectToCell(getResources(), width, height, result);
1427 }
1428
1429 public static int[] rectToCell(Resources resources, int width, int height, int[] result) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001430 // Always assume we're working with the smallest span to make sure we
1431 // reserve enough space in both orientations.
Joe Onorato79e56262009-09-21 15:23:04 -04001432 int actualWidth = resources.getDimensionPixelSize(R.dimen.workspace_cell_width);
1433 int actualHeight = resources.getDimensionPixelSize(R.dimen.workspace_cell_height);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001434 int smallerSize = Math.min(actualWidth, actualHeight);
Joe Onorato79e56262009-09-21 15:23:04 -04001435
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001436 // Always round up to next largest cell
1437 int spanX = (width + smallerSize) / smallerSize;
1438 int spanY = (height + smallerSize) / smallerSize;
Joe Onorato79e56262009-09-21 15:23:04 -04001439
Patrick Dubroy8f86ddc2010-07-16 13:55:32 -07001440 if (result == null) {
1441 return new int[] { spanX, spanY };
1442 }
1443 result[0] = spanX;
1444 result[1] = spanY;
1445 return result;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001446 }
1447
Michael Jurkaf12c75c2011-01-25 22:41:40 -08001448 public int[] cellSpansToSize(int hSpans, int vSpans) {
1449 int[] size = new int[2];
1450 size[0] = hSpans * mCellWidth + (hSpans - 1) * mWidthGap;
1451 size[1] = vSpans * mCellHeight + (vSpans - 1) * mHeightGap;
1452 return size;
1453 }
1454
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001455 /**
Patrick Dubroy047379a2010-12-19 22:02:04 -08001456 * Calculate the grid spans needed to fit given item
1457 */
1458 public void calculateSpans(ItemInfo info) {
1459 final int minWidth;
1460 final int minHeight;
1461
1462 if (info instanceof LauncherAppWidgetInfo) {
1463 minWidth = ((LauncherAppWidgetInfo) info).minWidth;
1464 minHeight = ((LauncherAppWidgetInfo) info).minHeight;
1465 } else if (info instanceof PendingAddWidgetInfo) {
1466 minWidth = ((PendingAddWidgetInfo) info).minWidth;
1467 minHeight = ((PendingAddWidgetInfo) info).minHeight;
1468 } else {
1469 // It's not a widget, so it must be 1x1
1470 info.spanX = info.spanY = 1;
1471 return;
1472 }
1473 int[] spans = rectToCell(minWidth, minHeight, null);
1474 info.spanX = spans[0];
1475 info.spanY = spans[1];
1476 }
1477
1478 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001479 * Find the first vacant cell, if there is one.
1480 *
1481 * @param vacant Holds the x and y coordinate of the vacant cell
1482 * @param spanX Horizontal cell span.
1483 * @param spanY Vertical cell span.
Winson Chungaafa03c2010-06-11 17:34:16 -07001484 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001485 * @return True if a vacant cell was found
1486 */
1487 public boolean getVacantCell(int[] vacant, int spanX, int spanY) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001488
Michael Jurka0280c3b2010-09-17 15:00:07 -07001489 return findVacantCell(vacant, spanX, spanY, mCountX, mCountY, mOccupied);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001490 }
1491
1492 static boolean findVacantCell(int[] vacant, int spanX, int spanY,
1493 int xCount, int yCount, boolean[][] occupied) {
1494
1495 for (int x = 0; x < xCount; x++) {
1496 for (int y = 0; y < yCount; y++) {
1497 boolean available = !occupied[x][y];
1498out: for (int i = x; i < x + spanX - 1 && x < xCount; i++) {
1499 for (int j = y; j < y + spanY - 1 && y < yCount; j++) {
1500 available = available && !occupied[i][j];
1501 if (!available) break out;
1502 }
1503 }
1504
1505 if (available) {
1506 vacant[0] = x;
1507 vacant[1] = y;
1508 return true;
1509 }
1510 }
1511 }
1512
1513 return false;
1514 }
1515
Michael Jurka0280c3b2010-09-17 15:00:07 -07001516 private void clearOccupiedCells() {
1517 for (int x = 0; x < mCountX; x++) {
1518 for (int y = 0; y < mCountY; y++) {
1519 mOccupied[x][y] = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001520 }
1521 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001522 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001523
Adam Cohen1b607ed2011-03-03 17:26:50 -08001524 /**
1525 * Given a view, determines how much that view can be expanded in all directions, in terms of
1526 * whether or not there are other items occupying adjacent cells. Used by the
1527 * AppWidgetResizeFrame to determine how the widget can be resized.
1528 */
Adam Cohend4844c32011-02-18 19:25:06 -08001529 public void getExpandabilityArrayForView(View view, int[] expandability) {
Adam Cohen1b607ed2011-03-03 17:26:50 -08001530 final LayoutParams lp = (LayoutParams) view.getLayoutParams();
Adam Cohend4844c32011-02-18 19:25:06 -08001531 boolean flag;
1532
Adam Cohen1b607ed2011-03-03 17:26:50 -08001533 expandability[AppWidgetResizeFrame.LEFT] = 0;
Adam Cohend4844c32011-02-18 19:25:06 -08001534 for (int x = lp.cellX - 1; x >= 0; x--) {
1535 flag = false;
1536 for (int y = lp.cellY; y < lp.cellY + lp.cellVSpan; y++) {
1537 if (mOccupied[x][y]) flag = true;
1538 }
1539 if (flag) break;
Adam Cohen1b607ed2011-03-03 17:26:50 -08001540 expandability[AppWidgetResizeFrame.LEFT]++;
Adam Cohend4844c32011-02-18 19:25:06 -08001541 }
1542
Adam Cohen1b607ed2011-03-03 17:26:50 -08001543 expandability[AppWidgetResizeFrame.TOP] = 0;
Adam Cohend4844c32011-02-18 19:25:06 -08001544 for (int y = lp.cellY - 1; y >= 0; y--) {
1545 flag = false;
1546 for (int x = lp.cellX; x < lp.cellX + lp.cellHSpan; x++) {
1547 if (mOccupied[x][y]) flag = true;
1548 }
1549 if (flag) break;
Adam Cohen1b607ed2011-03-03 17:26:50 -08001550 expandability[AppWidgetResizeFrame.TOP]++;
1551 }
Adam Cohend4844c32011-02-18 19:25:06 -08001552
Adam Cohen1b607ed2011-03-03 17:26:50 -08001553 expandability[AppWidgetResizeFrame.RIGHT] = 0;
Adam Cohend4844c32011-02-18 19:25:06 -08001554 for (int x = lp.cellX + lp.cellHSpan; x < mCountX; x++) {
1555 flag = false;
1556 for (int y = lp.cellY; y < lp.cellY + lp.cellVSpan; y++) {
1557 if (mOccupied[x][y]) flag = true;
1558 }
1559 if (flag) break;
Adam Cohen1b607ed2011-03-03 17:26:50 -08001560 expandability[AppWidgetResizeFrame.RIGHT]++;
1561 }
Adam Cohend4844c32011-02-18 19:25:06 -08001562
Adam Cohen1b607ed2011-03-03 17:26:50 -08001563 expandability[AppWidgetResizeFrame.BOTTOM] = 0;
Adam Cohend4844c32011-02-18 19:25:06 -08001564 for (int y = lp.cellY + lp.cellVSpan; y < mCountY; y++) {
1565 flag = false;
1566 for (int x = lp.cellX; x < lp.cellX + lp.cellHSpan; x++) {
1567 if (mOccupied[x][y]) flag = true;
1568 }
1569 if (flag) break;
Adam Cohen1b607ed2011-03-03 17:26:50 -08001570 expandability[AppWidgetResizeFrame.BOTTOM]++;
1571 }
Adam Cohend4844c32011-02-18 19:25:06 -08001572 }
1573
Michael Jurka0280c3b2010-09-17 15:00:07 -07001574 public void onMove(View view, int newCellX, int newCellY) {
1575 LayoutParams lp = (LayoutParams) view.getLayoutParams();
1576 markCellsAsUnoccupiedForView(view);
1577 markCellsForView(newCellX, newCellY, lp.cellHSpan, lp.cellVSpan, true);
1578 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001579
Adam Cohend4844c32011-02-18 19:25:06 -08001580 public void markCellsAsOccupiedForView(View view) {
Michael Jurka8c920dd2011-01-20 14:16:56 -08001581 if (view == null || view.getParent() != mChildren) return;
Michael Jurka0280c3b2010-09-17 15:00:07 -07001582 LayoutParams lp = (LayoutParams) view.getLayoutParams();
1583 markCellsForView(lp.cellX, lp.cellY, lp.cellHSpan, lp.cellVSpan, true);
1584 }
1585
Adam Cohend4844c32011-02-18 19:25:06 -08001586 public void markCellsAsUnoccupiedForView(View view) {
Michael Jurka8c920dd2011-01-20 14:16:56 -08001587 if (view == null || view.getParent() != mChildren) return;
Michael Jurka0280c3b2010-09-17 15:00:07 -07001588 LayoutParams lp = (LayoutParams) view.getLayoutParams();
1589 markCellsForView(lp.cellX, lp.cellY, lp.cellHSpan, lp.cellVSpan, false);
1590 }
1591
1592 private void markCellsForView(int cellX, int cellY, int spanX, int spanY, boolean value) {
1593 for (int x = cellX; x < cellX + spanX && x < mCountX; x++) {
1594 for (int y = cellY; y < cellY + spanY && y < mCountY; y++) {
1595 mOccupied[x][y] = value;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001596 }
1597 }
1598 }
1599
Michael Jurka66d72172011-04-12 16:29:25 -07001600 public boolean isOccupied(int x, int y) {
1601 if (x < mCountX && y < mCountY) {
1602 return mOccupied[x][y];
1603 } else {
1604 throw new RuntimeException("Position exceeds the bound of this CellLayout");
1605 }
1606 }
1607
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001608 @Override
1609 public ViewGroup.LayoutParams generateLayoutParams(AttributeSet attrs) {
1610 return new CellLayout.LayoutParams(getContext(), attrs);
1611 }
1612
1613 @Override
1614 protected boolean checkLayoutParams(ViewGroup.LayoutParams p) {
1615 return p instanceof CellLayout.LayoutParams;
1616 }
1617
1618 @Override
1619 protected ViewGroup.LayoutParams generateLayoutParams(ViewGroup.LayoutParams p) {
1620 return new CellLayout.LayoutParams(p);
1621 }
1622
Winson Chungaafa03c2010-06-11 17:34:16 -07001623 public static class CellLayoutAnimationController extends LayoutAnimationController {
1624 public CellLayoutAnimationController(Animation animation, float delay) {
1625 super(animation, delay);
1626 }
1627
1628 @Override
1629 protected long getDelayForView(View view) {
1630 return (int) (Math.random() * 150);
1631 }
1632 }
1633
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001634 public static class LayoutParams extends ViewGroup.MarginLayoutParams {
1635 /**
1636 * Horizontal location of the item in the grid.
1637 */
1638 @ViewDebug.ExportedProperty
1639 public int cellX;
1640
1641 /**
1642 * Vertical location of the item in the grid.
1643 */
1644 @ViewDebug.ExportedProperty
1645 public int cellY;
1646
1647 /**
1648 * Number of cells spanned horizontally by the item.
1649 */
1650 @ViewDebug.ExportedProperty
1651 public int cellHSpan;
1652
1653 /**
1654 * Number of cells spanned vertically by the item.
1655 */
1656 @ViewDebug.ExportedProperty
1657 public int cellVSpan;
Winson Chungaafa03c2010-06-11 17:34:16 -07001658
Adam Cohen1b607ed2011-03-03 17:26:50 -08001659 /**
1660 * Indicates whether the item will set its x, y, width and height parameters freely,
1661 * or whether these will be computed based on cellX, cellY, cellHSpan and cellVSpan.
1662 */
Adam Cohend4844c32011-02-18 19:25:06 -08001663 public boolean isLockedToGrid = true;
1664
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001665 /**
1666 * Is this item currently being dragged
1667 */
1668 public boolean isDragging;
1669
1670 // X coordinate of the view in the layout.
1671 @ViewDebug.ExportedProperty
1672 int x;
1673 // Y coordinate of the view in the layout.
1674 @ViewDebug.ExportedProperty
1675 int y;
1676
Patrick Dubroyce34a972010-10-19 10:34:32 -07001677 /**
1678 * The old X coordinate of this item, relative to its current parent.
1679 * Used to animate the item into its new position.
1680 */
1681 int oldX;
1682
1683 /**
1684 * The old Y coordinate of this item, relative to its current parent.
1685 * Used to animate the item into its new position.
1686 */
1687 int oldY;
1688
Romain Guy84f296c2009-11-04 15:00:44 -08001689 boolean dropped;
Romain Guyfcb9e712009-10-02 16:06:52 -07001690
Michael Jurkad3ef3062010-11-23 16:23:58 -08001691 boolean animateDrop;
1692
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001693 public LayoutParams(Context c, AttributeSet attrs) {
1694 super(c, attrs);
1695 cellHSpan = 1;
1696 cellVSpan = 1;
1697 }
1698
1699 public LayoutParams(ViewGroup.LayoutParams source) {
1700 super(source);
1701 cellHSpan = 1;
1702 cellVSpan = 1;
1703 }
Winson Chungaafa03c2010-06-11 17:34:16 -07001704
1705 public LayoutParams(LayoutParams source) {
1706 super(source);
1707 this.cellX = source.cellX;
1708 this.cellY = source.cellY;
1709 this.cellHSpan = source.cellHSpan;
1710 this.cellVSpan = source.cellVSpan;
1711 }
1712
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001713 public LayoutParams(int cellX, int cellY, int cellHSpan, int cellVSpan) {
Romain Guy8f19cdd2010-01-08 15:07:00 -08001714 super(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001715 this.cellX = cellX;
1716 this.cellY = cellY;
1717 this.cellHSpan = cellHSpan;
1718 this.cellVSpan = cellVSpan;
1719 }
1720
Adam Cohen7f4eabe2011-04-21 16:19:16 -07001721 public void setup(int cellWidth, int cellHeight, int widthGap, int heightGap) {
Adam Cohend4844c32011-02-18 19:25:06 -08001722 if (isLockedToGrid) {
1723 final int myCellHSpan = cellHSpan;
1724 final int myCellVSpan = cellVSpan;
1725 final int myCellX = cellX;
1726 final int myCellY = cellY;
Adam Cohen1b607ed2011-03-03 17:26:50 -08001727
Adam Cohend4844c32011-02-18 19:25:06 -08001728 width = myCellHSpan * cellWidth + ((myCellHSpan - 1) * widthGap) -
1729 leftMargin - rightMargin;
1730 height = myCellVSpan * cellHeight + ((myCellVSpan - 1) * heightGap) -
1731 topMargin - bottomMargin;
Adam Cohen7f4eabe2011-04-21 16:19:16 -07001732 x = myCellX * (cellWidth + widthGap) + leftMargin;
1733 y = myCellY * (cellHeight + heightGap) + topMargin;
Adam Cohend4844c32011-02-18 19:25:06 -08001734 }
1735 }
Winson Chungaafa03c2010-06-11 17:34:16 -07001736
Winson Chungaafa03c2010-06-11 17:34:16 -07001737 public String toString() {
1738 return "(" + this.cellX + ", " + this.cellY + ")";
1739 }
Adam Cohen7f4eabe2011-04-21 16:19:16 -07001740
1741 public void setWidth(int width) {
1742 this.width = width;
1743 }
1744
1745 public int getWidth() {
1746 return width;
1747 }
1748
1749 public void setHeight(int height) {
1750 this.height = height;
1751 }
1752
1753 public int getHeight() {
1754 return height;
1755 }
1756
1757 public void setX(int x) {
1758 this.x = x;
1759 }
1760
1761 public int getX() {
1762 return x;
1763 }
1764
1765 public void setY(int y) {
1766 this.y = y;
1767 }
1768
1769 public int getY() {
1770 return y;
1771 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001772 }
1773
Michael Jurka0280c3b2010-09-17 15:00:07 -07001774 // This class stores info for two purposes:
1775 // 1. When dragging items (mDragInfo in Workspace), we store the View, its cellX & cellY,
1776 // its spanX, spanY, and the screen it is on
1777 // 2. When long clicking on an empty cell in a CellLayout, we save information about the
1778 // cellX and cellY coordinates and which page was clicked. We then set this as a tag on
1779 // the CellLayout that was long clicked
Michael Jurkae5fb0f22011-04-11 13:27:46 -07001780 static final class CellInfo {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001781 View cell;
Michael Jurkaa63c4522010-08-19 13:52:27 -07001782 int cellX = -1;
1783 int cellY = -1;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001784 int spanX;
1785 int spanY;
1786 int screen;
1787 boolean valid;
1788
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001789 @Override
1790 public String toString() {
Winson Chungaafa03c2010-06-11 17:34:16 -07001791 return "Cell[view=" + (cell == null ? "null" : cell.getClass())
1792 + ", x=" + cellX + ", y=" + cellY + "]";
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001793 }
1794 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001795}